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