blob: 715356e00f587c35f1bb7bb7085ee0ce95e599c5 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Linux INET6 implementation
4 * FIB front-end.
5 *
6 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09007 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10/* Changes:
11 *
12 * YOSHIFUJI Hideaki @USAGI
13 * reworked default router selection.
14 * - respect outgoing interface
15 * - select from (probably) reachable routers (i.e.
16 * routers in REACHABLE, STALE, DELAY or PROBE states).
17 * - always select the same router if it is (probably)
18 * reachable. otherwise, round-robin the list.
YOSHIFUJI Hideakic0bece92006-08-23 17:23:25 -070019 * Ville Nuorvala
20 * Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
Joe Perchesf3213832012-05-15 14:11:53 +000023#define pr_fmt(fmt) "IPv6: " fmt
24
Randy Dunlap4fc268d2006-01-11 12:17:47 -080025#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/errno.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040027#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/types.h>
29#include <linux/times.h>
30#include <linux/socket.h>
31#include <linux/sockios.h>
32#include <linux/net.h>
33#include <linux/route.h>
34#include <linux/netdevice.h>
35#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090036#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/proc_fs.h>
40#include <linux/seq_file.h>
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080041#include <linux/nsproxy.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Wei Wang35732d02017-10-06 12:05:57 -070043#include <linux/jhash.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020044#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <net/snmp.h>
46#include <net/ipv6.h>
47#include <net/ip6_fib.h>
48#include <net/ip6_route.h>
49#include <net/ndisc.h>
50#include <net/addrconf.h>
51#include <net/tcp.h>
52#include <linux/rtnetlink.h>
53#include <net/dst.h>
Jiri Benc904af042015-08-20 13:56:31 +020054#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/xfrm.h>
Tom Tucker8d717402006-07-30 20:43:36 -070056#include <net/netevent.h>
Thomas Graf21713eb2006-08-15 00:35:24 -070057#include <net/netlink.h>
David Ahern3c618c12019-04-20 09:28:20 -070058#include <net/rtnh.h>
Roopa Prabhu19e42e42015-07-21 10:43:48 +020059#include <net/lwtunnel.h>
Jiri Benc904af042015-08-20 13:56:31 +020060#include <net/ip_tunnels.h>
David Ahernca254492015-10-12 11:47:10 -070061#include <net/l3mdev.h>
Roopa Prabhueacb9382018-05-22 14:03:28 -070062#include <net/ip.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080063#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#ifdef CONFIG_SYSCTL
66#include <linux/sysctl.h>
67#endif
68
David Ahern30d444d2018-05-23 17:08:48 -070069static int ip6_rt_type_to_error(u8 fib6_type);
70
71#define CREATE_TRACE_POINTS
72#include <trace/events/fib6.h>
73EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
74#undef CREATE_TRACE_POINTS
75
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020076enum rt6_nud_state {
Jiri Benc7e980562013-12-11 13:48:20 +010077 RT6_NUD_FAIL_HARD = -3,
78 RT6_NUD_FAIL_PROBE = -2,
79 RT6_NUD_FAIL_DO_RR = -1,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020080 RT6_NUD_SUCCEED = 1
81};
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
David S. Miller0dbaee32010-12-13 12:52:14 -080084static unsigned int ip6_default_advmss(const struct dst_entry *dst);
Steffen Klassertebb762f2011-11-23 02:12:51 +000085static unsigned int ip6_mtu(const struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static struct dst_entry *ip6_negative_advice(struct dst_entry *);
87static void ip6_dst_destroy(struct dst_entry *);
88static void ip6_dst_ifdown(struct dst_entry *,
89 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080090static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92static int ip6_pkt_discard(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050093static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Kamala R7150aed2013-12-02 19:55:21 +053094static int ip6_pkt_prohibit(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050095static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static void ip6_link_failure(struct sk_buff *skb);
David S. Miller6700c272012-07-17 03:29:28 -070097static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
98 struct sk_buff *skb, u32 mtu);
99static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
100 struct sk_buff *skb);
David Ahern702cea52019-04-09 14:41:13 -0700101static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
102 int strict);
David Aherna1b7a1f2019-06-08 14:53:26 -0700103static size_t rt6_nlmsg_size(struct fib6_info *f6i);
David Ahernd4ead6b2018-04-17 17:33:16 -0700104static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -0700105 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -0700106 struct in6_addr *dest, struct in6_addr *src,
David Ahern16a16cd2017-02-02 12:37:11 -0800107 int iif, int type, u32 portid, u32 seq,
108 unsigned int flags);
David Ahern7e4b5122019-04-16 14:36:00 -0700109static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
Wei Wang510e2ce2019-05-16 13:30:54 -0700110 const struct in6_addr *daddr,
111 const struct in6_addr *saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800113#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -0700114static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000115 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700116 const struct in6_addr *gwaddr,
117 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +0000118 unsigned int pref);
David Ahern8d1c8022018-04-17 17:33:26 -0700119static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000120 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700121 const struct in6_addr *gwaddr,
122 struct net_device *dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800123#endif
124
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700125struct uncached_list {
126 spinlock_t lock;
127 struct list_head head;
128};
129
130static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
131
Xin Long510c3212018-02-14 19:06:02 +0800132void rt6_uncached_list_add(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700133{
134 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
135
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700136 rt->rt6i_uncached_list = ul;
137
138 spin_lock_bh(&ul->lock);
139 list_add_tail(&rt->rt6i_uncached, &ul->head);
140 spin_unlock_bh(&ul->lock);
141}
142
Xin Long510c3212018-02-14 19:06:02 +0800143void rt6_uncached_list_del(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700144{
145 if (!list_empty(&rt->rt6i_uncached)) {
146 struct uncached_list *ul = rt->rt6i_uncached_list;
Wei Wang81eb8442017-10-06 12:06:11 -0700147 struct net *net = dev_net(rt->dst.dev);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700148
149 spin_lock_bh(&ul->lock);
150 list_del(&rt->rt6i_uncached);
Wei Wang81eb8442017-10-06 12:06:11 -0700151 atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700152 spin_unlock_bh(&ul->lock);
153 }
154}
155
156static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
157{
158 struct net_device *loopback_dev = net->loopback_dev;
159 int cpu;
160
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500161 if (dev == loopback_dev)
162 return;
163
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700164 for_each_possible_cpu(cpu) {
165 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
166 struct rt6_info *rt;
167
168 spin_lock_bh(&ul->lock);
169 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
170 struct inet6_dev *rt_idev = rt->rt6i_idev;
171 struct net_device *rt_dev = rt->dst.dev;
172
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500173 if (rt_idev->dev == dev) {
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700174 rt->rt6i_idev = in6_dev_get(loopback_dev);
175 in6_dev_put(rt_idev);
176 }
177
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500178 if (rt_dev == dev) {
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700179 rt->dst.dev = loopback_dev;
180 dev_hold(rt->dst.dev);
181 dev_put(rt_dev);
182 }
183 }
184 spin_unlock_bh(&ul->lock);
185 }
186}
187
David Ahernf8a1b432018-04-17 17:33:21 -0700188static inline const void *choose_neigh_daddr(const struct in6_addr *p,
David S. Millerf894cbf2012-07-02 21:52:24 -0700189 struct sk_buff *skb,
190 const void *daddr)
David S. Miller39232972012-01-26 15:22:32 -0500191{
David S. Millera7563f32012-01-26 16:29:16 -0500192 if (!ipv6_addr_any(p))
David S. Miller39232972012-01-26 15:22:32 -0500193 return (const void *) p;
David S. Millerf894cbf2012-07-02 21:52:24 -0700194 else if (skb)
195 return &ipv6_hdr(skb)->daddr;
David S. Miller39232972012-01-26 15:22:32 -0500196 return daddr;
197}
198
David Ahernf8a1b432018-04-17 17:33:21 -0700199struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
200 struct net_device *dev,
201 struct sk_buff *skb,
202 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700203{
David S. Miller39232972012-01-26 15:22:32 -0500204 struct neighbour *n;
205
David Ahernf8a1b432018-04-17 17:33:21 -0700206 daddr = choose_neigh_daddr(gw, skb, daddr);
207 n = __ipv6_neigh_lookup(dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500208 if (n)
209 return n;
Stefano Brivio7adf3242019-01-02 13:29:27 +0100210
211 n = neigh_create(&nd_tbl, daddr, dev);
212 return IS_ERR(n) ? NULL : n;
David Ahernf8a1b432018-04-17 17:33:21 -0700213}
214
215static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
216 struct sk_buff *skb,
217 const void *daddr)
218{
219 const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
220
221 return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500222}
223
Julian Anastasov63fca652017-02-06 23:14:15 +0200224static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
225{
226 struct net_device *dev = dst->dev;
227 struct rt6_info *rt = (struct rt6_info *)dst;
228
David Ahernf8a1b432018-04-17 17:33:21 -0700229 daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +0200230 if (!daddr)
231 return;
232 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
233 return;
234 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
235 return;
236 __ipv6_confirm_neigh(dev, daddr);
237}
238
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800239static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .gc = ip6_dst_gc,
242 .gc_thresh = 1024,
243 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800244 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000245 .mtu = ip6_mtu,
David Ahernd4ead6b2018-04-17 17:33:16 -0700246 .cow_metrics = dst_cow_metrics_generic,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 .destroy = ip6_dst_destroy,
248 .ifdown = ip6_dst_ifdown,
249 .negative_advice = ip6_negative_advice,
250 .link_failure = ip6_link_failure,
251 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700252 .redirect = rt6_do_redirect,
Eric W. Biederman9f8955c2015-10-07 16:48:39 -0500253 .local_out = __ip6_local_out,
David Ahernf8a1b432018-04-17 17:33:21 -0700254 .neigh_lookup = ip6_dst_neigh_lookup,
Julian Anastasov63fca652017-02-06 23:14:15 +0200255 .confirm_neigh = ip6_confirm_neigh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
Steffen Klassertebb762f2011-11-23 02:12:51 +0000258static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800259{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000260 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
261
262 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800263}
264
David S. Miller6700c272012-07-17 03:29:28 -0700265static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
266 struct sk_buff *skb, u32 mtu)
David S. Miller14e50e52007-05-24 18:17:54 -0700267{
268}
269
David S. Miller6700c272012-07-17 03:29:28 -0700270static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
271 struct sk_buff *skb)
David S. Millerb587ee32012-07-12 00:39:24 -0700272{
273}
274
David S. Miller14e50e52007-05-24 18:17:54 -0700275static struct dst_ops ip6_dst_blackhole_ops = {
276 .family = AF_INET6,
David S. Miller14e50e52007-05-24 18:17:54 -0700277 .destroy = ip6_dst_destroy,
278 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000279 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800280 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700281 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Millerb587ee32012-07-12 00:39:24 -0700282 .redirect = ip6_rt_blackhole_redirect,
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -0700283 .cow_metrics = dst_cow_metrics_generic,
David Ahernf8a1b432018-04-17 17:33:21 -0700284 .neigh_lookup = ip6_dst_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700285};
286
David S. Miller62fa8a82011-01-26 20:51:05 -0800287static const u32 ip6_template_metrics[RTAX_MAX] = {
Li RongQing14edd872012-10-24 14:01:18 +0800288 [RTAX_HOPLIMIT - 1] = 0,
David S. Miller62fa8a82011-01-26 20:51:05 -0800289};
290
David Ahern8d1c8022018-04-17 17:33:26 -0700291static const struct fib6_info fib6_null_entry_template = {
David Ahern93c2fb22018-04-18 15:38:59 -0700292 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
293 .fib6_protocol = RTPROT_KERNEL,
294 .fib6_metric = ~(u32)0,
Eric Dumazetf05713e2019-04-22 18:35:03 -0700295 .fib6_ref = REFCOUNT_INIT(1),
David Ahern421842e2018-04-17 17:33:18 -0700296 .fib6_type = RTN_UNREACHABLE,
297 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
298};
299
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000300static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700301 .dst = {
302 .__refcnt = ATOMIC_INIT(1),
303 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000304 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700305 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700306 .input = ip6_pkt_discard,
307 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 },
309 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
Thomas Graf101367c2006-08-04 03:39:02 -0700312#ifdef CONFIG_IPV6_MULTIPLE_TABLES
313
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000314static const struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700315 .dst = {
316 .__refcnt = ATOMIC_INIT(1),
317 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000318 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700319 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700320 .input = ip6_pkt_prohibit,
321 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700322 },
323 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700324};
325
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000326static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700327 .dst = {
328 .__refcnt = ATOMIC_INIT(1),
329 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000330 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700331 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700332 .input = dst_discard,
Eric W. Biedermanede20592015-10-07 16:48:47 -0500333 .output = dst_discard_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700334 },
335 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700336};
337
338#endif
339
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700340static void rt6_info_init(struct rt6_info *rt)
341{
342 struct dst_entry *dst = &rt->dst;
343
344 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700345 INIT_LIST_HEAD(&rt->rt6i_uncached);
346}
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/* allocate dst with ip6_dst_ops */
David Ahern93531c62018-04-17 17:33:25 -0700349struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
350 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
David S. Miller97bab732012-06-09 22:36:36 -0700352 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Wei Wangb2a9c0e2017-06-17 10:42:41 -0700353 1, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700354
Wei Wang81eb8442017-10-06 12:06:11 -0700355 if (rt) {
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700356 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -0700357 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
358 }
Steffen Klassert81048912012-07-05 23:37:09 +0000359
David S. Millercf911662011-04-28 14:31:47 -0700360 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
David Ahern9ab179d2016-04-07 11:10:06 -0700362EXPORT_SYMBOL(ip6_dst_alloc);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364static void ip6_dst_destroy(struct dst_entry *dst)
365{
366 struct rt6_info *rt = (struct rt6_info *)dst;
David Aherna68886a2018-04-20 15:38:02 -0700367 struct fib6_info *from;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700368 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
David Ahern1620a332018-10-04 20:07:54 -0700370 ip_dst_metrics_put(dst);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700371 rt6_uncached_list_del(rt);
372
373 idev = rt->rt6i_idev;
David S. Miller38308472011-12-03 18:02:47 -0500374 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 rt->rt6i_idev = NULL;
376 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900377 }
Gao feng1716a962012-04-06 00:13:10 +0000378
Eric Dumazet0e233872019-04-28 12:22:25 -0700379 from = xchg((__force struct fib6_info **)&rt->from, NULL);
David Ahern93531c62018-04-17 17:33:25 -0700380 fib6_info_release(from);
David S. Millerb3419362010-11-30 12:27:11 -0800381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
384 int how)
385{
386 struct rt6_info *rt = (struct rt6_info *)dst;
387 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800388 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900389 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Wei Wange5645f52017-08-14 10:44:59 -0700391 if (idev && idev->dev != loopback_dev) {
392 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
393 if (loopback_idev) {
394 rt->rt6i_idev = loopback_idev;
395 in6_dev_put(idev);
David S. Miller97cac082012-07-02 22:43:47 -0700396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398}
399
Martin KaFai Lau5973fb12015-11-11 11:51:07 -0800400static bool __rt6_check_expired(const struct rt6_info *rt)
401{
402 if (rt->rt6i_flags & RTF_EXPIRES)
403 return time_after(jiffies, rt->dst.expires);
404 else
405 return false;
406}
407
Eric Dumazeta50feda2012-05-18 18:57:34 +0000408static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
David Aherna68886a2018-04-20 15:38:02 -0700410 struct fib6_info *from;
411
412 from = rcu_dereference(rt->from);
413
Gao feng1716a962012-04-06 00:13:10 +0000414 if (rt->rt6i_flags & RTF_EXPIRES) {
415 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000416 return true;
David Aherna68886a2018-04-20 15:38:02 -0700417 } else if (from) {
Xin Long1e2ea8a2017-08-26 20:10:10 +0800418 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
David Aherna68886a2018-04-20 15:38:02 -0700419 fib6_check_expired(from);
Gao feng1716a962012-04-06 00:13:10 +0000420 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000421 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
David Ahernb1d40992019-04-16 14:35:59 -0700424void fib6_select_path(const struct net *net, struct fib6_result *res,
425 struct flowi6 *fl6, int oif, bool have_oif_match,
426 const struct sk_buff *skb, int strict)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000427{
David Ahern8d1c8022018-04-17 17:33:26 -0700428 struct fib6_info *sibling, *next_sibling;
David Ahernb1d40992019-04-16 14:35:59 -0700429 struct fib6_info *match = res->f6i;
430
David Ahernf88d8ea2019-06-03 20:19:52 -0700431 if ((!match->fib6_nsiblings && !match->nh) || have_oif_match)
David Ahernb1d40992019-04-16 14:35:59 -0700432 goto out;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000433
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200434 /* We might have already computed the hash for ICMPv6 errors. In such
435 * case it will always be non-zero. Otherwise now is the time to do it.
436 */
David Ahernf88d8ea2019-06-03 20:19:52 -0700437 if (!fl6->mp_hash &&
438 (!match->nh || nexthop_is_multipath(match->nh)))
David Ahernb4bac172018-03-02 08:32:18 -0800439 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200440
David Ahernf88d8ea2019-06-03 20:19:52 -0700441 if (unlikely(match->nh)) {
442 nexthop_path_fib6_result(res, fl6->mp_hash);
443 return;
444 }
445
David Ahern1cf844c2019-05-22 20:27:59 -0700446 if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
David Ahernb1d40992019-04-16 14:35:59 -0700447 goto out;
Ido Schimmelbbfcd772017-11-21 09:50:12 +0200448
David Ahern93c2fb22018-04-18 15:38:59 -0700449 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
450 fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -0700451 const struct fib6_nh *nh = sibling->fib6_nh;
David Ahern5e670d82018-04-17 17:33:14 -0700452 int nh_upper_bound;
453
David Ahern702cea52019-04-09 14:41:13 -0700454 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
David Ahern5e670d82018-04-17 17:33:14 -0700455 if (fl6->mp_hash > nh_upper_bound)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200456 continue;
David Ahern702cea52019-04-09 14:41:13 -0700457 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200458 break;
459 match = sibling;
460 break;
461 }
462
David Ahernb1d40992019-04-16 14:35:59 -0700463out:
464 res->f6i = match;
David Ahern1cf844c2019-05-22 20:27:59 -0700465 res->nh = match->fib6_nh;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*
Wei Wang66f5d6c2017-10-06 12:06:10 -0700469 * Route lookup. rcu_read_lock() should be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 */
471
David Ahern0c59d002019-04-09 14:41:18 -0700472static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
473 const struct in6_addr *saddr, int oif, int flags)
474{
475 const struct net_device *dev;
476
477 if (nh->fib_nh_flags & RTNH_F_DEAD)
478 return false;
479
480 dev = nh->fib_nh_dev;
481 if (oif) {
482 if (dev->ifindex == oif)
483 return true;
484 } else {
485 if (ipv6_chk_addr(net, saddr, dev,
486 flags & RT6_LOOKUP_F_IFACE))
487 return true;
488 }
489
490 return false;
491}
492
David Ahern962b6802019-06-08 14:53:24 -0700493struct fib6_nh_dm_arg {
494 struct net *net;
495 const struct in6_addr *saddr;
496 int oif;
497 int flags;
498 struct fib6_nh *nh;
499};
500
501static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
502{
503 struct fib6_nh_dm_arg *arg = _arg;
504
505 arg->nh = nh;
506 return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
507 arg->flags);
508}
509
510/* returns fib6_nh from nexthop or NULL */
511static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
512 struct fib6_result *res,
513 const struct in6_addr *saddr,
514 int oif, int flags)
515{
516 struct fib6_nh_dm_arg arg = {
517 .net = net,
518 .saddr = saddr,
519 .oif = oif,
520 .flags = flags,
521 };
522
523 if (nexthop_is_blackhole(nh))
524 return NULL;
525
526 if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
527 return arg.nh;
528
529 return NULL;
530}
531
David Ahern75ef7382019-04-16 14:36:07 -0700532static void rt6_device_match(struct net *net, struct fib6_result *res,
533 const struct in6_addr *saddr, int oif, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
David Ahern75ef7382019-04-16 14:36:07 -0700535 struct fib6_info *f6i = res->f6i;
536 struct fib6_info *spf6i;
537 struct fib6_nh *nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
David Ahern75ef7382019-04-16 14:36:07 -0700539 if (!oif && ipv6_addr_any(saddr)) {
David Ahernf88d8ea2019-06-03 20:19:52 -0700540 if (unlikely(f6i->nh)) {
541 nh = nexthop_fib6_nh(f6i->nh);
542 if (nexthop_is_blackhole(f6i->nh))
543 goto out_blackhole;
544 } else {
545 nh = f6i->fib6_nh;
546 }
David Ahern7d21fec2019-04-16 14:36:11 -0700547 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
548 goto out;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
David Ahern75ef7382019-04-16 14:36:07 -0700551 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
David Ahern962b6802019-06-08 14:53:24 -0700552 bool matched = false;
553
554 if (unlikely(spf6i->nh)) {
555 nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
556 oif, flags);
557 if (nh)
558 matched = true;
559 } else {
560 nh = spf6i->fib6_nh;
561 if (__rt6_device_match(net, nh, saddr, oif, flags))
562 matched = true;
563 }
564 if (matched) {
David Ahern75ef7382019-04-16 14:36:07 -0700565 res->f6i = spf6i;
David Ahern7d21fec2019-04-16 14:36:11 -0700566 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700567 }
568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
David Ahern75ef7382019-04-16 14:36:07 -0700570 if (oif && flags & RT6_LOOKUP_F_IFACE) {
571 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700572 nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700573 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700574 }
575
David Ahernf88d8ea2019-06-03 20:19:52 -0700576 if (unlikely(f6i->nh)) {
577 nh = nexthop_fib6_nh(f6i->nh);
578 if (nexthop_is_blackhole(f6i->nh))
579 goto out_blackhole;
580 } else {
581 nh = f6i->fib6_nh;
582 }
583
David Ahern7d21fec2019-04-16 14:36:11 -0700584 if (nh->fib_nh_flags & RTNH_F_DEAD) {
David Ahern75ef7382019-04-16 14:36:07 -0700585 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700586 nh = res->f6i->fib6_nh;
David Ahern75ef7382019-04-16 14:36:07 -0700587 }
David Ahern7d21fec2019-04-16 14:36:11 -0700588out:
589 res->nh = nh;
590 res->fib6_type = res->f6i->fib6_type;
591 res->fib6_flags = res->f6i->fib6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -0700592 return;
593
594out_blackhole:
595 res->fib6_flags |= RTF_REJECT;
596 res->fib6_type = RTN_BLACKHOLE;
597 res->nh = nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800600#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200601struct __rt6_probe_work {
602 struct work_struct work;
603 struct in6_addr target;
604 struct net_device *dev;
605};
606
607static void rt6_probe_deferred(struct work_struct *w)
608{
609 struct in6_addr mcaddr;
610 struct __rt6_probe_work *work =
611 container_of(w, struct __rt6_probe_work, work);
612
613 addrconf_addr_solict_mult(&work->target, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800614 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200615 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100616 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200617}
618
David Aherncc3a86c2019-04-09 14:41:12 -0700619static void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800620{
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200621 struct __rt6_probe_work *work = NULL;
David Ahern5e670d82018-04-17 17:33:14 -0700622 const struct in6_addr *nh_gw;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000623 struct neighbour *neigh;
David Ahern5e670d82018-04-17 17:33:14 -0700624 struct net_device *dev;
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200625 struct inet6_dev *idev;
David Ahern5e670d82018-04-17 17:33:14 -0700626
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800627 /*
628 * Okay, this does not seem to be appropriate
629 * for now, however, we need to check if it
630 * is really so; aka Router Reachability Probing.
631 *
632 * Router Reachability Probe MUST be rate-limited
633 * to no more than one per minute.
634 */
David Aherncc3a86c2019-04-09 14:41:12 -0700635 if (fib6_nh->fib_nh_gw_family)
Amerigo Wangfdd66812012-09-10 02:48:44 +0000636 return;
David Ahern5e670d82018-04-17 17:33:14 -0700637
David Aherncc3a86c2019-04-09 14:41:12 -0700638 nh_gw = &fib6_nh->fib_nh_gw6;
639 dev = fib6_nh->fib_nh_dev;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000640 rcu_read_lock_bh();
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200641 idev = __in6_dev_get(dev);
David Ahern5e670d82018-04-17 17:33:14 -0700642 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000643 if (neigh) {
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700644 if (neigh->nud_state & NUD_VALID)
645 goto out;
646
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000647 write_lock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700648 if (!(neigh->nud_state & NUD_VALID) &&
649 time_after(jiffies,
David Aherndcd1f572018-04-18 15:39:05 -0700650 neigh->updated + idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700651 work = kmalloc(sizeof(*work), GFP_ATOMIC);
652 if (work)
653 __neigh_set_probe_once(neigh);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200654 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000655 write_unlock(&neigh->lock);
David Aherncc3a86c2019-04-09 14:41:12 -0700656 } else if (time_after(jiffies, fib6_nh->last_probe +
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200657 idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700658 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000659 }
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700660
661 if (work) {
David Aherncc3a86c2019-04-09 14:41:12 -0700662 fib6_nh->last_probe = jiffies;
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700663 INIT_WORK(&work->work, rt6_probe_deferred);
David Ahern5e670d82018-04-17 17:33:14 -0700664 work->target = *nh_gw;
665 dev_hold(dev);
666 work->dev = dev;
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700667 schedule_work(&work->work);
668 }
669
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700670out:
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000671 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800672}
673#else
David Aherncc3a86c2019-04-09 14:41:12 -0700674static inline void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800675{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800676}
677#endif
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800680 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 */
David Ahern1ba9a892019-04-09 14:41:10 -0700682static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200684 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
David Ahern5e670d82018-04-17 17:33:14 -0700685 struct neighbour *neigh;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000686
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000687 rcu_read_lock_bh();
David Ahern1ba9a892019-04-09 14:41:10 -0700688 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
689 &fib6_nh->fib_nh_gw6);
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000690 if (neigh) {
691 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800692 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200693 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800694#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000695 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200696 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100697 else
698 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800699#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000700 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200701 } else {
702 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100703 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000704 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000705 rcu_read_unlock_bh();
706
Paul Marksa5a81f02012-12-03 10:26:54 +0000707 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800708}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
David Ahern702cea52019-04-09 14:41:13 -0700710static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
711 int strict)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800712{
David Ahern6e1809a2019-04-09 14:41:11 -0700713 int m = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900714
David Ahern6e1809a2019-04-09 14:41:11 -0700715 if (!oif || nh->fib_nh_dev->ifindex == oif)
716 m = 2;
717
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700718 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200719 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800720#ifdef CONFIG_IPV6_ROUTER_PREF
David Ahern702cea52019-04-09 14:41:13 -0700721 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800722#endif
David Ahern1ba9a892019-04-09 14:41:10 -0700723 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
David Ahern702cea52019-04-09 14:41:13 -0700724 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
David Ahern1ba9a892019-04-09 14:41:10 -0700725 int n = rt6_check_neigh(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200726 if (n < 0)
727 return n;
728 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800729 return m;
730}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
David Ahern28679ed2019-04-09 14:41:14 -0700732static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
733 int oif, int strict, int *mpri, bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800734{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200735 bool match_do_rr = false;
David Ahern28679ed2019-04-09 14:41:14 -0700736 bool rc = false;
737 int m;
Andy Gospodarek35103d12015-08-13 10:39:01 -0400738
David Ahern28679ed2019-04-09 14:41:14 -0700739 if (nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +0200740 goto out;
741
David Ahern28679ed2019-04-09 14:41:14 -0700742 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
743 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
David Ahernd5d32e42016-10-24 12:27:23 -0700744 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
Andy Gospodarek35103d12015-08-13 10:39:01 -0400745 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700746
David Ahern28679ed2019-04-09 14:41:14 -0700747 m = rt6_score_route(nh, fib6_flags, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100748 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200749 match_do_rr = true;
750 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100751 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700752 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700753 }
754
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200755 if (strict & RT6_LOOKUP_F_REACHABLE)
David Ahern28679ed2019-04-09 14:41:14 -0700756 rt6_probe(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200757
Jiri Benc7e980562013-12-11 13:48:20 +0100758 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200759 if (m > *mpri) {
760 *do_rr = match_do_rr;
761 *mpri = m;
David Ahern28679ed2019-04-09 14:41:14 -0700762 rc = true;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200763 }
David S. Millerf11e6652007-03-24 20:36:25 -0700764out:
David Ahern28679ed2019-04-09 14:41:14 -0700765 return rc;
David S. Millerf11e6652007-03-24 20:36:25 -0700766}
767
David Ahern17a59842019-06-08 14:53:25 -0700768struct fib6_nh_frl_arg {
769 u32 flags;
770 int oif;
771 int strict;
772 int *mpri;
773 bool *do_rr;
774 struct fib6_nh *nh;
775};
776
777static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
778{
779 struct fib6_nh_frl_arg *arg = _arg;
780
781 arg->nh = nh;
782 return find_match(nh, arg->flags, arg->oif, arg->strict,
783 arg->mpri, arg->do_rr);
784}
785
David Ahernb7bc4b62019-04-16 14:36:08 -0700786static void __find_rr_leaf(struct fib6_info *f6i_start,
David Ahern30c15f02019-04-09 14:41:15 -0700787 struct fib6_info *nomatch, u32 metric,
David Ahernb7bc4b62019-04-16 14:36:08 -0700788 struct fib6_result *res, struct fib6_info **cont,
David Ahern30c15f02019-04-09 14:41:15 -0700789 int oif, int strict, bool *do_rr, int *mpri)
David S. Millerf11e6652007-03-24 20:36:25 -0700790{
David Ahernb7bc4b62019-04-16 14:36:08 -0700791 struct fib6_info *f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700792
David Ahernb7bc4b62019-04-16 14:36:08 -0700793 for (f6i = f6i_start;
794 f6i && f6i != nomatch;
795 f6i = rcu_dereference(f6i->fib6_next)) {
David Ahern17a59842019-06-08 14:53:25 -0700796 bool matched = false;
David Ahern30c15f02019-04-09 14:41:15 -0700797 struct fib6_nh *nh;
798
David Ahernb7bc4b62019-04-16 14:36:08 -0700799 if (cont && f6i->fib6_metric != metric) {
800 *cont = f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700801 return;
802 }
803
David Ahernb7bc4b62019-04-16 14:36:08 -0700804 if (fib6_check_expired(f6i))
David Ahern30c15f02019-04-09 14:41:15 -0700805 continue;
806
David Ahern17a59842019-06-08 14:53:25 -0700807 if (unlikely(f6i->nh)) {
808 struct fib6_nh_frl_arg arg = {
809 .flags = f6i->fib6_flags,
810 .oif = oif,
811 .strict = strict,
812 .mpri = mpri,
813 .do_rr = do_rr
814 };
815
816 if (nexthop_is_blackhole(f6i->nh)) {
817 res->fib6_flags = RTF_REJECT;
818 res->fib6_type = RTN_BLACKHOLE;
819 res->f6i = f6i;
820 res->nh = nexthop_fib6_nh(f6i->nh);
821 return;
822 }
823 if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
824 &arg)) {
825 matched = true;
826 nh = arg.nh;
827 }
828 } else {
829 nh = f6i->fib6_nh;
830 if (find_match(nh, f6i->fib6_flags, oif, strict,
831 mpri, do_rr))
832 matched = true;
833 }
834 if (matched) {
David Ahernb7bc4b62019-04-16 14:36:08 -0700835 res->f6i = f6i;
836 res->nh = nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700837 res->fib6_flags = f6i->fib6_flags;
838 res->fib6_type = f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700839 }
David Ahern30c15f02019-04-09 14:41:15 -0700840 }
841}
842
David Ahernb7bc4b62019-04-16 14:36:08 -0700843static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
844 struct fib6_info *rr_head, int oif, int strict,
845 bool *do_rr, struct fib6_result *res)
David Ahern30c15f02019-04-09 14:41:15 -0700846{
David Ahernb7bc4b62019-04-16 14:36:08 -0700847 u32 metric = rr_head->fib6_metric;
848 struct fib6_info *cont = NULL;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800849 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
David Ahernb7bc4b62019-04-16 14:36:08 -0700851 __find_rr_leaf(rr_head, NULL, metric, res, &cont,
David Ahern30c15f02019-04-09 14:41:15 -0700852 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700853
David Ahernb7bc4b62019-04-16 14:36:08 -0700854 __find_rr_leaf(leaf, rr_head, metric, res, &cont,
David Ahern30c15f02019-04-09 14:41:15 -0700855 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700856
David Ahernb7bc4b62019-04-16 14:36:08 -0700857 if (res->f6i || !cont)
858 return;
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700859
David Ahernb7bc4b62019-04-16 14:36:08 -0700860 __find_rr_leaf(cont, NULL, metric, res, NULL,
David Ahern30c15f02019-04-09 14:41:15 -0700861 oif, strict, do_rr, &mpri);
David S. Millerf11e6652007-03-24 20:36:25 -0700862}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800863
David Ahernb7bc4b62019-04-16 14:36:08 -0700864static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
865 struct fib6_result *res, int strict)
David S. Millerf11e6652007-03-24 20:36:25 -0700866{
David Ahern8d1c8022018-04-17 17:33:26 -0700867 struct fib6_info *leaf = rcu_dereference(fn->leaf);
David Ahernb7bc4b62019-04-16 14:36:08 -0700868 struct fib6_info *rt0;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200869 bool do_rr = false;
Wei Wang17ecf592017-10-06 12:06:09 -0700870 int key_plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
David Ahernb7bc4b62019-04-16 14:36:08 -0700872 /* make sure this function or its helpers sets f6i */
873 res->f6i = NULL;
874
David Ahern421842e2018-04-17 17:33:18 -0700875 if (!leaf || leaf == net->ipv6.fib6_null_entry)
David Ahernb7bc4b62019-04-16 14:36:08 -0700876 goto out;
Wei Wang8d1040e2017-10-06 12:06:08 -0700877
Wei Wang66f5d6c2017-10-06 12:06:10 -0700878 rt0 = rcu_dereference(fn->rr_ptr);
David S. Millerf11e6652007-03-24 20:36:25 -0700879 if (!rt0)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700880 rt0 = leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Wei Wang17ecf592017-10-06 12:06:09 -0700882 /* Double check to make sure fn is not an intermediate node
883 * and fn->leaf does not points to its child's leaf
884 * (This might happen if all routes under fn are deleted from
885 * the tree and fib6_repair_tree() is called on the node.)
886 */
David Ahern93c2fb22018-04-18 15:38:59 -0700887 key_plen = rt0->fib6_dst.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700888#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -0700889 if (rt0->fib6_src.plen)
890 key_plen = rt0->fib6_src.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700891#endif
892 if (fn->fn_bit != key_plen)
David Ahernb7bc4b62019-04-16 14:36:08 -0700893 goto out;
Wei Wang17ecf592017-10-06 12:06:09 -0700894
David Ahernb7bc4b62019-04-16 14:36:08 -0700895 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200896 if (do_rr) {
David Ahern8fb11a92018-05-04 13:54:24 -0700897 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
David S. Millerf11e6652007-03-24 20:36:25 -0700898
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800899 /* no entries matched; do round-robin */
David Ahern93c2fb22018-04-18 15:38:59 -0700900 if (!next || next->fib6_metric != rt0->fib6_metric)
Wei Wang8d1040e2017-10-06 12:06:08 -0700901 next = leaf;
David S. Millerf11e6652007-03-24 20:36:25 -0700902
Wei Wang66f5d6c2017-10-06 12:06:10 -0700903 if (next != rt0) {
David Ahern93c2fb22018-04-18 15:38:59 -0700904 spin_lock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700905 /* make sure next is not being deleted from the tree */
David Ahern93c2fb22018-04-18 15:38:59 -0700906 if (next->fib6_node)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700907 rcu_assign_pointer(fn->rr_ptr, next);
David Ahern93c2fb22018-04-18 15:38:59 -0700908 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911
David Ahernb7bc4b62019-04-16 14:36:08 -0700912out:
913 if (!res->f6i) {
914 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700915 res->nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700916 res->fib6_flags = res->f6i->fib6_flags;
917 res->fib6_type = res->f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
David Ahern85bd05d2019-04-16 14:36:01 -0700921static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700922{
David Ahern85bd05d2019-04-16 14:36:01 -0700923 return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
924 res->nh->fib_nh_gw_family;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700925}
926
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800927#ifdef CONFIG_IPV6_ROUTE_INFO
928int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000929 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800930{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900931 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800932 struct route_info *rinfo = (struct route_info *) opt;
933 struct in6_addr prefix_buf, *prefix;
934 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900935 unsigned long lifetime;
David Ahern8d1c8022018-04-17 17:33:26 -0700936 struct fib6_info *rt;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800937
938 if (len < sizeof(struct route_info)) {
939 return -EINVAL;
940 }
941
942 /* Sanity check for prefix_len and length */
943 if (rinfo->length > 3) {
944 return -EINVAL;
945 } else if (rinfo->prefix_len > 128) {
946 return -EINVAL;
947 } else if (rinfo->prefix_len > 64) {
948 if (rinfo->length < 2) {
949 return -EINVAL;
950 }
951 } else if (rinfo->prefix_len > 0) {
952 if (rinfo->length < 1) {
953 return -EINVAL;
954 }
955 }
956
957 pref = rinfo->route_pref;
958 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000959 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800960
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900961 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800962
963 if (rinfo->length == 3)
964 prefix = (struct in6_addr *)rinfo->prefix;
965 else {
966 /* this function is safe */
967 ipv6_addr_prefix(&prefix_buf,
968 (struct in6_addr *)rinfo->prefix,
969 rinfo->prefix_len);
970 prefix = &prefix_buf;
971 }
972
Duan Jiongf104a562013-11-08 09:56:53 +0800973 if (rinfo->prefix_len == 0)
David Ahernafb1d4b52018-04-17 17:33:11 -0700974 rt = rt6_get_dflt_router(net, gwaddr, dev);
Duan Jiongf104a562013-11-08 09:56:53 +0800975 else
976 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
David Ahern830218c2016-10-24 10:52:35 -0700977 gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800978
979 if (rt && !lifetime) {
David Ahernafb1d4b52018-04-17 17:33:11 -0700980 ip6_del_rt(net, rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800981 rt = NULL;
982 }
983
984 if (!rt && lifetime)
David Ahern830218c2016-10-24 10:52:35 -0700985 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
986 dev, pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800987 else if (rt)
David Ahern93c2fb22018-04-18 15:38:59 -0700988 rt->fib6_flags = RTF_ROUTEINFO |
989 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800990
991 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000992 if (!addrconf_finite_timeout(lifetime))
David Ahern14895682018-04-17 17:33:17 -0700993 fib6_clean_expires(rt);
Gao feng1716a962012-04-06 00:13:10 +0000994 else
David Ahern14895682018-04-17 17:33:17 -0700995 fib6_set_expires(rt, jiffies + HZ * lifetime);
Gao feng1716a962012-04-06 00:13:10 +0000996
David Ahern93531c62018-04-17 17:33:25 -0700997 fib6_info_release(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800998 }
999 return 0;
1000}
1001#endif
1002
David Ahernae90d862018-04-17 17:33:12 -07001003/*
1004 * Misc support functions
1005 */
1006
1007/* called with rcu_lock held */
David Ahern0d161582019-04-16 14:36:04 -07001008static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
David Ahernae90d862018-04-17 17:33:12 -07001009{
David Ahern0d161582019-04-16 14:36:04 -07001010 struct net_device *dev = res->nh->fib_nh_dev;
David Ahernae90d862018-04-17 17:33:12 -07001011
David Ahern7d21fec2019-04-16 14:36:11 -07001012 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
David Ahernae90d862018-04-17 17:33:12 -07001013 /* for copies of local routes, dst->dev needs to be the
1014 * device if it is a master device, the master device if
1015 * device is enslaved, and the loopback as the default
1016 */
1017 if (netif_is_l3_slave(dev) &&
David Ahern7d21fec2019-04-16 14:36:11 -07001018 !rt6_need_strict(&res->f6i->fib6_dst.addr))
David Ahernae90d862018-04-17 17:33:12 -07001019 dev = l3mdev_master_dev_rcu(dev);
1020 else if (!netif_is_l3_master(dev))
1021 dev = dev_net(dev)->loopback_dev;
1022 /* last case is netif_is_l3_master(dev) is true in which
1023 * case we want dev returned to be dev
1024 */
1025 }
1026
1027 return dev;
1028}
1029
David Ahern6edb3c92018-04-17 17:33:15 -07001030static const int fib6_prop[RTN_MAX + 1] = {
1031 [RTN_UNSPEC] = 0,
1032 [RTN_UNICAST] = 0,
1033 [RTN_LOCAL] = 0,
1034 [RTN_BROADCAST] = 0,
1035 [RTN_ANYCAST] = 0,
1036 [RTN_MULTICAST] = 0,
1037 [RTN_BLACKHOLE] = -EINVAL,
1038 [RTN_UNREACHABLE] = -EHOSTUNREACH,
1039 [RTN_PROHIBIT] = -EACCES,
1040 [RTN_THROW] = -EAGAIN,
1041 [RTN_NAT] = -EINVAL,
1042 [RTN_XRESOLVE] = -EINVAL,
1043};
1044
1045static int ip6_rt_type_to_error(u8 fib6_type)
1046{
1047 return fib6_prop[fib6_type];
1048}
1049
David Ahern8d1c8022018-04-17 17:33:26 -07001050static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
David Ahern3b6761d2018-04-17 17:33:20 -07001051{
1052 unsigned short flags = 0;
1053
1054 if (rt->dst_nocount)
1055 flags |= DST_NOCOUNT;
1056 if (rt->dst_nopolicy)
1057 flags |= DST_NOPOLICY;
1058 if (rt->dst_host)
1059 flags |= DST_HOST;
1060
1061 return flags;
1062}
1063
David Ahern7d21fec2019-04-16 14:36:11 -07001064static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
David Ahern6edb3c92018-04-17 17:33:15 -07001065{
David Ahern7d21fec2019-04-16 14:36:11 -07001066 rt->dst.error = ip6_rt_type_to_error(fib6_type);
David Ahern6edb3c92018-04-17 17:33:15 -07001067
David Ahern7d21fec2019-04-16 14:36:11 -07001068 switch (fib6_type) {
David Ahern6edb3c92018-04-17 17:33:15 -07001069 case RTN_BLACKHOLE:
1070 rt->dst.output = dst_discard_out;
1071 rt->dst.input = dst_discard;
1072 break;
1073 case RTN_PROHIBIT:
1074 rt->dst.output = ip6_pkt_prohibit_out;
1075 rt->dst.input = ip6_pkt_prohibit;
1076 break;
1077 case RTN_THROW:
1078 case RTN_UNREACHABLE:
1079 default:
1080 rt->dst.output = ip6_pkt_discard_out;
1081 rt->dst.input = ip6_pkt_discard;
1082 break;
1083 }
1084}
1085
David Ahern0d161582019-04-16 14:36:04 -07001086static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
David Ahern6edb3c92018-04-17 17:33:15 -07001087{
David Ahern7d21fec2019-04-16 14:36:11 -07001088 struct fib6_info *f6i = res->f6i;
David Ahern0d161582019-04-16 14:36:04 -07001089
David Ahern7d21fec2019-04-16 14:36:11 -07001090 if (res->fib6_flags & RTF_REJECT) {
1091 ip6_rt_init_dst_reject(rt, res->fib6_type);
David Ahern6edb3c92018-04-17 17:33:15 -07001092 return;
1093 }
1094
1095 rt->dst.error = 0;
1096 rt->dst.output = ip6_output;
1097
David Ahern7d21fec2019-04-16 14:36:11 -07001098 if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
David Ahern6edb3c92018-04-17 17:33:15 -07001099 rt->dst.input = ip6_input;
David Ahern7d21fec2019-04-16 14:36:11 -07001100 } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
David Ahern6edb3c92018-04-17 17:33:15 -07001101 rt->dst.input = ip6_mc_input;
1102 } else {
1103 rt->dst.input = ip6_forward;
1104 }
1105
David Ahern0d161582019-04-16 14:36:04 -07001106 if (res->nh->fib_nh_lws) {
1107 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
David Ahern6edb3c92018-04-17 17:33:15 -07001108 lwtunnel_set_redirect(&rt->dst);
1109 }
1110
1111 rt->dst.lastuse = jiffies;
1112}
1113
Wei Wange873e4b2018-07-21 20:56:32 -07001114/* Caller must already hold reference to @from */
David Ahern8d1c8022018-04-17 17:33:26 -07001115static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
David Ahernae90d862018-04-17 17:33:12 -07001116{
David Ahernae90d862018-04-17 17:33:12 -07001117 rt->rt6i_flags &= ~RTF_EXPIRES;
David Aherna68886a2018-04-20 15:38:02 -07001118 rcu_assign_pointer(rt->from, from);
David Aherne1255ed2018-10-04 20:07:53 -07001119 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
David Ahernae90d862018-04-17 17:33:12 -07001120}
1121
David Ahern0d161582019-04-16 14:36:04 -07001122/* Caller must already hold reference to f6i in result */
1123static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
David Ahernae90d862018-04-17 17:33:12 -07001124{
David Ahern0d161582019-04-16 14:36:04 -07001125 const struct fib6_nh *nh = res->nh;
1126 const struct net_device *dev = nh->fib_nh_dev;
1127 struct fib6_info *f6i = res->f6i;
David Aherndcd1f572018-04-18 15:39:05 -07001128
David Ahern0d161582019-04-16 14:36:04 -07001129 ip6_rt_init_dst(rt, res);
David Ahern6edb3c92018-04-17 17:33:15 -07001130
David Ahern0d161582019-04-16 14:36:04 -07001131 rt->rt6i_dst = f6i->fib6_dst;
David Aherndcd1f572018-04-18 15:39:05 -07001132 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
David Ahern7d21fec2019-04-16 14:36:11 -07001133 rt->rt6i_flags = res->fib6_flags;
David Ahern0d161582019-04-16 14:36:04 -07001134 if (nh->fib_nh_gw_family) {
1135 rt->rt6i_gateway = nh->fib_nh_gw6;
David Ahern2b2450c2019-03-27 20:53:52 -07001136 rt->rt6i_flags |= RTF_GATEWAY;
1137 }
David Ahern0d161582019-04-16 14:36:04 -07001138 rt6_set_from(rt, f6i);
David Ahernae90d862018-04-17 17:33:12 -07001139#ifdef CONFIG_IPV6_SUBTREES
David Ahern0d161582019-04-16 14:36:04 -07001140 rt->rt6i_src = f6i->fib6_src;
David Ahernae90d862018-04-17 17:33:12 -07001141#endif
David Ahernae90d862018-04-17 17:33:12 -07001142}
1143
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001144static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1145 struct in6_addr *saddr)
1146{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001147 struct fib6_node *pn, *sn;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001148 while (1) {
1149 if (fn->fn_flags & RTN_TL_ROOT)
1150 return NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001151 pn = rcu_dereference(fn->parent);
1152 sn = FIB6_SUBTREE(pn);
1153 if (sn && sn != fn)
David Ahern64547432018-05-09 20:34:19 -07001154 fn = fib6_node_lookup(sn, NULL, saddr);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001155 else
1156 fn = pn;
1157 if (fn->fn_flags & RTN_RTINFO)
1158 return fn;
1159 }
1160}
Thomas Grafc71099a2006-08-04 23:20:06 -07001161
David Ahern10585b42019-03-20 09:24:50 -07001162static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
Wei Wangd3843fe2017-10-06 12:06:06 -07001163{
1164 struct rt6_info *rt = *prt;
1165
1166 if (dst_hold_safe(&rt->dst))
1167 return true;
David Ahern10585b42019-03-20 09:24:50 -07001168 if (net) {
Wei Wangd3843fe2017-10-06 12:06:06 -07001169 rt = net->ipv6.ip6_null_entry;
1170 dst_hold(&rt->dst);
1171 } else {
1172 rt = NULL;
1173 }
1174 *prt = rt;
1175 return false;
1176}
1177
David Aherndec9b0e2018-04-17 17:33:19 -07001178/* called with rcu_lock held */
David Ahern9b6b35a2019-04-16 14:36:02 -07001179static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
David Aherndec9b0e2018-04-17 17:33:19 -07001180{
David Ahern9b6b35a2019-04-16 14:36:02 -07001181 struct net_device *dev = res->nh->fib_nh_dev;
1182 struct fib6_info *f6i = res->f6i;
1183 unsigned short flags;
David Aherndec9b0e2018-04-17 17:33:19 -07001184 struct rt6_info *nrt;
1185
David Ahern9b6b35a2019-04-16 14:36:02 -07001186 if (!fib6_info_hold_safe(f6i))
Xin Long1c87e792019-03-20 14:45:48 +08001187 goto fallback;
Wei Wange873e4b2018-07-21 20:56:32 -07001188
David Ahern9b6b35a2019-04-16 14:36:02 -07001189 flags = fib6_info_dst_flags(f6i);
David Ahern93531c62018-04-17 17:33:25 -07001190 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
Xin Long1c87e792019-03-20 14:45:48 +08001191 if (!nrt) {
David Ahern9b6b35a2019-04-16 14:36:02 -07001192 fib6_info_release(f6i);
Xin Long1c87e792019-03-20 14:45:48 +08001193 goto fallback;
1194 }
David Aherndec9b0e2018-04-17 17:33:19 -07001195
David Ahern0d161582019-04-16 14:36:04 -07001196 ip6_rt_copy_init(nrt, res);
Xin Long1c87e792019-03-20 14:45:48 +08001197 return nrt;
1198
1199fallback:
1200 nrt = dev_net(dev)->ipv6.ip6_null_entry;
1201 dst_hold(&nrt->dst);
David Aherndec9b0e2018-04-17 17:33:19 -07001202 return nrt;
1203}
1204
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001205static struct rt6_info *ip6_pol_route_lookup(struct net *net,
1206 struct fib6_table *table,
David Ahernb75cc8f2018-03-02 08:32:17 -08001207 struct flowi6 *fl6,
1208 const struct sk_buff *skb,
1209 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
David Ahernb1d40992019-04-16 14:35:59 -07001211 struct fib6_result res = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 struct fib6_node *fn;
David Ahern23fb93a2018-04-17 17:33:23 -07001213 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
David Ahernb6cdbc82018-03-29 17:44:57 -07001215 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1216 flags &= ~RT6_LOOKUP_F_IFACE;
1217
Wei Wang66f5d6c2017-10-06 12:06:10 -07001218 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07001219 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -07001220restart:
David Ahernb1d40992019-04-16 14:35:59 -07001221 res.f6i = rcu_dereference(fn->leaf);
1222 if (!res.f6i)
1223 res.f6i = net->ipv6.fib6_null_entry;
David Ahernaf52a522019-04-09 14:41:16 -07001224 else
David Ahern75ef7382019-04-16 14:36:07 -07001225 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1226 flags);
David Ahernaf52a522019-04-09 14:41:16 -07001227
David Ahernb1d40992019-04-16 14:35:59 -07001228 if (res.f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001229 fn = fib6_backtrack(fn, &fl6->saddr);
1230 if (fn)
1231 goto restart;
David Ahernaf52a522019-04-09 14:41:16 -07001232
1233 rt = net->ipv6.ip6_null_entry;
1234 dst_hold(&rt->dst);
1235 goto out;
David Ahernf88d8ea2019-06-03 20:19:52 -07001236 } else if (res.fib6_flags & RTF_REJECT) {
1237 goto do_create;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001238 }
Wei Wang2b760fc2017-10-06 12:06:03 -07001239
David Ahernb1d40992019-04-16 14:35:59 -07001240 fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1241 fl6->flowi6_oif != 0, skb, flags);
1242
David S. Miller4c9483b2011-03-12 16:22:43 -05001243 /* Search through exception table */
David Ahern7e4b5122019-04-16 14:36:00 -07001244 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
David Ahern23fb93a2018-04-17 17:33:23 -07001245 if (rt) {
David Ahern10585b42019-03-20 09:24:50 -07001246 if (ip6_hold_safe(net, &rt))
David Aherndec9b0e2018-04-17 17:33:19 -07001247 dst_use_noref(&rt->dst, jiffies);
David Ahern23fb93a2018-04-17 17:33:23 -07001248 } else {
David Ahernf88d8ea2019-06-03 20:19:52 -07001249do_create:
David Ahern9b6b35a2019-04-16 14:36:02 -07001250 rt = ip6_create_rt_rcu(&res);
David Aherndec9b0e2018-04-17 17:33:19 -07001251 }
Wei Wangd3843fe2017-10-06 12:06:06 -07001252
David Ahernaf52a522019-04-09 14:41:16 -07001253out:
David Ahern8ff2e5b2019-04-16 14:36:09 -07001254 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahernaf52a522019-04-09 14:41:16 -07001255
Wei Wang66f5d6c2017-10-06 12:06:10 -07001256 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08001257
Thomas Grafc71099a2006-08-04 23:20:06 -07001258 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001259}
1260
Ian Morris67ba4152014-08-24 21:53:10 +01001261struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08001262 const struct sk_buff *skb, int flags)
Florian Westphalea6e5742011-09-05 16:05:44 +02001263{
David Ahernb75cc8f2018-03-02 08:32:17 -08001264 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
Florian Westphalea6e5742011-09-05 16:05:44 +02001265}
1266EXPORT_SYMBOL_GPL(ip6_route_lookup);
1267
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001268struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
David Ahernb75cc8f2018-03-02 08:32:17 -08001269 const struct in6_addr *saddr, int oif,
1270 const struct sk_buff *skb, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -07001271{
David S. Miller4c9483b2011-03-12 16:22:43 -05001272 struct flowi6 fl6 = {
1273 .flowi6_oif = oif,
1274 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001275 };
1276 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001277 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07001278
Thomas Grafadaa70b2006-10-13 15:01:03 -07001279 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -05001280 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -07001281 flags |= RT6_LOOKUP_F_HAS_SADDR;
1282 }
1283
David Ahernb75cc8f2018-03-02 08:32:17 -08001284 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -07001285 if (dst->error == 0)
1286 return (struct rt6_info *) dst;
1287
1288 dst_release(dst);
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return NULL;
1291}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001292EXPORT_SYMBOL(rt6_lookup);
1293
Thomas Grafc71099a2006-08-04 23:20:06 -07001294/* ip6_ins_rt is called with FREE table->tb6_lock.
Wei Wang1cfb71e2017-06-17 10:42:33 -07001295 * It takes new route entry, the addition fails by any reason the
1296 * route is released.
1297 * Caller must hold dst before calling it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 */
1299
David Ahern8d1c8022018-04-17 17:33:26 -07001300static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
David Ahern333c4302017-05-21 10:12:04 -06001301 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001304 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
David Ahern93c2fb22018-04-18 15:38:59 -07001306 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001307 spin_lock_bh(&table->tb6_lock);
David Ahernd4ead6b2018-04-17 17:33:16 -07001308 err = fib6_add(&table->tb6_root, rt, info, extack);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001309 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 return err;
1312}
1313
David Ahern8d1c8022018-04-17 17:33:26 -07001314int ip6_ins_rt(struct net *net, struct fib6_info *rt)
Thomas Graf40e22e82006-08-22 00:00:45 -07001315{
David Ahernafb1d4b52018-04-17 17:33:11 -07001316 struct nl_info info = { .nl_net = net, };
Florian Westphale715b6d2015-01-05 23:57:44 +01001317
David Ahernd4ead6b2018-04-17 17:33:16 -07001318 return __ip6_ins_rt(rt, &info, NULL);
Thomas Graf40e22e82006-08-22 00:00:45 -07001319}
1320
David Ahern85bd05d2019-04-16 14:36:01 -07001321static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001322 const struct in6_addr *daddr,
1323 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
David Ahern85bd05d2019-04-16 14:36:01 -07001325 struct fib6_info *f6i = res->f6i;
David Ahern4832c302017-08-17 12:17:20 -07001326 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 struct rt6_info *rt;
1328
1329 /*
1330 * Clone the route.
1331 */
1332
David Ahern85bd05d2019-04-16 14:36:01 -07001333 if (!fib6_info_hold_safe(f6i))
Wei Wange873e4b2018-07-21 20:56:32 -07001334 return NULL;
1335
David Ahern0d161582019-04-16 14:36:04 -07001336 dev = ip6_rt_get_dev_rcu(res);
David Ahern93531c62018-04-17 17:33:25 -07001337 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
Wei Wange873e4b2018-07-21 20:56:32 -07001338 if (!rt) {
David Ahern85bd05d2019-04-16 14:36:01 -07001339 fib6_info_release(f6i);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001340 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001341 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001342
David Ahern0d161582019-04-16 14:36:04 -07001343 ip6_rt_copy_init(rt, res);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001344 rt->rt6i_flags |= RTF_CACHE;
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001345 rt->dst.flags |= DST_HOST;
1346 rt->rt6i_dst.addr = *daddr;
1347 rt->rt6i_dst.plen = 128;
1348
David Ahern85bd05d2019-04-16 14:36:01 -07001349 if (!rt6_is_gw_or_nonexthop(res)) {
1350 if (f6i->fib6_dst.plen != 128 &&
1351 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001352 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001354 if (rt->rt6i_src.plen && saddr) {
1355 rt->rt6i_src.addr = *saddr;
1356 rt->rt6i_src.plen = 128;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001357 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001358#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001361 return rt;
1362}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
David Aherndb3fede2019-04-16 14:36:03 -07001364static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001365{
David Aherndb3fede2019-04-16 14:36:03 -07001366 struct fib6_info *f6i = res->f6i;
1367 unsigned short flags = fib6_info_dst_flags(f6i);
David Ahern4832c302017-08-17 12:17:20 -07001368 struct net_device *dev;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001369 struct rt6_info *pcpu_rt;
1370
David Aherndb3fede2019-04-16 14:36:03 -07001371 if (!fib6_info_hold_safe(f6i))
Wei Wange873e4b2018-07-21 20:56:32 -07001372 return NULL;
1373
David Ahern4832c302017-08-17 12:17:20 -07001374 rcu_read_lock();
David Ahern0d161582019-04-16 14:36:04 -07001375 dev = ip6_rt_get_dev_rcu(res);
David Ahern93531c62018-04-17 17:33:25 -07001376 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
David Ahern4832c302017-08-17 12:17:20 -07001377 rcu_read_unlock();
Wei Wange873e4b2018-07-21 20:56:32 -07001378 if (!pcpu_rt) {
David Aherndb3fede2019-04-16 14:36:03 -07001379 fib6_info_release(f6i);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001380 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001381 }
David Ahern0d161582019-04-16 14:36:04 -07001382 ip6_rt_copy_init(pcpu_rt, res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001383 pcpu_rt->rt6i_flags |= RTF_PCPU;
1384 return pcpu_rt;
1385}
1386
Wei Wang66f5d6c2017-10-06 12:06:10 -07001387/* It should be called with rcu_read_lock() acquired */
David Aherndb3fede2019-04-16 14:36:03 -07001388static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001389{
Eric Dumazetc3530712019-05-31 18:11:25 -07001390 struct rt6_info *pcpu_rt;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001391
Eric Dumazetc3530712019-05-31 18:11:25 -07001392 pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001393
David Ahernd4ead6b2018-04-17 17:33:16 -07001394 if (pcpu_rt)
David Ahern10585b42019-03-20 09:24:50 -07001395 ip6_hold_safe(NULL, &pcpu_rt);
Wei Wangd3843fe2017-10-06 12:06:06 -07001396
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001397 return pcpu_rt;
1398}
1399
David Ahernafb1d4b52018-04-17 17:33:11 -07001400static struct rt6_info *rt6_make_pcpu_route(struct net *net,
David Aherndb3fede2019-04-16 14:36:03 -07001401 const struct fib6_result *res)
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001402{
1403 struct rt6_info *pcpu_rt, *prev, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001404
David Aherndb3fede2019-04-16 14:36:03 -07001405 pcpu_rt = ip6_rt_pcpu_alloc(res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001406 if (!pcpu_rt) {
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001407 dst_hold(&net->ipv6.ip6_null_entry->dst);
1408 return net->ipv6.ip6_null_entry;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001409 }
1410
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001411 dst_hold(&pcpu_rt->dst);
David Ahernf40b6ae2019-05-22 20:27:55 -07001412 p = this_cpu_ptr(res->nh->rt6i_pcpu);
Wei Wanga94b9362017-10-06 12:06:04 -07001413 prev = cmpxchg(p, NULL, pcpu_rt);
Eric Dumazet951f7882017-10-08 21:07:18 -07001414 BUG_ON(prev);
Wei Wanga94b9362017-10-06 12:06:04 -07001415
Eric Dumazet61fb0d02019-05-15 19:39:52 -07001416 if (res->f6i->fib6_destroying) {
1417 struct fib6_info *from;
1418
1419 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1420 fib6_info_release(from);
1421 }
1422
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001423 return pcpu_rt;
1424}
1425
Wei Wang35732d02017-10-06 12:05:57 -07001426/* exception hash table implementation
1427 */
1428static DEFINE_SPINLOCK(rt6_exception_lock);
1429
1430/* Remove rt6_ex from hash table and free the memory
1431 * Caller must hold rt6_exception_lock
1432 */
1433static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1434 struct rt6_exception *rt6_ex)
1435{
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001436 struct fib6_info *from;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001437 struct net *net;
Wei Wang81eb8442017-10-06 12:06:11 -07001438
Wei Wang35732d02017-10-06 12:05:57 -07001439 if (!bucket || !rt6_ex)
1440 return;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001441
1442 net = dev_net(rt6_ex->rt6i->dst.dev);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001443 net->ipv6.rt6_stats->fib_rt_cache--;
1444
1445 /* purge completely the exception to allow releasing the held resources:
1446 * some [sk] cache may keep the dst around for unlimited time
1447 */
Eric Dumazet0e233872019-04-28 12:22:25 -07001448 from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001449 fib6_info_release(from);
1450 dst_dev_put(&rt6_ex->rt6i->dst);
1451
Wei Wang35732d02017-10-06 12:05:57 -07001452 hlist_del_rcu(&rt6_ex->hlist);
David Ahern77634cc2018-04-17 17:33:27 -07001453 dst_release(&rt6_ex->rt6i->dst);
Wei Wang35732d02017-10-06 12:05:57 -07001454 kfree_rcu(rt6_ex, rcu);
1455 WARN_ON_ONCE(!bucket->depth);
1456 bucket->depth--;
1457}
1458
1459/* Remove oldest rt6_ex in bucket and free the memory
1460 * Caller must hold rt6_exception_lock
1461 */
1462static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1463{
1464 struct rt6_exception *rt6_ex, *oldest = NULL;
1465
1466 if (!bucket)
1467 return;
1468
1469 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1470 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1471 oldest = rt6_ex;
1472 }
1473 rt6_remove_exception(bucket, oldest);
1474}
1475
1476static u32 rt6_exception_hash(const struct in6_addr *dst,
1477 const struct in6_addr *src)
1478{
1479 static u32 seed __read_mostly;
1480 u32 val;
1481
1482 net_get_random_once(&seed, sizeof(seed));
1483 val = jhash(dst, sizeof(*dst), seed);
1484
1485#ifdef CONFIG_IPV6_SUBTREES
1486 if (src)
1487 val = jhash(src, sizeof(*src), val);
1488#endif
1489 return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1490}
1491
1492/* Helper function to find the cached rt in the hash table
1493 * and update bucket pointer to point to the bucket for this
1494 * (daddr, saddr) pair
1495 * Caller must hold rt6_exception_lock
1496 */
1497static struct rt6_exception *
1498__rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1499 const struct in6_addr *daddr,
1500 const struct in6_addr *saddr)
1501{
1502 struct rt6_exception *rt6_ex;
1503 u32 hval;
1504
1505 if (!(*bucket) || !daddr)
1506 return NULL;
1507
1508 hval = rt6_exception_hash(daddr, saddr);
1509 *bucket += hval;
1510
1511 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1512 struct rt6_info *rt6 = rt6_ex->rt6i;
1513 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1514
1515#ifdef CONFIG_IPV6_SUBTREES
1516 if (matched && saddr)
1517 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1518#endif
1519 if (matched)
1520 return rt6_ex;
1521 }
1522 return NULL;
1523}
1524
1525/* Helper function to find the cached rt in the hash table
1526 * and update bucket pointer to point to the bucket for this
1527 * (daddr, saddr) pair
1528 * Caller must hold rcu_read_lock()
1529 */
1530static struct rt6_exception *
1531__rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1532 const struct in6_addr *daddr,
1533 const struct in6_addr *saddr)
1534{
1535 struct rt6_exception *rt6_ex;
1536 u32 hval;
1537
1538 WARN_ON_ONCE(!rcu_read_lock_held());
1539
1540 if (!(*bucket) || !daddr)
1541 return NULL;
1542
1543 hval = rt6_exception_hash(daddr, saddr);
1544 *bucket += hval;
1545
1546 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1547 struct rt6_info *rt6 = rt6_ex->rt6i;
1548 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1549
1550#ifdef CONFIG_IPV6_SUBTREES
1551 if (matched && saddr)
1552 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1553#endif
1554 if (matched)
1555 return rt6_ex;
1556 }
1557 return NULL;
1558}
1559
David Ahernb748f262019-04-16 14:36:06 -07001560static unsigned int fib6_mtu(const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001561{
David Ahernb748f262019-04-16 14:36:06 -07001562 const struct fib6_nh *nh = res->nh;
David Ahernd4ead6b2018-04-17 17:33:16 -07001563 unsigned int mtu;
1564
David Ahernb748f262019-04-16 14:36:06 -07001565 if (res->f6i->fib6_pmtu) {
1566 mtu = res->f6i->fib6_pmtu;
David Aherndcd1f572018-04-18 15:39:05 -07001567 } else {
David Ahernb748f262019-04-16 14:36:06 -07001568 struct net_device *dev = nh->fib_nh_dev;
David Aherndcd1f572018-04-18 15:39:05 -07001569 struct inet6_dev *idev;
1570
1571 rcu_read_lock();
1572 idev = __in6_dev_get(dev);
1573 mtu = idev->cnf.mtu6;
1574 rcu_read_unlock();
1575 }
1576
David Ahernd4ead6b2018-04-17 17:33:16 -07001577 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1578
David Ahernb748f262019-04-16 14:36:06 -07001579 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahernd4ead6b2018-04-17 17:33:16 -07001580}
1581
David Aherncc5c0732019-05-22 20:27:58 -07001582#define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1583
1584/* used when the flushed bit is not relevant, only access to the bucket
1585 * (ie., all bucket users except rt6_insert_exception);
1586 *
1587 * called under rcu lock; sometimes called with rt6_exception_lock held
1588 */
1589static
1590struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1591 spinlock_t *lock)
1592{
1593 struct rt6_exception_bucket *bucket;
1594
1595 if (lock)
1596 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1597 lockdep_is_held(lock));
1598 else
1599 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1600
1601 /* remove bucket flushed bit if set */
1602 if (bucket) {
1603 unsigned long p = (unsigned long)bucket;
1604
1605 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1606 bucket = (struct rt6_exception_bucket *)p;
1607 }
1608
1609 return bucket;
1610}
1611
1612static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1613{
1614 unsigned long p = (unsigned long)bucket;
1615
1616 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1617}
1618
1619/* called with rt6_exception_lock held */
1620static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1621 spinlock_t *lock)
1622{
1623 struct rt6_exception_bucket *bucket;
1624 unsigned long p;
1625
1626 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1627 lockdep_is_held(lock));
1628
1629 p = (unsigned long)bucket;
1630 p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1631 bucket = (struct rt6_exception_bucket *)p;
1632 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1633}
1634
Wei Wang35732d02017-10-06 12:05:57 -07001635static int rt6_insert_exception(struct rt6_info *nrt,
David Ahern5012f0a2019-04-16 14:36:05 -07001636 const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001637{
David Ahern5e670d82018-04-17 17:33:14 -07001638 struct net *net = dev_net(nrt->dst.dev);
Wei Wang35732d02017-10-06 12:05:57 -07001639 struct rt6_exception_bucket *bucket;
David Aherncc5c0732019-05-22 20:27:58 -07001640 struct fib6_info *f6i = res->f6i;
Wei Wang35732d02017-10-06 12:05:57 -07001641 struct in6_addr *src_key = NULL;
1642 struct rt6_exception *rt6_ex;
David Aherncc5c0732019-05-22 20:27:58 -07001643 struct fib6_nh *nh = res->nh;
Wei Wang35732d02017-10-06 12:05:57 -07001644 int err = 0;
1645
Wei Wang35732d02017-10-06 12:05:57 -07001646 spin_lock_bh(&rt6_exception_lock);
1647
David Aherncc5c0732019-05-22 20:27:58 -07001648 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1649 lockdep_is_held(&rt6_exception_lock));
Wei Wang35732d02017-10-06 12:05:57 -07001650 if (!bucket) {
1651 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1652 GFP_ATOMIC);
1653 if (!bucket) {
1654 err = -ENOMEM;
1655 goto out;
1656 }
David Aherncc5c0732019-05-22 20:27:58 -07001657 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1658 } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1659 err = -EINVAL;
1660 goto out;
Wei Wang35732d02017-10-06 12:05:57 -07001661 }
1662
1663#ifdef CONFIG_IPV6_SUBTREES
David Ahern5012f0a2019-04-16 14:36:05 -07001664 /* fib6_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001665 * and exception table is indexed by a hash of
David Ahern5012f0a2019-04-16 14:36:05 -07001666 * both fib6_dst and fib6_src.
Wei Wang35732d02017-10-06 12:05:57 -07001667 * Otherwise, the exception table is indexed by
David Ahern5012f0a2019-04-16 14:36:05 -07001668 * a hash of only fib6_dst.
Wei Wang35732d02017-10-06 12:05:57 -07001669 */
David Ahern5012f0a2019-04-16 14:36:05 -07001670 if (f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001671 src_key = &nrt->rt6i_src.addr;
1672#endif
David Ahern5012f0a2019-04-16 14:36:05 -07001673 /* rt6_mtu_change() might lower mtu on f6i.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001674 * Only insert this exception route if its mtu
David Ahern5012f0a2019-04-16 14:36:05 -07001675 * is less than f6i's mtu value.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001676 */
David Ahernb748f262019-04-16 14:36:06 -07001677 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001678 err = -EINVAL;
1679 goto out;
1680 }
Wei Wang60006a42017-10-06 12:05:58 -07001681
Wei Wang35732d02017-10-06 12:05:57 -07001682 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1683 src_key);
1684 if (rt6_ex)
1685 rt6_remove_exception(bucket, rt6_ex);
1686
1687 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1688 if (!rt6_ex) {
1689 err = -ENOMEM;
1690 goto out;
1691 }
1692 rt6_ex->rt6i = nrt;
1693 rt6_ex->stamp = jiffies;
Wei Wang35732d02017-10-06 12:05:57 -07001694 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1695 bucket->depth++;
Wei Wang81eb8442017-10-06 12:06:11 -07001696 net->ipv6.rt6_stats->fib_rt_cache++;
Wei Wang35732d02017-10-06 12:05:57 -07001697
1698 if (bucket->depth > FIB6_MAX_DEPTH)
1699 rt6_exception_remove_oldest(bucket);
1700
1701out:
1702 spin_unlock_bh(&rt6_exception_lock);
1703
1704 /* Update fn->fn_sernum to invalidate all cached dst */
Paolo Abenib886d5f2017-10-19 16:07:10 +02001705 if (!err) {
David Ahern5012f0a2019-04-16 14:36:05 -07001706 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1707 fib6_update_sernum(net, f6i);
1708 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
Paolo Abenib886d5f2017-10-19 16:07:10 +02001709 fib6_force_start_gc(net);
1710 }
Wei Wang35732d02017-10-06 12:05:57 -07001711
1712 return err;
1713}
1714
David Ahernc0b220c2019-05-22 20:27:57 -07001715static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
Wei Wang35732d02017-10-06 12:05:57 -07001716{
1717 struct rt6_exception_bucket *bucket;
1718 struct rt6_exception *rt6_ex;
1719 struct hlist_node *tmp;
1720 int i;
1721
1722 spin_lock_bh(&rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001723
David Aherncc5c0732019-05-22 20:27:58 -07001724 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001725 if (!bucket)
1726 goto out;
1727
David Aherncc5c0732019-05-22 20:27:58 -07001728 /* Prevent rt6_insert_exception() to recreate the bucket list */
1729 if (!from)
1730 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1731
Wei Wang35732d02017-10-06 12:05:57 -07001732 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
David Aherncc5c0732019-05-22 20:27:58 -07001733 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1734 if (!from ||
1735 rcu_access_pointer(rt6_ex->rt6i->from) == from)
1736 rt6_remove_exception(bucket, rt6_ex);
1737 }
1738 WARN_ON_ONCE(!from && bucket->depth);
Wei Wang35732d02017-10-06 12:05:57 -07001739 bucket++;
1740 }
Wei Wang35732d02017-10-06 12:05:57 -07001741out:
1742 spin_unlock_bh(&rt6_exception_lock);
1743}
1744
David Aherne659ba32019-06-08 14:53:28 -07001745static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1746{
1747 struct fib6_info *f6i = arg;
1748
1749 fib6_nh_flush_exceptions(nh, f6i);
1750
1751 return 0;
1752}
1753
David Ahernc0b220c2019-05-22 20:27:57 -07001754void rt6_flush_exceptions(struct fib6_info *f6i)
1755{
David Aherne659ba32019-06-08 14:53:28 -07001756 if (f6i->nh)
1757 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1758 f6i);
1759 else
1760 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
David Ahernc0b220c2019-05-22 20:27:57 -07001761}
1762
Wei Wang35732d02017-10-06 12:05:57 -07001763/* Find cached rt in the hash table inside passed in rt
1764 * Caller has to hold rcu_read_lock()
1765 */
David Ahern7e4b5122019-04-16 14:36:00 -07001766static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
Wei Wang510e2ce2019-05-16 13:30:54 -07001767 const struct in6_addr *daddr,
1768 const struct in6_addr *saddr)
Wei Wang35732d02017-10-06 12:05:57 -07001769{
Wei Wang510e2ce2019-05-16 13:30:54 -07001770 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001771 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001772 struct rt6_exception *rt6_ex;
David Ahern7e4b5122019-04-16 14:36:00 -07001773 struct rt6_info *ret = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001774
Wei Wang35732d02017-10-06 12:05:57 -07001775#ifdef CONFIG_IPV6_SUBTREES
David Ahern7e4b5122019-04-16 14:36:00 -07001776 /* fib6i_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001777 * and exception table is indexed by a hash of
David Ahern7e4b5122019-04-16 14:36:00 -07001778 * both fib6_dst and fib6_src.
Wei Wang510e2ce2019-05-16 13:30:54 -07001779 * However, the src addr used to create the hash
1780 * might not be exactly the passed in saddr which
1781 * is a /128 addr from the flow.
1782 * So we need to use f6i->fib6_src to redo lookup
1783 * if the passed in saddr does not find anything.
1784 * (See the logic in ip6_rt_cache_alloc() on how
1785 * rt->rt6i_src is updated.)
Wei Wang35732d02017-10-06 12:05:57 -07001786 */
David Ahern7e4b5122019-04-16 14:36:00 -07001787 if (res->f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001788 src_key = saddr;
Wei Wang510e2ce2019-05-16 13:30:54 -07001789find_ex:
Wei Wang35732d02017-10-06 12:05:57 -07001790#endif
David Aherncc5c0732019-05-22 20:27:58 -07001791 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001792 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1793
1794 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
David Ahern7e4b5122019-04-16 14:36:00 -07001795 ret = rt6_ex->rt6i;
Wei Wang35732d02017-10-06 12:05:57 -07001796
Wei Wang510e2ce2019-05-16 13:30:54 -07001797#ifdef CONFIG_IPV6_SUBTREES
1798 /* Use fib6_src as src_key and redo lookup */
1799 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1800 src_key = &res->f6i->fib6_src.addr;
1801 goto find_ex;
1802 }
1803#endif
1804
David Ahern7e4b5122019-04-16 14:36:00 -07001805 return ret;
Wei Wang35732d02017-10-06 12:05:57 -07001806}
1807
1808/* Remove the passed in cached rt from the hash table that contains it */
David Aherncc5c0732019-05-22 20:27:58 -07001809static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001810 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001811{
David Ahernc0b220c2019-05-22 20:27:57 -07001812 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001813 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001814 struct rt6_exception *rt6_ex;
1815 int err;
1816
David Aherncc5c0732019-05-22 20:27:58 -07001817 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wang35732d02017-10-06 12:05:57 -07001818 return -ENOENT;
1819
1820 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07001821 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1822
Wei Wang35732d02017-10-06 12:05:57 -07001823#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001824 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1825 * and exception table is indexed by a hash of
1826 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001827 * Otherwise, the exception table is indexed by
1828 * a hash of only rt6i_dst.
1829 */
David Ahernc0b220c2019-05-22 20:27:57 -07001830 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001831 src_key = &rt->rt6i_src.addr;
1832#endif
1833 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1834 &rt->rt6i_dst.addr,
1835 src_key);
1836 if (rt6_ex) {
1837 rt6_remove_exception(bucket, rt6_ex);
1838 err = 0;
1839 } else {
1840 err = -ENOENT;
1841 }
1842
1843 spin_unlock_bh(&rt6_exception_lock);
1844 return err;
1845}
1846
David Aherne659ba32019-06-08 14:53:28 -07001847struct fib6_nh_excptn_arg {
1848 struct rt6_info *rt;
1849 int plen;
1850};
1851
1852static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1853{
1854 struct fib6_nh_excptn_arg *arg = _arg;
1855 int err;
1856
1857 err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1858 if (err == 0)
1859 return 1;
1860
1861 return 0;
1862}
1863
David Ahernc0b220c2019-05-22 20:27:57 -07001864static int rt6_remove_exception_rt(struct rt6_info *rt)
1865{
1866 struct fib6_info *from;
1867
1868 from = rcu_dereference(rt->from);
David Aherncc5c0732019-05-22 20:27:58 -07001869 if (!from || !(rt->rt6i_flags & RTF_CACHE))
David Ahernc0b220c2019-05-22 20:27:57 -07001870 return -EINVAL;
1871
David Aherne659ba32019-06-08 14:53:28 -07001872 if (from->nh) {
1873 struct fib6_nh_excptn_arg arg = {
1874 .rt = rt,
1875 .plen = from->fib6_src.plen
1876 };
1877 int rc;
1878
1879 /* rc = 1 means an entry was found */
1880 rc = nexthop_for_each_fib6_nh(from->nh,
1881 rt6_nh_remove_exception_rt,
1882 &arg);
1883 return rc ? 0 : -ENOENT;
1884 }
1885
David Ahern1cf844c2019-05-22 20:27:59 -07001886 return fib6_nh_remove_exception(from->fib6_nh,
David Aherncc5c0732019-05-22 20:27:58 -07001887 from->fib6_src.plen, rt);
David Ahernc0b220c2019-05-22 20:27:57 -07001888}
1889
Wei Wang35732d02017-10-06 12:05:57 -07001890/* Find rt6_ex which contains the passed in rt cache and
1891 * refresh its stamp
1892 */
David Aherncc5c0732019-05-22 20:27:58 -07001893static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001894 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001895{
David Ahernc0b220c2019-05-22 20:27:57 -07001896 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001897 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001898 struct rt6_exception *rt6_ex;
Paolo Abeni193f3682019-02-21 11:19:41 +01001899
David Aherncc5c0732019-05-22 20:27:58 -07001900 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001901#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001902 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1903 * and exception table is indexed by a hash of
1904 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001905 * Otherwise, the exception table is indexed by
1906 * a hash of only rt6i_dst.
1907 */
David Ahernc0b220c2019-05-22 20:27:57 -07001908 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001909 src_key = &rt->rt6i_src.addr;
1910#endif
David Aherncc5c0732019-05-22 20:27:58 -07001911 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
Wei Wang35732d02017-10-06 12:05:57 -07001912 if (rt6_ex)
1913 rt6_ex->stamp = jiffies;
David Ahernc0b220c2019-05-22 20:27:57 -07001914}
Wei Wang35732d02017-10-06 12:05:57 -07001915
David Aherne659ba32019-06-08 14:53:28 -07001916struct fib6_nh_match_arg {
1917 const struct net_device *dev;
1918 const struct in6_addr *gw;
1919 struct fib6_nh *match;
1920};
1921
1922/* determine if fib6_nh has given device and gateway */
1923static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1924{
1925 struct fib6_nh_match_arg *arg = _arg;
1926
1927 if (arg->dev != nh->fib_nh_dev ||
1928 (arg->gw && !nh->fib_nh_gw_family) ||
1929 (!arg->gw && nh->fib_nh_gw_family) ||
1930 (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1931 return 0;
1932
1933 arg->match = nh;
1934
1935 /* found a match, break the loop */
1936 return 1;
1937}
1938
David Ahernc0b220c2019-05-22 20:27:57 -07001939static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1940{
1941 struct fib6_info *from;
David Aherne659ba32019-06-08 14:53:28 -07001942 struct fib6_nh *fib6_nh;
David Ahernc0b220c2019-05-22 20:27:57 -07001943
1944 rcu_read_lock();
1945
1946 from = rcu_dereference(rt->from);
1947 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1948 goto unlock;
1949
David Aherne659ba32019-06-08 14:53:28 -07001950 if (from->nh) {
1951 struct fib6_nh_match_arg arg = {
1952 .dev = rt->dst.dev,
1953 .gw = &rt->rt6i_gateway,
1954 };
1955
1956 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1957
1958 if (!arg.match)
1959 return;
1960 fib6_nh = arg.match;
1961 } else {
1962 fib6_nh = from->fib6_nh;
1963 }
1964 fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
Paolo Abeni193f3682019-02-21 11:19:41 +01001965unlock:
Wei Wang35732d02017-10-06 12:05:57 -07001966 rcu_read_unlock();
1967}
1968
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001969static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1970 struct rt6_info *rt, int mtu)
1971{
1972 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1973 * lowest MTU in the path: always allow updating the route PMTU to
1974 * reflect PMTU decreases.
1975 *
1976 * If the new MTU is higher, and the route PMTU is equal to the local
1977 * MTU, this means the old MTU is the lowest in the path, so allow
1978 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1979 * handle this.
1980 */
1981
1982 if (dst_mtu(&rt->dst) >= mtu)
1983 return true;
1984
1985 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1986 return true;
1987
1988 return false;
1989}
1990
1991static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
David Aherncc5c0732019-05-22 20:27:58 -07001992 const struct fib6_nh *nh, int mtu)
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001993{
1994 struct rt6_exception_bucket *bucket;
1995 struct rt6_exception *rt6_ex;
1996 int i;
1997
David Aherncc5c0732019-05-22 20:27:58 -07001998 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001999 if (!bucket)
2000 return;
2001
2002 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2003 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2004 struct rt6_info *entry = rt6_ex->rt6i;
2005
2006 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
David Ahernd4ead6b2018-04-17 17:33:16 -07002007 * route), the metrics of its rt->from have already
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002008 * been updated.
2009 */
David Ahernd4ead6b2018-04-17 17:33:16 -07002010 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002011 rt6_mtu_change_route_allowed(idev, entry, mtu))
David Ahernd4ead6b2018-04-17 17:33:16 -07002012 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002013 }
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002014 bucket++;
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002015 }
2016}
2017
Wei Wangb16cb452017-10-06 12:06:00 -07002018#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2019
David Aherncc5c0732019-05-22 20:27:58 -07002020static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2021 const struct in6_addr *gateway)
Wei Wangb16cb452017-10-06 12:06:00 -07002022{
2023 struct rt6_exception_bucket *bucket;
2024 struct rt6_exception *rt6_ex;
2025 struct hlist_node *tmp;
2026 int i;
2027
David Aherncc5c0732019-05-22 20:27:58 -07002028 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangb16cb452017-10-06 12:06:00 -07002029 return;
2030
2031 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07002032 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangb16cb452017-10-06 12:06:00 -07002033 if (bucket) {
2034 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2035 hlist_for_each_entry_safe(rt6_ex, tmp,
2036 &bucket->chain, hlist) {
2037 struct rt6_info *entry = rt6_ex->rt6i;
2038
2039 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2040 RTF_CACHE_GATEWAY &&
2041 ipv6_addr_equal(gateway,
2042 &entry->rt6i_gateway)) {
2043 rt6_remove_exception(bucket, rt6_ex);
2044 }
2045 }
2046 bucket++;
2047 }
2048 }
2049
2050 spin_unlock_bh(&rt6_exception_lock);
2051}
2052
Wei Wangc757faa2017-10-06 12:06:01 -07002053static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2054 struct rt6_exception *rt6_ex,
2055 struct fib6_gc_args *gc_args,
2056 unsigned long now)
2057{
2058 struct rt6_info *rt = rt6_ex->rt6i;
2059
Paolo Abeni1859bac2017-10-19 16:07:11 +02002060 /* we are pruning and obsoleting aged-out and non gateway exceptions
2061 * even if others have still references to them, so that on next
2062 * dst_check() such references can be dropped.
2063 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2064 * expired, independently from their aging, as per RFC 8201 section 4
2065 */
Wei Wang31afeb42018-01-26 11:40:17 -08002066 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2067 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2068 RT6_TRACE("aging clone %p\n", rt);
2069 rt6_remove_exception(bucket, rt6_ex);
2070 return;
2071 }
2072 } else if (time_after(jiffies, rt->dst.expires)) {
2073 RT6_TRACE("purging expired route %p\n", rt);
Wei Wangc757faa2017-10-06 12:06:01 -07002074 rt6_remove_exception(bucket, rt6_ex);
2075 return;
Wei Wang31afeb42018-01-26 11:40:17 -08002076 }
2077
2078 if (rt->rt6i_flags & RTF_GATEWAY) {
Wei Wangc757faa2017-10-06 12:06:01 -07002079 struct neighbour *neigh;
2080 __u8 neigh_flags = 0;
2081
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002082 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
2083 if (neigh)
Wei Wangc757faa2017-10-06 12:06:01 -07002084 neigh_flags = neigh->flags;
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002085
Wei Wangc757faa2017-10-06 12:06:01 -07002086 if (!(neigh_flags & NTF_ROUTER)) {
2087 RT6_TRACE("purging route %p via non-router but gateway\n",
2088 rt);
2089 rt6_remove_exception(bucket, rt6_ex);
2090 return;
2091 }
2092 }
Wei Wang31afeb42018-01-26 11:40:17 -08002093
Wei Wangc757faa2017-10-06 12:06:01 -07002094 gc_args->more++;
2095}
2096
David Aherncc5c0732019-05-22 20:27:58 -07002097static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
David Ahernc0b220c2019-05-22 20:27:57 -07002098 struct fib6_gc_args *gc_args,
2099 unsigned long now)
Wei Wangc757faa2017-10-06 12:06:01 -07002100{
2101 struct rt6_exception_bucket *bucket;
2102 struct rt6_exception *rt6_ex;
2103 struct hlist_node *tmp;
2104 int i;
2105
David Aherncc5c0732019-05-22 20:27:58 -07002106 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangc757faa2017-10-06 12:06:01 -07002107 return;
2108
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002109 rcu_read_lock_bh();
2110 spin_lock(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07002111 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangc757faa2017-10-06 12:06:01 -07002112 if (bucket) {
2113 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2114 hlist_for_each_entry_safe(rt6_ex, tmp,
2115 &bucket->chain, hlist) {
2116 rt6_age_examine_exception(bucket, rt6_ex,
2117 gc_args, now);
2118 }
2119 bucket++;
2120 }
2121 }
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002122 spin_unlock(&rt6_exception_lock);
2123 rcu_read_unlock_bh();
Wei Wangc757faa2017-10-06 12:06:01 -07002124}
2125
David Aherne659ba32019-06-08 14:53:28 -07002126struct fib6_nh_age_excptn_arg {
2127 struct fib6_gc_args *gc_args;
2128 unsigned long now;
2129};
2130
2131static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2132{
2133 struct fib6_nh_age_excptn_arg *arg = _arg;
2134
2135 fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2136 return 0;
2137}
2138
David Aherncc5c0732019-05-22 20:27:58 -07002139void rt6_age_exceptions(struct fib6_info *f6i,
David Ahernc0b220c2019-05-22 20:27:57 -07002140 struct fib6_gc_args *gc_args,
2141 unsigned long now)
2142{
David Aherne659ba32019-06-08 14:53:28 -07002143 if (f6i->nh) {
2144 struct fib6_nh_age_excptn_arg arg = {
2145 .gc_args = gc_args,
2146 .now = now
2147 };
2148
2149 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2150 &arg);
2151 } else {
2152 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2153 }
David Ahernc0b220c2019-05-22 20:27:57 -07002154}
2155
David Ahern1d053da2018-05-09 20:34:21 -07002156/* must be called with rcu lock held */
David Aherneffda4d2019-04-16 14:36:10 -07002157int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2158 struct flowi6 *fl6, struct fib6_result *res, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002160 struct fib6_node *fn, *saved_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
David Ahern64547432018-05-09 20:34:19 -07002162 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002163 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
David Ahernca254492015-10-12 11:47:10 -07002165 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2166 oif = 0;
2167
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002168redo_rt6_select:
David Aherneffda4d2019-04-16 14:36:10 -07002169 rt6_select(net, fn, oif, res, strict);
2170 if (res->f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002171 fn = fib6_backtrack(fn, &fl6->saddr);
2172 if (fn)
2173 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002174 else if (strict & RT6_LOOKUP_F_REACHABLE) {
2175 /* also consider unreachable route */
2176 strict &= ~RT6_LOOKUP_F_REACHABLE;
2177 fn = saved_fn;
2178 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002179 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002180 }
2181
David Aherneffda4d2019-04-16 14:36:10 -07002182 trace_fib6_table_lookup(net, res, table, fl6);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -08002183
David Aherneffda4d2019-04-16 14:36:10 -07002184 return 0;
David Ahern1d053da2018-05-09 20:34:21 -07002185}
2186
2187struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2188 int oif, struct flowi6 *fl6,
2189 const struct sk_buff *skb, int flags)
2190{
David Ahernb1d40992019-04-16 14:35:59 -07002191 struct fib6_result res = {};
David Ahern1d053da2018-05-09 20:34:21 -07002192 struct rt6_info *rt;
2193 int strict = 0;
2194
2195 strict |= flags & RT6_LOOKUP_F_IFACE;
2196 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
2197 if (net->ipv6.devconf_all->forwarding == 0)
2198 strict |= RT6_LOOKUP_F_REACHABLE;
2199
2200 rcu_read_lock();
2201
David Aherneffda4d2019-04-16 14:36:10 -07002202 fib6_table_lookup(net, table, oif, fl6, &res, strict);
David Ahernb1d40992019-04-16 14:35:59 -07002203 if (res.f6i == net->ipv6.fib6_null_entry) {
David Ahern421842e2018-04-17 17:33:18 -07002204 rt = net->ipv6.ip6_null_entry;
Wei Wang66f5d6c2017-10-06 12:06:10 -07002205 rcu_read_unlock();
Wei Wangd3843fe2017-10-06 12:06:06 -07002206 dst_hold(&rt->dst);
Wei Wangd3843fe2017-10-06 12:06:06 -07002207 return rt;
David Ahern23fb93a2018-04-17 17:33:23 -07002208 }
2209
David Ahernb1d40992019-04-16 14:35:59 -07002210 fib6_select_path(net, &res, fl6, oif, false, skb, strict);
David Ahernd83009d2019-04-09 14:41:17 -07002211
David Ahern23fb93a2018-04-17 17:33:23 -07002212 /*Search through exception table */
David Ahern7e4b5122019-04-16 14:36:00 -07002213 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
David Ahern23fb93a2018-04-17 17:33:23 -07002214 if (rt) {
David Ahern10585b42019-03-20 09:24:50 -07002215 if (ip6_hold_safe(net, &rt))
Wei Wangd3843fe2017-10-06 12:06:06 -07002216 dst_use_noref(&rt->dst, jiffies);
David Ahernd4ead6b2018-04-17 17:33:16 -07002217
Wei Wang66f5d6c2017-10-06 12:06:10 -07002218 rcu_read_unlock();
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002219 return rt;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002220 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
David Ahernb1d40992019-04-16 14:35:59 -07002221 !res.nh->fib_nh_gw_family)) {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002222 /* Create a RTF_CACHE clone which will not be
2223 * owned by the fib6 tree. It is for the special case where
2224 * the daddr in the skb during the neighbor look-up is different
2225 * from the fl6->daddr used to look-up route here.
2226 */
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002227 struct rt6_info *uncached_rt;
2228
David Ahern85bd05d2019-04-16 14:36:01 -07002229 uncached_rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002230
David Ahern4d85cd02018-04-20 15:37:59 -07002231 rcu_read_unlock();
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002232
Wei Wang1cfb71e2017-06-17 10:42:33 -07002233 if (uncached_rt) {
2234 /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
2235 * No need for another dst_hold()
2236 */
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07002237 rt6_uncached_list_add(uncached_rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002238 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Wei Wang1cfb71e2017-06-17 10:42:33 -07002239 } else {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002240 uncached_rt = net->ipv6.ip6_null_entry;
Wei Wang1cfb71e2017-06-17 10:42:33 -07002241 dst_hold(&uncached_rt->dst);
2242 }
David Ahernb8115802015-11-19 12:24:22 -08002243
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002244 return uncached_rt;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002245 } else {
2246 /* Get a percpu copy */
2247
2248 struct rt6_info *pcpu_rt;
2249
Eric Dumazet951f7882017-10-08 21:07:18 -07002250 local_bh_disable();
David Aherndb3fede2019-04-16 14:36:03 -07002251 pcpu_rt = rt6_get_pcpu_route(&res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002252
David Ahern93531c62018-04-17 17:33:25 -07002253 if (!pcpu_rt)
David Aherndb3fede2019-04-16 14:36:03 -07002254 pcpu_rt = rt6_make_pcpu_route(net, &res);
David Ahern93531c62018-04-17 17:33:25 -07002255
Eric Dumazet951f7882017-10-08 21:07:18 -07002256 local_bh_enable();
2257 rcu_read_unlock();
David Ahernd4bea422018-05-09 20:34:24 -07002258
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002259 return pcpu_rt;
2260 }
Thomas Grafc71099a2006-08-04 23:20:06 -07002261}
David Ahern9ff74382016-06-13 13:44:19 -07002262EXPORT_SYMBOL_GPL(ip6_pol_route);
Thomas Grafc71099a2006-08-04 23:20:06 -07002263
David Ahernb75cc8f2018-03-02 08:32:17 -08002264static struct rt6_info *ip6_pol_route_input(struct net *net,
2265 struct fib6_table *table,
2266 struct flowi6 *fl6,
2267 const struct sk_buff *skb,
2268 int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002269{
David Ahernb75cc8f2018-03-02 08:32:17 -08002270 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002271}
2272
Mahesh Bandeward409b842016-09-16 12:59:08 -07002273struct dst_entry *ip6_route_input_lookup(struct net *net,
2274 struct net_device *dev,
David Ahernb75cc8f2018-03-02 08:32:17 -08002275 struct flowi6 *fl6,
2276 const struct sk_buff *skb,
2277 int flags)
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002278{
2279 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2280 flags |= RT6_LOOKUP_F_IFACE;
2281
David Ahernb75cc8f2018-03-02 08:32:17 -08002282 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002283}
Mahesh Bandeward409b842016-09-16 12:59:08 -07002284EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002285
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002286static void ip6_multipath_l3_keys(const struct sk_buff *skb,
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002287 struct flow_keys *keys,
2288 struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002289{
2290 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2291 const struct ipv6hdr *key_iph = outer_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002292 struct flow_keys *_flkeys = flkeys;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002293 const struct ipv6hdr *inner_iph;
2294 const struct icmp6hdr *icmph;
2295 struct ipv6hdr _inner_iph;
Eric Dumazetcea67a22018-04-29 09:54:59 -07002296 struct icmp6hdr _icmph;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002297
2298 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2299 goto out;
2300
Eric Dumazetcea67a22018-04-29 09:54:59 -07002301 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2302 sizeof(_icmph), &_icmph);
2303 if (!icmph)
2304 goto out;
2305
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002306 if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
2307 icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
2308 icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
2309 icmph->icmp6_type != ICMPV6_PARAMPROB)
2310 goto out;
2311
2312 inner_iph = skb_header_pointer(skb,
2313 skb_transport_offset(skb) + sizeof(*icmph),
2314 sizeof(_inner_iph), &_inner_iph);
2315 if (!inner_iph)
2316 goto out;
2317
2318 key_iph = inner_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002319 _flkeys = NULL;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002320out:
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002321 if (_flkeys) {
2322 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2323 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2324 keys->tags.flow_label = _flkeys->tags.flow_label;
2325 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2326 } else {
2327 keys->addrs.v6addrs.src = key_iph->saddr;
2328 keys->addrs.v6addrs.dst = key_iph->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002329 keys->tags.flow_label = ip6_flowlabel(key_iph);
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002330 keys->basic.ip_proto = key_iph->nexthdr;
2331 }
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002332}
2333
2334/* if skb is set it will be used and fl6 can be NULL */
David Ahernb4bac172018-03-02 08:32:18 -08002335u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2336 const struct sk_buff *skb, struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002337{
2338 struct flow_keys hash_keys;
David Ahern9a2a5372018-03-02 08:32:15 -08002339 u32 mhash;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002340
David S. Millerbbfa0472018-03-12 11:09:33 -04002341 switch (ip6_multipath_hash_policy(net)) {
David Ahernb4bac172018-03-02 08:32:18 -08002342 case 0:
2343 memset(&hash_keys, 0, sizeof(hash_keys));
2344 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2345 if (skb) {
2346 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2347 } else {
2348 hash_keys.addrs.v6addrs.src = fl6->saddr;
2349 hash_keys.addrs.v6addrs.dst = fl6->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002350 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
David Ahernb4bac172018-03-02 08:32:18 -08002351 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2352 }
2353 break;
2354 case 1:
2355 if (skb) {
2356 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2357 struct flow_keys keys;
2358
2359 /* short-circuit if we already have L4 hash present */
2360 if (skb->l4_hash)
2361 return skb_get_hash_raw(skb) >> 1;
2362
2363 memset(&hash_keys, 0, sizeof(hash_keys));
2364
2365 if (!flkeys) {
2366 skb_flow_dissect_flow_keys(skb, &keys, flag);
2367 flkeys = &keys;
2368 }
2369 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2370 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2371 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2372 hash_keys.ports.src = flkeys->ports.src;
2373 hash_keys.ports.dst = flkeys->ports.dst;
2374 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2375 } else {
2376 memset(&hash_keys, 0, sizeof(hash_keys));
2377 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2378 hash_keys.addrs.v6addrs.src = fl6->saddr;
2379 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2380 hash_keys.ports.src = fl6->fl6_sport;
2381 hash_keys.ports.dst = fl6->fl6_dport;
2382 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2383 }
2384 break;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002385 }
David Ahern9a2a5372018-03-02 08:32:15 -08002386 mhash = flow_hash_from_keys(&hash_keys);
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002387
David Ahern9a2a5372018-03-02 08:32:15 -08002388 return mhash >> 1;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002389}
2390
Thomas Grafc71099a2006-08-04 23:20:06 -07002391void ip6_route_input(struct sk_buff *skb)
2392{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002393 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002394 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002395 int flags = RT6_LOOKUP_F_HAS_SADDR;
Jiri Benc904af042015-08-20 13:56:31 +02002396 struct ip_tunnel_info *tun_info;
David S. Miller4c9483b2011-03-12 16:22:43 -05002397 struct flowi6 fl6 = {
David Aherne0d56fd2016-09-10 12:09:57 -07002398 .flowi6_iif = skb->dev->ifindex,
David S. Miller4c9483b2011-03-12 16:22:43 -05002399 .daddr = iph->daddr,
2400 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002401 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05002402 .flowi6_mark = skb->mark,
2403 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07002404 };
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002405 struct flow_keys *flkeys = NULL, _flkeys;
Thomas Grafadaa70b2006-10-13 15:01:03 -07002406
Jiri Benc904af042015-08-20 13:56:31 +02002407 tun_info = skb_tunnel_info(skb);
Jiri Benc46fa0622015-08-28 20:48:19 +02002408 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
Jiri Benc904af042015-08-20 13:56:31 +02002409 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002410
2411 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2412 flkeys = &_flkeys;
2413
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002414 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
David Ahernb4bac172018-03-02 08:32:18 -08002415 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
Jiri Benc06e9d042015-08-20 13:56:26 +02002416 skb_dst_drop(skb);
David Ahernb75cc8f2018-03-02 08:32:17 -08002417 skb_dst_set(skb,
2418 ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07002419}
2420
David Ahernb75cc8f2018-03-02 08:32:17 -08002421static struct rt6_info *ip6_pol_route_output(struct net *net,
2422 struct fib6_table *table,
2423 struct flowi6 *fl6,
2424 const struct sk_buff *skb,
2425 int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002426{
David Ahernb75cc8f2018-03-02 08:32:17 -08002427 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07002428}
2429
Paolo Abeni6f21c962016-01-29 12:30:19 +01002430struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
2431 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002432{
David Ahernd46a9d62015-10-21 08:42:22 -07002433 bool any_src;
Thomas Grafc71099a2006-08-04 23:20:06 -07002434
Robert Shearman3ede0bb2018-09-19 13:56:53 +01002435 if (ipv6_addr_type(&fl6->daddr) &
2436 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
David Ahern4c1feac2016-09-10 12:09:56 -07002437 struct dst_entry *dst;
2438
2439 dst = l3mdev_link_scope_lookup(net, fl6);
2440 if (dst)
2441 return dst;
2442 }
David Ahernca254492015-10-12 11:47:10 -07002443
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002444 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00002445
David Ahernd46a9d62015-10-21 08:42:22 -07002446 any_src = ipv6_addr_any(&fl6->saddr);
David Ahern741a11d2015-09-28 10:12:13 -07002447 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
David Ahernd46a9d62015-10-21 08:42:22 -07002448 (fl6->flowi6_oif && any_src))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07002449 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07002450
David Ahernd46a9d62015-10-21 08:42:22 -07002451 if (!any_src)
Thomas Grafadaa70b2006-10-13 15:01:03 -07002452 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00002453 else if (sk)
2454 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002455
David Ahernb75cc8f2018-03-02 08:32:17 -08002456 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
Paolo Abeni6f21c962016-01-29 12:30:19 +01002458EXPORT_SYMBOL_GPL(ip6_route_output_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
David S. Miller2774c132011-03-01 14:59:04 -08002460struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07002461{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002462 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
Wei Wang1dbe32522017-06-17 10:42:26 -07002463 struct net_device *loopback_dev = net->loopback_dev;
David S. Miller14e50e52007-05-24 18:17:54 -07002464 struct dst_entry *new = NULL;
2465
Wei Wang1dbe32522017-06-17 10:42:26 -07002466 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
Steffen Klassert62cf27e2017-10-09 08:39:43 +02002467 DST_OBSOLETE_DEAD, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07002468 if (rt) {
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002469 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002470 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002471
Changli Gaod8d1f302010-06-10 23:31:35 -07002472 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07002473 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08002474 new->input = dst_discard;
Eric W. Biedermanede20592015-10-07 16:48:47 -05002475 new->output = dst_discard_out;
David S. Miller14e50e52007-05-24 18:17:54 -07002476
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002477 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07002478
Wei Wang1dbe32522017-06-17 10:42:26 -07002479 rt->rt6i_idev = in6_dev_get(loopback_dev);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002480 rt->rt6i_gateway = ort->rt6i_gateway;
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002481 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
David S. Miller14e50e52007-05-24 18:17:54 -07002482
2483 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2484#ifdef CONFIG_IPV6_SUBTREES
2485 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2486#endif
David S. Miller14e50e52007-05-24 18:17:54 -07002487 }
2488
David S. Miller69ead7a2011-03-01 14:45:33 -08002489 dst_release(dst_orig);
2490 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07002491}
David S. Miller14e50e52007-05-24 18:17:54 -07002492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493/*
2494 * Destination cache support functions
2495 */
2496
David Ahern8d1c8022018-04-17 17:33:26 -07002497static bool fib6_check(struct fib6_info *f6i, u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002498{
Steffen Klassert36143642017-08-25 09:05:42 +02002499 u32 rt_cookie = 0;
Wei Wangc5cff852017-08-21 09:47:10 -07002500
David Ahern8ae86972018-04-20 15:38:03 -07002501 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
David Ahern93531c62018-04-17 17:33:25 -07002502 return false;
2503
2504 if (fib6_check_expired(f6i))
2505 return false;
2506
2507 return true;
2508}
2509
David Aherna68886a2018-04-20 15:38:02 -07002510static struct dst_entry *rt6_check(struct rt6_info *rt,
2511 struct fib6_info *from,
2512 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002513{
Wei Wangc5cff852017-08-21 09:47:10 -07002514 u32 rt_cookie = 0;
2515
David Aherna68886a2018-04-20 15:38:02 -07002516 if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) ||
David Ahern93531c62018-04-17 17:33:25 -07002517 rt_cookie != cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002518 return NULL;
2519
2520 if (rt6_check_expired(rt))
2521 return NULL;
2522
2523 return &rt->dst;
2524}
2525
David Aherna68886a2018-04-20 15:38:02 -07002526static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2527 struct fib6_info *from,
2528 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002529{
Martin KaFai Lau5973fb12015-11-11 11:51:07 -08002530 if (!__rt6_check_expired(rt) &&
2531 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
David Aherna68886a2018-04-20 15:38:02 -07002532 fib6_check(from, cookie))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002533 return &rt->dst;
2534 else
2535 return NULL;
2536}
2537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2539{
David Aherna87b7dc2018-04-20 15:38:00 -07002540 struct dst_entry *dst_ret;
David Aherna68886a2018-04-20 15:38:02 -07002541 struct fib6_info *from;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 struct rt6_info *rt;
2543
David Aherna87b7dc2018-04-20 15:38:00 -07002544 rt = container_of(dst, struct rt6_info, dst);
2545
2546 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00002548 /* All IPV6 dsts are created with ->obsolete set to the value
2549 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2550 * into this function always.
2551 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02002552
David Aherna68886a2018-04-20 15:38:02 -07002553 from = rcu_dereference(rt->from);
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002554
David Aherna68886a2018-04-20 15:38:02 -07002555 if (from && (rt->rt6i_flags & RTF_PCPU ||
2556 unlikely(!list_empty(&rt->rt6i_uncached))))
2557 dst_ret = rt6_dst_from_check(rt, from, cookie);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002558 else
David Aherna68886a2018-04-20 15:38:02 -07002559 dst_ret = rt6_check(rt, from, cookie);
David Aherna87b7dc2018-04-20 15:38:00 -07002560
2561 rcu_read_unlock();
2562
2563 return dst_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564}
2565
2566static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2567{
2568 struct rt6_info *rt = (struct rt6_info *) dst;
2569
2570 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002571 if (rt->rt6i_flags & RTF_CACHE) {
David Ahernc3c14da2018-04-23 11:32:06 -07002572 rcu_read_lock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002573 if (rt6_check_expired(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07002574 rt6_remove_exception_rt(rt);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002575 dst = NULL;
2576 }
David Ahernc3c14da2018-04-23 11:32:06 -07002577 rcu_read_unlock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002578 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002580 dst = NULL;
2581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002583 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
2586static void ip6_link_failure(struct sk_buff *skb)
2587{
2588 struct rt6_info *rt;
2589
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002590 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Eric Dumazetadf30902009-06-02 05:19:30 +00002592 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (rt) {
David Ahern8a14e462018-04-23 11:32:07 -07002594 rcu_read_lock();
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002595 if (rt->rt6i_flags & RTF_CACHE) {
Xin Long761f6022018-11-14 00:48:28 +08002596 rt6_remove_exception_rt(rt);
Wei Wangc5cff852017-08-21 09:47:10 -07002597 } else {
David Aherna68886a2018-04-20 15:38:02 -07002598 struct fib6_info *from;
Wei Wangc5cff852017-08-21 09:47:10 -07002599 struct fib6_node *fn;
2600
David Aherna68886a2018-04-20 15:38:02 -07002601 from = rcu_dereference(rt->from);
2602 if (from) {
2603 fn = rcu_dereference(from->fib6_node);
2604 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2605 fn->fn_sernum = -1;
2606 }
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002607 }
David Ahern8a14e462018-04-23 11:32:07 -07002608 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
2610}
2611
David Ahern6a3e0302018-04-20 15:37:57 -07002612static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2613{
David Aherna68886a2018-04-20 15:38:02 -07002614 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2615 struct fib6_info *from;
2616
2617 rcu_read_lock();
2618 from = rcu_dereference(rt0->from);
2619 if (from)
2620 rt0->dst.expires = from->expires;
2621 rcu_read_unlock();
2622 }
David Ahern6a3e0302018-04-20 15:37:57 -07002623
2624 dst_set_expires(&rt0->dst, timeout);
2625 rt0->rt6i_flags |= RTF_EXPIRES;
2626}
2627
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002628static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2629{
2630 struct net *net = dev_net(rt->dst.dev);
2631
David Ahernd4ead6b2018-04-17 17:33:16 -07002632 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002633 rt->rt6i_flags |= RTF_MODIFIED;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002634 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2635}
2636
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002637static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2638{
2639 return !(rt->rt6i_flags & RTF_CACHE) &&
Paolo Abeni1490ed22019-02-15 18:15:37 +01002640 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002641}
2642
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002643static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2644 const struct ipv6hdr *iph, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645{
Julian Anastasov0dec8792017-02-06 23:14:16 +02002646 const struct in6_addr *daddr, *saddr;
Ian Morris67ba4152014-08-24 21:53:10 +01002647 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Xin Long19bda362016-10-28 18:18:01 +08002649 if (dst_metric_locked(dst, RTAX_MTU))
2650 return;
2651
Julian Anastasov0dec8792017-02-06 23:14:16 +02002652 if (iph) {
2653 daddr = &iph->daddr;
2654 saddr = &iph->saddr;
2655 } else if (sk) {
2656 daddr = &sk->sk_v6_daddr;
2657 saddr = &inet6_sk(sk)->saddr;
2658 } else {
2659 daddr = NULL;
2660 saddr = NULL;
2661 }
2662 dst_confirm_neigh(dst, daddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002663 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2664 if (mtu >= dst_mtu(dst))
2665 return;
David S. Miller81aded22012-06-15 14:54:11 -07002666
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002667 if (!rt6_cache_allowed_for_pmtu(rt6)) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002668 rt6_do_update_pmtu(rt6, mtu);
Wei Wang2b760fc2017-10-06 12:06:03 -07002669 /* update rt6_ex->stamp for cache */
2670 if (rt6->rt6i_flags & RTF_CACHE)
2671 rt6_update_exception_stamp_rt(rt6);
Julian Anastasov0dec8792017-02-06 23:14:16 +02002672 } else if (daddr) {
David Ahern85bd05d2019-04-16 14:36:01 -07002673 struct fib6_result res = {};
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002674 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01002675
David Ahern4d85cd02018-04-20 15:37:59 -07002676 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07002677 res.f6i = rcu_dereference(rt6->from);
2678 if (!res.f6i) {
Jonathan Lemon9c69a132019-04-14 14:21:29 -07002679 rcu_read_unlock();
2680 return;
2681 }
David Ahern1cf844c2019-05-22 20:27:59 -07002682 res.nh = res.f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -07002683 res.fib6_flags = res.f6i->fib6_flags;
2684 res.fib6_type = res.f6i->fib6_type;
2685
David Ahern85bd05d2019-04-16 14:36:01 -07002686 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002687 if (nrt6) {
2688 rt6_do_update_pmtu(nrt6, mtu);
David Ahern5012f0a2019-04-16 14:36:05 -07002689 if (rt6_insert_exception(nrt6, &res))
Wei Wang2b760fc2017-10-06 12:06:03 -07002690 dst_release_immediate(&nrt6->dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002691 }
David Aherna68886a2018-04-20 15:38:02 -07002692 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
2694}
2695
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002696static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2697 struct sk_buff *skb, u32 mtu)
2698{
2699 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
2700}
2701
David S. Miller42ae66c2012-06-15 20:01:57 -07002702void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002703 int oif, u32 mark, kuid_t uid)
David S. Miller81aded22012-06-15 14:54:11 -07002704{
2705 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2706 struct dst_entry *dst;
Maciej Żenczykowskidc920952018-09-29 23:44:51 -07002707 struct flowi6 fl6 = {
2708 .flowi6_oif = oif,
2709 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2710 .daddr = iph->daddr,
2711 .saddr = iph->saddr,
2712 .flowlabel = ip6_flowinfo(iph),
2713 .flowi6_uid = uid,
2714 };
David S. Miller81aded22012-06-15 14:54:11 -07002715
2716 dst = ip6_route_output(net, NULL, &fl6);
2717 if (!dst->error)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002718 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
David S. Miller81aded22012-06-15 14:54:11 -07002719 dst_release(dst);
2720}
2721EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2722
2723void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2724{
David Ahern7ddacfa2018-11-18 10:45:30 -08002725 int oif = sk->sk_bound_dev_if;
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002726 struct dst_entry *dst;
2727
David Ahern7ddacfa2018-11-18 10:45:30 -08002728 if (!oif && skb->dev)
2729 oif = l3mdev_master_ifindex(skb->dev);
2730
2731 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002732
2733 dst = __sk_dst_get(sk);
2734 if (!dst || !dst->obsolete ||
2735 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2736 return;
2737
2738 bh_lock_sock(sk);
2739 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2740 ip6_datagram_dst_update(sk, false);
2741 bh_unlock_sock(sk);
David S. Miller81aded22012-06-15 14:54:11 -07002742}
2743EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2744
Alexey Kodanev7d6850f2018-04-03 15:00:07 +03002745void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2746 const struct flowi6 *fl6)
2747{
2748#ifdef CONFIG_IPV6_SUBTREES
2749 struct ipv6_pinfo *np = inet6_sk(sk);
2750#endif
2751
2752 ip6_dst_store(sk, dst,
2753 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2754 &sk->sk_v6_daddr : NULL,
2755#ifdef CONFIG_IPV6_SUBTREES
2756 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2757 &np->saddr :
2758#endif
2759 NULL);
2760}
2761
David Ahern9b6b35a2019-04-16 14:36:02 -07002762static bool ip6_redirect_nh_match(const struct fib6_result *res,
David Ahern0b34eb02019-04-09 14:41:19 -07002763 struct flowi6 *fl6,
2764 const struct in6_addr *gw,
2765 struct rt6_info **ret)
2766{
David Ahern9b6b35a2019-04-16 14:36:02 -07002767 const struct fib6_nh *nh = res->nh;
2768
David Ahern0b34eb02019-04-09 14:41:19 -07002769 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
2770 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
2771 return false;
2772
2773 /* rt_cache's gateway might be different from its 'parent'
2774 * in the case of an ip redirect.
2775 * So we keep searching in the exception table if the gateway
2776 * is different.
2777 */
2778 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
2779 struct rt6_info *rt_cache;
2780
David Ahern9b6b35a2019-04-16 14:36:02 -07002781 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
David Ahern0b34eb02019-04-09 14:41:19 -07002782 if (rt_cache &&
2783 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
2784 *ret = rt_cache;
2785 return true;
2786 }
2787 return false;
2788 }
2789 return true;
2790}
2791
David Ahernc55c8982019-06-08 14:53:29 -07002792struct fib6_nh_rd_arg {
2793 struct fib6_result *res;
2794 struct flowi6 *fl6;
2795 const struct in6_addr *gw;
2796 struct rt6_info **ret;
2797};
2798
2799static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
2800{
2801 struct fib6_nh_rd_arg *arg = _arg;
2802
2803 arg->res->nh = nh;
2804 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
2805}
2806
Duan Jiongb55b76b2013-09-04 19:44:21 +08002807/* Handle redirects */
2808struct ip6rd_flowi {
2809 struct flowi6 fl6;
2810 struct in6_addr gateway;
2811};
2812
2813static struct rt6_info *__ip6_route_redirect(struct net *net,
2814 struct fib6_table *table,
2815 struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08002816 const struct sk_buff *skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002817 int flags)
2818{
2819 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
David Ahern0b34eb02019-04-09 14:41:19 -07002820 struct rt6_info *ret = NULL;
David Ahern9b6b35a2019-04-16 14:36:02 -07002821 struct fib6_result res = {};
David Ahernc55c8982019-06-08 14:53:29 -07002822 struct fib6_nh_rd_arg arg = {
2823 .res = &res,
2824 .fl6 = fl6,
2825 .gw = &rdfl->gateway,
2826 .ret = &ret
2827 };
David Ahern8d1c8022018-04-17 17:33:26 -07002828 struct fib6_info *rt;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002829 struct fib6_node *fn;
2830
David Ahern31680ac2019-05-22 15:12:18 -07002831 /* l3mdev_update_flow overrides oif if the device is enslaved; in
2832 * this case we must match on the real ingress device, so reset it
2833 */
2834 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2835 fl6->flowi6_oif = skb->dev->ifindex;
2836
Duan Jiongb55b76b2013-09-04 19:44:21 +08002837 /* Get the "current" route for this destination and
Alexander Alemayhu67c408c2017-01-07 23:53:00 +01002838 * check if the redirect has come from appropriate router.
Duan Jiongb55b76b2013-09-04 19:44:21 +08002839 *
2840 * RFC 4861 specifies that redirects should only be
2841 * accepted if they come from the nexthop to the target.
2842 * Due to the way the routes are chosen, this notion
2843 * is a bit fuzzy and one might need to check all possible
2844 * routes.
2845 */
2846
Wei Wang66f5d6c2017-10-06 12:06:10 -07002847 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07002848 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002849restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002850 for_each_fib6_node_rt_rcu(fn) {
David Ahern9b6b35a2019-04-16 14:36:02 -07002851 res.f6i = rt;
David Ahern14895682018-04-17 17:33:17 -07002852 if (fib6_check_expired(rt))
Duan Jiongb55b76b2013-09-04 19:44:21 +08002853 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002854 if (rt->fib6_flags & RTF_REJECT)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002855 break;
David Ahernc55c8982019-06-08 14:53:29 -07002856 if (unlikely(rt->nh)) {
2857 if (nexthop_is_blackhole(rt->nh))
2858 continue;
2859 /* on match, res->nh is filled in and potentially ret */
2860 if (nexthop_for_each_fib6_nh(rt->nh,
2861 fib6_nh_redirect_match,
2862 &arg))
2863 goto out;
2864 } else {
2865 res.nh = rt->fib6_nh;
2866 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
2867 &ret))
2868 goto out;
2869 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002870 }
2871
2872 if (!rt)
David Ahern421842e2018-04-17 17:33:18 -07002873 rt = net->ipv6.fib6_null_entry;
David Ahern93c2fb22018-04-18 15:38:59 -07002874 else if (rt->fib6_flags & RTF_REJECT) {
David Ahern23fb93a2018-04-17 17:33:23 -07002875 ret = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002876 goto out;
2877 }
2878
David Ahern421842e2018-04-17 17:33:18 -07002879 if (rt == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002880 fn = fib6_backtrack(fn, &fl6->saddr);
2881 if (fn)
2882 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002883 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002884
David Ahern9b6b35a2019-04-16 14:36:02 -07002885 res.f6i = rt;
David Ahern1cf844c2019-05-22 20:27:59 -07002886 res.nh = rt->fib6_nh;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002887out:
David Ahern7d21fec2019-04-16 14:36:11 -07002888 if (ret) {
David Ahern10585b42019-03-20 09:24:50 -07002889 ip6_hold_safe(net, &ret);
David Ahern7d21fec2019-04-16 14:36:11 -07002890 } else {
2891 res.fib6_flags = res.f6i->fib6_flags;
2892 res.fib6_type = res.f6i->fib6_type;
David Ahern9b6b35a2019-04-16 14:36:02 -07002893 ret = ip6_create_rt_rcu(&res);
David Ahern7d21fec2019-04-16 14:36:11 -07002894 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002895
Wei Wang66f5d6c2017-10-06 12:06:10 -07002896 rcu_read_unlock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08002897
David Ahern8ff2e5b2019-04-16 14:36:09 -07002898 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahern23fb93a2018-04-17 17:33:23 -07002899 return ret;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002900};
2901
2902static struct dst_entry *ip6_route_redirect(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08002903 const struct flowi6 *fl6,
2904 const struct sk_buff *skb,
2905 const struct in6_addr *gateway)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002906{
2907 int flags = RT6_LOOKUP_F_HAS_SADDR;
2908 struct ip6rd_flowi rdfl;
2909
2910 rdfl.fl6 = *fl6;
2911 rdfl.gateway = *gateway;
2912
David Ahernb75cc8f2018-03-02 08:32:17 -08002913 return fib6_rule_lookup(net, &rdfl.fl6, skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002914 flags, __ip6_route_redirect);
2915}
2916
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002917void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2918 kuid_t uid)
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002919{
2920 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2921 struct dst_entry *dst;
Maciej Żenczykowski1f7f10a2018-09-29 23:44:48 -07002922 struct flowi6 fl6 = {
2923 .flowi6_iif = LOOPBACK_IFINDEX,
2924 .flowi6_oif = oif,
2925 .flowi6_mark = mark,
2926 .daddr = iph->daddr,
2927 .saddr = iph->saddr,
2928 .flowlabel = ip6_flowinfo(iph),
2929 .flowi6_uid = uid,
2930 };
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002931
David Ahernb75cc8f2018-03-02 08:32:17 -08002932 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002933 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002934 dst_release(dst);
2935}
2936EXPORT_SYMBOL_GPL(ip6_redirect);
2937
Maciej Żenczykowskid4563362018-09-29 23:44:50 -07002938void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
Duan Jiongc92a59e2013-08-22 12:07:35 +08002939{
2940 const struct ipv6hdr *iph = ipv6_hdr(skb);
2941 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2942 struct dst_entry *dst;
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07002943 struct flowi6 fl6 = {
2944 .flowi6_iif = LOOPBACK_IFINDEX,
2945 .flowi6_oif = oif,
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07002946 .daddr = msg->dest,
2947 .saddr = iph->daddr,
2948 .flowi6_uid = sock_net_uid(net, NULL),
2949 };
Duan Jiongc92a59e2013-08-22 12:07:35 +08002950
David Ahernb75cc8f2018-03-02 08:32:17 -08002951 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002952 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08002953 dst_release(dst);
2954}
2955
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002956void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
2957{
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002958 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2959 sk->sk_uid);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002960}
2961EXPORT_SYMBOL_GPL(ip6_sk_redirect);
2962
David S. Miller0dbaee32010-12-13 12:52:14 -08002963static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964{
David S. Miller0dbaee32010-12-13 12:52:14 -08002965 struct net_device *dev = dst->dev;
2966 unsigned int mtu = dst_mtu(dst);
2967 struct net *net = dev_net(dev);
2968
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
2970
Daniel Lezcano55786892008-03-04 13:47:47 -08002971 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
2972 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
2974 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002975 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
2976 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
2977 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 * rely only on pmtu discovery"
2979 */
2980 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
2981 mtu = IPV6_MAXPLEN;
2982 return mtu;
2983}
2984
Steffen Klassertebb762f2011-11-23 02:12:51 +00002985static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002986{
David S. Millerd33e4552010-12-14 13:01:14 -08002987 struct inet6_dev *idev;
David Ahernd4ead6b2018-04-17 17:33:16 -07002988 unsigned int mtu;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002989
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002990 mtu = dst_metric_raw(dst, RTAX_MTU);
2991 if (mtu)
2992 goto out;
2993
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002994 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08002995
2996 rcu_read_lock();
2997 idev = __in6_dev_get(dst->dev);
2998 if (idev)
2999 mtu = idev->cnf.mtu6;
3000 rcu_read_unlock();
3001
Eric Dumazet30f78d82014-04-10 21:23:36 -07003002out:
Roopa Prabhu14972cb2016-08-24 20:10:43 -07003003 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3004
3005 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
David S. Millerd33e4552010-12-14 13:01:14 -08003006}
3007
David Ahern901731b2018-05-21 09:08:14 -07003008/* MTU selection:
3009 * 1. mtu on route is locked - use it
3010 * 2. mtu from nexthop exception
3011 * 3. mtu from egress device
3012 *
3013 * based on ip6_dst_mtu_forward and exception logic of
3014 * rt6_find_cached_rt; called with rcu_read_lock
3015 */
David Ahernb748f262019-04-16 14:36:06 -07003016u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3017 const struct in6_addr *daddr,
3018 const struct in6_addr *saddr)
David Ahern901731b2018-05-21 09:08:14 -07003019{
David Ahernb748f262019-04-16 14:36:06 -07003020 const struct fib6_nh *nh = res->nh;
3021 struct fib6_info *f6i = res->f6i;
David Ahern901731b2018-05-21 09:08:14 -07003022 struct inet6_dev *idev;
Wei Wang510e2ce2019-05-16 13:30:54 -07003023 struct rt6_info *rt;
David Ahern901731b2018-05-21 09:08:14 -07003024 u32 mtu = 0;
3025
3026 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3027 mtu = f6i->fib6_pmtu;
3028 if (mtu)
3029 goto out;
3030 }
3031
Wei Wang510e2ce2019-05-16 13:30:54 -07003032 rt = rt6_find_cached_rt(res, daddr, saddr);
3033 if (unlikely(rt)) {
3034 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3035 } else {
David Ahernb748f262019-04-16 14:36:06 -07003036 struct net_device *dev = nh->fib_nh_dev;
David Ahern901731b2018-05-21 09:08:14 -07003037
3038 mtu = IPV6_MIN_MTU;
3039 idev = __in6_dev_get(dev);
3040 if (idev && idev->cnf.mtu6 > mtu)
3041 mtu = idev->cnf.mtu6;
3042 }
3043
3044 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3045out:
David Ahernb748f262019-04-16 14:36:06 -07003046 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahern901731b2018-05-21 09:08:14 -07003047}
3048
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08003049struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05003050 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
David S. Miller87a11572011-12-06 17:04:13 -05003052 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 struct rt6_info *rt;
3054 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003055 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
David S. Miller38308472011-12-03 18:02:47 -05003057 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00003058 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Martin KaFai Lauad706862015-08-14 11:05:52 -07003060 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05003061 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05003063 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 goto out;
3065 }
3066
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003067 rt->dst.flags |= DST_HOST;
Brendan McGrath588753f2017-12-13 22:14:57 +11003068 rt->dst.input = ip6_input;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003069 rt->dst.output = ip6_output;
Julian Anastasov550bab42013-10-20 15:43:04 +03003070 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05003071 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003072 rt->rt6i_dst.plen = 128;
3073 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08003074 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Ido Schimmel4c981e22018-01-07 12:45:04 +02003076 /* Add this dst into uncached_list so that rt6_disable_ip() can
Wei Wang587fea72017-06-17 10:42:36 -07003077 * do proper release of the net_device
3078 */
3079 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07003080 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
David S. Miller87a11572011-12-06 17:04:13 -05003082 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084out:
David S. Miller87a11572011-12-06 17:04:13 -05003085 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086}
3087
Daniel Lezcano569d3642008-01-18 03:56:57 -08003088static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003090 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08003091 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3092 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
3093 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3094 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3095 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003096 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
Eric Dumazetfc66f952010-10-08 06:37:34 +00003098 entries = dst_entries_get_fast(ops);
Michal Kubeček49a18d82013-08-01 10:04:24 +02003099 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00003100 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 goto out;
3102
Benjamin Thery6891a342008-03-04 13:49:47 -08003103 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08003104 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003105 entries = dst_entries_get_slow(ops);
3106 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08003107 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08003109 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003110 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
David Ahern8c145862016-04-24 21:26:04 -07003113static struct rt6_info *ip6_nh_lookup_table(struct net *net,
3114 struct fib6_config *cfg,
David Ahernf4797b32018-01-25 16:55:08 -08003115 const struct in6_addr *gw_addr,
3116 u32 tbid, int flags)
David Ahern8c145862016-04-24 21:26:04 -07003117{
3118 struct flowi6 fl6 = {
3119 .flowi6_oif = cfg->fc_ifindex,
3120 .daddr = *gw_addr,
3121 .saddr = cfg->fc_prefsrc,
3122 };
3123 struct fib6_table *table;
3124 struct rt6_info *rt;
David Ahern8c145862016-04-24 21:26:04 -07003125
David Ahernf4797b32018-01-25 16:55:08 -08003126 table = fib6_get_table(net, tbid);
David Ahern8c145862016-04-24 21:26:04 -07003127 if (!table)
3128 return NULL;
3129
3130 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3131 flags |= RT6_LOOKUP_F_HAS_SADDR;
3132
David Ahernf4797b32018-01-25 16:55:08 -08003133 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
David Ahernb75cc8f2018-03-02 08:32:17 -08003134 rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
David Ahern8c145862016-04-24 21:26:04 -07003135
3136 /* if table lookup failed, fall back to full lookup */
3137 if (rt == net->ipv6.ip6_null_entry) {
3138 ip6_rt_put(rt);
3139 rt = NULL;
3140 }
3141
3142 return rt;
3143}
3144
David Ahernfc1e64e2018-01-25 16:55:09 -08003145static int ip6_route_check_nh_onlink(struct net *net,
3146 struct fib6_config *cfg,
David Ahern9fbb7042018-03-13 08:29:36 -07003147 const struct net_device *dev,
David Ahernfc1e64e2018-01-25 16:55:09 -08003148 struct netlink_ext_ack *extack)
3149{
David Ahern44750f82018-02-06 13:17:06 -08003150 u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
David Ahernfc1e64e2018-01-25 16:55:09 -08003151 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3152 u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
Paolo Abenibf1dc8b2019-02-21 11:19:42 +01003153 struct fib6_info *from;
David Ahernfc1e64e2018-01-25 16:55:09 -08003154 struct rt6_info *grt;
3155 int err;
3156
3157 err = 0;
3158 grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
3159 if (grt) {
Paolo Abenibf1dc8b2019-02-21 11:19:42 +01003160 rcu_read_lock();
3161 from = rcu_dereference(grt->from);
David Ahern58e354c2018-02-06 12:14:12 -08003162 if (!grt->dst.error &&
David Ahern4ed591c2018-10-24 13:58:39 -07003163 /* ignore match if it is the default route */
Paolo Abenibf1dc8b2019-02-21 11:19:42 +01003164 from && !ipv6_addr_any(&from->fib6_dst.addr) &&
David Ahern58e354c2018-02-06 12:14:12 -08003165 (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
David Ahern44750f82018-02-06 13:17:06 -08003166 NL_SET_ERR_MSG(extack,
3167 "Nexthop has invalid gateway or device mismatch");
David Ahernfc1e64e2018-01-25 16:55:09 -08003168 err = -EINVAL;
3169 }
Paolo Abenibf1dc8b2019-02-21 11:19:42 +01003170 rcu_read_unlock();
David Ahernfc1e64e2018-01-25 16:55:09 -08003171
3172 ip6_rt_put(grt);
3173 }
3174
3175 return err;
3176}
3177
David Ahern1edce992018-01-25 16:55:07 -08003178static int ip6_route_check_nh(struct net *net,
3179 struct fib6_config *cfg,
3180 struct net_device **_dev,
3181 struct inet6_dev **idev)
3182{
3183 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3184 struct net_device *dev = _dev ? *_dev : NULL;
3185 struct rt6_info *grt = NULL;
3186 int err = -EHOSTUNREACH;
3187
3188 if (cfg->fc_table) {
David Ahernf4797b32018-01-25 16:55:08 -08003189 int flags = RT6_LOOKUP_F_IFACE;
3190
3191 grt = ip6_nh_lookup_table(net, cfg, gw_addr,
3192 cfg->fc_table, flags);
David Ahern1edce992018-01-25 16:55:07 -08003193 if (grt) {
3194 if (grt->rt6i_flags & RTF_GATEWAY ||
3195 (dev && dev != grt->dst.dev)) {
3196 ip6_rt_put(grt);
3197 grt = NULL;
3198 }
3199 }
3200 }
3201
3202 if (!grt)
David Ahernb75cc8f2018-03-02 08:32:17 -08003203 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
David Ahern1edce992018-01-25 16:55:07 -08003204
3205 if (!grt)
3206 goto out;
3207
3208 if (dev) {
3209 if (dev != grt->dst.dev) {
3210 ip6_rt_put(grt);
3211 goto out;
3212 }
3213 } else {
3214 *_dev = dev = grt->dst.dev;
3215 *idev = grt->rt6i_idev;
3216 dev_hold(dev);
3217 in6_dev_hold(grt->rt6i_idev);
3218 }
3219
3220 if (!(grt->rt6i_flags & RTF_GATEWAY))
3221 err = 0;
3222
3223 ip6_rt_put(grt);
3224
3225out:
3226 return err;
3227}
3228
David Ahern9fbb7042018-03-13 08:29:36 -07003229static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3230 struct net_device **_dev, struct inet6_dev **idev,
3231 struct netlink_ext_ack *extack)
3232{
3233 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3234 int gwa_type = ipv6_addr_type(gw_addr);
David Ahern232378e2018-03-13 08:29:37 -07003235 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
David Ahern9fbb7042018-03-13 08:29:36 -07003236 const struct net_device *dev = *_dev;
David Ahern232378e2018-03-13 08:29:37 -07003237 bool need_addr_check = !dev;
David Ahern9fbb7042018-03-13 08:29:36 -07003238 int err = -EINVAL;
3239
3240 /* if gw_addr is local we will fail to detect this in case
3241 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3242 * will return already-added prefix route via interface that
3243 * prefix route was assigned to, which might be non-loopback.
3244 */
David Ahern232378e2018-03-13 08:29:37 -07003245 if (dev &&
3246 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3247 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
David Ahern9fbb7042018-03-13 08:29:36 -07003248 goto out;
3249 }
3250
3251 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3252 /* IPv6 strictly inhibits using not link-local
3253 * addresses as nexthop address.
3254 * Otherwise, router will not able to send redirects.
3255 * It is very good, but in some (rare!) circumstances
3256 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3257 * some exceptions. --ANK
3258 * We allow IPv4-mapped nexthops to support RFC4798-type
3259 * addressing
3260 */
3261 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3262 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3263 goto out;
3264 }
3265
3266 if (cfg->fc_flags & RTNH_F_ONLINK)
3267 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3268 else
3269 err = ip6_route_check_nh(net, cfg, _dev, idev);
3270
3271 if (err)
3272 goto out;
3273 }
3274
3275 /* reload in case device was changed */
3276 dev = *_dev;
3277
3278 err = -EINVAL;
3279 if (!dev) {
3280 NL_SET_ERR_MSG(extack, "Egress device not specified");
3281 goto out;
3282 } else if (dev->flags & IFF_LOOPBACK) {
3283 NL_SET_ERR_MSG(extack,
3284 "Egress device can not be loopback device for this route");
3285 goto out;
3286 }
David Ahern232378e2018-03-13 08:29:37 -07003287
3288 /* if we did not check gw_addr above, do so now that the
3289 * egress device has been resolved.
3290 */
3291 if (need_addr_check &&
3292 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3293 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3294 goto out;
3295 }
3296
David Ahern9fbb7042018-03-13 08:29:36 -07003297 err = 0;
3298out:
3299 return err;
3300}
3301
David Ahern83c442512019-03-27 20:53:50 -07003302static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3303{
3304 if ((flags & RTF_REJECT) ||
3305 (dev && (dev->flags & IFF_LOOPBACK) &&
3306 !(addr_type & IPV6_ADDR_LOOPBACK) &&
3307 !(flags & RTF_LOCAL)))
3308 return true;
3309
3310 return false;
3311}
3312
3313int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3314 struct fib6_config *cfg, gfp_t gfp_flags,
3315 struct netlink_ext_ack *extack)
3316{
3317 struct net_device *dev = NULL;
3318 struct inet6_dev *idev = NULL;
3319 int addr_type;
3320 int err;
3321
David Ahernf1741732019-03-27 20:53:57 -07003322 fib6_nh->fib_nh_family = AF_INET6;
3323
David Ahern83c442512019-03-27 20:53:50 -07003324 err = -ENODEV;
3325 if (cfg->fc_ifindex) {
3326 dev = dev_get_by_index(net, cfg->fc_ifindex);
3327 if (!dev)
3328 goto out;
3329 idev = in6_dev_get(dev);
3330 if (!idev)
3331 goto out;
3332 }
3333
3334 if (cfg->fc_flags & RTNH_F_ONLINK) {
3335 if (!dev) {
3336 NL_SET_ERR_MSG(extack,
3337 "Nexthop device required for onlink");
3338 goto out;
3339 }
3340
3341 if (!(dev->flags & IFF_UP)) {
3342 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3343 err = -ENETDOWN;
3344 goto out;
3345 }
3346
David Ahernad1601a2019-03-27 20:53:56 -07003347 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
David Ahern83c442512019-03-27 20:53:50 -07003348 }
3349
David Ahernad1601a2019-03-27 20:53:56 -07003350 fib6_nh->fib_nh_weight = 1;
David Ahern83c442512019-03-27 20:53:50 -07003351
3352 /* We cannot add true routes via loopback here,
3353 * they would result in kernel looping; promote them to reject routes
3354 */
3355 addr_type = ipv6_addr_type(&cfg->fc_dst);
3356 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3357 /* hold loopback dev/idev if we haven't done so. */
3358 if (dev != net->loopback_dev) {
3359 if (dev) {
3360 dev_put(dev);
3361 in6_dev_put(idev);
3362 }
3363 dev = net->loopback_dev;
3364 dev_hold(dev);
3365 idev = in6_dev_get(dev);
3366 if (!idev) {
3367 err = -ENODEV;
3368 goto out;
3369 }
3370 }
David Ahern7dd73162019-06-03 18:37:03 -07003371 goto pcpu_alloc;
David Ahern83c442512019-03-27 20:53:50 -07003372 }
3373
3374 if (cfg->fc_flags & RTF_GATEWAY) {
3375 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3376 if (err)
3377 goto out;
3378
David Ahernad1601a2019-03-27 20:53:56 -07003379 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
David Ahernbdf00462019-04-05 16:30:26 -07003380 fib6_nh->fib_nh_gw_family = AF_INET6;
David Ahern83c442512019-03-27 20:53:50 -07003381 }
3382
3383 err = -ENODEV;
3384 if (!dev)
3385 goto out;
3386
3387 if (idev->cnf.disable_ipv6) {
3388 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3389 err = -EACCES;
3390 goto out;
3391 }
3392
3393 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3394 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3395 err = -ENETDOWN;
3396 goto out;
3397 }
3398
3399 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3400 !netif_carrier_ok(dev))
David Ahernad1601a2019-03-27 20:53:56 -07003401 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
David Ahern83c442512019-03-27 20:53:50 -07003402
David Ahern7dd73162019-06-03 18:37:03 -07003403 err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap,
3404 cfg->fc_encap_type, cfg, gfp_flags, extack);
3405 if (err)
3406 goto out;
3407
3408pcpu_alloc:
David Ahernf40b6ae2019-05-22 20:27:55 -07003409 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3410 if (!fib6_nh->rt6i_pcpu) {
3411 err = -ENOMEM;
3412 goto out;
3413 }
3414
David Ahernad1601a2019-03-27 20:53:56 -07003415 fib6_nh->fib_nh_dev = dev;
David Ahernf1741732019-03-27 20:53:57 -07003416 fib6_nh->fib_nh_oif = dev->ifindex;
David Ahern83c442512019-03-27 20:53:50 -07003417 err = 0;
3418out:
3419 if (idev)
3420 in6_dev_put(idev);
3421
3422 if (err) {
David Ahernad1601a2019-03-27 20:53:56 -07003423 lwtstate_put(fib6_nh->fib_nh_lws);
3424 fib6_nh->fib_nh_lws = NULL;
David Ahern83c442512019-03-27 20:53:50 -07003425 if (dev)
3426 dev_put(dev);
3427 }
3428
3429 return err;
3430}
3431
David Aherndac7d0f2019-03-27 20:53:51 -07003432void fib6_nh_release(struct fib6_nh *fib6_nh)
3433{
David Aherncc5c0732019-05-22 20:27:58 -07003434 struct rt6_exception_bucket *bucket;
3435
3436 rcu_read_lock();
3437
3438 fib6_nh_flush_exceptions(fib6_nh, NULL);
3439 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3440 if (bucket) {
3441 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3442 kfree(bucket);
3443 }
3444
3445 rcu_read_unlock();
3446
David Ahernf40b6ae2019-05-22 20:27:55 -07003447 if (fib6_nh->rt6i_pcpu) {
3448 int cpu;
3449
3450 for_each_possible_cpu(cpu) {
3451 struct rt6_info **ppcpu_rt;
3452 struct rt6_info *pcpu_rt;
3453
3454 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3455 pcpu_rt = *ppcpu_rt;
3456 if (pcpu_rt) {
3457 dst_dev_put(&pcpu_rt->dst);
3458 dst_release(&pcpu_rt->dst);
3459 *ppcpu_rt = NULL;
3460 }
3461 }
3462
3463 free_percpu(fib6_nh->rt6i_pcpu);
3464 }
3465
David Ahern979e2762019-03-27 20:53:58 -07003466 fib_nh_common_release(&fib6_nh->nh_common);
David Aherndac7d0f2019-03-27 20:53:51 -07003467}
3468
David Ahern8d1c8022018-04-17 17:33:26 -07003469static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
David Ahernacb54e32018-04-17 17:33:22 -07003470 gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003471 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
Daniel Lezcano55786892008-03-04 13:47:47 -08003473 struct net *net = cfg->fc_nlinfo.nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07003474 struct fib6_info *rt = NULL;
David Ahernf88d8ea2019-06-03 20:19:52 -07003475 struct nexthop *nh = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003476 struct fib6_table *table;
David Ahernf88d8ea2019-06-03 20:19:52 -07003477 struct fib6_nh *fib6_nh;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003478 int err = -EINVAL;
David Ahern83c442512019-03-27 20:53:50 -07003479 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
David Ahern557c44b2017-04-19 14:19:43 -07003481 /* RTF_PCPU is an internal flag; can not be set by userspace */
David Ahernd5d531c2017-05-21 10:12:05 -06003482 if (cfg->fc_flags & RTF_PCPU) {
3483 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
David Ahern557c44b2017-04-19 14:19:43 -07003484 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003485 }
David Ahern557c44b2017-04-19 14:19:43 -07003486
Wei Wang2ea23522017-10-27 17:30:12 -07003487 /* RTF_CACHE is an internal flag; can not be set by userspace */
3488 if (cfg->fc_flags & RTF_CACHE) {
3489 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3490 goto out;
3491 }
3492
David Aherne8478e82018-04-17 17:33:13 -07003493 if (cfg->fc_type > RTN_MAX) {
3494 NL_SET_ERR_MSG(extack, "Invalid route type");
3495 goto out;
3496 }
3497
David Ahernd5d531c2017-05-21 10:12:05 -06003498 if (cfg->fc_dst_len > 128) {
3499 NL_SET_ERR_MSG(extack, "Invalid prefix length");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003500 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003501 }
3502 if (cfg->fc_src_len > 128) {
3503 NL_SET_ERR_MSG(extack, "Invalid source address length");
3504 goto out;
3505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506#ifndef CONFIG_IPV6_SUBTREES
David Ahernd5d531c2017-05-21 10:12:05 -06003507 if (cfg->fc_src_len) {
3508 NL_SET_ERR_MSG(extack,
3509 "Specifying source address requires IPV6_SUBTREES to be enabled");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003510 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512#endif
David Ahernfc1e64e2018-01-25 16:55:09 -08003513
Matti Vaittinend71314b2011-11-14 00:14:49 +00003514 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05003515 if (cfg->fc_nlinfo.nlh &&
3516 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00003517 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05003518 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00003519 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00003520 table = fib6_new_table(net, cfg->fc_table);
3521 }
3522 } else {
3523 table = fib6_new_table(net, cfg->fc_table);
3524 }
David S. Miller38308472011-12-03 18:02:47 -05003525
3526 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003527 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07003528
David Ahern93531c62018-04-17 17:33:25 -07003529 err = -ENOMEM;
David Ahernf88d8ea2019-06-03 20:19:52 -07003530 rt = fib6_info_alloc(gfp_flags, !nh);
David Ahern93531c62018-04-17 17:33:25 -07003531 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 goto out;
David Ahern93531c62018-04-17 17:33:25 -07003533
David Ahernd7e774f2018-11-06 12:51:15 -08003534 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3535 extack);
David Ahern767a2212018-10-04 20:07:51 -07003536 if (IS_ERR(rt->fib6_metrics)) {
3537 err = PTR_ERR(rt->fib6_metrics);
Eric Dumazetfda21d42018-10-05 09:17:50 -07003538 /* Do not leave garbage there. */
3539 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
David Ahern767a2212018-10-04 20:07:51 -07003540 goto out;
3541 }
3542
David Ahern93531c62018-04-17 17:33:25 -07003543 if (cfg->fc_flags & RTF_ADDRCONF)
3544 rt->dst_nocount = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Gao feng1716a962012-04-06 00:13:10 +00003546 if (cfg->fc_flags & RTF_EXPIRES)
David Ahern14895682018-04-17 17:33:17 -07003547 fib6_set_expires(rt, jiffies +
Gao feng1716a962012-04-06 00:13:10 +00003548 clock_t_to_jiffies(cfg->fc_expires));
3549 else
David Ahern14895682018-04-17 17:33:17 -07003550 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Thomas Graf86872cb2006-08-22 00:01:08 -07003552 if (cfg->fc_protocol == RTPROT_UNSPEC)
3553 cfg->fc_protocol = RTPROT_BOOT;
David Ahern93c2fb22018-04-18 15:38:59 -07003554 rt->fib6_protocol = cfg->fc_protocol;
Thomas Graf86872cb2006-08-22 00:01:08 -07003555
David Ahern83c442512019-03-27 20:53:50 -07003556 rt->fib6_table = table;
3557 rt->fib6_metric = cfg->fc_metric;
3558 rt->fib6_type = cfg->fc_type;
David Ahern2b2450c2019-03-27 20:53:52 -07003559 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003560
David Ahern93c2fb22018-04-18 15:38:59 -07003561 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3562 rt->fib6_dst.plen = cfg->fc_dst_len;
3563 if (rt->fib6_dst.plen == 128)
David Ahern3b6761d2018-04-17 17:33:20 -07003564 rt->dst_host = true;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01003565
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07003567 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3568 rt->fib6_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569#endif
David Ahernf88d8ea2019-06-03 20:19:52 -07003570 if (nh) {
3571 if (!nexthop_get(nh)) {
3572 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3573 goto out;
3574 }
3575 if (rt->fib6_src.plen) {
Colin Ian King4daa95a2019-06-06 09:40:39 +01003576 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
David Ahernf88d8ea2019-06-03 20:19:52 -07003577 goto out;
3578 }
3579 rt->nh = nh;
3580 fib6_nh = nexthop_fib6_nh(rt->nh);
3581 } else {
3582 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3583 if (err)
3584 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
David Ahernf88d8ea2019-06-03 20:19:52 -07003586 fib6_nh = rt->fib6_nh;
3587
3588 /* We cannot add true routes via loopback here, they would
3589 * result in kernel looping; promote them to reject routes
3590 */
3591 addr_type = ipv6_addr_type(&cfg->fc_dst);
3592 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3593 addr_type))
3594 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3595 }
David Ahern955ec4c2018-01-24 19:45:29 -08003596
Daniel Walterc3968a82011-04-13 21:10:57 +00003597 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
David Ahernf88d8ea2019-06-03 20:19:52 -07003598 struct net_device *dev = fib6_nh->fib_nh_dev;
David Ahern83c442512019-03-27 20:53:50 -07003599
Daniel Walterc3968a82011-04-13 21:10:57 +00003600 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
David Ahernd5d531c2017-05-21 10:12:05 -06003601 NL_SET_ERR_MSG(extack, "Invalid source address");
Daniel Walterc3968a82011-04-13 21:10:57 +00003602 err = -EINVAL;
3603 goto out;
3604 }
David Ahern93c2fb22018-04-18 15:38:59 -07003605 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3606 rt->fib6_prefsrc.plen = 128;
Daniel Walterc3968a82011-04-13 21:10:57 +00003607 } else
David Ahern93c2fb22018-04-18 15:38:59 -07003608 rt->fib6_prefsrc.plen = 0;
Daniel Walterc3968a82011-04-13 21:10:57 +00003609
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003610 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611out:
David Ahern93531c62018-04-17 17:33:25 -07003612 fib6_info_release(rt);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003613 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003614}
3615
David Ahernacb54e32018-04-17 17:33:22 -07003616int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003617 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003618{
David Ahern8d1c8022018-04-17 17:33:26 -07003619 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003620 int err;
3621
David Ahernacb54e32018-04-17 17:33:22 -07003622 rt = ip6_route_info_create(cfg, gfp_flags, extack);
David Ahernd4ead6b2018-04-17 17:33:16 -07003623 if (IS_ERR(rt))
3624 return PTR_ERR(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003625
David Ahernd4ead6b2018-04-17 17:33:16 -07003626 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
David Ahern93531c62018-04-17 17:33:25 -07003627 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003628
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 return err;
3630}
3631
David Ahern8d1c8022018-04-17 17:33:26 -07003632static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633{
David Ahernafb1d4b52018-04-17 17:33:11 -07003634 struct net *net = info->nl_net;
Thomas Grafc71099a2006-08-04 23:20:06 -07003635 struct fib6_table *table;
David Ahernafb1d4b52018-04-17 17:33:11 -07003636 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
David Ahern421842e2018-04-17 17:33:18 -07003638 if (rt == net->ipv6.fib6_null_entry) {
Gao feng6825a262012-09-19 19:25:34 +00003639 err = -ENOENT;
3640 goto out;
3641 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07003642
David Ahern93c2fb22018-04-18 15:38:59 -07003643 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003644 spin_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07003645 err = fib6_del(rt, info);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003646 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647
Gao feng6825a262012-09-19 19:25:34 +00003648out:
David Ahern93531c62018-04-17 17:33:25 -07003649 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 return err;
3651}
3652
David Ahern8d1c8022018-04-17 17:33:26 -07003653int ip6_del_rt(struct net *net, struct fib6_info *rt)
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003654{
David Ahernafb1d4b52018-04-17 17:33:11 -07003655 struct nl_info info = { .nl_net = net };
3656
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003657 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003658}
3659
David Ahern8d1c8022018-04-17 17:33:26 -07003660static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
David Ahern0ae81332017-02-02 12:37:08 -08003661{
3662 struct nl_info *info = &cfg->fc_nlinfo;
WANG Conge3330032017-02-27 16:07:43 -08003663 struct net *net = info->nl_net;
David Ahern16a16cd2017-02-02 12:37:11 -08003664 struct sk_buff *skb = NULL;
David Ahern0ae81332017-02-02 12:37:08 -08003665 struct fib6_table *table;
WANG Conge3330032017-02-27 16:07:43 -08003666 int err = -ENOENT;
David Ahern0ae81332017-02-02 12:37:08 -08003667
David Ahern421842e2018-04-17 17:33:18 -07003668 if (rt == net->ipv6.fib6_null_entry)
WANG Conge3330032017-02-27 16:07:43 -08003669 goto out_put;
David Ahern93c2fb22018-04-18 15:38:59 -07003670 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003671 spin_lock_bh(&table->tb6_lock);
David Ahern0ae81332017-02-02 12:37:08 -08003672
David Ahern93c2fb22018-04-18 15:38:59 -07003673 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
David Ahern8d1c8022018-04-17 17:33:26 -07003674 struct fib6_info *sibling, *next_sibling;
David Ahern0ae81332017-02-02 12:37:08 -08003675
David Ahern16a16cd2017-02-02 12:37:11 -08003676 /* prefer to send a single notification with all hops */
3677 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3678 if (skb) {
3679 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3680
David Ahernd4ead6b2018-04-17 17:33:16 -07003681 if (rt6_fill_node(net, skb, rt, NULL,
David Ahern16a16cd2017-02-02 12:37:11 -08003682 NULL, NULL, 0, RTM_DELROUTE,
3683 info->portid, seq, 0) < 0) {
3684 kfree_skb(skb);
3685 skb = NULL;
3686 } else
3687 info->skip_notify = 1;
3688 }
3689
David Ahern0ae81332017-02-02 12:37:08 -08003690 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07003691 &rt->fib6_siblings,
3692 fib6_siblings) {
David Ahern0ae81332017-02-02 12:37:08 -08003693 err = fib6_del(sibling, info);
3694 if (err)
WANG Conge3330032017-02-27 16:07:43 -08003695 goto out_unlock;
David Ahern0ae81332017-02-02 12:37:08 -08003696 }
3697 }
3698
3699 err = fib6_del(rt, info);
WANG Conge3330032017-02-27 16:07:43 -08003700out_unlock:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003701 spin_unlock_bh(&table->tb6_lock);
WANG Conge3330032017-02-27 16:07:43 -08003702out_put:
David Ahern93531c62018-04-17 17:33:25 -07003703 fib6_info_release(rt);
David Ahern16a16cd2017-02-02 12:37:11 -08003704
3705 if (skb) {
WANG Conge3330032017-02-27 16:07:43 -08003706 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
David Ahern16a16cd2017-02-02 12:37:11 -08003707 info->nlh, gfp_any());
3708 }
David Ahern0ae81332017-02-02 12:37:08 -08003709 return err;
3710}
3711
David Ahern0fa6efc2019-05-22 20:28:00 -07003712static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
David Ahern23fb93a2018-04-17 17:33:23 -07003713{
3714 int rc = -ESRCH;
3715
3716 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3717 goto out;
3718
3719 if (cfg->fc_flags & RTF_GATEWAY &&
3720 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3721 goto out;
Xin Long761f6022018-11-14 00:48:28 +08003722
3723 rc = rt6_remove_exception_rt(rt);
David Ahern23fb93a2018-04-17 17:33:23 -07003724out:
3725 return rc;
3726}
3727
David Ahern0fa6efc2019-05-22 20:28:00 -07003728static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3729 struct fib6_nh *nh)
3730{
3731 struct fib6_result res = {
3732 .f6i = rt,
3733 .nh = nh,
3734 };
3735 struct rt6_info *rt_cache;
3736
3737 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3738 if (rt_cache)
3739 return __ip6_del_cached_rt(rt_cache, cfg);
3740
3741 return 0;
3742}
3743
David Ahern333c4302017-05-21 10:12:04 -06003744static int ip6_route_del(struct fib6_config *cfg,
3745 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746{
Thomas Grafc71099a2006-08-04 23:20:06 -07003747 struct fib6_table *table;
David Ahern8d1c8022018-04-17 17:33:26 -07003748 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 int err = -ESRCH;
3751
Daniel Lezcano55786892008-03-04 13:47:47 -08003752 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David Ahernd5d531c2017-05-21 10:12:05 -06003753 if (!table) {
3754 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Grafc71099a2006-08-04 23:20:06 -07003755 return err;
David Ahernd5d531c2017-05-21 10:12:05 -06003756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Wei Wang66f5d6c2017-10-06 12:06:10 -07003758 rcu_read_lock();
Thomas Grafc71099a2006-08-04 23:20:06 -07003759
3760 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07003761 &cfg->fc_dst, cfg->fc_dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07003762 &cfg->fc_src, cfg->fc_src_len,
Wei Wang2b760fc2017-10-06 12:06:03 -07003763 !(cfg->fc_flags & RTF_CACHE));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003764
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 if (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003766 for_each_fib6_node_rt_rcu(fn) {
David Ahernad1601a2019-03-27 20:53:56 -07003767 struct fib6_nh *nh;
3768
David Ahern1cf844c2019-05-22 20:27:59 -07003769 nh = rt->fib6_nh;
Wei Wang2b760fc2017-10-06 12:06:03 -07003770 if (cfg->fc_flags & RTF_CACHE) {
David Ahern23fb93a2018-04-17 17:33:23 -07003771 int rc;
3772
David Ahern0fa6efc2019-05-22 20:28:00 -07003773 rc = ip6_del_cached_rt(cfg, rt, nh);
3774 if (rc != -ESRCH) {
3775 rcu_read_unlock();
3776 return rc;
David Ahern23fb93a2018-04-17 17:33:23 -07003777 }
3778 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07003779 }
David Ahernad1601a2019-03-27 20:53:56 -07003780
Thomas Graf86872cb2006-08-22 00:01:08 -07003781 if (cfg->fc_ifindex &&
David Ahernad1601a2019-03-27 20:53:56 -07003782 (!nh->fib_nh_dev ||
3783 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07003785 if (cfg->fc_flags & RTF_GATEWAY &&
David Ahernad1601a2019-03-27 20:53:56 -07003786 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003788 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003790 if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol)
Mantas Mc2ed1882016-12-16 10:30:59 +02003791 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07003792 if (!fib6_info_hold_safe(rt))
3793 continue;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003794 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
David Ahern0ae81332017-02-02 12:37:08 -08003796 /* if gateway was specified only delete the one hop */
3797 if (cfg->fc_flags & RTF_GATEWAY)
3798 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3799
3800 return __ip6_del_rt_siblings(rt, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 }
3802 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003803 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
3805 return err;
3806}
3807
David S. Miller6700c272012-07-17 03:29:28 -07003808static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003809{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003810 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07003811 struct rt6_info *rt, *nrt = NULL;
David Ahern85bd05d2019-04-16 14:36:01 -07003812 struct fib6_result res = {};
David S. Millere8599ff2012-07-11 23:43:53 -07003813 struct ndisc_options ndopts;
3814 struct inet6_dev *in6_dev;
3815 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003816 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07003817 int optlen, on_link;
3818 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07003819
Simon Horman29a3cad2013-05-28 20:34:26 +00003820 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003821 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07003822
3823 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07003824 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003825 return;
3826 }
3827
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003828 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07003829
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003830 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003831 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003832 return;
3833 }
3834
David S. Miller6e157b62012-07-12 00:05:02 -07003835 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003836 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003837 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003838 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07003839 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003840 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003841 return;
3842 }
3843
3844 in6_dev = __in6_dev_get(skb->dev);
3845 if (!in6_dev)
3846 return;
3847 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3848 return;
3849
3850 /* RFC2461 8.1:
3851 * The IP source address of the Redirect MUST be the same as the current
3852 * first-hop router for the specified ICMP Destination Address.
3853 */
3854
Alexander Aringf997c552016-06-15 21:20:23 +02003855 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003856 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3857 return;
3858 }
David S. Miller6e157b62012-07-12 00:05:02 -07003859
3860 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07003861 if (ndopts.nd_opts_tgt_lladdr) {
3862 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3863 skb->dev);
3864 if (!lladdr) {
3865 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3866 return;
3867 }
3868 }
3869
David S. Miller6e157b62012-07-12 00:05:02 -07003870 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01003871 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07003872 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
3873 return;
3874 }
3875
3876 /* Redirect received -> path was valid.
3877 * Look, redirects are sent only in response to data packets,
3878 * so that this nexthop apparently is reachable. --ANK
3879 */
Julian Anastasov0dec8792017-02-06 23:14:16 +02003880 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
David S. Miller6e157b62012-07-12 00:05:02 -07003881
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003882 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07003883 if (!neigh)
3884 return;
3885
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 /*
3887 * We have finally decided to accept it.
3888 */
3889
Alexander Aringf997c552016-06-15 21:20:23 +02003890 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 NEIGH_UPDATE_F_WEAK_OVERRIDE|
3892 NEIGH_UPDATE_F_OVERRIDE|
3893 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02003894 NEIGH_UPDATE_F_ISROUTER)),
3895 NDISC_REDIRECT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
David Ahern4d85cd02018-04-20 15:37:59 -07003897 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07003898 res.f6i = rcu_dereference(rt->from);
David S. Millerff24e492019-05-02 22:14:21 -04003899 if (!res.f6i)
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07003900 goto out;
David Ahern8a14e462018-04-23 11:32:07 -07003901
David Ahern49d5b8e2019-06-08 14:53:30 -07003902 if (res.f6i->nh) {
3903 struct fib6_nh_match_arg arg = {
3904 .dev = dst->dev,
3905 .gw = &rt->rt6i_gateway,
3906 };
3907
3908 nexthop_for_each_fib6_nh(res.f6i->nh,
3909 fib6_nh_find_match, &arg);
3910
3911 /* fib6_info uses a nexthop that does not have fib6_nh
3912 * using the dst->dev. Should be impossible
3913 */
3914 if (!arg.match)
3915 goto out;
3916 res.nh = arg.match;
3917 } else {
3918 res.nh = res.f6i->fib6_nh;
3919 }
3920
David Ahern7d21fec2019-04-16 14:36:11 -07003921 res.fib6_flags = res.f6i->fib6_flags;
3922 res.fib6_type = res.f6i->fib6_type;
David Ahern85bd05d2019-04-16 14:36:01 -07003923 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05003924 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 goto out;
3926
3927 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
3928 if (on_link)
3929 nrt->rt6i_flags &= ~RTF_GATEWAY;
3930
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003931 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07003933 /* rt6_insert_exception() will take care of duplicated exceptions */
David Ahern5012f0a2019-04-16 14:36:05 -07003934 if (rt6_insert_exception(nrt, &res)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07003935 dst_release_immediate(&nrt->dst);
3936 goto out;
3937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938
Changli Gaod8d1f302010-06-10 23:31:35 -07003939 netevent.old = &rt->dst;
3940 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003941 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00003942 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07003943 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
3944
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945out:
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07003946 rcu_read_unlock();
David S. Millere8599ff2012-07-11 23:43:53 -07003947 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07003948}
3949
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003950#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -07003951static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003952 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07003953 const struct in6_addr *gwaddr,
3954 struct net_device *dev)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003955{
David Ahern830218c2016-10-24 10:52:35 -07003956 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3957 int ifindex = dev->ifindex;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003958 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07003959 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003960 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003961
David Ahern830218c2016-10-24 10:52:35 -07003962 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05003963 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003964 return NULL;
3965
Wei Wang66f5d6c2017-10-06 12:06:10 -07003966 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07003967 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003968 if (!fn)
3969 goto out;
3970
Wei Wang66f5d6c2017-10-06 12:06:10 -07003971 for_each_fib6_node_rt_rcu(fn) {
David Ahernf88d8ea2019-06-03 20:19:52 -07003972 /* these routes do not use nexthops */
3973 if (rt->nh)
3974 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07003975 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003976 continue;
David Ahern2b2450c2019-03-27 20:53:52 -07003977 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
David Ahern1cf844c2019-05-22 20:27:59 -07003978 !rt->fib6_nh->fib_nh_gw_family)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003979 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07003980 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003981 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07003982 if (!fib6_info_hold_safe(rt))
3983 continue;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003984 break;
3985 }
3986out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003987 rcu_read_unlock();
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003988 return rt;
3989}
3990
David Ahern8d1c8022018-04-17 17:33:26 -07003991static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003992 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07003993 const struct in6_addr *gwaddr,
3994 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +00003995 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003996{
Thomas Graf86872cb2006-08-22 00:01:08 -07003997 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08003998 .fc_metric = IP6_RT_PRIO_USER,
David Ahern830218c2016-10-24 10:52:35 -07003999 .fc_ifindex = dev->ifindex,
Thomas Graf86872cb2006-08-22 00:01:08 -07004000 .fc_dst_len = prefixlen,
4001 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4002 RTF_UP | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004003 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004004 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004005 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08004006 .fc_nlinfo.nlh = NULL,
4007 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004008 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004009
David Ahern830218c2016-10-24 10:52:35 -07004010 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004011 cfg.fc_dst = *prefix;
4012 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07004013
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08004014 /* We should treat it as a default route if prefix length is 0. */
4015 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07004016 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004017
David Ahernacb54e32018-04-17 17:33:22 -07004018 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004019
David Ahern830218c2016-10-24 10:52:35 -07004020 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004021}
4022#endif
4023
David Ahern8d1c8022018-04-17 17:33:26 -07004024struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004025 const struct in6_addr *addr,
4026 struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004027{
David Ahern830218c2016-10-24 10:52:35 -07004028 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
David Ahern8d1c8022018-04-17 17:33:26 -07004029 struct fib6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07004030 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
David Ahernafb1d4b52018-04-17 17:33:11 -07004032 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004033 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004034 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
Wei Wang66f5d6c2017-10-06 12:06:10 -07004036 rcu_read_lock();
4037 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004038 struct fib6_nh *nh;
David Ahernad1601a2019-03-27 20:53:56 -07004039
David Ahernf88d8ea2019-06-03 20:19:52 -07004040 /* RA routes do not use nexthops */
4041 if (rt->nh)
4042 continue;
4043
4044 nh = rt->fib6_nh;
David Ahernad1601a2019-03-27 20:53:56 -07004045 if (dev == nh->fib_nh_dev &&
David Ahern93c2fb22018-04-18 15:38:59 -07004046 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Ahernad1601a2019-03-27 20:53:56 -07004047 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 break;
4049 }
Wei Wange873e4b2018-07-21 20:56:32 -07004050 if (rt && !fib6_info_hold_safe(rt))
4051 rt = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07004052 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 return rt;
4054}
4055
David Ahern8d1c8022018-04-17 17:33:26 -07004056struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004057 const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08004058 struct net_device *dev,
4059 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060{
Thomas Graf86872cb2006-08-22 00:01:08 -07004061 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07004062 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08004063 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07004064 .fc_ifindex = dev->ifindex,
4065 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4066 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004067 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004068 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004069 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08004070 .fc_nlinfo.nlh = NULL,
David Ahernafb1d4b52018-04-17 17:33:11 -07004071 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004072 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004074 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
David Ahernacb54e32018-04-17 17:33:22 -07004076 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
David Ahern830218c2016-10-24 10:52:35 -07004077 struct fib6_table *table;
4078
4079 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4080 if (table)
4081 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
David Ahernafb1d4b52018-04-17 17:33:11 -07004084 return rt6_get_dflt_router(net, gwaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085}
4086
David Ahernafb1d4b52018-04-17 17:33:11 -07004087static void __rt6_purge_dflt_routers(struct net *net,
4088 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089{
David Ahern8d1c8022018-04-17 17:33:26 -07004090 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
4092restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004093 rcu_read_lock();
4094 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Aherndcd1f572018-04-18 15:39:05 -07004095 struct net_device *dev = fib6_info_nh_dev(rt);
4096 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4097
David Ahern93c2fb22018-04-18 15:38:59 -07004098 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
Wei Wange873e4b2018-07-21 20:56:32 -07004099 (!idev || idev->cnf.accept_ra != 2) &&
4100 fib6_info_hold_safe(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07004101 rcu_read_unlock();
4102 ip6_del_rt(net, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 goto restart;
4104 }
4105 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07004106 rcu_read_unlock();
David Ahern830218c2016-10-24 10:52:35 -07004107
4108 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4109}
4110
4111void rt6_purge_dflt_routers(struct net *net)
4112{
4113 struct fib6_table *table;
4114 struct hlist_head *head;
4115 unsigned int h;
4116
4117 rcu_read_lock();
4118
4119 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4120 head = &net->ipv6.fib_table_hash[h];
4121 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4122 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
David Ahernafb1d4b52018-04-17 17:33:11 -07004123 __rt6_purge_dflt_routers(net, table);
David Ahern830218c2016-10-24 10:52:35 -07004124 }
4125 }
4126
4127 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128}
4129
Daniel Lezcano55786892008-03-04 13:47:47 -08004130static void rtmsg_to_fib6_config(struct net *net,
4131 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07004132 struct fib6_config *cfg)
4133{
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004134 *cfg = (struct fib6_config){
4135 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4136 : RT6_TABLE_MAIN,
4137 .fc_ifindex = rtmsg->rtmsg_ifindex,
David Ahern67f69512019-03-21 05:21:34 -07004138 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004139 .fc_expires = rtmsg->rtmsg_info,
4140 .fc_dst_len = rtmsg->rtmsg_dst_len,
4141 .fc_src_len = rtmsg->rtmsg_src_len,
4142 .fc_flags = rtmsg->rtmsg_flags,
4143 .fc_type = rtmsg->rtmsg_type,
Thomas Graf86872cb2006-08-22 00:01:08 -07004144
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004145 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004146
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004147 .fc_dst = rtmsg->rtmsg_dst,
4148 .fc_src = rtmsg->rtmsg_src,
4149 .fc_gateway = rtmsg->rtmsg_gateway,
4150 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004151}
4152
Daniel Lezcano55786892008-03-04 13:47:47 -08004153int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154{
Thomas Graf86872cb2006-08-22 00:01:08 -07004155 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 struct in6_rtmsg rtmsg;
4157 int err;
4158
Ian Morris67ba4152014-08-24 21:53:10 +01004159 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 case SIOCADDRT: /* Add a route */
4161 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00004162 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 return -EPERM;
4164 err = copy_from_user(&rtmsg, arg,
4165 sizeof(struct in6_rtmsg));
4166 if (err)
4167 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07004168
Daniel Lezcano55786892008-03-04 13:47:47 -08004169 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07004170
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 rtnl_lock();
4172 switch (cmd) {
4173 case SIOCADDRT:
David Ahernacb54e32018-04-17 17:33:22 -07004174 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 break;
4176 case SIOCDELRT:
David Ahern333c4302017-05-21 10:12:04 -06004177 err = ip6_route_del(&cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 break;
4179 default:
4180 err = -EINVAL;
4181 }
4182 rtnl_unlock();
4183
4184 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
4187 return -EINVAL;
4188}
4189
4190/*
4191 * Drop the packet on the floor
4192 */
4193
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07004194static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195{
Eric Dumazetadf30902009-06-02 05:19:30 +00004196 struct dst_entry *dst = skb_dst(skb);
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004197 struct net *net = dev_net(dst->dev);
4198 struct inet6_dev *idev;
4199 int type;
4200
4201 if (netif_is_l3_master(skb->dev) &&
4202 dst->dev == net->loopback_dev)
4203 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4204 else
4205 idev = ip6_dst_idev(dst);
4206
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004207 switch (ipstats_mib_noroutes) {
4208 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07004209 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00004210 if (type == IPV6_ADDR_ANY) {
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004211 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004212 break;
4213 }
4214 /* FALLTHROUGH */
4215 case IPSTATS_MIB_OUTNOROUTES:
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004216 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004217 break;
4218 }
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004219
4220 /* Start over by dropping the dst for l3mdev case */
4221 if (netif_is_l3_master(skb->dev))
4222 skb_dst_drop(skb);
4223
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00004224 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 kfree_skb(skb);
4226 return 0;
4227}
4228
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004229static int ip6_pkt_discard(struct sk_buff *skb)
4230{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004231 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004232}
4233
Eric W. Biedermanede20592015-10-07 16:48:47 -05004234static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235{
Eric Dumazetadf30902009-06-02 05:19:30 +00004236 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004237 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238}
4239
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004240static int ip6_pkt_prohibit(struct sk_buff *skb)
4241{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004242 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004243}
4244
Eric W. Biedermanede20592015-10-07 16:48:47 -05004245static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004246{
Eric Dumazetadf30902009-06-02 05:19:30 +00004247 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004248 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004249}
4250
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251/*
4252 * Allocate a dst for local (unicast / anycast) address.
4253 */
4254
David Ahern360a9882018-04-18 15:39:00 -07004255struct fib6_info *addrconf_f6i_alloc(struct net *net,
4256 struct inet6_dev *idev,
4257 const struct in6_addr *addr,
4258 bool anycast, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259{
David Ahernc7a1ce32019-03-21 05:21:35 -07004260 struct fib6_config cfg = {
4261 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4262 .fc_ifindex = idev->dev->ifindex,
4263 .fc_flags = RTF_UP | RTF_ADDRCONF | RTF_NONEXTHOP,
4264 .fc_dst = *addr,
4265 .fc_dst_len = 128,
4266 .fc_protocol = RTPROT_KERNEL,
4267 .fc_nlinfo.nl_net = net,
4268 .fc_ignore_dev_down = true,
4269 };
David Ahern5f02ce242016-09-10 12:09:54 -07004270
David Aherne8478e82018-04-17 17:33:13 -07004271 if (anycast) {
David Ahernc7a1ce32019-03-21 05:21:35 -07004272 cfg.fc_type = RTN_ANYCAST;
4273 cfg.fc_flags |= RTF_ANYCAST;
David Aherne8478e82018-04-17 17:33:13 -07004274 } else {
David Ahernc7a1ce32019-03-21 05:21:35 -07004275 cfg.fc_type = RTN_LOCAL;
4276 cfg.fc_flags |= RTF_LOCAL;
David Aherne8478e82018-04-17 17:33:13 -07004277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
David Ahernc7a1ce32019-03-21 05:21:35 -07004279 return ip6_route_info_create(&cfg, gfp_flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280}
4281
Daniel Walterc3968a82011-04-13 21:10:57 +00004282/* remove deleted ip from prefsrc entries */
4283struct arg_dev_net_ip {
4284 struct net_device *dev;
4285 struct net *net;
4286 struct in6_addr *addr;
4287};
4288
David Ahern8d1c8022018-04-17 17:33:26 -07004289static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
Daniel Walterc3968a82011-04-13 21:10:57 +00004290{
4291 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4292 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4293 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4294
David Ahernf88d8ea2019-06-03 20:19:52 -07004295 if (!rt->nh &&
4296 ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
David Ahern421842e2018-04-17 17:33:18 -07004297 rt != net->ipv6.fib6_null_entry &&
David Ahern93c2fb22018-04-18 15:38:59 -07004298 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
Wei Wang60006a42017-10-06 12:05:58 -07004299 spin_lock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004300 /* remove prefsrc entry */
David Ahern93c2fb22018-04-18 15:38:59 -07004301 rt->fib6_prefsrc.plen = 0;
Wei Wang60006a42017-10-06 12:05:58 -07004302 spin_unlock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004303 }
4304 return 0;
4305}
4306
4307void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4308{
4309 struct net *net = dev_net(ifp->idev->dev);
4310 struct arg_dev_net_ip adni = {
4311 .dev = ifp->idev->dev,
4312 .net = net,
4313 .addr = &ifp->addr,
4314 };
Li RongQing0c3584d2013-12-27 16:32:38 +08004315 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00004316}
4317
David Ahern2b2450c2019-03-27 20:53:52 -07004318#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004319
4320/* Remove routers and update dst entries when gateway turn into host. */
David Ahern8d1c8022018-04-17 17:33:26 -07004321static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004322{
4323 struct in6_addr *gateway = (struct in6_addr *)arg;
David Ahernf88d8ea2019-06-03 20:19:52 -07004324 struct fib6_nh *nh;
Duan Jiongbe7a0102014-05-15 15:56:14 +08004325
David Ahernf88d8ea2019-06-03 20:19:52 -07004326 /* RA routes do not use nexthops */
4327 if (rt->nh)
4328 return 0;
4329
4330 nh = rt->fib6_nh;
David Ahern93c2fb22018-04-18 15:38:59 -07004331 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Aherncc5c0732019-05-22 20:27:58 -07004332 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
Duan Jiongbe7a0102014-05-15 15:56:14 +08004333 return -1;
Wei Wangb16cb452017-10-06 12:06:00 -07004334
4335 /* Further clean up cached routes in exception table.
4336 * This is needed because cached route may have a different
4337 * gateway than its 'parent' in the case of an ip redirect.
4338 */
David Aherncc5c0732019-05-22 20:27:58 -07004339 fib6_nh_exceptions_clean_tohost(nh, gateway);
Wei Wangb16cb452017-10-06 12:06:00 -07004340
Duan Jiongbe7a0102014-05-15 15:56:14 +08004341 return 0;
4342}
4343
4344void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4345{
4346 fib6_clean_all(net, fib6_clean_tohost, gateway);
4347}
4348
Ido Schimmel2127d952018-01-07 12:45:03 +02004349struct arg_netdev_event {
4350 const struct net_device *dev;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004351 union {
David Ahernecc56632019-04-23 08:48:09 -07004352 unsigned char nh_flags;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004353 unsigned long event;
4354 };
Ido Schimmel2127d952018-01-07 12:45:03 +02004355};
4356
David Ahern8d1c8022018-04-17 17:33:26 -07004357static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004358{
David Ahern8d1c8022018-04-17 17:33:26 -07004359 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004360 struct fib6_node *fn;
4361
David Ahern93c2fb22018-04-18 15:38:59 -07004362 fn = rcu_dereference_protected(rt->fib6_node,
4363 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004364 iter = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -07004365 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004366 while (iter) {
David Ahern93c2fb22018-04-18 15:38:59 -07004367 if (iter->fib6_metric == rt->fib6_metric &&
David Ahern33bd5ac2018-07-03 14:36:21 -07004368 rt6_qualify_for_ecmp(iter))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004369 return iter;
David Ahern8fb11a92018-05-04 13:54:24 -07004370 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -07004371 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004372 }
4373
4374 return NULL;
4375}
4376
David Ahernf88d8ea2019-06-03 20:19:52 -07004377/* only called for fib entries with builtin fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004378static bool rt6_is_dead(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004379{
David Ahern1cf844c2019-05-22 20:27:59 -07004380 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4381 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4382 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004383 return true;
4384
4385 return false;
4386}
4387
David Ahern8d1c8022018-04-17 17:33:26 -07004388static int rt6_multipath_total_weight(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004389{
David Ahern8d1c8022018-04-17 17:33:26 -07004390 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004391 int total = 0;
4392
4393 if (!rt6_is_dead(rt))
David Ahern1cf844c2019-05-22 20:27:59 -07004394 total += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004395
David Ahern93c2fb22018-04-18 15:38:59 -07004396 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004397 if (!rt6_is_dead(iter))
David Ahern1cf844c2019-05-22 20:27:59 -07004398 total += iter->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004399 }
4400
4401 return total;
4402}
4403
David Ahern8d1c8022018-04-17 17:33:26 -07004404static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004405{
4406 int upper_bound = -1;
4407
4408 if (!rt6_is_dead(rt)) {
David Ahern1cf844c2019-05-22 20:27:59 -07004409 *weight += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004410 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4411 total) - 1;
4412 }
David Ahern1cf844c2019-05-22 20:27:59 -07004413 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004414}
4415
David Ahern8d1c8022018-04-17 17:33:26 -07004416static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004417{
David Ahern8d1c8022018-04-17 17:33:26 -07004418 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004419 int weight = 0;
4420
4421 rt6_upper_bound_set(rt, &weight, total);
4422
David Ahern93c2fb22018-04-18 15:38:59 -07004423 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004424 rt6_upper_bound_set(iter, &weight, total);
4425}
4426
David Ahern8d1c8022018-04-17 17:33:26 -07004427void rt6_multipath_rebalance(struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004428{
David Ahern8d1c8022018-04-17 17:33:26 -07004429 struct fib6_info *first;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004430 int total;
4431
4432 /* In case the entire multipath route was marked for flushing,
4433 * then there is no need to rebalance upon the removal of every
4434 * sibling route.
4435 */
David Ahern93c2fb22018-04-18 15:38:59 -07004436 if (!rt->fib6_nsiblings || rt->should_flush)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004437 return;
4438
4439 /* During lookup routes are evaluated in order, so we need to
4440 * make sure upper bounds are assigned from the first sibling
4441 * onwards.
4442 */
4443 first = rt6_multipath_first_sibling(rt);
4444 if (WARN_ON_ONCE(!first))
4445 return;
4446
4447 total = rt6_multipath_total_weight(first);
4448 rt6_multipath_upper_bound_set(first, total);
4449}
4450
David Ahern8d1c8022018-04-17 17:33:26 -07004451static int fib6_ifup(struct fib6_info *rt, void *p_arg)
Ido Schimmel2127d952018-01-07 12:45:03 +02004452{
4453 const struct arg_netdev_event *arg = p_arg;
David Ahern7aef6852018-04-17 17:33:10 -07004454 struct net *net = dev_net(arg->dev);
Ido Schimmel2127d952018-01-07 12:45:03 +02004455
David Ahernf88d8ea2019-06-03 20:19:52 -07004456 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
David Ahern1cf844c2019-05-22 20:27:59 -07004457 rt->fib6_nh->fib_nh_dev == arg->dev) {
4458 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
David Ahern7aef6852018-04-17 17:33:10 -07004459 fib6_update_sernum_upto_root(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004460 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004461 }
Ido Schimmel2127d952018-01-07 12:45:03 +02004462
4463 return 0;
4464}
4465
David Ahernecc56632019-04-23 08:48:09 -07004466void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
Ido Schimmel2127d952018-01-07 12:45:03 +02004467{
4468 struct arg_netdev_event arg = {
4469 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004470 {
4471 .nh_flags = nh_flags,
4472 },
Ido Schimmel2127d952018-01-07 12:45:03 +02004473 };
4474
4475 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4476 arg.nh_flags |= RTNH_F_LINKDOWN;
4477
4478 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4479}
4480
David Ahernf88d8ea2019-06-03 20:19:52 -07004481/* only called for fib entries with inline fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004482static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004483 const struct net_device *dev)
4484{
David Ahern8d1c8022018-04-17 17:33:26 -07004485 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004486
David Ahern1cf844c2019-05-22 20:27:59 -07004487 if (rt->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004488 return true;
David Ahern93c2fb22018-04-18 15:38:59 -07004489 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004490 if (iter->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004491 return true;
4492
4493 return false;
4494}
4495
David Ahern8d1c8022018-04-17 17:33:26 -07004496static void rt6_multipath_flush(struct fib6_info *rt)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004497{
David Ahern8d1c8022018-04-17 17:33:26 -07004498 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004499
4500 rt->should_flush = 1;
David Ahern93c2fb22018-04-18 15:38:59 -07004501 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004502 iter->should_flush = 1;
4503}
4504
David Ahern8d1c8022018-04-17 17:33:26 -07004505static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004506 const struct net_device *down_dev)
4507{
David Ahern8d1c8022018-04-17 17:33:26 -07004508 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004509 unsigned int dead = 0;
4510
David Ahern1cf844c2019-05-22 20:27:59 -07004511 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4512 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004513 dead++;
David Ahern93c2fb22018-04-18 15:38:59 -07004514 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004515 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4516 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004517 dead++;
4518
4519 return dead;
4520}
4521
David Ahern8d1c8022018-04-17 17:33:26 -07004522static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004523 const struct net_device *dev,
David Ahernecc56632019-04-23 08:48:09 -07004524 unsigned char nh_flags)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004525{
David Ahern8d1c8022018-04-17 17:33:26 -07004526 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004527
David Ahern1cf844c2019-05-22 20:27:59 -07004528 if (rt->fib6_nh->fib_nh_dev == dev)
4529 rt->fib6_nh->fib_nh_flags |= nh_flags;
David Ahern93c2fb22018-04-18 15:38:59 -07004530 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004531 if (iter->fib6_nh->fib_nh_dev == dev)
4532 iter->fib6_nh->fib_nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004533}
4534
David Aherna1a22c12017-01-18 07:40:36 -08004535/* called with write lock held for table with rt */
David Ahern8d1c8022018-04-17 17:33:26 -07004536static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004538 const struct arg_netdev_event *arg = p_arg;
4539 const struct net_device *dev = arg->dev;
David Ahern7aef6852018-04-17 17:33:10 -07004540 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004541
David Ahernf88d8ea2019-06-03 20:19:52 -07004542 if (rt == net->ipv6.fib6_null_entry || rt->nh)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004543 return 0;
4544
4545 switch (arg->event) {
4546 case NETDEV_UNREGISTER:
David Ahern1cf844c2019-05-22 20:27:59 -07004547 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004548 case NETDEV_DOWN:
Ido Schimmel1de178e2018-01-07 12:45:15 +02004549 if (rt->should_flush)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004550 return -1;
David Ahern93c2fb22018-04-18 15:38:59 -07004551 if (!rt->fib6_nsiblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004552 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004553 if (rt6_multipath_uses_dev(rt, dev)) {
4554 unsigned int count;
4555
4556 count = rt6_multipath_dead_count(rt, dev);
David Ahern93c2fb22018-04-18 15:38:59 -07004557 if (rt->fib6_nsiblings + 1 == count) {
Ido Schimmel1de178e2018-01-07 12:45:15 +02004558 rt6_multipath_flush(rt);
4559 return -1;
4560 }
4561 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4562 RTNH_F_LINKDOWN);
David Ahern7aef6852018-04-17 17:33:10 -07004563 fib6_update_sernum(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004564 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004565 }
4566 return -2;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004567 case NETDEV_CHANGE:
David Ahern1cf844c2019-05-22 20:27:59 -07004568 if (rt->fib6_nh->fib_nh_dev != dev ||
David Ahern93c2fb22018-04-18 15:38:59 -07004569 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004570 break;
David Ahern1cf844c2019-05-22 20:27:59 -07004571 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004572 rt6_multipath_rebalance(rt);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004573 break;
Ido Schimmel2b241362018-01-07 12:45:02 +02004574 }
David S. Millerc159d302011-12-26 15:24:36 -05004575
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 return 0;
4577}
4578
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004579void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004581 struct arg_netdev_event arg = {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004582 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004583 {
4584 .event = event,
4585 },
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004586 };
David Ahern7c6bb7d2018-10-11 20:17:21 -07004587 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004588
David Ahern7c6bb7d2018-10-11 20:17:21 -07004589 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4590 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4591 else
4592 fib6_clean_all(net, fib6_ifdown, &arg);
Ido Schimmel4c981e22018-01-07 12:45:04 +02004593}
4594
4595void rt6_disable_ip(struct net_device *dev, unsigned long event)
4596{
4597 rt6_sync_down_dev(dev, event);
4598 rt6_uncached_list_flush_dev(dev_net(dev), dev);
4599 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600}
4601
Eric Dumazet95c96172012-04-15 05:58:06 +00004602struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00004604 unsigned int mtu;
David Ahernc0b220c2019-05-22 20:27:57 -07004605 struct fib6_info *f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606};
4607
David Aherncc5c0732019-05-22 20:27:58 -07004608static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
David Ahernc0b220c2019-05-22 20:27:57 -07004609{
4610 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
David Aherncc5c0732019-05-22 20:27:58 -07004611 struct fib6_info *f6i = arg->f6i;
David Ahernc0b220c2019-05-22 20:27:57 -07004612
4613 /* For administrative MTU increase, there is no way to discover
4614 * IPv6 PMTU increase, so PMTU increase should be updated here.
4615 * Since RFC 1981 doesn't include administrative MTU increase
4616 * update PMTU increase is a MUST. (i.e. jumbo frame)
4617 */
4618 if (nh->fib_nh_dev == arg->dev) {
4619 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4620 u32 mtu = f6i->fib6_pmtu;
4621
4622 if (mtu >= arg->mtu ||
4623 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4624 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4625
4626 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07004627 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
David Ahernc0b220c2019-05-22 20:27:57 -07004628 spin_unlock_bh(&rt6_exception_lock);
4629 }
4630
4631 return 0;
4632}
4633
4634static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635{
4636 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4637 struct inet6_dev *idev;
4638
4639 /* In IPv6 pmtu discovery is not optional,
4640 so that RTAX_MTU lock cannot disable it.
4641 We still use this lock to block changes
4642 caused by addrconf/ndisc.
4643 */
4644
4645 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05004646 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 return 0;
4648
David Ahernc0b220c2019-05-22 20:27:57 -07004649 if (fib6_metric_locked(f6i, RTAX_MTU))
4650 return 0;
David Ahernd4ead6b2018-04-17 17:33:16 -07004651
David Ahernc0b220c2019-05-22 20:27:57 -07004652 arg->f6i = f6i;
David Ahern1cf844c2019-05-22 20:27:59 -07004653 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654}
4655
Eric Dumazet95c96172012-04-15 05:58:06 +00004656void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657{
Thomas Grafc71099a2006-08-04 23:20:06 -07004658 struct rt6_mtu_change_arg arg = {
4659 .dev = dev,
4660 .mtu = mtu,
4661 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Li RongQing0c3584d2013-12-27 16:32:38 +08004663 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664}
4665
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004666static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
David Ahern75425652019-05-22 12:07:43 -07004667 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
Thomas Graf5176f912006-08-26 20:13:18 -07004668 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004669 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07004670 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07004671 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004672 [RTA_PRIORITY] = { .type = NLA_U32 },
4673 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004674 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004675 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004676 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4677 [RTA_ENCAP] = { .type = NLA_NESTED },
Xin Long32bc2012015-12-16 17:50:11 +08004678 [RTA_EXPIRES] = { .type = NLA_U32 },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004679 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +08004680 [RTA_MARK] = { .type = NLA_U32 },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004681 [RTA_TABLE] = { .type = NLA_U32 },
Roopa Prabhueacb9382018-05-22 14:03:28 -07004682 [RTA_IP_PROTO] = { .type = NLA_U8 },
4683 [RTA_SPORT] = { .type = NLA_U16 },
4684 [RTA_DPORT] = { .type = NLA_U16 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004685};
4686
4687static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
David Ahern333c4302017-05-21 10:12:04 -06004688 struct fib6_config *cfg,
4689 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690{
Thomas Graf86872cb2006-08-22 00:01:08 -07004691 struct rtmsg *rtm;
4692 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004693 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07004694 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
Johannes Berg8cb08172019-04-26 14:07:28 +02004696 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
4697 rtm_ipv6_policy, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004698 if (err < 0)
4699 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
Thomas Graf86872cb2006-08-22 00:01:08 -07004701 err = -EINVAL;
4702 rtm = nlmsg_data(nlh);
Thomas Graf86872cb2006-08-22 00:01:08 -07004703
Maciej Żenczykowski84db8402018-09-29 23:44:53 -07004704 *cfg = (struct fib6_config){
4705 .fc_table = rtm->rtm_table,
4706 .fc_dst_len = rtm->rtm_dst_len,
4707 .fc_src_len = rtm->rtm_src_len,
4708 .fc_flags = RTF_UP,
4709 .fc_protocol = rtm->rtm_protocol,
4710 .fc_type = rtm->rtm_type,
4711
4712 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
4713 .fc_nlinfo.nlh = nlh,
4714 .fc_nlinfo.nl_net = sock_net(skb->sk),
4715 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004716
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004717 if (rtm->rtm_type == RTN_UNREACHABLE ||
4718 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00004719 rtm->rtm_type == RTN_PROHIBIT ||
4720 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07004721 cfg->fc_flags |= RTF_REJECT;
4722
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00004723 if (rtm->rtm_type == RTN_LOCAL)
4724 cfg->fc_flags |= RTF_LOCAL;
4725
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07004726 if (rtm->rtm_flags & RTM_F_CLONED)
4727 cfg->fc_flags |= RTF_CACHE;
4728
David Ahernfc1e64e2018-01-25 16:55:09 -08004729 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4730
Thomas Graf86872cb2006-08-22 00:01:08 -07004731 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004732 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07004733 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 }
David Aherne3818542019-02-26 09:00:03 -08004735 if (tb[RTA_VIA]) {
4736 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4737 goto errout;
4738 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004739
4740 if (tb[RTA_DST]) {
4741 int plen = (rtm->rtm_dst_len + 7) >> 3;
4742
4743 if (nla_len(tb[RTA_DST]) < plen)
4744 goto errout;
4745
4746 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004748
4749 if (tb[RTA_SRC]) {
4750 int plen = (rtm->rtm_src_len + 7) >> 3;
4751
4752 if (nla_len(tb[RTA_SRC]) < plen)
4753 goto errout;
4754
4755 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004757
Daniel Walterc3968a82011-04-13 21:10:57 +00004758 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02004759 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00004760
Thomas Graf86872cb2006-08-22 00:01:08 -07004761 if (tb[RTA_OIF])
4762 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4763
4764 if (tb[RTA_PRIORITY])
4765 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4766
4767 if (tb[RTA_METRICS]) {
4768 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4769 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004771
4772 if (tb[RTA_TABLE])
4773 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4774
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004775 if (tb[RTA_MULTIPATH]) {
4776 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4777 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
David Ahern9ed59592017-01-17 14:57:36 -08004778
4779 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
David Ahernc255bd62017-05-27 16:19:27 -06004780 cfg->fc_mp_len, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004781 if (err < 0)
4782 goto errout;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004783 }
4784
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004785 if (tb[RTA_PREF]) {
4786 pref = nla_get_u8(tb[RTA_PREF]);
4787 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4788 pref != ICMPV6_ROUTER_PREF_HIGH)
4789 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4790 cfg->fc_flags |= RTF_PREF(pref);
4791 }
4792
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004793 if (tb[RTA_ENCAP])
4794 cfg->fc_encap = tb[RTA_ENCAP];
4795
David Ahern9ed59592017-01-17 14:57:36 -08004796 if (tb[RTA_ENCAP_TYPE]) {
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004797 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
4798
David Ahernc255bd62017-05-27 16:19:27 -06004799 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004800 if (err < 0)
4801 goto errout;
4802 }
4803
Xin Long32bc2012015-12-16 17:50:11 +08004804 if (tb[RTA_EXPIRES]) {
4805 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
4806
4807 if (addrconf_finite_timeout(timeout)) {
4808 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
4809 cfg->fc_flags |= RTF_EXPIRES;
4810 }
4811 }
4812
Thomas Graf86872cb2006-08-22 00:01:08 -07004813 err = 0;
4814errout:
4815 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816}
4817
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004818struct rt6_nh {
David Ahern8d1c8022018-04-17 17:33:26 -07004819 struct fib6_info *fib6_info;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004820 struct fib6_config r_cfg;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004821 struct list_head next;
4822};
4823
David Ahernd4ead6b2018-04-17 17:33:16 -07004824static int ip6_route_info_append(struct net *net,
4825 struct list_head *rt6_nh_list,
David Ahern8d1c8022018-04-17 17:33:26 -07004826 struct fib6_info *rt,
4827 struct fib6_config *r_cfg)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004828{
4829 struct rt6_nh *nh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004830 int err = -EEXIST;
4831
4832 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004833 /* check if fib6_info already exists */
4834 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004835 return err;
4836 }
4837
4838 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
4839 if (!nh)
4840 return -ENOMEM;
David Ahern8d1c8022018-04-17 17:33:26 -07004841 nh->fib6_info = rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004842 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
4843 list_add_tail(&nh->next, rt6_nh_list);
4844
4845 return 0;
4846}
4847
David Ahern8d1c8022018-04-17 17:33:26 -07004848static void ip6_route_mpath_notify(struct fib6_info *rt,
4849 struct fib6_info *rt_last,
David Ahern3b1137f2017-02-02 12:37:10 -08004850 struct nl_info *info,
4851 __u16 nlflags)
4852{
4853 /* if this is an APPEND route, then rt points to the first route
4854 * inserted and rt_last points to last route inserted. Userspace
4855 * wants a consistent dump of the route which starts at the first
4856 * nexthop. Since sibling routes are always added at the end of
4857 * the list, find the first sibling of the last route appended
4858 */
David Ahern93c2fb22018-04-18 15:38:59 -07004859 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
4860 rt = list_first_entry(&rt_last->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -07004861 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -07004862 fib6_siblings);
David Ahern3b1137f2017-02-02 12:37:10 -08004863 }
4864
4865 if (rt)
4866 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
4867}
4868
David Ahern333c4302017-05-21 10:12:04 -06004869static int ip6_route_multipath_add(struct fib6_config *cfg,
4870 struct netlink_ext_ack *extack)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004871{
David Ahern8d1c8022018-04-17 17:33:26 -07004872 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
David Ahern3b1137f2017-02-02 12:37:10 -08004873 struct nl_info *info = &cfg->fc_nlinfo;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004874 struct fib6_config r_cfg;
4875 struct rtnexthop *rtnh;
David Ahern8d1c8022018-04-17 17:33:26 -07004876 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004877 struct rt6_nh *err_nh;
4878 struct rt6_nh *nh, *nh_safe;
David Ahern3b1137f2017-02-02 12:37:10 -08004879 __u16 nlflags;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004880 int remaining;
4881 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004882 int err = 1;
4883 int nhn = 0;
4884 int replace = (cfg->fc_nlinfo.nlh &&
4885 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
4886 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004887
David Ahern3b1137f2017-02-02 12:37:10 -08004888 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
4889 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
4890 nlflags |= NLM_F_APPEND;
4891
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02004892 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004893 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004894
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004895 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
David Ahern8d1c8022018-04-17 17:33:26 -07004896 * fib6_info structs per nexthop
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004897 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004898 while (rtnh_ok(rtnh, remaining)) {
4899 memcpy(&r_cfg, cfg, sizeof(*cfg));
4900 if (rtnh->rtnh_ifindex)
4901 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
4902
4903 attrlen = rtnh_attrlen(rtnh);
4904 if (attrlen > 0) {
4905 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4906
4907 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
4908 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004909 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004910 r_cfg.fc_flags |= RTF_GATEWAY;
4911 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004912 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
4913 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
4914 if (nla)
4915 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004916 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004917
David Ahern68e2ffd2018-03-20 10:06:59 -07004918 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
David Ahernacb54e32018-04-17 17:33:22 -07004919 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07004920 if (IS_ERR(rt)) {
4921 err = PTR_ERR(rt);
4922 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004923 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07004924 }
David Ahernb5d2d752018-07-15 09:35:19 -07004925 if (!rt6_qualify_for_ecmp(rt)) {
4926 err = -EINVAL;
4927 NL_SET_ERR_MSG(extack,
4928 "Device only routes can not be added for IPv6 using the multipath API.");
4929 fib6_info_release(rt);
4930 goto cleanup;
4931 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004932
David Ahern1cf844c2019-05-22 20:27:59 -07004933 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
Ido Schimmel398958a2018-01-09 16:40:28 +02004934
David Ahernd4ead6b2018-04-17 17:33:16 -07004935 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4936 rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004937 if (err) {
David Ahern93531c62018-04-17 17:33:25 -07004938 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004939 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004940 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004941
4942 rtnh = rtnh_next(rtnh, &remaining);
4943 }
4944
David Ahern3b1137f2017-02-02 12:37:10 -08004945 /* for add and replace send one notification with all nexthops.
4946 * Skip the notification in fib6_add_rt2node and send one with
4947 * the full route when done
4948 */
4949 info->skip_notify = 1;
4950
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004951 err_nh = NULL;
4952 list_for_each_entry(nh, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004953 err = __ip6_ins_rt(nh->fib6_info, info, extack);
4954 fib6_info_release(nh->fib6_info);
David Ahern3b1137f2017-02-02 12:37:10 -08004955
David Ahernf7225172018-06-04 13:41:42 -07004956 if (!err) {
4957 /* save reference to last route successfully inserted */
4958 rt_last = nh->fib6_info;
4959
4960 /* save reference to first route for notification */
4961 if (!rt_notif)
4962 rt_notif = nh->fib6_info;
4963 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004964
David Ahern8d1c8022018-04-17 17:33:26 -07004965 /* nh->fib6_info is used or freed at this point, reset to NULL*/
4966 nh->fib6_info = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004967 if (err) {
4968 if (replace && nhn)
Jakub Kicinskia5a82d82019-01-14 10:52:45 -08004969 NL_SET_ERR_MSG_MOD(extack,
4970 "multipath route replace failed (check consistency of installed routes)");
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004971 err_nh = nh;
4972 goto add_errout;
4973 }
4974
Nicolas Dichtel1a724182012-11-01 22:58:22 +00004975 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02004976 * these flags after the first nexthop: if there is a collision,
4977 * we have already failed to add the first nexthop:
4978 * fib6_add_rt2node() has rejected it; when replacing, old
4979 * nexthops have been replaced by first new, the rest should
4980 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00004981 */
Michal Kubeček27596472015-05-18 20:54:00 +02004982 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
4983 NLM_F_REPLACE);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004984 nhn++;
4985 }
4986
David Ahern3b1137f2017-02-02 12:37:10 -08004987 /* success ... tell user about new route */
4988 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004989 goto cleanup;
4990
4991add_errout:
David Ahern3b1137f2017-02-02 12:37:10 -08004992 /* send notification for routes that were added so that
4993 * the delete notifications sent by ip6_route_del are
4994 * coherent
4995 */
4996 if (rt_notif)
4997 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
4998
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004999 /* Delete routes that were already added */
5000 list_for_each_entry(nh, &rt6_nh_list, next) {
5001 if (err_nh == nh)
5002 break;
David Ahern333c4302017-05-21 10:12:04 -06005003 ip6_route_del(&nh->r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005004 }
5005
5006cleanup:
5007 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005008 if (nh->fib6_info)
5009 fib6_info_release(nh->fib6_info);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005010 list_del(&nh->next);
5011 kfree(nh);
5012 }
5013
5014 return err;
5015}
5016
David Ahern333c4302017-05-21 10:12:04 -06005017static int ip6_route_multipath_del(struct fib6_config *cfg,
5018 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005019{
5020 struct fib6_config r_cfg;
5021 struct rtnexthop *rtnh;
5022 int remaining;
5023 int attrlen;
5024 int err = 1, last_err = 0;
5025
5026 remaining = cfg->fc_mp_len;
5027 rtnh = (struct rtnexthop *)cfg->fc_mp;
5028
5029 /* Parse a Multipath Entry */
5030 while (rtnh_ok(rtnh, remaining)) {
5031 memcpy(&r_cfg, cfg, sizeof(*cfg));
5032 if (rtnh->rtnh_ifindex)
5033 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5034
5035 attrlen = rtnh_attrlen(rtnh);
5036 if (attrlen > 0) {
5037 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5038
5039 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5040 if (nla) {
5041 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
5042 r_cfg.fc_flags |= RTF_GATEWAY;
5043 }
5044 }
David Ahern333c4302017-05-21 10:12:04 -06005045 err = ip6_route_del(&r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005046 if (err)
5047 last_err = err;
5048
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005049 rtnh = rtnh_next(rtnh, &remaining);
5050 }
5051
5052 return last_err;
5053}
5054
David Ahernc21ef3e2017-04-16 09:48:24 -07005055static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5056 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057{
Thomas Graf86872cb2006-08-22 00:01:08 -07005058 struct fib6_config cfg;
5059 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
David Ahern333c4302017-05-21 10:12:04 -06005061 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005062 if (err < 0)
5063 return err;
5064
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005065 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005066 return ip6_route_multipath_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005067 else {
5068 cfg.fc_delete_all_nh = 1;
David Ahern333c4302017-05-21 10:12:04 -06005069 return ip6_route_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071}
5072
David Ahernc21ef3e2017-04-16 09:48:24 -07005073static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5074 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075{
Thomas Graf86872cb2006-08-22 00:01:08 -07005076 struct fib6_config cfg;
5077 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078
David Ahern333c4302017-05-21 10:12:04 -06005079 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005080 if (err < 0)
5081 return err;
5082
David Ahern67f69512019-03-21 05:21:34 -07005083 if (cfg.fc_metric == 0)
5084 cfg.fc_metric = IP6_RT_PRIO_USER;
5085
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005086 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005087 return ip6_route_multipath_add(&cfg, extack);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005088 else
David Ahernacb54e32018-04-17 17:33:22 -07005089 return ip6_route_add(&cfg, GFP_KERNEL, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090}
5091
David Aherna1b7a1f2019-06-08 14:53:26 -07005092/* add the overhead of this fib6_nh to nexthop_len */
5093static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
Thomas Graf339bf982006-11-10 14:10:15 -08005094{
David Aherna1b7a1f2019-06-08 14:53:26 -07005095 int *nexthop_len = arg;
David Ahernbeb1afac52017-02-02 12:37:09 -08005096
David Aherna1b7a1f2019-06-08 14:53:26 -07005097 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5098 + NLA_ALIGN(sizeof(struct rtnexthop))
5099 + nla_total_size(16); /* RTA_GATEWAY */
David Ahernf88d8ea2019-06-03 20:19:52 -07005100
David Aherna1b7a1f2019-06-08 14:53:26 -07005101 if (nh->fib_nh_lws) {
5102 /* RTA_ENCAP_TYPE */
5103 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5104 /* RTA_ENCAP */
5105 *nexthop_len += nla_total_size(2);
5106 }
David Ahernbeb1afac52017-02-02 12:37:09 -08005107
David Aherna1b7a1f2019-06-08 14:53:26 -07005108 return 0;
5109}
5110
5111static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5112{
5113 int nexthop_len;
5114
5115 if (f6i->nh) {
5116 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5117 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5118 &nexthop_len);
5119 } else {
5120 struct fib6_nh *nh = f6i->fib6_nh;
5121
5122 nexthop_len = 0;
5123 if (f6i->fib6_nsiblings) {
5124 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
5125 + NLA_ALIGN(sizeof(struct rtnexthop))
5126 + nla_total_size(16) /* RTA_GATEWAY */
5127 + lwtunnel_get_encap_size(nh->fib_nh_lws);
5128
5129 nexthop_len *= f6i->fib6_nsiblings;
5130 }
5131 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
David Ahernbeb1afac52017-02-02 12:37:09 -08005132 }
5133
Thomas Graf339bf982006-11-10 14:10:15 -08005134 return NLMSG_ALIGN(sizeof(struct rtmsg))
5135 + nla_total_size(16) /* RTA_SRC */
5136 + nla_total_size(16) /* RTA_DST */
5137 + nla_total_size(16) /* RTA_GATEWAY */
5138 + nla_total_size(16) /* RTA_PREFSRC */
5139 + nla_total_size(4) /* RTA_TABLE */
5140 + nla_total_size(4) /* RTA_IIF */
5141 + nla_total_size(4) /* RTA_OIF */
5142 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08005143 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01005144 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005145 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005146 + nla_total_size(1) /* RTA_PREF */
David Ahernbeb1afac52017-02-02 12:37:09 -08005147 + nexthop_len;
5148}
5149
David Ahernf88d8ea2019-06-03 20:19:52 -07005150static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5151 unsigned char *flags)
5152{
5153 if (nexthop_is_multipath(nh)) {
5154 struct nlattr *mp;
5155
5156 mp = nla_nest_start(skb, RTA_MULTIPATH);
5157 if (!mp)
5158 goto nla_put_failure;
5159
5160 if (nexthop_mpath_fill_node(skb, nh))
5161 goto nla_put_failure;
5162
5163 nla_nest_end(skb, mp);
5164 } else {
5165 struct fib6_nh *fib6_nh;
5166
5167 fib6_nh = nexthop_fib6_nh(nh);
5168 if (fib_nexthop_info(skb, &fib6_nh->nh_common,
5169 flags, false) < 0)
5170 goto nla_put_failure;
5171 }
5172
5173 return 0;
5174
5175nla_put_failure:
5176 return -EMSGSIZE;
5177}
5178
David Ahernd4ead6b2018-04-17 17:33:16 -07005179static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -07005180 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -07005181 struct in6_addr *dest, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005182 int iif, int type, u32 portid, u32 seq,
David Ahernf8cfe2c2017-01-17 15:51:08 -08005183 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184{
Xin Long22d0bd82018-09-11 14:33:58 +08005185 struct rt6_info *rt6 = (struct rt6_info *)dst;
5186 struct rt6key *rt6_dst, *rt6_src;
5187 u32 *pmetrics, table, rt6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -07005188 unsigned char nh_flags = 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005189 struct nlmsghdr *nlh;
Xin Long22d0bd82018-09-11 14:33:58 +08005190 struct rtmsg *rtm;
David Ahernd4ead6b2018-04-17 17:33:16 -07005191 long expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192
Eric W. Biederman15e47302012-09-07 20:12:54 +00005193 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05005194 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005195 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005196
Xin Long22d0bd82018-09-11 14:33:58 +08005197 if (rt6) {
5198 rt6_dst = &rt6->rt6i_dst;
5199 rt6_src = &rt6->rt6i_src;
5200 rt6_flags = rt6->rt6i_flags;
5201 } else {
5202 rt6_dst = &rt->fib6_dst;
5203 rt6_src = &rt->fib6_src;
5204 rt6_flags = rt->fib6_flags;
5205 }
5206
Thomas Graf2d7202b2006-08-22 00:01:27 -07005207 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 rtm->rtm_family = AF_INET6;
Xin Long22d0bd82018-09-11 14:33:58 +08005209 rtm->rtm_dst_len = rt6_dst->plen;
5210 rtm->rtm_src_len = rt6_src->plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 rtm->rtm_tos = 0;
David Ahern93c2fb22018-04-18 15:38:59 -07005212 if (rt->fib6_table)
5213 table = rt->fib6_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07005214 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07005215 table = RT6_TABLE_UNSPEC;
Kalash Nainwal97f00822019-02-20 16:23:04 -08005216 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
David S. Millerc78679e2012-04-01 20:27:33 -04005217 if (nla_put_u32(skb, RTA_TABLE, table))
5218 goto nla_put_failure;
David Aherne8478e82018-04-17 17:33:13 -07005219
5220 rtm->rtm_type = rt->fib6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 rtm->rtm_flags = 0;
5222 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
David Ahern93c2fb22018-04-18 15:38:59 -07005223 rtm->rtm_protocol = rt->fib6_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
Xin Long22d0bd82018-09-11 14:33:58 +08005225 if (rt6_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 rtm->rtm_flags |= RTM_F_CLONED;
5227
David Ahernd4ead6b2018-04-17 17:33:16 -07005228 if (dest) {
5229 if (nla_put_in6_addr(skb, RTA_DST, dest))
David S. Millerc78679e2012-04-01 20:27:33 -04005230 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005231 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 } else if (rtm->rtm_dst_len)
Xin Long22d0bd82018-09-11 14:33:58 +08005233 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005234 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235#ifdef CONFIG_IPV6_SUBTREES
5236 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02005237 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04005238 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005239 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04005240 } else if (rtm->rtm_src_len &&
Xin Long22d0bd82018-09-11 14:33:58 +08005241 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005242 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005244 if (iif) {
5245#ifdef CONFIG_IPV6_MROUTE
Xin Long22d0bd82018-09-11 14:33:58 +08005246 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
David Ahernfd61c6b2017-01-17 15:51:07 -08005247 int err = ip6mr_get_route(net, skb, rtm, portid);
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02005248
David Ahernfd61c6b2017-01-17 15:51:07 -08005249 if (err == 0)
5250 return 0;
5251 if (err < 0)
5252 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005253 } else
5254#endif
David S. Millerc78679e2012-04-01 20:27:33 -04005255 if (nla_put_u32(skb, RTA_IIF, iif))
5256 goto nla_put_failure;
David Ahernd4ead6b2018-04-17 17:33:16 -07005257 } else if (dest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 struct in6_addr saddr_buf;
David Ahernd4ead6b2018-04-17 17:33:16 -07005259 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02005260 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005261 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07005263
David Ahern93c2fb22018-04-18 15:38:59 -07005264 if (rt->fib6_prefsrc.plen) {
Daniel Walterc3968a82011-04-13 21:10:57 +00005265 struct in6_addr saddr_buf;
David Ahern93c2fb22018-04-18 15:38:59 -07005266 saddr_buf = rt->fib6_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02005267 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005268 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00005269 }
5270
David Ahernd4ead6b2018-04-17 17:33:16 -07005271 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5272 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07005273 goto nla_put_failure;
5274
David Ahern93c2fb22018-04-18 15:38:59 -07005275 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
David S. Millerc78679e2012-04-01 20:27:33 -04005276 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00005277
David Ahernbeb1afac52017-02-02 12:37:09 -08005278 /* For multipath routes, walk the siblings list and add
5279 * each as a nexthop within RTA_MULTIPATH.
5280 */
Xin Long22d0bd82018-09-11 14:33:58 +08005281 if (rt6) {
5282 if (rt6_flags & RTF_GATEWAY &&
5283 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5284 goto nla_put_failure;
5285
5286 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5287 goto nla_put_failure;
5288 } else if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07005289 struct fib6_info *sibling, *next_sibling;
David Ahernbeb1afac52017-02-02 12:37:09 -08005290 struct nlattr *mp;
5291
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005292 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernbeb1afac52017-02-02 12:37:09 -08005293 if (!mp)
5294 goto nla_put_failure;
5295
David Ahern1cf844c2019-05-22 20:27:59 -07005296 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
5297 rt->fib6_nh->fib_nh_weight) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005298 goto nla_put_failure;
5299
5300 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07005301 &rt->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005302 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
5303 sibling->fib6_nh->fib_nh_weight) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005304 goto nla_put_failure;
5305 }
5306
5307 nla_nest_end(skb, mp);
David Ahernf88d8ea2019-06-03 20:19:52 -07005308 } else if (rt->nh) {
5309 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5310 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005311
David Ahernf88d8ea2019-06-03 20:19:52 -07005312 if (nexthop_is_blackhole(rt->nh))
5313 rtm->rtm_type = RTN_BLACKHOLE;
5314
5315 if (rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
5316 goto nla_put_failure;
5317
5318 rtm->rtm_flags |= nh_flags;
5319 } else {
David Ahern1cf844c2019-05-22 20:27:59 -07005320 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common,
David Ahernecc56632019-04-23 08:48:09 -07005321 &nh_flags, false) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005322 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005323
5324 rtm->rtm_flags |= nh_flags;
David Ahernbeb1afac52017-02-02 12:37:09 -08005325 }
5326
Xin Long22d0bd82018-09-11 14:33:58 +08005327 if (rt6_flags & RTF_EXPIRES) {
David Ahern14895682018-04-17 17:33:17 -07005328 expires = dst ? dst->expires : rt->expires;
5329 expires -= jiffies;
5330 }
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07005331
David Ahernd4ead6b2018-04-17 17:33:16 -07005332 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08005333 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334
Xin Long22d0bd82018-09-11 14:33:58 +08005335 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005336 goto nla_put_failure;
5337
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005338
Johannes Berg053c0952015-01-16 22:09:00 +01005339 nlmsg_end(skb, nlh);
5340 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005341
5342nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005343 nlmsg_cancel(skb, nlh);
5344 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345}
5346
David Ahern2c170e02019-06-08 14:53:27 -07005347static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5348{
5349 const struct net_device *dev = arg;
5350
5351 if (nh->fib_nh_dev == dev)
5352 return 1;
5353
5354 return 0;
5355}
5356
David Ahern13e38902018-10-15 18:56:44 -07005357static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5358 const struct net_device *dev)
5359{
David Ahern2c170e02019-06-08 14:53:27 -07005360 if (f6i->nh) {
5361 struct net_device *_dev = (struct net_device *)dev;
5362
5363 return !!nexthop_for_each_fib6_nh(f6i->nh,
5364 fib6_info_nh_uses_dev,
5365 _dev);
5366 }
5367
David Ahern1cf844c2019-05-22 20:27:59 -07005368 if (f6i->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005369 return true;
5370
5371 if (f6i->fib6_nsiblings) {
5372 struct fib6_info *sibling, *next_sibling;
5373
5374 list_for_each_entry_safe(sibling, next_sibling,
5375 &f6i->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005376 if (sibling->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005377 return true;
5378 }
5379 }
5380
5381 return false;
5382}
5383
David Ahern8d1c8022018-04-17 17:33:26 -07005384int rt6_dump_route(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385{
5386 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
David Ahern13e38902018-10-15 18:56:44 -07005387 struct fib_dump_filter *filter = &arg->filter;
5388 unsigned int flags = NLM_F_MULTI;
David Ahern1f17e2f2017-01-26 13:54:08 -08005389 struct net *net = arg->net;
5390
David Ahern421842e2018-04-17 17:33:18 -07005391 if (rt == net->ipv6.fib6_null_entry)
David Ahern1f17e2f2017-01-26 13:54:08 -08005392 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393
David Ahern13e38902018-10-15 18:56:44 -07005394 if ((filter->flags & RTM_F_PREFIX) &&
5395 !(rt->fib6_flags & RTF_PREFIX_RT)) {
5396 /* success since this is not a prefix route */
5397 return 1;
5398 }
5399 if (filter->filter_set) {
5400 if ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5401 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5402 (filter->protocol && rt->fib6_protocol != filter->protocol)) {
David Ahernf8cfe2c2017-01-17 15:51:08 -08005403 return 1;
5404 }
David Ahern13e38902018-10-15 18:56:44 -07005405 flags |= NLM_F_DUMP_FILTERED;
David Ahernf8cfe2c2017-01-17 15:51:08 -08005406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407
David Ahernd4ead6b2018-04-17 17:33:16 -07005408 return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
5409 RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
David Ahern13e38902018-10-15 18:56:44 -07005410 arg->cb->nlh->nlmsg_seq, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411}
5412
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005413static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5414 const struct nlmsghdr *nlh,
5415 struct nlattr **tb,
5416 struct netlink_ext_ack *extack)
5417{
5418 struct rtmsg *rtm;
5419 int i, err;
5420
5421 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5422 NL_SET_ERR_MSG_MOD(extack,
5423 "Invalid header for get route request");
5424 return -EINVAL;
5425 }
5426
5427 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02005428 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5429 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005430
5431 rtm = nlmsg_data(nlh);
5432 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5433 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5434 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5435 rtm->rtm_type) {
5436 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5437 return -EINVAL;
5438 }
5439 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5440 NL_SET_ERR_MSG_MOD(extack,
5441 "Invalid flags for get route request");
5442 return -EINVAL;
5443 }
5444
Johannes Berg8cb08172019-04-26 14:07:28 +02005445 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5446 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005447 if (err)
5448 return err;
5449
5450 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5451 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5452 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5453 return -EINVAL;
5454 }
5455
5456 for (i = 0; i <= RTA_MAX; i++) {
5457 if (!tb[i])
5458 continue;
5459
5460 switch (i) {
5461 case RTA_SRC:
5462 case RTA_DST:
5463 case RTA_IIF:
5464 case RTA_OIF:
5465 case RTA_MARK:
5466 case RTA_UID:
5467 case RTA_SPORT:
5468 case RTA_DPORT:
5469 case RTA_IP_PROTO:
5470 break;
5471 default:
5472 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
5473 return -EINVAL;
5474 }
5475 }
5476
5477 return 0;
5478}
5479
David Ahernc21ef3e2017-04-16 09:48:24 -07005480static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5481 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005483 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07005484 struct nlattr *tb[RTA_MAX+1];
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005485 int err, iif = 0, oif = 0;
David Aherna68886a2018-04-20 15:38:02 -07005486 struct fib6_info *from;
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005487 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07005489 struct sk_buff *skb;
5490 struct rtmsg *rtm;
Maciej Żenczykowski744486d2018-09-29 23:44:54 -07005491 struct flowi6 fl6 = {};
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005492 bool fibmatch;
Thomas Grafab364a62006-08-22 00:01:47 -07005493
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005494 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Thomas Grafab364a62006-08-22 00:01:47 -07005495 if (err < 0)
5496 goto errout;
5497
5498 err = -EINVAL;
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02005499 rtm = nlmsg_data(nlh);
5500 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005501 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
Thomas Grafab364a62006-08-22 00:01:47 -07005502
5503 if (tb[RTA_SRC]) {
5504 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5505 goto errout;
5506
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005507 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07005508 }
5509
5510 if (tb[RTA_DST]) {
5511 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5512 goto errout;
5513
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005514 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07005515 }
5516
5517 if (tb[RTA_IIF])
5518 iif = nla_get_u32(tb[RTA_IIF]);
5519
5520 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005521 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07005522
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07005523 if (tb[RTA_MARK])
5524 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
5525
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09005526 if (tb[RTA_UID])
5527 fl6.flowi6_uid = make_kuid(current_user_ns(),
5528 nla_get_u32(tb[RTA_UID]));
5529 else
5530 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5531
Roopa Prabhueacb9382018-05-22 14:03:28 -07005532 if (tb[RTA_SPORT])
5533 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5534
5535 if (tb[RTA_DPORT])
5536 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5537
5538 if (tb[RTA_IP_PROTO]) {
5539 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
Hangbin Liu5e1a99e2019-02-27 16:15:29 +08005540 &fl6.flowi6_proto, AF_INET6,
5541 extack);
Roopa Prabhueacb9382018-05-22 14:03:28 -07005542 if (err)
5543 goto errout;
5544 }
5545
Thomas Grafab364a62006-08-22 00:01:47 -07005546 if (iif) {
5547 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005548 int flags = 0;
5549
Florian Westphal121622d2017-08-15 16:34:42 +02005550 rcu_read_lock();
5551
5552 dev = dev_get_by_index_rcu(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07005553 if (!dev) {
Florian Westphal121622d2017-08-15 16:34:42 +02005554 rcu_read_unlock();
Thomas Grafab364a62006-08-22 00:01:47 -07005555 err = -ENODEV;
5556 goto errout;
5557 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005558
5559 fl6.flowi6_iif = iif;
5560
5561 if (!ipv6_addr_any(&fl6.saddr))
5562 flags |= RT6_LOOKUP_F_HAS_SADDR;
5563
David Ahernb75cc8f2018-03-02 08:32:17 -08005564 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
Florian Westphal121622d2017-08-15 16:34:42 +02005565
5566 rcu_read_unlock();
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005567 } else {
5568 fl6.flowi6_oif = oif;
5569
Ido Schimmel58acfd72017-12-20 12:28:25 +02005570 dst = ip6_route_output(net, NULL, &fl6);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005571 }
5572
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005573
5574 rt = container_of(dst, struct rt6_info, dst);
5575 if (rt->dst.error) {
5576 err = rt->dst.error;
5577 ip6_rt_put(rt);
5578 goto errout;
Thomas Grafab364a62006-08-22 00:01:47 -07005579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580
WANG Cong9d6acb32017-03-01 20:48:39 -08005581 if (rt == net->ipv6.ip6_null_entry) {
5582 err = rt->dst.error;
5583 ip6_rt_put(rt);
5584 goto errout;
5585 }
5586
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05005588 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00005589 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07005590 err = -ENOBUFS;
5591 goto errout;
5592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593
Changli Gaod8d1f302010-06-10 23:31:35 -07005594 skb_dst_set(skb, &rt->dst);
David Aherna68886a2018-04-20 15:38:02 -07005595
5596 rcu_read_lock();
5597 from = rcu_dereference(rt->from);
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07005598 if (from) {
5599 if (fibmatch)
5600 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5601 iif, RTM_NEWROUTE,
5602 NETLINK_CB(in_skb).portid,
5603 nlh->nlmsg_seq, 0);
5604 else
5605 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5606 &fl6.saddr, iif, RTM_NEWROUTE,
5607 NETLINK_CB(in_skb).portid,
5608 nlh->nlmsg_seq, 0);
5609 } else {
5610 err = -ENETUNREACH;
5611 }
David Aherna68886a2018-04-20 15:38:02 -07005612 rcu_read_unlock();
5613
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07005615 kfree_skb(skb);
5616 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 }
5618
Eric W. Biederman15e47302012-09-07 20:12:54 +00005619 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07005620errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622}
5623
David Ahern8d1c8022018-04-17 17:33:26 -07005624void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07005625 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626{
5627 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08005628 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08005629 u32 seq;
5630 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08005632 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05005633 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07005634
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005635 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05005636 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07005637 goto errout;
5638
David Ahernd4ead6b2018-04-17 17:33:16 -07005639 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5640 event, info->portid, seq, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08005641 if (err < 0) {
5642 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
5643 WARN_ON(err == -EMSGSIZE);
5644 kfree_skb(skb);
5645 goto errout;
5646 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00005647 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005648 info->nlh, gfp_any());
5649 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07005650errout:
5651 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08005652 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653}
5654
David Ahern19a3b7e2019-05-22 12:04:41 -07005655void fib6_rt_update(struct net *net, struct fib6_info *rt,
5656 struct nl_info *info)
5657{
5658 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
5659 struct sk_buff *skb;
5660 int err = -ENOBUFS;
5661
5662 /* call_fib6_entry_notifiers will be removed when in-kernel notifier
5663 * is implemented and supported for nexthop objects
5664 */
5665 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
5666
5667 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
5668 if (!skb)
5669 goto errout;
5670
5671 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5672 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
5673 if (err < 0) {
5674 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
5675 WARN_ON(err == -EMSGSIZE);
5676 kfree_skb(skb);
5677 goto errout;
5678 }
5679 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
5680 info->nlh, gfp_any());
5681 return;
5682errout:
5683 if (err < 0)
5684 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
5685}
5686
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005687static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00005688 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005689{
Jiri Pirko351638e2013-05-28 01:30:21 +00005690 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005691 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005692
WANG Cong242d3a42017-05-08 10:12:13 -07005693 if (!(dev->flags & IFF_LOOPBACK))
5694 return NOTIFY_OK;
5695
5696 if (event == NETDEV_REGISTER) {
David Ahern1cf844c2019-05-22 20:27:59 -07005697 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
Changli Gaod8d1f302010-06-10 23:31:35 -07005698 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005699 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
5700#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07005701 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005702 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07005703 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005704 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
5705#endif
WANG Cong76da0702017-06-20 11:42:27 -07005706 } else if (event == NETDEV_UNREGISTER &&
5707 dev->reg_state != NETREG_UNREGISTERED) {
5708 /* NETDEV_UNREGISTER could be fired for multiple times by
5709 * netdev_wait_allrefs(). Make sure we only call this once.
5710 */
Eric Dumazet12d94a82017-08-15 04:09:51 -07005711 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07005712#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric Dumazet12d94a82017-08-15 04:09:51 -07005713 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
5714 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07005715#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005716 }
5717
5718 return NOTIFY_OK;
5719}
5720
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721/*
5722 * /proc
5723 */
5724
5725#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726static int rt6_stats_seq_show(struct seq_file *seq, void *v)
5727{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005728 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005730 net->ipv6.rt6_stats->fib_nodes,
5731 net->ipv6.rt6_stats->fib_route_nodes,
Wei Wang81eb8442017-10-06 12:06:11 -07005732 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005733 net->ipv6.rt6_stats->fib_rt_entries,
5734 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00005735 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005736 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737
5738 return 0;
5739}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740#endif /* CONFIG_PROC_FS */
5741
5742#ifdef CONFIG_SYSCTL
5743
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005745int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 void __user *buffer, size_t *lenp, loff_t *ppos)
5747{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005748 struct net *net;
5749 int delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06005750 int ret;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005751 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005753
5754 net = (struct net *)ctl->extra1;
5755 delay = net->ipv6.sysctl.flush_delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06005756 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5757 if (ret)
5758 return ret;
5759
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02005760 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005761 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762}
5763
David Ahern7c6bb7d2018-10-11 20:17:21 -07005764static int zero;
5765static int one = 1;
5766
David Aherned792e22018-10-08 14:06:34 -07005767static struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005768 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08005770 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07005772 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005773 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 },
5775 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08005777 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 .maxlen = sizeof(int),
5779 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005780 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 },
5782 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08005784 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 .maxlen = sizeof(int),
5786 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005787 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 },
5789 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08005791 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 .maxlen = sizeof(int),
5793 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005794 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 },
5796 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08005798 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 .maxlen = sizeof(int),
5800 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005801 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 },
5803 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08005805 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 .maxlen = sizeof(int),
5807 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005808 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 },
5810 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08005812 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 .maxlen = sizeof(int),
5814 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07005815 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 },
5817 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08005819 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 .maxlen = sizeof(int),
5821 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005822 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 },
5824 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08005826 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 .maxlen = sizeof(int),
5828 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07005829 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 },
5831 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08005833 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 .maxlen = sizeof(int),
5835 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005836 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 },
David Ahern7c6bb7d2018-10-11 20:17:21 -07005838 {
5839 .procname = "skip_notify_on_dev_down",
5840 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
5841 .maxlen = sizeof(int),
5842 .mode = 0644,
5843 .proc_handler = proc_dointvec,
5844 .extra1 = &zero,
5845 .extra2 = &one,
5846 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08005847 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848};
5849
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005850struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08005851{
5852 struct ctl_table *table;
5853
5854 table = kmemdup(ipv6_route_table_template,
5855 sizeof(ipv6_route_table_template),
5856 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005857
5858 if (table) {
5859 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005860 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00005861 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005862 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
5863 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
5864 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
5865 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
5866 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
5867 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
5868 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08005869 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
David Ahern7c6bb7d2018-10-11 20:17:21 -07005870 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
Eric W. Biederman464dc802012-11-16 03:02:59 +00005871
5872 /* Don't export sysctls to unprivileged users */
5873 if (net->user_ns != &init_user_ns)
5874 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005875 }
5876
Daniel Lezcano760f2d02008-01-10 02:53:43 -08005877 return table;
5878}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879#endif
5880
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005881static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005882{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07005883 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005884
Alexey Dobriyan86393e52009-08-29 01:34:49 +00005885 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
5886 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005887
Eric Dumazetfc66f952010-10-08 06:37:34 +00005888 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5889 goto out_ip6_dst_ops;
5890
David Ahern1cf844c2019-05-22 20:27:59 -07005891 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
David Ahern421842e2018-04-17 17:33:18 -07005892 if (!net->ipv6.fib6_null_entry)
5893 goto out_ip6_dst_entries;
David Ahern1cf844c2019-05-22 20:27:59 -07005894 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
5895 sizeof(*net->ipv6.fib6_null_entry));
David Ahern421842e2018-04-17 17:33:18 -07005896
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005897 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
5898 sizeof(*net->ipv6.ip6_null_entry),
5899 GFP_KERNEL);
5900 if (!net->ipv6.ip6_null_entry)
David Ahern421842e2018-04-17 17:33:18 -07005901 goto out_fib6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005902 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005903 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
5904 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005905
5906#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Vincent Bernatfeca7d82017-08-08 20:23:49 +02005907 net->ipv6.fib6_has_custom_rules = false;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005908 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
5909 sizeof(*net->ipv6.ip6_prohibit_entry),
5910 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005911 if (!net->ipv6.ip6_prohibit_entry)
5912 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005913 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005914 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
5915 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005916
5917 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
5918 sizeof(*net->ipv6.ip6_blk_hole_entry),
5919 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005920 if (!net->ipv6.ip6_blk_hole_entry)
5921 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005922 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005923 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
5924 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005925#endif
5926
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07005927 net->ipv6.sysctl.flush_delay = 0;
5928 net->ipv6.sysctl.ip6_rt_max_size = 4096;
5929 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5930 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5931 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5932 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5933 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5934 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
David Ahern7c6bb7d2018-10-11 20:17:21 -07005935 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07005936
Benjamin Thery6891a342008-03-04 13:49:47 -08005937 net->ipv6.ip6_rt_gc_expire = 30*HZ;
5938
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005939 ret = 0;
5940out:
5941 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005942
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005943#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5944out_ip6_prohibit_entry:
5945 kfree(net->ipv6.ip6_prohibit_entry);
5946out_ip6_null_entry:
5947 kfree(net->ipv6.ip6_null_entry);
5948#endif
David Ahern421842e2018-04-17 17:33:18 -07005949out_fib6_null_entry:
5950 kfree(net->ipv6.fib6_null_entry);
Eric Dumazetfc66f952010-10-08 06:37:34 +00005951out_ip6_dst_entries:
5952 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005953out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005954 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005955}
5956
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005957static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005958{
David Ahern421842e2018-04-17 17:33:18 -07005959 kfree(net->ipv6.fib6_null_entry);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005960 kfree(net->ipv6.ip6_null_entry);
5961#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5962 kfree(net->ipv6.ip6_prohibit_entry);
5963 kfree(net->ipv6.ip6_blk_hole_entry);
5964#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00005965 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005966}
5967
Thomas Grafd1896342012-06-18 12:08:33 +00005968static int __net_init ip6_route_net_init_late(struct net *net)
5969{
5970#ifdef CONFIG_PROC_FS
Christoph Hellwigc3506372018-04-10 19:42:55 +02005971 proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
5972 sizeof(struct ipv6_route_iter));
Christoph Hellwig3617d942018-04-13 20:38:35 +02005973 proc_create_net_single("rt6_stats", 0444, net->proc_net,
5974 rt6_stats_seq_show, NULL);
Thomas Grafd1896342012-06-18 12:08:33 +00005975#endif
5976 return 0;
5977}
5978
5979static void __net_exit ip6_route_net_exit_late(struct net *net)
5980{
5981#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00005982 remove_proc_entry("ipv6_route", net->proc_net);
5983 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00005984#endif
5985}
5986
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005987static struct pernet_operations ip6_route_net_ops = {
5988 .init = ip6_route_net_init,
5989 .exit = ip6_route_net_exit,
5990};
5991
David S. Millerc3426b42012-06-09 16:27:05 -07005992static int __net_init ipv6_inetpeer_init(struct net *net)
5993{
5994 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5995
5996 if (!bp)
5997 return -ENOMEM;
5998 inet_peer_base_init(bp);
5999 net->ipv6.peers = bp;
6000 return 0;
6001}
6002
6003static void __net_exit ipv6_inetpeer_exit(struct net *net)
6004{
6005 struct inet_peer_base *bp = net->ipv6.peers;
6006
6007 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07006008 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07006009 kfree(bp);
6010}
6011
David S. Miller2b823f72012-06-09 19:00:16 -07006012static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07006013 .init = ipv6_inetpeer_init,
6014 .exit = ipv6_inetpeer_exit,
6015};
6016
Thomas Grafd1896342012-06-18 12:08:33 +00006017static struct pernet_operations ip6_route_net_late_ops = {
6018 .init = ip6_route_net_init_late,
6019 .exit = ip6_route_net_exit_late,
6020};
6021
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006022static struct notifier_block ip6_route_dev_notifier = {
6023 .notifier_call = ip6_route_dev_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07006024 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006025};
6026
WANG Cong2f460932017-05-03 22:07:31 -07006027void __init ip6_route_init_special_entries(void)
6028{
6029 /* Registering of the loopback is done before this portion of code,
6030 * the loopback reference in rt6_info will not be taken, do it
6031 * manually for init_net */
David Ahern1cf844c2019-05-22 20:27:59 -07006032 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
WANG Cong2f460932017-05-03 22:07:31 -07006033 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6034 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6035 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6036 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6037 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6038 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6039 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6040 #endif
6041}
6042
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006043int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006045 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006046 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006047
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006048 ret = -ENOMEM;
6049 ip6_dst_ops_template.kmem_cachep =
6050 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6051 SLAB_HWCACHE_ALIGN, NULL);
6052 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08006053 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07006054
Eric Dumazetfc66f952010-10-08 06:37:34 +00006055 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006056 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006057 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006058
David S. Millerc3426b42012-06-09 16:27:05 -07006059 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6060 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006061 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00006062
David S. Miller7e52b332012-06-15 15:51:55 -07006063 ret = register_pernet_subsys(&ip6_route_net_ops);
6064 if (ret)
6065 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07006066
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07006067 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6068
David S. Millere8803b62012-06-16 01:12:19 -07006069 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006070 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006071 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006072
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006073 ret = xfrm6_init();
6074 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006075 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08006076
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006077 ret = fib6_rules_init();
6078 if (ret)
6079 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08006080
Thomas Grafd1896342012-06-18 12:08:33 +00006081 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6082 if (ret)
6083 goto fib6_rules_init;
6084
Florian Westphal16feebc2017-12-02 21:44:08 +01006085 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6086 inet6_rtm_newroute, NULL, 0);
6087 if (ret < 0)
6088 goto out_register_late_subsys;
6089
6090 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6091 inet6_rtm_delroute, NULL, 0);
6092 if (ret < 0)
6093 goto out_register_late_subsys;
6094
6095 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6096 inet6_rtm_getroute, NULL,
6097 RTNL_FLAG_DOIT_UNLOCKED);
6098 if (ret < 0)
Thomas Grafd1896342012-06-18 12:08:33 +00006099 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006100
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006101 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006102 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00006103 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006104
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006105 for_each_possible_cpu(cpu) {
6106 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6107
6108 INIT_LIST_HEAD(&ul->head);
6109 spin_lock_init(&ul->lock);
6110 }
6111
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006112out:
6113 return ret;
6114
Thomas Grafd1896342012-06-18 12:08:33 +00006115out_register_late_subsys:
Florian Westphal16feebc2017-12-02 21:44:08 +01006116 rtnl_unregister_all(PF_INET6);
Thomas Grafd1896342012-06-18 12:08:33 +00006117 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006118fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006119 fib6_rules_cleanup();
6120xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006121 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00006122out_fib6_init:
6123 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006124out_register_subsys:
6125 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07006126out_register_inetpeer:
6127 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006128out_dst_entries:
6129 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006130out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006131 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006132 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133}
6134
6135void ip6_route_cleanup(void)
6136{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006137 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00006138 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07006139 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07006142 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006143 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006144 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006145 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146}