blob: b91a9d3cf28841e4a6c5acd7f940b9fcade6fcd8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux INET6 implementation
3 * FIB front-end.
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14/* Changes:
15 *
16 * YOSHIFUJI Hideaki @USAGI
17 * reworked default router selection.
18 * - respect outgoing interface
19 * - select from (probably) reachable routers (i.e.
20 * routers in REACHABLE, STALE, DELAY or PROBE states).
21 * - always select the same router if it is (probably)
22 * reachable. otherwise, round-robin the list.
YOSHIFUJI Hideakic0bece92006-08-23 17:23:25 -070023 * Ville Nuorvala
24 * Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Joe Perchesf3213832012-05-15 14:11:53 +000027#define pr_fmt(fmt) "IPv6: " fmt
28
Randy Dunlap4fc268d2006-01-11 12:17:47 -080029#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/errno.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040031#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/types.h>
33#include <linux/times.h>
34#include <linux/socket.h>
35#include <linux/sockios.h>
36#include <linux/net.h>
37#include <linux/route.h>
38#include <linux/netdevice.h>
39#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090040#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/proc_fs.h>
44#include <linux/seq_file.h>
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080045#include <linux/nsproxy.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Wei Wang35732d02017-10-06 12:05:57 -070047#include <linux/jhash.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020048#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <net/snmp.h>
50#include <net/ipv6.h>
51#include <net/ip6_fib.h>
52#include <net/ip6_route.h>
53#include <net/ndisc.h>
54#include <net/addrconf.h>
55#include <net/tcp.h>
56#include <linux/rtnetlink.h>
57#include <net/dst.h>
Jiri Benc904af042015-08-20 13:56:31 +020058#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <net/xfrm.h>
Tom Tucker8d717402006-07-30 20:43:36 -070060#include <net/netevent.h>
Thomas Graf21713eb2006-08-15 00:35:24 -070061#include <net/netlink.h>
Nicolas Dichtel51ebd312012-10-22 03:42:09 +000062#include <net/nexthop.h>
Roopa Prabhu19e42e42015-07-21 10:43:48 +020063#include <net/lwtunnel.h>
Jiri Benc904af042015-08-20 13:56:31 +020064#include <net/ip_tunnels.h>
David Ahernca254492015-10-12 11:47:10 -070065#include <net/l3mdev.h>
Roopa Prabhueacb9382018-05-22 14:03:28 -070066#include <net/ip.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080067#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#ifdef CONFIG_SYSCTL
70#include <linux/sysctl.h>
71#endif
72
David Ahern30d444d2018-05-23 17:08:48 -070073static int ip6_rt_type_to_error(u8 fib6_type);
74
75#define CREATE_TRACE_POINTS
76#include <trace/events/fib6.h>
77EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
78#undef CREATE_TRACE_POINTS
79
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020080enum rt6_nud_state {
Jiri Benc7e980562013-12-11 13:48:20 +010081 RT6_NUD_FAIL_HARD = -3,
82 RT6_NUD_FAIL_PROBE = -2,
83 RT6_NUD_FAIL_DO_RR = -1,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020084 RT6_NUD_SUCCEED = 1
85};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
David S. Miller0dbaee32010-12-13 12:52:14 -080088static unsigned int ip6_default_advmss(const struct dst_entry *dst);
Steffen Klassertebb762f2011-11-23 02:12:51 +000089static unsigned int ip6_mtu(const struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static struct dst_entry *ip6_negative_advice(struct dst_entry *);
91static void ip6_dst_destroy(struct dst_entry *);
92static void ip6_dst_ifdown(struct dst_entry *,
93 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080094static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96static int ip6_pkt_discard(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050097static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Kamala R7150aed2013-12-02 19:55:21 +053098static int ip6_pkt_prohibit(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050099static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static void ip6_link_failure(struct sk_buff *skb);
David S. Miller6700c272012-07-17 03:29:28 -0700101static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
102 struct sk_buff *skb, u32 mtu);
103static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
104 struct sk_buff *skb);
David Ahern8d1c8022018-04-17 17:33:26 -0700105static int rt6_score_route(struct fib6_info *rt, int oif, int strict);
106static size_t rt6_nlmsg_size(struct fib6_info *rt);
David Ahernd4ead6b2018-04-17 17:33:16 -0700107static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -0700108 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -0700109 struct in6_addr *dest, struct in6_addr *src,
David Ahern16a16cd2017-02-02 12:37:11 -0800110 int iif, int type, u32 portid, u32 seq,
111 unsigned int flags);
David Ahern8d1c8022018-04-17 17:33:26 -0700112static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt,
Wei Wang35732d02017-10-06 12:05:57 -0700113 struct in6_addr *daddr,
114 struct in6_addr *saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800116#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -0700117static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000118 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700119 const struct in6_addr *gwaddr,
120 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +0000121 unsigned int pref);
David Ahern8d1c8022018-04-17 17:33:26 -0700122static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000123 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700124 const struct in6_addr *gwaddr,
125 struct net_device *dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800126#endif
127
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700128struct uncached_list {
129 spinlock_t lock;
130 struct list_head head;
131};
132
133static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
134
Xin Long510c3212018-02-14 19:06:02 +0800135void rt6_uncached_list_add(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700136{
137 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
138
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700139 rt->rt6i_uncached_list = ul;
140
141 spin_lock_bh(&ul->lock);
142 list_add_tail(&rt->rt6i_uncached, &ul->head);
143 spin_unlock_bh(&ul->lock);
144}
145
Xin Long510c3212018-02-14 19:06:02 +0800146void rt6_uncached_list_del(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700147{
148 if (!list_empty(&rt->rt6i_uncached)) {
149 struct uncached_list *ul = rt->rt6i_uncached_list;
Wei Wang81eb8442017-10-06 12:06:11 -0700150 struct net *net = dev_net(rt->dst.dev);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700151
152 spin_lock_bh(&ul->lock);
153 list_del(&rt->rt6i_uncached);
Wei Wang81eb8442017-10-06 12:06:11 -0700154 atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700155 spin_unlock_bh(&ul->lock);
156 }
157}
158
159static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
160{
161 struct net_device *loopback_dev = net->loopback_dev;
162 int cpu;
163
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500164 if (dev == loopback_dev)
165 return;
166
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700167 for_each_possible_cpu(cpu) {
168 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
169 struct rt6_info *rt;
170
171 spin_lock_bh(&ul->lock);
172 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
173 struct inet6_dev *rt_idev = rt->rt6i_idev;
174 struct net_device *rt_dev = rt->dst.dev;
175
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500176 if (rt_idev->dev == dev) {
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700177 rt->rt6i_idev = in6_dev_get(loopback_dev);
178 in6_dev_put(rt_idev);
179 }
180
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500181 if (rt_dev == dev) {
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700182 rt->dst.dev = loopback_dev;
183 dev_hold(rt->dst.dev);
184 dev_put(rt_dev);
185 }
186 }
187 spin_unlock_bh(&ul->lock);
188 }
189}
190
David Ahernf8a1b432018-04-17 17:33:21 -0700191static inline const void *choose_neigh_daddr(const struct in6_addr *p,
David S. Millerf894cbf2012-07-02 21:52:24 -0700192 struct sk_buff *skb,
193 const void *daddr)
David S. Miller39232972012-01-26 15:22:32 -0500194{
David S. Millera7563f32012-01-26 16:29:16 -0500195 if (!ipv6_addr_any(p))
David S. Miller39232972012-01-26 15:22:32 -0500196 return (const void *) p;
David S. Millerf894cbf2012-07-02 21:52:24 -0700197 else if (skb)
198 return &ipv6_hdr(skb)->daddr;
David S. Miller39232972012-01-26 15:22:32 -0500199 return daddr;
200}
201
David Ahernf8a1b432018-04-17 17:33:21 -0700202struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
203 struct net_device *dev,
204 struct sk_buff *skb,
205 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700206{
David S. Miller39232972012-01-26 15:22:32 -0500207 struct neighbour *n;
208
David Ahernf8a1b432018-04-17 17:33:21 -0700209 daddr = choose_neigh_daddr(gw, skb, daddr);
210 n = __ipv6_neigh_lookup(dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500211 if (n)
212 return n;
David Ahernf8a1b432018-04-17 17:33:21 -0700213 return neigh_create(&nd_tbl, daddr, dev);
214}
215
216static 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(&rt->rt6i_gateway, dst->dev, skb, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500223}
224
Julian Anastasov63fca652017-02-06 23:14:15 +0200225static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
226{
227 struct net_device *dev = dst->dev;
228 struct rt6_info *rt = (struct rt6_info *)dst;
229
David Ahernf8a1b432018-04-17 17:33:21 -0700230 daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +0200231 if (!daddr)
232 return;
233 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
234 return;
235 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
236 return;
237 __ipv6_confirm_neigh(dev, daddr);
238}
239
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800240static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .gc = ip6_dst_gc,
243 .gc_thresh = 1024,
244 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800245 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000246 .mtu = ip6_mtu,
David Ahernd4ead6b2018-04-17 17:33:16 -0700247 .cow_metrics = dst_cow_metrics_generic,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 .destroy = ip6_dst_destroy,
249 .ifdown = ip6_dst_ifdown,
250 .negative_advice = ip6_negative_advice,
251 .link_failure = ip6_link_failure,
252 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700253 .redirect = rt6_do_redirect,
Eric W. Biederman9f8955c2015-10-07 16:48:39 -0500254 .local_out = __ip6_local_out,
David Ahernf8a1b432018-04-17 17:33:21 -0700255 .neigh_lookup = ip6_dst_neigh_lookup,
Julian Anastasov63fca652017-02-06 23:14:15 +0200256 .confirm_neigh = ip6_confirm_neigh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257};
258
Steffen Klassertebb762f2011-11-23 02:12:51 +0000259static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800260{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000261 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
262
263 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800264}
265
David S. Miller6700c272012-07-17 03:29:28 -0700266static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
267 struct sk_buff *skb, u32 mtu)
David S. Miller14e50e52007-05-24 18:17:54 -0700268{
269}
270
David S. Miller6700c272012-07-17 03:29:28 -0700271static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
272 struct sk_buff *skb)
David S. Millerb587ee32012-07-12 00:39:24 -0700273{
274}
275
David S. Miller14e50e52007-05-24 18:17:54 -0700276static struct dst_ops ip6_dst_blackhole_ops = {
277 .family = AF_INET6,
David S. Miller14e50e52007-05-24 18:17:54 -0700278 .destroy = ip6_dst_destroy,
279 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000280 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800281 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700282 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Millerb587ee32012-07-12 00:39:24 -0700283 .redirect = ip6_rt_blackhole_redirect,
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -0700284 .cow_metrics = dst_cow_metrics_generic,
David Ahernf8a1b432018-04-17 17:33:21 -0700285 .neigh_lookup = ip6_dst_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700286};
287
David S. Miller62fa8a82011-01-26 20:51:05 -0800288static const u32 ip6_template_metrics[RTAX_MAX] = {
Li RongQing14edd872012-10-24 14:01:18 +0800289 [RTAX_HOPLIMIT - 1] = 0,
David S. Miller62fa8a82011-01-26 20:51:05 -0800290};
291
David Ahern8d1c8022018-04-17 17:33:26 -0700292static const struct fib6_info fib6_null_entry_template = {
David Ahern93c2fb22018-04-18 15:38:59 -0700293 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
294 .fib6_protocol = RTPROT_KERNEL,
295 .fib6_metric = ~(u32)0,
296 .fib6_ref = ATOMIC_INIT(1),
David Ahern421842e2018-04-17 17:33:18 -0700297 .fib6_type = RTN_UNREACHABLE,
298 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
299};
300
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000301static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700302 .dst = {
303 .__refcnt = ATOMIC_INIT(1),
304 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000305 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700306 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700307 .input = ip6_pkt_discard,
308 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 },
310 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
Thomas Graf101367c2006-08-04 03:39:02 -0700313#ifdef CONFIG_IPV6_MULTIPLE_TABLES
314
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000315static const struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700316 .dst = {
317 .__refcnt = ATOMIC_INIT(1),
318 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000319 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700320 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700321 .input = ip6_pkt_prohibit,
322 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700323 },
324 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700325};
326
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000327static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700328 .dst = {
329 .__refcnt = ATOMIC_INIT(1),
330 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000331 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700332 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700333 .input = dst_discard,
Eric W. Biedermanede20592015-10-07 16:48:47 -0500334 .output = dst_discard_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700335 },
336 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700337};
338
339#endif
340
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700341static void rt6_info_init(struct rt6_info *rt)
342{
343 struct dst_entry *dst = &rt->dst;
344
345 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700346 INIT_LIST_HEAD(&rt->rt6i_uncached);
347}
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349/* allocate dst with ip6_dst_ops */
David Ahern93531c62018-04-17 17:33:25 -0700350struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
351 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
David S. Miller97bab732012-06-09 22:36:36 -0700353 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Wei Wangb2a9c0e2017-06-17 10:42:41 -0700354 1, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700355
Wei Wang81eb8442017-10-06 12:06:11 -0700356 if (rt) {
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700357 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -0700358 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
359 }
Steffen Klassert81048912012-07-05 23:37:09 +0000360
David S. Millercf911662011-04-28 14:31:47 -0700361 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
David Ahern9ab179d2016-04-07 11:10:06 -0700363EXPORT_SYMBOL(ip6_dst_alloc);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365static void ip6_dst_destroy(struct dst_entry *dst)
366{
Wei Wangce7ea4a2018-09-18 13:45:00 -0700367 struct dst_metrics *p = (struct dst_metrics *)DST_METRICS_PTR(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 struct rt6_info *rt = (struct rt6_info *)dst;
David Aherna68886a2018-04-20 15:38:02 -0700369 struct fib6_info *from;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700370 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Wei Wangce7ea4a2018-09-18 13:45:00 -0700372 if (p != &dst_default_metrics && refcount_dec_and_test(&p->refcnt))
373 kfree(p);
374
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700375 rt6_uncached_list_del(rt);
376
377 idev = rt->rt6i_idev;
David S. Miller38308472011-12-03 18:02:47 -0500378 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 rt->rt6i_idev = NULL;
380 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900381 }
Gao feng1716a962012-04-06 00:13:10 +0000382
David Aherna68886a2018-04-20 15:38:02 -0700383 rcu_read_lock();
384 from = rcu_dereference(rt->from);
385 rcu_assign_pointer(rt->from, NULL);
David Ahern93531c62018-04-17 17:33:25 -0700386 fib6_info_release(from);
David Aherna68886a2018-04-20 15:38:02 -0700387 rcu_read_unlock();
David S. Millerb3419362010-11-30 12:27:11 -0800388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
391 int how)
392{
393 struct rt6_info *rt = (struct rt6_info *)dst;
394 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800395 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900396 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Wei Wange5645f52017-08-14 10:44:59 -0700398 if (idev && idev->dev != loopback_dev) {
399 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
400 if (loopback_idev) {
401 rt->rt6i_idev = loopback_idev;
402 in6_dev_put(idev);
David S. Miller97cac082012-07-02 22:43:47 -0700403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405}
406
Martin KaFai Lau5973fb12015-11-11 11:51:07 -0800407static bool __rt6_check_expired(const struct rt6_info *rt)
408{
409 if (rt->rt6i_flags & RTF_EXPIRES)
410 return time_after(jiffies, rt->dst.expires);
411 else
412 return false;
413}
414
Eric Dumazeta50feda2012-05-18 18:57:34 +0000415static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
David Aherna68886a2018-04-20 15:38:02 -0700417 struct fib6_info *from;
418
419 from = rcu_dereference(rt->from);
420
Gao feng1716a962012-04-06 00:13:10 +0000421 if (rt->rt6i_flags & RTF_EXPIRES) {
422 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000423 return true;
David Aherna68886a2018-04-20 15:38:02 -0700424 } else if (from) {
Xin Long1e2ea8a2017-08-26 20:10:10 +0800425 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
David Aherna68886a2018-04-20 15:38:02 -0700426 fib6_check_expired(from);
Gao feng1716a962012-04-06 00:13:10 +0000427 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000428 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
David Ahern3b290a32018-05-09 20:34:20 -0700431struct fib6_info *fib6_multipath_select(const struct net *net,
432 struct fib6_info *match,
433 struct flowi6 *fl6, int oif,
434 const struct sk_buff *skb,
435 int strict)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000436{
David Ahern8d1c8022018-04-17 17:33:26 -0700437 struct fib6_info *sibling, *next_sibling;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000438
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200439 /* We might have already computed the hash for ICMPv6 errors. In such
440 * case it will always be non-zero. Otherwise now is the time to do it.
441 */
442 if (!fl6->mp_hash)
David Ahernb4bac172018-03-02 08:32:18 -0800443 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200444
David Ahern5e670d82018-04-17 17:33:14 -0700445 if (fl6->mp_hash <= atomic_read(&match->fib6_nh.nh_upper_bound))
Ido Schimmel3d709f62018-01-09 16:40:27 +0200446 return match;
Ido Schimmelbbfcd772017-11-21 09:50:12 +0200447
David Ahern93c2fb22018-04-18 15:38:59 -0700448 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
449 fib6_siblings) {
David Ahern5e670d82018-04-17 17:33:14 -0700450 int nh_upper_bound;
451
452 nh_upper_bound = atomic_read(&sibling->fib6_nh.nh_upper_bound);
453 if (fl6->mp_hash > nh_upper_bound)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200454 continue;
455 if (rt6_score_route(sibling, oif, strict) < 0)
456 break;
457 match = sibling;
458 break;
459 }
460
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000461 return match;
462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/*
Wei Wang66f5d6c2017-10-06 12:06:10 -0700465 * Route lookup. rcu_read_lock() should be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 */
467
David Ahern8d1c8022018-04-17 17:33:26 -0700468static inline struct fib6_info *rt6_device_match(struct net *net,
469 struct fib6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000470 const struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700472 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
David Ahern8d1c8022018-04-17 17:33:26 -0700474 struct fib6_info *sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
David Ahern5e670d82018-04-17 17:33:14 -0700476 if (!oif && ipv6_addr_any(saddr) &&
477 !(rt->fib6_nh.nh_flags & RTNH_F_DEAD))
Ido Schimmel8067bb82018-01-07 12:45:09 +0200478 return rt;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900479
David Ahern8fb11a92018-05-04 13:54:24 -0700480 for (sprt = rt; sprt; sprt = rcu_dereference(sprt->fib6_next)) {
David Ahern5e670d82018-04-17 17:33:14 -0700481 const struct net_device *dev = sprt->fib6_nh.nh_dev;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900482
David Ahern5e670d82018-04-17 17:33:14 -0700483 if (sprt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +0200484 continue;
485
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900486 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (dev->ifindex == oif)
488 return sprt;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900489 } else {
490 if (ipv6_chk_addr(net, saddr, dev,
491 flags & RT6_LOOKUP_F_IFACE))
492 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
David Aherneea68cd2018-04-18 15:39:02 -0700496 if (oif && flags & RT6_LOOKUP_F_IFACE)
497 return net->ipv6.fib6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
David Ahern421842e2018-04-17 17:33:18 -0700499 return rt->fib6_nh.nh_flags & RTNH_F_DEAD ? net->ipv6.fib6_null_entry : rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800502#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200503struct __rt6_probe_work {
504 struct work_struct work;
505 struct in6_addr target;
506 struct net_device *dev;
507};
508
509static void rt6_probe_deferred(struct work_struct *w)
510{
511 struct in6_addr mcaddr;
512 struct __rt6_probe_work *work =
513 container_of(w, struct __rt6_probe_work, work);
514
515 addrconf_addr_solict_mult(&work->target, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800516 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200517 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100518 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200519}
520
David Ahern8d1c8022018-04-17 17:33:26 -0700521static void rt6_probe(struct fib6_info *rt)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800522{
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700523 struct __rt6_probe_work *work;
David Ahern5e670d82018-04-17 17:33:14 -0700524 const struct in6_addr *nh_gw;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000525 struct neighbour *neigh;
David Ahern5e670d82018-04-17 17:33:14 -0700526 struct net_device *dev;
527
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800528 /*
529 * Okay, this does not seem to be appropriate
530 * for now, however, we need to check if it
531 * is really so; aka Router Reachability Probing.
532 *
533 * Router Reachability Probe MUST be rate-limited
534 * to no more than one per minute.
535 */
David Ahern93c2fb22018-04-18 15:38:59 -0700536 if (!rt || !(rt->fib6_flags & RTF_GATEWAY))
Amerigo Wangfdd66812012-09-10 02:48:44 +0000537 return;
David Ahern5e670d82018-04-17 17:33:14 -0700538
539 nh_gw = &rt->fib6_nh.nh_gw;
540 dev = rt->fib6_nh.nh_dev;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000541 rcu_read_lock_bh();
David Ahern5e670d82018-04-17 17:33:14 -0700542 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000543 if (neigh) {
David Aherndcd1f572018-04-18 15:39:05 -0700544 struct inet6_dev *idev;
545
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700546 if (neigh->nud_state & NUD_VALID)
547 goto out;
548
David Aherndcd1f572018-04-18 15:39:05 -0700549 idev = __in6_dev_get(dev);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700550 work = NULL;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000551 write_lock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700552 if (!(neigh->nud_state & NUD_VALID) &&
553 time_after(jiffies,
David Aherndcd1f572018-04-18 15:39:05 -0700554 neigh->updated + idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700555 work = kmalloc(sizeof(*work), GFP_ATOMIC);
556 if (work)
557 __neigh_set_probe_once(neigh);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200558 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000559 write_unlock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700560 } else {
561 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000562 }
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700563
564 if (work) {
565 INIT_WORK(&work->work, rt6_probe_deferred);
David Ahern5e670d82018-04-17 17:33:14 -0700566 work->target = *nh_gw;
567 dev_hold(dev);
568 work->dev = dev;
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700569 schedule_work(&work->work);
570 }
571
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700572out:
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000573 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800574}
575#else
David Ahern8d1c8022018-04-17 17:33:26 -0700576static inline void rt6_probe(struct fib6_info *rt)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800577{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800578}
579#endif
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800582 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 */
David Ahern8d1c8022018-04-17 17:33:26 -0700584static inline int rt6_check_dev(struct fib6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
David Ahern5e670d82018-04-17 17:33:14 -0700586 const struct net_device *dev = rt->fib6_nh.nh_dev;
587
David S. Miller161980f2007-04-06 11:42:27 -0700588 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800589 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
David Ahern8d1c8022018-04-17 17:33:26 -0700593static inline enum rt6_nud_state rt6_check_neigh(struct fib6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200595 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
David Ahern5e670d82018-04-17 17:33:14 -0700596 struct neighbour *neigh;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000597
David Ahern93c2fb22018-04-18 15:38:59 -0700598 if (rt->fib6_flags & RTF_NONEXTHOP ||
599 !(rt->fib6_flags & RTF_GATEWAY))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200600 return RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000601
602 rcu_read_lock_bh();
David Ahern5e670d82018-04-17 17:33:14 -0700603 neigh = __ipv6_neigh_lookup_noref(rt->fib6_nh.nh_dev,
604 &rt->fib6_nh.nh_gw);
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000605 if (neigh) {
606 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800607 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200608 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800609#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000610 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200611 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100612 else
613 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800614#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000615 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200616 } else {
617 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100618 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000619 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000620 rcu_read_unlock_bh();
621
Paul Marksa5a81f02012-12-03 10:26:54 +0000622 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800623}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
David Ahern8d1c8022018-04-17 17:33:26 -0700625static int rt6_score_route(struct fib6_info *rt, int oif, int strict)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800626{
Paul Marksa5a81f02012-12-03 10:26:54 +0000627 int m;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900628
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700629 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700630 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200631 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800632#ifdef CONFIG_IPV6_ROUTER_PREF
David Ahern93c2fb22018-04-18 15:38:59 -0700633 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->fib6_flags)) << 2;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800634#endif
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200635 if (strict & RT6_LOOKUP_F_REACHABLE) {
636 int n = rt6_check_neigh(rt);
637 if (n < 0)
638 return n;
639 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800640 return m;
641}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
David Aherndcd1f572018-04-18 15:39:05 -0700643/* called with rc_read_lock held */
644static inline bool fib6_ignore_linkdown(const struct fib6_info *f6i)
645{
646 const struct net_device *dev = fib6_info_nh_dev(f6i);
647 bool rc = false;
648
649 if (dev) {
650 const struct inet6_dev *idev = __in6_dev_get(dev);
651
652 rc = !!idev->cnf.ignore_routes_with_linkdown;
653 }
654
655 return rc;
656}
657
David Ahern8d1c8022018-04-17 17:33:26 -0700658static struct fib6_info *find_match(struct fib6_info *rt, int oif, int strict,
659 int *mpri, struct fib6_info *match,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200660 bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800661{
David S. Millerf11e6652007-03-24 20:36:25 -0700662 int m;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200663 bool match_do_rr = false;
Andy Gospodarek35103d12015-08-13 10:39:01 -0400664
David Ahern5e670d82018-04-17 17:33:14 -0700665 if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +0200666 goto out;
667
David Aherndcd1f572018-04-18 15:39:05 -0700668 if (fib6_ignore_linkdown(rt) &&
David Ahern5e670d82018-04-17 17:33:14 -0700669 rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN &&
David Ahernd5d32e42016-10-24 12:27:23 -0700670 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
Andy Gospodarek35103d12015-08-13 10:39:01 -0400671 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700672
David Ahern14895682018-04-17 17:33:17 -0700673 if (fib6_check_expired(rt))
David S. Millerf11e6652007-03-24 20:36:25 -0700674 goto out;
675
676 m = rt6_score_route(rt, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100677 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200678 match_do_rr = true;
679 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100680 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700681 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700682 }
683
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200684 if (strict & RT6_LOOKUP_F_REACHABLE)
685 rt6_probe(rt);
686
Jiri Benc7e980562013-12-11 13:48:20 +0100687 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200688 if (m > *mpri) {
689 *do_rr = match_do_rr;
690 *mpri = m;
691 match = rt;
692 }
David S. Millerf11e6652007-03-24 20:36:25 -0700693out:
694 return match;
695}
696
David Ahern8d1c8022018-04-17 17:33:26 -0700697static struct fib6_info *find_rr_leaf(struct fib6_node *fn,
698 struct fib6_info *leaf,
699 struct fib6_info *rr_head,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200700 u32 metric, int oif, int strict,
701 bool *do_rr)
David S. Millerf11e6652007-03-24 20:36:25 -0700702{
David Ahern8d1c8022018-04-17 17:33:26 -0700703 struct fib6_info *rt, *match, *cont;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800704 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
David S. Millerf11e6652007-03-24 20:36:25 -0700706 match = NULL;
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700707 cont = NULL;
David Ahern8fb11a92018-05-04 13:54:24 -0700708 for (rt = rr_head; rt; rt = rcu_dereference(rt->fib6_next)) {
David Ahern93c2fb22018-04-18 15:38:59 -0700709 if (rt->fib6_metric != metric) {
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700710 cont = rt;
711 break;
712 }
713
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200714 match = find_match(rt, oif, strict, &mpri, match, do_rr);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700715 }
716
Wei Wang66f5d6c2017-10-06 12:06:10 -0700717 for (rt = leaf; rt && rt != rr_head;
David Ahern8fb11a92018-05-04 13:54:24 -0700718 rt = rcu_dereference(rt->fib6_next)) {
David Ahern93c2fb22018-04-18 15:38:59 -0700719 if (rt->fib6_metric != metric) {
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700720 cont = rt;
721 break;
722 }
723
724 match = find_match(rt, oif, strict, &mpri, match, do_rr);
725 }
726
727 if (match || !cont)
728 return match;
729
David Ahern8fb11a92018-05-04 13:54:24 -0700730 for (rt = cont; rt; rt = rcu_dereference(rt->fib6_next))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200731 match = find_match(rt, oif, strict, &mpri, match, do_rr);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800732
David S. Millerf11e6652007-03-24 20:36:25 -0700733 return match;
734}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800735
David Ahern8d1c8022018-04-17 17:33:26 -0700736static struct fib6_info *rt6_select(struct net *net, struct fib6_node *fn,
Wei Wang8d1040e2017-10-06 12:06:08 -0700737 int oif, int strict)
David S. Millerf11e6652007-03-24 20:36:25 -0700738{
David Ahern8d1c8022018-04-17 17:33:26 -0700739 struct fib6_info *leaf = rcu_dereference(fn->leaf);
740 struct fib6_info *match, *rt0;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200741 bool do_rr = false;
Wei Wang17ecf592017-10-06 12:06:09 -0700742 int key_plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
David Ahern421842e2018-04-17 17:33:18 -0700744 if (!leaf || leaf == net->ipv6.fib6_null_entry)
745 return net->ipv6.fib6_null_entry;
Wei Wang8d1040e2017-10-06 12:06:08 -0700746
Wei Wang66f5d6c2017-10-06 12:06:10 -0700747 rt0 = rcu_dereference(fn->rr_ptr);
David S. Millerf11e6652007-03-24 20:36:25 -0700748 if (!rt0)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700749 rt0 = leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Wei Wang17ecf592017-10-06 12:06:09 -0700751 /* Double check to make sure fn is not an intermediate node
752 * and fn->leaf does not points to its child's leaf
753 * (This might happen if all routes under fn are deleted from
754 * the tree and fib6_repair_tree() is called on the node.)
755 */
David Ahern93c2fb22018-04-18 15:38:59 -0700756 key_plen = rt0->fib6_dst.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700757#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -0700758 if (rt0->fib6_src.plen)
759 key_plen = rt0->fib6_src.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700760#endif
761 if (fn->fn_bit != key_plen)
David Ahern421842e2018-04-17 17:33:18 -0700762 return net->ipv6.fib6_null_entry;
Wei Wang17ecf592017-10-06 12:06:09 -0700763
David Ahern93c2fb22018-04-18 15:38:59 -0700764 match = find_rr_leaf(fn, leaf, rt0, rt0->fib6_metric, oif, strict,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200765 &do_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200767 if (do_rr) {
David Ahern8fb11a92018-05-04 13:54:24 -0700768 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
David S. Millerf11e6652007-03-24 20:36:25 -0700769
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800770 /* no entries matched; do round-robin */
David Ahern93c2fb22018-04-18 15:38:59 -0700771 if (!next || next->fib6_metric != rt0->fib6_metric)
Wei Wang8d1040e2017-10-06 12:06:08 -0700772 next = leaf;
David S. Millerf11e6652007-03-24 20:36:25 -0700773
Wei Wang66f5d6c2017-10-06 12:06:10 -0700774 if (next != rt0) {
David Ahern93c2fb22018-04-18 15:38:59 -0700775 spin_lock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700776 /* make sure next is not being deleted from the tree */
David Ahern93c2fb22018-04-18 15:38:59 -0700777 if (next->fib6_node)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700778 rcu_assign_pointer(fn->rr_ptr, next);
David Ahern93c2fb22018-04-18 15:38:59 -0700779 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782
David Ahern421842e2018-04-17 17:33:18 -0700783 return match ? match : net->ipv6.fib6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
David Ahern8d1c8022018-04-17 17:33:26 -0700786static bool rt6_is_gw_or_nonexthop(const struct fib6_info *rt)
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700787{
David Ahern93c2fb22018-04-18 15:38:59 -0700788 return (rt->fib6_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700789}
790
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800791#ifdef CONFIG_IPV6_ROUTE_INFO
792int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000793 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800794{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900795 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800796 struct route_info *rinfo = (struct route_info *) opt;
797 struct in6_addr prefix_buf, *prefix;
798 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900799 unsigned long lifetime;
David Ahern8d1c8022018-04-17 17:33:26 -0700800 struct fib6_info *rt;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800801
802 if (len < sizeof(struct route_info)) {
803 return -EINVAL;
804 }
805
806 /* Sanity check for prefix_len and length */
807 if (rinfo->length > 3) {
808 return -EINVAL;
809 } else if (rinfo->prefix_len > 128) {
810 return -EINVAL;
811 } else if (rinfo->prefix_len > 64) {
812 if (rinfo->length < 2) {
813 return -EINVAL;
814 }
815 } else if (rinfo->prefix_len > 0) {
816 if (rinfo->length < 1) {
817 return -EINVAL;
818 }
819 }
820
821 pref = rinfo->route_pref;
822 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000823 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800824
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900825 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800826
827 if (rinfo->length == 3)
828 prefix = (struct in6_addr *)rinfo->prefix;
829 else {
830 /* this function is safe */
831 ipv6_addr_prefix(&prefix_buf,
832 (struct in6_addr *)rinfo->prefix,
833 rinfo->prefix_len);
834 prefix = &prefix_buf;
835 }
836
Duan Jiongf104a562013-11-08 09:56:53 +0800837 if (rinfo->prefix_len == 0)
David Ahernafb1d4b52018-04-17 17:33:11 -0700838 rt = rt6_get_dflt_router(net, gwaddr, dev);
Duan Jiongf104a562013-11-08 09:56:53 +0800839 else
840 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
David Ahern830218c2016-10-24 10:52:35 -0700841 gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800842
843 if (rt && !lifetime) {
David Ahernafb1d4b52018-04-17 17:33:11 -0700844 ip6_del_rt(net, rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800845 rt = NULL;
846 }
847
848 if (!rt && lifetime)
David Ahern830218c2016-10-24 10:52:35 -0700849 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
850 dev, pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800851 else if (rt)
David Ahern93c2fb22018-04-18 15:38:59 -0700852 rt->fib6_flags = RTF_ROUTEINFO |
853 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800854
855 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000856 if (!addrconf_finite_timeout(lifetime))
David Ahern14895682018-04-17 17:33:17 -0700857 fib6_clean_expires(rt);
Gao feng1716a962012-04-06 00:13:10 +0000858 else
David Ahern14895682018-04-17 17:33:17 -0700859 fib6_set_expires(rt, jiffies + HZ * lifetime);
Gao feng1716a962012-04-06 00:13:10 +0000860
David Ahern93531c62018-04-17 17:33:25 -0700861 fib6_info_release(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800862 }
863 return 0;
864}
865#endif
866
David Ahernae90d862018-04-17 17:33:12 -0700867/*
868 * Misc support functions
869 */
870
871/* called with rcu_lock held */
David Ahern8d1c8022018-04-17 17:33:26 -0700872static struct net_device *ip6_rt_get_dev_rcu(struct fib6_info *rt)
David Ahernae90d862018-04-17 17:33:12 -0700873{
David Ahern5e670d82018-04-17 17:33:14 -0700874 struct net_device *dev = rt->fib6_nh.nh_dev;
David Ahernae90d862018-04-17 17:33:12 -0700875
David Ahern93c2fb22018-04-18 15:38:59 -0700876 if (rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
David Ahernae90d862018-04-17 17:33:12 -0700877 /* for copies of local routes, dst->dev needs to be the
878 * device if it is a master device, the master device if
879 * device is enslaved, and the loopback as the default
880 */
881 if (netif_is_l3_slave(dev) &&
David Ahern93c2fb22018-04-18 15:38:59 -0700882 !rt6_need_strict(&rt->fib6_dst.addr))
David Ahernae90d862018-04-17 17:33:12 -0700883 dev = l3mdev_master_dev_rcu(dev);
884 else if (!netif_is_l3_master(dev))
885 dev = dev_net(dev)->loopback_dev;
886 /* last case is netif_is_l3_master(dev) is true in which
887 * case we want dev returned to be dev
888 */
889 }
890
891 return dev;
892}
893
David Ahern6edb3c92018-04-17 17:33:15 -0700894static const int fib6_prop[RTN_MAX + 1] = {
895 [RTN_UNSPEC] = 0,
896 [RTN_UNICAST] = 0,
897 [RTN_LOCAL] = 0,
898 [RTN_BROADCAST] = 0,
899 [RTN_ANYCAST] = 0,
900 [RTN_MULTICAST] = 0,
901 [RTN_BLACKHOLE] = -EINVAL,
902 [RTN_UNREACHABLE] = -EHOSTUNREACH,
903 [RTN_PROHIBIT] = -EACCES,
904 [RTN_THROW] = -EAGAIN,
905 [RTN_NAT] = -EINVAL,
906 [RTN_XRESOLVE] = -EINVAL,
907};
908
909static int ip6_rt_type_to_error(u8 fib6_type)
910{
911 return fib6_prop[fib6_type];
912}
913
David Ahern8d1c8022018-04-17 17:33:26 -0700914static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
David Ahern3b6761d2018-04-17 17:33:20 -0700915{
916 unsigned short flags = 0;
917
918 if (rt->dst_nocount)
919 flags |= DST_NOCOUNT;
920 if (rt->dst_nopolicy)
921 flags |= DST_NOPOLICY;
922 if (rt->dst_host)
923 flags |= DST_HOST;
924
925 return flags;
926}
927
David Ahern8d1c8022018-04-17 17:33:26 -0700928static void ip6_rt_init_dst_reject(struct rt6_info *rt, struct fib6_info *ort)
David Ahern6edb3c92018-04-17 17:33:15 -0700929{
930 rt->dst.error = ip6_rt_type_to_error(ort->fib6_type);
931
932 switch (ort->fib6_type) {
933 case RTN_BLACKHOLE:
934 rt->dst.output = dst_discard_out;
935 rt->dst.input = dst_discard;
936 break;
937 case RTN_PROHIBIT:
938 rt->dst.output = ip6_pkt_prohibit_out;
939 rt->dst.input = ip6_pkt_prohibit;
940 break;
941 case RTN_THROW:
942 case RTN_UNREACHABLE:
943 default:
944 rt->dst.output = ip6_pkt_discard_out;
945 rt->dst.input = ip6_pkt_discard;
946 break;
947 }
948}
949
David Ahern8d1c8022018-04-17 17:33:26 -0700950static void ip6_rt_init_dst(struct rt6_info *rt, struct fib6_info *ort)
David Ahern6edb3c92018-04-17 17:33:15 -0700951{
David Ahern93c2fb22018-04-18 15:38:59 -0700952 if (ort->fib6_flags & RTF_REJECT) {
David Ahern6edb3c92018-04-17 17:33:15 -0700953 ip6_rt_init_dst_reject(rt, ort);
954 return;
955 }
956
957 rt->dst.error = 0;
958 rt->dst.output = ip6_output;
959
Hangbin Liud23c4b62018-08-23 11:31:37 +0800960 if (ort->fib6_type == RTN_LOCAL || ort->fib6_type == RTN_ANYCAST) {
David Ahern6edb3c92018-04-17 17:33:15 -0700961 rt->dst.input = ip6_input;
David Ahern93c2fb22018-04-18 15:38:59 -0700962 } else if (ipv6_addr_type(&ort->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
David Ahern6edb3c92018-04-17 17:33:15 -0700963 rt->dst.input = ip6_mc_input;
964 } else {
965 rt->dst.input = ip6_forward;
966 }
967
968 if (ort->fib6_nh.nh_lwtstate) {
969 rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.nh_lwtstate);
970 lwtunnel_set_redirect(&rt->dst);
971 }
972
973 rt->dst.lastuse = jiffies;
974}
975
Wei Wange873e4b2018-07-21 20:56:32 -0700976/* Caller must already hold reference to @from */
David Ahern8d1c8022018-04-17 17:33:26 -0700977static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
David Ahernae90d862018-04-17 17:33:12 -0700978{
David Ahernae90d862018-04-17 17:33:12 -0700979 rt->rt6i_flags &= ~RTF_EXPIRES;
David Aherna68886a2018-04-20 15:38:02 -0700980 rcu_assign_pointer(rt->from, from);
David Aherne1255ed2018-10-04 20:07:53 -0700981 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
David Ahernae90d862018-04-17 17:33:12 -0700982}
983
Wei Wange873e4b2018-07-21 20:56:32 -0700984/* Caller must already hold reference to @ort */
David Ahern8d1c8022018-04-17 17:33:26 -0700985static void ip6_rt_copy_init(struct rt6_info *rt, struct fib6_info *ort)
David Ahernae90d862018-04-17 17:33:12 -0700986{
David Aherndcd1f572018-04-18 15:39:05 -0700987 struct net_device *dev = fib6_info_nh_dev(ort);
988
David Ahern6edb3c92018-04-17 17:33:15 -0700989 ip6_rt_init_dst(rt, ort);
990
David Ahern93c2fb22018-04-18 15:38:59 -0700991 rt->rt6i_dst = ort->fib6_dst;
David Aherndcd1f572018-04-18 15:39:05 -0700992 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
David Ahern5e670d82018-04-17 17:33:14 -0700993 rt->rt6i_gateway = ort->fib6_nh.nh_gw;
David Ahern93c2fb22018-04-18 15:38:59 -0700994 rt->rt6i_flags = ort->fib6_flags;
David Ahernae90d862018-04-17 17:33:12 -0700995 rt6_set_from(rt, ort);
David Ahernae90d862018-04-17 17:33:12 -0700996#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -0700997 rt->rt6i_src = ort->fib6_src;
David Ahernae90d862018-04-17 17:33:12 -0700998#endif
David Ahernae90d862018-04-17 17:33:12 -0700999}
1000
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001001static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1002 struct in6_addr *saddr)
1003{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001004 struct fib6_node *pn, *sn;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001005 while (1) {
1006 if (fn->fn_flags & RTN_TL_ROOT)
1007 return NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001008 pn = rcu_dereference(fn->parent);
1009 sn = FIB6_SUBTREE(pn);
1010 if (sn && sn != fn)
David Ahern64547432018-05-09 20:34:19 -07001011 fn = fib6_node_lookup(sn, NULL, saddr);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001012 else
1013 fn = pn;
1014 if (fn->fn_flags & RTN_RTINFO)
1015 return fn;
1016 }
1017}
Thomas Grafc71099a2006-08-04 23:20:06 -07001018
Wei Wangd3843fe2017-10-06 12:06:06 -07001019static bool ip6_hold_safe(struct net *net, struct rt6_info **prt,
1020 bool null_fallback)
1021{
1022 struct rt6_info *rt = *prt;
1023
1024 if (dst_hold_safe(&rt->dst))
1025 return true;
1026 if (null_fallback) {
1027 rt = net->ipv6.ip6_null_entry;
1028 dst_hold(&rt->dst);
1029 } else {
1030 rt = NULL;
1031 }
1032 *prt = rt;
1033 return false;
1034}
1035
David Aherndec9b0e2018-04-17 17:33:19 -07001036/* called with rcu_lock held */
David Ahern8d1c8022018-04-17 17:33:26 -07001037static struct rt6_info *ip6_create_rt_rcu(struct fib6_info *rt)
David Aherndec9b0e2018-04-17 17:33:19 -07001038{
David Ahern3b6761d2018-04-17 17:33:20 -07001039 unsigned short flags = fib6_info_dst_flags(rt);
David Aherndec9b0e2018-04-17 17:33:19 -07001040 struct net_device *dev = rt->fib6_nh.nh_dev;
1041 struct rt6_info *nrt;
1042
Wei Wange873e4b2018-07-21 20:56:32 -07001043 if (!fib6_info_hold_safe(rt))
1044 return NULL;
1045
David Ahern93531c62018-04-17 17:33:25 -07001046 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
David Aherndec9b0e2018-04-17 17:33:19 -07001047 if (nrt)
1048 ip6_rt_copy_init(nrt, rt);
Wei Wange873e4b2018-07-21 20:56:32 -07001049 else
1050 fib6_info_release(rt);
David Aherndec9b0e2018-04-17 17:33:19 -07001051
1052 return nrt;
1053}
1054
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001055static struct rt6_info *ip6_pol_route_lookup(struct net *net,
1056 struct fib6_table *table,
David Ahernb75cc8f2018-03-02 08:32:17 -08001057 struct flowi6 *fl6,
1058 const struct sk_buff *skb,
1059 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
David Ahern8d1c8022018-04-17 17:33:26 -07001061 struct fib6_info *f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 struct fib6_node *fn;
David Ahern23fb93a2018-04-17 17:33:23 -07001063 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
David Ahernb6cdbc82018-03-29 17:44:57 -07001065 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1066 flags &= ~RT6_LOOKUP_F_IFACE;
1067
Wei Wang66f5d6c2017-10-06 12:06:10 -07001068 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07001069 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -07001070restart:
David Ahern23fb93a2018-04-17 17:33:23 -07001071 f6i = rcu_dereference(fn->leaf);
1072 if (!f6i) {
1073 f6i = net->ipv6.fib6_null_entry;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001074 } else {
David Ahern23fb93a2018-04-17 17:33:23 -07001075 f6i = rt6_device_match(net, f6i, &fl6->saddr,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001076 fl6->flowi6_oif, flags);
David Ahern93c2fb22018-04-18 15:38:59 -07001077 if (f6i->fib6_nsiblings && fl6->flowi6_oif == 0)
David Ahern3b290a32018-05-09 20:34:20 -07001078 f6i = fib6_multipath_select(net, f6i, fl6,
1079 fl6->flowi6_oif, skb,
1080 flags);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001081 }
David Ahern23fb93a2018-04-17 17:33:23 -07001082 if (f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001083 fn = fib6_backtrack(fn, &fl6->saddr);
1084 if (fn)
1085 goto restart;
1086 }
Wei Wang2b760fc2017-10-06 12:06:03 -07001087
David Ahernd4bea422018-05-09 20:34:24 -07001088 trace_fib6_table_lookup(net, f6i, table, fl6);
1089
David S. Miller4c9483b2011-03-12 16:22:43 -05001090 /* Search through exception table */
David Ahern23fb93a2018-04-17 17:33:23 -07001091 rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
1092 if (rt) {
David Aherndec9b0e2018-04-17 17:33:19 -07001093 if (ip6_hold_safe(net, &rt, true))
1094 dst_use_noref(&rt->dst, jiffies);
David Ahern23fb93a2018-04-17 17:33:23 -07001095 } else if (f6i == net->ipv6.fib6_null_entry) {
David Aherndec9b0e2018-04-17 17:33:19 -07001096 rt = net->ipv6.ip6_null_entry;
1097 dst_hold(&rt->dst);
David Ahern23fb93a2018-04-17 17:33:23 -07001098 } else {
1099 rt = ip6_create_rt_rcu(f6i);
1100 if (!rt) {
1101 rt = net->ipv6.ip6_null_entry;
1102 dst_hold(&rt->dst);
1103 }
David Aherndec9b0e2018-04-17 17:33:19 -07001104 }
Wei Wangd3843fe2017-10-06 12:06:06 -07001105
Wei Wang66f5d6c2017-10-06 12:06:10 -07001106 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08001107
Thomas Grafc71099a2006-08-04 23:20:06 -07001108 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001109}
1110
Ian Morris67ba4152014-08-24 21:53:10 +01001111struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08001112 const struct sk_buff *skb, int flags)
Florian Westphalea6e5742011-09-05 16:05:44 +02001113{
David Ahernb75cc8f2018-03-02 08:32:17 -08001114 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
Florian Westphalea6e5742011-09-05 16:05:44 +02001115}
1116EXPORT_SYMBOL_GPL(ip6_route_lookup);
1117
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001118struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
David Ahernb75cc8f2018-03-02 08:32:17 -08001119 const struct in6_addr *saddr, int oif,
1120 const struct sk_buff *skb, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -07001121{
David S. Miller4c9483b2011-03-12 16:22:43 -05001122 struct flowi6 fl6 = {
1123 .flowi6_oif = oif,
1124 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001125 };
1126 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001127 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07001128
Thomas Grafadaa70b2006-10-13 15:01:03 -07001129 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -05001130 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -07001131 flags |= RT6_LOOKUP_F_HAS_SADDR;
1132 }
1133
David Ahernb75cc8f2018-03-02 08:32:17 -08001134 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -07001135 if (dst->error == 0)
1136 return (struct rt6_info *) dst;
1137
1138 dst_release(dst);
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return NULL;
1141}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001142EXPORT_SYMBOL(rt6_lookup);
1143
Thomas Grafc71099a2006-08-04 23:20:06 -07001144/* ip6_ins_rt is called with FREE table->tb6_lock.
Wei Wang1cfb71e2017-06-17 10:42:33 -07001145 * It takes new route entry, the addition fails by any reason the
1146 * route is released.
1147 * Caller must hold dst before calling it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 */
1149
David Ahern8d1c8022018-04-17 17:33:26 -07001150static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
David Ahern333c4302017-05-21 10:12:04 -06001151 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001154 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
David Ahern93c2fb22018-04-18 15:38:59 -07001156 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001157 spin_lock_bh(&table->tb6_lock);
David Ahernd4ead6b2018-04-17 17:33:16 -07001158 err = fib6_add(&table->tb6_root, rt, info, extack);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001159 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 return err;
1162}
1163
David Ahern8d1c8022018-04-17 17:33:26 -07001164int ip6_ins_rt(struct net *net, struct fib6_info *rt)
Thomas Graf40e22e82006-08-22 00:00:45 -07001165{
David Ahernafb1d4b52018-04-17 17:33:11 -07001166 struct nl_info info = { .nl_net = net, };
Florian Westphale715b6d2015-01-05 23:57:44 +01001167
David Ahernd4ead6b2018-04-17 17:33:16 -07001168 return __ip6_ins_rt(rt, &info, NULL);
Thomas Graf40e22e82006-08-22 00:00:45 -07001169}
1170
David Ahern8d1c8022018-04-17 17:33:26 -07001171static struct rt6_info *ip6_rt_cache_alloc(struct fib6_info *ort,
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001172 const struct in6_addr *daddr,
1173 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
David Ahern4832c302017-08-17 12:17:20 -07001175 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 struct rt6_info *rt;
1177
1178 /*
1179 * Clone the route.
1180 */
1181
Wei Wange873e4b2018-07-21 20:56:32 -07001182 if (!fib6_info_hold_safe(ort))
1183 return NULL;
1184
David Ahern4832c302017-08-17 12:17:20 -07001185 dev = ip6_rt_get_dev_rcu(ort);
David Ahern93531c62018-04-17 17:33:25 -07001186 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
Wei Wange873e4b2018-07-21 20:56:32 -07001187 if (!rt) {
1188 fib6_info_release(ort);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001189 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001190 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001191
1192 ip6_rt_copy_init(rt, ort);
1193 rt->rt6i_flags |= RTF_CACHE;
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001194 rt->dst.flags |= DST_HOST;
1195 rt->rt6i_dst.addr = *daddr;
1196 rt->rt6i_dst.plen = 128;
1197
1198 if (!rt6_is_gw_or_nonexthop(ort)) {
David Ahern93c2fb22018-04-18 15:38:59 -07001199 if (ort->fib6_dst.plen != 128 &&
1200 ipv6_addr_equal(&ort->fib6_dst.addr, daddr))
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001201 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001203 if (rt->rt6i_src.plen && saddr) {
1204 rt->rt6i_src.addr = *saddr;
1205 rt->rt6i_src.plen = 128;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001206 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001207#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001210 return rt;
1211}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
David Ahern8d1c8022018-04-17 17:33:26 -07001213static struct rt6_info *ip6_rt_pcpu_alloc(struct fib6_info *rt)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001214{
David Ahern3b6761d2018-04-17 17:33:20 -07001215 unsigned short flags = fib6_info_dst_flags(rt);
David Ahern4832c302017-08-17 12:17:20 -07001216 struct net_device *dev;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001217 struct rt6_info *pcpu_rt;
1218
Wei Wange873e4b2018-07-21 20:56:32 -07001219 if (!fib6_info_hold_safe(rt))
1220 return NULL;
1221
David Ahern4832c302017-08-17 12:17:20 -07001222 rcu_read_lock();
1223 dev = ip6_rt_get_dev_rcu(rt);
David Ahern93531c62018-04-17 17:33:25 -07001224 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
David Ahern4832c302017-08-17 12:17:20 -07001225 rcu_read_unlock();
Wei Wange873e4b2018-07-21 20:56:32 -07001226 if (!pcpu_rt) {
1227 fib6_info_release(rt);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001228 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001229 }
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001230 ip6_rt_copy_init(pcpu_rt, rt);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001231 pcpu_rt->rt6i_flags |= RTF_PCPU;
1232 return pcpu_rt;
1233}
1234
Wei Wang66f5d6c2017-10-06 12:06:10 -07001235/* It should be called with rcu_read_lock() acquired */
David Ahern8d1c8022018-04-17 17:33:26 -07001236static struct rt6_info *rt6_get_pcpu_route(struct fib6_info *rt)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001237{
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001238 struct rt6_info *pcpu_rt, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001239
1240 p = this_cpu_ptr(rt->rt6i_pcpu);
1241 pcpu_rt = *p;
1242
David Ahernd4ead6b2018-04-17 17:33:16 -07001243 if (pcpu_rt)
1244 ip6_hold_safe(NULL, &pcpu_rt, false);
Wei Wangd3843fe2017-10-06 12:06:06 -07001245
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001246 return pcpu_rt;
1247}
1248
David Ahernafb1d4b52018-04-17 17:33:11 -07001249static struct rt6_info *rt6_make_pcpu_route(struct net *net,
David Ahern8d1c8022018-04-17 17:33:26 -07001250 struct fib6_info *rt)
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001251{
1252 struct rt6_info *pcpu_rt, *prev, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001253
1254 pcpu_rt = ip6_rt_pcpu_alloc(rt);
1255 if (!pcpu_rt) {
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001256 dst_hold(&net->ipv6.ip6_null_entry->dst);
1257 return net->ipv6.ip6_null_entry;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001258 }
1259
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001260 dst_hold(&pcpu_rt->dst);
Wei Wanga94b9362017-10-06 12:06:04 -07001261 p = this_cpu_ptr(rt->rt6i_pcpu);
1262 prev = cmpxchg(p, NULL, pcpu_rt);
Eric Dumazet951f7882017-10-08 21:07:18 -07001263 BUG_ON(prev);
Wei Wanga94b9362017-10-06 12:06:04 -07001264
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001265 return pcpu_rt;
1266}
1267
Wei Wang35732d02017-10-06 12:05:57 -07001268/* exception hash table implementation
1269 */
1270static DEFINE_SPINLOCK(rt6_exception_lock);
1271
1272/* Remove rt6_ex from hash table and free the memory
1273 * Caller must hold rt6_exception_lock
1274 */
1275static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1276 struct rt6_exception *rt6_ex)
1277{
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001278 struct net *net;
Wei Wang81eb8442017-10-06 12:06:11 -07001279
Wei Wang35732d02017-10-06 12:05:57 -07001280 if (!bucket || !rt6_ex)
1281 return;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001282
1283 net = dev_net(rt6_ex->rt6i->dst.dev);
Wei Wang35732d02017-10-06 12:05:57 -07001284 hlist_del_rcu(&rt6_ex->hlist);
David Ahern77634cc2018-04-17 17:33:27 -07001285 dst_release(&rt6_ex->rt6i->dst);
Wei Wang35732d02017-10-06 12:05:57 -07001286 kfree_rcu(rt6_ex, rcu);
1287 WARN_ON_ONCE(!bucket->depth);
1288 bucket->depth--;
Wei Wang81eb8442017-10-06 12:06:11 -07001289 net->ipv6.rt6_stats->fib_rt_cache--;
Wei Wang35732d02017-10-06 12:05:57 -07001290}
1291
1292/* Remove oldest rt6_ex in bucket and free the memory
1293 * Caller must hold rt6_exception_lock
1294 */
1295static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1296{
1297 struct rt6_exception *rt6_ex, *oldest = NULL;
1298
1299 if (!bucket)
1300 return;
1301
1302 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1303 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1304 oldest = rt6_ex;
1305 }
1306 rt6_remove_exception(bucket, oldest);
1307}
1308
1309static u32 rt6_exception_hash(const struct in6_addr *dst,
1310 const struct in6_addr *src)
1311{
1312 static u32 seed __read_mostly;
1313 u32 val;
1314
1315 net_get_random_once(&seed, sizeof(seed));
1316 val = jhash(dst, sizeof(*dst), seed);
1317
1318#ifdef CONFIG_IPV6_SUBTREES
1319 if (src)
1320 val = jhash(src, sizeof(*src), val);
1321#endif
1322 return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1323}
1324
1325/* Helper function to find the cached rt in the hash table
1326 * and update bucket pointer to point to the bucket for this
1327 * (daddr, saddr) pair
1328 * Caller must hold rt6_exception_lock
1329 */
1330static struct rt6_exception *
1331__rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1332 const struct in6_addr *daddr,
1333 const struct in6_addr *saddr)
1334{
1335 struct rt6_exception *rt6_ex;
1336 u32 hval;
1337
1338 if (!(*bucket) || !daddr)
1339 return NULL;
1340
1341 hval = rt6_exception_hash(daddr, saddr);
1342 *bucket += hval;
1343
1344 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1345 struct rt6_info *rt6 = rt6_ex->rt6i;
1346 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1347
1348#ifdef CONFIG_IPV6_SUBTREES
1349 if (matched && saddr)
1350 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1351#endif
1352 if (matched)
1353 return rt6_ex;
1354 }
1355 return NULL;
1356}
1357
1358/* Helper function to find the cached rt in the hash table
1359 * and update bucket pointer to point to the bucket for this
1360 * (daddr, saddr) pair
1361 * Caller must hold rcu_read_lock()
1362 */
1363static struct rt6_exception *
1364__rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1365 const struct in6_addr *daddr,
1366 const struct in6_addr *saddr)
1367{
1368 struct rt6_exception *rt6_ex;
1369 u32 hval;
1370
1371 WARN_ON_ONCE(!rcu_read_lock_held());
1372
1373 if (!(*bucket) || !daddr)
1374 return NULL;
1375
1376 hval = rt6_exception_hash(daddr, saddr);
1377 *bucket += hval;
1378
1379 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1380 struct rt6_info *rt6 = rt6_ex->rt6i;
1381 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1382
1383#ifdef CONFIG_IPV6_SUBTREES
1384 if (matched && saddr)
1385 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1386#endif
1387 if (matched)
1388 return rt6_ex;
1389 }
1390 return NULL;
1391}
1392
David Ahern8d1c8022018-04-17 17:33:26 -07001393static unsigned int fib6_mtu(const struct fib6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001394{
David Ahernd4ead6b2018-04-17 17:33:16 -07001395 unsigned int mtu;
1396
David Aherndcd1f572018-04-18 15:39:05 -07001397 if (rt->fib6_pmtu) {
1398 mtu = rt->fib6_pmtu;
1399 } else {
1400 struct net_device *dev = fib6_info_nh_dev(rt);
1401 struct inet6_dev *idev;
1402
1403 rcu_read_lock();
1404 idev = __in6_dev_get(dev);
1405 mtu = idev->cnf.mtu6;
1406 rcu_read_unlock();
1407 }
1408
David Ahernd4ead6b2018-04-17 17:33:16 -07001409 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1410
1411 return mtu - lwtunnel_headroom(rt->fib6_nh.nh_lwtstate, mtu);
1412}
1413
Wei Wang35732d02017-10-06 12:05:57 -07001414static int rt6_insert_exception(struct rt6_info *nrt,
David Ahern8d1c8022018-04-17 17:33:26 -07001415 struct fib6_info *ort)
Wei Wang35732d02017-10-06 12:05:57 -07001416{
David Ahern5e670d82018-04-17 17:33:14 -07001417 struct net *net = dev_net(nrt->dst.dev);
Wei Wang35732d02017-10-06 12:05:57 -07001418 struct rt6_exception_bucket *bucket;
1419 struct in6_addr *src_key = NULL;
1420 struct rt6_exception *rt6_ex;
1421 int err = 0;
1422
Wei Wang35732d02017-10-06 12:05:57 -07001423 spin_lock_bh(&rt6_exception_lock);
1424
1425 if (ort->exception_bucket_flushed) {
1426 err = -EINVAL;
1427 goto out;
1428 }
1429
1430 bucket = rcu_dereference_protected(ort->rt6i_exception_bucket,
1431 lockdep_is_held(&rt6_exception_lock));
1432 if (!bucket) {
1433 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1434 GFP_ATOMIC);
1435 if (!bucket) {
1436 err = -ENOMEM;
1437 goto out;
1438 }
1439 rcu_assign_pointer(ort->rt6i_exception_bucket, bucket);
1440 }
1441
1442#ifdef CONFIG_IPV6_SUBTREES
1443 /* rt6i_src.plen != 0 indicates ort is in subtree
1444 * and exception table is indexed by a hash of
1445 * both rt6i_dst and rt6i_src.
1446 * Otherwise, the exception table is indexed by
1447 * a hash of only rt6i_dst.
1448 */
David Ahern93c2fb22018-04-18 15:38:59 -07001449 if (ort->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001450 src_key = &nrt->rt6i_src.addr;
1451#endif
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001452 /* rt6_mtu_change() might lower mtu on ort.
1453 * Only insert this exception route if its mtu
1454 * is less than ort's mtu value.
1455 */
David Ahernd4ead6b2018-04-17 17:33:16 -07001456 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(ort)) {
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001457 err = -EINVAL;
1458 goto out;
1459 }
Wei Wang60006a42017-10-06 12:05:58 -07001460
Wei Wang35732d02017-10-06 12:05:57 -07001461 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1462 src_key);
1463 if (rt6_ex)
1464 rt6_remove_exception(bucket, rt6_ex);
1465
1466 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1467 if (!rt6_ex) {
1468 err = -ENOMEM;
1469 goto out;
1470 }
1471 rt6_ex->rt6i = nrt;
1472 rt6_ex->stamp = jiffies;
Wei Wang35732d02017-10-06 12:05:57 -07001473 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1474 bucket->depth++;
Wei Wang81eb8442017-10-06 12:06:11 -07001475 net->ipv6.rt6_stats->fib_rt_cache++;
Wei Wang35732d02017-10-06 12:05:57 -07001476
1477 if (bucket->depth > FIB6_MAX_DEPTH)
1478 rt6_exception_remove_oldest(bucket);
1479
1480out:
1481 spin_unlock_bh(&rt6_exception_lock);
1482
1483 /* Update fn->fn_sernum to invalidate all cached dst */
Paolo Abenib886d5f2017-10-19 16:07:10 +02001484 if (!err) {
David Ahern93c2fb22018-04-18 15:38:59 -07001485 spin_lock_bh(&ort->fib6_table->tb6_lock);
David Ahern7aef6852018-04-17 17:33:10 -07001486 fib6_update_sernum(net, ort);
David Ahern93c2fb22018-04-18 15:38:59 -07001487 spin_unlock_bh(&ort->fib6_table->tb6_lock);
Paolo Abenib886d5f2017-10-19 16:07:10 +02001488 fib6_force_start_gc(net);
1489 }
Wei Wang35732d02017-10-06 12:05:57 -07001490
1491 return err;
1492}
1493
David Ahern8d1c8022018-04-17 17:33:26 -07001494void rt6_flush_exceptions(struct fib6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001495{
1496 struct rt6_exception_bucket *bucket;
1497 struct rt6_exception *rt6_ex;
1498 struct hlist_node *tmp;
1499 int i;
1500
1501 spin_lock_bh(&rt6_exception_lock);
1502 /* Prevent rt6_insert_exception() to recreate the bucket list */
1503 rt->exception_bucket_flushed = 1;
1504
1505 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1506 lockdep_is_held(&rt6_exception_lock));
1507 if (!bucket)
1508 goto out;
1509
1510 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1511 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
1512 rt6_remove_exception(bucket, rt6_ex);
1513 WARN_ON_ONCE(bucket->depth);
1514 bucket++;
1515 }
1516
1517out:
1518 spin_unlock_bh(&rt6_exception_lock);
1519}
1520
1521/* Find cached rt in the hash table inside passed in rt
1522 * Caller has to hold rcu_read_lock()
1523 */
David Ahern8d1c8022018-04-17 17:33:26 -07001524static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt,
Wei Wang35732d02017-10-06 12:05:57 -07001525 struct in6_addr *daddr,
1526 struct in6_addr *saddr)
1527{
1528 struct rt6_exception_bucket *bucket;
1529 struct in6_addr *src_key = NULL;
1530 struct rt6_exception *rt6_ex;
1531 struct rt6_info *res = NULL;
1532
1533 bucket = rcu_dereference(rt->rt6i_exception_bucket);
1534
1535#ifdef CONFIG_IPV6_SUBTREES
1536 /* rt6i_src.plen != 0 indicates rt is in subtree
1537 * and exception table is indexed by a hash of
1538 * both rt6i_dst and rt6i_src.
1539 * Otherwise, the exception table is indexed by
1540 * a hash of only rt6i_dst.
1541 */
David Ahern93c2fb22018-04-18 15:38:59 -07001542 if (rt->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001543 src_key = saddr;
1544#endif
1545 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1546
1547 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
1548 res = rt6_ex->rt6i;
1549
1550 return res;
1551}
1552
1553/* Remove the passed in cached rt from the hash table that contains it */
David Ahern23fb93a2018-04-17 17:33:23 -07001554static int rt6_remove_exception_rt(struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001555{
Wei Wang35732d02017-10-06 12:05:57 -07001556 struct rt6_exception_bucket *bucket;
1557 struct in6_addr *src_key = NULL;
1558 struct rt6_exception *rt6_ex;
David Ahern8a14e462018-04-23 11:32:07 -07001559 struct fib6_info *from;
Wei Wang35732d02017-10-06 12:05:57 -07001560 int err;
1561
Eric Dumazet091311d2018-04-24 09:22:49 -07001562 from = rcu_dereference(rt->from);
Wei Wang35732d02017-10-06 12:05:57 -07001563 if (!from ||
Colin Ian King442d7132017-10-10 19:10:30 +01001564 !(rt->rt6i_flags & RTF_CACHE))
Wei Wang35732d02017-10-06 12:05:57 -07001565 return -EINVAL;
1566
1567 if (!rcu_access_pointer(from->rt6i_exception_bucket))
1568 return -ENOENT;
1569
1570 spin_lock_bh(&rt6_exception_lock);
1571 bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
1572 lockdep_is_held(&rt6_exception_lock));
1573#ifdef CONFIG_IPV6_SUBTREES
1574 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1575 * and exception table is indexed by a hash of
1576 * both rt6i_dst and rt6i_src.
1577 * Otherwise, the exception table is indexed by
1578 * a hash of only rt6i_dst.
1579 */
David Ahern93c2fb22018-04-18 15:38:59 -07001580 if (from->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001581 src_key = &rt->rt6i_src.addr;
1582#endif
1583 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1584 &rt->rt6i_dst.addr,
1585 src_key);
1586 if (rt6_ex) {
1587 rt6_remove_exception(bucket, rt6_ex);
1588 err = 0;
1589 } else {
1590 err = -ENOENT;
1591 }
1592
1593 spin_unlock_bh(&rt6_exception_lock);
1594 return err;
1595}
1596
1597/* Find rt6_ex which contains the passed in rt cache and
1598 * refresh its stamp
1599 */
1600static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1601{
Wei Wang35732d02017-10-06 12:05:57 -07001602 struct rt6_exception_bucket *bucket;
David Ahern8d1c8022018-04-17 17:33:26 -07001603 struct fib6_info *from = rt->from;
Wei Wang35732d02017-10-06 12:05:57 -07001604 struct in6_addr *src_key = NULL;
1605 struct rt6_exception *rt6_ex;
1606
1607 if (!from ||
Colin Ian King442d7132017-10-10 19:10:30 +01001608 !(rt->rt6i_flags & RTF_CACHE))
Wei Wang35732d02017-10-06 12:05:57 -07001609 return;
1610
1611 rcu_read_lock();
1612 bucket = rcu_dereference(from->rt6i_exception_bucket);
1613
1614#ifdef CONFIG_IPV6_SUBTREES
1615 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1616 * and exception table is indexed by a hash of
1617 * both rt6i_dst and rt6i_src.
1618 * Otherwise, the exception table is indexed by
1619 * a hash of only rt6i_dst.
1620 */
David Ahern93c2fb22018-04-18 15:38:59 -07001621 if (from->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001622 src_key = &rt->rt6i_src.addr;
1623#endif
1624 rt6_ex = __rt6_find_exception_rcu(&bucket,
1625 &rt->rt6i_dst.addr,
1626 src_key);
1627 if (rt6_ex)
1628 rt6_ex->stamp = jiffies;
1629
1630 rcu_read_unlock();
1631}
1632
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001633static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1634 struct rt6_info *rt, int mtu)
1635{
1636 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1637 * lowest MTU in the path: always allow updating the route PMTU to
1638 * reflect PMTU decreases.
1639 *
1640 * If the new MTU is higher, and the route PMTU is equal to the local
1641 * MTU, this means the old MTU is the lowest in the path, so allow
1642 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1643 * handle this.
1644 */
1645
1646 if (dst_mtu(&rt->dst) >= mtu)
1647 return true;
1648
1649 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1650 return true;
1651
1652 return false;
1653}
1654
1655static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
David Ahern8d1c8022018-04-17 17:33:26 -07001656 struct fib6_info *rt, int mtu)
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001657{
1658 struct rt6_exception_bucket *bucket;
1659 struct rt6_exception *rt6_ex;
1660 int i;
1661
1662 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1663 lockdep_is_held(&rt6_exception_lock));
1664
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001665 if (!bucket)
1666 return;
1667
1668 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1669 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1670 struct rt6_info *entry = rt6_ex->rt6i;
1671
1672 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
David Ahernd4ead6b2018-04-17 17:33:16 -07001673 * route), the metrics of its rt->from have already
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001674 * been updated.
1675 */
David Ahernd4ead6b2018-04-17 17:33:16 -07001676 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001677 rt6_mtu_change_route_allowed(idev, entry, mtu))
David Ahernd4ead6b2018-04-17 17:33:16 -07001678 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001679 }
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001680 bucket++;
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001681 }
1682}
1683
Wei Wangb16cb452017-10-06 12:06:00 -07001684#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
1685
David Ahern8d1c8022018-04-17 17:33:26 -07001686static void rt6_exceptions_clean_tohost(struct fib6_info *rt,
Wei Wangb16cb452017-10-06 12:06:00 -07001687 struct in6_addr *gateway)
1688{
1689 struct rt6_exception_bucket *bucket;
1690 struct rt6_exception *rt6_ex;
1691 struct hlist_node *tmp;
1692 int i;
1693
1694 if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1695 return;
1696
1697 spin_lock_bh(&rt6_exception_lock);
1698 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1699 lockdep_is_held(&rt6_exception_lock));
1700
1701 if (bucket) {
1702 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1703 hlist_for_each_entry_safe(rt6_ex, tmp,
1704 &bucket->chain, hlist) {
1705 struct rt6_info *entry = rt6_ex->rt6i;
1706
1707 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1708 RTF_CACHE_GATEWAY &&
1709 ipv6_addr_equal(gateway,
1710 &entry->rt6i_gateway)) {
1711 rt6_remove_exception(bucket, rt6_ex);
1712 }
1713 }
1714 bucket++;
1715 }
1716 }
1717
1718 spin_unlock_bh(&rt6_exception_lock);
1719}
1720
Wei Wangc757faa2017-10-06 12:06:01 -07001721static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1722 struct rt6_exception *rt6_ex,
1723 struct fib6_gc_args *gc_args,
1724 unsigned long now)
1725{
1726 struct rt6_info *rt = rt6_ex->rt6i;
1727
Paolo Abeni1859bac2017-10-19 16:07:11 +02001728 /* we are pruning and obsoleting aged-out and non gateway exceptions
1729 * even if others have still references to them, so that on next
1730 * dst_check() such references can be dropped.
1731 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
1732 * expired, independently from their aging, as per RFC 8201 section 4
1733 */
Wei Wang31afeb42018-01-26 11:40:17 -08001734 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
1735 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1736 RT6_TRACE("aging clone %p\n", rt);
1737 rt6_remove_exception(bucket, rt6_ex);
1738 return;
1739 }
1740 } else if (time_after(jiffies, rt->dst.expires)) {
1741 RT6_TRACE("purging expired route %p\n", rt);
Wei Wangc757faa2017-10-06 12:06:01 -07001742 rt6_remove_exception(bucket, rt6_ex);
1743 return;
Wei Wang31afeb42018-01-26 11:40:17 -08001744 }
1745
1746 if (rt->rt6i_flags & RTF_GATEWAY) {
Wei Wangc757faa2017-10-06 12:06:01 -07001747 struct neighbour *neigh;
1748 __u8 neigh_flags = 0;
1749
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001750 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
1751 if (neigh)
Wei Wangc757faa2017-10-06 12:06:01 -07001752 neigh_flags = neigh->flags;
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001753
Wei Wangc757faa2017-10-06 12:06:01 -07001754 if (!(neigh_flags & NTF_ROUTER)) {
1755 RT6_TRACE("purging route %p via non-router but gateway\n",
1756 rt);
1757 rt6_remove_exception(bucket, rt6_ex);
1758 return;
1759 }
1760 }
Wei Wang31afeb42018-01-26 11:40:17 -08001761
Wei Wangc757faa2017-10-06 12:06:01 -07001762 gc_args->more++;
1763}
1764
David Ahern8d1c8022018-04-17 17:33:26 -07001765void rt6_age_exceptions(struct fib6_info *rt,
Wei Wangc757faa2017-10-06 12:06:01 -07001766 struct fib6_gc_args *gc_args,
1767 unsigned long now)
1768{
1769 struct rt6_exception_bucket *bucket;
1770 struct rt6_exception *rt6_ex;
1771 struct hlist_node *tmp;
1772 int i;
1773
1774 if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1775 return;
1776
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001777 rcu_read_lock_bh();
1778 spin_lock(&rt6_exception_lock);
Wei Wangc757faa2017-10-06 12:06:01 -07001779 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1780 lockdep_is_held(&rt6_exception_lock));
1781
1782 if (bucket) {
1783 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1784 hlist_for_each_entry_safe(rt6_ex, tmp,
1785 &bucket->chain, hlist) {
1786 rt6_age_examine_exception(bucket, rt6_ex,
1787 gc_args, now);
1788 }
1789 bucket++;
1790 }
1791 }
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001792 spin_unlock(&rt6_exception_lock);
1793 rcu_read_unlock_bh();
Wei Wangc757faa2017-10-06 12:06:01 -07001794}
1795
David Ahern1d053da2018-05-09 20:34:21 -07001796/* must be called with rcu lock held */
1797struct fib6_info *fib6_table_lookup(struct net *net, struct fib6_table *table,
1798 int oif, struct flowi6 *fl6, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001800 struct fib6_node *fn, *saved_fn;
David Ahern8d1c8022018-04-17 17:33:26 -07001801 struct fib6_info *f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
David Ahern64547432018-05-09 20:34:19 -07001803 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001804 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
David Ahernca254492015-10-12 11:47:10 -07001806 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1807 oif = 0;
1808
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001809redo_rt6_select:
David Ahern23fb93a2018-04-17 17:33:23 -07001810 f6i = rt6_select(net, fn, oif, strict);
David Ahern23fb93a2018-04-17 17:33:23 -07001811 if (f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001812 fn = fib6_backtrack(fn, &fl6->saddr);
1813 if (fn)
1814 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001815 else if (strict & RT6_LOOKUP_F_REACHABLE) {
1816 /* also consider unreachable route */
1817 strict &= ~RT6_LOOKUP_F_REACHABLE;
1818 fn = saved_fn;
1819 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001820 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001821 }
1822
David Ahernd4bea422018-05-09 20:34:24 -07001823 trace_fib6_table_lookup(net, f6i, table, fl6);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -08001824
David Ahern1d053da2018-05-09 20:34:21 -07001825 return f6i;
1826}
1827
1828struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
1829 int oif, struct flowi6 *fl6,
1830 const struct sk_buff *skb, int flags)
1831{
1832 struct fib6_info *f6i;
1833 struct rt6_info *rt;
1834 int strict = 0;
1835
1836 strict |= flags & RT6_LOOKUP_F_IFACE;
1837 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
1838 if (net->ipv6.devconf_all->forwarding == 0)
1839 strict |= RT6_LOOKUP_F_REACHABLE;
1840
1841 rcu_read_lock();
1842
1843 f6i = fib6_table_lookup(net, table, oif, fl6, strict);
1844 if (f6i->fib6_nsiblings)
1845 f6i = fib6_multipath_select(net, f6i, fl6, oif, skb, strict);
1846
David Ahern23fb93a2018-04-17 17:33:23 -07001847 if (f6i == net->ipv6.fib6_null_entry) {
David Ahern421842e2018-04-17 17:33:18 -07001848 rt = net->ipv6.ip6_null_entry;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001849 rcu_read_unlock();
Wei Wangd3843fe2017-10-06 12:06:06 -07001850 dst_hold(&rt->dst);
Wei Wangd3843fe2017-10-06 12:06:06 -07001851 return rt;
David Ahern23fb93a2018-04-17 17:33:23 -07001852 }
1853
1854 /*Search through exception table */
1855 rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
1856 if (rt) {
David Ahernd4ead6b2018-04-17 17:33:16 -07001857 if (ip6_hold_safe(net, &rt, true))
Wei Wangd3843fe2017-10-06 12:06:06 -07001858 dst_use_noref(&rt->dst, jiffies);
David Ahernd4ead6b2018-04-17 17:33:16 -07001859
Wei Wang66f5d6c2017-10-06 12:06:10 -07001860 rcu_read_unlock();
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001861 return rt;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001862 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
David Ahern93c2fb22018-04-18 15:38:59 -07001863 !(f6i->fib6_flags & RTF_GATEWAY))) {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001864 /* Create a RTF_CACHE clone which will not be
1865 * owned by the fib6 tree. It is for the special case where
1866 * the daddr in the skb during the neighbor look-up is different
1867 * from the fl6->daddr used to look-up route here.
1868 */
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001869 struct rt6_info *uncached_rt;
1870
David Ahern23fb93a2018-04-17 17:33:23 -07001871 uncached_rt = ip6_rt_cache_alloc(f6i, &fl6->daddr, NULL);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001872
David Ahern4d85cd02018-04-20 15:37:59 -07001873 rcu_read_unlock();
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001874
Wei Wang1cfb71e2017-06-17 10:42:33 -07001875 if (uncached_rt) {
1876 /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
1877 * No need for another dst_hold()
1878 */
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07001879 rt6_uncached_list_add(uncached_rt);
Wei Wang81eb8442017-10-06 12:06:11 -07001880 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Wei Wang1cfb71e2017-06-17 10:42:33 -07001881 } else {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001882 uncached_rt = net->ipv6.ip6_null_entry;
Wei Wang1cfb71e2017-06-17 10:42:33 -07001883 dst_hold(&uncached_rt->dst);
1884 }
David Ahernb8115802015-11-19 12:24:22 -08001885
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001886 return uncached_rt;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001887 } else {
1888 /* Get a percpu copy */
1889
1890 struct rt6_info *pcpu_rt;
1891
Eric Dumazet951f7882017-10-08 21:07:18 -07001892 local_bh_disable();
David Ahern23fb93a2018-04-17 17:33:23 -07001893 pcpu_rt = rt6_get_pcpu_route(f6i);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001894
David Ahern93531c62018-04-17 17:33:25 -07001895 if (!pcpu_rt)
1896 pcpu_rt = rt6_make_pcpu_route(net, f6i);
1897
Eric Dumazet951f7882017-10-08 21:07:18 -07001898 local_bh_enable();
1899 rcu_read_unlock();
David Ahernd4bea422018-05-09 20:34:24 -07001900
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001901 return pcpu_rt;
1902 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001903}
David Ahern9ff74382016-06-13 13:44:19 -07001904EXPORT_SYMBOL_GPL(ip6_pol_route);
Thomas Grafc71099a2006-08-04 23:20:06 -07001905
David Ahernb75cc8f2018-03-02 08:32:17 -08001906static struct rt6_info *ip6_pol_route_input(struct net *net,
1907 struct fib6_table *table,
1908 struct flowi6 *fl6,
1909 const struct sk_buff *skb,
1910 int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -07001911{
David Ahernb75cc8f2018-03-02 08:32:17 -08001912 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -07001913}
1914
Mahesh Bandeward409b842016-09-16 12:59:08 -07001915struct dst_entry *ip6_route_input_lookup(struct net *net,
1916 struct net_device *dev,
David Ahernb75cc8f2018-03-02 08:32:17 -08001917 struct flowi6 *fl6,
1918 const struct sk_buff *skb,
1919 int flags)
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001920{
1921 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
1922 flags |= RT6_LOOKUP_F_IFACE;
1923
David Ahernb75cc8f2018-03-02 08:32:17 -08001924 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001925}
Mahesh Bandeward409b842016-09-16 12:59:08 -07001926EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001927
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001928static void ip6_multipath_l3_keys(const struct sk_buff *skb,
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001929 struct flow_keys *keys,
1930 struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001931{
1932 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
1933 const struct ipv6hdr *key_iph = outer_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001934 struct flow_keys *_flkeys = flkeys;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001935 const struct ipv6hdr *inner_iph;
1936 const struct icmp6hdr *icmph;
1937 struct ipv6hdr _inner_iph;
Eric Dumazetcea67a22018-04-29 09:54:59 -07001938 struct icmp6hdr _icmph;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001939
1940 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
1941 goto out;
1942
Eric Dumazetcea67a22018-04-29 09:54:59 -07001943 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
1944 sizeof(_icmph), &_icmph);
1945 if (!icmph)
1946 goto out;
1947
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001948 if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
1949 icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
1950 icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
1951 icmph->icmp6_type != ICMPV6_PARAMPROB)
1952 goto out;
1953
1954 inner_iph = skb_header_pointer(skb,
1955 skb_transport_offset(skb) + sizeof(*icmph),
1956 sizeof(_inner_iph), &_inner_iph);
1957 if (!inner_iph)
1958 goto out;
1959
1960 key_iph = inner_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001961 _flkeys = NULL;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001962out:
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001963 if (_flkeys) {
1964 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
1965 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
1966 keys->tags.flow_label = _flkeys->tags.flow_label;
1967 keys->basic.ip_proto = _flkeys->basic.ip_proto;
1968 } else {
1969 keys->addrs.v6addrs.src = key_iph->saddr;
1970 keys->addrs.v6addrs.dst = key_iph->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02001971 keys->tags.flow_label = ip6_flowlabel(key_iph);
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001972 keys->basic.ip_proto = key_iph->nexthdr;
1973 }
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001974}
1975
1976/* if skb is set it will be used and fl6 can be NULL */
David Ahernb4bac172018-03-02 08:32:18 -08001977u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
1978 const struct sk_buff *skb, struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001979{
1980 struct flow_keys hash_keys;
David Ahern9a2a5372018-03-02 08:32:15 -08001981 u32 mhash;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001982
David S. Millerbbfa0472018-03-12 11:09:33 -04001983 switch (ip6_multipath_hash_policy(net)) {
David Ahernb4bac172018-03-02 08:32:18 -08001984 case 0:
1985 memset(&hash_keys, 0, sizeof(hash_keys));
1986 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1987 if (skb) {
1988 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
1989 } else {
1990 hash_keys.addrs.v6addrs.src = fl6->saddr;
1991 hash_keys.addrs.v6addrs.dst = fl6->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02001992 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
David Ahernb4bac172018-03-02 08:32:18 -08001993 hash_keys.basic.ip_proto = fl6->flowi6_proto;
1994 }
1995 break;
1996 case 1:
1997 if (skb) {
1998 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
1999 struct flow_keys keys;
2000
2001 /* short-circuit if we already have L4 hash present */
2002 if (skb->l4_hash)
2003 return skb_get_hash_raw(skb) >> 1;
2004
2005 memset(&hash_keys, 0, sizeof(hash_keys));
2006
2007 if (!flkeys) {
2008 skb_flow_dissect_flow_keys(skb, &keys, flag);
2009 flkeys = &keys;
2010 }
2011 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2012 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2013 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2014 hash_keys.ports.src = flkeys->ports.src;
2015 hash_keys.ports.dst = flkeys->ports.dst;
2016 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2017 } else {
2018 memset(&hash_keys, 0, sizeof(hash_keys));
2019 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2020 hash_keys.addrs.v6addrs.src = fl6->saddr;
2021 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2022 hash_keys.ports.src = fl6->fl6_sport;
2023 hash_keys.ports.dst = fl6->fl6_dport;
2024 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2025 }
2026 break;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002027 }
David Ahern9a2a5372018-03-02 08:32:15 -08002028 mhash = flow_hash_from_keys(&hash_keys);
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002029
David Ahern9a2a5372018-03-02 08:32:15 -08002030 return mhash >> 1;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002031}
2032
Thomas Grafc71099a2006-08-04 23:20:06 -07002033void ip6_route_input(struct sk_buff *skb)
2034{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002035 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002036 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002037 int flags = RT6_LOOKUP_F_HAS_SADDR;
Jiri Benc904af042015-08-20 13:56:31 +02002038 struct ip_tunnel_info *tun_info;
David S. Miller4c9483b2011-03-12 16:22:43 -05002039 struct flowi6 fl6 = {
David Aherne0d56fd2016-09-10 12:09:57 -07002040 .flowi6_iif = skb->dev->ifindex,
David S. Miller4c9483b2011-03-12 16:22:43 -05002041 .daddr = iph->daddr,
2042 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002043 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05002044 .flowi6_mark = skb->mark,
2045 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07002046 };
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002047 struct flow_keys *flkeys = NULL, _flkeys;
Thomas Grafadaa70b2006-10-13 15:01:03 -07002048
Jiri Benc904af042015-08-20 13:56:31 +02002049 tun_info = skb_tunnel_info(skb);
Jiri Benc46fa0622015-08-28 20:48:19 +02002050 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
Jiri Benc904af042015-08-20 13:56:31 +02002051 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002052
2053 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2054 flkeys = &_flkeys;
2055
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002056 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
David Ahernb4bac172018-03-02 08:32:18 -08002057 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
Jiri Benc06e9d042015-08-20 13:56:26 +02002058 skb_dst_drop(skb);
David Ahernb75cc8f2018-03-02 08:32:17 -08002059 skb_dst_set(skb,
2060 ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07002061}
2062
David Ahernb75cc8f2018-03-02 08:32:17 -08002063static struct rt6_info *ip6_pol_route_output(struct net *net,
2064 struct fib6_table *table,
2065 struct flowi6 *fl6,
2066 const struct sk_buff *skb,
2067 int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002068{
David Ahernb75cc8f2018-03-02 08:32:17 -08002069 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07002070}
2071
Paolo Abeni6f21c962016-01-29 12:30:19 +01002072struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
2073 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002074{
David Ahernd46a9d62015-10-21 08:42:22 -07002075 bool any_src;
Thomas Grafc71099a2006-08-04 23:20:06 -07002076
Robert Shearman3ede0bb2018-09-19 13:56:53 +01002077 if (ipv6_addr_type(&fl6->daddr) &
2078 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
David Ahern4c1feac2016-09-10 12:09:56 -07002079 struct dst_entry *dst;
2080
2081 dst = l3mdev_link_scope_lookup(net, fl6);
2082 if (dst)
2083 return dst;
2084 }
David Ahernca254492015-10-12 11:47:10 -07002085
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002086 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00002087
David Ahernd46a9d62015-10-21 08:42:22 -07002088 any_src = ipv6_addr_any(&fl6->saddr);
David Ahern741a11d2015-09-28 10:12:13 -07002089 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
David Ahernd46a9d62015-10-21 08:42:22 -07002090 (fl6->flowi6_oif && any_src))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07002091 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07002092
David Ahernd46a9d62015-10-21 08:42:22 -07002093 if (!any_src)
Thomas Grafadaa70b2006-10-13 15:01:03 -07002094 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00002095 else if (sk)
2096 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002097
David Ahernb75cc8f2018-03-02 08:32:17 -08002098 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
Paolo Abeni6f21c962016-01-29 12:30:19 +01002100EXPORT_SYMBOL_GPL(ip6_route_output_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
David S. Miller2774c132011-03-01 14:59:04 -08002102struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07002103{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002104 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
Wei Wang1dbe32522017-06-17 10:42:26 -07002105 struct net_device *loopback_dev = net->loopback_dev;
David S. Miller14e50e52007-05-24 18:17:54 -07002106 struct dst_entry *new = NULL;
2107
Wei Wang1dbe32522017-06-17 10:42:26 -07002108 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
Steffen Klassert62cf27e2017-10-09 08:39:43 +02002109 DST_OBSOLETE_DEAD, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07002110 if (rt) {
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002111 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002112 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002113
Changli Gaod8d1f302010-06-10 23:31:35 -07002114 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07002115 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08002116 new->input = dst_discard;
Eric W. Biedermanede20592015-10-07 16:48:47 -05002117 new->output = dst_discard_out;
David S. Miller14e50e52007-05-24 18:17:54 -07002118
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002119 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07002120
Wei Wang1dbe32522017-06-17 10:42:26 -07002121 rt->rt6i_idev = in6_dev_get(loopback_dev);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002122 rt->rt6i_gateway = ort->rt6i_gateway;
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002123 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
David S. Miller14e50e52007-05-24 18:17:54 -07002124
2125 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2126#ifdef CONFIG_IPV6_SUBTREES
2127 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2128#endif
David S. Miller14e50e52007-05-24 18:17:54 -07002129 }
2130
David S. Miller69ead7a2011-03-01 14:45:33 -08002131 dst_release(dst_orig);
2132 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07002133}
David S. Miller14e50e52007-05-24 18:17:54 -07002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135/*
2136 * Destination cache support functions
2137 */
2138
David Ahern8d1c8022018-04-17 17:33:26 -07002139static bool fib6_check(struct fib6_info *f6i, u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002140{
Steffen Klassert36143642017-08-25 09:05:42 +02002141 u32 rt_cookie = 0;
Wei Wangc5cff852017-08-21 09:47:10 -07002142
David Ahern8ae86972018-04-20 15:38:03 -07002143 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
David Ahern93531c62018-04-17 17:33:25 -07002144 return false;
2145
2146 if (fib6_check_expired(f6i))
2147 return false;
2148
2149 return true;
2150}
2151
David Aherna68886a2018-04-20 15:38:02 -07002152static struct dst_entry *rt6_check(struct rt6_info *rt,
2153 struct fib6_info *from,
2154 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002155{
Wei Wangc5cff852017-08-21 09:47:10 -07002156 u32 rt_cookie = 0;
2157
David Aherna68886a2018-04-20 15:38:02 -07002158 if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) ||
David Ahern93531c62018-04-17 17:33:25 -07002159 rt_cookie != cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002160 return NULL;
2161
2162 if (rt6_check_expired(rt))
2163 return NULL;
2164
2165 return &rt->dst;
2166}
2167
David Aherna68886a2018-04-20 15:38:02 -07002168static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2169 struct fib6_info *from,
2170 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002171{
Martin KaFai Lau5973fb12015-11-11 11:51:07 -08002172 if (!__rt6_check_expired(rt) &&
2173 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
David Aherna68886a2018-04-20 15:38:02 -07002174 fib6_check(from, cookie))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002175 return &rt->dst;
2176 else
2177 return NULL;
2178}
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2181{
David Aherna87b7dc2018-04-20 15:38:00 -07002182 struct dst_entry *dst_ret;
David Aherna68886a2018-04-20 15:38:02 -07002183 struct fib6_info *from;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 struct rt6_info *rt;
2185
David Aherna87b7dc2018-04-20 15:38:00 -07002186 rt = container_of(dst, struct rt6_info, dst);
2187
2188 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00002190 /* All IPV6 dsts are created with ->obsolete set to the value
2191 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2192 * into this function always.
2193 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02002194
David Aherna68886a2018-04-20 15:38:02 -07002195 from = rcu_dereference(rt->from);
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002196
David Aherna68886a2018-04-20 15:38:02 -07002197 if (from && (rt->rt6i_flags & RTF_PCPU ||
2198 unlikely(!list_empty(&rt->rt6i_uncached))))
2199 dst_ret = rt6_dst_from_check(rt, from, cookie);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002200 else
David Aherna68886a2018-04-20 15:38:02 -07002201 dst_ret = rt6_check(rt, from, cookie);
David Aherna87b7dc2018-04-20 15:38:00 -07002202
2203 rcu_read_unlock();
2204
2205 return dst_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
2208static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2209{
2210 struct rt6_info *rt = (struct rt6_info *) dst;
2211
2212 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002213 if (rt->rt6i_flags & RTF_CACHE) {
David Ahernc3c14da2018-04-23 11:32:06 -07002214 rcu_read_lock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002215 if (rt6_check_expired(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07002216 rt6_remove_exception_rt(rt);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002217 dst = NULL;
2218 }
David Ahernc3c14da2018-04-23 11:32:06 -07002219 rcu_read_unlock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002220 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002222 dst = NULL;
2223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002225 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226}
2227
2228static void ip6_link_failure(struct sk_buff *skb)
2229{
2230 struct rt6_info *rt;
2231
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002232 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Eric Dumazetadf30902009-06-02 05:19:30 +00002234 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 if (rt) {
David Ahern8a14e462018-04-23 11:32:07 -07002236 rcu_read_lock();
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002237 if (rt->rt6i_flags & RTF_CACHE) {
Wei Wangad65a2f2017-06-17 10:42:35 -07002238 if (dst_hold_safe(&rt->dst))
David Ahern93531c62018-04-17 17:33:25 -07002239 rt6_remove_exception_rt(rt);
Wei Wangc5cff852017-08-21 09:47:10 -07002240 } else {
David Aherna68886a2018-04-20 15:38:02 -07002241 struct fib6_info *from;
Wei Wangc5cff852017-08-21 09:47:10 -07002242 struct fib6_node *fn;
2243
David Aherna68886a2018-04-20 15:38:02 -07002244 from = rcu_dereference(rt->from);
2245 if (from) {
2246 fn = rcu_dereference(from->fib6_node);
2247 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2248 fn->fn_sernum = -1;
2249 }
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002250 }
David Ahern8a14e462018-04-23 11:32:07 -07002251 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
2253}
2254
David Ahern6a3e0302018-04-20 15:37:57 -07002255static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2256{
David Aherna68886a2018-04-20 15:38:02 -07002257 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2258 struct fib6_info *from;
2259
2260 rcu_read_lock();
2261 from = rcu_dereference(rt0->from);
2262 if (from)
2263 rt0->dst.expires = from->expires;
2264 rcu_read_unlock();
2265 }
David Ahern6a3e0302018-04-20 15:37:57 -07002266
2267 dst_set_expires(&rt0->dst, timeout);
2268 rt0->rt6i_flags |= RTF_EXPIRES;
2269}
2270
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002271static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2272{
2273 struct net *net = dev_net(rt->dst.dev);
2274
David Ahernd4ead6b2018-04-17 17:33:16 -07002275 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002276 rt->rt6i_flags |= RTF_MODIFIED;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002277 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2278}
2279
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002280static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2281{
David Aherna68886a2018-04-20 15:38:02 -07002282 bool from_set;
2283
2284 rcu_read_lock();
2285 from_set = !!rcu_dereference(rt->from);
2286 rcu_read_unlock();
2287
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002288 return !(rt->rt6i_flags & RTF_CACHE) &&
David Aherna68886a2018-04-20 15:38:02 -07002289 (rt->rt6i_flags & RTF_PCPU || from_set);
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002290}
2291
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002292static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2293 const struct ipv6hdr *iph, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
Julian Anastasov0dec8792017-02-06 23:14:16 +02002295 const struct in6_addr *daddr, *saddr;
Ian Morris67ba4152014-08-24 21:53:10 +01002296 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Xin Long19bda362016-10-28 18:18:01 +08002298 if (dst_metric_locked(dst, RTAX_MTU))
2299 return;
2300
Julian Anastasov0dec8792017-02-06 23:14:16 +02002301 if (iph) {
2302 daddr = &iph->daddr;
2303 saddr = &iph->saddr;
2304 } else if (sk) {
2305 daddr = &sk->sk_v6_daddr;
2306 saddr = &inet6_sk(sk)->saddr;
2307 } else {
2308 daddr = NULL;
2309 saddr = NULL;
2310 }
2311 dst_confirm_neigh(dst, daddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002312 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2313 if (mtu >= dst_mtu(dst))
2314 return;
David S. Miller81aded22012-06-15 14:54:11 -07002315
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002316 if (!rt6_cache_allowed_for_pmtu(rt6)) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002317 rt6_do_update_pmtu(rt6, mtu);
Wei Wang2b760fc2017-10-06 12:06:03 -07002318 /* update rt6_ex->stamp for cache */
2319 if (rt6->rt6i_flags & RTF_CACHE)
2320 rt6_update_exception_stamp_rt(rt6);
Julian Anastasov0dec8792017-02-06 23:14:16 +02002321 } else if (daddr) {
David Aherna68886a2018-04-20 15:38:02 -07002322 struct fib6_info *from;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002323 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01002324
David Ahern4d85cd02018-04-20 15:37:59 -07002325 rcu_read_lock();
David Aherna68886a2018-04-20 15:38:02 -07002326 from = rcu_dereference(rt6->from);
2327 nrt6 = ip6_rt_cache_alloc(from, daddr, saddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002328 if (nrt6) {
2329 rt6_do_update_pmtu(nrt6, mtu);
David Aherna68886a2018-04-20 15:38:02 -07002330 if (rt6_insert_exception(nrt6, from))
Wei Wang2b760fc2017-10-06 12:06:03 -07002331 dst_release_immediate(&nrt6->dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002332 }
David Aherna68886a2018-04-20 15:38:02 -07002333 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
2335}
2336
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002337static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2338 struct sk_buff *skb, u32 mtu)
2339{
2340 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
2341}
2342
David S. Miller42ae66c2012-06-15 20:01:57 -07002343void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002344 int oif, u32 mark, kuid_t uid)
David S. Miller81aded22012-06-15 14:54:11 -07002345{
2346 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2347 struct dst_entry *dst;
Maciej Żenczykowskidc920952018-09-29 23:44:51 -07002348 struct flowi6 fl6 = {
2349 .flowi6_oif = oif,
2350 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2351 .daddr = iph->daddr,
2352 .saddr = iph->saddr,
2353 .flowlabel = ip6_flowinfo(iph),
2354 .flowi6_uid = uid,
2355 };
David S. Miller81aded22012-06-15 14:54:11 -07002356
2357 dst = ip6_route_output(net, NULL, &fl6);
2358 if (!dst->error)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002359 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
David S. Miller81aded22012-06-15 14:54:11 -07002360 dst_release(dst);
2361}
2362EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2363
2364void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2365{
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002366 struct dst_entry *dst;
2367
David S. Miller81aded22012-06-15 14:54:11 -07002368 ip6_update_pmtu(skb, sock_net(sk), mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002369 sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid);
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002370
2371 dst = __sk_dst_get(sk);
2372 if (!dst || !dst->obsolete ||
2373 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2374 return;
2375
2376 bh_lock_sock(sk);
2377 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2378 ip6_datagram_dst_update(sk, false);
2379 bh_unlock_sock(sk);
David S. Miller81aded22012-06-15 14:54:11 -07002380}
2381EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2382
Alexey Kodanev7d6850f2018-04-03 15:00:07 +03002383void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2384 const struct flowi6 *fl6)
2385{
2386#ifdef CONFIG_IPV6_SUBTREES
2387 struct ipv6_pinfo *np = inet6_sk(sk);
2388#endif
2389
2390 ip6_dst_store(sk, dst,
2391 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2392 &sk->sk_v6_daddr : NULL,
2393#ifdef CONFIG_IPV6_SUBTREES
2394 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2395 &np->saddr :
2396#endif
2397 NULL);
2398}
2399
Duan Jiongb55b76b2013-09-04 19:44:21 +08002400/* Handle redirects */
2401struct ip6rd_flowi {
2402 struct flowi6 fl6;
2403 struct in6_addr gateway;
2404};
2405
2406static struct rt6_info *__ip6_route_redirect(struct net *net,
2407 struct fib6_table *table,
2408 struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08002409 const struct sk_buff *skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002410 int flags)
2411{
2412 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
David Ahern23fb93a2018-04-17 17:33:23 -07002413 struct rt6_info *ret = NULL, *rt_cache;
David Ahern8d1c8022018-04-17 17:33:26 -07002414 struct fib6_info *rt;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002415 struct fib6_node *fn;
2416
2417 /* Get the "current" route for this destination and
Alexander Alemayhu67c408c2017-01-07 23:53:00 +01002418 * check if the redirect has come from appropriate router.
Duan Jiongb55b76b2013-09-04 19:44:21 +08002419 *
2420 * RFC 4861 specifies that redirects should only be
2421 * accepted if they come from the nexthop to the target.
2422 * Due to the way the routes are chosen, this notion
2423 * is a bit fuzzy and one might need to check all possible
2424 * routes.
2425 */
2426
Wei Wang66f5d6c2017-10-06 12:06:10 -07002427 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07002428 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002429restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002430 for_each_fib6_node_rt_rcu(fn) {
David Ahern5e670d82018-04-17 17:33:14 -07002431 if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +02002432 continue;
David Ahern14895682018-04-17 17:33:17 -07002433 if (fib6_check_expired(rt))
Duan Jiongb55b76b2013-09-04 19:44:21 +08002434 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002435 if (rt->fib6_flags & RTF_REJECT)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002436 break;
David Ahern93c2fb22018-04-18 15:38:59 -07002437 if (!(rt->fib6_flags & RTF_GATEWAY))
Duan Jiongb55b76b2013-09-04 19:44:21 +08002438 continue;
David Ahern5e670d82018-04-17 17:33:14 -07002439 if (fl6->flowi6_oif != rt->fib6_nh.nh_dev->ifindex)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002440 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07002441 /* rt_cache's gateway might be different from its 'parent'
2442 * in the case of an ip redirect.
2443 * So we keep searching in the exception table if the gateway
2444 * is different.
2445 */
David Ahern5e670d82018-04-17 17:33:14 -07002446 if (!ipv6_addr_equal(&rdfl->gateway, &rt->fib6_nh.nh_gw)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07002447 rt_cache = rt6_find_cached_rt(rt,
2448 &fl6->daddr,
2449 &fl6->saddr);
2450 if (rt_cache &&
2451 ipv6_addr_equal(&rdfl->gateway,
2452 &rt_cache->rt6i_gateway)) {
David Ahern23fb93a2018-04-17 17:33:23 -07002453 ret = rt_cache;
Wei Wang2b760fc2017-10-06 12:06:03 -07002454 break;
2455 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002456 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07002457 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002458 break;
2459 }
2460
2461 if (!rt)
David Ahern421842e2018-04-17 17:33:18 -07002462 rt = net->ipv6.fib6_null_entry;
David Ahern93c2fb22018-04-18 15:38:59 -07002463 else if (rt->fib6_flags & RTF_REJECT) {
David Ahern23fb93a2018-04-17 17:33:23 -07002464 ret = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002465 goto out;
2466 }
2467
David Ahern421842e2018-04-17 17:33:18 -07002468 if (rt == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002469 fn = fib6_backtrack(fn, &fl6->saddr);
2470 if (fn)
2471 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002472 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002473
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002474out:
David Ahern23fb93a2018-04-17 17:33:23 -07002475 if (ret)
Wei Wange873e4b2018-07-21 20:56:32 -07002476 ip6_hold_safe(net, &ret, true);
David Ahern23fb93a2018-04-17 17:33:23 -07002477 else
2478 ret = ip6_create_rt_rcu(rt);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002479
Wei Wang66f5d6c2017-10-06 12:06:10 -07002480 rcu_read_unlock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08002481
Paolo Abenib65f1642017-10-19 09:31:43 +02002482 trace_fib6_table_lookup(net, rt, table, fl6);
David Ahern23fb93a2018-04-17 17:33:23 -07002483 return ret;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002484};
2485
2486static struct dst_entry *ip6_route_redirect(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08002487 const struct flowi6 *fl6,
2488 const struct sk_buff *skb,
2489 const struct in6_addr *gateway)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002490{
2491 int flags = RT6_LOOKUP_F_HAS_SADDR;
2492 struct ip6rd_flowi rdfl;
2493
2494 rdfl.fl6 = *fl6;
2495 rdfl.gateway = *gateway;
2496
David Ahernb75cc8f2018-03-02 08:32:17 -08002497 return fib6_rule_lookup(net, &rdfl.fl6, skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002498 flags, __ip6_route_redirect);
2499}
2500
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002501void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2502 kuid_t uid)
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002503{
2504 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2505 struct dst_entry *dst;
Maciej Żenczykowski1f7f10a2018-09-29 23:44:48 -07002506 struct flowi6 fl6 = {
2507 .flowi6_iif = LOOPBACK_IFINDEX,
2508 .flowi6_oif = oif,
2509 .flowi6_mark = mark,
2510 .daddr = iph->daddr,
2511 .saddr = iph->saddr,
2512 .flowlabel = ip6_flowinfo(iph),
2513 .flowi6_uid = uid,
2514 };
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002515
David Ahernb75cc8f2018-03-02 08:32:17 -08002516 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002517 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002518 dst_release(dst);
2519}
2520EXPORT_SYMBOL_GPL(ip6_redirect);
2521
Maciej Żenczykowskid4563362018-09-29 23:44:50 -07002522void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
Duan Jiongc92a59e2013-08-22 12:07:35 +08002523{
2524 const struct ipv6hdr *iph = ipv6_hdr(skb);
2525 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2526 struct dst_entry *dst;
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07002527 struct flowi6 fl6 = {
2528 .flowi6_iif = LOOPBACK_IFINDEX,
2529 .flowi6_oif = oif,
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07002530 .daddr = msg->dest,
2531 .saddr = iph->daddr,
2532 .flowi6_uid = sock_net_uid(net, NULL),
2533 };
Duan Jiongc92a59e2013-08-22 12:07:35 +08002534
David Ahernb75cc8f2018-03-02 08:32:17 -08002535 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002536 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08002537 dst_release(dst);
2538}
2539
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002540void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
2541{
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002542 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2543 sk->sk_uid);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002544}
2545EXPORT_SYMBOL_GPL(ip6_sk_redirect);
2546
David S. Miller0dbaee32010-12-13 12:52:14 -08002547static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
David S. Miller0dbaee32010-12-13 12:52:14 -08002549 struct net_device *dev = dst->dev;
2550 unsigned int mtu = dst_mtu(dst);
2551 struct net *net = dev_net(dev);
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
2554
Daniel Lezcano55786892008-03-04 13:47:47 -08002555 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
2556 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
2558 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002559 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
2560 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
2561 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 * rely only on pmtu discovery"
2563 */
2564 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
2565 mtu = IPV6_MAXPLEN;
2566 return mtu;
2567}
2568
Steffen Klassertebb762f2011-11-23 02:12:51 +00002569static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002570{
David S. Millerd33e4552010-12-14 13:01:14 -08002571 struct inet6_dev *idev;
David Ahernd4ead6b2018-04-17 17:33:16 -07002572 unsigned int mtu;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002573
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002574 mtu = dst_metric_raw(dst, RTAX_MTU);
2575 if (mtu)
2576 goto out;
2577
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002578 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08002579
2580 rcu_read_lock();
2581 idev = __in6_dev_get(dst->dev);
2582 if (idev)
2583 mtu = idev->cnf.mtu6;
2584 rcu_read_unlock();
2585
Eric Dumazet30f78d82014-04-10 21:23:36 -07002586out:
Roopa Prabhu14972cb2016-08-24 20:10:43 -07002587 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2588
2589 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
David S. Millerd33e4552010-12-14 13:01:14 -08002590}
2591
David Ahern901731b2018-05-21 09:08:14 -07002592/* MTU selection:
2593 * 1. mtu on route is locked - use it
2594 * 2. mtu from nexthop exception
2595 * 3. mtu from egress device
2596 *
2597 * based on ip6_dst_mtu_forward and exception logic of
2598 * rt6_find_cached_rt; called with rcu_read_lock
2599 */
2600u32 ip6_mtu_from_fib6(struct fib6_info *f6i, struct in6_addr *daddr,
2601 struct in6_addr *saddr)
2602{
2603 struct rt6_exception_bucket *bucket;
2604 struct rt6_exception *rt6_ex;
2605 struct in6_addr *src_key;
2606 struct inet6_dev *idev;
2607 u32 mtu = 0;
2608
2609 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
2610 mtu = f6i->fib6_pmtu;
2611 if (mtu)
2612 goto out;
2613 }
2614
2615 src_key = NULL;
2616#ifdef CONFIG_IPV6_SUBTREES
2617 if (f6i->fib6_src.plen)
2618 src_key = saddr;
2619#endif
2620
2621 bucket = rcu_dereference(f6i->rt6i_exception_bucket);
2622 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
2623 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
2624 mtu = dst_metric_raw(&rt6_ex->rt6i->dst, RTAX_MTU);
2625
2626 if (likely(!mtu)) {
2627 struct net_device *dev = fib6_info_nh_dev(f6i);
2628
2629 mtu = IPV6_MIN_MTU;
2630 idev = __in6_dev_get(dev);
2631 if (idev && idev->cnf.mtu6 > mtu)
2632 mtu = idev->cnf.mtu6;
2633 }
2634
2635 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2636out:
2637 return mtu - lwtunnel_headroom(fib6_info_nh_lwt(f6i), mtu);
2638}
2639
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08002640struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05002641 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642{
David S. Miller87a11572011-12-06 17:04:13 -05002643 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 struct rt6_info *rt;
2645 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002646 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
David S. Miller38308472011-12-03 18:02:47 -05002648 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00002649 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Martin KaFai Lauad706862015-08-14 11:05:52 -07002651 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05002652 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05002654 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 goto out;
2656 }
2657
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002658 rt->dst.flags |= DST_HOST;
Brendan McGrath588753f2017-12-13 22:14:57 +11002659 rt->dst.input = ip6_input;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002660 rt->dst.output = ip6_output;
Julian Anastasov550bab42013-10-20 15:43:04 +03002661 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05002662 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002663 rt->rt6i_dst.plen = 128;
2664 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08002665 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Ido Schimmel4c981e22018-01-07 12:45:04 +02002667 /* Add this dst into uncached_list so that rt6_disable_ip() can
Wei Wang587fea72017-06-17 10:42:36 -07002668 * do proper release of the net_device
2669 */
2670 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002671 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
David S. Miller87a11572011-12-06 17:04:13 -05002673 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675out:
David S. Miller87a11572011-12-06 17:04:13 -05002676 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
Daniel Lezcano569d3642008-01-18 03:56:57 -08002679static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002681 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08002682 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
2683 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
2684 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
2685 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
2686 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00002687 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Eric Dumazetfc66f952010-10-08 06:37:34 +00002689 entries = dst_entries_get_fast(ops);
Michal Kubeček49a18d82013-08-01 10:04:24 +02002690 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00002691 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 goto out;
2693
Benjamin Thery6891a342008-03-04 13:49:47 -08002694 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08002695 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00002696 entries = dst_entries_get_slow(ops);
2697 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08002698 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08002700 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00002701 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}
2703
David Ahern8c145862016-04-24 21:26:04 -07002704static struct rt6_info *ip6_nh_lookup_table(struct net *net,
2705 struct fib6_config *cfg,
David Ahernf4797b32018-01-25 16:55:08 -08002706 const struct in6_addr *gw_addr,
2707 u32 tbid, int flags)
David Ahern8c145862016-04-24 21:26:04 -07002708{
2709 struct flowi6 fl6 = {
2710 .flowi6_oif = cfg->fc_ifindex,
2711 .daddr = *gw_addr,
2712 .saddr = cfg->fc_prefsrc,
2713 };
2714 struct fib6_table *table;
2715 struct rt6_info *rt;
David Ahern8c145862016-04-24 21:26:04 -07002716
David Ahernf4797b32018-01-25 16:55:08 -08002717 table = fib6_get_table(net, tbid);
David Ahern8c145862016-04-24 21:26:04 -07002718 if (!table)
2719 return NULL;
2720
2721 if (!ipv6_addr_any(&cfg->fc_prefsrc))
2722 flags |= RT6_LOOKUP_F_HAS_SADDR;
2723
David Ahernf4797b32018-01-25 16:55:08 -08002724 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
David Ahernb75cc8f2018-03-02 08:32:17 -08002725 rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
David Ahern8c145862016-04-24 21:26:04 -07002726
2727 /* if table lookup failed, fall back to full lookup */
2728 if (rt == net->ipv6.ip6_null_entry) {
2729 ip6_rt_put(rt);
2730 rt = NULL;
2731 }
2732
2733 return rt;
2734}
2735
David Ahernfc1e64e2018-01-25 16:55:09 -08002736static int ip6_route_check_nh_onlink(struct net *net,
2737 struct fib6_config *cfg,
David Ahern9fbb7042018-03-13 08:29:36 -07002738 const struct net_device *dev,
David Ahernfc1e64e2018-01-25 16:55:09 -08002739 struct netlink_ext_ack *extack)
2740{
David Ahern44750f82018-02-06 13:17:06 -08002741 u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
David Ahernfc1e64e2018-01-25 16:55:09 -08002742 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2743 u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
2744 struct rt6_info *grt;
2745 int err;
2746
2747 err = 0;
2748 grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
2749 if (grt) {
David Ahern58e354c2018-02-06 12:14:12 -08002750 if (!grt->dst.error &&
2751 (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
David Ahern44750f82018-02-06 13:17:06 -08002752 NL_SET_ERR_MSG(extack,
2753 "Nexthop has invalid gateway or device mismatch");
David Ahernfc1e64e2018-01-25 16:55:09 -08002754 err = -EINVAL;
2755 }
2756
2757 ip6_rt_put(grt);
2758 }
2759
2760 return err;
2761}
2762
David Ahern1edce992018-01-25 16:55:07 -08002763static int ip6_route_check_nh(struct net *net,
2764 struct fib6_config *cfg,
2765 struct net_device **_dev,
2766 struct inet6_dev **idev)
2767{
2768 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2769 struct net_device *dev = _dev ? *_dev : NULL;
2770 struct rt6_info *grt = NULL;
2771 int err = -EHOSTUNREACH;
2772
2773 if (cfg->fc_table) {
David Ahernf4797b32018-01-25 16:55:08 -08002774 int flags = RT6_LOOKUP_F_IFACE;
2775
2776 grt = ip6_nh_lookup_table(net, cfg, gw_addr,
2777 cfg->fc_table, flags);
David Ahern1edce992018-01-25 16:55:07 -08002778 if (grt) {
2779 if (grt->rt6i_flags & RTF_GATEWAY ||
2780 (dev && dev != grt->dst.dev)) {
2781 ip6_rt_put(grt);
2782 grt = NULL;
2783 }
2784 }
2785 }
2786
2787 if (!grt)
David Ahernb75cc8f2018-03-02 08:32:17 -08002788 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
David Ahern1edce992018-01-25 16:55:07 -08002789
2790 if (!grt)
2791 goto out;
2792
2793 if (dev) {
2794 if (dev != grt->dst.dev) {
2795 ip6_rt_put(grt);
2796 goto out;
2797 }
2798 } else {
2799 *_dev = dev = grt->dst.dev;
2800 *idev = grt->rt6i_idev;
2801 dev_hold(dev);
2802 in6_dev_hold(grt->rt6i_idev);
2803 }
2804
2805 if (!(grt->rt6i_flags & RTF_GATEWAY))
2806 err = 0;
2807
2808 ip6_rt_put(grt);
2809
2810out:
2811 return err;
2812}
2813
David Ahern9fbb7042018-03-13 08:29:36 -07002814static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
2815 struct net_device **_dev, struct inet6_dev **idev,
2816 struct netlink_ext_ack *extack)
2817{
2818 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2819 int gwa_type = ipv6_addr_type(gw_addr);
David Ahern232378e2018-03-13 08:29:37 -07002820 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
David Ahern9fbb7042018-03-13 08:29:36 -07002821 const struct net_device *dev = *_dev;
David Ahern232378e2018-03-13 08:29:37 -07002822 bool need_addr_check = !dev;
David Ahern9fbb7042018-03-13 08:29:36 -07002823 int err = -EINVAL;
2824
2825 /* if gw_addr is local we will fail to detect this in case
2826 * address is still TENTATIVE (DAD in progress). rt6_lookup()
2827 * will return already-added prefix route via interface that
2828 * prefix route was assigned to, which might be non-loopback.
2829 */
David Ahern232378e2018-03-13 08:29:37 -07002830 if (dev &&
2831 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2832 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
David Ahern9fbb7042018-03-13 08:29:36 -07002833 goto out;
2834 }
2835
2836 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
2837 /* IPv6 strictly inhibits using not link-local
2838 * addresses as nexthop address.
2839 * Otherwise, router will not able to send redirects.
2840 * It is very good, but in some (rare!) circumstances
2841 * (SIT, PtP, NBMA NOARP links) it is handy to allow
2842 * some exceptions. --ANK
2843 * We allow IPv4-mapped nexthops to support RFC4798-type
2844 * addressing
2845 */
2846 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
2847 NL_SET_ERR_MSG(extack, "Invalid gateway address");
2848 goto out;
2849 }
2850
2851 if (cfg->fc_flags & RTNH_F_ONLINK)
2852 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
2853 else
2854 err = ip6_route_check_nh(net, cfg, _dev, idev);
2855
2856 if (err)
2857 goto out;
2858 }
2859
2860 /* reload in case device was changed */
2861 dev = *_dev;
2862
2863 err = -EINVAL;
2864 if (!dev) {
2865 NL_SET_ERR_MSG(extack, "Egress device not specified");
2866 goto out;
2867 } else if (dev->flags & IFF_LOOPBACK) {
2868 NL_SET_ERR_MSG(extack,
2869 "Egress device can not be loopback device for this route");
2870 goto out;
2871 }
David Ahern232378e2018-03-13 08:29:37 -07002872
2873 /* if we did not check gw_addr above, do so now that the
2874 * egress device has been resolved.
2875 */
2876 if (need_addr_check &&
2877 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2878 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
2879 goto out;
2880 }
2881
David Ahern9fbb7042018-03-13 08:29:36 -07002882 err = 0;
2883out:
2884 return err;
2885}
2886
David Ahern8d1c8022018-04-17 17:33:26 -07002887static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
David Ahernacb54e32018-04-17 17:33:22 -07002888 gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06002889 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
Daniel Lezcano55786892008-03-04 13:47:47 -08002891 struct net *net = cfg->fc_nlinfo.nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07002892 struct fib6_info *rt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 struct net_device *dev = NULL;
2894 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07002895 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 int addr_type;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002897 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
David Ahern557c44b2017-04-19 14:19:43 -07002899 /* RTF_PCPU is an internal flag; can not be set by userspace */
David Ahernd5d531c2017-05-21 10:12:05 -06002900 if (cfg->fc_flags & RTF_PCPU) {
2901 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
David Ahern557c44b2017-04-19 14:19:43 -07002902 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06002903 }
David Ahern557c44b2017-04-19 14:19:43 -07002904
Wei Wang2ea23522017-10-27 17:30:12 -07002905 /* RTF_CACHE is an internal flag; can not be set by userspace */
2906 if (cfg->fc_flags & RTF_CACHE) {
2907 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
2908 goto out;
2909 }
2910
David Aherne8478e82018-04-17 17:33:13 -07002911 if (cfg->fc_type > RTN_MAX) {
2912 NL_SET_ERR_MSG(extack, "Invalid route type");
2913 goto out;
2914 }
2915
David Ahernd5d531c2017-05-21 10:12:05 -06002916 if (cfg->fc_dst_len > 128) {
2917 NL_SET_ERR_MSG(extack, "Invalid prefix length");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002918 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06002919 }
2920 if (cfg->fc_src_len > 128) {
2921 NL_SET_ERR_MSG(extack, "Invalid source address length");
2922 goto out;
2923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924#ifndef CONFIG_IPV6_SUBTREES
David Ahernd5d531c2017-05-21 10:12:05 -06002925 if (cfg->fc_src_len) {
2926 NL_SET_ERR_MSG(extack,
2927 "Specifying source address requires IPV6_SUBTREES to be enabled");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002928 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06002929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07002931 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08002933 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 if (!dev)
2935 goto out;
2936 idev = in6_dev_get(dev);
2937 if (!idev)
2938 goto out;
2939 }
2940
Thomas Graf86872cb2006-08-22 00:01:08 -07002941 if (cfg->fc_metric == 0)
2942 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
David Ahernfc1e64e2018-01-25 16:55:09 -08002944 if (cfg->fc_flags & RTNH_F_ONLINK) {
2945 if (!dev) {
2946 NL_SET_ERR_MSG(extack,
2947 "Nexthop device required for onlink");
2948 err = -ENODEV;
2949 goto out;
2950 }
2951
2952 if (!(dev->flags & IFF_UP)) {
2953 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2954 err = -ENETDOWN;
2955 goto out;
2956 }
2957 }
2958
Matti Vaittinend71314b2011-11-14 00:14:49 +00002959 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05002960 if (cfg->fc_nlinfo.nlh &&
2961 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00002962 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05002963 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00002964 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00002965 table = fib6_new_table(net, cfg->fc_table);
2966 }
2967 } else {
2968 table = fib6_new_table(net, cfg->fc_table);
2969 }
David S. Miller38308472011-12-03 18:02:47 -05002970
2971 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002972 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07002973
David Ahern93531c62018-04-17 17:33:25 -07002974 err = -ENOMEM;
2975 rt = fib6_info_alloc(gfp_flags);
2976 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 goto out;
David Ahern93531c62018-04-17 17:33:25 -07002978
David Ahern767a2212018-10-04 20:07:51 -07002979 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len);
2980 if (IS_ERR(rt->fib6_metrics)) {
2981 err = PTR_ERR(rt->fib6_metrics);
2982 goto out;
2983 }
2984
David Ahern93531c62018-04-17 17:33:25 -07002985 if (cfg->fc_flags & RTF_ADDRCONF)
2986 rt->dst_nocount = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Gao feng1716a962012-04-06 00:13:10 +00002988 if (cfg->fc_flags & RTF_EXPIRES)
David Ahern14895682018-04-17 17:33:17 -07002989 fib6_set_expires(rt, jiffies +
Gao feng1716a962012-04-06 00:13:10 +00002990 clock_t_to_jiffies(cfg->fc_expires));
2991 else
David Ahern14895682018-04-17 17:33:17 -07002992 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Thomas Graf86872cb2006-08-22 00:01:08 -07002994 if (cfg->fc_protocol == RTPROT_UNSPEC)
2995 cfg->fc_protocol = RTPROT_BOOT;
David Ahern93c2fb22018-04-18 15:38:59 -07002996 rt->fib6_protocol = cfg->fc_protocol;
Thomas Graf86872cb2006-08-22 00:01:08 -07002997
2998 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003000 if (cfg->fc_encap) {
3001 struct lwtunnel_state *lwtstate;
3002
David Ahern30357d72017-01-30 12:07:37 -08003003 err = lwtunnel_build_state(cfg->fc_encap_type,
Tom Herbert127eb7c2015-08-24 09:45:41 -07003004 cfg->fc_encap, AF_INET6, cfg,
David Ahern9ae28722017-05-27 16:19:28 -06003005 &lwtstate, extack);
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003006 if (err)
3007 goto out;
David Ahern5e670d82018-04-17 17:33:14 -07003008 rt->fib6_nh.nh_lwtstate = lwtstate_get(lwtstate);
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003009 }
3010
David Ahern93c2fb22018-04-18 15:38:59 -07003011 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3012 rt->fib6_dst.plen = cfg->fc_dst_len;
3013 if (rt->fib6_dst.plen == 128)
David Ahern3b6761d2018-04-17 17:33:20 -07003014 rt->dst_host = true;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01003015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07003017 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3018 rt->fib6_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019#endif
3020
David Ahern93c2fb22018-04-18 15:38:59 -07003021 rt->fib6_metric = cfg->fc_metric;
David Ahern5e670d82018-04-17 17:33:14 -07003022 rt->fib6_nh.nh_weight = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
David Aherne8478e82018-04-17 17:33:13 -07003024 rt->fib6_type = cfg->fc_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
3026 /* We cannot add true routes via loopback here,
3027 they would result in kernel looping; promote them to reject routes
3028 */
Thomas Graf86872cb2006-08-22 00:01:08 -07003029 if ((cfg->fc_flags & RTF_REJECT) ||
David S. Miller38308472011-12-03 18:02:47 -05003030 (dev && (dev->flags & IFF_LOOPBACK) &&
3031 !(addr_type & IPV6_ADDR_LOOPBACK) &&
3032 !(cfg->fc_flags & RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08003034 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 if (dev) {
3036 dev_put(dev);
3037 in6_dev_put(idev);
3038 }
Daniel Lezcano55786892008-03-04 13:47:47 -08003039 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 dev_hold(dev);
3041 idev = in6_dev_get(dev);
3042 if (!idev) {
3043 err = -ENODEV;
3044 goto out;
3045 }
3046 }
David Ahern93c2fb22018-04-18 15:38:59 -07003047 rt->fib6_flags = RTF_REJECT|RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 goto install_route;
3049 }
3050
Thomas Graf86872cb2006-08-22 00:01:08 -07003051 if (cfg->fc_flags & RTF_GATEWAY) {
David Ahern9fbb7042018-03-13 08:29:36 -07003052 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3053 if (err)
Florian Westphal48ed7b22015-05-21 00:25:41 +02003054 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
David Ahern93531c62018-04-17 17:33:25 -07003056 rt->fib6_nh.nh_gw = cfg->fc_gateway;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 }
3058
3059 err = -ENODEV;
David S. Miller38308472011-12-03 18:02:47 -05003060 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 goto out;
3062
Lorenzo Bianconi428604f2018-03-29 11:02:24 +02003063 if (idev->cnf.disable_ipv6) {
3064 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3065 err = -EACCES;
3066 goto out;
3067 }
3068
David Ahern955ec4c2018-01-24 19:45:29 -08003069 if (!(dev->flags & IFF_UP)) {
3070 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3071 err = -ENETDOWN;
3072 goto out;
3073 }
3074
Daniel Walterc3968a82011-04-13 21:10:57 +00003075 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
3076 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
David Ahernd5d531c2017-05-21 10:12:05 -06003077 NL_SET_ERR_MSG(extack, "Invalid source address");
Daniel Walterc3968a82011-04-13 21:10:57 +00003078 err = -EINVAL;
3079 goto out;
3080 }
David Ahern93c2fb22018-04-18 15:38:59 -07003081 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3082 rt->fib6_prefsrc.plen = 128;
Daniel Walterc3968a82011-04-13 21:10:57 +00003083 } else
David Ahern93c2fb22018-04-18 15:38:59 -07003084 rt->fib6_prefsrc.plen = 0;
Daniel Walterc3968a82011-04-13 21:10:57 +00003085
David Ahern93c2fb22018-04-18 15:38:59 -07003086 rt->fib6_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
3088install_route:
David Ahern93c2fb22018-04-18 15:38:59 -07003089 if (!(rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
Ido Schimmel5609b802018-01-07 12:45:06 +02003090 !netif_carrier_ok(dev))
David Ahern5e670d82018-04-17 17:33:14 -07003091 rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
3092 rt->fib6_nh.nh_flags |= (cfg->fc_flags & RTNH_F_ONLINK);
David Ahern93531c62018-04-17 17:33:25 -07003093 rt->fib6_nh.nh_dev = dev;
David Ahern93c2fb22018-04-18 15:38:59 -07003094 rt->fib6_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08003095
David Aherndcd1f572018-04-18 15:39:05 -07003096 if (idev)
3097 in6_dev_put(idev);
3098
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003099 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100out:
3101 if (dev)
3102 dev_put(dev);
3103 if (idev)
3104 in6_dev_put(idev);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003105
David Ahern93531c62018-04-17 17:33:25 -07003106 fib6_info_release(rt);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003107 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003108}
3109
David Ahernacb54e32018-04-17 17:33:22 -07003110int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003111 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003112{
David Ahern8d1c8022018-04-17 17:33:26 -07003113 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003114 int err;
3115
David Ahernacb54e32018-04-17 17:33:22 -07003116 rt = ip6_route_info_create(cfg, gfp_flags, extack);
David Ahernd4ead6b2018-04-17 17:33:16 -07003117 if (IS_ERR(rt))
3118 return PTR_ERR(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003119
David Ahernd4ead6b2018-04-17 17:33:16 -07003120 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
David Ahern93531c62018-04-17 17:33:25 -07003121 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 return err;
3124}
3125
David Ahern8d1c8022018-04-17 17:33:26 -07003126static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
David Ahernafb1d4b52018-04-17 17:33:11 -07003128 struct net *net = info->nl_net;
Thomas Grafc71099a2006-08-04 23:20:06 -07003129 struct fib6_table *table;
David Ahernafb1d4b52018-04-17 17:33:11 -07003130 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
David Ahern421842e2018-04-17 17:33:18 -07003132 if (rt == net->ipv6.fib6_null_entry) {
Gao feng6825a262012-09-19 19:25:34 +00003133 err = -ENOENT;
3134 goto out;
3135 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07003136
David Ahern93c2fb22018-04-18 15:38:59 -07003137 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003138 spin_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07003139 err = fib6_del(rt, info);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003140 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Gao feng6825a262012-09-19 19:25:34 +00003142out:
David Ahern93531c62018-04-17 17:33:25 -07003143 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 return err;
3145}
3146
David Ahern8d1c8022018-04-17 17:33:26 -07003147int ip6_del_rt(struct net *net, struct fib6_info *rt)
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003148{
David Ahernafb1d4b52018-04-17 17:33:11 -07003149 struct nl_info info = { .nl_net = net };
3150
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003151 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003152}
3153
David Ahern8d1c8022018-04-17 17:33:26 -07003154static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
David Ahern0ae81332017-02-02 12:37:08 -08003155{
3156 struct nl_info *info = &cfg->fc_nlinfo;
WANG Conge3330032017-02-27 16:07:43 -08003157 struct net *net = info->nl_net;
David Ahern16a16cd2017-02-02 12:37:11 -08003158 struct sk_buff *skb = NULL;
David Ahern0ae81332017-02-02 12:37:08 -08003159 struct fib6_table *table;
WANG Conge3330032017-02-27 16:07:43 -08003160 int err = -ENOENT;
David Ahern0ae81332017-02-02 12:37:08 -08003161
David Ahern421842e2018-04-17 17:33:18 -07003162 if (rt == net->ipv6.fib6_null_entry)
WANG Conge3330032017-02-27 16:07:43 -08003163 goto out_put;
David Ahern93c2fb22018-04-18 15:38:59 -07003164 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003165 spin_lock_bh(&table->tb6_lock);
David Ahern0ae81332017-02-02 12:37:08 -08003166
David Ahern93c2fb22018-04-18 15:38:59 -07003167 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
David Ahern8d1c8022018-04-17 17:33:26 -07003168 struct fib6_info *sibling, *next_sibling;
David Ahern0ae81332017-02-02 12:37:08 -08003169
David Ahern16a16cd2017-02-02 12:37:11 -08003170 /* prefer to send a single notification with all hops */
3171 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3172 if (skb) {
3173 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3174
David Ahernd4ead6b2018-04-17 17:33:16 -07003175 if (rt6_fill_node(net, skb, rt, NULL,
David Ahern16a16cd2017-02-02 12:37:11 -08003176 NULL, NULL, 0, RTM_DELROUTE,
3177 info->portid, seq, 0) < 0) {
3178 kfree_skb(skb);
3179 skb = NULL;
3180 } else
3181 info->skip_notify = 1;
3182 }
3183
David Ahern0ae81332017-02-02 12:37:08 -08003184 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07003185 &rt->fib6_siblings,
3186 fib6_siblings) {
David Ahern0ae81332017-02-02 12:37:08 -08003187 err = fib6_del(sibling, info);
3188 if (err)
WANG Conge3330032017-02-27 16:07:43 -08003189 goto out_unlock;
David Ahern0ae81332017-02-02 12:37:08 -08003190 }
3191 }
3192
3193 err = fib6_del(rt, info);
WANG Conge3330032017-02-27 16:07:43 -08003194out_unlock:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003195 spin_unlock_bh(&table->tb6_lock);
WANG Conge3330032017-02-27 16:07:43 -08003196out_put:
David Ahern93531c62018-04-17 17:33:25 -07003197 fib6_info_release(rt);
David Ahern16a16cd2017-02-02 12:37:11 -08003198
3199 if (skb) {
WANG Conge3330032017-02-27 16:07:43 -08003200 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
David Ahern16a16cd2017-02-02 12:37:11 -08003201 info->nlh, gfp_any());
3202 }
David Ahern0ae81332017-02-02 12:37:08 -08003203 return err;
3204}
3205
David Ahern23fb93a2018-04-17 17:33:23 -07003206static int ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
3207{
3208 int rc = -ESRCH;
3209
3210 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3211 goto out;
3212
3213 if (cfg->fc_flags & RTF_GATEWAY &&
3214 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3215 goto out;
3216 if (dst_hold_safe(&rt->dst))
3217 rc = rt6_remove_exception_rt(rt);
3218out:
3219 return rc;
3220}
3221
David Ahern333c4302017-05-21 10:12:04 -06003222static int ip6_route_del(struct fib6_config *cfg,
3223 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
David Ahern8d1c8022018-04-17 17:33:26 -07003225 struct rt6_info *rt_cache;
Thomas Grafc71099a2006-08-04 23:20:06 -07003226 struct fib6_table *table;
David Ahern8d1c8022018-04-17 17:33:26 -07003227 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 int err = -ESRCH;
3230
Daniel Lezcano55786892008-03-04 13:47:47 -08003231 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David Ahernd5d531c2017-05-21 10:12:05 -06003232 if (!table) {
3233 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Grafc71099a2006-08-04 23:20:06 -07003234 return err;
David Ahernd5d531c2017-05-21 10:12:05 -06003235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Wei Wang66f5d6c2017-10-06 12:06:10 -07003237 rcu_read_lock();
Thomas Grafc71099a2006-08-04 23:20:06 -07003238
3239 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07003240 &cfg->fc_dst, cfg->fc_dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07003241 &cfg->fc_src, cfg->fc_src_len,
Wei Wang2b760fc2017-10-06 12:06:03 -07003242 !(cfg->fc_flags & RTF_CACHE));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 if (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003245 for_each_fib6_node_rt_rcu(fn) {
Wei Wang2b760fc2017-10-06 12:06:03 -07003246 if (cfg->fc_flags & RTF_CACHE) {
David Ahern23fb93a2018-04-17 17:33:23 -07003247 int rc;
3248
Wei Wang2b760fc2017-10-06 12:06:03 -07003249 rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst,
3250 &cfg->fc_src);
David Ahern23fb93a2018-04-17 17:33:23 -07003251 if (rt_cache) {
3252 rc = ip6_del_cached_rt(rt_cache, cfg);
Eric Dumazet9e575012018-05-09 10:05:46 -07003253 if (rc != -ESRCH) {
3254 rcu_read_unlock();
David Ahern23fb93a2018-04-17 17:33:23 -07003255 return rc;
Eric Dumazet9e575012018-05-09 10:05:46 -07003256 }
David Ahern23fb93a2018-04-17 17:33:23 -07003257 }
3258 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07003259 }
Thomas Graf86872cb2006-08-22 00:01:08 -07003260 if (cfg->fc_ifindex &&
David Ahern5e670d82018-04-17 17:33:14 -07003261 (!rt->fib6_nh.nh_dev ||
3262 rt->fib6_nh.nh_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07003264 if (cfg->fc_flags & RTF_GATEWAY &&
David Ahern5e670d82018-04-17 17:33:14 -07003265 !ipv6_addr_equal(&cfg->fc_gateway, &rt->fib6_nh.nh_gw))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003267 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003269 if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol)
Mantas Mc2ed1882016-12-16 10:30:59 +02003270 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07003271 if (!fib6_info_hold_safe(rt))
3272 continue;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003273 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
David Ahern0ae81332017-02-02 12:37:08 -08003275 /* if gateway was specified only delete the one hop */
3276 if (cfg->fc_flags & RTF_GATEWAY)
3277 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3278
3279 return __ip6_del_rt_siblings(rt, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 }
3281 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003282 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
3284 return err;
3285}
3286
David S. Miller6700c272012-07-17 03:29:28 -07003287static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003288{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003289 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07003290 struct rt6_info *rt, *nrt = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07003291 struct ndisc_options ndopts;
3292 struct inet6_dev *in6_dev;
3293 struct neighbour *neigh;
David Aherna68886a2018-04-20 15:38:02 -07003294 struct fib6_info *from;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003295 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07003296 int optlen, on_link;
3297 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07003298
Simon Horman29a3cad2013-05-28 20:34:26 +00003299 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003300 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07003301
3302 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07003303 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003304 return;
3305 }
3306
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003307 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07003308
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003309 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003310 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003311 return;
3312 }
3313
David S. Miller6e157b62012-07-12 00:05:02 -07003314 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003315 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003316 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003317 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07003318 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003319 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003320 return;
3321 }
3322
3323 in6_dev = __in6_dev_get(skb->dev);
3324 if (!in6_dev)
3325 return;
3326 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3327 return;
3328
3329 /* RFC2461 8.1:
3330 * The IP source address of the Redirect MUST be the same as the current
3331 * first-hop router for the specified ICMP Destination Address.
3332 */
3333
Alexander Aringf997c552016-06-15 21:20:23 +02003334 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003335 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3336 return;
3337 }
David S. Miller6e157b62012-07-12 00:05:02 -07003338
3339 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07003340 if (ndopts.nd_opts_tgt_lladdr) {
3341 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3342 skb->dev);
3343 if (!lladdr) {
3344 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3345 return;
3346 }
3347 }
3348
David S. Miller6e157b62012-07-12 00:05:02 -07003349 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01003350 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07003351 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
3352 return;
3353 }
3354
3355 /* Redirect received -> path was valid.
3356 * Look, redirects are sent only in response to data packets,
3357 * so that this nexthop apparently is reachable. --ANK
3358 */
Julian Anastasov0dec8792017-02-06 23:14:16 +02003359 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
David S. Miller6e157b62012-07-12 00:05:02 -07003360
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003361 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07003362 if (!neigh)
3363 return;
3364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 /*
3366 * We have finally decided to accept it.
3367 */
3368
Alexander Aringf997c552016-06-15 21:20:23 +02003369 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 NEIGH_UPDATE_F_WEAK_OVERRIDE|
3371 NEIGH_UPDATE_F_OVERRIDE|
3372 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02003373 NEIGH_UPDATE_F_ISROUTER)),
3374 NDISC_REDIRECT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
David Ahern4d85cd02018-04-20 15:37:59 -07003376 rcu_read_lock();
David Aherna68886a2018-04-20 15:38:02 -07003377 from = rcu_dereference(rt->from);
Wei Wange873e4b2018-07-21 20:56:32 -07003378 /* This fib6_info_hold() is safe here because we hold reference to rt
3379 * and rt already holds reference to fib6_info.
3380 */
David Ahern8a14e462018-04-23 11:32:07 -07003381 fib6_info_hold(from);
David Ahern4d85cd02018-04-20 15:37:59 -07003382 rcu_read_unlock();
David Ahern8a14e462018-04-23 11:32:07 -07003383
3384 nrt = ip6_rt_cache_alloc(from, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05003385 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 goto out;
3387
3388 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
3389 if (on_link)
3390 nrt->rt6i_flags &= ~RTF_GATEWAY;
3391
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003392 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Wei Wang2b760fc2017-10-06 12:06:03 -07003394 /* No need to remove rt from the exception table if rt is
3395 * a cached route because rt6_insert_exception() will
3396 * takes care of it
3397 */
David Ahern8a14e462018-04-23 11:32:07 -07003398 if (rt6_insert_exception(nrt, from)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07003399 dst_release_immediate(&nrt->dst);
3400 goto out;
3401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
Changli Gaod8d1f302010-06-10 23:31:35 -07003403 netevent.old = &rt->dst;
3404 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003405 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00003406 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07003407 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
3408
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409out:
David Ahern8a14e462018-04-23 11:32:07 -07003410 fib6_info_release(from);
David S. Millere8599ff2012-07-11 23:43:53 -07003411 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07003412}
3413
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003414#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -07003415static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003416 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07003417 const struct in6_addr *gwaddr,
3418 struct net_device *dev)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003419{
David Ahern830218c2016-10-24 10:52:35 -07003420 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3421 int ifindex = dev->ifindex;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003422 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07003423 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003424 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003425
David Ahern830218c2016-10-24 10:52:35 -07003426 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05003427 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003428 return NULL;
3429
Wei Wang66f5d6c2017-10-06 12:06:10 -07003430 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07003431 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003432 if (!fn)
3433 goto out;
3434
Wei Wang66f5d6c2017-10-06 12:06:10 -07003435 for_each_fib6_node_rt_rcu(fn) {
David Ahern5e670d82018-04-17 17:33:14 -07003436 if (rt->fib6_nh.nh_dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003437 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003438 if ((rt->fib6_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003439 continue;
David Ahern5e670d82018-04-17 17:33:14 -07003440 if (!ipv6_addr_equal(&rt->fib6_nh.nh_gw, gwaddr))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003441 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07003442 if (!fib6_info_hold_safe(rt))
3443 continue;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003444 break;
3445 }
3446out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003447 rcu_read_unlock();
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003448 return rt;
3449}
3450
David Ahern8d1c8022018-04-17 17:33:26 -07003451static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003452 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07003453 const struct in6_addr *gwaddr,
3454 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +00003455 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003456{
Thomas Graf86872cb2006-08-22 00:01:08 -07003457 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08003458 .fc_metric = IP6_RT_PRIO_USER,
David Ahern830218c2016-10-24 10:52:35 -07003459 .fc_ifindex = dev->ifindex,
Thomas Graf86872cb2006-08-22 00:01:08 -07003460 .fc_dst_len = prefixlen,
3461 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
3462 RTF_UP | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08003463 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07003464 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003465 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08003466 .fc_nlinfo.nlh = NULL,
3467 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07003468 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003469
David Ahern830218c2016-10-24 10:52:35 -07003470 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003471 cfg.fc_dst = *prefix;
3472 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07003473
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08003474 /* We should treat it as a default route if prefix length is 0. */
3475 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07003476 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003477
David Ahernacb54e32018-04-17 17:33:22 -07003478 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003479
David Ahern830218c2016-10-24 10:52:35 -07003480 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003481}
3482#endif
3483
David Ahern8d1c8022018-04-17 17:33:26 -07003484struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07003485 const struct in6_addr *addr,
3486 struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003487{
David Ahern830218c2016-10-24 10:52:35 -07003488 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
David Ahern8d1c8022018-04-17 17:33:26 -07003489 struct fib6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07003490 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
David Ahernafb1d4b52018-04-17 17:33:11 -07003492 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05003493 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003494 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
Wei Wang66f5d6c2017-10-06 12:06:10 -07003496 rcu_read_lock();
3497 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Ahern5e670d82018-04-17 17:33:14 -07003498 if (dev == rt->fib6_nh.nh_dev &&
David Ahern93c2fb22018-04-18 15:38:59 -07003499 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Ahern5e670d82018-04-17 17:33:14 -07003500 ipv6_addr_equal(&rt->fib6_nh.nh_gw, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 break;
3502 }
Wei Wange873e4b2018-07-21 20:56:32 -07003503 if (rt && !fib6_info_hold_safe(rt))
3504 rt = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003505 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 return rt;
3507}
3508
David Ahern8d1c8022018-04-17 17:33:26 -07003509struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07003510 const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08003511 struct net_device *dev,
3512 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513{
Thomas Graf86872cb2006-08-22 00:01:08 -07003514 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07003515 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08003516 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07003517 .fc_ifindex = dev->ifindex,
3518 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
3519 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08003520 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07003521 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003522 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08003523 .fc_nlinfo.nlh = NULL,
David Ahernafb1d4b52018-04-17 17:33:11 -07003524 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07003525 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003527 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
David Ahernacb54e32018-04-17 17:33:22 -07003529 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
David Ahern830218c2016-10-24 10:52:35 -07003530 struct fib6_table *table;
3531
3532 table = fib6_get_table(dev_net(dev), cfg.fc_table);
3533 if (table)
3534 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
David Ahernafb1d4b52018-04-17 17:33:11 -07003537 return rt6_get_dflt_router(net, gwaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538}
3539
David Ahernafb1d4b52018-04-17 17:33:11 -07003540static void __rt6_purge_dflt_routers(struct net *net,
3541 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
David Ahern8d1c8022018-04-17 17:33:26 -07003543 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
3545restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003546 rcu_read_lock();
3547 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Aherndcd1f572018-04-18 15:39:05 -07003548 struct net_device *dev = fib6_info_nh_dev(rt);
3549 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
3550
David Ahern93c2fb22018-04-18 15:38:59 -07003551 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
Wei Wange873e4b2018-07-21 20:56:32 -07003552 (!idev || idev->cnf.accept_ra != 2) &&
3553 fib6_info_hold_safe(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07003554 rcu_read_unlock();
3555 ip6_del_rt(net, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 goto restart;
3557 }
3558 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003559 rcu_read_unlock();
David Ahern830218c2016-10-24 10:52:35 -07003560
3561 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3562}
3563
3564void rt6_purge_dflt_routers(struct net *net)
3565{
3566 struct fib6_table *table;
3567 struct hlist_head *head;
3568 unsigned int h;
3569
3570 rcu_read_lock();
3571
3572 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3573 head = &net->ipv6.fib_table_hash[h];
3574 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3575 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
David Ahernafb1d4b52018-04-17 17:33:11 -07003576 __rt6_purge_dflt_routers(net, table);
David Ahern830218c2016-10-24 10:52:35 -07003577 }
3578 }
3579
3580 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581}
3582
Daniel Lezcano55786892008-03-04 13:47:47 -08003583static void rtmsg_to_fib6_config(struct net *net,
3584 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07003585 struct fib6_config *cfg)
3586{
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07003587 *cfg = (struct fib6_config){
3588 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
3589 : RT6_TABLE_MAIN,
3590 .fc_ifindex = rtmsg->rtmsg_ifindex,
3591 .fc_metric = rtmsg->rtmsg_metric,
3592 .fc_expires = rtmsg->rtmsg_info,
3593 .fc_dst_len = rtmsg->rtmsg_dst_len,
3594 .fc_src_len = rtmsg->rtmsg_src_len,
3595 .fc_flags = rtmsg->rtmsg_flags,
3596 .fc_type = rtmsg->rtmsg_type,
Thomas Graf86872cb2006-08-22 00:01:08 -07003597
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07003598 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07003599
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07003600 .fc_dst = rtmsg->rtmsg_dst,
3601 .fc_src = rtmsg->rtmsg_src,
3602 .fc_gateway = rtmsg->rtmsg_gateway,
3603 };
Thomas Graf86872cb2006-08-22 00:01:08 -07003604}
3605
Daniel Lezcano55786892008-03-04 13:47:47 -08003606int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607{
Thomas Graf86872cb2006-08-22 00:01:08 -07003608 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 struct in6_rtmsg rtmsg;
3610 int err;
3611
Ian Morris67ba4152014-08-24 21:53:10 +01003612 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 case SIOCADDRT: /* Add a route */
3614 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00003615 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 return -EPERM;
3617 err = copy_from_user(&rtmsg, arg,
3618 sizeof(struct in6_rtmsg));
3619 if (err)
3620 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07003621
Daniel Lezcano55786892008-03-04 13:47:47 -08003622 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07003623
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 rtnl_lock();
3625 switch (cmd) {
3626 case SIOCADDRT:
David Ahernacb54e32018-04-17 17:33:22 -07003627 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 break;
3629 case SIOCDELRT:
David Ahern333c4302017-05-21 10:12:04 -06003630 err = ip6_route_del(&cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 break;
3632 default:
3633 err = -EINVAL;
3634 }
3635 rtnl_unlock();
3636
3637 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
3640 return -EINVAL;
3641}
3642
3643/*
3644 * Drop the packet on the floor
3645 */
3646
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07003647static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003649 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00003650 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003651 switch (ipstats_mib_noroutes) {
3652 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07003653 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00003654 if (type == IPV6_ADDR_ANY) {
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -04003655 IP6_INC_STATS(dev_net(dst->dev),
3656 __in6_dev_get_safely(skb->dev),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07003657 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003658 break;
3659 }
3660 /* FALLTHROUGH */
3661 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07003662 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
3663 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003664 break;
3665 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00003666 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 kfree_skb(skb);
3668 return 0;
3669}
3670
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003671static int ip6_pkt_discard(struct sk_buff *skb)
3672{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003673 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003674}
3675
Eric W. Biedermanede20592015-10-07 16:48:47 -05003676static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677{
Eric Dumazetadf30902009-06-02 05:19:30 +00003678 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003679 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680}
3681
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003682static int ip6_pkt_prohibit(struct sk_buff *skb)
3683{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003684 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003685}
3686
Eric W. Biedermanede20592015-10-07 16:48:47 -05003687static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003688{
Eric Dumazetadf30902009-06-02 05:19:30 +00003689 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003690 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003691}
3692
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693/*
3694 * Allocate a dst for local (unicast / anycast) address.
3695 */
3696
David Ahern360a9882018-04-18 15:39:00 -07003697struct fib6_info *addrconf_f6i_alloc(struct net *net,
3698 struct inet6_dev *idev,
3699 const struct in6_addr *addr,
3700 bool anycast, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701{
David Ahernca254492015-10-12 11:47:10 -07003702 u32 tb_id;
David Ahern4832c302017-08-17 12:17:20 -07003703 struct net_device *dev = idev->dev;
David Ahern360a9882018-04-18 15:39:00 -07003704 struct fib6_info *f6i;
David Ahern5f02ce242016-09-10 12:09:54 -07003705
David Ahern360a9882018-04-18 15:39:00 -07003706 f6i = fib6_info_alloc(gfp_flags);
3707 if (!f6i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 return ERR_PTR(-ENOMEM);
3709
David Ahern767a2212018-10-04 20:07:51 -07003710 f6i->fib6_metrics = ip_fib_metrics_init(net, NULL, 0);
David Ahern360a9882018-04-18 15:39:00 -07003711 f6i->dst_nocount = true;
David Ahern360a9882018-04-18 15:39:00 -07003712 f6i->dst_host = true;
3713 f6i->fib6_protocol = RTPROT_KERNEL;
3714 f6i->fib6_flags = RTF_UP | RTF_NONEXTHOP;
David Aherne8478e82018-04-17 17:33:13 -07003715 if (anycast) {
David Ahern360a9882018-04-18 15:39:00 -07003716 f6i->fib6_type = RTN_ANYCAST;
3717 f6i->fib6_flags |= RTF_ANYCAST;
David Aherne8478e82018-04-17 17:33:13 -07003718 } else {
David Ahern360a9882018-04-18 15:39:00 -07003719 f6i->fib6_type = RTN_LOCAL;
3720 f6i->fib6_flags |= RTF_LOCAL;
David Aherne8478e82018-04-17 17:33:13 -07003721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
David Ahern360a9882018-04-18 15:39:00 -07003723 f6i->fib6_nh.nh_gw = *addr;
David Ahern93531c62018-04-17 17:33:25 -07003724 dev_hold(dev);
David Ahern360a9882018-04-18 15:39:00 -07003725 f6i->fib6_nh.nh_dev = dev;
3726 f6i->fib6_dst.addr = *addr;
3727 f6i->fib6_dst.plen = 128;
David Ahernca254492015-10-12 11:47:10 -07003728 tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL;
David Ahern360a9882018-04-18 15:39:00 -07003729 f6i->fib6_table = fib6_get_table(net, tb_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
David Ahern360a9882018-04-18 15:39:00 -07003731 return f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732}
3733
Daniel Walterc3968a82011-04-13 21:10:57 +00003734/* remove deleted ip from prefsrc entries */
3735struct arg_dev_net_ip {
3736 struct net_device *dev;
3737 struct net *net;
3738 struct in6_addr *addr;
3739};
3740
David Ahern8d1c8022018-04-17 17:33:26 -07003741static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
Daniel Walterc3968a82011-04-13 21:10:57 +00003742{
3743 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3744 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3745 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3746
David Ahern5e670d82018-04-17 17:33:14 -07003747 if (((void *)rt->fib6_nh.nh_dev == dev || !dev) &&
David Ahern421842e2018-04-17 17:33:18 -07003748 rt != net->ipv6.fib6_null_entry &&
David Ahern93c2fb22018-04-18 15:38:59 -07003749 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
Wei Wang60006a42017-10-06 12:05:58 -07003750 spin_lock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00003751 /* remove prefsrc entry */
David Ahern93c2fb22018-04-18 15:38:59 -07003752 rt->fib6_prefsrc.plen = 0;
Wei Wang60006a42017-10-06 12:05:58 -07003753 spin_unlock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00003754 }
3755 return 0;
3756}
3757
3758void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
3759{
3760 struct net *net = dev_net(ifp->idev->dev);
3761 struct arg_dev_net_ip adni = {
3762 .dev = ifp->idev->dev,
3763 .net = net,
3764 .addr = &ifp->addr,
3765 };
Li RongQing0c3584d2013-12-27 16:32:38 +08003766 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00003767}
3768
Duan Jiongbe7a0102014-05-15 15:56:14 +08003769#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
Duan Jiongbe7a0102014-05-15 15:56:14 +08003770
3771/* Remove routers and update dst entries when gateway turn into host. */
David Ahern8d1c8022018-04-17 17:33:26 -07003772static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
Duan Jiongbe7a0102014-05-15 15:56:14 +08003773{
3774 struct in6_addr *gateway = (struct in6_addr *)arg;
3775
David Ahern93c2fb22018-04-18 15:38:59 -07003776 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Ahern5e670d82018-04-17 17:33:14 -07003777 ipv6_addr_equal(gateway, &rt->fib6_nh.nh_gw)) {
Duan Jiongbe7a0102014-05-15 15:56:14 +08003778 return -1;
3779 }
Wei Wangb16cb452017-10-06 12:06:00 -07003780
3781 /* Further clean up cached routes in exception table.
3782 * This is needed because cached route may have a different
3783 * gateway than its 'parent' in the case of an ip redirect.
3784 */
3785 rt6_exceptions_clean_tohost(rt, gateway);
3786
Duan Jiongbe7a0102014-05-15 15:56:14 +08003787 return 0;
3788}
3789
3790void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
3791{
3792 fib6_clean_all(net, fib6_clean_tohost, gateway);
3793}
3794
Ido Schimmel2127d952018-01-07 12:45:03 +02003795struct arg_netdev_event {
3796 const struct net_device *dev;
Ido Schimmel4c981e22018-01-07 12:45:04 +02003797 union {
3798 unsigned int nh_flags;
3799 unsigned long event;
3800 };
Ido Schimmel2127d952018-01-07 12:45:03 +02003801};
3802
David Ahern8d1c8022018-04-17 17:33:26 -07003803static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003804{
David Ahern8d1c8022018-04-17 17:33:26 -07003805 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003806 struct fib6_node *fn;
3807
David Ahern93c2fb22018-04-18 15:38:59 -07003808 fn = rcu_dereference_protected(rt->fib6_node,
3809 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003810 iter = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -07003811 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003812 while (iter) {
David Ahern93c2fb22018-04-18 15:38:59 -07003813 if (iter->fib6_metric == rt->fib6_metric &&
David Ahern33bd5ac2018-07-03 14:36:21 -07003814 rt6_qualify_for_ecmp(iter))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003815 return iter;
David Ahern8fb11a92018-05-04 13:54:24 -07003816 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -07003817 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003818 }
3819
3820 return NULL;
3821}
3822
David Ahern8d1c8022018-04-17 17:33:26 -07003823static bool rt6_is_dead(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003824{
David Ahern5e670d82018-04-17 17:33:14 -07003825 if (rt->fib6_nh.nh_flags & RTNH_F_DEAD ||
3826 (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN &&
David Aherndcd1f572018-04-18 15:39:05 -07003827 fib6_ignore_linkdown(rt)))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003828 return true;
3829
3830 return false;
3831}
3832
David Ahern8d1c8022018-04-17 17:33:26 -07003833static int rt6_multipath_total_weight(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003834{
David Ahern8d1c8022018-04-17 17:33:26 -07003835 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003836 int total = 0;
3837
3838 if (!rt6_is_dead(rt))
David Ahern5e670d82018-04-17 17:33:14 -07003839 total += rt->fib6_nh.nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003840
David Ahern93c2fb22018-04-18 15:38:59 -07003841 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003842 if (!rt6_is_dead(iter))
David Ahern5e670d82018-04-17 17:33:14 -07003843 total += iter->fib6_nh.nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003844 }
3845
3846 return total;
3847}
3848
David Ahern8d1c8022018-04-17 17:33:26 -07003849static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003850{
3851 int upper_bound = -1;
3852
3853 if (!rt6_is_dead(rt)) {
David Ahern5e670d82018-04-17 17:33:14 -07003854 *weight += rt->fib6_nh.nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003855 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
3856 total) - 1;
3857 }
David Ahern5e670d82018-04-17 17:33:14 -07003858 atomic_set(&rt->fib6_nh.nh_upper_bound, upper_bound);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003859}
3860
David Ahern8d1c8022018-04-17 17:33:26 -07003861static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003862{
David Ahern8d1c8022018-04-17 17:33:26 -07003863 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003864 int weight = 0;
3865
3866 rt6_upper_bound_set(rt, &weight, total);
3867
David Ahern93c2fb22018-04-18 15:38:59 -07003868 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003869 rt6_upper_bound_set(iter, &weight, total);
3870}
3871
David Ahern8d1c8022018-04-17 17:33:26 -07003872void rt6_multipath_rebalance(struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003873{
David Ahern8d1c8022018-04-17 17:33:26 -07003874 struct fib6_info *first;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003875 int total;
3876
3877 /* In case the entire multipath route was marked for flushing,
3878 * then there is no need to rebalance upon the removal of every
3879 * sibling route.
3880 */
David Ahern93c2fb22018-04-18 15:38:59 -07003881 if (!rt->fib6_nsiblings || rt->should_flush)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003882 return;
3883
3884 /* During lookup routes are evaluated in order, so we need to
3885 * make sure upper bounds are assigned from the first sibling
3886 * onwards.
3887 */
3888 first = rt6_multipath_first_sibling(rt);
3889 if (WARN_ON_ONCE(!first))
3890 return;
3891
3892 total = rt6_multipath_total_weight(first);
3893 rt6_multipath_upper_bound_set(first, total);
3894}
3895
David Ahern8d1c8022018-04-17 17:33:26 -07003896static int fib6_ifup(struct fib6_info *rt, void *p_arg)
Ido Schimmel2127d952018-01-07 12:45:03 +02003897{
3898 const struct arg_netdev_event *arg = p_arg;
David Ahern7aef6852018-04-17 17:33:10 -07003899 struct net *net = dev_net(arg->dev);
Ido Schimmel2127d952018-01-07 12:45:03 +02003900
David Ahern421842e2018-04-17 17:33:18 -07003901 if (rt != net->ipv6.fib6_null_entry && rt->fib6_nh.nh_dev == arg->dev) {
David Ahern5e670d82018-04-17 17:33:14 -07003902 rt->fib6_nh.nh_flags &= ~arg->nh_flags;
David Ahern7aef6852018-04-17 17:33:10 -07003903 fib6_update_sernum_upto_root(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003904 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02003905 }
Ido Schimmel2127d952018-01-07 12:45:03 +02003906
3907 return 0;
3908}
3909
3910void rt6_sync_up(struct net_device *dev, unsigned int nh_flags)
3911{
3912 struct arg_netdev_event arg = {
3913 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02003914 {
3915 .nh_flags = nh_flags,
3916 },
Ido Schimmel2127d952018-01-07 12:45:03 +02003917 };
3918
3919 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
3920 arg.nh_flags |= RTNH_F_LINKDOWN;
3921
3922 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
3923}
3924
David Ahern8d1c8022018-04-17 17:33:26 -07003925static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02003926 const struct net_device *dev)
3927{
David Ahern8d1c8022018-04-17 17:33:26 -07003928 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003929
David Ahern5e670d82018-04-17 17:33:14 -07003930 if (rt->fib6_nh.nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003931 return true;
David Ahern93c2fb22018-04-18 15:38:59 -07003932 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern5e670d82018-04-17 17:33:14 -07003933 if (iter->fib6_nh.nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003934 return true;
3935
3936 return false;
3937}
3938
David Ahern8d1c8022018-04-17 17:33:26 -07003939static void rt6_multipath_flush(struct fib6_info *rt)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003940{
David Ahern8d1c8022018-04-17 17:33:26 -07003941 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003942
3943 rt->should_flush = 1;
David Ahern93c2fb22018-04-18 15:38:59 -07003944 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003945 iter->should_flush = 1;
3946}
3947
David Ahern8d1c8022018-04-17 17:33:26 -07003948static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02003949 const struct net_device *down_dev)
3950{
David Ahern8d1c8022018-04-17 17:33:26 -07003951 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003952 unsigned int dead = 0;
3953
David Ahern5e670d82018-04-17 17:33:14 -07003954 if (rt->fib6_nh.nh_dev == down_dev ||
3955 rt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003956 dead++;
David Ahern93c2fb22018-04-18 15:38:59 -07003957 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern5e670d82018-04-17 17:33:14 -07003958 if (iter->fib6_nh.nh_dev == down_dev ||
3959 iter->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003960 dead++;
3961
3962 return dead;
3963}
3964
David Ahern8d1c8022018-04-17 17:33:26 -07003965static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02003966 const struct net_device *dev,
3967 unsigned int nh_flags)
3968{
David Ahern8d1c8022018-04-17 17:33:26 -07003969 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003970
David Ahern5e670d82018-04-17 17:33:14 -07003971 if (rt->fib6_nh.nh_dev == dev)
3972 rt->fib6_nh.nh_flags |= nh_flags;
David Ahern93c2fb22018-04-18 15:38:59 -07003973 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern5e670d82018-04-17 17:33:14 -07003974 if (iter->fib6_nh.nh_dev == dev)
3975 iter->fib6_nh.nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003976}
3977
David Aherna1a22c12017-01-18 07:40:36 -08003978/* called with write lock held for table with rt */
David Ahern8d1c8022018-04-17 17:33:26 -07003979static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980{
Ido Schimmel4c981e22018-01-07 12:45:04 +02003981 const struct arg_netdev_event *arg = p_arg;
3982 const struct net_device *dev = arg->dev;
David Ahern7aef6852018-04-17 17:33:10 -07003983 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003984
David Ahern421842e2018-04-17 17:33:18 -07003985 if (rt == net->ipv6.fib6_null_entry)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003986 return 0;
3987
3988 switch (arg->event) {
3989 case NETDEV_UNREGISTER:
David Ahern5e670d82018-04-17 17:33:14 -07003990 return rt->fib6_nh.nh_dev == dev ? -1 : 0;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003991 case NETDEV_DOWN:
Ido Schimmel1de178e2018-01-07 12:45:15 +02003992 if (rt->should_flush)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003993 return -1;
David Ahern93c2fb22018-04-18 15:38:59 -07003994 if (!rt->fib6_nsiblings)
David Ahern5e670d82018-04-17 17:33:14 -07003995 return rt->fib6_nh.nh_dev == dev ? -1 : 0;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003996 if (rt6_multipath_uses_dev(rt, dev)) {
3997 unsigned int count;
3998
3999 count = rt6_multipath_dead_count(rt, dev);
David Ahern93c2fb22018-04-18 15:38:59 -07004000 if (rt->fib6_nsiblings + 1 == count) {
Ido Schimmel1de178e2018-01-07 12:45:15 +02004001 rt6_multipath_flush(rt);
4002 return -1;
4003 }
4004 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4005 RTNH_F_LINKDOWN);
David Ahern7aef6852018-04-17 17:33:10 -07004006 fib6_update_sernum(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004007 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004008 }
4009 return -2;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004010 case NETDEV_CHANGE:
David Ahern5e670d82018-04-17 17:33:14 -07004011 if (rt->fib6_nh.nh_dev != dev ||
David Ahern93c2fb22018-04-18 15:38:59 -07004012 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004013 break;
David Ahern5e670d82018-04-17 17:33:14 -07004014 rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004015 rt6_multipath_rebalance(rt);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004016 break;
Ido Schimmel2b241362018-01-07 12:45:02 +02004017 }
David S. Millerc159d302011-12-26 15:24:36 -05004018
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 return 0;
4020}
4021
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004022void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004024 struct arg_netdev_event arg = {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004025 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004026 {
4027 .event = event,
4028 },
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004029 };
4030
Ido Schimmel4c981e22018-01-07 12:45:04 +02004031 fib6_clean_all(dev_net(dev), fib6_ifdown, &arg);
4032}
4033
4034void rt6_disable_ip(struct net_device *dev, unsigned long event)
4035{
4036 rt6_sync_down_dev(dev, event);
4037 rt6_uncached_list_flush_dev(dev_net(dev), dev);
4038 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039}
4040
Eric Dumazet95c96172012-04-15 05:58:06 +00004041struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00004043 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044};
4045
David Ahern8d1c8022018-04-17 17:33:26 -07004046static int rt6_mtu_change_route(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047{
4048 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4049 struct inet6_dev *idev;
4050
4051 /* In IPv6 pmtu discovery is not optional,
4052 so that RTAX_MTU lock cannot disable it.
4053 We still use this lock to block changes
4054 caused by addrconf/ndisc.
4055 */
4056
4057 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05004058 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 return 0;
4060
4061 /* For administrative MTU increase, there is no way to discover
4062 IPv6 PMTU increase, so PMTU increase should be updated here.
4063 Since RFC 1981 doesn't include administrative MTU increase
4064 update PMTU increase is a MUST. (i.e. jumbo frame)
4065 */
David Ahern5e670d82018-04-17 17:33:14 -07004066 if (rt->fib6_nh.nh_dev == arg->dev &&
David Ahernd4ead6b2018-04-17 17:33:16 -07004067 !fib6_metric_locked(rt, RTAX_MTU)) {
4068 u32 mtu = rt->fib6_pmtu;
4069
4070 if (mtu >= arg->mtu ||
4071 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4072 fib6_metric_set(rt, RTAX_MTU, arg->mtu);
4073
Wei Wangf5bbe7e2017-10-06 12:05:59 -07004074 spin_lock_bh(&rt6_exception_lock);
Stefano Brivioe9fa1492018-03-06 11:10:19 +01004075 rt6_exceptions_update_pmtu(idev, rt, arg->mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07004076 spin_unlock_bh(&rt6_exception_lock);
Simon Arlott566cfd82007-07-26 00:09:55 -07004077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 return 0;
4079}
4080
Eric Dumazet95c96172012-04-15 05:58:06 +00004081void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082{
Thomas Grafc71099a2006-08-04 23:20:06 -07004083 struct rt6_mtu_change_arg arg = {
4084 .dev = dev,
4085 .mtu = mtu,
4086 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087
Li RongQing0c3584d2013-12-27 16:32:38 +08004088 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089}
4090
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004091static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07004092 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004093 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07004094 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07004095 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004096 [RTA_PRIORITY] = { .type = NLA_U32 },
4097 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004098 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004099 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004100 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4101 [RTA_ENCAP] = { .type = NLA_NESTED },
Xin Long32bc2012015-12-16 17:50:11 +08004102 [RTA_EXPIRES] = { .type = NLA_U32 },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004103 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +08004104 [RTA_MARK] = { .type = NLA_U32 },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004105 [RTA_TABLE] = { .type = NLA_U32 },
Roopa Prabhueacb9382018-05-22 14:03:28 -07004106 [RTA_IP_PROTO] = { .type = NLA_U8 },
4107 [RTA_SPORT] = { .type = NLA_U16 },
4108 [RTA_DPORT] = { .type = NLA_U16 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004109};
4110
4111static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
David Ahern333c4302017-05-21 10:12:04 -06004112 struct fib6_config *cfg,
4113 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114{
Thomas Graf86872cb2006-08-22 00:01:08 -07004115 struct rtmsg *rtm;
4116 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004117 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07004118 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
Johannes Bergfceb6432017-04-12 14:34:07 +02004120 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
4121 NULL);
Thomas Graf86872cb2006-08-22 00:01:08 -07004122 if (err < 0)
4123 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
Thomas Graf86872cb2006-08-22 00:01:08 -07004125 err = -EINVAL;
4126 rtm = nlmsg_data(nlh);
Thomas Graf86872cb2006-08-22 00:01:08 -07004127
Maciej Żenczykowski84db8402018-09-29 23:44:53 -07004128 *cfg = (struct fib6_config){
4129 .fc_table = rtm->rtm_table,
4130 .fc_dst_len = rtm->rtm_dst_len,
4131 .fc_src_len = rtm->rtm_src_len,
4132 .fc_flags = RTF_UP,
4133 .fc_protocol = rtm->rtm_protocol,
4134 .fc_type = rtm->rtm_type,
4135
4136 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
4137 .fc_nlinfo.nlh = nlh,
4138 .fc_nlinfo.nl_net = sock_net(skb->sk),
4139 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004140
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004141 if (rtm->rtm_type == RTN_UNREACHABLE ||
4142 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00004143 rtm->rtm_type == RTN_PROHIBIT ||
4144 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07004145 cfg->fc_flags |= RTF_REJECT;
4146
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00004147 if (rtm->rtm_type == RTN_LOCAL)
4148 cfg->fc_flags |= RTF_LOCAL;
4149
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07004150 if (rtm->rtm_flags & RTM_F_CLONED)
4151 cfg->fc_flags |= RTF_CACHE;
4152
David Ahernfc1e64e2018-01-25 16:55:09 -08004153 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4154
Thomas Graf86872cb2006-08-22 00:01:08 -07004155 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004156 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07004157 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004159
4160 if (tb[RTA_DST]) {
4161 int plen = (rtm->rtm_dst_len + 7) >> 3;
4162
4163 if (nla_len(tb[RTA_DST]) < plen)
4164 goto errout;
4165
4166 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004168
4169 if (tb[RTA_SRC]) {
4170 int plen = (rtm->rtm_src_len + 7) >> 3;
4171
4172 if (nla_len(tb[RTA_SRC]) < plen)
4173 goto errout;
4174
4175 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004177
Daniel Walterc3968a82011-04-13 21:10:57 +00004178 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02004179 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00004180
Thomas Graf86872cb2006-08-22 00:01:08 -07004181 if (tb[RTA_OIF])
4182 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4183
4184 if (tb[RTA_PRIORITY])
4185 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4186
4187 if (tb[RTA_METRICS]) {
4188 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4189 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004191
4192 if (tb[RTA_TABLE])
4193 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4194
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004195 if (tb[RTA_MULTIPATH]) {
4196 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4197 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
David Ahern9ed59592017-01-17 14:57:36 -08004198
4199 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
David Ahernc255bd62017-05-27 16:19:27 -06004200 cfg->fc_mp_len, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004201 if (err < 0)
4202 goto errout;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004203 }
4204
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004205 if (tb[RTA_PREF]) {
4206 pref = nla_get_u8(tb[RTA_PREF]);
4207 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4208 pref != ICMPV6_ROUTER_PREF_HIGH)
4209 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4210 cfg->fc_flags |= RTF_PREF(pref);
4211 }
4212
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004213 if (tb[RTA_ENCAP])
4214 cfg->fc_encap = tb[RTA_ENCAP];
4215
David Ahern9ed59592017-01-17 14:57:36 -08004216 if (tb[RTA_ENCAP_TYPE]) {
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004217 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
4218
David Ahernc255bd62017-05-27 16:19:27 -06004219 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004220 if (err < 0)
4221 goto errout;
4222 }
4223
Xin Long32bc2012015-12-16 17:50:11 +08004224 if (tb[RTA_EXPIRES]) {
4225 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
4226
4227 if (addrconf_finite_timeout(timeout)) {
4228 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
4229 cfg->fc_flags |= RTF_EXPIRES;
4230 }
4231 }
4232
Thomas Graf86872cb2006-08-22 00:01:08 -07004233 err = 0;
4234errout:
4235 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236}
4237
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004238struct rt6_nh {
David Ahern8d1c8022018-04-17 17:33:26 -07004239 struct fib6_info *fib6_info;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004240 struct fib6_config r_cfg;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004241 struct list_head next;
4242};
4243
4244static void ip6_print_replace_route_err(struct list_head *rt6_nh_list)
4245{
4246 struct rt6_nh *nh;
4247
4248 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern7d4d5062017-02-02 12:37:12 -08004249 pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n",
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004250 &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway,
4251 nh->r_cfg.fc_ifindex);
4252 }
4253}
4254
David Ahernd4ead6b2018-04-17 17:33:16 -07004255static int ip6_route_info_append(struct net *net,
4256 struct list_head *rt6_nh_list,
David Ahern8d1c8022018-04-17 17:33:26 -07004257 struct fib6_info *rt,
4258 struct fib6_config *r_cfg)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004259{
4260 struct rt6_nh *nh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004261 int err = -EEXIST;
4262
4263 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004264 /* check if fib6_info already exists */
4265 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004266 return err;
4267 }
4268
4269 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
4270 if (!nh)
4271 return -ENOMEM;
David Ahern8d1c8022018-04-17 17:33:26 -07004272 nh->fib6_info = rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004273 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
4274 list_add_tail(&nh->next, rt6_nh_list);
4275
4276 return 0;
4277}
4278
David Ahern8d1c8022018-04-17 17:33:26 -07004279static void ip6_route_mpath_notify(struct fib6_info *rt,
4280 struct fib6_info *rt_last,
David Ahern3b1137f2017-02-02 12:37:10 -08004281 struct nl_info *info,
4282 __u16 nlflags)
4283{
4284 /* if this is an APPEND route, then rt points to the first route
4285 * inserted and rt_last points to last route inserted. Userspace
4286 * wants a consistent dump of the route which starts at the first
4287 * nexthop. Since sibling routes are always added at the end of
4288 * the list, find the first sibling of the last route appended
4289 */
David Ahern93c2fb22018-04-18 15:38:59 -07004290 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
4291 rt = list_first_entry(&rt_last->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -07004292 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -07004293 fib6_siblings);
David Ahern3b1137f2017-02-02 12:37:10 -08004294 }
4295
4296 if (rt)
4297 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
4298}
4299
David Ahern333c4302017-05-21 10:12:04 -06004300static int ip6_route_multipath_add(struct fib6_config *cfg,
4301 struct netlink_ext_ack *extack)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004302{
David Ahern8d1c8022018-04-17 17:33:26 -07004303 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
David Ahern3b1137f2017-02-02 12:37:10 -08004304 struct nl_info *info = &cfg->fc_nlinfo;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004305 struct fib6_config r_cfg;
4306 struct rtnexthop *rtnh;
David Ahern8d1c8022018-04-17 17:33:26 -07004307 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004308 struct rt6_nh *err_nh;
4309 struct rt6_nh *nh, *nh_safe;
David Ahern3b1137f2017-02-02 12:37:10 -08004310 __u16 nlflags;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004311 int remaining;
4312 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004313 int err = 1;
4314 int nhn = 0;
4315 int replace = (cfg->fc_nlinfo.nlh &&
4316 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
4317 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004318
David Ahern3b1137f2017-02-02 12:37:10 -08004319 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
4320 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
4321 nlflags |= NLM_F_APPEND;
4322
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02004323 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004324 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004325
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004326 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
David Ahern8d1c8022018-04-17 17:33:26 -07004327 * fib6_info structs per nexthop
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004328 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004329 while (rtnh_ok(rtnh, remaining)) {
4330 memcpy(&r_cfg, cfg, sizeof(*cfg));
4331 if (rtnh->rtnh_ifindex)
4332 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
4333
4334 attrlen = rtnh_attrlen(rtnh);
4335 if (attrlen > 0) {
4336 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4337
4338 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
4339 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004340 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004341 r_cfg.fc_flags |= RTF_GATEWAY;
4342 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004343 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
4344 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
4345 if (nla)
4346 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004347 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004348
David Ahern68e2ffd2018-03-20 10:06:59 -07004349 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
David Ahernacb54e32018-04-17 17:33:22 -07004350 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07004351 if (IS_ERR(rt)) {
4352 err = PTR_ERR(rt);
4353 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004354 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07004355 }
David Ahernb5d2d752018-07-15 09:35:19 -07004356 if (!rt6_qualify_for_ecmp(rt)) {
4357 err = -EINVAL;
4358 NL_SET_ERR_MSG(extack,
4359 "Device only routes can not be added for IPv6 using the multipath API.");
4360 fib6_info_release(rt);
4361 goto cleanup;
4362 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004363
David Ahern5e670d82018-04-17 17:33:14 -07004364 rt->fib6_nh.nh_weight = rtnh->rtnh_hops + 1;
Ido Schimmel398958a2018-01-09 16:40:28 +02004365
David Ahernd4ead6b2018-04-17 17:33:16 -07004366 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4367 rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004368 if (err) {
David Ahern93531c62018-04-17 17:33:25 -07004369 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004370 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004371 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004372
4373 rtnh = rtnh_next(rtnh, &remaining);
4374 }
4375
David Ahern3b1137f2017-02-02 12:37:10 -08004376 /* for add and replace send one notification with all nexthops.
4377 * Skip the notification in fib6_add_rt2node and send one with
4378 * the full route when done
4379 */
4380 info->skip_notify = 1;
4381
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004382 err_nh = NULL;
4383 list_for_each_entry(nh, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004384 err = __ip6_ins_rt(nh->fib6_info, info, extack);
4385 fib6_info_release(nh->fib6_info);
David Ahern3b1137f2017-02-02 12:37:10 -08004386
David Ahernf7225172018-06-04 13:41:42 -07004387 if (!err) {
4388 /* save reference to last route successfully inserted */
4389 rt_last = nh->fib6_info;
4390
4391 /* save reference to first route for notification */
4392 if (!rt_notif)
4393 rt_notif = nh->fib6_info;
4394 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004395
David Ahern8d1c8022018-04-17 17:33:26 -07004396 /* nh->fib6_info is used or freed at this point, reset to NULL*/
4397 nh->fib6_info = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004398 if (err) {
4399 if (replace && nhn)
4400 ip6_print_replace_route_err(&rt6_nh_list);
4401 err_nh = nh;
4402 goto add_errout;
4403 }
4404
Nicolas Dichtel1a724182012-11-01 22:58:22 +00004405 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02004406 * these flags after the first nexthop: if there is a collision,
4407 * we have already failed to add the first nexthop:
4408 * fib6_add_rt2node() has rejected it; when replacing, old
4409 * nexthops have been replaced by first new, the rest should
4410 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00004411 */
Michal Kubeček27596472015-05-18 20:54:00 +02004412 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
4413 NLM_F_REPLACE);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004414 nhn++;
4415 }
4416
David Ahern3b1137f2017-02-02 12:37:10 -08004417 /* success ... tell user about new route */
4418 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004419 goto cleanup;
4420
4421add_errout:
David Ahern3b1137f2017-02-02 12:37:10 -08004422 /* send notification for routes that were added so that
4423 * the delete notifications sent by ip6_route_del are
4424 * coherent
4425 */
4426 if (rt_notif)
4427 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
4428
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004429 /* Delete routes that were already added */
4430 list_for_each_entry(nh, &rt6_nh_list, next) {
4431 if (err_nh == nh)
4432 break;
David Ahern333c4302017-05-21 10:12:04 -06004433 ip6_route_del(&nh->r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004434 }
4435
4436cleanup:
4437 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004438 if (nh->fib6_info)
4439 fib6_info_release(nh->fib6_info);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004440 list_del(&nh->next);
4441 kfree(nh);
4442 }
4443
4444 return err;
4445}
4446
David Ahern333c4302017-05-21 10:12:04 -06004447static int ip6_route_multipath_del(struct fib6_config *cfg,
4448 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004449{
4450 struct fib6_config r_cfg;
4451 struct rtnexthop *rtnh;
4452 int remaining;
4453 int attrlen;
4454 int err = 1, last_err = 0;
4455
4456 remaining = cfg->fc_mp_len;
4457 rtnh = (struct rtnexthop *)cfg->fc_mp;
4458
4459 /* Parse a Multipath Entry */
4460 while (rtnh_ok(rtnh, remaining)) {
4461 memcpy(&r_cfg, cfg, sizeof(*cfg));
4462 if (rtnh->rtnh_ifindex)
4463 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
4464
4465 attrlen = rtnh_attrlen(rtnh);
4466 if (attrlen > 0) {
4467 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4468
4469 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
4470 if (nla) {
4471 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
4472 r_cfg.fc_flags |= RTF_GATEWAY;
4473 }
4474 }
David Ahern333c4302017-05-21 10:12:04 -06004475 err = ip6_route_del(&r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004476 if (err)
4477 last_err = err;
4478
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004479 rtnh = rtnh_next(rtnh, &remaining);
4480 }
4481
4482 return last_err;
4483}
4484
David Ahernc21ef3e2017-04-16 09:48:24 -07004485static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4486 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487{
Thomas Graf86872cb2006-08-22 00:01:08 -07004488 struct fib6_config cfg;
4489 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
David Ahern333c4302017-05-21 10:12:04 -06004491 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004492 if (err < 0)
4493 return err;
4494
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004495 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06004496 return ip6_route_multipath_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08004497 else {
4498 cfg.fc_delete_all_nh = 1;
David Ahern333c4302017-05-21 10:12:04 -06004499 return ip6_route_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08004500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501}
4502
David Ahernc21ef3e2017-04-16 09:48:24 -07004503static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4504 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505{
Thomas Graf86872cb2006-08-22 00:01:08 -07004506 struct fib6_config cfg;
4507 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
David Ahern333c4302017-05-21 10:12:04 -06004509 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004510 if (err < 0)
4511 return err;
4512
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004513 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06004514 return ip6_route_multipath_add(&cfg, extack);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004515 else
David Ahernacb54e32018-04-17 17:33:22 -07004516 return ip6_route_add(&cfg, GFP_KERNEL, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517}
4518
David Ahern8d1c8022018-04-17 17:33:26 -07004519static size_t rt6_nlmsg_size(struct fib6_info *rt)
Thomas Graf339bf982006-11-10 14:10:15 -08004520{
David Ahernbeb1afac52017-02-02 12:37:09 -08004521 int nexthop_len = 0;
4522
David Ahern93c2fb22018-04-18 15:38:59 -07004523 if (rt->fib6_nsiblings) {
David Ahernbeb1afac52017-02-02 12:37:09 -08004524 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
4525 + NLA_ALIGN(sizeof(struct rtnexthop))
4526 + nla_total_size(16) /* RTA_GATEWAY */
David Ahern5e670d82018-04-17 17:33:14 -07004527 + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate);
David Ahernbeb1afac52017-02-02 12:37:09 -08004528
David Ahern93c2fb22018-04-18 15:38:59 -07004529 nexthop_len *= rt->fib6_nsiblings;
David Ahernbeb1afac52017-02-02 12:37:09 -08004530 }
4531
Thomas Graf339bf982006-11-10 14:10:15 -08004532 return NLMSG_ALIGN(sizeof(struct rtmsg))
4533 + nla_total_size(16) /* RTA_SRC */
4534 + nla_total_size(16) /* RTA_DST */
4535 + nla_total_size(16) /* RTA_GATEWAY */
4536 + nla_total_size(16) /* RTA_PREFSRC */
4537 + nla_total_size(4) /* RTA_TABLE */
4538 + nla_total_size(4) /* RTA_IIF */
4539 + nla_total_size(4) /* RTA_OIF */
4540 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08004541 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01004542 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004543 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004544 + nla_total_size(1) /* RTA_PREF */
David Ahern5e670d82018-04-17 17:33:14 -07004545 + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate)
David Ahernbeb1afac52017-02-02 12:37:09 -08004546 + nexthop_len;
4547}
4548
David Ahern8d1c8022018-04-17 17:33:26 -07004549static int rt6_nexthop_info(struct sk_buff *skb, struct fib6_info *rt,
David Ahern5be083c2017-03-06 15:57:31 -08004550 unsigned int *flags, bool skip_oif)
David Ahernbeb1afac52017-02-02 12:37:09 -08004551{
David Ahern5e670d82018-04-17 17:33:14 -07004552 if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmelf9d882e2018-01-07 12:45:10 +02004553 *flags |= RTNH_F_DEAD;
4554
David Ahern5e670d82018-04-17 17:33:14 -07004555 if (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN) {
David Ahernbeb1afac52017-02-02 12:37:09 -08004556 *flags |= RTNH_F_LINKDOWN;
David Aherndcd1f572018-04-18 15:39:05 -07004557
4558 rcu_read_lock();
4559 if (fib6_ignore_linkdown(rt))
David Ahernbeb1afac52017-02-02 12:37:09 -08004560 *flags |= RTNH_F_DEAD;
David Aherndcd1f572018-04-18 15:39:05 -07004561 rcu_read_unlock();
David Ahernbeb1afac52017-02-02 12:37:09 -08004562 }
4563
David Ahern93c2fb22018-04-18 15:38:59 -07004564 if (rt->fib6_flags & RTF_GATEWAY) {
David Ahern5e670d82018-04-17 17:33:14 -07004565 if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->fib6_nh.nh_gw) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004566 goto nla_put_failure;
4567 }
4568
David Ahern5e670d82018-04-17 17:33:14 -07004569 *flags |= (rt->fib6_nh.nh_flags & RTNH_F_ONLINK);
4570 if (rt->fib6_nh.nh_flags & RTNH_F_OFFLOAD)
Ido Schimmel61e4d012017-08-03 13:28:20 +02004571 *flags |= RTNH_F_OFFLOAD;
4572
David Ahern5be083c2017-03-06 15:57:31 -08004573 /* not needed for multipath encoding b/c it has a rtnexthop struct */
David Ahern5e670d82018-04-17 17:33:14 -07004574 if (!skip_oif && rt->fib6_nh.nh_dev &&
4575 nla_put_u32(skb, RTA_OIF, rt->fib6_nh.nh_dev->ifindex))
David Ahernbeb1afac52017-02-02 12:37:09 -08004576 goto nla_put_failure;
4577
David Ahern5e670d82018-04-17 17:33:14 -07004578 if (rt->fib6_nh.nh_lwtstate &&
4579 lwtunnel_fill_encap(skb, rt->fib6_nh.nh_lwtstate) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004580 goto nla_put_failure;
4581
4582 return 0;
4583
4584nla_put_failure:
4585 return -EMSGSIZE;
4586}
4587
David Ahern5be083c2017-03-06 15:57:31 -08004588/* add multipath next hop */
David Ahern8d1c8022018-04-17 17:33:26 -07004589static int rt6_add_nexthop(struct sk_buff *skb, struct fib6_info *rt)
David Ahernbeb1afac52017-02-02 12:37:09 -08004590{
David Ahern5e670d82018-04-17 17:33:14 -07004591 const struct net_device *dev = rt->fib6_nh.nh_dev;
David Ahernbeb1afac52017-02-02 12:37:09 -08004592 struct rtnexthop *rtnh;
4593 unsigned int flags = 0;
4594
4595 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
4596 if (!rtnh)
4597 goto nla_put_failure;
4598
David Ahern5e670d82018-04-17 17:33:14 -07004599 rtnh->rtnh_hops = rt->fib6_nh.nh_weight - 1;
4600 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
David Ahernbeb1afac52017-02-02 12:37:09 -08004601
David Ahern5be083c2017-03-06 15:57:31 -08004602 if (rt6_nexthop_info(skb, rt, &flags, true) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004603 goto nla_put_failure;
4604
4605 rtnh->rtnh_flags = flags;
4606
4607 /* length of rtnetlink header + attributes */
4608 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
4609
4610 return 0;
4611
4612nla_put_failure:
4613 return -EMSGSIZE;
Thomas Graf339bf982006-11-10 14:10:15 -08004614}
4615
David Ahernd4ead6b2018-04-17 17:33:16 -07004616static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -07004617 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -07004618 struct in6_addr *dest, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004619 int iif, int type, u32 portid, u32 seq,
David Ahernf8cfe2c2017-01-17 15:51:08 -08004620 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621{
Xin Long22d0bd82018-09-11 14:33:58 +08004622 struct rt6_info *rt6 = (struct rt6_info *)dst;
4623 struct rt6key *rt6_dst, *rt6_src;
4624 u32 *pmetrics, table, rt6_flags;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004625 struct nlmsghdr *nlh;
Xin Long22d0bd82018-09-11 14:33:58 +08004626 struct rtmsg *rtm;
David Ahernd4ead6b2018-04-17 17:33:16 -07004627 long expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628
Eric W. Biederman15e47302012-09-07 20:12:54 +00004629 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05004630 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004631 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004632
Xin Long22d0bd82018-09-11 14:33:58 +08004633 if (rt6) {
4634 rt6_dst = &rt6->rt6i_dst;
4635 rt6_src = &rt6->rt6i_src;
4636 rt6_flags = rt6->rt6i_flags;
4637 } else {
4638 rt6_dst = &rt->fib6_dst;
4639 rt6_src = &rt->fib6_src;
4640 rt6_flags = rt->fib6_flags;
4641 }
4642
Thomas Graf2d7202b2006-08-22 00:01:27 -07004643 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 rtm->rtm_family = AF_INET6;
Xin Long22d0bd82018-09-11 14:33:58 +08004645 rtm->rtm_dst_len = rt6_dst->plen;
4646 rtm->rtm_src_len = rt6_src->plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 rtm->rtm_tos = 0;
David Ahern93c2fb22018-04-18 15:38:59 -07004648 if (rt->fib6_table)
4649 table = rt->fib6_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07004650 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07004651 table = RT6_TABLE_UNSPEC;
4652 rtm->rtm_table = table;
David S. Millerc78679e2012-04-01 20:27:33 -04004653 if (nla_put_u32(skb, RTA_TABLE, table))
4654 goto nla_put_failure;
David Aherne8478e82018-04-17 17:33:13 -07004655
4656 rtm->rtm_type = rt->fib6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 rtm->rtm_flags = 0;
4658 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
David Ahern93c2fb22018-04-18 15:38:59 -07004659 rtm->rtm_protocol = rt->fib6_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
Xin Long22d0bd82018-09-11 14:33:58 +08004661 if (rt6_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 rtm->rtm_flags |= RTM_F_CLONED;
4663
David Ahernd4ead6b2018-04-17 17:33:16 -07004664 if (dest) {
4665 if (nla_put_in6_addr(skb, RTA_DST, dest))
David S. Millerc78679e2012-04-01 20:27:33 -04004666 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004667 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 } else if (rtm->rtm_dst_len)
Xin Long22d0bd82018-09-11 14:33:58 +08004669 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04004670 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671#ifdef CONFIG_IPV6_SUBTREES
4672 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02004673 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04004674 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004675 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04004676 } else if (rtm->rtm_src_len &&
Xin Long22d0bd82018-09-11 14:33:58 +08004677 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04004678 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004680 if (iif) {
4681#ifdef CONFIG_IPV6_MROUTE
Xin Long22d0bd82018-09-11 14:33:58 +08004682 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
David Ahernfd61c6b2017-01-17 15:51:07 -08004683 int err = ip6mr_get_route(net, skb, rtm, portid);
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02004684
David Ahernfd61c6b2017-01-17 15:51:07 -08004685 if (err == 0)
4686 return 0;
4687 if (err < 0)
4688 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004689 } else
4690#endif
David S. Millerc78679e2012-04-01 20:27:33 -04004691 if (nla_put_u32(skb, RTA_IIF, iif))
4692 goto nla_put_failure;
David Ahernd4ead6b2018-04-17 17:33:16 -07004693 } else if (dest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 struct in6_addr saddr_buf;
David Ahernd4ead6b2018-04-17 17:33:16 -07004695 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02004696 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04004697 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07004699
David Ahern93c2fb22018-04-18 15:38:59 -07004700 if (rt->fib6_prefsrc.plen) {
Daniel Walterc3968a82011-04-13 21:10:57 +00004701 struct in6_addr saddr_buf;
David Ahern93c2fb22018-04-18 15:38:59 -07004702 saddr_buf = rt->fib6_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02004703 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04004704 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00004705 }
4706
David Ahernd4ead6b2018-04-17 17:33:16 -07004707 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
4708 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07004709 goto nla_put_failure;
4710
David Ahern93c2fb22018-04-18 15:38:59 -07004711 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
David S. Millerc78679e2012-04-01 20:27:33 -04004712 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00004713
David Ahernbeb1afac52017-02-02 12:37:09 -08004714 /* For multipath routes, walk the siblings list and add
4715 * each as a nexthop within RTA_MULTIPATH.
4716 */
Xin Long22d0bd82018-09-11 14:33:58 +08004717 if (rt6) {
4718 if (rt6_flags & RTF_GATEWAY &&
4719 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
4720 goto nla_put_failure;
4721
4722 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
4723 goto nla_put_failure;
4724 } else if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07004725 struct fib6_info *sibling, *next_sibling;
David Ahernbeb1afac52017-02-02 12:37:09 -08004726 struct nlattr *mp;
4727
4728 mp = nla_nest_start(skb, RTA_MULTIPATH);
4729 if (!mp)
4730 goto nla_put_failure;
4731
4732 if (rt6_add_nexthop(skb, rt) < 0)
4733 goto nla_put_failure;
4734
4735 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07004736 &rt->fib6_siblings, fib6_siblings) {
David Ahernbeb1afac52017-02-02 12:37:09 -08004737 if (rt6_add_nexthop(skb, sibling) < 0)
4738 goto nla_put_failure;
4739 }
4740
4741 nla_nest_end(skb, mp);
4742 } else {
David Ahern5be083c2017-03-06 15:57:31 -08004743 if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004744 goto nla_put_failure;
4745 }
4746
Xin Long22d0bd82018-09-11 14:33:58 +08004747 if (rt6_flags & RTF_EXPIRES) {
David Ahern14895682018-04-17 17:33:17 -07004748 expires = dst ? dst->expires : rt->expires;
4749 expires -= jiffies;
4750 }
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07004751
David Ahernd4ead6b2018-04-17 17:33:16 -07004752 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08004753 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
Xin Long22d0bd82018-09-11 14:33:58 +08004755 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004756 goto nla_put_failure;
4757
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004758
Johannes Berg053c0952015-01-16 22:09:00 +01004759 nlmsg_end(skb, nlh);
4760 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004761
4762nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004763 nlmsg_cancel(skb, nlh);
4764 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765}
4766
David Ahern8d1c8022018-04-17 17:33:26 -07004767int rt6_dump_route(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768{
4769 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
David Ahern1f17e2f2017-01-26 13:54:08 -08004770 struct net *net = arg->net;
4771
David Ahern421842e2018-04-17 17:33:18 -07004772 if (rt == net->ipv6.fib6_null_entry)
David Ahern1f17e2f2017-01-26 13:54:08 -08004773 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Thomas Graf2d7202b2006-08-22 00:01:27 -07004775 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
4776 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
David Ahernf8cfe2c2017-01-17 15:51:08 -08004777
4778 /* user wants prefix routes only */
4779 if (rtm->rtm_flags & RTM_F_PREFIX &&
David Ahern93c2fb22018-04-18 15:38:59 -07004780 !(rt->fib6_flags & RTF_PREFIX_RT)) {
David Ahernf8cfe2c2017-01-17 15:51:08 -08004781 /* success since this is not a prefix route */
4782 return 1;
4783 }
4784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
David Ahernd4ead6b2018-04-17 17:33:16 -07004786 return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
4787 RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
4788 arg->cb->nlh->nlmsg_seq, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789}
4790
David Ahernc21ef3e2017-04-16 09:48:24 -07004791static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4792 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004794 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07004795 struct nlattr *tb[RTA_MAX+1];
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004796 int err, iif = 0, oif = 0;
David Aherna68886a2018-04-20 15:38:02 -07004797 struct fib6_info *from;
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004798 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07004800 struct sk_buff *skb;
4801 struct rtmsg *rtm;
Maciej Żenczykowski744486d2018-09-29 23:44:54 -07004802 struct flowi6 fl6 = {};
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004803 bool fibmatch;
Thomas Grafab364a62006-08-22 00:01:47 -07004804
Johannes Bergfceb6432017-04-12 14:34:07 +02004805 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07004806 extack);
Thomas Grafab364a62006-08-22 00:01:47 -07004807 if (err < 0)
4808 goto errout;
4809
4810 err = -EINVAL;
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02004811 rtm = nlmsg_data(nlh);
4812 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004813 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
Thomas Grafab364a62006-08-22 00:01:47 -07004814
4815 if (tb[RTA_SRC]) {
4816 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4817 goto errout;
4818
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004819 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07004820 }
4821
4822 if (tb[RTA_DST]) {
4823 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4824 goto errout;
4825
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004826 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07004827 }
4828
4829 if (tb[RTA_IIF])
4830 iif = nla_get_u32(tb[RTA_IIF]);
4831
4832 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004833 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07004834
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07004835 if (tb[RTA_MARK])
4836 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
4837
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004838 if (tb[RTA_UID])
4839 fl6.flowi6_uid = make_kuid(current_user_ns(),
4840 nla_get_u32(tb[RTA_UID]));
4841 else
4842 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
4843
Roopa Prabhueacb9382018-05-22 14:03:28 -07004844 if (tb[RTA_SPORT])
4845 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
4846
4847 if (tb[RTA_DPORT])
4848 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
4849
4850 if (tb[RTA_IP_PROTO]) {
4851 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
4852 &fl6.flowi6_proto, extack);
4853 if (err)
4854 goto errout;
4855 }
4856
Thomas Grafab364a62006-08-22 00:01:47 -07004857 if (iif) {
4858 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004859 int flags = 0;
4860
Florian Westphal121622d2017-08-15 16:34:42 +02004861 rcu_read_lock();
4862
4863 dev = dev_get_by_index_rcu(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07004864 if (!dev) {
Florian Westphal121622d2017-08-15 16:34:42 +02004865 rcu_read_unlock();
Thomas Grafab364a62006-08-22 00:01:47 -07004866 err = -ENODEV;
4867 goto errout;
4868 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004869
4870 fl6.flowi6_iif = iif;
4871
4872 if (!ipv6_addr_any(&fl6.saddr))
4873 flags |= RT6_LOOKUP_F_HAS_SADDR;
4874
David Ahernb75cc8f2018-03-02 08:32:17 -08004875 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
Florian Westphal121622d2017-08-15 16:34:42 +02004876
4877 rcu_read_unlock();
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004878 } else {
4879 fl6.flowi6_oif = oif;
4880
Ido Schimmel58acfd72017-12-20 12:28:25 +02004881 dst = ip6_route_output(net, NULL, &fl6);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004882 }
4883
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004884
4885 rt = container_of(dst, struct rt6_info, dst);
4886 if (rt->dst.error) {
4887 err = rt->dst.error;
4888 ip6_rt_put(rt);
4889 goto errout;
Thomas Grafab364a62006-08-22 00:01:47 -07004890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891
WANG Cong9d6acb32017-03-01 20:48:39 -08004892 if (rt == net->ipv6.ip6_null_entry) {
4893 err = rt->dst.error;
4894 ip6_rt_put(rt);
4895 goto errout;
4896 }
4897
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05004899 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00004900 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07004901 err = -ENOBUFS;
4902 goto errout;
4903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
Changli Gaod8d1f302010-06-10 23:31:35 -07004905 skb_dst_set(skb, &rt->dst);
David Aherna68886a2018-04-20 15:38:02 -07004906
4907 rcu_read_lock();
4908 from = rcu_dereference(rt->from);
4909
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004910 if (fibmatch)
David Aherna68886a2018-04-20 15:38:02 -07004911 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, iif,
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004912 RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
4913 nlh->nlmsg_seq, 0);
4914 else
David Aherna68886a2018-04-20 15:38:02 -07004915 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
4916 &fl6.saddr, iif, RTM_NEWROUTE,
David Ahernd4ead6b2018-04-17 17:33:16 -07004917 NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
4918 0);
David Aherna68886a2018-04-20 15:38:02 -07004919 rcu_read_unlock();
4920
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07004922 kfree_skb(skb);
4923 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 }
4925
Eric W. Biederman15e47302012-09-07 20:12:54 +00004926 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07004927errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929}
4930
David Ahern8d1c8022018-04-17 17:33:26 -07004931void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07004932 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933{
4934 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08004935 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08004936 u32 seq;
4937 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08004939 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05004940 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07004941
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004942 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05004943 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07004944 goto errout;
4945
David Ahernd4ead6b2018-04-17 17:33:16 -07004946 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
4947 event, info->portid, seq, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08004948 if (err < 0) {
4949 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
4950 WARN_ON(err == -EMSGSIZE);
4951 kfree_skb(skb);
4952 goto errout;
4953 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004954 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004955 info->nlh, gfp_any());
4956 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07004957errout:
4958 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08004959 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960}
4961
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004962static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00004963 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004964{
Jiri Pirko351638e2013-05-28 01:30:21 +00004965 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004966 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004967
WANG Cong242d3a42017-05-08 10:12:13 -07004968 if (!(dev->flags & IFF_LOOPBACK))
4969 return NOTIFY_OK;
4970
4971 if (event == NETDEV_REGISTER) {
David Ahern421842e2018-04-17 17:33:18 -07004972 net->ipv6.fib6_null_entry->fib6_nh.nh_dev = dev;
Changli Gaod8d1f302010-06-10 23:31:35 -07004973 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004974 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
4975#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07004976 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004977 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07004978 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004979 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
4980#endif
WANG Cong76da0702017-06-20 11:42:27 -07004981 } else if (event == NETDEV_UNREGISTER &&
4982 dev->reg_state != NETREG_UNREGISTERED) {
4983 /* NETDEV_UNREGISTER could be fired for multiple times by
4984 * netdev_wait_allrefs(). Make sure we only call this once.
4985 */
Eric Dumazet12d94a82017-08-15 04:09:51 -07004986 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07004987#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric Dumazet12d94a82017-08-15 04:09:51 -07004988 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
4989 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07004990#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004991 }
4992
4993 return NOTIFY_OK;
4994}
4995
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996/*
4997 * /proc
4998 */
4999
5000#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001static int rt6_stats_seq_show(struct seq_file *seq, void *v)
5002{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005003 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005005 net->ipv6.rt6_stats->fib_nodes,
5006 net->ipv6.rt6_stats->fib_route_nodes,
Wei Wang81eb8442017-10-06 12:06:11 -07005007 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005008 net->ipv6.rt6_stats->fib_rt_entries,
5009 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00005010 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005011 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012
5013 return 0;
5014}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015#endif /* CONFIG_PROC_FS */
5016
5017#ifdef CONFIG_SYSCTL
5018
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005020int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 void __user *buffer, size_t *lenp, loff_t *ppos)
5022{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005023 struct net *net;
5024 int delay;
5025 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005027
5028 net = (struct net *)ctl->extra1;
5029 delay = net->ipv6.sysctl.flush_delay;
5030 proc_dointvec(ctl, write, buffer, lenp, ppos);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02005031 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033}
5034
Joe Perchesfe2c6332013-06-11 23:04:25 -07005035struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005036 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08005038 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07005040 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005041 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 },
5043 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08005045 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 .maxlen = sizeof(int),
5047 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005048 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 },
5050 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08005052 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 .maxlen = sizeof(int),
5054 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005055 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 },
5057 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08005059 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 .maxlen = sizeof(int),
5061 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005062 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 },
5064 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08005066 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 .maxlen = sizeof(int),
5068 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005069 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 },
5071 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08005073 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 .maxlen = sizeof(int),
5075 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005076 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 },
5078 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08005080 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 .maxlen = sizeof(int),
5082 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07005083 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 },
5085 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08005087 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 .maxlen = sizeof(int),
5089 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005090 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 },
5092 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08005094 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 .maxlen = sizeof(int),
5096 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07005097 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 },
5099 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08005101 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 .maxlen = sizeof(int),
5103 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005104 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08005106 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107};
5108
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005109struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08005110{
5111 struct ctl_table *table;
5112
5113 table = kmemdup(ipv6_route_table_template,
5114 sizeof(ipv6_route_table_template),
5115 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005116
5117 if (table) {
5118 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005119 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00005120 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005121 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
5122 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
5123 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
5124 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
5125 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
5126 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
5127 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08005128 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
Eric W. Biederman464dc802012-11-16 03:02:59 +00005129
5130 /* Don't export sysctls to unprivileged users */
5131 if (net->user_ns != &init_user_ns)
5132 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005133 }
5134
Daniel Lezcano760f2d02008-01-10 02:53:43 -08005135 return table;
5136}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137#endif
5138
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005139static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005140{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07005141 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005142
Alexey Dobriyan86393e52009-08-29 01:34:49 +00005143 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
5144 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005145
Eric Dumazetfc66f952010-10-08 06:37:34 +00005146 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5147 goto out_ip6_dst_ops;
5148
David Ahern421842e2018-04-17 17:33:18 -07005149 net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template,
5150 sizeof(*net->ipv6.fib6_null_entry),
5151 GFP_KERNEL);
5152 if (!net->ipv6.fib6_null_entry)
5153 goto out_ip6_dst_entries;
5154
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005155 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
5156 sizeof(*net->ipv6.ip6_null_entry),
5157 GFP_KERNEL);
5158 if (!net->ipv6.ip6_null_entry)
David Ahern421842e2018-04-17 17:33:18 -07005159 goto out_fib6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005160 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005161 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
5162 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005163
5164#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Vincent Bernatfeca7d82017-08-08 20:23:49 +02005165 net->ipv6.fib6_has_custom_rules = false;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005166 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
5167 sizeof(*net->ipv6.ip6_prohibit_entry),
5168 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005169 if (!net->ipv6.ip6_prohibit_entry)
5170 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005171 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005172 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
5173 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005174
5175 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
5176 sizeof(*net->ipv6.ip6_blk_hole_entry),
5177 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005178 if (!net->ipv6.ip6_blk_hole_entry)
5179 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005180 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005181 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
5182 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005183#endif
5184
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07005185 net->ipv6.sysctl.flush_delay = 0;
5186 net->ipv6.sysctl.ip6_rt_max_size = 4096;
5187 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5188 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5189 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5190 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5191 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5192 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
5193
Benjamin Thery6891a342008-03-04 13:49:47 -08005194 net->ipv6.ip6_rt_gc_expire = 30*HZ;
5195
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005196 ret = 0;
5197out:
5198 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005199
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005200#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5201out_ip6_prohibit_entry:
5202 kfree(net->ipv6.ip6_prohibit_entry);
5203out_ip6_null_entry:
5204 kfree(net->ipv6.ip6_null_entry);
5205#endif
David Ahern421842e2018-04-17 17:33:18 -07005206out_fib6_null_entry:
5207 kfree(net->ipv6.fib6_null_entry);
Eric Dumazetfc66f952010-10-08 06:37:34 +00005208out_ip6_dst_entries:
5209 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005210out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005211 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005212}
5213
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005214static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005215{
David Ahern421842e2018-04-17 17:33:18 -07005216 kfree(net->ipv6.fib6_null_entry);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005217 kfree(net->ipv6.ip6_null_entry);
5218#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5219 kfree(net->ipv6.ip6_prohibit_entry);
5220 kfree(net->ipv6.ip6_blk_hole_entry);
5221#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00005222 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005223}
5224
Thomas Grafd1896342012-06-18 12:08:33 +00005225static int __net_init ip6_route_net_init_late(struct net *net)
5226{
5227#ifdef CONFIG_PROC_FS
Christoph Hellwigc3506372018-04-10 19:42:55 +02005228 proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
5229 sizeof(struct ipv6_route_iter));
Christoph Hellwig3617d942018-04-13 20:38:35 +02005230 proc_create_net_single("rt6_stats", 0444, net->proc_net,
5231 rt6_stats_seq_show, NULL);
Thomas Grafd1896342012-06-18 12:08:33 +00005232#endif
5233 return 0;
5234}
5235
5236static void __net_exit ip6_route_net_exit_late(struct net *net)
5237{
5238#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00005239 remove_proc_entry("ipv6_route", net->proc_net);
5240 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00005241#endif
5242}
5243
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005244static struct pernet_operations ip6_route_net_ops = {
5245 .init = ip6_route_net_init,
5246 .exit = ip6_route_net_exit,
5247};
5248
David S. Millerc3426b42012-06-09 16:27:05 -07005249static int __net_init ipv6_inetpeer_init(struct net *net)
5250{
5251 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5252
5253 if (!bp)
5254 return -ENOMEM;
5255 inet_peer_base_init(bp);
5256 net->ipv6.peers = bp;
5257 return 0;
5258}
5259
5260static void __net_exit ipv6_inetpeer_exit(struct net *net)
5261{
5262 struct inet_peer_base *bp = net->ipv6.peers;
5263
5264 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07005265 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07005266 kfree(bp);
5267}
5268
David S. Miller2b823f72012-06-09 19:00:16 -07005269static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07005270 .init = ipv6_inetpeer_init,
5271 .exit = ipv6_inetpeer_exit,
5272};
5273
Thomas Grafd1896342012-06-18 12:08:33 +00005274static struct pernet_operations ip6_route_net_late_ops = {
5275 .init = ip6_route_net_init_late,
5276 .exit = ip6_route_net_exit_late,
5277};
5278
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005279static struct notifier_block ip6_route_dev_notifier = {
5280 .notifier_call = ip6_route_dev_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07005281 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005282};
5283
WANG Cong2f460932017-05-03 22:07:31 -07005284void __init ip6_route_init_special_entries(void)
5285{
5286 /* Registering of the loopback is done before this portion of code,
5287 * the loopback reference in rt6_info will not be taken, do it
5288 * manually for init_net */
David Ahern421842e2018-04-17 17:33:18 -07005289 init_net.ipv6.fib6_null_entry->fib6_nh.nh_dev = init_net.loopback_dev;
WANG Cong2f460932017-05-03 22:07:31 -07005290 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
5291 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5292 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5293 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
5294 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5295 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
5296 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5297 #endif
5298}
5299
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005300int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005302 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07005303 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005304
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08005305 ret = -ENOMEM;
5306 ip6_dst_ops_template.kmem_cachep =
5307 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
5308 SLAB_HWCACHE_ALIGN, NULL);
5309 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08005310 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07005311
Eric Dumazetfc66f952010-10-08 06:37:34 +00005312 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005313 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08005314 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08005315
David S. Millerc3426b42012-06-09 16:27:05 -07005316 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
5317 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07005318 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00005319
David S. Miller7e52b332012-06-15 15:51:55 -07005320 ret = register_pernet_subsys(&ip6_route_net_ops);
5321 if (ret)
5322 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07005323
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07005324 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
5325
David S. Millere8803b62012-06-16 01:12:19 -07005326 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005327 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005328 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005329
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005330 ret = xfrm6_init();
5331 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07005332 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08005333
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005334 ret = fib6_rules_init();
5335 if (ret)
5336 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08005337
Thomas Grafd1896342012-06-18 12:08:33 +00005338 ret = register_pernet_subsys(&ip6_route_net_late_ops);
5339 if (ret)
5340 goto fib6_rules_init;
5341
Florian Westphal16feebc2017-12-02 21:44:08 +01005342 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
5343 inet6_rtm_newroute, NULL, 0);
5344 if (ret < 0)
5345 goto out_register_late_subsys;
5346
5347 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
5348 inet6_rtm_delroute, NULL, 0);
5349 if (ret < 0)
5350 goto out_register_late_subsys;
5351
5352 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
5353 inet6_rtm_getroute, NULL,
5354 RTNL_FLAG_DOIT_UNLOCKED);
5355 if (ret < 0)
Thomas Grafd1896342012-06-18 12:08:33 +00005356 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005357
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005358 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005359 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00005360 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005361
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07005362 for_each_possible_cpu(cpu) {
5363 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
5364
5365 INIT_LIST_HEAD(&ul->head);
5366 spin_lock_init(&ul->lock);
5367 }
5368
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005369out:
5370 return ret;
5371
Thomas Grafd1896342012-06-18 12:08:33 +00005372out_register_late_subsys:
Florian Westphal16feebc2017-12-02 21:44:08 +01005373 rtnl_unregister_all(PF_INET6);
Thomas Grafd1896342012-06-18 12:08:33 +00005374 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005375fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005376 fib6_rules_cleanup();
5377xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005378 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00005379out_fib6_init:
5380 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005381out_register_subsys:
5382 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07005383out_register_inetpeer:
5384 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00005385out_dst_entries:
5386 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005387out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005388 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005389 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390}
5391
5392void ip6_route_cleanup(void)
5393{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005394 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00005395 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07005396 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07005399 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005400 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00005401 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005402 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403}