File Index
Files and directories
Sign in
FILE

fib_semantics.c

File preview with syntax highlighting.

linux-6.19.14/net/ipv4/fib_semantics.c C
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * INET		An implementation of the TCP/IP protocol suite for the LINUX
   4 *		operating system.  INET is implemented using the  BSD Socket
   5 *		interface as the means of communication with the user level.
   6 *
   7 *		IPv4 Forwarding Information Base: semantics.
   8 *
   9 * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10 */
  11
  12#include <linux/uaccess.h>
  13#include <linux/bitops.h>
  14#include <linux/types.h>
  15#include <linux/kernel.h>
  16#include <linux/jiffies.h>
  17#include <linux/mm.h>
  18#include <linux/string.h>
  19#include <linux/socket.h>
  20#include <linux/sockios.h>
  21#include <linux/errno.h>
  22#include <linux/in.h>
  23#include <linux/inet.h>
  24#include <linux/inetdevice.h>
  25#include <linux/netdevice.h>
  26#include <linux/if_arp.h>
  27#include <linux/proc_fs.h>
  28#include <linux/skbuff.h>
  29#include <linux/init.h>
  30#include <linux/slab.h>
  31#include <linux/netlink.h>
  32#include <linux/hash.h>
  33#include <linux/nospec.h>
  34
  35#include <net/arp.h>
  36#include <net/inet_dscp.h>
  37#include <net/ip.h>
  38#include <net/protocol.h>
  39#include <net/route.h>
  40#include <net/tcp.h>
  41#include <net/sock.h>
  42#include <net/ip_fib.h>
  43#include <net/ip6_fib.h>
  44#include <net/nexthop.h>
  45#include <net/netlink.h>
  46#include <net/rtnh.h>
  47#include <net/lwtunnel.h>
  48#include <net/fib_notifier.h>
  49#include <net/addrconf.h>
  50
  51#include "fib_lookup.h"
  52
  53/* for_nexthops and change_nexthops only used when nexthop object
  54 * is not set in a fib_info. The logic within can reference fib_nh.
  55 */
  56#ifdef CONFIG_IP_ROUTE_MULTIPATH
  57
  58#define for_nexthops(fi) {						\
  59	int nhsel; const struct fib_nh *nh;				\
  60	for (nhsel = 0, nh = (fi)->fib_nh;				\
  61	     nhsel < fib_info_num_path((fi));				\
  62	     nh++, nhsel++)
  63
  64#define change_nexthops(fi) {						\
  65	int nhsel; struct fib_nh *nexthop_nh;				\
  66	for (nhsel = 0,	nexthop_nh = (struct fib_nh *)((fi)->fib_nh);	\
  67	     nhsel < fib_info_num_path((fi));				\
  68	     nexthop_nh++, nhsel++)
  69
  70#else /* CONFIG_IP_ROUTE_MULTIPATH */
  71
  72/* Hope, that gcc will optimize it to get rid of dummy loop */
  73
  74#define for_nexthops(fi) {						\
  75	int nhsel; const struct fib_nh *nh = (fi)->fib_nh;		\
  76	for (nhsel = 0; nhsel < 1; nhsel++)
  77
  78#define change_nexthops(fi) {						\
  79	int nhsel;							\
  80	struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh);	\
  81	for (nhsel = 0; nhsel < 1; nhsel++)
  82
  83#endif /* CONFIG_IP_ROUTE_MULTIPATH */
  84
  85#define endfor_nexthops(fi) }
  86
  87
  88const struct fib_prop fib_props[RTN_MAX + 1] = {
  89	[RTN_UNSPEC] = {
  90		.error	= 0,
  91		.scope	= RT_SCOPE_NOWHERE,
  92	},
  93	[RTN_UNICAST] = {
  94		.error	= 0,
  95		.scope	= RT_SCOPE_UNIVERSE,
  96	},
  97	[RTN_LOCAL] = {
  98		.error	= 0,
  99		.scope	= RT_SCOPE_HOST,
 100	},
 101	[RTN_BROADCAST] = {
 102		.error	= 0,
 103		.scope	= RT_SCOPE_LINK,
 104	},
 105	[RTN_ANYCAST] = {
 106		.error	= 0,
 107		.scope	= RT_SCOPE_LINK,
 108	},
 109	[RTN_MULTICAST] = {
 110		.error	= 0,
 111		.scope	= RT_SCOPE_UNIVERSE,
 112	},
 113	[RTN_BLACKHOLE] = {
 114		.error	= -EINVAL,
 115		.scope	= RT_SCOPE_UNIVERSE,
 116	},
 117	[RTN_UNREACHABLE] = {
 118		.error	= -EHOSTUNREACH,
 119		.scope	= RT_SCOPE_UNIVERSE,
 120	},
 121	[RTN_PROHIBIT] = {
 122		.error	= -EACCES,
 123		.scope	= RT_SCOPE_UNIVERSE,
 124	},
 125	[RTN_THROW] = {
 126		.error	= -EAGAIN,
 127		.scope	= RT_SCOPE_UNIVERSE,
 128	},
 129	[RTN_NAT] = {
 130		.error	= -EINVAL,
 131		.scope	= RT_SCOPE_NOWHERE,
 132	},
 133	[RTN_XRESOLVE] = {
 134		.error	= -EINVAL,
 135		.scope	= RT_SCOPE_NOWHERE,
 136	},
 137};
 138
 139static void rt_fibinfo_free(struct rtable __rcu **rtp)
 140{
 141	struct rtable *rt = rcu_dereference_protected(*rtp, 1);
 142
 143	if (!rt)
 144		return;
 145
 146	/* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
 147	 * because we waited an RCU grace period before calling
 148	 * free_fib_info_rcu()
 149	 */
 150
 151	dst_dev_put(&rt->dst);
 152	dst_release_immediate(&rt->dst);
 153}
 154
 155static void free_nh_exceptions(struct fib_nh_common *nhc)
 156{
 157	struct fnhe_hash_bucket *hash;
 158	int i;
 159
 160	hash = rcu_dereference_protected(nhc->nhc_exceptions, 1);
 161	if (!hash)
 162		return;
 163	for (i = 0; i < FNHE_HASH_SIZE; i++) {
 164		struct fib_nh_exception *fnhe;
 165
 166		fnhe = rcu_dereference_protected(hash[i].chain, 1);
 167		while (fnhe) {
 168			struct fib_nh_exception *next;
 169
 170			next = rcu_dereference_protected(fnhe->fnhe_next, 1);
 171
 172			rt_fibinfo_free(&fnhe->fnhe_rth_input);
 173			rt_fibinfo_free(&fnhe->fnhe_rth_output);
 174
 175			kfree(fnhe);
 176
 177			fnhe = next;
 178		}
 179	}
 180	kfree(hash);
 181}
 182
 183static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
 184{
 185	int cpu;
 186
 187	if (!rtp)
 188		return;
 189
 190	for_each_possible_cpu(cpu) {
 191		struct rtable *rt;
 192
 193		rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
 194		if (rt) {
 195			dst_dev_put(&rt->dst);
 196			dst_release_immediate(&rt->dst);
 197		}
 198	}
 199	free_percpu(rtp);
 200}
 201
 202void fib_nh_common_release(struct fib_nh_common *nhc)
 203{
 204	netdev_put(nhc->nhc_dev, &nhc->nhc_dev_tracker);
 205	lwtstate_put(nhc->nhc_lwtstate);
 206	rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
 207	rt_fibinfo_free(&nhc->nhc_rth_input);
 208	free_nh_exceptions(nhc);
 209}
 210EXPORT_SYMBOL_GPL(fib_nh_common_release);
 211
 212void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
 213{
 214#ifdef CONFIG_IP_ROUTE_CLASSID
 215	if (fib_nh->nh_tclassid)
 216		atomic_dec(&net->ipv4.fib_num_tclassid_users);
 217#endif
 218	fib_nh_common_release(&fib_nh->nh_common);
 219}
 220
 221/* Release a nexthop info record */
 222static void free_fib_info_rcu(struct rcu_head *head)
 223{
 224	struct fib_info *fi = container_of(head, struct fib_info, rcu);
 225
 226	if (fi->nh) {
 227		nexthop_put(fi->nh);
 228	} else {
 229		change_nexthops(fi) {
 230			fib_nh_release(fi->fib_net, nexthop_nh);
 231		} endfor_nexthops(fi);
 232	}
 233
 234	ip_fib_metrics_put(fi->fib_metrics);
 235
 236	kfree(fi);
 237}
 238
 239void free_fib_info(struct fib_info *fi)
 240{
 241	if (fi->fib_dead == 0) {
 242		pr_warn("Freeing alive fib_info %p\n", fi);
 243		return;
 244	}
 245
 246	call_rcu_hurry(&fi->rcu, free_fib_info_rcu);
 247}
 248EXPORT_SYMBOL_GPL(free_fib_info);
 249
 250void fib_release_info(struct fib_info *fi)
 251{
 252	ASSERT_RTNL();
 253	if (fi && refcount_dec_and_test(&fi->fib_treeref)) {
 254		hlist_del(&fi->fib_hash);
 255		fi->fib_net->ipv4.fib_info_cnt--;
 256
 257		if (fi->fib_prefsrc)
 258			hlist_del(&fi->fib_lhash);
 259		if (fi->nh) {
 260			list_del(&fi->nh_list);
 261		} else {
 262			change_nexthops(fi) {
 263				if (!nexthop_nh->fib_nh_dev)
 264					continue;
 265				hlist_del_rcu(&nexthop_nh->nh_hash);
 266			} endfor_nexthops(fi)
 267		}
 268		/* Paired with READ_ONCE() from fib_table_lookup() */
 269		WRITE_ONCE(fi->fib_dead, 1);
 270		fib_info_put(fi);
 271	}
 272}
 273
 274static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi)
 275{
 276	const struct fib_nh *onh;
 277
 278	if (fi->nh || ofi->nh)
 279		return nexthop_cmp(fi->nh, ofi->nh) ? 0 : -1;
 280
 281	if (ofi->fib_nhs == 0)
 282		return 0;
 283
 284	for_nexthops(fi) {
 285		onh = fib_info_nh(ofi, nhsel);
 286
 287		if (nh->fib_nh_oif != onh->fib_nh_oif ||
 288		    nh->fib_nh_gw_family != onh->fib_nh_gw_family ||
 289		    nh->fib_nh_scope != onh->fib_nh_scope ||
 290#ifdef CONFIG_IP_ROUTE_MULTIPATH
 291		    nh->fib_nh_weight != onh->fib_nh_weight ||
 292#endif
 293#ifdef CONFIG_IP_ROUTE_CLASSID
 294		    nh->nh_tclassid != onh->nh_tclassid ||
 295#endif
 296		    lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
 297		    ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
 298			return -1;
 299
 300		if (nh->fib_nh_gw_family == AF_INET &&
 301		    nh->fib_nh_gw4 != onh->fib_nh_gw4)
 302			return -1;
 303
 304		if (nh->fib_nh_gw_family == AF_INET6 &&
 305		    ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6))
 306			return -1;
 307	} endfor_nexthops(fi);
 308	return 0;
 309}
 310
 311static struct hlist_head *fib_nh_head(struct net_device *dev)
 312{
 313	return &dev->fib_nh_head;
 314}
 315
 316static unsigned int fib_info_hashfn_1(int init_val, u8 protocol, u8 scope,
 317				      u32 prefsrc, u32 priority)
 318{
 319	unsigned int val = init_val;
 320
 321	val ^= (protocol << 8) | scope;
 322	val ^= prefsrc;
 323	val ^= priority;
 324
 325	return val;
 326}
 327
 328static unsigned int fib_info_hashfn_result(const struct net *net,
 329					   unsigned int val)
 330{
 331	return hash_32(val ^ net_hash_mix(net), net->ipv4.fib_info_hash_bits);
 332}
 333
 334static struct hlist_head *fib_info_hash_bucket(struct fib_info *fi)
 335{
 336	struct net *net = fi->fib_net;
 337	unsigned int val;
 338
 339	val = fib_info_hashfn_1(fi->fib_nhs, fi->fib_protocol,
 340				fi->fib_scope, (__force u32)fi->fib_prefsrc,
 341				fi->fib_priority);
 342
 343	if (fi->nh) {
 344		val ^= fi->nh->id;
 345	} else {
 346		for_nexthops(fi) {
 347			val ^= nh->fib_nh_oif;
 348		} endfor_nexthops(fi)
 349	}
 350
 351	return &net->ipv4.fib_info_hash[fib_info_hashfn_result(net, val)];
 352}
 353
 354static struct hlist_head *fib_info_laddrhash_bucket(const struct net *net,
 355						    __be32 val)
 356{
 357	unsigned int hash_bits = net->ipv4.fib_info_hash_bits;
 358	u32 slot;
 359
 360	slot = hash_32(net_hash_mix(net) ^ (__force u32)val, hash_bits);
 361
 362	return &net->ipv4.fib_info_hash[(1 << hash_bits) + slot];
 363}
 364
 365static struct hlist_head *fib_info_hash_alloc(unsigned int hash_bits)
 366{
 367	/* The second half is used for prefsrc */
 368	return kvcalloc((1 << hash_bits) * 2, sizeof(struct hlist_head),
 369			GFP_KERNEL);
 370}
 371
 372static void fib_info_hash_free(struct hlist_head *head)
 373{
 374	kvfree(head);
 375}
 376
 377static void fib_info_hash_grow(struct net *net)
 378{
 379	unsigned int old_size = 1 << net->ipv4.fib_info_hash_bits;
 380	struct hlist_head *new_info_hash, *old_info_hash;
 381	unsigned int i;
 382
 383	if (net->ipv4.fib_info_cnt < old_size)
 384		return;
 385
 386	new_info_hash = fib_info_hash_alloc(net->ipv4.fib_info_hash_bits + 1);
 387	if (!new_info_hash)
 388		return;
 389
 390	old_info_hash = net->ipv4.fib_info_hash;
 391	net->ipv4.fib_info_hash = new_info_hash;
 392	net->ipv4.fib_info_hash_bits += 1;
 393
 394	for (i = 0; i < old_size; i++) {
 395		struct hlist_head *head = &old_info_hash[i];
 396		struct hlist_node *n;
 397		struct fib_info *fi;
 398
 399		hlist_for_each_entry_safe(fi, n, head, fib_hash)
 400			hlist_add_head(&fi->fib_hash, fib_info_hash_bucket(fi));
 401	}
 402
 403	for (i = 0; i < old_size; i++) {
 404		struct hlist_head *lhead = &old_info_hash[old_size + i];
 405		struct hlist_node *n;
 406		struct fib_info *fi;
 407
 408		hlist_for_each_entry_safe(fi, n, lhead, fib_lhash)
 409			hlist_add_head(&fi->fib_lhash,
 410				       fib_info_laddrhash_bucket(fi->fib_net,
 411								 fi->fib_prefsrc));
 412	}
 413
 414	fib_info_hash_free(old_info_hash);
 415}
 416
 417/* no metrics, only nexthop id */
 418static struct fib_info *fib_find_info_nh(struct net *net,
 419					 const struct fib_config *cfg)
 420{
 421	struct hlist_head *head;
 422	struct fib_info *fi;
 423	unsigned int hash;
 424
 425	hash = fib_info_hashfn_1(cfg->fc_nh_id,
 426				 cfg->fc_protocol, cfg->fc_scope,
 427				 (__force u32)cfg->fc_prefsrc,
 428				 cfg->fc_priority);
 429	hash = fib_info_hashfn_result(net, hash);
 430	head = &net->ipv4.fib_info_hash[hash];
 431
 432	hlist_for_each_entry(fi, head, fib_hash) {
 433		if (!fi->nh || fi->nh->id != cfg->fc_nh_id)
 434			continue;
 435
 436		if (cfg->fc_protocol == fi->fib_protocol &&
 437		    cfg->fc_scope == fi->fib_scope &&
 438		    cfg->fc_prefsrc == fi->fib_prefsrc &&
 439		    cfg->fc_priority == fi->fib_priority &&
 440		    cfg->fc_type == fi->fib_type &&
 441		    cfg->fc_table == fi->fib_tb_id &&
 442		    !((cfg->fc_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK))
 443			return fi;
 444	}
 445
 446	return NULL;
 447}
 448
 449static struct fib_info *fib_find_info(struct fib_info *nfi)
 450{
 451	struct hlist_head *head = fib_info_hash_bucket(nfi);
 452	struct fib_info *fi;
 453
 454	hlist_for_each_entry(fi, head, fib_hash) {
 455		if (fi->fib_nhs != nfi->fib_nhs)
 456			continue;
 457
 458		if (nfi->fib_protocol == fi->fib_protocol &&
 459		    nfi->fib_scope == fi->fib_scope &&
 460		    nfi->fib_prefsrc == fi->fib_prefsrc &&
 461		    nfi->fib_priority == fi->fib_priority &&
 462		    nfi->fib_type == fi->fib_type &&
 463		    nfi->fib_tb_id == fi->fib_tb_id &&
 464		    memcmp(nfi->fib_metrics, fi->fib_metrics,
 465			   sizeof(u32) * RTAX_MAX) == 0 &&
 466		    !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
 467		    nh_comp(fi, nfi) == 0)
 468			return fi;
 469	}
 470
 471	return NULL;
 472}
 473
 474/* Check, that the gateway is already configured.
 475 * Used only by redirect accept routine, under rcu_read_lock();
 476 */
 477int ip_fib_check_default(__be32 gw, struct net_device *dev)
 478{
 479	struct hlist_head *head;
 480	struct fib_nh *nh;
 481
 482	head = fib_nh_head(dev);
 483
 484	hlist_for_each_entry_rcu(nh, head, nh_hash) {
 485		DEBUG_NET_WARN_ON_ONCE(nh->fib_nh_dev != dev);
 486		if (nh->fib_nh_gw4 == gw &&
 487		    !(nh->fib_nh_flags & RTNH_F_DEAD)) {
 488			return 0;
 489		}
 490	}
 491
 492	return -1;
 493}
 494
 495size_t fib_nlmsg_size(struct fib_info *fi)
 496{
 497	size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
 498			 + nla_total_size(4) /* RTA_TABLE */
 499			 + nla_total_size(4) /* RTA_DST */
 500			 + nla_total_size(4) /* RTA_PRIORITY */
 501			 + nla_total_size(4) /* RTA_PREFSRC */
 502			 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
 503	unsigned int nhs = fib_info_num_path(fi);
 504
 505	/* space for nested metrics */
 506	payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
 507
 508	if (fi->nh)
 509		payload += nla_total_size(4); /* RTA_NH_ID */
 510
 511	if (nhs) {
 512		size_t nh_encapsize = 0;
 513		/* Also handles the special case nhs == 1 */
 514
 515		/* each nexthop is packed in an attribute */
 516		size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
 517		unsigned int i;
 518
 519		/* may contain flow and gateway attribute */
 520		nhsize += 2 * nla_total_size(4);
 521
 522		/* grab encap info */
 523		for (i = 0; i < fib_info_num_path(fi); i++) {
 524			struct fib_nh_common *nhc = fib_info_nhc(fi, i);
 525
 526			if (nhc->nhc_lwtstate) {
 527				/* RTA_ENCAP_TYPE */
 528				nh_encapsize += lwtunnel_get_encap_size(
 529						nhc->nhc_lwtstate);
 530				/* RTA_ENCAP */
 531				nh_encapsize +=  nla_total_size(2);
 532			}
 533		}
 534
 535		/* all nexthops are packed in a nested attribute */
 536		payload += nla_total_size((nhs * nhsize) + nh_encapsize);
 537
 538	}
 539
 540	return payload;
 541}
 542
 543void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
 544	       int dst_len, u32 tb_id, const struct nl_info *info,
 545	       unsigned int nlm_flags)
 546{
 547	struct fib_rt_info fri;
 548	struct sk_buff *skb;
 549	u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
 550	int err = -ENOBUFS;
 551
 552	skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
 553	if (!skb)
 554		goto errout;
 555
 556	fri.fi = fa->fa_info;
 557	fri.tb_id = tb_id;
 558	fri.dst = key;
 559	fri.dst_len = dst_len;
 560	fri.dscp = fa->fa_dscp;
 561	fri.type = fa->fa_type;
 562	fri.offload = READ_ONCE(fa->offload);
 563	fri.trap = READ_ONCE(fa->trap);
 564	fri.offload_failed = READ_ONCE(fa->offload_failed);
 565	err = fib_dump_info(skb, info->portid, seq, event, &fri, nlm_flags);
 566	if (err < 0) {
 567		/* -EMSGSIZE implies BUG in fib_nlmsg_size() */
 568		WARN_ON(err == -EMSGSIZE);
 569		kfree_skb(skb);
 570		goto errout;
 571	}
 572	rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
 573		    info->nlh, GFP_KERNEL);
 574	return;
 575errout:
 576	rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
 577}
 578
 579static int fib_detect_death(struct fib_info *fi, int order,
 580			    struct fib_info **last_resort, int *last_idx,
 581			    int dflt)
 582{
 583	const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
 584	struct neighbour *n;
 585	int state = NUD_NONE;
 586
 587	if (likely(nhc->nhc_gw_family == AF_INET))
 588		n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev);
 589	else if (nhc->nhc_gw_family == AF_INET6)
 590		n = neigh_lookup(ipv6_stub->nd_tbl, &nhc->nhc_gw.ipv6,
 591				 nhc->nhc_dev);
 592	else
 593		n = NULL;
 594
 595	if (n) {
 596		state = READ_ONCE(n->nud_state);
 597		neigh_release(n);
 598	} else {
 599		return 0;
 600	}
 601	if (state == NUD_REACHABLE)
 602		return 0;
 603	if ((state & NUD_VALID) && order != dflt)
 604		return 0;
 605	if ((state & NUD_VALID) ||
 606	    (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
 607		*last_resort = fi;
 608		*last_idx = order;
 609	}
 610	return 1;
 611}
 612
 613int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc,
 614		       struct nlattr *encap, u16 encap_type,
 615		       void *cfg, gfp_t gfp_flags,
 616		       struct netlink_ext_ack *extack)
 617{
 618	int err;
 619
 620	nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *,
 621						    gfp_flags);
 622	if (!nhc->nhc_pcpu_rth_output)
 623		return -ENOMEM;
 624
 625	if (encap) {
 626		struct lwtunnel_state *lwtstate;
 627
 628		err = lwtunnel_build_state(net, encap_type, encap,
 629					   nhc->nhc_family, cfg, &lwtstate,
 630					   extack);
 631		if (err)
 632			goto lwt_failure;
 633
 634		nhc->nhc_lwtstate = lwtstate_get(lwtstate);
 635	}
 636
 637	return 0;
 638
 639lwt_failure:
 640	rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
 641	nhc->nhc_pcpu_rth_output = NULL;
 642	return err;
 643}
 644EXPORT_SYMBOL_GPL(fib_nh_common_init);
 645
 646int fib_nh_init(struct net *net, struct fib_nh *nh,
 647		struct fib_config *cfg, int nh_weight,
 648		struct netlink_ext_ack *extack)
 649{
 650	int err;
 651
 652	nh->fib_nh_family = AF_INET;
 653
 654	err = fib_nh_common_init(net, &nh->nh_common, cfg->fc_encap,
 655				 cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
 656	if (err)
 657		return err;
 658
 659	nh->fib_nh_oif = cfg->fc_oif;
 660	nh->fib_nh_gw_family = cfg->fc_gw_family;
 661	if (cfg->fc_gw_family == AF_INET)
 662		nh->fib_nh_gw4 = cfg->fc_gw4;
 663	else if (cfg->fc_gw_family == AF_INET6)
 664		nh->fib_nh_gw6 = cfg->fc_gw6;
 665
 666	nh->fib_nh_flags = cfg->fc_flags;
 667
 668#ifdef CONFIG_IP_ROUTE_CLASSID
 669	nh->nh_tclassid = cfg->fc_flow;
 670	if (nh->nh_tclassid)
 671		atomic_inc(&net->ipv4.fib_num_tclassid_users);
 672#endif
 673#ifdef CONFIG_IP_ROUTE_MULTIPATH
 674	nh->fib_nh_weight = nh_weight;
 675#endif
 676	return 0;
 677}
 678
 679#ifdef CONFIG_IP_ROUTE_MULTIPATH
 680
 681static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
 682			      struct netlink_ext_ack *extack)
 683{
 684	int nhs = 0;
 685
 686	while (rtnh_ok(rtnh, remaining)) {
 687		nhs++;
 688		rtnh = rtnh_next(rtnh, &remaining);
 689	}
 690
 691	/* leftover implies invalid nexthop configuration, discard it */
 692	if (remaining > 0) {
 693		NL_SET_ERR_MSG(extack,
 694			       "Invalid nexthop configuration - extra data after nexthops");
 695		nhs = 0;
 696	}
 697
 698	return nhs;
 699}
 700
 701static int fib_gw_from_attr(__be32 *gw, struct nlattr *nla,
 702			    struct netlink_ext_ack *extack)
 703{
 704	if (nla_len(nla) < sizeof(*gw)) {
 705		NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_GATEWAY");
 706		return -EINVAL;
 707	}
 708
 709	*gw = nla_get_in_addr(nla);
 710
 711	return 0;
 712}
 713
 714/* only called when fib_nh is integrated into fib_info */
 715static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
 716		       int remaining, struct fib_config *cfg,
 717		       struct netlink_ext_ack *extack)
 718{
 719	struct net *net = fi->fib_net;
 720	struct fib_config fib_cfg;
 721	struct fib_nh *nh;
 722	int ret;
 723
 724	change_nexthops(fi) {
 725		int attrlen;
 726
 727		memset(&fib_cfg, 0, sizeof(fib_cfg));
 728
 729		if (!rtnh_ok(rtnh, remaining)) {
 730			NL_SET_ERR_MSG(extack,
 731				       "Invalid nexthop configuration - extra data after nexthop");
 732			return -EINVAL;
 733		}
 734
 735		if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
 736			NL_SET_ERR_MSG(extack,
 737				       "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
 738			return -EINVAL;
 739		}
 740
 741		fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
 742		fib_cfg.fc_oif = rtnh->rtnh_ifindex;
 743
 744		attrlen = rtnh_attrlen(rtnh);
 745		if (attrlen > 0) {
 746			struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
 747
 748			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
 749			nlav = nla_find(attrs, attrlen, RTA_VIA);
 750			if (nla && nlav) {
 751				NL_SET_ERR_MSG(extack,
 752					       "Nexthop configuration can not contain both GATEWAY and VIA");
 753				return -EINVAL;
 754			}
 755			if (nla) {
 756				ret = fib_gw_from_attr(&fib_cfg.fc_gw4, nla,
 757						       extack);
 758				if (ret)
 759					goto errout;
 760
 761				if (fib_cfg.fc_gw4)
 762					fib_cfg.fc_gw_family = AF_INET;
 763			} else if (nlav) {
 764				ret = fib_gw_from_via(&fib_cfg, nlav, extack);
 765				if (ret)
 766					goto errout;
 767			}
 768
 769			nla = nla_find(attrs, attrlen, RTA_FLOW);
 770			if (nla) {
 771				if (nla_len(nla) < sizeof(u32)) {
 772					NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
 773					return -EINVAL;
 774				}
 775				fib_cfg.fc_flow = nla_get_u32(nla);
 776			}
 777
 778			fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
 779			/* RTA_ENCAP_TYPE length checked in
 780			 * lwtunnel_valid_encap_type_attr
 781			 */
 782			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
 783			if (nla)
 784				fib_cfg.fc_encap_type = nla_get_u16(nla);
 785		}
 786
 787		ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
 788				  rtnh->rtnh_hops + 1, extack);
 789		if (ret)
 790			goto errout;
 791
 792		rtnh = rtnh_next(rtnh, &remaining);
 793	} endfor_nexthops(fi);
 794
 795	ret = -EINVAL;
 796	nh = fib_info_nh(fi, 0);
 797	if (cfg->fc_oif && nh->fib_nh_oif != cfg->fc_oif) {
 798		NL_SET_ERR_MSG(extack,
 799			       "Nexthop device index does not match RTA_OIF");
 800		goto errout;
 801	}
 802	if (cfg->fc_gw_family) {
 803		if (cfg->fc_gw_family != nh->fib_nh_gw_family ||
 804		    (cfg->fc_gw_family == AF_INET &&
 805		     nh->fib_nh_gw4 != cfg->fc_gw4) ||
 806		    (cfg->fc_gw_family == AF_INET6 &&
 807		     ipv6_addr_cmp(&nh->fib_nh_gw6, &cfg->fc_gw6))) {
 808			NL_SET_ERR_MSG(extack,
 809				       "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA");
 810			goto errout;
 811		}
 812	}
 813#ifdef CONFIG_IP_ROUTE_CLASSID
 814	if (cfg->fc_flow && nh->nh_tclassid != cfg->fc_flow) {
 815		NL_SET_ERR_MSG(extack,
 816			       "Nexthop class id does not match RTA_FLOW");
 817		goto errout;
 818	}
 819#endif
 820	ret = 0;
 821errout:
 822	return ret;
 823}
 824
 825/* only called when fib_nh is integrated into fib_info */
 826static void fib_rebalance(struct fib_info *fi)
 827{
 828	int total;
 829	int w;
 830
 831	if (fib_info_num_path(fi) < 2)
 832		return;
 833
 834	total = 0;
 835	for_nexthops(fi) {
 836		if (nh->fib_nh_flags & RTNH_F_DEAD)
 837			continue;
 838
 839		if (ip_ignore_linkdown(nh->fib_nh_dev) &&
 840		    nh->fib_nh_flags & RTNH_F_LINKDOWN)
 841			continue;
 842
 843		total += nh->fib_nh_weight;
 844	} endfor_nexthops(fi);
 845
 846	w = 0;
 847	change_nexthops(fi) {
 848		int upper_bound;
 849
 850		if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
 851			upper_bound = -1;
 852		} else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
 853			   nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
 854			upper_bound = -1;
 855		} else {
 856			w += nexthop_nh->fib_nh_weight;
 857			upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
 858							    total) - 1;
 859		}
 860
 861		atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
 862	} endfor_nexthops(fi);
 863}
 864#else /* CONFIG_IP_ROUTE_MULTIPATH */
 865
 866static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
 867		       int remaining, struct fib_config *cfg,
 868		       struct netlink_ext_ack *extack)
 869{
 870	NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
 871
 872	return -EINVAL;
 873}
 874
 875#define fib_rebalance(fi) do { } while (0)
 876
 877#endif /* CONFIG_IP_ROUTE_MULTIPATH */
 878
 879static int fib_encap_match(struct net *net, u16 encap_type,
 880			   struct nlattr *encap,
 881			   const struct fib_nh *nh,
 882			   const struct fib_config *cfg,
 883			   struct netlink_ext_ack *extack)
 884{
 885	struct lwtunnel_state *lwtstate;
 886	int ret, result = 0;
 887
 888	if (encap_type == LWTUNNEL_ENCAP_NONE)
 889		return 0;
 890
 891	ret = lwtunnel_build_state(net, encap_type, encap, AF_INET,
 892				   cfg, &lwtstate, extack);
 893	if (!ret) {
 894		result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
 895		lwtstate_free(lwtstate);
 896	}
 897
 898	return result;
 899}
 900
 901int fib_nh_match(struct net *net, struct fib_config *cfg, struct fib_info *fi,
 902		 struct netlink_ext_ack *extack)
 903{
 904#ifdef CONFIG_IP_ROUTE_MULTIPATH
 905	struct rtnexthop *rtnh;
 906	int remaining;
 907#endif
 908
 909	if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
 910		return 1;
 911
 912	if (cfg->fc_nh_id) {
 913		if (fi->nh && cfg->fc_nh_id == fi->nh->id)
 914			return 0;
 915		return 1;
 916	}
 917
 918	if (fi->nh) {
 919		if (cfg->fc_oif || cfg->fc_gw_family || cfg->fc_mp)
 920			return 1;
 921		return 0;
 922	}
 923
 924	if (cfg->fc_oif || cfg->fc_gw_family) {
 925		struct fib_nh *nh;
 926
 927		nh = fib_info_nh(fi, 0);
 928		if (cfg->fc_encap) {
 929			if (fib_encap_match(net, cfg->fc_encap_type,
 930					    cfg->fc_encap, nh, cfg, extack))
 931				return 1;
 932		}
 933#ifdef CONFIG_IP_ROUTE_CLASSID
 934		if (cfg->fc_flow &&
 935		    cfg->fc_flow != nh->nh_tclassid)
 936			return 1;
 937#endif
 938		if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) ||
 939		    (cfg->fc_gw_family &&
 940		     cfg->fc_gw_family != nh->fib_nh_gw_family))
 941			return 1;
 942
 943		if (cfg->fc_gw_family == AF_INET &&
 944		    cfg->fc_gw4 != nh->fib_nh_gw4)
 945			return 1;
 946
 947		if (cfg->fc_gw_family == AF_INET6 &&
 948		    ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6))
 949			return 1;
 950
 951		return 0;
 952	}
 953
 954#ifdef CONFIG_IP_ROUTE_MULTIPATH
 955	if (!cfg->fc_mp)
 956		return 0;
 957
 958	rtnh = cfg->fc_mp;
 959	remaining = cfg->fc_mp_len;
 960
 961	for_nexthops(fi) {
 962		int attrlen;
 963
 964		if (!rtnh_ok(rtnh, remaining))
 965			return -EINVAL;
 966
 967		if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
 968			return 1;
 969
 970		attrlen = rtnh_attrlen(rtnh);
 971		if (attrlen > 0) {
 972			struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
 973			int err;
 974
 975			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
 976			nlav = nla_find(attrs, attrlen, RTA_VIA);
 977			if (nla && nlav) {
 978				NL_SET_ERR_MSG(extack,
 979					       "Nexthop configuration can not contain both GATEWAY and VIA");
 980				return -EINVAL;
 981			}
 982
 983			if (nla) {
 984				__be32 gw;
 985
 986				err = fib_gw_from_attr(&gw, nla, extack);
 987				if (err)
 988					return err;
 989
 990				if (nh->fib_nh_gw_family != AF_INET ||
 991				    gw != nh->fib_nh_gw4)
 992					return 1;
 993			} else if (nlav) {
 994				struct fib_config cfg2;
 995
 996				err = fib_gw_from_via(&cfg2, nlav, extack);
 997				if (err)
 998					return err;
 999
1000				switch (nh->fib_nh_gw_family) {
1001				case AF_INET:
1002					if (cfg2.fc_gw_family != AF_INET ||
1003					    cfg2.fc_gw4 != nh->fib_nh_gw4)
1004						return 1;
1005					break;
1006				case AF_INET6:
1007					if (cfg2.fc_gw_family != AF_INET6 ||
1008					    ipv6_addr_cmp(&cfg2.fc_gw6,
1009							  &nh->fib_nh_gw6))
1010						return 1;
1011					break;
1012				}
1013			}
1014
1015#ifdef CONFIG_IP_ROUTE_CLASSID
1016			nla = nla_find(attrs, attrlen, RTA_FLOW);
1017			if (nla) {
1018				if (nla_len(nla) < sizeof(u32)) {
1019					NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
1020					return -EINVAL;
1021				}
1022				if (nla_get_u32(nla) != nh->nh_tclassid)
1023					return 1;
1024			}
1025#endif
1026		}
1027
1028		rtnh = rtnh_next(rtnh, &remaining);
1029	} endfor_nexthops(fi);
1030#endif
1031	return 0;
1032}
1033
1034bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
1035{
1036	struct nlattr *nla;
1037	int remaining;
1038
1039	if (!cfg->fc_mx)
1040		return true;
1041
1042	nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1043		int type = nla_type(nla);
1044		u32 fi_val, val;
1045
1046		if (!type)
1047			continue;
1048		if (type > RTAX_MAX)
1049			return false;
1050
1051		type = array_index_nospec(type, RTAX_MAX + 1);
1052		if (type == RTAX_CC_ALGO) {
1053			char tmp[TCP_CA_NAME_MAX];
1054			bool ecn_ca = false;
1055
1056			nla_strscpy(tmp, nla, sizeof(tmp));
1057			val = tcp_ca_get_key_by_name(tmp, &ecn_ca);
1058		} else {
1059			if (nla_len(nla) != sizeof(u32))
1060				return false;
1061			val = nla_get_u32(nla);
1062		}
1063
1064		fi_val = fi->fib_metrics->metrics[type - 1];
1065		if (type == RTAX_FEATURES)
1066			fi_val &= ~DST_FEATURE_ECN_CA;
1067
1068		if (fi_val != val)
1069			return false;
1070	}
1071
1072	return true;
1073}
1074
1075static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
1076			      u32 table, struct netlink_ext_ack *extack)
1077{
1078	struct fib6_config cfg = {
1079		.fc_table = table,
1080		.fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
1081		.fc_ifindex = nh->fib_nh_oif,
1082		.fc_gateway = nh->fib_nh_gw6,
1083	};
1084	struct fib6_nh fib6_nh = {};
1085	int err;
1086
1087	err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
1088	if (!err) {
1089		nh->fib_nh_dev = fib6_nh.fib_nh_dev;
1090		netdev_hold(nh->fib_nh_dev, &nh->fib_nh_dev_tracker,
1091			    GFP_KERNEL);
1092		nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
1093		nh->fib_nh_scope = RT_SCOPE_LINK;
1094
1095		ipv6_stub->fib6_nh_release(&fib6_nh);
1096	}
1097
1098	return err;
1099}
1100
1101/*
1102 * Picture
1103 * -------
1104 *
1105 * Semantics of nexthop is very messy by historical reasons.
1106 * We have to take into account, that:
1107 * a) gateway can be actually local interface address,
1108 *    so that gatewayed route is direct.
1109 * b) gateway must be on-link address, possibly
1110 *    described not by an ifaddr, but also by a direct route.
1111 * c) If both gateway and interface are specified, they should not
1112 *    contradict.
1113 * d) If we use tunnel routes, gateway could be not on-link.
1114 *
1115 * Attempt to reconcile all of these (alas, self-contradictory) conditions
1116 * results in pretty ugly and hairy code with obscure logic.
1117 *
1118 * I chose to generalized it instead, so that the size
1119 * of code does not increase practically, but it becomes
1120 * much more general.
1121 * Every prefix is assigned a "scope" value: "host" is local address,
1122 * "link" is direct route,
1123 * [ ... "site" ... "interior" ... ]
1124 * and "universe" is true gateway route with global meaning.
1125 *
1126 * Every prefix refers to a set of "nexthop"s (gw, oif),
1127 * where gw must have narrower scope. This recursion stops
1128 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
1129 * which means that gw is forced to be on link.
1130 *
1131 * Code is still hairy, but now it is apparently logically
1132 * consistent and very flexible. F.e. as by-product it allows
1133 * to co-exists in peace independent exterior and interior
1134 * routing processes.
1135 *
1136 * Normally it looks as following.
1137 *
1138 * {universe prefix}  -> (gw, oif) [scope link]
1139 *		  |
1140 *		  |-> {link prefix} -> (gw, oif) [scope local]
1141 *					|
1142 *					|-> {local prefix} (terminal node)
1143 */
1144static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
1145			      u8 scope, struct netlink_ext_ack *extack)
1146{
1147	struct net_device *dev;
1148	struct fib_result res;
1149	int err = 0;
1150
1151	if (nh->fib_nh_flags & RTNH_F_ONLINK) {
1152		unsigned int addr_type;
1153
1154		if (scope >= RT_SCOPE_LINK) {
1155			NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
1156			return -EINVAL;
1157		}
1158		dev = __dev_get_by_index(net, nh->fib_nh_oif);
1159		if (!dev) {
1160			NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
1161			return -ENODEV;
1162		}
1163		if (!(dev->flags & IFF_UP)) {
1164			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
1165			return -ENETDOWN;
1166		}
1167		addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
1168		if (addr_type != RTN_UNICAST) {
1169			NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1170			return -EINVAL;
1171		}
1172		if (!netif_carrier_ok(dev))
1173			nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1174		nh->fib_nh_dev = dev;
1175		netdev_hold(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
1176		nh->fib_nh_scope = RT_SCOPE_LINK;
1177		return 0;
1178	}
1179	rcu_read_lock();
1180	{
1181		struct fib_table *tbl = NULL;
1182		struct flowi4 fl4 = {
1183			.daddr = nh->fib_nh_gw4,
1184			.flowi4_scope = scope + 1,
1185			.flowi4_oif = nh->fib_nh_oif,
1186			.flowi4_iif = LOOPBACK_IFINDEX,
1187		};
1188
1189		/* It is not necessary, but requires a bit of thinking */
1190		if (fl4.flowi4_scope < RT_SCOPE_LINK)
1191			fl4.flowi4_scope = RT_SCOPE_LINK;
1192
1193		if (table && table != RT_TABLE_MAIN)
1194			tbl = fib_get_table(net, table);
1195
1196		if (tbl)
1197			err = fib_table_lookup(tbl, &fl4, &res,
1198					       FIB_LOOKUP_IGNORE_LINKSTATE |
1199					       FIB_LOOKUP_NOREF);
1200
1201		/* on error or if no table given do full lookup. This
1202		 * is needed for example when nexthops are in the local
1203		 * table rather than the given table
1204		 */
1205		if (!tbl || err) {
1206			err = fib_lookup(net, &fl4, &res,
1207					 FIB_LOOKUP_IGNORE_LINKSTATE);
1208		}
1209
1210		if (err) {
1211			NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1212			goto out;
1213		}
1214	}
1215
1216	err = -EINVAL;
1217	if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
1218		NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1219		goto out;
1220	}
1221	nh->fib_nh_scope = res.scope;
1222	nh->fib_nh_oif = FIB_RES_OIF(res);
1223	nh->fib_nh_dev = dev = FIB_RES_DEV(res);
1224	if (!dev) {
1225		NL_SET_ERR_MSG(extack,
1226			       "No egress device for nexthop gateway");
1227		goto out;
1228	}
1229	netdev_hold(dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
1230	if (!netif_carrier_ok(dev))
1231		nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1232	err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
1233out:
1234	rcu_read_unlock();
1235	return err;
1236}
1237
1238static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
1239			      struct netlink_ext_ack *extack)
1240{
1241	struct in_device *in_dev;
1242	int err;
1243
1244	if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
1245		NL_SET_ERR_MSG(extack,
1246			       "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
1247		return -EINVAL;
1248	}
1249
1250	rcu_read_lock();
1251
1252	err = -ENODEV;
1253	in_dev = inetdev_by_index(net, nh->fib_nh_oif);
1254	if (!in_dev)
1255		goto out;
1256	err = -ENETDOWN;
1257	if (!(in_dev->dev->flags & IFF_UP)) {
1258		NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
1259		goto out;
1260	}
1261
1262	nh->fib_nh_dev = in_dev->dev;
1263	netdev_hold(nh->fib_nh_dev, &nh->fib_nh_dev_tracker, GFP_ATOMIC);
1264	nh->fib_nh_scope = RT_SCOPE_HOST;
1265	if (!netif_carrier_ok(nh->fib_nh_dev))
1266		nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1267	err = 0;
1268out:
1269	rcu_read_unlock();
1270	return err;
1271}
1272
1273int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
1274		 struct netlink_ext_ack *extack)
1275{
1276	int err;
1277
1278	if (nh->fib_nh_gw_family == AF_INET)
1279		err = fib_check_nh_v4_gw(net, nh, table, scope, extack);
1280	else if (nh->fib_nh_gw_family == AF_INET6)
1281		err = fib_check_nh_v6_gw(net, nh, table, extack);
1282	else
1283		err = fib_check_nh_nongw(net, nh, extack);
1284
1285	return err;
1286}
1287
1288__be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
1289				 unsigned char scope)
1290{
1291	struct fib_nh *nh;
1292	__be32 saddr;
1293
1294	if (nhc->nhc_family != AF_INET)
1295		return inet_select_addr(nhc->nhc_dev, 0, scope);
1296
1297	nh = container_of(nhc, struct fib_nh, nh_common);
1298	saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope);
1299
1300	WRITE_ONCE(nh->nh_saddr, saddr);
1301	WRITE_ONCE(nh->nh_saddr_genid, atomic_read(&net->ipv4.dev_addr_genid));
1302
1303	return saddr;
1304}
1305
1306__be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1307{
1308	struct fib_nh_common *nhc = res->nhc;
1309
1310	if (res->fi->fib_prefsrc)
1311		return res->fi->fib_prefsrc;
1312
1313	if (nhc->nhc_family == AF_INET) {
1314		struct fib_nh *nh;
1315
1316		nh = container_of(nhc, struct fib_nh, nh_common);
1317		if (READ_ONCE(nh->nh_saddr_genid) ==
1318		    atomic_read(&net->ipv4.dev_addr_genid))
1319			return READ_ONCE(nh->nh_saddr);
1320	}
1321
1322	return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope);
1323}
1324
1325static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1326{
1327	if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1328	    fib_prefsrc != cfg->fc_dst) {
1329		u32 tb_id = cfg->fc_table;
1330		int rc;
1331
1332		if (tb_id == RT_TABLE_MAIN)
1333			tb_id = RT_TABLE_LOCAL;
1334
1335		rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1336					  fib_prefsrc, tb_id);
1337
1338		if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1339			rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1340						  fib_prefsrc, RT_TABLE_LOCAL);
1341		}
1342
1343		if (rc != RTN_LOCAL)
1344			return false;
1345	}
1346	return true;
1347}
1348
1349struct fib_info *fib_create_info(struct fib_config *cfg,
1350				 struct netlink_ext_ack *extack)
1351{
1352	int err;
1353	struct fib_info *fi = NULL;
1354	struct nexthop *nh = NULL;
1355	struct fib_info *ofi;
1356	int nhs = 1;
1357	struct net *net = cfg->fc_nlinfo.nl_net;
1358
1359	ASSERT_RTNL();
1360	if (cfg->fc_type > RTN_MAX)
1361		goto err_inval;
1362
1363	/* Fast check to catch the most weird cases */
1364	if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1365		NL_SET_ERR_MSG(extack, "Invalid scope");
1366		goto err_inval;
1367	}
1368
1369	if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1370		NL_SET_ERR_MSG(extack,
1371			       "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
1372		goto err_inval;
1373	}
1374
1375	if (cfg->fc_nh_id) {
1376		if (!cfg->fc_mx) {
1377			fi = fib_find_info_nh(net, cfg);
1378			if (fi) {
1379				refcount_inc(&fi->fib_treeref);
1380				return fi;
1381			}
1382		}
1383
1384		nh = nexthop_find_by_id(net, cfg->fc_nh_id);
1385		if (!nh) {
1386			NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
1387			goto err_inval;
1388		}
1389		nhs = 0;
1390	}
1391
1392#ifdef CONFIG_IP_ROUTE_MULTIPATH
1393	if (cfg->fc_mp) {
1394		nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
1395		if (nhs == 0)
1396			goto err_inval;
1397	}
1398#endif
1399
1400	fib_info_hash_grow(net);
1401
1402	fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
1403	if (!fi) {
1404		err = -ENOBUFS;
1405		goto failure;
1406	}
1407
1408	fi->fib_metrics = ip_fib_metrics_init(cfg->fc_mx, cfg->fc_mx_len, extack);
1409	if (IS_ERR(fi->fib_metrics)) {
1410		err = PTR_ERR(fi->fib_metrics);
1411		kfree(fi);
1412		return ERR_PTR(err);
1413	}
1414
1415	fi->fib_net = net;
1416	fi->fib_protocol = cfg->fc_protocol;
1417	fi->fib_scope = cfg->fc_scope;
1418	fi->fib_flags = cfg->fc_flags;
1419	fi->fib_priority = cfg->fc_priority;
1420	fi->fib_prefsrc = cfg->fc_prefsrc;
1421	fi->fib_type = cfg->fc_type;
1422	fi->fib_tb_id = cfg->fc_table;
1423
1424	fi->fib_nhs = nhs;
1425	if (nh) {
1426		if (!nexthop_get(nh)) {
1427			NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
1428			err = -EINVAL;
1429		} else {
1430			err = 0;
1431			fi->nh = nh;
1432		}
1433	} else {
1434		change_nexthops(fi) {
1435			nexthop_nh->nh_parent = fi;
1436		} endfor_nexthops(fi)
1437
1438		if (cfg->fc_mp)
1439			err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg,
1440					  extack);
1441		else
1442			err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
1443	}
1444
1445	if (err != 0)
1446		goto failure;
1447
1448	if (fib_props[cfg->fc_type].error) {
1449		if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
1450			NL_SET_ERR_MSG(extack,
1451				       "Gateway, device and multipath can not be specified for this route type");
1452			goto err_inval;
1453		}
1454		goto link_it;
1455	} else {
1456		switch (cfg->fc_type) {
1457		case RTN_UNICAST:
1458		case RTN_LOCAL:
1459		case RTN_BROADCAST:
1460		case RTN_ANYCAST:
1461		case RTN_MULTICAST:
1462			break;
1463		default:
1464			NL_SET_ERR_MSG(extack, "Invalid route type");
1465			goto err_inval;
1466		}
1467	}
1468
1469	if (cfg->fc_scope > RT_SCOPE_HOST) {
1470		NL_SET_ERR_MSG(extack, "Invalid scope");
1471		goto err_inval;
1472	}
1473
1474	if (fi->nh) {
1475		err = fib_check_nexthop(fi->nh, cfg->fc_scope, extack);
1476		if (err)
1477			goto failure;
1478	} else if (cfg->fc_scope == RT_SCOPE_HOST) {
1479		struct fib_nh *nh = fi->fib_nh;
1480
1481		/* Local address is added. */
1482		if (nhs != 1) {
1483			NL_SET_ERR_MSG(extack,
1484				       "Route with host scope can not have multiple nexthops");
1485			goto err_inval;
1486		}
1487		if (nh->fib_nh_gw_family) {
1488			NL_SET_ERR_MSG(extack,
1489				       "Route with host scope can not have a gateway");
1490			goto err_inval;
1491		}
1492		nh->fib_nh_scope = RT_SCOPE_NOWHERE;
1493		nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif);
1494		err = -ENODEV;
1495		if (!nh->fib_nh_dev)
1496			goto failure;
1497		netdev_tracker_alloc(nh->fib_nh_dev, &nh->fib_nh_dev_tracker,
1498				     GFP_KERNEL);
1499	} else {
1500		int linkdown = 0;
1501
1502		change_nexthops(fi) {
1503			err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
1504					   cfg->fc_table, cfg->fc_scope,
1505					   extack);
1506			if (err != 0)
1507				goto failure;
1508			if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
1509				linkdown++;
1510		} endfor_nexthops(fi)
1511		if (linkdown == fi->fib_nhs)
1512			fi->fib_flags |= RTNH_F_LINKDOWN;
1513	}
1514
1515	if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1516		NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
1517		goto err_inval;
1518	}
1519
1520	if (!fi->nh) {
1521		change_nexthops(fi) {
1522			fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common,
1523						  fi->fib_scope);
1524			if (nexthop_nh->fib_nh_gw_family == AF_INET6)
1525				fi->fib_nh_is_v6 = true;
1526		} endfor_nexthops(fi)
1527
1528		fib_rebalance(fi);
1529	}
1530
1531link_it:
1532	ofi = fib_find_info(fi);
1533	if (ofi) {
1534		/* fib_table_lookup() should not see @fi yet. */
1535		fi->fib_dead = 1;
1536		free_fib_info(fi);
1537		refcount_inc(&ofi->fib_treeref);
1538		return ofi;
1539	}
1540
1541	refcount_set(&fi->fib_treeref, 1);
1542	refcount_set(&fi->fib_clntref, 1);
1543
1544	net->ipv4.fib_info_cnt++;
1545	hlist_add_head(&fi->fib_hash, fib_info_hash_bucket(fi));
1546
1547	if (fi->fib_prefsrc) {
1548		struct hlist_head *head;
1549
1550		head = fib_info_laddrhash_bucket(net, fi->fib_prefsrc);
1551		hlist_add_head(&fi->fib_lhash, head);
1552	}
1553	if (fi->nh) {
1554		list_add(&fi->nh_list, &nh->fi_list);
1555	} else {
1556		change_nexthops(fi) {
1557			struct hlist_head *head;
1558
1559			if (!nexthop_nh->fib_nh_dev)
1560				continue;
1561			head = fib_nh_head(nexthop_nh->fib_nh_dev);
1562			hlist_add_head_rcu(&nexthop_nh->nh_hash, head);
1563		} endfor_nexthops(fi)
1564	}
1565	return fi;
1566
1567err_inval:
1568	err = -EINVAL;
1569
1570failure:
1571	if (fi) {
1572		/* fib_table_lookup() should not see @fi yet. */
1573		fi->fib_dead = 1;
1574		free_fib_info(fi);
1575	}
1576
1577	return ERR_PTR(err);
1578}
1579
1580int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
1581		     u8 rt_family, unsigned char *flags, bool skip_oif)
1582{
1583	if (nhc->nhc_flags & RTNH_F_DEAD)
1584		*flags |= RTNH_F_DEAD;
1585
1586	if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
1587		*flags |= RTNH_F_LINKDOWN;
1588
1589		rcu_read_lock();
1590		switch (nhc->nhc_family) {
1591		case AF_INET:
1592			if (ip_ignore_linkdown(nhc->nhc_dev))
1593				*flags |= RTNH_F_DEAD;
1594			break;
1595		case AF_INET6:
1596			if (ip6_ignore_linkdown(nhc->nhc_dev))
1597				*flags |= RTNH_F_DEAD;
1598			break;
1599		}
1600		rcu_read_unlock();
1601	}
1602
1603	switch (nhc->nhc_gw_family) {
1604	case AF_INET:
1605		if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1606			goto nla_put_failure;
1607		break;
1608	case AF_INET6:
1609		/* if gateway family does not match nexthop family
1610		 * gateway is encoded as RTA_VIA
1611		 */
1612		if (rt_family != nhc->nhc_gw_family) {
1613			int alen = sizeof(struct in6_addr);
1614			struct nlattr *nla;
1615			struct rtvia *via;
1616
1617			nla = nla_reserve(skb, RTA_VIA, alen + 2);
1618			if (!nla)
1619				goto nla_put_failure;
1620
1621			via = nla_data(nla);
1622			via->rtvia_family = AF_INET6;
1623			memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen);
1624		} else if (nla_put_in6_addr(skb, RTA_GATEWAY,
1625					    &nhc->nhc_gw.ipv6) < 0) {
1626			goto nla_put_failure;
1627		}
1628		break;
1629	}
1630
1631	*flags |= (nhc->nhc_flags &
1632		   (RTNH_F_ONLINK | RTNH_F_OFFLOAD | RTNH_F_TRAP));
1633
1634	if (!skip_oif && nhc->nhc_dev &&
1635	    nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
1636		goto nla_put_failure;
1637
1638	if (lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
1639				RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
1640		goto nla_put_failure;
1641
1642	return 0;
1643
1644nla_put_failure:
1645	return -EMSGSIZE;
1646}
1647EXPORT_SYMBOL_GPL(fib_nexthop_info);
1648
1649#if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1650int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
1651		    int nh_weight, u8 rt_family, u32 nh_tclassid)
1652{
1653	const struct net_device *dev = nhc->nhc_dev;
1654	struct rtnexthop *rtnh;
1655	unsigned char flags = 0;
1656
1657	rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1658	if (!rtnh)
1659		goto nla_put_failure;
1660
1661	rtnh->rtnh_hops = nh_weight - 1;
1662	rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1663
1664	if (fib_nexthop_info(skb, nhc, rt_family, &flags, true) < 0)
1665		goto nla_put_failure;
1666
1667	rtnh->rtnh_flags = flags;
1668
1669	if (nh_tclassid && nla_put_u32(skb, RTA_FLOW, nh_tclassid))
1670		goto nla_put_failure;
1671
1672	/* length of rtnetlink header + attributes */
1673	rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1674
1675	return 0;
1676
1677nla_put_failure:
1678	return -EMSGSIZE;
1679}
1680EXPORT_SYMBOL_GPL(fib_add_nexthop);
1681#endif
1682
1683#ifdef CONFIG_IP_ROUTE_MULTIPATH
1684static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1685{
1686	struct nlattr *mp;
1687
1688	mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
1689	if (!mp)
1690		goto nla_put_failure;
1691
1692	if (unlikely(fi->nh)) {
1693		if (nexthop_mpath_fill_node(skb, fi->nh, AF_INET) < 0)
1694			goto nla_put_failure;
1695		goto mp_end;
1696	}
1697
1698	for_nexthops(fi) {
1699		u32 nh_tclassid = 0;
1700#ifdef CONFIG_IP_ROUTE_CLASSID
1701		nh_tclassid = nh->nh_tclassid;
1702#endif
1703		if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight,
1704				    AF_INET, nh_tclassid) < 0)
1705			goto nla_put_failure;
1706	} endfor_nexthops(fi);
1707
1708mp_end:
1709	nla_nest_end(skb, mp);
1710
1711	return 0;
1712
1713nla_put_failure:
1714	return -EMSGSIZE;
1715}
1716#else
1717static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1718{
1719	return 0;
1720}
1721#endif
1722
1723int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
1724		  const struct fib_rt_info *fri, unsigned int flags)
1725{
1726	unsigned int nhs = fib_info_num_path(fri->fi);
1727	struct fib_info *fi = fri->fi;
1728	u32 tb_id = fri->tb_id;
1729	struct nlmsghdr *nlh;
1730	struct rtmsg *rtm;
1731
1732	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1733	if (!nlh)
1734		return -EMSGSIZE;
1735
1736	rtm = nlmsg_data(nlh);
1737	rtm->rtm_family = AF_INET;
1738	rtm->rtm_dst_len = fri->dst_len;
1739	rtm->rtm_src_len = 0;
1740	rtm->rtm_tos = inet_dscp_to_dsfield(fri->dscp);
1741	if (tb_id < 256)
1742		rtm->rtm_table = tb_id;
1743	else
1744		rtm->rtm_table = RT_TABLE_COMPAT;
1745	if (nla_put_u32(skb, RTA_TABLE, tb_id))
1746		goto nla_put_failure;
1747	rtm->rtm_type = fri->type;
1748	rtm->rtm_flags = fi->fib_flags;
1749	rtm->rtm_scope = fi->fib_scope;
1750	rtm->rtm_protocol = fi->fib_protocol;
1751
1752	if (rtm->rtm_dst_len &&
1753	    nla_put_in_addr(skb, RTA_DST, fri->dst))
1754		goto nla_put_failure;
1755	if (fi->fib_priority &&
1756	    nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1757		goto nla_put_failure;
1758	if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
1759		goto nla_put_failure;
1760
1761	if (fi->fib_prefsrc &&
1762	    nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
1763		goto nla_put_failure;
1764
1765	if (fi->nh) {
1766		if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id))
1767			goto nla_put_failure;
1768		if (nexthop_is_blackhole(fi->nh))
1769			rtm->rtm_type = RTN_BLACKHOLE;
1770		if (!READ_ONCE(fi->fib_net->ipv4.sysctl_nexthop_compat_mode))
1771			goto offload;
1772	}
1773
1774	if (nhs == 1) {
1775		const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
1776		unsigned char flags = 0;
1777
1778		if (fib_nexthop_info(skb, nhc, AF_INET, &flags, false) < 0)
1779			goto nla_put_failure;
1780
1781		rtm->rtm_flags = flags;
1782#ifdef CONFIG_IP_ROUTE_CLASSID
1783		if (nhc->nhc_family == AF_INET) {
1784			struct fib_nh *nh;
1785
1786			nh = container_of(nhc, struct fib_nh, nh_common);
1787			if (nh->nh_tclassid &&
1788			    nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1789				goto nla_put_failure;
1790		}
1791#endif
1792	} else {
1793		if (fib_add_multipath(skb, fi) < 0)
1794			goto nla_put_failure;
1795	}
1796
1797offload:
1798	if (fri->offload)
1799		rtm->rtm_flags |= RTM_F_OFFLOAD;
1800	if (fri->trap)
1801		rtm->rtm_flags |= RTM_F_TRAP;
1802	if (fri->offload_failed)
1803		rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
1804
1805	nlmsg_end(skb, nlh);
1806	return 0;
1807
1808nla_put_failure:
1809	nlmsg_cancel(skb, nlh);
1810	return -EMSGSIZE;
1811}
1812
1813/*
1814 * Update FIB if:
1815 * - local address disappeared -> we must delete all the entries
1816 *   referring to it.
1817 * - device went down -> we must shutdown all nexthops going via it.
1818 */
1819int fib_sync_down_addr(struct net_device *dev, __be32 local)
1820{
1821	int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
1822	struct net *net = dev_net(dev);
1823	struct hlist_head *head;
1824	struct fib_info *fi;
1825	int ret = 0;
1826
1827	if (!local)
1828		return 0;
1829
1830	head = fib_info_laddrhash_bucket(net, local);
1831	hlist_for_each_entry(fi, head, fib_lhash) {
1832		if (!net_eq(fi->fib_net, net) ||
1833		    fi->fib_tb_id != tb_id)
1834			continue;
1835		if (fi->fib_prefsrc == local) {
1836			fi->fib_flags |= RTNH_F_DEAD;
1837			fi->pfsrc_removed = true;
1838			ret++;
1839		}
1840	}
1841	return ret;
1842}
1843
1844static int call_fib_nh_notifiers(struct fib_nh *nh,
1845				 enum fib_event_type event_type)
1846{
1847	bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
1848	struct fib_nh_notifier_info info = {
1849		.fib_nh = nh,
1850	};
1851
1852	switch (event_type) {
1853	case FIB_EVENT_NH_ADD:
1854		if (nh->fib_nh_flags & RTNH_F_DEAD)
1855			break;
1856		if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
1857			break;
1858		return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
1859					   &info.info);
1860	case FIB_EVENT_NH_DEL:
1861		if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1862		    (nh->fib_nh_flags & RTNH_F_DEAD))
1863			return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
1864						   event_type, &info.info);
1865		break;
1866	default:
1867		break;
1868	}
1869
1870	return NOTIFY_DONE;
1871}
1872
1873/* Update the PMTU of exceptions when:
1874 * - the new MTU of the first hop becomes smaller than the PMTU
1875 * - the old MTU was the same as the PMTU, and it limited discovery of
1876 *   larger MTUs on the path. With that limit raised, we can now
1877 *   discover larger MTUs
1878 * A special case is locked exceptions, for which the PMTU is smaller
1879 * than the minimal accepted PMTU:
1880 * - if the new MTU is greater than the PMTU, don't make any change
1881 * - otherwise, unlock and set PMTU
1882 */
1883void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
1884{
1885	struct fnhe_hash_bucket *bucket;
1886	int i;
1887
1888	bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
1889	if (!bucket)
1890		return;
1891
1892	for (i = 0; i < FNHE_HASH_SIZE; i++) {
1893		struct fib_nh_exception *fnhe;
1894
1895		for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1896		     fnhe;
1897		     fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1898			if (fnhe->fnhe_mtu_locked) {
1899				if (new <= fnhe->fnhe_pmtu) {
1900					fnhe->fnhe_pmtu = new;
1901					fnhe->fnhe_mtu_locked = false;
1902				}
1903			} else if (new < fnhe->fnhe_pmtu ||
1904				   orig == fnhe->fnhe_pmtu) {
1905				fnhe->fnhe_pmtu = new;
1906			}
1907		}
1908	}
1909}
1910
1911void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1912{
1913	struct hlist_head *head = fib_nh_head(dev);
1914	struct fib_nh *nh;
1915
1916	hlist_for_each_entry(nh, head, nh_hash) {
1917		DEBUG_NET_WARN_ON_ONCE(nh->fib_nh_dev != dev);
1918		fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
1919	}
1920}
1921
1922/* Event              force Flags           Description
1923 * NETDEV_CHANGE      0     LINKDOWN        Carrier OFF, not for scope host
1924 * NETDEV_DOWN        0     LINKDOWN|DEAD   Link down, not for scope host
1925 * NETDEV_DOWN        1     LINKDOWN|DEAD   Last address removed
1926 * NETDEV_UNREGISTER  1     LINKDOWN|DEAD   Device removed
1927 *
1928 * only used when fib_nh is built into fib_info
1929 */
1930int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
1931{
1932	struct hlist_head *head = fib_nh_head(dev);
1933	struct fib_info *prev_fi = NULL;
1934	int scope = RT_SCOPE_NOWHERE;
1935	struct fib_nh *nh;
1936	int ret = 0;
1937
1938	if (force)
1939		scope = -1;
1940
1941	hlist_for_each_entry(nh, head, nh_hash) {
1942		struct fib_info *fi = nh->nh_parent;
1943		int dead;
1944
1945		BUG_ON(!fi->fib_nhs);
1946		DEBUG_NET_WARN_ON_ONCE(nh->fib_nh_dev != dev);
1947		if (fi == prev_fi)
1948			continue;
1949		prev_fi = fi;
1950		dead = 0;
1951		change_nexthops(fi) {
1952			if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
1953				dead++;
1954			else if (nexthop_nh->fib_nh_dev == dev &&
1955				 nexthop_nh->fib_nh_scope != scope) {
1956				switch (event) {
1957				case NETDEV_DOWN:
1958				case NETDEV_UNREGISTER:
1959					nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
1960					fallthrough;
1961				case NETDEV_CHANGE:
1962					nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1963					break;
1964				}
1965				call_fib_nh_notifiers(nexthop_nh,
1966						      FIB_EVENT_NH_DEL);
1967				dead++;
1968			}
1969#ifdef CONFIG_IP_ROUTE_MULTIPATH
1970			if (event == NETDEV_UNREGISTER &&
1971			    nexthop_nh->fib_nh_dev == dev) {
1972				dead = fi->fib_nhs;
1973				break;
1974			}
1975#endif
1976		} endfor_nexthops(fi)
1977		if (dead == fi->fib_nhs) {
1978			switch (event) {
1979			case NETDEV_DOWN:
1980			case NETDEV_UNREGISTER:
1981				fi->fib_flags |= RTNH_F_DEAD;
1982				fallthrough;
1983			case NETDEV_CHANGE:
1984				fi->fib_flags |= RTNH_F_LINKDOWN;
1985				break;
1986			}
1987			ret++;
1988		}
1989
1990		fib_rebalance(fi);
1991	}
1992
1993	return ret;
1994}
1995
1996/* Must be invoked inside of an RCU protected region.  */
1997static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
1998{
1999	struct fib_info *fi = NULL, *last_resort = NULL;
2000	struct hlist_head *fa_head = res->fa_head;
2001	struct fib_table *tb = res->table;
2002	u8 slen = 32 - res->prefixlen;
2003	int order = -1, last_idx = -1;
2004	struct fib_alias *fa, *fa1 = NULL;
2005	u32 last_prio = res->fi->fib_priority;
2006	dscp_t last_dscp = 0;
2007
2008	hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
2009		struct fib_info *next_fi = fa->fa_info;
2010		struct fib_nh_common *nhc;
2011
2012		if (fa->fa_slen != slen)
2013			continue;
2014		if (fa->fa_dscp && !fib_dscp_masked_match(fa->fa_dscp, flp))
2015			continue;
2016		if (fa->tb_id != tb->tb_id)
2017			continue;
2018		if (next_fi->fib_priority > last_prio &&
2019		    fa->fa_dscp == last_dscp) {
2020			if (last_dscp)
2021				continue;
2022			break;
2023		}
2024		if (next_fi->fib_flags & RTNH_F_DEAD)
2025			continue;
2026		last_dscp = fa->fa_dscp;
2027		last_prio = next_fi->fib_priority;
2028
2029		if (next_fi->fib_scope != res->scope ||
2030		    fa->fa_type != RTN_UNICAST)
2031			continue;
2032
2033		nhc = fib_info_nhc(next_fi, 0);
2034		if (!nhc->nhc_gw_family || nhc->nhc_scope != RT_SCOPE_LINK)
2035			continue;
2036
2037		fib_alias_accessed(fa);
2038
2039		if (!fi) {
2040			if (next_fi != res->fi)
2041				break;
2042			fa1 = fa;
2043		} else if (!fib_detect_death(fi, order, &last_resort,
2044					     &last_idx, fa1->fa_default)) {
2045			fib_result_assign(res, fi);
2046			fa1->fa_default = order;
2047			goto out;
2048		}
2049		fi = next_fi;
2050		order++;
2051	}
2052
2053	if (order <= 0 || !fi) {
2054		if (fa1)
2055			fa1->fa_default = -1;
2056		goto out;
2057	}
2058
2059	if (!fib_detect_death(fi, order, &last_resort, &last_idx,
2060			      fa1->fa_default)) {
2061		fib_result_assign(res, fi);
2062		fa1->fa_default = order;
2063		goto out;
2064	}
2065
2066	if (last_idx >= 0)
2067		fib_result_assign(res, last_resort);
2068	fa1->fa_default = last_idx;
2069out:
2070	return;
2071}
2072
2073/*
2074 * Dead device goes up. We wake up dead nexthops.
2075 * It takes sense only on multipath routes.
2076 *
2077 * only used when fib_nh is built into fib_info
2078 */
2079int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
2080{
2081	struct fib_info *prev_fi;
2082	struct hlist_head *head;
2083	struct fib_nh *nh;
2084	int ret;
2085
2086	if (!(dev->flags & IFF_UP))
2087		return 0;
2088
2089	if (nh_flags & RTNH_F_DEAD) {
2090		unsigned int flags = netif_get_flags(dev);
2091
2092		if (flags & (IFF_RUNNING | IFF_LOWER_UP))
2093			nh_flags |= RTNH_F_LINKDOWN;
2094	}
2095
2096	prev_fi = NULL;
2097	head = fib_nh_head(dev);
2098	ret = 0;
2099
2100	hlist_for_each_entry(nh, head, nh_hash) {
2101		struct fib_info *fi = nh->nh_parent;
2102		int alive;
2103
2104		BUG_ON(!fi->fib_nhs);
2105		DEBUG_NET_WARN_ON_ONCE(nh->fib_nh_dev != dev);
2106		if (fi == prev_fi)
2107			continue;
2108
2109		prev_fi = fi;
2110		alive = 0;
2111		change_nexthops(fi) {
2112			if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
2113				alive++;
2114				continue;
2115			}
2116			if (!nexthop_nh->fib_nh_dev ||
2117			    !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
2118				continue;
2119			if (nexthop_nh->fib_nh_dev != dev ||
2120			    !__in_dev_get_rtnl(dev))
2121				continue;
2122			alive++;
2123			nexthop_nh->fib_nh_flags &= ~nh_flags;
2124			call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
2125		} endfor_nexthops(fi)
2126
2127		if (alive > 0) {
2128			fi->fib_flags &= ~nh_flags;
2129			ret++;
2130		}
2131
2132		fib_rebalance(fi);
2133	}
2134
2135	return ret;
2136}
2137
2138#ifdef CONFIG_IP_ROUTE_MULTIPATH
2139static bool fib_good_nh(const struct fib_nh *nh)
2140{
2141	int state = NUD_REACHABLE;
2142
2143	if (nh->fib_nh_scope == RT_SCOPE_LINK) {
2144		struct neighbour *n;
2145
2146		rcu_read_lock();
2147
2148		if (likely(nh->fib_nh_gw_family == AF_INET))
2149			n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
2150						   (__force u32)nh->fib_nh_gw4);
2151		else if (nh->fib_nh_gw_family == AF_INET6)
2152			n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev,
2153							   &nh->fib_nh_gw6);
2154		else
2155			n = NULL;
2156		if (n)
2157			state = READ_ONCE(n->nud_state);
2158
2159		rcu_read_unlock();
2160	}
2161
2162	return !!(state & NUD_VALID);
2163}
2164
2165void fib_select_multipath(struct fib_result *res, int hash,
2166			  const struct flowi4 *fl4)
2167{
2168	struct fib_info *fi = res->fi;
2169	struct net *net = fi->fib_net;
2170	bool use_neigh;
2171	int score = -1;
2172	__be32 saddr;
2173
2174	if (unlikely(res->fi->nh)) {
2175		nexthop_path_fib_result(res, hash);
2176		return;
2177	}
2178
2179	use_neigh = READ_ONCE(net->ipv4.sysctl_fib_multipath_use_neigh);
2180	saddr = fl4 ? fl4->saddr : 0;
2181
2182	change_nexthops(fi) {
2183		int nh_upper_bound, nh_score = 0;
2184
2185		/* Nexthops without a carrier are assigned an upper bound of
2186		 * minus one when "ignore_routes_with_linkdown" is set.
2187		 */
2188		nh_upper_bound = atomic_read(&nexthop_nh->fib_nh_upper_bound);
2189		if (nh_upper_bound == -1 ||
2190		    (use_neigh && !fib_good_nh(nexthop_nh)))
2191			continue;
2192
2193		if (saddr && nexthop_nh->nh_saddr == saddr)
2194			nh_score += 2;
2195		if (hash <= nh_upper_bound)
2196			nh_score++;
2197		if (score < nh_score) {
2198			res->nh_sel = nhsel;
2199			res->nhc = &nexthop_nh->nh_common;
2200			if (nh_score == 3 || (!saddr && nh_score == 1))
2201				return;
2202			score = nh_score;
2203		}
2204
2205	} endfor_nexthops(fi);
2206}
2207#endif
2208
2209void fib_select_path(struct net *net, struct fib_result *res,
2210		     struct flowi4 *fl4, const struct sk_buff *skb)
2211{
2212	if (fl4->flowi4_oif)
2213		goto check_saddr;
2214
2215#ifdef CONFIG_IP_ROUTE_MULTIPATH
2216	if (fib_info_num_path(res->fi) > 1) {
2217		int h = fib_multipath_hash(net, fl4, skb, NULL);
2218
2219		fib_select_multipath(res, h, fl4);
2220	}
2221	else
2222#endif
2223	if (!res->prefixlen &&
2224	    res->table->tb_num_default > 1 &&
2225	    res->type == RTN_UNICAST)
2226		fib_select_default(fl4, res);
2227
2228check_saddr:
2229	if (!fl4->saddr) {
2230		struct net_device *l3mdev;
2231
2232		l3mdev = dev_get_by_index_rcu(net, fl4->flowi4_l3mdev);
2233
2234		if (!l3mdev ||
2235		    l3mdev_master_dev_rcu(FIB_RES_DEV(*res)) == l3mdev)
2236			fl4->saddr = fib_result_prefsrc(net, res);
2237		else
2238			fl4->saddr = inet_select_addr(l3mdev, 0, RT_SCOPE_LINK);
2239	}
2240}
2241
2242int __net_init fib4_semantics_init(struct net *net)
2243{
2244	unsigned int hash_bits = 4;
2245
2246	net->ipv4.fib_info_hash = fib_info_hash_alloc(hash_bits);
2247	if (!net->ipv4.fib_info_hash)
2248		return -ENOMEM;
2249
2250	net->ipv4.fib_info_hash_bits = hash_bits;
2251	net->ipv4.fib_info_cnt = 0;
2252
2253	return 0;
2254}
2255
2256void __net_exit fib4_semantics_exit(struct net *net)
2257{
2258	fib_info_hash_free(net->ipv4.fib_info_hash);
2259}
~/ Read-only file preview.