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