1 // SPDX-License-Identifier: GPL-2.0
2 /* Generic nexthop implementation
3 *
4 * Copyright (c) 2017-19 Cumulus Networks
5 * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com>
6 */
7
8 #include <linux/nexthop.h>
9 #include <linux/rtnetlink.h>
10 #include <linux/slab.h>
11 #include <net/arp.h>
12 #include <net/ipv6_stubs.h>
13 #include <net/lwtunnel.h>
14 #include <net/ndisc.h>
15 #include <net/nexthop.h>
16 #include <net/route.h>
17 #include <net/sock.h>
18
19 static void remove_nexthop(struct net *net, struct nexthop *nh,
20 struct nl_info *nlinfo);
21
22 #define NH_DEV_HASHBITS 8
23 #define NH_DEV_HASHSIZE (1U << NH_DEV_HASHBITS)
24
25 static const struct nla_policy rtm_nh_policy[NHA_MAX + 1] = {
26 [NHA_ID] = { .type = NLA_U32 },
27 [NHA_GROUP] = { .type = NLA_BINARY },
28 [NHA_GROUP_TYPE] = { .type = NLA_U16 },
29 [NHA_BLACKHOLE] = { .type = NLA_FLAG },
30 [NHA_OIF] = { .type = NLA_U32 },
31 [NHA_GATEWAY] = { .type = NLA_BINARY },
32 [NHA_ENCAP_TYPE] = { .type = NLA_U16 },
33 [NHA_ENCAP] = { .type = NLA_NESTED },
34 [NHA_GROUPS] = { .type = NLA_FLAG },
35 [NHA_MASTER] = { .type = NLA_U32 },
36 [NHA_FDB] = { .type = NLA_FLAG },
37 };
38
call_nexthop_notifiers(struct net * net,enum nexthop_event_type event_type,struct nexthop * nh)39 static 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
nh_dev_hashfn(unsigned int val)50 static 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
nexthop_devhash_add(struct net * net,struct nh_info * nhi)59 static 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
nexthop_free_mpath(struct nexthop * nh)72 static 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);
78 for (i = 0; i < nhg->num_nh; ++i) {
79 struct nh_grp_entry *nhge = &nhg->nh_entries[i];
80
81 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);
88 kfree(nhg);
89 }
90
nexthop_free_single(struct nexthop * nh)91 static 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
nexthop_free_rcu(struct rcu_head * head)107 void 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 }
118 EXPORT_SYMBOL_GPL(nexthop_free_rcu);
119
nexthop_alloc(void)120 static 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);
129 INIT_LIST_HEAD(&nh->fdb_list);
130 }
131 return nh;
132 }
133
nexthop_grp_alloc(u16 num_nh)134 static struct nh_group *nexthop_grp_alloc(u16 num_nh)
135 {
136 struct nh_group *nhg;
137
138 nhg = kzalloc(struct_size(nhg, nh_entries, num_nh), GFP_KERNEL);
139 if (nhg)
140 nhg->num_nh = num_nh;
141
142 return nhg;
143 }
144
nh_base_seq_inc(struct net * net)145 static 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 */
nexthop_find_by_id(struct net * net,u32 id)152 struct 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 }
175 EXPORT_SYMBOL_GPL(nexthop_find_by_id);
176
177 /* used for auto id allocation; called with rtnl held */
nh_find_unused_id(struct net * net)178 static 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
nla_put_nh_group(struct sk_buff * skb,struct nh_group * nhg)193 static 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
220 nla_put_failure:
221 return -EMSGSIZE;
222 }
223
nh_fill_node(struct sk_buff * skb,struct nexthop * nh,int event,u32 portid,u32 seq,unsigned int nlflags)224 static 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
250 if (nhg->fdb_nh && nla_put_flag(skb, NHA_FDB))
251 goto nla_put_failure;
252 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;
263 } else if (nhi->fdb_nh) {
264 if (nla_put_flag(skb, NHA_FDB))
265 goto nla_put_failure;
266 } 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 &&
279 nla_put_be32(skb, NHA_GATEWAY, fib_nh->fib_nh_gw4))
280 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
296 out:
297 nlmsg_end(skb, nlh);
298 return 0;
299
300 nla_put_failure:
301 nlmsg_cancel(skb, nlh);
302 return -EMSGSIZE;
303 }
304
nh_nlmsg_size_grp(struct nexthop * nh)305 static 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
nh_nlmsg_size_single(struct nexthop * nh)314 static 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
nh_nlmsg_size(struct nexthop * nh)345 static size_t nh_nlmsg_size(struct nexthop *nh)
346 {
347 size_t sz = NLMSG_ALIGN(sizeof(struct nhmsg));
348
349 sz += nla_total_size(4); /* NHA_ID */
350
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
nexthop_notify(int event,struct nexthop * nh,struct nl_info * info)359 static 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;
381 errout:
382 if (err < 0)
383 rtnl_set_sk_err(info->nl_net, RTNLGRP_NEXTHOP, err);
384 }
385
valid_group_nh(struct nexthop * nh,unsigned int npaths,bool * is_fdb,struct netlink_ext_ack * extack)386 static bool valid_group_nh(struct nexthop *nh, unsigned int npaths,
387 bool *is_fdb, struct netlink_ext_ack *extack)
388 {
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 }
400 *is_fdb = nhg->fdb_nh;
401 } 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 }
409 *is_fdb = nhi->fdb_nh;
410 }
411
412 return true;
413 }
414
nh_check_attr_fdb_group(struct nexthop * nh,u8 * nh_family,struct netlink_ext_ack * extack)415 static 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
nh_check_attr_group(struct net * net,struct nlattr * tb[],struct netlink_ext_ack * extack)437 static 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]);
441 u8 nh_family = AF_UNSPEC;
442 struct nexthop_grp *nhg;
443 unsigned int i, j;
444 u8 nhg_fdb = 0;
445
446 if (!len || len & (sizeof(struct nexthop_grp) - 1)) {
447 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
473 if (tb[NHA_FDB])
474 nhg_fdb = 1;
475 nhg = nla_data(tb[NHA_GROUP]);
476 for (i = 0; i < len; ++i) {
477 struct nexthop *nh;
478 bool is_fdb_nh;
479
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 }
485 if (!valid_group_nh(nh, len, &is_fdb_nh, extack))
486 return -EINVAL;
487
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 }
495 }
496 for (i = NHA_GROUP_TYPE + 1; i < __NHA_MAX; ++i) {
497 if (!tb[i])
498 continue;
499 if (i == NHA_FDB)
500 continue;
501 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
ipv6_good_nh(const struct fib6_nh * nh)509 static 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
ipv4_good_nh(const struct fib_nh * nh)525 static 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
nexthop_select_path(struct nexthop * nh,int hash)542 struct 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
559 nhi = rcu_dereference(nhge->nh->nh_info);
560 if (nhi->fdb_nh)
561 return nhge->nh;
562
563 /* nexthops always check if it is good and does
564 * not rely on a sysctl for this behavior
565 */
566 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 }
583 EXPORT_SYMBOL_GPL(nexthop_select_path);
584
nexthop_for_each_fib6_nh(struct nexthop * nh,int (* cb)(struct fib6_nh * nh,void * arg),void * arg)585 int 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 }
614 EXPORT_SYMBOL_GPL(nexthop_for_each_fib6_nh);
615
check_src_addr(const struct in6_addr * saddr,struct netlink_ext_ack * extack)616 static 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
fib6_check_nexthop(struct nexthop * nh,struct fib6_config * cfg,struct netlink_ext_ack * extack)626 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
627 struct netlink_ext_ack *extack)
628 {
629 struct nh_info *nhi;
630 bool is_fdb_nh;
631
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;
647 is_fdb_nh = nhg->fdb_nh;
648 } else {
649 nhi = rtnl_dereference(nh->nh_info);
650 if (nhi->family == AF_INET)
651 goto no_v4_nh;
652 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;
658 }
659
660 return 0;
661 no_v4_nh:
662 NL_SET_ERR_MSG(extack, "IPv6 routes can not use an IPv4 nexthop");
663 return -EINVAL;
664 }
665 EXPORT_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 */
fib6_check_nh_list(struct nexthop * old,struct nexthop * new,struct netlink_ext_ack * extack)670 static 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
nexthop_check_scope(struct nh_info * nhi,u8 scope,struct netlink_ext_ack * extack)686 static int nexthop_check_scope(struct nh_info *nhi, u8 scope,
687 struct netlink_ext_ack *extack)
688 {
689 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 */
fib_check_nexthop(struct nexthop * nh,u8 scope,struct netlink_ext_ack * extack)707 int fib_check_nexthop(struct nexthop *nh, u8 scope,
708 struct netlink_ext_ack *extack)
709 {
710 struct nh_info *nhi;
711 int err = 0;
712
713 if (nh->is_group) {
714 struct nh_group *nhg;
715
716 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
723 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
729 /* all nexthops in a group have the same scope */
730 nhi = rtnl_dereference(nhg->nh_entries[0].nh->nh_info);
731 err = nexthop_check_scope(nhi, scope, extack);
732 } else {
733 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);
740 }
741
742 out:
743 return err;
744 }
745
fib_check_nh_list(struct nexthop * old,struct nexthop * new,struct netlink_ext_ack * extack)746 static 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
nh_group_rebalance(struct nh_group * nhg)761 static 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
remove_nh_grp_entry(struct net * net,struct nh_grp_entry * nhge,struct nl_info * nlinfo)780 static void remove_nh_grp_entry(struct net *net, struct nh_grp_entry *nhge,
781 struct nl_info *nlinfo)
782 {
783 struct nh_grp_entry *nhges, *new_nhges;
784 struct nexthop *nhp = nhge->nh_parent;
785 struct nexthop *nh = nhge->nh;
786 struct nh_group *nhg, *newg;
787 int i, j;
788
789 WARN_ON(!nh);
790
791 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;
798 }
799
800 newg->has_v4 = false;
801 newg->mpath = nhg->mpath;
802 newg->fdb_nh = nhg->fdb_nh;
803 newg->num_nh = nhg->num_nh;
804
805 /* 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) {
809 struct nh_info *nhi;
810
811 /* current nexthop getting removed */
812 if (nhg->nh_entries[i].nh == nh) {
813 newg->num_nh--;
814 continue;
815 }
816
817 nhi = rtnl_dereference(nhges[i].nh->nh_info);
818 if (nhi->family == AF_INET)
819 newg->has_v4 = true;
820
821 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 }
828
829 nh_group_rebalance(newg);
830 rcu_assign_pointer(nhp->nh_grp, newg);
831
832 list_del(&nhge->nh_list);
833 nexthop_put(nhge->nh);
834
835 if (nlinfo)
836 nexthop_notify(RTM_NEWNEXTHOP, nhp, nlinfo);
837 }
838
remove_nexthop_from_groups(struct net * net,struct nexthop * nh,struct nl_info * nlinfo)839 static 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
844 list_for_each_entry_safe(nhge, tmp, &nh->grp_list, nh_list)
845 remove_nh_grp_entry(net, nhge, nlinfo);
846
847 /* make sure all see the newly published array before releasing rtnl */
848 synchronize_net();
849 }
850
remove_nexthop_group(struct nexthop * nh,struct nl_info * nlinfo)851 static 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
862 list_del_init(&nhge->nh_list);
863 }
864 }
865
866 /* not called for nexthop replace */
__remove_nexthop_fib(struct net * net,struct nexthop * nh)867 static 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);
884 ipv6_stub->ip6_del_rt(net, f6i,
885 !net->ipv4.sysctl_nexthop_compat_mode);
886 }
887 }
888
__remove_nexthop(struct net * net,struct nexthop * nh,struct nl_info * nlinfo)889 static 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
remove_nexthop(struct net * net,struct nexthop * nh,struct nl_info * nlinfo)907 static void remove_nexthop(struct net *net, struct nexthop *nh,
908 struct nl_info *nlinfo)
909 {
910 call_nexthop_notifiers(net, NEXTHOP_EVENT_DEL, nh);
911
912 /* 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 */
nh_rt_cache_flush(struct net * net,struct nexthop * nh)927 static void nh_rt_cache_flush(struct net *net, struct nexthop *nh)
928 {
929 struct fib6_info *f6i;
930
931 if (!list_empty(&nh->fi_list))
932 rt_cache_flush(net);
933
934 list_for_each_entry(f6i, &nh->f6i_list, nh_list)
935 ipv6_stub->fib6_update_sernum(net, f6i);
936 }
937
replace_nexthop_grp(struct net * net,struct nexthop * old,struct nexthop * new,struct netlink_ext_ack * extack)938 static int replace_nexthop_grp(struct net *net, struct nexthop *old,
939 struct nexthop *new,
940 struct netlink_ext_ack *extack)
941 {
942 struct nh_group *oldg, *newg;
943 int i;
944
945 if (!new->is_group) {
946 NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with a nexthop.");
947 return -EINVAL;
948 }
949
950 oldg = rtnl_dereference(old->nh_grp);
951 newg = rtnl_dereference(new->nh_grp);
952
953 /* update parents - used by nexthop code for cleanup */
954 for (i = 0; i < newg->num_nh; i++)
955 newg->nh_entries[i].nh_parent = old;
956
957 rcu_assign_pointer(old->nh_grp, newg);
958
959 for (i = 0; i < oldg->num_nh; i++)
960 oldg->nh_entries[i].nh_parent = new;
961
962 rcu_assign_pointer(new->nh_grp, oldg);
963
964 return 0;
965 }
966
nh_group_v4_update(struct nh_group * nhg)967 static void nh_group_v4_update(struct nh_group *nhg)
968 {
969 struct nh_grp_entry *nhges;
970 bool has_v4 = false;
971 int i;
972
973 nhges = nhg->nh_entries;
974 for (i = 0; i < nhg->num_nh; i++) {
975 struct nh_info *nhi;
976
977 nhi = rtnl_dereference(nhges[i].nh->nh_info);
978 if (nhi->family == AF_INET)
979 has_v4 = true;
980 }
981 nhg->has_v4 = has_v4;
982 }
983
replace_nexthop_single(struct net * net,struct nexthop * old,struct nexthop * new,struct netlink_ext_ack * extack)984 static int replace_nexthop_single(struct net *net, struct nexthop *old,
985 struct nexthop *new,
986 struct netlink_ext_ack *extack)
987 {
988 struct nh_info *oldi, *newi;
989
990 if (new->is_group) {
991 NL_SET_ERR_MSG(extack, "Can not replace a nexthop with a nexthop group.");
992 return -EINVAL;
993 }
994
995 oldi = rtnl_dereference(old->nh_info);
996 newi = rtnl_dereference(new->nh_info);
997
998 newi->nh_parent = old;
999 oldi->nh_parent = new;
1000
1001 old->protocol = new->protocol;
1002 old->nh_flags = new->nh_flags;
1003
1004 rcu_assign_pointer(old->nh_info, newi);
1005 rcu_assign_pointer(new->nh_info, oldi);
1006
1007 /* When replacing an IPv4 nexthop with an IPv6 nexthop, potentially
1008 * update IPv4 indication in all the groups using the nexthop.
1009 */
1010 if (oldi->family == AF_INET && newi->family == AF_INET6) {
1011 struct nh_grp_entry *nhge;
1012
1013 list_for_each_entry(nhge, &old->grp_list, nh_list) {
1014 struct nexthop *nhp = nhge->nh_parent;
1015 struct nh_group *nhg;
1016
1017 nhg = rtnl_dereference(nhp->nh_grp);
1018 nh_group_v4_update(nhg);
1019 }
1020 }
1021
1022 return 0;
1023 }
1024
__nexthop_replace_notify(struct net * net,struct nexthop * nh,struct nl_info * info)1025 static void __nexthop_replace_notify(struct net *net, struct nexthop *nh,
1026 struct nl_info *info)
1027 {
1028 struct fib6_info *f6i;
1029
1030 if (!list_empty(&nh->fi_list)) {
1031 struct fib_info *fi;
1032
1033 /* expectation is a few fib_info per nexthop and then
1034 * a lot of routes per fib_info. So mark the fib_info
1035 * and then walk the fib tables once
1036 */
1037 list_for_each_entry(fi, &nh->fi_list, nh_list)
1038 fi->nh_updated = true;
1039
1040 fib_info_notify_update(net, info);
1041
1042 list_for_each_entry(fi, &nh->fi_list, nh_list)
1043 fi->nh_updated = false;
1044 }
1045
1046 list_for_each_entry(f6i, &nh->f6i_list, nh_list)
1047 ipv6_stub->fib6_rt_update(net, f6i, info);
1048 }
1049
1050 /* send RTM_NEWROUTE with REPLACE flag set for all FIB entries
1051 * linked to this nexthop and for all groups that the nexthop
1052 * is a member of
1053 */
nexthop_replace_notify(struct net * net,struct nexthop * nh,struct nl_info * info)1054 static void nexthop_replace_notify(struct net *net, struct nexthop *nh,
1055 struct nl_info *info)
1056 {
1057 struct nh_grp_entry *nhge;
1058
1059 __nexthop_replace_notify(net, nh, info);
1060
1061 list_for_each_entry(nhge, &nh->grp_list, nh_list)
1062 __nexthop_replace_notify(net, nhge->nh_parent, info);
1063 }
1064
replace_nexthop(struct net * net,struct nexthop * old,struct nexthop * new,struct netlink_ext_ack * extack)1065 static int replace_nexthop(struct net *net, struct nexthop *old,
1066 struct nexthop *new, struct netlink_ext_ack *extack)
1067 {
1068 bool new_is_reject = false;
1069 struct nh_grp_entry *nhge;
1070 int err;
1071
1072 /* check that existing FIB entries are ok with the
1073 * new nexthop definition
1074 */
1075 err = fib_check_nh_list(old, new, extack);
1076 if (err)
1077 return err;
1078
1079 err = fib6_check_nh_list(old, new, extack);
1080 if (err)
1081 return err;
1082
1083 if (!new->is_group) {
1084 struct nh_info *nhi = rtnl_dereference(new->nh_info);
1085
1086 new_is_reject = nhi->reject_nh;
1087 }
1088
1089 list_for_each_entry(nhge, &old->grp_list, nh_list) {
1090 /* if new nexthop is a blackhole, any groups using this
1091 * nexthop cannot have more than 1 path
1092 */
1093 if (new_is_reject &&
1094 nexthop_num_path(nhge->nh_parent) > 1) {
1095 NL_SET_ERR_MSG(extack, "Blackhole nexthop can not be a member of a group with more than one path");
1096 return -EINVAL;
1097 }
1098
1099 err = fib_check_nh_list(nhge->nh_parent, new, extack);
1100 if (err)
1101 return err;
1102
1103 err = fib6_check_nh_list(nhge->nh_parent, new, extack);
1104 if (err)
1105 return err;
1106 }
1107
1108 if (old->is_group)
1109 err = replace_nexthop_grp(net, old, new, extack);
1110 else
1111 err = replace_nexthop_single(net, old, new, extack);
1112
1113 if (!err) {
1114 nh_rt_cache_flush(net, old);
1115
1116 __remove_nexthop(net, new, NULL);
1117 nexthop_put(new);
1118 }
1119
1120 return err;
1121 }
1122
1123 /* called with rtnl_lock held */
insert_nexthop(struct net * net,struct nexthop * new_nh,struct nh_config * cfg,struct netlink_ext_ack * extack)1124 static int insert_nexthop(struct net *net, struct nexthop *new_nh,
1125 struct nh_config *cfg, struct netlink_ext_ack *extack)
1126 {
1127 struct rb_node **pp, *parent = NULL, *next;
1128 struct rb_root *root = &net->nexthop.rb_root;
1129 bool replace = !!(cfg->nlflags & NLM_F_REPLACE);
1130 bool create = !!(cfg->nlflags & NLM_F_CREATE);
1131 u32 new_id = new_nh->id;
1132 int replace_notify = 0;
1133 int rc = -EEXIST;
1134
1135 pp = &root->rb_node;
1136 while (1) {
1137 struct nexthop *nh;
1138
1139 next = *pp;
1140 if (!next)
1141 break;
1142
1143 parent = next;
1144
1145 nh = rb_entry(parent, struct nexthop, rb_node);
1146 if (new_id < nh->id) {
1147 pp = &next->rb_left;
1148 } else if (new_id > nh->id) {
1149 pp = &next->rb_right;
1150 } else if (replace) {
1151 rc = replace_nexthop(net, nh, new_nh, extack);
1152 if (!rc) {
1153 new_nh = nh; /* send notification with old nh */
1154 replace_notify = 1;
1155 }
1156 goto out;
1157 } else {
1158 /* id already exists and not a replace */
1159 goto out;
1160 }
1161 }
1162
1163 if (replace && !create) {
1164 NL_SET_ERR_MSG(extack, "Replace specified without create and no entry exists");
1165 rc = -ENOENT;
1166 goto out;
1167 }
1168
1169 rb_link_node_rcu(&new_nh->rb_node, parent, pp);
1170 rb_insert_color(&new_nh->rb_node, root);
1171 rc = 0;
1172 out:
1173 if (!rc) {
1174 nh_base_seq_inc(net);
1175 nexthop_notify(RTM_NEWNEXTHOP, new_nh, &cfg->nlinfo);
1176 if (replace_notify && net->ipv4.sysctl_nexthop_compat_mode)
1177 nexthop_replace_notify(net, new_nh, &cfg->nlinfo);
1178 }
1179
1180 return rc;
1181 }
1182
1183 /* rtnl */
1184 /* remove all nexthops tied to a device being deleted */
nexthop_flush_dev(struct net_device * dev,unsigned long event)1185 static void nexthop_flush_dev(struct net_device *dev, unsigned long event)
1186 {
1187 unsigned int hash = nh_dev_hashfn(dev->ifindex);
1188 struct net *net = dev_net(dev);
1189 struct hlist_head *head = &net->nexthop.devhash[hash];
1190 struct hlist_node *n;
1191 struct nh_info *nhi;
1192
1193 hlist_for_each_entry_safe(nhi, n, head, dev_hash) {
1194 if (nhi->fib_nhc.nhc_dev != dev)
1195 continue;
1196
1197 if (nhi->reject_nh &&
1198 (event == NETDEV_DOWN || event == NETDEV_CHANGE))
1199 continue;
1200
1201 remove_nexthop(net, nhi->nh_parent, NULL);
1202 }
1203 }
1204
1205 /* rtnl; called when net namespace is deleted */
flush_all_nexthops(struct net * net)1206 static void flush_all_nexthops(struct net *net)
1207 {
1208 struct rb_root *root = &net->nexthop.rb_root;
1209 struct rb_node *node;
1210 struct nexthop *nh;
1211
1212 while ((node = rb_first(root))) {
1213 nh = rb_entry(node, struct nexthop, rb_node);
1214 remove_nexthop(net, nh, NULL);
1215 cond_resched();
1216 }
1217 }
1218
nexthop_create_group(struct net * net,struct nh_config * cfg)1219 static struct nexthop *nexthop_create_group(struct net *net,
1220 struct nh_config *cfg)
1221 {
1222 struct nlattr *grps_attr = cfg->nh_grp;
1223 struct nexthop_grp *entry = nla_data(grps_attr);
1224 u16 num_nh = nla_len(grps_attr) / sizeof(*entry);
1225 struct nh_group *nhg;
1226 struct nexthop *nh;
1227 int i;
1228
1229 if (WARN_ON(!num_nh))
1230 return ERR_PTR(-EINVAL);
1231
1232 nh = nexthop_alloc();
1233 if (!nh)
1234 return ERR_PTR(-ENOMEM);
1235
1236 nh->is_group = 1;
1237
1238 nhg = nexthop_grp_alloc(num_nh);
1239 if (!nhg) {
1240 kfree(nh);
1241 return ERR_PTR(-ENOMEM);
1242 }
1243
1244 /* spare group used for removals */
1245 nhg->spare = nexthop_grp_alloc(num_nh);
1246 if (!nhg->spare) {
1247 kfree(nhg);
1248 kfree(nh);
1249 return ERR_PTR(-ENOMEM);
1250 }
1251 nhg->spare->spare = nhg;
1252
1253 for (i = 0; i < nhg->num_nh; ++i) {
1254 struct nexthop *nhe;
1255 struct nh_info *nhi;
1256
1257 nhe = nexthop_find_by_id(net, entry[i].id);
1258 if (!nexthop_get(nhe))
1259 goto out_no_nh;
1260
1261 nhi = rtnl_dereference(nhe->nh_info);
1262 if (nhi->family == AF_INET)
1263 nhg->has_v4 = true;
1264
1265 nhg->nh_entries[i].nh = nhe;
1266 nhg->nh_entries[i].weight = entry[i].weight + 1;
1267 list_add(&nhg->nh_entries[i].nh_list, &nhe->grp_list);
1268 nhg->nh_entries[i].nh_parent = nh;
1269 }
1270
1271 if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_MPATH) {
1272 nhg->mpath = 1;
1273 nh_group_rebalance(nhg);
1274 }
1275
1276 if (cfg->nh_fdb)
1277 nhg->fdb_nh = 1;
1278
1279 rcu_assign_pointer(nh->nh_grp, nhg);
1280
1281 return nh;
1282
1283 out_no_nh:
1284 for (i--; i >= 0; --i) {
1285 list_del(&nhg->nh_entries[i].nh_list);
1286 nexthop_put(nhg->nh_entries[i].nh);
1287 }
1288
1289 kfree(nhg->spare);
1290 kfree(nhg);
1291 kfree(nh);
1292
1293 return ERR_PTR(-ENOENT);
1294 }
1295
nh_create_ipv4(struct net * net,struct nexthop * nh,struct nh_info * nhi,struct nh_config * cfg,struct netlink_ext_ack * extack)1296 static int nh_create_ipv4(struct net *net, struct nexthop *nh,
1297 struct nh_info *nhi, struct nh_config *cfg,
1298 struct netlink_ext_ack *extack)
1299 {
1300 struct fib_nh *fib_nh = &nhi->fib_nh;
1301 struct fib_config fib_cfg = {
1302 .fc_oif = cfg->nh_ifindex,
1303 .fc_gw4 = cfg->gw.ipv4,
1304 .fc_gw_family = cfg->gw.ipv4 ? AF_INET : 0,
1305 .fc_flags = cfg->nh_flags,
1306 .fc_nlinfo = cfg->nlinfo,
1307 .fc_encap = cfg->nh_encap,
1308 .fc_encap_type = cfg->nh_encap_type,
1309 };
1310 u32 tb_id = (cfg->dev ? l3mdev_fib_table(cfg->dev) : RT_TABLE_MAIN);
1311 int err;
1312
1313 err = fib_nh_init(net, fib_nh, &fib_cfg, 1, extack);
1314 if (err) {
1315 fib_nh_release(net, fib_nh);
1316 goto out;
1317 }
1318
1319 if (nhi->fdb_nh)
1320 goto out;
1321
1322 /* sets nh_dev if successful */
1323 err = fib_check_nh(net, fib_nh, tb_id, 0, extack);
1324 if (!err) {
1325 nh->nh_flags = fib_nh->fib_nh_flags;
1326 fib_info_update_nhc_saddr(net, &fib_nh->nh_common,
1327 fib_nh->fib_nh_scope);
1328 } else {
1329 fib_nh_release(net, fib_nh);
1330 }
1331 out:
1332 return err;
1333 }
1334
nh_create_ipv6(struct net * net,struct nexthop * nh,struct nh_info * nhi,struct nh_config * cfg,struct netlink_ext_ack * extack)1335 static int nh_create_ipv6(struct net *net, struct nexthop *nh,
1336 struct nh_info *nhi, struct nh_config *cfg,
1337 struct netlink_ext_ack *extack)
1338 {
1339 struct fib6_nh *fib6_nh = &nhi->fib6_nh;
1340 struct fib6_config fib6_cfg = {
1341 .fc_table = l3mdev_fib_table(cfg->dev),
1342 .fc_ifindex = cfg->nh_ifindex,
1343 .fc_gateway = cfg->gw.ipv6,
1344 .fc_flags = cfg->nh_flags,
1345 .fc_nlinfo = cfg->nlinfo,
1346 .fc_encap = cfg->nh_encap,
1347 .fc_encap_type = cfg->nh_encap_type,
1348 .fc_is_fdb = cfg->nh_fdb,
1349 };
1350 int err;
1351
1352 if (!ipv6_addr_any(&cfg->gw.ipv6))
1353 fib6_cfg.fc_flags |= RTF_GATEWAY;
1354
1355 /* sets nh_dev if successful */
1356 err = ipv6_stub->fib6_nh_init(net, fib6_nh, &fib6_cfg, GFP_KERNEL,
1357 extack);
1358 if (err)
1359 ipv6_stub->fib6_nh_release(fib6_nh);
1360 else
1361 nh->nh_flags = fib6_nh->fib_nh_flags;
1362
1363 return err;
1364 }
1365
nexthop_create(struct net * net,struct nh_config * cfg,struct netlink_ext_ack * extack)1366 static struct nexthop *nexthop_create(struct net *net, struct nh_config *cfg,
1367 struct netlink_ext_ack *extack)
1368 {
1369 struct nh_info *nhi;
1370 struct nexthop *nh;
1371 int err = 0;
1372
1373 nh = nexthop_alloc();
1374 if (!nh)
1375 return ERR_PTR(-ENOMEM);
1376
1377 nhi = kzalloc(sizeof(*nhi), GFP_KERNEL);
1378 if (!nhi) {
1379 kfree(nh);
1380 return ERR_PTR(-ENOMEM);
1381 }
1382
1383 nh->nh_flags = cfg->nh_flags;
1384 nh->net = net;
1385
1386 nhi->nh_parent = nh;
1387 nhi->family = cfg->nh_family;
1388 nhi->fib_nhc.nhc_scope = RT_SCOPE_LINK;
1389
1390 if (cfg->nh_fdb)
1391 nhi->fdb_nh = 1;
1392
1393 if (cfg->nh_blackhole) {
1394 nhi->reject_nh = 1;
1395 cfg->nh_ifindex = net->loopback_dev->ifindex;
1396 }
1397
1398 switch (cfg->nh_family) {
1399 case AF_INET:
1400 err = nh_create_ipv4(net, nh, nhi, cfg, extack);
1401 break;
1402 case AF_INET6:
1403 err = nh_create_ipv6(net, nh, nhi, cfg, extack);
1404 break;
1405 }
1406
1407 if (err) {
1408 kfree(nhi);
1409 kfree(nh);
1410 return ERR_PTR(err);
1411 }
1412
1413 /* add the entry to the device based hash */
1414 if (!nhi->fdb_nh)
1415 nexthop_devhash_add(net, nhi);
1416
1417 rcu_assign_pointer(nh->nh_info, nhi);
1418
1419 return nh;
1420 }
1421
1422 /* called with rtnl lock held */
nexthop_add(struct net * net,struct nh_config * cfg,struct netlink_ext_ack * extack)1423 static struct nexthop *nexthop_add(struct net *net, struct nh_config *cfg,
1424 struct netlink_ext_ack *extack)
1425 {
1426 struct nexthop *nh;
1427 int err;
1428
1429 if (cfg->nlflags & NLM_F_REPLACE && !cfg->nh_id) {
1430 NL_SET_ERR_MSG(extack, "Replace requires nexthop id");
1431 return ERR_PTR(-EINVAL);
1432 }
1433
1434 if (!cfg->nh_id) {
1435 cfg->nh_id = nh_find_unused_id(net);
1436 if (!cfg->nh_id) {
1437 NL_SET_ERR_MSG(extack, "No unused id");
1438 return ERR_PTR(-EINVAL);
1439 }
1440 }
1441
1442 if (cfg->nh_grp)
1443 nh = nexthop_create_group(net, cfg);
1444 else
1445 nh = nexthop_create(net, cfg, extack);
1446
1447 if (IS_ERR(nh))
1448 return nh;
1449
1450 refcount_set(&nh->refcnt, 1);
1451 nh->id = cfg->nh_id;
1452 nh->protocol = cfg->nh_protocol;
1453 nh->net = net;
1454
1455 err = insert_nexthop(net, nh, cfg, extack);
1456 if (err) {
1457 __remove_nexthop(net, nh, NULL);
1458 nexthop_put(nh);
1459 nh = ERR_PTR(err);
1460 }
1461
1462 return nh;
1463 }
1464
rtm_to_nh_config(struct net * net,struct sk_buff * skb,struct nlmsghdr * nlh,struct nh_config * cfg,struct netlink_ext_ack * extack)1465 static int rtm_to_nh_config(struct net *net, struct sk_buff *skb,
1466 struct nlmsghdr *nlh, struct nh_config *cfg,
1467 struct netlink_ext_ack *extack)
1468 {
1469 struct nhmsg *nhm = nlmsg_data(nlh);
1470 struct nlattr *tb[NHA_MAX + 1];
1471 int err;
1472
1473 err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy,
1474 extack);
1475 if (err < 0)
1476 return err;
1477
1478 err = -EINVAL;
1479 if (nhm->resvd || nhm->nh_scope) {
1480 NL_SET_ERR_MSG(extack, "Invalid values in ancillary header");
1481 goto out;
1482 }
1483 if (nhm->nh_flags & ~NEXTHOP_VALID_USER_FLAGS) {
1484 NL_SET_ERR_MSG(extack, "Invalid nexthop flags in ancillary header");
1485 goto out;
1486 }
1487
1488 switch (nhm->nh_family) {
1489 case AF_INET:
1490 case AF_INET6:
1491 break;
1492 case AF_UNSPEC:
1493 if (tb[NHA_GROUP])
1494 break;
1495 fallthrough;
1496 default:
1497 NL_SET_ERR_MSG(extack, "Invalid address family");
1498 goto out;
1499 }
1500
1501 if (tb[NHA_GROUPS] || tb[NHA_MASTER]) {
1502 NL_SET_ERR_MSG(extack, "Invalid attributes in request");
1503 goto out;
1504 }
1505
1506 memset(cfg, 0, sizeof(*cfg));
1507 cfg->nlflags = nlh->nlmsg_flags;
1508 cfg->nlinfo.portid = NETLINK_CB(skb).portid;
1509 cfg->nlinfo.nlh = nlh;
1510 cfg->nlinfo.nl_net = net;
1511
1512 cfg->nh_family = nhm->nh_family;
1513 cfg->nh_protocol = nhm->nh_protocol;
1514 cfg->nh_flags = nhm->nh_flags;
1515
1516 if (tb[NHA_ID])
1517 cfg->nh_id = nla_get_u32(tb[NHA_ID]);
1518
1519 if (tb[NHA_FDB]) {
1520 if (tb[NHA_OIF] || tb[NHA_BLACKHOLE] ||
1521 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE]) {
1522 NL_SET_ERR_MSG(extack, "Fdb attribute can not be used with encap, oif or blackhole");
1523 goto out;
1524 }
1525 if (nhm->nh_flags) {
1526 NL_SET_ERR_MSG(extack, "Unsupported nexthop flags in ancillary header");
1527 goto out;
1528 }
1529 cfg->nh_fdb = nla_get_flag(tb[NHA_FDB]);
1530 }
1531
1532 if (tb[NHA_GROUP]) {
1533 if (nhm->nh_family != AF_UNSPEC) {
1534 NL_SET_ERR_MSG(extack, "Invalid family for group");
1535 goto out;
1536 }
1537 cfg->nh_grp = tb[NHA_GROUP];
1538
1539 cfg->nh_grp_type = NEXTHOP_GRP_TYPE_MPATH;
1540 if (tb[NHA_GROUP_TYPE])
1541 cfg->nh_grp_type = nla_get_u16(tb[NHA_GROUP_TYPE]);
1542
1543 if (cfg->nh_grp_type > NEXTHOP_GRP_TYPE_MAX) {
1544 NL_SET_ERR_MSG(extack, "Invalid group type");
1545 goto out;
1546 }
1547 err = nh_check_attr_group(net, tb, extack);
1548
1549 /* no other attributes should be set */
1550 goto out;
1551 }
1552
1553 if (tb[NHA_BLACKHOLE]) {
1554 if (tb[NHA_GATEWAY] || tb[NHA_OIF] ||
1555 tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE] || tb[NHA_FDB]) {
1556 NL_SET_ERR_MSG(extack, "Blackhole attribute can not be used with gateway, oif, encap or fdb");
1557 goto out;
1558 }
1559
1560 cfg->nh_blackhole = 1;
1561 err = 0;
1562 goto out;
1563 }
1564
1565 if (!cfg->nh_fdb && !tb[NHA_OIF]) {
1566 NL_SET_ERR_MSG(extack, "Device attribute required for non-blackhole and non-fdb nexthops");
1567 goto out;
1568 }
1569
1570 if (!cfg->nh_fdb && tb[NHA_OIF]) {
1571 cfg->nh_ifindex = nla_get_u32(tb[NHA_OIF]);
1572 if (cfg->nh_ifindex)
1573 cfg->dev = __dev_get_by_index(net, cfg->nh_ifindex);
1574
1575 if (!cfg->dev) {
1576 NL_SET_ERR_MSG(extack, "Invalid device index");
1577 goto out;
1578 } else if (!(cfg->dev->flags & IFF_UP)) {
1579 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
1580 err = -ENETDOWN;
1581 goto out;
1582 } else if (!netif_carrier_ok(cfg->dev)) {
1583 NL_SET_ERR_MSG(extack, "Carrier for nexthop device is down");
1584 err = -ENETDOWN;
1585 goto out;
1586 }
1587 }
1588
1589 err = -EINVAL;
1590 if (tb[NHA_GATEWAY]) {
1591 struct nlattr *gwa = tb[NHA_GATEWAY];
1592
1593 switch (cfg->nh_family) {
1594 case AF_INET:
1595 if (nla_len(gwa) != sizeof(u32)) {
1596 NL_SET_ERR_MSG(extack, "Invalid gateway");
1597 goto out;
1598 }
1599 cfg->gw.ipv4 = nla_get_be32(gwa);
1600 break;
1601 case AF_INET6:
1602 if (nla_len(gwa) != sizeof(struct in6_addr)) {
1603 NL_SET_ERR_MSG(extack, "Invalid gateway");
1604 goto out;
1605 }
1606 cfg->gw.ipv6 = nla_get_in6_addr(gwa);
1607 break;
1608 default:
1609 NL_SET_ERR_MSG(extack,
1610 "Unknown address family for gateway");
1611 goto out;
1612 }
1613 } else {
1614 /* device only nexthop (no gateway) */
1615 if (cfg->nh_flags & RTNH_F_ONLINK) {
1616 NL_SET_ERR_MSG(extack,
1617 "ONLINK flag can not be set for nexthop without a gateway");
1618 goto out;
1619 }
1620 }
1621
1622 if (tb[NHA_ENCAP]) {
1623 cfg->nh_encap = tb[NHA_ENCAP];
1624
1625 if (!tb[NHA_ENCAP_TYPE]) {
1626 NL_SET_ERR_MSG(extack, "LWT encapsulation type is missing");
1627 goto out;
1628 }
1629
1630 cfg->nh_encap_type = nla_get_u16(tb[NHA_ENCAP_TYPE]);
1631 err = lwtunnel_valid_encap_type(cfg->nh_encap_type, extack);
1632 if (err < 0)
1633 goto out;
1634
1635 } else if (tb[NHA_ENCAP_TYPE]) {
1636 NL_SET_ERR_MSG(extack, "LWT encapsulation attribute is missing");
1637 goto out;
1638 }
1639
1640
1641 err = 0;
1642 out:
1643 return err;
1644 }
1645
1646 /* rtnl */
rtm_new_nexthop(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)1647 static int rtm_new_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
1648 struct netlink_ext_ack *extack)
1649 {
1650 struct net *net = sock_net(skb->sk);
1651 struct nh_config cfg;
1652 struct nexthop *nh;
1653 int err;
1654
1655 err = rtm_to_nh_config(net, skb, nlh, &cfg, extack);
1656 if (!err) {
1657 nh = nexthop_add(net, &cfg, extack);
1658 if (IS_ERR(nh))
1659 err = PTR_ERR(nh);
1660 }
1661
1662 return err;
1663 }
1664
nh_valid_get_del_req(struct nlmsghdr * nlh,u32 * id,struct netlink_ext_ack * extack)1665 static int nh_valid_get_del_req(struct nlmsghdr *nlh, u32 *id,
1666 struct netlink_ext_ack *extack)
1667 {
1668 struct nhmsg *nhm = nlmsg_data(nlh);
1669 struct nlattr *tb[NHA_MAX + 1];
1670 int err, i;
1671
1672 err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy,
1673 extack);
1674 if (err < 0)
1675 return err;
1676
1677 err = -EINVAL;
1678 for (i = 0; i < __NHA_MAX; ++i) {
1679 if (!tb[i])
1680 continue;
1681
1682 switch (i) {
1683 case NHA_ID:
1684 break;
1685 default:
1686 NL_SET_ERR_MSG_ATTR(extack, tb[i],
1687 "Unexpected attribute in request");
1688 goto out;
1689 }
1690 }
1691 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) {
1692 NL_SET_ERR_MSG(extack, "Invalid values in header");
1693 goto out;
1694 }
1695
1696 if (!tb[NHA_ID]) {
1697 NL_SET_ERR_MSG(extack, "Nexthop id is missing");
1698 goto out;
1699 }
1700
1701 *id = nla_get_u32(tb[NHA_ID]);
1702 if (!(*id))
1703 NL_SET_ERR_MSG(extack, "Invalid nexthop id");
1704 else
1705 err = 0;
1706 out:
1707 return err;
1708 }
1709
1710 /* rtnl */
rtm_del_nexthop(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)1711 static int rtm_del_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
1712 struct netlink_ext_ack *extack)
1713 {
1714 struct net *net = sock_net(skb->sk);
1715 struct nl_info nlinfo = {
1716 .nlh = nlh,
1717 .nl_net = net,
1718 .portid = NETLINK_CB(skb).portid,
1719 };
1720 struct nexthop *nh;
1721 int err;
1722 u32 id;
1723
1724 err = nh_valid_get_del_req(nlh, &id, extack);
1725 if (err)
1726 return err;
1727
1728 nh = nexthop_find_by_id(net, id);
1729 if (!nh)
1730 return -ENOENT;
1731
1732 remove_nexthop(net, nh, &nlinfo);
1733
1734 return 0;
1735 }
1736
1737 /* rtnl */
rtm_get_nexthop(struct sk_buff * in_skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)1738 static int rtm_get_nexthop(struct sk_buff *in_skb, struct nlmsghdr *nlh,
1739 struct netlink_ext_ack *extack)
1740 {
1741 struct net *net = sock_net(in_skb->sk);
1742 struct sk_buff *skb = NULL;
1743 struct nexthop *nh;
1744 int err;
1745 u32 id;
1746
1747 err = nh_valid_get_del_req(nlh, &id, extack);
1748 if (err)
1749 return err;
1750
1751 err = -ENOBUFS;
1752 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1753 if (!skb)
1754 goto out;
1755
1756 err = -ENOENT;
1757 nh = nexthop_find_by_id(net, id);
1758 if (!nh)
1759 goto errout_free;
1760
1761 err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, NETLINK_CB(in_skb).portid,
1762 nlh->nlmsg_seq, 0);
1763 if (err < 0) {
1764 WARN_ON(err == -EMSGSIZE);
1765 goto errout_free;
1766 }
1767
1768 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
1769 out:
1770 return err;
1771 errout_free:
1772 kfree_skb(skb);
1773 goto out;
1774 }
1775
nh_dump_filtered(struct nexthop * nh,int dev_idx,int master_idx,bool group_filter,u8 family)1776 static bool nh_dump_filtered(struct nexthop *nh, int dev_idx, int master_idx,
1777 bool group_filter, u8 family)
1778 {
1779 const struct net_device *dev;
1780 const struct nh_info *nhi;
1781
1782 if (group_filter && !nh->is_group)
1783 return true;
1784
1785 if (!dev_idx && !master_idx && !family)
1786 return false;
1787
1788 if (nh->is_group)
1789 return true;
1790
1791 nhi = rtnl_dereference(nh->nh_info);
1792 if (family && nhi->family != family)
1793 return true;
1794
1795 dev = nhi->fib_nhc.nhc_dev;
1796 if (dev_idx && (!dev || dev->ifindex != dev_idx))
1797 return true;
1798
1799 if (master_idx) {
1800 struct net_device *master;
1801
1802 if (!dev)
1803 return true;
1804
1805 master = netdev_master_upper_dev_get((struct net_device *)dev);
1806 if (!master || master->ifindex != master_idx)
1807 return true;
1808 }
1809
1810 return false;
1811 }
1812
nh_valid_dump_req(const struct nlmsghdr * nlh,int * dev_idx,int * master_idx,bool * group_filter,bool * fdb_filter,struct netlink_callback * cb)1813 static int nh_valid_dump_req(const struct nlmsghdr *nlh, int *dev_idx,
1814 int *master_idx, bool *group_filter,
1815 bool *fdb_filter, struct netlink_callback *cb)
1816 {
1817 struct netlink_ext_ack *extack = cb->extack;
1818 struct nlattr *tb[NHA_MAX + 1];
1819 struct nhmsg *nhm;
1820 int err, i;
1821 u32 idx;
1822
1823 err = nlmsg_parse(nlh, sizeof(*nhm), tb, NHA_MAX, rtm_nh_policy,
1824 NULL);
1825 if (err < 0)
1826 return err;
1827
1828 for (i = 0; i <= NHA_MAX; ++i) {
1829 if (!tb[i])
1830 continue;
1831
1832 switch (i) {
1833 case NHA_OIF:
1834 idx = nla_get_u32(tb[i]);
1835 if (idx > INT_MAX) {
1836 NL_SET_ERR_MSG(extack, "Invalid device index");
1837 return -EINVAL;
1838 }
1839 *dev_idx = idx;
1840 break;
1841 case NHA_MASTER:
1842 idx = nla_get_u32(tb[i]);
1843 if (idx > INT_MAX) {
1844 NL_SET_ERR_MSG(extack, "Invalid master device index");
1845 return -EINVAL;
1846 }
1847 *master_idx = idx;
1848 break;
1849 case NHA_GROUPS:
1850 *group_filter = true;
1851 break;
1852 case NHA_FDB:
1853 *fdb_filter = true;
1854 break;
1855 default:
1856 NL_SET_ERR_MSG(extack, "Unsupported attribute in dump request");
1857 return -EINVAL;
1858 }
1859 }
1860
1861 nhm = nlmsg_data(nlh);
1862 if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) {
1863 NL_SET_ERR_MSG(extack, "Invalid values in header for nexthop dump request");
1864 return -EINVAL;
1865 }
1866
1867 return 0;
1868 }
1869
1870 /* rtnl */
rtm_dump_nexthop(struct sk_buff * skb,struct netlink_callback * cb)1871 static int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb)
1872 {
1873 bool group_filter = false, fdb_filter = false;
1874 struct nhmsg *nhm = nlmsg_data(cb->nlh);
1875 int dev_filter_idx = 0, master_idx = 0;
1876 struct net *net = sock_net(skb->sk);
1877 struct rb_root *root = &net->nexthop.rb_root;
1878 struct rb_node *node;
1879 int idx = 0, s_idx;
1880 int err;
1881
1882 err = nh_valid_dump_req(cb->nlh, &dev_filter_idx, &master_idx,
1883 &group_filter, &fdb_filter, cb);
1884 if (err < 0)
1885 return err;
1886
1887 s_idx = cb->args[0];
1888 for (node = rb_first(root); node; node = rb_next(node)) {
1889 struct nexthop *nh;
1890
1891 if (idx < s_idx)
1892 goto cont;
1893
1894 nh = rb_entry(node, struct nexthop, rb_node);
1895 if (nh_dump_filtered(nh, dev_filter_idx, master_idx,
1896 group_filter, nhm->nh_family))
1897 goto cont;
1898
1899 err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP,
1900 NETLINK_CB(cb->skb).portid,
1901 cb->nlh->nlmsg_seq, NLM_F_MULTI);
1902 if (err < 0) {
1903 if (likely(skb->len))
1904 goto out;
1905
1906 goto out_err;
1907 }
1908 cont:
1909 idx++;
1910 }
1911
1912 out:
1913 err = skb->len;
1914 out_err:
1915 cb->args[0] = idx;
1916 cb->seq = net->nexthop.seq;
1917 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1918
1919 return err;
1920 }
1921
nexthop_sync_mtu(struct net_device * dev,u32 orig_mtu)1922 static void nexthop_sync_mtu(struct net_device *dev, u32 orig_mtu)
1923 {
1924 unsigned int hash = nh_dev_hashfn(dev->ifindex);
1925 struct net *net = dev_net(dev);
1926 struct hlist_head *head = &net->nexthop.devhash[hash];
1927 struct hlist_node *n;
1928 struct nh_info *nhi;
1929
1930 hlist_for_each_entry_safe(nhi, n, head, dev_hash) {
1931 if (nhi->fib_nhc.nhc_dev == dev) {
1932 if (nhi->family == AF_INET)
1933 fib_nhc_update_mtu(&nhi->fib_nhc, dev->mtu,
1934 orig_mtu);
1935 }
1936 }
1937 }
1938
1939 /* rtnl */
nh_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)1940 static int nh_netdev_event(struct notifier_block *this,
1941 unsigned long event, void *ptr)
1942 {
1943 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1944 struct netdev_notifier_info_ext *info_ext;
1945
1946 switch (event) {
1947 case NETDEV_DOWN:
1948 case NETDEV_UNREGISTER:
1949 nexthop_flush_dev(dev, event);
1950 break;
1951 case NETDEV_CHANGE:
1952 if (!(dev_get_flags(dev) & (IFF_RUNNING | IFF_LOWER_UP)))
1953 nexthop_flush_dev(dev, event);
1954 break;
1955 case NETDEV_CHANGEMTU:
1956 info_ext = ptr;
1957 nexthop_sync_mtu(dev, info_ext->ext.mtu);
1958 rt_cache_flush(dev_net(dev));
1959 break;
1960 }
1961 return NOTIFY_DONE;
1962 }
1963
1964 static struct notifier_block nh_netdev_notifier = {
1965 .notifier_call = nh_netdev_event,
1966 };
1967
register_nexthop_notifier(struct net * net,struct notifier_block * nb)1968 int register_nexthop_notifier(struct net *net, struct notifier_block *nb)
1969 {
1970 return blocking_notifier_chain_register(&net->nexthop.notifier_chain,
1971 nb);
1972 }
1973 EXPORT_SYMBOL(register_nexthop_notifier);
1974
unregister_nexthop_notifier(struct net * net,struct notifier_block * nb)1975 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb)
1976 {
1977 return blocking_notifier_chain_unregister(&net->nexthop.notifier_chain,
1978 nb);
1979 }
1980 EXPORT_SYMBOL(unregister_nexthop_notifier);
1981
nexthop_net_exit(struct net * net)1982 static void __net_exit nexthop_net_exit(struct net *net)
1983 {
1984 rtnl_lock();
1985 flush_all_nexthops(net);
1986 rtnl_unlock();
1987 kfree(net->nexthop.devhash);
1988 }
1989
nexthop_net_init(struct net * net)1990 static int __net_init nexthop_net_init(struct net *net)
1991 {
1992 size_t sz = sizeof(struct hlist_head) * NH_DEV_HASHSIZE;
1993
1994 net->nexthop.rb_root = RB_ROOT;
1995 net->nexthop.devhash = kzalloc(sz, GFP_KERNEL);
1996 if (!net->nexthop.devhash)
1997 return -ENOMEM;
1998 BLOCKING_INIT_NOTIFIER_HEAD(&net->nexthop.notifier_chain);
1999
2000 return 0;
2001 }
2002
2003 static struct pernet_operations nexthop_net_ops = {
2004 .init = nexthop_net_init,
2005 .exit = nexthop_net_exit,
2006 };
2007
nexthop_init(void)2008 static int __init nexthop_init(void)
2009 {
2010 register_pernet_subsys(&nexthop_net_ops);
2011
2012 register_netdevice_notifier(&nh_netdev_notifier);
2013
2014 rtnl_register(PF_UNSPEC, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
2015 rtnl_register(PF_UNSPEC, RTM_DELNEXTHOP, rtm_del_nexthop, NULL, 0);
2016 rtnl_register(PF_UNSPEC, RTM_GETNEXTHOP, rtm_get_nexthop,
2017 rtm_dump_nexthop, 0);
2018
2019 rtnl_register(PF_INET, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
2020 rtnl_register(PF_INET, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0);
2021
2022 rtnl_register(PF_INET6, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
2023 rtnl_register(PF_INET6, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0);
2024
2025 return 0;
2026 }
2027 subsys_initcall(nexthop_init);
2028