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_new[] = {
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_FDB] = { .type = NLA_FLAG },
37 static const struct nla_policy rtm_nh_policy_get[] = {
38 [NHA_ID] = { .type = NLA_U32 },
41 static const struct nla_policy rtm_nh_policy_dump[] = {
42 [NHA_OIF] = { .type = NLA_U32 },
43 [NHA_GROUPS] = { .type = NLA_FLAG },
44 [NHA_MASTER] = { .type = NLA_U32 },
45 [NHA_FDB] = { .type = NLA_FLAG },
48 static bool nexthop_notifiers_is_empty(struct net *net)
50 return !net->nexthop.notifier_chain.head;
54 __nh_notifier_single_info_init(struct nh_notifier_single_info *nh_info,
55 const struct nexthop *nh)
57 struct nh_info *nhi = rtnl_dereference(nh->nh_info);
59 nh_info->dev = nhi->fib_nhc.nhc_dev;
60 nh_info->gw_family = nhi->fib_nhc.nhc_gw_family;
61 if (nh_info->gw_family == AF_INET)
62 nh_info->ipv4 = nhi->fib_nhc.nhc_gw.ipv4;
63 else if (nh_info->gw_family == AF_INET6)
64 nh_info->ipv6 = nhi->fib_nhc.nhc_gw.ipv6;
66 nh_info->is_reject = nhi->reject_nh;
67 nh_info->is_fdb = nhi->fdb_nh;
68 nh_info->has_encap = !!nhi->fib_nhc.nhc_lwtstate;
71 static int nh_notifier_single_info_init(struct nh_notifier_info *info,
72 const struct nexthop *nh)
74 info->type = NH_NOTIFIER_INFO_TYPE_SINGLE;
75 info->nh = kzalloc(sizeof(*info->nh), GFP_KERNEL);
79 __nh_notifier_single_info_init(info->nh, nh);
84 static void nh_notifier_single_info_fini(struct nh_notifier_info *info)
89 static int nh_notifier_grp_info_init(struct nh_notifier_info *info,
90 const struct nexthop *nh)
92 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
93 u16 num_nh = nhg->num_nh;
96 info->type = NH_NOTIFIER_INFO_TYPE_GRP;
97 info->nh_grp = kzalloc(struct_size(info->nh_grp, nh_entries, num_nh),
102 info->nh_grp->num_nh = num_nh;
103 info->nh_grp->is_fdb = nhg->fdb_nh;
105 for (i = 0; i < num_nh; i++) {
106 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
108 info->nh_grp->nh_entries[i].id = nhge->nh->id;
109 info->nh_grp->nh_entries[i].weight = nhge->weight;
110 __nh_notifier_single_info_init(&info->nh_grp->nh_entries[i].nh,
117 static void nh_notifier_grp_info_fini(struct nh_notifier_info *info)
122 static int nh_notifier_info_init(struct nh_notifier_info *info,
123 const struct nexthop *nh)
128 return nh_notifier_grp_info_init(info, nh);
130 return nh_notifier_single_info_init(info, nh);
133 static void nh_notifier_info_fini(struct nh_notifier_info *info,
134 const struct nexthop *nh)
137 nh_notifier_grp_info_fini(info);
139 nh_notifier_single_info_fini(info);
142 static int call_nexthop_notifiers(struct net *net,
143 enum nexthop_event_type event_type,
145 struct netlink_ext_ack *extack)
147 struct nh_notifier_info info = {
155 if (nexthop_notifiers_is_empty(net))
158 err = nh_notifier_info_init(&info, nh);
160 NL_SET_ERR_MSG(extack, "Failed to initialize nexthop notifier info");
164 err = blocking_notifier_call_chain(&net->nexthop.notifier_chain,
166 nh_notifier_info_fini(&info, nh);
168 return notifier_to_errno(err);
171 static int call_nexthop_notifier(struct notifier_block *nb, struct net *net,
172 enum nexthop_event_type event_type,
174 struct netlink_ext_ack *extack)
176 struct nh_notifier_info info = {
182 err = nh_notifier_info_init(&info, nh);
186 err = nb->notifier_call(nb, event_type, &info);
187 nh_notifier_info_fini(&info, nh);
189 return notifier_to_errno(err);
192 static unsigned int nh_dev_hashfn(unsigned int val)
194 unsigned int mask = NH_DEV_HASHSIZE - 1;
197 (val >> NH_DEV_HASHBITS) ^
198 (val >> (NH_DEV_HASHBITS * 2))) & mask;
201 static void nexthop_devhash_add(struct net *net, struct nh_info *nhi)
203 struct net_device *dev = nhi->fib_nhc.nhc_dev;
204 struct hlist_head *head;
209 hash = nh_dev_hashfn(dev->ifindex);
210 head = &net->nexthop.devhash[hash];
211 hlist_add_head(&nhi->dev_hash, head);
214 static void nexthop_free_group(struct nexthop *nh)
216 struct nh_group *nhg;
219 nhg = rcu_dereference_raw(nh->nh_grp);
220 for (i = 0; i < nhg->num_nh; ++i) {
221 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
223 WARN_ON(!list_empty(&nhge->nh_list));
224 nexthop_put(nhge->nh);
227 WARN_ON(nhg->spare == nhg);
233 static void nexthop_free_single(struct nexthop *nh)
237 nhi = rcu_dereference_raw(nh->nh_info);
238 switch (nhi->family) {
240 fib_nh_release(nh->net, &nhi->fib_nh);
243 ipv6_stub->fib6_nh_release(&nhi->fib6_nh);
249 void nexthop_free_rcu(struct rcu_head *head)
251 struct nexthop *nh = container_of(head, struct nexthop, rcu);
254 nexthop_free_group(nh);
256 nexthop_free_single(nh);
260 EXPORT_SYMBOL_GPL(nexthop_free_rcu);
262 static struct nexthop *nexthop_alloc(void)
266 nh = kzalloc(sizeof(struct nexthop), GFP_KERNEL);
268 INIT_LIST_HEAD(&nh->fi_list);
269 INIT_LIST_HEAD(&nh->f6i_list);
270 INIT_LIST_HEAD(&nh->grp_list);
271 INIT_LIST_HEAD(&nh->fdb_list);
276 static struct nh_group *nexthop_grp_alloc(u16 num_nh)
278 struct nh_group *nhg;
280 nhg = kzalloc(struct_size(nhg, nh_entries, num_nh), GFP_KERNEL);
282 nhg->num_nh = num_nh;
287 static void nh_base_seq_inc(struct net *net)
289 while (++net->nexthop.seq == 0)
293 /* no reference taken; rcu lock or rtnl must be held */
294 struct nexthop *nexthop_find_by_id(struct net *net, u32 id)
296 struct rb_node **pp, *parent = NULL, *next;
298 pp = &net->nexthop.rb_root.rb_node;
302 next = rcu_dereference_raw(*pp);
307 nh = rb_entry(parent, struct nexthop, rb_node);
310 else if (id > nh->id)
311 pp = &next->rb_right;
317 EXPORT_SYMBOL_GPL(nexthop_find_by_id);
319 /* used for auto id allocation; called with rtnl held */
320 static u32 nh_find_unused_id(struct net *net)
322 u32 id_start = net->nexthop.last_id_allocated;
325 net->nexthop.last_id_allocated++;
326 if (net->nexthop.last_id_allocated == id_start)
329 if (!nexthop_find_by_id(net, net->nexthop.last_id_allocated))
330 return net->nexthop.last_id_allocated;
335 static int nla_put_nh_group(struct sk_buff *skb, struct nh_group *nhg)
337 struct nexthop_grp *p;
338 size_t len = nhg->num_nh * sizeof(*p);
344 group_type = NEXTHOP_GRP_TYPE_MPATH;
346 if (nla_put_u16(skb, NHA_GROUP_TYPE, group_type))
347 goto nla_put_failure;
349 nla = nla_reserve(skb, NHA_GROUP, len);
351 goto nla_put_failure;
354 for (i = 0; i < nhg->num_nh; ++i) {
355 p->id = nhg->nh_entries[i].nh->id;
356 p->weight = nhg->nh_entries[i].weight - 1;
366 static int nh_fill_node(struct sk_buff *skb, struct nexthop *nh,
367 int event, u32 portid, u32 seq, unsigned int nlflags)
369 struct fib6_nh *fib6_nh;
370 struct fib_nh *fib_nh;
371 struct nlmsghdr *nlh;
375 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nhm), nlflags);
379 nhm = nlmsg_data(nlh);
380 nhm->nh_family = AF_UNSPEC;
381 nhm->nh_flags = nh->nh_flags;
382 nhm->nh_protocol = nh->protocol;
386 if (nla_put_u32(skb, NHA_ID, nh->id))
387 goto nla_put_failure;
390 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
392 if (nhg->fdb_nh && nla_put_flag(skb, NHA_FDB))
393 goto nla_put_failure;
394 if (nla_put_nh_group(skb, nhg))
395 goto nla_put_failure;
399 nhi = rtnl_dereference(nh->nh_info);
400 nhm->nh_family = nhi->family;
401 if (nhi->reject_nh) {
402 if (nla_put_flag(skb, NHA_BLACKHOLE))
403 goto nla_put_failure;
405 } else if (nhi->fdb_nh) {
406 if (nla_put_flag(skb, NHA_FDB))
407 goto nla_put_failure;
409 const struct net_device *dev;
411 dev = nhi->fib_nhc.nhc_dev;
412 if (dev && nla_put_u32(skb, NHA_OIF, dev->ifindex))
413 goto nla_put_failure;
416 nhm->nh_scope = nhi->fib_nhc.nhc_scope;
417 switch (nhi->family) {
419 fib_nh = &nhi->fib_nh;
420 if (fib_nh->fib_nh_gw_family &&
421 nla_put_be32(skb, NHA_GATEWAY, fib_nh->fib_nh_gw4))
422 goto nla_put_failure;
426 fib6_nh = &nhi->fib6_nh;
427 if (fib6_nh->fib_nh_gw_family &&
428 nla_put_in6_addr(skb, NHA_GATEWAY, &fib6_nh->fib_nh_gw6))
429 goto nla_put_failure;
433 if (nhi->fib_nhc.nhc_lwtstate &&
434 lwtunnel_fill_encap(skb, nhi->fib_nhc.nhc_lwtstate,
435 NHA_ENCAP, NHA_ENCAP_TYPE) < 0)
436 goto nla_put_failure;
443 nlmsg_cancel(skb, nlh);
447 static size_t nh_nlmsg_size_grp(struct nexthop *nh)
449 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
450 size_t sz = sizeof(struct nexthop_grp) * nhg->num_nh;
452 return nla_total_size(sz) +
453 nla_total_size(2); /* NHA_GROUP_TYPE */
456 static size_t nh_nlmsg_size_single(struct nexthop *nh)
458 struct nh_info *nhi = rtnl_dereference(nh->nh_info);
461 /* covers NHA_BLACKHOLE since NHA_OIF and BLACKHOLE
462 * are mutually exclusive
464 sz = nla_total_size(4); /* NHA_OIF */
466 switch (nhi->family) {
468 if (nhi->fib_nh.fib_nh_gw_family)
469 sz += nla_total_size(4); /* NHA_GATEWAY */
474 if (nhi->fib6_nh.fib_nh_gw_family)
475 sz += nla_total_size(sizeof(const struct in6_addr));
479 if (nhi->fib_nhc.nhc_lwtstate) {
480 sz += lwtunnel_get_encap_size(nhi->fib_nhc.nhc_lwtstate);
481 sz += nla_total_size(2); /* NHA_ENCAP_TYPE */
487 static size_t nh_nlmsg_size(struct nexthop *nh)
489 size_t sz = NLMSG_ALIGN(sizeof(struct nhmsg));
491 sz += nla_total_size(4); /* NHA_ID */
494 sz += nh_nlmsg_size_grp(nh);
496 sz += nh_nlmsg_size_single(nh);
501 static void nexthop_notify(int event, struct nexthop *nh, struct nl_info *info)
503 unsigned int nlflags = info->nlh ? info->nlh->nlmsg_flags : 0;
504 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
508 skb = nlmsg_new(nh_nlmsg_size(nh), gfp_any());
512 err = nh_fill_node(skb, nh, event, info->portid, seq, nlflags);
514 /* -EMSGSIZE implies BUG in nh_nlmsg_size() */
515 WARN_ON(err == -EMSGSIZE);
520 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_NEXTHOP,
521 info->nlh, gfp_any());
525 rtnl_set_sk_err(info->nl_net, RTNLGRP_NEXTHOP, err);
528 static bool valid_group_nh(struct nexthop *nh, unsigned int npaths,
529 bool *is_fdb, struct netlink_ext_ack *extack)
532 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
534 /* nested multipath (group within a group) is not
538 NL_SET_ERR_MSG(extack,
539 "Multipath group can not be a nexthop within a group");
542 *is_fdb = nhg->fdb_nh;
544 struct nh_info *nhi = rtnl_dereference(nh->nh_info);
546 if (nhi->reject_nh && npaths > 1) {
547 NL_SET_ERR_MSG(extack,
548 "Blackhole nexthop can not be used in a group with more than 1 path");
551 *is_fdb = nhi->fdb_nh;
557 static int nh_check_attr_fdb_group(struct nexthop *nh, u8 *nh_family,
558 struct netlink_ext_ack *extack)
562 nhi = rtnl_dereference(nh->nh_info);
565 NL_SET_ERR_MSG(extack, "FDB nexthop group can only have fdb nexthops");
569 if (*nh_family == AF_UNSPEC) {
570 *nh_family = nhi->family;
571 } else if (*nh_family != nhi->family) {
572 NL_SET_ERR_MSG(extack, "FDB nexthop group cannot have mixed family nexthops");
579 static int nh_check_attr_group(struct net *net,
580 struct nlattr *tb[], size_t tb_size,
581 struct netlink_ext_ack *extack)
583 unsigned int len = nla_len(tb[NHA_GROUP]);
584 u8 nh_family = AF_UNSPEC;
585 struct nexthop_grp *nhg;
589 if (!len || len & (sizeof(struct nexthop_grp) - 1)) {
590 NL_SET_ERR_MSG(extack,
591 "Invalid length for nexthop group attribute");
595 /* convert len to number of nexthop ids */
598 nhg = nla_data(tb[NHA_GROUP]);
599 for (i = 0; i < len; ++i) {
600 if (nhg[i].resvd1 || nhg[i].resvd2) {
601 NL_SET_ERR_MSG(extack, "Reserved fields in nexthop_grp must be 0");
604 if (nhg[i].weight > 254) {
605 NL_SET_ERR_MSG(extack, "Invalid value for weight");
608 for (j = i + 1; j < len; ++j) {
609 if (nhg[i].id == nhg[j].id) {
610 NL_SET_ERR_MSG(extack, "Nexthop id can not be used twice in a group");
618 nhg = nla_data(tb[NHA_GROUP]);
619 for (i = 0; i < len; ++i) {
623 nh = nexthop_find_by_id(net, nhg[i].id);
625 NL_SET_ERR_MSG(extack, "Invalid nexthop id");
628 if (!valid_group_nh(nh, len, &is_fdb_nh, extack))
631 if (nhg_fdb && nh_check_attr_fdb_group(nh, &nh_family, extack))
634 if (!nhg_fdb && is_fdb_nh) {
635 NL_SET_ERR_MSG(extack, "Non FDB nexthop group cannot have fdb nexthops");
639 for (i = NHA_GROUP_TYPE + 1; i < tb_size; ++i) {
644 NL_SET_ERR_MSG(extack,
645 "No other attributes can be set in nexthop groups");
652 static bool ipv6_good_nh(const struct fib6_nh *nh)
654 int state = NUD_REACHABLE;
659 n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev, &nh->fib_nh_gw6);
661 state = n->nud_state;
663 rcu_read_unlock_bh();
665 return !!(state & NUD_VALID);
668 static bool ipv4_good_nh(const struct fib_nh *nh)
670 int state = NUD_REACHABLE;
675 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
676 (__force u32)nh->fib_nh_gw4);
678 state = n->nud_state;
680 rcu_read_unlock_bh();
682 return !!(state & NUD_VALID);
685 static struct nexthop *nexthop_select_path_mp(struct nh_group *nhg, int hash)
687 struct nexthop *rc = NULL;
690 for (i = 0; i < nhg->num_nh; ++i) {
691 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
694 if (hash > atomic_read(&nhge->mpath.upper_bound))
697 nhi = rcu_dereference(nhge->nh->nh_info);
701 /* nexthops always check if it is good and does
702 * not rely on a sysctl for this behavior
704 switch (nhi->family) {
706 if (ipv4_good_nh(&nhi->fib_nh))
710 if (ipv6_good_nh(&nhi->fib6_nh))
722 struct nexthop *nexthop_select_path(struct nexthop *nh, int hash)
724 struct nh_group *nhg;
729 nhg = rcu_dereference(nh->nh_grp);
731 return nexthop_select_path_mp(nhg, hash);
736 EXPORT_SYMBOL_GPL(nexthop_select_path);
738 int nexthop_for_each_fib6_nh(struct nexthop *nh,
739 int (*cb)(struct fib6_nh *nh, void *arg),
746 struct nh_group *nhg;
749 nhg = rcu_dereference_rtnl(nh->nh_grp);
750 for (i = 0; i < nhg->num_nh; i++) {
751 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
753 nhi = rcu_dereference_rtnl(nhge->nh->nh_info);
754 err = cb(&nhi->fib6_nh, arg);
759 nhi = rcu_dereference_rtnl(nh->nh_info);
760 err = cb(&nhi->fib6_nh, arg);
767 EXPORT_SYMBOL_GPL(nexthop_for_each_fib6_nh);
769 static int check_src_addr(const struct in6_addr *saddr,
770 struct netlink_ext_ack *extack)
772 if (!ipv6_addr_any(saddr)) {
773 NL_SET_ERR_MSG(extack, "IPv6 routes using source address can not use nexthop objects");
779 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
780 struct netlink_ext_ack *extack)
785 /* fib6_src is unique to a fib6_info and limits the ability to cache
786 * routes in fib6_nh within a nexthop that is potentially shared
787 * across multiple fib entries. If the config wants to use source
788 * routing it can not use nexthop objects. mlxsw also does not allow
789 * fib6_src on routes.
791 if (cfg && check_src_addr(&cfg->fc_src, extack) < 0)
795 struct nh_group *nhg;
797 nhg = rtnl_dereference(nh->nh_grp);
800 is_fdb_nh = nhg->fdb_nh;
802 nhi = rtnl_dereference(nh->nh_info);
803 if (nhi->family == AF_INET)
805 is_fdb_nh = nhi->fdb_nh;
809 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
815 NL_SET_ERR_MSG(extack, "IPv6 routes can not use an IPv4 nexthop");
818 EXPORT_SYMBOL_GPL(fib6_check_nexthop);
820 /* if existing nexthop has ipv6 routes linked to it, need
821 * to verify this new spec works with ipv6
823 static int fib6_check_nh_list(struct nexthop *old, struct nexthop *new,
824 struct netlink_ext_ack *extack)
826 struct fib6_info *f6i;
828 if (list_empty(&old->f6i_list))
831 list_for_each_entry(f6i, &old->f6i_list, nh_list) {
832 if (check_src_addr(&f6i->fib6_src.addr, extack) < 0)
836 return fib6_check_nexthop(new, NULL, extack);
839 static int nexthop_check_scope(struct nh_info *nhi, u8 scope,
840 struct netlink_ext_ack *extack)
842 if (scope == RT_SCOPE_HOST && nhi->fib_nhc.nhc_gw_family) {
843 NL_SET_ERR_MSG(extack,
844 "Route with host scope can not have a gateway");
848 if (nhi->fib_nhc.nhc_flags & RTNH_F_ONLINK && scope >= RT_SCOPE_LINK) {
849 NL_SET_ERR_MSG(extack, "Scope mismatch with nexthop");
856 /* Invoked by fib add code to verify nexthop by id is ok with
857 * config for prefix; parts of fib_check_nh not done when nexthop
860 int fib_check_nexthop(struct nexthop *nh, u8 scope,
861 struct netlink_ext_ack *extack)
867 struct nh_group *nhg;
869 nhg = rtnl_dereference(nh->nh_grp);
871 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
876 if (scope == RT_SCOPE_HOST) {
877 NL_SET_ERR_MSG(extack, "Route with host scope can not have multiple nexthops");
882 /* all nexthops in a group have the same scope */
883 nhi = rtnl_dereference(nhg->nh_entries[0].nh->nh_info);
884 err = nexthop_check_scope(nhi, scope, extack);
886 nhi = rtnl_dereference(nh->nh_info);
888 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
892 err = nexthop_check_scope(nhi, scope, extack);
899 static int fib_check_nh_list(struct nexthop *old, struct nexthop *new,
900 struct netlink_ext_ack *extack)
904 list_for_each_entry(fi, &old->fi_list, nh_list) {
907 err = fib_check_nexthop(new, fi->fib_scope, extack);
914 static void nh_group_rebalance(struct nh_group *nhg)
920 for (i = 0; i < nhg->num_nh; ++i)
921 total += nhg->nh_entries[i].weight;
923 for (i = 0; i < nhg->num_nh; ++i) {
924 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
928 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31, total) - 1;
929 atomic_set(&nhge->mpath.upper_bound, upper_bound);
933 static void remove_nh_grp_entry(struct net *net, struct nh_grp_entry *nhge,
934 struct nl_info *nlinfo)
936 struct nh_grp_entry *nhges, *new_nhges;
937 struct nexthop *nhp = nhge->nh_parent;
938 struct netlink_ext_ack extack;
939 struct nexthop *nh = nhge->nh;
940 struct nh_group *nhg, *newg;
945 nhg = rtnl_dereference(nhp->nh_grp);
948 /* last entry, keep it visible and remove the parent */
949 if (nhg->num_nh == 1) {
950 remove_nexthop(net, nhp, nlinfo);
954 newg->has_v4 = false;
955 newg->mpath = nhg->mpath;
956 newg->fdb_nh = nhg->fdb_nh;
957 newg->num_nh = nhg->num_nh;
959 /* copy old entries to new except the one getting removed */
960 nhges = nhg->nh_entries;
961 new_nhges = newg->nh_entries;
962 for (i = 0, j = 0; i < nhg->num_nh; ++i) {
965 /* current nexthop getting removed */
966 if (nhg->nh_entries[i].nh == nh) {
971 nhi = rtnl_dereference(nhges[i].nh->nh_info);
972 if (nhi->family == AF_INET)
975 list_del(&nhges[i].nh_list);
976 new_nhges[j].nh_parent = nhges[i].nh_parent;
977 new_nhges[j].nh = nhges[i].nh;
978 new_nhges[j].weight = nhges[i].weight;
979 list_add(&new_nhges[j].nh_list, &new_nhges[j].nh->grp_list);
983 nh_group_rebalance(newg);
984 rcu_assign_pointer(nhp->nh_grp, newg);
986 list_del(&nhge->nh_list);
987 nexthop_put(nhge->nh);
989 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, nhp, &extack);
991 pr_err("%s\n", extack._msg);
994 nexthop_notify(RTM_NEWNEXTHOP, nhp, nlinfo);
997 static void remove_nexthop_from_groups(struct net *net, struct nexthop *nh,
998 struct nl_info *nlinfo)
1000 struct nh_grp_entry *nhge, *tmp;
1002 list_for_each_entry_safe(nhge, tmp, &nh->grp_list, nh_list)
1003 remove_nh_grp_entry(net, nhge, nlinfo);
1005 /* make sure all see the newly published array before releasing rtnl */
1009 static void remove_nexthop_group(struct nexthop *nh, struct nl_info *nlinfo)
1011 struct nh_group *nhg = rcu_dereference_rtnl(nh->nh_grp);
1012 int i, num_nh = nhg->num_nh;
1014 for (i = 0; i < num_nh; ++i) {
1015 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
1017 if (WARN_ON(!nhge->nh))
1020 list_del_init(&nhge->nh_list);
1024 /* not called for nexthop replace */
1025 static void __remove_nexthop_fib(struct net *net, struct nexthop *nh)
1027 struct fib6_info *f6i, *tmp;
1028 bool do_flush = false;
1029 struct fib_info *fi;
1031 list_for_each_entry(fi, &nh->fi_list, nh_list) {
1032 fi->fib_flags |= RTNH_F_DEAD;
1038 /* ip6_del_rt removes the entry from this list hence the _safe */
1039 list_for_each_entry_safe(f6i, tmp, &nh->f6i_list, nh_list) {
1040 /* __ip6_del_rt does a release, so do a hold here */
1041 fib6_info_hold(f6i);
1042 ipv6_stub->ip6_del_rt(net, f6i,
1043 !net->ipv4.sysctl_nexthop_compat_mode);
1047 static void __remove_nexthop(struct net *net, struct nexthop *nh,
1048 struct nl_info *nlinfo)
1050 __remove_nexthop_fib(net, nh);
1053 remove_nexthop_group(nh, nlinfo);
1055 struct nh_info *nhi;
1057 nhi = rtnl_dereference(nh->nh_info);
1058 if (nhi->fib_nhc.nhc_dev)
1059 hlist_del(&nhi->dev_hash);
1061 remove_nexthop_from_groups(net, nh, nlinfo);
1065 static void remove_nexthop(struct net *net, struct nexthop *nh,
1066 struct nl_info *nlinfo)
1068 call_nexthop_notifiers(net, NEXTHOP_EVENT_DEL, nh, NULL);
1070 /* remove from the tree */
1071 rb_erase(&nh->rb_node, &net->nexthop.rb_root);
1074 nexthop_notify(RTM_DELNEXTHOP, nh, nlinfo);
1076 __remove_nexthop(net, nh, nlinfo);
1077 nh_base_seq_inc(net);
1082 /* if any FIB entries reference this nexthop, any dst entries
1083 * need to be regenerated
1085 static void nh_rt_cache_flush(struct net *net, struct nexthop *nh)
1087 struct fib6_info *f6i;
1089 if (!list_empty(&nh->fi_list))
1090 rt_cache_flush(net);
1092 list_for_each_entry(f6i, &nh->f6i_list, nh_list)
1093 ipv6_stub->fib6_update_sernum(net, f6i);
1096 static int replace_nexthop_grp(struct net *net, struct nexthop *old,
1097 struct nexthop *new,
1098 struct netlink_ext_ack *extack)
1100 struct nh_group *oldg, *newg;
1103 if (!new->is_group) {
1104 NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with a nexthop.");
1108 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new, extack);
1112 oldg = rtnl_dereference(old->nh_grp);
1113 newg = rtnl_dereference(new->nh_grp);
1115 /* update parents - used by nexthop code for cleanup */
1116 for (i = 0; i < newg->num_nh; i++)
1117 newg->nh_entries[i].nh_parent = old;
1119 rcu_assign_pointer(old->nh_grp, newg);
1121 for (i = 0; i < oldg->num_nh; i++)
1122 oldg->nh_entries[i].nh_parent = new;
1124 rcu_assign_pointer(new->nh_grp, oldg);
1129 static void nh_group_v4_update(struct nh_group *nhg)
1131 struct nh_grp_entry *nhges;
1132 bool has_v4 = false;
1135 nhges = nhg->nh_entries;
1136 for (i = 0; i < nhg->num_nh; i++) {
1137 struct nh_info *nhi;
1139 nhi = rtnl_dereference(nhges[i].nh->nh_info);
1140 if (nhi->family == AF_INET)
1143 nhg->has_v4 = has_v4;
1146 static int replace_nexthop_single(struct net *net, struct nexthop *old,
1147 struct nexthop *new,
1148 struct netlink_ext_ack *extack)
1150 u8 old_protocol, old_nh_flags;
1151 struct nh_info *oldi, *newi;
1152 struct nh_grp_entry *nhge;
1155 if (new->is_group) {
1156 NL_SET_ERR_MSG(extack, "Can not replace a nexthop with a nexthop group.");
1160 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new, extack);
1164 /* Hardware flags were set on 'old' as 'new' is not in the red-black
1165 * tree. Therefore, inherit the flags from 'old' to 'new'.
1167 new->nh_flags |= old->nh_flags & (RTNH_F_OFFLOAD | RTNH_F_TRAP);
1169 oldi = rtnl_dereference(old->nh_info);
1170 newi = rtnl_dereference(new->nh_info);
1172 newi->nh_parent = old;
1173 oldi->nh_parent = new;
1175 old_protocol = old->protocol;
1176 old_nh_flags = old->nh_flags;
1178 old->protocol = new->protocol;
1179 old->nh_flags = new->nh_flags;
1181 rcu_assign_pointer(old->nh_info, newi);
1182 rcu_assign_pointer(new->nh_info, oldi);
1184 /* Send a replace notification for all the groups using the nexthop. */
1185 list_for_each_entry(nhge, &old->grp_list, nh_list) {
1186 struct nexthop *nhp = nhge->nh_parent;
1188 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, nhp,
1194 /* When replacing an IPv4 nexthop with an IPv6 nexthop, potentially
1195 * update IPv4 indication in all the groups using the nexthop.
1197 if (oldi->family == AF_INET && newi->family == AF_INET6) {
1198 list_for_each_entry(nhge, &old->grp_list, nh_list) {
1199 struct nexthop *nhp = nhge->nh_parent;
1200 struct nh_group *nhg;
1202 nhg = rtnl_dereference(nhp->nh_grp);
1203 nh_group_v4_update(nhg);
1210 rcu_assign_pointer(new->nh_info, newi);
1211 rcu_assign_pointer(old->nh_info, oldi);
1212 old->nh_flags = old_nh_flags;
1213 old->protocol = old_protocol;
1214 oldi->nh_parent = old;
1215 newi->nh_parent = new;
1216 list_for_each_entry_continue_reverse(nhge, &old->grp_list, nh_list) {
1217 struct nexthop *nhp = nhge->nh_parent;
1219 call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, nhp, extack);
1221 call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, old, extack);
1225 static void __nexthop_replace_notify(struct net *net, struct nexthop *nh,
1226 struct nl_info *info)
1228 struct fib6_info *f6i;
1230 if (!list_empty(&nh->fi_list)) {
1231 struct fib_info *fi;
1233 /* expectation is a few fib_info per nexthop and then
1234 * a lot of routes per fib_info. So mark the fib_info
1235 * and then walk the fib tables once
1237 list_for_each_entry(fi, &nh->fi_list, nh_list)
1238 fi->nh_updated = true;
1240 fib_info_notify_update(net, info);
1242 list_for_each_entry(fi, &nh->fi_list, nh_list)
1243 fi->nh_updated = false;
1246 list_for_each_entry(f6i, &nh->f6i_list, nh_list)
1247 ipv6_stub->fib6_rt_update(net, f6i, info);
1250 /* send RTM_NEWROUTE with REPLACE flag set for all FIB entries
1251 * linked to this nexthop and for all groups that the nexthop
1254 static void nexthop_replace_notify(struct net *net, struct nexthop *nh,
1255 struct nl_info *info)
1257 struct nh_grp_entry *nhge;
1259 __nexthop_replace_notify(net, nh, info);
1261 list_for_each_entry(nhge, &nh->grp_list, nh_list)
1262 __nexthop_replace_notify(net, nhge->nh_parent, info);
1265 static int replace_nexthop(struct net *net, struct nexthop *old,
1266 struct nexthop *new, struct netlink_ext_ack *extack)
1268 bool new_is_reject = false;
1269 struct nh_grp_entry *nhge;
1272 /* check that existing FIB entries are ok with the
1273 * new nexthop definition
1275 err = fib_check_nh_list(old, new, extack);
1279 err = fib6_check_nh_list(old, new, extack);
1283 if (!new->is_group) {
1284 struct nh_info *nhi = rtnl_dereference(new->nh_info);
1286 new_is_reject = nhi->reject_nh;
1289 list_for_each_entry(nhge, &old->grp_list, nh_list) {
1290 /* if new nexthop is a blackhole, any groups using this
1291 * nexthop cannot have more than 1 path
1293 if (new_is_reject &&
1294 nexthop_num_path(nhge->nh_parent) > 1) {
1295 NL_SET_ERR_MSG(extack, "Blackhole nexthop can not be a member of a group with more than one path");
1299 err = fib_check_nh_list(nhge->nh_parent, new, extack);
1303 err = fib6_check_nh_list(nhge->nh_parent, new, extack);
1309 err = replace_nexthop_grp(net, old, new, extack);
1311 err = replace_nexthop_single(net, old, new, extack);
1314 nh_rt_cache_flush(net, old);
1316 __remove_nexthop(net, new, NULL);
1323 /* called with rtnl_lock held */
1324 static int insert_nexthop(struct net *net, struct nexthop *new_nh,
1325 struct nh_config *cfg, struct netlink_ext_ack *extack)
1327 struct rb_node **pp, *parent = NULL, *next;
1328 struct rb_root *root = &net->nexthop.rb_root;
1329 bool replace = !!(cfg->nlflags & NLM_F_REPLACE);
1330 bool create = !!(cfg->nlflags & NLM_F_CREATE);
1331 u32 new_id = new_nh->id;
1332 int replace_notify = 0;
1335 pp = &root->rb_node;
1345 nh = rb_entry(parent, struct nexthop, rb_node);
1346 if (new_id < nh->id) {
1347 pp = &next->rb_left;
1348 } else if (new_id > nh->id) {
1349 pp = &next->rb_right;
1350 } else if (replace) {
1351 rc = replace_nexthop(net, nh, new_nh, extack);
1353 new_nh = nh; /* send notification with old nh */
1358 /* id already exists and not a replace */
1363 if (replace && !create) {
1364 NL_SET_ERR_MSG(extack, "Replace specified without create and no entry exists");
1369 rb_link_node_rcu(&new_nh->rb_node, parent, pp);
1370 rb_insert_color(&new_nh->rb_node, root);
1372 rc = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new_nh, extack);
1374 rb_erase(&new_nh->rb_node, &net->nexthop.rb_root);
1378 nh_base_seq_inc(net);
1379 nexthop_notify(RTM_NEWNEXTHOP, new_nh, &cfg->nlinfo);
1380 if (replace_notify && net->ipv4.sysctl_nexthop_compat_mode)
1381 nexthop_replace_notify(net, new_nh, &cfg->nlinfo);
1388 /* remove all nexthops tied to a device being deleted */
1389 static void nexthop_flush_dev(struct net_device *dev)
1391 unsigned int hash = nh_dev_hashfn(dev->ifindex);
1392 struct net *net = dev_net(dev);
1393 struct hlist_head *head = &net->nexthop.devhash[hash];
1394 struct hlist_node *n;
1395 struct nh_info *nhi;
1397 hlist_for_each_entry_safe(nhi, n, head, dev_hash) {
1398 if (nhi->fib_nhc.nhc_dev != dev)
1401 remove_nexthop(net, nhi->nh_parent, NULL);
1405 /* rtnl; called when net namespace is deleted */
1406 static void flush_all_nexthops(struct net *net)
1408 struct rb_root *root = &net->nexthop.rb_root;
1409 struct rb_node *node;
1412 while ((node = rb_first(root))) {
1413 nh = rb_entry(node, struct nexthop, rb_node);
1414 remove_nexthop(net, nh, NULL);
1419 static struct nexthop *nexthop_create_group(struct net *net,
1420 struct nh_config *cfg)
1422 struct nlattr *grps_attr = cfg->nh_grp;
1423 struct nexthop_grp *entry = nla_data(grps_attr);
1424 u16 num_nh = nla_len(grps_attr) / sizeof(*entry);
1425 struct nh_group *nhg;
1429 if (WARN_ON(!num_nh))
1430 return ERR_PTR(-EINVAL);
1432 nh = nexthop_alloc();
1434 return ERR_PTR(-ENOMEM);
1438 nhg = nexthop_grp_alloc(num_nh);
1441 return ERR_PTR(-ENOMEM);
1444 /* spare group used for removals */
1445 nhg->spare = nexthop_grp_alloc(num_nh);
1449 return ERR_PTR(-ENOMEM);
1451 nhg->spare->spare = nhg;
1453 for (i = 0; i < nhg->num_nh; ++i) {
1454 struct nexthop *nhe;
1455 struct nh_info *nhi;
1457 nhe = nexthop_find_by_id(net, entry[i].id);
1458 if (!nexthop_get(nhe))
1461 nhi = rtnl_dereference(nhe->nh_info);
1462 if (nhi->family == AF_INET)
1465 nhg->nh_entries[i].nh = nhe;
1466 nhg->nh_entries[i].weight = entry[i].weight + 1;
1467 list_add(&nhg->nh_entries[i].nh_list, &nhe->grp_list);
1468 nhg->nh_entries[i].nh_parent = nh;
1471 if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_MPATH)
1474 WARN_ON_ONCE(nhg->mpath != 1);
1477 nh_group_rebalance(nhg);
1482 rcu_assign_pointer(nh->nh_grp, nhg);
1487 for (i--; i >= 0; --i) {
1488 list_del(&nhg->nh_entries[i].nh_list);
1489 nexthop_put(nhg->nh_entries[i].nh);
1496 return ERR_PTR(-ENOENT);
1499 static int nh_create_ipv4(struct net *net, struct nexthop *nh,
1500 struct nh_info *nhi, struct nh_config *cfg,
1501 struct netlink_ext_ack *extack)
1503 struct fib_nh *fib_nh = &nhi->fib_nh;
1504 struct fib_config fib_cfg = {
1505 .fc_oif = cfg->nh_ifindex,
1506 .fc_gw4 = cfg->gw.ipv4,
1507 .fc_gw_family = cfg->gw.ipv4 ? AF_INET : 0,
1508 .fc_flags = cfg->nh_flags,
1509 .fc_encap = cfg->nh_encap,
1510 .fc_encap_type = cfg->nh_encap_type,
1512 u32 tb_id = (cfg->dev ? l3mdev_fib_table(cfg->dev) : RT_TABLE_MAIN);
1515 err = fib_nh_init(net, fib_nh, &fib_cfg, 1, extack);
1517 fib_nh_release(net, fib_nh);
1524 /* sets nh_dev if successful */
1525 err = fib_check_nh(net, fib_nh, tb_id, 0, extack);
1527 nh->nh_flags = fib_nh->fib_nh_flags;
1528 fib_info_update_nhc_saddr(net, &fib_nh->nh_common,
1529 fib_nh->fib_nh_scope);
1531 fib_nh_release(net, fib_nh);
1537 static int nh_create_ipv6(struct net *net, struct nexthop *nh,
1538 struct nh_info *nhi, struct nh_config *cfg,
1539 struct netlink_ext_ack *extack)
1541 struct fib6_nh *fib6_nh = &nhi->fib6_nh;
1542 struct fib6_config fib6_cfg = {
1543 .fc_table = l3mdev_fib_table(cfg->dev),
1544 .fc_ifindex = cfg->nh_ifindex,
1545 .fc_gateway = cfg->gw.ipv6,
1546 .fc_flags = cfg->nh_flags,
1547 .fc_encap = cfg->nh_encap,
1548 .fc_encap_type = cfg->nh_encap_type,
1549 .fc_is_fdb = cfg->nh_fdb,
1553 if (!ipv6_addr_any(&cfg->gw.ipv6))
1554 fib6_cfg.fc_flags |= RTF_GATEWAY;
1556 /* sets nh_dev if successful */
1557 err = ipv6_stub->fib6_nh_init(net, fib6_nh, &fib6_cfg, GFP_KERNEL,
1560 ipv6_stub->fib6_nh_release(fib6_nh);
1562 nh->nh_flags = fib6_nh->fib_nh_flags;
1567 static struct nexthop *nexthop_create(struct net *net, struct nh_config *cfg,
1568 struct netlink_ext_ack *extack)
1570 struct nh_info *nhi;
1574 nh = nexthop_alloc();
1576 return ERR_PTR(-ENOMEM);
1578 nhi = kzalloc(sizeof(*nhi), GFP_KERNEL);
1581 return ERR_PTR(-ENOMEM);
1584 nh->nh_flags = cfg->nh_flags;
1587 nhi->nh_parent = nh;
1588 nhi->family = cfg->nh_family;
1589 nhi->fib_nhc.nhc_scope = RT_SCOPE_LINK;
1594 if (cfg->nh_blackhole) {
1596 cfg->nh_ifindex = net->loopback_dev->ifindex;
1599 switch (cfg->nh_family) {
1601 err = nh_create_ipv4(net, nh, nhi, cfg, extack);
1604 err = nh_create_ipv6(net, nh, nhi, cfg, extack);
1611 return ERR_PTR(err);
1614 /* add the entry to the device based hash */
1616 nexthop_devhash_add(net, nhi);
1618 rcu_assign_pointer(nh->nh_info, nhi);
1623 /* called with rtnl lock held */
1624 static struct nexthop *nexthop_add(struct net *net, struct nh_config *cfg,
1625 struct netlink_ext_ack *extack)
1630 if (cfg->nlflags & NLM_F_REPLACE && !cfg->nh_id) {
1631 NL_SET_ERR_MSG(extack, "Replace requires nexthop id");
1632 return ERR_PTR(-EINVAL);
1636 cfg->nh_id = nh_find_unused_id(net);
1638 NL_SET_ERR_MSG(extack, "No unused id");
1639 return ERR_PTR(-EINVAL);
1644 nh = nexthop_create_group(net, cfg);
1646 nh = nexthop_create(net, cfg, extack);
1651 refcount_set(&nh->refcnt, 1);
1652 nh->id = cfg->nh_id;
1653 nh->protocol = cfg->nh_protocol;
1656 err = insert_nexthop(net, nh, cfg, extack);
1658 __remove_nexthop(net, nh, NULL);
1666 static int rtm_to_nh_config(struct net *net, struct sk_buff *skb,
1667 struct nlmsghdr *nlh, struct nh_config *cfg,
1668 struct netlink_ext_ack *extack)
1670 struct nhmsg *nhm = nlmsg_data(nlh);
1671 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_new)];
1674 err = nlmsg_parse(nlh, sizeof(*nhm), tb,
1675 ARRAY_SIZE(rtm_nh_policy_new) - 1,
1676 rtm_nh_policy_new, extack);
1681 if (nhm->resvd || nhm->nh_scope) {
1682 NL_SET_ERR_MSG(extack, "Invalid values in ancillary header");
1685 if (nhm->nh_flags & ~NEXTHOP_VALID_USER_FLAGS) {
1686 NL_SET_ERR_MSG(extack, "Invalid nexthop flags in ancillary header");
1690 switch (nhm->nh_family) {
1699 NL_SET_ERR_MSG(extack, "Invalid address family");
1703 memset(cfg, 0, sizeof(*cfg));
1704 cfg->nlflags = nlh->nlmsg_flags;
1705 cfg->nlinfo.portid = NETLINK_CB(skb).portid;
1706 cfg->nlinfo.nlh = nlh;
1707 cfg->nlinfo.nl_net = net;
1709 cfg->nh_family = nhm->nh_family;
1710 cfg->nh_protocol = nhm->nh_protocol;
1711 cfg->nh_flags = nhm->nh_flags;
1714 cfg->nh_id = nla_get_u32(tb[NHA_ID]);
1717 if (tb[NHA_OIF] || tb[NHA_BLACKHOLE] ||
1718 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE]) {
1719 NL_SET_ERR_MSG(extack, "Fdb attribute can not be used with encap, oif or blackhole");
1722 if (nhm->nh_flags) {
1723 NL_SET_ERR_MSG(extack, "Unsupported nexthop flags in ancillary header");
1726 cfg->nh_fdb = nla_get_flag(tb[NHA_FDB]);
1729 if (tb[NHA_GROUP]) {
1730 if (nhm->nh_family != AF_UNSPEC) {
1731 NL_SET_ERR_MSG(extack, "Invalid family for group");
1734 cfg->nh_grp = tb[NHA_GROUP];
1736 cfg->nh_grp_type = NEXTHOP_GRP_TYPE_MPATH;
1737 if (tb[NHA_GROUP_TYPE])
1738 cfg->nh_grp_type = nla_get_u16(tb[NHA_GROUP_TYPE]);
1740 if (cfg->nh_grp_type > NEXTHOP_GRP_TYPE_MAX) {
1741 NL_SET_ERR_MSG(extack, "Invalid group type");
1744 err = nh_check_attr_group(net, tb, ARRAY_SIZE(tb), extack);
1746 /* no other attributes should be set */
1750 if (tb[NHA_BLACKHOLE]) {
1751 if (tb[NHA_GATEWAY] || tb[NHA_OIF] ||
1752 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE] || tb[NHA_FDB]) {
1753 NL_SET_ERR_MSG(extack, "Blackhole attribute can not be used with gateway, oif, encap or fdb");
1757 cfg->nh_blackhole = 1;
1762 if (!cfg->nh_fdb && !tb[NHA_OIF]) {
1763 NL_SET_ERR_MSG(extack, "Device attribute required for non-blackhole and non-fdb nexthops");
1767 if (!cfg->nh_fdb && tb[NHA_OIF]) {
1768 cfg->nh_ifindex = nla_get_u32(tb[NHA_OIF]);
1769 if (cfg->nh_ifindex)
1770 cfg->dev = __dev_get_by_index(net, cfg->nh_ifindex);
1773 NL_SET_ERR_MSG(extack, "Invalid device index");
1775 } else if (!(cfg->dev->flags & IFF_UP)) {
1776 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
1779 } else if (!netif_carrier_ok(cfg->dev)) {
1780 NL_SET_ERR_MSG(extack, "Carrier for nexthop device is down");
1787 if (tb[NHA_GATEWAY]) {
1788 struct nlattr *gwa = tb[NHA_GATEWAY];
1790 switch (cfg->nh_family) {
1792 if (nla_len(gwa) != sizeof(u32)) {
1793 NL_SET_ERR_MSG(extack, "Invalid gateway");
1796 cfg->gw.ipv4 = nla_get_be32(gwa);
1799 if (nla_len(gwa) != sizeof(struct in6_addr)) {
1800 NL_SET_ERR_MSG(extack, "Invalid gateway");
1803 cfg->gw.ipv6 = nla_get_in6_addr(gwa);
1806 NL_SET_ERR_MSG(extack,
1807 "Unknown address family for gateway");
1811 /* device only nexthop (no gateway) */
1812 if (cfg->nh_flags & RTNH_F_ONLINK) {
1813 NL_SET_ERR_MSG(extack,
1814 "ONLINK flag can not be set for nexthop without a gateway");
1819 if (tb[NHA_ENCAP]) {
1820 cfg->nh_encap = tb[NHA_ENCAP];
1822 if (!tb[NHA_ENCAP_TYPE]) {
1823 NL_SET_ERR_MSG(extack, "LWT encapsulation type is missing");
1827 cfg->nh_encap_type = nla_get_u16(tb[NHA_ENCAP_TYPE]);
1828 err = lwtunnel_valid_encap_type(cfg->nh_encap_type, extack);
1832 } else if (tb[NHA_ENCAP_TYPE]) {
1833 NL_SET_ERR_MSG(extack, "LWT encapsulation attribute is missing");
1844 static int rtm_new_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
1845 struct netlink_ext_ack *extack)
1847 struct net *net = sock_net(skb->sk);
1848 struct nh_config cfg;
1852 err = rtm_to_nh_config(net, skb, nlh, &cfg, extack);
1854 nh = nexthop_add(net, &cfg, extack);
1862 static int nh_valid_get_del_req(struct nlmsghdr *nlh, u32 *id,
1863 struct netlink_ext_ack *extack)
1865 struct nhmsg *nhm = nlmsg_data(nlh);
1866 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_get)];
1869 err = nlmsg_parse(nlh, sizeof(*nhm), tb,
1870 ARRAY_SIZE(rtm_nh_policy_get) - 1,
1871 rtm_nh_policy_get, extack);
1876 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) {
1877 NL_SET_ERR_MSG(extack, "Invalid values in header");
1882 NL_SET_ERR_MSG(extack, "Nexthop id is missing");
1886 *id = nla_get_u32(tb[NHA_ID]);
1888 NL_SET_ERR_MSG(extack, "Invalid nexthop id");
1896 static int rtm_del_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
1897 struct netlink_ext_ack *extack)
1899 struct net *net = sock_net(skb->sk);
1900 struct nl_info nlinfo = {
1903 .portid = NETLINK_CB(skb).portid,
1909 err = nh_valid_get_del_req(nlh, &id, extack);
1913 nh = nexthop_find_by_id(net, id);
1917 remove_nexthop(net, nh, &nlinfo);
1923 static int rtm_get_nexthop(struct sk_buff *in_skb, struct nlmsghdr *nlh,
1924 struct netlink_ext_ack *extack)
1926 struct net *net = sock_net(in_skb->sk);
1927 struct sk_buff *skb = NULL;
1932 err = nh_valid_get_del_req(nlh, &id, extack);
1937 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1942 nh = nexthop_find_by_id(net, id);
1946 err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, NETLINK_CB(in_skb).portid,
1949 WARN_ON(err == -EMSGSIZE);
1953 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
1961 static bool nh_dump_filtered(struct nexthop *nh, int dev_idx, int master_idx,
1962 bool group_filter, u8 family)
1964 const struct net_device *dev;
1965 const struct nh_info *nhi;
1967 if (group_filter && !nh->is_group)
1970 if (!dev_idx && !master_idx && !family)
1976 nhi = rtnl_dereference(nh->nh_info);
1977 if (family && nhi->family != family)
1980 dev = nhi->fib_nhc.nhc_dev;
1981 if (dev_idx && (!dev || dev->ifindex != dev_idx))
1985 struct net_device *master;
1990 master = netdev_master_upper_dev_get((struct net_device *)dev);
1991 if (!master || master->ifindex != master_idx)
1998 static int nh_valid_dump_req(const struct nlmsghdr *nlh, int *dev_idx,
1999 int *master_idx, bool *group_filter,
2000 bool *fdb_filter, struct netlink_callback *cb)
2002 struct netlink_ext_ack *extack = cb->extack;
2003 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_dump)];
2008 err = nlmsg_parse(nlh, sizeof(*nhm), tb,
2009 ARRAY_SIZE(rtm_nh_policy_dump) - 1,
2010 rtm_nh_policy_dump, NULL);
2015 idx = nla_get_u32(tb[NHA_OIF]);
2016 if (idx > INT_MAX) {
2017 NL_SET_ERR_MSG(extack, "Invalid device index");
2022 if (tb[NHA_MASTER]) {
2023 idx = nla_get_u32(tb[NHA_MASTER]);
2024 if (idx > INT_MAX) {
2025 NL_SET_ERR_MSG(extack, "Invalid master device index");
2030 *group_filter = nla_get_flag(tb[NHA_GROUPS]);
2031 *fdb_filter = nla_get_flag(tb[NHA_FDB]);
2033 nhm = nlmsg_data(nlh);
2034 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) {
2035 NL_SET_ERR_MSG(extack, "Invalid values in header for nexthop dump request");
2043 static int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb)
2045 bool group_filter = false, fdb_filter = false;
2046 struct nhmsg *nhm = nlmsg_data(cb->nlh);
2047 int dev_filter_idx = 0, master_idx = 0;
2048 struct net *net = sock_net(skb->sk);
2049 struct rb_root *root = &net->nexthop.rb_root;
2050 struct rb_node *node;
2054 err = nh_valid_dump_req(cb->nlh, &dev_filter_idx, &master_idx,
2055 &group_filter, &fdb_filter, cb);
2059 s_idx = cb->args[0];
2060 for (node = rb_first(root); node; node = rb_next(node)) {
2066 nh = rb_entry(node, struct nexthop, rb_node);
2067 if (nh_dump_filtered(nh, dev_filter_idx, master_idx,
2068 group_filter, nhm->nh_family))
2071 err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP,
2072 NETLINK_CB(cb->skb).portid,
2073 cb->nlh->nlmsg_seq, NLM_F_MULTI);
2075 if (likely(skb->len))
2088 cb->seq = net->nexthop.seq;
2089 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2094 static void nexthop_sync_mtu(struct net_device *dev, u32 orig_mtu)
2096 unsigned int hash = nh_dev_hashfn(dev->ifindex);
2097 struct net *net = dev_net(dev);
2098 struct hlist_head *head = &net->nexthop.devhash[hash];
2099 struct hlist_node *n;
2100 struct nh_info *nhi;
2102 hlist_for_each_entry_safe(nhi, n, head, dev_hash) {
2103 if (nhi->fib_nhc.nhc_dev == dev) {
2104 if (nhi->family == AF_INET)
2105 fib_nhc_update_mtu(&nhi->fib_nhc, dev->mtu,
2112 static int nh_netdev_event(struct notifier_block *this,
2113 unsigned long event, void *ptr)
2115 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2116 struct netdev_notifier_info_ext *info_ext;
2120 case NETDEV_UNREGISTER:
2121 nexthop_flush_dev(dev);
2124 if (!(dev_get_flags(dev) & (IFF_RUNNING | IFF_LOWER_UP)))
2125 nexthop_flush_dev(dev);
2127 case NETDEV_CHANGEMTU:
2129 nexthop_sync_mtu(dev, info_ext->ext.mtu);
2130 rt_cache_flush(dev_net(dev));
2136 static struct notifier_block nh_netdev_notifier = {
2137 .notifier_call = nh_netdev_event,
2140 static int nexthops_dump(struct net *net, struct notifier_block *nb,
2141 struct netlink_ext_ack *extack)
2143 struct rb_root *root = &net->nexthop.rb_root;
2144 struct rb_node *node;
2147 for (node = rb_first(root); node; node = rb_next(node)) {
2150 nh = rb_entry(node, struct nexthop, rb_node);
2151 err = call_nexthop_notifier(nb, net, NEXTHOP_EVENT_REPLACE, nh,
2160 int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
2161 struct netlink_ext_ack *extack)
2166 err = nexthops_dump(net, nb, extack);
2169 err = blocking_notifier_chain_register(&net->nexthop.notifier_chain,
2175 EXPORT_SYMBOL(register_nexthop_notifier);
2177 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb)
2179 return blocking_notifier_chain_unregister(&net->nexthop.notifier_chain,
2182 EXPORT_SYMBOL(unregister_nexthop_notifier);
2184 void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap)
2186 struct nexthop *nexthop;
2190 nexthop = nexthop_find_by_id(net, id);
2194 nexthop->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP);
2196 nexthop->nh_flags |= RTNH_F_OFFLOAD;
2198 nexthop->nh_flags |= RTNH_F_TRAP;
2203 EXPORT_SYMBOL(nexthop_set_hw_flags);
2205 static void __net_exit nexthop_net_exit(struct net *net)
2208 flush_all_nexthops(net);
2210 kfree(net->nexthop.devhash);
2213 static int __net_init nexthop_net_init(struct net *net)
2215 size_t sz = sizeof(struct hlist_head) * NH_DEV_HASHSIZE;
2217 net->nexthop.rb_root = RB_ROOT;
2218 net->nexthop.devhash = kzalloc(sz, GFP_KERNEL);
2219 if (!net->nexthop.devhash)
2221 BLOCKING_INIT_NOTIFIER_HEAD(&net->nexthop.notifier_chain);
2226 static struct pernet_operations nexthop_net_ops = {
2227 .init = nexthop_net_init,
2228 .exit = nexthop_net_exit,
2231 static int __init nexthop_init(void)
2233 register_pernet_subsys(&nexthop_net_ops);
2235 register_netdevice_notifier(&nh_netdev_notifier);
2237 rtnl_register(PF_UNSPEC, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
2238 rtnl_register(PF_UNSPEC, RTM_DELNEXTHOP, rtm_del_nexthop, NULL, 0);
2239 rtnl_register(PF_UNSPEC, RTM_GETNEXTHOP, rtm_get_nexthop,
2240 rtm_dump_nexthop, 0);
2242 rtnl_register(PF_INET, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
2243 rtnl_register(PF_INET, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0);
2245 rtnl_register(PF_INET6, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
2246 rtnl_register(PF_INET6, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0);
2250 subsys_initcall(nexthop_init);