3 * DECnet An implementation of the DECnet protocol suite for the LINUX
4 * operating system. DECnet is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * DECnet Routing Forwarding Information Base (Rules)
9 * Author: Steve Whitehouse <SteveW@ACM.org>
10 * Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c
14 * Steve Whitehouse <steve@chygwyn.com>
15 * Updated for Thomas Graf's generic rules
18 #include <linux/net.h>
19 #include <linux/init.h>
20 #include <linux/netlink.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/netdevice.h>
23 #include <linux/spinlock.h>
24 #include <linux/list.h>
25 #include <linux/rcupdate.h>
26 #include <net/neighbour.h>
29 #include <net/fib_rules.h>
31 #include <net/dn_fib.h>
32 #include <net/dn_neigh.h>
33 #include <net/dn_dev.h>
35 static struct fib_rules_ops dn_fib_rules_ops;
39 struct fib_rule common;
40 unsigned char dst_len;
41 unsigned char src_len;
52 static struct dn_fib_rule default_rule = {
54 .refcnt = ATOMIC_INIT(2),
56 .table = RT_TABLE_MAIN,
57 .action = FR_ACT_TO_TBL,
61 static LIST_HEAD(dn_fib_rules);
64 int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res)
66 struct fib_lookup_arg arg = {
71 err = fib_rules_lookup(&dn_fib_rules_ops, flp, 0, &arg);
77 static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
78 int flags, struct fib_lookup_arg *arg)
81 struct dn_fib_table *tbl;
83 switch(rule->action) {
87 case FR_ACT_UNREACHABLE:
95 case FR_ACT_BLACKHOLE:
101 tbl = dn_fib_get_table(rule->table, 0);
105 err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result);
112 static struct nla_policy dn_fib_rule_policy[FRA_MAX+1] __read_mostly = {
113 [FRA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
114 [FRA_PRIORITY] = { .type = NLA_U32 },
115 [FRA_SRC] = { .type = NLA_U16 },
116 [FRA_DST] = { .type = NLA_U16 },
117 [FRA_FWMARK] = { .type = NLA_U32 },
118 [FRA_FWMASK] = { .type = NLA_U32 },
119 [FRA_TABLE] = { .type = NLA_U32 },
122 static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
124 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
125 __le16 daddr = fl->fld_dst;
126 __le16 saddr = fl->fld_src;
128 if (((saddr ^ r->src) & r->srcmask) ||
129 ((daddr ^ r->dst) & r->dstmask))
132 if ((r->fwmark ^ fl->mark) & r->fwmask)
138 static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
139 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
143 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
145 if (frh->src_len > 16 || frh->dst_len > 16 || frh->tos)
148 if (rule->table == RT_TABLE_UNSPEC) {
149 if (rule->action == FR_ACT_TO_TBL) {
150 struct dn_fib_table *table;
152 table = dn_fib_empty_table();
158 rule->table = table->n;
163 r->src = nla_get_u16(tb[FRA_SRC]);
166 r->dst = nla_get_u16(tb[FRA_DST]);
168 if (tb[FRA_FWMARK]) {
169 r->fwmark = nla_get_u32(tb[FRA_FWMARK]);
171 /* compatibility: if the mark value is non-zero all bits
172 * are compared unless a mask is explicitly specified.
174 r->fwmask = 0xFFFFFFFF;
178 r->fwmask = nla_get_u32(tb[FRA_FWMASK]);
180 r->src_len = frh->src_len;
181 r->srcmask = dnet_make_mask(r->src_len);
182 r->dst_len = frh->dst_len;
183 r->dstmask = dnet_make_mask(r->dst_len);
189 static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
192 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
194 if (frh->src_len && (r->src_len != frh->src_len))
197 if (frh->dst_len && (r->dst_len != frh->dst_len))
200 if (tb[FRA_FWMARK] && (r->fwmark != nla_get_u32(tb[FRA_FWMARK])))
203 if (tb[FRA_FWMASK] && (r->fwmask != nla_get_u32(tb[FRA_FWMASK])))
206 if (tb[FRA_SRC] && (r->src != nla_get_u16(tb[FRA_SRC])))
209 if (tb[FRA_DST] && (r->dst != nla_get_u16(tb[FRA_DST])))
215 unsigned dnet_addr_type(__le16 addr)
217 struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
218 struct dn_fib_res res;
219 unsigned ret = RTN_UNICAST;
220 struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
225 if (!tb->lookup(tb, &fl, &res)) {
227 dn_fib_res_put(&res);
233 static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
234 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
236 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
238 frh->family = AF_DECnet;
239 frh->dst_len = r->dst_len;
240 frh->src_len = r->src_len;
244 NLA_PUT_U32(skb, FRA_FWMARK, r->fwmark);
245 if (r->fwmask || r->fwmark)
246 NLA_PUT_U32(skb, FRA_FWMASK, r->fwmask);
248 NLA_PUT_U16(skb, FRA_DST, r->dst);
250 NLA_PUT_U16(skb, FRA_SRC, r->src);
258 static u32 dn_fib_rule_default_pref(void)
260 struct list_head *pos;
261 struct fib_rule *rule;
263 if (!list_empty(&dn_fib_rules)) {
264 pos = dn_fib_rules.next;
265 if (pos->next != &dn_fib_rules) {
266 rule = list_entry(pos->next, struct fib_rule, list);
268 return rule->pref - 1;
275 int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
277 return fib_rules_dump(skb, cb, AF_DECnet);
280 static struct fib_rules_ops dn_fib_rules_ops = {
282 .rule_size = sizeof(struct dn_fib_rule),
283 .action = dn_fib_rule_action,
284 .match = dn_fib_rule_match,
285 .configure = dn_fib_rule_configure,
286 .compare = dn_fib_rule_compare,
287 .fill = dn_fib_rule_fill,
288 .default_pref = dn_fib_rule_default_pref,
289 .nlgroup = RTNLGRP_DECnet_RULE,
290 .policy = dn_fib_rule_policy,
291 .rules_list = &dn_fib_rules,
292 .owner = THIS_MODULE,
295 void __init dn_fib_rules_init(void)
297 list_add_tail(&default_rule.common.list, &dn_fib_rules);
298 fib_rules_register(&dn_fib_rules_ops);
301 void __exit dn_fib_rules_cleanup(void)
303 fib_rules_unregister(&dn_fib_rules_ops);