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