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