Linux-libre 4.14.138-gnu
[librecmc/linux-libre.git] / net / decnet / dn_table.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * DECnet       An implementation of the DECnet protocol suite for the LINUX
4  *              operating system.  DECnet is implemented using the  BSD Socket
5  *              interface as the means of communication with the user level.
6  *
7  *              DECnet Routing Forwarding Information Base (Routing Tables)
8  *
9  * Author:      Steve Whitehouse <SteveW@ACM.org>
10  *              Mostly copied from the IPv4 routing code
11  *
12  *
13  * Changes:
14  *
15  */
16 #include <linux/string.h>
17 #include <linux/net.h>
18 #include <linux/socket.h>
19 #include <linux/slab.h>
20 #include <linux/sockios.h>
21 #include <linux/init.h>
22 #include <linux/skbuff.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 <linux/atomic.h>
29 #include <linux/uaccess.h>
30 #include <linux/route.h> /* RTF_xxx */
31 #include <net/neighbour.h>
32 #include <net/netlink.h>
33 #include <net/tcp.h>
34 #include <net/dst.h>
35 #include <net/flow.h>
36 #include <net/fib_rules.h>
37 #include <net/dn.h>
38 #include <net/dn_route.h>
39 #include <net/dn_fib.h>
40 #include <net/dn_neigh.h>
41 #include <net/dn_dev.h>
42
43 struct dn_zone
44 {
45         struct dn_zone          *dz_next;
46         struct dn_fib_node      **dz_hash;
47         int                     dz_nent;
48         int                     dz_divisor;
49         u32                     dz_hashmask;
50 #define DZ_HASHMASK(dz) ((dz)->dz_hashmask)
51         int                     dz_order;
52         __le16                  dz_mask;
53 #define DZ_MASK(dz)     ((dz)->dz_mask)
54 };
55
56 struct dn_hash
57 {
58         struct dn_zone  *dh_zones[17];
59         struct dn_zone  *dh_zone_list;
60 };
61
62 #define dz_key_0(key)           ((key).datum = 0)
63
64 #define for_nexthops(fi) { int nhsel; const struct dn_fib_nh *nh;\
65         for(nhsel = 0, nh = (fi)->fib_nh; nhsel < (fi)->fib_nhs; nh++, nhsel++)
66
67 #define endfor_nexthops(fi) }
68
69 #define DN_MAX_DIVISOR 1024
70 #define DN_S_ZOMBIE 1
71 #define DN_S_ACCESSED 2
72
73 #define DN_FIB_SCAN(f, fp) \
74 for( ; ((f) = *(fp)) != NULL; (fp) = &(f)->fn_next)
75
76 #define DN_FIB_SCAN_KEY(f, fp, key) \
77 for( ; ((f) = *(fp)) != NULL && dn_key_eq((f)->fn_key, (key)); (fp) = &(f)->fn_next)
78
79 #define RT_TABLE_MIN 1
80 #define DN_FIB_TABLE_HASHSZ 256
81 static struct hlist_head dn_fib_table_hash[DN_FIB_TABLE_HASHSZ];
82 static DEFINE_RWLOCK(dn_fib_tables_lock);
83
84 static struct kmem_cache *dn_hash_kmem __read_mostly;
85 static int dn_fib_hash_zombies;
86
87 static inline dn_fib_idx_t dn_hash(dn_fib_key_t key, struct dn_zone *dz)
88 {
89         u16 h = le16_to_cpu(key.datum)>>(16 - dz->dz_order);
90         h ^= (h >> 10);
91         h ^= (h >> 6);
92         h &= DZ_HASHMASK(dz);
93         return *(dn_fib_idx_t *)&h;
94 }
95
96 static inline dn_fib_key_t dz_key(__le16 dst, struct dn_zone *dz)
97 {
98         dn_fib_key_t k;
99         k.datum = dst & DZ_MASK(dz);
100         return k;
101 }
102
103 static inline struct dn_fib_node **dn_chain_p(dn_fib_key_t key, struct dn_zone *dz)
104 {
105         return &dz->dz_hash[dn_hash(key, dz).datum];
106 }
107
108 static inline struct dn_fib_node *dz_chain(dn_fib_key_t key, struct dn_zone *dz)
109 {
110         return dz->dz_hash[dn_hash(key, dz).datum];
111 }
112
113 static inline int dn_key_eq(dn_fib_key_t a, dn_fib_key_t b)
114 {
115         return a.datum == b.datum;
116 }
117
118 static inline int dn_key_leq(dn_fib_key_t a, dn_fib_key_t b)
119 {
120         return a.datum <= b.datum;
121 }
122
123 static inline void dn_rebuild_zone(struct dn_zone *dz,
124                                    struct dn_fib_node **old_ht,
125                                    int old_divisor)
126 {
127         struct dn_fib_node *f, **fp, *next;
128         int i;
129
130         for(i = 0; i < old_divisor; i++) {
131                 for(f = old_ht[i]; f; f = next) {
132                         next = f->fn_next;
133                         for(fp = dn_chain_p(f->fn_key, dz);
134                                 *fp && dn_key_leq((*fp)->fn_key, f->fn_key);
135                                 fp = &(*fp)->fn_next)
136                                 /* NOTHING */;
137                         f->fn_next = *fp;
138                         *fp = f;
139                 }
140         }
141 }
142
143 static void dn_rehash_zone(struct dn_zone *dz)
144 {
145         struct dn_fib_node **ht, **old_ht;
146         int old_divisor, new_divisor;
147         u32 new_hashmask;
148
149         old_divisor = dz->dz_divisor;
150
151         switch (old_divisor) {
152         case 16:
153                 new_divisor = 256;
154                 new_hashmask = 0xFF;
155                 break;
156         default:
157                 printk(KERN_DEBUG "DECnet: dn_rehash_zone: BUG! %d\n",
158                        old_divisor);
159         case 256:
160                 new_divisor = 1024;
161                 new_hashmask = 0x3FF;
162                 break;
163         }
164
165         ht = kcalloc(new_divisor, sizeof(struct dn_fib_node*), GFP_KERNEL);
166         if (ht == NULL)
167                 return;
168
169         write_lock_bh(&dn_fib_tables_lock);
170         old_ht = dz->dz_hash;
171         dz->dz_hash = ht;
172         dz->dz_hashmask = new_hashmask;
173         dz->dz_divisor = new_divisor;
174         dn_rebuild_zone(dz, old_ht, old_divisor);
175         write_unlock_bh(&dn_fib_tables_lock);
176         kfree(old_ht);
177 }
178
179 static void dn_free_node(struct dn_fib_node *f)
180 {
181         dn_fib_release_info(DN_FIB_INFO(f));
182         kmem_cache_free(dn_hash_kmem, f);
183 }
184
185
186 static struct dn_zone *dn_new_zone(struct dn_hash *table, int z)
187 {
188         int i;
189         struct dn_zone *dz = kzalloc(sizeof(struct dn_zone), GFP_KERNEL);
190         if (!dz)
191                 return NULL;
192
193         if (z) {
194                 dz->dz_divisor = 16;
195                 dz->dz_hashmask = 0x0F;
196         } else {
197                 dz->dz_divisor = 1;
198                 dz->dz_hashmask = 0;
199         }
200
201         dz->dz_hash = kcalloc(dz->dz_divisor, sizeof(struct dn_fib_node *), GFP_KERNEL);
202         if (!dz->dz_hash) {
203                 kfree(dz);
204                 return NULL;
205         }
206
207         dz->dz_order = z;
208         dz->dz_mask = dnet_make_mask(z);
209
210         for(i = z + 1; i <= 16; i++)
211                 if (table->dh_zones[i])
212                         break;
213
214         write_lock_bh(&dn_fib_tables_lock);
215         if (i>16) {
216                 dz->dz_next = table->dh_zone_list;
217                 table->dh_zone_list = dz;
218         } else {
219                 dz->dz_next = table->dh_zones[i]->dz_next;
220                 table->dh_zones[i]->dz_next = dz;
221         }
222         table->dh_zones[z] = dz;
223         write_unlock_bh(&dn_fib_tables_lock);
224         return dz;
225 }
226
227
228 static int dn_fib_nh_match(struct rtmsg *r, struct nlmsghdr *nlh, struct nlattr *attrs[], struct dn_fib_info *fi)
229 {
230         struct rtnexthop *nhp;
231         int nhlen;
232
233         if (attrs[RTA_PRIORITY] &&
234             nla_get_u32(attrs[RTA_PRIORITY]) != fi->fib_priority)
235                 return 1;
236
237         if (attrs[RTA_OIF] || attrs[RTA_GATEWAY]) {
238                 if ((!attrs[RTA_OIF] || nla_get_u32(attrs[RTA_OIF]) == fi->fib_nh->nh_oif) &&
239                     (!attrs[RTA_GATEWAY]  || nla_get_le16(attrs[RTA_GATEWAY]) != fi->fib_nh->nh_gw))
240                         return 0;
241                 return 1;
242         }
243
244         if (!attrs[RTA_MULTIPATH])
245                 return 0;
246
247         nhp = nla_data(attrs[RTA_MULTIPATH]);
248         nhlen = nla_len(attrs[RTA_MULTIPATH]);
249
250         for_nexthops(fi) {
251                 int attrlen = nhlen - sizeof(struct rtnexthop);
252                 __le16 gw;
253
254                 if (attrlen < 0 || (nhlen -= nhp->rtnh_len) < 0)
255                         return -EINVAL;
256                 if (nhp->rtnh_ifindex && nhp->rtnh_ifindex != nh->nh_oif)
257                         return 1;
258                 if (attrlen) {
259                         struct nlattr *gw_attr;
260
261                         gw_attr = nla_find((struct nlattr *) (nhp + 1), attrlen, RTA_GATEWAY);
262                         gw = gw_attr ? nla_get_le16(gw_attr) : 0;
263
264                         if (gw && gw != nh->nh_gw)
265                                 return 1;
266                 }
267                 nhp = RTNH_NEXT(nhp);
268         } endfor_nexthops(fi);
269
270         return 0;
271 }
272
273 static inline size_t dn_fib_nlmsg_size(struct dn_fib_info *fi)
274 {
275         size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
276                          + nla_total_size(4) /* RTA_TABLE */
277                          + nla_total_size(2) /* RTA_DST */
278                          + nla_total_size(4) /* RTA_PRIORITY */
279                          + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
280
281         /* space for nested metrics */
282         payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
283
284         if (fi->fib_nhs) {
285                 /* Also handles the special case fib_nhs == 1 */
286
287                 /* each nexthop is packed in an attribute */
288                 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
289
290                 /* may contain a gateway attribute */
291                 nhsize += nla_total_size(4);
292
293                 /* all nexthops are packed in a nested attribute */
294                 payload += nla_total_size(fi->fib_nhs * nhsize);
295         }
296
297         return payload;
298 }
299
300 static int dn_fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
301                         u32 tb_id, u8 type, u8 scope, void *dst, int dst_len,
302                         struct dn_fib_info *fi, unsigned int flags)
303 {
304         struct rtmsg *rtm;
305         struct nlmsghdr *nlh;
306
307         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
308         if (!nlh)
309                 return -EMSGSIZE;
310
311         rtm = nlmsg_data(nlh);
312         rtm->rtm_family = AF_DECnet;
313         rtm->rtm_dst_len = dst_len;
314         rtm->rtm_src_len = 0;
315         rtm->rtm_tos = 0;
316         rtm->rtm_table = tb_id;
317         rtm->rtm_flags = fi->fib_flags;
318         rtm->rtm_scope = scope;
319         rtm->rtm_type  = type;
320         rtm->rtm_protocol = fi->fib_protocol;
321
322         if (nla_put_u32(skb, RTA_TABLE, tb_id) < 0)
323                 goto errout;
324
325         if (rtm->rtm_dst_len &&
326             nla_put(skb, RTA_DST, 2, dst) < 0)
327                 goto errout;
328
329         if (fi->fib_priority &&
330             nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority) < 0)
331                 goto errout;
332
333         if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
334                 goto errout;
335
336         if (fi->fib_nhs == 1) {
337                 if (fi->fib_nh->nh_gw &&
338                     nla_put_le16(skb, RTA_GATEWAY, fi->fib_nh->nh_gw) < 0)
339                         goto errout;
340
341                 if (fi->fib_nh->nh_oif &&
342                     nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif) < 0)
343                         goto errout;
344         }
345
346         if (fi->fib_nhs > 1) {
347                 struct rtnexthop *nhp;
348                 struct nlattr *mp_head;
349
350                 if (!(mp_head = nla_nest_start(skb, RTA_MULTIPATH)))
351                         goto errout;
352
353                 for_nexthops(fi) {
354                         if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp))))
355                                 goto errout;
356
357                         nhp->rtnh_flags = nh->nh_flags & 0xFF;
358                         nhp->rtnh_hops = nh->nh_weight - 1;
359                         nhp->rtnh_ifindex = nh->nh_oif;
360
361                         if (nh->nh_gw &&
362                             nla_put_le16(skb, RTA_GATEWAY, nh->nh_gw) < 0)
363                                 goto errout;
364
365                         nhp->rtnh_len = skb_tail_pointer(skb) - (unsigned char *)nhp;
366                 } endfor_nexthops(fi);
367
368                 nla_nest_end(skb, mp_head);
369         }
370
371         nlmsg_end(skb, nlh);
372         return 0;
373
374 errout:
375         nlmsg_cancel(skb, nlh);
376         return -EMSGSIZE;
377 }
378
379
380 static void dn_rtmsg_fib(int event, struct dn_fib_node *f, int z, u32 tb_id,
381                         struct nlmsghdr *nlh, struct netlink_skb_parms *req)
382 {
383         struct sk_buff *skb;
384         u32 portid = req ? req->portid : 0;
385         int err = -ENOBUFS;
386
387         skb = nlmsg_new(dn_fib_nlmsg_size(DN_FIB_INFO(f)), GFP_KERNEL);
388         if (skb == NULL)
389                 goto errout;
390
391         err = dn_fib_dump_info(skb, portid, nlh->nlmsg_seq, event, tb_id,
392                                f->fn_type, f->fn_scope, &f->fn_key, z,
393                                DN_FIB_INFO(f), 0);
394         if (err < 0) {
395                 /* -EMSGSIZE implies BUG in dn_fib_nlmsg_size() */
396                 WARN_ON(err == -EMSGSIZE);
397                 kfree_skb(skb);
398                 goto errout;
399         }
400         rtnl_notify(skb, &init_net, portid, RTNLGRP_DECnet_ROUTE, nlh, GFP_KERNEL);
401         return;
402 errout:
403         if (err < 0)
404                 rtnl_set_sk_err(&init_net, RTNLGRP_DECnet_ROUTE, err);
405 }
406
407 static __inline__ int dn_hash_dump_bucket(struct sk_buff *skb,
408                                 struct netlink_callback *cb,
409                                 struct dn_fib_table *tb,
410                                 struct dn_zone *dz,
411                                 struct dn_fib_node *f)
412 {
413         int i, s_i;
414
415         s_i = cb->args[4];
416         for(i = 0; f; i++, f = f->fn_next) {
417                 if (i < s_i)
418                         continue;
419                 if (f->fn_state & DN_S_ZOMBIE)
420                         continue;
421                 if (dn_fib_dump_info(skb, NETLINK_CB(cb->skb).portid,
422                                 cb->nlh->nlmsg_seq,
423                                 RTM_NEWROUTE,
424                                 tb->n,
425                                 (f->fn_state & DN_S_ZOMBIE) ? 0 : f->fn_type,
426                                 f->fn_scope, &f->fn_key, dz->dz_order,
427                                 f->fn_info, NLM_F_MULTI) < 0) {
428                         cb->args[4] = i;
429                         return -1;
430                 }
431         }
432         cb->args[4] = i;
433         return skb->len;
434 }
435
436 static __inline__ int dn_hash_dump_zone(struct sk_buff *skb,
437                                 struct netlink_callback *cb,
438                                 struct dn_fib_table *tb,
439                                 struct dn_zone *dz)
440 {
441         int h, s_h;
442
443         s_h = cb->args[3];
444         for(h = 0; h < dz->dz_divisor; h++) {
445                 if (h < s_h)
446                         continue;
447                 if (h > s_h)
448                         memset(&cb->args[4], 0, sizeof(cb->args) - 4*sizeof(cb->args[0]));
449                 if (dz->dz_hash == NULL || dz->dz_hash[h] == NULL)
450                         continue;
451                 if (dn_hash_dump_bucket(skb, cb, tb, dz, dz->dz_hash[h]) < 0) {
452                         cb->args[3] = h;
453                         return -1;
454                 }
455         }
456         cb->args[3] = h;
457         return skb->len;
458 }
459
460 static int dn_fib_table_dump(struct dn_fib_table *tb, struct sk_buff *skb,
461                                 struct netlink_callback *cb)
462 {
463         int m, s_m;
464         struct dn_zone *dz;
465         struct dn_hash *table = (struct dn_hash *)tb->data;
466
467         s_m = cb->args[2];
468         read_lock(&dn_fib_tables_lock);
469         for(dz = table->dh_zone_list, m = 0; dz; dz = dz->dz_next, m++) {
470                 if (m < s_m)
471                         continue;
472                 if (m > s_m)
473                         memset(&cb->args[3], 0, sizeof(cb->args) - 3*sizeof(cb->args[0]));
474
475                 if (dn_hash_dump_zone(skb, cb, tb, dz) < 0) {
476                         cb->args[2] = m;
477                         read_unlock(&dn_fib_tables_lock);
478                         return -1;
479                 }
480         }
481         read_unlock(&dn_fib_tables_lock);
482         cb->args[2] = m;
483
484         return skb->len;
485 }
486
487 int dn_fib_dump(struct sk_buff *skb, struct netlink_callback *cb)
488 {
489         struct net *net = sock_net(skb->sk);
490         unsigned int h, s_h;
491         unsigned int e = 0, s_e;
492         struct dn_fib_table *tb;
493         int dumped = 0;
494
495         if (!net_eq(net, &init_net))
496                 return 0;
497
498         if (nlmsg_len(cb->nlh) >= sizeof(struct rtmsg) &&
499                 ((struct rtmsg *)nlmsg_data(cb->nlh))->rtm_flags&RTM_F_CLONED)
500                         return dn_cache_dump(skb, cb);
501
502         s_h = cb->args[0];
503         s_e = cb->args[1];
504
505         for (h = s_h; h < DN_FIB_TABLE_HASHSZ; h++, s_h = 0) {
506                 e = 0;
507                 hlist_for_each_entry(tb, &dn_fib_table_hash[h], hlist) {
508                         if (e < s_e)
509                                 goto next;
510                         if (dumped)
511                                 memset(&cb->args[2], 0, sizeof(cb->args) -
512                                                  2 * sizeof(cb->args[0]));
513                         if (tb->dump(tb, skb, cb) < 0)
514                                 goto out;
515                         dumped = 1;
516 next:
517                         e++;
518                 }
519         }
520 out:
521         cb->args[1] = e;
522         cb->args[0] = h;
523
524         return skb->len;
525 }
526
527 static int dn_fib_table_insert(struct dn_fib_table *tb, struct rtmsg *r, struct nlattr *attrs[],
528                                struct nlmsghdr *n, struct netlink_skb_parms *req)
529 {
530         struct dn_hash *table = (struct dn_hash *)tb->data;
531         struct dn_fib_node *new_f, *f, **fp, **del_fp;
532         struct dn_zone *dz;
533         struct dn_fib_info *fi;
534         int z = r->rtm_dst_len;
535         int type = r->rtm_type;
536         dn_fib_key_t key;
537         int err;
538
539         if (z > 16)
540                 return -EINVAL;
541
542         dz = table->dh_zones[z];
543         if (!dz && !(dz = dn_new_zone(table, z)))
544                 return -ENOBUFS;
545
546         dz_key_0(key);
547         if (attrs[RTA_DST]) {
548                 __le16 dst = nla_get_le16(attrs[RTA_DST]);
549                 if (dst & ~DZ_MASK(dz))
550                         return -EINVAL;
551                 key = dz_key(dst, dz);
552         }
553
554         if ((fi = dn_fib_create_info(r, attrs, n, &err)) == NULL)
555                 return err;
556
557         if (dz->dz_nent > (dz->dz_divisor << 2) &&
558                         dz->dz_divisor > DN_MAX_DIVISOR &&
559                         (z==16 || (1<<z) > dz->dz_divisor))
560                 dn_rehash_zone(dz);
561
562         fp = dn_chain_p(key, dz);
563
564         DN_FIB_SCAN(f, fp) {
565                 if (dn_key_leq(key, f->fn_key))
566                         break;
567         }
568
569         del_fp = NULL;
570
571         if (f && (f->fn_state & DN_S_ZOMBIE) &&
572                         dn_key_eq(f->fn_key, key)) {
573                 del_fp = fp;
574                 fp = &f->fn_next;
575                 f = *fp;
576                 goto create;
577         }
578
579         DN_FIB_SCAN_KEY(f, fp, key) {
580                 if (fi->fib_priority <= DN_FIB_INFO(f)->fib_priority)
581                         break;
582         }
583
584         if (f && dn_key_eq(f->fn_key, key) &&
585                         fi->fib_priority == DN_FIB_INFO(f)->fib_priority) {
586                 struct dn_fib_node **ins_fp;
587
588                 err = -EEXIST;
589                 if (n->nlmsg_flags & NLM_F_EXCL)
590                         goto out;
591
592                 if (n->nlmsg_flags & NLM_F_REPLACE) {
593                         del_fp = fp;
594                         fp = &f->fn_next;
595                         f = *fp;
596                         goto replace;
597                 }
598
599                 ins_fp = fp;
600                 err = -EEXIST;
601
602                 DN_FIB_SCAN_KEY(f, fp, key) {
603                         if (fi->fib_priority != DN_FIB_INFO(f)->fib_priority)
604                                 break;
605                         if (f->fn_type == type &&
606                             f->fn_scope == r->rtm_scope &&
607                             DN_FIB_INFO(f) == fi)
608                                 goto out;
609                 }
610
611                 if (!(n->nlmsg_flags & NLM_F_APPEND)) {
612                         fp = ins_fp;
613                         f = *fp;
614                 }
615         }
616
617 create:
618         err = -ENOENT;
619         if (!(n->nlmsg_flags & NLM_F_CREATE))
620                 goto out;
621
622 replace:
623         err = -ENOBUFS;
624         new_f = kmem_cache_zalloc(dn_hash_kmem, GFP_KERNEL);
625         if (new_f == NULL)
626                 goto out;
627
628         new_f->fn_key = key;
629         new_f->fn_type = type;
630         new_f->fn_scope = r->rtm_scope;
631         DN_FIB_INFO(new_f) = fi;
632
633         new_f->fn_next = f;
634         write_lock_bh(&dn_fib_tables_lock);
635         *fp = new_f;
636         write_unlock_bh(&dn_fib_tables_lock);
637         dz->dz_nent++;
638
639         if (del_fp) {
640                 f = *del_fp;
641                 write_lock_bh(&dn_fib_tables_lock);
642                 *del_fp = f->fn_next;
643                 write_unlock_bh(&dn_fib_tables_lock);
644
645                 if (!(f->fn_state & DN_S_ZOMBIE))
646                         dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
647                 if (f->fn_state & DN_S_ACCESSED)
648                         dn_rt_cache_flush(-1);
649                 dn_free_node(f);
650                 dz->dz_nent--;
651         } else {
652                 dn_rt_cache_flush(-1);
653         }
654
655         dn_rtmsg_fib(RTM_NEWROUTE, new_f, z, tb->n, n, req);
656
657         return 0;
658 out:
659         dn_fib_release_info(fi);
660         return err;
661 }
662
663
664 static int dn_fib_table_delete(struct dn_fib_table *tb, struct rtmsg *r, struct nlattr *attrs[],
665                                struct nlmsghdr *n, struct netlink_skb_parms *req)
666 {
667         struct dn_hash *table = (struct dn_hash*)tb->data;
668         struct dn_fib_node **fp, **del_fp, *f;
669         int z = r->rtm_dst_len;
670         struct dn_zone *dz;
671         dn_fib_key_t key;
672         int matched;
673
674
675         if (z > 16)
676                 return -EINVAL;
677
678         if ((dz = table->dh_zones[z]) == NULL)
679                 return -ESRCH;
680
681         dz_key_0(key);
682         if (attrs[RTA_DST]) {
683                 __le16 dst = nla_get_le16(attrs[RTA_DST]);
684                 if (dst & ~DZ_MASK(dz))
685                         return -EINVAL;
686                 key = dz_key(dst, dz);
687         }
688
689         fp = dn_chain_p(key, dz);
690
691         DN_FIB_SCAN(f, fp) {
692                 if (dn_key_eq(f->fn_key, key))
693                         break;
694                 if (dn_key_leq(key, f->fn_key))
695                         return -ESRCH;
696         }
697
698         matched = 0;
699         del_fp = NULL;
700         DN_FIB_SCAN_KEY(f, fp, key) {
701                 struct dn_fib_info *fi = DN_FIB_INFO(f);
702
703                 if (f->fn_state & DN_S_ZOMBIE)
704                         return -ESRCH;
705
706                 matched++;
707
708                 if (del_fp == NULL &&
709                                 (!r->rtm_type || f->fn_type == r->rtm_type) &&
710                                 (r->rtm_scope == RT_SCOPE_NOWHERE || f->fn_scope == r->rtm_scope) &&
711                                 (!r->rtm_protocol ||
712                                         fi->fib_protocol == r->rtm_protocol) &&
713                                 dn_fib_nh_match(r, n, attrs, fi) == 0)
714                         del_fp = fp;
715         }
716
717         if (del_fp) {
718                 f = *del_fp;
719                 dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
720
721                 if (matched != 1) {
722                         write_lock_bh(&dn_fib_tables_lock);
723                         *del_fp = f->fn_next;
724                         write_unlock_bh(&dn_fib_tables_lock);
725
726                         if (f->fn_state & DN_S_ACCESSED)
727                                 dn_rt_cache_flush(-1);
728                         dn_free_node(f);
729                         dz->dz_nent--;
730                 } else {
731                         f->fn_state |= DN_S_ZOMBIE;
732                         if (f->fn_state & DN_S_ACCESSED) {
733                                 f->fn_state &= ~DN_S_ACCESSED;
734                                 dn_rt_cache_flush(-1);
735                         }
736                         if (++dn_fib_hash_zombies > 128)
737                                 dn_fib_flush();
738                 }
739
740                 return 0;
741         }
742
743         return -ESRCH;
744 }
745
746 static inline int dn_flush_list(struct dn_fib_node **fp, int z, struct dn_hash *table)
747 {
748         int found = 0;
749         struct dn_fib_node *f;
750
751         while((f = *fp) != NULL) {
752                 struct dn_fib_info *fi = DN_FIB_INFO(f);
753
754                 if (fi && ((f->fn_state & DN_S_ZOMBIE) || (fi->fib_flags & RTNH_F_DEAD))) {
755                         write_lock_bh(&dn_fib_tables_lock);
756                         *fp = f->fn_next;
757                         write_unlock_bh(&dn_fib_tables_lock);
758
759                         dn_free_node(f);
760                         found++;
761                         continue;
762                 }
763                 fp = &f->fn_next;
764         }
765
766         return found;
767 }
768
769 static int dn_fib_table_flush(struct dn_fib_table *tb)
770 {
771         struct dn_hash *table = (struct dn_hash *)tb->data;
772         struct dn_zone *dz;
773         int found = 0;
774
775         dn_fib_hash_zombies = 0;
776         for(dz = table->dh_zone_list; dz; dz = dz->dz_next) {
777                 int i;
778                 int tmp = 0;
779                 for(i = dz->dz_divisor-1; i >= 0; i--)
780                         tmp += dn_flush_list(&dz->dz_hash[i], dz->dz_order, table);
781                 dz->dz_nent -= tmp;
782                 found += tmp;
783         }
784
785         return found;
786 }
787
788 static int dn_fib_table_lookup(struct dn_fib_table *tb, const struct flowidn *flp, struct dn_fib_res *res)
789 {
790         int err;
791         struct dn_zone *dz;
792         struct dn_hash *t = (struct dn_hash *)tb->data;
793
794         read_lock(&dn_fib_tables_lock);
795         for(dz = t->dh_zone_list; dz; dz = dz->dz_next) {
796                 struct dn_fib_node *f;
797                 dn_fib_key_t k = dz_key(flp->daddr, dz);
798
799                 for(f = dz_chain(k, dz); f; f = f->fn_next) {
800                         if (!dn_key_eq(k, f->fn_key)) {
801                                 if (dn_key_leq(k, f->fn_key))
802                                         break;
803                                 else
804                                         continue;
805                         }
806
807                         f->fn_state |= DN_S_ACCESSED;
808
809                         if (f->fn_state&DN_S_ZOMBIE)
810                                 continue;
811
812                         if (f->fn_scope < flp->flowidn_scope)
813                                 continue;
814
815                         err = dn_fib_semantic_match(f->fn_type, DN_FIB_INFO(f), flp, res);
816
817                         if (err == 0) {
818                                 res->type = f->fn_type;
819                                 res->scope = f->fn_scope;
820                                 res->prefixlen = dz->dz_order;
821                                 goto out;
822                         }
823                         if (err < 0)
824                                 goto out;
825                 }
826         }
827         err = 1;
828 out:
829         read_unlock(&dn_fib_tables_lock);
830         return err;
831 }
832
833
834 struct dn_fib_table *dn_fib_get_table(u32 n, int create)
835 {
836         struct dn_fib_table *t;
837         unsigned int h;
838
839         if (n < RT_TABLE_MIN)
840                 return NULL;
841
842         if (n > RT_TABLE_MAX)
843                 return NULL;
844
845         h = n & (DN_FIB_TABLE_HASHSZ - 1);
846         rcu_read_lock();
847         hlist_for_each_entry_rcu(t, &dn_fib_table_hash[h], hlist) {
848                 if (t->n == n) {
849                         rcu_read_unlock();
850                         return t;
851                 }
852         }
853         rcu_read_unlock();
854
855         if (!create)
856                 return NULL;
857
858         if (in_interrupt()) {
859                 net_dbg_ratelimited("DECnet: BUG! Attempt to create routing table from interrupt\n");
860                 return NULL;
861         }
862
863         t = kzalloc(sizeof(struct dn_fib_table) + sizeof(struct dn_hash),
864                     GFP_KERNEL);
865         if (t == NULL)
866                 return NULL;
867
868         t->n = n;
869         t->insert = dn_fib_table_insert;
870         t->delete = dn_fib_table_delete;
871         t->lookup = dn_fib_table_lookup;
872         t->flush  = dn_fib_table_flush;
873         t->dump = dn_fib_table_dump;
874         hlist_add_head_rcu(&t->hlist, &dn_fib_table_hash[h]);
875
876         return t;
877 }
878
879 struct dn_fib_table *dn_fib_empty_table(void)
880 {
881         u32 id;
882
883         for(id = RT_TABLE_MIN; id <= RT_TABLE_MAX; id++)
884                 if (dn_fib_get_table(id, 0) == NULL)
885                         return dn_fib_get_table(id, 1);
886         return NULL;
887 }
888
889 void dn_fib_flush(void)
890 {
891         int flushed = 0;
892         struct dn_fib_table *tb;
893         unsigned int h;
894
895         for (h = 0; h < DN_FIB_TABLE_HASHSZ; h++) {
896                 hlist_for_each_entry(tb, &dn_fib_table_hash[h], hlist)
897                         flushed += tb->flush(tb);
898         }
899
900         if (flushed)
901                 dn_rt_cache_flush(-1);
902 }
903
904 void __init dn_fib_table_init(void)
905 {
906         dn_hash_kmem = kmem_cache_create("dn_fib_info_cache",
907                                         sizeof(struct dn_fib_info),
908                                         0, SLAB_HWCACHE_ALIGN,
909                                         NULL);
910 }
911
912 void __exit dn_fib_table_cleanup(void)
913 {
914         struct dn_fib_table *t;
915         struct hlist_node *next;
916         unsigned int h;
917
918         write_lock(&dn_fib_tables_lock);
919         for (h = 0; h < DN_FIB_TABLE_HASHSZ; h++) {
920                 hlist_for_each_entry_safe(t, next, &dn_fib_table_hash[h],
921                                           hlist) {
922                         hlist_del(&t->hlist);
923                         kfree(t);
924                 }
925         }
926         write_unlock(&dn_fib_tables_lock);
927 }