162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* Generic nexthop implementation 362306a36Sopenharmony_ci * 462306a36Sopenharmony_ci * Copyright (c) 2017-19 Cumulus Networks 562306a36Sopenharmony_ci * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com> 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci#include <linux/nexthop.h> 962306a36Sopenharmony_ci#include <linux/rtnetlink.h> 1062306a36Sopenharmony_ci#include <linux/slab.h> 1162306a36Sopenharmony_ci#include <linux/vmalloc.h> 1262306a36Sopenharmony_ci#include <net/arp.h> 1362306a36Sopenharmony_ci#include <net/ipv6_stubs.h> 1462306a36Sopenharmony_ci#include <net/lwtunnel.h> 1562306a36Sopenharmony_ci#include <net/ndisc.h> 1662306a36Sopenharmony_ci#include <net/nexthop.h> 1762306a36Sopenharmony_ci#include <net/route.h> 1862306a36Sopenharmony_ci#include <net/sock.h> 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci#define NH_RES_DEFAULT_IDLE_TIMER (120 * HZ) 2162306a36Sopenharmony_ci#define NH_RES_DEFAULT_UNBALANCED_TIMER 0 /* No forced rebalancing. */ 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_cistatic void remove_nexthop(struct net *net, struct nexthop *nh, 2462306a36Sopenharmony_ci struct nl_info *nlinfo); 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci#define NH_DEV_HASHBITS 8 2762306a36Sopenharmony_ci#define NH_DEV_HASHSIZE (1U << NH_DEV_HASHBITS) 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_cistatic const struct nla_policy rtm_nh_policy_new[] = { 3062306a36Sopenharmony_ci [NHA_ID] = { .type = NLA_U32 }, 3162306a36Sopenharmony_ci [NHA_GROUP] = { .type = NLA_BINARY }, 3262306a36Sopenharmony_ci [NHA_GROUP_TYPE] = { .type = NLA_U16 }, 3362306a36Sopenharmony_ci [NHA_BLACKHOLE] = { .type = NLA_FLAG }, 3462306a36Sopenharmony_ci [NHA_OIF] = { .type = NLA_U32 }, 3562306a36Sopenharmony_ci [NHA_GATEWAY] = { .type = NLA_BINARY }, 3662306a36Sopenharmony_ci [NHA_ENCAP_TYPE] = { .type = NLA_U16 }, 3762306a36Sopenharmony_ci [NHA_ENCAP] = { .type = NLA_NESTED }, 3862306a36Sopenharmony_ci [NHA_FDB] = { .type = NLA_FLAG }, 3962306a36Sopenharmony_ci [NHA_RES_GROUP] = { .type = NLA_NESTED }, 4062306a36Sopenharmony_ci}; 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_cistatic const struct nla_policy rtm_nh_policy_get[] = { 4362306a36Sopenharmony_ci [NHA_ID] = { .type = NLA_U32 }, 4462306a36Sopenharmony_ci}; 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_cistatic const struct nla_policy rtm_nh_policy_dump[] = { 4762306a36Sopenharmony_ci [NHA_OIF] = { .type = NLA_U32 }, 4862306a36Sopenharmony_ci [NHA_GROUPS] = { .type = NLA_FLAG }, 4962306a36Sopenharmony_ci [NHA_MASTER] = { .type = NLA_U32 }, 5062306a36Sopenharmony_ci [NHA_FDB] = { .type = NLA_FLAG }, 5162306a36Sopenharmony_ci}; 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_cistatic const struct nla_policy rtm_nh_res_policy_new[] = { 5462306a36Sopenharmony_ci [NHA_RES_GROUP_BUCKETS] = { .type = NLA_U16 }, 5562306a36Sopenharmony_ci [NHA_RES_GROUP_IDLE_TIMER] = { .type = NLA_U32 }, 5662306a36Sopenharmony_ci [NHA_RES_GROUP_UNBALANCED_TIMER] = { .type = NLA_U32 }, 5762306a36Sopenharmony_ci}; 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_cistatic const struct nla_policy rtm_nh_policy_dump_bucket[] = { 6062306a36Sopenharmony_ci [NHA_ID] = { .type = NLA_U32 }, 6162306a36Sopenharmony_ci [NHA_OIF] = { .type = NLA_U32 }, 6262306a36Sopenharmony_ci [NHA_MASTER] = { .type = NLA_U32 }, 6362306a36Sopenharmony_ci [NHA_RES_BUCKET] = { .type = NLA_NESTED }, 6462306a36Sopenharmony_ci}; 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_cistatic const struct nla_policy rtm_nh_res_bucket_policy_dump[] = { 6762306a36Sopenharmony_ci [NHA_RES_BUCKET_NH_ID] = { .type = NLA_U32 }, 6862306a36Sopenharmony_ci}; 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_cistatic const struct nla_policy rtm_nh_policy_get_bucket[] = { 7162306a36Sopenharmony_ci [NHA_ID] = { .type = NLA_U32 }, 7262306a36Sopenharmony_ci [NHA_RES_BUCKET] = { .type = NLA_NESTED }, 7362306a36Sopenharmony_ci}; 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_cistatic const struct nla_policy rtm_nh_res_bucket_policy_get[] = { 7662306a36Sopenharmony_ci [NHA_RES_BUCKET_INDEX] = { .type = NLA_U16 }, 7762306a36Sopenharmony_ci}; 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_cistatic bool nexthop_notifiers_is_empty(struct net *net) 8062306a36Sopenharmony_ci{ 8162306a36Sopenharmony_ci return !net->nexthop.notifier_chain.head; 8262306a36Sopenharmony_ci} 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_cistatic void 8562306a36Sopenharmony_ci__nh_notifier_single_info_init(struct nh_notifier_single_info *nh_info, 8662306a36Sopenharmony_ci const struct nh_info *nhi) 8762306a36Sopenharmony_ci{ 8862306a36Sopenharmony_ci nh_info->dev = nhi->fib_nhc.nhc_dev; 8962306a36Sopenharmony_ci nh_info->gw_family = nhi->fib_nhc.nhc_gw_family; 9062306a36Sopenharmony_ci if (nh_info->gw_family == AF_INET) 9162306a36Sopenharmony_ci nh_info->ipv4 = nhi->fib_nhc.nhc_gw.ipv4; 9262306a36Sopenharmony_ci else if (nh_info->gw_family == AF_INET6) 9362306a36Sopenharmony_ci nh_info->ipv6 = nhi->fib_nhc.nhc_gw.ipv6; 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci nh_info->is_reject = nhi->reject_nh; 9662306a36Sopenharmony_ci nh_info->is_fdb = nhi->fdb_nh; 9762306a36Sopenharmony_ci nh_info->has_encap = !!nhi->fib_nhc.nhc_lwtstate; 9862306a36Sopenharmony_ci} 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_cistatic int nh_notifier_single_info_init(struct nh_notifier_info *info, 10162306a36Sopenharmony_ci const struct nexthop *nh) 10262306a36Sopenharmony_ci{ 10362306a36Sopenharmony_ci struct nh_info *nhi = rtnl_dereference(nh->nh_info); 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ci info->type = NH_NOTIFIER_INFO_TYPE_SINGLE; 10662306a36Sopenharmony_ci info->nh = kzalloc(sizeof(*info->nh), GFP_KERNEL); 10762306a36Sopenharmony_ci if (!info->nh) 10862306a36Sopenharmony_ci return -ENOMEM; 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci __nh_notifier_single_info_init(info->nh, nhi); 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci return 0; 11362306a36Sopenharmony_ci} 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_cistatic void nh_notifier_single_info_fini(struct nh_notifier_info *info) 11662306a36Sopenharmony_ci{ 11762306a36Sopenharmony_ci kfree(info->nh); 11862306a36Sopenharmony_ci} 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_cistatic int nh_notifier_mpath_info_init(struct nh_notifier_info *info, 12162306a36Sopenharmony_ci struct nh_group *nhg) 12262306a36Sopenharmony_ci{ 12362306a36Sopenharmony_ci u16 num_nh = nhg->num_nh; 12462306a36Sopenharmony_ci int i; 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci info->type = NH_NOTIFIER_INFO_TYPE_GRP; 12762306a36Sopenharmony_ci info->nh_grp = kzalloc(struct_size(info->nh_grp, nh_entries, num_nh), 12862306a36Sopenharmony_ci GFP_KERNEL); 12962306a36Sopenharmony_ci if (!info->nh_grp) 13062306a36Sopenharmony_ci return -ENOMEM; 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci info->nh_grp->num_nh = num_nh; 13362306a36Sopenharmony_ci info->nh_grp->is_fdb = nhg->fdb_nh; 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci for (i = 0; i < num_nh; i++) { 13662306a36Sopenharmony_ci struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 13762306a36Sopenharmony_ci struct nh_info *nhi; 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci nhi = rtnl_dereference(nhge->nh->nh_info); 14062306a36Sopenharmony_ci info->nh_grp->nh_entries[i].id = nhge->nh->id; 14162306a36Sopenharmony_ci info->nh_grp->nh_entries[i].weight = nhge->weight; 14262306a36Sopenharmony_ci __nh_notifier_single_info_init(&info->nh_grp->nh_entries[i].nh, 14362306a36Sopenharmony_ci nhi); 14462306a36Sopenharmony_ci } 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci return 0; 14762306a36Sopenharmony_ci} 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_cistatic int nh_notifier_res_table_info_init(struct nh_notifier_info *info, 15062306a36Sopenharmony_ci struct nh_group *nhg) 15162306a36Sopenharmony_ci{ 15262306a36Sopenharmony_ci struct nh_res_table *res_table = rtnl_dereference(nhg->res_table); 15362306a36Sopenharmony_ci u16 num_nh_buckets = res_table->num_nh_buckets; 15462306a36Sopenharmony_ci unsigned long size; 15562306a36Sopenharmony_ci u16 i; 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci info->type = NH_NOTIFIER_INFO_TYPE_RES_TABLE; 15862306a36Sopenharmony_ci size = struct_size(info->nh_res_table, nhs, num_nh_buckets); 15962306a36Sopenharmony_ci info->nh_res_table = __vmalloc(size, GFP_KERNEL | __GFP_ZERO | 16062306a36Sopenharmony_ci __GFP_NOWARN); 16162306a36Sopenharmony_ci if (!info->nh_res_table) 16262306a36Sopenharmony_ci return -ENOMEM; 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_ci info->nh_res_table->num_nh_buckets = num_nh_buckets; 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ci for (i = 0; i < num_nh_buckets; i++) { 16762306a36Sopenharmony_ci struct nh_res_bucket *bucket = &res_table->nh_buckets[i]; 16862306a36Sopenharmony_ci struct nh_grp_entry *nhge; 16962306a36Sopenharmony_ci struct nh_info *nhi; 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci nhge = rtnl_dereference(bucket->nh_entry); 17262306a36Sopenharmony_ci nhi = rtnl_dereference(nhge->nh->nh_info); 17362306a36Sopenharmony_ci __nh_notifier_single_info_init(&info->nh_res_table->nhs[i], 17462306a36Sopenharmony_ci nhi); 17562306a36Sopenharmony_ci } 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci return 0; 17862306a36Sopenharmony_ci} 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_cistatic int nh_notifier_grp_info_init(struct nh_notifier_info *info, 18162306a36Sopenharmony_ci const struct nexthop *nh) 18262306a36Sopenharmony_ci{ 18362306a36Sopenharmony_ci struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci if (nhg->hash_threshold) 18662306a36Sopenharmony_ci return nh_notifier_mpath_info_init(info, nhg); 18762306a36Sopenharmony_ci else if (nhg->resilient) 18862306a36Sopenharmony_ci return nh_notifier_res_table_info_init(info, nhg); 18962306a36Sopenharmony_ci return -EINVAL; 19062306a36Sopenharmony_ci} 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_cistatic void nh_notifier_grp_info_fini(struct nh_notifier_info *info, 19362306a36Sopenharmony_ci const struct nexthop *nh) 19462306a36Sopenharmony_ci{ 19562306a36Sopenharmony_ci struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci if (nhg->hash_threshold) 19862306a36Sopenharmony_ci kfree(info->nh_grp); 19962306a36Sopenharmony_ci else if (nhg->resilient) 20062306a36Sopenharmony_ci vfree(info->nh_res_table); 20162306a36Sopenharmony_ci} 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_cistatic int nh_notifier_info_init(struct nh_notifier_info *info, 20462306a36Sopenharmony_ci const struct nexthop *nh) 20562306a36Sopenharmony_ci{ 20662306a36Sopenharmony_ci info->id = nh->id; 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_ci if (nh->is_group) 20962306a36Sopenharmony_ci return nh_notifier_grp_info_init(info, nh); 21062306a36Sopenharmony_ci else 21162306a36Sopenharmony_ci return nh_notifier_single_info_init(info, nh); 21262306a36Sopenharmony_ci} 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_cistatic void nh_notifier_info_fini(struct nh_notifier_info *info, 21562306a36Sopenharmony_ci const struct nexthop *nh) 21662306a36Sopenharmony_ci{ 21762306a36Sopenharmony_ci if (nh->is_group) 21862306a36Sopenharmony_ci nh_notifier_grp_info_fini(info, nh); 21962306a36Sopenharmony_ci else 22062306a36Sopenharmony_ci nh_notifier_single_info_fini(info); 22162306a36Sopenharmony_ci} 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_cistatic int call_nexthop_notifiers(struct net *net, 22462306a36Sopenharmony_ci enum nexthop_event_type event_type, 22562306a36Sopenharmony_ci struct nexthop *nh, 22662306a36Sopenharmony_ci struct netlink_ext_ack *extack) 22762306a36Sopenharmony_ci{ 22862306a36Sopenharmony_ci struct nh_notifier_info info = { 22962306a36Sopenharmony_ci .net = net, 23062306a36Sopenharmony_ci .extack = extack, 23162306a36Sopenharmony_ci }; 23262306a36Sopenharmony_ci int err; 23362306a36Sopenharmony_ci 23462306a36Sopenharmony_ci ASSERT_RTNL(); 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_ci if (nexthop_notifiers_is_empty(net)) 23762306a36Sopenharmony_ci return 0; 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ci err = nh_notifier_info_init(&info, nh); 24062306a36Sopenharmony_ci if (err) { 24162306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Failed to initialize nexthop notifier info"); 24262306a36Sopenharmony_ci return err; 24362306a36Sopenharmony_ci } 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci err = blocking_notifier_call_chain(&net->nexthop.notifier_chain, 24662306a36Sopenharmony_ci event_type, &info); 24762306a36Sopenharmony_ci nh_notifier_info_fini(&info, nh); 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ci return notifier_to_errno(err); 25062306a36Sopenharmony_ci} 25162306a36Sopenharmony_ci 25262306a36Sopenharmony_cistatic int 25362306a36Sopenharmony_cinh_notifier_res_bucket_idle_timer_get(const struct nh_notifier_info *info, 25462306a36Sopenharmony_ci bool force, unsigned int *p_idle_timer_ms) 25562306a36Sopenharmony_ci{ 25662306a36Sopenharmony_ci struct nh_res_table *res_table; 25762306a36Sopenharmony_ci struct nh_group *nhg; 25862306a36Sopenharmony_ci struct nexthop *nh; 25962306a36Sopenharmony_ci int err = 0; 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ci /* When 'force' is false, nexthop bucket replacement is performed 26262306a36Sopenharmony_ci * because the bucket was deemed to be idle. In this case, capable 26362306a36Sopenharmony_ci * listeners can choose to perform an atomic replacement: The bucket is 26462306a36Sopenharmony_ci * only replaced if it is inactive. However, if the idle timer interval 26562306a36Sopenharmony_ci * is smaller than the interval in which a listener is querying 26662306a36Sopenharmony_ci * buckets' activity from the device, then atomic replacement should 26762306a36Sopenharmony_ci * not be tried. Pass the idle timer value to listeners, so that they 26862306a36Sopenharmony_ci * could determine which type of replacement to perform. 26962306a36Sopenharmony_ci */ 27062306a36Sopenharmony_ci if (force) { 27162306a36Sopenharmony_ci *p_idle_timer_ms = 0; 27262306a36Sopenharmony_ci return 0; 27362306a36Sopenharmony_ci } 27462306a36Sopenharmony_ci 27562306a36Sopenharmony_ci rcu_read_lock(); 27662306a36Sopenharmony_ci 27762306a36Sopenharmony_ci nh = nexthop_find_by_id(info->net, info->id); 27862306a36Sopenharmony_ci if (!nh) { 27962306a36Sopenharmony_ci err = -EINVAL; 28062306a36Sopenharmony_ci goto out; 28162306a36Sopenharmony_ci } 28262306a36Sopenharmony_ci 28362306a36Sopenharmony_ci nhg = rcu_dereference(nh->nh_grp); 28462306a36Sopenharmony_ci res_table = rcu_dereference(nhg->res_table); 28562306a36Sopenharmony_ci *p_idle_timer_ms = jiffies_to_msecs(res_table->idle_timer); 28662306a36Sopenharmony_ci 28762306a36Sopenharmony_ciout: 28862306a36Sopenharmony_ci rcu_read_unlock(); 28962306a36Sopenharmony_ci 29062306a36Sopenharmony_ci return err; 29162306a36Sopenharmony_ci} 29262306a36Sopenharmony_ci 29362306a36Sopenharmony_cistatic int nh_notifier_res_bucket_info_init(struct nh_notifier_info *info, 29462306a36Sopenharmony_ci u16 bucket_index, bool force, 29562306a36Sopenharmony_ci struct nh_info *oldi, 29662306a36Sopenharmony_ci struct nh_info *newi) 29762306a36Sopenharmony_ci{ 29862306a36Sopenharmony_ci unsigned int idle_timer_ms; 29962306a36Sopenharmony_ci int err; 30062306a36Sopenharmony_ci 30162306a36Sopenharmony_ci err = nh_notifier_res_bucket_idle_timer_get(info, force, 30262306a36Sopenharmony_ci &idle_timer_ms); 30362306a36Sopenharmony_ci if (err) 30462306a36Sopenharmony_ci return err; 30562306a36Sopenharmony_ci 30662306a36Sopenharmony_ci info->type = NH_NOTIFIER_INFO_TYPE_RES_BUCKET; 30762306a36Sopenharmony_ci info->nh_res_bucket = kzalloc(sizeof(*info->nh_res_bucket), 30862306a36Sopenharmony_ci GFP_KERNEL); 30962306a36Sopenharmony_ci if (!info->nh_res_bucket) 31062306a36Sopenharmony_ci return -ENOMEM; 31162306a36Sopenharmony_ci 31262306a36Sopenharmony_ci info->nh_res_bucket->bucket_index = bucket_index; 31362306a36Sopenharmony_ci info->nh_res_bucket->idle_timer_ms = idle_timer_ms; 31462306a36Sopenharmony_ci info->nh_res_bucket->force = force; 31562306a36Sopenharmony_ci __nh_notifier_single_info_init(&info->nh_res_bucket->old_nh, oldi); 31662306a36Sopenharmony_ci __nh_notifier_single_info_init(&info->nh_res_bucket->new_nh, newi); 31762306a36Sopenharmony_ci return 0; 31862306a36Sopenharmony_ci} 31962306a36Sopenharmony_ci 32062306a36Sopenharmony_cistatic void nh_notifier_res_bucket_info_fini(struct nh_notifier_info *info) 32162306a36Sopenharmony_ci{ 32262306a36Sopenharmony_ci kfree(info->nh_res_bucket); 32362306a36Sopenharmony_ci} 32462306a36Sopenharmony_ci 32562306a36Sopenharmony_cistatic int __call_nexthop_res_bucket_notifiers(struct net *net, u32 nhg_id, 32662306a36Sopenharmony_ci u16 bucket_index, bool force, 32762306a36Sopenharmony_ci struct nh_info *oldi, 32862306a36Sopenharmony_ci struct nh_info *newi, 32962306a36Sopenharmony_ci struct netlink_ext_ack *extack) 33062306a36Sopenharmony_ci{ 33162306a36Sopenharmony_ci struct nh_notifier_info info = { 33262306a36Sopenharmony_ci .net = net, 33362306a36Sopenharmony_ci .extack = extack, 33462306a36Sopenharmony_ci .id = nhg_id, 33562306a36Sopenharmony_ci }; 33662306a36Sopenharmony_ci int err; 33762306a36Sopenharmony_ci 33862306a36Sopenharmony_ci if (nexthop_notifiers_is_empty(net)) 33962306a36Sopenharmony_ci return 0; 34062306a36Sopenharmony_ci 34162306a36Sopenharmony_ci err = nh_notifier_res_bucket_info_init(&info, bucket_index, force, 34262306a36Sopenharmony_ci oldi, newi); 34362306a36Sopenharmony_ci if (err) 34462306a36Sopenharmony_ci return err; 34562306a36Sopenharmony_ci 34662306a36Sopenharmony_ci err = blocking_notifier_call_chain(&net->nexthop.notifier_chain, 34762306a36Sopenharmony_ci NEXTHOP_EVENT_BUCKET_REPLACE, &info); 34862306a36Sopenharmony_ci nh_notifier_res_bucket_info_fini(&info); 34962306a36Sopenharmony_ci 35062306a36Sopenharmony_ci return notifier_to_errno(err); 35162306a36Sopenharmony_ci} 35262306a36Sopenharmony_ci 35362306a36Sopenharmony_ci/* There are three users of RES_TABLE, and NHs etc. referenced from there: 35462306a36Sopenharmony_ci * 35562306a36Sopenharmony_ci * 1) a collection of callbacks for NH maintenance. This operates under 35662306a36Sopenharmony_ci * RTNL, 35762306a36Sopenharmony_ci * 2) the delayed work that gradually balances the resilient table, 35862306a36Sopenharmony_ci * 3) and nexthop_select_path(), operating under RCU. 35962306a36Sopenharmony_ci * 36062306a36Sopenharmony_ci * Both the delayed work and the RTNL block are writers, and need to 36162306a36Sopenharmony_ci * maintain mutual exclusion. Since there are only two and well-known 36262306a36Sopenharmony_ci * writers for each table, the RTNL code can make sure it has exclusive 36362306a36Sopenharmony_ci * access thus: 36462306a36Sopenharmony_ci * 36562306a36Sopenharmony_ci * - Have the DW operate without locking; 36662306a36Sopenharmony_ci * - synchronously cancel the DW; 36762306a36Sopenharmony_ci * - do the writing; 36862306a36Sopenharmony_ci * - if the write was not actually a delete, call upkeep, which schedules 36962306a36Sopenharmony_ci * DW again if necessary. 37062306a36Sopenharmony_ci * 37162306a36Sopenharmony_ci * The functions that are always called from the RTNL context use 37262306a36Sopenharmony_ci * rtnl_dereference(). The functions that can also be called from the DW do 37362306a36Sopenharmony_ci * a raw dereference and rely on the above mutual exclusion scheme. 37462306a36Sopenharmony_ci */ 37562306a36Sopenharmony_ci#define nh_res_dereference(p) (rcu_dereference_raw(p)) 37662306a36Sopenharmony_ci 37762306a36Sopenharmony_cistatic int call_nexthop_res_bucket_notifiers(struct net *net, u32 nhg_id, 37862306a36Sopenharmony_ci u16 bucket_index, bool force, 37962306a36Sopenharmony_ci struct nexthop *old_nh, 38062306a36Sopenharmony_ci struct nexthop *new_nh, 38162306a36Sopenharmony_ci struct netlink_ext_ack *extack) 38262306a36Sopenharmony_ci{ 38362306a36Sopenharmony_ci struct nh_info *oldi = nh_res_dereference(old_nh->nh_info); 38462306a36Sopenharmony_ci struct nh_info *newi = nh_res_dereference(new_nh->nh_info); 38562306a36Sopenharmony_ci 38662306a36Sopenharmony_ci return __call_nexthop_res_bucket_notifiers(net, nhg_id, bucket_index, 38762306a36Sopenharmony_ci force, oldi, newi, extack); 38862306a36Sopenharmony_ci} 38962306a36Sopenharmony_ci 39062306a36Sopenharmony_cistatic int call_nexthop_res_table_notifiers(struct net *net, struct nexthop *nh, 39162306a36Sopenharmony_ci struct netlink_ext_ack *extack) 39262306a36Sopenharmony_ci{ 39362306a36Sopenharmony_ci struct nh_notifier_info info = { 39462306a36Sopenharmony_ci .net = net, 39562306a36Sopenharmony_ci .extack = extack, 39662306a36Sopenharmony_ci }; 39762306a36Sopenharmony_ci struct nh_group *nhg; 39862306a36Sopenharmony_ci int err; 39962306a36Sopenharmony_ci 40062306a36Sopenharmony_ci ASSERT_RTNL(); 40162306a36Sopenharmony_ci 40262306a36Sopenharmony_ci if (nexthop_notifiers_is_empty(net)) 40362306a36Sopenharmony_ci return 0; 40462306a36Sopenharmony_ci 40562306a36Sopenharmony_ci /* At this point, the nexthop buckets are still not populated. Only 40662306a36Sopenharmony_ci * emit a notification with the logical nexthops, so that a listener 40762306a36Sopenharmony_ci * could potentially veto it in case of unsupported configuration. 40862306a36Sopenharmony_ci */ 40962306a36Sopenharmony_ci nhg = rtnl_dereference(nh->nh_grp); 41062306a36Sopenharmony_ci err = nh_notifier_mpath_info_init(&info, nhg); 41162306a36Sopenharmony_ci if (err) { 41262306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Failed to initialize nexthop notifier info"); 41362306a36Sopenharmony_ci return err; 41462306a36Sopenharmony_ci } 41562306a36Sopenharmony_ci 41662306a36Sopenharmony_ci err = blocking_notifier_call_chain(&net->nexthop.notifier_chain, 41762306a36Sopenharmony_ci NEXTHOP_EVENT_RES_TABLE_PRE_REPLACE, 41862306a36Sopenharmony_ci &info); 41962306a36Sopenharmony_ci kfree(info.nh_grp); 42062306a36Sopenharmony_ci 42162306a36Sopenharmony_ci return notifier_to_errno(err); 42262306a36Sopenharmony_ci} 42362306a36Sopenharmony_ci 42462306a36Sopenharmony_cistatic int call_nexthop_notifier(struct notifier_block *nb, struct net *net, 42562306a36Sopenharmony_ci enum nexthop_event_type event_type, 42662306a36Sopenharmony_ci struct nexthop *nh, 42762306a36Sopenharmony_ci struct netlink_ext_ack *extack) 42862306a36Sopenharmony_ci{ 42962306a36Sopenharmony_ci struct nh_notifier_info info = { 43062306a36Sopenharmony_ci .net = net, 43162306a36Sopenharmony_ci .extack = extack, 43262306a36Sopenharmony_ci }; 43362306a36Sopenharmony_ci int err; 43462306a36Sopenharmony_ci 43562306a36Sopenharmony_ci err = nh_notifier_info_init(&info, nh); 43662306a36Sopenharmony_ci if (err) 43762306a36Sopenharmony_ci return err; 43862306a36Sopenharmony_ci 43962306a36Sopenharmony_ci err = nb->notifier_call(nb, event_type, &info); 44062306a36Sopenharmony_ci nh_notifier_info_fini(&info, nh); 44162306a36Sopenharmony_ci 44262306a36Sopenharmony_ci return notifier_to_errno(err); 44362306a36Sopenharmony_ci} 44462306a36Sopenharmony_ci 44562306a36Sopenharmony_cistatic unsigned int nh_dev_hashfn(unsigned int val) 44662306a36Sopenharmony_ci{ 44762306a36Sopenharmony_ci unsigned int mask = NH_DEV_HASHSIZE - 1; 44862306a36Sopenharmony_ci 44962306a36Sopenharmony_ci return (val ^ 45062306a36Sopenharmony_ci (val >> NH_DEV_HASHBITS) ^ 45162306a36Sopenharmony_ci (val >> (NH_DEV_HASHBITS * 2))) & mask; 45262306a36Sopenharmony_ci} 45362306a36Sopenharmony_ci 45462306a36Sopenharmony_cistatic void nexthop_devhash_add(struct net *net, struct nh_info *nhi) 45562306a36Sopenharmony_ci{ 45662306a36Sopenharmony_ci struct net_device *dev = nhi->fib_nhc.nhc_dev; 45762306a36Sopenharmony_ci struct hlist_head *head; 45862306a36Sopenharmony_ci unsigned int hash; 45962306a36Sopenharmony_ci 46062306a36Sopenharmony_ci WARN_ON(!dev); 46162306a36Sopenharmony_ci 46262306a36Sopenharmony_ci hash = nh_dev_hashfn(dev->ifindex); 46362306a36Sopenharmony_ci head = &net->nexthop.devhash[hash]; 46462306a36Sopenharmony_ci hlist_add_head(&nhi->dev_hash, head); 46562306a36Sopenharmony_ci} 46662306a36Sopenharmony_ci 46762306a36Sopenharmony_cistatic void nexthop_free_group(struct nexthop *nh) 46862306a36Sopenharmony_ci{ 46962306a36Sopenharmony_ci struct nh_group *nhg; 47062306a36Sopenharmony_ci int i; 47162306a36Sopenharmony_ci 47262306a36Sopenharmony_ci nhg = rcu_dereference_raw(nh->nh_grp); 47362306a36Sopenharmony_ci for (i = 0; i < nhg->num_nh; ++i) { 47462306a36Sopenharmony_ci struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 47562306a36Sopenharmony_ci 47662306a36Sopenharmony_ci WARN_ON(!list_empty(&nhge->nh_list)); 47762306a36Sopenharmony_ci nexthop_put(nhge->nh); 47862306a36Sopenharmony_ci } 47962306a36Sopenharmony_ci 48062306a36Sopenharmony_ci WARN_ON(nhg->spare == nhg); 48162306a36Sopenharmony_ci 48262306a36Sopenharmony_ci if (nhg->resilient) 48362306a36Sopenharmony_ci vfree(rcu_dereference_raw(nhg->res_table)); 48462306a36Sopenharmony_ci 48562306a36Sopenharmony_ci kfree(nhg->spare); 48662306a36Sopenharmony_ci kfree(nhg); 48762306a36Sopenharmony_ci} 48862306a36Sopenharmony_ci 48962306a36Sopenharmony_cistatic void nexthop_free_single(struct nexthop *nh) 49062306a36Sopenharmony_ci{ 49162306a36Sopenharmony_ci struct nh_info *nhi; 49262306a36Sopenharmony_ci 49362306a36Sopenharmony_ci nhi = rcu_dereference_raw(nh->nh_info); 49462306a36Sopenharmony_ci switch (nhi->family) { 49562306a36Sopenharmony_ci case AF_INET: 49662306a36Sopenharmony_ci fib_nh_release(nh->net, &nhi->fib_nh); 49762306a36Sopenharmony_ci break; 49862306a36Sopenharmony_ci case AF_INET6: 49962306a36Sopenharmony_ci ipv6_stub->fib6_nh_release(&nhi->fib6_nh); 50062306a36Sopenharmony_ci break; 50162306a36Sopenharmony_ci } 50262306a36Sopenharmony_ci kfree(nhi); 50362306a36Sopenharmony_ci} 50462306a36Sopenharmony_ci 50562306a36Sopenharmony_civoid nexthop_free_rcu(struct rcu_head *head) 50662306a36Sopenharmony_ci{ 50762306a36Sopenharmony_ci struct nexthop *nh = container_of(head, struct nexthop, rcu); 50862306a36Sopenharmony_ci 50962306a36Sopenharmony_ci if (nh->is_group) 51062306a36Sopenharmony_ci nexthop_free_group(nh); 51162306a36Sopenharmony_ci else 51262306a36Sopenharmony_ci nexthop_free_single(nh); 51362306a36Sopenharmony_ci 51462306a36Sopenharmony_ci kfree(nh); 51562306a36Sopenharmony_ci} 51662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(nexthop_free_rcu); 51762306a36Sopenharmony_ci 51862306a36Sopenharmony_cistatic struct nexthop *nexthop_alloc(void) 51962306a36Sopenharmony_ci{ 52062306a36Sopenharmony_ci struct nexthop *nh; 52162306a36Sopenharmony_ci 52262306a36Sopenharmony_ci nh = kzalloc(sizeof(struct nexthop), GFP_KERNEL); 52362306a36Sopenharmony_ci if (nh) { 52462306a36Sopenharmony_ci INIT_LIST_HEAD(&nh->fi_list); 52562306a36Sopenharmony_ci INIT_LIST_HEAD(&nh->f6i_list); 52662306a36Sopenharmony_ci INIT_LIST_HEAD(&nh->grp_list); 52762306a36Sopenharmony_ci INIT_LIST_HEAD(&nh->fdb_list); 52862306a36Sopenharmony_ci } 52962306a36Sopenharmony_ci return nh; 53062306a36Sopenharmony_ci} 53162306a36Sopenharmony_ci 53262306a36Sopenharmony_cistatic struct nh_group *nexthop_grp_alloc(u16 num_nh) 53362306a36Sopenharmony_ci{ 53462306a36Sopenharmony_ci struct nh_group *nhg; 53562306a36Sopenharmony_ci 53662306a36Sopenharmony_ci nhg = kzalloc(struct_size(nhg, nh_entries, num_nh), GFP_KERNEL); 53762306a36Sopenharmony_ci if (nhg) 53862306a36Sopenharmony_ci nhg->num_nh = num_nh; 53962306a36Sopenharmony_ci 54062306a36Sopenharmony_ci return nhg; 54162306a36Sopenharmony_ci} 54262306a36Sopenharmony_ci 54362306a36Sopenharmony_cistatic void nh_res_table_upkeep_dw(struct work_struct *work); 54462306a36Sopenharmony_ci 54562306a36Sopenharmony_cistatic struct nh_res_table * 54662306a36Sopenharmony_cinexthop_res_table_alloc(struct net *net, u32 nhg_id, struct nh_config *cfg) 54762306a36Sopenharmony_ci{ 54862306a36Sopenharmony_ci const u16 num_nh_buckets = cfg->nh_grp_res_num_buckets; 54962306a36Sopenharmony_ci struct nh_res_table *res_table; 55062306a36Sopenharmony_ci unsigned long size; 55162306a36Sopenharmony_ci 55262306a36Sopenharmony_ci size = struct_size(res_table, nh_buckets, num_nh_buckets); 55362306a36Sopenharmony_ci res_table = __vmalloc(size, GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN); 55462306a36Sopenharmony_ci if (!res_table) 55562306a36Sopenharmony_ci return NULL; 55662306a36Sopenharmony_ci 55762306a36Sopenharmony_ci res_table->net = net; 55862306a36Sopenharmony_ci res_table->nhg_id = nhg_id; 55962306a36Sopenharmony_ci INIT_DELAYED_WORK(&res_table->upkeep_dw, &nh_res_table_upkeep_dw); 56062306a36Sopenharmony_ci INIT_LIST_HEAD(&res_table->uw_nh_entries); 56162306a36Sopenharmony_ci res_table->idle_timer = cfg->nh_grp_res_idle_timer; 56262306a36Sopenharmony_ci res_table->unbalanced_timer = cfg->nh_grp_res_unbalanced_timer; 56362306a36Sopenharmony_ci res_table->num_nh_buckets = num_nh_buckets; 56462306a36Sopenharmony_ci return res_table; 56562306a36Sopenharmony_ci} 56662306a36Sopenharmony_ci 56762306a36Sopenharmony_cistatic void nh_base_seq_inc(struct net *net) 56862306a36Sopenharmony_ci{ 56962306a36Sopenharmony_ci while (++net->nexthop.seq == 0) 57062306a36Sopenharmony_ci ; 57162306a36Sopenharmony_ci} 57262306a36Sopenharmony_ci 57362306a36Sopenharmony_ci/* no reference taken; rcu lock or rtnl must be held */ 57462306a36Sopenharmony_cistruct nexthop *nexthop_find_by_id(struct net *net, u32 id) 57562306a36Sopenharmony_ci{ 57662306a36Sopenharmony_ci struct rb_node **pp, *parent = NULL, *next; 57762306a36Sopenharmony_ci 57862306a36Sopenharmony_ci pp = &net->nexthop.rb_root.rb_node; 57962306a36Sopenharmony_ci while (1) { 58062306a36Sopenharmony_ci struct nexthop *nh; 58162306a36Sopenharmony_ci 58262306a36Sopenharmony_ci next = rcu_dereference_raw(*pp); 58362306a36Sopenharmony_ci if (!next) 58462306a36Sopenharmony_ci break; 58562306a36Sopenharmony_ci parent = next; 58662306a36Sopenharmony_ci 58762306a36Sopenharmony_ci nh = rb_entry(parent, struct nexthop, rb_node); 58862306a36Sopenharmony_ci if (id < nh->id) 58962306a36Sopenharmony_ci pp = &next->rb_left; 59062306a36Sopenharmony_ci else if (id > nh->id) 59162306a36Sopenharmony_ci pp = &next->rb_right; 59262306a36Sopenharmony_ci else 59362306a36Sopenharmony_ci return nh; 59462306a36Sopenharmony_ci } 59562306a36Sopenharmony_ci return NULL; 59662306a36Sopenharmony_ci} 59762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(nexthop_find_by_id); 59862306a36Sopenharmony_ci 59962306a36Sopenharmony_ci/* used for auto id allocation; called with rtnl held */ 60062306a36Sopenharmony_cistatic u32 nh_find_unused_id(struct net *net) 60162306a36Sopenharmony_ci{ 60262306a36Sopenharmony_ci u32 id_start = net->nexthop.last_id_allocated; 60362306a36Sopenharmony_ci 60462306a36Sopenharmony_ci while (1) { 60562306a36Sopenharmony_ci net->nexthop.last_id_allocated++; 60662306a36Sopenharmony_ci if (net->nexthop.last_id_allocated == id_start) 60762306a36Sopenharmony_ci break; 60862306a36Sopenharmony_ci 60962306a36Sopenharmony_ci if (!nexthop_find_by_id(net, net->nexthop.last_id_allocated)) 61062306a36Sopenharmony_ci return net->nexthop.last_id_allocated; 61162306a36Sopenharmony_ci } 61262306a36Sopenharmony_ci return 0; 61362306a36Sopenharmony_ci} 61462306a36Sopenharmony_ci 61562306a36Sopenharmony_cistatic void nh_res_time_set_deadline(unsigned long next_time, 61662306a36Sopenharmony_ci unsigned long *deadline) 61762306a36Sopenharmony_ci{ 61862306a36Sopenharmony_ci if (time_before(next_time, *deadline)) 61962306a36Sopenharmony_ci *deadline = next_time; 62062306a36Sopenharmony_ci} 62162306a36Sopenharmony_ci 62262306a36Sopenharmony_cistatic clock_t nh_res_table_unbalanced_time(struct nh_res_table *res_table) 62362306a36Sopenharmony_ci{ 62462306a36Sopenharmony_ci if (list_empty(&res_table->uw_nh_entries)) 62562306a36Sopenharmony_ci return 0; 62662306a36Sopenharmony_ci return jiffies_delta_to_clock_t(jiffies - res_table->unbalanced_since); 62762306a36Sopenharmony_ci} 62862306a36Sopenharmony_ci 62962306a36Sopenharmony_cistatic int nla_put_nh_group_res(struct sk_buff *skb, struct nh_group *nhg) 63062306a36Sopenharmony_ci{ 63162306a36Sopenharmony_ci struct nh_res_table *res_table = rtnl_dereference(nhg->res_table); 63262306a36Sopenharmony_ci struct nlattr *nest; 63362306a36Sopenharmony_ci 63462306a36Sopenharmony_ci nest = nla_nest_start(skb, NHA_RES_GROUP); 63562306a36Sopenharmony_ci if (!nest) 63662306a36Sopenharmony_ci return -EMSGSIZE; 63762306a36Sopenharmony_ci 63862306a36Sopenharmony_ci if (nla_put_u16(skb, NHA_RES_GROUP_BUCKETS, 63962306a36Sopenharmony_ci res_table->num_nh_buckets) || 64062306a36Sopenharmony_ci nla_put_u32(skb, NHA_RES_GROUP_IDLE_TIMER, 64162306a36Sopenharmony_ci jiffies_to_clock_t(res_table->idle_timer)) || 64262306a36Sopenharmony_ci nla_put_u32(skb, NHA_RES_GROUP_UNBALANCED_TIMER, 64362306a36Sopenharmony_ci jiffies_to_clock_t(res_table->unbalanced_timer)) || 64462306a36Sopenharmony_ci nla_put_u64_64bit(skb, NHA_RES_GROUP_UNBALANCED_TIME, 64562306a36Sopenharmony_ci nh_res_table_unbalanced_time(res_table), 64662306a36Sopenharmony_ci NHA_RES_GROUP_PAD)) 64762306a36Sopenharmony_ci goto nla_put_failure; 64862306a36Sopenharmony_ci 64962306a36Sopenharmony_ci nla_nest_end(skb, nest); 65062306a36Sopenharmony_ci return 0; 65162306a36Sopenharmony_ci 65262306a36Sopenharmony_cinla_put_failure: 65362306a36Sopenharmony_ci nla_nest_cancel(skb, nest); 65462306a36Sopenharmony_ci return -EMSGSIZE; 65562306a36Sopenharmony_ci} 65662306a36Sopenharmony_ci 65762306a36Sopenharmony_cistatic int nla_put_nh_group(struct sk_buff *skb, struct nh_group *nhg) 65862306a36Sopenharmony_ci{ 65962306a36Sopenharmony_ci struct nexthop_grp *p; 66062306a36Sopenharmony_ci size_t len = nhg->num_nh * sizeof(*p); 66162306a36Sopenharmony_ci struct nlattr *nla; 66262306a36Sopenharmony_ci u16 group_type = 0; 66362306a36Sopenharmony_ci int i; 66462306a36Sopenharmony_ci 66562306a36Sopenharmony_ci if (nhg->hash_threshold) 66662306a36Sopenharmony_ci group_type = NEXTHOP_GRP_TYPE_MPATH; 66762306a36Sopenharmony_ci else if (nhg->resilient) 66862306a36Sopenharmony_ci group_type = NEXTHOP_GRP_TYPE_RES; 66962306a36Sopenharmony_ci 67062306a36Sopenharmony_ci if (nla_put_u16(skb, NHA_GROUP_TYPE, group_type)) 67162306a36Sopenharmony_ci goto nla_put_failure; 67262306a36Sopenharmony_ci 67362306a36Sopenharmony_ci nla = nla_reserve(skb, NHA_GROUP, len); 67462306a36Sopenharmony_ci if (!nla) 67562306a36Sopenharmony_ci goto nla_put_failure; 67662306a36Sopenharmony_ci 67762306a36Sopenharmony_ci p = nla_data(nla); 67862306a36Sopenharmony_ci for (i = 0; i < nhg->num_nh; ++i) { 67962306a36Sopenharmony_ci p->id = nhg->nh_entries[i].nh->id; 68062306a36Sopenharmony_ci p->weight = nhg->nh_entries[i].weight - 1; 68162306a36Sopenharmony_ci p += 1; 68262306a36Sopenharmony_ci } 68362306a36Sopenharmony_ci 68462306a36Sopenharmony_ci if (nhg->resilient && nla_put_nh_group_res(skb, nhg)) 68562306a36Sopenharmony_ci goto nla_put_failure; 68662306a36Sopenharmony_ci 68762306a36Sopenharmony_ci return 0; 68862306a36Sopenharmony_ci 68962306a36Sopenharmony_cinla_put_failure: 69062306a36Sopenharmony_ci return -EMSGSIZE; 69162306a36Sopenharmony_ci} 69262306a36Sopenharmony_ci 69362306a36Sopenharmony_cistatic int nh_fill_node(struct sk_buff *skb, struct nexthop *nh, 69462306a36Sopenharmony_ci int event, u32 portid, u32 seq, unsigned int nlflags) 69562306a36Sopenharmony_ci{ 69662306a36Sopenharmony_ci struct fib6_nh *fib6_nh; 69762306a36Sopenharmony_ci struct fib_nh *fib_nh; 69862306a36Sopenharmony_ci struct nlmsghdr *nlh; 69962306a36Sopenharmony_ci struct nh_info *nhi; 70062306a36Sopenharmony_ci struct nhmsg *nhm; 70162306a36Sopenharmony_ci 70262306a36Sopenharmony_ci nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nhm), nlflags); 70362306a36Sopenharmony_ci if (!nlh) 70462306a36Sopenharmony_ci return -EMSGSIZE; 70562306a36Sopenharmony_ci 70662306a36Sopenharmony_ci nhm = nlmsg_data(nlh); 70762306a36Sopenharmony_ci nhm->nh_family = AF_UNSPEC; 70862306a36Sopenharmony_ci nhm->nh_flags = nh->nh_flags; 70962306a36Sopenharmony_ci nhm->nh_protocol = nh->protocol; 71062306a36Sopenharmony_ci nhm->nh_scope = 0; 71162306a36Sopenharmony_ci nhm->resvd = 0; 71262306a36Sopenharmony_ci 71362306a36Sopenharmony_ci if (nla_put_u32(skb, NHA_ID, nh->id)) 71462306a36Sopenharmony_ci goto nla_put_failure; 71562306a36Sopenharmony_ci 71662306a36Sopenharmony_ci if (nh->is_group) { 71762306a36Sopenharmony_ci struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 71862306a36Sopenharmony_ci 71962306a36Sopenharmony_ci if (nhg->fdb_nh && nla_put_flag(skb, NHA_FDB)) 72062306a36Sopenharmony_ci goto nla_put_failure; 72162306a36Sopenharmony_ci if (nla_put_nh_group(skb, nhg)) 72262306a36Sopenharmony_ci goto nla_put_failure; 72362306a36Sopenharmony_ci goto out; 72462306a36Sopenharmony_ci } 72562306a36Sopenharmony_ci 72662306a36Sopenharmony_ci nhi = rtnl_dereference(nh->nh_info); 72762306a36Sopenharmony_ci nhm->nh_family = nhi->family; 72862306a36Sopenharmony_ci if (nhi->reject_nh) { 72962306a36Sopenharmony_ci if (nla_put_flag(skb, NHA_BLACKHOLE)) 73062306a36Sopenharmony_ci goto nla_put_failure; 73162306a36Sopenharmony_ci goto out; 73262306a36Sopenharmony_ci } else if (nhi->fdb_nh) { 73362306a36Sopenharmony_ci if (nla_put_flag(skb, NHA_FDB)) 73462306a36Sopenharmony_ci goto nla_put_failure; 73562306a36Sopenharmony_ci } else { 73662306a36Sopenharmony_ci const struct net_device *dev; 73762306a36Sopenharmony_ci 73862306a36Sopenharmony_ci dev = nhi->fib_nhc.nhc_dev; 73962306a36Sopenharmony_ci if (dev && nla_put_u32(skb, NHA_OIF, dev->ifindex)) 74062306a36Sopenharmony_ci goto nla_put_failure; 74162306a36Sopenharmony_ci } 74262306a36Sopenharmony_ci 74362306a36Sopenharmony_ci nhm->nh_scope = nhi->fib_nhc.nhc_scope; 74462306a36Sopenharmony_ci switch (nhi->family) { 74562306a36Sopenharmony_ci case AF_INET: 74662306a36Sopenharmony_ci fib_nh = &nhi->fib_nh; 74762306a36Sopenharmony_ci if (fib_nh->fib_nh_gw_family && 74862306a36Sopenharmony_ci nla_put_be32(skb, NHA_GATEWAY, fib_nh->fib_nh_gw4)) 74962306a36Sopenharmony_ci goto nla_put_failure; 75062306a36Sopenharmony_ci break; 75162306a36Sopenharmony_ci 75262306a36Sopenharmony_ci case AF_INET6: 75362306a36Sopenharmony_ci fib6_nh = &nhi->fib6_nh; 75462306a36Sopenharmony_ci if (fib6_nh->fib_nh_gw_family && 75562306a36Sopenharmony_ci nla_put_in6_addr(skb, NHA_GATEWAY, &fib6_nh->fib_nh_gw6)) 75662306a36Sopenharmony_ci goto nla_put_failure; 75762306a36Sopenharmony_ci break; 75862306a36Sopenharmony_ci } 75962306a36Sopenharmony_ci 76062306a36Sopenharmony_ci if (nhi->fib_nhc.nhc_lwtstate && 76162306a36Sopenharmony_ci lwtunnel_fill_encap(skb, nhi->fib_nhc.nhc_lwtstate, 76262306a36Sopenharmony_ci NHA_ENCAP, NHA_ENCAP_TYPE) < 0) 76362306a36Sopenharmony_ci goto nla_put_failure; 76462306a36Sopenharmony_ci 76562306a36Sopenharmony_ciout: 76662306a36Sopenharmony_ci nlmsg_end(skb, nlh); 76762306a36Sopenharmony_ci return 0; 76862306a36Sopenharmony_ci 76962306a36Sopenharmony_cinla_put_failure: 77062306a36Sopenharmony_ci nlmsg_cancel(skb, nlh); 77162306a36Sopenharmony_ci return -EMSGSIZE; 77262306a36Sopenharmony_ci} 77362306a36Sopenharmony_ci 77462306a36Sopenharmony_cistatic size_t nh_nlmsg_size_grp_res(struct nh_group *nhg) 77562306a36Sopenharmony_ci{ 77662306a36Sopenharmony_ci return nla_total_size(0) + /* NHA_RES_GROUP */ 77762306a36Sopenharmony_ci nla_total_size(2) + /* NHA_RES_GROUP_BUCKETS */ 77862306a36Sopenharmony_ci nla_total_size(4) + /* NHA_RES_GROUP_IDLE_TIMER */ 77962306a36Sopenharmony_ci nla_total_size(4) + /* NHA_RES_GROUP_UNBALANCED_TIMER */ 78062306a36Sopenharmony_ci nla_total_size_64bit(8);/* NHA_RES_GROUP_UNBALANCED_TIME */ 78162306a36Sopenharmony_ci} 78262306a36Sopenharmony_ci 78362306a36Sopenharmony_cistatic size_t nh_nlmsg_size_grp(struct nexthop *nh) 78462306a36Sopenharmony_ci{ 78562306a36Sopenharmony_ci struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 78662306a36Sopenharmony_ci size_t sz = sizeof(struct nexthop_grp) * nhg->num_nh; 78762306a36Sopenharmony_ci size_t tot = nla_total_size(sz) + 78862306a36Sopenharmony_ci nla_total_size(2); /* NHA_GROUP_TYPE */ 78962306a36Sopenharmony_ci 79062306a36Sopenharmony_ci if (nhg->resilient) 79162306a36Sopenharmony_ci tot += nh_nlmsg_size_grp_res(nhg); 79262306a36Sopenharmony_ci 79362306a36Sopenharmony_ci return tot; 79462306a36Sopenharmony_ci} 79562306a36Sopenharmony_ci 79662306a36Sopenharmony_cistatic size_t nh_nlmsg_size_single(struct nexthop *nh) 79762306a36Sopenharmony_ci{ 79862306a36Sopenharmony_ci struct nh_info *nhi = rtnl_dereference(nh->nh_info); 79962306a36Sopenharmony_ci size_t sz; 80062306a36Sopenharmony_ci 80162306a36Sopenharmony_ci /* covers NHA_BLACKHOLE since NHA_OIF and BLACKHOLE 80262306a36Sopenharmony_ci * are mutually exclusive 80362306a36Sopenharmony_ci */ 80462306a36Sopenharmony_ci sz = nla_total_size(4); /* NHA_OIF */ 80562306a36Sopenharmony_ci 80662306a36Sopenharmony_ci switch (nhi->family) { 80762306a36Sopenharmony_ci case AF_INET: 80862306a36Sopenharmony_ci if (nhi->fib_nh.fib_nh_gw_family) 80962306a36Sopenharmony_ci sz += nla_total_size(4); /* NHA_GATEWAY */ 81062306a36Sopenharmony_ci break; 81162306a36Sopenharmony_ci 81262306a36Sopenharmony_ci case AF_INET6: 81362306a36Sopenharmony_ci /* NHA_GATEWAY */ 81462306a36Sopenharmony_ci if (nhi->fib6_nh.fib_nh_gw_family) 81562306a36Sopenharmony_ci sz += nla_total_size(sizeof(const struct in6_addr)); 81662306a36Sopenharmony_ci break; 81762306a36Sopenharmony_ci } 81862306a36Sopenharmony_ci 81962306a36Sopenharmony_ci if (nhi->fib_nhc.nhc_lwtstate) { 82062306a36Sopenharmony_ci sz += lwtunnel_get_encap_size(nhi->fib_nhc.nhc_lwtstate); 82162306a36Sopenharmony_ci sz += nla_total_size(2); /* NHA_ENCAP_TYPE */ 82262306a36Sopenharmony_ci } 82362306a36Sopenharmony_ci 82462306a36Sopenharmony_ci return sz; 82562306a36Sopenharmony_ci} 82662306a36Sopenharmony_ci 82762306a36Sopenharmony_cistatic size_t nh_nlmsg_size(struct nexthop *nh) 82862306a36Sopenharmony_ci{ 82962306a36Sopenharmony_ci size_t sz = NLMSG_ALIGN(sizeof(struct nhmsg)); 83062306a36Sopenharmony_ci 83162306a36Sopenharmony_ci sz += nla_total_size(4); /* NHA_ID */ 83262306a36Sopenharmony_ci 83362306a36Sopenharmony_ci if (nh->is_group) 83462306a36Sopenharmony_ci sz += nh_nlmsg_size_grp(nh); 83562306a36Sopenharmony_ci else 83662306a36Sopenharmony_ci sz += nh_nlmsg_size_single(nh); 83762306a36Sopenharmony_ci 83862306a36Sopenharmony_ci return sz; 83962306a36Sopenharmony_ci} 84062306a36Sopenharmony_ci 84162306a36Sopenharmony_cistatic void nexthop_notify(int event, struct nexthop *nh, struct nl_info *info) 84262306a36Sopenharmony_ci{ 84362306a36Sopenharmony_ci unsigned int nlflags = info->nlh ? info->nlh->nlmsg_flags : 0; 84462306a36Sopenharmony_ci u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 84562306a36Sopenharmony_ci struct sk_buff *skb; 84662306a36Sopenharmony_ci int err = -ENOBUFS; 84762306a36Sopenharmony_ci 84862306a36Sopenharmony_ci skb = nlmsg_new(nh_nlmsg_size(nh), gfp_any()); 84962306a36Sopenharmony_ci if (!skb) 85062306a36Sopenharmony_ci goto errout; 85162306a36Sopenharmony_ci 85262306a36Sopenharmony_ci err = nh_fill_node(skb, nh, event, info->portid, seq, nlflags); 85362306a36Sopenharmony_ci if (err < 0) { 85462306a36Sopenharmony_ci /* -EMSGSIZE implies BUG in nh_nlmsg_size() */ 85562306a36Sopenharmony_ci WARN_ON(err == -EMSGSIZE); 85662306a36Sopenharmony_ci kfree_skb(skb); 85762306a36Sopenharmony_ci goto errout; 85862306a36Sopenharmony_ci } 85962306a36Sopenharmony_ci 86062306a36Sopenharmony_ci rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_NEXTHOP, 86162306a36Sopenharmony_ci info->nlh, gfp_any()); 86262306a36Sopenharmony_ci return; 86362306a36Sopenharmony_cierrout: 86462306a36Sopenharmony_ci if (err < 0) 86562306a36Sopenharmony_ci rtnl_set_sk_err(info->nl_net, RTNLGRP_NEXTHOP, err); 86662306a36Sopenharmony_ci} 86762306a36Sopenharmony_ci 86862306a36Sopenharmony_cistatic unsigned long nh_res_bucket_used_time(const struct nh_res_bucket *bucket) 86962306a36Sopenharmony_ci{ 87062306a36Sopenharmony_ci return (unsigned long)atomic_long_read(&bucket->used_time); 87162306a36Sopenharmony_ci} 87262306a36Sopenharmony_ci 87362306a36Sopenharmony_cistatic unsigned long 87462306a36Sopenharmony_cinh_res_bucket_idle_point(const struct nh_res_table *res_table, 87562306a36Sopenharmony_ci const struct nh_res_bucket *bucket, 87662306a36Sopenharmony_ci unsigned long now) 87762306a36Sopenharmony_ci{ 87862306a36Sopenharmony_ci unsigned long time = nh_res_bucket_used_time(bucket); 87962306a36Sopenharmony_ci 88062306a36Sopenharmony_ci /* Bucket was not used since it was migrated. The idle time is now. */ 88162306a36Sopenharmony_ci if (time == bucket->migrated_time) 88262306a36Sopenharmony_ci return now; 88362306a36Sopenharmony_ci 88462306a36Sopenharmony_ci return time + res_table->idle_timer; 88562306a36Sopenharmony_ci} 88662306a36Sopenharmony_ci 88762306a36Sopenharmony_cistatic unsigned long 88862306a36Sopenharmony_cinh_res_table_unb_point(const struct nh_res_table *res_table) 88962306a36Sopenharmony_ci{ 89062306a36Sopenharmony_ci return res_table->unbalanced_since + res_table->unbalanced_timer; 89162306a36Sopenharmony_ci} 89262306a36Sopenharmony_ci 89362306a36Sopenharmony_cistatic void nh_res_bucket_set_idle(const struct nh_res_table *res_table, 89462306a36Sopenharmony_ci struct nh_res_bucket *bucket) 89562306a36Sopenharmony_ci{ 89662306a36Sopenharmony_ci unsigned long now = jiffies; 89762306a36Sopenharmony_ci 89862306a36Sopenharmony_ci atomic_long_set(&bucket->used_time, (long)now); 89962306a36Sopenharmony_ci bucket->migrated_time = now; 90062306a36Sopenharmony_ci} 90162306a36Sopenharmony_ci 90262306a36Sopenharmony_cistatic void nh_res_bucket_set_busy(struct nh_res_bucket *bucket) 90362306a36Sopenharmony_ci{ 90462306a36Sopenharmony_ci atomic_long_set(&bucket->used_time, (long)jiffies); 90562306a36Sopenharmony_ci} 90662306a36Sopenharmony_ci 90762306a36Sopenharmony_cistatic clock_t nh_res_bucket_idle_time(const struct nh_res_bucket *bucket) 90862306a36Sopenharmony_ci{ 90962306a36Sopenharmony_ci unsigned long used_time = nh_res_bucket_used_time(bucket); 91062306a36Sopenharmony_ci 91162306a36Sopenharmony_ci return jiffies_delta_to_clock_t(jiffies - used_time); 91262306a36Sopenharmony_ci} 91362306a36Sopenharmony_ci 91462306a36Sopenharmony_cistatic int nh_fill_res_bucket(struct sk_buff *skb, struct nexthop *nh, 91562306a36Sopenharmony_ci struct nh_res_bucket *bucket, u16 bucket_index, 91662306a36Sopenharmony_ci int event, u32 portid, u32 seq, 91762306a36Sopenharmony_ci unsigned int nlflags, 91862306a36Sopenharmony_ci struct netlink_ext_ack *extack) 91962306a36Sopenharmony_ci{ 92062306a36Sopenharmony_ci struct nh_grp_entry *nhge = nh_res_dereference(bucket->nh_entry); 92162306a36Sopenharmony_ci struct nlmsghdr *nlh; 92262306a36Sopenharmony_ci struct nlattr *nest; 92362306a36Sopenharmony_ci struct nhmsg *nhm; 92462306a36Sopenharmony_ci 92562306a36Sopenharmony_ci nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nhm), nlflags); 92662306a36Sopenharmony_ci if (!nlh) 92762306a36Sopenharmony_ci return -EMSGSIZE; 92862306a36Sopenharmony_ci 92962306a36Sopenharmony_ci nhm = nlmsg_data(nlh); 93062306a36Sopenharmony_ci nhm->nh_family = AF_UNSPEC; 93162306a36Sopenharmony_ci nhm->nh_flags = bucket->nh_flags; 93262306a36Sopenharmony_ci nhm->nh_protocol = nh->protocol; 93362306a36Sopenharmony_ci nhm->nh_scope = 0; 93462306a36Sopenharmony_ci nhm->resvd = 0; 93562306a36Sopenharmony_ci 93662306a36Sopenharmony_ci if (nla_put_u32(skb, NHA_ID, nh->id)) 93762306a36Sopenharmony_ci goto nla_put_failure; 93862306a36Sopenharmony_ci 93962306a36Sopenharmony_ci nest = nla_nest_start(skb, NHA_RES_BUCKET); 94062306a36Sopenharmony_ci if (!nest) 94162306a36Sopenharmony_ci goto nla_put_failure; 94262306a36Sopenharmony_ci 94362306a36Sopenharmony_ci if (nla_put_u16(skb, NHA_RES_BUCKET_INDEX, bucket_index) || 94462306a36Sopenharmony_ci nla_put_u32(skb, NHA_RES_BUCKET_NH_ID, nhge->nh->id) || 94562306a36Sopenharmony_ci nla_put_u64_64bit(skb, NHA_RES_BUCKET_IDLE_TIME, 94662306a36Sopenharmony_ci nh_res_bucket_idle_time(bucket), 94762306a36Sopenharmony_ci NHA_RES_BUCKET_PAD)) 94862306a36Sopenharmony_ci goto nla_put_failure_nest; 94962306a36Sopenharmony_ci 95062306a36Sopenharmony_ci nla_nest_end(skb, nest); 95162306a36Sopenharmony_ci nlmsg_end(skb, nlh); 95262306a36Sopenharmony_ci return 0; 95362306a36Sopenharmony_ci 95462306a36Sopenharmony_cinla_put_failure_nest: 95562306a36Sopenharmony_ci nla_nest_cancel(skb, nest); 95662306a36Sopenharmony_cinla_put_failure: 95762306a36Sopenharmony_ci nlmsg_cancel(skb, nlh); 95862306a36Sopenharmony_ci return -EMSGSIZE; 95962306a36Sopenharmony_ci} 96062306a36Sopenharmony_ci 96162306a36Sopenharmony_cistatic void nexthop_bucket_notify(struct nh_res_table *res_table, 96262306a36Sopenharmony_ci u16 bucket_index) 96362306a36Sopenharmony_ci{ 96462306a36Sopenharmony_ci struct nh_res_bucket *bucket = &res_table->nh_buckets[bucket_index]; 96562306a36Sopenharmony_ci struct nh_grp_entry *nhge = nh_res_dereference(bucket->nh_entry); 96662306a36Sopenharmony_ci struct nexthop *nh = nhge->nh_parent; 96762306a36Sopenharmony_ci struct sk_buff *skb; 96862306a36Sopenharmony_ci int err = -ENOBUFS; 96962306a36Sopenharmony_ci 97062306a36Sopenharmony_ci skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 97162306a36Sopenharmony_ci if (!skb) 97262306a36Sopenharmony_ci goto errout; 97362306a36Sopenharmony_ci 97462306a36Sopenharmony_ci err = nh_fill_res_bucket(skb, nh, bucket, bucket_index, 97562306a36Sopenharmony_ci RTM_NEWNEXTHOPBUCKET, 0, 0, NLM_F_REPLACE, 97662306a36Sopenharmony_ci NULL); 97762306a36Sopenharmony_ci if (err < 0) { 97862306a36Sopenharmony_ci kfree_skb(skb); 97962306a36Sopenharmony_ci goto errout; 98062306a36Sopenharmony_ci } 98162306a36Sopenharmony_ci 98262306a36Sopenharmony_ci rtnl_notify(skb, nh->net, 0, RTNLGRP_NEXTHOP, NULL, GFP_KERNEL); 98362306a36Sopenharmony_ci return; 98462306a36Sopenharmony_cierrout: 98562306a36Sopenharmony_ci if (err < 0) 98662306a36Sopenharmony_ci rtnl_set_sk_err(nh->net, RTNLGRP_NEXTHOP, err); 98762306a36Sopenharmony_ci} 98862306a36Sopenharmony_ci 98962306a36Sopenharmony_cistatic bool valid_group_nh(struct nexthop *nh, unsigned int npaths, 99062306a36Sopenharmony_ci bool *is_fdb, struct netlink_ext_ack *extack) 99162306a36Sopenharmony_ci{ 99262306a36Sopenharmony_ci if (nh->is_group) { 99362306a36Sopenharmony_ci struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 99462306a36Sopenharmony_ci 99562306a36Sopenharmony_ci /* Nesting groups within groups is not supported. */ 99662306a36Sopenharmony_ci if (nhg->hash_threshold) { 99762306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, 99862306a36Sopenharmony_ci "Hash-threshold group can not be a nexthop within a group"); 99962306a36Sopenharmony_ci return false; 100062306a36Sopenharmony_ci } 100162306a36Sopenharmony_ci if (nhg->resilient) { 100262306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, 100362306a36Sopenharmony_ci "Resilient group can not be a nexthop within a group"); 100462306a36Sopenharmony_ci return false; 100562306a36Sopenharmony_ci } 100662306a36Sopenharmony_ci *is_fdb = nhg->fdb_nh; 100762306a36Sopenharmony_ci } else { 100862306a36Sopenharmony_ci struct nh_info *nhi = rtnl_dereference(nh->nh_info); 100962306a36Sopenharmony_ci 101062306a36Sopenharmony_ci if (nhi->reject_nh && npaths > 1) { 101162306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, 101262306a36Sopenharmony_ci "Blackhole nexthop can not be used in a group with more than 1 path"); 101362306a36Sopenharmony_ci return false; 101462306a36Sopenharmony_ci } 101562306a36Sopenharmony_ci *is_fdb = nhi->fdb_nh; 101662306a36Sopenharmony_ci } 101762306a36Sopenharmony_ci 101862306a36Sopenharmony_ci return true; 101962306a36Sopenharmony_ci} 102062306a36Sopenharmony_ci 102162306a36Sopenharmony_cistatic int nh_check_attr_fdb_group(struct nexthop *nh, u8 *nh_family, 102262306a36Sopenharmony_ci struct netlink_ext_ack *extack) 102362306a36Sopenharmony_ci{ 102462306a36Sopenharmony_ci struct nh_info *nhi; 102562306a36Sopenharmony_ci 102662306a36Sopenharmony_ci nhi = rtnl_dereference(nh->nh_info); 102762306a36Sopenharmony_ci 102862306a36Sopenharmony_ci if (!nhi->fdb_nh) { 102962306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "FDB nexthop group can only have fdb nexthops"); 103062306a36Sopenharmony_ci return -EINVAL; 103162306a36Sopenharmony_ci } 103262306a36Sopenharmony_ci 103362306a36Sopenharmony_ci if (*nh_family == AF_UNSPEC) { 103462306a36Sopenharmony_ci *nh_family = nhi->family; 103562306a36Sopenharmony_ci } else if (*nh_family != nhi->family) { 103662306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "FDB nexthop group cannot have mixed family nexthops"); 103762306a36Sopenharmony_ci return -EINVAL; 103862306a36Sopenharmony_ci } 103962306a36Sopenharmony_ci 104062306a36Sopenharmony_ci return 0; 104162306a36Sopenharmony_ci} 104262306a36Sopenharmony_ci 104362306a36Sopenharmony_cistatic int nh_check_attr_group(struct net *net, 104462306a36Sopenharmony_ci struct nlattr *tb[], size_t tb_size, 104562306a36Sopenharmony_ci u16 nh_grp_type, struct netlink_ext_ack *extack) 104662306a36Sopenharmony_ci{ 104762306a36Sopenharmony_ci unsigned int len = nla_len(tb[NHA_GROUP]); 104862306a36Sopenharmony_ci u8 nh_family = AF_UNSPEC; 104962306a36Sopenharmony_ci struct nexthop_grp *nhg; 105062306a36Sopenharmony_ci unsigned int i, j; 105162306a36Sopenharmony_ci u8 nhg_fdb = 0; 105262306a36Sopenharmony_ci 105362306a36Sopenharmony_ci if (!len || len & (sizeof(struct nexthop_grp) - 1)) { 105462306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, 105562306a36Sopenharmony_ci "Invalid length for nexthop group attribute"); 105662306a36Sopenharmony_ci return -EINVAL; 105762306a36Sopenharmony_ci } 105862306a36Sopenharmony_ci 105962306a36Sopenharmony_ci /* convert len to number of nexthop ids */ 106062306a36Sopenharmony_ci len /= sizeof(*nhg); 106162306a36Sopenharmony_ci 106262306a36Sopenharmony_ci nhg = nla_data(tb[NHA_GROUP]); 106362306a36Sopenharmony_ci for (i = 0; i < len; ++i) { 106462306a36Sopenharmony_ci if (nhg[i].resvd1 || nhg[i].resvd2) { 106562306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Reserved fields in nexthop_grp must be 0"); 106662306a36Sopenharmony_ci return -EINVAL; 106762306a36Sopenharmony_ci } 106862306a36Sopenharmony_ci if (nhg[i].weight > 254) { 106962306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Invalid value for weight"); 107062306a36Sopenharmony_ci return -EINVAL; 107162306a36Sopenharmony_ci } 107262306a36Sopenharmony_ci for (j = i + 1; j < len; ++j) { 107362306a36Sopenharmony_ci if (nhg[i].id == nhg[j].id) { 107462306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Nexthop id can not be used twice in a group"); 107562306a36Sopenharmony_ci return -EINVAL; 107662306a36Sopenharmony_ci } 107762306a36Sopenharmony_ci } 107862306a36Sopenharmony_ci } 107962306a36Sopenharmony_ci 108062306a36Sopenharmony_ci if (tb[NHA_FDB]) 108162306a36Sopenharmony_ci nhg_fdb = 1; 108262306a36Sopenharmony_ci nhg = nla_data(tb[NHA_GROUP]); 108362306a36Sopenharmony_ci for (i = 0; i < len; ++i) { 108462306a36Sopenharmony_ci struct nexthop *nh; 108562306a36Sopenharmony_ci bool is_fdb_nh; 108662306a36Sopenharmony_ci 108762306a36Sopenharmony_ci nh = nexthop_find_by_id(net, nhg[i].id); 108862306a36Sopenharmony_ci if (!nh) { 108962306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Invalid nexthop id"); 109062306a36Sopenharmony_ci return -EINVAL; 109162306a36Sopenharmony_ci } 109262306a36Sopenharmony_ci if (!valid_group_nh(nh, len, &is_fdb_nh, extack)) 109362306a36Sopenharmony_ci return -EINVAL; 109462306a36Sopenharmony_ci 109562306a36Sopenharmony_ci if (nhg_fdb && nh_check_attr_fdb_group(nh, &nh_family, extack)) 109662306a36Sopenharmony_ci return -EINVAL; 109762306a36Sopenharmony_ci 109862306a36Sopenharmony_ci if (!nhg_fdb && is_fdb_nh) { 109962306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Non FDB nexthop group cannot have fdb nexthops"); 110062306a36Sopenharmony_ci return -EINVAL; 110162306a36Sopenharmony_ci } 110262306a36Sopenharmony_ci } 110362306a36Sopenharmony_ci for (i = NHA_GROUP_TYPE + 1; i < tb_size; ++i) { 110462306a36Sopenharmony_ci if (!tb[i]) 110562306a36Sopenharmony_ci continue; 110662306a36Sopenharmony_ci switch (i) { 110762306a36Sopenharmony_ci case NHA_FDB: 110862306a36Sopenharmony_ci continue; 110962306a36Sopenharmony_ci case NHA_RES_GROUP: 111062306a36Sopenharmony_ci if (nh_grp_type == NEXTHOP_GRP_TYPE_RES) 111162306a36Sopenharmony_ci continue; 111262306a36Sopenharmony_ci break; 111362306a36Sopenharmony_ci } 111462306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, 111562306a36Sopenharmony_ci "No other attributes can be set in nexthop groups"); 111662306a36Sopenharmony_ci return -EINVAL; 111762306a36Sopenharmony_ci } 111862306a36Sopenharmony_ci 111962306a36Sopenharmony_ci return 0; 112062306a36Sopenharmony_ci} 112162306a36Sopenharmony_ci 112262306a36Sopenharmony_cistatic bool ipv6_good_nh(const struct fib6_nh *nh) 112362306a36Sopenharmony_ci{ 112462306a36Sopenharmony_ci int state = NUD_REACHABLE; 112562306a36Sopenharmony_ci struct neighbour *n; 112662306a36Sopenharmony_ci 112762306a36Sopenharmony_ci rcu_read_lock(); 112862306a36Sopenharmony_ci 112962306a36Sopenharmony_ci n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev, &nh->fib_nh_gw6); 113062306a36Sopenharmony_ci if (n) 113162306a36Sopenharmony_ci state = READ_ONCE(n->nud_state); 113262306a36Sopenharmony_ci 113362306a36Sopenharmony_ci rcu_read_unlock(); 113462306a36Sopenharmony_ci 113562306a36Sopenharmony_ci return !!(state & NUD_VALID); 113662306a36Sopenharmony_ci} 113762306a36Sopenharmony_ci 113862306a36Sopenharmony_cistatic bool ipv4_good_nh(const struct fib_nh *nh) 113962306a36Sopenharmony_ci{ 114062306a36Sopenharmony_ci int state = NUD_REACHABLE; 114162306a36Sopenharmony_ci struct neighbour *n; 114262306a36Sopenharmony_ci 114362306a36Sopenharmony_ci rcu_read_lock(); 114462306a36Sopenharmony_ci 114562306a36Sopenharmony_ci n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev, 114662306a36Sopenharmony_ci (__force u32)nh->fib_nh_gw4); 114762306a36Sopenharmony_ci if (n) 114862306a36Sopenharmony_ci state = READ_ONCE(n->nud_state); 114962306a36Sopenharmony_ci 115062306a36Sopenharmony_ci rcu_read_unlock(); 115162306a36Sopenharmony_ci 115262306a36Sopenharmony_ci return !!(state & NUD_VALID); 115362306a36Sopenharmony_ci} 115462306a36Sopenharmony_ci 115562306a36Sopenharmony_cistatic bool nexthop_is_good_nh(const struct nexthop *nh) 115662306a36Sopenharmony_ci{ 115762306a36Sopenharmony_ci struct nh_info *nhi = rcu_dereference(nh->nh_info); 115862306a36Sopenharmony_ci 115962306a36Sopenharmony_ci switch (nhi->family) { 116062306a36Sopenharmony_ci case AF_INET: 116162306a36Sopenharmony_ci return ipv4_good_nh(&nhi->fib_nh); 116262306a36Sopenharmony_ci case AF_INET6: 116362306a36Sopenharmony_ci return ipv6_good_nh(&nhi->fib6_nh); 116462306a36Sopenharmony_ci } 116562306a36Sopenharmony_ci 116662306a36Sopenharmony_ci return false; 116762306a36Sopenharmony_ci} 116862306a36Sopenharmony_ci 116962306a36Sopenharmony_cistatic struct nexthop *nexthop_select_path_fdb(struct nh_group *nhg, int hash) 117062306a36Sopenharmony_ci{ 117162306a36Sopenharmony_ci int i; 117262306a36Sopenharmony_ci 117362306a36Sopenharmony_ci for (i = 0; i < nhg->num_nh; i++) { 117462306a36Sopenharmony_ci struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 117562306a36Sopenharmony_ci 117662306a36Sopenharmony_ci if (hash > atomic_read(&nhge->hthr.upper_bound)) 117762306a36Sopenharmony_ci continue; 117862306a36Sopenharmony_ci 117962306a36Sopenharmony_ci return nhge->nh; 118062306a36Sopenharmony_ci } 118162306a36Sopenharmony_ci 118262306a36Sopenharmony_ci WARN_ON_ONCE(1); 118362306a36Sopenharmony_ci return NULL; 118462306a36Sopenharmony_ci} 118562306a36Sopenharmony_ci 118662306a36Sopenharmony_cistatic struct nexthop *nexthop_select_path_hthr(struct nh_group *nhg, int hash) 118762306a36Sopenharmony_ci{ 118862306a36Sopenharmony_ci struct nexthop *rc = NULL; 118962306a36Sopenharmony_ci int i; 119062306a36Sopenharmony_ci 119162306a36Sopenharmony_ci if (nhg->fdb_nh) 119262306a36Sopenharmony_ci return nexthop_select_path_fdb(nhg, hash); 119362306a36Sopenharmony_ci 119462306a36Sopenharmony_ci for (i = 0; i < nhg->num_nh; ++i) { 119562306a36Sopenharmony_ci struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 119662306a36Sopenharmony_ci 119762306a36Sopenharmony_ci /* nexthops always check if it is good and does 119862306a36Sopenharmony_ci * not rely on a sysctl for this behavior 119962306a36Sopenharmony_ci */ 120062306a36Sopenharmony_ci if (!nexthop_is_good_nh(nhge->nh)) 120162306a36Sopenharmony_ci continue; 120262306a36Sopenharmony_ci 120362306a36Sopenharmony_ci if (!rc) 120462306a36Sopenharmony_ci rc = nhge->nh; 120562306a36Sopenharmony_ci 120662306a36Sopenharmony_ci if (hash > atomic_read(&nhge->hthr.upper_bound)) 120762306a36Sopenharmony_ci continue; 120862306a36Sopenharmony_ci 120962306a36Sopenharmony_ci return nhge->nh; 121062306a36Sopenharmony_ci } 121162306a36Sopenharmony_ci 121262306a36Sopenharmony_ci return rc ? : nhg->nh_entries[0].nh; 121362306a36Sopenharmony_ci} 121462306a36Sopenharmony_ci 121562306a36Sopenharmony_cistatic struct nexthop *nexthop_select_path_res(struct nh_group *nhg, int hash) 121662306a36Sopenharmony_ci{ 121762306a36Sopenharmony_ci struct nh_res_table *res_table = rcu_dereference(nhg->res_table); 121862306a36Sopenharmony_ci u16 bucket_index = hash % res_table->num_nh_buckets; 121962306a36Sopenharmony_ci struct nh_res_bucket *bucket; 122062306a36Sopenharmony_ci struct nh_grp_entry *nhge; 122162306a36Sopenharmony_ci 122262306a36Sopenharmony_ci /* nexthop_select_path() is expected to return a non-NULL value, so 122362306a36Sopenharmony_ci * skip protocol validation and just hand out whatever there is. 122462306a36Sopenharmony_ci */ 122562306a36Sopenharmony_ci bucket = &res_table->nh_buckets[bucket_index]; 122662306a36Sopenharmony_ci nh_res_bucket_set_busy(bucket); 122762306a36Sopenharmony_ci nhge = rcu_dereference(bucket->nh_entry); 122862306a36Sopenharmony_ci return nhge->nh; 122962306a36Sopenharmony_ci} 123062306a36Sopenharmony_ci 123162306a36Sopenharmony_cistruct nexthop *nexthop_select_path(struct nexthop *nh, int hash) 123262306a36Sopenharmony_ci{ 123362306a36Sopenharmony_ci struct nh_group *nhg; 123462306a36Sopenharmony_ci 123562306a36Sopenharmony_ci if (!nh->is_group) 123662306a36Sopenharmony_ci return nh; 123762306a36Sopenharmony_ci 123862306a36Sopenharmony_ci nhg = rcu_dereference(nh->nh_grp); 123962306a36Sopenharmony_ci if (nhg->hash_threshold) 124062306a36Sopenharmony_ci return nexthop_select_path_hthr(nhg, hash); 124162306a36Sopenharmony_ci else if (nhg->resilient) 124262306a36Sopenharmony_ci return nexthop_select_path_res(nhg, hash); 124362306a36Sopenharmony_ci 124462306a36Sopenharmony_ci /* Unreachable. */ 124562306a36Sopenharmony_ci return NULL; 124662306a36Sopenharmony_ci} 124762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(nexthop_select_path); 124862306a36Sopenharmony_ci 124962306a36Sopenharmony_ciint nexthop_for_each_fib6_nh(struct nexthop *nh, 125062306a36Sopenharmony_ci int (*cb)(struct fib6_nh *nh, void *arg), 125162306a36Sopenharmony_ci void *arg) 125262306a36Sopenharmony_ci{ 125362306a36Sopenharmony_ci struct nh_info *nhi; 125462306a36Sopenharmony_ci int err; 125562306a36Sopenharmony_ci 125662306a36Sopenharmony_ci if (nh->is_group) { 125762306a36Sopenharmony_ci struct nh_group *nhg; 125862306a36Sopenharmony_ci int i; 125962306a36Sopenharmony_ci 126062306a36Sopenharmony_ci nhg = rcu_dereference_rtnl(nh->nh_grp); 126162306a36Sopenharmony_ci for (i = 0; i < nhg->num_nh; i++) { 126262306a36Sopenharmony_ci struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 126362306a36Sopenharmony_ci 126462306a36Sopenharmony_ci nhi = rcu_dereference_rtnl(nhge->nh->nh_info); 126562306a36Sopenharmony_ci err = cb(&nhi->fib6_nh, arg); 126662306a36Sopenharmony_ci if (err) 126762306a36Sopenharmony_ci return err; 126862306a36Sopenharmony_ci } 126962306a36Sopenharmony_ci } else { 127062306a36Sopenharmony_ci nhi = rcu_dereference_rtnl(nh->nh_info); 127162306a36Sopenharmony_ci err = cb(&nhi->fib6_nh, arg); 127262306a36Sopenharmony_ci if (err) 127362306a36Sopenharmony_ci return err; 127462306a36Sopenharmony_ci } 127562306a36Sopenharmony_ci 127662306a36Sopenharmony_ci return 0; 127762306a36Sopenharmony_ci} 127862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(nexthop_for_each_fib6_nh); 127962306a36Sopenharmony_ci 128062306a36Sopenharmony_cistatic int check_src_addr(const struct in6_addr *saddr, 128162306a36Sopenharmony_ci struct netlink_ext_ack *extack) 128262306a36Sopenharmony_ci{ 128362306a36Sopenharmony_ci if (!ipv6_addr_any(saddr)) { 128462306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "IPv6 routes using source address can not use nexthop objects"); 128562306a36Sopenharmony_ci return -EINVAL; 128662306a36Sopenharmony_ci } 128762306a36Sopenharmony_ci return 0; 128862306a36Sopenharmony_ci} 128962306a36Sopenharmony_ci 129062306a36Sopenharmony_ciint fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg, 129162306a36Sopenharmony_ci struct netlink_ext_ack *extack) 129262306a36Sopenharmony_ci{ 129362306a36Sopenharmony_ci struct nh_info *nhi; 129462306a36Sopenharmony_ci bool is_fdb_nh; 129562306a36Sopenharmony_ci 129662306a36Sopenharmony_ci /* fib6_src is unique to a fib6_info and limits the ability to cache 129762306a36Sopenharmony_ci * routes in fib6_nh within a nexthop that is potentially shared 129862306a36Sopenharmony_ci * across multiple fib entries. If the config wants to use source 129962306a36Sopenharmony_ci * routing it can not use nexthop objects. mlxsw also does not allow 130062306a36Sopenharmony_ci * fib6_src on routes. 130162306a36Sopenharmony_ci */ 130262306a36Sopenharmony_ci if (cfg && check_src_addr(&cfg->fc_src, extack) < 0) 130362306a36Sopenharmony_ci return -EINVAL; 130462306a36Sopenharmony_ci 130562306a36Sopenharmony_ci if (nh->is_group) { 130662306a36Sopenharmony_ci struct nh_group *nhg; 130762306a36Sopenharmony_ci 130862306a36Sopenharmony_ci nhg = rtnl_dereference(nh->nh_grp); 130962306a36Sopenharmony_ci if (nhg->has_v4) 131062306a36Sopenharmony_ci goto no_v4_nh; 131162306a36Sopenharmony_ci is_fdb_nh = nhg->fdb_nh; 131262306a36Sopenharmony_ci } else { 131362306a36Sopenharmony_ci nhi = rtnl_dereference(nh->nh_info); 131462306a36Sopenharmony_ci if (nhi->family == AF_INET) 131562306a36Sopenharmony_ci goto no_v4_nh; 131662306a36Sopenharmony_ci is_fdb_nh = nhi->fdb_nh; 131762306a36Sopenharmony_ci } 131862306a36Sopenharmony_ci 131962306a36Sopenharmony_ci if (is_fdb_nh) { 132062306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop"); 132162306a36Sopenharmony_ci return -EINVAL; 132262306a36Sopenharmony_ci } 132362306a36Sopenharmony_ci 132462306a36Sopenharmony_ci return 0; 132562306a36Sopenharmony_cino_v4_nh: 132662306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "IPv6 routes can not use an IPv4 nexthop"); 132762306a36Sopenharmony_ci return -EINVAL; 132862306a36Sopenharmony_ci} 132962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(fib6_check_nexthop); 133062306a36Sopenharmony_ci 133162306a36Sopenharmony_ci/* if existing nexthop has ipv6 routes linked to it, need 133262306a36Sopenharmony_ci * to verify this new spec works with ipv6 133362306a36Sopenharmony_ci */ 133462306a36Sopenharmony_cistatic int fib6_check_nh_list(struct nexthop *old, struct nexthop *new, 133562306a36Sopenharmony_ci struct netlink_ext_ack *extack) 133662306a36Sopenharmony_ci{ 133762306a36Sopenharmony_ci struct fib6_info *f6i; 133862306a36Sopenharmony_ci 133962306a36Sopenharmony_ci if (list_empty(&old->f6i_list)) 134062306a36Sopenharmony_ci return 0; 134162306a36Sopenharmony_ci 134262306a36Sopenharmony_ci list_for_each_entry(f6i, &old->f6i_list, nh_list) { 134362306a36Sopenharmony_ci if (check_src_addr(&f6i->fib6_src.addr, extack) < 0) 134462306a36Sopenharmony_ci return -EINVAL; 134562306a36Sopenharmony_ci } 134662306a36Sopenharmony_ci 134762306a36Sopenharmony_ci return fib6_check_nexthop(new, NULL, extack); 134862306a36Sopenharmony_ci} 134962306a36Sopenharmony_ci 135062306a36Sopenharmony_cistatic int nexthop_check_scope(struct nh_info *nhi, u8 scope, 135162306a36Sopenharmony_ci struct netlink_ext_ack *extack) 135262306a36Sopenharmony_ci{ 135362306a36Sopenharmony_ci if (scope == RT_SCOPE_HOST && nhi->fib_nhc.nhc_gw_family) { 135462306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, 135562306a36Sopenharmony_ci "Route with host scope can not have a gateway"); 135662306a36Sopenharmony_ci return -EINVAL; 135762306a36Sopenharmony_ci } 135862306a36Sopenharmony_ci 135962306a36Sopenharmony_ci if (nhi->fib_nhc.nhc_flags & RTNH_F_ONLINK && scope >= RT_SCOPE_LINK) { 136062306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Scope mismatch with nexthop"); 136162306a36Sopenharmony_ci return -EINVAL; 136262306a36Sopenharmony_ci } 136362306a36Sopenharmony_ci 136462306a36Sopenharmony_ci return 0; 136562306a36Sopenharmony_ci} 136662306a36Sopenharmony_ci 136762306a36Sopenharmony_ci/* Invoked by fib add code to verify nexthop by id is ok with 136862306a36Sopenharmony_ci * config for prefix; parts of fib_check_nh not done when nexthop 136962306a36Sopenharmony_ci * object is used. 137062306a36Sopenharmony_ci */ 137162306a36Sopenharmony_ciint fib_check_nexthop(struct nexthop *nh, u8 scope, 137262306a36Sopenharmony_ci struct netlink_ext_ack *extack) 137362306a36Sopenharmony_ci{ 137462306a36Sopenharmony_ci struct nh_info *nhi; 137562306a36Sopenharmony_ci int err = 0; 137662306a36Sopenharmony_ci 137762306a36Sopenharmony_ci if (nh->is_group) { 137862306a36Sopenharmony_ci struct nh_group *nhg; 137962306a36Sopenharmony_ci 138062306a36Sopenharmony_ci nhg = rtnl_dereference(nh->nh_grp); 138162306a36Sopenharmony_ci if (nhg->fdb_nh) { 138262306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop"); 138362306a36Sopenharmony_ci err = -EINVAL; 138462306a36Sopenharmony_ci goto out; 138562306a36Sopenharmony_ci } 138662306a36Sopenharmony_ci 138762306a36Sopenharmony_ci if (scope == RT_SCOPE_HOST) { 138862306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Route with host scope can not have multiple nexthops"); 138962306a36Sopenharmony_ci err = -EINVAL; 139062306a36Sopenharmony_ci goto out; 139162306a36Sopenharmony_ci } 139262306a36Sopenharmony_ci 139362306a36Sopenharmony_ci /* all nexthops in a group have the same scope */ 139462306a36Sopenharmony_ci nhi = rtnl_dereference(nhg->nh_entries[0].nh->nh_info); 139562306a36Sopenharmony_ci err = nexthop_check_scope(nhi, scope, extack); 139662306a36Sopenharmony_ci } else { 139762306a36Sopenharmony_ci nhi = rtnl_dereference(nh->nh_info); 139862306a36Sopenharmony_ci if (nhi->fdb_nh) { 139962306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop"); 140062306a36Sopenharmony_ci err = -EINVAL; 140162306a36Sopenharmony_ci goto out; 140262306a36Sopenharmony_ci } 140362306a36Sopenharmony_ci err = nexthop_check_scope(nhi, scope, extack); 140462306a36Sopenharmony_ci } 140562306a36Sopenharmony_ci 140662306a36Sopenharmony_ciout: 140762306a36Sopenharmony_ci return err; 140862306a36Sopenharmony_ci} 140962306a36Sopenharmony_ci 141062306a36Sopenharmony_cistatic int fib_check_nh_list(struct nexthop *old, struct nexthop *new, 141162306a36Sopenharmony_ci struct netlink_ext_ack *extack) 141262306a36Sopenharmony_ci{ 141362306a36Sopenharmony_ci struct fib_info *fi; 141462306a36Sopenharmony_ci 141562306a36Sopenharmony_ci list_for_each_entry(fi, &old->fi_list, nh_list) { 141662306a36Sopenharmony_ci int err; 141762306a36Sopenharmony_ci 141862306a36Sopenharmony_ci err = fib_check_nexthop(new, fi->fib_scope, extack); 141962306a36Sopenharmony_ci if (err) 142062306a36Sopenharmony_ci return err; 142162306a36Sopenharmony_ci } 142262306a36Sopenharmony_ci return 0; 142362306a36Sopenharmony_ci} 142462306a36Sopenharmony_ci 142562306a36Sopenharmony_cistatic bool nh_res_nhge_is_balanced(const struct nh_grp_entry *nhge) 142662306a36Sopenharmony_ci{ 142762306a36Sopenharmony_ci return nhge->res.count_buckets == nhge->res.wants_buckets; 142862306a36Sopenharmony_ci} 142962306a36Sopenharmony_ci 143062306a36Sopenharmony_cistatic bool nh_res_nhge_is_ow(const struct nh_grp_entry *nhge) 143162306a36Sopenharmony_ci{ 143262306a36Sopenharmony_ci return nhge->res.count_buckets > nhge->res.wants_buckets; 143362306a36Sopenharmony_ci} 143462306a36Sopenharmony_ci 143562306a36Sopenharmony_cistatic bool nh_res_nhge_is_uw(const struct nh_grp_entry *nhge) 143662306a36Sopenharmony_ci{ 143762306a36Sopenharmony_ci return nhge->res.count_buckets < nhge->res.wants_buckets; 143862306a36Sopenharmony_ci} 143962306a36Sopenharmony_ci 144062306a36Sopenharmony_cistatic bool nh_res_table_is_balanced(const struct nh_res_table *res_table) 144162306a36Sopenharmony_ci{ 144262306a36Sopenharmony_ci return list_empty(&res_table->uw_nh_entries); 144362306a36Sopenharmony_ci} 144462306a36Sopenharmony_ci 144562306a36Sopenharmony_cistatic void nh_res_bucket_unset_nh(struct nh_res_bucket *bucket) 144662306a36Sopenharmony_ci{ 144762306a36Sopenharmony_ci struct nh_grp_entry *nhge; 144862306a36Sopenharmony_ci 144962306a36Sopenharmony_ci if (bucket->occupied) { 145062306a36Sopenharmony_ci nhge = nh_res_dereference(bucket->nh_entry); 145162306a36Sopenharmony_ci nhge->res.count_buckets--; 145262306a36Sopenharmony_ci bucket->occupied = false; 145362306a36Sopenharmony_ci } 145462306a36Sopenharmony_ci} 145562306a36Sopenharmony_ci 145662306a36Sopenharmony_cistatic void nh_res_bucket_set_nh(struct nh_res_bucket *bucket, 145762306a36Sopenharmony_ci struct nh_grp_entry *nhge) 145862306a36Sopenharmony_ci{ 145962306a36Sopenharmony_ci nh_res_bucket_unset_nh(bucket); 146062306a36Sopenharmony_ci 146162306a36Sopenharmony_ci bucket->occupied = true; 146262306a36Sopenharmony_ci rcu_assign_pointer(bucket->nh_entry, nhge); 146362306a36Sopenharmony_ci nhge->res.count_buckets++; 146462306a36Sopenharmony_ci} 146562306a36Sopenharmony_ci 146662306a36Sopenharmony_cistatic bool nh_res_bucket_should_migrate(struct nh_res_table *res_table, 146762306a36Sopenharmony_ci struct nh_res_bucket *bucket, 146862306a36Sopenharmony_ci unsigned long *deadline, bool *force) 146962306a36Sopenharmony_ci{ 147062306a36Sopenharmony_ci unsigned long now = jiffies; 147162306a36Sopenharmony_ci struct nh_grp_entry *nhge; 147262306a36Sopenharmony_ci unsigned long idle_point; 147362306a36Sopenharmony_ci 147462306a36Sopenharmony_ci if (!bucket->occupied) { 147562306a36Sopenharmony_ci /* The bucket is not occupied, its NHGE pointer is either 147662306a36Sopenharmony_ci * NULL or obsolete. We _have to_ migrate: set force. 147762306a36Sopenharmony_ci */ 147862306a36Sopenharmony_ci *force = true; 147962306a36Sopenharmony_ci return true; 148062306a36Sopenharmony_ci } 148162306a36Sopenharmony_ci 148262306a36Sopenharmony_ci nhge = nh_res_dereference(bucket->nh_entry); 148362306a36Sopenharmony_ci 148462306a36Sopenharmony_ci /* If the bucket is populated by an underweight or balanced 148562306a36Sopenharmony_ci * nexthop, do not migrate. 148662306a36Sopenharmony_ci */ 148762306a36Sopenharmony_ci if (!nh_res_nhge_is_ow(nhge)) 148862306a36Sopenharmony_ci return false; 148962306a36Sopenharmony_ci 149062306a36Sopenharmony_ci /* At this point we know that the bucket is populated with an 149162306a36Sopenharmony_ci * overweight nexthop. It needs to be migrated to a new nexthop if 149262306a36Sopenharmony_ci * the idle timer of unbalanced timer expired. 149362306a36Sopenharmony_ci */ 149462306a36Sopenharmony_ci 149562306a36Sopenharmony_ci idle_point = nh_res_bucket_idle_point(res_table, bucket, now); 149662306a36Sopenharmony_ci if (time_after_eq(now, idle_point)) { 149762306a36Sopenharmony_ci /* The bucket is idle. We _can_ migrate: unset force. */ 149862306a36Sopenharmony_ci *force = false; 149962306a36Sopenharmony_ci return true; 150062306a36Sopenharmony_ci } 150162306a36Sopenharmony_ci 150262306a36Sopenharmony_ci /* Unbalanced timer of 0 means "never force". */ 150362306a36Sopenharmony_ci if (res_table->unbalanced_timer) { 150462306a36Sopenharmony_ci unsigned long unb_point; 150562306a36Sopenharmony_ci 150662306a36Sopenharmony_ci unb_point = nh_res_table_unb_point(res_table); 150762306a36Sopenharmony_ci if (time_after(now, unb_point)) { 150862306a36Sopenharmony_ci /* The bucket is not idle, but the unbalanced timer 150962306a36Sopenharmony_ci * expired. We _can_ migrate, but set force anyway, 151062306a36Sopenharmony_ci * so that drivers know to ignore activity reports 151162306a36Sopenharmony_ci * from the HW. 151262306a36Sopenharmony_ci */ 151362306a36Sopenharmony_ci *force = true; 151462306a36Sopenharmony_ci return true; 151562306a36Sopenharmony_ci } 151662306a36Sopenharmony_ci 151762306a36Sopenharmony_ci nh_res_time_set_deadline(unb_point, deadline); 151862306a36Sopenharmony_ci } 151962306a36Sopenharmony_ci 152062306a36Sopenharmony_ci nh_res_time_set_deadline(idle_point, deadline); 152162306a36Sopenharmony_ci return false; 152262306a36Sopenharmony_ci} 152362306a36Sopenharmony_ci 152462306a36Sopenharmony_cistatic bool nh_res_bucket_migrate(struct nh_res_table *res_table, 152562306a36Sopenharmony_ci u16 bucket_index, bool notify, 152662306a36Sopenharmony_ci bool notify_nl, bool force) 152762306a36Sopenharmony_ci{ 152862306a36Sopenharmony_ci struct nh_res_bucket *bucket = &res_table->nh_buckets[bucket_index]; 152962306a36Sopenharmony_ci struct nh_grp_entry *new_nhge; 153062306a36Sopenharmony_ci struct netlink_ext_ack extack; 153162306a36Sopenharmony_ci int err; 153262306a36Sopenharmony_ci 153362306a36Sopenharmony_ci new_nhge = list_first_entry_or_null(&res_table->uw_nh_entries, 153462306a36Sopenharmony_ci struct nh_grp_entry, 153562306a36Sopenharmony_ci res.uw_nh_entry); 153662306a36Sopenharmony_ci if (WARN_ON_ONCE(!new_nhge)) 153762306a36Sopenharmony_ci /* If this function is called, "bucket" is either not 153862306a36Sopenharmony_ci * occupied, or it belongs to a next hop that is 153962306a36Sopenharmony_ci * overweight. In either case, there ought to be a 154062306a36Sopenharmony_ci * corresponding underweight next hop. 154162306a36Sopenharmony_ci */ 154262306a36Sopenharmony_ci return false; 154362306a36Sopenharmony_ci 154462306a36Sopenharmony_ci if (notify) { 154562306a36Sopenharmony_ci struct nh_grp_entry *old_nhge; 154662306a36Sopenharmony_ci 154762306a36Sopenharmony_ci old_nhge = nh_res_dereference(bucket->nh_entry); 154862306a36Sopenharmony_ci err = call_nexthop_res_bucket_notifiers(res_table->net, 154962306a36Sopenharmony_ci res_table->nhg_id, 155062306a36Sopenharmony_ci bucket_index, force, 155162306a36Sopenharmony_ci old_nhge->nh, 155262306a36Sopenharmony_ci new_nhge->nh, &extack); 155362306a36Sopenharmony_ci if (err) { 155462306a36Sopenharmony_ci pr_err_ratelimited("%s\n", extack._msg); 155562306a36Sopenharmony_ci if (!force) 155662306a36Sopenharmony_ci return false; 155762306a36Sopenharmony_ci /* It is not possible to veto a forced replacement, so 155862306a36Sopenharmony_ci * just clear the hardware flags from the nexthop 155962306a36Sopenharmony_ci * bucket to indicate to user space that this bucket is 156062306a36Sopenharmony_ci * not correctly populated in hardware. 156162306a36Sopenharmony_ci */ 156262306a36Sopenharmony_ci bucket->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP); 156362306a36Sopenharmony_ci } 156462306a36Sopenharmony_ci } 156562306a36Sopenharmony_ci 156662306a36Sopenharmony_ci nh_res_bucket_set_nh(bucket, new_nhge); 156762306a36Sopenharmony_ci nh_res_bucket_set_idle(res_table, bucket); 156862306a36Sopenharmony_ci 156962306a36Sopenharmony_ci if (notify_nl) 157062306a36Sopenharmony_ci nexthop_bucket_notify(res_table, bucket_index); 157162306a36Sopenharmony_ci 157262306a36Sopenharmony_ci if (nh_res_nhge_is_balanced(new_nhge)) 157362306a36Sopenharmony_ci list_del(&new_nhge->res.uw_nh_entry); 157462306a36Sopenharmony_ci return true; 157562306a36Sopenharmony_ci} 157662306a36Sopenharmony_ci 157762306a36Sopenharmony_ci#define NH_RES_UPKEEP_DW_MINIMUM_INTERVAL (HZ / 2) 157862306a36Sopenharmony_ci 157962306a36Sopenharmony_cistatic void nh_res_table_upkeep(struct nh_res_table *res_table, 158062306a36Sopenharmony_ci bool notify, bool notify_nl) 158162306a36Sopenharmony_ci{ 158262306a36Sopenharmony_ci unsigned long now = jiffies; 158362306a36Sopenharmony_ci unsigned long deadline; 158462306a36Sopenharmony_ci u16 i; 158562306a36Sopenharmony_ci 158662306a36Sopenharmony_ci /* Deadline is the next time that upkeep should be run. It is the 158762306a36Sopenharmony_ci * earliest time at which one of the buckets might be migrated. 158862306a36Sopenharmony_ci * Start at the most pessimistic estimate: either unbalanced_timer 158962306a36Sopenharmony_ci * from now, or if there is none, idle_timer from now. For each 159062306a36Sopenharmony_ci * encountered time point, call nh_res_time_set_deadline() to 159162306a36Sopenharmony_ci * refine the estimate. 159262306a36Sopenharmony_ci */ 159362306a36Sopenharmony_ci if (res_table->unbalanced_timer) 159462306a36Sopenharmony_ci deadline = now + res_table->unbalanced_timer; 159562306a36Sopenharmony_ci else 159662306a36Sopenharmony_ci deadline = now + res_table->idle_timer; 159762306a36Sopenharmony_ci 159862306a36Sopenharmony_ci for (i = 0; i < res_table->num_nh_buckets; i++) { 159962306a36Sopenharmony_ci struct nh_res_bucket *bucket = &res_table->nh_buckets[i]; 160062306a36Sopenharmony_ci bool force; 160162306a36Sopenharmony_ci 160262306a36Sopenharmony_ci if (nh_res_bucket_should_migrate(res_table, bucket, 160362306a36Sopenharmony_ci &deadline, &force)) { 160462306a36Sopenharmony_ci if (!nh_res_bucket_migrate(res_table, i, notify, 160562306a36Sopenharmony_ci notify_nl, force)) { 160662306a36Sopenharmony_ci unsigned long idle_point; 160762306a36Sopenharmony_ci 160862306a36Sopenharmony_ci /* A driver can override the migration 160962306a36Sopenharmony_ci * decision if the HW reports that the 161062306a36Sopenharmony_ci * bucket is actually not idle. Therefore 161162306a36Sopenharmony_ci * remark the bucket as busy again and 161262306a36Sopenharmony_ci * update the deadline. 161362306a36Sopenharmony_ci */ 161462306a36Sopenharmony_ci nh_res_bucket_set_busy(bucket); 161562306a36Sopenharmony_ci idle_point = nh_res_bucket_idle_point(res_table, 161662306a36Sopenharmony_ci bucket, 161762306a36Sopenharmony_ci now); 161862306a36Sopenharmony_ci nh_res_time_set_deadline(idle_point, &deadline); 161962306a36Sopenharmony_ci } 162062306a36Sopenharmony_ci } 162162306a36Sopenharmony_ci } 162262306a36Sopenharmony_ci 162362306a36Sopenharmony_ci /* If the group is still unbalanced, schedule the next upkeep to 162462306a36Sopenharmony_ci * either the deadline computed above, or the minimum deadline, 162562306a36Sopenharmony_ci * whichever comes later. 162662306a36Sopenharmony_ci */ 162762306a36Sopenharmony_ci if (!nh_res_table_is_balanced(res_table)) { 162862306a36Sopenharmony_ci unsigned long now = jiffies; 162962306a36Sopenharmony_ci unsigned long min_deadline; 163062306a36Sopenharmony_ci 163162306a36Sopenharmony_ci min_deadline = now + NH_RES_UPKEEP_DW_MINIMUM_INTERVAL; 163262306a36Sopenharmony_ci if (time_before(deadline, min_deadline)) 163362306a36Sopenharmony_ci deadline = min_deadline; 163462306a36Sopenharmony_ci 163562306a36Sopenharmony_ci queue_delayed_work(system_power_efficient_wq, 163662306a36Sopenharmony_ci &res_table->upkeep_dw, deadline - now); 163762306a36Sopenharmony_ci } 163862306a36Sopenharmony_ci} 163962306a36Sopenharmony_ci 164062306a36Sopenharmony_cistatic void nh_res_table_upkeep_dw(struct work_struct *work) 164162306a36Sopenharmony_ci{ 164262306a36Sopenharmony_ci struct delayed_work *dw = to_delayed_work(work); 164362306a36Sopenharmony_ci struct nh_res_table *res_table; 164462306a36Sopenharmony_ci 164562306a36Sopenharmony_ci res_table = container_of(dw, struct nh_res_table, upkeep_dw); 164662306a36Sopenharmony_ci nh_res_table_upkeep(res_table, true, true); 164762306a36Sopenharmony_ci} 164862306a36Sopenharmony_ci 164962306a36Sopenharmony_cistatic void nh_res_table_cancel_upkeep(struct nh_res_table *res_table) 165062306a36Sopenharmony_ci{ 165162306a36Sopenharmony_ci cancel_delayed_work_sync(&res_table->upkeep_dw); 165262306a36Sopenharmony_ci} 165362306a36Sopenharmony_ci 165462306a36Sopenharmony_cistatic void nh_res_group_rebalance(struct nh_group *nhg, 165562306a36Sopenharmony_ci struct nh_res_table *res_table) 165662306a36Sopenharmony_ci{ 165762306a36Sopenharmony_ci int prev_upper_bound = 0; 165862306a36Sopenharmony_ci int total = 0; 165962306a36Sopenharmony_ci int w = 0; 166062306a36Sopenharmony_ci int i; 166162306a36Sopenharmony_ci 166262306a36Sopenharmony_ci INIT_LIST_HEAD(&res_table->uw_nh_entries); 166362306a36Sopenharmony_ci 166462306a36Sopenharmony_ci for (i = 0; i < nhg->num_nh; ++i) 166562306a36Sopenharmony_ci total += nhg->nh_entries[i].weight; 166662306a36Sopenharmony_ci 166762306a36Sopenharmony_ci for (i = 0; i < nhg->num_nh; ++i) { 166862306a36Sopenharmony_ci struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 166962306a36Sopenharmony_ci int upper_bound; 167062306a36Sopenharmony_ci 167162306a36Sopenharmony_ci w += nhge->weight; 167262306a36Sopenharmony_ci upper_bound = DIV_ROUND_CLOSEST(res_table->num_nh_buckets * w, 167362306a36Sopenharmony_ci total); 167462306a36Sopenharmony_ci nhge->res.wants_buckets = upper_bound - prev_upper_bound; 167562306a36Sopenharmony_ci prev_upper_bound = upper_bound; 167662306a36Sopenharmony_ci 167762306a36Sopenharmony_ci if (nh_res_nhge_is_uw(nhge)) { 167862306a36Sopenharmony_ci if (list_empty(&res_table->uw_nh_entries)) 167962306a36Sopenharmony_ci res_table->unbalanced_since = jiffies; 168062306a36Sopenharmony_ci list_add(&nhge->res.uw_nh_entry, 168162306a36Sopenharmony_ci &res_table->uw_nh_entries); 168262306a36Sopenharmony_ci } 168362306a36Sopenharmony_ci } 168462306a36Sopenharmony_ci} 168562306a36Sopenharmony_ci 168662306a36Sopenharmony_ci/* Migrate buckets in res_table so that they reference NHGE's from NHG with 168762306a36Sopenharmony_ci * the right NH ID. Set those buckets that do not have a corresponding NHGE 168862306a36Sopenharmony_ci * entry in NHG as not occupied. 168962306a36Sopenharmony_ci */ 169062306a36Sopenharmony_cistatic void nh_res_table_migrate_buckets(struct nh_res_table *res_table, 169162306a36Sopenharmony_ci struct nh_group *nhg) 169262306a36Sopenharmony_ci{ 169362306a36Sopenharmony_ci u16 i; 169462306a36Sopenharmony_ci 169562306a36Sopenharmony_ci for (i = 0; i < res_table->num_nh_buckets; i++) { 169662306a36Sopenharmony_ci struct nh_res_bucket *bucket = &res_table->nh_buckets[i]; 169762306a36Sopenharmony_ci u32 id = rtnl_dereference(bucket->nh_entry)->nh->id; 169862306a36Sopenharmony_ci bool found = false; 169962306a36Sopenharmony_ci int j; 170062306a36Sopenharmony_ci 170162306a36Sopenharmony_ci for (j = 0; j < nhg->num_nh; j++) { 170262306a36Sopenharmony_ci struct nh_grp_entry *nhge = &nhg->nh_entries[j]; 170362306a36Sopenharmony_ci 170462306a36Sopenharmony_ci if (nhge->nh->id == id) { 170562306a36Sopenharmony_ci nh_res_bucket_set_nh(bucket, nhge); 170662306a36Sopenharmony_ci found = true; 170762306a36Sopenharmony_ci break; 170862306a36Sopenharmony_ci } 170962306a36Sopenharmony_ci } 171062306a36Sopenharmony_ci 171162306a36Sopenharmony_ci if (!found) 171262306a36Sopenharmony_ci nh_res_bucket_unset_nh(bucket); 171362306a36Sopenharmony_ci } 171462306a36Sopenharmony_ci} 171562306a36Sopenharmony_ci 171662306a36Sopenharmony_cistatic void replace_nexthop_grp_res(struct nh_group *oldg, 171762306a36Sopenharmony_ci struct nh_group *newg) 171862306a36Sopenharmony_ci{ 171962306a36Sopenharmony_ci /* For NH group replacement, the new NHG might only have a stub 172062306a36Sopenharmony_ci * hash table with 0 buckets, because the number of buckets was not 172162306a36Sopenharmony_ci * specified. For NH removal, oldg and newg both reference the same 172262306a36Sopenharmony_ci * res_table. So in any case, in the following, we want to work 172362306a36Sopenharmony_ci * with oldg->res_table. 172462306a36Sopenharmony_ci */ 172562306a36Sopenharmony_ci struct nh_res_table *old_res_table = rtnl_dereference(oldg->res_table); 172662306a36Sopenharmony_ci unsigned long prev_unbalanced_since = old_res_table->unbalanced_since; 172762306a36Sopenharmony_ci bool prev_has_uw = !list_empty(&old_res_table->uw_nh_entries); 172862306a36Sopenharmony_ci 172962306a36Sopenharmony_ci nh_res_table_cancel_upkeep(old_res_table); 173062306a36Sopenharmony_ci nh_res_table_migrate_buckets(old_res_table, newg); 173162306a36Sopenharmony_ci nh_res_group_rebalance(newg, old_res_table); 173262306a36Sopenharmony_ci if (prev_has_uw && !list_empty(&old_res_table->uw_nh_entries)) 173362306a36Sopenharmony_ci old_res_table->unbalanced_since = prev_unbalanced_since; 173462306a36Sopenharmony_ci nh_res_table_upkeep(old_res_table, true, false); 173562306a36Sopenharmony_ci} 173662306a36Sopenharmony_ci 173762306a36Sopenharmony_cistatic void nh_hthr_group_rebalance(struct nh_group *nhg) 173862306a36Sopenharmony_ci{ 173962306a36Sopenharmony_ci int total = 0; 174062306a36Sopenharmony_ci int w = 0; 174162306a36Sopenharmony_ci int i; 174262306a36Sopenharmony_ci 174362306a36Sopenharmony_ci for (i = 0; i < nhg->num_nh; ++i) 174462306a36Sopenharmony_ci total += nhg->nh_entries[i].weight; 174562306a36Sopenharmony_ci 174662306a36Sopenharmony_ci for (i = 0; i < nhg->num_nh; ++i) { 174762306a36Sopenharmony_ci struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 174862306a36Sopenharmony_ci int upper_bound; 174962306a36Sopenharmony_ci 175062306a36Sopenharmony_ci w += nhge->weight; 175162306a36Sopenharmony_ci upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31, total) - 1; 175262306a36Sopenharmony_ci atomic_set(&nhge->hthr.upper_bound, upper_bound); 175362306a36Sopenharmony_ci } 175462306a36Sopenharmony_ci} 175562306a36Sopenharmony_ci 175662306a36Sopenharmony_cistatic void remove_nh_grp_entry(struct net *net, struct nh_grp_entry *nhge, 175762306a36Sopenharmony_ci struct nl_info *nlinfo) 175862306a36Sopenharmony_ci{ 175962306a36Sopenharmony_ci struct nh_grp_entry *nhges, *new_nhges; 176062306a36Sopenharmony_ci struct nexthop *nhp = nhge->nh_parent; 176162306a36Sopenharmony_ci struct netlink_ext_ack extack; 176262306a36Sopenharmony_ci struct nexthop *nh = nhge->nh; 176362306a36Sopenharmony_ci struct nh_group *nhg, *newg; 176462306a36Sopenharmony_ci int i, j, err; 176562306a36Sopenharmony_ci 176662306a36Sopenharmony_ci WARN_ON(!nh); 176762306a36Sopenharmony_ci 176862306a36Sopenharmony_ci nhg = rtnl_dereference(nhp->nh_grp); 176962306a36Sopenharmony_ci newg = nhg->spare; 177062306a36Sopenharmony_ci 177162306a36Sopenharmony_ci /* last entry, keep it visible and remove the parent */ 177262306a36Sopenharmony_ci if (nhg->num_nh == 1) { 177362306a36Sopenharmony_ci remove_nexthop(net, nhp, nlinfo); 177462306a36Sopenharmony_ci return; 177562306a36Sopenharmony_ci } 177662306a36Sopenharmony_ci 177762306a36Sopenharmony_ci newg->has_v4 = false; 177862306a36Sopenharmony_ci newg->is_multipath = nhg->is_multipath; 177962306a36Sopenharmony_ci newg->hash_threshold = nhg->hash_threshold; 178062306a36Sopenharmony_ci newg->resilient = nhg->resilient; 178162306a36Sopenharmony_ci newg->fdb_nh = nhg->fdb_nh; 178262306a36Sopenharmony_ci newg->num_nh = nhg->num_nh; 178362306a36Sopenharmony_ci 178462306a36Sopenharmony_ci /* copy old entries to new except the one getting removed */ 178562306a36Sopenharmony_ci nhges = nhg->nh_entries; 178662306a36Sopenharmony_ci new_nhges = newg->nh_entries; 178762306a36Sopenharmony_ci for (i = 0, j = 0; i < nhg->num_nh; ++i) { 178862306a36Sopenharmony_ci struct nh_info *nhi; 178962306a36Sopenharmony_ci 179062306a36Sopenharmony_ci /* current nexthop getting removed */ 179162306a36Sopenharmony_ci if (nhg->nh_entries[i].nh == nh) { 179262306a36Sopenharmony_ci newg->num_nh--; 179362306a36Sopenharmony_ci continue; 179462306a36Sopenharmony_ci } 179562306a36Sopenharmony_ci 179662306a36Sopenharmony_ci nhi = rtnl_dereference(nhges[i].nh->nh_info); 179762306a36Sopenharmony_ci if (nhi->family == AF_INET) 179862306a36Sopenharmony_ci newg->has_v4 = true; 179962306a36Sopenharmony_ci 180062306a36Sopenharmony_ci list_del(&nhges[i].nh_list); 180162306a36Sopenharmony_ci new_nhges[j].nh_parent = nhges[i].nh_parent; 180262306a36Sopenharmony_ci new_nhges[j].nh = nhges[i].nh; 180362306a36Sopenharmony_ci new_nhges[j].weight = nhges[i].weight; 180462306a36Sopenharmony_ci list_add(&new_nhges[j].nh_list, &new_nhges[j].nh->grp_list); 180562306a36Sopenharmony_ci j++; 180662306a36Sopenharmony_ci } 180762306a36Sopenharmony_ci 180862306a36Sopenharmony_ci if (newg->hash_threshold) 180962306a36Sopenharmony_ci nh_hthr_group_rebalance(newg); 181062306a36Sopenharmony_ci else if (newg->resilient) 181162306a36Sopenharmony_ci replace_nexthop_grp_res(nhg, newg); 181262306a36Sopenharmony_ci 181362306a36Sopenharmony_ci rcu_assign_pointer(nhp->nh_grp, newg); 181462306a36Sopenharmony_ci 181562306a36Sopenharmony_ci list_del(&nhge->nh_list); 181662306a36Sopenharmony_ci nexthop_put(nhge->nh); 181762306a36Sopenharmony_ci 181862306a36Sopenharmony_ci /* Removal of a NH from a resilient group is notified through 181962306a36Sopenharmony_ci * bucket notifications. 182062306a36Sopenharmony_ci */ 182162306a36Sopenharmony_ci if (newg->hash_threshold) { 182262306a36Sopenharmony_ci err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, nhp, 182362306a36Sopenharmony_ci &extack); 182462306a36Sopenharmony_ci if (err) 182562306a36Sopenharmony_ci pr_err("%s\n", extack._msg); 182662306a36Sopenharmony_ci } 182762306a36Sopenharmony_ci 182862306a36Sopenharmony_ci if (nlinfo) 182962306a36Sopenharmony_ci nexthop_notify(RTM_NEWNEXTHOP, nhp, nlinfo); 183062306a36Sopenharmony_ci} 183162306a36Sopenharmony_ci 183262306a36Sopenharmony_cistatic void remove_nexthop_from_groups(struct net *net, struct nexthop *nh, 183362306a36Sopenharmony_ci struct nl_info *nlinfo) 183462306a36Sopenharmony_ci{ 183562306a36Sopenharmony_ci struct nh_grp_entry *nhge, *tmp; 183662306a36Sopenharmony_ci 183762306a36Sopenharmony_ci list_for_each_entry_safe(nhge, tmp, &nh->grp_list, nh_list) 183862306a36Sopenharmony_ci remove_nh_grp_entry(net, nhge, nlinfo); 183962306a36Sopenharmony_ci 184062306a36Sopenharmony_ci /* make sure all see the newly published array before releasing rtnl */ 184162306a36Sopenharmony_ci synchronize_net(); 184262306a36Sopenharmony_ci} 184362306a36Sopenharmony_ci 184462306a36Sopenharmony_cistatic void remove_nexthop_group(struct nexthop *nh, struct nl_info *nlinfo) 184562306a36Sopenharmony_ci{ 184662306a36Sopenharmony_ci struct nh_group *nhg = rcu_dereference_rtnl(nh->nh_grp); 184762306a36Sopenharmony_ci struct nh_res_table *res_table; 184862306a36Sopenharmony_ci int i, num_nh = nhg->num_nh; 184962306a36Sopenharmony_ci 185062306a36Sopenharmony_ci for (i = 0; i < num_nh; ++i) { 185162306a36Sopenharmony_ci struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 185262306a36Sopenharmony_ci 185362306a36Sopenharmony_ci if (WARN_ON(!nhge->nh)) 185462306a36Sopenharmony_ci continue; 185562306a36Sopenharmony_ci 185662306a36Sopenharmony_ci list_del_init(&nhge->nh_list); 185762306a36Sopenharmony_ci } 185862306a36Sopenharmony_ci 185962306a36Sopenharmony_ci if (nhg->resilient) { 186062306a36Sopenharmony_ci res_table = rtnl_dereference(nhg->res_table); 186162306a36Sopenharmony_ci nh_res_table_cancel_upkeep(res_table); 186262306a36Sopenharmony_ci } 186362306a36Sopenharmony_ci} 186462306a36Sopenharmony_ci 186562306a36Sopenharmony_ci/* not called for nexthop replace */ 186662306a36Sopenharmony_cistatic void __remove_nexthop_fib(struct net *net, struct nexthop *nh) 186762306a36Sopenharmony_ci{ 186862306a36Sopenharmony_ci struct fib6_info *f6i, *tmp; 186962306a36Sopenharmony_ci bool do_flush = false; 187062306a36Sopenharmony_ci struct fib_info *fi; 187162306a36Sopenharmony_ci 187262306a36Sopenharmony_ci list_for_each_entry(fi, &nh->fi_list, nh_list) { 187362306a36Sopenharmony_ci fi->fib_flags |= RTNH_F_DEAD; 187462306a36Sopenharmony_ci do_flush = true; 187562306a36Sopenharmony_ci } 187662306a36Sopenharmony_ci if (do_flush) 187762306a36Sopenharmony_ci fib_flush(net); 187862306a36Sopenharmony_ci 187962306a36Sopenharmony_ci /* ip6_del_rt removes the entry from this list hence the _safe */ 188062306a36Sopenharmony_ci list_for_each_entry_safe(f6i, tmp, &nh->f6i_list, nh_list) { 188162306a36Sopenharmony_ci /* __ip6_del_rt does a release, so do a hold here */ 188262306a36Sopenharmony_ci fib6_info_hold(f6i); 188362306a36Sopenharmony_ci ipv6_stub->ip6_del_rt(net, f6i, 188462306a36Sopenharmony_ci !READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode)); 188562306a36Sopenharmony_ci } 188662306a36Sopenharmony_ci} 188762306a36Sopenharmony_ci 188862306a36Sopenharmony_cistatic void __remove_nexthop(struct net *net, struct nexthop *nh, 188962306a36Sopenharmony_ci struct nl_info *nlinfo) 189062306a36Sopenharmony_ci{ 189162306a36Sopenharmony_ci __remove_nexthop_fib(net, nh); 189262306a36Sopenharmony_ci 189362306a36Sopenharmony_ci if (nh->is_group) { 189462306a36Sopenharmony_ci remove_nexthop_group(nh, nlinfo); 189562306a36Sopenharmony_ci } else { 189662306a36Sopenharmony_ci struct nh_info *nhi; 189762306a36Sopenharmony_ci 189862306a36Sopenharmony_ci nhi = rtnl_dereference(nh->nh_info); 189962306a36Sopenharmony_ci if (nhi->fib_nhc.nhc_dev) 190062306a36Sopenharmony_ci hlist_del(&nhi->dev_hash); 190162306a36Sopenharmony_ci 190262306a36Sopenharmony_ci remove_nexthop_from_groups(net, nh, nlinfo); 190362306a36Sopenharmony_ci } 190462306a36Sopenharmony_ci} 190562306a36Sopenharmony_ci 190662306a36Sopenharmony_cistatic void remove_nexthop(struct net *net, struct nexthop *nh, 190762306a36Sopenharmony_ci struct nl_info *nlinfo) 190862306a36Sopenharmony_ci{ 190962306a36Sopenharmony_ci call_nexthop_notifiers(net, NEXTHOP_EVENT_DEL, nh, NULL); 191062306a36Sopenharmony_ci 191162306a36Sopenharmony_ci /* remove from the tree */ 191262306a36Sopenharmony_ci rb_erase(&nh->rb_node, &net->nexthop.rb_root); 191362306a36Sopenharmony_ci 191462306a36Sopenharmony_ci if (nlinfo) 191562306a36Sopenharmony_ci nexthop_notify(RTM_DELNEXTHOP, nh, nlinfo); 191662306a36Sopenharmony_ci 191762306a36Sopenharmony_ci __remove_nexthop(net, nh, nlinfo); 191862306a36Sopenharmony_ci nh_base_seq_inc(net); 191962306a36Sopenharmony_ci 192062306a36Sopenharmony_ci nexthop_put(nh); 192162306a36Sopenharmony_ci} 192262306a36Sopenharmony_ci 192362306a36Sopenharmony_ci/* if any FIB entries reference this nexthop, any dst entries 192462306a36Sopenharmony_ci * need to be regenerated 192562306a36Sopenharmony_ci */ 192662306a36Sopenharmony_cistatic void nh_rt_cache_flush(struct net *net, struct nexthop *nh, 192762306a36Sopenharmony_ci struct nexthop *replaced_nh) 192862306a36Sopenharmony_ci{ 192962306a36Sopenharmony_ci struct fib6_info *f6i; 193062306a36Sopenharmony_ci struct nh_group *nhg; 193162306a36Sopenharmony_ci int i; 193262306a36Sopenharmony_ci 193362306a36Sopenharmony_ci if (!list_empty(&nh->fi_list)) 193462306a36Sopenharmony_ci rt_cache_flush(net); 193562306a36Sopenharmony_ci 193662306a36Sopenharmony_ci list_for_each_entry(f6i, &nh->f6i_list, nh_list) 193762306a36Sopenharmony_ci ipv6_stub->fib6_update_sernum(net, f6i); 193862306a36Sopenharmony_ci 193962306a36Sopenharmony_ci /* if an IPv6 group was replaced, we have to release all old 194062306a36Sopenharmony_ci * dsts to make sure all refcounts are released 194162306a36Sopenharmony_ci */ 194262306a36Sopenharmony_ci if (!replaced_nh->is_group) 194362306a36Sopenharmony_ci return; 194462306a36Sopenharmony_ci 194562306a36Sopenharmony_ci nhg = rtnl_dereference(replaced_nh->nh_grp); 194662306a36Sopenharmony_ci for (i = 0; i < nhg->num_nh; i++) { 194762306a36Sopenharmony_ci struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 194862306a36Sopenharmony_ci struct nh_info *nhi = rtnl_dereference(nhge->nh->nh_info); 194962306a36Sopenharmony_ci 195062306a36Sopenharmony_ci if (nhi->family == AF_INET6) 195162306a36Sopenharmony_ci ipv6_stub->fib6_nh_release_dsts(&nhi->fib6_nh); 195262306a36Sopenharmony_ci } 195362306a36Sopenharmony_ci} 195462306a36Sopenharmony_ci 195562306a36Sopenharmony_cistatic int replace_nexthop_grp(struct net *net, struct nexthop *old, 195662306a36Sopenharmony_ci struct nexthop *new, const struct nh_config *cfg, 195762306a36Sopenharmony_ci struct netlink_ext_ack *extack) 195862306a36Sopenharmony_ci{ 195962306a36Sopenharmony_ci struct nh_res_table *tmp_table = NULL; 196062306a36Sopenharmony_ci struct nh_res_table *new_res_table; 196162306a36Sopenharmony_ci struct nh_res_table *old_res_table; 196262306a36Sopenharmony_ci struct nh_group *oldg, *newg; 196362306a36Sopenharmony_ci int i, err; 196462306a36Sopenharmony_ci 196562306a36Sopenharmony_ci if (!new->is_group) { 196662306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with a nexthop."); 196762306a36Sopenharmony_ci return -EINVAL; 196862306a36Sopenharmony_ci } 196962306a36Sopenharmony_ci 197062306a36Sopenharmony_ci oldg = rtnl_dereference(old->nh_grp); 197162306a36Sopenharmony_ci newg = rtnl_dereference(new->nh_grp); 197262306a36Sopenharmony_ci 197362306a36Sopenharmony_ci if (newg->hash_threshold != oldg->hash_threshold) { 197462306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with one of a different type."); 197562306a36Sopenharmony_ci return -EINVAL; 197662306a36Sopenharmony_ci } 197762306a36Sopenharmony_ci 197862306a36Sopenharmony_ci if (newg->hash_threshold) { 197962306a36Sopenharmony_ci err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new, 198062306a36Sopenharmony_ci extack); 198162306a36Sopenharmony_ci if (err) 198262306a36Sopenharmony_ci return err; 198362306a36Sopenharmony_ci } else if (newg->resilient) { 198462306a36Sopenharmony_ci new_res_table = rtnl_dereference(newg->res_table); 198562306a36Sopenharmony_ci old_res_table = rtnl_dereference(oldg->res_table); 198662306a36Sopenharmony_ci 198762306a36Sopenharmony_ci /* Accept if num_nh_buckets was not given, but if it was 198862306a36Sopenharmony_ci * given, demand that the value be correct. 198962306a36Sopenharmony_ci */ 199062306a36Sopenharmony_ci if (cfg->nh_grp_res_has_num_buckets && 199162306a36Sopenharmony_ci cfg->nh_grp_res_num_buckets != 199262306a36Sopenharmony_ci old_res_table->num_nh_buckets) { 199362306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Can not change number of buckets of a resilient nexthop group."); 199462306a36Sopenharmony_ci return -EINVAL; 199562306a36Sopenharmony_ci } 199662306a36Sopenharmony_ci 199762306a36Sopenharmony_ci /* Emit a pre-replace notification so that listeners could veto 199862306a36Sopenharmony_ci * a potentially unsupported configuration. Otherwise, 199962306a36Sopenharmony_ci * individual bucket replacement notifications would need to be 200062306a36Sopenharmony_ci * vetoed, which is something that should only happen if the 200162306a36Sopenharmony_ci * bucket is currently active. 200262306a36Sopenharmony_ci */ 200362306a36Sopenharmony_ci err = call_nexthop_res_table_notifiers(net, new, extack); 200462306a36Sopenharmony_ci if (err) 200562306a36Sopenharmony_ci return err; 200662306a36Sopenharmony_ci 200762306a36Sopenharmony_ci if (cfg->nh_grp_res_has_idle_timer) 200862306a36Sopenharmony_ci old_res_table->idle_timer = cfg->nh_grp_res_idle_timer; 200962306a36Sopenharmony_ci if (cfg->nh_grp_res_has_unbalanced_timer) 201062306a36Sopenharmony_ci old_res_table->unbalanced_timer = 201162306a36Sopenharmony_ci cfg->nh_grp_res_unbalanced_timer; 201262306a36Sopenharmony_ci 201362306a36Sopenharmony_ci replace_nexthop_grp_res(oldg, newg); 201462306a36Sopenharmony_ci 201562306a36Sopenharmony_ci tmp_table = new_res_table; 201662306a36Sopenharmony_ci rcu_assign_pointer(newg->res_table, old_res_table); 201762306a36Sopenharmony_ci rcu_assign_pointer(newg->spare->res_table, old_res_table); 201862306a36Sopenharmony_ci } 201962306a36Sopenharmony_ci 202062306a36Sopenharmony_ci /* update parents - used by nexthop code for cleanup */ 202162306a36Sopenharmony_ci for (i = 0; i < newg->num_nh; i++) 202262306a36Sopenharmony_ci newg->nh_entries[i].nh_parent = old; 202362306a36Sopenharmony_ci 202462306a36Sopenharmony_ci rcu_assign_pointer(old->nh_grp, newg); 202562306a36Sopenharmony_ci 202662306a36Sopenharmony_ci /* Make sure concurrent readers are not using 'oldg' anymore. */ 202762306a36Sopenharmony_ci synchronize_net(); 202862306a36Sopenharmony_ci 202962306a36Sopenharmony_ci if (newg->resilient) { 203062306a36Sopenharmony_ci rcu_assign_pointer(oldg->res_table, tmp_table); 203162306a36Sopenharmony_ci rcu_assign_pointer(oldg->spare->res_table, tmp_table); 203262306a36Sopenharmony_ci } 203362306a36Sopenharmony_ci 203462306a36Sopenharmony_ci for (i = 0; i < oldg->num_nh; i++) 203562306a36Sopenharmony_ci oldg->nh_entries[i].nh_parent = new; 203662306a36Sopenharmony_ci 203762306a36Sopenharmony_ci rcu_assign_pointer(new->nh_grp, oldg); 203862306a36Sopenharmony_ci 203962306a36Sopenharmony_ci return 0; 204062306a36Sopenharmony_ci} 204162306a36Sopenharmony_ci 204262306a36Sopenharmony_cistatic void nh_group_v4_update(struct nh_group *nhg) 204362306a36Sopenharmony_ci{ 204462306a36Sopenharmony_ci struct nh_grp_entry *nhges; 204562306a36Sopenharmony_ci bool has_v4 = false; 204662306a36Sopenharmony_ci int i; 204762306a36Sopenharmony_ci 204862306a36Sopenharmony_ci nhges = nhg->nh_entries; 204962306a36Sopenharmony_ci for (i = 0; i < nhg->num_nh; i++) { 205062306a36Sopenharmony_ci struct nh_info *nhi; 205162306a36Sopenharmony_ci 205262306a36Sopenharmony_ci nhi = rtnl_dereference(nhges[i].nh->nh_info); 205362306a36Sopenharmony_ci if (nhi->family == AF_INET) 205462306a36Sopenharmony_ci has_v4 = true; 205562306a36Sopenharmony_ci } 205662306a36Sopenharmony_ci nhg->has_v4 = has_v4; 205762306a36Sopenharmony_ci} 205862306a36Sopenharmony_ci 205962306a36Sopenharmony_cistatic int replace_nexthop_single_notify_res(struct net *net, 206062306a36Sopenharmony_ci struct nh_res_table *res_table, 206162306a36Sopenharmony_ci struct nexthop *old, 206262306a36Sopenharmony_ci struct nh_info *oldi, 206362306a36Sopenharmony_ci struct nh_info *newi, 206462306a36Sopenharmony_ci struct netlink_ext_ack *extack) 206562306a36Sopenharmony_ci{ 206662306a36Sopenharmony_ci u32 nhg_id = res_table->nhg_id; 206762306a36Sopenharmony_ci int err; 206862306a36Sopenharmony_ci u16 i; 206962306a36Sopenharmony_ci 207062306a36Sopenharmony_ci for (i = 0; i < res_table->num_nh_buckets; i++) { 207162306a36Sopenharmony_ci struct nh_res_bucket *bucket = &res_table->nh_buckets[i]; 207262306a36Sopenharmony_ci struct nh_grp_entry *nhge; 207362306a36Sopenharmony_ci 207462306a36Sopenharmony_ci nhge = rtnl_dereference(bucket->nh_entry); 207562306a36Sopenharmony_ci if (nhge->nh == old) { 207662306a36Sopenharmony_ci err = __call_nexthop_res_bucket_notifiers(net, nhg_id, 207762306a36Sopenharmony_ci i, true, 207862306a36Sopenharmony_ci oldi, newi, 207962306a36Sopenharmony_ci extack); 208062306a36Sopenharmony_ci if (err) 208162306a36Sopenharmony_ci goto err_notify; 208262306a36Sopenharmony_ci } 208362306a36Sopenharmony_ci } 208462306a36Sopenharmony_ci 208562306a36Sopenharmony_ci return 0; 208662306a36Sopenharmony_ci 208762306a36Sopenharmony_cierr_notify: 208862306a36Sopenharmony_ci while (i-- > 0) { 208962306a36Sopenharmony_ci struct nh_res_bucket *bucket = &res_table->nh_buckets[i]; 209062306a36Sopenharmony_ci struct nh_grp_entry *nhge; 209162306a36Sopenharmony_ci 209262306a36Sopenharmony_ci nhge = rtnl_dereference(bucket->nh_entry); 209362306a36Sopenharmony_ci if (nhge->nh == old) 209462306a36Sopenharmony_ci __call_nexthop_res_bucket_notifiers(net, nhg_id, i, 209562306a36Sopenharmony_ci true, newi, oldi, 209662306a36Sopenharmony_ci extack); 209762306a36Sopenharmony_ci } 209862306a36Sopenharmony_ci return err; 209962306a36Sopenharmony_ci} 210062306a36Sopenharmony_ci 210162306a36Sopenharmony_cistatic int replace_nexthop_single_notify(struct net *net, 210262306a36Sopenharmony_ci struct nexthop *group_nh, 210362306a36Sopenharmony_ci struct nexthop *old, 210462306a36Sopenharmony_ci struct nh_info *oldi, 210562306a36Sopenharmony_ci struct nh_info *newi, 210662306a36Sopenharmony_ci struct netlink_ext_ack *extack) 210762306a36Sopenharmony_ci{ 210862306a36Sopenharmony_ci struct nh_group *nhg = rtnl_dereference(group_nh->nh_grp); 210962306a36Sopenharmony_ci struct nh_res_table *res_table; 211062306a36Sopenharmony_ci 211162306a36Sopenharmony_ci if (nhg->hash_threshold) { 211262306a36Sopenharmony_ci return call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, 211362306a36Sopenharmony_ci group_nh, extack); 211462306a36Sopenharmony_ci } else if (nhg->resilient) { 211562306a36Sopenharmony_ci res_table = rtnl_dereference(nhg->res_table); 211662306a36Sopenharmony_ci return replace_nexthop_single_notify_res(net, res_table, 211762306a36Sopenharmony_ci old, oldi, newi, 211862306a36Sopenharmony_ci extack); 211962306a36Sopenharmony_ci } 212062306a36Sopenharmony_ci 212162306a36Sopenharmony_ci return -EINVAL; 212262306a36Sopenharmony_ci} 212362306a36Sopenharmony_ci 212462306a36Sopenharmony_cistatic int replace_nexthop_single(struct net *net, struct nexthop *old, 212562306a36Sopenharmony_ci struct nexthop *new, 212662306a36Sopenharmony_ci struct netlink_ext_ack *extack) 212762306a36Sopenharmony_ci{ 212862306a36Sopenharmony_ci u8 old_protocol, old_nh_flags; 212962306a36Sopenharmony_ci struct nh_info *oldi, *newi; 213062306a36Sopenharmony_ci struct nh_grp_entry *nhge; 213162306a36Sopenharmony_ci int err; 213262306a36Sopenharmony_ci 213362306a36Sopenharmony_ci if (new->is_group) { 213462306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Can not replace a nexthop with a nexthop group."); 213562306a36Sopenharmony_ci return -EINVAL; 213662306a36Sopenharmony_ci } 213762306a36Sopenharmony_ci 213862306a36Sopenharmony_ci err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new, extack); 213962306a36Sopenharmony_ci if (err) 214062306a36Sopenharmony_ci return err; 214162306a36Sopenharmony_ci 214262306a36Sopenharmony_ci /* Hardware flags were set on 'old' as 'new' is not in the red-black 214362306a36Sopenharmony_ci * tree. Therefore, inherit the flags from 'old' to 'new'. 214462306a36Sopenharmony_ci */ 214562306a36Sopenharmony_ci new->nh_flags |= old->nh_flags & (RTNH_F_OFFLOAD | RTNH_F_TRAP); 214662306a36Sopenharmony_ci 214762306a36Sopenharmony_ci oldi = rtnl_dereference(old->nh_info); 214862306a36Sopenharmony_ci newi = rtnl_dereference(new->nh_info); 214962306a36Sopenharmony_ci 215062306a36Sopenharmony_ci newi->nh_parent = old; 215162306a36Sopenharmony_ci oldi->nh_parent = new; 215262306a36Sopenharmony_ci 215362306a36Sopenharmony_ci old_protocol = old->protocol; 215462306a36Sopenharmony_ci old_nh_flags = old->nh_flags; 215562306a36Sopenharmony_ci 215662306a36Sopenharmony_ci old->protocol = new->protocol; 215762306a36Sopenharmony_ci old->nh_flags = new->nh_flags; 215862306a36Sopenharmony_ci 215962306a36Sopenharmony_ci rcu_assign_pointer(old->nh_info, newi); 216062306a36Sopenharmony_ci rcu_assign_pointer(new->nh_info, oldi); 216162306a36Sopenharmony_ci 216262306a36Sopenharmony_ci /* Send a replace notification for all the groups using the nexthop. */ 216362306a36Sopenharmony_ci list_for_each_entry(nhge, &old->grp_list, nh_list) { 216462306a36Sopenharmony_ci struct nexthop *nhp = nhge->nh_parent; 216562306a36Sopenharmony_ci 216662306a36Sopenharmony_ci err = replace_nexthop_single_notify(net, nhp, old, oldi, newi, 216762306a36Sopenharmony_ci extack); 216862306a36Sopenharmony_ci if (err) 216962306a36Sopenharmony_ci goto err_notify; 217062306a36Sopenharmony_ci } 217162306a36Sopenharmony_ci 217262306a36Sopenharmony_ci /* When replacing an IPv4 nexthop with an IPv6 nexthop, potentially 217362306a36Sopenharmony_ci * update IPv4 indication in all the groups using the nexthop. 217462306a36Sopenharmony_ci */ 217562306a36Sopenharmony_ci if (oldi->family == AF_INET && newi->family == AF_INET6) { 217662306a36Sopenharmony_ci list_for_each_entry(nhge, &old->grp_list, nh_list) { 217762306a36Sopenharmony_ci struct nexthop *nhp = nhge->nh_parent; 217862306a36Sopenharmony_ci struct nh_group *nhg; 217962306a36Sopenharmony_ci 218062306a36Sopenharmony_ci nhg = rtnl_dereference(nhp->nh_grp); 218162306a36Sopenharmony_ci nh_group_v4_update(nhg); 218262306a36Sopenharmony_ci } 218362306a36Sopenharmony_ci } 218462306a36Sopenharmony_ci 218562306a36Sopenharmony_ci return 0; 218662306a36Sopenharmony_ci 218762306a36Sopenharmony_cierr_notify: 218862306a36Sopenharmony_ci rcu_assign_pointer(new->nh_info, newi); 218962306a36Sopenharmony_ci rcu_assign_pointer(old->nh_info, oldi); 219062306a36Sopenharmony_ci old->nh_flags = old_nh_flags; 219162306a36Sopenharmony_ci old->protocol = old_protocol; 219262306a36Sopenharmony_ci oldi->nh_parent = old; 219362306a36Sopenharmony_ci newi->nh_parent = new; 219462306a36Sopenharmony_ci list_for_each_entry_continue_reverse(nhge, &old->grp_list, nh_list) { 219562306a36Sopenharmony_ci struct nexthop *nhp = nhge->nh_parent; 219662306a36Sopenharmony_ci 219762306a36Sopenharmony_ci replace_nexthop_single_notify(net, nhp, old, newi, oldi, NULL); 219862306a36Sopenharmony_ci } 219962306a36Sopenharmony_ci call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, old, extack); 220062306a36Sopenharmony_ci return err; 220162306a36Sopenharmony_ci} 220262306a36Sopenharmony_ci 220362306a36Sopenharmony_cistatic void __nexthop_replace_notify(struct net *net, struct nexthop *nh, 220462306a36Sopenharmony_ci struct nl_info *info) 220562306a36Sopenharmony_ci{ 220662306a36Sopenharmony_ci struct fib6_info *f6i; 220762306a36Sopenharmony_ci 220862306a36Sopenharmony_ci if (!list_empty(&nh->fi_list)) { 220962306a36Sopenharmony_ci struct fib_info *fi; 221062306a36Sopenharmony_ci 221162306a36Sopenharmony_ci /* expectation is a few fib_info per nexthop and then 221262306a36Sopenharmony_ci * a lot of routes per fib_info. So mark the fib_info 221362306a36Sopenharmony_ci * and then walk the fib tables once 221462306a36Sopenharmony_ci */ 221562306a36Sopenharmony_ci list_for_each_entry(fi, &nh->fi_list, nh_list) 221662306a36Sopenharmony_ci fi->nh_updated = true; 221762306a36Sopenharmony_ci 221862306a36Sopenharmony_ci fib_info_notify_update(net, info); 221962306a36Sopenharmony_ci 222062306a36Sopenharmony_ci list_for_each_entry(fi, &nh->fi_list, nh_list) 222162306a36Sopenharmony_ci fi->nh_updated = false; 222262306a36Sopenharmony_ci } 222362306a36Sopenharmony_ci 222462306a36Sopenharmony_ci list_for_each_entry(f6i, &nh->f6i_list, nh_list) 222562306a36Sopenharmony_ci ipv6_stub->fib6_rt_update(net, f6i, info); 222662306a36Sopenharmony_ci} 222762306a36Sopenharmony_ci 222862306a36Sopenharmony_ci/* send RTM_NEWROUTE with REPLACE flag set for all FIB entries 222962306a36Sopenharmony_ci * linked to this nexthop and for all groups that the nexthop 223062306a36Sopenharmony_ci * is a member of 223162306a36Sopenharmony_ci */ 223262306a36Sopenharmony_cistatic void nexthop_replace_notify(struct net *net, struct nexthop *nh, 223362306a36Sopenharmony_ci struct nl_info *info) 223462306a36Sopenharmony_ci{ 223562306a36Sopenharmony_ci struct nh_grp_entry *nhge; 223662306a36Sopenharmony_ci 223762306a36Sopenharmony_ci __nexthop_replace_notify(net, nh, info); 223862306a36Sopenharmony_ci 223962306a36Sopenharmony_ci list_for_each_entry(nhge, &nh->grp_list, nh_list) 224062306a36Sopenharmony_ci __nexthop_replace_notify(net, nhge->nh_parent, info); 224162306a36Sopenharmony_ci} 224262306a36Sopenharmony_ci 224362306a36Sopenharmony_cistatic int replace_nexthop(struct net *net, struct nexthop *old, 224462306a36Sopenharmony_ci struct nexthop *new, const struct nh_config *cfg, 224562306a36Sopenharmony_ci struct netlink_ext_ack *extack) 224662306a36Sopenharmony_ci{ 224762306a36Sopenharmony_ci bool new_is_reject = false; 224862306a36Sopenharmony_ci struct nh_grp_entry *nhge; 224962306a36Sopenharmony_ci int err; 225062306a36Sopenharmony_ci 225162306a36Sopenharmony_ci /* check that existing FIB entries are ok with the 225262306a36Sopenharmony_ci * new nexthop definition 225362306a36Sopenharmony_ci */ 225462306a36Sopenharmony_ci err = fib_check_nh_list(old, new, extack); 225562306a36Sopenharmony_ci if (err) 225662306a36Sopenharmony_ci return err; 225762306a36Sopenharmony_ci 225862306a36Sopenharmony_ci err = fib6_check_nh_list(old, new, extack); 225962306a36Sopenharmony_ci if (err) 226062306a36Sopenharmony_ci return err; 226162306a36Sopenharmony_ci 226262306a36Sopenharmony_ci if (!new->is_group) { 226362306a36Sopenharmony_ci struct nh_info *nhi = rtnl_dereference(new->nh_info); 226462306a36Sopenharmony_ci 226562306a36Sopenharmony_ci new_is_reject = nhi->reject_nh; 226662306a36Sopenharmony_ci } 226762306a36Sopenharmony_ci 226862306a36Sopenharmony_ci list_for_each_entry(nhge, &old->grp_list, nh_list) { 226962306a36Sopenharmony_ci /* if new nexthop is a blackhole, any groups using this 227062306a36Sopenharmony_ci * nexthop cannot have more than 1 path 227162306a36Sopenharmony_ci */ 227262306a36Sopenharmony_ci if (new_is_reject && 227362306a36Sopenharmony_ci nexthop_num_path(nhge->nh_parent) > 1) { 227462306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Blackhole nexthop can not be a member of a group with more than one path"); 227562306a36Sopenharmony_ci return -EINVAL; 227662306a36Sopenharmony_ci } 227762306a36Sopenharmony_ci 227862306a36Sopenharmony_ci err = fib_check_nh_list(nhge->nh_parent, new, extack); 227962306a36Sopenharmony_ci if (err) 228062306a36Sopenharmony_ci return err; 228162306a36Sopenharmony_ci 228262306a36Sopenharmony_ci err = fib6_check_nh_list(nhge->nh_parent, new, extack); 228362306a36Sopenharmony_ci if (err) 228462306a36Sopenharmony_ci return err; 228562306a36Sopenharmony_ci } 228662306a36Sopenharmony_ci 228762306a36Sopenharmony_ci if (old->is_group) 228862306a36Sopenharmony_ci err = replace_nexthop_grp(net, old, new, cfg, extack); 228962306a36Sopenharmony_ci else 229062306a36Sopenharmony_ci err = replace_nexthop_single(net, old, new, extack); 229162306a36Sopenharmony_ci 229262306a36Sopenharmony_ci if (!err) { 229362306a36Sopenharmony_ci nh_rt_cache_flush(net, old, new); 229462306a36Sopenharmony_ci 229562306a36Sopenharmony_ci __remove_nexthop(net, new, NULL); 229662306a36Sopenharmony_ci nexthop_put(new); 229762306a36Sopenharmony_ci } 229862306a36Sopenharmony_ci 229962306a36Sopenharmony_ci return err; 230062306a36Sopenharmony_ci} 230162306a36Sopenharmony_ci 230262306a36Sopenharmony_ci/* called with rtnl_lock held */ 230362306a36Sopenharmony_cistatic int insert_nexthop(struct net *net, struct nexthop *new_nh, 230462306a36Sopenharmony_ci struct nh_config *cfg, struct netlink_ext_ack *extack) 230562306a36Sopenharmony_ci{ 230662306a36Sopenharmony_ci struct rb_node **pp, *parent = NULL, *next; 230762306a36Sopenharmony_ci struct rb_root *root = &net->nexthop.rb_root; 230862306a36Sopenharmony_ci bool replace = !!(cfg->nlflags & NLM_F_REPLACE); 230962306a36Sopenharmony_ci bool create = !!(cfg->nlflags & NLM_F_CREATE); 231062306a36Sopenharmony_ci u32 new_id = new_nh->id; 231162306a36Sopenharmony_ci int replace_notify = 0; 231262306a36Sopenharmony_ci int rc = -EEXIST; 231362306a36Sopenharmony_ci 231462306a36Sopenharmony_ci pp = &root->rb_node; 231562306a36Sopenharmony_ci while (1) { 231662306a36Sopenharmony_ci struct nexthop *nh; 231762306a36Sopenharmony_ci 231862306a36Sopenharmony_ci next = *pp; 231962306a36Sopenharmony_ci if (!next) 232062306a36Sopenharmony_ci break; 232162306a36Sopenharmony_ci 232262306a36Sopenharmony_ci parent = next; 232362306a36Sopenharmony_ci 232462306a36Sopenharmony_ci nh = rb_entry(parent, struct nexthop, rb_node); 232562306a36Sopenharmony_ci if (new_id < nh->id) { 232662306a36Sopenharmony_ci pp = &next->rb_left; 232762306a36Sopenharmony_ci } else if (new_id > nh->id) { 232862306a36Sopenharmony_ci pp = &next->rb_right; 232962306a36Sopenharmony_ci } else if (replace) { 233062306a36Sopenharmony_ci rc = replace_nexthop(net, nh, new_nh, cfg, extack); 233162306a36Sopenharmony_ci if (!rc) { 233262306a36Sopenharmony_ci new_nh = nh; /* send notification with old nh */ 233362306a36Sopenharmony_ci replace_notify = 1; 233462306a36Sopenharmony_ci } 233562306a36Sopenharmony_ci goto out; 233662306a36Sopenharmony_ci } else { 233762306a36Sopenharmony_ci /* id already exists and not a replace */ 233862306a36Sopenharmony_ci goto out; 233962306a36Sopenharmony_ci } 234062306a36Sopenharmony_ci } 234162306a36Sopenharmony_ci 234262306a36Sopenharmony_ci if (replace && !create) { 234362306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Replace specified without create and no entry exists"); 234462306a36Sopenharmony_ci rc = -ENOENT; 234562306a36Sopenharmony_ci goto out; 234662306a36Sopenharmony_ci } 234762306a36Sopenharmony_ci 234862306a36Sopenharmony_ci if (new_nh->is_group) { 234962306a36Sopenharmony_ci struct nh_group *nhg = rtnl_dereference(new_nh->nh_grp); 235062306a36Sopenharmony_ci struct nh_res_table *res_table; 235162306a36Sopenharmony_ci 235262306a36Sopenharmony_ci if (nhg->resilient) { 235362306a36Sopenharmony_ci res_table = rtnl_dereference(nhg->res_table); 235462306a36Sopenharmony_ci 235562306a36Sopenharmony_ci /* Not passing the number of buckets is OK when 235662306a36Sopenharmony_ci * replacing, but not when creating a new group. 235762306a36Sopenharmony_ci */ 235862306a36Sopenharmony_ci if (!cfg->nh_grp_res_has_num_buckets) { 235962306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Number of buckets not specified for nexthop group insertion"); 236062306a36Sopenharmony_ci rc = -EINVAL; 236162306a36Sopenharmony_ci goto out; 236262306a36Sopenharmony_ci } 236362306a36Sopenharmony_ci 236462306a36Sopenharmony_ci nh_res_group_rebalance(nhg, res_table); 236562306a36Sopenharmony_ci 236662306a36Sopenharmony_ci /* Do not send bucket notifications, we do full 236762306a36Sopenharmony_ci * notification below. 236862306a36Sopenharmony_ci */ 236962306a36Sopenharmony_ci nh_res_table_upkeep(res_table, false, false); 237062306a36Sopenharmony_ci } 237162306a36Sopenharmony_ci } 237262306a36Sopenharmony_ci 237362306a36Sopenharmony_ci rb_link_node_rcu(&new_nh->rb_node, parent, pp); 237462306a36Sopenharmony_ci rb_insert_color(&new_nh->rb_node, root); 237562306a36Sopenharmony_ci 237662306a36Sopenharmony_ci /* The initial insertion is a full notification for hash-threshold as 237762306a36Sopenharmony_ci * well as resilient groups. 237862306a36Sopenharmony_ci */ 237962306a36Sopenharmony_ci rc = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new_nh, extack); 238062306a36Sopenharmony_ci if (rc) 238162306a36Sopenharmony_ci rb_erase(&new_nh->rb_node, &net->nexthop.rb_root); 238262306a36Sopenharmony_ci 238362306a36Sopenharmony_ciout: 238462306a36Sopenharmony_ci if (!rc) { 238562306a36Sopenharmony_ci nh_base_seq_inc(net); 238662306a36Sopenharmony_ci nexthop_notify(RTM_NEWNEXTHOP, new_nh, &cfg->nlinfo); 238762306a36Sopenharmony_ci if (replace_notify && 238862306a36Sopenharmony_ci READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode)) 238962306a36Sopenharmony_ci nexthop_replace_notify(net, new_nh, &cfg->nlinfo); 239062306a36Sopenharmony_ci } 239162306a36Sopenharmony_ci 239262306a36Sopenharmony_ci return rc; 239362306a36Sopenharmony_ci} 239462306a36Sopenharmony_ci 239562306a36Sopenharmony_ci/* rtnl */ 239662306a36Sopenharmony_ci/* remove all nexthops tied to a device being deleted */ 239762306a36Sopenharmony_cistatic void nexthop_flush_dev(struct net_device *dev, unsigned long event) 239862306a36Sopenharmony_ci{ 239962306a36Sopenharmony_ci unsigned int hash = nh_dev_hashfn(dev->ifindex); 240062306a36Sopenharmony_ci struct net *net = dev_net(dev); 240162306a36Sopenharmony_ci struct hlist_head *head = &net->nexthop.devhash[hash]; 240262306a36Sopenharmony_ci struct hlist_node *n; 240362306a36Sopenharmony_ci struct nh_info *nhi; 240462306a36Sopenharmony_ci 240562306a36Sopenharmony_ci hlist_for_each_entry_safe(nhi, n, head, dev_hash) { 240662306a36Sopenharmony_ci if (nhi->fib_nhc.nhc_dev != dev) 240762306a36Sopenharmony_ci continue; 240862306a36Sopenharmony_ci 240962306a36Sopenharmony_ci if (nhi->reject_nh && 241062306a36Sopenharmony_ci (event == NETDEV_DOWN || event == NETDEV_CHANGE)) 241162306a36Sopenharmony_ci continue; 241262306a36Sopenharmony_ci 241362306a36Sopenharmony_ci remove_nexthop(net, nhi->nh_parent, NULL); 241462306a36Sopenharmony_ci } 241562306a36Sopenharmony_ci} 241662306a36Sopenharmony_ci 241762306a36Sopenharmony_ci/* rtnl; called when net namespace is deleted */ 241862306a36Sopenharmony_cistatic void flush_all_nexthops(struct net *net) 241962306a36Sopenharmony_ci{ 242062306a36Sopenharmony_ci struct rb_root *root = &net->nexthop.rb_root; 242162306a36Sopenharmony_ci struct rb_node *node; 242262306a36Sopenharmony_ci struct nexthop *nh; 242362306a36Sopenharmony_ci 242462306a36Sopenharmony_ci while ((node = rb_first(root))) { 242562306a36Sopenharmony_ci nh = rb_entry(node, struct nexthop, rb_node); 242662306a36Sopenharmony_ci remove_nexthop(net, nh, NULL); 242762306a36Sopenharmony_ci cond_resched(); 242862306a36Sopenharmony_ci } 242962306a36Sopenharmony_ci} 243062306a36Sopenharmony_ci 243162306a36Sopenharmony_cistatic struct nexthop *nexthop_create_group(struct net *net, 243262306a36Sopenharmony_ci struct nh_config *cfg) 243362306a36Sopenharmony_ci{ 243462306a36Sopenharmony_ci struct nlattr *grps_attr = cfg->nh_grp; 243562306a36Sopenharmony_ci struct nexthop_grp *entry = nla_data(grps_attr); 243662306a36Sopenharmony_ci u16 num_nh = nla_len(grps_attr) / sizeof(*entry); 243762306a36Sopenharmony_ci struct nh_group *nhg; 243862306a36Sopenharmony_ci struct nexthop *nh; 243962306a36Sopenharmony_ci int err; 244062306a36Sopenharmony_ci int i; 244162306a36Sopenharmony_ci 244262306a36Sopenharmony_ci if (WARN_ON(!num_nh)) 244362306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 244462306a36Sopenharmony_ci 244562306a36Sopenharmony_ci nh = nexthop_alloc(); 244662306a36Sopenharmony_ci if (!nh) 244762306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 244862306a36Sopenharmony_ci 244962306a36Sopenharmony_ci nh->is_group = 1; 245062306a36Sopenharmony_ci 245162306a36Sopenharmony_ci nhg = nexthop_grp_alloc(num_nh); 245262306a36Sopenharmony_ci if (!nhg) { 245362306a36Sopenharmony_ci kfree(nh); 245462306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 245562306a36Sopenharmony_ci } 245662306a36Sopenharmony_ci 245762306a36Sopenharmony_ci /* spare group used for removals */ 245862306a36Sopenharmony_ci nhg->spare = nexthop_grp_alloc(num_nh); 245962306a36Sopenharmony_ci if (!nhg->spare) { 246062306a36Sopenharmony_ci kfree(nhg); 246162306a36Sopenharmony_ci kfree(nh); 246262306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 246362306a36Sopenharmony_ci } 246462306a36Sopenharmony_ci nhg->spare->spare = nhg; 246562306a36Sopenharmony_ci 246662306a36Sopenharmony_ci for (i = 0; i < nhg->num_nh; ++i) { 246762306a36Sopenharmony_ci struct nexthop *nhe; 246862306a36Sopenharmony_ci struct nh_info *nhi; 246962306a36Sopenharmony_ci 247062306a36Sopenharmony_ci nhe = nexthop_find_by_id(net, entry[i].id); 247162306a36Sopenharmony_ci if (!nexthop_get(nhe)) { 247262306a36Sopenharmony_ci err = -ENOENT; 247362306a36Sopenharmony_ci goto out_no_nh; 247462306a36Sopenharmony_ci } 247562306a36Sopenharmony_ci 247662306a36Sopenharmony_ci nhi = rtnl_dereference(nhe->nh_info); 247762306a36Sopenharmony_ci if (nhi->family == AF_INET) 247862306a36Sopenharmony_ci nhg->has_v4 = true; 247962306a36Sopenharmony_ci 248062306a36Sopenharmony_ci nhg->nh_entries[i].nh = nhe; 248162306a36Sopenharmony_ci nhg->nh_entries[i].weight = entry[i].weight + 1; 248262306a36Sopenharmony_ci list_add(&nhg->nh_entries[i].nh_list, &nhe->grp_list); 248362306a36Sopenharmony_ci nhg->nh_entries[i].nh_parent = nh; 248462306a36Sopenharmony_ci } 248562306a36Sopenharmony_ci 248662306a36Sopenharmony_ci if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_MPATH) { 248762306a36Sopenharmony_ci nhg->hash_threshold = 1; 248862306a36Sopenharmony_ci nhg->is_multipath = true; 248962306a36Sopenharmony_ci } else if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_RES) { 249062306a36Sopenharmony_ci struct nh_res_table *res_table; 249162306a36Sopenharmony_ci 249262306a36Sopenharmony_ci res_table = nexthop_res_table_alloc(net, cfg->nh_id, cfg); 249362306a36Sopenharmony_ci if (!res_table) { 249462306a36Sopenharmony_ci err = -ENOMEM; 249562306a36Sopenharmony_ci goto out_no_nh; 249662306a36Sopenharmony_ci } 249762306a36Sopenharmony_ci 249862306a36Sopenharmony_ci rcu_assign_pointer(nhg->spare->res_table, res_table); 249962306a36Sopenharmony_ci rcu_assign_pointer(nhg->res_table, res_table); 250062306a36Sopenharmony_ci nhg->resilient = true; 250162306a36Sopenharmony_ci nhg->is_multipath = true; 250262306a36Sopenharmony_ci } 250362306a36Sopenharmony_ci 250462306a36Sopenharmony_ci WARN_ON_ONCE(nhg->hash_threshold + nhg->resilient != 1); 250562306a36Sopenharmony_ci 250662306a36Sopenharmony_ci if (nhg->hash_threshold) 250762306a36Sopenharmony_ci nh_hthr_group_rebalance(nhg); 250862306a36Sopenharmony_ci 250962306a36Sopenharmony_ci if (cfg->nh_fdb) 251062306a36Sopenharmony_ci nhg->fdb_nh = 1; 251162306a36Sopenharmony_ci 251262306a36Sopenharmony_ci rcu_assign_pointer(nh->nh_grp, nhg); 251362306a36Sopenharmony_ci 251462306a36Sopenharmony_ci return nh; 251562306a36Sopenharmony_ci 251662306a36Sopenharmony_ciout_no_nh: 251762306a36Sopenharmony_ci for (i--; i >= 0; --i) { 251862306a36Sopenharmony_ci list_del(&nhg->nh_entries[i].nh_list); 251962306a36Sopenharmony_ci nexthop_put(nhg->nh_entries[i].nh); 252062306a36Sopenharmony_ci } 252162306a36Sopenharmony_ci 252262306a36Sopenharmony_ci kfree(nhg->spare); 252362306a36Sopenharmony_ci kfree(nhg); 252462306a36Sopenharmony_ci kfree(nh); 252562306a36Sopenharmony_ci 252662306a36Sopenharmony_ci return ERR_PTR(err); 252762306a36Sopenharmony_ci} 252862306a36Sopenharmony_ci 252962306a36Sopenharmony_cistatic int nh_create_ipv4(struct net *net, struct nexthop *nh, 253062306a36Sopenharmony_ci struct nh_info *nhi, struct nh_config *cfg, 253162306a36Sopenharmony_ci struct netlink_ext_ack *extack) 253262306a36Sopenharmony_ci{ 253362306a36Sopenharmony_ci struct fib_nh *fib_nh = &nhi->fib_nh; 253462306a36Sopenharmony_ci struct fib_config fib_cfg = { 253562306a36Sopenharmony_ci .fc_oif = cfg->nh_ifindex, 253662306a36Sopenharmony_ci .fc_gw4 = cfg->gw.ipv4, 253762306a36Sopenharmony_ci .fc_gw_family = cfg->gw.ipv4 ? AF_INET : 0, 253862306a36Sopenharmony_ci .fc_flags = cfg->nh_flags, 253962306a36Sopenharmony_ci .fc_nlinfo = cfg->nlinfo, 254062306a36Sopenharmony_ci .fc_encap = cfg->nh_encap, 254162306a36Sopenharmony_ci .fc_encap_type = cfg->nh_encap_type, 254262306a36Sopenharmony_ci }; 254362306a36Sopenharmony_ci u32 tb_id = (cfg->dev ? l3mdev_fib_table(cfg->dev) : RT_TABLE_MAIN); 254462306a36Sopenharmony_ci int err; 254562306a36Sopenharmony_ci 254662306a36Sopenharmony_ci err = fib_nh_init(net, fib_nh, &fib_cfg, 1, extack); 254762306a36Sopenharmony_ci if (err) { 254862306a36Sopenharmony_ci fib_nh_release(net, fib_nh); 254962306a36Sopenharmony_ci goto out; 255062306a36Sopenharmony_ci } 255162306a36Sopenharmony_ci 255262306a36Sopenharmony_ci if (nhi->fdb_nh) 255362306a36Sopenharmony_ci goto out; 255462306a36Sopenharmony_ci 255562306a36Sopenharmony_ci /* sets nh_dev if successful */ 255662306a36Sopenharmony_ci err = fib_check_nh(net, fib_nh, tb_id, 0, extack); 255762306a36Sopenharmony_ci if (!err) { 255862306a36Sopenharmony_ci nh->nh_flags = fib_nh->fib_nh_flags; 255962306a36Sopenharmony_ci fib_info_update_nhc_saddr(net, &fib_nh->nh_common, 256062306a36Sopenharmony_ci !fib_nh->fib_nh_scope ? 0 : fib_nh->fib_nh_scope - 1); 256162306a36Sopenharmony_ci } else { 256262306a36Sopenharmony_ci fib_nh_release(net, fib_nh); 256362306a36Sopenharmony_ci } 256462306a36Sopenharmony_ciout: 256562306a36Sopenharmony_ci return err; 256662306a36Sopenharmony_ci} 256762306a36Sopenharmony_ci 256862306a36Sopenharmony_cistatic int nh_create_ipv6(struct net *net, struct nexthop *nh, 256962306a36Sopenharmony_ci struct nh_info *nhi, struct nh_config *cfg, 257062306a36Sopenharmony_ci struct netlink_ext_ack *extack) 257162306a36Sopenharmony_ci{ 257262306a36Sopenharmony_ci struct fib6_nh *fib6_nh = &nhi->fib6_nh; 257362306a36Sopenharmony_ci struct fib6_config fib6_cfg = { 257462306a36Sopenharmony_ci .fc_table = l3mdev_fib_table(cfg->dev), 257562306a36Sopenharmony_ci .fc_ifindex = cfg->nh_ifindex, 257662306a36Sopenharmony_ci .fc_gateway = cfg->gw.ipv6, 257762306a36Sopenharmony_ci .fc_flags = cfg->nh_flags, 257862306a36Sopenharmony_ci .fc_nlinfo = cfg->nlinfo, 257962306a36Sopenharmony_ci .fc_encap = cfg->nh_encap, 258062306a36Sopenharmony_ci .fc_encap_type = cfg->nh_encap_type, 258162306a36Sopenharmony_ci .fc_is_fdb = cfg->nh_fdb, 258262306a36Sopenharmony_ci }; 258362306a36Sopenharmony_ci int err; 258462306a36Sopenharmony_ci 258562306a36Sopenharmony_ci if (!ipv6_addr_any(&cfg->gw.ipv6)) 258662306a36Sopenharmony_ci fib6_cfg.fc_flags |= RTF_GATEWAY; 258762306a36Sopenharmony_ci 258862306a36Sopenharmony_ci /* sets nh_dev if successful */ 258962306a36Sopenharmony_ci err = ipv6_stub->fib6_nh_init(net, fib6_nh, &fib6_cfg, GFP_KERNEL, 259062306a36Sopenharmony_ci extack); 259162306a36Sopenharmony_ci if (err) { 259262306a36Sopenharmony_ci /* IPv6 is not enabled, don't call fib6_nh_release */ 259362306a36Sopenharmony_ci if (err == -EAFNOSUPPORT) 259462306a36Sopenharmony_ci goto out; 259562306a36Sopenharmony_ci ipv6_stub->fib6_nh_release(fib6_nh); 259662306a36Sopenharmony_ci } else { 259762306a36Sopenharmony_ci nh->nh_flags = fib6_nh->fib_nh_flags; 259862306a36Sopenharmony_ci } 259962306a36Sopenharmony_ciout: 260062306a36Sopenharmony_ci return err; 260162306a36Sopenharmony_ci} 260262306a36Sopenharmony_ci 260362306a36Sopenharmony_cistatic struct nexthop *nexthop_create(struct net *net, struct nh_config *cfg, 260462306a36Sopenharmony_ci struct netlink_ext_ack *extack) 260562306a36Sopenharmony_ci{ 260662306a36Sopenharmony_ci struct nh_info *nhi; 260762306a36Sopenharmony_ci struct nexthop *nh; 260862306a36Sopenharmony_ci int err = 0; 260962306a36Sopenharmony_ci 261062306a36Sopenharmony_ci nh = nexthop_alloc(); 261162306a36Sopenharmony_ci if (!nh) 261262306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 261362306a36Sopenharmony_ci 261462306a36Sopenharmony_ci nhi = kzalloc(sizeof(*nhi), GFP_KERNEL); 261562306a36Sopenharmony_ci if (!nhi) { 261662306a36Sopenharmony_ci kfree(nh); 261762306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 261862306a36Sopenharmony_ci } 261962306a36Sopenharmony_ci 262062306a36Sopenharmony_ci nh->nh_flags = cfg->nh_flags; 262162306a36Sopenharmony_ci nh->net = net; 262262306a36Sopenharmony_ci 262362306a36Sopenharmony_ci nhi->nh_parent = nh; 262462306a36Sopenharmony_ci nhi->family = cfg->nh_family; 262562306a36Sopenharmony_ci nhi->fib_nhc.nhc_scope = RT_SCOPE_LINK; 262662306a36Sopenharmony_ci 262762306a36Sopenharmony_ci if (cfg->nh_fdb) 262862306a36Sopenharmony_ci nhi->fdb_nh = 1; 262962306a36Sopenharmony_ci 263062306a36Sopenharmony_ci if (cfg->nh_blackhole) { 263162306a36Sopenharmony_ci nhi->reject_nh = 1; 263262306a36Sopenharmony_ci cfg->nh_ifindex = net->loopback_dev->ifindex; 263362306a36Sopenharmony_ci } 263462306a36Sopenharmony_ci 263562306a36Sopenharmony_ci switch (cfg->nh_family) { 263662306a36Sopenharmony_ci case AF_INET: 263762306a36Sopenharmony_ci err = nh_create_ipv4(net, nh, nhi, cfg, extack); 263862306a36Sopenharmony_ci break; 263962306a36Sopenharmony_ci case AF_INET6: 264062306a36Sopenharmony_ci err = nh_create_ipv6(net, nh, nhi, cfg, extack); 264162306a36Sopenharmony_ci break; 264262306a36Sopenharmony_ci } 264362306a36Sopenharmony_ci 264462306a36Sopenharmony_ci if (err) { 264562306a36Sopenharmony_ci kfree(nhi); 264662306a36Sopenharmony_ci kfree(nh); 264762306a36Sopenharmony_ci return ERR_PTR(err); 264862306a36Sopenharmony_ci } 264962306a36Sopenharmony_ci 265062306a36Sopenharmony_ci /* add the entry to the device based hash */ 265162306a36Sopenharmony_ci if (!nhi->fdb_nh) 265262306a36Sopenharmony_ci nexthop_devhash_add(net, nhi); 265362306a36Sopenharmony_ci 265462306a36Sopenharmony_ci rcu_assign_pointer(nh->nh_info, nhi); 265562306a36Sopenharmony_ci 265662306a36Sopenharmony_ci return nh; 265762306a36Sopenharmony_ci} 265862306a36Sopenharmony_ci 265962306a36Sopenharmony_ci/* called with rtnl lock held */ 266062306a36Sopenharmony_cistatic struct nexthop *nexthop_add(struct net *net, struct nh_config *cfg, 266162306a36Sopenharmony_ci struct netlink_ext_ack *extack) 266262306a36Sopenharmony_ci{ 266362306a36Sopenharmony_ci struct nexthop *nh; 266462306a36Sopenharmony_ci int err; 266562306a36Sopenharmony_ci 266662306a36Sopenharmony_ci if (cfg->nlflags & NLM_F_REPLACE && !cfg->nh_id) { 266762306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Replace requires nexthop id"); 266862306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 266962306a36Sopenharmony_ci } 267062306a36Sopenharmony_ci 267162306a36Sopenharmony_ci if (!cfg->nh_id) { 267262306a36Sopenharmony_ci cfg->nh_id = nh_find_unused_id(net); 267362306a36Sopenharmony_ci if (!cfg->nh_id) { 267462306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "No unused id"); 267562306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 267662306a36Sopenharmony_ci } 267762306a36Sopenharmony_ci } 267862306a36Sopenharmony_ci 267962306a36Sopenharmony_ci if (cfg->nh_grp) 268062306a36Sopenharmony_ci nh = nexthop_create_group(net, cfg); 268162306a36Sopenharmony_ci else 268262306a36Sopenharmony_ci nh = nexthop_create(net, cfg, extack); 268362306a36Sopenharmony_ci 268462306a36Sopenharmony_ci if (IS_ERR(nh)) 268562306a36Sopenharmony_ci return nh; 268662306a36Sopenharmony_ci 268762306a36Sopenharmony_ci refcount_set(&nh->refcnt, 1); 268862306a36Sopenharmony_ci nh->id = cfg->nh_id; 268962306a36Sopenharmony_ci nh->protocol = cfg->nh_protocol; 269062306a36Sopenharmony_ci nh->net = net; 269162306a36Sopenharmony_ci 269262306a36Sopenharmony_ci err = insert_nexthop(net, nh, cfg, extack); 269362306a36Sopenharmony_ci if (err) { 269462306a36Sopenharmony_ci __remove_nexthop(net, nh, NULL); 269562306a36Sopenharmony_ci nexthop_put(nh); 269662306a36Sopenharmony_ci nh = ERR_PTR(err); 269762306a36Sopenharmony_ci } 269862306a36Sopenharmony_ci 269962306a36Sopenharmony_ci return nh; 270062306a36Sopenharmony_ci} 270162306a36Sopenharmony_ci 270262306a36Sopenharmony_cistatic int rtm_nh_get_timer(struct nlattr *attr, unsigned long fallback, 270362306a36Sopenharmony_ci unsigned long *timer_p, bool *has_p, 270462306a36Sopenharmony_ci struct netlink_ext_ack *extack) 270562306a36Sopenharmony_ci{ 270662306a36Sopenharmony_ci unsigned long timer; 270762306a36Sopenharmony_ci u32 value; 270862306a36Sopenharmony_ci 270962306a36Sopenharmony_ci if (!attr) { 271062306a36Sopenharmony_ci *timer_p = fallback; 271162306a36Sopenharmony_ci *has_p = false; 271262306a36Sopenharmony_ci return 0; 271362306a36Sopenharmony_ci } 271462306a36Sopenharmony_ci 271562306a36Sopenharmony_ci value = nla_get_u32(attr); 271662306a36Sopenharmony_ci timer = clock_t_to_jiffies(value); 271762306a36Sopenharmony_ci if (timer == ~0UL) { 271862306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Timer value too large"); 271962306a36Sopenharmony_ci return -EINVAL; 272062306a36Sopenharmony_ci } 272162306a36Sopenharmony_ci 272262306a36Sopenharmony_ci *timer_p = timer; 272362306a36Sopenharmony_ci *has_p = true; 272462306a36Sopenharmony_ci return 0; 272562306a36Sopenharmony_ci} 272662306a36Sopenharmony_ci 272762306a36Sopenharmony_cistatic int rtm_to_nh_config_grp_res(struct nlattr *res, struct nh_config *cfg, 272862306a36Sopenharmony_ci struct netlink_ext_ack *extack) 272962306a36Sopenharmony_ci{ 273062306a36Sopenharmony_ci struct nlattr *tb[ARRAY_SIZE(rtm_nh_res_policy_new)] = {}; 273162306a36Sopenharmony_ci int err; 273262306a36Sopenharmony_ci 273362306a36Sopenharmony_ci if (res) { 273462306a36Sopenharmony_ci err = nla_parse_nested(tb, 273562306a36Sopenharmony_ci ARRAY_SIZE(rtm_nh_res_policy_new) - 1, 273662306a36Sopenharmony_ci res, rtm_nh_res_policy_new, extack); 273762306a36Sopenharmony_ci if (err < 0) 273862306a36Sopenharmony_ci return err; 273962306a36Sopenharmony_ci } 274062306a36Sopenharmony_ci 274162306a36Sopenharmony_ci if (tb[NHA_RES_GROUP_BUCKETS]) { 274262306a36Sopenharmony_ci cfg->nh_grp_res_num_buckets = 274362306a36Sopenharmony_ci nla_get_u16(tb[NHA_RES_GROUP_BUCKETS]); 274462306a36Sopenharmony_ci cfg->nh_grp_res_has_num_buckets = true; 274562306a36Sopenharmony_ci if (!cfg->nh_grp_res_num_buckets) { 274662306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Number of buckets needs to be non-0"); 274762306a36Sopenharmony_ci return -EINVAL; 274862306a36Sopenharmony_ci } 274962306a36Sopenharmony_ci } 275062306a36Sopenharmony_ci 275162306a36Sopenharmony_ci err = rtm_nh_get_timer(tb[NHA_RES_GROUP_IDLE_TIMER], 275262306a36Sopenharmony_ci NH_RES_DEFAULT_IDLE_TIMER, 275362306a36Sopenharmony_ci &cfg->nh_grp_res_idle_timer, 275462306a36Sopenharmony_ci &cfg->nh_grp_res_has_idle_timer, 275562306a36Sopenharmony_ci extack); 275662306a36Sopenharmony_ci if (err) 275762306a36Sopenharmony_ci return err; 275862306a36Sopenharmony_ci 275962306a36Sopenharmony_ci return rtm_nh_get_timer(tb[NHA_RES_GROUP_UNBALANCED_TIMER], 276062306a36Sopenharmony_ci NH_RES_DEFAULT_UNBALANCED_TIMER, 276162306a36Sopenharmony_ci &cfg->nh_grp_res_unbalanced_timer, 276262306a36Sopenharmony_ci &cfg->nh_grp_res_has_unbalanced_timer, 276362306a36Sopenharmony_ci extack); 276462306a36Sopenharmony_ci} 276562306a36Sopenharmony_ci 276662306a36Sopenharmony_cistatic int rtm_to_nh_config(struct net *net, struct sk_buff *skb, 276762306a36Sopenharmony_ci struct nlmsghdr *nlh, struct nh_config *cfg, 276862306a36Sopenharmony_ci struct netlink_ext_ack *extack) 276962306a36Sopenharmony_ci{ 277062306a36Sopenharmony_ci struct nhmsg *nhm = nlmsg_data(nlh); 277162306a36Sopenharmony_ci struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_new)]; 277262306a36Sopenharmony_ci int err; 277362306a36Sopenharmony_ci 277462306a36Sopenharmony_ci err = nlmsg_parse(nlh, sizeof(*nhm), tb, 277562306a36Sopenharmony_ci ARRAY_SIZE(rtm_nh_policy_new) - 1, 277662306a36Sopenharmony_ci rtm_nh_policy_new, extack); 277762306a36Sopenharmony_ci if (err < 0) 277862306a36Sopenharmony_ci return err; 277962306a36Sopenharmony_ci 278062306a36Sopenharmony_ci err = -EINVAL; 278162306a36Sopenharmony_ci if (nhm->resvd || nhm->nh_scope) { 278262306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Invalid values in ancillary header"); 278362306a36Sopenharmony_ci goto out; 278462306a36Sopenharmony_ci } 278562306a36Sopenharmony_ci if (nhm->nh_flags & ~NEXTHOP_VALID_USER_FLAGS) { 278662306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Invalid nexthop flags in ancillary header"); 278762306a36Sopenharmony_ci goto out; 278862306a36Sopenharmony_ci } 278962306a36Sopenharmony_ci 279062306a36Sopenharmony_ci switch (nhm->nh_family) { 279162306a36Sopenharmony_ci case AF_INET: 279262306a36Sopenharmony_ci case AF_INET6: 279362306a36Sopenharmony_ci break; 279462306a36Sopenharmony_ci case AF_UNSPEC: 279562306a36Sopenharmony_ci if (tb[NHA_GROUP]) 279662306a36Sopenharmony_ci break; 279762306a36Sopenharmony_ci fallthrough; 279862306a36Sopenharmony_ci default: 279962306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Invalid address family"); 280062306a36Sopenharmony_ci goto out; 280162306a36Sopenharmony_ci } 280262306a36Sopenharmony_ci 280362306a36Sopenharmony_ci memset(cfg, 0, sizeof(*cfg)); 280462306a36Sopenharmony_ci cfg->nlflags = nlh->nlmsg_flags; 280562306a36Sopenharmony_ci cfg->nlinfo.portid = NETLINK_CB(skb).portid; 280662306a36Sopenharmony_ci cfg->nlinfo.nlh = nlh; 280762306a36Sopenharmony_ci cfg->nlinfo.nl_net = net; 280862306a36Sopenharmony_ci 280962306a36Sopenharmony_ci cfg->nh_family = nhm->nh_family; 281062306a36Sopenharmony_ci cfg->nh_protocol = nhm->nh_protocol; 281162306a36Sopenharmony_ci cfg->nh_flags = nhm->nh_flags; 281262306a36Sopenharmony_ci 281362306a36Sopenharmony_ci if (tb[NHA_ID]) 281462306a36Sopenharmony_ci cfg->nh_id = nla_get_u32(tb[NHA_ID]); 281562306a36Sopenharmony_ci 281662306a36Sopenharmony_ci if (tb[NHA_FDB]) { 281762306a36Sopenharmony_ci if (tb[NHA_OIF] || tb[NHA_BLACKHOLE] || 281862306a36Sopenharmony_ci tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE]) { 281962306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Fdb attribute can not be used with encap, oif or blackhole"); 282062306a36Sopenharmony_ci goto out; 282162306a36Sopenharmony_ci } 282262306a36Sopenharmony_ci if (nhm->nh_flags) { 282362306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Unsupported nexthop flags in ancillary header"); 282462306a36Sopenharmony_ci goto out; 282562306a36Sopenharmony_ci } 282662306a36Sopenharmony_ci cfg->nh_fdb = nla_get_flag(tb[NHA_FDB]); 282762306a36Sopenharmony_ci } 282862306a36Sopenharmony_ci 282962306a36Sopenharmony_ci if (tb[NHA_GROUP]) { 283062306a36Sopenharmony_ci if (nhm->nh_family != AF_UNSPEC) { 283162306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Invalid family for group"); 283262306a36Sopenharmony_ci goto out; 283362306a36Sopenharmony_ci } 283462306a36Sopenharmony_ci cfg->nh_grp = tb[NHA_GROUP]; 283562306a36Sopenharmony_ci 283662306a36Sopenharmony_ci cfg->nh_grp_type = NEXTHOP_GRP_TYPE_MPATH; 283762306a36Sopenharmony_ci if (tb[NHA_GROUP_TYPE]) 283862306a36Sopenharmony_ci cfg->nh_grp_type = nla_get_u16(tb[NHA_GROUP_TYPE]); 283962306a36Sopenharmony_ci 284062306a36Sopenharmony_ci if (cfg->nh_grp_type > NEXTHOP_GRP_TYPE_MAX) { 284162306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Invalid group type"); 284262306a36Sopenharmony_ci goto out; 284362306a36Sopenharmony_ci } 284462306a36Sopenharmony_ci err = nh_check_attr_group(net, tb, ARRAY_SIZE(tb), 284562306a36Sopenharmony_ci cfg->nh_grp_type, extack); 284662306a36Sopenharmony_ci if (err) 284762306a36Sopenharmony_ci goto out; 284862306a36Sopenharmony_ci 284962306a36Sopenharmony_ci if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_RES) 285062306a36Sopenharmony_ci err = rtm_to_nh_config_grp_res(tb[NHA_RES_GROUP], 285162306a36Sopenharmony_ci cfg, extack); 285262306a36Sopenharmony_ci 285362306a36Sopenharmony_ci /* no other attributes should be set */ 285462306a36Sopenharmony_ci goto out; 285562306a36Sopenharmony_ci } 285662306a36Sopenharmony_ci 285762306a36Sopenharmony_ci if (tb[NHA_BLACKHOLE]) { 285862306a36Sopenharmony_ci if (tb[NHA_GATEWAY] || tb[NHA_OIF] || 285962306a36Sopenharmony_ci tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE] || tb[NHA_FDB]) { 286062306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Blackhole attribute can not be used with gateway, oif, encap or fdb"); 286162306a36Sopenharmony_ci goto out; 286262306a36Sopenharmony_ci } 286362306a36Sopenharmony_ci 286462306a36Sopenharmony_ci cfg->nh_blackhole = 1; 286562306a36Sopenharmony_ci err = 0; 286662306a36Sopenharmony_ci goto out; 286762306a36Sopenharmony_ci } 286862306a36Sopenharmony_ci 286962306a36Sopenharmony_ci if (!cfg->nh_fdb && !tb[NHA_OIF]) { 287062306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Device attribute required for non-blackhole and non-fdb nexthops"); 287162306a36Sopenharmony_ci goto out; 287262306a36Sopenharmony_ci } 287362306a36Sopenharmony_ci 287462306a36Sopenharmony_ci if (!cfg->nh_fdb && tb[NHA_OIF]) { 287562306a36Sopenharmony_ci cfg->nh_ifindex = nla_get_u32(tb[NHA_OIF]); 287662306a36Sopenharmony_ci if (cfg->nh_ifindex) 287762306a36Sopenharmony_ci cfg->dev = __dev_get_by_index(net, cfg->nh_ifindex); 287862306a36Sopenharmony_ci 287962306a36Sopenharmony_ci if (!cfg->dev) { 288062306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Invalid device index"); 288162306a36Sopenharmony_ci goto out; 288262306a36Sopenharmony_ci } else if (!(cfg->dev->flags & IFF_UP)) { 288362306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 288462306a36Sopenharmony_ci err = -ENETDOWN; 288562306a36Sopenharmony_ci goto out; 288662306a36Sopenharmony_ci } else if (!netif_carrier_ok(cfg->dev)) { 288762306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Carrier for nexthop device is down"); 288862306a36Sopenharmony_ci err = -ENETDOWN; 288962306a36Sopenharmony_ci goto out; 289062306a36Sopenharmony_ci } 289162306a36Sopenharmony_ci } 289262306a36Sopenharmony_ci 289362306a36Sopenharmony_ci err = -EINVAL; 289462306a36Sopenharmony_ci if (tb[NHA_GATEWAY]) { 289562306a36Sopenharmony_ci struct nlattr *gwa = tb[NHA_GATEWAY]; 289662306a36Sopenharmony_ci 289762306a36Sopenharmony_ci switch (cfg->nh_family) { 289862306a36Sopenharmony_ci case AF_INET: 289962306a36Sopenharmony_ci if (nla_len(gwa) != sizeof(u32)) { 290062306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Invalid gateway"); 290162306a36Sopenharmony_ci goto out; 290262306a36Sopenharmony_ci } 290362306a36Sopenharmony_ci cfg->gw.ipv4 = nla_get_be32(gwa); 290462306a36Sopenharmony_ci break; 290562306a36Sopenharmony_ci case AF_INET6: 290662306a36Sopenharmony_ci if (nla_len(gwa) != sizeof(struct in6_addr)) { 290762306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Invalid gateway"); 290862306a36Sopenharmony_ci goto out; 290962306a36Sopenharmony_ci } 291062306a36Sopenharmony_ci cfg->gw.ipv6 = nla_get_in6_addr(gwa); 291162306a36Sopenharmony_ci break; 291262306a36Sopenharmony_ci default: 291362306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, 291462306a36Sopenharmony_ci "Unknown address family for gateway"); 291562306a36Sopenharmony_ci goto out; 291662306a36Sopenharmony_ci } 291762306a36Sopenharmony_ci } else { 291862306a36Sopenharmony_ci /* device only nexthop (no gateway) */ 291962306a36Sopenharmony_ci if (cfg->nh_flags & RTNH_F_ONLINK) { 292062306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, 292162306a36Sopenharmony_ci "ONLINK flag can not be set for nexthop without a gateway"); 292262306a36Sopenharmony_ci goto out; 292362306a36Sopenharmony_ci } 292462306a36Sopenharmony_ci } 292562306a36Sopenharmony_ci 292662306a36Sopenharmony_ci if (tb[NHA_ENCAP]) { 292762306a36Sopenharmony_ci cfg->nh_encap = tb[NHA_ENCAP]; 292862306a36Sopenharmony_ci 292962306a36Sopenharmony_ci if (!tb[NHA_ENCAP_TYPE]) { 293062306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "LWT encapsulation type is missing"); 293162306a36Sopenharmony_ci goto out; 293262306a36Sopenharmony_ci } 293362306a36Sopenharmony_ci 293462306a36Sopenharmony_ci cfg->nh_encap_type = nla_get_u16(tb[NHA_ENCAP_TYPE]); 293562306a36Sopenharmony_ci err = lwtunnel_valid_encap_type(cfg->nh_encap_type, extack); 293662306a36Sopenharmony_ci if (err < 0) 293762306a36Sopenharmony_ci goto out; 293862306a36Sopenharmony_ci 293962306a36Sopenharmony_ci } else if (tb[NHA_ENCAP_TYPE]) { 294062306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "LWT encapsulation attribute is missing"); 294162306a36Sopenharmony_ci goto out; 294262306a36Sopenharmony_ci } 294362306a36Sopenharmony_ci 294462306a36Sopenharmony_ci 294562306a36Sopenharmony_ci err = 0; 294662306a36Sopenharmony_ciout: 294762306a36Sopenharmony_ci return err; 294862306a36Sopenharmony_ci} 294962306a36Sopenharmony_ci 295062306a36Sopenharmony_ci/* rtnl */ 295162306a36Sopenharmony_cistatic int rtm_new_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh, 295262306a36Sopenharmony_ci struct netlink_ext_ack *extack) 295362306a36Sopenharmony_ci{ 295462306a36Sopenharmony_ci struct net *net = sock_net(skb->sk); 295562306a36Sopenharmony_ci struct nh_config cfg; 295662306a36Sopenharmony_ci struct nexthop *nh; 295762306a36Sopenharmony_ci int err; 295862306a36Sopenharmony_ci 295962306a36Sopenharmony_ci err = rtm_to_nh_config(net, skb, nlh, &cfg, extack); 296062306a36Sopenharmony_ci if (!err) { 296162306a36Sopenharmony_ci nh = nexthop_add(net, &cfg, extack); 296262306a36Sopenharmony_ci if (IS_ERR(nh)) 296362306a36Sopenharmony_ci err = PTR_ERR(nh); 296462306a36Sopenharmony_ci } 296562306a36Sopenharmony_ci 296662306a36Sopenharmony_ci return err; 296762306a36Sopenharmony_ci} 296862306a36Sopenharmony_ci 296962306a36Sopenharmony_cistatic int __nh_valid_get_del_req(const struct nlmsghdr *nlh, 297062306a36Sopenharmony_ci struct nlattr **tb, u32 *id, 297162306a36Sopenharmony_ci struct netlink_ext_ack *extack) 297262306a36Sopenharmony_ci{ 297362306a36Sopenharmony_ci struct nhmsg *nhm = nlmsg_data(nlh); 297462306a36Sopenharmony_ci 297562306a36Sopenharmony_ci if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) { 297662306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Invalid values in header"); 297762306a36Sopenharmony_ci return -EINVAL; 297862306a36Sopenharmony_ci } 297962306a36Sopenharmony_ci 298062306a36Sopenharmony_ci if (!tb[NHA_ID]) { 298162306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Nexthop id is missing"); 298262306a36Sopenharmony_ci return -EINVAL; 298362306a36Sopenharmony_ci } 298462306a36Sopenharmony_ci 298562306a36Sopenharmony_ci *id = nla_get_u32(tb[NHA_ID]); 298662306a36Sopenharmony_ci if (!(*id)) { 298762306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Invalid nexthop id"); 298862306a36Sopenharmony_ci return -EINVAL; 298962306a36Sopenharmony_ci } 299062306a36Sopenharmony_ci 299162306a36Sopenharmony_ci return 0; 299262306a36Sopenharmony_ci} 299362306a36Sopenharmony_ci 299462306a36Sopenharmony_cistatic int nh_valid_get_del_req(const struct nlmsghdr *nlh, u32 *id, 299562306a36Sopenharmony_ci struct netlink_ext_ack *extack) 299662306a36Sopenharmony_ci{ 299762306a36Sopenharmony_ci struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_get)]; 299862306a36Sopenharmony_ci int err; 299962306a36Sopenharmony_ci 300062306a36Sopenharmony_ci err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb, 300162306a36Sopenharmony_ci ARRAY_SIZE(rtm_nh_policy_get) - 1, 300262306a36Sopenharmony_ci rtm_nh_policy_get, extack); 300362306a36Sopenharmony_ci if (err < 0) 300462306a36Sopenharmony_ci return err; 300562306a36Sopenharmony_ci 300662306a36Sopenharmony_ci return __nh_valid_get_del_req(nlh, tb, id, extack); 300762306a36Sopenharmony_ci} 300862306a36Sopenharmony_ci 300962306a36Sopenharmony_ci/* rtnl */ 301062306a36Sopenharmony_cistatic int rtm_del_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh, 301162306a36Sopenharmony_ci struct netlink_ext_ack *extack) 301262306a36Sopenharmony_ci{ 301362306a36Sopenharmony_ci struct net *net = sock_net(skb->sk); 301462306a36Sopenharmony_ci struct nl_info nlinfo = { 301562306a36Sopenharmony_ci .nlh = nlh, 301662306a36Sopenharmony_ci .nl_net = net, 301762306a36Sopenharmony_ci .portid = NETLINK_CB(skb).portid, 301862306a36Sopenharmony_ci }; 301962306a36Sopenharmony_ci struct nexthop *nh; 302062306a36Sopenharmony_ci int err; 302162306a36Sopenharmony_ci u32 id; 302262306a36Sopenharmony_ci 302362306a36Sopenharmony_ci err = nh_valid_get_del_req(nlh, &id, extack); 302462306a36Sopenharmony_ci if (err) 302562306a36Sopenharmony_ci return err; 302662306a36Sopenharmony_ci 302762306a36Sopenharmony_ci nh = nexthop_find_by_id(net, id); 302862306a36Sopenharmony_ci if (!nh) 302962306a36Sopenharmony_ci return -ENOENT; 303062306a36Sopenharmony_ci 303162306a36Sopenharmony_ci remove_nexthop(net, nh, &nlinfo); 303262306a36Sopenharmony_ci 303362306a36Sopenharmony_ci return 0; 303462306a36Sopenharmony_ci} 303562306a36Sopenharmony_ci 303662306a36Sopenharmony_ci/* rtnl */ 303762306a36Sopenharmony_cistatic int rtm_get_nexthop(struct sk_buff *in_skb, struct nlmsghdr *nlh, 303862306a36Sopenharmony_ci struct netlink_ext_ack *extack) 303962306a36Sopenharmony_ci{ 304062306a36Sopenharmony_ci struct net *net = sock_net(in_skb->sk); 304162306a36Sopenharmony_ci struct sk_buff *skb = NULL; 304262306a36Sopenharmony_ci struct nexthop *nh; 304362306a36Sopenharmony_ci int err; 304462306a36Sopenharmony_ci u32 id; 304562306a36Sopenharmony_ci 304662306a36Sopenharmony_ci err = nh_valid_get_del_req(nlh, &id, extack); 304762306a36Sopenharmony_ci if (err) 304862306a36Sopenharmony_ci return err; 304962306a36Sopenharmony_ci 305062306a36Sopenharmony_ci err = -ENOBUFS; 305162306a36Sopenharmony_ci skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 305262306a36Sopenharmony_ci if (!skb) 305362306a36Sopenharmony_ci goto out; 305462306a36Sopenharmony_ci 305562306a36Sopenharmony_ci err = -ENOENT; 305662306a36Sopenharmony_ci nh = nexthop_find_by_id(net, id); 305762306a36Sopenharmony_ci if (!nh) 305862306a36Sopenharmony_ci goto errout_free; 305962306a36Sopenharmony_ci 306062306a36Sopenharmony_ci err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, NETLINK_CB(in_skb).portid, 306162306a36Sopenharmony_ci nlh->nlmsg_seq, 0); 306262306a36Sopenharmony_ci if (err < 0) { 306362306a36Sopenharmony_ci WARN_ON(err == -EMSGSIZE); 306462306a36Sopenharmony_ci goto errout_free; 306562306a36Sopenharmony_ci } 306662306a36Sopenharmony_ci 306762306a36Sopenharmony_ci err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 306862306a36Sopenharmony_ciout: 306962306a36Sopenharmony_ci return err; 307062306a36Sopenharmony_cierrout_free: 307162306a36Sopenharmony_ci kfree_skb(skb); 307262306a36Sopenharmony_ci goto out; 307362306a36Sopenharmony_ci} 307462306a36Sopenharmony_ci 307562306a36Sopenharmony_cistruct nh_dump_filter { 307662306a36Sopenharmony_ci u32 nh_id; 307762306a36Sopenharmony_ci int dev_idx; 307862306a36Sopenharmony_ci int master_idx; 307962306a36Sopenharmony_ci bool group_filter; 308062306a36Sopenharmony_ci bool fdb_filter; 308162306a36Sopenharmony_ci u32 res_bucket_nh_id; 308262306a36Sopenharmony_ci}; 308362306a36Sopenharmony_ci 308462306a36Sopenharmony_cistatic bool nh_dump_filtered(struct nexthop *nh, 308562306a36Sopenharmony_ci struct nh_dump_filter *filter, u8 family) 308662306a36Sopenharmony_ci{ 308762306a36Sopenharmony_ci const struct net_device *dev; 308862306a36Sopenharmony_ci const struct nh_info *nhi; 308962306a36Sopenharmony_ci 309062306a36Sopenharmony_ci if (filter->group_filter && !nh->is_group) 309162306a36Sopenharmony_ci return true; 309262306a36Sopenharmony_ci 309362306a36Sopenharmony_ci if (!filter->dev_idx && !filter->master_idx && !family) 309462306a36Sopenharmony_ci return false; 309562306a36Sopenharmony_ci 309662306a36Sopenharmony_ci if (nh->is_group) 309762306a36Sopenharmony_ci return true; 309862306a36Sopenharmony_ci 309962306a36Sopenharmony_ci nhi = rtnl_dereference(nh->nh_info); 310062306a36Sopenharmony_ci if (family && nhi->family != family) 310162306a36Sopenharmony_ci return true; 310262306a36Sopenharmony_ci 310362306a36Sopenharmony_ci dev = nhi->fib_nhc.nhc_dev; 310462306a36Sopenharmony_ci if (filter->dev_idx && (!dev || dev->ifindex != filter->dev_idx)) 310562306a36Sopenharmony_ci return true; 310662306a36Sopenharmony_ci 310762306a36Sopenharmony_ci if (filter->master_idx) { 310862306a36Sopenharmony_ci struct net_device *master; 310962306a36Sopenharmony_ci 311062306a36Sopenharmony_ci if (!dev) 311162306a36Sopenharmony_ci return true; 311262306a36Sopenharmony_ci 311362306a36Sopenharmony_ci master = netdev_master_upper_dev_get((struct net_device *)dev); 311462306a36Sopenharmony_ci if (!master || master->ifindex != filter->master_idx) 311562306a36Sopenharmony_ci return true; 311662306a36Sopenharmony_ci } 311762306a36Sopenharmony_ci 311862306a36Sopenharmony_ci return false; 311962306a36Sopenharmony_ci} 312062306a36Sopenharmony_ci 312162306a36Sopenharmony_cistatic int __nh_valid_dump_req(const struct nlmsghdr *nlh, struct nlattr **tb, 312262306a36Sopenharmony_ci struct nh_dump_filter *filter, 312362306a36Sopenharmony_ci struct netlink_ext_ack *extack) 312462306a36Sopenharmony_ci{ 312562306a36Sopenharmony_ci struct nhmsg *nhm; 312662306a36Sopenharmony_ci u32 idx; 312762306a36Sopenharmony_ci 312862306a36Sopenharmony_ci if (tb[NHA_OIF]) { 312962306a36Sopenharmony_ci idx = nla_get_u32(tb[NHA_OIF]); 313062306a36Sopenharmony_ci if (idx > INT_MAX) { 313162306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Invalid device index"); 313262306a36Sopenharmony_ci return -EINVAL; 313362306a36Sopenharmony_ci } 313462306a36Sopenharmony_ci filter->dev_idx = idx; 313562306a36Sopenharmony_ci } 313662306a36Sopenharmony_ci if (tb[NHA_MASTER]) { 313762306a36Sopenharmony_ci idx = nla_get_u32(tb[NHA_MASTER]); 313862306a36Sopenharmony_ci if (idx > INT_MAX) { 313962306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Invalid master device index"); 314062306a36Sopenharmony_ci return -EINVAL; 314162306a36Sopenharmony_ci } 314262306a36Sopenharmony_ci filter->master_idx = idx; 314362306a36Sopenharmony_ci } 314462306a36Sopenharmony_ci filter->group_filter = nla_get_flag(tb[NHA_GROUPS]); 314562306a36Sopenharmony_ci filter->fdb_filter = nla_get_flag(tb[NHA_FDB]); 314662306a36Sopenharmony_ci 314762306a36Sopenharmony_ci nhm = nlmsg_data(nlh); 314862306a36Sopenharmony_ci if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) { 314962306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Invalid values in header for nexthop dump request"); 315062306a36Sopenharmony_ci return -EINVAL; 315162306a36Sopenharmony_ci } 315262306a36Sopenharmony_ci 315362306a36Sopenharmony_ci return 0; 315462306a36Sopenharmony_ci} 315562306a36Sopenharmony_ci 315662306a36Sopenharmony_cistatic int nh_valid_dump_req(const struct nlmsghdr *nlh, 315762306a36Sopenharmony_ci struct nh_dump_filter *filter, 315862306a36Sopenharmony_ci struct netlink_callback *cb) 315962306a36Sopenharmony_ci{ 316062306a36Sopenharmony_ci struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_dump)]; 316162306a36Sopenharmony_ci int err; 316262306a36Sopenharmony_ci 316362306a36Sopenharmony_ci err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb, 316462306a36Sopenharmony_ci ARRAY_SIZE(rtm_nh_policy_dump) - 1, 316562306a36Sopenharmony_ci rtm_nh_policy_dump, cb->extack); 316662306a36Sopenharmony_ci if (err < 0) 316762306a36Sopenharmony_ci return err; 316862306a36Sopenharmony_ci 316962306a36Sopenharmony_ci return __nh_valid_dump_req(nlh, tb, filter, cb->extack); 317062306a36Sopenharmony_ci} 317162306a36Sopenharmony_ci 317262306a36Sopenharmony_cistruct rtm_dump_nh_ctx { 317362306a36Sopenharmony_ci u32 idx; 317462306a36Sopenharmony_ci}; 317562306a36Sopenharmony_ci 317662306a36Sopenharmony_cistatic struct rtm_dump_nh_ctx * 317762306a36Sopenharmony_cirtm_dump_nh_ctx(struct netlink_callback *cb) 317862306a36Sopenharmony_ci{ 317962306a36Sopenharmony_ci struct rtm_dump_nh_ctx *ctx = (void *)cb->ctx; 318062306a36Sopenharmony_ci 318162306a36Sopenharmony_ci BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx)); 318262306a36Sopenharmony_ci return ctx; 318362306a36Sopenharmony_ci} 318462306a36Sopenharmony_ci 318562306a36Sopenharmony_cistatic int rtm_dump_walk_nexthops(struct sk_buff *skb, 318662306a36Sopenharmony_ci struct netlink_callback *cb, 318762306a36Sopenharmony_ci struct rb_root *root, 318862306a36Sopenharmony_ci struct rtm_dump_nh_ctx *ctx, 318962306a36Sopenharmony_ci int (*nh_cb)(struct sk_buff *skb, 319062306a36Sopenharmony_ci struct netlink_callback *cb, 319162306a36Sopenharmony_ci struct nexthop *nh, void *data), 319262306a36Sopenharmony_ci void *data) 319362306a36Sopenharmony_ci{ 319462306a36Sopenharmony_ci struct rb_node *node; 319562306a36Sopenharmony_ci int s_idx; 319662306a36Sopenharmony_ci int err; 319762306a36Sopenharmony_ci 319862306a36Sopenharmony_ci s_idx = ctx->idx; 319962306a36Sopenharmony_ci for (node = rb_first(root); node; node = rb_next(node)) { 320062306a36Sopenharmony_ci struct nexthop *nh; 320162306a36Sopenharmony_ci 320262306a36Sopenharmony_ci nh = rb_entry(node, struct nexthop, rb_node); 320362306a36Sopenharmony_ci if (nh->id < s_idx) 320462306a36Sopenharmony_ci continue; 320562306a36Sopenharmony_ci 320662306a36Sopenharmony_ci ctx->idx = nh->id; 320762306a36Sopenharmony_ci err = nh_cb(skb, cb, nh, data); 320862306a36Sopenharmony_ci if (err) 320962306a36Sopenharmony_ci return err; 321062306a36Sopenharmony_ci } 321162306a36Sopenharmony_ci 321262306a36Sopenharmony_ci return 0; 321362306a36Sopenharmony_ci} 321462306a36Sopenharmony_ci 321562306a36Sopenharmony_cistatic int rtm_dump_nexthop_cb(struct sk_buff *skb, struct netlink_callback *cb, 321662306a36Sopenharmony_ci struct nexthop *nh, void *data) 321762306a36Sopenharmony_ci{ 321862306a36Sopenharmony_ci struct nhmsg *nhm = nlmsg_data(cb->nlh); 321962306a36Sopenharmony_ci struct nh_dump_filter *filter = data; 322062306a36Sopenharmony_ci 322162306a36Sopenharmony_ci if (nh_dump_filtered(nh, filter, nhm->nh_family)) 322262306a36Sopenharmony_ci return 0; 322362306a36Sopenharmony_ci 322462306a36Sopenharmony_ci return nh_fill_node(skb, nh, RTM_NEWNEXTHOP, 322562306a36Sopenharmony_ci NETLINK_CB(cb->skb).portid, 322662306a36Sopenharmony_ci cb->nlh->nlmsg_seq, NLM_F_MULTI); 322762306a36Sopenharmony_ci} 322862306a36Sopenharmony_ci 322962306a36Sopenharmony_ci/* rtnl */ 323062306a36Sopenharmony_cistatic int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb) 323162306a36Sopenharmony_ci{ 323262306a36Sopenharmony_ci struct rtm_dump_nh_ctx *ctx = rtm_dump_nh_ctx(cb); 323362306a36Sopenharmony_ci struct net *net = sock_net(skb->sk); 323462306a36Sopenharmony_ci struct rb_root *root = &net->nexthop.rb_root; 323562306a36Sopenharmony_ci struct nh_dump_filter filter = {}; 323662306a36Sopenharmony_ci int err; 323762306a36Sopenharmony_ci 323862306a36Sopenharmony_ci err = nh_valid_dump_req(cb->nlh, &filter, cb); 323962306a36Sopenharmony_ci if (err < 0) 324062306a36Sopenharmony_ci return err; 324162306a36Sopenharmony_ci 324262306a36Sopenharmony_ci err = rtm_dump_walk_nexthops(skb, cb, root, ctx, 324362306a36Sopenharmony_ci &rtm_dump_nexthop_cb, &filter); 324462306a36Sopenharmony_ci if (err < 0) { 324562306a36Sopenharmony_ci if (likely(skb->len)) 324662306a36Sopenharmony_ci err = skb->len; 324762306a36Sopenharmony_ci } 324862306a36Sopenharmony_ci 324962306a36Sopenharmony_ci cb->seq = net->nexthop.seq; 325062306a36Sopenharmony_ci nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 325162306a36Sopenharmony_ci return err; 325262306a36Sopenharmony_ci} 325362306a36Sopenharmony_ci 325462306a36Sopenharmony_cistatic struct nexthop * 325562306a36Sopenharmony_cinexthop_find_group_resilient(struct net *net, u32 id, 325662306a36Sopenharmony_ci struct netlink_ext_ack *extack) 325762306a36Sopenharmony_ci{ 325862306a36Sopenharmony_ci struct nh_group *nhg; 325962306a36Sopenharmony_ci struct nexthop *nh; 326062306a36Sopenharmony_ci 326162306a36Sopenharmony_ci nh = nexthop_find_by_id(net, id); 326262306a36Sopenharmony_ci if (!nh) 326362306a36Sopenharmony_ci return ERR_PTR(-ENOENT); 326462306a36Sopenharmony_ci 326562306a36Sopenharmony_ci if (!nh->is_group) { 326662306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Not a nexthop group"); 326762306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 326862306a36Sopenharmony_ci } 326962306a36Sopenharmony_ci 327062306a36Sopenharmony_ci nhg = rtnl_dereference(nh->nh_grp); 327162306a36Sopenharmony_ci if (!nhg->resilient) { 327262306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Nexthop group not of type resilient"); 327362306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 327462306a36Sopenharmony_ci } 327562306a36Sopenharmony_ci 327662306a36Sopenharmony_ci return nh; 327762306a36Sopenharmony_ci} 327862306a36Sopenharmony_ci 327962306a36Sopenharmony_cistatic int nh_valid_dump_nhid(struct nlattr *attr, u32 *nh_id_p, 328062306a36Sopenharmony_ci struct netlink_ext_ack *extack) 328162306a36Sopenharmony_ci{ 328262306a36Sopenharmony_ci u32 idx; 328362306a36Sopenharmony_ci 328462306a36Sopenharmony_ci if (attr) { 328562306a36Sopenharmony_ci idx = nla_get_u32(attr); 328662306a36Sopenharmony_ci if (!idx) { 328762306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Invalid nexthop id"); 328862306a36Sopenharmony_ci return -EINVAL; 328962306a36Sopenharmony_ci } 329062306a36Sopenharmony_ci *nh_id_p = idx; 329162306a36Sopenharmony_ci } else { 329262306a36Sopenharmony_ci *nh_id_p = 0; 329362306a36Sopenharmony_ci } 329462306a36Sopenharmony_ci 329562306a36Sopenharmony_ci return 0; 329662306a36Sopenharmony_ci} 329762306a36Sopenharmony_ci 329862306a36Sopenharmony_cistatic int nh_valid_dump_bucket_req(const struct nlmsghdr *nlh, 329962306a36Sopenharmony_ci struct nh_dump_filter *filter, 330062306a36Sopenharmony_ci struct netlink_callback *cb) 330162306a36Sopenharmony_ci{ 330262306a36Sopenharmony_ci struct nlattr *res_tb[ARRAY_SIZE(rtm_nh_res_bucket_policy_dump)]; 330362306a36Sopenharmony_ci struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_dump_bucket)]; 330462306a36Sopenharmony_ci int err; 330562306a36Sopenharmony_ci 330662306a36Sopenharmony_ci err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb, 330762306a36Sopenharmony_ci ARRAY_SIZE(rtm_nh_policy_dump_bucket) - 1, 330862306a36Sopenharmony_ci rtm_nh_policy_dump_bucket, NULL); 330962306a36Sopenharmony_ci if (err < 0) 331062306a36Sopenharmony_ci return err; 331162306a36Sopenharmony_ci 331262306a36Sopenharmony_ci err = nh_valid_dump_nhid(tb[NHA_ID], &filter->nh_id, cb->extack); 331362306a36Sopenharmony_ci if (err) 331462306a36Sopenharmony_ci return err; 331562306a36Sopenharmony_ci 331662306a36Sopenharmony_ci if (tb[NHA_RES_BUCKET]) { 331762306a36Sopenharmony_ci size_t max = ARRAY_SIZE(rtm_nh_res_bucket_policy_dump) - 1; 331862306a36Sopenharmony_ci 331962306a36Sopenharmony_ci err = nla_parse_nested(res_tb, max, 332062306a36Sopenharmony_ci tb[NHA_RES_BUCKET], 332162306a36Sopenharmony_ci rtm_nh_res_bucket_policy_dump, 332262306a36Sopenharmony_ci cb->extack); 332362306a36Sopenharmony_ci if (err < 0) 332462306a36Sopenharmony_ci return err; 332562306a36Sopenharmony_ci 332662306a36Sopenharmony_ci err = nh_valid_dump_nhid(res_tb[NHA_RES_BUCKET_NH_ID], 332762306a36Sopenharmony_ci &filter->res_bucket_nh_id, 332862306a36Sopenharmony_ci cb->extack); 332962306a36Sopenharmony_ci if (err) 333062306a36Sopenharmony_ci return err; 333162306a36Sopenharmony_ci } 333262306a36Sopenharmony_ci 333362306a36Sopenharmony_ci return __nh_valid_dump_req(nlh, tb, filter, cb->extack); 333462306a36Sopenharmony_ci} 333562306a36Sopenharmony_ci 333662306a36Sopenharmony_cistruct rtm_dump_res_bucket_ctx { 333762306a36Sopenharmony_ci struct rtm_dump_nh_ctx nh; 333862306a36Sopenharmony_ci u16 bucket_index; 333962306a36Sopenharmony_ci}; 334062306a36Sopenharmony_ci 334162306a36Sopenharmony_cistatic struct rtm_dump_res_bucket_ctx * 334262306a36Sopenharmony_cirtm_dump_res_bucket_ctx(struct netlink_callback *cb) 334362306a36Sopenharmony_ci{ 334462306a36Sopenharmony_ci struct rtm_dump_res_bucket_ctx *ctx = (void *)cb->ctx; 334562306a36Sopenharmony_ci 334662306a36Sopenharmony_ci BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx)); 334762306a36Sopenharmony_ci return ctx; 334862306a36Sopenharmony_ci} 334962306a36Sopenharmony_ci 335062306a36Sopenharmony_cistruct rtm_dump_nexthop_bucket_data { 335162306a36Sopenharmony_ci struct rtm_dump_res_bucket_ctx *ctx; 335262306a36Sopenharmony_ci struct nh_dump_filter filter; 335362306a36Sopenharmony_ci}; 335462306a36Sopenharmony_ci 335562306a36Sopenharmony_cistatic int rtm_dump_nexthop_bucket_nh(struct sk_buff *skb, 335662306a36Sopenharmony_ci struct netlink_callback *cb, 335762306a36Sopenharmony_ci struct nexthop *nh, 335862306a36Sopenharmony_ci struct rtm_dump_nexthop_bucket_data *dd) 335962306a36Sopenharmony_ci{ 336062306a36Sopenharmony_ci u32 portid = NETLINK_CB(cb->skb).portid; 336162306a36Sopenharmony_ci struct nhmsg *nhm = nlmsg_data(cb->nlh); 336262306a36Sopenharmony_ci struct nh_res_table *res_table; 336362306a36Sopenharmony_ci struct nh_group *nhg; 336462306a36Sopenharmony_ci u16 bucket_index; 336562306a36Sopenharmony_ci int err; 336662306a36Sopenharmony_ci 336762306a36Sopenharmony_ci nhg = rtnl_dereference(nh->nh_grp); 336862306a36Sopenharmony_ci res_table = rtnl_dereference(nhg->res_table); 336962306a36Sopenharmony_ci for (bucket_index = dd->ctx->bucket_index; 337062306a36Sopenharmony_ci bucket_index < res_table->num_nh_buckets; 337162306a36Sopenharmony_ci bucket_index++) { 337262306a36Sopenharmony_ci struct nh_res_bucket *bucket; 337362306a36Sopenharmony_ci struct nh_grp_entry *nhge; 337462306a36Sopenharmony_ci 337562306a36Sopenharmony_ci bucket = &res_table->nh_buckets[bucket_index]; 337662306a36Sopenharmony_ci nhge = rtnl_dereference(bucket->nh_entry); 337762306a36Sopenharmony_ci if (nh_dump_filtered(nhge->nh, &dd->filter, nhm->nh_family)) 337862306a36Sopenharmony_ci continue; 337962306a36Sopenharmony_ci 338062306a36Sopenharmony_ci if (dd->filter.res_bucket_nh_id && 338162306a36Sopenharmony_ci dd->filter.res_bucket_nh_id != nhge->nh->id) 338262306a36Sopenharmony_ci continue; 338362306a36Sopenharmony_ci 338462306a36Sopenharmony_ci dd->ctx->bucket_index = bucket_index; 338562306a36Sopenharmony_ci err = nh_fill_res_bucket(skb, nh, bucket, bucket_index, 338662306a36Sopenharmony_ci RTM_NEWNEXTHOPBUCKET, portid, 338762306a36Sopenharmony_ci cb->nlh->nlmsg_seq, NLM_F_MULTI, 338862306a36Sopenharmony_ci cb->extack); 338962306a36Sopenharmony_ci if (err) 339062306a36Sopenharmony_ci return err; 339162306a36Sopenharmony_ci } 339262306a36Sopenharmony_ci 339362306a36Sopenharmony_ci dd->ctx->bucket_index = 0; 339462306a36Sopenharmony_ci 339562306a36Sopenharmony_ci return 0; 339662306a36Sopenharmony_ci} 339762306a36Sopenharmony_ci 339862306a36Sopenharmony_cistatic int rtm_dump_nexthop_bucket_cb(struct sk_buff *skb, 339962306a36Sopenharmony_ci struct netlink_callback *cb, 340062306a36Sopenharmony_ci struct nexthop *nh, void *data) 340162306a36Sopenharmony_ci{ 340262306a36Sopenharmony_ci struct rtm_dump_nexthop_bucket_data *dd = data; 340362306a36Sopenharmony_ci struct nh_group *nhg; 340462306a36Sopenharmony_ci 340562306a36Sopenharmony_ci if (!nh->is_group) 340662306a36Sopenharmony_ci return 0; 340762306a36Sopenharmony_ci 340862306a36Sopenharmony_ci nhg = rtnl_dereference(nh->nh_grp); 340962306a36Sopenharmony_ci if (!nhg->resilient) 341062306a36Sopenharmony_ci return 0; 341162306a36Sopenharmony_ci 341262306a36Sopenharmony_ci return rtm_dump_nexthop_bucket_nh(skb, cb, nh, dd); 341362306a36Sopenharmony_ci} 341462306a36Sopenharmony_ci 341562306a36Sopenharmony_ci/* rtnl */ 341662306a36Sopenharmony_cistatic int rtm_dump_nexthop_bucket(struct sk_buff *skb, 341762306a36Sopenharmony_ci struct netlink_callback *cb) 341862306a36Sopenharmony_ci{ 341962306a36Sopenharmony_ci struct rtm_dump_res_bucket_ctx *ctx = rtm_dump_res_bucket_ctx(cb); 342062306a36Sopenharmony_ci struct rtm_dump_nexthop_bucket_data dd = { .ctx = ctx }; 342162306a36Sopenharmony_ci struct net *net = sock_net(skb->sk); 342262306a36Sopenharmony_ci struct nexthop *nh; 342362306a36Sopenharmony_ci int err; 342462306a36Sopenharmony_ci 342562306a36Sopenharmony_ci err = nh_valid_dump_bucket_req(cb->nlh, &dd.filter, cb); 342662306a36Sopenharmony_ci if (err) 342762306a36Sopenharmony_ci return err; 342862306a36Sopenharmony_ci 342962306a36Sopenharmony_ci if (dd.filter.nh_id) { 343062306a36Sopenharmony_ci nh = nexthop_find_group_resilient(net, dd.filter.nh_id, 343162306a36Sopenharmony_ci cb->extack); 343262306a36Sopenharmony_ci if (IS_ERR(nh)) 343362306a36Sopenharmony_ci return PTR_ERR(nh); 343462306a36Sopenharmony_ci err = rtm_dump_nexthop_bucket_nh(skb, cb, nh, &dd); 343562306a36Sopenharmony_ci } else { 343662306a36Sopenharmony_ci struct rb_root *root = &net->nexthop.rb_root; 343762306a36Sopenharmony_ci 343862306a36Sopenharmony_ci err = rtm_dump_walk_nexthops(skb, cb, root, &ctx->nh, 343962306a36Sopenharmony_ci &rtm_dump_nexthop_bucket_cb, &dd); 344062306a36Sopenharmony_ci } 344162306a36Sopenharmony_ci 344262306a36Sopenharmony_ci if (err < 0) { 344362306a36Sopenharmony_ci if (likely(skb->len)) 344462306a36Sopenharmony_ci err = skb->len; 344562306a36Sopenharmony_ci } 344662306a36Sopenharmony_ci 344762306a36Sopenharmony_ci cb->seq = net->nexthop.seq; 344862306a36Sopenharmony_ci nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 344962306a36Sopenharmony_ci return err; 345062306a36Sopenharmony_ci} 345162306a36Sopenharmony_ci 345262306a36Sopenharmony_cistatic int nh_valid_get_bucket_req_res_bucket(struct nlattr *res, 345362306a36Sopenharmony_ci u16 *bucket_index, 345462306a36Sopenharmony_ci struct netlink_ext_ack *extack) 345562306a36Sopenharmony_ci{ 345662306a36Sopenharmony_ci struct nlattr *tb[ARRAY_SIZE(rtm_nh_res_bucket_policy_get)]; 345762306a36Sopenharmony_ci int err; 345862306a36Sopenharmony_ci 345962306a36Sopenharmony_ci err = nla_parse_nested(tb, ARRAY_SIZE(rtm_nh_res_bucket_policy_get) - 1, 346062306a36Sopenharmony_ci res, rtm_nh_res_bucket_policy_get, extack); 346162306a36Sopenharmony_ci if (err < 0) 346262306a36Sopenharmony_ci return err; 346362306a36Sopenharmony_ci 346462306a36Sopenharmony_ci if (!tb[NHA_RES_BUCKET_INDEX]) { 346562306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Bucket index is missing"); 346662306a36Sopenharmony_ci return -EINVAL; 346762306a36Sopenharmony_ci } 346862306a36Sopenharmony_ci 346962306a36Sopenharmony_ci *bucket_index = nla_get_u16(tb[NHA_RES_BUCKET_INDEX]); 347062306a36Sopenharmony_ci return 0; 347162306a36Sopenharmony_ci} 347262306a36Sopenharmony_ci 347362306a36Sopenharmony_cistatic int nh_valid_get_bucket_req(const struct nlmsghdr *nlh, 347462306a36Sopenharmony_ci u32 *id, u16 *bucket_index, 347562306a36Sopenharmony_ci struct netlink_ext_ack *extack) 347662306a36Sopenharmony_ci{ 347762306a36Sopenharmony_ci struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_get_bucket)]; 347862306a36Sopenharmony_ci int err; 347962306a36Sopenharmony_ci 348062306a36Sopenharmony_ci err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb, 348162306a36Sopenharmony_ci ARRAY_SIZE(rtm_nh_policy_get_bucket) - 1, 348262306a36Sopenharmony_ci rtm_nh_policy_get_bucket, extack); 348362306a36Sopenharmony_ci if (err < 0) 348462306a36Sopenharmony_ci return err; 348562306a36Sopenharmony_ci 348662306a36Sopenharmony_ci err = __nh_valid_get_del_req(nlh, tb, id, extack); 348762306a36Sopenharmony_ci if (err) 348862306a36Sopenharmony_ci return err; 348962306a36Sopenharmony_ci 349062306a36Sopenharmony_ci if (!tb[NHA_RES_BUCKET]) { 349162306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Bucket information is missing"); 349262306a36Sopenharmony_ci return -EINVAL; 349362306a36Sopenharmony_ci } 349462306a36Sopenharmony_ci 349562306a36Sopenharmony_ci err = nh_valid_get_bucket_req_res_bucket(tb[NHA_RES_BUCKET], 349662306a36Sopenharmony_ci bucket_index, extack); 349762306a36Sopenharmony_ci if (err) 349862306a36Sopenharmony_ci return err; 349962306a36Sopenharmony_ci 350062306a36Sopenharmony_ci return 0; 350162306a36Sopenharmony_ci} 350262306a36Sopenharmony_ci 350362306a36Sopenharmony_ci/* rtnl */ 350462306a36Sopenharmony_cistatic int rtm_get_nexthop_bucket(struct sk_buff *in_skb, struct nlmsghdr *nlh, 350562306a36Sopenharmony_ci struct netlink_ext_ack *extack) 350662306a36Sopenharmony_ci{ 350762306a36Sopenharmony_ci struct net *net = sock_net(in_skb->sk); 350862306a36Sopenharmony_ci struct nh_res_table *res_table; 350962306a36Sopenharmony_ci struct sk_buff *skb = NULL; 351062306a36Sopenharmony_ci struct nh_group *nhg; 351162306a36Sopenharmony_ci struct nexthop *nh; 351262306a36Sopenharmony_ci u16 bucket_index; 351362306a36Sopenharmony_ci int err; 351462306a36Sopenharmony_ci u32 id; 351562306a36Sopenharmony_ci 351662306a36Sopenharmony_ci err = nh_valid_get_bucket_req(nlh, &id, &bucket_index, extack); 351762306a36Sopenharmony_ci if (err) 351862306a36Sopenharmony_ci return err; 351962306a36Sopenharmony_ci 352062306a36Sopenharmony_ci nh = nexthop_find_group_resilient(net, id, extack); 352162306a36Sopenharmony_ci if (IS_ERR(nh)) 352262306a36Sopenharmony_ci return PTR_ERR(nh); 352362306a36Sopenharmony_ci 352462306a36Sopenharmony_ci nhg = rtnl_dereference(nh->nh_grp); 352562306a36Sopenharmony_ci res_table = rtnl_dereference(nhg->res_table); 352662306a36Sopenharmony_ci if (bucket_index >= res_table->num_nh_buckets) { 352762306a36Sopenharmony_ci NL_SET_ERR_MSG(extack, "Bucket index out of bounds"); 352862306a36Sopenharmony_ci return -ENOENT; 352962306a36Sopenharmony_ci } 353062306a36Sopenharmony_ci 353162306a36Sopenharmony_ci skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 353262306a36Sopenharmony_ci if (!skb) 353362306a36Sopenharmony_ci return -ENOBUFS; 353462306a36Sopenharmony_ci 353562306a36Sopenharmony_ci err = nh_fill_res_bucket(skb, nh, &res_table->nh_buckets[bucket_index], 353662306a36Sopenharmony_ci bucket_index, RTM_NEWNEXTHOPBUCKET, 353762306a36Sopenharmony_ci NETLINK_CB(in_skb).portid, nlh->nlmsg_seq, 353862306a36Sopenharmony_ci 0, extack); 353962306a36Sopenharmony_ci if (err < 0) { 354062306a36Sopenharmony_ci WARN_ON(err == -EMSGSIZE); 354162306a36Sopenharmony_ci goto errout_free; 354262306a36Sopenharmony_ci } 354362306a36Sopenharmony_ci 354462306a36Sopenharmony_ci return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 354562306a36Sopenharmony_ci 354662306a36Sopenharmony_cierrout_free: 354762306a36Sopenharmony_ci kfree_skb(skb); 354862306a36Sopenharmony_ci return err; 354962306a36Sopenharmony_ci} 355062306a36Sopenharmony_ci 355162306a36Sopenharmony_cistatic void nexthop_sync_mtu(struct net_device *dev, u32 orig_mtu) 355262306a36Sopenharmony_ci{ 355362306a36Sopenharmony_ci unsigned int hash = nh_dev_hashfn(dev->ifindex); 355462306a36Sopenharmony_ci struct net *net = dev_net(dev); 355562306a36Sopenharmony_ci struct hlist_head *head = &net->nexthop.devhash[hash]; 355662306a36Sopenharmony_ci struct hlist_node *n; 355762306a36Sopenharmony_ci struct nh_info *nhi; 355862306a36Sopenharmony_ci 355962306a36Sopenharmony_ci hlist_for_each_entry_safe(nhi, n, head, dev_hash) { 356062306a36Sopenharmony_ci if (nhi->fib_nhc.nhc_dev == dev) { 356162306a36Sopenharmony_ci if (nhi->family == AF_INET) 356262306a36Sopenharmony_ci fib_nhc_update_mtu(&nhi->fib_nhc, dev->mtu, 356362306a36Sopenharmony_ci orig_mtu); 356462306a36Sopenharmony_ci } 356562306a36Sopenharmony_ci } 356662306a36Sopenharmony_ci} 356762306a36Sopenharmony_ci 356862306a36Sopenharmony_ci/* rtnl */ 356962306a36Sopenharmony_cistatic int nh_netdev_event(struct notifier_block *this, 357062306a36Sopenharmony_ci unsigned long event, void *ptr) 357162306a36Sopenharmony_ci{ 357262306a36Sopenharmony_ci struct net_device *dev = netdev_notifier_info_to_dev(ptr); 357362306a36Sopenharmony_ci struct netdev_notifier_info_ext *info_ext; 357462306a36Sopenharmony_ci 357562306a36Sopenharmony_ci switch (event) { 357662306a36Sopenharmony_ci case NETDEV_DOWN: 357762306a36Sopenharmony_ci case NETDEV_UNREGISTER: 357862306a36Sopenharmony_ci nexthop_flush_dev(dev, event); 357962306a36Sopenharmony_ci break; 358062306a36Sopenharmony_ci case NETDEV_CHANGE: 358162306a36Sopenharmony_ci if (!(dev_get_flags(dev) & (IFF_RUNNING | IFF_LOWER_UP))) 358262306a36Sopenharmony_ci nexthop_flush_dev(dev, event); 358362306a36Sopenharmony_ci break; 358462306a36Sopenharmony_ci case NETDEV_CHANGEMTU: 358562306a36Sopenharmony_ci info_ext = ptr; 358662306a36Sopenharmony_ci nexthop_sync_mtu(dev, info_ext->ext.mtu); 358762306a36Sopenharmony_ci rt_cache_flush(dev_net(dev)); 358862306a36Sopenharmony_ci break; 358962306a36Sopenharmony_ci } 359062306a36Sopenharmony_ci return NOTIFY_DONE; 359162306a36Sopenharmony_ci} 359262306a36Sopenharmony_ci 359362306a36Sopenharmony_cistatic struct notifier_block nh_netdev_notifier = { 359462306a36Sopenharmony_ci .notifier_call = nh_netdev_event, 359562306a36Sopenharmony_ci}; 359662306a36Sopenharmony_ci 359762306a36Sopenharmony_cistatic int nexthops_dump(struct net *net, struct notifier_block *nb, 359862306a36Sopenharmony_ci enum nexthop_event_type event_type, 359962306a36Sopenharmony_ci struct netlink_ext_ack *extack) 360062306a36Sopenharmony_ci{ 360162306a36Sopenharmony_ci struct rb_root *root = &net->nexthop.rb_root; 360262306a36Sopenharmony_ci struct rb_node *node; 360362306a36Sopenharmony_ci int err = 0; 360462306a36Sopenharmony_ci 360562306a36Sopenharmony_ci for (node = rb_first(root); node; node = rb_next(node)) { 360662306a36Sopenharmony_ci struct nexthop *nh; 360762306a36Sopenharmony_ci 360862306a36Sopenharmony_ci nh = rb_entry(node, struct nexthop, rb_node); 360962306a36Sopenharmony_ci err = call_nexthop_notifier(nb, net, event_type, nh, extack); 361062306a36Sopenharmony_ci if (err) 361162306a36Sopenharmony_ci break; 361262306a36Sopenharmony_ci } 361362306a36Sopenharmony_ci 361462306a36Sopenharmony_ci return err; 361562306a36Sopenharmony_ci} 361662306a36Sopenharmony_ci 361762306a36Sopenharmony_ciint register_nexthop_notifier(struct net *net, struct notifier_block *nb, 361862306a36Sopenharmony_ci struct netlink_ext_ack *extack) 361962306a36Sopenharmony_ci{ 362062306a36Sopenharmony_ci int err; 362162306a36Sopenharmony_ci 362262306a36Sopenharmony_ci rtnl_lock(); 362362306a36Sopenharmony_ci err = nexthops_dump(net, nb, NEXTHOP_EVENT_REPLACE, extack); 362462306a36Sopenharmony_ci if (err) 362562306a36Sopenharmony_ci goto unlock; 362662306a36Sopenharmony_ci err = blocking_notifier_chain_register(&net->nexthop.notifier_chain, 362762306a36Sopenharmony_ci nb); 362862306a36Sopenharmony_ciunlock: 362962306a36Sopenharmony_ci rtnl_unlock(); 363062306a36Sopenharmony_ci return err; 363162306a36Sopenharmony_ci} 363262306a36Sopenharmony_ciEXPORT_SYMBOL(register_nexthop_notifier); 363362306a36Sopenharmony_ci 363462306a36Sopenharmony_ciint unregister_nexthop_notifier(struct net *net, struct notifier_block *nb) 363562306a36Sopenharmony_ci{ 363662306a36Sopenharmony_ci int err; 363762306a36Sopenharmony_ci 363862306a36Sopenharmony_ci rtnl_lock(); 363962306a36Sopenharmony_ci err = blocking_notifier_chain_unregister(&net->nexthop.notifier_chain, 364062306a36Sopenharmony_ci nb); 364162306a36Sopenharmony_ci if (err) 364262306a36Sopenharmony_ci goto unlock; 364362306a36Sopenharmony_ci nexthops_dump(net, nb, NEXTHOP_EVENT_DEL, NULL); 364462306a36Sopenharmony_ciunlock: 364562306a36Sopenharmony_ci rtnl_unlock(); 364662306a36Sopenharmony_ci return err; 364762306a36Sopenharmony_ci} 364862306a36Sopenharmony_ciEXPORT_SYMBOL(unregister_nexthop_notifier); 364962306a36Sopenharmony_ci 365062306a36Sopenharmony_civoid nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap) 365162306a36Sopenharmony_ci{ 365262306a36Sopenharmony_ci struct nexthop *nexthop; 365362306a36Sopenharmony_ci 365462306a36Sopenharmony_ci rcu_read_lock(); 365562306a36Sopenharmony_ci 365662306a36Sopenharmony_ci nexthop = nexthop_find_by_id(net, id); 365762306a36Sopenharmony_ci if (!nexthop) 365862306a36Sopenharmony_ci goto out; 365962306a36Sopenharmony_ci 366062306a36Sopenharmony_ci nexthop->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP); 366162306a36Sopenharmony_ci if (offload) 366262306a36Sopenharmony_ci nexthop->nh_flags |= RTNH_F_OFFLOAD; 366362306a36Sopenharmony_ci if (trap) 366462306a36Sopenharmony_ci nexthop->nh_flags |= RTNH_F_TRAP; 366562306a36Sopenharmony_ci 366662306a36Sopenharmony_ciout: 366762306a36Sopenharmony_ci rcu_read_unlock(); 366862306a36Sopenharmony_ci} 366962306a36Sopenharmony_ciEXPORT_SYMBOL(nexthop_set_hw_flags); 367062306a36Sopenharmony_ci 367162306a36Sopenharmony_civoid nexthop_bucket_set_hw_flags(struct net *net, u32 id, u16 bucket_index, 367262306a36Sopenharmony_ci bool offload, bool trap) 367362306a36Sopenharmony_ci{ 367462306a36Sopenharmony_ci struct nh_res_table *res_table; 367562306a36Sopenharmony_ci struct nh_res_bucket *bucket; 367662306a36Sopenharmony_ci struct nexthop *nexthop; 367762306a36Sopenharmony_ci struct nh_group *nhg; 367862306a36Sopenharmony_ci 367962306a36Sopenharmony_ci rcu_read_lock(); 368062306a36Sopenharmony_ci 368162306a36Sopenharmony_ci nexthop = nexthop_find_by_id(net, id); 368262306a36Sopenharmony_ci if (!nexthop || !nexthop->is_group) 368362306a36Sopenharmony_ci goto out; 368462306a36Sopenharmony_ci 368562306a36Sopenharmony_ci nhg = rcu_dereference(nexthop->nh_grp); 368662306a36Sopenharmony_ci if (!nhg->resilient) 368762306a36Sopenharmony_ci goto out; 368862306a36Sopenharmony_ci 368962306a36Sopenharmony_ci if (bucket_index >= nhg->res_table->num_nh_buckets) 369062306a36Sopenharmony_ci goto out; 369162306a36Sopenharmony_ci 369262306a36Sopenharmony_ci res_table = rcu_dereference(nhg->res_table); 369362306a36Sopenharmony_ci bucket = &res_table->nh_buckets[bucket_index]; 369462306a36Sopenharmony_ci bucket->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP); 369562306a36Sopenharmony_ci if (offload) 369662306a36Sopenharmony_ci bucket->nh_flags |= RTNH_F_OFFLOAD; 369762306a36Sopenharmony_ci if (trap) 369862306a36Sopenharmony_ci bucket->nh_flags |= RTNH_F_TRAP; 369962306a36Sopenharmony_ci 370062306a36Sopenharmony_ciout: 370162306a36Sopenharmony_ci rcu_read_unlock(); 370262306a36Sopenharmony_ci} 370362306a36Sopenharmony_ciEXPORT_SYMBOL(nexthop_bucket_set_hw_flags); 370462306a36Sopenharmony_ci 370562306a36Sopenharmony_civoid nexthop_res_grp_activity_update(struct net *net, u32 id, u16 num_buckets, 370662306a36Sopenharmony_ci unsigned long *activity) 370762306a36Sopenharmony_ci{ 370862306a36Sopenharmony_ci struct nh_res_table *res_table; 370962306a36Sopenharmony_ci struct nexthop *nexthop; 371062306a36Sopenharmony_ci struct nh_group *nhg; 371162306a36Sopenharmony_ci u16 i; 371262306a36Sopenharmony_ci 371362306a36Sopenharmony_ci rcu_read_lock(); 371462306a36Sopenharmony_ci 371562306a36Sopenharmony_ci nexthop = nexthop_find_by_id(net, id); 371662306a36Sopenharmony_ci if (!nexthop || !nexthop->is_group) 371762306a36Sopenharmony_ci goto out; 371862306a36Sopenharmony_ci 371962306a36Sopenharmony_ci nhg = rcu_dereference(nexthop->nh_grp); 372062306a36Sopenharmony_ci if (!nhg->resilient) 372162306a36Sopenharmony_ci goto out; 372262306a36Sopenharmony_ci 372362306a36Sopenharmony_ci /* Instead of silently ignoring some buckets, demand that the sizes 372462306a36Sopenharmony_ci * be the same. 372562306a36Sopenharmony_ci */ 372662306a36Sopenharmony_ci res_table = rcu_dereference(nhg->res_table); 372762306a36Sopenharmony_ci if (num_buckets != res_table->num_nh_buckets) 372862306a36Sopenharmony_ci goto out; 372962306a36Sopenharmony_ci 373062306a36Sopenharmony_ci for (i = 0; i < num_buckets; i++) { 373162306a36Sopenharmony_ci if (test_bit(i, activity)) 373262306a36Sopenharmony_ci nh_res_bucket_set_busy(&res_table->nh_buckets[i]); 373362306a36Sopenharmony_ci } 373462306a36Sopenharmony_ci 373562306a36Sopenharmony_ciout: 373662306a36Sopenharmony_ci rcu_read_unlock(); 373762306a36Sopenharmony_ci} 373862306a36Sopenharmony_ciEXPORT_SYMBOL(nexthop_res_grp_activity_update); 373962306a36Sopenharmony_ci 374062306a36Sopenharmony_cistatic void __net_exit nexthop_net_exit_batch(struct list_head *net_list) 374162306a36Sopenharmony_ci{ 374262306a36Sopenharmony_ci struct net *net; 374362306a36Sopenharmony_ci 374462306a36Sopenharmony_ci rtnl_lock(); 374562306a36Sopenharmony_ci list_for_each_entry(net, net_list, exit_list) { 374662306a36Sopenharmony_ci flush_all_nexthops(net); 374762306a36Sopenharmony_ci kfree(net->nexthop.devhash); 374862306a36Sopenharmony_ci } 374962306a36Sopenharmony_ci rtnl_unlock(); 375062306a36Sopenharmony_ci} 375162306a36Sopenharmony_ci 375262306a36Sopenharmony_cistatic int __net_init nexthop_net_init(struct net *net) 375362306a36Sopenharmony_ci{ 375462306a36Sopenharmony_ci size_t sz = sizeof(struct hlist_head) * NH_DEV_HASHSIZE; 375562306a36Sopenharmony_ci 375662306a36Sopenharmony_ci net->nexthop.rb_root = RB_ROOT; 375762306a36Sopenharmony_ci net->nexthop.devhash = kzalloc(sz, GFP_KERNEL); 375862306a36Sopenharmony_ci if (!net->nexthop.devhash) 375962306a36Sopenharmony_ci return -ENOMEM; 376062306a36Sopenharmony_ci BLOCKING_INIT_NOTIFIER_HEAD(&net->nexthop.notifier_chain); 376162306a36Sopenharmony_ci 376262306a36Sopenharmony_ci return 0; 376362306a36Sopenharmony_ci} 376462306a36Sopenharmony_ci 376562306a36Sopenharmony_cistatic struct pernet_operations nexthop_net_ops = { 376662306a36Sopenharmony_ci .init = nexthop_net_init, 376762306a36Sopenharmony_ci .exit_batch = nexthop_net_exit_batch, 376862306a36Sopenharmony_ci}; 376962306a36Sopenharmony_ci 377062306a36Sopenharmony_cistatic int __init nexthop_init(void) 377162306a36Sopenharmony_ci{ 377262306a36Sopenharmony_ci register_pernet_subsys(&nexthop_net_ops); 377362306a36Sopenharmony_ci 377462306a36Sopenharmony_ci register_netdevice_notifier(&nh_netdev_notifier); 377562306a36Sopenharmony_ci 377662306a36Sopenharmony_ci rtnl_register(PF_UNSPEC, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0); 377762306a36Sopenharmony_ci rtnl_register(PF_UNSPEC, RTM_DELNEXTHOP, rtm_del_nexthop, NULL, 0); 377862306a36Sopenharmony_ci rtnl_register(PF_UNSPEC, RTM_GETNEXTHOP, rtm_get_nexthop, 377962306a36Sopenharmony_ci rtm_dump_nexthop, 0); 378062306a36Sopenharmony_ci 378162306a36Sopenharmony_ci rtnl_register(PF_INET, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0); 378262306a36Sopenharmony_ci rtnl_register(PF_INET, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0); 378362306a36Sopenharmony_ci 378462306a36Sopenharmony_ci rtnl_register(PF_INET6, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0); 378562306a36Sopenharmony_ci rtnl_register(PF_INET6, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0); 378662306a36Sopenharmony_ci 378762306a36Sopenharmony_ci rtnl_register(PF_UNSPEC, RTM_GETNEXTHOPBUCKET, rtm_get_nexthop_bucket, 378862306a36Sopenharmony_ci rtm_dump_nexthop_bucket, 0); 378962306a36Sopenharmony_ci 379062306a36Sopenharmony_ci return 0; 379162306a36Sopenharmony_ci} 379262306a36Sopenharmony_cisubsys_initcall(nexthop_init); 3793