blob: fdf598926883994111fd39e7d642e76e97157cce [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>
David Ahern3c618c12019-04-20 09:28:20 -070062#include <net/rtnh.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 Ahern7e4b5122019-04-16 14:36:00 -0700113static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
Wei Wang510e2ce2019-05-16 13:30:54 -0700114 const struct in6_addr *daddr,
115 const 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,
Eric Dumazetf05713e2019-04-22 18:35:03 -0700299 .fib6_ref = REFCOUNT_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
Eric Dumazet0e233872019-04-28 12:22:25 -0700383 from = xchg((__force struct fib6_info **)&rt->from, NULL);
David Ahern93531c62018-04-17 17:33:25 -0700384 fib6_info_release(from);
David S. Millerb3419362010-11-30 12:27:11 -0800385}
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
388 int how)
389{
390 struct rt6_info *rt = (struct rt6_info *)dst;
391 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800392 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900393 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Wei Wange5645f52017-08-14 10:44:59 -0700395 if (idev && idev->dev != loopback_dev) {
396 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
397 if (loopback_idev) {
398 rt->rt6i_idev = loopback_idev;
399 in6_dev_put(idev);
David S. Miller97cac082012-07-02 22:43:47 -0700400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402}
403
Martin KaFai Lau5973fb12015-11-11 11:51:07 -0800404static bool __rt6_check_expired(const struct rt6_info *rt)
405{
406 if (rt->rt6i_flags & RTF_EXPIRES)
407 return time_after(jiffies, rt->dst.expires);
408 else
409 return false;
410}
411
Eric Dumazeta50feda2012-05-18 18:57:34 +0000412static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
David Aherna68886a2018-04-20 15:38:02 -0700414 struct fib6_info *from;
415
416 from = rcu_dereference(rt->from);
417
Gao feng1716a962012-04-06 00:13:10 +0000418 if (rt->rt6i_flags & RTF_EXPIRES) {
419 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000420 return true;
David Aherna68886a2018-04-20 15:38:02 -0700421 } else if (from) {
Xin Long1e2ea8a2017-08-26 20:10:10 +0800422 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
David Aherna68886a2018-04-20 15:38:02 -0700423 fib6_check_expired(from);
Gao feng1716a962012-04-06 00:13:10 +0000424 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000425 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
David Ahernb1d40992019-04-16 14:35:59 -0700428void fib6_select_path(const struct net *net, struct fib6_result *res,
429 struct flowi6 *fl6, int oif, bool have_oif_match,
430 const struct sk_buff *skb, int strict)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000431{
David Ahern8d1c8022018-04-17 17:33:26 -0700432 struct fib6_info *sibling, *next_sibling;
David Ahernb1d40992019-04-16 14:35:59 -0700433 struct fib6_info *match = res->f6i;
434
435 if (!match->fib6_nsiblings || have_oif_match)
436 goto out;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000437
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200438 /* We might have already computed the hash for ICMPv6 errors. In such
439 * case it will always be non-zero. Otherwise now is the time to do it.
440 */
441 if (!fl6->mp_hash)
David Ahernb4bac172018-03-02 08:32:18 -0800442 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200443
David Ahern1cf844c2019-05-22 20:27:59 -0700444 if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
David Ahernb1d40992019-04-16 14:35:59 -0700445 goto out;
Ido Schimmelbbfcd772017-11-21 09:50:12 +0200446
David Ahern93c2fb22018-04-18 15:38:59 -0700447 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
448 fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -0700449 const struct fib6_nh *nh = sibling->fib6_nh;
David Ahern5e670d82018-04-17 17:33:14 -0700450 int nh_upper_bound;
451
David Ahern702cea52019-04-09 14:41:13 -0700452 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
David Ahern5e670d82018-04-17 17:33:14 -0700453 if (fl6->mp_hash > nh_upper_bound)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200454 continue;
David Ahern702cea52019-04-09 14:41:13 -0700455 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200456 break;
457 match = sibling;
458 break;
459 }
460
David Ahernb1d40992019-04-16 14:35:59 -0700461out:
462 res->f6i = match;
David Ahern1cf844c2019-05-22 20:27:59 -0700463 res->nh = match->fib6_nh;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000464}
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466/*
Wei Wang66f5d6c2017-10-06 12:06:10 -0700467 * Route lookup. rcu_read_lock() should be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 */
469
David Ahern0c59d002019-04-09 14:41:18 -0700470static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
471 const struct in6_addr *saddr, int oif, int flags)
472{
473 const struct net_device *dev;
474
475 if (nh->fib_nh_flags & RTNH_F_DEAD)
476 return false;
477
478 dev = nh->fib_nh_dev;
479 if (oif) {
480 if (dev->ifindex == oif)
481 return true;
482 } else {
483 if (ipv6_chk_addr(net, saddr, dev,
484 flags & RT6_LOOKUP_F_IFACE))
485 return true;
486 }
487
488 return false;
489}
490
David Ahern75ef7382019-04-16 14:36:07 -0700491static void rt6_device_match(struct net *net, struct fib6_result *res,
492 const struct in6_addr *saddr, int oif, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
David Ahern75ef7382019-04-16 14:36:07 -0700494 struct fib6_info *f6i = res->f6i;
495 struct fib6_info *spf6i;
496 struct fib6_nh *nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
David Ahern75ef7382019-04-16 14:36:07 -0700498 if (!oif && ipv6_addr_any(saddr)) {
David Ahern1cf844c2019-05-22 20:27:59 -0700499 nh = f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700500 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
501 goto out;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
David Ahern75ef7382019-04-16 14:36:07 -0700504 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
David Ahern1cf844c2019-05-22 20:27:59 -0700505 nh = spf6i->fib6_nh;
David Ahern75ef7382019-04-16 14:36:07 -0700506 if (__rt6_device_match(net, nh, saddr, oif, flags)) {
507 res->f6i = spf6i;
David Ahern7d21fec2019-04-16 14:36:11 -0700508 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700509 }
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
David Ahern75ef7382019-04-16 14:36:07 -0700512 if (oif && flags & RT6_LOOKUP_F_IFACE) {
513 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700514 nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700515 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700516 }
517
David Ahern1cf844c2019-05-22 20:27:59 -0700518 nh = f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700519 if (nh->fib_nh_flags & RTNH_F_DEAD) {
David Ahern75ef7382019-04-16 14:36:07 -0700520 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700521 nh = res->f6i->fib6_nh;
David Ahern75ef7382019-04-16 14:36:07 -0700522 }
David Ahern7d21fec2019-04-16 14:36:11 -0700523out:
524 res->nh = nh;
525 res->fib6_type = res->f6i->fib6_type;
526 res->fib6_flags = res->f6i->fib6_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800529#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200530struct __rt6_probe_work {
531 struct work_struct work;
532 struct in6_addr target;
533 struct net_device *dev;
534};
535
536static void rt6_probe_deferred(struct work_struct *w)
537{
538 struct in6_addr mcaddr;
539 struct __rt6_probe_work *work =
540 container_of(w, struct __rt6_probe_work, work);
541
542 addrconf_addr_solict_mult(&work->target, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800543 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200544 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100545 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200546}
547
David Aherncc3a86c2019-04-09 14:41:12 -0700548static void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800549{
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200550 struct __rt6_probe_work *work = NULL;
David Ahern5e670d82018-04-17 17:33:14 -0700551 const struct in6_addr *nh_gw;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000552 struct neighbour *neigh;
David Ahern5e670d82018-04-17 17:33:14 -0700553 struct net_device *dev;
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200554 struct inet6_dev *idev;
David Ahern5e670d82018-04-17 17:33:14 -0700555
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800556 /*
557 * Okay, this does not seem to be appropriate
558 * for now, however, we need to check if it
559 * is really so; aka Router Reachability Probing.
560 *
561 * Router Reachability Probe MUST be rate-limited
562 * to no more than one per minute.
563 */
David Aherncc3a86c2019-04-09 14:41:12 -0700564 if (fib6_nh->fib_nh_gw_family)
Amerigo Wangfdd66812012-09-10 02:48:44 +0000565 return;
David Ahern5e670d82018-04-17 17:33:14 -0700566
David Aherncc3a86c2019-04-09 14:41:12 -0700567 nh_gw = &fib6_nh->fib_nh_gw6;
568 dev = fib6_nh->fib_nh_dev;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000569 rcu_read_lock_bh();
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200570 idev = __in6_dev_get(dev);
David Ahern5e670d82018-04-17 17:33:14 -0700571 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000572 if (neigh) {
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700573 if (neigh->nud_state & NUD_VALID)
574 goto out;
575
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000576 write_lock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700577 if (!(neigh->nud_state & NUD_VALID) &&
578 time_after(jiffies,
David Aherndcd1f572018-04-18 15:39:05 -0700579 neigh->updated + idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700580 work = kmalloc(sizeof(*work), GFP_ATOMIC);
581 if (work)
582 __neigh_set_probe_once(neigh);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200583 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000584 write_unlock(&neigh->lock);
David Aherncc3a86c2019-04-09 14:41:12 -0700585 } else if (time_after(jiffies, fib6_nh->last_probe +
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200586 idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700587 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000588 }
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700589
590 if (work) {
David Aherncc3a86c2019-04-09 14:41:12 -0700591 fib6_nh->last_probe = jiffies;
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700592 INIT_WORK(&work->work, rt6_probe_deferred);
David Ahern5e670d82018-04-17 17:33:14 -0700593 work->target = *nh_gw;
594 dev_hold(dev);
595 work->dev = dev;
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700596 schedule_work(&work->work);
597 }
598
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700599out:
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000600 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800601}
602#else
David Aherncc3a86c2019-04-09 14:41:12 -0700603static inline void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800604{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800605}
606#endif
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800609 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
David Ahern1ba9a892019-04-09 14:41:10 -0700611static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200613 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
David Ahern5e670d82018-04-17 17:33:14 -0700614 struct neighbour *neigh;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000615
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000616 rcu_read_lock_bh();
David Ahern1ba9a892019-04-09 14:41:10 -0700617 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
618 &fib6_nh->fib_nh_gw6);
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000619 if (neigh) {
620 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800621 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200622 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800623#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000624 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200625 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100626 else
627 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800628#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000629 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200630 } else {
631 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100632 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000633 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000634 rcu_read_unlock_bh();
635
Paul Marksa5a81f02012-12-03 10:26:54 +0000636 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800637}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
David Ahern702cea52019-04-09 14:41:13 -0700639static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
640 int strict)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800641{
David Ahern6e1809a2019-04-09 14:41:11 -0700642 int m = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900643
David Ahern6e1809a2019-04-09 14:41:11 -0700644 if (!oif || nh->fib_nh_dev->ifindex == oif)
645 m = 2;
646
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700647 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200648 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800649#ifdef CONFIG_IPV6_ROUTER_PREF
David Ahern702cea52019-04-09 14:41:13 -0700650 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800651#endif
David Ahern1ba9a892019-04-09 14:41:10 -0700652 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
David Ahern702cea52019-04-09 14:41:13 -0700653 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
David Ahern1ba9a892019-04-09 14:41:10 -0700654 int n = rt6_check_neigh(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200655 if (n < 0)
656 return n;
657 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800658 return m;
659}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
David Ahern28679ed2019-04-09 14:41:14 -0700661static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
662 int oif, int strict, int *mpri, bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800663{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200664 bool match_do_rr = false;
David Ahern28679ed2019-04-09 14:41:14 -0700665 bool rc = false;
666 int m;
Andy Gospodarek35103d12015-08-13 10:39:01 -0400667
David Ahern28679ed2019-04-09 14:41:14 -0700668 if (nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +0200669 goto out;
670
David Ahern28679ed2019-04-09 14:41:14 -0700671 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
672 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
David Ahernd5d32e42016-10-24 12:27:23 -0700673 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
Andy Gospodarek35103d12015-08-13 10:39:01 -0400674 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700675
David Ahern28679ed2019-04-09 14:41:14 -0700676 m = rt6_score_route(nh, fib6_flags, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100677 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200678 match_do_rr = true;
679 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100680 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700681 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700682 }
683
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200684 if (strict & RT6_LOOKUP_F_REACHABLE)
David Ahern28679ed2019-04-09 14:41:14 -0700685 rt6_probe(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200686
Jiri Benc7e980562013-12-11 13:48:20 +0100687 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200688 if (m > *mpri) {
689 *do_rr = match_do_rr;
690 *mpri = m;
David Ahern28679ed2019-04-09 14:41:14 -0700691 rc = true;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200692 }
David S. Millerf11e6652007-03-24 20:36:25 -0700693out:
David Ahern28679ed2019-04-09 14:41:14 -0700694 return rc;
David S. Millerf11e6652007-03-24 20:36:25 -0700695}
696
David Ahernb7bc4b62019-04-16 14:36:08 -0700697static void __find_rr_leaf(struct fib6_info *f6i_start,
David Ahern30c15f02019-04-09 14:41:15 -0700698 struct fib6_info *nomatch, u32 metric,
David Ahernb7bc4b62019-04-16 14:36:08 -0700699 struct fib6_result *res, struct fib6_info **cont,
David Ahern30c15f02019-04-09 14:41:15 -0700700 int oif, int strict, bool *do_rr, int *mpri)
David S. Millerf11e6652007-03-24 20:36:25 -0700701{
David Ahernb7bc4b62019-04-16 14:36:08 -0700702 struct fib6_info *f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700703
David Ahernb7bc4b62019-04-16 14:36:08 -0700704 for (f6i = f6i_start;
705 f6i && f6i != nomatch;
706 f6i = rcu_dereference(f6i->fib6_next)) {
David Ahern30c15f02019-04-09 14:41:15 -0700707 struct fib6_nh *nh;
708
David Ahernb7bc4b62019-04-16 14:36:08 -0700709 if (cont && f6i->fib6_metric != metric) {
710 *cont = f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700711 return;
712 }
713
David Ahernb7bc4b62019-04-16 14:36:08 -0700714 if (fib6_check_expired(f6i))
David Ahern30c15f02019-04-09 14:41:15 -0700715 continue;
716
David Ahern1cf844c2019-05-22 20:27:59 -0700717 nh = f6i->fib6_nh;
David Ahernb7bc4b62019-04-16 14:36:08 -0700718 if (find_match(nh, f6i->fib6_flags, oif, strict, mpri, do_rr)) {
719 res->f6i = f6i;
720 res->nh = nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700721 res->fib6_flags = f6i->fib6_flags;
722 res->fib6_type = f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700723 }
David Ahern30c15f02019-04-09 14:41:15 -0700724 }
725}
726
David Ahernb7bc4b62019-04-16 14:36:08 -0700727static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
728 struct fib6_info *rr_head, int oif, int strict,
729 bool *do_rr, struct fib6_result *res)
David Ahern30c15f02019-04-09 14:41:15 -0700730{
David Ahernb7bc4b62019-04-16 14:36:08 -0700731 u32 metric = rr_head->fib6_metric;
732 struct fib6_info *cont = NULL;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800733 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
David Ahernb7bc4b62019-04-16 14:36:08 -0700735 __find_rr_leaf(rr_head, NULL, metric, res, &cont,
David Ahern30c15f02019-04-09 14:41:15 -0700736 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700737
David Ahernb7bc4b62019-04-16 14:36:08 -0700738 __find_rr_leaf(leaf, rr_head, metric, res, &cont,
David Ahern30c15f02019-04-09 14:41:15 -0700739 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700740
David Ahernb7bc4b62019-04-16 14:36:08 -0700741 if (res->f6i || !cont)
742 return;
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700743
David Ahernb7bc4b62019-04-16 14:36:08 -0700744 __find_rr_leaf(cont, NULL, metric, res, NULL,
David Ahern30c15f02019-04-09 14:41:15 -0700745 oif, strict, do_rr, &mpri);
David S. Millerf11e6652007-03-24 20:36:25 -0700746}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800747
David Ahernb7bc4b62019-04-16 14:36:08 -0700748static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
749 struct fib6_result *res, int strict)
David S. Millerf11e6652007-03-24 20:36:25 -0700750{
David Ahern8d1c8022018-04-17 17:33:26 -0700751 struct fib6_info *leaf = rcu_dereference(fn->leaf);
David Ahernb7bc4b62019-04-16 14:36:08 -0700752 struct fib6_info *rt0;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200753 bool do_rr = false;
Wei Wang17ecf592017-10-06 12:06:09 -0700754 int key_plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
David Ahernb7bc4b62019-04-16 14:36:08 -0700756 /* make sure this function or its helpers sets f6i */
757 res->f6i = NULL;
758
David Ahern421842e2018-04-17 17:33:18 -0700759 if (!leaf || leaf == net->ipv6.fib6_null_entry)
David Ahernb7bc4b62019-04-16 14:36:08 -0700760 goto out;
Wei Wang8d1040e2017-10-06 12:06:08 -0700761
Wei Wang66f5d6c2017-10-06 12:06:10 -0700762 rt0 = rcu_dereference(fn->rr_ptr);
David S. Millerf11e6652007-03-24 20:36:25 -0700763 if (!rt0)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700764 rt0 = leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Wei Wang17ecf592017-10-06 12:06:09 -0700766 /* Double check to make sure fn is not an intermediate node
767 * and fn->leaf does not points to its child's leaf
768 * (This might happen if all routes under fn are deleted from
769 * the tree and fib6_repair_tree() is called on the node.)
770 */
David Ahern93c2fb22018-04-18 15:38:59 -0700771 key_plen = rt0->fib6_dst.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700772#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -0700773 if (rt0->fib6_src.plen)
774 key_plen = rt0->fib6_src.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700775#endif
776 if (fn->fn_bit != key_plen)
David Ahernb7bc4b62019-04-16 14:36:08 -0700777 goto out;
Wei Wang17ecf592017-10-06 12:06:09 -0700778
David Ahernb7bc4b62019-04-16 14:36:08 -0700779 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200780 if (do_rr) {
David Ahern8fb11a92018-05-04 13:54:24 -0700781 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
David S. Millerf11e6652007-03-24 20:36:25 -0700782
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800783 /* no entries matched; do round-robin */
David Ahern93c2fb22018-04-18 15:38:59 -0700784 if (!next || next->fib6_metric != rt0->fib6_metric)
Wei Wang8d1040e2017-10-06 12:06:08 -0700785 next = leaf;
David S. Millerf11e6652007-03-24 20:36:25 -0700786
Wei Wang66f5d6c2017-10-06 12:06:10 -0700787 if (next != rt0) {
David Ahern93c2fb22018-04-18 15:38:59 -0700788 spin_lock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700789 /* make sure next is not being deleted from the tree */
David Ahern93c2fb22018-04-18 15:38:59 -0700790 if (next->fib6_node)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700791 rcu_assign_pointer(fn->rr_ptr, next);
David Ahern93c2fb22018-04-18 15:38:59 -0700792 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
David Ahernb7bc4b62019-04-16 14:36:08 -0700796out:
797 if (!res->f6i) {
798 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700799 res->nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700800 res->fib6_flags = res->f6i->fib6_flags;
801 res->fib6_type = res->f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
David Ahern85bd05d2019-04-16 14:36:01 -0700805static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700806{
David Ahern85bd05d2019-04-16 14:36:01 -0700807 return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
808 res->nh->fib_nh_gw_family;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700809}
810
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800811#ifdef CONFIG_IPV6_ROUTE_INFO
812int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000813 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800814{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900815 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800816 struct route_info *rinfo = (struct route_info *) opt;
817 struct in6_addr prefix_buf, *prefix;
818 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900819 unsigned long lifetime;
David Ahern8d1c8022018-04-17 17:33:26 -0700820 struct fib6_info *rt;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800821
822 if (len < sizeof(struct route_info)) {
823 return -EINVAL;
824 }
825
826 /* Sanity check for prefix_len and length */
827 if (rinfo->length > 3) {
828 return -EINVAL;
829 } else if (rinfo->prefix_len > 128) {
830 return -EINVAL;
831 } else if (rinfo->prefix_len > 64) {
832 if (rinfo->length < 2) {
833 return -EINVAL;
834 }
835 } else if (rinfo->prefix_len > 0) {
836 if (rinfo->length < 1) {
837 return -EINVAL;
838 }
839 }
840
841 pref = rinfo->route_pref;
842 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000843 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800844
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900845 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800846
847 if (rinfo->length == 3)
848 prefix = (struct in6_addr *)rinfo->prefix;
849 else {
850 /* this function is safe */
851 ipv6_addr_prefix(&prefix_buf,
852 (struct in6_addr *)rinfo->prefix,
853 rinfo->prefix_len);
854 prefix = &prefix_buf;
855 }
856
Duan Jiongf104a562013-11-08 09:56:53 +0800857 if (rinfo->prefix_len == 0)
David Ahernafb1d4b52018-04-17 17:33:11 -0700858 rt = rt6_get_dflt_router(net, gwaddr, dev);
Duan Jiongf104a562013-11-08 09:56:53 +0800859 else
860 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
David Ahern830218c2016-10-24 10:52:35 -0700861 gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800862
863 if (rt && !lifetime) {
David Ahernafb1d4b52018-04-17 17:33:11 -0700864 ip6_del_rt(net, rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800865 rt = NULL;
866 }
867
868 if (!rt && lifetime)
David Ahern830218c2016-10-24 10:52:35 -0700869 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
870 dev, pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800871 else if (rt)
David Ahern93c2fb22018-04-18 15:38:59 -0700872 rt->fib6_flags = RTF_ROUTEINFO |
873 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800874
875 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000876 if (!addrconf_finite_timeout(lifetime))
David Ahern14895682018-04-17 17:33:17 -0700877 fib6_clean_expires(rt);
Gao feng1716a962012-04-06 00:13:10 +0000878 else
David Ahern14895682018-04-17 17:33:17 -0700879 fib6_set_expires(rt, jiffies + HZ * lifetime);
Gao feng1716a962012-04-06 00:13:10 +0000880
David Ahern93531c62018-04-17 17:33:25 -0700881 fib6_info_release(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800882 }
883 return 0;
884}
885#endif
886
David Ahernae90d862018-04-17 17:33:12 -0700887/*
888 * Misc support functions
889 */
890
891/* called with rcu_lock held */
David Ahern0d161582019-04-16 14:36:04 -0700892static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
David Ahernae90d862018-04-17 17:33:12 -0700893{
David Ahern0d161582019-04-16 14:36:04 -0700894 struct net_device *dev = res->nh->fib_nh_dev;
David Ahernae90d862018-04-17 17:33:12 -0700895
David Ahern7d21fec2019-04-16 14:36:11 -0700896 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
David Ahernae90d862018-04-17 17:33:12 -0700897 /* for copies of local routes, dst->dev needs to be the
898 * device if it is a master device, the master device if
899 * device is enslaved, and the loopback as the default
900 */
901 if (netif_is_l3_slave(dev) &&
David Ahern7d21fec2019-04-16 14:36:11 -0700902 !rt6_need_strict(&res->f6i->fib6_dst.addr))
David Ahernae90d862018-04-17 17:33:12 -0700903 dev = l3mdev_master_dev_rcu(dev);
904 else if (!netif_is_l3_master(dev))
905 dev = dev_net(dev)->loopback_dev;
906 /* last case is netif_is_l3_master(dev) is true in which
907 * case we want dev returned to be dev
908 */
909 }
910
911 return dev;
912}
913
David Ahern6edb3c92018-04-17 17:33:15 -0700914static const int fib6_prop[RTN_MAX + 1] = {
915 [RTN_UNSPEC] = 0,
916 [RTN_UNICAST] = 0,
917 [RTN_LOCAL] = 0,
918 [RTN_BROADCAST] = 0,
919 [RTN_ANYCAST] = 0,
920 [RTN_MULTICAST] = 0,
921 [RTN_BLACKHOLE] = -EINVAL,
922 [RTN_UNREACHABLE] = -EHOSTUNREACH,
923 [RTN_PROHIBIT] = -EACCES,
924 [RTN_THROW] = -EAGAIN,
925 [RTN_NAT] = -EINVAL,
926 [RTN_XRESOLVE] = -EINVAL,
927};
928
929static int ip6_rt_type_to_error(u8 fib6_type)
930{
931 return fib6_prop[fib6_type];
932}
933
David Ahern8d1c8022018-04-17 17:33:26 -0700934static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
David Ahern3b6761d2018-04-17 17:33:20 -0700935{
936 unsigned short flags = 0;
937
938 if (rt->dst_nocount)
939 flags |= DST_NOCOUNT;
940 if (rt->dst_nopolicy)
941 flags |= DST_NOPOLICY;
942 if (rt->dst_host)
943 flags |= DST_HOST;
944
945 return flags;
946}
947
David Ahern7d21fec2019-04-16 14:36:11 -0700948static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
David Ahern6edb3c92018-04-17 17:33:15 -0700949{
David Ahern7d21fec2019-04-16 14:36:11 -0700950 rt->dst.error = ip6_rt_type_to_error(fib6_type);
David Ahern6edb3c92018-04-17 17:33:15 -0700951
David Ahern7d21fec2019-04-16 14:36:11 -0700952 switch (fib6_type) {
David Ahern6edb3c92018-04-17 17:33:15 -0700953 case RTN_BLACKHOLE:
954 rt->dst.output = dst_discard_out;
955 rt->dst.input = dst_discard;
956 break;
957 case RTN_PROHIBIT:
958 rt->dst.output = ip6_pkt_prohibit_out;
959 rt->dst.input = ip6_pkt_prohibit;
960 break;
961 case RTN_THROW:
962 case RTN_UNREACHABLE:
963 default:
964 rt->dst.output = ip6_pkt_discard_out;
965 rt->dst.input = ip6_pkt_discard;
966 break;
967 }
968}
969
David Ahern0d161582019-04-16 14:36:04 -0700970static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
David Ahern6edb3c92018-04-17 17:33:15 -0700971{
David Ahern7d21fec2019-04-16 14:36:11 -0700972 struct fib6_info *f6i = res->f6i;
David Ahern0d161582019-04-16 14:36:04 -0700973
David Ahern7d21fec2019-04-16 14:36:11 -0700974 if (res->fib6_flags & RTF_REJECT) {
975 ip6_rt_init_dst_reject(rt, res->fib6_type);
David Ahern6edb3c92018-04-17 17:33:15 -0700976 return;
977 }
978
979 rt->dst.error = 0;
980 rt->dst.output = ip6_output;
981
David Ahern7d21fec2019-04-16 14:36:11 -0700982 if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
David Ahern6edb3c92018-04-17 17:33:15 -0700983 rt->dst.input = ip6_input;
David Ahern7d21fec2019-04-16 14:36:11 -0700984 } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
David Ahern6edb3c92018-04-17 17:33:15 -0700985 rt->dst.input = ip6_mc_input;
986 } else {
987 rt->dst.input = ip6_forward;
988 }
989
David Ahern0d161582019-04-16 14:36:04 -0700990 if (res->nh->fib_nh_lws) {
991 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
David Ahern6edb3c92018-04-17 17:33:15 -0700992 lwtunnel_set_redirect(&rt->dst);
993 }
994
995 rt->dst.lastuse = jiffies;
996}
997
Wei Wange873e4b2018-07-21 20:56:32 -0700998/* Caller must already hold reference to @from */
David Ahern8d1c8022018-04-17 17:33:26 -0700999static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
David Ahernae90d862018-04-17 17:33:12 -07001000{
David Ahernae90d862018-04-17 17:33:12 -07001001 rt->rt6i_flags &= ~RTF_EXPIRES;
David Aherna68886a2018-04-20 15:38:02 -07001002 rcu_assign_pointer(rt->from, from);
David Aherne1255ed2018-10-04 20:07:53 -07001003 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
David Ahernae90d862018-04-17 17:33:12 -07001004}
1005
David Ahern0d161582019-04-16 14:36:04 -07001006/* Caller must already hold reference to f6i in result */
1007static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
David Ahernae90d862018-04-17 17:33:12 -07001008{
David Ahern0d161582019-04-16 14:36:04 -07001009 const struct fib6_nh *nh = res->nh;
1010 const struct net_device *dev = nh->fib_nh_dev;
1011 struct fib6_info *f6i = res->f6i;
David Aherndcd1f572018-04-18 15:39:05 -07001012
David Ahern0d161582019-04-16 14:36:04 -07001013 ip6_rt_init_dst(rt, res);
David Ahern6edb3c92018-04-17 17:33:15 -07001014
David Ahern0d161582019-04-16 14:36:04 -07001015 rt->rt6i_dst = f6i->fib6_dst;
David Aherndcd1f572018-04-18 15:39:05 -07001016 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
David Ahern7d21fec2019-04-16 14:36:11 -07001017 rt->rt6i_flags = res->fib6_flags;
David Ahern0d161582019-04-16 14:36:04 -07001018 if (nh->fib_nh_gw_family) {
1019 rt->rt6i_gateway = nh->fib_nh_gw6;
David Ahern2b2450c2019-03-27 20:53:52 -07001020 rt->rt6i_flags |= RTF_GATEWAY;
1021 }
David Ahern0d161582019-04-16 14:36:04 -07001022 rt6_set_from(rt, f6i);
David Ahernae90d862018-04-17 17:33:12 -07001023#ifdef CONFIG_IPV6_SUBTREES
David Ahern0d161582019-04-16 14:36:04 -07001024 rt->rt6i_src = f6i->fib6_src;
David Ahernae90d862018-04-17 17:33:12 -07001025#endif
David Ahernae90d862018-04-17 17:33:12 -07001026}
1027
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001028static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1029 struct in6_addr *saddr)
1030{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001031 struct fib6_node *pn, *sn;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001032 while (1) {
1033 if (fn->fn_flags & RTN_TL_ROOT)
1034 return NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001035 pn = rcu_dereference(fn->parent);
1036 sn = FIB6_SUBTREE(pn);
1037 if (sn && sn != fn)
David Ahern64547432018-05-09 20:34:19 -07001038 fn = fib6_node_lookup(sn, NULL, saddr);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001039 else
1040 fn = pn;
1041 if (fn->fn_flags & RTN_RTINFO)
1042 return fn;
1043 }
1044}
Thomas Grafc71099a2006-08-04 23:20:06 -07001045
David Ahern10585b42019-03-20 09:24:50 -07001046static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
Wei Wangd3843fe2017-10-06 12:06:06 -07001047{
1048 struct rt6_info *rt = *prt;
1049
1050 if (dst_hold_safe(&rt->dst))
1051 return true;
David Ahern10585b42019-03-20 09:24:50 -07001052 if (net) {
Wei Wangd3843fe2017-10-06 12:06:06 -07001053 rt = net->ipv6.ip6_null_entry;
1054 dst_hold(&rt->dst);
1055 } else {
1056 rt = NULL;
1057 }
1058 *prt = rt;
1059 return false;
1060}
1061
David Aherndec9b0e2018-04-17 17:33:19 -07001062/* called with rcu_lock held */
David Ahern9b6b35a2019-04-16 14:36:02 -07001063static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
David Aherndec9b0e2018-04-17 17:33:19 -07001064{
David Ahern9b6b35a2019-04-16 14:36:02 -07001065 struct net_device *dev = res->nh->fib_nh_dev;
1066 struct fib6_info *f6i = res->f6i;
1067 unsigned short flags;
David Aherndec9b0e2018-04-17 17:33:19 -07001068 struct rt6_info *nrt;
1069
David Ahern9b6b35a2019-04-16 14:36:02 -07001070 if (!fib6_info_hold_safe(f6i))
Xin Long1c87e792019-03-20 14:45:48 +08001071 goto fallback;
Wei Wange873e4b2018-07-21 20:56:32 -07001072
David Ahern9b6b35a2019-04-16 14:36:02 -07001073 flags = fib6_info_dst_flags(f6i);
David Ahern93531c62018-04-17 17:33:25 -07001074 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
Xin Long1c87e792019-03-20 14:45:48 +08001075 if (!nrt) {
David Ahern9b6b35a2019-04-16 14:36:02 -07001076 fib6_info_release(f6i);
Xin Long1c87e792019-03-20 14:45:48 +08001077 goto fallback;
1078 }
David Aherndec9b0e2018-04-17 17:33:19 -07001079
David Ahern0d161582019-04-16 14:36:04 -07001080 ip6_rt_copy_init(nrt, res);
Xin Long1c87e792019-03-20 14:45:48 +08001081 return nrt;
1082
1083fallback:
1084 nrt = dev_net(dev)->ipv6.ip6_null_entry;
1085 dst_hold(&nrt->dst);
David Aherndec9b0e2018-04-17 17:33:19 -07001086 return nrt;
1087}
1088
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001089static struct rt6_info *ip6_pol_route_lookup(struct net *net,
1090 struct fib6_table *table,
David Ahernb75cc8f2018-03-02 08:32:17 -08001091 struct flowi6 *fl6,
1092 const struct sk_buff *skb,
1093 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
David Ahernb1d40992019-04-16 14:35:59 -07001095 struct fib6_result res = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 struct fib6_node *fn;
David Ahern23fb93a2018-04-17 17:33:23 -07001097 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
David Ahernb6cdbc82018-03-29 17:44:57 -07001099 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1100 flags &= ~RT6_LOOKUP_F_IFACE;
1101
Wei Wang66f5d6c2017-10-06 12:06:10 -07001102 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07001103 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -07001104restart:
David Ahernb1d40992019-04-16 14:35:59 -07001105 res.f6i = rcu_dereference(fn->leaf);
1106 if (!res.f6i)
1107 res.f6i = net->ipv6.fib6_null_entry;
David Ahernaf52a522019-04-09 14:41:16 -07001108 else
David Ahern75ef7382019-04-16 14:36:07 -07001109 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1110 flags);
David Ahernaf52a522019-04-09 14:41:16 -07001111
David Ahernb1d40992019-04-16 14:35:59 -07001112 if (res.f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001113 fn = fib6_backtrack(fn, &fl6->saddr);
1114 if (fn)
1115 goto restart;
David Ahernaf52a522019-04-09 14:41:16 -07001116
1117 rt = net->ipv6.ip6_null_entry;
1118 dst_hold(&rt->dst);
1119 goto out;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001120 }
Wei Wang2b760fc2017-10-06 12:06:03 -07001121
David Ahernb1d40992019-04-16 14:35:59 -07001122 fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1123 fl6->flowi6_oif != 0, skb, flags);
1124
David S. Miller4c9483b2011-03-12 16:22:43 -05001125 /* Search through exception table */
David Ahern7e4b5122019-04-16 14:36:00 -07001126 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
David Ahern23fb93a2018-04-17 17:33:23 -07001127 if (rt) {
David Ahern10585b42019-03-20 09:24:50 -07001128 if (ip6_hold_safe(net, &rt))
David Aherndec9b0e2018-04-17 17:33:19 -07001129 dst_use_noref(&rt->dst, jiffies);
David Ahern23fb93a2018-04-17 17:33:23 -07001130 } else {
David Ahern9b6b35a2019-04-16 14:36:02 -07001131 rt = ip6_create_rt_rcu(&res);
David Aherndec9b0e2018-04-17 17:33:19 -07001132 }
Wei Wangd3843fe2017-10-06 12:06:06 -07001133
David Ahernaf52a522019-04-09 14:41:16 -07001134out:
David Ahern8ff2e5b2019-04-16 14:36:09 -07001135 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahernaf52a522019-04-09 14:41:16 -07001136
Wei Wang66f5d6c2017-10-06 12:06:10 -07001137 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08001138
Thomas Grafc71099a2006-08-04 23:20:06 -07001139 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001140}
1141
Ian Morris67ba4152014-08-24 21:53:10 +01001142struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08001143 const struct sk_buff *skb, int flags)
Florian Westphalea6e5742011-09-05 16:05:44 +02001144{
David Ahernb75cc8f2018-03-02 08:32:17 -08001145 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
Florian Westphalea6e5742011-09-05 16:05:44 +02001146}
1147EXPORT_SYMBOL_GPL(ip6_route_lookup);
1148
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001149struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
David Ahernb75cc8f2018-03-02 08:32:17 -08001150 const struct in6_addr *saddr, int oif,
1151 const struct sk_buff *skb, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -07001152{
David S. Miller4c9483b2011-03-12 16:22:43 -05001153 struct flowi6 fl6 = {
1154 .flowi6_oif = oif,
1155 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001156 };
1157 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001158 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07001159
Thomas Grafadaa70b2006-10-13 15:01:03 -07001160 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -05001161 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -07001162 flags |= RT6_LOOKUP_F_HAS_SADDR;
1163 }
1164
David Ahernb75cc8f2018-03-02 08:32:17 -08001165 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -07001166 if (dst->error == 0)
1167 return (struct rt6_info *) dst;
1168
1169 dst_release(dst);
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return NULL;
1172}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001173EXPORT_SYMBOL(rt6_lookup);
1174
Thomas Grafc71099a2006-08-04 23:20:06 -07001175/* ip6_ins_rt is called with FREE table->tb6_lock.
Wei Wang1cfb71e2017-06-17 10:42:33 -07001176 * It takes new route entry, the addition fails by any reason the
1177 * route is released.
1178 * Caller must hold dst before calling it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 */
1180
David Ahern8d1c8022018-04-17 17:33:26 -07001181static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
David Ahern333c4302017-05-21 10:12:04 -06001182 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001185 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
David Ahern93c2fb22018-04-18 15:38:59 -07001187 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001188 spin_lock_bh(&table->tb6_lock);
David Ahernd4ead6b2018-04-17 17:33:16 -07001189 err = fib6_add(&table->tb6_root, rt, info, extack);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001190 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 return err;
1193}
1194
David Ahern8d1c8022018-04-17 17:33:26 -07001195int ip6_ins_rt(struct net *net, struct fib6_info *rt)
Thomas Graf40e22e82006-08-22 00:00:45 -07001196{
David Ahernafb1d4b52018-04-17 17:33:11 -07001197 struct nl_info info = { .nl_net = net, };
Florian Westphale715b6d2015-01-05 23:57:44 +01001198
David Ahernd4ead6b2018-04-17 17:33:16 -07001199 return __ip6_ins_rt(rt, &info, NULL);
Thomas Graf40e22e82006-08-22 00:00:45 -07001200}
1201
David Ahern85bd05d2019-04-16 14:36:01 -07001202static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001203 const struct in6_addr *daddr,
1204 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
David Ahern85bd05d2019-04-16 14:36:01 -07001206 struct fib6_info *f6i = res->f6i;
David Ahern4832c302017-08-17 12:17:20 -07001207 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 struct rt6_info *rt;
1209
1210 /*
1211 * Clone the route.
1212 */
1213
David Ahern85bd05d2019-04-16 14:36:01 -07001214 if (!fib6_info_hold_safe(f6i))
Wei Wange873e4b2018-07-21 20:56:32 -07001215 return NULL;
1216
David Ahern0d161582019-04-16 14:36:04 -07001217 dev = ip6_rt_get_dev_rcu(res);
David Ahern93531c62018-04-17 17:33:25 -07001218 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
Wei Wange873e4b2018-07-21 20:56:32 -07001219 if (!rt) {
David Ahern85bd05d2019-04-16 14:36:01 -07001220 fib6_info_release(f6i);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001221 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001222 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001223
David Ahern0d161582019-04-16 14:36:04 -07001224 ip6_rt_copy_init(rt, res);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001225 rt->rt6i_flags |= RTF_CACHE;
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001226 rt->dst.flags |= DST_HOST;
1227 rt->rt6i_dst.addr = *daddr;
1228 rt->rt6i_dst.plen = 128;
1229
David Ahern85bd05d2019-04-16 14:36:01 -07001230 if (!rt6_is_gw_or_nonexthop(res)) {
1231 if (f6i->fib6_dst.plen != 128 &&
1232 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001233 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001235 if (rt->rt6i_src.plen && saddr) {
1236 rt->rt6i_src.addr = *saddr;
1237 rt->rt6i_src.plen = 128;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001238 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001239#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001242 return rt;
1243}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
David Aherndb3fede2019-04-16 14:36:03 -07001245static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001246{
David Aherndb3fede2019-04-16 14:36:03 -07001247 struct fib6_info *f6i = res->f6i;
1248 unsigned short flags = fib6_info_dst_flags(f6i);
David Ahern4832c302017-08-17 12:17:20 -07001249 struct net_device *dev;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001250 struct rt6_info *pcpu_rt;
1251
David Aherndb3fede2019-04-16 14:36:03 -07001252 if (!fib6_info_hold_safe(f6i))
Wei Wange873e4b2018-07-21 20:56:32 -07001253 return NULL;
1254
David Ahern4832c302017-08-17 12:17:20 -07001255 rcu_read_lock();
David Ahern0d161582019-04-16 14:36:04 -07001256 dev = ip6_rt_get_dev_rcu(res);
David Ahern93531c62018-04-17 17:33:25 -07001257 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
David Ahern4832c302017-08-17 12:17:20 -07001258 rcu_read_unlock();
Wei Wange873e4b2018-07-21 20:56:32 -07001259 if (!pcpu_rt) {
David Aherndb3fede2019-04-16 14:36:03 -07001260 fib6_info_release(f6i);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001261 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001262 }
David Ahern0d161582019-04-16 14:36:04 -07001263 ip6_rt_copy_init(pcpu_rt, res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001264 pcpu_rt->rt6i_flags |= RTF_PCPU;
1265 return pcpu_rt;
1266}
1267
Wei Wang66f5d6c2017-10-06 12:06:10 -07001268/* It should be called with rcu_read_lock() acquired */
David Aherndb3fede2019-04-16 14:36:03 -07001269static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001270{
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001271 struct rt6_info *pcpu_rt, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001272
David Ahernf40b6ae2019-05-22 20:27:55 -07001273 p = this_cpu_ptr(res->nh->rt6i_pcpu);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001274 pcpu_rt = *p;
1275
David Ahernd4ead6b2018-04-17 17:33:16 -07001276 if (pcpu_rt)
David Ahern10585b42019-03-20 09:24:50 -07001277 ip6_hold_safe(NULL, &pcpu_rt);
Wei Wangd3843fe2017-10-06 12:06:06 -07001278
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001279 return pcpu_rt;
1280}
1281
David Ahernafb1d4b52018-04-17 17:33:11 -07001282static struct rt6_info *rt6_make_pcpu_route(struct net *net,
David Aherndb3fede2019-04-16 14:36:03 -07001283 const struct fib6_result *res)
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001284{
1285 struct rt6_info *pcpu_rt, *prev, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001286
David Aherndb3fede2019-04-16 14:36:03 -07001287 pcpu_rt = ip6_rt_pcpu_alloc(res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001288 if (!pcpu_rt) {
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001289 dst_hold(&net->ipv6.ip6_null_entry->dst);
1290 return net->ipv6.ip6_null_entry;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001291 }
1292
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001293 dst_hold(&pcpu_rt->dst);
David Ahernf40b6ae2019-05-22 20:27:55 -07001294 p = this_cpu_ptr(res->nh->rt6i_pcpu);
Wei Wanga94b9362017-10-06 12:06:04 -07001295 prev = cmpxchg(p, NULL, pcpu_rt);
Eric Dumazet951f7882017-10-08 21:07:18 -07001296 BUG_ON(prev);
Wei Wanga94b9362017-10-06 12:06:04 -07001297
Eric Dumazet61fb0d02019-05-15 19:39:52 -07001298 if (res->f6i->fib6_destroying) {
1299 struct fib6_info *from;
1300
1301 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1302 fib6_info_release(from);
1303 }
1304
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001305 return pcpu_rt;
1306}
1307
Wei Wang35732d02017-10-06 12:05:57 -07001308/* exception hash table implementation
1309 */
1310static DEFINE_SPINLOCK(rt6_exception_lock);
1311
1312/* Remove rt6_ex from hash table and free the memory
1313 * Caller must hold rt6_exception_lock
1314 */
1315static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1316 struct rt6_exception *rt6_ex)
1317{
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001318 struct fib6_info *from;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001319 struct net *net;
Wei Wang81eb8442017-10-06 12:06:11 -07001320
Wei Wang35732d02017-10-06 12:05:57 -07001321 if (!bucket || !rt6_ex)
1322 return;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001323
1324 net = dev_net(rt6_ex->rt6i->dst.dev);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001325 net->ipv6.rt6_stats->fib_rt_cache--;
1326
1327 /* purge completely the exception to allow releasing the held resources:
1328 * some [sk] cache may keep the dst around for unlimited time
1329 */
Eric Dumazet0e233872019-04-28 12:22:25 -07001330 from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001331 fib6_info_release(from);
1332 dst_dev_put(&rt6_ex->rt6i->dst);
1333
Wei Wang35732d02017-10-06 12:05:57 -07001334 hlist_del_rcu(&rt6_ex->hlist);
David Ahern77634cc2018-04-17 17:33:27 -07001335 dst_release(&rt6_ex->rt6i->dst);
Wei Wang35732d02017-10-06 12:05:57 -07001336 kfree_rcu(rt6_ex, rcu);
1337 WARN_ON_ONCE(!bucket->depth);
1338 bucket->depth--;
1339}
1340
1341/* Remove oldest rt6_ex in bucket and free the memory
1342 * Caller must hold rt6_exception_lock
1343 */
1344static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1345{
1346 struct rt6_exception *rt6_ex, *oldest = NULL;
1347
1348 if (!bucket)
1349 return;
1350
1351 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1352 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1353 oldest = rt6_ex;
1354 }
1355 rt6_remove_exception(bucket, oldest);
1356}
1357
1358static u32 rt6_exception_hash(const struct in6_addr *dst,
1359 const struct in6_addr *src)
1360{
1361 static u32 seed __read_mostly;
1362 u32 val;
1363
1364 net_get_random_once(&seed, sizeof(seed));
1365 val = jhash(dst, sizeof(*dst), seed);
1366
1367#ifdef CONFIG_IPV6_SUBTREES
1368 if (src)
1369 val = jhash(src, sizeof(*src), val);
1370#endif
1371 return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1372}
1373
1374/* Helper function to find the cached rt in the hash table
1375 * and update bucket pointer to point to the bucket for this
1376 * (daddr, saddr) pair
1377 * Caller must hold rt6_exception_lock
1378 */
1379static struct rt6_exception *
1380__rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1381 const struct in6_addr *daddr,
1382 const struct in6_addr *saddr)
1383{
1384 struct rt6_exception *rt6_ex;
1385 u32 hval;
1386
1387 if (!(*bucket) || !daddr)
1388 return NULL;
1389
1390 hval = rt6_exception_hash(daddr, saddr);
1391 *bucket += hval;
1392
1393 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1394 struct rt6_info *rt6 = rt6_ex->rt6i;
1395 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1396
1397#ifdef CONFIG_IPV6_SUBTREES
1398 if (matched && saddr)
1399 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1400#endif
1401 if (matched)
1402 return rt6_ex;
1403 }
1404 return NULL;
1405}
1406
1407/* Helper function to find the cached rt in the hash table
1408 * and update bucket pointer to point to the bucket for this
1409 * (daddr, saddr) pair
1410 * Caller must hold rcu_read_lock()
1411 */
1412static struct rt6_exception *
1413__rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1414 const struct in6_addr *daddr,
1415 const struct in6_addr *saddr)
1416{
1417 struct rt6_exception *rt6_ex;
1418 u32 hval;
1419
1420 WARN_ON_ONCE(!rcu_read_lock_held());
1421
1422 if (!(*bucket) || !daddr)
1423 return NULL;
1424
1425 hval = rt6_exception_hash(daddr, saddr);
1426 *bucket += hval;
1427
1428 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1429 struct rt6_info *rt6 = rt6_ex->rt6i;
1430 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1431
1432#ifdef CONFIG_IPV6_SUBTREES
1433 if (matched && saddr)
1434 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1435#endif
1436 if (matched)
1437 return rt6_ex;
1438 }
1439 return NULL;
1440}
1441
David Ahernb748f262019-04-16 14:36:06 -07001442static unsigned int fib6_mtu(const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001443{
David Ahernb748f262019-04-16 14:36:06 -07001444 const struct fib6_nh *nh = res->nh;
David Ahernd4ead6b2018-04-17 17:33:16 -07001445 unsigned int mtu;
1446
David Ahernb748f262019-04-16 14:36:06 -07001447 if (res->f6i->fib6_pmtu) {
1448 mtu = res->f6i->fib6_pmtu;
David Aherndcd1f572018-04-18 15:39:05 -07001449 } else {
David Ahernb748f262019-04-16 14:36:06 -07001450 struct net_device *dev = nh->fib_nh_dev;
David Aherndcd1f572018-04-18 15:39:05 -07001451 struct inet6_dev *idev;
1452
1453 rcu_read_lock();
1454 idev = __in6_dev_get(dev);
1455 mtu = idev->cnf.mtu6;
1456 rcu_read_unlock();
1457 }
1458
David Ahernd4ead6b2018-04-17 17:33:16 -07001459 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1460
David Ahernb748f262019-04-16 14:36:06 -07001461 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahernd4ead6b2018-04-17 17:33:16 -07001462}
1463
David Aherncc5c0732019-05-22 20:27:58 -07001464#define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1465
1466/* used when the flushed bit is not relevant, only access to the bucket
1467 * (ie., all bucket users except rt6_insert_exception);
1468 *
1469 * called under rcu lock; sometimes called with rt6_exception_lock held
1470 */
1471static
1472struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1473 spinlock_t *lock)
1474{
1475 struct rt6_exception_bucket *bucket;
1476
1477 if (lock)
1478 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1479 lockdep_is_held(lock));
1480 else
1481 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1482
1483 /* remove bucket flushed bit if set */
1484 if (bucket) {
1485 unsigned long p = (unsigned long)bucket;
1486
1487 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1488 bucket = (struct rt6_exception_bucket *)p;
1489 }
1490
1491 return bucket;
1492}
1493
1494static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1495{
1496 unsigned long p = (unsigned long)bucket;
1497
1498 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1499}
1500
1501/* called with rt6_exception_lock held */
1502static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1503 spinlock_t *lock)
1504{
1505 struct rt6_exception_bucket *bucket;
1506 unsigned long p;
1507
1508 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1509 lockdep_is_held(lock));
1510
1511 p = (unsigned long)bucket;
1512 p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1513 bucket = (struct rt6_exception_bucket *)p;
1514 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1515}
1516
Wei Wang35732d02017-10-06 12:05:57 -07001517static int rt6_insert_exception(struct rt6_info *nrt,
David Ahern5012f0a2019-04-16 14:36:05 -07001518 const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001519{
David Ahern5e670d82018-04-17 17:33:14 -07001520 struct net *net = dev_net(nrt->dst.dev);
Wei Wang35732d02017-10-06 12:05:57 -07001521 struct rt6_exception_bucket *bucket;
David Aherncc5c0732019-05-22 20:27:58 -07001522 struct fib6_info *f6i = res->f6i;
Wei Wang35732d02017-10-06 12:05:57 -07001523 struct in6_addr *src_key = NULL;
1524 struct rt6_exception *rt6_ex;
David Aherncc5c0732019-05-22 20:27:58 -07001525 struct fib6_nh *nh = res->nh;
Wei Wang35732d02017-10-06 12:05:57 -07001526 int err = 0;
1527
Wei Wang35732d02017-10-06 12:05:57 -07001528 spin_lock_bh(&rt6_exception_lock);
1529
David Aherncc5c0732019-05-22 20:27:58 -07001530 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1531 lockdep_is_held(&rt6_exception_lock));
Wei Wang35732d02017-10-06 12:05:57 -07001532 if (!bucket) {
1533 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1534 GFP_ATOMIC);
1535 if (!bucket) {
1536 err = -ENOMEM;
1537 goto out;
1538 }
David Aherncc5c0732019-05-22 20:27:58 -07001539 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1540 } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1541 err = -EINVAL;
1542 goto out;
Wei Wang35732d02017-10-06 12:05:57 -07001543 }
1544
1545#ifdef CONFIG_IPV6_SUBTREES
David Ahern5012f0a2019-04-16 14:36:05 -07001546 /* fib6_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001547 * and exception table is indexed by a hash of
David Ahern5012f0a2019-04-16 14:36:05 -07001548 * both fib6_dst and fib6_src.
Wei Wang35732d02017-10-06 12:05:57 -07001549 * Otherwise, the exception table is indexed by
David Ahern5012f0a2019-04-16 14:36:05 -07001550 * a hash of only fib6_dst.
Wei Wang35732d02017-10-06 12:05:57 -07001551 */
David Ahern5012f0a2019-04-16 14:36:05 -07001552 if (f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001553 src_key = &nrt->rt6i_src.addr;
1554#endif
David Ahern5012f0a2019-04-16 14:36:05 -07001555 /* rt6_mtu_change() might lower mtu on f6i.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001556 * Only insert this exception route if its mtu
David Ahern5012f0a2019-04-16 14:36:05 -07001557 * is less than f6i's mtu value.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001558 */
David Ahernb748f262019-04-16 14:36:06 -07001559 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001560 err = -EINVAL;
1561 goto out;
1562 }
Wei Wang60006a42017-10-06 12:05:58 -07001563
Wei Wang35732d02017-10-06 12:05:57 -07001564 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1565 src_key);
1566 if (rt6_ex)
1567 rt6_remove_exception(bucket, rt6_ex);
1568
1569 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1570 if (!rt6_ex) {
1571 err = -ENOMEM;
1572 goto out;
1573 }
1574 rt6_ex->rt6i = nrt;
1575 rt6_ex->stamp = jiffies;
Wei Wang35732d02017-10-06 12:05:57 -07001576 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1577 bucket->depth++;
Wei Wang81eb8442017-10-06 12:06:11 -07001578 net->ipv6.rt6_stats->fib_rt_cache++;
Wei Wang35732d02017-10-06 12:05:57 -07001579
1580 if (bucket->depth > FIB6_MAX_DEPTH)
1581 rt6_exception_remove_oldest(bucket);
1582
1583out:
1584 spin_unlock_bh(&rt6_exception_lock);
1585
1586 /* Update fn->fn_sernum to invalidate all cached dst */
Paolo Abenib886d5f2017-10-19 16:07:10 +02001587 if (!err) {
David Ahern5012f0a2019-04-16 14:36:05 -07001588 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1589 fib6_update_sernum(net, f6i);
1590 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
Paolo Abenib886d5f2017-10-19 16:07:10 +02001591 fib6_force_start_gc(net);
1592 }
Wei Wang35732d02017-10-06 12:05:57 -07001593
1594 return err;
1595}
1596
David Ahernc0b220c2019-05-22 20:27:57 -07001597static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
Wei Wang35732d02017-10-06 12:05:57 -07001598{
1599 struct rt6_exception_bucket *bucket;
1600 struct rt6_exception *rt6_ex;
1601 struct hlist_node *tmp;
1602 int i;
1603
1604 spin_lock_bh(&rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001605
David Aherncc5c0732019-05-22 20:27:58 -07001606 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001607 if (!bucket)
1608 goto out;
1609
David Aherncc5c0732019-05-22 20:27:58 -07001610 /* Prevent rt6_insert_exception() to recreate the bucket list */
1611 if (!from)
1612 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1613
Wei Wang35732d02017-10-06 12:05:57 -07001614 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
David Aherncc5c0732019-05-22 20:27:58 -07001615 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1616 if (!from ||
1617 rcu_access_pointer(rt6_ex->rt6i->from) == from)
1618 rt6_remove_exception(bucket, rt6_ex);
1619 }
1620 WARN_ON_ONCE(!from && bucket->depth);
Wei Wang35732d02017-10-06 12:05:57 -07001621 bucket++;
1622 }
Wei Wang35732d02017-10-06 12:05:57 -07001623out:
1624 spin_unlock_bh(&rt6_exception_lock);
1625}
1626
David Ahernc0b220c2019-05-22 20:27:57 -07001627void rt6_flush_exceptions(struct fib6_info *f6i)
1628{
David Ahern1cf844c2019-05-22 20:27:59 -07001629 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
David Ahernc0b220c2019-05-22 20:27:57 -07001630}
1631
Wei Wang35732d02017-10-06 12:05:57 -07001632/* Find cached rt in the hash table inside passed in rt
1633 * Caller has to hold rcu_read_lock()
1634 */
David Ahern7e4b5122019-04-16 14:36:00 -07001635static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
Wei Wang510e2ce2019-05-16 13:30:54 -07001636 const struct in6_addr *daddr,
1637 const struct in6_addr *saddr)
Wei Wang35732d02017-10-06 12:05:57 -07001638{
Wei Wang510e2ce2019-05-16 13:30:54 -07001639 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001640 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001641 struct rt6_exception *rt6_ex;
David Ahern7e4b5122019-04-16 14:36:00 -07001642 struct rt6_info *ret = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001643
Wei Wang35732d02017-10-06 12:05:57 -07001644#ifdef CONFIG_IPV6_SUBTREES
David Ahern7e4b5122019-04-16 14:36:00 -07001645 /* fib6i_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001646 * and exception table is indexed by a hash of
David Ahern7e4b5122019-04-16 14:36:00 -07001647 * both fib6_dst and fib6_src.
Wei Wang510e2ce2019-05-16 13:30:54 -07001648 * However, the src addr used to create the hash
1649 * might not be exactly the passed in saddr which
1650 * is a /128 addr from the flow.
1651 * So we need to use f6i->fib6_src to redo lookup
1652 * if the passed in saddr does not find anything.
1653 * (See the logic in ip6_rt_cache_alloc() on how
1654 * rt->rt6i_src is updated.)
Wei Wang35732d02017-10-06 12:05:57 -07001655 */
David Ahern7e4b5122019-04-16 14:36:00 -07001656 if (res->f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001657 src_key = saddr;
Wei Wang510e2ce2019-05-16 13:30:54 -07001658find_ex:
Wei Wang35732d02017-10-06 12:05:57 -07001659#endif
David Aherncc5c0732019-05-22 20:27:58 -07001660 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001661 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1662
1663 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
David Ahern7e4b5122019-04-16 14:36:00 -07001664 ret = rt6_ex->rt6i;
Wei Wang35732d02017-10-06 12:05:57 -07001665
Wei Wang510e2ce2019-05-16 13:30:54 -07001666#ifdef CONFIG_IPV6_SUBTREES
1667 /* Use fib6_src as src_key and redo lookup */
1668 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1669 src_key = &res->f6i->fib6_src.addr;
1670 goto find_ex;
1671 }
1672#endif
1673
David Ahern7e4b5122019-04-16 14:36:00 -07001674 return ret;
Wei Wang35732d02017-10-06 12:05:57 -07001675}
1676
1677/* Remove the passed in cached rt from the hash table that contains it */
David Aherncc5c0732019-05-22 20:27:58 -07001678static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001679 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001680{
David Ahernc0b220c2019-05-22 20:27:57 -07001681 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001682 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001683 struct rt6_exception *rt6_ex;
1684 int err;
1685
David Aherncc5c0732019-05-22 20:27:58 -07001686 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wang35732d02017-10-06 12:05:57 -07001687 return -ENOENT;
1688
1689 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07001690 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1691
Wei Wang35732d02017-10-06 12:05:57 -07001692#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001693 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1694 * and exception table is indexed by a hash of
1695 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001696 * Otherwise, the exception table is indexed by
1697 * a hash of only rt6i_dst.
1698 */
David Ahernc0b220c2019-05-22 20:27:57 -07001699 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001700 src_key = &rt->rt6i_src.addr;
1701#endif
1702 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1703 &rt->rt6i_dst.addr,
1704 src_key);
1705 if (rt6_ex) {
1706 rt6_remove_exception(bucket, rt6_ex);
1707 err = 0;
1708 } else {
1709 err = -ENOENT;
1710 }
1711
1712 spin_unlock_bh(&rt6_exception_lock);
1713 return err;
1714}
1715
David Ahernc0b220c2019-05-22 20:27:57 -07001716static int rt6_remove_exception_rt(struct rt6_info *rt)
1717{
1718 struct fib6_info *from;
1719
1720 from = rcu_dereference(rt->from);
David Aherncc5c0732019-05-22 20:27:58 -07001721 if (!from || !(rt->rt6i_flags & RTF_CACHE))
David Ahernc0b220c2019-05-22 20:27:57 -07001722 return -EINVAL;
1723
David Ahern1cf844c2019-05-22 20:27:59 -07001724 return fib6_nh_remove_exception(from->fib6_nh,
David Aherncc5c0732019-05-22 20:27:58 -07001725 from->fib6_src.plen, rt);
David Ahernc0b220c2019-05-22 20:27:57 -07001726}
1727
Wei Wang35732d02017-10-06 12:05:57 -07001728/* Find rt6_ex which contains the passed in rt cache and
1729 * refresh its stamp
1730 */
David Aherncc5c0732019-05-22 20:27:58 -07001731static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001732 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001733{
David Ahernc0b220c2019-05-22 20:27:57 -07001734 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001735 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001736 struct rt6_exception *rt6_ex;
Paolo Abeni193f3682019-02-21 11:19:41 +01001737
David Aherncc5c0732019-05-22 20:27:58 -07001738 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001739#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001740 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1741 * and exception table is indexed by a hash of
1742 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001743 * Otherwise, the exception table is indexed by
1744 * a hash of only rt6i_dst.
1745 */
David Ahernc0b220c2019-05-22 20:27:57 -07001746 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001747 src_key = &rt->rt6i_src.addr;
1748#endif
David Aherncc5c0732019-05-22 20:27:58 -07001749 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
Wei Wang35732d02017-10-06 12:05:57 -07001750 if (rt6_ex)
1751 rt6_ex->stamp = jiffies;
David Ahernc0b220c2019-05-22 20:27:57 -07001752}
Wei Wang35732d02017-10-06 12:05:57 -07001753
David Ahernc0b220c2019-05-22 20:27:57 -07001754static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1755{
1756 struct fib6_info *from;
1757
1758 rcu_read_lock();
1759
1760 from = rcu_dereference(rt->from);
1761 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1762 goto unlock;
1763
David Ahern1cf844c2019-05-22 20:27:59 -07001764 fib6_nh_update_exception(from->fib6_nh, from->fib6_src.plen, rt);
Paolo Abeni193f3682019-02-21 11:19:41 +01001765unlock:
Wei Wang35732d02017-10-06 12:05:57 -07001766 rcu_read_unlock();
1767}
1768
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001769static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1770 struct rt6_info *rt, int mtu)
1771{
1772 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1773 * lowest MTU in the path: always allow updating the route PMTU to
1774 * reflect PMTU decreases.
1775 *
1776 * If the new MTU is higher, and the route PMTU is equal to the local
1777 * MTU, this means the old MTU is the lowest in the path, so allow
1778 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1779 * handle this.
1780 */
1781
1782 if (dst_mtu(&rt->dst) >= mtu)
1783 return true;
1784
1785 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1786 return true;
1787
1788 return false;
1789}
1790
1791static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
David Aherncc5c0732019-05-22 20:27:58 -07001792 const struct fib6_nh *nh, int mtu)
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001793{
1794 struct rt6_exception_bucket *bucket;
1795 struct rt6_exception *rt6_ex;
1796 int i;
1797
David Aherncc5c0732019-05-22 20:27:58 -07001798 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001799 if (!bucket)
1800 return;
1801
1802 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1803 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1804 struct rt6_info *entry = rt6_ex->rt6i;
1805
1806 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
David Ahernd4ead6b2018-04-17 17:33:16 -07001807 * route), the metrics of its rt->from have already
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001808 * been updated.
1809 */
David Ahernd4ead6b2018-04-17 17:33:16 -07001810 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001811 rt6_mtu_change_route_allowed(idev, entry, mtu))
David Ahernd4ead6b2018-04-17 17:33:16 -07001812 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001813 }
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001814 bucket++;
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001815 }
1816}
1817
Wei Wangb16cb452017-10-06 12:06:00 -07001818#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
1819
David Aherncc5c0732019-05-22 20:27:58 -07001820static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
1821 const struct in6_addr *gateway)
Wei Wangb16cb452017-10-06 12:06:00 -07001822{
1823 struct rt6_exception_bucket *bucket;
1824 struct rt6_exception *rt6_ex;
1825 struct hlist_node *tmp;
1826 int i;
1827
David Aherncc5c0732019-05-22 20:27:58 -07001828 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangb16cb452017-10-06 12:06:00 -07001829 return;
1830
1831 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07001832 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangb16cb452017-10-06 12:06:00 -07001833 if (bucket) {
1834 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1835 hlist_for_each_entry_safe(rt6_ex, tmp,
1836 &bucket->chain, hlist) {
1837 struct rt6_info *entry = rt6_ex->rt6i;
1838
1839 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1840 RTF_CACHE_GATEWAY &&
1841 ipv6_addr_equal(gateway,
1842 &entry->rt6i_gateway)) {
1843 rt6_remove_exception(bucket, rt6_ex);
1844 }
1845 }
1846 bucket++;
1847 }
1848 }
1849
1850 spin_unlock_bh(&rt6_exception_lock);
1851}
1852
Wei Wangc757faa2017-10-06 12:06:01 -07001853static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1854 struct rt6_exception *rt6_ex,
1855 struct fib6_gc_args *gc_args,
1856 unsigned long now)
1857{
1858 struct rt6_info *rt = rt6_ex->rt6i;
1859
Paolo Abeni1859bac2017-10-19 16:07:11 +02001860 /* we are pruning and obsoleting aged-out and non gateway exceptions
1861 * even if others have still references to them, so that on next
1862 * dst_check() such references can be dropped.
1863 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
1864 * expired, independently from their aging, as per RFC 8201 section 4
1865 */
Wei Wang31afeb42018-01-26 11:40:17 -08001866 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
1867 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1868 RT6_TRACE("aging clone %p\n", rt);
1869 rt6_remove_exception(bucket, rt6_ex);
1870 return;
1871 }
1872 } else if (time_after(jiffies, rt->dst.expires)) {
1873 RT6_TRACE("purging expired route %p\n", rt);
Wei Wangc757faa2017-10-06 12:06:01 -07001874 rt6_remove_exception(bucket, rt6_ex);
1875 return;
Wei Wang31afeb42018-01-26 11:40:17 -08001876 }
1877
1878 if (rt->rt6i_flags & RTF_GATEWAY) {
Wei Wangc757faa2017-10-06 12:06:01 -07001879 struct neighbour *neigh;
1880 __u8 neigh_flags = 0;
1881
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001882 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
1883 if (neigh)
Wei Wangc757faa2017-10-06 12:06:01 -07001884 neigh_flags = neigh->flags;
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001885
Wei Wangc757faa2017-10-06 12:06:01 -07001886 if (!(neigh_flags & NTF_ROUTER)) {
1887 RT6_TRACE("purging route %p via non-router but gateway\n",
1888 rt);
1889 rt6_remove_exception(bucket, rt6_ex);
1890 return;
1891 }
1892 }
Wei Wang31afeb42018-01-26 11:40:17 -08001893
Wei Wangc757faa2017-10-06 12:06:01 -07001894 gc_args->more++;
1895}
1896
David Aherncc5c0732019-05-22 20:27:58 -07001897static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
David Ahernc0b220c2019-05-22 20:27:57 -07001898 struct fib6_gc_args *gc_args,
1899 unsigned long now)
Wei Wangc757faa2017-10-06 12:06:01 -07001900{
1901 struct rt6_exception_bucket *bucket;
1902 struct rt6_exception *rt6_ex;
1903 struct hlist_node *tmp;
1904 int i;
1905
David Aherncc5c0732019-05-22 20:27:58 -07001906 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangc757faa2017-10-06 12:06:01 -07001907 return;
1908
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001909 rcu_read_lock_bh();
1910 spin_lock(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07001911 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangc757faa2017-10-06 12:06:01 -07001912 if (bucket) {
1913 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1914 hlist_for_each_entry_safe(rt6_ex, tmp,
1915 &bucket->chain, hlist) {
1916 rt6_age_examine_exception(bucket, rt6_ex,
1917 gc_args, now);
1918 }
1919 bucket++;
1920 }
1921 }
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001922 spin_unlock(&rt6_exception_lock);
1923 rcu_read_unlock_bh();
Wei Wangc757faa2017-10-06 12:06:01 -07001924}
1925
David Aherncc5c0732019-05-22 20:27:58 -07001926void rt6_age_exceptions(struct fib6_info *f6i,
David Ahernc0b220c2019-05-22 20:27:57 -07001927 struct fib6_gc_args *gc_args,
1928 unsigned long now)
1929{
David Ahern1cf844c2019-05-22 20:27:59 -07001930 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
David Ahernc0b220c2019-05-22 20:27:57 -07001931}
1932
David Ahern1d053da2018-05-09 20:34:21 -07001933/* must be called with rcu lock held */
David Aherneffda4d2019-04-16 14:36:10 -07001934int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
1935 struct flowi6 *fl6, struct fib6_result *res, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001937 struct fib6_node *fn, *saved_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
David Ahern64547432018-05-09 20:34:19 -07001939 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001940 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
David Ahernca254492015-10-12 11:47:10 -07001942 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1943 oif = 0;
1944
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001945redo_rt6_select:
David Aherneffda4d2019-04-16 14:36:10 -07001946 rt6_select(net, fn, oif, res, strict);
1947 if (res->f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001948 fn = fib6_backtrack(fn, &fl6->saddr);
1949 if (fn)
1950 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001951 else if (strict & RT6_LOOKUP_F_REACHABLE) {
1952 /* also consider unreachable route */
1953 strict &= ~RT6_LOOKUP_F_REACHABLE;
1954 fn = saved_fn;
1955 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001956 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001957 }
1958
David Aherneffda4d2019-04-16 14:36:10 -07001959 trace_fib6_table_lookup(net, res, table, fl6);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -08001960
David Aherneffda4d2019-04-16 14:36:10 -07001961 return 0;
David Ahern1d053da2018-05-09 20:34:21 -07001962}
1963
1964struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
1965 int oif, struct flowi6 *fl6,
1966 const struct sk_buff *skb, int flags)
1967{
David Ahernb1d40992019-04-16 14:35:59 -07001968 struct fib6_result res = {};
David Ahern1d053da2018-05-09 20:34:21 -07001969 struct rt6_info *rt;
1970 int strict = 0;
1971
1972 strict |= flags & RT6_LOOKUP_F_IFACE;
1973 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
1974 if (net->ipv6.devconf_all->forwarding == 0)
1975 strict |= RT6_LOOKUP_F_REACHABLE;
1976
1977 rcu_read_lock();
1978
David Aherneffda4d2019-04-16 14:36:10 -07001979 fib6_table_lookup(net, table, oif, fl6, &res, strict);
David Ahernb1d40992019-04-16 14:35:59 -07001980 if (res.f6i == net->ipv6.fib6_null_entry) {
David Ahern421842e2018-04-17 17:33:18 -07001981 rt = net->ipv6.ip6_null_entry;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001982 rcu_read_unlock();
Wei Wangd3843fe2017-10-06 12:06:06 -07001983 dst_hold(&rt->dst);
Wei Wangd3843fe2017-10-06 12:06:06 -07001984 return rt;
David Ahern23fb93a2018-04-17 17:33:23 -07001985 }
1986
David Ahernb1d40992019-04-16 14:35:59 -07001987 fib6_select_path(net, &res, fl6, oif, false, skb, strict);
David Ahernd83009d2019-04-09 14:41:17 -07001988
David Ahern23fb93a2018-04-17 17:33:23 -07001989 /*Search through exception table */
David Ahern7e4b5122019-04-16 14:36:00 -07001990 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
David Ahern23fb93a2018-04-17 17:33:23 -07001991 if (rt) {
David Ahern10585b42019-03-20 09:24:50 -07001992 if (ip6_hold_safe(net, &rt))
Wei Wangd3843fe2017-10-06 12:06:06 -07001993 dst_use_noref(&rt->dst, jiffies);
David Ahernd4ead6b2018-04-17 17:33:16 -07001994
Wei Wang66f5d6c2017-10-06 12:06:10 -07001995 rcu_read_unlock();
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001996 return rt;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001997 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
David Ahernb1d40992019-04-16 14:35:59 -07001998 !res.nh->fib_nh_gw_family)) {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001999 /* Create a RTF_CACHE clone which will not be
2000 * owned by the fib6 tree. It is for the special case where
2001 * the daddr in the skb during the neighbor look-up is different
2002 * from the fl6->daddr used to look-up route here.
2003 */
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002004 struct rt6_info *uncached_rt;
2005
David Ahern85bd05d2019-04-16 14:36:01 -07002006 uncached_rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002007
David Ahern4d85cd02018-04-20 15:37:59 -07002008 rcu_read_unlock();
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002009
Wei Wang1cfb71e2017-06-17 10:42:33 -07002010 if (uncached_rt) {
2011 /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
2012 * No need for another dst_hold()
2013 */
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07002014 rt6_uncached_list_add(uncached_rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002015 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Wei Wang1cfb71e2017-06-17 10:42:33 -07002016 } else {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002017 uncached_rt = net->ipv6.ip6_null_entry;
Wei Wang1cfb71e2017-06-17 10:42:33 -07002018 dst_hold(&uncached_rt->dst);
2019 }
David Ahernb8115802015-11-19 12:24:22 -08002020
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002021 return uncached_rt;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002022 } else {
2023 /* Get a percpu copy */
2024
2025 struct rt6_info *pcpu_rt;
2026
Eric Dumazet951f7882017-10-08 21:07:18 -07002027 local_bh_disable();
David Aherndb3fede2019-04-16 14:36:03 -07002028 pcpu_rt = rt6_get_pcpu_route(&res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002029
David Ahern93531c62018-04-17 17:33:25 -07002030 if (!pcpu_rt)
David Aherndb3fede2019-04-16 14:36:03 -07002031 pcpu_rt = rt6_make_pcpu_route(net, &res);
David Ahern93531c62018-04-17 17:33:25 -07002032
Eric Dumazet951f7882017-10-08 21:07:18 -07002033 local_bh_enable();
2034 rcu_read_unlock();
David Ahernd4bea422018-05-09 20:34:24 -07002035
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002036 return pcpu_rt;
2037 }
Thomas Grafc71099a2006-08-04 23:20:06 -07002038}
David Ahern9ff74382016-06-13 13:44:19 -07002039EXPORT_SYMBOL_GPL(ip6_pol_route);
Thomas Grafc71099a2006-08-04 23:20:06 -07002040
David Ahernb75cc8f2018-03-02 08:32:17 -08002041static struct rt6_info *ip6_pol_route_input(struct net *net,
2042 struct fib6_table *table,
2043 struct flowi6 *fl6,
2044 const struct sk_buff *skb,
2045 int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002046{
David Ahernb75cc8f2018-03-02 08:32:17 -08002047 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002048}
2049
Mahesh Bandeward409b842016-09-16 12:59:08 -07002050struct dst_entry *ip6_route_input_lookup(struct net *net,
2051 struct net_device *dev,
David Ahernb75cc8f2018-03-02 08:32:17 -08002052 struct flowi6 *fl6,
2053 const struct sk_buff *skb,
2054 int flags)
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002055{
2056 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2057 flags |= RT6_LOOKUP_F_IFACE;
2058
David Ahernb75cc8f2018-03-02 08:32:17 -08002059 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002060}
Mahesh Bandeward409b842016-09-16 12:59:08 -07002061EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002062
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002063static void ip6_multipath_l3_keys(const struct sk_buff *skb,
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002064 struct flow_keys *keys,
2065 struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002066{
2067 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2068 const struct ipv6hdr *key_iph = outer_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002069 struct flow_keys *_flkeys = flkeys;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002070 const struct ipv6hdr *inner_iph;
2071 const struct icmp6hdr *icmph;
2072 struct ipv6hdr _inner_iph;
Eric Dumazetcea67a22018-04-29 09:54:59 -07002073 struct icmp6hdr _icmph;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002074
2075 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2076 goto out;
2077
Eric Dumazetcea67a22018-04-29 09:54:59 -07002078 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2079 sizeof(_icmph), &_icmph);
2080 if (!icmph)
2081 goto out;
2082
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002083 if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
2084 icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
2085 icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
2086 icmph->icmp6_type != ICMPV6_PARAMPROB)
2087 goto out;
2088
2089 inner_iph = skb_header_pointer(skb,
2090 skb_transport_offset(skb) + sizeof(*icmph),
2091 sizeof(_inner_iph), &_inner_iph);
2092 if (!inner_iph)
2093 goto out;
2094
2095 key_iph = inner_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002096 _flkeys = NULL;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002097out:
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002098 if (_flkeys) {
2099 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2100 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2101 keys->tags.flow_label = _flkeys->tags.flow_label;
2102 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2103 } else {
2104 keys->addrs.v6addrs.src = key_iph->saddr;
2105 keys->addrs.v6addrs.dst = key_iph->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002106 keys->tags.flow_label = ip6_flowlabel(key_iph);
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002107 keys->basic.ip_proto = key_iph->nexthdr;
2108 }
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002109}
2110
2111/* if skb is set it will be used and fl6 can be NULL */
David Ahernb4bac172018-03-02 08:32:18 -08002112u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2113 const struct sk_buff *skb, struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002114{
2115 struct flow_keys hash_keys;
David Ahern9a2a5372018-03-02 08:32:15 -08002116 u32 mhash;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002117
David S. Millerbbfa0472018-03-12 11:09:33 -04002118 switch (ip6_multipath_hash_policy(net)) {
David Ahernb4bac172018-03-02 08:32:18 -08002119 case 0:
2120 memset(&hash_keys, 0, sizeof(hash_keys));
2121 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2122 if (skb) {
2123 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2124 } else {
2125 hash_keys.addrs.v6addrs.src = fl6->saddr;
2126 hash_keys.addrs.v6addrs.dst = fl6->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002127 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
David Ahernb4bac172018-03-02 08:32:18 -08002128 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2129 }
2130 break;
2131 case 1:
2132 if (skb) {
2133 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2134 struct flow_keys keys;
2135
2136 /* short-circuit if we already have L4 hash present */
2137 if (skb->l4_hash)
2138 return skb_get_hash_raw(skb) >> 1;
2139
2140 memset(&hash_keys, 0, sizeof(hash_keys));
2141
2142 if (!flkeys) {
2143 skb_flow_dissect_flow_keys(skb, &keys, flag);
2144 flkeys = &keys;
2145 }
2146 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2147 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2148 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2149 hash_keys.ports.src = flkeys->ports.src;
2150 hash_keys.ports.dst = flkeys->ports.dst;
2151 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2152 } else {
2153 memset(&hash_keys, 0, sizeof(hash_keys));
2154 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2155 hash_keys.addrs.v6addrs.src = fl6->saddr;
2156 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2157 hash_keys.ports.src = fl6->fl6_sport;
2158 hash_keys.ports.dst = fl6->fl6_dport;
2159 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2160 }
2161 break;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002162 }
David Ahern9a2a5372018-03-02 08:32:15 -08002163 mhash = flow_hash_from_keys(&hash_keys);
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002164
David Ahern9a2a5372018-03-02 08:32:15 -08002165 return mhash >> 1;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002166}
2167
Thomas Grafc71099a2006-08-04 23:20:06 -07002168void ip6_route_input(struct sk_buff *skb)
2169{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002170 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002171 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002172 int flags = RT6_LOOKUP_F_HAS_SADDR;
Jiri Benc904af042015-08-20 13:56:31 +02002173 struct ip_tunnel_info *tun_info;
David S. Miller4c9483b2011-03-12 16:22:43 -05002174 struct flowi6 fl6 = {
David Aherne0d56fd2016-09-10 12:09:57 -07002175 .flowi6_iif = skb->dev->ifindex,
David S. Miller4c9483b2011-03-12 16:22:43 -05002176 .daddr = iph->daddr,
2177 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002178 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05002179 .flowi6_mark = skb->mark,
2180 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07002181 };
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002182 struct flow_keys *flkeys = NULL, _flkeys;
Thomas Grafadaa70b2006-10-13 15:01:03 -07002183
Jiri Benc904af042015-08-20 13:56:31 +02002184 tun_info = skb_tunnel_info(skb);
Jiri Benc46fa0622015-08-28 20:48:19 +02002185 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
Jiri Benc904af042015-08-20 13:56:31 +02002186 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002187
2188 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2189 flkeys = &_flkeys;
2190
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002191 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
David Ahernb4bac172018-03-02 08:32:18 -08002192 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
Jiri Benc06e9d042015-08-20 13:56:26 +02002193 skb_dst_drop(skb);
David Ahernb75cc8f2018-03-02 08:32:17 -08002194 skb_dst_set(skb,
2195 ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07002196}
2197
David Ahernb75cc8f2018-03-02 08:32:17 -08002198static struct rt6_info *ip6_pol_route_output(struct net *net,
2199 struct fib6_table *table,
2200 struct flowi6 *fl6,
2201 const struct sk_buff *skb,
2202 int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002203{
David Ahernb75cc8f2018-03-02 08:32:17 -08002204 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07002205}
2206
Paolo Abeni6f21c962016-01-29 12:30:19 +01002207struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
2208 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002209{
David Ahernd46a9d62015-10-21 08:42:22 -07002210 bool any_src;
Thomas Grafc71099a2006-08-04 23:20:06 -07002211
Robert Shearman3ede0bb2018-09-19 13:56:53 +01002212 if (ipv6_addr_type(&fl6->daddr) &
2213 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
David Ahern4c1feac2016-09-10 12:09:56 -07002214 struct dst_entry *dst;
2215
2216 dst = l3mdev_link_scope_lookup(net, fl6);
2217 if (dst)
2218 return dst;
2219 }
David Ahernca254492015-10-12 11:47:10 -07002220
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002221 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00002222
David Ahernd46a9d62015-10-21 08:42:22 -07002223 any_src = ipv6_addr_any(&fl6->saddr);
David Ahern741a11d2015-09-28 10:12:13 -07002224 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
David Ahernd46a9d62015-10-21 08:42:22 -07002225 (fl6->flowi6_oif && any_src))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07002226 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07002227
David Ahernd46a9d62015-10-21 08:42:22 -07002228 if (!any_src)
Thomas Grafadaa70b2006-10-13 15:01:03 -07002229 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00002230 else if (sk)
2231 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002232
David Ahernb75cc8f2018-03-02 08:32:17 -08002233 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234}
Paolo Abeni6f21c962016-01-29 12:30:19 +01002235EXPORT_SYMBOL_GPL(ip6_route_output_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
David S. Miller2774c132011-03-01 14:59:04 -08002237struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07002238{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002239 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
Wei Wang1dbe32522017-06-17 10:42:26 -07002240 struct net_device *loopback_dev = net->loopback_dev;
David S. Miller14e50e52007-05-24 18:17:54 -07002241 struct dst_entry *new = NULL;
2242
Wei Wang1dbe32522017-06-17 10:42:26 -07002243 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
Steffen Klassert62cf27e2017-10-09 08:39:43 +02002244 DST_OBSOLETE_DEAD, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07002245 if (rt) {
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002246 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002247 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002248
Changli Gaod8d1f302010-06-10 23:31:35 -07002249 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07002250 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08002251 new->input = dst_discard;
Eric W. Biedermanede20592015-10-07 16:48:47 -05002252 new->output = dst_discard_out;
David S. Miller14e50e52007-05-24 18:17:54 -07002253
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002254 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07002255
Wei Wang1dbe32522017-06-17 10:42:26 -07002256 rt->rt6i_idev = in6_dev_get(loopback_dev);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002257 rt->rt6i_gateway = ort->rt6i_gateway;
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002258 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
David S. Miller14e50e52007-05-24 18:17:54 -07002259
2260 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2261#ifdef CONFIG_IPV6_SUBTREES
2262 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2263#endif
David S. Miller14e50e52007-05-24 18:17:54 -07002264 }
2265
David S. Miller69ead7a2011-03-01 14:45:33 -08002266 dst_release(dst_orig);
2267 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07002268}
David S. Miller14e50e52007-05-24 18:17:54 -07002269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270/*
2271 * Destination cache support functions
2272 */
2273
David Ahern8d1c8022018-04-17 17:33:26 -07002274static bool fib6_check(struct fib6_info *f6i, u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002275{
Steffen Klassert36143642017-08-25 09:05:42 +02002276 u32 rt_cookie = 0;
Wei Wangc5cff852017-08-21 09:47:10 -07002277
David Ahern8ae86972018-04-20 15:38:03 -07002278 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
David Ahern93531c62018-04-17 17:33:25 -07002279 return false;
2280
2281 if (fib6_check_expired(f6i))
2282 return false;
2283
2284 return true;
2285}
2286
David Aherna68886a2018-04-20 15:38:02 -07002287static struct dst_entry *rt6_check(struct rt6_info *rt,
2288 struct fib6_info *from,
2289 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002290{
Wei Wangc5cff852017-08-21 09:47:10 -07002291 u32 rt_cookie = 0;
2292
David Aherna68886a2018-04-20 15:38:02 -07002293 if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) ||
David Ahern93531c62018-04-17 17:33:25 -07002294 rt_cookie != cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002295 return NULL;
2296
2297 if (rt6_check_expired(rt))
2298 return NULL;
2299
2300 return &rt->dst;
2301}
2302
David Aherna68886a2018-04-20 15:38:02 -07002303static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2304 struct fib6_info *from,
2305 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002306{
Martin KaFai Lau5973fb12015-11-11 11:51:07 -08002307 if (!__rt6_check_expired(rt) &&
2308 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
David Aherna68886a2018-04-20 15:38:02 -07002309 fib6_check(from, cookie))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002310 return &rt->dst;
2311 else
2312 return NULL;
2313}
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2316{
David Aherna87b7dc2018-04-20 15:38:00 -07002317 struct dst_entry *dst_ret;
David Aherna68886a2018-04-20 15:38:02 -07002318 struct fib6_info *from;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 struct rt6_info *rt;
2320
David Aherna87b7dc2018-04-20 15:38:00 -07002321 rt = container_of(dst, struct rt6_info, dst);
2322
2323 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00002325 /* All IPV6 dsts are created with ->obsolete set to the value
2326 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2327 * into this function always.
2328 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02002329
David Aherna68886a2018-04-20 15:38:02 -07002330 from = rcu_dereference(rt->from);
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002331
David Aherna68886a2018-04-20 15:38:02 -07002332 if (from && (rt->rt6i_flags & RTF_PCPU ||
2333 unlikely(!list_empty(&rt->rt6i_uncached))))
2334 dst_ret = rt6_dst_from_check(rt, from, cookie);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002335 else
David Aherna68886a2018-04-20 15:38:02 -07002336 dst_ret = rt6_check(rt, from, cookie);
David Aherna87b7dc2018-04-20 15:38:00 -07002337
2338 rcu_read_unlock();
2339
2340 return dst_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
2342
2343static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2344{
2345 struct rt6_info *rt = (struct rt6_info *) dst;
2346
2347 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002348 if (rt->rt6i_flags & RTF_CACHE) {
David Ahernc3c14da2018-04-23 11:32:06 -07002349 rcu_read_lock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002350 if (rt6_check_expired(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07002351 rt6_remove_exception_rt(rt);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002352 dst = NULL;
2353 }
David Ahernc3c14da2018-04-23 11:32:06 -07002354 rcu_read_unlock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002355 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002357 dst = NULL;
2358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002360 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
2363static void ip6_link_failure(struct sk_buff *skb)
2364{
2365 struct rt6_info *rt;
2366
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002367 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Eric Dumazetadf30902009-06-02 05:19:30 +00002369 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (rt) {
David Ahern8a14e462018-04-23 11:32:07 -07002371 rcu_read_lock();
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002372 if (rt->rt6i_flags & RTF_CACHE) {
Xin Long761f6022018-11-14 00:48:28 +08002373 rt6_remove_exception_rt(rt);
Wei Wangc5cff852017-08-21 09:47:10 -07002374 } else {
David Aherna68886a2018-04-20 15:38:02 -07002375 struct fib6_info *from;
Wei Wangc5cff852017-08-21 09:47:10 -07002376 struct fib6_node *fn;
2377
David Aherna68886a2018-04-20 15:38:02 -07002378 from = rcu_dereference(rt->from);
2379 if (from) {
2380 fn = rcu_dereference(from->fib6_node);
2381 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2382 fn->fn_sernum = -1;
2383 }
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002384 }
David Ahern8a14e462018-04-23 11:32:07 -07002385 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387}
2388
David Ahern6a3e0302018-04-20 15:37:57 -07002389static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2390{
David Aherna68886a2018-04-20 15:38:02 -07002391 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2392 struct fib6_info *from;
2393
2394 rcu_read_lock();
2395 from = rcu_dereference(rt0->from);
2396 if (from)
2397 rt0->dst.expires = from->expires;
2398 rcu_read_unlock();
2399 }
David Ahern6a3e0302018-04-20 15:37:57 -07002400
2401 dst_set_expires(&rt0->dst, timeout);
2402 rt0->rt6i_flags |= RTF_EXPIRES;
2403}
2404
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002405static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2406{
2407 struct net *net = dev_net(rt->dst.dev);
2408
David Ahernd4ead6b2018-04-17 17:33:16 -07002409 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002410 rt->rt6i_flags |= RTF_MODIFIED;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002411 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2412}
2413
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002414static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2415{
2416 return !(rt->rt6i_flags & RTF_CACHE) &&
Paolo Abeni1490ed22019-02-15 18:15:37 +01002417 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002418}
2419
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002420static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2421 const struct ipv6hdr *iph, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
Julian Anastasov0dec8792017-02-06 23:14:16 +02002423 const struct in6_addr *daddr, *saddr;
Ian Morris67ba4152014-08-24 21:53:10 +01002424 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Xin Long19bda362016-10-28 18:18:01 +08002426 if (dst_metric_locked(dst, RTAX_MTU))
2427 return;
2428
Julian Anastasov0dec8792017-02-06 23:14:16 +02002429 if (iph) {
2430 daddr = &iph->daddr;
2431 saddr = &iph->saddr;
2432 } else if (sk) {
2433 daddr = &sk->sk_v6_daddr;
2434 saddr = &inet6_sk(sk)->saddr;
2435 } else {
2436 daddr = NULL;
2437 saddr = NULL;
2438 }
2439 dst_confirm_neigh(dst, daddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002440 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2441 if (mtu >= dst_mtu(dst))
2442 return;
David S. Miller81aded22012-06-15 14:54:11 -07002443
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002444 if (!rt6_cache_allowed_for_pmtu(rt6)) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002445 rt6_do_update_pmtu(rt6, mtu);
Wei Wang2b760fc2017-10-06 12:06:03 -07002446 /* update rt6_ex->stamp for cache */
2447 if (rt6->rt6i_flags & RTF_CACHE)
2448 rt6_update_exception_stamp_rt(rt6);
Julian Anastasov0dec8792017-02-06 23:14:16 +02002449 } else if (daddr) {
David Ahern85bd05d2019-04-16 14:36:01 -07002450 struct fib6_result res = {};
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002451 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01002452
David Ahern4d85cd02018-04-20 15:37:59 -07002453 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07002454 res.f6i = rcu_dereference(rt6->from);
2455 if (!res.f6i) {
Jonathan Lemon9c69a132019-04-14 14:21:29 -07002456 rcu_read_unlock();
2457 return;
2458 }
David Ahern1cf844c2019-05-22 20:27:59 -07002459 res.nh = res.f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -07002460 res.fib6_flags = res.f6i->fib6_flags;
2461 res.fib6_type = res.f6i->fib6_type;
2462
David Ahern85bd05d2019-04-16 14:36:01 -07002463 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002464 if (nrt6) {
2465 rt6_do_update_pmtu(nrt6, mtu);
David Ahern5012f0a2019-04-16 14:36:05 -07002466 if (rt6_insert_exception(nrt6, &res))
Wei Wang2b760fc2017-10-06 12:06:03 -07002467 dst_release_immediate(&nrt6->dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002468 }
David Aherna68886a2018-04-20 15:38:02 -07002469 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
2471}
2472
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002473static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2474 struct sk_buff *skb, u32 mtu)
2475{
2476 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
2477}
2478
David S. Miller42ae66c2012-06-15 20:01:57 -07002479void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002480 int oif, u32 mark, kuid_t uid)
David S. Miller81aded22012-06-15 14:54:11 -07002481{
2482 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2483 struct dst_entry *dst;
Maciej Żenczykowskidc920952018-09-29 23:44:51 -07002484 struct flowi6 fl6 = {
2485 .flowi6_oif = oif,
2486 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2487 .daddr = iph->daddr,
2488 .saddr = iph->saddr,
2489 .flowlabel = ip6_flowinfo(iph),
2490 .flowi6_uid = uid,
2491 };
David S. Miller81aded22012-06-15 14:54:11 -07002492
2493 dst = ip6_route_output(net, NULL, &fl6);
2494 if (!dst->error)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002495 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
David S. Miller81aded22012-06-15 14:54:11 -07002496 dst_release(dst);
2497}
2498EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2499
2500void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2501{
David Ahern7ddacfa2018-11-18 10:45:30 -08002502 int oif = sk->sk_bound_dev_if;
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002503 struct dst_entry *dst;
2504
David Ahern7ddacfa2018-11-18 10:45:30 -08002505 if (!oif && skb->dev)
2506 oif = l3mdev_master_ifindex(skb->dev);
2507
2508 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002509
2510 dst = __sk_dst_get(sk);
2511 if (!dst || !dst->obsolete ||
2512 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2513 return;
2514
2515 bh_lock_sock(sk);
2516 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2517 ip6_datagram_dst_update(sk, false);
2518 bh_unlock_sock(sk);
David S. Miller81aded22012-06-15 14:54:11 -07002519}
2520EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2521
Alexey Kodanev7d6850f2018-04-03 15:00:07 +03002522void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2523 const struct flowi6 *fl6)
2524{
2525#ifdef CONFIG_IPV6_SUBTREES
2526 struct ipv6_pinfo *np = inet6_sk(sk);
2527#endif
2528
2529 ip6_dst_store(sk, dst,
2530 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2531 &sk->sk_v6_daddr : NULL,
2532#ifdef CONFIG_IPV6_SUBTREES
2533 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2534 &np->saddr :
2535#endif
2536 NULL);
2537}
2538
David Ahern9b6b35a2019-04-16 14:36:02 -07002539static bool ip6_redirect_nh_match(const struct fib6_result *res,
David Ahern0b34eb02019-04-09 14:41:19 -07002540 struct flowi6 *fl6,
2541 const struct in6_addr *gw,
2542 struct rt6_info **ret)
2543{
David Ahern9b6b35a2019-04-16 14:36:02 -07002544 const struct fib6_nh *nh = res->nh;
2545
David Ahern0b34eb02019-04-09 14:41:19 -07002546 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
2547 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
2548 return false;
2549
2550 /* rt_cache's gateway might be different from its 'parent'
2551 * in the case of an ip redirect.
2552 * So we keep searching in the exception table if the gateway
2553 * is different.
2554 */
2555 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
2556 struct rt6_info *rt_cache;
2557
David Ahern9b6b35a2019-04-16 14:36:02 -07002558 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
David Ahern0b34eb02019-04-09 14:41:19 -07002559 if (rt_cache &&
2560 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
2561 *ret = rt_cache;
2562 return true;
2563 }
2564 return false;
2565 }
2566 return true;
2567}
2568
Duan Jiongb55b76b2013-09-04 19:44:21 +08002569/* Handle redirects */
2570struct ip6rd_flowi {
2571 struct flowi6 fl6;
2572 struct in6_addr gateway;
2573};
2574
2575static struct rt6_info *__ip6_route_redirect(struct net *net,
2576 struct fib6_table *table,
2577 struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08002578 const struct sk_buff *skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002579 int flags)
2580{
2581 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
David Ahern0b34eb02019-04-09 14:41:19 -07002582 struct rt6_info *ret = NULL;
David Ahern9b6b35a2019-04-16 14:36:02 -07002583 struct fib6_result res = {};
David Ahern8d1c8022018-04-17 17:33:26 -07002584 struct fib6_info *rt;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002585 struct fib6_node *fn;
2586
2587 /* Get the "current" route for this destination and
Alexander Alemayhu67c408c2017-01-07 23:53:00 +01002588 * check if the redirect has come from appropriate router.
Duan Jiongb55b76b2013-09-04 19:44:21 +08002589 *
2590 * RFC 4861 specifies that redirects should only be
2591 * accepted if they come from the nexthop to the target.
2592 * Due to the way the routes are chosen, this notion
2593 * is a bit fuzzy and one might need to check all possible
2594 * routes.
2595 */
2596
Wei Wang66f5d6c2017-10-06 12:06:10 -07002597 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07002598 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002599restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002600 for_each_fib6_node_rt_rcu(fn) {
David Ahern9b6b35a2019-04-16 14:36:02 -07002601 res.f6i = rt;
David Ahern1cf844c2019-05-22 20:27:59 -07002602 res.nh = rt->fib6_nh;
David Ahern9b6b35a2019-04-16 14:36:02 -07002603
David Ahern14895682018-04-17 17:33:17 -07002604 if (fib6_check_expired(rt))
Duan Jiongb55b76b2013-09-04 19:44:21 +08002605 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002606 if (rt->fib6_flags & RTF_REJECT)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002607 break;
David Ahern9b6b35a2019-04-16 14:36:02 -07002608 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway, &ret))
David Ahern0b34eb02019-04-09 14:41:19 -07002609 goto out;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002610 }
2611
2612 if (!rt)
David Ahern421842e2018-04-17 17:33:18 -07002613 rt = net->ipv6.fib6_null_entry;
David Ahern93c2fb22018-04-18 15:38:59 -07002614 else if (rt->fib6_flags & RTF_REJECT) {
David Ahern23fb93a2018-04-17 17:33:23 -07002615 ret = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002616 goto out;
2617 }
2618
David Ahern421842e2018-04-17 17:33:18 -07002619 if (rt == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002620 fn = fib6_backtrack(fn, &fl6->saddr);
2621 if (fn)
2622 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002623 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002624
David Ahern9b6b35a2019-04-16 14:36:02 -07002625 res.f6i = rt;
David Ahern1cf844c2019-05-22 20:27:59 -07002626 res.nh = rt->fib6_nh;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002627out:
David Ahern7d21fec2019-04-16 14:36:11 -07002628 if (ret) {
David Ahern10585b42019-03-20 09:24:50 -07002629 ip6_hold_safe(net, &ret);
David Ahern7d21fec2019-04-16 14:36:11 -07002630 } else {
2631 res.fib6_flags = res.f6i->fib6_flags;
2632 res.fib6_type = res.f6i->fib6_type;
David Ahern9b6b35a2019-04-16 14:36:02 -07002633 ret = ip6_create_rt_rcu(&res);
David Ahern7d21fec2019-04-16 14:36:11 -07002634 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002635
Wei Wang66f5d6c2017-10-06 12:06:10 -07002636 rcu_read_unlock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08002637
David Ahern8ff2e5b2019-04-16 14:36:09 -07002638 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahern23fb93a2018-04-17 17:33:23 -07002639 return ret;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002640};
2641
2642static struct dst_entry *ip6_route_redirect(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08002643 const struct flowi6 *fl6,
2644 const struct sk_buff *skb,
2645 const struct in6_addr *gateway)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002646{
2647 int flags = RT6_LOOKUP_F_HAS_SADDR;
2648 struct ip6rd_flowi rdfl;
2649
2650 rdfl.fl6 = *fl6;
2651 rdfl.gateway = *gateway;
2652
David Ahernb75cc8f2018-03-02 08:32:17 -08002653 return fib6_rule_lookup(net, &rdfl.fl6, skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002654 flags, __ip6_route_redirect);
2655}
2656
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002657void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2658 kuid_t uid)
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002659{
2660 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2661 struct dst_entry *dst;
Maciej Żenczykowski1f7f10a2018-09-29 23:44:48 -07002662 struct flowi6 fl6 = {
2663 .flowi6_iif = LOOPBACK_IFINDEX,
2664 .flowi6_oif = oif,
2665 .flowi6_mark = mark,
2666 .daddr = iph->daddr,
2667 .saddr = iph->saddr,
2668 .flowlabel = ip6_flowinfo(iph),
2669 .flowi6_uid = uid,
2670 };
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002671
David Ahernb75cc8f2018-03-02 08:32:17 -08002672 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002673 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002674 dst_release(dst);
2675}
2676EXPORT_SYMBOL_GPL(ip6_redirect);
2677
Maciej Żenczykowskid4563362018-09-29 23:44:50 -07002678void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
Duan Jiongc92a59e2013-08-22 12:07:35 +08002679{
2680 const struct ipv6hdr *iph = ipv6_hdr(skb);
2681 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2682 struct dst_entry *dst;
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07002683 struct flowi6 fl6 = {
2684 .flowi6_iif = LOOPBACK_IFINDEX,
2685 .flowi6_oif = oif,
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07002686 .daddr = msg->dest,
2687 .saddr = iph->daddr,
2688 .flowi6_uid = sock_net_uid(net, NULL),
2689 };
Duan Jiongc92a59e2013-08-22 12:07:35 +08002690
David Ahernb75cc8f2018-03-02 08:32:17 -08002691 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002692 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08002693 dst_release(dst);
2694}
2695
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002696void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
2697{
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002698 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2699 sk->sk_uid);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002700}
2701EXPORT_SYMBOL_GPL(ip6_sk_redirect);
2702
David S. Miller0dbaee32010-12-13 12:52:14 -08002703static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
David S. Miller0dbaee32010-12-13 12:52:14 -08002705 struct net_device *dev = dst->dev;
2706 unsigned int mtu = dst_mtu(dst);
2707 struct net *net = dev_net(dev);
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
2710
Daniel Lezcano55786892008-03-04 13:47:47 -08002711 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
2712 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
2714 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002715 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
2716 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
2717 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 * rely only on pmtu discovery"
2719 */
2720 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
2721 mtu = IPV6_MAXPLEN;
2722 return mtu;
2723}
2724
Steffen Klassertebb762f2011-11-23 02:12:51 +00002725static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002726{
David S. Millerd33e4552010-12-14 13:01:14 -08002727 struct inet6_dev *idev;
David Ahernd4ead6b2018-04-17 17:33:16 -07002728 unsigned int mtu;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002729
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002730 mtu = dst_metric_raw(dst, RTAX_MTU);
2731 if (mtu)
2732 goto out;
2733
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002734 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08002735
2736 rcu_read_lock();
2737 idev = __in6_dev_get(dst->dev);
2738 if (idev)
2739 mtu = idev->cnf.mtu6;
2740 rcu_read_unlock();
2741
Eric Dumazet30f78d82014-04-10 21:23:36 -07002742out:
Roopa Prabhu14972cb2016-08-24 20:10:43 -07002743 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2744
2745 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
David S. Millerd33e4552010-12-14 13:01:14 -08002746}
2747
David Ahern901731b2018-05-21 09:08:14 -07002748/* MTU selection:
2749 * 1. mtu on route is locked - use it
2750 * 2. mtu from nexthop exception
2751 * 3. mtu from egress device
2752 *
2753 * based on ip6_dst_mtu_forward and exception logic of
2754 * rt6_find_cached_rt; called with rcu_read_lock
2755 */
David Ahernb748f262019-04-16 14:36:06 -07002756u32 ip6_mtu_from_fib6(const struct fib6_result *res,
2757 const struct in6_addr *daddr,
2758 const struct in6_addr *saddr)
David Ahern901731b2018-05-21 09:08:14 -07002759{
David Ahernb748f262019-04-16 14:36:06 -07002760 const struct fib6_nh *nh = res->nh;
2761 struct fib6_info *f6i = res->f6i;
David Ahern901731b2018-05-21 09:08:14 -07002762 struct inet6_dev *idev;
Wei Wang510e2ce2019-05-16 13:30:54 -07002763 struct rt6_info *rt;
David Ahern901731b2018-05-21 09:08:14 -07002764 u32 mtu = 0;
2765
2766 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
2767 mtu = f6i->fib6_pmtu;
2768 if (mtu)
2769 goto out;
2770 }
2771
Wei Wang510e2ce2019-05-16 13:30:54 -07002772 rt = rt6_find_cached_rt(res, daddr, saddr);
2773 if (unlikely(rt)) {
2774 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
2775 } else {
David Ahernb748f262019-04-16 14:36:06 -07002776 struct net_device *dev = nh->fib_nh_dev;
David Ahern901731b2018-05-21 09:08:14 -07002777
2778 mtu = IPV6_MIN_MTU;
2779 idev = __in6_dev_get(dev);
2780 if (idev && idev->cnf.mtu6 > mtu)
2781 mtu = idev->cnf.mtu6;
2782 }
2783
2784 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2785out:
David Ahernb748f262019-04-16 14:36:06 -07002786 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahern901731b2018-05-21 09:08:14 -07002787}
2788
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08002789struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05002790 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
David S. Miller87a11572011-12-06 17:04:13 -05002792 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 struct rt6_info *rt;
2794 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002795 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
David S. Miller38308472011-12-03 18:02:47 -05002797 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00002798 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Martin KaFai Lauad706862015-08-14 11:05:52 -07002800 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05002801 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05002803 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 goto out;
2805 }
2806
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002807 rt->dst.flags |= DST_HOST;
Brendan McGrath588753f2017-12-13 22:14:57 +11002808 rt->dst.input = ip6_input;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002809 rt->dst.output = ip6_output;
Julian Anastasov550bab42013-10-20 15:43:04 +03002810 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05002811 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002812 rt->rt6i_dst.plen = 128;
2813 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08002814 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Ido Schimmel4c981e22018-01-07 12:45:04 +02002816 /* Add this dst into uncached_list so that rt6_disable_ip() can
Wei Wang587fea72017-06-17 10:42:36 -07002817 * do proper release of the net_device
2818 */
2819 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002820 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
David S. Miller87a11572011-12-06 17:04:13 -05002822 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
2823
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824out:
David S. Miller87a11572011-12-06 17:04:13 -05002825 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
Daniel Lezcano569d3642008-01-18 03:56:57 -08002828static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002830 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08002831 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
2832 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
2833 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
2834 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
2835 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00002836 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Eric Dumazetfc66f952010-10-08 06:37:34 +00002838 entries = dst_entries_get_fast(ops);
Michal Kubeček49a18d82013-08-01 10:04:24 +02002839 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00002840 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 goto out;
2842
Benjamin Thery6891a342008-03-04 13:49:47 -08002843 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08002844 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00002845 entries = dst_entries_get_slow(ops);
2846 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08002847 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08002849 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00002850 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851}
2852
David Ahern8c145862016-04-24 21:26:04 -07002853static struct rt6_info *ip6_nh_lookup_table(struct net *net,
2854 struct fib6_config *cfg,
David Ahernf4797b32018-01-25 16:55:08 -08002855 const struct in6_addr *gw_addr,
2856 u32 tbid, int flags)
David Ahern8c145862016-04-24 21:26:04 -07002857{
2858 struct flowi6 fl6 = {
2859 .flowi6_oif = cfg->fc_ifindex,
2860 .daddr = *gw_addr,
2861 .saddr = cfg->fc_prefsrc,
2862 };
2863 struct fib6_table *table;
2864 struct rt6_info *rt;
David Ahern8c145862016-04-24 21:26:04 -07002865
David Ahernf4797b32018-01-25 16:55:08 -08002866 table = fib6_get_table(net, tbid);
David Ahern8c145862016-04-24 21:26:04 -07002867 if (!table)
2868 return NULL;
2869
2870 if (!ipv6_addr_any(&cfg->fc_prefsrc))
2871 flags |= RT6_LOOKUP_F_HAS_SADDR;
2872
David Ahernf4797b32018-01-25 16:55:08 -08002873 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
David Ahernb75cc8f2018-03-02 08:32:17 -08002874 rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
David Ahern8c145862016-04-24 21:26:04 -07002875
2876 /* if table lookup failed, fall back to full lookup */
2877 if (rt == net->ipv6.ip6_null_entry) {
2878 ip6_rt_put(rt);
2879 rt = NULL;
2880 }
2881
2882 return rt;
2883}
2884
David Ahernfc1e64e2018-01-25 16:55:09 -08002885static int ip6_route_check_nh_onlink(struct net *net,
2886 struct fib6_config *cfg,
David Ahern9fbb7042018-03-13 08:29:36 -07002887 const struct net_device *dev,
David Ahernfc1e64e2018-01-25 16:55:09 -08002888 struct netlink_ext_ack *extack)
2889{
David Ahern44750f82018-02-06 13:17:06 -08002890 u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
David Ahernfc1e64e2018-01-25 16:55:09 -08002891 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2892 u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
Paolo Abenibf1dc8b2019-02-21 11:19:42 +01002893 struct fib6_info *from;
David Ahernfc1e64e2018-01-25 16:55:09 -08002894 struct rt6_info *grt;
2895 int err;
2896
2897 err = 0;
2898 grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
2899 if (grt) {
Paolo Abenibf1dc8b2019-02-21 11:19:42 +01002900 rcu_read_lock();
2901 from = rcu_dereference(grt->from);
David Ahern58e354c2018-02-06 12:14:12 -08002902 if (!grt->dst.error &&
David Ahern4ed591c2018-10-24 13:58:39 -07002903 /* ignore match if it is the default route */
Paolo Abenibf1dc8b2019-02-21 11:19:42 +01002904 from && !ipv6_addr_any(&from->fib6_dst.addr) &&
David Ahern58e354c2018-02-06 12:14:12 -08002905 (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
David Ahern44750f82018-02-06 13:17:06 -08002906 NL_SET_ERR_MSG(extack,
2907 "Nexthop has invalid gateway or device mismatch");
David Ahernfc1e64e2018-01-25 16:55:09 -08002908 err = -EINVAL;
2909 }
Paolo Abenibf1dc8b2019-02-21 11:19:42 +01002910 rcu_read_unlock();
David Ahernfc1e64e2018-01-25 16:55:09 -08002911
2912 ip6_rt_put(grt);
2913 }
2914
2915 return err;
2916}
2917
David Ahern1edce992018-01-25 16:55:07 -08002918static int ip6_route_check_nh(struct net *net,
2919 struct fib6_config *cfg,
2920 struct net_device **_dev,
2921 struct inet6_dev **idev)
2922{
2923 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2924 struct net_device *dev = _dev ? *_dev : NULL;
2925 struct rt6_info *grt = NULL;
2926 int err = -EHOSTUNREACH;
2927
2928 if (cfg->fc_table) {
David Ahernf4797b32018-01-25 16:55:08 -08002929 int flags = RT6_LOOKUP_F_IFACE;
2930
2931 grt = ip6_nh_lookup_table(net, cfg, gw_addr,
2932 cfg->fc_table, flags);
David Ahern1edce992018-01-25 16:55:07 -08002933 if (grt) {
2934 if (grt->rt6i_flags & RTF_GATEWAY ||
2935 (dev && dev != grt->dst.dev)) {
2936 ip6_rt_put(grt);
2937 grt = NULL;
2938 }
2939 }
2940 }
2941
2942 if (!grt)
David Ahernb75cc8f2018-03-02 08:32:17 -08002943 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
David Ahern1edce992018-01-25 16:55:07 -08002944
2945 if (!grt)
2946 goto out;
2947
2948 if (dev) {
2949 if (dev != grt->dst.dev) {
2950 ip6_rt_put(grt);
2951 goto out;
2952 }
2953 } else {
2954 *_dev = dev = grt->dst.dev;
2955 *idev = grt->rt6i_idev;
2956 dev_hold(dev);
2957 in6_dev_hold(grt->rt6i_idev);
2958 }
2959
2960 if (!(grt->rt6i_flags & RTF_GATEWAY))
2961 err = 0;
2962
2963 ip6_rt_put(grt);
2964
2965out:
2966 return err;
2967}
2968
David Ahern9fbb7042018-03-13 08:29:36 -07002969static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
2970 struct net_device **_dev, struct inet6_dev **idev,
2971 struct netlink_ext_ack *extack)
2972{
2973 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2974 int gwa_type = ipv6_addr_type(gw_addr);
David Ahern232378e2018-03-13 08:29:37 -07002975 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
David Ahern9fbb7042018-03-13 08:29:36 -07002976 const struct net_device *dev = *_dev;
David Ahern232378e2018-03-13 08:29:37 -07002977 bool need_addr_check = !dev;
David Ahern9fbb7042018-03-13 08:29:36 -07002978 int err = -EINVAL;
2979
2980 /* if gw_addr is local we will fail to detect this in case
2981 * address is still TENTATIVE (DAD in progress). rt6_lookup()
2982 * will return already-added prefix route via interface that
2983 * prefix route was assigned to, which might be non-loopback.
2984 */
David Ahern232378e2018-03-13 08:29:37 -07002985 if (dev &&
2986 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2987 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
David Ahern9fbb7042018-03-13 08:29:36 -07002988 goto out;
2989 }
2990
2991 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
2992 /* IPv6 strictly inhibits using not link-local
2993 * addresses as nexthop address.
2994 * Otherwise, router will not able to send redirects.
2995 * It is very good, but in some (rare!) circumstances
2996 * (SIT, PtP, NBMA NOARP links) it is handy to allow
2997 * some exceptions. --ANK
2998 * We allow IPv4-mapped nexthops to support RFC4798-type
2999 * addressing
3000 */
3001 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3002 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3003 goto out;
3004 }
3005
3006 if (cfg->fc_flags & RTNH_F_ONLINK)
3007 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3008 else
3009 err = ip6_route_check_nh(net, cfg, _dev, idev);
3010
3011 if (err)
3012 goto out;
3013 }
3014
3015 /* reload in case device was changed */
3016 dev = *_dev;
3017
3018 err = -EINVAL;
3019 if (!dev) {
3020 NL_SET_ERR_MSG(extack, "Egress device not specified");
3021 goto out;
3022 } else if (dev->flags & IFF_LOOPBACK) {
3023 NL_SET_ERR_MSG(extack,
3024 "Egress device can not be loopback device for this route");
3025 goto out;
3026 }
David Ahern232378e2018-03-13 08:29:37 -07003027
3028 /* if we did not check gw_addr above, do so now that the
3029 * egress device has been resolved.
3030 */
3031 if (need_addr_check &&
3032 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3033 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3034 goto out;
3035 }
3036
David Ahern9fbb7042018-03-13 08:29:36 -07003037 err = 0;
3038out:
3039 return err;
3040}
3041
David Ahern83c442512019-03-27 20:53:50 -07003042static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3043{
3044 if ((flags & RTF_REJECT) ||
3045 (dev && (dev->flags & IFF_LOOPBACK) &&
3046 !(addr_type & IPV6_ADDR_LOOPBACK) &&
3047 !(flags & RTF_LOCAL)))
3048 return true;
3049
3050 return false;
3051}
3052
3053int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3054 struct fib6_config *cfg, gfp_t gfp_flags,
3055 struct netlink_ext_ack *extack)
3056{
3057 struct net_device *dev = NULL;
3058 struct inet6_dev *idev = NULL;
3059 int addr_type;
3060 int err;
3061
David Ahernf1741732019-03-27 20:53:57 -07003062 fib6_nh->fib_nh_family = AF_INET6;
3063
David Ahern83c442512019-03-27 20:53:50 -07003064 err = -ENODEV;
3065 if (cfg->fc_ifindex) {
3066 dev = dev_get_by_index(net, cfg->fc_ifindex);
3067 if (!dev)
3068 goto out;
3069 idev = in6_dev_get(dev);
3070 if (!idev)
3071 goto out;
3072 }
3073
3074 if (cfg->fc_flags & RTNH_F_ONLINK) {
3075 if (!dev) {
3076 NL_SET_ERR_MSG(extack,
3077 "Nexthop device required for onlink");
3078 goto out;
3079 }
3080
3081 if (!(dev->flags & IFF_UP)) {
3082 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3083 err = -ENETDOWN;
3084 goto out;
3085 }
3086
David Ahernad1601a2019-03-27 20:53:56 -07003087 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
David Ahern83c442512019-03-27 20:53:50 -07003088 }
3089
David Ahernad1601a2019-03-27 20:53:56 -07003090 fib6_nh->fib_nh_weight = 1;
David Ahern83c442512019-03-27 20:53:50 -07003091
3092 /* We cannot add true routes via loopback here,
3093 * they would result in kernel looping; promote them to reject routes
3094 */
3095 addr_type = ipv6_addr_type(&cfg->fc_dst);
3096 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3097 /* hold loopback dev/idev if we haven't done so. */
3098 if (dev != net->loopback_dev) {
3099 if (dev) {
3100 dev_put(dev);
3101 in6_dev_put(idev);
3102 }
3103 dev = net->loopback_dev;
3104 dev_hold(dev);
3105 idev = in6_dev_get(dev);
3106 if (!idev) {
3107 err = -ENODEV;
3108 goto out;
3109 }
3110 }
3111 goto set_dev;
3112 }
3113
3114 if (cfg->fc_flags & RTF_GATEWAY) {
3115 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3116 if (err)
3117 goto out;
3118
David Ahernad1601a2019-03-27 20:53:56 -07003119 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
David Ahernbdf00462019-04-05 16:30:26 -07003120 fib6_nh->fib_nh_gw_family = AF_INET6;
David Ahern83c442512019-03-27 20:53:50 -07003121 }
3122
3123 err = -ENODEV;
3124 if (!dev)
3125 goto out;
3126
3127 if (idev->cnf.disable_ipv6) {
3128 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3129 err = -EACCES;
3130 goto out;
3131 }
3132
3133 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3134 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3135 err = -ENETDOWN;
3136 goto out;
3137 }
3138
3139 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3140 !netif_carrier_ok(dev))
David Ahernad1601a2019-03-27 20:53:56 -07003141 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
David Ahern83c442512019-03-27 20:53:50 -07003142
David Ahernf40b6ae2019-05-22 20:27:55 -07003143 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3144 if (!fib6_nh->rt6i_pcpu) {
3145 err = -ENOMEM;
3146 goto out;
3147 }
3148
David Ahern979e2762019-03-27 20:53:58 -07003149 err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap,
3150 cfg->fc_encap_type, cfg, gfp_flags, extack);
3151 if (err)
3152 goto out;
David Ahern83c442512019-03-27 20:53:50 -07003153set_dev:
David Ahernad1601a2019-03-27 20:53:56 -07003154 fib6_nh->fib_nh_dev = dev;
David Ahernf1741732019-03-27 20:53:57 -07003155 fib6_nh->fib_nh_oif = dev->ifindex;
David Ahern83c442512019-03-27 20:53:50 -07003156 err = 0;
3157out:
3158 if (idev)
3159 in6_dev_put(idev);
3160
3161 if (err) {
David Ahernad1601a2019-03-27 20:53:56 -07003162 lwtstate_put(fib6_nh->fib_nh_lws);
3163 fib6_nh->fib_nh_lws = NULL;
David Ahern83c442512019-03-27 20:53:50 -07003164 if (dev)
3165 dev_put(dev);
3166 }
3167
3168 return err;
3169}
3170
David Aherndac7d0f2019-03-27 20:53:51 -07003171void fib6_nh_release(struct fib6_nh *fib6_nh)
3172{
David Aherncc5c0732019-05-22 20:27:58 -07003173 struct rt6_exception_bucket *bucket;
3174
3175 rcu_read_lock();
3176
3177 fib6_nh_flush_exceptions(fib6_nh, NULL);
3178 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3179 if (bucket) {
3180 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3181 kfree(bucket);
3182 }
3183
3184 rcu_read_unlock();
3185
David Ahernf40b6ae2019-05-22 20:27:55 -07003186 if (fib6_nh->rt6i_pcpu) {
3187 int cpu;
3188
3189 for_each_possible_cpu(cpu) {
3190 struct rt6_info **ppcpu_rt;
3191 struct rt6_info *pcpu_rt;
3192
3193 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3194 pcpu_rt = *ppcpu_rt;
3195 if (pcpu_rt) {
3196 dst_dev_put(&pcpu_rt->dst);
3197 dst_release(&pcpu_rt->dst);
3198 *ppcpu_rt = NULL;
3199 }
3200 }
3201
3202 free_percpu(fib6_nh->rt6i_pcpu);
3203 }
3204
David Ahern979e2762019-03-27 20:53:58 -07003205 fib_nh_common_release(&fib6_nh->nh_common);
David Aherndac7d0f2019-03-27 20:53:51 -07003206}
3207
David Ahern8d1c8022018-04-17 17:33:26 -07003208static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
David Ahernacb54e32018-04-17 17:33:22 -07003209 gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003210 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
Daniel Lezcano55786892008-03-04 13:47:47 -08003212 struct net *net = cfg->fc_nlinfo.nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07003213 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003214 struct fib6_table *table;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003215 int err = -EINVAL;
David Ahern83c442512019-03-27 20:53:50 -07003216 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
David Ahern557c44b2017-04-19 14:19:43 -07003218 /* RTF_PCPU is an internal flag; can not be set by userspace */
David Ahernd5d531c2017-05-21 10:12:05 -06003219 if (cfg->fc_flags & RTF_PCPU) {
3220 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
David Ahern557c44b2017-04-19 14:19:43 -07003221 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003222 }
David Ahern557c44b2017-04-19 14:19:43 -07003223
Wei Wang2ea23522017-10-27 17:30:12 -07003224 /* RTF_CACHE is an internal flag; can not be set by userspace */
3225 if (cfg->fc_flags & RTF_CACHE) {
3226 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3227 goto out;
3228 }
3229
David Aherne8478e82018-04-17 17:33:13 -07003230 if (cfg->fc_type > RTN_MAX) {
3231 NL_SET_ERR_MSG(extack, "Invalid route type");
3232 goto out;
3233 }
3234
David Ahernd5d531c2017-05-21 10:12:05 -06003235 if (cfg->fc_dst_len > 128) {
3236 NL_SET_ERR_MSG(extack, "Invalid prefix length");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003237 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003238 }
3239 if (cfg->fc_src_len > 128) {
3240 NL_SET_ERR_MSG(extack, "Invalid source address length");
3241 goto out;
3242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243#ifndef CONFIG_IPV6_SUBTREES
David Ahernd5d531c2017-05-21 10:12:05 -06003244 if (cfg->fc_src_len) {
3245 NL_SET_ERR_MSG(extack,
3246 "Specifying source address requires IPV6_SUBTREES to be enabled");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003247 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249#endif
David Ahernfc1e64e2018-01-25 16:55:09 -08003250
Matti Vaittinend71314b2011-11-14 00:14:49 +00003251 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05003252 if (cfg->fc_nlinfo.nlh &&
3253 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00003254 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05003255 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00003256 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00003257 table = fib6_new_table(net, cfg->fc_table);
3258 }
3259 } else {
3260 table = fib6_new_table(net, cfg->fc_table);
3261 }
David S. Miller38308472011-12-03 18:02:47 -05003262
3263 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003264 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07003265
David Ahern93531c62018-04-17 17:33:25 -07003266 err = -ENOMEM;
David Ahern1cf844c2019-05-22 20:27:59 -07003267 rt = fib6_info_alloc(gfp_flags, true);
David Ahern93531c62018-04-17 17:33:25 -07003268 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 goto out;
David Ahern93531c62018-04-17 17:33:25 -07003270
David Ahernd7e774f2018-11-06 12:51:15 -08003271 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3272 extack);
David Ahern767a2212018-10-04 20:07:51 -07003273 if (IS_ERR(rt->fib6_metrics)) {
3274 err = PTR_ERR(rt->fib6_metrics);
Eric Dumazetfda21d42018-10-05 09:17:50 -07003275 /* Do not leave garbage there. */
3276 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
David Ahern767a2212018-10-04 20:07:51 -07003277 goto out;
3278 }
3279
David Ahern93531c62018-04-17 17:33:25 -07003280 if (cfg->fc_flags & RTF_ADDRCONF)
3281 rt->dst_nocount = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Gao feng1716a962012-04-06 00:13:10 +00003283 if (cfg->fc_flags & RTF_EXPIRES)
David Ahern14895682018-04-17 17:33:17 -07003284 fib6_set_expires(rt, jiffies +
Gao feng1716a962012-04-06 00:13:10 +00003285 clock_t_to_jiffies(cfg->fc_expires));
3286 else
David Ahern14895682018-04-17 17:33:17 -07003287 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
Thomas Graf86872cb2006-08-22 00:01:08 -07003289 if (cfg->fc_protocol == RTPROT_UNSPEC)
3290 cfg->fc_protocol = RTPROT_BOOT;
David Ahern93c2fb22018-04-18 15:38:59 -07003291 rt->fib6_protocol = cfg->fc_protocol;
Thomas Graf86872cb2006-08-22 00:01:08 -07003292
David Ahern83c442512019-03-27 20:53:50 -07003293 rt->fib6_table = table;
3294 rt->fib6_metric = cfg->fc_metric;
3295 rt->fib6_type = cfg->fc_type;
David Ahern2b2450c2019-03-27 20:53:52 -07003296 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003297
David Ahern93c2fb22018-04-18 15:38:59 -07003298 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3299 rt->fib6_dst.plen = cfg->fc_dst_len;
3300 if (rt->fib6_dst.plen == 128)
David Ahern3b6761d2018-04-17 17:33:20 -07003301 rt->dst_host = true;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01003302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07003304 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3305 rt->fib6_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306#endif
David Ahern1cf844c2019-05-22 20:27:59 -07003307 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
David Ahern83c442512019-03-27 20:53:50 -07003308 if (err)
3309 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
3311 /* We cannot add true routes via loopback here,
David Ahern83c442512019-03-27 20:53:50 -07003312 * they would result in kernel looping; promote them to reject routes
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 */
David Ahern83c442512019-03-27 20:53:50 -07003314 addr_type = ipv6_addr_type(&cfg->fc_dst);
David Ahern1cf844c2019-05-22 20:27:59 -07003315 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev, addr_type))
David Ahern83c442512019-03-27 20:53:50 -07003316 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
David Ahern955ec4c2018-01-24 19:45:29 -08003317
Daniel Walterc3968a82011-04-13 21:10:57 +00003318 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
David Ahern83c442512019-03-27 20:53:50 -07003319 struct net_device *dev = fib6_info_nh_dev(rt);
3320
Daniel Walterc3968a82011-04-13 21:10:57 +00003321 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
David Ahernd5d531c2017-05-21 10:12:05 -06003322 NL_SET_ERR_MSG(extack, "Invalid source address");
Daniel Walterc3968a82011-04-13 21:10:57 +00003323 err = -EINVAL;
3324 goto out;
3325 }
David Ahern93c2fb22018-04-18 15:38:59 -07003326 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3327 rt->fib6_prefsrc.plen = 128;
Daniel Walterc3968a82011-04-13 21:10:57 +00003328 } else
David Ahern93c2fb22018-04-18 15:38:59 -07003329 rt->fib6_prefsrc.plen = 0;
Daniel Walterc3968a82011-04-13 21:10:57 +00003330
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003331 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332out:
David Ahern93531c62018-04-17 17:33:25 -07003333 fib6_info_release(rt);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003334 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003335}
3336
David Ahernacb54e32018-04-17 17:33:22 -07003337int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003338 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003339{
David Ahern8d1c8022018-04-17 17:33:26 -07003340 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003341 int err;
3342
David Ahernacb54e32018-04-17 17:33:22 -07003343 rt = ip6_route_info_create(cfg, gfp_flags, extack);
David Ahernd4ead6b2018-04-17 17:33:16 -07003344 if (IS_ERR(rt))
3345 return PTR_ERR(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003346
David Ahernd4ead6b2018-04-17 17:33:16 -07003347 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
David Ahern93531c62018-04-17 17:33:25 -07003348 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 return err;
3351}
3352
David Ahern8d1c8022018-04-17 17:33:26 -07003353static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
David Ahernafb1d4b52018-04-17 17:33:11 -07003355 struct net *net = info->nl_net;
Thomas Grafc71099a2006-08-04 23:20:06 -07003356 struct fib6_table *table;
David Ahernafb1d4b52018-04-17 17:33:11 -07003357 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
David Ahern421842e2018-04-17 17:33:18 -07003359 if (rt == net->ipv6.fib6_null_entry) {
Gao feng6825a262012-09-19 19:25:34 +00003360 err = -ENOENT;
3361 goto out;
3362 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07003363
David Ahern93c2fb22018-04-18 15:38:59 -07003364 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003365 spin_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07003366 err = fib6_del(rt, info);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003367 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Gao feng6825a262012-09-19 19:25:34 +00003369out:
David Ahern93531c62018-04-17 17:33:25 -07003370 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 return err;
3372}
3373
David Ahern8d1c8022018-04-17 17:33:26 -07003374int ip6_del_rt(struct net *net, struct fib6_info *rt)
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003375{
David Ahernafb1d4b52018-04-17 17:33:11 -07003376 struct nl_info info = { .nl_net = net };
3377
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003378 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003379}
3380
David Ahern8d1c8022018-04-17 17:33:26 -07003381static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
David Ahern0ae81332017-02-02 12:37:08 -08003382{
3383 struct nl_info *info = &cfg->fc_nlinfo;
WANG Conge3330032017-02-27 16:07:43 -08003384 struct net *net = info->nl_net;
David Ahern16a16cd2017-02-02 12:37:11 -08003385 struct sk_buff *skb = NULL;
David Ahern0ae81332017-02-02 12:37:08 -08003386 struct fib6_table *table;
WANG Conge3330032017-02-27 16:07:43 -08003387 int err = -ENOENT;
David Ahern0ae81332017-02-02 12:37:08 -08003388
David Ahern421842e2018-04-17 17:33:18 -07003389 if (rt == net->ipv6.fib6_null_entry)
WANG Conge3330032017-02-27 16:07:43 -08003390 goto out_put;
David Ahern93c2fb22018-04-18 15:38:59 -07003391 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003392 spin_lock_bh(&table->tb6_lock);
David Ahern0ae81332017-02-02 12:37:08 -08003393
David Ahern93c2fb22018-04-18 15:38:59 -07003394 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
David Ahern8d1c8022018-04-17 17:33:26 -07003395 struct fib6_info *sibling, *next_sibling;
David Ahern0ae81332017-02-02 12:37:08 -08003396
David Ahern16a16cd2017-02-02 12:37:11 -08003397 /* prefer to send a single notification with all hops */
3398 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3399 if (skb) {
3400 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3401
David Ahernd4ead6b2018-04-17 17:33:16 -07003402 if (rt6_fill_node(net, skb, rt, NULL,
David Ahern16a16cd2017-02-02 12:37:11 -08003403 NULL, NULL, 0, RTM_DELROUTE,
3404 info->portid, seq, 0) < 0) {
3405 kfree_skb(skb);
3406 skb = NULL;
3407 } else
3408 info->skip_notify = 1;
3409 }
3410
David Ahern0ae81332017-02-02 12:37:08 -08003411 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07003412 &rt->fib6_siblings,
3413 fib6_siblings) {
David Ahern0ae81332017-02-02 12:37:08 -08003414 err = fib6_del(sibling, info);
3415 if (err)
WANG Conge3330032017-02-27 16:07:43 -08003416 goto out_unlock;
David Ahern0ae81332017-02-02 12:37:08 -08003417 }
3418 }
3419
3420 err = fib6_del(rt, info);
WANG Conge3330032017-02-27 16:07:43 -08003421out_unlock:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003422 spin_unlock_bh(&table->tb6_lock);
WANG Conge3330032017-02-27 16:07:43 -08003423out_put:
David Ahern93531c62018-04-17 17:33:25 -07003424 fib6_info_release(rt);
David Ahern16a16cd2017-02-02 12:37:11 -08003425
3426 if (skb) {
WANG Conge3330032017-02-27 16:07:43 -08003427 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
David Ahern16a16cd2017-02-02 12:37:11 -08003428 info->nlh, gfp_any());
3429 }
David Ahern0ae81332017-02-02 12:37:08 -08003430 return err;
3431}
3432
David Ahern0fa6efc2019-05-22 20:28:00 -07003433static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
David Ahern23fb93a2018-04-17 17:33:23 -07003434{
3435 int rc = -ESRCH;
3436
3437 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3438 goto out;
3439
3440 if (cfg->fc_flags & RTF_GATEWAY &&
3441 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3442 goto out;
Xin Long761f6022018-11-14 00:48:28 +08003443
3444 rc = rt6_remove_exception_rt(rt);
David Ahern23fb93a2018-04-17 17:33:23 -07003445out:
3446 return rc;
3447}
3448
David Ahern0fa6efc2019-05-22 20:28:00 -07003449static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3450 struct fib6_nh *nh)
3451{
3452 struct fib6_result res = {
3453 .f6i = rt,
3454 .nh = nh,
3455 };
3456 struct rt6_info *rt_cache;
3457
3458 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3459 if (rt_cache)
3460 return __ip6_del_cached_rt(rt_cache, cfg);
3461
3462 return 0;
3463}
3464
David Ahern333c4302017-05-21 10:12:04 -06003465static int ip6_route_del(struct fib6_config *cfg,
3466 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467{
Thomas Grafc71099a2006-08-04 23:20:06 -07003468 struct fib6_table *table;
David Ahern8d1c8022018-04-17 17:33:26 -07003469 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 int err = -ESRCH;
3472
Daniel Lezcano55786892008-03-04 13:47:47 -08003473 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David Ahernd5d531c2017-05-21 10:12:05 -06003474 if (!table) {
3475 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Grafc71099a2006-08-04 23:20:06 -07003476 return err;
David Ahernd5d531c2017-05-21 10:12:05 -06003477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Wei Wang66f5d6c2017-10-06 12:06:10 -07003479 rcu_read_lock();
Thomas Grafc71099a2006-08-04 23:20:06 -07003480
3481 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07003482 &cfg->fc_dst, cfg->fc_dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07003483 &cfg->fc_src, cfg->fc_src_len,
Wei Wang2b760fc2017-10-06 12:06:03 -07003484 !(cfg->fc_flags & RTF_CACHE));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003485
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 if (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003487 for_each_fib6_node_rt_rcu(fn) {
David Ahernad1601a2019-03-27 20:53:56 -07003488 struct fib6_nh *nh;
3489
David Ahern1cf844c2019-05-22 20:27:59 -07003490 nh = rt->fib6_nh;
Wei Wang2b760fc2017-10-06 12:06:03 -07003491 if (cfg->fc_flags & RTF_CACHE) {
David Ahern23fb93a2018-04-17 17:33:23 -07003492 int rc;
3493
David Ahern0fa6efc2019-05-22 20:28:00 -07003494 rc = ip6_del_cached_rt(cfg, rt, nh);
3495 if (rc != -ESRCH) {
3496 rcu_read_unlock();
3497 return rc;
David Ahern23fb93a2018-04-17 17:33:23 -07003498 }
3499 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07003500 }
David Ahernad1601a2019-03-27 20:53:56 -07003501
Thomas Graf86872cb2006-08-22 00:01:08 -07003502 if (cfg->fc_ifindex &&
David Ahernad1601a2019-03-27 20:53:56 -07003503 (!nh->fib_nh_dev ||
3504 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07003506 if (cfg->fc_flags & RTF_GATEWAY &&
David Ahernad1601a2019-03-27 20:53:56 -07003507 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003509 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003511 if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol)
Mantas Mc2ed1882016-12-16 10:30:59 +02003512 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07003513 if (!fib6_info_hold_safe(rt))
3514 continue;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003515 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
David Ahern0ae81332017-02-02 12:37:08 -08003517 /* if gateway was specified only delete the one hop */
3518 if (cfg->fc_flags & RTF_GATEWAY)
3519 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3520
3521 return __ip6_del_rt_siblings(rt, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 }
3523 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003524 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
3526 return err;
3527}
3528
David S. Miller6700c272012-07-17 03:29:28 -07003529static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003530{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003531 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07003532 struct rt6_info *rt, *nrt = NULL;
David Ahern85bd05d2019-04-16 14:36:01 -07003533 struct fib6_result res = {};
David S. Millere8599ff2012-07-11 23:43:53 -07003534 struct ndisc_options ndopts;
3535 struct inet6_dev *in6_dev;
3536 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003537 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07003538 int optlen, on_link;
3539 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07003540
Simon Horman29a3cad2013-05-28 20:34:26 +00003541 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003542 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07003543
3544 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07003545 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003546 return;
3547 }
3548
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003549 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07003550
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003551 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003552 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003553 return;
3554 }
3555
David S. Miller6e157b62012-07-12 00:05:02 -07003556 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003557 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003558 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003559 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07003560 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003561 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003562 return;
3563 }
3564
3565 in6_dev = __in6_dev_get(skb->dev);
3566 if (!in6_dev)
3567 return;
3568 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3569 return;
3570
3571 /* RFC2461 8.1:
3572 * The IP source address of the Redirect MUST be the same as the current
3573 * first-hop router for the specified ICMP Destination Address.
3574 */
3575
Alexander Aringf997c552016-06-15 21:20:23 +02003576 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003577 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3578 return;
3579 }
David S. Miller6e157b62012-07-12 00:05:02 -07003580
3581 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07003582 if (ndopts.nd_opts_tgt_lladdr) {
3583 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3584 skb->dev);
3585 if (!lladdr) {
3586 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3587 return;
3588 }
3589 }
3590
David S. Miller6e157b62012-07-12 00:05:02 -07003591 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01003592 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07003593 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
3594 return;
3595 }
3596
3597 /* Redirect received -> path was valid.
3598 * Look, redirects are sent only in response to data packets,
3599 * so that this nexthop apparently is reachable. --ANK
3600 */
Julian Anastasov0dec8792017-02-06 23:14:16 +02003601 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
David S. Miller6e157b62012-07-12 00:05:02 -07003602
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003603 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07003604 if (!neigh)
3605 return;
3606
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 /*
3608 * We have finally decided to accept it.
3609 */
3610
Alexander Aringf997c552016-06-15 21:20:23 +02003611 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 NEIGH_UPDATE_F_WEAK_OVERRIDE|
3613 NEIGH_UPDATE_F_OVERRIDE|
3614 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02003615 NEIGH_UPDATE_F_ISROUTER)),
3616 NDISC_REDIRECT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
David Ahern4d85cd02018-04-20 15:37:59 -07003618 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07003619 res.f6i = rcu_dereference(rt->from);
David S. Millerff24e492019-05-02 22:14:21 -04003620 if (!res.f6i)
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07003621 goto out;
David Ahern8a14e462018-04-23 11:32:07 -07003622
David Ahern1cf844c2019-05-22 20:27:59 -07003623 res.nh = res.f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -07003624 res.fib6_flags = res.f6i->fib6_flags;
3625 res.fib6_type = res.f6i->fib6_type;
David Ahern85bd05d2019-04-16 14:36:01 -07003626 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05003627 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 goto out;
3629
3630 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
3631 if (on_link)
3632 nrt->rt6i_flags &= ~RTF_GATEWAY;
3633
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003634 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07003636 /* rt6_insert_exception() will take care of duplicated exceptions */
David Ahern5012f0a2019-04-16 14:36:05 -07003637 if (rt6_insert_exception(nrt, &res)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07003638 dst_release_immediate(&nrt->dst);
3639 goto out;
3640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
Changli Gaod8d1f302010-06-10 23:31:35 -07003642 netevent.old = &rt->dst;
3643 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003644 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00003645 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07003646 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
3647
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648out:
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07003649 rcu_read_unlock();
David S. Millere8599ff2012-07-11 23:43:53 -07003650 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07003651}
3652
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003653#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -07003654static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003655 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07003656 const struct in6_addr *gwaddr,
3657 struct net_device *dev)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003658{
David Ahern830218c2016-10-24 10:52:35 -07003659 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3660 int ifindex = dev->ifindex;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003661 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07003662 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003663 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003664
David Ahern830218c2016-10-24 10:52:35 -07003665 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05003666 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003667 return NULL;
3668
Wei Wang66f5d6c2017-10-06 12:06:10 -07003669 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07003670 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003671 if (!fn)
3672 goto out;
3673
Wei Wang66f5d6c2017-10-06 12:06:10 -07003674 for_each_fib6_node_rt_rcu(fn) {
David Ahern1cf844c2019-05-22 20:27:59 -07003675 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003676 continue;
David Ahern2b2450c2019-03-27 20:53:52 -07003677 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
David Ahern1cf844c2019-05-22 20:27:59 -07003678 !rt->fib6_nh->fib_nh_gw_family)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003679 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07003680 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003681 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07003682 if (!fib6_info_hold_safe(rt))
3683 continue;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003684 break;
3685 }
3686out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003687 rcu_read_unlock();
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003688 return rt;
3689}
3690
David Ahern8d1c8022018-04-17 17:33:26 -07003691static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003692 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07003693 const struct in6_addr *gwaddr,
3694 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +00003695 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003696{
Thomas Graf86872cb2006-08-22 00:01:08 -07003697 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08003698 .fc_metric = IP6_RT_PRIO_USER,
David Ahern830218c2016-10-24 10:52:35 -07003699 .fc_ifindex = dev->ifindex,
Thomas Graf86872cb2006-08-22 00:01:08 -07003700 .fc_dst_len = prefixlen,
3701 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
3702 RTF_UP | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08003703 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07003704 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003705 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08003706 .fc_nlinfo.nlh = NULL,
3707 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07003708 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003709
David Ahern830218c2016-10-24 10:52:35 -07003710 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003711 cfg.fc_dst = *prefix;
3712 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07003713
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08003714 /* We should treat it as a default route if prefix length is 0. */
3715 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07003716 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003717
David Ahernacb54e32018-04-17 17:33:22 -07003718 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003719
David Ahern830218c2016-10-24 10:52:35 -07003720 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003721}
3722#endif
3723
David Ahern8d1c8022018-04-17 17:33:26 -07003724struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07003725 const struct in6_addr *addr,
3726 struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003727{
David Ahern830218c2016-10-24 10:52:35 -07003728 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
David Ahern8d1c8022018-04-17 17:33:26 -07003729 struct fib6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07003730 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731
David Ahernafb1d4b52018-04-17 17:33:11 -07003732 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05003733 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003734 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Wei Wang66f5d6c2017-10-06 12:06:10 -07003736 rcu_read_lock();
3737 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Ahern1cf844c2019-05-22 20:27:59 -07003738 struct fib6_nh *nh = rt->fib6_nh;
David Ahernad1601a2019-03-27 20:53:56 -07003739
3740 if (dev == nh->fib_nh_dev &&
David Ahern93c2fb22018-04-18 15:38:59 -07003741 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Ahernad1601a2019-03-27 20:53:56 -07003742 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 break;
3744 }
Wei Wange873e4b2018-07-21 20:56:32 -07003745 if (rt && !fib6_info_hold_safe(rt))
3746 rt = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003747 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 return rt;
3749}
3750
David Ahern8d1c8022018-04-17 17:33:26 -07003751struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07003752 const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08003753 struct net_device *dev,
3754 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
Thomas Graf86872cb2006-08-22 00:01:08 -07003756 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07003757 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08003758 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07003759 .fc_ifindex = dev->ifindex,
3760 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
3761 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08003762 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07003763 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003764 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08003765 .fc_nlinfo.nlh = NULL,
David Ahernafb1d4b52018-04-17 17:33:11 -07003766 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07003767 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003769 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
David Ahernacb54e32018-04-17 17:33:22 -07003771 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
David Ahern830218c2016-10-24 10:52:35 -07003772 struct fib6_table *table;
3773
3774 table = fib6_get_table(dev_net(dev), cfg.fc_table);
3775 if (table)
3776 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
David Ahernafb1d4b52018-04-17 17:33:11 -07003779 return rt6_get_dflt_router(net, gwaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780}
3781
David Ahernafb1d4b52018-04-17 17:33:11 -07003782static void __rt6_purge_dflt_routers(struct net *net,
3783 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784{
David Ahern8d1c8022018-04-17 17:33:26 -07003785 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
3787restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003788 rcu_read_lock();
3789 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Aherndcd1f572018-04-18 15:39:05 -07003790 struct net_device *dev = fib6_info_nh_dev(rt);
3791 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
3792
David Ahern93c2fb22018-04-18 15:38:59 -07003793 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
Wei Wange873e4b2018-07-21 20:56:32 -07003794 (!idev || idev->cnf.accept_ra != 2) &&
3795 fib6_info_hold_safe(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07003796 rcu_read_unlock();
3797 ip6_del_rt(net, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 goto restart;
3799 }
3800 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003801 rcu_read_unlock();
David Ahern830218c2016-10-24 10:52:35 -07003802
3803 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3804}
3805
3806void rt6_purge_dflt_routers(struct net *net)
3807{
3808 struct fib6_table *table;
3809 struct hlist_head *head;
3810 unsigned int h;
3811
3812 rcu_read_lock();
3813
3814 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3815 head = &net->ipv6.fib_table_hash[h];
3816 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3817 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
David Ahernafb1d4b52018-04-17 17:33:11 -07003818 __rt6_purge_dflt_routers(net, table);
David Ahern830218c2016-10-24 10:52:35 -07003819 }
3820 }
3821
3822 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823}
3824
Daniel Lezcano55786892008-03-04 13:47:47 -08003825static void rtmsg_to_fib6_config(struct net *net,
3826 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07003827 struct fib6_config *cfg)
3828{
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07003829 *cfg = (struct fib6_config){
3830 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
3831 : RT6_TABLE_MAIN,
3832 .fc_ifindex = rtmsg->rtmsg_ifindex,
David Ahern67f69512019-03-21 05:21:34 -07003833 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07003834 .fc_expires = rtmsg->rtmsg_info,
3835 .fc_dst_len = rtmsg->rtmsg_dst_len,
3836 .fc_src_len = rtmsg->rtmsg_src_len,
3837 .fc_flags = rtmsg->rtmsg_flags,
3838 .fc_type = rtmsg->rtmsg_type,
Thomas Graf86872cb2006-08-22 00:01:08 -07003839
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07003840 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07003841
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07003842 .fc_dst = rtmsg->rtmsg_dst,
3843 .fc_src = rtmsg->rtmsg_src,
3844 .fc_gateway = rtmsg->rtmsg_gateway,
3845 };
Thomas Graf86872cb2006-08-22 00:01:08 -07003846}
3847
Daniel Lezcano55786892008-03-04 13:47:47 -08003848int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849{
Thomas Graf86872cb2006-08-22 00:01:08 -07003850 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 struct in6_rtmsg rtmsg;
3852 int err;
3853
Ian Morris67ba4152014-08-24 21:53:10 +01003854 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 case SIOCADDRT: /* Add a route */
3856 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00003857 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 return -EPERM;
3859 err = copy_from_user(&rtmsg, arg,
3860 sizeof(struct in6_rtmsg));
3861 if (err)
3862 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07003863
Daniel Lezcano55786892008-03-04 13:47:47 -08003864 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07003865
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 rtnl_lock();
3867 switch (cmd) {
3868 case SIOCADDRT:
David Ahernacb54e32018-04-17 17:33:22 -07003869 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 break;
3871 case SIOCDELRT:
David Ahern333c4302017-05-21 10:12:04 -06003872 err = ip6_route_del(&cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 break;
3874 default:
3875 err = -EINVAL;
3876 }
3877 rtnl_unlock();
3878
3879 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
3882 return -EINVAL;
3883}
3884
3885/*
3886 * Drop the packet on the floor
3887 */
3888
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07003889static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890{
Eric Dumazetadf30902009-06-02 05:19:30 +00003891 struct dst_entry *dst = skb_dst(skb);
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04003892 struct net *net = dev_net(dst->dev);
3893 struct inet6_dev *idev;
3894 int type;
3895
3896 if (netif_is_l3_master(skb->dev) &&
3897 dst->dev == net->loopback_dev)
3898 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
3899 else
3900 idev = ip6_dst_idev(dst);
3901
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003902 switch (ipstats_mib_noroutes) {
3903 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07003904 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00003905 if (type == IPV6_ADDR_ANY) {
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04003906 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003907 break;
3908 }
3909 /* FALLTHROUGH */
3910 case IPSTATS_MIB_OUTNOROUTES:
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04003911 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003912 break;
3913 }
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04003914
3915 /* Start over by dropping the dst for l3mdev case */
3916 if (netif_is_l3_master(skb->dev))
3917 skb_dst_drop(skb);
3918
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00003919 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 kfree_skb(skb);
3921 return 0;
3922}
3923
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003924static int ip6_pkt_discard(struct sk_buff *skb)
3925{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003926 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003927}
3928
Eric W. Biedermanede20592015-10-07 16:48:47 -05003929static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930{
Eric Dumazetadf30902009-06-02 05:19:30 +00003931 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003932 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933}
3934
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003935static int ip6_pkt_prohibit(struct sk_buff *skb)
3936{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003937 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003938}
3939
Eric W. Biedermanede20592015-10-07 16:48:47 -05003940static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003941{
Eric Dumazetadf30902009-06-02 05:19:30 +00003942 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003943 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003944}
3945
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946/*
3947 * Allocate a dst for local (unicast / anycast) address.
3948 */
3949
David Ahern360a9882018-04-18 15:39:00 -07003950struct fib6_info *addrconf_f6i_alloc(struct net *net,
3951 struct inet6_dev *idev,
3952 const struct in6_addr *addr,
3953 bool anycast, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954{
David Ahernc7a1ce32019-03-21 05:21:35 -07003955 struct fib6_config cfg = {
3956 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
3957 .fc_ifindex = idev->dev->ifindex,
3958 .fc_flags = RTF_UP | RTF_ADDRCONF | RTF_NONEXTHOP,
3959 .fc_dst = *addr,
3960 .fc_dst_len = 128,
3961 .fc_protocol = RTPROT_KERNEL,
3962 .fc_nlinfo.nl_net = net,
3963 .fc_ignore_dev_down = true,
3964 };
David Ahern5f02ce242016-09-10 12:09:54 -07003965
David Aherne8478e82018-04-17 17:33:13 -07003966 if (anycast) {
David Ahernc7a1ce32019-03-21 05:21:35 -07003967 cfg.fc_type = RTN_ANYCAST;
3968 cfg.fc_flags |= RTF_ANYCAST;
David Aherne8478e82018-04-17 17:33:13 -07003969 } else {
David Ahernc7a1ce32019-03-21 05:21:35 -07003970 cfg.fc_type = RTN_LOCAL;
3971 cfg.fc_flags |= RTF_LOCAL;
David Aherne8478e82018-04-17 17:33:13 -07003972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
David Ahernc7a1ce32019-03-21 05:21:35 -07003974 return ip6_route_info_create(&cfg, gfp_flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975}
3976
Daniel Walterc3968a82011-04-13 21:10:57 +00003977/* remove deleted ip from prefsrc entries */
3978struct arg_dev_net_ip {
3979 struct net_device *dev;
3980 struct net *net;
3981 struct in6_addr *addr;
3982};
3983
David Ahern8d1c8022018-04-17 17:33:26 -07003984static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
Daniel Walterc3968a82011-04-13 21:10:57 +00003985{
3986 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3987 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3988 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3989
David Ahern1cf844c2019-05-22 20:27:59 -07003990 if (((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
David Ahern421842e2018-04-17 17:33:18 -07003991 rt != net->ipv6.fib6_null_entry &&
David Ahern93c2fb22018-04-18 15:38:59 -07003992 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
Wei Wang60006a42017-10-06 12:05:58 -07003993 spin_lock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00003994 /* remove prefsrc entry */
David Ahern93c2fb22018-04-18 15:38:59 -07003995 rt->fib6_prefsrc.plen = 0;
Wei Wang60006a42017-10-06 12:05:58 -07003996 spin_unlock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00003997 }
3998 return 0;
3999}
4000
4001void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4002{
4003 struct net *net = dev_net(ifp->idev->dev);
4004 struct arg_dev_net_ip adni = {
4005 .dev = ifp->idev->dev,
4006 .net = net,
4007 .addr = &ifp->addr,
4008 };
Li RongQing0c3584d2013-12-27 16:32:38 +08004009 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00004010}
4011
David Ahern2b2450c2019-03-27 20:53:52 -07004012#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004013
4014/* Remove routers and update dst entries when gateway turn into host. */
David Ahern8d1c8022018-04-17 17:33:26 -07004015static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004016{
4017 struct in6_addr *gateway = (struct in6_addr *)arg;
David Ahern1cf844c2019-05-22 20:27:59 -07004018 struct fib6_nh *nh = rt->fib6_nh;
Duan Jiongbe7a0102014-05-15 15:56:14 +08004019
David Ahern93c2fb22018-04-18 15:38:59 -07004020 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Aherncc5c0732019-05-22 20:27:58 -07004021 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
Duan Jiongbe7a0102014-05-15 15:56:14 +08004022 return -1;
Wei Wangb16cb452017-10-06 12:06:00 -07004023
4024 /* Further clean up cached routes in exception table.
4025 * This is needed because cached route may have a different
4026 * gateway than its 'parent' in the case of an ip redirect.
4027 */
David Aherncc5c0732019-05-22 20:27:58 -07004028 fib6_nh_exceptions_clean_tohost(nh, gateway);
Wei Wangb16cb452017-10-06 12:06:00 -07004029
Duan Jiongbe7a0102014-05-15 15:56:14 +08004030 return 0;
4031}
4032
4033void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4034{
4035 fib6_clean_all(net, fib6_clean_tohost, gateway);
4036}
4037
Ido Schimmel2127d952018-01-07 12:45:03 +02004038struct arg_netdev_event {
4039 const struct net_device *dev;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004040 union {
David Ahernecc56632019-04-23 08:48:09 -07004041 unsigned char nh_flags;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004042 unsigned long event;
4043 };
Ido Schimmel2127d952018-01-07 12:45:03 +02004044};
4045
David Ahern8d1c8022018-04-17 17:33:26 -07004046static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004047{
David Ahern8d1c8022018-04-17 17:33:26 -07004048 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004049 struct fib6_node *fn;
4050
David Ahern93c2fb22018-04-18 15:38:59 -07004051 fn = rcu_dereference_protected(rt->fib6_node,
4052 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004053 iter = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -07004054 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004055 while (iter) {
David Ahern93c2fb22018-04-18 15:38:59 -07004056 if (iter->fib6_metric == rt->fib6_metric &&
David Ahern33bd5ac2018-07-03 14:36:21 -07004057 rt6_qualify_for_ecmp(iter))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004058 return iter;
David Ahern8fb11a92018-05-04 13:54:24 -07004059 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -07004060 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004061 }
4062
4063 return NULL;
4064}
4065
David Ahern8d1c8022018-04-17 17:33:26 -07004066static bool rt6_is_dead(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004067{
David Ahern1cf844c2019-05-22 20:27:59 -07004068 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4069 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4070 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004071 return true;
4072
4073 return false;
4074}
4075
David Ahern8d1c8022018-04-17 17:33:26 -07004076static int rt6_multipath_total_weight(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004077{
David Ahern8d1c8022018-04-17 17:33:26 -07004078 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004079 int total = 0;
4080
4081 if (!rt6_is_dead(rt))
David Ahern1cf844c2019-05-22 20:27:59 -07004082 total += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004083
David Ahern93c2fb22018-04-18 15:38:59 -07004084 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004085 if (!rt6_is_dead(iter))
David Ahern1cf844c2019-05-22 20:27:59 -07004086 total += iter->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004087 }
4088
4089 return total;
4090}
4091
David Ahern8d1c8022018-04-17 17:33:26 -07004092static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004093{
4094 int upper_bound = -1;
4095
4096 if (!rt6_is_dead(rt)) {
David Ahern1cf844c2019-05-22 20:27:59 -07004097 *weight += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004098 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4099 total) - 1;
4100 }
David Ahern1cf844c2019-05-22 20:27:59 -07004101 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004102}
4103
David Ahern8d1c8022018-04-17 17:33:26 -07004104static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004105{
David Ahern8d1c8022018-04-17 17:33:26 -07004106 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004107 int weight = 0;
4108
4109 rt6_upper_bound_set(rt, &weight, total);
4110
David Ahern93c2fb22018-04-18 15:38:59 -07004111 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004112 rt6_upper_bound_set(iter, &weight, total);
4113}
4114
David Ahern8d1c8022018-04-17 17:33:26 -07004115void rt6_multipath_rebalance(struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004116{
David Ahern8d1c8022018-04-17 17:33:26 -07004117 struct fib6_info *first;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004118 int total;
4119
4120 /* In case the entire multipath route was marked for flushing,
4121 * then there is no need to rebalance upon the removal of every
4122 * sibling route.
4123 */
David Ahern93c2fb22018-04-18 15:38:59 -07004124 if (!rt->fib6_nsiblings || rt->should_flush)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004125 return;
4126
4127 /* During lookup routes are evaluated in order, so we need to
4128 * make sure upper bounds are assigned from the first sibling
4129 * onwards.
4130 */
4131 first = rt6_multipath_first_sibling(rt);
4132 if (WARN_ON_ONCE(!first))
4133 return;
4134
4135 total = rt6_multipath_total_weight(first);
4136 rt6_multipath_upper_bound_set(first, total);
4137}
4138
David Ahern8d1c8022018-04-17 17:33:26 -07004139static int fib6_ifup(struct fib6_info *rt, void *p_arg)
Ido Schimmel2127d952018-01-07 12:45:03 +02004140{
4141 const struct arg_netdev_event *arg = p_arg;
David Ahern7aef6852018-04-17 17:33:10 -07004142 struct net *net = dev_net(arg->dev);
Ido Schimmel2127d952018-01-07 12:45:03 +02004143
David Ahernad1601a2019-03-27 20:53:56 -07004144 if (rt != net->ipv6.fib6_null_entry &&
David Ahern1cf844c2019-05-22 20:27:59 -07004145 rt->fib6_nh->fib_nh_dev == arg->dev) {
4146 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
David Ahern7aef6852018-04-17 17:33:10 -07004147 fib6_update_sernum_upto_root(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004148 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004149 }
Ido Schimmel2127d952018-01-07 12:45:03 +02004150
4151 return 0;
4152}
4153
David Ahernecc56632019-04-23 08:48:09 -07004154void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
Ido Schimmel2127d952018-01-07 12:45:03 +02004155{
4156 struct arg_netdev_event arg = {
4157 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004158 {
4159 .nh_flags = nh_flags,
4160 },
Ido Schimmel2127d952018-01-07 12:45:03 +02004161 };
4162
4163 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4164 arg.nh_flags |= RTNH_F_LINKDOWN;
4165
4166 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4167}
4168
David Ahern8d1c8022018-04-17 17:33:26 -07004169static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004170 const struct net_device *dev)
4171{
David Ahern8d1c8022018-04-17 17:33:26 -07004172 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004173
David Ahern1cf844c2019-05-22 20:27:59 -07004174 if (rt->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004175 return true;
David Ahern93c2fb22018-04-18 15:38:59 -07004176 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004177 if (iter->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004178 return true;
4179
4180 return false;
4181}
4182
David Ahern8d1c8022018-04-17 17:33:26 -07004183static void rt6_multipath_flush(struct fib6_info *rt)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004184{
David Ahern8d1c8022018-04-17 17:33:26 -07004185 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004186
4187 rt->should_flush = 1;
David Ahern93c2fb22018-04-18 15:38:59 -07004188 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004189 iter->should_flush = 1;
4190}
4191
David Ahern8d1c8022018-04-17 17:33:26 -07004192static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004193 const struct net_device *down_dev)
4194{
David Ahern8d1c8022018-04-17 17:33:26 -07004195 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004196 unsigned int dead = 0;
4197
David Ahern1cf844c2019-05-22 20:27:59 -07004198 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4199 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004200 dead++;
David Ahern93c2fb22018-04-18 15:38:59 -07004201 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004202 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4203 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004204 dead++;
4205
4206 return dead;
4207}
4208
David Ahern8d1c8022018-04-17 17:33:26 -07004209static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004210 const struct net_device *dev,
David Ahernecc56632019-04-23 08:48:09 -07004211 unsigned char nh_flags)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004212{
David Ahern8d1c8022018-04-17 17:33:26 -07004213 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004214
David Ahern1cf844c2019-05-22 20:27:59 -07004215 if (rt->fib6_nh->fib_nh_dev == dev)
4216 rt->fib6_nh->fib_nh_flags |= nh_flags;
David Ahern93c2fb22018-04-18 15:38:59 -07004217 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004218 if (iter->fib6_nh->fib_nh_dev == dev)
4219 iter->fib6_nh->fib_nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004220}
4221
David Aherna1a22c12017-01-18 07:40:36 -08004222/* called with write lock held for table with rt */
David Ahern8d1c8022018-04-17 17:33:26 -07004223static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004225 const struct arg_netdev_event *arg = p_arg;
4226 const struct net_device *dev = arg->dev;
David Ahern7aef6852018-04-17 17:33:10 -07004227 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004228
David Ahern421842e2018-04-17 17:33:18 -07004229 if (rt == net->ipv6.fib6_null_entry)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004230 return 0;
4231
4232 switch (arg->event) {
4233 case NETDEV_UNREGISTER:
David Ahern1cf844c2019-05-22 20:27:59 -07004234 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004235 case NETDEV_DOWN:
Ido Schimmel1de178e2018-01-07 12:45:15 +02004236 if (rt->should_flush)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004237 return -1;
David Ahern93c2fb22018-04-18 15:38:59 -07004238 if (!rt->fib6_nsiblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004239 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004240 if (rt6_multipath_uses_dev(rt, dev)) {
4241 unsigned int count;
4242
4243 count = rt6_multipath_dead_count(rt, dev);
David Ahern93c2fb22018-04-18 15:38:59 -07004244 if (rt->fib6_nsiblings + 1 == count) {
Ido Schimmel1de178e2018-01-07 12:45:15 +02004245 rt6_multipath_flush(rt);
4246 return -1;
4247 }
4248 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4249 RTNH_F_LINKDOWN);
David Ahern7aef6852018-04-17 17:33:10 -07004250 fib6_update_sernum(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004251 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004252 }
4253 return -2;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004254 case NETDEV_CHANGE:
David Ahern1cf844c2019-05-22 20:27:59 -07004255 if (rt->fib6_nh->fib_nh_dev != dev ||
David Ahern93c2fb22018-04-18 15:38:59 -07004256 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004257 break;
David Ahern1cf844c2019-05-22 20:27:59 -07004258 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004259 rt6_multipath_rebalance(rt);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004260 break;
Ido Schimmel2b241362018-01-07 12:45:02 +02004261 }
David S. Millerc159d302011-12-26 15:24:36 -05004262
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 return 0;
4264}
4265
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004266void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004268 struct arg_netdev_event arg = {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004269 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004270 {
4271 .event = event,
4272 },
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004273 };
David Ahern7c6bb7d2018-10-11 20:17:21 -07004274 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004275
David Ahern7c6bb7d2018-10-11 20:17:21 -07004276 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4277 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4278 else
4279 fib6_clean_all(net, fib6_ifdown, &arg);
Ido Schimmel4c981e22018-01-07 12:45:04 +02004280}
4281
4282void rt6_disable_ip(struct net_device *dev, unsigned long event)
4283{
4284 rt6_sync_down_dev(dev, event);
4285 rt6_uncached_list_flush_dev(dev_net(dev), dev);
4286 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287}
4288
Eric Dumazet95c96172012-04-15 05:58:06 +00004289struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00004291 unsigned int mtu;
David Ahernc0b220c2019-05-22 20:27:57 -07004292 struct fib6_info *f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293};
4294
David Aherncc5c0732019-05-22 20:27:58 -07004295static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
David Ahernc0b220c2019-05-22 20:27:57 -07004296{
4297 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
David Aherncc5c0732019-05-22 20:27:58 -07004298 struct fib6_info *f6i = arg->f6i;
David Ahernc0b220c2019-05-22 20:27:57 -07004299
4300 /* For administrative MTU increase, there is no way to discover
4301 * IPv6 PMTU increase, so PMTU increase should be updated here.
4302 * Since RFC 1981 doesn't include administrative MTU increase
4303 * update PMTU increase is a MUST. (i.e. jumbo frame)
4304 */
4305 if (nh->fib_nh_dev == arg->dev) {
4306 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4307 u32 mtu = f6i->fib6_pmtu;
4308
4309 if (mtu >= arg->mtu ||
4310 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4311 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4312
4313 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07004314 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
David Ahernc0b220c2019-05-22 20:27:57 -07004315 spin_unlock_bh(&rt6_exception_lock);
4316 }
4317
4318 return 0;
4319}
4320
4321static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322{
4323 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4324 struct inet6_dev *idev;
4325
4326 /* In IPv6 pmtu discovery is not optional,
4327 so that RTAX_MTU lock cannot disable it.
4328 We still use this lock to block changes
4329 caused by addrconf/ndisc.
4330 */
4331
4332 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05004333 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 return 0;
4335
David Ahernc0b220c2019-05-22 20:27:57 -07004336 if (fib6_metric_locked(f6i, RTAX_MTU))
4337 return 0;
David Ahernd4ead6b2018-04-17 17:33:16 -07004338
David Ahernc0b220c2019-05-22 20:27:57 -07004339 arg->f6i = f6i;
David Ahern1cf844c2019-05-22 20:27:59 -07004340 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341}
4342
Eric Dumazet95c96172012-04-15 05:58:06 +00004343void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344{
Thomas Grafc71099a2006-08-04 23:20:06 -07004345 struct rt6_mtu_change_arg arg = {
4346 .dev = dev,
4347 .mtu = mtu,
4348 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
Li RongQing0c3584d2013-12-27 16:32:38 +08004350 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351}
4352
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004353static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
David Ahern75425652019-05-22 12:07:43 -07004354 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
Thomas Graf5176f912006-08-26 20:13:18 -07004355 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004356 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07004357 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07004358 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004359 [RTA_PRIORITY] = { .type = NLA_U32 },
4360 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004361 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004362 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004363 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4364 [RTA_ENCAP] = { .type = NLA_NESTED },
Xin Long32bc2012015-12-16 17:50:11 +08004365 [RTA_EXPIRES] = { .type = NLA_U32 },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004366 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +08004367 [RTA_MARK] = { .type = NLA_U32 },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004368 [RTA_TABLE] = { .type = NLA_U32 },
Roopa Prabhueacb9382018-05-22 14:03:28 -07004369 [RTA_IP_PROTO] = { .type = NLA_U8 },
4370 [RTA_SPORT] = { .type = NLA_U16 },
4371 [RTA_DPORT] = { .type = NLA_U16 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004372};
4373
4374static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
David Ahern333c4302017-05-21 10:12:04 -06004375 struct fib6_config *cfg,
4376 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377{
Thomas Graf86872cb2006-08-22 00:01:08 -07004378 struct rtmsg *rtm;
4379 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004380 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07004381 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382
Johannes Berg8cb08172019-04-26 14:07:28 +02004383 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
4384 rtm_ipv6_policy, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004385 if (err < 0)
4386 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
Thomas Graf86872cb2006-08-22 00:01:08 -07004388 err = -EINVAL;
4389 rtm = nlmsg_data(nlh);
Thomas Graf86872cb2006-08-22 00:01:08 -07004390
Maciej Żenczykowski84db8402018-09-29 23:44:53 -07004391 *cfg = (struct fib6_config){
4392 .fc_table = rtm->rtm_table,
4393 .fc_dst_len = rtm->rtm_dst_len,
4394 .fc_src_len = rtm->rtm_src_len,
4395 .fc_flags = RTF_UP,
4396 .fc_protocol = rtm->rtm_protocol,
4397 .fc_type = rtm->rtm_type,
4398
4399 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
4400 .fc_nlinfo.nlh = nlh,
4401 .fc_nlinfo.nl_net = sock_net(skb->sk),
4402 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004403
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004404 if (rtm->rtm_type == RTN_UNREACHABLE ||
4405 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00004406 rtm->rtm_type == RTN_PROHIBIT ||
4407 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07004408 cfg->fc_flags |= RTF_REJECT;
4409
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00004410 if (rtm->rtm_type == RTN_LOCAL)
4411 cfg->fc_flags |= RTF_LOCAL;
4412
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07004413 if (rtm->rtm_flags & RTM_F_CLONED)
4414 cfg->fc_flags |= RTF_CACHE;
4415
David Ahernfc1e64e2018-01-25 16:55:09 -08004416 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4417
Thomas Graf86872cb2006-08-22 00:01:08 -07004418 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004419 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07004420 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 }
David Aherne3818542019-02-26 09:00:03 -08004422 if (tb[RTA_VIA]) {
4423 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4424 goto errout;
4425 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004426
4427 if (tb[RTA_DST]) {
4428 int plen = (rtm->rtm_dst_len + 7) >> 3;
4429
4430 if (nla_len(tb[RTA_DST]) < plen)
4431 goto errout;
4432
4433 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004435
4436 if (tb[RTA_SRC]) {
4437 int plen = (rtm->rtm_src_len + 7) >> 3;
4438
4439 if (nla_len(tb[RTA_SRC]) < plen)
4440 goto errout;
4441
4442 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004444
Daniel Walterc3968a82011-04-13 21:10:57 +00004445 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02004446 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00004447
Thomas Graf86872cb2006-08-22 00:01:08 -07004448 if (tb[RTA_OIF])
4449 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4450
4451 if (tb[RTA_PRIORITY])
4452 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4453
4454 if (tb[RTA_METRICS]) {
4455 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4456 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004458
4459 if (tb[RTA_TABLE])
4460 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4461
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004462 if (tb[RTA_MULTIPATH]) {
4463 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4464 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
David Ahern9ed59592017-01-17 14:57:36 -08004465
4466 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
David Ahernc255bd62017-05-27 16:19:27 -06004467 cfg->fc_mp_len, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004468 if (err < 0)
4469 goto errout;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004470 }
4471
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004472 if (tb[RTA_PREF]) {
4473 pref = nla_get_u8(tb[RTA_PREF]);
4474 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4475 pref != ICMPV6_ROUTER_PREF_HIGH)
4476 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4477 cfg->fc_flags |= RTF_PREF(pref);
4478 }
4479
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004480 if (tb[RTA_ENCAP])
4481 cfg->fc_encap = tb[RTA_ENCAP];
4482
David Ahern9ed59592017-01-17 14:57:36 -08004483 if (tb[RTA_ENCAP_TYPE]) {
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004484 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
4485
David Ahernc255bd62017-05-27 16:19:27 -06004486 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004487 if (err < 0)
4488 goto errout;
4489 }
4490
Xin Long32bc2012015-12-16 17:50:11 +08004491 if (tb[RTA_EXPIRES]) {
4492 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
4493
4494 if (addrconf_finite_timeout(timeout)) {
4495 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
4496 cfg->fc_flags |= RTF_EXPIRES;
4497 }
4498 }
4499
Thomas Graf86872cb2006-08-22 00:01:08 -07004500 err = 0;
4501errout:
4502 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503}
4504
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004505struct rt6_nh {
David Ahern8d1c8022018-04-17 17:33:26 -07004506 struct fib6_info *fib6_info;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004507 struct fib6_config r_cfg;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004508 struct list_head next;
4509};
4510
David Ahernd4ead6b2018-04-17 17:33:16 -07004511static int ip6_route_info_append(struct net *net,
4512 struct list_head *rt6_nh_list,
David Ahern8d1c8022018-04-17 17:33:26 -07004513 struct fib6_info *rt,
4514 struct fib6_config *r_cfg)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004515{
4516 struct rt6_nh *nh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004517 int err = -EEXIST;
4518
4519 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004520 /* check if fib6_info already exists */
4521 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004522 return err;
4523 }
4524
4525 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
4526 if (!nh)
4527 return -ENOMEM;
David Ahern8d1c8022018-04-17 17:33:26 -07004528 nh->fib6_info = rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004529 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
4530 list_add_tail(&nh->next, rt6_nh_list);
4531
4532 return 0;
4533}
4534
David Ahern8d1c8022018-04-17 17:33:26 -07004535static void ip6_route_mpath_notify(struct fib6_info *rt,
4536 struct fib6_info *rt_last,
David Ahern3b1137f2017-02-02 12:37:10 -08004537 struct nl_info *info,
4538 __u16 nlflags)
4539{
4540 /* if this is an APPEND route, then rt points to the first route
4541 * inserted and rt_last points to last route inserted. Userspace
4542 * wants a consistent dump of the route which starts at the first
4543 * nexthop. Since sibling routes are always added at the end of
4544 * the list, find the first sibling of the last route appended
4545 */
David Ahern93c2fb22018-04-18 15:38:59 -07004546 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
4547 rt = list_first_entry(&rt_last->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -07004548 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -07004549 fib6_siblings);
David Ahern3b1137f2017-02-02 12:37:10 -08004550 }
4551
4552 if (rt)
4553 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
4554}
4555
David Ahern333c4302017-05-21 10:12:04 -06004556static int ip6_route_multipath_add(struct fib6_config *cfg,
4557 struct netlink_ext_ack *extack)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004558{
David Ahern8d1c8022018-04-17 17:33:26 -07004559 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
David Ahern3b1137f2017-02-02 12:37:10 -08004560 struct nl_info *info = &cfg->fc_nlinfo;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004561 struct fib6_config r_cfg;
4562 struct rtnexthop *rtnh;
David Ahern8d1c8022018-04-17 17:33:26 -07004563 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004564 struct rt6_nh *err_nh;
4565 struct rt6_nh *nh, *nh_safe;
David Ahern3b1137f2017-02-02 12:37:10 -08004566 __u16 nlflags;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004567 int remaining;
4568 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004569 int err = 1;
4570 int nhn = 0;
4571 int replace = (cfg->fc_nlinfo.nlh &&
4572 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
4573 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004574
David Ahern3b1137f2017-02-02 12:37:10 -08004575 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
4576 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
4577 nlflags |= NLM_F_APPEND;
4578
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02004579 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004580 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004581
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004582 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
David Ahern8d1c8022018-04-17 17:33:26 -07004583 * fib6_info structs per nexthop
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004584 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004585 while (rtnh_ok(rtnh, remaining)) {
4586 memcpy(&r_cfg, cfg, sizeof(*cfg));
4587 if (rtnh->rtnh_ifindex)
4588 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
4589
4590 attrlen = rtnh_attrlen(rtnh);
4591 if (attrlen > 0) {
4592 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4593
4594 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
4595 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004596 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004597 r_cfg.fc_flags |= RTF_GATEWAY;
4598 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004599 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
4600 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
4601 if (nla)
4602 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004603 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004604
David Ahern68e2ffd2018-03-20 10:06:59 -07004605 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
David Ahernacb54e32018-04-17 17:33:22 -07004606 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07004607 if (IS_ERR(rt)) {
4608 err = PTR_ERR(rt);
4609 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004610 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07004611 }
David Ahernb5d2d752018-07-15 09:35:19 -07004612 if (!rt6_qualify_for_ecmp(rt)) {
4613 err = -EINVAL;
4614 NL_SET_ERR_MSG(extack,
4615 "Device only routes can not be added for IPv6 using the multipath API.");
4616 fib6_info_release(rt);
4617 goto cleanup;
4618 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004619
David Ahern1cf844c2019-05-22 20:27:59 -07004620 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
Ido Schimmel398958a2018-01-09 16:40:28 +02004621
David Ahernd4ead6b2018-04-17 17:33:16 -07004622 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4623 rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004624 if (err) {
David Ahern93531c62018-04-17 17:33:25 -07004625 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004626 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004627 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004628
4629 rtnh = rtnh_next(rtnh, &remaining);
4630 }
4631
David Ahern3b1137f2017-02-02 12:37:10 -08004632 /* for add and replace send one notification with all nexthops.
4633 * Skip the notification in fib6_add_rt2node and send one with
4634 * the full route when done
4635 */
4636 info->skip_notify = 1;
4637
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004638 err_nh = NULL;
4639 list_for_each_entry(nh, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004640 err = __ip6_ins_rt(nh->fib6_info, info, extack);
4641 fib6_info_release(nh->fib6_info);
David Ahern3b1137f2017-02-02 12:37:10 -08004642
David Ahernf7225172018-06-04 13:41:42 -07004643 if (!err) {
4644 /* save reference to last route successfully inserted */
4645 rt_last = nh->fib6_info;
4646
4647 /* save reference to first route for notification */
4648 if (!rt_notif)
4649 rt_notif = nh->fib6_info;
4650 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004651
David Ahern8d1c8022018-04-17 17:33:26 -07004652 /* nh->fib6_info is used or freed at this point, reset to NULL*/
4653 nh->fib6_info = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004654 if (err) {
4655 if (replace && nhn)
Jakub Kicinskia5a82d82019-01-14 10:52:45 -08004656 NL_SET_ERR_MSG_MOD(extack,
4657 "multipath route replace failed (check consistency of installed routes)");
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004658 err_nh = nh;
4659 goto add_errout;
4660 }
4661
Nicolas Dichtel1a724182012-11-01 22:58:22 +00004662 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02004663 * these flags after the first nexthop: if there is a collision,
4664 * we have already failed to add the first nexthop:
4665 * fib6_add_rt2node() has rejected it; when replacing, old
4666 * nexthops have been replaced by first new, the rest should
4667 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00004668 */
Michal Kubeček27596472015-05-18 20:54:00 +02004669 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
4670 NLM_F_REPLACE);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004671 nhn++;
4672 }
4673
David Ahern3b1137f2017-02-02 12:37:10 -08004674 /* success ... tell user about new route */
4675 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004676 goto cleanup;
4677
4678add_errout:
David Ahern3b1137f2017-02-02 12:37:10 -08004679 /* send notification for routes that were added so that
4680 * the delete notifications sent by ip6_route_del are
4681 * coherent
4682 */
4683 if (rt_notif)
4684 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
4685
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004686 /* Delete routes that were already added */
4687 list_for_each_entry(nh, &rt6_nh_list, next) {
4688 if (err_nh == nh)
4689 break;
David Ahern333c4302017-05-21 10:12:04 -06004690 ip6_route_del(&nh->r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004691 }
4692
4693cleanup:
4694 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004695 if (nh->fib6_info)
4696 fib6_info_release(nh->fib6_info);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004697 list_del(&nh->next);
4698 kfree(nh);
4699 }
4700
4701 return err;
4702}
4703
David Ahern333c4302017-05-21 10:12:04 -06004704static int ip6_route_multipath_del(struct fib6_config *cfg,
4705 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004706{
4707 struct fib6_config r_cfg;
4708 struct rtnexthop *rtnh;
4709 int remaining;
4710 int attrlen;
4711 int err = 1, last_err = 0;
4712
4713 remaining = cfg->fc_mp_len;
4714 rtnh = (struct rtnexthop *)cfg->fc_mp;
4715
4716 /* Parse a Multipath Entry */
4717 while (rtnh_ok(rtnh, remaining)) {
4718 memcpy(&r_cfg, cfg, sizeof(*cfg));
4719 if (rtnh->rtnh_ifindex)
4720 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
4721
4722 attrlen = rtnh_attrlen(rtnh);
4723 if (attrlen > 0) {
4724 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4725
4726 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
4727 if (nla) {
4728 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
4729 r_cfg.fc_flags |= RTF_GATEWAY;
4730 }
4731 }
David Ahern333c4302017-05-21 10:12:04 -06004732 err = ip6_route_del(&r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004733 if (err)
4734 last_err = err;
4735
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004736 rtnh = rtnh_next(rtnh, &remaining);
4737 }
4738
4739 return last_err;
4740}
4741
David Ahernc21ef3e2017-04-16 09:48:24 -07004742static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4743 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744{
Thomas Graf86872cb2006-08-22 00:01:08 -07004745 struct fib6_config cfg;
4746 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
David Ahern333c4302017-05-21 10:12:04 -06004748 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004749 if (err < 0)
4750 return err;
4751
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004752 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06004753 return ip6_route_multipath_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08004754 else {
4755 cfg.fc_delete_all_nh = 1;
David Ahern333c4302017-05-21 10:12:04 -06004756 return ip6_route_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08004757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758}
4759
David Ahernc21ef3e2017-04-16 09:48:24 -07004760static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4761 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762{
Thomas Graf86872cb2006-08-22 00:01:08 -07004763 struct fib6_config cfg;
4764 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765
David Ahern333c4302017-05-21 10:12:04 -06004766 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004767 if (err < 0)
4768 return err;
4769
David Ahern67f69512019-03-21 05:21:34 -07004770 if (cfg.fc_metric == 0)
4771 cfg.fc_metric = IP6_RT_PRIO_USER;
4772
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004773 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06004774 return ip6_route_multipath_add(&cfg, extack);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004775 else
David Ahernacb54e32018-04-17 17:33:22 -07004776 return ip6_route_add(&cfg, GFP_KERNEL, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777}
4778
David Ahern8d1c8022018-04-17 17:33:26 -07004779static size_t rt6_nlmsg_size(struct fib6_info *rt)
Thomas Graf339bf982006-11-10 14:10:15 -08004780{
David Ahernbeb1afac52017-02-02 12:37:09 -08004781 int nexthop_len = 0;
4782
David Ahern93c2fb22018-04-18 15:38:59 -07004783 if (rt->fib6_nsiblings) {
David Ahernbeb1afac52017-02-02 12:37:09 -08004784 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
4785 + NLA_ALIGN(sizeof(struct rtnexthop))
4786 + nla_total_size(16) /* RTA_GATEWAY */
David Ahern1cf844c2019-05-22 20:27:59 -07004787 + lwtunnel_get_encap_size(rt->fib6_nh->fib_nh_lws);
David Ahernbeb1afac52017-02-02 12:37:09 -08004788
David Ahern93c2fb22018-04-18 15:38:59 -07004789 nexthop_len *= rt->fib6_nsiblings;
David Ahernbeb1afac52017-02-02 12:37:09 -08004790 }
4791
Thomas Graf339bf982006-11-10 14:10:15 -08004792 return NLMSG_ALIGN(sizeof(struct rtmsg))
4793 + nla_total_size(16) /* RTA_SRC */
4794 + nla_total_size(16) /* RTA_DST */
4795 + nla_total_size(16) /* RTA_GATEWAY */
4796 + nla_total_size(16) /* RTA_PREFSRC */
4797 + nla_total_size(4) /* RTA_TABLE */
4798 + nla_total_size(4) /* RTA_IIF */
4799 + nla_total_size(4) /* RTA_OIF */
4800 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08004801 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01004802 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004803 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004804 + nla_total_size(1) /* RTA_PREF */
David Ahern1cf844c2019-05-22 20:27:59 -07004805 + lwtunnel_get_encap_size(rt->fib6_nh->fib_nh_lws)
David Ahernbeb1afac52017-02-02 12:37:09 -08004806 + nexthop_len;
4807}
4808
David Ahernd4ead6b2018-04-17 17:33:16 -07004809static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -07004810 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -07004811 struct in6_addr *dest, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004812 int iif, int type, u32 portid, u32 seq,
David Ahernf8cfe2c2017-01-17 15:51:08 -08004813 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814{
Xin Long22d0bd82018-09-11 14:33:58 +08004815 struct rt6_info *rt6 = (struct rt6_info *)dst;
4816 struct rt6key *rt6_dst, *rt6_src;
4817 u32 *pmetrics, table, rt6_flags;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004818 struct nlmsghdr *nlh;
Xin Long22d0bd82018-09-11 14:33:58 +08004819 struct rtmsg *rtm;
David Ahernd4ead6b2018-04-17 17:33:16 -07004820 long expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
Eric W. Biederman15e47302012-09-07 20:12:54 +00004822 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05004823 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004824 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004825
Xin Long22d0bd82018-09-11 14:33:58 +08004826 if (rt6) {
4827 rt6_dst = &rt6->rt6i_dst;
4828 rt6_src = &rt6->rt6i_src;
4829 rt6_flags = rt6->rt6i_flags;
4830 } else {
4831 rt6_dst = &rt->fib6_dst;
4832 rt6_src = &rt->fib6_src;
4833 rt6_flags = rt->fib6_flags;
4834 }
4835
Thomas Graf2d7202b2006-08-22 00:01:27 -07004836 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 rtm->rtm_family = AF_INET6;
Xin Long22d0bd82018-09-11 14:33:58 +08004838 rtm->rtm_dst_len = rt6_dst->plen;
4839 rtm->rtm_src_len = rt6_src->plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 rtm->rtm_tos = 0;
David Ahern93c2fb22018-04-18 15:38:59 -07004841 if (rt->fib6_table)
4842 table = rt->fib6_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07004843 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07004844 table = RT6_TABLE_UNSPEC;
Kalash Nainwal97f00822019-02-20 16:23:04 -08004845 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
David S. Millerc78679e2012-04-01 20:27:33 -04004846 if (nla_put_u32(skb, RTA_TABLE, table))
4847 goto nla_put_failure;
David Aherne8478e82018-04-17 17:33:13 -07004848
4849 rtm->rtm_type = rt->fib6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 rtm->rtm_flags = 0;
4851 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
David Ahern93c2fb22018-04-18 15:38:59 -07004852 rtm->rtm_protocol = rt->fib6_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
Xin Long22d0bd82018-09-11 14:33:58 +08004854 if (rt6_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 rtm->rtm_flags |= RTM_F_CLONED;
4856
David Ahernd4ead6b2018-04-17 17:33:16 -07004857 if (dest) {
4858 if (nla_put_in6_addr(skb, RTA_DST, dest))
David S. Millerc78679e2012-04-01 20:27:33 -04004859 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004860 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 } else if (rtm->rtm_dst_len)
Xin Long22d0bd82018-09-11 14:33:58 +08004862 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04004863 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864#ifdef CONFIG_IPV6_SUBTREES
4865 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02004866 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04004867 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004868 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04004869 } else if (rtm->rtm_src_len &&
Xin Long22d0bd82018-09-11 14:33:58 +08004870 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04004871 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004873 if (iif) {
4874#ifdef CONFIG_IPV6_MROUTE
Xin Long22d0bd82018-09-11 14:33:58 +08004875 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
David Ahernfd61c6b2017-01-17 15:51:07 -08004876 int err = ip6mr_get_route(net, skb, rtm, portid);
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02004877
David Ahernfd61c6b2017-01-17 15:51:07 -08004878 if (err == 0)
4879 return 0;
4880 if (err < 0)
4881 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004882 } else
4883#endif
David S. Millerc78679e2012-04-01 20:27:33 -04004884 if (nla_put_u32(skb, RTA_IIF, iif))
4885 goto nla_put_failure;
David Ahernd4ead6b2018-04-17 17:33:16 -07004886 } else if (dest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 struct in6_addr saddr_buf;
David Ahernd4ead6b2018-04-17 17:33:16 -07004888 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02004889 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04004890 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07004892
David Ahern93c2fb22018-04-18 15:38:59 -07004893 if (rt->fib6_prefsrc.plen) {
Daniel Walterc3968a82011-04-13 21:10:57 +00004894 struct in6_addr saddr_buf;
David Ahern93c2fb22018-04-18 15:38:59 -07004895 saddr_buf = rt->fib6_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02004896 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04004897 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00004898 }
4899
David Ahernd4ead6b2018-04-17 17:33:16 -07004900 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
4901 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07004902 goto nla_put_failure;
4903
David Ahern93c2fb22018-04-18 15:38:59 -07004904 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
David S. Millerc78679e2012-04-01 20:27:33 -04004905 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00004906
David Ahernbeb1afac52017-02-02 12:37:09 -08004907 /* For multipath routes, walk the siblings list and add
4908 * each as a nexthop within RTA_MULTIPATH.
4909 */
Xin Long22d0bd82018-09-11 14:33:58 +08004910 if (rt6) {
4911 if (rt6_flags & RTF_GATEWAY &&
4912 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
4913 goto nla_put_failure;
4914
4915 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
4916 goto nla_put_failure;
4917 } else if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07004918 struct fib6_info *sibling, *next_sibling;
David Ahernbeb1afac52017-02-02 12:37:09 -08004919 struct nlattr *mp;
4920
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004921 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernbeb1afac52017-02-02 12:37:09 -08004922 if (!mp)
4923 goto nla_put_failure;
4924
David Ahern1cf844c2019-05-22 20:27:59 -07004925 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
4926 rt->fib6_nh->fib_nh_weight) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004927 goto nla_put_failure;
4928
4929 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07004930 &rt->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07004931 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
4932 sibling->fib6_nh->fib_nh_weight) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004933 goto nla_put_failure;
4934 }
4935
4936 nla_nest_end(skb, mp);
4937 } else {
David Ahernecc56632019-04-23 08:48:09 -07004938 unsigned char nh_flags = 0;
4939
David Ahern1cf844c2019-05-22 20:27:59 -07004940 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common,
David Ahernecc56632019-04-23 08:48:09 -07004941 &nh_flags, false) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004942 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07004943
4944 rtm->rtm_flags |= nh_flags;
David Ahernbeb1afac52017-02-02 12:37:09 -08004945 }
4946
Xin Long22d0bd82018-09-11 14:33:58 +08004947 if (rt6_flags & RTF_EXPIRES) {
David Ahern14895682018-04-17 17:33:17 -07004948 expires = dst ? dst->expires : rt->expires;
4949 expires -= jiffies;
4950 }
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07004951
David Ahernd4ead6b2018-04-17 17:33:16 -07004952 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08004953 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954
Xin Long22d0bd82018-09-11 14:33:58 +08004955 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004956 goto nla_put_failure;
4957
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004958
Johannes Berg053c0952015-01-16 22:09:00 +01004959 nlmsg_end(skb, nlh);
4960 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004961
4962nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004963 nlmsg_cancel(skb, nlh);
4964 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965}
4966
David Ahern13e38902018-10-15 18:56:44 -07004967static bool fib6_info_uses_dev(const struct fib6_info *f6i,
4968 const struct net_device *dev)
4969{
David Ahern1cf844c2019-05-22 20:27:59 -07004970 if (f6i->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07004971 return true;
4972
4973 if (f6i->fib6_nsiblings) {
4974 struct fib6_info *sibling, *next_sibling;
4975
4976 list_for_each_entry_safe(sibling, next_sibling,
4977 &f6i->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07004978 if (sibling->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07004979 return true;
4980 }
4981 }
4982
4983 return false;
4984}
4985
David Ahern8d1c8022018-04-17 17:33:26 -07004986int rt6_dump_route(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987{
4988 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
David Ahern13e38902018-10-15 18:56:44 -07004989 struct fib_dump_filter *filter = &arg->filter;
4990 unsigned int flags = NLM_F_MULTI;
David Ahern1f17e2f2017-01-26 13:54:08 -08004991 struct net *net = arg->net;
4992
David Ahern421842e2018-04-17 17:33:18 -07004993 if (rt == net->ipv6.fib6_null_entry)
David Ahern1f17e2f2017-01-26 13:54:08 -08004994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
David Ahern13e38902018-10-15 18:56:44 -07004996 if ((filter->flags & RTM_F_PREFIX) &&
4997 !(rt->fib6_flags & RTF_PREFIX_RT)) {
4998 /* success since this is not a prefix route */
4999 return 1;
5000 }
5001 if (filter->filter_set) {
5002 if ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5003 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5004 (filter->protocol && rt->fib6_protocol != filter->protocol)) {
David Ahernf8cfe2c2017-01-17 15:51:08 -08005005 return 1;
5006 }
David Ahern13e38902018-10-15 18:56:44 -07005007 flags |= NLM_F_DUMP_FILTERED;
David Ahernf8cfe2c2017-01-17 15:51:08 -08005008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009
David Ahernd4ead6b2018-04-17 17:33:16 -07005010 return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
5011 RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
David Ahern13e38902018-10-15 18:56:44 -07005012 arg->cb->nlh->nlmsg_seq, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013}
5014
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005015static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5016 const struct nlmsghdr *nlh,
5017 struct nlattr **tb,
5018 struct netlink_ext_ack *extack)
5019{
5020 struct rtmsg *rtm;
5021 int i, err;
5022
5023 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5024 NL_SET_ERR_MSG_MOD(extack,
5025 "Invalid header for get route request");
5026 return -EINVAL;
5027 }
5028
5029 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02005030 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5031 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005032
5033 rtm = nlmsg_data(nlh);
5034 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5035 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5036 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5037 rtm->rtm_type) {
5038 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5039 return -EINVAL;
5040 }
5041 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5042 NL_SET_ERR_MSG_MOD(extack,
5043 "Invalid flags for get route request");
5044 return -EINVAL;
5045 }
5046
Johannes Berg8cb08172019-04-26 14:07:28 +02005047 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5048 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005049 if (err)
5050 return err;
5051
5052 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5053 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5054 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5055 return -EINVAL;
5056 }
5057
5058 for (i = 0; i <= RTA_MAX; i++) {
5059 if (!tb[i])
5060 continue;
5061
5062 switch (i) {
5063 case RTA_SRC:
5064 case RTA_DST:
5065 case RTA_IIF:
5066 case RTA_OIF:
5067 case RTA_MARK:
5068 case RTA_UID:
5069 case RTA_SPORT:
5070 case RTA_DPORT:
5071 case RTA_IP_PROTO:
5072 break;
5073 default:
5074 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
5075 return -EINVAL;
5076 }
5077 }
5078
5079 return 0;
5080}
5081
David Ahernc21ef3e2017-04-16 09:48:24 -07005082static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5083 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005085 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07005086 struct nlattr *tb[RTA_MAX+1];
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005087 int err, iif = 0, oif = 0;
David Aherna68886a2018-04-20 15:38:02 -07005088 struct fib6_info *from;
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005089 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07005091 struct sk_buff *skb;
5092 struct rtmsg *rtm;
Maciej Żenczykowski744486d2018-09-29 23:44:54 -07005093 struct flowi6 fl6 = {};
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005094 bool fibmatch;
Thomas Grafab364a62006-08-22 00:01:47 -07005095
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005096 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Thomas Grafab364a62006-08-22 00:01:47 -07005097 if (err < 0)
5098 goto errout;
5099
5100 err = -EINVAL;
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02005101 rtm = nlmsg_data(nlh);
5102 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005103 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
Thomas Grafab364a62006-08-22 00:01:47 -07005104
5105 if (tb[RTA_SRC]) {
5106 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5107 goto errout;
5108
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005109 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07005110 }
5111
5112 if (tb[RTA_DST]) {
5113 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5114 goto errout;
5115
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005116 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07005117 }
5118
5119 if (tb[RTA_IIF])
5120 iif = nla_get_u32(tb[RTA_IIF]);
5121
5122 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005123 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07005124
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07005125 if (tb[RTA_MARK])
5126 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
5127
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09005128 if (tb[RTA_UID])
5129 fl6.flowi6_uid = make_kuid(current_user_ns(),
5130 nla_get_u32(tb[RTA_UID]));
5131 else
5132 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5133
Roopa Prabhueacb9382018-05-22 14:03:28 -07005134 if (tb[RTA_SPORT])
5135 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5136
5137 if (tb[RTA_DPORT])
5138 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5139
5140 if (tb[RTA_IP_PROTO]) {
5141 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
Hangbin Liu5e1a99e2019-02-27 16:15:29 +08005142 &fl6.flowi6_proto, AF_INET6,
5143 extack);
Roopa Prabhueacb9382018-05-22 14:03:28 -07005144 if (err)
5145 goto errout;
5146 }
5147
Thomas Grafab364a62006-08-22 00:01:47 -07005148 if (iif) {
5149 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005150 int flags = 0;
5151
Florian Westphal121622d2017-08-15 16:34:42 +02005152 rcu_read_lock();
5153
5154 dev = dev_get_by_index_rcu(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07005155 if (!dev) {
Florian Westphal121622d2017-08-15 16:34:42 +02005156 rcu_read_unlock();
Thomas Grafab364a62006-08-22 00:01:47 -07005157 err = -ENODEV;
5158 goto errout;
5159 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005160
5161 fl6.flowi6_iif = iif;
5162
5163 if (!ipv6_addr_any(&fl6.saddr))
5164 flags |= RT6_LOOKUP_F_HAS_SADDR;
5165
David Ahernb75cc8f2018-03-02 08:32:17 -08005166 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
Florian Westphal121622d2017-08-15 16:34:42 +02005167
5168 rcu_read_unlock();
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005169 } else {
5170 fl6.flowi6_oif = oif;
5171
Ido Schimmel58acfd72017-12-20 12:28:25 +02005172 dst = ip6_route_output(net, NULL, &fl6);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005173 }
5174
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005175
5176 rt = container_of(dst, struct rt6_info, dst);
5177 if (rt->dst.error) {
5178 err = rt->dst.error;
5179 ip6_rt_put(rt);
5180 goto errout;
Thomas Grafab364a62006-08-22 00:01:47 -07005181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182
WANG Cong9d6acb32017-03-01 20:48:39 -08005183 if (rt == net->ipv6.ip6_null_entry) {
5184 err = rt->dst.error;
5185 ip6_rt_put(rt);
5186 goto errout;
5187 }
5188
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05005190 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00005191 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07005192 err = -ENOBUFS;
5193 goto errout;
5194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195
Changli Gaod8d1f302010-06-10 23:31:35 -07005196 skb_dst_set(skb, &rt->dst);
David Aherna68886a2018-04-20 15:38:02 -07005197
5198 rcu_read_lock();
5199 from = rcu_dereference(rt->from);
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07005200 if (from) {
5201 if (fibmatch)
5202 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5203 iif, RTM_NEWROUTE,
5204 NETLINK_CB(in_skb).portid,
5205 nlh->nlmsg_seq, 0);
5206 else
5207 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5208 &fl6.saddr, iif, RTM_NEWROUTE,
5209 NETLINK_CB(in_skb).portid,
5210 nlh->nlmsg_seq, 0);
5211 } else {
5212 err = -ENETUNREACH;
5213 }
David Aherna68886a2018-04-20 15:38:02 -07005214 rcu_read_unlock();
5215
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07005217 kfree_skb(skb);
5218 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 }
5220
Eric W. Biederman15e47302012-09-07 20:12:54 +00005221 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07005222errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224}
5225
David Ahern8d1c8022018-04-17 17:33:26 -07005226void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07005227 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228{
5229 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08005230 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08005231 u32 seq;
5232 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08005234 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05005235 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07005236
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005237 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05005238 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07005239 goto errout;
5240
David Ahernd4ead6b2018-04-17 17:33:16 -07005241 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5242 event, info->portid, seq, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08005243 if (err < 0) {
5244 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
5245 WARN_ON(err == -EMSGSIZE);
5246 kfree_skb(skb);
5247 goto errout;
5248 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00005249 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005250 info->nlh, gfp_any());
5251 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07005252errout:
5253 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08005254 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255}
5256
David Ahern19a3b7e2019-05-22 12:04:41 -07005257void fib6_rt_update(struct net *net, struct fib6_info *rt,
5258 struct nl_info *info)
5259{
5260 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
5261 struct sk_buff *skb;
5262 int err = -ENOBUFS;
5263
5264 /* call_fib6_entry_notifiers will be removed when in-kernel notifier
5265 * is implemented and supported for nexthop objects
5266 */
5267 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
5268
5269 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
5270 if (!skb)
5271 goto errout;
5272
5273 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5274 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
5275 if (err < 0) {
5276 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
5277 WARN_ON(err == -EMSGSIZE);
5278 kfree_skb(skb);
5279 goto errout;
5280 }
5281 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
5282 info->nlh, gfp_any());
5283 return;
5284errout:
5285 if (err < 0)
5286 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
5287}
5288
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005289static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00005290 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005291{
Jiri Pirko351638e2013-05-28 01:30:21 +00005292 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005293 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005294
WANG Cong242d3a42017-05-08 10:12:13 -07005295 if (!(dev->flags & IFF_LOOPBACK))
5296 return NOTIFY_OK;
5297
5298 if (event == NETDEV_REGISTER) {
David Ahern1cf844c2019-05-22 20:27:59 -07005299 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
Changli Gaod8d1f302010-06-10 23:31:35 -07005300 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005301 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
5302#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07005303 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005304 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07005305 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005306 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
5307#endif
WANG Cong76da0702017-06-20 11:42:27 -07005308 } else if (event == NETDEV_UNREGISTER &&
5309 dev->reg_state != NETREG_UNREGISTERED) {
5310 /* NETDEV_UNREGISTER could be fired for multiple times by
5311 * netdev_wait_allrefs(). Make sure we only call this once.
5312 */
Eric Dumazet12d94a82017-08-15 04:09:51 -07005313 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07005314#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric Dumazet12d94a82017-08-15 04:09:51 -07005315 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
5316 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07005317#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005318 }
5319
5320 return NOTIFY_OK;
5321}
5322
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323/*
5324 * /proc
5325 */
5326
5327#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328static int rt6_stats_seq_show(struct seq_file *seq, void *v)
5329{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005330 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005332 net->ipv6.rt6_stats->fib_nodes,
5333 net->ipv6.rt6_stats->fib_route_nodes,
Wei Wang81eb8442017-10-06 12:06:11 -07005334 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005335 net->ipv6.rt6_stats->fib_rt_entries,
5336 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00005337 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005338 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339
5340 return 0;
5341}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342#endif /* CONFIG_PROC_FS */
5343
5344#ifdef CONFIG_SYSCTL
5345
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005347int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 void __user *buffer, size_t *lenp, loff_t *ppos)
5349{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005350 struct net *net;
5351 int delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06005352 int ret;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005353 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005355
5356 net = (struct net *)ctl->extra1;
5357 delay = net->ipv6.sysctl.flush_delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06005358 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5359 if (ret)
5360 return ret;
5361
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02005362 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364}
5365
David Ahern7c6bb7d2018-10-11 20:17:21 -07005366static int zero;
5367static int one = 1;
5368
David Aherned792e22018-10-08 14:06:34 -07005369static struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005370 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08005372 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07005374 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005375 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 },
5377 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08005379 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 .maxlen = sizeof(int),
5381 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005382 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 },
5384 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08005386 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 .maxlen = sizeof(int),
5388 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005389 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 },
5391 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08005393 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 .maxlen = sizeof(int),
5395 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005396 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 },
5398 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08005400 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 .maxlen = sizeof(int),
5402 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005403 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 },
5405 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08005407 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 .maxlen = sizeof(int),
5409 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005410 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 },
5412 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08005414 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 .maxlen = sizeof(int),
5416 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07005417 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 },
5419 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08005421 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 .maxlen = sizeof(int),
5423 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005424 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 },
5426 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08005428 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 .maxlen = sizeof(int),
5430 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07005431 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 },
5433 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08005435 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 .maxlen = sizeof(int),
5437 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005438 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 },
David Ahern7c6bb7d2018-10-11 20:17:21 -07005440 {
5441 .procname = "skip_notify_on_dev_down",
5442 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
5443 .maxlen = sizeof(int),
5444 .mode = 0644,
5445 .proc_handler = proc_dointvec,
5446 .extra1 = &zero,
5447 .extra2 = &one,
5448 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08005449 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450};
5451
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005452struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08005453{
5454 struct ctl_table *table;
5455
5456 table = kmemdup(ipv6_route_table_template,
5457 sizeof(ipv6_route_table_template),
5458 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005459
5460 if (table) {
5461 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005462 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00005463 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005464 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
5465 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
5466 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
5467 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
5468 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
5469 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
5470 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08005471 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
David Ahern7c6bb7d2018-10-11 20:17:21 -07005472 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
Eric W. Biederman464dc802012-11-16 03:02:59 +00005473
5474 /* Don't export sysctls to unprivileged users */
5475 if (net->user_ns != &init_user_ns)
5476 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005477 }
5478
Daniel Lezcano760f2d02008-01-10 02:53:43 -08005479 return table;
5480}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481#endif
5482
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005483static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005484{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07005485 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005486
Alexey Dobriyan86393e52009-08-29 01:34:49 +00005487 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
5488 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005489
Eric Dumazetfc66f952010-10-08 06:37:34 +00005490 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5491 goto out_ip6_dst_ops;
5492
David Ahern1cf844c2019-05-22 20:27:59 -07005493 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
David Ahern421842e2018-04-17 17:33:18 -07005494 if (!net->ipv6.fib6_null_entry)
5495 goto out_ip6_dst_entries;
David Ahern1cf844c2019-05-22 20:27:59 -07005496 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
5497 sizeof(*net->ipv6.fib6_null_entry));
David Ahern421842e2018-04-17 17:33:18 -07005498
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005499 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
5500 sizeof(*net->ipv6.ip6_null_entry),
5501 GFP_KERNEL);
5502 if (!net->ipv6.ip6_null_entry)
David Ahern421842e2018-04-17 17:33:18 -07005503 goto out_fib6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005504 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005505 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
5506 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005507
5508#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Vincent Bernatfeca7d82017-08-08 20:23:49 +02005509 net->ipv6.fib6_has_custom_rules = false;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005510 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
5511 sizeof(*net->ipv6.ip6_prohibit_entry),
5512 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005513 if (!net->ipv6.ip6_prohibit_entry)
5514 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005515 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005516 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
5517 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005518
5519 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
5520 sizeof(*net->ipv6.ip6_blk_hole_entry),
5521 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005522 if (!net->ipv6.ip6_blk_hole_entry)
5523 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005524 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005525 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
5526 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005527#endif
5528
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07005529 net->ipv6.sysctl.flush_delay = 0;
5530 net->ipv6.sysctl.ip6_rt_max_size = 4096;
5531 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5532 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5533 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5534 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5535 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5536 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
David Ahern7c6bb7d2018-10-11 20:17:21 -07005537 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07005538
Benjamin Thery6891a342008-03-04 13:49:47 -08005539 net->ipv6.ip6_rt_gc_expire = 30*HZ;
5540
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005541 ret = 0;
5542out:
5543 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005544
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005545#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5546out_ip6_prohibit_entry:
5547 kfree(net->ipv6.ip6_prohibit_entry);
5548out_ip6_null_entry:
5549 kfree(net->ipv6.ip6_null_entry);
5550#endif
David Ahern421842e2018-04-17 17:33:18 -07005551out_fib6_null_entry:
5552 kfree(net->ipv6.fib6_null_entry);
Eric Dumazetfc66f952010-10-08 06:37:34 +00005553out_ip6_dst_entries:
5554 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005555out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005556 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005557}
5558
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005559static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005560{
David Ahern421842e2018-04-17 17:33:18 -07005561 kfree(net->ipv6.fib6_null_entry);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005562 kfree(net->ipv6.ip6_null_entry);
5563#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5564 kfree(net->ipv6.ip6_prohibit_entry);
5565 kfree(net->ipv6.ip6_blk_hole_entry);
5566#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00005567 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005568}
5569
Thomas Grafd1896342012-06-18 12:08:33 +00005570static int __net_init ip6_route_net_init_late(struct net *net)
5571{
5572#ifdef CONFIG_PROC_FS
Christoph Hellwigc3506372018-04-10 19:42:55 +02005573 proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
5574 sizeof(struct ipv6_route_iter));
Christoph Hellwig3617d942018-04-13 20:38:35 +02005575 proc_create_net_single("rt6_stats", 0444, net->proc_net,
5576 rt6_stats_seq_show, NULL);
Thomas Grafd1896342012-06-18 12:08:33 +00005577#endif
5578 return 0;
5579}
5580
5581static void __net_exit ip6_route_net_exit_late(struct net *net)
5582{
5583#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00005584 remove_proc_entry("ipv6_route", net->proc_net);
5585 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00005586#endif
5587}
5588
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005589static struct pernet_operations ip6_route_net_ops = {
5590 .init = ip6_route_net_init,
5591 .exit = ip6_route_net_exit,
5592};
5593
David S. Millerc3426b42012-06-09 16:27:05 -07005594static int __net_init ipv6_inetpeer_init(struct net *net)
5595{
5596 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5597
5598 if (!bp)
5599 return -ENOMEM;
5600 inet_peer_base_init(bp);
5601 net->ipv6.peers = bp;
5602 return 0;
5603}
5604
5605static void __net_exit ipv6_inetpeer_exit(struct net *net)
5606{
5607 struct inet_peer_base *bp = net->ipv6.peers;
5608
5609 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07005610 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07005611 kfree(bp);
5612}
5613
David S. Miller2b823f72012-06-09 19:00:16 -07005614static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07005615 .init = ipv6_inetpeer_init,
5616 .exit = ipv6_inetpeer_exit,
5617};
5618
Thomas Grafd1896342012-06-18 12:08:33 +00005619static struct pernet_operations ip6_route_net_late_ops = {
5620 .init = ip6_route_net_init_late,
5621 .exit = ip6_route_net_exit_late,
5622};
5623
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005624static struct notifier_block ip6_route_dev_notifier = {
5625 .notifier_call = ip6_route_dev_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07005626 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005627};
5628
WANG Cong2f460932017-05-03 22:07:31 -07005629void __init ip6_route_init_special_entries(void)
5630{
5631 /* Registering of the loopback is done before this portion of code,
5632 * the loopback reference in rt6_info will not be taken, do it
5633 * manually for init_net */
David Ahern1cf844c2019-05-22 20:27:59 -07005634 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
WANG Cong2f460932017-05-03 22:07:31 -07005635 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
5636 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5637 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5638 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
5639 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5640 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
5641 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5642 #endif
5643}
5644
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005645int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005647 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07005648 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005649
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08005650 ret = -ENOMEM;
5651 ip6_dst_ops_template.kmem_cachep =
5652 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
5653 SLAB_HWCACHE_ALIGN, NULL);
5654 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08005655 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07005656
Eric Dumazetfc66f952010-10-08 06:37:34 +00005657 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005658 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08005659 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08005660
David S. Millerc3426b42012-06-09 16:27:05 -07005661 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
5662 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07005663 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00005664
David S. Miller7e52b332012-06-15 15:51:55 -07005665 ret = register_pernet_subsys(&ip6_route_net_ops);
5666 if (ret)
5667 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07005668
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07005669 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
5670
David S. Millere8803b62012-06-16 01:12:19 -07005671 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005672 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005673 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005674
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005675 ret = xfrm6_init();
5676 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07005677 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08005678
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005679 ret = fib6_rules_init();
5680 if (ret)
5681 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08005682
Thomas Grafd1896342012-06-18 12:08:33 +00005683 ret = register_pernet_subsys(&ip6_route_net_late_ops);
5684 if (ret)
5685 goto fib6_rules_init;
5686
Florian Westphal16feebc2017-12-02 21:44:08 +01005687 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
5688 inet6_rtm_newroute, NULL, 0);
5689 if (ret < 0)
5690 goto out_register_late_subsys;
5691
5692 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
5693 inet6_rtm_delroute, NULL, 0);
5694 if (ret < 0)
5695 goto out_register_late_subsys;
5696
5697 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
5698 inet6_rtm_getroute, NULL,
5699 RTNL_FLAG_DOIT_UNLOCKED);
5700 if (ret < 0)
Thomas Grafd1896342012-06-18 12:08:33 +00005701 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005702
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005703 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005704 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00005705 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005706
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07005707 for_each_possible_cpu(cpu) {
5708 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
5709
5710 INIT_LIST_HEAD(&ul->head);
5711 spin_lock_init(&ul->lock);
5712 }
5713
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005714out:
5715 return ret;
5716
Thomas Grafd1896342012-06-18 12:08:33 +00005717out_register_late_subsys:
Florian Westphal16feebc2017-12-02 21:44:08 +01005718 rtnl_unregister_all(PF_INET6);
Thomas Grafd1896342012-06-18 12:08:33 +00005719 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005720fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005721 fib6_rules_cleanup();
5722xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005723 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00005724out_fib6_init:
5725 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005726out_register_subsys:
5727 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07005728out_register_inetpeer:
5729 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00005730out_dst_entries:
5731 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005732out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005733 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005734 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735}
5736
5737void ip6_route_cleanup(void)
5738{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005739 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00005740 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07005741 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07005744 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005745 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00005746 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005747 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748}