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