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