[DECNET]: Covert rules to use generic code
[linux-3.10.git] / net / decnet / dn_table.c
1 /*
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.
5  *
6  *              DECnet Routing Forwarding Information Base (Routing Tables)
7  *
8  * Author:      Steve Whitehouse <SteveW@ACM.org>
9  *              Mostly copied from the IPv4 routing code
10  *
11  *
12  * Changes:
13  *
14  */
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>
31 #include <net/dst.h>
32 #include <net/flow.h>
33 #include <net/fib_rules.h>
34 #include <net/dn.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>
39
40 struct dn_zone
41 {
42         struct dn_zone          *dz_next;
43         struct dn_fib_node      **dz_hash;
44         int                     dz_nent;
45         int                     dz_divisor;
46         u32                     dz_hashmask;
47 #define DZ_HASHMASK(dz) ((dz)->dz_hashmask)
48         int                     dz_order;
49         __le16                  dz_mask;
50 #define DZ_MASK(dz)     ((dz)->dz_mask)
51 };
52
53 struct dn_hash
54 {
55         struct dn_zone  *dh_zones[17];
56         struct dn_zone  *dh_zone_list;
57 };
58
59 #define dz_key_0(key)           ((key).datum = 0)
60 #define dz_prefix(key,dz)       ((key).datum)
61
62 #define for_nexthops(fi) { int nhsel; const struct dn_fib_nh *nh;\
63         for(nhsel = 0, nh = (fi)->fib_nh; nhsel < (fi)->fib_nhs; nh++, nhsel++)
64
65 #define endfor_nexthops(fi) }
66
67 #define DN_MAX_DIVISOR 1024
68 #define DN_S_ZOMBIE 1
69 #define DN_S_ACCESSED 2
70
71 #define DN_FIB_SCAN(f, fp) \
72 for( ; ((f) = *(fp)) != NULL; (fp) = &(f)->fn_next)
73
74 #define DN_FIB_SCAN_KEY(f, fp, key) \
75 for( ; ((f) = *(fp)) != NULL && dn_key_eq((f)->fn_key, (key)); (fp) = &(f)->fn_next)
76
77 #define RT_TABLE_MIN 1
78
79 static DEFINE_RWLOCK(dn_fib_tables_lock);
80 struct dn_fib_table *dn_fib_tables[RT_TABLE_MAX + 1];
81
82 static kmem_cache_t *dn_hash_kmem __read_mostly;
83 static int dn_fib_hash_zombies;
84
85 static inline dn_fib_idx_t dn_hash(dn_fib_key_t key, struct dn_zone *dz)
86 {
87         u16 h = dn_ntohs(key.datum)>>(16 - dz->dz_order);
88         h ^= (h >> 10);
89         h ^= (h >> 6);
90         h &= DZ_HASHMASK(dz);
91         return *(dn_fib_idx_t *)&h;
92 }
93
94 static inline dn_fib_key_t dz_key(__le16 dst, struct dn_zone *dz)
95 {
96         dn_fib_key_t k;
97         k.datum = dst & DZ_MASK(dz);
98         return k;
99 }
100
101 static inline struct dn_fib_node **dn_chain_p(dn_fib_key_t key, struct dn_zone *dz)
102 {
103         return &dz->dz_hash[dn_hash(key, dz).datum];
104 }
105
106 static inline struct dn_fib_node *dz_chain(dn_fib_key_t key, struct dn_zone *dz)
107 {
108         return dz->dz_hash[dn_hash(key, dz).datum];
109 }
110
111 static inline int dn_key_eq(dn_fib_key_t a, dn_fib_key_t b)
112 {
113         return a.datum == b.datum;
114 }
115
116 static inline int dn_key_leq(dn_fib_key_t a, dn_fib_key_t b)
117 {
118         return a.datum <= b.datum;
119 }
120
121 static inline void dn_rebuild_zone(struct dn_zone *dz,
122                                    struct dn_fib_node **old_ht,
123                                    int old_divisor)
124 {
125         int i;
126         struct dn_fib_node *f, **fp, *next;
127
128         for(i = 0; i < old_divisor; i++) {
129                 for(f = old_ht[i]; f; f = f->fn_next) {
130                         next = f->fn_next;
131                         for(fp = dn_chain_p(f->fn_key, dz);
132                                 *fp && dn_key_leq((*fp)->fn_key, f->fn_key);
133                                 fp = &(*fp)->fn_next)
134                                 /* NOTHING */;
135                         f->fn_next = *fp;
136                         *fp = f;
137                 }
138         }
139 }
140
141 static void dn_rehash_zone(struct dn_zone *dz)
142 {
143         struct dn_fib_node **ht, **old_ht;
144         int old_divisor, new_divisor;
145         u32 new_hashmask;
146
147         old_divisor = dz->dz_divisor;
148
149         switch(old_divisor) {
150                 case 16:
151                         new_divisor = 256;
152                         new_hashmask = 0xFF;
153                         break;
154                 default:
155                         printk(KERN_DEBUG "DECnet: dn_rehash_zone: BUG! %d\n", old_divisor);
156                 case 256:
157                         new_divisor = 1024;
158                         new_hashmask = 0x3FF;
159                         break;
160         }
161
162         ht = kcalloc(new_divisor, sizeof(struct dn_fib_node*), GFP_KERNEL);
163         if (ht == NULL)
164                 return;
165
166         write_lock_bh(&dn_fib_tables_lock);
167         old_ht = dz->dz_hash;
168         dz->dz_hash = ht;
169         dz->dz_hashmask = new_hashmask;
170         dz->dz_divisor = new_divisor;
171         dn_rebuild_zone(dz, old_ht, old_divisor);
172         write_unlock_bh(&dn_fib_tables_lock);
173         kfree(old_ht);
174 }
175
176 static void dn_free_node(struct dn_fib_node *f)
177 {
178         dn_fib_release_info(DN_FIB_INFO(f));
179         kmem_cache_free(dn_hash_kmem, f);
180 }
181
182
183 static struct dn_zone *dn_new_zone(struct dn_hash *table, int z)
184 {
185         int i;
186         struct dn_zone *dz = kzalloc(sizeof(struct dn_zone), GFP_KERNEL);
187         if (!dz)
188                 return NULL;
189
190         if (z) {
191                 dz->dz_divisor = 16;
192                 dz->dz_hashmask = 0x0F;
193         } else {
194                 dz->dz_divisor = 1;
195                 dz->dz_hashmask = 0;
196         }
197
198         dz->dz_hash = kcalloc(dz->dz_divisor, sizeof(struct dn_fib_node *), GFP_KERNEL);
199         if (!dz->dz_hash) {
200                 kfree(dz);
201                 return NULL;
202         }
203
204         dz->dz_order = z;
205         dz->dz_mask = dnet_make_mask(z);
206
207         for(i = z + 1; i <= 16; i++)
208                 if (table->dh_zones[i])
209                         break;
210
211         write_lock_bh(&dn_fib_tables_lock);
212         if (i>16) {
213                 dz->dz_next = table->dh_zone_list;
214                 table->dh_zone_list = dz;
215         } else {
216                 dz->dz_next = table->dh_zones[i]->dz_next;
217                 table->dh_zones[i]->dz_next = dz;
218         }
219         table->dh_zones[z] = dz;
220         write_unlock_bh(&dn_fib_tables_lock);
221         return dz;
222 }
223
224
225 static int dn_fib_nh_match(struct rtmsg *r, struct nlmsghdr *nlh, struct dn_kern_rta *rta, struct dn_fib_info *fi)
226 {
227         struct rtnexthop *nhp;
228         int nhlen;
229
230         if (rta->rta_priority && *rta->rta_priority != fi->fib_priority)
231                 return 1;
232
233         if (rta->rta_oif || rta->rta_gw) {
234                 if ((!rta->rta_oif || *rta->rta_oif == fi->fib_nh->nh_oif) &&
235                     (!rta->rta_gw  || memcmp(rta->rta_gw, &fi->fib_nh->nh_gw, 2) == 0))
236                         return 0;
237                 return 1;
238         }
239
240         if (rta->rta_mp == NULL)
241                 return 0;
242
243         nhp = RTA_DATA(rta->rta_mp);
244         nhlen = RTA_PAYLOAD(rta->rta_mp);
245
246         for_nexthops(fi) {
247                 int attrlen = nhlen - sizeof(struct rtnexthop);
248                 __le16 gw;
249
250                 if (attrlen < 0 || (nhlen -= nhp->rtnh_len) < 0)
251                         return -EINVAL;
252                 if (nhp->rtnh_ifindex && nhp->rtnh_ifindex != nh->nh_oif)
253                         return 1;
254                 if (attrlen) {
255                         gw = dn_fib_get_attr16(RTNH_DATA(nhp), attrlen, RTA_GATEWAY);
256
257                         if (gw && gw != nh->nh_gw)
258                                 return 1;
259                 }
260                 nhp = RTNH_NEXT(nhp);
261         } endfor_nexthops(fi);
262
263         return 0;
264 }
265
266 static int dn_fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
267                         u8 tb_id, u8 type, u8 scope, void *dst, int dst_len,
268                         struct dn_fib_info *fi, unsigned int flags)
269 {
270         struct rtmsg *rtm;
271         struct nlmsghdr *nlh;
272         unsigned char *b = skb->tail;
273
274         nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*rtm), flags);
275         rtm = NLMSG_DATA(nlh);
276         rtm->rtm_family = AF_DECnet;
277         rtm->rtm_dst_len = dst_len;
278         rtm->rtm_src_len = 0;
279         rtm->rtm_tos = 0;
280         rtm->rtm_table = tb_id;
281         rtm->rtm_flags = fi->fib_flags;
282         rtm->rtm_scope = scope;
283         rtm->rtm_type  = type;
284         if (rtm->rtm_dst_len)
285                 RTA_PUT(skb, RTA_DST, 2, dst);
286         rtm->rtm_protocol = fi->fib_protocol;
287         if (fi->fib_priority)
288                 RTA_PUT(skb, RTA_PRIORITY, 4, &fi->fib_priority);
289         if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
290                 goto rtattr_failure;
291         if (fi->fib_nhs == 1) {
292                 if (fi->fib_nh->nh_gw)
293                         RTA_PUT(skb, RTA_GATEWAY, 2, &fi->fib_nh->nh_gw);
294                 if (fi->fib_nh->nh_oif)
295                         RTA_PUT(skb, RTA_OIF, sizeof(int), &fi->fib_nh->nh_oif);
296         }
297         if (fi->fib_nhs > 1) {
298                 struct rtnexthop *nhp;
299                 struct rtattr *mp_head;
300                 if (skb_tailroom(skb) <= RTA_SPACE(0))
301                         goto rtattr_failure;
302                 mp_head = (struct rtattr *)skb_put(skb, RTA_SPACE(0));
303
304                 for_nexthops(fi) {
305                         if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
306                                 goto rtattr_failure;
307                         nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
308                         nhp->rtnh_flags = nh->nh_flags & 0xFF;
309                         nhp->rtnh_hops = nh->nh_weight - 1;
310                         nhp->rtnh_ifindex = nh->nh_oif;
311                         if (nh->nh_gw)
312                                 RTA_PUT(skb, RTA_GATEWAY, 2, &nh->nh_gw);
313                         nhp->rtnh_len = skb->tail - (unsigned char *)nhp;
314                 } endfor_nexthops(fi);
315                 mp_head->rta_type = RTA_MULTIPATH;
316                 mp_head->rta_len = skb->tail - (u8*)mp_head;
317         }
318
319         nlh->nlmsg_len = skb->tail - b;
320         return skb->len;
321
322
323 nlmsg_failure:
324 rtattr_failure:
325         skb_trim(skb, b - skb->data);
326         return -1;
327 }
328
329
330 static void dn_rtmsg_fib(int event, struct dn_fib_node *f, int z, int tb_id,
331                         struct nlmsghdr *nlh, struct netlink_skb_parms *req)
332 {
333         struct sk_buff *skb;
334         u32 pid = req ? req->pid : 0;
335         int size = NLMSG_SPACE(sizeof(struct rtmsg) + 256);
336
337         skb = alloc_skb(size, GFP_KERNEL);
338         if (!skb)
339                 return;
340
341         if (dn_fib_dump_info(skb, pid, nlh->nlmsg_seq, event, tb_id, 
342                                 f->fn_type, f->fn_scope, &f->fn_key, z, 
343                                 DN_FIB_INFO(f), 0) < 0) {
344                 kfree_skb(skb);
345                 return;
346         }
347         NETLINK_CB(skb).dst_group = RTNLGRP_DECnet_ROUTE;
348         if (nlh->nlmsg_flags & NLM_F_ECHO)
349                 atomic_inc(&skb->users);
350         netlink_broadcast(rtnl, skb, pid, RTNLGRP_DECnet_ROUTE, GFP_KERNEL);
351         if (nlh->nlmsg_flags & NLM_F_ECHO)
352                 netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
353 }
354
355 static __inline__ int dn_hash_dump_bucket(struct sk_buff *skb, 
356                                 struct netlink_callback *cb,
357                                 struct dn_fib_table *tb,
358                                 struct dn_zone *dz,
359                                 struct dn_fib_node *f)
360 {
361         int i, s_i;
362
363         s_i = cb->args[3];
364         for(i = 0; f; i++, f = f->fn_next) {
365                 if (i < s_i)
366                         continue;
367                 if (f->fn_state & DN_S_ZOMBIE)
368                         continue;
369                 if (dn_fib_dump_info(skb, NETLINK_CB(cb->skb).pid, 
370                                 cb->nlh->nlmsg_seq,
371                                 RTM_NEWROUTE,
372                                 tb->n, 
373                                 (f->fn_state & DN_S_ZOMBIE) ? 0 : f->fn_type,
374                                 f->fn_scope, &f->fn_key, dz->dz_order, 
375                                 f->fn_info, NLM_F_MULTI) < 0) {
376                         cb->args[3] = i;
377                         return -1;
378                 }
379         }
380         cb->args[3] = i;
381         return skb->len;
382 }
383
384 static __inline__ int dn_hash_dump_zone(struct sk_buff *skb, 
385                                 struct netlink_callback *cb,
386                                 struct dn_fib_table *tb,
387                                 struct dn_zone *dz)
388 {
389         int h, s_h;
390
391         s_h = cb->args[2];
392         for(h = 0; h < dz->dz_divisor; h++) {
393                 if (h < s_h)
394                         continue;
395                 if (h > s_h)
396                         memset(&cb->args[3], 0, sizeof(cb->args) - 3*sizeof(cb->args[0]));
397                 if (dz->dz_hash == NULL || dz->dz_hash[h] == NULL)
398                         continue;
399                 if (dn_hash_dump_bucket(skb, cb, tb, dz, dz->dz_hash[h]) < 0) {
400                         cb->args[2] = h;
401                         return -1;
402                 }
403         }
404         cb->args[2] = h;
405         return skb->len;
406 }
407
408 static int dn_fib_table_dump(struct dn_fib_table *tb, struct sk_buff *skb, 
409                                 struct netlink_callback *cb)
410 {
411         int m, s_m;
412         struct dn_zone *dz;
413         struct dn_hash *table = (struct dn_hash *)tb->data;
414
415         s_m = cb->args[1];
416         read_lock(&dn_fib_tables_lock);
417         for(dz = table->dh_zone_list, m = 0; dz; dz = dz->dz_next, m++) {
418                 if (m < s_m)
419                         continue;
420                 if (m > s_m)
421                         memset(&cb->args[2], 0, sizeof(cb->args) - 2*sizeof(cb->args[0]));
422
423                 if (dn_hash_dump_zone(skb, cb, tb, dz) < 0) {
424                         cb->args[1] = m;
425                         read_unlock(&dn_fib_tables_lock);
426                         return -1;
427                 }
428         }
429         read_unlock(&dn_fib_tables_lock);
430         cb->args[1] = m;
431
432         return skb->len;
433 }
434
435 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)
436 {
437         struct dn_hash *table = (struct dn_hash *)tb->data;
438         struct dn_fib_node *new_f, *f, **fp, **del_fp;
439         struct dn_zone *dz;
440         struct dn_fib_info *fi;
441         int z = r->rtm_dst_len;
442         int type = r->rtm_type;
443         dn_fib_key_t key;
444         int err;
445
446         if (z > 16)
447                 return -EINVAL;
448
449         dz = table->dh_zones[z];
450         if (!dz && !(dz = dn_new_zone(table, z)))
451                 return -ENOBUFS;
452
453         dz_key_0(key);
454         if (rta->rta_dst) {
455                 __le16 dst;
456                 memcpy(&dst, rta->rta_dst, 2);
457                 if (dst & ~DZ_MASK(dz))
458                         return -EINVAL;
459                 key = dz_key(dst, dz);
460         }
461
462         if ((fi = dn_fib_create_info(r, rta, n, &err)) == NULL)
463                 return err;
464
465         if (dz->dz_nent > (dz->dz_divisor << 2) &&
466                         dz->dz_divisor > DN_MAX_DIVISOR &&
467                         (z==16 || (1<<z) > dz->dz_divisor))
468                 dn_rehash_zone(dz);
469
470         fp = dn_chain_p(key, dz);
471
472         DN_FIB_SCAN(f, fp) {
473                 if (dn_key_leq(key, f->fn_key))
474                         break;
475         }
476
477         del_fp = NULL;
478
479         if (f && (f->fn_state & DN_S_ZOMBIE) &&
480                         dn_key_eq(f->fn_key, key)) {
481                 del_fp = fp;
482                 fp = &f->fn_next;
483                 f = *fp;
484                 goto create;
485         }
486
487         DN_FIB_SCAN_KEY(f, fp, key) {
488                 if (fi->fib_priority <= DN_FIB_INFO(f)->fib_priority)
489                         break;
490         }
491
492         if (f && dn_key_eq(f->fn_key, key) &&
493                         fi->fib_priority == DN_FIB_INFO(f)->fib_priority) {
494                 struct dn_fib_node **ins_fp;
495
496                 err = -EEXIST;
497                 if (n->nlmsg_flags & NLM_F_EXCL)
498                         goto out;
499
500                 if (n->nlmsg_flags & NLM_F_REPLACE) {
501                         del_fp = fp;
502                         fp = &f->fn_next;
503                         f = *fp;
504                         goto replace;
505                 }
506
507                 ins_fp = fp;
508                 err = -EEXIST;
509
510                 DN_FIB_SCAN_KEY(f, fp, key) {
511                         if (fi->fib_priority != DN_FIB_INFO(f)->fib_priority)
512                                 break;
513                         if (f->fn_type == type && f->fn_scope == r->rtm_scope
514                                         && DN_FIB_INFO(f) == fi)
515                                 goto out;
516                 }
517
518                 if (!(n->nlmsg_flags & NLM_F_APPEND)) {
519                         fp = ins_fp;
520                         f = *fp;
521                 }
522         }
523
524 create:
525         err = -ENOENT;
526         if (!(n->nlmsg_flags & NLM_F_CREATE))
527                 goto out;
528
529 replace:
530         err = -ENOBUFS;
531         new_f = kmem_cache_alloc(dn_hash_kmem, SLAB_KERNEL);
532         if (new_f == NULL)
533                 goto out;
534
535         memset(new_f, 0, sizeof(struct dn_fib_node));
536
537         new_f->fn_key = key;
538         new_f->fn_type = type;
539         new_f->fn_scope = r->rtm_scope;
540         DN_FIB_INFO(new_f) = fi;
541
542         new_f->fn_next = f;
543         write_lock_bh(&dn_fib_tables_lock);
544         *fp = new_f;
545         write_unlock_bh(&dn_fib_tables_lock);
546         dz->dz_nent++;
547
548         if (del_fp) {
549                 f = *del_fp;
550                 write_lock_bh(&dn_fib_tables_lock);
551                 *del_fp = f->fn_next;
552                 write_unlock_bh(&dn_fib_tables_lock);
553
554                 if (!(f->fn_state & DN_S_ZOMBIE))
555                         dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
556                 if (f->fn_state & DN_S_ACCESSED)
557                         dn_rt_cache_flush(-1);
558                 dn_free_node(f);
559                 dz->dz_nent--;
560         } else {
561                 dn_rt_cache_flush(-1);
562         }
563
564         dn_rtmsg_fib(RTM_NEWROUTE, new_f, z, tb->n, n, req);
565
566         return 0;
567 out:
568         dn_fib_release_info(fi);
569         return err;
570 }
571
572
573 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)
574 {
575         struct dn_hash *table = (struct dn_hash*)tb->data;
576         struct dn_fib_node **fp, **del_fp, *f;
577         int z = r->rtm_dst_len;
578         struct dn_zone *dz;
579         dn_fib_key_t key;
580         int matched;
581
582
583         if (z > 16)
584                 return -EINVAL;
585
586         if ((dz = table->dh_zones[z]) == NULL)
587                 return -ESRCH;
588
589         dz_key_0(key);
590         if (rta->rta_dst) {
591                 __le16 dst;
592                 memcpy(&dst, rta->rta_dst, 2);
593                 if (dst & ~DZ_MASK(dz))
594                         return -EINVAL;
595                 key = dz_key(dst, dz);
596         }
597
598         fp = dn_chain_p(key, dz);
599
600         DN_FIB_SCAN(f, fp) {
601                 if (dn_key_eq(f->fn_key, key))
602                         break;
603                 if (dn_key_leq(key, f->fn_key))
604                         return -ESRCH;
605         }
606
607         matched = 0;
608         del_fp = NULL;
609         DN_FIB_SCAN_KEY(f, fp, key) {
610                 struct dn_fib_info *fi = DN_FIB_INFO(f);
611
612                 if (f->fn_state & DN_S_ZOMBIE)
613                         return -ESRCH;
614
615                 matched++;
616
617                 if (del_fp == NULL &&
618                                 (!r->rtm_type || f->fn_type == r->rtm_type) &&
619                                 (r->rtm_scope == RT_SCOPE_NOWHERE || f->fn_scope == r->rtm_scope) &&
620                                 (!r->rtm_protocol || 
621                                         fi->fib_protocol == r->rtm_protocol) &&
622                                 dn_fib_nh_match(r, n, rta, fi) == 0)
623                         del_fp = fp;
624         }
625
626         if (del_fp) {
627                 f = *del_fp;
628                 dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
629
630                 if (matched != 1) {
631                         write_lock_bh(&dn_fib_tables_lock);
632                         *del_fp = f->fn_next;
633                         write_unlock_bh(&dn_fib_tables_lock);
634
635                         if (f->fn_state & DN_S_ACCESSED)
636                                 dn_rt_cache_flush(-1);
637                         dn_free_node(f);
638                         dz->dz_nent--;
639                 } else {
640                         f->fn_state |= DN_S_ZOMBIE;
641                         if (f->fn_state & DN_S_ACCESSED) {
642                                 f->fn_state &= ~DN_S_ACCESSED;
643                                 dn_rt_cache_flush(-1);
644                         }
645                         if (++dn_fib_hash_zombies > 128)
646                                 dn_fib_flush();
647                 }
648
649                 return 0;
650         }
651
652         return -ESRCH;
653 }
654
655 static inline int dn_flush_list(struct dn_fib_node **fp, int z, struct dn_hash *table)
656 {
657         int found = 0;
658         struct dn_fib_node *f;
659
660         while((f = *fp) != NULL) {
661                 struct dn_fib_info *fi = DN_FIB_INFO(f);
662
663                 if (fi && ((f->fn_state & DN_S_ZOMBIE) || (fi->fib_flags & RTNH_F_DEAD))) {
664                         write_lock_bh(&dn_fib_tables_lock);
665                         *fp = f->fn_next;
666                         write_unlock_bh(&dn_fib_tables_lock);
667
668                         dn_free_node(f);
669                         found++;
670                         continue;
671                 }
672                 fp = &f->fn_next;
673         }
674
675         return found;
676 }
677
678 static int dn_fib_table_flush(struct dn_fib_table *tb)
679 {
680         struct dn_hash *table = (struct dn_hash *)tb->data;
681         struct dn_zone *dz;
682         int found = 0;
683
684         dn_fib_hash_zombies = 0;
685         for(dz = table->dh_zone_list; dz; dz = dz->dz_next) {
686                 int i;
687                 int tmp = 0;
688                 for(i = dz->dz_divisor-1; i >= 0; i--)
689                         tmp += dn_flush_list(&dz->dz_hash[i], dz->dz_order, table);
690                 dz->dz_nent -= tmp;
691                 found += tmp;
692         }
693
694         return found;
695 }
696
697 static int dn_fib_table_lookup(struct dn_fib_table *tb, const struct flowi *flp, struct dn_fib_res *res)
698 {
699         int err;
700         struct dn_zone *dz;
701         struct dn_hash *t = (struct dn_hash *)tb->data;
702
703         read_lock(&dn_fib_tables_lock);
704         for(dz = t->dh_zone_list; dz; dz = dz->dz_next) {
705                 struct dn_fib_node *f;
706                 dn_fib_key_t k = dz_key(flp->fld_dst, dz);
707
708                 for(f = dz_chain(k, dz); f; f = f->fn_next) {
709                         if (!dn_key_eq(k, f->fn_key)) {
710                                 if (dn_key_leq(k, f->fn_key))
711                                         break;
712                                 else
713                                         continue;
714                         }
715
716                         f->fn_state |= DN_S_ACCESSED;
717
718                         if (f->fn_state&DN_S_ZOMBIE)
719                                 continue;
720
721                         if (f->fn_scope < flp->fld_scope)
722                                 continue;
723
724                         err = dn_fib_semantic_match(f->fn_type, DN_FIB_INFO(f), flp, res);
725
726                         if (err == 0) {
727                                 res->type = f->fn_type;
728                                 res->scope = f->fn_scope; 
729                                 res->prefixlen = dz->dz_order;
730                                 goto out;
731                         }
732                         if (err < 0)
733                                 goto out;
734                 }
735         }
736         err = 1;
737 out:
738         read_unlock(&dn_fib_tables_lock);
739         return err;
740 }
741
742
743 struct dn_fib_table *dn_fib_get_table(int n, int create)
744 {
745         struct dn_fib_table *t;
746
747         if (n < RT_TABLE_MIN)
748                 return NULL;
749
750         if (n > RT_TABLE_MAX)
751                 return NULL;
752
753         if (dn_fib_tables[n]) 
754                 return dn_fib_tables[n];
755
756         if (!create)
757                 return NULL;
758
759         if (in_interrupt() && net_ratelimit()) {
760                 printk(KERN_DEBUG "DECnet: BUG! Attempt to create routing table from interrupt\n"); 
761                 return NULL;
762         }
763         if ((t = kmalloc(sizeof(struct dn_fib_table) + sizeof(struct dn_hash), GFP_KERNEL)) == NULL)
764                 return NULL;
765
766         memset(t, 0, sizeof(struct dn_fib_table));
767
768         t->n = n;
769         t->insert = dn_fib_table_insert;
770         t->delete = dn_fib_table_delete;
771         t->lookup = dn_fib_table_lookup;
772         t->flush  = dn_fib_table_flush;
773         t->dump = dn_fib_table_dump;
774         memset(t->data, 0, sizeof(struct dn_hash));
775         dn_fib_tables[n] = t;
776
777         return t;
778 }
779
780 static void dn_fib_del_tree(int n)
781 {
782         struct dn_fib_table *t;
783
784         write_lock(&dn_fib_tables_lock);
785         t = dn_fib_tables[n];
786         dn_fib_tables[n] = NULL;
787         write_unlock(&dn_fib_tables_lock);
788
789         kfree(t);
790 }
791
792 struct dn_fib_table *dn_fib_empty_table(void)
793 {
794         int id;
795
796         for(id = RT_TABLE_MIN; id <= RT_TABLE_MAX; id++)
797                 if (dn_fib_tables[id] == NULL)
798                         return dn_fib_get_table(id, 1);
799         return NULL;
800 }
801
802 void __init dn_fib_table_init(void)
803 {
804         dn_hash_kmem = kmem_cache_create("dn_fib_info_cache",
805                                         sizeof(struct dn_fib_info),
806                                         0, SLAB_HWCACHE_ALIGN,
807                                         NULL, NULL);
808 }
809
810 void __exit dn_fib_table_cleanup(void)
811 {
812         int i;
813
814         for (i = RT_TABLE_MIN; i <= RT_TABLE_MAX; ++i)
815                 dn_fib_del_tree(i);
816
817         return;
818 }