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