1 // SPDX-License-Identifier: GPL-2.0
2 /* Generic nexthop implementation
4 * Copyright (c) 2017-19 Cumulus Networks
5 * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com>
8 #include <linux/nexthop.h>
9 #include <linux/rtnetlink.h>
10 #include <linux/slab.h>
12 #include <net/ipv6_stubs.h>
13 #include <net/lwtunnel.h>
14 #include <net/ndisc.h>
15 #include <net/nexthop.h>
16 #include <net/route.h>
19 static void remove_nexthop(struct net *net, struct nexthop *nh,
20 struct nl_info *nlinfo);
22 #define NH_DEV_HASHBITS 8
23 #define NH_DEV_HASHSIZE (1U << NH_DEV_HASHBITS)
25 static const struct nla_policy rtm_nh_policy[NHA_MAX + 1] = {
26 [NHA_ID] = { .type = NLA_U32 },
27 [NHA_GROUP] = { .type = NLA_BINARY },
28 [NHA_GROUP_TYPE] = { .type = NLA_U16 },
29 [NHA_BLACKHOLE] = { .type = NLA_FLAG },
30 [NHA_OIF] = { .type = NLA_U32 },
31 [NHA_GATEWAY] = { .type = NLA_BINARY },
32 [NHA_ENCAP_TYPE] = { .type = NLA_U16 },
33 [NHA_ENCAP] = { .type = NLA_NESTED },
34 [NHA_GROUPS] = { .type = NLA_FLAG },
35 [NHA_MASTER] = { .type = NLA_U32 },
36 [NHA_FDB] = { .type = NLA_FLAG },
39 static int call_nexthop_notifiers(struct net *net,
40 enum nexthop_event_type event_type,
45 err = blocking_notifier_call_chain(&net->nexthop.notifier_chain,
47 return notifier_to_errno(err);
50 static unsigned int nh_dev_hashfn(unsigned int val)
52 unsigned int mask = NH_DEV_HASHSIZE - 1;
55 (val >> NH_DEV_HASHBITS) ^
56 (val >> (NH_DEV_HASHBITS * 2))) & mask;
59 static void nexthop_devhash_add(struct net *net, struct nh_info *nhi)
61 struct net_device *dev = nhi->fib_nhc.nhc_dev;
62 struct hlist_head *head;
67 hash = nh_dev_hashfn(dev->ifindex);
68 head = &net->nexthop.devhash[hash];
69 hlist_add_head(&nhi->dev_hash, head);
72 static void nexthop_free_mpath(struct nexthop *nh)
77 nhg = rcu_dereference_raw(nh->nh_grp);
78 for (i = 0; i < nhg->num_nh; ++i) {
79 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
81 WARN_ON(!list_empty(&nhge->nh_list));
82 nexthop_put(nhge->nh);
85 WARN_ON(nhg->spare == nhg);
91 static void nexthop_free_single(struct nexthop *nh)
95 nhi = rcu_dereference_raw(nh->nh_info);
96 switch (nhi->family) {
98 fib_nh_release(nh->net, &nhi->fib_nh);
101 ipv6_stub->fib6_nh_release(&nhi->fib6_nh);
107 void nexthop_free_rcu(struct rcu_head *head)
109 struct nexthop *nh = container_of(head, struct nexthop, rcu);
112 nexthop_free_mpath(nh);
114 nexthop_free_single(nh);
118 EXPORT_SYMBOL_GPL(nexthop_free_rcu);
120 static struct nexthop *nexthop_alloc(void)
124 nh = kzalloc(sizeof(struct nexthop), GFP_KERNEL);
126 INIT_LIST_HEAD(&nh->fi_list);
127 INIT_LIST_HEAD(&nh->f6i_list);
128 INIT_LIST_HEAD(&nh->grp_list);
129 INIT_LIST_HEAD(&nh->fdb_list);
134 static struct nh_group *nexthop_grp_alloc(u16 num_nh)
136 struct nh_group *nhg;
138 nhg = kzalloc(struct_size(nhg, nh_entries, num_nh), GFP_KERNEL);
140 nhg->num_nh = num_nh;
145 static void nh_base_seq_inc(struct net *net)
147 while (++net->nexthop.seq == 0)
151 /* no reference taken; rcu lock or rtnl must be held */
152 struct nexthop *nexthop_find_by_id(struct net *net, u32 id)
154 struct rb_node **pp, *parent = NULL, *next;
156 pp = &net->nexthop.rb_root.rb_node;
160 next = rcu_dereference_raw(*pp);
165 nh = rb_entry(parent, struct nexthop, rb_node);
168 else if (id > nh->id)
169 pp = &next->rb_right;
175 EXPORT_SYMBOL_GPL(nexthop_find_by_id);
177 /* used for auto id allocation; called with rtnl held */
178 static u32 nh_find_unused_id(struct net *net)
180 u32 id_start = net->nexthop.last_id_allocated;
183 net->nexthop.last_id_allocated++;
184 if (net->nexthop.last_id_allocated == id_start)
187 if (!nexthop_find_by_id(net, net->nexthop.last_id_allocated))
188 return net->nexthop.last_id_allocated;
193 static int nla_put_nh_group(struct sk_buff *skb, struct nh_group *nhg)
195 struct nexthop_grp *p;
196 size_t len = nhg->num_nh * sizeof(*p);
202 group_type = NEXTHOP_GRP_TYPE_MPATH;
204 if (nla_put_u16(skb, NHA_GROUP_TYPE, group_type))
205 goto nla_put_failure;
207 nla = nla_reserve(skb, NHA_GROUP, len);
209 goto nla_put_failure;
212 for (i = 0; i < nhg->num_nh; ++i) {
213 p->id = nhg->nh_entries[i].nh->id;
214 p->weight = nhg->nh_entries[i].weight - 1;
224 static int nh_fill_node(struct sk_buff *skb, struct nexthop *nh,
225 int event, u32 portid, u32 seq, unsigned int nlflags)
227 struct fib6_nh *fib6_nh;
228 struct fib_nh *fib_nh;
229 struct nlmsghdr *nlh;
233 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nhm), nlflags);
237 nhm = nlmsg_data(nlh);
238 nhm->nh_family = AF_UNSPEC;
239 nhm->nh_flags = nh->nh_flags;
240 nhm->nh_protocol = nh->protocol;
244 if (nla_put_u32(skb, NHA_ID, nh->id))
245 goto nla_put_failure;
248 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
250 if (nhg->fdb_nh && nla_put_flag(skb, NHA_FDB))
251 goto nla_put_failure;
252 if (nla_put_nh_group(skb, nhg))
253 goto nla_put_failure;
257 nhi = rtnl_dereference(nh->nh_info);
258 nhm->nh_family = nhi->family;
259 if (nhi->reject_nh) {
260 if (nla_put_flag(skb, NHA_BLACKHOLE))
261 goto nla_put_failure;
263 } else if (nhi->fdb_nh) {
264 if (nla_put_flag(skb, NHA_FDB))
265 goto nla_put_failure;
267 const struct net_device *dev;
269 dev = nhi->fib_nhc.nhc_dev;
270 if (dev && nla_put_u32(skb, NHA_OIF, dev->ifindex))
271 goto nla_put_failure;
274 nhm->nh_scope = nhi->fib_nhc.nhc_scope;
275 switch (nhi->family) {
277 fib_nh = &nhi->fib_nh;
278 if (fib_nh->fib_nh_gw_family &&
279 nla_put_be32(skb, NHA_GATEWAY, fib_nh->fib_nh_gw4))
280 goto nla_put_failure;
284 fib6_nh = &nhi->fib6_nh;
285 if (fib6_nh->fib_nh_gw_family &&
286 nla_put_in6_addr(skb, NHA_GATEWAY, &fib6_nh->fib_nh_gw6))
287 goto nla_put_failure;
291 if (nhi->fib_nhc.nhc_lwtstate &&
292 lwtunnel_fill_encap(skb, nhi->fib_nhc.nhc_lwtstate,
293 NHA_ENCAP, NHA_ENCAP_TYPE) < 0)
294 goto nla_put_failure;
301 nlmsg_cancel(skb, nlh);
305 static size_t nh_nlmsg_size_grp(struct nexthop *nh)
307 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
308 size_t sz = sizeof(struct nexthop_grp) * nhg->num_nh;
310 return nla_total_size(sz) +
311 nla_total_size(2); /* NHA_GROUP_TYPE */
314 static size_t nh_nlmsg_size_single(struct nexthop *nh)
316 struct nh_info *nhi = rtnl_dereference(nh->nh_info);
319 /* covers NHA_BLACKHOLE since NHA_OIF and BLACKHOLE
320 * are mutually exclusive
322 sz = nla_total_size(4); /* NHA_OIF */
324 switch (nhi->family) {
326 if (nhi->fib_nh.fib_nh_gw_family)
327 sz += nla_total_size(4); /* NHA_GATEWAY */
332 if (nhi->fib6_nh.fib_nh_gw_family)
333 sz += nla_total_size(sizeof(const struct in6_addr));
337 if (nhi->fib_nhc.nhc_lwtstate) {
338 sz += lwtunnel_get_encap_size(nhi->fib_nhc.nhc_lwtstate);
339 sz += nla_total_size(2); /* NHA_ENCAP_TYPE */
345 static size_t nh_nlmsg_size(struct nexthop *nh)
347 size_t sz = NLMSG_ALIGN(sizeof(struct nhmsg));
349 sz += nla_total_size(4); /* NHA_ID */
352 sz += nh_nlmsg_size_grp(nh);
354 sz += nh_nlmsg_size_single(nh);
359 static void nexthop_notify(int event, struct nexthop *nh, struct nl_info *info)
361 unsigned int nlflags = info->nlh ? info->nlh->nlmsg_flags : 0;
362 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
366 skb = nlmsg_new(nh_nlmsg_size(nh), gfp_any());
370 err = nh_fill_node(skb, nh, event, info->portid, seq, nlflags);
372 /* -EMSGSIZE implies BUG in nh_nlmsg_size() */
373 WARN_ON(err == -EMSGSIZE);
378 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_NEXTHOP,
379 info->nlh, gfp_any());
383 rtnl_set_sk_err(info->nl_net, RTNLGRP_NEXTHOP, err);
386 static bool valid_group_nh(struct nexthop *nh, unsigned int npaths,
387 bool *is_fdb, struct netlink_ext_ack *extack)
390 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
392 /* nested multipath (group within a group) is not
396 NL_SET_ERR_MSG(extack,
397 "Multipath group can not be a nexthop within a group");
400 *is_fdb = nhg->fdb_nh;
402 struct nh_info *nhi = rtnl_dereference(nh->nh_info);
404 if (nhi->reject_nh && npaths > 1) {
405 NL_SET_ERR_MSG(extack,
406 "Blackhole nexthop can not be used in a group with more than 1 path");
409 *is_fdb = nhi->fdb_nh;
415 static int nh_check_attr_fdb_group(struct nexthop *nh, u8 *nh_family,
416 struct netlink_ext_ack *extack)
420 nhi = rtnl_dereference(nh->nh_info);
423 NL_SET_ERR_MSG(extack, "FDB nexthop group can only have fdb nexthops");
427 if (*nh_family == AF_UNSPEC) {
428 *nh_family = nhi->family;
429 } else if (*nh_family != nhi->family) {
430 NL_SET_ERR_MSG(extack, "FDB nexthop group cannot have mixed family nexthops");
437 static int nh_check_attr_group(struct net *net, struct nlattr *tb[],
438 struct netlink_ext_ack *extack)
440 unsigned int len = nla_len(tb[NHA_GROUP]);
441 u8 nh_family = AF_UNSPEC;
442 struct nexthop_grp *nhg;
446 if (!len || len & (sizeof(struct nexthop_grp) - 1)) {
447 NL_SET_ERR_MSG(extack,
448 "Invalid length for nexthop group attribute");
452 /* convert len to number of nexthop ids */
455 nhg = nla_data(tb[NHA_GROUP]);
456 for (i = 0; i < len; ++i) {
457 if (nhg[i].resvd1 || nhg[i].resvd2) {
458 NL_SET_ERR_MSG(extack, "Reserved fields in nexthop_grp must be 0");
461 if (nhg[i].weight > 254) {
462 NL_SET_ERR_MSG(extack, "Invalid value for weight");
465 for (j = i + 1; j < len; ++j) {
466 if (nhg[i].id == nhg[j].id) {
467 NL_SET_ERR_MSG(extack, "Nexthop id can not be used twice in a group");
475 nhg = nla_data(tb[NHA_GROUP]);
476 for (i = 0; i < len; ++i) {
480 nh = nexthop_find_by_id(net, nhg[i].id);
482 NL_SET_ERR_MSG(extack, "Invalid nexthop id");
485 if (!valid_group_nh(nh, len, &is_fdb_nh, extack))
488 if (nhg_fdb && nh_check_attr_fdb_group(nh, &nh_family, extack))
491 if (!nhg_fdb && is_fdb_nh) {
492 NL_SET_ERR_MSG(extack, "Non FDB nexthop group cannot have fdb nexthops");
496 for (i = NHA_GROUP_TYPE + 1; i < __NHA_MAX; ++i) {
501 NL_SET_ERR_MSG(extack,
502 "No other attributes can be set in nexthop groups");
509 static bool ipv6_good_nh(const struct fib6_nh *nh)
511 int state = NUD_REACHABLE;
516 n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev, &nh->fib_nh_gw6);
518 state = n->nud_state;
520 rcu_read_unlock_bh();
522 return !!(state & NUD_VALID);
525 static bool ipv4_good_nh(const struct fib_nh *nh)
527 int state = NUD_REACHABLE;
532 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
533 (__force u32)nh->fib_nh_gw4);
535 state = n->nud_state;
537 rcu_read_unlock_bh();
539 return !!(state & NUD_VALID);
542 struct nexthop *nexthop_select_path(struct nexthop *nh, int hash)
544 struct nexthop *rc = NULL;
545 struct nh_group *nhg;
551 nhg = rcu_dereference(nh->nh_grp);
552 for (i = 0; i < nhg->num_nh; ++i) {
553 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
556 if (hash > atomic_read(&nhge->upper_bound))
559 nhi = rcu_dereference(nhge->nh->nh_info);
563 /* nexthops always check if it is good and does
564 * not rely on a sysctl for this behavior
566 switch (nhi->family) {
568 if (ipv4_good_nh(&nhi->fib_nh))
572 if (ipv6_good_nh(&nhi->fib6_nh))
583 EXPORT_SYMBOL_GPL(nexthop_select_path);
585 int nexthop_for_each_fib6_nh(struct nexthop *nh,
586 int (*cb)(struct fib6_nh *nh, void *arg),
593 struct nh_group *nhg;
596 nhg = rcu_dereference_rtnl(nh->nh_grp);
597 for (i = 0; i < nhg->num_nh; i++) {
598 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
600 nhi = rcu_dereference_rtnl(nhge->nh->nh_info);
601 err = cb(&nhi->fib6_nh, arg);
606 nhi = rcu_dereference_rtnl(nh->nh_info);
607 err = cb(&nhi->fib6_nh, arg);
614 EXPORT_SYMBOL_GPL(nexthop_for_each_fib6_nh);
616 static int check_src_addr(const struct in6_addr *saddr,
617 struct netlink_ext_ack *extack)
619 if (!ipv6_addr_any(saddr)) {
620 NL_SET_ERR_MSG(extack, "IPv6 routes using source address can not use nexthop objects");
626 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
627 struct netlink_ext_ack *extack)
632 /* fib6_src is unique to a fib6_info and limits the ability to cache
633 * routes in fib6_nh within a nexthop that is potentially shared
634 * across multiple fib entries. If the config wants to use source
635 * routing it can not use nexthop objects. mlxsw also does not allow
636 * fib6_src on routes.
638 if (cfg && check_src_addr(&cfg->fc_src, extack) < 0)
642 struct nh_group *nhg;
644 nhg = rtnl_dereference(nh->nh_grp);
647 is_fdb_nh = nhg->fdb_nh;
649 nhi = rtnl_dereference(nh->nh_info);
650 if (nhi->family == AF_INET)
652 is_fdb_nh = nhi->fdb_nh;
656 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
662 NL_SET_ERR_MSG(extack, "IPv6 routes can not use an IPv4 nexthop");
665 EXPORT_SYMBOL_GPL(fib6_check_nexthop);
667 /* if existing nexthop has ipv6 routes linked to it, need
668 * to verify this new spec works with ipv6
670 static int fib6_check_nh_list(struct nexthop *old, struct nexthop *new,
671 struct netlink_ext_ack *extack)
673 struct fib6_info *f6i;
675 if (list_empty(&old->f6i_list))
678 list_for_each_entry(f6i, &old->f6i_list, nh_list) {
679 if (check_src_addr(&f6i->fib6_src.addr, extack) < 0)
683 return fib6_check_nexthop(new, NULL, extack);
686 static int nexthop_check_scope(struct nh_info *nhi, u8 scope,
687 struct netlink_ext_ack *extack)
689 if (scope == RT_SCOPE_HOST && nhi->fib_nhc.nhc_gw_family) {
690 NL_SET_ERR_MSG(extack,
691 "Route with host scope can not have a gateway");
695 if (nhi->fib_nhc.nhc_flags & RTNH_F_ONLINK && scope >= RT_SCOPE_LINK) {
696 NL_SET_ERR_MSG(extack, "Scope mismatch with nexthop");
703 /* Invoked by fib add code to verify nexthop by id is ok with
704 * config for prefix; parts of fib_check_nh not done when nexthop
707 int fib_check_nexthop(struct nexthop *nh, u8 scope,
708 struct netlink_ext_ack *extack)
714 struct nh_group *nhg;
716 nhg = rtnl_dereference(nh->nh_grp);
718 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
723 if (scope == RT_SCOPE_HOST) {
724 NL_SET_ERR_MSG(extack, "Route with host scope can not have multiple nexthops");
729 /* all nexthops in a group have the same scope */
730 nhi = rtnl_dereference(nhg->nh_entries[0].nh->nh_info);
731 err = nexthop_check_scope(nhi, scope, extack);
733 nhi = rtnl_dereference(nh->nh_info);
735 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
739 err = nexthop_check_scope(nhi, scope, extack);
746 static int fib_check_nh_list(struct nexthop *old, struct nexthop *new,
747 struct netlink_ext_ack *extack)
751 list_for_each_entry(fi, &old->fi_list, nh_list) {
754 err = fib_check_nexthop(new, fi->fib_scope, extack);
761 static void nh_group_rebalance(struct nh_group *nhg)
767 for (i = 0; i < nhg->num_nh; ++i)
768 total += nhg->nh_entries[i].weight;
770 for (i = 0; i < nhg->num_nh; ++i) {
771 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
775 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31, total) - 1;
776 atomic_set(&nhge->upper_bound, upper_bound);
780 static void remove_nh_grp_entry(struct net *net, struct nh_grp_entry *nhge,
781 struct nl_info *nlinfo)
783 struct nh_grp_entry *nhges, *new_nhges;
784 struct nexthop *nhp = nhge->nh_parent;
785 struct nexthop *nh = nhge->nh;
786 struct nh_group *nhg, *newg;
791 nhg = rtnl_dereference(nhp->nh_grp);
794 /* last entry, keep it visible and remove the parent */
795 if (nhg->num_nh == 1) {
796 remove_nexthop(net, nhp, nlinfo);
800 newg->has_v4 = false;
801 newg->mpath = nhg->mpath;
802 newg->fdb_nh = nhg->fdb_nh;
803 newg->num_nh = nhg->num_nh;
805 /* copy old entries to new except the one getting removed */
806 nhges = nhg->nh_entries;
807 new_nhges = newg->nh_entries;
808 for (i = 0, j = 0; i < nhg->num_nh; ++i) {
811 /* current nexthop getting removed */
812 if (nhg->nh_entries[i].nh == nh) {
817 nhi = rtnl_dereference(nhges[i].nh->nh_info);
818 if (nhi->family == AF_INET)
821 list_del(&nhges[i].nh_list);
822 new_nhges[j].nh_parent = nhges[i].nh_parent;
823 new_nhges[j].nh = nhges[i].nh;
824 new_nhges[j].weight = nhges[i].weight;
825 list_add(&new_nhges[j].nh_list, &new_nhges[j].nh->grp_list);
829 nh_group_rebalance(newg);
830 rcu_assign_pointer(nhp->nh_grp, newg);
832 list_del(&nhge->nh_list);
833 nexthop_put(nhge->nh);
836 nexthop_notify(RTM_NEWNEXTHOP, nhp, nlinfo);
839 static void remove_nexthop_from_groups(struct net *net, struct nexthop *nh,
840 struct nl_info *nlinfo)
842 struct nh_grp_entry *nhge, *tmp;
844 list_for_each_entry_safe(nhge, tmp, &nh->grp_list, nh_list)
845 remove_nh_grp_entry(net, nhge, nlinfo);
847 /* make sure all see the newly published array before releasing rtnl */
851 static void remove_nexthop_group(struct nexthop *nh, struct nl_info *nlinfo)
853 struct nh_group *nhg = rcu_dereference_rtnl(nh->nh_grp);
854 int i, num_nh = nhg->num_nh;
856 for (i = 0; i < num_nh; ++i) {
857 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
859 if (WARN_ON(!nhge->nh))
862 list_del_init(&nhge->nh_list);
866 /* not called for nexthop replace */
867 static void __remove_nexthop_fib(struct net *net, struct nexthop *nh)
869 struct fib6_info *f6i, *tmp;
870 bool do_flush = false;
873 list_for_each_entry(fi, &nh->fi_list, nh_list) {
874 fi->fib_flags |= RTNH_F_DEAD;
880 /* ip6_del_rt removes the entry from this list hence the _safe */
881 list_for_each_entry_safe(f6i, tmp, &nh->f6i_list, nh_list) {
882 /* __ip6_del_rt does a release, so do a hold here */
884 ipv6_stub->ip6_del_rt(net, f6i,
885 !READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode));
889 static void __remove_nexthop(struct net *net, struct nexthop *nh,
890 struct nl_info *nlinfo)
892 __remove_nexthop_fib(net, nh);
895 remove_nexthop_group(nh, nlinfo);
899 nhi = rtnl_dereference(nh->nh_info);
900 if (nhi->fib_nhc.nhc_dev)
901 hlist_del(&nhi->dev_hash);
903 remove_nexthop_from_groups(net, nh, nlinfo);
907 static void remove_nexthop(struct net *net, struct nexthop *nh,
908 struct nl_info *nlinfo)
910 call_nexthop_notifiers(net, NEXTHOP_EVENT_DEL, nh);
912 /* remove from the tree */
913 rb_erase(&nh->rb_node, &net->nexthop.rb_root);
916 nexthop_notify(RTM_DELNEXTHOP, nh, nlinfo);
918 __remove_nexthop(net, nh, nlinfo);
919 nh_base_seq_inc(net);
924 /* if any FIB entries reference this nexthop, any dst entries
925 * need to be regenerated
927 static void nh_rt_cache_flush(struct net *net, struct nexthop *nh,
928 struct nexthop *replaced_nh)
930 struct fib6_info *f6i;
931 struct nh_group *nhg;
934 if (!list_empty(&nh->fi_list))
937 list_for_each_entry(f6i, &nh->f6i_list, nh_list)
938 ipv6_stub->fib6_update_sernum(net, f6i);
940 /* if an IPv6 group was replaced, we have to release all old
941 * dsts to make sure all refcounts are released
943 if (!replaced_nh->is_group)
946 /* new dsts must use only the new nexthop group */
949 nhg = rtnl_dereference(replaced_nh->nh_grp);
950 for (i = 0; i < nhg->num_nh; i++) {
951 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
952 struct nh_info *nhi = rtnl_dereference(nhge->nh->nh_info);
954 if (nhi->family == AF_INET6)
955 ipv6_stub->fib6_nh_release_dsts(&nhi->fib6_nh);
959 static int replace_nexthop_grp(struct net *net, struct nexthop *old,
961 struct netlink_ext_ack *extack)
963 struct nh_group *oldg, *newg;
966 if (!new->is_group) {
967 NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with a nexthop.");
971 oldg = rtnl_dereference(old->nh_grp);
972 newg = rtnl_dereference(new->nh_grp);
974 /* update parents - used by nexthop code for cleanup */
975 for (i = 0; i < newg->num_nh; i++)
976 newg->nh_entries[i].nh_parent = old;
978 rcu_assign_pointer(old->nh_grp, newg);
980 for (i = 0; i < oldg->num_nh; i++)
981 oldg->nh_entries[i].nh_parent = new;
983 rcu_assign_pointer(new->nh_grp, oldg);
988 static void nh_group_v4_update(struct nh_group *nhg)
990 struct nh_grp_entry *nhges;
994 nhges = nhg->nh_entries;
995 for (i = 0; i < nhg->num_nh; i++) {
998 nhi = rtnl_dereference(nhges[i].nh->nh_info);
999 if (nhi->family == AF_INET)
1002 nhg->has_v4 = has_v4;
1005 static int replace_nexthop_single(struct net *net, struct nexthop *old,
1006 struct nexthop *new,
1007 struct netlink_ext_ack *extack)
1009 struct nh_info *oldi, *newi;
1011 if (new->is_group) {
1012 NL_SET_ERR_MSG(extack, "Can not replace a nexthop with a nexthop group.");
1016 oldi = rtnl_dereference(old->nh_info);
1017 newi = rtnl_dereference(new->nh_info);
1019 newi->nh_parent = old;
1020 oldi->nh_parent = new;
1022 old->protocol = new->protocol;
1023 old->nh_flags = new->nh_flags;
1025 rcu_assign_pointer(old->nh_info, newi);
1026 rcu_assign_pointer(new->nh_info, oldi);
1028 /* When replacing an IPv4 nexthop with an IPv6 nexthop, potentially
1029 * update IPv4 indication in all the groups using the nexthop.
1031 if (oldi->family == AF_INET && newi->family == AF_INET6) {
1032 struct nh_grp_entry *nhge;
1034 list_for_each_entry(nhge, &old->grp_list, nh_list) {
1035 struct nexthop *nhp = nhge->nh_parent;
1036 struct nh_group *nhg;
1038 nhg = rtnl_dereference(nhp->nh_grp);
1039 nh_group_v4_update(nhg);
1046 static void __nexthop_replace_notify(struct net *net, struct nexthop *nh,
1047 struct nl_info *info)
1049 struct fib6_info *f6i;
1051 if (!list_empty(&nh->fi_list)) {
1052 struct fib_info *fi;
1054 /* expectation is a few fib_info per nexthop and then
1055 * a lot of routes per fib_info. So mark the fib_info
1056 * and then walk the fib tables once
1058 list_for_each_entry(fi, &nh->fi_list, nh_list)
1059 fi->nh_updated = true;
1061 fib_info_notify_update(net, info);
1063 list_for_each_entry(fi, &nh->fi_list, nh_list)
1064 fi->nh_updated = false;
1067 list_for_each_entry(f6i, &nh->f6i_list, nh_list)
1068 ipv6_stub->fib6_rt_update(net, f6i, info);
1071 /* send RTM_NEWROUTE with REPLACE flag set for all FIB entries
1072 * linked to this nexthop and for all groups that the nexthop
1075 static void nexthop_replace_notify(struct net *net, struct nexthop *nh,
1076 struct nl_info *info)
1078 struct nh_grp_entry *nhge;
1080 __nexthop_replace_notify(net, nh, info);
1082 list_for_each_entry(nhge, &nh->grp_list, nh_list)
1083 __nexthop_replace_notify(net, nhge->nh_parent, info);
1086 static int replace_nexthop(struct net *net, struct nexthop *old,
1087 struct nexthop *new, struct netlink_ext_ack *extack)
1089 bool new_is_reject = false;
1090 struct nh_grp_entry *nhge;
1093 /* check that existing FIB entries are ok with the
1094 * new nexthop definition
1096 err = fib_check_nh_list(old, new, extack);
1100 err = fib6_check_nh_list(old, new, extack);
1104 if (!new->is_group) {
1105 struct nh_info *nhi = rtnl_dereference(new->nh_info);
1107 new_is_reject = nhi->reject_nh;
1110 list_for_each_entry(nhge, &old->grp_list, nh_list) {
1111 /* if new nexthop is a blackhole, any groups using this
1112 * nexthop cannot have more than 1 path
1114 if (new_is_reject &&
1115 nexthop_num_path(nhge->nh_parent) > 1) {
1116 NL_SET_ERR_MSG(extack, "Blackhole nexthop can not be a member of a group with more than one path");
1120 err = fib_check_nh_list(nhge->nh_parent, new, extack);
1124 err = fib6_check_nh_list(nhge->nh_parent, new, extack);
1130 err = replace_nexthop_grp(net, old, new, extack);
1132 err = replace_nexthop_single(net, old, new, extack);
1135 nh_rt_cache_flush(net, old, new);
1137 __remove_nexthop(net, new, NULL);
1144 /* called with rtnl_lock held */
1145 static int insert_nexthop(struct net *net, struct nexthop *new_nh,
1146 struct nh_config *cfg, struct netlink_ext_ack *extack)
1148 struct rb_node **pp, *parent = NULL, *next;
1149 struct rb_root *root = &net->nexthop.rb_root;
1150 bool replace = !!(cfg->nlflags & NLM_F_REPLACE);
1151 bool create = !!(cfg->nlflags & NLM_F_CREATE);
1152 u32 new_id = new_nh->id;
1153 int replace_notify = 0;
1156 pp = &root->rb_node;
1166 nh = rb_entry(parent, struct nexthop, rb_node);
1167 if (new_id < nh->id) {
1168 pp = &next->rb_left;
1169 } else if (new_id > nh->id) {
1170 pp = &next->rb_right;
1171 } else if (replace) {
1172 rc = replace_nexthop(net, nh, new_nh, extack);
1174 new_nh = nh; /* send notification with old nh */
1179 /* id already exists and not a replace */
1184 if (replace && !create) {
1185 NL_SET_ERR_MSG(extack, "Replace specified without create and no entry exists");
1190 rb_link_node_rcu(&new_nh->rb_node, parent, pp);
1191 rb_insert_color(&new_nh->rb_node, root);
1195 nh_base_seq_inc(net);
1196 nexthop_notify(RTM_NEWNEXTHOP, new_nh, &cfg->nlinfo);
1197 if (replace_notify &&
1198 READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode))
1199 nexthop_replace_notify(net, new_nh, &cfg->nlinfo);
1206 /* remove all nexthops tied to a device being deleted */
1207 static void nexthop_flush_dev(struct net_device *dev, unsigned long event)
1209 unsigned int hash = nh_dev_hashfn(dev->ifindex);
1210 struct net *net = dev_net(dev);
1211 struct hlist_head *head = &net->nexthop.devhash[hash];
1212 struct hlist_node *n;
1213 struct nh_info *nhi;
1215 hlist_for_each_entry_safe(nhi, n, head, dev_hash) {
1216 if (nhi->fib_nhc.nhc_dev != dev)
1219 if (nhi->reject_nh &&
1220 (event == NETDEV_DOWN || event == NETDEV_CHANGE))
1223 remove_nexthop(net, nhi->nh_parent, NULL);
1227 /* rtnl; called when net namespace is deleted */
1228 static void flush_all_nexthops(struct net *net)
1230 struct rb_root *root = &net->nexthop.rb_root;
1231 struct rb_node *node;
1234 while ((node = rb_first(root))) {
1235 nh = rb_entry(node, struct nexthop, rb_node);
1236 remove_nexthop(net, nh, NULL);
1241 static struct nexthop *nexthop_create_group(struct net *net,
1242 struct nh_config *cfg)
1244 struct nlattr *grps_attr = cfg->nh_grp;
1245 struct nexthop_grp *entry = nla_data(grps_attr);
1246 u16 num_nh = nla_len(grps_attr) / sizeof(*entry);
1247 struct nh_group *nhg;
1251 if (WARN_ON(!num_nh))
1252 return ERR_PTR(-EINVAL);
1254 nh = nexthop_alloc();
1256 return ERR_PTR(-ENOMEM);
1260 nhg = nexthop_grp_alloc(num_nh);
1263 return ERR_PTR(-ENOMEM);
1266 /* spare group used for removals */
1267 nhg->spare = nexthop_grp_alloc(num_nh);
1271 return ERR_PTR(-ENOMEM);
1273 nhg->spare->spare = nhg;
1275 for (i = 0; i < nhg->num_nh; ++i) {
1276 struct nexthop *nhe;
1277 struct nh_info *nhi;
1279 nhe = nexthop_find_by_id(net, entry[i].id);
1280 if (!nexthop_get(nhe))
1283 nhi = rtnl_dereference(nhe->nh_info);
1284 if (nhi->family == AF_INET)
1287 nhg->nh_entries[i].nh = nhe;
1288 nhg->nh_entries[i].weight = entry[i].weight + 1;
1289 list_add(&nhg->nh_entries[i].nh_list, &nhe->grp_list);
1290 nhg->nh_entries[i].nh_parent = nh;
1293 if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_MPATH) {
1295 nh_group_rebalance(nhg);
1301 rcu_assign_pointer(nh->nh_grp, nhg);
1306 for (i--; i >= 0; --i) {
1307 list_del(&nhg->nh_entries[i].nh_list);
1308 nexthop_put(nhg->nh_entries[i].nh);
1315 return ERR_PTR(-ENOENT);
1318 static int nh_create_ipv4(struct net *net, struct nexthop *nh,
1319 struct nh_info *nhi, struct nh_config *cfg,
1320 struct netlink_ext_ack *extack)
1322 struct fib_nh *fib_nh = &nhi->fib_nh;
1323 struct fib_config fib_cfg = {
1324 .fc_oif = cfg->nh_ifindex,
1325 .fc_gw4 = cfg->gw.ipv4,
1326 .fc_gw_family = cfg->gw.ipv4 ? AF_INET : 0,
1327 .fc_flags = cfg->nh_flags,
1328 .fc_nlinfo = cfg->nlinfo,
1329 .fc_encap = cfg->nh_encap,
1330 .fc_encap_type = cfg->nh_encap_type,
1332 u32 tb_id = (cfg->dev ? l3mdev_fib_table(cfg->dev) : RT_TABLE_MAIN);
1335 err = fib_nh_init(net, fib_nh, &fib_cfg, 1, extack);
1337 fib_nh_release(net, fib_nh);
1344 /* sets nh_dev if successful */
1345 err = fib_check_nh(net, fib_nh, tb_id, 0, extack);
1347 nh->nh_flags = fib_nh->fib_nh_flags;
1348 fib_info_update_nhc_saddr(net, &fib_nh->nh_common,
1349 !fib_nh->fib_nh_scope ? 0 : fib_nh->fib_nh_scope - 1);
1351 fib_nh_release(net, fib_nh);
1357 static int nh_create_ipv6(struct net *net, struct nexthop *nh,
1358 struct nh_info *nhi, struct nh_config *cfg,
1359 struct netlink_ext_ack *extack)
1361 struct fib6_nh *fib6_nh = &nhi->fib6_nh;
1362 struct fib6_config fib6_cfg = {
1363 .fc_table = l3mdev_fib_table(cfg->dev),
1364 .fc_ifindex = cfg->nh_ifindex,
1365 .fc_gateway = cfg->gw.ipv6,
1366 .fc_flags = cfg->nh_flags,
1367 .fc_nlinfo = cfg->nlinfo,
1368 .fc_encap = cfg->nh_encap,
1369 .fc_encap_type = cfg->nh_encap_type,
1370 .fc_is_fdb = cfg->nh_fdb,
1374 if (!ipv6_addr_any(&cfg->gw.ipv6))
1375 fib6_cfg.fc_flags |= RTF_GATEWAY;
1377 /* sets nh_dev if successful */
1378 err = ipv6_stub->fib6_nh_init(net, fib6_nh, &fib6_cfg, GFP_KERNEL,
1381 /* IPv6 is not enabled, don't call fib6_nh_release */
1382 if (err == -EAFNOSUPPORT)
1384 ipv6_stub->fib6_nh_release(fib6_nh);
1386 nh->nh_flags = fib6_nh->fib_nh_flags;
1392 static struct nexthop *nexthop_create(struct net *net, struct nh_config *cfg,
1393 struct netlink_ext_ack *extack)
1395 struct nh_info *nhi;
1399 nh = nexthop_alloc();
1401 return ERR_PTR(-ENOMEM);
1403 nhi = kzalloc(sizeof(*nhi), GFP_KERNEL);
1406 return ERR_PTR(-ENOMEM);
1409 nh->nh_flags = cfg->nh_flags;
1412 nhi->nh_parent = nh;
1413 nhi->family = cfg->nh_family;
1414 nhi->fib_nhc.nhc_scope = RT_SCOPE_LINK;
1419 if (cfg->nh_blackhole) {
1421 cfg->nh_ifindex = net->loopback_dev->ifindex;
1424 switch (cfg->nh_family) {
1426 err = nh_create_ipv4(net, nh, nhi, cfg, extack);
1429 err = nh_create_ipv6(net, nh, nhi, cfg, extack);
1436 return ERR_PTR(err);
1439 /* add the entry to the device based hash */
1441 nexthop_devhash_add(net, nhi);
1443 rcu_assign_pointer(nh->nh_info, nhi);
1448 /* called with rtnl lock held */
1449 static struct nexthop *nexthop_add(struct net *net, struct nh_config *cfg,
1450 struct netlink_ext_ack *extack)
1455 if (cfg->nlflags & NLM_F_REPLACE && !cfg->nh_id) {
1456 NL_SET_ERR_MSG(extack, "Replace requires nexthop id");
1457 return ERR_PTR(-EINVAL);
1461 cfg->nh_id = nh_find_unused_id(net);
1463 NL_SET_ERR_MSG(extack, "No unused id");
1464 return ERR_PTR(-EINVAL);
1469 nh = nexthop_create_group(net, cfg);
1471 nh = nexthop_create(net, cfg, extack);
1476 refcount_set(&nh->refcnt, 1);
1477 nh->id = cfg->nh_id;
1478 nh->protocol = cfg->nh_protocol;
1481 err = insert_nexthop(net, nh, cfg, extack);
1483 __remove_nexthop(net, nh, NULL);
1491 static int rtm_to_nh_config(struct net *net, struct sk_buff *skb,
1492 struct nlmsghdr *nlh, struct nh_config *cfg,
1493 struct netlink_ext_ack *extack)
1495 struct nhmsg *nhm = nlmsg_data(nlh);
1496 struct nlattr *tb[NHA_MAX + 1];
1499 err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy,
1505 if (nhm->resvd || nhm->nh_scope) {
1506 NL_SET_ERR_MSG(extack, "Invalid values in ancillary header");
1509 if (nhm->nh_flags & ~NEXTHOP_VALID_USER_FLAGS) {
1510 NL_SET_ERR_MSG(extack, "Invalid nexthop flags in ancillary header");
1514 switch (nhm->nh_family) {
1523 NL_SET_ERR_MSG(extack, "Invalid address family");
1527 if (tb[NHA_GROUPS] || tb[NHA_MASTER]) {
1528 NL_SET_ERR_MSG(extack, "Invalid attributes in request");
1532 memset(cfg, 0, sizeof(*cfg));
1533 cfg->nlflags = nlh->nlmsg_flags;
1534 cfg->nlinfo.portid = NETLINK_CB(skb).portid;
1535 cfg->nlinfo.nlh = nlh;
1536 cfg->nlinfo.nl_net = net;
1538 cfg->nh_family = nhm->nh_family;
1539 cfg->nh_protocol = nhm->nh_protocol;
1540 cfg->nh_flags = nhm->nh_flags;
1543 cfg->nh_id = nla_get_u32(tb[NHA_ID]);
1546 if (tb[NHA_OIF] || tb[NHA_BLACKHOLE] ||
1547 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE]) {
1548 NL_SET_ERR_MSG(extack, "Fdb attribute can not be used with encap, oif or blackhole");
1551 if (nhm->nh_flags) {
1552 NL_SET_ERR_MSG(extack, "Unsupported nexthop flags in ancillary header");
1555 cfg->nh_fdb = nla_get_flag(tb[NHA_FDB]);
1558 if (tb[NHA_GROUP]) {
1559 if (nhm->nh_family != AF_UNSPEC) {
1560 NL_SET_ERR_MSG(extack, "Invalid family for group");
1563 cfg->nh_grp = tb[NHA_GROUP];
1565 cfg->nh_grp_type = NEXTHOP_GRP_TYPE_MPATH;
1566 if (tb[NHA_GROUP_TYPE])
1567 cfg->nh_grp_type = nla_get_u16(tb[NHA_GROUP_TYPE]);
1569 if (cfg->nh_grp_type > NEXTHOP_GRP_TYPE_MAX) {
1570 NL_SET_ERR_MSG(extack, "Invalid group type");
1573 err = nh_check_attr_group(net, tb, extack);
1575 /* no other attributes should be set */
1579 if (tb[NHA_BLACKHOLE]) {
1580 if (tb[NHA_GATEWAY] || tb[NHA_OIF] ||
1581 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE] || tb[NHA_FDB]) {
1582 NL_SET_ERR_MSG(extack, "Blackhole attribute can not be used with gateway, oif, encap or fdb");
1586 cfg->nh_blackhole = 1;
1591 if (!cfg->nh_fdb && !tb[NHA_OIF]) {
1592 NL_SET_ERR_MSG(extack, "Device attribute required for non-blackhole and non-fdb nexthops");
1596 if (!cfg->nh_fdb && tb[NHA_OIF]) {
1597 cfg->nh_ifindex = nla_get_u32(tb[NHA_OIF]);
1598 if (cfg->nh_ifindex)
1599 cfg->dev = __dev_get_by_index(net, cfg->nh_ifindex);
1602 NL_SET_ERR_MSG(extack, "Invalid device index");
1604 } else if (!(cfg->dev->flags & IFF_UP)) {
1605 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
1608 } else if (!netif_carrier_ok(cfg->dev)) {
1609 NL_SET_ERR_MSG(extack, "Carrier for nexthop device is down");
1616 if (tb[NHA_GATEWAY]) {
1617 struct nlattr *gwa = tb[NHA_GATEWAY];
1619 switch (cfg->nh_family) {
1621 if (nla_len(gwa) != sizeof(u32)) {
1622 NL_SET_ERR_MSG(extack, "Invalid gateway");
1625 cfg->gw.ipv4 = nla_get_be32(gwa);
1628 if (nla_len(gwa) != sizeof(struct in6_addr)) {
1629 NL_SET_ERR_MSG(extack, "Invalid gateway");
1632 cfg->gw.ipv6 = nla_get_in6_addr(gwa);
1635 NL_SET_ERR_MSG(extack,
1636 "Unknown address family for gateway");
1640 /* device only nexthop (no gateway) */
1641 if (cfg->nh_flags & RTNH_F_ONLINK) {
1642 NL_SET_ERR_MSG(extack,
1643 "ONLINK flag can not be set for nexthop without a gateway");
1648 if (tb[NHA_ENCAP]) {
1649 cfg->nh_encap = tb[NHA_ENCAP];
1651 if (!tb[NHA_ENCAP_TYPE]) {
1652 NL_SET_ERR_MSG(extack, "LWT encapsulation type is missing");
1656 cfg->nh_encap_type = nla_get_u16(tb[NHA_ENCAP_TYPE]);
1657 err = lwtunnel_valid_encap_type(cfg->nh_encap_type, extack);
1661 } else if (tb[NHA_ENCAP_TYPE]) {
1662 NL_SET_ERR_MSG(extack, "LWT encapsulation attribute is missing");
1673 static int rtm_new_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
1674 struct netlink_ext_ack *extack)
1676 struct net *net = sock_net(skb->sk);
1677 struct nh_config cfg;
1681 err = rtm_to_nh_config(net, skb, nlh, &cfg, extack);
1683 nh = nexthop_add(net, &cfg, extack);
1691 static int nh_valid_get_del_req(struct nlmsghdr *nlh, u32 *id,
1692 struct netlink_ext_ack *extack)
1694 struct nhmsg *nhm = nlmsg_data(nlh);
1695 struct nlattr *tb[NHA_MAX + 1];
1698 err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy,
1704 for (i = 0; i < __NHA_MAX; ++i) {
1712 NL_SET_ERR_MSG_ATTR(extack, tb[i],
1713 "Unexpected attribute in request");
1717 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) {
1718 NL_SET_ERR_MSG(extack, "Invalid values in header");
1723 NL_SET_ERR_MSG(extack, "Nexthop id is missing");
1727 *id = nla_get_u32(tb[NHA_ID]);
1729 NL_SET_ERR_MSG(extack, "Invalid nexthop id");
1737 static int rtm_del_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
1738 struct netlink_ext_ack *extack)
1740 struct net *net = sock_net(skb->sk);
1741 struct nl_info nlinfo = {
1744 .portid = NETLINK_CB(skb).portid,
1750 err = nh_valid_get_del_req(nlh, &id, extack);
1754 nh = nexthop_find_by_id(net, id);
1758 remove_nexthop(net, nh, &nlinfo);
1764 static int rtm_get_nexthop(struct sk_buff *in_skb, struct nlmsghdr *nlh,
1765 struct netlink_ext_ack *extack)
1767 struct net *net = sock_net(in_skb->sk);
1768 struct sk_buff *skb = NULL;
1773 err = nh_valid_get_del_req(nlh, &id, extack);
1778 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1783 nh = nexthop_find_by_id(net, id);
1787 err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, NETLINK_CB(in_skb).portid,
1790 WARN_ON(err == -EMSGSIZE);
1794 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
1802 static bool nh_dump_filtered(struct nexthop *nh, int dev_idx, int master_idx,
1803 bool group_filter, u8 family)
1805 const struct net_device *dev;
1806 const struct nh_info *nhi;
1808 if (group_filter && !nh->is_group)
1811 if (!dev_idx && !master_idx && !family)
1817 nhi = rtnl_dereference(nh->nh_info);
1818 if (family && nhi->family != family)
1821 dev = nhi->fib_nhc.nhc_dev;
1822 if (dev_idx && (!dev || dev->ifindex != dev_idx))
1826 struct net_device *master;
1831 master = netdev_master_upper_dev_get((struct net_device *)dev);
1832 if (!master || master->ifindex != master_idx)
1839 static int nh_valid_dump_req(const struct nlmsghdr *nlh, int *dev_idx,
1840 int *master_idx, bool *group_filter,
1841 bool *fdb_filter, struct netlink_callback *cb)
1843 struct netlink_ext_ack *extack = cb->extack;
1844 struct nlattr *tb[NHA_MAX + 1];
1849 err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy,
1854 for (i = 0; i <= NHA_MAX; ++i) {
1860 idx = nla_get_u32(tb[i]);
1861 if (idx > INT_MAX) {
1862 NL_SET_ERR_MSG(extack, "Invalid device index");
1868 idx = nla_get_u32(tb[i]);
1869 if (idx > INT_MAX) {
1870 NL_SET_ERR_MSG(extack, "Invalid master device index");
1876 *group_filter = true;
1882 NL_SET_ERR_MSG(extack, "Unsupported attribute in dump request");
1887 nhm = nlmsg_data(nlh);
1888 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) {
1889 NL_SET_ERR_MSG(extack, "Invalid values in header for nexthop dump request");
1897 static int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb)
1899 bool group_filter = false, fdb_filter = false;
1900 struct nhmsg *nhm = nlmsg_data(cb->nlh);
1901 int dev_filter_idx = 0, master_idx = 0;
1902 struct net *net = sock_net(skb->sk);
1903 struct rb_root *root = &net->nexthop.rb_root;
1904 struct rb_node *node;
1908 err = nh_valid_dump_req(cb->nlh, &dev_filter_idx, &master_idx,
1909 &group_filter, &fdb_filter, cb);
1913 s_idx = cb->args[0];
1914 for (node = rb_first(root); node; node = rb_next(node)) {
1920 nh = rb_entry(node, struct nexthop, rb_node);
1921 if (nh_dump_filtered(nh, dev_filter_idx, master_idx,
1922 group_filter, nhm->nh_family))
1925 err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP,
1926 NETLINK_CB(cb->skb).portid,
1927 cb->nlh->nlmsg_seq, NLM_F_MULTI);
1929 if (likely(skb->len))
1942 cb->seq = net->nexthop.seq;
1943 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1948 static void nexthop_sync_mtu(struct net_device *dev, u32 orig_mtu)
1950 unsigned int hash = nh_dev_hashfn(dev->ifindex);
1951 struct net *net = dev_net(dev);
1952 struct hlist_head *head = &net->nexthop.devhash[hash];
1953 struct hlist_node *n;
1954 struct nh_info *nhi;
1956 hlist_for_each_entry_safe(nhi, n, head, dev_hash) {
1957 if (nhi->fib_nhc.nhc_dev == dev) {
1958 if (nhi->family == AF_INET)
1959 fib_nhc_update_mtu(&nhi->fib_nhc, dev->mtu,
1966 static int nh_netdev_event(struct notifier_block *this,
1967 unsigned long event, void *ptr)
1969 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1970 struct netdev_notifier_info_ext *info_ext;
1974 case NETDEV_UNREGISTER:
1975 nexthop_flush_dev(dev, event);
1978 if (!(dev_get_flags(dev) & (IFF_RUNNING | IFF_LOWER_UP)))
1979 nexthop_flush_dev(dev, event);
1981 case NETDEV_CHANGEMTU:
1983 nexthop_sync_mtu(dev, info_ext->ext.mtu);
1984 rt_cache_flush(dev_net(dev));
1990 static struct notifier_block nh_netdev_notifier = {
1991 .notifier_call = nh_netdev_event,
1994 int register_nexthop_notifier(struct net *net, struct notifier_block *nb)
1996 return blocking_notifier_chain_register(&net->nexthop.notifier_chain,
1999 EXPORT_SYMBOL(register_nexthop_notifier);
2001 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb)
2003 return blocking_notifier_chain_unregister(&net->nexthop.notifier_chain,
2006 EXPORT_SYMBOL(unregister_nexthop_notifier);
2008 static void __net_exit nexthop_net_exit(struct net *net)
2011 flush_all_nexthops(net);
2013 kfree(net->nexthop.devhash);
2016 static int __net_init nexthop_net_init(struct net *net)
2018 size_t sz = sizeof(struct hlist_head) * NH_DEV_HASHSIZE;
2020 net->nexthop.rb_root = RB_ROOT;
2021 net->nexthop.devhash = kzalloc(sz, GFP_KERNEL);
2022 if (!net->nexthop.devhash)
2024 BLOCKING_INIT_NOTIFIER_HEAD(&net->nexthop.notifier_chain);
2029 static struct pernet_operations nexthop_net_ops = {
2030 .init = nexthop_net_init,
2031 .exit = nexthop_net_exit,
2034 static int __init nexthop_init(void)
2036 register_pernet_subsys(&nexthop_net_ops);
2038 register_netdevice_notifier(&nh_netdev_notifier);
2040 rtnl_register(PF_UNSPEC, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
2041 rtnl_register(PF_UNSPEC, RTM_DELNEXTHOP, rtm_del_nexthop, NULL, 0);
2042 rtnl_register(PF_UNSPEC, RTM_GETNEXTHOP, rtm_get_nexthop,
2043 rtm_dump_nexthop, 0);
2045 rtnl_register(PF_INET, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
2046 rtnl_register(PF_INET, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0);
2048 rtnl_register(PF_INET6, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
2049 rtnl_register(PF_INET6, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0);
2053 subsys_initcall(nexthop_init);