net: ipv6: autoconf routes into per-device tables
[firefly-linux-kernel-4.4.55.git] / net / ipv6 / route.c
1 /*
2  *      Linux INET6 implementation
3  *      FIB front-end.
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>
7  *
8  *      This program is free software; you can redistribute it and/or
9  *      modify it under the terms of the GNU General Public License
10  *      as published by the Free Software Foundation; either version
11  *      2 of the License, or (at your option) any later version.
12  */
13
14 /*      Changes:
15  *
16  *      YOSHIFUJI Hideaki @USAGI
17  *              reworked default router selection.
18  *              - respect outgoing interface
19  *              - select from (probably) reachable routers (i.e.
20  *              routers in REACHABLE, STALE, DELAY or PROBE states).
21  *              - always select the same router if it is (probably)
22  *              reachable.  otherwise, round-robin the list.
23  *      Ville Nuorvala
24  *              Fixed routing subtrees.
25  */
26
27 #define pr_fmt(fmt) "IPv6: " fmt
28
29 #include <linux/capability.h>
30 #include <linux/errno.h>
31 #include <linux/export.h>
32 #include <linux/types.h>
33 #include <linux/times.h>
34 #include <linux/socket.h>
35 #include <linux/sockios.h>
36 #include <linux/net.h>
37 #include <linux/route.h>
38 #include <linux/netdevice.h>
39 #include <linux/in6.h>
40 #include <linux/mroute6.h>
41 #include <linux/init.h>
42 #include <linux/if_arp.h>
43 #include <linux/proc_fs.h>
44 #include <linux/seq_file.h>
45 #include <linux/nsproxy.h>
46 #include <linux/slab.h>
47 #include <net/net_namespace.h>
48 #include <net/snmp.h>
49 #include <net/ipv6.h>
50 #include <net/ip6_fib.h>
51 #include <net/ip6_route.h>
52 #include <net/ndisc.h>
53 #include <net/addrconf.h>
54 #include <net/tcp.h>
55 #include <linux/rtnetlink.h>
56 #include <net/dst.h>
57 #include <net/xfrm.h>
58 #include <net/netevent.h>
59 #include <net/netlink.h>
60 #include <net/nexthop.h>
61
62 #include <asm/uaccess.h>
63
64 #ifdef CONFIG_SYSCTL
65 #include <linux/sysctl.h>
66 #endif
67
68 static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
69                                     const struct in6_addr *dest);
70 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
71 static unsigned int      ip6_default_advmss(const struct dst_entry *dst);
72 static unsigned int      ip6_mtu(const struct dst_entry *dst);
73 static struct dst_entry *ip6_negative_advice(struct dst_entry *);
74 static void             ip6_dst_destroy(struct dst_entry *);
75 static void             ip6_dst_ifdown(struct dst_entry *,
76                                        struct net_device *dev, int how);
77 static int               ip6_dst_gc(struct dst_ops *ops);
78
79 static int              ip6_pkt_discard(struct sk_buff *skb);
80 static int              ip6_pkt_discard_out(struct sk_buff *skb);
81 static void             ip6_link_failure(struct sk_buff *skb);
82 static void             ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
83                                            struct sk_buff *skb, u32 mtu);
84 static void             rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
85                                         struct sk_buff *skb);
86
87 #ifdef CONFIG_IPV6_ROUTE_INFO
88 static struct rt6_info *rt6_add_route_info(struct net_device *dev,
89                                            const struct in6_addr *prefix, int prefixlen,
90                                            const struct in6_addr *gwaddr, unsigned int pref);
91 static struct rt6_info *rt6_get_route_info(struct net_device *dev,
92                                            const struct in6_addr *prefix, int prefixlen,
93                                            const struct in6_addr *gwaddr);
94 #endif
95
96 static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
97 {
98         struct rt6_info *rt = (struct rt6_info *) dst;
99         struct inet_peer *peer;
100         u32 *p = NULL;
101
102         if (!(rt->dst.flags & DST_HOST))
103                 return NULL;
104
105         peer = rt6_get_peer_create(rt);
106         if (peer) {
107                 u32 *old_p = __DST_METRICS_PTR(old);
108                 unsigned long prev, new;
109
110                 p = peer->metrics;
111                 if (inet_metrics_new(peer))
112                         memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
113
114                 new = (unsigned long) p;
115                 prev = cmpxchg(&dst->_metrics, old, new);
116
117                 if (prev != old) {
118                         p = __DST_METRICS_PTR(prev);
119                         if (prev & DST_METRICS_READ_ONLY)
120                                 p = NULL;
121                 }
122         }
123         return p;
124 }
125
126 static inline const void *choose_neigh_daddr(struct rt6_info *rt,
127                                              struct sk_buff *skb,
128                                              const void *daddr)
129 {
130         struct in6_addr *p = &rt->rt6i_gateway;
131
132         if (!ipv6_addr_any(p))
133                 return (const void *) p;
134         else if (skb)
135                 return &ipv6_hdr(skb)->daddr;
136         return daddr;
137 }
138
139 static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
140                                           struct sk_buff *skb,
141                                           const void *daddr)
142 {
143         struct rt6_info *rt = (struct rt6_info *) dst;
144         struct neighbour *n;
145
146         daddr = choose_neigh_daddr(rt, skb, daddr);
147         n = __ipv6_neigh_lookup(dst->dev, daddr);
148         if (n)
149                 return n;
150         return neigh_create(&nd_tbl, daddr, dst->dev);
151 }
152
153 static struct dst_ops ip6_dst_ops_template = {
154         .family                 =       AF_INET6,
155         .protocol               =       cpu_to_be16(ETH_P_IPV6),
156         .gc                     =       ip6_dst_gc,
157         .gc_thresh              =       1024,
158         .check                  =       ip6_dst_check,
159         .default_advmss         =       ip6_default_advmss,
160         .mtu                    =       ip6_mtu,
161         .cow_metrics            =       ipv6_cow_metrics,
162         .destroy                =       ip6_dst_destroy,
163         .ifdown                 =       ip6_dst_ifdown,
164         .negative_advice        =       ip6_negative_advice,
165         .link_failure           =       ip6_link_failure,
166         .update_pmtu            =       ip6_rt_update_pmtu,
167         .redirect               =       rt6_do_redirect,
168         .local_out              =       __ip6_local_out,
169         .neigh_lookup           =       ip6_neigh_lookup,
170 };
171
172 static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
173 {
174         unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
175
176         return mtu ? : dst->dev->mtu;
177 }
178
179 static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
180                                          struct sk_buff *skb, u32 mtu)
181 {
182 }
183
184 static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
185                                       struct sk_buff *skb)
186 {
187 }
188
189 static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
190                                          unsigned long old)
191 {
192         return NULL;
193 }
194
195 static struct dst_ops ip6_dst_blackhole_ops = {
196         .family                 =       AF_INET6,
197         .protocol               =       cpu_to_be16(ETH_P_IPV6),
198         .destroy                =       ip6_dst_destroy,
199         .check                  =       ip6_dst_check,
200         .mtu                    =       ip6_blackhole_mtu,
201         .default_advmss         =       ip6_default_advmss,
202         .update_pmtu            =       ip6_rt_blackhole_update_pmtu,
203         .redirect               =       ip6_rt_blackhole_redirect,
204         .cow_metrics            =       ip6_rt_blackhole_cow_metrics,
205         .neigh_lookup           =       ip6_neigh_lookup,
206 };
207
208 static const u32 ip6_template_metrics[RTAX_MAX] = {
209         [RTAX_HOPLIMIT - 1] = 0,
210 };
211
212 static const struct rt6_info ip6_null_entry_template = {
213         .dst = {
214                 .__refcnt       = ATOMIC_INIT(1),
215                 .__use          = 1,
216                 .obsolete       = DST_OBSOLETE_FORCE_CHK,
217                 .error          = -ENETUNREACH,
218                 .input          = ip6_pkt_discard,
219                 .output         = ip6_pkt_discard_out,
220         },
221         .rt6i_flags     = (RTF_REJECT | RTF_NONEXTHOP),
222         .rt6i_protocol  = RTPROT_KERNEL,
223         .rt6i_metric    = ~(u32) 0,
224         .rt6i_ref       = ATOMIC_INIT(1),
225 };
226
227 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
228
229 static int ip6_pkt_prohibit(struct sk_buff *skb);
230 static int ip6_pkt_prohibit_out(struct sk_buff *skb);
231
232 static const struct rt6_info ip6_prohibit_entry_template = {
233         .dst = {
234                 .__refcnt       = ATOMIC_INIT(1),
235                 .__use          = 1,
236                 .obsolete       = DST_OBSOLETE_FORCE_CHK,
237                 .error          = -EACCES,
238                 .input          = ip6_pkt_prohibit,
239                 .output         = ip6_pkt_prohibit_out,
240         },
241         .rt6i_flags     = (RTF_REJECT | RTF_NONEXTHOP),
242         .rt6i_protocol  = RTPROT_KERNEL,
243         .rt6i_metric    = ~(u32) 0,
244         .rt6i_ref       = ATOMIC_INIT(1),
245 };
246
247 static const struct rt6_info ip6_blk_hole_entry_template = {
248         .dst = {
249                 .__refcnt       = ATOMIC_INIT(1),
250                 .__use          = 1,
251                 .obsolete       = DST_OBSOLETE_FORCE_CHK,
252                 .error          = -EINVAL,
253                 .input          = dst_discard,
254                 .output         = dst_discard,
255         },
256         .rt6i_flags     = (RTF_REJECT | RTF_NONEXTHOP),
257         .rt6i_protocol  = RTPROT_KERNEL,
258         .rt6i_metric    = ~(u32) 0,
259         .rt6i_ref       = ATOMIC_INIT(1),
260 };
261
262 #endif
263
264 /* allocate dst with ip6_dst_ops */
265 static inline struct rt6_info *ip6_dst_alloc(struct net *net,
266                                              struct net_device *dev,
267                                              int flags,
268                                              struct fib6_table *table)
269 {
270         struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
271                                         0, DST_OBSOLETE_FORCE_CHK, flags);
272
273         if (rt) {
274                 struct dst_entry *dst = &rt->dst;
275
276                 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
277                 rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
278                 rt->rt6i_genid = rt_genid(net);
279                 INIT_LIST_HEAD(&rt->rt6i_siblings);
280                 rt->rt6i_nsiblings = 0;
281         }
282         return rt;
283 }
284
285 static void ip6_dst_destroy(struct dst_entry *dst)
286 {
287         struct rt6_info *rt = (struct rt6_info *)dst;
288         struct inet6_dev *idev = rt->rt6i_idev;
289         struct dst_entry *from = dst->from;
290
291         if (!(rt->dst.flags & DST_HOST))
292                 dst_destroy_metrics_generic(dst);
293
294         if (idev) {
295                 rt->rt6i_idev = NULL;
296                 in6_dev_put(idev);
297         }
298
299         dst->from = NULL;
300         dst_release(from);
301
302         if (rt6_has_peer(rt)) {
303                 struct inet_peer *peer = rt6_peer_ptr(rt);
304                 inet_putpeer(peer);
305         }
306 }
307
308 void rt6_bind_peer(struct rt6_info *rt, int create)
309 {
310         struct inet_peer_base *base;
311         struct inet_peer *peer;
312
313         base = inetpeer_base_ptr(rt->_rt6i_peer);
314         if (!base)
315                 return;
316
317         peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
318         if (peer) {
319                 if (!rt6_set_peer(rt, peer))
320                         inet_putpeer(peer);
321         }
322 }
323
324 static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
325                            int how)
326 {
327         struct rt6_info *rt = (struct rt6_info *)dst;
328         struct inet6_dev *idev = rt->rt6i_idev;
329         struct net_device *loopback_dev =
330                 dev_net(dev)->loopback_dev;
331
332         if (dev != loopback_dev) {
333                 if (idev && idev->dev == dev) {
334                         struct inet6_dev *loopback_idev =
335                                 in6_dev_get(loopback_dev);
336                         if (loopback_idev) {
337                                 rt->rt6i_idev = loopback_idev;
338                                 in6_dev_put(idev);
339                         }
340                 }
341         }
342 }
343
344 static bool rt6_check_expired(const struct rt6_info *rt)
345 {
346         if (rt->rt6i_flags & RTF_EXPIRES) {
347                 if (time_after(jiffies, rt->dst.expires))
348                         return true;
349         } else if (rt->dst.from) {
350                 return rt6_check_expired((struct rt6_info *) rt->dst.from);
351         }
352         return false;
353 }
354
355 static bool rt6_need_strict(const struct in6_addr *daddr)
356 {
357         return ipv6_addr_type(daddr) &
358                 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
359 }
360
361 /* Multipath route selection:
362  *   Hash based function using packet header and flowlabel.
363  * Adapted from fib_info_hashfn()
364  */
365 static int rt6_info_hash_nhsfn(unsigned int candidate_count,
366                                const struct flowi6 *fl6)
367 {
368         unsigned int val = fl6->flowi6_proto;
369
370         val ^= ipv6_addr_hash(&fl6->daddr);
371         val ^= ipv6_addr_hash(&fl6->saddr);
372
373         /* Work only if this not encapsulated */
374         switch (fl6->flowi6_proto) {
375         case IPPROTO_UDP:
376         case IPPROTO_TCP:
377         case IPPROTO_SCTP:
378                 val ^= (__force u16)fl6->fl6_sport;
379                 val ^= (__force u16)fl6->fl6_dport;
380                 break;
381
382         case IPPROTO_ICMPV6:
383                 val ^= (__force u16)fl6->fl6_icmp_type;
384                 val ^= (__force u16)fl6->fl6_icmp_code;
385                 break;
386         }
387         /* RFC6438 recommands to use flowlabel */
388         val ^= (__force u32)fl6->flowlabel;
389
390         /* Perhaps, we need to tune, this function? */
391         val = val ^ (val >> 7) ^ (val >> 12);
392         return val % candidate_count;
393 }
394
395 static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
396                                              struct flowi6 *fl6)
397 {
398         struct rt6_info *sibling, *next_sibling;
399         int route_choosen;
400
401         route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
402         /* Don't change the route, if route_choosen == 0
403          * (siblings does not include ourself)
404          */
405         if (route_choosen)
406                 list_for_each_entry_safe(sibling, next_sibling,
407                                 &match->rt6i_siblings, rt6i_siblings) {
408                         route_choosen--;
409                         if (route_choosen == 0) {
410                                 match = sibling;
411                                 break;
412                         }
413                 }
414         return match;
415 }
416
417 /*
418  *      Route lookup. Any table->tb6_lock is implied.
419  */
420
421 static inline struct rt6_info *rt6_device_match(struct net *net,
422                                                     struct rt6_info *rt,
423                                                     const struct in6_addr *saddr,
424                                                     int oif,
425                                                     int flags)
426 {
427         struct rt6_info *local = NULL;
428         struct rt6_info *sprt;
429
430         if (!oif && ipv6_addr_any(saddr))
431                 goto out;
432
433         for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
434                 struct net_device *dev = sprt->dst.dev;
435
436                 if (oif) {
437                         if (dev->ifindex == oif)
438                                 return sprt;
439                         if (dev->flags & IFF_LOOPBACK) {
440                                 if (!sprt->rt6i_idev ||
441                                     sprt->rt6i_idev->dev->ifindex != oif) {
442                                         if (flags & RT6_LOOKUP_F_IFACE && oif)
443                                                 continue;
444                                         if (local && (!oif ||
445                                                       local->rt6i_idev->dev->ifindex == oif))
446                                                 continue;
447                                 }
448                                 local = sprt;
449                         }
450                 } else {
451                         if (ipv6_chk_addr(net, saddr, dev,
452                                           flags & RT6_LOOKUP_F_IFACE))
453                                 return sprt;
454                 }
455         }
456
457         if (oif) {
458                 if (local)
459                         return local;
460
461                 if (flags & RT6_LOOKUP_F_IFACE)
462                         return net->ipv6.ip6_null_entry;
463         }
464 out:
465         return rt;
466 }
467
468 #ifdef CONFIG_IPV6_ROUTER_PREF
469 static void rt6_probe(struct rt6_info *rt)
470 {
471         struct neighbour *neigh;
472         /*
473          * Okay, this does not seem to be appropriate
474          * for now, however, we need to check if it
475          * is really so; aka Router Reachability Probing.
476          *
477          * Router Reachability Probe MUST be rate-limited
478          * to no more than one per minute.
479          */
480         if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
481                 return;
482         rcu_read_lock_bh();
483         neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
484         if (neigh) {
485                 write_lock(&neigh->lock);
486                 if (neigh->nud_state & NUD_VALID)
487                         goto out;
488         }
489
490         if (!neigh ||
491             time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
492                 struct in6_addr mcaddr;
493                 struct in6_addr *target;
494
495                 if (neigh) {
496                         neigh->updated = jiffies;
497                         write_unlock(&neigh->lock);
498                 }
499
500                 target = (struct in6_addr *)&rt->rt6i_gateway;
501                 addrconf_addr_solict_mult(target, &mcaddr);
502                 ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL);
503         } else {
504 out:
505                 write_unlock(&neigh->lock);
506         }
507         rcu_read_unlock_bh();
508 }
509 #else
510 static inline void rt6_probe(struct rt6_info *rt)
511 {
512 }
513 #endif
514
515 /*
516  * Default Router Selection (RFC 2461 6.3.6)
517  */
518 static inline int rt6_check_dev(struct rt6_info *rt, int oif)
519 {
520         struct net_device *dev = rt->dst.dev;
521         if (!oif || dev->ifindex == oif)
522                 return 2;
523         if ((dev->flags & IFF_LOOPBACK) &&
524             rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
525                 return 1;
526         return 0;
527 }
528
529 static inline bool rt6_check_neigh(struct rt6_info *rt)
530 {
531         struct neighbour *neigh;
532         bool ret = false;
533
534         if (rt->rt6i_flags & RTF_NONEXTHOP ||
535             !(rt->rt6i_flags & RTF_GATEWAY))
536                 return true;
537
538         rcu_read_lock_bh();
539         neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
540         if (neigh) {
541                 read_lock(&neigh->lock);
542                 if (neigh->nud_state & NUD_VALID)
543                         ret = true;
544 #ifdef CONFIG_IPV6_ROUTER_PREF
545                 else if (!(neigh->nud_state & NUD_FAILED))
546                         ret = true;
547 #endif
548                 read_unlock(&neigh->lock);
549         }
550         rcu_read_unlock_bh();
551
552         return ret;
553 }
554
555 static int rt6_score_route(struct rt6_info *rt, int oif,
556                            int strict)
557 {
558         int m;
559
560         m = rt6_check_dev(rt, oif);
561         if (!m && (strict & RT6_LOOKUP_F_IFACE))
562                 return -1;
563 #ifdef CONFIG_IPV6_ROUTER_PREF
564         m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
565 #endif
566         if (!rt6_check_neigh(rt) && (strict & RT6_LOOKUP_F_REACHABLE))
567                 return -1;
568         return m;
569 }
570
571 static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
572                                    int *mpri, struct rt6_info *match)
573 {
574         int m;
575
576         if (rt6_check_expired(rt))
577                 goto out;
578
579         m = rt6_score_route(rt, oif, strict);
580         if (m < 0)
581                 goto out;
582
583         if (m > *mpri) {
584                 if (strict & RT6_LOOKUP_F_REACHABLE)
585                         rt6_probe(match);
586                 *mpri = m;
587                 match = rt;
588         } else if (strict & RT6_LOOKUP_F_REACHABLE) {
589                 rt6_probe(rt);
590         }
591
592 out:
593         return match;
594 }
595
596 static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
597                                      struct rt6_info *rr_head,
598                                      u32 metric, int oif, int strict)
599 {
600         struct rt6_info *rt, *match;
601         int mpri = -1;
602
603         match = NULL;
604         for (rt = rr_head; rt && rt->rt6i_metric == metric;
605              rt = rt->dst.rt6_next)
606                 match = find_match(rt, oif, strict, &mpri, match);
607         for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
608              rt = rt->dst.rt6_next)
609                 match = find_match(rt, oif, strict, &mpri, match);
610
611         return match;
612 }
613
614 static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
615 {
616         struct rt6_info *match, *rt0;
617         struct net *net;
618
619         rt0 = fn->rr_ptr;
620         if (!rt0)
621                 fn->rr_ptr = rt0 = fn->leaf;
622
623         match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
624
625         if (!match &&
626             (strict & RT6_LOOKUP_F_REACHABLE)) {
627                 struct rt6_info *next = rt0->dst.rt6_next;
628
629                 /* no entries matched; do round-robin */
630                 if (!next || next->rt6i_metric != rt0->rt6i_metric)
631                         next = fn->leaf;
632
633                 if (next != rt0)
634                         fn->rr_ptr = next;
635         }
636
637         net = dev_net(rt0->dst.dev);
638         return match ? match : net->ipv6.ip6_null_entry;
639 }
640
641 #ifdef CONFIG_IPV6_ROUTE_INFO
642 int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
643                   const struct in6_addr *gwaddr)
644 {
645         struct route_info *rinfo = (struct route_info *) opt;
646         struct in6_addr prefix_buf, *prefix;
647         unsigned int pref;
648         unsigned long lifetime;
649         struct rt6_info *rt;
650
651         if (len < sizeof(struct route_info)) {
652                 return -EINVAL;
653         }
654
655         /* Sanity check for prefix_len and length */
656         if (rinfo->length > 3) {
657                 return -EINVAL;
658         } else if (rinfo->prefix_len > 128) {
659                 return -EINVAL;
660         } else if (rinfo->prefix_len > 64) {
661                 if (rinfo->length < 2) {
662                         return -EINVAL;
663                 }
664         } else if (rinfo->prefix_len > 0) {
665                 if (rinfo->length < 1) {
666                         return -EINVAL;
667                 }
668         }
669
670         pref = rinfo->route_pref;
671         if (pref == ICMPV6_ROUTER_PREF_INVALID)
672                 return -EINVAL;
673
674         lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
675
676         if (rinfo->length == 3)
677                 prefix = (struct in6_addr *)rinfo->prefix;
678         else {
679                 /* this function is safe */
680                 ipv6_addr_prefix(&prefix_buf,
681                                  (struct in6_addr *)rinfo->prefix,
682                                  rinfo->prefix_len);
683                 prefix = &prefix_buf;
684         }
685
686         rt = rt6_get_route_info(dev, prefix, rinfo->prefix_len, gwaddr);
687
688         if (rt && !lifetime) {
689                 ip6_del_rt(rt);
690                 rt = NULL;
691         }
692
693         if (!rt && lifetime)
694                 rt = rt6_add_route_info(dev, prefix, rinfo->prefix_len, gwaddr, pref);
695         else if (rt)
696                 rt->rt6i_flags = RTF_ROUTEINFO |
697                                  (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
698
699         if (rt) {
700                 if (!addrconf_finite_timeout(lifetime))
701                         rt6_clean_expires(rt);
702                 else
703                         rt6_set_expires(rt, jiffies + HZ * lifetime);
704
705                 ip6_rt_put(rt);
706         }
707         return 0;
708 }
709 #endif
710
711 #define BACKTRACK(__net, saddr)                 \
712 do { \
713         if (rt == __net->ipv6.ip6_null_entry) { \
714                 struct fib6_node *pn; \
715                 while (1) { \
716                         if (fn->fn_flags & RTN_TL_ROOT) \
717                                 goto out; \
718                         pn = fn->parent; \
719                         if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
720                                 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
721                         else \
722                                 fn = pn; \
723                         if (fn->fn_flags & RTN_RTINFO) \
724                                 goto restart; \
725                 } \
726         } \
727 } while (0)
728
729 static struct rt6_info *ip6_pol_route_lookup(struct net *net,
730                                              struct fib6_table *table,
731                                              struct flowi6 *fl6, int flags)
732 {
733         struct fib6_node *fn;
734         struct rt6_info *rt;
735
736         read_lock_bh(&table->tb6_lock);
737         fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
738 restart:
739         rt = fn->leaf;
740         rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
741         if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
742                 rt = rt6_multipath_select(rt, fl6);
743         BACKTRACK(net, &fl6->saddr);
744 out:
745         dst_use(&rt->dst, jiffies);
746         read_unlock_bh(&table->tb6_lock);
747         return rt;
748
749 }
750
751 struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
752                                     int flags)
753 {
754         return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
755 }
756 EXPORT_SYMBOL_GPL(ip6_route_lookup);
757
758 struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
759                             const struct in6_addr *saddr, int oif, int strict)
760 {
761         struct flowi6 fl6 = {
762                 .flowi6_oif = oif,
763                 .daddr = *daddr,
764         };
765         struct dst_entry *dst;
766         int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
767
768         if (saddr) {
769                 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
770                 flags |= RT6_LOOKUP_F_HAS_SADDR;
771         }
772
773         dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
774         if (dst->error == 0)
775                 return (struct rt6_info *) dst;
776
777         dst_release(dst);
778
779         return NULL;
780 }
781
782 EXPORT_SYMBOL(rt6_lookup);
783
784 /* ip6_ins_rt is called with FREE table->tb6_lock.
785    It takes new route entry, the addition fails by any reason the
786    route is freed. In any case, if caller does not hold it, it may
787    be destroyed.
788  */
789
790 static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
791 {
792         int err;
793         struct fib6_table *table;
794
795         table = rt->rt6i_table;
796         write_lock_bh(&table->tb6_lock);
797         err = fib6_add(&table->tb6_root, rt, info);
798         write_unlock_bh(&table->tb6_lock);
799
800         return err;
801 }
802
803 int ip6_ins_rt(struct rt6_info *rt)
804 {
805         struct nl_info info = {
806                 .nl_net = dev_net(rt->dst.dev),
807         };
808         return __ip6_ins_rt(rt, &info);
809 }
810
811 static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
812                                       const struct in6_addr *daddr,
813                                       const struct in6_addr *saddr)
814 {
815         struct rt6_info *rt;
816
817         /*
818          *      Clone the route.
819          */
820
821         rt = ip6_rt_copy(ort, daddr);
822
823         if (rt) {
824                 if (!(rt->rt6i_flags & RTF_GATEWAY)) {
825                         if (ort->rt6i_dst.plen != 128 &&
826                             ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
827                                 rt->rt6i_flags |= RTF_ANYCAST;
828                         rt->rt6i_gateway = *daddr;
829                 }
830
831                 rt->rt6i_flags |= RTF_CACHE;
832
833 #ifdef CONFIG_IPV6_SUBTREES
834                 if (rt->rt6i_src.plen && saddr) {
835                         rt->rt6i_src.addr = *saddr;
836                         rt->rt6i_src.plen = 128;
837                 }
838 #endif
839         }
840
841         return rt;
842 }
843
844 static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
845                                         const struct in6_addr *daddr)
846 {
847         struct rt6_info *rt = ip6_rt_copy(ort, daddr);
848
849         if (rt)
850                 rt->rt6i_flags |= RTF_CACHE;
851         return rt;
852 }
853
854 static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
855                                       struct flowi6 *fl6, int flags)
856 {
857         struct fib6_node *fn;
858         struct rt6_info *rt, *nrt;
859         int strict = 0;
860         int attempts = 3;
861         int err;
862         int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
863
864         strict |= flags & RT6_LOOKUP_F_IFACE;
865
866 relookup:
867         read_lock_bh(&table->tb6_lock);
868
869 restart_2:
870         fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
871
872 restart:
873         rt = rt6_select(fn, oif, strict | reachable);
874         if (rt->rt6i_nsiblings && oif == 0)
875                 rt = rt6_multipath_select(rt, fl6);
876         BACKTRACK(net, &fl6->saddr);
877         if (rt == net->ipv6.ip6_null_entry ||
878             rt->rt6i_flags & RTF_CACHE)
879                 goto out;
880
881         dst_hold(&rt->dst);
882         read_unlock_bh(&table->tb6_lock);
883
884         if (!(rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)))
885                 nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
886         else if (!(rt->dst.flags & DST_HOST))
887                 nrt = rt6_alloc_clone(rt, &fl6->daddr);
888         else
889                 goto out2;
890
891         ip6_rt_put(rt);
892         rt = nrt ? : net->ipv6.ip6_null_entry;
893
894         dst_hold(&rt->dst);
895         if (nrt) {
896                 err = ip6_ins_rt(nrt);
897                 if (!err)
898                         goto out2;
899         }
900
901         if (--attempts <= 0)
902                 goto out2;
903
904         /*
905          * Race condition! In the gap, when table->tb6_lock was
906          * released someone could insert this route.  Relookup.
907          */
908         ip6_rt_put(rt);
909         goto relookup;
910
911 out:
912         if (reachable) {
913                 reachable = 0;
914                 goto restart_2;
915         }
916         dst_hold(&rt->dst);
917         read_unlock_bh(&table->tb6_lock);
918 out2:
919         rt->dst.lastuse = jiffies;
920         rt->dst.__use++;
921
922         return rt;
923 }
924
925 static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
926                                             struct flowi6 *fl6, int flags)
927 {
928         return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
929 }
930
931 static struct dst_entry *ip6_route_input_lookup(struct net *net,
932                                                 struct net_device *dev,
933                                                 struct flowi6 *fl6, int flags)
934 {
935         if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
936                 flags |= RT6_LOOKUP_F_IFACE;
937
938         return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
939 }
940
941 void ip6_route_input(struct sk_buff *skb)
942 {
943         const struct ipv6hdr *iph = ipv6_hdr(skb);
944         struct net *net = dev_net(skb->dev);
945         int flags = RT6_LOOKUP_F_HAS_SADDR;
946         struct flowi6 fl6 = {
947                 .flowi6_iif = skb->dev->ifindex,
948                 .daddr = iph->daddr,
949                 .saddr = iph->saddr,
950                 .flowlabel = ip6_flowinfo(iph),
951                 .flowi6_mark = skb->mark,
952                 .flowi6_proto = iph->nexthdr,
953         };
954
955         skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
956 }
957
958 static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
959                                              struct flowi6 *fl6, int flags)
960 {
961         return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
962 }
963
964 struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
965                                     struct flowi6 *fl6)
966 {
967         int flags = 0;
968
969         fl6->flowi6_iif = LOOPBACK_IFINDEX;
970
971         if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
972                 flags |= RT6_LOOKUP_F_IFACE;
973
974         if (!ipv6_addr_any(&fl6->saddr))
975                 flags |= RT6_LOOKUP_F_HAS_SADDR;
976         else if (sk)
977                 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
978
979         return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
980 }
981
982 EXPORT_SYMBOL(ip6_route_output);
983
984 struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
985 {
986         struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
987         struct dst_entry *new = NULL;
988
989         rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
990         if (rt) {
991                 new = &rt->dst;
992
993                 memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
994                 rt6_init_peer(rt, net->ipv6.peers);
995
996                 new->__use = 1;
997                 new->input = dst_discard;
998                 new->output = dst_discard;
999
1000                 if (dst_metrics_read_only(&ort->dst))
1001                         new->_metrics = ort->dst._metrics;
1002                 else
1003                         dst_copy_metrics(new, &ort->dst);
1004                 rt->rt6i_idev = ort->rt6i_idev;
1005                 if (rt->rt6i_idev)
1006                         in6_dev_hold(rt->rt6i_idev);
1007
1008                 rt->rt6i_gateway = ort->rt6i_gateway;
1009                 rt->rt6i_flags = ort->rt6i_flags;
1010                 rt->rt6i_metric = 0;
1011
1012                 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1013 #ifdef CONFIG_IPV6_SUBTREES
1014                 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1015 #endif
1016
1017                 dst_free(new);
1018         }
1019
1020         dst_release(dst_orig);
1021         return new ? new : ERR_PTR(-ENOMEM);
1022 }
1023
1024 /*
1025  *      Destination cache support functions
1026  */
1027
1028 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
1029 {
1030         struct rt6_info *rt;
1031
1032         rt = (struct rt6_info *) dst;
1033
1034         /* All IPV6 dsts are created with ->obsolete set to the value
1035          * DST_OBSOLETE_FORCE_CHK which forces validation calls down
1036          * into this function always.
1037          */
1038         if (rt->rt6i_genid != rt_genid(dev_net(rt->dst.dev)))
1039                 return NULL;
1040
1041         if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
1042                 return dst;
1043
1044         return NULL;
1045 }
1046
1047 static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
1048 {
1049         struct rt6_info *rt = (struct rt6_info *) dst;
1050
1051         if (rt) {
1052                 if (rt->rt6i_flags & RTF_CACHE) {
1053                         if (rt6_check_expired(rt)) {
1054                                 ip6_del_rt(rt);
1055                                 dst = NULL;
1056                         }
1057                 } else {
1058                         dst_release(dst);
1059                         dst = NULL;
1060                 }
1061         }
1062         return dst;
1063 }
1064
1065 static void ip6_link_failure(struct sk_buff *skb)
1066 {
1067         struct rt6_info *rt;
1068
1069         icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
1070
1071         rt = (struct rt6_info *) skb_dst(skb);
1072         if (rt) {
1073                 if (rt->rt6i_flags & RTF_CACHE)
1074                         rt6_update_expires(rt, 0);
1075                 else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
1076                         rt->rt6i_node->fn_sernum = -1;
1077         }
1078 }
1079
1080 static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
1081                                struct sk_buff *skb, u32 mtu)
1082 {
1083         struct rt6_info *rt6 = (struct rt6_info*)dst;
1084
1085         dst_confirm(dst);
1086         if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
1087                 struct net *net = dev_net(dst->dev);
1088
1089                 rt6->rt6i_flags |= RTF_MODIFIED;
1090                 if (mtu < IPV6_MIN_MTU) {
1091                         u32 features = dst_metric(dst, RTAX_FEATURES);
1092                         mtu = IPV6_MIN_MTU;
1093                         features |= RTAX_FEATURE_ALLFRAG;
1094                         dst_metric_set(dst, RTAX_FEATURES, features);
1095                 }
1096                 dst_metric_set(dst, RTAX_MTU, mtu);
1097                 rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
1098         }
1099 }
1100
1101 void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
1102                      int oif, u32 mark)
1103 {
1104         const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1105         struct dst_entry *dst;
1106         struct flowi6 fl6;
1107
1108         memset(&fl6, 0, sizeof(fl6));
1109         fl6.flowi6_oif = oif;
1110         fl6.flowi6_mark = mark;
1111         fl6.flowi6_flags = 0;
1112         fl6.daddr = iph->daddr;
1113         fl6.saddr = iph->saddr;
1114         fl6.flowlabel = ip6_flowinfo(iph);
1115
1116         dst = ip6_route_output(net, NULL, &fl6);
1117         if (!dst->error)
1118                 ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu));
1119         dst_release(dst);
1120 }
1121 EXPORT_SYMBOL_GPL(ip6_update_pmtu);
1122
1123 void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
1124 {
1125         ip6_update_pmtu(skb, sock_net(sk), mtu,
1126                         sk->sk_bound_dev_if, sk->sk_mark);
1127 }
1128 EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
1129
1130 void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
1131 {
1132         const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1133         struct dst_entry *dst;
1134         struct flowi6 fl6;
1135
1136         memset(&fl6, 0, sizeof(fl6));
1137         fl6.flowi6_oif = oif;
1138         fl6.flowi6_mark = mark;
1139         fl6.flowi6_flags = 0;
1140         fl6.daddr = iph->daddr;
1141         fl6.saddr = iph->saddr;
1142         fl6.flowlabel = ip6_flowinfo(iph);
1143
1144         dst = ip6_route_output(net, NULL, &fl6);
1145         if (!dst->error)
1146                 rt6_do_redirect(dst, NULL, skb);
1147         dst_release(dst);
1148 }
1149 EXPORT_SYMBOL_GPL(ip6_redirect);
1150
1151 void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
1152 {
1153         ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
1154 }
1155 EXPORT_SYMBOL_GPL(ip6_sk_redirect);
1156
1157 static unsigned int ip6_default_advmss(const struct dst_entry *dst)
1158 {
1159         struct net_device *dev = dst->dev;
1160         unsigned int mtu = dst_mtu(dst);
1161         struct net *net = dev_net(dev);
1162
1163         mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
1164
1165         if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
1166                 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
1167
1168         /*
1169          * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
1170          * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
1171          * IPV6_MAXPLEN is also valid and means: "any MSS,
1172          * rely only on pmtu discovery"
1173          */
1174         if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1175                 mtu = IPV6_MAXPLEN;
1176         return mtu;
1177 }
1178
1179 static unsigned int ip6_mtu(const struct dst_entry *dst)
1180 {
1181         struct inet6_dev *idev;
1182         unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1183
1184         if (mtu)
1185                 return mtu;
1186
1187         mtu = IPV6_MIN_MTU;
1188
1189         rcu_read_lock();
1190         idev = __in6_dev_get(dst->dev);
1191         if (idev)
1192                 mtu = idev->cnf.mtu6;
1193         rcu_read_unlock();
1194
1195         return mtu;
1196 }
1197
1198 static struct dst_entry *icmp6_dst_gc_list;
1199 static DEFINE_SPINLOCK(icmp6_dst_lock);
1200
1201 struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
1202                                   struct flowi6 *fl6)
1203 {
1204         struct dst_entry *dst;
1205         struct rt6_info *rt;
1206         struct inet6_dev *idev = in6_dev_get(dev);
1207         struct net *net = dev_net(dev);
1208
1209         if (unlikely(!idev))
1210                 return ERR_PTR(-ENODEV);
1211
1212         rt = ip6_dst_alloc(net, dev, 0, NULL);
1213         if (unlikely(!rt)) {
1214                 in6_dev_put(idev);
1215                 dst = ERR_PTR(-ENOMEM);
1216                 goto out;
1217         }
1218
1219         rt->dst.flags |= DST_HOST;
1220         rt->dst.output  = ip6_output;
1221         atomic_set(&rt->dst.__refcnt, 1);
1222         rt->rt6i_dst.addr = fl6->daddr;
1223         rt->rt6i_dst.plen = 128;
1224         rt->rt6i_idev     = idev;
1225         dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
1226
1227         spin_lock_bh(&icmp6_dst_lock);
1228         rt->dst.next = icmp6_dst_gc_list;
1229         icmp6_dst_gc_list = &rt->dst;
1230         spin_unlock_bh(&icmp6_dst_lock);
1231
1232         fib6_force_start_gc(net);
1233
1234         dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
1235
1236 out:
1237         return dst;
1238 }
1239
1240 int icmp6_dst_gc(void)
1241 {
1242         struct dst_entry *dst, **pprev;
1243         int more = 0;
1244
1245         spin_lock_bh(&icmp6_dst_lock);
1246         pprev = &icmp6_dst_gc_list;
1247
1248         while ((dst = *pprev) != NULL) {
1249                 if (!atomic_read(&dst->__refcnt)) {
1250                         *pprev = dst->next;
1251                         dst_free(dst);
1252                 } else {
1253                         pprev = &dst->next;
1254                         ++more;
1255                 }
1256         }
1257
1258         spin_unlock_bh(&icmp6_dst_lock);
1259
1260         return more;
1261 }
1262
1263 static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1264                             void *arg)
1265 {
1266         struct dst_entry *dst, **pprev;
1267
1268         spin_lock_bh(&icmp6_dst_lock);
1269         pprev = &icmp6_dst_gc_list;
1270         while ((dst = *pprev) != NULL) {
1271                 struct rt6_info *rt = (struct rt6_info *) dst;
1272                 if (func(rt, arg)) {
1273                         *pprev = dst->next;
1274                         dst_free(dst);
1275                 } else {
1276                         pprev = &dst->next;
1277                 }
1278         }
1279         spin_unlock_bh(&icmp6_dst_lock);
1280 }
1281
1282 static int ip6_dst_gc(struct dst_ops *ops)
1283 {
1284         unsigned long now = jiffies;
1285         struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
1286         int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1287         int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1288         int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1289         int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1290         unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1291         int entries;
1292
1293         entries = dst_entries_get_fast(ops);
1294         if (time_after(rt_last_gc + rt_min_interval, now) &&
1295             entries <= rt_max_size)
1296                 goto out;
1297
1298         net->ipv6.ip6_rt_gc_expire++;
1299         fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1300         net->ipv6.ip6_rt_last_gc = now;
1301         entries = dst_entries_get_slow(ops);
1302         if (entries < ops->gc_thresh)
1303                 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
1304 out:
1305         net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1306         return entries > rt_max_size;
1307 }
1308
1309 int ip6_dst_hoplimit(struct dst_entry *dst)
1310 {
1311         int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1312         if (hoplimit == 0) {
1313                 struct net_device *dev = dst->dev;
1314                 struct inet6_dev *idev;
1315
1316                 rcu_read_lock();
1317                 idev = __in6_dev_get(dev);
1318                 if (idev)
1319                         hoplimit = idev->cnf.hop_limit;
1320                 else
1321                         hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1322                 rcu_read_unlock();
1323         }
1324         return hoplimit;
1325 }
1326 EXPORT_SYMBOL(ip6_dst_hoplimit);
1327
1328 /*
1329  *
1330  */
1331
1332 int ip6_route_add(struct fib6_config *cfg)
1333 {
1334         int err;
1335         struct net *net = cfg->fc_nlinfo.nl_net;
1336         struct rt6_info *rt = NULL;
1337         struct net_device *dev = NULL;
1338         struct inet6_dev *idev = NULL;
1339         struct fib6_table *table;
1340         int addr_type;
1341
1342         if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
1343                 return -EINVAL;
1344 #ifndef CONFIG_IPV6_SUBTREES
1345         if (cfg->fc_src_len)
1346                 return -EINVAL;
1347 #endif
1348         if (cfg->fc_ifindex) {
1349                 err = -ENODEV;
1350                 dev = dev_get_by_index(net, cfg->fc_ifindex);
1351                 if (!dev)
1352                         goto out;
1353                 idev = in6_dev_get(dev);
1354                 if (!idev)
1355                         goto out;
1356         }
1357
1358         if (cfg->fc_metric == 0)
1359                 cfg->fc_metric = IP6_RT_PRIO_USER;
1360
1361         err = -ENOBUFS;
1362         if (cfg->fc_nlinfo.nlh &&
1363             !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1364                 table = fib6_get_table(net, cfg->fc_table);
1365                 if (!table) {
1366                         pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1367                         table = fib6_new_table(net, cfg->fc_table);
1368                 }
1369         } else {
1370                 table = fib6_new_table(net, cfg->fc_table);
1371         }
1372
1373         if (!table)
1374                 goto out;
1375
1376         rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table);
1377
1378         if (!rt) {
1379                 err = -ENOMEM;
1380                 goto out;
1381         }
1382
1383         if (cfg->fc_flags & RTF_EXPIRES)
1384                 rt6_set_expires(rt, jiffies +
1385                                 clock_t_to_jiffies(cfg->fc_expires));
1386         else
1387                 rt6_clean_expires(rt);
1388
1389         if (cfg->fc_protocol == RTPROT_UNSPEC)
1390                 cfg->fc_protocol = RTPROT_BOOT;
1391         rt->rt6i_protocol = cfg->fc_protocol;
1392
1393         addr_type = ipv6_addr_type(&cfg->fc_dst);
1394
1395         if (addr_type & IPV6_ADDR_MULTICAST)
1396                 rt->dst.input = ip6_mc_input;
1397         else if (cfg->fc_flags & RTF_LOCAL)
1398                 rt->dst.input = ip6_input;
1399         else
1400                 rt->dst.input = ip6_forward;
1401
1402         rt->dst.output = ip6_output;
1403
1404         ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1405         rt->rt6i_dst.plen = cfg->fc_dst_len;
1406         if (rt->rt6i_dst.plen == 128)
1407                rt->dst.flags |= DST_HOST;
1408
1409         if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
1410                 u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
1411                 if (!metrics) {
1412                         err = -ENOMEM;
1413                         goto out;
1414                 }
1415                 dst_init_metrics(&rt->dst, metrics, 0);
1416         }
1417 #ifdef CONFIG_IPV6_SUBTREES
1418         ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1419         rt->rt6i_src.plen = cfg->fc_src_len;
1420 #endif
1421
1422         rt->rt6i_metric = cfg->fc_metric;
1423
1424         /* We cannot add true routes via loopback here,
1425            they would result in kernel looping; promote them to reject routes
1426          */
1427         if ((cfg->fc_flags & RTF_REJECT) ||
1428             (dev && (dev->flags & IFF_LOOPBACK) &&
1429              !(addr_type & IPV6_ADDR_LOOPBACK) &&
1430              !(cfg->fc_flags & RTF_LOCAL))) {
1431                 /* hold loopback dev/idev if we haven't done so. */
1432                 if (dev != net->loopback_dev) {
1433                         if (dev) {
1434                                 dev_put(dev);
1435                                 in6_dev_put(idev);
1436                         }
1437                         dev = net->loopback_dev;
1438                         dev_hold(dev);
1439                         idev = in6_dev_get(dev);
1440                         if (!idev) {
1441                                 err = -ENODEV;
1442                                 goto out;
1443                         }
1444                 }
1445                 rt->dst.output = ip6_pkt_discard_out;
1446                 rt->dst.input = ip6_pkt_discard;
1447                 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1448                 switch (cfg->fc_type) {
1449                 case RTN_BLACKHOLE:
1450                         rt->dst.error = -EINVAL;
1451                         break;
1452                 case RTN_PROHIBIT:
1453                         rt->dst.error = -EACCES;
1454                         break;
1455                 case RTN_THROW:
1456                         rt->dst.error = -EAGAIN;
1457                         break;
1458                 default:
1459                         rt->dst.error = -ENETUNREACH;
1460                         break;
1461                 }
1462                 goto install_route;
1463         }
1464
1465         if (cfg->fc_flags & RTF_GATEWAY) {
1466                 const struct in6_addr *gw_addr;
1467                 int gwa_type;
1468
1469                 gw_addr = &cfg->fc_gateway;
1470                 rt->rt6i_gateway = *gw_addr;
1471                 gwa_type = ipv6_addr_type(gw_addr);
1472
1473                 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1474                         struct rt6_info *grt;
1475
1476                         /* IPv6 strictly inhibits using not link-local
1477                            addresses as nexthop address.
1478                            Otherwise, router will not able to send redirects.
1479                            It is very good, but in some (rare!) circumstances
1480                            (SIT, PtP, NBMA NOARP links) it is handy to allow
1481                            some exceptions. --ANK
1482                          */
1483                         err = -EINVAL;
1484                         if (!(gwa_type & IPV6_ADDR_UNICAST))
1485                                 goto out;
1486
1487                         grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
1488
1489                         err = -EHOSTUNREACH;
1490                         if (!grt)
1491                                 goto out;
1492                         if (dev) {
1493                                 if (dev != grt->dst.dev) {
1494                                         ip6_rt_put(grt);
1495                                         goto out;
1496                                 }
1497                         } else {
1498                                 dev = grt->dst.dev;
1499                                 idev = grt->rt6i_idev;
1500                                 dev_hold(dev);
1501                                 in6_dev_hold(grt->rt6i_idev);
1502                         }
1503                         if (!(grt->rt6i_flags & RTF_GATEWAY))
1504                                 err = 0;
1505                         ip6_rt_put(grt);
1506
1507                         if (err)
1508                                 goto out;
1509                 }
1510                 err = -EINVAL;
1511                 if (!dev || (dev->flags & IFF_LOOPBACK))
1512                         goto out;
1513         }
1514
1515         err = -ENODEV;
1516         if (!dev)
1517                 goto out;
1518
1519         if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1520                 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1521                         err = -EINVAL;
1522                         goto out;
1523                 }
1524                 rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1525                 rt->rt6i_prefsrc.plen = 128;
1526         } else
1527                 rt->rt6i_prefsrc.plen = 0;
1528
1529         rt->rt6i_flags = cfg->fc_flags;
1530
1531 install_route:
1532         if (cfg->fc_mx) {
1533                 struct nlattr *nla;
1534                 int remaining;
1535
1536                 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1537                         int type = nla_type(nla);
1538
1539                         if (type) {
1540                                 if (type > RTAX_MAX) {
1541                                         err = -EINVAL;
1542                                         goto out;
1543                                 }
1544
1545                                 dst_metric_set(&rt->dst, type, nla_get_u32(nla));
1546                         }
1547                 }
1548         }
1549
1550         rt->dst.dev = dev;
1551         rt->rt6i_idev = idev;
1552         rt->rt6i_table = table;
1553
1554         cfg->fc_nlinfo.nl_net = dev_net(dev);
1555
1556         return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
1557
1558 out:
1559         if (dev)
1560                 dev_put(dev);
1561         if (idev)
1562                 in6_dev_put(idev);
1563         if (rt)
1564                 dst_free(&rt->dst);
1565         return err;
1566 }
1567
1568 static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
1569 {
1570         int err;
1571         struct fib6_table *table;
1572         struct net *net = dev_net(rt->dst.dev);
1573
1574         if (rt == net->ipv6.ip6_null_entry) {
1575                 err = -ENOENT;
1576                 goto out;
1577         }
1578
1579         table = rt->rt6i_table;
1580         write_lock_bh(&table->tb6_lock);
1581         err = fib6_del(rt, info);
1582         write_unlock_bh(&table->tb6_lock);
1583
1584 out:
1585         ip6_rt_put(rt);
1586         return err;
1587 }
1588
1589 int ip6_del_rt(struct rt6_info *rt)
1590 {
1591         struct nl_info info = {
1592                 .nl_net = dev_net(rt->dst.dev),
1593         };
1594         return __ip6_del_rt(rt, &info);
1595 }
1596
1597 static int ip6_route_del(struct fib6_config *cfg)
1598 {
1599         struct fib6_table *table;
1600         struct fib6_node *fn;
1601         struct rt6_info *rt;
1602         int err = -ESRCH;
1603
1604         table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
1605         if (!table)
1606                 return err;
1607
1608         read_lock_bh(&table->tb6_lock);
1609
1610         fn = fib6_locate(&table->tb6_root,
1611                          &cfg->fc_dst, cfg->fc_dst_len,
1612                          &cfg->fc_src, cfg->fc_src_len);
1613
1614         if (fn) {
1615                 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1616                         if (cfg->fc_ifindex &&
1617                             (!rt->dst.dev ||
1618                              rt->dst.dev->ifindex != cfg->fc_ifindex))
1619                                 continue;
1620                         if (cfg->fc_flags & RTF_GATEWAY &&
1621                             !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
1622                                 continue;
1623                         if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
1624                                 continue;
1625                         dst_hold(&rt->dst);
1626                         read_unlock_bh(&table->tb6_lock);
1627
1628                         return __ip6_del_rt(rt, &cfg->fc_nlinfo);
1629                 }
1630         }
1631         read_unlock_bh(&table->tb6_lock);
1632
1633         return err;
1634 }
1635
1636 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
1637 {
1638         struct net *net = dev_net(skb->dev);
1639         struct netevent_redirect netevent;
1640         struct rt6_info *rt, *nrt = NULL;
1641         struct ndisc_options ndopts;
1642         struct inet6_dev *in6_dev;
1643         struct neighbour *neigh;
1644         struct rd_msg *msg;
1645         int optlen, on_link;
1646         u8 *lladdr;
1647
1648         optlen = skb->tail - skb->transport_header;
1649         optlen -= sizeof(*msg);
1650
1651         if (optlen < 0) {
1652                 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
1653                 return;
1654         }
1655
1656         msg = (struct rd_msg *)icmp6_hdr(skb);
1657
1658         if (ipv6_addr_is_multicast(&msg->dest)) {
1659                 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
1660                 return;
1661         }
1662
1663         on_link = 0;
1664         if (ipv6_addr_equal(&msg->dest, &msg->target)) {
1665                 on_link = 1;
1666         } else if (ipv6_addr_type(&msg->target) !=
1667                    (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
1668                 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
1669                 return;
1670         }
1671
1672         in6_dev = __in6_dev_get(skb->dev);
1673         if (!in6_dev)
1674                 return;
1675         if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1676                 return;
1677
1678         /* RFC2461 8.1:
1679          *      The IP source address of the Redirect MUST be the same as the current
1680          *      first-hop router for the specified ICMP Destination Address.
1681          */
1682
1683         if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
1684                 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1685                 return;
1686         }
1687
1688         lladdr = NULL;
1689         if (ndopts.nd_opts_tgt_lladdr) {
1690                 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1691                                              skb->dev);
1692                 if (!lladdr) {
1693                         net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1694                         return;
1695                 }
1696         }
1697
1698         rt = (struct rt6_info *) dst;
1699         if (rt == net->ipv6.ip6_null_entry) {
1700                 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
1701                 return;
1702         }
1703
1704         /* Redirect received -> path was valid.
1705          * Look, redirects are sent only in response to data packets,
1706          * so that this nexthop apparently is reachable. --ANK
1707          */
1708         dst_confirm(&rt->dst);
1709
1710         neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
1711         if (!neigh)
1712                 return;
1713
1714         /*
1715          *      We have finally decided to accept it.
1716          */
1717
1718         neigh_update(neigh, lladdr, NUD_STALE,
1719                      NEIGH_UPDATE_F_WEAK_OVERRIDE|
1720                      NEIGH_UPDATE_F_OVERRIDE|
1721                      (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1722                                      NEIGH_UPDATE_F_ISROUTER))
1723                      );
1724
1725         nrt = ip6_rt_copy(rt, &msg->dest);
1726         if (!nrt)
1727                 goto out;
1728
1729         nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1730         if (on_link)
1731                 nrt->rt6i_flags &= ~RTF_GATEWAY;
1732
1733         nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
1734
1735         if (ip6_ins_rt(nrt))
1736                 goto out;
1737
1738         netevent.old = &rt->dst;
1739         netevent.new = &nrt->dst;
1740         netevent.daddr = &msg->dest;
1741         netevent.neigh = neigh;
1742         call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1743
1744         if (rt->rt6i_flags & RTF_CACHE) {
1745                 rt = (struct rt6_info *) dst_clone(&rt->dst);
1746                 ip6_del_rt(rt);
1747         }
1748
1749 out:
1750         neigh_release(neigh);
1751 }
1752
1753 /*
1754  *      Misc support functions
1755  */
1756
1757 static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
1758                                     const struct in6_addr *dest)
1759 {
1760         struct net *net = dev_net(ort->dst.dev);
1761         struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
1762                                             ort->rt6i_table);
1763
1764         if (rt) {
1765                 rt->dst.input = ort->dst.input;
1766                 rt->dst.output = ort->dst.output;
1767                 rt->dst.flags |= DST_HOST;
1768
1769                 rt->rt6i_dst.addr = *dest;
1770                 rt->rt6i_dst.plen = 128;
1771                 dst_copy_metrics(&rt->dst, &ort->dst);
1772                 rt->dst.error = ort->dst.error;
1773                 rt->rt6i_idev = ort->rt6i_idev;
1774                 if (rt->rt6i_idev)
1775                         in6_dev_hold(rt->rt6i_idev);
1776                 rt->dst.lastuse = jiffies;
1777
1778                 rt->rt6i_gateway = ort->rt6i_gateway;
1779                 rt->rt6i_flags = ort->rt6i_flags;
1780                 if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ==
1781                     (RTF_DEFAULT | RTF_ADDRCONF))
1782                         rt6_set_from(rt, ort);
1783                 rt->rt6i_metric = 0;
1784
1785 #ifdef CONFIG_IPV6_SUBTREES
1786                 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1787 #endif
1788                 memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1789                 rt->rt6i_table = ort->rt6i_table;
1790         }
1791         return rt;
1792 }
1793
1794 #ifdef CONFIG_IPV6_ROUTE_INFO
1795 static struct rt6_info *rt6_get_route_info(struct net_device *dev,
1796                                            const struct in6_addr *prefix, int prefixlen,
1797                                            const struct in6_addr *gwaddr)
1798 {
1799         struct fib6_node *fn;
1800         struct rt6_info *rt = NULL;
1801         struct fib6_table *table;
1802
1803         table = fib6_get_table(dev_net(dev),
1804                                addrconf_rt_table(dev, RT6_TABLE_INFO));
1805         if (!table)
1806                 return NULL;
1807
1808         read_lock_bh(&table->tb6_lock);
1809         fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
1810         if (!fn)
1811                 goto out;
1812
1813         for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1814                 if (rt->dst.dev->ifindex != dev->ifindex)
1815                         continue;
1816                 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1817                         continue;
1818                 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1819                         continue;
1820                 dst_hold(&rt->dst);
1821                 break;
1822         }
1823 out:
1824         read_unlock_bh(&table->tb6_lock);
1825         return rt;
1826 }
1827
1828 static struct rt6_info *rt6_add_route_info(struct net_device *dev,
1829                                            const struct in6_addr *prefix, int prefixlen,
1830                                            const struct in6_addr *gwaddr, unsigned int pref)
1831 {
1832         struct fib6_config cfg = {
1833                 .fc_table       = addrconf_rt_table(dev, RT6_TABLE_INFO),
1834                 .fc_metric      = IP6_RT_PRIO_USER,
1835                 .fc_ifindex     = dev->ifindex,
1836                 .fc_dst_len     = prefixlen,
1837                 .fc_flags       = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1838                                   RTF_UP | RTF_PREF(pref),
1839                 .fc_nlinfo.portid = 0,
1840                 .fc_nlinfo.nlh = NULL,
1841                 .fc_nlinfo.nl_net = dev_net(dev),
1842         };
1843
1844         cfg.fc_dst = *prefix;
1845         cfg.fc_gateway = *gwaddr;
1846
1847         /* We should treat it as a default route if prefix length is 0. */
1848         if (!prefixlen)
1849                 cfg.fc_flags |= RTF_DEFAULT;
1850
1851         ip6_route_add(&cfg);
1852
1853         return rt6_get_route_info(dev, prefix, prefixlen, gwaddr);
1854 }
1855 #endif
1856
1857 struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
1858 {
1859         struct rt6_info *rt;
1860         struct fib6_table *table;
1861
1862         table = fib6_get_table(dev_net(dev),
1863                                addrconf_rt_table(dev, RT6_TABLE_MAIN));
1864         if (!table)
1865                 return NULL;
1866
1867         read_lock_bh(&table->tb6_lock);
1868         for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
1869                 if (dev == rt->dst.dev &&
1870                     ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
1871                     ipv6_addr_equal(&rt->rt6i_gateway, addr))
1872                         break;
1873         }
1874         if (rt)
1875                 dst_hold(&rt->dst);
1876         read_unlock_bh(&table->tb6_lock);
1877         return rt;
1878 }
1879
1880 struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1881                                      struct net_device *dev,
1882                                      unsigned int pref)
1883 {
1884         struct fib6_config cfg = {
1885                 .fc_table       = addrconf_rt_table(dev, RT6_TABLE_DFLT),
1886                 .fc_metric      = IP6_RT_PRIO_USER,
1887                 .fc_ifindex     = dev->ifindex,
1888                 .fc_flags       = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
1889                                   RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
1890                 .fc_nlinfo.portid = 0,
1891                 .fc_nlinfo.nlh = NULL,
1892                 .fc_nlinfo.nl_net = dev_net(dev),
1893         };
1894
1895         cfg.fc_gateway = *gwaddr;
1896
1897         ip6_route_add(&cfg);
1898
1899         return rt6_get_dflt_router(gwaddr, dev);
1900 }
1901
1902
1903 int rt6_addrconf_purge(struct rt6_info *rt, void *arg) {
1904         if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
1905             (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2))
1906                 return -1;
1907         return 0;
1908 }
1909
1910 void rt6_purge_dflt_routers(struct net *net)
1911 {
1912         fib6_clean_all(net, rt6_addrconf_purge, 0, NULL);
1913 }
1914
1915 static void rtmsg_to_fib6_config(struct net *net,
1916                                  struct in6_rtmsg *rtmsg,
1917                                  struct fib6_config *cfg)
1918 {
1919         memset(cfg, 0, sizeof(*cfg));
1920
1921         cfg->fc_table = RT6_TABLE_MAIN;
1922         cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
1923         cfg->fc_metric = rtmsg->rtmsg_metric;
1924         cfg->fc_expires = rtmsg->rtmsg_info;
1925         cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
1926         cfg->fc_src_len = rtmsg->rtmsg_src_len;
1927         cfg->fc_flags = rtmsg->rtmsg_flags;
1928
1929         cfg->fc_nlinfo.nl_net = net;
1930
1931         cfg->fc_dst = rtmsg->rtmsg_dst;
1932         cfg->fc_src = rtmsg->rtmsg_src;
1933         cfg->fc_gateway = rtmsg->rtmsg_gateway;
1934 }
1935
1936 int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
1937 {
1938         struct fib6_config cfg;
1939         struct in6_rtmsg rtmsg;
1940         int err;
1941
1942         switch(cmd) {
1943         case SIOCADDRT:         /* Add a route */
1944         case SIOCDELRT:         /* Delete a route */
1945                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1946                         return -EPERM;
1947                 err = copy_from_user(&rtmsg, arg,
1948                                      sizeof(struct in6_rtmsg));
1949                 if (err)
1950                         return -EFAULT;
1951
1952                 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
1953
1954                 rtnl_lock();
1955                 switch (cmd) {
1956                 case SIOCADDRT:
1957                         err = ip6_route_add(&cfg);
1958                         break;
1959                 case SIOCDELRT:
1960                         err = ip6_route_del(&cfg);
1961                         break;
1962                 default:
1963                         err = -EINVAL;
1964                 }
1965                 rtnl_unlock();
1966
1967                 return err;
1968         }
1969
1970         return -EINVAL;
1971 }
1972
1973 /*
1974  *      Drop the packet on the floor
1975  */
1976
1977 static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
1978 {
1979         int type;
1980         struct dst_entry *dst = skb_dst(skb);
1981         switch (ipstats_mib_noroutes) {
1982         case IPSTATS_MIB_INNOROUTES:
1983                 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
1984                 if (type == IPV6_ADDR_ANY) {
1985                         IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1986                                       IPSTATS_MIB_INADDRERRORS);
1987                         break;
1988                 }
1989                 /* FALLTHROUGH */
1990         case IPSTATS_MIB_OUTNOROUTES:
1991                 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1992                               ipstats_mib_noroutes);
1993                 break;
1994         }
1995         icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
1996         kfree_skb(skb);
1997         return 0;
1998 }
1999
2000 static int ip6_pkt_discard(struct sk_buff *skb)
2001 {
2002         return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
2003 }
2004
2005 static int ip6_pkt_discard_out(struct sk_buff *skb)
2006 {
2007         skb->dev = skb_dst(skb)->dev;
2008         return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
2009 }
2010
2011 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2012
2013 static int ip6_pkt_prohibit(struct sk_buff *skb)
2014 {
2015         return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
2016 }
2017
2018 static int ip6_pkt_prohibit_out(struct sk_buff *skb)
2019 {
2020         skb->dev = skb_dst(skb)->dev;
2021         return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
2022 }
2023
2024 #endif
2025
2026 /*
2027  *      Allocate a dst for local (unicast / anycast) address.
2028  */
2029
2030 struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2031                                     const struct in6_addr *addr,
2032                                     bool anycast)
2033 {
2034         struct net *net = dev_net(idev->dev);
2035         struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL);
2036
2037         if (!rt) {
2038                 net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n");
2039                 return ERR_PTR(-ENOMEM);
2040         }
2041
2042         in6_dev_hold(idev);
2043
2044         rt->dst.flags |= DST_HOST;
2045         rt->dst.input = ip6_input;
2046         rt->dst.output = ip6_output;
2047         rt->rt6i_idev = idev;
2048
2049         rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
2050         if (anycast)
2051                 rt->rt6i_flags |= RTF_ANYCAST;
2052         else
2053                 rt->rt6i_flags |= RTF_LOCAL;
2054
2055         rt->rt6i_dst.addr = *addr;
2056         rt->rt6i_dst.plen = 128;
2057         rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
2058
2059         atomic_set(&rt->dst.__refcnt, 1);
2060
2061         return rt;
2062 }
2063
2064 int ip6_route_get_saddr(struct net *net,
2065                         struct rt6_info *rt,
2066                         const struct in6_addr *daddr,
2067                         unsigned int prefs,
2068                         struct in6_addr *saddr)
2069 {
2070         struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2071         int err = 0;
2072         if (rt->rt6i_prefsrc.plen)
2073                 *saddr = rt->rt6i_prefsrc.addr;
2074         else
2075                 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2076                                          daddr, prefs, saddr);
2077         return err;
2078 }
2079
2080 /* remove deleted ip from prefsrc entries */
2081 struct arg_dev_net_ip {
2082         struct net_device *dev;
2083         struct net *net;
2084         struct in6_addr *addr;
2085 };
2086
2087 static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2088 {
2089         struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2090         struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2091         struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2092
2093         if (((void *)rt->dst.dev == dev || !dev) &&
2094             rt != net->ipv6.ip6_null_entry &&
2095             ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2096                 /* remove prefsrc entry */
2097                 rt->rt6i_prefsrc.plen = 0;
2098         }
2099         return 0;
2100 }
2101
2102 void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2103 {
2104         struct net *net = dev_net(ifp->idev->dev);
2105         struct arg_dev_net_ip adni = {
2106                 .dev = ifp->idev->dev,
2107                 .net = net,
2108                 .addr = &ifp->addr,
2109         };
2110         fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2111 }
2112
2113 struct arg_dev_net {
2114         struct net_device *dev;
2115         struct net *net;
2116 };
2117
2118 static int fib6_ifdown(struct rt6_info *rt, void *arg)
2119 {
2120         const struct arg_dev_net *adn = arg;
2121         const struct net_device *dev = adn->dev;
2122
2123         if ((rt->dst.dev == dev || !dev) &&
2124             rt != adn->net->ipv6.ip6_null_entry)
2125                 return -1;
2126
2127         return 0;
2128 }
2129
2130 void rt6_ifdown(struct net *net, struct net_device *dev)
2131 {
2132         struct arg_dev_net adn = {
2133                 .dev = dev,
2134                 .net = net,
2135         };
2136
2137         fib6_clean_all(net, fib6_ifdown, 0, &adn);
2138         icmp6_clean_all(fib6_ifdown, &adn);
2139 }
2140
2141 struct rt6_mtu_change_arg {
2142         struct net_device *dev;
2143         unsigned int mtu;
2144 };
2145
2146 static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2147 {
2148         struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2149         struct inet6_dev *idev;
2150
2151         /* In IPv6 pmtu discovery is not optional,
2152            so that RTAX_MTU lock cannot disable it.
2153            We still use this lock to block changes
2154            caused by addrconf/ndisc.
2155         */
2156
2157         idev = __in6_dev_get(arg->dev);
2158         if (!idev)
2159                 return 0;
2160
2161         /* For administrative MTU increase, there is no way to discover
2162            IPv6 PMTU increase, so PMTU increase should be updated here.
2163            Since RFC 1981 doesn't include administrative MTU increase
2164            update PMTU increase is a MUST. (i.e. jumbo frame)
2165          */
2166         /*
2167            If new MTU is less than route PMTU, this new MTU will be the
2168            lowest MTU in the path, update the route PMTU to reflect PMTU
2169            decreases; if new MTU is greater than route PMTU, and the
2170            old MTU is the lowest MTU in the path, update the route PMTU
2171            to reflect the increase. In this case if the other nodes' MTU
2172            also have the lowest MTU, TOO BIG MESSAGE will be lead to
2173            PMTU discouvery.
2174          */
2175         if (rt->dst.dev == arg->dev &&
2176             !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2177             (dst_mtu(&rt->dst) >= arg->mtu ||
2178              (dst_mtu(&rt->dst) < arg->mtu &&
2179               dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2180                 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2181         }
2182         return 0;
2183 }
2184
2185 void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
2186 {
2187         struct rt6_mtu_change_arg arg = {
2188                 .dev = dev,
2189                 .mtu = mtu,
2190         };
2191
2192         fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
2193 }
2194
2195 static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
2196         [RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
2197         [RTA_OIF]               = { .type = NLA_U32 },
2198         [RTA_IIF]               = { .type = NLA_U32 },
2199         [RTA_PRIORITY]          = { .type = NLA_U32 },
2200         [RTA_METRICS]           = { .type = NLA_NESTED },
2201         [RTA_MULTIPATH]         = { .len = sizeof(struct rtnexthop) },
2202 };
2203
2204 static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2205                               struct fib6_config *cfg)
2206 {
2207         struct rtmsg *rtm;
2208         struct nlattr *tb[RTA_MAX+1];
2209         int err;
2210
2211         err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2212         if (err < 0)
2213                 goto errout;
2214
2215         err = -EINVAL;
2216         rtm = nlmsg_data(nlh);
2217         memset(cfg, 0, sizeof(*cfg));
2218
2219         cfg->fc_table = rtm->rtm_table;
2220         cfg->fc_dst_len = rtm->rtm_dst_len;
2221         cfg->fc_src_len = rtm->rtm_src_len;
2222         cfg->fc_flags = RTF_UP;
2223         cfg->fc_protocol = rtm->rtm_protocol;
2224         cfg->fc_type = rtm->rtm_type;
2225
2226         if (rtm->rtm_type == RTN_UNREACHABLE ||
2227             rtm->rtm_type == RTN_BLACKHOLE ||
2228             rtm->rtm_type == RTN_PROHIBIT ||
2229             rtm->rtm_type == RTN_THROW)
2230                 cfg->fc_flags |= RTF_REJECT;
2231
2232         if (rtm->rtm_type == RTN_LOCAL)
2233                 cfg->fc_flags |= RTF_LOCAL;
2234
2235         cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
2236         cfg->fc_nlinfo.nlh = nlh;
2237         cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
2238
2239         if (tb[RTA_GATEWAY]) {
2240                 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2241                 cfg->fc_flags |= RTF_GATEWAY;
2242         }
2243
2244         if (tb[RTA_DST]) {
2245                 int plen = (rtm->rtm_dst_len + 7) >> 3;
2246
2247                 if (nla_len(tb[RTA_DST]) < plen)
2248                         goto errout;
2249
2250                 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
2251         }
2252
2253         if (tb[RTA_SRC]) {
2254                 int plen = (rtm->rtm_src_len + 7) >> 3;
2255
2256                 if (nla_len(tb[RTA_SRC]) < plen)
2257                         goto errout;
2258
2259                 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
2260         }
2261
2262         if (tb[RTA_PREFSRC])
2263                 nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2264
2265         if (tb[RTA_OIF])
2266                 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2267
2268         if (tb[RTA_PRIORITY])
2269                 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2270
2271         if (tb[RTA_METRICS]) {
2272                 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2273                 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
2274         }
2275
2276         if (tb[RTA_TABLE])
2277                 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2278
2279         if (tb[RTA_MULTIPATH]) {
2280                 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
2281                 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
2282         }
2283
2284         err = 0;
2285 errout:
2286         return err;
2287 }
2288
2289 static int ip6_route_multipath(struct fib6_config *cfg, int add)
2290 {
2291         struct fib6_config r_cfg;
2292         struct rtnexthop *rtnh;
2293         int remaining;
2294         int attrlen;
2295         int err = 0, last_err = 0;
2296
2297 beginning:
2298         rtnh = (struct rtnexthop *)cfg->fc_mp;
2299         remaining = cfg->fc_mp_len;
2300
2301         /* Parse a Multipath Entry */
2302         while (rtnh_ok(rtnh, remaining)) {
2303                 memcpy(&r_cfg, cfg, sizeof(*cfg));
2304                 if (rtnh->rtnh_ifindex)
2305                         r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
2306
2307                 attrlen = rtnh_attrlen(rtnh);
2308                 if (attrlen > 0) {
2309                         struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
2310
2311                         nla = nla_find(attrs, attrlen, RTA_GATEWAY);
2312                         if (nla) {
2313                                 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
2314                                 r_cfg.fc_flags |= RTF_GATEWAY;
2315                         }
2316                 }
2317                 err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
2318                 if (err) {
2319                         last_err = err;
2320                         /* If we are trying to remove a route, do not stop the
2321                          * loop when ip6_route_del() fails (because next hop is
2322                          * already gone), we should try to remove all next hops.
2323                          */
2324                         if (add) {
2325                                 /* If add fails, we should try to delete all
2326                                  * next hops that have been already added.
2327                                  */
2328                                 add = 0;
2329                                 goto beginning;
2330                         }
2331                 }
2332                 /* Because each route is added like a single route we remove
2333                  * this flag after the first nexthop (if there is a collision,
2334                  * we have already fail to add the first nexthop:
2335                  * fib6_add_rt2node() has reject it).
2336                  */
2337                 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL;
2338                 rtnh = rtnh_next(rtnh, &remaining);
2339         }
2340
2341         return last_err;
2342 }
2343
2344 static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh)
2345 {
2346         struct fib6_config cfg;
2347         int err;
2348
2349         err = rtm_to_fib6_config(skb, nlh, &cfg);
2350         if (err < 0)
2351                 return err;
2352
2353         if (cfg.fc_mp)
2354                 return ip6_route_multipath(&cfg, 0);
2355         else
2356                 return ip6_route_del(&cfg);
2357 }
2358
2359 static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh)
2360 {
2361         struct fib6_config cfg;
2362         int err;
2363
2364         err = rtm_to_fib6_config(skb, nlh, &cfg);
2365         if (err < 0)
2366                 return err;
2367
2368         if (cfg.fc_mp)
2369                 return ip6_route_multipath(&cfg, 1);
2370         else
2371                 return ip6_route_add(&cfg);
2372 }
2373
2374 static inline size_t rt6_nlmsg_size(void)
2375 {
2376         return NLMSG_ALIGN(sizeof(struct rtmsg))
2377                + nla_total_size(16) /* RTA_SRC */
2378                + nla_total_size(16) /* RTA_DST */
2379                + nla_total_size(16) /* RTA_GATEWAY */
2380                + nla_total_size(16) /* RTA_PREFSRC */
2381                + nla_total_size(4) /* RTA_TABLE */
2382                + nla_total_size(4) /* RTA_IIF */
2383                + nla_total_size(4) /* RTA_OIF */
2384                + nla_total_size(4) /* RTA_PRIORITY */
2385                + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2386                + nla_total_size(sizeof(struct rta_cacheinfo));
2387 }
2388
2389 static int rt6_fill_node(struct net *net,
2390                          struct sk_buff *skb, struct rt6_info *rt,
2391                          struct in6_addr *dst, struct in6_addr *src,
2392                          int iif, int type, u32 portid, u32 seq,
2393                          int prefix, int nowait, unsigned int flags)
2394 {
2395         struct rtmsg *rtm;
2396         struct nlmsghdr *nlh;
2397         long expires;
2398         u32 table;
2399
2400         if (prefix) {   /* user wants prefix routes only */
2401                 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2402                         /* success since this is not a prefix route */
2403                         return 1;
2404                 }
2405         }
2406
2407         nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
2408         if (!nlh)
2409                 return -EMSGSIZE;
2410
2411         rtm = nlmsg_data(nlh);
2412         rtm->rtm_family = AF_INET6;
2413         rtm->rtm_dst_len = rt->rt6i_dst.plen;
2414         rtm->rtm_src_len = rt->rt6i_src.plen;
2415         rtm->rtm_tos = 0;
2416         if (rt->rt6i_table)
2417                 table = rt->rt6i_table->tb6_id;
2418         else
2419                 table = RT6_TABLE_UNSPEC;
2420         rtm->rtm_table = table;
2421         if (nla_put_u32(skb, RTA_TABLE, table))
2422                 goto nla_put_failure;
2423         if (rt->rt6i_flags & RTF_REJECT) {
2424                 switch (rt->dst.error) {
2425                 case -EINVAL:
2426                         rtm->rtm_type = RTN_BLACKHOLE;
2427                         break;
2428                 case -EACCES:
2429                         rtm->rtm_type = RTN_PROHIBIT;
2430                         break;
2431                 case -EAGAIN:
2432                         rtm->rtm_type = RTN_THROW;
2433                         break;
2434                 default:
2435                         rtm->rtm_type = RTN_UNREACHABLE;
2436                         break;
2437                 }
2438         }
2439         else if (rt->rt6i_flags & RTF_LOCAL)
2440                 rtm->rtm_type = RTN_LOCAL;
2441         else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
2442                 rtm->rtm_type = RTN_LOCAL;
2443         else
2444                 rtm->rtm_type = RTN_UNICAST;
2445         rtm->rtm_flags = 0;
2446         rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2447         rtm->rtm_protocol = rt->rt6i_protocol;
2448         if (rt->rt6i_flags & RTF_DYNAMIC)
2449                 rtm->rtm_protocol = RTPROT_REDIRECT;
2450         else if (rt->rt6i_flags & RTF_ADDRCONF) {
2451                 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
2452                         rtm->rtm_protocol = RTPROT_RA;
2453                 else
2454                         rtm->rtm_protocol = RTPROT_KERNEL;
2455         }
2456
2457         if (rt->rt6i_flags & RTF_CACHE)
2458                 rtm->rtm_flags |= RTM_F_CLONED;
2459
2460         if (dst) {
2461                 if (nla_put(skb, RTA_DST, 16, dst))
2462                         goto nla_put_failure;
2463                 rtm->rtm_dst_len = 128;
2464         } else if (rtm->rtm_dst_len)
2465                 if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2466                         goto nla_put_failure;
2467 #ifdef CONFIG_IPV6_SUBTREES
2468         if (src) {
2469                 if (nla_put(skb, RTA_SRC, 16, src))
2470                         goto nla_put_failure;
2471                 rtm->rtm_src_len = 128;
2472         } else if (rtm->rtm_src_len &&
2473                    nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2474                 goto nla_put_failure;
2475 #endif
2476         if (iif) {
2477 #ifdef CONFIG_IPV6_MROUTE
2478                 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
2479                         int err = ip6mr_get_route(net, skb, rtm, nowait);
2480                         if (err <= 0) {
2481                                 if (!nowait) {
2482                                         if (err == 0)
2483                                                 return 0;
2484                                         goto nla_put_failure;
2485                                 } else {
2486                                         if (err == -EMSGSIZE)
2487                                                 goto nla_put_failure;
2488                                 }
2489                         }
2490                 } else
2491 #endif
2492                         if (nla_put_u32(skb, RTA_IIF, iif))
2493                                 goto nla_put_failure;
2494         } else if (dst) {
2495                 struct in6_addr saddr_buf;
2496                 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2497                     nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2498                         goto nla_put_failure;
2499         }
2500
2501         if (rt->rt6i_prefsrc.plen) {
2502                 struct in6_addr saddr_buf;
2503                 saddr_buf = rt->rt6i_prefsrc.addr;
2504                 if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2505                         goto nla_put_failure;
2506         }
2507
2508         if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
2509                 goto nla_put_failure;
2510
2511         if (rt->rt6i_flags & RTF_GATEWAY) {
2512                 if (nla_put(skb, RTA_GATEWAY, 16, &rt->rt6i_gateway) < 0)
2513                         goto nla_put_failure;
2514         }
2515
2516         if (rt->dst.dev &&
2517             nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2518                 goto nla_put_failure;
2519         if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2520                 goto nla_put_failure;
2521
2522         expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
2523
2524         if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
2525                 goto nla_put_failure;
2526
2527         return nlmsg_end(skb, nlh);
2528
2529 nla_put_failure:
2530         nlmsg_cancel(skb, nlh);
2531         return -EMSGSIZE;
2532 }
2533
2534 int rt6_dump_route(struct rt6_info *rt, void *p_arg)
2535 {
2536         struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2537         int prefix;
2538
2539         if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2540                 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
2541                 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2542         } else
2543                 prefix = 0;
2544
2545         return rt6_fill_node(arg->net,
2546                      arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
2547                      NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
2548                      prefix, 0, NLM_F_MULTI);
2549 }
2550
2551 static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh)
2552 {
2553         struct net *net = sock_net(in_skb->sk);
2554         struct nlattr *tb[RTA_MAX+1];
2555         struct rt6_info *rt;
2556         struct sk_buff *skb;
2557         struct rtmsg *rtm;
2558         struct flowi6 fl6;
2559         int err, iif = 0, oif = 0;
2560
2561         err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2562         if (err < 0)
2563                 goto errout;
2564
2565         err = -EINVAL;
2566         memset(&fl6, 0, sizeof(fl6));
2567
2568         if (tb[RTA_SRC]) {
2569                 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2570                         goto errout;
2571
2572                 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2573         }
2574
2575         if (tb[RTA_DST]) {
2576                 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2577                         goto errout;
2578
2579                 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2580         }
2581
2582         if (tb[RTA_IIF])
2583                 iif = nla_get_u32(tb[RTA_IIF]);
2584
2585         if (tb[RTA_OIF])
2586                 oif = nla_get_u32(tb[RTA_OIF]);
2587
2588         if (iif) {
2589                 struct net_device *dev;
2590                 int flags = 0;
2591
2592                 dev = __dev_get_by_index(net, iif);
2593                 if (!dev) {
2594                         err = -ENODEV;
2595                         goto errout;
2596                 }
2597
2598                 fl6.flowi6_iif = iif;
2599
2600                 if (!ipv6_addr_any(&fl6.saddr))
2601                         flags |= RT6_LOOKUP_F_HAS_SADDR;
2602
2603                 rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
2604                                                                flags);
2605         } else {
2606                 fl6.flowi6_oif = oif;
2607
2608                 rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2609         }
2610
2611         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2612         if (!skb) {
2613                 ip6_rt_put(rt);
2614                 err = -ENOBUFS;
2615                 goto errout;
2616         }
2617
2618         /* Reserve room for dummy headers, this skb can pass
2619            through good chunk of routing engine.
2620          */
2621         skb_reset_mac_header(skb);
2622         skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2623
2624         skb_dst_set(skb, &rt->dst);
2625
2626         err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
2627                             RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
2628                             nlh->nlmsg_seq, 0, 0, 0);
2629         if (err < 0) {
2630                 kfree_skb(skb);
2631                 goto errout;
2632         }
2633
2634         err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2635 errout:
2636         return err;
2637 }
2638
2639 void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
2640 {
2641         struct sk_buff *skb;
2642         struct net *net = info->nl_net;
2643         u32 seq;
2644         int err;
2645
2646         err = -ENOBUFS;
2647         seq = info->nlh ? info->nlh->nlmsg_seq : 0;
2648
2649         skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
2650         if (!skb)
2651                 goto errout;
2652
2653         err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
2654                                 event, info->portid, seq, 0, 0, 0);
2655         if (err < 0) {
2656                 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2657                 WARN_ON(err == -EMSGSIZE);
2658                 kfree_skb(skb);
2659                 goto errout;
2660         }
2661         rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
2662                     info->nlh, gfp_any());
2663         return;
2664 errout:
2665         if (err < 0)
2666                 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
2667 }
2668
2669 static int ip6_route_dev_notify(struct notifier_block *this,
2670                                 unsigned long event, void *data)
2671 {
2672         struct net_device *dev = (struct net_device *)data;
2673         struct net *net = dev_net(dev);
2674
2675         if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2676                 net->ipv6.ip6_null_entry->dst.dev = dev;
2677                 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2678 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2679                 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
2680                 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2681                 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
2682                 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2683 #endif
2684         }
2685
2686         return NOTIFY_OK;
2687 }
2688
2689 /*
2690  *      /proc
2691  */
2692
2693 #ifdef CONFIG_PROC_FS
2694
2695 struct rt6_proc_arg
2696 {
2697         char *buffer;
2698         int offset;
2699         int length;
2700         int skip;
2701         int len;
2702 };
2703
2704 static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2705 {
2706         struct seq_file *m = p_arg;
2707
2708         seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2709
2710 #ifdef CONFIG_IPV6_SUBTREES
2711         seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2712 #else
2713         seq_puts(m, "00000000000000000000000000000000 00 ");
2714 #endif
2715         if (rt->rt6i_flags & RTF_GATEWAY) {
2716                 seq_printf(m, "%pi6", &rt->rt6i_gateway);
2717         } else {
2718                 seq_puts(m, "00000000000000000000000000000000");
2719         }
2720         seq_printf(m, " %08x %08x %08x %08x %8s\n",
2721                    rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2722                    rt->dst.__use, rt->rt6i_flags,
2723                    rt->dst.dev ? rt->dst.dev->name : "");
2724         return 0;
2725 }
2726
2727 static int ipv6_route_show(struct seq_file *m, void *v)
2728 {
2729         struct net *net = (struct net *)m->private;
2730         fib6_clean_all_ro(net, rt6_info_route, 0, m);
2731         return 0;
2732 }
2733
2734 static int ipv6_route_open(struct inode *inode, struct file *file)
2735 {
2736         return single_open_net(inode, file, ipv6_route_show);
2737 }
2738
2739 static const struct file_operations ipv6_route_proc_fops = {
2740         .owner          = THIS_MODULE,
2741         .open           = ipv6_route_open,
2742         .read           = seq_read,
2743         .llseek         = seq_lseek,
2744         .release        = single_release_net,
2745 };
2746
2747 static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2748 {
2749         struct net *net = (struct net *)seq->private;
2750         seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
2751                    net->ipv6.rt6_stats->fib_nodes,
2752                    net->ipv6.rt6_stats->fib_route_nodes,
2753                    net->ipv6.rt6_stats->fib_rt_alloc,
2754                    net->ipv6.rt6_stats->fib_rt_entries,
2755                    net->ipv6.rt6_stats->fib_rt_cache,
2756                    dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
2757                    net->ipv6.rt6_stats->fib_discarded_routes);
2758
2759         return 0;
2760 }
2761
2762 static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2763 {
2764         return single_open_net(inode, file, rt6_stats_seq_show);
2765 }
2766
2767 static const struct file_operations rt6_stats_seq_fops = {
2768         .owner   = THIS_MODULE,
2769         .open    = rt6_stats_seq_open,
2770         .read    = seq_read,
2771         .llseek  = seq_lseek,
2772         .release = single_release_net,
2773 };
2774 #endif  /* CONFIG_PROC_FS */
2775
2776 #ifdef CONFIG_SYSCTL
2777
2778 static
2779 int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
2780                               void __user *buffer, size_t *lenp, loff_t *ppos)
2781 {
2782         struct net *net;
2783         int delay;
2784         if (!write)
2785                 return -EINVAL;
2786
2787         net = (struct net *)ctl->extra1;
2788         delay = net->ipv6.sysctl.flush_delay;
2789         proc_dointvec(ctl, write, buffer, lenp, ppos);
2790         fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
2791         return 0;
2792 }
2793
2794 ctl_table ipv6_route_table_template[] = {
2795         {
2796                 .procname       =       "flush",
2797                 .data           =       &init_net.ipv6.sysctl.flush_delay,
2798                 .maxlen         =       sizeof(int),
2799                 .mode           =       0200,
2800                 .proc_handler   =       ipv6_sysctl_rtcache_flush
2801         },
2802         {
2803                 .procname       =       "gc_thresh",
2804                 .data           =       &ip6_dst_ops_template.gc_thresh,
2805                 .maxlen         =       sizeof(int),
2806                 .mode           =       0644,
2807                 .proc_handler   =       proc_dointvec,
2808         },
2809         {
2810                 .procname       =       "max_size",
2811                 .data           =       &init_net.ipv6.sysctl.ip6_rt_max_size,
2812                 .maxlen         =       sizeof(int),
2813                 .mode           =       0644,
2814                 .proc_handler   =       proc_dointvec,
2815         },
2816         {
2817                 .procname       =       "gc_min_interval",
2818                 .data           =       &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
2819                 .maxlen         =       sizeof(int),
2820                 .mode           =       0644,
2821                 .proc_handler   =       proc_dointvec_jiffies,
2822         },
2823         {
2824                 .procname       =       "gc_timeout",
2825                 .data           =       &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
2826                 .maxlen         =       sizeof(int),
2827                 .mode           =       0644,
2828                 .proc_handler   =       proc_dointvec_jiffies,
2829         },
2830         {
2831                 .procname       =       "gc_interval",
2832                 .data           =       &init_net.ipv6.sysctl.ip6_rt_gc_interval,
2833                 .maxlen         =       sizeof(int),
2834                 .mode           =       0644,
2835                 .proc_handler   =       proc_dointvec_jiffies,
2836         },
2837         {
2838                 .procname       =       "gc_elasticity",
2839                 .data           =       &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
2840                 .maxlen         =       sizeof(int),
2841                 .mode           =       0644,
2842                 .proc_handler   =       proc_dointvec,
2843         },
2844         {
2845                 .procname       =       "mtu_expires",
2846                 .data           =       &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
2847                 .maxlen         =       sizeof(int),
2848                 .mode           =       0644,
2849                 .proc_handler   =       proc_dointvec_jiffies,
2850         },
2851         {
2852                 .procname       =       "min_adv_mss",
2853                 .data           =       &init_net.ipv6.sysctl.ip6_rt_min_advmss,
2854                 .maxlen         =       sizeof(int),
2855                 .mode           =       0644,
2856                 .proc_handler   =       proc_dointvec,
2857         },
2858         {
2859                 .procname       =       "gc_min_interval_ms",
2860                 .data           =       &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
2861                 .maxlen         =       sizeof(int),
2862                 .mode           =       0644,
2863                 .proc_handler   =       proc_dointvec_ms_jiffies,
2864         },
2865         { }
2866 };
2867
2868 struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2869 {
2870         struct ctl_table *table;
2871
2872         table = kmemdup(ipv6_route_table_template,
2873                         sizeof(ipv6_route_table_template),
2874                         GFP_KERNEL);
2875
2876         if (table) {
2877                 table[0].data = &net->ipv6.sysctl.flush_delay;
2878                 table[0].extra1 = net;
2879                 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
2880                 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2881                 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2882                 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2883                 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2884                 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2885                 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2886                 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
2887                 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2888
2889                 /* Don't export sysctls to unprivileged users */
2890                 if (net->user_ns != &init_user_ns)
2891                         table[0].procname = NULL;
2892         }
2893
2894         return table;
2895 }
2896 #endif
2897
2898 static int __net_init ip6_route_net_init(struct net *net)
2899 {
2900         int ret = -ENOMEM;
2901
2902         memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
2903                sizeof(net->ipv6.ip6_dst_ops));
2904
2905         if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2906                 goto out_ip6_dst_ops;
2907
2908         net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
2909                                            sizeof(*net->ipv6.ip6_null_entry),
2910                                            GFP_KERNEL);
2911         if (!net->ipv6.ip6_null_entry)
2912                 goto out_ip6_dst_entries;
2913         net->ipv6.ip6_null_entry->dst.path =
2914                 (struct dst_entry *)net->ipv6.ip6_null_entry;
2915         net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
2916         dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
2917                          ip6_template_metrics, true);
2918
2919 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2920         net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
2921                                                sizeof(*net->ipv6.ip6_prohibit_entry),
2922                                                GFP_KERNEL);
2923         if (!net->ipv6.ip6_prohibit_entry)
2924                 goto out_ip6_null_entry;
2925         net->ipv6.ip6_prohibit_entry->dst.path =
2926                 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2927         net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
2928         dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
2929                          ip6_template_metrics, true);
2930
2931         net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
2932                                                sizeof(*net->ipv6.ip6_blk_hole_entry),
2933                                                GFP_KERNEL);
2934         if (!net->ipv6.ip6_blk_hole_entry)
2935                 goto out_ip6_prohibit_entry;
2936         net->ipv6.ip6_blk_hole_entry->dst.path =
2937                 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2938         net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
2939         dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
2940                          ip6_template_metrics, true);
2941 #endif
2942
2943         net->ipv6.sysctl.flush_delay = 0;
2944         net->ipv6.sysctl.ip6_rt_max_size = 4096;
2945         net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2946         net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2947         net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2948         net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2949         net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2950         net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2951
2952         net->ipv6.ip6_rt_gc_expire = 30*HZ;
2953
2954         ret = 0;
2955 out:
2956         return ret;
2957
2958 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2959 out_ip6_prohibit_entry:
2960         kfree(net->ipv6.ip6_prohibit_entry);
2961 out_ip6_null_entry:
2962         kfree(net->ipv6.ip6_null_entry);
2963 #endif
2964 out_ip6_dst_entries:
2965         dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2966 out_ip6_dst_ops:
2967         goto out;
2968 }
2969
2970 static void __net_exit ip6_route_net_exit(struct net *net)
2971 {
2972         kfree(net->ipv6.ip6_null_entry);
2973 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2974         kfree(net->ipv6.ip6_prohibit_entry);
2975         kfree(net->ipv6.ip6_blk_hole_entry);
2976 #endif
2977         dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2978 }
2979
2980 static int __net_init ip6_route_net_init_late(struct net *net)
2981 {
2982 #ifdef CONFIG_PROC_FS
2983         proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
2984         proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
2985 #endif
2986         return 0;
2987 }
2988
2989 static void __net_exit ip6_route_net_exit_late(struct net *net)
2990 {
2991 #ifdef CONFIG_PROC_FS
2992         remove_proc_entry("ipv6_route", net->proc_net);
2993         remove_proc_entry("rt6_stats", net->proc_net);
2994 #endif
2995 }
2996
2997 static struct pernet_operations ip6_route_net_ops = {
2998         .init = ip6_route_net_init,
2999         .exit = ip6_route_net_exit,
3000 };
3001
3002 static int __net_init ipv6_inetpeer_init(struct net *net)
3003 {
3004         struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3005
3006         if (!bp)
3007                 return -ENOMEM;
3008         inet_peer_base_init(bp);
3009         net->ipv6.peers = bp;
3010         return 0;
3011 }
3012
3013 static void __net_exit ipv6_inetpeer_exit(struct net *net)
3014 {
3015         struct inet_peer_base *bp = net->ipv6.peers;
3016
3017         net->ipv6.peers = NULL;
3018         inetpeer_invalidate_tree(bp);
3019         kfree(bp);
3020 }
3021
3022 static struct pernet_operations ipv6_inetpeer_ops = {
3023         .init   =       ipv6_inetpeer_init,
3024         .exit   =       ipv6_inetpeer_exit,
3025 };
3026
3027 static struct pernet_operations ip6_route_net_late_ops = {
3028         .init = ip6_route_net_init_late,
3029         .exit = ip6_route_net_exit_late,
3030 };
3031
3032 static struct notifier_block ip6_route_dev_notifier = {
3033         .notifier_call = ip6_route_dev_notify,
3034         .priority = 0,
3035 };
3036
3037 int __init ip6_route_init(void)
3038 {
3039         int ret;
3040
3041         ret = -ENOMEM;
3042         ip6_dst_ops_template.kmem_cachep =
3043                 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
3044                                   SLAB_HWCACHE_ALIGN, NULL);
3045         if (!ip6_dst_ops_template.kmem_cachep)
3046                 goto out;
3047
3048         ret = dst_entries_init(&ip6_dst_blackhole_ops);
3049         if (ret)
3050                 goto out_kmem_cache;
3051
3052         ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3053         if (ret)
3054                 goto out_dst_entries;
3055
3056         ret = register_pernet_subsys(&ip6_route_net_ops);
3057         if (ret)
3058                 goto out_register_inetpeer;
3059
3060         ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
3061
3062         /* Registering of the loopback is done before this portion of code,
3063          * the loopback reference in rt6_info will not be taken, do it
3064          * manually for init_net */
3065         init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
3066         init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3067   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3068         init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
3069         init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3070         init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
3071         init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3072   #endif
3073         ret = fib6_init();
3074         if (ret)
3075                 goto out_register_subsys;
3076
3077         ret = xfrm6_init();
3078         if (ret)
3079                 goto out_fib6_init;
3080
3081         ret = fib6_rules_init();
3082         if (ret)
3083                 goto xfrm6_init;
3084
3085         ret = register_pernet_subsys(&ip6_route_net_late_ops);
3086         if (ret)
3087                 goto fib6_rules_init;
3088
3089         ret = -ENOBUFS;
3090         if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3091             __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3092             __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3093                 goto out_register_late_subsys;
3094
3095         ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3096         if (ret)
3097                 goto out_register_late_subsys;
3098
3099 out:
3100         return ret;
3101
3102 out_register_late_subsys:
3103         unregister_pernet_subsys(&ip6_route_net_late_ops);
3104 fib6_rules_init:
3105         fib6_rules_cleanup();
3106 xfrm6_init:
3107         xfrm6_fini();
3108 out_fib6_init:
3109         fib6_gc_cleanup();
3110 out_register_subsys:
3111         unregister_pernet_subsys(&ip6_route_net_ops);
3112 out_register_inetpeer:
3113         unregister_pernet_subsys(&ipv6_inetpeer_ops);
3114 out_dst_entries:
3115         dst_entries_destroy(&ip6_dst_blackhole_ops);
3116 out_kmem_cache:
3117         kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3118         goto out;
3119 }
3120
3121 void ip6_route_cleanup(void)
3122 {
3123         unregister_netdevice_notifier(&ip6_route_dev_notifier);
3124         unregister_pernet_subsys(&ip6_route_net_late_ops);
3125         fib6_rules_cleanup();
3126         xfrm6_fini();
3127         fib6_gc_cleanup();
3128         unregister_pernet_subsys(&ipv6_inetpeer_ops);
3129         unregister_pernet_subsys(&ip6_route_net_ops);
3130         dst_entries_destroy(&ip6_dst_blackhole_ops);
3131         kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3132 }