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