]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/decnet/dn_table.c
via-rhine: Assign random MAC address if necessary
[mirror_ubuntu-jammy-kernel.git] / net / decnet / dn_table.c
CommitLineData
1da177e4
LT
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 */
1da177e4
LT
15#include <linux/string.h>
16#include <linux/net.h>
17#include <linux/socket.h>
5a0e3ad6 18#include <linux/slab.h>
1da177e4
LT
19#include <linux/sockios.h>
20#include <linux/init.h>
21#include <linux/skbuff.h>
22#include <linux/netlink.h>
23#include <linux/rtnetlink.h>
24#include <linux/proc_fs.h>
25#include <linux/netdevice.h>
26#include <linux/timer.h>
27#include <linux/spinlock.h>
28#include <asm/atomic.h>
29#include <asm/uaccess.h>
30#include <linux/route.h> /* RTF_xxx */
31#include <net/neighbour.h>
dc5fc579 32#include <net/netlink.h>
1da177e4
LT
33#include <net/dst.h>
34#include <net/flow.h>
a8731cbf 35#include <net/fib_rules.h>
1da177e4
LT
36#include <net/dn.h>
37#include <net/dn_route.h>
38#include <net/dn_fib.h>
39#include <net/dn_neigh.h>
40#include <net/dn_dev.h>
41
42struct dn_zone
43{
44 struct dn_zone *dz_next;
45 struct dn_fib_node **dz_hash;
46 int dz_nent;
47 int dz_divisor;
48 u32 dz_hashmask;
49#define DZ_HASHMASK(dz) ((dz)->dz_hashmask)
50 int dz_order;
c4ea94ab 51 __le16 dz_mask;
1da177e4
LT
52#define DZ_MASK(dz) ((dz)->dz_mask)
53};
54
55struct dn_hash
56{
57 struct dn_zone *dh_zones[17];
58 struct dn_zone *dh_zone_list;
59};
60
61#define dz_key_0(key) ((key).datum = 0)
1da177e4
LT
62
63#define for_nexthops(fi) { int nhsel; const struct dn_fib_nh *nh;\
429eb0fa 64 for(nhsel = 0, nh = (fi)->fib_nh; nhsel < (fi)->fib_nhs; nh++, nhsel++)
1da177e4
LT
65
66#define endfor_nexthops(fi) }
67
68#define DN_MAX_DIVISOR 1024
69#define DN_S_ZOMBIE 1
70#define DN_S_ACCESSED 2
71
72#define DN_FIB_SCAN(f, fp) \
73for( ; ((f) = *(fp)) != NULL; (fp) = &(f)->fn_next)
74
75#define DN_FIB_SCAN_KEY(f, fp, key) \
76for( ; ((f) = *(fp)) != NULL && dn_key_eq((f)->fn_key, (key)); (fp) = &(f)->fn_next)
77
78#define RT_TABLE_MIN 1
abcab268
PM
79#define DN_FIB_TABLE_HASHSZ 256
80static struct hlist_head dn_fib_table_hash[DN_FIB_TABLE_HASHSZ];
1da177e4 81static DEFINE_RWLOCK(dn_fib_tables_lock);
1da177e4 82
e18b890b 83static struct kmem_cache *dn_hash_kmem __read_mostly;
1da177e4
LT
84static int dn_fib_hash_zombies;
85
86static inline dn_fib_idx_t dn_hash(dn_fib_key_t key, struct dn_zone *dz)
87{
c4106aa8 88 u16 h = le16_to_cpu(key.datum)>>(16 - dz->dz_order);
1da177e4
LT
89 h ^= (h >> 10);
90 h ^= (h >> 6);
91 h &= DZ_HASHMASK(dz);
92 return *(dn_fib_idx_t *)&h;
93}
94
c4ea94ab 95static inline dn_fib_key_t dz_key(__le16 dst, struct dn_zone *dz)
1da177e4
LT
96{
97 dn_fib_key_t k;
98 k.datum = dst & DZ_MASK(dz);
99 return k;
100}
101
102static inline struct dn_fib_node **dn_chain_p(dn_fib_key_t key, struct dn_zone *dz)
103{
104 return &dz->dz_hash[dn_hash(key, dz).datum];
105}
106
107static inline struct dn_fib_node *dz_chain(dn_fib_key_t key, struct dn_zone *dz)
108{
109 return dz->dz_hash[dn_hash(key, dz).datum];
110}
111
112static inline int dn_key_eq(dn_fib_key_t a, dn_fib_key_t b)
113{
114 return a.datum == b.datum;
115}
116
117static inline int dn_key_leq(dn_fib_key_t a, dn_fib_key_t b)
118{
119 return a.datum <= b.datum;
120}
121
122static inline void dn_rebuild_zone(struct dn_zone *dz,
123 struct dn_fib_node **old_ht,
124 int old_divisor)
125{
126 int i;
9bf9055e 127 struct dn_fib_node *f, **fp;
1da177e4
LT
128
129 for(i = 0; i < old_divisor; i++) {
130 for(f = old_ht[i]; f; f = f->fn_next) {
1da177e4
LT
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
141static 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
0da974f4 162 ht = kcalloc(new_divisor, sizeof(struct dn_fib_node*), GFP_KERNEL);
1da177e4
LT
163 if (ht == NULL)
164 return;
165
1da177e4
LT
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
176static 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
183static struct dn_zone *dn_new_zone(struct dn_hash *table, int z)
184{
185 int i;
0da974f4 186 struct dn_zone *dz = kzalloc(sizeof(struct dn_zone), GFP_KERNEL);
1da177e4
LT
187 if (!dz)
188 return NULL;
189
1da177e4
LT
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
0da974f4 198 dz->dz_hash = kcalloc(dz->dz_divisor, sizeof(struct dn_fib_node *), GFP_KERNEL);
1da177e4
LT
199 if (!dz->dz_hash) {
200 kfree(dz);
201 return NULL;
202 }
203
1da177e4
LT
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
225static 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);
c4ea94ab 248 __le16 gw;
1da177e4
LT
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
339bf98f
TG
266static inline size_t dn_fib_nlmsg_size(struct dn_fib_info *fi)
267{
75356f27 268 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
339bf98f
TG
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
1da177e4 292static int dn_fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
429eb0fa
YH
293 u32 tb_id, u8 type, u8 scope, void *dst, int dst_len,
294 struct dn_fib_info *fi, unsigned int flags)
1da177e4 295{
429eb0fa
YH
296 struct rtmsg *rtm;
297 struct nlmsghdr *nlh;
27a884dc 298 unsigned char *b = skb_tail_pointer(skb);
429eb0fa
YH
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;
9e762a4a 307 RTA_PUT_U32(skb, RTA_TABLE, tb_id);
429eb0fa
YH
308 rtm->rtm_flags = fi->fib_flags;
309 rtm->rtm_scope = scope;
1da177e4 310 rtm->rtm_type = type;
429eb0fa
YH
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);
1da177e4
LT
316 if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
317 goto rtattr_failure;
429eb0fa
YH
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);
27a884dc 340 nhp->rtnh_len = skb_tail_pointer(skb) - (unsigned char *)nhp;
429eb0fa
YH
341 } endfor_nexthops(fi);
342 mp_head->rta_type = RTA_MULTIPATH;
27a884dc 343 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
429eb0fa
YH
344 }
345
27a884dc 346 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
429eb0fa 347 return skb->len;
1da177e4
LT
348
349
350nlmsg_failure:
351rtattr_failure:
dc5fc579 352 nlmsg_trim(skb, b);
429eb0fa 353 return -EMSGSIZE;
1da177e4
LT
354}
355
356
2dfe55b4 357static void dn_rtmsg_fib(int event, struct dn_fib_node *f, int z, u32 tb_id,
429eb0fa 358 struct nlmsghdr *nlh, struct netlink_skb_parms *req)
1da177e4 359{
429eb0fa
YH
360 struct sk_buff *skb;
361 u32 pid = req ? req->pid : 0;
dc738dd8 362 int err = -ENOBUFS;
1da177e4 363
429eb0fa
YH
364 skb = nlmsg_new(dn_fib_nlmsg_size(DN_FIB_INFO(f)), GFP_KERNEL);
365 if (skb == NULL)
dc738dd8 366 goto errout;
1da177e4 367
429eb0fa 368 err = dn_fib_dump_info(skb, pid, nlh->nlmsg_seq, event, tb_id,
dc738dd8
TG
369 f->fn_type, f->fn_scope, &f->fn_key, z,
370 DN_FIB_INFO(f), 0);
26932566
PM
371 if (err < 0) {
372 /* -EMSGSIZE implies BUG in dn_fib_nlmsg_size() */
373 WARN_ON(err == -EMSGSIZE);
374 kfree_skb(skb);
375 goto errout;
376 }
1ce85fe4
PNA
377 rtnl_notify(skb, &init_net, pid, RTNLGRP_DECnet_ROUTE, nlh, GFP_KERNEL);
378 return;
dc738dd8
TG
379errout:
380 if (err < 0)
97c53cac 381 rtnl_set_sk_err(&init_net, RTNLGRP_DECnet_ROUTE, err);
1da177e4
LT
382}
383
429eb0fa 384static __inline__ int dn_hash_dump_bucket(struct sk_buff *skb,
1da177e4
LT
385 struct netlink_callback *cb,
386 struct dn_fib_table *tb,
387 struct dn_zone *dz,
388 struct dn_fib_node *f)
389{
390 int i, s_i;
391
abcab268 392 s_i = cb->args[4];
1da177e4
LT
393 for(i = 0; f; i++, f = f->fn_next) {
394 if (i < s_i)
395 continue;
396 if (f->fn_state & DN_S_ZOMBIE)
397 continue;
429eb0fa 398 if (dn_fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
1da177e4
LT
399 cb->nlh->nlmsg_seq,
400 RTM_NEWROUTE,
429eb0fa 401 tb->n,
1da177e4 402 (f->fn_state & DN_S_ZOMBIE) ? 0 : f->fn_type,
429eb0fa 403 f->fn_scope, &f->fn_key, dz->dz_order,
b6544c0b 404 f->fn_info, NLM_F_MULTI) < 0) {
abcab268 405 cb->args[4] = i;
1da177e4
LT
406 return -1;
407 }
408 }
abcab268 409 cb->args[4] = i;
1da177e4
LT
410 return skb->len;
411}
412
429eb0fa 413static __inline__ int dn_hash_dump_zone(struct sk_buff *skb,
1da177e4
LT
414 struct netlink_callback *cb,
415 struct dn_fib_table *tb,
416 struct dn_zone *dz)
417{
418 int h, s_h;
419
abcab268 420 s_h = cb->args[3];
1da177e4
LT
421 for(h = 0; h < dz->dz_divisor; h++) {
422 if (h < s_h)
423 continue;
424 if (h > s_h)
abcab268 425 memset(&cb->args[4], 0, sizeof(cb->args) - 4*sizeof(cb->args[0]));
1da177e4
LT
426 if (dz->dz_hash == NULL || dz->dz_hash[h] == NULL)
427 continue;
428 if (dn_hash_dump_bucket(skb, cb, tb, dz, dz->dz_hash[h]) < 0) {
abcab268 429 cb->args[3] = h;
1da177e4
LT
430 return -1;
431 }
432 }
abcab268 433 cb->args[3] = h;
1da177e4
LT
434 return skb->len;
435}
436
429eb0fa
YH
437static int dn_fib_table_dump(struct dn_fib_table *tb, struct sk_buff *skb,
438 struct netlink_callback *cb)
1da177e4 439{
429eb0fa 440 int m, s_m;
1da177e4
LT
441 struct dn_zone *dz;
442 struct dn_hash *table = (struct dn_hash *)tb->data;
443
abcab268 444 s_m = cb->args[2];
1da177e4
LT
445 read_lock(&dn_fib_tables_lock);
446 for(dz = table->dh_zone_list, m = 0; dz; dz = dz->dz_next, m++) {
447 if (m < s_m)
448 continue;
449 if (m > s_m)
abcab268 450 memset(&cb->args[3], 0, sizeof(cb->args) - 3*sizeof(cb->args[0]));
1da177e4
LT
451
452 if (dn_hash_dump_zone(skb, cb, tb, dz) < 0) {
abcab268 453 cb->args[2] = m;
1da177e4
LT
454 read_unlock(&dn_fib_tables_lock);
455 return -1;
456 }
457 }
458 read_unlock(&dn_fib_tables_lock);
abcab268 459 cb->args[2] = m;
1da177e4 460
429eb0fa 461 return skb->len;
1da177e4
LT
462}
463
abcab268
PM
464int dn_fib_dump(struct sk_buff *skb, struct netlink_callback *cb)
465{
3b1e0a65 466 struct net *net = sock_net(skb->sk);
abcab268
PM
467 unsigned int h, s_h;
468 unsigned int e = 0, s_e;
469 struct dn_fib_table *tb;
470 struct hlist_node *node;
471 int dumped = 0;
472
09ad9bc7 473 if (!net_eq(net, &init_net))
b854272b
DL
474 return 0;
475
abcab268
PM
476 if (NLMSG_PAYLOAD(cb->nlh, 0) >= sizeof(struct rtmsg) &&
477 ((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED)
478 return dn_cache_dump(skb, cb);
479
480 s_h = cb->args[0];
481 s_e = cb->args[1];
482
483 for (h = s_h; h < DN_FIB_TABLE_HASHSZ; h++, s_h = 0) {
484 e = 0;
485 hlist_for_each_entry(tb, node, &dn_fib_table_hash[h], hlist) {
486 if (e < s_e)
487 goto next;
488 if (dumped)
489 memset(&cb->args[2], 0, sizeof(cb->args) -
429eb0fa 490 2 * sizeof(cb->args[0]));
abcab268
PM
491 if (tb->dump(tb, skb, cb) < 0)
492 goto out;
493 dumped = 1;
494next:
495 e++;
496 }
497 }
498out:
499 cb->args[1] = e;
500 cb->args[0] = h;
501
502 return skb->len;
503}
504
1da177e4
LT
505static 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)
506{
507 struct dn_hash *table = (struct dn_hash *)tb->data;
508 struct dn_fib_node *new_f, *f, **fp, **del_fp;
509 struct dn_zone *dz;
510 struct dn_fib_info *fi;
429eb0fa 511 int z = r->rtm_dst_len;
1da177e4
LT
512 int type = r->rtm_type;
513 dn_fib_key_t key;
429eb0fa 514 int err;
1da177e4 515
429eb0fa
YH
516 if (z > 16)
517 return -EINVAL;
1da177e4
LT
518
519 dz = table->dh_zones[z];
520 if (!dz && !(dz = dn_new_zone(table, z)))
521 return -ENOBUFS;
522
523 dz_key_0(key);
524 if (rta->rta_dst) {
c4ea94ab 525 __le16 dst;
1da177e4
LT
526 memcpy(&dst, rta->rta_dst, 2);
527 if (dst & ~DZ_MASK(dz))
528 return -EINVAL;
529 key = dz_key(dst, dz);
530 }
531
429eb0fa
YH
532 if ((fi = dn_fib_create_info(r, rta, n, &err)) == NULL)
533 return err;
1da177e4
LT
534
535 if (dz->dz_nent > (dz->dz_divisor << 2) &&
536 dz->dz_divisor > DN_MAX_DIVISOR &&
537 (z==16 || (1<<z) > dz->dz_divisor))
538 dn_rehash_zone(dz);
539
540 fp = dn_chain_p(key, dz);
541
542 DN_FIB_SCAN(f, fp) {
543 if (dn_key_leq(key, f->fn_key))
544 break;
545 }
546
547 del_fp = NULL;
548
549 if (f && (f->fn_state & DN_S_ZOMBIE) &&
550 dn_key_eq(f->fn_key, key)) {
551 del_fp = fp;
552 fp = &f->fn_next;
553 f = *fp;
554 goto create;
555 }
556
557 DN_FIB_SCAN_KEY(f, fp, key) {
558 if (fi->fib_priority <= DN_FIB_INFO(f)->fib_priority)
559 break;
560 }
561
562 if (f && dn_key_eq(f->fn_key, key) &&
563 fi->fib_priority == DN_FIB_INFO(f)->fib_priority) {
564 struct dn_fib_node **ins_fp;
565
566 err = -EEXIST;
567 if (n->nlmsg_flags & NLM_F_EXCL)
568 goto out;
569
570 if (n->nlmsg_flags & NLM_F_REPLACE) {
571 del_fp = fp;
572 fp = &f->fn_next;
573 f = *fp;
574 goto replace;
575 }
576
577 ins_fp = fp;
578 err = -EEXIST;
579
580 DN_FIB_SCAN_KEY(f, fp, key) {
581 if (fi->fib_priority != DN_FIB_INFO(f)->fib_priority)
582 break;
f64f9e71
JP
583 if (f->fn_type == type &&
584 f->fn_scope == r->rtm_scope &&
585 DN_FIB_INFO(f) == fi)
1da177e4
LT
586 goto out;
587 }
588
589 if (!(n->nlmsg_flags & NLM_F_APPEND)) {
590 fp = ins_fp;
591 f = *fp;
592 }
593 }
594
595create:
596 err = -ENOENT;
597 if (!(n->nlmsg_flags & NLM_F_CREATE))
598 goto out;
599
600replace:
601 err = -ENOBUFS;
c3762229 602 new_f = kmem_cache_zalloc(dn_hash_kmem, GFP_KERNEL);
1da177e4
LT
603 if (new_f == NULL)
604 goto out;
605
1da177e4
LT
606 new_f->fn_key = key;
607 new_f->fn_type = type;
608 new_f->fn_scope = r->rtm_scope;
609 DN_FIB_INFO(new_f) = fi;
610
611 new_f->fn_next = f;
612 write_lock_bh(&dn_fib_tables_lock);
613 *fp = new_f;
614 write_unlock_bh(&dn_fib_tables_lock);
615 dz->dz_nent++;
616
617 if (del_fp) {
618 f = *del_fp;
619 write_lock_bh(&dn_fib_tables_lock);
620 *del_fp = f->fn_next;
621 write_unlock_bh(&dn_fib_tables_lock);
622
623 if (!(f->fn_state & DN_S_ZOMBIE))
624 dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
625 if (f->fn_state & DN_S_ACCESSED)
626 dn_rt_cache_flush(-1);
627 dn_free_node(f);
628 dz->dz_nent--;
629 } else {
630 dn_rt_cache_flush(-1);
631 }
632
429eb0fa 633 dn_rtmsg_fib(RTM_NEWROUTE, new_f, z, tb->n, n, req);
1da177e4 634
429eb0fa 635 return 0;
1da177e4
LT
636out:
637 dn_fib_release_info(fi);
638 return err;
639}
640
641
642static 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)
643{
644 struct dn_hash *table = (struct dn_hash*)tb->data;
645 struct dn_fib_node **fp, **del_fp, *f;
429eb0fa 646 int z = r->rtm_dst_len;
1da177e4
LT
647 struct dn_zone *dz;
648 dn_fib_key_t key;
649 int matched;
650
651
429eb0fa
YH
652 if (z > 16)
653 return -EINVAL;
1da177e4
LT
654
655 if ((dz = table->dh_zones[z]) == NULL)
656 return -ESRCH;
657
658 dz_key_0(key);
659 if (rta->rta_dst) {
c4ea94ab 660 __le16 dst;
1da177e4
LT
661 memcpy(&dst, rta->rta_dst, 2);
662 if (dst & ~DZ_MASK(dz))
663 return -EINVAL;
664 key = dz_key(dst, dz);
665 }
666
667 fp = dn_chain_p(key, dz);
668
669 DN_FIB_SCAN(f, fp) {
670 if (dn_key_eq(f->fn_key, key))
671 break;
672 if (dn_key_leq(key, f->fn_key))
673 return -ESRCH;
674 }
675
676 matched = 0;
677 del_fp = NULL;
678 DN_FIB_SCAN_KEY(f, fp, key) {
679 struct dn_fib_info *fi = DN_FIB_INFO(f);
680
681 if (f->fn_state & DN_S_ZOMBIE)
682 return -ESRCH;
683
684 matched++;
685
686 if (del_fp == NULL &&
687 (!r->rtm_type || f->fn_type == r->rtm_type) &&
688 (r->rtm_scope == RT_SCOPE_NOWHERE || f->fn_scope == r->rtm_scope) &&
429eb0fa 689 (!r->rtm_protocol ||
1da177e4
LT
690 fi->fib_protocol == r->rtm_protocol) &&
691 dn_fib_nh_match(r, n, rta, fi) == 0)
692 del_fp = fp;
693 }
694
695 if (del_fp) {
696 f = *del_fp;
429eb0fa 697 dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
1da177e4
LT
698
699 if (matched != 1) {
700 write_lock_bh(&dn_fib_tables_lock);
701 *del_fp = f->fn_next;
702 write_unlock_bh(&dn_fib_tables_lock);
703
704 if (f->fn_state & DN_S_ACCESSED)
705 dn_rt_cache_flush(-1);
706 dn_free_node(f);
707 dz->dz_nent--;
708 } else {
709 f->fn_state |= DN_S_ZOMBIE;
710 if (f->fn_state & DN_S_ACCESSED) {
711 f->fn_state &= ~DN_S_ACCESSED;
712 dn_rt_cache_flush(-1);
713 }
714 if (++dn_fib_hash_zombies > 128)
715 dn_fib_flush();
716 }
717
718 return 0;
719 }
720
429eb0fa 721 return -ESRCH;
1da177e4
LT
722}
723
724static inline int dn_flush_list(struct dn_fib_node **fp, int z, struct dn_hash *table)
725{
726 int found = 0;
727 struct dn_fib_node *f;
728
729 while((f = *fp) != NULL) {
730 struct dn_fib_info *fi = DN_FIB_INFO(f);
731
732 if (fi && ((f->fn_state & DN_S_ZOMBIE) || (fi->fib_flags & RTNH_F_DEAD))) {
733 write_lock_bh(&dn_fib_tables_lock);
734 *fp = f->fn_next;
735 write_unlock_bh(&dn_fib_tables_lock);
736
737 dn_free_node(f);
738 found++;
739 continue;
740 }
741 fp = &f->fn_next;
742 }
743
744 return found;
745}
746
747static int dn_fib_table_flush(struct dn_fib_table *tb)
748{
749 struct dn_hash *table = (struct dn_hash *)tb->data;
750 struct dn_zone *dz;
751 int found = 0;
752
753 dn_fib_hash_zombies = 0;
754 for(dz = table->dh_zone_list; dz; dz = dz->dz_next) {
755 int i;
756 int tmp = 0;
757 for(i = dz->dz_divisor-1; i >= 0; i--)
758 tmp += dn_flush_list(&dz->dz_hash[i], dz->dz_order, table);
759 dz->dz_nent -= tmp;
760 found += tmp;
761 }
762
763 return found;
764}
765
bef55aeb 766static int dn_fib_table_lookup(struct dn_fib_table *tb, const struct flowidn *flp, struct dn_fib_res *res)
1da177e4 767{
429eb0fa 768 int err;
1da177e4
LT
769 struct dn_zone *dz;
770 struct dn_hash *t = (struct dn_hash *)tb->data;
771
772 read_lock(&dn_fib_tables_lock);
773 for(dz = t->dh_zone_list; dz; dz = dz->dz_next) {
774 struct dn_fib_node *f;
bef55aeb 775 dn_fib_key_t k = dz_key(flp->daddr, dz);
1da177e4
LT
776
777 for(f = dz_chain(k, dz); f; f = f->fn_next) {
778 if (!dn_key_eq(k, f->fn_key)) {
779 if (dn_key_leq(k, f->fn_key))
780 break;
781 else
782 continue;
783 }
784
785 f->fn_state |= DN_S_ACCESSED;
786
787 if (f->fn_state&DN_S_ZOMBIE)
788 continue;
789
bef55aeb 790 if (f->fn_scope < flp->flowidn_scope)
1da177e4
LT
791 continue;
792
793 err = dn_fib_semantic_match(f->fn_type, DN_FIB_INFO(f), flp, res);
794
795 if (err == 0) {
796 res->type = f->fn_type;
429eb0fa 797 res->scope = f->fn_scope;
1da177e4
LT
798 res->prefixlen = dz->dz_order;
799 goto out;
800 }
801 if (err < 0)
802 goto out;
803 }
804 }
805 err = 1;
806out:
807 read_unlock(&dn_fib_tables_lock);
429eb0fa 808 return err;
1da177e4
LT
809}
810
811
2dfe55b4 812struct dn_fib_table *dn_fib_get_table(u32 n, int create)
1da177e4 813{
429eb0fa 814 struct dn_fib_table *t;
abcab268
PM
815 struct hlist_node *node;
816 unsigned int h;
1da177e4 817
429eb0fa
YH
818 if (n < RT_TABLE_MIN)
819 return NULL;
1da177e4 820
429eb0fa
YH
821 if (n > RT_TABLE_MAX)
822 return NULL;
1da177e4 823
abcab268
PM
824 h = n & (DN_FIB_TABLE_HASHSZ - 1);
825 rcu_read_lock();
826 hlist_for_each_entry_rcu(t, node, &dn_fib_table_hash[h], hlist) {
827 if (t->n == n) {
828 rcu_read_unlock();
829 return t;
830 }
831 }
832 rcu_read_unlock();
1da177e4 833
429eb0fa
YH
834 if (!create)
835 return NULL;
1da177e4 836
429eb0fa
YH
837 if (in_interrupt() && net_ratelimit()) {
838 printk(KERN_DEBUG "DECnet: BUG! Attempt to create routing table from interrupt\n");
839 return NULL;
840 }
1da177e4 841
429eb0fa 842 t = kzalloc(sizeof(struct dn_fib_table) + sizeof(struct dn_hash),
e6b61105 843 GFP_KERNEL);
429eb0fa
YH
844 if (t == NULL)
845 return NULL;
846
847 t->n = n;
848 t->insert = dn_fib_table_insert;
849 t->delete = dn_fib_table_delete;
850 t->lookup = dn_fib_table_lookup;
851 t->flush = dn_fib_table_flush;
852 t->dump = dn_fib_table_dump;
abcab268 853 hlist_add_head_rcu(&t->hlist, &dn_fib_table_hash[h]);
1da177e4 854
429eb0fa 855 return t;
1da177e4
LT
856}
857
1da177e4
LT
858struct dn_fib_table *dn_fib_empty_table(void)
859{
429eb0fa 860 u32 id;
1da177e4 861
429eb0fa 862 for(id = RT_TABLE_MIN; id <= RT_TABLE_MAX; id++)
abcab268 863 if (dn_fib_get_table(id, 0) == NULL)
429eb0fa
YH
864 return dn_fib_get_table(id, 1);
865 return NULL;
1da177e4
LT
866}
867
abcab268
PM
868void dn_fib_flush(void)
869{
429eb0fa
YH
870 int flushed = 0;
871 struct dn_fib_table *tb;
abcab268
PM
872 struct hlist_node *node;
873 unsigned int h;
874
875 for (h = 0; h < DN_FIB_TABLE_HASHSZ; h++) {
876 hlist_for_each_entry(tb, node, &dn_fib_table_hash[h], hlist)
429eb0fa
YH
877 flushed += tb->flush(tb);
878 }
abcab268 879
429eb0fa
YH
880 if (flushed)
881 dn_rt_cache_flush(-1);
abcab268
PM
882}
883
1da177e4
LT
884void __init dn_fib_table_init(void)
885{
886 dn_hash_kmem = kmem_cache_create("dn_fib_info_cache",
887 sizeof(struct dn_fib_info),
888 0, SLAB_HWCACHE_ALIGN,
20c2df83 889 NULL);
1da177e4
LT
890}
891
892void __exit dn_fib_table_cleanup(void)
893{
abcab268
PM
894 struct dn_fib_table *t;
895 struct hlist_node *node, *next;
896 unsigned int h;
1da177e4 897
abcab268
PM
898 write_lock(&dn_fib_tables_lock);
899 for (h = 0; h < DN_FIB_TABLE_HASHSZ; h++) {
900 hlist_for_each_entry_safe(t, node, next, &dn_fib_table_hash[h],
429eb0fa 901 hlist) {
abcab268
PM
902 hlist_del(&t->hlist);
903 kfree(t);
904 }
905 }
906 write_unlock(&dn_fib_tables_lock);
1da177e4 907}