]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv6/route.c
inet: frags: call inet_frags_fini() after unregister_pernet_subsys()
[mirror_ubuntu-bionic-kernel.git] / net / ipv6 / route.c
CommitLineData
1da177e4
LT
1/*
2 * Linux INET6 implementation
3 * FIB front-end.
4 *
5 * Authors:
1ab1457c 6 * Pedro Roque <roque@di.fc.ul.pt>
1da177e4 7 *
1da177e4
LT
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14/* Changes:
15 *
16 * YOSHIFUJI Hideaki @USAGI
17 * reworked default router selection.
18 * - respect outgoing interface
19 * - select from (probably) reachable routers (i.e.
20 * routers in REACHABLE, STALE, DELAY or PROBE states).
21 * - always select the same router if it is (probably)
22 * reachable. otherwise, round-robin the list.
c0bece9f
YH
23 * Ville Nuorvala
24 * Fixed routing subtrees.
1da177e4
LT
25 */
26
f3213831
JP
27#define pr_fmt(fmt) "IPv6: " fmt
28
4fc268d2 29#include <linux/capability.h>
1da177e4 30#include <linux/errno.h>
bc3b2d7f 31#include <linux/export.h>
1da177e4
LT
32#include <linux/types.h>
33#include <linux/times.h>
34#include <linux/socket.h>
35#include <linux/sockios.h>
36#include <linux/net.h>
37#include <linux/route.h>
38#include <linux/netdevice.h>
39#include <linux/in6.h>
7bc570c8 40#include <linux/mroute6.h>
1da177e4 41#include <linux/init.h>
1da177e4 42#include <linux/if_arp.h>
1da177e4
LT
43#include <linux/proc_fs.h>
44#include <linux/seq_file.h>
5b7c931d 45#include <linux/nsproxy.h>
5a0e3ad6 46#include <linux/slab.h>
35732d01 47#include <linux/jhash.h>
457c4cbc 48#include <net/net_namespace.h>
1da177e4
LT
49#include <net/snmp.h>
50#include <net/ipv6.h>
51#include <net/ip6_fib.h>
52#include <net/ip6_route.h>
53#include <net/ndisc.h>
54#include <net/addrconf.h>
55#include <net/tcp.h>
56#include <linux/rtnetlink.h>
57#include <net/dst.h>
904af04d 58#include <net/dst_metadata.h>
1da177e4 59#include <net/xfrm.h>
8d71740c 60#include <net/netevent.h>
21713ebc 61#include <net/netlink.h>
51ebd318 62#include <net/nexthop.h>
19e42e45 63#include <net/lwtunnel.h>
904af04d 64#include <net/ip_tunnels.h>
ca254490 65#include <net/l3mdev.h>
b811580d 66#include <trace/events/fib6.h>
1da177e4 67
7c0f6ba6 68#include <linux/uaccess.h>
1da177e4
LT
69
70#ifdef CONFIG_SYSCTL
71#include <linux/sysctl.h>
72#endif
73
afc154e9 74enum rt6_nud_state {
7e980569
JB
75 RT6_NUD_FAIL_HARD = -3,
76 RT6_NUD_FAIL_PROBE = -2,
77 RT6_NUD_FAIL_DO_RR = -1,
afc154e9
HFS
78 RT6_NUD_SUCCEED = 1
79};
80
83a09abd 81static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort);
1da177e4 82static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
0dbaee3b 83static unsigned int ip6_default_advmss(const struct dst_entry *dst);
ebb762f2 84static unsigned int ip6_mtu(const struct dst_entry *dst);
1da177e4
LT
85static struct dst_entry *ip6_negative_advice(struct dst_entry *);
86static void ip6_dst_destroy(struct dst_entry *);
87static void ip6_dst_ifdown(struct dst_entry *,
88 struct net_device *dev, int how);
569d3645 89static int ip6_dst_gc(struct dst_ops *ops);
1da177e4
LT
90
91static int ip6_pkt_discard(struct sk_buff *skb);
ede2059d 92static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
7150aede 93static int ip6_pkt_prohibit(struct sk_buff *skb);
ede2059d 94static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
1da177e4 95static void ip6_link_failure(struct sk_buff *skb);
6700c270 96static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
b8a653b5
HL
97 struct sk_buff *skb, u32 mtu,
98 bool confirm_neigh);
6700c270
DM
99static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
100 struct sk_buff *skb);
4b32b5ad 101static void rt6_dst_from_metrics_check(struct rt6_info *rt);
52bd4c0c 102static int rt6_score_route(struct rt6_info *rt, int oif, int strict);
16a16cd3
DA
103static size_t rt6_nlmsg_size(struct rt6_info *rt);
104static int rt6_fill_node(struct net *net,
105 struct sk_buff *skb, struct rt6_info *rt,
106 struct in6_addr *dst, struct in6_addr *src,
107 int iif, int type, u32 portid, u32 seq,
108 unsigned int flags);
35732d01
WW
109static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt,
110 struct in6_addr *daddr,
111 struct in6_addr *saddr);
1da177e4 112
70ceb4f5 113#ifdef CONFIG_IPV6_ROUTE_INFO
efa2cea0 114static struct rt6_info *rt6_add_route_info(struct net *net,
b71d1d42 115 const struct in6_addr *prefix, int prefixlen,
830218c1
DA
116 const struct in6_addr *gwaddr,
117 struct net_device *dev,
95c96174 118 unsigned int pref);
efa2cea0 119static struct rt6_info *rt6_get_route_info(struct net *net,
b71d1d42 120 const struct in6_addr *prefix, int prefixlen,
830218c1
DA
121 const struct in6_addr *gwaddr,
122 struct net_device *dev);
70ceb4f5
YH
123#endif
124
8d0b94af
MKL
125struct uncached_list {
126 spinlock_t lock;
127 struct list_head head;
128};
129
130static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
131
31a5b09d 132void rt6_uncached_list_add(struct rt6_info *rt)
8d0b94af
MKL
133{
134 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
135
8d0b94af
MKL
136 rt->rt6i_uncached_list = ul;
137
138 spin_lock_bh(&ul->lock);
139 list_add_tail(&rt->rt6i_uncached, &ul->head);
140 spin_unlock_bh(&ul->lock);
141}
142
31a5b09d 143void rt6_uncached_list_del(struct rt6_info *rt)
8d0b94af
MKL
144{
145 if (!list_empty(&rt->rt6i_uncached)) {
146 struct uncached_list *ul = rt->rt6i_uncached_list;
81eb8447 147 struct net *net = dev_net(rt->dst.dev);
8d0b94af
MKL
148
149 spin_lock_bh(&ul->lock);
150 list_del(&rt->rt6i_uncached);
81eb8447 151 atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
8d0b94af
MKL
152 spin_unlock_bh(&ul->lock);
153 }
154}
155
156static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
157{
158 struct net_device *loopback_dev = net->loopback_dev;
159 int cpu;
160
e332bc67
EB
161 if (dev == loopback_dev)
162 return;
163
8d0b94af
MKL
164 for_each_possible_cpu(cpu) {
165 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
166 struct rt6_info *rt;
167
168 spin_lock_bh(&ul->lock);
169 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
170 struct inet6_dev *rt_idev = rt->rt6i_idev;
171 struct net_device *rt_dev = rt->dst.dev;
172
e332bc67 173 if (rt_idev->dev == dev) {
8d0b94af
MKL
174 rt->rt6i_idev = in6_dev_get(loopback_dev);
175 in6_dev_put(rt_idev);
176 }
177
e332bc67 178 if (rt_dev == dev) {
8d0b94af
MKL
179 rt->dst.dev = loopback_dev;
180 dev_hold(rt->dst.dev);
181 dev_put(rt_dev);
182 }
183 }
184 spin_unlock_bh(&ul->lock);
185 }
186}
187
d52d3997
MKL
188static u32 *rt6_pcpu_cow_metrics(struct rt6_info *rt)
189{
190 return dst_metrics_write_ptr(rt->dst.from);
191}
192
06582540
DM
193static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
194{
4b32b5ad 195 struct rt6_info *rt = (struct rt6_info *)dst;
06582540 196
d52d3997
MKL
197 if (rt->rt6i_flags & RTF_PCPU)
198 return rt6_pcpu_cow_metrics(rt);
199 else if (rt->rt6i_flags & RTF_CACHE)
4b32b5ad
MKL
200 return NULL;
201 else
3b471175 202 return dst_cow_metrics_generic(dst, old);
06582540
DM
203}
204
7ef825d3 205static inline const void *choose_neigh_daddr(const struct in6_addr *p,
f894cbf8
DM
206 struct sk_buff *skb,
207 const void *daddr)
39232973 208{
a7563f34 209 if (!ipv6_addr_any(p))
39232973 210 return (const void *) p;
f894cbf8
DM
211 else if (skb)
212 return &ipv6_hdr(skb)->daddr;
39232973
DM
213 return daddr;
214}
215
f894cbf8
DM
216static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
217 struct sk_buff *skb,
218 const void *daddr)
d3aaeb38 219{
39232973
DM
220 struct rt6_info *rt = (struct rt6_info *) dst;
221 struct neighbour *n;
222
7ef825d3 223 daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), skb, daddr);
8e022ee6 224 n = __ipv6_neigh_lookup(dst->dev, daddr);
f83c7790
DM
225 if (n)
226 return n;
227 return neigh_create(&nd_tbl, daddr, dst->dev);
228}
229
63fca65d
JA
230static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
231{
232 struct net_device *dev = dst->dev;
233 struct rt6_info *rt = (struct rt6_info *)dst;
234
7ef825d3 235 daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr);
63fca65d
JA
236 if (!daddr)
237 return;
238 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
239 return;
240 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
241 return;
242 __ipv6_confirm_neigh(dev, daddr);
243}
244
9a7ec3a9 245static struct dst_ops ip6_dst_ops_template = {
1da177e4 246 .family = AF_INET6,
1da177e4
LT
247 .gc = ip6_dst_gc,
248 .gc_thresh = 1024,
249 .check = ip6_dst_check,
0dbaee3b 250 .default_advmss = ip6_default_advmss,
ebb762f2 251 .mtu = ip6_mtu,
06582540 252 .cow_metrics = ipv6_cow_metrics,
1da177e4
LT
253 .destroy = ip6_dst_destroy,
254 .ifdown = ip6_dst_ifdown,
255 .negative_advice = ip6_negative_advice,
256 .link_failure = ip6_link_failure,
257 .update_pmtu = ip6_rt_update_pmtu,
6e157b6a 258 .redirect = rt6_do_redirect,
9f8955cc 259 .local_out = __ip6_local_out,
d3aaeb38 260 .neigh_lookup = ip6_neigh_lookup,
63fca65d 261 .confirm_neigh = ip6_confirm_neigh,
1da177e4
LT
262};
263
ebb762f2 264static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
ec831ea7 265{
618f9bc7
SK
266 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
267
268 return mtu ? : dst->dev->mtu;
ec831ea7
RD
269}
270
6700c270 271static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
b8a653b5
HL
272 struct sk_buff *skb, u32 mtu,
273 bool confirm_neigh)
14e50e57
DM
274{
275}
276
6700c270
DM
277static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
278 struct sk_buff *skb)
b587ee3b
DM
279{
280}
281
14e50e57
DM
282static struct dst_ops ip6_dst_blackhole_ops = {
283 .family = AF_INET6,
14e50e57
DM
284 .destroy = ip6_dst_destroy,
285 .check = ip6_dst_check,
ebb762f2 286 .mtu = ip6_blackhole_mtu,
214f45c9 287 .default_advmss = ip6_default_advmss,
14e50e57 288 .update_pmtu = ip6_rt_blackhole_update_pmtu,
b587ee3b 289 .redirect = ip6_rt_blackhole_redirect,
0a1f5962 290 .cow_metrics = dst_cow_metrics_generic,
d3aaeb38 291 .neigh_lookup = ip6_neigh_lookup,
14e50e57
DM
292};
293
62fa8a84 294static const u32 ip6_template_metrics[RTAX_MAX] = {
14edd87d 295 [RTAX_HOPLIMIT - 1] = 0,
62fa8a84
DM
296};
297
fb0af4c7 298static const struct rt6_info ip6_null_entry_template = {
d8d1f30b
CG
299 .dst = {
300 .__refcnt = ATOMIC_INIT(1),
301 .__use = 1,
2c20cbd7 302 .obsolete = DST_OBSOLETE_FORCE_CHK,
d8d1f30b 303 .error = -ENETUNREACH,
d8d1f30b
CG
304 .input = ip6_pkt_discard,
305 .output = ip6_pkt_discard_out,
1da177e4
LT
306 },
307 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
4f724279 308 .rt6i_protocol = RTPROT_KERNEL,
1da177e4
LT
309 .rt6i_metric = ~(u32) 0,
310 .rt6i_ref = ATOMIC_INIT(1),
311};
312
101367c2
TG
313#ifdef CONFIG_IPV6_MULTIPLE_TABLES
314
fb0af4c7 315static const struct rt6_info ip6_prohibit_entry_template = {
d8d1f30b
CG
316 .dst = {
317 .__refcnt = ATOMIC_INIT(1),
318 .__use = 1,
2c20cbd7 319 .obsolete = DST_OBSOLETE_FORCE_CHK,
d8d1f30b 320 .error = -EACCES,
d8d1f30b
CG
321 .input = ip6_pkt_prohibit,
322 .output = ip6_pkt_prohibit_out,
101367c2
TG
323 },
324 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
4f724279 325 .rt6i_protocol = RTPROT_KERNEL,
101367c2
TG
326 .rt6i_metric = ~(u32) 0,
327 .rt6i_ref = ATOMIC_INIT(1),
328};
329
fb0af4c7 330static const struct rt6_info ip6_blk_hole_entry_template = {
d8d1f30b
CG
331 .dst = {
332 .__refcnt = ATOMIC_INIT(1),
333 .__use = 1,
2c20cbd7 334 .obsolete = DST_OBSOLETE_FORCE_CHK,
d8d1f30b 335 .error = -EINVAL,
d8d1f30b 336 .input = dst_discard,
ede2059d 337 .output = dst_discard_out,
101367c2
TG
338 },
339 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
4f724279 340 .rt6i_protocol = RTPROT_KERNEL,
101367c2
TG
341 .rt6i_metric = ~(u32) 0,
342 .rt6i_ref = ATOMIC_INIT(1),
343};
344
345#endif
346
ebfa45f0
MKL
347static void rt6_info_init(struct rt6_info *rt)
348{
349 struct dst_entry *dst = &rt->dst;
350
351 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
352 INIT_LIST_HEAD(&rt->rt6i_siblings);
353 INIT_LIST_HEAD(&rt->rt6i_uncached);
354}
355
1da177e4 356/* allocate dst with ip6_dst_ops */
d52d3997
MKL
357static struct rt6_info *__ip6_dst_alloc(struct net *net,
358 struct net_device *dev,
ad706862 359 int flags)
1da177e4 360{
97bab73f 361 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
b2a9c0ed 362 1, DST_OBSOLETE_FORCE_CHK, flags);
cf911662 363
81eb8447 364 if (rt) {
ebfa45f0 365 rt6_info_init(rt);
81eb8447
WW
366 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
367 }
8104891b 368
cf911662 369 return rt;
1da177e4
LT
370}
371
9ab179d8
DA
372struct rt6_info *ip6_dst_alloc(struct net *net,
373 struct net_device *dev,
374 int flags)
d52d3997 375{
ad706862 376 struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags);
d52d3997
MKL
377
378 if (rt) {
379 rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC);
bfd8e5a4 380 if (!rt->rt6i_pcpu) {
587fea74 381 dst_release_immediate(&rt->dst);
d52d3997
MKL
382 return NULL;
383 }
384 }
385
386 return rt;
387}
9ab179d8 388EXPORT_SYMBOL(ip6_dst_alloc);
d52d3997 389
1da177e4
LT
390static void ip6_dst_destroy(struct dst_entry *dst)
391{
392 struct rt6_info *rt = (struct rt6_info *)dst;
35732d01 393 struct rt6_exception_bucket *bucket;
ecd98837 394 struct dst_entry *from = dst->from;
8d0b94af 395 struct inet6_dev *idev;
1da177e4 396
4b32b5ad 397 dst_destroy_metrics_generic(dst);
87775312 398 free_percpu(rt->rt6i_pcpu);
8d0b94af
MKL
399 rt6_uncached_list_del(rt);
400
401 idev = rt->rt6i_idev;
38308473 402 if (idev) {
1da177e4
LT
403 rt->rt6i_idev = NULL;
404 in6_dev_put(idev);
1ab1457c 405 }
35732d01
WW
406 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1);
407 if (bucket) {
408 rt->rt6i_exception_bucket = NULL;
409 kfree(bucket);
410 }
1716a961 411
ecd98837
YH
412 dst->from = NULL;
413 dst_release(from);
b3419363
DM
414}
415
1da177e4
LT
416static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
417 int how)
418{
419 struct rt6_info *rt = (struct rt6_info *)dst;
420 struct inet6_dev *idev = rt->rt6i_idev;
5a3e55d6 421 struct net_device *loopback_dev =
c346dca1 422 dev_net(dev)->loopback_dev;
1da177e4 423
e5645f51
WW
424 if (idev && idev->dev != loopback_dev) {
425 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
426 if (loopback_idev) {
427 rt->rt6i_idev = loopback_idev;
428 in6_dev_put(idev);
97cac082 429 }
1da177e4
LT
430 }
431}
432
5973fb1e
MKL
433static bool __rt6_check_expired(const struct rt6_info *rt)
434{
435 if (rt->rt6i_flags & RTF_EXPIRES)
436 return time_after(jiffies, rt->dst.expires);
437 else
438 return false;
439}
440
a50feda5 441static bool rt6_check_expired(const struct rt6_info *rt)
1da177e4 442{
1716a961
G
443 if (rt->rt6i_flags & RTF_EXPIRES) {
444 if (time_after(jiffies, rt->dst.expires))
a50feda5 445 return true;
1716a961 446 } else if (rt->dst.from) {
1e2ea8ad
XL
447 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
448 rt6_check_expired((struct rt6_info *)rt->dst.from);
1716a961 449 }
a50feda5 450 return false;
1da177e4
LT
451}
452
51ebd318 453static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
52bd4c0c
ND
454 struct flowi6 *fl6, int oif,
455 int strict)
51ebd318
ND
456{
457 struct rt6_info *sibling, *next_sibling;
458 int route_choosen;
459
b673d6cc
JS
460 /* We might have already computed the hash for ICMPv6 errors. In such
461 * case it will always be non-zero. Otherwise now is the time to do it.
462 */
463 if (!fl6->mp_hash)
464 fl6->mp_hash = rt6_multipath_hash(fl6, NULL);
465
466 route_choosen = fl6->mp_hash % (match->rt6i_nsiblings + 1);
51ebd318
ND
467 /* Don't change the route, if route_choosen == 0
468 * (siblings does not include ourself)
469 */
470 if (route_choosen)
471 list_for_each_entry_safe(sibling, next_sibling,
472 &match->rt6i_siblings, rt6i_siblings) {
473 route_choosen--;
474 if (route_choosen == 0) {
bbfcd776
IS
475 struct inet6_dev *idev = sibling->rt6i_idev;
476
477 if (!netif_carrier_ok(sibling->dst.dev) &&
478 idev->cnf.ignore_routes_with_linkdown)
479 break;
52bd4c0c
ND
480 if (rt6_score_route(sibling, oif, strict) < 0)
481 break;
51ebd318
ND
482 match = sibling;
483 break;
484 }
485 }
486 return match;
487}
488
1da177e4 489/*
66f5d6ce 490 * Route lookup. rcu_read_lock() should be held.
1da177e4
LT
491 */
492
8ed67789
DL
493static inline struct rt6_info *rt6_device_match(struct net *net,
494 struct rt6_info *rt,
b71d1d42 495 const struct in6_addr *saddr,
1da177e4 496 int oif,
d420895e 497 int flags)
1da177e4
LT
498{
499 struct rt6_info *local = NULL;
500 struct rt6_info *sprt;
501
dd3abc4e
YH
502 if (!oif && ipv6_addr_any(saddr))
503 goto out;
504
66f5d6ce 505 for (sprt = rt; sprt; sprt = rcu_dereference(sprt->dst.rt6_next)) {
d1918542 506 struct net_device *dev = sprt->dst.dev;
dd3abc4e
YH
507
508 if (oif) {
1da177e4
LT
509 if (dev->ifindex == oif)
510 return sprt;
511 if (dev->flags & IFF_LOOPBACK) {
38308473 512 if (!sprt->rt6i_idev ||
1da177e4 513 sprt->rt6i_idev->dev->ifindex != oif) {
17fb0b2b 514 if (flags & RT6_LOOKUP_F_IFACE)
1da177e4 515 continue;
17fb0b2b
DA
516 if (local &&
517 local->rt6i_idev->dev->ifindex == oif)
1da177e4
LT
518 continue;
519 }
520 local = sprt;
521 }
dd3abc4e
YH
522 } else {
523 if (ipv6_chk_addr(net, saddr, dev,
524 flags & RT6_LOOKUP_F_IFACE))
525 return sprt;
1da177e4 526 }
dd3abc4e 527 }
1da177e4 528
dd3abc4e 529 if (oif) {
1da177e4
LT
530 if (local)
531 return local;
532
d420895e 533 if (flags & RT6_LOOKUP_F_IFACE)
8ed67789 534 return net->ipv6.ip6_null_entry;
1da177e4 535 }
dd3abc4e 536out:
1da177e4
LT
537 return rt;
538}
539
27097255 540#ifdef CONFIG_IPV6_ROUTER_PREF
c2f17e82
HFS
541struct __rt6_probe_work {
542 struct work_struct work;
543 struct in6_addr target;
544 struct net_device *dev;
545};
546
547static void rt6_probe_deferred(struct work_struct *w)
548{
549 struct in6_addr mcaddr;
550 struct __rt6_probe_work *work =
551 container_of(w, struct __rt6_probe_work, work);
552
553 addrconf_addr_solict_mult(&work->target, &mcaddr);
adc176c5 554 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
c2f17e82 555 dev_put(work->dev);
662f5533 556 kfree(work);
c2f17e82
HFS
557}
558
27097255
YH
559static void rt6_probe(struct rt6_info *rt)
560{
990edb42 561 struct __rt6_probe_work *work;
f2c31e32 562 struct neighbour *neigh;
27097255
YH
563 /*
564 * Okay, this does not seem to be appropriate
565 * for now, however, we need to check if it
566 * is really so; aka Router Reachability Probing.
567 *
568 * Router Reachability Probe MUST be rate-limited
569 * to no more than one per minute.
570 */
2152caea 571 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
7ff74a59 572 return;
2152caea
YH
573 rcu_read_lock_bh();
574 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
575 if (neigh) {
8d6c31bf
MKL
576 if (neigh->nud_state & NUD_VALID)
577 goto out;
578
990edb42 579 work = NULL;
2152caea 580 write_lock(&neigh->lock);
990edb42
MKL
581 if (!(neigh->nud_state & NUD_VALID) &&
582 time_after(jiffies,
583 neigh->updated +
584 rt->rt6i_idev->cnf.rtr_probe_interval)) {
585 work = kmalloc(sizeof(*work), GFP_ATOMIC);
586 if (work)
587 __neigh_set_probe_once(neigh);
c2f17e82 588 }
2152caea 589 write_unlock(&neigh->lock);
990edb42
MKL
590 } else {
591 work = kmalloc(sizeof(*work), GFP_ATOMIC);
f2c31e32 592 }
990edb42
MKL
593
594 if (work) {
595 INIT_WORK(&work->work, rt6_probe_deferred);
596 work->target = rt->rt6i_gateway;
597 dev_hold(rt->dst.dev);
598 work->dev = rt->dst.dev;
599 schedule_work(&work->work);
600 }
601
8d6c31bf 602out:
2152caea 603 rcu_read_unlock_bh();
27097255
YH
604}
605#else
606static inline void rt6_probe(struct rt6_info *rt)
607{
27097255
YH
608}
609#endif
610
1da177e4 611/*
554cfb7e 612 * Default Router Selection (RFC 2461 6.3.6)
1da177e4 613 */
b6f99a21 614static inline int rt6_check_dev(struct rt6_info *rt, int oif)
554cfb7e 615{
d1918542 616 struct net_device *dev = rt->dst.dev;
161980f4 617 if (!oif || dev->ifindex == oif)
554cfb7e 618 return 2;
161980f4
DM
619 if ((dev->flags & IFF_LOOPBACK) &&
620 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
621 return 1;
622 return 0;
554cfb7e 623}
1da177e4 624
afc154e9 625static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
1da177e4 626{
f2c31e32 627 struct neighbour *neigh;
afc154e9 628 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
f2c31e32 629
4d0c5911
YH
630 if (rt->rt6i_flags & RTF_NONEXTHOP ||
631 !(rt->rt6i_flags & RTF_GATEWAY))
afc154e9 632 return RT6_NUD_SUCCEED;
145a3621
YH
633
634 rcu_read_lock_bh();
635 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
636 if (neigh) {
637 read_lock(&neigh->lock);
554cfb7e 638 if (neigh->nud_state & NUD_VALID)
afc154e9 639 ret = RT6_NUD_SUCCEED;
398bcbeb 640#ifdef CONFIG_IPV6_ROUTER_PREF
a5a81f0b 641 else if (!(neigh->nud_state & NUD_FAILED))
afc154e9 642 ret = RT6_NUD_SUCCEED;
7e980569
JB
643 else
644 ret = RT6_NUD_FAIL_PROBE;
398bcbeb 645#endif
145a3621 646 read_unlock(&neigh->lock);
afc154e9
HFS
647 } else {
648 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
7e980569 649 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
a5a81f0b 650 }
145a3621
YH
651 rcu_read_unlock_bh();
652
a5a81f0b 653 return ret;
1da177e4
LT
654}
655
554cfb7e
YH
656static int rt6_score_route(struct rt6_info *rt, int oif,
657 int strict)
1da177e4 658{
a5a81f0b 659 int m;
1ab1457c 660
4d0c5911 661 m = rt6_check_dev(rt, oif);
77d16f45 662 if (!m && (strict & RT6_LOOKUP_F_IFACE))
afc154e9 663 return RT6_NUD_FAIL_HARD;
ebacaaa0
YH
664#ifdef CONFIG_IPV6_ROUTER_PREF
665 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
666#endif
afc154e9
HFS
667 if (strict & RT6_LOOKUP_F_REACHABLE) {
668 int n = rt6_check_neigh(rt);
669 if (n < 0)
670 return n;
671 }
554cfb7e
YH
672 return m;
673}
674
f11e6659 675static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
afc154e9
HFS
676 int *mpri, struct rt6_info *match,
677 bool *do_rr)
554cfb7e 678{
f11e6659 679 int m;
afc154e9 680 bool match_do_rr = false;
35103d11
AG
681 struct inet6_dev *idev = rt->rt6i_idev;
682 struct net_device *dev = rt->dst.dev;
683
684 if (dev && !netif_carrier_ok(dev) &&
d5d32e4b
DA
685 idev->cnf.ignore_routes_with_linkdown &&
686 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
35103d11 687 goto out;
f11e6659
DM
688
689 if (rt6_check_expired(rt))
690 goto out;
691
692 m = rt6_score_route(rt, oif, strict);
7e980569 693 if (m == RT6_NUD_FAIL_DO_RR) {
afc154e9
HFS
694 match_do_rr = true;
695 m = 0; /* lowest valid score */
7e980569 696 } else if (m == RT6_NUD_FAIL_HARD) {
f11e6659 697 goto out;
afc154e9
HFS
698 }
699
700 if (strict & RT6_LOOKUP_F_REACHABLE)
701 rt6_probe(rt);
f11e6659 702
7e980569 703 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
f11e6659 704 if (m > *mpri) {
afc154e9 705 *do_rr = match_do_rr;
f11e6659
DM
706 *mpri = m;
707 match = rt;
f11e6659 708 }
f11e6659
DM
709out:
710 return match;
711}
712
713static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
8d1040e8 714 struct rt6_info *leaf,
f11e6659 715 struct rt6_info *rr_head,
afc154e9
HFS
716 u32 metric, int oif, int strict,
717 bool *do_rr)
f11e6659 718{
9fbdcfaf 719 struct rt6_info *rt, *match, *cont;
554cfb7e 720 int mpri = -1;
1da177e4 721
f11e6659 722 match = NULL;
9fbdcfaf 723 cont = NULL;
66f5d6ce 724 for (rt = rr_head; rt; rt = rcu_dereference(rt->dst.rt6_next)) {
9fbdcfaf
SK
725 if (rt->rt6i_metric != metric) {
726 cont = rt;
727 break;
728 }
729
730 match = find_match(rt, oif, strict, &mpri, match, do_rr);
731 }
732
66f5d6ce
WW
733 for (rt = leaf; rt && rt != rr_head;
734 rt = rcu_dereference(rt->dst.rt6_next)) {
9fbdcfaf
SK
735 if (rt->rt6i_metric != metric) {
736 cont = rt;
737 break;
738 }
739
afc154e9 740 match = find_match(rt, oif, strict, &mpri, match, do_rr);
9fbdcfaf
SK
741 }
742
743 if (match || !cont)
744 return match;
745
66f5d6ce 746 for (rt = cont; rt; rt = rcu_dereference(rt->dst.rt6_next))
afc154e9 747 match = find_match(rt, oif, strict, &mpri, match, do_rr);
1da177e4 748
f11e6659
DM
749 return match;
750}
1da177e4 751
8d1040e8
WW
752static struct rt6_info *rt6_select(struct net *net, struct fib6_node *fn,
753 int oif, int strict)
f11e6659 754{
66f5d6ce 755 struct rt6_info *leaf = rcu_dereference(fn->leaf);
f11e6659 756 struct rt6_info *match, *rt0;
afc154e9 757 bool do_rr = false;
17ecf590 758 int key_plen;
1da177e4 759
87b1af8d 760 if (!leaf || leaf == net->ipv6.ip6_null_entry)
8d1040e8
WW
761 return net->ipv6.ip6_null_entry;
762
66f5d6ce 763 rt0 = rcu_dereference(fn->rr_ptr);
f11e6659 764 if (!rt0)
66f5d6ce 765 rt0 = leaf;
1da177e4 766
17ecf590
WW
767 /* Double check to make sure fn is not an intermediate node
768 * and fn->leaf does not points to its child's leaf
769 * (This might happen if all routes under fn are deleted from
770 * the tree and fib6_repair_tree() is called on the node.)
771 */
772 key_plen = rt0->rt6i_dst.plen;
773#ifdef CONFIG_IPV6_SUBTREES
774 if (rt0->rt6i_src.plen)
775 key_plen = rt0->rt6i_src.plen;
776#endif
777 if (fn->fn_bit != key_plen)
778 return net->ipv6.ip6_null_entry;
779
8d1040e8 780 match = find_rr_leaf(fn, leaf, rt0, rt0->rt6i_metric, oif, strict,
afc154e9 781 &do_rr);
1da177e4 782
afc154e9 783 if (do_rr) {
66f5d6ce 784 struct rt6_info *next = rcu_dereference(rt0->dst.rt6_next);
f11e6659 785
554cfb7e 786 /* no entries matched; do round-robin */
f11e6659 787 if (!next || next->rt6i_metric != rt0->rt6i_metric)
8d1040e8 788 next = leaf;
f11e6659 789
66f5d6ce
WW
790 if (next != rt0) {
791 spin_lock_bh(&leaf->rt6i_table->tb6_lock);
792 /* make sure next is not being deleted from the tree */
793 if (next->rt6i_node)
794 rcu_assign_pointer(fn->rr_ptr, next);
795 spin_unlock_bh(&leaf->rt6i_table->tb6_lock);
796 }
1da177e4 797 }
1da177e4 798
a02cec21 799 return match ? match : net->ipv6.ip6_null_entry;
1da177e4
LT
800}
801
8b9df265
MKL
802static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt)
803{
804 return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
805}
806
70ceb4f5
YH
807#ifdef CONFIG_IPV6_ROUTE_INFO
808int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
b71d1d42 809 const struct in6_addr *gwaddr)
70ceb4f5 810{
c346dca1 811 struct net *net = dev_net(dev);
70ceb4f5
YH
812 struct route_info *rinfo = (struct route_info *) opt;
813 struct in6_addr prefix_buf, *prefix;
814 unsigned int pref;
4bed72e4 815 unsigned long lifetime;
70ceb4f5
YH
816 struct rt6_info *rt;
817
818 if (len < sizeof(struct route_info)) {
819 return -EINVAL;
820 }
821
822 /* Sanity check for prefix_len and length */
823 if (rinfo->length > 3) {
824 return -EINVAL;
825 } else if (rinfo->prefix_len > 128) {
826 return -EINVAL;
827 } else if (rinfo->prefix_len > 64) {
828 if (rinfo->length < 2) {
829 return -EINVAL;
830 }
831 } else if (rinfo->prefix_len > 0) {
832 if (rinfo->length < 1) {
833 return -EINVAL;
834 }
835 }
836
837 pref = rinfo->route_pref;
838 if (pref == ICMPV6_ROUTER_PREF_INVALID)
3933fc95 839 return -EINVAL;
70ceb4f5 840
4bed72e4 841 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
70ceb4f5
YH
842
843 if (rinfo->length == 3)
844 prefix = (struct in6_addr *)rinfo->prefix;
845 else {
846 /* this function is safe */
847 ipv6_addr_prefix(&prefix_buf,
848 (struct in6_addr *)rinfo->prefix,
849 rinfo->prefix_len);
850 prefix = &prefix_buf;
851 }
852
f104a567
DJ
853 if (rinfo->prefix_len == 0)
854 rt = rt6_get_dflt_router(gwaddr, dev);
855 else
856 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
830218c1 857 gwaddr, dev);
70ceb4f5
YH
858
859 if (rt && !lifetime) {
e0a1ad73 860 ip6_del_rt(rt);
70ceb4f5
YH
861 rt = NULL;
862 }
863
864 if (!rt && lifetime)
830218c1
DA
865 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
866 dev, pref);
70ceb4f5
YH
867 else if (rt)
868 rt->rt6i_flags = RTF_ROUTEINFO |
869 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
870
871 if (rt) {
1716a961
G
872 if (!addrconf_finite_timeout(lifetime))
873 rt6_clean_expires(rt);
874 else
875 rt6_set_expires(rt, jiffies + HZ * lifetime);
876
94e187c0 877 ip6_rt_put(rt);
70ceb4f5
YH
878 }
879 return 0;
880}
881#endif
882
a3c00e46
MKL
883static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
884 struct in6_addr *saddr)
885{
66f5d6ce 886 struct fib6_node *pn, *sn;
a3c00e46
MKL
887 while (1) {
888 if (fn->fn_flags & RTN_TL_ROOT)
889 return NULL;
66f5d6ce
WW
890 pn = rcu_dereference(fn->parent);
891 sn = FIB6_SUBTREE(pn);
892 if (sn && sn != fn)
893 fn = fib6_lookup(sn, NULL, saddr);
a3c00e46
MKL
894 else
895 fn = pn;
896 if (fn->fn_flags & RTN_RTINFO)
897 return fn;
898 }
899}
c71099ac 900
d3843fe5
WW
901static bool ip6_hold_safe(struct net *net, struct rt6_info **prt,
902 bool null_fallback)
903{
904 struct rt6_info *rt = *prt;
905
906 if (dst_hold_safe(&rt->dst))
907 return true;
908 if (null_fallback) {
909 rt = net->ipv6.ip6_null_entry;
910 dst_hold(&rt->dst);
911 } else {
912 rt = NULL;
913 }
914 *prt = rt;
915 return false;
916}
917
8ed67789
DL
918static struct rt6_info *ip6_pol_route_lookup(struct net *net,
919 struct fib6_table *table,
4c9483b2 920 struct flowi6 *fl6, int flags)
1da177e4 921{
2b760fcf 922 struct rt6_info *rt, *rt_cache;
1da177e4 923 struct fib6_node *fn;
1da177e4 924
1c4377ae
DA
925 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
926 flags &= ~RT6_LOOKUP_F_IFACE;
927
66f5d6ce 928 rcu_read_lock();
4c9483b2 929 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
c71099ac 930restart:
66f5d6ce
WW
931 rt = rcu_dereference(fn->leaf);
932 if (!rt) {
933 rt = net->ipv6.ip6_null_entry;
934 } else {
935 rt = rt6_device_match(net, rt, &fl6->saddr,
936 fl6->flowi6_oif, flags);
937 if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
938 rt = rt6_multipath_select(rt, fl6,
939 fl6->flowi6_oif, flags);
940 }
a3c00e46
MKL
941 if (rt == net->ipv6.ip6_null_entry) {
942 fn = fib6_backtrack(fn, &fl6->saddr);
943 if (fn)
944 goto restart;
945 }
2b760fcf
WW
946 /* Search through exception table */
947 rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr);
948 if (rt_cache)
949 rt = rt_cache;
950
d3843fe5
WW
951 if (ip6_hold_safe(net, &rt, true))
952 dst_use_noref(&rt->dst, jiffies);
953
66f5d6ce 954 rcu_read_unlock();
b811580d 955
b65f164d 956 trace_fib6_table_lookup(net, rt, table, fl6);
b811580d 957
c71099ac
TG
958 return rt;
959
960}
961
67ba4152 962struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
ea6e574e
FW
963 int flags)
964{
965 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
966}
967EXPORT_SYMBOL_GPL(ip6_route_lookup);
968
9acd9f3a
YH
969struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
970 const struct in6_addr *saddr, int oif, int strict)
c71099ac 971{
4c9483b2
DM
972 struct flowi6 fl6 = {
973 .flowi6_oif = oif,
974 .daddr = *daddr,
c71099ac
TG
975 };
976 struct dst_entry *dst;
77d16f45 977 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
c71099ac 978
adaa70bb 979 if (saddr) {
4c9483b2 980 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
adaa70bb
TG
981 flags |= RT6_LOOKUP_F_HAS_SADDR;
982 }
983
4c9483b2 984 dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
c71099ac
TG
985 if (dst->error == 0)
986 return (struct rt6_info *) dst;
987
988 dst_release(dst);
989
1da177e4
LT
990 return NULL;
991}
7159039a
YH
992EXPORT_SYMBOL(rt6_lookup);
993
c71099ac 994/* ip6_ins_rt is called with FREE table->tb6_lock.
1cfb71ee
WW
995 * It takes new route entry, the addition fails by any reason the
996 * route is released.
997 * Caller must hold dst before calling it.
1da177e4
LT
998 */
999
e5fd387a 1000static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info,
333c4301
DA
1001 struct mx6_config *mxc,
1002 struct netlink_ext_ack *extack)
1da177e4
LT
1003{
1004 int err;
c71099ac 1005 struct fib6_table *table;
1da177e4 1006
c71099ac 1007 table = rt->rt6i_table;
66f5d6ce 1008 spin_lock_bh(&table->tb6_lock);
333c4301 1009 err = fib6_add(&table->tb6_root, rt, info, mxc, extack);
66f5d6ce 1010 spin_unlock_bh(&table->tb6_lock);
1da177e4
LT
1011
1012 return err;
1013}
1014
40e22e8f
TG
1015int ip6_ins_rt(struct rt6_info *rt)
1016{
e715b6d3
FW
1017 struct nl_info info = { .nl_net = dev_net(rt->dst.dev), };
1018 struct mx6_config mxc = { .mx = NULL, };
1019
1cfb71ee
WW
1020 /* Hold dst to account for the reference from the fib6 tree */
1021 dst_hold(&rt->dst);
333c4301 1022 return __ip6_ins_rt(rt, &info, &mxc, NULL);
40e22e8f
TG
1023}
1024
4832c30d
DA
1025/* called with rcu_lock held */
1026static struct net_device *ip6_rt_get_dev_rcu(struct rt6_info *rt)
1027{
1028 struct net_device *dev = rt->dst.dev;
1029
98d11291 1030 if (rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) {
4832c30d
DA
1031 /* for copies of local routes, dst->dev needs to be the
1032 * device if it is a master device, the master device if
1033 * device is enslaved, and the loopback as the default
1034 */
1035 if (netif_is_l3_slave(dev) &&
1036 !rt6_need_strict(&rt->rt6i_dst.addr))
1037 dev = l3mdev_master_dev_rcu(dev);
1038 else if (!netif_is_l3_master(dev))
1039 dev = dev_net(dev)->loopback_dev;
1040 /* last case is netif_is_l3_master(dev) is true in which
1041 * case we want dev returned to be dev
1042 */
1043 }
1044
1045 return dev;
1046}
1047
8b9df265
MKL
1048static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort,
1049 const struct in6_addr *daddr,
1050 const struct in6_addr *saddr)
1da177e4 1051{
4832c30d 1052 struct net_device *dev;
1da177e4
LT
1053 struct rt6_info *rt;
1054
1055 /*
1056 * Clone the route.
1057 */
1058
d52d3997 1059 if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU))
83a09abd 1060 ort = (struct rt6_info *)ort->dst.from;
1da177e4 1061
4832c30d
DA
1062 rcu_read_lock();
1063 dev = ip6_rt_get_dev_rcu(ort);
1064 rt = __ip6_dst_alloc(dev_net(dev), dev, 0);
1065 rcu_read_unlock();
83a09abd
MKL
1066 if (!rt)
1067 return NULL;
1068
1069 ip6_rt_copy_init(rt, ort);
1070 rt->rt6i_flags |= RTF_CACHE;
1071 rt->rt6i_metric = 0;
1072 rt->dst.flags |= DST_HOST;
1073 rt->rt6i_dst.addr = *daddr;
1074 rt->rt6i_dst.plen = 128;
1da177e4 1075
83a09abd
MKL
1076 if (!rt6_is_gw_or_nonexthop(ort)) {
1077 if (ort->rt6i_dst.plen != 128 &&
1078 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
1079 rt->rt6i_flags |= RTF_ANYCAST;
1da177e4 1080#ifdef CONFIG_IPV6_SUBTREES
83a09abd
MKL
1081 if (rt->rt6i_src.plen && saddr) {
1082 rt->rt6i_src.addr = *saddr;
1083 rt->rt6i_src.plen = 128;
8b9df265 1084 }
83a09abd 1085#endif
95a9a5ba 1086 }
1da177e4 1087
95a9a5ba
YH
1088 return rt;
1089}
1da177e4 1090
d52d3997
MKL
1091static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt)
1092{
4832c30d 1093 struct net_device *dev;
d52d3997
MKL
1094 struct rt6_info *pcpu_rt;
1095
4832c30d
DA
1096 rcu_read_lock();
1097 dev = ip6_rt_get_dev_rcu(rt);
1098 pcpu_rt = __ip6_dst_alloc(dev_net(dev), dev, rt->dst.flags);
1099 rcu_read_unlock();
d52d3997
MKL
1100 if (!pcpu_rt)
1101 return NULL;
1102 ip6_rt_copy_init(pcpu_rt, rt);
1103 pcpu_rt->rt6i_protocol = rt->rt6i_protocol;
1104 pcpu_rt->rt6i_flags |= RTF_PCPU;
1105 return pcpu_rt;
1106}
1107
66f5d6ce 1108/* It should be called with rcu_read_lock() acquired */
d52d3997
MKL
1109static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt)
1110{
a73e4195 1111 struct rt6_info *pcpu_rt, **p;
d52d3997
MKL
1112
1113 p = this_cpu_ptr(rt->rt6i_pcpu);
1114 pcpu_rt = *p;
1115
d3843fe5 1116 if (pcpu_rt && ip6_hold_safe(NULL, &pcpu_rt, false))
a73e4195 1117 rt6_dst_from_metrics_check(pcpu_rt);
d3843fe5 1118
a73e4195
MKL
1119 return pcpu_rt;
1120}
1121
1122static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt)
1123{
1124 struct rt6_info *pcpu_rt, *prev, **p;
d52d3997
MKL
1125
1126 pcpu_rt = ip6_rt_pcpu_alloc(rt);
1127 if (!pcpu_rt) {
1128 struct net *net = dev_net(rt->dst.dev);
1129
9c7370a1
MKL
1130 dst_hold(&net->ipv6.ip6_null_entry->dst);
1131 return net->ipv6.ip6_null_entry;
d52d3997
MKL
1132 }
1133
a94b9367
WW
1134 dst_hold(&pcpu_rt->dst);
1135 p = this_cpu_ptr(rt->rt6i_pcpu);
1136 prev = cmpxchg(p, NULL, pcpu_rt);
951f788a 1137 BUG_ON(prev);
a94b9367 1138
d52d3997
MKL
1139 rt6_dst_from_metrics_check(pcpu_rt);
1140 return pcpu_rt;
1141}
1142
35732d01
WW
1143/* exception hash table implementation
1144 */
1145static DEFINE_SPINLOCK(rt6_exception_lock);
1146
1147/* Remove rt6_ex from hash table and free the memory
1148 * Caller must hold rt6_exception_lock
1149 */
1150static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1151 struct rt6_exception *rt6_ex)
1152{
b2427e67 1153 struct net *net;
81eb8447 1154
35732d01
WW
1155 if (!bucket || !rt6_ex)
1156 return;
b2427e67
CIK
1157
1158 net = dev_net(rt6_ex->rt6i->dst.dev);
35732d01
WW
1159 rt6_ex->rt6i->rt6i_node = NULL;
1160 hlist_del_rcu(&rt6_ex->hlist);
1161 rt6_release(rt6_ex->rt6i);
1162 kfree_rcu(rt6_ex, rcu);
1163 WARN_ON_ONCE(!bucket->depth);
1164 bucket->depth--;
81eb8447 1165 net->ipv6.rt6_stats->fib_rt_cache--;
35732d01
WW
1166}
1167
1168/* Remove oldest rt6_ex in bucket and free the memory
1169 * Caller must hold rt6_exception_lock
1170 */
1171static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1172{
1173 struct rt6_exception *rt6_ex, *oldest = NULL;
1174
1175 if (!bucket)
1176 return;
1177
1178 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1179 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1180 oldest = rt6_ex;
1181 }
1182 rt6_remove_exception(bucket, oldest);
1183}
1184
1185static u32 rt6_exception_hash(const struct in6_addr *dst,
1186 const struct in6_addr *src)
1187{
1188 static u32 seed __read_mostly;
1189 u32 val;
1190
1191 net_get_random_once(&seed, sizeof(seed));
1192 val = jhash(dst, sizeof(*dst), seed);
1193
1194#ifdef CONFIG_IPV6_SUBTREES
1195 if (src)
1196 val = jhash(src, sizeof(*src), val);
1197#endif
1198 return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1199}
1200
1201/* Helper function to find the cached rt in the hash table
1202 * and update bucket pointer to point to the bucket for this
1203 * (daddr, saddr) pair
1204 * Caller must hold rt6_exception_lock
1205 */
1206static struct rt6_exception *
1207__rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1208 const struct in6_addr *daddr,
1209 const struct in6_addr *saddr)
1210{
1211 struct rt6_exception *rt6_ex;
1212 u32 hval;
1213
1214 if (!(*bucket) || !daddr)
1215 return NULL;
1216
1217 hval = rt6_exception_hash(daddr, saddr);
1218 *bucket += hval;
1219
1220 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1221 struct rt6_info *rt6 = rt6_ex->rt6i;
1222 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1223
1224#ifdef CONFIG_IPV6_SUBTREES
1225 if (matched && saddr)
1226 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1227#endif
1228 if (matched)
1229 return rt6_ex;
1230 }
1231 return NULL;
1232}
1233
1234/* Helper function to find the cached rt in the hash table
1235 * and update bucket pointer to point to the bucket for this
1236 * (daddr, saddr) pair
1237 * Caller must hold rcu_read_lock()
1238 */
1239static struct rt6_exception *
1240__rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1241 const struct in6_addr *daddr,
1242 const struct in6_addr *saddr)
1243{
1244 struct rt6_exception *rt6_ex;
1245 u32 hval;
1246
1247 WARN_ON_ONCE(!rcu_read_lock_held());
1248
1249 if (!(*bucket) || !daddr)
1250 return NULL;
1251
1252 hval = rt6_exception_hash(daddr, saddr);
1253 *bucket += hval;
1254
1255 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1256 struct rt6_info *rt6 = rt6_ex->rt6i;
1257 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1258
1259#ifdef CONFIG_IPV6_SUBTREES
1260 if (matched && saddr)
1261 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1262#endif
1263 if (matched)
1264 return rt6_ex;
1265 }
1266 return NULL;
1267}
1268
1269static int rt6_insert_exception(struct rt6_info *nrt,
1270 struct rt6_info *ort)
1271{
81eb8447 1272 struct net *net = dev_net(ort->dst.dev);
35732d01
WW
1273 struct rt6_exception_bucket *bucket;
1274 struct in6_addr *src_key = NULL;
1275 struct rt6_exception *rt6_ex;
1276 int err = 0;
1277
1278 /* ort can't be a cache or pcpu route */
1279 if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU))
1280 ort = (struct rt6_info *)ort->dst.from;
1281 WARN_ON_ONCE(ort->rt6i_flags & (RTF_CACHE | RTF_PCPU));
1282
1283 spin_lock_bh(&rt6_exception_lock);
1284
1285 if (ort->exception_bucket_flushed) {
1286 err = -EINVAL;
1287 goto out;
1288 }
1289
1290 bucket = rcu_dereference_protected(ort->rt6i_exception_bucket,
1291 lockdep_is_held(&rt6_exception_lock));
1292 if (!bucket) {
1293 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1294 GFP_ATOMIC);
1295 if (!bucket) {
1296 err = -ENOMEM;
1297 goto out;
1298 }
1299 rcu_assign_pointer(ort->rt6i_exception_bucket, bucket);
1300 }
1301
1302#ifdef CONFIG_IPV6_SUBTREES
1303 /* rt6i_src.plen != 0 indicates ort is in subtree
1304 * and exception table is indexed by a hash of
1305 * both rt6i_dst and rt6i_src.
1306 * Otherwise, the exception table is indexed by
1307 * a hash of only rt6i_dst.
1308 */
1309 if (ort->rt6i_src.plen)
1310 src_key = &nrt->rt6i_src.addr;
1311#endif
60006a48
WW
1312
1313 /* Update rt6i_prefsrc as it could be changed
1314 * in rt6_remove_prefsrc()
1315 */
1316 nrt->rt6i_prefsrc = ort->rt6i_prefsrc;
f5bbe7ee
WW
1317 /* rt6_mtu_change() might lower mtu on ort.
1318 * Only insert this exception route if its mtu
1319 * is less than ort's mtu value.
1320 */
1321 if (nrt->rt6i_pmtu >= dst_mtu(&ort->dst)) {
1322 err = -EINVAL;
1323 goto out;
1324 }
60006a48 1325
35732d01
WW
1326 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1327 src_key);
1328 if (rt6_ex)
1329 rt6_remove_exception(bucket, rt6_ex);
1330
1331 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1332 if (!rt6_ex) {
1333 err = -ENOMEM;
1334 goto out;
1335 }
1336 rt6_ex->rt6i = nrt;
1337 rt6_ex->stamp = jiffies;
1338 atomic_inc(&nrt->rt6i_ref);
1339 nrt->rt6i_node = ort->rt6i_node;
1340 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1341 bucket->depth++;
81eb8447 1342 net->ipv6.rt6_stats->fib_rt_cache++;
35732d01
WW
1343
1344 if (bucket->depth > FIB6_MAX_DEPTH)
1345 rt6_exception_remove_oldest(bucket);
1346
1347out:
1348 spin_unlock_bh(&rt6_exception_lock);
1349
1350 /* Update fn->fn_sernum to invalidate all cached dst */
b886d5f2 1351 if (!err) {
35732d01 1352 fib6_update_sernum(ort);
b886d5f2
PA
1353 fib6_force_start_gc(net);
1354 }
35732d01
WW
1355
1356 return err;
1357}
1358
1359void rt6_flush_exceptions(struct rt6_info *rt)
1360{
1361 struct rt6_exception_bucket *bucket;
1362 struct rt6_exception *rt6_ex;
1363 struct hlist_node *tmp;
1364 int i;
1365
1366 spin_lock_bh(&rt6_exception_lock);
1367 /* Prevent rt6_insert_exception() to recreate the bucket list */
1368 rt->exception_bucket_flushed = 1;
1369
1370 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1371 lockdep_is_held(&rt6_exception_lock));
1372 if (!bucket)
1373 goto out;
1374
1375 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1376 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
1377 rt6_remove_exception(bucket, rt6_ex);
1378 WARN_ON_ONCE(bucket->depth);
1379 bucket++;
1380 }
1381
1382out:
1383 spin_unlock_bh(&rt6_exception_lock);
1384}
1385
1386/* Find cached rt in the hash table inside passed in rt
1387 * Caller has to hold rcu_read_lock()
1388 */
1389static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt,
1390 struct in6_addr *daddr,
1391 struct in6_addr *saddr)
1392{
1393 struct rt6_exception_bucket *bucket;
1394 struct in6_addr *src_key = NULL;
1395 struct rt6_exception *rt6_ex;
1396 struct rt6_info *res = NULL;
1397
1398 bucket = rcu_dereference(rt->rt6i_exception_bucket);
1399
1400#ifdef CONFIG_IPV6_SUBTREES
1401 /* rt6i_src.plen != 0 indicates rt is in subtree
1402 * and exception table is indexed by a hash of
1403 * both rt6i_dst and rt6i_src.
1404 * Otherwise, the exception table is indexed by
1405 * a hash of only rt6i_dst.
1406 */
1407 if (rt->rt6i_src.plen)
1408 src_key = saddr;
1409#endif
1410 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1411
1412 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
1413 res = rt6_ex->rt6i;
1414
1415 return res;
1416}
1417
1418/* Remove the passed in cached rt from the hash table that contains it */
1419int rt6_remove_exception_rt(struct rt6_info *rt)
1420{
1421 struct rt6_info *from = (struct rt6_info *)rt->dst.from;
1422 struct rt6_exception_bucket *bucket;
1423 struct in6_addr *src_key = NULL;
1424 struct rt6_exception *rt6_ex;
1425 int err;
1426
1427 if (!from ||
442d713b 1428 !(rt->rt6i_flags & RTF_CACHE))
35732d01
WW
1429 return -EINVAL;
1430
1431 if (!rcu_access_pointer(from->rt6i_exception_bucket))
1432 return -ENOENT;
1433
1434 spin_lock_bh(&rt6_exception_lock);
1435 bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
1436 lockdep_is_held(&rt6_exception_lock));
1437#ifdef CONFIG_IPV6_SUBTREES
1438 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1439 * and exception table is indexed by a hash of
1440 * both rt6i_dst and rt6i_src.
1441 * Otherwise, the exception table is indexed by
1442 * a hash of only rt6i_dst.
1443 */
1444 if (from->rt6i_src.plen)
1445 src_key = &rt->rt6i_src.addr;
1446#endif
1447 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1448 &rt->rt6i_dst.addr,
1449 src_key);
1450 if (rt6_ex) {
1451 rt6_remove_exception(bucket, rt6_ex);
1452 err = 0;
1453 } else {
1454 err = -ENOENT;
1455 }
1456
1457 spin_unlock_bh(&rt6_exception_lock);
1458 return err;
1459}
1460
1461/* Find rt6_ex which contains the passed in rt cache and
1462 * refresh its stamp
1463 */
1464static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1465{
35732d01
WW
1466 struct rt6_exception_bucket *bucket;
1467 struct in6_addr *src_key = NULL;
1468 struct rt6_exception *rt6_ex;
01361885 1469 struct rt6_info *from;
35732d01
WW
1470
1471 rcu_read_lock();
01361885
PA
1472 from = (struct rt6_info *)rcu_dereference(rt->dst.from);
1473 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1474 goto unlock;
1475
35732d01
WW
1476 bucket = rcu_dereference(from->rt6i_exception_bucket);
1477
1478#ifdef CONFIG_IPV6_SUBTREES
1479 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1480 * and exception table is indexed by a hash of
1481 * both rt6i_dst and rt6i_src.
1482 * Otherwise, the exception table is indexed by
1483 * a hash of only rt6i_dst.
1484 */
1485 if (from->rt6i_src.plen)
1486 src_key = &rt->rt6i_src.addr;
1487#endif
1488 rt6_ex = __rt6_find_exception_rcu(&bucket,
1489 &rt->rt6i_dst.addr,
1490 src_key);
1491 if (rt6_ex)
1492 rt6_ex->stamp = jiffies;
1493
01361885 1494unlock:
35732d01
WW
1495 rcu_read_unlock();
1496}
1497
60006a48
WW
1498static void rt6_exceptions_remove_prefsrc(struct rt6_info *rt)
1499{
1500 struct rt6_exception_bucket *bucket;
1501 struct rt6_exception *rt6_ex;
1502 int i;
1503
1504 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1505 lockdep_is_held(&rt6_exception_lock));
1506
1507 if (bucket) {
1508 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1509 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1510 rt6_ex->rt6i->rt6i_prefsrc.plen = 0;
1511 }
1512 bucket++;
1513 }
1514 }
1515}
1516
8f523861
SB
1517static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1518 struct rt6_info *rt, int mtu)
1519{
1520 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1521 * lowest MTU in the path: always allow updating the route PMTU to
1522 * reflect PMTU decreases.
1523 *
1524 * If the new MTU is higher, and the route PMTU is equal to the local
1525 * MTU, this means the old MTU is the lowest in the path, so allow
1526 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1527 * handle this.
1528 */
1529
1530 if (dst_mtu(&rt->dst) >= mtu)
1531 return true;
1532
1533 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1534 return true;
1535
1536 return false;
1537}
1538
1539static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
1540 struct rt6_info *rt, int mtu)
f5bbe7ee
WW
1541{
1542 struct rt6_exception_bucket *bucket;
1543 struct rt6_exception *rt6_ex;
1544 int i;
1545
1546 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1547 lockdep_is_held(&rt6_exception_lock));
1548
8f523861
SB
1549 if (!bucket)
1550 return;
1551
1552 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1553 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1554 struct rt6_info *entry = rt6_ex->rt6i;
1555
1556 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
1557 * route), the metrics of its rt->dst.from have already
1558 * been updated.
1559 */
1560 if (entry->rt6i_pmtu &&
1561 rt6_mtu_change_route_allowed(idev, entry, mtu))
1562 entry->rt6i_pmtu = mtu;
f5bbe7ee 1563 }
8f523861 1564 bucket++;
f5bbe7ee
WW
1565 }
1566}
1567
b16cb459
WW
1568#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
1569
1570static void rt6_exceptions_clean_tohost(struct rt6_info *rt,
1571 struct in6_addr *gateway)
1572{
1573 struct rt6_exception_bucket *bucket;
1574 struct rt6_exception *rt6_ex;
1575 struct hlist_node *tmp;
1576 int i;
1577
1578 if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1579 return;
1580
1581 spin_lock_bh(&rt6_exception_lock);
1582 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1583 lockdep_is_held(&rt6_exception_lock));
1584
1585 if (bucket) {
1586 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1587 hlist_for_each_entry_safe(rt6_ex, tmp,
1588 &bucket->chain, hlist) {
1589 struct rt6_info *entry = rt6_ex->rt6i;
1590
1591 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1592 RTF_CACHE_GATEWAY &&
1593 ipv6_addr_equal(gateway,
1594 &entry->rt6i_gateway)) {
1595 rt6_remove_exception(bucket, rt6_ex);
1596 }
1597 }
1598 bucket++;
1599 }
1600 }
1601
1602 spin_unlock_bh(&rt6_exception_lock);
1603}
1604
c757faa8
WW
1605static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1606 struct rt6_exception *rt6_ex,
1607 struct fib6_gc_args *gc_args,
1608 unsigned long now)
1609{
1610 struct rt6_info *rt = rt6_ex->rt6i;
1611
1859bac0
PA
1612 /* we are pruning and obsoleting aged-out and non gateway exceptions
1613 * even if others have still references to them, so that on next
1614 * dst_check() such references can be dropped.
1615 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
1616 * expired, independently from their aging, as per RFC 8201 section 4
1617 */
afe95ad6
WW
1618 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
1619 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1620 RT6_TRACE("aging clone %p\n", rt);
1621 rt6_remove_exception(bucket, rt6_ex);
1622 return;
1623 }
1624 } else if (time_after(jiffies, rt->dst.expires)) {
1625 RT6_TRACE("purging expired route %p\n", rt);
c757faa8
WW
1626 rt6_remove_exception(bucket, rt6_ex);
1627 return;
afe95ad6
WW
1628 }
1629
1630 if (rt->rt6i_flags & RTF_GATEWAY) {
c757faa8
WW
1631 struct neighbour *neigh;
1632 __u8 neigh_flags = 0;
1633
9006a004
ED
1634 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
1635 if (neigh)
c757faa8 1636 neigh_flags = neigh->flags;
9006a004 1637
c757faa8
WW
1638 if (!(neigh_flags & NTF_ROUTER)) {
1639 RT6_TRACE("purging route %p via non-router but gateway\n",
1640 rt);
1641 rt6_remove_exception(bucket, rt6_ex);
1642 return;
1643 }
1644 }
afe95ad6 1645
c757faa8
WW
1646 gc_args->more++;
1647}
1648
1649void rt6_age_exceptions(struct rt6_info *rt,
1650 struct fib6_gc_args *gc_args,
1651 unsigned long now)
1652{
1653 struct rt6_exception_bucket *bucket;
1654 struct rt6_exception *rt6_ex;
1655 struct hlist_node *tmp;
1656 int i;
1657
1658 if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1659 return;
1660
9006a004
ED
1661 rcu_read_lock_bh();
1662 spin_lock(&rt6_exception_lock);
c757faa8
WW
1663 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1664 lockdep_is_held(&rt6_exception_lock));
1665
1666 if (bucket) {
1667 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1668 hlist_for_each_entry_safe(rt6_ex, tmp,
1669 &bucket->chain, hlist) {
1670 rt6_age_examine_exception(bucket, rt6_ex,
1671 gc_args, now);
1672 }
1673 bucket++;
1674 }
1675 }
9006a004
ED
1676 spin_unlock(&rt6_exception_lock);
1677 rcu_read_unlock_bh();
c757faa8
WW
1678}
1679
9ff74384
DA
1680struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
1681 int oif, struct flowi6 *fl6, int flags)
1da177e4 1682{
367efcb9 1683 struct fib6_node *fn, *saved_fn;
2b760fcf 1684 struct rt6_info *rt, *rt_cache;
c71099ac 1685 int strict = 0;
1da177e4 1686
77d16f45 1687 strict |= flags & RT6_LOOKUP_F_IFACE;
d5d32e4b 1688 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
367efcb9
MKL
1689 if (net->ipv6.devconf_all->forwarding == 0)
1690 strict |= RT6_LOOKUP_F_REACHABLE;
1da177e4 1691
66f5d6ce 1692 rcu_read_lock();
1da177e4 1693
4c9483b2 1694 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
367efcb9 1695 saved_fn = fn;
1da177e4 1696
ca254490
DA
1697 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1698 oif = 0;
1699
a3c00e46 1700redo_rt6_select:
8d1040e8 1701 rt = rt6_select(net, fn, oif, strict);
52bd4c0c 1702 if (rt->rt6i_nsiblings)
367efcb9 1703 rt = rt6_multipath_select(rt, fl6, oif, strict);
a3c00e46
MKL
1704 if (rt == net->ipv6.ip6_null_entry) {
1705 fn = fib6_backtrack(fn, &fl6->saddr);
1706 if (fn)
1707 goto redo_rt6_select;
367efcb9
MKL
1708 else if (strict & RT6_LOOKUP_F_REACHABLE) {
1709 /* also consider unreachable route */
1710 strict &= ~RT6_LOOKUP_F_REACHABLE;
1711 fn = saved_fn;
1712 goto redo_rt6_select;
367efcb9 1713 }
a3c00e46
MKL
1714 }
1715
2b760fcf
WW
1716 /*Search through exception table */
1717 rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr);
1718 if (rt_cache)
1719 rt = rt_cache;
fb9de91e 1720
d3843fe5 1721 if (rt == net->ipv6.ip6_null_entry) {
66f5d6ce 1722 rcu_read_unlock();
d3843fe5 1723 dst_hold(&rt->dst);
b65f164d 1724 trace_fib6_table_lookup(net, rt, table, fl6);
d3843fe5
WW
1725 return rt;
1726 } else if (rt->rt6i_flags & RTF_CACHE) {
1727 if (ip6_hold_safe(net, &rt, true)) {
1728 dst_use_noref(&rt->dst, jiffies);
1729 rt6_dst_from_metrics_check(rt);
1730 }
66f5d6ce 1731 rcu_read_unlock();
b65f164d 1732 trace_fib6_table_lookup(net, rt, table, fl6);
d52d3997 1733 return rt;
3da59bd9
MKL
1734 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
1735 !(rt->rt6i_flags & RTF_GATEWAY))) {
1736 /* Create a RTF_CACHE clone which will not be
1737 * owned by the fib6 tree. It is for the special case where
1738 * the daddr in the skb during the neighbor look-up is different
1739 * from the fl6->daddr used to look-up route here.
1740 */
1741
1742 struct rt6_info *uncached_rt;
1743
d3843fe5
WW
1744 if (ip6_hold_safe(net, &rt, true)) {
1745 dst_use_noref(&rt->dst, jiffies);
1746 } else {
66f5d6ce 1747 rcu_read_unlock();
d3843fe5
WW
1748 uncached_rt = rt;
1749 goto uncached_rt_out;
1750 }
66f5d6ce 1751 rcu_read_unlock();
d52d3997 1752
3da59bd9
MKL
1753 uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL);
1754 dst_release(&rt->dst);
c71099ac 1755
1cfb71ee
WW
1756 if (uncached_rt) {
1757 /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
1758 * No need for another dst_hold()
1759 */
8d0b94af 1760 rt6_uncached_list_add(uncached_rt);
81eb8447 1761 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
1cfb71ee 1762 } else {
3da59bd9 1763 uncached_rt = net->ipv6.ip6_null_entry;
1cfb71ee
WW
1764 dst_hold(&uncached_rt->dst);
1765 }
b811580d 1766
d3843fe5 1767uncached_rt_out:
b65f164d 1768 trace_fib6_table_lookup(net, uncached_rt, table, fl6);
3da59bd9 1769 return uncached_rt;
3da59bd9 1770
d52d3997
MKL
1771 } else {
1772 /* Get a percpu copy */
1773
1774 struct rt6_info *pcpu_rt;
1775
d3843fe5 1776 dst_use_noref(&rt->dst, jiffies);
951f788a 1777 local_bh_disable();
d52d3997 1778 pcpu_rt = rt6_get_pcpu_route(rt);
d52d3997 1779
951f788a 1780 if (!pcpu_rt) {
a94b9367
WW
1781 /* atomic_inc_not_zero() is needed when using rcu */
1782 if (atomic_inc_not_zero(&rt->rt6i_ref)) {
951f788a 1783 /* No dst_hold() on rt is needed because grabbing
a94b9367
WW
1784 * rt->rt6i_ref makes sure rt can't be released.
1785 */
a94b9367
WW
1786 pcpu_rt = rt6_make_pcpu_route(rt);
1787 rt6_release(rt);
1788 } else {
1789 /* rt is already removed from tree */
a94b9367
WW
1790 pcpu_rt = net->ipv6.ip6_null_entry;
1791 dst_hold(&pcpu_rt->dst);
1792 }
9c7370a1 1793 }
951f788a
ED
1794 local_bh_enable();
1795 rcu_read_unlock();
b65f164d 1796 trace_fib6_table_lookup(net, pcpu_rt, table, fl6);
d52d3997
MKL
1797 return pcpu_rt;
1798 }
1da177e4 1799}
9ff74384 1800EXPORT_SYMBOL_GPL(ip6_pol_route);
1da177e4 1801
8ed67789 1802static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
4c9483b2 1803 struct flowi6 *fl6, int flags)
4acad72d 1804{
4c9483b2 1805 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
4acad72d
PE
1806}
1807
d409b847
MB
1808struct dst_entry *ip6_route_input_lookup(struct net *net,
1809 struct net_device *dev,
1810 struct flowi6 *fl6, int flags)
72331bc0
SL
1811{
1812 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
1813 flags |= RT6_LOOKUP_F_IFACE;
1814
1815 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
1816}
d409b847 1817EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
72331bc0 1818
23aebdac
JS
1819static void ip6_multipath_l3_keys(const struct sk_buff *skb,
1820 struct flow_keys *keys)
1821{
1822 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
1823 const struct ipv6hdr *key_iph = outer_iph;
1824 const struct ipv6hdr *inner_iph;
1825 const struct icmp6hdr *icmph;
1826 struct ipv6hdr _inner_iph;
9c3bf900 1827 struct icmp6hdr _icmph;
23aebdac
JS
1828
1829 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
1830 goto out;
1831
9c3bf900
ED
1832 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
1833 sizeof(_icmph), &_icmph);
1834 if (!icmph)
1835 goto out;
1836
23aebdac
JS
1837 if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
1838 icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
1839 icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
1840 icmph->icmp6_type != ICMPV6_PARAMPROB)
1841 goto out;
1842
1843 inner_iph = skb_header_pointer(skb,
1844 skb_transport_offset(skb) + sizeof(*icmph),
1845 sizeof(_inner_iph), &_inner_iph);
1846 if (!inner_iph)
1847 goto out;
1848
1849 key_iph = inner_iph;
1850out:
1851 memset(keys, 0, sizeof(*keys));
1852 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1853 keys->addrs.v6addrs.src = key_iph->saddr;
1854 keys->addrs.v6addrs.dst = key_iph->daddr;
4081f0dc 1855 keys->tags.flow_label = ip6_flowlabel(key_iph);
23aebdac
JS
1856 keys->basic.ip_proto = key_iph->nexthdr;
1857}
1858
1859/* if skb is set it will be used and fl6 can be NULL */
1860u32 rt6_multipath_hash(const struct flowi6 *fl6, const struct sk_buff *skb)
1861{
1862 struct flow_keys hash_keys;
1863
1864 if (skb) {
1865 ip6_multipath_l3_keys(skb, &hash_keys);
1866 return flow_hash_from_keys(&hash_keys);
1867 }
1868
1869 return get_hash_from_flowi6(fl6);
1870}
1871
c71099ac
TG
1872void ip6_route_input(struct sk_buff *skb)
1873{
b71d1d42 1874 const struct ipv6hdr *iph = ipv6_hdr(skb);
c346dca1 1875 struct net *net = dev_net(skb->dev);
adaa70bb 1876 int flags = RT6_LOOKUP_F_HAS_SADDR;
904af04d 1877 struct ip_tunnel_info *tun_info;
4c9483b2 1878 struct flowi6 fl6 = {
e0d56fdd 1879 .flowi6_iif = skb->dev->ifindex,
4c9483b2
DM
1880 .daddr = iph->daddr,
1881 .saddr = iph->saddr,
6502ca52 1882 .flowlabel = ip6_flowinfo(iph),
4c9483b2
DM
1883 .flowi6_mark = skb->mark,
1884 .flowi6_proto = iph->nexthdr,
c71099ac 1885 };
adaa70bb 1886
904af04d 1887 tun_info = skb_tunnel_info(skb);
46fa062a 1888 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
904af04d 1889 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
23aebdac
JS
1890 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
1891 fl6.mp_hash = rt6_multipath_hash(&fl6, skb);
06e9d040 1892 skb_dst_drop(skb);
72331bc0 1893 skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
c71099ac
TG
1894}
1895
8ed67789 1896static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
4c9483b2 1897 struct flowi6 *fl6, int flags)
1da177e4 1898{
4c9483b2 1899 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
c71099ac
TG
1900}
1901
6f21c96a
PA
1902struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
1903 struct flowi6 *fl6, int flags)
c71099ac 1904{
d46a9d67 1905 bool any_src;
c71099ac 1906
4c1feac5
DA
1907 if (rt6_need_strict(&fl6->daddr)) {
1908 struct dst_entry *dst;
1909
1910 dst = l3mdev_link_scope_lookup(net, fl6);
1911 if (dst)
1912 return dst;
1913 }
ca254490 1914
1fb9489b 1915 fl6->flowi6_iif = LOOPBACK_IFINDEX;
4dc27d1c 1916
d46a9d67 1917 any_src = ipv6_addr_any(&fl6->saddr);
741a11d9 1918 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
d46a9d67 1919 (fl6->flowi6_oif && any_src))
77d16f45 1920 flags |= RT6_LOOKUP_F_IFACE;
c71099ac 1921
d46a9d67 1922 if (!any_src)
adaa70bb 1923 flags |= RT6_LOOKUP_F_HAS_SADDR;
0c9a2ac1
YH
1924 else if (sk)
1925 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
adaa70bb 1926
4c9483b2 1927 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
1da177e4 1928}
6f21c96a 1929EXPORT_SYMBOL_GPL(ip6_route_output_flags);
1da177e4 1930
2774c131 1931struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
14e50e57 1932{
5c1e6aa3 1933 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
1dbe3252 1934 struct net_device *loopback_dev = net->loopback_dev;
14e50e57
DM
1935 struct dst_entry *new = NULL;
1936
1dbe3252 1937 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
62cf27e5 1938 DST_OBSOLETE_DEAD, 0);
14e50e57 1939 if (rt) {
0a1f5962 1940 rt6_info_init(rt);
81eb8447 1941 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
8104891b 1942
0a1f5962 1943 new = &rt->dst;
14e50e57 1944 new->__use = 1;
352e512c 1945 new->input = dst_discard;
ede2059d 1946 new->output = dst_discard_out;
14e50e57 1947
0a1f5962 1948 dst_copy_metrics(new, &ort->dst);
14e50e57 1949
1dbe3252 1950 rt->rt6i_idev = in6_dev_get(loopback_dev);
4e3fd7a0 1951 rt->rt6i_gateway = ort->rt6i_gateway;
0a1f5962 1952 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
14e50e57
DM
1953 rt->rt6i_metric = 0;
1954
1955 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1956#ifdef CONFIG_IPV6_SUBTREES
1957 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1958#endif
14e50e57
DM
1959 }
1960
69ead7af
DM
1961 dst_release(dst_orig);
1962 return new ? new : ERR_PTR(-ENOMEM);
14e50e57 1963}
14e50e57 1964
1da177e4
LT
1965/*
1966 * Destination cache support functions
1967 */
1968
4b32b5ad
MKL
1969static void rt6_dst_from_metrics_check(struct rt6_info *rt)
1970{
1971 if (rt->dst.from &&
1972 dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from))
1973 dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true);
1974}
1975
3da59bd9
MKL
1976static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie)
1977{
36143645 1978 u32 rt_cookie = 0;
c5cff856
WW
1979
1980 if (!rt6_get_cookie_safe(rt, &rt_cookie) || rt_cookie != cookie)
3da59bd9
MKL
1981 return NULL;
1982
1983 if (rt6_check_expired(rt))
1984 return NULL;
1985
1986 return &rt->dst;
1987}
1988
1989static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie)
1990{
5973fb1e
MKL
1991 if (!__rt6_check_expired(rt) &&
1992 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
3da59bd9
MKL
1993 rt6_check((struct rt6_info *)(rt->dst.from), cookie))
1994 return &rt->dst;
1995 else
1996 return NULL;
1997}
1998
1da177e4
LT
1999static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2000{
2001 struct rt6_info *rt;
2002
2003 rt = (struct rt6_info *) dst;
2004
6f3118b5
ND
2005 /* All IPV6 dsts are created with ->obsolete set to the value
2006 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2007 * into this function always.
2008 */
e3bc10bd 2009
4b32b5ad
MKL
2010 rt6_dst_from_metrics_check(rt);
2011
02bcf4e0 2012 if (rt->rt6i_flags & RTF_PCPU ||
a4c2fd7f 2013 (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->dst.from))
3da59bd9
MKL
2014 return rt6_dst_from_check(rt, cookie);
2015 else
2016 return rt6_check(rt, cookie);
1da177e4
LT
2017}
2018
2019static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2020{
2021 struct rt6_info *rt = (struct rt6_info *) dst;
2022
2023 if (rt) {
54c1a859
YH
2024 if (rt->rt6i_flags & RTF_CACHE) {
2025 if (rt6_check_expired(rt)) {
2026 ip6_del_rt(rt);
2027 dst = NULL;
2028 }
2029 } else {
1da177e4 2030 dst_release(dst);
54c1a859
YH
2031 dst = NULL;
2032 }
1da177e4 2033 }
54c1a859 2034 return dst;
1da177e4
LT
2035}
2036
2037static void ip6_link_failure(struct sk_buff *skb)
2038{
2039 struct rt6_info *rt;
2040
3ffe533c 2041 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
1da177e4 2042
adf30907 2043 rt = (struct rt6_info *) skb_dst(skb);
1da177e4 2044 if (rt) {
1eb4f758 2045 if (rt->rt6i_flags & RTF_CACHE) {
ad65a2f0
WW
2046 if (dst_hold_safe(&rt->dst))
2047 ip6_del_rt(rt);
c5cff856
WW
2048 } else {
2049 struct fib6_node *fn;
2050
2051 rcu_read_lock();
2052 fn = rcu_dereference(rt->rt6i_node);
2053 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2054 fn->fn_sernum = -1;
2055 rcu_read_unlock();
1eb4f758 2056 }
1da177e4
LT
2057 }
2058}
2059
45e4fd26
MKL
2060static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2061{
2062 struct net *net = dev_net(rt->dst.dev);
2063
2064 rt->rt6i_flags |= RTF_MODIFIED;
2065 rt->rt6i_pmtu = mtu;
2066 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2067}
2068
0d3f6d29
MKL
2069static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2070{
2071 return !(rt->rt6i_flags & RTF_CACHE) &&
4e587ea7
WW
2072 (rt->rt6i_flags & RTF_PCPU ||
2073 rcu_access_pointer(rt->rt6i_node));
0d3f6d29
MKL
2074}
2075
45e4fd26 2076static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
b8a653b5
HL
2077 const struct ipv6hdr *iph, u32 mtu,
2078 bool confirm_neigh)
1da177e4 2079{
0dec879f 2080 const struct in6_addr *daddr, *saddr;
67ba4152 2081 struct rt6_info *rt6 = (struct rt6_info *)dst;
1da177e4 2082
19bda36c
XL
2083 if (dst_metric_locked(dst, RTAX_MTU))
2084 return;
2085
0dec879f
JA
2086 if (iph) {
2087 daddr = &iph->daddr;
2088 saddr = &iph->saddr;
2089 } else if (sk) {
2090 daddr = &sk->sk_v6_daddr;
2091 saddr = &inet6_sk(sk)->saddr;
2092 } else {
2093 daddr = NULL;
2094 saddr = NULL;
2095 }
b8a653b5
HL
2096
2097 if (confirm_neigh)
2098 dst_confirm_neigh(dst, daddr);
2099
45e4fd26
MKL
2100 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2101 if (mtu >= dst_mtu(dst))
2102 return;
9d289715 2103
0d3f6d29 2104 if (!rt6_cache_allowed_for_pmtu(rt6)) {
45e4fd26 2105 rt6_do_update_pmtu(rt6, mtu);
2b760fcf
WW
2106 /* update rt6_ex->stamp for cache */
2107 if (rt6->rt6i_flags & RTF_CACHE)
2108 rt6_update_exception_stamp_rt(rt6);
0dec879f 2109 } else if (daddr) {
45e4fd26
MKL
2110 struct rt6_info *nrt6;
2111
45e4fd26
MKL
2112 nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr);
2113 if (nrt6) {
2114 rt6_do_update_pmtu(nrt6, mtu);
2b760fcf
WW
2115 if (rt6_insert_exception(nrt6, rt6))
2116 dst_release_immediate(&nrt6->dst);
45e4fd26 2117 }
1da177e4
LT
2118 }
2119}
2120
45e4fd26 2121static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
b8a653b5
HL
2122 struct sk_buff *skb, u32 mtu,
2123 bool confirm_neigh)
45e4fd26 2124{
b8a653b5
HL
2125 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2126 confirm_neigh);
45e4fd26
MKL
2127}
2128
42ae66c8 2129void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
e2d118a1 2130 int oif, u32 mark, kuid_t uid)
81aded24
DM
2131{
2132 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2133 struct dst_entry *dst;
2134 struct flowi6 fl6;
2135
2136 memset(&fl6, 0, sizeof(fl6));
2137 fl6.flowi6_oif = oif;
1b3c61dc 2138 fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
81aded24
DM
2139 fl6.daddr = iph->daddr;
2140 fl6.saddr = iph->saddr;
6502ca52 2141 fl6.flowlabel = ip6_flowinfo(iph);
e2d118a1 2142 fl6.flowi6_uid = uid;
81aded24
DM
2143
2144 dst = ip6_route_output(net, NULL, &fl6);
2145 if (!dst->error)
b8a653b5 2146 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
81aded24
DM
2147 dst_release(dst);
2148}
2149EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2150
2151void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2152{
99ed2b58 2153 int oif = sk->sk_bound_dev_if;
33c162a9
MKL
2154 struct dst_entry *dst;
2155
99ed2b58
DA
2156 if (!oif && skb->dev)
2157 oif = l3mdev_master_ifindex(skb->dev);
2158
2159 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
33c162a9
MKL
2160
2161 dst = __sk_dst_get(sk);
2162 if (!dst || !dst->obsolete ||
2163 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2164 return;
2165
2166 bh_lock_sock(sk);
2167 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2168 ip6_datagram_dst_update(sk, false);
2169 bh_unlock_sock(sk);
81aded24
DM
2170}
2171EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2172
b55b76b2
DJ
2173/* Handle redirects */
2174struct ip6rd_flowi {
2175 struct flowi6 fl6;
2176 struct in6_addr gateway;
2177};
2178
2179static struct rt6_info *__ip6_route_redirect(struct net *net,
2180 struct fib6_table *table,
2181 struct flowi6 *fl6,
2182 int flags)
2183{
2184 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
2b760fcf 2185 struct rt6_info *rt, *rt_cache;
b55b76b2
DJ
2186 struct fib6_node *fn;
2187
2188 /* Get the "current" route for this destination and
67c408cf 2189 * check if the redirect has come from appropriate router.
b55b76b2
DJ
2190 *
2191 * RFC 4861 specifies that redirects should only be
2192 * accepted if they come from the nexthop to the target.
2193 * Due to the way the routes are chosen, this notion
2194 * is a bit fuzzy and one might need to check all possible
2195 * routes.
2196 */
2197
66f5d6ce 2198 rcu_read_lock();
b55b76b2
DJ
2199 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2200restart:
66f5d6ce 2201 for_each_fib6_node_rt_rcu(fn) {
b55b76b2
DJ
2202 if (rt6_check_expired(rt))
2203 continue;
2204 if (rt->dst.error)
2205 break;
2206 if (!(rt->rt6i_flags & RTF_GATEWAY))
2207 continue;
2208 if (fl6->flowi6_oif != rt->dst.dev->ifindex)
2209 continue;
2b760fcf
WW
2210 /* rt_cache's gateway might be different from its 'parent'
2211 * in the case of an ip redirect.
2212 * So we keep searching in the exception table if the gateway
2213 * is different.
2214 */
2215 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) {
2216 rt_cache = rt6_find_cached_rt(rt,
2217 &fl6->daddr,
2218 &fl6->saddr);
2219 if (rt_cache &&
2220 ipv6_addr_equal(&rdfl->gateway,
2221 &rt_cache->rt6i_gateway)) {
2222 rt = rt_cache;
2223 break;
2224 }
b55b76b2 2225 continue;
2b760fcf 2226 }
b55b76b2
DJ
2227 break;
2228 }
2229
2230 if (!rt)
2231 rt = net->ipv6.ip6_null_entry;
2232 else if (rt->dst.error) {
2233 rt = net->ipv6.ip6_null_entry;
b0a1ba59
MKL
2234 goto out;
2235 }
2236
2237 if (rt == net->ipv6.ip6_null_entry) {
a3c00e46
MKL
2238 fn = fib6_backtrack(fn, &fl6->saddr);
2239 if (fn)
2240 goto restart;
b55b76b2 2241 }
a3c00e46 2242
b0a1ba59 2243out:
d3843fe5 2244 ip6_hold_safe(net, &rt, true);
b55b76b2 2245
66f5d6ce 2246 rcu_read_unlock();
b55b76b2 2247
b65f164d 2248 trace_fib6_table_lookup(net, rt, table, fl6);
b55b76b2
DJ
2249 return rt;
2250};
2251
2252static struct dst_entry *ip6_route_redirect(struct net *net,
2253 const struct flowi6 *fl6,
2254 const struct in6_addr *gateway)
2255{
2256 int flags = RT6_LOOKUP_F_HAS_SADDR;
2257 struct ip6rd_flowi rdfl;
2258
2259 rdfl.fl6 = *fl6;
2260 rdfl.gateway = *gateway;
2261
2262 return fib6_rule_lookup(net, &rdfl.fl6,
2263 flags, __ip6_route_redirect);
2264}
2265
e2d118a1
LC
2266void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2267 kuid_t uid)
3a5ad2ee
DM
2268{
2269 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2270 struct dst_entry *dst;
2271 struct flowi6 fl6;
2272
2273 memset(&fl6, 0, sizeof(fl6));
e374c618 2274 fl6.flowi6_iif = LOOPBACK_IFINDEX;
3a5ad2ee
DM
2275 fl6.flowi6_oif = oif;
2276 fl6.flowi6_mark = mark;
3a5ad2ee
DM
2277 fl6.daddr = iph->daddr;
2278 fl6.saddr = iph->saddr;
6502ca52 2279 fl6.flowlabel = ip6_flowinfo(iph);
e2d118a1 2280 fl6.flowi6_uid = uid;
3a5ad2ee 2281
b55b76b2
DJ
2282 dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
2283 rt6_do_redirect(dst, NULL, skb);
3a5ad2ee
DM
2284 dst_release(dst);
2285}
2286EXPORT_SYMBOL_GPL(ip6_redirect);
2287
c92a59ec
DJ
2288void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
2289 u32 mark)
2290{
2291 const struct ipv6hdr *iph = ipv6_hdr(skb);
2292 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2293 struct dst_entry *dst;
2294 struct flowi6 fl6;
2295
2296 memset(&fl6, 0, sizeof(fl6));
e374c618 2297 fl6.flowi6_iif = LOOPBACK_IFINDEX;
c92a59ec
DJ
2298 fl6.flowi6_oif = oif;
2299 fl6.flowi6_mark = mark;
c92a59ec
DJ
2300 fl6.daddr = msg->dest;
2301 fl6.saddr = iph->daddr;
e2d118a1 2302 fl6.flowi6_uid = sock_net_uid(net, NULL);
c92a59ec 2303
b55b76b2
DJ
2304 dst = ip6_route_redirect(net, &fl6, &iph->saddr);
2305 rt6_do_redirect(dst, NULL, skb);
c92a59ec
DJ
2306 dst_release(dst);
2307}
2308
3a5ad2ee
DM
2309void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
2310{
e2d118a1
LC
2311 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2312 sk->sk_uid);
3a5ad2ee
DM
2313}
2314EXPORT_SYMBOL_GPL(ip6_sk_redirect);
2315
0dbaee3b 2316static unsigned int ip6_default_advmss(const struct dst_entry *dst)
1da177e4 2317{
0dbaee3b
DM
2318 struct net_device *dev = dst->dev;
2319 unsigned int mtu = dst_mtu(dst);
2320 struct net *net = dev_net(dev);
2321
1da177e4
LT
2322 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
2323
5578689a
DL
2324 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
2325 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
1da177e4
LT
2326
2327 /*
1ab1457c
YH
2328 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
2329 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
2330 * IPV6_MAXPLEN is also valid and means: "any MSS,
1da177e4
LT
2331 * rely only on pmtu discovery"
2332 */
2333 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
2334 mtu = IPV6_MAXPLEN;
2335 return mtu;
2336}
2337
ebb762f2 2338static unsigned int ip6_mtu(const struct dst_entry *dst)
d33e4553 2339{
4b32b5ad
MKL
2340 const struct rt6_info *rt = (const struct rt6_info *)dst;
2341 unsigned int mtu = rt->rt6i_pmtu;
d33e4553 2342 struct inet6_dev *idev;
618f9bc7 2343
4b32b5ad
MKL
2344 if (mtu)
2345 goto out;
2346
2347 mtu = dst_metric_raw(dst, RTAX_MTU);
618f9bc7 2348 if (mtu)
30f78d8e 2349 goto out;
618f9bc7
SK
2350
2351 mtu = IPV6_MIN_MTU;
d33e4553
DM
2352
2353 rcu_read_lock();
2354 idev = __in6_dev_get(dst->dev);
2355 if (idev)
2356 mtu = idev->cnf.mtu6;
2357 rcu_read_unlock();
2358
30f78d8e 2359out:
14972cbd
RP
2360 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2361
2362 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
d33e4553
DM
2363}
2364
3b00944c 2365struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
87a11578 2366 struct flowi6 *fl6)
1da177e4 2367{
87a11578 2368 struct dst_entry *dst;
1da177e4
LT
2369 struct rt6_info *rt;
2370 struct inet6_dev *idev = in6_dev_get(dev);
c346dca1 2371 struct net *net = dev_net(dev);
1da177e4 2372
38308473 2373 if (unlikely(!idev))
122bdf67 2374 return ERR_PTR(-ENODEV);
1da177e4 2375
ad706862 2376 rt = ip6_dst_alloc(net, dev, 0);
38308473 2377 if (unlikely(!rt)) {
1da177e4 2378 in6_dev_put(idev);
87a11578 2379 dst = ERR_PTR(-ENOMEM);
1da177e4
LT
2380 goto out;
2381 }
2382
8e2ec639 2383 rt->dst.flags |= DST_HOST;
588753f1 2384 rt->dst.input = ip6_input;
8e2ec639 2385 rt->dst.output = ip6_output;
550bab42 2386 rt->rt6i_gateway = fl6->daddr;
87a11578 2387 rt->rt6i_dst.addr = fl6->daddr;
8e2ec639
YZ
2388 rt->rt6i_dst.plen = 128;
2389 rt->rt6i_idev = idev;
14edd87d 2390 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
1da177e4 2391
587fea74
WW
2392 /* Add this dst into uncached_list so that rt6_ifdown() can
2393 * do proper release of the net_device
2394 */
2395 rt6_uncached_list_add(rt);
81eb8447 2396 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
1da177e4 2397
87a11578
DM
2398 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
2399
1da177e4 2400out:
87a11578 2401 return dst;
1da177e4
LT
2402}
2403
569d3645 2404static int ip6_dst_gc(struct dst_ops *ops)
1da177e4 2405{
86393e52 2406 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
7019b78e
DL
2407 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
2408 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
2409 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
2410 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
2411 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
fc66f95c 2412 int entries;
7019b78e 2413
fc66f95c 2414 entries = dst_entries_get_fast(ops);
49a18d86 2415 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
fc66f95c 2416 entries <= rt_max_size)
1da177e4
LT
2417 goto out;
2418
6891a346 2419 net->ipv6.ip6_rt_gc_expire++;
14956643 2420 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
fc66f95c
ED
2421 entries = dst_entries_get_slow(ops);
2422 if (entries < ops->gc_thresh)
7019b78e 2423 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
1da177e4 2424out:
7019b78e 2425 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
fc66f95c 2426 return entries > rt_max_size;
1da177e4
LT
2427}
2428
e715b6d3
FW
2429static int ip6_convert_metrics(struct mx6_config *mxc,
2430 const struct fib6_config *cfg)
2431{
6670e152 2432 struct net *net = cfg->fc_nlinfo.nl_net;
c3a8d947 2433 bool ecn_ca = false;
e715b6d3
FW
2434 struct nlattr *nla;
2435 int remaining;
2436 u32 *mp;
2437
63159f29 2438 if (!cfg->fc_mx)
e715b6d3
FW
2439 return 0;
2440
2441 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
2442 if (unlikely(!mp))
2443 return -ENOMEM;
2444
2445 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
2446 int type = nla_type(nla);
1bb14807 2447 u32 val;
e715b6d3 2448
1bb14807
DB
2449 if (!type)
2450 continue;
2451 if (unlikely(type > RTAX_MAX))
2452 goto err;
ea697639 2453
1bb14807
DB
2454 if (type == RTAX_CC_ALGO) {
2455 char tmp[TCP_CA_NAME_MAX];
e715b6d3 2456
1bb14807 2457 nla_strlcpy(tmp, nla, sizeof(tmp));
6670e152 2458 val = tcp_ca_get_key_by_name(net, tmp, &ecn_ca);
1bb14807
DB
2459 if (val == TCP_CA_UNSPEC)
2460 goto err;
2461 } else {
2462 val = nla_get_u32(nla);
e715b6d3 2463 }
626abd59
PA
2464 if (type == RTAX_HOPLIMIT && val > 255)
2465 val = 255;
b8d3e416
DB
2466 if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK))
2467 goto err;
1bb14807
DB
2468
2469 mp[type - 1] = val;
2470 __set_bit(type - 1, mxc->mx_valid);
e715b6d3
FW
2471 }
2472
c3a8d947
DB
2473 if (ecn_ca) {
2474 __set_bit(RTAX_FEATURES - 1, mxc->mx_valid);
2475 mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA;
2476 }
e715b6d3 2477
c3a8d947 2478 mxc->mx = mp;
e715b6d3
FW
2479 return 0;
2480 err:
2481 kfree(mp);
2482 return -EINVAL;
2483}
1da177e4 2484
8c14586f
DA
2485static struct rt6_info *ip6_nh_lookup_table(struct net *net,
2486 struct fib6_config *cfg,
2487 const struct in6_addr *gw_addr)
2488{
2489 struct flowi6 fl6 = {
2490 .flowi6_oif = cfg->fc_ifindex,
2491 .daddr = *gw_addr,
2492 .saddr = cfg->fc_prefsrc,
2493 };
2494 struct fib6_table *table;
2495 struct rt6_info *rt;
d5d32e4b 2496 int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_IGNORE_LINKSTATE;
8c14586f
DA
2497
2498 table = fib6_get_table(net, cfg->fc_table);
2499 if (!table)
2500 return NULL;
2501
2502 if (!ipv6_addr_any(&cfg->fc_prefsrc))
2503 flags |= RT6_LOOKUP_F_HAS_SADDR;
2504
2505 rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags);
2506
2507 /* if table lookup failed, fall back to full lookup */
2508 if (rt == net->ipv6.ip6_null_entry) {
2509 ip6_rt_put(rt);
2510 rt = NULL;
2511 }
2512
2513 return rt;
2514}
2515
333c4301
DA
2516static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg,
2517 struct netlink_ext_ack *extack)
1da177e4 2518{
5578689a 2519 struct net *net = cfg->fc_nlinfo.nl_net;
1da177e4
LT
2520 struct rt6_info *rt = NULL;
2521 struct net_device *dev = NULL;
2522 struct inet6_dev *idev = NULL;
c71099ac 2523 struct fib6_table *table;
1da177e4 2524 int addr_type;
8c5b83f0 2525 int err = -EINVAL;
1da177e4 2526
557c44be 2527 /* RTF_PCPU is an internal flag; can not be set by userspace */
d5d531cb
DA
2528 if (cfg->fc_flags & RTF_PCPU) {
2529 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
557c44be 2530 goto out;
d5d531cb 2531 }
557c44be 2532
2ea2352e
WW
2533 /* RTF_CACHE is an internal flag; can not be set by userspace */
2534 if (cfg->fc_flags & RTF_CACHE) {
2535 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
2536 goto out;
2537 }
2538
d5d531cb
DA
2539 if (cfg->fc_dst_len > 128) {
2540 NL_SET_ERR_MSG(extack, "Invalid prefix length");
2541 goto out;
2542 }
2543 if (cfg->fc_src_len > 128) {
2544 NL_SET_ERR_MSG(extack, "Invalid source address length");
8c5b83f0 2545 goto out;
d5d531cb 2546 }
1da177e4 2547#ifndef CONFIG_IPV6_SUBTREES
d5d531cb
DA
2548 if (cfg->fc_src_len) {
2549 NL_SET_ERR_MSG(extack,
2550 "Specifying source address requires IPV6_SUBTREES to be enabled");
8c5b83f0 2551 goto out;
d5d531cb 2552 }
1da177e4 2553#endif
86872cb5 2554 if (cfg->fc_ifindex) {
1da177e4 2555 err = -ENODEV;
5578689a 2556 dev = dev_get_by_index(net, cfg->fc_ifindex);
1da177e4
LT
2557 if (!dev)
2558 goto out;
2559 idev = in6_dev_get(dev);
2560 if (!idev)
2561 goto out;
2562 }
2563
86872cb5
TG
2564 if (cfg->fc_metric == 0)
2565 cfg->fc_metric = IP6_RT_PRIO_USER;
1da177e4 2566
d71314b4 2567 err = -ENOBUFS;
38308473
DM
2568 if (cfg->fc_nlinfo.nlh &&
2569 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
d71314b4 2570 table = fib6_get_table(net, cfg->fc_table);
38308473 2571 if (!table) {
f3213831 2572 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
d71314b4
MV
2573 table = fib6_new_table(net, cfg->fc_table);
2574 }
2575 } else {
2576 table = fib6_new_table(net, cfg->fc_table);
2577 }
38308473
DM
2578
2579 if (!table)
c71099ac 2580 goto out;
c71099ac 2581
ad706862
MKL
2582 rt = ip6_dst_alloc(net, NULL,
2583 (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT);
1da177e4 2584
38308473 2585 if (!rt) {
1da177e4
LT
2586 err = -ENOMEM;
2587 goto out;
2588 }
2589
1716a961
G
2590 if (cfg->fc_flags & RTF_EXPIRES)
2591 rt6_set_expires(rt, jiffies +
2592 clock_t_to_jiffies(cfg->fc_expires));
2593 else
2594 rt6_clean_expires(rt);
1da177e4 2595
86872cb5
TG
2596 if (cfg->fc_protocol == RTPROT_UNSPEC)
2597 cfg->fc_protocol = RTPROT_BOOT;
2598 rt->rt6i_protocol = cfg->fc_protocol;
2599
2600 addr_type = ipv6_addr_type(&cfg->fc_dst);
1da177e4
LT
2601
2602 if (addr_type & IPV6_ADDR_MULTICAST)
d8d1f30b 2603 rt->dst.input = ip6_mc_input;
ab79ad14
2604 else if (cfg->fc_flags & RTF_LOCAL)
2605 rt->dst.input = ip6_input;
1da177e4 2606 else
d8d1f30b 2607 rt->dst.input = ip6_forward;
1da177e4 2608
d8d1f30b 2609 rt->dst.output = ip6_output;
1da177e4 2610
19e42e45
RP
2611 if (cfg->fc_encap) {
2612 struct lwtunnel_state *lwtstate;
2613
30357d7d 2614 err = lwtunnel_build_state(cfg->fc_encap_type,
127eb7cd 2615 cfg->fc_encap, AF_INET6, cfg,
9ae28727 2616 &lwtstate, extack);
19e42e45
RP
2617 if (err)
2618 goto out;
61adedf3
JB
2619 rt->dst.lwtstate = lwtstate_get(lwtstate);
2620 if (lwtunnel_output_redirect(rt->dst.lwtstate)) {
2621 rt->dst.lwtstate->orig_output = rt->dst.output;
2622 rt->dst.output = lwtunnel_output;
25368623 2623 }
61adedf3
JB
2624 if (lwtunnel_input_redirect(rt->dst.lwtstate)) {
2625 rt->dst.lwtstate->orig_input = rt->dst.input;
2626 rt->dst.input = lwtunnel_input;
25368623 2627 }
19e42e45
RP
2628 }
2629
86872cb5
TG
2630 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
2631 rt->rt6i_dst.plen = cfg->fc_dst_len;
afc4eef8 2632 if (rt->rt6i_dst.plen == 128)
e5fd387a 2633 rt->dst.flags |= DST_HOST;
e5fd387a 2634
1da177e4 2635#ifdef CONFIG_IPV6_SUBTREES
86872cb5
TG
2636 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
2637 rt->rt6i_src.plen = cfg->fc_src_len;
1da177e4
LT
2638#endif
2639
86872cb5 2640 rt->rt6i_metric = cfg->fc_metric;
1da177e4
LT
2641
2642 /* We cannot add true routes via loopback here,
2643 they would result in kernel looping; promote them to reject routes
2644 */
86872cb5 2645 if ((cfg->fc_flags & RTF_REJECT) ||
38308473
DM
2646 (dev && (dev->flags & IFF_LOOPBACK) &&
2647 !(addr_type & IPV6_ADDR_LOOPBACK) &&
2648 !(cfg->fc_flags & RTF_LOCAL))) {
1da177e4 2649 /* hold loopback dev/idev if we haven't done so. */
5578689a 2650 if (dev != net->loopback_dev) {
1da177e4
LT
2651 if (dev) {
2652 dev_put(dev);
2653 in6_dev_put(idev);
2654 }
5578689a 2655 dev = net->loopback_dev;
1da177e4
LT
2656 dev_hold(dev);
2657 idev = in6_dev_get(dev);
2658 if (!idev) {
2659 err = -ENODEV;
2660 goto out;
2661 }
2662 }
1da177e4 2663 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
ef2c7d7b
ND
2664 switch (cfg->fc_type) {
2665 case RTN_BLACKHOLE:
2666 rt->dst.error = -EINVAL;
ede2059d 2667 rt->dst.output = dst_discard_out;
7150aede 2668 rt->dst.input = dst_discard;
ef2c7d7b
ND
2669 break;
2670 case RTN_PROHIBIT:
2671 rt->dst.error = -EACCES;
7150aede
K
2672 rt->dst.output = ip6_pkt_prohibit_out;
2673 rt->dst.input = ip6_pkt_prohibit;
ef2c7d7b 2674 break;
b4949ab2 2675 case RTN_THROW:
0315e382 2676 case RTN_UNREACHABLE:
ef2c7d7b 2677 default:
7150aede 2678 rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
0315e382
NF
2679 : (cfg->fc_type == RTN_UNREACHABLE)
2680 ? -EHOSTUNREACH : -ENETUNREACH;
7150aede
K
2681 rt->dst.output = ip6_pkt_discard_out;
2682 rt->dst.input = ip6_pkt_discard;
ef2c7d7b
ND
2683 break;
2684 }
1da177e4
LT
2685 goto install_route;
2686 }
2687
86872cb5 2688 if (cfg->fc_flags & RTF_GATEWAY) {
b71d1d42 2689 const struct in6_addr *gw_addr;
1da177e4
LT
2690 int gwa_type;
2691
86872cb5 2692 gw_addr = &cfg->fc_gateway;
330567b7 2693 gwa_type = ipv6_addr_type(gw_addr);
48ed7b26
FW
2694
2695 /* if gw_addr is local we will fail to detect this in case
2696 * address is still TENTATIVE (DAD in progress). rt6_lookup()
2697 * will return already-added prefix route via interface that
2698 * prefix route was assigned to, which might be non-loopback.
2699 */
2700 err = -EINVAL;
330567b7
FW
2701 if (ipv6_chk_addr_and_flags(net, gw_addr,
2702 gwa_type & IPV6_ADDR_LINKLOCAL ?
d5d531cb
DA
2703 dev : NULL, 0, 0)) {
2704 NL_SET_ERR_MSG(extack, "Invalid gateway address");
48ed7b26 2705 goto out;
d5d531cb 2706 }
4e3fd7a0 2707 rt->rt6i_gateway = *gw_addr;
1da177e4
LT
2708
2709 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
8c14586f 2710 struct rt6_info *grt = NULL;
1da177e4
LT
2711
2712 /* IPv6 strictly inhibits using not link-local
2713 addresses as nexthop address.
2714 Otherwise, router will not able to send redirects.
2715 It is very good, but in some (rare!) circumstances
2716 (SIT, PtP, NBMA NOARP links) it is handy to allow
2717 some exceptions. --ANK
96d5822c
EN
2718 We allow IPv4-mapped nexthops to support RFC4798-type
2719 addressing
1da177e4 2720 */
96d5822c 2721 if (!(gwa_type & (IPV6_ADDR_UNICAST |
d5d531cb
DA
2722 IPV6_ADDR_MAPPED))) {
2723 NL_SET_ERR_MSG(extack,
2724 "Invalid gateway address");
1da177e4 2725 goto out;
d5d531cb 2726 }
1da177e4 2727
a435a07f 2728 if (cfg->fc_table) {
8c14586f
DA
2729 grt = ip6_nh_lookup_table(net, cfg, gw_addr);
2730
a435a07f
VB
2731 if (grt) {
2732 if (grt->rt6i_flags & RTF_GATEWAY ||
2733 (dev && dev != grt->dst.dev)) {
2734 ip6_rt_put(grt);
2735 grt = NULL;
2736 }
2737 }
2738 }
2739
8c14586f
DA
2740 if (!grt)
2741 grt = rt6_lookup(net, gw_addr, NULL,
2742 cfg->fc_ifindex, 1);
1da177e4
LT
2743
2744 err = -EHOSTUNREACH;
38308473 2745 if (!grt)
1da177e4
LT
2746 goto out;
2747 if (dev) {
d1918542 2748 if (dev != grt->dst.dev) {
94e187c0 2749 ip6_rt_put(grt);
1da177e4
LT
2750 goto out;
2751 }
2752 } else {
d1918542 2753 dev = grt->dst.dev;
1da177e4
LT
2754 idev = grt->rt6i_idev;
2755 dev_hold(dev);
2756 in6_dev_hold(grt->rt6i_idev);
2757 }
38308473 2758 if (!(grt->rt6i_flags & RTF_GATEWAY))
1da177e4 2759 err = 0;
94e187c0 2760 ip6_rt_put(grt);
1da177e4
LT
2761
2762 if (err)
2763 goto out;
2764 }
2765 err = -EINVAL;
d5d531cb
DA
2766 if (!dev) {
2767 NL_SET_ERR_MSG(extack, "Egress device not specified");
2768 goto out;
2769 } else if (dev->flags & IFF_LOOPBACK) {
2770 NL_SET_ERR_MSG(extack,
2771 "Egress device can not be loopback device for this route");
1da177e4 2772 goto out;
d5d531cb 2773 }
1da177e4
LT
2774 }
2775
2776 err = -ENODEV;
38308473 2777 if (!dev)
1da177e4
LT
2778 goto out;
2779
c3968a85
DW
2780 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
2781 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
d5d531cb 2782 NL_SET_ERR_MSG(extack, "Invalid source address");
c3968a85
DW
2783 err = -EINVAL;
2784 goto out;
2785 }
4e3fd7a0 2786 rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
c3968a85
DW
2787 rt->rt6i_prefsrc.plen = 128;
2788 } else
2789 rt->rt6i_prefsrc.plen = 0;
2790
86872cb5 2791 rt->rt6i_flags = cfg->fc_flags;
1da177e4
LT
2792
2793install_route:
d8d1f30b 2794 rt->dst.dev = dev;
1da177e4 2795 rt->rt6i_idev = idev;
c71099ac 2796 rt->rt6i_table = table;
63152fc0 2797
c346dca1 2798 cfg->fc_nlinfo.nl_net = dev_net(dev);
63152fc0 2799
8c5b83f0 2800 return rt;
6b9ea5a6
RP
2801out:
2802 if (dev)
2803 dev_put(dev);
2804 if (idev)
2805 in6_dev_put(idev);
587fea74
WW
2806 if (rt)
2807 dst_release_immediate(&rt->dst);
6b9ea5a6 2808
8c5b83f0 2809 return ERR_PTR(err);
6b9ea5a6
RP
2810}
2811
333c4301
DA
2812int ip6_route_add(struct fib6_config *cfg,
2813 struct netlink_ext_ack *extack)
6b9ea5a6
RP
2814{
2815 struct mx6_config mxc = { .mx = NULL, };
8c5b83f0 2816 struct rt6_info *rt;
6b9ea5a6
RP
2817 int err;
2818
333c4301 2819 rt = ip6_route_info_create(cfg, extack);
8c5b83f0
RP
2820 if (IS_ERR(rt)) {
2821 err = PTR_ERR(rt);
2822 rt = NULL;
6b9ea5a6 2823 goto out;
8c5b83f0 2824 }
6b9ea5a6 2825
e715b6d3
FW
2826 err = ip6_convert_metrics(&mxc, cfg);
2827 if (err)
2828 goto out;
1da177e4 2829
333c4301 2830 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack);
e715b6d3
FW
2831
2832 kfree(mxc.mx);
6b9ea5a6 2833
e715b6d3 2834 return err;
1da177e4 2835out:
587fea74
WW
2836 if (rt)
2837 dst_release_immediate(&rt->dst);
6b9ea5a6 2838
1da177e4
LT
2839 return err;
2840}
2841
86872cb5 2842static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
1da177e4
LT
2843{
2844 int err;
c71099ac 2845 struct fib6_table *table;
d1918542 2846 struct net *net = dev_net(rt->dst.dev);
1da177e4 2847
a4c2fd7f 2848 if (rt == net->ipv6.ip6_null_entry) {
6825a26c
G
2849 err = -ENOENT;
2850 goto out;
2851 }
6c813a72 2852
c71099ac 2853 table = rt->rt6i_table;
66f5d6ce 2854 spin_lock_bh(&table->tb6_lock);
86872cb5 2855 err = fib6_del(rt, info);
66f5d6ce 2856 spin_unlock_bh(&table->tb6_lock);
1da177e4 2857
6825a26c 2858out:
94e187c0 2859 ip6_rt_put(rt);
1da177e4
LT
2860 return err;
2861}
2862
e0a1ad73
TG
2863int ip6_del_rt(struct rt6_info *rt)
2864{
4d1169c1 2865 struct nl_info info = {
d1918542 2866 .nl_net = dev_net(rt->dst.dev),
4d1169c1 2867 };
528c4ceb 2868 return __ip6_del_rt(rt, &info);
e0a1ad73
TG
2869}
2870
0ae81335
DA
2871static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg)
2872{
2873 struct nl_info *info = &cfg->fc_nlinfo;
e3330039 2874 struct net *net = info->nl_net;
16a16cd3 2875 struct sk_buff *skb = NULL;
0ae81335 2876 struct fib6_table *table;
e3330039 2877 int err = -ENOENT;
0ae81335 2878
e3330039
WC
2879 if (rt == net->ipv6.ip6_null_entry)
2880 goto out_put;
0ae81335 2881 table = rt->rt6i_table;
66f5d6ce 2882 spin_lock_bh(&table->tb6_lock);
0ae81335
DA
2883
2884 if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) {
2885 struct rt6_info *sibling, *next_sibling;
2886
16a16cd3
DA
2887 /* prefer to send a single notification with all hops */
2888 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
2889 if (skb) {
2890 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
2891
e3330039 2892 if (rt6_fill_node(net, skb, rt,
16a16cd3
DA
2893 NULL, NULL, 0, RTM_DELROUTE,
2894 info->portid, seq, 0) < 0) {
2895 kfree_skb(skb);
2896 skb = NULL;
2897 } else
2898 info->skip_notify = 1;
2899 }
2900
0ae81335
DA
2901 list_for_each_entry_safe(sibling, next_sibling,
2902 &rt->rt6i_siblings,
2903 rt6i_siblings) {
2904 err = fib6_del(sibling, info);
2905 if (err)
e3330039 2906 goto out_unlock;
0ae81335
DA
2907 }
2908 }
2909
2910 err = fib6_del(rt, info);
e3330039 2911out_unlock:
66f5d6ce 2912 spin_unlock_bh(&table->tb6_lock);
e3330039 2913out_put:
0ae81335 2914 ip6_rt_put(rt);
16a16cd3
DA
2915
2916 if (skb) {
e3330039 2917 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
16a16cd3
DA
2918 info->nlh, gfp_any());
2919 }
0ae81335
DA
2920 return err;
2921}
2922
333c4301
DA
2923static int ip6_route_del(struct fib6_config *cfg,
2924 struct netlink_ext_ack *extack)
1da177e4 2925{
2b760fcf 2926 struct rt6_info *rt, *rt_cache;
c71099ac 2927 struct fib6_table *table;
1da177e4 2928 struct fib6_node *fn;
1da177e4
LT
2929 int err = -ESRCH;
2930
5578689a 2931 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
d5d531cb
DA
2932 if (!table) {
2933 NL_SET_ERR_MSG(extack, "FIB table does not exist");
c71099ac 2934 return err;
d5d531cb 2935 }
c71099ac 2936
66f5d6ce 2937 rcu_read_lock();
1da177e4 2938
c71099ac 2939 fn = fib6_locate(&table->tb6_root,
86872cb5 2940 &cfg->fc_dst, cfg->fc_dst_len,
38fbeeee 2941 &cfg->fc_src, cfg->fc_src_len,
2b760fcf 2942 !(cfg->fc_flags & RTF_CACHE));
1ab1457c 2943
1da177e4 2944 if (fn) {
66f5d6ce 2945 for_each_fib6_node_rt_rcu(fn) {
2b760fcf
WW
2946 if (cfg->fc_flags & RTF_CACHE) {
2947 rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst,
2948 &cfg->fc_src);
2949 if (!rt_cache)
2950 continue;
2951 rt = rt_cache;
2952 }
86872cb5 2953 if (cfg->fc_ifindex &&
d1918542
DM
2954 (!rt->dst.dev ||
2955 rt->dst.dev->ifindex != cfg->fc_ifindex))
1da177e4 2956 continue;
86872cb5
TG
2957 if (cfg->fc_flags & RTF_GATEWAY &&
2958 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
1da177e4 2959 continue;
86872cb5 2960 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
1da177e4 2961 continue;
c2ed1880
M
2962 if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol)
2963 continue;
d3843fe5
WW
2964 if (!dst_hold_safe(&rt->dst))
2965 break;
66f5d6ce 2966 rcu_read_unlock();
1da177e4 2967
0ae81335
DA
2968 /* if gateway was specified only delete the one hop */
2969 if (cfg->fc_flags & RTF_GATEWAY)
2970 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
2971
2972 return __ip6_del_rt_siblings(rt, cfg);
1da177e4
LT
2973 }
2974 }
66f5d6ce 2975 rcu_read_unlock();
1da177e4
LT
2976
2977 return err;
2978}
2979
6700c270 2980static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
a6279458 2981{
a6279458 2982 struct netevent_redirect netevent;
e8599ff4 2983 struct rt6_info *rt, *nrt = NULL;
e8599ff4
DM
2984 struct ndisc_options ndopts;
2985 struct inet6_dev *in6_dev;
2986 struct neighbour *neigh;
71bcdba0 2987 struct rd_msg *msg;
6e157b6a
DM
2988 int optlen, on_link;
2989 u8 *lladdr;
e8599ff4 2990
29a3cad5 2991 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
71bcdba0 2992 optlen -= sizeof(*msg);
e8599ff4
DM
2993
2994 if (optlen < 0) {
6e157b6a 2995 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
e8599ff4
DM
2996 return;
2997 }
2998
71bcdba0 2999 msg = (struct rd_msg *)icmp6_hdr(skb);
e8599ff4 3000
71bcdba0 3001 if (ipv6_addr_is_multicast(&msg->dest)) {
6e157b6a 3002 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
e8599ff4
DM
3003 return;
3004 }
3005
6e157b6a 3006 on_link = 0;
71bcdba0 3007 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
e8599ff4 3008 on_link = 1;
71bcdba0 3009 } else if (ipv6_addr_type(&msg->target) !=
e8599ff4 3010 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
6e157b6a 3011 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
e8599ff4
DM
3012 return;
3013 }
3014
3015 in6_dev = __in6_dev_get(skb->dev);
3016 if (!in6_dev)
3017 return;
3018 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3019 return;
3020
3021 /* RFC2461 8.1:
3022 * The IP source address of the Redirect MUST be the same as the current
3023 * first-hop router for the specified ICMP Destination Address.
3024 */
3025
f997c55c 3026 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
e8599ff4
DM
3027 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3028 return;
3029 }
6e157b6a
DM
3030
3031 lladdr = NULL;
e8599ff4
DM
3032 if (ndopts.nd_opts_tgt_lladdr) {
3033 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3034 skb->dev);
3035 if (!lladdr) {
3036 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3037 return;
3038 }
3039 }
3040
6e157b6a 3041 rt = (struct rt6_info *) dst;
ec13ad1d 3042 if (rt->rt6i_flags & RTF_REJECT) {
6e157b6a 3043 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
e8599ff4 3044 return;
6e157b6a 3045 }
e8599ff4 3046
6e157b6a
DM
3047 /* Redirect received -> path was valid.
3048 * Look, redirects are sent only in response to data packets,
3049 * so that this nexthop apparently is reachable. --ANK
3050 */
0dec879f 3051 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
a6279458 3052
71bcdba0 3053 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
6e157b6a
DM
3054 if (!neigh)
3055 return;
a6279458 3056
1da177e4
LT
3057 /*
3058 * We have finally decided to accept it.
3059 */
3060
f997c55c 3061 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
1da177e4
LT
3062 NEIGH_UPDATE_F_WEAK_OVERRIDE|
3063 NEIGH_UPDATE_F_OVERRIDE|
3064 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
f997c55c
AA
3065 NEIGH_UPDATE_F_ISROUTER)),
3066 NDISC_REDIRECT, &ndopts);
1da177e4 3067
83a09abd 3068 nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL);
38308473 3069 if (!nrt)
1da177e4
LT
3070 goto out;
3071
3072 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
3073 if (on_link)
3074 nrt->rt6i_flags &= ~RTF_GATEWAY;
3075
b91d5329 3076 nrt->rt6i_protocol = RTPROT_REDIRECT;
4e3fd7a0 3077 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
1da177e4 3078
2b760fcf
WW
3079 /* No need to remove rt from the exception table if rt is
3080 * a cached route because rt6_insert_exception() will
3081 * takes care of it
3082 */
3083 if (rt6_insert_exception(nrt, rt)) {
3084 dst_release_immediate(&nrt->dst);
3085 goto out;
3086 }
1da177e4 3087
d8d1f30b
CG
3088 netevent.old = &rt->dst;
3089 netevent.new = &nrt->dst;
71bcdba0 3090 netevent.daddr = &msg->dest;
60592833 3091 netevent.neigh = neigh;
8d71740c
TT
3092 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
3093
1da177e4 3094out:
e8599ff4 3095 neigh_release(neigh);
6e157b6a
DM
3096}
3097
1da177e4
LT
3098/*
3099 * Misc support functions
3100 */
3101
4b32b5ad
MKL
3102static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
3103{
3104 BUG_ON(from->dst.from);
3105
3106 rt->rt6i_flags &= ~RTF_EXPIRES;
3107 dst_hold(&from->dst);
3108 rt->dst.from = &from->dst;
3109 dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true);
3110}
3111
83a09abd
MKL
3112static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort)
3113{
3114 rt->dst.input = ort->dst.input;
3115 rt->dst.output = ort->dst.output;
3116 rt->rt6i_dst = ort->rt6i_dst;
3117 rt->dst.error = ort->dst.error;
3118 rt->rt6i_idev = ort->rt6i_idev;
3119 if (rt->rt6i_idev)
3120 in6_dev_hold(rt->rt6i_idev);
3121 rt->dst.lastuse = jiffies;
3122 rt->rt6i_gateway = ort->rt6i_gateway;
3123 rt->rt6i_flags = ort->rt6i_flags;
3124 rt6_set_from(rt, ort);
3125 rt->rt6i_metric = ort->rt6i_metric;
1da177e4 3126#ifdef CONFIG_IPV6_SUBTREES
83a09abd 3127 rt->rt6i_src = ort->rt6i_src;
1da177e4 3128#endif
83a09abd
MKL
3129 rt->rt6i_prefsrc = ort->rt6i_prefsrc;
3130 rt->rt6i_table = ort->rt6i_table;
61adedf3 3131 rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate);
1da177e4
LT
3132}
3133
70ceb4f5 3134#ifdef CONFIG_IPV6_ROUTE_INFO
efa2cea0 3135static struct rt6_info *rt6_get_route_info(struct net *net,
b71d1d42 3136 const struct in6_addr *prefix, int prefixlen,
830218c1
DA
3137 const struct in6_addr *gwaddr,
3138 struct net_device *dev)
70ceb4f5 3139{
830218c1
DA
3140 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3141 int ifindex = dev->ifindex;
70ceb4f5
YH
3142 struct fib6_node *fn;
3143 struct rt6_info *rt = NULL;
c71099ac
TG
3144 struct fib6_table *table;
3145
830218c1 3146 table = fib6_get_table(net, tb_id);
38308473 3147 if (!table)
c71099ac 3148 return NULL;
70ceb4f5 3149
66f5d6ce 3150 rcu_read_lock();
38fbeeee 3151 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
70ceb4f5
YH
3152 if (!fn)
3153 goto out;
3154
66f5d6ce 3155 for_each_fib6_node_rt_rcu(fn) {
d1918542 3156 if (rt->dst.dev->ifindex != ifindex)
70ceb4f5
YH
3157 continue;
3158 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
3159 continue;
3160 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
3161 continue;
d3843fe5 3162 ip6_hold_safe(NULL, &rt, false);
70ceb4f5
YH
3163 break;
3164 }
3165out:
66f5d6ce 3166 rcu_read_unlock();
70ceb4f5
YH
3167 return rt;
3168}
3169
efa2cea0 3170static struct rt6_info *rt6_add_route_info(struct net *net,
b71d1d42 3171 const struct in6_addr *prefix, int prefixlen,
830218c1
DA
3172 const struct in6_addr *gwaddr,
3173 struct net_device *dev,
95c96174 3174 unsigned int pref)
70ceb4f5 3175{
86872cb5 3176 struct fib6_config cfg = {
238fc7ea 3177 .fc_metric = IP6_RT_PRIO_USER,
830218c1 3178 .fc_ifindex = dev->ifindex,
86872cb5
TG
3179 .fc_dst_len = prefixlen,
3180 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
3181 RTF_UP | RTF_PREF(pref),
b91d5329 3182 .fc_protocol = RTPROT_RA,
15e47304 3183 .fc_nlinfo.portid = 0,
efa2cea0
DL
3184 .fc_nlinfo.nlh = NULL,
3185 .fc_nlinfo.nl_net = net,
86872cb5
TG
3186 };
3187
830218c1 3188 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
4e3fd7a0
AD
3189 cfg.fc_dst = *prefix;
3190 cfg.fc_gateway = *gwaddr;
70ceb4f5 3191
e317da96
YH
3192 /* We should treat it as a default route if prefix length is 0. */
3193 if (!prefixlen)
86872cb5 3194 cfg.fc_flags |= RTF_DEFAULT;
70ceb4f5 3195
333c4301 3196 ip6_route_add(&cfg, NULL);
70ceb4f5 3197
830218c1 3198 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
70ceb4f5
YH
3199}
3200#endif
3201
b71d1d42 3202struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
1ab1457c 3203{
830218c1 3204 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
1da177e4 3205 struct rt6_info *rt;
c71099ac 3206 struct fib6_table *table;
1da177e4 3207
830218c1 3208 table = fib6_get_table(dev_net(dev), tb_id);
38308473 3209 if (!table)
c71099ac 3210 return NULL;
1da177e4 3211
66f5d6ce
WW
3212 rcu_read_lock();
3213 for_each_fib6_node_rt_rcu(&table->tb6_root) {
d1918542 3214 if (dev == rt->dst.dev &&
045927ff 3215 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
1da177e4
LT
3216 ipv6_addr_equal(&rt->rt6i_gateway, addr))
3217 break;
3218 }
3219 if (rt)
d3843fe5 3220 ip6_hold_safe(NULL, &rt, false);
66f5d6ce 3221 rcu_read_unlock();
1da177e4
LT
3222 return rt;
3223}
3224
b71d1d42 3225struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
ebacaaa0
YH
3226 struct net_device *dev,
3227 unsigned int pref)
1da177e4 3228{
86872cb5 3229 struct fib6_config cfg = {
ca254490 3230 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
238fc7ea 3231 .fc_metric = IP6_RT_PRIO_USER,
86872cb5
TG
3232 .fc_ifindex = dev->ifindex,
3233 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
3234 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
b91d5329 3235 .fc_protocol = RTPROT_RA,
15e47304 3236 .fc_nlinfo.portid = 0,
5578689a 3237 .fc_nlinfo.nlh = NULL,
c346dca1 3238 .fc_nlinfo.nl_net = dev_net(dev),
86872cb5 3239 };
1da177e4 3240
4e3fd7a0 3241 cfg.fc_gateway = *gwaddr;
1da177e4 3242
333c4301 3243 if (!ip6_route_add(&cfg, NULL)) {
830218c1
DA
3244 struct fib6_table *table;
3245
3246 table = fib6_get_table(dev_net(dev), cfg.fc_table);
3247 if (table)
3248 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3249 }
1da177e4 3250
1da177e4
LT
3251 return rt6_get_dflt_router(gwaddr, dev);
3252}
3253
830218c1 3254static void __rt6_purge_dflt_routers(struct fib6_table *table)
1da177e4
LT
3255{
3256 struct rt6_info *rt;
3257
3258restart:
66f5d6ce
WW
3259 rcu_read_lock();
3260 for_each_fib6_node_rt_rcu(&table->tb6_root) {
3e8b0ac3
LC
3261 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
3262 (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
d3843fe5 3263 if (dst_hold_safe(&rt->dst)) {
66f5d6ce 3264 rcu_read_unlock();
d3843fe5
WW
3265 ip6_del_rt(rt);
3266 } else {
66f5d6ce 3267 rcu_read_unlock();
d3843fe5 3268 }
1da177e4
LT
3269 goto restart;
3270 }
3271 }
66f5d6ce 3272 rcu_read_unlock();
830218c1
DA
3273
3274 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3275}
3276
3277void rt6_purge_dflt_routers(struct net *net)
3278{
3279 struct fib6_table *table;
3280 struct hlist_head *head;
3281 unsigned int h;
3282
3283 rcu_read_lock();
3284
3285 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3286 head = &net->ipv6.fib_table_hash[h];
3287 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3288 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
3289 __rt6_purge_dflt_routers(table);
3290 }
3291 }
3292
3293 rcu_read_unlock();
1da177e4
LT
3294}
3295
5578689a
DL
3296static void rtmsg_to_fib6_config(struct net *net,
3297 struct in6_rtmsg *rtmsg,
86872cb5
TG
3298 struct fib6_config *cfg)
3299{
3300 memset(cfg, 0, sizeof(*cfg));
3301
ca254490
DA
3302 cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
3303 : RT6_TABLE_MAIN;
86872cb5
TG
3304 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
3305 cfg->fc_metric = rtmsg->rtmsg_metric;
3306 cfg->fc_expires = rtmsg->rtmsg_info;
3307 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
3308 cfg->fc_src_len = rtmsg->rtmsg_src_len;
3309 cfg->fc_flags = rtmsg->rtmsg_flags;
3310
5578689a 3311 cfg->fc_nlinfo.nl_net = net;
f1243c2d 3312
4e3fd7a0
AD
3313 cfg->fc_dst = rtmsg->rtmsg_dst;
3314 cfg->fc_src = rtmsg->rtmsg_src;
3315 cfg->fc_gateway = rtmsg->rtmsg_gateway;
86872cb5
TG
3316}
3317
5578689a 3318int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
1da177e4 3319{
86872cb5 3320 struct fib6_config cfg;
1da177e4
LT
3321 struct in6_rtmsg rtmsg;
3322 int err;
3323
67ba4152 3324 switch (cmd) {
1da177e4
LT
3325 case SIOCADDRT: /* Add a route */
3326 case SIOCDELRT: /* Delete a route */
af31f412 3327 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1da177e4
LT
3328 return -EPERM;
3329 err = copy_from_user(&rtmsg, arg,
3330 sizeof(struct in6_rtmsg));
3331 if (err)
3332 return -EFAULT;
86872cb5 3333
5578689a 3334 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
86872cb5 3335
1da177e4
LT
3336 rtnl_lock();
3337 switch (cmd) {
3338 case SIOCADDRT:
333c4301 3339 err = ip6_route_add(&cfg, NULL);
1da177e4
LT
3340 break;
3341 case SIOCDELRT:
333c4301 3342 err = ip6_route_del(&cfg, NULL);
1da177e4
LT
3343 break;
3344 default:
3345 err = -EINVAL;
3346 }
3347 rtnl_unlock();
3348
3349 return err;
3ff50b79 3350 }
1da177e4
LT
3351
3352 return -EINVAL;
3353}
3354
3355/*
3356 * Drop the packet on the floor
3357 */
3358
d5fdd6ba 3359static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
1da177e4 3360{
612f09e8 3361 int type;
adf30907 3362 struct dst_entry *dst = skb_dst(skb);
612f09e8
YH
3363 switch (ipstats_mib_noroutes) {
3364 case IPSTATS_MIB_INNOROUTES:
0660e03f 3365 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
45bb0060 3366 if (type == IPV6_ADDR_ANY) {
3bd653c8
DL
3367 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
3368 IPSTATS_MIB_INADDRERRORS);
612f09e8
YH
3369 break;
3370 }
3371 /* FALLTHROUGH */
3372 case IPSTATS_MIB_OUTNOROUTES:
3bd653c8
DL
3373 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
3374 ipstats_mib_noroutes);
612f09e8
YH
3375 break;
3376 }
3ffe533c 3377 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
1da177e4
LT
3378 kfree_skb(skb);
3379 return 0;
3380}
3381
9ce8ade0
TG
3382static int ip6_pkt_discard(struct sk_buff *skb)
3383{
612f09e8 3384 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
9ce8ade0
TG
3385}
3386
ede2059d 3387static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
1da177e4 3388{
adf30907 3389 skb->dev = skb_dst(skb)->dev;
612f09e8 3390 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
1da177e4
LT
3391}
3392
9ce8ade0
TG
3393static int ip6_pkt_prohibit(struct sk_buff *skb)
3394{
612f09e8 3395 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
9ce8ade0
TG
3396}
3397
ede2059d 3398static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
9ce8ade0 3399{
adf30907 3400 skb->dev = skb_dst(skb)->dev;
612f09e8 3401 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
9ce8ade0
TG
3402}
3403
1da177e4
LT
3404/*
3405 * Allocate a dst for local (unicast / anycast) address.
3406 */
3407
3408struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
3409 const struct in6_addr *addr,
8f031519 3410 bool anycast)
1da177e4 3411{
ca254490 3412 u32 tb_id;
c346dca1 3413 struct net *net = dev_net(idev->dev);
4832c30d 3414 struct net_device *dev = idev->dev;
5f02ce24
DA
3415 struct rt6_info *rt;
3416
5f02ce24 3417 rt = ip6_dst_alloc(net, dev, DST_NOCOUNT);
a3300ef4 3418 if (!rt)
1da177e4
LT
3419 return ERR_PTR(-ENOMEM);
3420
1da177e4
LT
3421 in6_dev_hold(idev);
3422
11d53b49 3423 rt->dst.flags |= DST_HOST;
d8d1f30b
CG
3424 rt->dst.input = ip6_input;
3425 rt->dst.output = ip6_output;
1da177e4 3426 rt->rt6i_idev = idev;
1da177e4 3427
94b5e0f9 3428 rt->rt6i_protocol = RTPROT_KERNEL;
1da177e4 3429 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
58c4fb86
YH
3430 if (anycast)
3431 rt->rt6i_flags |= RTF_ANYCAST;
3432 else
1da177e4 3433 rt->rt6i_flags |= RTF_LOCAL;
1da177e4 3434
550bab42 3435 rt->rt6i_gateway = *addr;
4e3fd7a0 3436 rt->rt6i_dst.addr = *addr;
1da177e4 3437 rt->rt6i_dst.plen = 128;
ca254490
DA
3438 tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL;
3439 rt->rt6i_table = fib6_get_table(net, tb_id);
1da177e4 3440
1da177e4
LT
3441 return rt;
3442}
3443
c3968a85
DW
3444/* remove deleted ip from prefsrc entries */
3445struct arg_dev_net_ip {
3446 struct net_device *dev;
3447 struct net *net;
3448 struct in6_addr *addr;
3449};
3450
3451static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
3452{
3453 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3454 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3455 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3456
d1918542 3457 if (((void *)rt->dst.dev == dev || !dev) &&
c3968a85
DW
3458 rt != net->ipv6.ip6_null_entry &&
3459 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
60006a48 3460 spin_lock_bh(&rt6_exception_lock);
c3968a85
DW
3461 /* remove prefsrc entry */
3462 rt->rt6i_prefsrc.plen = 0;
60006a48
WW
3463 /* need to update cache as well */
3464 rt6_exceptions_remove_prefsrc(rt);
3465 spin_unlock_bh(&rt6_exception_lock);
c3968a85
DW
3466 }
3467 return 0;
3468}
3469
3470void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
3471{
3472 struct net *net = dev_net(ifp->idev->dev);
3473 struct arg_dev_net_ip adni = {
3474 .dev = ifp->idev->dev,
3475 .net = net,
3476 .addr = &ifp->addr,
3477 };
0c3584d5 3478 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
c3968a85
DW
3479}
3480
be7a010d 3481#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
be7a010d
DJ
3482
3483/* Remove routers and update dst entries when gateway turn into host. */
3484static int fib6_clean_tohost(struct rt6_info *rt, void *arg)
3485{
3486 struct in6_addr *gateway = (struct in6_addr *)arg;
3487
2b760fcf
WW
3488 if (((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
3489 ipv6_addr_equal(gateway, &rt->rt6i_gateway)) {
be7a010d
DJ
3490 return -1;
3491 }
b16cb459
WW
3492
3493 /* Further clean up cached routes in exception table.
3494 * This is needed because cached route may have a different
3495 * gateway than its 'parent' in the case of an ip redirect.
3496 */
3497 rt6_exceptions_clean_tohost(rt, gateway);
3498
be7a010d
DJ
3499 return 0;
3500}
3501
3502void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
3503{
3504 fib6_clean_all(net, fib6_clean_tohost, gateway);
3505}
3506
8ed67789
DL
3507struct arg_dev_net {
3508 struct net_device *dev;
3509 struct net *net;
3510};
3511
a1a22c12 3512/* called with write lock held for table with rt */
1da177e4
LT
3513static int fib6_ifdown(struct rt6_info *rt, void *arg)
3514{
bc3ef660 3515 const struct arg_dev_net *adn = arg;
3516 const struct net_device *dev = adn->dev;
8ed67789 3517
d1918542 3518 if ((rt->dst.dev == dev || !dev) &&
a1a22c12
DA
3519 rt != adn->net->ipv6.ip6_null_entry &&
3520 (rt->rt6i_nsiblings == 0 ||
8397ed36 3521 (dev && netdev_unregistering(dev)) ||
a1a22c12 3522 !rt->rt6i_idev->cnf.ignore_routes_with_linkdown))
1da177e4 3523 return -1;
c159d30c 3524
1da177e4
LT
3525 return 0;
3526}
3527
f3db4851 3528void rt6_ifdown(struct net *net, struct net_device *dev)
1da177e4 3529{
8ed67789
DL
3530 struct arg_dev_net adn = {
3531 .dev = dev,
3532 .net = net,
3533 };
3534
0c3584d5 3535 fib6_clean_all(net, fib6_ifdown, &adn);
e332bc67
EB
3536 if (dev)
3537 rt6_uncached_list_flush_dev(net, dev);
1da177e4
LT
3538}
3539
95c96174 3540struct rt6_mtu_change_arg {
1da177e4 3541 struct net_device *dev;
95c96174 3542 unsigned int mtu;
1da177e4
LT
3543};
3544
3545static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
3546{
3547 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
3548 struct inet6_dev *idev;
3549
3550 /* In IPv6 pmtu discovery is not optional,
3551 so that RTAX_MTU lock cannot disable it.
3552 We still use this lock to block changes
3553 caused by addrconf/ndisc.
3554 */
3555
3556 idev = __in6_dev_get(arg->dev);
38308473 3557 if (!idev)
1da177e4
LT
3558 return 0;
3559
3560 /* For administrative MTU increase, there is no way to discover
3561 IPv6 PMTU increase, so PMTU increase should be updated here.
3562 Since RFC 1981 doesn't include administrative MTU increase
3563 update PMTU increase is a MUST. (i.e. jumbo frame)
3564 */
d1918542 3565 if (rt->dst.dev == arg->dev &&
4b32b5ad 3566 !dst_metric_locked(&rt->dst, RTAX_MTU)) {
f5bbe7ee 3567 spin_lock_bh(&rt6_exception_lock);
8f523861
SB
3568 if (dst_metric_raw(&rt->dst, RTAX_MTU) &&
3569 rt6_mtu_change_route_allowed(idev, rt, arg->mtu))
4b32b5ad 3570 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
8f523861 3571 rt6_exceptions_update_pmtu(idev, rt, arg->mtu);
f5bbe7ee 3572 spin_unlock_bh(&rt6_exception_lock);
566cfd8f 3573 }
1da177e4
LT
3574 return 0;
3575}
3576
95c96174 3577void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
1da177e4 3578{
c71099ac
TG
3579 struct rt6_mtu_change_arg arg = {
3580 .dev = dev,
3581 .mtu = mtu,
3582 };
1da177e4 3583
0c3584d5 3584 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
1da177e4
LT
3585}
3586
ef7c79ed 3587static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
5176f91e 3588 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
41912cc6 3589 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
86872cb5 3590 [RTA_OIF] = { .type = NLA_U32 },
ab364a6f 3591 [RTA_IIF] = { .type = NLA_U32 },
86872cb5
TG
3592 [RTA_PRIORITY] = { .type = NLA_U32 },
3593 [RTA_METRICS] = { .type = NLA_NESTED },
51ebd318 3594 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
c78ba6d6 3595 [RTA_PREF] = { .type = NLA_U8 },
19e42e45
RP
3596 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
3597 [RTA_ENCAP] = { .type = NLA_NESTED },
32bc201e 3598 [RTA_EXPIRES] = { .type = NLA_U32 },
622ec2c9 3599 [RTA_UID] = { .type = NLA_U32 },
3b45a410 3600 [RTA_MARK] = { .type = NLA_U32 },
41912cc6 3601 [RTA_TABLE] = { .type = NLA_U32 },
86872cb5
TG
3602};
3603
3604static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
333c4301
DA
3605 struct fib6_config *cfg,
3606 struct netlink_ext_ack *extack)
1da177e4 3607{
86872cb5
TG
3608 struct rtmsg *rtm;
3609 struct nlattr *tb[RTA_MAX+1];
c78ba6d6 3610 unsigned int pref;
86872cb5 3611 int err;
1da177e4 3612
fceb6435
JB
3613 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
3614 NULL);
86872cb5
TG
3615 if (err < 0)
3616 goto errout;
1da177e4 3617
86872cb5
TG
3618 err = -EINVAL;
3619 rtm = nlmsg_data(nlh);
3620 memset(cfg, 0, sizeof(*cfg));
3621
3622 cfg->fc_table = rtm->rtm_table;
3623 cfg->fc_dst_len = rtm->rtm_dst_len;
3624 cfg->fc_src_len = rtm->rtm_src_len;
3625 cfg->fc_flags = RTF_UP;
3626 cfg->fc_protocol = rtm->rtm_protocol;
ef2c7d7b 3627 cfg->fc_type = rtm->rtm_type;
86872cb5 3628
ef2c7d7b
ND
3629 if (rtm->rtm_type == RTN_UNREACHABLE ||
3630 rtm->rtm_type == RTN_BLACKHOLE ||
b4949ab2
ND
3631 rtm->rtm_type == RTN_PROHIBIT ||
3632 rtm->rtm_type == RTN_THROW)
86872cb5
TG
3633 cfg->fc_flags |= RTF_REJECT;
3634
ab79ad14
3635 if (rtm->rtm_type == RTN_LOCAL)
3636 cfg->fc_flags |= RTF_LOCAL;
3637
1f56a01f
MKL
3638 if (rtm->rtm_flags & RTM_F_CLONED)
3639 cfg->fc_flags |= RTF_CACHE;
3640
15e47304 3641 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
86872cb5 3642 cfg->fc_nlinfo.nlh = nlh;
3b1e0a65 3643 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
86872cb5
TG
3644
3645 if (tb[RTA_GATEWAY]) {
67b61f6c 3646 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
86872cb5 3647 cfg->fc_flags |= RTF_GATEWAY;
1da177e4 3648 }
93e82672
DA
3649 if (tb[RTA_VIA]) {
3650 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
3651 goto errout;
3652 }
86872cb5
TG
3653
3654 if (tb[RTA_DST]) {
3655 int plen = (rtm->rtm_dst_len + 7) >> 3;
3656
3657 if (nla_len(tb[RTA_DST]) < plen)
3658 goto errout;
3659
3660 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
1da177e4 3661 }
86872cb5
TG
3662
3663 if (tb[RTA_SRC]) {
3664 int plen = (rtm->rtm_src_len + 7) >> 3;
3665
3666 if (nla_len(tb[RTA_SRC]) < plen)
3667 goto errout;
3668
3669 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
1da177e4 3670 }
86872cb5 3671
c3968a85 3672 if (tb[RTA_PREFSRC])
67b61f6c 3673 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
c3968a85 3674
86872cb5
TG
3675 if (tb[RTA_OIF])
3676 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
3677
3678 if (tb[RTA_PRIORITY])
3679 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
3680
3681 if (tb[RTA_METRICS]) {
3682 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
3683 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
1da177e4 3684 }
86872cb5
TG
3685
3686 if (tb[RTA_TABLE])
3687 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
3688
51ebd318
ND
3689 if (tb[RTA_MULTIPATH]) {
3690 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
3691 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
9ed59592
DA
3692
3693 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
c255bd68 3694 cfg->fc_mp_len, extack);
9ed59592
DA
3695 if (err < 0)
3696 goto errout;
51ebd318
ND
3697 }
3698
c78ba6d6
LR
3699 if (tb[RTA_PREF]) {
3700 pref = nla_get_u8(tb[RTA_PREF]);
3701 if (pref != ICMPV6_ROUTER_PREF_LOW &&
3702 pref != ICMPV6_ROUTER_PREF_HIGH)
3703 pref = ICMPV6_ROUTER_PREF_MEDIUM;
3704 cfg->fc_flags |= RTF_PREF(pref);
3705 }
3706
19e42e45
RP
3707 if (tb[RTA_ENCAP])
3708 cfg->fc_encap = tb[RTA_ENCAP];
3709
9ed59592 3710 if (tb[RTA_ENCAP_TYPE]) {
19e42e45
RP
3711 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
3712
c255bd68 3713 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
9ed59592
DA
3714 if (err < 0)
3715 goto errout;
3716 }
3717
32bc201e
XL
3718 if (tb[RTA_EXPIRES]) {
3719 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
3720
3721 if (addrconf_finite_timeout(timeout)) {
3722 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
3723 cfg->fc_flags |= RTF_EXPIRES;
3724 }
3725 }
3726
86872cb5
TG
3727 err = 0;
3728errout:
3729 return err;
1da177e4
LT
3730}
3731
6b9ea5a6
RP
3732struct rt6_nh {
3733 struct rt6_info *rt6_info;
3734 struct fib6_config r_cfg;
3735 struct mx6_config mxc;
3736 struct list_head next;
3737};
3738
3739static void ip6_print_replace_route_err(struct list_head *rt6_nh_list)
3740{
3741 struct rt6_nh *nh;
3742
3743 list_for_each_entry(nh, rt6_nh_list, next) {
7d4d5065 3744 pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n",
6b9ea5a6
RP
3745 &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway,
3746 nh->r_cfg.fc_ifindex);
3747 }
3748}
3749
3750static int ip6_route_info_append(struct list_head *rt6_nh_list,
3751 struct rt6_info *rt, struct fib6_config *r_cfg)
3752{
3753 struct rt6_nh *nh;
6b9ea5a6
RP
3754 int err = -EEXIST;
3755
3756 list_for_each_entry(nh, rt6_nh_list, next) {
3757 /* check if rt6_info already exists */
f06b7549 3758 if (rt6_duplicate_nexthop(nh->rt6_info, rt))
6b9ea5a6
RP
3759 return err;
3760 }
3761
3762 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
3763 if (!nh)
3764 return -ENOMEM;
3765 nh->rt6_info = rt;
3766 err = ip6_convert_metrics(&nh->mxc, r_cfg);
3767 if (err) {
3768 kfree(nh);
3769 return err;
3770 }
3771 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
3772 list_add_tail(&nh->next, rt6_nh_list);
3773
3774 return 0;
3775}
3776
3b1137fe
DA
3777static void ip6_route_mpath_notify(struct rt6_info *rt,
3778 struct rt6_info *rt_last,
3779 struct nl_info *info,
3780 __u16 nlflags)
3781{
3782 /* if this is an APPEND route, then rt points to the first route
3783 * inserted and rt_last points to last route inserted. Userspace
3784 * wants a consistent dump of the route which starts at the first
3785 * nexthop. Since sibling routes are always added at the end of
3786 * the list, find the first sibling of the last route appended
3787 */
3788 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) {
3789 rt = list_first_entry(&rt_last->rt6i_siblings,
3790 struct rt6_info,
3791 rt6i_siblings);
3792 }
3793
3794 if (rt)
3795 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
3796}
3797
333c4301
DA
3798static int ip6_route_multipath_add(struct fib6_config *cfg,
3799 struct netlink_ext_ack *extack)
51ebd318 3800{
3b1137fe
DA
3801 struct rt6_info *rt_notif = NULL, *rt_last = NULL;
3802 struct nl_info *info = &cfg->fc_nlinfo;
51ebd318
ND
3803 struct fib6_config r_cfg;
3804 struct rtnexthop *rtnh;
6b9ea5a6
RP
3805 struct rt6_info *rt;
3806 struct rt6_nh *err_nh;
3807 struct rt6_nh *nh, *nh_safe;
3b1137fe 3808 __u16 nlflags;
51ebd318
ND
3809 int remaining;
3810 int attrlen;
6b9ea5a6
RP
3811 int err = 1;
3812 int nhn = 0;
3813 int replace = (cfg->fc_nlinfo.nlh &&
3814 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
3815 LIST_HEAD(rt6_nh_list);
51ebd318 3816
3b1137fe
DA
3817 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
3818 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
3819 nlflags |= NLM_F_APPEND;
3820
35f1b4e9 3821 remaining = cfg->fc_mp_len;
51ebd318 3822 rtnh = (struct rtnexthop *)cfg->fc_mp;
51ebd318 3823
6b9ea5a6
RP
3824 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
3825 * rt6_info structs per nexthop
3826 */
51ebd318
ND
3827 while (rtnh_ok(rtnh, remaining)) {
3828 memcpy(&r_cfg, cfg, sizeof(*cfg));
3829 if (rtnh->rtnh_ifindex)
3830 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
3831
3832 attrlen = rtnh_attrlen(rtnh);
3833 if (attrlen > 0) {
3834 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
3835
3836 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
3837 if (nla) {
67b61f6c 3838 r_cfg.fc_gateway = nla_get_in6_addr(nla);
51ebd318
ND
3839 r_cfg.fc_flags |= RTF_GATEWAY;
3840 }
19e42e45
RP
3841 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
3842 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
3843 if (nla)
3844 r_cfg.fc_encap_type = nla_get_u16(nla);
51ebd318 3845 }
6b9ea5a6 3846
333c4301 3847 rt = ip6_route_info_create(&r_cfg, extack);
8c5b83f0
RP
3848 if (IS_ERR(rt)) {
3849 err = PTR_ERR(rt);
3850 rt = NULL;
6b9ea5a6 3851 goto cleanup;
8c5b83f0 3852 }
6b9ea5a6
RP
3853
3854 err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg);
51ebd318 3855 if (err) {
587fea74 3856 dst_release_immediate(&rt->dst);
6b9ea5a6
RP
3857 goto cleanup;
3858 }
3859
3860 rtnh = rtnh_next(rtnh, &remaining);
3861 }
3862
3b1137fe
DA
3863 /* for add and replace send one notification with all nexthops.
3864 * Skip the notification in fib6_add_rt2node and send one with
3865 * the full route when done
3866 */
3867 info->skip_notify = 1;
3868
6b9ea5a6
RP
3869 err_nh = NULL;
3870 list_for_each_entry(nh, &rt6_nh_list, next) {
333c4301 3871 err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack);
1d96d794
DA
3872
3873 if (!err) {
3874 /* save reference to last route successfully inserted */
3875 rt_last = nh->rt6_info;
3876
3877 /* save reference to first route for notification */
3878 if (!rt_notif)
3879 rt_notif = nh->rt6_info;
3880 }
3b1137fe 3881
6b9ea5a6
RP
3882 /* nh->rt6_info is used or freed at this point, reset to NULL*/
3883 nh->rt6_info = NULL;
3884 if (err) {
3885 if (replace && nhn)
3886 ip6_print_replace_route_err(&rt6_nh_list);
3887 err_nh = nh;
3888 goto add_errout;
51ebd318 3889 }
6b9ea5a6 3890
1a72418b 3891 /* Because each route is added like a single route we remove
27596472
MK
3892 * these flags after the first nexthop: if there is a collision,
3893 * we have already failed to add the first nexthop:
3894 * fib6_add_rt2node() has rejected it; when replacing, old
3895 * nexthops have been replaced by first new, the rest should
3896 * be added to it.
1a72418b 3897 */
27596472
MK
3898 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
3899 NLM_F_REPLACE);
6b9ea5a6
RP
3900 nhn++;
3901 }
3902
3b1137fe
DA
3903 /* success ... tell user about new route */
3904 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
6b9ea5a6
RP
3905 goto cleanup;
3906
3907add_errout:
3b1137fe
DA
3908 /* send notification for routes that were added so that
3909 * the delete notifications sent by ip6_route_del are
3910 * coherent
3911 */
3912 if (rt_notif)
3913 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
3914
6b9ea5a6
RP
3915 /* Delete routes that were already added */
3916 list_for_each_entry(nh, &rt6_nh_list, next) {
3917 if (err_nh == nh)
3918 break;
333c4301 3919 ip6_route_del(&nh->r_cfg, extack);
6b9ea5a6
RP
3920 }
3921
3922cleanup:
3923 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
587fea74
WW
3924 if (nh->rt6_info)
3925 dst_release_immediate(&nh->rt6_info->dst);
52fe51f8 3926 kfree(nh->mxc.mx);
6b9ea5a6
RP
3927 list_del(&nh->next);
3928 kfree(nh);
3929 }
3930
3931 return err;
3932}
3933
333c4301
DA
3934static int ip6_route_multipath_del(struct fib6_config *cfg,
3935 struct netlink_ext_ack *extack)
6b9ea5a6
RP
3936{
3937 struct fib6_config r_cfg;
3938 struct rtnexthop *rtnh;
3939 int remaining;
3940 int attrlen;
3941 int err = 1, last_err = 0;
3942
3943 remaining = cfg->fc_mp_len;
3944 rtnh = (struct rtnexthop *)cfg->fc_mp;
3945
3946 /* Parse a Multipath Entry */
3947 while (rtnh_ok(rtnh, remaining)) {
3948 memcpy(&r_cfg, cfg, sizeof(*cfg));
3949 if (rtnh->rtnh_ifindex)
3950 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
3951
3952 attrlen = rtnh_attrlen(rtnh);
3953 if (attrlen > 0) {
3954 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
3955
3956 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
3957 if (nla) {
3958 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
3959 r_cfg.fc_flags |= RTF_GATEWAY;
3960 }
3961 }
333c4301 3962 err = ip6_route_del(&r_cfg, extack);
6b9ea5a6
RP
3963 if (err)
3964 last_err = err;
3965
51ebd318
ND
3966 rtnh = rtnh_next(rtnh, &remaining);
3967 }
3968
3969 return last_err;
3970}
3971
c21ef3e3
DA
3972static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
3973 struct netlink_ext_ack *extack)
1da177e4 3974{
86872cb5
TG
3975 struct fib6_config cfg;
3976 int err;
1da177e4 3977
333c4301 3978 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
86872cb5
TG
3979 if (err < 0)
3980 return err;
3981
51ebd318 3982 if (cfg.fc_mp)
333c4301 3983 return ip6_route_multipath_del(&cfg, extack);
0ae81335
DA
3984 else {
3985 cfg.fc_delete_all_nh = 1;
333c4301 3986 return ip6_route_del(&cfg, extack);
0ae81335 3987 }
1da177e4
LT
3988}
3989
c21ef3e3
DA
3990static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
3991 struct netlink_ext_ack *extack)
1da177e4 3992{
86872cb5
TG
3993 struct fib6_config cfg;
3994 int err;
1da177e4 3995
333c4301 3996 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
86872cb5
TG
3997 if (err < 0)
3998 return err;
3999
51ebd318 4000 if (cfg.fc_mp)
333c4301 4001 return ip6_route_multipath_add(&cfg, extack);
51ebd318 4002 else
333c4301 4003 return ip6_route_add(&cfg, extack);
1da177e4
LT
4004}
4005
beb1afac 4006static size_t rt6_nlmsg_size(struct rt6_info *rt)
339bf98f 4007{
beb1afac
DA
4008 int nexthop_len = 0;
4009
4010 if (rt->rt6i_nsiblings) {
4011 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
4012 + NLA_ALIGN(sizeof(struct rtnexthop))
4013 + nla_total_size(16) /* RTA_GATEWAY */
beb1afac
DA
4014 + lwtunnel_get_encap_size(rt->dst.lwtstate);
4015
4016 nexthop_len *= rt->rt6i_nsiblings;
4017 }
4018
339bf98f
TG
4019 return NLMSG_ALIGN(sizeof(struct rtmsg))
4020 + nla_total_size(16) /* RTA_SRC */
4021 + nla_total_size(16) /* RTA_DST */
4022 + nla_total_size(16) /* RTA_GATEWAY */
4023 + nla_total_size(16) /* RTA_PREFSRC */
4024 + nla_total_size(4) /* RTA_TABLE */
4025 + nla_total_size(4) /* RTA_IIF */
4026 + nla_total_size(4) /* RTA_OIF */
4027 + nla_total_size(4) /* RTA_PRIORITY */
6a2b9ce0 4028 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
ea697639 4029 + nla_total_size(sizeof(struct rta_cacheinfo))
c78ba6d6 4030 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
19e42e45 4031 + nla_total_size(1) /* RTA_PREF */
beb1afac
DA
4032 + lwtunnel_get_encap_size(rt->dst.lwtstate)
4033 + nexthop_len;
4034}
4035
4036static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt,
5be083ce 4037 unsigned int *flags, bool skip_oif)
beb1afac
DA
4038{
4039 if (!netif_running(rt->dst.dev) || !netif_carrier_ok(rt->dst.dev)) {
4040 *flags |= RTNH_F_LINKDOWN;
4041 if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown)
4042 *flags |= RTNH_F_DEAD;
4043 }
4044
4045 if (rt->rt6i_flags & RTF_GATEWAY) {
4046 if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0)
4047 goto nla_put_failure;
4048 }
4049
fe400799 4050 if (rt->rt6i_nh_flags & RTNH_F_OFFLOAD)
61e4d01e
IS
4051 *flags |= RTNH_F_OFFLOAD;
4052
5be083ce
DA
4053 /* not needed for multipath encoding b/c it has a rtnexthop struct */
4054 if (!skip_oif && rt->dst.dev &&
beb1afac
DA
4055 nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
4056 goto nla_put_failure;
4057
4058 if (rt->dst.lwtstate &&
4059 lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0)
4060 goto nla_put_failure;
4061
4062 return 0;
4063
4064nla_put_failure:
4065 return -EMSGSIZE;
4066}
4067
5be083ce 4068/* add multipath next hop */
beb1afac
DA
4069static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt)
4070{
4071 struct rtnexthop *rtnh;
4072 unsigned int flags = 0;
4073
4074 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
4075 if (!rtnh)
4076 goto nla_put_failure;
4077
4078 rtnh->rtnh_hops = 0;
4079 rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0;
4080
5be083ce 4081 if (rt6_nexthop_info(skb, rt, &flags, true) < 0)
beb1afac
DA
4082 goto nla_put_failure;
4083
4084 rtnh->rtnh_flags = flags;
4085
4086 /* length of rtnetlink header + attributes */
4087 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
4088
4089 return 0;
4090
4091nla_put_failure:
4092 return -EMSGSIZE;
339bf98f
TG
4093}
4094
191cd582
BH
4095static int rt6_fill_node(struct net *net,
4096 struct sk_buff *skb, struct rt6_info *rt,
0d51aa80 4097 struct in6_addr *dst, struct in6_addr *src,
15e47304 4098 int iif, int type, u32 portid, u32 seq,
f8cfe2ce 4099 unsigned int flags)
1da177e4 4100{
4b32b5ad 4101 u32 metrics[RTAX_MAX];
1da177e4 4102 struct rtmsg *rtm;
2d7202bf 4103 struct nlmsghdr *nlh;
e3703b3d 4104 long expires;
9e762a4a 4105 u32 table;
1da177e4 4106
15e47304 4107 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
38308473 4108 if (!nlh)
26932566 4109 return -EMSGSIZE;
2d7202bf
TG
4110
4111 rtm = nlmsg_data(nlh);
1da177e4
LT
4112 rtm->rtm_family = AF_INET6;
4113 rtm->rtm_dst_len = rt->rt6i_dst.plen;
4114 rtm->rtm_src_len = rt->rt6i_src.plen;
4115 rtm->rtm_tos = 0;
c71099ac 4116 if (rt->rt6i_table)
9e762a4a 4117 table = rt->rt6i_table->tb6_id;
c71099ac 4118 else
9e762a4a 4119 table = RT6_TABLE_UNSPEC;
208d427f 4120 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
c78679e8
DM
4121 if (nla_put_u32(skb, RTA_TABLE, table))
4122 goto nla_put_failure;
ef2c7d7b
ND
4123 if (rt->rt6i_flags & RTF_REJECT) {
4124 switch (rt->dst.error) {
4125 case -EINVAL:
4126 rtm->rtm_type = RTN_BLACKHOLE;
4127 break;
4128 case -EACCES:
4129 rtm->rtm_type = RTN_PROHIBIT;
4130 break;
b4949ab2
ND
4131 case -EAGAIN:
4132 rtm->rtm_type = RTN_THROW;
4133 break;
ef2c7d7b
ND
4134 default:
4135 rtm->rtm_type = RTN_UNREACHABLE;
4136 break;
4137 }
4138 }
38308473 4139 else if (rt->rt6i_flags & RTF_LOCAL)
ab79ad14 4140 rtm->rtm_type = RTN_LOCAL;
4ee39733
DA
4141 else if (rt->rt6i_flags & RTF_ANYCAST)
4142 rtm->rtm_type = RTN_ANYCAST;
d1918542 4143 else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
1da177e4
LT
4144 rtm->rtm_type = RTN_LOCAL;
4145 else
4146 rtm->rtm_type = RTN_UNICAST;
4147 rtm->rtm_flags = 0;
4148 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
4149 rtm->rtm_protocol = rt->rt6i_protocol;
1da177e4 4150
38308473 4151 if (rt->rt6i_flags & RTF_CACHE)
1da177e4
LT
4152 rtm->rtm_flags |= RTM_F_CLONED;
4153
4154 if (dst) {
930345ea 4155 if (nla_put_in6_addr(skb, RTA_DST, dst))
c78679e8 4156 goto nla_put_failure;
1ab1457c 4157 rtm->rtm_dst_len = 128;
1da177e4 4158 } else if (rtm->rtm_dst_len)
930345ea 4159 if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr))
c78679e8 4160 goto nla_put_failure;
1da177e4
LT
4161#ifdef CONFIG_IPV6_SUBTREES
4162 if (src) {
930345ea 4163 if (nla_put_in6_addr(skb, RTA_SRC, src))
c78679e8 4164 goto nla_put_failure;
1ab1457c 4165 rtm->rtm_src_len = 128;
c78679e8 4166 } else if (rtm->rtm_src_len &&
930345ea 4167 nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr))
c78679e8 4168 goto nla_put_failure;
1da177e4 4169#endif
7bc570c8
YH
4170 if (iif) {
4171#ifdef CONFIG_IPV6_MROUTE
4172 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
fd61c6ba
DA
4173 int err = ip6mr_get_route(net, skb, rtm, portid);
4174
4175 if (err == 0)
4176 return 0;
4177 if (err < 0)
4178 goto nla_put_failure;
7bc570c8
YH
4179 } else
4180#endif
c78679e8
DM
4181 if (nla_put_u32(skb, RTA_IIF, iif))
4182 goto nla_put_failure;
7bc570c8 4183 } else if (dst) {
1da177e4 4184 struct in6_addr saddr_buf;
c78679e8 4185 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
930345ea 4186 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
c78679e8 4187 goto nla_put_failure;
1da177e4 4188 }
2d7202bf 4189
c3968a85
DW
4190 if (rt->rt6i_prefsrc.plen) {
4191 struct in6_addr saddr_buf;
4e3fd7a0 4192 saddr_buf = rt->rt6i_prefsrc.addr;
930345ea 4193 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
c78679e8 4194 goto nla_put_failure;
c3968a85
DW
4195 }
4196
4b32b5ad
MKL
4197 memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
4198 if (rt->rt6i_pmtu)
4199 metrics[RTAX_MTU - 1] = rt->rt6i_pmtu;
4200 if (rtnetlink_put_metrics(skb, metrics) < 0)
2d7202bf
TG
4201 goto nla_put_failure;
4202
c78679e8
DM
4203 if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
4204 goto nla_put_failure;
8253947e 4205
beb1afac
DA
4206 /* For multipath routes, walk the siblings list and add
4207 * each as a nexthop within RTA_MULTIPATH.
4208 */
4209 if (rt->rt6i_nsiblings) {
4210 struct rt6_info *sibling, *next_sibling;
4211 struct nlattr *mp;
4212
4213 mp = nla_nest_start(skb, RTA_MULTIPATH);
4214 if (!mp)
4215 goto nla_put_failure;
4216
4217 if (rt6_add_nexthop(skb, rt) < 0)
4218 goto nla_put_failure;
4219
4220 list_for_each_entry_safe(sibling, next_sibling,
4221 &rt->rt6i_siblings, rt6i_siblings) {
4222 if (rt6_add_nexthop(skb, sibling) < 0)
4223 goto nla_put_failure;
4224 }
4225
4226 nla_nest_end(skb, mp);
4227 } else {
5be083ce 4228 if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0)
beb1afac
DA
4229 goto nla_put_failure;
4230 }
4231
8253947e 4232 expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
69cdf8f9 4233
87a50699 4234 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
e3703b3d 4235 goto nla_put_failure;
2d7202bf 4236
c78ba6d6
LR
4237 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags)))
4238 goto nla_put_failure;
4239
19e42e45 4240
053c095a
JB
4241 nlmsg_end(skb, nlh);
4242 return 0;
2d7202bf
TG
4243
4244nla_put_failure:
26932566
PM
4245 nlmsg_cancel(skb, nlh);
4246 return -EMSGSIZE;
1da177e4
LT
4247}
4248
1b43af54 4249int rt6_dump_route(struct rt6_info *rt, void *p_arg)
1da177e4
LT
4250{
4251 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
1f17e2f2
DA
4252 struct net *net = arg->net;
4253
4254 if (rt == net->ipv6.ip6_null_entry)
4255 return 0;
1da177e4 4256
2d7202bf
TG
4257 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
4258 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
f8cfe2ce
DA
4259
4260 /* user wants prefix routes only */
4261 if (rtm->rtm_flags & RTM_F_PREFIX &&
4262 !(rt->rt6i_flags & RTF_PREFIX_RT)) {
4263 /* success since this is not a prefix route */
4264 return 1;
4265 }
4266 }
1da177e4 4267
1f17e2f2 4268 return rt6_fill_node(net,
191cd582 4269 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
15e47304 4270 NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
f8cfe2ce 4271 NLM_F_MULTI);
1da177e4
LT
4272}
4273
c21ef3e3
DA
4274static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4275 struct netlink_ext_ack *extack)
1da177e4 4276{
3b1e0a65 4277 struct net *net = sock_net(in_skb->sk);
ab364a6f 4278 struct nlattr *tb[RTA_MAX+1];
18c3a61c
RP
4279 int err, iif = 0, oif = 0;
4280 struct dst_entry *dst;
ab364a6f 4281 struct rt6_info *rt;
1da177e4 4282 struct sk_buff *skb;
ab364a6f 4283 struct rtmsg *rtm;
4c9483b2 4284 struct flowi6 fl6;
18c3a61c 4285 bool fibmatch;
1da177e4 4286
fceb6435 4287 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
c21ef3e3 4288 extack);
ab364a6f
TG
4289 if (err < 0)
4290 goto errout;
1da177e4 4291
ab364a6f 4292 err = -EINVAL;
4c9483b2 4293 memset(&fl6, 0, sizeof(fl6));
38b7097b
HFS
4294 rtm = nlmsg_data(nlh);
4295 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
18c3a61c 4296 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
1da177e4 4297
ab364a6f
TG
4298 if (tb[RTA_SRC]) {
4299 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4300 goto errout;
4301
4e3fd7a0 4302 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
ab364a6f
TG
4303 }
4304
4305 if (tb[RTA_DST]) {
4306 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4307 goto errout;
4308
4e3fd7a0 4309 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
ab364a6f
TG
4310 }
4311
4312 if (tb[RTA_IIF])
4313 iif = nla_get_u32(tb[RTA_IIF]);
4314
4315 if (tb[RTA_OIF])
72331bc0 4316 oif = nla_get_u32(tb[RTA_OIF]);
1da177e4 4317
2e47b291
LC
4318 if (tb[RTA_MARK])
4319 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
4320
622ec2c9
LC
4321 if (tb[RTA_UID])
4322 fl6.flowi6_uid = make_kuid(current_user_ns(),
4323 nla_get_u32(tb[RTA_UID]));
4324 else
4325 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
4326
1da177e4
LT
4327 if (iif) {
4328 struct net_device *dev;
72331bc0
SL
4329 int flags = 0;
4330
121622db
FW
4331 rcu_read_lock();
4332
4333 dev = dev_get_by_index_rcu(net, iif);
1da177e4 4334 if (!dev) {
121622db 4335 rcu_read_unlock();
1da177e4 4336 err = -ENODEV;
ab364a6f 4337 goto errout;
1da177e4 4338 }
72331bc0
SL
4339
4340 fl6.flowi6_iif = iif;
4341
4342 if (!ipv6_addr_any(&fl6.saddr))
4343 flags |= RT6_LOOKUP_F_HAS_SADDR;
4344
58acfd71 4345 dst = ip6_route_input_lookup(net, dev, &fl6, flags);
121622db
FW
4346
4347 rcu_read_unlock();
72331bc0
SL
4348 } else {
4349 fl6.flowi6_oif = oif;
4350
58acfd71 4351 dst = ip6_route_output(net, NULL, &fl6);
18c3a61c
RP
4352 }
4353
18c3a61c
RP
4354
4355 rt = container_of(dst, struct rt6_info, dst);
4356 if (rt->dst.error) {
4357 err = rt->dst.error;
4358 ip6_rt_put(rt);
4359 goto errout;
1da177e4
LT
4360 }
4361
9d6acb3b
WC
4362 if (rt == net->ipv6.ip6_null_entry) {
4363 err = rt->dst.error;
4364 ip6_rt_put(rt);
4365 goto errout;
4366 }
4367
58acfd71
IS
4368 if (fibmatch && rt->dst.from) {
4369 struct rt6_info *ort = container_of(rt->dst.from,
4370 struct rt6_info, dst);
4371
4372 dst_hold(&ort->dst);
4373 ip6_rt_put(rt);
4374 rt = ort;
4375 }
4376
ab364a6f 4377 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
38308473 4378 if (!skb) {
94e187c0 4379 ip6_rt_put(rt);
ab364a6f
TG
4380 err = -ENOBUFS;
4381 goto errout;
4382 }
1da177e4 4383
d8d1f30b 4384 skb_dst_set(skb, &rt->dst);
18c3a61c
RP
4385 if (fibmatch)
4386 err = rt6_fill_node(net, skb, rt, NULL, NULL, iif,
4387 RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
4388 nlh->nlmsg_seq, 0);
4389 else
4390 err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
4391 RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
4392 nlh->nlmsg_seq, 0);
1da177e4 4393 if (err < 0) {
ab364a6f
TG
4394 kfree_skb(skb);
4395 goto errout;
1da177e4
LT
4396 }
4397
15e47304 4398 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
ab364a6f 4399errout:
1da177e4 4400 return err;
1da177e4
LT
4401}
4402
37a1d361
RP
4403void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info,
4404 unsigned int nlm_flags)
1da177e4
LT
4405{
4406 struct sk_buff *skb;
5578689a 4407 struct net *net = info->nl_net;
528c4ceb
DL
4408 u32 seq;
4409 int err;
4410
4411 err = -ENOBUFS;
38308473 4412 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
86872cb5 4413
19e42e45 4414 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
38308473 4415 if (!skb)
21713ebc
TG
4416 goto errout;
4417
191cd582 4418 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
f8cfe2ce 4419 event, info->portid, seq, nlm_flags);
26932566
PM
4420 if (err < 0) {
4421 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
4422 WARN_ON(err == -EMSGSIZE);
4423 kfree_skb(skb);
4424 goto errout;
4425 }
15e47304 4426 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
1ce85fe4
PNA
4427 info->nlh, gfp_any());
4428 return;
21713ebc
TG
4429errout:
4430 if (err < 0)
5578689a 4431 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
1da177e4
LT
4432}
4433
8ed67789 4434static int ip6_route_dev_notify(struct notifier_block *this,
351638e7 4435 unsigned long event, void *ptr)
8ed67789 4436{
351638e7 4437 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
c346dca1 4438 struct net *net = dev_net(dev);
8ed67789 4439
242d3a49
WC
4440 if (!(dev->flags & IFF_LOOPBACK))
4441 return NOTIFY_OK;
4442
4443 if (event == NETDEV_REGISTER) {
d8d1f30b 4444 net->ipv6.ip6_null_entry->dst.dev = dev;
8ed67789
DL
4445 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
4446#ifdef CONFIG_IPV6_MULTIPLE_TABLES
d8d1f30b 4447 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
8ed67789 4448 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
d8d1f30b 4449 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
8ed67789 4450 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
242d3a49 4451#endif
76da0704
WC
4452 } else if (event == NETDEV_UNREGISTER &&
4453 dev->reg_state != NETREG_UNREGISTERED) {
4454 /* NETDEV_UNREGISTER could be fired for multiple times by
4455 * netdev_wait_allrefs(). Make sure we only call this once.
4456 */
12d94a80 4457 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
242d3a49 4458#ifdef CONFIG_IPV6_MULTIPLE_TABLES
12d94a80
ED
4459 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
4460 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
8ed67789
DL
4461#endif
4462 }
4463
4464 return NOTIFY_OK;
4465}
4466
1da177e4
LT
4467/*
4468 * /proc
4469 */
4470
4471#ifdef CONFIG_PROC_FS
4472
33120b30
AD
4473static const struct file_operations ipv6_route_proc_fops = {
4474 .owner = THIS_MODULE,
4475 .open = ipv6_route_open,
4476 .read = seq_read,
4477 .llseek = seq_lseek,
8d2ca1d7 4478 .release = seq_release_net,
33120b30
AD
4479};
4480
1da177e4
LT
4481static int rt6_stats_seq_show(struct seq_file *seq, void *v)
4482{
69ddb805 4483 struct net *net = (struct net *)seq->private;
1da177e4 4484 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
69ddb805
DL
4485 net->ipv6.rt6_stats->fib_nodes,
4486 net->ipv6.rt6_stats->fib_route_nodes,
81eb8447 4487 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
69ddb805
DL
4488 net->ipv6.rt6_stats->fib_rt_entries,
4489 net->ipv6.rt6_stats->fib_rt_cache,
fc66f95c 4490 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
69ddb805 4491 net->ipv6.rt6_stats->fib_discarded_routes);
1da177e4
LT
4492
4493 return 0;
4494}
4495
4496static int rt6_stats_seq_open(struct inode *inode, struct file *file)
4497{
de05c557 4498 return single_open_net(inode, file, rt6_stats_seq_show);
69ddb805
DL
4499}
4500
9a32144e 4501static const struct file_operations rt6_stats_seq_fops = {
1da177e4
LT
4502 .owner = THIS_MODULE,
4503 .open = rt6_stats_seq_open,
4504 .read = seq_read,
4505 .llseek = seq_lseek,
b6fcbdb4 4506 .release = single_release_net,
1da177e4
LT
4507};
4508#endif /* CONFIG_PROC_FS */
4509
4510#ifdef CONFIG_SYSCTL
4511
1da177e4 4512static
fe2c6338 4513int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
1da177e4
LT
4514 void __user *buffer, size_t *lenp, loff_t *ppos)
4515{
c486da34
LAG
4516 struct net *net;
4517 int delay;
4518 if (!write)
1da177e4 4519 return -EINVAL;
c486da34
LAG
4520
4521 net = (struct net *)ctl->extra1;
4522 delay = net->ipv6.sysctl.flush_delay;
4523 proc_dointvec(ctl, write, buffer, lenp, ppos);
2ac3ac8f 4524 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
c486da34 4525 return 0;
1da177e4
LT
4526}
4527
fe2c6338 4528struct ctl_table ipv6_route_table_template[] = {
1ab1457c 4529 {
1da177e4 4530 .procname = "flush",
4990509f 4531 .data = &init_net.ipv6.sysctl.flush_delay,
1da177e4 4532 .maxlen = sizeof(int),
89c8b3a1 4533 .mode = 0200,
6d9f239a 4534 .proc_handler = ipv6_sysctl_rtcache_flush
1da177e4
LT
4535 },
4536 {
1da177e4 4537 .procname = "gc_thresh",
9a7ec3a9 4538 .data = &ip6_dst_ops_template.gc_thresh,
1da177e4
LT
4539 .maxlen = sizeof(int),
4540 .mode = 0644,
6d9f239a 4541 .proc_handler = proc_dointvec,
1da177e4
LT
4542 },
4543 {
1da177e4 4544 .procname = "max_size",
4990509f 4545 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
1da177e4
LT
4546 .maxlen = sizeof(int),
4547 .mode = 0644,
6d9f239a 4548 .proc_handler = proc_dointvec,
1da177e4
LT
4549 },
4550 {
1da177e4 4551 .procname = "gc_min_interval",
4990509f 4552 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
1da177e4
LT
4553 .maxlen = sizeof(int),
4554 .mode = 0644,
6d9f239a 4555 .proc_handler = proc_dointvec_jiffies,
1da177e4
LT
4556 },
4557 {
1da177e4 4558 .procname = "gc_timeout",
4990509f 4559 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
1da177e4
LT
4560 .maxlen = sizeof(int),
4561 .mode = 0644,
6d9f239a 4562 .proc_handler = proc_dointvec_jiffies,
1da177e4
LT
4563 },
4564 {
1da177e4 4565 .procname = "gc_interval",
4990509f 4566 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
1da177e4
LT
4567 .maxlen = sizeof(int),
4568 .mode = 0644,
6d9f239a 4569 .proc_handler = proc_dointvec_jiffies,
1da177e4
LT
4570 },
4571 {
1da177e4 4572 .procname = "gc_elasticity",
4990509f 4573 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
1da177e4
LT
4574 .maxlen = sizeof(int),
4575 .mode = 0644,
f3d3f616 4576 .proc_handler = proc_dointvec,
1da177e4
LT
4577 },
4578 {
1da177e4 4579 .procname = "mtu_expires",
4990509f 4580 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
1da177e4
LT
4581 .maxlen = sizeof(int),
4582 .mode = 0644,
6d9f239a 4583 .proc_handler = proc_dointvec_jiffies,
1da177e4
LT
4584 },
4585 {
1da177e4 4586 .procname = "min_adv_mss",
4990509f 4587 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
1da177e4
LT
4588 .maxlen = sizeof(int),
4589 .mode = 0644,
f3d3f616 4590 .proc_handler = proc_dointvec,
1da177e4
LT
4591 },
4592 {
1da177e4 4593 .procname = "gc_min_interval_ms",
4990509f 4594 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
1da177e4
LT
4595 .maxlen = sizeof(int),
4596 .mode = 0644,
6d9f239a 4597 .proc_handler = proc_dointvec_ms_jiffies,
1da177e4 4598 },
f8572d8f 4599 { }
1da177e4
LT
4600};
4601
2c8c1e72 4602struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
760f2d01
DL
4603{
4604 struct ctl_table *table;
4605
4606 table = kmemdup(ipv6_route_table_template,
4607 sizeof(ipv6_route_table_template),
4608 GFP_KERNEL);
5ee09105
YH
4609
4610 if (table) {
4611 table[0].data = &net->ipv6.sysctl.flush_delay;
c486da34 4612 table[0].extra1 = net;
86393e52 4613 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
5ee09105
YH
4614 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
4615 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
4616 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
4617 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
4618 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
4619 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
4620 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
9c69fabe 4621 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
464dc801
EB
4622
4623 /* Don't export sysctls to unprivileged users */
4624 if (net->user_ns != &init_user_ns)
4625 table[0].procname = NULL;
5ee09105
YH
4626 }
4627
760f2d01
DL
4628 return table;
4629}
1da177e4
LT
4630#endif
4631
2c8c1e72 4632static int __net_init ip6_route_net_init(struct net *net)
cdb18761 4633{
633d424b 4634 int ret = -ENOMEM;
8ed67789 4635
86393e52
AD
4636 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
4637 sizeof(net->ipv6.ip6_dst_ops));
f2fc6a54 4638
fc66f95c
ED
4639 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
4640 goto out_ip6_dst_ops;
4641
8ed67789
DL
4642 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
4643 sizeof(*net->ipv6.ip6_null_entry),
4644 GFP_KERNEL);
4645 if (!net->ipv6.ip6_null_entry)
fc66f95c 4646 goto out_ip6_dst_entries;
d8d1f30b 4647 net->ipv6.ip6_null_entry->dst.path =
8ed67789 4648 (struct dst_entry *)net->ipv6.ip6_null_entry;
d8d1f30b 4649 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
62fa8a84
DM
4650 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
4651 ip6_template_metrics, true);
8ed67789
DL
4652
4653#ifdef CONFIG_IPV6_MULTIPLE_TABLES
feca7d8c 4654 net->ipv6.fib6_has_custom_rules = false;
8ed67789
DL
4655 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
4656 sizeof(*net->ipv6.ip6_prohibit_entry),
4657 GFP_KERNEL);
68fffc67
PZ
4658 if (!net->ipv6.ip6_prohibit_entry)
4659 goto out_ip6_null_entry;
d8d1f30b 4660 net->ipv6.ip6_prohibit_entry->dst.path =
8ed67789 4661 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
d8d1f30b 4662 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
62fa8a84
DM
4663 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
4664 ip6_template_metrics, true);
8ed67789
DL
4665
4666 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
4667 sizeof(*net->ipv6.ip6_blk_hole_entry),
4668 GFP_KERNEL);
68fffc67
PZ
4669 if (!net->ipv6.ip6_blk_hole_entry)
4670 goto out_ip6_prohibit_entry;
d8d1f30b 4671 net->ipv6.ip6_blk_hole_entry->dst.path =
8ed67789 4672 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
d8d1f30b 4673 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
62fa8a84
DM
4674 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
4675 ip6_template_metrics, true);
8ed67789
DL
4676#endif
4677
b339a47c
PZ
4678 net->ipv6.sysctl.flush_delay = 0;
4679 net->ipv6.sysctl.ip6_rt_max_size = 4096;
4680 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
4681 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
4682 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
4683 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
4684 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
4685 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
4686
6891a346
BT
4687 net->ipv6.ip6_rt_gc_expire = 30*HZ;
4688
8ed67789
DL
4689 ret = 0;
4690out:
4691 return ret;
f2fc6a54 4692
68fffc67
PZ
4693#ifdef CONFIG_IPV6_MULTIPLE_TABLES
4694out_ip6_prohibit_entry:
4695 kfree(net->ipv6.ip6_prohibit_entry);
4696out_ip6_null_entry:
4697 kfree(net->ipv6.ip6_null_entry);
4698#endif
fc66f95c
ED
4699out_ip6_dst_entries:
4700 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
f2fc6a54 4701out_ip6_dst_ops:
f2fc6a54 4702 goto out;
cdb18761
DL
4703}
4704
2c8c1e72 4705static void __net_exit ip6_route_net_exit(struct net *net)
cdb18761 4706{
8ed67789
DL
4707 kfree(net->ipv6.ip6_null_entry);
4708#ifdef CONFIG_IPV6_MULTIPLE_TABLES
4709 kfree(net->ipv6.ip6_prohibit_entry);
4710 kfree(net->ipv6.ip6_blk_hole_entry);
4711#endif
41bb78b4 4712 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
cdb18761
DL
4713}
4714
d189634e
TG
4715static int __net_init ip6_route_net_init_late(struct net *net)
4716{
4717#ifdef CONFIG_PROC_FS
d4beaa66
G
4718 proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
4719 proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
d189634e
TG
4720#endif
4721 return 0;
4722}
4723
4724static void __net_exit ip6_route_net_exit_late(struct net *net)
4725{
4726#ifdef CONFIG_PROC_FS
ece31ffd
G
4727 remove_proc_entry("ipv6_route", net->proc_net);
4728 remove_proc_entry("rt6_stats", net->proc_net);
d189634e
TG
4729#endif
4730}
4731
cdb18761
DL
4732static struct pernet_operations ip6_route_net_ops = {
4733 .init = ip6_route_net_init,
4734 .exit = ip6_route_net_exit,
4735};
4736
c3426b47
DM
4737static int __net_init ipv6_inetpeer_init(struct net *net)
4738{
4739 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
4740
4741 if (!bp)
4742 return -ENOMEM;
4743 inet_peer_base_init(bp);
4744 net->ipv6.peers = bp;
4745 return 0;
4746}
4747
4748static void __net_exit ipv6_inetpeer_exit(struct net *net)
4749{
4750 struct inet_peer_base *bp = net->ipv6.peers;
4751
4752 net->ipv6.peers = NULL;
56a6b248 4753 inetpeer_invalidate_tree(bp);
c3426b47
DM
4754 kfree(bp);
4755}
4756
2b823f72 4757static struct pernet_operations ipv6_inetpeer_ops = {
c3426b47
DM
4758 .init = ipv6_inetpeer_init,
4759 .exit = ipv6_inetpeer_exit,
4760};
4761
d189634e
TG
4762static struct pernet_operations ip6_route_net_late_ops = {
4763 .init = ip6_route_net_init_late,
4764 .exit = ip6_route_net_exit_late,
4765};
4766
8ed67789
DL
4767static struct notifier_block ip6_route_dev_notifier = {
4768 .notifier_call = ip6_route_dev_notify,
242d3a49 4769 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
8ed67789
DL
4770};
4771
2f460933
WC
4772void __init ip6_route_init_special_entries(void)
4773{
4774 /* Registering of the loopback is done before this portion of code,
4775 * the loopback reference in rt6_info will not be taken, do it
4776 * manually for init_net */
4777 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
4778 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
4779 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
4780 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
4781 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
4782 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
4783 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
4784 #endif
4785}
4786
433d49c3 4787int __init ip6_route_init(void)
1da177e4 4788{
433d49c3 4789 int ret;
8d0b94af 4790 int cpu;
433d49c3 4791
9a7ec3a9
DL
4792 ret = -ENOMEM;
4793 ip6_dst_ops_template.kmem_cachep =
e5d679f3 4794 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
f845ab6b 4795 SLAB_HWCACHE_ALIGN, NULL);
9a7ec3a9 4796 if (!ip6_dst_ops_template.kmem_cachep)
c19a28e1 4797 goto out;
14e50e57 4798
fc66f95c 4799 ret = dst_entries_init(&ip6_dst_blackhole_ops);
8ed67789 4800 if (ret)
bdb3289f 4801 goto out_kmem_cache;
bdb3289f 4802
c3426b47
DM
4803 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
4804 if (ret)
e8803b6c 4805 goto out_dst_entries;
2a0c451a 4806
7e52b33b
DM
4807 ret = register_pernet_subsys(&ip6_route_net_ops);
4808 if (ret)
4809 goto out_register_inetpeer;
c3426b47 4810
5dc121e9
AE
4811 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
4812
e8803b6c 4813 ret = fib6_init();
433d49c3 4814 if (ret)
8ed67789 4815 goto out_register_subsys;
433d49c3 4816
433d49c3
DL
4817 ret = xfrm6_init();
4818 if (ret)
e8803b6c 4819 goto out_fib6_init;
c35b7e72 4820
433d49c3
DL
4821 ret = fib6_rules_init();
4822 if (ret)
4823 goto xfrm6_init;
7e5449c2 4824
d189634e
TG
4825 ret = register_pernet_subsys(&ip6_route_net_late_ops);
4826 if (ret)
4827 goto fib6_rules_init;
4828
433d49c3 4829 ret = -ENOBUFS;
b97bac64
FW
4830 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, 0) ||
4831 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, 0) ||
e3a22b7f
FW
4832 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL,
4833 RTNL_FLAG_DOIT_UNLOCKED))
d189634e 4834 goto out_register_late_subsys;
c127ea2c 4835
8ed67789 4836 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
cdb18761 4837 if (ret)
d189634e 4838 goto out_register_late_subsys;
8ed67789 4839
8d0b94af
MKL
4840 for_each_possible_cpu(cpu) {
4841 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
4842
4843 INIT_LIST_HEAD(&ul->head);
4844 spin_lock_init(&ul->lock);
4845 }
4846
433d49c3
DL
4847out:
4848 return ret;
4849
d189634e
TG
4850out_register_late_subsys:
4851 unregister_pernet_subsys(&ip6_route_net_late_ops);
433d49c3 4852fib6_rules_init:
433d49c3
DL
4853 fib6_rules_cleanup();
4854xfrm6_init:
433d49c3 4855 xfrm6_fini();
2a0c451a
TG
4856out_fib6_init:
4857 fib6_gc_cleanup();
8ed67789
DL
4858out_register_subsys:
4859 unregister_pernet_subsys(&ip6_route_net_ops);
7e52b33b
DM
4860out_register_inetpeer:
4861 unregister_pernet_subsys(&ipv6_inetpeer_ops);
fc66f95c
ED
4862out_dst_entries:
4863 dst_entries_destroy(&ip6_dst_blackhole_ops);
433d49c3 4864out_kmem_cache:
f2fc6a54 4865 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
433d49c3 4866 goto out;
1da177e4
LT
4867}
4868
4869void ip6_route_cleanup(void)
4870{
8ed67789 4871 unregister_netdevice_notifier(&ip6_route_dev_notifier);
d189634e 4872 unregister_pernet_subsys(&ip6_route_net_late_ops);
101367c2 4873 fib6_rules_cleanup();
1da177e4 4874 xfrm6_fini();
1da177e4 4875 fib6_gc_cleanup();
c3426b47 4876 unregister_pernet_subsys(&ipv6_inetpeer_ops);
8ed67789 4877 unregister_pernet_subsys(&ip6_route_net_ops);
41bb78b4 4878 dst_entries_destroy(&ip6_dst_blackhole_ops);
f2fc6a54 4879 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
1da177e4 4880}