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
16 #include <linux/config.h>
17 #include <linux/string.h>
18 #include <linux/net.h>
19 #include <linux/socket.h>
20 #include <linux/sockios.h>
21 #include <linux/init.h>
22 #include <linux/skbuff.h>
23 #include <linux/netlink.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/proc_fs.h>
26 #include <linux/netdevice.h>
27 #include <linux/timer.h>
28 #include <linux/spinlock.h>
29 #include <linux/in_route.h>
30 #include <asm/atomic.h>
31 #include <asm/uaccess.h>
32 #include <net/neighbour.h>
36 #include <net/dn_fib.h>
37 #include <net/dn_neigh.h>
38 #include <net/dn_dev.h>
42 struct dn_fib_rule *r_next;
45 unsigned char r_table;
46 unsigned char r_action;
47 unsigned char r_dst_len;
48 unsigned char r_src_len;
55 #ifdef CONFIG_DECNET_ROUTE_FWMARK
59 char r_ifname[IFNAMSIZ];
63 static struct dn_fib_rule default_rule = {
64 .r_clntref = ATOMIC_INIT(2),
65 .r_preference = 0x7fff,
66 .r_table = RT_TABLE_MAIN,
67 .r_action = RTN_UNICAST
70 static struct dn_fib_rule *dn_fib_rules = &default_rule;
71 static rwlock_t dn_fib_rules_lock = RW_LOCK_UNLOCKED;
74 int dn_fib_rtm_delrule(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
76 struct rtattr **rta = arg;
77 struct rtmsg *rtm = NLMSG_DATA(nlh);
78 struct dn_fib_rule *r, **rp;
81 for(rp=&dn_fib_rules; (r=*rp) != NULL; rp = &r->r_next) {
82 if ((!rta[RTA_SRC-1] || memcmp(RTA_DATA(rta[RTA_SRC-1]), &r->r_src, 2) == 0) &&
83 rtm->rtm_src_len == r->r_src_len &&
84 rtm->rtm_dst_len == r->r_dst_len &&
85 (!rta[RTA_DST-1] || memcmp(RTA_DATA(rta[RTA_DST-1]), &r->r_dst, 2) == 0) &&
86 #ifdef CONFIG_DECNET_ROUTE_FWMARK
87 (!rta[RTA_PROTOINFO-1] || memcmp(RTA_DATA(rta[RTA_PROTOINFO-1]), &r->r_fwmark, 4) == 0) &&
89 (!rtm->rtm_type || rtm->rtm_type == r->r_action) &&
90 (!rta[RTA_PRIORITY-1] || memcmp(RTA_DATA(rta[RTA_PRIORITY-1]), &r->r_preference, 4) == 0) &&
91 (!rta[RTA_IIF-1] || strcmp(RTA_DATA(rta[RTA_IIF-1]), r->r_ifname) == 0) &&
92 (!rtm->rtm_table || (r && rtm->rtm_table == r->r_table))) {
95 if (r == &default_rule)
98 write_lock_bh(&dn_fib_rules_lock);
101 write_unlock_bh(&dn_fib_rules_lock);
111 void dn_fib_rule_put(struct dn_fib_rule *r)
113 if (atomic_dec_and_test(&r->r_clntref)) {
117 printk(KERN_DEBUG "Attempt to free alive dn_fib_rule\n");
122 int dn_fib_rtm_newrule(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
124 struct rtattr **rta = arg;
125 struct rtmsg *rtm = NLMSG_DATA(nlh);
126 struct dn_fib_rule *r, *new_r, **rp;
127 unsigned char table_id;
129 if (rtm->rtm_src_len > 16 || rtm->rtm_dst_len > 16)
132 if (rta[RTA_IIF-1] && RTA_PAYLOAD(rta[RTA_IIF-1]) > IFNAMSIZ)
135 if (rtm->rtm_type == RTN_NAT)
138 table_id = rtm->rtm_table;
139 if (table_id == RT_TABLE_UNSPEC) {
140 struct dn_fib_table *tb;
141 if (rtm->rtm_type == RTN_UNICAST) {
142 if ((tb = dn_fib_empty_table()) == NULL)
148 new_r = kmalloc(sizeof(*new_r), GFP_KERNEL);
151 memset(new_r, 0, sizeof(*new_r));
153 memcpy(&new_r->r_src, RTA_DATA(rta[RTA_SRC-1]), 2);
155 memcpy(&new_r->r_dst, RTA_DATA(rta[RTA_DST-1]), 2);
156 if (rta[RTA_GATEWAY-1])
157 memcpy(&new_r->r_srcmap, RTA_DATA(rta[RTA_GATEWAY-1]), 2);
158 new_r->r_src_len = rtm->rtm_src_len;
159 new_r->r_dst_len = rtm->rtm_dst_len;
160 new_r->r_srcmask = dnet_make_mask(rtm->rtm_src_len);
161 new_r->r_dstmask = dnet_make_mask(rtm->rtm_dst_len);
162 #ifdef CONFIG_DECNET_ROUTE_FWMARK
163 if (rta[RTA_PROTOINFO-1])
164 memcpy(&new_r->r_fwmark, RTA_DATA(rta[RTA_PROTOINFO-1]), 4);
166 new_r->r_action = rtm->rtm_type;
167 new_r->r_flags = rtm->rtm_flags;
168 if (rta[RTA_PRIORITY-1])
169 memcpy(&new_r->r_preference, RTA_DATA(rta[RTA_PRIORITY-1]), 4);
170 new_r->r_table = table_id;
171 if (rta[RTA_IIF-1]) {
172 struct net_device *dev;
173 memcpy(new_r->r_ifname, RTA_DATA(rta[RTA_IIF-1]), IFNAMSIZ);
174 new_r->r_ifname[IFNAMSIZ-1] = 0;
175 new_r->r_ifindex = -1;
176 dev = dev_get_by_name(new_r->r_ifname);
178 new_r->r_ifindex = dev->ifindex;
184 if (!new_r->r_preference) {
186 if (r && (r = r->r_next) != NULL) {
187 rp = &dn_fib_rules->r_next;
189 new_r->r_preference = r->r_preference - 1;
193 while((r=*rp) != NULL) {
194 if (r->r_preference > new_r->r_preference)
200 atomic_inc(&new_r->r_clntref);
201 write_lock_bh(&dn_fib_rules_lock);
203 write_unlock_bh(&dn_fib_rules_lock);
208 int dn_fib_lookup(const struct flowi *flp, struct dn_fib_res *res)
210 struct dn_fib_rule *r, *policy;
211 struct dn_fib_table *tb;
212 dn_address saddr = flp->fld_src;
213 dn_address daddr = flp->fld_dst;
216 read_lock(&dn_fib_rules_lock);
217 for(r = dn_fib_rules; r; r = r->r_next) {
218 if (((saddr^r->r_src) & r->r_srcmask) ||
219 ((daddr^r->r_dst) & r->r_dstmask) ||
220 #ifdef CONFIG_DECNET_ROUTE_FWMARK
221 (r->r_fwmark && r->r_fwmark != flp->fld_fwmark) ||
223 (r->r_ifindex && r->r_ifindex != flp->iif))
226 switch(r->r_action) {
231 case RTN_UNREACHABLE:
232 read_unlock(&dn_fib_rules_lock);
236 read_unlock(&dn_fib_rules_lock);
239 read_unlock(&dn_fib_rules_lock);
243 if ((tb = dn_fib_get_table(r->r_table, 0)) == NULL)
245 err = tb->lookup(tb, flp, res);
249 atomic_inc(&policy->r_clntref);
250 read_unlock(&dn_fib_rules_lock);
253 if (err < 0 && err != -EAGAIN) {
254 read_unlock(&dn_fib_rules_lock);
259 read_unlock(&dn_fib_rules_lock);
263 unsigned dnet_addr_type(__u16 addr)
265 struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
266 struct dn_fib_res res;
267 unsigned ret = RTN_UNICAST;
268 struct dn_fib_table *tb = dn_fib_tables[RT_TABLE_LOCAL];
273 if (!tb->lookup(tb, &fl, &res)) {
275 dn_fib_res_put(&res);
281 __u16 dn_fib_rules_policy(__u16 saddr, struct dn_fib_res *res, unsigned *flags)
283 struct dn_fib_rule *r = res->r;
285 if (r->r_action == RTN_NAT) {
286 int addrtype = dnet_addr_type(r->r_srcmap);
288 if (addrtype == RTN_NAT) {
289 saddr = (saddr&~r->r_srcmask)|r->r_srcmap;
291 } else if (addrtype == RTN_LOCAL || r->r_srcmap == 0) {
299 static void dn_fib_rules_detach(struct net_device *dev)
301 struct dn_fib_rule *r;
303 for(r = dn_fib_rules; r; r = r->r_next) {
304 if (r->r_ifindex == dev->ifindex) {
305 write_lock_bh(&dn_fib_rules_lock);
307 write_unlock_bh(&dn_fib_rules_lock);
312 static void dn_fib_rules_attach(struct net_device *dev)
314 struct dn_fib_rule *r;
316 for(r = dn_fib_rules; r; r = r->r_next) {
317 if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0) {
318 write_lock_bh(&dn_fib_rules_lock);
319 r->r_ifindex = dev->ifindex;
320 write_unlock_bh(&dn_fib_rules_lock);
325 static int dn_fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr)
327 struct net_device *dev = ptr;
330 case NETDEV_UNREGISTER:
331 dn_fib_rules_detach(dev);
332 dn_fib_sync_down(0, dev, 1);
333 case NETDEV_REGISTER:
334 dn_fib_rules_attach(dev);
342 static struct notifier_block dn_fib_rules_notifier = {
343 .notifier_call = dn_fib_rules_event,
346 static int dn_fib_fill_rule(struct sk_buff *skb, struct dn_fib_rule *r, struct netlink_callback *cb)
349 struct nlmsghdr *nlh;
350 unsigned char *b = skb->tail;
353 nlh = NLMSG_PUT(skb, NETLINK_CREDS(cb->skb)->pid, cb->nlh->nlmsg_seq, RTM_NEWRULE, sizeof(*rtm));
354 rtm = NLMSG_DATA(nlh);
355 rtm->rtm_family = AF_DECnet;
356 rtm->rtm_dst_len = r->r_dst_len;
357 rtm->rtm_src_len = r->r_src_len;
359 #ifdef CONFIG_DECNET_ROUTE_FWMARK
361 RTA_PUT(skb, RTA_PROTOINFO, 4, &r->r_fwmark);
363 rtm->rtm_table = r->r_table;
364 rtm->rtm_protocol = 0;
366 rtm->rtm_type = r->r_action;
367 rtm->rtm_flags = r->r_flags;
370 RTA_PUT(skb, RTA_DST, 2, &r->r_dst);
372 RTA_PUT(skb, RTA_SRC, 2, &r->r_src);
374 RTA_PUT(skb, RTA_IIF, IFNAMSIZ, &r->r_ifname);
376 RTA_PUT(skb, RTA_PRIORITY, 4, &r->r_preference);
378 RTA_PUT(skb, RTA_GATEWAY, 2, &r->r_srcmap);
379 nlh->nlmsg_len = skb->tail - b;
384 skb_trim(skb, b - skb->data);
388 int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
391 int s_idx = cb->args[0];
392 struct dn_fib_rule *r;
394 read_lock(&dn_fib_rules_lock);
395 for(r = dn_fib_rules, idx = 0; r; r = r->r_next, idx++) {
398 if (dn_fib_fill_rule(skb, r, cb) < 0)
401 read_unlock(&dn_fib_rules_lock);
407 void __init dn_fib_rules_init(void)
409 register_netdevice_notifier(&dn_fib_rules_notifier);
412 void __exit dn_fib_rules_cleanup(void)
414 unregister_netdevice_notifier(&dn_fib_rules_notifier);