1ab84be7eSDavid Ahern // SPDX-License-Identifier: GPL-2.0 2ab84be7eSDavid Ahern /* Generic nexthop implementation 3ab84be7eSDavid Ahern * 4ab84be7eSDavid Ahern * Copyright (c) 2017-19 Cumulus Networks 5ab84be7eSDavid Ahern * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com> 6ab84be7eSDavid Ahern */ 7ab84be7eSDavid Ahern 8ab84be7eSDavid Ahern #include <linux/nexthop.h> 9ab84be7eSDavid Ahern #include <linux/rtnetlink.h> 10ab84be7eSDavid Ahern #include <linux/slab.h> 11430a0491SDavid Ahern #include <net/arp.h> 1253010f99SDavid Ahern #include <net/ipv6_stubs.h> 13b513bd03SDavid Ahern #include <net/lwtunnel.h> 14430a0491SDavid Ahern #include <net/ndisc.h> 15ab84be7eSDavid Ahern #include <net/nexthop.h> 16597cfe4fSDavid Ahern #include <net/route.h> 17ab84be7eSDavid Ahern #include <net/sock.h> 18ab84be7eSDavid Ahern 19430a0491SDavid Ahern static void remove_nexthop(struct net *net, struct nexthop *nh, 20430a0491SDavid Ahern struct nl_info *nlinfo); 21430a0491SDavid Ahern 22597cfe4fSDavid Ahern #define NH_DEV_HASHBITS 8 23597cfe4fSDavid Ahern #define NH_DEV_HASHSIZE (1U << NH_DEV_HASHBITS) 24597cfe4fSDavid Ahern 25ab84be7eSDavid Ahern static const struct nla_policy rtm_nh_policy[NHA_MAX + 1] = { 26ab84be7eSDavid Ahern [NHA_ID] = { .type = NLA_U32 }, 27ab84be7eSDavid Ahern [NHA_GROUP] = { .type = NLA_BINARY }, 28ab84be7eSDavid Ahern [NHA_GROUP_TYPE] = { .type = NLA_U16 }, 29ab84be7eSDavid Ahern [NHA_BLACKHOLE] = { .type = NLA_FLAG }, 30ab84be7eSDavid Ahern [NHA_OIF] = { .type = NLA_U32 }, 31ab84be7eSDavid Ahern [NHA_GATEWAY] = { .type = NLA_BINARY }, 32ab84be7eSDavid Ahern [NHA_ENCAP_TYPE] = { .type = NLA_U16 }, 33ab84be7eSDavid Ahern [NHA_ENCAP] = { .type = NLA_NESTED }, 34ab84be7eSDavid Ahern [NHA_GROUPS] = { .type = NLA_FLAG }, 35ab84be7eSDavid Ahern [NHA_MASTER] = { .type = NLA_U32 }, 3638428d68SRoopa Prabhu [NHA_FDB] = { .type = NLA_FLAG }, 37ab84be7eSDavid Ahern }; 38ab84be7eSDavid Ahern 395ca474f2SIdo Schimmel static bool nexthop_notifiers_is_empty(struct net *net) 405ca474f2SIdo Schimmel { 415ca474f2SIdo Schimmel return !net->nexthop.notifier_chain.head; 425ca474f2SIdo Schimmel } 435ca474f2SIdo Schimmel 445ca474f2SIdo Schimmel static void 455ca474f2SIdo Schimmel __nh_notifier_single_info_init(struct nh_notifier_single_info *nh_info, 465ca474f2SIdo Schimmel const struct nexthop *nh) 475ca474f2SIdo Schimmel { 485ca474f2SIdo Schimmel struct nh_info *nhi = rtnl_dereference(nh->nh_info); 495ca474f2SIdo Schimmel 505ca474f2SIdo Schimmel nh_info->dev = nhi->fib_nhc.nhc_dev; 515ca474f2SIdo Schimmel nh_info->gw_family = nhi->fib_nhc.nhc_gw_family; 525ca474f2SIdo Schimmel if (nh_info->gw_family == AF_INET) 535ca474f2SIdo Schimmel nh_info->ipv4 = nhi->fib_nhc.nhc_gw.ipv4; 545ca474f2SIdo Schimmel else if (nh_info->gw_family == AF_INET6) 555ca474f2SIdo Schimmel nh_info->ipv6 = nhi->fib_nhc.nhc_gw.ipv6; 565ca474f2SIdo Schimmel 575ca474f2SIdo Schimmel nh_info->is_reject = nhi->reject_nh; 585ca474f2SIdo Schimmel nh_info->is_fdb = nhi->fdb_nh; 595ca474f2SIdo Schimmel nh_info->has_encap = !!nhi->fib_nhc.nhc_lwtstate; 605ca474f2SIdo Schimmel } 615ca474f2SIdo Schimmel 625ca474f2SIdo Schimmel static int nh_notifier_single_info_init(struct nh_notifier_info *info, 635ca474f2SIdo Schimmel const struct nexthop *nh) 645ca474f2SIdo Schimmel { 655ca474f2SIdo Schimmel info->nh = kzalloc(sizeof(*info->nh), GFP_KERNEL); 665ca474f2SIdo Schimmel if (!info->nh) 675ca474f2SIdo Schimmel return -ENOMEM; 685ca474f2SIdo Schimmel 695ca474f2SIdo Schimmel __nh_notifier_single_info_init(info->nh, nh); 705ca474f2SIdo Schimmel 715ca474f2SIdo Schimmel return 0; 725ca474f2SIdo Schimmel } 735ca474f2SIdo Schimmel 745ca474f2SIdo Schimmel static void nh_notifier_single_info_fini(struct nh_notifier_info *info) 755ca474f2SIdo Schimmel { 765ca474f2SIdo Schimmel kfree(info->nh); 775ca474f2SIdo Schimmel } 785ca474f2SIdo Schimmel 795ca474f2SIdo Schimmel static int nh_notifier_grp_info_init(struct nh_notifier_info *info, 805ca474f2SIdo Schimmel const struct nexthop *nh) 815ca474f2SIdo Schimmel { 825ca474f2SIdo Schimmel struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 835ca474f2SIdo Schimmel u16 num_nh = nhg->num_nh; 845ca474f2SIdo Schimmel int i; 855ca474f2SIdo Schimmel 865ca474f2SIdo Schimmel info->nh_grp = kzalloc(struct_size(info->nh_grp, nh_entries, num_nh), 875ca474f2SIdo Schimmel GFP_KERNEL); 885ca474f2SIdo Schimmel if (!info->nh_grp) 895ca474f2SIdo Schimmel return -ENOMEM; 905ca474f2SIdo Schimmel 915ca474f2SIdo Schimmel info->nh_grp->num_nh = num_nh; 925ca474f2SIdo Schimmel info->nh_grp->is_fdb = nhg->fdb_nh; 935ca474f2SIdo Schimmel 945ca474f2SIdo Schimmel for (i = 0; i < num_nh; i++) { 955ca474f2SIdo Schimmel struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 965ca474f2SIdo Schimmel 975ca474f2SIdo Schimmel info->nh_grp->nh_entries[i].id = nhge->nh->id; 985ca474f2SIdo Schimmel info->nh_grp->nh_entries[i].weight = nhge->weight; 995ca474f2SIdo Schimmel __nh_notifier_single_info_init(&info->nh_grp->nh_entries[i].nh, 1005ca474f2SIdo Schimmel nhge->nh); 1015ca474f2SIdo Schimmel } 1025ca474f2SIdo Schimmel 1035ca474f2SIdo Schimmel return 0; 1045ca474f2SIdo Schimmel } 1055ca474f2SIdo Schimmel 1065ca474f2SIdo Schimmel static void nh_notifier_grp_info_fini(struct nh_notifier_info *info) 1075ca474f2SIdo Schimmel { 1085ca474f2SIdo Schimmel kfree(info->nh_grp); 1095ca474f2SIdo Schimmel } 1105ca474f2SIdo Schimmel 1115ca474f2SIdo Schimmel static int nh_notifier_info_init(struct nh_notifier_info *info, 1125ca474f2SIdo Schimmel const struct nexthop *nh) 1135ca474f2SIdo Schimmel { 1145ca474f2SIdo Schimmel info->id = nh->id; 1155ca474f2SIdo Schimmel info->is_grp = nh->is_group; 1165ca474f2SIdo Schimmel 1175ca474f2SIdo Schimmel if (info->is_grp) 1185ca474f2SIdo Schimmel return nh_notifier_grp_info_init(info, nh); 1195ca474f2SIdo Schimmel else 1205ca474f2SIdo Schimmel return nh_notifier_single_info_init(info, nh); 1215ca474f2SIdo Schimmel } 1225ca474f2SIdo Schimmel 1235ca474f2SIdo Schimmel static void nh_notifier_info_fini(struct nh_notifier_info *info) 1245ca474f2SIdo Schimmel { 1255ca474f2SIdo Schimmel if (info->is_grp) 1265ca474f2SIdo Schimmel nh_notifier_grp_info_fini(info); 1275ca474f2SIdo Schimmel else 1285ca474f2SIdo Schimmel nh_notifier_single_info_fini(info); 1295ca474f2SIdo Schimmel } 1305ca474f2SIdo Schimmel 1318590ceedSRoopa Prabhu static int call_nexthop_notifiers(struct net *net, 132d8e79f1dSNathan Chancellor enum nexthop_event_type event_type, 1333578d53dSIdo Schimmel struct nexthop *nh, 1343578d53dSIdo Schimmel struct netlink_ext_ack *extack) 1358590ceedSRoopa Prabhu { 1365ca474f2SIdo Schimmel struct nh_notifier_info info = { 1375ca474f2SIdo Schimmel .net = net, 1385ca474f2SIdo Schimmel .extack = extack, 1395ca474f2SIdo Schimmel }; 1408590ceedSRoopa Prabhu int err; 1418590ceedSRoopa Prabhu 1425ca474f2SIdo Schimmel ASSERT_RTNL(); 1435ca474f2SIdo Schimmel 1445ca474f2SIdo Schimmel if (nexthop_notifiers_is_empty(net)) 1455ca474f2SIdo Schimmel return 0; 1465ca474f2SIdo Schimmel 1475ca474f2SIdo Schimmel err = nh_notifier_info_init(&info, nh); 1485ca474f2SIdo Schimmel if (err) { 1495ca474f2SIdo Schimmel NL_SET_ERR_MSG(extack, "Failed to initialize nexthop notifier info"); 1505ca474f2SIdo Schimmel return err; 1515ca474f2SIdo Schimmel } 1525ca474f2SIdo Schimmel 15380690ec6SIdo Schimmel err = blocking_notifier_call_chain(&net->nexthop.notifier_chain, 1541ec69d18SIdo Schimmel event_type, &info); 1555ca474f2SIdo Schimmel nh_notifier_info_fini(&info); 1565ca474f2SIdo Schimmel 1578590ceedSRoopa Prabhu return notifier_to_errno(err); 1588590ceedSRoopa Prabhu } 1598590ceedSRoopa Prabhu 160597cfe4fSDavid Ahern static unsigned int nh_dev_hashfn(unsigned int val) 161597cfe4fSDavid Ahern { 162597cfe4fSDavid Ahern unsigned int mask = NH_DEV_HASHSIZE - 1; 163597cfe4fSDavid Ahern 164597cfe4fSDavid Ahern return (val ^ 165597cfe4fSDavid Ahern (val >> NH_DEV_HASHBITS) ^ 166597cfe4fSDavid Ahern (val >> (NH_DEV_HASHBITS * 2))) & mask; 167597cfe4fSDavid Ahern } 168597cfe4fSDavid Ahern 169597cfe4fSDavid Ahern static void nexthop_devhash_add(struct net *net, struct nh_info *nhi) 170597cfe4fSDavid Ahern { 171597cfe4fSDavid Ahern struct net_device *dev = nhi->fib_nhc.nhc_dev; 172597cfe4fSDavid Ahern struct hlist_head *head; 173597cfe4fSDavid Ahern unsigned int hash; 174597cfe4fSDavid Ahern 175597cfe4fSDavid Ahern WARN_ON(!dev); 176597cfe4fSDavid Ahern 177597cfe4fSDavid Ahern hash = nh_dev_hashfn(dev->ifindex); 178597cfe4fSDavid Ahern head = &net->nexthop.devhash[hash]; 179597cfe4fSDavid Ahern hlist_add_head(&nhi->dev_hash, head); 180597cfe4fSDavid Ahern } 181597cfe4fSDavid Ahern 182430a0491SDavid Ahern static void nexthop_free_mpath(struct nexthop *nh) 183ab84be7eSDavid Ahern { 184430a0491SDavid Ahern struct nh_group *nhg; 185430a0491SDavid Ahern int i; 186430a0491SDavid Ahern 187430a0491SDavid Ahern nhg = rcu_dereference_raw(nh->nh_grp); 18890f33bffSNikolay Aleksandrov for (i = 0; i < nhg->num_nh; ++i) { 18990f33bffSNikolay Aleksandrov struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 190430a0491SDavid Ahern 19190f33bffSNikolay Aleksandrov WARN_ON(!list_empty(&nhge->nh_list)); 19290f33bffSNikolay Aleksandrov nexthop_put(nhge->nh); 19390f33bffSNikolay Aleksandrov } 19490f33bffSNikolay Aleksandrov 19590f33bffSNikolay Aleksandrov WARN_ON(nhg->spare == nhg); 19690f33bffSNikolay Aleksandrov 19790f33bffSNikolay Aleksandrov kfree(nhg->spare); 198430a0491SDavid Ahern kfree(nhg); 199430a0491SDavid Ahern } 200430a0491SDavid Ahern 201430a0491SDavid Ahern static void nexthop_free_single(struct nexthop *nh) 202430a0491SDavid Ahern { 203ab84be7eSDavid Ahern struct nh_info *nhi; 204ab84be7eSDavid Ahern 205ab84be7eSDavid Ahern nhi = rcu_dereference_raw(nh->nh_info); 206597cfe4fSDavid Ahern switch (nhi->family) { 207597cfe4fSDavid Ahern case AF_INET: 208597cfe4fSDavid Ahern fib_nh_release(nh->net, &nhi->fib_nh); 209597cfe4fSDavid Ahern break; 21053010f99SDavid Ahern case AF_INET6: 21153010f99SDavid Ahern ipv6_stub->fib6_nh_release(&nhi->fib6_nh); 21253010f99SDavid Ahern break; 213597cfe4fSDavid Ahern } 214ab84be7eSDavid Ahern kfree(nhi); 215430a0491SDavid Ahern } 216430a0491SDavid Ahern 217430a0491SDavid Ahern void nexthop_free_rcu(struct rcu_head *head) 218430a0491SDavid Ahern { 219430a0491SDavid Ahern struct nexthop *nh = container_of(head, struct nexthop, rcu); 220430a0491SDavid Ahern 221430a0491SDavid Ahern if (nh->is_group) 222430a0491SDavid Ahern nexthop_free_mpath(nh); 223430a0491SDavid Ahern else 224430a0491SDavid Ahern nexthop_free_single(nh); 225ab84be7eSDavid Ahern 226ab84be7eSDavid Ahern kfree(nh); 227ab84be7eSDavid Ahern } 228ab84be7eSDavid Ahern EXPORT_SYMBOL_GPL(nexthop_free_rcu); 229ab84be7eSDavid Ahern 230ab84be7eSDavid Ahern static struct nexthop *nexthop_alloc(void) 231ab84be7eSDavid Ahern { 232ab84be7eSDavid Ahern struct nexthop *nh; 233ab84be7eSDavid Ahern 234ab84be7eSDavid Ahern nh = kzalloc(sizeof(struct nexthop), GFP_KERNEL); 235430a0491SDavid Ahern if (nh) { 2364c7e8084SDavid Ahern INIT_LIST_HEAD(&nh->fi_list); 237f88d8ea6SDavid Ahern INIT_LIST_HEAD(&nh->f6i_list); 238430a0491SDavid Ahern INIT_LIST_HEAD(&nh->grp_list); 23938428d68SRoopa Prabhu INIT_LIST_HEAD(&nh->fdb_list); 240430a0491SDavid Ahern } 241ab84be7eSDavid Ahern return nh; 242ab84be7eSDavid Ahern } 243ab84be7eSDavid Ahern 244430a0491SDavid Ahern static struct nh_group *nexthop_grp_alloc(u16 num_nh) 245430a0491SDavid Ahern { 246430a0491SDavid Ahern struct nh_group *nhg; 247430a0491SDavid Ahern 248d7d49dc7SIdo Schimmel nhg = kzalloc(struct_size(nhg, nh_entries, num_nh), GFP_KERNEL); 249430a0491SDavid Ahern if (nhg) 250430a0491SDavid Ahern nhg->num_nh = num_nh; 251430a0491SDavid Ahern 252430a0491SDavid Ahern return nhg; 253430a0491SDavid Ahern } 254430a0491SDavid Ahern 255ab84be7eSDavid Ahern static void nh_base_seq_inc(struct net *net) 256ab84be7eSDavid Ahern { 257ab84be7eSDavid Ahern while (++net->nexthop.seq == 0) 258ab84be7eSDavid Ahern ; 259ab84be7eSDavid Ahern } 260ab84be7eSDavid Ahern 261ab84be7eSDavid Ahern /* no reference taken; rcu lock or rtnl must be held */ 262ab84be7eSDavid Ahern struct nexthop *nexthop_find_by_id(struct net *net, u32 id) 263ab84be7eSDavid Ahern { 264ab84be7eSDavid Ahern struct rb_node **pp, *parent = NULL, *next; 265ab84be7eSDavid Ahern 266ab84be7eSDavid Ahern pp = &net->nexthop.rb_root.rb_node; 267ab84be7eSDavid Ahern while (1) { 268ab84be7eSDavid Ahern struct nexthop *nh; 269ab84be7eSDavid Ahern 270ab84be7eSDavid Ahern next = rcu_dereference_raw(*pp); 271ab84be7eSDavid Ahern if (!next) 272ab84be7eSDavid Ahern break; 273ab84be7eSDavid Ahern parent = next; 274ab84be7eSDavid Ahern 275ab84be7eSDavid Ahern nh = rb_entry(parent, struct nexthop, rb_node); 276ab84be7eSDavid Ahern if (id < nh->id) 277ab84be7eSDavid Ahern pp = &next->rb_left; 278ab84be7eSDavid Ahern else if (id > nh->id) 279ab84be7eSDavid Ahern pp = &next->rb_right; 280ab84be7eSDavid Ahern else 281ab84be7eSDavid Ahern return nh; 282ab84be7eSDavid Ahern } 283ab84be7eSDavid Ahern return NULL; 284ab84be7eSDavid Ahern } 285ab84be7eSDavid Ahern EXPORT_SYMBOL_GPL(nexthop_find_by_id); 286ab84be7eSDavid Ahern 287ab84be7eSDavid Ahern /* used for auto id allocation; called with rtnl held */ 288ab84be7eSDavid Ahern static u32 nh_find_unused_id(struct net *net) 289ab84be7eSDavid Ahern { 290ab84be7eSDavid Ahern u32 id_start = net->nexthop.last_id_allocated; 291ab84be7eSDavid Ahern 292ab84be7eSDavid Ahern while (1) { 293ab84be7eSDavid Ahern net->nexthop.last_id_allocated++; 294ab84be7eSDavid Ahern if (net->nexthop.last_id_allocated == id_start) 295ab84be7eSDavid Ahern break; 296ab84be7eSDavid Ahern 297ab84be7eSDavid Ahern if (!nexthop_find_by_id(net, net->nexthop.last_id_allocated)) 298ab84be7eSDavid Ahern return net->nexthop.last_id_allocated; 299ab84be7eSDavid Ahern } 300ab84be7eSDavid Ahern return 0; 301ab84be7eSDavid Ahern } 302ab84be7eSDavid Ahern 303430a0491SDavid Ahern static int nla_put_nh_group(struct sk_buff *skb, struct nh_group *nhg) 304430a0491SDavid Ahern { 305430a0491SDavid Ahern struct nexthop_grp *p; 306430a0491SDavid Ahern size_t len = nhg->num_nh * sizeof(*p); 307430a0491SDavid Ahern struct nlattr *nla; 308430a0491SDavid Ahern u16 group_type = 0; 309430a0491SDavid Ahern int i; 310430a0491SDavid Ahern 311430a0491SDavid Ahern if (nhg->mpath) 312430a0491SDavid Ahern group_type = NEXTHOP_GRP_TYPE_MPATH; 313430a0491SDavid Ahern 314430a0491SDavid Ahern if (nla_put_u16(skb, NHA_GROUP_TYPE, group_type)) 315430a0491SDavid Ahern goto nla_put_failure; 316430a0491SDavid Ahern 317430a0491SDavid Ahern nla = nla_reserve(skb, NHA_GROUP, len); 318430a0491SDavid Ahern if (!nla) 319430a0491SDavid Ahern goto nla_put_failure; 320430a0491SDavid Ahern 321430a0491SDavid Ahern p = nla_data(nla); 322430a0491SDavid Ahern for (i = 0; i < nhg->num_nh; ++i) { 323430a0491SDavid Ahern p->id = nhg->nh_entries[i].nh->id; 324430a0491SDavid Ahern p->weight = nhg->nh_entries[i].weight - 1; 325430a0491SDavid Ahern p += 1; 326430a0491SDavid Ahern } 327430a0491SDavid Ahern 328430a0491SDavid Ahern return 0; 329430a0491SDavid Ahern 330430a0491SDavid Ahern nla_put_failure: 331430a0491SDavid Ahern return -EMSGSIZE; 332430a0491SDavid Ahern } 333430a0491SDavid Ahern 334ab84be7eSDavid Ahern static int nh_fill_node(struct sk_buff *skb, struct nexthop *nh, 335ab84be7eSDavid Ahern int event, u32 portid, u32 seq, unsigned int nlflags) 336ab84be7eSDavid Ahern { 33753010f99SDavid Ahern struct fib6_nh *fib6_nh; 338597cfe4fSDavid Ahern struct fib_nh *fib_nh; 339ab84be7eSDavid Ahern struct nlmsghdr *nlh; 340ab84be7eSDavid Ahern struct nh_info *nhi; 341ab84be7eSDavid Ahern struct nhmsg *nhm; 342ab84be7eSDavid Ahern 343ab84be7eSDavid Ahern nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nhm), nlflags); 344ab84be7eSDavid Ahern if (!nlh) 345ab84be7eSDavid Ahern return -EMSGSIZE; 346ab84be7eSDavid Ahern 347ab84be7eSDavid Ahern nhm = nlmsg_data(nlh); 348ab84be7eSDavid Ahern nhm->nh_family = AF_UNSPEC; 349ab84be7eSDavid Ahern nhm->nh_flags = nh->nh_flags; 350ab84be7eSDavid Ahern nhm->nh_protocol = nh->protocol; 351ab84be7eSDavid Ahern nhm->nh_scope = 0; 352ab84be7eSDavid Ahern nhm->resvd = 0; 353ab84be7eSDavid Ahern 354ab84be7eSDavid Ahern if (nla_put_u32(skb, NHA_ID, nh->id)) 355ab84be7eSDavid Ahern goto nla_put_failure; 356ab84be7eSDavid Ahern 357430a0491SDavid Ahern if (nh->is_group) { 358430a0491SDavid Ahern struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 359430a0491SDavid Ahern 360ce9ac056SDavid Ahern if (nhg->fdb_nh && nla_put_flag(skb, NHA_FDB)) 361ce9ac056SDavid Ahern goto nla_put_failure; 362430a0491SDavid Ahern if (nla_put_nh_group(skb, nhg)) 363430a0491SDavid Ahern goto nla_put_failure; 364430a0491SDavid Ahern goto out; 365430a0491SDavid Ahern } 366430a0491SDavid Ahern 367ab84be7eSDavid Ahern nhi = rtnl_dereference(nh->nh_info); 368ab84be7eSDavid Ahern nhm->nh_family = nhi->family; 369ab84be7eSDavid Ahern if (nhi->reject_nh) { 370ab84be7eSDavid Ahern if (nla_put_flag(skb, NHA_BLACKHOLE)) 371ab84be7eSDavid Ahern goto nla_put_failure; 372ab84be7eSDavid Ahern goto out; 373ce9ac056SDavid Ahern } else if (nhi->fdb_nh) { 374ce9ac056SDavid Ahern if (nla_put_flag(skb, NHA_FDB)) 375ce9ac056SDavid Ahern goto nla_put_failure; 376ce9ac056SDavid Ahern } else { 377597cfe4fSDavid Ahern const struct net_device *dev; 378597cfe4fSDavid Ahern 379597cfe4fSDavid Ahern dev = nhi->fib_nhc.nhc_dev; 380597cfe4fSDavid Ahern if (dev && nla_put_u32(skb, NHA_OIF, dev->ifindex)) 381597cfe4fSDavid Ahern goto nla_put_failure; 382597cfe4fSDavid Ahern } 383597cfe4fSDavid Ahern 384597cfe4fSDavid Ahern nhm->nh_scope = nhi->fib_nhc.nhc_scope; 385597cfe4fSDavid Ahern switch (nhi->family) { 386597cfe4fSDavid Ahern case AF_INET: 387597cfe4fSDavid Ahern fib_nh = &nhi->fib_nh; 388597cfe4fSDavid Ahern if (fib_nh->fib_nh_gw_family && 38933d80996SIdo Schimmel nla_put_be32(skb, NHA_GATEWAY, fib_nh->fib_nh_gw4)) 390597cfe4fSDavid Ahern goto nla_put_failure; 391597cfe4fSDavid Ahern break; 39253010f99SDavid Ahern 39353010f99SDavid Ahern case AF_INET6: 39453010f99SDavid Ahern fib6_nh = &nhi->fib6_nh; 39553010f99SDavid Ahern if (fib6_nh->fib_nh_gw_family && 39653010f99SDavid Ahern nla_put_in6_addr(skb, NHA_GATEWAY, &fib6_nh->fib_nh_gw6)) 39753010f99SDavid Ahern goto nla_put_failure; 39853010f99SDavid Ahern break; 399ab84be7eSDavid Ahern } 400ab84be7eSDavid Ahern 401b513bd03SDavid Ahern if (nhi->fib_nhc.nhc_lwtstate && 402b513bd03SDavid Ahern lwtunnel_fill_encap(skb, nhi->fib_nhc.nhc_lwtstate, 403b513bd03SDavid Ahern NHA_ENCAP, NHA_ENCAP_TYPE) < 0) 404b513bd03SDavid Ahern goto nla_put_failure; 405b513bd03SDavid Ahern 406ab84be7eSDavid Ahern out: 407ab84be7eSDavid Ahern nlmsg_end(skb, nlh); 408ab84be7eSDavid Ahern return 0; 409ab84be7eSDavid Ahern 410ab84be7eSDavid Ahern nla_put_failure: 411d69100b8SStephen Worley nlmsg_cancel(skb, nlh); 412ab84be7eSDavid Ahern return -EMSGSIZE; 413ab84be7eSDavid Ahern } 414ab84be7eSDavid Ahern 415430a0491SDavid Ahern static size_t nh_nlmsg_size_grp(struct nexthop *nh) 416430a0491SDavid Ahern { 417430a0491SDavid Ahern struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 418430a0491SDavid Ahern size_t sz = sizeof(struct nexthop_grp) * nhg->num_nh; 419430a0491SDavid Ahern 420430a0491SDavid Ahern return nla_total_size(sz) + 421430a0491SDavid Ahern nla_total_size(2); /* NHA_GROUP_TYPE */ 422430a0491SDavid Ahern } 423430a0491SDavid Ahern 424430a0491SDavid Ahern static size_t nh_nlmsg_size_single(struct nexthop *nh) 425ab84be7eSDavid Ahern { 426597cfe4fSDavid Ahern struct nh_info *nhi = rtnl_dereference(nh->nh_info); 427430a0491SDavid Ahern size_t sz; 428ab84be7eSDavid Ahern 429ab84be7eSDavid Ahern /* covers NHA_BLACKHOLE since NHA_OIF and BLACKHOLE 430ab84be7eSDavid Ahern * are mutually exclusive 431ab84be7eSDavid Ahern */ 432430a0491SDavid Ahern sz = nla_total_size(4); /* NHA_OIF */ 433ab84be7eSDavid Ahern 434597cfe4fSDavid Ahern switch (nhi->family) { 435597cfe4fSDavid Ahern case AF_INET: 436597cfe4fSDavid Ahern if (nhi->fib_nh.fib_nh_gw_family) 437597cfe4fSDavid Ahern sz += nla_total_size(4); /* NHA_GATEWAY */ 438597cfe4fSDavid Ahern break; 43953010f99SDavid Ahern 44053010f99SDavid Ahern case AF_INET6: 44153010f99SDavid Ahern /* NHA_GATEWAY */ 44253010f99SDavid Ahern if (nhi->fib6_nh.fib_nh_gw_family) 44353010f99SDavid Ahern sz += nla_total_size(sizeof(const struct in6_addr)); 44453010f99SDavid Ahern break; 445597cfe4fSDavid Ahern } 446597cfe4fSDavid Ahern 447b513bd03SDavid Ahern if (nhi->fib_nhc.nhc_lwtstate) { 448b513bd03SDavid Ahern sz += lwtunnel_get_encap_size(nhi->fib_nhc.nhc_lwtstate); 449b513bd03SDavid Ahern sz += nla_total_size(2); /* NHA_ENCAP_TYPE */ 450b513bd03SDavid Ahern } 451b513bd03SDavid Ahern 452ab84be7eSDavid Ahern return sz; 453ab84be7eSDavid Ahern } 454ab84be7eSDavid Ahern 455430a0491SDavid Ahern static size_t nh_nlmsg_size(struct nexthop *nh) 456430a0491SDavid Ahern { 457f9e95555SStephen Worley size_t sz = NLMSG_ALIGN(sizeof(struct nhmsg)); 458f9e95555SStephen Worley 459f9e95555SStephen Worley sz += nla_total_size(4); /* NHA_ID */ 460430a0491SDavid Ahern 461430a0491SDavid Ahern if (nh->is_group) 462430a0491SDavid Ahern sz += nh_nlmsg_size_grp(nh); 463430a0491SDavid Ahern else 464430a0491SDavid Ahern sz += nh_nlmsg_size_single(nh); 465430a0491SDavid Ahern 466430a0491SDavid Ahern return sz; 467430a0491SDavid Ahern } 468430a0491SDavid Ahern 469ab84be7eSDavid Ahern static void nexthop_notify(int event, struct nexthop *nh, struct nl_info *info) 470ab84be7eSDavid Ahern { 471ab84be7eSDavid Ahern unsigned int nlflags = info->nlh ? info->nlh->nlmsg_flags : 0; 472ab84be7eSDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 473ab84be7eSDavid Ahern struct sk_buff *skb; 474ab84be7eSDavid Ahern int err = -ENOBUFS; 475ab84be7eSDavid Ahern 476ab84be7eSDavid Ahern skb = nlmsg_new(nh_nlmsg_size(nh), gfp_any()); 477ab84be7eSDavid Ahern if (!skb) 478ab84be7eSDavid Ahern goto errout; 479ab84be7eSDavid Ahern 480ab84be7eSDavid Ahern err = nh_fill_node(skb, nh, event, info->portid, seq, nlflags); 481ab84be7eSDavid Ahern if (err < 0) { 482ab84be7eSDavid Ahern /* -EMSGSIZE implies BUG in nh_nlmsg_size() */ 483ab84be7eSDavid Ahern WARN_ON(err == -EMSGSIZE); 484ab84be7eSDavid Ahern kfree_skb(skb); 485ab84be7eSDavid Ahern goto errout; 486ab84be7eSDavid Ahern } 487ab84be7eSDavid Ahern 488ab84be7eSDavid Ahern rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_NEXTHOP, 489ab84be7eSDavid Ahern info->nlh, gfp_any()); 490ab84be7eSDavid Ahern return; 491ab84be7eSDavid Ahern errout: 492ab84be7eSDavid Ahern if (err < 0) 493ab84be7eSDavid Ahern rtnl_set_sk_err(info->nl_net, RTNLGRP_NEXTHOP, err); 494ab84be7eSDavid Ahern } 495ab84be7eSDavid Ahern 496430a0491SDavid Ahern static bool valid_group_nh(struct nexthop *nh, unsigned int npaths, 497ce9ac056SDavid Ahern bool *is_fdb, struct netlink_ext_ack *extack) 498597cfe4fSDavid Ahern { 499430a0491SDavid Ahern if (nh->is_group) { 500430a0491SDavid Ahern struct nh_group *nhg = rtnl_dereference(nh->nh_grp); 501430a0491SDavid Ahern 502430a0491SDavid Ahern /* nested multipath (group within a group) is not 503430a0491SDavid Ahern * supported 504430a0491SDavid Ahern */ 505430a0491SDavid Ahern if (nhg->mpath) { 506430a0491SDavid Ahern NL_SET_ERR_MSG(extack, 507430a0491SDavid Ahern "Multipath group can not be a nexthop within a group"); 508430a0491SDavid Ahern return false; 509430a0491SDavid Ahern } 510ce9ac056SDavid Ahern *is_fdb = nhg->fdb_nh; 511430a0491SDavid Ahern } else { 512430a0491SDavid Ahern struct nh_info *nhi = rtnl_dereference(nh->nh_info); 513430a0491SDavid Ahern 514430a0491SDavid Ahern if (nhi->reject_nh && npaths > 1) { 515430a0491SDavid Ahern NL_SET_ERR_MSG(extack, 516430a0491SDavid Ahern "Blackhole nexthop can not be used in a group with more than 1 path"); 517430a0491SDavid Ahern return false; 518430a0491SDavid Ahern } 519ce9ac056SDavid Ahern *is_fdb = nhi->fdb_nh; 520430a0491SDavid Ahern } 521430a0491SDavid Ahern 522430a0491SDavid Ahern return true; 523430a0491SDavid Ahern } 524430a0491SDavid Ahern 52538428d68SRoopa Prabhu static int nh_check_attr_fdb_group(struct nexthop *nh, u8 *nh_family, 52638428d68SRoopa Prabhu struct netlink_ext_ack *extack) 52738428d68SRoopa Prabhu { 52838428d68SRoopa Prabhu struct nh_info *nhi; 52938428d68SRoopa Prabhu 530ce9ac056SDavid Ahern nhi = rtnl_dereference(nh->nh_info); 531ce9ac056SDavid Ahern 532ce9ac056SDavid Ahern if (!nhi->fdb_nh) { 53338428d68SRoopa Prabhu NL_SET_ERR_MSG(extack, "FDB nexthop group can only have fdb nexthops"); 53438428d68SRoopa Prabhu return -EINVAL; 53538428d68SRoopa Prabhu } 53638428d68SRoopa Prabhu 53738428d68SRoopa Prabhu if (*nh_family == AF_UNSPEC) { 53838428d68SRoopa Prabhu *nh_family = nhi->family; 53938428d68SRoopa Prabhu } else if (*nh_family != nhi->family) { 54038428d68SRoopa Prabhu NL_SET_ERR_MSG(extack, "FDB nexthop group cannot have mixed family nexthops"); 54138428d68SRoopa Prabhu return -EINVAL; 54238428d68SRoopa Prabhu } 54338428d68SRoopa Prabhu 54438428d68SRoopa Prabhu return 0; 54538428d68SRoopa Prabhu } 54638428d68SRoopa Prabhu 547430a0491SDavid Ahern static int nh_check_attr_group(struct net *net, struct nlattr *tb[], 548430a0491SDavid Ahern struct netlink_ext_ack *extack) 549430a0491SDavid Ahern { 550430a0491SDavid Ahern unsigned int len = nla_len(tb[NHA_GROUP]); 55138428d68SRoopa Prabhu u8 nh_family = AF_UNSPEC; 552430a0491SDavid Ahern struct nexthop_grp *nhg; 553430a0491SDavid Ahern unsigned int i, j; 55438428d68SRoopa Prabhu u8 nhg_fdb = 0; 555430a0491SDavid Ahern 556eeaac363SNikolay Aleksandrov if (!len || len & (sizeof(struct nexthop_grp) - 1)) { 557430a0491SDavid Ahern NL_SET_ERR_MSG(extack, 558430a0491SDavid Ahern "Invalid length for nexthop group attribute"); 559430a0491SDavid Ahern return -EINVAL; 560430a0491SDavid Ahern } 561430a0491SDavid Ahern 562430a0491SDavid Ahern /* convert len to number of nexthop ids */ 563430a0491SDavid Ahern len /= sizeof(*nhg); 564430a0491SDavid Ahern 565430a0491SDavid Ahern nhg = nla_data(tb[NHA_GROUP]); 566430a0491SDavid Ahern for (i = 0; i < len; ++i) { 567430a0491SDavid Ahern if (nhg[i].resvd1 || nhg[i].resvd2) { 568430a0491SDavid Ahern NL_SET_ERR_MSG(extack, "Reserved fields in nexthop_grp must be 0"); 569430a0491SDavid Ahern return -EINVAL; 570430a0491SDavid Ahern } 571430a0491SDavid Ahern if (nhg[i].weight > 254) { 572430a0491SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid value for weight"); 573430a0491SDavid Ahern return -EINVAL; 574430a0491SDavid Ahern } 575430a0491SDavid Ahern for (j = i + 1; j < len; ++j) { 576430a0491SDavid Ahern if (nhg[i].id == nhg[j].id) { 577430a0491SDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop id can not be used twice in a group"); 578430a0491SDavid Ahern return -EINVAL; 579430a0491SDavid Ahern } 580430a0491SDavid Ahern } 581430a0491SDavid Ahern } 582430a0491SDavid Ahern 58338428d68SRoopa Prabhu if (tb[NHA_FDB]) 58438428d68SRoopa Prabhu nhg_fdb = 1; 585430a0491SDavid Ahern nhg = nla_data(tb[NHA_GROUP]); 586430a0491SDavid Ahern for (i = 0; i < len; ++i) { 587430a0491SDavid Ahern struct nexthop *nh; 588ce9ac056SDavid Ahern bool is_fdb_nh; 589430a0491SDavid Ahern 590430a0491SDavid Ahern nh = nexthop_find_by_id(net, nhg[i].id); 591430a0491SDavid Ahern if (!nh) { 592430a0491SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid nexthop id"); 593430a0491SDavid Ahern return -EINVAL; 594430a0491SDavid Ahern } 595ce9ac056SDavid Ahern if (!valid_group_nh(nh, len, &is_fdb_nh, extack)) 596430a0491SDavid Ahern return -EINVAL; 59738428d68SRoopa Prabhu 59838428d68SRoopa Prabhu if (nhg_fdb && nh_check_attr_fdb_group(nh, &nh_family, extack)) 59938428d68SRoopa Prabhu return -EINVAL; 60038428d68SRoopa Prabhu 601ce9ac056SDavid Ahern if (!nhg_fdb && is_fdb_nh) { 60238428d68SRoopa Prabhu NL_SET_ERR_MSG(extack, "Non FDB nexthop group cannot have fdb nexthops"); 60338428d68SRoopa Prabhu return -EINVAL; 60438428d68SRoopa Prabhu } 605430a0491SDavid Ahern } 60684be69b8SDavid Ahern for (i = NHA_GROUP_TYPE + 1; i < __NHA_MAX; ++i) { 607430a0491SDavid Ahern if (!tb[i]) 608430a0491SDavid Ahern continue; 60938428d68SRoopa Prabhu if (tb[NHA_FDB]) 61038428d68SRoopa Prabhu continue; 611430a0491SDavid Ahern NL_SET_ERR_MSG(extack, 612430a0491SDavid Ahern "No other attributes can be set in nexthop groups"); 613430a0491SDavid Ahern return -EINVAL; 614430a0491SDavid Ahern } 615430a0491SDavid Ahern 616430a0491SDavid Ahern return 0; 617430a0491SDavid Ahern } 618430a0491SDavid Ahern 619430a0491SDavid Ahern static bool ipv6_good_nh(const struct fib6_nh *nh) 620430a0491SDavid Ahern { 621430a0491SDavid Ahern int state = NUD_REACHABLE; 622430a0491SDavid Ahern struct neighbour *n; 623430a0491SDavid Ahern 624430a0491SDavid Ahern rcu_read_lock_bh(); 625430a0491SDavid Ahern 626430a0491SDavid Ahern n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev, &nh->fib_nh_gw6); 627430a0491SDavid Ahern if (n) 628430a0491SDavid Ahern state = n->nud_state; 629430a0491SDavid Ahern 630430a0491SDavid Ahern rcu_read_unlock_bh(); 631430a0491SDavid Ahern 632430a0491SDavid Ahern return !!(state & NUD_VALID); 633430a0491SDavid Ahern } 634430a0491SDavid Ahern 635430a0491SDavid Ahern static bool ipv4_good_nh(const struct fib_nh *nh) 636430a0491SDavid Ahern { 637430a0491SDavid Ahern int state = NUD_REACHABLE; 638430a0491SDavid Ahern struct neighbour *n; 639430a0491SDavid Ahern 640430a0491SDavid Ahern rcu_read_lock_bh(); 641430a0491SDavid Ahern 642430a0491SDavid Ahern n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev, 643430a0491SDavid Ahern (__force u32)nh->fib_nh_gw4); 644430a0491SDavid Ahern if (n) 645430a0491SDavid Ahern state = n->nud_state; 646430a0491SDavid Ahern 647430a0491SDavid Ahern rcu_read_unlock_bh(); 648430a0491SDavid Ahern 649430a0491SDavid Ahern return !!(state & NUD_VALID); 650430a0491SDavid Ahern } 651430a0491SDavid Ahern 652430a0491SDavid Ahern struct nexthop *nexthop_select_path(struct nexthop *nh, int hash) 653430a0491SDavid Ahern { 654430a0491SDavid Ahern struct nexthop *rc = NULL; 655430a0491SDavid Ahern struct nh_group *nhg; 656430a0491SDavid Ahern int i; 657430a0491SDavid Ahern 658430a0491SDavid Ahern if (!nh->is_group) 659430a0491SDavid Ahern return nh; 660430a0491SDavid Ahern 661430a0491SDavid Ahern nhg = rcu_dereference(nh->nh_grp); 662430a0491SDavid Ahern for (i = 0; i < nhg->num_nh; ++i) { 663430a0491SDavid Ahern struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 664430a0491SDavid Ahern struct nh_info *nhi; 665430a0491SDavid Ahern 666430a0491SDavid Ahern if (hash > atomic_read(&nhge->upper_bound)) 667430a0491SDavid Ahern continue; 668430a0491SDavid Ahern 669ce9ac056SDavid Ahern nhi = rcu_dereference(nhge->nh->nh_info); 670ce9ac056SDavid Ahern if (nhi->fdb_nh) 67138428d68SRoopa Prabhu return nhge->nh; 67238428d68SRoopa Prabhu 673430a0491SDavid Ahern /* nexthops always check if it is good and does 674430a0491SDavid Ahern * not rely on a sysctl for this behavior 675430a0491SDavid Ahern */ 676430a0491SDavid Ahern switch (nhi->family) { 677430a0491SDavid Ahern case AF_INET: 678430a0491SDavid Ahern if (ipv4_good_nh(&nhi->fib_nh)) 679430a0491SDavid Ahern return nhge->nh; 680430a0491SDavid Ahern break; 681430a0491SDavid Ahern case AF_INET6: 682430a0491SDavid Ahern if (ipv6_good_nh(&nhi->fib6_nh)) 683430a0491SDavid Ahern return nhge->nh; 684430a0491SDavid Ahern break; 685430a0491SDavid Ahern } 686430a0491SDavid Ahern 687430a0491SDavid Ahern if (!rc) 688430a0491SDavid Ahern rc = nhge->nh; 689430a0491SDavid Ahern } 690430a0491SDavid Ahern 691430a0491SDavid Ahern return rc; 692430a0491SDavid Ahern } 693430a0491SDavid Ahern EXPORT_SYMBOL_GPL(nexthop_select_path); 694430a0491SDavid Ahern 695f88c9aa1SDavid Ahern int nexthop_for_each_fib6_nh(struct nexthop *nh, 696f88c9aa1SDavid Ahern int (*cb)(struct fib6_nh *nh, void *arg), 697f88c9aa1SDavid Ahern void *arg) 698f88c9aa1SDavid Ahern { 699f88c9aa1SDavid Ahern struct nh_info *nhi; 700f88c9aa1SDavid Ahern int err; 701f88c9aa1SDavid Ahern 702f88c9aa1SDavid Ahern if (nh->is_group) { 703f88c9aa1SDavid Ahern struct nh_group *nhg; 704f88c9aa1SDavid Ahern int i; 705f88c9aa1SDavid Ahern 706f88c9aa1SDavid Ahern nhg = rcu_dereference_rtnl(nh->nh_grp); 707f88c9aa1SDavid Ahern for (i = 0; i < nhg->num_nh; i++) { 708f88c9aa1SDavid Ahern struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 709f88c9aa1SDavid Ahern 710f88c9aa1SDavid Ahern nhi = rcu_dereference_rtnl(nhge->nh->nh_info); 711f88c9aa1SDavid Ahern err = cb(&nhi->fib6_nh, arg); 712f88c9aa1SDavid Ahern if (err) 713f88c9aa1SDavid Ahern return err; 714f88c9aa1SDavid Ahern } 715f88c9aa1SDavid Ahern } else { 716f88c9aa1SDavid Ahern nhi = rcu_dereference_rtnl(nh->nh_info); 717f88c9aa1SDavid Ahern err = cb(&nhi->fib6_nh, arg); 718f88c9aa1SDavid Ahern if (err) 719f88c9aa1SDavid Ahern return err; 720f88c9aa1SDavid Ahern } 721f88c9aa1SDavid Ahern 722f88c9aa1SDavid Ahern return 0; 723f88c9aa1SDavid Ahern } 724f88c9aa1SDavid Ahern EXPORT_SYMBOL_GPL(nexthop_for_each_fib6_nh); 725f88c9aa1SDavid Ahern 7267bf4796dSDavid Ahern static int check_src_addr(const struct in6_addr *saddr, 7277bf4796dSDavid Ahern struct netlink_ext_ack *extack) 7287bf4796dSDavid Ahern { 7297bf4796dSDavid Ahern if (!ipv6_addr_any(saddr)) { 7307bf4796dSDavid Ahern NL_SET_ERR_MSG(extack, "IPv6 routes using source address can not use nexthop objects"); 7317bf4796dSDavid Ahern return -EINVAL; 7327bf4796dSDavid Ahern } 7337bf4796dSDavid Ahern return 0; 7347bf4796dSDavid Ahern } 7357bf4796dSDavid Ahern 736f88d8ea6SDavid Ahern int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg, 737f88d8ea6SDavid Ahern struct netlink_ext_ack *extack) 738f88d8ea6SDavid Ahern { 739f88d8ea6SDavid Ahern struct nh_info *nhi; 740ce9ac056SDavid Ahern bool is_fdb_nh; 74138428d68SRoopa Prabhu 742f88d8ea6SDavid Ahern /* fib6_src is unique to a fib6_info and limits the ability to cache 743f88d8ea6SDavid Ahern * routes in fib6_nh within a nexthop that is potentially shared 744f88d8ea6SDavid Ahern * across multiple fib entries. If the config wants to use source 745f88d8ea6SDavid Ahern * routing it can not use nexthop objects. mlxsw also does not allow 746f88d8ea6SDavid Ahern * fib6_src on routes. 747f88d8ea6SDavid Ahern */ 7487bf4796dSDavid Ahern if (cfg && check_src_addr(&cfg->fc_src, extack) < 0) 749f88d8ea6SDavid Ahern return -EINVAL; 750f88d8ea6SDavid Ahern 751f88d8ea6SDavid Ahern if (nh->is_group) { 752f88d8ea6SDavid Ahern struct nh_group *nhg; 753f88d8ea6SDavid Ahern 754f88d8ea6SDavid Ahern nhg = rtnl_dereference(nh->nh_grp); 755f88d8ea6SDavid Ahern if (nhg->has_v4) 756f88d8ea6SDavid Ahern goto no_v4_nh; 757ce9ac056SDavid Ahern is_fdb_nh = nhg->fdb_nh; 758f88d8ea6SDavid Ahern } else { 759f88d8ea6SDavid Ahern nhi = rtnl_dereference(nh->nh_info); 760f88d8ea6SDavid Ahern if (nhi->family == AF_INET) 761f88d8ea6SDavid Ahern goto no_v4_nh; 762ce9ac056SDavid Ahern is_fdb_nh = nhi->fdb_nh; 763ce9ac056SDavid Ahern } 764ce9ac056SDavid Ahern 765ce9ac056SDavid Ahern if (is_fdb_nh) { 766ce9ac056SDavid Ahern NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop"); 767ce9ac056SDavid Ahern return -EINVAL; 768f88d8ea6SDavid Ahern } 769f88d8ea6SDavid Ahern 770f88d8ea6SDavid Ahern return 0; 771f88d8ea6SDavid Ahern no_v4_nh: 772f88d8ea6SDavid Ahern NL_SET_ERR_MSG(extack, "IPv6 routes can not use an IPv4 nexthop"); 773f88d8ea6SDavid Ahern return -EINVAL; 774f88d8ea6SDavid Ahern } 775f88d8ea6SDavid Ahern EXPORT_SYMBOL_GPL(fib6_check_nexthop); 776f88d8ea6SDavid Ahern 7777bf4796dSDavid Ahern /* if existing nexthop has ipv6 routes linked to it, need 7787bf4796dSDavid Ahern * to verify this new spec works with ipv6 7797bf4796dSDavid Ahern */ 7807bf4796dSDavid Ahern static int fib6_check_nh_list(struct nexthop *old, struct nexthop *new, 7817bf4796dSDavid Ahern struct netlink_ext_ack *extack) 7827bf4796dSDavid Ahern { 7837bf4796dSDavid Ahern struct fib6_info *f6i; 7847bf4796dSDavid Ahern 7857bf4796dSDavid Ahern if (list_empty(&old->f6i_list)) 7867bf4796dSDavid Ahern return 0; 7877bf4796dSDavid Ahern 7887bf4796dSDavid Ahern list_for_each_entry(f6i, &old->f6i_list, nh_list) { 7897bf4796dSDavid Ahern if (check_src_addr(&f6i->fib6_src.addr, extack) < 0) 7907bf4796dSDavid Ahern return -EINVAL; 7917bf4796dSDavid Ahern } 7927bf4796dSDavid Ahern 7937bf4796dSDavid Ahern return fib6_check_nexthop(new, NULL, extack); 7947bf4796dSDavid Ahern } 7957bf4796dSDavid Ahern 796ce9ac056SDavid Ahern static int nexthop_check_scope(struct nh_info *nhi, u8 scope, 7974c7e8084SDavid Ahern struct netlink_ext_ack *extack) 7984c7e8084SDavid Ahern { 7994c7e8084SDavid Ahern if (scope == RT_SCOPE_HOST && nhi->fib_nhc.nhc_gw_family) { 8004c7e8084SDavid Ahern NL_SET_ERR_MSG(extack, 8014c7e8084SDavid Ahern "Route with host scope can not have a gateway"); 8024c7e8084SDavid Ahern return -EINVAL; 8034c7e8084SDavid Ahern } 8044c7e8084SDavid Ahern 8054c7e8084SDavid Ahern if (nhi->fib_nhc.nhc_flags & RTNH_F_ONLINK && scope >= RT_SCOPE_LINK) { 8064c7e8084SDavid Ahern NL_SET_ERR_MSG(extack, "Scope mismatch with nexthop"); 8074c7e8084SDavid Ahern return -EINVAL; 8084c7e8084SDavid Ahern } 8094c7e8084SDavid Ahern 8104c7e8084SDavid Ahern return 0; 8114c7e8084SDavid Ahern } 8124c7e8084SDavid Ahern 8134c7e8084SDavid Ahern /* Invoked by fib add code to verify nexthop by id is ok with 8144c7e8084SDavid Ahern * config for prefix; parts of fib_check_nh not done when nexthop 8154c7e8084SDavid Ahern * object is used. 8164c7e8084SDavid Ahern */ 8174c7e8084SDavid Ahern int fib_check_nexthop(struct nexthop *nh, u8 scope, 8184c7e8084SDavid Ahern struct netlink_ext_ack *extack) 8194c7e8084SDavid Ahern { 820ce9ac056SDavid Ahern struct nh_info *nhi; 8214c7e8084SDavid Ahern int err = 0; 8224c7e8084SDavid Ahern 823ce9ac056SDavid Ahern if (nh->is_group) { 824ce9ac056SDavid Ahern struct nh_group *nhg; 825ce9ac056SDavid Ahern 826ce9ac056SDavid Ahern nhg = rtnl_dereference(nh->nh_grp); 827ce9ac056SDavid Ahern if (nhg->fdb_nh) { 82838428d68SRoopa Prabhu NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop"); 82938428d68SRoopa Prabhu err = -EINVAL; 83038428d68SRoopa Prabhu goto out; 83138428d68SRoopa Prabhu } 83238428d68SRoopa Prabhu 8334c7e8084SDavid Ahern if (scope == RT_SCOPE_HOST) { 8344c7e8084SDavid Ahern NL_SET_ERR_MSG(extack, "Route with host scope can not have multiple nexthops"); 8354c7e8084SDavid Ahern err = -EINVAL; 8364c7e8084SDavid Ahern goto out; 8374c7e8084SDavid Ahern } 8384c7e8084SDavid Ahern 8394c7e8084SDavid Ahern /* all nexthops in a group have the same scope */ 840ce9ac056SDavid Ahern nhi = rtnl_dereference(nhg->nh_entries[0].nh->nh_info); 841ce9ac056SDavid Ahern err = nexthop_check_scope(nhi, scope, extack); 8424c7e8084SDavid Ahern } else { 843ce9ac056SDavid Ahern nhi = rtnl_dereference(nh->nh_info); 844ce9ac056SDavid Ahern if (nhi->fdb_nh) { 845ce9ac056SDavid Ahern NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop"); 846ce9ac056SDavid Ahern err = -EINVAL; 847ce9ac056SDavid Ahern goto out; 8484c7e8084SDavid Ahern } 849ce9ac056SDavid Ahern err = nexthop_check_scope(nhi, scope, extack); 850ce9ac056SDavid Ahern } 851ce9ac056SDavid Ahern 8524c7e8084SDavid Ahern out: 8534c7e8084SDavid Ahern return err; 8544c7e8084SDavid Ahern } 8554c7e8084SDavid Ahern 8567bf4796dSDavid Ahern static int fib_check_nh_list(struct nexthop *old, struct nexthop *new, 8577bf4796dSDavid Ahern struct netlink_ext_ack *extack) 8587bf4796dSDavid Ahern { 8597bf4796dSDavid Ahern struct fib_info *fi; 8607bf4796dSDavid Ahern 8617bf4796dSDavid Ahern list_for_each_entry(fi, &old->fi_list, nh_list) { 8627bf4796dSDavid Ahern int err; 8637bf4796dSDavid Ahern 8647bf4796dSDavid Ahern err = fib_check_nexthop(new, fi->fib_scope, extack); 8657bf4796dSDavid Ahern if (err) 8667bf4796dSDavid Ahern return err; 8677bf4796dSDavid Ahern } 8687bf4796dSDavid Ahern return 0; 8697bf4796dSDavid Ahern } 8707bf4796dSDavid Ahern 871430a0491SDavid Ahern static void nh_group_rebalance(struct nh_group *nhg) 872430a0491SDavid Ahern { 873430a0491SDavid Ahern int total = 0; 874430a0491SDavid Ahern int w = 0; 875430a0491SDavid Ahern int i; 876430a0491SDavid Ahern 877430a0491SDavid Ahern for (i = 0; i < nhg->num_nh; ++i) 878430a0491SDavid Ahern total += nhg->nh_entries[i].weight; 879430a0491SDavid Ahern 880430a0491SDavid Ahern for (i = 0; i < nhg->num_nh; ++i) { 881430a0491SDavid Ahern struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 882430a0491SDavid Ahern int upper_bound; 883430a0491SDavid Ahern 884430a0491SDavid Ahern w += nhge->weight; 885430a0491SDavid Ahern upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31, total) - 1; 886430a0491SDavid Ahern atomic_set(&nhge->upper_bound, upper_bound); 887430a0491SDavid Ahern } 888430a0491SDavid Ahern } 889430a0491SDavid Ahern 890ac21753aSDavid Ahern static void remove_nh_grp_entry(struct net *net, struct nh_grp_entry *nhge, 891430a0491SDavid Ahern struct nl_info *nlinfo) 892430a0491SDavid Ahern { 89390f33bffSNikolay Aleksandrov struct nh_grp_entry *nhges, *new_nhges; 894ac21753aSDavid Ahern struct nexthop *nhp = nhge->nh_parent; 895430a0491SDavid Ahern struct nexthop *nh = nhge->nh; 89690f33bffSNikolay Aleksandrov struct nh_group *nhg, *newg; 89790f33bffSNikolay Aleksandrov int i, j; 898430a0491SDavid Ahern 899430a0491SDavid Ahern WARN_ON(!nh); 900430a0491SDavid Ahern 901ac21753aSDavid Ahern nhg = rtnl_dereference(nhp->nh_grp); 90290f33bffSNikolay Aleksandrov newg = nhg->spare; 903430a0491SDavid Ahern 90490f33bffSNikolay Aleksandrov /* last entry, keep it visible and remove the parent */ 90590f33bffSNikolay Aleksandrov if (nhg->num_nh == 1) { 90690f33bffSNikolay Aleksandrov remove_nexthop(net, nhp, nlinfo); 907430a0491SDavid Ahern return; 90890f33bffSNikolay Aleksandrov } 909430a0491SDavid Ahern 910863b2558SIdo Schimmel newg->has_v4 = false; 91190f33bffSNikolay Aleksandrov newg->mpath = nhg->mpath; 912ce9ac056SDavid Ahern newg->fdb_nh = nhg->fdb_nh; 91390f33bffSNikolay Aleksandrov newg->num_nh = nhg->num_nh; 914430a0491SDavid Ahern 91590f33bffSNikolay Aleksandrov /* copy old entries to new except the one getting removed */ 91690f33bffSNikolay Aleksandrov nhges = nhg->nh_entries; 91790f33bffSNikolay Aleksandrov new_nhges = newg->nh_entries; 91890f33bffSNikolay Aleksandrov for (i = 0, j = 0; i < nhg->num_nh; ++i) { 919863b2558SIdo Schimmel struct nh_info *nhi; 920863b2558SIdo Schimmel 92190f33bffSNikolay Aleksandrov /* current nexthop getting removed */ 92290f33bffSNikolay Aleksandrov if (nhg->nh_entries[i].nh == nh) { 92390f33bffSNikolay Aleksandrov newg->num_nh--; 92490f33bffSNikolay Aleksandrov continue; 92590f33bffSNikolay Aleksandrov } 926430a0491SDavid Ahern 927863b2558SIdo Schimmel nhi = rtnl_dereference(nhges[i].nh->nh_info); 928863b2558SIdo Schimmel if (nhi->family == AF_INET) 929863b2558SIdo Schimmel newg->has_v4 = true; 930863b2558SIdo Schimmel 93190f33bffSNikolay Aleksandrov list_del(&nhges[i].nh_list); 93290f33bffSNikolay Aleksandrov new_nhges[j].nh_parent = nhges[i].nh_parent; 93390f33bffSNikolay Aleksandrov new_nhges[j].nh = nhges[i].nh; 93490f33bffSNikolay Aleksandrov new_nhges[j].weight = nhges[i].weight; 93590f33bffSNikolay Aleksandrov list_add(&new_nhges[j].nh_list, &new_nhges[j].nh->grp_list); 93690f33bffSNikolay Aleksandrov j++; 93790f33bffSNikolay Aleksandrov } 93890f33bffSNikolay Aleksandrov 93990f33bffSNikolay Aleksandrov nh_group_rebalance(newg); 94090f33bffSNikolay Aleksandrov rcu_assign_pointer(nhp->nh_grp, newg); 94190f33bffSNikolay Aleksandrov 94290f33bffSNikolay Aleksandrov list_del(&nhge->nh_list); 94390f33bffSNikolay Aleksandrov nexthop_put(nhge->nh); 944430a0491SDavid Ahern 945430a0491SDavid Ahern if (nlinfo) 946ac21753aSDavid Ahern nexthop_notify(RTM_NEWNEXTHOP, nhp, nlinfo); 947430a0491SDavid Ahern } 948430a0491SDavid Ahern 949430a0491SDavid Ahern static void remove_nexthop_from_groups(struct net *net, struct nexthop *nh, 950430a0491SDavid Ahern struct nl_info *nlinfo) 951430a0491SDavid Ahern { 952430a0491SDavid Ahern struct nh_grp_entry *nhge, *tmp; 953430a0491SDavid Ahern 954ac21753aSDavid Ahern list_for_each_entry_safe(nhge, tmp, &nh->grp_list, nh_list) 955ac21753aSDavid Ahern remove_nh_grp_entry(net, nhge, nlinfo); 956430a0491SDavid Ahern 95790f33bffSNikolay Aleksandrov /* make sure all see the newly published array before releasing rtnl */ 958df6afe2fSIdo Schimmel synchronize_net(); 959430a0491SDavid Ahern } 960430a0491SDavid Ahern 961430a0491SDavid Ahern static void remove_nexthop_group(struct nexthop *nh, struct nl_info *nlinfo) 962430a0491SDavid Ahern { 963430a0491SDavid Ahern struct nh_group *nhg = rcu_dereference_rtnl(nh->nh_grp); 964430a0491SDavid Ahern int i, num_nh = nhg->num_nh; 965430a0491SDavid Ahern 966430a0491SDavid Ahern for (i = 0; i < num_nh; ++i) { 967430a0491SDavid Ahern struct nh_grp_entry *nhge = &nhg->nh_entries[i]; 968430a0491SDavid Ahern 969430a0491SDavid Ahern if (WARN_ON(!nhge->nh)) 970430a0491SDavid Ahern continue; 971430a0491SDavid Ahern 97290f33bffSNikolay Aleksandrov list_del_init(&nhge->nh_list); 973430a0491SDavid Ahern } 974430a0491SDavid Ahern } 975430a0491SDavid Ahern 9767bf4796dSDavid Ahern /* not called for nexthop replace */ 9774c7e8084SDavid Ahern static void __remove_nexthop_fib(struct net *net, struct nexthop *nh) 9784c7e8084SDavid Ahern { 979f88d8ea6SDavid Ahern struct fib6_info *f6i, *tmp; 9804c7e8084SDavid Ahern bool do_flush = false; 9814c7e8084SDavid Ahern struct fib_info *fi; 9824c7e8084SDavid Ahern 9834c7e8084SDavid Ahern list_for_each_entry(fi, &nh->fi_list, nh_list) { 9844c7e8084SDavid Ahern fi->fib_flags |= RTNH_F_DEAD; 9854c7e8084SDavid Ahern do_flush = true; 9864c7e8084SDavid Ahern } 9874c7e8084SDavid Ahern if (do_flush) 9884c7e8084SDavid Ahern fib_flush(net); 989f88d8ea6SDavid Ahern 990f88d8ea6SDavid Ahern /* ip6_del_rt removes the entry from this list hence the _safe */ 991f88d8ea6SDavid Ahern list_for_each_entry_safe(f6i, tmp, &nh->f6i_list, nh_list) { 992f88d8ea6SDavid Ahern /* __ip6_del_rt does a release, so do a hold here */ 993f88d8ea6SDavid Ahern fib6_info_hold(f6i); 9944f80116dSRoopa Prabhu ipv6_stub->ip6_del_rt(net, f6i, 9954f80116dSRoopa Prabhu !net->ipv4.sysctl_nexthop_compat_mode); 996f88d8ea6SDavid Ahern } 9974c7e8084SDavid Ahern } 9984c7e8084SDavid Ahern 999430a0491SDavid Ahern static void __remove_nexthop(struct net *net, struct nexthop *nh, 1000430a0491SDavid Ahern struct nl_info *nlinfo) 1001430a0491SDavid Ahern { 10024c7e8084SDavid Ahern __remove_nexthop_fib(net, nh); 10034c7e8084SDavid Ahern 1004430a0491SDavid Ahern if (nh->is_group) { 1005430a0491SDavid Ahern remove_nexthop_group(nh, nlinfo); 1006430a0491SDavid Ahern } else { 1007597cfe4fSDavid Ahern struct nh_info *nhi; 1008597cfe4fSDavid Ahern 1009597cfe4fSDavid Ahern nhi = rtnl_dereference(nh->nh_info); 1010597cfe4fSDavid Ahern if (nhi->fib_nhc.nhc_dev) 1011597cfe4fSDavid Ahern hlist_del(&nhi->dev_hash); 1012430a0491SDavid Ahern 1013430a0491SDavid Ahern remove_nexthop_from_groups(net, nh, nlinfo); 1014430a0491SDavid Ahern } 1015597cfe4fSDavid Ahern } 1016597cfe4fSDavid Ahern 1017ab84be7eSDavid Ahern static void remove_nexthop(struct net *net, struct nexthop *nh, 1018430a0491SDavid Ahern struct nl_info *nlinfo) 1019ab84be7eSDavid Ahern { 10203578d53dSIdo Schimmel call_nexthop_notifiers(net, NEXTHOP_EVENT_DEL, nh, NULL); 10210695564bSIdo Schimmel 1022ab84be7eSDavid Ahern /* remove from the tree */ 1023ab84be7eSDavid Ahern rb_erase(&nh->rb_node, &net->nexthop.rb_root); 1024ab84be7eSDavid Ahern 1025ab84be7eSDavid Ahern if (nlinfo) 1026ab84be7eSDavid Ahern nexthop_notify(RTM_DELNEXTHOP, nh, nlinfo); 1027ab84be7eSDavid Ahern 1028430a0491SDavid Ahern __remove_nexthop(net, nh, nlinfo); 1029ab84be7eSDavid Ahern nh_base_seq_inc(net); 1030ab84be7eSDavid Ahern 1031ab84be7eSDavid Ahern nexthop_put(nh); 1032ab84be7eSDavid Ahern } 1033ab84be7eSDavid Ahern 10347bf4796dSDavid Ahern /* if any FIB entries reference this nexthop, any dst entries 10357bf4796dSDavid Ahern * need to be regenerated 10367bf4796dSDavid Ahern */ 10377bf4796dSDavid Ahern static void nh_rt_cache_flush(struct net *net, struct nexthop *nh) 10387bf4796dSDavid Ahern { 10397bf4796dSDavid Ahern struct fib6_info *f6i; 10407bf4796dSDavid Ahern 10417bf4796dSDavid Ahern if (!list_empty(&nh->fi_list)) 10427bf4796dSDavid Ahern rt_cache_flush(net); 10437bf4796dSDavid Ahern 10447bf4796dSDavid Ahern list_for_each_entry(f6i, &nh->f6i_list, nh_list) 10457bf4796dSDavid Ahern ipv6_stub->fib6_update_sernum(net, f6i); 10467bf4796dSDavid Ahern } 10477bf4796dSDavid Ahern 10487bf4796dSDavid Ahern static int replace_nexthop_grp(struct net *net, struct nexthop *old, 10497bf4796dSDavid Ahern struct nexthop *new, 10507bf4796dSDavid Ahern struct netlink_ext_ack *extack) 10517bf4796dSDavid Ahern { 10527bf4796dSDavid Ahern struct nh_group *oldg, *newg; 10537bf4796dSDavid Ahern int i; 10547bf4796dSDavid Ahern 10557bf4796dSDavid Ahern if (!new->is_group) { 10567bf4796dSDavid Ahern NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with a nexthop."); 10577bf4796dSDavid Ahern return -EINVAL; 10587bf4796dSDavid Ahern } 10597bf4796dSDavid Ahern 10607bf4796dSDavid Ahern oldg = rtnl_dereference(old->nh_grp); 10617bf4796dSDavid Ahern newg = rtnl_dereference(new->nh_grp); 10627bf4796dSDavid Ahern 10637bf4796dSDavid Ahern /* update parents - used by nexthop code for cleanup */ 10647bf4796dSDavid Ahern for (i = 0; i < newg->num_nh; i++) 10657bf4796dSDavid Ahern newg->nh_entries[i].nh_parent = old; 10667bf4796dSDavid Ahern 10677bf4796dSDavid Ahern rcu_assign_pointer(old->nh_grp, newg); 10687bf4796dSDavid Ahern 10697bf4796dSDavid Ahern for (i = 0; i < oldg->num_nh; i++) 10707bf4796dSDavid Ahern oldg->nh_entries[i].nh_parent = new; 10717bf4796dSDavid Ahern 10727bf4796dSDavid Ahern rcu_assign_pointer(new->nh_grp, oldg); 10737bf4796dSDavid Ahern 10747bf4796dSDavid Ahern return 0; 10757bf4796dSDavid Ahern } 10767bf4796dSDavid Ahern 1077885a3b15SIdo Schimmel static void nh_group_v4_update(struct nh_group *nhg) 1078885a3b15SIdo Schimmel { 1079885a3b15SIdo Schimmel struct nh_grp_entry *nhges; 1080885a3b15SIdo Schimmel bool has_v4 = false; 1081885a3b15SIdo Schimmel int i; 1082885a3b15SIdo Schimmel 1083885a3b15SIdo Schimmel nhges = nhg->nh_entries; 1084885a3b15SIdo Schimmel for (i = 0; i < nhg->num_nh; i++) { 1085885a3b15SIdo Schimmel struct nh_info *nhi; 1086885a3b15SIdo Schimmel 1087885a3b15SIdo Schimmel nhi = rtnl_dereference(nhges[i].nh->nh_info); 1088885a3b15SIdo Schimmel if (nhi->family == AF_INET) 1089885a3b15SIdo Schimmel has_v4 = true; 1090885a3b15SIdo Schimmel } 1091885a3b15SIdo Schimmel nhg->has_v4 = has_v4; 1092885a3b15SIdo Schimmel } 1093885a3b15SIdo Schimmel 10947bf4796dSDavid Ahern static int replace_nexthop_single(struct net *net, struct nexthop *old, 10957bf4796dSDavid Ahern struct nexthop *new, 10967bf4796dSDavid Ahern struct netlink_ext_ack *extack) 10977bf4796dSDavid Ahern { 10987bf4796dSDavid Ahern struct nh_info *oldi, *newi; 10997bf4796dSDavid Ahern 11007bf4796dSDavid Ahern if (new->is_group) { 11017bf4796dSDavid Ahern NL_SET_ERR_MSG(extack, "Can not replace a nexthop with a nexthop group."); 11027bf4796dSDavid Ahern return -EINVAL; 11037bf4796dSDavid Ahern } 11047bf4796dSDavid Ahern 11057bf4796dSDavid Ahern oldi = rtnl_dereference(old->nh_info); 11067bf4796dSDavid Ahern newi = rtnl_dereference(new->nh_info); 11077bf4796dSDavid Ahern 11087bf4796dSDavid Ahern newi->nh_parent = old; 11097bf4796dSDavid Ahern oldi->nh_parent = new; 11107bf4796dSDavid Ahern 11117bf4796dSDavid Ahern old->protocol = new->protocol; 11127bf4796dSDavid Ahern old->nh_flags = new->nh_flags; 11137bf4796dSDavid Ahern 11147bf4796dSDavid Ahern rcu_assign_pointer(old->nh_info, newi); 11157bf4796dSDavid Ahern rcu_assign_pointer(new->nh_info, oldi); 11167bf4796dSDavid Ahern 1117885a3b15SIdo Schimmel /* When replacing an IPv4 nexthop with an IPv6 nexthop, potentially 1118885a3b15SIdo Schimmel * update IPv4 indication in all the groups using the nexthop. 1119885a3b15SIdo Schimmel */ 1120885a3b15SIdo Schimmel if (oldi->family == AF_INET && newi->family == AF_INET6) { 1121885a3b15SIdo Schimmel struct nh_grp_entry *nhge; 1122885a3b15SIdo Schimmel 1123885a3b15SIdo Schimmel list_for_each_entry(nhge, &old->grp_list, nh_list) { 1124885a3b15SIdo Schimmel struct nexthop *nhp = nhge->nh_parent; 1125885a3b15SIdo Schimmel struct nh_group *nhg; 1126885a3b15SIdo Schimmel 1127885a3b15SIdo Schimmel nhg = rtnl_dereference(nhp->nh_grp); 1128885a3b15SIdo Schimmel nh_group_v4_update(nhg); 1129885a3b15SIdo Schimmel } 1130885a3b15SIdo Schimmel } 1131885a3b15SIdo Schimmel 11327bf4796dSDavid Ahern return 0; 11337bf4796dSDavid Ahern } 11347bf4796dSDavid Ahern 11357bf4796dSDavid Ahern static void __nexthop_replace_notify(struct net *net, struct nexthop *nh, 11367bf4796dSDavid Ahern struct nl_info *info) 11377bf4796dSDavid Ahern { 11387bf4796dSDavid Ahern struct fib6_info *f6i; 11397bf4796dSDavid Ahern 11407bf4796dSDavid Ahern if (!list_empty(&nh->fi_list)) { 11417bf4796dSDavid Ahern struct fib_info *fi; 11427bf4796dSDavid Ahern 11437bf4796dSDavid Ahern /* expectation is a few fib_info per nexthop and then 11447bf4796dSDavid Ahern * a lot of routes per fib_info. So mark the fib_info 11457bf4796dSDavid Ahern * and then walk the fib tables once 11467bf4796dSDavid Ahern */ 11477bf4796dSDavid Ahern list_for_each_entry(fi, &nh->fi_list, nh_list) 11487bf4796dSDavid Ahern fi->nh_updated = true; 11497bf4796dSDavid Ahern 11507bf4796dSDavid Ahern fib_info_notify_update(net, info); 11517bf4796dSDavid Ahern 11527bf4796dSDavid Ahern list_for_each_entry(fi, &nh->fi_list, nh_list) 11537bf4796dSDavid Ahern fi->nh_updated = false; 11547bf4796dSDavid Ahern } 11557bf4796dSDavid Ahern 11567bf4796dSDavid Ahern list_for_each_entry(f6i, &nh->f6i_list, nh_list) 11577bf4796dSDavid Ahern ipv6_stub->fib6_rt_update(net, f6i, info); 11587bf4796dSDavid Ahern } 11597bf4796dSDavid Ahern 11607bf4796dSDavid Ahern /* send RTM_NEWROUTE with REPLACE flag set for all FIB entries 11617bf4796dSDavid Ahern * linked to this nexthop and for all groups that the nexthop 11627bf4796dSDavid Ahern * is a member of 11637bf4796dSDavid Ahern */ 11647bf4796dSDavid Ahern static void nexthop_replace_notify(struct net *net, struct nexthop *nh, 11657bf4796dSDavid Ahern struct nl_info *info) 11667bf4796dSDavid Ahern { 11677bf4796dSDavid Ahern struct nh_grp_entry *nhge; 11687bf4796dSDavid Ahern 11697bf4796dSDavid Ahern __nexthop_replace_notify(net, nh, info); 11707bf4796dSDavid Ahern 11717bf4796dSDavid Ahern list_for_each_entry(nhge, &nh->grp_list, nh_list) 11727bf4796dSDavid Ahern __nexthop_replace_notify(net, nhge->nh_parent, info); 11737bf4796dSDavid Ahern } 11747bf4796dSDavid Ahern 1175ab84be7eSDavid Ahern static int replace_nexthop(struct net *net, struct nexthop *old, 1176ab84be7eSDavid Ahern struct nexthop *new, struct netlink_ext_ack *extack) 1177ab84be7eSDavid Ahern { 11787bf4796dSDavid Ahern bool new_is_reject = false; 11797bf4796dSDavid Ahern struct nh_grp_entry *nhge; 11807bf4796dSDavid Ahern int err; 11817bf4796dSDavid Ahern 11827bf4796dSDavid Ahern /* check that existing FIB entries are ok with the 11837bf4796dSDavid Ahern * new nexthop definition 11847bf4796dSDavid Ahern */ 11857bf4796dSDavid Ahern err = fib_check_nh_list(old, new, extack); 11867bf4796dSDavid Ahern if (err) 11877bf4796dSDavid Ahern return err; 11887bf4796dSDavid Ahern 11897bf4796dSDavid Ahern err = fib6_check_nh_list(old, new, extack); 11907bf4796dSDavid Ahern if (err) 11917bf4796dSDavid Ahern return err; 11927bf4796dSDavid Ahern 11937bf4796dSDavid Ahern if (!new->is_group) { 11947bf4796dSDavid Ahern struct nh_info *nhi = rtnl_dereference(new->nh_info); 11957bf4796dSDavid Ahern 11967bf4796dSDavid Ahern new_is_reject = nhi->reject_nh; 11977bf4796dSDavid Ahern } 11987bf4796dSDavid Ahern 11997bf4796dSDavid Ahern list_for_each_entry(nhge, &old->grp_list, nh_list) { 12007bf4796dSDavid Ahern /* if new nexthop is a blackhole, any groups using this 12017bf4796dSDavid Ahern * nexthop cannot have more than 1 path 12027bf4796dSDavid Ahern */ 12037bf4796dSDavid Ahern if (new_is_reject && 12047bf4796dSDavid Ahern nexthop_num_path(nhge->nh_parent) > 1) { 12057bf4796dSDavid Ahern NL_SET_ERR_MSG(extack, "Blackhole nexthop can not be a member of a group with more than one path"); 12067bf4796dSDavid Ahern return -EINVAL; 12077bf4796dSDavid Ahern } 12087bf4796dSDavid Ahern 12097bf4796dSDavid Ahern err = fib_check_nh_list(nhge->nh_parent, new, extack); 12107bf4796dSDavid Ahern if (err) 12117bf4796dSDavid Ahern return err; 12127bf4796dSDavid Ahern 12137bf4796dSDavid Ahern err = fib6_check_nh_list(nhge->nh_parent, new, extack); 12147bf4796dSDavid Ahern if (err) 12157bf4796dSDavid Ahern return err; 12167bf4796dSDavid Ahern } 12177bf4796dSDavid Ahern 12187bf4796dSDavid Ahern if (old->is_group) 12197bf4796dSDavid Ahern err = replace_nexthop_grp(net, old, new, extack); 12207bf4796dSDavid Ahern else 12217bf4796dSDavid Ahern err = replace_nexthop_single(net, old, new, extack); 12227bf4796dSDavid Ahern 12237bf4796dSDavid Ahern if (!err) { 12247bf4796dSDavid Ahern nh_rt_cache_flush(net, old); 12257bf4796dSDavid Ahern 12267bf4796dSDavid Ahern __remove_nexthop(net, new, NULL); 12277bf4796dSDavid Ahern nexthop_put(new); 12287bf4796dSDavid Ahern } 12297bf4796dSDavid Ahern 12307bf4796dSDavid Ahern return err; 1231ab84be7eSDavid Ahern } 1232ab84be7eSDavid Ahern 1233ab84be7eSDavid Ahern /* called with rtnl_lock held */ 1234ab84be7eSDavid Ahern static int insert_nexthop(struct net *net, struct nexthop *new_nh, 1235ab84be7eSDavid Ahern struct nh_config *cfg, struct netlink_ext_ack *extack) 1236ab84be7eSDavid Ahern { 1237ab84be7eSDavid Ahern struct rb_node **pp, *parent = NULL, *next; 1238ab84be7eSDavid Ahern struct rb_root *root = &net->nexthop.rb_root; 1239ab84be7eSDavid Ahern bool replace = !!(cfg->nlflags & NLM_F_REPLACE); 1240ab84be7eSDavid Ahern bool create = !!(cfg->nlflags & NLM_F_CREATE); 1241ab84be7eSDavid Ahern u32 new_id = new_nh->id; 12427bf4796dSDavid Ahern int replace_notify = 0; 1243ab84be7eSDavid Ahern int rc = -EEXIST; 1244ab84be7eSDavid Ahern 1245ab84be7eSDavid Ahern pp = &root->rb_node; 1246ab84be7eSDavid Ahern while (1) { 1247ab84be7eSDavid Ahern struct nexthop *nh; 1248ab84be7eSDavid Ahern 1249233c6378SIdo Schimmel next = *pp; 1250ab84be7eSDavid Ahern if (!next) 1251ab84be7eSDavid Ahern break; 1252ab84be7eSDavid Ahern 1253ab84be7eSDavid Ahern parent = next; 1254ab84be7eSDavid Ahern 1255ab84be7eSDavid Ahern nh = rb_entry(parent, struct nexthop, rb_node); 1256ab84be7eSDavid Ahern if (new_id < nh->id) { 1257ab84be7eSDavid Ahern pp = &next->rb_left; 1258ab84be7eSDavid Ahern } else if (new_id > nh->id) { 1259ab84be7eSDavid Ahern pp = &next->rb_right; 1260ab84be7eSDavid Ahern } else if (replace) { 1261ab84be7eSDavid Ahern rc = replace_nexthop(net, nh, new_nh, extack); 12627bf4796dSDavid Ahern if (!rc) { 1263ab84be7eSDavid Ahern new_nh = nh; /* send notification with old nh */ 12647bf4796dSDavid Ahern replace_notify = 1; 12657bf4796dSDavid Ahern } 1266ab84be7eSDavid Ahern goto out; 1267ab84be7eSDavid Ahern } else { 1268ab84be7eSDavid Ahern /* id already exists and not a replace */ 1269ab84be7eSDavid Ahern goto out; 1270ab84be7eSDavid Ahern } 1271ab84be7eSDavid Ahern } 1272ab84be7eSDavid Ahern 1273ab84be7eSDavid Ahern if (replace && !create) { 1274ab84be7eSDavid Ahern NL_SET_ERR_MSG(extack, "Replace specified without create and no entry exists"); 1275ab84be7eSDavid Ahern rc = -ENOENT; 1276ab84be7eSDavid Ahern goto out; 1277ab84be7eSDavid Ahern } 1278ab84be7eSDavid Ahern 1279ab84be7eSDavid Ahern rb_link_node_rcu(&new_nh->rb_node, parent, pp); 1280ab84be7eSDavid Ahern rb_insert_color(&new_nh->rb_node, root); 1281*732d167bSIdo Schimmel 1282*732d167bSIdo Schimmel rc = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new_nh, extack); 1283*732d167bSIdo Schimmel if (rc) 1284*732d167bSIdo Schimmel rb_erase(&new_nh->rb_node, &net->nexthop.rb_root); 1285*732d167bSIdo Schimmel 1286ab84be7eSDavid Ahern out: 1287ab84be7eSDavid Ahern if (!rc) { 1288ab84be7eSDavid Ahern nh_base_seq_inc(net); 1289ab84be7eSDavid Ahern nexthop_notify(RTM_NEWNEXTHOP, new_nh, &cfg->nlinfo); 12904f80116dSRoopa Prabhu if (replace_notify && net->ipv4.sysctl_nexthop_compat_mode) 12917bf4796dSDavid Ahern nexthop_replace_notify(net, new_nh, &cfg->nlinfo); 1292ab84be7eSDavid Ahern } 1293ab84be7eSDavid Ahern 1294ab84be7eSDavid Ahern return rc; 1295ab84be7eSDavid Ahern } 1296ab84be7eSDavid Ahern 1297597cfe4fSDavid Ahern /* rtnl */ 1298597cfe4fSDavid Ahern /* remove all nexthops tied to a device being deleted */ 1299597cfe4fSDavid Ahern static void nexthop_flush_dev(struct net_device *dev) 1300597cfe4fSDavid Ahern { 1301597cfe4fSDavid Ahern unsigned int hash = nh_dev_hashfn(dev->ifindex); 1302597cfe4fSDavid Ahern struct net *net = dev_net(dev); 1303597cfe4fSDavid Ahern struct hlist_head *head = &net->nexthop.devhash[hash]; 1304597cfe4fSDavid Ahern struct hlist_node *n; 1305597cfe4fSDavid Ahern struct nh_info *nhi; 1306597cfe4fSDavid Ahern 1307597cfe4fSDavid Ahern hlist_for_each_entry_safe(nhi, n, head, dev_hash) { 1308597cfe4fSDavid Ahern if (nhi->fib_nhc.nhc_dev != dev) 1309597cfe4fSDavid Ahern continue; 1310597cfe4fSDavid Ahern 1311430a0491SDavid Ahern remove_nexthop(net, nhi->nh_parent, NULL); 1312597cfe4fSDavid Ahern } 1313597cfe4fSDavid Ahern } 1314597cfe4fSDavid Ahern 1315ab84be7eSDavid Ahern /* rtnl; called when net namespace is deleted */ 1316ab84be7eSDavid Ahern static void flush_all_nexthops(struct net *net) 1317ab84be7eSDavid Ahern { 1318ab84be7eSDavid Ahern struct rb_root *root = &net->nexthop.rb_root; 1319ab84be7eSDavid Ahern struct rb_node *node; 1320ab84be7eSDavid Ahern struct nexthop *nh; 1321ab84be7eSDavid Ahern 1322ab84be7eSDavid Ahern while ((node = rb_first(root))) { 1323ab84be7eSDavid Ahern nh = rb_entry(node, struct nexthop, rb_node); 1324430a0491SDavid Ahern remove_nexthop(net, nh, NULL); 1325ab84be7eSDavid Ahern cond_resched(); 1326ab84be7eSDavid Ahern } 1327ab84be7eSDavid Ahern } 1328ab84be7eSDavid Ahern 1329430a0491SDavid Ahern static struct nexthop *nexthop_create_group(struct net *net, 1330430a0491SDavid Ahern struct nh_config *cfg) 1331430a0491SDavid Ahern { 1332430a0491SDavid Ahern struct nlattr *grps_attr = cfg->nh_grp; 1333430a0491SDavid Ahern struct nexthop_grp *entry = nla_data(grps_attr); 133490f33bffSNikolay Aleksandrov u16 num_nh = nla_len(grps_attr) / sizeof(*entry); 1335430a0491SDavid Ahern struct nh_group *nhg; 1336430a0491SDavid Ahern struct nexthop *nh; 1337430a0491SDavid Ahern int i; 1338430a0491SDavid Ahern 1339eeaac363SNikolay Aleksandrov if (WARN_ON(!num_nh)) 1340eeaac363SNikolay Aleksandrov return ERR_PTR(-EINVAL); 1341eeaac363SNikolay Aleksandrov 1342430a0491SDavid Ahern nh = nexthop_alloc(); 1343430a0491SDavid Ahern if (!nh) 1344430a0491SDavid Ahern return ERR_PTR(-ENOMEM); 1345430a0491SDavid Ahern 1346430a0491SDavid Ahern nh->is_group = 1; 1347430a0491SDavid Ahern 134890f33bffSNikolay Aleksandrov nhg = nexthop_grp_alloc(num_nh); 1349430a0491SDavid Ahern if (!nhg) { 1350430a0491SDavid Ahern kfree(nh); 1351430a0491SDavid Ahern return ERR_PTR(-ENOMEM); 1352430a0491SDavid Ahern } 1353430a0491SDavid Ahern 135490f33bffSNikolay Aleksandrov /* spare group used for removals */ 135590f33bffSNikolay Aleksandrov nhg->spare = nexthop_grp_alloc(num_nh); 1356dafe2078SPatrick Eigensatz if (!nhg->spare) { 135790f33bffSNikolay Aleksandrov kfree(nhg); 135890f33bffSNikolay Aleksandrov kfree(nh); 1359dafe2078SPatrick Eigensatz return ERR_PTR(-ENOMEM); 136090f33bffSNikolay Aleksandrov } 136190f33bffSNikolay Aleksandrov nhg->spare->spare = nhg; 136290f33bffSNikolay Aleksandrov 1363430a0491SDavid Ahern for (i = 0; i < nhg->num_nh; ++i) { 1364430a0491SDavid Ahern struct nexthop *nhe; 1365430a0491SDavid Ahern struct nh_info *nhi; 1366430a0491SDavid Ahern 1367430a0491SDavid Ahern nhe = nexthop_find_by_id(net, entry[i].id); 1368430a0491SDavid Ahern if (!nexthop_get(nhe)) 1369430a0491SDavid Ahern goto out_no_nh; 1370430a0491SDavid Ahern 1371430a0491SDavid Ahern nhi = rtnl_dereference(nhe->nh_info); 1372430a0491SDavid Ahern if (nhi->family == AF_INET) 1373430a0491SDavid Ahern nhg->has_v4 = true; 1374430a0491SDavid Ahern 1375430a0491SDavid Ahern nhg->nh_entries[i].nh = nhe; 1376430a0491SDavid Ahern nhg->nh_entries[i].weight = entry[i].weight + 1; 1377430a0491SDavid Ahern list_add(&nhg->nh_entries[i].nh_list, &nhe->grp_list); 1378430a0491SDavid Ahern nhg->nh_entries[i].nh_parent = nh; 1379430a0491SDavid Ahern } 1380430a0491SDavid Ahern 1381430a0491SDavid Ahern if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_MPATH) { 1382430a0491SDavid Ahern nhg->mpath = 1; 1383430a0491SDavid Ahern nh_group_rebalance(nhg); 1384430a0491SDavid Ahern } 1385430a0491SDavid Ahern 138638428d68SRoopa Prabhu if (cfg->nh_fdb) 1387ce9ac056SDavid Ahern nhg->fdb_nh = 1; 138838428d68SRoopa Prabhu 1389430a0491SDavid Ahern rcu_assign_pointer(nh->nh_grp, nhg); 1390430a0491SDavid Ahern 1391430a0491SDavid Ahern return nh; 1392430a0491SDavid Ahern 1393430a0491SDavid Ahern out_no_nh: 1394430a0491SDavid Ahern for (; i >= 0; --i) 1395430a0491SDavid Ahern nexthop_put(nhg->nh_entries[i].nh); 1396430a0491SDavid Ahern 139790f33bffSNikolay Aleksandrov kfree(nhg->spare); 1398430a0491SDavid Ahern kfree(nhg); 1399430a0491SDavid Ahern kfree(nh); 1400430a0491SDavid Ahern 1401430a0491SDavid Ahern return ERR_PTR(-ENOENT); 1402430a0491SDavid Ahern } 1403430a0491SDavid Ahern 1404597cfe4fSDavid Ahern static int nh_create_ipv4(struct net *net, struct nexthop *nh, 1405597cfe4fSDavid Ahern struct nh_info *nhi, struct nh_config *cfg, 1406597cfe4fSDavid Ahern struct netlink_ext_ack *extack) 1407597cfe4fSDavid Ahern { 1408597cfe4fSDavid Ahern struct fib_nh *fib_nh = &nhi->fib_nh; 1409597cfe4fSDavid Ahern struct fib_config fib_cfg = { 1410597cfe4fSDavid Ahern .fc_oif = cfg->nh_ifindex, 1411597cfe4fSDavid Ahern .fc_gw4 = cfg->gw.ipv4, 1412597cfe4fSDavid Ahern .fc_gw_family = cfg->gw.ipv4 ? AF_INET : 0, 1413597cfe4fSDavid Ahern .fc_flags = cfg->nh_flags, 1414b513bd03SDavid Ahern .fc_encap = cfg->nh_encap, 1415b513bd03SDavid Ahern .fc_encap_type = cfg->nh_encap_type, 1416597cfe4fSDavid Ahern }; 141738428d68SRoopa Prabhu u32 tb_id = (cfg->dev ? l3mdev_fib_table(cfg->dev) : RT_TABLE_MAIN); 1418c76c9925SColin Ian King int err; 1419597cfe4fSDavid Ahern 1420597cfe4fSDavid Ahern err = fib_nh_init(net, fib_nh, &fib_cfg, 1, extack); 1421597cfe4fSDavid Ahern if (err) { 1422597cfe4fSDavid Ahern fib_nh_release(net, fib_nh); 1423597cfe4fSDavid Ahern goto out; 1424597cfe4fSDavid Ahern } 1425597cfe4fSDavid Ahern 1426ce9ac056SDavid Ahern if (nhi->fdb_nh) 142738428d68SRoopa Prabhu goto out; 142838428d68SRoopa Prabhu 1429597cfe4fSDavid Ahern /* sets nh_dev if successful */ 1430597cfe4fSDavid Ahern err = fib_check_nh(net, fib_nh, tb_id, 0, extack); 1431597cfe4fSDavid Ahern if (!err) { 1432597cfe4fSDavid Ahern nh->nh_flags = fib_nh->fib_nh_flags; 1433dcb1ecb5SDavid Ahern fib_info_update_nhc_saddr(net, &fib_nh->nh_common, 1434dcb1ecb5SDavid Ahern fib_nh->fib_nh_scope); 1435597cfe4fSDavid Ahern } else { 1436597cfe4fSDavid Ahern fib_nh_release(net, fib_nh); 1437597cfe4fSDavid Ahern } 1438597cfe4fSDavid Ahern out: 1439597cfe4fSDavid Ahern return err; 1440597cfe4fSDavid Ahern } 1441597cfe4fSDavid Ahern 144253010f99SDavid Ahern static int nh_create_ipv6(struct net *net, struct nexthop *nh, 144353010f99SDavid Ahern struct nh_info *nhi, struct nh_config *cfg, 144453010f99SDavid Ahern struct netlink_ext_ack *extack) 144553010f99SDavid Ahern { 144653010f99SDavid Ahern struct fib6_nh *fib6_nh = &nhi->fib6_nh; 144753010f99SDavid Ahern struct fib6_config fib6_cfg = { 144853010f99SDavid Ahern .fc_table = l3mdev_fib_table(cfg->dev), 144953010f99SDavid Ahern .fc_ifindex = cfg->nh_ifindex, 145053010f99SDavid Ahern .fc_gateway = cfg->gw.ipv6, 145153010f99SDavid Ahern .fc_flags = cfg->nh_flags, 1452b513bd03SDavid Ahern .fc_encap = cfg->nh_encap, 1453b513bd03SDavid Ahern .fc_encap_type = cfg->nh_encap_type, 145438428d68SRoopa Prabhu .fc_is_fdb = cfg->nh_fdb, 145553010f99SDavid Ahern }; 14566f43e525SColin Ian King int err; 145753010f99SDavid Ahern 145853010f99SDavid Ahern if (!ipv6_addr_any(&cfg->gw.ipv6)) 145953010f99SDavid Ahern fib6_cfg.fc_flags |= RTF_GATEWAY; 146053010f99SDavid Ahern 146153010f99SDavid Ahern /* sets nh_dev if successful */ 146253010f99SDavid Ahern err = ipv6_stub->fib6_nh_init(net, fib6_nh, &fib6_cfg, GFP_KERNEL, 146353010f99SDavid Ahern extack); 146453010f99SDavid Ahern if (err) 146553010f99SDavid Ahern ipv6_stub->fib6_nh_release(fib6_nh); 146653010f99SDavid Ahern else 146753010f99SDavid Ahern nh->nh_flags = fib6_nh->fib_nh_flags; 146853010f99SDavid Ahern 146953010f99SDavid Ahern return err; 147053010f99SDavid Ahern } 147153010f99SDavid Ahern 1472ab84be7eSDavid Ahern static struct nexthop *nexthop_create(struct net *net, struct nh_config *cfg, 1473ab84be7eSDavid Ahern struct netlink_ext_ack *extack) 1474ab84be7eSDavid Ahern { 1475ab84be7eSDavid Ahern struct nh_info *nhi; 1476ab84be7eSDavid Ahern struct nexthop *nh; 1477ab84be7eSDavid Ahern int err = 0; 1478ab84be7eSDavid Ahern 1479ab84be7eSDavid Ahern nh = nexthop_alloc(); 1480ab84be7eSDavid Ahern if (!nh) 1481ab84be7eSDavid Ahern return ERR_PTR(-ENOMEM); 1482ab84be7eSDavid Ahern 1483ab84be7eSDavid Ahern nhi = kzalloc(sizeof(*nhi), GFP_KERNEL); 1484ab84be7eSDavid Ahern if (!nhi) { 1485ab84be7eSDavid Ahern kfree(nh); 1486ab84be7eSDavid Ahern return ERR_PTR(-ENOMEM); 1487ab84be7eSDavid Ahern } 1488ab84be7eSDavid Ahern 1489ab84be7eSDavid Ahern nh->nh_flags = cfg->nh_flags; 1490ab84be7eSDavid Ahern nh->net = net; 1491ab84be7eSDavid Ahern 1492ab84be7eSDavid Ahern nhi->nh_parent = nh; 1493ab84be7eSDavid Ahern nhi->family = cfg->nh_family; 1494ab84be7eSDavid Ahern nhi->fib_nhc.nhc_scope = RT_SCOPE_LINK; 1495ab84be7eSDavid Ahern 149638428d68SRoopa Prabhu if (cfg->nh_fdb) 1497ce9ac056SDavid Ahern nhi->fdb_nh = 1; 149838428d68SRoopa Prabhu 1499ab84be7eSDavid Ahern if (cfg->nh_blackhole) { 1500ab84be7eSDavid Ahern nhi->reject_nh = 1; 1501ab84be7eSDavid Ahern cfg->nh_ifindex = net->loopback_dev->ifindex; 1502ab84be7eSDavid Ahern } 1503ab84be7eSDavid Ahern 1504597cfe4fSDavid Ahern switch (cfg->nh_family) { 1505597cfe4fSDavid Ahern case AF_INET: 1506597cfe4fSDavid Ahern err = nh_create_ipv4(net, nh, nhi, cfg, extack); 1507597cfe4fSDavid Ahern break; 150853010f99SDavid Ahern case AF_INET6: 150953010f99SDavid Ahern err = nh_create_ipv6(net, nh, nhi, cfg, extack); 151053010f99SDavid Ahern break; 1511597cfe4fSDavid Ahern } 1512597cfe4fSDavid Ahern 1513ab84be7eSDavid Ahern if (err) { 1514ab84be7eSDavid Ahern kfree(nhi); 1515ab84be7eSDavid Ahern kfree(nh); 1516ab84be7eSDavid Ahern return ERR_PTR(err); 1517ab84be7eSDavid Ahern } 1518ab84be7eSDavid Ahern 1519597cfe4fSDavid Ahern /* add the entry to the device based hash */ 1520ce9ac056SDavid Ahern if (!nhi->fdb_nh) 1521597cfe4fSDavid Ahern nexthop_devhash_add(net, nhi); 1522597cfe4fSDavid Ahern 1523ab84be7eSDavid Ahern rcu_assign_pointer(nh->nh_info, nhi); 1524ab84be7eSDavid Ahern 1525ab84be7eSDavid Ahern return nh; 1526ab84be7eSDavid Ahern } 1527ab84be7eSDavid Ahern 1528ab84be7eSDavid Ahern /* called with rtnl lock held */ 1529ab84be7eSDavid Ahern static struct nexthop *nexthop_add(struct net *net, struct nh_config *cfg, 1530ab84be7eSDavid Ahern struct netlink_ext_ack *extack) 1531ab84be7eSDavid Ahern { 1532ab84be7eSDavid Ahern struct nexthop *nh; 1533ab84be7eSDavid Ahern int err; 1534ab84be7eSDavid Ahern 1535ab84be7eSDavid Ahern if (cfg->nlflags & NLM_F_REPLACE && !cfg->nh_id) { 1536ab84be7eSDavid Ahern NL_SET_ERR_MSG(extack, "Replace requires nexthop id"); 1537ab84be7eSDavid Ahern return ERR_PTR(-EINVAL); 1538ab84be7eSDavid Ahern } 1539ab84be7eSDavid Ahern 1540ab84be7eSDavid Ahern if (!cfg->nh_id) { 1541ab84be7eSDavid Ahern cfg->nh_id = nh_find_unused_id(net); 1542ab84be7eSDavid Ahern if (!cfg->nh_id) { 1543ab84be7eSDavid Ahern NL_SET_ERR_MSG(extack, "No unused id"); 1544ab84be7eSDavid Ahern return ERR_PTR(-EINVAL); 1545ab84be7eSDavid Ahern } 1546ab84be7eSDavid Ahern } 1547ab84be7eSDavid Ahern 1548430a0491SDavid Ahern if (cfg->nh_grp) 1549430a0491SDavid Ahern nh = nexthop_create_group(net, cfg); 1550430a0491SDavid Ahern else 1551ab84be7eSDavid Ahern nh = nexthop_create(net, cfg, extack); 1552430a0491SDavid Ahern 1553ab84be7eSDavid Ahern if (IS_ERR(nh)) 1554ab84be7eSDavid Ahern return nh; 1555ab84be7eSDavid Ahern 1556ab84be7eSDavid Ahern refcount_set(&nh->refcnt, 1); 1557ab84be7eSDavid Ahern nh->id = cfg->nh_id; 1558ab84be7eSDavid Ahern nh->protocol = cfg->nh_protocol; 1559ab84be7eSDavid Ahern nh->net = net; 1560ab84be7eSDavid Ahern 1561ab84be7eSDavid Ahern err = insert_nexthop(net, nh, cfg, extack); 1562ab84be7eSDavid Ahern if (err) { 1563430a0491SDavid Ahern __remove_nexthop(net, nh, NULL); 1564ab84be7eSDavid Ahern nexthop_put(nh); 1565ab84be7eSDavid Ahern nh = ERR_PTR(err); 1566ab84be7eSDavid Ahern } 1567ab84be7eSDavid Ahern 1568ab84be7eSDavid Ahern return nh; 1569ab84be7eSDavid Ahern } 1570ab84be7eSDavid Ahern 1571ab84be7eSDavid Ahern static int rtm_to_nh_config(struct net *net, struct sk_buff *skb, 1572ab84be7eSDavid Ahern struct nlmsghdr *nlh, struct nh_config *cfg, 1573ab84be7eSDavid Ahern struct netlink_ext_ack *extack) 1574ab84be7eSDavid Ahern { 1575ab84be7eSDavid Ahern struct nhmsg *nhm = nlmsg_data(nlh); 1576ab84be7eSDavid Ahern struct nlattr *tb[NHA_MAX + 1]; 1577ab84be7eSDavid Ahern int err; 1578ab84be7eSDavid Ahern 1579ab84be7eSDavid Ahern err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy, 1580ab84be7eSDavid Ahern extack); 1581ab84be7eSDavid Ahern if (err < 0) 1582ab84be7eSDavid Ahern return err; 1583ab84be7eSDavid Ahern 1584ab84be7eSDavid Ahern err = -EINVAL; 1585ab84be7eSDavid Ahern if (nhm->resvd || nhm->nh_scope) { 1586ab84be7eSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in ancillary header"); 1587ab84be7eSDavid Ahern goto out; 1588ab84be7eSDavid Ahern } 1589ab84be7eSDavid Ahern if (nhm->nh_flags & ~NEXTHOP_VALID_USER_FLAGS) { 1590ab84be7eSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid nexthop flags in ancillary header"); 1591ab84be7eSDavid Ahern goto out; 1592ab84be7eSDavid Ahern } 1593ab84be7eSDavid Ahern 1594ab84be7eSDavid Ahern switch (nhm->nh_family) { 1595597cfe4fSDavid Ahern case AF_INET: 159653010f99SDavid Ahern case AF_INET6: 1597597cfe4fSDavid Ahern break; 1598430a0491SDavid Ahern case AF_UNSPEC: 1599430a0491SDavid Ahern if (tb[NHA_GROUP]) 1600430a0491SDavid Ahern break; 1601a8eceea8SJoe Perches fallthrough; 1602ab84be7eSDavid Ahern default: 1603ab84be7eSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid address family"); 1604ab84be7eSDavid Ahern goto out; 1605ab84be7eSDavid Ahern } 1606ab84be7eSDavid Ahern 1607ab84be7eSDavid Ahern if (tb[NHA_GROUPS] || tb[NHA_MASTER]) { 1608ab84be7eSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid attributes in request"); 1609ab84be7eSDavid Ahern goto out; 1610ab84be7eSDavid Ahern } 1611ab84be7eSDavid Ahern 1612ab84be7eSDavid Ahern memset(cfg, 0, sizeof(*cfg)); 1613ab84be7eSDavid Ahern cfg->nlflags = nlh->nlmsg_flags; 1614ab84be7eSDavid Ahern cfg->nlinfo.portid = NETLINK_CB(skb).portid; 1615ab84be7eSDavid Ahern cfg->nlinfo.nlh = nlh; 1616ab84be7eSDavid Ahern cfg->nlinfo.nl_net = net; 1617ab84be7eSDavid Ahern 1618ab84be7eSDavid Ahern cfg->nh_family = nhm->nh_family; 1619ab84be7eSDavid Ahern cfg->nh_protocol = nhm->nh_protocol; 1620ab84be7eSDavid Ahern cfg->nh_flags = nhm->nh_flags; 1621ab84be7eSDavid Ahern 1622ab84be7eSDavid Ahern if (tb[NHA_ID]) 1623ab84be7eSDavid Ahern cfg->nh_id = nla_get_u32(tb[NHA_ID]); 1624ab84be7eSDavid Ahern 162538428d68SRoopa Prabhu if (tb[NHA_FDB]) { 162638428d68SRoopa Prabhu if (tb[NHA_OIF] || tb[NHA_BLACKHOLE] || 162738428d68SRoopa Prabhu tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE]) { 162838428d68SRoopa Prabhu NL_SET_ERR_MSG(extack, "Fdb attribute can not be used with encap, oif or blackhole"); 162938428d68SRoopa Prabhu goto out; 163038428d68SRoopa Prabhu } 163138428d68SRoopa Prabhu if (nhm->nh_flags) { 163238428d68SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unsupported nexthop flags in ancillary header"); 163338428d68SRoopa Prabhu goto out; 163438428d68SRoopa Prabhu } 163538428d68SRoopa Prabhu cfg->nh_fdb = nla_get_flag(tb[NHA_FDB]); 163638428d68SRoopa Prabhu } 163738428d68SRoopa Prabhu 1638430a0491SDavid Ahern if (tb[NHA_GROUP]) { 1639430a0491SDavid Ahern if (nhm->nh_family != AF_UNSPEC) { 1640430a0491SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid family for group"); 1641430a0491SDavid Ahern goto out; 1642430a0491SDavid Ahern } 1643430a0491SDavid Ahern cfg->nh_grp = tb[NHA_GROUP]; 1644430a0491SDavid Ahern 1645430a0491SDavid Ahern cfg->nh_grp_type = NEXTHOP_GRP_TYPE_MPATH; 1646430a0491SDavid Ahern if (tb[NHA_GROUP_TYPE]) 1647430a0491SDavid Ahern cfg->nh_grp_type = nla_get_u16(tb[NHA_GROUP_TYPE]); 1648430a0491SDavid Ahern 1649430a0491SDavid Ahern if (cfg->nh_grp_type > NEXTHOP_GRP_TYPE_MAX) { 1650430a0491SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid group type"); 1651430a0491SDavid Ahern goto out; 1652430a0491SDavid Ahern } 1653430a0491SDavid Ahern err = nh_check_attr_group(net, tb, extack); 1654430a0491SDavid Ahern 1655430a0491SDavid Ahern /* no other attributes should be set */ 1656430a0491SDavid Ahern goto out; 1657430a0491SDavid Ahern } 1658430a0491SDavid Ahern 1659ab84be7eSDavid Ahern if (tb[NHA_BLACKHOLE]) { 1660b513bd03SDavid Ahern if (tb[NHA_GATEWAY] || tb[NHA_OIF] || 166138428d68SRoopa Prabhu tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE] || tb[NHA_FDB]) { 166238428d68SRoopa Prabhu NL_SET_ERR_MSG(extack, "Blackhole attribute can not be used with gateway, oif, encap or fdb"); 1663ab84be7eSDavid Ahern goto out; 1664ab84be7eSDavid Ahern } 1665ab84be7eSDavid Ahern 1666ab84be7eSDavid Ahern cfg->nh_blackhole = 1; 1667ab84be7eSDavid Ahern err = 0; 1668ab84be7eSDavid Ahern goto out; 1669ab84be7eSDavid Ahern } 1670ab84be7eSDavid Ahern 167138428d68SRoopa Prabhu if (!cfg->nh_fdb && !tb[NHA_OIF]) { 167238428d68SRoopa Prabhu NL_SET_ERR_MSG(extack, "Device attribute required for non-blackhole and non-fdb nexthops"); 1673ab84be7eSDavid Ahern goto out; 1674ab84be7eSDavid Ahern } 1675ab84be7eSDavid Ahern 167638428d68SRoopa Prabhu if (!cfg->nh_fdb && tb[NHA_OIF]) { 1677ab84be7eSDavid Ahern cfg->nh_ifindex = nla_get_u32(tb[NHA_OIF]); 1678ab84be7eSDavid Ahern if (cfg->nh_ifindex) 1679ab84be7eSDavid Ahern cfg->dev = __dev_get_by_index(net, cfg->nh_ifindex); 1680ab84be7eSDavid Ahern 1681ab84be7eSDavid Ahern if (!cfg->dev) { 1682ab84be7eSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid device index"); 1683ab84be7eSDavid Ahern goto out; 1684ab84be7eSDavid Ahern } else if (!(cfg->dev->flags & IFF_UP)) { 1685ab84be7eSDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop device is not up"); 1686ab84be7eSDavid Ahern err = -ENETDOWN; 1687ab84be7eSDavid Ahern goto out; 1688ab84be7eSDavid Ahern } else if (!netif_carrier_ok(cfg->dev)) { 1689ab84be7eSDavid Ahern NL_SET_ERR_MSG(extack, "Carrier for nexthop device is down"); 1690ab84be7eSDavid Ahern err = -ENETDOWN; 1691ab84be7eSDavid Ahern goto out; 1692ab84be7eSDavid Ahern } 169338428d68SRoopa Prabhu } 1694ab84be7eSDavid Ahern 1695597cfe4fSDavid Ahern err = -EINVAL; 1696597cfe4fSDavid Ahern if (tb[NHA_GATEWAY]) { 1697597cfe4fSDavid Ahern struct nlattr *gwa = tb[NHA_GATEWAY]; 1698597cfe4fSDavid Ahern 1699597cfe4fSDavid Ahern switch (cfg->nh_family) { 1700597cfe4fSDavid Ahern case AF_INET: 1701597cfe4fSDavid Ahern if (nla_len(gwa) != sizeof(u32)) { 1702597cfe4fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway"); 1703597cfe4fSDavid Ahern goto out; 1704597cfe4fSDavid Ahern } 1705597cfe4fSDavid Ahern cfg->gw.ipv4 = nla_get_be32(gwa); 1706597cfe4fSDavid Ahern break; 170753010f99SDavid Ahern case AF_INET6: 170853010f99SDavid Ahern if (nla_len(gwa) != sizeof(struct in6_addr)) { 170953010f99SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway"); 171053010f99SDavid Ahern goto out; 171153010f99SDavid Ahern } 171253010f99SDavid Ahern cfg->gw.ipv6 = nla_get_in6_addr(gwa); 171353010f99SDavid Ahern break; 1714597cfe4fSDavid Ahern default: 1715597cfe4fSDavid Ahern NL_SET_ERR_MSG(extack, 1716597cfe4fSDavid Ahern "Unknown address family for gateway"); 1717597cfe4fSDavid Ahern goto out; 1718597cfe4fSDavid Ahern } 1719597cfe4fSDavid Ahern } else { 1720597cfe4fSDavid Ahern /* device only nexthop (no gateway) */ 1721597cfe4fSDavid Ahern if (cfg->nh_flags & RTNH_F_ONLINK) { 1722597cfe4fSDavid Ahern NL_SET_ERR_MSG(extack, 1723597cfe4fSDavid Ahern "ONLINK flag can not be set for nexthop without a gateway"); 1724597cfe4fSDavid Ahern goto out; 1725597cfe4fSDavid Ahern } 1726597cfe4fSDavid Ahern } 1727597cfe4fSDavid Ahern 1728b513bd03SDavid Ahern if (tb[NHA_ENCAP]) { 1729b513bd03SDavid Ahern cfg->nh_encap = tb[NHA_ENCAP]; 1730b513bd03SDavid Ahern 1731b513bd03SDavid Ahern if (!tb[NHA_ENCAP_TYPE]) { 1732b513bd03SDavid Ahern NL_SET_ERR_MSG(extack, "LWT encapsulation type is missing"); 1733b513bd03SDavid Ahern goto out; 1734b513bd03SDavid Ahern } 1735b513bd03SDavid Ahern 1736b513bd03SDavid Ahern cfg->nh_encap_type = nla_get_u16(tb[NHA_ENCAP_TYPE]); 1737b513bd03SDavid Ahern err = lwtunnel_valid_encap_type(cfg->nh_encap_type, extack); 1738b513bd03SDavid Ahern if (err < 0) 1739b513bd03SDavid Ahern goto out; 1740b513bd03SDavid Ahern 1741b513bd03SDavid Ahern } else if (tb[NHA_ENCAP_TYPE]) { 1742b513bd03SDavid Ahern NL_SET_ERR_MSG(extack, "LWT encapsulation attribute is missing"); 1743b513bd03SDavid Ahern goto out; 1744b513bd03SDavid Ahern } 1745b513bd03SDavid Ahern 1746b513bd03SDavid Ahern 1747ab84be7eSDavid Ahern err = 0; 1748ab84be7eSDavid Ahern out: 1749ab84be7eSDavid Ahern return err; 1750ab84be7eSDavid Ahern } 1751ab84be7eSDavid Ahern 1752ab84be7eSDavid Ahern /* rtnl */ 1753ab84be7eSDavid Ahern static int rtm_new_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh, 1754ab84be7eSDavid Ahern struct netlink_ext_ack *extack) 1755ab84be7eSDavid Ahern { 1756ab84be7eSDavid Ahern struct net *net = sock_net(skb->sk); 1757ab84be7eSDavid Ahern struct nh_config cfg; 1758ab84be7eSDavid Ahern struct nexthop *nh; 1759ab84be7eSDavid Ahern int err; 1760ab84be7eSDavid Ahern 1761ab84be7eSDavid Ahern err = rtm_to_nh_config(net, skb, nlh, &cfg, extack); 1762ab84be7eSDavid Ahern if (!err) { 1763ab84be7eSDavid Ahern nh = nexthop_add(net, &cfg, extack); 1764ab84be7eSDavid Ahern if (IS_ERR(nh)) 1765ab84be7eSDavid Ahern err = PTR_ERR(nh); 1766ab84be7eSDavid Ahern } 1767ab84be7eSDavid Ahern 1768ab84be7eSDavid Ahern return err; 1769ab84be7eSDavid Ahern } 1770ab84be7eSDavid Ahern 1771ab84be7eSDavid Ahern static int nh_valid_get_del_req(struct nlmsghdr *nlh, u32 *id, 1772ab84be7eSDavid Ahern struct netlink_ext_ack *extack) 1773ab84be7eSDavid Ahern { 1774ab84be7eSDavid Ahern struct nhmsg *nhm = nlmsg_data(nlh); 1775ab84be7eSDavid Ahern struct nlattr *tb[NHA_MAX + 1]; 1776ab84be7eSDavid Ahern int err, i; 1777ab84be7eSDavid Ahern 1778ab84be7eSDavid Ahern err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy, 1779ab84be7eSDavid Ahern extack); 1780ab84be7eSDavid Ahern if (err < 0) 1781ab84be7eSDavid Ahern return err; 1782ab84be7eSDavid Ahern 1783ab84be7eSDavid Ahern err = -EINVAL; 1784ab84be7eSDavid Ahern for (i = 0; i < __NHA_MAX; ++i) { 1785ab84be7eSDavid Ahern if (!tb[i]) 1786ab84be7eSDavid Ahern continue; 1787ab84be7eSDavid Ahern 1788ab84be7eSDavid Ahern switch (i) { 1789ab84be7eSDavid Ahern case NHA_ID: 1790ab84be7eSDavid Ahern break; 1791ab84be7eSDavid Ahern default: 1792ab84be7eSDavid Ahern NL_SET_ERR_MSG_ATTR(extack, tb[i], 1793ab84be7eSDavid Ahern "Unexpected attribute in request"); 1794ab84be7eSDavid Ahern goto out; 1795ab84be7eSDavid Ahern } 1796ab84be7eSDavid Ahern } 1797ab84be7eSDavid Ahern if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) { 1798ab84be7eSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header"); 1799ab84be7eSDavid Ahern goto out; 1800ab84be7eSDavid Ahern } 1801ab84be7eSDavid Ahern 1802ab84be7eSDavid Ahern if (!tb[NHA_ID]) { 1803ab84be7eSDavid Ahern NL_SET_ERR_MSG(extack, "Nexthop id is missing"); 1804ab84be7eSDavid Ahern goto out; 1805ab84be7eSDavid Ahern } 1806ab84be7eSDavid Ahern 1807ab84be7eSDavid Ahern *id = nla_get_u32(tb[NHA_ID]); 1808ab84be7eSDavid Ahern if (!(*id)) 1809ab84be7eSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid nexthop id"); 1810ab84be7eSDavid Ahern else 1811ab84be7eSDavid Ahern err = 0; 1812ab84be7eSDavid Ahern out: 1813ab84be7eSDavid Ahern return err; 1814ab84be7eSDavid Ahern } 1815ab84be7eSDavid Ahern 1816ab84be7eSDavid Ahern /* rtnl */ 1817ab84be7eSDavid Ahern static int rtm_del_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh, 1818ab84be7eSDavid Ahern struct netlink_ext_ack *extack) 1819ab84be7eSDavid Ahern { 1820ab84be7eSDavid Ahern struct net *net = sock_net(skb->sk); 1821ab84be7eSDavid Ahern struct nl_info nlinfo = { 1822ab84be7eSDavid Ahern .nlh = nlh, 1823ab84be7eSDavid Ahern .nl_net = net, 1824ab84be7eSDavid Ahern .portid = NETLINK_CB(skb).portid, 1825ab84be7eSDavid Ahern }; 1826ab84be7eSDavid Ahern struct nexthop *nh; 1827ab84be7eSDavid Ahern int err; 1828ab84be7eSDavid Ahern u32 id; 1829ab84be7eSDavid Ahern 1830ab84be7eSDavid Ahern err = nh_valid_get_del_req(nlh, &id, extack); 1831ab84be7eSDavid Ahern if (err) 1832ab84be7eSDavid Ahern return err; 1833ab84be7eSDavid Ahern 1834ab84be7eSDavid Ahern nh = nexthop_find_by_id(net, id); 1835ab84be7eSDavid Ahern if (!nh) 1836ab84be7eSDavid Ahern return -ENOENT; 1837ab84be7eSDavid Ahern 1838430a0491SDavid Ahern remove_nexthop(net, nh, &nlinfo); 1839ab84be7eSDavid Ahern 1840ab84be7eSDavid Ahern return 0; 1841ab84be7eSDavid Ahern } 1842ab84be7eSDavid Ahern 1843ab84be7eSDavid Ahern /* rtnl */ 1844ab84be7eSDavid Ahern static int rtm_get_nexthop(struct sk_buff *in_skb, struct nlmsghdr *nlh, 1845ab84be7eSDavid Ahern struct netlink_ext_ack *extack) 1846ab84be7eSDavid Ahern { 1847ab84be7eSDavid Ahern struct net *net = sock_net(in_skb->sk); 1848ab84be7eSDavid Ahern struct sk_buff *skb = NULL; 1849ab84be7eSDavid Ahern struct nexthop *nh; 1850ab84be7eSDavid Ahern int err; 1851ab84be7eSDavid Ahern u32 id; 1852ab84be7eSDavid Ahern 1853ab84be7eSDavid Ahern err = nh_valid_get_del_req(nlh, &id, extack); 1854ab84be7eSDavid Ahern if (err) 1855ab84be7eSDavid Ahern return err; 1856ab84be7eSDavid Ahern 1857ab84be7eSDavid Ahern err = -ENOBUFS; 1858ab84be7eSDavid Ahern skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 1859ab84be7eSDavid Ahern if (!skb) 1860ab84be7eSDavid Ahern goto out; 1861ab84be7eSDavid Ahern 1862ab84be7eSDavid Ahern err = -ENOENT; 1863ab84be7eSDavid Ahern nh = nexthop_find_by_id(net, id); 1864ab84be7eSDavid Ahern if (!nh) 1865ab84be7eSDavid Ahern goto errout_free; 1866ab84be7eSDavid Ahern 1867ab84be7eSDavid Ahern err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, NETLINK_CB(in_skb).portid, 1868ab84be7eSDavid Ahern nlh->nlmsg_seq, 0); 1869ab84be7eSDavid Ahern if (err < 0) { 1870ab84be7eSDavid Ahern WARN_ON(err == -EMSGSIZE); 1871ab84be7eSDavid Ahern goto errout_free; 1872ab84be7eSDavid Ahern } 1873ab84be7eSDavid Ahern 1874ab84be7eSDavid Ahern err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 1875ab84be7eSDavid Ahern out: 1876ab84be7eSDavid Ahern return err; 1877ab84be7eSDavid Ahern errout_free: 1878ab84be7eSDavid Ahern kfree_skb(skb); 1879ab84be7eSDavid Ahern goto out; 1880ab84be7eSDavid Ahern } 1881ab84be7eSDavid Ahern 1882430a0491SDavid Ahern static bool nh_dump_filtered(struct nexthop *nh, int dev_idx, int master_idx, 1883430a0491SDavid Ahern bool group_filter, u8 family) 1884ab84be7eSDavid Ahern { 1885ab84be7eSDavid Ahern const struct net_device *dev; 1886ab84be7eSDavid Ahern const struct nh_info *nhi; 1887ab84be7eSDavid Ahern 1888430a0491SDavid Ahern if (group_filter && !nh->is_group) 1889430a0491SDavid Ahern return true; 1890430a0491SDavid Ahern 1891ab84be7eSDavid Ahern if (!dev_idx && !master_idx && !family) 1892ab84be7eSDavid Ahern return false; 1893ab84be7eSDavid Ahern 1894430a0491SDavid Ahern if (nh->is_group) 1895430a0491SDavid Ahern return true; 1896430a0491SDavid Ahern 1897ab84be7eSDavid Ahern nhi = rtnl_dereference(nh->nh_info); 1898ab84be7eSDavid Ahern if (family && nhi->family != family) 1899ab84be7eSDavid Ahern return true; 1900ab84be7eSDavid Ahern 1901ab84be7eSDavid Ahern dev = nhi->fib_nhc.nhc_dev; 1902ab84be7eSDavid Ahern if (dev_idx && (!dev || dev->ifindex != dev_idx)) 1903ab84be7eSDavid Ahern return true; 1904ab84be7eSDavid Ahern 1905ab84be7eSDavid Ahern if (master_idx) { 1906ab84be7eSDavid Ahern struct net_device *master; 1907ab84be7eSDavid Ahern 1908ab84be7eSDavid Ahern if (!dev) 1909ab84be7eSDavid Ahern return true; 1910ab84be7eSDavid Ahern 1911ab84be7eSDavid Ahern master = netdev_master_upper_dev_get((struct net_device *)dev); 1912ab84be7eSDavid Ahern if (!master || master->ifindex != master_idx) 1913ab84be7eSDavid Ahern return true; 1914ab84be7eSDavid Ahern } 1915ab84be7eSDavid Ahern 1916ab84be7eSDavid Ahern return false; 1917ab84be7eSDavid Ahern } 1918ab84be7eSDavid Ahern 1919430a0491SDavid Ahern static int nh_valid_dump_req(const struct nlmsghdr *nlh, int *dev_idx, 1920430a0491SDavid Ahern int *master_idx, bool *group_filter, 192138428d68SRoopa Prabhu bool *fdb_filter, struct netlink_callback *cb) 1922ab84be7eSDavid Ahern { 1923ab84be7eSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 1924ab84be7eSDavid Ahern struct nlattr *tb[NHA_MAX + 1]; 1925ab84be7eSDavid Ahern struct nhmsg *nhm; 1926ab84be7eSDavid Ahern int err, i; 1927ab84be7eSDavid Ahern u32 idx; 1928ab84be7eSDavid Ahern 1929ab84be7eSDavid Ahern err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy, 1930ab84be7eSDavid Ahern NULL); 1931ab84be7eSDavid Ahern if (err < 0) 1932ab84be7eSDavid Ahern return err; 1933ab84be7eSDavid Ahern 1934ab84be7eSDavid Ahern for (i = 0; i <= NHA_MAX; ++i) { 1935ab84be7eSDavid Ahern if (!tb[i]) 1936ab84be7eSDavid Ahern continue; 1937ab84be7eSDavid Ahern 1938ab84be7eSDavid Ahern switch (i) { 1939ab84be7eSDavid Ahern case NHA_OIF: 1940ab84be7eSDavid Ahern idx = nla_get_u32(tb[i]); 1941ab84be7eSDavid Ahern if (idx > INT_MAX) { 1942ab84be7eSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid device index"); 1943ab84be7eSDavid Ahern return -EINVAL; 1944ab84be7eSDavid Ahern } 1945ab84be7eSDavid Ahern *dev_idx = idx; 1946ab84be7eSDavid Ahern break; 1947ab84be7eSDavid Ahern case NHA_MASTER: 1948ab84be7eSDavid Ahern idx = nla_get_u32(tb[i]); 1949ab84be7eSDavid Ahern if (idx > INT_MAX) { 1950ab84be7eSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid master device index"); 1951ab84be7eSDavid Ahern return -EINVAL; 1952ab84be7eSDavid Ahern } 1953ab84be7eSDavid Ahern *master_idx = idx; 1954ab84be7eSDavid Ahern break; 1955430a0491SDavid Ahern case NHA_GROUPS: 1956430a0491SDavid Ahern *group_filter = true; 1957430a0491SDavid Ahern break; 195838428d68SRoopa Prabhu case NHA_FDB: 195938428d68SRoopa Prabhu *fdb_filter = true; 196038428d68SRoopa Prabhu break; 1961ab84be7eSDavid Ahern default: 1962ab84be7eSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in dump request"); 1963ab84be7eSDavid Ahern return -EINVAL; 1964ab84be7eSDavid Ahern } 1965ab84be7eSDavid Ahern } 1966ab84be7eSDavid Ahern 1967ab84be7eSDavid Ahern nhm = nlmsg_data(nlh); 1968ab84be7eSDavid Ahern if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) { 1969ab84be7eSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for nexthop dump request"); 1970ab84be7eSDavid Ahern return -EINVAL; 1971ab84be7eSDavid Ahern } 1972ab84be7eSDavid Ahern 1973ab84be7eSDavid Ahern return 0; 1974ab84be7eSDavid Ahern } 1975ab84be7eSDavid Ahern 1976ab84be7eSDavid Ahern /* rtnl */ 1977ab84be7eSDavid Ahern static int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb) 1978ab84be7eSDavid Ahern { 197938428d68SRoopa Prabhu bool group_filter = false, fdb_filter = false; 1980ab84be7eSDavid Ahern struct nhmsg *nhm = nlmsg_data(cb->nlh); 1981ab84be7eSDavid Ahern int dev_filter_idx = 0, master_idx = 0; 1982ab84be7eSDavid Ahern struct net *net = sock_net(skb->sk); 1983ab84be7eSDavid Ahern struct rb_root *root = &net->nexthop.rb_root; 1984ab84be7eSDavid Ahern struct rb_node *node; 1985ab84be7eSDavid Ahern int idx = 0, s_idx; 1986ab84be7eSDavid Ahern int err; 1987ab84be7eSDavid Ahern 1988430a0491SDavid Ahern err = nh_valid_dump_req(cb->nlh, &dev_filter_idx, &master_idx, 198938428d68SRoopa Prabhu &group_filter, &fdb_filter, cb); 1990ab84be7eSDavid Ahern if (err < 0) 1991ab84be7eSDavid Ahern return err; 1992ab84be7eSDavid Ahern 1993ab84be7eSDavid Ahern s_idx = cb->args[0]; 1994ab84be7eSDavid Ahern for (node = rb_first(root); node; node = rb_next(node)) { 1995ab84be7eSDavid Ahern struct nexthop *nh; 1996ab84be7eSDavid Ahern 1997ab84be7eSDavid Ahern if (idx < s_idx) 1998ab84be7eSDavid Ahern goto cont; 1999ab84be7eSDavid Ahern 2000ab84be7eSDavid Ahern nh = rb_entry(node, struct nexthop, rb_node); 2001ab84be7eSDavid Ahern if (nh_dump_filtered(nh, dev_filter_idx, master_idx, 2002430a0491SDavid Ahern group_filter, nhm->nh_family)) 2003ab84be7eSDavid Ahern goto cont; 2004ab84be7eSDavid Ahern 2005ab84be7eSDavid Ahern err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, 2006ab84be7eSDavid Ahern NETLINK_CB(cb->skb).portid, 2007ab84be7eSDavid Ahern cb->nlh->nlmsg_seq, NLM_F_MULTI); 2008ab84be7eSDavid Ahern if (err < 0) { 2009ab84be7eSDavid Ahern if (likely(skb->len)) 2010ab84be7eSDavid Ahern goto out; 2011ab84be7eSDavid Ahern 2012ab84be7eSDavid Ahern goto out_err; 2013ab84be7eSDavid Ahern } 2014ab84be7eSDavid Ahern cont: 2015ab84be7eSDavid Ahern idx++; 2016ab84be7eSDavid Ahern } 2017ab84be7eSDavid Ahern 2018ab84be7eSDavid Ahern out: 2019ab84be7eSDavid Ahern err = skb->len; 2020ab84be7eSDavid Ahern out_err: 2021ab84be7eSDavid Ahern cb->args[0] = idx; 2022ab84be7eSDavid Ahern cb->seq = net->nexthop.seq; 2023ab84be7eSDavid Ahern nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 2024ab84be7eSDavid Ahern 2025ab84be7eSDavid Ahern return err; 2026ab84be7eSDavid Ahern } 2027ab84be7eSDavid Ahern 2028597cfe4fSDavid Ahern static void nexthop_sync_mtu(struct net_device *dev, u32 orig_mtu) 2029597cfe4fSDavid Ahern { 2030597cfe4fSDavid Ahern unsigned int hash = nh_dev_hashfn(dev->ifindex); 2031597cfe4fSDavid Ahern struct net *net = dev_net(dev); 2032597cfe4fSDavid Ahern struct hlist_head *head = &net->nexthop.devhash[hash]; 2033597cfe4fSDavid Ahern struct hlist_node *n; 2034597cfe4fSDavid Ahern struct nh_info *nhi; 2035597cfe4fSDavid Ahern 2036597cfe4fSDavid Ahern hlist_for_each_entry_safe(nhi, n, head, dev_hash) { 2037597cfe4fSDavid Ahern if (nhi->fib_nhc.nhc_dev == dev) { 2038597cfe4fSDavid Ahern if (nhi->family == AF_INET) 2039597cfe4fSDavid Ahern fib_nhc_update_mtu(&nhi->fib_nhc, dev->mtu, 2040597cfe4fSDavid Ahern orig_mtu); 2041597cfe4fSDavid Ahern } 2042597cfe4fSDavid Ahern } 2043597cfe4fSDavid Ahern } 2044597cfe4fSDavid Ahern 2045597cfe4fSDavid Ahern /* rtnl */ 2046597cfe4fSDavid Ahern static int nh_netdev_event(struct notifier_block *this, 2047597cfe4fSDavid Ahern unsigned long event, void *ptr) 2048597cfe4fSDavid Ahern { 2049597cfe4fSDavid Ahern struct net_device *dev = netdev_notifier_info_to_dev(ptr); 2050597cfe4fSDavid Ahern struct netdev_notifier_info_ext *info_ext; 2051597cfe4fSDavid Ahern 2052597cfe4fSDavid Ahern switch (event) { 2053597cfe4fSDavid Ahern case NETDEV_DOWN: 2054597cfe4fSDavid Ahern case NETDEV_UNREGISTER: 2055597cfe4fSDavid Ahern nexthop_flush_dev(dev); 2056597cfe4fSDavid Ahern break; 2057597cfe4fSDavid Ahern case NETDEV_CHANGE: 2058597cfe4fSDavid Ahern if (!(dev_get_flags(dev) & (IFF_RUNNING | IFF_LOWER_UP))) 2059597cfe4fSDavid Ahern nexthop_flush_dev(dev); 2060597cfe4fSDavid Ahern break; 2061597cfe4fSDavid Ahern case NETDEV_CHANGEMTU: 2062597cfe4fSDavid Ahern info_ext = ptr; 2063597cfe4fSDavid Ahern nexthop_sync_mtu(dev, info_ext->ext.mtu); 2064597cfe4fSDavid Ahern rt_cache_flush(dev_net(dev)); 2065597cfe4fSDavid Ahern break; 2066597cfe4fSDavid Ahern } 2067597cfe4fSDavid Ahern return NOTIFY_DONE; 2068597cfe4fSDavid Ahern } 2069597cfe4fSDavid Ahern 2070597cfe4fSDavid Ahern static struct notifier_block nh_netdev_notifier = { 2071597cfe4fSDavid Ahern .notifier_call = nh_netdev_event, 2072597cfe4fSDavid Ahern }; 2073597cfe4fSDavid Ahern 20748590ceedSRoopa Prabhu int register_nexthop_notifier(struct net *net, struct notifier_block *nb) 20758590ceedSRoopa Prabhu { 207680690ec6SIdo Schimmel return blocking_notifier_chain_register(&net->nexthop.notifier_chain, 207780690ec6SIdo Schimmel nb); 20788590ceedSRoopa Prabhu } 20798590ceedSRoopa Prabhu EXPORT_SYMBOL(register_nexthop_notifier); 20808590ceedSRoopa Prabhu 20818590ceedSRoopa Prabhu int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb) 20828590ceedSRoopa Prabhu { 208380690ec6SIdo Schimmel return blocking_notifier_chain_unregister(&net->nexthop.notifier_chain, 20848590ceedSRoopa Prabhu nb); 20858590ceedSRoopa Prabhu } 20868590ceedSRoopa Prabhu EXPORT_SYMBOL(unregister_nexthop_notifier); 20878590ceedSRoopa Prabhu 2088e95f2592SIdo Schimmel void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap) 2089e95f2592SIdo Schimmel { 2090e95f2592SIdo Schimmel struct nexthop *nexthop; 2091e95f2592SIdo Schimmel 2092e95f2592SIdo Schimmel rcu_read_lock(); 2093e95f2592SIdo Schimmel 2094e95f2592SIdo Schimmel nexthop = nexthop_find_by_id(net, id); 2095e95f2592SIdo Schimmel if (!nexthop) 2096e95f2592SIdo Schimmel goto out; 2097e95f2592SIdo Schimmel 2098e95f2592SIdo Schimmel nexthop->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP); 2099e95f2592SIdo Schimmel if (offload) 2100e95f2592SIdo Schimmel nexthop->nh_flags |= RTNH_F_OFFLOAD; 2101e95f2592SIdo Schimmel if (trap) 2102e95f2592SIdo Schimmel nexthop->nh_flags |= RTNH_F_TRAP; 2103e95f2592SIdo Schimmel 2104e95f2592SIdo Schimmel out: 2105e95f2592SIdo Schimmel rcu_read_unlock(); 2106e95f2592SIdo Schimmel } 2107e95f2592SIdo Schimmel EXPORT_SYMBOL(nexthop_set_hw_flags); 2108e95f2592SIdo Schimmel 2109ab84be7eSDavid Ahern static void __net_exit nexthop_net_exit(struct net *net) 2110ab84be7eSDavid Ahern { 2111ab84be7eSDavid Ahern rtnl_lock(); 2112ab84be7eSDavid Ahern flush_all_nexthops(net); 2113ab84be7eSDavid Ahern rtnl_unlock(); 2114597cfe4fSDavid Ahern kfree(net->nexthop.devhash); 2115ab84be7eSDavid Ahern } 2116ab84be7eSDavid Ahern 2117ab84be7eSDavid Ahern static int __net_init nexthop_net_init(struct net *net) 2118ab84be7eSDavid Ahern { 2119597cfe4fSDavid Ahern size_t sz = sizeof(struct hlist_head) * NH_DEV_HASHSIZE; 2120597cfe4fSDavid Ahern 2121ab84be7eSDavid Ahern net->nexthop.rb_root = RB_ROOT; 2122597cfe4fSDavid Ahern net->nexthop.devhash = kzalloc(sz, GFP_KERNEL); 2123597cfe4fSDavid Ahern if (!net->nexthop.devhash) 2124597cfe4fSDavid Ahern return -ENOMEM; 212580690ec6SIdo Schimmel BLOCKING_INIT_NOTIFIER_HEAD(&net->nexthop.notifier_chain); 2126ab84be7eSDavid Ahern 2127ab84be7eSDavid Ahern return 0; 2128ab84be7eSDavid Ahern } 2129ab84be7eSDavid Ahern 2130ab84be7eSDavid Ahern static struct pernet_operations nexthop_net_ops = { 2131ab84be7eSDavid Ahern .init = nexthop_net_init, 2132ab84be7eSDavid Ahern .exit = nexthop_net_exit, 2133ab84be7eSDavid Ahern }; 2134ab84be7eSDavid Ahern 2135ab84be7eSDavid Ahern static int __init nexthop_init(void) 2136ab84be7eSDavid Ahern { 2137ab84be7eSDavid Ahern register_pernet_subsys(&nexthop_net_ops); 2138ab84be7eSDavid Ahern 2139597cfe4fSDavid Ahern register_netdevice_notifier(&nh_netdev_notifier); 2140597cfe4fSDavid Ahern 2141ab84be7eSDavid Ahern rtnl_register(PF_UNSPEC, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0); 2142ab84be7eSDavid Ahern rtnl_register(PF_UNSPEC, RTM_DELNEXTHOP, rtm_del_nexthop, NULL, 0); 2143ab84be7eSDavid Ahern rtnl_register(PF_UNSPEC, RTM_GETNEXTHOP, rtm_get_nexthop, 2144ab84be7eSDavid Ahern rtm_dump_nexthop, 0); 2145ab84be7eSDavid Ahern 2146ab84be7eSDavid Ahern rtnl_register(PF_INET, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0); 2147ab84be7eSDavid Ahern rtnl_register(PF_INET, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0); 2148ab84be7eSDavid Ahern 2149ab84be7eSDavid Ahern rtnl_register(PF_INET6, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0); 2150ab84be7eSDavid Ahern rtnl_register(PF_INET6, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0); 2151ab84be7eSDavid Ahern 2152ab84be7eSDavid Ahern return 0; 2153ab84be7eSDavid Ahern } 2154ab84be7eSDavid Ahern subsys_initcall(nexthop_init); 2155