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