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