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