Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.5.6.
   1// SPDX-License-Identifier: GPL-2.0
   2/* Generic nexthop implementation
   3 *
   4 * Copyright (c) 2017-19 Cumulus Networks
   5 * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com>
   6 */
   7
   8#include <linux/nexthop.h>
   9#include <linux/rtnetlink.h>
  10#include <linux/slab.h>
  11#include <net/arp.h>
  12#include <net/ipv6_stubs.h>
  13#include <net/lwtunnel.h>
  14#include <net/ndisc.h>
  15#include <net/nexthop.h>
  16#include <net/route.h>
  17#include <net/sock.h>
  18
  19static void remove_nexthop(struct net *net, struct nexthop *nh,
  20			   struct nl_info *nlinfo);
  21
  22#define NH_DEV_HASHBITS  8
  23#define NH_DEV_HASHSIZE (1U << NH_DEV_HASHBITS)
  24
  25static const struct nla_policy rtm_nh_policy[NHA_MAX + 1] = {
  26	[NHA_ID]		= { .type = NLA_U32 },
  27	[NHA_GROUP]		= { .type = NLA_BINARY },
  28	[NHA_GROUP_TYPE]	= { .type = NLA_U16 },
  29	[NHA_BLACKHOLE]		= { .type = NLA_FLAG },
  30	[NHA_OIF]		= { .type = NLA_U32 },
  31	[NHA_GATEWAY]		= { .type = NLA_BINARY },
  32	[NHA_ENCAP_TYPE]	= { .type = NLA_U16 },
  33	[NHA_ENCAP]		= { .type = NLA_NESTED },
  34	[NHA_GROUPS]		= { .type = NLA_FLAG },
  35	[NHA_MASTER]		= { .type = NLA_U32 },
  36	[NHA_FDB]		= { .type = NLA_FLAG },
  37};
  38
  39static int call_nexthop_notifiers(struct net *net,
  40				  enum nexthop_event_type event_type,
  41				  struct nexthop *nh)
  42{
  43	int err;
  44
  45	err = atomic_notifier_call_chain(&net->nexthop.notifier_chain,
  46					 event_type, nh);
  47	return notifier_to_errno(err);
  48}
  49
  50static unsigned int nh_dev_hashfn(unsigned int val)
  51{
  52	unsigned int mask = NH_DEV_HASHSIZE - 1;
  53
  54	return (val ^
  55		(val >> NH_DEV_HASHBITS) ^
  56		(val >> (NH_DEV_HASHBITS * 2))) & mask;
  57}
  58
  59static void nexthop_devhash_add(struct net *net, struct nh_info *nhi)
  60{
  61	struct net_device *dev = nhi->fib_nhc.nhc_dev;
  62	struct hlist_head *head;
  63	unsigned int hash;
  64
  65	WARN_ON(!dev);
  66
  67	hash = nh_dev_hashfn(dev->ifindex);
  68	head = &net->nexthop.devhash[hash];
  69	hlist_add_head(&nhi->dev_hash, head);
  70}
  71
  72static void nexthop_free_mpath(struct nexthop *nh)
  73{
  74	struct nh_group *nhg;
  75	int i;
  76
  77	nhg = rcu_dereference_raw(nh->nh_grp);
  78	for (i = 0; i < nhg->num_nh; ++i) {
  79		struct nh_grp_entry *nhge = &nhg->nh_entries[i];
  80
  81		WARN_ON(!list_empty(&nhge->nh_list));
  82		nexthop_put(nhge->nh);
  83	}
  84
  85	WARN_ON(nhg->spare == nhg);
  86
  87	kfree(nhg->spare);
  88	kfree(nhg);
  89}
  90
  91static void nexthop_free_single(struct nexthop *nh)
  92{
  93	struct nh_info *nhi;
  94
  95	nhi = rcu_dereference_raw(nh->nh_info);
  96	switch (nhi->family) {
  97	case AF_INET:
  98		fib_nh_release(nh->net, &nhi->fib_nh);
  99		break;
 100	case AF_INET6:
 101		ipv6_stub->fib6_nh_release(&nhi->fib6_nh);
 102		break;
 103	}
 104	kfree(nhi);
 105}
 106
 107void nexthop_free_rcu(struct rcu_head *head)
 108{
 109	struct nexthop *nh = container_of(head, struct nexthop, rcu);
 110
 111	if (nh->is_group)
 112		nexthop_free_mpath(nh);
 113	else
 114		nexthop_free_single(nh);
 115
 116	kfree(nh);
 117}
 118EXPORT_SYMBOL_GPL(nexthop_free_rcu);
 119
 120static struct nexthop *nexthop_alloc(void)
 121{
 122	struct nexthop *nh;
 123
 124	nh = kzalloc(sizeof(struct nexthop), GFP_KERNEL);
 125	if (nh) {
 126		INIT_LIST_HEAD(&nh->fi_list);
 127		INIT_LIST_HEAD(&nh->f6i_list);
 128		INIT_LIST_HEAD(&nh->grp_list);
 129		INIT_LIST_HEAD(&nh->fdb_list);
 130	}
 131	return nh;
 132}
 133
 134static struct nh_group *nexthop_grp_alloc(u16 num_nh)
 135{
 136	size_t sz = offsetof(struct nexthop, nh_grp)
 137		    + sizeof(struct nh_group)
 138		    + sizeof(struct nh_grp_entry) * num_nh;
 139	struct nh_group *nhg;
 140
 141	nhg = kzalloc(sz, GFP_KERNEL);
 142	if (nhg)
 143		nhg->num_nh = num_nh;
 144
 145	return nhg;
 146}
 147
 148static void nh_base_seq_inc(struct net *net)
 149{
 150	while (++net->nexthop.seq == 0)
 151		;
 152}
 153
 154/* no reference taken; rcu lock or rtnl must be held */
 155struct nexthop *nexthop_find_by_id(struct net *net, u32 id)
 156{
 157	struct rb_node **pp, *parent = NULL, *next;
 158
 159	pp = &net->nexthop.rb_root.rb_node;
 160	while (1) {
 161		struct nexthop *nh;
 162
 163		next = rcu_dereference_raw(*pp);
 164		if (!next)
 165			break;
 166		parent = next;
 167
 168		nh = rb_entry(parent, struct nexthop, rb_node);
 169		if (id < nh->id)
 170			pp = &next->rb_left;
 171		else if (id > nh->id)
 172			pp = &next->rb_right;
 173		else
 174			return nh;
 175	}
 176	return NULL;
 177}
 178EXPORT_SYMBOL_GPL(nexthop_find_by_id);
 179
 180/* used for auto id allocation; called with rtnl held */
 181static u32 nh_find_unused_id(struct net *net)
 182{
 183	u32 id_start = net->nexthop.last_id_allocated;
 184
 185	while (1) {
 186		net->nexthop.last_id_allocated++;
 187		if (net->nexthop.last_id_allocated == id_start)
 188			break;
 189
 190		if (!nexthop_find_by_id(net, net->nexthop.last_id_allocated))
 191			return net->nexthop.last_id_allocated;
 192	}
 193	return 0;
 194}
 195
 196static int nla_put_nh_group(struct sk_buff *skb, struct nh_group *nhg)
 197{
 198	struct nexthop_grp *p;
 199	size_t len = nhg->num_nh * sizeof(*p);
 200	struct nlattr *nla;
 201	u16 group_type = 0;
 202	int i;
 203
 204	if (nhg->mpath)
 205		group_type = NEXTHOP_GRP_TYPE_MPATH;
 206
 207	if (nla_put_u16(skb, NHA_GROUP_TYPE, group_type))
 208		goto nla_put_failure;
 209
 210	nla = nla_reserve(skb, NHA_GROUP, len);
 211	if (!nla)
 212		goto nla_put_failure;
 213
 214	p = nla_data(nla);
 215	for (i = 0; i < nhg->num_nh; ++i) {
 216		p->id = nhg->nh_entries[i].nh->id;
 217		p->weight = nhg->nh_entries[i].weight - 1;
 218		p += 1;
 219	}
 220
 221	return 0;
 222
 223nla_put_failure:
 224	return -EMSGSIZE;
 225}
 226
 227static int nh_fill_node(struct sk_buff *skb, struct nexthop *nh,
 228			int event, u32 portid, u32 seq, unsigned int nlflags)
 229{
 230	struct fib6_nh *fib6_nh;
 231	struct fib_nh *fib_nh;
 232	struct nlmsghdr *nlh;
 233	struct nh_info *nhi;
 234	struct nhmsg *nhm;
 235
 236	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nhm), nlflags);
 237	if (!nlh)
 238		return -EMSGSIZE;
 239
 240	nhm = nlmsg_data(nlh);
 241	nhm->nh_family = AF_UNSPEC;
 242	nhm->nh_flags = nh->nh_flags;
 243	nhm->nh_protocol = nh->protocol;
 244	nhm->nh_scope = 0;
 245	nhm->resvd = 0;
 246
 247	if (nla_put_u32(skb, NHA_ID, nh->id))
 248		goto nla_put_failure;
 249
 250	if (nh->is_group) {
 251		struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
 252
 253		if (nhg->fdb_nh && nla_put_flag(skb, NHA_FDB))
 254			goto nla_put_failure;
 255		if (nla_put_nh_group(skb, nhg))
 256			goto nla_put_failure;
 257		goto out;
 258	}
 259
 260	nhi = rtnl_dereference(nh->nh_info);
 261	nhm->nh_family = nhi->family;
 262	if (nhi->reject_nh) {
 263		if (nla_put_flag(skb, NHA_BLACKHOLE))
 264			goto nla_put_failure;
 265		goto out;
 266	} else if (nhi->fdb_nh) {
 267		if (nla_put_flag(skb, NHA_FDB))
 268			goto nla_put_failure;
 269	} else {
 270		const struct net_device *dev;
 271
 272		dev = nhi->fib_nhc.nhc_dev;
 273		if (dev && nla_put_u32(skb, NHA_OIF, dev->ifindex))
 274			goto nla_put_failure;
 275	}
 276
 277	nhm->nh_scope = nhi->fib_nhc.nhc_scope;
 278	switch (nhi->family) {
 279	case AF_INET:
 280		fib_nh = &nhi->fib_nh;
 281		if (fib_nh->fib_nh_gw_family &&
 282		    nla_put_u32(skb, NHA_GATEWAY, fib_nh->fib_nh_gw4))
 283			goto nla_put_failure;
 284		break;
 285
 286	case AF_INET6:
 287		fib6_nh = &nhi->fib6_nh;
 288		if (fib6_nh->fib_nh_gw_family &&
 289		    nla_put_in6_addr(skb, NHA_GATEWAY, &fib6_nh->fib_nh_gw6))
 290			goto nla_put_failure;
 291		break;
 292	}
 293
 294	if (nhi->fib_nhc.nhc_lwtstate &&
 295	    lwtunnel_fill_encap(skb, nhi->fib_nhc.nhc_lwtstate,
 296				NHA_ENCAP, NHA_ENCAP_TYPE) < 0)
 297		goto nla_put_failure;
 298
 299out:
 300	nlmsg_end(skb, nlh);
 301	return 0;
 302
 303nla_put_failure:
 304	nlmsg_cancel(skb, nlh);
 305	return -EMSGSIZE;
 306}
 307
 308static size_t nh_nlmsg_size_grp(struct nexthop *nh)
 309{
 310	struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
 311	size_t sz = sizeof(struct nexthop_grp) * nhg->num_nh;
 312
 313	return nla_total_size(sz) +
 314	       nla_total_size(2);  /* NHA_GROUP_TYPE */
 315}
 316
 317static size_t nh_nlmsg_size_single(struct nexthop *nh)
 318{
 319	struct nh_info *nhi = rtnl_dereference(nh->nh_info);
 320	size_t sz;
 321
 322	/* covers NHA_BLACKHOLE since NHA_OIF and BLACKHOLE
 323	 * are mutually exclusive
 324	 */
 325	sz = nla_total_size(4);  /* NHA_OIF */
 326
 327	switch (nhi->family) {
 328	case AF_INET:
 329		if (nhi->fib_nh.fib_nh_gw_family)
 330			sz += nla_total_size(4);  /* NHA_GATEWAY */
 331		break;
 332
 333	case AF_INET6:
 334		/* NHA_GATEWAY */
 335		if (nhi->fib6_nh.fib_nh_gw_family)
 336			sz += nla_total_size(sizeof(const struct in6_addr));
 337		break;
 338	}
 339
 340	if (nhi->fib_nhc.nhc_lwtstate) {
 341		sz += lwtunnel_get_encap_size(nhi->fib_nhc.nhc_lwtstate);
 342		sz += nla_total_size(2);  /* NHA_ENCAP_TYPE */
 343	}
 344
 345	return sz;
 346}
 347
 348static size_t nh_nlmsg_size(struct nexthop *nh)
 349{
 350	size_t sz = NLMSG_ALIGN(sizeof(struct nhmsg));
 351
 352	sz += nla_total_size(4); /* NHA_ID */
 353
 354	if (nh->is_group)
 355		sz += nh_nlmsg_size_grp(nh);
 356	else
 357		sz += nh_nlmsg_size_single(nh);
 358
 359	return sz;
 360}
 361
 362static void nexthop_notify(int event, struct nexthop *nh, struct nl_info *info)
 363{
 364	unsigned int nlflags = info->nlh ? info->nlh->nlmsg_flags : 0;
 365	u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
 366	struct sk_buff *skb;
 367	int err = -ENOBUFS;
 368
 369	skb = nlmsg_new(nh_nlmsg_size(nh), gfp_any());
 370	if (!skb)
 371		goto errout;
 372
 373	err = nh_fill_node(skb, nh, event, info->portid, seq, nlflags);
 374	if (err < 0) {
 375		/* -EMSGSIZE implies BUG in nh_nlmsg_size() */
 376		WARN_ON(err == -EMSGSIZE);
 377		kfree_skb(skb);
 378		goto errout;
 379	}
 380
 381	rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_NEXTHOP,
 382		    info->nlh, gfp_any());
 383	return;
 384errout:
 385	if (err < 0)
 386		rtnl_set_sk_err(info->nl_net, RTNLGRP_NEXTHOP, err);
 387}
 388
 389static bool valid_group_nh(struct nexthop *nh, unsigned int npaths,
 390			   bool *is_fdb, struct netlink_ext_ack *extack)
 391{
 392	if (nh->is_group) {
 393		struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
 394
 395		/* nested multipath (group within a group) is not
 396		 * supported
 397		 */
 398		if (nhg->mpath) {
 399			NL_SET_ERR_MSG(extack,
 400				       "Multipath group can not be a nexthop within a group");
 401			return false;
 402		}
 403		*is_fdb = nhg->fdb_nh;
 404	} else {
 405		struct nh_info *nhi = rtnl_dereference(nh->nh_info);
 406
 407		if (nhi->reject_nh && npaths > 1) {
 408			NL_SET_ERR_MSG(extack,
 409				       "Blackhole nexthop can not be used in a group with more than 1 path");
 410			return false;
 411		}
 412		*is_fdb = nhi->fdb_nh;
 413	}
 414
 415	return true;
 416}
 417
 418static int nh_check_attr_fdb_group(struct nexthop *nh, u8 *nh_family,
 419				   struct netlink_ext_ack *extack)
 420{
 421	struct nh_info *nhi;
 422
 423	nhi = rtnl_dereference(nh->nh_info);
 424
 425	if (!nhi->fdb_nh) {
 426		NL_SET_ERR_MSG(extack, "FDB nexthop group can only have fdb nexthops");
 427		return -EINVAL;
 428	}
 429
 430	if (*nh_family == AF_UNSPEC) {
 431		*nh_family = nhi->family;
 432	} else if (*nh_family != nhi->family) {
 433		NL_SET_ERR_MSG(extack, "FDB nexthop group cannot have mixed family nexthops");
 434		return -EINVAL;
 435	}
 436
 437	return 0;
 438}
 439
 440static int nh_check_attr_group(struct net *net, struct nlattr *tb[],
 441			       struct netlink_ext_ack *extack)
 442{
 443	unsigned int len = nla_len(tb[NHA_GROUP]);
 444	u8 nh_family = AF_UNSPEC;
 445	struct nexthop_grp *nhg;
 446	unsigned int i, j;
 447	u8 nhg_fdb = 0;
 448
 449	if (!len || len & (sizeof(struct nexthop_grp) - 1)) {
 450		NL_SET_ERR_MSG(extack,
 451			       "Invalid length for nexthop group attribute");
 452		return -EINVAL;
 453	}
 454
 455	/* convert len to number of nexthop ids */
 456	len /= sizeof(*nhg);
 457
 458	nhg = nla_data(tb[NHA_GROUP]);
 459	for (i = 0; i < len; ++i) {
 460		if (nhg[i].resvd1 || nhg[i].resvd2) {
 461			NL_SET_ERR_MSG(extack, "Reserved fields in nexthop_grp must be 0");
 462			return -EINVAL;
 463		}
 464		if (nhg[i].weight > 254) {
 465			NL_SET_ERR_MSG(extack, "Invalid value for weight");
 466			return -EINVAL;
 467		}
 468		for (j = i + 1; j < len; ++j) {
 469			if (nhg[i].id == nhg[j].id) {
 470				NL_SET_ERR_MSG(extack, "Nexthop id can not be used twice in a group");
 471				return -EINVAL;
 472			}
 473		}
 474	}
 475
 476	if (tb[NHA_FDB])
 477		nhg_fdb = 1;
 478	nhg = nla_data(tb[NHA_GROUP]);
 479	for (i = 0; i < len; ++i) {
 480		struct nexthop *nh;
 481		bool is_fdb_nh;
 482
 483		nh = nexthop_find_by_id(net, nhg[i].id);
 484		if (!nh) {
 485			NL_SET_ERR_MSG(extack, "Invalid nexthop id");
 486			return -EINVAL;
 487		}
 488		if (!valid_group_nh(nh, len, &is_fdb_nh, extack))
 489			return -EINVAL;
 490
 491		if (nhg_fdb && nh_check_attr_fdb_group(nh, &nh_family, extack))
 492			return -EINVAL;
 493
 494		if (!nhg_fdb && is_fdb_nh) {
 495			NL_SET_ERR_MSG(extack, "Non FDB nexthop group cannot have fdb nexthops");
 496			return -EINVAL;
 497		}
 498	}
 499	for (i = NHA_GROUP_TYPE + 1; i < __NHA_MAX; ++i) {
 500		if (!tb[i])
 501			continue;
 502		if (tb[NHA_FDB])
 503			continue;
 504		NL_SET_ERR_MSG(extack,
 505			       "No other attributes can be set in nexthop groups");
 506		return -EINVAL;
 507	}
 508
 509	return 0;
 510}
 511
 512static bool ipv6_good_nh(const struct fib6_nh *nh)
 513{
 514	int state = NUD_REACHABLE;
 515	struct neighbour *n;
 516
 517	rcu_read_lock_bh();
 518
 519	n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev, &nh->fib_nh_gw6);
 520	if (n)
 521		state = n->nud_state;
 522
 523	rcu_read_unlock_bh();
 524
 525	return !!(state & NUD_VALID);
 526}
 527
 528static bool ipv4_good_nh(const struct fib_nh *nh)
 529{
 530	int state = NUD_REACHABLE;
 531	struct neighbour *n;
 532
 533	rcu_read_lock_bh();
 534
 535	n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
 536				      (__force u32)nh->fib_nh_gw4);
 537	if (n)
 538		state = n->nud_state;
 539
 540	rcu_read_unlock_bh();
 541
 542	return !!(state & NUD_VALID);
 543}
 544
 545struct nexthop *nexthop_select_path(struct nexthop *nh, int hash)
 546{
 547	struct nexthop *rc = NULL;
 548	struct nh_group *nhg;
 549	int i;
 550
 551	if (!nh->is_group)
 552		return nh;
 553
 554	nhg = rcu_dereference(nh->nh_grp);
 555	for (i = 0; i < nhg->num_nh; ++i) {
 556		struct nh_grp_entry *nhge = &nhg->nh_entries[i];
 557		struct nh_info *nhi;
 558
 559		if (hash > atomic_read(&nhge->upper_bound))
 560			continue;
 561
 562		nhi = rcu_dereference(nhge->nh->nh_info);
 563		if (nhi->fdb_nh)
 564			return nhge->nh;
 565
 566		/* nexthops always check if it is good and does
 567		 * not rely on a sysctl for this behavior
 568		 */
 569		switch (nhi->family) {
 570		case AF_INET:
 571			if (ipv4_good_nh(&nhi->fib_nh))
 572				return nhge->nh;
 573			break;
 574		case AF_INET6:
 575			if (ipv6_good_nh(&nhi->fib6_nh))
 576				return nhge->nh;
 577			break;
 578		}
 579
 580		if (!rc)
 581			rc = nhge->nh;
 582	}
 583
 584	return rc;
 585}
 586EXPORT_SYMBOL_GPL(nexthop_select_path);
 587
 588int nexthop_for_each_fib6_nh(struct nexthop *nh,
 589			     int (*cb)(struct fib6_nh *nh, void *arg),
 590			     void *arg)
 591{
 592	struct nh_info *nhi;
 593	int err;
 594
 595	if (nh->is_group) {
 596		struct nh_group *nhg;
 597		int i;
 598
 599		nhg = rcu_dereference_rtnl(nh->nh_grp);
 600		for (i = 0; i < nhg->num_nh; i++) {
 601			struct nh_grp_entry *nhge = &nhg->nh_entries[i];
 602
 603			nhi = rcu_dereference_rtnl(nhge->nh->nh_info);
 604			err = cb(&nhi->fib6_nh, arg);
 605			if (err)
 606				return err;
 607		}
 608	} else {
 609		nhi = rcu_dereference_rtnl(nh->nh_info);
 610		err = cb(&nhi->fib6_nh, arg);
 611		if (err)
 612			return err;
 613	}
 614
 615	return 0;
 616}
 617EXPORT_SYMBOL_GPL(nexthop_for_each_fib6_nh);
 618
 619static int check_src_addr(const struct in6_addr *saddr,
 620			  struct netlink_ext_ack *extack)
 621{
 622	if (!ipv6_addr_any(saddr)) {
 623		NL_SET_ERR_MSG(extack, "IPv6 routes using source address can not use nexthop objects");
 624		return -EINVAL;
 625	}
 626	return 0;
 627}
 628
 629int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
 630		       struct netlink_ext_ack *extack)
 631{
 632	struct nh_info *nhi;
 633	bool is_fdb_nh;
 634
 635	/* fib6_src is unique to a fib6_info and limits the ability to cache
 636	 * routes in fib6_nh within a nexthop that is potentially shared
 637	 * across multiple fib entries. If the config wants to use source
 638	 * routing it can not use nexthop objects. mlxsw also does not allow
 639	 * fib6_src on routes.
 640	 */
 641	if (cfg && check_src_addr(&cfg->fc_src, extack) < 0)
 642		return -EINVAL;
 643
 644	if (nh->is_group) {
 645		struct nh_group *nhg;
 646
 647		nhg = rtnl_dereference(nh->nh_grp);
 648		if (nhg->has_v4)
 649			goto no_v4_nh;
 650		is_fdb_nh = nhg->fdb_nh;
 651	} else {
 652		nhi = rtnl_dereference(nh->nh_info);
 653		if (nhi->family == AF_INET)
 654			goto no_v4_nh;
 655		is_fdb_nh = nhi->fdb_nh;
 656	}
 657
 658	if (is_fdb_nh) {
 659		NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
 660		return -EINVAL;
 661	}
 662
 663	return 0;
 664no_v4_nh:
 665	NL_SET_ERR_MSG(extack, "IPv6 routes can not use an IPv4 nexthop");
 666	return -EINVAL;
 667}
 668EXPORT_SYMBOL_GPL(fib6_check_nexthop);
 669
 670/* if existing nexthop has ipv6 routes linked to it, need
 671 * to verify this new spec works with ipv6
 672 */
 673static int fib6_check_nh_list(struct nexthop *old, struct nexthop *new,
 674			      struct netlink_ext_ack *extack)
 675{
 676	struct fib6_info *f6i;
 677
 678	if (list_empty(&old->f6i_list))
 679		return 0;
 680
 681	list_for_each_entry(f6i, &old->f6i_list, nh_list) {
 682		if (check_src_addr(&f6i->fib6_src.addr, extack) < 0)
 683			return -EINVAL;
 684	}
 685
 686	return fib6_check_nexthop(new, NULL, extack);
 687}
 688
 689static int nexthop_check_scope(struct nh_info *nhi, u8 scope,
 690			       struct netlink_ext_ack *extack)
 691{
 692	if (scope == RT_SCOPE_HOST && nhi->fib_nhc.nhc_gw_family) {
 693		NL_SET_ERR_MSG(extack,
 694			       "Route with host scope can not have a gateway");
 695		return -EINVAL;
 696	}
 697
 698	if (nhi->fib_nhc.nhc_flags & RTNH_F_ONLINK && scope >= RT_SCOPE_LINK) {
 699		NL_SET_ERR_MSG(extack, "Scope mismatch with nexthop");
 700		return -EINVAL;
 701	}
 702
 703	return 0;
 704}
 705
 706/* Invoked by fib add code to verify nexthop by id is ok with
 707 * config for prefix; parts of fib_check_nh not done when nexthop
 708 * object is used.
 709 */
 710int fib_check_nexthop(struct nexthop *nh, u8 scope,
 711		      struct netlink_ext_ack *extack)
 712{
 713	struct nh_info *nhi;
 714	int err = 0;
 715
 716	if (nh->is_group) {
 717		struct nh_group *nhg;
 718
 719		nhg = rtnl_dereference(nh->nh_grp);
 720		if (nhg->fdb_nh) {
 721			NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
 722			err = -EINVAL;
 723			goto out;
 724		}
 725
 726		if (scope == RT_SCOPE_HOST) {
 727			NL_SET_ERR_MSG(extack, "Route with host scope can not have multiple nexthops");
 728			err = -EINVAL;
 729			goto out;
 730		}
 731
 732		/* all nexthops in a group have the same scope */
 733		nhi = rtnl_dereference(nhg->nh_entries[0].nh->nh_info);
 734		err = nexthop_check_scope(nhi, scope, extack);
 735	} else {
 736		nhi = rtnl_dereference(nh->nh_info);
 737		if (nhi->fdb_nh) {
 738			NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
 739			err = -EINVAL;
 740			goto out;
 741		}
 742		err = nexthop_check_scope(nhi, scope, extack);
 743	}
 744
 745out:
 746	return err;
 747}
 748
 749static int fib_check_nh_list(struct nexthop *old, struct nexthop *new,
 750			     struct netlink_ext_ack *extack)
 751{
 752	struct fib_info *fi;
 753
 754	list_for_each_entry(fi, &old->fi_list, nh_list) {
 755		int err;
 756
 757		err = fib_check_nexthop(new, fi->fib_scope, extack);
 758		if (err)
 759			return err;
 760	}
 761	return 0;
 762}
 763
 764static void nh_group_rebalance(struct nh_group *nhg)
 765{
 766	int total = 0;
 767	int w = 0;
 768	int i;
 769
 770	for (i = 0; i < nhg->num_nh; ++i)
 771		total += nhg->nh_entries[i].weight;
 772
 773	for (i = 0; i < nhg->num_nh; ++i) {
 774		struct nh_grp_entry *nhge = &nhg->nh_entries[i];
 775		int upper_bound;
 776
 777		w += nhge->weight;
 778		upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31, total) - 1;
 779		atomic_set(&nhge->upper_bound, upper_bound);
 780	}
 781}
 782
 783static void remove_nh_grp_entry(struct net *net, struct nh_grp_entry *nhge,
 784				struct nl_info *nlinfo)
 785{
 786	struct nh_grp_entry *nhges, *new_nhges;
 787	struct nexthop *nhp = nhge->nh_parent;
 788	struct nexthop *nh = nhge->nh;
 789	struct nh_group *nhg, *newg;
 790	int i, j;
 791
 792	WARN_ON(!nh);
 793
 794	nhg = rtnl_dereference(nhp->nh_grp);
 795	newg = nhg->spare;
 796
 797	/* last entry, keep it visible and remove the parent */
 798	if (nhg->num_nh == 1) {
 799		remove_nexthop(net, nhp, nlinfo);
 800		return;
 801	}
 802
 803	newg->has_v4 = nhg->has_v4;
 804	newg->mpath = nhg->mpath;
 805	newg->fdb_nh = nhg->fdb_nh;
 806	newg->num_nh = nhg->num_nh;
 807
 808	/* copy old entries to new except the one getting removed */
 809	nhges = nhg->nh_entries;
 810	new_nhges = newg->nh_entries;
 811	for (i = 0, j = 0; i < nhg->num_nh; ++i) {
 812		/* current nexthop getting removed */
 813		if (nhg->nh_entries[i].nh == nh) {
 814			newg->num_nh--;
 815			continue;
 816		}
 817
 818		list_del(&nhges[i].nh_list);
 819		new_nhges[j].nh_parent = nhges[i].nh_parent;
 820		new_nhges[j].nh = nhges[i].nh;
 821		new_nhges[j].weight = nhges[i].weight;
 822		list_add(&new_nhges[j].nh_list, &new_nhges[j].nh->grp_list);
 823		j++;
 824	}
 825
 826	nh_group_rebalance(newg);
 827	rcu_assign_pointer(nhp->nh_grp, newg);
 828
 829	list_del(&nhge->nh_list);
 830	nexthop_put(nhge->nh);
 831
 832	if (nlinfo)
 833		nexthop_notify(RTM_NEWNEXTHOP, nhp, nlinfo);
 834}
 835
 836static void remove_nexthop_from_groups(struct net *net, struct nexthop *nh,
 837				       struct nl_info *nlinfo)
 838{
 839	struct nh_grp_entry *nhge, *tmp;
 840
 841	list_for_each_entry_safe(nhge, tmp, &nh->grp_list, nh_list)
 842		remove_nh_grp_entry(net, nhge, nlinfo);
 843
 844	/* make sure all see the newly published array before releasing rtnl */
 845	synchronize_rcu();
 846}
 847
 848static void remove_nexthop_group(struct nexthop *nh, struct nl_info *nlinfo)
 849{
 850	struct nh_group *nhg = rcu_dereference_rtnl(nh->nh_grp);
 851	int i, num_nh = nhg->num_nh;
 852
 853	for (i = 0; i < num_nh; ++i) {
 854		struct nh_grp_entry *nhge = &nhg->nh_entries[i];
 855
 856		if (WARN_ON(!nhge->nh))
 857			continue;
 858
 859		list_del_init(&nhge->nh_list);
 860	}
 861}
 862
 863/* not called for nexthop replace */
 864static void __remove_nexthop_fib(struct net *net, struct nexthop *nh)
 865{
 866	struct fib6_info *f6i, *tmp;
 867	bool do_flush = false;
 868	struct fib_info *fi;
 869
 870	call_nexthop_notifiers(net, NEXTHOP_EVENT_DEL, nh);
 871
 872	list_for_each_entry(fi, &nh->fi_list, nh_list) {
 873		fi->fib_flags |= RTNH_F_DEAD;
 874		do_flush = true;
 875	}
 876	if (do_flush)
 877		fib_flush(net);
 878
 879	/* ip6_del_rt removes the entry from this list hence the _safe */
 880	list_for_each_entry_safe(f6i, tmp, &nh->f6i_list, nh_list) {
 881		/* __ip6_del_rt does a release, so do a hold here */
 882		fib6_info_hold(f6i);
 883		ipv6_stub->ip6_del_rt(net, f6i,
 884				      !net->ipv4.sysctl_nexthop_compat_mode);
 885	}
 886}
 887
 888static void __remove_nexthop(struct net *net, struct nexthop *nh,
 889			     struct nl_info *nlinfo)
 890{
 891	__remove_nexthop_fib(net, nh);
 892
 893	if (nh->is_group) {
 894		remove_nexthop_group(nh, nlinfo);
 895	} else {
 896		struct nh_info *nhi;
 897
 898		nhi = rtnl_dereference(nh->nh_info);
 899		if (nhi->fib_nhc.nhc_dev)
 900			hlist_del(&nhi->dev_hash);
 901
 902		remove_nexthop_from_groups(net, nh, nlinfo);
 903	}
 904}
 905
 906static void remove_nexthop(struct net *net, struct nexthop *nh,
 907			   struct nl_info *nlinfo)
 908{
 909	/* remove from the tree */
 910	rb_erase(&nh->rb_node, &net->nexthop.rb_root);
 911
 912	if (nlinfo)
 913		nexthop_notify(RTM_DELNEXTHOP, nh, nlinfo);
 914
 915	__remove_nexthop(net, nh, nlinfo);
 916	nh_base_seq_inc(net);
 917
 918	nexthop_put(nh);
 919}
 920
 921/* if any FIB entries reference this nexthop, any dst entries
 922 * need to be regenerated
 923 */
 924static void nh_rt_cache_flush(struct net *net, struct nexthop *nh)
 925{
 926	struct fib6_info *f6i;
 927
 928	if (!list_empty(&nh->fi_list))
 929		rt_cache_flush(net);
 930
 931	list_for_each_entry(f6i, &nh->f6i_list, nh_list)
 932		ipv6_stub->fib6_update_sernum(net, f6i);
 933}
 934
 935static int replace_nexthop_grp(struct net *net, struct nexthop *old,
 936			       struct nexthop *new,
 937			       struct netlink_ext_ack *extack)
 938{
 939	struct nh_group *oldg, *newg;
 940	int i;
 941
 942	if (!new->is_group) {
 943		NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with a nexthop.");
 944		return -EINVAL;
 945	}
 946
 947	oldg = rtnl_dereference(old->nh_grp);
 948	newg = rtnl_dereference(new->nh_grp);
 949
 950	/* update parents - used by nexthop code for cleanup */
 951	for (i = 0; i < newg->num_nh; i++)
 952		newg->nh_entries[i].nh_parent = old;
 953
 954	rcu_assign_pointer(old->nh_grp, newg);
 955
 956	for (i = 0; i < oldg->num_nh; i++)
 957		oldg->nh_entries[i].nh_parent = new;
 958
 959	rcu_assign_pointer(new->nh_grp, oldg);
 960
 961	return 0;
 962}
 963
 964static int replace_nexthop_single(struct net *net, struct nexthop *old,
 965				  struct nexthop *new,
 966				  struct netlink_ext_ack *extack)
 967{
 968	struct nh_info *oldi, *newi;
 969
 970	if (new->is_group) {
 971		NL_SET_ERR_MSG(extack, "Can not replace a nexthop with a nexthop group.");
 972		return -EINVAL;
 973	}
 974
 975	oldi = rtnl_dereference(old->nh_info);
 976	newi = rtnl_dereference(new->nh_info);
 977
 978	newi->nh_parent = old;
 979	oldi->nh_parent = new;
 980
 981	old->protocol = new->protocol;
 982	old->nh_flags = new->nh_flags;
 983
 984	rcu_assign_pointer(old->nh_info, newi);
 985	rcu_assign_pointer(new->nh_info, oldi);
 986
 987	return 0;
 988}
 989
 990static void __nexthop_replace_notify(struct net *net, struct nexthop *nh,
 991				     struct nl_info *info)
 992{
 993	struct fib6_info *f6i;
 994
 995	if (!list_empty(&nh->fi_list)) {
 996		struct fib_info *fi;
 997
 998		/* expectation is a few fib_info per nexthop and then
 999		 * a lot of routes per fib_info. So mark the fib_info
1000		 * and then walk the fib tables once
1001		 */
1002		list_for_each_entry(fi, &nh->fi_list, nh_list)
1003			fi->nh_updated = true;
1004
1005		fib_info_notify_update(net, info);
1006
1007		list_for_each_entry(fi, &nh->fi_list, nh_list)
1008			fi->nh_updated = false;
1009	}
1010
1011	list_for_each_entry(f6i, &nh->f6i_list, nh_list)
1012		ipv6_stub->fib6_rt_update(net, f6i, info);
1013}
1014
1015/* send RTM_NEWROUTE with REPLACE flag set for all FIB entries
1016 * linked to this nexthop and for all groups that the nexthop
1017 * is a member of
1018 */
1019static void nexthop_replace_notify(struct net *net, struct nexthop *nh,
1020				   struct nl_info *info)
1021{
1022	struct nh_grp_entry *nhge;
1023
1024	__nexthop_replace_notify(net, nh, info);
1025
1026	list_for_each_entry(nhge, &nh->grp_list, nh_list)
1027		__nexthop_replace_notify(net, nhge->nh_parent, info);
1028}
1029
1030static int replace_nexthop(struct net *net, struct nexthop *old,
1031			   struct nexthop *new, struct netlink_ext_ack *extack)
1032{
1033	bool new_is_reject = false;
1034	struct nh_grp_entry *nhge;
1035	int err;
1036
1037	/* check that existing FIB entries are ok with the
1038	 * new nexthop definition
1039	 */
1040	err = fib_check_nh_list(old, new, extack);
1041	if (err)
1042		return err;
1043
1044	err = fib6_check_nh_list(old, new, extack);
1045	if (err)
1046		return err;
1047
1048	if (!new->is_group) {
1049		struct nh_info *nhi = rtnl_dereference(new->nh_info);
1050
1051		new_is_reject = nhi->reject_nh;
1052	}
1053
1054	list_for_each_entry(nhge, &old->grp_list, nh_list) {
1055		/* if new nexthop is a blackhole, any groups using this
1056		 * nexthop cannot have more than 1 path
1057		 */
1058		if (new_is_reject &&
1059		    nexthop_num_path(nhge->nh_parent) > 1) {
1060			NL_SET_ERR_MSG(extack, "Blackhole nexthop can not be a member of a group with more than one path");
1061			return -EINVAL;
1062		}
1063
1064		err = fib_check_nh_list(nhge->nh_parent, new, extack);
1065		if (err)
1066			return err;
1067
1068		err = fib6_check_nh_list(nhge->nh_parent, new, extack);
1069		if (err)
1070			return err;
1071	}
1072
1073	if (old->is_group)
1074		err = replace_nexthop_grp(net, old, new, extack);
1075	else
1076		err = replace_nexthop_single(net, old, new, extack);
1077
1078	if (!err) {
1079		nh_rt_cache_flush(net, old);
1080
1081		__remove_nexthop(net, new, NULL);
1082		nexthop_put(new);
1083	}
1084
1085	return err;
1086}
1087
1088/* called with rtnl_lock held */
1089static int insert_nexthop(struct net *net, struct nexthop *new_nh,
1090			  struct nh_config *cfg, struct netlink_ext_ack *extack)
1091{
1092	struct rb_node **pp, *parent = NULL, *next;
1093	struct rb_root *root = &net->nexthop.rb_root;
1094	bool replace = !!(cfg->nlflags & NLM_F_REPLACE);
1095	bool create = !!(cfg->nlflags & NLM_F_CREATE);
1096	u32 new_id = new_nh->id;
1097	int replace_notify = 0;
1098	int rc = -EEXIST;
1099
1100	pp = &root->rb_node;
1101	while (1) {
1102		struct nexthop *nh;
1103
1104		next = rtnl_dereference(*pp);
1105		if (!next)
1106			break;
1107
1108		parent = next;
1109
1110		nh = rb_entry(parent, struct nexthop, rb_node);
1111		if (new_id < nh->id) {
1112			pp = &next->rb_left;
1113		} else if (new_id > nh->id) {
1114			pp = &next->rb_right;
1115		} else if (replace) {
1116			rc = replace_nexthop(net, nh, new_nh, extack);
1117			if (!rc) {
1118				new_nh = nh; /* send notification with old nh */
1119				replace_notify = 1;
1120			}
1121			goto out;
1122		} else {
1123			/* id already exists and not a replace */
1124			goto out;
1125		}
1126	}
1127
1128	if (replace && !create) {
1129		NL_SET_ERR_MSG(extack, "Replace specified without create and no entry exists");
1130		rc = -ENOENT;
1131		goto out;
1132	}
1133
1134	rb_link_node_rcu(&new_nh->rb_node, parent, pp);
1135	rb_insert_color(&new_nh->rb_node, root);
1136	rc = 0;
1137out:
1138	if (!rc) {
1139		nh_base_seq_inc(net);
1140		nexthop_notify(RTM_NEWNEXTHOP, new_nh, &cfg->nlinfo);
1141		if (replace_notify && net->ipv4.sysctl_nexthop_compat_mode)
1142			nexthop_replace_notify(net, new_nh, &cfg->nlinfo);
1143	}
1144
1145	return rc;
1146}
1147
1148/* rtnl */
1149/* remove all nexthops tied to a device being deleted */
1150static void nexthop_flush_dev(struct net_device *dev)
1151{
1152	unsigned int hash = nh_dev_hashfn(dev->ifindex);
1153	struct net *net = dev_net(dev);
1154	struct hlist_head *head = &net->nexthop.devhash[hash];
1155	struct hlist_node *n;
1156	struct nh_info *nhi;
1157
1158	hlist_for_each_entry_safe(nhi, n, head, dev_hash) {
1159		if (nhi->fib_nhc.nhc_dev != dev)
1160			continue;
1161
1162		remove_nexthop(net, nhi->nh_parent, NULL);
1163	}
1164}
1165
1166/* rtnl; called when net namespace is deleted */
1167static void flush_all_nexthops(struct net *net)
1168{
1169	struct rb_root *root = &net->nexthop.rb_root;
1170	struct rb_node *node;
1171	struct nexthop *nh;
1172
1173	while ((node = rb_first(root))) {
1174		nh = rb_entry(node, struct nexthop, rb_node);
1175		remove_nexthop(net, nh, NULL);
1176		cond_resched();
1177	}
1178}
1179
1180static struct nexthop *nexthop_create_group(struct net *net,
1181					    struct nh_config *cfg)
1182{
1183	struct nlattr *grps_attr = cfg->nh_grp;
1184	struct nexthop_grp *entry = nla_data(grps_attr);
1185	u16 num_nh = nla_len(grps_attr) / sizeof(*entry);
1186	struct nh_group *nhg;
1187	struct nexthop *nh;
1188	int i;
1189
1190	if (WARN_ON(!num_nh))
1191		return ERR_PTR(-EINVAL);
1192
1193	nh = nexthop_alloc();
1194	if (!nh)
1195		return ERR_PTR(-ENOMEM);
1196
1197	nh->is_group = 1;
1198
1199	nhg = nexthop_grp_alloc(num_nh);
1200	if (!nhg) {
1201		kfree(nh);
1202		return ERR_PTR(-ENOMEM);
1203	}
1204
1205	/* spare group used for removals */
1206	nhg->spare = nexthop_grp_alloc(num_nh);
1207	if (!nhg->spare) {
1208		kfree(nhg);
1209		kfree(nh);
1210		return ERR_PTR(-ENOMEM);
1211	}
1212	nhg->spare->spare = nhg;
1213
1214	for (i = 0; i < nhg->num_nh; ++i) {
1215		struct nexthop *nhe;
1216		struct nh_info *nhi;
1217
1218		nhe = nexthop_find_by_id(net, entry[i].id);
1219		if (!nexthop_get(nhe))
1220			goto out_no_nh;
1221
1222		nhi = rtnl_dereference(nhe->nh_info);
1223		if (nhi->family == AF_INET)
1224			nhg->has_v4 = true;
1225
1226		nhg->nh_entries[i].nh = nhe;
1227		nhg->nh_entries[i].weight = entry[i].weight + 1;
1228		list_add(&nhg->nh_entries[i].nh_list, &nhe->grp_list);
1229		nhg->nh_entries[i].nh_parent = nh;
1230	}
1231
1232	if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_MPATH) {
1233		nhg->mpath = 1;
1234		nh_group_rebalance(nhg);
1235	}
1236
1237	if (cfg->nh_fdb)
1238		nhg->fdb_nh = 1;
1239
1240	rcu_assign_pointer(nh->nh_grp, nhg);
1241
1242	return nh;
1243
1244out_no_nh:
1245	for (; i >= 0; --i)
1246		nexthop_put(nhg->nh_entries[i].nh);
1247
1248	kfree(nhg->spare);
1249	kfree(nhg);
1250	kfree(nh);
1251
1252	return ERR_PTR(-ENOENT);
1253}
1254
1255static int nh_create_ipv4(struct net *net, struct nexthop *nh,
1256			  struct nh_info *nhi, struct nh_config *cfg,
1257			  struct netlink_ext_ack *extack)
1258{
1259	struct fib_nh *fib_nh = &nhi->fib_nh;
1260	struct fib_config fib_cfg = {
1261		.fc_oif   = cfg->nh_ifindex,
1262		.fc_gw4   = cfg->gw.ipv4,
1263		.fc_gw_family = cfg->gw.ipv4 ? AF_INET : 0,
1264		.fc_flags = cfg->nh_flags,
1265		.fc_encap = cfg->nh_encap,
1266		.fc_encap_type = cfg->nh_encap_type,
1267	};
1268	u32 tb_id = (cfg->dev ? l3mdev_fib_table(cfg->dev) : RT_TABLE_MAIN);
1269	int err;
1270
1271	err = fib_nh_init(net, fib_nh, &fib_cfg, 1, extack);
1272	if (err) {
1273		fib_nh_release(net, fib_nh);
1274		goto out;
1275	}
1276
1277	if (nhi->fdb_nh)
1278		goto out;
1279
1280	/* sets nh_dev if successful */
1281	err = fib_check_nh(net, fib_nh, tb_id, 0, extack);
1282	if (!err) {
1283		nh->nh_flags = fib_nh->fib_nh_flags;
1284		fib_info_update_nhc_saddr(net, &fib_nh->nh_common,
1285					  fib_nh->fib_nh_scope);
1286	} else {
1287		fib_nh_release(net, fib_nh);
1288	}
1289out:
1290	return err;
1291}
1292
1293static int nh_create_ipv6(struct net *net,  struct nexthop *nh,
1294			  struct nh_info *nhi, struct nh_config *cfg,
1295			  struct netlink_ext_ack *extack)
1296{
1297	struct fib6_nh *fib6_nh = &nhi->fib6_nh;
1298	struct fib6_config fib6_cfg = {
1299		.fc_table = l3mdev_fib_table(cfg->dev),
1300		.fc_ifindex = cfg->nh_ifindex,
1301		.fc_gateway = cfg->gw.ipv6,
1302		.fc_flags = cfg->nh_flags,
1303		.fc_encap = cfg->nh_encap,
1304		.fc_encap_type = cfg->nh_encap_type,
1305		.fc_is_fdb = cfg->nh_fdb,
1306	};
1307	int err;
1308
1309	if (!ipv6_addr_any(&cfg->gw.ipv6))
1310		fib6_cfg.fc_flags |= RTF_GATEWAY;
1311
1312	/* sets nh_dev if successful */
1313	err = ipv6_stub->fib6_nh_init(net, fib6_nh, &fib6_cfg, GFP_KERNEL,
1314				      extack);
1315	if (err)
1316		ipv6_stub->fib6_nh_release(fib6_nh);
1317	else
1318		nh->nh_flags = fib6_nh->fib_nh_flags;
1319
1320	return err;
1321}
1322
1323static struct nexthop *nexthop_create(struct net *net, struct nh_config *cfg,
1324				      struct netlink_ext_ack *extack)
1325{
1326	struct nh_info *nhi;
1327	struct nexthop *nh;
1328	int err = 0;
1329
1330	nh = nexthop_alloc();
1331	if (!nh)
1332		return ERR_PTR(-ENOMEM);
1333
1334	nhi = kzalloc(sizeof(*nhi), GFP_KERNEL);
1335	if (!nhi) {
1336		kfree(nh);
1337		return ERR_PTR(-ENOMEM);
1338	}
1339
1340	nh->nh_flags = cfg->nh_flags;
1341	nh->net = net;
1342
1343	nhi->nh_parent = nh;
1344	nhi->family = cfg->nh_family;
1345	nhi->fib_nhc.nhc_scope = RT_SCOPE_LINK;
1346
1347	if (cfg->nh_fdb)
1348		nhi->fdb_nh = 1;
1349
1350	if (cfg->nh_blackhole) {
1351		nhi->reject_nh = 1;
1352		cfg->nh_ifindex = net->loopback_dev->ifindex;
1353	}
1354
1355	switch (cfg->nh_family) {
1356	case AF_INET:
1357		err = nh_create_ipv4(net, nh, nhi, cfg, extack);
1358		break;
1359	case AF_INET6:
1360		err = nh_create_ipv6(net, nh, nhi, cfg, extack);
1361		break;
1362	}
1363
1364	if (err) {
1365		kfree(nhi);
1366		kfree(nh);
1367		return ERR_PTR(err);
1368	}
1369
1370	/* add the entry to the device based hash */
1371	if (!nhi->fdb_nh)
1372		nexthop_devhash_add(net, nhi);
1373
1374	rcu_assign_pointer(nh->nh_info, nhi);
1375
1376	return nh;
1377}
1378
1379/* called with rtnl lock held */
1380static struct nexthop *nexthop_add(struct net *net, struct nh_config *cfg,
1381				   struct netlink_ext_ack *extack)
1382{
1383	struct nexthop *nh;
1384	int err;
1385
1386	if (cfg->nlflags & NLM_F_REPLACE && !cfg->nh_id) {
1387		NL_SET_ERR_MSG(extack, "Replace requires nexthop id");
1388		return ERR_PTR(-EINVAL);
1389	}
1390
1391	if (!cfg->nh_id) {
1392		cfg->nh_id = nh_find_unused_id(net);
1393		if (!cfg->nh_id) {
1394			NL_SET_ERR_MSG(extack, "No unused id");
1395			return ERR_PTR(-EINVAL);
1396		}
1397	}
1398
1399	if (cfg->nh_grp)
1400		nh = nexthop_create_group(net, cfg);
1401	else
1402		nh = nexthop_create(net, cfg, extack);
1403
1404	if (IS_ERR(nh))
1405		return nh;
1406
1407	refcount_set(&nh->refcnt, 1);
1408	nh->id = cfg->nh_id;
1409	nh->protocol = cfg->nh_protocol;
1410	nh->net = net;
1411
1412	err = insert_nexthop(net, nh, cfg, extack);
1413	if (err) {
1414		__remove_nexthop(net, nh, NULL);
1415		nexthop_put(nh);
1416		nh = ERR_PTR(err);
1417	}
1418
1419	return nh;
1420}
1421
1422static int rtm_to_nh_config(struct net *net, struct sk_buff *skb,
1423			    struct nlmsghdr *nlh, struct nh_config *cfg,
1424			    struct netlink_ext_ack *extack)
1425{
1426	struct nhmsg *nhm = nlmsg_data(nlh);
1427	struct nlattr *tb[NHA_MAX + 1];
1428	int err;
1429
1430	err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy,
1431			  extack);
1432	if (err < 0)
1433		return err;
1434
1435	err = -EINVAL;
1436	if (nhm->resvd || nhm->nh_scope) {
1437		NL_SET_ERR_MSG(extack, "Invalid values in ancillary header");
1438		goto out;
1439	}
1440	if (nhm->nh_flags & ~NEXTHOP_VALID_USER_FLAGS) {
1441		NL_SET_ERR_MSG(extack, "Invalid nexthop flags in ancillary header");
1442		goto out;
1443	}
1444
1445	switch (nhm->nh_family) {
1446	case AF_INET:
1447	case AF_INET6:
1448		break;
1449	case AF_UNSPEC:
1450		if (tb[NHA_GROUP])
1451			break;
1452		fallthrough;
1453	default:
1454		NL_SET_ERR_MSG(extack, "Invalid address family");
1455		goto out;
1456	}
1457
1458	if (tb[NHA_GROUPS] || tb[NHA_MASTER]) {
1459		NL_SET_ERR_MSG(extack, "Invalid attributes in request");
1460		goto out;
1461	}
1462
1463	memset(cfg, 0, sizeof(*cfg));
1464	cfg->nlflags = nlh->nlmsg_flags;
1465	cfg->nlinfo.portid = NETLINK_CB(skb).portid;
1466	cfg->nlinfo.nlh = nlh;
1467	cfg->nlinfo.nl_net = net;
1468
1469	cfg->nh_family = nhm->nh_family;
1470	cfg->nh_protocol = nhm->nh_protocol;
1471	cfg->nh_flags = nhm->nh_flags;
1472
1473	if (tb[NHA_ID])
1474		cfg->nh_id = nla_get_u32(tb[NHA_ID]);
1475
1476	if (tb[NHA_FDB]) {
1477		if (tb[NHA_OIF] || tb[NHA_BLACKHOLE] ||
1478		    tb[NHA_ENCAP]   || tb[NHA_ENCAP_TYPE]) {
1479			NL_SET_ERR_MSG(extack, "Fdb attribute can not be used with encap, oif or blackhole");
1480			goto out;
1481		}
1482		if (nhm->nh_flags) {
1483			NL_SET_ERR_MSG(extack, "Unsupported nexthop flags in ancillary header");
1484			goto out;
1485		}
1486		cfg->nh_fdb = nla_get_flag(tb[NHA_FDB]);
1487	}
1488
1489	if (tb[NHA_GROUP]) {
1490		if (nhm->nh_family != AF_UNSPEC) {
1491			NL_SET_ERR_MSG(extack, "Invalid family for group");
1492			goto out;
1493		}
1494		cfg->nh_grp = tb[NHA_GROUP];
1495
1496		cfg->nh_grp_type = NEXTHOP_GRP_TYPE_MPATH;
1497		if (tb[NHA_GROUP_TYPE])
1498			cfg->nh_grp_type = nla_get_u16(tb[NHA_GROUP_TYPE]);
1499
1500		if (cfg->nh_grp_type > NEXTHOP_GRP_TYPE_MAX) {
1501			NL_SET_ERR_MSG(extack, "Invalid group type");
1502			goto out;
1503		}
1504		err = nh_check_attr_group(net, tb, extack);
1505
1506		/* no other attributes should be set */
1507		goto out;
1508	}
1509
1510	if (tb[NHA_BLACKHOLE]) {
1511		if (tb[NHA_GATEWAY] || tb[NHA_OIF] ||
1512		    tb[NHA_ENCAP]   || tb[NHA_ENCAP_TYPE] || tb[NHA_FDB]) {
1513			NL_SET_ERR_MSG(extack, "Blackhole attribute can not be used with gateway, oif, encap or fdb");
1514			goto out;
1515		}
1516
1517		cfg->nh_blackhole = 1;
1518		err = 0;
1519		goto out;
1520	}
1521
1522	if (!cfg->nh_fdb && !tb[NHA_OIF]) {
1523		NL_SET_ERR_MSG(extack, "Device attribute required for non-blackhole and non-fdb nexthops");
1524		goto out;
1525	}
1526
1527	if (!cfg->nh_fdb && tb[NHA_OIF]) {
1528		cfg->nh_ifindex = nla_get_u32(tb[NHA_OIF]);
1529		if (cfg->nh_ifindex)
1530			cfg->dev = __dev_get_by_index(net, cfg->nh_ifindex);
1531
1532		if (!cfg->dev) {
1533			NL_SET_ERR_MSG(extack, "Invalid device index");
1534			goto out;
1535		} else if (!(cfg->dev->flags & IFF_UP)) {
1536			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
1537			err = -ENETDOWN;
1538			goto out;
1539		} else if (!netif_carrier_ok(cfg->dev)) {
1540			NL_SET_ERR_MSG(extack, "Carrier for nexthop device is down");
1541			err = -ENETDOWN;
1542			goto out;
1543		}
1544	}
1545
1546	err = -EINVAL;
1547	if (tb[NHA_GATEWAY]) {
1548		struct nlattr *gwa = tb[NHA_GATEWAY];
1549
1550		switch (cfg->nh_family) {
1551		case AF_INET:
1552			if (nla_len(gwa) != sizeof(u32)) {
1553				NL_SET_ERR_MSG(extack, "Invalid gateway");
1554				goto out;
1555			}
1556			cfg->gw.ipv4 = nla_get_be32(gwa);
1557			break;
1558		case AF_INET6:
1559			if (nla_len(gwa) != sizeof(struct in6_addr)) {
1560				NL_SET_ERR_MSG(extack, "Invalid gateway");
1561				goto out;
1562			}
1563			cfg->gw.ipv6 = nla_get_in6_addr(gwa);
1564			break;
1565		default:
1566			NL_SET_ERR_MSG(extack,
1567				       "Unknown address family for gateway");
1568			goto out;
1569		}
1570	} else {
1571		/* device only nexthop (no gateway) */
1572		if (cfg->nh_flags & RTNH_F_ONLINK) {
1573			NL_SET_ERR_MSG(extack,
1574				       "ONLINK flag can not be set for nexthop without a gateway");
1575			goto out;
1576		}
1577	}
1578
1579	if (tb[NHA_ENCAP]) {
1580		cfg->nh_encap = tb[NHA_ENCAP];
1581
1582		if (!tb[NHA_ENCAP_TYPE]) {
1583			NL_SET_ERR_MSG(extack, "LWT encapsulation type is missing");
1584			goto out;
1585		}
1586
1587		cfg->nh_encap_type = nla_get_u16(tb[NHA_ENCAP_TYPE]);
1588		err = lwtunnel_valid_encap_type(cfg->nh_encap_type, extack);
1589		if (err < 0)
1590			goto out;
1591
1592	} else if (tb[NHA_ENCAP_TYPE]) {
1593		NL_SET_ERR_MSG(extack, "LWT encapsulation attribute is missing");
1594		goto out;
1595	}
1596
1597
1598	err = 0;
1599out:
1600	return err;
1601}
1602
1603/* rtnl */
1604static int rtm_new_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
1605			   struct netlink_ext_ack *extack)
1606{
1607	struct net *net = sock_net(skb->sk);
1608	struct nh_config cfg;
1609	struct nexthop *nh;
1610	int err;
1611
1612	err = rtm_to_nh_config(net, skb, nlh, &cfg, extack);
1613	if (!err) {
1614		nh = nexthop_add(net, &cfg, extack);
1615		if (IS_ERR(nh))
1616			err = PTR_ERR(nh);
1617	}
1618
1619	return err;
1620}
1621
1622static int nh_valid_get_del_req(struct nlmsghdr *nlh, u32 *id,
1623				struct netlink_ext_ack *extack)
1624{
1625	struct nhmsg *nhm = nlmsg_data(nlh);
1626	struct nlattr *tb[NHA_MAX + 1];
1627	int err, i;
1628
1629	err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy,
1630			  extack);
1631	if (err < 0)
1632		return err;
1633
1634	err = -EINVAL;
1635	for (i = 0; i < __NHA_MAX; ++i) {
1636		if (!tb[i])
1637			continue;
1638
1639		switch (i) {
1640		case NHA_ID:
1641			break;
1642		default:
1643			NL_SET_ERR_MSG_ATTR(extack, tb[i],
1644					    "Unexpected attribute in request");
1645			goto out;
1646		}
1647	}
1648	if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) {
1649		NL_SET_ERR_MSG(extack, "Invalid values in header");
1650		goto out;
1651	}
1652
1653	if (!tb[NHA_ID]) {
1654		NL_SET_ERR_MSG(extack, "Nexthop id is missing");
1655		goto out;
1656	}
1657
1658	*id = nla_get_u32(tb[NHA_ID]);
1659	if (!(*id))
1660		NL_SET_ERR_MSG(extack, "Invalid nexthop id");
1661	else
1662		err = 0;
1663out:
1664	return err;
1665}
1666
1667/* rtnl */
1668static int rtm_del_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
1669			   struct netlink_ext_ack *extack)
1670{
1671	struct net *net = sock_net(skb->sk);
1672	struct nl_info nlinfo = {
1673		.nlh = nlh,
1674		.nl_net = net,
1675		.portid = NETLINK_CB(skb).portid,
1676	};
1677	struct nexthop *nh;
1678	int err;
1679	u32 id;
1680
1681	err = nh_valid_get_del_req(nlh, &id, extack);
1682	if (err)
1683		return err;
1684
1685	nh = nexthop_find_by_id(net, id);
1686	if (!nh)
1687		return -ENOENT;
1688
1689	remove_nexthop(net, nh, &nlinfo);
1690
1691	return 0;
1692}
1693
1694/* rtnl */
1695static int rtm_get_nexthop(struct sk_buff *in_skb, struct nlmsghdr *nlh,
1696			   struct netlink_ext_ack *extack)
1697{
1698	struct net *net = sock_net(in_skb->sk);
1699	struct sk_buff *skb = NULL;
1700	struct nexthop *nh;
1701	int err;
1702	u32 id;
1703
1704	err = nh_valid_get_del_req(nlh, &id, extack);
1705	if (err)
1706		return err;
1707
1708	err = -ENOBUFS;
1709	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1710	if (!skb)
1711		goto out;
1712
1713	err = -ENOENT;
1714	nh = nexthop_find_by_id(net, id);
1715	if (!nh)
1716		goto errout_free;
1717
1718	err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, NETLINK_CB(in_skb).portid,
1719			   nlh->nlmsg_seq, 0);
1720	if (err < 0) {
1721		WARN_ON(err == -EMSGSIZE);
1722		goto errout_free;
1723	}
1724
1725	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
1726out:
1727	return err;
1728errout_free:
1729	kfree_skb(skb);
1730	goto out;
1731}
1732
1733static bool nh_dump_filtered(struct nexthop *nh, int dev_idx, int master_idx,
1734			     bool group_filter, u8 family)
1735{
1736	const struct net_device *dev;
1737	const struct nh_info *nhi;
1738
1739	if (group_filter && !nh->is_group)
1740		return true;
1741
1742	if (!dev_idx && !master_idx && !family)
1743		return false;
1744
1745	if (nh->is_group)
1746		return true;
1747
1748	nhi = rtnl_dereference(nh->nh_info);
1749	if (family && nhi->family != family)
1750		return true;
1751
1752	dev = nhi->fib_nhc.nhc_dev;
1753	if (dev_idx && (!dev || dev->ifindex != dev_idx))
1754		return true;
1755
1756	if (master_idx) {
1757		struct net_device *master;
1758
1759		if (!dev)
1760			return true;
1761
1762		master = netdev_master_upper_dev_get((struct net_device *)dev);
1763		if (!master || master->ifindex != master_idx)
1764			return true;
1765	}
1766
1767	return false;
1768}
1769
1770static int nh_valid_dump_req(const struct nlmsghdr *nlh, int *dev_idx,
1771			     int *master_idx, bool *group_filter,
1772			     bool *fdb_filter, struct netlink_callback *cb)
1773{
1774	struct netlink_ext_ack *extack = cb->extack;
1775	struct nlattr *tb[NHA_MAX + 1];
1776	struct nhmsg *nhm;
1777	int err, i;
1778	u32 idx;
1779
1780	err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy,
1781			  NULL);
1782	if (err < 0)
1783		return err;
1784
1785	for (i = 0; i <= NHA_MAX; ++i) {
1786		if (!tb[i])
1787			continue;
1788
1789		switch (i) {
1790		case NHA_OIF:
1791			idx = nla_get_u32(tb[i]);
1792			if (idx > INT_MAX) {
1793				NL_SET_ERR_MSG(extack, "Invalid device index");
1794				return -EINVAL;
1795			}
1796			*dev_idx = idx;
1797			break;
1798		case NHA_MASTER:
1799			idx = nla_get_u32(tb[i]);
1800			if (idx > INT_MAX) {
1801				NL_SET_ERR_MSG(extack, "Invalid master device index");
1802				return -EINVAL;
1803			}
1804			*master_idx = idx;
1805			break;
1806		case NHA_GROUPS:
1807			*group_filter = true;
1808			break;
1809		case NHA_FDB:
1810			*fdb_filter = true;
1811			break;
1812		default:
1813			NL_SET_ERR_MSG(extack, "Unsupported attribute in dump request");
1814			return -EINVAL;
1815		}
1816	}
1817
1818	nhm = nlmsg_data(nlh);
1819	if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) {
1820		NL_SET_ERR_MSG(extack, "Invalid values in header for nexthop dump request");
1821		return -EINVAL;
1822	}
1823
1824	return 0;
1825}
1826
1827/* rtnl */
1828static int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb)
1829{
1830	bool group_filter = false, fdb_filter = false;
1831	struct nhmsg *nhm = nlmsg_data(cb->nlh);
1832	int dev_filter_idx = 0, master_idx = 0;
1833	struct net *net = sock_net(skb->sk);
1834	struct rb_root *root = &net->nexthop.rb_root;
1835	struct rb_node *node;
1836	int idx = 0, s_idx;
1837	int err;
1838
1839	err = nh_valid_dump_req(cb->nlh, &dev_filter_idx, &master_idx,
1840				&group_filter, &fdb_filter, cb);
1841	if (err < 0)
1842		return err;
1843
1844	s_idx = cb->args[0];
1845	for (node = rb_first(root); node; node = rb_next(node)) {
1846		struct nexthop *nh;
1847
1848		if (idx < s_idx)
1849			goto cont;
1850
1851		nh = rb_entry(node, struct nexthop, rb_node);
1852		if (nh_dump_filtered(nh, dev_filter_idx, master_idx,
1853				     group_filter, nhm->nh_family))
1854			goto cont;
1855
1856		err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP,
1857				   NETLINK_CB(cb->skb).portid,
1858				   cb->nlh->nlmsg_seq, NLM_F_MULTI);
1859		if (err < 0) {
1860			if (likely(skb->len))
1861				goto out;
1862
1863			goto out_err;
1864		}
1865cont:
1866		idx++;
1867	}
1868
1869out:
1870	err = skb->len;
1871out_err:
1872	cb->args[0] = idx;
1873	cb->seq = net->nexthop.seq;
1874	nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1875
1876	return err;
1877}
1878
1879static void nexthop_sync_mtu(struct net_device *dev, u32 orig_mtu)
1880{
1881	unsigned int hash = nh_dev_hashfn(dev->ifindex);
1882	struct net *net = dev_net(dev);
1883	struct hlist_head *head = &net->nexthop.devhash[hash];
1884	struct hlist_node *n;
1885	struct nh_info *nhi;
1886
1887	hlist_for_each_entry_safe(nhi, n, head, dev_hash) {
1888		if (nhi->fib_nhc.nhc_dev == dev) {
1889			if (nhi->family == AF_INET)
1890				fib_nhc_update_mtu(&nhi->fib_nhc, dev->mtu,
1891						   orig_mtu);
1892		}
1893	}
1894}
1895
1896/* rtnl */
1897static int nh_netdev_event(struct notifier_block *this,
1898			   unsigned long event, void *ptr)
1899{
1900	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1901	struct netdev_notifier_info_ext *info_ext;
1902
1903	switch (event) {
1904	case NETDEV_DOWN:
1905	case NETDEV_UNREGISTER:
1906		nexthop_flush_dev(dev);
1907		break;
1908	case NETDEV_CHANGE:
1909		if (!(dev_get_flags(dev) & (IFF_RUNNING | IFF_LOWER_UP)))
1910			nexthop_flush_dev(dev);
1911		break;
1912	case NETDEV_CHANGEMTU:
1913		info_ext = ptr;
1914		nexthop_sync_mtu(dev, info_ext->ext.mtu);
1915		rt_cache_flush(dev_net(dev));
1916		break;
1917	}
1918	return NOTIFY_DONE;
1919}
1920
1921static struct notifier_block nh_netdev_notifier = {
1922	.notifier_call = nh_netdev_event,
1923};
1924
1925int register_nexthop_notifier(struct net *net, struct notifier_block *nb)
1926{
1927	return atomic_notifier_chain_register(&net->nexthop.notifier_chain, nb);
1928}
1929EXPORT_SYMBOL(register_nexthop_notifier);
1930
1931int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb)
1932{
1933	return atomic_notifier_chain_unregister(&net->nexthop.notifier_chain,
1934						nb);
1935}
1936EXPORT_SYMBOL(unregister_nexthop_notifier);
1937
1938static void __net_exit nexthop_net_exit(struct net *net)
1939{
1940	rtnl_lock();
1941	flush_all_nexthops(net);
1942	rtnl_unlock();
1943	kfree(net->nexthop.devhash);
1944}
1945
1946static int __net_init nexthop_net_init(struct net *net)
1947{
1948	size_t sz = sizeof(struct hlist_head) * NH_DEV_HASHSIZE;
1949
1950	net->nexthop.rb_root = RB_ROOT;
1951	net->nexthop.devhash = kzalloc(sz, GFP_KERNEL);
1952	if (!net->nexthop.devhash)
1953		return -ENOMEM;
1954	ATOMIC_INIT_NOTIFIER_HEAD(&net->nexthop.notifier_chain);
1955
1956	return 0;
1957}
1958
1959static struct pernet_operations nexthop_net_ops = {
1960	.init = nexthop_net_init,
1961	.exit = nexthop_net_exit,
1962};
1963
1964static int __init nexthop_init(void)
1965{
1966	register_pernet_subsys(&nexthop_net_ops);
1967
1968	register_netdevice_notifier(&nh_netdev_notifier);
1969
1970	rtnl_register(PF_UNSPEC, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
1971	rtnl_register(PF_UNSPEC, RTM_DELNEXTHOP, rtm_del_nexthop, NULL, 0);
1972	rtnl_register(PF_UNSPEC, RTM_GETNEXTHOP, rtm_get_nexthop,
1973		      rtm_dump_nexthop, 0);
1974
1975	rtnl_register(PF_INET, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
1976	rtnl_register(PF_INET, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0);
1977
1978	rtnl_register(PF_INET6, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
1979	rtnl_register(PF_INET6, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0);
1980
1981	return 0;
1982}
1983subsys_initcall(nexthop_init);