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