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