blob: 062dd4d8232c01f77d3e72b9498f5a698aa19f5c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux INET6 implementation
3 * FIB front-end.
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14/* Changes:
15 *
16 * YOSHIFUJI Hideaki @USAGI
17 * reworked default router selection.
18 * - respect outgoing interface
19 * - select from (probably) reachable routers (i.e.
20 * routers in REACHABLE, STALE, DELAY or PROBE states).
21 * - always select the same router if it is (probably)
22 * reachable. otherwise, round-robin the list.
YOSHIFUJI Hideakic0bece92006-08-23 17:23:25 -070023 * Ville Nuorvala
24 * Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Joe Perchesf3213832012-05-15 14:11:53 +000027#define pr_fmt(fmt) "IPv6: " fmt
28
Randy Dunlap4fc268d2006-01-11 12:17:47 -080029#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/errno.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040031#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/types.h>
33#include <linux/times.h>
34#include <linux/socket.h>
35#include <linux/sockios.h>
36#include <linux/net.h>
37#include <linux/route.h>
38#include <linux/netdevice.h>
39#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090040#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/proc_fs.h>
44#include <linux/seq_file.h>
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080045#include <linux/nsproxy.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Wei Wang35732d02017-10-06 12:05:57 -070047#include <linux/jhash.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020048#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <net/snmp.h>
50#include <net/ipv6.h>
51#include <net/ip6_fib.h>
52#include <net/ip6_route.h>
53#include <net/ndisc.h>
54#include <net/addrconf.h>
55#include <net/tcp.h>
56#include <linux/rtnetlink.h>
57#include <net/dst.h>
Jiri Benc904af042015-08-20 13:56:31 +020058#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <net/xfrm.h>
Tom Tucker8d717402006-07-30 20:43:36 -070060#include <net/netevent.h>
Thomas Graf21713eb2006-08-15 00:35:24 -070061#include <net/netlink.h>
Nicolas Dichtel51ebd312012-10-22 03:42:09 +000062#include <net/nexthop.h>
Roopa Prabhu19e42e42015-07-21 10:43:48 +020063#include <net/lwtunnel.h>
Jiri Benc904af042015-08-20 13:56:31 +020064#include <net/ip_tunnels.h>
David Ahernca254492015-10-12 11:47:10 -070065#include <net/l3mdev.h>
David Ahernb8115802015-11-19 12:24:22 -080066#include <trace/events/fib6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080068#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#ifdef CONFIG_SYSCTL
71#include <linux/sysctl.h>
72#endif
73
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020074enum rt6_nud_state {
Jiri Benc7e980562013-12-11 13:48:20 +010075 RT6_NUD_FAIL_HARD = -3,
76 RT6_NUD_FAIL_PROBE = -2,
77 RT6_NUD_FAIL_DO_RR = -1,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020078 RT6_NUD_SUCCEED = 1
79};
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
David S. Miller0dbaee32010-12-13 12:52:14 -080082static unsigned int ip6_default_advmss(const struct dst_entry *dst);
Steffen Klassertebb762f2011-11-23 02:12:51 +000083static unsigned int ip6_mtu(const struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static struct dst_entry *ip6_negative_advice(struct dst_entry *);
85static void ip6_dst_destroy(struct dst_entry *);
86static void ip6_dst_ifdown(struct dst_entry *,
87 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080088static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90static int ip6_pkt_discard(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050091static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Kamala R7150aed2013-12-02 19:55:21 +053092static int ip6_pkt_prohibit(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050093static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static void ip6_link_failure(struct sk_buff *skb);
David S. Miller6700c272012-07-17 03:29:28 -070095static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
96 struct sk_buff *skb, u32 mtu);
97static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
98 struct sk_buff *skb);
David Ahern8d1c8022018-04-17 17:33:26 -070099static int rt6_score_route(struct fib6_info *rt, int oif, int strict);
100static size_t rt6_nlmsg_size(struct fib6_info *rt);
David Ahernd4ead6b2018-04-17 17:33:16 -0700101static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -0700102 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -0700103 struct in6_addr *dest, struct in6_addr *src,
David Ahern16a16cd2017-02-02 12:37:11 -0800104 int iif, int type, u32 portid, u32 seq,
105 unsigned int flags);
David Ahern8d1c8022018-04-17 17:33:26 -0700106static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt,
Wei Wang35732d02017-10-06 12:05:57 -0700107 struct in6_addr *daddr,
108 struct in6_addr *saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800110#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -0700111static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000112 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700113 const struct in6_addr *gwaddr,
114 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +0000115 unsigned int pref);
David Ahern8d1c8022018-04-17 17:33:26 -0700116static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000117 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700118 const struct in6_addr *gwaddr,
119 struct net_device *dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800120#endif
121
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700122struct uncached_list {
123 spinlock_t lock;
124 struct list_head head;
125};
126
127static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
128
Xin Long510c3212018-02-14 19:06:02 +0800129void rt6_uncached_list_add(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700130{
131 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
132
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700133 rt->rt6i_uncached_list = ul;
134
135 spin_lock_bh(&ul->lock);
136 list_add_tail(&rt->rt6i_uncached, &ul->head);
137 spin_unlock_bh(&ul->lock);
138}
139
Xin Long510c3212018-02-14 19:06:02 +0800140void rt6_uncached_list_del(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700141{
142 if (!list_empty(&rt->rt6i_uncached)) {
143 struct uncached_list *ul = rt->rt6i_uncached_list;
Wei Wang81eb8442017-10-06 12:06:11 -0700144 struct net *net = dev_net(rt->dst.dev);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700145
146 spin_lock_bh(&ul->lock);
147 list_del(&rt->rt6i_uncached);
Wei Wang81eb8442017-10-06 12:06:11 -0700148 atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700149 spin_unlock_bh(&ul->lock);
150 }
151}
152
153static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
154{
155 struct net_device *loopback_dev = net->loopback_dev;
156 int cpu;
157
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500158 if (dev == loopback_dev)
159 return;
160
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700161 for_each_possible_cpu(cpu) {
162 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
163 struct rt6_info *rt;
164
165 spin_lock_bh(&ul->lock);
166 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
167 struct inet6_dev *rt_idev = rt->rt6i_idev;
168 struct net_device *rt_dev = rt->dst.dev;
169
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500170 if (rt_idev->dev == dev) {
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700171 rt->rt6i_idev = in6_dev_get(loopback_dev);
172 in6_dev_put(rt_idev);
173 }
174
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500175 if (rt_dev == dev) {
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700176 rt->dst.dev = loopback_dev;
177 dev_hold(rt->dst.dev);
178 dev_put(rt_dev);
179 }
180 }
181 spin_unlock_bh(&ul->lock);
182 }
183}
184
David Ahernf8a1b432018-04-17 17:33:21 -0700185static inline const void *choose_neigh_daddr(const struct in6_addr *p,
David S. Millerf894cbf2012-07-02 21:52:24 -0700186 struct sk_buff *skb,
187 const void *daddr)
David S. Miller39232972012-01-26 15:22:32 -0500188{
David S. Millera7563f32012-01-26 16:29:16 -0500189 if (!ipv6_addr_any(p))
David S. Miller39232972012-01-26 15:22:32 -0500190 return (const void *) p;
David S. Millerf894cbf2012-07-02 21:52:24 -0700191 else if (skb)
192 return &ipv6_hdr(skb)->daddr;
David S. Miller39232972012-01-26 15:22:32 -0500193 return daddr;
194}
195
David Ahernf8a1b432018-04-17 17:33:21 -0700196struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
197 struct net_device *dev,
198 struct sk_buff *skb,
199 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700200{
David S. Miller39232972012-01-26 15:22:32 -0500201 struct neighbour *n;
202
David Ahernf8a1b432018-04-17 17:33:21 -0700203 daddr = choose_neigh_daddr(gw, skb, daddr);
204 n = __ipv6_neigh_lookup(dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500205 if (n)
206 return n;
David Ahernf8a1b432018-04-17 17:33:21 -0700207 return neigh_create(&nd_tbl, daddr, dev);
208}
209
210static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
211 struct sk_buff *skb,
212 const void *daddr)
213{
214 const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
215
216 return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500217}
218
Julian Anastasov63fca652017-02-06 23:14:15 +0200219static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
220{
221 struct net_device *dev = dst->dev;
222 struct rt6_info *rt = (struct rt6_info *)dst;
223
David Ahernf8a1b432018-04-17 17:33:21 -0700224 daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +0200225 if (!daddr)
226 return;
227 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
228 return;
229 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
230 return;
231 __ipv6_confirm_neigh(dev, daddr);
232}
233
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800234static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 .gc = ip6_dst_gc,
237 .gc_thresh = 1024,
238 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800239 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000240 .mtu = ip6_mtu,
David Ahernd4ead6b2018-04-17 17:33:16 -0700241 .cow_metrics = dst_cow_metrics_generic,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .destroy = ip6_dst_destroy,
243 .ifdown = ip6_dst_ifdown,
244 .negative_advice = ip6_negative_advice,
245 .link_failure = ip6_link_failure,
246 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700247 .redirect = rt6_do_redirect,
Eric W. Biederman9f8955c2015-10-07 16:48:39 -0500248 .local_out = __ip6_local_out,
David Ahernf8a1b432018-04-17 17:33:21 -0700249 .neigh_lookup = ip6_dst_neigh_lookup,
Julian Anastasov63fca652017-02-06 23:14:15 +0200250 .confirm_neigh = ip6_confirm_neigh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251};
252
Steffen Klassertebb762f2011-11-23 02:12:51 +0000253static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800254{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000255 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
256
257 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800258}
259
David S. Miller6700c272012-07-17 03:29:28 -0700260static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
261 struct sk_buff *skb, u32 mtu)
David S. Miller14e50e52007-05-24 18:17:54 -0700262{
263}
264
David S. Miller6700c272012-07-17 03:29:28 -0700265static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
266 struct sk_buff *skb)
David S. Millerb587ee32012-07-12 00:39:24 -0700267{
268}
269
David S. Miller14e50e52007-05-24 18:17:54 -0700270static struct dst_ops ip6_dst_blackhole_ops = {
271 .family = AF_INET6,
David S. Miller14e50e52007-05-24 18:17:54 -0700272 .destroy = ip6_dst_destroy,
273 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000274 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800275 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700276 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Millerb587ee32012-07-12 00:39:24 -0700277 .redirect = ip6_rt_blackhole_redirect,
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -0700278 .cow_metrics = dst_cow_metrics_generic,
David Ahernf8a1b432018-04-17 17:33:21 -0700279 .neigh_lookup = ip6_dst_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700280};
281
David S. Miller62fa8a82011-01-26 20:51:05 -0800282static const u32 ip6_template_metrics[RTAX_MAX] = {
Li RongQing14edd872012-10-24 14:01:18 +0800283 [RTAX_HOPLIMIT - 1] = 0,
David S. Miller62fa8a82011-01-26 20:51:05 -0800284};
285
David Ahern8d1c8022018-04-17 17:33:26 -0700286static const struct fib6_info fib6_null_entry_template = {
David Ahern93c2fb22018-04-18 15:38:59 -0700287 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
288 .fib6_protocol = RTPROT_KERNEL,
289 .fib6_metric = ~(u32)0,
290 .fib6_ref = ATOMIC_INIT(1),
David Ahern421842e2018-04-17 17:33:18 -0700291 .fib6_type = RTN_UNREACHABLE,
292 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
293};
294
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000295static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700296 .dst = {
297 .__refcnt = ATOMIC_INIT(1),
298 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000299 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700300 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700301 .input = ip6_pkt_discard,
302 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 },
304 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305};
306
Thomas Graf101367c2006-08-04 03:39:02 -0700307#ifdef CONFIG_IPV6_MULTIPLE_TABLES
308
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000309static const struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700310 .dst = {
311 .__refcnt = ATOMIC_INIT(1),
312 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000313 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700314 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700315 .input = ip6_pkt_prohibit,
316 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700317 },
318 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700319};
320
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000321static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700322 .dst = {
323 .__refcnt = ATOMIC_INIT(1),
324 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000325 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700326 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700327 .input = dst_discard,
Eric W. Biedermanede20592015-10-07 16:48:47 -0500328 .output = dst_discard_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700329 },
330 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700331};
332
333#endif
334
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700335static void rt6_info_init(struct rt6_info *rt)
336{
337 struct dst_entry *dst = &rt->dst;
338
339 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700340 INIT_LIST_HEAD(&rt->rt6i_uncached);
341}
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343/* allocate dst with ip6_dst_ops */
David Ahern93531c62018-04-17 17:33:25 -0700344struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
345 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
David S. Miller97bab732012-06-09 22:36:36 -0700347 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Wei Wangb2a9c0e2017-06-17 10:42:41 -0700348 1, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700349
Wei Wang81eb8442017-10-06 12:06:11 -0700350 if (rt) {
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700351 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -0700352 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
353 }
Steffen Klassert81048912012-07-05 23:37:09 +0000354
David S. Millercf911662011-04-28 14:31:47 -0700355 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
David Ahern9ab179d2016-04-07 11:10:06 -0700357EXPORT_SYMBOL(ip6_dst_alloc);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359static void ip6_dst_destroy(struct dst_entry *dst)
360{
361 struct rt6_info *rt = (struct rt6_info *)dst;
David Ahern8d1c8022018-04-17 17:33:26 -0700362 struct fib6_info *from = rt->from;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700363 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700365 dst_destroy_metrics_generic(dst);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700366 rt6_uncached_list_del(rt);
367
368 idev = rt->rt6i_idev;
David S. Miller38308472011-12-03 18:02:47 -0500369 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 rt->rt6i_idev = NULL;
371 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900372 }
David Ahernd4ead6b2018-04-17 17:33:16 -0700373
David Miller3a2232e2017-11-28 15:40:40 -0500374 rt->from = NULL;
David Ahern93531c62018-04-17 17:33:25 -0700375 fib6_info_release(from);
David S. Millerb3419362010-11-30 12:27:11 -0800376}
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
379 int how)
380{
381 struct rt6_info *rt = (struct rt6_info *)dst;
382 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800383 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900384 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Wei Wange5645f52017-08-14 10:44:59 -0700386 if (idev && idev->dev != loopback_dev) {
387 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
388 if (loopback_idev) {
389 rt->rt6i_idev = loopback_idev;
390 in6_dev_put(idev);
David S. Miller97cac082012-07-02 22:43:47 -0700391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393}
394
Martin KaFai Lau5973fb12015-11-11 11:51:07 -0800395static bool __rt6_check_expired(const struct rt6_info *rt)
396{
397 if (rt->rt6i_flags & RTF_EXPIRES)
398 return time_after(jiffies, rt->dst.expires);
399 else
400 return false;
401}
402
Eric Dumazeta50feda2012-05-18 18:57:34 +0000403static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Gao feng1716a962012-04-06 00:13:10 +0000405 if (rt->rt6i_flags & RTF_EXPIRES) {
406 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000407 return true;
David Miller3a2232e2017-11-28 15:40:40 -0500408 } else if (rt->from) {
Xin Long1e2ea8a2017-08-26 20:10:10 +0800409 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
David Ahern14895682018-04-17 17:33:17 -0700410 fib6_check_expired(rt->from);
Gao feng1716a962012-04-06 00:13:10 +0000411 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000412 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
David Ahern8d1c8022018-04-17 17:33:26 -0700415static struct fib6_info *rt6_multipath_select(const struct net *net,
416 struct fib6_info *match,
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200417 struct flowi6 *fl6, int oif,
David Ahernb75cc8f2018-03-02 08:32:17 -0800418 const struct sk_buff *skb,
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200419 int strict)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000420{
David Ahern8d1c8022018-04-17 17:33:26 -0700421 struct fib6_info *sibling, *next_sibling;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000422
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200423 /* We might have already computed the hash for ICMPv6 errors. In such
424 * case it will always be non-zero. Otherwise now is the time to do it.
425 */
426 if (!fl6->mp_hash)
David Ahernb4bac172018-03-02 08:32:18 -0800427 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200428
David Ahern5e670d82018-04-17 17:33:14 -0700429 if (fl6->mp_hash <= atomic_read(&match->fib6_nh.nh_upper_bound))
Ido Schimmel3d709f62018-01-09 16:40:27 +0200430 return match;
Ido Schimmelbbfcd772017-11-21 09:50:12 +0200431
David Ahern93c2fb22018-04-18 15:38:59 -0700432 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
433 fib6_siblings) {
David Ahern5e670d82018-04-17 17:33:14 -0700434 int nh_upper_bound;
435
436 nh_upper_bound = atomic_read(&sibling->fib6_nh.nh_upper_bound);
437 if (fl6->mp_hash > nh_upper_bound)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200438 continue;
439 if (rt6_score_route(sibling, oif, strict) < 0)
440 break;
441 match = sibling;
442 break;
443 }
444
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000445 return match;
446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/*
Wei Wang66f5d6c2017-10-06 12:06:10 -0700449 * Route lookup. rcu_read_lock() should be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
451
David Ahern8d1c8022018-04-17 17:33:26 -0700452static inline struct fib6_info *rt6_device_match(struct net *net,
453 struct fib6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000454 const struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700456 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
David Ahern8d1c8022018-04-17 17:33:26 -0700458 struct fib6_info *sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
David Ahern5e670d82018-04-17 17:33:14 -0700460 if (!oif && ipv6_addr_any(saddr) &&
461 !(rt->fib6_nh.nh_flags & RTNH_F_DEAD))
Ido Schimmel8067bb82018-01-07 12:45:09 +0200462 return rt;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900463
David Miller071fb372017-11-28 15:40:15 -0500464 for (sprt = rt; sprt; sprt = rcu_dereference(sprt->rt6_next)) {
David Ahern5e670d82018-04-17 17:33:14 -0700465 const struct net_device *dev = sprt->fib6_nh.nh_dev;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900466
David Ahern5e670d82018-04-17 17:33:14 -0700467 if (sprt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +0200468 continue;
469
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900470 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (dev->ifindex == oif)
472 return sprt;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900473 } else {
474 if (ipv6_chk_addr(net, saddr, dev,
475 flags & RT6_LOOKUP_F_IFACE))
476 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
David Aherneea68cd2018-04-18 15:39:02 -0700480 if (oif && flags & RT6_LOOKUP_F_IFACE)
481 return net->ipv6.fib6_null_entry;
Ido Schimmel8067bb82018-01-07 12:45:09 +0200482
David Ahern421842e2018-04-17 17:33:18 -0700483 return rt->fib6_nh.nh_flags & RTNH_F_DEAD ? net->ipv6.fib6_null_entry : rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800486#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200487struct __rt6_probe_work {
488 struct work_struct work;
489 struct in6_addr target;
490 struct net_device *dev;
491};
492
493static void rt6_probe_deferred(struct work_struct *w)
494{
495 struct in6_addr mcaddr;
496 struct __rt6_probe_work *work =
497 container_of(w, struct __rt6_probe_work, work);
498
499 addrconf_addr_solict_mult(&work->target, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800500 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200501 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100502 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200503}
504
David Ahern8d1c8022018-04-17 17:33:26 -0700505static void rt6_probe(struct fib6_info *rt)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800506{
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700507 struct __rt6_probe_work *work;
David Ahern5e670d82018-04-17 17:33:14 -0700508 const struct in6_addr *nh_gw;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000509 struct neighbour *neigh;
David Ahern5e670d82018-04-17 17:33:14 -0700510 struct net_device *dev;
511
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800512 /*
513 * Okay, this does not seem to be appropriate
514 * for now, however, we need to check if it
515 * is really so; aka Router Reachability Probing.
516 *
517 * Router Reachability Probe MUST be rate-limited
518 * to no more than one per minute.
519 */
David Ahern93c2fb22018-04-18 15:38:59 -0700520 if (!rt || !(rt->fib6_flags & RTF_GATEWAY))
Amerigo Wangfdd66812012-09-10 02:48:44 +0000521 return;
David Ahern5e670d82018-04-17 17:33:14 -0700522
523 nh_gw = &rt->fib6_nh.nh_gw;
524 dev = rt->fib6_nh.nh_dev;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000525 rcu_read_lock_bh();
David Ahern5e670d82018-04-17 17:33:14 -0700526 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000527 if (neigh) {
David Aherndcd1f572018-04-18 15:39:05 -0700528 struct inet6_dev *idev;
529
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700530 if (neigh->nud_state & NUD_VALID)
531 goto out;
532
David Aherndcd1f572018-04-18 15:39:05 -0700533 idev = __in6_dev_get(dev);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700534 work = NULL;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000535 write_lock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700536 if (!(neigh->nud_state & NUD_VALID) &&
537 time_after(jiffies,
David Aherndcd1f572018-04-18 15:39:05 -0700538 neigh->updated + idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700539 work = kmalloc(sizeof(*work), GFP_ATOMIC);
540 if (work)
541 __neigh_set_probe_once(neigh);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200542 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000543 write_unlock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700544 } else {
545 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000546 }
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700547
548 if (work) {
549 INIT_WORK(&work->work, rt6_probe_deferred);
David Ahern5e670d82018-04-17 17:33:14 -0700550 work->target = *nh_gw;
551 dev_hold(dev);
552 work->dev = dev;
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700553 schedule_work(&work->work);
554 }
555
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700556out:
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000557 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800558}
559#else
David Ahern8d1c8022018-04-17 17:33:26 -0700560static inline void rt6_probe(struct fib6_info *rt)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800561{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800562}
563#endif
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800566 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 */
David Ahern8d1c8022018-04-17 17:33:26 -0700568static inline int rt6_check_dev(struct fib6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
David Ahern5e670d82018-04-17 17:33:14 -0700570 const struct net_device *dev = rt->fib6_nh.nh_dev;
571
David S. Miller161980f2007-04-06 11:42:27 -0700572 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800573 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
David Ahern8d1c8022018-04-17 17:33:26 -0700577static inline enum rt6_nud_state rt6_check_neigh(struct fib6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200579 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
David Ahern5e670d82018-04-17 17:33:14 -0700580 struct neighbour *neigh;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000581
David Ahern93c2fb22018-04-18 15:38:59 -0700582 if (rt->fib6_flags & RTF_NONEXTHOP ||
583 !(rt->fib6_flags & RTF_GATEWAY))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200584 return RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000585
586 rcu_read_lock_bh();
David Ahern5e670d82018-04-17 17:33:14 -0700587 neigh = __ipv6_neigh_lookup_noref(rt->fib6_nh.nh_dev,
588 &rt->fib6_nh.nh_gw);
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000589 if (neigh) {
590 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800591 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200592 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800593#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000594 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200595 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100596 else
597 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800598#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000599 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200600 } else {
601 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100602 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000603 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000604 rcu_read_unlock_bh();
605
Paul Marksa5a81f02012-12-03 10:26:54 +0000606 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800607}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
David Ahern8d1c8022018-04-17 17:33:26 -0700609static int rt6_score_route(struct fib6_info *rt, int oif, int strict)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800610{
Paul Marksa5a81f02012-12-03 10:26:54 +0000611 int m;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900612
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700613 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700614 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200615 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800616#ifdef CONFIG_IPV6_ROUTER_PREF
David Ahern93c2fb22018-04-18 15:38:59 -0700617 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->fib6_flags)) << 2;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800618#endif
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200619 if (strict & RT6_LOOKUP_F_REACHABLE) {
620 int n = rt6_check_neigh(rt);
621 if (n < 0)
622 return n;
623 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800624 return m;
625}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
David Aherndcd1f572018-04-18 15:39:05 -0700627/* called with rc_read_lock held */
628static inline bool fib6_ignore_linkdown(const struct fib6_info *f6i)
629{
630 const struct net_device *dev = fib6_info_nh_dev(f6i);
631 bool rc = false;
632
633 if (dev) {
634 const struct inet6_dev *idev = __in6_dev_get(dev);
635
636 rc = !!idev->cnf.ignore_routes_with_linkdown;
637 }
638
639 return rc;
640}
641
David Ahern8d1c8022018-04-17 17:33:26 -0700642static struct fib6_info *find_match(struct fib6_info *rt, int oif, int strict,
643 int *mpri, struct fib6_info *match,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200644 bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800645{
David S. Millerf11e6652007-03-24 20:36:25 -0700646 int m;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200647 bool match_do_rr = false;
Andy Gospodarek35103d12015-08-13 10:39:01 -0400648
David Ahern5e670d82018-04-17 17:33:14 -0700649 if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +0200650 goto out;
651
David Aherndcd1f572018-04-18 15:39:05 -0700652 if (fib6_ignore_linkdown(rt) &&
David Ahern5e670d82018-04-17 17:33:14 -0700653 rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN &&
David Ahernd5d32e42016-10-24 12:27:23 -0700654 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
Andy Gospodarek35103d12015-08-13 10:39:01 -0400655 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700656
David Ahern14895682018-04-17 17:33:17 -0700657 if (fib6_check_expired(rt))
David S. Millerf11e6652007-03-24 20:36:25 -0700658 goto out;
659
660 m = rt6_score_route(rt, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100661 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200662 match_do_rr = true;
663 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100664 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700665 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700666 }
667
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200668 if (strict & RT6_LOOKUP_F_REACHABLE)
669 rt6_probe(rt);
670
Jiri Benc7e980562013-12-11 13:48:20 +0100671 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200672 if (m > *mpri) {
673 *do_rr = match_do_rr;
674 *mpri = m;
675 match = rt;
676 }
David S. Millerf11e6652007-03-24 20:36:25 -0700677out:
678 return match;
679}
680
David Ahern8d1c8022018-04-17 17:33:26 -0700681static struct fib6_info *find_rr_leaf(struct fib6_node *fn,
682 struct fib6_info *leaf,
683 struct fib6_info *rr_head,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200684 u32 metric, int oif, int strict,
685 bool *do_rr)
David S. Millerf11e6652007-03-24 20:36:25 -0700686{
David Ahern8d1c8022018-04-17 17:33:26 -0700687 struct fib6_info *rt, *match, *cont;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800688 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
David S. Millerf11e6652007-03-24 20:36:25 -0700690 match = NULL;
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700691 cont = NULL;
David Miller071fb372017-11-28 15:40:15 -0500692 for (rt = rr_head; rt; rt = rcu_dereference(rt->rt6_next)) {
David Ahern93c2fb22018-04-18 15:38:59 -0700693 if (rt->fib6_metric != metric) {
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700694 cont = rt;
695 break;
696 }
697
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200698 match = find_match(rt, oif, strict, &mpri, match, do_rr);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700699 }
700
Wei Wang66f5d6c2017-10-06 12:06:10 -0700701 for (rt = leaf; rt && rt != rr_head;
David Miller071fb372017-11-28 15:40:15 -0500702 rt = rcu_dereference(rt->rt6_next)) {
David Ahern93c2fb22018-04-18 15:38:59 -0700703 if (rt->fib6_metric != metric) {
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700704 cont = rt;
705 break;
706 }
707
708 match = find_match(rt, oif, strict, &mpri, match, do_rr);
709 }
710
711 if (match || !cont)
712 return match;
713
David Miller071fb372017-11-28 15:40:15 -0500714 for (rt = cont; rt; rt = rcu_dereference(rt->rt6_next))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200715 match = find_match(rt, oif, strict, &mpri, match, do_rr);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800716
David S. Millerf11e6652007-03-24 20:36:25 -0700717 return match;
718}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800719
David Ahern8d1c8022018-04-17 17:33:26 -0700720static struct fib6_info *rt6_select(struct net *net, struct fib6_node *fn,
Wei Wang8d1040e2017-10-06 12:06:08 -0700721 int oif, int strict)
David S. Millerf11e6652007-03-24 20:36:25 -0700722{
David Ahern8d1c8022018-04-17 17:33:26 -0700723 struct fib6_info *leaf = rcu_dereference(fn->leaf);
724 struct fib6_info *match, *rt0;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200725 bool do_rr = false;
Wei Wang17ecf592017-10-06 12:06:09 -0700726 int key_plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
David Ahern421842e2018-04-17 17:33:18 -0700728 if (!leaf || leaf == net->ipv6.fib6_null_entry)
729 return net->ipv6.fib6_null_entry;
Wei Wang8d1040e2017-10-06 12:06:08 -0700730
Wei Wang66f5d6c2017-10-06 12:06:10 -0700731 rt0 = rcu_dereference(fn->rr_ptr);
David S. Millerf11e6652007-03-24 20:36:25 -0700732 if (!rt0)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700733 rt0 = leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Wei Wang17ecf592017-10-06 12:06:09 -0700735 /* Double check to make sure fn is not an intermediate node
736 * and fn->leaf does not points to its child's leaf
737 * (This might happen if all routes under fn are deleted from
738 * the tree and fib6_repair_tree() is called on the node.)
739 */
David Ahern93c2fb22018-04-18 15:38:59 -0700740 key_plen = rt0->fib6_dst.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700741#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -0700742 if (rt0->fib6_src.plen)
743 key_plen = rt0->fib6_src.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700744#endif
745 if (fn->fn_bit != key_plen)
David Ahern421842e2018-04-17 17:33:18 -0700746 return net->ipv6.fib6_null_entry;
Wei Wang17ecf592017-10-06 12:06:09 -0700747
David Ahern93c2fb22018-04-18 15:38:59 -0700748 match = find_rr_leaf(fn, leaf, rt0, rt0->fib6_metric, oif, strict,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200749 &do_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200751 if (do_rr) {
David Ahern8d1c8022018-04-17 17:33:26 -0700752 struct fib6_info *next = rcu_dereference(rt0->rt6_next);
David S. Millerf11e6652007-03-24 20:36:25 -0700753
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800754 /* no entries matched; do round-robin */
David Ahern93c2fb22018-04-18 15:38:59 -0700755 if (!next || next->fib6_metric != rt0->fib6_metric)
Wei Wang8d1040e2017-10-06 12:06:08 -0700756 next = leaf;
David S. Millerf11e6652007-03-24 20:36:25 -0700757
Wei Wang66f5d6c2017-10-06 12:06:10 -0700758 if (next != rt0) {
David Ahern93c2fb22018-04-18 15:38:59 -0700759 spin_lock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700760 /* make sure next is not being deleted from the tree */
David Ahern93c2fb22018-04-18 15:38:59 -0700761 if (next->fib6_node)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700762 rcu_assign_pointer(fn->rr_ptr, next);
David Ahern93c2fb22018-04-18 15:38:59 -0700763 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766
David Ahern421842e2018-04-17 17:33:18 -0700767 return match ? match : net->ipv6.fib6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
David Ahern8d1c8022018-04-17 17:33:26 -0700770static bool rt6_is_gw_or_nonexthop(const struct fib6_info *rt)
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700771{
David Ahern93c2fb22018-04-18 15:38:59 -0700772 return (rt->fib6_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700773}
774
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800775#ifdef CONFIG_IPV6_ROUTE_INFO
776int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000777 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800778{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900779 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800780 struct route_info *rinfo = (struct route_info *) opt;
781 struct in6_addr prefix_buf, *prefix;
782 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900783 unsigned long lifetime;
David Ahern8d1c8022018-04-17 17:33:26 -0700784 struct fib6_info *rt;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800785
786 if (len < sizeof(struct route_info)) {
787 return -EINVAL;
788 }
789
790 /* Sanity check for prefix_len and length */
791 if (rinfo->length > 3) {
792 return -EINVAL;
793 } else if (rinfo->prefix_len > 128) {
794 return -EINVAL;
795 } else if (rinfo->prefix_len > 64) {
796 if (rinfo->length < 2) {
797 return -EINVAL;
798 }
799 } else if (rinfo->prefix_len > 0) {
800 if (rinfo->length < 1) {
801 return -EINVAL;
802 }
803 }
804
805 pref = rinfo->route_pref;
806 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000807 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800808
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900809 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800810
811 if (rinfo->length == 3)
812 prefix = (struct in6_addr *)rinfo->prefix;
813 else {
814 /* this function is safe */
815 ipv6_addr_prefix(&prefix_buf,
816 (struct in6_addr *)rinfo->prefix,
817 rinfo->prefix_len);
818 prefix = &prefix_buf;
819 }
820
Duan Jiongf104a562013-11-08 09:56:53 +0800821 if (rinfo->prefix_len == 0)
David Ahernafb1d4b52018-04-17 17:33:11 -0700822 rt = rt6_get_dflt_router(net, gwaddr, dev);
Duan Jiongf104a562013-11-08 09:56:53 +0800823 else
824 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
David Ahern830218c2016-10-24 10:52:35 -0700825 gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800826
827 if (rt && !lifetime) {
David Ahernafb1d4b52018-04-17 17:33:11 -0700828 ip6_del_rt(net, rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800829 rt = NULL;
830 }
831
832 if (!rt && lifetime)
David Ahern830218c2016-10-24 10:52:35 -0700833 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
834 dev, pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800835 else if (rt)
David Ahern93c2fb22018-04-18 15:38:59 -0700836 rt->fib6_flags = RTF_ROUTEINFO |
837 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800838
839 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000840 if (!addrconf_finite_timeout(lifetime))
David Ahern14895682018-04-17 17:33:17 -0700841 fib6_clean_expires(rt);
Gao feng1716a962012-04-06 00:13:10 +0000842 else
David Ahern14895682018-04-17 17:33:17 -0700843 fib6_set_expires(rt, jiffies + HZ * lifetime);
Gao feng1716a962012-04-06 00:13:10 +0000844
David Ahern93531c62018-04-17 17:33:25 -0700845 fib6_info_release(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800846 }
847 return 0;
848}
849#endif
850
David Ahernae90d862018-04-17 17:33:12 -0700851/*
852 * Misc support functions
853 */
854
855/* called with rcu_lock held */
David Ahern8d1c8022018-04-17 17:33:26 -0700856static struct net_device *ip6_rt_get_dev_rcu(struct fib6_info *rt)
David Ahernae90d862018-04-17 17:33:12 -0700857{
David Ahern5e670d82018-04-17 17:33:14 -0700858 struct net_device *dev = rt->fib6_nh.nh_dev;
David Ahernae90d862018-04-17 17:33:12 -0700859
David Ahern93c2fb22018-04-18 15:38:59 -0700860 if (rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
David Ahernae90d862018-04-17 17:33:12 -0700861 /* for copies of local routes, dst->dev needs to be the
862 * device if it is a master device, the master device if
863 * device is enslaved, and the loopback as the default
864 */
865 if (netif_is_l3_slave(dev) &&
David Ahern93c2fb22018-04-18 15:38:59 -0700866 !rt6_need_strict(&rt->fib6_dst.addr))
David Ahernae90d862018-04-17 17:33:12 -0700867 dev = l3mdev_master_dev_rcu(dev);
868 else if (!netif_is_l3_master(dev))
869 dev = dev_net(dev)->loopback_dev;
870 /* last case is netif_is_l3_master(dev) is true in which
871 * case we want dev returned to be dev
872 */
873 }
874
875 return dev;
876}
877
David Ahern6edb3c92018-04-17 17:33:15 -0700878static const int fib6_prop[RTN_MAX + 1] = {
879 [RTN_UNSPEC] = 0,
880 [RTN_UNICAST] = 0,
881 [RTN_LOCAL] = 0,
882 [RTN_BROADCAST] = 0,
883 [RTN_ANYCAST] = 0,
884 [RTN_MULTICAST] = 0,
885 [RTN_BLACKHOLE] = -EINVAL,
886 [RTN_UNREACHABLE] = -EHOSTUNREACH,
887 [RTN_PROHIBIT] = -EACCES,
888 [RTN_THROW] = -EAGAIN,
889 [RTN_NAT] = -EINVAL,
890 [RTN_XRESOLVE] = -EINVAL,
891};
892
893static int ip6_rt_type_to_error(u8 fib6_type)
894{
895 return fib6_prop[fib6_type];
896}
897
David Ahern8d1c8022018-04-17 17:33:26 -0700898static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
David Ahern3b6761d2018-04-17 17:33:20 -0700899{
900 unsigned short flags = 0;
901
902 if (rt->dst_nocount)
903 flags |= DST_NOCOUNT;
904 if (rt->dst_nopolicy)
905 flags |= DST_NOPOLICY;
906 if (rt->dst_host)
907 flags |= DST_HOST;
908
909 return flags;
910}
911
David Ahern8d1c8022018-04-17 17:33:26 -0700912static void ip6_rt_init_dst_reject(struct rt6_info *rt, struct fib6_info *ort)
David Ahern6edb3c92018-04-17 17:33:15 -0700913{
914 rt->dst.error = ip6_rt_type_to_error(ort->fib6_type);
915
916 switch (ort->fib6_type) {
917 case RTN_BLACKHOLE:
918 rt->dst.output = dst_discard_out;
919 rt->dst.input = dst_discard;
920 break;
921 case RTN_PROHIBIT:
922 rt->dst.output = ip6_pkt_prohibit_out;
923 rt->dst.input = ip6_pkt_prohibit;
924 break;
925 case RTN_THROW:
926 case RTN_UNREACHABLE:
927 default:
928 rt->dst.output = ip6_pkt_discard_out;
929 rt->dst.input = ip6_pkt_discard;
930 break;
931 }
932}
933
David Ahern8d1c8022018-04-17 17:33:26 -0700934static void ip6_rt_init_dst(struct rt6_info *rt, struct fib6_info *ort)
David Ahern6edb3c92018-04-17 17:33:15 -0700935{
David Ahern3b6761d2018-04-17 17:33:20 -0700936 rt->dst.flags |= fib6_info_dst_flags(ort);
937
David Ahern93c2fb22018-04-18 15:38:59 -0700938 if (ort->fib6_flags & RTF_REJECT) {
David Ahern6edb3c92018-04-17 17:33:15 -0700939 ip6_rt_init_dst_reject(rt, ort);
940 return;
941 }
942
943 rt->dst.error = 0;
944 rt->dst.output = ip6_output;
945
946 if (ort->fib6_type == RTN_LOCAL) {
David Ahern6edb3c92018-04-17 17:33:15 -0700947 rt->dst.input = ip6_input;
David Ahern93c2fb22018-04-18 15:38:59 -0700948 } else if (ipv6_addr_type(&ort->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
David Ahern6edb3c92018-04-17 17:33:15 -0700949 rt->dst.input = ip6_mc_input;
950 } else {
951 rt->dst.input = ip6_forward;
952 }
953
954 if (ort->fib6_nh.nh_lwtstate) {
955 rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.nh_lwtstate);
956 lwtunnel_set_redirect(&rt->dst);
957 }
958
959 rt->dst.lastuse = jiffies;
960}
961
David Ahern8d1c8022018-04-17 17:33:26 -0700962static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
David Ahernae90d862018-04-17 17:33:12 -0700963{
David Ahernae90d862018-04-17 17:33:12 -0700964 rt->rt6i_flags &= ~RTF_EXPIRES;
David Ahern93531c62018-04-17 17:33:25 -0700965 fib6_info_hold(from);
966 rt->from = from;
David Ahernd4ead6b2018-04-17 17:33:16 -0700967 dst_init_metrics(&rt->dst, from->fib6_metrics->metrics, true);
968 if (from->fib6_metrics != &dst_default_metrics) {
969 rt->dst._metrics |= DST_METRICS_REFCOUNTED;
970 refcount_inc(&from->fib6_metrics->refcnt);
971 }
David Ahernae90d862018-04-17 17:33:12 -0700972}
973
David Ahern8d1c8022018-04-17 17:33:26 -0700974static void ip6_rt_copy_init(struct rt6_info *rt, struct fib6_info *ort)
David Ahernae90d862018-04-17 17:33:12 -0700975{
David Aherndcd1f572018-04-18 15:39:05 -0700976 struct net_device *dev = fib6_info_nh_dev(ort);
977
David Ahern6edb3c92018-04-17 17:33:15 -0700978 ip6_rt_init_dst(rt, ort);
979
David Ahern93c2fb22018-04-18 15:38:59 -0700980 rt->rt6i_dst = ort->fib6_dst;
David Aherndcd1f572018-04-18 15:39:05 -0700981 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
David Ahern5e670d82018-04-17 17:33:14 -0700982 rt->rt6i_gateway = ort->fib6_nh.nh_gw;
David Ahern93c2fb22018-04-18 15:38:59 -0700983 rt->rt6i_flags = ort->fib6_flags;
David Ahernae90d862018-04-17 17:33:12 -0700984 rt6_set_from(rt, ort);
David Ahernae90d862018-04-17 17:33:12 -0700985#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -0700986 rt->rt6i_src = ort->fib6_src;
David Ahernae90d862018-04-17 17:33:12 -0700987#endif
David Ahern93c2fb22018-04-18 15:38:59 -0700988 rt->rt6i_prefsrc = ort->fib6_prefsrc;
David Ahern5e670d82018-04-17 17:33:14 -0700989 rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.nh_lwtstate);
David Ahernae90d862018-04-17 17:33:12 -0700990}
991
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700992static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
993 struct in6_addr *saddr)
994{
Wei Wang66f5d6c2017-10-06 12:06:10 -0700995 struct fib6_node *pn, *sn;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -0700996 while (1) {
997 if (fn->fn_flags & RTN_TL_ROOT)
998 return NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -0700999 pn = rcu_dereference(fn->parent);
1000 sn = FIB6_SUBTREE(pn);
1001 if (sn && sn != fn)
1002 fn = fib6_lookup(sn, NULL, saddr);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001003 else
1004 fn = pn;
1005 if (fn->fn_flags & RTN_RTINFO)
1006 return fn;
1007 }
1008}
Thomas Grafc71099a2006-08-04 23:20:06 -07001009
Wei Wangd3843fe2017-10-06 12:06:06 -07001010static bool ip6_hold_safe(struct net *net, struct rt6_info **prt,
1011 bool null_fallback)
1012{
1013 struct rt6_info *rt = *prt;
1014
1015 if (dst_hold_safe(&rt->dst))
1016 return true;
1017 if (null_fallback) {
1018 rt = net->ipv6.ip6_null_entry;
1019 dst_hold(&rt->dst);
1020 } else {
1021 rt = NULL;
1022 }
1023 *prt = rt;
1024 return false;
1025}
1026
David Aherndec9b0e2018-04-17 17:33:19 -07001027/* called with rcu_lock held */
David Ahern8d1c8022018-04-17 17:33:26 -07001028static struct rt6_info *ip6_create_rt_rcu(struct fib6_info *rt)
David Aherndec9b0e2018-04-17 17:33:19 -07001029{
David Ahern3b6761d2018-04-17 17:33:20 -07001030 unsigned short flags = fib6_info_dst_flags(rt);
David Aherndec9b0e2018-04-17 17:33:19 -07001031 struct net_device *dev = rt->fib6_nh.nh_dev;
1032 struct rt6_info *nrt;
1033
David Ahern93531c62018-04-17 17:33:25 -07001034 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
David Aherndec9b0e2018-04-17 17:33:19 -07001035 if (nrt)
1036 ip6_rt_copy_init(nrt, rt);
1037
1038 return nrt;
1039}
1040
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001041static struct rt6_info *ip6_pol_route_lookup(struct net *net,
1042 struct fib6_table *table,
David Ahernb75cc8f2018-03-02 08:32:17 -08001043 struct flowi6 *fl6,
1044 const struct sk_buff *skb,
1045 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
David Ahern8d1c8022018-04-17 17:33:26 -07001047 struct fib6_info *f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 struct fib6_node *fn;
David Ahern23fb93a2018-04-17 17:33:23 -07001049 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
David Ahernb6cdbc82018-03-29 17:44:57 -07001051 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1052 flags &= ~RT6_LOOKUP_F_IFACE;
1053
Wei Wang66f5d6c2017-10-06 12:06:10 -07001054 rcu_read_lock();
David S. Miller4c9483b2011-03-12 16:22:43 -05001055 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -07001056restart:
David Ahern23fb93a2018-04-17 17:33:23 -07001057 f6i = rcu_dereference(fn->leaf);
1058 if (!f6i) {
1059 f6i = net->ipv6.fib6_null_entry;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001060 } else {
David Ahern23fb93a2018-04-17 17:33:23 -07001061 f6i = rt6_device_match(net, f6i, &fl6->saddr,
Wei Wang66f5d6c2017-10-06 12:06:10 -07001062 fl6->flowi6_oif, flags);
David Ahern93c2fb22018-04-18 15:38:59 -07001063 if (f6i->fib6_nsiblings && fl6->flowi6_oif == 0)
David Ahern23fb93a2018-04-17 17:33:23 -07001064 f6i = rt6_multipath_select(net, f6i, fl6,
1065 fl6->flowi6_oif, skb, flags);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001066 }
David Ahern23fb93a2018-04-17 17:33:23 -07001067 if (f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001068 fn = fib6_backtrack(fn, &fl6->saddr);
1069 if (fn)
1070 goto restart;
1071 }
David Ahern23fb93a2018-04-17 17:33:23 -07001072
Wei Wang2b760fc2017-10-06 12:06:03 -07001073 /* Search through exception table */
David Ahern23fb93a2018-04-17 17:33:23 -07001074 rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
1075 if (rt) {
David Aherndec9b0e2018-04-17 17:33:19 -07001076 if (ip6_hold_safe(net, &rt, true))
1077 dst_use_noref(&rt->dst, jiffies);
David Ahern23fb93a2018-04-17 17:33:23 -07001078 } else if (f6i == net->ipv6.fib6_null_entry) {
David Aherndec9b0e2018-04-17 17:33:19 -07001079 rt = net->ipv6.ip6_null_entry;
1080 dst_hold(&rt->dst);
David Ahern23fb93a2018-04-17 17:33:23 -07001081 } else {
1082 rt = ip6_create_rt_rcu(f6i);
1083 if (!rt) {
1084 rt = net->ipv6.ip6_null_entry;
1085 dst_hold(&rt->dst);
1086 }
David Aherndec9b0e2018-04-17 17:33:19 -07001087 }
Wei Wangd3843fe2017-10-06 12:06:06 -07001088
Wei Wang66f5d6c2017-10-06 12:06:10 -07001089 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08001090
Paolo Abenib65f1642017-10-19 09:31:43 +02001091 trace_fib6_table_lookup(net, rt, table, fl6);
David Ahernb8115802015-11-19 12:24:22 -08001092
Thomas Grafc71099a2006-08-04 23:20:06 -07001093 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001094}
1095
Ian Morris67ba4152014-08-24 21:53:10 +01001096struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08001097 const struct sk_buff *skb, int flags)
Florian Westphalea6e5742011-09-05 16:05:44 +02001098{
David Ahernb75cc8f2018-03-02 08:32:17 -08001099 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
Florian Westphalea6e5742011-09-05 16:05:44 +02001100}
1101EXPORT_SYMBOL_GPL(ip6_route_lookup);
1102
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001103struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
David Ahernb75cc8f2018-03-02 08:32:17 -08001104 const struct in6_addr *saddr, int oif,
1105 const struct sk_buff *skb, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -07001106{
David S. Miller4c9483b2011-03-12 16:22:43 -05001107 struct flowi6 fl6 = {
1108 .flowi6_oif = oif,
1109 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001110 };
1111 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001112 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07001113
Thomas Grafadaa70b2006-10-13 15:01:03 -07001114 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -05001115 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -07001116 flags |= RT6_LOOKUP_F_HAS_SADDR;
1117 }
1118
David Ahernb75cc8f2018-03-02 08:32:17 -08001119 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -07001120 if (dst->error == 0)
1121 return (struct rt6_info *) dst;
1122
1123 dst_release(dst);
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return NULL;
1126}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001127EXPORT_SYMBOL(rt6_lookup);
1128
Thomas Grafc71099a2006-08-04 23:20:06 -07001129/* ip6_ins_rt is called with FREE table->tb6_lock.
Wei Wang1cfb71e2017-06-17 10:42:33 -07001130 * It takes new route entry, the addition fails by any reason the
1131 * route is released.
1132 * Caller must hold dst before calling it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 */
1134
David Ahern8d1c8022018-04-17 17:33:26 -07001135static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
David Ahern333c4302017-05-21 10:12:04 -06001136 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001139 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
David Ahern93c2fb22018-04-18 15:38:59 -07001141 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001142 spin_lock_bh(&table->tb6_lock);
David Ahernd4ead6b2018-04-17 17:33:16 -07001143 err = fib6_add(&table->tb6_root, rt, info, extack);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001144 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 return err;
1147}
1148
David Ahern8d1c8022018-04-17 17:33:26 -07001149int ip6_ins_rt(struct net *net, struct fib6_info *rt)
Thomas Graf40e22e82006-08-22 00:00:45 -07001150{
David Ahernafb1d4b52018-04-17 17:33:11 -07001151 struct nl_info info = { .nl_net = net, };
Florian Westphale715b6d2015-01-05 23:57:44 +01001152
David Ahernd4ead6b2018-04-17 17:33:16 -07001153 return __ip6_ins_rt(rt, &info, NULL);
Thomas Graf40e22e82006-08-22 00:00:45 -07001154}
1155
David Ahern8d1c8022018-04-17 17:33:26 -07001156static struct rt6_info *ip6_rt_cache_alloc(struct fib6_info *ort,
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001157 const struct in6_addr *daddr,
1158 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
David Ahern4832c302017-08-17 12:17:20 -07001160 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 struct rt6_info *rt;
1162
1163 /*
1164 * Clone the route.
1165 */
1166
David Ahern4832c302017-08-17 12:17:20 -07001167 rcu_read_lock();
1168 dev = ip6_rt_get_dev_rcu(ort);
David Ahern93531c62018-04-17 17:33:25 -07001169 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
David Ahern4832c302017-08-17 12:17:20 -07001170 rcu_read_unlock();
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001171 if (!rt)
1172 return NULL;
1173
1174 ip6_rt_copy_init(rt, ort);
1175 rt->rt6i_flags |= RTF_CACHE;
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001176 rt->dst.flags |= DST_HOST;
1177 rt->rt6i_dst.addr = *daddr;
1178 rt->rt6i_dst.plen = 128;
1179
1180 if (!rt6_is_gw_or_nonexthop(ort)) {
David Ahern93c2fb22018-04-18 15:38:59 -07001181 if (ort->fib6_dst.plen != 128 &&
1182 ipv6_addr_equal(&ort->fib6_dst.addr, daddr))
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001183 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001185 if (rt->rt6i_src.plen && saddr) {
1186 rt->rt6i_src.addr = *saddr;
1187 rt->rt6i_src.plen = 128;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001188 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001189#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001192 return rt;
1193}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
David Ahern8d1c8022018-04-17 17:33:26 -07001195static struct rt6_info *ip6_rt_pcpu_alloc(struct fib6_info *rt)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001196{
David Ahern3b6761d2018-04-17 17:33:20 -07001197 unsigned short flags = fib6_info_dst_flags(rt);
David Ahern4832c302017-08-17 12:17:20 -07001198 struct net_device *dev;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001199 struct rt6_info *pcpu_rt;
1200
David Ahern4832c302017-08-17 12:17:20 -07001201 rcu_read_lock();
1202 dev = ip6_rt_get_dev_rcu(rt);
David Ahern93531c62018-04-17 17:33:25 -07001203 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
David Ahern4832c302017-08-17 12:17:20 -07001204 rcu_read_unlock();
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001205 if (!pcpu_rt)
1206 return NULL;
1207 ip6_rt_copy_init(pcpu_rt, rt);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001208 pcpu_rt->rt6i_flags |= RTF_PCPU;
1209 return pcpu_rt;
1210}
1211
Wei Wang66f5d6c2017-10-06 12:06:10 -07001212/* It should be called with rcu_read_lock() acquired */
David Ahern8d1c8022018-04-17 17:33:26 -07001213static struct rt6_info *rt6_get_pcpu_route(struct fib6_info *rt)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001214{
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001215 struct rt6_info *pcpu_rt, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001216
1217 p = this_cpu_ptr(rt->rt6i_pcpu);
1218 pcpu_rt = *p;
1219
David Ahernd4ead6b2018-04-17 17:33:16 -07001220 if (pcpu_rt)
1221 ip6_hold_safe(NULL, &pcpu_rt, false);
Wei Wangd3843fe2017-10-06 12:06:06 -07001222
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001223 return pcpu_rt;
1224}
1225
David Ahernafb1d4b52018-04-17 17:33:11 -07001226static struct rt6_info *rt6_make_pcpu_route(struct net *net,
David Ahern8d1c8022018-04-17 17:33:26 -07001227 struct fib6_info *rt)
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001228{
1229 struct rt6_info *pcpu_rt, *prev, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001230
1231 pcpu_rt = ip6_rt_pcpu_alloc(rt);
1232 if (!pcpu_rt) {
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001233 dst_hold(&net->ipv6.ip6_null_entry->dst);
1234 return net->ipv6.ip6_null_entry;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001235 }
1236
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001237 dst_hold(&pcpu_rt->dst);
Wei Wanga94b9362017-10-06 12:06:04 -07001238 p = this_cpu_ptr(rt->rt6i_pcpu);
1239 prev = cmpxchg(p, NULL, pcpu_rt);
Eric Dumazet951f7882017-10-08 21:07:18 -07001240 BUG_ON(prev);
Wei Wanga94b9362017-10-06 12:06:04 -07001241
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001242 return pcpu_rt;
1243}
1244
Wei Wang35732d02017-10-06 12:05:57 -07001245/* exception hash table implementation
1246 */
1247static DEFINE_SPINLOCK(rt6_exception_lock);
1248
1249/* Remove rt6_ex from hash table and free the memory
1250 * Caller must hold rt6_exception_lock
1251 */
1252static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1253 struct rt6_exception *rt6_ex)
1254{
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001255 struct net *net;
Wei Wang81eb8442017-10-06 12:06:11 -07001256
Wei Wang35732d02017-10-06 12:05:57 -07001257 if (!bucket || !rt6_ex)
1258 return;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001259
1260 net = dev_net(rt6_ex->rt6i->dst.dev);
Wei Wang35732d02017-10-06 12:05:57 -07001261 hlist_del_rcu(&rt6_ex->hlist);
David Ahern77634cc2018-04-17 17:33:27 -07001262 dst_release(&rt6_ex->rt6i->dst);
Wei Wang35732d02017-10-06 12:05:57 -07001263 kfree_rcu(rt6_ex, rcu);
1264 WARN_ON_ONCE(!bucket->depth);
1265 bucket->depth--;
Wei Wang81eb8442017-10-06 12:06:11 -07001266 net->ipv6.rt6_stats->fib_rt_cache--;
Wei Wang35732d02017-10-06 12:05:57 -07001267}
1268
1269/* Remove oldest rt6_ex in bucket and free the memory
1270 * Caller must hold rt6_exception_lock
1271 */
1272static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1273{
1274 struct rt6_exception *rt6_ex, *oldest = NULL;
1275
1276 if (!bucket)
1277 return;
1278
1279 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1280 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1281 oldest = rt6_ex;
1282 }
1283 rt6_remove_exception(bucket, oldest);
1284}
1285
1286static u32 rt6_exception_hash(const struct in6_addr *dst,
1287 const struct in6_addr *src)
1288{
1289 static u32 seed __read_mostly;
1290 u32 val;
1291
1292 net_get_random_once(&seed, sizeof(seed));
1293 val = jhash(dst, sizeof(*dst), seed);
1294
1295#ifdef CONFIG_IPV6_SUBTREES
1296 if (src)
1297 val = jhash(src, sizeof(*src), val);
1298#endif
1299 return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1300}
1301
1302/* Helper function to find the cached rt in the hash table
1303 * and update bucket pointer to point to the bucket for this
1304 * (daddr, saddr) pair
1305 * Caller must hold rt6_exception_lock
1306 */
1307static struct rt6_exception *
1308__rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1309 const struct in6_addr *daddr,
1310 const struct in6_addr *saddr)
1311{
1312 struct rt6_exception *rt6_ex;
1313 u32 hval;
1314
1315 if (!(*bucket) || !daddr)
1316 return NULL;
1317
1318 hval = rt6_exception_hash(daddr, saddr);
1319 *bucket += hval;
1320
1321 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1322 struct rt6_info *rt6 = rt6_ex->rt6i;
1323 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1324
1325#ifdef CONFIG_IPV6_SUBTREES
1326 if (matched && saddr)
1327 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1328#endif
1329 if (matched)
1330 return rt6_ex;
1331 }
1332 return NULL;
1333}
1334
1335/* Helper function to find the cached rt in the hash table
1336 * and update bucket pointer to point to the bucket for this
1337 * (daddr, saddr) pair
1338 * Caller must hold rcu_read_lock()
1339 */
1340static struct rt6_exception *
1341__rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1342 const struct in6_addr *daddr,
1343 const struct in6_addr *saddr)
1344{
1345 struct rt6_exception *rt6_ex;
1346 u32 hval;
1347
1348 WARN_ON_ONCE(!rcu_read_lock_held());
1349
1350 if (!(*bucket) || !daddr)
1351 return NULL;
1352
1353 hval = rt6_exception_hash(daddr, saddr);
1354 *bucket += hval;
1355
1356 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1357 struct rt6_info *rt6 = rt6_ex->rt6i;
1358 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1359
1360#ifdef CONFIG_IPV6_SUBTREES
1361 if (matched && saddr)
1362 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1363#endif
1364 if (matched)
1365 return rt6_ex;
1366 }
1367 return NULL;
1368}
1369
David Ahern8d1c8022018-04-17 17:33:26 -07001370static unsigned int fib6_mtu(const struct fib6_info *rt)
David Ahernd4ead6b2018-04-17 17:33:16 -07001371{
1372 unsigned int mtu;
1373
David Aherndcd1f572018-04-18 15:39:05 -07001374 if (rt->fib6_pmtu) {
1375 mtu = rt->fib6_pmtu;
1376 } else {
1377 struct net_device *dev = fib6_info_nh_dev(rt);
1378 struct inet6_dev *idev;
1379
1380 rcu_read_lock();
1381 idev = __in6_dev_get(dev);
1382 mtu = idev->cnf.mtu6;
1383 rcu_read_unlock();
1384 }
1385
David Ahernd4ead6b2018-04-17 17:33:16 -07001386 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1387
1388 return mtu - lwtunnel_headroom(rt->fib6_nh.nh_lwtstate, mtu);
1389}
1390
Wei Wang35732d02017-10-06 12:05:57 -07001391static int rt6_insert_exception(struct rt6_info *nrt,
David Ahern8d1c8022018-04-17 17:33:26 -07001392 struct fib6_info *ort)
Wei Wang35732d02017-10-06 12:05:57 -07001393{
David Ahern5e670d82018-04-17 17:33:14 -07001394 struct net *net = dev_net(nrt->dst.dev);
Wei Wang35732d02017-10-06 12:05:57 -07001395 struct rt6_exception_bucket *bucket;
1396 struct in6_addr *src_key = NULL;
1397 struct rt6_exception *rt6_ex;
1398 int err = 0;
1399
Wei Wang35732d02017-10-06 12:05:57 -07001400 spin_lock_bh(&rt6_exception_lock);
1401
1402 if (ort->exception_bucket_flushed) {
1403 err = -EINVAL;
1404 goto out;
1405 }
1406
1407 bucket = rcu_dereference_protected(ort->rt6i_exception_bucket,
1408 lockdep_is_held(&rt6_exception_lock));
1409 if (!bucket) {
1410 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1411 GFP_ATOMIC);
1412 if (!bucket) {
1413 err = -ENOMEM;
1414 goto out;
1415 }
1416 rcu_assign_pointer(ort->rt6i_exception_bucket, bucket);
1417 }
1418
1419#ifdef CONFIG_IPV6_SUBTREES
1420 /* rt6i_src.plen != 0 indicates ort is in subtree
1421 * and exception table is indexed by a hash of
1422 * both rt6i_dst and rt6i_src.
1423 * Otherwise, the exception table is indexed by
1424 * a hash of only rt6i_dst.
1425 */
David Ahern93c2fb22018-04-18 15:38:59 -07001426 if (ort->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001427 src_key = &nrt->rt6i_src.addr;
1428#endif
Wei Wang60006a42017-10-06 12:05:58 -07001429
1430 /* Update rt6i_prefsrc as it could be changed
1431 * in rt6_remove_prefsrc()
1432 */
David Ahern93c2fb22018-04-18 15:38:59 -07001433 nrt->rt6i_prefsrc = ort->fib6_prefsrc;
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001434 /* rt6_mtu_change() might lower mtu on ort.
1435 * Only insert this exception route if its mtu
1436 * is less than ort's mtu value.
1437 */
David Ahernd4ead6b2018-04-17 17:33:16 -07001438 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(ort)) {
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001439 err = -EINVAL;
1440 goto out;
1441 }
Wei Wang60006a42017-10-06 12:05:58 -07001442
Wei Wang35732d02017-10-06 12:05:57 -07001443 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1444 src_key);
1445 if (rt6_ex)
1446 rt6_remove_exception(bucket, rt6_ex);
1447
1448 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1449 if (!rt6_ex) {
1450 err = -ENOMEM;
1451 goto out;
1452 }
1453 rt6_ex->rt6i = nrt;
1454 rt6_ex->stamp = jiffies;
Wei Wang35732d02017-10-06 12:05:57 -07001455 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1456 bucket->depth++;
Wei Wang81eb8442017-10-06 12:06:11 -07001457 net->ipv6.rt6_stats->fib_rt_cache++;
Wei Wang35732d02017-10-06 12:05:57 -07001458
1459 if (bucket->depth > FIB6_MAX_DEPTH)
1460 rt6_exception_remove_oldest(bucket);
1461
1462out:
1463 spin_unlock_bh(&rt6_exception_lock);
1464
1465 /* Update fn->fn_sernum to invalidate all cached dst */
Paolo Abenib886d5f2017-10-19 16:07:10 +02001466 if (!err) {
David Ahern93c2fb22018-04-18 15:38:59 -07001467 spin_lock_bh(&ort->fib6_table->tb6_lock);
David Ahern7aef6852018-04-17 17:33:10 -07001468 fib6_update_sernum(net, ort);
David Ahern93c2fb22018-04-18 15:38:59 -07001469 spin_unlock_bh(&ort->fib6_table->tb6_lock);
Paolo Abenib886d5f2017-10-19 16:07:10 +02001470 fib6_force_start_gc(net);
1471 }
Wei Wang35732d02017-10-06 12:05:57 -07001472
1473 return err;
1474}
1475
David Ahern8d1c8022018-04-17 17:33:26 -07001476void rt6_flush_exceptions(struct fib6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001477{
1478 struct rt6_exception_bucket *bucket;
1479 struct rt6_exception *rt6_ex;
1480 struct hlist_node *tmp;
1481 int i;
1482
1483 spin_lock_bh(&rt6_exception_lock);
1484 /* Prevent rt6_insert_exception() to recreate the bucket list */
1485 rt->exception_bucket_flushed = 1;
1486
1487 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1488 lockdep_is_held(&rt6_exception_lock));
1489 if (!bucket)
1490 goto out;
1491
1492 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1493 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
1494 rt6_remove_exception(bucket, rt6_ex);
1495 WARN_ON_ONCE(bucket->depth);
1496 bucket++;
1497 }
1498
1499out:
1500 spin_unlock_bh(&rt6_exception_lock);
1501}
1502
1503/* Find cached rt in the hash table inside passed in rt
1504 * Caller has to hold rcu_read_lock()
1505 */
David Ahern8d1c8022018-04-17 17:33:26 -07001506static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt,
Wei Wang35732d02017-10-06 12:05:57 -07001507 struct in6_addr *daddr,
1508 struct in6_addr *saddr)
1509{
1510 struct rt6_exception_bucket *bucket;
1511 struct in6_addr *src_key = NULL;
1512 struct rt6_exception *rt6_ex;
1513 struct rt6_info *res = NULL;
1514
1515 bucket = rcu_dereference(rt->rt6i_exception_bucket);
1516
1517#ifdef CONFIG_IPV6_SUBTREES
1518 /* rt6i_src.plen != 0 indicates rt is in subtree
1519 * and exception table is indexed by a hash of
1520 * both rt6i_dst and rt6i_src.
1521 * Otherwise, the exception table is indexed by
1522 * a hash of only rt6i_dst.
1523 */
David Ahern93c2fb22018-04-18 15:38:59 -07001524 if (rt->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001525 src_key = saddr;
1526#endif
1527 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1528
1529 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
1530 res = rt6_ex->rt6i;
1531
1532 return res;
1533}
1534
1535/* Remove the passed in cached rt from the hash table that contains it */
David Ahern23fb93a2018-04-17 17:33:23 -07001536static int rt6_remove_exception_rt(struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001537{
Wei Wang35732d02017-10-06 12:05:57 -07001538 struct rt6_exception_bucket *bucket;
David Ahern8d1c8022018-04-17 17:33:26 -07001539 struct fib6_info *from = rt->from;
Wei Wang35732d02017-10-06 12:05:57 -07001540 struct in6_addr *src_key = NULL;
1541 struct rt6_exception *rt6_ex;
1542 int err;
1543
1544 if (!from ||
Colin Ian King442d7132017-10-10 19:10:30 +01001545 !(rt->rt6i_flags & RTF_CACHE))
Wei Wang35732d02017-10-06 12:05:57 -07001546 return -EINVAL;
1547
1548 if (!rcu_access_pointer(from->rt6i_exception_bucket))
1549 return -ENOENT;
1550
1551 spin_lock_bh(&rt6_exception_lock);
1552 bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
1553 lockdep_is_held(&rt6_exception_lock));
1554#ifdef CONFIG_IPV6_SUBTREES
1555 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1556 * and exception table is indexed by a hash of
1557 * both rt6i_dst and rt6i_src.
1558 * Otherwise, the exception table is indexed by
1559 * a hash of only rt6i_dst.
1560 */
David Ahern93c2fb22018-04-18 15:38:59 -07001561 if (from->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001562 src_key = &rt->rt6i_src.addr;
1563#endif
1564 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1565 &rt->rt6i_dst.addr,
1566 src_key);
1567 if (rt6_ex) {
1568 rt6_remove_exception(bucket, rt6_ex);
1569 err = 0;
1570 } else {
1571 err = -ENOENT;
1572 }
1573
1574 spin_unlock_bh(&rt6_exception_lock);
1575 return err;
1576}
1577
1578/* Find rt6_ex which contains the passed in rt cache and
1579 * refresh its stamp
1580 */
1581static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1582{
Wei Wang35732d02017-10-06 12:05:57 -07001583 struct rt6_exception_bucket *bucket;
David Ahern8d1c8022018-04-17 17:33:26 -07001584 struct fib6_info *from = rt->from;
Wei Wang35732d02017-10-06 12:05:57 -07001585 struct in6_addr *src_key = NULL;
1586 struct rt6_exception *rt6_ex;
1587
1588 if (!from ||
Colin Ian King442d7132017-10-10 19:10:30 +01001589 !(rt->rt6i_flags & RTF_CACHE))
Wei Wang35732d02017-10-06 12:05:57 -07001590 return;
1591
1592 rcu_read_lock();
1593 bucket = rcu_dereference(from->rt6i_exception_bucket);
1594
1595#ifdef CONFIG_IPV6_SUBTREES
1596 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1597 * and exception table is indexed by a hash of
1598 * both rt6i_dst and rt6i_src.
1599 * Otherwise, the exception table is indexed by
1600 * a hash of only rt6i_dst.
1601 */
David Ahern93c2fb22018-04-18 15:38:59 -07001602 if (from->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001603 src_key = &rt->rt6i_src.addr;
1604#endif
1605 rt6_ex = __rt6_find_exception_rcu(&bucket,
1606 &rt->rt6i_dst.addr,
1607 src_key);
1608 if (rt6_ex)
1609 rt6_ex->stamp = jiffies;
1610
1611 rcu_read_unlock();
1612}
1613
David Ahern8d1c8022018-04-17 17:33:26 -07001614static void rt6_exceptions_remove_prefsrc(struct fib6_info *rt)
Wei Wang60006a42017-10-06 12:05:58 -07001615{
1616 struct rt6_exception_bucket *bucket;
1617 struct rt6_exception *rt6_ex;
1618 int i;
1619
1620 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1621 lockdep_is_held(&rt6_exception_lock));
1622
1623 if (bucket) {
1624 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1625 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1626 rt6_ex->rt6i->rt6i_prefsrc.plen = 0;
1627 }
1628 bucket++;
1629 }
1630 }
1631}
1632
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001633static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1634 struct rt6_info *rt, int mtu)
1635{
1636 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1637 * lowest MTU in the path: always allow updating the route PMTU to
1638 * reflect PMTU decreases.
1639 *
1640 * If the new MTU is higher, and the route PMTU is equal to the local
1641 * MTU, this means the old MTU is the lowest in the path, so allow
1642 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1643 * handle this.
1644 */
1645
1646 if (dst_mtu(&rt->dst) >= mtu)
1647 return true;
1648
1649 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1650 return true;
1651
1652 return false;
1653}
1654
1655static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
David Ahern8d1c8022018-04-17 17:33:26 -07001656 struct fib6_info *rt, int mtu)
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001657{
1658 struct rt6_exception_bucket *bucket;
1659 struct rt6_exception *rt6_ex;
1660 int i;
1661
1662 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1663 lockdep_is_held(&rt6_exception_lock));
1664
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001665 if (!bucket)
1666 return;
1667
1668 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1669 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1670 struct rt6_info *entry = rt6_ex->rt6i;
1671
1672 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
David Ahernd4ead6b2018-04-17 17:33:16 -07001673 * route), the metrics of its rt->from have already
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001674 * been updated.
1675 */
David Ahernd4ead6b2018-04-17 17:33:16 -07001676 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001677 rt6_mtu_change_route_allowed(idev, entry, mtu))
David Ahernd4ead6b2018-04-17 17:33:16 -07001678 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001679 }
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001680 bucket++;
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001681 }
1682}
1683
Wei Wangb16cb452017-10-06 12:06:00 -07001684#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
1685
David Ahern8d1c8022018-04-17 17:33:26 -07001686static void rt6_exceptions_clean_tohost(struct fib6_info *rt,
Wei Wangb16cb452017-10-06 12:06:00 -07001687 struct in6_addr *gateway)
1688{
1689 struct rt6_exception_bucket *bucket;
1690 struct rt6_exception *rt6_ex;
1691 struct hlist_node *tmp;
1692 int i;
1693
1694 if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1695 return;
1696
1697 spin_lock_bh(&rt6_exception_lock);
1698 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1699 lockdep_is_held(&rt6_exception_lock));
1700
1701 if (bucket) {
1702 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1703 hlist_for_each_entry_safe(rt6_ex, tmp,
1704 &bucket->chain, hlist) {
1705 struct rt6_info *entry = rt6_ex->rt6i;
1706
1707 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1708 RTF_CACHE_GATEWAY &&
1709 ipv6_addr_equal(gateway,
1710 &entry->rt6i_gateway)) {
1711 rt6_remove_exception(bucket, rt6_ex);
1712 }
1713 }
1714 bucket++;
1715 }
1716 }
1717
1718 spin_unlock_bh(&rt6_exception_lock);
1719}
1720
Wei Wangc757faa2017-10-06 12:06:01 -07001721static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1722 struct rt6_exception *rt6_ex,
1723 struct fib6_gc_args *gc_args,
1724 unsigned long now)
1725{
1726 struct rt6_info *rt = rt6_ex->rt6i;
1727
Paolo Abeni1859bac2017-10-19 16:07:11 +02001728 /* we are pruning and obsoleting aged-out and non gateway exceptions
1729 * even if others have still references to them, so that on next
1730 * dst_check() such references can be dropped.
1731 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
1732 * expired, independently from their aging, as per RFC 8201 section 4
1733 */
Wei Wang31afeb42018-01-26 11:40:17 -08001734 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
1735 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1736 RT6_TRACE("aging clone %p\n", rt);
1737 rt6_remove_exception(bucket, rt6_ex);
1738 return;
1739 }
1740 } else if (time_after(jiffies, rt->dst.expires)) {
1741 RT6_TRACE("purging expired route %p\n", rt);
Wei Wangc757faa2017-10-06 12:06:01 -07001742 rt6_remove_exception(bucket, rt6_ex);
1743 return;
Wei Wang31afeb42018-01-26 11:40:17 -08001744 }
1745
1746 if (rt->rt6i_flags & RTF_GATEWAY) {
Wei Wangc757faa2017-10-06 12:06:01 -07001747 struct neighbour *neigh;
1748 __u8 neigh_flags = 0;
1749
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001750 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
1751 if (neigh)
Wei Wangc757faa2017-10-06 12:06:01 -07001752 neigh_flags = neigh->flags;
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001753
Wei Wangc757faa2017-10-06 12:06:01 -07001754 if (!(neigh_flags & NTF_ROUTER)) {
1755 RT6_TRACE("purging route %p via non-router but gateway\n",
1756 rt);
1757 rt6_remove_exception(bucket, rt6_ex);
1758 return;
1759 }
1760 }
Wei Wang31afeb42018-01-26 11:40:17 -08001761
Wei Wangc757faa2017-10-06 12:06:01 -07001762 gc_args->more++;
1763}
1764
David Ahern8d1c8022018-04-17 17:33:26 -07001765void rt6_age_exceptions(struct fib6_info *rt,
Wei Wangc757faa2017-10-06 12:06:01 -07001766 struct fib6_gc_args *gc_args,
1767 unsigned long now)
1768{
1769 struct rt6_exception_bucket *bucket;
1770 struct rt6_exception *rt6_ex;
1771 struct hlist_node *tmp;
1772 int i;
1773
1774 if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1775 return;
1776
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001777 rcu_read_lock_bh();
1778 spin_lock(&rt6_exception_lock);
Wei Wangc757faa2017-10-06 12:06:01 -07001779 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1780 lockdep_is_held(&rt6_exception_lock));
1781
1782 if (bucket) {
1783 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1784 hlist_for_each_entry_safe(rt6_ex, tmp,
1785 &bucket->chain, hlist) {
1786 rt6_age_examine_exception(bucket, rt6_ex,
1787 gc_args, now);
1788 }
1789 bucket++;
1790 }
1791 }
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001792 spin_unlock(&rt6_exception_lock);
1793 rcu_read_unlock_bh();
Wei Wangc757faa2017-10-06 12:06:01 -07001794}
1795
David Ahern9ff74382016-06-13 13:44:19 -07001796struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
David Ahernb75cc8f2018-03-02 08:32:17 -08001797 int oif, struct flowi6 *fl6,
1798 const struct sk_buff *skb, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001800 struct fib6_node *fn, *saved_fn;
David Ahern8d1c8022018-04-17 17:33:26 -07001801 struct fib6_info *f6i;
David Ahern23fb93a2018-04-17 17:33:23 -07001802 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001803 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001805 strict |= flags & RT6_LOOKUP_F_IFACE;
David Ahernd5d32e42016-10-24 12:27:23 -07001806 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001807 if (net->ipv6.devconf_all->forwarding == 0)
1808 strict |= RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Wei Wang66f5d6c2017-10-06 12:06:10 -07001810 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
David S. Miller4c9483b2011-03-12 16:22:43 -05001812 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001813 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
David Ahernca254492015-10-12 11:47:10 -07001815 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1816 oif = 0;
1817
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001818redo_rt6_select:
David Ahern23fb93a2018-04-17 17:33:23 -07001819 f6i = rt6_select(net, fn, oif, strict);
David Ahern93c2fb22018-04-18 15:38:59 -07001820 if (f6i->fib6_nsiblings)
David Ahern23fb93a2018-04-17 17:33:23 -07001821 f6i = rt6_multipath_select(net, f6i, fl6, oif, skb, strict);
1822 if (f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001823 fn = fib6_backtrack(fn, &fl6->saddr);
1824 if (fn)
1825 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001826 else if (strict & RT6_LOOKUP_F_REACHABLE) {
1827 /* also consider unreachable route */
1828 strict &= ~RT6_LOOKUP_F_REACHABLE;
1829 fn = saved_fn;
1830 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001831 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001832 }
1833
David Ahern23fb93a2018-04-17 17:33:23 -07001834 if (f6i == net->ipv6.fib6_null_entry) {
David Ahern421842e2018-04-17 17:33:18 -07001835 rt = net->ipv6.ip6_null_entry;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001836 rcu_read_unlock();
Wei Wangd3843fe2017-10-06 12:06:06 -07001837 dst_hold(&rt->dst);
Paolo Abenib65f1642017-10-19 09:31:43 +02001838 trace_fib6_table_lookup(net, rt, table, fl6);
Wei Wangd3843fe2017-10-06 12:06:06 -07001839 return rt;
David Ahern23fb93a2018-04-17 17:33:23 -07001840 }
1841
1842 /*Search through exception table */
1843 rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
1844 if (rt) {
David Ahernd4ead6b2018-04-17 17:33:16 -07001845 if (ip6_hold_safe(net, &rt, true))
Wei Wangd3843fe2017-10-06 12:06:06 -07001846 dst_use_noref(&rt->dst, jiffies);
David Ahernd4ead6b2018-04-17 17:33:16 -07001847
Wei Wang66f5d6c2017-10-06 12:06:10 -07001848 rcu_read_unlock();
Paolo Abenib65f1642017-10-19 09:31:43 +02001849 trace_fib6_table_lookup(net, rt, table, fl6);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001850 return rt;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001851 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
David Ahern93c2fb22018-04-18 15:38:59 -07001852 !(f6i->fib6_flags & RTF_GATEWAY))) {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001853 /* Create a RTF_CACHE clone which will not be
1854 * owned by the fib6 tree. It is for the special case where
1855 * the daddr in the skb during the neighbor look-up is different
1856 * from the fl6->daddr used to look-up route here.
1857 */
Thomas Grafc71099a2006-08-04 23:20:06 -07001858
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001859 struct rt6_info *uncached_rt;
1860
David Ahern93531c62018-04-17 17:33:25 -07001861 fib6_info_hold(f6i);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001862 rcu_read_unlock();
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001863
David Ahern23fb93a2018-04-17 17:33:23 -07001864 uncached_rt = ip6_rt_cache_alloc(f6i, &fl6->daddr, NULL);
David Ahern93531c62018-04-17 17:33:25 -07001865 fib6_info_release(f6i);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001866
Wei Wang1cfb71e2017-06-17 10:42:33 -07001867 if (uncached_rt) {
1868 /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
1869 * No need for another dst_hold()
1870 */
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07001871 rt6_uncached_list_add(uncached_rt);
Wei Wang81eb8442017-10-06 12:06:11 -07001872 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Wei Wang1cfb71e2017-06-17 10:42:33 -07001873 } else {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001874 uncached_rt = net->ipv6.ip6_null_entry;
Wei Wang1cfb71e2017-06-17 10:42:33 -07001875 dst_hold(&uncached_rt->dst);
1876 }
David Ahernb8115802015-11-19 12:24:22 -08001877
Paolo Abenib65f1642017-10-19 09:31:43 +02001878 trace_fib6_table_lookup(net, uncached_rt, table, fl6);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001879 return uncached_rt;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001880
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001881 } else {
1882 /* Get a percpu copy */
1883
1884 struct rt6_info *pcpu_rt;
1885
Eric Dumazet951f7882017-10-08 21:07:18 -07001886 local_bh_disable();
David Ahern23fb93a2018-04-17 17:33:23 -07001887 pcpu_rt = rt6_get_pcpu_route(f6i);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001888
David Ahern93531c62018-04-17 17:33:25 -07001889 if (!pcpu_rt)
1890 pcpu_rt = rt6_make_pcpu_route(net, f6i);
1891
Eric Dumazet951f7882017-10-08 21:07:18 -07001892 local_bh_enable();
1893 rcu_read_unlock();
Paolo Abenib65f1642017-10-19 09:31:43 +02001894 trace_fib6_table_lookup(net, pcpu_rt, table, fl6);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001895 return pcpu_rt;
1896 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001897}
David Ahern9ff74382016-06-13 13:44:19 -07001898EXPORT_SYMBOL_GPL(ip6_pol_route);
Thomas Grafc71099a2006-08-04 23:20:06 -07001899
David Ahernb75cc8f2018-03-02 08:32:17 -08001900static struct rt6_info *ip6_pol_route_input(struct net *net,
1901 struct fib6_table *table,
1902 struct flowi6 *fl6,
1903 const struct sk_buff *skb,
1904 int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -07001905{
David Ahernb75cc8f2018-03-02 08:32:17 -08001906 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -07001907}
1908
Mahesh Bandeward409b842016-09-16 12:59:08 -07001909struct dst_entry *ip6_route_input_lookup(struct net *net,
1910 struct net_device *dev,
David Ahernb75cc8f2018-03-02 08:32:17 -08001911 struct flowi6 *fl6,
1912 const struct sk_buff *skb,
1913 int flags)
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001914{
1915 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
1916 flags |= RT6_LOOKUP_F_IFACE;
1917
David Ahernb75cc8f2018-03-02 08:32:17 -08001918 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001919}
Mahesh Bandeward409b842016-09-16 12:59:08 -07001920EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001921
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001922static void ip6_multipath_l3_keys(const struct sk_buff *skb,
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001923 struct flow_keys *keys,
1924 struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001925{
1926 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
1927 const struct ipv6hdr *key_iph = outer_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001928 struct flow_keys *_flkeys = flkeys;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001929 const struct ipv6hdr *inner_iph;
1930 const struct icmp6hdr *icmph;
1931 struct ipv6hdr _inner_iph;
1932
1933 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
1934 goto out;
1935
1936 icmph = icmp6_hdr(skb);
1937 if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
1938 icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
1939 icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
1940 icmph->icmp6_type != ICMPV6_PARAMPROB)
1941 goto out;
1942
1943 inner_iph = skb_header_pointer(skb,
1944 skb_transport_offset(skb) + sizeof(*icmph),
1945 sizeof(_inner_iph), &_inner_iph);
1946 if (!inner_iph)
1947 goto out;
1948
1949 key_iph = inner_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001950 _flkeys = NULL;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001951out:
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001952 if (_flkeys) {
1953 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
1954 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
1955 keys->tags.flow_label = _flkeys->tags.flow_label;
1956 keys->basic.ip_proto = _flkeys->basic.ip_proto;
1957 } else {
1958 keys->addrs.v6addrs.src = key_iph->saddr;
1959 keys->addrs.v6addrs.dst = key_iph->daddr;
1960 keys->tags.flow_label = ip6_flowinfo(key_iph);
1961 keys->basic.ip_proto = key_iph->nexthdr;
1962 }
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001963}
1964
1965/* if skb is set it will be used and fl6 can be NULL */
David Ahernb4bac172018-03-02 08:32:18 -08001966u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
1967 const struct sk_buff *skb, struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001968{
1969 struct flow_keys hash_keys;
David Ahern9a2a5372018-03-02 08:32:15 -08001970 u32 mhash;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001971
David S. Millerbbfa0472018-03-12 11:09:33 -04001972 switch (ip6_multipath_hash_policy(net)) {
David Ahernb4bac172018-03-02 08:32:18 -08001973 case 0:
1974 memset(&hash_keys, 0, sizeof(hash_keys));
1975 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1976 if (skb) {
1977 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
1978 } else {
1979 hash_keys.addrs.v6addrs.src = fl6->saddr;
1980 hash_keys.addrs.v6addrs.dst = fl6->daddr;
1981 hash_keys.tags.flow_label = (__force u32)fl6->flowlabel;
1982 hash_keys.basic.ip_proto = fl6->flowi6_proto;
1983 }
1984 break;
1985 case 1:
1986 if (skb) {
1987 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
1988 struct flow_keys keys;
1989
1990 /* short-circuit if we already have L4 hash present */
1991 if (skb->l4_hash)
1992 return skb_get_hash_raw(skb) >> 1;
1993
1994 memset(&hash_keys, 0, sizeof(hash_keys));
1995
1996 if (!flkeys) {
1997 skb_flow_dissect_flow_keys(skb, &keys, flag);
1998 flkeys = &keys;
1999 }
2000 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2001 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2002 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2003 hash_keys.ports.src = flkeys->ports.src;
2004 hash_keys.ports.dst = flkeys->ports.dst;
2005 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2006 } else {
2007 memset(&hash_keys, 0, sizeof(hash_keys));
2008 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2009 hash_keys.addrs.v6addrs.src = fl6->saddr;
2010 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2011 hash_keys.ports.src = fl6->fl6_sport;
2012 hash_keys.ports.dst = fl6->fl6_dport;
2013 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2014 }
2015 break;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002016 }
David Ahern9a2a5372018-03-02 08:32:15 -08002017 mhash = flow_hash_from_keys(&hash_keys);
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002018
David Ahern9a2a5372018-03-02 08:32:15 -08002019 return mhash >> 1;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002020}
2021
Thomas Grafc71099a2006-08-04 23:20:06 -07002022void ip6_route_input(struct sk_buff *skb)
2023{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002024 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002025 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002026 int flags = RT6_LOOKUP_F_HAS_SADDR;
Jiri Benc904af042015-08-20 13:56:31 +02002027 struct ip_tunnel_info *tun_info;
David S. Miller4c9483b2011-03-12 16:22:43 -05002028 struct flowi6 fl6 = {
David Aherne0d56fd2016-09-10 12:09:57 -07002029 .flowi6_iif = skb->dev->ifindex,
David S. Miller4c9483b2011-03-12 16:22:43 -05002030 .daddr = iph->daddr,
2031 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002032 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05002033 .flowi6_mark = skb->mark,
2034 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07002035 };
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002036 struct flow_keys *flkeys = NULL, _flkeys;
Thomas Grafadaa70b2006-10-13 15:01:03 -07002037
Jiri Benc904af042015-08-20 13:56:31 +02002038 tun_info = skb_tunnel_info(skb);
Jiri Benc46fa0622015-08-28 20:48:19 +02002039 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
Jiri Benc904af042015-08-20 13:56:31 +02002040 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002041
2042 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2043 flkeys = &_flkeys;
2044
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002045 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
David Ahernb4bac172018-03-02 08:32:18 -08002046 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
Jiri Benc06e9d042015-08-20 13:56:26 +02002047 skb_dst_drop(skb);
David Ahernb75cc8f2018-03-02 08:32:17 -08002048 skb_dst_set(skb,
2049 ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07002050}
2051
David Ahernb75cc8f2018-03-02 08:32:17 -08002052static struct rt6_info *ip6_pol_route_output(struct net *net,
2053 struct fib6_table *table,
2054 struct flowi6 *fl6,
2055 const struct sk_buff *skb,
2056 int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002057{
David Ahernb75cc8f2018-03-02 08:32:17 -08002058 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07002059}
2060
Paolo Abeni6f21c962016-01-29 12:30:19 +01002061struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
2062 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002063{
David Ahernd46a9d62015-10-21 08:42:22 -07002064 bool any_src;
Thomas Grafc71099a2006-08-04 23:20:06 -07002065
David Ahern4c1feac2016-09-10 12:09:56 -07002066 if (rt6_need_strict(&fl6->daddr)) {
2067 struct dst_entry *dst;
2068
2069 dst = l3mdev_link_scope_lookup(net, fl6);
2070 if (dst)
2071 return dst;
2072 }
David Ahernca254492015-10-12 11:47:10 -07002073
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002074 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00002075
David Ahernd46a9d62015-10-21 08:42:22 -07002076 any_src = ipv6_addr_any(&fl6->saddr);
David Ahern741a11d2015-09-28 10:12:13 -07002077 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
David Ahernd46a9d62015-10-21 08:42:22 -07002078 (fl6->flowi6_oif && any_src))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07002079 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07002080
David Ahernd46a9d62015-10-21 08:42:22 -07002081 if (!any_src)
Thomas Grafadaa70b2006-10-13 15:01:03 -07002082 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00002083 else if (sk)
2084 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002085
David Ahernb75cc8f2018-03-02 08:32:17 -08002086 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
Paolo Abeni6f21c962016-01-29 12:30:19 +01002088EXPORT_SYMBOL_GPL(ip6_route_output_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
David S. Miller2774c132011-03-01 14:59:04 -08002090struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07002091{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002092 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
Wei Wang1dbe32522017-06-17 10:42:26 -07002093 struct net_device *loopback_dev = net->loopback_dev;
David S. Miller14e50e52007-05-24 18:17:54 -07002094 struct dst_entry *new = NULL;
2095
Wei Wang1dbe32522017-06-17 10:42:26 -07002096 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
Steffen Klassert62cf27e2017-10-09 08:39:43 +02002097 DST_OBSOLETE_DEAD, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07002098 if (rt) {
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002099 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002100 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002101
Changli Gaod8d1f302010-06-10 23:31:35 -07002102 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07002103 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08002104 new->input = dst_discard;
Eric W. Biedermanede20592015-10-07 16:48:47 -05002105 new->output = dst_discard_out;
David S. Miller14e50e52007-05-24 18:17:54 -07002106
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002107 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07002108
Wei Wang1dbe32522017-06-17 10:42:26 -07002109 rt->rt6i_idev = in6_dev_get(loopback_dev);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002110 rt->rt6i_gateway = ort->rt6i_gateway;
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002111 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
David S. Miller14e50e52007-05-24 18:17:54 -07002112
2113 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2114#ifdef CONFIG_IPV6_SUBTREES
2115 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2116#endif
David S. Miller14e50e52007-05-24 18:17:54 -07002117 }
2118
David S. Miller69ead7a2011-03-01 14:45:33 -08002119 dst_release(dst_orig);
2120 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07002121}
David S. Miller14e50e52007-05-24 18:17:54 -07002122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123/*
2124 * Destination cache support functions
2125 */
2126
David Ahern8d1c8022018-04-17 17:33:26 -07002127static bool fib6_check(struct fib6_info *f6i, u32 cookie)
David Ahern93531c62018-04-17 17:33:25 -07002128{
2129 u32 rt_cookie = 0;
2130
2131 if ((f6i && !rt6_get_cookie_safe(f6i, &rt_cookie)) ||
2132 rt_cookie != cookie)
2133 return false;
2134
2135 if (fib6_check_expired(f6i))
2136 return false;
2137
2138 return true;
2139}
2140
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002141static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie)
2142{
Steffen Klassert36143642017-08-25 09:05:42 +02002143 u32 rt_cookie = 0;
Wei Wangc5cff852017-08-21 09:47:10 -07002144
David Ahern93531c62018-04-17 17:33:25 -07002145 if ((rt->from && !rt6_get_cookie_safe(rt->from, &rt_cookie)) ||
2146 rt_cookie != cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002147 return NULL;
2148
2149 if (rt6_check_expired(rt))
2150 return NULL;
2151
2152 return &rt->dst;
2153}
2154
2155static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie)
2156{
Martin KaFai Lau5973fb12015-11-11 11:51:07 -08002157 if (!__rt6_check_expired(rt) &&
2158 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
David Ahern93531c62018-04-17 17:33:25 -07002159 fib6_check(rt->from, cookie))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002160 return &rt->dst;
2161 else
2162 return NULL;
2163}
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2166{
2167 struct rt6_info *rt;
2168
2169 rt = (struct rt6_info *) dst;
2170
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00002171 /* All IPV6 dsts are created with ->obsolete set to the value
2172 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2173 * into this function always.
2174 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02002175
Martin KaFai Lau02bcf4e2015-11-11 11:51:08 -08002176 if (rt->rt6i_flags & RTF_PCPU ||
David Miller3a2232e2017-11-28 15:40:40 -05002177 (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->from))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002178 return rt6_dst_from_check(rt, cookie);
2179 else
2180 return rt6_check(rt, cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
2183static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2184{
2185 struct rt6_info *rt = (struct rt6_info *) dst;
2186
2187 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002188 if (rt->rt6i_flags & RTF_CACHE) {
2189 if (rt6_check_expired(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07002190 rt6_remove_exception_rt(rt);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002191 dst = NULL;
2192 }
2193 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002195 dst = NULL;
2196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002198 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199}
2200
2201static void ip6_link_failure(struct sk_buff *skb)
2202{
2203 struct rt6_info *rt;
2204
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002205 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Eric Dumazetadf30902009-06-02 05:19:30 +00002207 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (rt) {
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002209 if (rt->rt6i_flags & RTF_CACHE) {
Wei Wangad65a2f2017-06-17 10:42:35 -07002210 if (dst_hold_safe(&rt->dst))
David Ahern93531c62018-04-17 17:33:25 -07002211 rt6_remove_exception_rt(rt);
2212 } else if (rt->from) {
Wei Wangc5cff852017-08-21 09:47:10 -07002213 struct fib6_node *fn;
2214
2215 rcu_read_lock();
David Ahern93c2fb22018-04-18 15:38:59 -07002216 fn = rcu_dereference(rt->from->fib6_node);
Wei Wangc5cff852017-08-21 09:47:10 -07002217 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2218 fn->fn_sernum = -1;
2219 rcu_read_unlock();
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
2222}
2223
David Ahern6a3e0302018-04-20 15:37:57 -07002224static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2225{
2226 if (!(rt0->rt6i_flags & RTF_EXPIRES) && rt0->from)
2227 rt0->dst.expires = rt0->from->expires;
2228
2229 dst_set_expires(&rt0->dst, timeout);
2230 rt0->rt6i_flags |= RTF_EXPIRES;
2231}
2232
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002233static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2234{
2235 struct net *net = dev_net(rt->dst.dev);
2236
David Ahernd4ead6b2018-04-17 17:33:16 -07002237 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002238 rt->rt6i_flags |= RTF_MODIFIED;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002239 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2240}
2241
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002242static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2243{
2244 return !(rt->rt6i_flags & RTF_CACHE) &&
David Ahern77634cc2018-04-17 17:33:27 -07002245 (rt->rt6i_flags & RTF_PCPU || rt->from);
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002246}
2247
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002248static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2249 const struct ipv6hdr *iph, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
Julian Anastasov0dec8792017-02-06 23:14:16 +02002251 const struct in6_addr *daddr, *saddr;
Ian Morris67ba4152014-08-24 21:53:10 +01002252 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002254 if (rt6->rt6i_flags & RTF_LOCAL)
2255 return;
2256
Xin Long19bda362016-10-28 18:18:01 +08002257 if (dst_metric_locked(dst, RTAX_MTU))
2258 return;
2259
Julian Anastasov0dec8792017-02-06 23:14:16 +02002260 if (iph) {
2261 daddr = &iph->daddr;
2262 saddr = &iph->saddr;
2263 } else if (sk) {
2264 daddr = &sk->sk_v6_daddr;
2265 saddr = &inet6_sk(sk)->saddr;
2266 } else {
2267 daddr = NULL;
2268 saddr = NULL;
2269 }
2270 dst_confirm_neigh(dst, daddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002271 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2272 if (mtu >= dst_mtu(dst))
2273 return;
David S. Miller81aded22012-06-15 14:54:11 -07002274
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002275 if (!rt6_cache_allowed_for_pmtu(rt6)) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002276 rt6_do_update_pmtu(rt6, mtu);
Wei Wang2b760fc2017-10-06 12:06:03 -07002277 /* update rt6_ex->stamp for cache */
2278 if (rt6->rt6i_flags & RTF_CACHE)
2279 rt6_update_exception_stamp_rt(rt6);
Julian Anastasov0dec8792017-02-06 23:14:16 +02002280 } else if (daddr) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002281 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01002282
David Ahernd4ead6b2018-04-17 17:33:16 -07002283 nrt6 = ip6_rt_cache_alloc(rt6->from, daddr, saddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002284 if (nrt6) {
2285 rt6_do_update_pmtu(nrt6, mtu);
David Ahernd4ead6b2018-04-17 17:33:16 -07002286 if (rt6_insert_exception(nrt6, rt6->from))
Wei Wang2b760fc2017-10-06 12:06:03 -07002287 dst_release_immediate(&nrt6->dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
2290}
2291
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002292static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2293 struct sk_buff *skb, u32 mtu)
2294{
2295 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
2296}
2297
David S. Miller42ae66c2012-06-15 20:01:57 -07002298void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002299 int oif, u32 mark, kuid_t uid)
David S. Miller81aded22012-06-15 14:54:11 -07002300{
2301 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2302 struct dst_entry *dst;
2303 struct flowi6 fl6;
2304
2305 memset(&fl6, 0, sizeof(fl6));
2306 fl6.flowi6_oif = oif;
Lorenzo Colitti1b3c61d2014-05-13 10:17:34 -07002307 fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
David S. Miller81aded22012-06-15 14:54:11 -07002308 fl6.daddr = iph->daddr;
2309 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002310 fl6.flowlabel = ip6_flowinfo(iph);
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002311 fl6.flowi6_uid = uid;
David S. Miller81aded22012-06-15 14:54:11 -07002312
2313 dst = ip6_route_output(net, NULL, &fl6);
2314 if (!dst->error)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002315 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
David S. Miller81aded22012-06-15 14:54:11 -07002316 dst_release(dst);
2317}
2318EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2319
2320void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2321{
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002322 struct dst_entry *dst;
2323
David S. Miller81aded22012-06-15 14:54:11 -07002324 ip6_update_pmtu(skb, sock_net(sk), mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002325 sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid);
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002326
2327 dst = __sk_dst_get(sk);
2328 if (!dst || !dst->obsolete ||
2329 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2330 return;
2331
2332 bh_lock_sock(sk);
2333 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2334 ip6_datagram_dst_update(sk, false);
2335 bh_unlock_sock(sk);
David S. Miller81aded22012-06-15 14:54:11 -07002336}
2337EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2338
Alexey Kodanev7d6850f2018-04-03 15:00:07 +03002339void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2340 const struct flowi6 *fl6)
2341{
2342#ifdef CONFIG_IPV6_SUBTREES
2343 struct ipv6_pinfo *np = inet6_sk(sk);
2344#endif
2345
2346 ip6_dst_store(sk, dst,
2347 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2348 &sk->sk_v6_daddr : NULL,
2349#ifdef CONFIG_IPV6_SUBTREES
2350 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2351 &np->saddr :
2352#endif
2353 NULL);
2354}
2355
Duan Jiongb55b76b2013-09-04 19:44:21 +08002356/* Handle redirects */
2357struct ip6rd_flowi {
2358 struct flowi6 fl6;
2359 struct in6_addr gateway;
2360};
2361
2362static struct rt6_info *__ip6_route_redirect(struct net *net,
2363 struct fib6_table *table,
2364 struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08002365 const struct sk_buff *skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002366 int flags)
2367{
2368 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
David Ahern23fb93a2018-04-17 17:33:23 -07002369 struct rt6_info *ret = NULL, *rt_cache;
David Ahern8d1c8022018-04-17 17:33:26 -07002370 struct fib6_info *rt;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002371 struct fib6_node *fn;
2372
2373 /* Get the "current" route for this destination and
Alexander Alemayhu67c408c2017-01-07 23:53:00 +01002374 * check if the redirect has come from appropriate router.
Duan Jiongb55b76b2013-09-04 19:44:21 +08002375 *
2376 * RFC 4861 specifies that redirects should only be
2377 * accepted if they come from the nexthop to the target.
2378 * Due to the way the routes are chosen, this notion
2379 * is a bit fuzzy and one might need to check all possible
2380 * routes.
2381 */
2382
Wei Wang66f5d6c2017-10-06 12:06:10 -07002383 rcu_read_lock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08002384 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2385restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002386 for_each_fib6_node_rt_rcu(fn) {
David Ahern5e670d82018-04-17 17:33:14 -07002387 if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +02002388 continue;
David Ahern14895682018-04-17 17:33:17 -07002389 if (fib6_check_expired(rt))
Duan Jiongb55b76b2013-09-04 19:44:21 +08002390 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002391 if (rt->fib6_flags & RTF_REJECT)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002392 break;
David Ahern93c2fb22018-04-18 15:38:59 -07002393 if (!(rt->fib6_flags & RTF_GATEWAY))
Duan Jiongb55b76b2013-09-04 19:44:21 +08002394 continue;
David Ahern5e670d82018-04-17 17:33:14 -07002395 if (fl6->flowi6_oif != rt->fib6_nh.nh_dev->ifindex)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002396 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07002397 /* rt_cache's gateway might be different from its 'parent'
2398 * in the case of an ip redirect.
2399 * So we keep searching in the exception table if the gateway
2400 * is different.
2401 */
David Ahern5e670d82018-04-17 17:33:14 -07002402 if (!ipv6_addr_equal(&rdfl->gateway, &rt->fib6_nh.nh_gw)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07002403 rt_cache = rt6_find_cached_rt(rt,
2404 &fl6->daddr,
2405 &fl6->saddr);
2406 if (rt_cache &&
2407 ipv6_addr_equal(&rdfl->gateway,
2408 &rt_cache->rt6i_gateway)) {
David Ahern23fb93a2018-04-17 17:33:23 -07002409 ret = rt_cache;
Wei Wang2b760fc2017-10-06 12:06:03 -07002410 break;
2411 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002412 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07002413 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002414 break;
2415 }
2416
2417 if (!rt)
David Ahern421842e2018-04-17 17:33:18 -07002418 rt = net->ipv6.fib6_null_entry;
David Ahern93c2fb22018-04-18 15:38:59 -07002419 else if (rt->fib6_flags & RTF_REJECT) {
David Ahern23fb93a2018-04-17 17:33:23 -07002420 ret = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002421 goto out;
2422 }
2423
David Ahern421842e2018-04-17 17:33:18 -07002424 if (rt == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002425 fn = fib6_backtrack(fn, &fl6->saddr);
2426 if (fn)
2427 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002428 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002429
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002430out:
David Ahern23fb93a2018-04-17 17:33:23 -07002431 if (ret)
2432 dst_hold(&ret->dst);
2433 else
2434 ret = ip6_create_rt_rcu(rt);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002435
Wei Wang66f5d6c2017-10-06 12:06:10 -07002436 rcu_read_unlock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08002437
David Ahern23fb93a2018-04-17 17:33:23 -07002438 trace_fib6_table_lookup(net, ret, table, fl6);
2439 return ret;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002440};
2441
2442static struct dst_entry *ip6_route_redirect(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08002443 const struct flowi6 *fl6,
2444 const struct sk_buff *skb,
2445 const struct in6_addr *gateway)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002446{
2447 int flags = RT6_LOOKUP_F_HAS_SADDR;
2448 struct ip6rd_flowi rdfl;
2449
2450 rdfl.fl6 = *fl6;
2451 rdfl.gateway = *gateway;
2452
David Ahernb75cc8f2018-03-02 08:32:17 -08002453 return fib6_rule_lookup(net, &rdfl.fl6, skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002454 flags, __ip6_route_redirect);
2455}
2456
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002457void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2458 kuid_t uid)
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002459{
2460 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2461 struct dst_entry *dst;
2462 struct flowi6 fl6;
2463
2464 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03002465 fl6.flowi6_iif = LOOPBACK_IFINDEX;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002466 fl6.flowi6_oif = oif;
2467 fl6.flowi6_mark = mark;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002468 fl6.daddr = iph->daddr;
2469 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002470 fl6.flowlabel = ip6_flowinfo(iph);
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002471 fl6.flowi6_uid = uid;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002472
David Ahernb75cc8f2018-03-02 08:32:17 -08002473 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002474 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002475 dst_release(dst);
2476}
2477EXPORT_SYMBOL_GPL(ip6_redirect);
2478
Duan Jiongc92a59e2013-08-22 12:07:35 +08002479void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
2480 u32 mark)
2481{
2482 const struct ipv6hdr *iph = ipv6_hdr(skb);
2483 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2484 struct dst_entry *dst;
2485 struct flowi6 fl6;
2486
2487 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03002488 fl6.flowi6_iif = LOOPBACK_IFINDEX;
Duan Jiongc92a59e2013-08-22 12:07:35 +08002489 fl6.flowi6_oif = oif;
2490 fl6.flowi6_mark = mark;
Duan Jiongc92a59e2013-08-22 12:07:35 +08002491 fl6.daddr = msg->dest;
2492 fl6.saddr = iph->daddr;
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002493 fl6.flowi6_uid = sock_net_uid(net, NULL);
Duan Jiongc92a59e2013-08-22 12:07:35 +08002494
David Ahernb75cc8f2018-03-02 08:32:17 -08002495 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002496 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08002497 dst_release(dst);
2498}
2499
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002500void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
2501{
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002502 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2503 sk->sk_uid);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002504}
2505EXPORT_SYMBOL_GPL(ip6_sk_redirect);
2506
David S. Miller0dbaee32010-12-13 12:52:14 -08002507static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
David S. Miller0dbaee32010-12-13 12:52:14 -08002509 struct net_device *dev = dst->dev;
2510 unsigned int mtu = dst_mtu(dst);
2511 struct net *net = dev_net(dev);
2512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
2514
Daniel Lezcano55786892008-03-04 13:47:47 -08002515 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
2516 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002519 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
2520 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
2521 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 * rely only on pmtu discovery"
2523 */
2524 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
2525 mtu = IPV6_MAXPLEN;
2526 return mtu;
2527}
2528
Steffen Klassertebb762f2011-11-23 02:12:51 +00002529static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002530{
David S. Millerd33e4552010-12-14 13:01:14 -08002531 struct inet6_dev *idev;
David Ahernd4ead6b2018-04-17 17:33:16 -07002532 unsigned int mtu;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002533
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002534 mtu = dst_metric_raw(dst, RTAX_MTU);
2535 if (mtu)
2536 goto out;
2537
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002538 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08002539
2540 rcu_read_lock();
2541 idev = __in6_dev_get(dst->dev);
2542 if (idev)
2543 mtu = idev->cnf.mtu6;
2544 rcu_read_unlock();
2545
Eric Dumazet30f78d82014-04-10 21:23:36 -07002546out:
Roopa Prabhu14972cb2016-08-24 20:10:43 -07002547 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2548
2549 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
David S. Millerd33e4552010-12-14 13:01:14 -08002550}
2551
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08002552struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05002553 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
David S. Miller87a11572011-12-06 17:04:13 -05002555 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 struct rt6_info *rt;
2557 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002558 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
David S. Miller38308472011-12-03 18:02:47 -05002560 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00002561 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Martin KaFai Lauad706862015-08-14 11:05:52 -07002563 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05002564 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05002566 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 goto out;
2568 }
2569
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002570 rt->dst.flags |= DST_HOST;
Brendan McGrath588753f2017-12-13 22:14:57 +11002571 rt->dst.input = ip6_input;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002572 rt->dst.output = ip6_output;
Julian Anastasov550bab42013-10-20 15:43:04 +03002573 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05002574 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002575 rt->rt6i_dst.plen = 128;
2576 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08002577 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Ido Schimmel4c981e22018-01-07 12:45:04 +02002579 /* Add this dst into uncached_list so that rt6_disable_ip() can
Wei Wang587fea72017-06-17 10:42:36 -07002580 * do proper release of the net_device
2581 */
2582 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002583 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
David S. Miller87a11572011-12-06 17:04:13 -05002585 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
2586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587out:
David S. Miller87a11572011-12-06 17:04:13 -05002588 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589}
2590
Daniel Lezcano569d3642008-01-18 03:56:57 -08002591static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002593 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08002594 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
2595 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
2596 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
2597 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
2598 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00002599 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Eric Dumazetfc66f952010-10-08 06:37:34 +00002601 entries = dst_entries_get_fast(ops);
Michal Kubeček49a18d82013-08-01 10:04:24 +02002602 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00002603 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 goto out;
2605
Benjamin Thery6891a342008-03-04 13:49:47 -08002606 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08002607 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00002608 entries = dst_entries_get_slow(ops);
2609 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08002610 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08002612 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00002613 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
2615
David Ahern8d1c8022018-04-17 17:33:26 -07002616static int ip6_convert_metrics(struct net *net, struct fib6_info *rt,
David Ahernd4ead6b2018-04-17 17:33:16 -07002617 struct fib6_config *cfg)
Florian Westphale715b6d2015-01-05 23:57:44 +01002618{
Eric Dumazet263243d2018-04-19 09:14:53 -07002619 struct dst_metrics *p;
Florian Westphale715b6d2015-01-05 23:57:44 +01002620
Eric Dumazet263243d2018-04-19 09:14:53 -07002621 if (!cfg->fc_mx)
2622 return 0;
Florian Westphale715b6d2015-01-05 23:57:44 +01002623
Eric Dumazet263243d2018-04-19 09:14:53 -07002624 p = kzalloc(sizeof(*rt->fib6_metrics), GFP_KERNEL);
2625 if (unlikely(!p))
2626 return -ENOMEM;
Florian Westphale715b6d2015-01-05 23:57:44 +01002627
Eric Dumazet263243d2018-04-19 09:14:53 -07002628 refcount_set(&p->refcnt, 1);
2629 rt->fib6_metrics = p;
Florian Westphale715b6d2015-01-05 23:57:44 +01002630
Eric Dumazet263243d2018-04-19 09:14:53 -07002631 return ip_metrics_convert(net, cfg->fc_mx, cfg->fc_mx_len, p->metrics);
Florian Westphale715b6d2015-01-05 23:57:44 +01002632}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
David Ahern8c145862016-04-24 21:26:04 -07002634static struct rt6_info *ip6_nh_lookup_table(struct net *net,
2635 struct fib6_config *cfg,
David Ahernf4797b32018-01-25 16:55:08 -08002636 const struct in6_addr *gw_addr,
2637 u32 tbid, int flags)
David Ahern8c145862016-04-24 21:26:04 -07002638{
2639 struct flowi6 fl6 = {
2640 .flowi6_oif = cfg->fc_ifindex,
2641 .daddr = *gw_addr,
2642 .saddr = cfg->fc_prefsrc,
2643 };
2644 struct fib6_table *table;
2645 struct rt6_info *rt;
David Ahern8c145862016-04-24 21:26:04 -07002646
David Ahernf4797b32018-01-25 16:55:08 -08002647 table = fib6_get_table(net, tbid);
David Ahern8c145862016-04-24 21:26:04 -07002648 if (!table)
2649 return NULL;
2650
2651 if (!ipv6_addr_any(&cfg->fc_prefsrc))
2652 flags |= RT6_LOOKUP_F_HAS_SADDR;
2653
David Ahernf4797b32018-01-25 16:55:08 -08002654 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
David Ahernb75cc8f2018-03-02 08:32:17 -08002655 rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
David Ahern8c145862016-04-24 21:26:04 -07002656
2657 /* if table lookup failed, fall back to full lookup */
2658 if (rt == net->ipv6.ip6_null_entry) {
2659 ip6_rt_put(rt);
2660 rt = NULL;
2661 }
2662
2663 return rt;
2664}
2665
David Ahernfc1e64e2018-01-25 16:55:09 -08002666static int ip6_route_check_nh_onlink(struct net *net,
2667 struct fib6_config *cfg,
David Ahern9fbb7042018-03-13 08:29:36 -07002668 const struct net_device *dev,
David Ahernfc1e64e2018-01-25 16:55:09 -08002669 struct netlink_ext_ack *extack)
2670{
David Ahern44750f82018-02-06 13:17:06 -08002671 u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
David Ahernfc1e64e2018-01-25 16:55:09 -08002672 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2673 u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
2674 struct rt6_info *grt;
2675 int err;
2676
2677 err = 0;
2678 grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
2679 if (grt) {
David Ahern58e354c2018-02-06 12:14:12 -08002680 if (!grt->dst.error &&
2681 (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
David Ahern44750f82018-02-06 13:17:06 -08002682 NL_SET_ERR_MSG(extack,
2683 "Nexthop has invalid gateway or device mismatch");
David Ahernfc1e64e2018-01-25 16:55:09 -08002684 err = -EINVAL;
2685 }
2686
2687 ip6_rt_put(grt);
2688 }
2689
2690 return err;
2691}
2692
David Ahern1edce992018-01-25 16:55:07 -08002693static int ip6_route_check_nh(struct net *net,
2694 struct fib6_config *cfg,
2695 struct net_device **_dev,
2696 struct inet6_dev **idev)
2697{
2698 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2699 struct net_device *dev = _dev ? *_dev : NULL;
2700 struct rt6_info *grt = NULL;
2701 int err = -EHOSTUNREACH;
2702
2703 if (cfg->fc_table) {
David Ahernf4797b32018-01-25 16:55:08 -08002704 int flags = RT6_LOOKUP_F_IFACE;
2705
2706 grt = ip6_nh_lookup_table(net, cfg, gw_addr,
2707 cfg->fc_table, flags);
David Ahern1edce992018-01-25 16:55:07 -08002708 if (grt) {
2709 if (grt->rt6i_flags & RTF_GATEWAY ||
2710 (dev && dev != grt->dst.dev)) {
2711 ip6_rt_put(grt);
2712 grt = NULL;
2713 }
2714 }
2715 }
2716
2717 if (!grt)
David Ahernb75cc8f2018-03-02 08:32:17 -08002718 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
David Ahern1edce992018-01-25 16:55:07 -08002719
2720 if (!grt)
2721 goto out;
2722
2723 if (dev) {
2724 if (dev != grt->dst.dev) {
2725 ip6_rt_put(grt);
2726 goto out;
2727 }
2728 } else {
2729 *_dev = dev = grt->dst.dev;
2730 *idev = grt->rt6i_idev;
2731 dev_hold(dev);
2732 in6_dev_hold(grt->rt6i_idev);
2733 }
2734
2735 if (!(grt->rt6i_flags & RTF_GATEWAY))
2736 err = 0;
2737
2738 ip6_rt_put(grt);
2739
2740out:
2741 return err;
2742}
2743
David Ahern9fbb7042018-03-13 08:29:36 -07002744static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
2745 struct net_device **_dev, struct inet6_dev **idev,
2746 struct netlink_ext_ack *extack)
2747{
2748 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2749 int gwa_type = ipv6_addr_type(gw_addr);
David Ahern232378e2018-03-13 08:29:37 -07002750 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
David Ahern9fbb7042018-03-13 08:29:36 -07002751 const struct net_device *dev = *_dev;
David Ahern232378e2018-03-13 08:29:37 -07002752 bool need_addr_check = !dev;
David Ahern9fbb7042018-03-13 08:29:36 -07002753 int err = -EINVAL;
2754
2755 /* if gw_addr is local we will fail to detect this in case
2756 * address is still TENTATIVE (DAD in progress). rt6_lookup()
2757 * will return already-added prefix route via interface that
2758 * prefix route was assigned to, which might be non-loopback.
2759 */
David Ahern232378e2018-03-13 08:29:37 -07002760 if (dev &&
2761 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2762 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
David Ahern9fbb7042018-03-13 08:29:36 -07002763 goto out;
2764 }
2765
2766 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
2767 /* IPv6 strictly inhibits using not link-local
2768 * addresses as nexthop address.
2769 * Otherwise, router will not able to send redirects.
2770 * It is very good, but in some (rare!) circumstances
2771 * (SIT, PtP, NBMA NOARP links) it is handy to allow
2772 * some exceptions. --ANK
2773 * We allow IPv4-mapped nexthops to support RFC4798-type
2774 * addressing
2775 */
2776 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
2777 NL_SET_ERR_MSG(extack, "Invalid gateway address");
2778 goto out;
2779 }
2780
2781 if (cfg->fc_flags & RTNH_F_ONLINK)
2782 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
2783 else
2784 err = ip6_route_check_nh(net, cfg, _dev, idev);
2785
2786 if (err)
2787 goto out;
2788 }
2789
2790 /* reload in case device was changed */
2791 dev = *_dev;
2792
2793 err = -EINVAL;
2794 if (!dev) {
2795 NL_SET_ERR_MSG(extack, "Egress device not specified");
2796 goto out;
2797 } else if (dev->flags & IFF_LOOPBACK) {
2798 NL_SET_ERR_MSG(extack,
2799 "Egress device can not be loopback device for this route");
2800 goto out;
2801 }
David Ahern232378e2018-03-13 08:29:37 -07002802
2803 /* if we did not check gw_addr above, do so now that the
2804 * egress device has been resolved.
2805 */
2806 if (need_addr_check &&
2807 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2808 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
2809 goto out;
2810 }
2811
David Ahern9fbb7042018-03-13 08:29:36 -07002812 err = 0;
2813out:
2814 return err;
2815}
2816
David Ahern8d1c8022018-04-17 17:33:26 -07002817static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
David Ahernacb54e32018-04-17 17:33:22 -07002818 gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06002819 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
Daniel Lezcano55786892008-03-04 13:47:47 -08002821 struct net *net = cfg->fc_nlinfo.nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07002822 struct fib6_info *rt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 struct net_device *dev = NULL;
2824 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07002825 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 int addr_type;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002827 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
David Ahern557c44b2017-04-19 14:19:43 -07002829 /* RTF_PCPU is an internal flag; can not be set by userspace */
David Ahernd5d531c2017-05-21 10:12:05 -06002830 if (cfg->fc_flags & RTF_PCPU) {
2831 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
David Ahern557c44b2017-04-19 14:19:43 -07002832 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06002833 }
David Ahern557c44b2017-04-19 14:19:43 -07002834
Wei Wang2ea23522017-10-27 17:30:12 -07002835 /* RTF_CACHE is an internal flag; can not be set by userspace */
2836 if (cfg->fc_flags & RTF_CACHE) {
2837 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
2838 goto out;
2839 }
2840
David Aherne8478e82018-04-17 17:33:13 -07002841 if (cfg->fc_type > RTN_MAX) {
2842 NL_SET_ERR_MSG(extack, "Invalid route type");
2843 goto out;
2844 }
2845
David Ahernd5d531c2017-05-21 10:12:05 -06002846 if (cfg->fc_dst_len > 128) {
2847 NL_SET_ERR_MSG(extack, "Invalid prefix length");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002848 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06002849 }
2850 if (cfg->fc_src_len > 128) {
2851 NL_SET_ERR_MSG(extack, "Invalid source address length");
2852 goto out;
2853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854#ifndef CONFIG_IPV6_SUBTREES
David Ahernd5d531c2017-05-21 10:12:05 -06002855 if (cfg->fc_src_len) {
2856 NL_SET_ERR_MSG(extack,
2857 "Specifying source address requires IPV6_SUBTREES to be enabled");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002858 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06002859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07002861 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08002863 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (!dev)
2865 goto out;
2866 idev = in6_dev_get(dev);
2867 if (!idev)
2868 goto out;
2869 }
2870
Thomas Graf86872cb2006-08-22 00:01:08 -07002871 if (cfg->fc_metric == 0)
2872 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
David Ahernfc1e64e2018-01-25 16:55:09 -08002874 if (cfg->fc_flags & RTNH_F_ONLINK) {
2875 if (!dev) {
2876 NL_SET_ERR_MSG(extack,
2877 "Nexthop device required for onlink");
2878 err = -ENODEV;
2879 goto out;
2880 }
2881
2882 if (!(dev->flags & IFF_UP)) {
2883 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2884 err = -ENETDOWN;
2885 goto out;
2886 }
2887 }
2888
Matti Vaittinend71314b2011-11-14 00:14:49 +00002889 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05002890 if (cfg->fc_nlinfo.nlh &&
2891 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00002892 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05002893 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00002894 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00002895 table = fib6_new_table(net, cfg->fc_table);
2896 }
2897 } else {
2898 table = fib6_new_table(net, cfg->fc_table);
2899 }
David S. Miller38308472011-12-03 18:02:47 -05002900
2901 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002902 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07002903
David Ahern93531c62018-04-17 17:33:25 -07002904 err = -ENOMEM;
2905 rt = fib6_info_alloc(gfp_flags);
2906 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 goto out;
David Ahern93531c62018-04-17 17:33:25 -07002908
2909 if (cfg->fc_flags & RTF_ADDRCONF)
2910 rt->dst_nocount = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
David Ahernd4ead6b2018-04-17 17:33:16 -07002912 err = ip6_convert_metrics(net, rt, cfg);
2913 if (err < 0)
2914 goto out;
2915
Gao feng1716a962012-04-06 00:13:10 +00002916 if (cfg->fc_flags & RTF_EXPIRES)
David Ahern14895682018-04-17 17:33:17 -07002917 fib6_set_expires(rt, jiffies +
Gao feng1716a962012-04-06 00:13:10 +00002918 clock_t_to_jiffies(cfg->fc_expires));
2919 else
David Ahern14895682018-04-17 17:33:17 -07002920 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Thomas Graf86872cb2006-08-22 00:01:08 -07002922 if (cfg->fc_protocol == RTPROT_UNSPEC)
2923 cfg->fc_protocol = RTPROT_BOOT;
David Ahern93c2fb22018-04-18 15:38:59 -07002924 rt->fib6_protocol = cfg->fc_protocol;
Thomas Graf86872cb2006-08-22 00:01:08 -07002925
2926 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002928 if (cfg->fc_encap) {
2929 struct lwtunnel_state *lwtstate;
2930
David Ahern30357d72017-01-30 12:07:37 -08002931 err = lwtunnel_build_state(cfg->fc_encap_type,
Tom Herbert127eb7c2015-08-24 09:45:41 -07002932 cfg->fc_encap, AF_INET6, cfg,
David Ahern9ae28722017-05-27 16:19:28 -06002933 &lwtstate, extack);
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002934 if (err)
2935 goto out;
David Ahern5e670d82018-04-17 17:33:14 -07002936 rt->fib6_nh.nh_lwtstate = lwtstate_get(lwtstate);
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002937 }
2938
David Ahern93c2fb22018-04-18 15:38:59 -07002939 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
2940 rt->fib6_dst.plen = cfg->fc_dst_len;
2941 if (rt->fib6_dst.plen == 128)
David Ahern3b6761d2018-04-17 17:33:20 -07002942 rt->dst_host = true;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01002943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07002945 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
2946 rt->fib6_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947#endif
2948
David Ahern93c2fb22018-04-18 15:38:59 -07002949 rt->fib6_metric = cfg->fc_metric;
David Ahern5e670d82018-04-17 17:33:14 -07002950 rt->fib6_nh.nh_weight = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
David Aherne8478e82018-04-17 17:33:13 -07002952 rt->fib6_type = cfg->fc_type;
2953
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 /* We cannot add true routes via loopback here,
2955 they would result in kernel looping; promote them to reject routes
2956 */
Thomas Graf86872cb2006-08-22 00:01:08 -07002957 if ((cfg->fc_flags & RTF_REJECT) ||
David S. Miller38308472011-12-03 18:02:47 -05002958 (dev && (dev->flags & IFF_LOOPBACK) &&
2959 !(addr_type & IPV6_ADDR_LOOPBACK) &&
2960 !(cfg->fc_flags & RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08002962 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 if (dev) {
2964 dev_put(dev);
2965 in6_dev_put(idev);
2966 }
Daniel Lezcano55786892008-03-04 13:47:47 -08002967 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 dev_hold(dev);
2969 idev = in6_dev_get(dev);
2970 if (!idev) {
2971 err = -ENODEV;
2972 goto out;
2973 }
2974 }
David Ahern93c2fb22018-04-18 15:38:59 -07002975 rt->fib6_flags = RTF_REJECT|RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 goto install_route;
2977 }
2978
Thomas Graf86872cb2006-08-22 00:01:08 -07002979 if (cfg->fc_flags & RTF_GATEWAY) {
David Ahern9fbb7042018-03-13 08:29:36 -07002980 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
2981 if (err)
Florian Westphal48ed7b22015-05-21 00:25:41 +02002982 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
David Ahern93531c62018-04-17 17:33:25 -07002984 rt->fib6_nh.nh_gw = cfg->fc_gateway;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 }
2986
2987 err = -ENODEV;
David S. Miller38308472011-12-03 18:02:47 -05002988 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 goto out;
2990
Lorenzo Bianconi428604f2018-03-29 11:02:24 +02002991 if (idev->cnf.disable_ipv6) {
2992 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
2993 err = -EACCES;
2994 goto out;
2995 }
2996
David Ahern955ec4c2018-01-24 19:45:29 -08002997 if (!(dev->flags & IFF_UP)) {
2998 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2999 err = -ENETDOWN;
3000 goto out;
3001 }
3002
Daniel Walterc3968a82011-04-13 21:10:57 +00003003 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
3004 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
David Ahernd5d531c2017-05-21 10:12:05 -06003005 NL_SET_ERR_MSG(extack, "Invalid source address");
Daniel Walterc3968a82011-04-13 21:10:57 +00003006 err = -EINVAL;
3007 goto out;
3008 }
David Ahern93c2fb22018-04-18 15:38:59 -07003009 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3010 rt->fib6_prefsrc.plen = 128;
Daniel Walterc3968a82011-04-13 21:10:57 +00003011 } else
David Ahern93c2fb22018-04-18 15:38:59 -07003012 rt->fib6_prefsrc.plen = 0;
Daniel Walterc3968a82011-04-13 21:10:57 +00003013
David Ahern93c2fb22018-04-18 15:38:59 -07003014 rt->fib6_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
3016install_route:
David Ahern93c2fb22018-04-18 15:38:59 -07003017 if (!(rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
Ido Schimmel5609b802018-01-07 12:45:06 +02003018 !netif_carrier_ok(dev))
David Ahern5e670d82018-04-17 17:33:14 -07003019 rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
3020 rt->fib6_nh.nh_flags |= (cfg->fc_flags & RTNH_F_ONLINK);
David Ahern93531c62018-04-17 17:33:25 -07003021 rt->fib6_nh.nh_dev = dev;
David Ahern93c2fb22018-04-18 15:38:59 -07003022 rt->fib6_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08003023
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003024 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08003025
David Aherndcd1f572018-04-18 15:39:05 -07003026 if (idev)
3027 in6_dev_put(idev);
3028
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003029 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030out:
3031 if (dev)
3032 dev_put(dev);
3033 if (idev)
3034 in6_dev_put(idev);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003035
David Ahern93531c62018-04-17 17:33:25 -07003036 fib6_info_release(rt);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003037 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003038}
3039
David Ahernacb54e32018-04-17 17:33:22 -07003040int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3041 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003042{
David Ahern8d1c8022018-04-17 17:33:26 -07003043 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003044 int err;
3045
David Ahernacb54e32018-04-17 17:33:22 -07003046 rt = ip6_route_info_create(cfg, gfp_flags, extack);
David Ahernd4ead6b2018-04-17 17:33:16 -07003047 if (IS_ERR(rt))
3048 return PTR_ERR(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003049
David Ahernd4ead6b2018-04-17 17:33:16 -07003050 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
David Ahern93531c62018-04-17 17:33:25 -07003051 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 return err;
3054}
3055
David Ahern8d1c8022018-04-17 17:33:26 -07003056static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
David Ahernafb1d4b52018-04-17 17:33:11 -07003058 struct net *net = info->nl_net;
Thomas Grafc71099a2006-08-04 23:20:06 -07003059 struct fib6_table *table;
David Ahernafb1d4b52018-04-17 17:33:11 -07003060 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
David Ahern421842e2018-04-17 17:33:18 -07003062 if (rt == net->ipv6.fib6_null_entry) {
Gao feng6825a262012-09-19 19:25:34 +00003063 err = -ENOENT;
3064 goto out;
3065 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07003066
David Ahern93c2fb22018-04-18 15:38:59 -07003067 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003068 spin_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07003069 err = fib6_del(rt, info);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003070 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Gao feng6825a262012-09-19 19:25:34 +00003072out:
David Ahern93531c62018-04-17 17:33:25 -07003073 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 return err;
3075}
3076
David Ahern8d1c8022018-04-17 17:33:26 -07003077int ip6_del_rt(struct net *net, struct fib6_info *rt)
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003078{
David Ahernafb1d4b52018-04-17 17:33:11 -07003079 struct nl_info info = { .nl_net = net };
3080
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003081 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003082}
3083
David Ahern8d1c8022018-04-17 17:33:26 -07003084static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
David Ahern0ae81332017-02-02 12:37:08 -08003085{
3086 struct nl_info *info = &cfg->fc_nlinfo;
WANG Conge3330032017-02-27 16:07:43 -08003087 struct net *net = info->nl_net;
David Ahern16a16cd2017-02-02 12:37:11 -08003088 struct sk_buff *skb = NULL;
David Ahern0ae81332017-02-02 12:37:08 -08003089 struct fib6_table *table;
WANG Conge3330032017-02-27 16:07:43 -08003090 int err = -ENOENT;
David Ahern0ae81332017-02-02 12:37:08 -08003091
David Ahern421842e2018-04-17 17:33:18 -07003092 if (rt == net->ipv6.fib6_null_entry)
WANG Conge3330032017-02-27 16:07:43 -08003093 goto out_put;
David Ahern93c2fb22018-04-18 15:38:59 -07003094 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003095 spin_lock_bh(&table->tb6_lock);
David Ahern0ae81332017-02-02 12:37:08 -08003096
David Ahern93c2fb22018-04-18 15:38:59 -07003097 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
David Ahern8d1c8022018-04-17 17:33:26 -07003098 struct fib6_info *sibling, *next_sibling;
David Ahern0ae81332017-02-02 12:37:08 -08003099
David Ahern16a16cd2017-02-02 12:37:11 -08003100 /* prefer to send a single notification with all hops */
3101 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3102 if (skb) {
3103 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3104
David Ahernd4ead6b2018-04-17 17:33:16 -07003105 if (rt6_fill_node(net, skb, rt, NULL,
David Ahern16a16cd2017-02-02 12:37:11 -08003106 NULL, NULL, 0, RTM_DELROUTE,
3107 info->portid, seq, 0) < 0) {
3108 kfree_skb(skb);
3109 skb = NULL;
3110 } else
3111 info->skip_notify = 1;
3112 }
3113
David Ahern0ae81332017-02-02 12:37:08 -08003114 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07003115 &rt->fib6_siblings,
3116 fib6_siblings) {
David Ahern0ae81332017-02-02 12:37:08 -08003117 err = fib6_del(sibling, info);
3118 if (err)
WANG Conge3330032017-02-27 16:07:43 -08003119 goto out_unlock;
David Ahern0ae81332017-02-02 12:37:08 -08003120 }
3121 }
3122
3123 err = fib6_del(rt, info);
WANG Conge3330032017-02-27 16:07:43 -08003124out_unlock:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003125 spin_unlock_bh(&table->tb6_lock);
WANG Conge3330032017-02-27 16:07:43 -08003126out_put:
David Ahern93531c62018-04-17 17:33:25 -07003127 fib6_info_release(rt);
David Ahern16a16cd2017-02-02 12:37:11 -08003128
3129 if (skb) {
WANG Conge3330032017-02-27 16:07:43 -08003130 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
David Ahern16a16cd2017-02-02 12:37:11 -08003131 info->nlh, gfp_any());
3132 }
David Ahern0ae81332017-02-02 12:37:08 -08003133 return err;
3134}
3135
David Ahern23fb93a2018-04-17 17:33:23 -07003136static int ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
3137{
3138 int rc = -ESRCH;
3139
3140 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3141 goto out;
3142
3143 if (cfg->fc_flags & RTF_GATEWAY &&
3144 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3145 goto out;
3146 if (dst_hold_safe(&rt->dst))
3147 rc = rt6_remove_exception_rt(rt);
3148out:
3149 return rc;
3150}
3151
David Ahern333c4302017-05-21 10:12:04 -06003152static int ip6_route_del(struct fib6_config *cfg,
3153 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154{
David Ahern8d1c8022018-04-17 17:33:26 -07003155 struct rt6_info *rt_cache;
Thomas Grafc71099a2006-08-04 23:20:06 -07003156 struct fib6_table *table;
David Ahern8d1c8022018-04-17 17:33:26 -07003157 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 int err = -ESRCH;
3160
Daniel Lezcano55786892008-03-04 13:47:47 -08003161 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David Ahernd5d531c2017-05-21 10:12:05 -06003162 if (!table) {
3163 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Grafc71099a2006-08-04 23:20:06 -07003164 return err;
David Ahernd5d531c2017-05-21 10:12:05 -06003165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
Wei Wang66f5d6c2017-10-06 12:06:10 -07003167 rcu_read_lock();
Thomas Grafc71099a2006-08-04 23:20:06 -07003168
3169 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07003170 &cfg->fc_dst, cfg->fc_dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07003171 &cfg->fc_src, cfg->fc_src_len,
Wei Wang2b760fc2017-10-06 12:06:03 -07003172 !(cfg->fc_flags & RTF_CACHE));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003173
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 if (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003175 for_each_fib6_node_rt_rcu(fn) {
Wei Wang2b760fc2017-10-06 12:06:03 -07003176 if (cfg->fc_flags & RTF_CACHE) {
David Ahern23fb93a2018-04-17 17:33:23 -07003177 int rc;
3178
Wei Wang2b760fc2017-10-06 12:06:03 -07003179 rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst,
3180 &cfg->fc_src);
David Ahern23fb93a2018-04-17 17:33:23 -07003181 if (rt_cache) {
3182 rc = ip6_del_cached_rt(rt_cache, cfg);
3183 if (rc != -ESRCH)
3184 return rc;
3185 }
3186 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07003187 }
Thomas Graf86872cb2006-08-22 00:01:08 -07003188 if (cfg->fc_ifindex &&
David Ahern5e670d82018-04-17 17:33:14 -07003189 (!rt->fib6_nh.nh_dev ||
3190 rt->fib6_nh.nh_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07003192 if (cfg->fc_flags & RTF_GATEWAY &&
David Ahern5e670d82018-04-17 17:33:14 -07003193 !ipv6_addr_equal(&cfg->fc_gateway, &rt->fib6_nh.nh_gw))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003195 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003197 if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol)
Mantas Mc2ed1882016-12-16 10:30:59 +02003198 continue;
David Ahern93531c62018-04-17 17:33:25 -07003199 fib6_info_hold(rt);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003200 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
David Ahern0ae81332017-02-02 12:37:08 -08003202 /* if gateway was specified only delete the one hop */
3203 if (cfg->fc_flags & RTF_GATEWAY)
3204 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3205
3206 return __ip6_del_rt_siblings(rt, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 }
3208 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003209 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
3211 return err;
3212}
3213
David S. Miller6700c272012-07-17 03:29:28 -07003214static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003215{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003216 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07003217 struct rt6_info *rt, *nrt = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07003218 struct ndisc_options ndopts;
3219 struct inet6_dev *in6_dev;
3220 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003221 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07003222 int optlen, on_link;
3223 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07003224
Simon Horman29a3cad2013-05-28 20:34:26 +00003225 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003226 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07003227
3228 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07003229 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003230 return;
3231 }
3232
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003233 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07003234
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003235 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003236 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003237 return;
3238 }
3239
David S. Miller6e157b62012-07-12 00:05:02 -07003240 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003241 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003242 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003243 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07003244 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003245 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003246 return;
3247 }
3248
3249 in6_dev = __in6_dev_get(skb->dev);
3250 if (!in6_dev)
3251 return;
3252 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3253 return;
3254
3255 /* RFC2461 8.1:
3256 * The IP source address of the Redirect MUST be the same as the current
3257 * first-hop router for the specified ICMP Destination Address.
3258 */
3259
Alexander Aringf997c552016-06-15 21:20:23 +02003260 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003261 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3262 return;
3263 }
David S. Miller6e157b62012-07-12 00:05:02 -07003264
3265 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07003266 if (ndopts.nd_opts_tgt_lladdr) {
3267 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3268 skb->dev);
3269 if (!lladdr) {
3270 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3271 return;
3272 }
3273 }
3274
David S. Miller6e157b62012-07-12 00:05:02 -07003275 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01003276 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07003277 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
3278 return;
3279 }
3280
3281 /* Redirect received -> path was valid.
3282 * Look, redirects are sent only in response to data packets,
3283 * so that this nexthop apparently is reachable. --ANK
3284 */
Julian Anastasov0dec8792017-02-06 23:14:16 +02003285 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
David S. Miller6e157b62012-07-12 00:05:02 -07003286
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003287 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07003288 if (!neigh)
3289 return;
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 /*
3292 * We have finally decided to accept it.
3293 */
3294
Alexander Aringf997c552016-06-15 21:20:23 +02003295 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 NEIGH_UPDATE_F_WEAK_OVERRIDE|
3297 NEIGH_UPDATE_F_OVERRIDE|
3298 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02003299 NEIGH_UPDATE_F_ISROUTER)),
3300 NDISC_REDIRECT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
David Ahern23fb93a2018-04-17 17:33:23 -07003302 nrt = ip6_rt_cache_alloc(rt->from, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05003303 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 goto out;
3305
3306 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
3307 if (on_link)
3308 nrt->rt6i_flags &= ~RTF_GATEWAY;
3309
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003310 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
Wei Wang2b760fc2017-10-06 12:06:03 -07003312 /* No need to remove rt from the exception table if rt is
3313 * a cached route because rt6_insert_exception() will
3314 * takes care of it
3315 */
David Ahernd4ead6b2018-04-17 17:33:16 -07003316 if (rt6_insert_exception(nrt, rt->from)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07003317 dst_release_immediate(&nrt->dst);
3318 goto out;
3319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Changli Gaod8d1f302010-06-10 23:31:35 -07003321 netevent.old = &rt->dst;
3322 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003323 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00003324 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07003325 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
3326
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327out:
David S. Millere8599ff2012-07-11 23:43:53 -07003328 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07003329}
3330
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003331#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -07003332static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003333 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07003334 const struct in6_addr *gwaddr,
3335 struct net_device *dev)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003336{
David Ahern830218c2016-10-24 10:52:35 -07003337 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3338 int ifindex = dev->ifindex;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003339 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07003340 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003341 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003342
David Ahern830218c2016-10-24 10:52:35 -07003343 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05003344 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003345 return NULL;
3346
Wei Wang66f5d6c2017-10-06 12:06:10 -07003347 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07003348 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003349 if (!fn)
3350 goto out;
3351
Wei Wang66f5d6c2017-10-06 12:06:10 -07003352 for_each_fib6_node_rt_rcu(fn) {
David Ahern5e670d82018-04-17 17:33:14 -07003353 if (rt->fib6_nh.nh_dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003354 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003355 if ((rt->fib6_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003356 continue;
David Ahern5e670d82018-04-17 17:33:14 -07003357 if (!ipv6_addr_equal(&rt->fib6_nh.nh_gw, gwaddr))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003358 continue;
David Ahern8d1c8022018-04-17 17:33:26 -07003359 fib6_info_hold(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003360 break;
3361 }
3362out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003363 rcu_read_unlock();
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003364 return rt;
3365}
3366
David Ahern8d1c8022018-04-17 17:33:26 -07003367static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003368 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07003369 const struct in6_addr *gwaddr,
3370 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +00003371 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003372{
Thomas Graf86872cb2006-08-22 00:01:08 -07003373 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08003374 .fc_metric = IP6_RT_PRIO_USER,
David Ahern830218c2016-10-24 10:52:35 -07003375 .fc_ifindex = dev->ifindex,
Thomas Graf86872cb2006-08-22 00:01:08 -07003376 .fc_dst_len = prefixlen,
3377 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
3378 RTF_UP | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08003379 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07003380 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003381 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08003382 .fc_nlinfo.nlh = NULL,
3383 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07003384 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003385
David Ahern830218c2016-10-24 10:52:35 -07003386 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003387 cfg.fc_dst = *prefix;
3388 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07003389
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08003390 /* We should treat it as a default route if prefix length is 0. */
3391 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07003392 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003393
David Ahernacb54e32018-04-17 17:33:22 -07003394 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003395
David Ahern830218c2016-10-24 10:52:35 -07003396 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003397}
3398#endif
3399
David Ahern8d1c8022018-04-17 17:33:26 -07003400struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07003401 const struct in6_addr *addr,
3402 struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003403{
David Ahern830218c2016-10-24 10:52:35 -07003404 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
David Ahern8d1c8022018-04-17 17:33:26 -07003405 struct fib6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07003406 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
David Ahernafb1d4b52018-04-17 17:33:11 -07003408 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05003409 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003410 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411
Wei Wang66f5d6c2017-10-06 12:06:10 -07003412 rcu_read_lock();
3413 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Ahern5e670d82018-04-17 17:33:14 -07003414 if (dev == rt->fib6_nh.nh_dev &&
David Ahern93c2fb22018-04-18 15:38:59 -07003415 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Ahern5e670d82018-04-17 17:33:14 -07003416 ipv6_addr_equal(&rt->fib6_nh.nh_gw, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 break;
3418 }
3419 if (rt)
David Ahern8d1c8022018-04-17 17:33:26 -07003420 fib6_info_hold(rt);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003421 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 return rt;
3423}
3424
David Ahern8d1c8022018-04-17 17:33:26 -07003425struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07003426 const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08003427 struct net_device *dev,
3428 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429{
Thomas Graf86872cb2006-08-22 00:01:08 -07003430 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07003431 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08003432 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07003433 .fc_ifindex = dev->ifindex,
3434 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
3435 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08003436 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07003437 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003438 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08003439 .fc_nlinfo.nlh = NULL,
David Ahernafb1d4b52018-04-17 17:33:11 -07003440 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07003441 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003443 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
David Ahernacb54e32018-04-17 17:33:22 -07003445 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
David Ahern830218c2016-10-24 10:52:35 -07003446 struct fib6_table *table;
3447
3448 table = fib6_get_table(dev_net(dev), cfg.fc_table);
3449 if (table)
3450 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
David Ahernafb1d4b52018-04-17 17:33:11 -07003453 return rt6_get_dflt_router(net, gwaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454}
3455
David Ahernafb1d4b52018-04-17 17:33:11 -07003456static void __rt6_purge_dflt_routers(struct net *net,
3457 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
David Ahern8d1c8022018-04-17 17:33:26 -07003459 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
3461restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003462 rcu_read_lock();
3463 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Aherndcd1f572018-04-18 15:39:05 -07003464 struct net_device *dev = fib6_info_nh_dev(rt);
3465 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
3466
David Ahern93c2fb22018-04-18 15:38:59 -07003467 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
David Aherndcd1f572018-04-18 15:39:05 -07003468 (!idev || idev->cnf.accept_ra != 2)) {
David Ahern93531c62018-04-17 17:33:25 -07003469 fib6_info_hold(rt);
3470 rcu_read_unlock();
3471 ip6_del_rt(net, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 goto restart;
3473 }
3474 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003475 rcu_read_unlock();
David Ahern830218c2016-10-24 10:52:35 -07003476
3477 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3478}
3479
3480void rt6_purge_dflt_routers(struct net *net)
3481{
3482 struct fib6_table *table;
3483 struct hlist_head *head;
3484 unsigned int h;
3485
3486 rcu_read_lock();
3487
3488 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3489 head = &net->ipv6.fib_table_hash[h];
3490 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3491 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
David Ahernafb1d4b52018-04-17 17:33:11 -07003492 __rt6_purge_dflt_routers(net, table);
David Ahern830218c2016-10-24 10:52:35 -07003493 }
3494 }
3495
3496 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497}
3498
Daniel Lezcano55786892008-03-04 13:47:47 -08003499static void rtmsg_to_fib6_config(struct net *net,
3500 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07003501 struct fib6_config *cfg)
3502{
3503 memset(cfg, 0, sizeof(*cfg));
3504
David Ahernca254492015-10-12 11:47:10 -07003505 cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
3506 : RT6_TABLE_MAIN;
Thomas Graf86872cb2006-08-22 00:01:08 -07003507 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
3508 cfg->fc_metric = rtmsg->rtmsg_metric;
3509 cfg->fc_expires = rtmsg->rtmsg_info;
3510 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
3511 cfg->fc_src_len = rtmsg->rtmsg_src_len;
3512 cfg->fc_flags = rtmsg->rtmsg_flags;
David Aherne8478e82018-04-17 17:33:13 -07003513 cfg->fc_type = rtmsg->rtmsg_type;
Thomas Graf86872cb2006-08-22 00:01:08 -07003514
Daniel Lezcano55786892008-03-04 13:47:47 -08003515 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08003516
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003517 cfg->fc_dst = rtmsg->rtmsg_dst;
3518 cfg->fc_src = rtmsg->rtmsg_src;
3519 cfg->fc_gateway = rtmsg->rtmsg_gateway;
Thomas Graf86872cb2006-08-22 00:01:08 -07003520}
3521
Daniel Lezcano55786892008-03-04 13:47:47 -08003522int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523{
Thomas Graf86872cb2006-08-22 00:01:08 -07003524 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 struct in6_rtmsg rtmsg;
3526 int err;
3527
Ian Morris67ba4152014-08-24 21:53:10 +01003528 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 case SIOCADDRT: /* Add a route */
3530 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00003531 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 return -EPERM;
3533 err = copy_from_user(&rtmsg, arg,
3534 sizeof(struct in6_rtmsg));
3535 if (err)
3536 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07003537
Daniel Lezcano55786892008-03-04 13:47:47 -08003538 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07003539
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 rtnl_lock();
3541 switch (cmd) {
3542 case SIOCADDRT:
David Ahernacb54e32018-04-17 17:33:22 -07003543 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 break;
3545 case SIOCDELRT:
David Ahern333c4302017-05-21 10:12:04 -06003546 err = ip6_route_del(&cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 break;
3548 default:
3549 err = -EINVAL;
3550 }
3551 rtnl_unlock();
3552
3553 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
3556 return -EINVAL;
3557}
3558
3559/*
3560 * Drop the packet on the floor
3561 */
3562
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07003563static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003565 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00003566 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003567 switch (ipstats_mib_noroutes) {
3568 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07003569 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00003570 if (type == IPV6_ADDR_ANY) {
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -04003571 IP6_INC_STATS(dev_net(dst->dev),
3572 __in6_dev_get_safely(skb->dev),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07003573 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003574 break;
3575 }
3576 /* FALLTHROUGH */
3577 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07003578 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
3579 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003580 break;
3581 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00003582 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 kfree_skb(skb);
3584 return 0;
3585}
3586
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003587static int ip6_pkt_discard(struct sk_buff *skb)
3588{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003589 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003590}
3591
Eric W. Biedermanede20592015-10-07 16:48:47 -05003592static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593{
Eric Dumazetadf30902009-06-02 05:19:30 +00003594 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003595 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596}
3597
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003598static int ip6_pkt_prohibit(struct sk_buff *skb)
3599{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003600 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003601}
3602
Eric W. Biedermanede20592015-10-07 16:48:47 -05003603static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003604{
Eric Dumazetadf30902009-06-02 05:19:30 +00003605 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003606 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003607}
3608
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609/*
3610 * Allocate a dst for local (unicast / anycast) address.
3611 */
3612
David Ahern360a9882018-04-18 15:39:00 -07003613struct fib6_info *addrconf_f6i_alloc(struct net *net,
3614 struct inet6_dev *idev,
3615 const struct in6_addr *addr,
3616 bool anycast, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617{
David Ahernca254492015-10-12 11:47:10 -07003618 u32 tb_id;
David Ahern4832c302017-08-17 12:17:20 -07003619 struct net_device *dev = idev->dev;
David Ahern360a9882018-04-18 15:39:00 -07003620 struct fib6_info *f6i;
David Ahern5f02ce242016-09-10 12:09:54 -07003621
David Ahern360a9882018-04-18 15:39:00 -07003622 f6i = fib6_info_alloc(gfp_flags);
3623 if (!f6i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 return ERR_PTR(-ENOMEM);
3625
David Ahern360a9882018-04-18 15:39:00 -07003626 f6i->dst_nocount = true;
David Ahern360a9882018-04-18 15:39:00 -07003627 f6i->dst_host = true;
3628 f6i->fib6_protocol = RTPROT_KERNEL;
3629 f6i->fib6_flags = RTF_UP | RTF_NONEXTHOP;
David Aherne8478e82018-04-17 17:33:13 -07003630 if (anycast) {
David Ahern360a9882018-04-18 15:39:00 -07003631 f6i->fib6_type = RTN_ANYCAST;
3632 f6i->fib6_flags |= RTF_ANYCAST;
David Aherne8478e82018-04-17 17:33:13 -07003633 } else {
David Ahern360a9882018-04-18 15:39:00 -07003634 f6i->fib6_type = RTN_LOCAL;
3635 f6i->fib6_flags |= RTF_LOCAL;
David Aherne8478e82018-04-17 17:33:13 -07003636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
David Ahern360a9882018-04-18 15:39:00 -07003638 f6i->fib6_nh.nh_gw = *addr;
David Ahern93531c62018-04-17 17:33:25 -07003639 dev_hold(dev);
David Ahern360a9882018-04-18 15:39:00 -07003640 f6i->fib6_nh.nh_dev = dev;
3641 f6i->fib6_dst.addr = *addr;
3642 f6i->fib6_dst.plen = 128;
David Ahernca254492015-10-12 11:47:10 -07003643 tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL;
David Ahern360a9882018-04-18 15:39:00 -07003644 f6i->fib6_table = fib6_get_table(net, tb_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
David Ahern360a9882018-04-18 15:39:00 -07003646 return f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647}
3648
Daniel Walterc3968a82011-04-13 21:10:57 +00003649/* remove deleted ip from prefsrc entries */
3650struct arg_dev_net_ip {
3651 struct net_device *dev;
3652 struct net *net;
3653 struct in6_addr *addr;
3654};
3655
David Ahern8d1c8022018-04-17 17:33:26 -07003656static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
Daniel Walterc3968a82011-04-13 21:10:57 +00003657{
3658 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3659 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3660 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3661
David Ahern5e670d82018-04-17 17:33:14 -07003662 if (((void *)rt->fib6_nh.nh_dev == dev || !dev) &&
David Ahern421842e2018-04-17 17:33:18 -07003663 rt != net->ipv6.fib6_null_entry &&
David Ahern93c2fb22018-04-18 15:38:59 -07003664 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
Wei Wang60006a42017-10-06 12:05:58 -07003665 spin_lock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00003666 /* remove prefsrc entry */
David Ahern93c2fb22018-04-18 15:38:59 -07003667 rt->fib6_prefsrc.plen = 0;
Wei Wang60006a42017-10-06 12:05:58 -07003668 /* need to update cache as well */
3669 rt6_exceptions_remove_prefsrc(rt);
3670 spin_unlock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00003671 }
3672 return 0;
3673}
3674
3675void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
3676{
3677 struct net *net = dev_net(ifp->idev->dev);
3678 struct arg_dev_net_ip adni = {
3679 .dev = ifp->idev->dev,
3680 .net = net,
3681 .addr = &ifp->addr,
3682 };
Li RongQing0c3584d2013-12-27 16:32:38 +08003683 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00003684}
3685
Duan Jiongbe7a0102014-05-15 15:56:14 +08003686#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
Duan Jiongbe7a0102014-05-15 15:56:14 +08003687
3688/* Remove routers and update dst entries when gateway turn into host. */
David Ahern8d1c8022018-04-17 17:33:26 -07003689static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
Duan Jiongbe7a0102014-05-15 15:56:14 +08003690{
3691 struct in6_addr *gateway = (struct in6_addr *)arg;
3692
David Ahern93c2fb22018-04-18 15:38:59 -07003693 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Ahern5e670d82018-04-17 17:33:14 -07003694 ipv6_addr_equal(gateway, &rt->fib6_nh.nh_gw)) {
Duan Jiongbe7a0102014-05-15 15:56:14 +08003695 return -1;
3696 }
Wei Wangb16cb452017-10-06 12:06:00 -07003697
3698 /* Further clean up cached routes in exception table.
3699 * This is needed because cached route may have a different
3700 * gateway than its 'parent' in the case of an ip redirect.
3701 */
3702 rt6_exceptions_clean_tohost(rt, gateway);
3703
Duan Jiongbe7a0102014-05-15 15:56:14 +08003704 return 0;
3705}
3706
3707void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
3708{
3709 fib6_clean_all(net, fib6_clean_tohost, gateway);
3710}
3711
Ido Schimmel2127d952018-01-07 12:45:03 +02003712struct arg_netdev_event {
3713 const struct net_device *dev;
Ido Schimmel4c981e22018-01-07 12:45:04 +02003714 union {
3715 unsigned int nh_flags;
3716 unsigned long event;
3717 };
Ido Schimmel2127d952018-01-07 12:45:03 +02003718};
3719
David Ahern8d1c8022018-04-17 17:33:26 -07003720static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003721{
David Ahern8d1c8022018-04-17 17:33:26 -07003722 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003723 struct fib6_node *fn;
3724
David Ahern93c2fb22018-04-18 15:38:59 -07003725 fn = rcu_dereference_protected(rt->fib6_node,
3726 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003727 iter = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -07003728 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003729 while (iter) {
David Ahern93c2fb22018-04-18 15:38:59 -07003730 if (iter->fib6_metric == rt->fib6_metric &&
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003731 rt6_qualify_for_ecmp(iter))
3732 return iter;
3733 iter = rcu_dereference_protected(iter->rt6_next,
David Ahern93c2fb22018-04-18 15:38:59 -07003734 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003735 }
3736
3737 return NULL;
3738}
3739
David Ahern8d1c8022018-04-17 17:33:26 -07003740static bool rt6_is_dead(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003741{
David Ahern5e670d82018-04-17 17:33:14 -07003742 if (rt->fib6_nh.nh_flags & RTNH_F_DEAD ||
3743 (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN &&
David Aherndcd1f572018-04-18 15:39:05 -07003744 fib6_ignore_linkdown(rt)))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003745 return true;
3746
3747 return false;
3748}
3749
David Ahern8d1c8022018-04-17 17:33:26 -07003750static int rt6_multipath_total_weight(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003751{
David Ahern8d1c8022018-04-17 17:33:26 -07003752 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003753 int total = 0;
3754
3755 if (!rt6_is_dead(rt))
David Ahern5e670d82018-04-17 17:33:14 -07003756 total += rt->fib6_nh.nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003757
David Ahern93c2fb22018-04-18 15:38:59 -07003758 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003759 if (!rt6_is_dead(iter))
David Ahern5e670d82018-04-17 17:33:14 -07003760 total += iter->fib6_nh.nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003761 }
3762
3763 return total;
3764}
3765
David Ahern8d1c8022018-04-17 17:33:26 -07003766static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003767{
3768 int upper_bound = -1;
3769
3770 if (!rt6_is_dead(rt)) {
David Ahern5e670d82018-04-17 17:33:14 -07003771 *weight += rt->fib6_nh.nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003772 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
3773 total) - 1;
3774 }
David Ahern5e670d82018-04-17 17:33:14 -07003775 atomic_set(&rt->fib6_nh.nh_upper_bound, upper_bound);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003776}
3777
David Ahern8d1c8022018-04-17 17:33:26 -07003778static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003779{
David Ahern8d1c8022018-04-17 17:33:26 -07003780 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003781 int weight = 0;
3782
3783 rt6_upper_bound_set(rt, &weight, total);
3784
David Ahern93c2fb22018-04-18 15:38:59 -07003785 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003786 rt6_upper_bound_set(iter, &weight, total);
3787}
3788
David Ahern8d1c8022018-04-17 17:33:26 -07003789void rt6_multipath_rebalance(struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003790{
David Ahern8d1c8022018-04-17 17:33:26 -07003791 struct fib6_info *first;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003792 int total;
3793
3794 /* In case the entire multipath route was marked for flushing,
3795 * then there is no need to rebalance upon the removal of every
3796 * sibling route.
3797 */
David Ahern93c2fb22018-04-18 15:38:59 -07003798 if (!rt->fib6_nsiblings || rt->should_flush)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003799 return;
3800
3801 /* During lookup routes are evaluated in order, so we need to
3802 * make sure upper bounds are assigned from the first sibling
3803 * onwards.
3804 */
3805 first = rt6_multipath_first_sibling(rt);
3806 if (WARN_ON_ONCE(!first))
3807 return;
3808
3809 total = rt6_multipath_total_weight(first);
3810 rt6_multipath_upper_bound_set(first, total);
3811}
3812
David Ahern8d1c8022018-04-17 17:33:26 -07003813static int fib6_ifup(struct fib6_info *rt, void *p_arg)
Ido Schimmel2127d952018-01-07 12:45:03 +02003814{
3815 const struct arg_netdev_event *arg = p_arg;
David Ahern7aef6852018-04-17 17:33:10 -07003816 struct net *net = dev_net(arg->dev);
Ido Schimmel2127d952018-01-07 12:45:03 +02003817
David Ahern421842e2018-04-17 17:33:18 -07003818 if (rt != net->ipv6.fib6_null_entry && rt->fib6_nh.nh_dev == arg->dev) {
David Ahern5e670d82018-04-17 17:33:14 -07003819 rt->fib6_nh.nh_flags &= ~arg->nh_flags;
David Ahern7aef6852018-04-17 17:33:10 -07003820 fib6_update_sernum_upto_root(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003821 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02003822 }
Ido Schimmel2127d952018-01-07 12:45:03 +02003823
3824 return 0;
3825}
3826
3827void rt6_sync_up(struct net_device *dev, unsigned int nh_flags)
3828{
3829 struct arg_netdev_event arg = {
3830 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02003831 {
3832 .nh_flags = nh_flags,
3833 },
Ido Schimmel2127d952018-01-07 12:45:03 +02003834 };
3835
3836 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
3837 arg.nh_flags |= RTNH_F_LINKDOWN;
3838
3839 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
3840}
3841
David Ahern8d1c8022018-04-17 17:33:26 -07003842static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02003843 const struct net_device *dev)
3844{
David Ahern8d1c8022018-04-17 17:33:26 -07003845 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003846
David Ahern5e670d82018-04-17 17:33:14 -07003847 if (rt->fib6_nh.nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003848 return true;
David Ahern93c2fb22018-04-18 15:38:59 -07003849 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern5e670d82018-04-17 17:33:14 -07003850 if (iter->fib6_nh.nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003851 return true;
3852
3853 return false;
3854}
3855
David Ahern8d1c8022018-04-17 17:33:26 -07003856static void rt6_multipath_flush(struct fib6_info *rt)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003857{
David Ahern8d1c8022018-04-17 17:33:26 -07003858 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003859
3860 rt->should_flush = 1;
David Ahern93c2fb22018-04-18 15:38:59 -07003861 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003862 iter->should_flush = 1;
3863}
3864
David Ahern8d1c8022018-04-17 17:33:26 -07003865static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02003866 const struct net_device *down_dev)
3867{
David Ahern8d1c8022018-04-17 17:33:26 -07003868 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003869 unsigned int dead = 0;
3870
David Ahern5e670d82018-04-17 17:33:14 -07003871 if (rt->fib6_nh.nh_dev == down_dev ||
3872 rt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003873 dead++;
David Ahern93c2fb22018-04-18 15:38:59 -07003874 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern5e670d82018-04-17 17:33:14 -07003875 if (iter->fib6_nh.nh_dev == down_dev ||
3876 iter->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003877 dead++;
3878
3879 return dead;
3880}
3881
David Ahern8d1c8022018-04-17 17:33:26 -07003882static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02003883 const struct net_device *dev,
3884 unsigned int nh_flags)
3885{
David Ahern8d1c8022018-04-17 17:33:26 -07003886 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003887
David Ahern5e670d82018-04-17 17:33:14 -07003888 if (rt->fib6_nh.nh_dev == dev)
3889 rt->fib6_nh.nh_flags |= nh_flags;
David Ahern93c2fb22018-04-18 15:38:59 -07003890 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern5e670d82018-04-17 17:33:14 -07003891 if (iter->fib6_nh.nh_dev == dev)
3892 iter->fib6_nh.nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003893}
3894
David Aherna1a22c12017-01-18 07:40:36 -08003895/* called with write lock held for table with rt */
David Ahern8d1c8022018-04-17 17:33:26 -07003896static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897{
Ido Schimmel4c981e22018-01-07 12:45:04 +02003898 const struct arg_netdev_event *arg = p_arg;
3899 const struct net_device *dev = arg->dev;
David Ahern7aef6852018-04-17 17:33:10 -07003900 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003901
David Ahern421842e2018-04-17 17:33:18 -07003902 if (rt == net->ipv6.fib6_null_entry)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003903 return 0;
3904
3905 switch (arg->event) {
3906 case NETDEV_UNREGISTER:
David Ahern5e670d82018-04-17 17:33:14 -07003907 return rt->fib6_nh.nh_dev == dev ? -1 : 0;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003908 case NETDEV_DOWN:
Ido Schimmel1de178e2018-01-07 12:45:15 +02003909 if (rt->should_flush)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003910 return -1;
David Ahern93c2fb22018-04-18 15:38:59 -07003911 if (!rt->fib6_nsiblings)
David Ahern5e670d82018-04-17 17:33:14 -07003912 return rt->fib6_nh.nh_dev == dev ? -1 : 0;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003913 if (rt6_multipath_uses_dev(rt, dev)) {
3914 unsigned int count;
3915
3916 count = rt6_multipath_dead_count(rt, dev);
David Ahern93c2fb22018-04-18 15:38:59 -07003917 if (rt->fib6_nsiblings + 1 == count) {
Ido Schimmel1de178e2018-01-07 12:45:15 +02003918 rt6_multipath_flush(rt);
3919 return -1;
3920 }
3921 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
3922 RTNH_F_LINKDOWN);
David Ahern7aef6852018-04-17 17:33:10 -07003923 fib6_update_sernum(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003924 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02003925 }
3926 return -2;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003927 case NETDEV_CHANGE:
David Ahern5e670d82018-04-17 17:33:14 -07003928 if (rt->fib6_nh.nh_dev != dev ||
David Ahern93c2fb22018-04-18 15:38:59 -07003929 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003930 break;
David Ahern5e670d82018-04-17 17:33:14 -07003931 rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003932 rt6_multipath_rebalance(rt);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003933 break;
Ido Schimmel2b241362018-01-07 12:45:02 +02003934 }
David S. Millerc159d302011-12-26 15:24:36 -05003935
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 return 0;
3937}
3938
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003939void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940{
Ido Schimmel4c981e22018-01-07 12:45:04 +02003941 struct arg_netdev_event arg = {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003942 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02003943 {
3944 .event = event,
3945 },
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003946 };
3947
Ido Schimmel4c981e22018-01-07 12:45:04 +02003948 fib6_clean_all(dev_net(dev), fib6_ifdown, &arg);
3949}
3950
3951void rt6_disable_ip(struct net_device *dev, unsigned long event)
3952{
3953 rt6_sync_down_dev(dev, event);
3954 rt6_uncached_list_flush_dev(dev_net(dev), dev);
3955 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956}
3957
Eric Dumazet95c96172012-04-15 05:58:06 +00003958struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00003960 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961};
3962
David Ahern8d1c8022018-04-17 17:33:26 -07003963static int rt6_mtu_change_route(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964{
3965 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
3966 struct inet6_dev *idev;
3967
3968 /* In IPv6 pmtu discovery is not optional,
3969 so that RTAX_MTU lock cannot disable it.
3970 We still use this lock to block changes
3971 caused by addrconf/ndisc.
3972 */
3973
3974 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05003975 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 return 0;
3977
3978 /* For administrative MTU increase, there is no way to discover
3979 IPv6 PMTU increase, so PMTU increase should be updated here.
3980 Since RFC 1981 doesn't include administrative MTU increase
3981 update PMTU increase is a MUST. (i.e. jumbo frame)
3982 */
David Ahern5e670d82018-04-17 17:33:14 -07003983 if (rt->fib6_nh.nh_dev == arg->dev &&
David Ahernd4ead6b2018-04-17 17:33:16 -07003984 !fib6_metric_locked(rt, RTAX_MTU)) {
3985 u32 mtu = rt->fib6_pmtu;
3986
3987 if (mtu >= arg->mtu ||
3988 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
3989 fib6_metric_set(rt, RTAX_MTU, arg->mtu);
3990
Wei Wangf5bbe7e2017-10-06 12:05:59 -07003991 spin_lock_bh(&rt6_exception_lock);
Stefano Brivioe9fa1492018-03-06 11:10:19 +01003992 rt6_exceptions_update_pmtu(idev, rt, arg->mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07003993 spin_unlock_bh(&rt6_exception_lock);
Simon Arlott566cfd82007-07-26 00:09:55 -07003994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 return 0;
3996}
3997
Eric Dumazet95c96172012-04-15 05:58:06 +00003998void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999{
Thomas Grafc71099a2006-08-04 23:20:06 -07004000 struct rt6_mtu_change_arg arg = {
4001 .dev = dev,
4002 .mtu = mtu,
4003 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
Li RongQing0c3584d2013-12-27 16:32:38 +08004005 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006}
4007
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004008static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07004009 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07004010 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07004011 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004012 [RTA_PRIORITY] = { .type = NLA_U32 },
4013 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004014 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004015 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004016 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4017 [RTA_ENCAP] = { .type = NLA_NESTED },
Xin Long32bc2012015-12-16 17:50:11 +08004018 [RTA_EXPIRES] = { .type = NLA_U32 },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004019 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +08004020 [RTA_MARK] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004021};
4022
4023static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
David Ahern333c4302017-05-21 10:12:04 -06004024 struct fib6_config *cfg,
4025 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026{
Thomas Graf86872cb2006-08-22 00:01:08 -07004027 struct rtmsg *rtm;
4028 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004029 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07004030 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
Johannes Bergfceb6432017-04-12 14:34:07 +02004032 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
4033 NULL);
Thomas Graf86872cb2006-08-22 00:01:08 -07004034 if (err < 0)
4035 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Thomas Graf86872cb2006-08-22 00:01:08 -07004037 err = -EINVAL;
4038 rtm = nlmsg_data(nlh);
4039 memset(cfg, 0, sizeof(*cfg));
4040
4041 cfg->fc_table = rtm->rtm_table;
4042 cfg->fc_dst_len = rtm->rtm_dst_len;
4043 cfg->fc_src_len = rtm->rtm_src_len;
4044 cfg->fc_flags = RTF_UP;
4045 cfg->fc_protocol = rtm->rtm_protocol;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004046 cfg->fc_type = rtm->rtm_type;
Thomas Graf86872cb2006-08-22 00:01:08 -07004047
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004048 if (rtm->rtm_type == RTN_UNREACHABLE ||
4049 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00004050 rtm->rtm_type == RTN_PROHIBIT ||
4051 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07004052 cfg->fc_flags |= RTF_REJECT;
4053
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00004054 if (rtm->rtm_type == RTN_LOCAL)
4055 cfg->fc_flags |= RTF_LOCAL;
4056
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07004057 if (rtm->rtm_flags & RTM_F_CLONED)
4058 cfg->fc_flags |= RTF_CACHE;
4059
David Ahernfc1e64e2018-01-25 16:55:09 -08004060 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4061
Eric W. Biederman15e47302012-09-07 20:12:54 +00004062 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
Thomas Graf86872cb2006-08-22 00:01:08 -07004063 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004064 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07004065
4066 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004067 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07004068 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004070
4071 if (tb[RTA_DST]) {
4072 int plen = (rtm->rtm_dst_len + 7) >> 3;
4073
4074 if (nla_len(tb[RTA_DST]) < plen)
4075 goto errout;
4076
4077 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004079
4080 if (tb[RTA_SRC]) {
4081 int plen = (rtm->rtm_src_len + 7) >> 3;
4082
4083 if (nla_len(tb[RTA_SRC]) < plen)
4084 goto errout;
4085
4086 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004088
Daniel Walterc3968a82011-04-13 21:10:57 +00004089 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02004090 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00004091
Thomas Graf86872cb2006-08-22 00:01:08 -07004092 if (tb[RTA_OIF])
4093 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4094
4095 if (tb[RTA_PRIORITY])
4096 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4097
4098 if (tb[RTA_METRICS]) {
4099 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4100 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004102
4103 if (tb[RTA_TABLE])
4104 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4105
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004106 if (tb[RTA_MULTIPATH]) {
4107 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4108 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
David Ahern9ed59592017-01-17 14:57:36 -08004109
4110 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
David Ahernc255bd62017-05-27 16:19:27 -06004111 cfg->fc_mp_len, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004112 if (err < 0)
4113 goto errout;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004114 }
4115
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004116 if (tb[RTA_PREF]) {
4117 pref = nla_get_u8(tb[RTA_PREF]);
4118 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4119 pref != ICMPV6_ROUTER_PREF_HIGH)
4120 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4121 cfg->fc_flags |= RTF_PREF(pref);
4122 }
4123
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004124 if (tb[RTA_ENCAP])
4125 cfg->fc_encap = tb[RTA_ENCAP];
4126
David Ahern9ed59592017-01-17 14:57:36 -08004127 if (tb[RTA_ENCAP_TYPE]) {
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004128 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
4129
David Ahernc255bd62017-05-27 16:19:27 -06004130 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004131 if (err < 0)
4132 goto errout;
4133 }
4134
Xin Long32bc2012015-12-16 17:50:11 +08004135 if (tb[RTA_EXPIRES]) {
4136 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
4137
4138 if (addrconf_finite_timeout(timeout)) {
4139 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
4140 cfg->fc_flags |= RTF_EXPIRES;
4141 }
4142 }
4143
Thomas Graf86872cb2006-08-22 00:01:08 -07004144 err = 0;
4145errout:
4146 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147}
4148
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004149struct rt6_nh {
David Ahern8d1c8022018-04-17 17:33:26 -07004150 struct fib6_info *fib6_info;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004151 struct fib6_config r_cfg;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004152 struct list_head next;
4153};
4154
4155static void ip6_print_replace_route_err(struct list_head *rt6_nh_list)
4156{
4157 struct rt6_nh *nh;
4158
4159 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern7d4d5062017-02-02 12:37:12 -08004160 pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n",
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004161 &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway,
4162 nh->r_cfg.fc_ifindex);
4163 }
4164}
4165
David Ahernd4ead6b2018-04-17 17:33:16 -07004166static int ip6_route_info_append(struct net *net,
4167 struct list_head *rt6_nh_list,
David Ahern8d1c8022018-04-17 17:33:26 -07004168 struct fib6_info *rt,
4169 struct fib6_config *r_cfg)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004170{
4171 struct rt6_nh *nh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004172 int err = -EEXIST;
4173
4174 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004175 /* check if fib6_info already exists */
4176 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004177 return err;
4178 }
4179
4180 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
4181 if (!nh)
4182 return -ENOMEM;
David Ahern8d1c8022018-04-17 17:33:26 -07004183 nh->fib6_info = rt;
David Ahernd4ead6b2018-04-17 17:33:16 -07004184 err = ip6_convert_metrics(net, rt, r_cfg);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004185 if (err) {
4186 kfree(nh);
4187 return err;
4188 }
4189 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
4190 list_add_tail(&nh->next, rt6_nh_list);
4191
4192 return 0;
4193}
4194
David Ahern8d1c8022018-04-17 17:33:26 -07004195static void ip6_route_mpath_notify(struct fib6_info *rt,
4196 struct fib6_info *rt_last,
David Ahern3b1137f2017-02-02 12:37:10 -08004197 struct nl_info *info,
4198 __u16 nlflags)
4199{
4200 /* if this is an APPEND route, then rt points to the first route
4201 * inserted and rt_last points to last route inserted. Userspace
4202 * wants a consistent dump of the route which starts at the first
4203 * nexthop. Since sibling routes are always added at the end of
4204 * the list, find the first sibling of the last route appended
4205 */
David Ahern93c2fb22018-04-18 15:38:59 -07004206 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
4207 rt = list_first_entry(&rt_last->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -07004208 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -07004209 fib6_siblings);
David Ahern3b1137f2017-02-02 12:37:10 -08004210 }
4211
4212 if (rt)
4213 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
4214}
4215
David Ahern333c4302017-05-21 10:12:04 -06004216static int ip6_route_multipath_add(struct fib6_config *cfg,
4217 struct netlink_ext_ack *extack)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004218{
David Ahern8d1c8022018-04-17 17:33:26 -07004219 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
David Ahern3b1137f2017-02-02 12:37:10 -08004220 struct nl_info *info = &cfg->fc_nlinfo;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004221 struct fib6_config r_cfg;
4222 struct rtnexthop *rtnh;
David Ahern8d1c8022018-04-17 17:33:26 -07004223 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004224 struct rt6_nh *err_nh;
4225 struct rt6_nh *nh, *nh_safe;
David Ahern3b1137f2017-02-02 12:37:10 -08004226 __u16 nlflags;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004227 int remaining;
4228 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004229 int err = 1;
4230 int nhn = 0;
4231 int replace = (cfg->fc_nlinfo.nlh &&
4232 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
4233 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004234
David Ahern3b1137f2017-02-02 12:37:10 -08004235 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
4236 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
4237 nlflags |= NLM_F_APPEND;
4238
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02004239 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004240 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004241
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004242 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
David Ahern8d1c8022018-04-17 17:33:26 -07004243 * fib6_info structs per nexthop
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004244 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004245 while (rtnh_ok(rtnh, remaining)) {
4246 memcpy(&r_cfg, cfg, sizeof(*cfg));
4247 if (rtnh->rtnh_ifindex)
4248 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
4249
4250 attrlen = rtnh_attrlen(rtnh);
4251 if (attrlen > 0) {
4252 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4253
4254 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
4255 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004256 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004257 r_cfg.fc_flags |= RTF_GATEWAY;
4258 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004259 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
4260 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
4261 if (nla)
4262 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004263 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004264
David Ahern68e2ffd2018-03-20 10:06:59 -07004265 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
David Ahernacb54e32018-04-17 17:33:22 -07004266 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07004267 if (IS_ERR(rt)) {
4268 err = PTR_ERR(rt);
4269 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004270 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07004271 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004272
David Ahern5e670d82018-04-17 17:33:14 -07004273 rt->fib6_nh.nh_weight = rtnh->rtnh_hops + 1;
Ido Schimmel398958a2018-01-09 16:40:28 +02004274
David Ahernd4ead6b2018-04-17 17:33:16 -07004275 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4276 rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004277 if (err) {
David Ahern93531c62018-04-17 17:33:25 -07004278 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004279 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004280 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004281
4282 rtnh = rtnh_next(rtnh, &remaining);
4283 }
4284
David Ahern3b1137f2017-02-02 12:37:10 -08004285 /* for add and replace send one notification with all nexthops.
4286 * Skip the notification in fib6_add_rt2node and send one with
4287 * the full route when done
4288 */
4289 info->skip_notify = 1;
4290
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004291 err_nh = NULL;
4292 list_for_each_entry(nh, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004293 rt_last = nh->fib6_info;
4294 err = __ip6_ins_rt(nh->fib6_info, info, extack);
4295 fib6_info_release(nh->fib6_info);
David Ahern93531c62018-04-17 17:33:25 -07004296
David Ahern3b1137f2017-02-02 12:37:10 -08004297 /* save reference to first route for notification */
4298 if (!rt_notif && !err)
David Ahern8d1c8022018-04-17 17:33:26 -07004299 rt_notif = nh->fib6_info;
David Ahern3b1137f2017-02-02 12:37:10 -08004300
David Ahern8d1c8022018-04-17 17:33:26 -07004301 /* nh->fib6_info is used or freed at this point, reset to NULL*/
4302 nh->fib6_info = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004303 if (err) {
4304 if (replace && nhn)
4305 ip6_print_replace_route_err(&rt6_nh_list);
4306 err_nh = nh;
4307 goto add_errout;
4308 }
4309
Nicolas Dichtel1a724182012-11-01 22:58:22 +00004310 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02004311 * these flags after the first nexthop: if there is a collision,
4312 * we have already failed to add the first nexthop:
4313 * fib6_add_rt2node() has rejected it; when replacing, old
4314 * nexthops have been replaced by first new, the rest should
4315 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00004316 */
Michal Kubeček27596472015-05-18 20:54:00 +02004317 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
4318 NLM_F_REPLACE);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004319 nhn++;
4320 }
4321
David Ahern3b1137f2017-02-02 12:37:10 -08004322 /* success ... tell user about new route */
4323 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004324 goto cleanup;
4325
4326add_errout:
David Ahern3b1137f2017-02-02 12:37:10 -08004327 /* send notification for routes that were added so that
4328 * the delete notifications sent by ip6_route_del are
4329 * coherent
4330 */
4331 if (rt_notif)
4332 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
4333
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004334 /* Delete routes that were already added */
4335 list_for_each_entry(nh, &rt6_nh_list, next) {
4336 if (err_nh == nh)
4337 break;
David Ahern333c4302017-05-21 10:12:04 -06004338 ip6_route_del(&nh->r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004339 }
4340
4341cleanup:
4342 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004343 if (nh->fib6_info)
4344 fib6_info_release(nh->fib6_info);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004345 list_del(&nh->next);
4346 kfree(nh);
4347 }
4348
4349 return err;
4350}
4351
David Ahern333c4302017-05-21 10:12:04 -06004352static int ip6_route_multipath_del(struct fib6_config *cfg,
4353 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004354{
4355 struct fib6_config r_cfg;
4356 struct rtnexthop *rtnh;
4357 int remaining;
4358 int attrlen;
4359 int err = 1, last_err = 0;
4360
4361 remaining = cfg->fc_mp_len;
4362 rtnh = (struct rtnexthop *)cfg->fc_mp;
4363
4364 /* Parse a Multipath Entry */
4365 while (rtnh_ok(rtnh, remaining)) {
4366 memcpy(&r_cfg, cfg, sizeof(*cfg));
4367 if (rtnh->rtnh_ifindex)
4368 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
4369
4370 attrlen = rtnh_attrlen(rtnh);
4371 if (attrlen > 0) {
4372 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4373
4374 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
4375 if (nla) {
4376 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
4377 r_cfg.fc_flags |= RTF_GATEWAY;
4378 }
4379 }
David Ahern333c4302017-05-21 10:12:04 -06004380 err = ip6_route_del(&r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004381 if (err)
4382 last_err = err;
4383
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004384 rtnh = rtnh_next(rtnh, &remaining);
4385 }
4386
4387 return last_err;
4388}
4389
David Ahernc21ef3e2017-04-16 09:48:24 -07004390static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4391 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392{
Thomas Graf86872cb2006-08-22 00:01:08 -07004393 struct fib6_config cfg;
4394 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
David Ahern333c4302017-05-21 10:12:04 -06004396 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004397 if (err < 0)
4398 return err;
4399
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004400 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06004401 return ip6_route_multipath_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08004402 else {
4403 cfg.fc_delete_all_nh = 1;
David Ahern333c4302017-05-21 10:12:04 -06004404 return ip6_route_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08004405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406}
4407
David Ahernc21ef3e2017-04-16 09:48:24 -07004408static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4409 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
Thomas Graf86872cb2006-08-22 00:01:08 -07004411 struct fib6_config cfg;
4412 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
David Ahern333c4302017-05-21 10:12:04 -06004414 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004415 if (err < 0)
4416 return err;
4417
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004418 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06004419 return ip6_route_multipath_add(&cfg, extack);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004420 else
David Ahernacb54e32018-04-17 17:33:22 -07004421 return ip6_route_add(&cfg, GFP_KERNEL, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422}
4423
David Ahern8d1c8022018-04-17 17:33:26 -07004424static size_t rt6_nlmsg_size(struct fib6_info *rt)
Thomas Graf339bf982006-11-10 14:10:15 -08004425{
David Ahernbeb1afac52017-02-02 12:37:09 -08004426 int nexthop_len = 0;
4427
David Ahern93c2fb22018-04-18 15:38:59 -07004428 if (rt->fib6_nsiblings) {
David Ahernbeb1afac52017-02-02 12:37:09 -08004429 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
4430 + NLA_ALIGN(sizeof(struct rtnexthop))
4431 + nla_total_size(16) /* RTA_GATEWAY */
David Ahern5e670d82018-04-17 17:33:14 -07004432 + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate);
David Ahernbeb1afac52017-02-02 12:37:09 -08004433
David Ahern93c2fb22018-04-18 15:38:59 -07004434 nexthop_len *= rt->fib6_nsiblings;
David Ahernbeb1afac52017-02-02 12:37:09 -08004435 }
4436
Thomas Graf339bf982006-11-10 14:10:15 -08004437 return NLMSG_ALIGN(sizeof(struct rtmsg))
4438 + nla_total_size(16) /* RTA_SRC */
4439 + nla_total_size(16) /* RTA_DST */
4440 + nla_total_size(16) /* RTA_GATEWAY */
4441 + nla_total_size(16) /* RTA_PREFSRC */
4442 + nla_total_size(4) /* RTA_TABLE */
4443 + nla_total_size(4) /* RTA_IIF */
4444 + nla_total_size(4) /* RTA_OIF */
4445 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08004446 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01004447 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004448 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004449 + nla_total_size(1) /* RTA_PREF */
David Ahern5e670d82018-04-17 17:33:14 -07004450 + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate)
David Ahernbeb1afac52017-02-02 12:37:09 -08004451 + nexthop_len;
4452}
4453
David Ahern8d1c8022018-04-17 17:33:26 -07004454static int rt6_nexthop_info(struct sk_buff *skb, struct fib6_info *rt,
David Ahern5be083c2017-03-06 15:57:31 -08004455 unsigned int *flags, bool skip_oif)
David Ahernbeb1afac52017-02-02 12:37:09 -08004456{
David Ahern5e670d82018-04-17 17:33:14 -07004457 if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmelf9d882e2018-01-07 12:45:10 +02004458 *flags |= RTNH_F_DEAD;
4459
David Ahern5e670d82018-04-17 17:33:14 -07004460 if (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN) {
David Ahernbeb1afac52017-02-02 12:37:09 -08004461 *flags |= RTNH_F_LINKDOWN;
David Aherndcd1f572018-04-18 15:39:05 -07004462
4463 rcu_read_lock();
4464 if (fib6_ignore_linkdown(rt))
David Ahernbeb1afac52017-02-02 12:37:09 -08004465 *flags |= RTNH_F_DEAD;
David Aherndcd1f572018-04-18 15:39:05 -07004466 rcu_read_unlock();
David Ahernbeb1afac52017-02-02 12:37:09 -08004467 }
4468
David Ahern93c2fb22018-04-18 15:38:59 -07004469 if (rt->fib6_flags & RTF_GATEWAY) {
David Ahern5e670d82018-04-17 17:33:14 -07004470 if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->fib6_nh.nh_gw) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004471 goto nla_put_failure;
4472 }
4473
David Ahern5e670d82018-04-17 17:33:14 -07004474 *flags |= (rt->fib6_nh.nh_flags & RTNH_F_ONLINK);
4475 if (rt->fib6_nh.nh_flags & RTNH_F_OFFLOAD)
Ido Schimmel61e4d012017-08-03 13:28:20 +02004476 *flags |= RTNH_F_OFFLOAD;
4477
David Ahern5be083c2017-03-06 15:57:31 -08004478 /* not needed for multipath encoding b/c it has a rtnexthop struct */
David Ahern5e670d82018-04-17 17:33:14 -07004479 if (!skip_oif && rt->fib6_nh.nh_dev &&
4480 nla_put_u32(skb, RTA_OIF, rt->fib6_nh.nh_dev->ifindex))
David Ahernbeb1afac52017-02-02 12:37:09 -08004481 goto nla_put_failure;
4482
David Ahern5e670d82018-04-17 17:33:14 -07004483 if (rt->fib6_nh.nh_lwtstate &&
4484 lwtunnel_fill_encap(skb, rt->fib6_nh.nh_lwtstate) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004485 goto nla_put_failure;
4486
4487 return 0;
4488
4489nla_put_failure:
4490 return -EMSGSIZE;
4491}
4492
David Ahern5be083c2017-03-06 15:57:31 -08004493/* add multipath next hop */
David Ahern8d1c8022018-04-17 17:33:26 -07004494static int rt6_add_nexthop(struct sk_buff *skb, struct fib6_info *rt)
David Ahernbeb1afac52017-02-02 12:37:09 -08004495{
David Ahern5e670d82018-04-17 17:33:14 -07004496 const struct net_device *dev = rt->fib6_nh.nh_dev;
David Ahernbeb1afac52017-02-02 12:37:09 -08004497 struct rtnexthop *rtnh;
4498 unsigned int flags = 0;
4499
4500 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
4501 if (!rtnh)
4502 goto nla_put_failure;
4503
David Ahern5e670d82018-04-17 17:33:14 -07004504 rtnh->rtnh_hops = rt->fib6_nh.nh_weight - 1;
4505 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
David Ahernbeb1afac52017-02-02 12:37:09 -08004506
David Ahern5be083c2017-03-06 15:57:31 -08004507 if (rt6_nexthop_info(skb, rt, &flags, true) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004508 goto nla_put_failure;
4509
4510 rtnh->rtnh_flags = flags;
4511
4512 /* length of rtnetlink header + attributes */
4513 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
4514
4515 return 0;
4516
4517nla_put_failure:
4518 return -EMSGSIZE;
Thomas Graf339bf982006-11-10 14:10:15 -08004519}
4520
David Ahernd4ead6b2018-04-17 17:33:16 -07004521static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -07004522 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -07004523 struct in6_addr *dest, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004524 int iif, int type, u32 portid, u32 seq,
David Ahernf8cfe2c2017-01-17 15:51:08 -08004525 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526{
4527 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004528 struct nlmsghdr *nlh;
David Ahernd4ead6b2018-04-17 17:33:16 -07004529 long expires = 0;
4530 u32 *pmetrics;
Patrick McHardy9e762a42006-08-10 23:09:48 -07004531 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Eric W. Biederman15e47302012-09-07 20:12:54 +00004533 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05004534 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004535 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004536
4537 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 rtm->rtm_family = AF_INET6;
David Ahern93c2fb22018-04-18 15:38:59 -07004539 rtm->rtm_dst_len = rt->fib6_dst.plen;
4540 rtm->rtm_src_len = rt->fib6_src.plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 rtm->rtm_tos = 0;
David Ahern93c2fb22018-04-18 15:38:59 -07004542 if (rt->fib6_table)
4543 table = rt->fib6_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07004544 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07004545 table = RT6_TABLE_UNSPEC;
4546 rtm->rtm_table = table;
David S. Millerc78679e2012-04-01 20:27:33 -04004547 if (nla_put_u32(skb, RTA_TABLE, table))
4548 goto nla_put_failure;
David Aherne8478e82018-04-17 17:33:13 -07004549
4550 rtm->rtm_type = rt->fib6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 rtm->rtm_flags = 0;
4552 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
David Ahern93c2fb22018-04-18 15:38:59 -07004553 rtm->rtm_protocol = rt->fib6_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
David Ahern93c2fb22018-04-18 15:38:59 -07004555 if (rt->fib6_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 rtm->rtm_flags |= RTM_F_CLONED;
4557
David Ahernd4ead6b2018-04-17 17:33:16 -07004558 if (dest) {
4559 if (nla_put_in6_addr(skb, RTA_DST, dest))
David S. Millerc78679e2012-04-01 20:27:33 -04004560 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004561 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 } else if (rtm->rtm_dst_len)
David Ahern93c2fb22018-04-18 15:38:59 -07004563 if (nla_put_in6_addr(skb, RTA_DST, &rt->fib6_dst.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04004564 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565#ifdef CONFIG_IPV6_SUBTREES
4566 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02004567 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04004568 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004569 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04004570 } else if (rtm->rtm_src_len &&
David Ahern93c2fb22018-04-18 15:38:59 -07004571 nla_put_in6_addr(skb, RTA_SRC, &rt->fib6_src.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04004572 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004574 if (iif) {
4575#ifdef CONFIG_IPV6_MROUTE
David Ahern93c2fb22018-04-18 15:38:59 -07004576 if (ipv6_addr_is_multicast(&rt->fib6_dst.addr)) {
David Ahernfd61c6b2017-01-17 15:51:07 -08004577 int err = ip6mr_get_route(net, skb, rtm, portid);
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02004578
David Ahernfd61c6b2017-01-17 15:51:07 -08004579 if (err == 0)
4580 return 0;
4581 if (err < 0)
4582 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004583 } else
4584#endif
David S. Millerc78679e2012-04-01 20:27:33 -04004585 if (nla_put_u32(skb, RTA_IIF, iif))
4586 goto nla_put_failure;
David Ahernd4ead6b2018-04-17 17:33:16 -07004587 } else if (dest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 struct in6_addr saddr_buf;
David Ahernd4ead6b2018-04-17 17:33:16 -07004589 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02004590 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04004591 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07004593
David Ahern93c2fb22018-04-18 15:38:59 -07004594 if (rt->fib6_prefsrc.plen) {
Daniel Walterc3968a82011-04-13 21:10:57 +00004595 struct in6_addr saddr_buf;
David Ahern93c2fb22018-04-18 15:38:59 -07004596 saddr_buf = rt->fib6_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02004597 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04004598 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00004599 }
4600
David Ahernd4ead6b2018-04-17 17:33:16 -07004601 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
4602 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07004603 goto nla_put_failure;
4604
David Ahern93c2fb22018-04-18 15:38:59 -07004605 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
David S. Millerc78679e2012-04-01 20:27:33 -04004606 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00004607
David Ahernbeb1afac52017-02-02 12:37:09 -08004608 /* For multipath routes, walk the siblings list and add
4609 * each as a nexthop within RTA_MULTIPATH.
4610 */
David Ahern93c2fb22018-04-18 15:38:59 -07004611 if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07004612 struct fib6_info *sibling, *next_sibling;
David Ahernbeb1afac52017-02-02 12:37:09 -08004613 struct nlattr *mp;
4614
4615 mp = nla_nest_start(skb, RTA_MULTIPATH);
4616 if (!mp)
4617 goto nla_put_failure;
4618
4619 if (rt6_add_nexthop(skb, rt) < 0)
4620 goto nla_put_failure;
4621
4622 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07004623 &rt->fib6_siblings, fib6_siblings) {
David Ahernbeb1afac52017-02-02 12:37:09 -08004624 if (rt6_add_nexthop(skb, sibling) < 0)
4625 goto nla_put_failure;
4626 }
4627
4628 nla_nest_end(skb, mp);
4629 } else {
David Ahern5be083c2017-03-06 15:57:31 -08004630 if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004631 goto nla_put_failure;
4632 }
4633
David Ahern93c2fb22018-04-18 15:38:59 -07004634 if (rt->fib6_flags & RTF_EXPIRES) {
David Ahern14895682018-04-17 17:33:17 -07004635 expires = dst ? dst->expires : rt->expires;
4636 expires -= jiffies;
4637 }
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07004638
David Ahernd4ead6b2018-04-17 17:33:16 -07004639 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08004640 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
David Ahern93c2fb22018-04-18 15:38:59 -07004642 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->fib6_flags)))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004643 goto nla_put_failure;
4644
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004645
Johannes Berg053c0952015-01-16 22:09:00 +01004646 nlmsg_end(skb, nlh);
4647 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004648
4649nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004650 nlmsg_cancel(skb, nlh);
4651 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652}
4653
David Ahern8d1c8022018-04-17 17:33:26 -07004654int rt6_dump_route(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655{
4656 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
David Ahern1f17e2f2017-01-26 13:54:08 -08004657 struct net *net = arg->net;
4658
David Ahern421842e2018-04-17 17:33:18 -07004659 if (rt == net->ipv6.fib6_null_entry)
David Ahern1f17e2f2017-01-26 13:54:08 -08004660 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
Thomas Graf2d7202b2006-08-22 00:01:27 -07004662 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
4663 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
David Ahernf8cfe2c2017-01-17 15:51:08 -08004664
4665 /* user wants prefix routes only */
4666 if (rtm->rtm_flags & RTM_F_PREFIX &&
David Ahern93c2fb22018-04-18 15:38:59 -07004667 !(rt->fib6_flags & RTF_PREFIX_RT)) {
David Ahernf8cfe2c2017-01-17 15:51:08 -08004668 /* success since this is not a prefix route */
4669 return 1;
4670 }
4671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
David Ahernd4ead6b2018-04-17 17:33:16 -07004673 return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
4674 RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
4675 arg->cb->nlh->nlmsg_seq, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676}
4677
David Ahernc21ef3e2017-04-16 09:48:24 -07004678static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4679 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004681 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07004682 struct nlattr *tb[RTA_MAX+1];
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004683 int err, iif = 0, oif = 0;
4684 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07004686 struct sk_buff *skb;
4687 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05004688 struct flowi6 fl6;
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004689 bool fibmatch;
Thomas Grafab364a62006-08-22 00:01:47 -07004690
Johannes Bergfceb6432017-04-12 14:34:07 +02004691 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07004692 extack);
Thomas Grafab364a62006-08-22 00:01:47 -07004693 if (err < 0)
4694 goto errout;
4695
4696 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05004697 memset(&fl6, 0, sizeof(fl6));
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02004698 rtm = nlmsg_data(nlh);
4699 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004700 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
Thomas Grafab364a62006-08-22 00:01:47 -07004701
4702 if (tb[RTA_SRC]) {
4703 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4704 goto errout;
4705
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004706 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07004707 }
4708
4709 if (tb[RTA_DST]) {
4710 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4711 goto errout;
4712
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004713 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07004714 }
4715
4716 if (tb[RTA_IIF])
4717 iif = nla_get_u32(tb[RTA_IIF]);
4718
4719 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004720 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07004721
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07004722 if (tb[RTA_MARK])
4723 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
4724
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004725 if (tb[RTA_UID])
4726 fl6.flowi6_uid = make_kuid(current_user_ns(),
4727 nla_get_u32(tb[RTA_UID]));
4728 else
4729 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
4730
Thomas Grafab364a62006-08-22 00:01:47 -07004731 if (iif) {
4732 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004733 int flags = 0;
4734
Florian Westphal121622d2017-08-15 16:34:42 +02004735 rcu_read_lock();
4736
4737 dev = dev_get_by_index_rcu(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07004738 if (!dev) {
Florian Westphal121622d2017-08-15 16:34:42 +02004739 rcu_read_unlock();
Thomas Grafab364a62006-08-22 00:01:47 -07004740 err = -ENODEV;
4741 goto errout;
4742 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004743
4744 fl6.flowi6_iif = iif;
4745
4746 if (!ipv6_addr_any(&fl6.saddr))
4747 flags |= RT6_LOOKUP_F_HAS_SADDR;
4748
David Ahernb75cc8f2018-03-02 08:32:17 -08004749 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
Florian Westphal121622d2017-08-15 16:34:42 +02004750
4751 rcu_read_unlock();
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004752 } else {
4753 fl6.flowi6_oif = oif;
4754
Ido Schimmel58acfd72017-12-20 12:28:25 +02004755 dst = ip6_route_output(net, NULL, &fl6);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004756 }
4757
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004758
4759 rt = container_of(dst, struct rt6_info, dst);
4760 if (rt->dst.error) {
4761 err = rt->dst.error;
4762 ip6_rt_put(rt);
4763 goto errout;
Thomas Grafab364a62006-08-22 00:01:47 -07004764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765
WANG Cong9d6acb32017-03-01 20:48:39 -08004766 if (rt == net->ipv6.ip6_null_entry) {
4767 err = rt->dst.error;
4768 ip6_rt_put(rt);
4769 goto errout;
4770 }
4771
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05004773 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00004774 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07004775 err = -ENOBUFS;
4776 goto errout;
4777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
Changli Gaod8d1f302010-06-10 23:31:35 -07004779 skb_dst_set(skb, &rt->dst);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004780 if (fibmatch)
David Ahern93531c62018-04-17 17:33:25 -07004781 err = rt6_fill_node(net, skb, rt->from, NULL, NULL, NULL, iif,
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004782 RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
4783 nlh->nlmsg_seq, 0);
4784 else
David Ahern93531c62018-04-17 17:33:25 -07004785 err = rt6_fill_node(net, skb, rt->from, dst,
4786 &fl6.daddr, &fl6.saddr, iif, RTM_NEWROUTE,
David Ahernd4ead6b2018-04-17 17:33:16 -07004787 NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
4788 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07004790 kfree_skb(skb);
4791 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 }
4793
Eric W. Biederman15e47302012-09-07 20:12:54 +00004794 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07004795errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797}
4798
David Ahern8d1c8022018-04-17 17:33:26 -07004799void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07004800 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801{
4802 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08004803 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08004804 u32 seq;
4805 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08004807 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05004808 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07004809
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004810 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05004811 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07004812 goto errout;
4813
David Ahernd4ead6b2018-04-17 17:33:16 -07004814 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
4815 event, info->portid, seq, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08004816 if (err < 0) {
4817 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
4818 WARN_ON(err == -EMSGSIZE);
4819 kfree_skb(skb);
4820 goto errout;
4821 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004822 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004823 info->nlh, gfp_any());
4824 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07004825errout:
4826 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08004827 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828}
4829
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004830static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00004831 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004832{
Jiri Pirko351638e2013-05-28 01:30:21 +00004833 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004834 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004835
WANG Cong242d3a42017-05-08 10:12:13 -07004836 if (!(dev->flags & IFF_LOOPBACK))
4837 return NOTIFY_OK;
4838
4839 if (event == NETDEV_REGISTER) {
David Ahern421842e2018-04-17 17:33:18 -07004840 net->ipv6.fib6_null_entry->fib6_nh.nh_dev = dev;
Changli Gaod8d1f302010-06-10 23:31:35 -07004841 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004842 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
4843#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07004844 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004845 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07004846 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004847 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
4848#endif
WANG Cong76da0702017-06-20 11:42:27 -07004849 } else if (event == NETDEV_UNREGISTER &&
4850 dev->reg_state != NETREG_UNREGISTERED) {
4851 /* NETDEV_UNREGISTER could be fired for multiple times by
4852 * netdev_wait_allrefs(). Make sure we only call this once.
4853 */
Eric Dumazet12d94a82017-08-15 04:09:51 -07004854 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07004855#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric Dumazet12d94a82017-08-15 04:09:51 -07004856 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
4857 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07004858#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004859 }
4860
4861 return NOTIFY_OK;
4862}
4863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864/*
4865 * /proc
4866 */
4867
4868#ifdef CONFIG_PROC_FS
4869
Alexey Dobriyan33120b32007-11-06 05:27:11 -08004870static const struct file_operations ipv6_route_proc_fops = {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08004871 .open = ipv6_route_open,
4872 .read = seq_read,
4873 .llseek = seq_lseek,
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02004874 .release = seq_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08004875};
4876
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877static int rt6_stats_seq_show(struct seq_file *seq, void *v)
4878{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004879 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004881 net->ipv6.rt6_stats->fib_nodes,
4882 net->ipv6.rt6_stats->fib_route_nodes,
Wei Wang81eb8442017-10-06 12:06:11 -07004883 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004884 net->ipv6.rt6_stats->fib_rt_entries,
4885 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00004886 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004887 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
4889 return 0;
4890}
4891
4892static int rt6_stats_seq_open(struct inode *inode, struct file *file)
4893{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07004894 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004895}
4896
Arjan van de Ven9a321442007-02-12 00:55:35 -08004897static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 .open = rt6_stats_seq_open,
4899 .read = seq_read,
4900 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07004901 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902};
4903#endif /* CONFIG_PROC_FS */
4904
4905#ifdef CONFIG_SYSCTL
4906
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907static
Joe Perchesfe2c6332013-06-11 23:04:25 -07004908int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 void __user *buffer, size_t *lenp, loff_t *ppos)
4910{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00004911 struct net *net;
4912 int delay;
4913 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00004915
4916 net = (struct net *)ctl->extra1;
4917 delay = net->ipv6.sysctl.flush_delay;
4918 proc_dointvec(ctl, write, buffer, lenp, ppos);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02004919 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00004920 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921}
4922
Joe Perchesfe2c6332013-06-11 23:04:25 -07004923struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004924 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08004926 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07004928 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004929 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 },
4931 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08004933 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 .maxlen = sizeof(int),
4935 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004936 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 },
4938 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08004940 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 .maxlen = sizeof(int),
4942 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004943 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 },
4945 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08004947 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 .maxlen = sizeof(int),
4949 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004950 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 },
4952 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08004954 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 .maxlen = sizeof(int),
4956 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004957 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 },
4959 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08004961 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 .maxlen = sizeof(int),
4963 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004964 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 },
4966 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08004968 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 .maxlen = sizeof(int),
4970 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07004971 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 },
4973 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08004975 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 .maxlen = sizeof(int),
4977 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004978 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 },
4980 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08004982 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 .maxlen = sizeof(int),
4984 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07004985 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 },
4987 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08004989 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 .maxlen = sizeof(int),
4991 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004992 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004994 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995};
4996
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004997struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08004998{
4999 struct ctl_table *table;
5000
5001 table = kmemdup(ipv6_route_table_template,
5002 sizeof(ipv6_route_table_template),
5003 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005004
5005 if (table) {
5006 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005007 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00005008 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005009 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
5010 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
5011 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
5012 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
5013 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
5014 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
5015 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08005016 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
Eric W. Biederman464dc802012-11-16 03:02:59 +00005017
5018 /* Don't export sysctls to unprivileged users */
5019 if (net->user_ns != &init_user_ns)
5020 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005021 }
5022
Daniel Lezcano760f2d02008-01-10 02:53:43 -08005023 return table;
5024}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025#endif
5026
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005027static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005028{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07005029 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005030
Alexey Dobriyan86393e52009-08-29 01:34:49 +00005031 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
5032 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005033
Eric Dumazetfc66f952010-10-08 06:37:34 +00005034 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5035 goto out_ip6_dst_ops;
5036
David Ahern421842e2018-04-17 17:33:18 -07005037 net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template,
5038 sizeof(*net->ipv6.fib6_null_entry),
5039 GFP_KERNEL);
5040 if (!net->ipv6.fib6_null_entry)
5041 goto out_ip6_dst_entries;
5042
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005043 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
5044 sizeof(*net->ipv6.ip6_null_entry),
5045 GFP_KERNEL);
5046 if (!net->ipv6.ip6_null_entry)
David Ahern421842e2018-04-17 17:33:18 -07005047 goto out_fib6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005048 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005049 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
5050 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005051
5052#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Vincent Bernatfeca7d82017-08-08 20:23:49 +02005053 net->ipv6.fib6_has_custom_rules = false;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005054 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
5055 sizeof(*net->ipv6.ip6_prohibit_entry),
5056 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005057 if (!net->ipv6.ip6_prohibit_entry)
5058 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005059 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005060 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
5061 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005062
5063 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
5064 sizeof(*net->ipv6.ip6_blk_hole_entry),
5065 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005066 if (!net->ipv6.ip6_blk_hole_entry)
5067 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005068 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005069 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
5070 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005071#endif
5072
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07005073 net->ipv6.sysctl.flush_delay = 0;
5074 net->ipv6.sysctl.ip6_rt_max_size = 4096;
5075 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5076 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5077 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5078 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5079 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5080 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
5081
Benjamin Thery6891a342008-03-04 13:49:47 -08005082 net->ipv6.ip6_rt_gc_expire = 30*HZ;
5083
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005084 ret = 0;
5085out:
5086 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005087
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005088#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5089out_ip6_prohibit_entry:
5090 kfree(net->ipv6.ip6_prohibit_entry);
5091out_ip6_null_entry:
5092 kfree(net->ipv6.ip6_null_entry);
5093#endif
David Ahern421842e2018-04-17 17:33:18 -07005094out_fib6_null_entry:
5095 kfree(net->ipv6.fib6_null_entry);
Eric Dumazetfc66f952010-10-08 06:37:34 +00005096out_ip6_dst_entries:
5097 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005098out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005099 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005100}
5101
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005102static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005103{
David Ahern421842e2018-04-17 17:33:18 -07005104 kfree(net->ipv6.fib6_null_entry);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005105 kfree(net->ipv6.ip6_null_entry);
5106#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5107 kfree(net->ipv6.ip6_prohibit_entry);
5108 kfree(net->ipv6.ip6_blk_hole_entry);
5109#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00005110 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005111}
5112
Thomas Grafd1896342012-06-18 12:08:33 +00005113static int __net_init ip6_route_net_init_late(struct net *net)
5114{
5115#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00005116 proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
Joe Perchesd6444062018-03-23 15:54:38 -07005117 proc_create("rt6_stats", 0444, net->proc_net, &rt6_stats_seq_fops);
Thomas Grafd1896342012-06-18 12:08:33 +00005118#endif
5119 return 0;
5120}
5121
5122static void __net_exit ip6_route_net_exit_late(struct net *net)
5123{
5124#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00005125 remove_proc_entry("ipv6_route", net->proc_net);
5126 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00005127#endif
5128}
5129
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005130static struct pernet_operations ip6_route_net_ops = {
5131 .init = ip6_route_net_init,
5132 .exit = ip6_route_net_exit,
5133};
5134
David S. Millerc3426b42012-06-09 16:27:05 -07005135static int __net_init ipv6_inetpeer_init(struct net *net)
5136{
5137 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5138
5139 if (!bp)
5140 return -ENOMEM;
5141 inet_peer_base_init(bp);
5142 net->ipv6.peers = bp;
5143 return 0;
5144}
5145
5146static void __net_exit ipv6_inetpeer_exit(struct net *net)
5147{
5148 struct inet_peer_base *bp = net->ipv6.peers;
5149
5150 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07005151 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07005152 kfree(bp);
5153}
5154
David S. Miller2b823f72012-06-09 19:00:16 -07005155static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07005156 .init = ipv6_inetpeer_init,
5157 .exit = ipv6_inetpeer_exit,
5158};
5159
Thomas Grafd1896342012-06-18 12:08:33 +00005160static struct pernet_operations ip6_route_net_late_ops = {
5161 .init = ip6_route_net_init_late,
5162 .exit = ip6_route_net_exit_late,
5163};
5164
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005165static struct notifier_block ip6_route_dev_notifier = {
5166 .notifier_call = ip6_route_dev_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07005167 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005168};
5169
WANG Cong2f460932017-05-03 22:07:31 -07005170void __init ip6_route_init_special_entries(void)
5171{
5172 /* Registering of the loopback is done before this portion of code,
5173 * the loopback reference in rt6_info will not be taken, do it
5174 * manually for init_net */
David Ahern421842e2018-04-17 17:33:18 -07005175 init_net.ipv6.fib6_null_entry->fib6_nh.nh_dev = init_net.loopback_dev;
WANG Cong2f460932017-05-03 22:07:31 -07005176 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
5177 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5178 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5179 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
5180 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5181 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
5182 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5183 #endif
5184}
5185
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005186int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005188 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07005189 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005190
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08005191 ret = -ENOMEM;
5192 ip6_dst_ops_template.kmem_cachep =
5193 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
5194 SLAB_HWCACHE_ALIGN, NULL);
5195 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08005196 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07005197
Eric Dumazetfc66f952010-10-08 06:37:34 +00005198 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005199 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08005200 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08005201
David S. Millerc3426b42012-06-09 16:27:05 -07005202 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
5203 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07005204 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00005205
David S. Miller7e52b332012-06-15 15:51:55 -07005206 ret = register_pernet_subsys(&ip6_route_net_ops);
5207 if (ret)
5208 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07005209
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07005210 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
5211
David S. Millere8803b62012-06-16 01:12:19 -07005212 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005213 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005214 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005215
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005216 ret = xfrm6_init();
5217 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07005218 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08005219
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005220 ret = fib6_rules_init();
5221 if (ret)
5222 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08005223
Thomas Grafd1896342012-06-18 12:08:33 +00005224 ret = register_pernet_subsys(&ip6_route_net_late_ops);
5225 if (ret)
5226 goto fib6_rules_init;
5227
Florian Westphal16feebc2017-12-02 21:44:08 +01005228 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
5229 inet6_rtm_newroute, NULL, 0);
5230 if (ret < 0)
5231 goto out_register_late_subsys;
5232
5233 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
5234 inet6_rtm_delroute, NULL, 0);
5235 if (ret < 0)
5236 goto out_register_late_subsys;
5237
5238 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
5239 inet6_rtm_getroute, NULL,
5240 RTNL_FLAG_DOIT_UNLOCKED);
5241 if (ret < 0)
Thomas Grafd1896342012-06-18 12:08:33 +00005242 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005243
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005244 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005245 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00005246 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005247
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07005248 for_each_possible_cpu(cpu) {
5249 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
5250
5251 INIT_LIST_HEAD(&ul->head);
5252 spin_lock_init(&ul->lock);
5253 }
5254
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005255out:
5256 return ret;
5257
Thomas Grafd1896342012-06-18 12:08:33 +00005258out_register_late_subsys:
Florian Westphal16feebc2017-12-02 21:44:08 +01005259 rtnl_unregister_all(PF_INET6);
Thomas Grafd1896342012-06-18 12:08:33 +00005260 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005261fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005262 fib6_rules_cleanup();
5263xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005264 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00005265out_fib6_init:
5266 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005267out_register_subsys:
5268 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07005269out_register_inetpeer:
5270 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00005271out_dst_entries:
5272 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005273out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005274 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005275 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276}
5277
5278void ip6_route_cleanup(void)
5279{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005280 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00005281 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07005282 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07005285 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005286 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00005287 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005288 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289}