NetLabel: correct CIPSO tag handling when adding new DOI definitions
[linux-2.6] / 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 #define DN_FIB_TABLE_HASHSZ 256
79 static struct hlist_head dn_fib_table_hash[DN_FIB_TABLE_HASHSZ];
80 static DEFINE_RWLOCK(dn_fib_tables_lock);
81
82 static struct kmem_cache *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 inline size_t dn_fib_nlmsg_size(struct dn_fib_info *fi)
267 {
268         size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
269                          + nla_total_size(4) /* RTA_TABLE */
270                          + nla_total_size(2) /* RTA_DST */
271                          + nla_total_size(4); /* RTA_PRIORITY */
272
273         /* space for nested metrics */
274         payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
275
276         if (fi->fib_nhs) {
277                 /* Also handles the special case fib_nhs == 1 */
278
279                 /* each nexthop is packed in an attribute */
280                 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
281
282                 /* may contain a gateway attribute */
283                 nhsize += nla_total_size(4);
284
285                 /* all nexthops are packed in a nested attribute */
286                 payload += nla_total_size(fi->fib_nhs * nhsize);
287         }
288
289         return payload;
290 }
291
292 static int dn_fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
293                         u32 tb_id, u8 type, u8 scope, void *dst, int dst_len,
294                         struct dn_fib_info *fi, unsigned int flags)
295 {
296         struct rtmsg *rtm;
297         struct nlmsghdr *nlh;
298         unsigned char *b = skb->tail;
299
300         nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*rtm), flags);
301         rtm = NLMSG_DATA(nlh);
302         rtm->rtm_family = AF_DECnet;
303         rtm->rtm_dst_len = dst_len;
304         rtm->rtm_src_len = 0;
305         rtm->rtm_tos = 0;
306         rtm->rtm_table = tb_id;
307         RTA_PUT_U32(skb, RTA_TABLE, tb_id);
308         rtm->rtm_flags = fi->fib_flags;
309         rtm->rtm_scope = scope;
310         rtm->rtm_type  = type;
311         if (rtm->rtm_dst_len)
312                 RTA_PUT(skb, RTA_DST, 2, dst);
313         rtm->rtm_protocol = fi->fib_protocol;
314         if (fi->fib_priority)
315                 RTA_PUT(skb, RTA_PRIORITY, 4, &fi->fib_priority);
316         if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
317                 goto rtattr_failure;
318         if (fi->fib_nhs == 1) {
319                 if (fi->fib_nh->nh_gw)
320                         RTA_PUT(skb, RTA_GATEWAY, 2, &fi->fib_nh->nh_gw);
321                 if (fi->fib_nh->nh_oif)
322                         RTA_PUT(skb, RTA_OIF, sizeof(int), &fi->fib_nh->nh_oif);
323         }
324         if (fi->fib_nhs > 1) {
325                 struct rtnexthop *nhp;
326                 struct rtattr *mp_head;
327                 if (skb_tailroom(skb) <= RTA_SPACE(0))
328                         goto rtattr_failure;
329                 mp_head = (struct rtattr *)skb_put(skb, RTA_SPACE(0));
330
331                 for_nexthops(fi) {
332                         if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
333                                 goto rtattr_failure;
334                         nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
335                         nhp->rtnh_flags = nh->nh_flags & 0xFF;
336                         nhp->rtnh_hops = nh->nh_weight - 1;
337                         nhp->rtnh_ifindex = nh->nh_oif;
338                         if (nh->nh_gw)
339                                 RTA_PUT(skb, RTA_GATEWAY, 2, &nh->nh_gw);
340                         nhp->rtnh_len = skb->tail - (unsigned char *)nhp;
341                 } endfor_nexthops(fi);
342                 mp_head->rta_type = RTA_MULTIPATH;
343                 mp_head->rta_len = skb->tail - (u8*)mp_head;
344         }
345
346         nlh->nlmsg_len = skb->tail - b;
347         return skb->len;
348
349
350 nlmsg_failure:
351 rtattr_failure:
352         skb_trim(skb, b - skb->data);
353         return -1;
354 }
355
356
357 static void dn_rtmsg_fib(int event, struct dn_fib_node *f, int z, u32 tb_id,
358                         struct nlmsghdr *nlh, struct netlink_skb_parms *req)
359 {
360         struct sk_buff *skb;
361         u32 pid = req ? req->pid : 0;
362         int err = -ENOBUFS;
363
364         skb = nlmsg_new(dn_fib_nlmsg_size(DN_FIB_INFO(f)), GFP_KERNEL);
365         if (skb == NULL)
366                 goto errout;
367
368         err = dn_fib_dump_info(skb, pid, nlh->nlmsg_seq, event, tb_id,
369                                f->fn_type, f->fn_scope, &f->fn_key, z,
370                                DN_FIB_INFO(f), 0);
371         /* failure implies BUG in dn_fib_nlmsg_size() */
372         BUG_ON(err < 0);
373
374         err = rtnl_notify(skb, pid, RTNLGRP_DECnet_ROUTE, nlh, GFP_KERNEL);
375 errout:
376         if (err < 0)
377                 rtnl_set_sk_err(RTNLGRP_DECnet_ROUTE, err);
378 }
379
380 static __inline__ int dn_hash_dump_bucket(struct sk_buff *skb, 
381                                 struct netlink_callback *cb,
382                                 struct dn_fib_table *tb,
383                                 struct dn_zone *dz,
384                                 struct dn_fib_node *f)
385 {
386         int i, s_i;
387
388         s_i = cb->args[4];
389         for(i = 0; f; i++, f = f->fn_next) {
390                 if (i < s_i)
391                         continue;
392                 if (f->fn_state & DN_S_ZOMBIE)
393                         continue;
394                 if (dn_fib_dump_info(skb, NETLINK_CB(cb->skb).pid, 
395                                 cb->nlh->nlmsg_seq,
396                                 RTM_NEWROUTE,
397                                 tb->n, 
398                                 (f->fn_state & DN_S_ZOMBIE) ? 0 : f->fn_type,
399                                 f->fn_scope, &f->fn_key, dz->dz_order, 
400                                 f->fn_info, NLM_F_MULTI) < 0) {
401                         cb->args[4] = i;
402                         return -1;
403                 }
404         }
405         cb->args[4] = i;
406         return skb->len;
407 }
408
409 static __inline__ int dn_hash_dump_zone(struct sk_buff *skb, 
410                                 struct netlink_callback *cb,
411                                 struct dn_fib_table *tb,
412                                 struct dn_zone *dz)
413 {
414         int h, s_h;
415
416         s_h = cb->args[3];
417         for(h = 0; h < dz->dz_divisor; h++) {
418                 if (h < s_h)
419                         continue;
420                 if (h > s_h)
421                         memset(&cb->args[4], 0, sizeof(cb->args) - 4*sizeof(cb->args[0]));
422                 if (dz->dz_hash == NULL || dz->dz_hash[h] == NULL)
423                         continue;
424                 if (dn_hash_dump_bucket(skb, cb, tb, dz, dz->dz_hash[h]) < 0) {
425                         cb->args[3] = h;
426                         return -1;
427                 }
428         }
429         cb->args[3] = h;
430         return skb->len;
431 }
432
433 static int dn_fib_table_dump(struct dn_fib_table *tb, struct sk_buff *skb, 
434                                 struct netlink_callback *cb)
435 {
436         int m, s_m;
437         struct dn_zone *dz;
438         struct dn_hash *table = (struct dn_hash *)tb->data;
439
440         s_m = cb->args[2];
441         read_lock(&dn_fib_tables_lock);
442         for(dz = table->dh_zone_list, m = 0; dz; dz = dz->dz_next, m++) {
443                 if (m < s_m)
444                         continue;
445                 if (m > s_m)
446                         memset(&cb->args[3], 0, sizeof(cb->args) - 3*sizeof(cb->args[0]));
447
448                 if (dn_hash_dump_zone(skb, cb, tb, dz) < 0) {
449                         cb->args[2] = m;
450                         read_unlock(&dn_fib_tables_lock);
451                         return -1;
452                 }
453         }
454         read_unlock(&dn_fib_tables_lock);
455         cb->args[2] = m;
456
457         return skb->len;
458 }
459
460 int dn_fib_dump(struct sk_buff *skb, struct netlink_callback *cb)
461 {
462         unsigned int h, s_h;
463         unsigned int e = 0, s_e;
464         struct dn_fib_table *tb;
465         struct hlist_node *node;
466         int dumped = 0;
467
468         if (NLMSG_PAYLOAD(cb->nlh, 0) >= sizeof(struct rtmsg) &&
469                 ((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED)
470                         return dn_cache_dump(skb, cb);
471
472         s_h = cb->args[0];
473         s_e = cb->args[1];
474
475         for (h = s_h; h < DN_FIB_TABLE_HASHSZ; h++, s_h = 0) {
476                 e = 0;
477                 hlist_for_each_entry(tb, node, &dn_fib_table_hash[h], hlist) {
478                         if (e < s_e)
479                                 goto next;
480                         if (dumped)
481                                 memset(&cb->args[2], 0, sizeof(cb->args) -
482                                                  2 * sizeof(cb->args[0]));
483                         if (tb->dump(tb, skb, cb) < 0)
484                                 goto out;
485                         dumped = 1;
486 next:
487                         e++;
488                 }
489         }
490 out:
491         cb->args[1] = e;
492         cb->args[0] = h;
493
494         return skb->len;
495 }
496
497 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)
498 {
499         struct dn_hash *table = (struct dn_hash *)tb->data;
500         struct dn_fib_node *new_f, *f, **fp, **del_fp;
501         struct dn_zone *dz;
502         struct dn_fib_info *fi;
503         int z = r->rtm_dst_len;
504         int type = r->rtm_type;
505         dn_fib_key_t key;
506         int err;
507
508         if (z > 16)
509                 return -EINVAL;
510
511         dz = table->dh_zones[z];
512         if (!dz && !(dz = dn_new_zone(table, z)))
513                 return -ENOBUFS;
514
515         dz_key_0(key);
516         if (rta->rta_dst) {
517                 __le16 dst;
518                 memcpy(&dst, rta->rta_dst, 2);
519                 if (dst & ~DZ_MASK(dz))
520                         return -EINVAL;
521                 key = dz_key(dst, dz);
522         }
523
524         if ((fi = dn_fib_create_info(r, rta, n, &err)) == NULL)
525                 return err;
526
527         if (dz->dz_nent > (dz->dz_divisor << 2) &&
528                         dz->dz_divisor > DN_MAX_DIVISOR &&
529                         (z==16 || (1<<z) > dz->dz_divisor))
530                 dn_rehash_zone(dz);
531
532         fp = dn_chain_p(key, dz);
533
534         DN_FIB_SCAN(f, fp) {
535                 if (dn_key_leq(key, f->fn_key))
536                         break;
537         }
538
539         del_fp = NULL;
540
541         if (f && (f->fn_state & DN_S_ZOMBIE) &&
542                         dn_key_eq(f->fn_key, key)) {
543                 del_fp = fp;
544                 fp = &f->fn_next;
545                 f = *fp;
546                 goto create;
547         }
548
549         DN_FIB_SCAN_KEY(f, fp, key) {
550                 if (fi->fib_priority <= DN_FIB_INFO(f)->fib_priority)
551                         break;
552         }
553
554         if (f && dn_key_eq(f->fn_key, key) &&
555                         fi->fib_priority == DN_FIB_INFO(f)->fib_priority) {
556                 struct dn_fib_node **ins_fp;
557
558                 err = -EEXIST;
559                 if (n->nlmsg_flags & NLM_F_EXCL)
560                         goto out;
561
562                 if (n->nlmsg_flags & NLM_F_REPLACE) {
563                         del_fp = fp;
564                         fp = &f->fn_next;
565                         f = *fp;
566                         goto replace;
567                 }
568
569                 ins_fp = fp;
570                 err = -EEXIST;
571
572                 DN_FIB_SCAN_KEY(f, fp, key) {
573                         if (fi->fib_priority != DN_FIB_INFO(f)->fib_priority)
574                                 break;
575                         if (f->fn_type == type && f->fn_scope == r->rtm_scope
576                                         && DN_FIB_INFO(f) == fi)
577                                 goto out;
578                 }
579
580                 if (!(n->nlmsg_flags & NLM_F_APPEND)) {
581                         fp = ins_fp;
582                         f = *fp;
583                 }
584         }
585
586 create:
587         err = -ENOENT;
588         if (!(n->nlmsg_flags & NLM_F_CREATE))
589                 goto out;
590
591 replace:
592         err = -ENOBUFS;
593         new_f = kmem_cache_alloc(dn_hash_kmem, GFP_KERNEL);
594         if (new_f == NULL)
595                 goto out;
596
597         memset(new_f, 0, sizeof(struct dn_fib_node));
598
599         new_f->fn_key = key;
600         new_f->fn_type = type;
601         new_f->fn_scope = r->rtm_scope;
602         DN_FIB_INFO(new_f) = fi;
603
604         new_f->fn_next = f;
605         write_lock_bh(&dn_fib_tables_lock);
606         *fp = new_f;
607         write_unlock_bh(&dn_fib_tables_lock);
608         dz->dz_nent++;
609
610         if (del_fp) {
611                 f = *del_fp;
612                 write_lock_bh(&dn_fib_tables_lock);
613                 *del_fp = f->fn_next;
614                 write_unlock_bh(&dn_fib_tables_lock);
615
616                 if (!(f->fn_state & DN_S_ZOMBIE))
617                         dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
618                 if (f->fn_state & DN_S_ACCESSED)
619                         dn_rt_cache_flush(-1);
620                 dn_free_node(f);
621                 dz->dz_nent--;
622         } else {
623                 dn_rt_cache_flush(-1);
624         }
625
626         dn_rtmsg_fib(RTM_NEWROUTE, new_f, z, tb->n, n, req);
627
628         return 0;
629 out:
630         dn_fib_release_info(fi);
631         return err;
632 }
633
634
635 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)
636 {
637         struct dn_hash *table = (struct dn_hash*)tb->data;
638         struct dn_fib_node **fp, **del_fp, *f;
639         int z = r->rtm_dst_len;
640         struct dn_zone *dz;
641         dn_fib_key_t key;
642         int matched;
643
644
645         if (z > 16)
646                 return -EINVAL;
647
648         if ((dz = table->dh_zones[z]) == NULL)
649                 return -ESRCH;
650
651         dz_key_0(key);
652         if (rta->rta_dst) {
653                 __le16 dst;
654                 memcpy(&dst, rta->rta_dst, 2);
655                 if (dst & ~DZ_MASK(dz))
656                         return -EINVAL;
657                 key = dz_key(dst, dz);
658         }
659
660         fp = dn_chain_p(key, dz);
661
662         DN_FIB_SCAN(f, fp) {
663                 if (dn_key_eq(f->fn_key, key))
664                         break;
665                 if (dn_key_leq(key, f->fn_key))
666                         return -ESRCH;
667         }
668
669         matched = 0;
670         del_fp = NULL;
671         DN_FIB_SCAN_KEY(f, fp, key) {
672                 struct dn_fib_info *fi = DN_FIB_INFO(f);
673
674                 if (f->fn_state & DN_S_ZOMBIE)
675                         return -ESRCH;
676
677                 matched++;
678
679                 if (del_fp == NULL &&
680                                 (!r->rtm_type || f->fn_type == r->rtm_type) &&
681                                 (r->rtm_scope == RT_SCOPE_NOWHERE || f->fn_scope == r->rtm_scope) &&
682                                 (!r->rtm_protocol || 
683                                         fi->fib_protocol == r->rtm_protocol) &&
684                                 dn_fib_nh_match(r, n, rta, fi) == 0)
685                         del_fp = fp;
686         }
687
688         if (del_fp) {
689                 f = *del_fp;
690                 dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
691
692                 if (matched != 1) {
693                         write_lock_bh(&dn_fib_tables_lock);
694                         *del_fp = f->fn_next;
695                         write_unlock_bh(&dn_fib_tables_lock);
696
697                         if (f->fn_state & DN_S_ACCESSED)
698                                 dn_rt_cache_flush(-1);
699                         dn_free_node(f);
700                         dz->dz_nent--;
701                 } else {
702                         f->fn_state |= DN_S_ZOMBIE;
703                         if (f->fn_state & DN_S_ACCESSED) {
704                                 f->fn_state &= ~DN_S_ACCESSED;
705                                 dn_rt_cache_flush(-1);
706                         }
707                         if (++dn_fib_hash_zombies > 128)
708                                 dn_fib_flush();
709                 }
710
711                 return 0;
712         }
713
714         return -ESRCH;
715 }
716
717 static inline int dn_flush_list(struct dn_fib_node **fp, int z, struct dn_hash *table)
718 {
719         int found = 0;
720         struct dn_fib_node *f;
721
722         while((f = *fp) != NULL) {
723                 struct dn_fib_info *fi = DN_FIB_INFO(f);
724
725                 if (fi && ((f->fn_state & DN_S_ZOMBIE) || (fi->fib_flags & RTNH_F_DEAD))) {
726                         write_lock_bh(&dn_fib_tables_lock);
727                         *fp = f->fn_next;
728                         write_unlock_bh(&dn_fib_tables_lock);
729
730                         dn_free_node(f);
731                         found++;
732                         continue;
733                 }
734                 fp = &f->fn_next;
735         }
736
737         return found;
738 }
739
740 static int dn_fib_table_flush(struct dn_fib_table *tb)
741 {
742         struct dn_hash *table = (struct dn_hash *)tb->data;
743         struct dn_zone *dz;
744         int found = 0;
745
746         dn_fib_hash_zombies = 0;
747         for(dz = table->dh_zone_list; dz; dz = dz->dz_next) {
748                 int i;
749                 int tmp = 0;
750                 for(i = dz->dz_divisor-1; i >= 0; i--)
751                         tmp += dn_flush_list(&dz->dz_hash[i], dz->dz_order, table);
752                 dz->dz_nent -= tmp;
753                 found += tmp;
754         }
755
756         return found;
757 }
758
759 static int dn_fib_table_lookup(struct dn_fib_table *tb, const struct flowi *flp, struct dn_fib_res *res)
760 {
761         int err;
762         struct dn_zone *dz;
763         struct dn_hash *t = (struct dn_hash *)tb->data;
764
765         read_lock(&dn_fib_tables_lock);
766         for(dz = t->dh_zone_list; dz; dz = dz->dz_next) {
767                 struct dn_fib_node *f;
768                 dn_fib_key_t k = dz_key(flp->fld_dst, dz);
769
770                 for(f = dz_chain(k, dz); f; f = f->fn_next) {
771                         if (!dn_key_eq(k, f->fn_key)) {
772                                 if (dn_key_leq(k, f->fn_key))
773                                         break;
774                                 else
775                                         continue;
776                         }
777
778                         f->fn_state |= DN_S_ACCESSED;
779
780                         if (f->fn_state&DN_S_ZOMBIE)
781                                 continue;
782
783                         if (f->fn_scope < flp->fld_scope)
784                                 continue;
785
786                         err = dn_fib_semantic_match(f->fn_type, DN_FIB_INFO(f), flp, res);
787
788                         if (err == 0) {
789                                 res->type = f->fn_type;
790                                 res->scope = f->fn_scope; 
791                                 res->prefixlen = dz->dz_order;
792                                 goto out;
793                         }
794                         if (err < 0)
795                                 goto out;
796                 }
797         }
798         err = 1;
799 out:
800         read_unlock(&dn_fib_tables_lock);
801         return err;
802 }
803
804
805 struct dn_fib_table *dn_fib_get_table(u32 n, int create)
806 {
807         struct dn_fib_table *t;
808         struct hlist_node *node;
809         unsigned int h;
810
811         if (n < RT_TABLE_MIN)
812                 return NULL;
813
814         if (n > RT_TABLE_MAX)
815                 return NULL;
816
817         h = n & (DN_FIB_TABLE_HASHSZ - 1);
818         rcu_read_lock();
819         hlist_for_each_entry_rcu(t, node, &dn_fib_table_hash[h], hlist) {
820                 if (t->n == n) {
821                         rcu_read_unlock();
822                         return t;
823                 }
824         }
825         rcu_read_unlock();
826
827         if (!create)
828                 return NULL;
829
830         if (in_interrupt() && net_ratelimit()) {
831                 printk(KERN_DEBUG "DECnet: BUG! Attempt to create routing table from interrupt\n"); 
832                 return NULL;
833         }
834
835         t = kzalloc(sizeof(struct dn_fib_table) + sizeof(struct dn_hash),
836                     GFP_KERNEL);
837         if (t == NULL)
838                 return NULL;
839
840         t->n = n;
841         t->insert = dn_fib_table_insert;
842         t->delete = dn_fib_table_delete;
843         t->lookup = dn_fib_table_lookup;
844         t->flush  = dn_fib_table_flush;
845         t->dump = dn_fib_table_dump;
846         hlist_add_head_rcu(&t->hlist, &dn_fib_table_hash[h]);
847
848         return t;
849 }
850
851 struct dn_fib_table *dn_fib_empty_table(void)
852 {
853         u32 id;
854
855         for(id = RT_TABLE_MIN; id <= RT_TABLE_MAX; id++)
856                 if (dn_fib_get_table(id, 0) == NULL)
857                         return dn_fib_get_table(id, 1);
858         return NULL;
859 }
860
861 void dn_fib_flush(void)
862 {
863         int flushed = 0;
864         struct dn_fib_table *tb;
865         struct hlist_node *node;
866         unsigned int h;
867
868         for (h = 0; h < DN_FIB_TABLE_HASHSZ; h++) {
869                 hlist_for_each_entry(tb, node, &dn_fib_table_hash[h], hlist)
870                         flushed += tb->flush(tb);
871         }
872
873         if (flushed)
874                 dn_rt_cache_flush(-1);
875 }
876
877 void __init dn_fib_table_init(void)
878 {
879         dn_hash_kmem = kmem_cache_create("dn_fib_info_cache",
880                                         sizeof(struct dn_fib_info),
881                                         0, SLAB_HWCACHE_ALIGN,
882                                         NULL, NULL);
883 }
884
885 void __exit dn_fib_table_cleanup(void)
886 {
887         struct dn_fib_table *t;
888         struct hlist_node *node, *next;
889         unsigned int h;
890
891         write_lock(&dn_fib_tables_lock);
892         for (h = 0; h < DN_FIB_TABLE_HASHSZ; h++) {
893                 hlist_for_each_entry_safe(t, node, next, &dn_fib_table_hash[h],
894                                           hlist) {
895                         hlist_del(&t->hlist);
896                         kfree(t);
897                 }
898         }
899         write_unlock(&dn_fib_tables_lock);
900 }