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;
48 #ifdef CONFIG_DECNET_ROUTE_FWMARK
54 static struct dn_fib_rule default_rule = {
56 .refcnt = ATOMIC_INIT(2),
58 .table = RT_TABLE_MAIN,
59 .action = FR_ACT_TO_TBL,
63 static LIST_HEAD(dn_fib_rules);
66 int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res)
68 struct fib_lookup_arg arg = {
73 err = fib_rules_lookup(&dn_fib_rules_ops, flp, 0, &arg);
79 static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
80 int flags, struct fib_lookup_arg *arg)
83 struct dn_fib_table *tbl;
85 switch(rule->action) {
89 case FR_ACT_UNREACHABLE:
97 case FR_ACT_BLACKHOLE:
103 tbl = dn_fib_get_table(rule->table, 0);
107 err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result);
114 static struct nla_policy dn_fib_rule_policy[FRA_MAX+1] __read_mostly = {
115 [FRA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
116 [FRA_PRIORITY] = { .type = NLA_U32 },
117 [FRA_SRC] = { .type = NLA_U16 },
118 [FRA_DST] = { .type = NLA_U16 },
119 [FRA_FWMARK] = { .type = NLA_U32 },
120 [FRA_FWMASK] = { .type = NLA_U32 },
121 [FRA_TABLE] = { .type = NLA_U32 },
124 static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
126 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
127 u16 daddr = fl->fld_dst;
128 u16 saddr = fl->fld_src;
130 if (((saddr ^ r->src) & r->srcmask) ||
131 ((daddr ^ r->dst) & r->dstmask))
134 #ifdef CONFIG_DECNET_ROUTE_FWMARK
135 if ((r->fwmark ^ fl->fld_fwmark) & r->fwmask)
142 static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
143 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
147 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
149 if (frh->src_len > 16 || frh->dst_len > 16 || frh->tos)
152 if (rule->table == RT_TABLE_UNSPEC) {
153 if (rule->action == FR_ACT_TO_TBL) {
154 struct dn_fib_table *table;
156 table = dn_fib_empty_table();
162 rule->table = table->n;
167 r->src = nla_get_u16(tb[FRA_SRC]);
170 r->dst = nla_get_u16(tb[FRA_DST]);
172 #ifdef CONFIG_DECNET_ROUTE_FWMARK
173 if (tb[FRA_FWMARK]) {
174 r->fwmark = nla_get_u32(tb[FRA_FWMARK]);
176 /* compatibility: if the mark value is non-zero all bits
177 * are compared unless a mask is explicitly specified.
179 r->fwmask = 0xFFFFFFFF;
183 r->fwmask = nla_get_u32(tb[FRA_FWMASK]);
186 r->src_len = frh->src_len;
187 r->srcmask = dnet_make_mask(r->src_len);
188 r->dst_len = frh->dst_len;
189 r->dstmask = dnet_make_mask(r->dst_len);
195 static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
198 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
200 if (frh->src_len && (r->src_len != frh->src_len))
203 if (frh->dst_len && (r->dst_len != frh->dst_len))
206 #ifdef CONFIG_DECNET_ROUTE_FWMARK
207 if (tb[FRA_FWMARK] && (r->fwmark != nla_get_u32(tb[FRA_FWMARK])))
210 if (tb[FRA_FWMASK] && (r->fwmask != nla_get_u32(tb[FRA_FWMASK])))
214 if (tb[FRA_SRC] && (r->src != nla_get_u16(tb[FRA_SRC])))
217 if (tb[FRA_DST] && (r->dst != nla_get_u16(tb[FRA_DST])))
223 unsigned dnet_addr_type(__le16 addr)
225 struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
226 struct dn_fib_res res;
227 unsigned ret = RTN_UNICAST;
228 struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
233 if (!tb->lookup(tb, &fl, &res)) {
235 dn_fib_res_put(&res);
241 static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
242 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
244 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
246 frh->family = AF_DECnet;
247 frh->dst_len = r->dst_len;
248 frh->src_len = r->src_len;
251 #ifdef CONFIG_DECNET_ROUTE_FWMARK
253 NLA_PUT_U32(skb, FRA_FWMARK, r->fwmark);
254 if (r->fwmask || r->fwmark)
255 NLA_PUT_U32(skb, FRA_FWMASK, r->fwmask);
258 NLA_PUT_U16(skb, FRA_DST, r->dst);
260 NLA_PUT_U16(skb, FRA_SRC, r->src);
268 static u32 dn_fib_rule_default_pref(void)
270 struct list_head *pos;
271 struct fib_rule *rule;
273 if (!list_empty(&dn_fib_rules)) {
274 pos = dn_fib_rules.next;
275 if (pos->next != &dn_fib_rules) {
276 rule = list_entry(pos->next, struct fib_rule, list);
278 return rule->pref - 1;
285 int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
287 return fib_rules_dump(skb, cb, AF_DECnet);
290 static struct fib_rules_ops dn_fib_rules_ops = {
292 .rule_size = sizeof(struct dn_fib_rule),
293 .action = dn_fib_rule_action,
294 .match = dn_fib_rule_match,
295 .configure = dn_fib_rule_configure,
296 .compare = dn_fib_rule_compare,
297 .fill = dn_fib_rule_fill,
298 .default_pref = dn_fib_rule_default_pref,
299 .nlgroup = RTNLGRP_DECnet_RULE,
300 .policy = dn_fib_rule_policy,
301 .rules_list = &dn_fib_rules,
302 .owner = THIS_MODULE,
305 void __init dn_fib_rules_init(void)
307 list_add_tail(&default_rule.common.list, &dn_fib_rules);
308 fib_rules_register(&dn_fib_rules_ops);
311 void __exit dn_fib_rules_cleanup(void)
313 fib_rules_unregister(&dn_fib_rules_ops);