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 (Routing Tables)
8 * Author: Steve Whitehouse <SteveW@ACM.org>
9 * Mostly copied from the IPv4 routing code
15 #include <linux/config.h>
16 #include <linux/string.h>
17 #include <linux/net.h>
18 #include <linux/socket.h>
19 #include <linux/sockios.h>
20 #include <linux/init.h>
21 #include <linux/skbuff.h>
22 #include <linux/netlink.h>
23 #include <linux/rtnetlink.h>
24 #include <linux/proc_fs.h>
25 #include <linux/netdevice.h>
26 #include <linux/timer.h>
27 #include <linux/spinlock.h>
28 #include <asm/atomic.h>
29 #include <asm/uaccess.h>
30 #include <linux/route.h> /* RTF_xxx */
31 #include <net/neighbour.h>
34 #include <net/dn_route.h>
35 #include <net/dn_fib.h>
36 #include <net/dn_neigh.h>
37 #include <net/dn_dev.h>
41 struct dn_zone *dz_next;
42 struct dn_fib_node **dz_hash;
46 #define DZ_HASHMASK(dz) ((dz)->dz_hashmask)
49 #define DZ_MASK(dz) ((dz)->dz_mask)
54 struct dn_zone *dh_zones[17];
55 struct dn_zone *dh_zone_list;
58 #define dz_key_0(key) ((key).datum = 0)
59 #define dz_prefix(key,dz) ((key).datum)
61 #define for_nexthops(fi) { int nhsel; const struct dn_fib_nh *nh;\
62 for(nhsel = 0, nh = (fi)->fib_nh; nhsel < (fi)->fib_nhs; nh++, nhsel++)
64 #define endfor_nexthops(fi) }
66 #define DN_MAX_DIVISOR 1024
68 #define DN_S_ACCESSED 2
70 #define DN_FIB_SCAN(f, fp) \
71 for( ; ((f) = *(fp)) != NULL; (fp) = &(f)->fn_next)
73 #define DN_FIB_SCAN_KEY(f, fp, key) \
74 for( ; ((f) = *(fp)) != NULL && dn_key_eq((f)->fn_key, (key)); (fp) = &(f)->fn_next)
77 static rwlock_t dn_fib_tables_lock = RW_LOCK_UNLOCKED;
78 static struct dn_fib_table *dn_fib_tables[DN_NUM_TABLES + 1];
80 static kmem_cache_t *dn_hash_kmem;
81 static int dn_fib_hash_zombies;
83 static __inline__ dn_fib_idx_t dn_hash(dn_fib_key_t key, struct dn_zone *dz)
85 u32 h = ntohs(key.datum)>>(16 - dz->dz_order);
90 return *(dn_fib_idx_t *)&h;
93 static __inline__ dn_fib_key_t dz_key(u16 dst, struct dn_zone *dz)
96 k.datum = dst & DZ_MASK(dz);
100 static __inline__ struct dn_fib_node **dn_chain_p(dn_fib_key_t key, struct dn_zone *dz)
102 return &dz->dz_hash[dn_hash(key, dz).datum];
105 static __inline__ struct dn_fib_node *dz_chain(dn_fib_key_t key, struct dn_zone *dz)
107 return dz->dz_hash[dn_hash(key, dz).datum];
110 static __inline__ int dn_key_eq(dn_fib_key_t a, dn_fib_key_t b)
112 return a.datum == b.datum;
115 static __inline__ int dn_key_leq(dn_fib_key_t a, dn_fib_key_t b)
117 return a.datum <= b.datum;
120 static __inline__ void dn_rebuild_zone(struct dn_zone *dz,
121 struct dn_fib_node **old_ht,
125 struct dn_fib_node *f, **fp, *next;
127 for(i = 0; i < old_divisor; i++) {
128 for(f = old_ht[i]; f; f = f->fn_next) {
130 for(fp = dn_chain_p(f->fn_key, dz);
131 *fp && dn_key_leq((*fp)->fn_key, f->fn_key);
132 fp = &(*fp)->fn_next)
140 static void dn_rehash_zone(struct dn_zone *dz)
142 struct dn_fib_node **ht, **old_ht;
143 int old_divisor, new_divisor;
146 old_divisor = dz->dz_divisor;
148 switch(old_divisor) {
154 printk(KERN_DEBUG "DECnet: dn_rehash_zone: BUG! %d\n", old_divisor);
157 new_hashmask = 0x3FF;
161 ht = kmalloc(new_divisor*sizeof(struct dn_fib_node*), GFP_KERNEL);
166 memset(ht, 0, new_divisor*sizeof(struct dn_fib_node *));
167 write_lock_bh(&dn_fib_tables_lock);
168 old_ht = dz->dz_hash;
170 dz->dz_hashmask = new_hashmask;
171 dz->dz_divisor = new_divisor;
172 dn_rebuild_zone(dz, old_ht, old_divisor);
173 write_unlock_bh(&dn_fib_tables_lock);
177 static void dn_free_node(struct dn_fib_node *f)
179 dn_fib_release_info(DN_FIB_INFO(f));
180 kmem_cache_free(dn_hash_kmem, f);
184 static struct dn_zone *dn_new_zone(struct dn_hash *table, int z)
187 struct dn_zone *dz = kmalloc(sizeof(struct dn_zone), GFP_KERNEL);
191 memset(dz, 0, sizeof(struct dn_zone));
194 dz->dz_hashmask = 0x0F;
200 dz->dz_hash = kmalloc(dz->dz_divisor*sizeof(struct dn_fib_node *), GFP_KERNEL);
207 memset(dz->dz_hash, 0, dz->dz_divisor*sizeof(struct dn_fib_node*));
209 dz->dz_mask = dnet_make_mask(z);
211 for(i = z + 1; i <= 16; i++)
212 if (table->dh_zones[i])
215 write_lock_bh(&dn_fib_tables_lock);
217 dz->dz_next = table->dh_zone_list;
218 table->dh_zone_list = dz;
220 dz->dz_next = table->dh_zones[i]->dz_next;
221 table->dh_zones[i]->dz_next = dz;
223 table->dh_zones[z] = dz;
224 write_unlock_bh(&dn_fib_tables_lock);
229 static int dn_fib_nh_match(struct rtmsg *r, struct nlmsghdr *nlh, struct dn_kern_rta *rta, struct dn_fib_info *fi)
231 struct rtnexthop *nhp;
234 if (rta->rta_priority && *rta->rta_priority != fi->fib_priority)
237 if (rta->rta_oif || rta->rta_gw) {
238 if ((!rta->rta_oif || *rta->rta_oif == fi->fib_nh->nh_oif) &&
239 (!rta->rta_gw || memcmp(rta->rta_gw, &fi->fib_nh->nh_gw, 2) == 0))
244 if (rta->rta_mp == NULL)
247 nhp = RTA_DATA(rta->rta_mp);
248 nhlen = RTA_PAYLOAD(rta->rta_mp);
251 int attrlen = nhlen - sizeof(struct rtnexthop);
254 if (attrlen < 0 || (nhlen -= nhp->rtnh_len) < 0)
256 if (nhp->rtnh_ifindex && nhp->rtnh_ifindex != nh->nh_oif)
259 gw = dn_fib_get_attr16(RTNH_DATA(nhp), attrlen, RTA_GATEWAY);
261 if (gw && gw != nh->nh_gw)
264 nhp = RTNH_NEXT(nhp);
265 } endfor_nexthops(fi);
270 static int dn_fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
271 u8 tb_id, u8 type, u8 scope, void *dst, int dst_len,
272 struct dn_fib_info *fi)
275 struct nlmsghdr *nlh;
276 unsigned char *b = skb->tail;
278 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*rtm));
279 rtm = NLMSG_DATA(nlh);
280 rtm->rtm_family = AF_DECnet;
281 rtm->rtm_dst_len = dst_len;
282 rtm->rtm_src_len = 0;
284 rtm->rtm_table = tb_id;
285 rtm->rtm_flags = fi->fib_flags;
286 rtm->rtm_scope = scope;
287 rtm->rtm_type = type;
288 if (rtm->rtm_dst_len)
289 RTA_PUT(skb, RTA_DST, 2, dst);
290 rtm->rtm_protocol = fi->fib_protocol;
291 if (fi->fib_priority)
292 RTA_PUT(skb, RTA_PRIORITY, 4, &fi->fib_priority);
293 if (fi->fib_nhs == 1) {
294 if (fi->fib_nh->nh_gw)
295 RTA_PUT(skb, RTA_GATEWAY, 2, &fi->fib_nh->nh_gw);
296 if (fi->fib_nh->nh_oif)
297 RTA_PUT(skb, RTA_OIF, sizeof(int), &fi->fib_nh->nh_oif);
299 if (fi->fib_nhs > 1) {
300 struct rtnexthop *nhp;
301 struct rtattr *mp_head;
302 if (skb_tailroom(skb) <= RTA_SPACE(0))
304 mp_head = (struct rtattr *)skb_put(skb, RTA_SPACE(0));
307 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
309 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
310 nhp->rtnh_flags = nh->nh_flags & 0xFF;
311 nhp->rtnh_hops = nh->nh_weight - 1;
312 nhp->rtnh_ifindex = nh->nh_oif;
314 RTA_PUT(skb, RTA_GATEWAY, 2, &nh->nh_gw);
315 nhp->rtnh_len = skb->tail - (unsigned char *)nhp;
316 } endfor_nexthops(fi);
317 mp_head->rta_type = RTA_MULTIPATH;
318 mp_head->rta_len = skb->tail - (u8*)mp_head;
321 nlh->nlmsg_len = skb->tail - b;
327 skb_trim(skb, b - skb->data);
332 static void dn_rtmsg_fib(int event, struct dn_fib_node *f, int z, int tb_id,
333 struct nlmsghdr *nlh, struct netlink_skb_parms *req)
336 u32 pid = req ? req->pid : 0;
337 int size = NLMSG_SPACE(sizeof(struct rtmsg) + 256);
339 skb = alloc_skb(size, GFP_KERNEL);
343 if (dn_fib_dump_info(skb, pid, nlh->nlmsg_seq, event, tb_id,
344 f->fn_type, f->fn_scope, &f->fn_key, z,
345 DN_FIB_INFO(f)) < 0) {
349 NETLINK_CB(skb).dst_groups = RTMGRP_DECnet_ROUTE;
350 if (nlh->nlmsg_flags & NLM_F_ECHO)
351 atomic_inc(&skb->users);
352 netlink_broadcast(rtnl, skb, pid, RTMGRP_DECnet_ROUTE, GFP_KERNEL);
353 if (nlh->nlmsg_flags & NLM_F_ECHO)
354 netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
357 static __inline__ int dn_hash_dump_bucket(struct sk_buff *skb,
358 struct netlink_callback *cb,
359 struct dn_fib_table *tb,
361 struct dn_fib_node *f)
366 for(i = 0; f; i++, f = f->fn_next) {
369 if (f->fn_state & DN_S_ZOMBIE)
371 if (dn_fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
375 (f->fn_state & DN_S_ZOMBIE) ? 0 : f->fn_type,
376 f->fn_scope, &f->fn_key, dz->dz_order,
386 static __inline__ int dn_hash_dump_zone(struct sk_buff *skb,
387 struct netlink_callback *cb,
388 struct dn_fib_table *tb,
394 for(h = 0; h < dz->dz_divisor; h++) {
398 memset(&cb->args[3], 0, sizeof(cb->args) - 3*sizeof(cb->args[0]));
399 if (dz->dz_hash == NULL || dz->dz_hash[h] == NULL)
401 if (dn_hash_dump_bucket(skb, cb, tb, dz, dz->dz_hash[h]) < 0) {
410 static int dn_fib_table_dump(struct dn_fib_table *tb, struct sk_buff *skb,
411 struct netlink_callback *cb)
415 struct dn_hash *table = (struct dn_hash *)tb->data;
418 read_lock(&dn_fib_tables_lock);
419 for(dz = table->dh_zone_list, m = 0; dz; dz = dz->dz_next, m++) {
423 memset(&cb->args[2], 0, sizeof(cb->args) - 2*sizeof(cb->args[0]));
425 if (dn_hash_dump_zone(skb, cb, tb, dz) < 0) {
427 read_unlock(&dn_fib_tables_lock);
431 read_unlock(&dn_fib_tables_lock);
437 static int dn_fib_table_insert(struct dn_fib_table *tb, struct rtmsg *r, struct dn_kern_rta *rta, struct nlmsghdr *n, struct netlink_skb_parms *req)
439 struct dn_hash *table = (struct dn_hash *)tb->data;
440 struct dn_fib_node *new_f, *f, **fp, **del_fp;
442 struct dn_fib_info *fi;
443 int z = r->rtm_dst_len;
444 int type = r->rtm_type;
451 dz = table->dh_zones[z];
452 if (!dz && !(dz = dn_new_zone(table, z)))
458 memcpy(&dst, rta->rta_dst, 2);
459 if (dst & ~DZ_MASK(dz))
461 key = dz_key(dst, dz);
464 if ((fi = dn_fib_create_info(r, rta, n, &err)) == NULL)
467 if (dz->dz_nent > (dz->dz_divisor << 2) &&
468 dz->dz_divisor > DN_MAX_DIVISOR &&
469 (z==16 || (1<<z) > dz->dz_divisor))
472 fp = dn_chain_p(key, dz);
475 if (dn_key_leq(key, f->fn_key))
481 if (f && (f->fn_state & DN_S_ZOMBIE) &&
482 dn_key_eq(f->fn_key, key)) {
489 DN_FIB_SCAN_KEY(f, fp, key) {
490 if (fi->fib_priority <= DN_FIB_INFO(f)->fib_priority)
494 if (f && dn_key_eq(f->fn_key, key) &&
495 fi->fib_priority == DN_FIB_INFO(f)->fib_priority) {
496 struct dn_fib_node **ins_fp;
499 if (n->nlmsg_flags & NLM_F_EXCL)
502 if (n->nlmsg_flags & NLM_F_REPLACE) {
512 DN_FIB_SCAN_KEY(f, fp, key) {
513 if (fi->fib_priority != DN_FIB_INFO(f)->fib_priority)
515 if (f->fn_type == type && f->fn_scope == r->rtm_scope
516 && DN_FIB_INFO(f) == fi)
520 if (!(n->nlmsg_flags & NLM_F_APPEND)) {
528 if (!(n->nlmsg_flags & NLM_F_CREATE))
533 new_f = kmem_cache_alloc(dn_hash_kmem, SLAB_KERNEL);
537 memset(new_f, 0, sizeof(struct dn_fib_node));
540 new_f->fn_type = type;
541 new_f->fn_scope = r->rtm_scope;
542 DN_FIB_INFO(new_f) = fi;
545 write_lock_bh(&dn_fib_tables_lock);
547 write_unlock_bh(&dn_fib_tables_lock);
552 write_lock_bh(&dn_fib_tables_lock);
553 *del_fp = f->fn_next;
554 write_unlock_bh(&dn_fib_tables_lock);
556 if (!(f->fn_state & DN_S_ZOMBIE))
557 dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
558 if (f->fn_state & DN_S_ACCESSED)
559 dn_rt_cache_flush(-1);
563 dn_rt_cache_flush(-1);
566 dn_rtmsg_fib(RTM_NEWROUTE, new_f, z, tb->n, n, req);
570 dn_fib_release_info(fi);
575 static int dn_fib_table_delete(struct dn_fib_table *tb, struct rtmsg *r, struct dn_kern_rta *rta, struct nlmsghdr *n, struct netlink_skb_parms *req)
577 struct dn_hash *table = (struct dn_hash*)tb->data;
578 struct dn_fib_node **fp, **del_fp, *f;
579 int z = r->rtm_dst_len;
588 if ((dz = table->dh_zones[z]) == NULL)
594 memcpy(&dst, rta->rta_dst, 2);
595 if (dst & ~DZ_MASK(dz))
597 key = dz_key(dst, dz);
600 fp = dn_chain_p(key, dz);
603 if (dn_key_eq(f->fn_key, key))
605 if (dn_key_leq(key, f->fn_key))
611 DN_FIB_SCAN_KEY(f, fp, key) {
612 struct dn_fib_info *fi = DN_FIB_INFO(f);
614 if (f->fn_state & DN_S_ZOMBIE)
619 if (del_fp == NULL &&
620 (!r->rtm_type || f->fn_type == r->rtm_type) &&
621 (r->rtm_scope == RT_SCOPE_NOWHERE || f->fn_scope == r->rtm_scope) &&
623 fi->fib_protocol == r->rtm_protocol) &&
624 dn_fib_nh_match(r, n, rta, fi) == 0)
630 dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
633 write_lock_bh(&dn_fib_tables_lock);
634 *del_fp = f->fn_next;
635 write_unlock_bh(&dn_fib_tables_lock);
637 if (f->fn_state & DN_S_ACCESSED)
638 dn_rt_cache_flush(-1);
642 f->fn_state |= DN_S_ZOMBIE;
643 if (f->fn_state & DN_S_ACCESSED) {
644 f->fn_state &= ~DN_S_ACCESSED;
645 dn_rt_cache_flush(-1);
647 if (++dn_fib_hash_zombies > 128)
657 static __inline__ int dn_flush_list(struct dn_fib_node **fp, int z, struct dn_hash *table)
660 struct dn_fib_node *f;
662 while((f = *fp) != NULL) {
663 struct dn_fib_info *fi = DN_FIB_INFO(f);
665 if (fi && ((f->fn_state & DN_S_ZOMBIE) || (fi->fib_flags & RTNH_F_DEAD))) {
666 write_lock_bh(&dn_fib_tables_lock);
668 write_unlock_bh(&dn_fib_tables_lock);
680 static int dn_fib_table_flush(struct dn_fib_table *tb)
682 struct dn_hash *table = (struct dn_hash *)tb->data;
686 dn_fib_hash_zombies = 0;
687 for(dz = table->dh_zone_list; dz; dz = dz->dz_next) {
690 for(i = dz->dz_divisor-1; i >= 0; i--)
691 tmp += dn_flush_list(&dz->dz_hash[i], dz->dz_order, table);
699 static int dn_fib_table_lookup(struct dn_fib_table *tb, const struct dn_fib_key *
700 key, struct dn_fib_res *res)
704 struct dn_hash *t = (struct dn_hash *)tb->data;
706 read_lock(&dn_fib_tables_lock);
707 for(dz = t->dh_zone_list; dz; dz = dz->dz_next) {
708 struct dn_fib_node *f;
709 dn_fib_key_t k = dz_key(key->dst, dz);
711 for(f = dz_chain(k, dz); f; f = f->fn_next) {
712 if (!dn_key_leq(k, f->fn_key))
717 f->fn_state |= DN_S_ACCESSED;
719 if (f->fn_state&DN_S_ZOMBIE)
721 if (f->fn_scope < key->scope)
724 err = dn_fib_semantic_match(f->fn_type, DN_FIB_INFO(f), key, res);
726 res->type = f->fn_type;
727 res->scope = f->fn_scope;
728 res->prefixlen = dz->dz_order;
737 read_unlock(&dn_fib_tables_lock);
741 #ifdef CONFIG_PROC_FS
743 static unsigned dn_fib_flag_trans(int type, int dead, u16 mask, struct dn_fib_info *fi)
745 static unsigned type2flags[RTN_MAX+1] = {
746 0, 0, 0, 0, 0, 0, 0, RTF_REJECT, RTF_REJECT, 0, 0, 0
748 unsigned flags = type2flags[type];
750 if (fi && fi->fib_nh->nh_gw)
751 flags |= RTF_GATEWAY;
759 static void dn_fib_node_get_info(int type, int dead, struct dn_fib_info *fi, u16 prefix, u16 mask, char *buffer)
762 unsigned flags = dn_fib_flag_trans(type, dead, mask, fi);
765 len = sprintf(buffer, "%s\t%04x\t%04x\t%04x\t%d\t%u\t%d\t%04x\t%d\t%u\t%u",
766 fi->fib_dev ? fi->fib_dev->name : "*", prefix,
767 fi->fib_nh->nh_gw, flags, 0, 0, fi->fib_priority,
770 len = sprintf(buffer, "*\t%04x\t%04x\t%04x\t%d\t%u\t%d\t%04x\t%d\t%u\t%u",
775 memset(buffer+len, ' ', 127-len);
779 static int dn_fib_table_get_info(struct dn_fib_table *tb, char *buffer, int first, int count)
781 struct dn_hash *table = (struct dn_hash *)tb->data;
786 read_lock(&dn_fib_tables_lock);
787 for(dz = table->dh_zone_list; dz; dz = dz->dz_next) {
789 struct dn_fib_node *f;
790 int maxslot = dz->dz_divisor;
791 struct dn_fib_node **fp = dz->dz_hash;
793 if (dz->dz_nent == 0)
796 if (pos + dz->dz_nent < first) {
801 for(i = 0; i < maxslot; i++, fp++) {
802 for(f = *fp; f ; f = f->fn_next) {
805 dn_fib_node_get_info(f->fn_type,
806 f->fn_state & DN_S_ZOMBIE,
808 dz_prefix(f->fn_key, dz),
809 DZ_MASK(dz), buffer);
817 read_unlock(&dn_fib_tables_lock);
820 #endif /* CONFIG_PROC_FS */
822 struct dn_fib_table *dn_fib_get_table(int n, int create)
824 struct dn_fib_table *t;
826 if (n < DN_MIN_TABLE)
829 if (n > DN_NUM_TABLES)
832 if (dn_fib_tables[n])
833 return dn_fib_tables[n];
838 if (in_interrupt() && net_ratelimit()) {
839 printk(KERN_DEBUG "DECnet: BUG! Attempt to create routing table from interrupt\n");
842 if ((t = kmalloc(sizeof(struct dn_fib_table), GFP_KERNEL)) == NULL)
845 memset(t, 0, sizeof(struct dn_fib_table));
848 t->insert = dn_fib_table_insert;
849 t->delete = dn_fib_table_delete;
850 t->lookup = dn_fib_table_lookup;
851 t->flush = dn_fib_table_flush;
852 #ifdef CONFIG_PROC_FS
853 t->get_info = dn_fib_table_get_info;
855 t->dump = dn_fib_table_dump;
856 dn_fib_tables[n] = t;
861 static void dn_fib_del_tree(int n)
863 struct dn_fib_table *t;
865 write_lock(&dn_fib_tables_lock);
866 t = dn_fib_tables[n];
867 dn_fib_tables[n] = NULL;
868 write_unlock(&dn_fib_tables_lock);
875 struct dn_fib_table *dn_fib_empty_table(void)
879 for(id = DN_MIN_TABLE; id <= DN_NUM_TABLES; id++)
880 if (dn_fib_tables[id] == NULL)
881 return dn_fib_get_table(id, 1);
885 void __init dn_fib_table_init(void)
887 dn_hash_kmem = kmem_cache_create("dn_fib_info_cache",
888 sizeof(struct dn_fib_info),
889 0, SLAB_HWCACHE_ALIGN,
893 void __exit dn_fib_table_cleanup(void)
897 for (i = 0; i < DN_NUM_TABLES + 1; ++i)