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