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