blob: 0745ed872e5b20608f2aa0b3961c84c6af3b3a55 [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 Ahern702cea52019-04-09 14:41:13 -0700105static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
106 int strict);
David Ahern8d1c8022018-04-17 17:33:26 -0700107static size_t rt6_nlmsg_size(struct fib6_info *rt);
David Ahernd4ead6b2018-04-17 17:33:16 -0700108static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -0700109 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -0700110 struct in6_addr *dest, struct in6_addr *src,
David Ahern16a16cd2017-02-02 12:37:11 -0800111 int iif, int type, u32 portid, u32 seq,
112 unsigned int flags);
David Ahern8d1c8022018-04-17 17:33:26 -0700113static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt,
Wei Wang35732d02017-10-06 12:05:57 -0700114 struct in6_addr *daddr,
115 struct in6_addr *saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800117#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -0700118static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000119 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700120 const struct in6_addr *gwaddr,
121 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +0000122 unsigned int pref);
David Ahern8d1c8022018-04-17 17:33:26 -0700123static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000124 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700125 const struct in6_addr *gwaddr,
126 struct net_device *dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800127#endif
128
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700129struct uncached_list {
130 spinlock_t lock;
131 struct list_head head;
132};
133
134static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
135
Xin Long510c3212018-02-14 19:06:02 +0800136void rt6_uncached_list_add(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700137{
138 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
139
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700140 rt->rt6i_uncached_list = ul;
141
142 spin_lock_bh(&ul->lock);
143 list_add_tail(&rt->rt6i_uncached, &ul->head);
144 spin_unlock_bh(&ul->lock);
145}
146
Xin Long510c3212018-02-14 19:06:02 +0800147void rt6_uncached_list_del(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700148{
149 if (!list_empty(&rt->rt6i_uncached)) {
150 struct uncached_list *ul = rt->rt6i_uncached_list;
Wei Wang81eb8442017-10-06 12:06:11 -0700151 struct net *net = dev_net(rt->dst.dev);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700152
153 spin_lock_bh(&ul->lock);
154 list_del(&rt->rt6i_uncached);
Wei Wang81eb8442017-10-06 12:06:11 -0700155 atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700156 spin_unlock_bh(&ul->lock);
157 }
158}
159
160static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
161{
162 struct net_device *loopback_dev = net->loopback_dev;
163 int cpu;
164
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500165 if (dev == loopback_dev)
166 return;
167
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700168 for_each_possible_cpu(cpu) {
169 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
170 struct rt6_info *rt;
171
172 spin_lock_bh(&ul->lock);
173 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
174 struct inet6_dev *rt_idev = rt->rt6i_idev;
175 struct net_device *rt_dev = rt->dst.dev;
176
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500177 if (rt_idev->dev == dev) {
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700178 rt->rt6i_idev = in6_dev_get(loopback_dev);
179 in6_dev_put(rt_idev);
180 }
181
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500182 if (rt_dev == dev) {
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700183 rt->dst.dev = loopback_dev;
184 dev_hold(rt->dst.dev);
185 dev_put(rt_dev);
186 }
187 }
188 spin_unlock_bh(&ul->lock);
189 }
190}
191
David Ahernf8a1b432018-04-17 17:33:21 -0700192static inline const void *choose_neigh_daddr(const struct in6_addr *p,
David S. Millerf894cbf2012-07-02 21:52:24 -0700193 struct sk_buff *skb,
194 const void *daddr)
David S. Miller39232972012-01-26 15:22:32 -0500195{
David S. Millera7563f32012-01-26 16:29:16 -0500196 if (!ipv6_addr_any(p))
David S. Miller39232972012-01-26 15:22:32 -0500197 return (const void *) p;
David S. Millerf894cbf2012-07-02 21:52:24 -0700198 else if (skb)
199 return &ipv6_hdr(skb)->daddr;
David S. Miller39232972012-01-26 15:22:32 -0500200 return daddr;
201}
202
David Ahernf8a1b432018-04-17 17:33:21 -0700203struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
204 struct net_device *dev,
205 struct sk_buff *skb,
206 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700207{
David S. Miller39232972012-01-26 15:22:32 -0500208 struct neighbour *n;
209
David Ahernf8a1b432018-04-17 17:33:21 -0700210 daddr = choose_neigh_daddr(gw, skb, daddr);
211 n = __ipv6_neigh_lookup(dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500212 if (n)
213 return n;
Stefano Brivio7adf3242019-01-02 13:29:27 +0100214
215 n = neigh_create(&nd_tbl, daddr, dev);
216 return IS_ERR(n) ? NULL : n;
David Ahernf8a1b432018-04-17 17:33:21 -0700217}
218
219static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
220 struct sk_buff *skb,
221 const void *daddr)
222{
223 const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
224
225 return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500226}
227
Julian Anastasov63fca652017-02-06 23:14:15 +0200228static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
229{
230 struct net_device *dev = dst->dev;
231 struct rt6_info *rt = (struct rt6_info *)dst;
232
David Ahernf8a1b432018-04-17 17:33:21 -0700233 daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +0200234 if (!daddr)
235 return;
236 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
237 return;
238 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
239 return;
240 __ipv6_confirm_neigh(dev, daddr);
241}
242
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800243static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 .gc = ip6_dst_gc,
246 .gc_thresh = 1024,
247 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800248 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000249 .mtu = ip6_mtu,
David Ahernd4ead6b2018-04-17 17:33:16 -0700250 .cow_metrics = dst_cow_metrics_generic,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 .destroy = ip6_dst_destroy,
252 .ifdown = ip6_dst_ifdown,
253 .negative_advice = ip6_negative_advice,
254 .link_failure = ip6_link_failure,
255 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700256 .redirect = rt6_do_redirect,
Eric W. Biederman9f8955c2015-10-07 16:48:39 -0500257 .local_out = __ip6_local_out,
David Ahernf8a1b432018-04-17 17:33:21 -0700258 .neigh_lookup = ip6_dst_neigh_lookup,
Julian Anastasov63fca652017-02-06 23:14:15 +0200259 .confirm_neigh = ip6_confirm_neigh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260};
261
Steffen Klassertebb762f2011-11-23 02:12:51 +0000262static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800263{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000264 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
265
266 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800267}
268
David S. Miller6700c272012-07-17 03:29:28 -0700269static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
270 struct sk_buff *skb, u32 mtu)
David S. Miller14e50e52007-05-24 18:17:54 -0700271{
272}
273
David S. Miller6700c272012-07-17 03:29:28 -0700274static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
275 struct sk_buff *skb)
David S. Millerb587ee32012-07-12 00:39:24 -0700276{
277}
278
David S. Miller14e50e52007-05-24 18:17:54 -0700279static struct dst_ops ip6_dst_blackhole_ops = {
280 .family = AF_INET6,
David S. Miller14e50e52007-05-24 18:17:54 -0700281 .destroy = ip6_dst_destroy,
282 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000283 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800284 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700285 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Millerb587ee32012-07-12 00:39:24 -0700286 .redirect = ip6_rt_blackhole_redirect,
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -0700287 .cow_metrics = dst_cow_metrics_generic,
David Ahernf8a1b432018-04-17 17:33:21 -0700288 .neigh_lookup = ip6_dst_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700289};
290
David S. Miller62fa8a82011-01-26 20:51:05 -0800291static const u32 ip6_template_metrics[RTAX_MAX] = {
Li RongQing14edd872012-10-24 14:01:18 +0800292 [RTAX_HOPLIMIT - 1] = 0,
David S. Miller62fa8a82011-01-26 20:51:05 -0800293};
294
David Ahern8d1c8022018-04-17 17:33:26 -0700295static const struct fib6_info fib6_null_entry_template = {
David Ahern93c2fb22018-04-18 15:38:59 -0700296 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
297 .fib6_protocol = RTPROT_KERNEL,
298 .fib6_metric = ~(u32)0,
299 .fib6_ref = ATOMIC_INIT(1),
David Ahern421842e2018-04-17 17:33:18 -0700300 .fib6_type = RTN_UNREACHABLE,
301 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
302};
303
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000304static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700305 .dst = {
306 .__refcnt = ATOMIC_INIT(1),
307 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000308 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700309 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700310 .input = ip6_pkt_discard,
311 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 },
313 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314};
315
Thomas Graf101367c2006-08-04 03:39:02 -0700316#ifdef CONFIG_IPV6_MULTIPLE_TABLES
317
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000318static const struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700319 .dst = {
320 .__refcnt = ATOMIC_INIT(1),
321 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000322 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700323 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700324 .input = ip6_pkt_prohibit,
325 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700326 },
327 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700328};
329
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000330static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700331 .dst = {
332 .__refcnt = ATOMIC_INIT(1),
333 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000334 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700335 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700336 .input = dst_discard,
Eric W. Biedermanede20592015-10-07 16:48:47 -0500337 .output = dst_discard_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700338 },
339 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700340};
341
342#endif
343
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700344static void rt6_info_init(struct rt6_info *rt)
345{
346 struct dst_entry *dst = &rt->dst;
347
348 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700349 INIT_LIST_HEAD(&rt->rt6i_uncached);
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/* allocate dst with ip6_dst_ops */
David Ahern93531c62018-04-17 17:33:25 -0700353struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
354 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
David S. Miller97bab732012-06-09 22:36:36 -0700356 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Wei Wangb2a9c0e2017-06-17 10:42:41 -0700357 1, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700358
Wei Wang81eb8442017-10-06 12:06:11 -0700359 if (rt) {
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700360 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -0700361 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
362 }
Steffen Klassert81048912012-07-05 23:37:09 +0000363
David S. Millercf911662011-04-28 14:31:47 -0700364 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
David Ahern9ab179d2016-04-07 11:10:06 -0700366EXPORT_SYMBOL(ip6_dst_alloc);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368static void ip6_dst_destroy(struct dst_entry *dst)
369{
370 struct rt6_info *rt = (struct rt6_info *)dst;
David Aherna68886a2018-04-20 15:38:02 -0700371 struct fib6_info *from;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700372 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
David Ahern1620a332018-10-04 20:07:54 -0700374 ip_dst_metrics_put(dst);
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 Ahernad1601a2019-03-27 20:53:56 -0700445 if (fl6->mp_hash <= atomic_read(&match->fib6_nh.fib_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 Ahern702cea52019-04-09 14:41:13 -0700450 const struct fib6_nh *nh = &sibling->fib6_nh;
David Ahern5e670d82018-04-17 17:33:14 -0700451 int nh_upper_bound;
452
David Ahern702cea52019-04-09 14:41:13 -0700453 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
David Ahern5e670d82018-04-17 17:33:14 -0700454 if (fl6->mp_hash > nh_upper_bound)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200455 continue;
David Ahern702cea52019-04-09 14:41:13 -0700456 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200457 break;
458 match = sibling;
459 break;
460 }
461
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000462 return match;
463}
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465/*
Wei Wang66f5d6c2017-10-06 12:06:10 -0700466 * Route lookup. rcu_read_lock() should be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 */
468
David Ahern8d1c8022018-04-17 17:33:26 -0700469static inline struct fib6_info *rt6_device_match(struct net *net,
470 struct fib6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000471 const struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700473 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
David Ahern8d1c8022018-04-17 17:33:26 -0700475 struct fib6_info *sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
David Ahern5e670d82018-04-17 17:33:14 -0700477 if (!oif && ipv6_addr_any(saddr) &&
David Ahernad1601a2019-03-27 20:53:56 -0700478 !(rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD))
Ido Schimmel8067bb82018-01-07 12:45:09 +0200479 return rt;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900480
David Ahern8fb11a92018-05-04 13:54:24 -0700481 for (sprt = rt; sprt; sprt = rcu_dereference(sprt->fib6_next)) {
David Ahernad1601a2019-03-27 20:53:56 -0700482 const struct net_device *dev = sprt->fib6_nh.fib_nh_dev;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900483
David Ahernad1601a2019-03-27 20:53:56 -0700484 if (sprt->fib6_nh.fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +0200485 continue;
486
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900487 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (dev->ifindex == oif)
489 return sprt;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900490 } else {
491 if (ipv6_chk_addr(net, saddr, dev,
492 flags & RT6_LOOKUP_F_IFACE))
493 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
David Aherneea68cd2018-04-18 15:39:02 -0700497 if (oif && flags & RT6_LOOKUP_F_IFACE)
498 return net->ipv6.fib6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
David Ahernad1601a2019-03-27 20:53:56 -0700500 return rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD ? net->ipv6.fib6_null_entry : rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800503#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200504struct __rt6_probe_work {
505 struct work_struct work;
506 struct in6_addr target;
507 struct net_device *dev;
508};
509
510static void rt6_probe_deferred(struct work_struct *w)
511{
512 struct in6_addr mcaddr;
513 struct __rt6_probe_work *work =
514 container_of(w, struct __rt6_probe_work, work);
515
516 addrconf_addr_solict_mult(&work->target, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800517 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200518 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100519 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200520}
521
David Aherncc3a86c2019-04-09 14:41:12 -0700522static void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800523{
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200524 struct __rt6_probe_work *work = NULL;
David Ahern5e670d82018-04-17 17:33:14 -0700525 const struct in6_addr *nh_gw;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000526 struct neighbour *neigh;
David Ahern5e670d82018-04-17 17:33:14 -0700527 struct net_device *dev;
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200528 struct inet6_dev *idev;
David Ahern5e670d82018-04-17 17:33:14 -0700529
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800530 /*
531 * Okay, this does not seem to be appropriate
532 * for now, however, we need to check if it
533 * is really so; aka Router Reachability Probing.
534 *
535 * Router Reachability Probe MUST be rate-limited
536 * to no more than one per minute.
537 */
David Aherncc3a86c2019-04-09 14:41:12 -0700538 if (fib6_nh->fib_nh_gw_family)
Amerigo Wangfdd66812012-09-10 02:48:44 +0000539 return;
David Ahern5e670d82018-04-17 17:33:14 -0700540
David Aherncc3a86c2019-04-09 14:41:12 -0700541 nh_gw = &fib6_nh->fib_nh_gw6;
542 dev = fib6_nh->fib_nh_dev;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000543 rcu_read_lock_bh();
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200544 idev = __in6_dev_get(dev);
David Ahern5e670d82018-04-17 17:33:14 -0700545 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000546 if (neigh) {
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700547 if (neigh->nud_state & NUD_VALID)
548 goto out;
549
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000550 write_lock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700551 if (!(neigh->nud_state & NUD_VALID) &&
552 time_after(jiffies,
David Aherndcd1f572018-04-18 15:39:05 -0700553 neigh->updated + idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700554 work = kmalloc(sizeof(*work), GFP_ATOMIC);
555 if (work)
556 __neigh_set_probe_once(neigh);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200557 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000558 write_unlock(&neigh->lock);
David Aherncc3a86c2019-04-09 14:41:12 -0700559 } else if (time_after(jiffies, fib6_nh->last_probe +
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200560 idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700561 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) {
David Aherncc3a86c2019-04-09 14:41:12 -0700565 fib6_nh->last_probe = jiffies;
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700566 INIT_WORK(&work->work, rt6_probe_deferred);
David Ahern5e670d82018-04-17 17:33:14 -0700567 work->target = *nh_gw;
568 dev_hold(dev);
569 work->dev = dev;
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700570 schedule_work(&work->work);
571 }
572
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700573out:
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000574 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800575}
576#else
David Aherncc3a86c2019-04-09 14:41:12 -0700577static inline void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800578{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800579}
580#endif
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800583 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 */
David Ahern1ba9a892019-04-09 14:41:10 -0700585static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200587 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
David Ahern5e670d82018-04-17 17:33:14 -0700588 struct neighbour *neigh;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000589
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000590 rcu_read_lock_bh();
David Ahern1ba9a892019-04-09 14:41:10 -0700591 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
592 &fib6_nh->fib_nh_gw6);
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000593 if (neigh) {
594 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800595 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200596 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800597#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000598 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200599 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100600 else
601 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800602#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000603 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200604 } else {
605 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100606 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000607 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000608 rcu_read_unlock_bh();
609
Paul Marksa5a81f02012-12-03 10:26:54 +0000610 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800611}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
David Ahern702cea52019-04-09 14:41:13 -0700613static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
614 int strict)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800615{
David Ahern6e1809a2019-04-09 14:41:11 -0700616 int m = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900617
David Ahern6e1809a2019-04-09 14:41:11 -0700618 if (!oif || nh->fib_nh_dev->ifindex == oif)
619 m = 2;
620
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700621 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200622 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800623#ifdef CONFIG_IPV6_ROUTER_PREF
David Ahern702cea52019-04-09 14:41:13 -0700624 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800625#endif
David Ahern1ba9a892019-04-09 14:41:10 -0700626 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
David Ahern702cea52019-04-09 14:41:13 -0700627 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
David Ahern1ba9a892019-04-09 14:41:10 -0700628 int n = rt6_check_neigh(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200629 if (n < 0)
630 return n;
631 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800632 return m;
633}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
David Ahern28679ed2019-04-09 14:41:14 -0700635static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
636 int oif, int strict, int *mpri, bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800637{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200638 bool match_do_rr = false;
David Ahern28679ed2019-04-09 14:41:14 -0700639 bool rc = false;
640 int m;
Andy Gospodarek35103d12015-08-13 10:39:01 -0400641
David Ahern28679ed2019-04-09 14:41:14 -0700642 if (nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +0200643 goto out;
644
David Ahern28679ed2019-04-09 14:41:14 -0700645 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
646 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
David Ahernd5d32e42016-10-24 12:27:23 -0700647 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
Andy Gospodarek35103d12015-08-13 10:39:01 -0400648 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700649
David Ahern28679ed2019-04-09 14:41:14 -0700650 m = rt6_score_route(nh, fib6_flags, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100651 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200652 match_do_rr = true;
653 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100654 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700655 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700656 }
657
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200658 if (strict & RT6_LOOKUP_F_REACHABLE)
David Ahern28679ed2019-04-09 14:41:14 -0700659 rt6_probe(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200660
Jiri Benc7e980562013-12-11 13:48:20 +0100661 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200662 if (m > *mpri) {
663 *do_rr = match_do_rr;
664 *mpri = m;
David Ahern28679ed2019-04-09 14:41:14 -0700665 rc = true;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200666 }
David S. Millerf11e6652007-03-24 20:36:25 -0700667out:
David Ahern28679ed2019-04-09 14:41:14 -0700668 return rc;
David S. Millerf11e6652007-03-24 20:36:25 -0700669}
670
David Ahern30c15f02019-04-09 14:41:15 -0700671static void __find_rr_leaf(struct fib6_info *rt_start,
672 struct fib6_info *nomatch, u32 metric,
673 struct fib6_info **match, struct fib6_info **cont,
674 int oif, int strict, bool *do_rr, int *mpri)
David S. Millerf11e6652007-03-24 20:36:25 -0700675{
David Ahern30c15f02019-04-09 14:41:15 -0700676 struct fib6_info *rt;
677
678 for (rt = rt_start;
679 rt && rt != nomatch;
680 rt = rcu_dereference(rt->fib6_next)) {
681 struct fib6_nh *nh;
682
683 if (cont && rt->fib6_metric != metric) {
684 *cont = rt;
685 return;
686 }
687
688 if (fib6_check_expired(rt))
689 continue;
690
691 nh = &rt->fib6_nh;
692 if (find_match(nh, rt->fib6_flags, oif, strict, mpri, do_rr))
693 *match = rt;
694 }
695}
696
697static struct fib6_info *find_rr_leaf(struct fib6_node *fn,
698 struct fib6_info *leaf,
699 struct fib6_info *rr_head,
700 u32 metric, int oif, int strict,
701 bool *do_rr)
702{
703 struct fib6_info *match = NULL, *cont = NULL;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800704 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
David Ahern30c15f02019-04-09 14:41:15 -0700706 __find_rr_leaf(rr_head, NULL, metric, &match, &cont,
707 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700708
David Ahern30c15f02019-04-09 14:41:15 -0700709 __find_rr_leaf(leaf, rr_head, metric, &match, &cont,
710 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700711
712 if (match || !cont)
713 return match;
714
David Ahern30c15f02019-04-09 14:41:15 -0700715 __find_rr_leaf(cont, NULL, metric, &match, NULL,
716 oif, strict, do_rr, &mpri);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800717
David S. Millerf11e6652007-03-24 20:36:25 -0700718 return match;
719}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800720
David Ahern8d1c8022018-04-17 17:33:26 -0700721static struct fib6_info *rt6_select(struct net *net, struct fib6_node *fn,
Wei Wang8d1040e2017-10-06 12:06:08 -0700722 int oif, int strict)
David S. Millerf11e6652007-03-24 20:36:25 -0700723{
David Ahern8d1c8022018-04-17 17:33:26 -0700724 struct fib6_info *leaf = rcu_dereference(fn->leaf);
725 struct fib6_info *match, *rt0;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200726 bool do_rr = false;
Wei Wang17ecf592017-10-06 12:06:09 -0700727 int key_plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
David Ahern421842e2018-04-17 17:33:18 -0700729 if (!leaf || leaf == net->ipv6.fib6_null_entry)
730 return net->ipv6.fib6_null_entry;
Wei Wang8d1040e2017-10-06 12:06:08 -0700731
Wei Wang66f5d6c2017-10-06 12:06:10 -0700732 rt0 = rcu_dereference(fn->rr_ptr);
David S. Millerf11e6652007-03-24 20:36:25 -0700733 if (!rt0)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700734 rt0 = leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Wei Wang17ecf592017-10-06 12:06:09 -0700736 /* Double check to make sure fn is not an intermediate node
737 * and fn->leaf does not points to its child's leaf
738 * (This might happen if all routes under fn are deleted from
739 * the tree and fib6_repair_tree() is called on the node.)
740 */
David Ahern93c2fb22018-04-18 15:38:59 -0700741 key_plen = rt0->fib6_dst.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700742#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -0700743 if (rt0->fib6_src.plen)
744 key_plen = rt0->fib6_src.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700745#endif
746 if (fn->fn_bit != key_plen)
David Ahern421842e2018-04-17 17:33:18 -0700747 return net->ipv6.fib6_null_entry;
Wei Wang17ecf592017-10-06 12:06:09 -0700748
David Ahern93c2fb22018-04-18 15:38:59 -0700749 match = find_rr_leaf(fn, leaf, rt0, rt0->fib6_metric, oif, strict,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200750 &do_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200752 if (do_rr) {
David Ahern8fb11a92018-05-04 13:54:24 -0700753 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
David S. Millerf11e6652007-03-24 20:36:25 -0700754
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800755 /* no entries matched; do round-robin */
David Ahern93c2fb22018-04-18 15:38:59 -0700756 if (!next || next->fib6_metric != rt0->fib6_metric)
Wei Wang8d1040e2017-10-06 12:06:08 -0700757 next = leaf;
David S. Millerf11e6652007-03-24 20:36:25 -0700758
Wei Wang66f5d6c2017-10-06 12:06:10 -0700759 if (next != rt0) {
David Ahern93c2fb22018-04-18 15:38:59 -0700760 spin_lock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700761 /* make sure next is not being deleted from the tree */
David Ahern93c2fb22018-04-18 15:38:59 -0700762 if (next->fib6_node)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700763 rcu_assign_pointer(fn->rr_ptr, next);
David Ahern93c2fb22018-04-18 15:38:59 -0700764 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767
David Ahern421842e2018-04-17 17:33:18 -0700768 return match ? match : net->ipv6.fib6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
David Ahern8d1c8022018-04-17 17:33:26 -0700771static bool rt6_is_gw_or_nonexthop(const struct fib6_info *rt)
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700772{
David Ahernbdf00462019-04-05 16:30:26 -0700773 return (rt->fib6_flags & RTF_NONEXTHOP) || rt->fib6_nh.fib_nh_gw_family;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700774}
775
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800776#ifdef CONFIG_IPV6_ROUTE_INFO
777int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000778 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800779{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900780 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800781 struct route_info *rinfo = (struct route_info *) opt;
782 struct in6_addr prefix_buf, *prefix;
783 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900784 unsigned long lifetime;
David Ahern8d1c8022018-04-17 17:33:26 -0700785 struct fib6_info *rt;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800786
787 if (len < sizeof(struct route_info)) {
788 return -EINVAL;
789 }
790
791 /* Sanity check for prefix_len and length */
792 if (rinfo->length > 3) {
793 return -EINVAL;
794 } else if (rinfo->prefix_len > 128) {
795 return -EINVAL;
796 } else if (rinfo->prefix_len > 64) {
797 if (rinfo->length < 2) {
798 return -EINVAL;
799 }
800 } else if (rinfo->prefix_len > 0) {
801 if (rinfo->length < 1) {
802 return -EINVAL;
803 }
804 }
805
806 pref = rinfo->route_pref;
807 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000808 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800809
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900810 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800811
812 if (rinfo->length == 3)
813 prefix = (struct in6_addr *)rinfo->prefix;
814 else {
815 /* this function is safe */
816 ipv6_addr_prefix(&prefix_buf,
817 (struct in6_addr *)rinfo->prefix,
818 rinfo->prefix_len);
819 prefix = &prefix_buf;
820 }
821
Duan Jiongf104a562013-11-08 09:56:53 +0800822 if (rinfo->prefix_len == 0)
David Ahernafb1d4b52018-04-17 17:33:11 -0700823 rt = rt6_get_dflt_router(net, gwaddr, dev);
Duan Jiongf104a562013-11-08 09:56:53 +0800824 else
825 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
David Ahern830218c2016-10-24 10:52:35 -0700826 gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800827
828 if (rt && !lifetime) {
David Ahernafb1d4b52018-04-17 17:33:11 -0700829 ip6_del_rt(net, rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800830 rt = NULL;
831 }
832
833 if (!rt && lifetime)
David Ahern830218c2016-10-24 10:52:35 -0700834 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
835 dev, pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800836 else if (rt)
David Ahern93c2fb22018-04-18 15:38:59 -0700837 rt->fib6_flags = RTF_ROUTEINFO |
838 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800839
840 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000841 if (!addrconf_finite_timeout(lifetime))
David Ahern14895682018-04-17 17:33:17 -0700842 fib6_clean_expires(rt);
Gao feng1716a962012-04-06 00:13:10 +0000843 else
David Ahern14895682018-04-17 17:33:17 -0700844 fib6_set_expires(rt, jiffies + HZ * lifetime);
Gao feng1716a962012-04-06 00:13:10 +0000845
David Ahern93531c62018-04-17 17:33:25 -0700846 fib6_info_release(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800847 }
848 return 0;
849}
850#endif
851
David Ahernae90d862018-04-17 17:33:12 -0700852/*
853 * Misc support functions
854 */
855
856/* called with rcu_lock held */
David Ahern8d1c8022018-04-17 17:33:26 -0700857static struct net_device *ip6_rt_get_dev_rcu(struct fib6_info *rt)
David Ahernae90d862018-04-17 17:33:12 -0700858{
David Ahernad1601a2019-03-27 20:53:56 -0700859 struct net_device *dev = rt->fib6_nh.fib_nh_dev;
David Ahernae90d862018-04-17 17:33:12 -0700860
David Ahern93c2fb22018-04-18 15:38:59 -0700861 if (rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
David Ahernae90d862018-04-17 17:33:12 -0700862 /* for copies of local routes, dst->dev needs to be the
863 * device if it is a master device, the master device if
864 * device is enslaved, and the loopback as the default
865 */
866 if (netif_is_l3_slave(dev) &&
David Ahern93c2fb22018-04-18 15:38:59 -0700867 !rt6_need_strict(&rt->fib6_dst.addr))
David Ahernae90d862018-04-17 17:33:12 -0700868 dev = l3mdev_master_dev_rcu(dev);
869 else if (!netif_is_l3_master(dev))
870 dev = dev_net(dev)->loopback_dev;
871 /* last case is netif_is_l3_master(dev) is true in which
872 * case we want dev returned to be dev
873 */
874 }
875
876 return dev;
877}
878
David Ahern6edb3c92018-04-17 17:33:15 -0700879static const int fib6_prop[RTN_MAX + 1] = {
880 [RTN_UNSPEC] = 0,
881 [RTN_UNICAST] = 0,
882 [RTN_LOCAL] = 0,
883 [RTN_BROADCAST] = 0,
884 [RTN_ANYCAST] = 0,
885 [RTN_MULTICAST] = 0,
886 [RTN_BLACKHOLE] = -EINVAL,
887 [RTN_UNREACHABLE] = -EHOSTUNREACH,
888 [RTN_PROHIBIT] = -EACCES,
889 [RTN_THROW] = -EAGAIN,
890 [RTN_NAT] = -EINVAL,
891 [RTN_XRESOLVE] = -EINVAL,
892};
893
894static int ip6_rt_type_to_error(u8 fib6_type)
895{
896 return fib6_prop[fib6_type];
897}
898
David Ahern8d1c8022018-04-17 17:33:26 -0700899static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
David Ahern3b6761d2018-04-17 17:33:20 -0700900{
901 unsigned short flags = 0;
902
903 if (rt->dst_nocount)
904 flags |= DST_NOCOUNT;
905 if (rt->dst_nopolicy)
906 flags |= DST_NOPOLICY;
907 if (rt->dst_host)
908 flags |= DST_HOST;
909
910 return flags;
911}
912
David Ahern8d1c8022018-04-17 17:33:26 -0700913static void ip6_rt_init_dst_reject(struct rt6_info *rt, struct fib6_info *ort)
David Ahern6edb3c92018-04-17 17:33:15 -0700914{
915 rt->dst.error = ip6_rt_type_to_error(ort->fib6_type);
916
917 switch (ort->fib6_type) {
918 case RTN_BLACKHOLE:
919 rt->dst.output = dst_discard_out;
920 rt->dst.input = dst_discard;
921 break;
922 case RTN_PROHIBIT:
923 rt->dst.output = ip6_pkt_prohibit_out;
924 rt->dst.input = ip6_pkt_prohibit;
925 break;
926 case RTN_THROW:
927 case RTN_UNREACHABLE:
928 default:
929 rt->dst.output = ip6_pkt_discard_out;
930 rt->dst.input = ip6_pkt_discard;
931 break;
932 }
933}
934
David Ahern8d1c8022018-04-17 17:33:26 -0700935static void ip6_rt_init_dst(struct rt6_info *rt, struct fib6_info *ort)
David Ahern6edb3c92018-04-17 17:33:15 -0700936{
David Ahern93c2fb22018-04-18 15:38:59 -0700937 if (ort->fib6_flags & RTF_REJECT) {
David Ahern6edb3c92018-04-17 17:33:15 -0700938 ip6_rt_init_dst_reject(rt, ort);
939 return;
940 }
941
942 rt->dst.error = 0;
943 rt->dst.output = ip6_output;
944
Hangbin Liud23c4b62018-08-23 11:31:37 +0800945 if (ort->fib6_type == RTN_LOCAL || ort->fib6_type == RTN_ANYCAST) {
David Ahern6edb3c92018-04-17 17:33:15 -0700946 rt->dst.input = ip6_input;
David Ahern93c2fb22018-04-18 15:38:59 -0700947 } else if (ipv6_addr_type(&ort->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
David Ahern6edb3c92018-04-17 17:33:15 -0700948 rt->dst.input = ip6_mc_input;
949 } else {
950 rt->dst.input = ip6_forward;
951 }
952
David Ahernad1601a2019-03-27 20:53:56 -0700953 if (ort->fib6_nh.fib_nh_lws) {
954 rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.fib_nh_lws);
David Ahern6edb3c92018-04-17 17:33:15 -0700955 lwtunnel_set_redirect(&rt->dst);
956 }
957
958 rt->dst.lastuse = jiffies;
959}
960
Wei Wange873e4b2018-07-21 20:56:32 -0700961/* Caller must already hold reference to @from */
David Ahern8d1c8022018-04-17 17:33:26 -0700962static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
David Ahernae90d862018-04-17 17:33:12 -0700963{
David Ahernae90d862018-04-17 17:33:12 -0700964 rt->rt6i_flags &= ~RTF_EXPIRES;
David Aherna68886a2018-04-20 15:38:02 -0700965 rcu_assign_pointer(rt->from, from);
David Aherne1255ed2018-10-04 20:07:53 -0700966 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
David Ahernae90d862018-04-17 17:33:12 -0700967}
968
Wei Wange873e4b2018-07-21 20:56:32 -0700969/* Caller must already hold reference to @ort */
David Ahern8d1c8022018-04-17 17:33:26 -0700970static void ip6_rt_copy_init(struct rt6_info *rt, struct fib6_info *ort)
David Ahernae90d862018-04-17 17:33:12 -0700971{
David Aherndcd1f572018-04-18 15:39:05 -0700972 struct net_device *dev = fib6_info_nh_dev(ort);
973
David Ahern6edb3c92018-04-17 17:33:15 -0700974 ip6_rt_init_dst(rt, ort);
975
David Ahern93c2fb22018-04-18 15:38:59 -0700976 rt->rt6i_dst = ort->fib6_dst;
David Aherndcd1f572018-04-18 15:39:05 -0700977 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
David Ahern93c2fb22018-04-18 15:38:59 -0700978 rt->rt6i_flags = ort->fib6_flags;
David Ahernbdf00462019-04-05 16:30:26 -0700979 if (ort->fib6_nh.fib_nh_gw_family) {
David Ahernad1601a2019-03-27 20:53:56 -0700980 rt->rt6i_gateway = ort->fib6_nh.fib_nh_gw6;
David Ahern2b2450c2019-03-27 20:53:52 -0700981 rt->rt6i_flags |= RTF_GATEWAY;
982 }
David Ahernae90d862018-04-17 17:33:12 -0700983 rt6_set_from(rt, ort);
David Ahernae90d862018-04-17 17:33:12 -0700984#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -0700985 rt->rt6i_src = ort->fib6_src;
David Ahernae90d862018-04-17 17:33:12 -0700986#endif
David Ahernae90d862018-04-17 17:33:12 -0700987}
988
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700989static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
990 struct in6_addr *saddr)
991{
Wei Wang66f5d6c2017-10-06 12:06:10 -0700992 struct fib6_node *pn, *sn;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700993 while (1) {
994 if (fn->fn_flags & RTN_TL_ROOT)
995 return NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700996 pn = rcu_dereference(fn->parent);
997 sn = FIB6_SUBTREE(pn);
998 if (sn && sn != fn)
David Ahern64547432018-05-09 20:34:19 -0700999 fn = fib6_node_lookup(sn, NULL, saddr);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001000 else
1001 fn = pn;
1002 if (fn->fn_flags & RTN_RTINFO)
1003 return fn;
1004 }
1005}
Thomas Grafc71099a2006-08-04 23:20:06 -07001006
David Ahern10585b42019-03-20 09:24:50 -07001007static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
Wei Wangd3843fe2017-10-06 12:06:06 -07001008{
1009 struct rt6_info *rt = *prt;
1010
1011 if (dst_hold_safe(&rt->dst))
1012 return true;
David Ahern10585b42019-03-20 09:24:50 -07001013 if (net) {
Wei Wangd3843fe2017-10-06 12:06:06 -07001014 rt = net->ipv6.ip6_null_entry;
1015 dst_hold(&rt->dst);
1016 } else {
1017 rt = NULL;
1018 }
1019 *prt = rt;
1020 return false;
1021}
1022
David Aherndec9b0e2018-04-17 17:33:19 -07001023/* called with rcu_lock held */
David Ahern8d1c8022018-04-17 17:33:26 -07001024static struct rt6_info *ip6_create_rt_rcu(struct fib6_info *rt)
David Aherndec9b0e2018-04-17 17:33:19 -07001025{
David Ahern3b6761d2018-04-17 17:33:20 -07001026 unsigned short flags = fib6_info_dst_flags(rt);
David Ahernad1601a2019-03-27 20:53:56 -07001027 struct net_device *dev = rt->fib6_nh.fib_nh_dev;
David Aherndec9b0e2018-04-17 17:33:19 -07001028 struct rt6_info *nrt;
1029
Wei Wange873e4b2018-07-21 20:56:32 -07001030 if (!fib6_info_hold_safe(rt))
Xin Long1c87e792019-03-20 14:45:48 +08001031 goto fallback;
Wei Wange873e4b2018-07-21 20:56:32 -07001032
David Ahern93531c62018-04-17 17:33:25 -07001033 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
Xin Long1c87e792019-03-20 14:45:48 +08001034 if (!nrt) {
Wei Wange873e4b2018-07-21 20:56:32 -07001035 fib6_info_release(rt);
Xin Long1c87e792019-03-20 14:45:48 +08001036 goto fallback;
1037 }
David Aherndec9b0e2018-04-17 17:33:19 -07001038
Xin Long1c87e792019-03-20 14:45:48 +08001039 ip6_rt_copy_init(nrt, rt);
1040 return nrt;
1041
1042fallback:
1043 nrt = dev_net(dev)->ipv6.ip6_null_entry;
1044 dst_hold(&nrt->dst);
David Aherndec9b0e2018-04-17 17:33:19 -07001045 return nrt;
1046}
1047
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001048static struct rt6_info *ip6_pol_route_lookup(struct net *net,
1049 struct fib6_table *table,
David Ahernb75cc8f2018-03-02 08:32:17 -08001050 struct flowi6 *fl6,
1051 const struct sk_buff *skb,
1052 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
David Ahern8d1c8022018-04-17 17:33:26 -07001054 struct fib6_info *f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 struct fib6_node *fn;
David Ahern23fb93a2018-04-17 17:33:23 -07001056 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
David Ahernb6cdbc82018-03-29 17:44:57 -07001058 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1059 flags &= ~RT6_LOOKUP_F_IFACE;
1060
Wei Wang66f5d6c2017-10-06 12:06:10 -07001061 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07001062 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -07001063restart:
David Ahern23fb93a2018-04-17 17:33:23 -07001064 f6i = rcu_dereference(fn->leaf);
David Ahernaf52a522019-04-09 14:41:16 -07001065 if (!f6i)
David Ahern23fb93a2018-04-17 17:33:23 -07001066 f6i = net->ipv6.fib6_null_entry;
David Ahernaf52a522019-04-09 14:41:16 -07001067 else
David Ahern23fb93a2018-04-17 17:33:23 -07001068 f6i = rt6_device_match(net, f6i, &fl6->saddr,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001069 fl6->flowi6_oif, flags);
David Ahernaf52a522019-04-09 14:41:16 -07001070
David Ahern23fb93a2018-04-17 17:33:23 -07001071 if (f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001072 fn = fib6_backtrack(fn, &fl6->saddr);
1073 if (fn)
1074 goto restart;
David Ahernaf52a522019-04-09 14:41:16 -07001075
1076 rt = net->ipv6.ip6_null_entry;
1077 dst_hold(&rt->dst);
1078 goto out;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001079 }
Wei Wang2b760fc2017-10-06 12:06:03 -07001080
David Ahernaf52a522019-04-09 14:41:16 -07001081 if (f6i->fib6_nsiblings && fl6->flowi6_oif == 0)
1082 f6i = fib6_multipath_select(net, f6i, fl6, fl6->flowi6_oif, skb,
1083 flags);
David S. Miller4c9483b2011-03-12 16:22:43 -05001084 /* Search through exception table */
David Ahern23fb93a2018-04-17 17:33:23 -07001085 rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
1086 if (rt) {
David Ahern10585b42019-03-20 09:24:50 -07001087 if (ip6_hold_safe(net, &rt))
David Aherndec9b0e2018-04-17 17:33:19 -07001088 dst_use_noref(&rt->dst, jiffies);
David Ahern23fb93a2018-04-17 17:33:23 -07001089 } else {
1090 rt = ip6_create_rt_rcu(f6i);
David Aherndec9b0e2018-04-17 17:33:19 -07001091 }
Wei Wangd3843fe2017-10-06 12:06:06 -07001092
David Ahernaf52a522019-04-09 14:41:16 -07001093out:
1094 trace_fib6_table_lookup(net, f6i, table, fl6);
1095
Wei Wang66f5d6c2017-10-06 12:06:10 -07001096 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08001097
Thomas Grafc71099a2006-08-04 23:20:06 -07001098 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001099}
1100
Ian Morris67ba4152014-08-24 21:53:10 +01001101struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08001102 const struct sk_buff *skb, int flags)
Florian Westphalea6e5742011-09-05 16:05:44 +02001103{
David Ahernb75cc8f2018-03-02 08:32:17 -08001104 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
Florian Westphalea6e5742011-09-05 16:05:44 +02001105}
1106EXPORT_SYMBOL_GPL(ip6_route_lookup);
1107
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001108struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
David Ahernb75cc8f2018-03-02 08:32:17 -08001109 const struct in6_addr *saddr, int oif,
1110 const struct sk_buff *skb, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -07001111{
David S. Miller4c9483b2011-03-12 16:22:43 -05001112 struct flowi6 fl6 = {
1113 .flowi6_oif = oif,
1114 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001115 };
1116 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001117 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07001118
Thomas Grafadaa70b2006-10-13 15:01:03 -07001119 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -05001120 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -07001121 flags |= RT6_LOOKUP_F_HAS_SADDR;
1122 }
1123
David Ahernb75cc8f2018-03-02 08:32:17 -08001124 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -07001125 if (dst->error == 0)
1126 return (struct rt6_info *) dst;
1127
1128 dst_release(dst);
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return NULL;
1131}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001132EXPORT_SYMBOL(rt6_lookup);
1133
Thomas Grafc71099a2006-08-04 23:20:06 -07001134/* ip6_ins_rt is called with FREE table->tb6_lock.
Wei Wang1cfb71e2017-06-17 10:42:33 -07001135 * It takes new route entry, the addition fails by any reason the
1136 * route is released.
1137 * Caller must hold dst before calling it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 */
1139
David Ahern8d1c8022018-04-17 17:33:26 -07001140static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
David Ahern333c4302017-05-21 10:12:04 -06001141 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001144 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
David Ahern93c2fb22018-04-18 15:38:59 -07001146 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001147 spin_lock_bh(&table->tb6_lock);
David Ahernd4ead6b2018-04-17 17:33:16 -07001148 err = fib6_add(&table->tb6_root, rt, info, extack);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001149 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 return err;
1152}
1153
David Ahern8d1c8022018-04-17 17:33:26 -07001154int ip6_ins_rt(struct net *net, struct fib6_info *rt)
Thomas Graf40e22e82006-08-22 00:00:45 -07001155{
David Ahernafb1d4b52018-04-17 17:33:11 -07001156 struct nl_info info = { .nl_net = net, };
Florian Westphale715b6d2015-01-05 23:57:44 +01001157
David Ahernd4ead6b2018-04-17 17:33:16 -07001158 return __ip6_ins_rt(rt, &info, NULL);
Thomas Graf40e22e82006-08-22 00:00:45 -07001159}
1160
David Ahern8d1c8022018-04-17 17:33:26 -07001161static struct rt6_info *ip6_rt_cache_alloc(struct fib6_info *ort,
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001162 const struct in6_addr *daddr,
1163 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
David Ahern4832c302017-08-17 12:17:20 -07001165 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 struct rt6_info *rt;
1167
1168 /*
1169 * Clone the route.
1170 */
1171
Wei Wange873e4b2018-07-21 20:56:32 -07001172 if (!fib6_info_hold_safe(ort))
1173 return NULL;
1174
David Ahern4832c302017-08-17 12:17:20 -07001175 dev = ip6_rt_get_dev_rcu(ort);
David Ahern93531c62018-04-17 17:33:25 -07001176 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
Wei Wange873e4b2018-07-21 20:56:32 -07001177 if (!rt) {
1178 fib6_info_release(ort);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001179 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001180 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001181
1182 ip6_rt_copy_init(rt, ort);
1183 rt->rt6i_flags |= RTF_CACHE;
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001184 rt->dst.flags |= DST_HOST;
1185 rt->rt6i_dst.addr = *daddr;
1186 rt->rt6i_dst.plen = 128;
1187
1188 if (!rt6_is_gw_or_nonexthop(ort)) {
David Ahern93c2fb22018-04-18 15:38:59 -07001189 if (ort->fib6_dst.plen != 128 &&
1190 ipv6_addr_equal(&ort->fib6_dst.addr, daddr))
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001191 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001193 if (rt->rt6i_src.plen && saddr) {
1194 rt->rt6i_src.addr = *saddr;
1195 rt->rt6i_src.plen = 128;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001196 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001197#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001200 return rt;
1201}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
David Ahern8d1c8022018-04-17 17:33:26 -07001203static struct rt6_info *ip6_rt_pcpu_alloc(struct fib6_info *rt)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001204{
David Ahern3b6761d2018-04-17 17:33:20 -07001205 unsigned short flags = fib6_info_dst_flags(rt);
David Ahern4832c302017-08-17 12:17:20 -07001206 struct net_device *dev;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001207 struct rt6_info *pcpu_rt;
1208
Wei Wange873e4b2018-07-21 20:56:32 -07001209 if (!fib6_info_hold_safe(rt))
1210 return NULL;
1211
David Ahern4832c302017-08-17 12:17:20 -07001212 rcu_read_lock();
1213 dev = ip6_rt_get_dev_rcu(rt);
David Ahern93531c62018-04-17 17:33:25 -07001214 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
David Ahern4832c302017-08-17 12:17:20 -07001215 rcu_read_unlock();
Wei Wange873e4b2018-07-21 20:56:32 -07001216 if (!pcpu_rt) {
1217 fib6_info_release(rt);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001218 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001219 }
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001220 ip6_rt_copy_init(pcpu_rt, rt);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001221 pcpu_rt->rt6i_flags |= RTF_PCPU;
1222 return pcpu_rt;
1223}
1224
Wei Wang66f5d6c2017-10-06 12:06:10 -07001225/* It should be called with rcu_read_lock() acquired */
David Ahern8d1c8022018-04-17 17:33:26 -07001226static struct rt6_info *rt6_get_pcpu_route(struct fib6_info *rt)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001227{
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001228 struct rt6_info *pcpu_rt, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001229
1230 p = this_cpu_ptr(rt->rt6i_pcpu);
1231 pcpu_rt = *p;
1232
David Ahernd4ead6b2018-04-17 17:33:16 -07001233 if (pcpu_rt)
David Ahern10585b42019-03-20 09:24:50 -07001234 ip6_hold_safe(NULL, &pcpu_rt);
Wei Wangd3843fe2017-10-06 12:06:06 -07001235
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001236 return pcpu_rt;
1237}
1238
David Ahernafb1d4b52018-04-17 17:33:11 -07001239static struct rt6_info *rt6_make_pcpu_route(struct net *net,
David Ahern8d1c8022018-04-17 17:33:26 -07001240 struct fib6_info *rt)
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001241{
1242 struct rt6_info *pcpu_rt, *prev, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001243
1244 pcpu_rt = ip6_rt_pcpu_alloc(rt);
1245 if (!pcpu_rt) {
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001246 dst_hold(&net->ipv6.ip6_null_entry->dst);
1247 return net->ipv6.ip6_null_entry;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001248 }
1249
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001250 dst_hold(&pcpu_rt->dst);
Wei Wanga94b9362017-10-06 12:06:04 -07001251 p = this_cpu_ptr(rt->rt6i_pcpu);
1252 prev = cmpxchg(p, NULL, pcpu_rt);
Eric Dumazet951f7882017-10-08 21:07:18 -07001253 BUG_ON(prev);
Wei Wanga94b9362017-10-06 12:06:04 -07001254
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001255 return pcpu_rt;
1256}
1257
Wei Wang35732d02017-10-06 12:05:57 -07001258/* exception hash table implementation
1259 */
1260static DEFINE_SPINLOCK(rt6_exception_lock);
1261
1262/* Remove rt6_ex from hash table and free the memory
1263 * Caller must hold rt6_exception_lock
1264 */
1265static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1266 struct rt6_exception *rt6_ex)
1267{
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001268 struct fib6_info *from;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001269 struct net *net;
Wei Wang81eb8442017-10-06 12:06:11 -07001270
Wei Wang35732d02017-10-06 12:05:57 -07001271 if (!bucket || !rt6_ex)
1272 return;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001273
1274 net = dev_net(rt6_ex->rt6i->dst.dev);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001275 net->ipv6.rt6_stats->fib_rt_cache--;
1276
1277 /* purge completely the exception to allow releasing the held resources:
1278 * some [sk] cache may keep the dst around for unlimited time
1279 */
1280 from = rcu_dereference_protected(rt6_ex->rt6i->from,
1281 lockdep_is_held(&rt6_exception_lock));
1282 rcu_assign_pointer(rt6_ex->rt6i->from, NULL);
1283 fib6_info_release(from);
1284 dst_dev_put(&rt6_ex->rt6i->dst);
1285
Wei Wang35732d02017-10-06 12:05:57 -07001286 hlist_del_rcu(&rt6_ex->hlist);
David Ahern77634cc2018-04-17 17:33:27 -07001287 dst_release(&rt6_ex->rt6i->dst);
Wei Wang35732d02017-10-06 12:05:57 -07001288 kfree_rcu(rt6_ex, rcu);
1289 WARN_ON_ONCE(!bucket->depth);
1290 bucket->depth--;
1291}
1292
1293/* Remove oldest rt6_ex in bucket and free the memory
1294 * Caller must hold rt6_exception_lock
1295 */
1296static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1297{
1298 struct rt6_exception *rt6_ex, *oldest = NULL;
1299
1300 if (!bucket)
1301 return;
1302
1303 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1304 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1305 oldest = rt6_ex;
1306 }
1307 rt6_remove_exception(bucket, oldest);
1308}
1309
1310static u32 rt6_exception_hash(const struct in6_addr *dst,
1311 const struct in6_addr *src)
1312{
1313 static u32 seed __read_mostly;
1314 u32 val;
1315
1316 net_get_random_once(&seed, sizeof(seed));
1317 val = jhash(dst, sizeof(*dst), seed);
1318
1319#ifdef CONFIG_IPV6_SUBTREES
1320 if (src)
1321 val = jhash(src, sizeof(*src), val);
1322#endif
1323 return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1324}
1325
1326/* Helper function to find the cached rt in the hash table
1327 * and update bucket pointer to point to the bucket for this
1328 * (daddr, saddr) pair
1329 * Caller must hold rt6_exception_lock
1330 */
1331static struct rt6_exception *
1332__rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1333 const struct in6_addr *daddr,
1334 const struct in6_addr *saddr)
1335{
1336 struct rt6_exception *rt6_ex;
1337 u32 hval;
1338
1339 if (!(*bucket) || !daddr)
1340 return NULL;
1341
1342 hval = rt6_exception_hash(daddr, saddr);
1343 *bucket += hval;
1344
1345 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1346 struct rt6_info *rt6 = rt6_ex->rt6i;
1347 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1348
1349#ifdef CONFIG_IPV6_SUBTREES
1350 if (matched && saddr)
1351 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1352#endif
1353 if (matched)
1354 return rt6_ex;
1355 }
1356 return NULL;
1357}
1358
1359/* Helper function to find the cached rt in the hash table
1360 * and update bucket pointer to point to the bucket for this
1361 * (daddr, saddr) pair
1362 * Caller must hold rcu_read_lock()
1363 */
1364static struct rt6_exception *
1365__rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1366 const struct in6_addr *daddr,
1367 const struct in6_addr *saddr)
1368{
1369 struct rt6_exception *rt6_ex;
1370 u32 hval;
1371
1372 WARN_ON_ONCE(!rcu_read_lock_held());
1373
1374 if (!(*bucket) || !daddr)
1375 return NULL;
1376
1377 hval = rt6_exception_hash(daddr, saddr);
1378 *bucket += hval;
1379
1380 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1381 struct rt6_info *rt6 = rt6_ex->rt6i;
1382 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1383
1384#ifdef CONFIG_IPV6_SUBTREES
1385 if (matched && saddr)
1386 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1387#endif
1388 if (matched)
1389 return rt6_ex;
1390 }
1391 return NULL;
1392}
1393
David Ahern8d1c8022018-04-17 17:33:26 -07001394static unsigned int fib6_mtu(const struct fib6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001395{
David Ahernd4ead6b2018-04-17 17:33:16 -07001396 unsigned int mtu;
1397
David Aherndcd1f572018-04-18 15:39:05 -07001398 if (rt->fib6_pmtu) {
1399 mtu = rt->fib6_pmtu;
1400 } else {
1401 struct net_device *dev = fib6_info_nh_dev(rt);
1402 struct inet6_dev *idev;
1403
1404 rcu_read_lock();
1405 idev = __in6_dev_get(dev);
1406 mtu = idev->cnf.mtu6;
1407 rcu_read_unlock();
1408 }
1409
David Ahernd4ead6b2018-04-17 17:33:16 -07001410 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1411
David Ahernad1601a2019-03-27 20:53:56 -07001412 return mtu - lwtunnel_headroom(rt->fib6_nh.fib_nh_lws, mtu);
David Ahernd4ead6b2018-04-17 17:33:16 -07001413}
1414
Wei Wang35732d02017-10-06 12:05:57 -07001415static int rt6_insert_exception(struct rt6_info *nrt,
David Ahern8d1c8022018-04-17 17:33:26 -07001416 struct fib6_info *ort)
Wei Wang35732d02017-10-06 12:05:57 -07001417{
David Ahern5e670d82018-04-17 17:33:14 -07001418 struct net *net = dev_net(nrt->dst.dev);
Wei Wang35732d02017-10-06 12:05:57 -07001419 struct rt6_exception_bucket *bucket;
1420 struct in6_addr *src_key = NULL;
1421 struct rt6_exception *rt6_ex;
1422 int err = 0;
1423
Wei Wang35732d02017-10-06 12:05:57 -07001424 spin_lock_bh(&rt6_exception_lock);
1425
1426 if (ort->exception_bucket_flushed) {
1427 err = -EINVAL;
1428 goto out;
1429 }
1430
1431 bucket = rcu_dereference_protected(ort->rt6i_exception_bucket,
1432 lockdep_is_held(&rt6_exception_lock));
1433 if (!bucket) {
1434 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1435 GFP_ATOMIC);
1436 if (!bucket) {
1437 err = -ENOMEM;
1438 goto out;
1439 }
1440 rcu_assign_pointer(ort->rt6i_exception_bucket, bucket);
1441 }
1442
1443#ifdef CONFIG_IPV6_SUBTREES
1444 /* rt6i_src.plen != 0 indicates ort is in subtree
1445 * and exception table is indexed by a hash of
1446 * both rt6i_dst and rt6i_src.
1447 * Otherwise, the exception table is indexed by
1448 * a hash of only rt6i_dst.
1449 */
David Ahern93c2fb22018-04-18 15:38:59 -07001450 if (ort->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001451 src_key = &nrt->rt6i_src.addr;
1452#endif
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001453 /* rt6_mtu_change() might lower mtu on ort.
1454 * Only insert this exception route if its mtu
1455 * is less than ort's mtu value.
1456 */
David Ahernd4ead6b2018-04-17 17:33:16 -07001457 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(ort)) {
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001458 err = -EINVAL;
1459 goto out;
1460 }
Wei Wang60006a42017-10-06 12:05:58 -07001461
Wei Wang35732d02017-10-06 12:05:57 -07001462 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1463 src_key);
1464 if (rt6_ex)
1465 rt6_remove_exception(bucket, rt6_ex);
1466
1467 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1468 if (!rt6_ex) {
1469 err = -ENOMEM;
1470 goto out;
1471 }
1472 rt6_ex->rt6i = nrt;
1473 rt6_ex->stamp = jiffies;
Wei Wang35732d02017-10-06 12:05:57 -07001474 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1475 bucket->depth++;
Wei Wang81eb8442017-10-06 12:06:11 -07001476 net->ipv6.rt6_stats->fib_rt_cache++;
Wei Wang35732d02017-10-06 12:05:57 -07001477
1478 if (bucket->depth > FIB6_MAX_DEPTH)
1479 rt6_exception_remove_oldest(bucket);
1480
1481out:
1482 spin_unlock_bh(&rt6_exception_lock);
1483
1484 /* Update fn->fn_sernum to invalidate all cached dst */
Paolo Abenib886d5f2017-10-19 16:07:10 +02001485 if (!err) {
David Ahern93c2fb22018-04-18 15:38:59 -07001486 spin_lock_bh(&ort->fib6_table->tb6_lock);
David Ahern7aef6852018-04-17 17:33:10 -07001487 fib6_update_sernum(net, ort);
David Ahern93c2fb22018-04-18 15:38:59 -07001488 spin_unlock_bh(&ort->fib6_table->tb6_lock);
Paolo Abenib886d5f2017-10-19 16:07:10 +02001489 fib6_force_start_gc(net);
1490 }
Wei Wang35732d02017-10-06 12:05:57 -07001491
1492 return err;
1493}
1494
David Ahern8d1c8022018-04-17 17:33:26 -07001495void rt6_flush_exceptions(struct fib6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001496{
1497 struct rt6_exception_bucket *bucket;
1498 struct rt6_exception *rt6_ex;
1499 struct hlist_node *tmp;
1500 int i;
1501
1502 spin_lock_bh(&rt6_exception_lock);
1503 /* Prevent rt6_insert_exception() to recreate the bucket list */
1504 rt->exception_bucket_flushed = 1;
1505
1506 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1507 lockdep_is_held(&rt6_exception_lock));
1508 if (!bucket)
1509 goto out;
1510
1511 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1512 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
1513 rt6_remove_exception(bucket, rt6_ex);
1514 WARN_ON_ONCE(bucket->depth);
1515 bucket++;
1516 }
1517
1518out:
1519 spin_unlock_bh(&rt6_exception_lock);
1520}
1521
1522/* Find cached rt in the hash table inside passed in rt
1523 * Caller has to hold rcu_read_lock()
1524 */
David Ahern8d1c8022018-04-17 17:33:26 -07001525static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt,
Wei Wang35732d02017-10-06 12:05:57 -07001526 struct in6_addr *daddr,
1527 struct in6_addr *saddr)
1528{
1529 struct rt6_exception_bucket *bucket;
1530 struct in6_addr *src_key = NULL;
1531 struct rt6_exception *rt6_ex;
1532 struct rt6_info *res = NULL;
1533
1534 bucket = rcu_dereference(rt->rt6i_exception_bucket);
1535
1536#ifdef CONFIG_IPV6_SUBTREES
1537 /* rt6i_src.plen != 0 indicates rt is in subtree
1538 * and exception table is indexed by a hash of
1539 * both rt6i_dst and rt6i_src.
1540 * Otherwise, the exception table is indexed by
1541 * a hash of only rt6i_dst.
1542 */
David Ahern93c2fb22018-04-18 15:38:59 -07001543 if (rt->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001544 src_key = saddr;
1545#endif
1546 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1547
1548 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
1549 res = rt6_ex->rt6i;
1550
1551 return res;
1552}
1553
1554/* Remove the passed in cached rt from the hash table that contains it */
David Ahern23fb93a2018-04-17 17:33:23 -07001555static int rt6_remove_exception_rt(struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001556{
Wei Wang35732d02017-10-06 12:05:57 -07001557 struct rt6_exception_bucket *bucket;
1558 struct in6_addr *src_key = NULL;
1559 struct rt6_exception *rt6_ex;
David Ahern8a14e462018-04-23 11:32:07 -07001560 struct fib6_info *from;
Wei Wang35732d02017-10-06 12:05:57 -07001561 int err;
1562
Eric Dumazet091311d2018-04-24 09:22:49 -07001563 from = rcu_dereference(rt->from);
Wei Wang35732d02017-10-06 12:05:57 -07001564 if (!from ||
Colin Ian King442d7132017-10-10 19:10:30 +01001565 !(rt->rt6i_flags & RTF_CACHE))
Wei Wang35732d02017-10-06 12:05:57 -07001566 return -EINVAL;
1567
1568 if (!rcu_access_pointer(from->rt6i_exception_bucket))
1569 return -ENOENT;
1570
1571 spin_lock_bh(&rt6_exception_lock);
1572 bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
1573 lockdep_is_held(&rt6_exception_lock));
1574#ifdef CONFIG_IPV6_SUBTREES
1575 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1576 * and exception table is indexed by a hash of
1577 * both rt6i_dst and rt6i_src.
1578 * Otherwise, the exception table is indexed by
1579 * a hash of only rt6i_dst.
1580 */
David Ahern93c2fb22018-04-18 15:38:59 -07001581 if (from->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001582 src_key = &rt->rt6i_src.addr;
1583#endif
1584 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1585 &rt->rt6i_dst.addr,
1586 src_key);
1587 if (rt6_ex) {
1588 rt6_remove_exception(bucket, rt6_ex);
1589 err = 0;
1590 } else {
1591 err = -ENOENT;
1592 }
1593
1594 spin_unlock_bh(&rt6_exception_lock);
1595 return err;
1596}
1597
1598/* Find rt6_ex which contains the passed in rt cache and
1599 * refresh its stamp
1600 */
1601static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1602{
Wei Wang35732d02017-10-06 12:05:57 -07001603 struct rt6_exception_bucket *bucket;
1604 struct in6_addr *src_key = NULL;
1605 struct rt6_exception *rt6_ex;
Paolo Abeni193f3682019-02-21 11:19:41 +01001606 struct fib6_info *from;
Wei Wang35732d02017-10-06 12:05:57 -07001607
1608 rcu_read_lock();
Paolo Abeni193f3682019-02-21 11:19:41 +01001609 from = rcu_dereference(rt->from);
1610 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1611 goto unlock;
1612
Wei Wang35732d02017-10-06 12:05:57 -07001613 bucket = rcu_dereference(from->rt6i_exception_bucket);
1614
1615#ifdef CONFIG_IPV6_SUBTREES
1616 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1617 * and exception table is indexed by a hash of
1618 * both rt6i_dst and rt6i_src.
1619 * Otherwise, the exception table is indexed by
1620 * a hash of only rt6i_dst.
1621 */
David Ahern93c2fb22018-04-18 15:38:59 -07001622 if (from->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001623 src_key = &rt->rt6i_src.addr;
1624#endif
1625 rt6_ex = __rt6_find_exception_rcu(&bucket,
1626 &rt->rt6i_dst.addr,
1627 src_key);
1628 if (rt6_ex)
1629 rt6_ex->stamp = jiffies;
1630
Paolo Abeni193f3682019-02-21 11:19:41 +01001631unlock:
Wei Wang35732d02017-10-06 12:05:57 -07001632 rcu_read_unlock();
1633}
1634
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001635static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1636 struct rt6_info *rt, int mtu)
1637{
1638 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1639 * lowest MTU in the path: always allow updating the route PMTU to
1640 * reflect PMTU decreases.
1641 *
1642 * If the new MTU is higher, and the route PMTU is equal to the local
1643 * MTU, this means the old MTU is the lowest in the path, so allow
1644 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1645 * handle this.
1646 */
1647
1648 if (dst_mtu(&rt->dst) >= mtu)
1649 return true;
1650
1651 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1652 return true;
1653
1654 return false;
1655}
1656
1657static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
David Ahern8d1c8022018-04-17 17:33:26 -07001658 struct fib6_info *rt, int mtu)
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001659{
1660 struct rt6_exception_bucket *bucket;
1661 struct rt6_exception *rt6_ex;
1662 int i;
1663
1664 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1665 lockdep_is_held(&rt6_exception_lock));
1666
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001667 if (!bucket)
1668 return;
1669
1670 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1671 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1672 struct rt6_info *entry = rt6_ex->rt6i;
1673
1674 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
David Ahernd4ead6b2018-04-17 17:33:16 -07001675 * route), the metrics of its rt->from have already
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001676 * been updated.
1677 */
David Ahernd4ead6b2018-04-17 17:33:16 -07001678 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001679 rt6_mtu_change_route_allowed(idev, entry, mtu))
David Ahernd4ead6b2018-04-17 17:33:16 -07001680 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001681 }
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001682 bucket++;
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001683 }
1684}
1685
Wei Wangb16cb452017-10-06 12:06:00 -07001686#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
1687
David Ahern8d1c8022018-04-17 17:33:26 -07001688static void rt6_exceptions_clean_tohost(struct fib6_info *rt,
Wei Wangb16cb452017-10-06 12:06:00 -07001689 struct in6_addr *gateway)
1690{
1691 struct rt6_exception_bucket *bucket;
1692 struct rt6_exception *rt6_ex;
1693 struct hlist_node *tmp;
1694 int i;
1695
1696 if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1697 return;
1698
1699 spin_lock_bh(&rt6_exception_lock);
1700 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1701 lockdep_is_held(&rt6_exception_lock));
1702
1703 if (bucket) {
1704 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1705 hlist_for_each_entry_safe(rt6_ex, tmp,
1706 &bucket->chain, hlist) {
1707 struct rt6_info *entry = rt6_ex->rt6i;
1708
1709 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1710 RTF_CACHE_GATEWAY &&
1711 ipv6_addr_equal(gateway,
1712 &entry->rt6i_gateway)) {
1713 rt6_remove_exception(bucket, rt6_ex);
1714 }
1715 }
1716 bucket++;
1717 }
1718 }
1719
1720 spin_unlock_bh(&rt6_exception_lock);
1721}
1722
Wei Wangc757faa2017-10-06 12:06:01 -07001723static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1724 struct rt6_exception *rt6_ex,
1725 struct fib6_gc_args *gc_args,
1726 unsigned long now)
1727{
1728 struct rt6_info *rt = rt6_ex->rt6i;
1729
Paolo Abeni1859bac2017-10-19 16:07:11 +02001730 /* we are pruning and obsoleting aged-out and non gateway exceptions
1731 * even if others have still references to them, so that on next
1732 * dst_check() such references can be dropped.
1733 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
1734 * expired, independently from their aging, as per RFC 8201 section 4
1735 */
Wei Wang31afeb42018-01-26 11:40:17 -08001736 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
1737 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1738 RT6_TRACE("aging clone %p\n", rt);
1739 rt6_remove_exception(bucket, rt6_ex);
1740 return;
1741 }
1742 } else if (time_after(jiffies, rt->dst.expires)) {
1743 RT6_TRACE("purging expired route %p\n", rt);
Wei Wangc757faa2017-10-06 12:06:01 -07001744 rt6_remove_exception(bucket, rt6_ex);
1745 return;
Wei Wang31afeb42018-01-26 11:40:17 -08001746 }
1747
1748 if (rt->rt6i_flags & RTF_GATEWAY) {
Wei Wangc757faa2017-10-06 12:06:01 -07001749 struct neighbour *neigh;
1750 __u8 neigh_flags = 0;
1751
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001752 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
1753 if (neigh)
Wei Wangc757faa2017-10-06 12:06:01 -07001754 neigh_flags = neigh->flags;
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001755
Wei Wangc757faa2017-10-06 12:06:01 -07001756 if (!(neigh_flags & NTF_ROUTER)) {
1757 RT6_TRACE("purging route %p via non-router but gateway\n",
1758 rt);
1759 rt6_remove_exception(bucket, rt6_ex);
1760 return;
1761 }
1762 }
Wei Wang31afeb42018-01-26 11:40:17 -08001763
Wei Wangc757faa2017-10-06 12:06:01 -07001764 gc_args->more++;
1765}
1766
David Ahern8d1c8022018-04-17 17:33:26 -07001767void rt6_age_exceptions(struct fib6_info *rt,
Wei Wangc757faa2017-10-06 12:06:01 -07001768 struct fib6_gc_args *gc_args,
1769 unsigned long now)
1770{
1771 struct rt6_exception_bucket *bucket;
1772 struct rt6_exception *rt6_ex;
1773 struct hlist_node *tmp;
1774 int i;
1775
1776 if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1777 return;
1778
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001779 rcu_read_lock_bh();
1780 spin_lock(&rt6_exception_lock);
Wei Wangc757faa2017-10-06 12:06:01 -07001781 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1782 lockdep_is_held(&rt6_exception_lock));
1783
1784 if (bucket) {
1785 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1786 hlist_for_each_entry_safe(rt6_ex, tmp,
1787 &bucket->chain, hlist) {
1788 rt6_age_examine_exception(bucket, rt6_ex,
1789 gc_args, now);
1790 }
1791 bucket++;
1792 }
1793 }
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001794 spin_unlock(&rt6_exception_lock);
1795 rcu_read_unlock_bh();
Wei Wangc757faa2017-10-06 12:06:01 -07001796}
1797
David Ahern1d053da2018-05-09 20:34:21 -07001798/* must be called with rcu lock held */
1799struct fib6_info *fib6_table_lookup(struct net *net, struct fib6_table *table,
1800 int oif, struct flowi6 *fl6, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001802 struct fib6_node *fn, *saved_fn;
David Ahern8d1c8022018-04-17 17:33:26 -07001803 struct fib6_info *f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
David Ahern64547432018-05-09 20:34:19 -07001805 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001806 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
David Ahernca254492015-10-12 11:47:10 -07001808 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1809 oif = 0;
1810
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001811redo_rt6_select:
David Ahern23fb93a2018-04-17 17:33:23 -07001812 f6i = rt6_select(net, fn, oif, strict);
David Ahern23fb93a2018-04-17 17:33:23 -07001813 if (f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001814 fn = fib6_backtrack(fn, &fl6->saddr);
1815 if (fn)
1816 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001817 else if (strict & RT6_LOOKUP_F_REACHABLE) {
1818 /* also consider unreachable route */
1819 strict &= ~RT6_LOOKUP_F_REACHABLE;
1820 fn = saved_fn;
1821 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001822 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001823 }
1824
David Ahernd4bea422018-05-09 20:34:24 -07001825 trace_fib6_table_lookup(net, f6i, table, fl6);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -08001826
David Ahern1d053da2018-05-09 20:34:21 -07001827 return f6i;
1828}
1829
1830struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
1831 int oif, struct flowi6 *fl6,
1832 const struct sk_buff *skb, int flags)
1833{
1834 struct fib6_info *f6i;
1835 struct rt6_info *rt;
1836 int strict = 0;
1837
1838 strict |= flags & RT6_LOOKUP_F_IFACE;
1839 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
1840 if (net->ipv6.devconf_all->forwarding == 0)
1841 strict |= RT6_LOOKUP_F_REACHABLE;
1842
1843 rcu_read_lock();
1844
1845 f6i = fib6_table_lookup(net, table, oif, fl6, strict);
1846 if (f6i->fib6_nsiblings)
1847 f6i = fib6_multipath_select(net, f6i, fl6, oif, skb, strict);
1848
David Ahern23fb93a2018-04-17 17:33:23 -07001849 if (f6i == net->ipv6.fib6_null_entry) {
David Ahern421842e2018-04-17 17:33:18 -07001850 rt = net->ipv6.ip6_null_entry;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001851 rcu_read_unlock();
Wei Wangd3843fe2017-10-06 12:06:06 -07001852 dst_hold(&rt->dst);
Wei Wangd3843fe2017-10-06 12:06:06 -07001853 return rt;
David Ahern23fb93a2018-04-17 17:33:23 -07001854 }
1855
1856 /*Search through exception table */
1857 rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
1858 if (rt) {
David Ahern10585b42019-03-20 09:24:50 -07001859 if (ip6_hold_safe(net, &rt))
Wei Wangd3843fe2017-10-06 12:06:06 -07001860 dst_use_noref(&rt->dst, jiffies);
David Ahernd4ead6b2018-04-17 17:33:16 -07001861
Wei Wang66f5d6c2017-10-06 12:06:10 -07001862 rcu_read_unlock();
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001863 return rt;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001864 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
David Ahernbdf00462019-04-05 16:30:26 -07001865 !f6i->fib6_nh.fib_nh_gw_family)) {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001866 /* Create a RTF_CACHE clone which will not be
1867 * owned by the fib6 tree. It is for the special case where
1868 * the daddr in the skb during the neighbor look-up is different
1869 * from the fl6->daddr used to look-up route here.
1870 */
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001871 struct rt6_info *uncached_rt;
1872
David Ahern23fb93a2018-04-17 17:33:23 -07001873 uncached_rt = ip6_rt_cache_alloc(f6i, &fl6->daddr, NULL);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001874
David Ahern4d85cd02018-04-20 15:37:59 -07001875 rcu_read_unlock();
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001876
Wei Wang1cfb71e2017-06-17 10:42:33 -07001877 if (uncached_rt) {
1878 /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
1879 * No need for another dst_hold()
1880 */
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07001881 rt6_uncached_list_add(uncached_rt);
Wei Wang81eb8442017-10-06 12:06:11 -07001882 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Wei Wang1cfb71e2017-06-17 10:42:33 -07001883 } else {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001884 uncached_rt = net->ipv6.ip6_null_entry;
Wei Wang1cfb71e2017-06-17 10:42:33 -07001885 dst_hold(&uncached_rt->dst);
1886 }
David Ahernb8115802015-11-19 12:24:22 -08001887
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001888 return uncached_rt;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001889 } else {
1890 /* Get a percpu copy */
1891
1892 struct rt6_info *pcpu_rt;
1893
Eric Dumazet951f7882017-10-08 21:07:18 -07001894 local_bh_disable();
David Ahern23fb93a2018-04-17 17:33:23 -07001895 pcpu_rt = rt6_get_pcpu_route(f6i);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001896
David Ahern93531c62018-04-17 17:33:25 -07001897 if (!pcpu_rt)
1898 pcpu_rt = rt6_make_pcpu_route(net, f6i);
1899
Eric Dumazet951f7882017-10-08 21:07:18 -07001900 local_bh_enable();
1901 rcu_read_unlock();
David Ahernd4bea422018-05-09 20:34:24 -07001902
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001903 return pcpu_rt;
1904 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001905}
David Ahern9ff74382016-06-13 13:44:19 -07001906EXPORT_SYMBOL_GPL(ip6_pol_route);
Thomas Grafc71099a2006-08-04 23:20:06 -07001907
David Ahernb75cc8f2018-03-02 08:32:17 -08001908static struct rt6_info *ip6_pol_route_input(struct net *net,
1909 struct fib6_table *table,
1910 struct flowi6 *fl6,
1911 const struct sk_buff *skb,
1912 int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -07001913{
David Ahernb75cc8f2018-03-02 08:32:17 -08001914 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -07001915}
1916
Mahesh Bandeward409b842016-09-16 12:59:08 -07001917struct dst_entry *ip6_route_input_lookup(struct net *net,
1918 struct net_device *dev,
David Ahernb75cc8f2018-03-02 08:32:17 -08001919 struct flowi6 *fl6,
1920 const struct sk_buff *skb,
1921 int flags)
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001922{
1923 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
1924 flags |= RT6_LOOKUP_F_IFACE;
1925
David Ahernb75cc8f2018-03-02 08:32:17 -08001926 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001927}
Mahesh Bandeward409b842016-09-16 12:59:08 -07001928EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001929
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001930static void ip6_multipath_l3_keys(const struct sk_buff *skb,
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001931 struct flow_keys *keys,
1932 struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001933{
1934 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
1935 const struct ipv6hdr *key_iph = outer_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001936 struct flow_keys *_flkeys = flkeys;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001937 const struct ipv6hdr *inner_iph;
1938 const struct icmp6hdr *icmph;
1939 struct ipv6hdr _inner_iph;
Eric Dumazetcea67a22018-04-29 09:54:59 -07001940 struct icmp6hdr _icmph;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001941
1942 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
1943 goto out;
1944
Eric Dumazetcea67a22018-04-29 09:54:59 -07001945 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
1946 sizeof(_icmph), &_icmph);
1947 if (!icmph)
1948 goto out;
1949
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001950 if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
1951 icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
1952 icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
1953 icmph->icmp6_type != ICMPV6_PARAMPROB)
1954 goto out;
1955
1956 inner_iph = skb_header_pointer(skb,
1957 skb_transport_offset(skb) + sizeof(*icmph),
1958 sizeof(_inner_iph), &_inner_iph);
1959 if (!inner_iph)
1960 goto out;
1961
1962 key_iph = inner_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001963 _flkeys = NULL;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001964out:
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001965 if (_flkeys) {
1966 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
1967 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
1968 keys->tags.flow_label = _flkeys->tags.flow_label;
1969 keys->basic.ip_proto = _flkeys->basic.ip_proto;
1970 } else {
1971 keys->addrs.v6addrs.src = key_iph->saddr;
1972 keys->addrs.v6addrs.dst = key_iph->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02001973 keys->tags.flow_label = ip6_flowlabel(key_iph);
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001974 keys->basic.ip_proto = key_iph->nexthdr;
1975 }
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001976}
1977
1978/* if skb is set it will be used and fl6 can be NULL */
David Ahernb4bac172018-03-02 08:32:18 -08001979u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
1980 const struct sk_buff *skb, struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001981{
1982 struct flow_keys hash_keys;
David Ahern9a2a5372018-03-02 08:32:15 -08001983 u32 mhash;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001984
David S. Millerbbfa0472018-03-12 11:09:33 -04001985 switch (ip6_multipath_hash_policy(net)) {
David Ahernb4bac172018-03-02 08:32:18 -08001986 case 0:
1987 memset(&hash_keys, 0, sizeof(hash_keys));
1988 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1989 if (skb) {
1990 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
1991 } else {
1992 hash_keys.addrs.v6addrs.src = fl6->saddr;
1993 hash_keys.addrs.v6addrs.dst = fl6->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02001994 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
David Ahernb4bac172018-03-02 08:32:18 -08001995 hash_keys.basic.ip_proto = fl6->flowi6_proto;
1996 }
1997 break;
1998 case 1:
1999 if (skb) {
2000 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2001 struct flow_keys keys;
2002
2003 /* short-circuit if we already have L4 hash present */
2004 if (skb->l4_hash)
2005 return skb_get_hash_raw(skb) >> 1;
2006
2007 memset(&hash_keys, 0, sizeof(hash_keys));
2008
2009 if (!flkeys) {
2010 skb_flow_dissect_flow_keys(skb, &keys, flag);
2011 flkeys = &keys;
2012 }
2013 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2014 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2015 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2016 hash_keys.ports.src = flkeys->ports.src;
2017 hash_keys.ports.dst = flkeys->ports.dst;
2018 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2019 } else {
2020 memset(&hash_keys, 0, sizeof(hash_keys));
2021 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2022 hash_keys.addrs.v6addrs.src = fl6->saddr;
2023 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2024 hash_keys.ports.src = fl6->fl6_sport;
2025 hash_keys.ports.dst = fl6->fl6_dport;
2026 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2027 }
2028 break;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002029 }
David Ahern9a2a5372018-03-02 08:32:15 -08002030 mhash = flow_hash_from_keys(&hash_keys);
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002031
David Ahern9a2a5372018-03-02 08:32:15 -08002032 return mhash >> 1;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002033}
2034
Thomas Grafc71099a2006-08-04 23:20:06 -07002035void ip6_route_input(struct sk_buff *skb)
2036{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002037 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002038 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002039 int flags = RT6_LOOKUP_F_HAS_SADDR;
Jiri Benc904af042015-08-20 13:56:31 +02002040 struct ip_tunnel_info *tun_info;
David S. Miller4c9483b2011-03-12 16:22:43 -05002041 struct flowi6 fl6 = {
David Aherne0d56fd2016-09-10 12:09:57 -07002042 .flowi6_iif = skb->dev->ifindex,
David S. Miller4c9483b2011-03-12 16:22:43 -05002043 .daddr = iph->daddr,
2044 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002045 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05002046 .flowi6_mark = skb->mark,
2047 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07002048 };
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002049 struct flow_keys *flkeys = NULL, _flkeys;
Thomas Grafadaa70b2006-10-13 15:01:03 -07002050
Jiri Benc904af042015-08-20 13:56:31 +02002051 tun_info = skb_tunnel_info(skb);
Jiri Benc46fa0622015-08-28 20:48:19 +02002052 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
Jiri Benc904af042015-08-20 13:56:31 +02002053 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002054
2055 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2056 flkeys = &_flkeys;
2057
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002058 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
David Ahernb4bac172018-03-02 08:32:18 -08002059 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
Jiri Benc06e9d042015-08-20 13:56:26 +02002060 skb_dst_drop(skb);
David Ahernb75cc8f2018-03-02 08:32:17 -08002061 skb_dst_set(skb,
2062 ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07002063}
2064
David Ahernb75cc8f2018-03-02 08:32:17 -08002065static struct rt6_info *ip6_pol_route_output(struct net *net,
2066 struct fib6_table *table,
2067 struct flowi6 *fl6,
2068 const struct sk_buff *skb,
2069 int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002070{
David Ahernb75cc8f2018-03-02 08:32:17 -08002071 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07002072}
2073
Paolo Abeni6f21c962016-01-29 12:30:19 +01002074struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
2075 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002076{
David Ahernd46a9d62015-10-21 08:42:22 -07002077 bool any_src;
Thomas Grafc71099a2006-08-04 23:20:06 -07002078
Robert Shearman3ede0bb2018-09-19 13:56:53 +01002079 if (ipv6_addr_type(&fl6->daddr) &
2080 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
David Ahern4c1feac2016-09-10 12:09:56 -07002081 struct dst_entry *dst;
2082
2083 dst = l3mdev_link_scope_lookup(net, fl6);
2084 if (dst)
2085 return dst;
2086 }
David Ahernca254492015-10-12 11:47:10 -07002087
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002088 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00002089
David Ahernd46a9d62015-10-21 08:42:22 -07002090 any_src = ipv6_addr_any(&fl6->saddr);
David Ahern741a11d2015-09-28 10:12:13 -07002091 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
David Ahernd46a9d62015-10-21 08:42:22 -07002092 (fl6->flowi6_oif && any_src))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07002093 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07002094
David Ahernd46a9d62015-10-21 08:42:22 -07002095 if (!any_src)
Thomas Grafadaa70b2006-10-13 15:01:03 -07002096 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00002097 else if (sk)
2098 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002099
David Ahernb75cc8f2018-03-02 08:32:17 -08002100 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
Paolo Abeni6f21c962016-01-29 12:30:19 +01002102EXPORT_SYMBOL_GPL(ip6_route_output_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
David S. Miller2774c132011-03-01 14:59:04 -08002104struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07002105{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002106 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
Wei Wang1dbe32522017-06-17 10:42:26 -07002107 struct net_device *loopback_dev = net->loopback_dev;
David S. Miller14e50e52007-05-24 18:17:54 -07002108 struct dst_entry *new = NULL;
2109
Wei Wang1dbe32522017-06-17 10:42:26 -07002110 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
Steffen Klassert62cf27e2017-10-09 08:39:43 +02002111 DST_OBSOLETE_DEAD, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07002112 if (rt) {
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002113 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002114 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002115
Changli Gaod8d1f302010-06-10 23:31:35 -07002116 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07002117 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08002118 new->input = dst_discard;
Eric W. Biedermanede20592015-10-07 16:48:47 -05002119 new->output = dst_discard_out;
David S. Miller14e50e52007-05-24 18:17:54 -07002120
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002121 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07002122
Wei Wang1dbe32522017-06-17 10:42:26 -07002123 rt->rt6i_idev = in6_dev_get(loopback_dev);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002124 rt->rt6i_gateway = ort->rt6i_gateway;
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002125 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
David S. Miller14e50e52007-05-24 18:17:54 -07002126
2127 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2128#ifdef CONFIG_IPV6_SUBTREES
2129 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2130#endif
David S. Miller14e50e52007-05-24 18:17:54 -07002131 }
2132
David S. Miller69ead7a2011-03-01 14:45:33 -08002133 dst_release(dst_orig);
2134 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07002135}
David S. Miller14e50e52007-05-24 18:17:54 -07002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137/*
2138 * Destination cache support functions
2139 */
2140
David Ahern8d1c8022018-04-17 17:33:26 -07002141static bool fib6_check(struct fib6_info *f6i, u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002142{
Steffen Klassert36143642017-08-25 09:05:42 +02002143 u32 rt_cookie = 0;
Wei Wangc5cff852017-08-21 09:47:10 -07002144
David Ahern8ae86972018-04-20 15:38:03 -07002145 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
David Ahern93531c62018-04-17 17:33:25 -07002146 return false;
2147
2148 if (fib6_check_expired(f6i))
2149 return false;
2150
2151 return true;
2152}
2153
David Aherna68886a2018-04-20 15:38:02 -07002154static struct dst_entry *rt6_check(struct rt6_info *rt,
2155 struct fib6_info *from,
2156 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002157{
Wei Wangc5cff852017-08-21 09:47:10 -07002158 u32 rt_cookie = 0;
2159
David Aherna68886a2018-04-20 15:38:02 -07002160 if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) ||
David Ahern93531c62018-04-17 17:33:25 -07002161 rt_cookie != cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002162 return NULL;
2163
2164 if (rt6_check_expired(rt))
2165 return NULL;
2166
2167 return &rt->dst;
2168}
2169
David Aherna68886a2018-04-20 15:38:02 -07002170static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2171 struct fib6_info *from,
2172 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002173{
Martin KaFai Lau5973fb12015-11-11 11:51:07 -08002174 if (!__rt6_check_expired(rt) &&
2175 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
David Aherna68886a2018-04-20 15:38:02 -07002176 fib6_check(from, cookie))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002177 return &rt->dst;
2178 else
2179 return NULL;
2180}
2181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2183{
David Aherna87b7dc2018-04-20 15:38:00 -07002184 struct dst_entry *dst_ret;
David Aherna68886a2018-04-20 15:38:02 -07002185 struct fib6_info *from;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 struct rt6_info *rt;
2187
David Aherna87b7dc2018-04-20 15:38:00 -07002188 rt = container_of(dst, struct rt6_info, dst);
2189
2190 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00002192 /* All IPV6 dsts are created with ->obsolete set to the value
2193 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2194 * into this function always.
2195 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02002196
David Aherna68886a2018-04-20 15:38:02 -07002197 from = rcu_dereference(rt->from);
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002198
David Aherna68886a2018-04-20 15:38:02 -07002199 if (from && (rt->rt6i_flags & RTF_PCPU ||
2200 unlikely(!list_empty(&rt->rt6i_uncached))))
2201 dst_ret = rt6_dst_from_check(rt, from, cookie);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002202 else
David Aherna68886a2018-04-20 15:38:02 -07002203 dst_ret = rt6_check(rt, from, cookie);
David Aherna87b7dc2018-04-20 15:38:00 -07002204
2205 rcu_read_unlock();
2206
2207 return dst_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209
2210static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2211{
2212 struct rt6_info *rt = (struct rt6_info *) dst;
2213
2214 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002215 if (rt->rt6i_flags & RTF_CACHE) {
David Ahernc3c14da2018-04-23 11:32:06 -07002216 rcu_read_lock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002217 if (rt6_check_expired(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07002218 rt6_remove_exception_rt(rt);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002219 dst = NULL;
2220 }
David Ahernc3c14da2018-04-23 11:32:06 -07002221 rcu_read_unlock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002222 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002224 dst = NULL;
2225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002227 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
2230static void ip6_link_failure(struct sk_buff *skb)
2231{
2232 struct rt6_info *rt;
2233
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002234 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Eric Dumazetadf30902009-06-02 05:19:30 +00002236 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (rt) {
David Ahern8a14e462018-04-23 11:32:07 -07002238 rcu_read_lock();
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002239 if (rt->rt6i_flags & RTF_CACHE) {
Xin Long761f6022018-11-14 00:48:28 +08002240 rt6_remove_exception_rt(rt);
Wei Wangc5cff852017-08-21 09:47:10 -07002241 } else {
David Aherna68886a2018-04-20 15:38:02 -07002242 struct fib6_info *from;
Wei Wangc5cff852017-08-21 09:47:10 -07002243 struct fib6_node *fn;
2244
David Aherna68886a2018-04-20 15:38:02 -07002245 from = rcu_dereference(rt->from);
2246 if (from) {
2247 fn = rcu_dereference(from->fib6_node);
2248 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2249 fn->fn_sernum = -1;
2250 }
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002251 }
David Ahern8a14e462018-04-23 11:32:07 -07002252 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 }
2254}
2255
David Ahern6a3e0302018-04-20 15:37:57 -07002256static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2257{
David Aherna68886a2018-04-20 15:38:02 -07002258 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2259 struct fib6_info *from;
2260
2261 rcu_read_lock();
2262 from = rcu_dereference(rt0->from);
2263 if (from)
2264 rt0->dst.expires = from->expires;
2265 rcu_read_unlock();
2266 }
David Ahern6a3e0302018-04-20 15:37:57 -07002267
2268 dst_set_expires(&rt0->dst, timeout);
2269 rt0->rt6i_flags |= RTF_EXPIRES;
2270}
2271
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002272static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2273{
2274 struct net *net = dev_net(rt->dst.dev);
2275
David Ahernd4ead6b2018-04-17 17:33:16 -07002276 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002277 rt->rt6i_flags |= RTF_MODIFIED;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002278 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2279}
2280
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002281static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2282{
2283 return !(rt->rt6i_flags & RTF_CACHE) &&
Paolo Abeni1490ed22019-02-15 18:15:37 +01002284 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002285}
2286
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002287static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2288 const struct ipv6hdr *iph, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
Julian Anastasov0dec8792017-02-06 23:14:16 +02002290 const struct in6_addr *daddr, *saddr;
Ian Morris67ba4152014-08-24 21:53:10 +01002291 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Xin Long19bda362016-10-28 18:18:01 +08002293 if (dst_metric_locked(dst, RTAX_MTU))
2294 return;
2295
Julian Anastasov0dec8792017-02-06 23:14:16 +02002296 if (iph) {
2297 daddr = &iph->daddr;
2298 saddr = &iph->saddr;
2299 } else if (sk) {
2300 daddr = &sk->sk_v6_daddr;
2301 saddr = &inet6_sk(sk)->saddr;
2302 } else {
2303 daddr = NULL;
2304 saddr = NULL;
2305 }
2306 dst_confirm_neigh(dst, daddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002307 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2308 if (mtu >= dst_mtu(dst))
2309 return;
David S. Miller81aded22012-06-15 14:54:11 -07002310
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002311 if (!rt6_cache_allowed_for_pmtu(rt6)) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002312 rt6_do_update_pmtu(rt6, mtu);
Wei Wang2b760fc2017-10-06 12:06:03 -07002313 /* update rt6_ex->stamp for cache */
2314 if (rt6->rt6i_flags & RTF_CACHE)
2315 rt6_update_exception_stamp_rt(rt6);
Julian Anastasov0dec8792017-02-06 23:14:16 +02002316 } else if (daddr) {
David Aherna68886a2018-04-20 15:38:02 -07002317 struct fib6_info *from;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002318 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01002319
David Ahern4d85cd02018-04-20 15:37:59 -07002320 rcu_read_lock();
David Aherna68886a2018-04-20 15:38:02 -07002321 from = rcu_dereference(rt6->from);
2322 nrt6 = ip6_rt_cache_alloc(from, daddr, saddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002323 if (nrt6) {
2324 rt6_do_update_pmtu(nrt6, mtu);
David Aherna68886a2018-04-20 15:38:02 -07002325 if (rt6_insert_exception(nrt6, from))
Wei Wang2b760fc2017-10-06 12:06:03 -07002326 dst_release_immediate(&nrt6->dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002327 }
David Aherna68886a2018-04-20 15:38:02 -07002328 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
2330}
2331
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002332static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2333 struct sk_buff *skb, u32 mtu)
2334{
2335 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
2336}
2337
David S. Miller42ae66c2012-06-15 20:01:57 -07002338void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002339 int oif, u32 mark, kuid_t uid)
David S. Miller81aded22012-06-15 14:54:11 -07002340{
2341 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2342 struct dst_entry *dst;
Maciej Żenczykowskidc920952018-09-29 23:44:51 -07002343 struct flowi6 fl6 = {
2344 .flowi6_oif = oif,
2345 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2346 .daddr = iph->daddr,
2347 .saddr = iph->saddr,
2348 .flowlabel = ip6_flowinfo(iph),
2349 .flowi6_uid = uid,
2350 };
David S. Miller81aded22012-06-15 14:54:11 -07002351
2352 dst = ip6_route_output(net, NULL, &fl6);
2353 if (!dst->error)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002354 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
David S. Miller81aded22012-06-15 14:54:11 -07002355 dst_release(dst);
2356}
2357EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2358
2359void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2360{
David Ahern7ddacfa2018-11-18 10:45:30 -08002361 int oif = sk->sk_bound_dev_if;
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002362 struct dst_entry *dst;
2363
David Ahern7ddacfa2018-11-18 10:45:30 -08002364 if (!oif && skb->dev)
2365 oif = l3mdev_master_ifindex(skb->dev);
2366
2367 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002368
2369 dst = __sk_dst_get(sk);
2370 if (!dst || !dst->obsolete ||
2371 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2372 return;
2373
2374 bh_lock_sock(sk);
2375 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2376 ip6_datagram_dst_update(sk, false);
2377 bh_unlock_sock(sk);
David S. Miller81aded22012-06-15 14:54:11 -07002378}
2379EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2380
Alexey Kodanev7d6850f2018-04-03 15:00:07 +03002381void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2382 const struct flowi6 *fl6)
2383{
2384#ifdef CONFIG_IPV6_SUBTREES
2385 struct ipv6_pinfo *np = inet6_sk(sk);
2386#endif
2387
2388 ip6_dst_store(sk, dst,
2389 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2390 &sk->sk_v6_daddr : NULL,
2391#ifdef CONFIG_IPV6_SUBTREES
2392 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2393 &np->saddr :
2394#endif
2395 NULL);
2396}
2397
Duan Jiongb55b76b2013-09-04 19:44:21 +08002398/* Handle redirects */
2399struct ip6rd_flowi {
2400 struct flowi6 fl6;
2401 struct in6_addr gateway;
2402};
2403
2404static struct rt6_info *__ip6_route_redirect(struct net *net,
2405 struct fib6_table *table,
2406 struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08002407 const struct sk_buff *skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002408 int flags)
2409{
2410 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
David Ahern23fb93a2018-04-17 17:33:23 -07002411 struct rt6_info *ret = NULL, *rt_cache;
David Ahern8d1c8022018-04-17 17:33:26 -07002412 struct fib6_info *rt;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002413 struct fib6_node *fn;
2414
2415 /* Get the "current" route for this destination and
Alexander Alemayhu67c408c2017-01-07 23:53:00 +01002416 * check if the redirect has come from appropriate router.
Duan Jiongb55b76b2013-09-04 19:44:21 +08002417 *
2418 * RFC 4861 specifies that redirects should only be
2419 * accepted if they come from the nexthop to the target.
2420 * Due to the way the routes are chosen, this notion
2421 * is a bit fuzzy and one might need to check all possible
2422 * routes.
2423 */
2424
Wei Wang66f5d6c2017-10-06 12:06:10 -07002425 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07002426 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002427restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002428 for_each_fib6_node_rt_rcu(fn) {
David Ahernad1601a2019-03-27 20:53:56 -07002429 if (rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +02002430 continue;
David Ahern14895682018-04-17 17:33:17 -07002431 if (fib6_check_expired(rt))
Duan Jiongb55b76b2013-09-04 19:44:21 +08002432 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002433 if (rt->fib6_flags & RTF_REJECT)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002434 break;
David Ahernbdf00462019-04-05 16:30:26 -07002435 if (!rt->fib6_nh.fib_nh_gw_family)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002436 continue;
David Ahernad1601a2019-03-27 20:53:56 -07002437 if (fl6->flowi6_oif != rt->fib6_nh.fib_nh_dev->ifindex)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002438 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07002439 /* rt_cache's gateway might be different from its 'parent'
2440 * in the case of an ip redirect.
2441 * So we keep searching in the exception table if the gateway
2442 * is different.
2443 */
David Ahernad1601a2019-03-27 20:53:56 -07002444 if (!ipv6_addr_equal(&rdfl->gateway, &rt->fib6_nh.fib_nh_gw6)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07002445 rt_cache = rt6_find_cached_rt(rt,
2446 &fl6->daddr,
2447 &fl6->saddr);
2448 if (rt_cache &&
2449 ipv6_addr_equal(&rdfl->gateway,
2450 &rt_cache->rt6i_gateway)) {
David Ahern23fb93a2018-04-17 17:33:23 -07002451 ret = rt_cache;
Wei Wang2b760fc2017-10-06 12:06:03 -07002452 break;
2453 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002454 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07002455 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002456 break;
2457 }
2458
2459 if (!rt)
David Ahern421842e2018-04-17 17:33:18 -07002460 rt = net->ipv6.fib6_null_entry;
David Ahern93c2fb22018-04-18 15:38:59 -07002461 else if (rt->fib6_flags & RTF_REJECT) {
David Ahern23fb93a2018-04-17 17:33:23 -07002462 ret = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002463 goto out;
2464 }
2465
David Ahern421842e2018-04-17 17:33:18 -07002466 if (rt == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002467 fn = fib6_backtrack(fn, &fl6->saddr);
2468 if (fn)
2469 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002470 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002471
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002472out:
David Ahern23fb93a2018-04-17 17:33:23 -07002473 if (ret)
David Ahern10585b42019-03-20 09:24:50 -07002474 ip6_hold_safe(net, &ret);
David Ahern23fb93a2018-04-17 17:33:23 -07002475 else
2476 ret = ip6_create_rt_rcu(rt);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002477
Wei Wang66f5d6c2017-10-06 12:06:10 -07002478 rcu_read_unlock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08002479
Paolo Abenib65f1642017-10-19 09:31:43 +02002480 trace_fib6_table_lookup(net, rt, table, fl6);
David Ahern23fb93a2018-04-17 17:33:23 -07002481 return ret;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002482};
2483
2484static struct dst_entry *ip6_route_redirect(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08002485 const struct flowi6 *fl6,
2486 const struct sk_buff *skb,
2487 const struct in6_addr *gateway)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002488{
2489 int flags = RT6_LOOKUP_F_HAS_SADDR;
2490 struct ip6rd_flowi rdfl;
2491
2492 rdfl.fl6 = *fl6;
2493 rdfl.gateway = *gateway;
2494
David Ahernb75cc8f2018-03-02 08:32:17 -08002495 return fib6_rule_lookup(net, &rdfl.fl6, skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002496 flags, __ip6_route_redirect);
2497}
2498
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002499void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2500 kuid_t uid)
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002501{
2502 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2503 struct dst_entry *dst;
Maciej Żenczykowski1f7f10a2018-09-29 23:44:48 -07002504 struct flowi6 fl6 = {
2505 .flowi6_iif = LOOPBACK_IFINDEX,
2506 .flowi6_oif = oif,
2507 .flowi6_mark = mark,
2508 .daddr = iph->daddr,
2509 .saddr = iph->saddr,
2510 .flowlabel = ip6_flowinfo(iph),
2511 .flowi6_uid = uid,
2512 };
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002513
David Ahernb75cc8f2018-03-02 08:32:17 -08002514 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002515 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002516 dst_release(dst);
2517}
2518EXPORT_SYMBOL_GPL(ip6_redirect);
2519
Maciej Żenczykowskid4563362018-09-29 23:44:50 -07002520void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
Duan Jiongc92a59e2013-08-22 12:07:35 +08002521{
2522 const struct ipv6hdr *iph = ipv6_hdr(skb);
2523 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2524 struct dst_entry *dst;
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07002525 struct flowi6 fl6 = {
2526 .flowi6_iif = LOOPBACK_IFINDEX,
2527 .flowi6_oif = oif,
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07002528 .daddr = msg->dest,
2529 .saddr = iph->daddr,
2530 .flowi6_uid = sock_net_uid(net, NULL),
2531 };
Duan Jiongc92a59e2013-08-22 12:07:35 +08002532
David Ahernb75cc8f2018-03-02 08:32:17 -08002533 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002534 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08002535 dst_release(dst);
2536}
2537
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002538void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
2539{
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002540 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2541 sk->sk_uid);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002542}
2543EXPORT_SYMBOL_GPL(ip6_sk_redirect);
2544
David S. Miller0dbaee32010-12-13 12:52:14 -08002545static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
David S. Miller0dbaee32010-12-13 12:52:14 -08002547 struct net_device *dev = dst->dev;
2548 unsigned int mtu = dst_mtu(dst);
2549 struct net *net = dev_net(dev);
2550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
2552
Daniel Lezcano55786892008-03-04 13:47:47 -08002553 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
2554 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
2556 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002557 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
2558 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
2559 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 * rely only on pmtu discovery"
2561 */
2562 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
2563 mtu = IPV6_MAXPLEN;
2564 return mtu;
2565}
2566
Steffen Klassertebb762f2011-11-23 02:12:51 +00002567static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002568{
David S. Millerd33e4552010-12-14 13:01:14 -08002569 struct inet6_dev *idev;
David Ahernd4ead6b2018-04-17 17:33:16 -07002570 unsigned int mtu;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002571
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002572 mtu = dst_metric_raw(dst, RTAX_MTU);
2573 if (mtu)
2574 goto out;
2575
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002576 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08002577
2578 rcu_read_lock();
2579 idev = __in6_dev_get(dst->dev);
2580 if (idev)
2581 mtu = idev->cnf.mtu6;
2582 rcu_read_unlock();
2583
Eric Dumazet30f78d82014-04-10 21:23:36 -07002584out:
Roopa Prabhu14972cb2016-08-24 20:10:43 -07002585 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2586
2587 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
David S. Millerd33e4552010-12-14 13:01:14 -08002588}
2589
David Ahern901731b2018-05-21 09:08:14 -07002590/* MTU selection:
2591 * 1. mtu on route is locked - use it
2592 * 2. mtu from nexthop exception
2593 * 3. mtu from egress device
2594 *
2595 * based on ip6_dst_mtu_forward and exception logic of
2596 * rt6_find_cached_rt; called with rcu_read_lock
2597 */
2598u32 ip6_mtu_from_fib6(struct fib6_info *f6i, struct in6_addr *daddr,
2599 struct in6_addr *saddr)
2600{
2601 struct rt6_exception_bucket *bucket;
2602 struct rt6_exception *rt6_ex;
2603 struct in6_addr *src_key;
2604 struct inet6_dev *idev;
2605 u32 mtu = 0;
2606
2607 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
2608 mtu = f6i->fib6_pmtu;
2609 if (mtu)
2610 goto out;
2611 }
2612
2613 src_key = NULL;
2614#ifdef CONFIG_IPV6_SUBTREES
2615 if (f6i->fib6_src.plen)
2616 src_key = saddr;
2617#endif
2618
2619 bucket = rcu_dereference(f6i->rt6i_exception_bucket);
2620 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
2621 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
2622 mtu = dst_metric_raw(&rt6_ex->rt6i->dst, RTAX_MTU);
2623
2624 if (likely(!mtu)) {
2625 struct net_device *dev = fib6_info_nh_dev(f6i);
2626
2627 mtu = IPV6_MIN_MTU;
2628 idev = __in6_dev_get(dev);
2629 if (idev && idev->cnf.mtu6 > mtu)
2630 mtu = idev->cnf.mtu6;
2631 }
2632
2633 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2634out:
2635 return mtu - lwtunnel_headroom(fib6_info_nh_lwt(f6i), mtu);
2636}
2637
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08002638struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05002639 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
David S. Miller87a11572011-12-06 17:04:13 -05002641 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 struct rt6_info *rt;
2643 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002644 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
David S. Miller38308472011-12-03 18:02:47 -05002646 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00002647 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Martin KaFai Lauad706862015-08-14 11:05:52 -07002649 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05002650 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05002652 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 goto out;
2654 }
2655
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002656 rt->dst.flags |= DST_HOST;
Brendan McGrath588753f2017-12-13 22:14:57 +11002657 rt->dst.input = ip6_input;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002658 rt->dst.output = ip6_output;
Julian Anastasov550bab42013-10-20 15:43:04 +03002659 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05002660 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002661 rt->rt6i_dst.plen = 128;
2662 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08002663 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Ido Schimmel4c981e22018-01-07 12:45:04 +02002665 /* Add this dst into uncached_list so that rt6_disable_ip() can
Wei Wang587fea72017-06-17 10:42:36 -07002666 * do proper release of the net_device
2667 */
2668 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002669 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
David S. Miller87a11572011-12-06 17:04:13 -05002671 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
2672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673out:
David S. Miller87a11572011-12-06 17:04:13 -05002674 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}
2676
Daniel Lezcano569d3642008-01-18 03:56:57 -08002677static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002679 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08002680 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
2681 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
2682 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
2683 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
2684 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00002685 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Eric Dumazetfc66f952010-10-08 06:37:34 +00002687 entries = dst_entries_get_fast(ops);
Michal Kubeček49a18d82013-08-01 10:04:24 +02002688 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00002689 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 goto out;
2691
Benjamin Thery6891a342008-03-04 13:49:47 -08002692 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08002693 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00002694 entries = dst_entries_get_slow(ops);
2695 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08002696 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08002698 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00002699 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700}
2701
David Ahern8c145862016-04-24 21:26:04 -07002702static struct rt6_info *ip6_nh_lookup_table(struct net *net,
2703 struct fib6_config *cfg,
David Ahernf4797b32018-01-25 16:55:08 -08002704 const struct in6_addr *gw_addr,
2705 u32 tbid, int flags)
David Ahern8c145862016-04-24 21:26:04 -07002706{
2707 struct flowi6 fl6 = {
2708 .flowi6_oif = cfg->fc_ifindex,
2709 .daddr = *gw_addr,
2710 .saddr = cfg->fc_prefsrc,
2711 };
2712 struct fib6_table *table;
2713 struct rt6_info *rt;
David Ahern8c145862016-04-24 21:26:04 -07002714
David Ahernf4797b32018-01-25 16:55:08 -08002715 table = fib6_get_table(net, tbid);
David Ahern8c145862016-04-24 21:26:04 -07002716 if (!table)
2717 return NULL;
2718
2719 if (!ipv6_addr_any(&cfg->fc_prefsrc))
2720 flags |= RT6_LOOKUP_F_HAS_SADDR;
2721
David Ahernf4797b32018-01-25 16:55:08 -08002722 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
David Ahernb75cc8f2018-03-02 08:32:17 -08002723 rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
David Ahern8c145862016-04-24 21:26:04 -07002724
2725 /* if table lookup failed, fall back to full lookup */
2726 if (rt == net->ipv6.ip6_null_entry) {
2727 ip6_rt_put(rt);
2728 rt = NULL;
2729 }
2730
2731 return rt;
2732}
2733
David Ahernfc1e64e2018-01-25 16:55:09 -08002734static int ip6_route_check_nh_onlink(struct net *net,
2735 struct fib6_config *cfg,
David Ahern9fbb7042018-03-13 08:29:36 -07002736 const struct net_device *dev,
David Ahernfc1e64e2018-01-25 16:55:09 -08002737 struct netlink_ext_ack *extack)
2738{
David Ahern44750f82018-02-06 13:17:06 -08002739 u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
David Ahernfc1e64e2018-01-25 16:55:09 -08002740 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2741 u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
Paolo Abenibf1dc8b2019-02-21 11:19:42 +01002742 struct fib6_info *from;
David Ahernfc1e64e2018-01-25 16:55:09 -08002743 struct rt6_info *grt;
2744 int err;
2745
2746 err = 0;
2747 grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
2748 if (grt) {
Paolo Abenibf1dc8b2019-02-21 11:19:42 +01002749 rcu_read_lock();
2750 from = rcu_dereference(grt->from);
David Ahern58e354c2018-02-06 12:14:12 -08002751 if (!grt->dst.error &&
David Ahern4ed591c2018-10-24 13:58:39 -07002752 /* ignore match if it is the default route */
Paolo Abenibf1dc8b2019-02-21 11:19:42 +01002753 from && !ipv6_addr_any(&from->fib6_dst.addr) &&
David Ahern58e354c2018-02-06 12:14:12 -08002754 (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
David Ahern44750f82018-02-06 13:17:06 -08002755 NL_SET_ERR_MSG(extack,
2756 "Nexthop has invalid gateway or device mismatch");
David Ahernfc1e64e2018-01-25 16:55:09 -08002757 err = -EINVAL;
2758 }
Paolo Abenibf1dc8b2019-02-21 11:19:42 +01002759 rcu_read_unlock();
David Ahernfc1e64e2018-01-25 16:55:09 -08002760
2761 ip6_rt_put(grt);
2762 }
2763
2764 return err;
2765}
2766
David Ahern1edce992018-01-25 16:55:07 -08002767static int ip6_route_check_nh(struct net *net,
2768 struct fib6_config *cfg,
2769 struct net_device **_dev,
2770 struct inet6_dev **idev)
2771{
2772 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2773 struct net_device *dev = _dev ? *_dev : NULL;
2774 struct rt6_info *grt = NULL;
2775 int err = -EHOSTUNREACH;
2776
2777 if (cfg->fc_table) {
David Ahernf4797b32018-01-25 16:55:08 -08002778 int flags = RT6_LOOKUP_F_IFACE;
2779
2780 grt = ip6_nh_lookup_table(net, cfg, gw_addr,
2781 cfg->fc_table, flags);
David Ahern1edce992018-01-25 16:55:07 -08002782 if (grt) {
2783 if (grt->rt6i_flags & RTF_GATEWAY ||
2784 (dev && dev != grt->dst.dev)) {
2785 ip6_rt_put(grt);
2786 grt = NULL;
2787 }
2788 }
2789 }
2790
2791 if (!grt)
David Ahernb75cc8f2018-03-02 08:32:17 -08002792 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
David Ahern1edce992018-01-25 16:55:07 -08002793
2794 if (!grt)
2795 goto out;
2796
2797 if (dev) {
2798 if (dev != grt->dst.dev) {
2799 ip6_rt_put(grt);
2800 goto out;
2801 }
2802 } else {
2803 *_dev = dev = grt->dst.dev;
2804 *idev = grt->rt6i_idev;
2805 dev_hold(dev);
2806 in6_dev_hold(grt->rt6i_idev);
2807 }
2808
2809 if (!(grt->rt6i_flags & RTF_GATEWAY))
2810 err = 0;
2811
2812 ip6_rt_put(grt);
2813
2814out:
2815 return err;
2816}
2817
David Ahern9fbb7042018-03-13 08:29:36 -07002818static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
2819 struct net_device **_dev, struct inet6_dev **idev,
2820 struct netlink_ext_ack *extack)
2821{
2822 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2823 int gwa_type = ipv6_addr_type(gw_addr);
David Ahern232378e2018-03-13 08:29:37 -07002824 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
David Ahern9fbb7042018-03-13 08:29:36 -07002825 const struct net_device *dev = *_dev;
David Ahern232378e2018-03-13 08:29:37 -07002826 bool need_addr_check = !dev;
David Ahern9fbb7042018-03-13 08:29:36 -07002827 int err = -EINVAL;
2828
2829 /* if gw_addr is local we will fail to detect this in case
2830 * address is still TENTATIVE (DAD in progress). rt6_lookup()
2831 * will return already-added prefix route via interface that
2832 * prefix route was assigned to, which might be non-loopback.
2833 */
David Ahern232378e2018-03-13 08:29:37 -07002834 if (dev &&
2835 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2836 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
David Ahern9fbb7042018-03-13 08:29:36 -07002837 goto out;
2838 }
2839
2840 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
2841 /* IPv6 strictly inhibits using not link-local
2842 * addresses as nexthop address.
2843 * Otherwise, router will not able to send redirects.
2844 * It is very good, but in some (rare!) circumstances
2845 * (SIT, PtP, NBMA NOARP links) it is handy to allow
2846 * some exceptions. --ANK
2847 * We allow IPv4-mapped nexthops to support RFC4798-type
2848 * addressing
2849 */
2850 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
2851 NL_SET_ERR_MSG(extack, "Invalid gateway address");
2852 goto out;
2853 }
2854
2855 if (cfg->fc_flags & RTNH_F_ONLINK)
2856 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
2857 else
2858 err = ip6_route_check_nh(net, cfg, _dev, idev);
2859
2860 if (err)
2861 goto out;
2862 }
2863
2864 /* reload in case device was changed */
2865 dev = *_dev;
2866
2867 err = -EINVAL;
2868 if (!dev) {
2869 NL_SET_ERR_MSG(extack, "Egress device not specified");
2870 goto out;
2871 } else if (dev->flags & IFF_LOOPBACK) {
2872 NL_SET_ERR_MSG(extack,
2873 "Egress device can not be loopback device for this route");
2874 goto out;
2875 }
David Ahern232378e2018-03-13 08:29:37 -07002876
2877 /* if we did not check gw_addr above, do so now that the
2878 * egress device has been resolved.
2879 */
2880 if (need_addr_check &&
2881 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2882 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
2883 goto out;
2884 }
2885
David Ahern9fbb7042018-03-13 08:29:36 -07002886 err = 0;
2887out:
2888 return err;
2889}
2890
David Ahern83c442512019-03-27 20:53:50 -07002891static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
2892{
2893 if ((flags & RTF_REJECT) ||
2894 (dev && (dev->flags & IFF_LOOPBACK) &&
2895 !(addr_type & IPV6_ADDR_LOOPBACK) &&
2896 !(flags & RTF_LOCAL)))
2897 return true;
2898
2899 return false;
2900}
2901
2902int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
2903 struct fib6_config *cfg, gfp_t gfp_flags,
2904 struct netlink_ext_ack *extack)
2905{
2906 struct net_device *dev = NULL;
2907 struct inet6_dev *idev = NULL;
2908 int addr_type;
2909 int err;
2910
David Ahernf1741732019-03-27 20:53:57 -07002911 fib6_nh->fib_nh_family = AF_INET6;
2912
David Ahern83c442512019-03-27 20:53:50 -07002913 err = -ENODEV;
2914 if (cfg->fc_ifindex) {
2915 dev = dev_get_by_index(net, cfg->fc_ifindex);
2916 if (!dev)
2917 goto out;
2918 idev = in6_dev_get(dev);
2919 if (!idev)
2920 goto out;
2921 }
2922
2923 if (cfg->fc_flags & RTNH_F_ONLINK) {
2924 if (!dev) {
2925 NL_SET_ERR_MSG(extack,
2926 "Nexthop device required for onlink");
2927 goto out;
2928 }
2929
2930 if (!(dev->flags & IFF_UP)) {
2931 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2932 err = -ENETDOWN;
2933 goto out;
2934 }
2935
David Ahernad1601a2019-03-27 20:53:56 -07002936 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
David Ahern83c442512019-03-27 20:53:50 -07002937 }
2938
David Ahernad1601a2019-03-27 20:53:56 -07002939 fib6_nh->fib_nh_weight = 1;
David Ahern83c442512019-03-27 20:53:50 -07002940
2941 /* We cannot add true routes via loopback here,
2942 * they would result in kernel looping; promote them to reject routes
2943 */
2944 addr_type = ipv6_addr_type(&cfg->fc_dst);
2945 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
2946 /* hold loopback dev/idev if we haven't done so. */
2947 if (dev != net->loopback_dev) {
2948 if (dev) {
2949 dev_put(dev);
2950 in6_dev_put(idev);
2951 }
2952 dev = net->loopback_dev;
2953 dev_hold(dev);
2954 idev = in6_dev_get(dev);
2955 if (!idev) {
2956 err = -ENODEV;
2957 goto out;
2958 }
2959 }
2960 goto set_dev;
2961 }
2962
2963 if (cfg->fc_flags & RTF_GATEWAY) {
2964 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
2965 if (err)
2966 goto out;
2967
David Ahernad1601a2019-03-27 20:53:56 -07002968 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
David Ahernbdf00462019-04-05 16:30:26 -07002969 fib6_nh->fib_nh_gw_family = AF_INET6;
David Ahern83c442512019-03-27 20:53:50 -07002970 }
2971
2972 err = -ENODEV;
2973 if (!dev)
2974 goto out;
2975
2976 if (idev->cnf.disable_ipv6) {
2977 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
2978 err = -EACCES;
2979 goto out;
2980 }
2981
2982 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
2983 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2984 err = -ENETDOWN;
2985 goto out;
2986 }
2987
2988 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
2989 !netif_carrier_ok(dev))
David Ahernad1601a2019-03-27 20:53:56 -07002990 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
David Ahern83c442512019-03-27 20:53:50 -07002991
David Ahern979e2762019-03-27 20:53:58 -07002992 err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap,
2993 cfg->fc_encap_type, cfg, gfp_flags, extack);
2994 if (err)
2995 goto out;
David Ahern83c442512019-03-27 20:53:50 -07002996set_dev:
David Ahernad1601a2019-03-27 20:53:56 -07002997 fib6_nh->fib_nh_dev = dev;
David Ahernf1741732019-03-27 20:53:57 -07002998 fib6_nh->fib_nh_oif = dev->ifindex;
David Ahern83c442512019-03-27 20:53:50 -07002999 err = 0;
3000out:
3001 if (idev)
3002 in6_dev_put(idev);
3003
3004 if (err) {
David Ahernad1601a2019-03-27 20:53:56 -07003005 lwtstate_put(fib6_nh->fib_nh_lws);
3006 fib6_nh->fib_nh_lws = NULL;
David Ahern83c442512019-03-27 20:53:50 -07003007 if (dev)
3008 dev_put(dev);
3009 }
3010
3011 return err;
3012}
3013
David Aherndac7d0f2019-03-27 20:53:51 -07003014void fib6_nh_release(struct fib6_nh *fib6_nh)
3015{
David Ahern979e2762019-03-27 20:53:58 -07003016 fib_nh_common_release(&fib6_nh->nh_common);
David Aherndac7d0f2019-03-27 20:53:51 -07003017}
3018
David Ahern8d1c8022018-04-17 17:33:26 -07003019static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
David Ahernacb54e32018-04-17 17:33:22 -07003020 gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003021 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
Daniel Lezcano55786892008-03-04 13:47:47 -08003023 struct net *net = cfg->fc_nlinfo.nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07003024 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003025 struct fib6_table *table;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003026 int err = -EINVAL;
David Ahern83c442512019-03-27 20:53:50 -07003027 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
David Ahern557c44b2017-04-19 14:19:43 -07003029 /* RTF_PCPU is an internal flag; can not be set by userspace */
David Ahernd5d531c2017-05-21 10:12:05 -06003030 if (cfg->fc_flags & RTF_PCPU) {
3031 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
David Ahern557c44b2017-04-19 14:19:43 -07003032 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003033 }
David Ahern557c44b2017-04-19 14:19:43 -07003034
Wei Wang2ea23522017-10-27 17:30:12 -07003035 /* RTF_CACHE is an internal flag; can not be set by userspace */
3036 if (cfg->fc_flags & RTF_CACHE) {
3037 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3038 goto out;
3039 }
3040
David Aherne8478e82018-04-17 17:33:13 -07003041 if (cfg->fc_type > RTN_MAX) {
3042 NL_SET_ERR_MSG(extack, "Invalid route type");
3043 goto out;
3044 }
3045
David Ahernd5d531c2017-05-21 10:12:05 -06003046 if (cfg->fc_dst_len > 128) {
3047 NL_SET_ERR_MSG(extack, "Invalid prefix length");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003048 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003049 }
3050 if (cfg->fc_src_len > 128) {
3051 NL_SET_ERR_MSG(extack, "Invalid source address length");
3052 goto out;
3053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054#ifndef CONFIG_IPV6_SUBTREES
David Ahernd5d531c2017-05-21 10:12:05 -06003055 if (cfg->fc_src_len) {
3056 NL_SET_ERR_MSG(extack,
3057 "Specifying source address requires IPV6_SUBTREES to be enabled");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003058 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060#endif
David Ahernfc1e64e2018-01-25 16:55:09 -08003061
Matti Vaittinend71314b2011-11-14 00:14:49 +00003062 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05003063 if (cfg->fc_nlinfo.nlh &&
3064 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00003065 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05003066 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00003067 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00003068 table = fib6_new_table(net, cfg->fc_table);
3069 }
3070 } else {
3071 table = fib6_new_table(net, cfg->fc_table);
3072 }
David S. Miller38308472011-12-03 18:02:47 -05003073
3074 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003075 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07003076
David Ahern93531c62018-04-17 17:33:25 -07003077 err = -ENOMEM;
3078 rt = fib6_info_alloc(gfp_flags);
3079 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 goto out;
David Ahern93531c62018-04-17 17:33:25 -07003081
David Ahernd7e774f2018-11-06 12:51:15 -08003082 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3083 extack);
David Ahern767a2212018-10-04 20:07:51 -07003084 if (IS_ERR(rt->fib6_metrics)) {
3085 err = PTR_ERR(rt->fib6_metrics);
Eric Dumazetfda21d42018-10-05 09:17:50 -07003086 /* Do not leave garbage there. */
3087 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
David Ahern767a2212018-10-04 20:07:51 -07003088 goto out;
3089 }
3090
David Ahern93531c62018-04-17 17:33:25 -07003091 if (cfg->fc_flags & RTF_ADDRCONF)
3092 rt->dst_nocount = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Gao feng1716a962012-04-06 00:13:10 +00003094 if (cfg->fc_flags & RTF_EXPIRES)
David Ahern14895682018-04-17 17:33:17 -07003095 fib6_set_expires(rt, jiffies +
Gao feng1716a962012-04-06 00:13:10 +00003096 clock_t_to_jiffies(cfg->fc_expires));
3097 else
David Ahern14895682018-04-17 17:33:17 -07003098 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Thomas Graf86872cb2006-08-22 00:01:08 -07003100 if (cfg->fc_protocol == RTPROT_UNSPEC)
3101 cfg->fc_protocol = RTPROT_BOOT;
David Ahern93c2fb22018-04-18 15:38:59 -07003102 rt->fib6_protocol = cfg->fc_protocol;
Thomas Graf86872cb2006-08-22 00:01:08 -07003103
David Ahern83c442512019-03-27 20:53:50 -07003104 rt->fib6_table = table;
3105 rt->fib6_metric = cfg->fc_metric;
3106 rt->fib6_type = cfg->fc_type;
David Ahern2b2450c2019-03-27 20:53:52 -07003107 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003108
David Ahern93c2fb22018-04-18 15:38:59 -07003109 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3110 rt->fib6_dst.plen = cfg->fc_dst_len;
3111 if (rt->fib6_dst.plen == 128)
David Ahern3b6761d2018-04-17 17:33:20 -07003112 rt->dst_host = true;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01003113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07003115 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3116 rt->fib6_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117#endif
David Ahern83c442512019-03-27 20:53:50 -07003118 err = fib6_nh_init(net, &rt->fib6_nh, cfg, gfp_flags, extack);
3119 if (err)
3120 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
3122 /* We cannot add true routes via loopback here,
David Ahern83c442512019-03-27 20:53:50 -07003123 * they would result in kernel looping; promote them to reject routes
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 */
David Ahern83c442512019-03-27 20:53:50 -07003125 addr_type = ipv6_addr_type(&cfg->fc_dst);
David Ahernad1601a2019-03-27 20:53:56 -07003126 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh.fib_nh_dev, addr_type))
David Ahern83c442512019-03-27 20:53:50 -07003127 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
David Ahern955ec4c2018-01-24 19:45:29 -08003128
Daniel Walterc3968a82011-04-13 21:10:57 +00003129 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
David Ahern83c442512019-03-27 20:53:50 -07003130 struct net_device *dev = fib6_info_nh_dev(rt);
3131
Daniel Walterc3968a82011-04-13 21:10:57 +00003132 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
David Ahernd5d531c2017-05-21 10:12:05 -06003133 NL_SET_ERR_MSG(extack, "Invalid source address");
Daniel Walterc3968a82011-04-13 21:10:57 +00003134 err = -EINVAL;
3135 goto out;
3136 }
David Ahern93c2fb22018-04-18 15:38:59 -07003137 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3138 rt->fib6_prefsrc.plen = 128;
Daniel Walterc3968a82011-04-13 21:10:57 +00003139 } else
David Ahern93c2fb22018-04-18 15:38:59 -07003140 rt->fib6_prefsrc.plen = 0;
Daniel Walterc3968a82011-04-13 21:10:57 +00003141
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003142 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143out:
David Ahern93531c62018-04-17 17:33:25 -07003144 fib6_info_release(rt);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003145 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003146}
3147
David Ahernacb54e32018-04-17 17:33:22 -07003148int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003149 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003150{
David Ahern8d1c8022018-04-17 17:33:26 -07003151 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003152 int err;
3153
David Ahernacb54e32018-04-17 17:33:22 -07003154 rt = ip6_route_info_create(cfg, gfp_flags, extack);
David Ahernd4ead6b2018-04-17 17:33:16 -07003155 if (IS_ERR(rt))
3156 return PTR_ERR(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003157
David Ahernd4ead6b2018-04-17 17:33:16 -07003158 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
David Ahern93531c62018-04-17 17:33:25 -07003159 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 return err;
3162}
3163
David Ahern8d1c8022018-04-17 17:33:26 -07003164static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
David Ahernafb1d4b52018-04-17 17:33:11 -07003166 struct net *net = info->nl_net;
Thomas Grafc71099a2006-08-04 23:20:06 -07003167 struct fib6_table *table;
David Ahernafb1d4b52018-04-17 17:33:11 -07003168 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
David Ahern421842e2018-04-17 17:33:18 -07003170 if (rt == net->ipv6.fib6_null_entry) {
Gao feng6825a262012-09-19 19:25:34 +00003171 err = -ENOENT;
3172 goto out;
3173 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07003174
David Ahern93c2fb22018-04-18 15:38:59 -07003175 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003176 spin_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07003177 err = fib6_del(rt, info);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003178 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Gao feng6825a262012-09-19 19:25:34 +00003180out:
David Ahern93531c62018-04-17 17:33:25 -07003181 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 return err;
3183}
3184
David Ahern8d1c8022018-04-17 17:33:26 -07003185int ip6_del_rt(struct net *net, struct fib6_info *rt)
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003186{
David Ahernafb1d4b52018-04-17 17:33:11 -07003187 struct nl_info info = { .nl_net = net };
3188
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003189 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003190}
3191
David Ahern8d1c8022018-04-17 17:33:26 -07003192static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
David Ahern0ae81332017-02-02 12:37:08 -08003193{
3194 struct nl_info *info = &cfg->fc_nlinfo;
WANG Conge3330032017-02-27 16:07:43 -08003195 struct net *net = info->nl_net;
David Ahern16a16cd2017-02-02 12:37:11 -08003196 struct sk_buff *skb = NULL;
David Ahern0ae81332017-02-02 12:37:08 -08003197 struct fib6_table *table;
WANG Conge3330032017-02-27 16:07:43 -08003198 int err = -ENOENT;
David Ahern0ae81332017-02-02 12:37:08 -08003199
David Ahern421842e2018-04-17 17:33:18 -07003200 if (rt == net->ipv6.fib6_null_entry)
WANG Conge3330032017-02-27 16:07:43 -08003201 goto out_put;
David Ahern93c2fb22018-04-18 15:38:59 -07003202 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003203 spin_lock_bh(&table->tb6_lock);
David Ahern0ae81332017-02-02 12:37:08 -08003204
David Ahern93c2fb22018-04-18 15:38:59 -07003205 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
David Ahern8d1c8022018-04-17 17:33:26 -07003206 struct fib6_info *sibling, *next_sibling;
David Ahern0ae81332017-02-02 12:37:08 -08003207
David Ahern16a16cd2017-02-02 12:37:11 -08003208 /* prefer to send a single notification with all hops */
3209 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3210 if (skb) {
3211 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3212
David Ahernd4ead6b2018-04-17 17:33:16 -07003213 if (rt6_fill_node(net, skb, rt, NULL,
David Ahern16a16cd2017-02-02 12:37:11 -08003214 NULL, NULL, 0, RTM_DELROUTE,
3215 info->portid, seq, 0) < 0) {
3216 kfree_skb(skb);
3217 skb = NULL;
3218 } else
3219 info->skip_notify = 1;
3220 }
3221
David Ahern0ae81332017-02-02 12:37:08 -08003222 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07003223 &rt->fib6_siblings,
3224 fib6_siblings) {
David Ahern0ae81332017-02-02 12:37:08 -08003225 err = fib6_del(sibling, info);
3226 if (err)
WANG Conge3330032017-02-27 16:07:43 -08003227 goto out_unlock;
David Ahern0ae81332017-02-02 12:37:08 -08003228 }
3229 }
3230
3231 err = fib6_del(rt, info);
WANG Conge3330032017-02-27 16:07:43 -08003232out_unlock:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003233 spin_unlock_bh(&table->tb6_lock);
WANG Conge3330032017-02-27 16:07:43 -08003234out_put:
David Ahern93531c62018-04-17 17:33:25 -07003235 fib6_info_release(rt);
David Ahern16a16cd2017-02-02 12:37:11 -08003236
3237 if (skb) {
WANG Conge3330032017-02-27 16:07:43 -08003238 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
David Ahern16a16cd2017-02-02 12:37:11 -08003239 info->nlh, gfp_any());
3240 }
David Ahern0ae81332017-02-02 12:37:08 -08003241 return err;
3242}
3243
David Ahern23fb93a2018-04-17 17:33:23 -07003244static int ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
3245{
3246 int rc = -ESRCH;
3247
3248 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3249 goto out;
3250
3251 if (cfg->fc_flags & RTF_GATEWAY &&
3252 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3253 goto out;
Xin Long761f6022018-11-14 00:48:28 +08003254
3255 rc = rt6_remove_exception_rt(rt);
David Ahern23fb93a2018-04-17 17:33:23 -07003256out:
3257 return rc;
3258}
3259
David Ahern333c4302017-05-21 10:12:04 -06003260static int ip6_route_del(struct fib6_config *cfg,
3261 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
David Ahern8d1c8022018-04-17 17:33:26 -07003263 struct rt6_info *rt_cache;
Thomas Grafc71099a2006-08-04 23:20:06 -07003264 struct fib6_table *table;
David Ahern8d1c8022018-04-17 17:33:26 -07003265 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 int err = -ESRCH;
3268
Daniel Lezcano55786892008-03-04 13:47:47 -08003269 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David Ahernd5d531c2017-05-21 10:12:05 -06003270 if (!table) {
3271 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Grafc71099a2006-08-04 23:20:06 -07003272 return err;
David Ahernd5d531c2017-05-21 10:12:05 -06003273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
Wei Wang66f5d6c2017-10-06 12:06:10 -07003275 rcu_read_lock();
Thomas Grafc71099a2006-08-04 23:20:06 -07003276
3277 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07003278 &cfg->fc_dst, cfg->fc_dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07003279 &cfg->fc_src, cfg->fc_src_len,
Wei Wang2b760fc2017-10-06 12:06:03 -07003280 !(cfg->fc_flags & RTF_CACHE));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 if (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003283 for_each_fib6_node_rt_rcu(fn) {
David Ahernad1601a2019-03-27 20:53:56 -07003284 struct fib6_nh *nh;
3285
Wei Wang2b760fc2017-10-06 12:06:03 -07003286 if (cfg->fc_flags & RTF_CACHE) {
David Ahern23fb93a2018-04-17 17:33:23 -07003287 int rc;
3288
Wei Wang2b760fc2017-10-06 12:06:03 -07003289 rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst,
3290 &cfg->fc_src);
David Ahern23fb93a2018-04-17 17:33:23 -07003291 if (rt_cache) {
3292 rc = ip6_del_cached_rt(rt_cache, cfg);
Eric Dumazet9e575012018-05-09 10:05:46 -07003293 if (rc != -ESRCH) {
3294 rcu_read_unlock();
David Ahern23fb93a2018-04-17 17:33:23 -07003295 return rc;
Eric Dumazet9e575012018-05-09 10:05:46 -07003296 }
David Ahern23fb93a2018-04-17 17:33:23 -07003297 }
3298 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07003299 }
David Ahernad1601a2019-03-27 20:53:56 -07003300
3301 nh = &rt->fib6_nh;
Thomas Graf86872cb2006-08-22 00:01:08 -07003302 if (cfg->fc_ifindex &&
David Ahernad1601a2019-03-27 20:53:56 -07003303 (!nh->fib_nh_dev ||
3304 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07003306 if (cfg->fc_flags & RTF_GATEWAY &&
David Ahernad1601a2019-03-27 20:53:56 -07003307 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003309 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003311 if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol)
Mantas Mc2ed1882016-12-16 10:30:59 +02003312 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07003313 if (!fib6_info_hold_safe(rt))
3314 continue;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003315 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
David Ahern0ae81332017-02-02 12:37:08 -08003317 /* if gateway was specified only delete the one hop */
3318 if (cfg->fc_flags & RTF_GATEWAY)
3319 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3320
3321 return __ip6_del_rt_siblings(rt, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 }
3323 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003324 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
3326 return err;
3327}
3328
David S. Miller6700c272012-07-17 03:29:28 -07003329static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003330{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003331 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07003332 struct rt6_info *rt, *nrt = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07003333 struct ndisc_options ndopts;
3334 struct inet6_dev *in6_dev;
3335 struct neighbour *neigh;
David Aherna68886a2018-04-20 15:38:02 -07003336 struct fib6_info *from;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003337 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07003338 int optlen, on_link;
3339 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07003340
Simon Horman29a3cad2013-05-28 20:34:26 +00003341 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003342 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07003343
3344 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07003345 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003346 return;
3347 }
3348
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003349 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07003350
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003351 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003352 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003353 return;
3354 }
3355
David S. Miller6e157b62012-07-12 00:05:02 -07003356 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003357 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003358 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003359 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07003360 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003361 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003362 return;
3363 }
3364
3365 in6_dev = __in6_dev_get(skb->dev);
3366 if (!in6_dev)
3367 return;
3368 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3369 return;
3370
3371 /* RFC2461 8.1:
3372 * The IP source address of the Redirect MUST be the same as the current
3373 * first-hop router for the specified ICMP Destination Address.
3374 */
3375
Alexander Aringf997c552016-06-15 21:20:23 +02003376 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003377 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3378 return;
3379 }
David S. Miller6e157b62012-07-12 00:05:02 -07003380
3381 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07003382 if (ndopts.nd_opts_tgt_lladdr) {
3383 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3384 skb->dev);
3385 if (!lladdr) {
3386 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3387 return;
3388 }
3389 }
3390
David S. Miller6e157b62012-07-12 00:05:02 -07003391 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01003392 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07003393 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
3394 return;
3395 }
3396
3397 /* Redirect received -> path was valid.
3398 * Look, redirects are sent only in response to data packets,
3399 * so that this nexthop apparently is reachable. --ANK
3400 */
Julian Anastasov0dec8792017-02-06 23:14:16 +02003401 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
David S. Miller6e157b62012-07-12 00:05:02 -07003402
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003403 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07003404 if (!neigh)
3405 return;
3406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 /*
3408 * We have finally decided to accept it.
3409 */
3410
Alexander Aringf997c552016-06-15 21:20:23 +02003411 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 NEIGH_UPDATE_F_WEAK_OVERRIDE|
3413 NEIGH_UPDATE_F_OVERRIDE|
3414 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02003415 NEIGH_UPDATE_F_ISROUTER)),
3416 NDISC_REDIRECT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
David Ahern4d85cd02018-04-20 15:37:59 -07003418 rcu_read_lock();
David Aherna68886a2018-04-20 15:38:02 -07003419 from = rcu_dereference(rt->from);
Wei Wange873e4b2018-07-21 20:56:32 -07003420 /* This fib6_info_hold() is safe here because we hold reference to rt
3421 * and rt already holds reference to fib6_info.
3422 */
David Ahern8a14e462018-04-23 11:32:07 -07003423 fib6_info_hold(from);
David Ahern4d85cd02018-04-20 15:37:59 -07003424 rcu_read_unlock();
David Ahern8a14e462018-04-23 11:32:07 -07003425
3426 nrt = ip6_rt_cache_alloc(from, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05003427 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 goto out;
3429
3430 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
3431 if (on_link)
3432 nrt->rt6i_flags &= ~RTF_GATEWAY;
3433
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003434 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Wei Wang2b760fc2017-10-06 12:06:03 -07003436 /* No need to remove rt from the exception table if rt is
3437 * a cached route because rt6_insert_exception() will
3438 * takes care of it
3439 */
David Ahern8a14e462018-04-23 11:32:07 -07003440 if (rt6_insert_exception(nrt, from)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07003441 dst_release_immediate(&nrt->dst);
3442 goto out;
3443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Changli Gaod8d1f302010-06-10 23:31:35 -07003445 netevent.old = &rt->dst;
3446 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003447 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00003448 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07003449 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
3450
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451out:
David Ahern8a14e462018-04-23 11:32:07 -07003452 fib6_info_release(from);
David S. Millere8599ff2012-07-11 23:43:53 -07003453 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07003454}
3455
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003456#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -07003457static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003458 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07003459 const struct in6_addr *gwaddr,
3460 struct net_device *dev)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003461{
David Ahern830218c2016-10-24 10:52:35 -07003462 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3463 int ifindex = dev->ifindex;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003464 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07003465 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003466 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003467
David Ahern830218c2016-10-24 10:52:35 -07003468 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05003469 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003470 return NULL;
3471
Wei Wang66f5d6c2017-10-06 12:06:10 -07003472 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07003473 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003474 if (!fn)
3475 goto out;
3476
Wei Wang66f5d6c2017-10-06 12:06:10 -07003477 for_each_fib6_node_rt_rcu(fn) {
David Ahernad1601a2019-03-27 20:53:56 -07003478 if (rt->fib6_nh.fib_nh_dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003479 continue;
David Ahern2b2450c2019-03-27 20:53:52 -07003480 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
David Ahernbdf00462019-04-05 16:30:26 -07003481 !rt->fib6_nh.fib_nh_gw_family)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003482 continue;
David Ahernad1601a2019-03-27 20:53:56 -07003483 if (!ipv6_addr_equal(&rt->fib6_nh.fib_nh_gw6, gwaddr))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003484 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07003485 if (!fib6_info_hold_safe(rt))
3486 continue;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003487 break;
3488 }
3489out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003490 rcu_read_unlock();
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003491 return rt;
3492}
3493
David Ahern8d1c8022018-04-17 17:33:26 -07003494static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003495 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07003496 const struct in6_addr *gwaddr,
3497 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +00003498 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003499{
Thomas Graf86872cb2006-08-22 00:01:08 -07003500 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08003501 .fc_metric = IP6_RT_PRIO_USER,
David Ahern830218c2016-10-24 10:52:35 -07003502 .fc_ifindex = dev->ifindex,
Thomas Graf86872cb2006-08-22 00:01:08 -07003503 .fc_dst_len = prefixlen,
3504 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
3505 RTF_UP | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08003506 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07003507 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003508 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08003509 .fc_nlinfo.nlh = NULL,
3510 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07003511 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003512
David Ahern830218c2016-10-24 10:52:35 -07003513 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003514 cfg.fc_dst = *prefix;
3515 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07003516
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08003517 /* We should treat it as a default route if prefix length is 0. */
3518 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07003519 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003520
David Ahernacb54e32018-04-17 17:33:22 -07003521 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003522
David Ahern830218c2016-10-24 10:52:35 -07003523 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003524}
3525#endif
3526
David Ahern8d1c8022018-04-17 17:33:26 -07003527struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07003528 const struct in6_addr *addr,
3529 struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003530{
David Ahern830218c2016-10-24 10:52:35 -07003531 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
David Ahern8d1c8022018-04-17 17:33:26 -07003532 struct fib6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07003533 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
David Ahernafb1d4b52018-04-17 17:33:11 -07003535 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05003536 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003537 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Wei Wang66f5d6c2017-10-06 12:06:10 -07003539 rcu_read_lock();
3540 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Ahernad1601a2019-03-27 20:53:56 -07003541 struct fib6_nh *nh = &rt->fib6_nh;
3542
3543 if (dev == nh->fib_nh_dev &&
David Ahern93c2fb22018-04-18 15:38:59 -07003544 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Ahernad1601a2019-03-27 20:53:56 -07003545 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 break;
3547 }
Wei Wange873e4b2018-07-21 20:56:32 -07003548 if (rt && !fib6_info_hold_safe(rt))
3549 rt = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003550 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 return rt;
3552}
3553
David Ahern8d1c8022018-04-17 17:33:26 -07003554struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07003555 const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08003556 struct net_device *dev,
3557 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
Thomas Graf86872cb2006-08-22 00:01:08 -07003559 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07003560 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08003561 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07003562 .fc_ifindex = dev->ifindex,
3563 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
3564 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08003565 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07003566 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003567 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08003568 .fc_nlinfo.nlh = NULL,
David Ahernafb1d4b52018-04-17 17:33:11 -07003569 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07003570 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003572 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
David Ahernacb54e32018-04-17 17:33:22 -07003574 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
David Ahern830218c2016-10-24 10:52:35 -07003575 struct fib6_table *table;
3576
3577 table = fib6_get_table(dev_net(dev), cfg.fc_table);
3578 if (table)
3579 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
David Ahernafb1d4b52018-04-17 17:33:11 -07003582 return rt6_get_dflt_router(net, gwaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583}
3584
David Ahernafb1d4b52018-04-17 17:33:11 -07003585static void __rt6_purge_dflt_routers(struct net *net,
3586 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
David Ahern8d1c8022018-04-17 17:33:26 -07003588 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
3590restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003591 rcu_read_lock();
3592 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Aherndcd1f572018-04-18 15:39:05 -07003593 struct net_device *dev = fib6_info_nh_dev(rt);
3594 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
3595
David Ahern93c2fb22018-04-18 15:38:59 -07003596 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
Wei Wange873e4b2018-07-21 20:56:32 -07003597 (!idev || idev->cnf.accept_ra != 2) &&
3598 fib6_info_hold_safe(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07003599 rcu_read_unlock();
3600 ip6_del_rt(net, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 goto restart;
3602 }
3603 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003604 rcu_read_unlock();
David Ahern830218c2016-10-24 10:52:35 -07003605
3606 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3607}
3608
3609void rt6_purge_dflt_routers(struct net *net)
3610{
3611 struct fib6_table *table;
3612 struct hlist_head *head;
3613 unsigned int h;
3614
3615 rcu_read_lock();
3616
3617 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3618 head = &net->ipv6.fib_table_hash[h];
3619 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3620 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
David Ahernafb1d4b52018-04-17 17:33:11 -07003621 __rt6_purge_dflt_routers(net, table);
David Ahern830218c2016-10-24 10:52:35 -07003622 }
3623 }
3624
3625 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626}
3627
Daniel Lezcano55786892008-03-04 13:47:47 -08003628static void rtmsg_to_fib6_config(struct net *net,
3629 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07003630 struct fib6_config *cfg)
3631{
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07003632 *cfg = (struct fib6_config){
3633 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
3634 : RT6_TABLE_MAIN,
3635 .fc_ifindex = rtmsg->rtmsg_ifindex,
David Ahern67f69512019-03-21 05:21:34 -07003636 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07003637 .fc_expires = rtmsg->rtmsg_info,
3638 .fc_dst_len = rtmsg->rtmsg_dst_len,
3639 .fc_src_len = rtmsg->rtmsg_src_len,
3640 .fc_flags = rtmsg->rtmsg_flags,
3641 .fc_type = rtmsg->rtmsg_type,
Thomas Graf86872cb2006-08-22 00:01:08 -07003642
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07003643 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07003644
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07003645 .fc_dst = rtmsg->rtmsg_dst,
3646 .fc_src = rtmsg->rtmsg_src,
3647 .fc_gateway = rtmsg->rtmsg_gateway,
3648 };
Thomas Graf86872cb2006-08-22 00:01:08 -07003649}
3650
Daniel Lezcano55786892008-03-04 13:47:47 -08003651int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652{
Thomas Graf86872cb2006-08-22 00:01:08 -07003653 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 struct in6_rtmsg rtmsg;
3655 int err;
3656
Ian Morris67ba4152014-08-24 21:53:10 +01003657 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 case SIOCADDRT: /* Add a route */
3659 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00003660 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 return -EPERM;
3662 err = copy_from_user(&rtmsg, arg,
3663 sizeof(struct in6_rtmsg));
3664 if (err)
3665 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07003666
Daniel Lezcano55786892008-03-04 13:47:47 -08003667 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07003668
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 rtnl_lock();
3670 switch (cmd) {
3671 case SIOCADDRT:
David Ahernacb54e32018-04-17 17:33:22 -07003672 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 break;
3674 case SIOCDELRT:
David Ahern333c4302017-05-21 10:12:04 -06003675 err = ip6_route_del(&cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 break;
3677 default:
3678 err = -EINVAL;
3679 }
3680 rtnl_unlock();
3681
3682 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
3685 return -EINVAL;
3686}
3687
3688/*
3689 * Drop the packet on the floor
3690 */
3691
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07003692static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003694 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00003695 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003696 switch (ipstats_mib_noroutes) {
3697 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07003698 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00003699 if (type == IPV6_ADDR_ANY) {
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -04003700 IP6_INC_STATS(dev_net(dst->dev),
3701 __in6_dev_get_safely(skb->dev),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07003702 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003703 break;
3704 }
3705 /* FALLTHROUGH */
3706 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07003707 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
3708 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003709 break;
3710 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00003711 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 kfree_skb(skb);
3713 return 0;
3714}
3715
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003716static int ip6_pkt_discard(struct sk_buff *skb)
3717{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003718 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003719}
3720
Eric W. Biedermanede20592015-10-07 16:48:47 -05003721static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722{
Eric Dumazetadf30902009-06-02 05:19:30 +00003723 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003724 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725}
3726
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003727static int ip6_pkt_prohibit(struct sk_buff *skb)
3728{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003729 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003730}
3731
Eric W. Biedermanede20592015-10-07 16:48:47 -05003732static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003733{
Eric Dumazetadf30902009-06-02 05:19:30 +00003734 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003735 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003736}
3737
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738/*
3739 * Allocate a dst for local (unicast / anycast) address.
3740 */
3741
David Ahern360a9882018-04-18 15:39:00 -07003742struct fib6_info *addrconf_f6i_alloc(struct net *net,
3743 struct inet6_dev *idev,
3744 const struct in6_addr *addr,
3745 bool anycast, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746{
David Ahernc7a1ce32019-03-21 05:21:35 -07003747 struct fib6_config cfg = {
3748 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
3749 .fc_ifindex = idev->dev->ifindex,
3750 .fc_flags = RTF_UP | RTF_ADDRCONF | RTF_NONEXTHOP,
3751 .fc_dst = *addr,
3752 .fc_dst_len = 128,
3753 .fc_protocol = RTPROT_KERNEL,
3754 .fc_nlinfo.nl_net = net,
3755 .fc_ignore_dev_down = true,
3756 };
David Ahern5f02ce242016-09-10 12:09:54 -07003757
David Aherne8478e82018-04-17 17:33:13 -07003758 if (anycast) {
David Ahernc7a1ce32019-03-21 05:21:35 -07003759 cfg.fc_type = RTN_ANYCAST;
3760 cfg.fc_flags |= RTF_ANYCAST;
David Aherne8478e82018-04-17 17:33:13 -07003761 } else {
David Ahernc7a1ce32019-03-21 05:21:35 -07003762 cfg.fc_type = RTN_LOCAL;
3763 cfg.fc_flags |= RTF_LOCAL;
David Aherne8478e82018-04-17 17:33:13 -07003764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
David Ahernc7a1ce32019-03-21 05:21:35 -07003766 return ip6_route_info_create(&cfg, gfp_flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767}
3768
Daniel Walterc3968a82011-04-13 21:10:57 +00003769/* remove deleted ip from prefsrc entries */
3770struct arg_dev_net_ip {
3771 struct net_device *dev;
3772 struct net *net;
3773 struct in6_addr *addr;
3774};
3775
David Ahern8d1c8022018-04-17 17:33:26 -07003776static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
Daniel Walterc3968a82011-04-13 21:10:57 +00003777{
3778 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3779 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3780 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3781
David Ahernad1601a2019-03-27 20:53:56 -07003782 if (((void *)rt->fib6_nh.fib_nh_dev == dev || !dev) &&
David Ahern421842e2018-04-17 17:33:18 -07003783 rt != net->ipv6.fib6_null_entry &&
David Ahern93c2fb22018-04-18 15:38:59 -07003784 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
Wei Wang60006a42017-10-06 12:05:58 -07003785 spin_lock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00003786 /* remove prefsrc entry */
David Ahern93c2fb22018-04-18 15:38:59 -07003787 rt->fib6_prefsrc.plen = 0;
Wei Wang60006a42017-10-06 12:05:58 -07003788 spin_unlock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00003789 }
3790 return 0;
3791}
3792
3793void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
3794{
3795 struct net *net = dev_net(ifp->idev->dev);
3796 struct arg_dev_net_ip adni = {
3797 .dev = ifp->idev->dev,
3798 .net = net,
3799 .addr = &ifp->addr,
3800 };
Li RongQing0c3584d2013-12-27 16:32:38 +08003801 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00003802}
3803
David Ahern2b2450c2019-03-27 20:53:52 -07003804#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
Duan Jiongbe7a0102014-05-15 15:56:14 +08003805
3806/* Remove routers and update dst entries when gateway turn into host. */
David Ahern8d1c8022018-04-17 17:33:26 -07003807static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
Duan Jiongbe7a0102014-05-15 15:56:14 +08003808{
3809 struct in6_addr *gateway = (struct in6_addr *)arg;
3810
David Ahern93c2fb22018-04-18 15:38:59 -07003811 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Ahernbdf00462019-04-05 16:30:26 -07003812 rt->fib6_nh.fib_nh_gw_family &&
David Ahernad1601a2019-03-27 20:53:56 -07003813 ipv6_addr_equal(gateway, &rt->fib6_nh.fib_nh_gw6)) {
Duan Jiongbe7a0102014-05-15 15:56:14 +08003814 return -1;
3815 }
Wei Wangb16cb452017-10-06 12:06:00 -07003816
3817 /* Further clean up cached routes in exception table.
3818 * This is needed because cached route may have a different
3819 * gateway than its 'parent' in the case of an ip redirect.
3820 */
3821 rt6_exceptions_clean_tohost(rt, gateway);
3822
Duan Jiongbe7a0102014-05-15 15:56:14 +08003823 return 0;
3824}
3825
3826void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
3827{
3828 fib6_clean_all(net, fib6_clean_tohost, gateway);
3829}
3830
Ido Schimmel2127d952018-01-07 12:45:03 +02003831struct arg_netdev_event {
3832 const struct net_device *dev;
Ido Schimmel4c981e22018-01-07 12:45:04 +02003833 union {
3834 unsigned int nh_flags;
3835 unsigned long event;
3836 };
Ido Schimmel2127d952018-01-07 12:45:03 +02003837};
3838
David Ahern8d1c8022018-04-17 17:33:26 -07003839static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003840{
David Ahern8d1c8022018-04-17 17:33:26 -07003841 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003842 struct fib6_node *fn;
3843
David Ahern93c2fb22018-04-18 15:38:59 -07003844 fn = rcu_dereference_protected(rt->fib6_node,
3845 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003846 iter = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -07003847 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003848 while (iter) {
David Ahern93c2fb22018-04-18 15:38:59 -07003849 if (iter->fib6_metric == rt->fib6_metric &&
David Ahern33bd5ac2018-07-03 14:36:21 -07003850 rt6_qualify_for_ecmp(iter))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003851 return iter;
David Ahern8fb11a92018-05-04 13:54:24 -07003852 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -07003853 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003854 }
3855
3856 return NULL;
3857}
3858
David Ahern8d1c8022018-04-17 17:33:26 -07003859static bool rt6_is_dead(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003860{
David Ahernad1601a2019-03-27 20:53:56 -07003861 if (rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD ||
3862 (rt->fib6_nh.fib_nh_flags & RTNH_F_LINKDOWN &&
3863 ip6_ignore_linkdown(rt->fib6_nh.fib_nh_dev)))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003864 return true;
3865
3866 return false;
3867}
3868
David Ahern8d1c8022018-04-17 17:33:26 -07003869static int rt6_multipath_total_weight(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003870{
David Ahern8d1c8022018-04-17 17:33:26 -07003871 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003872 int total = 0;
3873
3874 if (!rt6_is_dead(rt))
David Ahernad1601a2019-03-27 20:53:56 -07003875 total += rt->fib6_nh.fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003876
David Ahern93c2fb22018-04-18 15:38:59 -07003877 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003878 if (!rt6_is_dead(iter))
David Ahernad1601a2019-03-27 20:53:56 -07003879 total += iter->fib6_nh.fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003880 }
3881
3882 return total;
3883}
3884
David Ahern8d1c8022018-04-17 17:33:26 -07003885static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003886{
3887 int upper_bound = -1;
3888
3889 if (!rt6_is_dead(rt)) {
David Ahernad1601a2019-03-27 20:53:56 -07003890 *weight += rt->fib6_nh.fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003891 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
3892 total) - 1;
3893 }
David Ahernad1601a2019-03-27 20:53:56 -07003894 atomic_set(&rt->fib6_nh.fib_nh_upper_bound, upper_bound);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003895}
3896
David Ahern8d1c8022018-04-17 17:33:26 -07003897static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003898{
David Ahern8d1c8022018-04-17 17:33:26 -07003899 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003900 int weight = 0;
3901
3902 rt6_upper_bound_set(rt, &weight, total);
3903
David Ahern93c2fb22018-04-18 15:38:59 -07003904 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003905 rt6_upper_bound_set(iter, &weight, total);
3906}
3907
David Ahern8d1c8022018-04-17 17:33:26 -07003908void rt6_multipath_rebalance(struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003909{
David Ahern8d1c8022018-04-17 17:33:26 -07003910 struct fib6_info *first;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003911 int total;
3912
3913 /* In case the entire multipath route was marked for flushing,
3914 * then there is no need to rebalance upon the removal of every
3915 * sibling route.
3916 */
David Ahern93c2fb22018-04-18 15:38:59 -07003917 if (!rt->fib6_nsiblings || rt->should_flush)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003918 return;
3919
3920 /* During lookup routes are evaluated in order, so we need to
3921 * make sure upper bounds are assigned from the first sibling
3922 * onwards.
3923 */
3924 first = rt6_multipath_first_sibling(rt);
3925 if (WARN_ON_ONCE(!first))
3926 return;
3927
3928 total = rt6_multipath_total_weight(first);
3929 rt6_multipath_upper_bound_set(first, total);
3930}
3931
David Ahern8d1c8022018-04-17 17:33:26 -07003932static int fib6_ifup(struct fib6_info *rt, void *p_arg)
Ido Schimmel2127d952018-01-07 12:45:03 +02003933{
3934 const struct arg_netdev_event *arg = p_arg;
David Ahern7aef6852018-04-17 17:33:10 -07003935 struct net *net = dev_net(arg->dev);
Ido Schimmel2127d952018-01-07 12:45:03 +02003936
David Ahernad1601a2019-03-27 20:53:56 -07003937 if (rt != net->ipv6.fib6_null_entry &&
3938 rt->fib6_nh.fib_nh_dev == arg->dev) {
3939 rt->fib6_nh.fib_nh_flags &= ~arg->nh_flags;
David Ahern7aef6852018-04-17 17:33:10 -07003940 fib6_update_sernum_upto_root(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003941 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02003942 }
Ido Schimmel2127d952018-01-07 12:45:03 +02003943
3944 return 0;
3945}
3946
3947void rt6_sync_up(struct net_device *dev, unsigned int nh_flags)
3948{
3949 struct arg_netdev_event arg = {
3950 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02003951 {
3952 .nh_flags = nh_flags,
3953 },
Ido Schimmel2127d952018-01-07 12:45:03 +02003954 };
3955
3956 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
3957 arg.nh_flags |= RTNH_F_LINKDOWN;
3958
3959 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
3960}
3961
David Ahern8d1c8022018-04-17 17:33:26 -07003962static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02003963 const struct net_device *dev)
3964{
David Ahern8d1c8022018-04-17 17:33:26 -07003965 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003966
David Ahernad1601a2019-03-27 20:53:56 -07003967 if (rt->fib6_nh.fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003968 return true;
David Ahern93c2fb22018-04-18 15:38:59 -07003969 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahernad1601a2019-03-27 20:53:56 -07003970 if (iter->fib6_nh.fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003971 return true;
3972
3973 return false;
3974}
3975
David Ahern8d1c8022018-04-17 17:33:26 -07003976static void rt6_multipath_flush(struct fib6_info *rt)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003977{
David Ahern8d1c8022018-04-17 17:33:26 -07003978 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003979
3980 rt->should_flush = 1;
David Ahern93c2fb22018-04-18 15:38:59 -07003981 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003982 iter->should_flush = 1;
3983}
3984
David Ahern8d1c8022018-04-17 17:33:26 -07003985static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02003986 const struct net_device *down_dev)
3987{
David Ahern8d1c8022018-04-17 17:33:26 -07003988 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003989 unsigned int dead = 0;
3990
David Ahernad1601a2019-03-27 20:53:56 -07003991 if (rt->fib6_nh.fib_nh_dev == down_dev ||
3992 rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003993 dead++;
David Ahern93c2fb22018-04-18 15:38:59 -07003994 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahernad1601a2019-03-27 20:53:56 -07003995 if (iter->fib6_nh.fib_nh_dev == down_dev ||
3996 iter->fib6_nh.fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003997 dead++;
3998
3999 return dead;
4000}
4001
David Ahern8d1c8022018-04-17 17:33:26 -07004002static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004003 const struct net_device *dev,
4004 unsigned int nh_flags)
4005{
David Ahern8d1c8022018-04-17 17:33:26 -07004006 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004007
David Ahernad1601a2019-03-27 20:53:56 -07004008 if (rt->fib6_nh.fib_nh_dev == dev)
4009 rt->fib6_nh.fib_nh_flags |= nh_flags;
David Ahern93c2fb22018-04-18 15:38:59 -07004010 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahernad1601a2019-03-27 20:53:56 -07004011 if (iter->fib6_nh.fib_nh_dev == dev)
4012 iter->fib6_nh.fib_nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004013}
4014
David Aherna1a22c12017-01-18 07:40:36 -08004015/* called with write lock held for table with rt */
David Ahern8d1c8022018-04-17 17:33:26 -07004016static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004018 const struct arg_netdev_event *arg = p_arg;
4019 const struct net_device *dev = arg->dev;
David Ahern7aef6852018-04-17 17:33:10 -07004020 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004021
David Ahern421842e2018-04-17 17:33:18 -07004022 if (rt == net->ipv6.fib6_null_entry)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004023 return 0;
4024
4025 switch (arg->event) {
4026 case NETDEV_UNREGISTER:
David Ahernad1601a2019-03-27 20:53:56 -07004027 return rt->fib6_nh.fib_nh_dev == dev ? -1 : 0;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004028 case NETDEV_DOWN:
Ido Schimmel1de178e2018-01-07 12:45:15 +02004029 if (rt->should_flush)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004030 return -1;
David Ahern93c2fb22018-04-18 15:38:59 -07004031 if (!rt->fib6_nsiblings)
David Ahernad1601a2019-03-27 20:53:56 -07004032 return rt->fib6_nh.fib_nh_dev == dev ? -1 : 0;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004033 if (rt6_multipath_uses_dev(rt, dev)) {
4034 unsigned int count;
4035
4036 count = rt6_multipath_dead_count(rt, dev);
David Ahern93c2fb22018-04-18 15:38:59 -07004037 if (rt->fib6_nsiblings + 1 == count) {
Ido Schimmel1de178e2018-01-07 12:45:15 +02004038 rt6_multipath_flush(rt);
4039 return -1;
4040 }
4041 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4042 RTNH_F_LINKDOWN);
David Ahern7aef6852018-04-17 17:33:10 -07004043 fib6_update_sernum(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004044 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004045 }
4046 return -2;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004047 case NETDEV_CHANGE:
David Ahernad1601a2019-03-27 20:53:56 -07004048 if (rt->fib6_nh.fib_nh_dev != dev ||
David Ahern93c2fb22018-04-18 15:38:59 -07004049 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004050 break;
David Ahernad1601a2019-03-27 20:53:56 -07004051 rt->fib6_nh.fib_nh_flags |= RTNH_F_LINKDOWN;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004052 rt6_multipath_rebalance(rt);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004053 break;
Ido Schimmel2b241362018-01-07 12:45:02 +02004054 }
David S. Millerc159d302011-12-26 15:24:36 -05004055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 return 0;
4057}
4058
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004059void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004061 struct arg_netdev_event arg = {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004062 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004063 {
4064 .event = event,
4065 },
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004066 };
David Ahern7c6bb7d2018-10-11 20:17:21 -07004067 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004068
David Ahern7c6bb7d2018-10-11 20:17:21 -07004069 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4070 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4071 else
4072 fib6_clean_all(net, fib6_ifdown, &arg);
Ido Schimmel4c981e22018-01-07 12:45:04 +02004073}
4074
4075void rt6_disable_ip(struct net_device *dev, unsigned long event)
4076{
4077 rt6_sync_down_dev(dev, event);
4078 rt6_uncached_list_flush_dev(dev_net(dev), dev);
4079 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080}
4081
Eric Dumazet95c96172012-04-15 05:58:06 +00004082struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00004084 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085};
4086
David Ahern8d1c8022018-04-17 17:33:26 -07004087static int rt6_mtu_change_route(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088{
4089 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4090 struct inet6_dev *idev;
4091
4092 /* In IPv6 pmtu discovery is not optional,
4093 so that RTAX_MTU lock cannot disable it.
4094 We still use this lock to block changes
4095 caused by addrconf/ndisc.
4096 */
4097
4098 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05004099 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 return 0;
4101
4102 /* For administrative MTU increase, there is no way to discover
4103 IPv6 PMTU increase, so PMTU increase should be updated here.
4104 Since RFC 1981 doesn't include administrative MTU increase
4105 update PMTU increase is a MUST. (i.e. jumbo frame)
4106 */
David Ahernad1601a2019-03-27 20:53:56 -07004107 if (rt->fib6_nh.fib_nh_dev == arg->dev &&
David Ahernd4ead6b2018-04-17 17:33:16 -07004108 !fib6_metric_locked(rt, RTAX_MTU)) {
4109 u32 mtu = rt->fib6_pmtu;
4110
4111 if (mtu >= arg->mtu ||
4112 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4113 fib6_metric_set(rt, RTAX_MTU, arg->mtu);
4114
Wei Wangf5bbe7e2017-10-06 12:05:59 -07004115 spin_lock_bh(&rt6_exception_lock);
Stefano Brivioe9fa1492018-03-06 11:10:19 +01004116 rt6_exceptions_update_pmtu(idev, rt, arg->mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07004117 spin_unlock_bh(&rt6_exception_lock);
Simon Arlott566cfd82007-07-26 00:09:55 -07004118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 return 0;
4120}
4121
Eric Dumazet95c96172012-04-15 05:58:06 +00004122void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123{
Thomas Grafc71099a2006-08-04 23:20:06 -07004124 struct rt6_mtu_change_arg arg = {
4125 .dev = dev,
4126 .mtu = mtu,
4127 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
Li RongQing0c3584d2013-12-27 16:32:38 +08004129 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130}
4131
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004132static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07004133 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004134 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07004135 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07004136 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004137 [RTA_PRIORITY] = { .type = NLA_U32 },
4138 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004139 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004140 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004141 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4142 [RTA_ENCAP] = { .type = NLA_NESTED },
Xin Long32bc2012015-12-16 17:50:11 +08004143 [RTA_EXPIRES] = { .type = NLA_U32 },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004144 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +08004145 [RTA_MARK] = { .type = NLA_U32 },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004146 [RTA_TABLE] = { .type = NLA_U32 },
Roopa Prabhueacb9382018-05-22 14:03:28 -07004147 [RTA_IP_PROTO] = { .type = NLA_U8 },
4148 [RTA_SPORT] = { .type = NLA_U16 },
4149 [RTA_DPORT] = { .type = NLA_U16 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004150};
4151
4152static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
David Ahern333c4302017-05-21 10:12:04 -06004153 struct fib6_config *cfg,
4154 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155{
Thomas Graf86872cb2006-08-22 00:01:08 -07004156 struct rtmsg *rtm;
4157 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004158 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07004159 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
Johannes Bergfceb6432017-04-12 14:34:07 +02004161 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
David Aherndac9c972018-10-07 20:16:24 -07004162 extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004163 if (err < 0)
4164 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
Thomas Graf86872cb2006-08-22 00:01:08 -07004166 err = -EINVAL;
4167 rtm = nlmsg_data(nlh);
Thomas Graf86872cb2006-08-22 00:01:08 -07004168
Maciej Żenczykowski84db8402018-09-29 23:44:53 -07004169 *cfg = (struct fib6_config){
4170 .fc_table = rtm->rtm_table,
4171 .fc_dst_len = rtm->rtm_dst_len,
4172 .fc_src_len = rtm->rtm_src_len,
4173 .fc_flags = RTF_UP,
4174 .fc_protocol = rtm->rtm_protocol,
4175 .fc_type = rtm->rtm_type,
4176
4177 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
4178 .fc_nlinfo.nlh = nlh,
4179 .fc_nlinfo.nl_net = sock_net(skb->sk),
4180 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004181
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004182 if (rtm->rtm_type == RTN_UNREACHABLE ||
4183 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00004184 rtm->rtm_type == RTN_PROHIBIT ||
4185 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07004186 cfg->fc_flags |= RTF_REJECT;
4187
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00004188 if (rtm->rtm_type == RTN_LOCAL)
4189 cfg->fc_flags |= RTF_LOCAL;
4190
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07004191 if (rtm->rtm_flags & RTM_F_CLONED)
4192 cfg->fc_flags |= RTF_CACHE;
4193
David Ahernfc1e64e2018-01-25 16:55:09 -08004194 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4195
Thomas Graf86872cb2006-08-22 00:01:08 -07004196 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004197 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07004198 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 }
David Aherne3818542019-02-26 09:00:03 -08004200 if (tb[RTA_VIA]) {
4201 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4202 goto errout;
4203 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004204
4205 if (tb[RTA_DST]) {
4206 int plen = (rtm->rtm_dst_len + 7) >> 3;
4207
4208 if (nla_len(tb[RTA_DST]) < plen)
4209 goto errout;
4210
4211 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004213
4214 if (tb[RTA_SRC]) {
4215 int plen = (rtm->rtm_src_len + 7) >> 3;
4216
4217 if (nla_len(tb[RTA_SRC]) < plen)
4218 goto errout;
4219
4220 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004222
Daniel Walterc3968a82011-04-13 21:10:57 +00004223 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02004224 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00004225
Thomas Graf86872cb2006-08-22 00:01:08 -07004226 if (tb[RTA_OIF])
4227 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4228
4229 if (tb[RTA_PRIORITY])
4230 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4231
4232 if (tb[RTA_METRICS]) {
4233 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4234 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004236
4237 if (tb[RTA_TABLE])
4238 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4239
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004240 if (tb[RTA_MULTIPATH]) {
4241 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4242 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
David Ahern9ed59592017-01-17 14:57:36 -08004243
4244 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
David Ahernc255bd62017-05-27 16:19:27 -06004245 cfg->fc_mp_len, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004246 if (err < 0)
4247 goto errout;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004248 }
4249
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004250 if (tb[RTA_PREF]) {
4251 pref = nla_get_u8(tb[RTA_PREF]);
4252 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4253 pref != ICMPV6_ROUTER_PREF_HIGH)
4254 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4255 cfg->fc_flags |= RTF_PREF(pref);
4256 }
4257
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004258 if (tb[RTA_ENCAP])
4259 cfg->fc_encap = tb[RTA_ENCAP];
4260
David Ahern9ed59592017-01-17 14:57:36 -08004261 if (tb[RTA_ENCAP_TYPE]) {
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004262 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
4263
David Ahernc255bd62017-05-27 16:19:27 -06004264 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004265 if (err < 0)
4266 goto errout;
4267 }
4268
Xin Long32bc2012015-12-16 17:50:11 +08004269 if (tb[RTA_EXPIRES]) {
4270 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
4271
4272 if (addrconf_finite_timeout(timeout)) {
4273 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
4274 cfg->fc_flags |= RTF_EXPIRES;
4275 }
4276 }
4277
Thomas Graf86872cb2006-08-22 00:01:08 -07004278 err = 0;
4279errout:
4280 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281}
4282
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004283struct rt6_nh {
David Ahern8d1c8022018-04-17 17:33:26 -07004284 struct fib6_info *fib6_info;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004285 struct fib6_config r_cfg;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004286 struct list_head next;
4287};
4288
David Ahernd4ead6b2018-04-17 17:33:16 -07004289static int ip6_route_info_append(struct net *net,
4290 struct list_head *rt6_nh_list,
David Ahern8d1c8022018-04-17 17:33:26 -07004291 struct fib6_info *rt,
4292 struct fib6_config *r_cfg)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004293{
4294 struct rt6_nh *nh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004295 int err = -EEXIST;
4296
4297 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004298 /* check if fib6_info already exists */
4299 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004300 return err;
4301 }
4302
4303 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
4304 if (!nh)
4305 return -ENOMEM;
David Ahern8d1c8022018-04-17 17:33:26 -07004306 nh->fib6_info = rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004307 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
4308 list_add_tail(&nh->next, rt6_nh_list);
4309
4310 return 0;
4311}
4312
David Ahern8d1c8022018-04-17 17:33:26 -07004313static void ip6_route_mpath_notify(struct fib6_info *rt,
4314 struct fib6_info *rt_last,
David Ahern3b1137f2017-02-02 12:37:10 -08004315 struct nl_info *info,
4316 __u16 nlflags)
4317{
4318 /* if this is an APPEND route, then rt points to the first route
4319 * inserted and rt_last points to last route inserted. Userspace
4320 * wants a consistent dump of the route which starts at the first
4321 * nexthop. Since sibling routes are always added at the end of
4322 * the list, find the first sibling of the last route appended
4323 */
David Ahern93c2fb22018-04-18 15:38:59 -07004324 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
4325 rt = list_first_entry(&rt_last->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -07004326 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -07004327 fib6_siblings);
David Ahern3b1137f2017-02-02 12:37:10 -08004328 }
4329
4330 if (rt)
4331 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
4332}
4333
David Ahern333c4302017-05-21 10:12:04 -06004334static int ip6_route_multipath_add(struct fib6_config *cfg,
4335 struct netlink_ext_ack *extack)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004336{
David Ahern8d1c8022018-04-17 17:33:26 -07004337 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
David Ahern3b1137f2017-02-02 12:37:10 -08004338 struct nl_info *info = &cfg->fc_nlinfo;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004339 struct fib6_config r_cfg;
4340 struct rtnexthop *rtnh;
David Ahern8d1c8022018-04-17 17:33:26 -07004341 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004342 struct rt6_nh *err_nh;
4343 struct rt6_nh *nh, *nh_safe;
David Ahern3b1137f2017-02-02 12:37:10 -08004344 __u16 nlflags;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004345 int remaining;
4346 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004347 int err = 1;
4348 int nhn = 0;
4349 int replace = (cfg->fc_nlinfo.nlh &&
4350 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
4351 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004352
David Ahern3b1137f2017-02-02 12:37:10 -08004353 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
4354 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
4355 nlflags |= NLM_F_APPEND;
4356
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02004357 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004358 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004359
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004360 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
David Ahern8d1c8022018-04-17 17:33:26 -07004361 * fib6_info structs per nexthop
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004362 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004363 while (rtnh_ok(rtnh, remaining)) {
4364 memcpy(&r_cfg, cfg, sizeof(*cfg));
4365 if (rtnh->rtnh_ifindex)
4366 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
4367
4368 attrlen = rtnh_attrlen(rtnh);
4369 if (attrlen > 0) {
4370 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4371
4372 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
4373 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004374 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004375 r_cfg.fc_flags |= RTF_GATEWAY;
4376 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004377 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
4378 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
4379 if (nla)
4380 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004381 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004382
David Ahern68e2ffd2018-03-20 10:06:59 -07004383 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
David Ahernacb54e32018-04-17 17:33:22 -07004384 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07004385 if (IS_ERR(rt)) {
4386 err = PTR_ERR(rt);
4387 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004388 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07004389 }
David Ahernb5d2d752018-07-15 09:35:19 -07004390 if (!rt6_qualify_for_ecmp(rt)) {
4391 err = -EINVAL;
4392 NL_SET_ERR_MSG(extack,
4393 "Device only routes can not be added for IPv6 using the multipath API.");
4394 fib6_info_release(rt);
4395 goto cleanup;
4396 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004397
David Ahernad1601a2019-03-27 20:53:56 -07004398 rt->fib6_nh.fib_nh_weight = rtnh->rtnh_hops + 1;
Ido Schimmel398958a2018-01-09 16:40:28 +02004399
David Ahernd4ead6b2018-04-17 17:33:16 -07004400 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4401 rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004402 if (err) {
David Ahern93531c62018-04-17 17:33:25 -07004403 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004404 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004405 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004406
4407 rtnh = rtnh_next(rtnh, &remaining);
4408 }
4409
David Ahern3b1137f2017-02-02 12:37:10 -08004410 /* for add and replace send one notification with all nexthops.
4411 * Skip the notification in fib6_add_rt2node and send one with
4412 * the full route when done
4413 */
4414 info->skip_notify = 1;
4415
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004416 err_nh = NULL;
4417 list_for_each_entry(nh, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004418 err = __ip6_ins_rt(nh->fib6_info, info, extack);
4419 fib6_info_release(nh->fib6_info);
David Ahern3b1137f2017-02-02 12:37:10 -08004420
David Ahernf7225172018-06-04 13:41:42 -07004421 if (!err) {
4422 /* save reference to last route successfully inserted */
4423 rt_last = nh->fib6_info;
4424
4425 /* save reference to first route for notification */
4426 if (!rt_notif)
4427 rt_notif = nh->fib6_info;
4428 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004429
David Ahern8d1c8022018-04-17 17:33:26 -07004430 /* nh->fib6_info is used or freed at this point, reset to NULL*/
4431 nh->fib6_info = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004432 if (err) {
4433 if (replace && nhn)
Jakub Kicinskia5a82d82019-01-14 10:52:45 -08004434 NL_SET_ERR_MSG_MOD(extack,
4435 "multipath route replace failed (check consistency of installed routes)");
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004436 err_nh = nh;
4437 goto add_errout;
4438 }
4439
Nicolas Dichtel1a724182012-11-01 22:58:22 +00004440 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02004441 * these flags after the first nexthop: if there is a collision,
4442 * we have already failed to add the first nexthop:
4443 * fib6_add_rt2node() has rejected it; when replacing, old
4444 * nexthops have been replaced by first new, the rest should
4445 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00004446 */
Michal Kubeček27596472015-05-18 20:54:00 +02004447 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
4448 NLM_F_REPLACE);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004449 nhn++;
4450 }
4451
David Ahern3b1137f2017-02-02 12:37:10 -08004452 /* success ... tell user about new route */
4453 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004454 goto cleanup;
4455
4456add_errout:
David Ahern3b1137f2017-02-02 12:37:10 -08004457 /* send notification for routes that were added so that
4458 * the delete notifications sent by ip6_route_del are
4459 * coherent
4460 */
4461 if (rt_notif)
4462 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
4463
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004464 /* Delete routes that were already added */
4465 list_for_each_entry(nh, &rt6_nh_list, next) {
4466 if (err_nh == nh)
4467 break;
David Ahern333c4302017-05-21 10:12:04 -06004468 ip6_route_del(&nh->r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004469 }
4470
4471cleanup:
4472 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004473 if (nh->fib6_info)
4474 fib6_info_release(nh->fib6_info);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004475 list_del(&nh->next);
4476 kfree(nh);
4477 }
4478
4479 return err;
4480}
4481
David Ahern333c4302017-05-21 10:12:04 -06004482static int ip6_route_multipath_del(struct fib6_config *cfg,
4483 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004484{
4485 struct fib6_config r_cfg;
4486 struct rtnexthop *rtnh;
4487 int remaining;
4488 int attrlen;
4489 int err = 1, last_err = 0;
4490
4491 remaining = cfg->fc_mp_len;
4492 rtnh = (struct rtnexthop *)cfg->fc_mp;
4493
4494 /* Parse a Multipath Entry */
4495 while (rtnh_ok(rtnh, remaining)) {
4496 memcpy(&r_cfg, cfg, sizeof(*cfg));
4497 if (rtnh->rtnh_ifindex)
4498 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
4499
4500 attrlen = rtnh_attrlen(rtnh);
4501 if (attrlen > 0) {
4502 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4503
4504 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
4505 if (nla) {
4506 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
4507 r_cfg.fc_flags |= RTF_GATEWAY;
4508 }
4509 }
David Ahern333c4302017-05-21 10:12:04 -06004510 err = ip6_route_del(&r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004511 if (err)
4512 last_err = err;
4513
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004514 rtnh = rtnh_next(rtnh, &remaining);
4515 }
4516
4517 return last_err;
4518}
4519
David Ahernc21ef3e2017-04-16 09:48:24 -07004520static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4521 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522{
Thomas Graf86872cb2006-08-22 00:01:08 -07004523 struct fib6_config cfg;
4524 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
David Ahern333c4302017-05-21 10:12:04 -06004526 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004527 if (err < 0)
4528 return err;
4529
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004530 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06004531 return ip6_route_multipath_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08004532 else {
4533 cfg.fc_delete_all_nh = 1;
David Ahern333c4302017-05-21 10:12:04 -06004534 return ip6_route_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08004535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536}
4537
David Ahernc21ef3e2017-04-16 09:48:24 -07004538static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4539 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540{
Thomas Graf86872cb2006-08-22 00:01:08 -07004541 struct fib6_config cfg;
4542 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
David Ahern333c4302017-05-21 10:12:04 -06004544 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004545 if (err < 0)
4546 return err;
4547
David Ahern67f69512019-03-21 05:21:34 -07004548 if (cfg.fc_metric == 0)
4549 cfg.fc_metric = IP6_RT_PRIO_USER;
4550
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004551 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06004552 return ip6_route_multipath_add(&cfg, extack);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004553 else
David Ahernacb54e32018-04-17 17:33:22 -07004554 return ip6_route_add(&cfg, GFP_KERNEL, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555}
4556
David Ahern8d1c8022018-04-17 17:33:26 -07004557static size_t rt6_nlmsg_size(struct fib6_info *rt)
Thomas Graf339bf982006-11-10 14:10:15 -08004558{
David Ahernbeb1afac52017-02-02 12:37:09 -08004559 int nexthop_len = 0;
4560
David Ahern93c2fb22018-04-18 15:38:59 -07004561 if (rt->fib6_nsiblings) {
David Ahernbeb1afac52017-02-02 12:37:09 -08004562 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
4563 + NLA_ALIGN(sizeof(struct rtnexthop))
4564 + nla_total_size(16) /* RTA_GATEWAY */
David Ahernad1601a2019-03-27 20:53:56 -07004565 + lwtunnel_get_encap_size(rt->fib6_nh.fib_nh_lws);
David Ahernbeb1afac52017-02-02 12:37:09 -08004566
David Ahern93c2fb22018-04-18 15:38:59 -07004567 nexthop_len *= rt->fib6_nsiblings;
David Ahernbeb1afac52017-02-02 12:37:09 -08004568 }
4569
Thomas Graf339bf982006-11-10 14:10:15 -08004570 return NLMSG_ALIGN(sizeof(struct rtmsg))
4571 + nla_total_size(16) /* RTA_SRC */
4572 + nla_total_size(16) /* RTA_DST */
4573 + nla_total_size(16) /* RTA_GATEWAY */
4574 + nla_total_size(16) /* RTA_PREFSRC */
4575 + nla_total_size(4) /* RTA_TABLE */
4576 + nla_total_size(4) /* RTA_IIF */
4577 + nla_total_size(4) /* RTA_OIF */
4578 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08004579 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01004580 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004581 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004582 + nla_total_size(1) /* RTA_PREF */
David Ahernad1601a2019-03-27 20:53:56 -07004583 + lwtunnel_get_encap_size(rt->fib6_nh.fib_nh_lws)
David Ahernbeb1afac52017-02-02 12:37:09 -08004584 + nexthop_len;
4585}
4586
David Ahernd4ead6b2018-04-17 17:33:16 -07004587static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -07004588 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -07004589 struct in6_addr *dest, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004590 int iif, int type, u32 portid, u32 seq,
David Ahernf8cfe2c2017-01-17 15:51:08 -08004591 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592{
Xin Long22d0bd82018-09-11 14:33:58 +08004593 struct rt6_info *rt6 = (struct rt6_info *)dst;
4594 struct rt6key *rt6_dst, *rt6_src;
4595 u32 *pmetrics, table, rt6_flags;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004596 struct nlmsghdr *nlh;
Xin Long22d0bd82018-09-11 14:33:58 +08004597 struct rtmsg *rtm;
David Ahernd4ead6b2018-04-17 17:33:16 -07004598 long expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
Eric W. Biederman15e47302012-09-07 20:12:54 +00004600 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05004601 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004602 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004603
Xin Long22d0bd82018-09-11 14:33:58 +08004604 if (rt6) {
4605 rt6_dst = &rt6->rt6i_dst;
4606 rt6_src = &rt6->rt6i_src;
4607 rt6_flags = rt6->rt6i_flags;
4608 } else {
4609 rt6_dst = &rt->fib6_dst;
4610 rt6_src = &rt->fib6_src;
4611 rt6_flags = rt->fib6_flags;
4612 }
4613
Thomas Graf2d7202b2006-08-22 00:01:27 -07004614 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 rtm->rtm_family = AF_INET6;
Xin Long22d0bd82018-09-11 14:33:58 +08004616 rtm->rtm_dst_len = rt6_dst->plen;
4617 rtm->rtm_src_len = rt6_src->plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 rtm->rtm_tos = 0;
David Ahern93c2fb22018-04-18 15:38:59 -07004619 if (rt->fib6_table)
4620 table = rt->fib6_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07004621 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07004622 table = RT6_TABLE_UNSPEC;
Kalash Nainwal97f00822019-02-20 16:23:04 -08004623 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
David S. Millerc78679e2012-04-01 20:27:33 -04004624 if (nla_put_u32(skb, RTA_TABLE, table))
4625 goto nla_put_failure;
David Aherne8478e82018-04-17 17:33:13 -07004626
4627 rtm->rtm_type = rt->fib6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 rtm->rtm_flags = 0;
4629 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
David Ahern93c2fb22018-04-18 15:38:59 -07004630 rtm->rtm_protocol = rt->fib6_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
Xin Long22d0bd82018-09-11 14:33:58 +08004632 if (rt6_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 rtm->rtm_flags |= RTM_F_CLONED;
4634
David Ahernd4ead6b2018-04-17 17:33:16 -07004635 if (dest) {
4636 if (nla_put_in6_addr(skb, RTA_DST, dest))
David S. Millerc78679e2012-04-01 20:27:33 -04004637 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004638 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 } else if (rtm->rtm_dst_len)
Xin Long22d0bd82018-09-11 14:33:58 +08004640 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04004641 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642#ifdef CONFIG_IPV6_SUBTREES
4643 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02004644 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04004645 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004646 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04004647 } else if (rtm->rtm_src_len &&
Xin Long22d0bd82018-09-11 14:33:58 +08004648 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04004649 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004651 if (iif) {
4652#ifdef CONFIG_IPV6_MROUTE
Xin Long22d0bd82018-09-11 14:33:58 +08004653 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
David Ahernfd61c6b2017-01-17 15:51:07 -08004654 int err = ip6mr_get_route(net, skb, rtm, portid);
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02004655
David Ahernfd61c6b2017-01-17 15:51:07 -08004656 if (err == 0)
4657 return 0;
4658 if (err < 0)
4659 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004660 } else
4661#endif
David S. Millerc78679e2012-04-01 20:27:33 -04004662 if (nla_put_u32(skb, RTA_IIF, iif))
4663 goto nla_put_failure;
David Ahernd4ead6b2018-04-17 17:33:16 -07004664 } else if (dest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 struct in6_addr saddr_buf;
David Ahernd4ead6b2018-04-17 17:33:16 -07004666 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02004667 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04004668 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07004670
David Ahern93c2fb22018-04-18 15:38:59 -07004671 if (rt->fib6_prefsrc.plen) {
Daniel Walterc3968a82011-04-13 21:10:57 +00004672 struct in6_addr saddr_buf;
David Ahern93c2fb22018-04-18 15:38:59 -07004673 saddr_buf = rt->fib6_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02004674 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04004675 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00004676 }
4677
David Ahernd4ead6b2018-04-17 17:33:16 -07004678 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
4679 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07004680 goto nla_put_failure;
4681
David Ahern93c2fb22018-04-18 15:38:59 -07004682 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
David S. Millerc78679e2012-04-01 20:27:33 -04004683 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00004684
David Ahernbeb1afac52017-02-02 12:37:09 -08004685 /* For multipath routes, walk the siblings list and add
4686 * each as a nexthop within RTA_MULTIPATH.
4687 */
Xin Long22d0bd82018-09-11 14:33:58 +08004688 if (rt6) {
4689 if (rt6_flags & RTF_GATEWAY &&
4690 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
4691 goto nla_put_failure;
4692
4693 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
4694 goto nla_put_failure;
4695 } else if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07004696 struct fib6_info *sibling, *next_sibling;
David Ahernbeb1afac52017-02-02 12:37:09 -08004697 struct nlattr *mp;
4698
4699 mp = nla_nest_start(skb, RTA_MULTIPATH);
4700 if (!mp)
4701 goto nla_put_failure;
4702
David Ahernc0a72072019-04-02 14:11:58 -07004703 if (fib_add_nexthop(skb, &rt->fib6_nh.nh_common,
4704 rt->fib6_nh.fib_nh_weight) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004705 goto nla_put_failure;
4706
4707 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07004708 &rt->fib6_siblings, fib6_siblings) {
David Ahernc0a72072019-04-02 14:11:58 -07004709 if (fib_add_nexthop(skb, &sibling->fib6_nh.nh_common,
4710 sibling->fib6_nh.fib_nh_weight) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004711 goto nla_put_failure;
4712 }
4713
4714 nla_nest_end(skb, mp);
4715 } else {
David Ahernc0a72072019-04-02 14:11:58 -07004716 if (fib_nexthop_info(skb, &rt->fib6_nh.nh_common,
4717 &rtm->rtm_flags, false) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004718 goto nla_put_failure;
4719 }
4720
Xin Long22d0bd82018-09-11 14:33:58 +08004721 if (rt6_flags & RTF_EXPIRES) {
David Ahern14895682018-04-17 17:33:17 -07004722 expires = dst ? dst->expires : rt->expires;
4723 expires -= jiffies;
4724 }
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07004725
David Ahernd4ead6b2018-04-17 17:33:16 -07004726 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08004727 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
Xin Long22d0bd82018-09-11 14:33:58 +08004729 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004730 goto nla_put_failure;
4731
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004732
Johannes Berg053c0952015-01-16 22:09:00 +01004733 nlmsg_end(skb, nlh);
4734 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004735
4736nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004737 nlmsg_cancel(skb, nlh);
4738 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739}
4740
David Ahern13e38902018-10-15 18:56:44 -07004741static bool fib6_info_uses_dev(const struct fib6_info *f6i,
4742 const struct net_device *dev)
4743{
David Ahernad1601a2019-03-27 20:53:56 -07004744 if (f6i->fib6_nh.fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07004745 return true;
4746
4747 if (f6i->fib6_nsiblings) {
4748 struct fib6_info *sibling, *next_sibling;
4749
4750 list_for_each_entry_safe(sibling, next_sibling,
4751 &f6i->fib6_siblings, fib6_siblings) {
David Ahernad1601a2019-03-27 20:53:56 -07004752 if (sibling->fib6_nh.fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07004753 return true;
4754 }
4755 }
4756
4757 return false;
4758}
4759
David Ahern8d1c8022018-04-17 17:33:26 -07004760int rt6_dump_route(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761{
4762 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
David Ahern13e38902018-10-15 18:56:44 -07004763 struct fib_dump_filter *filter = &arg->filter;
4764 unsigned int flags = NLM_F_MULTI;
David Ahern1f17e2f2017-01-26 13:54:08 -08004765 struct net *net = arg->net;
4766
David Ahern421842e2018-04-17 17:33:18 -07004767 if (rt == net->ipv6.fib6_null_entry)
David Ahern1f17e2f2017-01-26 13:54:08 -08004768 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769
David Ahern13e38902018-10-15 18:56:44 -07004770 if ((filter->flags & RTM_F_PREFIX) &&
4771 !(rt->fib6_flags & RTF_PREFIX_RT)) {
4772 /* success since this is not a prefix route */
4773 return 1;
4774 }
4775 if (filter->filter_set) {
4776 if ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
4777 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
4778 (filter->protocol && rt->fib6_protocol != filter->protocol)) {
David Ahernf8cfe2c2017-01-17 15:51:08 -08004779 return 1;
4780 }
David Ahern13e38902018-10-15 18:56:44 -07004781 flags |= NLM_F_DUMP_FILTERED;
David Ahernf8cfe2c2017-01-17 15:51:08 -08004782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
David Ahernd4ead6b2018-04-17 17:33:16 -07004784 return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
4785 RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
David Ahern13e38902018-10-15 18:56:44 -07004786 arg->cb->nlh->nlmsg_seq, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787}
4788
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08004789static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
4790 const struct nlmsghdr *nlh,
4791 struct nlattr **tb,
4792 struct netlink_ext_ack *extack)
4793{
4794 struct rtmsg *rtm;
4795 int i, err;
4796
4797 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
4798 NL_SET_ERR_MSG_MOD(extack,
4799 "Invalid header for get route request");
4800 return -EINVAL;
4801 }
4802
4803 if (!netlink_strict_get_check(skb))
4804 return nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
4805 rtm_ipv6_policy, extack);
4806
4807 rtm = nlmsg_data(nlh);
4808 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
4809 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
4810 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
4811 rtm->rtm_type) {
4812 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
4813 return -EINVAL;
4814 }
4815 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
4816 NL_SET_ERR_MSG_MOD(extack,
4817 "Invalid flags for get route request");
4818 return -EINVAL;
4819 }
4820
4821 err = nlmsg_parse_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
4822 rtm_ipv6_policy, extack);
4823 if (err)
4824 return err;
4825
4826 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
4827 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
4828 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
4829 return -EINVAL;
4830 }
4831
4832 for (i = 0; i <= RTA_MAX; i++) {
4833 if (!tb[i])
4834 continue;
4835
4836 switch (i) {
4837 case RTA_SRC:
4838 case RTA_DST:
4839 case RTA_IIF:
4840 case RTA_OIF:
4841 case RTA_MARK:
4842 case RTA_UID:
4843 case RTA_SPORT:
4844 case RTA_DPORT:
4845 case RTA_IP_PROTO:
4846 break;
4847 default:
4848 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
4849 return -EINVAL;
4850 }
4851 }
4852
4853 return 0;
4854}
4855
David Ahernc21ef3e2017-04-16 09:48:24 -07004856static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4857 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004859 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07004860 struct nlattr *tb[RTA_MAX+1];
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004861 int err, iif = 0, oif = 0;
David Aherna68886a2018-04-20 15:38:02 -07004862 struct fib6_info *from;
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004863 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07004865 struct sk_buff *skb;
4866 struct rtmsg *rtm;
Maciej Żenczykowski744486d2018-09-29 23:44:54 -07004867 struct flowi6 fl6 = {};
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004868 bool fibmatch;
Thomas Grafab364a62006-08-22 00:01:47 -07004869
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08004870 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Thomas Grafab364a62006-08-22 00:01:47 -07004871 if (err < 0)
4872 goto errout;
4873
4874 err = -EINVAL;
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02004875 rtm = nlmsg_data(nlh);
4876 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004877 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
Thomas Grafab364a62006-08-22 00:01:47 -07004878
4879 if (tb[RTA_SRC]) {
4880 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4881 goto errout;
4882
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004883 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07004884 }
4885
4886 if (tb[RTA_DST]) {
4887 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4888 goto errout;
4889
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004890 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07004891 }
4892
4893 if (tb[RTA_IIF])
4894 iif = nla_get_u32(tb[RTA_IIF]);
4895
4896 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004897 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07004898
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07004899 if (tb[RTA_MARK])
4900 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
4901
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004902 if (tb[RTA_UID])
4903 fl6.flowi6_uid = make_kuid(current_user_ns(),
4904 nla_get_u32(tb[RTA_UID]));
4905 else
4906 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
4907
Roopa Prabhueacb9382018-05-22 14:03:28 -07004908 if (tb[RTA_SPORT])
4909 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
4910
4911 if (tb[RTA_DPORT])
4912 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
4913
4914 if (tb[RTA_IP_PROTO]) {
4915 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
Hangbin Liu5e1a99e2019-02-27 16:15:29 +08004916 &fl6.flowi6_proto, AF_INET6,
4917 extack);
Roopa Prabhueacb9382018-05-22 14:03:28 -07004918 if (err)
4919 goto errout;
4920 }
4921
Thomas Grafab364a62006-08-22 00:01:47 -07004922 if (iif) {
4923 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004924 int flags = 0;
4925
Florian Westphal121622d2017-08-15 16:34:42 +02004926 rcu_read_lock();
4927
4928 dev = dev_get_by_index_rcu(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07004929 if (!dev) {
Florian Westphal121622d2017-08-15 16:34:42 +02004930 rcu_read_unlock();
Thomas Grafab364a62006-08-22 00:01:47 -07004931 err = -ENODEV;
4932 goto errout;
4933 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004934
4935 fl6.flowi6_iif = iif;
4936
4937 if (!ipv6_addr_any(&fl6.saddr))
4938 flags |= RT6_LOOKUP_F_HAS_SADDR;
4939
David Ahernb75cc8f2018-03-02 08:32:17 -08004940 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
Florian Westphal121622d2017-08-15 16:34:42 +02004941
4942 rcu_read_unlock();
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004943 } else {
4944 fl6.flowi6_oif = oif;
4945
Ido Schimmel58acfd72017-12-20 12:28:25 +02004946 dst = ip6_route_output(net, NULL, &fl6);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004947 }
4948
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004949
4950 rt = container_of(dst, struct rt6_info, dst);
4951 if (rt->dst.error) {
4952 err = rt->dst.error;
4953 ip6_rt_put(rt);
4954 goto errout;
Thomas Grafab364a62006-08-22 00:01:47 -07004955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956
WANG Cong9d6acb32017-03-01 20:48:39 -08004957 if (rt == net->ipv6.ip6_null_entry) {
4958 err = rt->dst.error;
4959 ip6_rt_put(rt);
4960 goto errout;
4961 }
4962
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05004964 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00004965 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07004966 err = -ENOBUFS;
4967 goto errout;
4968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
Changli Gaod8d1f302010-06-10 23:31:35 -07004970 skb_dst_set(skb, &rt->dst);
David Aherna68886a2018-04-20 15:38:02 -07004971
4972 rcu_read_lock();
4973 from = rcu_dereference(rt->from);
4974
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004975 if (fibmatch)
David Aherna68886a2018-04-20 15:38:02 -07004976 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, iif,
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004977 RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
4978 nlh->nlmsg_seq, 0);
4979 else
David Aherna68886a2018-04-20 15:38:02 -07004980 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
4981 &fl6.saddr, iif, RTM_NEWROUTE,
David Ahernd4ead6b2018-04-17 17:33:16 -07004982 NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
4983 0);
David Aherna68886a2018-04-20 15:38:02 -07004984 rcu_read_unlock();
4985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07004987 kfree_skb(skb);
4988 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 }
4990
Eric W. Biederman15e47302012-09-07 20:12:54 +00004991 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07004992errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994}
4995
David Ahern8d1c8022018-04-17 17:33:26 -07004996void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07004997 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998{
4999 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08005000 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08005001 u32 seq;
5002 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08005004 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05005005 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07005006
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005007 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05005008 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07005009 goto errout;
5010
David Ahernd4ead6b2018-04-17 17:33:16 -07005011 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5012 event, info->portid, seq, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08005013 if (err < 0) {
5014 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
5015 WARN_ON(err == -EMSGSIZE);
5016 kfree_skb(skb);
5017 goto errout;
5018 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00005019 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005020 info->nlh, gfp_any());
5021 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07005022errout:
5023 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08005024 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025}
5026
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005027static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00005028 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005029{
Jiri Pirko351638e2013-05-28 01:30:21 +00005030 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005031 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005032
WANG Cong242d3a42017-05-08 10:12:13 -07005033 if (!(dev->flags & IFF_LOOPBACK))
5034 return NOTIFY_OK;
5035
5036 if (event == NETDEV_REGISTER) {
David Ahernad1601a2019-03-27 20:53:56 -07005037 net->ipv6.fib6_null_entry->fib6_nh.fib_nh_dev = dev;
Changli Gaod8d1f302010-06-10 23:31:35 -07005038 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005039 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
5040#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07005041 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005042 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07005043 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005044 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
5045#endif
WANG Cong76da0702017-06-20 11:42:27 -07005046 } else if (event == NETDEV_UNREGISTER &&
5047 dev->reg_state != NETREG_UNREGISTERED) {
5048 /* NETDEV_UNREGISTER could be fired for multiple times by
5049 * netdev_wait_allrefs(). Make sure we only call this once.
5050 */
Eric Dumazet12d94a82017-08-15 04:09:51 -07005051 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07005052#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric Dumazet12d94a82017-08-15 04:09:51 -07005053 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
5054 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07005055#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005056 }
5057
5058 return NOTIFY_OK;
5059}
5060
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061/*
5062 * /proc
5063 */
5064
5065#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066static int rt6_stats_seq_show(struct seq_file *seq, void *v)
5067{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005068 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005070 net->ipv6.rt6_stats->fib_nodes,
5071 net->ipv6.rt6_stats->fib_route_nodes,
Wei Wang81eb8442017-10-06 12:06:11 -07005072 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005073 net->ipv6.rt6_stats->fib_rt_entries,
5074 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00005075 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005076 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077
5078 return 0;
5079}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080#endif /* CONFIG_PROC_FS */
5081
5082#ifdef CONFIG_SYSCTL
5083
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005085int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 void __user *buffer, size_t *lenp, loff_t *ppos)
5087{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005088 struct net *net;
5089 int delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06005090 int ret;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005091 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005093
5094 net = (struct net *)ctl->extra1;
5095 delay = net->ipv6.sysctl.flush_delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06005096 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5097 if (ret)
5098 return ret;
5099
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02005100 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102}
5103
David Ahern7c6bb7d2018-10-11 20:17:21 -07005104static int zero;
5105static int one = 1;
5106
David Aherned792e22018-10-08 14:06:34 -07005107static struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005108 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08005110 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07005112 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005113 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 },
5115 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08005117 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 .maxlen = sizeof(int),
5119 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005120 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 },
5122 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08005124 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 .maxlen = sizeof(int),
5126 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005127 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 },
5129 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08005131 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 .maxlen = sizeof(int),
5133 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005134 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 },
5136 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08005138 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 .maxlen = sizeof(int),
5140 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005141 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 },
5143 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08005145 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 .maxlen = sizeof(int),
5147 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005148 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 },
5150 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08005152 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 .maxlen = sizeof(int),
5154 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07005155 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 },
5157 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08005159 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 .maxlen = sizeof(int),
5161 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005162 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 },
5164 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08005166 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 .maxlen = sizeof(int),
5168 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07005169 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 },
5171 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08005173 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 .maxlen = sizeof(int),
5175 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005176 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 },
David Ahern7c6bb7d2018-10-11 20:17:21 -07005178 {
5179 .procname = "skip_notify_on_dev_down",
5180 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
5181 .maxlen = sizeof(int),
5182 .mode = 0644,
5183 .proc_handler = proc_dointvec,
5184 .extra1 = &zero,
5185 .extra2 = &one,
5186 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08005187 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188};
5189
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005190struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08005191{
5192 struct ctl_table *table;
5193
5194 table = kmemdup(ipv6_route_table_template,
5195 sizeof(ipv6_route_table_template),
5196 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005197
5198 if (table) {
5199 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005200 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00005201 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005202 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
5203 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
5204 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
5205 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
5206 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
5207 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
5208 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08005209 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
David Ahern7c6bb7d2018-10-11 20:17:21 -07005210 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
Eric W. Biederman464dc802012-11-16 03:02:59 +00005211
5212 /* Don't export sysctls to unprivileged users */
5213 if (net->user_ns != &init_user_ns)
5214 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005215 }
5216
Daniel Lezcano760f2d02008-01-10 02:53:43 -08005217 return table;
5218}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219#endif
5220
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005221static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005222{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07005223 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005224
Alexey Dobriyan86393e52009-08-29 01:34:49 +00005225 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
5226 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005227
Eric Dumazetfc66f952010-10-08 06:37:34 +00005228 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5229 goto out_ip6_dst_ops;
5230
David Ahern421842e2018-04-17 17:33:18 -07005231 net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template,
5232 sizeof(*net->ipv6.fib6_null_entry),
5233 GFP_KERNEL);
5234 if (!net->ipv6.fib6_null_entry)
5235 goto out_ip6_dst_entries;
5236
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005237 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
5238 sizeof(*net->ipv6.ip6_null_entry),
5239 GFP_KERNEL);
5240 if (!net->ipv6.ip6_null_entry)
David Ahern421842e2018-04-17 17:33:18 -07005241 goto out_fib6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005242 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005243 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
5244 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005245
5246#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Vincent Bernatfeca7d82017-08-08 20:23:49 +02005247 net->ipv6.fib6_has_custom_rules = false;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005248 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
5249 sizeof(*net->ipv6.ip6_prohibit_entry),
5250 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005251 if (!net->ipv6.ip6_prohibit_entry)
5252 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005253 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005254 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
5255 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005256
5257 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
5258 sizeof(*net->ipv6.ip6_blk_hole_entry),
5259 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005260 if (!net->ipv6.ip6_blk_hole_entry)
5261 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005262 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005263 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
5264 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005265#endif
5266
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07005267 net->ipv6.sysctl.flush_delay = 0;
5268 net->ipv6.sysctl.ip6_rt_max_size = 4096;
5269 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5270 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5271 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5272 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5273 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5274 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
David Ahern7c6bb7d2018-10-11 20:17:21 -07005275 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07005276
Benjamin Thery6891a342008-03-04 13:49:47 -08005277 net->ipv6.ip6_rt_gc_expire = 30*HZ;
5278
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005279 ret = 0;
5280out:
5281 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005282
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005283#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5284out_ip6_prohibit_entry:
5285 kfree(net->ipv6.ip6_prohibit_entry);
5286out_ip6_null_entry:
5287 kfree(net->ipv6.ip6_null_entry);
5288#endif
David Ahern421842e2018-04-17 17:33:18 -07005289out_fib6_null_entry:
5290 kfree(net->ipv6.fib6_null_entry);
Eric Dumazetfc66f952010-10-08 06:37:34 +00005291out_ip6_dst_entries:
5292 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005293out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005294 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005295}
5296
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005297static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005298{
David Ahern421842e2018-04-17 17:33:18 -07005299 kfree(net->ipv6.fib6_null_entry);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005300 kfree(net->ipv6.ip6_null_entry);
5301#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5302 kfree(net->ipv6.ip6_prohibit_entry);
5303 kfree(net->ipv6.ip6_blk_hole_entry);
5304#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00005305 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005306}
5307
Thomas Grafd1896342012-06-18 12:08:33 +00005308static int __net_init ip6_route_net_init_late(struct net *net)
5309{
5310#ifdef CONFIG_PROC_FS
Christoph Hellwigc3506372018-04-10 19:42:55 +02005311 proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
5312 sizeof(struct ipv6_route_iter));
Christoph Hellwig3617d942018-04-13 20:38:35 +02005313 proc_create_net_single("rt6_stats", 0444, net->proc_net,
5314 rt6_stats_seq_show, NULL);
Thomas Grafd1896342012-06-18 12:08:33 +00005315#endif
5316 return 0;
5317}
5318
5319static void __net_exit ip6_route_net_exit_late(struct net *net)
5320{
5321#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00005322 remove_proc_entry("ipv6_route", net->proc_net);
5323 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00005324#endif
5325}
5326
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005327static struct pernet_operations ip6_route_net_ops = {
5328 .init = ip6_route_net_init,
5329 .exit = ip6_route_net_exit,
5330};
5331
David S. Millerc3426b42012-06-09 16:27:05 -07005332static int __net_init ipv6_inetpeer_init(struct net *net)
5333{
5334 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5335
5336 if (!bp)
5337 return -ENOMEM;
5338 inet_peer_base_init(bp);
5339 net->ipv6.peers = bp;
5340 return 0;
5341}
5342
5343static void __net_exit ipv6_inetpeer_exit(struct net *net)
5344{
5345 struct inet_peer_base *bp = net->ipv6.peers;
5346
5347 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07005348 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07005349 kfree(bp);
5350}
5351
David S. Miller2b823f72012-06-09 19:00:16 -07005352static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07005353 .init = ipv6_inetpeer_init,
5354 .exit = ipv6_inetpeer_exit,
5355};
5356
Thomas Grafd1896342012-06-18 12:08:33 +00005357static struct pernet_operations ip6_route_net_late_ops = {
5358 .init = ip6_route_net_init_late,
5359 .exit = ip6_route_net_exit_late,
5360};
5361
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005362static struct notifier_block ip6_route_dev_notifier = {
5363 .notifier_call = ip6_route_dev_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07005364 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005365};
5366
WANG Cong2f460932017-05-03 22:07:31 -07005367void __init ip6_route_init_special_entries(void)
5368{
5369 /* Registering of the loopback is done before this portion of code,
5370 * the loopback reference in rt6_info will not be taken, do it
5371 * manually for init_net */
David Ahernad1601a2019-03-27 20:53:56 -07005372 init_net.ipv6.fib6_null_entry->fib6_nh.fib_nh_dev = init_net.loopback_dev;
WANG Cong2f460932017-05-03 22:07:31 -07005373 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
5374 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5375 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5376 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
5377 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5378 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
5379 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5380 #endif
5381}
5382
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005383int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005385 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07005386 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005387
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08005388 ret = -ENOMEM;
5389 ip6_dst_ops_template.kmem_cachep =
5390 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
5391 SLAB_HWCACHE_ALIGN, NULL);
5392 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08005393 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07005394
Eric Dumazetfc66f952010-10-08 06:37:34 +00005395 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005396 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08005397 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08005398
David S. Millerc3426b42012-06-09 16:27:05 -07005399 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
5400 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07005401 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00005402
David S. Miller7e52b332012-06-15 15:51:55 -07005403 ret = register_pernet_subsys(&ip6_route_net_ops);
5404 if (ret)
5405 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07005406
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07005407 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
5408
David S. Millere8803b62012-06-16 01:12:19 -07005409 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005410 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005411 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005412
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005413 ret = xfrm6_init();
5414 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07005415 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08005416
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005417 ret = fib6_rules_init();
5418 if (ret)
5419 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08005420
Thomas Grafd1896342012-06-18 12:08:33 +00005421 ret = register_pernet_subsys(&ip6_route_net_late_ops);
5422 if (ret)
5423 goto fib6_rules_init;
5424
Florian Westphal16feebc2017-12-02 21:44:08 +01005425 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
5426 inet6_rtm_newroute, NULL, 0);
5427 if (ret < 0)
5428 goto out_register_late_subsys;
5429
5430 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
5431 inet6_rtm_delroute, NULL, 0);
5432 if (ret < 0)
5433 goto out_register_late_subsys;
5434
5435 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
5436 inet6_rtm_getroute, NULL,
5437 RTNL_FLAG_DOIT_UNLOCKED);
5438 if (ret < 0)
Thomas Grafd1896342012-06-18 12:08:33 +00005439 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005440
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005441 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005442 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00005443 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005444
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07005445 for_each_possible_cpu(cpu) {
5446 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
5447
5448 INIT_LIST_HEAD(&ul->head);
5449 spin_lock_init(&ul->lock);
5450 }
5451
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005452out:
5453 return ret;
5454
Thomas Grafd1896342012-06-18 12:08:33 +00005455out_register_late_subsys:
Florian Westphal16feebc2017-12-02 21:44:08 +01005456 rtnl_unregister_all(PF_INET6);
Thomas Grafd1896342012-06-18 12:08:33 +00005457 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005458fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005459 fib6_rules_cleanup();
5460xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005461 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00005462out_fib6_init:
5463 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005464out_register_subsys:
5465 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07005466out_register_inetpeer:
5467 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00005468out_dst_entries:
5469 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005470out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005471 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005472 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473}
5474
5475void ip6_route_cleanup(void)
5476{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005477 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00005478 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07005479 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07005482 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005483 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00005484 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005485 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486}