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