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