ipv4: Cache output routes in fib_info nexthops.
[deliverable/linux.git] / net / ipv4 / fib_semantics.c
1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * IPv4 Forwarding Information Base: semantics.
7 *
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
16 #include <asm/uaccess.h>
17 #include <linux/bitops.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/jiffies.h>
21 #include <linux/mm.h>
22 #include <linux/string.h>
23 #include <linux/socket.h>
24 #include <linux/sockios.h>
25 #include <linux/errno.h>
26 #include <linux/in.h>
27 #include <linux/inet.h>
28 #include <linux/inetdevice.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
31 #include <linux/proc_fs.h>
32 #include <linux/skbuff.h>
33 #include <linux/init.h>
34 #include <linux/slab.h>
35
36 #include <net/arp.h>
37 #include <net/ip.h>
38 #include <net/protocol.h>
39 #include <net/route.h>
40 #include <net/tcp.h>
41 #include <net/sock.h>
42 #include <net/ip_fib.h>
43 #include <net/netlink.h>
44 #include <net/nexthop.h>
45
46 #include "fib_lookup.h"
47
48 static DEFINE_SPINLOCK(fib_info_lock);
49 static struct hlist_head *fib_info_hash;
50 static struct hlist_head *fib_info_laddrhash;
51 static unsigned int fib_info_hash_size;
52 static unsigned int fib_info_cnt;
53
54 #define DEVINDEX_HASHBITS 8
55 #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
56 static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
57
58 #ifdef CONFIG_IP_ROUTE_MULTIPATH
59
60 static DEFINE_SPINLOCK(fib_multipath_lock);
61
62 #define for_nexthops(fi) { \
63 int nhsel; const struct fib_nh *nh; \
64 for (nhsel = 0, nh = (fi)->fib_nh; \
65 nhsel < (fi)->fib_nhs; \
66 nh++, nhsel++)
67
68 #define change_nexthops(fi) { \
69 int nhsel; struct fib_nh *nexthop_nh; \
70 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
71 nhsel < (fi)->fib_nhs; \
72 nexthop_nh++, nhsel++)
73
74 #else /* CONFIG_IP_ROUTE_MULTIPATH */
75
76 /* Hope, that gcc will optimize it to get rid of dummy loop */
77
78 #define for_nexthops(fi) { \
79 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
80 for (nhsel = 0; nhsel < 1; nhsel++)
81
82 #define change_nexthops(fi) { \
83 int nhsel; \
84 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
85 for (nhsel = 0; nhsel < 1; nhsel++)
86
87 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
88
89 #define endfor_nexthops(fi) }
90
91
92 const struct fib_prop fib_props[RTN_MAX + 1] = {
93 [RTN_UNSPEC] = {
94 .error = 0,
95 .scope = RT_SCOPE_NOWHERE,
96 },
97 [RTN_UNICAST] = {
98 .error = 0,
99 .scope = RT_SCOPE_UNIVERSE,
100 },
101 [RTN_LOCAL] = {
102 .error = 0,
103 .scope = RT_SCOPE_HOST,
104 },
105 [RTN_BROADCAST] = {
106 .error = 0,
107 .scope = RT_SCOPE_LINK,
108 },
109 [RTN_ANYCAST] = {
110 .error = 0,
111 .scope = RT_SCOPE_LINK,
112 },
113 [RTN_MULTICAST] = {
114 .error = 0,
115 .scope = RT_SCOPE_UNIVERSE,
116 },
117 [RTN_BLACKHOLE] = {
118 .error = -EINVAL,
119 .scope = RT_SCOPE_UNIVERSE,
120 },
121 [RTN_UNREACHABLE] = {
122 .error = -EHOSTUNREACH,
123 .scope = RT_SCOPE_UNIVERSE,
124 },
125 [RTN_PROHIBIT] = {
126 .error = -EACCES,
127 .scope = RT_SCOPE_UNIVERSE,
128 },
129 [RTN_THROW] = {
130 .error = -EAGAIN,
131 .scope = RT_SCOPE_UNIVERSE,
132 },
133 [RTN_NAT] = {
134 .error = -EINVAL,
135 .scope = RT_SCOPE_NOWHERE,
136 },
137 [RTN_XRESOLVE] = {
138 .error = -EINVAL,
139 .scope = RT_SCOPE_NOWHERE,
140 },
141 };
142
143 static void free_nh_exceptions(struct fib_nh *nh)
144 {
145 struct fnhe_hash_bucket *hash = nh->nh_exceptions;
146 int i;
147
148 for (i = 0; i < FNHE_HASH_SIZE; i++) {
149 struct fib_nh_exception *fnhe;
150
151 fnhe = rcu_dereference_protected(hash[i].chain, 1);
152 while (fnhe) {
153 struct fib_nh_exception *next;
154
155 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
156 kfree(fnhe);
157
158 fnhe = next;
159 }
160 }
161 kfree(hash);
162 }
163
164 /* Release a nexthop info record */
165 static void free_fib_info_rcu(struct rcu_head *head)
166 {
167 struct fib_info *fi = container_of(head, struct fib_info, rcu);
168
169 change_nexthops(fi) {
170 if (nexthop_nh->nh_dev)
171 dev_put(nexthop_nh->nh_dev);
172 if (nexthop_nh->nh_exceptions)
173 free_nh_exceptions(nexthop_nh);
174 if (nexthop_nh->nh_rth_output)
175 dst_release(&nexthop_nh->nh_rth_output->dst);
176 } endfor_nexthops(fi);
177
178 release_net(fi->fib_net);
179 if (fi->fib_metrics != (u32 *) dst_default_metrics)
180 kfree(fi->fib_metrics);
181 kfree(fi);
182 }
183
184 void free_fib_info(struct fib_info *fi)
185 {
186 if (fi->fib_dead == 0) {
187 pr_warn("Freeing alive fib_info %p\n", fi);
188 return;
189 }
190 fib_info_cnt--;
191 #ifdef CONFIG_IP_ROUTE_CLASSID
192 change_nexthops(fi) {
193 if (nexthop_nh->nh_tclassid)
194 fi->fib_net->ipv4.fib_num_tclassid_users--;
195 } endfor_nexthops(fi);
196 #endif
197 call_rcu(&fi->rcu, free_fib_info_rcu);
198 }
199
200 void fib_release_info(struct fib_info *fi)
201 {
202 spin_lock_bh(&fib_info_lock);
203 if (fi && --fi->fib_treeref == 0) {
204 hlist_del(&fi->fib_hash);
205 if (fi->fib_prefsrc)
206 hlist_del(&fi->fib_lhash);
207 change_nexthops(fi) {
208 if (!nexthop_nh->nh_dev)
209 continue;
210 hlist_del(&nexthop_nh->nh_hash);
211 } endfor_nexthops(fi)
212 fi->fib_dead = 1;
213 fib_info_put(fi);
214 }
215 spin_unlock_bh(&fib_info_lock);
216 }
217
218 static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
219 {
220 const struct fib_nh *onh = ofi->fib_nh;
221
222 for_nexthops(fi) {
223 if (nh->nh_oif != onh->nh_oif ||
224 nh->nh_gw != onh->nh_gw ||
225 nh->nh_scope != onh->nh_scope ||
226 #ifdef CONFIG_IP_ROUTE_MULTIPATH
227 nh->nh_weight != onh->nh_weight ||
228 #endif
229 #ifdef CONFIG_IP_ROUTE_CLASSID
230 nh->nh_tclassid != onh->nh_tclassid ||
231 #endif
232 ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD))
233 return -1;
234 onh++;
235 } endfor_nexthops(fi);
236 return 0;
237 }
238
239 static inline unsigned int fib_devindex_hashfn(unsigned int val)
240 {
241 unsigned int mask = DEVINDEX_HASHSIZE - 1;
242
243 return (val ^
244 (val >> DEVINDEX_HASHBITS) ^
245 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
246 }
247
248 static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
249 {
250 unsigned int mask = (fib_info_hash_size - 1);
251 unsigned int val = fi->fib_nhs;
252
253 val ^= (fi->fib_protocol << 8) | fi->fib_scope;
254 val ^= (__force u32)fi->fib_prefsrc;
255 val ^= fi->fib_priority;
256 for_nexthops(fi) {
257 val ^= fib_devindex_hashfn(nh->nh_oif);
258 } endfor_nexthops(fi)
259
260 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
261 }
262
263 static struct fib_info *fib_find_info(const struct fib_info *nfi)
264 {
265 struct hlist_head *head;
266 struct hlist_node *node;
267 struct fib_info *fi;
268 unsigned int hash;
269
270 hash = fib_info_hashfn(nfi);
271 head = &fib_info_hash[hash];
272
273 hlist_for_each_entry(fi, node, head, fib_hash) {
274 if (!net_eq(fi->fib_net, nfi->fib_net))
275 continue;
276 if (fi->fib_nhs != nfi->fib_nhs)
277 continue;
278 if (nfi->fib_protocol == fi->fib_protocol &&
279 nfi->fib_scope == fi->fib_scope &&
280 nfi->fib_prefsrc == fi->fib_prefsrc &&
281 nfi->fib_priority == fi->fib_priority &&
282 memcmp(nfi->fib_metrics, fi->fib_metrics,
283 sizeof(u32) * RTAX_MAX) == 0 &&
284 ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 &&
285 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
286 return fi;
287 }
288
289 return NULL;
290 }
291
292 /* Check, that the gateway is already configured.
293 * Used only by redirect accept routine.
294 */
295 int ip_fib_check_default(__be32 gw, struct net_device *dev)
296 {
297 struct hlist_head *head;
298 struct hlist_node *node;
299 struct fib_nh *nh;
300 unsigned int hash;
301
302 spin_lock(&fib_info_lock);
303
304 hash = fib_devindex_hashfn(dev->ifindex);
305 head = &fib_info_devhash[hash];
306 hlist_for_each_entry(nh, node, head, nh_hash) {
307 if (nh->nh_dev == dev &&
308 nh->nh_gw == gw &&
309 !(nh->nh_flags & RTNH_F_DEAD)) {
310 spin_unlock(&fib_info_lock);
311 return 0;
312 }
313 }
314
315 spin_unlock(&fib_info_lock);
316
317 return -1;
318 }
319
320 static inline size_t fib_nlmsg_size(struct fib_info *fi)
321 {
322 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
323 + nla_total_size(4) /* RTA_TABLE */
324 + nla_total_size(4) /* RTA_DST */
325 + nla_total_size(4) /* RTA_PRIORITY */
326 + nla_total_size(4); /* RTA_PREFSRC */
327
328 /* space for nested metrics */
329 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
330
331 if (fi->fib_nhs) {
332 /* Also handles the special case fib_nhs == 1 */
333
334 /* each nexthop is packed in an attribute */
335 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
336
337 /* may contain flow and gateway attribute */
338 nhsize += 2 * nla_total_size(4);
339
340 /* all nexthops are packed in a nested attribute */
341 payload += nla_total_size(fi->fib_nhs * nhsize);
342 }
343
344 return payload;
345 }
346
347 void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
348 int dst_len, u32 tb_id, struct nl_info *info,
349 unsigned int nlm_flags)
350 {
351 struct sk_buff *skb;
352 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
353 int err = -ENOBUFS;
354
355 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
356 if (skb == NULL)
357 goto errout;
358
359 err = fib_dump_info(skb, info->pid, seq, event, tb_id,
360 fa->fa_type, key, dst_len,
361 fa->fa_tos, fa->fa_info, nlm_flags);
362 if (err < 0) {
363 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
364 WARN_ON(err == -EMSGSIZE);
365 kfree_skb(skb);
366 goto errout;
367 }
368 rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE,
369 info->nlh, GFP_KERNEL);
370 return;
371 errout:
372 if (err < 0)
373 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
374 }
375
376 /* Return the first fib alias matching TOS with
377 * priority less than or equal to PRIO.
378 */
379 struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio)
380 {
381 if (fah) {
382 struct fib_alias *fa;
383 list_for_each_entry(fa, fah, fa_list) {
384 if (fa->fa_tos > tos)
385 continue;
386 if (fa->fa_info->fib_priority >= prio ||
387 fa->fa_tos < tos)
388 return fa;
389 }
390 }
391 return NULL;
392 }
393
394 int fib_detect_death(struct fib_info *fi, int order,
395 struct fib_info **last_resort, int *last_idx, int dflt)
396 {
397 struct neighbour *n;
398 int state = NUD_NONE;
399
400 n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
401 if (n) {
402 state = n->nud_state;
403 neigh_release(n);
404 }
405 if (state == NUD_REACHABLE)
406 return 0;
407 if ((state & NUD_VALID) && order != dflt)
408 return 0;
409 if ((state & NUD_VALID) ||
410 (*last_idx < 0 && order > dflt)) {
411 *last_resort = fi;
412 *last_idx = order;
413 }
414 return 1;
415 }
416
417 #ifdef CONFIG_IP_ROUTE_MULTIPATH
418
419 static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)
420 {
421 int nhs = 0;
422
423 while (rtnh_ok(rtnh, remaining)) {
424 nhs++;
425 rtnh = rtnh_next(rtnh, &remaining);
426 }
427
428 /* leftover implies invalid nexthop configuration, discard it */
429 return remaining > 0 ? 0 : nhs;
430 }
431
432 static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
433 int remaining, struct fib_config *cfg)
434 {
435 change_nexthops(fi) {
436 int attrlen;
437
438 if (!rtnh_ok(rtnh, remaining))
439 return -EINVAL;
440
441 nexthop_nh->nh_flags =
442 (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
443 nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
444 nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
445
446 attrlen = rtnh_attrlen(rtnh);
447 if (attrlen > 0) {
448 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
449
450 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
451 nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0;
452 #ifdef CONFIG_IP_ROUTE_CLASSID
453 nla = nla_find(attrs, attrlen, RTA_FLOW);
454 nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
455 if (nexthop_nh->nh_tclassid)
456 fi->fib_net->ipv4.fib_num_tclassid_users++;
457 #endif
458 }
459
460 rtnh = rtnh_next(rtnh, &remaining);
461 } endfor_nexthops(fi);
462
463 return 0;
464 }
465
466 #endif
467
468 int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
469 {
470 #ifdef CONFIG_IP_ROUTE_MULTIPATH
471 struct rtnexthop *rtnh;
472 int remaining;
473 #endif
474
475 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
476 return 1;
477
478 if (cfg->fc_oif || cfg->fc_gw) {
479 if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
480 (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
481 return 0;
482 return 1;
483 }
484
485 #ifdef CONFIG_IP_ROUTE_MULTIPATH
486 if (cfg->fc_mp == NULL)
487 return 0;
488
489 rtnh = cfg->fc_mp;
490 remaining = cfg->fc_mp_len;
491
492 for_nexthops(fi) {
493 int attrlen;
494
495 if (!rtnh_ok(rtnh, remaining))
496 return -EINVAL;
497
498 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
499 return 1;
500
501 attrlen = rtnh_attrlen(rtnh);
502 if (attrlen < 0) {
503 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
504
505 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
506 if (nla && nla_get_be32(nla) != nh->nh_gw)
507 return 1;
508 #ifdef CONFIG_IP_ROUTE_CLASSID
509 nla = nla_find(attrs, attrlen, RTA_FLOW);
510 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
511 return 1;
512 #endif
513 }
514
515 rtnh = rtnh_next(rtnh, &remaining);
516 } endfor_nexthops(fi);
517 #endif
518 return 0;
519 }
520
521
522 /*
523 * Picture
524 * -------
525 *
526 * Semantics of nexthop is very messy by historical reasons.
527 * We have to take into account, that:
528 * a) gateway can be actually local interface address,
529 * so that gatewayed route is direct.
530 * b) gateway must be on-link address, possibly
531 * described not by an ifaddr, but also by a direct route.
532 * c) If both gateway and interface are specified, they should not
533 * contradict.
534 * d) If we use tunnel routes, gateway could be not on-link.
535 *
536 * Attempt to reconcile all of these (alas, self-contradictory) conditions
537 * results in pretty ugly and hairy code with obscure logic.
538 *
539 * I chose to generalized it instead, so that the size
540 * of code does not increase practically, but it becomes
541 * much more general.
542 * Every prefix is assigned a "scope" value: "host" is local address,
543 * "link" is direct route,
544 * [ ... "site" ... "interior" ... ]
545 * and "universe" is true gateway route with global meaning.
546 *
547 * Every prefix refers to a set of "nexthop"s (gw, oif),
548 * where gw must have narrower scope. This recursion stops
549 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
550 * which means that gw is forced to be on link.
551 *
552 * Code is still hairy, but now it is apparently logically
553 * consistent and very flexible. F.e. as by-product it allows
554 * to co-exists in peace independent exterior and interior
555 * routing processes.
556 *
557 * Normally it looks as following.
558 *
559 * {universe prefix} -> (gw, oif) [scope link]
560 * |
561 * |-> {link prefix} -> (gw, oif) [scope local]
562 * |
563 * |-> {local prefix} (terminal node)
564 */
565 static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
566 struct fib_nh *nh)
567 {
568 int err;
569 struct net *net;
570 struct net_device *dev;
571
572 net = cfg->fc_nlinfo.nl_net;
573 if (nh->nh_gw) {
574 struct fib_result res;
575
576 if (nh->nh_flags & RTNH_F_ONLINK) {
577
578 if (cfg->fc_scope >= RT_SCOPE_LINK)
579 return -EINVAL;
580 if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST)
581 return -EINVAL;
582 dev = __dev_get_by_index(net, nh->nh_oif);
583 if (!dev)
584 return -ENODEV;
585 if (!(dev->flags & IFF_UP))
586 return -ENETDOWN;
587 nh->nh_dev = dev;
588 dev_hold(dev);
589 nh->nh_scope = RT_SCOPE_LINK;
590 return 0;
591 }
592 rcu_read_lock();
593 {
594 struct flowi4 fl4 = {
595 .daddr = nh->nh_gw,
596 .flowi4_scope = cfg->fc_scope + 1,
597 .flowi4_oif = nh->nh_oif,
598 };
599
600 /* It is not necessary, but requires a bit of thinking */
601 if (fl4.flowi4_scope < RT_SCOPE_LINK)
602 fl4.flowi4_scope = RT_SCOPE_LINK;
603 err = fib_lookup(net, &fl4, &res);
604 if (err) {
605 rcu_read_unlock();
606 return err;
607 }
608 }
609 err = -EINVAL;
610 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
611 goto out;
612 nh->nh_scope = res.scope;
613 nh->nh_oif = FIB_RES_OIF(res);
614 nh->nh_dev = dev = FIB_RES_DEV(res);
615 if (!dev)
616 goto out;
617 dev_hold(dev);
618 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
619 } else {
620 struct in_device *in_dev;
621
622 if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
623 return -EINVAL;
624
625 rcu_read_lock();
626 err = -ENODEV;
627 in_dev = inetdev_by_index(net, nh->nh_oif);
628 if (in_dev == NULL)
629 goto out;
630 err = -ENETDOWN;
631 if (!(in_dev->dev->flags & IFF_UP))
632 goto out;
633 nh->nh_dev = in_dev->dev;
634 dev_hold(nh->nh_dev);
635 nh->nh_scope = RT_SCOPE_HOST;
636 err = 0;
637 }
638 out:
639 rcu_read_unlock();
640 return err;
641 }
642
643 static inline unsigned int fib_laddr_hashfn(__be32 val)
644 {
645 unsigned int mask = (fib_info_hash_size - 1);
646
647 return ((__force u32)val ^
648 ((__force u32)val >> 7) ^
649 ((__force u32)val >> 14)) & mask;
650 }
651
652 static struct hlist_head *fib_info_hash_alloc(int bytes)
653 {
654 if (bytes <= PAGE_SIZE)
655 return kzalloc(bytes, GFP_KERNEL);
656 else
657 return (struct hlist_head *)
658 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
659 get_order(bytes));
660 }
661
662 static void fib_info_hash_free(struct hlist_head *hash, int bytes)
663 {
664 if (!hash)
665 return;
666
667 if (bytes <= PAGE_SIZE)
668 kfree(hash);
669 else
670 free_pages((unsigned long) hash, get_order(bytes));
671 }
672
673 static void fib_info_hash_move(struct hlist_head *new_info_hash,
674 struct hlist_head *new_laddrhash,
675 unsigned int new_size)
676 {
677 struct hlist_head *old_info_hash, *old_laddrhash;
678 unsigned int old_size = fib_info_hash_size;
679 unsigned int i, bytes;
680
681 spin_lock_bh(&fib_info_lock);
682 old_info_hash = fib_info_hash;
683 old_laddrhash = fib_info_laddrhash;
684 fib_info_hash_size = new_size;
685
686 for (i = 0; i < old_size; i++) {
687 struct hlist_head *head = &fib_info_hash[i];
688 struct hlist_node *node, *n;
689 struct fib_info *fi;
690
691 hlist_for_each_entry_safe(fi, node, n, head, fib_hash) {
692 struct hlist_head *dest;
693 unsigned int new_hash;
694
695 hlist_del(&fi->fib_hash);
696
697 new_hash = fib_info_hashfn(fi);
698 dest = &new_info_hash[new_hash];
699 hlist_add_head(&fi->fib_hash, dest);
700 }
701 }
702 fib_info_hash = new_info_hash;
703
704 for (i = 0; i < old_size; i++) {
705 struct hlist_head *lhead = &fib_info_laddrhash[i];
706 struct hlist_node *node, *n;
707 struct fib_info *fi;
708
709 hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) {
710 struct hlist_head *ldest;
711 unsigned int new_hash;
712
713 hlist_del(&fi->fib_lhash);
714
715 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
716 ldest = &new_laddrhash[new_hash];
717 hlist_add_head(&fi->fib_lhash, ldest);
718 }
719 }
720 fib_info_laddrhash = new_laddrhash;
721
722 spin_unlock_bh(&fib_info_lock);
723
724 bytes = old_size * sizeof(struct hlist_head *);
725 fib_info_hash_free(old_info_hash, bytes);
726 fib_info_hash_free(old_laddrhash, bytes);
727 }
728
729 __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
730 {
731 nh->nh_saddr = inet_select_addr(nh->nh_dev,
732 nh->nh_gw,
733 nh->nh_parent->fib_scope);
734 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
735
736 return nh->nh_saddr;
737 }
738
739 struct fib_info *fib_create_info(struct fib_config *cfg)
740 {
741 int err;
742 struct fib_info *fi = NULL;
743 struct fib_info *ofi;
744 int nhs = 1;
745 struct net *net = cfg->fc_nlinfo.nl_net;
746
747 if (cfg->fc_type > RTN_MAX)
748 goto err_inval;
749
750 /* Fast check to catch the most weird cases */
751 if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
752 goto err_inval;
753
754 #ifdef CONFIG_IP_ROUTE_MULTIPATH
755 if (cfg->fc_mp) {
756 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
757 if (nhs == 0)
758 goto err_inval;
759 }
760 #endif
761
762 err = -ENOBUFS;
763 if (fib_info_cnt >= fib_info_hash_size) {
764 unsigned int new_size = fib_info_hash_size << 1;
765 struct hlist_head *new_info_hash;
766 struct hlist_head *new_laddrhash;
767 unsigned int bytes;
768
769 if (!new_size)
770 new_size = 1;
771 bytes = new_size * sizeof(struct hlist_head *);
772 new_info_hash = fib_info_hash_alloc(bytes);
773 new_laddrhash = fib_info_hash_alloc(bytes);
774 if (!new_info_hash || !new_laddrhash) {
775 fib_info_hash_free(new_info_hash, bytes);
776 fib_info_hash_free(new_laddrhash, bytes);
777 } else
778 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
779
780 if (!fib_info_hash_size)
781 goto failure;
782 }
783
784 fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
785 if (fi == NULL)
786 goto failure;
787 if (cfg->fc_mx) {
788 fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
789 if (!fi->fib_metrics)
790 goto failure;
791 } else
792 fi->fib_metrics = (u32 *) dst_default_metrics;
793 fib_info_cnt++;
794
795 fi->fib_net = hold_net(net);
796 fi->fib_protocol = cfg->fc_protocol;
797 fi->fib_scope = cfg->fc_scope;
798 fi->fib_flags = cfg->fc_flags;
799 fi->fib_priority = cfg->fc_priority;
800 fi->fib_prefsrc = cfg->fc_prefsrc;
801
802 fi->fib_nhs = nhs;
803 change_nexthops(fi) {
804 nexthop_nh->nh_parent = fi;
805 } endfor_nexthops(fi)
806
807 if (cfg->fc_mx) {
808 struct nlattr *nla;
809 int remaining;
810
811 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
812 int type = nla_type(nla);
813
814 if (type) {
815 u32 val;
816
817 if (type > RTAX_MAX)
818 goto err_inval;
819 val = nla_get_u32(nla);
820 if (type == RTAX_ADVMSS && val > 65535 - 40)
821 val = 65535 - 40;
822 if (type == RTAX_MTU && val > 65535 - 15)
823 val = 65535 - 15;
824 fi->fib_metrics[type - 1] = val;
825 }
826 }
827 }
828
829 if (cfg->fc_mp) {
830 #ifdef CONFIG_IP_ROUTE_MULTIPATH
831 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg);
832 if (err != 0)
833 goto failure;
834 if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif)
835 goto err_inval;
836 if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw)
837 goto err_inval;
838 #ifdef CONFIG_IP_ROUTE_CLASSID
839 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow)
840 goto err_inval;
841 #endif
842 #else
843 goto err_inval;
844 #endif
845 } else {
846 struct fib_nh *nh = fi->fib_nh;
847
848 nh->nh_oif = cfg->fc_oif;
849 nh->nh_gw = cfg->fc_gw;
850 nh->nh_flags = cfg->fc_flags;
851 #ifdef CONFIG_IP_ROUTE_CLASSID
852 nh->nh_tclassid = cfg->fc_flow;
853 if (nh->nh_tclassid)
854 fi->fib_net->ipv4.fib_num_tclassid_users++;
855 #endif
856 #ifdef CONFIG_IP_ROUTE_MULTIPATH
857 nh->nh_weight = 1;
858 #endif
859 }
860
861 if (fib_props[cfg->fc_type].error) {
862 if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
863 goto err_inval;
864 goto link_it;
865 } else {
866 switch (cfg->fc_type) {
867 case RTN_UNICAST:
868 case RTN_LOCAL:
869 case RTN_BROADCAST:
870 case RTN_ANYCAST:
871 case RTN_MULTICAST:
872 break;
873 default:
874 goto err_inval;
875 }
876 }
877
878 if (cfg->fc_scope > RT_SCOPE_HOST)
879 goto err_inval;
880
881 if (cfg->fc_scope == RT_SCOPE_HOST) {
882 struct fib_nh *nh = fi->fib_nh;
883
884 /* Local address is added. */
885 if (nhs != 1 || nh->nh_gw)
886 goto err_inval;
887 nh->nh_scope = RT_SCOPE_NOWHERE;
888 nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
889 err = -ENODEV;
890 if (nh->nh_dev == NULL)
891 goto failure;
892 } else {
893 change_nexthops(fi) {
894 err = fib_check_nh(cfg, fi, nexthop_nh);
895 if (err != 0)
896 goto failure;
897 } endfor_nexthops(fi)
898 }
899
900 if (fi->fib_prefsrc) {
901 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
902 fi->fib_prefsrc != cfg->fc_dst)
903 if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL)
904 goto err_inval;
905 }
906
907 change_nexthops(fi) {
908 fib_info_update_nh_saddr(net, nexthop_nh);
909 } endfor_nexthops(fi)
910
911 link_it:
912 ofi = fib_find_info(fi);
913 if (ofi) {
914 fi->fib_dead = 1;
915 free_fib_info(fi);
916 ofi->fib_treeref++;
917 return ofi;
918 }
919
920 fi->fib_treeref++;
921 atomic_inc(&fi->fib_clntref);
922 spin_lock_bh(&fib_info_lock);
923 hlist_add_head(&fi->fib_hash,
924 &fib_info_hash[fib_info_hashfn(fi)]);
925 if (fi->fib_prefsrc) {
926 struct hlist_head *head;
927
928 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
929 hlist_add_head(&fi->fib_lhash, head);
930 }
931 change_nexthops(fi) {
932 struct hlist_head *head;
933 unsigned int hash;
934
935 if (!nexthop_nh->nh_dev)
936 continue;
937 hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
938 head = &fib_info_devhash[hash];
939 hlist_add_head(&nexthop_nh->nh_hash, head);
940 } endfor_nexthops(fi)
941 spin_unlock_bh(&fib_info_lock);
942 return fi;
943
944 err_inval:
945 err = -EINVAL;
946
947 failure:
948 if (fi) {
949 fi->fib_dead = 1;
950 free_fib_info(fi);
951 }
952
953 return ERR_PTR(err);
954 }
955
956 int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
957 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
958 struct fib_info *fi, unsigned int flags)
959 {
960 struct nlmsghdr *nlh;
961 struct rtmsg *rtm;
962
963 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags);
964 if (nlh == NULL)
965 return -EMSGSIZE;
966
967 rtm = nlmsg_data(nlh);
968 rtm->rtm_family = AF_INET;
969 rtm->rtm_dst_len = dst_len;
970 rtm->rtm_src_len = 0;
971 rtm->rtm_tos = tos;
972 if (tb_id < 256)
973 rtm->rtm_table = tb_id;
974 else
975 rtm->rtm_table = RT_TABLE_COMPAT;
976 if (nla_put_u32(skb, RTA_TABLE, tb_id))
977 goto nla_put_failure;
978 rtm->rtm_type = type;
979 rtm->rtm_flags = fi->fib_flags;
980 rtm->rtm_scope = fi->fib_scope;
981 rtm->rtm_protocol = fi->fib_protocol;
982
983 if (rtm->rtm_dst_len &&
984 nla_put_be32(skb, RTA_DST, dst))
985 goto nla_put_failure;
986 if (fi->fib_priority &&
987 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
988 goto nla_put_failure;
989 if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
990 goto nla_put_failure;
991
992 if (fi->fib_prefsrc &&
993 nla_put_be32(skb, RTA_PREFSRC, fi->fib_prefsrc))
994 goto nla_put_failure;
995 if (fi->fib_nhs == 1) {
996 if (fi->fib_nh->nh_gw &&
997 nla_put_be32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw))
998 goto nla_put_failure;
999 if (fi->fib_nh->nh_oif &&
1000 nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
1001 goto nla_put_failure;
1002 #ifdef CONFIG_IP_ROUTE_CLASSID
1003 if (fi->fib_nh[0].nh_tclassid &&
1004 nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid))
1005 goto nla_put_failure;
1006 #endif
1007 }
1008 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1009 if (fi->fib_nhs > 1) {
1010 struct rtnexthop *rtnh;
1011 struct nlattr *mp;
1012
1013 mp = nla_nest_start(skb, RTA_MULTIPATH);
1014 if (mp == NULL)
1015 goto nla_put_failure;
1016
1017 for_nexthops(fi) {
1018 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1019 if (rtnh == NULL)
1020 goto nla_put_failure;
1021
1022 rtnh->rtnh_flags = nh->nh_flags & 0xFF;
1023 rtnh->rtnh_hops = nh->nh_weight - 1;
1024 rtnh->rtnh_ifindex = nh->nh_oif;
1025
1026 if (nh->nh_gw &&
1027 nla_put_be32(skb, RTA_GATEWAY, nh->nh_gw))
1028 goto nla_put_failure;
1029 #ifdef CONFIG_IP_ROUTE_CLASSID
1030 if (nh->nh_tclassid &&
1031 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1032 goto nla_put_failure;
1033 #endif
1034 /* length of rtnetlink header + attributes */
1035 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
1036 } endfor_nexthops(fi);
1037
1038 nla_nest_end(skb, mp);
1039 }
1040 #endif
1041 return nlmsg_end(skb, nlh);
1042
1043 nla_put_failure:
1044 nlmsg_cancel(skb, nlh);
1045 return -EMSGSIZE;
1046 }
1047
1048 /*
1049 * Update FIB if:
1050 * - local address disappeared -> we must delete all the entries
1051 * referring to it.
1052 * - device went down -> we must shutdown all nexthops going via it.
1053 */
1054 int fib_sync_down_addr(struct net *net, __be32 local)
1055 {
1056 int ret = 0;
1057 unsigned int hash = fib_laddr_hashfn(local);
1058 struct hlist_head *head = &fib_info_laddrhash[hash];
1059 struct hlist_node *node;
1060 struct fib_info *fi;
1061
1062 if (fib_info_laddrhash == NULL || local == 0)
1063 return 0;
1064
1065 hlist_for_each_entry(fi, node, head, fib_lhash) {
1066 if (!net_eq(fi->fib_net, net))
1067 continue;
1068 if (fi->fib_prefsrc == local) {
1069 fi->fib_flags |= RTNH_F_DEAD;
1070 ret++;
1071 }
1072 }
1073 return ret;
1074 }
1075
1076 int fib_sync_down_dev(struct net_device *dev, int force)
1077 {
1078 int ret = 0;
1079 int scope = RT_SCOPE_NOWHERE;
1080 struct fib_info *prev_fi = NULL;
1081 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1082 struct hlist_head *head = &fib_info_devhash[hash];
1083 struct hlist_node *node;
1084 struct fib_nh *nh;
1085
1086 if (force)
1087 scope = -1;
1088
1089 hlist_for_each_entry(nh, node, head, nh_hash) {
1090 struct fib_info *fi = nh->nh_parent;
1091 int dead;
1092
1093 BUG_ON(!fi->fib_nhs);
1094 if (nh->nh_dev != dev || fi == prev_fi)
1095 continue;
1096 prev_fi = fi;
1097 dead = 0;
1098 change_nexthops(fi) {
1099 if (nexthop_nh->nh_flags & RTNH_F_DEAD)
1100 dead++;
1101 else if (nexthop_nh->nh_dev == dev &&
1102 nexthop_nh->nh_scope != scope) {
1103 nexthop_nh->nh_flags |= RTNH_F_DEAD;
1104 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1105 spin_lock_bh(&fib_multipath_lock);
1106 fi->fib_power -= nexthop_nh->nh_power;
1107 nexthop_nh->nh_power = 0;
1108 spin_unlock_bh(&fib_multipath_lock);
1109 #endif
1110 dead++;
1111 }
1112 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1113 if (force > 1 && nexthop_nh->nh_dev == dev) {
1114 dead = fi->fib_nhs;
1115 break;
1116 }
1117 #endif
1118 } endfor_nexthops(fi)
1119 if (dead == fi->fib_nhs) {
1120 fi->fib_flags |= RTNH_F_DEAD;
1121 ret++;
1122 }
1123 }
1124
1125 return ret;
1126 }
1127
1128 /* Must be invoked inside of an RCU protected region. */
1129 void fib_select_default(struct fib_result *res)
1130 {
1131 struct fib_info *fi = NULL, *last_resort = NULL;
1132 struct list_head *fa_head = res->fa_head;
1133 struct fib_table *tb = res->table;
1134 int order = -1, last_idx = -1;
1135 struct fib_alias *fa;
1136
1137 list_for_each_entry_rcu(fa, fa_head, fa_list) {
1138 struct fib_info *next_fi = fa->fa_info;
1139
1140 if (next_fi->fib_scope != res->scope ||
1141 fa->fa_type != RTN_UNICAST)
1142 continue;
1143
1144 if (next_fi->fib_priority > res->fi->fib_priority)
1145 break;
1146 if (!next_fi->fib_nh[0].nh_gw ||
1147 next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
1148 continue;
1149
1150 fib_alias_accessed(fa);
1151
1152 if (fi == NULL) {
1153 if (next_fi != res->fi)
1154 break;
1155 } else if (!fib_detect_death(fi, order, &last_resort,
1156 &last_idx, tb->tb_default)) {
1157 fib_result_assign(res, fi);
1158 tb->tb_default = order;
1159 goto out;
1160 }
1161 fi = next_fi;
1162 order++;
1163 }
1164
1165 if (order <= 0 || fi == NULL) {
1166 tb->tb_default = -1;
1167 goto out;
1168 }
1169
1170 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
1171 tb->tb_default)) {
1172 fib_result_assign(res, fi);
1173 tb->tb_default = order;
1174 goto out;
1175 }
1176
1177 if (last_idx >= 0)
1178 fib_result_assign(res, last_resort);
1179 tb->tb_default = last_idx;
1180 out:
1181 return;
1182 }
1183
1184 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1185
1186 /*
1187 * Dead device goes up. We wake up dead nexthops.
1188 * It takes sense only on multipath routes.
1189 */
1190 int fib_sync_up(struct net_device *dev)
1191 {
1192 struct fib_info *prev_fi;
1193 unsigned int hash;
1194 struct hlist_head *head;
1195 struct hlist_node *node;
1196 struct fib_nh *nh;
1197 int ret;
1198
1199 if (!(dev->flags & IFF_UP))
1200 return 0;
1201
1202 prev_fi = NULL;
1203 hash = fib_devindex_hashfn(dev->ifindex);
1204 head = &fib_info_devhash[hash];
1205 ret = 0;
1206
1207 hlist_for_each_entry(nh, node, head, nh_hash) {
1208 struct fib_info *fi = nh->nh_parent;
1209 int alive;
1210
1211 BUG_ON(!fi->fib_nhs);
1212 if (nh->nh_dev != dev || fi == prev_fi)
1213 continue;
1214
1215 prev_fi = fi;
1216 alive = 0;
1217 change_nexthops(fi) {
1218 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
1219 alive++;
1220 continue;
1221 }
1222 if (nexthop_nh->nh_dev == NULL ||
1223 !(nexthop_nh->nh_dev->flags & IFF_UP))
1224 continue;
1225 if (nexthop_nh->nh_dev != dev ||
1226 !__in_dev_get_rtnl(dev))
1227 continue;
1228 alive++;
1229 spin_lock_bh(&fib_multipath_lock);
1230 nexthop_nh->nh_power = 0;
1231 nexthop_nh->nh_flags &= ~RTNH_F_DEAD;
1232 spin_unlock_bh(&fib_multipath_lock);
1233 } endfor_nexthops(fi)
1234
1235 if (alive > 0) {
1236 fi->fib_flags &= ~RTNH_F_DEAD;
1237 ret++;
1238 }
1239 }
1240
1241 return ret;
1242 }
1243
1244 /*
1245 * The algorithm is suboptimal, but it provides really
1246 * fair weighted route distribution.
1247 */
1248 void fib_select_multipath(struct fib_result *res)
1249 {
1250 struct fib_info *fi = res->fi;
1251 int w;
1252
1253 spin_lock_bh(&fib_multipath_lock);
1254 if (fi->fib_power <= 0) {
1255 int power = 0;
1256 change_nexthops(fi) {
1257 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
1258 power += nexthop_nh->nh_weight;
1259 nexthop_nh->nh_power = nexthop_nh->nh_weight;
1260 }
1261 } endfor_nexthops(fi);
1262 fi->fib_power = power;
1263 if (power <= 0) {
1264 spin_unlock_bh(&fib_multipath_lock);
1265 /* Race condition: route has just become dead. */
1266 res->nh_sel = 0;
1267 return;
1268 }
1269 }
1270
1271
1272 /* w should be random number [0..fi->fib_power-1],
1273 * it is pretty bad approximation.
1274 */
1275
1276 w = jiffies % fi->fib_power;
1277
1278 change_nexthops(fi) {
1279 if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) &&
1280 nexthop_nh->nh_power) {
1281 w -= nexthop_nh->nh_power;
1282 if (w <= 0) {
1283 nexthop_nh->nh_power--;
1284 fi->fib_power--;
1285 res->nh_sel = nhsel;
1286 spin_unlock_bh(&fib_multipath_lock);
1287 return;
1288 }
1289 }
1290 } endfor_nexthops(fi);
1291
1292 /* Race condition: route has just become dead. */
1293 res->nh_sel = 0;
1294 spin_unlock_bh(&fib_multipath_lock);
1295 }
1296 #endif
This page took 0.070062 seconds and 6 git commands to generate.