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