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