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>
34 #include <net/dn_route.h>
36 static struct fib_rules_ops dn_fib_rules_ops;
40 struct fib_rule common;
41 unsigned char dst_len;
42 unsigned char src_len;
52 int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res)
54 struct fib_lookup_arg arg = {
59 err = fib_rules_lookup(&dn_fib_rules_ops, flp, 0, &arg);
65 static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
66 int flags, struct fib_lookup_arg *arg)
69 struct dn_fib_table *tbl;
71 switch(rule->action) {
75 case FR_ACT_UNREACHABLE:
83 case FR_ACT_BLACKHOLE:
89 tbl = dn_fib_get_table(rule->table, 0);
93 err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result);
100 static const struct nla_policy dn_fib_rule_policy[FRA_MAX+1] = {
104 static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
106 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
107 __le16 daddr = fl->fld_dst;
108 __le16 saddr = fl->fld_src;
110 if (((saddr ^ r->src) & r->srcmask) ||
111 ((daddr ^ r->dst) & r->dstmask))
117 static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
118 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
122 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
127 if (rule->table == RT_TABLE_UNSPEC) {
128 if (rule->action == FR_ACT_TO_TBL) {
129 struct dn_fib_table *table;
131 table = dn_fib_empty_table();
137 rule->table = table->n;
142 r->src = nla_get_le16(tb[FRA_SRC]);
145 r->dst = nla_get_le16(tb[FRA_DST]);
147 r->src_len = frh->src_len;
148 r->srcmask = dnet_make_mask(r->src_len);
149 r->dst_len = frh->dst_len;
150 r->dstmask = dnet_make_mask(r->dst_len);
156 static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
159 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
161 if (frh->src_len && (r->src_len != frh->src_len))
164 if (frh->dst_len && (r->dst_len != frh->dst_len))
167 if (frh->src_len && (r->src != nla_get_le16(tb[FRA_SRC])))
170 if (frh->dst_len && (r->dst != nla_get_le16(tb[FRA_DST])))
176 unsigned dnet_addr_type(__le16 addr)
178 struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
179 struct dn_fib_res res;
180 unsigned ret = RTN_UNICAST;
181 struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
186 if (!tb->lookup(tb, &fl, &res)) {
188 dn_fib_res_put(&res);
194 static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
195 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
197 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
199 frh->family = AF_DECnet;
200 frh->dst_len = r->dst_len;
201 frh->src_len = r->src_len;
205 NLA_PUT_LE16(skb, FRA_DST, r->dst);
207 NLA_PUT_LE16(skb, FRA_SRC, r->src);
215 static u32 dn_fib_rule_default_pref(struct fib_rules_ops *ops)
217 struct list_head *pos;
218 struct fib_rule *rule;
220 if (!list_empty(&dn_fib_rules_ops.rules_list)) {
221 pos = dn_fib_rules_ops.rules_list.next;
222 if (pos->next != &dn_fib_rules_ops.rules_list) {
223 rule = list_entry(pos->next, struct fib_rule, list);
225 return rule->pref - 1;
232 static void dn_fib_rule_flush_cache(struct fib_rules_ops *ops)
234 dn_rt_cache_flush(-1);
237 static struct fib_rules_ops dn_fib_rules_ops = {
239 .rule_size = sizeof(struct dn_fib_rule),
240 .addr_size = sizeof(u16),
241 .action = dn_fib_rule_action,
242 .match = dn_fib_rule_match,
243 .configure = dn_fib_rule_configure,
244 .compare = dn_fib_rule_compare,
245 .fill = dn_fib_rule_fill,
246 .default_pref = dn_fib_rule_default_pref,
247 .flush_cache = dn_fib_rule_flush_cache,
248 .nlgroup = RTNLGRP_DECnet_RULE,
249 .policy = dn_fib_rule_policy,
250 .rules_list = LIST_HEAD_INIT(dn_fib_rules_ops.rules_list),
251 .owner = THIS_MODULE,
252 .fro_net = &init_net,
255 void __init dn_fib_rules_init(void)
257 BUG_ON(fib_default_rule_add(&dn_fib_rules_ops, 0x7fff,
259 fib_rules_register(&dn_fib_rules_ops);
262 void __exit dn_fib_rules_cleanup(void)
264 fib_rules_unregister(&dn_fib_rules_ops);