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_mp_info_init(struct nh_notifier_info *info,
92 u16 num_nh = nhg->num_nh;
95 info->type = NH_NOTIFIER_INFO_TYPE_GRP;
96 info->nh_grp = kzalloc(struct_size(info->nh_grp, nh_entries, num_nh),
101 info->nh_grp->num_nh = num_nh;
102 info->nh_grp->is_fdb = nhg->fdb_nh;
104 for (i = 0; i < num_nh; i++) {
105 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
107 info->nh_grp->nh_entries[i].id = nhge->nh->id;
108 info->nh_grp->nh_entries[i].weight = nhge->weight;
109 __nh_notifier_single_info_init(&info->nh_grp->nh_entries[i].nh,
116 static int nh_notifier_grp_info_init(struct nh_notifier_info *info,
117 const struct nexthop *nh)
119 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
122 return nh_notifier_mp_info_init(info, nhg);
126 static void nh_notifier_grp_info_fini(struct nh_notifier_info *info,
127 const struct nexthop *nh)
129 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
135 static int nh_notifier_info_init(struct nh_notifier_info *info,
136 const struct nexthop *nh)
141 return nh_notifier_grp_info_init(info, nh);
143 return nh_notifier_single_info_init(info, nh);
146 static void nh_notifier_info_fini(struct nh_notifier_info *info,
147 const struct nexthop *nh)
150 nh_notifier_grp_info_fini(info, nh);
152 nh_notifier_single_info_fini(info);
155 static int call_nexthop_notifiers(struct net *net,
156 enum nexthop_event_type event_type,
158 struct netlink_ext_ack *extack)
160 struct nh_notifier_info info = {
168 if (nexthop_notifiers_is_empty(net))
171 err = nh_notifier_info_init(&info, nh);
173 NL_SET_ERR_MSG(extack, "Failed to initialize nexthop notifier info");
177 err = blocking_notifier_call_chain(&net->nexthop.notifier_chain,
179 nh_notifier_info_fini(&info, nh);
181 return notifier_to_errno(err);
184 static int call_nexthop_notifier(struct notifier_block *nb, struct net *net,
185 enum nexthop_event_type event_type,
187 struct netlink_ext_ack *extack)
189 struct nh_notifier_info info = {
195 err = nh_notifier_info_init(&info, nh);
199 err = nb->notifier_call(nb, event_type, &info);
200 nh_notifier_info_fini(&info, nh);
202 return notifier_to_errno(err);
205 static unsigned int nh_dev_hashfn(unsigned int val)
207 unsigned int mask = NH_DEV_HASHSIZE - 1;
210 (val >> NH_DEV_HASHBITS) ^
211 (val >> (NH_DEV_HASHBITS * 2))) & mask;
214 static void nexthop_devhash_add(struct net *net, struct nh_info *nhi)
216 struct net_device *dev = nhi->fib_nhc.nhc_dev;
217 struct hlist_head *head;
222 hash = nh_dev_hashfn(dev->ifindex);
223 head = &net->nexthop.devhash[hash];
224 hlist_add_head(&nhi->dev_hash, head);
227 static void nexthop_free_group(struct nexthop *nh)
229 struct nh_group *nhg;
232 nhg = rcu_dereference_raw(nh->nh_grp);
233 for (i = 0; i < nhg->num_nh; ++i) {
234 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
236 WARN_ON(!list_empty(&nhge->nh_list));
237 nexthop_put(nhge->nh);
240 WARN_ON(nhg->spare == nhg);
246 static void nexthop_free_single(struct nexthop *nh)
250 nhi = rcu_dereference_raw(nh->nh_info);
251 switch (nhi->family) {
253 fib_nh_release(nh->net, &nhi->fib_nh);
256 ipv6_stub->fib6_nh_release(&nhi->fib6_nh);
262 void nexthop_free_rcu(struct rcu_head *head)
264 struct nexthop *nh = container_of(head, struct nexthop, rcu);
267 nexthop_free_group(nh);
269 nexthop_free_single(nh);
273 EXPORT_SYMBOL_GPL(nexthop_free_rcu);
275 static struct nexthop *nexthop_alloc(void)
279 nh = kzalloc(sizeof(struct nexthop), GFP_KERNEL);
281 INIT_LIST_HEAD(&nh->fi_list);
282 INIT_LIST_HEAD(&nh->f6i_list);
283 INIT_LIST_HEAD(&nh->grp_list);
284 INIT_LIST_HEAD(&nh->fdb_list);
289 static struct nh_group *nexthop_grp_alloc(u16 num_nh)
291 struct nh_group *nhg;
293 nhg = kzalloc(struct_size(nhg, nh_entries, num_nh), GFP_KERNEL);
295 nhg->num_nh = num_nh;
300 static void nh_base_seq_inc(struct net *net)
302 while (++net->nexthop.seq == 0)
306 /* no reference taken; rcu lock or rtnl must be held */
307 struct nexthop *nexthop_find_by_id(struct net *net, u32 id)
309 struct rb_node **pp, *parent = NULL, *next;
311 pp = &net->nexthop.rb_root.rb_node;
315 next = rcu_dereference_raw(*pp);
320 nh = rb_entry(parent, struct nexthop, rb_node);
323 else if (id > nh->id)
324 pp = &next->rb_right;
330 EXPORT_SYMBOL_GPL(nexthop_find_by_id);
332 /* used for auto id allocation; called with rtnl held */
333 static u32 nh_find_unused_id(struct net *net)
335 u32 id_start = net->nexthop.last_id_allocated;
338 net->nexthop.last_id_allocated++;
339 if (net->nexthop.last_id_allocated == id_start)
342 if (!nexthop_find_by_id(net, net->nexthop.last_id_allocated))
343 return net->nexthop.last_id_allocated;
348 static int nla_put_nh_group(struct sk_buff *skb, struct nh_group *nhg)
350 struct nexthop_grp *p;
351 size_t len = nhg->num_nh * sizeof(*p);
357 group_type = NEXTHOP_GRP_TYPE_MPATH;
359 if (nla_put_u16(skb, NHA_GROUP_TYPE, group_type))
360 goto nla_put_failure;
362 nla = nla_reserve(skb, NHA_GROUP, len);
364 goto nla_put_failure;
367 for (i = 0; i < nhg->num_nh; ++i) {
368 p->id = nhg->nh_entries[i].nh->id;
369 p->weight = nhg->nh_entries[i].weight - 1;
379 static int nh_fill_node(struct sk_buff *skb, struct nexthop *nh,
380 int event, u32 portid, u32 seq, unsigned int nlflags)
382 struct fib6_nh *fib6_nh;
383 struct fib_nh *fib_nh;
384 struct nlmsghdr *nlh;
388 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nhm), nlflags);
392 nhm = nlmsg_data(nlh);
393 nhm->nh_family = AF_UNSPEC;
394 nhm->nh_flags = nh->nh_flags;
395 nhm->nh_protocol = nh->protocol;
399 if (nla_put_u32(skb, NHA_ID, nh->id))
400 goto nla_put_failure;
403 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
405 if (nhg->fdb_nh && nla_put_flag(skb, NHA_FDB))
406 goto nla_put_failure;
407 if (nla_put_nh_group(skb, nhg))
408 goto nla_put_failure;
412 nhi = rtnl_dereference(nh->nh_info);
413 nhm->nh_family = nhi->family;
414 if (nhi->reject_nh) {
415 if (nla_put_flag(skb, NHA_BLACKHOLE))
416 goto nla_put_failure;
418 } else if (nhi->fdb_nh) {
419 if (nla_put_flag(skb, NHA_FDB))
420 goto nla_put_failure;
422 const struct net_device *dev;
424 dev = nhi->fib_nhc.nhc_dev;
425 if (dev && nla_put_u32(skb, NHA_OIF, dev->ifindex))
426 goto nla_put_failure;
429 nhm->nh_scope = nhi->fib_nhc.nhc_scope;
430 switch (nhi->family) {
432 fib_nh = &nhi->fib_nh;
433 if (fib_nh->fib_nh_gw_family &&
434 nla_put_be32(skb, NHA_GATEWAY, fib_nh->fib_nh_gw4))
435 goto nla_put_failure;
439 fib6_nh = &nhi->fib6_nh;
440 if (fib6_nh->fib_nh_gw_family &&
441 nla_put_in6_addr(skb, NHA_GATEWAY, &fib6_nh->fib_nh_gw6))
442 goto nla_put_failure;
446 if (nhi->fib_nhc.nhc_lwtstate &&
447 lwtunnel_fill_encap(skb, nhi->fib_nhc.nhc_lwtstate,
448 NHA_ENCAP, NHA_ENCAP_TYPE) < 0)
449 goto nla_put_failure;
456 nlmsg_cancel(skb, nlh);
460 static size_t nh_nlmsg_size_grp(struct nexthop *nh)
462 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
463 size_t sz = sizeof(struct nexthop_grp) * nhg->num_nh;
465 return nla_total_size(sz) +
466 nla_total_size(2); /* NHA_GROUP_TYPE */
469 static size_t nh_nlmsg_size_single(struct nexthop *nh)
471 struct nh_info *nhi = rtnl_dereference(nh->nh_info);
474 /* covers NHA_BLACKHOLE since NHA_OIF and BLACKHOLE
475 * are mutually exclusive
477 sz = nla_total_size(4); /* NHA_OIF */
479 switch (nhi->family) {
481 if (nhi->fib_nh.fib_nh_gw_family)
482 sz += nla_total_size(4); /* NHA_GATEWAY */
487 if (nhi->fib6_nh.fib_nh_gw_family)
488 sz += nla_total_size(sizeof(const struct in6_addr));
492 if (nhi->fib_nhc.nhc_lwtstate) {
493 sz += lwtunnel_get_encap_size(nhi->fib_nhc.nhc_lwtstate);
494 sz += nla_total_size(2); /* NHA_ENCAP_TYPE */
500 static size_t nh_nlmsg_size(struct nexthop *nh)
502 size_t sz = NLMSG_ALIGN(sizeof(struct nhmsg));
504 sz += nla_total_size(4); /* NHA_ID */
507 sz += nh_nlmsg_size_grp(nh);
509 sz += nh_nlmsg_size_single(nh);
514 static void nexthop_notify(int event, struct nexthop *nh, struct nl_info *info)
516 unsigned int nlflags = info->nlh ? info->nlh->nlmsg_flags : 0;
517 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
521 skb = nlmsg_new(nh_nlmsg_size(nh), gfp_any());
525 err = nh_fill_node(skb, nh, event, info->portid, seq, nlflags);
527 /* -EMSGSIZE implies BUG in nh_nlmsg_size() */
528 WARN_ON(err == -EMSGSIZE);
533 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_NEXTHOP,
534 info->nlh, gfp_any());
538 rtnl_set_sk_err(info->nl_net, RTNLGRP_NEXTHOP, err);
541 static bool valid_group_nh(struct nexthop *nh, unsigned int npaths,
542 bool *is_fdb, struct netlink_ext_ack *extack)
545 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
547 /* nested multipath (group within a group) is not
551 NL_SET_ERR_MSG(extack,
552 "Multipath group can not be a nexthop within a group");
555 *is_fdb = nhg->fdb_nh;
557 struct nh_info *nhi = rtnl_dereference(nh->nh_info);
559 if (nhi->reject_nh && npaths > 1) {
560 NL_SET_ERR_MSG(extack,
561 "Blackhole nexthop can not be used in a group with more than 1 path");
564 *is_fdb = nhi->fdb_nh;
570 static int nh_check_attr_fdb_group(struct nexthop *nh, u8 *nh_family,
571 struct netlink_ext_ack *extack)
575 nhi = rtnl_dereference(nh->nh_info);
578 NL_SET_ERR_MSG(extack, "FDB nexthop group can only have fdb nexthops");
582 if (*nh_family == AF_UNSPEC) {
583 *nh_family = nhi->family;
584 } else if (*nh_family != nhi->family) {
585 NL_SET_ERR_MSG(extack, "FDB nexthop group cannot have mixed family nexthops");
592 static int nh_check_attr_group(struct net *net,
593 struct nlattr *tb[], size_t tb_size,
594 struct netlink_ext_ack *extack)
596 unsigned int len = nla_len(tb[NHA_GROUP]);
597 u8 nh_family = AF_UNSPEC;
598 struct nexthop_grp *nhg;
602 if (!len || len & (sizeof(struct nexthop_grp) - 1)) {
603 NL_SET_ERR_MSG(extack,
604 "Invalid length for nexthop group attribute");
608 /* convert len to number of nexthop ids */
611 nhg = nla_data(tb[NHA_GROUP]);
612 for (i = 0; i < len; ++i) {
613 if (nhg[i].resvd1 || nhg[i].resvd2) {
614 NL_SET_ERR_MSG(extack, "Reserved fields in nexthop_grp must be 0");
617 if (nhg[i].weight > 254) {
618 NL_SET_ERR_MSG(extack, "Invalid value for weight");
621 for (j = i + 1; j < len; ++j) {
622 if (nhg[i].id == nhg[j].id) {
623 NL_SET_ERR_MSG(extack, "Nexthop id can not be used twice in a group");
631 nhg = nla_data(tb[NHA_GROUP]);
632 for (i = 0; i < len; ++i) {
636 nh = nexthop_find_by_id(net, nhg[i].id);
638 NL_SET_ERR_MSG(extack, "Invalid nexthop id");
641 if (!valid_group_nh(nh, len, &is_fdb_nh, extack))
644 if (nhg_fdb && nh_check_attr_fdb_group(nh, &nh_family, extack))
647 if (!nhg_fdb && is_fdb_nh) {
648 NL_SET_ERR_MSG(extack, "Non FDB nexthop group cannot have fdb nexthops");
652 for (i = NHA_GROUP_TYPE + 1; i < tb_size; ++i) {
657 NL_SET_ERR_MSG(extack,
658 "No other attributes can be set in nexthop groups");
665 static bool ipv6_good_nh(const struct fib6_nh *nh)
667 int state = NUD_REACHABLE;
672 n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev, &nh->fib_nh_gw6);
674 state = n->nud_state;
676 rcu_read_unlock_bh();
678 return !!(state & NUD_VALID);
681 static bool ipv4_good_nh(const struct fib_nh *nh)
683 int state = NUD_REACHABLE;
688 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
689 (__force u32)nh->fib_nh_gw4);
691 state = n->nud_state;
693 rcu_read_unlock_bh();
695 return !!(state & NUD_VALID);
698 static struct nexthop *nexthop_select_path_mp(struct nh_group *nhg, int hash)
700 struct nexthop *rc = NULL;
703 for (i = 0; i < nhg->num_nh; ++i) {
704 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
707 if (hash > atomic_read(&nhge->mpath.upper_bound))
710 nhi = rcu_dereference(nhge->nh->nh_info);
714 /* nexthops always check if it is good and does
715 * not rely on a sysctl for this behavior
717 switch (nhi->family) {
719 if (ipv4_good_nh(&nhi->fib_nh))
723 if (ipv6_good_nh(&nhi->fib6_nh))
735 struct nexthop *nexthop_select_path(struct nexthop *nh, int hash)
737 struct nh_group *nhg;
742 nhg = rcu_dereference(nh->nh_grp);
744 return nexthop_select_path_mp(nhg, hash);
749 EXPORT_SYMBOL_GPL(nexthop_select_path);
751 int nexthop_for_each_fib6_nh(struct nexthop *nh,
752 int (*cb)(struct fib6_nh *nh, void *arg),
759 struct nh_group *nhg;
762 nhg = rcu_dereference_rtnl(nh->nh_grp);
763 for (i = 0; i < nhg->num_nh; i++) {
764 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
766 nhi = rcu_dereference_rtnl(nhge->nh->nh_info);
767 err = cb(&nhi->fib6_nh, arg);
772 nhi = rcu_dereference_rtnl(nh->nh_info);
773 err = cb(&nhi->fib6_nh, arg);
780 EXPORT_SYMBOL_GPL(nexthop_for_each_fib6_nh);
782 static int check_src_addr(const struct in6_addr *saddr,
783 struct netlink_ext_ack *extack)
785 if (!ipv6_addr_any(saddr)) {
786 NL_SET_ERR_MSG(extack, "IPv6 routes using source address can not use nexthop objects");
792 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
793 struct netlink_ext_ack *extack)
798 /* fib6_src is unique to a fib6_info and limits the ability to cache
799 * routes in fib6_nh within a nexthop that is potentially shared
800 * across multiple fib entries. If the config wants to use source
801 * routing it can not use nexthop objects. mlxsw also does not allow
802 * fib6_src on routes.
804 if (cfg && check_src_addr(&cfg->fc_src, extack) < 0)
808 struct nh_group *nhg;
810 nhg = rtnl_dereference(nh->nh_grp);
813 is_fdb_nh = nhg->fdb_nh;
815 nhi = rtnl_dereference(nh->nh_info);
816 if (nhi->family == AF_INET)
818 is_fdb_nh = nhi->fdb_nh;
822 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
828 NL_SET_ERR_MSG(extack, "IPv6 routes can not use an IPv4 nexthop");
831 EXPORT_SYMBOL_GPL(fib6_check_nexthop);
833 /* if existing nexthop has ipv6 routes linked to it, need
834 * to verify this new spec works with ipv6
836 static int fib6_check_nh_list(struct nexthop *old, struct nexthop *new,
837 struct netlink_ext_ack *extack)
839 struct fib6_info *f6i;
841 if (list_empty(&old->f6i_list))
844 list_for_each_entry(f6i, &old->f6i_list, nh_list) {
845 if (check_src_addr(&f6i->fib6_src.addr, extack) < 0)
849 return fib6_check_nexthop(new, NULL, extack);
852 static int nexthop_check_scope(struct nh_info *nhi, u8 scope,
853 struct netlink_ext_ack *extack)
855 if (scope == RT_SCOPE_HOST && nhi->fib_nhc.nhc_gw_family) {
856 NL_SET_ERR_MSG(extack,
857 "Route with host scope can not have a gateway");
861 if (nhi->fib_nhc.nhc_flags & RTNH_F_ONLINK && scope >= RT_SCOPE_LINK) {
862 NL_SET_ERR_MSG(extack, "Scope mismatch with nexthop");
869 /* Invoked by fib add code to verify nexthop by id is ok with
870 * config for prefix; parts of fib_check_nh not done when nexthop
873 int fib_check_nexthop(struct nexthop *nh, u8 scope,
874 struct netlink_ext_ack *extack)
880 struct nh_group *nhg;
882 nhg = rtnl_dereference(nh->nh_grp);
884 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
889 if (scope == RT_SCOPE_HOST) {
890 NL_SET_ERR_MSG(extack, "Route with host scope can not have multiple nexthops");
895 /* all nexthops in a group have the same scope */
896 nhi = rtnl_dereference(nhg->nh_entries[0].nh->nh_info);
897 err = nexthop_check_scope(nhi, scope, extack);
899 nhi = rtnl_dereference(nh->nh_info);
901 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
905 err = nexthop_check_scope(nhi, scope, extack);
912 static int fib_check_nh_list(struct nexthop *old, struct nexthop *new,
913 struct netlink_ext_ack *extack)
917 list_for_each_entry(fi, &old->fi_list, nh_list) {
920 err = fib_check_nexthop(new, fi->fib_scope, extack);
927 static void nh_group_rebalance(struct nh_group *nhg)
933 for (i = 0; i < nhg->num_nh; ++i)
934 total += nhg->nh_entries[i].weight;
936 for (i = 0; i < nhg->num_nh; ++i) {
937 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
941 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31, total) - 1;
942 atomic_set(&nhge->mpath.upper_bound, upper_bound);
946 static void remove_nh_grp_entry(struct net *net, struct nh_grp_entry *nhge,
947 struct nl_info *nlinfo)
949 struct nh_grp_entry *nhges, *new_nhges;
950 struct nexthop *nhp = nhge->nh_parent;
951 struct netlink_ext_ack extack;
952 struct nexthop *nh = nhge->nh;
953 struct nh_group *nhg, *newg;
958 nhg = rtnl_dereference(nhp->nh_grp);
961 /* last entry, keep it visible and remove the parent */
962 if (nhg->num_nh == 1) {
963 remove_nexthop(net, nhp, nlinfo);
967 newg->has_v4 = false;
968 newg->mpath = nhg->mpath;
969 newg->fdb_nh = nhg->fdb_nh;
970 newg->num_nh = nhg->num_nh;
972 /* copy old entries to new except the one getting removed */
973 nhges = nhg->nh_entries;
974 new_nhges = newg->nh_entries;
975 for (i = 0, j = 0; i < nhg->num_nh; ++i) {
978 /* current nexthop getting removed */
979 if (nhg->nh_entries[i].nh == nh) {
984 nhi = rtnl_dereference(nhges[i].nh->nh_info);
985 if (nhi->family == AF_INET)
988 list_del(&nhges[i].nh_list);
989 new_nhges[j].nh_parent = nhges[i].nh_parent;
990 new_nhges[j].nh = nhges[i].nh;
991 new_nhges[j].weight = nhges[i].weight;
992 list_add(&new_nhges[j].nh_list, &new_nhges[j].nh->grp_list);
996 nh_group_rebalance(newg);
997 rcu_assign_pointer(nhp->nh_grp, newg);
999 list_del(&nhge->nh_list);
1000 nexthop_put(nhge->nh);
1002 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, nhp, &extack);
1004 pr_err("%s\n", extack._msg);
1007 nexthop_notify(RTM_NEWNEXTHOP, nhp, nlinfo);
1010 static void remove_nexthop_from_groups(struct net *net, struct nexthop *nh,
1011 struct nl_info *nlinfo)
1013 struct nh_grp_entry *nhge, *tmp;
1015 list_for_each_entry_safe(nhge, tmp, &nh->grp_list, nh_list)
1016 remove_nh_grp_entry(net, nhge, nlinfo);
1018 /* make sure all see the newly published array before releasing rtnl */
1022 static void remove_nexthop_group(struct nexthop *nh, struct nl_info *nlinfo)
1024 struct nh_group *nhg = rcu_dereference_rtnl(nh->nh_grp);
1025 int i, num_nh = nhg->num_nh;
1027 for (i = 0; i < num_nh; ++i) {
1028 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
1030 if (WARN_ON(!nhge->nh))
1033 list_del_init(&nhge->nh_list);
1037 /* not called for nexthop replace */
1038 static void __remove_nexthop_fib(struct net *net, struct nexthop *nh)
1040 struct fib6_info *f6i, *tmp;
1041 bool do_flush = false;
1042 struct fib_info *fi;
1044 list_for_each_entry(fi, &nh->fi_list, nh_list) {
1045 fi->fib_flags |= RTNH_F_DEAD;
1051 /* ip6_del_rt removes the entry from this list hence the _safe */
1052 list_for_each_entry_safe(f6i, tmp, &nh->f6i_list, nh_list) {
1053 /* __ip6_del_rt does a release, so do a hold here */
1054 fib6_info_hold(f6i);
1055 ipv6_stub->ip6_del_rt(net, f6i,
1056 !net->ipv4.sysctl_nexthop_compat_mode);
1060 static void __remove_nexthop(struct net *net, struct nexthop *nh,
1061 struct nl_info *nlinfo)
1063 __remove_nexthop_fib(net, nh);
1066 remove_nexthop_group(nh, nlinfo);
1068 struct nh_info *nhi;
1070 nhi = rtnl_dereference(nh->nh_info);
1071 if (nhi->fib_nhc.nhc_dev)
1072 hlist_del(&nhi->dev_hash);
1074 remove_nexthop_from_groups(net, nh, nlinfo);
1078 static void remove_nexthop(struct net *net, struct nexthop *nh,
1079 struct nl_info *nlinfo)
1081 call_nexthop_notifiers(net, NEXTHOP_EVENT_DEL, nh, NULL);
1083 /* remove from the tree */
1084 rb_erase(&nh->rb_node, &net->nexthop.rb_root);
1087 nexthop_notify(RTM_DELNEXTHOP, nh, nlinfo);
1089 __remove_nexthop(net, nh, nlinfo);
1090 nh_base_seq_inc(net);
1095 /* if any FIB entries reference this nexthop, any dst entries
1096 * need to be regenerated
1098 static void nh_rt_cache_flush(struct net *net, struct nexthop *nh)
1100 struct fib6_info *f6i;
1102 if (!list_empty(&nh->fi_list))
1103 rt_cache_flush(net);
1105 list_for_each_entry(f6i, &nh->f6i_list, nh_list)
1106 ipv6_stub->fib6_update_sernum(net, f6i);
1109 static int replace_nexthop_grp(struct net *net, struct nexthop *old,
1110 struct nexthop *new,
1111 struct netlink_ext_ack *extack)
1113 struct nh_group *oldg, *newg;
1116 if (!new->is_group) {
1117 NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with a nexthop.");
1121 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new, extack);
1125 oldg = rtnl_dereference(old->nh_grp);
1126 newg = rtnl_dereference(new->nh_grp);
1128 /* update parents - used by nexthop code for cleanup */
1129 for (i = 0; i < newg->num_nh; i++)
1130 newg->nh_entries[i].nh_parent = old;
1132 rcu_assign_pointer(old->nh_grp, newg);
1134 for (i = 0; i < oldg->num_nh; i++)
1135 oldg->nh_entries[i].nh_parent = new;
1137 rcu_assign_pointer(new->nh_grp, oldg);
1142 static void nh_group_v4_update(struct nh_group *nhg)
1144 struct nh_grp_entry *nhges;
1145 bool has_v4 = false;
1148 nhges = nhg->nh_entries;
1149 for (i = 0; i < nhg->num_nh; i++) {
1150 struct nh_info *nhi;
1152 nhi = rtnl_dereference(nhges[i].nh->nh_info);
1153 if (nhi->family == AF_INET)
1156 nhg->has_v4 = has_v4;
1159 static int replace_nexthop_single(struct net *net, struct nexthop *old,
1160 struct nexthop *new,
1161 struct netlink_ext_ack *extack)
1163 u8 old_protocol, old_nh_flags;
1164 struct nh_info *oldi, *newi;
1165 struct nh_grp_entry *nhge;
1168 if (new->is_group) {
1169 NL_SET_ERR_MSG(extack, "Can not replace a nexthop with a nexthop group.");
1173 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new, extack);
1177 /* Hardware flags were set on 'old' as 'new' is not in the red-black
1178 * tree. Therefore, inherit the flags from 'old' to 'new'.
1180 new->nh_flags |= old->nh_flags & (RTNH_F_OFFLOAD | RTNH_F_TRAP);
1182 oldi = rtnl_dereference(old->nh_info);
1183 newi = rtnl_dereference(new->nh_info);
1185 newi->nh_parent = old;
1186 oldi->nh_parent = new;
1188 old_protocol = old->protocol;
1189 old_nh_flags = old->nh_flags;
1191 old->protocol = new->protocol;
1192 old->nh_flags = new->nh_flags;
1194 rcu_assign_pointer(old->nh_info, newi);
1195 rcu_assign_pointer(new->nh_info, oldi);
1197 /* Send a replace notification for all the groups using the nexthop. */
1198 list_for_each_entry(nhge, &old->grp_list, nh_list) {
1199 struct nexthop *nhp = nhge->nh_parent;
1201 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, nhp,
1207 /* When replacing an IPv4 nexthop with an IPv6 nexthop, potentially
1208 * update IPv4 indication in all the groups using the nexthop.
1210 if (oldi->family == AF_INET && newi->family == AF_INET6) {
1211 list_for_each_entry(nhge, &old->grp_list, nh_list) {
1212 struct nexthop *nhp = nhge->nh_parent;
1213 struct nh_group *nhg;
1215 nhg = rtnl_dereference(nhp->nh_grp);
1216 nh_group_v4_update(nhg);
1223 rcu_assign_pointer(new->nh_info, newi);
1224 rcu_assign_pointer(old->nh_info, oldi);
1225 old->nh_flags = old_nh_flags;
1226 old->protocol = old_protocol;
1227 oldi->nh_parent = old;
1228 newi->nh_parent = new;
1229 list_for_each_entry_continue_reverse(nhge, &old->grp_list, nh_list) {
1230 struct nexthop *nhp = nhge->nh_parent;
1232 call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, nhp, extack);
1234 call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, old, extack);
1238 static void __nexthop_replace_notify(struct net *net, struct nexthop *nh,
1239 struct nl_info *info)
1241 struct fib6_info *f6i;
1243 if (!list_empty(&nh->fi_list)) {
1244 struct fib_info *fi;
1246 /* expectation is a few fib_info per nexthop and then
1247 * a lot of routes per fib_info. So mark the fib_info
1248 * and then walk the fib tables once
1250 list_for_each_entry(fi, &nh->fi_list, nh_list)
1251 fi->nh_updated = true;
1253 fib_info_notify_update(net, info);
1255 list_for_each_entry(fi, &nh->fi_list, nh_list)
1256 fi->nh_updated = false;
1259 list_for_each_entry(f6i, &nh->f6i_list, nh_list)
1260 ipv6_stub->fib6_rt_update(net, f6i, info);
1263 /* send RTM_NEWROUTE with REPLACE flag set for all FIB entries
1264 * linked to this nexthop and for all groups that the nexthop
1267 static void nexthop_replace_notify(struct net *net, struct nexthop *nh,
1268 struct nl_info *info)
1270 struct nh_grp_entry *nhge;
1272 __nexthop_replace_notify(net, nh, info);
1274 list_for_each_entry(nhge, &nh->grp_list, nh_list)
1275 __nexthop_replace_notify(net, nhge->nh_parent, info);
1278 static int replace_nexthop(struct net *net, struct nexthop *old,
1279 struct nexthop *new, struct netlink_ext_ack *extack)
1281 bool new_is_reject = false;
1282 struct nh_grp_entry *nhge;
1285 /* check that existing FIB entries are ok with the
1286 * new nexthop definition
1288 err = fib_check_nh_list(old, new, extack);
1292 err = fib6_check_nh_list(old, new, extack);
1296 if (!new->is_group) {
1297 struct nh_info *nhi = rtnl_dereference(new->nh_info);
1299 new_is_reject = nhi->reject_nh;
1302 list_for_each_entry(nhge, &old->grp_list, nh_list) {
1303 /* if new nexthop is a blackhole, any groups using this
1304 * nexthop cannot have more than 1 path
1306 if (new_is_reject &&
1307 nexthop_num_path(nhge->nh_parent) > 1) {
1308 NL_SET_ERR_MSG(extack, "Blackhole nexthop can not be a member of a group with more than one path");
1312 err = fib_check_nh_list(nhge->nh_parent, new, extack);
1316 err = fib6_check_nh_list(nhge->nh_parent, new, extack);
1322 err = replace_nexthop_grp(net, old, new, extack);
1324 err = replace_nexthop_single(net, old, new, extack);
1327 nh_rt_cache_flush(net, old);
1329 __remove_nexthop(net, new, NULL);
1336 /* called with rtnl_lock held */
1337 static int insert_nexthop(struct net *net, struct nexthop *new_nh,
1338 struct nh_config *cfg, struct netlink_ext_ack *extack)
1340 struct rb_node **pp, *parent = NULL, *next;
1341 struct rb_root *root = &net->nexthop.rb_root;
1342 bool replace = !!(cfg->nlflags & NLM_F_REPLACE);
1343 bool create = !!(cfg->nlflags & NLM_F_CREATE);
1344 u32 new_id = new_nh->id;
1345 int replace_notify = 0;
1348 pp = &root->rb_node;
1358 nh = rb_entry(parent, struct nexthop, rb_node);
1359 if (new_id < nh->id) {
1360 pp = &next->rb_left;
1361 } else if (new_id > nh->id) {
1362 pp = &next->rb_right;
1363 } else if (replace) {
1364 rc = replace_nexthop(net, nh, new_nh, extack);
1366 new_nh = nh; /* send notification with old nh */
1371 /* id already exists and not a replace */
1376 if (replace && !create) {
1377 NL_SET_ERR_MSG(extack, "Replace specified without create and no entry exists");
1382 rb_link_node_rcu(&new_nh->rb_node, parent, pp);
1383 rb_insert_color(&new_nh->rb_node, root);
1385 rc = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new_nh, extack);
1387 rb_erase(&new_nh->rb_node, &net->nexthop.rb_root);
1391 nh_base_seq_inc(net);
1392 nexthop_notify(RTM_NEWNEXTHOP, new_nh, &cfg->nlinfo);
1393 if (replace_notify && net->ipv4.sysctl_nexthop_compat_mode)
1394 nexthop_replace_notify(net, new_nh, &cfg->nlinfo);
1401 /* remove all nexthops tied to a device being deleted */
1402 static void nexthop_flush_dev(struct net_device *dev)
1404 unsigned int hash = nh_dev_hashfn(dev->ifindex);
1405 struct net *net = dev_net(dev);
1406 struct hlist_head *head = &net->nexthop.devhash[hash];
1407 struct hlist_node *n;
1408 struct nh_info *nhi;
1410 hlist_for_each_entry_safe(nhi, n, head, dev_hash) {
1411 if (nhi->fib_nhc.nhc_dev != dev)
1414 remove_nexthop(net, nhi->nh_parent, NULL);
1418 /* rtnl; called when net namespace is deleted */
1419 static void flush_all_nexthops(struct net *net)
1421 struct rb_root *root = &net->nexthop.rb_root;
1422 struct rb_node *node;
1425 while ((node = rb_first(root))) {
1426 nh = rb_entry(node, struct nexthop, rb_node);
1427 remove_nexthop(net, nh, NULL);
1432 static struct nexthop *nexthop_create_group(struct net *net,
1433 struct nh_config *cfg)
1435 struct nlattr *grps_attr = cfg->nh_grp;
1436 struct nexthop_grp *entry = nla_data(grps_attr);
1437 u16 num_nh = nla_len(grps_attr) / sizeof(*entry);
1438 struct nh_group *nhg;
1442 if (WARN_ON(!num_nh))
1443 return ERR_PTR(-EINVAL);
1445 nh = nexthop_alloc();
1447 return ERR_PTR(-ENOMEM);
1451 nhg = nexthop_grp_alloc(num_nh);
1454 return ERR_PTR(-ENOMEM);
1457 /* spare group used for removals */
1458 nhg->spare = nexthop_grp_alloc(num_nh);
1462 return ERR_PTR(-ENOMEM);
1464 nhg->spare->spare = nhg;
1466 for (i = 0; i < nhg->num_nh; ++i) {
1467 struct nexthop *nhe;
1468 struct nh_info *nhi;
1470 nhe = nexthop_find_by_id(net, entry[i].id);
1471 if (!nexthop_get(nhe))
1474 nhi = rtnl_dereference(nhe->nh_info);
1475 if (nhi->family == AF_INET)
1478 nhg->nh_entries[i].nh = nhe;
1479 nhg->nh_entries[i].weight = entry[i].weight + 1;
1480 list_add(&nhg->nh_entries[i].nh_list, &nhe->grp_list);
1481 nhg->nh_entries[i].nh_parent = nh;
1484 if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_MPATH)
1487 WARN_ON_ONCE(nhg->mpath != 1);
1490 nh_group_rebalance(nhg);
1495 rcu_assign_pointer(nh->nh_grp, nhg);
1500 for (i--; i >= 0; --i) {
1501 list_del(&nhg->nh_entries[i].nh_list);
1502 nexthop_put(nhg->nh_entries[i].nh);
1509 return ERR_PTR(-ENOENT);
1512 static int nh_create_ipv4(struct net *net, struct nexthop *nh,
1513 struct nh_info *nhi, struct nh_config *cfg,
1514 struct netlink_ext_ack *extack)
1516 struct fib_nh *fib_nh = &nhi->fib_nh;
1517 struct fib_config fib_cfg = {
1518 .fc_oif = cfg->nh_ifindex,
1519 .fc_gw4 = cfg->gw.ipv4,
1520 .fc_gw_family = cfg->gw.ipv4 ? AF_INET : 0,
1521 .fc_flags = cfg->nh_flags,
1522 .fc_encap = cfg->nh_encap,
1523 .fc_encap_type = cfg->nh_encap_type,
1525 u32 tb_id = (cfg->dev ? l3mdev_fib_table(cfg->dev) : RT_TABLE_MAIN);
1528 err = fib_nh_init(net, fib_nh, &fib_cfg, 1, extack);
1530 fib_nh_release(net, fib_nh);
1537 /* sets nh_dev if successful */
1538 err = fib_check_nh(net, fib_nh, tb_id, 0, extack);
1540 nh->nh_flags = fib_nh->fib_nh_flags;
1541 fib_info_update_nhc_saddr(net, &fib_nh->nh_common,
1542 fib_nh->fib_nh_scope);
1544 fib_nh_release(net, fib_nh);
1550 static int nh_create_ipv6(struct net *net, struct nexthop *nh,
1551 struct nh_info *nhi, struct nh_config *cfg,
1552 struct netlink_ext_ack *extack)
1554 struct fib6_nh *fib6_nh = &nhi->fib6_nh;
1555 struct fib6_config fib6_cfg = {
1556 .fc_table = l3mdev_fib_table(cfg->dev),
1557 .fc_ifindex = cfg->nh_ifindex,
1558 .fc_gateway = cfg->gw.ipv6,
1559 .fc_flags = cfg->nh_flags,
1560 .fc_encap = cfg->nh_encap,
1561 .fc_encap_type = cfg->nh_encap_type,
1562 .fc_is_fdb = cfg->nh_fdb,
1566 if (!ipv6_addr_any(&cfg->gw.ipv6))
1567 fib6_cfg.fc_flags |= RTF_GATEWAY;
1569 /* sets nh_dev if successful */
1570 err = ipv6_stub->fib6_nh_init(net, fib6_nh, &fib6_cfg, GFP_KERNEL,
1573 ipv6_stub->fib6_nh_release(fib6_nh);
1575 nh->nh_flags = fib6_nh->fib_nh_flags;
1580 static struct nexthop *nexthop_create(struct net *net, struct nh_config *cfg,
1581 struct netlink_ext_ack *extack)
1583 struct nh_info *nhi;
1587 nh = nexthop_alloc();
1589 return ERR_PTR(-ENOMEM);
1591 nhi = kzalloc(sizeof(*nhi), GFP_KERNEL);
1594 return ERR_PTR(-ENOMEM);
1597 nh->nh_flags = cfg->nh_flags;
1600 nhi->nh_parent = nh;
1601 nhi->family = cfg->nh_family;
1602 nhi->fib_nhc.nhc_scope = RT_SCOPE_LINK;
1607 if (cfg->nh_blackhole) {
1609 cfg->nh_ifindex = net->loopback_dev->ifindex;
1612 switch (cfg->nh_family) {
1614 err = nh_create_ipv4(net, nh, nhi, cfg, extack);
1617 err = nh_create_ipv6(net, nh, nhi, cfg, extack);
1624 return ERR_PTR(err);
1627 /* add the entry to the device based hash */
1629 nexthop_devhash_add(net, nhi);
1631 rcu_assign_pointer(nh->nh_info, nhi);
1636 /* called with rtnl lock held */
1637 static struct nexthop *nexthop_add(struct net *net, struct nh_config *cfg,
1638 struct netlink_ext_ack *extack)
1643 if (cfg->nlflags & NLM_F_REPLACE && !cfg->nh_id) {
1644 NL_SET_ERR_MSG(extack, "Replace requires nexthop id");
1645 return ERR_PTR(-EINVAL);
1649 cfg->nh_id = nh_find_unused_id(net);
1651 NL_SET_ERR_MSG(extack, "No unused id");
1652 return ERR_PTR(-EINVAL);
1657 nh = nexthop_create_group(net, cfg);
1659 nh = nexthop_create(net, cfg, extack);
1664 refcount_set(&nh->refcnt, 1);
1665 nh->id = cfg->nh_id;
1666 nh->protocol = cfg->nh_protocol;
1669 err = insert_nexthop(net, nh, cfg, extack);
1671 __remove_nexthop(net, nh, NULL);
1679 static int rtm_to_nh_config(struct net *net, struct sk_buff *skb,
1680 struct nlmsghdr *nlh, struct nh_config *cfg,
1681 struct netlink_ext_ack *extack)
1683 struct nhmsg *nhm = nlmsg_data(nlh);
1684 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_new)];
1687 err = nlmsg_parse(nlh, sizeof(*nhm), tb,
1688 ARRAY_SIZE(rtm_nh_policy_new) - 1,
1689 rtm_nh_policy_new, extack);
1694 if (nhm->resvd || nhm->nh_scope) {
1695 NL_SET_ERR_MSG(extack, "Invalid values in ancillary header");
1698 if (nhm->nh_flags & ~NEXTHOP_VALID_USER_FLAGS) {
1699 NL_SET_ERR_MSG(extack, "Invalid nexthop flags in ancillary header");
1703 switch (nhm->nh_family) {
1712 NL_SET_ERR_MSG(extack, "Invalid address family");
1716 memset(cfg, 0, sizeof(*cfg));
1717 cfg->nlflags = nlh->nlmsg_flags;
1718 cfg->nlinfo.portid = NETLINK_CB(skb).portid;
1719 cfg->nlinfo.nlh = nlh;
1720 cfg->nlinfo.nl_net = net;
1722 cfg->nh_family = nhm->nh_family;
1723 cfg->nh_protocol = nhm->nh_protocol;
1724 cfg->nh_flags = nhm->nh_flags;
1727 cfg->nh_id = nla_get_u32(tb[NHA_ID]);
1730 if (tb[NHA_OIF] || tb[NHA_BLACKHOLE] ||
1731 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE]) {
1732 NL_SET_ERR_MSG(extack, "Fdb attribute can not be used with encap, oif or blackhole");
1735 if (nhm->nh_flags) {
1736 NL_SET_ERR_MSG(extack, "Unsupported nexthop flags in ancillary header");
1739 cfg->nh_fdb = nla_get_flag(tb[NHA_FDB]);
1742 if (tb[NHA_GROUP]) {
1743 if (nhm->nh_family != AF_UNSPEC) {
1744 NL_SET_ERR_MSG(extack, "Invalid family for group");
1747 cfg->nh_grp = tb[NHA_GROUP];
1749 cfg->nh_grp_type = NEXTHOP_GRP_TYPE_MPATH;
1750 if (tb[NHA_GROUP_TYPE])
1751 cfg->nh_grp_type = nla_get_u16(tb[NHA_GROUP_TYPE]);
1753 if (cfg->nh_grp_type > NEXTHOP_GRP_TYPE_MAX) {
1754 NL_SET_ERR_MSG(extack, "Invalid group type");
1757 err = nh_check_attr_group(net, tb, ARRAY_SIZE(tb), extack);
1759 /* no other attributes should be set */
1763 if (tb[NHA_BLACKHOLE]) {
1764 if (tb[NHA_GATEWAY] || tb[NHA_OIF] ||
1765 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE] || tb[NHA_FDB]) {
1766 NL_SET_ERR_MSG(extack, "Blackhole attribute can not be used with gateway, oif, encap or fdb");
1770 cfg->nh_blackhole = 1;
1775 if (!cfg->nh_fdb && !tb[NHA_OIF]) {
1776 NL_SET_ERR_MSG(extack, "Device attribute required for non-blackhole and non-fdb nexthops");
1780 if (!cfg->nh_fdb && tb[NHA_OIF]) {
1781 cfg->nh_ifindex = nla_get_u32(tb[NHA_OIF]);
1782 if (cfg->nh_ifindex)
1783 cfg->dev = __dev_get_by_index(net, cfg->nh_ifindex);
1786 NL_SET_ERR_MSG(extack, "Invalid device index");
1788 } else if (!(cfg->dev->flags & IFF_UP)) {
1789 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
1792 } else if (!netif_carrier_ok(cfg->dev)) {
1793 NL_SET_ERR_MSG(extack, "Carrier for nexthop device is down");
1800 if (tb[NHA_GATEWAY]) {
1801 struct nlattr *gwa = tb[NHA_GATEWAY];
1803 switch (cfg->nh_family) {
1805 if (nla_len(gwa) != sizeof(u32)) {
1806 NL_SET_ERR_MSG(extack, "Invalid gateway");
1809 cfg->gw.ipv4 = nla_get_be32(gwa);
1812 if (nla_len(gwa) != sizeof(struct in6_addr)) {
1813 NL_SET_ERR_MSG(extack, "Invalid gateway");
1816 cfg->gw.ipv6 = nla_get_in6_addr(gwa);
1819 NL_SET_ERR_MSG(extack,
1820 "Unknown address family for gateway");
1824 /* device only nexthop (no gateway) */
1825 if (cfg->nh_flags & RTNH_F_ONLINK) {
1826 NL_SET_ERR_MSG(extack,
1827 "ONLINK flag can not be set for nexthop without a gateway");
1832 if (tb[NHA_ENCAP]) {
1833 cfg->nh_encap = tb[NHA_ENCAP];
1835 if (!tb[NHA_ENCAP_TYPE]) {
1836 NL_SET_ERR_MSG(extack, "LWT encapsulation type is missing");
1840 cfg->nh_encap_type = nla_get_u16(tb[NHA_ENCAP_TYPE]);
1841 err = lwtunnel_valid_encap_type(cfg->nh_encap_type, extack);
1845 } else if (tb[NHA_ENCAP_TYPE]) {
1846 NL_SET_ERR_MSG(extack, "LWT encapsulation attribute is missing");
1857 static int rtm_new_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
1858 struct netlink_ext_ack *extack)
1860 struct net *net = sock_net(skb->sk);
1861 struct nh_config cfg;
1865 err = rtm_to_nh_config(net, skb, nlh, &cfg, extack);
1867 nh = nexthop_add(net, &cfg, extack);
1875 static int nh_valid_get_del_req(struct nlmsghdr *nlh, u32 *id,
1876 struct netlink_ext_ack *extack)
1878 struct nhmsg *nhm = nlmsg_data(nlh);
1879 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_get)];
1882 err = nlmsg_parse(nlh, sizeof(*nhm), tb,
1883 ARRAY_SIZE(rtm_nh_policy_get) - 1,
1884 rtm_nh_policy_get, extack);
1889 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) {
1890 NL_SET_ERR_MSG(extack, "Invalid values in header");
1895 NL_SET_ERR_MSG(extack, "Nexthop id is missing");
1899 *id = nla_get_u32(tb[NHA_ID]);
1901 NL_SET_ERR_MSG(extack, "Invalid nexthop id");
1909 static int rtm_del_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
1910 struct netlink_ext_ack *extack)
1912 struct net *net = sock_net(skb->sk);
1913 struct nl_info nlinfo = {
1916 .portid = NETLINK_CB(skb).portid,
1922 err = nh_valid_get_del_req(nlh, &id, extack);
1926 nh = nexthop_find_by_id(net, id);
1930 remove_nexthop(net, nh, &nlinfo);
1936 static int rtm_get_nexthop(struct sk_buff *in_skb, struct nlmsghdr *nlh,
1937 struct netlink_ext_ack *extack)
1939 struct net *net = sock_net(in_skb->sk);
1940 struct sk_buff *skb = NULL;
1945 err = nh_valid_get_del_req(nlh, &id, extack);
1950 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1955 nh = nexthop_find_by_id(net, id);
1959 err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, NETLINK_CB(in_skb).portid,
1962 WARN_ON(err == -EMSGSIZE);
1966 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
1974 struct nh_dump_filter {
1981 static bool nh_dump_filtered(struct nexthop *nh,
1982 struct nh_dump_filter *filter, u8 family)
1984 const struct net_device *dev;
1985 const struct nh_info *nhi;
1987 if (filter->group_filter && !nh->is_group)
1990 if (!filter->dev_idx && !filter->master_idx && !family)
1996 nhi = rtnl_dereference(nh->nh_info);
1997 if (family && nhi->family != family)
2000 dev = nhi->fib_nhc.nhc_dev;
2001 if (filter->dev_idx && (!dev || dev->ifindex != filter->dev_idx))
2004 if (filter->master_idx) {
2005 struct net_device *master;
2010 master = netdev_master_upper_dev_get((struct net_device *)dev);
2011 if (!master || master->ifindex != filter->master_idx)
2018 static int __nh_valid_dump_req(const struct nlmsghdr *nlh, struct nlattr **tb,
2019 struct nh_dump_filter *filter,
2020 struct netlink_ext_ack *extack)
2026 idx = nla_get_u32(tb[NHA_OIF]);
2027 if (idx > INT_MAX) {
2028 NL_SET_ERR_MSG(extack, "Invalid device index");
2031 filter->dev_idx = idx;
2033 if (tb[NHA_MASTER]) {
2034 idx = nla_get_u32(tb[NHA_MASTER]);
2035 if (idx > INT_MAX) {
2036 NL_SET_ERR_MSG(extack, "Invalid master device index");
2039 filter->master_idx = idx;
2041 filter->group_filter = nla_get_flag(tb[NHA_GROUPS]);
2042 filter->fdb_filter = nla_get_flag(tb[NHA_FDB]);
2044 nhm = nlmsg_data(nlh);
2045 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) {
2046 NL_SET_ERR_MSG(extack, "Invalid values in header for nexthop dump request");
2053 static int nh_valid_dump_req(const struct nlmsghdr *nlh,
2054 struct nh_dump_filter *filter,
2055 struct netlink_callback *cb)
2057 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_dump)];
2060 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb,
2061 ARRAY_SIZE(rtm_nh_policy_dump) - 1,
2062 rtm_nh_policy_dump, cb->extack);
2066 return __nh_valid_dump_req(nlh, tb, filter, cb->extack);
2069 struct rtm_dump_nh_ctx {
2073 static struct rtm_dump_nh_ctx *
2074 rtm_dump_nh_ctx(struct netlink_callback *cb)
2076 struct rtm_dump_nh_ctx *ctx = (void *)cb->ctx;
2078 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
2083 static int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb)
2085 struct rtm_dump_nh_ctx *ctx = rtm_dump_nh_ctx(cb);
2086 struct nhmsg *nhm = nlmsg_data(cb->nlh);
2087 struct net *net = sock_net(skb->sk);
2088 struct rb_root *root = &net->nexthop.rb_root;
2089 struct nh_dump_filter filter = {};
2090 struct rb_node *node;
2094 err = nh_valid_dump_req(cb->nlh, &filter, cb);
2099 for (node = rb_first(root); node; node = rb_next(node)) {
2105 nh = rb_entry(node, struct nexthop, rb_node);
2106 if (nh_dump_filtered(nh, &filter, nhm->nh_family))
2109 err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP,
2110 NETLINK_CB(cb->skb).portid,
2111 cb->nlh->nlmsg_seq, NLM_F_MULTI);
2113 if (likely(skb->len))
2126 cb->seq = net->nexthop.seq;
2127 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2132 static void nexthop_sync_mtu(struct net_device *dev, u32 orig_mtu)
2134 unsigned int hash = nh_dev_hashfn(dev->ifindex);
2135 struct net *net = dev_net(dev);
2136 struct hlist_head *head = &net->nexthop.devhash[hash];
2137 struct hlist_node *n;
2138 struct nh_info *nhi;
2140 hlist_for_each_entry_safe(nhi, n, head, dev_hash) {
2141 if (nhi->fib_nhc.nhc_dev == dev) {
2142 if (nhi->family == AF_INET)
2143 fib_nhc_update_mtu(&nhi->fib_nhc, dev->mtu,
2150 static int nh_netdev_event(struct notifier_block *this,
2151 unsigned long event, void *ptr)
2153 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2154 struct netdev_notifier_info_ext *info_ext;
2158 case NETDEV_UNREGISTER:
2159 nexthop_flush_dev(dev);
2162 if (!(dev_get_flags(dev) & (IFF_RUNNING | IFF_LOWER_UP)))
2163 nexthop_flush_dev(dev);
2165 case NETDEV_CHANGEMTU:
2167 nexthop_sync_mtu(dev, info_ext->ext.mtu);
2168 rt_cache_flush(dev_net(dev));
2174 static struct notifier_block nh_netdev_notifier = {
2175 .notifier_call = nh_netdev_event,
2178 static int nexthops_dump(struct net *net, struct notifier_block *nb,
2179 struct netlink_ext_ack *extack)
2181 struct rb_root *root = &net->nexthop.rb_root;
2182 struct rb_node *node;
2185 for (node = rb_first(root); node; node = rb_next(node)) {
2188 nh = rb_entry(node, struct nexthop, rb_node);
2189 err = call_nexthop_notifier(nb, net, NEXTHOP_EVENT_REPLACE, nh,
2198 int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
2199 struct netlink_ext_ack *extack)
2204 err = nexthops_dump(net, nb, extack);
2207 err = blocking_notifier_chain_register(&net->nexthop.notifier_chain,
2213 EXPORT_SYMBOL(register_nexthop_notifier);
2215 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb)
2217 return blocking_notifier_chain_unregister(&net->nexthop.notifier_chain,
2220 EXPORT_SYMBOL(unregister_nexthop_notifier);
2222 void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap)
2224 struct nexthop *nexthop;
2228 nexthop = nexthop_find_by_id(net, id);
2232 nexthop->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP);
2234 nexthop->nh_flags |= RTNH_F_OFFLOAD;
2236 nexthop->nh_flags |= RTNH_F_TRAP;
2241 EXPORT_SYMBOL(nexthop_set_hw_flags);
2243 static void __net_exit nexthop_net_exit(struct net *net)
2246 flush_all_nexthops(net);
2248 kfree(net->nexthop.devhash);
2251 static int __net_init nexthop_net_init(struct net *net)
2253 size_t sz = sizeof(struct hlist_head) * NH_DEV_HASHSIZE;
2255 net->nexthop.rb_root = RB_ROOT;
2256 net->nexthop.devhash = kzalloc(sz, GFP_KERNEL);
2257 if (!net->nexthop.devhash)
2259 BLOCKING_INIT_NOTIFIER_HEAD(&net->nexthop.notifier_chain);
2264 static struct pernet_operations nexthop_net_ops = {
2265 .init = nexthop_net_init,
2266 .exit = nexthop_net_exit,
2269 static int __init nexthop_init(void)
2271 register_pernet_subsys(&nexthop_net_ops);
2273 register_netdevice_notifier(&nh_netdev_notifier);
2275 rtnl_register(PF_UNSPEC, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
2276 rtnl_register(PF_UNSPEC, RTM_DELNEXTHOP, rtm_del_nexthop, NULL, 0);
2277 rtnl_register(PF_UNSPEC, RTM_GETNEXTHOP, rtm_get_nexthop,
2278 rtm_dump_nexthop, 0);
2280 rtnl_register(PF_INET, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
2281 rtnl_register(PF_INET, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0);
2283 rtnl_register(PF_INET6, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
2284 rtnl_register(PF_INET6, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0);
2288 subsys_initcall(nexthop_init);