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);
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 if (rinfo->length < 2) {
992 return -EINVAL;
993 }
994 } else if (rinfo->prefix_len > 0) {
995 if (rinfo->length < 1) {
996 return -EINVAL;
997 }
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(net, rt, false);
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)3976 static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
3977 {
3978 struct net *net = info->nl_net;
3979 struct fib6_table *table;
3980 int err;
3981
3982 if (rt == net->ipv6.fib6_null_entry) {
3983 err = -ENOENT;
3984 goto out;
3985 }
3986
3987 table = rt->fib6_table;
3988 spin_lock_bh(&table->tb6_lock);
3989 err = fib6_del(rt, info);
3990 spin_unlock_bh(&table->tb6_lock);
3991
3992 out:
3993 fib6_info_release(rt);
3994 return err;
3995 }
3996
ip6_del_rt(struct net * net,struct fib6_info * rt,bool skip_notify)3997 int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify)
3998 {
3999 struct nl_info info = {
4000 .nl_net = net,
4001 .skip_notify = skip_notify
4002 };
4003
4004 return __ip6_del_rt(rt, &info);
4005 }
4006
__ip6_del_rt_siblings(struct fib6_info * rt,struct fib6_config * cfg)4007 static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
4008 {
4009 struct nl_info *info = &cfg->fc_nlinfo;
4010 struct net *net = info->nl_net;
4011 struct sk_buff *skb = NULL;
4012 struct fib6_table *table;
4013 int err = -ENOENT;
4014
4015 if (rt == net->ipv6.fib6_null_entry)
4016 goto out_put;
4017 table = rt->fib6_table;
4018 spin_lock_bh(&table->tb6_lock);
4019
4020 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
4021 struct fib6_info *sibling, *next_sibling;
4022 struct fib6_node *fn;
4023
4024 /* prefer to send a single notification with all hops */
4025 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
4026 if (skb) {
4027 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
4028
4029 if (rt6_fill_node(net, skb, rt, NULL,
4030 NULL, NULL, 0, RTM_DELROUTE,
4031 info->portid, seq, 0) < 0) {
4032 kfree_skb(skb);
4033 skb = NULL;
4034 } else
4035 info->skip_notify = 1;
4036 }
4037
4038 /* 'rt' points to the first sibling route. If it is not the
4039 * leaf, then we do not need to send a notification. Otherwise,
4040 * we need to check if the last sibling has a next route or not
4041 * and emit a replace or delete notification, respectively.
4042 */
4043 info->skip_notify_kernel = 1;
4044 fn = rcu_dereference_protected(rt->fib6_node,
4045 lockdep_is_held(&table->tb6_lock));
4046 if (rcu_access_pointer(fn->leaf) == rt) {
4047 struct fib6_info *last_sibling, *replace_rt;
4048
4049 last_sibling = list_last_entry(&rt->fib6_siblings,
4050 struct fib6_info,
4051 fib6_siblings);
4052 replace_rt = rcu_dereference_protected(
4053 last_sibling->fib6_next,
4054 lockdep_is_held(&table->tb6_lock));
4055 if (replace_rt)
4056 call_fib6_entry_notifiers_replace(net,
4057 replace_rt);
4058 else
4059 call_fib6_multipath_entry_notifiers(net,
4060 FIB_EVENT_ENTRY_DEL,
4061 rt, rt->fib6_nsiblings,
4062 NULL);
4063 }
4064 list_for_each_entry_safe(sibling, next_sibling,
4065 &rt->fib6_siblings,
4066 fib6_siblings) {
4067 err = fib6_del(sibling, info);
4068 if (err)
4069 goto out_unlock;
4070 }
4071 }
4072
4073 err = fib6_del(rt, info);
4074 out_unlock:
4075 spin_unlock_bh(&table->tb6_lock);
4076 out_put:
4077 fib6_info_release(rt);
4078
4079 if (skb) {
4080 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
4081 info->nlh, gfp_any());
4082 }
4083 return err;
4084 }
4085
__ip6_del_cached_rt(struct rt6_info * rt,struct fib6_config * cfg)4086 static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
4087 {
4088 int rc = -ESRCH;
4089
4090 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
4091 goto out;
4092
4093 if (cfg->fc_flags & RTF_GATEWAY &&
4094 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
4095 goto out;
4096
4097 rc = rt6_remove_exception_rt(rt);
4098 out:
4099 return rc;
4100 }
4101
ip6_del_cached_rt(struct fib6_config * cfg,struct fib6_info * rt,struct fib6_nh * nh)4102 static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
4103 struct fib6_nh *nh)
4104 {
4105 struct fib6_result res = {
4106 .f6i = rt,
4107 .nh = nh,
4108 };
4109 struct rt6_info *rt_cache;
4110
4111 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
4112 if (rt_cache)
4113 return __ip6_del_cached_rt(rt_cache, cfg);
4114
4115 return 0;
4116 }
4117
4118 struct fib6_nh_del_cached_rt_arg {
4119 struct fib6_config *cfg;
4120 struct fib6_info *f6i;
4121 };
4122
fib6_nh_del_cached_rt(struct fib6_nh * nh,void * _arg)4123 static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
4124 {
4125 struct fib6_nh_del_cached_rt_arg *arg = _arg;
4126 int rc;
4127
4128 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
4129 return rc != -ESRCH ? rc : 0;
4130 }
4131
ip6_del_cached_rt_nh(struct fib6_config * cfg,struct fib6_info * f6i)4132 static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
4133 {
4134 struct fib6_nh_del_cached_rt_arg arg = {
4135 .cfg = cfg,
4136 .f6i = f6i
4137 };
4138
4139 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
4140 }
4141
ip6_route_del(struct fib6_config * cfg,struct netlink_ext_ack * extack)4142 static int ip6_route_del(struct fib6_config *cfg,
4143 struct netlink_ext_ack *extack)
4144 {
4145 struct fib6_table *table;
4146 struct fib6_info *rt;
4147 struct fib6_node *fn;
4148 int err = -ESRCH;
4149
4150 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
4151 if (!table) {
4152 NL_SET_ERR_MSG(extack, "FIB table does not exist");
4153 return err;
4154 }
4155
4156 rcu_read_lock();
4157
4158 fn = fib6_locate(&table->tb6_root,
4159 &cfg->fc_dst, cfg->fc_dst_len,
4160 &cfg->fc_src, cfg->fc_src_len,
4161 !(cfg->fc_flags & RTF_CACHE));
4162
4163 if (fn) {
4164 for_each_fib6_node_rt_rcu(fn) {
4165 struct fib6_nh *nh;
4166
4167 if (rt->nh && cfg->fc_nh_id &&
4168 rt->nh->id != cfg->fc_nh_id)
4169 continue;
4170
4171 if (cfg->fc_flags & RTF_CACHE) {
4172 int rc = 0;
4173
4174 if (rt->nh) {
4175 rc = ip6_del_cached_rt_nh(cfg, rt);
4176 } else if (cfg->fc_nh_id) {
4177 continue;
4178 } else {
4179 nh = rt->fib6_nh;
4180 rc = ip6_del_cached_rt(cfg, rt, nh);
4181 }
4182 if (rc != -ESRCH) {
4183 rcu_read_unlock();
4184 return rc;
4185 }
4186 continue;
4187 }
4188
4189 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
4190 continue;
4191 if (cfg->fc_protocol &&
4192 cfg->fc_protocol != rt->fib6_protocol)
4193 continue;
4194
4195 if (rt->nh) {
4196 if (!fib6_info_hold_safe(rt))
4197 continue;
4198
4199 err = __ip6_del_rt(rt, &cfg->fc_nlinfo);
4200 break;
4201 }
4202 if (cfg->fc_nh_id)
4203 continue;
4204
4205 nh = rt->fib6_nh;
4206 if (cfg->fc_ifindex &&
4207 (!nh->fib_nh_dev ||
4208 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
4209 continue;
4210 if (cfg->fc_flags & RTF_GATEWAY &&
4211 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
4212 continue;
4213 if (!fib6_info_hold_safe(rt))
4214 continue;
4215
4216 /* if gateway was specified only delete the one hop */
4217 if (cfg->fc_flags & RTF_GATEWAY)
4218 err = __ip6_del_rt(rt, &cfg->fc_nlinfo);
4219 else
4220 err = __ip6_del_rt_siblings(rt, cfg);
4221 break;
4222 }
4223 }
4224 rcu_read_unlock();
4225
4226 return err;
4227 }
4228
rt6_do_redirect(struct dst_entry * dst,struct sock * sk,struct sk_buff * skb)4229 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
4230 {
4231 struct netevent_redirect netevent;
4232 struct rt6_info *rt, *nrt = NULL;
4233 struct fib6_result res = {};
4234 struct ndisc_options ndopts;
4235 struct inet6_dev *in6_dev;
4236 struct neighbour *neigh;
4237 struct rd_msg *msg;
4238 int optlen, on_link;
4239 u8 *lladdr;
4240
4241 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
4242 optlen -= sizeof(*msg);
4243
4244 if (optlen < 0) {
4245 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
4246 return;
4247 }
4248
4249 msg = (struct rd_msg *)icmp6_hdr(skb);
4250
4251 if (ipv6_addr_is_multicast(&msg->dest)) {
4252 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
4253 return;
4254 }
4255
4256 on_link = 0;
4257 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
4258 on_link = 1;
4259 } else if (ipv6_addr_type(&msg->target) !=
4260 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
4261 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
4262 return;
4263 }
4264
4265 in6_dev = __in6_dev_get(skb->dev);
4266 if (!in6_dev)
4267 return;
4268 if (READ_ONCE(in6_dev->cnf.forwarding) ||
4269 !READ_ONCE(in6_dev->cnf.accept_redirects))
4270 return;
4271
4272 /* RFC2461 8.1:
4273 * The IP source address of the Redirect MUST be the same as the current
4274 * first-hop router for the specified ICMP Destination Address.
4275 */
4276
4277 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
4278 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4279 return;
4280 }
4281
4282 lladdr = NULL;
4283 if (ndopts.nd_opts_tgt_lladdr) {
4284 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4285 skb->dev);
4286 if (!lladdr) {
4287 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4288 return;
4289 }
4290 }
4291
4292 rt = dst_rt6_info(dst);
4293 if (rt->rt6i_flags & RTF_REJECT) {
4294 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4295 return;
4296 }
4297
4298 /* Redirect received -> path was valid.
4299 * Look, redirects are sent only in response to data packets,
4300 * so that this nexthop apparently is reachable. --ANK
4301 */
4302 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
4303
4304 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
4305 if (!neigh)
4306 return;
4307
4308 /*
4309 * We have finally decided to accept it.
4310 */
4311
4312 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
4313 NEIGH_UPDATE_F_WEAK_OVERRIDE|
4314 NEIGH_UPDATE_F_OVERRIDE|
4315 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
4316 NEIGH_UPDATE_F_ISROUTER)),
4317 NDISC_REDIRECT, &ndopts);
4318
4319 rcu_read_lock();
4320 res.f6i = rcu_dereference(rt->from);
4321 if (!res.f6i)
4322 goto out;
4323
4324 if (res.f6i->nh) {
4325 struct fib6_nh_match_arg arg = {
4326 .dev = dst_dev_rcu(dst),
4327 .gw = &rt->rt6i_gateway,
4328 };
4329
4330 nexthop_for_each_fib6_nh(res.f6i->nh,
4331 fib6_nh_find_match, &arg);
4332
4333 /* fib6_info uses a nexthop that does not have fib6_nh
4334 * using the dst->dev. Should be impossible
4335 */
4336 if (!arg.match)
4337 goto out;
4338 res.nh = arg.match;
4339 } else {
4340 res.nh = res.f6i->fib6_nh;
4341 }
4342
4343 res.fib6_flags = res.f6i->fib6_flags;
4344 res.fib6_type = res.f6i->fib6_type;
4345 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
4346 if (!nrt)
4347 goto out;
4348
4349 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4350 if (on_link)
4351 nrt->rt6i_flags &= ~RTF_GATEWAY;
4352
4353 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
4354
4355 /* rt6_insert_exception() will take care of duplicated exceptions */
4356 if (rt6_insert_exception(nrt, &res)) {
4357 dst_release_immediate(&nrt->dst);
4358 goto out;
4359 }
4360
4361 netevent.old = &rt->dst;
4362 netevent.new = &nrt->dst;
4363 netevent.daddr = &msg->dest;
4364 netevent.neigh = neigh;
4365 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4366
4367 out:
4368 rcu_read_unlock();
4369 neigh_release(neigh);
4370 }
4371
4372 #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)4373 static struct fib6_info *rt6_get_route_info(struct net *net,
4374 const struct in6_addr *prefix, int prefixlen,
4375 const struct in6_addr *gwaddr,
4376 struct net_device *dev)
4377 {
4378 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4379 int ifindex = dev->ifindex;
4380 struct fib6_node *fn;
4381 struct fib6_info *rt = NULL;
4382 struct fib6_table *table;
4383
4384 table = fib6_get_table(net, tb_id);
4385 if (!table)
4386 return NULL;
4387
4388 rcu_read_lock();
4389 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
4390 if (!fn)
4391 goto out;
4392
4393 for_each_fib6_node_rt_rcu(fn) {
4394 /* these routes do not use nexthops */
4395 if (rt->nh)
4396 continue;
4397 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
4398 continue;
4399 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
4400 !rt->fib6_nh->fib_nh_gw_family)
4401 continue;
4402 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
4403 continue;
4404 if (!fib6_info_hold_safe(rt))
4405 continue;
4406 break;
4407 }
4408 out:
4409 rcu_read_unlock();
4410 return rt;
4411 }
4412
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)4413 static struct fib6_info *rt6_add_route_info(struct net *net,
4414 const struct in6_addr *prefix, int prefixlen,
4415 const struct in6_addr *gwaddr,
4416 struct net_device *dev,
4417 unsigned int pref)
4418 {
4419 struct fib6_config cfg = {
4420 .fc_metric = IP6_RT_PRIO_USER,
4421 .fc_ifindex = dev->ifindex,
4422 .fc_dst_len = prefixlen,
4423 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4424 RTF_UP | RTF_PREF(pref),
4425 .fc_protocol = RTPROT_RA,
4426 .fc_type = RTN_UNICAST,
4427 .fc_nlinfo.portid = 0,
4428 .fc_nlinfo.nlh = NULL,
4429 .fc_nlinfo.nl_net = net,
4430 };
4431
4432 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4433 cfg.fc_dst = *prefix;
4434 cfg.fc_gateway = *gwaddr;
4435
4436 /* We should treat it as a default route if prefix length is 0. */
4437 if (!prefixlen)
4438 cfg.fc_flags |= RTF_DEFAULT;
4439
4440 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
4441
4442 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
4443 }
4444 #endif
4445
rt6_get_dflt_router(struct net * net,const struct in6_addr * addr,struct net_device * dev)4446 struct fib6_info *rt6_get_dflt_router(struct net *net,
4447 const struct in6_addr *addr,
4448 struct net_device *dev)
4449 {
4450 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
4451 struct fib6_info *rt;
4452 struct fib6_table *table;
4453
4454 table = fib6_get_table(net, tb_id);
4455 if (!table)
4456 return NULL;
4457
4458 rcu_read_lock();
4459 for_each_fib6_node_rt_rcu(&table->tb6_root) {
4460 struct fib6_nh *nh;
4461
4462 /* RA routes do not use nexthops */
4463 if (rt->nh)
4464 continue;
4465
4466 nh = rt->fib6_nh;
4467 if (dev == nh->fib_nh_dev &&
4468 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
4469 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
4470 break;
4471 }
4472 if (rt && !fib6_info_hold_safe(rt))
4473 rt = NULL;
4474 rcu_read_unlock();
4475 return rt;
4476 }
4477
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)4478 struct fib6_info *rt6_add_dflt_router(struct net *net,
4479 const struct in6_addr *gwaddr,
4480 struct net_device *dev,
4481 unsigned int pref,
4482 u32 defrtr_usr_metric,
4483 int lifetime)
4484 {
4485 struct fib6_config cfg = {
4486 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
4487 .fc_metric = defrtr_usr_metric,
4488 .fc_ifindex = dev->ifindex,
4489 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4490 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
4491 .fc_protocol = RTPROT_RA,
4492 .fc_type = RTN_UNICAST,
4493 .fc_nlinfo.portid = 0,
4494 .fc_nlinfo.nlh = NULL,
4495 .fc_nlinfo.nl_net = net,
4496 .fc_expires = jiffies_to_clock_t(lifetime * HZ),
4497 };
4498
4499 cfg.fc_gateway = *gwaddr;
4500
4501 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
4502 struct fib6_table *table;
4503
4504 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4505 if (table)
4506 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4507 }
4508
4509 return rt6_get_dflt_router(net, gwaddr, dev);
4510 }
4511
__rt6_purge_dflt_routers(struct net * net,struct fib6_table * table)4512 static void __rt6_purge_dflt_routers(struct net *net,
4513 struct fib6_table *table)
4514 {
4515 struct fib6_info *rt;
4516
4517 restart:
4518 rcu_read_lock();
4519 for_each_fib6_node_rt_rcu(&table->tb6_root) {
4520 struct net_device *dev = fib6_info_nh_dev(rt);
4521 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4522
4523 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
4524 (!idev || idev->cnf.accept_ra != 2) &&
4525 fib6_info_hold_safe(rt)) {
4526 rcu_read_unlock();
4527 ip6_del_rt(net, rt, false);
4528 goto restart;
4529 }
4530 }
4531 rcu_read_unlock();
4532
4533 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4534 }
4535
rt6_purge_dflt_routers(struct net * net)4536 void rt6_purge_dflt_routers(struct net *net)
4537 {
4538 struct fib6_table *table;
4539 struct hlist_head *head;
4540 unsigned int h;
4541
4542 rcu_read_lock();
4543
4544 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4545 head = &net->ipv6.fib_table_hash[h];
4546 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4547 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
4548 __rt6_purge_dflt_routers(net, table);
4549 }
4550 }
4551
4552 rcu_read_unlock();
4553 }
4554
rtmsg_to_fib6_config(struct net * net,struct in6_rtmsg * rtmsg,struct fib6_config * cfg)4555 static void rtmsg_to_fib6_config(struct net *net,
4556 struct in6_rtmsg *rtmsg,
4557 struct fib6_config *cfg)
4558 {
4559 *cfg = (struct fib6_config){
4560 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4561 : RT6_TABLE_MAIN,
4562 .fc_ifindex = rtmsg->rtmsg_ifindex,
4563 .fc_metric = rtmsg->rtmsg_metric,
4564 .fc_expires = rtmsg->rtmsg_info,
4565 .fc_dst_len = rtmsg->rtmsg_dst_len,
4566 .fc_src_len = rtmsg->rtmsg_src_len,
4567 .fc_flags = rtmsg->rtmsg_flags,
4568 .fc_type = rtmsg->rtmsg_type,
4569
4570 .fc_nlinfo.nl_net = net,
4571
4572 .fc_dst = rtmsg->rtmsg_dst,
4573 .fc_src = rtmsg->rtmsg_src,
4574 .fc_gateway = rtmsg->rtmsg_gateway,
4575 };
4576 }
4577
ipv6_route_ioctl(struct net * net,unsigned int cmd,struct in6_rtmsg * rtmsg)4578 int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg)
4579 {
4580 struct fib6_config cfg;
4581 int err;
4582
4583 if (cmd != SIOCADDRT && cmd != SIOCDELRT)
4584 return -EINVAL;
4585 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4586 return -EPERM;
4587
4588 rtmsg_to_fib6_config(net, rtmsg, &cfg);
4589
4590 switch (cmd) {
4591 case SIOCADDRT:
4592 /* Only do the default setting of fc_metric in route adding */
4593 if (cfg.fc_metric == 0)
4594 cfg.fc_metric = IP6_RT_PRIO_USER;
4595 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
4596 break;
4597 case SIOCDELRT:
4598 err = ip6_route_del(&cfg, NULL);
4599 break;
4600 }
4601
4602 return err;
4603 }
4604
4605 /*
4606 * Drop the packet on the floor
4607 */
4608
ip6_pkt_drop(struct sk_buff * skb,u8 code,int ipstats_mib_noroutes)4609 static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
4610 {
4611 struct dst_entry *dst = skb_dst(skb);
4612 struct net_device *dev = dst_dev(dst);
4613 struct net *net = dev_net(dev);
4614 struct inet6_dev *idev;
4615 SKB_DR(reason);
4616 int type;
4617
4618 if (netif_is_l3_master(skb->dev) ||
4619 dev == net->loopback_dev)
4620 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4621 else
4622 idev = ip6_dst_idev(dst);
4623
4624 switch (ipstats_mib_noroutes) {
4625 case IPSTATS_MIB_INNOROUTES:
4626 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
4627 if (type == IPV6_ADDR_ANY) {
4628 SKB_DR_SET(reason, IP_INADDRERRORS);
4629 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
4630 break;
4631 }
4632 SKB_DR_SET(reason, IP_INNOROUTES);
4633 fallthrough;
4634 case IPSTATS_MIB_OUTNOROUTES:
4635 SKB_DR_OR(reason, IP_OUTNOROUTES);
4636 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
4637 break;
4638 }
4639
4640 /* Start over by dropping the dst for l3mdev case */
4641 if (netif_is_l3_master(skb->dev))
4642 skb_dst_drop(skb);
4643
4644 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
4645 kfree_skb_reason(skb, reason);
4646 return 0;
4647 }
4648
ip6_pkt_discard(struct sk_buff * skb)4649 static int ip6_pkt_discard(struct sk_buff *skb)
4650 {
4651 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
4652 }
4653
ip6_pkt_discard_out(struct net * net,struct sock * sk,struct sk_buff * skb)4654 static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4655 {
4656 skb->dev = skb_dst_dev(skb);
4657 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
4658 }
4659
ip6_pkt_prohibit(struct sk_buff * skb)4660 static int ip6_pkt_prohibit(struct sk_buff *skb)
4661 {
4662 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
4663 }
4664
ip6_pkt_prohibit_out(struct net * net,struct sock * sk,struct sk_buff * skb)4665 static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4666 {
4667 skb->dev = skb_dst_dev(skb);
4668 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
4669 }
4670
4671 /*
4672 * Allocate a dst for local (unicast / anycast) address.
4673 */
4674
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)4675 struct fib6_info *addrconf_f6i_alloc(struct net *net,
4676 struct inet6_dev *idev,
4677 const struct in6_addr *addr,
4678 bool anycast, gfp_t gfp_flags,
4679 struct netlink_ext_ack *extack)
4680 {
4681 struct fib6_config cfg = {
4682 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4683 .fc_ifindex = idev->dev->ifindex,
4684 .fc_flags = RTF_UP | RTF_NONEXTHOP,
4685 .fc_dst = *addr,
4686 .fc_dst_len = 128,
4687 .fc_protocol = RTPROT_KERNEL,
4688 .fc_nlinfo.nl_net = net,
4689 .fc_ignore_dev_down = true,
4690 };
4691 struct fib6_info *f6i;
4692 int err;
4693
4694 if (anycast) {
4695 cfg.fc_type = RTN_ANYCAST;
4696 cfg.fc_flags |= RTF_ANYCAST;
4697 } else {
4698 cfg.fc_type = RTN_LOCAL;
4699 cfg.fc_flags |= RTF_LOCAL;
4700 }
4701
4702 f6i = ip6_route_info_create(&cfg, gfp_flags, extack);
4703 if (IS_ERR(f6i))
4704 return f6i;
4705
4706 err = ip6_route_info_create_nh(f6i, &cfg, gfp_flags, extack);
4707 if (err)
4708 return ERR_PTR(err);
4709
4710 f6i->dst_nocount = true;
4711
4712 if (!anycast &&
4713 (READ_ONCE(net->ipv6.devconf_all->disable_policy) ||
4714 READ_ONCE(idev->cnf.disable_policy)))
4715 f6i->dst_nopolicy = true;
4716
4717 return f6i;
4718 }
4719
4720 /* remove deleted ip from prefsrc entries */
4721 struct arg_dev_net_ip {
4722 struct net *net;
4723 struct in6_addr *addr;
4724 };
4725
fib6_remove_prefsrc(struct fib6_info * rt,void * arg)4726 static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
4727 {
4728 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4729 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4730
4731 if (!rt->nh &&
4732 rt != net->ipv6.fib6_null_entry &&
4733 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr) &&
4734 !ipv6_chk_addr(net, addr, rt->fib6_nh->fib_nh_dev, 0)) {
4735 spin_lock_bh(&rt6_exception_lock);
4736 /* remove prefsrc entry */
4737 rt->fib6_prefsrc.plen = 0;
4738 spin_unlock_bh(&rt6_exception_lock);
4739 }
4740 return 0;
4741 }
4742
rt6_remove_prefsrc(struct inet6_ifaddr * ifp)4743 void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4744 {
4745 struct net *net = dev_net(ifp->idev->dev);
4746 struct arg_dev_net_ip adni = {
4747 .net = net,
4748 .addr = &ifp->addr,
4749 };
4750 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
4751 }
4752
4753 #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
4754
4755 /* Remove routers and update dst entries when gateway turn into host. */
fib6_clean_tohost(struct fib6_info * rt,void * arg)4756 static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
4757 {
4758 struct in6_addr *gateway = (struct in6_addr *)arg;
4759 struct fib6_nh *nh;
4760
4761 /* RA routes do not use nexthops */
4762 if (rt->nh)
4763 return 0;
4764
4765 nh = rt->fib6_nh;
4766 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
4767 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
4768 return -1;
4769
4770 /* Further clean up cached routes in exception table.
4771 * This is needed because cached route may have a different
4772 * gateway than its 'parent' in the case of an ip redirect.
4773 */
4774 fib6_nh_exceptions_clean_tohost(nh, gateway);
4775
4776 return 0;
4777 }
4778
rt6_clean_tohost(struct net * net,struct in6_addr * gateway)4779 void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4780 {
4781 fib6_clean_all(net, fib6_clean_tohost, gateway);
4782 }
4783
4784 struct arg_netdev_event {
4785 const struct net_device *dev;
4786 union {
4787 unsigned char nh_flags;
4788 unsigned long event;
4789 };
4790 };
4791
rt6_multipath_first_sibling(const struct fib6_info * rt)4792 static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
4793 {
4794 struct fib6_info *iter;
4795 struct fib6_node *fn;
4796
4797 fn = rcu_dereference_protected(rt->fib6_node,
4798 lockdep_is_held(&rt->fib6_table->tb6_lock));
4799 iter = rcu_dereference_protected(fn->leaf,
4800 lockdep_is_held(&rt->fib6_table->tb6_lock));
4801 while (iter) {
4802 if (iter->fib6_metric == rt->fib6_metric &&
4803 rt6_qualify_for_ecmp(iter))
4804 return iter;
4805 iter = rcu_dereference_protected(iter->fib6_next,
4806 lockdep_is_held(&rt->fib6_table->tb6_lock));
4807 }
4808
4809 return NULL;
4810 }
4811
4812 /* only called for fib entries with builtin fib6_nh */
rt6_is_dead(const struct fib6_info * rt)4813 static bool rt6_is_dead(const struct fib6_info *rt)
4814 {
4815 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4816 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4817 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
4818 return true;
4819
4820 return false;
4821 }
4822
rt6_multipath_total_weight(const struct fib6_info * rt)4823 static int rt6_multipath_total_weight(const struct fib6_info *rt)
4824 {
4825 struct fib6_info *iter;
4826 int total = 0;
4827
4828 if (!rt6_is_dead(rt))
4829 total += rt->fib6_nh->fib_nh_weight;
4830
4831 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
4832 if (!rt6_is_dead(iter))
4833 total += iter->fib6_nh->fib_nh_weight;
4834 }
4835
4836 return total;
4837 }
4838
rt6_upper_bound_set(struct fib6_info * rt,int * weight,int total)4839 static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
4840 {
4841 int upper_bound = -1;
4842
4843 if (!rt6_is_dead(rt)) {
4844 *weight += rt->fib6_nh->fib_nh_weight;
4845 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4846 total) - 1;
4847 }
4848 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
4849 }
4850
rt6_multipath_upper_bound_set(struct fib6_info * rt,int total)4851 static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
4852 {
4853 struct fib6_info *iter;
4854 int weight = 0;
4855
4856 rt6_upper_bound_set(rt, &weight, total);
4857
4858 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4859 rt6_upper_bound_set(iter, &weight, total);
4860 }
4861
rt6_multipath_rebalance(struct fib6_info * rt)4862 void rt6_multipath_rebalance(struct fib6_info *rt)
4863 {
4864 struct fib6_info *first;
4865 int total;
4866
4867 /* In case the entire multipath route was marked for flushing,
4868 * then there is no need to rebalance upon the removal of every
4869 * sibling route.
4870 */
4871 if (!rt->fib6_nsiblings || rt->should_flush)
4872 return;
4873
4874 /* During lookup routes are evaluated in order, so we need to
4875 * make sure upper bounds are assigned from the first sibling
4876 * onwards.
4877 */
4878 first = rt6_multipath_first_sibling(rt);
4879 if (WARN_ON_ONCE(!first))
4880 return;
4881
4882 total = rt6_multipath_total_weight(first);
4883 rt6_multipath_upper_bound_set(first, total);
4884 }
4885
fib6_ifup(struct fib6_info * rt,void * p_arg)4886 static int fib6_ifup(struct fib6_info *rt, void *p_arg)
4887 {
4888 const struct arg_netdev_event *arg = p_arg;
4889 struct net *net = dev_net(arg->dev);
4890
4891 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
4892 rt->fib6_nh->fib_nh_dev == arg->dev) {
4893 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
4894 fib6_update_sernum_upto_root(net, rt);
4895 rt6_multipath_rebalance(rt);
4896 }
4897
4898 return 0;
4899 }
4900
rt6_sync_up(struct net_device * dev,unsigned char nh_flags)4901 void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
4902 {
4903 struct arg_netdev_event arg = {
4904 .dev = dev,
4905 {
4906 .nh_flags = nh_flags,
4907 },
4908 };
4909
4910 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4911 arg.nh_flags |= RTNH_F_LINKDOWN;
4912
4913 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4914 }
4915
4916 /* only called for fib entries with inline fib6_nh */
rt6_multipath_uses_dev(const struct fib6_info * rt,const struct net_device * dev)4917 static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
4918 const struct net_device *dev)
4919 {
4920 struct fib6_info *iter;
4921
4922 if (rt->fib6_nh->fib_nh_dev == dev)
4923 return true;
4924 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4925 if (iter->fib6_nh->fib_nh_dev == dev)
4926 return true;
4927
4928 return false;
4929 }
4930
rt6_multipath_flush(struct fib6_info * rt)4931 static void rt6_multipath_flush(struct fib6_info *rt)
4932 {
4933 struct fib6_info *iter;
4934
4935 rt->should_flush = 1;
4936 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4937 iter->should_flush = 1;
4938 }
4939
rt6_multipath_dead_count(const struct fib6_info * rt,const struct net_device * down_dev)4940 static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
4941 const struct net_device *down_dev)
4942 {
4943 struct fib6_info *iter;
4944 unsigned int dead = 0;
4945
4946 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4947 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
4948 dead++;
4949 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4950 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4951 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
4952 dead++;
4953
4954 return dead;
4955 }
4956
rt6_multipath_nh_flags_set(struct fib6_info * rt,const struct net_device * dev,unsigned char nh_flags)4957 static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
4958 const struct net_device *dev,
4959 unsigned char nh_flags)
4960 {
4961 struct fib6_info *iter;
4962
4963 if (rt->fib6_nh->fib_nh_dev == dev)
4964 rt->fib6_nh->fib_nh_flags |= nh_flags;
4965 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4966 if (iter->fib6_nh->fib_nh_dev == dev)
4967 iter->fib6_nh->fib_nh_flags |= nh_flags;
4968 }
4969
4970 /* called with write lock held for table with rt */
fib6_ifdown(struct fib6_info * rt,void * p_arg)4971 static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
4972 {
4973 const struct arg_netdev_event *arg = p_arg;
4974 const struct net_device *dev = arg->dev;
4975 struct net *net = dev_net(dev);
4976
4977 if (rt == net->ipv6.fib6_null_entry || rt->nh)
4978 return 0;
4979
4980 switch (arg->event) {
4981 case NETDEV_UNREGISTER:
4982 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
4983 case NETDEV_DOWN:
4984 if (rt->should_flush)
4985 return -1;
4986 if (!rt->fib6_nsiblings)
4987 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
4988 if (rt6_multipath_uses_dev(rt, dev)) {
4989 unsigned int count;
4990
4991 count = rt6_multipath_dead_count(rt, dev);
4992 if (rt->fib6_nsiblings + 1 == count) {
4993 rt6_multipath_flush(rt);
4994 return -1;
4995 }
4996 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4997 RTNH_F_LINKDOWN);
4998 fib6_update_sernum(net, rt);
4999 rt6_multipath_rebalance(rt);
5000 }
5001 return -2;
5002 case NETDEV_CHANGE:
5003 if (rt->fib6_nh->fib_nh_dev != dev ||
5004 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
5005 break;
5006 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
5007 fib6_update_sernum(net, rt);
5008 rt6_multipath_rebalance(rt);
5009 break;
5010 }
5011
5012 return 0;
5013 }
5014
rt6_sync_down_dev(struct net_device * dev,unsigned long event)5015 void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
5016 {
5017 struct arg_netdev_event arg = {
5018 .dev = dev,
5019 {
5020 .event = event,
5021 },
5022 };
5023 struct net *net = dev_net(dev);
5024
5025 if (READ_ONCE(net->ipv6.sysctl.skip_notify_on_dev_down))
5026 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
5027 else
5028 fib6_clean_all(net, fib6_ifdown, &arg);
5029 }
5030
rt6_disable_ip(struct net_device * dev,unsigned long event)5031 void rt6_disable_ip(struct net_device *dev, unsigned long event)
5032 {
5033 rt6_sync_down_dev(dev, event);
5034 rt6_uncached_list_flush_dev(dev);
5035 neigh_ifdown(&nd_tbl, dev);
5036 }
5037
5038 struct rt6_mtu_change_arg {
5039 struct net_device *dev;
5040 unsigned int mtu;
5041 struct fib6_info *f6i;
5042 };
5043
fib6_nh_mtu_change(struct fib6_nh * nh,void * _arg)5044 static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
5045 {
5046 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
5047 struct fib6_info *f6i = arg->f6i;
5048
5049 /* For administrative MTU increase, there is no way to discover
5050 * IPv6 PMTU increase, so PMTU increase should be updated here.
5051 * Since RFC 1981 doesn't include administrative MTU increase
5052 * update PMTU increase is a MUST. (i.e. jumbo frame)
5053 */
5054 if (nh->fib_nh_dev == arg->dev) {
5055 struct inet6_dev *idev = __in6_dev_get(arg->dev);
5056 u32 mtu = f6i->fib6_pmtu;
5057
5058 if (!idev)
5059 return 0;
5060
5061 if (mtu >= arg->mtu ||
5062 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
5063 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
5064
5065 spin_lock_bh(&rt6_exception_lock);
5066 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
5067 spin_unlock_bh(&rt6_exception_lock);
5068 }
5069
5070 return 0;
5071 }
5072
rt6_mtu_change_route(struct fib6_info * f6i,void * p_arg)5073 static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
5074 {
5075 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
5076 struct inet6_dev *idev;
5077
5078 /* In IPv6 pmtu discovery is not optional,
5079 so that RTAX_MTU lock cannot disable it.
5080 We still use this lock to block changes
5081 caused by addrconf/ndisc.
5082 */
5083
5084 idev = __in6_dev_get(arg->dev);
5085 if (!idev)
5086 return 0;
5087
5088 if (fib6_metric_locked(f6i, RTAX_MTU))
5089 return 0;
5090
5091 arg->f6i = f6i;
5092 if (f6i->nh) {
5093 /* fib6_nh_mtu_change only returns 0, so this is safe */
5094 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
5095 arg);
5096 }
5097
5098 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
5099 }
5100
rt6_mtu_change(struct net_device * dev,unsigned int mtu)5101 void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
5102 {
5103 struct rt6_mtu_change_arg arg = {
5104 .dev = dev,
5105 .mtu = mtu,
5106 };
5107
5108 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
5109 }
5110
5111 static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
5112 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
5113 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
5114 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
5115 [RTA_OIF] = { .type = NLA_U32 },
5116 [RTA_IIF] = { .type = NLA_U32 },
5117 [RTA_PRIORITY] = { .type = NLA_U32 },
5118 [RTA_METRICS] = { .type = NLA_NESTED },
5119 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
5120 [RTA_PREF] = { .type = NLA_U8 },
5121 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
5122 [RTA_ENCAP] = { .type = NLA_NESTED },
5123 [RTA_EXPIRES] = { .type = NLA_U32 },
5124 [RTA_UID] = { .type = NLA_U32 },
5125 [RTA_MARK] = { .type = NLA_U32 },
5126 [RTA_TABLE] = { .type = NLA_U32 },
5127 [RTA_IP_PROTO] = { .type = NLA_U8 },
5128 [RTA_SPORT] = { .type = NLA_U16 },
5129 [RTA_DPORT] = { .type = NLA_U16 },
5130 [RTA_NH_ID] = { .type = NLA_U32 },
5131 [RTA_FLOWLABEL] = { .type = NLA_BE32 },
5132 };
5133
rtm_to_fib6_multipath_config(struct fib6_config * cfg,struct netlink_ext_ack * extack,bool newroute)5134 static int rtm_to_fib6_multipath_config(struct fib6_config *cfg,
5135 struct netlink_ext_ack *extack,
5136 bool newroute)
5137 {
5138 struct rtnexthop *rtnh;
5139 int remaining;
5140
5141 remaining = cfg->fc_mp_len;
5142 rtnh = (struct rtnexthop *)cfg->fc_mp;
5143
5144 if (!rtnh_ok(rtnh, remaining)) {
5145 NL_SET_ERR_MSG(extack, "Invalid nexthop configuration - no valid nexthops");
5146 return -EINVAL;
5147 }
5148
5149 do {
5150 bool has_gateway = cfg->fc_flags & RTF_GATEWAY;
5151 int attrlen = rtnh_attrlen(rtnh);
5152
5153 if (attrlen > 0) {
5154 struct nlattr *nla, *attrs;
5155
5156 attrs = rtnh_attrs(rtnh);
5157 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5158 if (nla) {
5159 if (nla_len(nla) < sizeof(cfg->fc_gateway)) {
5160 NL_SET_ERR_MSG(extack,
5161 "Invalid IPv6 address in RTA_GATEWAY");
5162 return -EINVAL;
5163 }
5164
5165 has_gateway = true;
5166 }
5167 }
5168
5169 if (newroute && (cfg->fc_nh_id || !has_gateway)) {
5170 NL_SET_ERR_MSG(extack,
5171 "Device only routes can not be added for IPv6 using the multipath API.");
5172 return -EINVAL;
5173 }
5174
5175 rtnh = rtnh_next(rtnh, &remaining);
5176 } while (rtnh_ok(rtnh, remaining));
5177
5178 return lwtunnel_valid_encap_type_attr(cfg->fc_mp, cfg->fc_mp_len, extack);
5179 }
5180
rtm_to_fib6_config(struct sk_buff * skb,struct nlmsghdr * nlh,struct fib6_config * cfg,struct netlink_ext_ack * extack)5181 static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
5182 struct fib6_config *cfg,
5183 struct netlink_ext_ack *extack)
5184 {
5185 bool newroute = nlh->nlmsg_type == RTM_NEWROUTE;
5186 struct nlattr *tb[RTA_MAX+1];
5187 struct rtmsg *rtm;
5188 unsigned int pref;
5189 int err;
5190
5191 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5192 rtm_ipv6_policy, extack);
5193 if (err < 0)
5194 goto errout;
5195
5196 err = -EINVAL;
5197 rtm = nlmsg_data(nlh);
5198
5199 if (rtm->rtm_tos) {
5200 NL_SET_ERR_MSG(extack,
5201 "Invalid dsfield (tos): option not available for IPv6");
5202 goto errout;
5203 }
5204
5205 if (tb[RTA_FLOWLABEL]) {
5206 NL_SET_ERR_MSG_ATTR(extack, tb[RTA_FLOWLABEL],
5207 "Flow label cannot be specified for this operation");
5208 goto errout;
5209 }
5210
5211 *cfg = (struct fib6_config){
5212 .fc_table = rtm->rtm_table,
5213 .fc_dst_len = rtm->rtm_dst_len,
5214 .fc_src_len = rtm->rtm_src_len,
5215 .fc_flags = RTF_UP,
5216 .fc_protocol = rtm->rtm_protocol,
5217 .fc_type = rtm->rtm_type,
5218
5219 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
5220 .fc_nlinfo.nlh = nlh,
5221 .fc_nlinfo.nl_net = sock_net(skb->sk),
5222 };
5223
5224 if (rtm->rtm_type == RTN_UNREACHABLE ||
5225 rtm->rtm_type == RTN_BLACKHOLE ||
5226 rtm->rtm_type == RTN_PROHIBIT ||
5227 rtm->rtm_type == RTN_THROW)
5228 cfg->fc_flags |= RTF_REJECT;
5229
5230 if (rtm->rtm_type == RTN_LOCAL)
5231 cfg->fc_flags |= RTF_LOCAL;
5232
5233 if (rtm->rtm_flags & RTM_F_CLONED)
5234 cfg->fc_flags |= RTF_CACHE;
5235
5236 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
5237
5238 if (tb[RTA_NH_ID]) {
5239 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
5240 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
5241 NL_SET_ERR_MSG(extack,
5242 "Nexthop specification and nexthop id are mutually exclusive");
5243 goto errout;
5244 }
5245 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
5246 }
5247
5248 if (tb[RTA_GATEWAY]) {
5249 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
5250 cfg->fc_flags |= RTF_GATEWAY;
5251 }
5252 if (tb[RTA_VIA]) {
5253 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
5254 goto errout;
5255 }
5256
5257 if (tb[RTA_DST]) {
5258 int plen = (rtm->rtm_dst_len + 7) >> 3;
5259
5260 if (nla_len(tb[RTA_DST]) < plen)
5261 goto errout;
5262
5263 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
5264 }
5265
5266 if (tb[RTA_SRC]) {
5267 int plen = (rtm->rtm_src_len + 7) >> 3;
5268
5269 if (nla_len(tb[RTA_SRC]) < plen)
5270 goto errout;
5271
5272 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
5273 }
5274
5275 if (tb[RTA_PREFSRC])
5276 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
5277
5278 if (tb[RTA_OIF])
5279 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
5280
5281 if (tb[RTA_PRIORITY])
5282 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
5283
5284 if (tb[RTA_METRICS]) {
5285 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
5286 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
5287 }
5288
5289 if (tb[RTA_TABLE])
5290 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
5291
5292 if (tb[RTA_MULTIPATH]) {
5293 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
5294 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
5295
5296 err = rtm_to_fib6_multipath_config(cfg, extack, newroute);
5297 if (err < 0)
5298 goto errout;
5299 }
5300
5301 if (tb[RTA_PREF]) {
5302 pref = nla_get_u8(tb[RTA_PREF]);
5303 if (pref != ICMPV6_ROUTER_PREF_LOW &&
5304 pref != ICMPV6_ROUTER_PREF_HIGH)
5305 pref = ICMPV6_ROUTER_PREF_MEDIUM;
5306 cfg->fc_flags |= RTF_PREF(pref);
5307 }
5308
5309 if (tb[RTA_ENCAP])
5310 cfg->fc_encap = tb[RTA_ENCAP];
5311
5312 if (tb[RTA_ENCAP_TYPE]) {
5313 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
5314
5315 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
5316 if (err < 0)
5317 goto errout;
5318 }
5319
5320 if (tb[RTA_EXPIRES]) {
5321 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
5322
5323 if (addrconf_finite_timeout(timeout)) {
5324 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
5325 cfg->fc_flags |= RTF_EXPIRES;
5326 }
5327 }
5328
5329 err = 0;
5330 errout:
5331 return err;
5332 }
5333
5334 struct rt6_nh {
5335 struct fib6_info *fib6_info;
5336 struct fib6_config r_cfg;
5337 struct list_head list;
5338 };
5339
ip6_route_info_append(struct list_head * rt6_nh_list,struct fib6_info * rt,struct fib6_config * r_cfg)5340 static int ip6_route_info_append(struct list_head *rt6_nh_list,
5341 struct fib6_info *rt,
5342 struct fib6_config *r_cfg)
5343 {
5344 struct rt6_nh *nh;
5345
5346 list_for_each_entry(nh, rt6_nh_list, list) {
5347 /* check if fib6_info already exists */
5348 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
5349 return -EEXIST;
5350 }
5351
5352 nh = kzalloc_obj(*nh);
5353 if (!nh)
5354 return -ENOMEM;
5355
5356 nh->fib6_info = rt;
5357 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
5358 list_add_tail(&nh->list, rt6_nh_list);
5359
5360 return 0;
5361 }
5362
ip6_route_mpath_notify(struct fib6_info * rt,struct fib6_info * rt_last,struct nl_info * info,__u16 nlflags)5363 static void ip6_route_mpath_notify(struct fib6_info *rt,
5364 struct fib6_info *rt_last,
5365 struct nl_info *info,
5366 __u16 nlflags)
5367 {
5368 /* if this is an APPEND route, then rt points to the first route
5369 * inserted and rt_last points to last route inserted. Userspace
5370 * wants a consistent dump of the route which starts at the first
5371 * nexthop. Since sibling routes are always added at the end of
5372 * the list, find the first sibling of the last route appended
5373 */
5374 rcu_read_lock();
5375
5376 if ((nlflags & NLM_F_APPEND) && rt_last &&
5377 READ_ONCE(rt_last->fib6_nsiblings)) {
5378 rt = list_first_or_null_rcu(&rt_last->fib6_siblings,
5379 struct fib6_info,
5380 fib6_siblings);
5381 }
5382
5383 if (rt)
5384 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5385
5386 rcu_read_unlock();
5387 }
5388
ip6_route_mpath_should_notify(const struct fib6_info * rt)5389 static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
5390 {
5391 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
5392 bool should_notify = false;
5393 struct fib6_info *leaf;
5394 struct fib6_node *fn;
5395
5396 rcu_read_lock();
5397 fn = rcu_dereference(rt->fib6_node);
5398 if (!fn)
5399 goto out;
5400
5401 leaf = rcu_dereference(fn->leaf);
5402 if (!leaf)
5403 goto out;
5404
5405 if (rt == leaf ||
5406 (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
5407 rt6_qualify_for_ecmp(leaf)))
5408 should_notify = true;
5409 out:
5410 rcu_read_unlock();
5411
5412 return should_notify;
5413 }
5414
ip6_route_multipath_add(struct fib6_config * cfg,struct netlink_ext_ack * extack)5415 static int ip6_route_multipath_add(struct fib6_config *cfg,
5416 struct netlink_ext_ack *extack)
5417 {
5418 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
5419 struct nl_info *info = &cfg->fc_nlinfo;
5420 struct rt6_nh *nh, *nh_safe;
5421 struct fib6_config r_cfg;
5422 struct rtnexthop *rtnh;
5423 LIST_HEAD(rt6_nh_list);
5424 struct rt6_nh *err_nh;
5425 struct fib6_info *rt;
5426 __u16 nlflags;
5427 int remaining;
5428 int attrlen;
5429 int replace;
5430 int nhn = 0;
5431 int err;
5432
5433 err = fib6_config_validate(cfg, extack);
5434 if (err)
5435 return err;
5436
5437 replace = (cfg->fc_nlinfo.nlh &&
5438 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5439
5440 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5441 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5442 nlflags |= NLM_F_APPEND;
5443
5444 remaining = cfg->fc_mp_len;
5445 rtnh = (struct rtnexthop *)cfg->fc_mp;
5446
5447 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
5448 * fib6_info structs per nexthop
5449 */
5450 while (rtnh_ok(rtnh, remaining)) {
5451 memcpy(&r_cfg, cfg, sizeof(*cfg));
5452 if (rtnh->rtnh_ifindex)
5453 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5454
5455 attrlen = rtnh_attrlen(rtnh);
5456 if (attrlen > 0) {
5457 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5458
5459 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5460 if (nla) {
5461 r_cfg.fc_gateway = nla_get_in6_addr(nla);
5462 r_cfg.fc_flags |= RTF_GATEWAY;
5463 }
5464
5465 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5466 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5467 if (nla)
5468 r_cfg.fc_encap_type = nla_get_u16(nla);
5469 }
5470
5471 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
5472 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
5473 if (IS_ERR(rt)) {
5474 err = PTR_ERR(rt);
5475 rt = NULL;
5476 goto cleanup;
5477 }
5478
5479 err = ip6_route_info_create_nh(rt, &r_cfg, GFP_KERNEL, extack);
5480 if (err) {
5481 rt = NULL;
5482 goto cleanup;
5483 }
5484
5485 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
5486
5487 err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg);
5488 if (err) {
5489 fib6_info_release(rt);
5490 goto cleanup;
5491 }
5492
5493 rtnh = rtnh_next(rtnh, &remaining);
5494 }
5495
5496 /* for add and replace send one notification with all nexthops.
5497 * Skip the notification in fib6_add_rt2node and send one with
5498 * the full route when done
5499 */
5500 info->skip_notify = 1;
5501
5502 /* For add and replace, send one notification with all nexthops. For
5503 * append, send one notification with all appended nexthops.
5504 */
5505 info->skip_notify_kernel = 1;
5506
5507 err_nh = NULL;
5508 list_for_each_entry(nh, &rt6_nh_list, list) {
5509 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5510
5511 if (err) {
5512 if (replace && nhn)
5513 NL_SET_ERR_MSG_MOD(extack,
5514 "multipath route replace failed (check consistency of installed routes)");
5515 err_nh = nh;
5516 goto add_errout;
5517 }
5518 /* save reference to last route successfully inserted */
5519 rt_last = nh->fib6_info;
5520
5521 /* save reference to first route for notification */
5522 if (!rt_notif)
5523 rt_notif = nh->fib6_info;
5524
5525 /* Because each route is added like a single route we remove
5526 * these flags after the first nexthop: if there is a collision,
5527 * we have already failed to add the first nexthop:
5528 * fib6_add_rt2node() has rejected it; when replacing, old
5529 * nexthops have been replaced by first new, the rest should
5530 * be added to it.
5531 */
5532 if (cfg->fc_nlinfo.nlh) {
5533 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5534 NLM_F_REPLACE);
5535 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
5536 }
5537 nhn++;
5538 }
5539
5540 /* An in-kernel notification should only be sent in case the new
5541 * multipath route is added as the first route in the node, or if
5542 * it was appended to it. We pass 'rt_notif' since it is the first
5543 * sibling and might allow us to skip some checks in the replace case.
5544 */
5545 if (ip6_route_mpath_should_notify(rt_notif)) {
5546 enum fib_event_type fib_event;
5547
5548 if (rt_notif->fib6_nsiblings != nhn - 1)
5549 fib_event = FIB_EVENT_ENTRY_APPEND;
5550 else
5551 fib_event = FIB_EVENT_ENTRY_REPLACE;
5552
5553 err = call_fib6_multipath_entry_notifiers(info->nl_net,
5554 fib_event, rt_notif,
5555 nhn - 1, extack);
5556 if (err) {
5557 /* Delete all the siblings that were just added */
5558 err_nh = NULL;
5559 goto add_errout;
5560 }
5561 }
5562
5563 /* success ... tell user about new route */
5564 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5565 goto cleanup;
5566
5567 add_errout:
5568 /* send notification for routes that were added so that
5569 * the delete notifications sent by ip6_route_del are
5570 * coherent
5571 */
5572 if (rt_notif)
5573 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5574
5575 /* Delete routes that were already added */
5576 list_for_each_entry(nh, &rt6_nh_list, list) {
5577 if (err_nh == nh)
5578 break;
5579 ip6_route_del(&nh->r_cfg, extack);
5580 }
5581
5582 cleanup:
5583 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, list) {
5584 fib6_info_release(nh->fib6_info);
5585 list_del(&nh->list);
5586 kfree(nh);
5587 }
5588
5589 return err;
5590 }
5591
ip6_route_multipath_del(struct fib6_config * cfg,struct netlink_ext_ack * extack)5592 static int ip6_route_multipath_del(struct fib6_config *cfg,
5593 struct netlink_ext_ack *extack)
5594 {
5595 struct fib6_config r_cfg;
5596 struct rtnexthop *rtnh;
5597 int last_err = 0;
5598 int remaining;
5599 int attrlen;
5600 int err;
5601
5602 remaining = cfg->fc_mp_len;
5603 rtnh = (struct rtnexthop *)cfg->fc_mp;
5604
5605 /* Parse a Multipath Entry */
5606 while (rtnh_ok(rtnh, remaining)) {
5607 memcpy(&r_cfg, cfg, sizeof(*cfg));
5608 if (rtnh->rtnh_ifindex)
5609 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5610
5611 attrlen = rtnh_attrlen(rtnh);
5612 if (attrlen > 0) {
5613 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5614
5615 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5616 if (nla) {
5617 r_cfg.fc_gateway = nla_get_in6_addr(nla);
5618 r_cfg.fc_flags |= RTF_GATEWAY;
5619 }
5620 }
5621
5622 err = ip6_route_del(&r_cfg, extack);
5623 if (err)
5624 last_err = err;
5625
5626 rtnh = rtnh_next(rtnh, &remaining);
5627 }
5628
5629 return last_err;
5630 }
5631
inet6_rtm_delroute(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)5632 static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5633 struct netlink_ext_ack *extack)
5634 {
5635 struct fib6_config cfg;
5636 int err;
5637
5638 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5639 if (err < 0)
5640 return err;
5641
5642 if (cfg.fc_nh_id) {
5643 rcu_read_lock();
5644 err = !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id);
5645 rcu_read_unlock();
5646
5647 if (err) {
5648 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5649 return -EINVAL;
5650 }
5651 }
5652
5653 if (cfg.fc_mp) {
5654 return ip6_route_multipath_del(&cfg, extack);
5655 } else {
5656 cfg.fc_delete_all_nh = 1;
5657 return ip6_route_del(&cfg, extack);
5658 }
5659 }
5660
inet6_rtm_newroute(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)5661 static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5662 struct netlink_ext_ack *extack)
5663 {
5664 struct fib6_config cfg;
5665 int err;
5666
5667 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5668 if (err < 0)
5669 return err;
5670
5671 if (cfg.fc_metric == 0)
5672 cfg.fc_metric = IP6_RT_PRIO_USER;
5673
5674 if (cfg.fc_mp)
5675 return ip6_route_multipath_add(&cfg, extack);
5676 else
5677 return ip6_route_add(&cfg, GFP_KERNEL, extack);
5678 }
5679
5680 /* add the overhead of this fib6_nh to nexthop_len */
rt6_nh_nlmsg_size(struct fib6_nh * nh,void * arg)5681 static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
5682 {
5683 int *nexthop_len = arg;
5684
5685 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5686 + NLA_ALIGN(sizeof(struct rtnexthop))
5687 + nla_total_size(16); /* RTA_GATEWAY */
5688
5689 if (nh->fib_nh_lws) {
5690 /* RTA_ENCAP_TYPE */
5691 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5692 /* RTA_ENCAP */
5693 *nexthop_len += nla_total_size(2);
5694 }
5695
5696 return 0;
5697 }
5698
rt6_nlmsg_size(struct fib6_info * f6i)5699 static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5700 {
5701 struct fib6_info *sibling;
5702 struct fib6_nh *nh;
5703 int nexthop_len;
5704
5705 if (f6i->nh) {
5706 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5707 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5708 &nexthop_len);
5709 goto common;
5710 }
5711
5712 rcu_read_lock();
5713 retry:
5714 nh = f6i->fib6_nh;
5715 nexthop_len = 0;
5716 if (READ_ONCE(f6i->fib6_nsiblings)) {
5717 rt6_nh_nlmsg_size(nh, &nexthop_len);
5718
5719 list_for_each_entry_rcu(sibling, &f6i->fib6_siblings,
5720 fib6_siblings) {
5721 rt6_nh_nlmsg_size(sibling->fib6_nh, &nexthop_len);
5722 if (!READ_ONCE(f6i->fib6_nsiblings))
5723 goto retry;
5724 }
5725 }
5726 rcu_read_unlock();
5727 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5728 common:
5729 return NLMSG_ALIGN(sizeof(struct rtmsg))
5730 + nla_total_size(16) /* RTA_SRC */
5731 + nla_total_size(16) /* RTA_DST */
5732 + nla_total_size(16) /* RTA_GATEWAY */
5733 + nla_total_size(16) /* RTA_PREFSRC */
5734 + nla_total_size(4) /* RTA_TABLE */
5735 + nla_total_size(4) /* RTA_IIF */
5736 + nla_total_size(4) /* RTA_OIF */
5737 + nla_total_size(4) /* RTA_PRIORITY */
5738 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
5739 + nla_total_size(sizeof(struct rta_cacheinfo))
5740 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
5741 + nla_total_size(1) /* RTA_PREF */
5742 + nexthop_len;
5743 }
5744
rt6_fill_node_nexthop(struct sk_buff * skb,struct nexthop * nh,unsigned char * flags)5745 static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5746 unsigned char *flags)
5747 {
5748 if (nexthop_is_multipath(nh)) {
5749 struct nlattr *mp;
5750
5751 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5752 if (!mp)
5753 goto nla_put_failure;
5754
5755 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
5756 goto nla_put_failure;
5757
5758 nla_nest_end(skb, mp);
5759 } else {
5760 struct fib6_nh *fib6_nh;
5761
5762 fib6_nh = nexthop_fib6_nh(nh);
5763 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
5764 flags, false) < 0)
5765 goto nla_put_failure;
5766 }
5767
5768 return 0;
5769
5770 nla_put_failure:
5771 return -EMSGSIZE;
5772 }
5773
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)5774 static int rt6_fill_node(struct net *net, struct sk_buff *skb,
5775 struct fib6_info *rt, struct dst_entry *dst,
5776 struct in6_addr *dest, struct in6_addr *src,
5777 int iif, int type, u32 portid, u32 seq,
5778 unsigned int flags)
5779 {
5780 struct rt6_info *rt6 = dst_rt6_info(dst);
5781 struct rt6key *rt6_dst, *rt6_src;
5782 u32 *pmetrics, table, rt6_flags;
5783 unsigned char nh_flags = 0;
5784 struct nlmsghdr *nlh;
5785 struct rtmsg *rtm;
5786 long expires = 0;
5787
5788 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
5789 if (!nlh)
5790 return -EMSGSIZE;
5791
5792 if (rt6) {
5793 rt6_dst = &rt6->rt6i_dst;
5794 rt6_src = &rt6->rt6i_src;
5795 rt6_flags = rt6->rt6i_flags;
5796 } else {
5797 rt6_dst = &rt->fib6_dst;
5798 rt6_src = &rt->fib6_src;
5799 rt6_flags = rt->fib6_flags;
5800 }
5801
5802 rtm = nlmsg_data(nlh);
5803 rtm->rtm_family = AF_INET6;
5804 rtm->rtm_dst_len = rt6_dst->plen;
5805 rtm->rtm_src_len = rt6_src->plen;
5806 rtm->rtm_tos = 0;
5807 if (rt->fib6_table)
5808 table = rt->fib6_table->tb6_id;
5809 else
5810 table = RT6_TABLE_UNSPEC;
5811 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
5812 if (nla_put_u32(skb, RTA_TABLE, table))
5813 goto nla_put_failure;
5814
5815 rtm->rtm_type = rt->fib6_type;
5816 rtm->rtm_flags = 0;
5817 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
5818 rtm->rtm_protocol = rt->fib6_protocol;
5819
5820 if (rt6_flags & RTF_CACHE)
5821 rtm->rtm_flags |= RTM_F_CLONED;
5822
5823 if (dest) {
5824 if (nla_put_in6_addr(skb, RTA_DST, dest))
5825 goto nla_put_failure;
5826 rtm->rtm_dst_len = 128;
5827 } else if (rtm->rtm_dst_len)
5828 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
5829 goto nla_put_failure;
5830 #ifdef CONFIG_IPV6_SUBTREES
5831 if (src) {
5832 if (nla_put_in6_addr(skb, RTA_SRC, src))
5833 goto nla_put_failure;
5834 rtm->rtm_src_len = 128;
5835 } else if (rtm->rtm_src_len &&
5836 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
5837 goto nla_put_failure;
5838 #endif
5839 if (iif) {
5840 #ifdef CONFIG_IPV6_MROUTE
5841 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
5842 int err = ip6mr_get_route(net, skb, rtm, portid);
5843
5844 if (err == 0)
5845 return 0;
5846 if (err < 0)
5847 goto nla_put_failure;
5848 } else
5849 #endif
5850 if (nla_put_u32(skb, RTA_IIF, iif))
5851 goto nla_put_failure;
5852 } else if (dest) {
5853 struct in6_addr saddr_buf;
5854 if (ip6_route_get_saddr(net, rt, dest, 0, 0, &saddr_buf) == 0 &&
5855 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5856 goto nla_put_failure;
5857 }
5858
5859 if (rt->fib6_prefsrc.plen) {
5860 struct in6_addr saddr_buf;
5861 saddr_buf = rt->fib6_prefsrc.addr;
5862 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5863 goto nla_put_failure;
5864 }
5865
5866 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5867 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
5868 goto nla_put_failure;
5869
5870 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
5871 goto nla_put_failure;
5872
5873 /* For multipath routes, walk the siblings list and add
5874 * each as a nexthop within RTA_MULTIPATH.
5875 */
5876 if (rt6) {
5877 struct net_device *dev;
5878
5879 if (rt6_flags & RTF_GATEWAY &&
5880 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5881 goto nla_put_failure;
5882
5883 dev = dst_dev(dst);
5884 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
5885 goto nla_put_failure;
5886
5887 if (lwtunnel_fill_encap(skb, dst->lwtstate, RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
5888 goto nla_put_failure;
5889 } else if (READ_ONCE(rt->fib6_nsiblings)) {
5890 struct fib6_info *sibling;
5891 struct nlattr *mp;
5892
5893 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5894 if (!mp)
5895 goto nla_put_failure;
5896
5897 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
5898 rt->fib6_nh->fib_nh_weight, AF_INET6,
5899 0) < 0)
5900 goto nla_put_failure;
5901
5902 rcu_read_lock();
5903
5904 list_for_each_entry_rcu(sibling, &rt->fib6_siblings,
5905 fib6_siblings) {
5906 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
5907 sibling->fib6_nh->fib_nh_weight,
5908 AF_INET6, 0) < 0) {
5909 rcu_read_unlock();
5910
5911 goto nla_put_failure;
5912 }
5913 if (!READ_ONCE(rt->fib6_nsiblings))
5914 break;
5915 }
5916
5917 rcu_read_unlock();
5918
5919 nla_nest_end(skb, mp);
5920 } else if (rt->nh) {
5921 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5922 goto nla_put_failure;
5923
5924 if (nexthop_is_blackhole(rt->nh))
5925 rtm->rtm_type = RTN_BLACKHOLE;
5926
5927 if (READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode) &&
5928 rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
5929 goto nla_put_failure;
5930
5931 rtm->rtm_flags |= nh_flags;
5932 } else {
5933 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
5934 &nh_flags, false) < 0)
5935 goto nla_put_failure;
5936
5937 rtm->rtm_flags |= nh_flags;
5938 }
5939
5940 if (rt6_flags & RTF_EXPIRES) {
5941 expires = dst ? READ_ONCE(dst->expires) : rt->expires;
5942 expires -= jiffies;
5943 }
5944
5945 if (!dst) {
5946 if (READ_ONCE(rt->offload))
5947 rtm->rtm_flags |= RTM_F_OFFLOAD;
5948 if (READ_ONCE(rt->trap))
5949 rtm->rtm_flags |= RTM_F_TRAP;
5950 if (READ_ONCE(rt->offload_failed))
5951 rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
5952 }
5953
5954 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
5955 goto nla_put_failure;
5956
5957 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
5958 goto nla_put_failure;
5959
5960
5961 nlmsg_end(skb, nlh);
5962 return 0;
5963
5964 nla_put_failure:
5965 nlmsg_cancel(skb, nlh);
5966 return -EMSGSIZE;
5967 }
5968
fib6_info_nh_uses_dev(struct fib6_nh * nh,void * arg)5969 static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5970 {
5971 const struct net_device *dev = arg;
5972
5973 if (nh->fib_nh_dev == dev)
5974 return 1;
5975
5976 return 0;
5977 }
5978
fib6_info_uses_dev(const struct fib6_info * f6i,const struct net_device * dev)5979 static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5980 const struct net_device *dev)
5981 {
5982 if (f6i->nh) {
5983 struct net_device *_dev = (struct net_device *)dev;
5984
5985 return !!nexthop_for_each_fib6_nh(f6i->nh,
5986 fib6_info_nh_uses_dev,
5987 _dev);
5988 }
5989
5990 if (f6i->fib6_nh->fib_nh_dev == dev)
5991 return true;
5992
5993 if (READ_ONCE(f6i->fib6_nsiblings)) {
5994 const struct fib6_info *sibling;
5995
5996 rcu_read_lock();
5997 list_for_each_entry_rcu(sibling, &f6i->fib6_siblings,
5998 fib6_siblings) {
5999 if (sibling->fib6_nh->fib_nh_dev == dev) {
6000 rcu_read_unlock();
6001 return true;
6002 }
6003 if (!READ_ONCE(f6i->fib6_nsiblings))
6004 break;
6005 }
6006 rcu_read_unlock();
6007 }
6008 return false;
6009 }
6010
6011 struct fib6_nh_exception_dump_walker {
6012 struct rt6_rtnl_dump_arg *dump;
6013 struct fib6_info *rt;
6014 unsigned int flags;
6015 unsigned int skip;
6016 unsigned int count;
6017 };
6018
rt6_nh_dump_exceptions(struct fib6_nh * nh,void * arg)6019 static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
6020 {
6021 struct fib6_nh_exception_dump_walker *w = arg;
6022 struct rt6_rtnl_dump_arg *dump = w->dump;
6023 struct rt6_exception_bucket *bucket;
6024 struct rt6_exception *rt6_ex;
6025 int i, err;
6026
6027 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
6028 if (!bucket)
6029 return 0;
6030
6031 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
6032 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
6033 if (w->skip) {
6034 w->skip--;
6035 continue;
6036 }
6037
6038 /* Expiration of entries doesn't bump sernum, insertion
6039 * does. Removal is triggered by insertion, so we can
6040 * rely on the fact that if entries change between two
6041 * partial dumps, this node is scanned again completely,
6042 * see rt6_insert_exception() and fib6_dump_table().
6043 *
6044 * Count expired entries we go through as handled
6045 * entries that we'll skip next time, in case of partial
6046 * node dump. Otherwise, if entries expire meanwhile,
6047 * we'll skip the wrong amount.
6048 */
6049 if (rt6_check_expired(rt6_ex->rt6i)) {
6050 w->count++;
6051 continue;
6052 }
6053
6054 err = rt6_fill_node(dump->net, dump->skb, w->rt,
6055 &rt6_ex->rt6i->dst, NULL, NULL, 0,
6056 RTM_NEWROUTE,
6057 NETLINK_CB(dump->cb->skb).portid,
6058 dump->cb->nlh->nlmsg_seq, w->flags);
6059 if (err)
6060 return err;
6061
6062 w->count++;
6063 }
6064 bucket++;
6065 }
6066
6067 return 0;
6068 }
6069
6070 /* 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)6071 int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
6072 {
6073 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
6074 struct fib_dump_filter *filter = &arg->filter;
6075 unsigned int flags = NLM_F_MULTI;
6076 struct net *net = arg->net;
6077 int count = 0;
6078
6079 if (rt == net->ipv6.fib6_null_entry)
6080 return -1;
6081
6082 if ((filter->flags & RTM_F_PREFIX) &&
6083 !(rt->fib6_flags & RTF_PREFIX_RT)) {
6084 /* success since this is not a prefix route */
6085 return -1;
6086 }
6087 if (filter->filter_set &&
6088 ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
6089 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
6090 (filter->protocol && rt->fib6_protocol != filter->protocol))) {
6091 return -1;
6092 }
6093
6094 if (filter->filter_set ||
6095 !filter->dump_routes || !filter->dump_exceptions) {
6096 flags |= NLM_F_DUMP_FILTERED;
6097 }
6098
6099 if (filter->dump_routes) {
6100 if (skip) {
6101 skip--;
6102 } else {
6103 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
6104 0, RTM_NEWROUTE,
6105 NETLINK_CB(arg->cb->skb).portid,
6106 arg->cb->nlh->nlmsg_seq, flags)) {
6107 return 0;
6108 }
6109 count++;
6110 }
6111 }
6112
6113 if (filter->dump_exceptions) {
6114 struct fib6_nh_exception_dump_walker w = { .dump = arg,
6115 .rt = rt,
6116 .flags = flags,
6117 .skip = skip,
6118 .count = 0 };
6119 int err;
6120
6121 rcu_read_lock();
6122 if (rt->nh) {
6123 err = nexthop_for_each_fib6_nh(rt->nh,
6124 rt6_nh_dump_exceptions,
6125 &w);
6126 } else {
6127 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
6128 }
6129 rcu_read_unlock();
6130
6131 if (err)
6132 return count + w.count;
6133 }
6134
6135 return -1;
6136 }
6137
inet6_rtm_valid_getroute_req(struct sk_buff * skb,const struct nlmsghdr * nlh,struct nlattr ** tb,struct netlink_ext_ack * extack)6138 static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
6139 const struct nlmsghdr *nlh,
6140 struct nlattr **tb,
6141 struct netlink_ext_ack *extack)
6142 {
6143 struct rtmsg *rtm;
6144 int i, err;
6145
6146 rtm = nlmsg_payload(nlh, sizeof(*rtm));
6147 if (!rtm) {
6148 NL_SET_ERR_MSG_MOD(extack,
6149 "Invalid header for get route request");
6150 return -EINVAL;
6151 }
6152
6153 if (!netlink_strict_get_check(skb))
6154 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
6155 rtm_ipv6_policy, extack);
6156
6157 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
6158 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
6159 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
6160 rtm->rtm_type) {
6161 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
6162 return -EINVAL;
6163 }
6164 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
6165 NL_SET_ERR_MSG_MOD(extack,
6166 "Invalid flags for get route request");
6167 return -EINVAL;
6168 }
6169
6170 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
6171 rtm_ipv6_policy, extack);
6172 if (err)
6173 return err;
6174
6175 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
6176 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
6177 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
6178 return -EINVAL;
6179 }
6180
6181 if (tb[RTA_FLOWLABEL] &&
6182 (nla_get_be32(tb[RTA_FLOWLABEL]) & ~IPV6_FLOWLABEL_MASK)) {
6183 NL_SET_ERR_MSG_ATTR(extack, tb[RTA_FLOWLABEL],
6184 "Invalid flow label");
6185 return -EINVAL;
6186 }
6187
6188 for (i = 0; i <= RTA_MAX; i++) {
6189 if (!tb[i])
6190 continue;
6191
6192 switch (i) {
6193 case RTA_SRC:
6194 case RTA_DST:
6195 case RTA_IIF:
6196 case RTA_OIF:
6197 case RTA_MARK:
6198 case RTA_UID:
6199 case RTA_SPORT:
6200 case RTA_DPORT:
6201 case RTA_IP_PROTO:
6202 case RTA_FLOWLABEL:
6203 break;
6204 default:
6205 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
6206 return -EINVAL;
6207 }
6208 }
6209
6210 return 0;
6211 }
6212
inet6_rtm_getroute(struct sk_buff * in_skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)6213 static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
6214 struct netlink_ext_ack *extack)
6215 {
6216 struct net *net = sock_net(in_skb->sk);
6217 struct nlattr *tb[RTA_MAX+1];
6218 int err, iif = 0, oif = 0;
6219 struct fib6_info *from;
6220 struct dst_entry *dst;
6221 struct rt6_info *rt;
6222 struct sk_buff *skb;
6223 struct rtmsg *rtm;
6224 struct flowi6 fl6 = {};
6225 __be32 flowlabel;
6226 bool fibmatch;
6227
6228 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
6229 if (err < 0)
6230 goto errout;
6231
6232 err = -EINVAL;
6233 rtm = nlmsg_data(nlh);
6234 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
6235
6236 if (tb[RTA_SRC]) {
6237 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
6238 goto errout;
6239
6240 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
6241 }
6242
6243 if (tb[RTA_DST]) {
6244 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
6245 goto errout;
6246
6247 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
6248 }
6249
6250 if (tb[RTA_IIF])
6251 iif = nla_get_u32(tb[RTA_IIF]);
6252
6253 if (tb[RTA_OIF])
6254 oif = nla_get_u32(tb[RTA_OIF]);
6255
6256 if (tb[RTA_MARK])
6257 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
6258
6259 if (tb[RTA_UID])
6260 fl6.flowi6_uid = make_kuid(current_user_ns(),
6261 nla_get_u32(tb[RTA_UID]));
6262 else
6263 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
6264
6265 if (tb[RTA_SPORT])
6266 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
6267
6268 if (tb[RTA_DPORT])
6269 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
6270
6271 if (tb[RTA_IP_PROTO]) {
6272 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
6273 &fl6.flowi6_proto, AF_INET6,
6274 extack);
6275 if (err)
6276 goto errout;
6277 }
6278
6279 flowlabel = nla_get_be32_default(tb[RTA_FLOWLABEL], 0);
6280 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, flowlabel);
6281
6282 if (iif) {
6283 struct net_device *dev;
6284 int flags = 0;
6285
6286 rcu_read_lock();
6287
6288 dev = dev_get_by_index_rcu(net, iif);
6289 if (!dev) {
6290 rcu_read_unlock();
6291 err = -ENODEV;
6292 goto errout;
6293 }
6294
6295 fl6.flowi6_iif = iif;
6296
6297 if (!ipv6_addr_any(&fl6.saddr))
6298 flags |= RT6_LOOKUP_F_HAS_SADDR;
6299
6300 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
6301
6302 rcu_read_unlock();
6303 } else {
6304 fl6.flowi6_oif = oif;
6305
6306 dst = ip6_route_output(net, NULL, &fl6);
6307 }
6308
6309
6310 rt = dst_rt6_info(dst);
6311 if (rt->dst.error) {
6312 err = rt->dst.error;
6313 ip6_rt_put(rt);
6314 goto errout;
6315 }
6316
6317 if (rt == net->ipv6.ip6_null_entry) {
6318 err = rt->dst.error;
6319 ip6_rt_put(rt);
6320 goto errout;
6321 }
6322
6323 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
6324 if (!skb) {
6325 ip6_rt_put(rt);
6326 err = -ENOBUFS;
6327 goto errout;
6328 }
6329
6330 skb_dst_set(skb, &rt->dst);
6331
6332 rcu_read_lock();
6333 from = rcu_dereference(rt->from);
6334 if (from) {
6335 if (fibmatch)
6336 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
6337 iif, RTM_NEWROUTE,
6338 NETLINK_CB(in_skb).portid,
6339 nlh->nlmsg_seq, 0);
6340 else
6341 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
6342 &fl6.saddr, iif, RTM_NEWROUTE,
6343 NETLINK_CB(in_skb).portid,
6344 nlh->nlmsg_seq, 0);
6345 } else {
6346 err = -ENETUNREACH;
6347 }
6348 rcu_read_unlock();
6349
6350 if (err < 0) {
6351 kfree_skb(skb);
6352 goto errout;
6353 }
6354
6355 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
6356 errout:
6357 return err;
6358 }
6359
inet6_rt_notify(int event,struct fib6_info * rt,struct nl_info * info,unsigned int nlm_flags)6360 void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
6361 unsigned int nlm_flags)
6362 {
6363 struct net *net = info->nl_net;
6364 struct sk_buff *skb;
6365 size_t sz;
6366 u32 seq;
6367 int err;
6368
6369 err = -ENOBUFS;
6370 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6371
6372 rcu_read_lock();
6373 sz = rt6_nlmsg_size(rt);
6374 retry:
6375 skb = nlmsg_new(sz, GFP_ATOMIC);
6376 if (!skb)
6377 goto errout;
6378
6379 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6380 event, info->portid, seq, nlm_flags);
6381 if (err < 0) {
6382 kfree_skb(skb);
6383 /* -EMSGSIZE implies needed space grew under us. */
6384 if (err == -EMSGSIZE) {
6385 sz = max(rt6_nlmsg_size(rt), sz << 1);
6386 goto retry;
6387 }
6388 goto errout;
6389 }
6390
6391 rcu_read_unlock();
6392
6393 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6394 info->nlh, GFP_ATOMIC);
6395 return;
6396 errout:
6397 rcu_read_unlock();
6398 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6399 }
6400
fib6_rt_update(struct net * net,struct fib6_info * rt,struct nl_info * info)6401 void fib6_rt_update(struct net *net, struct fib6_info *rt,
6402 struct nl_info *info)
6403 {
6404 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6405 struct sk_buff *skb;
6406 int err = -ENOBUFS;
6407
6408 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6409 if (!skb)
6410 goto errout;
6411
6412 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6413 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
6414 if (err < 0) {
6415 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6416 WARN_ON(err == -EMSGSIZE);
6417 kfree_skb(skb);
6418 goto errout;
6419 }
6420 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6421 info->nlh, gfp_any());
6422 return;
6423 errout:
6424 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6425 }
6426
fib6_info_hw_flags_set(struct net * net,struct fib6_info * f6i,bool offload,bool trap,bool offload_failed)6427 void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i,
6428 bool offload, bool trap, bool offload_failed)
6429 {
6430 u8 fib_notify_on_flag_change;
6431 struct sk_buff *skb;
6432 int err;
6433
6434 if (READ_ONCE(f6i->offload) == offload &&
6435 READ_ONCE(f6i->trap) == trap &&
6436 READ_ONCE(f6i->offload_failed) == offload_failed)
6437 return;
6438
6439 WRITE_ONCE(f6i->offload, offload);
6440 WRITE_ONCE(f6i->trap, trap);
6441
6442 fib_notify_on_flag_change = READ_ONCE(net->ipv6.sysctl.fib_notify_on_flag_change);
6443 /* 2 means send notifications only if offload_failed was changed. */
6444 if (fib_notify_on_flag_change == 2 &&
6445 READ_ONCE(f6i->offload_failed) == offload_failed)
6446 return;
6447
6448 WRITE_ONCE(f6i->offload_failed, offload_failed);
6449
6450 if (!rcu_access_pointer(f6i->fib6_node))
6451 /* The route was removed from the tree, do not send
6452 * notification.
6453 */
6454 return;
6455
6456 if (!fib_notify_on_flag_change)
6457 return;
6458
6459 skb = nlmsg_new(rt6_nlmsg_size(f6i), GFP_KERNEL);
6460 if (!skb) {
6461 err = -ENOBUFS;
6462 goto errout;
6463 }
6464
6465 err = rt6_fill_node(net, skb, f6i, NULL, NULL, NULL, 0, RTM_NEWROUTE, 0,
6466 0, 0);
6467 if (err < 0) {
6468 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6469 WARN_ON(err == -EMSGSIZE);
6470 kfree_skb(skb);
6471 goto errout;
6472 }
6473
6474 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_ROUTE, NULL, GFP_KERNEL);
6475 return;
6476
6477 errout:
6478 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6479 }
6480 EXPORT_SYMBOL(fib6_info_hw_flags_set);
6481
ip6_route_dev_notify(struct notifier_block * this,unsigned long event,void * ptr)6482 static int ip6_route_dev_notify(struct notifier_block *this,
6483 unsigned long event, void *ptr)
6484 {
6485 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6486 struct net *net = dev_net(dev);
6487
6488 if (!(dev->flags & IFF_LOOPBACK))
6489 return NOTIFY_OK;
6490
6491 if (event == NETDEV_REGISTER) {
6492 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
6493 net->ipv6.ip6_null_entry->dst.dev = dev;
6494 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
6495 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6496 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
6497 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
6498 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
6499 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
6500 #endif
6501 } else if (event == NETDEV_UNREGISTER &&
6502 dev->reg_state != NETREG_UNREGISTERED) {
6503 /* NETDEV_UNREGISTER could be fired for multiple times by
6504 * netdev_wait_allrefs(). Make sure we only call this once.
6505 */
6506 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
6507 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6508 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
6509 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
6510 #endif
6511 }
6512
6513 return NOTIFY_OK;
6514 }
6515
6516 /*
6517 * /proc
6518 */
6519
6520 #ifdef CONFIG_PROC_FS
rt6_stats_seq_show(struct seq_file * seq,void * v)6521 static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6522 {
6523 struct net *net = (struct net *)seq->private;
6524 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
6525 net->ipv6.rt6_stats->fib_nodes,
6526 net->ipv6.rt6_stats->fib_route_nodes,
6527 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
6528 net->ipv6.rt6_stats->fib_rt_entries,
6529 net->ipv6.rt6_stats->fib_rt_cache,
6530 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
6531 net->ipv6.rt6_stats->fib_discarded_routes);
6532
6533 return 0;
6534 }
6535 #endif /* CONFIG_PROC_FS */
6536
6537 #ifdef CONFIG_SYSCTL
6538
ipv6_sysctl_rtcache_flush(const struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)6539 static int ipv6_sysctl_rtcache_flush(const struct ctl_table *ctl, int write,
6540 void *buffer, size_t *lenp, loff_t *ppos)
6541 {
6542 struct net *net;
6543 int delay;
6544 int ret;
6545 if (!write)
6546 return -EINVAL;
6547
6548 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6549 if (ret)
6550 return ret;
6551
6552 net = (struct net *)ctl->extra1;
6553 delay = READ_ONCE(net->ipv6.sysctl.flush_delay);
6554 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
6555 return 0;
6556 }
6557
6558 static struct ctl_table ipv6_route_table_template[] = {
6559 {
6560 .procname = "max_size",
6561 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
6562 .maxlen = sizeof(int),
6563 .mode = 0644,
6564 .proc_handler = proc_dointvec,
6565 },
6566 {
6567 .procname = "gc_thresh",
6568 .data = &ip6_dst_ops_template.gc_thresh,
6569 .maxlen = sizeof(int),
6570 .mode = 0644,
6571 .proc_handler = proc_dointvec,
6572 },
6573 {
6574 .procname = "flush",
6575 .data = &init_net.ipv6.sysctl.flush_delay,
6576 .maxlen = sizeof(int),
6577 .mode = 0200,
6578 .proc_handler = ipv6_sysctl_rtcache_flush
6579 },
6580 {
6581 .procname = "gc_min_interval",
6582 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6583 .maxlen = sizeof(int),
6584 .mode = 0644,
6585 .proc_handler = proc_dointvec_jiffies,
6586 },
6587 {
6588 .procname = "gc_timeout",
6589 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
6590 .maxlen = sizeof(int),
6591 .mode = 0644,
6592 .proc_handler = proc_dointvec_jiffies,
6593 },
6594 {
6595 .procname = "gc_interval",
6596 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
6597 .maxlen = sizeof(int),
6598 .mode = 0644,
6599 .proc_handler = proc_dointvec_jiffies,
6600 },
6601 {
6602 .procname = "gc_elasticity",
6603 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
6604 .maxlen = sizeof(int),
6605 .mode = 0644,
6606 .proc_handler = proc_dointvec,
6607 },
6608 {
6609 .procname = "mtu_expires",
6610 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
6611 .maxlen = sizeof(int),
6612 .mode = 0644,
6613 .proc_handler = proc_dointvec_jiffies,
6614 },
6615 {
6616 .procname = "min_adv_mss",
6617 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
6618 .maxlen = sizeof(int),
6619 .mode = 0644,
6620 .proc_handler = proc_dointvec,
6621 },
6622 {
6623 .procname = "gc_min_interval_ms",
6624 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6625 .maxlen = sizeof(int),
6626 .mode = 0644,
6627 .proc_handler = proc_dointvec_ms_jiffies,
6628 },
6629 {
6630 .procname = "skip_notify_on_dev_down",
6631 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
6632 .maxlen = sizeof(u8),
6633 .mode = 0644,
6634 .proc_handler = proc_dou8vec_minmax,
6635 .extra1 = SYSCTL_ZERO,
6636 .extra2 = SYSCTL_ONE,
6637 },
6638 };
6639
ipv6_route_sysctl_init(struct net * net)6640 struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
6641 {
6642 struct ctl_table *table;
6643
6644 table = kmemdup(ipv6_route_table_template,
6645 sizeof(ipv6_route_table_template),
6646 GFP_KERNEL);
6647
6648 if (table) {
6649 table[0].data = &net->ipv6.sysctl.ip6_rt_max_size;
6650 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
6651 table[2].data = &net->ipv6.sysctl.flush_delay;
6652 table[2].extra1 = net;
6653 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6654 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6655 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6656 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6657 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6658 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
6659 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6660 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
6661 }
6662
6663 return table;
6664 }
6665
ipv6_route_sysctl_table_size(struct net * net)6666 size_t ipv6_route_sysctl_table_size(struct net *net)
6667 {
6668 /* Don't export sysctls to unprivileged users */
6669 if (net->user_ns != &init_user_ns)
6670 return 1;
6671
6672 return ARRAY_SIZE(ipv6_route_table_template);
6673 }
6674 #endif
6675
ip6_route_net_init(struct net * net)6676 static int __net_init ip6_route_net_init(struct net *net)
6677 {
6678 int ret = -ENOMEM;
6679
6680 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6681 sizeof(net->ipv6.ip6_dst_ops));
6682
6683 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6684 goto out_ip6_dst_ops;
6685
6686 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
6687 if (!net->ipv6.fib6_null_entry)
6688 goto out_ip6_dst_entries;
6689 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6690 sizeof(*net->ipv6.fib6_null_entry));
6691
6692 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6693 sizeof(*net->ipv6.ip6_null_entry),
6694 GFP_KERNEL);
6695 if (!net->ipv6.ip6_null_entry)
6696 goto out_fib6_null_entry;
6697 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6698 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6699 ip6_template_metrics, true);
6700 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->dst.rt_uncached);
6701
6702 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6703 net->ipv6.fib6_has_custom_rules = false;
6704 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6705 sizeof(*net->ipv6.ip6_prohibit_entry),
6706 GFP_KERNEL);
6707 if (!net->ipv6.ip6_prohibit_entry)
6708 goto out_ip6_null_entry;
6709 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6710 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6711 ip6_template_metrics, true);
6712 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->dst.rt_uncached);
6713
6714 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6715 sizeof(*net->ipv6.ip6_blk_hole_entry),
6716 GFP_KERNEL);
6717 if (!net->ipv6.ip6_blk_hole_entry)
6718 goto out_ip6_prohibit_entry;
6719 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6720 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6721 ip6_template_metrics, true);
6722 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->dst.rt_uncached);
6723 #ifdef CONFIG_IPV6_SUBTREES
6724 net->ipv6.fib6_routes_require_src = 0;
6725 #endif
6726 #endif
6727
6728 net->ipv6.sysctl.flush_delay = 0;
6729 net->ipv6.sysctl.ip6_rt_max_size = INT_MAX;
6730 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6731 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6732 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6733 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6734 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6735 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
6736 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
6737
6738 atomic_set(&net->ipv6.ip6_rt_gc_expire, 30*HZ);
6739
6740 ret = 0;
6741 out:
6742 return ret;
6743
6744 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6745 out_ip6_prohibit_entry:
6746 kfree(net->ipv6.ip6_prohibit_entry);
6747 out_ip6_null_entry:
6748 kfree(net->ipv6.ip6_null_entry);
6749 #endif
6750 out_fib6_null_entry:
6751 kfree(net->ipv6.fib6_null_entry);
6752 out_ip6_dst_entries:
6753 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6754 out_ip6_dst_ops:
6755 goto out;
6756 }
6757
ip6_route_net_exit(struct net * net)6758 static void __net_exit ip6_route_net_exit(struct net *net)
6759 {
6760 kfree(net->ipv6.fib6_null_entry);
6761 kfree(net->ipv6.ip6_null_entry);
6762 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6763 kfree(net->ipv6.ip6_prohibit_entry);
6764 kfree(net->ipv6.ip6_blk_hole_entry);
6765 #endif
6766 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6767 }
6768
ip6_route_net_init_late(struct net * net)6769 static int __net_init ip6_route_net_init_late(struct net *net)
6770 {
6771 #ifdef CONFIG_PROC_FS
6772 if (!proc_create_net("ipv6_route", 0, net->proc_net,
6773 &ipv6_route_seq_ops,
6774 sizeof(struct ipv6_route_iter)))
6775 return -ENOMEM;
6776
6777 if (!proc_create_net_single("rt6_stats", 0444, net->proc_net,
6778 rt6_stats_seq_show, NULL)) {
6779 remove_proc_entry("ipv6_route", net->proc_net);
6780 return -ENOMEM;
6781 }
6782 #endif
6783 return 0;
6784 }
6785
ip6_route_net_exit_late(struct net * net)6786 static void __net_exit ip6_route_net_exit_late(struct net *net)
6787 {
6788 #ifdef CONFIG_PROC_FS
6789 remove_proc_entry("ipv6_route", net->proc_net);
6790 remove_proc_entry("rt6_stats", net->proc_net);
6791 #endif
6792 }
6793
6794 static struct pernet_operations ip6_route_net_ops = {
6795 .init = ip6_route_net_init,
6796 .exit = ip6_route_net_exit,
6797 };
6798
ipv6_inetpeer_init(struct net * net)6799 static int __net_init ipv6_inetpeer_init(struct net *net)
6800 {
6801 struct inet_peer_base *bp = kmalloc_obj(*bp);
6802
6803 if (!bp)
6804 return -ENOMEM;
6805 inet_peer_base_init(bp);
6806 net->ipv6.peers = bp;
6807 return 0;
6808 }
6809
ipv6_inetpeer_exit(struct net * net)6810 static void __net_exit ipv6_inetpeer_exit(struct net *net)
6811 {
6812 struct inet_peer_base *bp = net->ipv6.peers;
6813
6814 net->ipv6.peers = NULL;
6815 inetpeer_invalidate_tree(bp);
6816 kfree(bp);
6817 }
6818
6819 static struct pernet_operations ipv6_inetpeer_ops = {
6820 .init = ipv6_inetpeer_init,
6821 .exit = ipv6_inetpeer_exit,
6822 };
6823
6824 static struct pernet_operations ip6_route_net_late_ops = {
6825 .init = ip6_route_net_init_late,
6826 .exit = ip6_route_net_exit_late,
6827 };
6828
6829 static struct notifier_block ip6_route_dev_notifier = {
6830 .notifier_call = ip6_route_dev_notify,
6831 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
6832 };
6833
ip6_route_init_special_entries(void)6834 void __init ip6_route_init_special_entries(void)
6835 {
6836 /* Registering of the loopback is done before this portion of code,
6837 * the loopback reference in rt6_info will not be taken, do it
6838 * manually for init_net */
6839 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
6840 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6841 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6842 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6843 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6844 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6845 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6846 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6847 #endif
6848 }
6849
6850 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6851 DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
6852
6853 BTF_ID_LIST_SINGLE(btf_fib6_info_id, struct, fib6_info)
6854
6855 static const struct bpf_iter_seq_info ipv6_route_seq_info = {
6856 .seq_ops = &ipv6_route_seq_ops,
6857 .init_seq_private = bpf_iter_init_seq_net,
6858 .fini_seq_private = bpf_iter_fini_seq_net,
6859 .seq_priv_size = sizeof(struct ipv6_route_iter),
6860 };
6861
6862 static struct bpf_iter_reg ipv6_route_reg_info = {
6863 .target = "ipv6_route",
6864 .ctx_arg_info_size = 1,
6865 .ctx_arg_info = {
6866 { offsetof(struct bpf_iter__ipv6_route, rt),
6867 PTR_TO_BTF_ID_OR_NULL },
6868 },
6869 .seq_info = &ipv6_route_seq_info,
6870 };
6871
bpf_iter_register(void)6872 static int __init bpf_iter_register(void)
6873 {
6874 ipv6_route_reg_info.ctx_arg_info[0].btf_id = *btf_fib6_info_id;
6875 return bpf_iter_reg_target(&ipv6_route_reg_info);
6876 }
6877
bpf_iter_unregister(void)6878 static void bpf_iter_unregister(void)
6879 {
6880 bpf_iter_unreg_target(&ipv6_route_reg_info);
6881 }
6882 #endif
6883
6884 static const struct rtnl_msg_handler ip6_route_rtnl_msg_handlers[] __initconst_or_module = {
6885 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_NEWROUTE,
6886 .doit = inet6_rtm_newroute, .flags = RTNL_FLAG_DOIT_UNLOCKED},
6887 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_DELROUTE,
6888 .doit = inet6_rtm_delroute, .flags = RTNL_FLAG_DOIT_UNLOCKED},
6889 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETROUTE,
6890 .doit = inet6_rtm_getroute, .flags = RTNL_FLAG_DOIT_UNLOCKED},
6891 };
6892
ip6_route_init(void)6893 int __init ip6_route_init(void)
6894 {
6895 int ret;
6896 int cpu;
6897
6898 ret = -ENOMEM;
6899 ip6_dst_ops_template.kmem_cachep =
6900 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6901 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
6902 if (!ip6_dst_ops_template.kmem_cachep)
6903 goto out;
6904
6905 ret = dst_entries_init(&ip6_dst_blackhole_ops);
6906 if (ret)
6907 goto out_kmem_cache;
6908
6909 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6910 if (ret)
6911 goto out_dst_entries;
6912
6913 ret = register_pernet_subsys(&ip6_route_net_ops);
6914 if (ret)
6915 goto out_register_inetpeer;
6916
6917 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6918
6919 ret = fib6_init();
6920 if (ret)
6921 goto out_register_subsys;
6922
6923 ret = xfrm6_init();
6924 if (ret)
6925 goto out_fib6_init;
6926
6927 ret = fib6_rules_init();
6928 if (ret)
6929 goto xfrm6_init;
6930
6931 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6932 if (ret)
6933 goto fib6_rules_init;
6934
6935 ret = rtnl_register_many(ip6_route_rtnl_msg_handlers);
6936 if (ret < 0)
6937 goto out_register_late_subsys;
6938
6939 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
6940 if (ret)
6941 goto out_register_late_subsys;
6942
6943 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6944 ret = bpf_iter_register();
6945 if (ret)
6946 goto out_register_notifier;
6947 #endif
6948
6949 for_each_possible_cpu(cpu) {
6950 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6951
6952 INIT_LIST_HEAD(&ul->head);
6953 spin_lock_init(&ul->lock);
6954 }
6955
6956 out:
6957 return ret;
6958
6959 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6960 out_register_notifier:
6961 unregister_netdevice_notifier(&ip6_route_dev_notifier);
6962 #endif
6963 out_register_late_subsys:
6964 rtnl_unregister_all(PF_INET6);
6965 unregister_pernet_subsys(&ip6_route_net_late_ops);
6966 fib6_rules_init:
6967 fib6_rules_cleanup();
6968 xfrm6_init:
6969 xfrm6_fini();
6970 out_fib6_init:
6971 fib6_gc_cleanup();
6972 out_register_subsys:
6973 unregister_pernet_subsys(&ip6_route_net_ops);
6974 out_register_inetpeer:
6975 unregister_pernet_subsys(&ipv6_inetpeer_ops);
6976 out_dst_entries:
6977 dst_entries_destroy(&ip6_dst_blackhole_ops);
6978 out_kmem_cache:
6979 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6980 goto out;
6981 }
6982
ip6_route_cleanup(void)6983 void ip6_route_cleanup(void)
6984 {
6985 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6986 bpf_iter_unregister();
6987 #endif
6988 unregister_netdevice_notifier(&ip6_route_dev_notifier);
6989 unregister_pernet_subsys(&ip6_route_net_late_ops);
6990 fib6_rules_cleanup();
6991 xfrm6_fini();
6992 fib6_gc_cleanup();
6993 unregister_pernet_subsys(&ipv6_inetpeer_ops);
6994 unregister_pernet_subsys(&ip6_route_net_ops);
6995 dst_entries_destroy(&ip6_dst_blackhole_ops);
6996 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6997 }
6998