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