Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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