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/string.h>
16 #include <linux/net.h>
17 #include <linux/socket.h>
18 #include <linux/sockios.h>
19 #include <linux/init.h>
20 #include <linux/skbuff.h>
21 #include <linux/netlink.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/proc_fs.h>
24 #include <linux/netdevice.h>
25 #include <linux/timer.h>
26 #include <linux/spinlock.h>
27 #include <asm/atomic.h>
28 #include <asm/uaccess.h>
29 #include <linux/route.h> /* RTF_xxx */
30 #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)
76 #define RT_TABLE_MIN 1
78 static DEFINE_RWLOCK(dn_fib_tables_lock);
79 struct dn_fib_table *dn_fib_tables[RT_TABLE_MAX + 1];
81 static kmem_cache_t *dn_hash_kmem __read_mostly;
82 static int dn_fib_hash_zombies;
84 static inline dn_fib_idx_t dn_hash(dn_fib_key_t key, struct dn_zone *dz)
86 u16 h = dn_ntohs(key.datum)>>(16 - dz->dz_order);
90 return *(dn_fib_idx_t *)&h;
93 static inline dn_fib_key_t dz_key(__le16 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, unsigned int flags)
275 struct nlmsghdr *nlh;
276 unsigned char *b = skb->tail;
278 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*rtm), flags);
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 (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
295 if (fi->fib_nhs == 1) {
296 if (fi->fib_nh->nh_gw)
297 RTA_PUT(skb, RTA_GATEWAY, 2, &fi->fib_nh->nh_gw);
298 if (fi->fib_nh->nh_oif)
299 RTA_PUT(skb, RTA_OIF, sizeof(int), &fi->fib_nh->nh_oif);
301 if (fi->fib_nhs > 1) {
302 struct rtnexthop *nhp;
303 struct rtattr *mp_head;
304 if (skb_tailroom(skb) <= RTA_SPACE(0))
306 mp_head = (struct rtattr *)skb_put(skb, RTA_SPACE(0));
309 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
311 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
312 nhp->rtnh_flags = nh->nh_flags & 0xFF;
313 nhp->rtnh_hops = nh->nh_weight - 1;
314 nhp->rtnh_ifindex = nh->nh_oif;
316 RTA_PUT(skb, RTA_GATEWAY, 2, &nh->nh_gw);
317 nhp->rtnh_len = skb->tail - (unsigned char *)nhp;
318 } endfor_nexthops(fi);
319 mp_head->rta_type = RTA_MULTIPATH;
320 mp_head->rta_len = skb->tail - (u8*)mp_head;
323 nlh->nlmsg_len = skb->tail - b;
329 skb_trim(skb, b - skb->data);
334 static void dn_rtmsg_fib(int event, struct dn_fib_node *f, int z, int tb_id,
335 struct nlmsghdr *nlh, struct netlink_skb_parms *req)
338 u32 pid = req ? req->pid : 0;
339 int size = NLMSG_SPACE(sizeof(struct rtmsg) + 256);
341 skb = alloc_skb(size, GFP_KERNEL);
345 if (dn_fib_dump_info(skb, pid, nlh->nlmsg_seq, event, tb_id,
346 f->fn_type, f->fn_scope, &f->fn_key, z,
347 DN_FIB_INFO(f), 0) < 0) {
351 NETLINK_CB(skb).dst_group = RTNLGRP_DECnet_ROUTE;
352 if (nlh->nlmsg_flags & NLM_F_ECHO)
353 atomic_inc(&skb->users);
354 netlink_broadcast(rtnl, skb, pid, RTNLGRP_DECnet_ROUTE, GFP_KERNEL);
355 if (nlh->nlmsg_flags & NLM_F_ECHO)
356 netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
359 static __inline__ int dn_hash_dump_bucket(struct sk_buff *skb,
360 struct netlink_callback *cb,
361 struct dn_fib_table *tb,
363 struct dn_fib_node *f)
368 for(i = 0; f; i++, f = f->fn_next) {
371 if (f->fn_state & DN_S_ZOMBIE)
373 if (dn_fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
377 (f->fn_state & DN_S_ZOMBIE) ? 0 : f->fn_type,
378 f->fn_scope, &f->fn_key, dz->dz_order,
379 f->fn_info, NLM_F_MULTI) < 0) {
388 static __inline__ int dn_hash_dump_zone(struct sk_buff *skb,
389 struct netlink_callback *cb,
390 struct dn_fib_table *tb,
396 for(h = 0; h < dz->dz_divisor; h++) {
400 memset(&cb->args[3], 0, sizeof(cb->args) - 3*sizeof(cb->args[0]));
401 if (dz->dz_hash == NULL || dz->dz_hash[h] == NULL)
403 if (dn_hash_dump_bucket(skb, cb, tb, dz, dz->dz_hash[h]) < 0) {
412 static int dn_fib_table_dump(struct dn_fib_table *tb, struct sk_buff *skb,
413 struct netlink_callback *cb)
417 struct dn_hash *table = (struct dn_hash *)tb->data;
420 read_lock(&dn_fib_tables_lock);
421 for(dz = table->dh_zone_list, m = 0; dz; dz = dz->dz_next, m++) {
425 memset(&cb->args[2], 0, sizeof(cb->args) - 2*sizeof(cb->args[0]));
427 if (dn_hash_dump_zone(skb, cb, tb, dz) < 0) {
429 read_unlock(&dn_fib_tables_lock);
433 read_unlock(&dn_fib_tables_lock);
439 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)
441 struct dn_hash *table = (struct dn_hash *)tb->data;
442 struct dn_fib_node *new_f, *f, **fp, **del_fp;
444 struct dn_fib_info *fi;
445 int z = r->rtm_dst_len;
446 int type = r->rtm_type;
453 dz = table->dh_zones[z];
454 if (!dz && !(dz = dn_new_zone(table, z)))
460 memcpy(&dst, rta->rta_dst, 2);
461 if (dst & ~DZ_MASK(dz))
463 key = dz_key(dst, dz);
466 if ((fi = dn_fib_create_info(r, rta, n, &err)) == NULL)
469 if (dz->dz_nent > (dz->dz_divisor << 2) &&
470 dz->dz_divisor > DN_MAX_DIVISOR &&
471 (z==16 || (1<<z) > dz->dz_divisor))
474 fp = dn_chain_p(key, dz);
477 if (dn_key_leq(key, f->fn_key))
483 if (f && (f->fn_state & DN_S_ZOMBIE) &&
484 dn_key_eq(f->fn_key, key)) {
491 DN_FIB_SCAN_KEY(f, fp, key) {
492 if (fi->fib_priority <= DN_FIB_INFO(f)->fib_priority)
496 if (f && dn_key_eq(f->fn_key, key) &&
497 fi->fib_priority == DN_FIB_INFO(f)->fib_priority) {
498 struct dn_fib_node **ins_fp;
501 if (n->nlmsg_flags & NLM_F_EXCL)
504 if (n->nlmsg_flags & NLM_F_REPLACE) {
514 DN_FIB_SCAN_KEY(f, fp, key) {
515 if (fi->fib_priority != DN_FIB_INFO(f)->fib_priority)
517 if (f->fn_type == type && f->fn_scope == r->rtm_scope
518 && DN_FIB_INFO(f) == fi)
522 if (!(n->nlmsg_flags & NLM_F_APPEND)) {
530 if (!(n->nlmsg_flags & NLM_F_CREATE))
535 new_f = kmem_cache_alloc(dn_hash_kmem, SLAB_KERNEL);
539 memset(new_f, 0, sizeof(struct dn_fib_node));
542 new_f->fn_type = type;
543 new_f->fn_scope = r->rtm_scope;
544 DN_FIB_INFO(new_f) = fi;
547 write_lock_bh(&dn_fib_tables_lock);
549 write_unlock_bh(&dn_fib_tables_lock);
554 write_lock_bh(&dn_fib_tables_lock);
555 *del_fp = f->fn_next;
556 write_unlock_bh(&dn_fib_tables_lock);
558 if (!(f->fn_state & DN_S_ZOMBIE))
559 dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
560 if (f->fn_state & DN_S_ACCESSED)
561 dn_rt_cache_flush(-1);
565 dn_rt_cache_flush(-1);
568 dn_rtmsg_fib(RTM_NEWROUTE, new_f, z, tb->n, n, req);
572 dn_fib_release_info(fi);
577 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)
579 struct dn_hash *table = (struct dn_hash*)tb->data;
580 struct dn_fib_node **fp, **del_fp, *f;
581 int z = r->rtm_dst_len;
590 if ((dz = table->dh_zones[z]) == NULL)
596 memcpy(&dst, rta->rta_dst, 2);
597 if (dst & ~DZ_MASK(dz))
599 key = dz_key(dst, dz);
602 fp = dn_chain_p(key, dz);
605 if (dn_key_eq(f->fn_key, key))
607 if (dn_key_leq(key, f->fn_key))
613 DN_FIB_SCAN_KEY(f, fp, key) {
614 struct dn_fib_info *fi = DN_FIB_INFO(f);
616 if (f->fn_state & DN_S_ZOMBIE)
621 if (del_fp == NULL &&
622 (!r->rtm_type || f->fn_type == r->rtm_type) &&
623 (r->rtm_scope == RT_SCOPE_NOWHERE || f->fn_scope == r->rtm_scope) &&
625 fi->fib_protocol == r->rtm_protocol) &&
626 dn_fib_nh_match(r, n, rta, fi) == 0)
632 dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
635 write_lock_bh(&dn_fib_tables_lock);
636 *del_fp = f->fn_next;
637 write_unlock_bh(&dn_fib_tables_lock);
639 if (f->fn_state & DN_S_ACCESSED)
640 dn_rt_cache_flush(-1);
644 f->fn_state |= DN_S_ZOMBIE;
645 if (f->fn_state & DN_S_ACCESSED) {
646 f->fn_state &= ~DN_S_ACCESSED;
647 dn_rt_cache_flush(-1);
649 if (++dn_fib_hash_zombies > 128)
659 static inline int dn_flush_list(struct dn_fib_node **fp, int z, struct dn_hash *table)
662 struct dn_fib_node *f;
664 while((f = *fp) != NULL) {
665 struct dn_fib_info *fi = DN_FIB_INFO(f);
667 if (fi && ((f->fn_state & DN_S_ZOMBIE) || (fi->fib_flags & RTNH_F_DEAD))) {
668 write_lock_bh(&dn_fib_tables_lock);
670 write_unlock_bh(&dn_fib_tables_lock);
682 static int dn_fib_table_flush(struct dn_fib_table *tb)
684 struct dn_hash *table = (struct dn_hash *)tb->data;
688 dn_fib_hash_zombies = 0;
689 for(dz = table->dh_zone_list; dz; dz = dz->dz_next) {
692 for(i = dz->dz_divisor-1; i >= 0; i--)
693 tmp += dn_flush_list(&dz->dz_hash[i], dz->dz_order, table);
701 static int dn_fib_table_lookup(struct dn_fib_table *tb, const struct flowi *flp, struct dn_fib_res *res)
705 struct dn_hash *t = (struct dn_hash *)tb->data;
707 read_lock(&dn_fib_tables_lock);
708 for(dz = t->dh_zone_list; dz; dz = dz->dz_next) {
709 struct dn_fib_node *f;
710 dn_fib_key_t k = dz_key(flp->fld_dst, dz);
712 for(f = dz_chain(k, dz); f; f = f->fn_next) {
713 if (!dn_key_eq(k, f->fn_key)) {
714 if (dn_key_leq(k, f->fn_key))
720 f->fn_state |= DN_S_ACCESSED;
722 if (f->fn_state&DN_S_ZOMBIE)
725 if (f->fn_scope < flp->fld_scope)
728 err = dn_fib_semantic_match(f->fn_type, DN_FIB_INFO(f), flp, res);
731 res->type = f->fn_type;
732 res->scope = f->fn_scope;
733 res->prefixlen = dz->dz_order;
742 read_unlock(&dn_fib_tables_lock);
747 struct dn_fib_table *dn_fib_get_table(int n, int create)
749 struct dn_fib_table *t;
751 if (n < RT_TABLE_MIN)
754 if (n > RT_TABLE_MAX)
757 if (dn_fib_tables[n])
758 return dn_fib_tables[n];
763 if (in_interrupt() && net_ratelimit()) {
764 printk(KERN_DEBUG "DECnet: BUG! Attempt to create routing table from interrupt\n");
767 if ((t = kmalloc(sizeof(struct dn_fib_table) + sizeof(struct dn_hash), GFP_KERNEL)) == NULL)
770 memset(t, 0, sizeof(struct dn_fib_table));
773 t->insert = dn_fib_table_insert;
774 t->delete = dn_fib_table_delete;
775 t->lookup = dn_fib_table_lookup;
776 t->flush = dn_fib_table_flush;
777 t->dump = dn_fib_table_dump;
778 memset(t->data, 0, sizeof(struct dn_hash));
779 dn_fib_tables[n] = t;
784 static void dn_fib_del_tree(int n)
786 struct dn_fib_table *t;
788 write_lock(&dn_fib_tables_lock);
789 t = dn_fib_tables[n];
790 dn_fib_tables[n] = NULL;
791 write_unlock(&dn_fib_tables_lock);
796 struct dn_fib_table *dn_fib_empty_table(void)
800 for(id = RT_TABLE_MIN; id <= RT_TABLE_MAX; id++)
801 if (dn_fib_tables[id] == NULL)
802 return dn_fib_get_table(id, 1);
806 void __init dn_fib_table_init(void)
808 dn_hash_kmem = kmem_cache_create("dn_fib_info_cache",
809 sizeof(struct dn_fib_info),
810 0, SLAB_HWCACHE_ALIGN,
814 void __exit dn_fib_table_cleanup(void)
818 for (i = RT_TABLE_MIN; i <= RT_TABLE_MAX; ++i)