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 #define NH_RES_DEFAULT_IDLE_TIMER (120 * HZ)
20 #define NH_RES_DEFAULT_UNBALANCED_TIMER 0 /* No forced rebalancing. */
22 static void remove_nexthop(struct net *net, struct nexthop *nh,
23 struct nl_info *nlinfo);
25 #define NH_DEV_HASHBITS 8
26 #define NH_DEV_HASHSIZE (1U << NH_DEV_HASHBITS)
28 static const struct nla_policy rtm_nh_policy_new[] = {
29 [NHA_ID] = { .type = NLA_U32 },
30 [NHA_GROUP] = { .type = NLA_BINARY },
31 [NHA_GROUP_TYPE] = { .type = NLA_U16 },
32 [NHA_BLACKHOLE] = { .type = NLA_FLAG },
33 [NHA_OIF] = { .type = NLA_U32 },
34 [NHA_GATEWAY] = { .type = NLA_BINARY },
35 [NHA_ENCAP_TYPE] = { .type = NLA_U16 },
36 [NHA_ENCAP] = { .type = NLA_NESTED },
37 [NHA_FDB] = { .type = NLA_FLAG },
38 [NHA_RES_GROUP] = { .type = NLA_NESTED },
41 static const struct nla_policy rtm_nh_policy_get[] = {
42 [NHA_ID] = { .type = NLA_U32 },
45 static const struct nla_policy rtm_nh_policy_dump[] = {
46 [NHA_OIF] = { .type = NLA_U32 },
47 [NHA_GROUPS] = { .type = NLA_FLAG },
48 [NHA_MASTER] = { .type = NLA_U32 },
49 [NHA_FDB] = { .type = NLA_FLAG },
52 static const struct nla_policy rtm_nh_res_policy_new[] = {
53 [NHA_RES_GROUP_BUCKETS] = { .type = NLA_U16 },
54 [NHA_RES_GROUP_IDLE_TIMER] = { .type = NLA_U32 },
55 [NHA_RES_GROUP_UNBALANCED_TIMER] = { .type = NLA_U32 },
58 static bool nexthop_notifiers_is_empty(struct net *net)
60 return !net->nexthop.notifier_chain.head;
64 __nh_notifier_single_info_init(struct nh_notifier_single_info *nh_info,
65 const struct nh_info *nhi)
67 nh_info->dev = nhi->fib_nhc.nhc_dev;
68 nh_info->gw_family = nhi->fib_nhc.nhc_gw_family;
69 if (nh_info->gw_family == AF_INET)
70 nh_info->ipv4 = nhi->fib_nhc.nhc_gw.ipv4;
71 else if (nh_info->gw_family == AF_INET6)
72 nh_info->ipv6 = nhi->fib_nhc.nhc_gw.ipv6;
74 nh_info->is_reject = nhi->reject_nh;
75 nh_info->is_fdb = nhi->fdb_nh;
76 nh_info->has_encap = !!nhi->fib_nhc.nhc_lwtstate;
79 static int nh_notifier_single_info_init(struct nh_notifier_info *info,
80 const struct nexthop *nh)
82 struct nh_info *nhi = rtnl_dereference(nh->nh_info);
84 info->type = NH_NOTIFIER_INFO_TYPE_SINGLE;
85 info->nh = kzalloc(sizeof(*info->nh), GFP_KERNEL);
89 __nh_notifier_single_info_init(info->nh, nhi);
94 static void nh_notifier_single_info_fini(struct nh_notifier_info *info)
99 static int nh_notifier_mp_info_init(struct nh_notifier_info *info,
100 struct nh_group *nhg)
102 u16 num_nh = nhg->num_nh;
105 info->type = NH_NOTIFIER_INFO_TYPE_GRP;
106 info->nh_grp = kzalloc(struct_size(info->nh_grp, nh_entries, num_nh),
111 info->nh_grp->num_nh = num_nh;
112 info->nh_grp->is_fdb = nhg->fdb_nh;
114 for (i = 0; i < num_nh; i++) {
115 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
118 nhi = rtnl_dereference(nhge->nh->nh_info);
119 info->nh_grp->nh_entries[i].id = nhge->nh->id;
120 info->nh_grp->nh_entries[i].weight = nhge->weight;
121 __nh_notifier_single_info_init(&info->nh_grp->nh_entries[i].nh,
128 static int nh_notifier_res_table_info_init(struct nh_notifier_info *info,
129 struct nh_group *nhg)
131 struct nh_res_table *res_table = rtnl_dereference(nhg->res_table);
132 u16 num_nh_buckets = res_table->num_nh_buckets;
136 info->type = NH_NOTIFIER_INFO_TYPE_RES_TABLE;
137 size = struct_size(info->nh_res_table, nhs, num_nh_buckets);
138 info->nh_res_table = __vmalloc(size, GFP_KERNEL | __GFP_ZERO |
140 if (!info->nh_res_table)
143 info->nh_res_table->num_nh_buckets = num_nh_buckets;
145 for (i = 0; i < num_nh_buckets; i++) {
146 struct nh_res_bucket *bucket = &res_table->nh_buckets[i];
147 struct nh_grp_entry *nhge;
150 nhge = rtnl_dereference(bucket->nh_entry);
151 nhi = rtnl_dereference(nhge->nh->nh_info);
152 __nh_notifier_single_info_init(&info->nh_res_table->nhs[i],
159 static int nh_notifier_grp_info_init(struct nh_notifier_info *info,
160 const struct nexthop *nh)
162 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
165 return nh_notifier_mp_info_init(info, nhg);
166 else if (nhg->resilient)
167 return nh_notifier_res_table_info_init(info, nhg);
171 static void nh_notifier_grp_info_fini(struct nh_notifier_info *info,
172 const struct nexthop *nh)
174 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
178 else if (nhg->resilient)
179 vfree(info->nh_res_table);
182 static int nh_notifier_info_init(struct nh_notifier_info *info,
183 const struct nexthop *nh)
188 return nh_notifier_grp_info_init(info, nh);
190 return nh_notifier_single_info_init(info, nh);
193 static void nh_notifier_info_fini(struct nh_notifier_info *info,
194 const struct nexthop *nh)
197 nh_notifier_grp_info_fini(info, nh);
199 nh_notifier_single_info_fini(info);
202 static int call_nexthop_notifiers(struct net *net,
203 enum nexthop_event_type event_type,
205 struct netlink_ext_ack *extack)
207 struct nh_notifier_info info = {
215 if (nexthop_notifiers_is_empty(net))
218 err = nh_notifier_info_init(&info, nh);
220 NL_SET_ERR_MSG(extack, "Failed to initialize nexthop notifier info");
224 err = blocking_notifier_call_chain(&net->nexthop.notifier_chain,
226 nh_notifier_info_fini(&info, nh);
228 return notifier_to_errno(err);
232 nh_notifier_res_bucket_idle_timer_get(const struct nh_notifier_info *info,
233 bool force, unsigned int *p_idle_timer_ms)
235 struct nh_res_table *res_table;
236 struct nh_group *nhg;
240 /* When 'force' is false, nexthop bucket replacement is performed
241 * because the bucket was deemed to be idle. In this case, capable
242 * listeners can choose to perform an atomic replacement: The bucket is
243 * only replaced if it is inactive. However, if the idle timer interval
244 * is smaller than the interval in which a listener is querying
245 * buckets' activity from the device, then atomic replacement should
246 * not be tried. Pass the idle timer value to listeners, so that they
247 * could determine which type of replacement to perform.
250 *p_idle_timer_ms = 0;
256 nh = nexthop_find_by_id(info->net, info->id);
262 nhg = rcu_dereference(nh->nh_grp);
263 res_table = rcu_dereference(nhg->res_table);
264 *p_idle_timer_ms = jiffies_to_msecs(res_table->idle_timer);
272 static int nh_notifier_res_bucket_info_init(struct nh_notifier_info *info,
273 u16 bucket_index, bool force,
274 struct nh_info *oldi,
275 struct nh_info *newi)
277 unsigned int idle_timer_ms;
280 err = nh_notifier_res_bucket_idle_timer_get(info, force,
285 info->type = NH_NOTIFIER_INFO_TYPE_RES_BUCKET;
286 info->nh_res_bucket = kzalloc(sizeof(*info->nh_res_bucket),
288 if (!info->nh_res_bucket)
291 info->nh_res_bucket->bucket_index = bucket_index;
292 info->nh_res_bucket->idle_timer_ms = idle_timer_ms;
293 info->nh_res_bucket->force = force;
294 __nh_notifier_single_info_init(&info->nh_res_bucket->old_nh, oldi);
295 __nh_notifier_single_info_init(&info->nh_res_bucket->new_nh, newi);
299 static void nh_notifier_res_bucket_info_fini(struct nh_notifier_info *info)
301 kfree(info->nh_res_bucket);
304 static int __call_nexthop_res_bucket_notifiers(struct net *net, u32 nhg_id,
305 u16 bucket_index, bool force,
306 struct nh_info *oldi,
307 struct nh_info *newi,
308 struct netlink_ext_ack *extack)
310 struct nh_notifier_info info = {
317 if (nexthop_notifiers_is_empty(net))
320 err = nh_notifier_res_bucket_info_init(&info, bucket_index, force,
325 err = blocking_notifier_call_chain(&net->nexthop.notifier_chain,
326 NEXTHOP_EVENT_BUCKET_REPLACE, &info);
327 nh_notifier_res_bucket_info_fini(&info);
329 return notifier_to_errno(err);
332 /* There are three users of RES_TABLE, and NHs etc. referenced from there:
334 * 1) a collection of callbacks for NH maintenance. This operates under
336 * 2) the delayed work that gradually balances the resilient table,
337 * 3) and nexthop_select_path(), operating under RCU.
339 * Both the delayed work and the RTNL block are writers, and need to
340 * maintain mutual exclusion. Since there are only two and well-known
341 * writers for each table, the RTNL code can make sure it has exclusive
344 * - Have the DW operate without locking;
345 * - synchronously cancel the DW;
347 * - if the write was not actually a delete, call upkeep, which schedules
348 * DW again if necessary.
350 * The functions that are always called from the RTNL context use
351 * rtnl_dereference(). The functions that can also be called from the DW do
352 * a raw dereference and rely on the above mutual exclusion scheme.
354 #define nh_res_dereference(p) (rcu_dereference_raw(p))
356 static int call_nexthop_res_bucket_notifiers(struct net *net, u32 nhg_id,
357 u16 bucket_index, bool force,
358 struct nexthop *old_nh,
359 struct nexthop *new_nh,
360 struct netlink_ext_ack *extack)
362 struct nh_info *oldi = nh_res_dereference(old_nh->nh_info);
363 struct nh_info *newi = nh_res_dereference(new_nh->nh_info);
365 return __call_nexthop_res_bucket_notifiers(net, nhg_id, bucket_index,
366 force, oldi, newi, extack);
369 static int call_nexthop_res_table_notifiers(struct net *net, struct nexthop *nh,
370 struct netlink_ext_ack *extack)
372 struct nh_notifier_info info = {
376 struct nh_group *nhg;
381 if (nexthop_notifiers_is_empty(net))
384 /* At this point, the nexthop buckets are still not populated. Only
385 * emit a notification with the logical nexthops, so that a listener
386 * could potentially veto it in case of unsupported configuration.
388 nhg = rtnl_dereference(nh->nh_grp);
389 err = nh_notifier_mp_info_init(&info, nhg);
391 NL_SET_ERR_MSG(extack, "Failed to initialize nexthop notifier info");
395 err = blocking_notifier_call_chain(&net->nexthop.notifier_chain,
396 NEXTHOP_EVENT_RES_TABLE_PRE_REPLACE,
400 return notifier_to_errno(err);
403 static int call_nexthop_notifier(struct notifier_block *nb, struct net *net,
404 enum nexthop_event_type event_type,
406 struct netlink_ext_ack *extack)
408 struct nh_notifier_info info = {
414 err = nh_notifier_info_init(&info, nh);
418 err = nb->notifier_call(nb, event_type, &info);
419 nh_notifier_info_fini(&info, nh);
421 return notifier_to_errno(err);
424 static unsigned int nh_dev_hashfn(unsigned int val)
426 unsigned int mask = NH_DEV_HASHSIZE - 1;
429 (val >> NH_DEV_HASHBITS) ^
430 (val >> (NH_DEV_HASHBITS * 2))) & mask;
433 static void nexthop_devhash_add(struct net *net, struct nh_info *nhi)
435 struct net_device *dev = nhi->fib_nhc.nhc_dev;
436 struct hlist_head *head;
441 hash = nh_dev_hashfn(dev->ifindex);
442 head = &net->nexthop.devhash[hash];
443 hlist_add_head(&nhi->dev_hash, head);
446 static void nexthop_free_group(struct nexthop *nh)
448 struct nh_group *nhg;
451 nhg = rcu_dereference_raw(nh->nh_grp);
452 for (i = 0; i < nhg->num_nh; ++i) {
453 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
455 WARN_ON(!list_empty(&nhge->nh_list));
456 nexthop_put(nhge->nh);
459 WARN_ON(nhg->spare == nhg);
462 vfree(rcu_dereference_raw(nhg->res_table));
468 static void nexthop_free_single(struct nexthop *nh)
472 nhi = rcu_dereference_raw(nh->nh_info);
473 switch (nhi->family) {
475 fib_nh_release(nh->net, &nhi->fib_nh);
478 ipv6_stub->fib6_nh_release(&nhi->fib6_nh);
484 void nexthop_free_rcu(struct rcu_head *head)
486 struct nexthop *nh = container_of(head, struct nexthop, rcu);
489 nexthop_free_group(nh);
491 nexthop_free_single(nh);
495 EXPORT_SYMBOL_GPL(nexthop_free_rcu);
497 static struct nexthop *nexthop_alloc(void)
501 nh = kzalloc(sizeof(struct nexthop), GFP_KERNEL);
503 INIT_LIST_HEAD(&nh->fi_list);
504 INIT_LIST_HEAD(&nh->f6i_list);
505 INIT_LIST_HEAD(&nh->grp_list);
506 INIT_LIST_HEAD(&nh->fdb_list);
511 static struct nh_group *nexthop_grp_alloc(u16 num_nh)
513 struct nh_group *nhg;
515 nhg = kzalloc(struct_size(nhg, nh_entries, num_nh), GFP_KERNEL);
517 nhg->num_nh = num_nh;
522 static void nh_res_table_upkeep_dw(struct work_struct *work);
524 static struct nh_res_table *
525 nexthop_res_table_alloc(struct net *net, u32 nhg_id, struct nh_config *cfg)
527 const u16 num_nh_buckets = cfg->nh_grp_res_num_buckets;
528 struct nh_res_table *res_table;
531 size = struct_size(res_table, nh_buckets, num_nh_buckets);
532 res_table = __vmalloc(size, GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN);
536 res_table->net = net;
537 res_table->nhg_id = nhg_id;
538 INIT_DELAYED_WORK(&res_table->upkeep_dw, &nh_res_table_upkeep_dw);
539 INIT_LIST_HEAD(&res_table->uw_nh_entries);
540 res_table->idle_timer = cfg->nh_grp_res_idle_timer;
541 res_table->unbalanced_timer = cfg->nh_grp_res_unbalanced_timer;
542 res_table->num_nh_buckets = num_nh_buckets;
546 static void nh_base_seq_inc(struct net *net)
548 while (++net->nexthop.seq == 0)
552 /* no reference taken; rcu lock or rtnl must be held */
553 struct nexthop *nexthop_find_by_id(struct net *net, u32 id)
555 struct rb_node **pp, *parent = NULL, *next;
557 pp = &net->nexthop.rb_root.rb_node;
561 next = rcu_dereference_raw(*pp);
566 nh = rb_entry(parent, struct nexthop, rb_node);
569 else if (id > nh->id)
570 pp = &next->rb_right;
576 EXPORT_SYMBOL_GPL(nexthop_find_by_id);
578 /* used for auto id allocation; called with rtnl held */
579 static u32 nh_find_unused_id(struct net *net)
581 u32 id_start = net->nexthop.last_id_allocated;
584 net->nexthop.last_id_allocated++;
585 if (net->nexthop.last_id_allocated == id_start)
588 if (!nexthop_find_by_id(net, net->nexthop.last_id_allocated))
589 return net->nexthop.last_id_allocated;
594 static void nh_res_time_set_deadline(unsigned long next_time,
595 unsigned long *deadline)
597 if (time_before(next_time, *deadline))
598 *deadline = next_time;
601 static clock_t nh_res_table_unbalanced_time(struct nh_res_table *res_table)
603 if (list_empty(&res_table->uw_nh_entries))
605 return jiffies_delta_to_clock_t(jiffies - res_table->unbalanced_since);
608 static int nla_put_nh_group_res(struct sk_buff *skb, struct nh_group *nhg)
610 struct nh_res_table *res_table = rtnl_dereference(nhg->res_table);
613 nest = nla_nest_start(skb, NHA_RES_GROUP);
617 if (nla_put_u16(skb, NHA_RES_GROUP_BUCKETS,
618 res_table->num_nh_buckets) ||
619 nla_put_u32(skb, NHA_RES_GROUP_IDLE_TIMER,
620 jiffies_to_clock_t(res_table->idle_timer)) ||
621 nla_put_u32(skb, NHA_RES_GROUP_UNBALANCED_TIMER,
622 jiffies_to_clock_t(res_table->unbalanced_timer)) ||
623 nla_put_u64_64bit(skb, NHA_RES_GROUP_UNBALANCED_TIME,
624 nh_res_table_unbalanced_time(res_table),
626 goto nla_put_failure;
628 nla_nest_end(skb, nest);
632 nla_nest_cancel(skb, nest);
636 static int nla_put_nh_group(struct sk_buff *skb, struct nh_group *nhg)
638 struct nexthop_grp *p;
639 size_t len = nhg->num_nh * sizeof(*p);
645 group_type = NEXTHOP_GRP_TYPE_MPATH;
646 else if (nhg->resilient)
647 group_type = NEXTHOP_GRP_TYPE_RES;
649 if (nla_put_u16(skb, NHA_GROUP_TYPE, group_type))
650 goto nla_put_failure;
652 nla = nla_reserve(skb, NHA_GROUP, len);
654 goto nla_put_failure;
657 for (i = 0; i < nhg->num_nh; ++i) {
658 p->id = nhg->nh_entries[i].nh->id;
659 p->weight = nhg->nh_entries[i].weight - 1;
663 if (nhg->resilient && nla_put_nh_group_res(skb, nhg))
664 goto nla_put_failure;
672 static int nh_fill_node(struct sk_buff *skb, struct nexthop *nh,
673 int event, u32 portid, u32 seq, unsigned int nlflags)
675 struct fib6_nh *fib6_nh;
676 struct fib_nh *fib_nh;
677 struct nlmsghdr *nlh;
681 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nhm), nlflags);
685 nhm = nlmsg_data(nlh);
686 nhm->nh_family = AF_UNSPEC;
687 nhm->nh_flags = nh->nh_flags;
688 nhm->nh_protocol = nh->protocol;
692 if (nla_put_u32(skb, NHA_ID, nh->id))
693 goto nla_put_failure;
696 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
698 if (nhg->fdb_nh && nla_put_flag(skb, NHA_FDB))
699 goto nla_put_failure;
700 if (nla_put_nh_group(skb, nhg))
701 goto nla_put_failure;
705 nhi = rtnl_dereference(nh->nh_info);
706 nhm->nh_family = nhi->family;
707 if (nhi->reject_nh) {
708 if (nla_put_flag(skb, NHA_BLACKHOLE))
709 goto nla_put_failure;
711 } else if (nhi->fdb_nh) {
712 if (nla_put_flag(skb, NHA_FDB))
713 goto nla_put_failure;
715 const struct net_device *dev;
717 dev = nhi->fib_nhc.nhc_dev;
718 if (dev && nla_put_u32(skb, NHA_OIF, dev->ifindex))
719 goto nla_put_failure;
722 nhm->nh_scope = nhi->fib_nhc.nhc_scope;
723 switch (nhi->family) {
725 fib_nh = &nhi->fib_nh;
726 if (fib_nh->fib_nh_gw_family &&
727 nla_put_be32(skb, NHA_GATEWAY, fib_nh->fib_nh_gw4))
728 goto nla_put_failure;
732 fib6_nh = &nhi->fib6_nh;
733 if (fib6_nh->fib_nh_gw_family &&
734 nla_put_in6_addr(skb, NHA_GATEWAY, &fib6_nh->fib_nh_gw6))
735 goto nla_put_failure;
739 if (nhi->fib_nhc.nhc_lwtstate &&
740 lwtunnel_fill_encap(skb, nhi->fib_nhc.nhc_lwtstate,
741 NHA_ENCAP, NHA_ENCAP_TYPE) < 0)
742 goto nla_put_failure;
749 nlmsg_cancel(skb, nlh);
753 static size_t nh_nlmsg_size_grp_res(struct nh_group *nhg)
755 return nla_total_size(0) + /* NHA_RES_GROUP */
756 nla_total_size(2) + /* NHA_RES_GROUP_BUCKETS */
757 nla_total_size(4) + /* NHA_RES_GROUP_IDLE_TIMER */
758 nla_total_size(4) + /* NHA_RES_GROUP_UNBALANCED_TIMER */
759 nla_total_size_64bit(8);/* NHA_RES_GROUP_UNBALANCED_TIME */
762 static size_t nh_nlmsg_size_grp(struct nexthop *nh)
764 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
765 size_t sz = sizeof(struct nexthop_grp) * nhg->num_nh;
766 size_t tot = nla_total_size(sz) +
767 nla_total_size(2); /* NHA_GROUP_TYPE */
770 tot += nh_nlmsg_size_grp_res(nhg);
775 static size_t nh_nlmsg_size_single(struct nexthop *nh)
777 struct nh_info *nhi = rtnl_dereference(nh->nh_info);
780 /* covers NHA_BLACKHOLE since NHA_OIF and BLACKHOLE
781 * are mutually exclusive
783 sz = nla_total_size(4); /* NHA_OIF */
785 switch (nhi->family) {
787 if (nhi->fib_nh.fib_nh_gw_family)
788 sz += nla_total_size(4); /* NHA_GATEWAY */
793 if (nhi->fib6_nh.fib_nh_gw_family)
794 sz += nla_total_size(sizeof(const struct in6_addr));
798 if (nhi->fib_nhc.nhc_lwtstate) {
799 sz += lwtunnel_get_encap_size(nhi->fib_nhc.nhc_lwtstate);
800 sz += nla_total_size(2); /* NHA_ENCAP_TYPE */
806 static size_t nh_nlmsg_size(struct nexthop *nh)
808 size_t sz = NLMSG_ALIGN(sizeof(struct nhmsg));
810 sz += nla_total_size(4); /* NHA_ID */
813 sz += nh_nlmsg_size_grp(nh);
815 sz += nh_nlmsg_size_single(nh);
820 static void nexthop_notify(int event, struct nexthop *nh, struct nl_info *info)
822 unsigned int nlflags = info->nlh ? info->nlh->nlmsg_flags : 0;
823 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
827 skb = nlmsg_new(nh_nlmsg_size(nh), gfp_any());
831 err = nh_fill_node(skb, nh, event, info->portid, seq, nlflags);
833 /* -EMSGSIZE implies BUG in nh_nlmsg_size() */
834 WARN_ON(err == -EMSGSIZE);
839 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_NEXTHOP,
840 info->nlh, gfp_any());
844 rtnl_set_sk_err(info->nl_net, RTNLGRP_NEXTHOP, err);
847 static unsigned long nh_res_bucket_used_time(const struct nh_res_bucket *bucket)
849 return (unsigned long)atomic_long_read(&bucket->used_time);
853 nh_res_bucket_idle_point(const struct nh_res_table *res_table,
854 const struct nh_res_bucket *bucket,
857 unsigned long time = nh_res_bucket_used_time(bucket);
859 /* Bucket was not used since it was migrated. The idle time is now. */
860 if (time == bucket->migrated_time)
863 return time + res_table->idle_timer;
867 nh_res_table_unb_point(const struct nh_res_table *res_table)
869 return res_table->unbalanced_since + res_table->unbalanced_timer;
872 static void nh_res_bucket_set_idle(const struct nh_res_table *res_table,
873 struct nh_res_bucket *bucket)
875 unsigned long now = jiffies;
877 atomic_long_set(&bucket->used_time, (long)now);
878 bucket->migrated_time = now;
881 static void nh_res_bucket_set_busy(struct nh_res_bucket *bucket)
883 atomic_long_set(&bucket->used_time, (long)jiffies);
886 static bool valid_group_nh(struct nexthop *nh, unsigned int npaths,
887 bool *is_fdb, struct netlink_ext_ack *extack)
890 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
892 /* Nesting groups within groups is not supported. */
894 NL_SET_ERR_MSG(extack,
895 "Multipath group can not be a nexthop within a group");
898 if (nhg->resilient) {
899 NL_SET_ERR_MSG(extack,
900 "Resilient group can not be a nexthop within a group");
903 *is_fdb = nhg->fdb_nh;
905 struct nh_info *nhi = rtnl_dereference(nh->nh_info);
907 if (nhi->reject_nh && npaths > 1) {
908 NL_SET_ERR_MSG(extack,
909 "Blackhole nexthop can not be used in a group with more than 1 path");
912 *is_fdb = nhi->fdb_nh;
918 static int nh_check_attr_fdb_group(struct nexthop *nh, u8 *nh_family,
919 struct netlink_ext_ack *extack)
923 nhi = rtnl_dereference(nh->nh_info);
926 NL_SET_ERR_MSG(extack, "FDB nexthop group can only have fdb nexthops");
930 if (*nh_family == AF_UNSPEC) {
931 *nh_family = nhi->family;
932 } else if (*nh_family != nhi->family) {
933 NL_SET_ERR_MSG(extack, "FDB nexthop group cannot have mixed family nexthops");
940 static int nh_check_attr_group(struct net *net,
941 struct nlattr *tb[], size_t tb_size,
942 u16 nh_grp_type, struct netlink_ext_ack *extack)
944 unsigned int len = nla_len(tb[NHA_GROUP]);
945 u8 nh_family = AF_UNSPEC;
946 struct nexthop_grp *nhg;
950 if (!len || len & (sizeof(struct nexthop_grp) - 1)) {
951 NL_SET_ERR_MSG(extack,
952 "Invalid length for nexthop group attribute");
956 /* convert len to number of nexthop ids */
959 nhg = nla_data(tb[NHA_GROUP]);
960 for (i = 0; i < len; ++i) {
961 if (nhg[i].resvd1 || nhg[i].resvd2) {
962 NL_SET_ERR_MSG(extack, "Reserved fields in nexthop_grp must be 0");
965 if (nhg[i].weight > 254) {
966 NL_SET_ERR_MSG(extack, "Invalid value for weight");
969 for (j = i + 1; j < len; ++j) {
970 if (nhg[i].id == nhg[j].id) {
971 NL_SET_ERR_MSG(extack, "Nexthop id can not be used twice in a group");
979 nhg = nla_data(tb[NHA_GROUP]);
980 for (i = 0; i < len; ++i) {
984 nh = nexthop_find_by_id(net, nhg[i].id);
986 NL_SET_ERR_MSG(extack, "Invalid nexthop id");
989 if (!valid_group_nh(nh, len, &is_fdb_nh, extack))
992 if (nhg_fdb && nh_check_attr_fdb_group(nh, &nh_family, extack))
995 if (!nhg_fdb && is_fdb_nh) {
996 NL_SET_ERR_MSG(extack, "Non FDB nexthop group cannot have fdb nexthops");
1000 for (i = NHA_GROUP_TYPE + 1; i < tb_size; ++i) {
1007 if (nh_grp_type == NEXTHOP_GRP_TYPE_RES)
1011 NL_SET_ERR_MSG(extack,
1012 "No other attributes can be set in nexthop groups");
1019 static bool ipv6_good_nh(const struct fib6_nh *nh)
1021 int state = NUD_REACHABLE;
1022 struct neighbour *n;
1026 n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev, &nh->fib_nh_gw6);
1028 state = n->nud_state;
1030 rcu_read_unlock_bh();
1032 return !!(state & NUD_VALID);
1035 static bool ipv4_good_nh(const struct fib_nh *nh)
1037 int state = NUD_REACHABLE;
1038 struct neighbour *n;
1042 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
1043 (__force u32)nh->fib_nh_gw4);
1045 state = n->nud_state;
1047 rcu_read_unlock_bh();
1049 return !!(state & NUD_VALID);
1052 static struct nexthop *nexthop_select_path_mp(struct nh_group *nhg, int hash)
1054 struct nexthop *rc = NULL;
1057 for (i = 0; i < nhg->num_nh; ++i) {
1058 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
1059 struct nh_info *nhi;
1061 if (hash > atomic_read(&nhge->mpath.upper_bound))
1064 nhi = rcu_dereference(nhge->nh->nh_info);
1068 /* nexthops always check if it is good and does
1069 * not rely on a sysctl for this behavior
1071 switch (nhi->family) {
1073 if (ipv4_good_nh(&nhi->fib_nh))
1077 if (ipv6_good_nh(&nhi->fib6_nh))
1089 static struct nexthop *nexthop_select_path_res(struct nh_group *nhg, int hash)
1091 struct nh_res_table *res_table = rcu_dereference(nhg->res_table);
1092 u16 bucket_index = hash % res_table->num_nh_buckets;
1093 struct nh_res_bucket *bucket;
1094 struct nh_grp_entry *nhge;
1096 /* nexthop_select_path() is expected to return a non-NULL value, so
1097 * skip protocol validation and just hand out whatever there is.
1099 bucket = &res_table->nh_buckets[bucket_index];
1100 nh_res_bucket_set_busy(bucket);
1101 nhge = rcu_dereference(bucket->nh_entry);
1105 struct nexthop *nexthop_select_path(struct nexthop *nh, int hash)
1107 struct nh_group *nhg;
1112 nhg = rcu_dereference(nh->nh_grp);
1114 return nexthop_select_path_mp(nhg, hash);
1115 else if (nhg->resilient)
1116 return nexthop_select_path_res(nhg, hash);
1121 EXPORT_SYMBOL_GPL(nexthop_select_path);
1123 int nexthop_for_each_fib6_nh(struct nexthop *nh,
1124 int (*cb)(struct fib6_nh *nh, void *arg),
1127 struct nh_info *nhi;
1131 struct nh_group *nhg;
1134 nhg = rcu_dereference_rtnl(nh->nh_grp);
1135 for (i = 0; i < nhg->num_nh; i++) {
1136 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
1138 nhi = rcu_dereference_rtnl(nhge->nh->nh_info);
1139 err = cb(&nhi->fib6_nh, arg);
1144 nhi = rcu_dereference_rtnl(nh->nh_info);
1145 err = cb(&nhi->fib6_nh, arg);
1152 EXPORT_SYMBOL_GPL(nexthop_for_each_fib6_nh);
1154 static int check_src_addr(const struct in6_addr *saddr,
1155 struct netlink_ext_ack *extack)
1157 if (!ipv6_addr_any(saddr)) {
1158 NL_SET_ERR_MSG(extack, "IPv6 routes using source address can not use nexthop objects");
1164 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
1165 struct netlink_ext_ack *extack)
1167 struct nh_info *nhi;
1170 /* fib6_src is unique to a fib6_info and limits the ability to cache
1171 * routes in fib6_nh within a nexthop that is potentially shared
1172 * across multiple fib entries. If the config wants to use source
1173 * routing it can not use nexthop objects. mlxsw also does not allow
1174 * fib6_src on routes.
1176 if (cfg && check_src_addr(&cfg->fc_src, extack) < 0)
1180 struct nh_group *nhg;
1182 nhg = rtnl_dereference(nh->nh_grp);
1185 is_fdb_nh = nhg->fdb_nh;
1187 nhi = rtnl_dereference(nh->nh_info);
1188 if (nhi->family == AF_INET)
1190 is_fdb_nh = nhi->fdb_nh;
1194 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
1200 NL_SET_ERR_MSG(extack, "IPv6 routes can not use an IPv4 nexthop");
1203 EXPORT_SYMBOL_GPL(fib6_check_nexthop);
1205 /* if existing nexthop has ipv6 routes linked to it, need
1206 * to verify this new spec works with ipv6
1208 static int fib6_check_nh_list(struct nexthop *old, struct nexthop *new,
1209 struct netlink_ext_ack *extack)
1211 struct fib6_info *f6i;
1213 if (list_empty(&old->f6i_list))
1216 list_for_each_entry(f6i, &old->f6i_list, nh_list) {
1217 if (check_src_addr(&f6i->fib6_src.addr, extack) < 0)
1221 return fib6_check_nexthop(new, NULL, extack);
1224 static int nexthop_check_scope(struct nh_info *nhi, u8 scope,
1225 struct netlink_ext_ack *extack)
1227 if (scope == RT_SCOPE_HOST && nhi->fib_nhc.nhc_gw_family) {
1228 NL_SET_ERR_MSG(extack,
1229 "Route with host scope can not have a gateway");
1233 if (nhi->fib_nhc.nhc_flags & RTNH_F_ONLINK && scope >= RT_SCOPE_LINK) {
1234 NL_SET_ERR_MSG(extack, "Scope mismatch with nexthop");
1241 /* Invoked by fib add code to verify nexthop by id is ok with
1242 * config for prefix; parts of fib_check_nh not done when nexthop
1245 int fib_check_nexthop(struct nexthop *nh, u8 scope,
1246 struct netlink_ext_ack *extack)
1248 struct nh_info *nhi;
1252 struct nh_group *nhg;
1254 nhg = rtnl_dereference(nh->nh_grp);
1256 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
1261 if (scope == RT_SCOPE_HOST) {
1262 NL_SET_ERR_MSG(extack, "Route with host scope can not have multiple nexthops");
1267 /* all nexthops in a group have the same scope */
1268 nhi = rtnl_dereference(nhg->nh_entries[0].nh->nh_info);
1269 err = nexthop_check_scope(nhi, scope, extack);
1271 nhi = rtnl_dereference(nh->nh_info);
1273 NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
1277 err = nexthop_check_scope(nhi, scope, extack);
1284 static int fib_check_nh_list(struct nexthop *old, struct nexthop *new,
1285 struct netlink_ext_ack *extack)
1287 struct fib_info *fi;
1289 list_for_each_entry(fi, &old->fi_list, nh_list) {
1292 err = fib_check_nexthop(new, fi->fib_scope, extack);
1299 static bool nh_res_nhge_is_balanced(const struct nh_grp_entry *nhge)
1301 return nhge->res.count_buckets == nhge->res.wants_buckets;
1304 static bool nh_res_nhge_is_ow(const struct nh_grp_entry *nhge)
1306 return nhge->res.count_buckets > nhge->res.wants_buckets;
1309 static bool nh_res_nhge_is_uw(const struct nh_grp_entry *nhge)
1311 return nhge->res.count_buckets < nhge->res.wants_buckets;
1314 static bool nh_res_table_is_balanced(const struct nh_res_table *res_table)
1316 return list_empty(&res_table->uw_nh_entries);
1319 static void nh_res_bucket_unset_nh(struct nh_res_bucket *bucket)
1321 struct nh_grp_entry *nhge;
1323 if (bucket->occupied) {
1324 nhge = nh_res_dereference(bucket->nh_entry);
1325 nhge->res.count_buckets--;
1326 bucket->occupied = false;
1330 static void nh_res_bucket_set_nh(struct nh_res_bucket *bucket,
1331 struct nh_grp_entry *nhge)
1333 nh_res_bucket_unset_nh(bucket);
1335 bucket->occupied = true;
1336 rcu_assign_pointer(bucket->nh_entry, nhge);
1337 nhge->res.count_buckets++;
1340 static bool nh_res_bucket_should_migrate(struct nh_res_table *res_table,
1341 struct nh_res_bucket *bucket,
1342 unsigned long *deadline, bool *force)
1344 unsigned long now = jiffies;
1345 struct nh_grp_entry *nhge;
1346 unsigned long idle_point;
1348 if (!bucket->occupied) {
1349 /* The bucket is not occupied, its NHGE pointer is either
1350 * NULL or obsolete. We _have to_ migrate: set force.
1356 nhge = nh_res_dereference(bucket->nh_entry);
1358 /* If the bucket is populated by an underweight or balanced
1359 * nexthop, do not migrate.
1361 if (!nh_res_nhge_is_ow(nhge))
1364 /* At this point we know that the bucket is populated with an
1365 * overweight nexthop. It needs to be migrated to a new nexthop if
1366 * the idle timer of unbalanced timer expired.
1369 idle_point = nh_res_bucket_idle_point(res_table, bucket, now);
1370 if (time_after_eq(now, idle_point)) {
1371 /* The bucket is idle. We _can_ migrate: unset force. */
1376 /* Unbalanced timer of 0 means "never force". */
1377 if (res_table->unbalanced_timer) {
1378 unsigned long unb_point;
1380 unb_point = nh_res_table_unb_point(res_table);
1381 if (time_after(now, unb_point)) {
1382 /* The bucket is not idle, but the unbalanced timer
1383 * expired. We _can_ migrate, but set force anyway,
1384 * so that drivers know to ignore activity reports
1391 nh_res_time_set_deadline(unb_point, deadline);
1394 nh_res_time_set_deadline(idle_point, deadline);
1398 static bool nh_res_bucket_migrate(struct nh_res_table *res_table,
1399 u16 bucket_index, bool notify, bool force)
1401 struct nh_res_bucket *bucket = &res_table->nh_buckets[bucket_index];
1402 struct nh_grp_entry *new_nhge;
1403 struct netlink_ext_ack extack;
1406 new_nhge = list_first_entry_or_null(&res_table->uw_nh_entries,
1407 struct nh_grp_entry,
1409 if (WARN_ON_ONCE(!new_nhge))
1410 /* If this function is called, "bucket" is either not
1411 * occupied, or it belongs to a next hop that is
1412 * overweight. In either case, there ought to be a
1413 * corresponding underweight next hop.
1418 struct nh_grp_entry *old_nhge;
1420 old_nhge = nh_res_dereference(bucket->nh_entry);
1421 err = call_nexthop_res_bucket_notifiers(res_table->net,
1423 bucket_index, force,
1425 new_nhge->nh, &extack);
1427 pr_err_ratelimited("%s\n", extack._msg);
1430 /* It is not possible to veto a forced replacement, so
1431 * just clear the hardware flags from the nexthop
1432 * bucket to indicate to user space that this bucket is
1433 * not correctly populated in hardware.
1435 bucket->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP);
1439 nh_res_bucket_set_nh(bucket, new_nhge);
1440 nh_res_bucket_set_idle(res_table, bucket);
1442 if (nh_res_nhge_is_balanced(new_nhge))
1443 list_del(&new_nhge->res.uw_nh_entry);
1447 #define NH_RES_UPKEEP_DW_MINIMUM_INTERVAL (HZ / 2)
1449 static void nh_res_table_upkeep(struct nh_res_table *res_table, bool notify)
1451 unsigned long now = jiffies;
1452 unsigned long deadline;
1455 /* Deadline is the next time that upkeep should be run. It is the
1456 * earliest time at which one of the buckets might be migrated.
1457 * Start at the most pessimistic estimate: either unbalanced_timer
1458 * from now, or if there is none, idle_timer from now. For each
1459 * encountered time point, call nh_res_time_set_deadline() to
1460 * refine the estimate.
1462 if (res_table->unbalanced_timer)
1463 deadline = now + res_table->unbalanced_timer;
1465 deadline = now + res_table->idle_timer;
1467 for (i = 0; i < res_table->num_nh_buckets; i++) {
1468 struct nh_res_bucket *bucket = &res_table->nh_buckets[i];
1471 if (nh_res_bucket_should_migrate(res_table, bucket,
1472 &deadline, &force)) {
1473 if (!nh_res_bucket_migrate(res_table, i, notify,
1475 unsigned long idle_point;
1477 /* A driver can override the migration
1478 * decision if the HW reports that the
1479 * bucket is actually not idle. Therefore
1480 * remark the bucket as busy again and
1481 * update the deadline.
1483 nh_res_bucket_set_busy(bucket);
1484 idle_point = nh_res_bucket_idle_point(res_table,
1487 nh_res_time_set_deadline(idle_point, &deadline);
1492 /* If the group is still unbalanced, schedule the next upkeep to
1493 * either the deadline computed above, or the minimum deadline,
1494 * whichever comes later.
1496 if (!nh_res_table_is_balanced(res_table)) {
1497 unsigned long now = jiffies;
1498 unsigned long min_deadline;
1500 min_deadline = now + NH_RES_UPKEEP_DW_MINIMUM_INTERVAL;
1501 if (time_before(deadline, min_deadline))
1502 deadline = min_deadline;
1504 queue_delayed_work(system_power_efficient_wq,
1505 &res_table->upkeep_dw, deadline - now);
1509 static void nh_res_table_upkeep_dw(struct work_struct *work)
1511 struct delayed_work *dw = to_delayed_work(work);
1512 struct nh_res_table *res_table;
1514 res_table = container_of(dw, struct nh_res_table, upkeep_dw);
1515 nh_res_table_upkeep(res_table, true);
1518 static void nh_res_table_cancel_upkeep(struct nh_res_table *res_table)
1520 cancel_delayed_work_sync(&res_table->upkeep_dw);
1523 static void nh_res_group_rebalance(struct nh_group *nhg,
1524 struct nh_res_table *res_table)
1526 int prev_upper_bound = 0;
1531 INIT_LIST_HEAD(&res_table->uw_nh_entries);
1533 for (i = 0; i < nhg->num_nh; ++i)
1534 total += nhg->nh_entries[i].weight;
1536 for (i = 0; i < nhg->num_nh; ++i) {
1537 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
1541 upper_bound = DIV_ROUND_CLOSEST(res_table->num_nh_buckets * w,
1543 nhge->res.wants_buckets = upper_bound - prev_upper_bound;
1544 prev_upper_bound = upper_bound;
1546 if (nh_res_nhge_is_uw(nhge)) {
1547 if (list_empty(&res_table->uw_nh_entries))
1548 res_table->unbalanced_since = jiffies;
1549 list_add(&nhge->res.uw_nh_entry,
1550 &res_table->uw_nh_entries);
1555 /* Migrate buckets in res_table so that they reference NHGE's from NHG with
1556 * the right NH ID. Set those buckets that do not have a corresponding NHGE
1557 * entry in NHG as not occupied.
1559 static void nh_res_table_migrate_buckets(struct nh_res_table *res_table,
1560 struct nh_group *nhg)
1564 for (i = 0; i < res_table->num_nh_buckets; i++) {
1565 struct nh_res_bucket *bucket = &res_table->nh_buckets[i];
1566 u32 id = rtnl_dereference(bucket->nh_entry)->nh->id;
1570 for (j = 0; j < nhg->num_nh; j++) {
1571 struct nh_grp_entry *nhge = &nhg->nh_entries[j];
1573 if (nhge->nh->id == id) {
1574 nh_res_bucket_set_nh(bucket, nhge);
1581 nh_res_bucket_unset_nh(bucket);
1585 static void replace_nexthop_grp_res(struct nh_group *oldg,
1586 struct nh_group *newg)
1588 /* For NH group replacement, the new NHG might only have a stub
1589 * hash table with 0 buckets, because the number of buckets was not
1590 * specified. For NH removal, oldg and newg both reference the same
1591 * res_table. So in any case, in the following, we want to work
1592 * with oldg->res_table.
1594 struct nh_res_table *old_res_table = rtnl_dereference(oldg->res_table);
1595 unsigned long prev_unbalanced_since = old_res_table->unbalanced_since;
1596 bool prev_has_uw = !list_empty(&old_res_table->uw_nh_entries);
1598 nh_res_table_cancel_upkeep(old_res_table);
1599 nh_res_table_migrate_buckets(old_res_table, newg);
1600 nh_res_group_rebalance(newg, old_res_table);
1601 if (prev_has_uw && !list_empty(&old_res_table->uw_nh_entries))
1602 old_res_table->unbalanced_since = prev_unbalanced_since;
1603 nh_res_table_upkeep(old_res_table, true);
1606 static void nh_mp_group_rebalance(struct nh_group *nhg)
1612 for (i = 0; i < nhg->num_nh; ++i)
1613 total += nhg->nh_entries[i].weight;
1615 for (i = 0; i < nhg->num_nh; ++i) {
1616 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
1620 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31, total) - 1;
1621 atomic_set(&nhge->mpath.upper_bound, upper_bound);
1625 static void remove_nh_grp_entry(struct net *net, struct nh_grp_entry *nhge,
1626 struct nl_info *nlinfo)
1628 struct nh_grp_entry *nhges, *new_nhges;
1629 struct nexthop *nhp = nhge->nh_parent;
1630 struct netlink_ext_ack extack;
1631 struct nexthop *nh = nhge->nh;
1632 struct nh_group *nhg, *newg;
1637 nhg = rtnl_dereference(nhp->nh_grp);
1640 /* last entry, keep it visible and remove the parent */
1641 if (nhg->num_nh == 1) {
1642 remove_nexthop(net, nhp, nlinfo);
1646 newg->has_v4 = false;
1647 newg->is_multipath = nhg->is_multipath;
1648 newg->mpath = nhg->mpath;
1649 newg->resilient = nhg->resilient;
1650 newg->fdb_nh = nhg->fdb_nh;
1651 newg->num_nh = nhg->num_nh;
1653 /* copy old entries to new except the one getting removed */
1654 nhges = nhg->nh_entries;
1655 new_nhges = newg->nh_entries;
1656 for (i = 0, j = 0; i < nhg->num_nh; ++i) {
1657 struct nh_info *nhi;
1659 /* current nexthop getting removed */
1660 if (nhg->nh_entries[i].nh == nh) {
1665 nhi = rtnl_dereference(nhges[i].nh->nh_info);
1666 if (nhi->family == AF_INET)
1667 newg->has_v4 = true;
1669 list_del(&nhges[i].nh_list);
1670 new_nhges[j].nh_parent = nhges[i].nh_parent;
1671 new_nhges[j].nh = nhges[i].nh;
1672 new_nhges[j].weight = nhges[i].weight;
1673 list_add(&new_nhges[j].nh_list, &new_nhges[j].nh->grp_list);
1678 nh_mp_group_rebalance(newg);
1679 else if (newg->resilient)
1680 replace_nexthop_grp_res(nhg, newg);
1682 rcu_assign_pointer(nhp->nh_grp, newg);
1684 list_del(&nhge->nh_list);
1685 nexthop_put(nhge->nh);
1687 /* Removal of a NH from a resilient group is notified through
1688 * bucket notifications.
1691 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, nhp,
1694 pr_err("%s\n", extack._msg);
1698 nexthop_notify(RTM_NEWNEXTHOP, nhp, nlinfo);
1701 static void remove_nexthop_from_groups(struct net *net, struct nexthop *nh,
1702 struct nl_info *nlinfo)
1704 struct nh_grp_entry *nhge, *tmp;
1706 list_for_each_entry_safe(nhge, tmp, &nh->grp_list, nh_list)
1707 remove_nh_grp_entry(net, nhge, nlinfo);
1709 /* make sure all see the newly published array before releasing rtnl */
1713 static void remove_nexthop_group(struct nexthop *nh, struct nl_info *nlinfo)
1715 struct nh_group *nhg = rcu_dereference_rtnl(nh->nh_grp);
1716 struct nh_res_table *res_table;
1717 int i, num_nh = nhg->num_nh;
1719 for (i = 0; i < num_nh; ++i) {
1720 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
1722 if (WARN_ON(!nhge->nh))
1725 list_del_init(&nhge->nh_list);
1728 if (nhg->resilient) {
1729 res_table = rtnl_dereference(nhg->res_table);
1730 nh_res_table_cancel_upkeep(res_table);
1734 /* not called for nexthop replace */
1735 static void __remove_nexthop_fib(struct net *net, struct nexthop *nh)
1737 struct fib6_info *f6i, *tmp;
1738 bool do_flush = false;
1739 struct fib_info *fi;
1741 list_for_each_entry(fi, &nh->fi_list, nh_list) {
1742 fi->fib_flags |= RTNH_F_DEAD;
1748 /* ip6_del_rt removes the entry from this list hence the _safe */
1749 list_for_each_entry_safe(f6i, tmp, &nh->f6i_list, nh_list) {
1750 /* __ip6_del_rt does a release, so do a hold here */
1751 fib6_info_hold(f6i);
1752 ipv6_stub->ip6_del_rt(net, f6i,
1753 !net->ipv4.sysctl_nexthop_compat_mode);
1757 static void __remove_nexthop(struct net *net, struct nexthop *nh,
1758 struct nl_info *nlinfo)
1760 __remove_nexthop_fib(net, nh);
1763 remove_nexthop_group(nh, nlinfo);
1765 struct nh_info *nhi;
1767 nhi = rtnl_dereference(nh->nh_info);
1768 if (nhi->fib_nhc.nhc_dev)
1769 hlist_del(&nhi->dev_hash);
1771 remove_nexthop_from_groups(net, nh, nlinfo);
1775 static void remove_nexthop(struct net *net, struct nexthop *nh,
1776 struct nl_info *nlinfo)
1778 call_nexthop_notifiers(net, NEXTHOP_EVENT_DEL, nh, NULL);
1780 /* remove from the tree */
1781 rb_erase(&nh->rb_node, &net->nexthop.rb_root);
1784 nexthop_notify(RTM_DELNEXTHOP, nh, nlinfo);
1786 __remove_nexthop(net, nh, nlinfo);
1787 nh_base_seq_inc(net);
1792 /* if any FIB entries reference this nexthop, any dst entries
1793 * need to be regenerated
1795 static void nh_rt_cache_flush(struct net *net, struct nexthop *nh)
1797 struct fib6_info *f6i;
1799 if (!list_empty(&nh->fi_list))
1800 rt_cache_flush(net);
1802 list_for_each_entry(f6i, &nh->f6i_list, nh_list)
1803 ipv6_stub->fib6_update_sernum(net, f6i);
1806 static int replace_nexthop_grp(struct net *net, struct nexthop *old,
1807 struct nexthop *new, const struct nh_config *cfg,
1808 struct netlink_ext_ack *extack)
1810 struct nh_res_table *tmp_table = NULL;
1811 struct nh_res_table *new_res_table;
1812 struct nh_res_table *old_res_table;
1813 struct nh_group *oldg, *newg;
1816 if (!new->is_group) {
1817 NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with a nexthop.");
1821 oldg = rtnl_dereference(old->nh_grp);
1822 newg = rtnl_dereference(new->nh_grp);
1824 if (newg->mpath != oldg->mpath) {
1825 NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with one of a different type.");
1830 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new,
1834 } else if (newg->resilient) {
1835 new_res_table = rtnl_dereference(newg->res_table);
1836 old_res_table = rtnl_dereference(oldg->res_table);
1838 /* Accept if num_nh_buckets was not given, but if it was
1839 * given, demand that the value be correct.
1841 if (cfg->nh_grp_res_has_num_buckets &&
1842 cfg->nh_grp_res_num_buckets !=
1843 old_res_table->num_nh_buckets) {
1844 NL_SET_ERR_MSG(extack, "Can not change number of buckets of a resilient nexthop group.");
1848 /* Emit a pre-replace notification so that listeners could veto
1849 * a potentially unsupported configuration. Otherwise,
1850 * individual bucket replacement notifications would need to be
1851 * vetoed, which is something that should only happen if the
1852 * bucket is currently active.
1854 err = call_nexthop_res_table_notifiers(net, new, extack);
1858 if (cfg->nh_grp_res_has_idle_timer)
1859 old_res_table->idle_timer = cfg->nh_grp_res_idle_timer;
1860 if (cfg->nh_grp_res_has_unbalanced_timer)
1861 old_res_table->unbalanced_timer =
1862 cfg->nh_grp_res_unbalanced_timer;
1864 replace_nexthop_grp_res(oldg, newg);
1866 tmp_table = new_res_table;
1867 rcu_assign_pointer(newg->res_table, old_res_table);
1868 rcu_assign_pointer(newg->spare->res_table, old_res_table);
1871 /* update parents - used by nexthop code for cleanup */
1872 for (i = 0; i < newg->num_nh; i++)
1873 newg->nh_entries[i].nh_parent = old;
1875 rcu_assign_pointer(old->nh_grp, newg);
1877 if (newg->resilient) {
1878 rcu_assign_pointer(oldg->res_table, tmp_table);
1879 rcu_assign_pointer(oldg->spare->res_table, tmp_table);
1882 for (i = 0; i < oldg->num_nh; i++)
1883 oldg->nh_entries[i].nh_parent = new;
1885 rcu_assign_pointer(new->nh_grp, oldg);
1890 static void nh_group_v4_update(struct nh_group *nhg)
1892 struct nh_grp_entry *nhges;
1893 bool has_v4 = false;
1896 nhges = nhg->nh_entries;
1897 for (i = 0; i < nhg->num_nh; i++) {
1898 struct nh_info *nhi;
1900 nhi = rtnl_dereference(nhges[i].nh->nh_info);
1901 if (nhi->family == AF_INET)
1904 nhg->has_v4 = has_v4;
1907 static int replace_nexthop_single_notify_res(struct net *net,
1908 struct nh_res_table *res_table,
1909 struct nexthop *old,
1910 struct nh_info *oldi,
1911 struct nh_info *newi,
1912 struct netlink_ext_ack *extack)
1914 u32 nhg_id = res_table->nhg_id;
1918 for (i = 0; i < res_table->num_nh_buckets; i++) {
1919 struct nh_res_bucket *bucket = &res_table->nh_buckets[i];
1920 struct nh_grp_entry *nhge;
1922 nhge = rtnl_dereference(bucket->nh_entry);
1923 if (nhge->nh == old) {
1924 err = __call_nexthop_res_bucket_notifiers(net, nhg_id,
1937 struct nh_res_bucket *bucket = &res_table->nh_buckets[i];
1938 struct nh_grp_entry *nhge;
1940 nhge = rtnl_dereference(bucket->nh_entry);
1941 if (nhge->nh == old)
1942 __call_nexthop_res_bucket_notifiers(net, nhg_id, i,
1949 static int replace_nexthop_single_notify(struct net *net,
1950 struct nexthop *group_nh,
1951 struct nexthop *old,
1952 struct nh_info *oldi,
1953 struct nh_info *newi,
1954 struct netlink_ext_ack *extack)
1956 struct nh_group *nhg = rtnl_dereference(group_nh->nh_grp);
1957 struct nh_res_table *res_table;
1960 return call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE,
1962 } else if (nhg->resilient) {
1963 res_table = rtnl_dereference(nhg->res_table);
1964 return replace_nexthop_single_notify_res(net, res_table,
1972 static int replace_nexthop_single(struct net *net, struct nexthop *old,
1973 struct nexthop *new,
1974 struct netlink_ext_ack *extack)
1976 u8 old_protocol, old_nh_flags;
1977 struct nh_info *oldi, *newi;
1978 struct nh_grp_entry *nhge;
1981 if (new->is_group) {
1982 NL_SET_ERR_MSG(extack, "Can not replace a nexthop with a nexthop group.");
1986 err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new, extack);
1990 /* Hardware flags were set on 'old' as 'new' is not in the red-black
1991 * tree. Therefore, inherit the flags from 'old' to 'new'.
1993 new->nh_flags |= old->nh_flags & (RTNH_F_OFFLOAD | RTNH_F_TRAP);
1995 oldi = rtnl_dereference(old->nh_info);
1996 newi = rtnl_dereference(new->nh_info);
1998 newi->nh_parent = old;
1999 oldi->nh_parent = new;
2001 old_protocol = old->protocol;
2002 old_nh_flags = old->nh_flags;
2004 old->protocol = new->protocol;
2005 old->nh_flags = new->nh_flags;
2007 rcu_assign_pointer(old->nh_info, newi);
2008 rcu_assign_pointer(new->nh_info, oldi);
2010 /* Send a replace notification for all the groups using the nexthop. */
2011 list_for_each_entry(nhge, &old->grp_list, nh_list) {
2012 struct nexthop *nhp = nhge->nh_parent;
2014 err = replace_nexthop_single_notify(net, nhp, old, oldi, newi,
2020 /* When replacing an IPv4 nexthop with an IPv6 nexthop, potentially
2021 * update IPv4 indication in all the groups using the nexthop.
2023 if (oldi->family == AF_INET && newi->family == AF_INET6) {
2024 list_for_each_entry(nhge, &old->grp_list, nh_list) {
2025 struct nexthop *nhp = nhge->nh_parent;
2026 struct nh_group *nhg;
2028 nhg = rtnl_dereference(nhp->nh_grp);
2029 nh_group_v4_update(nhg);
2036 rcu_assign_pointer(new->nh_info, newi);
2037 rcu_assign_pointer(old->nh_info, oldi);
2038 old->nh_flags = old_nh_flags;
2039 old->protocol = old_protocol;
2040 oldi->nh_parent = old;
2041 newi->nh_parent = new;
2042 list_for_each_entry_continue_reverse(nhge, &old->grp_list, nh_list) {
2043 struct nexthop *nhp = nhge->nh_parent;
2045 replace_nexthop_single_notify(net, nhp, old, newi, oldi, NULL);
2047 call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, old, extack);
2051 static void __nexthop_replace_notify(struct net *net, struct nexthop *nh,
2052 struct nl_info *info)
2054 struct fib6_info *f6i;
2056 if (!list_empty(&nh->fi_list)) {
2057 struct fib_info *fi;
2059 /* expectation is a few fib_info per nexthop and then
2060 * a lot of routes per fib_info. So mark the fib_info
2061 * and then walk the fib tables once
2063 list_for_each_entry(fi, &nh->fi_list, nh_list)
2064 fi->nh_updated = true;
2066 fib_info_notify_update(net, info);
2068 list_for_each_entry(fi, &nh->fi_list, nh_list)
2069 fi->nh_updated = false;
2072 list_for_each_entry(f6i, &nh->f6i_list, nh_list)
2073 ipv6_stub->fib6_rt_update(net, f6i, info);
2076 /* send RTM_NEWROUTE with REPLACE flag set for all FIB entries
2077 * linked to this nexthop and for all groups that the nexthop
2080 static void nexthop_replace_notify(struct net *net, struct nexthop *nh,
2081 struct nl_info *info)
2083 struct nh_grp_entry *nhge;
2085 __nexthop_replace_notify(net, nh, info);
2087 list_for_each_entry(nhge, &nh->grp_list, nh_list)
2088 __nexthop_replace_notify(net, nhge->nh_parent, info);
2091 static int replace_nexthop(struct net *net, struct nexthop *old,
2092 struct nexthop *new, const struct nh_config *cfg,
2093 struct netlink_ext_ack *extack)
2095 bool new_is_reject = false;
2096 struct nh_grp_entry *nhge;
2099 /* check that existing FIB entries are ok with the
2100 * new nexthop definition
2102 err = fib_check_nh_list(old, new, extack);
2106 err = fib6_check_nh_list(old, new, extack);
2110 if (!new->is_group) {
2111 struct nh_info *nhi = rtnl_dereference(new->nh_info);
2113 new_is_reject = nhi->reject_nh;
2116 list_for_each_entry(nhge, &old->grp_list, nh_list) {
2117 /* if new nexthop is a blackhole, any groups using this
2118 * nexthop cannot have more than 1 path
2120 if (new_is_reject &&
2121 nexthop_num_path(nhge->nh_parent) > 1) {
2122 NL_SET_ERR_MSG(extack, "Blackhole nexthop can not be a member of a group with more than one path");
2126 err = fib_check_nh_list(nhge->nh_parent, new, extack);
2130 err = fib6_check_nh_list(nhge->nh_parent, new, extack);
2136 err = replace_nexthop_grp(net, old, new, cfg, extack);
2138 err = replace_nexthop_single(net, old, new, extack);
2141 nh_rt_cache_flush(net, old);
2143 __remove_nexthop(net, new, NULL);
2150 /* called with rtnl_lock held */
2151 static int insert_nexthop(struct net *net, struct nexthop *new_nh,
2152 struct nh_config *cfg, struct netlink_ext_ack *extack)
2154 struct rb_node **pp, *parent = NULL, *next;
2155 struct rb_root *root = &net->nexthop.rb_root;
2156 bool replace = !!(cfg->nlflags & NLM_F_REPLACE);
2157 bool create = !!(cfg->nlflags & NLM_F_CREATE);
2158 u32 new_id = new_nh->id;
2159 int replace_notify = 0;
2162 pp = &root->rb_node;
2172 nh = rb_entry(parent, struct nexthop, rb_node);
2173 if (new_id < nh->id) {
2174 pp = &next->rb_left;
2175 } else if (new_id > nh->id) {
2176 pp = &next->rb_right;
2177 } else if (replace) {
2178 rc = replace_nexthop(net, nh, new_nh, cfg, extack);
2180 new_nh = nh; /* send notification with old nh */
2185 /* id already exists and not a replace */
2190 if (replace && !create) {
2191 NL_SET_ERR_MSG(extack, "Replace specified without create and no entry exists");
2196 if (new_nh->is_group) {
2197 struct nh_group *nhg = rtnl_dereference(new_nh->nh_grp);
2198 struct nh_res_table *res_table;
2200 if (nhg->resilient) {
2201 res_table = rtnl_dereference(nhg->res_table);
2203 /* Not passing the number of buckets is OK when
2204 * replacing, but not when creating a new group.
2206 if (!cfg->nh_grp_res_has_num_buckets) {
2207 NL_SET_ERR_MSG(extack, "Number of buckets not specified for nexthop group insertion");
2212 nh_res_group_rebalance(nhg, res_table);
2214 /* Do not send bucket notifications, we do full
2215 * notification below.
2217 nh_res_table_upkeep(res_table, false);
2221 rb_link_node_rcu(&new_nh->rb_node, parent, pp);
2222 rb_insert_color(&new_nh->rb_node, root);
2224 /* The initial insertion is a full notification for mpath as well
2225 * as resilient groups.
2227 rc = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new_nh, extack);
2229 rb_erase(&new_nh->rb_node, &net->nexthop.rb_root);
2233 nh_base_seq_inc(net);
2234 nexthop_notify(RTM_NEWNEXTHOP, new_nh, &cfg->nlinfo);
2235 if (replace_notify && net->ipv4.sysctl_nexthop_compat_mode)
2236 nexthop_replace_notify(net, new_nh, &cfg->nlinfo);
2243 /* remove all nexthops tied to a device being deleted */
2244 static void nexthop_flush_dev(struct net_device *dev, unsigned long event)
2246 unsigned int hash = nh_dev_hashfn(dev->ifindex);
2247 struct net *net = dev_net(dev);
2248 struct hlist_head *head = &net->nexthop.devhash[hash];
2249 struct hlist_node *n;
2250 struct nh_info *nhi;
2252 hlist_for_each_entry_safe(nhi, n, head, dev_hash) {
2253 if (nhi->fib_nhc.nhc_dev != dev)
2256 if (nhi->reject_nh &&
2257 (event == NETDEV_DOWN || event == NETDEV_CHANGE))
2260 remove_nexthop(net, nhi->nh_parent, NULL);
2264 /* rtnl; called when net namespace is deleted */
2265 static void flush_all_nexthops(struct net *net)
2267 struct rb_root *root = &net->nexthop.rb_root;
2268 struct rb_node *node;
2271 while ((node = rb_first(root))) {
2272 nh = rb_entry(node, struct nexthop, rb_node);
2273 remove_nexthop(net, nh, NULL);
2278 static struct nexthop *nexthop_create_group(struct net *net,
2279 struct nh_config *cfg)
2281 struct nlattr *grps_attr = cfg->nh_grp;
2282 struct nexthop_grp *entry = nla_data(grps_attr);
2283 u16 num_nh = nla_len(grps_attr) / sizeof(*entry);
2284 struct nh_group *nhg;
2289 if (WARN_ON(!num_nh))
2290 return ERR_PTR(-EINVAL);
2292 nh = nexthop_alloc();
2294 return ERR_PTR(-ENOMEM);
2298 nhg = nexthop_grp_alloc(num_nh);
2301 return ERR_PTR(-ENOMEM);
2304 /* spare group used for removals */
2305 nhg->spare = nexthop_grp_alloc(num_nh);
2309 return ERR_PTR(-ENOMEM);
2311 nhg->spare->spare = nhg;
2313 for (i = 0; i < nhg->num_nh; ++i) {
2314 struct nexthop *nhe;
2315 struct nh_info *nhi;
2317 nhe = nexthop_find_by_id(net, entry[i].id);
2318 if (!nexthop_get(nhe)) {
2323 nhi = rtnl_dereference(nhe->nh_info);
2324 if (nhi->family == AF_INET)
2327 nhg->nh_entries[i].nh = nhe;
2328 nhg->nh_entries[i].weight = entry[i].weight + 1;
2329 list_add(&nhg->nh_entries[i].nh_list, &nhe->grp_list);
2330 nhg->nh_entries[i].nh_parent = nh;
2333 if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_MPATH) {
2335 nhg->is_multipath = true;
2336 } else if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_RES) {
2337 struct nh_res_table *res_table;
2339 /* Bounce resilient groups for now. */
2343 res_table = nexthop_res_table_alloc(net, cfg->nh_id, cfg);
2349 rcu_assign_pointer(nhg->spare->res_table, res_table);
2350 rcu_assign_pointer(nhg->res_table, res_table);
2351 nhg->resilient = true;
2352 nhg->is_multipath = true;
2355 WARN_ON_ONCE(nhg->mpath + nhg->resilient != 1);
2358 nh_mp_group_rebalance(nhg);
2363 rcu_assign_pointer(nh->nh_grp, nhg);
2368 for (i--; i >= 0; --i) {
2369 list_del(&nhg->nh_entries[i].nh_list);
2370 nexthop_put(nhg->nh_entries[i].nh);
2377 return ERR_PTR(err);
2380 static int nh_create_ipv4(struct net *net, struct nexthop *nh,
2381 struct nh_info *nhi, struct nh_config *cfg,
2382 struct netlink_ext_ack *extack)
2384 struct fib_nh *fib_nh = &nhi->fib_nh;
2385 struct fib_config fib_cfg = {
2386 .fc_oif = cfg->nh_ifindex,
2387 .fc_gw4 = cfg->gw.ipv4,
2388 .fc_gw_family = cfg->gw.ipv4 ? AF_INET : 0,
2389 .fc_flags = cfg->nh_flags,
2390 .fc_encap = cfg->nh_encap,
2391 .fc_encap_type = cfg->nh_encap_type,
2393 u32 tb_id = (cfg->dev ? l3mdev_fib_table(cfg->dev) : RT_TABLE_MAIN);
2396 err = fib_nh_init(net, fib_nh, &fib_cfg, 1, extack);
2398 fib_nh_release(net, fib_nh);
2405 /* sets nh_dev if successful */
2406 err = fib_check_nh(net, fib_nh, tb_id, 0, extack);
2408 nh->nh_flags = fib_nh->fib_nh_flags;
2409 fib_info_update_nhc_saddr(net, &fib_nh->nh_common,
2410 fib_nh->fib_nh_scope);
2412 fib_nh_release(net, fib_nh);
2418 static int nh_create_ipv6(struct net *net, struct nexthop *nh,
2419 struct nh_info *nhi, struct nh_config *cfg,
2420 struct netlink_ext_ack *extack)
2422 struct fib6_nh *fib6_nh = &nhi->fib6_nh;
2423 struct fib6_config fib6_cfg = {
2424 .fc_table = l3mdev_fib_table(cfg->dev),
2425 .fc_ifindex = cfg->nh_ifindex,
2426 .fc_gateway = cfg->gw.ipv6,
2427 .fc_flags = cfg->nh_flags,
2428 .fc_encap = cfg->nh_encap,
2429 .fc_encap_type = cfg->nh_encap_type,
2430 .fc_is_fdb = cfg->nh_fdb,
2434 if (!ipv6_addr_any(&cfg->gw.ipv6))
2435 fib6_cfg.fc_flags |= RTF_GATEWAY;
2437 /* sets nh_dev if successful */
2438 err = ipv6_stub->fib6_nh_init(net, fib6_nh, &fib6_cfg, GFP_KERNEL,
2441 ipv6_stub->fib6_nh_release(fib6_nh);
2443 nh->nh_flags = fib6_nh->fib_nh_flags;
2448 static struct nexthop *nexthop_create(struct net *net, struct nh_config *cfg,
2449 struct netlink_ext_ack *extack)
2451 struct nh_info *nhi;
2455 nh = nexthop_alloc();
2457 return ERR_PTR(-ENOMEM);
2459 nhi = kzalloc(sizeof(*nhi), GFP_KERNEL);
2462 return ERR_PTR(-ENOMEM);
2465 nh->nh_flags = cfg->nh_flags;
2468 nhi->nh_parent = nh;
2469 nhi->family = cfg->nh_family;
2470 nhi->fib_nhc.nhc_scope = RT_SCOPE_LINK;
2475 if (cfg->nh_blackhole) {
2477 cfg->nh_ifindex = net->loopback_dev->ifindex;
2480 switch (cfg->nh_family) {
2482 err = nh_create_ipv4(net, nh, nhi, cfg, extack);
2485 err = nh_create_ipv6(net, nh, nhi, cfg, extack);
2492 return ERR_PTR(err);
2495 /* add the entry to the device based hash */
2497 nexthop_devhash_add(net, nhi);
2499 rcu_assign_pointer(nh->nh_info, nhi);
2504 /* called with rtnl lock held */
2505 static struct nexthop *nexthop_add(struct net *net, struct nh_config *cfg,
2506 struct netlink_ext_ack *extack)
2511 if (cfg->nlflags & NLM_F_REPLACE && !cfg->nh_id) {
2512 NL_SET_ERR_MSG(extack, "Replace requires nexthop id");
2513 return ERR_PTR(-EINVAL);
2517 cfg->nh_id = nh_find_unused_id(net);
2519 NL_SET_ERR_MSG(extack, "No unused id");
2520 return ERR_PTR(-EINVAL);
2525 nh = nexthop_create_group(net, cfg);
2527 nh = nexthop_create(net, cfg, extack);
2532 refcount_set(&nh->refcnt, 1);
2533 nh->id = cfg->nh_id;
2534 nh->protocol = cfg->nh_protocol;
2537 err = insert_nexthop(net, nh, cfg, extack);
2539 __remove_nexthop(net, nh, NULL);
2547 static int rtm_nh_get_timer(struct nlattr *attr, unsigned long fallback,
2548 unsigned long *timer_p, bool *has_p,
2549 struct netlink_ext_ack *extack)
2551 unsigned long timer;
2555 *timer_p = fallback;
2560 value = nla_get_u32(attr);
2561 timer = clock_t_to_jiffies(value);
2562 if (timer == ~0UL) {
2563 NL_SET_ERR_MSG(extack, "Timer value too large");
2572 static int rtm_to_nh_config_grp_res(struct nlattr *res, struct nh_config *cfg,
2573 struct netlink_ext_ack *extack)
2575 struct nlattr *tb[ARRAY_SIZE(rtm_nh_res_policy_new)] = {};
2579 err = nla_parse_nested(tb,
2580 ARRAY_SIZE(rtm_nh_res_policy_new) - 1,
2581 res, rtm_nh_res_policy_new, extack);
2586 if (tb[NHA_RES_GROUP_BUCKETS]) {
2587 cfg->nh_grp_res_num_buckets =
2588 nla_get_u16(tb[NHA_RES_GROUP_BUCKETS]);
2589 cfg->nh_grp_res_has_num_buckets = true;
2590 if (!cfg->nh_grp_res_num_buckets) {
2591 NL_SET_ERR_MSG(extack, "Number of buckets needs to be non-0");
2596 err = rtm_nh_get_timer(tb[NHA_RES_GROUP_IDLE_TIMER],
2597 NH_RES_DEFAULT_IDLE_TIMER,
2598 &cfg->nh_grp_res_idle_timer,
2599 &cfg->nh_grp_res_has_idle_timer,
2604 return rtm_nh_get_timer(tb[NHA_RES_GROUP_UNBALANCED_TIMER],
2605 NH_RES_DEFAULT_UNBALANCED_TIMER,
2606 &cfg->nh_grp_res_unbalanced_timer,
2607 &cfg->nh_grp_res_has_unbalanced_timer,
2611 static int rtm_to_nh_config(struct net *net, struct sk_buff *skb,
2612 struct nlmsghdr *nlh, struct nh_config *cfg,
2613 struct netlink_ext_ack *extack)
2615 struct nhmsg *nhm = nlmsg_data(nlh);
2616 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_new)];
2619 err = nlmsg_parse(nlh, sizeof(*nhm), tb,
2620 ARRAY_SIZE(rtm_nh_policy_new) - 1,
2621 rtm_nh_policy_new, extack);
2626 if (nhm->resvd || nhm->nh_scope) {
2627 NL_SET_ERR_MSG(extack, "Invalid values in ancillary header");
2630 if (nhm->nh_flags & ~NEXTHOP_VALID_USER_FLAGS) {
2631 NL_SET_ERR_MSG(extack, "Invalid nexthop flags in ancillary header");
2635 switch (nhm->nh_family) {
2644 NL_SET_ERR_MSG(extack, "Invalid address family");
2648 memset(cfg, 0, sizeof(*cfg));
2649 cfg->nlflags = nlh->nlmsg_flags;
2650 cfg->nlinfo.portid = NETLINK_CB(skb).portid;
2651 cfg->nlinfo.nlh = nlh;
2652 cfg->nlinfo.nl_net = net;
2654 cfg->nh_family = nhm->nh_family;
2655 cfg->nh_protocol = nhm->nh_protocol;
2656 cfg->nh_flags = nhm->nh_flags;
2659 cfg->nh_id = nla_get_u32(tb[NHA_ID]);
2662 if (tb[NHA_OIF] || tb[NHA_BLACKHOLE] ||
2663 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE]) {
2664 NL_SET_ERR_MSG(extack, "Fdb attribute can not be used with encap, oif or blackhole");
2667 if (nhm->nh_flags) {
2668 NL_SET_ERR_MSG(extack, "Unsupported nexthop flags in ancillary header");
2671 cfg->nh_fdb = nla_get_flag(tb[NHA_FDB]);
2674 if (tb[NHA_GROUP]) {
2675 if (nhm->nh_family != AF_UNSPEC) {
2676 NL_SET_ERR_MSG(extack, "Invalid family for group");
2679 cfg->nh_grp = tb[NHA_GROUP];
2681 cfg->nh_grp_type = NEXTHOP_GRP_TYPE_MPATH;
2682 if (tb[NHA_GROUP_TYPE])
2683 cfg->nh_grp_type = nla_get_u16(tb[NHA_GROUP_TYPE]);
2685 if (cfg->nh_grp_type > NEXTHOP_GRP_TYPE_MAX) {
2686 NL_SET_ERR_MSG(extack, "Invalid group type");
2689 err = nh_check_attr_group(net, tb, ARRAY_SIZE(tb),
2690 cfg->nh_grp_type, extack);
2694 if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_RES)
2695 err = rtm_to_nh_config_grp_res(tb[NHA_RES_GROUP],
2698 /* no other attributes should be set */
2702 if (tb[NHA_BLACKHOLE]) {
2703 if (tb[NHA_GATEWAY] || tb[NHA_OIF] ||
2704 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE] || tb[NHA_FDB]) {
2705 NL_SET_ERR_MSG(extack, "Blackhole attribute can not be used with gateway, oif, encap or fdb");
2709 cfg->nh_blackhole = 1;
2714 if (!cfg->nh_fdb && !tb[NHA_OIF]) {
2715 NL_SET_ERR_MSG(extack, "Device attribute required for non-blackhole and non-fdb nexthops");
2719 if (!cfg->nh_fdb && tb[NHA_OIF]) {
2720 cfg->nh_ifindex = nla_get_u32(tb[NHA_OIF]);
2721 if (cfg->nh_ifindex)
2722 cfg->dev = __dev_get_by_index(net, cfg->nh_ifindex);
2725 NL_SET_ERR_MSG(extack, "Invalid device index");
2727 } else if (!(cfg->dev->flags & IFF_UP)) {
2728 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2731 } else if (!netif_carrier_ok(cfg->dev)) {
2732 NL_SET_ERR_MSG(extack, "Carrier for nexthop device is down");
2739 if (tb[NHA_GATEWAY]) {
2740 struct nlattr *gwa = tb[NHA_GATEWAY];
2742 switch (cfg->nh_family) {
2744 if (nla_len(gwa) != sizeof(u32)) {
2745 NL_SET_ERR_MSG(extack, "Invalid gateway");
2748 cfg->gw.ipv4 = nla_get_be32(gwa);
2751 if (nla_len(gwa) != sizeof(struct in6_addr)) {
2752 NL_SET_ERR_MSG(extack, "Invalid gateway");
2755 cfg->gw.ipv6 = nla_get_in6_addr(gwa);
2758 NL_SET_ERR_MSG(extack,
2759 "Unknown address family for gateway");
2763 /* device only nexthop (no gateway) */
2764 if (cfg->nh_flags & RTNH_F_ONLINK) {
2765 NL_SET_ERR_MSG(extack,
2766 "ONLINK flag can not be set for nexthop without a gateway");
2771 if (tb[NHA_ENCAP]) {
2772 cfg->nh_encap = tb[NHA_ENCAP];
2774 if (!tb[NHA_ENCAP_TYPE]) {
2775 NL_SET_ERR_MSG(extack, "LWT encapsulation type is missing");
2779 cfg->nh_encap_type = nla_get_u16(tb[NHA_ENCAP_TYPE]);
2780 err = lwtunnel_valid_encap_type(cfg->nh_encap_type, extack);
2784 } else if (tb[NHA_ENCAP_TYPE]) {
2785 NL_SET_ERR_MSG(extack, "LWT encapsulation attribute is missing");
2796 static int rtm_new_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
2797 struct netlink_ext_ack *extack)
2799 struct net *net = sock_net(skb->sk);
2800 struct nh_config cfg;
2804 err = rtm_to_nh_config(net, skb, nlh, &cfg, extack);
2806 nh = nexthop_add(net, &cfg, extack);
2814 static int __nh_valid_get_del_req(const struct nlmsghdr *nlh,
2815 struct nlattr **tb, u32 *id,
2816 struct netlink_ext_ack *extack)
2818 struct nhmsg *nhm = nlmsg_data(nlh);
2820 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) {
2821 NL_SET_ERR_MSG(extack, "Invalid values in header");
2826 NL_SET_ERR_MSG(extack, "Nexthop id is missing");
2830 *id = nla_get_u32(tb[NHA_ID]);
2832 NL_SET_ERR_MSG(extack, "Invalid nexthop id");
2839 static int nh_valid_get_del_req(const struct nlmsghdr *nlh, u32 *id,
2840 struct netlink_ext_ack *extack)
2842 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_get)];
2845 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb,
2846 ARRAY_SIZE(rtm_nh_policy_get) - 1,
2847 rtm_nh_policy_get, extack);
2851 return __nh_valid_get_del_req(nlh, tb, id, extack);
2855 static int rtm_del_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
2856 struct netlink_ext_ack *extack)
2858 struct net *net = sock_net(skb->sk);
2859 struct nl_info nlinfo = {
2862 .portid = NETLINK_CB(skb).portid,
2868 err = nh_valid_get_del_req(nlh, &id, extack);
2872 nh = nexthop_find_by_id(net, id);
2876 remove_nexthop(net, nh, &nlinfo);
2882 static int rtm_get_nexthop(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2883 struct netlink_ext_ack *extack)
2885 struct net *net = sock_net(in_skb->sk);
2886 struct sk_buff *skb = NULL;
2891 err = nh_valid_get_del_req(nlh, &id, extack);
2896 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2901 nh = nexthop_find_by_id(net, id);
2905 err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, NETLINK_CB(in_skb).portid,
2908 WARN_ON(err == -EMSGSIZE);
2912 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2920 struct nh_dump_filter {
2927 static bool nh_dump_filtered(struct nexthop *nh,
2928 struct nh_dump_filter *filter, u8 family)
2930 const struct net_device *dev;
2931 const struct nh_info *nhi;
2933 if (filter->group_filter && !nh->is_group)
2936 if (!filter->dev_idx && !filter->master_idx && !family)
2942 nhi = rtnl_dereference(nh->nh_info);
2943 if (family && nhi->family != family)
2946 dev = nhi->fib_nhc.nhc_dev;
2947 if (filter->dev_idx && (!dev || dev->ifindex != filter->dev_idx))
2950 if (filter->master_idx) {
2951 struct net_device *master;
2956 master = netdev_master_upper_dev_get((struct net_device *)dev);
2957 if (!master || master->ifindex != filter->master_idx)
2964 static int __nh_valid_dump_req(const struct nlmsghdr *nlh, struct nlattr **tb,
2965 struct nh_dump_filter *filter,
2966 struct netlink_ext_ack *extack)
2972 idx = nla_get_u32(tb[NHA_OIF]);
2973 if (idx > INT_MAX) {
2974 NL_SET_ERR_MSG(extack, "Invalid device index");
2977 filter->dev_idx = idx;
2979 if (tb[NHA_MASTER]) {
2980 idx = nla_get_u32(tb[NHA_MASTER]);
2981 if (idx > INT_MAX) {
2982 NL_SET_ERR_MSG(extack, "Invalid master device index");
2985 filter->master_idx = idx;
2987 filter->group_filter = nla_get_flag(tb[NHA_GROUPS]);
2988 filter->fdb_filter = nla_get_flag(tb[NHA_FDB]);
2990 nhm = nlmsg_data(nlh);
2991 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) {
2992 NL_SET_ERR_MSG(extack, "Invalid values in header for nexthop dump request");
2999 static int nh_valid_dump_req(const struct nlmsghdr *nlh,
3000 struct nh_dump_filter *filter,
3001 struct netlink_callback *cb)
3003 struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_dump)];
3006 err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb,
3007 ARRAY_SIZE(rtm_nh_policy_dump) - 1,
3008 rtm_nh_policy_dump, cb->extack);
3012 return __nh_valid_dump_req(nlh, tb, filter, cb->extack);
3015 struct rtm_dump_nh_ctx {
3019 static struct rtm_dump_nh_ctx *
3020 rtm_dump_nh_ctx(struct netlink_callback *cb)
3022 struct rtm_dump_nh_ctx *ctx = (void *)cb->ctx;
3024 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
3028 static int rtm_dump_walk_nexthops(struct sk_buff *skb,
3029 struct netlink_callback *cb,
3030 struct rb_root *root,
3031 struct rtm_dump_nh_ctx *ctx,
3032 int (*nh_cb)(struct sk_buff *skb,
3033 struct netlink_callback *cb,
3034 struct nexthop *nh, void *data),
3037 struct rb_node *node;
3042 for (node = rb_first(root); node; node = rb_next(node)) {
3048 nh = rb_entry(node, struct nexthop, rb_node);
3050 err = nh_cb(skb, cb, nh, data);
3061 static int rtm_dump_nexthop_cb(struct sk_buff *skb, struct netlink_callback *cb,
3062 struct nexthop *nh, void *data)
3064 struct nhmsg *nhm = nlmsg_data(cb->nlh);
3065 struct nh_dump_filter *filter = data;
3067 if (nh_dump_filtered(nh, filter, nhm->nh_family))
3070 return nh_fill_node(skb, nh, RTM_NEWNEXTHOP,
3071 NETLINK_CB(cb->skb).portid,
3072 cb->nlh->nlmsg_seq, NLM_F_MULTI);
3076 static int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb)
3078 struct rtm_dump_nh_ctx *ctx = rtm_dump_nh_ctx(cb);
3079 struct net *net = sock_net(skb->sk);
3080 struct rb_root *root = &net->nexthop.rb_root;
3081 struct nh_dump_filter filter = {};
3084 err = nh_valid_dump_req(cb->nlh, &filter, cb);
3088 err = rtm_dump_walk_nexthops(skb, cb, root, ctx,
3089 &rtm_dump_nexthop_cb, &filter);
3091 if (likely(skb->len))
3099 cb->seq = net->nexthop.seq;
3100 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3104 static void nexthop_sync_mtu(struct net_device *dev, u32 orig_mtu)
3106 unsigned int hash = nh_dev_hashfn(dev->ifindex);
3107 struct net *net = dev_net(dev);
3108 struct hlist_head *head = &net->nexthop.devhash[hash];
3109 struct hlist_node *n;
3110 struct nh_info *nhi;
3112 hlist_for_each_entry_safe(nhi, n, head, dev_hash) {
3113 if (nhi->fib_nhc.nhc_dev == dev) {
3114 if (nhi->family == AF_INET)
3115 fib_nhc_update_mtu(&nhi->fib_nhc, dev->mtu,
3122 static int nh_netdev_event(struct notifier_block *this,
3123 unsigned long event, void *ptr)
3125 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3126 struct netdev_notifier_info_ext *info_ext;
3130 case NETDEV_UNREGISTER:
3131 nexthop_flush_dev(dev, event);
3134 if (!(dev_get_flags(dev) & (IFF_RUNNING | IFF_LOWER_UP)))
3135 nexthop_flush_dev(dev, event);
3137 case NETDEV_CHANGEMTU:
3139 nexthop_sync_mtu(dev, info_ext->ext.mtu);
3140 rt_cache_flush(dev_net(dev));
3146 static struct notifier_block nh_netdev_notifier = {
3147 .notifier_call = nh_netdev_event,
3150 static int nexthops_dump(struct net *net, struct notifier_block *nb,
3151 struct netlink_ext_ack *extack)
3153 struct rb_root *root = &net->nexthop.rb_root;
3154 struct rb_node *node;
3157 for (node = rb_first(root); node; node = rb_next(node)) {
3160 nh = rb_entry(node, struct nexthop, rb_node);
3161 err = call_nexthop_notifier(nb, net, NEXTHOP_EVENT_REPLACE, nh,
3170 int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
3171 struct netlink_ext_ack *extack)
3176 err = nexthops_dump(net, nb, extack);
3179 err = blocking_notifier_chain_register(&net->nexthop.notifier_chain,
3185 EXPORT_SYMBOL(register_nexthop_notifier);
3187 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb)
3189 return blocking_notifier_chain_unregister(&net->nexthop.notifier_chain,
3192 EXPORT_SYMBOL(unregister_nexthop_notifier);
3194 void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap)
3196 struct nexthop *nexthop;
3200 nexthop = nexthop_find_by_id(net, id);
3204 nexthop->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP);
3206 nexthop->nh_flags |= RTNH_F_OFFLOAD;
3208 nexthop->nh_flags |= RTNH_F_TRAP;
3213 EXPORT_SYMBOL(nexthop_set_hw_flags);
3215 void nexthop_bucket_set_hw_flags(struct net *net, u32 id, u16 bucket_index,
3216 bool offload, bool trap)
3218 struct nh_res_table *res_table;
3219 struct nh_res_bucket *bucket;
3220 struct nexthop *nexthop;
3221 struct nh_group *nhg;
3225 nexthop = nexthop_find_by_id(net, id);
3226 if (!nexthop || !nexthop->is_group)
3229 nhg = rcu_dereference(nexthop->nh_grp);
3230 if (!nhg->resilient)
3233 if (bucket_index >= nhg->res_table->num_nh_buckets)
3236 res_table = rcu_dereference(nhg->res_table);
3237 bucket = &res_table->nh_buckets[bucket_index];
3238 bucket->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP);
3240 bucket->nh_flags |= RTNH_F_OFFLOAD;
3242 bucket->nh_flags |= RTNH_F_TRAP;
3247 EXPORT_SYMBOL(nexthop_bucket_set_hw_flags);
3249 void nexthop_res_grp_activity_update(struct net *net, u32 id, u16 num_buckets,
3250 unsigned long *activity)
3252 struct nh_res_table *res_table;
3253 struct nexthop *nexthop;
3254 struct nh_group *nhg;
3259 nexthop = nexthop_find_by_id(net, id);
3260 if (!nexthop || !nexthop->is_group)
3263 nhg = rcu_dereference(nexthop->nh_grp);
3264 if (!nhg->resilient)
3267 /* Instead of silently ignoring some buckets, demand that the sizes
3270 res_table = rcu_dereference(nhg->res_table);
3271 if (num_buckets != res_table->num_nh_buckets)
3274 for (i = 0; i < num_buckets; i++) {
3275 if (test_bit(i, activity))
3276 nh_res_bucket_set_busy(&res_table->nh_buckets[i]);
3282 EXPORT_SYMBOL(nexthop_res_grp_activity_update);
3284 static void __net_exit nexthop_net_exit(struct net *net)
3287 flush_all_nexthops(net);
3289 kfree(net->nexthop.devhash);
3292 static int __net_init nexthop_net_init(struct net *net)
3294 size_t sz = sizeof(struct hlist_head) * NH_DEV_HASHSIZE;
3296 net->nexthop.rb_root = RB_ROOT;
3297 net->nexthop.devhash = kzalloc(sz, GFP_KERNEL);
3298 if (!net->nexthop.devhash)
3300 BLOCKING_INIT_NOTIFIER_HEAD(&net->nexthop.notifier_chain);
3305 static struct pernet_operations nexthop_net_ops = {
3306 .init = nexthop_net_init,
3307 .exit = nexthop_net_exit,
3310 static int __init nexthop_init(void)
3312 register_pernet_subsys(&nexthop_net_ops);
3314 register_netdevice_notifier(&nh_netdev_notifier);
3316 rtnl_register(PF_UNSPEC, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
3317 rtnl_register(PF_UNSPEC, RTM_DELNEXTHOP, rtm_del_nexthop, NULL, 0);
3318 rtnl_register(PF_UNSPEC, RTM_GETNEXTHOP, rtm_get_nexthop,
3319 rtm_dump_nexthop, 0);
3321 rtnl_register(PF_INET, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
3322 rtnl_register(PF_INET, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0);
3324 rtnl_register(PF_INET6, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
3325 rtnl_register(PF_INET6, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0);
3329 subsys_initcall(nexthop_init);