2 * DECnet An implementation of the DECnet protocol suite for the LINUX
3 * operating system. DECnet is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * DECnet Routing Forwarding Information Base (Glue/Info List)
8 * Author: Steve Whitehouse <SteveW@ACM.org>
12 * Alexey Kuznetsov : SMP locking changes
13 * Steve Whitehouse : Rewrote it... Well to be more correct, I
14 * copied most of it from the ipv4 fib code.
17 #include <linux/config.h>
18 #include <linux/string.h>
19 #include <linux/net.h>
20 #include <linux/socket.h>
21 #include <linux/sockios.h>
22 #include <linux/init.h>
23 #include <linux/skbuff.h>
24 #include <linux/netlink.h>
25 #include <linux/rtnetlink.h>
26 #include <linux/proc_fs.h>
27 #include <linux/netdevice.h>
28 #include <linux/timer.h>
29 #include <linux/spinlock.h>
30 #include <asm/atomic.h>
31 #include <asm/uaccess.h>
32 #include <net/neighbour.h>
35 #include <net/dn_route.h>
36 #include <net/dn_fib.h>
37 #include <net/dn_neigh.h>
38 #include <net/dn_dev.h>
41 #define for_fib_info() { struct dn_fib_info *fi;\
42 for(fi = dn_fib_info_list; fi; fi = fi->fib_next)
43 #define endfor_fib_info() }
45 #define for_nexthops(fi) { int nhsel; const struct dn_fib_nh *nh;\
46 for(nhsel = 0, nh = (fi)->fib_nh; nhsel < (fi)->fib_nhs; nh++, nhsel++)
48 #define change_nexthops(fi) { int nhsel; struct dn_fib_nh *nh;\
49 for(nhsel = 0, nh = (struct dn_fib_nh *)((fi)->fib_nh); nhsel < (fi)->fib_nhs; nh++, nhsel++)
51 #define endfor_nexthops(fi) }
53 extern int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb);
56 static struct dn_fib_info *dn_fib_info_list;
57 static rwlock_t dn_fib_info_lock = RW_LOCK_UNLOCKED;
64 } dn_fib_props[RTN_MAX+1] = {
65 { 0, RT_SCOPE_NOWHERE }, /* RTN_UNSPEC */
66 { 0, RT_SCOPE_UNIVERSE }, /* RTN_UNICAST */
67 { 0, RT_SCOPE_HOST }, /* RTN_LOCAL */
68 { -EINVAL, RT_SCOPE_NOWHERE }, /* RTN_BROADCAST */
69 { -EINVAL, RT_SCOPE_NOWHERE }, /* RTN_ANYCAST */
70 { -EINVAL, RT_SCOPE_NOWHERE }, /* RTN_MULTICAST */
71 { -EINVAL, RT_SCOPE_UNIVERSE }, /* RTN_BLACKHOLE */
72 { -EHOSTUNREACH, RT_SCOPE_UNIVERSE }, /* RTN_UNREACHABLE */
73 { -EACCES, RT_SCOPE_UNIVERSE }, /* RTN_PROHIBIT */
74 { -EAGAIN, RT_SCOPE_UNIVERSE }, /* RTN_THROW */
75 { -EINVAL, RT_SCOPE_NOWHERE }, /* RTN_NAT */
76 { -EINVAL, RT_SCOPE_NOWHERE } /* RTN_XRESOLVE */
79 void dn_fib_free_info(struct dn_fib_info *fi)
81 if (fi->fib_dead == 0) {
82 printk(KERN_DEBUG "DECnet: BUG! Attempt to free alive dn_fib_info\n");
90 } endfor_nexthops(fi);
95 void dn_fib_release_info(struct dn_fib_info *fi)
97 write_lock(&dn_fib_info_lock);
98 if (fi && --fi->fib_treeref == 0) {
100 fi->fib_next->fib_prev = fi->fib_prev;
102 fi->fib_prev->fib_next = fi->fib_next;
103 if (fi == dn_fib_info_list)
104 dn_fib_info_list = fi->fib_next;
108 write_unlock(&dn_fib_info_lock);
111 static __inline__ int dn_fib_nh_comp(const struct dn_fib_info *fi, const struct dn_fib_info *ofi)
113 const struct dn_fib_nh *onh = ofi->fib_nh;
116 if (nh->nh_oif != onh->nh_oif ||
117 nh->nh_gw != onh->nh_gw ||
118 nh->nh_scope != onh->nh_scope ||
119 nh->nh_weight != onh->nh_weight ||
120 ((nh->nh_flags^onh->nh_flags)&~RTNH_F_DEAD))
123 } endfor_nexthops(fi);
127 static __inline__ struct dn_fib_info *dn_fib_find_info(const struct dn_fib_info *nfi)
130 if (fi->fib_nhs != nfi->fib_nhs)
132 if (nfi->fib_protocol == fi->fib_protocol &&
133 nfi->fib_prefsrc == fi->fib_prefsrc &&
134 nfi->fib_priority == fi->fib_priority &&
135 ((nfi->fib_flags^fi->fib_flags)&~RTNH_F_DEAD) == 0 &&
136 (nfi->fib_nhs == 0 || dn_fib_nh_comp(fi, nfi) == 0))
142 u16 dn_fib_get_attr16(struct rtattr *attr, int attrlen, int type)
144 while(RTA_OK(attr,attrlen)) {
145 if (attr->rta_type == type)
146 return *(u16*)RTA_DATA(attr);
147 attr = RTA_NEXT(attr, attrlen);
153 static int dn_fib_count_nhs(struct rtattr *rta)
156 struct rtnexthop *nhp = RTA_DATA(rta);
157 int nhlen = RTA_PAYLOAD(rta);
159 while(nhlen >= (int)sizeof(struct rtnexthop)) {
160 if ((nhlen -= nhp->rtnh_len) < 0)
163 nhp = RTNH_NEXT(nhp);
169 static int dn_fib_get_nhs(struct dn_fib_info *fi, const struct rtattr *rta, const struct rtmsg *r)
171 struct rtnexthop *nhp = RTA_DATA(rta);
172 int nhlen = RTA_PAYLOAD(rta);
174 change_nexthops(fi) {
175 int attrlen = nhlen - sizeof(struct rtnexthop);
176 if (attrlen < 0 || (nhlen -= nhp->rtnh_len) < 0)
179 nh->nh_flags = (r->rtm_flags&~0xFF) | nhp->rtnh_flags;
180 nh->nh_oif = nhp->rtnh_ifindex;
181 nh->nh_weight = nhp->rtnh_hops + 1;
184 nh->nh_gw = dn_fib_get_attr16(RTNH_DATA(nhp), attrlen, RTA_GATEWAY);
186 nhp = RTNH_NEXT(nhp);
187 } endfor_nexthops(fi);
193 static int dn_fib_check_nh(const struct rtmsg *r, struct dn_fib_info *fi, struct dn_fib_nh *nh)
198 struct dn_fib_key key;
199 struct dn_fib_res res;
201 if (nh->nh_flags&RTNH_F_ONLINK) {
202 struct net_device *dev;
204 if (r->rtm_scope >= RT_SCOPE_LINK)
206 if ((dev = __dev_get_by_index(nh->nh_oif)) == NULL)
208 if (!(dev->flags&IFF_UP))
211 atomic_inc(&dev->refcnt);
212 nh->nh_scope = RT_SCOPE_LINK;
216 memset(&key, 0, sizeof(key));
218 key.oif = nh->nh_oif;
219 key.scope = r->rtm_scope + 1;
221 if (key.scope < RT_SCOPE_LINK)
222 key.scope = RT_SCOPE_LINK;
224 if ((err = dn_fib_lookup(&key, &res)) != 0)
227 nh->nh_scope = res.scope;
228 nh->nh_oif = DN_FIB_RES_OIF(res);
229 nh->nh_dev = DN_FIB_RES_DEV(res);
231 atomic_inc(&nh->nh_dev->refcnt);
232 dn_fib_res_put(&res);
234 struct net_device *dev;
236 if (nh->nh_flags&(RTNH_F_PERVASIVE|RTNH_F_ONLINK))
239 dev = __dev_get_by_index(nh->nh_oif);
240 if (dev == NULL || dev->dn_ptr == NULL)
242 if (!(dev->flags&IFF_UP))
245 atomic_inc(&nh->nh_dev->refcnt);
246 nh->nh_scope = RT_SCOPE_HOST;
253 struct dn_fib_info *dn_fib_create_info(const struct rtmsg *r, struct dn_kern_rta *rta, const struct nlmsghdr *nlh, int *errp)
256 struct dn_fib_info *fi = NULL;
257 struct dn_fib_info *ofi;
260 if (r->rtm_type > RTN_MAX)
263 if (dn_fib_props[r->rtm_type].scope > r->rtm_scope)
267 nhs = dn_fib_count_nhs(rta->rta_mp);
272 fi = kmalloc(sizeof(*fi)+nhs*sizeof(struct dn_fib_nh), GFP_KERNEL);
276 memset(fi, 0, sizeof(*fi)+nhs*sizeof(struct dn_fib_nh));
278 fi->fib_protocol = r->rtm_protocol;
280 fi->fib_flags = r->rtm_flags;
281 if (rta->rta_priority)
282 fi->fib_priority = *rta->rta_priority;
283 if (rta->rta_prefsrc)
284 memcpy(&fi->fib_prefsrc, rta->rta_prefsrc, 2);
287 if ((err = dn_fib_get_nhs(fi, rta->rta_mp, r)) != 0)
289 if (rta->rta_oif && fi->fib_nh->nh_oif != *rta->rta_oif)
291 if (rta->rta_gw && memcmp(&fi->fib_nh->nh_gw, rta->rta_gw, 2))
294 struct dn_fib_nh *nh = fi->fib_nh;
296 nh->nh_oif = *rta->rta_oif;
298 memcpy(&nh->nh_gw, rta->rta_gw, 2);
299 nh->nh_flags = r->rtm_flags;
303 if (dn_fib_props[r->rtm_type].error) {
304 if (rta->rta_gw || rta->rta_oif || rta->rta_mp)
309 if (r->rtm_scope > RT_SCOPE_HOST)
312 if (r->rtm_scope == RT_SCOPE_HOST) {
313 struct dn_fib_nh *nh = fi->fib_nh;
315 /* Local address is added */
316 if (nhs != 1 || nh->nh_gw)
318 nh->nh_scope = RT_SCOPE_NOWHERE;
319 nh->nh_dev = dev_get_by_index(fi->fib_nh->nh_oif);
321 if (nh->nh_dev == NULL)
324 change_nexthops(fi) {
325 if ((err = dn_fib_check_nh(r, fi, nh)) != 0)
327 } endfor_nexthops(fi)
330 #if I_GET_AROUND_TO_FIXING_PREFSRC
331 if (fi->fib_prefsrc) {
332 if (r->rtm_type != RTN_LOCAL || rta->rta_dst == NULL ||
333 memcmp(&fi->fib_prefsrc, rta->rta_dst, 2))
334 if (dn_addr_type(fi->fib_prefsrc) != RTN_LOCAL)
340 if ((ofi = dn_fib_find_info(fi)) != NULL) {
342 dn_fib_free_info(fi);
348 atomic_inc(&fi->fib_clntref);
349 write_lock(&dn_fib_info_lock);
350 fi->fib_next = dn_fib_info_list;
352 if (dn_fib_info_list)
353 dn_fib_info_list->fib_prev = fi;
354 dn_fib_info_list = fi;
356 write_unlock(&dn_fib_info_lock);
366 dn_fib_free_info(fi);
373 void dn_fib_select_multipath(const struct dn_fib_key *key, struct dn_fib_res *res)
375 struct dn_fib_info *fi = res->fi;
378 if (fi->fib_power <= 0) {
380 change_nexthops(fi) {
381 if (!(nh->nh_flags&RTNH_F_DEAD)) {
382 power += nh->nh_weight;
383 nh->nh_power = nh->nh_weight;
385 } endfor_nexthops(fi);
386 fi->fib_power = power;
389 w = jiffies % fi->fib_power;
391 change_nexthops(fi) {
392 if (!(nh->nh_flags&RTNH_F_DEAD) && nh->nh_power) {
393 if ((w -= nh->nh_power) <= 0) {
400 } endfor_nexthops(fi);
402 printk(KERN_DEBUG "DECnet: BUG! dn_fib_select_multipath\n");
408 * Punt to user via netlink for example, but for now
411 int dn_fib_rt_message(struct sk_buff *skb)
419 static int dn_fib_check_attr(struct rtmsg *r, struct rtattr **rta)
423 for(i = 1; i <= RTA_MAX; i++) {
424 struct rtattr *attr = rta[i-1];
426 if (RTA_PAYLOAD(attr) < 4 && RTA_PAYLOAD(attr) != 2)
428 if (i != RTA_MULTIPATH && i != RTA_METRICS)
429 rta[i-1] = (struct rtattr *)RTA_DATA(attr);
436 int dn_fib_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
438 struct dn_fib_table *tb;
439 struct rtattr **rta = arg;
440 struct rtmsg *r = NLMSG_DATA(nlh);
442 if (dn_fib_check_attr(r, rta))
445 tb = dn_fib_get_table(r->rtm_table, 0);
447 return tb->delete(tb, r, (struct dn_kern_rta *)rta, nlh, &NETLINK_CB(skb));
452 int dn_fib_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
454 struct dn_fib_table *tb;
455 struct rtattr **rta = arg;
456 struct rtmsg *r = NLMSG_DATA(nlh);
458 if (dn_fib_check_attr(r, rta))
461 tb = dn_fib_get_table(r->rtm_table, 1);
463 return tb->insert(tb, r, (struct dn_kern_rta *)rta, nlh, &NETLINK_CB(skb));
469 int dn_fib_dump(struct sk_buff *skb, struct netlink_callback *cb)
473 struct dn_fib_table *tb;
475 if (NLMSG_PAYLOAD(cb->nlh, 0) >= sizeof(struct rtmsg) &&
476 ((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED)
477 return dn_cache_dump(skb, cb);
481 s_t = cb->args[0] = DN_MIN_TABLE;
483 for(t = s_t; t < DN_NUM_TABLES; t++) {
487 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(int));
488 tb = dn_fib_get_table(t, 0);
491 if (tb->dump(tb, skb, cb) < 0)
500 int dn_fib_sync_down(dn_address local, struct net_device *dev, int force)
503 int scope = RT_SCOPE_NOWHERE;
510 * This makes no sense for DECnet.... we will almost
511 * certainly have more than one local address the same
512 * over all our interfaces. It needs thinking about
515 if (local && fi->fib_prefsrc == local) {
516 fi->fib_flags |= RTNH_F_DEAD;
518 } else if (dev && fi->fib_nhs) {
521 change_nexthops(fi) {
522 if (nh->nh_flags&RTNH_F_DEAD)
524 else if (nh->nh_dev == dev &&
525 nh->nh_scope != scope) {
526 nh->nh_flags |= RTNH_F_DEAD;
527 fi->fib_power -= nh->nh_power;
531 } endfor_nexthops(fi)
532 if (dead == fi->fib_nhs) {
533 fi->fib_flags |= RTNH_F_DEAD;
542 int dn_fib_sync_up(struct net_device *dev)
546 if (!(dev->flags&IFF_UP))
552 change_nexthops(fi) {
553 if (!(nh->nh_flags&RTNH_F_DEAD)) {
557 if (nh->nh_dev == NULL || !(nh->nh_dev->flags&IFF_UP))
559 if (nh->nh_dev != dev || dev->dn_ptr == NULL)
563 nh->nh_flags &= ~RTNH_F_DEAD;
564 } endfor_nexthops(fi);
566 if (alive == fi->fib_nhs) {
567 fi->fib_flags &= ~RTNH_F_DEAD;
574 void dn_fib_flush(void)
577 struct dn_fib_table *tb;
580 for(id = DN_NUM_TABLES; id > 0; id--) {
581 if ((tb = dn_fib_get_table(id, 0)) == NULL)
583 flushed += tb->flush(tb);
587 dn_rt_cache_flush(-1);
590 int dn_fib_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
593 if (!capable(CAP_NET_ADMIN))
605 #ifdef CONFIG_PROC_FS
607 static int decnet_rt_get_info(char *buffer, char **start, off_t offset, int length)
609 int first = offset / 128;
611 int count = (length + 127) / 128;
614 struct dn_fib_table *tb;
616 *start = buffer + (offset % 128);
619 sprintf(buffer, "%-127s\n", "Iface\tDest\tGW \tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU\tWindow\tIRTT");
626 for(i = DN_MIN_TABLE; (i <= DN_NUM_TABLES) && (count > 0); i++) {
627 if ((tb = dn_fib_get_table(i, 0)) != NULL) {
628 int n = tb->get_info(tb, ptr, first, count);
642 #endif /* CONFIG_PROC_FS */
644 void __exit dn_fib_cleanup(void)
646 proc_net_remove("decnet_route");
648 dn_fib_table_cleanup();
649 dn_fib_rules_cleanup();
653 void __init dn_fib_init(void)
656 #ifdef CONFIG_PROC_FS
657 proc_net_create("decnet_route", 0, decnet_rt_get_info);