]> Git Repo - linux.git/blob - net/ipv6/route.c
KVM: x86: Replace late check_nested_events() hack with more precise fix
[linux.git] / net / ipv6 / route.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *      Linux INET6 implementation
4  *      FIB front-end.
5  *
6  *      Authors:
7  *      Pedro Roque             <[email protected]>
8  */
9
10 /*      Changes:
11  *
12  *      YOSHIFUJI Hideaki @USAGI
13  *              reworked default router selection.
14  *              - respect outgoing interface
15  *              - select from (probably) reachable routers (i.e.
16  *              routers in REACHABLE, STALE, DELAY or PROBE states).
17  *              - always select the same router if it is (probably)
18  *              reachable.  otherwise, round-robin the list.
19  *      Ville Nuorvala
20  *              Fixed routing subtrees.
21  */
22
23 #define pr_fmt(fmt) "IPv6: " fmt
24
25 #include <linux/capability.h>
26 #include <linux/errno.h>
27 #include <linux/export.h>
28 #include <linux/types.h>
29 #include <linux/times.h>
30 #include <linux/socket.h>
31 #include <linux/sockios.h>
32 #include <linux/net.h>
33 #include <linux/route.h>
34 #include <linux/netdevice.h>
35 #include <linux/in6.h>
36 #include <linux/mroute6.h>
37 #include <linux/init.h>
38 #include <linux/if_arp.h>
39 #include <linux/proc_fs.h>
40 #include <linux/seq_file.h>
41 #include <linux/nsproxy.h>
42 #include <linux/slab.h>
43 #include <linux/jhash.h>
44 #include <net/net_namespace.h>
45 #include <net/snmp.h>
46 #include <net/ipv6.h>
47 #include <net/ip6_fib.h>
48 #include <net/ip6_route.h>
49 #include <net/ndisc.h>
50 #include <net/addrconf.h>
51 #include <net/tcp.h>
52 #include <linux/rtnetlink.h>
53 #include <net/dst.h>
54 #include <net/dst_metadata.h>
55 #include <net/xfrm.h>
56 #include <net/netevent.h>
57 #include <net/netlink.h>
58 #include <net/rtnh.h>
59 #include <net/lwtunnel.h>
60 #include <net/ip_tunnels.h>
61 #include <net/l3mdev.h>
62 #include <net/ip.h>
63 #include <linux/uaccess.h>
64
65 #ifdef CONFIG_SYSCTL
66 #include <linux/sysctl.h>
67 #endif
68
69 static int ip6_rt_type_to_error(u8 fib6_type);
70
71 #define CREATE_TRACE_POINTS
72 #include <trace/events/fib6.h>
73 EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
74 #undef CREATE_TRACE_POINTS
75
76 enum rt6_nud_state {
77         RT6_NUD_FAIL_HARD = -3,
78         RT6_NUD_FAIL_PROBE = -2,
79         RT6_NUD_FAIL_DO_RR = -1,
80         RT6_NUD_SUCCEED = 1
81 };
82
83 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
84 static unsigned int      ip6_default_advmss(const struct dst_entry *dst);
85 static unsigned int      ip6_mtu(const struct dst_entry *dst);
86 static struct dst_entry *ip6_negative_advice(struct dst_entry *);
87 static void             ip6_dst_destroy(struct dst_entry *);
88 static void             ip6_dst_ifdown(struct dst_entry *,
89                                        struct net_device *dev, int how);
90 static int               ip6_dst_gc(struct dst_ops *ops);
91
92 static int              ip6_pkt_discard(struct sk_buff *skb);
93 static int              ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
94 static int              ip6_pkt_prohibit(struct sk_buff *skb);
95 static int              ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
96 static void             ip6_link_failure(struct sk_buff *skb);
97 static void             ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
98                                            struct sk_buff *skb, u32 mtu,
99                                            bool confirm_neigh);
100 static void             rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
101                                         struct sk_buff *skb);
102 static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
103                            int strict);
104 static size_t rt6_nlmsg_size(struct fib6_info *f6i);
105 static int rt6_fill_node(struct net *net, struct sk_buff *skb,
106                          struct fib6_info *rt, struct dst_entry *dst,
107                          struct in6_addr *dest, struct in6_addr *src,
108                          int iif, int type, u32 portid, u32 seq,
109                          unsigned int flags);
110 static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
111                                            const struct in6_addr *daddr,
112                                            const struct in6_addr *saddr);
113
114 #ifdef CONFIG_IPV6_ROUTE_INFO
115 static struct fib6_info *rt6_add_route_info(struct net *net,
116                                            const struct in6_addr *prefix, int prefixlen,
117                                            const struct in6_addr *gwaddr,
118                                            struct net_device *dev,
119                                            unsigned int pref);
120 static struct fib6_info *rt6_get_route_info(struct net *net,
121                                            const struct in6_addr *prefix, int prefixlen,
122                                            const struct in6_addr *gwaddr,
123                                            struct net_device *dev);
124 #endif
125
126 struct uncached_list {
127         spinlock_t              lock;
128         struct list_head        head;
129 };
130
131 static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
132
133 void rt6_uncached_list_add(struct rt6_info *rt)
134 {
135         struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
136
137         rt->rt6i_uncached_list = ul;
138
139         spin_lock_bh(&ul->lock);
140         list_add_tail(&rt->rt6i_uncached, &ul->head);
141         spin_unlock_bh(&ul->lock);
142 }
143
144 void rt6_uncached_list_del(struct rt6_info *rt)
145 {
146         if (!list_empty(&rt->rt6i_uncached)) {
147                 struct uncached_list *ul = rt->rt6i_uncached_list;
148                 struct net *net = dev_net(rt->dst.dev);
149
150                 spin_lock_bh(&ul->lock);
151                 list_del(&rt->rt6i_uncached);
152                 atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
153                 spin_unlock_bh(&ul->lock);
154         }
155 }
156
157 static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
158 {
159         struct net_device *loopback_dev = net->loopback_dev;
160         int cpu;
161
162         if (dev == loopback_dev)
163                 return;
164
165         for_each_possible_cpu(cpu) {
166                 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
167                 struct rt6_info *rt;
168
169                 spin_lock_bh(&ul->lock);
170                 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
171                         struct inet6_dev *rt_idev = rt->rt6i_idev;
172                         struct net_device *rt_dev = rt->dst.dev;
173
174                         if (rt_idev->dev == dev) {
175                                 rt->rt6i_idev = in6_dev_get(loopback_dev);
176                                 in6_dev_put(rt_idev);
177                         }
178
179                         if (rt_dev == dev) {
180                                 rt->dst.dev = blackhole_netdev;
181                                 dev_hold(rt->dst.dev);
182                                 dev_put(rt_dev);
183                         }
184                 }
185                 spin_unlock_bh(&ul->lock);
186         }
187 }
188
189 static inline const void *choose_neigh_daddr(const struct in6_addr *p,
190                                              struct sk_buff *skb,
191                                              const void *daddr)
192 {
193         if (!ipv6_addr_any(p))
194                 return (const void *) p;
195         else if (skb)
196                 return &ipv6_hdr(skb)->daddr;
197         return daddr;
198 }
199
200 struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
201                                    struct net_device *dev,
202                                    struct sk_buff *skb,
203                                    const void *daddr)
204 {
205         struct neighbour *n;
206
207         daddr = choose_neigh_daddr(gw, skb, daddr);
208         n = __ipv6_neigh_lookup(dev, daddr);
209         if (n)
210                 return n;
211
212         n = neigh_create(&nd_tbl, daddr, dev);
213         return IS_ERR(n) ? NULL : n;
214 }
215
216 static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
217                                               struct sk_buff *skb,
218                                               const void *daddr)
219 {
220         const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
221
222         return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
223                                 dst->dev, skb, daddr);
224 }
225
226 static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
227 {
228         struct net_device *dev = dst->dev;
229         struct rt6_info *rt = (struct rt6_info *)dst;
230
231         daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
232         if (!daddr)
233                 return;
234         if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
235                 return;
236         if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
237                 return;
238         __ipv6_confirm_neigh(dev, daddr);
239 }
240
241 static struct dst_ops ip6_dst_ops_template = {
242         .family                 =       AF_INET6,
243         .gc                     =       ip6_dst_gc,
244         .gc_thresh              =       1024,
245         .check                  =       ip6_dst_check,
246         .default_advmss         =       ip6_default_advmss,
247         .mtu                    =       ip6_mtu,
248         .cow_metrics            =       dst_cow_metrics_generic,
249         .destroy                =       ip6_dst_destroy,
250         .ifdown                 =       ip6_dst_ifdown,
251         .negative_advice        =       ip6_negative_advice,
252         .link_failure           =       ip6_link_failure,
253         .update_pmtu            =       ip6_rt_update_pmtu,
254         .redirect               =       rt6_do_redirect,
255         .local_out              =       __ip6_local_out,
256         .neigh_lookup           =       ip6_dst_neigh_lookup,
257         .confirm_neigh          =       ip6_confirm_neigh,
258 };
259
260 static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
261 {
262         unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
263
264         return mtu ? : dst->dev->mtu;
265 }
266
267 static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
268                                          struct sk_buff *skb, u32 mtu,
269                                          bool confirm_neigh)
270 {
271 }
272
273 static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
274                                       struct sk_buff *skb)
275 {
276 }
277
278 static struct dst_ops ip6_dst_blackhole_ops = {
279         .family                 =       AF_INET6,
280         .destroy                =       ip6_dst_destroy,
281         .check                  =       ip6_dst_check,
282         .mtu                    =       ip6_blackhole_mtu,
283         .default_advmss         =       ip6_default_advmss,
284         .update_pmtu            =       ip6_rt_blackhole_update_pmtu,
285         .redirect               =       ip6_rt_blackhole_redirect,
286         .cow_metrics            =       dst_cow_metrics_generic,
287         .neigh_lookup           =       ip6_dst_neigh_lookup,
288 };
289
290 static const u32 ip6_template_metrics[RTAX_MAX] = {
291         [RTAX_HOPLIMIT - 1] = 0,
292 };
293
294 static const struct fib6_info fib6_null_entry_template = {
295         .fib6_flags     = (RTF_REJECT | RTF_NONEXTHOP),
296         .fib6_protocol  = RTPROT_KERNEL,
297         .fib6_metric    = ~(u32)0,
298         .fib6_ref       = REFCOUNT_INIT(1),
299         .fib6_type      = RTN_UNREACHABLE,
300         .fib6_metrics   = (struct dst_metrics *)&dst_default_metrics,
301 };
302
303 static const struct rt6_info ip6_null_entry_template = {
304         .dst = {
305                 .__refcnt       = ATOMIC_INIT(1),
306                 .__use          = 1,
307                 .obsolete       = DST_OBSOLETE_FORCE_CHK,
308                 .error          = -ENETUNREACH,
309                 .input          = ip6_pkt_discard,
310                 .output         = ip6_pkt_discard_out,
311         },
312         .rt6i_flags     = (RTF_REJECT | RTF_NONEXTHOP),
313 };
314
315 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
316
317 static const struct rt6_info ip6_prohibit_entry_template = {
318         .dst = {
319                 .__refcnt       = ATOMIC_INIT(1),
320                 .__use          = 1,
321                 .obsolete       = DST_OBSOLETE_FORCE_CHK,
322                 .error          = -EACCES,
323                 .input          = ip6_pkt_prohibit,
324                 .output         = ip6_pkt_prohibit_out,
325         },
326         .rt6i_flags     = (RTF_REJECT | RTF_NONEXTHOP),
327 };
328
329 static const struct rt6_info ip6_blk_hole_entry_template = {
330         .dst = {
331                 .__refcnt       = ATOMIC_INIT(1),
332                 .__use          = 1,
333                 .obsolete       = DST_OBSOLETE_FORCE_CHK,
334                 .error          = -EINVAL,
335                 .input          = dst_discard,
336                 .output         = dst_discard_out,
337         },
338         .rt6i_flags     = (RTF_REJECT | RTF_NONEXTHOP),
339 };
340
341 #endif
342
343 static void rt6_info_init(struct rt6_info *rt)
344 {
345         struct dst_entry *dst = &rt->dst;
346
347         memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
348         INIT_LIST_HEAD(&rt->rt6i_uncached);
349 }
350
351 /* allocate dst with ip6_dst_ops */
352 struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
353                                int flags)
354 {
355         struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
356                                         1, DST_OBSOLETE_FORCE_CHK, flags);
357
358         if (rt) {
359                 rt6_info_init(rt);
360                 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
361         }
362
363         return rt;
364 }
365 EXPORT_SYMBOL(ip6_dst_alloc);
366
367 static void ip6_dst_destroy(struct dst_entry *dst)
368 {
369         struct rt6_info *rt = (struct rt6_info *)dst;
370         struct fib6_info *from;
371         struct inet6_dev *idev;
372
373         ip_dst_metrics_put(dst);
374         rt6_uncached_list_del(rt);
375
376         idev = rt->rt6i_idev;
377         if (idev) {
378                 rt->rt6i_idev = NULL;
379                 in6_dev_put(idev);
380         }
381
382         from = xchg((__force struct fib6_info **)&rt->from, NULL);
383         fib6_info_release(from);
384 }
385
386 static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
387                            int how)
388 {
389         struct rt6_info *rt = (struct rt6_info *)dst;
390         struct inet6_dev *idev = rt->rt6i_idev;
391         struct net_device *loopback_dev =
392                 dev_net(dev)->loopback_dev;
393
394         if (idev && idev->dev != loopback_dev) {
395                 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
396                 if (loopback_idev) {
397                         rt->rt6i_idev = loopback_idev;
398                         in6_dev_put(idev);
399                 }
400         }
401 }
402
403 static bool __rt6_check_expired(const struct rt6_info *rt)
404 {
405         if (rt->rt6i_flags & RTF_EXPIRES)
406                 return time_after(jiffies, rt->dst.expires);
407         else
408                 return false;
409 }
410
411 static bool rt6_check_expired(const struct rt6_info *rt)
412 {
413         struct fib6_info *from;
414
415         from = rcu_dereference(rt->from);
416
417         if (rt->rt6i_flags & RTF_EXPIRES) {
418                 if (time_after(jiffies, rt->dst.expires))
419                         return true;
420         } else if (from) {
421                 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
422                         fib6_check_expired(from);
423         }
424         return false;
425 }
426
427 void fib6_select_path(const struct net *net, struct fib6_result *res,
428                       struct flowi6 *fl6, int oif, bool have_oif_match,
429                       const struct sk_buff *skb, int strict)
430 {
431         struct fib6_info *sibling, *next_sibling;
432         struct fib6_info *match = res->f6i;
433
434         if ((!match->fib6_nsiblings && !match->nh) || have_oif_match)
435                 goto out;
436
437         /* We might have already computed the hash for ICMPv6 errors. In such
438          * case it will always be non-zero. Otherwise now is the time to do it.
439          */
440         if (!fl6->mp_hash &&
441             (!match->nh || nexthop_is_multipath(match->nh)))
442                 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
443
444         if (unlikely(match->nh)) {
445                 nexthop_path_fib6_result(res, fl6->mp_hash);
446                 return;
447         }
448
449         if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
450                 goto out;
451
452         list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
453                                  fib6_siblings) {
454                 const struct fib6_nh *nh = sibling->fib6_nh;
455                 int nh_upper_bound;
456
457                 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
458                 if (fl6->mp_hash > nh_upper_bound)
459                         continue;
460                 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
461                         break;
462                 match = sibling;
463                 break;
464         }
465
466 out:
467         res->f6i = match;
468         res->nh = match->fib6_nh;
469 }
470
471 /*
472  *      Route lookup. rcu_read_lock() should be held.
473  */
474
475 static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
476                                const struct in6_addr *saddr, int oif, int flags)
477 {
478         const struct net_device *dev;
479
480         if (nh->fib_nh_flags & RTNH_F_DEAD)
481                 return false;
482
483         dev = nh->fib_nh_dev;
484         if (oif) {
485                 if (dev->ifindex == oif)
486                         return true;
487         } else {
488                 if (ipv6_chk_addr(net, saddr, dev,
489                                   flags & RT6_LOOKUP_F_IFACE))
490                         return true;
491         }
492
493         return false;
494 }
495
496 struct fib6_nh_dm_arg {
497         struct net              *net;
498         const struct in6_addr   *saddr;
499         int                     oif;
500         int                     flags;
501         struct fib6_nh          *nh;
502 };
503
504 static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
505 {
506         struct fib6_nh_dm_arg *arg = _arg;
507
508         arg->nh = nh;
509         return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
510                                   arg->flags);
511 }
512
513 /* returns fib6_nh from nexthop or NULL */
514 static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
515                                         struct fib6_result *res,
516                                         const struct in6_addr *saddr,
517                                         int oif, int flags)
518 {
519         struct fib6_nh_dm_arg arg = {
520                 .net   = net,
521                 .saddr = saddr,
522                 .oif   = oif,
523                 .flags = flags,
524         };
525
526         if (nexthop_is_blackhole(nh))
527                 return NULL;
528
529         if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
530                 return arg.nh;
531
532         return NULL;
533 }
534
535 static void rt6_device_match(struct net *net, struct fib6_result *res,
536                              const struct in6_addr *saddr, int oif, int flags)
537 {
538         struct fib6_info *f6i = res->f6i;
539         struct fib6_info *spf6i;
540         struct fib6_nh *nh;
541
542         if (!oif && ipv6_addr_any(saddr)) {
543                 if (unlikely(f6i->nh)) {
544                         nh = nexthop_fib6_nh(f6i->nh);
545                         if (nexthop_is_blackhole(f6i->nh))
546                                 goto out_blackhole;
547                 } else {
548                         nh = f6i->fib6_nh;
549                 }
550                 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
551                         goto out;
552         }
553
554         for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
555                 bool matched = false;
556
557                 if (unlikely(spf6i->nh)) {
558                         nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
559                                               oif, flags);
560                         if (nh)
561                                 matched = true;
562                 } else {
563                         nh = spf6i->fib6_nh;
564                         if (__rt6_device_match(net, nh, saddr, oif, flags))
565                                 matched = true;
566                 }
567                 if (matched) {
568                         res->f6i = spf6i;
569                         goto out;
570                 }
571         }
572
573         if (oif && flags & RT6_LOOKUP_F_IFACE) {
574                 res->f6i = net->ipv6.fib6_null_entry;
575                 nh = res->f6i->fib6_nh;
576                 goto out;
577         }
578
579         if (unlikely(f6i->nh)) {
580                 nh = nexthop_fib6_nh(f6i->nh);
581                 if (nexthop_is_blackhole(f6i->nh))
582                         goto out_blackhole;
583         } else {
584                 nh = f6i->fib6_nh;
585         }
586
587         if (nh->fib_nh_flags & RTNH_F_DEAD) {
588                 res->f6i = net->ipv6.fib6_null_entry;
589                 nh = res->f6i->fib6_nh;
590         }
591 out:
592         res->nh = nh;
593         res->fib6_type = res->f6i->fib6_type;
594         res->fib6_flags = res->f6i->fib6_flags;
595         return;
596
597 out_blackhole:
598         res->fib6_flags |= RTF_REJECT;
599         res->fib6_type = RTN_BLACKHOLE;
600         res->nh = nh;
601 }
602
603 #ifdef CONFIG_IPV6_ROUTER_PREF
604 struct __rt6_probe_work {
605         struct work_struct work;
606         struct in6_addr target;
607         struct net_device *dev;
608 };
609
610 static void rt6_probe_deferred(struct work_struct *w)
611 {
612         struct in6_addr mcaddr;
613         struct __rt6_probe_work *work =
614                 container_of(w, struct __rt6_probe_work, work);
615
616         addrconf_addr_solict_mult(&work->target, &mcaddr);
617         ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
618         dev_put(work->dev);
619         kfree(work);
620 }
621
622 static void rt6_probe(struct fib6_nh *fib6_nh)
623 {
624         struct __rt6_probe_work *work = NULL;
625         const struct in6_addr *nh_gw;
626         unsigned long last_probe;
627         struct neighbour *neigh;
628         struct net_device *dev;
629         struct inet6_dev *idev;
630
631         /*
632          * Okay, this does not seem to be appropriate
633          * for now, however, we need to check if it
634          * is really so; aka Router Reachability Probing.
635          *
636          * Router Reachability Probe MUST be rate-limited
637          * to no more than one per minute.
638          */
639         if (!fib6_nh->fib_nh_gw_family)
640                 return;
641
642         nh_gw = &fib6_nh->fib_nh_gw6;
643         dev = fib6_nh->fib_nh_dev;
644         rcu_read_lock_bh();
645         last_probe = READ_ONCE(fib6_nh->last_probe);
646         idev = __in6_dev_get(dev);
647         neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
648         if (neigh) {
649                 if (neigh->nud_state & NUD_VALID)
650                         goto out;
651
652                 write_lock(&neigh->lock);
653                 if (!(neigh->nud_state & NUD_VALID) &&
654                     time_after(jiffies,
655                                neigh->updated + idev->cnf.rtr_probe_interval)) {
656                         work = kmalloc(sizeof(*work), GFP_ATOMIC);
657                         if (work)
658                                 __neigh_set_probe_once(neigh);
659                 }
660                 write_unlock(&neigh->lock);
661         } else if (time_after(jiffies, last_probe +
662                                        idev->cnf.rtr_probe_interval)) {
663                 work = kmalloc(sizeof(*work), GFP_ATOMIC);
664         }
665
666         if (!work || cmpxchg(&fib6_nh->last_probe,
667                              last_probe, jiffies) != last_probe) {
668                 kfree(work);
669         } else {
670                 INIT_WORK(&work->work, rt6_probe_deferred);
671                 work->target = *nh_gw;
672                 dev_hold(dev);
673                 work->dev = dev;
674                 schedule_work(&work->work);
675         }
676
677 out:
678         rcu_read_unlock_bh();
679 }
680 #else
681 static inline void rt6_probe(struct fib6_nh *fib6_nh)
682 {
683 }
684 #endif
685
686 /*
687  * Default Router Selection (RFC 2461 6.3.6)
688  */
689 static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
690 {
691         enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
692         struct neighbour *neigh;
693
694         rcu_read_lock_bh();
695         neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
696                                           &fib6_nh->fib_nh_gw6);
697         if (neigh) {
698                 read_lock(&neigh->lock);
699                 if (neigh->nud_state & NUD_VALID)
700                         ret = RT6_NUD_SUCCEED;
701 #ifdef CONFIG_IPV6_ROUTER_PREF
702                 else if (!(neigh->nud_state & NUD_FAILED))
703                         ret = RT6_NUD_SUCCEED;
704                 else
705                         ret = RT6_NUD_FAIL_PROBE;
706 #endif
707                 read_unlock(&neigh->lock);
708         } else {
709                 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
710                       RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
711         }
712         rcu_read_unlock_bh();
713
714         return ret;
715 }
716
717 static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
718                            int strict)
719 {
720         int m = 0;
721
722         if (!oif || nh->fib_nh_dev->ifindex == oif)
723                 m = 2;
724
725         if (!m && (strict & RT6_LOOKUP_F_IFACE))
726                 return RT6_NUD_FAIL_HARD;
727 #ifdef CONFIG_IPV6_ROUTER_PREF
728         m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
729 #endif
730         if ((strict & RT6_LOOKUP_F_REACHABLE) &&
731             !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
732                 int n = rt6_check_neigh(nh);
733                 if (n < 0)
734                         return n;
735         }
736         return m;
737 }
738
739 static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
740                        int oif, int strict, int *mpri, bool *do_rr)
741 {
742         bool match_do_rr = false;
743         bool rc = false;
744         int m;
745
746         if (nh->fib_nh_flags & RTNH_F_DEAD)
747                 goto out;
748
749         if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
750             nh->fib_nh_flags & RTNH_F_LINKDOWN &&
751             !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
752                 goto out;
753
754         m = rt6_score_route(nh, fib6_flags, oif, strict);
755         if (m == RT6_NUD_FAIL_DO_RR) {
756                 match_do_rr = true;
757                 m = 0; /* lowest valid score */
758         } else if (m == RT6_NUD_FAIL_HARD) {
759                 goto out;
760         }
761
762         if (strict & RT6_LOOKUP_F_REACHABLE)
763                 rt6_probe(nh);
764
765         /* note that m can be RT6_NUD_FAIL_PROBE at this point */
766         if (m > *mpri) {
767                 *do_rr = match_do_rr;
768                 *mpri = m;
769                 rc = true;
770         }
771 out:
772         return rc;
773 }
774
775 struct fib6_nh_frl_arg {
776         u32             flags;
777         int             oif;
778         int             strict;
779         int             *mpri;
780         bool            *do_rr;
781         struct fib6_nh  *nh;
782 };
783
784 static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
785 {
786         struct fib6_nh_frl_arg *arg = _arg;
787
788         arg->nh = nh;
789         return find_match(nh, arg->flags, arg->oif, arg->strict,
790                           arg->mpri, arg->do_rr);
791 }
792
793 static void __find_rr_leaf(struct fib6_info *f6i_start,
794                            struct fib6_info *nomatch, u32 metric,
795                            struct fib6_result *res, struct fib6_info **cont,
796                            int oif, int strict, bool *do_rr, int *mpri)
797 {
798         struct fib6_info *f6i;
799
800         for (f6i = f6i_start;
801              f6i && f6i != nomatch;
802              f6i = rcu_dereference(f6i->fib6_next)) {
803                 bool matched = false;
804                 struct fib6_nh *nh;
805
806                 if (cont && f6i->fib6_metric != metric) {
807                         *cont = f6i;
808                         return;
809                 }
810
811                 if (fib6_check_expired(f6i))
812                         continue;
813
814                 if (unlikely(f6i->nh)) {
815                         struct fib6_nh_frl_arg arg = {
816                                 .flags  = f6i->fib6_flags,
817                                 .oif    = oif,
818                                 .strict = strict,
819                                 .mpri   = mpri,
820                                 .do_rr  = do_rr
821                         };
822
823                         if (nexthop_is_blackhole(f6i->nh)) {
824                                 res->fib6_flags = RTF_REJECT;
825                                 res->fib6_type = RTN_BLACKHOLE;
826                                 res->f6i = f6i;
827                                 res->nh = nexthop_fib6_nh(f6i->nh);
828                                 return;
829                         }
830                         if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
831                                                      &arg)) {
832                                 matched = true;
833                                 nh = arg.nh;
834                         }
835                 } else {
836                         nh = f6i->fib6_nh;
837                         if (find_match(nh, f6i->fib6_flags, oif, strict,
838                                        mpri, do_rr))
839                                 matched = true;
840                 }
841                 if (matched) {
842                         res->f6i = f6i;
843                         res->nh = nh;
844                         res->fib6_flags = f6i->fib6_flags;
845                         res->fib6_type = f6i->fib6_type;
846                 }
847         }
848 }
849
850 static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
851                          struct fib6_info *rr_head, int oif, int strict,
852                          bool *do_rr, struct fib6_result *res)
853 {
854         u32 metric = rr_head->fib6_metric;
855         struct fib6_info *cont = NULL;
856         int mpri = -1;
857
858         __find_rr_leaf(rr_head, NULL, metric, res, &cont,
859                        oif, strict, do_rr, &mpri);
860
861         __find_rr_leaf(leaf, rr_head, metric, res, &cont,
862                        oif, strict, do_rr, &mpri);
863
864         if (res->f6i || !cont)
865                 return;
866
867         __find_rr_leaf(cont, NULL, metric, res, NULL,
868                        oif, strict, do_rr, &mpri);
869 }
870
871 static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
872                        struct fib6_result *res, int strict)
873 {
874         struct fib6_info *leaf = rcu_dereference(fn->leaf);
875         struct fib6_info *rt0;
876         bool do_rr = false;
877         int key_plen;
878
879         /* make sure this function or its helpers sets f6i */
880         res->f6i = NULL;
881
882         if (!leaf || leaf == net->ipv6.fib6_null_entry)
883                 goto out;
884
885         rt0 = rcu_dereference(fn->rr_ptr);
886         if (!rt0)
887                 rt0 = leaf;
888
889         /* Double check to make sure fn is not an intermediate node
890          * and fn->leaf does not points to its child's leaf
891          * (This might happen if all routes under fn are deleted from
892          * the tree and fib6_repair_tree() is called on the node.)
893          */
894         key_plen = rt0->fib6_dst.plen;
895 #ifdef CONFIG_IPV6_SUBTREES
896         if (rt0->fib6_src.plen)
897                 key_plen = rt0->fib6_src.plen;
898 #endif
899         if (fn->fn_bit != key_plen)
900                 goto out;
901
902         find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
903         if (do_rr) {
904                 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
905
906                 /* no entries matched; do round-robin */
907                 if (!next || next->fib6_metric != rt0->fib6_metric)
908                         next = leaf;
909
910                 if (next != rt0) {
911                         spin_lock_bh(&leaf->fib6_table->tb6_lock);
912                         /* make sure next is not being deleted from the tree */
913                         if (next->fib6_node)
914                                 rcu_assign_pointer(fn->rr_ptr, next);
915                         spin_unlock_bh(&leaf->fib6_table->tb6_lock);
916                 }
917         }
918
919 out:
920         if (!res->f6i) {
921                 res->f6i = net->ipv6.fib6_null_entry;
922                 res->nh = res->f6i->fib6_nh;
923                 res->fib6_flags = res->f6i->fib6_flags;
924                 res->fib6_type = res->f6i->fib6_type;
925         }
926 }
927
928 static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
929 {
930         return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
931                res->nh->fib_nh_gw_family;
932 }
933
934 #ifdef CONFIG_IPV6_ROUTE_INFO
935 int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
936                   const struct in6_addr *gwaddr)
937 {
938         struct net *net = dev_net(dev);
939         struct route_info *rinfo = (struct route_info *) opt;
940         struct in6_addr prefix_buf, *prefix;
941         unsigned int pref;
942         unsigned long lifetime;
943         struct fib6_info *rt;
944
945         if (len < sizeof(struct route_info)) {
946                 return -EINVAL;
947         }
948
949         /* Sanity check for prefix_len and length */
950         if (rinfo->length > 3) {
951                 return -EINVAL;
952         } else if (rinfo->prefix_len > 128) {
953                 return -EINVAL;
954         } else if (rinfo->prefix_len > 64) {
955                 if (rinfo->length < 2) {
956                         return -EINVAL;
957                 }
958         } else if (rinfo->prefix_len > 0) {
959                 if (rinfo->length < 1) {
960                         return -EINVAL;
961                 }
962         }
963
964         pref = rinfo->route_pref;
965         if (pref == ICMPV6_ROUTER_PREF_INVALID)
966                 return -EINVAL;
967
968         lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
969
970         if (rinfo->length == 3)
971                 prefix = (struct in6_addr *)rinfo->prefix;
972         else {
973                 /* this function is safe */
974                 ipv6_addr_prefix(&prefix_buf,
975                                  (struct in6_addr *)rinfo->prefix,
976                                  rinfo->prefix_len);
977                 prefix = &prefix_buf;
978         }
979
980         if (rinfo->prefix_len == 0)
981                 rt = rt6_get_dflt_router(net, gwaddr, dev);
982         else
983                 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
984                                         gwaddr, dev);
985
986         if (rt && !lifetime) {
987                 ip6_del_rt(net, rt);
988                 rt = NULL;
989         }
990
991         if (!rt && lifetime)
992                 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
993                                         dev, pref);
994         else if (rt)
995                 rt->fib6_flags = RTF_ROUTEINFO |
996                                  (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
997
998         if (rt) {
999                 if (!addrconf_finite_timeout(lifetime))
1000                         fib6_clean_expires(rt);
1001                 else
1002                         fib6_set_expires(rt, jiffies + HZ * lifetime);
1003
1004                 fib6_info_release(rt);
1005         }
1006         return 0;
1007 }
1008 #endif
1009
1010 /*
1011  *      Misc support functions
1012  */
1013
1014 /* called with rcu_lock held */
1015 static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
1016 {
1017         struct net_device *dev = res->nh->fib_nh_dev;
1018
1019         if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
1020                 /* for copies of local routes, dst->dev needs to be the
1021                  * device if it is a master device, the master device if
1022                  * device is enslaved, and the loopback as the default
1023                  */
1024                 if (netif_is_l3_slave(dev) &&
1025                     !rt6_need_strict(&res->f6i->fib6_dst.addr))
1026                         dev = l3mdev_master_dev_rcu(dev);
1027                 else if (!netif_is_l3_master(dev))
1028                         dev = dev_net(dev)->loopback_dev;
1029                 /* last case is netif_is_l3_master(dev) is true in which
1030                  * case we want dev returned to be dev
1031                  */
1032         }
1033
1034         return dev;
1035 }
1036
1037 static const int fib6_prop[RTN_MAX + 1] = {
1038         [RTN_UNSPEC]    = 0,
1039         [RTN_UNICAST]   = 0,
1040         [RTN_LOCAL]     = 0,
1041         [RTN_BROADCAST] = 0,
1042         [RTN_ANYCAST]   = 0,
1043         [RTN_MULTICAST] = 0,
1044         [RTN_BLACKHOLE] = -EINVAL,
1045         [RTN_UNREACHABLE] = -EHOSTUNREACH,
1046         [RTN_PROHIBIT]  = -EACCES,
1047         [RTN_THROW]     = -EAGAIN,
1048         [RTN_NAT]       = -EINVAL,
1049         [RTN_XRESOLVE]  = -EINVAL,
1050 };
1051
1052 static int ip6_rt_type_to_error(u8 fib6_type)
1053 {
1054         return fib6_prop[fib6_type];
1055 }
1056
1057 static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
1058 {
1059         unsigned short flags = 0;
1060
1061         if (rt->dst_nocount)
1062                 flags |= DST_NOCOUNT;
1063         if (rt->dst_nopolicy)
1064                 flags |= DST_NOPOLICY;
1065
1066         return flags;
1067 }
1068
1069 static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
1070 {
1071         rt->dst.error = ip6_rt_type_to_error(fib6_type);
1072
1073         switch (fib6_type) {
1074         case RTN_BLACKHOLE:
1075                 rt->dst.output = dst_discard_out;
1076                 rt->dst.input = dst_discard;
1077                 break;
1078         case RTN_PROHIBIT:
1079                 rt->dst.output = ip6_pkt_prohibit_out;
1080                 rt->dst.input = ip6_pkt_prohibit;
1081                 break;
1082         case RTN_THROW:
1083         case RTN_UNREACHABLE:
1084         default:
1085                 rt->dst.output = ip6_pkt_discard_out;
1086                 rt->dst.input = ip6_pkt_discard;
1087                 break;
1088         }
1089 }
1090
1091 static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
1092 {
1093         struct fib6_info *f6i = res->f6i;
1094
1095         if (res->fib6_flags & RTF_REJECT) {
1096                 ip6_rt_init_dst_reject(rt, res->fib6_type);
1097                 return;
1098         }
1099
1100         rt->dst.error = 0;
1101         rt->dst.output = ip6_output;
1102
1103         if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
1104                 rt->dst.input = ip6_input;
1105         } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
1106                 rt->dst.input = ip6_mc_input;
1107         } else {
1108                 rt->dst.input = ip6_forward;
1109         }
1110
1111         if (res->nh->fib_nh_lws) {
1112                 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
1113                 lwtunnel_set_redirect(&rt->dst);
1114         }
1115
1116         rt->dst.lastuse = jiffies;
1117 }
1118
1119 /* Caller must already hold reference to @from */
1120 static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
1121 {
1122         rt->rt6i_flags &= ~RTF_EXPIRES;
1123         rcu_assign_pointer(rt->from, from);
1124         ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
1125 }
1126
1127 /* Caller must already hold reference to f6i in result */
1128 static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
1129 {
1130         const struct fib6_nh *nh = res->nh;
1131         const struct net_device *dev = nh->fib_nh_dev;
1132         struct fib6_info *f6i = res->f6i;
1133
1134         ip6_rt_init_dst(rt, res);
1135
1136         rt->rt6i_dst = f6i->fib6_dst;
1137         rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
1138         rt->rt6i_flags = res->fib6_flags;
1139         if (nh->fib_nh_gw_family) {
1140                 rt->rt6i_gateway = nh->fib_nh_gw6;
1141                 rt->rt6i_flags |= RTF_GATEWAY;
1142         }
1143         rt6_set_from(rt, f6i);
1144 #ifdef CONFIG_IPV6_SUBTREES
1145         rt->rt6i_src = f6i->fib6_src;
1146 #endif
1147 }
1148
1149 static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1150                                         struct in6_addr *saddr)
1151 {
1152         struct fib6_node *pn, *sn;
1153         while (1) {
1154                 if (fn->fn_flags & RTN_TL_ROOT)
1155                         return NULL;
1156                 pn = rcu_dereference(fn->parent);
1157                 sn = FIB6_SUBTREE(pn);
1158                 if (sn && sn != fn)
1159                         fn = fib6_node_lookup(sn, NULL, saddr);
1160                 else
1161                         fn = pn;
1162                 if (fn->fn_flags & RTN_RTINFO)
1163                         return fn;
1164         }
1165 }
1166
1167 static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
1168 {
1169         struct rt6_info *rt = *prt;
1170
1171         if (dst_hold_safe(&rt->dst))
1172                 return true;
1173         if (net) {
1174                 rt = net->ipv6.ip6_null_entry;
1175                 dst_hold(&rt->dst);
1176         } else {
1177                 rt = NULL;
1178         }
1179         *prt = rt;
1180         return false;
1181 }
1182
1183 /* called with rcu_lock held */
1184 static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
1185 {
1186         struct net_device *dev = res->nh->fib_nh_dev;
1187         struct fib6_info *f6i = res->f6i;
1188         unsigned short flags;
1189         struct rt6_info *nrt;
1190
1191         if (!fib6_info_hold_safe(f6i))
1192                 goto fallback;
1193
1194         flags = fib6_info_dst_flags(f6i);
1195         nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
1196         if (!nrt) {
1197                 fib6_info_release(f6i);
1198                 goto fallback;
1199         }
1200
1201         ip6_rt_copy_init(nrt, res);
1202         return nrt;
1203
1204 fallback:
1205         nrt = dev_net(dev)->ipv6.ip6_null_entry;
1206         dst_hold(&nrt->dst);
1207         return nrt;
1208 }
1209
1210 static struct rt6_info *ip6_pol_route_lookup(struct net *net,
1211                                              struct fib6_table *table,
1212                                              struct flowi6 *fl6,
1213                                              const struct sk_buff *skb,
1214                                              int flags)
1215 {
1216         struct fib6_result res = {};
1217         struct fib6_node *fn;
1218         struct rt6_info *rt;
1219
1220         if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1221                 flags &= ~RT6_LOOKUP_F_IFACE;
1222
1223         rcu_read_lock();
1224         fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1225 restart:
1226         res.f6i = rcu_dereference(fn->leaf);
1227         if (!res.f6i)
1228                 res.f6i = net->ipv6.fib6_null_entry;
1229         else
1230                 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1231                                  flags);
1232
1233         if (res.f6i == net->ipv6.fib6_null_entry) {
1234                 fn = fib6_backtrack(fn, &fl6->saddr);
1235                 if (fn)
1236                         goto restart;
1237
1238                 rt = net->ipv6.ip6_null_entry;
1239                 dst_hold(&rt->dst);
1240                 goto out;
1241         } else if (res.fib6_flags & RTF_REJECT) {
1242                 goto do_create;
1243         }
1244
1245         fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1246                          fl6->flowi6_oif != 0, skb, flags);
1247
1248         /* Search through exception table */
1249         rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
1250         if (rt) {
1251                 if (ip6_hold_safe(net, &rt))
1252                         dst_use_noref(&rt->dst, jiffies);
1253         } else {
1254 do_create:
1255                 rt = ip6_create_rt_rcu(&res);
1256         }
1257
1258 out:
1259         trace_fib6_table_lookup(net, &res, table, fl6);
1260
1261         rcu_read_unlock();
1262
1263         return rt;
1264 }
1265
1266 struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1267                                    const struct sk_buff *skb, int flags)
1268 {
1269         return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1270 }
1271 EXPORT_SYMBOL_GPL(ip6_route_lookup);
1272
1273 struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1274                             const struct in6_addr *saddr, int oif,
1275                             const struct sk_buff *skb, int strict)
1276 {
1277         struct flowi6 fl6 = {
1278                 .flowi6_oif = oif,
1279                 .daddr = *daddr,
1280         };
1281         struct dst_entry *dst;
1282         int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1283
1284         if (saddr) {
1285                 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1286                 flags |= RT6_LOOKUP_F_HAS_SADDR;
1287         }
1288
1289         dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1290         if (dst->error == 0)
1291                 return (struct rt6_info *) dst;
1292
1293         dst_release(dst);
1294
1295         return NULL;
1296 }
1297 EXPORT_SYMBOL(rt6_lookup);
1298
1299 /* ip6_ins_rt is called with FREE table->tb6_lock.
1300  * It takes new route entry, the addition fails by any reason the
1301  * route is released.
1302  * Caller must hold dst before calling it.
1303  */
1304
1305 static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1306                         struct netlink_ext_ack *extack)
1307 {
1308         int err;
1309         struct fib6_table *table;
1310
1311         table = rt->fib6_table;
1312         spin_lock_bh(&table->tb6_lock);
1313         err = fib6_add(&table->tb6_root, rt, info, extack);
1314         spin_unlock_bh(&table->tb6_lock);
1315
1316         return err;
1317 }
1318
1319 int ip6_ins_rt(struct net *net, struct fib6_info *rt)
1320 {
1321         struct nl_info info = { .nl_net = net, };
1322
1323         return __ip6_ins_rt(rt, &info, NULL);
1324 }
1325
1326 static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
1327                                            const struct in6_addr *daddr,
1328                                            const struct in6_addr *saddr)
1329 {
1330         struct fib6_info *f6i = res->f6i;
1331         struct net_device *dev;
1332         struct rt6_info *rt;
1333
1334         /*
1335          *      Clone the route.
1336          */
1337
1338         if (!fib6_info_hold_safe(f6i))
1339                 return NULL;
1340
1341         dev = ip6_rt_get_dev_rcu(res);
1342         rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1343         if (!rt) {
1344                 fib6_info_release(f6i);
1345                 return NULL;
1346         }
1347
1348         ip6_rt_copy_init(rt, res);
1349         rt->rt6i_flags |= RTF_CACHE;
1350         rt->rt6i_dst.addr = *daddr;
1351         rt->rt6i_dst.plen = 128;
1352
1353         if (!rt6_is_gw_or_nonexthop(res)) {
1354                 if (f6i->fib6_dst.plen != 128 &&
1355                     ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
1356                         rt->rt6i_flags |= RTF_ANYCAST;
1357 #ifdef CONFIG_IPV6_SUBTREES
1358                 if (rt->rt6i_src.plen && saddr) {
1359                         rt->rt6i_src.addr = *saddr;
1360                         rt->rt6i_src.plen = 128;
1361                 }
1362 #endif
1363         }
1364
1365         return rt;
1366 }
1367
1368 static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
1369 {
1370         struct fib6_info *f6i = res->f6i;
1371         unsigned short flags = fib6_info_dst_flags(f6i);
1372         struct net_device *dev;
1373         struct rt6_info *pcpu_rt;
1374
1375         if (!fib6_info_hold_safe(f6i))
1376                 return NULL;
1377
1378         rcu_read_lock();
1379         dev = ip6_rt_get_dev_rcu(res);
1380         pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
1381         rcu_read_unlock();
1382         if (!pcpu_rt) {
1383                 fib6_info_release(f6i);
1384                 return NULL;
1385         }
1386         ip6_rt_copy_init(pcpu_rt, res);
1387         pcpu_rt->rt6i_flags |= RTF_PCPU;
1388         return pcpu_rt;
1389 }
1390
1391 /* It should be called with rcu_read_lock() acquired */
1392 static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
1393 {
1394         struct rt6_info *pcpu_rt;
1395
1396         pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
1397
1398         return pcpu_rt;
1399 }
1400
1401 static struct rt6_info *rt6_make_pcpu_route(struct net *net,
1402                                             const struct fib6_result *res)
1403 {
1404         struct rt6_info *pcpu_rt, *prev, **p;
1405
1406         pcpu_rt = ip6_rt_pcpu_alloc(res);
1407         if (!pcpu_rt)
1408                 return NULL;
1409
1410         p = this_cpu_ptr(res->nh->rt6i_pcpu);
1411         prev = cmpxchg(p, NULL, pcpu_rt);
1412         BUG_ON(prev);
1413
1414         if (res->f6i->fib6_destroying) {
1415                 struct fib6_info *from;
1416
1417                 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1418                 fib6_info_release(from);
1419         }
1420
1421         return pcpu_rt;
1422 }
1423
1424 /* exception hash table implementation
1425  */
1426 static DEFINE_SPINLOCK(rt6_exception_lock);
1427
1428 /* Remove rt6_ex from hash table and free the memory
1429  * Caller must hold rt6_exception_lock
1430  */
1431 static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1432                                  struct rt6_exception *rt6_ex)
1433 {
1434         struct fib6_info *from;
1435         struct net *net;
1436
1437         if (!bucket || !rt6_ex)
1438                 return;
1439
1440         net = dev_net(rt6_ex->rt6i->dst.dev);
1441         net->ipv6.rt6_stats->fib_rt_cache--;
1442
1443         /* purge completely the exception to allow releasing the held resources:
1444          * some [sk] cache may keep the dst around for unlimited time
1445          */
1446         from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
1447         fib6_info_release(from);
1448         dst_dev_put(&rt6_ex->rt6i->dst);
1449
1450         hlist_del_rcu(&rt6_ex->hlist);
1451         dst_release(&rt6_ex->rt6i->dst);
1452         kfree_rcu(rt6_ex, rcu);
1453         WARN_ON_ONCE(!bucket->depth);
1454         bucket->depth--;
1455 }
1456
1457 /* Remove oldest rt6_ex in bucket and free the memory
1458  * Caller must hold rt6_exception_lock
1459  */
1460 static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1461 {
1462         struct rt6_exception *rt6_ex, *oldest = NULL;
1463
1464         if (!bucket)
1465                 return;
1466
1467         hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1468                 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1469                         oldest = rt6_ex;
1470         }
1471         rt6_remove_exception(bucket, oldest);
1472 }
1473
1474 static u32 rt6_exception_hash(const struct in6_addr *dst,
1475                               const struct in6_addr *src)
1476 {
1477         static u32 seed __read_mostly;
1478         u32 val;
1479
1480         net_get_random_once(&seed, sizeof(seed));
1481         val = jhash2((const u32 *)dst, sizeof(*dst)/sizeof(u32), seed);
1482
1483 #ifdef CONFIG_IPV6_SUBTREES
1484         if (src)
1485                 val = jhash2((const u32 *)src, sizeof(*src)/sizeof(u32), val);
1486 #endif
1487         return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1488 }
1489
1490 /* Helper function to find the cached rt in the hash table
1491  * and update bucket pointer to point to the bucket for this
1492  * (daddr, saddr) pair
1493  * Caller must hold rt6_exception_lock
1494  */
1495 static struct rt6_exception *
1496 __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1497                               const struct in6_addr *daddr,
1498                               const struct in6_addr *saddr)
1499 {
1500         struct rt6_exception *rt6_ex;
1501         u32 hval;
1502
1503         if (!(*bucket) || !daddr)
1504                 return NULL;
1505
1506         hval = rt6_exception_hash(daddr, saddr);
1507         *bucket += hval;
1508
1509         hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1510                 struct rt6_info *rt6 = rt6_ex->rt6i;
1511                 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1512
1513 #ifdef CONFIG_IPV6_SUBTREES
1514                 if (matched && saddr)
1515                         matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1516 #endif
1517                 if (matched)
1518                         return rt6_ex;
1519         }
1520         return NULL;
1521 }
1522
1523 /* Helper function to find the cached rt in the hash table
1524  * and update bucket pointer to point to the bucket for this
1525  * (daddr, saddr) pair
1526  * Caller must hold rcu_read_lock()
1527  */
1528 static struct rt6_exception *
1529 __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1530                          const struct in6_addr *daddr,
1531                          const struct in6_addr *saddr)
1532 {
1533         struct rt6_exception *rt6_ex;
1534         u32 hval;
1535
1536         WARN_ON_ONCE(!rcu_read_lock_held());
1537
1538         if (!(*bucket) || !daddr)
1539                 return NULL;
1540
1541         hval = rt6_exception_hash(daddr, saddr);
1542         *bucket += hval;
1543
1544         hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1545                 struct rt6_info *rt6 = rt6_ex->rt6i;
1546                 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1547
1548 #ifdef CONFIG_IPV6_SUBTREES
1549                 if (matched && saddr)
1550                         matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1551 #endif
1552                 if (matched)
1553                         return rt6_ex;
1554         }
1555         return NULL;
1556 }
1557
1558 static unsigned int fib6_mtu(const struct fib6_result *res)
1559 {
1560         const struct fib6_nh *nh = res->nh;
1561         unsigned int mtu;
1562
1563         if (res->f6i->fib6_pmtu) {
1564                 mtu = res->f6i->fib6_pmtu;
1565         } else {
1566                 struct net_device *dev = nh->fib_nh_dev;
1567                 struct inet6_dev *idev;
1568
1569                 rcu_read_lock();
1570                 idev = __in6_dev_get(dev);
1571                 mtu = idev->cnf.mtu6;
1572                 rcu_read_unlock();
1573         }
1574
1575         mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1576
1577         return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
1578 }
1579
1580 #define FIB6_EXCEPTION_BUCKET_FLUSHED  0x1UL
1581
1582 /* used when the flushed bit is not relevant, only access to the bucket
1583  * (ie., all bucket users except rt6_insert_exception);
1584  *
1585  * called under rcu lock; sometimes called with rt6_exception_lock held
1586  */
1587 static
1588 struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1589                                                        spinlock_t *lock)
1590 {
1591         struct rt6_exception_bucket *bucket;
1592
1593         if (lock)
1594                 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1595                                                    lockdep_is_held(lock));
1596         else
1597                 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1598
1599         /* remove bucket flushed bit if set */
1600         if (bucket) {
1601                 unsigned long p = (unsigned long)bucket;
1602
1603                 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1604                 bucket = (struct rt6_exception_bucket *)p;
1605         }
1606
1607         return bucket;
1608 }
1609
1610 static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1611 {
1612         unsigned long p = (unsigned long)bucket;
1613
1614         return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1615 }
1616
1617 /* called with rt6_exception_lock held */
1618 static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1619                                               spinlock_t *lock)
1620 {
1621         struct rt6_exception_bucket *bucket;
1622         unsigned long p;
1623
1624         bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1625                                            lockdep_is_held(lock));
1626
1627         p = (unsigned long)bucket;
1628         p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1629         bucket = (struct rt6_exception_bucket *)p;
1630         rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1631 }
1632
1633 static int rt6_insert_exception(struct rt6_info *nrt,
1634                                 const struct fib6_result *res)
1635 {
1636         struct net *net = dev_net(nrt->dst.dev);
1637         struct rt6_exception_bucket *bucket;
1638         struct fib6_info *f6i = res->f6i;
1639         struct in6_addr *src_key = NULL;
1640         struct rt6_exception *rt6_ex;
1641         struct fib6_nh *nh = res->nh;
1642         int err = 0;
1643
1644         spin_lock_bh(&rt6_exception_lock);
1645
1646         bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1647                                           lockdep_is_held(&rt6_exception_lock));
1648         if (!bucket) {
1649                 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1650                                  GFP_ATOMIC);
1651                 if (!bucket) {
1652                         err = -ENOMEM;
1653                         goto out;
1654                 }
1655                 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1656         } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1657                 err = -EINVAL;
1658                 goto out;
1659         }
1660
1661 #ifdef CONFIG_IPV6_SUBTREES
1662         /* fib6_src.plen != 0 indicates f6i is in subtree
1663          * and exception table is indexed by a hash of
1664          * both fib6_dst and fib6_src.
1665          * Otherwise, the exception table is indexed by
1666          * a hash of only fib6_dst.
1667          */
1668         if (f6i->fib6_src.plen)
1669                 src_key = &nrt->rt6i_src.addr;
1670 #endif
1671         /* rt6_mtu_change() might lower mtu on f6i.
1672          * Only insert this exception route if its mtu
1673          * is less than f6i's mtu value.
1674          */
1675         if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
1676                 err = -EINVAL;
1677                 goto out;
1678         }
1679
1680         rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1681                                                src_key);
1682         if (rt6_ex)
1683                 rt6_remove_exception(bucket, rt6_ex);
1684
1685         rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1686         if (!rt6_ex) {
1687                 err = -ENOMEM;
1688                 goto out;
1689         }
1690         rt6_ex->rt6i = nrt;
1691         rt6_ex->stamp = jiffies;
1692         hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1693         bucket->depth++;
1694         net->ipv6.rt6_stats->fib_rt_cache++;
1695
1696         if (bucket->depth > FIB6_MAX_DEPTH)
1697                 rt6_exception_remove_oldest(bucket);
1698
1699 out:
1700         spin_unlock_bh(&rt6_exception_lock);
1701
1702         /* Update fn->fn_sernum to invalidate all cached dst */
1703         if (!err) {
1704                 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1705                 fib6_update_sernum(net, f6i);
1706                 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1707                 fib6_force_start_gc(net);
1708         }
1709
1710         return err;
1711 }
1712
1713 static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
1714 {
1715         struct rt6_exception_bucket *bucket;
1716         struct rt6_exception *rt6_ex;
1717         struct hlist_node *tmp;
1718         int i;
1719
1720         spin_lock_bh(&rt6_exception_lock);
1721
1722         bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1723         if (!bucket)
1724                 goto out;
1725
1726         /* Prevent rt6_insert_exception() to recreate the bucket list */
1727         if (!from)
1728                 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1729
1730         for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1731                 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1732                         if (!from ||
1733                             rcu_access_pointer(rt6_ex->rt6i->from) == from)
1734                                 rt6_remove_exception(bucket, rt6_ex);
1735                 }
1736                 WARN_ON_ONCE(!from && bucket->depth);
1737                 bucket++;
1738         }
1739 out:
1740         spin_unlock_bh(&rt6_exception_lock);
1741 }
1742
1743 static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1744 {
1745         struct fib6_info *f6i = arg;
1746
1747         fib6_nh_flush_exceptions(nh, f6i);
1748
1749         return 0;
1750 }
1751
1752 void rt6_flush_exceptions(struct fib6_info *f6i)
1753 {
1754         if (f6i->nh)
1755                 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1756                                          f6i);
1757         else
1758                 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
1759 }
1760
1761 /* Find cached rt in the hash table inside passed in rt
1762  * Caller has to hold rcu_read_lock()
1763  */
1764 static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
1765                                            const struct in6_addr *daddr,
1766                                            const struct in6_addr *saddr)
1767 {
1768         const struct in6_addr *src_key = NULL;
1769         struct rt6_exception_bucket *bucket;
1770         struct rt6_exception *rt6_ex;
1771         struct rt6_info *ret = NULL;
1772
1773 #ifdef CONFIG_IPV6_SUBTREES
1774         /* fib6i_src.plen != 0 indicates f6i is in subtree
1775          * and exception table is indexed by a hash of
1776          * both fib6_dst and fib6_src.
1777          * However, the src addr used to create the hash
1778          * might not be exactly the passed in saddr which
1779          * is a /128 addr from the flow.
1780          * So we need to use f6i->fib6_src to redo lookup
1781          * if the passed in saddr does not find anything.
1782          * (See the logic in ip6_rt_cache_alloc() on how
1783          * rt->rt6i_src is updated.)
1784          */
1785         if (res->f6i->fib6_src.plen)
1786                 src_key = saddr;
1787 find_ex:
1788 #endif
1789         bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
1790         rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1791
1792         if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
1793                 ret = rt6_ex->rt6i;
1794
1795 #ifdef CONFIG_IPV6_SUBTREES
1796         /* Use fib6_src as src_key and redo lookup */
1797         if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1798                 src_key = &res->f6i->fib6_src.addr;
1799                 goto find_ex;
1800         }
1801 #endif
1802
1803         return ret;
1804 }
1805
1806 /* Remove the passed in cached rt from the hash table that contains it */
1807 static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
1808                                     const struct rt6_info *rt)
1809 {
1810         const struct in6_addr *src_key = NULL;
1811         struct rt6_exception_bucket *bucket;
1812         struct rt6_exception *rt6_ex;
1813         int err;
1814
1815         if (!rcu_access_pointer(nh->rt6i_exception_bucket))
1816                 return -ENOENT;
1817
1818         spin_lock_bh(&rt6_exception_lock);
1819         bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1820
1821 #ifdef CONFIG_IPV6_SUBTREES
1822         /* rt6i_src.plen != 0 indicates 'from' is in subtree
1823          * and exception table is indexed by a hash of
1824          * both rt6i_dst and rt6i_src.
1825          * Otherwise, the exception table is indexed by
1826          * a hash of only rt6i_dst.
1827          */
1828         if (plen)
1829                 src_key = &rt->rt6i_src.addr;
1830 #endif
1831         rt6_ex = __rt6_find_exception_spinlock(&bucket,
1832                                                &rt->rt6i_dst.addr,
1833                                                src_key);
1834         if (rt6_ex) {
1835                 rt6_remove_exception(bucket, rt6_ex);
1836                 err = 0;
1837         } else {
1838                 err = -ENOENT;
1839         }
1840
1841         spin_unlock_bh(&rt6_exception_lock);
1842         return err;
1843 }
1844
1845 struct fib6_nh_excptn_arg {
1846         struct rt6_info *rt;
1847         int             plen;
1848 };
1849
1850 static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1851 {
1852         struct fib6_nh_excptn_arg *arg = _arg;
1853         int err;
1854
1855         err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1856         if (err == 0)
1857                 return 1;
1858
1859         return 0;
1860 }
1861
1862 static int rt6_remove_exception_rt(struct rt6_info *rt)
1863 {
1864         struct fib6_info *from;
1865
1866         from = rcu_dereference(rt->from);
1867         if (!from || !(rt->rt6i_flags & RTF_CACHE))
1868                 return -EINVAL;
1869
1870         if (from->nh) {
1871                 struct fib6_nh_excptn_arg arg = {
1872                         .rt = rt,
1873                         .plen = from->fib6_src.plen
1874                 };
1875                 int rc;
1876
1877                 /* rc = 1 means an entry was found */
1878                 rc = nexthop_for_each_fib6_nh(from->nh,
1879                                               rt6_nh_remove_exception_rt,
1880                                               &arg);
1881                 return rc ? 0 : -ENOENT;
1882         }
1883
1884         return fib6_nh_remove_exception(from->fib6_nh,
1885                                         from->fib6_src.plen, rt);
1886 }
1887
1888 /* Find rt6_ex which contains the passed in rt cache and
1889  * refresh its stamp
1890  */
1891 static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
1892                                      const struct rt6_info *rt)
1893 {
1894         const struct in6_addr *src_key = NULL;
1895         struct rt6_exception_bucket *bucket;
1896         struct rt6_exception *rt6_ex;
1897
1898         bucket = fib6_nh_get_excptn_bucket(nh, NULL);
1899 #ifdef CONFIG_IPV6_SUBTREES
1900         /* rt6i_src.plen != 0 indicates 'from' is in subtree
1901          * and exception table is indexed by a hash of
1902          * both rt6i_dst and rt6i_src.
1903          * Otherwise, the exception table is indexed by
1904          * a hash of only rt6i_dst.
1905          */
1906         if (plen)
1907                 src_key = &rt->rt6i_src.addr;
1908 #endif
1909         rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
1910         if (rt6_ex)
1911                 rt6_ex->stamp = jiffies;
1912 }
1913
1914 struct fib6_nh_match_arg {
1915         const struct net_device *dev;
1916         const struct in6_addr   *gw;
1917         struct fib6_nh          *match;
1918 };
1919
1920 /* determine if fib6_nh has given device and gateway */
1921 static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1922 {
1923         struct fib6_nh_match_arg *arg = _arg;
1924
1925         if (arg->dev != nh->fib_nh_dev ||
1926             (arg->gw && !nh->fib_nh_gw_family) ||
1927             (!arg->gw && nh->fib_nh_gw_family) ||
1928             (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1929                 return 0;
1930
1931         arg->match = nh;
1932
1933         /* found a match, break the loop */
1934         return 1;
1935 }
1936
1937 static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1938 {
1939         struct fib6_info *from;
1940         struct fib6_nh *fib6_nh;
1941
1942         rcu_read_lock();
1943
1944         from = rcu_dereference(rt->from);
1945         if (!from || !(rt->rt6i_flags & RTF_CACHE))
1946                 goto unlock;
1947
1948         if (from->nh) {
1949                 struct fib6_nh_match_arg arg = {
1950                         .dev = rt->dst.dev,
1951                         .gw = &rt->rt6i_gateway,
1952                 };
1953
1954                 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1955
1956                 if (!arg.match)
1957                         goto unlock;
1958                 fib6_nh = arg.match;
1959         } else {
1960                 fib6_nh = from->fib6_nh;
1961         }
1962         fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
1963 unlock:
1964         rcu_read_unlock();
1965 }
1966
1967 static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1968                                          struct rt6_info *rt, int mtu)
1969 {
1970         /* If the new MTU is lower than the route PMTU, this new MTU will be the
1971          * lowest MTU in the path: always allow updating the route PMTU to
1972          * reflect PMTU decreases.
1973          *
1974          * If the new MTU is higher, and the route PMTU is equal to the local
1975          * MTU, this means the old MTU is the lowest in the path, so allow
1976          * updating it: if other nodes now have lower MTUs, PMTU discovery will
1977          * handle this.
1978          */
1979
1980         if (dst_mtu(&rt->dst) >= mtu)
1981                 return true;
1982
1983         if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1984                 return true;
1985
1986         return false;
1987 }
1988
1989 static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
1990                                        const struct fib6_nh *nh, int mtu)
1991 {
1992         struct rt6_exception_bucket *bucket;
1993         struct rt6_exception *rt6_ex;
1994         int i;
1995
1996         bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1997         if (!bucket)
1998                 return;
1999
2000         for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2001                 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2002                         struct rt6_info *entry = rt6_ex->rt6i;
2003
2004                         /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
2005                          * route), the metrics of its rt->from have already
2006                          * been updated.
2007                          */
2008                         if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
2009                             rt6_mtu_change_route_allowed(idev, entry, mtu))
2010                                 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
2011                 }
2012                 bucket++;
2013         }
2014 }
2015
2016 #define RTF_CACHE_GATEWAY       (RTF_GATEWAY | RTF_CACHE)
2017
2018 static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2019                                             const struct in6_addr *gateway)
2020 {
2021         struct rt6_exception_bucket *bucket;
2022         struct rt6_exception *rt6_ex;
2023         struct hlist_node *tmp;
2024         int i;
2025
2026         if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2027                 return;
2028
2029         spin_lock_bh(&rt6_exception_lock);
2030         bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2031         if (bucket) {
2032                 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2033                         hlist_for_each_entry_safe(rt6_ex, tmp,
2034                                                   &bucket->chain, hlist) {
2035                                 struct rt6_info *entry = rt6_ex->rt6i;
2036
2037                                 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2038                                     RTF_CACHE_GATEWAY &&
2039                                     ipv6_addr_equal(gateway,
2040                                                     &entry->rt6i_gateway)) {
2041                                         rt6_remove_exception(bucket, rt6_ex);
2042                                 }
2043                         }
2044                         bucket++;
2045                 }
2046         }
2047
2048         spin_unlock_bh(&rt6_exception_lock);
2049 }
2050
2051 static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2052                                       struct rt6_exception *rt6_ex,
2053                                       struct fib6_gc_args *gc_args,
2054                                       unsigned long now)
2055 {
2056         struct rt6_info *rt = rt6_ex->rt6i;
2057
2058         /* we are pruning and obsoleting aged-out and non gateway exceptions
2059          * even if others have still references to them, so that on next
2060          * dst_check() such references can be dropped.
2061          * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2062          * expired, independently from their aging, as per RFC 8201 section 4
2063          */
2064         if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2065                 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2066                         RT6_TRACE("aging clone %p\n", rt);
2067                         rt6_remove_exception(bucket, rt6_ex);
2068                         return;
2069                 }
2070         } else if (time_after(jiffies, rt->dst.expires)) {
2071                 RT6_TRACE("purging expired route %p\n", rt);
2072                 rt6_remove_exception(bucket, rt6_ex);
2073                 return;
2074         }
2075
2076         if (rt->rt6i_flags & RTF_GATEWAY) {
2077                 struct neighbour *neigh;
2078                 __u8 neigh_flags = 0;
2079
2080                 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
2081                 if (neigh)
2082                         neigh_flags = neigh->flags;
2083
2084                 if (!(neigh_flags & NTF_ROUTER)) {
2085                         RT6_TRACE("purging route %p via non-router but gateway\n",
2086                                   rt);
2087                         rt6_remove_exception(bucket, rt6_ex);
2088                         return;
2089                 }
2090         }
2091
2092         gc_args->more++;
2093 }
2094
2095 static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
2096                                    struct fib6_gc_args *gc_args,
2097                                    unsigned long now)
2098 {
2099         struct rt6_exception_bucket *bucket;
2100         struct rt6_exception *rt6_ex;
2101         struct hlist_node *tmp;
2102         int i;
2103
2104         if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2105                 return;
2106
2107         rcu_read_lock_bh();
2108         spin_lock(&rt6_exception_lock);
2109         bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2110         if (bucket) {
2111                 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2112                         hlist_for_each_entry_safe(rt6_ex, tmp,
2113                                                   &bucket->chain, hlist) {
2114                                 rt6_age_examine_exception(bucket, rt6_ex,
2115                                                           gc_args, now);
2116                         }
2117                         bucket++;
2118                 }
2119         }
2120         spin_unlock(&rt6_exception_lock);
2121         rcu_read_unlock_bh();
2122 }
2123
2124 struct fib6_nh_age_excptn_arg {
2125         struct fib6_gc_args     *gc_args;
2126         unsigned long           now;
2127 };
2128
2129 static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2130 {
2131         struct fib6_nh_age_excptn_arg *arg = _arg;
2132
2133         fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2134         return 0;
2135 }
2136
2137 void rt6_age_exceptions(struct fib6_info *f6i,
2138                         struct fib6_gc_args *gc_args,
2139                         unsigned long now)
2140 {
2141         if (f6i->nh) {
2142                 struct fib6_nh_age_excptn_arg arg = {
2143                         .gc_args = gc_args,
2144                         .now = now
2145                 };
2146
2147                 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2148                                          &arg);
2149         } else {
2150                 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2151         }
2152 }
2153
2154 /* must be called with rcu lock held */
2155 int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2156                       struct flowi6 *fl6, struct fib6_result *res, int strict)
2157 {
2158         struct fib6_node *fn, *saved_fn;
2159
2160         fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2161         saved_fn = fn;
2162
2163         if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2164                 oif = 0;
2165
2166 redo_rt6_select:
2167         rt6_select(net, fn, oif, res, strict);
2168         if (res->f6i == net->ipv6.fib6_null_entry) {
2169                 fn = fib6_backtrack(fn, &fl6->saddr);
2170                 if (fn)
2171                         goto redo_rt6_select;
2172                 else if (strict & RT6_LOOKUP_F_REACHABLE) {
2173                         /* also consider unreachable route */
2174                         strict &= ~RT6_LOOKUP_F_REACHABLE;
2175                         fn = saved_fn;
2176                         goto redo_rt6_select;
2177                 }
2178         }
2179
2180         trace_fib6_table_lookup(net, res, table, fl6);
2181
2182         return 0;
2183 }
2184
2185 struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2186                                int oif, struct flowi6 *fl6,
2187                                const struct sk_buff *skb, int flags)
2188 {
2189         struct fib6_result res = {};
2190         struct rt6_info *rt = NULL;
2191         int strict = 0;
2192
2193         WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
2194                      !rcu_read_lock_held());
2195
2196         strict |= flags & RT6_LOOKUP_F_IFACE;
2197         strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
2198         if (net->ipv6.devconf_all->forwarding == 0)
2199                 strict |= RT6_LOOKUP_F_REACHABLE;
2200
2201         rcu_read_lock();
2202
2203         fib6_table_lookup(net, table, oif, fl6, &res, strict);
2204         if (res.f6i == net->ipv6.fib6_null_entry)
2205                 goto out;
2206
2207         fib6_select_path(net, &res, fl6, oif, false, skb, strict);
2208
2209         /*Search through exception table */
2210         rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
2211         if (rt) {
2212                 goto out;
2213         } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
2214                             !res.nh->fib_nh_gw_family)) {
2215                 /* Create a RTF_CACHE clone which will not be
2216                  * owned by the fib6 tree.  It is for the special case where
2217                  * the daddr in the skb during the neighbor look-up is different
2218                  * from the fl6->daddr used to look-up route here.
2219                  */
2220                 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
2221
2222                 if (rt) {
2223                         /* 1 refcnt is taken during ip6_rt_cache_alloc().
2224                          * As rt6_uncached_list_add() does not consume refcnt,
2225                          * this refcnt is always returned to the caller even
2226                          * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
2227                          */
2228                         rt6_uncached_list_add(rt);
2229                         atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
2230                         rcu_read_unlock();
2231
2232                         return rt;
2233                 }
2234         } else {
2235                 /* Get a percpu copy */
2236                 local_bh_disable();
2237                 rt = rt6_get_pcpu_route(&res);
2238
2239                 if (!rt)
2240                         rt = rt6_make_pcpu_route(net, &res);
2241
2242                 local_bh_enable();
2243         }
2244 out:
2245         if (!rt)
2246                 rt = net->ipv6.ip6_null_entry;
2247         if (!(flags & RT6_LOOKUP_F_DST_NOREF))
2248                 ip6_hold_safe(net, &rt);
2249         rcu_read_unlock();
2250
2251         return rt;
2252 }
2253 EXPORT_SYMBOL_GPL(ip6_pol_route);
2254
2255 static struct rt6_info *ip6_pol_route_input(struct net *net,
2256                                             struct fib6_table *table,
2257                                             struct flowi6 *fl6,
2258                                             const struct sk_buff *skb,
2259                                             int flags)
2260 {
2261         return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
2262 }
2263
2264 struct dst_entry *ip6_route_input_lookup(struct net *net,
2265                                          struct net_device *dev,
2266                                          struct flowi6 *fl6,
2267                                          const struct sk_buff *skb,
2268                                          int flags)
2269 {
2270         if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2271                 flags |= RT6_LOOKUP_F_IFACE;
2272
2273         return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
2274 }
2275 EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
2276
2277 static void ip6_multipath_l3_keys(const struct sk_buff *skb,
2278                                   struct flow_keys *keys,
2279                                   struct flow_keys *flkeys)
2280 {
2281         const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2282         const struct ipv6hdr *key_iph = outer_iph;
2283         struct flow_keys *_flkeys = flkeys;
2284         const struct ipv6hdr *inner_iph;
2285         const struct icmp6hdr *icmph;
2286         struct ipv6hdr _inner_iph;
2287         struct icmp6hdr _icmph;
2288
2289         if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2290                 goto out;
2291
2292         icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2293                                    sizeof(_icmph), &_icmph);
2294         if (!icmph)
2295                 goto out;
2296
2297         if (!icmpv6_is_err(icmph->icmp6_type))
2298                 goto out;
2299
2300         inner_iph = skb_header_pointer(skb,
2301                                        skb_transport_offset(skb) + sizeof(*icmph),
2302                                        sizeof(_inner_iph), &_inner_iph);
2303         if (!inner_iph)
2304                 goto out;
2305
2306         key_iph = inner_iph;
2307         _flkeys = NULL;
2308 out:
2309         if (_flkeys) {
2310                 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2311                 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2312                 keys->tags.flow_label = _flkeys->tags.flow_label;
2313                 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2314         } else {
2315                 keys->addrs.v6addrs.src = key_iph->saddr;
2316                 keys->addrs.v6addrs.dst = key_iph->daddr;
2317                 keys->tags.flow_label = ip6_flowlabel(key_iph);
2318                 keys->basic.ip_proto = key_iph->nexthdr;
2319         }
2320 }
2321
2322 /* if skb is set it will be used and fl6 can be NULL */
2323 u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2324                        const struct sk_buff *skb, struct flow_keys *flkeys)
2325 {
2326         struct flow_keys hash_keys;
2327         u32 mhash;
2328
2329         switch (ip6_multipath_hash_policy(net)) {
2330         case 0:
2331                 memset(&hash_keys, 0, sizeof(hash_keys));
2332                 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2333                 if (skb) {
2334                         ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2335                 } else {
2336                         hash_keys.addrs.v6addrs.src = fl6->saddr;
2337                         hash_keys.addrs.v6addrs.dst = fl6->daddr;
2338                         hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2339                         hash_keys.basic.ip_proto = fl6->flowi6_proto;
2340                 }
2341                 break;
2342         case 1:
2343                 if (skb) {
2344                         unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2345                         struct flow_keys keys;
2346
2347                         /* short-circuit if we already have L4 hash present */
2348                         if (skb->l4_hash)
2349                                 return skb_get_hash_raw(skb) >> 1;
2350
2351                         memset(&hash_keys, 0, sizeof(hash_keys));
2352
2353                         if (!flkeys) {
2354                                 skb_flow_dissect_flow_keys(skb, &keys, flag);
2355                                 flkeys = &keys;
2356                         }
2357                         hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2358                         hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2359                         hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2360                         hash_keys.ports.src = flkeys->ports.src;
2361                         hash_keys.ports.dst = flkeys->ports.dst;
2362                         hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2363                 } else {
2364                         memset(&hash_keys, 0, sizeof(hash_keys));
2365                         hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2366                         hash_keys.addrs.v6addrs.src = fl6->saddr;
2367                         hash_keys.addrs.v6addrs.dst = fl6->daddr;
2368                         hash_keys.ports.src = fl6->fl6_sport;
2369                         hash_keys.ports.dst = fl6->fl6_dport;
2370                         hash_keys.basic.ip_proto = fl6->flowi6_proto;
2371                 }
2372                 break;
2373         case 2:
2374                 memset(&hash_keys, 0, sizeof(hash_keys));
2375                 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2376                 if (skb) {
2377                         struct flow_keys keys;
2378
2379                         if (!flkeys) {
2380                                 skb_flow_dissect_flow_keys(skb, &keys, 0);
2381                                 flkeys = &keys;
2382                         }
2383
2384                         /* Inner can be v4 or v6 */
2385                         if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2386                                 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2387                                 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2388                                 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2389                         } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2390                                 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2391                                 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2392                                 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2393                                 hash_keys.tags.flow_label = flkeys->tags.flow_label;
2394                                 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2395                         } else {
2396                                 /* Same as case 0 */
2397                                 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2398                                 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2399                         }
2400                 } else {
2401                         /* Same as case 0 */
2402                         hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2403                         hash_keys.addrs.v6addrs.src = fl6->saddr;
2404                         hash_keys.addrs.v6addrs.dst = fl6->daddr;
2405                         hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2406                         hash_keys.basic.ip_proto = fl6->flowi6_proto;
2407                 }
2408                 break;
2409         }
2410         mhash = flow_hash_from_keys(&hash_keys);
2411
2412         return mhash >> 1;
2413 }
2414
2415 /* Called with rcu held */
2416 void ip6_route_input(struct sk_buff *skb)
2417 {
2418         const struct ipv6hdr *iph = ipv6_hdr(skb);
2419         struct net *net = dev_net(skb->dev);
2420         int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
2421         struct ip_tunnel_info *tun_info;
2422         struct flowi6 fl6 = {
2423                 .flowi6_iif = skb->dev->ifindex,
2424                 .daddr = iph->daddr,
2425                 .saddr = iph->saddr,
2426                 .flowlabel = ip6_flowinfo(iph),
2427                 .flowi6_mark = skb->mark,
2428                 .flowi6_proto = iph->nexthdr,
2429         };
2430         struct flow_keys *flkeys = NULL, _flkeys;
2431
2432         tun_info = skb_tunnel_info(skb);
2433         if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2434                 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
2435
2436         if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2437                 flkeys = &_flkeys;
2438
2439         if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2440                 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
2441         skb_dst_drop(skb);
2442         skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
2443                                                       &fl6, skb, flags));
2444 }
2445
2446 static struct rt6_info *ip6_pol_route_output(struct net *net,
2447                                              struct fib6_table *table,
2448                                              struct flowi6 *fl6,
2449                                              const struct sk_buff *skb,
2450                                              int flags)
2451 {
2452         return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2453 }
2454
2455 struct dst_entry *ip6_route_output_flags_noref(struct net *net,
2456                                                const struct sock *sk,
2457                                                struct flowi6 *fl6, int flags)
2458 {
2459         bool any_src;
2460
2461         if (ipv6_addr_type(&fl6->daddr) &
2462             (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
2463                 struct dst_entry *dst;
2464
2465                 /* This function does not take refcnt on the dst */
2466                 dst = l3mdev_link_scope_lookup(net, fl6);
2467                 if (dst)
2468                         return dst;
2469         }
2470
2471         fl6->flowi6_iif = LOOPBACK_IFINDEX;
2472
2473         flags |= RT6_LOOKUP_F_DST_NOREF;
2474         any_src = ipv6_addr_any(&fl6->saddr);
2475         if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2476             (fl6->flowi6_oif && any_src))
2477                 flags |= RT6_LOOKUP_F_IFACE;
2478
2479         if (!any_src)
2480                 flags |= RT6_LOOKUP_F_HAS_SADDR;
2481         else if (sk)
2482                 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2483
2484         return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
2485 }
2486 EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref);
2487
2488 struct dst_entry *ip6_route_output_flags(struct net *net,
2489                                          const struct sock *sk,
2490                                          struct flowi6 *fl6,
2491                                          int flags)
2492 {
2493         struct dst_entry *dst;
2494         struct rt6_info *rt6;
2495
2496         rcu_read_lock();
2497         dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
2498         rt6 = (struct rt6_info *)dst;
2499         /* For dst cached in uncached_list, refcnt is already taken. */
2500         if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) {
2501                 dst = &net->ipv6.ip6_null_entry->dst;
2502                 dst_hold(dst);
2503         }
2504         rcu_read_unlock();
2505
2506         return dst;
2507 }
2508 EXPORT_SYMBOL_GPL(ip6_route_output_flags);
2509
2510 struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
2511 {
2512         struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
2513         struct net_device *loopback_dev = net->loopback_dev;
2514         struct dst_entry *new = NULL;
2515
2516         rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
2517                        DST_OBSOLETE_DEAD, 0);
2518         if (rt) {
2519                 rt6_info_init(rt);
2520                 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
2521
2522                 new = &rt->dst;
2523                 new->__use = 1;
2524                 new->input = dst_discard;
2525                 new->output = dst_discard_out;
2526
2527                 dst_copy_metrics(new, &ort->dst);
2528
2529                 rt->rt6i_idev = in6_dev_get(loopback_dev);
2530                 rt->rt6i_gateway = ort->rt6i_gateway;
2531                 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
2532
2533                 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2534 #ifdef CONFIG_IPV6_SUBTREES
2535                 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2536 #endif
2537         }
2538
2539         dst_release(dst_orig);
2540         return new ? new : ERR_PTR(-ENOMEM);
2541 }
2542
2543 /*
2544  *      Destination cache support functions
2545  */
2546
2547 static bool fib6_check(struct fib6_info *f6i, u32 cookie)
2548 {
2549         u32 rt_cookie = 0;
2550
2551         if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
2552                 return false;
2553
2554         if (fib6_check_expired(f6i))
2555                 return false;
2556
2557         return true;
2558 }
2559
2560 static struct dst_entry *rt6_check(struct rt6_info *rt,
2561                                    struct fib6_info *from,
2562                                    u32 cookie)
2563 {
2564         u32 rt_cookie = 0;
2565
2566         if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
2567             rt_cookie != cookie)
2568                 return NULL;
2569
2570         if (rt6_check_expired(rt))
2571                 return NULL;
2572
2573         return &rt->dst;
2574 }
2575
2576 static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2577                                             struct fib6_info *from,
2578                                             u32 cookie)
2579 {
2580         if (!__rt6_check_expired(rt) &&
2581             rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2582             fib6_check(from, cookie))
2583                 return &rt->dst;
2584         else
2585                 return NULL;
2586 }
2587
2588 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2589 {
2590         struct dst_entry *dst_ret;
2591         struct fib6_info *from;
2592         struct rt6_info *rt;
2593
2594         rt = container_of(dst, struct rt6_info, dst);
2595
2596         rcu_read_lock();
2597
2598         /* All IPV6 dsts are created with ->obsolete set to the value
2599          * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2600          * into this function always.
2601          */
2602
2603         from = rcu_dereference(rt->from);
2604
2605         if (from && (rt->rt6i_flags & RTF_PCPU ||
2606             unlikely(!list_empty(&rt->rt6i_uncached))))
2607                 dst_ret = rt6_dst_from_check(rt, from, cookie);
2608         else
2609                 dst_ret = rt6_check(rt, from, cookie);
2610
2611         rcu_read_unlock();
2612
2613         return dst_ret;
2614 }
2615
2616 static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2617 {
2618         struct rt6_info *rt = (struct rt6_info *) dst;
2619
2620         if (rt) {
2621                 if (rt->rt6i_flags & RTF_CACHE) {
2622                         rcu_read_lock();
2623                         if (rt6_check_expired(rt)) {
2624                                 rt6_remove_exception_rt(rt);
2625                                 dst = NULL;
2626                         }
2627                         rcu_read_unlock();
2628                 } else {
2629                         dst_release(dst);
2630                         dst = NULL;
2631                 }
2632         }
2633         return dst;
2634 }
2635
2636 static void ip6_link_failure(struct sk_buff *skb)
2637 {
2638         struct rt6_info *rt;
2639
2640         icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
2641
2642         rt = (struct rt6_info *) skb_dst(skb);
2643         if (rt) {
2644                 rcu_read_lock();
2645                 if (rt->rt6i_flags & RTF_CACHE) {
2646                         rt6_remove_exception_rt(rt);
2647                 } else {
2648                         struct fib6_info *from;
2649                         struct fib6_node *fn;
2650
2651                         from = rcu_dereference(rt->from);
2652                         if (from) {
2653                                 fn = rcu_dereference(from->fib6_node);
2654                                 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2655                                         fn->fn_sernum = -1;
2656                         }
2657                 }
2658                 rcu_read_unlock();
2659         }
2660 }
2661
2662 static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2663 {
2664         if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2665                 struct fib6_info *from;
2666
2667                 rcu_read_lock();
2668                 from = rcu_dereference(rt0->from);
2669                 if (from)
2670                         rt0->dst.expires = from->expires;
2671                 rcu_read_unlock();
2672         }
2673
2674         dst_set_expires(&rt0->dst, timeout);
2675         rt0->rt6i_flags |= RTF_EXPIRES;
2676 }
2677
2678 static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2679 {
2680         struct net *net = dev_net(rt->dst.dev);
2681
2682         dst_metric_set(&rt->dst, RTAX_MTU, mtu);
2683         rt->rt6i_flags |= RTF_MODIFIED;
2684         rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2685 }
2686
2687 static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2688 {
2689         return !(rt->rt6i_flags & RTF_CACHE) &&
2690                 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
2691 }
2692
2693 static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2694                                  const struct ipv6hdr *iph, u32 mtu,
2695                                  bool confirm_neigh)
2696 {
2697         const struct in6_addr *daddr, *saddr;
2698         struct rt6_info *rt6 = (struct rt6_info *)dst;
2699
2700         if (dst_metric_locked(dst, RTAX_MTU))
2701                 return;
2702
2703         if (iph) {
2704                 daddr = &iph->daddr;
2705                 saddr = &iph->saddr;
2706         } else if (sk) {
2707                 daddr = &sk->sk_v6_daddr;
2708                 saddr = &inet6_sk(sk)->saddr;
2709         } else {
2710                 daddr = NULL;
2711                 saddr = NULL;
2712         }
2713
2714         if (confirm_neigh)
2715                 dst_confirm_neigh(dst, daddr);
2716
2717         mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2718         if (mtu >= dst_mtu(dst))
2719                 return;
2720
2721         if (!rt6_cache_allowed_for_pmtu(rt6)) {
2722                 rt6_do_update_pmtu(rt6, mtu);
2723                 /* update rt6_ex->stamp for cache */
2724                 if (rt6->rt6i_flags & RTF_CACHE)
2725                         rt6_update_exception_stamp_rt(rt6);
2726         } else if (daddr) {
2727                 struct fib6_result res = {};
2728                 struct rt6_info *nrt6;
2729
2730                 rcu_read_lock();
2731                 res.f6i = rcu_dereference(rt6->from);
2732                 if (!res.f6i)
2733                         goto out_unlock;
2734
2735                 res.fib6_flags = res.f6i->fib6_flags;
2736                 res.fib6_type = res.f6i->fib6_type;
2737
2738                 if (res.f6i->nh) {
2739                         struct fib6_nh_match_arg arg = {
2740                                 .dev = dst->dev,
2741                                 .gw = &rt6->rt6i_gateway,
2742                         };
2743
2744                         nexthop_for_each_fib6_nh(res.f6i->nh,
2745                                                  fib6_nh_find_match, &arg);
2746
2747                         /* fib6_info uses a nexthop that does not have fib6_nh
2748                          * using the dst->dev + gw. Should be impossible.
2749                          */
2750                         if (!arg.match)
2751                                 goto out_unlock;
2752
2753                         res.nh = arg.match;
2754                 } else {
2755                         res.nh = res.f6i->fib6_nh;
2756                 }
2757
2758                 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
2759                 if (nrt6) {
2760                         rt6_do_update_pmtu(nrt6, mtu);
2761                         if (rt6_insert_exception(nrt6, &res))
2762                                 dst_release_immediate(&nrt6->dst);
2763                 }
2764 out_unlock:
2765                 rcu_read_unlock();
2766         }
2767 }
2768
2769 static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2770                                struct sk_buff *skb, u32 mtu,
2771                                bool confirm_neigh)
2772 {
2773         __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2774                              confirm_neigh);
2775 }
2776
2777 void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2778                      int oif, u32 mark, kuid_t uid)
2779 {
2780         const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2781         struct dst_entry *dst;
2782         struct flowi6 fl6 = {
2783                 .flowi6_oif = oif,
2784                 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2785                 .daddr = iph->daddr,
2786                 .saddr = iph->saddr,
2787                 .flowlabel = ip6_flowinfo(iph),
2788                 .flowi6_uid = uid,
2789         };
2790
2791         dst = ip6_route_output(net, NULL, &fl6);
2792         if (!dst->error)
2793                 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
2794         dst_release(dst);
2795 }
2796 EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2797
2798 void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2799 {
2800         int oif = sk->sk_bound_dev_if;
2801         struct dst_entry *dst;
2802
2803         if (!oif && skb->dev)
2804                 oif = l3mdev_master_ifindex(skb->dev);
2805
2806         ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
2807
2808         dst = __sk_dst_get(sk);
2809         if (!dst || !dst->obsolete ||
2810             dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2811                 return;
2812
2813         bh_lock_sock(sk);
2814         if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2815                 ip6_datagram_dst_update(sk, false);
2816         bh_unlock_sock(sk);
2817 }
2818 EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2819
2820 void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2821                            const struct flowi6 *fl6)
2822 {
2823 #ifdef CONFIG_IPV6_SUBTREES
2824         struct ipv6_pinfo *np = inet6_sk(sk);
2825 #endif
2826
2827         ip6_dst_store(sk, dst,
2828                       ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2829                       &sk->sk_v6_daddr : NULL,
2830 #ifdef CONFIG_IPV6_SUBTREES
2831                       ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2832                       &np->saddr :
2833 #endif
2834                       NULL);
2835 }
2836
2837 static bool ip6_redirect_nh_match(const struct fib6_result *res,
2838                                   struct flowi6 *fl6,
2839                                   const struct in6_addr *gw,
2840                                   struct rt6_info **ret)
2841 {
2842         const struct fib6_nh *nh = res->nh;
2843
2844         if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
2845             fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
2846                 return false;
2847
2848         /* rt_cache's gateway might be different from its 'parent'
2849          * in the case of an ip redirect.
2850          * So we keep searching in the exception table if the gateway
2851          * is different.
2852          */
2853         if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
2854                 struct rt6_info *rt_cache;
2855
2856                 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
2857                 if (rt_cache &&
2858                     ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
2859                         *ret = rt_cache;
2860                         return true;
2861                 }
2862                 return false;
2863         }
2864         return true;
2865 }
2866
2867 struct fib6_nh_rd_arg {
2868         struct fib6_result      *res;
2869         struct flowi6           *fl6;
2870         const struct in6_addr   *gw;
2871         struct rt6_info         **ret;
2872 };
2873
2874 static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
2875 {
2876         struct fib6_nh_rd_arg *arg = _arg;
2877
2878         arg->res->nh = nh;
2879         return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
2880 }
2881
2882 /* Handle redirects */
2883 struct ip6rd_flowi {
2884         struct flowi6 fl6;
2885         struct in6_addr gateway;
2886 };
2887
2888 static struct rt6_info *__ip6_route_redirect(struct net *net,
2889                                              struct fib6_table *table,
2890                                              struct flowi6 *fl6,
2891                                              const struct sk_buff *skb,
2892                                              int flags)
2893 {
2894         struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
2895         struct rt6_info *ret = NULL;
2896         struct fib6_result res = {};
2897         struct fib6_nh_rd_arg arg = {
2898                 .res = &res,
2899                 .fl6 = fl6,
2900                 .gw  = &rdfl->gateway,
2901                 .ret = &ret
2902         };
2903         struct fib6_info *rt;
2904         struct fib6_node *fn;
2905
2906         /* l3mdev_update_flow overrides oif if the device is enslaved; in
2907          * this case we must match on the real ingress device, so reset it
2908          */
2909         if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2910                 fl6->flowi6_oif = skb->dev->ifindex;
2911
2912         /* Get the "current" route for this destination and
2913          * check if the redirect has come from appropriate router.
2914          *
2915          * RFC 4861 specifies that redirects should only be
2916          * accepted if they come from the nexthop to the target.
2917          * Due to the way the routes are chosen, this notion
2918          * is a bit fuzzy and one might need to check all possible
2919          * routes.
2920          */
2921
2922         rcu_read_lock();
2923         fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2924 restart:
2925         for_each_fib6_node_rt_rcu(fn) {
2926                 res.f6i = rt;
2927                 if (fib6_check_expired(rt))
2928                         continue;
2929                 if (rt->fib6_flags & RTF_REJECT)
2930                         break;
2931                 if (unlikely(rt->nh)) {
2932                         if (nexthop_is_blackhole(rt->nh))
2933                                 continue;
2934                         /* on match, res->nh is filled in and potentially ret */
2935                         if (nexthop_for_each_fib6_nh(rt->nh,
2936                                                      fib6_nh_redirect_match,
2937                                                      &arg))
2938                                 goto out;
2939                 } else {
2940                         res.nh = rt->fib6_nh;
2941                         if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
2942                                                   &ret))
2943                                 goto out;
2944                 }
2945         }
2946
2947         if (!rt)
2948                 rt = net->ipv6.fib6_null_entry;
2949         else if (rt->fib6_flags & RTF_REJECT) {
2950                 ret = net->ipv6.ip6_null_entry;
2951                 goto out;
2952         }
2953
2954         if (rt == net->ipv6.fib6_null_entry) {
2955                 fn = fib6_backtrack(fn, &fl6->saddr);
2956                 if (fn)
2957                         goto restart;
2958         }
2959
2960         res.f6i = rt;
2961         res.nh = rt->fib6_nh;
2962 out:
2963         if (ret) {
2964                 ip6_hold_safe(net, &ret);
2965         } else {
2966                 res.fib6_flags = res.f6i->fib6_flags;
2967                 res.fib6_type = res.f6i->fib6_type;
2968                 ret = ip6_create_rt_rcu(&res);
2969         }
2970
2971         rcu_read_unlock();
2972
2973         trace_fib6_table_lookup(net, &res, table, fl6);
2974         return ret;
2975 };
2976
2977 static struct dst_entry *ip6_route_redirect(struct net *net,
2978                                             const struct flowi6 *fl6,
2979                                             const struct sk_buff *skb,
2980                                             const struct in6_addr *gateway)
2981 {
2982         int flags = RT6_LOOKUP_F_HAS_SADDR;
2983         struct ip6rd_flowi rdfl;
2984
2985         rdfl.fl6 = *fl6;
2986         rdfl.gateway = *gateway;
2987
2988         return fib6_rule_lookup(net, &rdfl.fl6, skb,
2989                                 flags, __ip6_route_redirect);
2990 }
2991
2992 void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2993                   kuid_t uid)
2994 {
2995         const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2996         struct dst_entry *dst;
2997         struct flowi6 fl6 = {
2998                 .flowi6_iif = LOOPBACK_IFINDEX,
2999                 .flowi6_oif = oif,
3000                 .flowi6_mark = mark,
3001                 .daddr = iph->daddr,
3002                 .saddr = iph->saddr,
3003                 .flowlabel = ip6_flowinfo(iph),
3004                 .flowi6_uid = uid,
3005         };
3006
3007         dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
3008         rt6_do_redirect(dst, NULL, skb);
3009         dst_release(dst);
3010 }
3011 EXPORT_SYMBOL_GPL(ip6_redirect);
3012
3013 void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
3014 {
3015         const struct ipv6hdr *iph = ipv6_hdr(skb);
3016         const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3017         struct dst_entry *dst;
3018         struct flowi6 fl6 = {
3019                 .flowi6_iif = LOOPBACK_IFINDEX,
3020                 .flowi6_oif = oif,
3021                 .daddr = msg->dest,
3022                 .saddr = iph->daddr,
3023                 .flowi6_uid = sock_net_uid(net, NULL),
3024         };
3025
3026         dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
3027         rt6_do_redirect(dst, NULL, skb);
3028         dst_release(dst);
3029 }
3030
3031 void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
3032 {
3033         ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
3034                      sk->sk_uid);
3035 }
3036 EXPORT_SYMBOL_GPL(ip6_sk_redirect);
3037
3038 static unsigned int ip6_default_advmss(const struct dst_entry *dst)
3039 {
3040         struct net_device *dev = dst->dev;
3041         unsigned int mtu = dst_mtu(dst);
3042         struct net *net = dev_net(dev);
3043
3044         mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
3045
3046         if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
3047                 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
3048
3049         /*
3050          * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3051          * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3052          * IPV6_MAXPLEN is also valid and means: "any MSS,
3053          * rely only on pmtu discovery"
3054          */
3055         if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
3056                 mtu = IPV6_MAXPLEN;
3057         return mtu;
3058 }
3059
3060 static unsigned int ip6_mtu(const struct dst_entry *dst)
3061 {
3062         struct inet6_dev *idev;
3063         unsigned int mtu;
3064
3065         mtu = dst_metric_raw(dst, RTAX_MTU);
3066         if (mtu)
3067                 goto out;
3068
3069         mtu = IPV6_MIN_MTU;
3070
3071         rcu_read_lock();
3072         idev = __in6_dev_get(dst->dev);
3073         if (idev)
3074                 mtu = idev->cnf.mtu6;
3075         rcu_read_unlock();
3076
3077 out:
3078         mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3079
3080         return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
3081 }
3082
3083 /* MTU selection:
3084  * 1. mtu on route is locked - use it
3085  * 2. mtu from nexthop exception
3086  * 3. mtu from egress device
3087  *
3088  * based on ip6_dst_mtu_forward and exception logic of
3089  * rt6_find_cached_rt; called with rcu_read_lock
3090  */
3091 u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3092                       const struct in6_addr *daddr,
3093                       const struct in6_addr *saddr)
3094 {
3095         const struct fib6_nh *nh = res->nh;
3096         struct fib6_info *f6i = res->f6i;
3097         struct inet6_dev *idev;
3098         struct rt6_info *rt;
3099         u32 mtu = 0;
3100
3101         if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3102                 mtu = f6i->fib6_pmtu;
3103                 if (mtu)
3104                         goto out;
3105         }
3106
3107         rt = rt6_find_cached_rt(res, daddr, saddr);
3108         if (unlikely(rt)) {
3109                 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3110         } else {
3111                 struct net_device *dev = nh->fib_nh_dev;
3112
3113                 mtu = IPV6_MIN_MTU;
3114                 idev = __in6_dev_get(dev);
3115                 if (idev && idev->cnf.mtu6 > mtu)
3116                         mtu = idev->cnf.mtu6;
3117         }
3118
3119         mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3120 out:
3121         return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
3122 }
3123
3124 struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
3125                                   struct flowi6 *fl6)
3126 {
3127         struct dst_entry *dst;
3128         struct rt6_info *rt;
3129         struct inet6_dev *idev = in6_dev_get(dev);
3130         struct net *net = dev_net(dev);
3131
3132         if (unlikely(!idev))
3133                 return ERR_PTR(-ENODEV);
3134
3135         rt = ip6_dst_alloc(net, dev, 0);
3136         if (unlikely(!rt)) {
3137                 in6_dev_put(idev);
3138                 dst = ERR_PTR(-ENOMEM);
3139                 goto out;
3140         }
3141
3142         rt->dst.input = ip6_input;
3143         rt->dst.output  = ip6_output;
3144         rt->rt6i_gateway  = fl6->daddr;
3145         rt->rt6i_dst.addr = fl6->daddr;
3146         rt->rt6i_dst.plen = 128;
3147         rt->rt6i_idev     = idev;
3148         dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
3149
3150         /* Add this dst into uncached_list so that rt6_disable_ip() can
3151          * do proper release of the net_device
3152          */
3153         rt6_uncached_list_add(rt);
3154         atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
3155
3156         dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3157
3158 out:
3159         return dst;
3160 }
3161
3162 static int ip6_dst_gc(struct dst_ops *ops)
3163 {
3164         struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
3165         int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3166         int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
3167         int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3168         int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3169         unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
3170         int entries;
3171
3172         entries = dst_entries_get_fast(ops);
3173         if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
3174             entries <= rt_max_size)
3175                 goto out;
3176
3177         net->ipv6.ip6_rt_gc_expire++;
3178         fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
3179         entries = dst_entries_get_slow(ops);
3180         if (entries < ops->gc_thresh)
3181                 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
3182 out:
3183         net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
3184         return entries > rt_max_size;
3185 }
3186
3187 static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3188                                const struct in6_addr *gw_addr, u32 tbid,
3189                                int flags, struct fib6_result *res)
3190 {
3191         struct flowi6 fl6 = {
3192                 .flowi6_oif = cfg->fc_ifindex,
3193                 .daddr = *gw_addr,
3194                 .saddr = cfg->fc_prefsrc,
3195         };
3196         struct fib6_table *table;
3197         int err;
3198
3199         table = fib6_get_table(net, tbid);
3200         if (!table)
3201                 return -EINVAL;
3202
3203         if (!ipv6_addr_any(&cfg->fc_prefsrc))
3204                 flags |= RT6_LOOKUP_F_HAS_SADDR;
3205
3206         flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
3207
3208         err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3209         if (!err && res->f6i != net->ipv6.fib6_null_entry)
3210                 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3211                                  cfg->fc_ifindex != 0, NULL, flags);
3212
3213         return err;
3214 }
3215
3216 static int ip6_route_check_nh_onlink(struct net *net,
3217                                      struct fib6_config *cfg,
3218                                      const struct net_device *dev,
3219                                      struct netlink_ext_ack *extack)
3220 {
3221         u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
3222         const struct in6_addr *gw_addr = &cfg->fc_gateway;
3223         struct fib6_result res = {};
3224         int err;
3225
3226         err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3227         if (!err && !(res.fib6_flags & RTF_REJECT) &&
3228             /* ignore match if it is the default route */
3229             !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3230             (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
3231                 NL_SET_ERR_MSG(extack,
3232                                "Nexthop has invalid gateway or device mismatch");
3233                 err = -EINVAL;
3234         }
3235
3236         return err;
3237 }
3238
3239 static int ip6_route_check_nh(struct net *net,
3240                               struct fib6_config *cfg,
3241                               struct net_device **_dev,
3242                               struct inet6_dev **idev)
3243 {
3244         const struct in6_addr *gw_addr = &cfg->fc_gateway;
3245         struct net_device *dev = _dev ? *_dev : NULL;
3246         int flags = RT6_LOOKUP_F_IFACE;
3247         struct fib6_result res = {};
3248         int err = -EHOSTUNREACH;
3249
3250         if (cfg->fc_table) {
3251                 err = ip6_nh_lookup_table(net, cfg, gw_addr,
3252                                           cfg->fc_table, flags, &res);
3253                 /* gw_addr can not require a gateway or resolve to a reject
3254                  * route. If a device is given, it must match the result.
3255                  */
3256                 if (err || res.fib6_flags & RTF_REJECT ||
3257                     res.nh->fib_nh_gw_family ||
3258                     (dev && dev != res.nh->fib_nh_dev))
3259                         err = -EHOSTUNREACH;
3260         }
3261
3262         if (err < 0) {
3263                 struct flowi6 fl6 = {
3264                         .flowi6_oif = cfg->fc_ifindex,
3265                         .daddr = *gw_addr,
3266                 };
3267
3268                 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3269                 if (err || res.fib6_flags & RTF_REJECT ||
3270                     res.nh->fib_nh_gw_family)
3271                         err = -EHOSTUNREACH;
3272
3273                 if (err)
3274                         return err;
3275
3276                 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3277                                  cfg->fc_ifindex != 0, NULL, flags);
3278         }
3279
3280         err = 0;
3281         if (dev) {
3282                 if (dev != res.nh->fib_nh_dev)
3283                         err = -EHOSTUNREACH;
3284         } else {
3285                 *_dev = dev = res.nh->fib_nh_dev;
3286                 dev_hold(dev);
3287                 *idev = in6_dev_get(dev);
3288         }
3289
3290         return err;
3291 }
3292
3293 static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3294                            struct net_device **_dev, struct inet6_dev **idev,
3295                            struct netlink_ext_ack *extack)
3296 {
3297         const struct in6_addr *gw_addr = &cfg->fc_gateway;
3298         int gwa_type = ipv6_addr_type(gw_addr);
3299         bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
3300         const struct net_device *dev = *_dev;
3301         bool need_addr_check = !dev;
3302         int err = -EINVAL;
3303
3304         /* if gw_addr is local we will fail to detect this in case
3305          * address is still TENTATIVE (DAD in progress). rt6_lookup()
3306          * will return already-added prefix route via interface that
3307          * prefix route was assigned to, which might be non-loopback.
3308          */
3309         if (dev &&
3310             ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3311                 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3312                 goto out;
3313         }
3314
3315         if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3316                 /* IPv6 strictly inhibits using not link-local
3317                  * addresses as nexthop address.
3318                  * Otherwise, router will not able to send redirects.
3319                  * It is very good, but in some (rare!) circumstances
3320                  * (SIT, PtP, NBMA NOARP links) it is handy to allow
3321                  * some exceptions. --ANK
3322                  * We allow IPv4-mapped nexthops to support RFC4798-type
3323                  * addressing
3324                  */
3325                 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3326                         NL_SET_ERR_MSG(extack, "Invalid gateway address");
3327                         goto out;
3328                 }
3329
3330                 rcu_read_lock();
3331
3332                 if (cfg->fc_flags & RTNH_F_ONLINK)
3333                         err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3334                 else
3335                         err = ip6_route_check_nh(net, cfg, _dev, idev);
3336
3337                 rcu_read_unlock();
3338
3339                 if (err)
3340                         goto out;
3341         }
3342
3343         /* reload in case device was changed */
3344         dev = *_dev;
3345
3346         err = -EINVAL;
3347         if (!dev) {
3348                 NL_SET_ERR_MSG(extack, "Egress device not specified");
3349                 goto out;
3350         } else if (dev->flags & IFF_LOOPBACK) {
3351                 NL_SET_ERR_MSG(extack,
3352                                "Egress device can not be loopback device for this route");
3353                 goto out;
3354         }
3355
3356         /* if we did not check gw_addr above, do so now that the
3357          * egress device has been resolved.
3358          */
3359         if (need_addr_check &&
3360             ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3361                 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3362                 goto out;
3363         }
3364
3365         err = 0;
3366 out:
3367         return err;
3368 }
3369
3370 static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3371 {
3372         if ((flags & RTF_REJECT) ||
3373             (dev && (dev->flags & IFF_LOOPBACK) &&
3374              !(addr_type & IPV6_ADDR_LOOPBACK) &&
3375              !(flags & RTF_LOCAL)))
3376                 return true;
3377
3378         return false;
3379 }
3380
3381 int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3382                  struct fib6_config *cfg, gfp_t gfp_flags,
3383                  struct netlink_ext_ack *extack)
3384 {
3385         struct net_device *dev = NULL;
3386         struct inet6_dev *idev = NULL;
3387         int addr_type;
3388         int err;
3389
3390         fib6_nh->fib_nh_family = AF_INET6;
3391 #ifdef CONFIG_IPV6_ROUTER_PREF
3392         fib6_nh->last_probe = jiffies;
3393 #endif
3394
3395         err = -ENODEV;
3396         if (cfg->fc_ifindex) {
3397                 dev = dev_get_by_index(net, cfg->fc_ifindex);
3398                 if (!dev)
3399                         goto out;
3400                 idev = in6_dev_get(dev);
3401                 if (!idev)
3402                         goto out;
3403         }
3404
3405         if (cfg->fc_flags & RTNH_F_ONLINK) {
3406                 if (!dev) {
3407                         NL_SET_ERR_MSG(extack,
3408                                        "Nexthop device required for onlink");
3409                         goto out;
3410                 }
3411
3412                 if (!(dev->flags & IFF_UP)) {
3413                         NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3414                         err = -ENETDOWN;
3415                         goto out;
3416                 }
3417
3418                 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
3419         }
3420
3421         fib6_nh->fib_nh_weight = 1;
3422
3423         /* We cannot add true routes via loopback here,
3424          * they would result in kernel looping; promote them to reject routes
3425          */
3426         addr_type = ipv6_addr_type(&cfg->fc_dst);
3427         if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3428                 /* hold loopback dev/idev if we haven't done so. */
3429                 if (dev != net->loopback_dev) {
3430                         if (dev) {
3431                                 dev_put(dev);
3432                                 in6_dev_put(idev);
3433                         }
3434                         dev = net->loopback_dev;
3435                         dev_hold(dev);
3436                         idev = in6_dev_get(dev);
3437                         if (!idev) {
3438                                 err = -ENODEV;
3439                                 goto out;
3440                         }
3441                 }
3442                 goto pcpu_alloc;
3443         }
3444
3445         if (cfg->fc_flags & RTF_GATEWAY) {
3446                 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3447                 if (err)
3448                         goto out;
3449
3450                 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3451                 fib6_nh->fib_nh_gw_family = AF_INET6;
3452         }
3453
3454         err = -ENODEV;
3455         if (!dev)
3456                 goto out;
3457
3458         if (idev->cnf.disable_ipv6) {
3459                 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3460                 err = -EACCES;
3461                 goto out;
3462         }
3463
3464         if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3465                 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3466                 err = -ENETDOWN;
3467                 goto out;
3468         }
3469
3470         if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3471             !netif_carrier_ok(dev))
3472                 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
3473
3474         err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
3475                                  cfg->fc_encap_type, cfg, gfp_flags, extack);
3476         if (err)
3477                 goto out;
3478
3479 pcpu_alloc:
3480         fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3481         if (!fib6_nh->rt6i_pcpu) {
3482                 err = -ENOMEM;
3483                 goto out;
3484         }
3485
3486         fib6_nh->fib_nh_dev = dev;
3487         fib6_nh->fib_nh_oif = dev->ifindex;
3488         err = 0;
3489 out:
3490         if (idev)
3491                 in6_dev_put(idev);
3492
3493         if (err) {
3494                 lwtstate_put(fib6_nh->fib_nh_lws);
3495                 fib6_nh->fib_nh_lws = NULL;
3496                 if (dev)
3497                         dev_put(dev);
3498         }
3499
3500         return err;
3501 }
3502
3503 void fib6_nh_release(struct fib6_nh *fib6_nh)
3504 {
3505         struct rt6_exception_bucket *bucket;
3506
3507         rcu_read_lock();
3508
3509         fib6_nh_flush_exceptions(fib6_nh, NULL);
3510         bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3511         if (bucket) {
3512                 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3513                 kfree(bucket);
3514         }
3515
3516         rcu_read_unlock();
3517
3518         if (fib6_nh->rt6i_pcpu) {
3519                 int cpu;
3520
3521                 for_each_possible_cpu(cpu) {
3522                         struct rt6_info **ppcpu_rt;
3523                         struct rt6_info *pcpu_rt;
3524
3525                         ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3526                         pcpu_rt = *ppcpu_rt;
3527                         if (pcpu_rt) {
3528                                 dst_dev_put(&pcpu_rt->dst);
3529                                 dst_release(&pcpu_rt->dst);
3530                                 *ppcpu_rt = NULL;
3531                         }
3532                 }
3533
3534                 free_percpu(fib6_nh->rt6i_pcpu);
3535         }
3536
3537         fib_nh_common_release(&fib6_nh->nh_common);
3538 }
3539
3540 static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
3541                                               gfp_t gfp_flags,
3542                                               struct netlink_ext_ack *extack)
3543 {
3544         struct net *net = cfg->fc_nlinfo.nl_net;
3545         struct fib6_info *rt = NULL;
3546         struct nexthop *nh = NULL;
3547         struct fib6_table *table;
3548         struct fib6_nh *fib6_nh;
3549         int err = -EINVAL;
3550         int addr_type;
3551
3552         /* RTF_PCPU is an internal flag; can not be set by userspace */
3553         if (cfg->fc_flags & RTF_PCPU) {
3554                 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
3555                 goto out;
3556         }
3557
3558         /* RTF_CACHE is an internal flag; can not be set by userspace */
3559         if (cfg->fc_flags & RTF_CACHE) {
3560                 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3561                 goto out;
3562         }
3563
3564         if (cfg->fc_type > RTN_MAX) {
3565                 NL_SET_ERR_MSG(extack, "Invalid route type");
3566                 goto out;
3567         }
3568
3569         if (cfg->fc_dst_len > 128) {
3570                 NL_SET_ERR_MSG(extack, "Invalid prefix length");
3571                 goto out;
3572         }
3573         if (cfg->fc_src_len > 128) {
3574                 NL_SET_ERR_MSG(extack, "Invalid source address length");
3575                 goto out;
3576         }
3577 #ifndef CONFIG_IPV6_SUBTREES
3578         if (cfg->fc_src_len) {
3579                 NL_SET_ERR_MSG(extack,
3580                                "Specifying source address requires IPV6_SUBTREES to be enabled");
3581                 goto out;
3582         }
3583 #endif
3584         if (cfg->fc_nh_id) {
3585                 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3586                 if (!nh) {
3587                         NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
3588                         goto out;
3589                 }
3590                 err = fib6_check_nexthop(nh, cfg, extack);
3591                 if (err)
3592                         goto out;
3593         }
3594
3595         err = -ENOBUFS;
3596         if (cfg->fc_nlinfo.nlh &&
3597             !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
3598                 table = fib6_get_table(net, cfg->fc_table);
3599                 if (!table) {
3600                         pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3601                         table = fib6_new_table(net, cfg->fc_table);
3602                 }
3603         } else {
3604                 table = fib6_new_table(net, cfg->fc_table);
3605         }
3606
3607         if (!table)
3608                 goto out;
3609
3610         err = -ENOMEM;
3611         rt = fib6_info_alloc(gfp_flags, !nh);
3612         if (!rt)
3613                 goto out;
3614
3615         rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3616                                                extack);
3617         if (IS_ERR(rt->fib6_metrics)) {
3618                 err = PTR_ERR(rt->fib6_metrics);
3619                 /* Do not leave garbage there. */
3620                 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
3621                 goto out;
3622         }
3623
3624         if (cfg->fc_flags & RTF_ADDRCONF)
3625                 rt->dst_nocount = true;
3626
3627         if (cfg->fc_flags & RTF_EXPIRES)
3628                 fib6_set_expires(rt, jiffies +
3629                                 clock_t_to_jiffies(cfg->fc_expires));
3630         else
3631                 fib6_clean_expires(rt);
3632
3633         if (cfg->fc_protocol == RTPROT_UNSPEC)
3634                 cfg->fc_protocol = RTPROT_BOOT;
3635         rt->fib6_protocol = cfg->fc_protocol;
3636
3637         rt->fib6_table = table;
3638         rt->fib6_metric = cfg->fc_metric;
3639         rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
3640         rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
3641
3642         ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3643         rt->fib6_dst.plen = cfg->fc_dst_len;
3644
3645 #ifdef CONFIG_IPV6_SUBTREES
3646         ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3647         rt->fib6_src.plen = cfg->fc_src_len;
3648 #endif
3649         if (nh) {
3650                 if (!nexthop_get(nh)) {
3651                         NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3652                         goto out;
3653                 }
3654                 if (rt->fib6_src.plen) {
3655                         NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
3656                         goto out;
3657                 }
3658                 rt->nh = nh;
3659                 fib6_nh = nexthop_fib6_nh(rt->nh);
3660         } else {
3661                 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3662                 if (err)
3663                         goto out;
3664
3665                 fib6_nh = rt->fib6_nh;
3666
3667                 /* We cannot add true routes via loopback here, they would
3668                  * result in kernel looping; promote them to reject routes
3669                  */
3670                 addr_type = ipv6_addr_type(&cfg->fc_dst);
3671                 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3672                                    addr_type))
3673                         rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3674         }
3675
3676         if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
3677                 struct net_device *dev = fib6_nh->fib_nh_dev;
3678
3679                 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3680                         NL_SET_ERR_MSG(extack, "Invalid source address");
3681                         err = -EINVAL;
3682                         goto out;
3683                 }
3684                 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3685                 rt->fib6_prefsrc.plen = 128;
3686         } else
3687                 rt->fib6_prefsrc.plen = 0;
3688
3689         return rt;
3690 out:
3691         fib6_info_release(rt);
3692         return ERR_PTR(err);
3693 }
3694
3695 int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3696                   struct netlink_ext_ack *extack)
3697 {
3698         struct fib6_info *rt;
3699         int err;
3700
3701         rt = ip6_route_info_create(cfg, gfp_flags, extack);
3702         if (IS_ERR(rt))
3703                 return PTR_ERR(rt);
3704
3705         err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
3706         fib6_info_release(rt);
3707
3708         return err;
3709 }
3710
3711 static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
3712 {
3713         struct net *net = info->nl_net;
3714         struct fib6_table *table;
3715         int err;
3716
3717         if (rt == net->ipv6.fib6_null_entry) {
3718                 err = -ENOENT;
3719                 goto out;
3720         }
3721
3722         table = rt->fib6_table;
3723         spin_lock_bh(&table->tb6_lock);
3724         err = fib6_del(rt, info);
3725         spin_unlock_bh(&table->tb6_lock);
3726
3727 out:
3728         fib6_info_release(rt);
3729         return err;
3730 }
3731
3732 int ip6_del_rt(struct net *net, struct fib6_info *rt)
3733 {
3734         struct nl_info info = { .nl_net = net };
3735
3736         return __ip6_del_rt(rt, &info);
3737 }
3738
3739 static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
3740 {
3741         struct nl_info *info = &cfg->fc_nlinfo;
3742         struct net *net = info->nl_net;
3743         struct sk_buff *skb = NULL;
3744         struct fib6_table *table;
3745         int err = -ENOENT;
3746
3747         if (rt == net->ipv6.fib6_null_entry)
3748                 goto out_put;
3749         table = rt->fib6_table;
3750         spin_lock_bh(&table->tb6_lock);
3751
3752         if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
3753                 struct fib6_info *sibling, *next_sibling;
3754                 struct fib6_node *fn;
3755
3756                 /* prefer to send a single notification with all hops */
3757                 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3758                 if (skb) {
3759                         u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3760
3761                         if (rt6_fill_node(net, skb, rt, NULL,
3762                                           NULL, NULL, 0, RTM_DELROUTE,
3763                                           info->portid, seq, 0) < 0) {
3764                                 kfree_skb(skb);
3765                                 skb = NULL;
3766                         } else
3767                                 info->skip_notify = 1;
3768                 }
3769
3770                 /* 'rt' points to the first sibling route. If it is not the
3771                  * leaf, then we do not need to send a notification. Otherwise,
3772                  * we need to check if the last sibling has a next route or not
3773                  * and emit a replace or delete notification, respectively.
3774                  */
3775                 info->skip_notify_kernel = 1;
3776                 fn = rcu_dereference_protected(rt->fib6_node,
3777                                             lockdep_is_held(&table->tb6_lock));
3778                 if (rcu_access_pointer(fn->leaf) == rt) {
3779                         struct fib6_info *last_sibling, *replace_rt;
3780
3781                         last_sibling = list_last_entry(&rt->fib6_siblings,
3782                                                        struct fib6_info,
3783                                                        fib6_siblings);
3784                         replace_rt = rcu_dereference_protected(
3785                                             last_sibling->fib6_next,
3786                                             lockdep_is_held(&table->tb6_lock));
3787                         if (replace_rt)
3788                                 call_fib6_entry_notifiers_replace(net,
3789                                                                   replace_rt);
3790                         else
3791                                 call_fib6_multipath_entry_notifiers(net,
3792                                                        FIB_EVENT_ENTRY_DEL,
3793                                                        rt, rt->fib6_nsiblings,
3794                                                        NULL);
3795                 }
3796                 list_for_each_entry_safe(sibling, next_sibling,
3797                                          &rt->fib6_siblings,
3798                                          fib6_siblings) {
3799                         err = fib6_del(sibling, info);
3800                         if (err)
3801                                 goto out_unlock;
3802                 }
3803         }
3804
3805         err = fib6_del(rt, info);
3806 out_unlock:
3807         spin_unlock_bh(&table->tb6_lock);
3808 out_put:
3809         fib6_info_release(rt);
3810
3811         if (skb) {
3812                 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
3813                             info->nlh, gfp_any());
3814         }
3815         return err;
3816 }
3817
3818 static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
3819 {
3820         int rc = -ESRCH;
3821
3822         if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3823                 goto out;
3824
3825         if (cfg->fc_flags & RTF_GATEWAY &&
3826             !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3827                 goto out;
3828
3829         rc = rt6_remove_exception_rt(rt);
3830 out:
3831         return rc;
3832 }
3833
3834 static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3835                              struct fib6_nh *nh)
3836 {
3837         struct fib6_result res = {
3838                 .f6i = rt,
3839                 .nh = nh,
3840         };
3841         struct rt6_info *rt_cache;
3842
3843         rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3844         if (rt_cache)
3845                 return __ip6_del_cached_rt(rt_cache, cfg);
3846
3847         return 0;
3848 }
3849
3850 struct fib6_nh_del_cached_rt_arg {
3851         struct fib6_config *cfg;
3852         struct fib6_info *f6i;
3853 };
3854
3855 static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
3856 {
3857         struct fib6_nh_del_cached_rt_arg *arg = _arg;
3858         int rc;
3859
3860         rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
3861         return rc != -ESRCH ? rc : 0;
3862 }
3863
3864 static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
3865 {
3866         struct fib6_nh_del_cached_rt_arg arg = {
3867                 .cfg = cfg,
3868                 .f6i = f6i
3869         };
3870
3871         return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
3872 }
3873
3874 static int ip6_route_del(struct fib6_config *cfg,
3875                          struct netlink_ext_ack *extack)
3876 {
3877         struct fib6_table *table;
3878         struct fib6_info *rt;
3879         struct fib6_node *fn;
3880         int err = -ESRCH;
3881
3882         table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
3883         if (!table) {
3884                 NL_SET_ERR_MSG(extack, "FIB table does not exist");
3885                 return err;
3886         }
3887
3888         rcu_read_lock();
3889
3890         fn = fib6_locate(&table->tb6_root,
3891                          &cfg->fc_dst, cfg->fc_dst_len,
3892                          &cfg->fc_src, cfg->fc_src_len,
3893                          !(cfg->fc_flags & RTF_CACHE));
3894
3895         if (fn) {
3896                 for_each_fib6_node_rt_rcu(fn) {
3897                         struct fib6_nh *nh;
3898
3899                         if (rt->nh && cfg->fc_nh_id &&
3900                             rt->nh->id != cfg->fc_nh_id)
3901                                 continue;
3902
3903                         if (cfg->fc_flags & RTF_CACHE) {
3904                                 int rc = 0;
3905
3906                                 if (rt->nh) {
3907                                         rc = ip6_del_cached_rt_nh(cfg, rt);
3908                                 } else if (cfg->fc_nh_id) {
3909                                         continue;
3910                                 } else {
3911                                         nh = rt->fib6_nh;
3912                                         rc = ip6_del_cached_rt(cfg, rt, nh);
3913                                 }
3914                                 if (rc != -ESRCH) {
3915                                         rcu_read_unlock();
3916                                         return rc;
3917                                 }
3918                                 continue;
3919                         }
3920
3921                         if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
3922                                 continue;
3923                         if (cfg->fc_protocol &&
3924                             cfg->fc_protocol != rt->fib6_protocol)
3925                                 continue;
3926
3927                         if (rt->nh) {
3928                                 if (!fib6_info_hold_safe(rt))
3929                                         continue;
3930                                 rcu_read_unlock();
3931
3932                                 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3933                         }
3934                         if (cfg->fc_nh_id)
3935                                 continue;
3936
3937                         nh = rt->fib6_nh;
3938                         if (cfg->fc_ifindex &&
3939                             (!nh->fib_nh_dev ||
3940                              nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
3941                                 continue;
3942                         if (cfg->fc_flags & RTF_GATEWAY &&
3943                             !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
3944                                 continue;
3945                         if (!fib6_info_hold_safe(rt))
3946                                 continue;
3947                         rcu_read_unlock();
3948
3949                         /* if gateway was specified only delete the one hop */
3950                         if (cfg->fc_flags & RTF_GATEWAY)
3951                                 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3952
3953                         return __ip6_del_rt_siblings(rt, cfg);
3954                 }
3955         }
3956         rcu_read_unlock();
3957
3958         return err;
3959 }
3960
3961 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
3962 {
3963         struct netevent_redirect netevent;
3964         struct rt6_info *rt, *nrt = NULL;
3965         struct fib6_result res = {};
3966         struct ndisc_options ndopts;
3967         struct inet6_dev *in6_dev;
3968         struct neighbour *neigh;
3969         struct rd_msg *msg;
3970         int optlen, on_link;
3971         u8 *lladdr;
3972
3973         optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
3974         optlen -= sizeof(*msg);
3975
3976         if (optlen < 0) {
3977                 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
3978                 return;
3979         }
3980
3981         msg = (struct rd_msg *)icmp6_hdr(skb);
3982
3983         if (ipv6_addr_is_multicast(&msg->dest)) {
3984                 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
3985                 return;
3986         }
3987
3988         on_link = 0;
3989         if (ipv6_addr_equal(&msg->dest, &msg->target)) {
3990                 on_link = 1;
3991         } else if (ipv6_addr_type(&msg->target) !=
3992                    (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
3993                 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
3994                 return;
3995         }
3996
3997         in6_dev = __in6_dev_get(skb->dev);
3998         if (!in6_dev)
3999                 return;
4000         if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
4001                 return;
4002
4003         /* RFC2461 8.1:
4004          *      The IP source address of the Redirect MUST be the same as the current
4005          *      first-hop router for the specified ICMP Destination Address.
4006          */
4007
4008         if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
4009                 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4010                 return;
4011         }
4012
4013         lladdr = NULL;
4014         if (ndopts.nd_opts_tgt_lladdr) {
4015                 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4016                                              skb->dev);
4017                 if (!lladdr) {
4018                         net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4019                         return;
4020                 }
4021         }
4022
4023         rt = (struct rt6_info *) dst;
4024         if (rt->rt6i_flags & RTF_REJECT) {
4025                 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4026                 return;
4027         }
4028
4029         /* Redirect received -> path was valid.
4030          * Look, redirects are sent only in response to data packets,
4031          * so that this nexthop apparently is reachable. --ANK
4032          */
4033         dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
4034
4035         neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
4036         if (!neigh)
4037                 return;
4038
4039         /*
4040          *      We have finally decided to accept it.
4041          */
4042
4043         ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
4044                      NEIGH_UPDATE_F_WEAK_OVERRIDE|
4045                      NEIGH_UPDATE_F_OVERRIDE|
4046                      (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
4047                                      NEIGH_UPDATE_F_ISROUTER)),
4048                      NDISC_REDIRECT, &ndopts);
4049
4050         rcu_read_lock();
4051         res.f6i = rcu_dereference(rt->from);
4052         if (!res.f6i)
4053                 goto out;
4054
4055         if (res.f6i->nh) {
4056                 struct fib6_nh_match_arg arg = {
4057                         .dev = dst->dev,
4058                         .gw = &rt->rt6i_gateway,
4059                 };
4060
4061                 nexthop_for_each_fib6_nh(res.f6i->nh,
4062                                          fib6_nh_find_match, &arg);
4063
4064                 /* fib6_info uses a nexthop that does not have fib6_nh
4065                  * using the dst->dev. Should be impossible
4066                  */
4067                 if (!arg.match)
4068                         goto out;
4069                 res.nh = arg.match;
4070         } else {
4071                 res.nh = res.f6i->fib6_nh;
4072         }
4073
4074         res.fib6_flags = res.f6i->fib6_flags;
4075         res.fib6_type = res.f6i->fib6_type;
4076         nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
4077         if (!nrt)
4078                 goto out;
4079
4080         nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4081         if (on_link)
4082                 nrt->rt6i_flags &= ~RTF_GATEWAY;
4083
4084         nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
4085
4086         /* rt6_insert_exception() will take care of duplicated exceptions */
4087         if (rt6_insert_exception(nrt, &res)) {
4088                 dst_release_immediate(&nrt->dst);
4089                 goto out;
4090         }
4091
4092         netevent.old = &rt->dst;
4093         netevent.new = &nrt->dst;
4094         netevent.daddr = &msg->dest;
4095         netevent.neigh = neigh;
4096         call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4097
4098 out:
4099         rcu_read_unlock();
4100         neigh_release(neigh);
4101 }
4102
4103 #ifdef CONFIG_IPV6_ROUTE_INFO
4104 static struct fib6_info *rt6_get_route_info(struct net *net,
4105                                            const struct in6_addr *prefix, int prefixlen,
4106                                            const struct in6_addr *gwaddr,
4107                                            struct net_device *dev)
4108 {
4109         u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4110         int ifindex = dev->ifindex;
4111         struct fib6_node *fn;
4112         struct fib6_info *rt = NULL;
4113         struct fib6_table *table;
4114
4115         table = fib6_get_table(net, tb_id);
4116         if (!table)
4117                 return NULL;
4118
4119         rcu_read_lock();
4120         fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
4121         if (!fn)
4122                 goto out;
4123
4124         for_each_fib6_node_rt_rcu(fn) {
4125                 /* these routes do not use nexthops */
4126                 if (rt->nh)
4127                         continue;
4128                 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
4129                         continue;
4130                 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
4131                     !rt->fib6_nh->fib_nh_gw_family)
4132                         continue;
4133                 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
4134                         continue;
4135                 if (!fib6_info_hold_safe(rt))
4136                         continue;
4137                 break;
4138         }
4139 out:
4140         rcu_read_unlock();
4141         return rt;
4142 }
4143
4144 static struct fib6_info *rt6_add_route_info(struct net *net,
4145                                            const struct in6_addr *prefix, int prefixlen,
4146                                            const struct in6_addr *gwaddr,
4147                                            struct net_device *dev,
4148                                            unsigned int pref)
4149 {
4150         struct fib6_config cfg = {
4151                 .fc_metric      = IP6_RT_PRIO_USER,
4152                 .fc_ifindex     = dev->ifindex,
4153                 .fc_dst_len     = prefixlen,
4154                 .fc_flags       = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4155                                   RTF_UP | RTF_PREF(pref),
4156                 .fc_protocol = RTPROT_RA,
4157                 .fc_type = RTN_UNICAST,
4158                 .fc_nlinfo.portid = 0,
4159                 .fc_nlinfo.nlh = NULL,
4160                 .fc_nlinfo.nl_net = net,
4161         };
4162
4163         cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
4164         cfg.fc_dst = *prefix;
4165         cfg.fc_gateway = *gwaddr;
4166
4167         /* We should treat it as a default route if prefix length is 0. */
4168         if (!prefixlen)
4169                 cfg.fc_flags |= RTF_DEFAULT;
4170
4171         ip6_route_add(&cfg, GFP_ATOMIC, NULL);
4172
4173         return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
4174 }
4175 #endif
4176
4177 struct fib6_info *rt6_get_dflt_router(struct net *net,
4178                                      const struct in6_addr *addr,
4179                                      struct net_device *dev)
4180 {
4181         u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
4182         struct fib6_info *rt;
4183         struct fib6_table *table;
4184
4185         table = fib6_get_table(net, tb_id);
4186         if (!table)
4187                 return NULL;
4188
4189         rcu_read_lock();
4190         for_each_fib6_node_rt_rcu(&table->tb6_root) {
4191                 struct fib6_nh *nh;
4192
4193                 /* RA routes do not use nexthops */
4194                 if (rt->nh)
4195                         continue;
4196
4197                 nh = rt->fib6_nh;
4198                 if (dev == nh->fib_nh_dev &&
4199                     ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
4200                     ipv6_addr_equal(&nh->fib_nh_gw6, addr))
4201                         break;
4202         }
4203         if (rt && !fib6_info_hold_safe(rt))
4204                 rt = NULL;
4205         rcu_read_unlock();
4206         return rt;
4207 }
4208
4209 struct fib6_info *rt6_add_dflt_router(struct net *net,
4210                                      const struct in6_addr *gwaddr,
4211                                      struct net_device *dev,
4212                                      unsigned int pref)
4213 {
4214         struct fib6_config cfg = {
4215                 .fc_table       = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
4216                 .fc_metric      = IP6_RT_PRIO_USER,
4217                 .fc_ifindex     = dev->ifindex,
4218                 .fc_flags       = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4219                                   RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
4220                 .fc_protocol = RTPROT_RA,
4221                 .fc_type = RTN_UNICAST,
4222                 .fc_nlinfo.portid = 0,
4223                 .fc_nlinfo.nlh = NULL,
4224                 .fc_nlinfo.nl_net = net,
4225         };
4226
4227         cfg.fc_gateway = *gwaddr;
4228
4229         if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
4230                 struct fib6_table *table;
4231
4232                 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4233                 if (table)
4234                         table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4235         }
4236
4237         return rt6_get_dflt_router(net, gwaddr, dev);
4238 }
4239
4240 static void __rt6_purge_dflt_routers(struct net *net,
4241                                      struct fib6_table *table)
4242 {
4243         struct fib6_info *rt;
4244
4245 restart:
4246         rcu_read_lock();
4247         for_each_fib6_node_rt_rcu(&table->tb6_root) {
4248                 struct net_device *dev = fib6_info_nh_dev(rt);
4249                 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4250
4251                 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
4252                     (!idev || idev->cnf.accept_ra != 2) &&
4253                     fib6_info_hold_safe(rt)) {
4254                         rcu_read_unlock();
4255                         ip6_del_rt(net, rt);
4256                         goto restart;
4257                 }
4258         }
4259         rcu_read_unlock();
4260
4261         table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4262 }
4263
4264 void rt6_purge_dflt_routers(struct net *net)
4265 {
4266         struct fib6_table *table;
4267         struct hlist_head *head;
4268         unsigned int h;
4269
4270         rcu_read_lock();
4271
4272         for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4273                 head = &net->ipv6.fib_table_hash[h];
4274                 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4275                         if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
4276                                 __rt6_purge_dflt_routers(net, table);
4277                 }
4278         }
4279
4280         rcu_read_unlock();
4281 }
4282
4283 static void rtmsg_to_fib6_config(struct net *net,
4284                                  struct in6_rtmsg *rtmsg,
4285                                  struct fib6_config *cfg)
4286 {
4287         *cfg = (struct fib6_config){
4288                 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4289                          : RT6_TABLE_MAIN,
4290                 .fc_ifindex = rtmsg->rtmsg_ifindex,
4291                 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
4292                 .fc_expires = rtmsg->rtmsg_info,
4293                 .fc_dst_len = rtmsg->rtmsg_dst_len,
4294                 .fc_src_len = rtmsg->rtmsg_src_len,
4295                 .fc_flags = rtmsg->rtmsg_flags,
4296                 .fc_type = rtmsg->rtmsg_type,
4297
4298                 .fc_nlinfo.nl_net = net,
4299
4300                 .fc_dst = rtmsg->rtmsg_dst,
4301                 .fc_src = rtmsg->rtmsg_src,
4302                 .fc_gateway = rtmsg->rtmsg_gateway,
4303         };
4304 }
4305
4306 int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
4307 {
4308         struct fib6_config cfg;
4309         struct in6_rtmsg rtmsg;
4310         int err;
4311
4312         switch (cmd) {
4313         case SIOCADDRT:         /* Add a route */
4314         case SIOCDELRT:         /* Delete a route */
4315                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4316                         return -EPERM;
4317                 err = copy_from_user(&rtmsg, arg,
4318                                      sizeof(struct in6_rtmsg));
4319                 if (err)
4320                         return -EFAULT;
4321
4322                 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
4323
4324                 rtnl_lock();
4325                 switch (cmd) {
4326                 case SIOCADDRT:
4327                         err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
4328                         break;
4329                 case SIOCDELRT:
4330                         err = ip6_route_del(&cfg, NULL);
4331                         break;
4332                 default:
4333                         err = -EINVAL;
4334                 }
4335                 rtnl_unlock();
4336
4337                 return err;
4338         }
4339
4340         return -EINVAL;
4341 }
4342
4343 /*
4344  *      Drop the packet on the floor
4345  */
4346
4347 static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
4348 {
4349         struct dst_entry *dst = skb_dst(skb);
4350         struct net *net = dev_net(dst->dev);
4351         struct inet6_dev *idev;
4352         int type;
4353
4354         if (netif_is_l3_master(skb->dev) &&
4355             dst->dev == net->loopback_dev)
4356                 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4357         else
4358                 idev = ip6_dst_idev(dst);
4359
4360         switch (ipstats_mib_noroutes) {
4361         case IPSTATS_MIB_INNOROUTES:
4362                 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
4363                 if (type == IPV6_ADDR_ANY) {
4364                         IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
4365                         break;
4366                 }
4367                 fallthrough;
4368         case IPSTATS_MIB_OUTNOROUTES:
4369                 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
4370                 break;
4371         }
4372
4373         /* Start over by dropping the dst for l3mdev case */
4374         if (netif_is_l3_master(skb->dev))
4375                 skb_dst_drop(skb);
4376
4377         icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
4378         kfree_skb(skb);
4379         return 0;
4380 }
4381
4382 static int ip6_pkt_discard(struct sk_buff *skb)
4383 {
4384         return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
4385 }
4386
4387 static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4388 {
4389         skb->dev = skb_dst(skb)->dev;
4390         return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
4391 }
4392
4393 static int ip6_pkt_prohibit(struct sk_buff *skb)
4394 {
4395         return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
4396 }
4397
4398 static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4399 {
4400         skb->dev = skb_dst(skb)->dev;
4401         return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
4402 }
4403
4404 /*
4405  *      Allocate a dst for local (unicast / anycast) address.
4406  */
4407
4408 struct fib6_info *addrconf_f6i_alloc(struct net *net,
4409                                      struct inet6_dev *idev,
4410                                      const struct in6_addr *addr,
4411                                      bool anycast, gfp_t gfp_flags)
4412 {
4413         struct fib6_config cfg = {
4414                 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4415                 .fc_ifindex = idev->dev->ifindex,
4416                 .fc_flags = RTF_UP | RTF_NONEXTHOP,
4417                 .fc_dst = *addr,
4418                 .fc_dst_len = 128,
4419                 .fc_protocol = RTPROT_KERNEL,
4420                 .fc_nlinfo.nl_net = net,
4421                 .fc_ignore_dev_down = true,
4422         };
4423         struct fib6_info *f6i;
4424
4425         if (anycast) {
4426                 cfg.fc_type = RTN_ANYCAST;
4427                 cfg.fc_flags |= RTF_ANYCAST;
4428         } else {
4429                 cfg.fc_type = RTN_LOCAL;
4430                 cfg.fc_flags |= RTF_LOCAL;
4431         }
4432
4433         f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
4434         if (!IS_ERR(f6i))
4435                 f6i->dst_nocount = true;
4436         return f6i;
4437 }
4438
4439 /* remove deleted ip from prefsrc entries */
4440 struct arg_dev_net_ip {
4441         struct net_device *dev;
4442         struct net *net;
4443         struct in6_addr *addr;
4444 };
4445
4446 static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
4447 {
4448         struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4449         struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4450         struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4451
4452         if (!rt->nh &&
4453             ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
4454             rt != net->ipv6.fib6_null_entry &&
4455             ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
4456                 spin_lock_bh(&rt6_exception_lock);
4457                 /* remove prefsrc entry */
4458                 rt->fib6_prefsrc.plen = 0;
4459                 spin_unlock_bh(&rt6_exception_lock);
4460         }
4461         return 0;
4462 }
4463
4464 void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4465 {
4466         struct net *net = dev_net(ifp->idev->dev);
4467         struct arg_dev_net_ip adni = {
4468                 .dev = ifp->idev->dev,
4469                 .net = net,
4470                 .addr = &ifp->addr,
4471         };
4472         fib6_clean_all(net, fib6_remove_prefsrc, &adni);
4473 }
4474
4475 #define RTF_RA_ROUTER           (RTF_ADDRCONF | RTF_DEFAULT)
4476
4477 /* Remove routers and update dst entries when gateway turn into host. */
4478 static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
4479 {
4480         struct in6_addr *gateway = (struct in6_addr *)arg;
4481         struct fib6_nh *nh;
4482
4483         /* RA routes do not use nexthops */
4484         if (rt->nh)
4485                 return 0;
4486
4487         nh = rt->fib6_nh;
4488         if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
4489             nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
4490                 return -1;
4491
4492         /* Further clean up cached routes in exception table.
4493          * This is needed because cached route may have a different
4494          * gateway than its 'parent' in the case of an ip redirect.
4495          */
4496         fib6_nh_exceptions_clean_tohost(nh, gateway);
4497
4498         return 0;
4499 }
4500
4501 void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4502 {
4503         fib6_clean_all(net, fib6_clean_tohost, gateway);
4504 }
4505
4506 struct arg_netdev_event {
4507         const struct net_device *dev;
4508         union {
4509                 unsigned char nh_flags;
4510                 unsigned long event;
4511         };
4512 };
4513
4514 static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
4515 {
4516         struct fib6_info *iter;
4517         struct fib6_node *fn;
4518
4519         fn = rcu_dereference_protected(rt->fib6_node,
4520                         lockdep_is_held(&rt->fib6_table->tb6_lock));
4521         iter = rcu_dereference_protected(fn->leaf,
4522                         lockdep_is_held(&rt->fib6_table->tb6_lock));
4523         while (iter) {
4524                 if (iter->fib6_metric == rt->fib6_metric &&
4525                     rt6_qualify_for_ecmp(iter))
4526                         return iter;
4527                 iter = rcu_dereference_protected(iter->fib6_next,
4528                                 lockdep_is_held(&rt->fib6_table->tb6_lock));
4529         }
4530
4531         return NULL;
4532 }
4533
4534 /* only called for fib entries with builtin fib6_nh */
4535 static bool rt6_is_dead(const struct fib6_info *rt)
4536 {
4537         if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4538             (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4539              ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
4540                 return true;
4541
4542         return false;
4543 }
4544
4545 static int rt6_multipath_total_weight(const struct fib6_info *rt)
4546 {
4547         struct fib6_info *iter;
4548         int total = 0;
4549
4550         if (!rt6_is_dead(rt))
4551                 total += rt->fib6_nh->fib_nh_weight;
4552
4553         list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
4554                 if (!rt6_is_dead(iter))
4555                         total += iter->fib6_nh->fib_nh_weight;
4556         }
4557
4558         return total;
4559 }
4560
4561 static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
4562 {
4563         int upper_bound = -1;
4564
4565         if (!rt6_is_dead(rt)) {
4566                 *weight += rt->fib6_nh->fib_nh_weight;
4567                 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4568                                                     total) - 1;
4569         }
4570         atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
4571 }
4572
4573 static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
4574 {
4575         struct fib6_info *iter;
4576         int weight = 0;
4577
4578         rt6_upper_bound_set(rt, &weight, total);
4579
4580         list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4581                 rt6_upper_bound_set(iter, &weight, total);
4582 }
4583
4584 void rt6_multipath_rebalance(struct fib6_info *rt)
4585 {
4586         struct fib6_info *first;
4587         int total;
4588
4589         /* In case the entire multipath route was marked for flushing,
4590          * then there is no need to rebalance upon the removal of every
4591          * sibling route.
4592          */
4593         if (!rt->fib6_nsiblings || rt->should_flush)
4594                 return;
4595
4596         /* During lookup routes are evaluated in order, so we need to
4597          * make sure upper bounds are assigned from the first sibling
4598          * onwards.
4599          */
4600         first = rt6_multipath_first_sibling(rt);
4601         if (WARN_ON_ONCE(!first))
4602                 return;
4603
4604         total = rt6_multipath_total_weight(first);
4605         rt6_multipath_upper_bound_set(first, total);
4606 }
4607
4608 static int fib6_ifup(struct fib6_info *rt, void *p_arg)
4609 {
4610         const struct arg_netdev_event *arg = p_arg;
4611         struct net *net = dev_net(arg->dev);
4612
4613         if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
4614             rt->fib6_nh->fib_nh_dev == arg->dev) {
4615                 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
4616                 fib6_update_sernum_upto_root(net, rt);
4617                 rt6_multipath_rebalance(rt);
4618         }
4619
4620         return 0;
4621 }
4622
4623 void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
4624 {
4625         struct arg_netdev_event arg = {
4626                 .dev = dev,
4627                 {
4628                         .nh_flags = nh_flags,
4629                 },
4630         };
4631
4632         if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4633                 arg.nh_flags |= RTNH_F_LINKDOWN;
4634
4635         fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4636 }
4637
4638 /* only called for fib entries with inline fib6_nh */
4639 static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
4640                                    const struct net_device *dev)
4641 {
4642         struct fib6_info *iter;
4643
4644         if (rt->fib6_nh->fib_nh_dev == dev)
4645                 return true;
4646         list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4647                 if (iter->fib6_nh->fib_nh_dev == dev)
4648                         return true;
4649
4650         return false;
4651 }
4652
4653 static void rt6_multipath_flush(struct fib6_info *rt)
4654 {
4655         struct fib6_info *iter;
4656
4657         rt->should_flush = 1;
4658         list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4659                 iter->should_flush = 1;
4660 }
4661
4662 static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
4663                                              const struct net_device *down_dev)
4664 {
4665         struct fib6_info *iter;
4666         unsigned int dead = 0;
4667
4668         if (rt->fib6_nh->fib_nh_dev == down_dev ||
4669             rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
4670                 dead++;
4671         list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4672                 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4673                     iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
4674                         dead++;
4675
4676         return dead;
4677 }
4678
4679 static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
4680                                        const struct net_device *dev,
4681                                        unsigned char nh_flags)
4682 {
4683         struct fib6_info *iter;
4684
4685         if (rt->fib6_nh->fib_nh_dev == dev)
4686                 rt->fib6_nh->fib_nh_flags |= nh_flags;
4687         list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4688                 if (iter->fib6_nh->fib_nh_dev == dev)
4689                         iter->fib6_nh->fib_nh_flags |= nh_flags;
4690 }
4691
4692 /* called with write lock held for table with rt */
4693 static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
4694 {
4695         const struct arg_netdev_event *arg = p_arg;
4696         const struct net_device *dev = arg->dev;
4697         struct net *net = dev_net(dev);
4698
4699         if (rt == net->ipv6.fib6_null_entry || rt->nh)
4700                 return 0;
4701
4702         switch (arg->event) {
4703         case NETDEV_UNREGISTER:
4704                 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
4705         case NETDEV_DOWN:
4706                 if (rt->should_flush)
4707                         return -1;
4708                 if (!rt->fib6_nsiblings)
4709                         return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
4710                 if (rt6_multipath_uses_dev(rt, dev)) {
4711                         unsigned int count;
4712
4713                         count = rt6_multipath_dead_count(rt, dev);
4714                         if (rt->fib6_nsiblings + 1 == count) {
4715                                 rt6_multipath_flush(rt);
4716                                 return -1;
4717                         }
4718                         rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4719                                                    RTNH_F_LINKDOWN);
4720                         fib6_update_sernum(net, rt);
4721                         rt6_multipath_rebalance(rt);
4722                 }
4723                 return -2;
4724         case NETDEV_CHANGE:
4725                 if (rt->fib6_nh->fib_nh_dev != dev ||
4726                     rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
4727                         break;
4728                 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
4729                 rt6_multipath_rebalance(rt);
4730                 break;
4731         }
4732
4733         return 0;
4734 }
4735
4736 void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
4737 {
4738         struct arg_netdev_event arg = {
4739                 .dev = dev,
4740                 {
4741                         .event = event,
4742                 },
4743         };
4744         struct net *net = dev_net(dev);
4745
4746         if (net->ipv6.sysctl.skip_notify_on_dev_down)
4747                 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4748         else
4749                 fib6_clean_all(net, fib6_ifdown, &arg);
4750 }
4751
4752 void rt6_disable_ip(struct net_device *dev, unsigned long event)
4753 {
4754         rt6_sync_down_dev(dev, event);
4755         rt6_uncached_list_flush_dev(dev_net(dev), dev);
4756         neigh_ifdown(&nd_tbl, dev);
4757 }
4758
4759 struct rt6_mtu_change_arg {
4760         struct net_device *dev;
4761         unsigned int mtu;
4762         struct fib6_info *f6i;
4763 };
4764
4765 static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
4766 {
4767         struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
4768         struct fib6_info *f6i = arg->f6i;
4769
4770         /* For administrative MTU increase, there is no way to discover
4771          * IPv6 PMTU increase, so PMTU increase should be updated here.
4772          * Since RFC 1981 doesn't include administrative MTU increase
4773          * update PMTU increase is a MUST. (i.e. jumbo frame)
4774          */
4775         if (nh->fib_nh_dev == arg->dev) {
4776                 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4777                 u32 mtu = f6i->fib6_pmtu;
4778
4779                 if (mtu >= arg->mtu ||
4780                     (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4781                         fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4782
4783                 spin_lock_bh(&rt6_exception_lock);
4784                 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
4785                 spin_unlock_bh(&rt6_exception_lock);
4786         }
4787
4788         return 0;
4789 }
4790
4791 static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
4792 {
4793         struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4794         struct inet6_dev *idev;
4795
4796         /* In IPv6 pmtu discovery is not optional,
4797            so that RTAX_MTU lock cannot disable it.
4798            We still use this lock to block changes
4799            caused by addrconf/ndisc.
4800         */
4801
4802         idev = __in6_dev_get(arg->dev);
4803         if (!idev)
4804                 return 0;
4805
4806         if (fib6_metric_locked(f6i, RTAX_MTU))
4807                 return 0;
4808
4809         arg->f6i = f6i;
4810         if (f6i->nh) {
4811                 /* fib6_nh_mtu_change only returns 0, so this is safe */
4812                 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4813                                                 arg);
4814         }
4815
4816         return fib6_nh_mtu_change(f6i->fib6_nh, arg);
4817 }
4818
4819 void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
4820 {
4821         struct rt6_mtu_change_arg arg = {
4822                 .dev = dev,
4823                 .mtu = mtu,
4824         };
4825
4826         fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
4827 }
4828
4829 static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
4830         [RTA_UNSPEC]            = { .strict_start_type = RTA_DPORT + 1 },
4831         [RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
4832         [RTA_PREFSRC]           = { .len = sizeof(struct in6_addr) },
4833         [RTA_OIF]               = { .type = NLA_U32 },
4834         [RTA_IIF]               = { .type = NLA_U32 },
4835         [RTA_PRIORITY]          = { .type = NLA_U32 },
4836         [RTA_METRICS]           = { .type = NLA_NESTED },
4837         [RTA_MULTIPATH]         = { .len = sizeof(struct rtnexthop) },
4838         [RTA_PREF]              = { .type = NLA_U8 },
4839         [RTA_ENCAP_TYPE]        = { .type = NLA_U16 },
4840         [RTA_ENCAP]             = { .type = NLA_NESTED },
4841         [RTA_EXPIRES]           = { .type = NLA_U32 },
4842         [RTA_UID]               = { .type = NLA_U32 },
4843         [RTA_MARK]              = { .type = NLA_U32 },
4844         [RTA_TABLE]             = { .type = NLA_U32 },
4845         [RTA_IP_PROTO]          = { .type = NLA_U8 },
4846         [RTA_SPORT]             = { .type = NLA_U16 },
4847         [RTA_DPORT]             = { .type = NLA_U16 },
4848         [RTA_NH_ID]             = { .type = NLA_U32 },
4849 };
4850
4851 static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4852                               struct fib6_config *cfg,
4853                               struct netlink_ext_ack *extack)
4854 {
4855         struct rtmsg *rtm;
4856         struct nlattr *tb[RTA_MAX+1];
4857         unsigned int pref;
4858         int err;
4859
4860         err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
4861                                      rtm_ipv6_policy, extack);
4862         if (err < 0)
4863                 goto errout;
4864
4865         err = -EINVAL;
4866         rtm = nlmsg_data(nlh);
4867
4868         *cfg = (struct fib6_config){
4869                 .fc_table = rtm->rtm_table,
4870                 .fc_dst_len = rtm->rtm_dst_len,
4871                 .fc_src_len = rtm->rtm_src_len,
4872                 .fc_flags = RTF_UP,
4873                 .fc_protocol = rtm->rtm_protocol,
4874                 .fc_type = rtm->rtm_type,
4875
4876                 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
4877                 .fc_nlinfo.nlh = nlh,
4878                 .fc_nlinfo.nl_net = sock_net(skb->sk),
4879         };
4880
4881         if (rtm->rtm_type == RTN_UNREACHABLE ||
4882             rtm->rtm_type == RTN_BLACKHOLE ||
4883             rtm->rtm_type == RTN_PROHIBIT ||
4884             rtm->rtm_type == RTN_THROW)
4885                 cfg->fc_flags |= RTF_REJECT;
4886
4887         if (rtm->rtm_type == RTN_LOCAL)
4888                 cfg->fc_flags |= RTF_LOCAL;
4889
4890         if (rtm->rtm_flags & RTM_F_CLONED)
4891                 cfg->fc_flags |= RTF_CACHE;
4892
4893         cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4894
4895         if (tb[RTA_NH_ID]) {
4896                 if (tb[RTA_GATEWAY]   || tb[RTA_OIF] ||
4897                     tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
4898                         NL_SET_ERR_MSG(extack,
4899                                        "Nexthop specification and nexthop id are mutually exclusive");
4900                         goto errout;
4901                 }
4902                 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
4903         }
4904
4905         if (tb[RTA_GATEWAY]) {
4906                 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
4907                 cfg->fc_flags |= RTF_GATEWAY;
4908         }
4909         if (tb[RTA_VIA]) {
4910                 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4911                 goto errout;
4912         }
4913
4914         if (tb[RTA_DST]) {
4915                 int plen = (rtm->rtm_dst_len + 7) >> 3;
4916
4917                 if (nla_len(tb[RTA_DST]) < plen)
4918                         goto errout;
4919
4920                 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
4921         }
4922
4923         if (tb[RTA_SRC]) {
4924                 int plen = (rtm->rtm_src_len + 7) >> 3;
4925
4926                 if (nla_len(tb[RTA_SRC]) < plen)
4927                         goto errout;
4928
4929                 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
4930         }
4931
4932         if (tb[RTA_PREFSRC])
4933                 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4934
4935         if (tb[RTA_OIF])
4936                 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4937
4938         if (tb[RTA_PRIORITY])
4939                 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4940
4941         if (tb[RTA_METRICS]) {
4942                 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4943                 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
4944         }
4945
4946         if (tb[RTA_TABLE])
4947                 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4948
4949         if (tb[RTA_MULTIPATH]) {
4950                 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4951                 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
4952
4953                 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
4954                                                      cfg->fc_mp_len, extack);
4955                 if (err < 0)
4956                         goto errout;
4957         }
4958
4959         if (tb[RTA_PREF]) {
4960                 pref = nla_get_u8(tb[RTA_PREF]);
4961                 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4962                     pref != ICMPV6_ROUTER_PREF_HIGH)
4963                         pref = ICMPV6_ROUTER_PREF_MEDIUM;
4964                 cfg->fc_flags |= RTF_PREF(pref);
4965         }
4966
4967         if (tb[RTA_ENCAP])
4968                 cfg->fc_encap = tb[RTA_ENCAP];
4969
4970         if (tb[RTA_ENCAP_TYPE]) {
4971                 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
4972
4973                 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
4974                 if (err < 0)
4975                         goto errout;
4976         }
4977
4978         if (tb[RTA_EXPIRES]) {
4979                 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
4980
4981                 if (addrconf_finite_timeout(timeout)) {
4982                         cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
4983                         cfg->fc_flags |= RTF_EXPIRES;
4984                 }
4985         }
4986
4987         err = 0;
4988 errout:
4989         return err;
4990 }
4991
4992 struct rt6_nh {
4993         struct fib6_info *fib6_info;
4994         struct fib6_config r_cfg;
4995         struct list_head next;
4996 };
4997
4998 static int ip6_route_info_append(struct net *net,
4999                                  struct list_head *rt6_nh_list,
5000                                  struct fib6_info *rt,
5001                                  struct fib6_config *r_cfg)
5002 {
5003         struct rt6_nh *nh;
5004         int err = -EEXIST;
5005
5006         list_for_each_entry(nh, rt6_nh_list, next) {
5007                 /* check if fib6_info already exists */
5008                 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
5009                         return err;
5010         }
5011
5012         nh = kzalloc(sizeof(*nh), GFP_KERNEL);
5013         if (!nh)
5014                 return -ENOMEM;
5015         nh->fib6_info = rt;
5016         memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
5017         list_add_tail(&nh->next, rt6_nh_list);
5018
5019         return 0;
5020 }
5021
5022 static void ip6_route_mpath_notify(struct fib6_info *rt,
5023                                    struct fib6_info *rt_last,
5024                                    struct nl_info *info,
5025                                    __u16 nlflags)
5026 {
5027         /* if this is an APPEND route, then rt points to the first route
5028          * inserted and rt_last points to last route inserted. Userspace
5029          * wants a consistent dump of the route which starts at the first
5030          * nexthop. Since sibling routes are always added at the end of
5031          * the list, find the first sibling of the last route appended
5032          */
5033         if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
5034                 rt = list_first_entry(&rt_last->fib6_siblings,
5035                                       struct fib6_info,
5036                                       fib6_siblings);
5037         }
5038
5039         if (rt)
5040                 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5041 }
5042
5043 static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
5044 {
5045         bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
5046         bool should_notify = false;
5047         struct fib6_info *leaf;
5048         struct fib6_node *fn;
5049
5050         rcu_read_lock();
5051         fn = rcu_dereference(rt->fib6_node);
5052         if (!fn)
5053                 goto out;
5054
5055         leaf = rcu_dereference(fn->leaf);
5056         if (!leaf)
5057                 goto out;
5058
5059         if (rt == leaf ||
5060             (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
5061              rt6_qualify_for_ecmp(leaf)))
5062                 should_notify = true;
5063 out:
5064         rcu_read_unlock();
5065
5066         return should_notify;
5067 }
5068
5069 static int ip6_route_multipath_add(struct fib6_config *cfg,
5070                                    struct netlink_ext_ack *extack)
5071 {
5072         struct fib6_info *rt_notif = NULL, *rt_last = NULL;
5073         struct nl_info *info = &cfg->fc_nlinfo;
5074         struct fib6_config r_cfg;
5075         struct rtnexthop *rtnh;
5076         struct fib6_info *rt;
5077         struct rt6_nh *err_nh;
5078         struct rt6_nh *nh, *nh_safe;
5079         __u16 nlflags;
5080         int remaining;
5081         int attrlen;
5082         int err = 1;
5083         int nhn = 0;
5084         int replace = (cfg->fc_nlinfo.nlh &&
5085                        (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5086         LIST_HEAD(rt6_nh_list);
5087
5088         nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5089         if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5090                 nlflags |= NLM_F_APPEND;
5091
5092         remaining = cfg->fc_mp_len;
5093         rtnh = (struct rtnexthop *)cfg->fc_mp;
5094
5095         /* Parse a Multipath Entry and build a list (rt6_nh_list) of
5096          * fib6_info structs per nexthop
5097          */
5098         while (rtnh_ok(rtnh, remaining)) {
5099                 memcpy(&r_cfg, cfg, sizeof(*cfg));
5100                 if (rtnh->rtnh_ifindex)
5101                         r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5102
5103                 attrlen = rtnh_attrlen(rtnh);
5104                 if (attrlen > 0) {
5105                         struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5106
5107                         nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5108                         if (nla) {
5109                                 r_cfg.fc_gateway = nla_get_in6_addr(nla);
5110                                 r_cfg.fc_flags |= RTF_GATEWAY;
5111                         }
5112                         r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5113                         nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5114                         if (nla)
5115                                 r_cfg.fc_encap_type = nla_get_u16(nla);
5116                 }
5117
5118                 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
5119                 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
5120                 if (IS_ERR(rt)) {
5121                         err = PTR_ERR(rt);
5122                         rt = NULL;
5123                         goto cleanup;
5124                 }
5125                 if (!rt6_qualify_for_ecmp(rt)) {
5126                         err = -EINVAL;
5127                         NL_SET_ERR_MSG(extack,
5128                                        "Device only routes can not be added for IPv6 using the multipath API.");
5129                         fib6_info_release(rt);
5130                         goto cleanup;
5131                 }
5132
5133                 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
5134
5135                 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5136                                             rt, &r_cfg);
5137                 if (err) {
5138                         fib6_info_release(rt);
5139                         goto cleanup;
5140                 }
5141
5142                 rtnh = rtnh_next(rtnh, &remaining);
5143         }
5144
5145         if (list_empty(&rt6_nh_list)) {
5146                 NL_SET_ERR_MSG(extack,
5147                                "Invalid nexthop configuration - no valid nexthops");
5148                 return -EINVAL;
5149         }
5150
5151         /* for add and replace send one notification with all nexthops.
5152          * Skip the notification in fib6_add_rt2node and send one with
5153          * the full route when done
5154          */
5155         info->skip_notify = 1;
5156
5157         /* For add and replace, send one notification with all nexthops. For
5158          * append, send one notification with all appended nexthops.
5159          */
5160         info->skip_notify_kernel = 1;
5161
5162         err_nh = NULL;
5163         list_for_each_entry(nh, &rt6_nh_list, next) {
5164                 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5165                 fib6_info_release(nh->fib6_info);
5166
5167                 if (!err) {
5168                         /* save reference to last route successfully inserted */
5169                         rt_last = nh->fib6_info;
5170
5171                         /* save reference to first route for notification */
5172                         if (!rt_notif)
5173                                 rt_notif = nh->fib6_info;
5174                 }
5175
5176                 /* nh->fib6_info is used or freed at this point, reset to NULL*/
5177                 nh->fib6_info = NULL;
5178                 if (err) {
5179                         if (replace && nhn)
5180                                 NL_SET_ERR_MSG_MOD(extack,
5181                                                    "multipath route replace failed (check consistency of installed routes)");
5182                         err_nh = nh;
5183                         goto add_errout;
5184                 }
5185
5186                 /* Because each route is added like a single route we remove
5187                  * these flags after the first nexthop: if there is a collision,
5188                  * we have already failed to add the first nexthop:
5189                  * fib6_add_rt2node() has rejected it; when replacing, old
5190                  * nexthops have been replaced by first new, the rest should
5191                  * be added to it.
5192                  */
5193                 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5194                                                      NLM_F_REPLACE);
5195                 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
5196                 nhn++;
5197         }
5198
5199         /* An in-kernel notification should only be sent in case the new
5200          * multipath route is added as the first route in the node, or if
5201          * it was appended to it. We pass 'rt_notif' since it is the first
5202          * sibling and might allow us to skip some checks in the replace case.
5203          */
5204         if (ip6_route_mpath_should_notify(rt_notif)) {
5205                 enum fib_event_type fib_event;
5206
5207                 if (rt_notif->fib6_nsiblings != nhn - 1)
5208                         fib_event = FIB_EVENT_ENTRY_APPEND;
5209                 else
5210                         fib_event = FIB_EVENT_ENTRY_REPLACE;
5211
5212                 err = call_fib6_multipath_entry_notifiers(info->nl_net,
5213                                                           fib_event, rt_notif,
5214                                                           nhn - 1, extack);
5215                 if (err) {
5216                         /* Delete all the siblings that were just added */
5217                         err_nh = NULL;
5218                         goto add_errout;
5219                 }
5220         }
5221
5222         /* success ... tell user about new route */
5223         ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5224         goto cleanup;
5225
5226 add_errout:
5227         /* send notification for routes that were added so that
5228          * the delete notifications sent by ip6_route_del are
5229          * coherent
5230          */
5231         if (rt_notif)
5232                 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5233
5234         /* Delete routes that were already added */
5235         list_for_each_entry(nh, &rt6_nh_list, next) {
5236                 if (err_nh == nh)
5237                         break;
5238                 ip6_route_del(&nh->r_cfg, extack);
5239         }
5240
5241 cleanup:
5242         list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
5243                 if (nh->fib6_info)
5244                         fib6_info_release(nh->fib6_info);
5245                 list_del(&nh->next);
5246                 kfree(nh);
5247         }
5248
5249         return err;
5250 }
5251
5252 static int ip6_route_multipath_del(struct fib6_config *cfg,
5253                                    struct netlink_ext_ack *extack)
5254 {
5255         struct fib6_config r_cfg;
5256         struct rtnexthop *rtnh;
5257         int remaining;
5258         int attrlen;
5259         int err = 1, last_err = 0;
5260
5261         remaining = cfg->fc_mp_len;
5262         rtnh = (struct rtnexthop *)cfg->fc_mp;
5263
5264         /* Parse a Multipath Entry */
5265         while (rtnh_ok(rtnh, remaining)) {
5266                 memcpy(&r_cfg, cfg, sizeof(*cfg));
5267                 if (rtnh->rtnh_ifindex)
5268                         r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5269
5270                 attrlen = rtnh_attrlen(rtnh);
5271                 if (attrlen > 0) {
5272                         struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5273
5274                         nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5275                         if (nla) {
5276                                 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
5277                                 r_cfg.fc_flags |= RTF_GATEWAY;
5278                         }
5279                 }
5280                 err = ip6_route_del(&r_cfg, extack);
5281                 if (err)
5282                         last_err = err;
5283
5284                 rtnh = rtnh_next(rtnh, &remaining);
5285         }
5286
5287         return last_err;
5288 }
5289
5290 static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5291                               struct netlink_ext_ack *extack)
5292 {
5293         struct fib6_config cfg;
5294         int err;
5295
5296         err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5297         if (err < 0)
5298                 return err;
5299
5300         if (cfg.fc_nh_id &&
5301             !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5302                 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5303                 return -EINVAL;
5304         }
5305
5306         if (cfg.fc_mp)
5307                 return ip6_route_multipath_del(&cfg, extack);
5308         else {
5309                 cfg.fc_delete_all_nh = 1;
5310                 return ip6_route_del(&cfg, extack);
5311         }
5312 }
5313
5314 static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5315                               struct netlink_ext_ack *extack)
5316 {
5317         struct fib6_config cfg;
5318         int err;
5319
5320         err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5321         if (err < 0)
5322                 return err;
5323
5324         if (cfg.fc_metric == 0)
5325                 cfg.fc_metric = IP6_RT_PRIO_USER;
5326
5327         if (cfg.fc_mp)
5328                 return ip6_route_multipath_add(&cfg, extack);
5329         else
5330                 return ip6_route_add(&cfg, GFP_KERNEL, extack);
5331 }
5332
5333 /* add the overhead of this fib6_nh to nexthop_len */
5334 static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
5335 {
5336         int *nexthop_len = arg;
5337
5338         *nexthop_len += nla_total_size(0)        /* RTA_MULTIPATH */
5339                      + NLA_ALIGN(sizeof(struct rtnexthop))
5340                      + nla_total_size(16); /* RTA_GATEWAY */
5341
5342         if (nh->fib_nh_lws) {
5343                 /* RTA_ENCAP_TYPE */
5344                 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5345                 /* RTA_ENCAP */
5346                 *nexthop_len += nla_total_size(2);
5347         }
5348
5349         return 0;
5350 }
5351
5352 static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5353 {
5354         int nexthop_len;
5355
5356         if (f6i->nh) {
5357                 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5358                 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5359                                          &nexthop_len);
5360         } else {
5361                 struct fib6_nh *nh = f6i->fib6_nh;
5362
5363                 nexthop_len = 0;
5364                 if (f6i->fib6_nsiblings) {
5365                         nexthop_len = nla_total_size(0)  /* RTA_MULTIPATH */
5366                                     + NLA_ALIGN(sizeof(struct rtnexthop))
5367                                     + nla_total_size(16) /* RTA_GATEWAY */
5368                                     + lwtunnel_get_encap_size(nh->fib_nh_lws);
5369
5370                         nexthop_len *= f6i->fib6_nsiblings;
5371                 }
5372                 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5373         }
5374
5375         return NLMSG_ALIGN(sizeof(struct rtmsg))
5376                + nla_total_size(16) /* RTA_SRC */
5377                + nla_total_size(16) /* RTA_DST */
5378                + nla_total_size(16) /* RTA_GATEWAY */
5379                + nla_total_size(16) /* RTA_PREFSRC */
5380                + nla_total_size(4) /* RTA_TABLE */
5381                + nla_total_size(4) /* RTA_IIF */
5382                + nla_total_size(4) /* RTA_OIF */
5383                + nla_total_size(4) /* RTA_PRIORITY */
5384                + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
5385                + nla_total_size(sizeof(struct rta_cacheinfo))
5386                + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
5387                + nla_total_size(1) /* RTA_PREF */
5388                + nexthop_len;
5389 }
5390
5391 static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5392                                  unsigned char *flags)
5393 {
5394         if (nexthop_is_multipath(nh)) {
5395                 struct nlattr *mp;
5396
5397                 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5398                 if (!mp)
5399                         goto nla_put_failure;
5400
5401                 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
5402                         goto nla_put_failure;
5403
5404                 nla_nest_end(skb, mp);
5405         } else {
5406                 struct fib6_nh *fib6_nh;
5407
5408                 fib6_nh = nexthop_fib6_nh(nh);
5409                 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
5410                                      flags, false) < 0)
5411                         goto nla_put_failure;
5412         }
5413
5414         return 0;
5415
5416 nla_put_failure:
5417         return -EMSGSIZE;
5418 }
5419
5420 static int rt6_fill_node(struct net *net, struct sk_buff *skb,
5421                          struct fib6_info *rt, struct dst_entry *dst,
5422                          struct in6_addr *dest, struct in6_addr *src,
5423                          int iif, int type, u32 portid, u32 seq,
5424                          unsigned int flags)
5425 {
5426         struct rt6_info *rt6 = (struct rt6_info *)dst;
5427         struct rt6key *rt6_dst, *rt6_src;
5428         u32 *pmetrics, table, rt6_flags;
5429         unsigned char nh_flags = 0;
5430         struct nlmsghdr *nlh;
5431         struct rtmsg *rtm;
5432         long expires = 0;
5433
5434         nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
5435         if (!nlh)
5436                 return -EMSGSIZE;
5437
5438         if (rt6) {
5439                 rt6_dst = &rt6->rt6i_dst;
5440                 rt6_src = &rt6->rt6i_src;
5441                 rt6_flags = rt6->rt6i_flags;
5442         } else {
5443                 rt6_dst = &rt->fib6_dst;
5444                 rt6_src = &rt->fib6_src;
5445                 rt6_flags = rt->fib6_flags;
5446         }
5447
5448         rtm = nlmsg_data(nlh);
5449         rtm->rtm_family = AF_INET6;
5450         rtm->rtm_dst_len = rt6_dst->plen;
5451         rtm->rtm_src_len = rt6_src->plen;
5452         rtm->rtm_tos = 0;
5453         if (rt->fib6_table)
5454                 table = rt->fib6_table->tb6_id;
5455         else
5456                 table = RT6_TABLE_UNSPEC;
5457         rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
5458         if (nla_put_u32(skb, RTA_TABLE, table))
5459                 goto nla_put_failure;
5460
5461         rtm->rtm_type = rt->fib6_type;
5462         rtm->rtm_flags = 0;
5463         rtm->rtm_scope = RT_SCOPE_UNIVERSE;
5464         rtm->rtm_protocol = rt->fib6_protocol;
5465
5466         if (rt6_flags & RTF_CACHE)
5467                 rtm->rtm_flags |= RTM_F_CLONED;
5468
5469         if (dest) {
5470                 if (nla_put_in6_addr(skb, RTA_DST, dest))
5471                         goto nla_put_failure;
5472                 rtm->rtm_dst_len = 128;
5473         } else if (rtm->rtm_dst_len)
5474                 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
5475                         goto nla_put_failure;
5476 #ifdef CONFIG_IPV6_SUBTREES
5477         if (src) {
5478                 if (nla_put_in6_addr(skb, RTA_SRC, src))
5479                         goto nla_put_failure;
5480                 rtm->rtm_src_len = 128;
5481         } else if (rtm->rtm_src_len &&
5482                    nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
5483                 goto nla_put_failure;
5484 #endif
5485         if (iif) {
5486 #ifdef CONFIG_IPV6_MROUTE
5487                 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
5488                         int err = ip6mr_get_route(net, skb, rtm, portid);
5489
5490                         if (err == 0)
5491                                 return 0;
5492                         if (err < 0)
5493                                 goto nla_put_failure;
5494                 } else
5495 #endif
5496                         if (nla_put_u32(skb, RTA_IIF, iif))
5497                                 goto nla_put_failure;
5498         } else if (dest) {
5499                 struct in6_addr saddr_buf;
5500                 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
5501                     nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5502                         goto nla_put_failure;
5503         }
5504
5505         if (rt->fib6_prefsrc.plen) {
5506                 struct in6_addr saddr_buf;
5507                 saddr_buf = rt->fib6_prefsrc.addr;
5508                 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5509                         goto nla_put_failure;
5510         }
5511
5512         pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5513         if (rtnetlink_put_metrics(skb, pmetrics) < 0)
5514                 goto nla_put_failure;
5515
5516         if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
5517                 goto nla_put_failure;
5518
5519         /* For multipath routes, walk the siblings list and add
5520          * each as a nexthop within RTA_MULTIPATH.
5521          */
5522         if (rt6) {
5523                 if (rt6_flags & RTF_GATEWAY &&
5524                     nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5525                         goto nla_put_failure;
5526
5527                 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5528                         goto nla_put_failure;
5529         } else if (rt->fib6_nsiblings) {
5530                 struct fib6_info *sibling, *next_sibling;
5531                 struct nlattr *mp;
5532
5533                 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5534                 if (!mp)
5535                         goto nla_put_failure;
5536
5537                 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
5538                                     rt->fib6_nh->fib_nh_weight, AF_INET6) < 0)
5539                         goto nla_put_failure;
5540
5541                 list_for_each_entry_safe(sibling, next_sibling,
5542                                          &rt->fib6_siblings, fib6_siblings) {
5543                         if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
5544                                             sibling->fib6_nh->fib_nh_weight,
5545                                             AF_INET6) < 0)
5546                                 goto nla_put_failure;
5547                 }
5548
5549                 nla_nest_end(skb, mp);
5550         } else if (rt->nh) {
5551                 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5552                         goto nla_put_failure;
5553
5554                 if (nexthop_is_blackhole(rt->nh))
5555                         rtm->rtm_type = RTN_BLACKHOLE;
5556
5557                 if (rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
5558                         goto nla_put_failure;
5559
5560                 rtm->rtm_flags |= nh_flags;
5561         } else {
5562                 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
5563                                      &nh_flags, false) < 0)
5564                         goto nla_put_failure;
5565
5566                 rtm->rtm_flags |= nh_flags;
5567         }
5568
5569         if (rt6_flags & RTF_EXPIRES) {
5570                 expires = dst ? dst->expires : rt->expires;
5571                 expires -= jiffies;
5572         }
5573
5574         if (!dst) {
5575                 if (rt->offload)
5576                         rtm->rtm_flags |= RTM_F_OFFLOAD;
5577                 if (rt->trap)
5578                         rtm->rtm_flags |= RTM_F_TRAP;
5579         }
5580
5581         if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
5582                 goto nla_put_failure;
5583
5584         if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
5585                 goto nla_put_failure;
5586
5587
5588         nlmsg_end(skb, nlh);
5589         return 0;
5590
5591 nla_put_failure:
5592         nlmsg_cancel(skb, nlh);
5593         return -EMSGSIZE;
5594 }
5595
5596 static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5597 {
5598         const struct net_device *dev = arg;
5599
5600         if (nh->fib_nh_dev == dev)
5601                 return 1;
5602
5603         return 0;
5604 }
5605
5606 static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5607                                const struct net_device *dev)
5608 {
5609         if (f6i->nh) {
5610                 struct net_device *_dev = (struct net_device *)dev;
5611
5612                 return !!nexthop_for_each_fib6_nh(f6i->nh,
5613                                                   fib6_info_nh_uses_dev,
5614                                                   _dev);
5615         }
5616
5617         if (f6i->fib6_nh->fib_nh_dev == dev)
5618                 return true;
5619
5620         if (f6i->fib6_nsiblings) {
5621                 struct fib6_info *sibling, *next_sibling;
5622
5623                 list_for_each_entry_safe(sibling, next_sibling,
5624                                          &f6i->fib6_siblings, fib6_siblings) {
5625                         if (sibling->fib6_nh->fib_nh_dev == dev)
5626                                 return true;
5627                 }
5628         }
5629
5630         return false;
5631 }
5632
5633 struct fib6_nh_exception_dump_walker {
5634         struct rt6_rtnl_dump_arg *dump;
5635         struct fib6_info *rt;
5636         unsigned int flags;
5637         unsigned int skip;
5638         unsigned int count;
5639 };
5640
5641 static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
5642 {
5643         struct fib6_nh_exception_dump_walker *w = arg;
5644         struct rt6_rtnl_dump_arg *dump = w->dump;
5645         struct rt6_exception_bucket *bucket;
5646         struct rt6_exception *rt6_ex;
5647         int i, err;
5648
5649         bucket = fib6_nh_get_excptn_bucket(nh, NULL);
5650         if (!bucket)
5651                 return 0;
5652
5653         for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
5654                 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
5655                         if (w->skip) {
5656                                 w->skip--;
5657                                 continue;
5658                         }
5659
5660                         /* Expiration of entries doesn't bump sernum, insertion
5661                          * does. Removal is triggered by insertion, so we can
5662                          * rely on the fact that if entries change between two
5663                          * partial dumps, this node is scanned again completely,
5664                          * see rt6_insert_exception() and fib6_dump_table().
5665                          *
5666                          * Count expired entries we go through as handled
5667                          * entries that we'll skip next time, in case of partial
5668                          * node dump. Otherwise, if entries expire meanwhile,
5669                          * we'll skip the wrong amount.
5670                          */
5671                         if (rt6_check_expired(rt6_ex->rt6i)) {
5672                                 w->count++;
5673                                 continue;
5674                         }
5675
5676                         err = rt6_fill_node(dump->net, dump->skb, w->rt,
5677                                             &rt6_ex->rt6i->dst, NULL, NULL, 0,
5678                                             RTM_NEWROUTE,
5679                                             NETLINK_CB(dump->cb->skb).portid,
5680                                             dump->cb->nlh->nlmsg_seq, w->flags);
5681                         if (err)
5682                                 return err;
5683
5684                         w->count++;
5685                 }
5686                 bucket++;
5687         }
5688
5689         return 0;
5690 }
5691
5692 /* Return -1 if done with node, number of handled routes on partial dump */
5693 int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
5694 {
5695         struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
5696         struct fib_dump_filter *filter = &arg->filter;
5697         unsigned int flags = NLM_F_MULTI;
5698         struct net *net = arg->net;
5699         int count = 0;
5700
5701         if (rt == net->ipv6.fib6_null_entry)
5702                 return -1;
5703
5704         if ((filter->flags & RTM_F_PREFIX) &&
5705             !(rt->fib6_flags & RTF_PREFIX_RT)) {
5706                 /* success since this is not a prefix route */
5707                 return -1;
5708         }
5709         if (filter->filter_set &&
5710             ((filter->rt_type  && rt->fib6_type != filter->rt_type) ||
5711              (filter->dev      && !fib6_info_uses_dev(rt, filter->dev)) ||
5712              (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5713                 return -1;
5714         }
5715
5716         if (filter->filter_set ||
5717             !filter->dump_routes || !filter->dump_exceptions) {
5718                 flags |= NLM_F_DUMP_FILTERED;
5719         }
5720
5721         if (filter->dump_routes) {
5722                 if (skip) {
5723                         skip--;
5724                 } else {
5725                         if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
5726                                           0, RTM_NEWROUTE,
5727                                           NETLINK_CB(arg->cb->skb).portid,
5728                                           arg->cb->nlh->nlmsg_seq, flags)) {
5729                                 return 0;
5730                         }
5731                         count++;
5732                 }
5733         }
5734
5735         if (filter->dump_exceptions) {
5736                 struct fib6_nh_exception_dump_walker w = { .dump = arg,
5737                                                            .rt = rt,
5738                                                            .flags = flags,
5739                                                            .skip = skip,
5740                                                            .count = 0 };
5741                 int err;
5742
5743                 rcu_read_lock();
5744                 if (rt->nh) {
5745                         err = nexthop_for_each_fib6_nh(rt->nh,
5746                                                        rt6_nh_dump_exceptions,
5747                                                        &w);
5748                 } else {
5749                         err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
5750                 }
5751                 rcu_read_unlock();
5752
5753                 if (err)
5754                         return count += w.count;
5755         }
5756
5757         return -1;
5758 }
5759
5760 static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5761                                         const struct nlmsghdr *nlh,
5762                                         struct nlattr **tb,
5763                                         struct netlink_ext_ack *extack)
5764 {
5765         struct rtmsg *rtm;
5766         int i, err;
5767
5768         if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5769                 NL_SET_ERR_MSG_MOD(extack,
5770                                    "Invalid header for get route request");
5771                 return -EINVAL;
5772         }
5773
5774         if (!netlink_strict_get_check(skb))
5775                 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5776                                               rtm_ipv6_policy, extack);
5777
5778         rtm = nlmsg_data(nlh);
5779         if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5780             (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5781             rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5782             rtm->rtm_type) {
5783                 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5784                 return -EINVAL;
5785         }
5786         if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5787                 NL_SET_ERR_MSG_MOD(extack,
5788                                    "Invalid flags for get route request");
5789                 return -EINVAL;
5790         }
5791
5792         err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5793                                             rtm_ipv6_policy, extack);
5794         if (err)
5795                 return err;
5796
5797         if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5798             (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5799                 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5800                 return -EINVAL;
5801         }
5802
5803         for (i = 0; i <= RTA_MAX; i++) {
5804                 if (!tb[i])
5805                         continue;
5806
5807                 switch (i) {
5808                 case RTA_SRC:
5809                 case RTA_DST:
5810                 case RTA_IIF:
5811                 case RTA_OIF:
5812                 case RTA_MARK:
5813                 case RTA_UID:
5814                 case RTA_SPORT:
5815                 case RTA_DPORT:
5816                 case RTA_IP_PROTO:
5817                         break;
5818                 default:
5819                         NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
5820                         return -EINVAL;
5821                 }
5822         }
5823
5824         return 0;
5825 }
5826
5827 static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5828                               struct netlink_ext_ack *extack)
5829 {
5830         struct net *net = sock_net(in_skb->sk);
5831         struct nlattr *tb[RTA_MAX+1];
5832         int err, iif = 0, oif = 0;
5833         struct fib6_info *from;
5834         struct dst_entry *dst;
5835         struct rt6_info *rt;
5836         struct sk_buff *skb;
5837         struct rtmsg *rtm;
5838         struct flowi6 fl6 = {};
5839         bool fibmatch;
5840
5841         err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
5842         if (err < 0)
5843                 goto errout;
5844
5845         err = -EINVAL;
5846         rtm = nlmsg_data(nlh);
5847         fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
5848         fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
5849
5850         if (tb[RTA_SRC]) {
5851                 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5852                         goto errout;
5853
5854                 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
5855         }
5856
5857         if (tb[RTA_DST]) {
5858                 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5859                         goto errout;
5860
5861                 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
5862         }
5863
5864         if (tb[RTA_IIF])
5865                 iif = nla_get_u32(tb[RTA_IIF]);
5866
5867         if (tb[RTA_OIF])
5868                 oif = nla_get_u32(tb[RTA_OIF]);
5869
5870         if (tb[RTA_MARK])
5871                 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
5872
5873         if (tb[RTA_UID])
5874                 fl6.flowi6_uid = make_kuid(current_user_ns(),
5875                                            nla_get_u32(tb[RTA_UID]));
5876         else
5877                 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5878
5879         if (tb[RTA_SPORT])
5880                 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5881
5882         if (tb[RTA_DPORT])
5883                 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5884
5885         if (tb[RTA_IP_PROTO]) {
5886                 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
5887                                                   &fl6.flowi6_proto, AF_INET6,
5888                                                   extack);
5889                 if (err)
5890                         goto errout;
5891         }
5892
5893         if (iif) {
5894                 struct net_device *dev;
5895                 int flags = 0;
5896
5897                 rcu_read_lock();
5898
5899                 dev = dev_get_by_index_rcu(net, iif);
5900                 if (!dev) {
5901                         rcu_read_unlock();
5902                         err = -ENODEV;
5903                         goto errout;
5904                 }
5905
5906                 fl6.flowi6_iif = iif;
5907
5908                 if (!ipv6_addr_any(&fl6.saddr))
5909                         flags |= RT6_LOOKUP_F_HAS_SADDR;
5910
5911                 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
5912
5913                 rcu_read_unlock();
5914         } else {
5915                 fl6.flowi6_oif = oif;
5916
5917                 dst = ip6_route_output(net, NULL, &fl6);
5918         }
5919
5920
5921         rt = container_of(dst, struct rt6_info, dst);
5922         if (rt->dst.error) {
5923                 err = rt->dst.error;
5924                 ip6_rt_put(rt);
5925                 goto errout;
5926         }
5927
5928         if (rt == net->ipv6.ip6_null_entry) {
5929                 err = rt->dst.error;
5930                 ip6_rt_put(rt);
5931                 goto errout;
5932         }
5933
5934         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
5935         if (!skb) {
5936                 ip6_rt_put(rt);
5937                 err = -ENOBUFS;
5938                 goto errout;
5939         }
5940
5941         skb_dst_set(skb, &rt->dst);
5942
5943         rcu_read_lock();
5944         from = rcu_dereference(rt->from);
5945         if (from) {
5946                 if (fibmatch)
5947                         err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5948                                             iif, RTM_NEWROUTE,
5949                                             NETLINK_CB(in_skb).portid,
5950                                             nlh->nlmsg_seq, 0);
5951                 else
5952                         err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5953                                             &fl6.saddr, iif, RTM_NEWROUTE,
5954                                             NETLINK_CB(in_skb).portid,
5955                                             nlh->nlmsg_seq, 0);
5956         } else {
5957                 err = -ENETUNREACH;
5958         }
5959         rcu_read_unlock();
5960
5961         if (err < 0) {
5962                 kfree_skb(skb);
5963                 goto errout;
5964         }
5965
5966         err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
5967 errout:
5968         return err;
5969 }
5970
5971 void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
5972                      unsigned int nlm_flags)
5973 {
5974         struct sk_buff *skb;
5975         struct net *net = info->nl_net;
5976         u32 seq;
5977         int err;
5978
5979         err = -ENOBUFS;
5980         seq = info->nlh ? info->nlh->nlmsg_seq : 0;
5981
5982         skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
5983         if (!skb)
5984                 goto errout;
5985
5986         err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5987                             event, info->portid, seq, nlm_flags);
5988         if (err < 0) {
5989                 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
5990                 WARN_ON(err == -EMSGSIZE);
5991                 kfree_skb(skb);
5992                 goto errout;
5993         }
5994         rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
5995                     info->nlh, gfp_any());
5996         return;
5997 errout:
5998         if (err < 0)
5999                 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6000 }
6001
6002 void fib6_rt_update(struct net *net, struct fib6_info *rt,
6003                     struct nl_info *info)
6004 {
6005         u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6006         struct sk_buff *skb;
6007         int err = -ENOBUFS;
6008
6009         /* call_fib6_entry_notifiers will be removed when in-kernel notifier
6010          * is implemented and supported for nexthop objects
6011          */
6012         call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
6013
6014         skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6015         if (!skb)
6016                 goto errout;
6017
6018         err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6019                             RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
6020         if (err < 0) {
6021                 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6022                 WARN_ON(err == -EMSGSIZE);
6023                 kfree_skb(skb);
6024                 goto errout;
6025         }
6026         rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6027                     info->nlh, gfp_any());
6028         return;
6029 errout:
6030         if (err < 0)
6031                 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6032 }
6033
6034 static int ip6_route_dev_notify(struct notifier_block *this,
6035                                 unsigned long event, void *ptr)
6036 {
6037         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6038         struct net *net = dev_net(dev);
6039
6040         if (!(dev->flags & IFF_LOOPBACK))
6041                 return NOTIFY_OK;
6042
6043         if (event == NETDEV_REGISTER) {
6044                 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
6045                 net->ipv6.ip6_null_entry->dst.dev = dev;
6046                 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
6047 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6048                 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
6049                 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
6050                 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
6051                 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
6052 #endif
6053          } else if (event == NETDEV_UNREGISTER &&
6054                     dev->reg_state != NETREG_UNREGISTERED) {
6055                 /* NETDEV_UNREGISTER could be fired for multiple times by
6056                  * netdev_wait_allrefs(). Make sure we only call this once.
6057                  */
6058                 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
6059 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6060                 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
6061                 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
6062 #endif
6063         }
6064
6065         return NOTIFY_OK;
6066 }
6067
6068 /*
6069  *      /proc
6070  */
6071
6072 #ifdef CONFIG_PROC_FS
6073 static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6074 {
6075         struct net *net = (struct net *)seq->private;
6076         seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
6077                    net->ipv6.rt6_stats->fib_nodes,
6078                    net->ipv6.rt6_stats->fib_route_nodes,
6079                    atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
6080                    net->ipv6.rt6_stats->fib_rt_entries,
6081                    net->ipv6.rt6_stats->fib_rt_cache,
6082                    dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
6083                    net->ipv6.rt6_stats->fib_discarded_routes);
6084
6085         return 0;
6086 }
6087 #endif  /* CONFIG_PROC_FS */
6088
6089 #ifdef CONFIG_SYSCTL
6090
6091 static
6092 int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
6093                               void __user *buffer, size_t *lenp, loff_t *ppos)
6094 {
6095         struct net *net;
6096         int delay;
6097         int ret;
6098         if (!write)
6099                 return -EINVAL;
6100
6101         net = (struct net *)ctl->extra1;
6102         delay = net->ipv6.sysctl.flush_delay;
6103         ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6104         if (ret)
6105                 return ret;
6106
6107         fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
6108         return 0;
6109 }
6110
6111 static struct ctl_table ipv6_route_table_template[] = {
6112         {
6113                 .procname       =       "flush",
6114                 .data           =       &init_net.ipv6.sysctl.flush_delay,
6115                 .maxlen         =       sizeof(int),
6116                 .mode           =       0200,
6117                 .proc_handler   =       ipv6_sysctl_rtcache_flush
6118         },
6119         {
6120                 .procname       =       "gc_thresh",
6121                 .data           =       &ip6_dst_ops_template.gc_thresh,
6122                 .maxlen         =       sizeof(int),
6123                 .mode           =       0644,
6124                 .proc_handler   =       proc_dointvec,
6125         },
6126         {
6127                 .procname       =       "max_size",
6128                 .data           =       &init_net.ipv6.sysctl.ip6_rt_max_size,
6129                 .maxlen         =       sizeof(int),
6130                 .mode           =       0644,
6131                 .proc_handler   =       proc_dointvec,
6132         },
6133         {
6134                 .procname       =       "gc_min_interval",
6135                 .data           =       &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6136                 .maxlen         =       sizeof(int),
6137                 .mode           =       0644,
6138                 .proc_handler   =       proc_dointvec_jiffies,
6139         },
6140         {
6141                 .procname       =       "gc_timeout",
6142                 .data           =       &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
6143                 .maxlen         =       sizeof(int),
6144                 .mode           =       0644,
6145                 .proc_handler   =       proc_dointvec_jiffies,
6146         },
6147         {
6148                 .procname       =       "gc_interval",
6149                 .data           =       &init_net.ipv6.sysctl.ip6_rt_gc_interval,
6150                 .maxlen         =       sizeof(int),
6151                 .mode           =       0644,
6152                 .proc_handler   =       proc_dointvec_jiffies,
6153         },
6154         {
6155                 .procname       =       "gc_elasticity",
6156                 .data           =       &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
6157                 .maxlen         =       sizeof(int),
6158                 .mode           =       0644,
6159                 .proc_handler   =       proc_dointvec,
6160         },
6161         {
6162                 .procname       =       "mtu_expires",
6163                 .data           =       &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
6164                 .maxlen         =       sizeof(int),
6165                 .mode           =       0644,
6166                 .proc_handler   =       proc_dointvec_jiffies,
6167         },
6168         {
6169                 .procname       =       "min_adv_mss",
6170                 .data           =       &init_net.ipv6.sysctl.ip6_rt_min_advmss,
6171                 .maxlen         =       sizeof(int),
6172                 .mode           =       0644,
6173                 .proc_handler   =       proc_dointvec,
6174         },
6175         {
6176                 .procname       =       "gc_min_interval_ms",
6177                 .data           =       &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6178                 .maxlen         =       sizeof(int),
6179                 .mode           =       0644,
6180                 .proc_handler   =       proc_dointvec_ms_jiffies,
6181         },
6182         {
6183                 .procname       =       "skip_notify_on_dev_down",
6184                 .data           =       &init_net.ipv6.sysctl.skip_notify_on_dev_down,
6185                 .maxlen         =       sizeof(int),
6186                 .mode           =       0644,
6187                 .proc_handler   =       proc_dointvec_minmax,
6188                 .extra1         =       SYSCTL_ZERO,
6189                 .extra2         =       SYSCTL_ONE,
6190         },
6191         { }
6192 };
6193
6194 struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
6195 {
6196         struct ctl_table *table;
6197
6198         table = kmemdup(ipv6_route_table_template,
6199                         sizeof(ipv6_route_table_template),
6200                         GFP_KERNEL);
6201
6202         if (table) {
6203                 table[0].data = &net->ipv6.sysctl.flush_delay;
6204                 table[0].extra1 = net;
6205                 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
6206                 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
6207                 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6208                 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6209                 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6210                 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6211                 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6212                 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
6213                 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6214                 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
6215
6216                 /* Don't export sysctls to unprivileged users */
6217                 if (net->user_ns != &init_user_ns)
6218                         table[0].procname = NULL;
6219         }
6220
6221         return table;
6222 }
6223 #endif
6224
6225 static int __net_init ip6_route_net_init(struct net *net)
6226 {
6227         int ret = -ENOMEM;
6228
6229         memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6230                sizeof(net->ipv6.ip6_dst_ops));
6231
6232         if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6233                 goto out_ip6_dst_ops;
6234
6235         net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
6236         if (!net->ipv6.fib6_null_entry)
6237                 goto out_ip6_dst_entries;
6238         memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6239                sizeof(*net->ipv6.fib6_null_entry));
6240
6241         net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6242                                            sizeof(*net->ipv6.ip6_null_entry),
6243                                            GFP_KERNEL);
6244         if (!net->ipv6.ip6_null_entry)
6245                 goto out_fib6_null_entry;
6246         net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6247         dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6248                          ip6_template_metrics, true);
6249         INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
6250
6251 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6252         net->ipv6.fib6_has_custom_rules = false;
6253         net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6254                                                sizeof(*net->ipv6.ip6_prohibit_entry),
6255                                                GFP_KERNEL);
6256         if (!net->ipv6.ip6_prohibit_entry)
6257                 goto out_ip6_null_entry;
6258         net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6259         dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6260                          ip6_template_metrics, true);
6261         INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
6262
6263         net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6264                                                sizeof(*net->ipv6.ip6_blk_hole_entry),
6265                                                GFP_KERNEL);
6266         if (!net->ipv6.ip6_blk_hole_entry)
6267                 goto out_ip6_prohibit_entry;
6268         net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6269         dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6270                          ip6_template_metrics, true);
6271         INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
6272 #ifdef CONFIG_IPV6_SUBTREES
6273         net->ipv6.fib6_routes_require_src = 0;
6274 #endif
6275 #endif
6276
6277         net->ipv6.sysctl.flush_delay = 0;
6278         net->ipv6.sysctl.ip6_rt_max_size = 4096;
6279         net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6280         net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6281         net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6282         net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6283         net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6284         net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
6285         net->ipv6.sysctl.skip_notify_on_dev_down = 0;
6286
6287         net->ipv6.ip6_rt_gc_expire = 30*HZ;
6288
6289         ret = 0;
6290 out:
6291         return ret;
6292
6293 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6294 out_ip6_prohibit_entry:
6295         kfree(net->ipv6.ip6_prohibit_entry);
6296 out_ip6_null_entry:
6297         kfree(net->ipv6.ip6_null_entry);
6298 #endif
6299 out_fib6_null_entry:
6300         kfree(net->ipv6.fib6_null_entry);
6301 out_ip6_dst_entries:
6302         dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6303 out_ip6_dst_ops:
6304         goto out;
6305 }
6306
6307 static void __net_exit ip6_route_net_exit(struct net *net)
6308 {
6309         kfree(net->ipv6.fib6_null_entry);
6310         kfree(net->ipv6.ip6_null_entry);
6311 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6312         kfree(net->ipv6.ip6_prohibit_entry);
6313         kfree(net->ipv6.ip6_blk_hole_entry);
6314 #endif
6315         dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6316 }
6317
6318 static int __net_init ip6_route_net_init_late(struct net *net)
6319 {
6320 #ifdef CONFIG_PROC_FS
6321         proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
6322                         sizeof(struct ipv6_route_iter));
6323         proc_create_net_single("rt6_stats", 0444, net->proc_net,
6324                         rt6_stats_seq_show, NULL);
6325 #endif
6326         return 0;
6327 }
6328
6329 static void __net_exit ip6_route_net_exit_late(struct net *net)
6330 {
6331 #ifdef CONFIG_PROC_FS
6332         remove_proc_entry("ipv6_route", net->proc_net);
6333         remove_proc_entry("rt6_stats", net->proc_net);
6334 #endif
6335 }
6336
6337 static struct pernet_operations ip6_route_net_ops = {
6338         .init = ip6_route_net_init,
6339         .exit = ip6_route_net_exit,
6340 };
6341
6342 static int __net_init ipv6_inetpeer_init(struct net *net)
6343 {
6344         struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6345
6346         if (!bp)
6347                 return -ENOMEM;
6348         inet_peer_base_init(bp);
6349         net->ipv6.peers = bp;
6350         return 0;
6351 }
6352
6353 static void __net_exit ipv6_inetpeer_exit(struct net *net)
6354 {
6355         struct inet_peer_base *bp = net->ipv6.peers;
6356
6357         net->ipv6.peers = NULL;
6358         inetpeer_invalidate_tree(bp);
6359         kfree(bp);
6360 }
6361
6362 static struct pernet_operations ipv6_inetpeer_ops = {
6363         .init   =       ipv6_inetpeer_init,
6364         .exit   =       ipv6_inetpeer_exit,
6365 };
6366
6367 static struct pernet_operations ip6_route_net_late_ops = {
6368         .init = ip6_route_net_init_late,
6369         .exit = ip6_route_net_exit_late,
6370 };
6371
6372 static struct notifier_block ip6_route_dev_notifier = {
6373         .notifier_call = ip6_route_dev_notify,
6374         .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
6375 };
6376
6377 void __init ip6_route_init_special_entries(void)
6378 {
6379         /* Registering of the loopback is done before this portion of code,
6380          * the loopback reference in rt6_info will not be taken, do it
6381          * manually for init_net */
6382         init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
6383         init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6384         init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6385   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6386         init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6387         init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6388         init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6389         init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6390   #endif
6391 }
6392
6393 int __init ip6_route_init(void)
6394 {
6395         int ret;
6396         int cpu;
6397
6398         ret = -ENOMEM;
6399         ip6_dst_ops_template.kmem_cachep =
6400                 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6401                                   SLAB_HWCACHE_ALIGN, NULL);
6402         if (!ip6_dst_ops_template.kmem_cachep)
6403                 goto out;
6404
6405         ret = dst_entries_init(&ip6_dst_blackhole_ops);
6406         if (ret)
6407                 goto out_kmem_cache;
6408
6409         ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6410         if (ret)
6411                 goto out_dst_entries;
6412
6413         ret = register_pernet_subsys(&ip6_route_net_ops);
6414         if (ret)
6415                 goto out_register_inetpeer;
6416
6417         ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6418
6419         ret = fib6_init();
6420         if (ret)
6421                 goto out_register_subsys;
6422
6423         ret = xfrm6_init();
6424         if (ret)
6425                 goto out_fib6_init;
6426
6427         ret = fib6_rules_init();
6428         if (ret)
6429                 goto xfrm6_init;
6430
6431         ret = register_pernet_subsys(&ip6_route_net_late_ops);
6432         if (ret)
6433                 goto fib6_rules_init;
6434
6435         ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6436                                    inet6_rtm_newroute, NULL, 0);
6437         if (ret < 0)
6438                 goto out_register_late_subsys;
6439
6440         ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6441                                    inet6_rtm_delroute, NULL, 0);
6442         if (ret < 0)
6443                 goto out_register_late_subsys;
6444
6445         ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6446                                    inet6_rtm_getroute, NULL,
6447                                    RTNL_FLAG_DOIT_UNLOCKED);
6448         if (ret < 0)
6449                 goto out_register_late_subsys;
6450
6451         ret = register_netdevice_notifier(&ip6_route_dev_notifier);
6452         if (ret)
6453                 goto out_register_late_subsys;
6454
6455         for_each_possible_cpu(cpu) {
6456                 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6457
6458                 INIT_LIST_HEAD(&ul->head);
6459                 spin_lock_init(&ul->lock);
6460         }
6461
6462 out:
6463         return ret;
6464
6465 out_register_late_subsys:
6466         rtnl_unregister_all(PF_INET6);
6467         unregister_pernet_subsys(&ip6_route_net_late_ops);
6468 fib6_rules_init:
6469         fib6_rules_cleanup();
6470 xfrm6_init:
6471         xfrm6_fini();
6472 out_fib6_init:
6473         fib6_gc_cleanup();
6474 out_register_subsys:
6475         unregister_pernet_subsys(&ip6_route_net_ops);
6476 out_register_inetpeer:
6477         unregister_pernet_subsys(&ipv6_inetpeer_ops);
6478 out_dst_entries:
6479         dst_entries_destroy(&ip6_dst_blackhole_ops);
6480 out_kmem_cache:
6481         kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6482         goto out;
6483 }
6484
6485 void ip6_route_cleanup(void)
6486 {
6487         unregister_netdevice_notifier(&ip6_route_dev_notifier);
6488         unregister_pernet_subsys(&ip6_route_net_late_ops);
6489         fib6_rules_cleanup();
6490         xfrm6_fini();
6491         fib6_gc_cleanup();
6492         unregister_pernet_subsys(&ipv6_inetpeer_ops);
6493         unregister_pernet_subsys(&ip6_route_net_ops);
6494         dst_entries_destroy(&ip6_dst_blackhole_ops);
6495         kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6496 }
This page took 0.39354 seconds and 4 git commands to generate.