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.
6 * IPv4 Forwarding Information Base: semantics.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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.
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>
22 #include <linux/string.h>
23 #include <linux/socket.h>
24 #include <linux/sockios.h>
25 #include <linux/errno.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>
39 #include <net/protocol.h>
40 #include <net/route.h>
43 #include <net/ip_fib.h>
44 #include <net/ip6_fib.h>
45 #include <net/nexthop.h>
46 #include <net/netlink.h>
48 #include <net/lwtunnel.h>
49 #include <net/fib_notifier.h>
50 #include <net/addrconf.h>
52 #include "fib_lookup.h"
54 static DEFINE_SPINLOCK(fib_info_lock);
55 static struct hlist_head *fib_info_hash;
56 static struct hlist_head *fib_info_laddrhash;
57 static unsigned int fib_info_hash_size;
58 static unsigned int fib_info_cnt;
60 #define DEVINDEX_HASHBITS 8
61 #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
62 static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
64 /* for_nexthops and change_nexthops only used when nexthop object
65 * is not set in a fib_info. The logic within can reference fib_nh.
67 #ifdef CONFIG_IP_ROUTE_MULTIPATH
69 #define for_nexthops(fi) { \
70 int nhsel; const struct fib_nh *nh; \
71 for (nhsel = 0, nh = (fi)->fib_nh; \
72 nhsel < fib_info_num_path((fi)); \
75 #define change_nexthops(fi) { \
76 int nhsel; struct fib_nh *nexthop_nh; \
77 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
78 nhsel < fib_info_num_path((fi)); \
79 nexthop_nh++, nhsel++)
81 #else /* CONFIG_IP_ROUTE_MULTIPATH */
83 /* Hope, that gcc will optimize it to get rid of dummy loop */
85 #define for_nexthops(fi) { \
86 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
87 for (nhsel = 0; nhsel < 1; nhsel++)
89 #define change_nexthops(fi) { \
91 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
92 for (nhsel = 0; nhsel < 1; nhsel++)
94 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
96 #define endfor_nexthops(fi) }
99 const struct fib_prop fib_props[RTN_MAX + 1] = {
102 .scope = RT_SCOPE_NOWHERE,
106 .scope = RT_SCOPE_UNIVERSE,
110 .scope = RT_SCOPE_HOST,
114 .scope = RT_SCOPE_LINK,
118 .scope = RT_SCOPE_LINK,
122 .scope = RT_SCOPE_UNIVERSE,
126 .scope = RT_SCOPE_UNIVERSE,
128 [RTN_UNREACHABLE] = {
129 .error = -EHOSTUNREACH,
130 .scope = RT_SCOPE_UNIVERSE,
134 .scope = RT_SCOPE_UNIVERSE,
138 .scope = RT_SCOPE_UNIVERSE,
142 .scope = RT_SCOPE_NOWHERE,
146 .scope = RT_SCOPE_NOWHERE,
150 static void rt_fibinfo_free(struct rtable __rcu **rtp)
152 struct rtable *rt = rcu_dereference_protected(*rtp, 1);
157 /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
158 * because we waited an RCU grace period before calling
159 * free_fib_info_rcu()
162 dst_dev_put(&rt->dst);
163 dst_release_immediate(&rt->dst);
166 static void free_nh_exceptions(struct fib_nh_common *nhc)
168 struct fnhe_hash_bucket *hash;
171 hash = rcu_dereference_protected(nhc->nhc_exceptions, 1);
174 for (i = 0; i < FNHE_HASH_SIZE; i++) {
175 struct fib_nh_exception *fnhe;
177 fnhe = rcu_dereference_protected(hash[i].chain, 1);
179 struct fib_nh_exception *next;
181 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
183 rt_fibinfo_free(&fnhe->fnhe_rth_input);
184 rt_fibinfo_free(&fnhe->fnhe_rth_output);
194 static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
201 for_each_possible_cpu(cpu) {
204 rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
206 dst_dev_put(&rt->dst);
207 dst_release_immediate(&rt->dst);
213 void fib_nh_common_release(struct fib_nh_common *nhc)
216 dev_put(nhc->nhc_dev);
218 lwtstate_put(nhc->nhc_lwtstate);
219 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
220 rt_fibinfo_free(&nhc->nhc_rth_input);
221 free_nh_exceptions(nhc);
223 EXPORT_SYMBOL_GPL(fib_nh_common_release);
225 void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
227 #ifdef CONFIG_IP_ROUTE_CLASSID
228 if (fib_nh->nh_tclassid)
229 net->ipv4.fib_num_tclassid_users--;
231 fib_nh_common_release(&fib_nh->nh_common);
234 /* Release a nexthop info record */
235 static void free_fib_info_rcu(struct rcu_head *head)
237 struct fib_info *fi = container_of(head, struct fib_info, rcu);
239 change_nexthops(fi) {
240 fib_nh_release(fi->fib_net, nexthop_nh);
241 } endfor_nexthops(fi);
243 ip_fib_metrics_put(fi->fib_metrics);
248 void free_fib_info(struct fib_info *fi)
250 if (fi->fib_dead == 0) {
251 pr_warn("Freeing alive fib_info %p\n", fi);
256 call_rcu(&fi->rcu, free_fib_info_rcu);
258 EXPORT_SYMBOL_GPL(free_fib_info);
260 void fib_release_info(struct fib_info *fi)
262 spin_lock_bh(&fib_info_lock);
263 if (fi && --fi->fib_treeref == 0) {
264 hlist_del(&fi->fib_hash);
266 hlist_del(&fi->fib_lhash);
267 change_nexthops(fi) {
268 if (!nexthop_nh->fib_nh_dev)
270 hlist_del(&nexthop_nh->nh_hash);
271 } endfor_nexthops(fi)
275 spin_unlock_bh(&fib_info_lock);
278 static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi)
280 const struct fib_nh *onh;
283 onh = fib_info_nh(ofi, nhsel);
285 if (nh->fib_nh_oif != onh->fib_nh_oif ||
286 nh->fib_nh_gw_family != onh->fib_nh_gw_family ||
287 nh->fib_nh_scope != onh->fib_nh_scope ||
288 #ifdef CONFIG_IP_ROUTE_MULTIPATH
289 nh->fib_nh_weight != onh->fib_nh_weight ||
291 #ifdef CONFIG_IP_ROUTE_CLASSID
292 nh->nh_tclassid != onh->nh_tclassid ||
294 lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
295 ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
298 if (nh->fib_nh_gw_family == AF_INET &&
299 nh->fib_nh_gw4 != onh->fib_nh_gw4)
302 if (nh->fib_nh_gw_family == AF_INET6 &&
303 ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6))
305 } endfor_nexthops(fi);
309 static inline unsigned int fib_devindex_hashfn(unsigned int val)
311 unsigned int mask = DEVINDEX_HASHSIZE - 1;
314 (val >> DEVINDEX_HASHBITS) ^
315 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
318 static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
320 unsigned int mask = (fib_info_hash_size - 1);
321 unsigned int val = fi->fib_nhs;
323 val ^= (fi->fib_protocol << 8) | fi->fib_scope;
324 val ^= (__force u32)fi->fib_prefsrc;
325 val ^= fi->fib_priority;
327 val ^= fib_devindex_hashfn(nh->fib_nh_oif);
328 } endfor_nexthops(fi)
330 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
333 static struct fib_info *fib_find_info(struct fib_info *nfi)
335 struct hlist_head *head;
339 hash = fib_info_hashfn(nfi);
340 head = &fib_info_hash[hash];
342 hlist_for_each_entry(fi, head, fib_hash) {
343 if (!net_eq(fi->fib_net, nfi->fib_net))
345 if (fi->fib_nhs != nfi->fib_nhs)
347 if (nfi->fib_protocol == fi->fib_protocol &&
348 nfi->fib_scope == fi->fib_scope &&
349 nfi->fib_prefsrc == fi->fib_prefsrc &&
350 nfi->fib_priority == fi->fib_priority &&
351 nfi->fib_type == fi->fib_type &&
352 memcmp(nfi->fib_metrics, fi->fib_metrics,
353 sizeof(u32) * RTAX_MAX) == 0 &&
354 !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
355 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
362 /* Check, that the gateway is already configured.
363 * Used only by redirect accept routine.
365 int ip_fib_check_default(__be32 gw, struct net_device *dev)
367 struct hlist_head *head;
371 spin_lock(&fib_info_lock);
373 hash = fib_devindex_hashfn(dev->ifindex);
374 head = &fib_info_devhash[hash];
375 hlist_for_each_entry(nh, head, nh_hash) {
376 if (nh->fib_nh_dev == dev &&
377 nh->fib_nh_gw4 == gw &&
378 !(nh->fib_nh_flags & RTNH_F_DEAD)) {
379 spin_unlock(&fib_info_lock);
384 spin_unlock(&fib_info_lock);
389 static inline size_t fib_nlmsg_size(struct fib_info *fi)
391 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
392 + nla_total_size(4) /* RTA_TABLE */
393 + nla_total_size(4) /* RTA_DST */
394 + nla_total_size(4) /* RTA_PRIORITY */
395 + nla_total_size(4) /* RTA_PREFSRC */
396 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
397 unsigned int nhs = fib_info_num_path(fi);
399 /* space for nested metrics */
400 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
403 size_t nh_encapsize = 0;
404 /* Also handles the special case nhs == 1 */
406 /* each nexthop is packed in an attribute */
407 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
410 /* may contain flow and gateway attribute */
411 nhsize += 2 * nla_total_size(4);
413 /* grab encap info */
414 for (i = 0; i < fib_info_num_path(fi); i++) {
415 struct fib_nh_common *nhc = fib_info_nhc(fi, i);
417 if (nhc->nhc_lwtstate) {
419 nh_encapsize += lwtunnel_get_encap_size(
422 nh_encapsize += nla_total_size(2);
426 /* all nexthops are packed in a nested attribute */
427 payload += nla_total_size((nhs * nhsize) + nh_encapsize);
434 void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
435 int dst_len, u32 tb_id, const struct nl_info *info,
436 unsigned int nlm_flags)
439 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
442 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
446 err = fib_dump_info(skb, info->portid, seq, event, tb_id,
447 fa->fa_type, key, dst_len,
448 fa->fa_tos, fa->fa_info, nlm_flags);
450 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
451 WARN_ON(err == -EMSGSIZE);
455 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
456 info->nlh, GFP_KERNEL);
460 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
463 static int fib_detect_death(struct fib_info *fi, int order,
464 struct fib_info **last_resort, int *last_idx,
467 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
469 int state = NUD_NONE;
471 if (likely(nhc->nhc_gw_family == AF_INET))
472 n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev);
473 else if (nhc->nhc_gw_family == AF_INET6)
474 n = neigh_lookup(ipv6_stub->nd_tbl, &nhc->nhc_gw.ipv6,
480 state = n->nud_state;
485 if (state == NUD_REACHABLE)
487 if ((state & NUD_VALID) && order != dflt)
489 if ((state & NUD_VALID) ||
490 (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
497 int fib_nh_common_init(struct fib_nh_common *nhc, struct nlattr *encap,
498 u16 encap_type, void *cfg, gfp_t gfp_flags,
499 struct netlink_ext_ack *extack)
503 nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *,
505 if (!nhc->nhc_pcpu_rth_output)
509 struct lwtunnel_state *lwtstate;
511 if (encap_type == LWTUNNEL_ENCAP_NONE) {
512 NL_SET_ERR_MSG(extack, "LWT encap type not specified");
516 err = lwtunnel_build_state(encap_type, encap, nhc->nhc_family,
517 cfg, &lwtstate, extack);
521 nhc->nhc_lwtstate = lwtstate_get(lwtstate);
527 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
528 nhc->nhc_pcpu_rth_output = NULL;
531 EXPORT_SYMBOL_GPL(fib_nh_common_init);
533 int fib_nh_init(struct net *net, struct fib_nh *nh,
534 struct fib_config *cfg, int nh_weight,
535 struct netlink_ext_ack *extack)
539 nh->fib_nh_family = AF_INET;
541 err = fib_nh_common_init(&nh->nh_common, cfg->fc_encap,
542 cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
546 nh->fib_nh_oif = cfg->fc_oif;
547 nh->fib_nh_gw_family = cfg->fc_gw_family;
548 if (cfg->fc_gw_family == AF_INET)
549 nh->fib_nh_gw4 = cfg->fc_gw4;
550 else if (cfg->fc_gw_family == AF_INET6)
551 nh->fib_nh_gw6 = cfg->fc_gw6;
553 nh->fib_nh_flags = cfg->fc_flags;
555 #ifdef CONFIG_IP_ROUTE_CLASSID
556 nh->nh_tclassid = cfg->fc_flow;
558 net->ipv4.fib_num_tclassid_users++;
560 #ifdef CONFIG_IP_ROUTE_MULTIPATH
561 nh->fib_nh_weight = nh_weight;
566 #ifdef CONFIG_IP_ROUTE_MULTIPATH
568 static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
569 struct netlink_ext_ack *extack)
573 while (rtnh_ok(rtnh, remaining)) {
575 rtnh = rtnh_next(rtnh, &remaining);
578 /* leftover implies invalid nexthop configuration, discard it */
580 NL_SET_ERR_MSG(extack,
581 "Invalid nexthop configuration - extra data after nexthops");
588 static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
589 int remaining, struct fib_config *cfg,
590 struct netlink_ext_ack *extack)
592 struct net *net = fi->fib_net;
593 struct fib_config fib_cfg;
597 change_nexthops(fi) {
600 memset(&fib_cfg, 0, sizeof(fib_cfg));
602 if (!rtnh_ok(rtnh, remaining)) {
603 NL_SET_ERR_MSG(extack,
604 "Invalid nexthop configuration - extra data after nexthop");
608 if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
609 NL_SET_ERR_MSG(extack,
610 "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
614 fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
615 fib_cfg.fc_oif = rtnh->rtnh_ifindex;
617 attrlen = rtnh_attrlen(rtnh);
619 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
621 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
622 nlav = nla_find(attrs, attrlen, RTA_VIA);
624 NL_SET_ERR_MSG(extack,
625 "Nexthop configuration can not contain both GATEWAY and VIA");
629 fib_cfg.fc_gw4 = nla_get_in_addr(nla);
631 fib_cfg.fc_gw_family = AF_INET;
633 ret = fib_gw_from_via(&fib_cfg, nlav, extack);
638 nla = nla_find(attrs, attrlen, RTA_FLOW);
640 fib_cfg.fc_flow = nla_get_u32(nla);
642 fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
643 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
645 fib_cfg.fc_encap_type = nla_get_u16(nla);
648 ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
649 rtnh->rtnh_hops + 1, extack);
653 rtnh = rtnh_next(rtnh, &remaining);
654 } endfor_nexthops(fi);
657 nh = fib_info_nh(fi, 0);
658 if (cfg->fc_oif && nh->fib_nh_oif != cfg->fc_oif) {
659 NL_SET_ERR_MSG(extack,
660 "Nexthop device index does not match RTA_OIF");
663 if (cfg->fc_gw_family) {
664 if (cfg->fc_gw_family != nh->fib_nh_gw_family ||
665 (cfg->fc_gw_family == AF_INET &&
666 nh->fib_nh_gw4 != cfg->fc_gw4) ||
667 (cfg->fc_gw_family == AF_INET6 &&
668 ipv6_addr_cmp(&nh->fib_nh_gw6, &cfg->fc_gw6))) {
669 NL_SET_ERR_MSG(extack,
670 "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA");
674 #ifdef CONFIG_IP_ROUTE_CLASSID
675 if (cfg->fc_flow && nh->nh_tclassid != cfg->fc_flow) {
676 NL_SET_ERR_MSG(extack,
677 "Nexthop class id does not match RTA_FLOW");
686 static void fib_rebalance(struct fib_info *fi)
691 if (fib_info_num_path(fi) < 2)
696 if (nh->fib_nh_flags & RTNH_F_DEAD)
699 if (ip_ignore_linkdown(nh->fib_nh_dev) &&
700 nh->fib_nh_flags & RTNH_F_LINKDOWN)
703 total += nh->fib_nh_weight;
704 } endfor_nexthops(fi);
707 change_nexthops(fi) {
710 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
712 } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
713 nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
716 w += nexthop_nh->fib_nh_weight;
717 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
721 atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
722 } endfor_nexthops(fi);
724 #else /* CONFIG_IP_ROUTE_MULTIPATH */
726 static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
727 int remaining, struct fib_config *cfg,
728 struct netlink_ext_ack *extack)
730 NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
735 #define fib_rebalance(fi) do { } while (0)
737 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
739 static int fib_encap_match(u16 encap_type,
740 struct nlattr *encap,
741 const struct fib_nh *nh,
742 const struct fib_config *cfg,
743 struct netlink_ext_ack *extack)
745 struct lwtunnel_state *lwtstate;
748 if (encap_type == LWTUNNEL_ENCAP_NONE)
751 ret = lwtunnel_build_state(encap_type, encap, AF_INET,
752 cfg, &lwtstate, extack);
754 result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
755 lwtstate_free(lwtstate);
761 int fib_nh_match(struct fib_config *cfg, struct fib_info *fi,
762 struct netlink_ext_ack *extack)
764 #ifdef CONFIG_IP_ROUTE_MULTIPATH
765 struct rtnexthop *rtnh;
769 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
772 if (cfg->fc_oif || cfg->fc_gw_family) {
773 struct fib_nh *nh = fib_info_nh(fi, 0);
776 if (fib_encap_match(cfg->fc_encap_type, cfg->fc_encap,
780 #ifdef CONFIG_IP_ROUTE_CLASSID
782 cfg->fc_flow != nh->nh_tclassid)
785 if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) ||
786 (cfg->fc_gw_family &&
787 cfg->fc_gw_family != nh->fib_nh_gw_family))
790 if (cfg->fc_gw_family == AF_INET &&
791 cfg->fc_gw4 != nh->fib_nh_gw4)
794 if (cfg->fc_gw_family == AF_INET6 &&
795 ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6))
801 #ifdef CONFIG_IP_ROUTE_MULTIPATH
806 remaining = cfg->fc_mp_len;
811 if (!rtnh_ok(rtnh, remaining))
814 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
817 attrlen = rtnh_attrlen(rtnh);
819 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
821 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
822 nlav = nla_find(attrs, attrlen, RTA_VIA);
824 NL_SET_ERR_MSG(extack,
825 "Nexthop configuration can not contain both GATEWAY and VIA");
830 if (nh->fib_nh_gw_family != AF_INET ||
831 nla_get_in_addr(nla) != nh->fib_nh_gw4)
834 struct fib_config cfg2;
837 err = fib_gw_from_via(&cfg2, nlav, extack);
841 switch (nh->fib_nh_gw_family) {
843 if (cfg2.fc_gw_family != AF_INET ||
844 cfg2.fc_gw4 != nh->fib_nh_gw4)
848 if (cfg2.fc_gw_family != AF_INET6 ||
849 ipv6_addr_cmp(&cfg2.fc_gw6,
856 #ifdef CONFIG_IP_ROUTE_CLASSID
857 nla = nla_find(attrs, attrlen, RTA_FLOW);
858 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
863 rtnh = rtnh_next(rtnh, &remaining);
864 } endfor_nexthops(fi);
869 bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
877 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
878 int type = nla_type(nla);
886 if (type == RTAX_CC_ALGO) {
887 char tmp[TCP_CA_NAME_MAX];
890 nla_strlcpy(tmp, nla, sizeof(tmp));
891 val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
893 if (nla_len(nla) != sizeof(u32))
895 val = nla_get_u32(nla);
898 fi_val = fi->fib_metrics->metrics[type - 1];
899 if (type == RTAX_FEATURES)
900 fi_val &= ~DST_FEATURE_ECN_CA;
909 static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
910 u32 table, struct netlink_ext_ack *extack)
912 struct fib6_config cfg = {
914 .fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
915 .fc_ifindex = nh->fib_nh_oif,
916 .fc_gateway = nh->fib_nh_gw6,
918 struct fib6_nh fib6_nh = {};
921 err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
923 nh->fib_nh_dev = fib6_nh.fib_nh_dev;
924 dev_hold(nh->fib_nh_dev);
925 nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
926 nh->fib_nh_scope = RT_SCOPE_LINK;
928 ipv6_stub->fib6_nh_release(&fib6_nh);
938 * Semantics of nexthop is very messy by historical reasons.
939 * We have to take into account, that:
940 * a) gateway can be actually local interface address,
941 * so that gatewayed route is direct.
942 * b) gateway must be on-link address, possibly
943 * described not by an ifaddr, but also by a direct route.
944 * c) If both gateway and interface are specified, they should not
946 * d) If we use tunnel routes, gateway could be not on-link.
948 * Attempt to reconcile all of these (alas, self-contradictory) conditions
949 * results in pretty ugly and hairy code with obscure logic.
951 * I chose to generalized it instead, so that the size
952 * of code does not increase practically, but it becomes
954 * Every prefix is assigned a "scope" value: "host" is local address,
955 * "link" is direct route,
956 * [ ... "site" ... "interior" ... ]
957 * and "universe" is true gateway route with global meaning.
959 * Every prefix refers to a set of "nexthop"s (gw, oif),
960 * where gw must have narrower scope. This recursion stops
961 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
962 * which means that gw is forced to be on link.
964 * Code is still hairy, but now it is apparently logically
965 * consistent and very flexible. F.e. as by-product it allows
966 * to co-exists in peace independent exterior and interior
969 * Normally it looks as following.
971 * {universe prefix} -> (gw, oif) [scope link]
973 * |-> {link prefix} -> (gw, oif) [scope local]
975 * |-> {local prefix} (terminal node)
977 static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
978 u8 scope, struct netlink_ext_ack *extack)
980 struct net_device *dev;
981 struct fib_result res;
984 if (nh->fib_nh_flags & RTNH_F_ONLINK) {
985 unsigned int addr_type;
987 if (scope >= RT_SCOPE_LINK) {
988 NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
991 dev = __dev_get_by_index(net, nh->fib_nh_oif);
993 NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
996 if (!(dev->flags & IFF_UP)) {
997 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
1000 addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
1001 if (addr_type != RTN_UNICAST) {
1002 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1005 if (!netif_carrier_ok(dev))
1006 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1007 nh->fib_nh_dev = dev;
1009 nh->fib_nh_scope = RT_SCOPE_LINK;
1014 struct fib_table *tbl = NULL;
1015 struct flowi4 fl4 = {
1016 .daddr = nh->fib_nh_gw4,
1017 .flowi4_scope = scope + 1,
1018 .flowi4_oif = nh->fib_nh_oif,
1019 .flowi4_iif = LOOPBACK_IFINDEX,
1022 /* It is not necessary, but requires a bit of thinking */
1023 if (fl4.flowi4_scope < RT_SCOPE_LINK)
1024 fl4.flowi4_scope = RT_SCOPE_LINK;
1027 tbl = fib_get_table(net, table);
1030 err = fib_table_lookup(tbl, &fl4, &res,
1031 FIB_LOOKUP_IGNORE_LINKSTATE |
1034 /* on error or if no table given do full lookup. This
1035 * is needed for example when nexthops are in the local
1036 * table rather than the given table
1039 err = fib_lookup(net, &fl4, &res,
1040 FIB_LOOKUP_IGNORE_LINKSTATE);
1044 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1050 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
1051 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1054 nh->fib_nh_scope = res.scope;
1055 nh->fib_nh_oif = FIB_RES_OIF(res);
1056 nh->fib_nh_dev = dev = FIB_RES_DEV(res);
1058 NL_SET_ERR_MSG(extack,
1059 "No egress device for nexthop gateway");
1063 if (!netif_carrier_ok(dev))
1064 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1065 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
1071 static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
1072 struct netlink_ext_ack *extack)
1074 struct in_device *in_dev;
1077 if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
1078 NL_SET_ERR_MSG(extack,
1079 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
1086 in_dev = inetdev_by_index(net, nh->fib_nh_oif);
1090 if (!(in_dev->dev->flags & IFF_UP)) {
1091 NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
1095 nh->fib_nh_dev = in_dev->dev;
1096 dev_hold(nh->fib_nh_dev);
1097 nh->fib_nh_scope = RT_SCOPE_HOST;
1098 if (!netif_carrier_ok(nh->fib_nh_dev))
1099 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1106 int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
1107 struct netlink_ext_ack *extack)
1111 if (nh->fib_nh_gw_family == AF_INET)
1112 err = fib_check_nh_v4_gw(net, nh, table, scope, extack);
1113 else if (nh->fib_nh_gw_family == AF_INET6)
1114 err = fib_check_nh_v6_gw(net, nh, table, extack);
1116 err = fib_check_nh_nongw(net, nh, extack);
1121 static inline unsigned int fib_laddr_hashfn(__be32 val)
1123 unsigned int mask = (fib_info_hash_size - 1);
1125 return ((__force u32)val ^
1126 ((__force u32)val >> 7) ^
1127 ((__force u32)val >> 14)) & mask;
1130 static struct hlist_head *fib_info_hash_alloc(int bytes)
1132 if (bytes <= PAGE_SIZE)
1133 return kzalloc(bytes, GFP_KERNEL);
1135 return (struct hlist_head *)
1136 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1140 static void fib_info_hash_free(struct hlist_head *hash, int bytes)
1145 if (bytes <= PAGE_SIZE)
1148 free_pages((unsigned long) hash, get_order(bytes));
1151 static void fib_info_hash_move(struct hlist_head *new_info_hash,
1152 struct hlist_head *new_laddrhash,
1153 unsigned int new_size)
1155 struct hlist_head *old_info_hash, *old_laddrhash;
1156 unsigned int old_size = fib_info_hash_size;
1157 unsigned int i, bytes;
1159 spin_lock_bh(&fib_info_lock);
1160 old_info_hash = fib_info_hash;
1161 old_laddrhash = fib_info_laddrhash;
1162 fib_info_hash_size = new_size;
1164 for (i = 0; i < old_size; i++) {
1165 struct hlist_head *head = &fib_info_hash[i];
1166 struct hlist_node *n;
1167 struct fib_info *fi;
1169 hlist_for_each_entry_safe(fi, n, head, fib_hash) {
1170 struct hlist_head *dest;
1171 unsigned int new_hash;
1173 new_hash = fib_info_hashfn(fi);
1174 dest = &new_info_hash[new_hash];
1175 hlist_add_head(&fi->fib_hash, dest);
1178 fib_info_hash = new_info_hash;
1180 for (i = 0; i < old_size; i++) {
1181 struct hlist_head *lhead = &fib_info_laddrhash[i];
1182 struct hlist_node *n;
1183 struct fib_info *fi;
1185 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
1186 struct hlist_head *ldest;
1187 unsigned int new_hash;
1189 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
1190 ldest = &new_laddrhash[new_hash];
1191 hlist_add_head(&fi->fib_lhash, ldest);
1194 fib_info_laddrhash = new_laddrhash;
1196 spin_unlock_bh(&fib_info_lock);
1198 bytes = old_size * sizeof(struct hlist_head *);
1199 fib_info_hash_free(old_info_hash, bytes);
1200 fib_info_hash_free(old_laddrhash, bytes);
1203 __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
1204 unsigned char scope)
1208 if (nhc->nhc_family != AF_INET)
1209 return inet_select_addr(nhc->nhc_dev, 0, scope);
1211 nh = container_of(nhc, struct fib_nh, nh_common);
1212 nh->nh_saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope);
1213 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
1215 return nh->nh_saddr;
1218 __be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1220 struct fib_nh_common *nhc = res->nhc;
1222 if (res->fi->fib_prefsrc)
1223 return res->fi->fib_prefsrc;
1225 if (nhc->nhc_family == AF_INET) {
1228 nh = container_of(nhc, struct fib_nh, nh_common);
1229 if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid))
1230 return nh->nh_saddr;
1233 return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope);
1236 static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1238 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1239 fib_prefsrc != cfg->fc_dst) {
1240 u32 tb_id = cfg->fc_table;
1243 if (tb_id == RT_TABLE_MAIN)
1244 tb_id = RT_TABLE_LOCAL;
1246 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1247 fib_prefsrc, tb_id);
1249 if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1250 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1251 fib_prefsrc, RT_TABLE_LOCAL);
1254 if (rc != RTN_LOCAL)
1260 struct fib_info *fib_create_info(struct fib_config *cfg,
1261 struct netlink_ext_ack *extack)
1264 struct fib_info *fi = NULL;
1265 struct fib_info *ofi;
1267 struct net *net = cfg->fc_nlinfo.nl_net;
1269 if (cfg->fc_type > RTN_MAX)
1272 /* Fast check to catch the most weird cases */
1273 if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1274 NL_SET_ERR_MSG(extack, "Invalid scope");
1278 if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1279 NL_SET_ERR_MSG(extack,
1280 "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
1284 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1286 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
1293 if (fib_info_cnt >= fib_info_hash_size) {
1294 unsigned int new_size = fib_info_hash_size << 1;
1295 struct hlist_head *new_info_hash;
1296 struct hlist_head *new_laddrhash;
1301 bytes = new_size * sizeof(struct hlist_head *);
1302 new_info_hash = fib_info_hash_alloc(bytes);
1303 new_laddrhash = fib_info_hash_alloc(bytes);
1304 if (!new_info_hash || !new_laddrhash) {
1305 fib_info_hash_free(new_info_hash, bytes);
1306 fib_info_hash_free(new_laddrhash, bytes);
1308 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
1310 if (!fib_info_hash_size)
1314 fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
1317 fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
1318 cfg->fc_mx_len, extack);
1319 if (unlikely(IS_ERR(fi->fib_metrics))) {
1320 err = PTR_ERR(fi->fib_metrics);
1322 return ERR_PTR(err);
1327 fi->fib_protocol = cfg->fc_protocol;
1328 fi->fib_scope = cfg->fc_scope;
1329 fi->fib_flags = cfg->fc_flags;
1330 fi->fib_priority = cfg->fc_priority;
1331 fi->fib_prefsrc = cfg->fc_prefsrc;
1332 fi->fib_type = cfg->fc_type;
1333 fi->fib_tb_id = cfg->fc_table;
1336 change_nexthops(fi) {
1337 nexthop_nh->nh_parent = fi;
1338 } endfor_nexthops(fi)
1341 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg, extack);
1343 err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
1348 if (fib_props[cfg->fc_type].error) {
1349 if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
1350 NL_SET_ERR_MSG(extack,
1351 "Gateway, device and multipath can not be specified for this route type");
1356 switch (cfg->fc_type) {
1364 NL_SET_ERR_MSG(extack, "Invalid route type");
1369 if (cfg->fc_scope > RT_SCOPE_HOST) {
1370 NL_SET_ERR_MSG(extack, "Invalid scope");
1374 if (cfg->fc_scope == RT_SCOPE_HOST) {
1375 struct fib_nh *nh = fi->fib_nh;
1377 /* Local address is added. */
1379 NL_SET_ERR_MSG(extack,
1380 "Route with host scope can not have multiple nexthops");
1383 if (nh->fib_nh_gw_family) {
1384 NL_SET_ERR_MSG(extack,
1385 "Route with host scope can not have a gateway");
1388 nh->fib_nh_scope = RT_SCOPE_NOWHERE;
1389 nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif);
1391 if (!nh->fib_nh_dev)
1396 change_nexthops(fi) {
1397 err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
1398 cfg->fc_table, cfg->fc_scope,
1402 if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
1404 } endfor_nexthops(fi)
1405 if (linkdown == fi->fib_nhs)
1406 fi->fib_flags |= RTNH_F_LINKDOWN;
1409 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1410 NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
1414 change_nexthops(fi) {
1415 fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common,
1417 if (nexthop_nh->fib_nh_gw_family == AF_INET6)
1418 fi->fib_nh_is_v6 = true;
1419 } endfor_nexthops(fi)
1424 ofi = fib_find_info(fi);
1433 refcount_set(&fi->fib_clntref, 1);
1434 spin_lock_bh(&fib_info_lock);
1435 hlist_add_head(&fi->fib_hash,
1436 &fib_info_hash[fib_info_hashfn(fi)]);
1437 if (fi->fib_prefsrc) {
1438 struct hlist_head *head;
1440 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
1441 hlist_add_head(&fi->fib_lhash, head);
1443 change_nexthops(fi) {
1444 struct hlist_head *head;
1447 if (!nexthop_nh->fib_nh_dev)
1449 hash = fib_devindex_hashfn(nexthop_nh->fib_nh_dev->ifindex);
1450 head = &fib_info_devhash[hash];
1451 hlist_add_head(&nexthop_nh->nh_hash, head);
1452 } endfor_nexthops(fi)
1453 spin_unlock_bh(&fib_info_lock);
1465 return ERR_PTR(err);
1468 int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
1469 unsigned char *flags, bool skip_oif)
1471 if (nhc->nhc_flags & RTNH_F_DEAD)
1472 *flags |= RTNH_F_DEAD;
1474 if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
1475 *flags |= RTNH_F_LINKDOWN;
1478 switch (nhc->nhc_family) {
1480 if (ip_ignore_linkdown(nhc->nhc_dev))
1481 *flags |= RTNH_F_DEAD;
1484 if (ip6_ignore_linkdown(nhc->nhc_dev))
1485 *flags |= RTNH_F_DEAD;
1491 switch (nhc->nhc_gw_family) {
1493 if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1494 goto nla_put_failure;
1497 /* if gateway family does not match nexthop family
1498 * gateway is encoded as RTA_VIA
1500 if (nhc->nhc_gw_family != nhc->nhc_family) {
1501 int alen = sizeof(struct in6_addr);
1505 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1507 goto nla_put_failure;
1509 via = nla_data(nla);
1510 via->rtvia_family = AF_INET6;
1511 memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen);
1512 } else if (nla_put_in6_addr(skb, RTA_GATEWAY,
1513 &nhc->nhc_gw.ipv6) < 0) {
1514 goto nla_put_failure;
1519 *flags |= (nhc->nhc_flags & RTNH_F_ONLINK);
1520 if (nhc->nhc_flags & RTNH_F_OFFLOAD)
1521 *flags |= RTNH_F_OFFLOAD;
1523 if (!skip_oif && nhc->nhc_dev &&
1524 nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
1525 goto nla_put_failure;
1527 if (nhc->nhc_lwtstate &&
1528 lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
1529 RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
1530 goto nla_put_failure;
1537 EXPORT_SYMBOL_GPL(fib_nexthop_info);
1539 #if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1540 int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
1543 const struct net_device *dev = nhc->nhc_dev;
1544 struct rtnexthop *rtnh;
1545 unsigned char flags = 0;
1547 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1549 goto nla_put_failure;
1551 rtnh->rtnh_hops = nh_weight - 1;
1552 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1554 if (fib_nexthop_info(skb, nhc, &flags, true) < 0)
1555 goto nla_put_failure;
1557 rtnh->rtnh_flags = flags;
1559 /* length of rtnetlink header + attributes */
1560 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1567 EXPORT_SYMBOL_GPL(fib_add_nexthop);
1570 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1571 static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1575 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
1577 goto nla_put_failure;
1580 if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight) < 0)
1581 goto nla_put_failure;
1582 #ifdef CONFIG_IP_ROUTE_CLASSID
1583 if (nh->nh_tclassid &&
1584 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1585 goto nla_put_failure;
1587 } endfor_nexthops(fi);
1589 nla_nest_end(skb, mp);
1597 static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1603 int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
1604 u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
1605 struct fib_info *fi, unsigned int flags)
1607 unsigned int nhs = fib_info_num_path(fi);
1608 struct nlmsghdr *nlh;
1611 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1615 rtm = nlmsg_data(nlh);
1616 rtm->rtm_family = AF_INET;
1617 rtm->rtm_dst_len = dst_len;
1618 rtm->rtm_src_len = 0;
1621 rtm->rtm_table = tb_id;
1623 rtm->rtm_table = RT_TABLE_COMPAT;
1624 if (nla_put_u32(skb, RTA_TABLE, tb_id))
1625 goto nla_put_failure;
1626 rtm->rtm_type = type;
1627 rtm->rtm_flags = fi->fib_flags;
1628 rtm->rtm_scope = fi->fib_scope;
1629 rtm->rtm_protocol = fi->fib_protocol;
1631 if (rtm->rtm_dst_len &&
1632 nla_put_in_addr(skb, RTA_DST, dst))
1633 goto nla_put_failure;
1634 if (fi->fib_priority &&
1635 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1636 goto nla_put_failure;
1637 if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
1638 goto nla_put_failure;
1640 if (fi->fib_prefsrc &&
1641 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
1642 goto nla_put_failure;
1644 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
1645 unsigned char flags = 0;
1647 if (fib_nexthop_info(skb, nhc, &flags, false) < 0)
1648 goto nla_put_failure;
1650 rtm->rtm_flags = flags;
1651 #ifdef CONFIG_IP_ROUTE_CLASSID
1652 if (nhc->nhc_family == AF_INET) {
1655 nh = container_of(nhc, struct fib_nh, nh_common);
1656 if (nh->nh_tclassid &&
1657 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1658 goto nla_put_failure;
1662 if (fib_add_multipath(skb, fi) < 0)
1663 goto nla_put_failure;
1666 nlmsg_end(skb, nlh);
1670 nlmsg_cancel(skb, nlh);
1676 * - local address disappeared -> we must delete all the entries
1678 * - device went down -> we must shutdown all nexthops going via it.
1680 int fib_sync_down_addr(struct net_device *dev, __be32 local)
1683 unsigned int hash = fib_laddr_hashfn(local);
1684 struct hlist_head *head = &fib_info_laddrhash[hash];
1685 struct net *net = dev_net(dev);
1686 int tb_id = l3mdev_fib_table(dev);
1687 struct fib_info *fi;
1689 if (!fib_info_laddrhash || local == 0)
1692 hlist_for_each_entry(fi, head, fib_lhash) {
1693 if (!net_eq(fi->fib_net, net) ||
1694 fi->fib_tb_id != tb_id)
1696 if (fi->fib_prefsrc == local) {
1697 fi->fib_flags |= RTNH_F_DEAD;
1704 static int call_fib_nh_notifiers(struct fib_nh *nh,
1705 enum fib_event_type event_type)
1707 bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
1708 struct fib_nh_notifier_info info = {
1712 switch (event_type) {
1713 case FIB_EVENT_NH_ADD:
1714 if (nh->fib_nh_flags & RTNH_F_DEAD)
1716 if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
1718 return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
1720 case FIB_EVENT_NH_DEL:
1721 if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1722 (nh->fib_nh_flags & RTNH_F_DEAD))
1723 return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
1724 event_type, &info.info);
1732 /* Update the PMTU of exceptions when:
1733 * - the new MTU of the first hop becomes smaller than the PMTU
1734 * - the old MTU was the same as the PMTU, and it limited discovery of
1735 * larger MTUs on the path. With that limit raised, we can now
1736 * discover larger MTUs
1737 * A special case is locked exceptions, for which the PMTU is smaller
1738 * than the minimal accepted PMTU:
1739 * - if the new MTU is greater than the PMTU, don't make any change
1740 * - otherwise, unlock and set PMTU
1742 void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
1744 struct fnhe_hash_bucket *bucket;
1747 bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
1751 for (i = 0; i < FNHE_HASH_SIZE; i++) {
1752 struct fib_nh_exception *fnhe;
1754 for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1756 fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1757 if (fnhe->fnhe_mtu_locked) {
1758 if (new <= fnhe->fnhe_pmtu) {
1759 fnhe->fnhe_pmtu = new;
1760 fnhe->fnhe_mtu_locked = false;
1762 } else if (new < fnhe->fnhe_pmtu ||
1763 orig == fnhe->fnhe_pmtu) {
1764 fnhe->fnhe_pmtu = new;
1770 void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1772 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1773 struct hlist_head *head = &fib_info_devhash[hash];
1776 hlist_for_each_entry(nh, head, nh_hash) {
1777 if (nh->fib_nh_dev == dev)
1778 fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
1782 /* Event force Flags Description
1783 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
1784 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
1785 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
1786 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
1788 int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
1791 int scope = RT_SCOPE_NOWHERE;
1792 struct fib_info *prev_fi = NULL;
1793 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1794 struct hlist_head *head = &fib_info_devhash[hash];
1800 hlist_for_each_entry(nh, head, nh_hash) {
1801 struct fib_info *fi = nh->nh_parent;
1804 BUG_ON(!fi->fib_nhs);
1805 if (nh->fib_nh_dev != dev || fi == prev_fi)
1809 change_nexthops(fi) {
1810 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
1812 else if (nexthop_nh->fib_nh_dev == dev &&
1813 nexthop_nh->fib_nh_scope != scope) {
1816 case NETDEV_UNREGISTER:
1817 nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
1820 nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1823 call_fib_nh_notifiers(nexthop_nh,
1827 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1828 if (event == NETDEV_UNREGISTER &&
1829 nexthop_nh->fib_nh_dev == dev) {
1834 } endfor_nexthops(fi)
1835 if (dead == fi->fib_nhs) {
1838 case NETDEV_UNREGISTER:
1839 fi->fib_flags |= RTNH_F_DEAD;
1842 fi->fib_flags |= RTNH_F_LINKDOWN;
1854 /* Must be invoked inside of an RCU protected region. */
1855 static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
1857 struct fib_info *fi = NULL, *last_resort = NULL;
1858 struct hlist_head *fa_head = res->fa_head;
1859 struct fib_table *tb = res->table;
1860 u8 slen = 32 - res->prefixlen;
1861 int order = -1, last_idx = -1;
1862 struct fib_alias *fa, *fa1 = NULL;
1863 u32 last_prio = res->fi->fib_priority;
1866 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
1867 struct fib_info *next_fi = fa->fa_info;
1870 if (fa->fa_slen != slen)
1872 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1874 if (fa->tb_id != tb->tb_id)
1876 if (next_fi->fib_priority > last_prio &&
1877 fa->fa_tos == last_tos) {
1882 if (next_fi->fib_flags & RTNH_F_DEAD)
1884 last_tos = fa->fa_tos;
1885 last_prio = next_fi->fib_priority;
1887 if (next_fi->fib_scope != res->scope ||
1888 fa->fa_type != RTN_UNICAST)
1891 nh = fib_info_nh(next_fi, 0);
1892 if (!nh->fib_nh_gw4 || nh->fib_nh_scope != RT_SCOPE_LINK)
1895 fib_alias_accessed(fa);
1898 if (next_fi != res->fi)
1901 } else if (!fib_detect_death(fi, order, &last_resort,
1902 &last_idx, fa1->fa_default)) {
1903 fib_result_assign(res, fi);
1904 fa1->fa_default = order;
1911 if (order <= 0 || !fi) {
1913 fa1->fa_default = -1;
1917 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
1919 fib_result_assign(res, fi);
1920 fa1->fa_default = order;
1925 fib_result_assign(res, last_resort);
1926 fa1->fa_default = last_idx;
1932 * Dead device goes up. We wake up dead nexthops.
1933 * It takes sense only on multipath routes.
1935 int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
1937 struct fib_info *prev_fi;
1939 struct hlist_head *head;
1943 if (!(dev->flags & IFF_UP))
1946 if (nh_flags & RTNH_F_DEAD) {
1947 unsigned int flags = dev_get_flags(dev);
1949 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1950 nh_flags |= RTNH_F_LINKDOWN;
1954 hash = fib_devindex_hashfn(dev->ifindex);
1955 head = &fib_info_devhash[hash];
1958 hlist_for_each_entry(nh, head, nh_hash) {
1959 struct fib_info *fi = nh->nh_parent;
1962 BUG_ON(!fi->fib_nhs);
1963 if (nh->fib_nh_dev != dev || fi == prev_fi)
1968 change_nexthops(fi) {
1969 if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
1973 if (!nexthop_nh->fib_nh_dev ||
1974 !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
1976 if (nexthop_nh->fib_nh_dev != dev ||
1977 !__in_dev_get_rtnl(dev))
1980 nexthop_nh->fib_nh_flags &= ~nh_flags;
1981 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
1982 } endfor_nexthops(fi)
1985 fi->fib_flags &= ~nh_flags;
1995 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1996 static bool fib_good_nh(const struct fib_nh *nh)
1998 int state = NUD_REACHABLE;
2000 if (nh->fib_nh_scope == RT_SCOPE_LINK) {
2001 struct neighbour *n;
2005 if (likely(nh->fib_nh_gw_family == AF_INET))
2006 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
2007 (__force u32)nh->fib_nh_gw4);
2008 else if (nh->fib_nh_gw_family == AF_INET6)
2009 n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev,
2014 state = n->nud_state;
2016 rcu_read_unlock_bh();
2019 return !!(state & NUD_VALID);
2022 void fib_select_multipath(struct fib_result *res, int hash)
2024 struct fib_info *fi = res->fi;
2025 struct net *net = fi->fib_net;
2028 change_nexthops(fi) {
2029 if (net->ipv4.sysctl_fib_multipath_use_neigh) {
2030 if (!fib_good_nh(nexthop_nh))
2033 res->nh_sel = nhsel;
2034 res->nhc = &nexthop_nh->nh_common;
2039 if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
2042 res->nh_sel = nhsel;
2043 res->nhc = &nexthop_nh->nh_common;
2045 } endfor_nexthops(fi);
2049 void fib_select_path(struct net *net, struct fib_result *res,
2050 struct flowi4 *fl4, const struct sk_buff *skb)
2052 if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
2055 #ifdef CONFIG_IP_ROUTE_MULTIPATH
2056 if (fib_info_num_path(res->fi) > 1) {
2057 int h = fib_multipath_hash(net, fl4, skb, NULL);
2059 fib_select_multipath(res, h);
2063 if (!res->prefixlen &&
2064 res->table->tb_num_default > 1 &&
2065 res->type == RTN_UNICAST)
2066 fib_select_default(fl4, res);
2070 fl4->saddr = fib_result_prefsrc(net, res);