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