blob: f32feba60d64b89323a6e77c5ba1965cd47c49a7 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
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 <linux/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#include <linux/netlink.h>
36
37#include <net/arp.h>
38#include <net/ip.h>
39#include <net/protocol.h>
40#include <net/route.h>
41#include <net/tcp.h>
42#include <net/sock.h>
43#include <net/ip_fib.h>
44#include <net/netlink.h>
45#include <net/nexthop.h>
46#include <net/lwtunnel.h>
47#include <net/fib_notifier.h>
48
49#include "fib_lookup.h"
50
51static DEFINE_SPINLOCK(fib_info_lock);
52static struct hlist_head *fib_info_hash;
53static struct hlist_head *fib_info_laddrhash;
54static unsigned int fib_info_hash_size;
55static unsigned int fib_info_cnt;
56
57#define DEVINDEX_HASHBITS 8
58#define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
59static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
60
61#ifdef CONFIG_IP_ROUTE_MULTIPATH
62
63#define for_nexthops(fi) { \
64 int nhsel; const struct fib_nh *nh; \
65 for (nhsel = 0, nh = (fi)->fib_nh; \
66 nhsel < (fi)->fib_nhs; \
67 nh++, nhsel++)
68
69#define change_nexthops(fi) { \
70 int nhsel; struct fib_nh *nexthop_nh; \
71 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
72 nhsel < (fi)->fib_nhs; \
73 nexthop_nh++, nhsel++)
74
75#else /* CONFIG_IP_ROUTE_MULTIPATH */
76
77/* Hope, that gcc will optimize it to get rid of dummy loop */
78
79#define for_nexthops(fi) { \
80 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
81 for (nhsel = 0; nhsel < 1; nhsel++)
82
83#define change_nexthops(fi) { \
84 int nhsel; \
85 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
86 for (nhsel = 0; nhsel < 1; nhsel++)
87
88#endif /* CONFIG_IP_ROUTE_MULTIPATH */
89
90#define endfor_nexthops(fi) }
91
92
93const struct fib_prop fib_props[RTN_MAX + 1] = {
94 [RTN_UNSPEC] = {
95 .error = 0,
96 .scope = RT_SCOPE_NOWHERE,
97 },
98 [RTN_UNICAST] = {
99 .error = 0,
100 .scope = RT_SCOPE_UNIVERSE,
101 },
102 [RTN_LOCAL] = {
103 .error = 0,
104 .scope = RT_SCOPE_HOST,
105 },
106 [RTN_BROADCAST] = {
107 .error = 0,
108 .scope = RT_SCOPE_LINK,
109 },
110 [RTN_ANYCAST] = {
111 .error = 0,
112 .scope = RT_SCOPE_LINK,
113 },
114 [RTN_MULTICAST] = {
115 .error = 0,
116 .scope = RT_SCOPE_UNIVERSE,
117 },
118 [RTN_BLACKHOLE] = {
119 .error = -EINVAL,
120 .scope = RT_SCOPE_UNIVERSE,
121 },
122 [RTN_UNREACHABLE] = {
123 .error = -EHOSTUNREACH,
124 .scope = RT_SCOPE_UNIVERSE,
125 },
126 [RTN_PROHIBIT] = {
127 .error = -EACCES,
128 .scope = RT_SCOPE_UNIVERSE,
129 },
130 [RTN_THROW] = {
131 .error = -EAGAIN,
132 .scope = RT_SCOPE_UNIVERSE,
133 },
134 [RTN_NAT] = {
135 .error = -EINVAL,
136 .scope = RT_SCOPE_NOWHERE,
137 },
138 [RTN_XRESOLVE] = {
139 .error = -EINVAL,
140 .scope = RT_SCOPE_NOWHERE,
141 },
142 [RTN_POLICY_FAILED] = {
143 .error = -EACCES,
144 .scope = RT_SCOPE_UNIVERSE,
145 },
146};
147
148static void rt_fibinfo_free(struct rtable __rcu **rtp)
149{
150 struct rtable *rt = rcu_dereference_protected(*rtp, 1);
151
152 if (!rt)
153 return;
154
155 /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
156 * because we waited an RCU grace period before calling
157 * free_fib_info_rcu()
158 */
159
160 dst_dev_put(&rt->dst);
161 dst_release_immediate(&rt->dst);
162}
163
164static void free_nh_exceptions(struct fib_nh *nh)
165{
166 struct fnhe_hash_bucket *hash;
167 int i;
168
169 hash = rcu_dereference_protected(nh->nh_exceptions, 1);
170 if (!hash)
171 return;
172 for (i = 0; i < FNHE_HASH_SIZE; i++) {
173 struct fib_nh_exception *fnhe;
174
175 fnhe = rcu_dereference_protected(hash[i].chain, 1);
176 while (fnhe) {
177 struct fib_nh_exception *next;
178
179 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
180
181 rt_fibinfo_free(&fnhe->fnhe_rth_input);
182 rt_fibinfo_free(&fnhe->fnhe_rth_output);
183
184 kfree(fnhe);
185
186 fnhe = next;
187 }
188 }
189 kfree(hash);
190}
191
192static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
193{
194 int cpu;
195
196 if (!rtp)
197 return;
198
199 for_each_possible_cpu(cpu) {
200 struct rtable *rt;
201
202 rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
203 if (rt) {
204 dst_dev_put(&rt->dst);
205 dst_release_immediate(&rt->dst);
206 }
207 }
208 free_percpu(rtp);
209}
210
211/* Release a nexthop info record */
212static void free_fib_info_rcu(struct rcu_head *head)
213{
214 struct fib_info *fi = container_of(head, struct fib_info, rcu);
215 struct dst_metrics *m;
216
217 change_nexthops(fi) {
218 if (nexthop_nh->nh_dev)
219 dev_put(nexthop_nh->nh_dev);
220 lwtstate_put(nexthop_nh->nh_lwtstate);
221 free_nh_exceptions(nexthop_nh);
222 rt_fibinfo_free_cpus(nexthop_nh->nh_pcpu_rth_output);
223 rt_fibinfo_free(&nexthop_nh->nh_rth_input);
224 } endfor_nexthops(fi);
225
226 m = fi->fib_metrics;
227 if (m != &dst_default_metrics && refcount_dec_and_test(&m->refcnt))
228 kfree(m);
229 kfree(fi);
230}
231
232void free_fib_info(struct fib_info *fi)
233{
234 if (fi->fib_dead == 0) {
235 pr_warn("Freeing alive fib_info %p\n", fi);
236 return;
237 }
238 fib_info_cnt--;
239#ifdef CONFIG_IP_ROUTE_CLASSID
240 change_nexthops(fi) {
241 if (nexthop_nh->nh_tclassid)
242 fi->fib_net->ipv4.fib_num_tclassid_users--;
243 } endfor_nexthops(fi);
244#endif
245 call_rcu(&fi->rcu, free_fib_info_rcu);
246}
247EXPORT_SYMBOL_GPL(free_fib_info);
248
249void fib_release_info(struct fib_info *fi)
250{
251 spin_lock_bh(&fib_info_lock);
252 if (fi && --fi->fib_treeref == 0) {
253 hlist_del(&fi->fib_hash);
254 if (fi->fib_prefsrc)
255 hlist_del(&fi->fib_lhash);
256 change_nexthops(fi) {
257 if (!nexthop_nh->nh_dev)
258 continue;
259 hlist_del(&nexthop_nh->nh_hash);
260 } endfor_nexthops(fi)
261 fi->fib_dead = 1;
262 fib_info_put(fi);
263 }
264 spin_unlock_bh(&fib_info_lock);
265}
266
267static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
268{
269 const struct fib_nh *onh = ofi->fib_nh;
270
271 for_nexthops(fi) {
272 if (nh->nh_oif != onh->nh_oif ||
273 nh->nh_gw != onh->nh_gw ||
274 nh->nh_scope != onh->nh_scope ||
275#ifdef CONFIG_IP_ROUTE_MULTIPATH
276 nh->nh_weight != onh->nh_weight ||
277#endif
278#ifdef CONFIG_IP_ROUTE_CLASSID
279 nh->nh_tclassid != onh->nh_tclassid ||
280#endif
281 lwtunnel_cmp_encap(nh->nh_lwtstate, onh->nh_lwtstate) ||
282 ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_COMPARE_MASK))
283 return -1;
284 onh++;
285 } endfor_nexthops(fi);
286 return 0;
287}
288
289static inline unsigned int fib_devindex_hashfn(unsigned int val)
290{
291 unsigned int mask = DEVINDEX_HASHSIZE - 1;
292
293 return (val ^
294 (val >> DEVINDEX_HASHBITS) ^
295 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
296}
297
298static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
299{
300 unsigned int mask = (fib_info_hash_size - 1);
301 unsigned int val = fi->fib_nhs;
302
303 val ^= (fi->fib_protocol << 8) | fi->fib_scope;
304 val ^= (__force u32)fi->fib_prefsrc;
305 val ^= fi->fib_priority;
306 for_nexthops(fi) {
307 val ^= fib_devindex_hashfn(nh->nh_oif);
308 } endfor_nexthops(fi)
309
310 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
311}
312
313static struct fib_info *fib_find_info(const struct fib_info *nfi)
314{
315 struct hlist_head *head;
316 struct fib_info *fi;
317 unsigned int hash;
318
319 hash = fib_info_hashfn(nfi);
320 head = &fib_info_hash[hash];
321
322 hlist_for_each_entry(fi, head, fib_hash) {
323 if (!net_eq(fi->fib_net, nfi->fib_net))
324 continue;
325 if (fi->fib_nhs != nfi->fib_nhs)
326 continue;
327 if (nfi->fib_protocol == fi->fib_protocol &&
328 nfi->fib_scope == fi->fib_scope &&
329 nfi->fib_prefsrc == fi->fib_prefsrc &&
330 nfi->fib_priority == fi->fib_priority &&
331 nfi->fib_type == fi->fib_type &&
332 memcmp(nfi->fib_metrics, fi->fib_metrics,
333 sizeof(u32) * RTAX_MAX) == 0 &&
334 !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
335 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
336 return fi;
337 }
338
339 return NULL;
340}
341
342/* Check, that the gateway is already configured.
343 * Used only by redirect accept routine.
344 */
345int ip_fib_check_default(__be32 gw, struct net_device *dev)
346{
347 struct hlist_head *head;
348 struct fib_nh *nh;
349 unsigned int hash;
350
351 spin_lock(&fib_info_lock);
352
353 hash = fib_devindex_hashfn(dev->ifindex);
354 head = &fib_info_devhash[hash];
355 hlist_for_each_entry(nh, head, nh_hash) {
356 if (nh->nh_dev == dev &&
357 nh->nh_gw == gw &&
358 !(nh->nh_flags & RTNH_F_DEAD)) {
359 spin_unlock(&fib_info_lock);
360 return 0;
361 }
362 }
363
364 spin_unlock(&fib_info_lock);
365
366 return -1;
367}
368
369static inline size_t fib_nlmsg_size(struct fib_info *fi)
370{
371 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
372 + nla_total_size(4) /* RTA_TABLE */
373 + nla_total_size(4) /* RTA_DST */
374 + nla_total_size(4) /* RTA_PRIORITY */
375 + nla_total_size(4) /* RTA_PREFSRC */
376 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
377
378 /* space for nested metrics */
379 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
380
381 if (fi->fib_nhs) {
382 size_t nh_encapsize = 0;
383 /* Also handles the special case fib_nhs == 1 */
384
385 /* each nexthop is packed in an attribute */
386 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
387
388 /* may contain flow and gateway attribute */
389 nhsize += 2 * nla_total_size(4);
390
391 /* grab encap info */
392 for_nexthops(fi) {
393 if (nh->nh_lwtstate) {
394 /* RTA_ENCAP_TYPE */
395 nh_encapsize += lwtunnel_get_encap_size(
396 nh->nh_lwtstate);
397 /* RTA_ENCAP */
398 nh_encapsize += nla_total_size(2);
399 }
400 } endfor_nexthops(fi);
401
402 /* all nexthops are packed in a nested attribute */
403 payload += nla_total_size((fi->fib_nhs * nhsize) +
404 nh_encapsize);
405
406 }
407
408 return payload;
409}
410
411void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
412 int dst_len, u32 tb_id, const struct nl_info *info,
413 unsigned int nlm_flags)
414{
415 struct sk_buff *skb;
416 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
417 int err = -ENOBUFS;
418
419 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
420 if (!skb)
421 goto errout;
422
423 err = fib_dump_info(skb, info->portid, seq, event, tb_id,
424 fa->fa_type, key, dst_len,
425 fa->fa_tos, fa->fa_info, nlm_flags);
426 if (err < 0) {
427 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
428 WARN_ON(err == -EMSGSIZE);
429 kfree_skb(skb);
430 goto errout;
431 }
432 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
433 info->nlh, GFP_KERNEL);
434 return;
435errout:
436 if (err < 0)
437 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
438}
439
440static int fib_detect_death(struct fib_info *fi, int order,
441 struct fib_info **last_resort, int *last_idx,
442 int dflt)
443{
444 struct neighbour *n;
445 int state = NUD_NONE;
446
447 n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
448 if (n) {
449 state = n->nud_state;
450 neigh_release(n);
451 } else {
452 return 0;
453 }
454 if (state == NUD_REACHABLE)
455 return 0;
456 if ((state & NUD_VALID) && order != dflt)
457 return 0;
458 if ((state & NUD_VALID) ||
459 (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
460 *last_resort = fi;
461 *last_idx = order;
462 }
463 return 1;
464}
465
466#ifdef CONFIG_IP_ROUTE_MULTIPATH
467
468static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
469 struct netlink_ext_ack *extack)
470{
471 int nhs = 0;
472
473 while (rtnh_ok(rtnh, remaining)) {
474 nhs++;
475 rtnh = rtnh_next(rtnh, &remaining);
476 }
477
478 /* leftover implies invalid nexthop configuration, discard it */
479 if (remaining > 0) {
480 NL_SET_ERR_MSG(extack,
481 "Invalid nexthop configuration - extra data after nexthops");
482 nhs = 0;
483 }
484
485 return nhs;
486}
487
488static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
489 int remaining, struct fib_config *cfg,
490 struct netlink_ext_ack *extack)
491{
492 int ret;
493
494 change_nexthops(fi) {
495 int attrlen;
496
497 if (!rtnh_ok(rtnh, remaining)) {
498 NL_SET_ERR_MSG(extack,
499 "Invalid nexthop configuration - extra data after nexthop");
500 return -EINVAL;
501 }
502
503 if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
504 NL_SET_ERR_MSG(extack,
505 "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
506 return -EINVAL;
507 }
508
509 nexthop_nh->nh_flags =
510 (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
511 nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
512 nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
513
514 attrlen = rtnh_attrlen(rtnh);
515 if (attrlen > 0) {
516 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
517
518 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
519 nexthop_nh->nh_gw = nla ? nla_get_in_addr(nla) : 0;
520#ifdef CONFIG_IP_ROUTE_CLASSID
521 nla = nla_find(attrs, attrlen, RTA_FLOW);
522 nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
523 if (nexthop_nh->nh_tclassid)
524 fi->fib_net->ipv4.fib_num_tclassid_users++;
525#endif
526 nla = nla_find(attrs, attrlen, RTA_ENCAP);
527 if (nla) {
528 struct lwtunnel_state *lwtstate;
529 struct nlattr *nla_entype;
530
531 nla_entype = nla_find(attrs, attrlen,
532 RTA_ENCAP_TYPE);
533 if (!nla_entype) {
534 NL_SET_BAD_ATTR(extack, nla);
535 NL_SET_ERR_MSG(extack,
536 "Encap type is missing");
537 goto err_inval;
538 }
539
540 ret = lwtunnel_build_state(nla_get_u16(
541 nla_entype),
542 nla, AF_INET, cfg,
543 &lwtstate, extack);
544 if (ret)
545 goto errout;
546 nexthop_nh->nh_lwtstate =
547 lwtstate_get(lwtstate);
548 }
549 }
550
551 rtnh = rtnh_next(rtnh, &remaining);
552 } endfor_nexthops(fi);
553
554 return 0;
555
556err_inval:
557 ret = -EINVAL;
558
559errout:
560 return ret;
561}
562
563static void fib_rebalance(struct fib_info *fi)
564{
565 int total;
566 int w;
567 struct in_device *in_dev;
568
569 if (fi->fib_nhs < 2)
570 return;
571
572 total = 0;
573 for_nexthops(fi) {
574 if (nh->nh_flags & RTNH_F_DEAD)
575 continue;
576
577 in_dev = __in_dev_get_rtnl(nh->nh_dev);
578
579 if (in_dev &&
580 IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
581 nh->nh_flags & RTNH_F_LINKDOWN)
582 continue;
583
584 total += nh->nh_weight;
585 } endfor_nexthops(fi);
586
587 w = 0;
588 change_nexthops(fi) {
589 int upper_bound;
590
591 in_dev = __in_dev_get_rtnl(nexthop_nh->nh_dev);
592
593 if (nexthop_nh->nh_flags & RTNH_F_DEAD) {
594 upper_bound = -1;
595 } else if (in_dev &&
596 IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
597 nexthop_nh->nh_flags & RTNH_F_LINKDOWN) {
598 upper_bound = -1;
599 } else {
600 w += nexthop_nh->nh_weight;
601 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
602 total) - 1;
603 }
604
605 atomic_set(&nexthop_nh->nh_upper_bound, upper_bound);
606 } endfor_nexthops(fi);
607}
608#else /* CONFIG_IP_ROUTE_MULTIPATH */
609
610#define fib_rebalance(fi) do { } while (0)
611
612#endif /* CONFIG_IP_ROUTE_MULTIPATH */
613
614static int fib_encap_match(u16 encap_type,
615 struct nlattr *encap,
616 const struct fib_nh *nh,
617 const struct fib_config *cfg,
618 struct netlink_ext_ack *extack)
619{
620 struct lwtunnel_state *lwtstate;
621 int ret, result = 0;
622
623 if (encap_type == LWTUNNEL_ENCAP_NONE)
624 return 0;
625
626 ret = lwtunnel_build_state(encap_type, encap, AF_INET,
627 cfg, &lwtstate, extack);
628 if (!ret) {
629 result = lwtunnel_cmp_encap(lwtstate, nh->nh_lwtstate);
630 lwtstate_free(lwtstate);
631 }
632
633 return result;
634}
635
636int fib_nh_match(struct fib_config *cfg, struct fib_info *fi,
637 struct netlink_ext_ack *extack)
638{
639#ifdef CONFIG_IP_ROUTE_MULTIPATH
640 struct rtnexthop *rtnh;
641 int remaining;
642#endif
643
644 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
645 return 1;
646
647 if (cfg->fc_oif || cfg->fc_gw) {
648 if (cfg->fc_encap) {
649 if (fib_encap_match(cfg->fc_encap_type, cfg->fc_encap,
650 fi->fib_nh, cfg, extack))
651 return 1;
652 }
653#ifdef CONFIG_IP_ROUTE_CLASSID
654 if (cfg->fc_flow &&
655 cfg->fc_flow != fi->fib_nh->nh_tclassid)
656 return 1;
657#endif
658 if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
659 (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
660 return 0;
661 return 1;
662 }
663
664#ifdef CONFIG_IP_ROUTE_MULTIPATH
665 if (!cfg->fc_mp)
666 return 0;
667
668 rtnh = cfg->fc_mp;
669 remaining = cfg->fc_mp_len;
670
671 for_nexthops(fi) {
672 int attrlen;
673
674 if (!rtnh_ok(rtnh, remaining))
675 return -EINVAL;
676
677 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
678 return 1;
679
680 attrlen = rtnh_attrlen(rtnh);
681 if (attrlen > 0) {
682 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
683
684 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
685 if (nla && nla_get_in_addr(nla) != nh->nh_gw)
686 return 1;
687#ifdef CONFIG_IP_ROUTE_CLASSID
688 nla = nla_find(attrs, attrlen, RTA_FLOW);
689 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
690 return 1;
691#endif
692 }
693
694 rtnh = rtnh_next(rtnh, &remaining);
695 } endfor_nexthops(fi);
696#endif
697 return 0;
698}
699
700bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
701{
702 struct nlattr *nla;
703 int remaining;
704
705 if (!cfg->fc_mx)
706 return true;
707
708 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
709 int type = nla_type(nla);
710 u32 fi_val, val;
711
712 if (!type)
713 continue;
714 if (type > RTAX_MAX)
715 return false;
716
717 if (type == RTAX_CC_ALGO) {
718 char tmp[TCP_CA_NAME_MAX];
719 bool ecn_ca = false;
720
721 nla_strlcpy(tmp, nla, sizeof(tmp));
722 val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
723 } else {
724 if (nla_len(nla) != sizeof(u32))
725 return false;
726 val = nla_get_u32(nla);
727 }
728
729 fi_val = fi->fib_metrics->metrics[type - 1];
730 if (type == RTAX_FEATURES)
731 fi_val &= ~DST_FEATURE_ECN_CA;
732
733 if (fi_val != val)
734 return false;
735 }
736
737 return true;
738}
739
740
741/*
742 * Picture
743 * -------
744 *
745 * Semantics of nexthop is very messy by historical reasons.
746 * We have to take into account, that:
747 * a) gateway can be actually local interface address,
748 * so that gatewayed route is direct.
749 * b) gateway must be on-link address, possibly
750 * described not by an ifaddr, but also by a direct route.
751 * c) If both gateway and interface are specified, they should not
752 * contradict.
753 * d) If we use tunnel routes, gateway could be not on-link.
754 *
755 * Attempt to reconcile all of these (alas, self-contradictory) conditions
756 * results in pretty ugly and hairy code with obscure logic.
757 *
758 * I chose to generalized it instead, so that the size
759 * of code does not increase practically, but it becomes
760 * much more general.
761 * Every prefix is assigned a "scope" value: "host" is local address,
762 * "link" is direct route,
763 * [ ... "site" ... "interior" ... ]
764 * and "universe" is true gateway route with global meaning.
765 *
766 * Every prefix refers to a set of "nexthop"s (gw, oif),
767 * where gw must have narrower scope. This recursion stops
768 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
769 * which means that gw is forced to be on link.
770 *
771 * Code is still hairy, but now it is apparently logically
772 * consistent and very flexible. F.e. as by-product it allows
773 * to co-exists in peace independent exterior and interior
774 * routing processes.
775 *
776 * Normally it looks as following.
777 *
778 * {universe prefix} -> (gw, oif) [scope link]
779 * |
780 * |-> {link prefix} -> (gw, oif) [scope local]
781 * |
782 * |-> {local prefix} (terminal node)
783 */
784static int fib_check_nh(struct fib_config *cfg, struct fib_nh *nh,
785 struct netlink_ext_ack *extack)
786{
787 int err = 0;
788 struct net *net;
789 struct net_device *dev;
790
791 net = cfg->fc_nlinfo.nl_net;
792 if (nh->nh_gw) {
793 struct fib_result res;
794
795 if (nh->nh_flags & RTNH_F_ONLINK) {
796 unsigned int addr_type;
797
798 if (cfg->fc_scope >= RT_SCOPE_LINK) {
799 NL_SET_ERR_MSG(extack,
800 "Nexthop has invalid scope");
801 return -EINVAL;
802 }
803 dev = __dev_get_by_index(net, nh->nh_oif);
804 if (!dev)
805 return -ENODEV;
806 if (!(dev->flags & IFF_UP)) {
807 NL_SET_ERR_MSG(extack,
808 "Nexthop device is not up");
809 return -ENETDOWN;
810 }
811 addr_type = inet_addr_type_dev_table(net, dev, nh->nh_gw);
812 if (addr_type != RTN_UNICAST) {
813 NL_SET_ERR_MSG(extack,
814 "Nexthop has invalid gateway");
815 return -EINVAL;
816 }
817 if (!netif_carrier_ok(dev))
818 nh->nh_flags |= RTNH_F_LINKDOWN;
819 nh->nh_dev = dev;
820 dev_hold(dev);
821 nh->nh_scope = RT_SCOPE_LINK;
822 return 0;
823 }
824 rcu_read_lock();
825 {
826 struct fib_table *tbl = NULL;
827 struct flowi4 fl4 = {
828 .daddr = nh->nh_gw,
829 .flowi4_scope = cfg->fc_scope + 1,
830 .flowi4_oif = nh->nh_oif,
831 .flowi4_iif = LOOPBACK_IFINDEX,
832 };
833
834 /* It is not necessary, but requires a bit of thinking */
835 if (fl4.flowi4_scope < RT_SCOPE_LINK)
836 fl4.flowi4_scope = RT_SCOPE_LINK;
837
838 if (cfg->fc_table)
839 tbl = fib_get_table(net, cfg->fc_table);
840
841 if (tbl)
842 err = fib_table_lookup(tbl, &fl4, &res,
843 FIB_LOOKUP_IGNORE_LINKSTATE |
844 FIB_LOOKUP_NOREF);
845
846 /* on error or if no table given do full lookup. This
847 * is needed for example when nexthops are in the local
848 * table rather than the given table
849 */
850 if (!tbl || err) {
851 err = fib_lookup(net, &fl4, &res,
852 FIB_LOOKUP_IGNORE_LINKSTATE);
853 }
854
855 if (err) {
856 NL_SET_ERR_MSG(extack,
857 "Nexthop has invalid gateway");
858 rcu_read_unlock();
859 return err;
860 }
861 }
862 err = -EINVAL;
863 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
864 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
865 goto out;
866 }
867 nh->nh_scope = res.scope;
868 nh->nh_oif = FIB_RES_OIF(res);
869 nh->nh_dev = dev = FIB_RES_DEV(res);
870 if (!dev) {
871 NL_SET_ERR_MSG(extack,
872 "No egress device for nexthop gateway");
873 goto out;
874 }
875 dev_hold(dev);
876 if (!netif_carrier_ok(dev))
877 nh->nh_flags |= RTNH_F_LINKDOWN;
878 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
879 } else {
880 struct in_device *in_dev;
881
882 if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
883 NL_SET_ERR_MSG(extack,
884 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
885 return -EINVAL;
886 }
887 rcu_read_lock();
888 err = -ENODEV;
889 in_dev = inetdev_by_index(net, nh->nh_oif);
890 if (!in_dev)
891 goto out;
892 err = -ENETDOWN;
893 if (!(in_dev->dev->flags & IFF_UP)) {
894 NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
895 goto out;
896 }
897 nh->nh_dev = in_dev->dev;
898 dev_hold(nh->nh_dev);
899 nh->nh_scope = RT_SCOPE_HOST;
900 if (!netif_carrier_ok(nh->nh_dev))
901 nh->nh_flags |= RTNH_F_LINKDOWN;
902 err = 0;
903 }
904out:
905 rcu_read_unlock();
906 return err;
907}
908
909static inline unsigned int fib_laddr_hashfn(__be32 val)
910{
911 unsigned int mask = (fib_info_hash_size - 1);
912
913 return ((__force u32)val ^
914 ((__force u32)val >> 7) ^
915 ((__force u32)val >> 14)) & mask;
916}
917
918static struct hlist_head *fib_info_hash_alloc(int bytes)
919{
920 if (bytes <= PAGE_SIZE)
921 return kzalloc(bytes, GFP_KERNEL);
922 else
923 return (struct hlist_head *)
924 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
925 get_order(bytes));
926}
927
928static void fib_info_hash_free(struct hlist_head *hash, int bytes)
929{
930 if (!hash)
931 return;
932
933 if (bytes <= PAGE_SIZE)
934 kfree(hash);
935 else
936 free_pages((unsigned long) hash, get_order(bytes));
937}
938
939static void fib_info_hash_move(struct hlist_head *new_info_hash,
940 struct hlist_head *new_laddrhash,
941 unsigned int new_size)
942{
943 struct hlist_head *old_info_hash, *old_laddrhash;
944 unsigned int old_size = fib_info_hash_size;
945 unsigned int i, bytes;
946
947 spin_lock_bh(&fib_info_lock);
948 old_info_hash = fib_info_hash;
949 old_laddrhash = fib_info_laddrhash;
950 fib_info_hash_size = new_size;
951
952 for (i = 0; i < old_size; i++) {
953 struct hlist_head *head = &fib_info_hash[i];
954 struct hlist_node *n;
955 struct fib_info *fi;
956
957 hlist_for_each_entry_safe(fi, n, head, fib_hash) {
958 struct hlist_head *dest;
959 unsigned int new_hash;
960
961 new_hash = fib_info_hashfn(fi);
962 dest = &new_info_hash[new_hash];
963 hlist_add_head(&fi->fib_hash, dest);
964 }
965 }
966 fib_info_hash = new_info_hash;
967
968 for (i = 0; i < old_size; i++) {
969 struct hlist_head *lhead = &fib_info_laddrhash[i];
970 struct hlist_node *n;
971 struct fib_info *fi;
972
973 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
974 struct hlist_head *ldest;
975 unsigned int new_hash;
976
977 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
978 ldest = &new_laddrhash[new_hash];
979 hlist_add_head(&fi->fib_lhash, ldest);
980 }
981 }
982 fib_info_laddrhash = new_laddrhash;
983
984 spin_unlock_bh(&fib_info_lock);
985
986 bytes = old_size * sizeof(struct hlist_head *);
987 fib_info_hash_free(old_info_hash, bytes);
988 fib_info_hash_free(old_laddrhash, bytes);
989}
990
991__be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
992{
993 nh->nh_saddr = inet_select_addr(nh->nh_dev,
994 nh->nh_gw,
995 nh->nh_parent->fib_scope);
996 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
997
998 return nh->nh_saddr;
999}
1000
1001static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1002{
1003 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1004 fib_prefsrc != cfg->fc_dst) {
1005 u32 tb_id = cfg->fc_table;
1006 int rc;
1007
1008 if (tb_id == RT_TABLE_MAIN)
1009 tb_id = RT_TABLE_LOCAL;
1010
1011 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1012 fib_prefsrc, tb_id);
1013
1014 if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1015 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1016 fib_prefsrc, RT_TABLE_LOCAL);
1017 }
1018
1019 if (rc != RTN_LOCAL)
1020 return false;
1021 }
1022 return true;
1023}
1024
1025static int
1026fib_convert_metrics(struct fib_info *fi, const struct fib_config *cfg)
1027{
1028 return ip_metrics_convert(fi->fib_net, cfg->fc_mx, cfg->fc_mx_len,
1029 fi->fib_metrics->metrics);
1030}
1031
1032struct fib_info *fib_create_info(struct fib_config *cfg,
1033 struct netlink_ext_ack *extack)
1034{
1035 int err;
1036 struct fib_info *fi = NULL;
1037 struct fib_info *ofi;
1038 int nhs = 1;
1039 struct net *net = cfg->fc_nlinfo.nl_net;
1040
1041 if (cfg->fc_type > RTN_MAX)
1042 goto err_inval;
1043
1044 /* Fast check to catch the most weird cases */
1045 if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1046 NL_SET_ERR_MSG(extack, "Invalid scope");
1047 goto err_inval;
1048 }
1049
1050 if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1051 NL_SET_ERR_MSG(extack,
1052 "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
1053 goto err_inval;
1054 }
1055
1056#ifdef CONFIG_IP_ROUTE_MULTIPATH
1057 if (cfg->fc_mp) {
1058 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
1059 if (nhs == 0)
1060 goto err_inval;
1061 }
1062#endif
1063
1064 err = -ENOBUFS;
1065 if (fib_info_cnt >= fib_info_hash_size) {
1066 unsigned int new_size = fib_info_hash_size << 1;
1067 struct hlist_head *new_info_hash;
1068 struct hlist_head *new_laddrhash;
1069 unsigned int bytes;
1070
1071 if (!new_size)
1072 new_size = 16;
1073 bytes = new_size * sizeof(struct hlist_head *);
1074 new_info_hash = fib_info_hash_alloc(bytes);
1075 new_laddrhash = fib_info_hash_alloc(bytes);
1076 if (!new_info_hash || !new_laddrhash) {
1077 fib_info_hash_free(new_info_hash, bytes);
1078 fib_info_hash_free(new_laddrhash, bytes);
1079 } else
1080 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
1081
1082 if (!fib_info_hash_size)
1083 goto failure;
1084 }
1085
1086 fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
1087 if (!fi)
1088 goto failure;
1089 if (cfg->fc_mx) {
1090 fi->fib_metrics = kzalloc(sizeof(*fi->fib_metrics), GFP_KERNEL);
1091 if (unlikely(!fi->fib_metrics)) {
1092 kfree(fi);
1093 return ERR_PTR(err);
1094 }
1095 refcount_set(&fi->fib_metrics->refcnt, 1);
1096 } else {
1097 fi->fib_metrics = (struct dst_metrics *)&dst_default_metrics;
1098 }
1099 fib_info_cnt++;
1100 fi->fib_net = net;
1101 fi->fib_protocol = cfg->fc_protocol;
1102 fi->fib_scope = cfg->fc_scope;
1103 fi->fib_flags = cfg->fc_flags;
1104 fi->fib_priority = cfg->fc_priority;
1105 fi->fib_prefsrc = cfg->fc_prefsrc;
1106 fi->fib_type = cfg->fc_type;
1107 fi->fib_tb_id = cfg->fc_table;
1108
1109 fi->fib_nhs = nhs;
1110 change_nexthops(fi) {
1111 nexthop_nh->nh_parent = fi;
1112 nexthop_nh->nh_pcpu_rth_output = alloc_percpu(struct rtable __rcu *);
1113 if (!nexthop_nh->nh_pcpu_rth_output)
1114 goto failure;
1115 } endfor_nexthops(fi)
1116
1117 err = fib_convert_metrics(fi, cfg);
1118 if (err)
1119 goto failure;
1120
1121 if (cfg->fc_mp) {
1122#ifdef CONFIG_IP_ROUTE_MULTIPATH
1123 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg, extack);
1124 if (err != 0)
1125 goto failure;
1126 if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif) {
1127 NL_SET_ERR_MSG(extack,
1128 "Nexthop device index does not match RTA_OIF");
1129 goto err_inval;
1130 }
1131 if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw) {
1132 NL_SET_ERR_MSG(extack,
1133 "Nexthop gateway does not match RTA_GATEWAY");
1134 goto err_inval;
1135 }
1136#ifdef CONFIG_IP_ROUTE_CLASSID
1137 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) {
1138 NL_SET_ERR_MSG(extack,
1139 "Nexthop class id does not match RTA_FLOW");
1140 goto err_inval;
1141 }
1142#endif
1143#else
1144 NL_SET_ERR_MSG(extack,
1145 "Multipath support not enabled in kernel");
1146 goto err_inval;
1147#endif
1148 } else {
1149 struct fib_nh *nh = fi->fib_nh;
1150
1151 if (cfg->fc_encap) {
1152 struct lwtunnel_state *lwtstate;
1153
1154 if (cfg->fc_encap_type == LWTUNNEL_ENCAP_NONE) {
1155 NL_SET_ERR_MSG(extack,
1156 "LWT encap type not specified");
1157 goto err_inval;
1158 }
1159 err = lwtunnel_build_state(cfg->fc_encap_type,
1160 cfg->fc_encap, AF_INET, cfg,
1161 &lwtstate, extack);
1162 if (err)
1163 goto failure;
1164
1165 nh->nh_lwtstate = lwtstate_get(lwtstate);
1166 }
1167 nh->nh_oif = cfg->fc_oif;
1168 nh->nh_gw = cfg->fc_gw;
1169 nh->nh_flags = cfg->fc_flags;
1170#ifdef CONFIG_IP_ROUTE_CLASSID
1171 nh->nh_tclassid = cfg->fc_flow;
1172 if (nh->nh_tclassid)
1173 fi->fib_net->ipv4.fib_num_tclassid_users++;
1174#endif
1175#ifdef CONFIG_IP_ROUTE_MULTIPATH
1176 nh->nh_weight = 1;
1177#endif
1178 }
1179
1180 if (fib_props[cfg->fc_type].error) {
1181 if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp) {
1182 NL_SET_ERR_MSG(extack,
1183 "Gateway, device and multipath can not be specified for this route type");
1184 goto err_inval;
1185 }
1186 goto link_it;
1187 } else {
1188 switch (cfg->fc_type) {
1189 case RTN_UNICAST:
1190 case RTN_LOCAL:
1191 case RTN_BROADCAST:
1192 case RTN_ANYCAST:
1193 case RTN_MULTICAST:
1194 break;
1195 default:
1196 NL_SET_ERR_MSG(extack, "Invalid route type");
1197 goto err_inval;
1198 }
1199 }
1200
1201 if (cfg->fc_scope > RT_SCOPE_HOST) {
1202 NL_SET_ERR_MSG(extack, "Invalid scope");
1203 goto err_inval;
1204 }
1205
1206 if (cfg->fc_scope == RT_SCOPE_HOST) {
1207 struct fib_nh *nh = fi->fib_nh;
1208
1209 /* Local address is added. */
1210 if (nhs != 1) {
1211 NL_SET_ERR_MSG(extack,
1212 "Route with host scope can not have multiple nexthops");
1213 goto err_inval;
1214 }
1215 if (nh->nh_gw) {
1216 NL_SET_ERR_MSG(extack,
1217 "Route with host scope can not have a gateway");
1218 goto err_inval;
1219 }
1220 nh->nh_scope = RT_SCOPE_NOWHERE;
1221 nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
1222 err = -ENODEV;
1223 if (!nh->nh_dev)
1224 goto failure;
1225 } else {
1226 int linkdown = 0;
1227
1228 change_nexthops(fi) {
1229 err = fib_check_nh(cfg, nexthop_nh, extack);
1230 if (err != 0)
1231 goto failure;
1232 if (nexthop_nh->nh_flags & RTNH_F_LINKDOWN)
1233 linkdown++;
1234 } endfor_nexthops(fi)
1235 if (linkdown == fi->fib_nhs)
1236 fi->fib_flags |= RTNH_F_LINKDOWN;
1237 }
1238
1239 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1240 NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
1241 goto err_inval;
1242 }
1243
1244 change_nexthops(fi) {
1245 fib_info_update_nh_saddr(net, nexthop_nh);
1246 } endfor_nexthops(fi)
1247
1248 fib_rebalance(fi);
1249
1250link_it:
1251 ofi = fib_find_info(fi);
1252 if (ofi) {
1253 fi->fib_dead = 1;
1254 free_fib_info(fi);
1255 ofi->fib_treeref++;
1256 return ofi;
1257 }
1258
1259 fi->fib_treeref++;
1260 refcount_set(&fi->fib_clntref, 1);
1261 spin_lock_bh(&fib_info_lock);
1262 hlist_add_head(&fi->fib_hash,
1263 &fib_info_hash[fib_info_hashfn(fi)]);
1264 if (fi->fib_prefsrc) {
1265 struct hlist_head *head;
1266
1267 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
1268 hlist_add_head(&fi->fib_lhash, head);
1269 }
1270 change_nexthops(fi) {
1271 struct hlist_head *head;
1272 unsigned int hash;
1273
1274 if (!nexthop_nh->nh_dev)
1275 continue;
1276 hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
1277 head = &fib_info_devhash[hash];
1278 hlist_add_head(&nexthop_nh->nh_hash, head);
1279 } endfor_nexthops(fi)
1280 spin_unlock_bh(&fib_info_lock);
1281 return fi;
1282
1283err_inval:
1284 err = -EINVAL;
1285
1286failure:
1287 if (fi) {
1288 fi->fib_dead = 1;
1289 free_fib_info(fi);
1290 }
1291
1292 return ERR_PTR(err);
1293}
1294
1295int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
1296 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
1297 struct fib_info *fi, unsigned int flags)
1298{
1299 struct nlmsghdr *nlh;
1300 struct rtmsg *rtm;
1301
1302 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1303 if (!nlh)
1304 return -EMSGSIZE;
1305
1306 rtm = nlmsg_data(nlh);
1307 rtm->rtm_family = AF_INET;
1308 rtm->rtm_dst_len = dst_len;
1309 rtm->rtm_src_len = 0;
1310 rtm->rtm_tos = tos;
1311 if (tb_id < 256)
1312 rtm->rtm_table = tb_id;
1313 else
1314 rtm->rtm_table = RT_TABLE_COMPAT;
1315 if (nla_put_u32(skb, RTA_TABLE, tb_id))
1316 goto nla_put_failure;
1317 rtm->rtm_type = type;
1318 rtm->rtm_flags = fi->fib_flags;
1319 rtm->rtm_scope = fi->fib_scope;
1320 rtm->rtm_protocol = fi->fib_protocol;
1321
1322 if (rtm->rtm_dst_len &&
1323 nla_put_in_addr(skb, RTA_DST, dst))
1324 goto nla_put_failure;
1325 if (fi->fib_priority &&
1326 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1327 goto nla_put_failure;
1328 if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
1329 goto nla_put_failure;
1330
1331 if (fi->fib_prefsrc &&
1332 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
1333 goto nla_put_failure;
1334 if (fi->fib_nhs == 1) {
1335 if (fi->fib_nh->nh_gw &&
1336 nla_put_in_addr(skb, RTA_GATEWAY, fi->fib_nh->nh_gw))
1337 goto nla_put_failure;
1338 if (fi->fib_nh->nh_oif &&
1339 nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
1340 goto nla_put_failure;
1341 if (fi->fib_nh->nh_flags & RTNH_F_LINKDOWN) {
1342 struct in_device *in_dev;
1343
1344 rcu_read_lock();
1345 in_dev = __in_dev_get_rcu(fi->fib_nh->nh_dev);
1346 if (in_dev &&
1347 IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
1348 rtm->rtm_flags |= RTNH_F_DEAD;
1349 rcu_read_unlock();
1350 }
1351 if (fi->fib_nh->nh_flags & RTNH_F_OFFLOAD)
1352 rtm->rtm_flags |= RTNH_F_OFFLOAD;
1353#ifdef CONFIG_IP_ROUTE_CLASSID
1354 if (fi->fib_nh[0].nh_tclassid &&
1355 nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid))
1356 goto nla_put_failure;
1357#endif
1358 if (fi->fib_nh->nh_lwtstate &&
1359 lwtunnel_fill_encap(skb, fi->fib_nh->nh_lwtstate) < 0)
1360 goto nla_put_failure;
1361 }
1362#ifdef CONFIG_IP_ROUTE_MULTIPATH
1363 if (fi->fib_nhs > 1) {
1364 struct rtnexthop *rtnh;
1365 struct nlattr *mp;
1366
1367 mp = nla_nest_start(skb, RTA_MULTIPATH);
1368 if (!mp)
1369 goto nla_put_failure;
1370
1371 for_nexthops(fi) {
1372 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1373 if (!rtnh)
1374 goto nla_put_failure;
1375
1376 rtnh->rtnh_flags = nh->nh_flags & 0xFF;
1377 if (nh->nh_flags & RTNH_F_LINKDOWN) {
1378 struct in_device *in_dev;
1379
1380 rcu_read_lock();
1381 in_dev = __in_dev_get_rcu(nh->nh_dev);
1382 if (in_dev &&
1383 IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
1384 rtnh->rtnh_flags |= RTNH_F_DEAD;
1385 rcu_read_unlock();
1386 }
1387 rtnh->rtnh_hops = nh->nh_weight - 1;
1388 rtnh->rtnh_ifindex = nh->nh_oif;
1389
1390 if (nh->nh_gw &&
1391 nla_put_in_addr(skb, RTA_GATEWAY, nh->nh_gw))
1392 goto nla_put_failure;
1393#ifdef CONFIG_IP_ROUTE_CLASSID
1394 if (nh->nh_tclassid &&
1395 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1396 goto nla_put_failure;
1397#endif
1398 if (nh->nh_lwtstate &&
1399 lwtunnel_fill_encap(skb, nh->nh_lwtstate) < 0)
1400 goto nla_put_failure;
1401
1402 /* length of rtnetlink header + attributes */
1403 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
1404 } endfor_nexthops(fi);
1405
1406 nla_nest_end(skb, mp);
1407 }
1408#endif
1409 nlmsg_end(skb, nlh);
1410 return 0;
1411
1412nla_put_failure:
1413 nlmsg_cancel(skb, nlh);
1414 return -EMSGSIZE;
1415}
1416
1417/*
1418 * Update FIB if:
1419 * - local address disappeared -> we must delete all the entries
1420 * referring to it.
1421 * - device went down -> we must shutdown all nexthops going via it.
1422 */
1423int fib_sync_down_addr(struct net_device *dev, __be32 local)
1424{
1425 int ret = 0;
1426 unsigned int hash = fib_laddr_hashfn(local);
1427 struct hlist_head *head = &fib_info_laddrhash[hash];
1428 int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
1429 struct net *net = dev_net(dev);
1430 struct fib_info *fi;
1431
1432 if (!fib_info_laddrhash || local == 0)
1433 return 0;
1434
1435 hlist_for_each_entry(fi, head, fib_lhash) {
1436 if (!net_eq(fi->fib_net, net) ||
1437 fi->fib_tb_id != tb_id)
1438 continue;
1439 if (fi->fib_prefsrc == local) {
1440 fi->fib_flags |= RTNH_F_DEAD;
1441 ret++;
1442 }
1443 }
1444 return ret;
1445}
1446
1447static int call_fib_nh_notifiers(struct fib_nh *fib_nh,
1448 enum fib_event_type event_type)
1449{
1450 struct in_device *in_dev = __in_dev_get_rtnl(fib_nh->nh_dev);
1451 struct fib_nh_notifier_info info = {
1452 .fib_nh = fib_nh,
1453 };
1454
1455 switch (event_type) {
1456 case FIB_EVENT_NH_ADD:
1457 if (fib_nh->nh_flags & RTNH_F_DEAD)
1458 break;
1459 if (IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
1460 fib_nh->nh_flags & RTNH_F_LINKDOWN)
1461 break;
1462 return call_fib4_notifiers(dev_net(fib_nh->nh_dev), event_type,
1463 &info.info);
1464 case FIB_EVENT_NH_DEL:
1465 if ((in_dev && IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
1466 fib_nh->nh_flags & RTNH_F_LINKDOWN) ||
1467 (fib_nh->nh_flags & RTNH_F_DEAD))
1468 return call_fib4_notifiers(dev_net(fib_nh->nh_dev),
1469 event_type, &info.info);
1470 default:
1471 break;
1472 }
1473
1474 return NOTIFY_DONE;
1475}
1476
1477/* Update the PMTU of exceptions when:
1478 * - the new MTU of the first hop becomes smaller than the PMTU
1479 * - the old MTU was the same as the PMTU, and it limited discovery of
1480 * larger MTUs on the path. With that limit raised, we can now
1481 * discover larger MTUs
1482 * A special case is locked exceptions, for which the PMTU is smaller
1483 * than the minimal accepted PMTU:
1484 * - if the new MTU is greater than the PMTU, don't make any change
1485 * - otherwise, unlock and set PMTU
1486 */
1487static void nh_update_mtu(struct fib_nh *nh, u32 new, u32 orig)
1488{
1489 struct fnhe_hash_bucket *bucket;
1490 int i;
1491
1492 bucket = rcu_dereference_protected(nh->nh_exceptions, 1);
1493 if (!bucket)
1494 return;
1495
1496 for (i = 0; i < FNHE_HASH_SIZE; i++) {
1497 struct fib_nh_exception *fnhe;
1498
1499 for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1500 fnhe;
1501 fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1502 if (fnhe->fnhe_mtu_locked) {
1503 if (new <= fnhe->fnhe_pmtu) {
1504 fnhe->fnhe_pmtu = new;
1505 fnhe->fnhe_mtu_locked = false;
1506 }
1507 } else if (new < fnhe->fnhe_pmtu ||
1508 orig == fnhe->fnhe_pmtu) {
1509 fnhe->fnhe_pmtu = new;
1510 }
1511 }
1512 }
1513}
1514
1515void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1516{
1517 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1518 struct hlist_head *head = &fib_info_devhash[hash];
1519 struct fib_nh *nh;
1520
1521 hlist_for_each_entry(nh, head, nh_hash) {
1522 if (nh->nh_dev == dev)
1523 nh_update_mtu(nh, dev->mtu, orig_mtu);
1524 }
1525}
1526
1527/* Event force Flags Description
1528 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
1529 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
1530 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
1531 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
1532 */
1533int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
1534{
1535 int ret = 0;
1536 int scope = RT_SCOPE_NOWHERE;
1537 struct fib_info *prev_fi = NULL;
1538 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1539 struct hlist_head *head = &fib_info_devhash[hash];
1540 struct fib_nh *nh;
1541
1542 if (force)
1543 scope = -1;
1544
1545 hlist_for_each_entry(nh, head, nh_hash) {
1546 struct fib_info *fi = nh->nh_parent;
1547 int dead;
1548
1549 BUG_ON(!fi->fib_nhs);
1550 if (nh->nh_dev != dev || fi == prev_fi)
1551 continue;
1552 prev_fi = fi;
1553 dead = 0;
1554 change_nexthops(fi) {
1555 if (nexthop_nh->nh_flags & RTNH_F_DEAD)
1556 dead++;
1557 else if (nexthop_nh->nh_dev == dev &&
1558 nexthop_nh->nh_scope != scope) {
1559 switch (event) {
1560 case NETDEV_DOWN:
1561 case NETDEV_UNREGISTER:
1562 nexthop_nh->nh_flags |= RTNH_F_DEAD;
1563 /* fall through */
1564 case NETDEV_CHANGE:
1565 nexthop_nh->nh_flags |= RTNH_F_LINKDOWN;
1566 break;
1567 }
1568 call_fib_nh_notifiers(nexthop_nh,
1569 FIB_EVENT_NH_DEL);
1570 dead++;
1571 }
1572#ifdef CONFIG_IP_ROUTE_MULTIPATH
1573 if (event == NETDEV_UNREGISTER &&
1574 nexthop_nh->nh_dev == dev) {
1575 dead = fi->fib_nhs;
1576 break;
1577 }
1578#endif
1579 } endfor_nexthops(fi)
1580 if (dead == fi->fib_nhs) {
1581 switch (event) {
1582 case NETDEV_DOWN:
1583 case NETDEV_UNREGISTER:
1584 fi->fib_flags |= RTNH_F_DEAD;
1585 /* fall through */
1586 case NETDEV_CHANGE:
1587 fi->fib_flags |= RTNH_F_LINKDOWN;
1588 break;
1589 }
1590 ret++;
1591 }
1592
1593 fib_rebalance(fi);
1594 }
1595
1596 return ret;
1597}
1598
1599/* Must be invoked inside of an RCU protected region. */
1600static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
1601{
1602 struct fib_info *fi = NULL, *last_resort = NULL;
1603 struct hlist_head *fa_head = res->fa_head;
1604 struct fib_table *tb = res->table;
1605 u8 slen = 32 - res->prefixlen;
1606 int order = -1, last_idx = -1;
1607 struct fib_alias *fa, *fa1 = NULL;
1608 u32 last_prio = res->fi->fib_priority;
1609 u8 last_tos = 0;
1610
1611 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
1612 struct fib_info *next_fi = fa->fa_info;
1613
1614 if (fa->fa_slen != slen)
1615 continue;
1616 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1617 continue;
1618 if (fa->tb_id != tb->tb_id)
1619 continue;
1620 if (next_fi->fib_priority > last_prio &&
1621 fa->fa_tos == last_tos) {
1622 if (last_tos)
1623 continue;
1624 break;
1625 }
1626 if (next_fi->fib_flags & RTNH_F_DEAD)
1627 continue;
1628 last_tos = fa->fa_tos;
1629 last_prio = next_fi->fib_priority;
1630
1631 if (next_fi->fib_scope != res->scope ||
1632 fa->fa_type != RTN_UNICAST)
1633 continue;
1634 if (!next_fi->fib_nh[0].nh_gw ||
1635 next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
1636 continue;
1637
1638 fib_alias_accessed(fa);
1639
1640 if (!fi) {
1641 if (next_fi != res->fi)
1642 break;
1643 fa1 = fa;
1644 } else if (!fib_detect_death(fi, order, &last_resort,
1645 &last_idx, fa1->fa_default)) {
1646 fib_result_assign(res, fi);
1647 fa1->fa_default = order;
1648 goto out;
1649 }
1650 fi = next_fi;
1651 order++;
1652 }
1653
1654 if (order <= 0 || !fi) {
1655 if (fa1)
1656 fa1->fa_default = -1;
1657 goto out;
1658 }
1659
1660 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
1661 fa1->fa_default)) {
1662 fib_result_assign(res, fi);
1663 fa1->fa_default = order;
1664 goto out;
1665 }
1666
1667 if (last_idx >= 0)
1668 fib_result_assign(res, last_resort);
1669 fa1->fa_default = last_idx;
1670out:
1671 return;
1672}
1673
1674/*
1675 * Dead device goes up. We wake up dead nexthops.
1676 * It takes sense only on multipath routes.
1677 */
1678int fib_sync_up(struct net_device *dev, unsigned int nh_flags)
1679{
1680 struct fib_info *prev_fi;
1681 unsigned int hash;
1682 struct hlist_head *head;
1683 struct fib_nh *nh;
1684 int ret;
1685
1686 if (!(dev->flags & IFF_UP))
1687 return 0;
1688
1689 if (nh_flags & RTNH_F_DEAD) {
1690 unsigned int flags = dev_get_flags(dev);
1691
1692 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1693 nh_flags |= RTNH_F_LINKDOWN;
1694 }
1695
1696 prev_fi = NULL;
1697 hash = fib_devindex_hashfn(dev->ifindex);
1698 head = &fib_info_devhash[hash];
1699 ret = 0;
1700
1701 hlist_for_each_entry(nh, head, nh_hash) {
1702 struct fib_info *fi = nh->nh_parent;
1703 int alive;
1704
1705 BUG_ON(!fi->fib_nhs);
1706 if (nh->nh_dev != dev || fi == prev_fi)
1707 continue;
1708
1709 prev_fi = fi;
1710 alive = 0;
1711 change_nexthops(fi) {
1712 if (!(nexthop_nh->nh_flags & nh_flags)) {
1713 alive++;
1714 continue;
1715 }
1716 if (!nexthop_nh->nh_dev ||
1717 !(nexthop_nh->nh_dev->flags & IFF_UP))
1718 continue;
1719 if (nexthop_nh->nh_dev != dev ||
1720 !__in_dev_get_rtnl(dev))
1721 continue;
1722 alive++;
1723 nexthop_nh->nh_flags &= ~nh_flags;
1724 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
1725 } endfor_nexthops(fi)
1726
1727 if (alive > 0) {
1728 fi->fib_flags &= ~nh_flags;
1729 ret++;
1730 }
1731
1732 fib_rebalance(fi);
1733 }
1734
1735 return ret;
1736}
1737
1738#ifdef CONFIG_IP_ROUTE_MULTIPATH
1739static bool fib_good_nh(const struct fib_nh *nh)
1740{
1741 int state = NUD_REACHABLE;
1742
1743 if (nh->nh_scope == RT_SCOPE_LINK) {
1744 struct neighbour *n;
1745
1746 rcu_read_lock_bh();
1747
1748 n = __ipv4_neigh_lookup_noref(nh->nh_dev,
1749 (__force u32)nh->nh_gw);
1750 if (n)
1751 state = n->nud_state;
1752
1753 rcu_read_unlock_bh();
1754 }
1755
1756 return !!(state & NUD_VALID);
1757}
1758
1759void fib_select_multipath(struct fib_result *res, int hash)
1760{
1761 struct fib_info *fi = res->fi;
1762 struct net *net = fi->fib_net;
1763 bool first = false;
1764
1765 for_nexthops(fi) {
1766 if (net->ipv4.sysctl_fib_multipath_use_neigh) {
1767 if (!fib_good_nh(nh))
1768 continue;
1769 if (!first) {
1770 res->nh_sel = nhsel;
1771 first = true;
1772 }
1773 }
1774
1775 if (hash > atomic_read(&nh->nh_upper_bound))
1776 continue;
1777
1778 res->nh_sel = nhsel;
1779 return;
1780 } endfor_nexthops(fi);
1781}
1782#endif
1783
1784void fib_select_path(struct net *net, struct fib_result *res,
1785 struct flowi4 *fl4, const struct sk_buff *skb)
1786{
1787 if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
1788 goto check_saddr;
1789
1790#ifdef CONFIG_IP_ROUTE_MULTIPATH
1791 if (res->fi->fib_nhs > 1) {
1792 int h = fib_multipath_hash(net, fl4, skb, NULL);
1793
1794 fib_select_multipath(res, h);
1795 }
1796 else
1797#endif
1798 if (!res->prefixlen &&
1799 res->table->tb_num_default > 1 &&
1800 res->type == RTN_UNICAST)
1801 fib_select_default(fl4, res);
1802
1803check_saddr:
1804 if (!fl4->saddr)
1805 fl4->saddr = FIB_RES_PREFSRC(net, *res);
1806}