blob: 884d9cee790fcd0a1a58e1e5681fedcbc3e4ad72 [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
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002224static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2225{
2226 struct net *net = dev_net(rt->dst.dev);
2227
David Ahernd4ead6b2018-04-17 17:33:16 -07002228 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002229 rt->rt6i_flags |= RTF_MODIFIED;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002230 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2231}
2232
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002233static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2234{
2235 return !(rt->rt6i_flags & RTF_CACHE) &&
David Ahern77634cc2018-04-17 17:33:27 -07002236 (rt->rt6i_flags & RTF_PCPU || rt->from);
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002237}
2238
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002239static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2240 const struct ipv6hdr *iph, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
Julian Anastasov0dec8792017-02-06 23:14:16 +02002242 const struct in6_addr *daddr, *saddr;
Ian Morris67ba4152014-08-24 21:53:10 +01002243 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002245 if (rt6->rt6i_flags & RTF_LOCAL)
2246 return;
2247
Xin Long19bda362016-10-28 18:18:01 +08002248 if (dst_metric_locked(dst, RTAX_MTU))
2249 return;
2250
Julian Anastasov0dec8792017-02-06 23:14:16 +02002251 if (iph) {
2252 daddr = &iph->daddr;
2253 saddr = &iph->saddr;
2254 } else if (sk) {
2255 daddr = &sk->sk_v6_daddr;
2256 saddr = &inet6_sk(sk)->saddr;
2257 } else {
2258 daddr = NULL;
2259 saddr = NULL;
2260 }
2261 dst_confirm_neigh(dst, daddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002262 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2263 if (mtu >= dst_mtu(dst))
2264 return;
David S. Miller81aded22012-06-15 14:54:11 -07002265
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002266 if (!rt6_cache_allowed_for_pmtu(rt6)) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002267 rt6_do_update_pmtu(rt6, mtu);
Wei Wang2b760fc2017-10-06 12:06:03 -07002268 /* update rt6_ex->stamp for cache */
2269 if (rt6->rt6i_flags & RTF_CACHE)
2270 rt6_update_exception_stamp_rt(rt6);
Julian Anastasov0dec8792017-02-06 23:14:16 +02002271 } else if (daddr) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002272 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01002273
David Ahernd4ead6b2018-04-17 17:33:16 -07002274 nrt6 = ip6_rt_cache_alloc(rt6->from, daddr, saddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002275 if (nrt6) {
2276 rt6_do_update_pmtu(nrt6, mtu);
David Ahernd4ead6b2018-04-17 17:33:16 -07002277 if (rt6_insert_exception(nrt6, rt6->from))
Wei Wang2b760fc2017-10-06 12:06:03 -07002278 dst_release_immediate(&nrt6->dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
2281}
2282
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002283static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2284 struct sk_buff *skb, u32 mtu)
2285{
2286 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
2287}
2288
David S. Miller42ae66c2012-06-15 20:01:57 -07002289void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002290 int oif, u32 mark, kuid_t uid)
David S. Miller81aded22012-06-15 14:54:11 -07002291{
2292 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2293 struct dst_entry *dst;
2294 struct flowi6 fl6;
2295
2296 memset(&fl6, 0, sizeof(fl6));
2297 fl6.flowi6_oif = oif;
Lorenzo Colitti1b3c61d2014-05-13 10:17:34 -07002298 fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
David S. Miller81aded22012-06-15 14:54:11 -07002299 fl6.daddr = iph->daddr;
2300 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002301 fl6.flowlabel = ip6_flowinfo(iph);
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002302 fl6.flowi6_uid = uid;
David S. Miller81aded22012-06-15 14:54:11 -07002303
2304 dst = ip6_route_output(net, NULL, &fl6);
2305 if (!dst->error)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002306 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
David S. Miller81aded22012-06-15 14:54:11 -07002307 dst_release(dst);
2308}
2309EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2310
2311void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2312{
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002313 struct dst_entry *dst;
2314
David S. Miller81aded22012-06-15 14:54:11 -07002315 ip6_update_pmtu(skb, sock_net(sk), mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002316 sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid);
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002317
2318 dst = __sk_dst_get(sk);
2319 if (!dst || !dst->obsolete ||
2320 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2321 return;
2322
2323 bh_lock_sock(sk);
2324 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2325 ip6_datagram_dst_update(sk, false);
2326 bh_unlock_sock(sk);
David S. Miller81aded22012-06-15 14:54:11 -07002327}
2328EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2329
Alexey Kodanev7d6850f2018-04-03 15:00:07 +03002330void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2331 const struct flowi6 *fl6)
2332{
2333#ifdef CONFIG_IPV6_SUBTREES
2334 struct ipv6_pinfo *np = inet6_sk(sk);
2335#endif
2336
2337 ip6_dst_store(sk, dst,
2338 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2339 &sk->sk_v6_daddr : NULL,
2340#ifdef CONFIG_IPV6_SUBTREES
2341 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2342 &np->saddr :
2343#endif
2344 NULL);
2345}
2346
Duan Jiongb55b76b2013-09-04 19:44:21 +08002347/* Handle redirects */
2348struct ip6rd_flowi {
2349 struct flowi6 fl6;
2350 struct in6_addr gateway;
2351};
2352
2353static struct rt6_info *__ip6_route_redirect(struct net *net,
2354 struct fib6_table *table,
2355 struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08002356 const struct sk_buff *skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002357 int flags)
2358{
2359 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
David Ahern23fb93a2018-04-17 17:33:23 -07002360 struct rt6_info *ret = NULL, *rt_cache;
David Ahern8d1c8022018-04-17 17:33:26 -07002361 struct fib6_info *rt;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002362 struct fib6_node *fn;
2363
2364 /* Get the "current" route for this destination and
Alexander Alemayhu67c408c2017-01-07 23:53:00 +01002365 * check if the redirect has come from appropriate router.
Duan Jiongb55b76b2013-09-04 19:44:21 +08002366 *
2367 * RFC 4861 specifies that redirects should only be
2368 * accepted if they come from the nexthop to the target.
2369 * Due to the way the routes are chosen, this notion
2370 * is a bit fuzzy and one might need to check all possible
2371 * routes.
2372 */
2373
Wei Wang66f5d6c2017-10-06 12:06:10 -07002374 rcu_read_lock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08002375 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2376restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002377 for_each_fib6_node_rt_rcu(fn) {
David Ahern5e670d82018-04-17 17:33:14 -07002378 if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +02002379 continue;
David Ahern14895682018-04-17 17:33:17 -07002380 if (fib6_check_expired(rt))
Duan Jiongb55b76b2013-09-04 19:44:21 +08002381 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002382 if (rt->fib6_flags & RTF_REJECT)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002383 break;
David Ahern93c2fb22018-04-18 15:38:59 -07002384 if (!(rt->fib6_flags & RTF_GATEWAY))
Duan Jiongb55b76b2013-09-04 19:44:21 +08002385 continue;
David Ahern5e670d82018-04-17 17:33:14 -07002386 if (fl6->flowi6_oif != rt->fib6_nh.nh_dev->ifindex)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002387 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07002388 /* rt_cache's gateway might be different from its 'parent'
2389 * in the case of an ip redirect.
2390 * So we keep searching in the exception table if the gateway
2391 * is different.
2392 */
David Ahern5e670d82018-04-17 17:33:14 -07002393 if (!ipv6_addr_equal(&rdfl->gateway, &rt->fib6_nh.nh_gw)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07002394 rt_cache = rt6_find_cached_rt(rt,
2395 &fl6->daddr,
2396 &fl6->saddr);
2397 if (rt_cache &&
2398 ipv6_addr_equal(&rdfl->gateway,
2399 &rt_cache->rt6i_gateway)) {
David Ahern23fb93a2018-04-17 17:33:23 -07002400 ret = rt_cache;
Wei Wang2b760fc2017-10-06 12:06:03 -07002401 break;
2402 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002403 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07002404 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002405 break;
2406 }
2407
2408 if (!rt)
David Ahern421842e2018-04-17 17:33:18 -07002409 rt = net->ipv6.fib6_null_entry;
David Ahern93c2fb22018-04-18 15:38:59 -07002410 else if (rt->fib6_flags & RTF_REJECT) {
David Ahern23fb93a2018-04-17 17:33:23 -07002411 ret = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002412 goto out;
2413 }
2414
David Ahern421842e2018-04-17 17:33:18 -07002415 if (rt == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002416 fn = fib6_backtrack(fn, &fl6->saddr);
2417 if (fn)
2418 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002419 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002420
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002421out:
David Ahern23fb93a2018-04-17 17:33:23 -07002422 if (ret)
2423 dst_hold(&ret->dst);
2424 else
2425 ret = ip6_create_rt_rcu(rt);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002426
Wei Wang66f5d6c2017-10-06 12:06:10 -07002427 rcu_read_unlock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08002428
David Ahern23fb93a2018-04-17 17:33:23 -07002429 trace_fib6_table_lookup(net, ret, table, fl6);
2430 return ret;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002431};
2432
2433static struct dst_entry *ip6_route_redirect(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08002434 const struct flowi6 *fl6,
2435 const struct sk_buff *skb,
2436 const struct in6_addr *gateway)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002437{
2438 int flags = RT6_LOOKUP_F_HAS_SADDR;
2439 struct ip6rd_flowi rdfl;
2440
2441 rdfl.fl6 = *fl6;
2442 rdfl.gateway = *gateway;
2443
David Ahernb75cc8f2018-03-02 08:32:17 -08002444 return fib6_rule_lookup(net, &rdfl.fl6, skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002445 flags, __ip6_route_redirect);
2446}
2447
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002448void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2449 kuid_t uid)
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002450{
2451 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2452 struct dst_entry *dst;
2453 struct flowi6 fl6;
2454
2455 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03002456 fl6.flowi6_iif = LOOPBACK_IFINDEX;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002457 fl6.flowi6_oif = oif;
2458 fl6.flowi6_mark = mark;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002459 fl6.daddr = iph->daddr;
2460 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002461 fl6.flowlabel = ip6_flowinfo(iph);
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002462 fl6.flowi6_uid = uid;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002463
David Ahernb75cc8f2018-03-02 08:32:17 -08002464 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002465 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002466 dst_release(dst);
2467}
2468EXPORT_SYMBOL_GPL(ip6_redirect);
2469
Duan Jiongc92a59e2013-08-22 12:07:35 +08002470void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
2471 u32 mark)
2472{
2473 const struct ipv6hdr *iph = ipv6_hdr(skb);
2474 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2475 struct dst_entry *dst;
2476 struct flowi6 fl6;
2477
2478 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03002479 fl6.flowi6_iif = LOOPBACK_IFINDEX;
Duan Jiongc92a59e2013-08-22 12:07:35 +08002480 fl6.flowi6_oif = oif;
2481 fl6.flowi6_mark = mark;
Duan Jiongc92a59e2013-08-22 12:07:35 +08002482 fl6.daddr = msg->dest;
2483 fl6.saddr = iph->daddr;
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002484 fl6.flowi6_uid = sock_net_uid(net, NULL);
Duan Jiongc92a59e2013-08-22 12:07:35 +08002485
David Ahernb75cc8f2018-03-02 08:32:17 -08002486 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002487 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08002488 dst_release(dst);
2489}
2490
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002491void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
2492{
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002493 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2494 sk->sk_uid);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002495}
2496EXPORT_SYMBOL_GPL(ip6_sk_redirect);
2497
David S. Miller0dbaee32010-12-13 12:52:14 -08002498static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
David S. Miller0dbaee32010-12-13 12:52:14 -08002500 struct net_device *dev = dst->dev;
2501 unsigned int mtu = dst_mtu(dst);
2502 struct net *net = dev_net(dev);
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
2505
Daniel Lezcano55786892008-03-04 13:47:47 -08002506 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
2507 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002510 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
2511 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
2512 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 * rely only on pmtu discovery"
2514 */
2515 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
2516 mtu = IPV6_MAXPLEN;
2517 return mtu;
2518}
2519
Steffen Klassertebb762f2011-11-23 02:12:51 +00002520static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002521{
David S. Millerd33e4552010-12-14 13:01:14 -08002522 struct inet6_dev *idev;
David Ahernd4ead6b2018-04-17 17:33:16 -07002523 unsigned int mtu;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002524
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002525 mtu = dst_metric_raw(dst, RTAX_MTU);
2526 if (mtu)
2527 goto out;
2528
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002529 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08002530
2531 rcu_read_lock();
2532 idev = __in6_dev_get(dst->dev);
2533 if (idev)
2534 mtu = idev->cnf.mtu6;
2535 rcu_read_unlock();
2536
Eric Dumazet30f78d82014-04-10 21:23:36 -07002537out:
Roopa Prabhu14972cb2016-08-24 20:10:43 -07002538 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2539
2540 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
David S. Millerd33e4552010-12-14 13:01:14 -08002541}
2542
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08002543struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05002544 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
David S. Miller87a11572011-12-06 17:04:13 -05002546 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 struct rt6_info *rt;
2548 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002549 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
David S. Miller38308472011-12-03 18:02:47 -05002551 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00002552 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Martin KaFai Lauad706862015-08-14 11:05:52 -07002554 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05002555 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05002557 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 goto out;
2559 }
2560
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002561 rt->dst.flags |= DST_HOST;
Brendan McGrath588753f2017-12-13 22:14:57 +11002562 rt->dst.input = ip6_input;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002563 rt->dst.output = ip6_output;
Julian Anastasov550bab42013-10-20 15:43:04 +03002564 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05002565 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002566 rt->rt6i_dst.plen = 128;
2567 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08002568 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Ido Schimmel4c981e22018-01-07 12:45:04 +02002570 /* Add this dst into uncached_list so that rt6_disable_ip() can
Wei Wang587fea72017-06-17 10:42:36 -07002571 * do proper release of the net_device
2572 */
2573 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002574 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
David S. Miller87a11572011-12-06 17:04:13 -05002576 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578out:
David S. Miller87a11572011-12-06 17:04:13 -05002579 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
2581
Daniel Lezcano569d3642008-01-18 03:56:57 -08002582static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002584 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08002585 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
2586 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
2587 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
2588 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
2589 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00002590 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Eric Dumazetfc66f952010-10-08 06:37:34 +00002592 entries = dst_entries_get_fast(ops);
Michal Kubeček49a18d82013-08-01 10:04:24 +02002593 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00002594 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 goto out;
2596
Benjamin Thery6891a342008-03-04 13:49:47 -08002597 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08002598 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00002599 entries = dst_entries_get_slow(ops);
2600 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08002601 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08002603 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00002604 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605}
2606
David Ahern8d1c8022018-04-17 17:33:26 -07002607static int ip6_convert_metrics(struct net *net, struct fib6_info *rt,
David Ahernd4ead6b2018-04-17 17:33:16 -07002608 struct fib6_config *cfg)
Florian Westphale715b6d2015-01-05 23:57:44 +01002609{
Eric Dumazet263243d2018-04-19 09:14:53 -07002610 struct dst_metrics *p;
Florian Westphale715b6d2015-01-05 23:57:44 +01002611
Eric Dumazet263243d2018-04-19 09:14:53 -07002612 if (!cfg->fc_mx)
2613 return 0;
Florian Westphale715b6d2015-01-05 23:57:44 +01002614
Eric Dumazet263243d2018-04-19 09:14:53 -07002615 p = kzalloc(sizeof(*rt->fib6_metrics), GFP_KERNEL);
2616 if (unlikely(!p))
2617 return -ENOMEM;
Florian Westphale715b6d2015-01-05 23:57:44 +01002618
Eric Dumazet263243d2018-04-19 09:14:53 -07002619 refcount_set(&p->refcnt, 1);
2620 rt->fib6_metrics = p;
Florian Westphale715b6d2015-01-05 23:57:44 +01002621
Eric Dumazet263243d2018-04-19 09:14:53 -07002622 return ip_metrics_convert(net, cfg->fc_mx, cfg->fc_mx_len, p->metrics);
Florian Westphale715b6d2015-01-05 23:57:44 +01002623}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
David Ahern8c145862016-04-24 21:26:04 -07002625static struct rt6_info *ip6_nh_lookup_table(struct net *net,
2626 struct fib6_config *cfg,
David Ahernf4797b32018-01-25 16:55:08 -08002627 const struct in6_addr *gw_addr,
2628 u32 tbid, int flags)
David Ahern8c145862016-04-24 21:26:04 -07002629{
2630 struct flowi6 fl6 = {
2631 .flowi6_oif = cfg->fc_ifindex,
2632 .daddr = *gw_addr,
2633 .saddr = cfg->fc_prefsrc,
2634 };
2635 struct fib6_table *table;
2636 struct rt6_info *rt;
David Ahern8c145862016-04-24 21:26:04 -07002637
David Ahernf4797b32018-01-25 16:55:08 -08002638 table = fib6_get_table(net, tbid);
David Ahern8c145862016-04-24 21:26:04 -07002639 if (!table)
2640 return NULL;
2641
2642 if (!ipv6_addr_any(&cfg->fc_prefsrc))
2643 flags |= RT6_LOOKUP_F_HAS_SADDR;
2644
David Ahernf4797b32018-01-25 16:55:08 -08002645 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
David Ahernb75cc8f2018-03-02 08:32:17 -08002646 rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
David Ahern8c145862016-04-24 21:26:04 -07002647
2648 /* if table lookup failed, fall back to full lookup */
2649 if (rt == net->ipv6.ip6_null_entry) {
2650 ip6_rt_put(rt);
2651 rt = NULL;
2652 }
2653
2654 return rt;
2655}
2656
David Ahernfc1e64e2018-01-25 16:55:09 -08002657static int ip6_route_check_nh_onlink(struct net *net,
2658 struct fib6_config *cfg,
David Ahern9fbb7042018-03-13 08:29:36 -07002659 const struct net_device *dev,
David Ahernfc1e64e2018-01-25 16:55:09 -08002660 struct netlink_ext_ack *extack)
2661{
David Ahern44750f82018-02-06 13:17:06 -08002662 u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
David Ahernfc1e64e2018-01-25 16:55:09 -08002663 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2664 u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
2665 struct rt6_info *grt;
2666 int err;
2667
2668 err = 0;
2669 grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
2670 if (grt) {
David Ahern58e354c2018-02-06 12:14:12 -08002671 if (!grt->dst.error &&
2672 (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
David Ahern44750f82018-02-06 13:17:06 -08002673 NL_SET_ERR_MSG(extack,
2674 "Nexthop has invalid gateway or device mismatch");
David Ahernfc1e64e2018-01-25 16:55:09 -08002675 err = -EINVAL;
2676 }
2677
2678 ip6_rt_put(grt);
2679 }
2680
2681 return err;
2682}
2683
David Ahern1edce992018-01-25 16:55:07 -08002684static int ip6_route_check_nh(struct net *net,
2685 struct fib6_config *cfg,
2686 struct net_device **_dev,
2687 struct inet6_dev **idev)
2688{
2689 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2690 struct net_device *dev = _dev ? *_dev : NULL;
2691 struct rt6_info *grt = NULL;
2692 int err = -EHOSTUNREACH;
2693
2694 if (cfg->fc_table) {
David Ahernf4797b32018-01-25 16:55:08 -08002695 int flags = RT6_LOOKUP_F_IFACE;
2696
2697 grt = ip6_nh_lookup_table(net, cfg, gw_addr,
2698 cfg->fc_table, flags);
David Ahern1edce992018-01-25 16:55:07 -08002699 if (grt) {
2700 if (grt->rt6i_flags & RTF_GATEWAY ||
2701 (dev && dev != grt->dst.dev)) {
2702 ip6_rt_put(grt);
2703 grt = NULL;
2704 }
2705 }
2706 }
2707
2708 if (!grt)
David Ahernb75cc8f2018-03-02 08:32:17 -08002709 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
David Ahern1edce992018-01-25 16:55:07 -08002710
2711 if (!grt)
2712 goto out;
2713
2714 if (dev) {
2715 if (dev != grt->dst.dev) {
2716 ip6_rt_put(grt);
2717 goto out;
2718 }
2719 } else {
2720 *_dev = dev = grt->dst.dev;
2721 *idev = grt->rt6i_idev;
2722 dev_hold(dev);
2723 in6_dev_hold(grt->rt6i_idev);
2724 }
2725
2726 if (!(grt->rt6i_flags & RTF_GATEWAY))
2727 err = 0;
2728
2729 ip6_rt_put(grt);
2730
2731out:
2732 return err;
2733}
2734
David Ahern9fbb7042018-03-13 08:29:36 -07002735static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
2736 struct net_device **_dev, struct inet6_dev **idev,
2737 struct netlink_ext_ack *extack)
2738{
2739 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2740 int gwa_type = ipv6_addr_type(gw_addr);
David Ahern232378e2018-03-13 08:29:37 -07002741 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
David Ahern9fbb7042018-03-13 08:29:36 -07002742 const struct net_device *dev = *_dev;
David Ahern232378e2018-03-13 08:29:37 -07002743 bool need_addr_check = !dev;
David Ahern9fbb7042018-03-13 08:29:36 -07002744 int err = -EINVAL;
2745
2746 /* if gw_addr is local we will fail to detect this in case
2747 * address is still TENTATIVE (DAD in progress). rt6_lookup()
2748 * will return already-added prefix route via interface that
2749 * prefix route was assigned to, which might be non-loopback.
2750 */
David Ahern232378e2018-03-13 08:29:37 -07002751 if (dev &&
2752 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2753 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
David Ahern9fbb7042018-03-13 08:29:36 -07002754 goto out;
2755 }
2756
2757 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
2758 /* IPv6 strictly inhibits using not link-local
2759 * addresses as nexthop address.
2760 * Otherwise, router will not able to send redirects.
2761 * It is very good, but in some (rare!) circumstances
2762 * (SIT, PtP, NBMA NOARP links) it is handy to allow
2763 * some exceptions. --ANK
2764 * We allow IPv4-mapped nexthops to support RFC4798-type
2765 * addressing
2766 */
2767 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
2768 NL_SET_ERR_MSG(extack, "Invalid gateway address");
2769 goto out;
2770 }
2771
2772 if (cfg->fc_flags & RTNH_F_ONLINK)
2773 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
2774 else
2775 err = ip6_route_check_nh(net, cfg, _dev, idev);
2776
2777 if (err)
2778 goto out;
2779 }
2780
2781 /* reload in case device was changed */
2782 dev = *_dev;
2783
2784 err = -EINVAL;
2785 if (!dev) {
2786 NL_SET_ERR_MSG(extack, "Egress device not specified");
2787 goto out;
2788 } else if (dev->flags & IFF_LOOPBACK) {
2789 NL_SET_ERR_MSG(extack,
2790 "Egress device can not be loopback device for this route");
2791 goto out;
2792 }
David Ahern232378e2018-03-13 08:29:37 -07002793
2794 /* if we did not check gw_addr above, do so now that the
2795 * egress device has been resolved.
2796 */
2797 if (need_addr_check &&
2798 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2799 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
2800 goto out;
2801 }
2802
David Ahern9fbb7042018-03-13 08:29:36 -07002803 err = 0;
2804out:
2805 return err;
2806}
2807
David Ahern8d1c8022018-04-17 17:33:26 -07002808static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
David Ahernacb54e32018-04-17 17:33:22 -07002809 gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06002810 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811{
Daniel Lezcano55786892008-03-04 13:47:47 -08002812 struct net *net = cfg->fc_nlinfo.nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07002813 struct fib6_info *rt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 struct net_device *dev = NULL;
2815 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07002816 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 int addr_type;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002818 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
David Ahern557c44b2017-04-19 14:19:43 -07002820 /* RTF_PCPU is an internal flag; can not be set by userspace */
David Ahernd5d531c2017-05-21 10:12:05 -06002821 if (cfg->fc_flags & RTF_PCPU) {
2822 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
David Ahern557c44b2017-04-19 14:19:43 -07002823 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06002824 }
David Ahern557c44b2017-04-19 14:19:43 -07002825
Wei Wang2ea23522017-10-27 17:30:12 -07002826 /* RTF_CACHE is an internal flag; can not be set by userspace */
2827 if (cfg->fc_flags & RTF_CACHE) {
2828 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
2829 goto out;
2830 }
2831
David Aherne8478e82018-04-17 17:33:13 -07002832 if (cfg->fc_type > RTN_MAX) {
2833 NL_SET_ERR_MSG(extack, "Invalid route type");
2834 goto out;
2835 }
2836
David Ahernd5d531c2017-05-21 10:12:05 -06002837 if (cfg->fc_dst_len > 128) {
2838 NL_SET_ERR_MSG(extack, "Invalid prefix length");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002839 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06002840 }
2841 if (cfg->fc_src_len > 128) {
2842 NL_SET_ERR_MSG(extack, "Invalid source address length");
2843 goto out;
2844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845#ifndef CONFIG_IPV6_SUBTREES
David Ahernd5d531c2017-05-21 10:12:05 -06002846 if (cfg->fc_src_len) {
2847 NL_SET_ERR_MSG(extack,
2848 "Specifying source address requires IPV6_SUBTREES to be enabled");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002849 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06002850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07002852 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08002854 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 if (!dev)
2856 goto out;
2857 idev = in6_dev_get(dev);
2858 if (!idev)
2859 goto out;
2860 }
2861
Thomas Graf86872cb2006-08-22 00:01:08 -07002862 if (cfg->fc_metric == 0)
2863 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
David Ahernfc1e64e2018-01-25 16:55:09 -08002865 if (cfg->fc_flags & RTNH_F_ONLINK) {
2866 if (!dev) {
2867 NL_SET_ERR_MSG(extack,
2868 "Nexthop device required for onlink");
2869 err = -ENODEV;
2870 goto out;
2871 }
2872
2873 if (!(dev->flags & IFF_UP)) {
2874 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2875 err = -ENETDOWN;
2876 goto out;
2877 }
2878 }
2879
Matti Vaittinend71314b2011-11-14 00:14:49 +00002880 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05002881 if (cfg->fc_nlinfo.nlh &&
2882 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00002883 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05002884 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00002885 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00002886 table = fib6_new_table(net, cfg->fc_table);
2887 }
2888 } else {
2889 table = fib6_new_table(net, cfg->fc_table);
2890 }
David S. Miller38308472011-12-03 18:02:47 -05002891
2892 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002893 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07002894
David Ahern93531c62018-04-17 17:33:25 -07002895 err = -ENOMEM;
2896 rt = fib6_info_alloc(gfp_flags);
2897 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 goto out;
David Ahern93531c62018-04-17 17:33:25 -07002899
2900 if (cfg->fc_flags & RTF_ADDRCONF)
2901 rt->dst_nocount = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
David Ahernd4ead6b2018-04-17 17:33:16 -07002903 err = ip6_convert_metrics(net, rt, cfg);
2904 if (err < 0)
2905 goto out;
2906
Gao feng1716a962012-04-06 00:13:10 +00002907 if (cfg->fc_flags & RTF_EXPIRES)
David Ahern14895682018-04-17 17:33:17 -07002908 fib6_set_expires(rt, jiffies +
Gao feng1716a962012-04-06 00:13:10 +00002909 clock_t_to_jiffies(cfg->fc_expires));
2910 else
David Ahern14895682018-04-17 17:33:17 -07002911 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Thomas Graf86872cb2006-08-22 00:01:08 -07002913 if (cfg->fc_protocol == RTPROT_UNSPEC)
2914 cfg->fc_protocol = RTPROT_BOOT;
David Ahern93c2fb22018-04-18 15:38:59 -07002915 rt->fib6_protocol = cfg->fc_protocol;
Thomas Graf86872cb2006-08-22 00:01:08 -07002916
2917 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002919 if (cfg->fc_encap) {
2920 struct lwtunnel_state *lwtstate;
2921
David Ahern30357d72017-01-30 12:07:37 -08002922 err = lwtunnel_build_state(cfg->fc_encap_type,
Tom Herbert127eb7c2015-08-24 09:45:41 -07002923 cfg->fc_encap, AF_INET6, cfg,
David Ahern9ae28722017-05-27 16:19:28 -06002924 &lwtstate, extack);
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002925 if (err)
2926 goto out;
David Ahern5e670d82018-04-17 17:33:14 -07002927 rt->fib6_nh.nh_lwtstate = lwtstate_get(lwtstate);
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002928 }
2929
David Ahern93c2fb22018-04-18 15:38:59 -07002930 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
2931 rt->fib6_dst.plen = cfg->fc_dst_len;
2932 if (rt->fib6_dst.plen == 128)
David Ahern3b6761d2018-04-17 17:33:20 -07002933 rt->dst_host = true;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01002934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07002936 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
2937 rt->fib6_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938#endif
2939
David Ahern93c2fb22018-04-18 15:38:59 -07002940 rt->fib6_metric = cfg->fc_metric;
David Ahern5e670d82018-04-17 17:33:14 -07002941 rt->fib6_nh.nh_weight = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
David Aherne8478e82018-04-17 17:33:13 -07002943 rt->fib6_type = cfg->fc_type;
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 /* We cannot add true routes via loopback here,
2946 they would result in kernel looping; promote them to reject routes
2947 */
Thomas Graf86872cb2006-08-22 00:01:08 -07002948 if ((cfg->fc_flags & RTF_REJECT) ||
David S. Miller38308472011-12-03 18:02:47 -05002949 (dev && (dev->flags & IFF_LOOPBACK) &&
2950 !(addr_type & IPV6_ADDR_LOOPBACK) &&
2951 !(cfg->fc_flags & RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08002953 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if (dev) {
2955 dev_put(dev);
2956 in6_dev_put(idev);
2957 }
Daniel Lezcano55786892008-03-04 13:47:47 -08002958 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 dev_hold(dev);
2960 idev = in6_dev_get(dev);
2961 if (!idev) {
2962 err = -ENODEV;
2963 goto out;
2964 }
2965 }
David Ahern93c2fb22018-04-18 15:38:59 -07002966 rt->fib6_flags = RTF_REJECT|RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 goto install_route;
2968 }
2969
Thomas Graf86872cb2006-08-22 00:01:08 -07002970 if (cfg->fc_flags & RTF_GATEWAY) {
David Ahern9fbb7042018-03-13 08:29:36 -07002971 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
2972 if (err)
Florian Westphal48ed7b22015-05-21 00:25:41 +02002973 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
David Ahern93531c62018-04-17 17:33:25 -07002975 rt->fib6_nh.nh_gw = cfg->fc_gateway;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 }
2977
2978 err = -ENODEV;
David S. Miller38308472011-12-03 18:02:47 -05002979 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 goto out;
2981
Lorenzo Bianconi428604f2018-03-29 11:02:24 +02002982 if (idev->cnf.disable_ipv6) {
2983 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
2984 err = -EACCES;
2985 goto out;
2986 }
2987
David Ahern955ec4c2018-01-24 19:45:29 -08002988 if (!(dev->flags & IFF_UP)) {
2989 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2990 err = -ENETDOWN;
2991 goto out;
2992 }
2993
Daniel Walterc3968a82011-04-13 21:10:57 +00002994 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
2995 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
David Ahernd5d531c2017-05-21 10:12:05 -06002996 NL_SET_ERR_MSG(extack, "Invalid source address");
Daniel Walterc3968a82011-04-13 21:10:57 +00002997 err = -EINVAL;
2998 goto out;
2999 }
David Ahern93c2fb22018-04-18 15:38:59 -07003000 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3001 rt->fib6_prefsrc.plen = 128;
Daniel Walterc3968a82011-04-13 21:10:57 +00003002 } else
David Ahern93c2fb22018-04-18 15:38:59 -07003003 rt->fib6_prefsrc.plen = 0;
Daniel Walterc3968a82011-04-13 21:10:57 +00003004
David Ahern93c2fb22018-04-18 15:38:59 -07003005 rt->fib6_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007install_route:
David Ahern93c2fb22018-04-18 15:38:59 -07003008 if (!(rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
Ido Schimmel5609b802018-01-07 12:45:06 +02003009 !netif_carrier_ok(dev))
David Ahern5e670d82018-04-17 17:33:14 -07003010 rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
3011 rt->fib6_nh.nh_flags |= (cfg->fc_flags & RTNH_F_ONLINK);
David Ahern93531c62018-04-17 17:33:25 -07003012 rt->fib6_nh.nh_dev = dev;
David Ahern93c2fb22018-04-18 15:38:59 -07003013 rt->fib6_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08003014
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003015 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08003016
David Aherndcd1f572018-04-18 15:39:05 -07003017 if (idev)
3018 in6_dev_put(idev);
3019
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003020 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021out:
3022 if (dev)
3023 dev_put(dev);
3024 if (idev)
3025 in6_dev_put(idev);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003026
David Ahern93531c62018-04-17 17:33:25 -07003027 fib6_info_release(rt);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003028 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003029}
3030
David Ahernacb54e32018-04-17 17:33:22 -07003031int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3032 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003033{
David Ahern8d1c8022018-04-17 17:33:26 -07003034 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003035 int err;
3036
David Ahernacb54e32018-04-17 17:33:22 -07003037 rt = ip6_route_info_create(cfg, gfp_flags, extack);
David Ahernd4ead6b2018-04-17 17:33:16 -07003038 if (IS_ERR(rt))
3039 return PTR_ERR(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003040
David Ahernd4ead6b2018-04-17 17:33:16 -07003041 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
David Ahern93531c62018-04-17 17:33:25 -07003042 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 return err;
3045}
3046
David Ahern8d1c8022018-04-17 17:33:26 -07003047static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
David Ahernafb1d4b52018-04-17 17:33:11 -07003049 struct net *net = info->nl_net;
Thomas Grafc71099a2006-08-04 23:20:06 -07003050 struct fib6_table *table;
David Ahernafb1d4b52018-04-17 17:33:11 -07003051 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
David Ahern421842e2018-04-17 17:33:18 -07003053 if (rt == net->ipv6.fib6_null_entry) {
Gao feng6825a262012-09-19 19:25:34 +00003054 err = -ENOENT;
3055 goto out;
3056 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07003057
David Ahern93c2fb22018-04-18 15:38:59 -07003058 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003059 spin_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07003060 err = fib6_del(rt, info);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003061 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Gao feng6825a262012-09-19 19:25:34 +00003063out:
David Ahern93531c62018-04-17 17:33:25 -07003064 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 return err;
3066}
3067
David Ahern8d1c8022018-04-17 17:33:26 -07003068int ip6_del_rt(struct net *net, struct fib6_info *rt)
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003069{
David Ahernafb1d4b52018-04-17 17:33:11 -07003070 struct nl_info info = { .nl_net = net };
3071
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003072 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003073}
3074
David Ahern8d1c8022018-04-17 17:33:26 -07003075static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
David Ahern0ae81332017-02-02 12:37:08 -08003076{
3077 struct nl_info *info = &cfg->fc_nlinfo;
WANG Conge3330032017-02-27 16:07:43 -08003078 struct net *net = info->nl_net;
David Ahern16a16cd2017-02-02 12:37:11 -08003079 struct sk_buff *skb = NULL;
David Ahern0ae81332017-02-02 12:37:08 -08003080 struct fib6_table *table;
WANG Conge3330032017-02-27 16:07:43 -08003081 int err = -ENOENT;
David Ahern0ae81332017-02-02 12:37:08 -08003082
David Ahern421842e2018-04-17 17:33:18 -07003083 if (rt == net->ipv6.fib6_null_entry)
WANG Conge3330032017-02-27 16:07:43 -08003084 goto out_put;
David Ahern93c2fb22018-04-18 15:38:59 -07003085 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003086 spin_lock_bh(&table->tb6_lock);
David Ahern0ae81332017-02-02 12:37:08 -08003087
David Ahern93c2fb22018-04-18 15:38:59 -07003088 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
David Ahern8d1c8022018-04-17 17:33:26 -07003089 struct fib6_info *sibling, *next_sibling;
David Ahern0ae81332017-02-02 12:37:08 -08003090
David Ahern16a16cd2017-02-02 12:37:11 -08003091 /* prefer to send a single notification with all hops */
3092 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3093 if (skb) {
3094 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3095
David Ahernd4ead6b2018-04-17 17:33:16 -07003096 if (rt6_fill_node(net, skb, rt, NULL,
David Ahern16a16cd2017-02-02 12:37:11 -08003097 NULL, NULL, 0, RTM_DELROUTE,
3098 info->portid, seq, 0) < 0) {
3099 kfree_skb(skb);
3100 skb = NULL;
3101 } else
3102 info->skip_notify = 1;
3103 }
3104
David Ahern0ae81332017-02-02 12:37:08 -08003105 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07003106 &rt->fib6_siblings,
3107 fib6_siblings) {
David Ahern0ae81332017-02-02 12:37:08 -08003108 err = fib6_del(sibling, info);
3109 if (err)
WANG Conge3330032017-02-27 16:07:43 -08003110 goto out_unlock;
David Ahern0ae81332017-02-02 12:37:08 -08003111 }
3112 }
3113
3114 err = fib6_del(rt, info);
WANG Conge3330032017-02-27 16:07:43 -08003115out_unlock:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003116 spin_unlock_bh(&table->tb6_lock);
WANG Conge3330032017-02-27 16:07:43 -08003117out_put:
David Ahern93531c62018-04-17 17:33:25 -07003118 fib6_info_release(rt);
David Ahern16a16cd2017-02-02 12:37:11 -08003119
3120 if (skb) {
WANG Conge3330032017-02-27 16:07:43 -08003121 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
David Ahern16a16cd2017-02-02 12:37:11 -08003122 info->nlh, gfp_any());
3123 }
David Ahern0ae81332017-02-02 12:37:08 -08003124 return err;
3125}
3126
David Ahern23fb93a2018-04-17 17:33:23 -07003127static int ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
3128{
3129 int rc = -ESRCH;
3130
3131 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3132 goto out;
3133
3134 if (cfg->fc_flags & RTF_GATEWAY &&
3135 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3136 goto out;
3137 if (dst_hold_safe(&rt->dst))
3138 rc = rt6_remove_exception_rt(rt);
3139out:
3140 return rc;
3141}
3142
David Ahern333c4302017-05-21 10:12:04 -06003143static int ip6_route_del(struct fib6_config *cfg,
3144 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145{
David Ahern8d1c8022018-04-17 17:33:26 -07003146 struct rt6_info *rt_cache;
Thomas Grafc71099a2006-08-04 23:20:06 -07003147 struct fib6_table *table;
David Ahern8d1c8022018-04-17 17:33:26 -07003148 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 int err = -ESRCH;
3151
Daniel Lezcano55786892008-03-04 13:47:47 -08003152 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David Ahernd5d531c2017-05-21 10:12:05 -06003153 if (!table) {
3154 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Grafc71099a2006-08-04 23:20:06 -07003155 return err;
David Ahernd5d531c2017-05-21 10:12:05 -06003156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Wei Wang66f5d6c2017-10-06 12:06:10 -07003158 rcu_read_lock();
Thomas Grafc71099a2006-08-04 23:20:06 -07003159
3160 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07003161 &cfg->fc_dst, cfg->fc_dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07003162 &cfg->fc_src, cfg->fc_src_len,
Wei Wang2b760fc2017-10-06 12:06:03 -07003163 !(cfg->fc_flags & RTF_CACHE));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 if (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003166 for_each_fib6_node_rt_rcu(fn) {
Wei Wang2b760fc2017-10-06 12:06:03 -07003167 if (cfg->fc_flags & RTF_CACHE) {
David Ahern23fb93a2018-04-17 17:33:23 -07003168 int rc;
3169
Wei Wang2b760fc2017-10-06 12:06:03 -07003170 rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst,
3171 &cfg->fc_src);
David Ahern23fb93a2018-04-17 17:33:23 -07003172 if (rt_cache) {
3173 rc = ip6_del_cached_rt(rt_cache, cfg);
3174 if (rc != -ESRCH)
3175 return rc;
3176 }
3177 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07003178 }
Thomas Graf86872cb2006-08-22 00:01:08 -07003179 if (cfg->fc_ifindex &&
David Ahern5e670d82018-04-17 17:33:14 -07003180 (!rt->fib6_nh.nh_dev ||
3181 rt->fib6_nh.nh_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07003183 if (cfg->fc_flags & RTF_GATEWAY &&
David Ahern5e670d82018-04-17 17:33:14 -07003184 !ipv6_addr_equal(&cfg->fc_gateway, &rt->fib6_nh.nh_gw))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003186 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003188 if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol)
Mantas Mc2ed1882016-12-16 10:30:59 +02003189 continue;
David Ahern93531c62018-04-17 17:33:25 -07003190 fib6_info_hold(rt);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003191 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
David Ahern0ae81332017-02-02 12:37:08 -08003193 /* if gateway was specified only delete the one hop */
3194 if (cfg->fc_flags & RTF_GATEWAY)
3195 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3196
3197 return __ip6_del_rt_siblings(rt, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 }
3199 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003200 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
3202 return err;
3203}
3204
David S. Miller6700c272012-07-17 03:29:28 -07003205static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003206{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003207 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07003208 struct rt6_info *rt, *nrt = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07003209 struct ndisc_options ndopts;
3210 struct inet6_dev *in6_dev;
3211 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003212 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07003213 int optlen, on_link;
3214 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07003215
Simon Horman29a3cad2013-05-28 20:34:26 +00003216 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003217 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07003218
3219 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07003220 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003221 return;
3222 }
3223
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003224 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07003225
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003226 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003227 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003228 return;
3229 }
3230
David S. Miller6e157b62012-07-12 00:05:02 -07003231 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003232 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003233 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003234 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07003235 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003236 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003237 return;
3238 }
3239
3240 in6_dev = __in6_dev_get(skb->dev);
3241 if (!in6_dev)
3242 return;
3243 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3244 return;
3245
3246 /* RFC2461 8.1:
3247 * The IP source address of the Redirect MUST be the same as the current
3248 * first-hop router for the specified ICMP Destination Address.
3249 */
3250
Alexander Aringf997c552016-06-15 21:20:23 +02003251 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003252 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3253 return;
3254 }
David S. Miller6e157b62012-07-12 00:05:02 -07003255
3256 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07003257 if (ndopts.nd_opts_tgt_lladdr) {
3258 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3259 skb->dev);
3260 if (!lladdr) {
3261 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3262 return;
3263 }
3264 }
3265
David S. Miller6e157b62012-07-12 00:05:02 -07003266 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01003267 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07003268 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
3269 return;
3270 }
3271
3272 /* Redirect received -> path was valid.
3273 * Look, redirects are sent only in response to data packets,
3274 * so that this nexthop apparently is reachable. --ANK
3275 */
Julian Anastasov0dec8792017-02-06 23:14:16 +02003276 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
David S. Miller6e157b62012-07-12 00:05:02 -07003277
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003278 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07003279 if (!neigh)
3280 return;
3281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 /*
3283 * We have finally decided to accept it.
3284 */
3285
Alexander Aringf997c552016-06-15 21:20:23 +02003286 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 NEIGH_UPDATE_F_WEAK_OVERRIDE|
3288 NEIGH_UPDATE_F_OVERRIDE|
3289 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02003290 NEIGH_UPDATE_F_ISROUTER)),
3291 NDISC_REDIRECT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
David Ahern23fb93a2018-04-17 17:33:23 -07003293 nrt = ip6_rt_cache_alloc(rt->from, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05003294 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 goto out;
3296
3297 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
3298 if (on_link)
3299 nrt->rt6i_flags &= ~RTF_GATEWAY;
3300
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003301 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
Wei Wang2b760fc2017-10-06 12:06:03 -07003303 /* No need to remove rt from the exception table if rt is
3304 * a cached route because rt6_insert_exception() will
3305 * takes care of it
3306 */
David Ahernd4ead6b2018-04-17 17:33:16 -07003307 if (rt6_insert_exception(nrt, rt->from)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07003308 dst_release_immediate(&nrt->dst);
3309 goto out;
3310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
Changli Gaod8d1f302010-06-10 23:31:35 -07003312 netevent.old = &rt->dst;
3313 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003314 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00003315 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07003316 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
3317
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318out:
David S. Millere8599ff2012-07-11 23:43:53 -07003319 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07003320}
3321
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003322#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -07003323static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003324 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07003325 const struct in6_addr *gwaddr,
3326 struct net_device *dev)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003327{
David Ahern830218c2016-10-24 10:52:35 -07003328 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3329 int ifindex = dev->ifindex;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003330 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07003331 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003332 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003333
David Ahern830218c2016-10-24 10:52:35 -07003334 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05003335 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003336 return NULL;
3337
Wei Wang66f5d6c2017-10-06 12:06:10 -07003338 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07003339 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003340 if (!fn)
3341 goto out;
3342
Wei Wang66f5d6c2017-10-06 12:06:10 -07003343 for_each_fib6_node_rt_rcu(fn) {
David Ahern5e670d82018-04-17 17:33:14 -07003344 if (rt->fib6_nh.nh_dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003345 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003346 if ((rt->fib6_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003347 continue;
David Ahern5e670d82018-04-17 17:33:14 -07003348 if (!ipv6_addr_equal(&rt->fib6_nh.nh_gw, gwaddr))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003349 continue;
David Ahern8d1c8022018-04-17 17:33:26 -07003350 fib6_info_hold(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003351 break;
3352 }
3353out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003354 rcu_read_unlock();
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003355 return rt;
3356}
3357
David Ahern8d1c8022018-04-17 17:33:26 -07003358static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003359 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07003360 const struct in6_addr *gwaddr,
3361 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +00003362 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003363{
Thomas Graf86872cb2006-08-22 00:01:08 -07003364 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08003365 .fc_metric = IP6_RT_PRIO_USER,
David Ahern830218c2016-10-24 10:52:35 -07003366 .fc_ifindex = dev->ifindex,
Thomas Graf86872cb2006-08-22 00:01:08 -07003367 .fc_dst_len = prefixlen,
3368 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
3369 RTF_UP | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08003370 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07003371 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003372 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08003373 .fc_nlinfo.nlh = NULL,
3374 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07003375 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003376
David Ahern830218c2016-10-24 10:52:35 -07003377 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003378 cfg.fc_dst = *prefix;
3379 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07003380
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08003381 /* We should treat it as a default route if prefix length is 0. */
3382 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07003383 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003384
David Ahernacb54e32018-04-17 17:33:22 -07003385 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003386
David Ahern830218c2016-10-24 10:52:35 -07003387 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003388}
3389#endif
3390
David Ahern8d1c8022018-04-17 17:33:26 -07003391struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07003392 const struct in6_addr *addr,
3393 struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003394{
David Ahern830218c2016-10-24 10:52:35 -07003395 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
David Ahern8d1c8022018-04-17 17:33:26 -07003396 struct fib6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07003397 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
David Ahernafb1d4b52018-04-17 17:33:11 -07003399 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05003400 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003401 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
Wei Wang66f5d6c2017-10-06 12:06:10 -07003403 rcu_read_lock();
3404 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Ahern5e670d82018-04-17 17:33:14 -07003405 if (dev == rt->fib6_nh.nh_dev &&
David Ahern93c2fb22018-04-18 15:38:59 -07003406 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Ahern5e670d82018-04-17 17:33:14 -07003407 ipv6_addr_equal(&rt->fib6_nh.nh_gw, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 break;
3409 }
3410 if (rt)
David Ahern8d1c8022018-04-17 17:33:26 -07003411 fib6_info_hold(rt);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003412 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return rt;
3414}
3415
David Ahern8d1c8022018-04-17 17:33:26 -07003416struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07003417 const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08003418 struct net_device *dev,
3419 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420{
Thomas Graf86872cb2006-08-22 00:01:08 -07003421 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07003422 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08003423 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07003424 .fc_ifindex = dev->ifindex,
3425 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
3426 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08003427 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07003428 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003429 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08003430 .fc_nlinfo.nlh = NULL,
David Ahernafb1d4b52018-04-17 17:33:11 -07003431 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07003432 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003434 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
David Ahernacb54e32018-04-17 17:33:22 -07003436 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
David Ahern830218c2016-10-24 10:52:35 -07003437 struct fib6_table *table;
3438
3439 table = fib6_get_table(dev_net(dev), cfg.fc_table);
3440 if (table)
3441 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
David Ahernafb1d4b52018-04-17 17:33:11 -07003444 return rt6_get_dflt_router(net, gwaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445}
3446
David Ahernafb1d4b52018-04-17 17:33:11 -07003447static void __rt6_purge_dflt_routers(struct net *net,
3448 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449{
David Ahern8d1c8022018-04-17 17:33:26 -07003450 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
3452restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003453 rcu_read_lock();
3454 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Aherndcd1f572018-04-18 15:39:05 -07003455 struct net_device *dev = fib6_info_nh_dev(rt);
3456 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
3457
David Ahern93c2fb22018-04-18 15:38:59 -07003458 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
David Aherndcd1f572018-04-18 15:39:05 -07003459 (!idev || idev->cnf.accept_ra != 2)) {
David Ahern93531c62018-04-17 17:33:25 -07003460 fib6_info_hold(rt);
3461 rcu_read_unlock();
3462 ip6_del_rt(net, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 goto restart;
3464 }
3465 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003466 rcu_read_unlock();
David Ahern830218c2016-10-24 10:52:35 -07003467
3468 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3469}
3470
3471void rt6_purge_dflt_routers(struct net *net)
3472{
3473 struct fib6_table *table;
3474 struct hlist_head *head;
3475 unsigned int h;
3476
3477 rcu_read_lock();
3478
3479 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3480 head = &net->ipv6.fib_table_hash[h];
3481 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3482 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
David Ahernafb1d4b52018-04-17 17:33:11 -07003483 __rt6_purge_dflt_routers(net, table);
David Ahern830218c2016-10-24 10:52:35 -07003484 }
3485 }
3486
3487 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488}
3489
Daniel Lezcano55786892008-03-04 13:47:47 -08003490static void rtmsg_to_fib6_config(struct net *net,
3491 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07003492 struct fib6_config *cfg)
3493{
3494 memset(cfg, 0, sizeof(*cfg));
3495
David Ahernca254492015-10-12 11:47:10 -07003496 cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
3497 : RT6_TABLE_MAIN;
Thomas Graf86872cb2006-08-22 00:01:08 -07003498 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
3499 cfg->fc_metric = rtmsg->rtmsg_metric;
3500 cfg->fc_expires = rtmsg->rtmsg_info;
3501 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
3502 cfg->fc_src_len = rtmsg->rtmsg_src_len;
3503 cfg->fc_flags = rtmsg->rtmsg_flags;
David Aherne8478e82018-04-17 17:33:13 -07003504 cfg->fc_type = rtmsg->rtmsg_type;
Thomas Graf86872cb2006-08-22 00:01:08 -07003505
Daniel Lezcano55786892008-03-04 13:47:47 -08003506 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08003507
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003508 cfg->fc_dst = rtmsg->rtmsg_dst;
3509 cfg->fc_src = rtmsg->rtmsg_src;
3510 cfg->fc_gateway = rtmsg->rtmsg_gateway;
Thomas Graf86872cb2006-08-22 00:01:08 -07003511}
3512
Daniel Lezcano55786892008-03-04 13:47:47 -08003513int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514{
Thomas Graf86872cb2006-08-22 00:01:08 -07003515 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 struct in6_rtmsg rtmsg;
3517 int err;
3518
Ian Morris67ba4152014-08-24 21:53:10 +01003519 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 case SIOCADDRT: /* Add a route */
3521 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00003522 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 return -EPERM;
3524 err = copy_from_user(&rtmsg, arg,
3525 sizeof(struct in6_rtmsg));
3526 if (err)
3527 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07003528
Daniel Lezcano55786892008-03-04 13:47:47 -08003529 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07003530
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 rtnl_lock();
3532 switch (cmd) {
3533 case SIOCADDRT:
David Ahernacb54e32018-04-17 17:33:22 -07003534 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 break;
3536 case SIOCDELRT:
David Ahern333c4302017-05-21 10:12:04 -06003537 err = ip6_route_del(&cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 break;
3539 default:
3540 err = -EINVAL;
3541 }
3542 rtnl_unlock();
3543
3544 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
3547 return -EINVAL;
3548}
3549
3550/*
3551 * Drop the packet on the floor
3552 */
3553
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07003554static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003556 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00003557 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003558 switch (ipstats_mib_noroutes) {
3559 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07003560 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00003561 if (type == IPV6_ADDR_ANY) {
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -04003562 IP6_INC_STATS(dev_net(dst->dev),
3563 __in6_dev_get_safely(skb->dev),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07003564 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003565 break;
3566 }
3567 /* FALLTHROUGH */
3568 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07003569 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
3570 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003571 break;
3572 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00003573 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 kfree_skb(skb);
3575 return 0;
3576}
3577
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003578static int ip6_pkt_discard(struct sk_buff *skb)
3579{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003580 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003581}
3582
Eric W. Biedermanede20592015-10-07 16:48:47 -05003583static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
Eric Dumazetadf30902009-06-02 05:19:30 +00003585 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003586 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587}
3588
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003589static int ip6_pkt_prohibit(struct sk_buff *skb)
3590{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003591 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003592}
3593
Eric W. Biedermanede20592015-10-07 16:48:47 -05003594static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003595{
Eric Dumazetadf30902009-06-02 05:19:30 +00003596 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003597 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003598}
3599
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600/*
3601 * Allocate a dst for local (unicast / anycast) address.
3602 */
3603
David Ahern360a9882018-04-18 15:39:00 -07003604struct fib6_info *addrconf_f6i_alloc(struct net *net,
3605 struct inet6_dev *idev,
3606 const struct in6_addr *addr,
3607 bool anycast, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{
David Ahernca254492015-10-12 11:47:10 -07003609 u32 tb_id;
David Ahern4832c302017-08-17 12:17:20 -07003610 struct net_device *dev = idev->dev;
David Ahern360a9882018-04-18 15:39:00 -07003611 struct fib6_info *f6i;
David Ahern5f02ce242016-09-10 12:09:54 -07003612
David Ahern360a9882018-04-18 15:39:00 -07003613 f6i = fib6_info_alloc(gfp_flags);
3614 if (!f6i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 return ERR_PTR(-ENOMEM);
3616
David Ahern360a9882018-04-18 15:39:00 -07003617 f6i->dst_nocount = true;
David Ahern360a9882018-04-18 15:39:00 -07003618 f6i->dst_host = true;
3619 f6i->fib6_protocol = RTPROT_KERNEL;
3620 f6i->fib6_flags = RTF_UP | RTF_NONEXTHOP;
David Aherne8478e82018-04-17 17:33:13 -07003621 if (anycast) {
David Ahern360a9882018-04-18 15:39:00 -07003622 f6i->fib6_type = RTN_ANYCAST;
3623 f6i->fib6_flags |= RTF_ANYCAST;
David Aherne8478e82018-04-17 17:33:13 -07003624 } else {
David Ahern360a9882018-04-18 15:39:00 -07003625 f6i->fib6_type = RTN_LOCAL;
3626 f6i->fib6_flags |= RTF_LOCAL;
David Aherne8478e82018-04-17 17:33:13 -07003627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
David Ahern360a9882018-04-18 15:39:00 -07003629 f6i->fib6_nh.nh_gw = *addr;
David Ahern93531c62018-04-17 17:33:25 -07003630 dev_hold(dev);
David Ahern360a9882018-04-18 15:39:00 -07003631 f6i->fib6_nh.nh_dev = dev;
3632 f6i->fib6_dst.addr = *addr;
3633 f6i->fib6_dst.plen = 128;
David Ahernca254492015-10-12 11:47:10 -07003634 tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL;
David Ahern360a9882018-04-18 15:39:00 -07003635 f6i->fib6_table = fib6_get_table(net, tb_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
David Ahern360a9882018-04-18 15:39:00 -07003637 return f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638}
3639
Daniel Walterc3968a82011-04-13 21:10:57 +00003640/* remove deleted ip from prefsrc entries */
3641struct arg_dev_net_ip {
3642 struct net_device *dev;
3643 struct net *net;
3644 struct in6_addr *addr;
3645};
3646
David Ahern8d1c8022018-04-17 17:33:26 -07003647static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
Daniel Walterc3968a82011-04-13 21:10:57 +00003648{
3649 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3650 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3651 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3652
David Ahern5e670d82018-04-17 17:33:14 -07003653 if (((void *)rt->fib6_nh.nh_dev == dev || !dev) &&
David Ahern421842e2018-04-17 17:33:18 -07003654 rt != net->ipv6.fib6_null_entry &&
David Ahern93c2fb22018-04-18 15:38:59 -07003655 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
Wei Wang60006a42017-10-06 12:05:58 -07003656 spin_lock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00003657 /* remove prefsrc entry */
David Ahern93c2fb22018-04-18 15:38:59 -07003658 rt->fib6_prefsrc.plen = 0;
Wei Wang60006a42017-10-06 12:05:58 -07003659 /* need to update cache as well */
3660 rt6_exceptions_remove_prefsrc(rt);
3661 spin_unlock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00003662 }
3663 return 0;
3664}
3665
3666void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
3667{
3668 struct net *net = dev_net(ifp->idev->dev);
3669 struct arg_dev_net_ip adni = {
3670 .dev = ifp->idev->dev,
3671 .net = net,
3672 .addr = &ifp->addr,
3673 };
Li RongQing0c3584d2013-12-27 16:32:38 +08003674 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00003675}
3676
Duan Jiongbe7a0102014-05-15 15:56:14 +08003677#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
Duan Jiongbe7a0102014-05-15 15:56:14 +08003678
3679/* Remove routers and update dst entries when gateway turn into host. */
David Ahern8d1c8022018-04-17 17:33:26 -07003680static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
Duan Jiongbe7a0102014-05-15 15:56:14 +08003681{
3682 struct in6_addr *gateway = (struct in6_addr *)arg;
3683
David Ahern93c2fb22018-04-18 15:38:59 -07003684 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Ahern5e670d82018-04-17 17:33:14 -07003685 ipv6_addr_equal(gateway, &rt->fib6_nh.nh_gw)) {
Duan Jiongbe7a0102014-05-15 15:56:14 +08003686 return -1;
3687 }
Wei Wangb16cb452017-10-06 12:06:00 -07003688
3689 /* Further clean up cached routes in exception table.
3690 * This is needed because cached route may have a different
3691 * gateway than its 'parent' in the case of an ip redirect.
3692 */
3693 rt6_exceptions_clean_tohost(rt, gateway);
3694
Duan Jiongbe7a0102014-05-15 15:56:14 +08003695 return 0;
3696}
3697
3698void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
3699{
3700 fib6_clean_all(net, fib6_clean_tohost, gateway);
3701}
3702
Ido Schimmel2127d952018-01-07 12:45:03 +02003703struct arg_netdev_event {
3704 const struct net_device *dev;
Ido Schimmel4c981e22018-01-07 12:45:04 +02003705 union {
3706 unsigned int nh_flags;
3707 unsigned long event;
3708 };
Ido Schimmel2127d952018-01-07 12:45:03 +02003709};
3710
David Ahern8d1c8022018-04-17 17:33:26 -07003711static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003712{
David Ahern8d1c8022018-04-17 17:33:26 -07003713 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003714 struct fib6_node *fn;
3715
David Ahern93c2fb22018-04-18 15:38:59 -07003716 fn = rcu_dereference_protected(rt->fib6_node,
3717 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003718 iter = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -07003719 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003720 while (iter) {
David Ahern93c2fb22018-04-18 15:38:59 -07003721 if (iter->fib6_metric == rt->fib6_metric &&
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003722 rt6_qualify_for_ecmp(iter))
3723 return iter;
3724 iter = rcu_dereference_protected(iter->rt6_next,
David Ahern93c2fb22018-04-18 15:38:59 -07003725 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003726 }
3727
3728 return NULL;
3729}
3730
David Ahern8d1c8022018-04-17 17:33:26 -07003731static bool rt6_is_dead(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003732{
David Ahern5e670d82018-04-17 17:33:14 -07003733 if (rt->fib6_nh.nh_flags & RTNH_F_DEAD ||
3734 (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN &&
David Aherndcd1f572018-04-18 15:39:05 -07003735 fib6_ignore_linkdown(rt)))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003736 return true;
3737
3738 return false;
3739}
3740
David Ahern8d1c8022018-04-17 17:33:26 -07003741static int rt6_multipath_total_weight(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003742{
David Ahern8d1c8022018-04-17 17:33:26 -07003743 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003744 int total = 0;
3745
3746 if (!rt6_is_dead(rt))
David Ahern5e670d82018-04-17 17:33:14 -07003747 total += rt->fib6_nh.nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003748
David Ahern93c2fb22018-04-18 15:38:59 -07003749 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003750 if (!rt6_is_dead(iter))
David Ahern5e670d82018-04-17 17:33:14 -07003751 total += iter->fib6_nh.nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003752 }
3753
3754 return total;
3755}
3756
David Ahern8d1c8022018-04-17 17:33:26 -07003757static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003758{
3759 int upper_bound = -1;
3760
3761 if (!rt6_is_dead(rt)) {
David Ahern5e670d82018-04-17 17:33:14 -07003762 *weight += rt->fib6_nh.nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003763 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
3764 total) - 1;
3765 }
David Ahern5e670d82018-04-17 17:33:14 -07003766 atomic_set(&rt->fib6_nh.nh_upper_bound, upper_bound);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003767}
3768
David Ahern8d1c8022018-04-17 17:33:26 -07003769static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003770{
David Ahern8d1c8022018-04-17 17:33:26 -07003771 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003772 int weight = 0;
3773
3774 rt6_upper_bound_set(rt, &weight, total);
3775
David Ahern93c2fb22018-04-18 15:38:59 -07003776 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003777 rt6_upper_bound_set(iter, &weight, total);
3778}
3779
David Ahern8d1c8022018-04-17 17:33:26 -07003780void rt6_multipath_rebalance(struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003781{
David Ahern8d1c8022018-04-17 17:33:26 -07003782 struct fib6_info *first;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003783 int total;
3784
3785 /* In case the entire multipath route was marked for flushing,
3786 * then there is no need to rebalance upon the removal of every
3787 * sibling route.
3788 */
David Ahern93c2fb22018-04-18 15:38:59 -07003789 if (!rt->fib6_nsiblings || rt->should_flush)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003790 return;
3791
3792 /* During lookup routes are evaluated in order, so we need to
3793 * make sure upper bounds are assigned from the first sibling
3794 * onwards.
3795 */
3796 first = rt6_multipath_first_sibling(rt);
3797 if (WARN_ON_ONCE(!first))
3798 return;
3799
3800 total = rt6_multipath_total_weight(first);
3801 rt6_multipath_upper_bound_set(first, total);
3802}
3803
David Ahern8d1c8022018-04-17 17:33:26 -07003804static int fib6_ifup(struct fib6_info *rt, void *p_arg)
Ido Schimmel2127d952018-01-07 12:45:03 +02003805{
3806 const struct arg_netdev_event *arg = p_arg;
David Ahern7aef6852018-04-17 17:33:10 -07003807 struct net *net = dev_net(arg->dev);
Ido Schimmel2127d952018-01-07 12:45:03 +02003808
David Ahern421842e2018-04-17 17:33:18 -07003809 if (rt != net->ipv6.fib6_null_entry && rt->fib6_nh.nh_dev == arg->dev) {
David Ahern5e670d82018-04-17 17:33:14 -07003810 rt->fib6_nh.nh_flags &= ~arg->nh_flags;
David Ahern7aef6852018-04-17 17:33:10 -07003811 fib6_update_sernum_upto_root(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003812 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02003813 }
Ido Schimmel2127d952018-01-07 12:45:03 +02003814
3815 return 0;
3816}
3817
3818void rt6_sync_up(struct net_device *dev, unsigned int nh_flags)
3819{
3820 struct arg_netdev_event arg = {
3821 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02003822 {
3823 .nh_flags = nh_flags,
3824 },
Ido Schimmel2127d952018-01-07 12:45:03 +02003825 };
3826
3827 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
3828 arg.nh_flags |= RTNH_F_LINKDOWN;
3829
3830 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
3831}
3832
David Ahern8d1c8022018-04-17 17:33:26 -07003833static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02003834 const struct net_device *dev)
3835{
David Ahern8d1c8022018-04-17 17:33:26 -07003836 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003837
David Ahern5e670d82018-04-17 17:33:14 -07003838 if (rt->fib6_nh.nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003839 return true;
David Ahern93c2fb22018-04-18 15:38:59 -07003840 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern5e670d82018-04-17 17:33:14 -07003841 if (iter->fib6_nh.nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003842 return true;
3843
3844 return false;
3845}
3846
David Ahern8d1c8022018-04-17 17:33:26 -07003847static void rt6_multipath_flush(struct fib6_info *rt)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003848{
David Ahern8d1c8022018-04-17 17:33:26 -07003849 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003850
3851 rt->should_flush = 1;
David Ahern93c2fb22018-04-18 15:38:59 -07003852 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003853 iter->should_flush = 1;
3854}
3855
David Ahern8d1c8022018-04-17 17:33:26 -07003856static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02003857 const struct net_device *down_dev)
3858{
David Ahern8d1c8022018-04-17 17:33:26 -07003859 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003860 unsigned int dead = 0;
3861
David Ahern5e670d82018-04-17 17:33:14 -07003862 if (rt->fib6_nh.nh_dev == down_dev ||
3863 rt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003864 dead++;
David Ahern93c2fb22018-04-18 15:38:59 -07003865 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern5e670d82018-04-17 17:33:14 -07003866 if (iter->fib6_nh.nh_dev == down_dev ||
3867 iter->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003868 dead++;
3869
3870 return dead;
3871}
3872
David Ahern8d1c8022018-04-17 17:33:26 -07003873static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02003874 const struct net_device *dev,
3875 unsigned int nh_flags)
3876{
David Ahern8d1c8022018-04-17 17:33:26 -07003877 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003878
David Ahern5e670d82018-04-17 17:33:14 -07003879 if (rt->fib6_nh.nh_dev == dev)
3880 rt->fib6_nh.nh_flags |= nh_flags;
David Ahern93c2fb22018-04-18 15:38:59 -07003881 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern5e670d82018-04-17 17:33:14 -07003882 if (iter->fib6_nh.nh_dev == dev)
3883 iter->fib6_nh.nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003884}
3885
David Aherna1a22c12017-01-18 07:40:36 -08003886/* called with write lock held for table with rt */
David Ahern8d1c8022018-04-17 17:33:26 -07003887static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888{
Ido Schimmel4c981e22018-01-07 12:45:04 +02003889 const struct arg_netdev_event *arg = p_arg;
3890 const struct net_device *dev = arg->dev;
David Ahern7aef6852018-04-17 17:33:10 -07003891 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003892
David Ahern421842e2018-04-17 17:33:18 -07003893 if (rt == net->ipv6.fib6_null_entry)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003894 return 0;
3895
3896 switch (arg->event) {
3897 case NETDEV_UNREGISTER:
David Ahern5e670d82018-04-17 17:33:14 -07003898 return rt->fib6_nh.nh_dev == dev ? -1 : 0;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003899 case NETDEV_DOWN:
Ido Schimmel1de178e2018-01-07 12:45:15 +02003900 if (rt->should_flush)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003901 return -1;
David Ahern93c2fb22018-04-18 15:38:59 -07003902 if (!rt->fib6_nsiblings)
David Ahern5e670d82018-04-17 17:33:14 -07003903 return rt->fib6_nh.nh_dev == dev ? -1 : 0;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003904 if (rt6_multipath_uses_dev(rt, dev)) {
3905 unsigned int count;
3906
3907 count = rt6_multipath_dead_count(rt, dev);
David Ahern93c2fb22018-04-18 15:38:59 -07003908 if (rt->fib6_nsiblings + 1 == count) {
Ido Schimmel1de178e2018-01-07 12:45:15 +02003909 rt6_multipath_flush(rt);
3910 return -1;
3911 }
3912 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
3913 RTNH_F_LINKDOWN);
David Ahern7aef6852018-04-17 17:33:10 -07003914 fib6_update_sernum(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003915 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02003916 }
3917 return -2;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003918 case NETDEV_CHANGE:
David Ahern5e670d82018-04-17 17:33:14 -07003919 if (rt->fib6_nh.nh_dev != dev ||
David Ahern93c2fb22018-04-18 15:38:59 -07003920 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003921 break;
David Ahern5e670d82018-04-17 17:33:14 -07003922 rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003923 rt6_multipath_rebalance(rt);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003924 break;
Ido Schimmel2b241362018-01-07 12:45:02 +02003925 }
David S. Millerc159d302011-12-26 15:24:36 -05003926
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 return 0;
3928}
3929
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003930void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931{
Ido Schimmel4c981e22018-01-07 12:45:04 +02003932 struct arg_netdev_event arg = {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003933 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02003934 {
3935 .event = event,
3936 },
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003937 };
3938
Ido Schimmel4c981e22018-01-07 12:45:04 +02003939 fib6_clean_all(dev_net(dev), fib6_ifdown, &arg);
3940}
3941
3942void rt6_disable_ip(struct net_device *dev, unsigned long event)
3943{
3944 rt6_sync_down_dev(dev, event);
3945 rt6_uncached_list_flush_dev(dev_net(dev), dev);
3946 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947}
3948
Eric Dumazet95c96172012-04-15 05:58:06 +00003949struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00003951 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952};
3953
David Ahern8d1c8022018-04-17 17:33:26 -07003954static int rt6_mtu_change_route(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955{
3956 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
3957 struct inet6_dev *idev;
3958
3959 /* In IPv6 pmtu discovery is not optional,
3960 so that RTAX_MTU lock cannot disable it.
3961 We still use this lock to block changes
3962 caused by addrconf/ndisc.
3963 */
3964
3965 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05003966 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 return 0;
3968
3969 /* For administrative MTU increase, there is no way to discover
3970 IPv6 PMTU increase, so PMTU increase should be updated here.
3971 Since RFC 1981 doesn't include administrative MTU increase
3972 update PMTU increase is a MUST. (i.e. jumbo frame)
3973 */
David Ahern5e670d82018-04-17 17:33:14 -07003974 if (rt->fib6_nh.nh_dev == arg->dev &&
David Ahernd4ead6b2018-04-17 17:33:16 -07003975 !fib6_metric_locked(rt, RTAX_MTU)) {
3976 u32 mtu = rt->fib6_pmtu;
3977
3978 if (mtu >= arg->mtu ||
3979 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
3980 fib6_metric_set(rt, RTAX_MTU, arg->mtu);
3981
Wei Wangf5bbe7e2017-10-06 12:05:59 -07003982 spin_lock_bh(&rt6_exception_lock);
Stefano Brivioe9fa1492018-03-06 11:10:19 +01003983 rt6_exceptions_update_pmtu(idev, rt, arg->mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07003984 spin_unlock_bh(&rt6_exception_lock);
Simon Arlott566cfd82007-07-26 00:09:55 -07003985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 return 0;
3987}
3988
Eric Dumazet95c96172012-04-15 05:58:06 +00003989void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990{
Thomas Grafc71099a2006-08-04 23:20:06 -07003991 struct rt6_mtu_change_arg arg = {
3992 .dev = dev,
3993 .mtu = mtu,
3994 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Li RongQing0c3584d2013-12-27 16:32:38 +08003996 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997}
3998
Patrick McHardyef7c79e2007-06-05 12:38:30 -07003999static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07004000 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07004001 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07004002 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004003 [RTA_PRIORITY] = { .type = NLA_U32 },
4004 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004005 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004006 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004007 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4008 [RTA_ENCAP] = { .type = NLA_NESTED },
Xin Long32bc2012015-12-16 17:50:11 +08004009 [RTA_EXPIRES] = { .type = NLA_U32 },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004010 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +08004011 [RTA_MARK] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004012};
4013
4014static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
David Ahern333c4302017-05-21 10:12:04 -06004015 struct fib6_config *cfg,
4016 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
Thomas Graf86872cb2006-08-22 00:01:08 -07004018 struct rtmsg *rtm;
4019 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004020 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07004021 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Johannes Bergfceb6432017-04-12 14:34:07 +02004023 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
4024 NULL);
Thomas Graf86872cb2006-08-22 00:01:08 -07004025 if (err < 0)
4026 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
Thomas Graf86872cb2006-08-22 00:01:08 -07004028 err = -EINVAL;
4029 rtm = nlmsg_data(nlh);
4030 memset(cfg, 0, sizeof(*cfg));
4031
4032 cfg->fc_table = rtm->rtm_table;
4033 cfg->fc_dst_len = rtm->rtm_dst_len;
4034 cfg->fc_src_len = rtm->rtm_src_len;
4035 cfg->fc_flags = RTF_UP;
4036 cfg->fc_protocol = rtm->rtm_protocol;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004037 cfg->fc_type = rtm->rtm_type;
Thomas Graf86872cb2006-08-22 00:01:08 -07004038
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004039 if (rtm->rtm_type == RTN_UNREACHABLE ||
4040 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00004041 rtm->rtm_type == RTN_PROHIBIT ||
4042 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07004043 cfg->fc_flags |= RTF_REJECT;
4044
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00004045 if (rtm->rtm_type == RTN_LOCAL)
4046 cfg->fc_flags |= RTF_LOCAL;
4047
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07004048 if (rtm->rtm_flags & RTM_F_CLONED)
4049 cfg->fc_flags |= RTF_CACHE;
4050
David Ahernfc1e64e2018-01-25 16:55:09 -08004051 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4052
Eric W. Biederman15e47302012-09-07 20:12:54 +00004053 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
Thomas Graf86872cb2006-08-22 00:01:08 -07004054 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004055 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07004056
4057 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004058 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07004059 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004061
4062 if (tb[RTA_DST]) {
4063 int plen = (rtm->rtm_dst_len + 7) >> 3;
4064
4065 if (nla_len(tb[RTA_DST]) < plen)
4066 goto errout;
4067
4068 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004070
4071 if (tb[RTA_SRC]) {
4072 int plen = (rtm->rtm_src_len + 7) >> 3;
4073
4074 if (nla_len(tb[RTA_SRC]) < plen)
4075 goto errout;
4076
4077 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004079
Daniel Walterc3968a82011-04-13 21:10:57 +00004080 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02004081 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00004082
Thomas Graf86872cb2006-08-22 00:01:08 -07004083 if (tb[RTA_OIF])
4084 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4085
4086 if (tb[RTA_PRIORITY])
4087 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4088
4089 if (tb[RTA_METRICS]) {
4090 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4091 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004093
4094 if (tb[RTA_TABLE])
4095 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4096
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004097 if (tb[RTA_MULTIPATH]) {
4098 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4099 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
David Ahern9ed59592017-01-17 14:57:36 -08004100
4101 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
David Ahernc255bd62017-05-27 16:19:27 -06004102 cfg->fc_mp_len, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004103 if (err < 0)
4104 goto errout;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004105 }
4106
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004107 if (tb[RTA_PREF]) {
4108 pref = nla_get_u8(tb[RTA_PREF]);
4109 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4110 pref != ICMPV6_ROUTER_PREF_HIGH)
4111 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4112 cfg->fc_flags |= RTF_PREF(pref);
4113 }
4114
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004115 if (tb[RTA_ENCAP])
4116 cfg->fc_encap = tb[RTA_ENCAP];
4117
David Ahern9ed59592017-01-17 14:57:36 -08004118 if (tb[RTA_ENCAP_TYPE]) {
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004119 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
4120
David Ahernc255bd62017-05-27 16:19:27 -06004121 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004122 if (err < 0)
4123 goto errout;
4124 }
4125
Xin Long32bc2012015-12-16 17:50:11 +08004126 if (tb[RTA_EXPIRES]) {
4127 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
4128
4129 if (addrconf_finite_timeout(timeout)) {
4130 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
4131 cfg->fc_flags |= RTF_EXPIRES;
4132 }
4133 }
4134
Thomas Graf86872cb2006-08-22 00:01:08 -07004135 err = 0;
4136errout:
4137 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138}
4139
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004140struct rt6_nh {
David Ahern8d1c8022018-04-17 17:33:26 -07004141 struct fib6_info *fib6_info;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004142 struct fib6_config r_cfg;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004143 struct list_head next;
4144};
4145
4146static void ip6_print_replace_route_err(struct list_head *rt6_nh_list)
4147{
4148 struct rt6_nh *nh;
4149
4150 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern7d4d5062017-02-02 12:37:12 -08004151 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 -07004152 &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway,
4153 nh->r_cfg.fc_ifindex);
4154 }
4155}
4156
David Ahernd4ead6b2018-04-17 17:33:16 -07004157static int ip6_route_info_append(struct net *net,
4158 struct list_head *rt6_nh_list,
David Ahern8d1c8022018-04-17 17:33:26 -07004159 struct fib6_info *rt,
4160 struct fib6_config *r_cfg)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004161{
4162 struct rt6_nh *nh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004163 int err = -EEXIST;
4164
4165 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004166 /* check if fib6_info already exists */
4167 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004168 return err;
4169 }
4170
4171 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
4172 if (!nh)
4173 return -ENOMEM;
David Ahern8d1c8022018-04-17 17:33:26 -07004174 nh->fib6_info = rt;
David Ahernd4ead6b2018-04-17 17:33:16 -07004175 err = ip6_convert_metrics(net, rt, r_cfg);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004176 if (err) {
4177 kfree(nh);
4178 return err;
4179 }
4180 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
4181 list_add_tail(&nh->next, rt6_nh_list);
4182
4183 return 0;
4184}
4185
David Ahern8d1c8022018-04-17 17:33:26 -07004186static void ip6_route_mpath_notify(struct fib6_info *rt,
4187 struct fib6_info *rt_last,
David Ahern3b1137f2017-02-02 12:37:10 -08004188 struct nl_info *info,
4189 __u16 nlflags)
4190{
4191 /* if this is an APPEND route, then rt points to the first route
4192 * inserted and rt_last points to last route inserted. Userspace
4193 * wants a consistent dump of the route which starts at the first
4194 * nexthop. Since sibling routes are always added at the end of
4195 * the list, find the first sibling of the last route appended
4196 */
David Ahern93c2fb22018-04-18 15:38:59 -07004197 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
4198 rt = list_first_entry(&rt_last->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -07004199 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -07004200 fib6_siblings);
David Ahern3b1137f2017-02-02 12:37:10 -08004201 }
4202
4203 if (rt)
4204 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
4205}
4206
David Ahern333c4302017-05-21 10:12:04 -06004207static int ip6_route_multipath_add(struct fib6_config *cfg,
4208 struct netlink_ext_ack *extack)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004209{
David Ahern8d1c8022018-04-17 17:33:26 -07004210 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
David Ahern3b1137f2017-02-02 12:37:10 -08004211 struct nl_info *info = &cfg->fc_nlinfo;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004212 struct fib6_config r_cfg;
4213 struct rtnexthop *rtnh;
David Ahern8d1c8022018-04-17 17:33:26 -07004214 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004215 struct rt6_nh *err_nh;
4216 struct rt6_nh *nh, *nh_safe;
David Ahern3b1137f2017-02-02 12:37:10 -08004217 __u16 nlflags;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004218 int remaining;
4219 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004220 int err = 1;
4221 int nhn = 0;
4222 int replace = (cfg->fc_nlinfo.nlh &&
4223 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
4224 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004225
David Ahern3b1137f2017-02-02 12:37:10 -08004226 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
4227 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
4228 nlflags |= NLM_F_APPEND;
4229
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02004230 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004231 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004232
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004233 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
David Ahern8d1c8022018-04-17 17:33:26 -07004234 * fib6_info structs per nexthop
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004235 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004236 while (rtnh_ok(rtnh, remaining)) {
4237 memcpy(&r_cfg, cfg, sizeof(*cfg));
4238 if (rtnh->rtnh_ifindex)
4239 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
4240
4241 attrlen = rtnh_attrlen(rtnh);
4242 if (attrlen > 0) {
4243 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4244
4245 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
4246 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004247 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004248 r_cfg.fc_flags |= RTF_GATEWAY;
4249 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004250 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
4251 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
4252 if (nla)
4253 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004254 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004255
David Ahern68e2ffd2018-03-20 10:06:59 -07004256 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
David Ahernacb54e32018-04-17 17:33:22 -07004257 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07004258 if (IS_ERR(rt)) {
4259 err = PTR_ERR(rt);
4260 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004261 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07004262 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004263
David Ahern5e670d82018-04-17 17:33:14 -07004264 rt->fib6_nh.nh_weight = rtnh->rtnh_hops + 1;
Ido Schimmel398958a2018-01-09 16:40:28 +02004265
David Ahernd4ead6b2018-04-17 17:33:16 -07004266 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4267 rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004268 if (err) {
David Ahern93531c62018-04-17 17:33:25 -07004269 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004270 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004271 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004272
4273 rtnh = rtnh_next(rtnh, &remaining);
4274 }
4275
David Ahern3b1137f2017-02-02 12:37:10 -08004276 /* for add and replace send one notification with all nexthops.
4277 * Skip the notification in fib6_add_rt2node and send one with
4278 * the full route when done
4279 */
4280 info->skip_notify = 1;
4281
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004282 err_nh = NULL;
4283 list_for_each_entry(nh, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004284 rt_last = nh->fib6_info;
4285 err = __ip6_ins_rt(nh->fib6_info, info, extack);
4286 fib6_info_release(nh->fib6_info);
David Ahern93531c62018-04-17 17:33:25 -07004287
David Ahern3b1137f2017-02-02 12:37:10 -08004288 /* save reference to first route for notification */
4289 if (!rt_notif && !err)
David Ahern8d1c8022018-04-17 17:33:26 -07004290 rt_notif = nh->fib6_info;
David Ahern3b1137f2017-02-02 12:37:10 -08004291
David Ahern8d1c8022018-04-17 17:33:26 -07004292 /* nh->fib6_info is used or freed at this point, reset to NULL*/
4293 nh->fib6_info = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004294 if (err) {
4295 if (replace && nhn)
4296 ip6_print_replace_route_err(&rt6_nh_list);
4297 err_nh = nh;
4298 goto add_errout;
4299 }
4300
Nicolas Dichtel1a724182012-11-01 22:58:22 +00004301 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02004302 * these flags after the first nexthop: if there is a collision,
4303 * we have already failed to add the first nexthop:
4304 * fib6_add_rt2node() has rejected it; when replacing, old
4305 * nexthops have been replaced by first new, the rest should
4306 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00004307 */
Michal Kubeček27596472015-05-18 20:54:00 +02004308 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
4309 NLM_F_REPLACE);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004310 nhn++;
4311 }
4312
David Ahern3b1137f2017-02-02 12:37:10 -08004313 /* success ... tell user about new route */
4314 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004315 goto cleanup;
4316
4317add_errout:
David Ahern3b1137f2017-02-02 12:37:10 -08004318 /* send notification for routes that were added so that
4319 * the delete notifications sent by ip6_route_del are
4320 * coherent
4321 */
4322 if (rt_notif)
4323 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
4324
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004325 /* Delete routes that were already added */
4326 list_for_each_entry(nh, &rt6_nh_list, next) {
4327 if (err_nh == nh)
4328 break;
David Ahern333c4302017-05-21 10:12:04 -06004329 ip6_route_del(&nh->r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004330 }
4331
4332cleanup:
4333 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004334 if (nh->fib6_info)
4335 fib6_info_release(nh->fib6_info);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004336 list_del(&nh->next);
4337 kfree(nh);
4338 }
4339
4340 return err;
4341}
4342
David Ahern333c4302017-05-21 10:12:04 -06004343static int ip6_route_multipath_del(struct fib6_config *cfg,
4344 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004345{
4346 struct fib6_config r_cfg;
4347 struct rtnexthop *rtnh;
4348 int remaining;
4349 int attrlen;
4350 int err = 1, last_err = 0;
4351
4352 remaining = cfg->fc_mp_len;
4353 rtnh = (struct rtnexthop *)cfg->fc_mp;
4354
4355 /* Parse a Multipath Entry */
4356 while (rtnh_ok(rtnh, remaining)) {
4357 memcpy(&r_cfg, cfg, sizeof(*cfg));
4358 if (rtnh->rtnh_ifindex)
4359 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
4360
4361 attrlen = rtnh_attrlen(rtnh);
4362 if (attrlen > 0) {
4363 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4364
4365 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
4366 if (nla) {
4367 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
4368 r_cfg.fc_flags |= RTF_GATEWAY;
4369 }
4370 }
David Ahern333c4302017-05-21 10:12:04 -06004371 err = ip6_route_del(&r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004372 if (err)
4373 last_err = err;
4374
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004375 rtnh = rtnh_next(rtnh, &remaining);
4376 }
4377
4378 return last_err;
4379}
4380
David Ahernc21ef3e2017-04-16 09:48:24 -07004381static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4382 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383{
Thomas Graf86872cb2006-08-22 00:01:08 -07004384 struct fib6_config cfg;
4385 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
David Ahern333c4302017-05-21 10:12:04 -06004387 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004388 if (err < 0)
4389 return err;
4390
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004391 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06004392 return ip6_route_multipath_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08004393 else {
4394 cfg.fc_delete_all_nh = 1;
David Ahern333c4302017-05-21 10:12:04 -06004395 return ip6_route_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08004396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397}
4398
David Ahernc21ef3e2017-04-16 09:48:24 -07004399static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4400 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401{
Thomas Graf86872cb2006-08-22 00:01:08 -07004402 struct fib6_config cfg;
4403 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
David Ahern333c4302017-05-21 10:12:04 -06004405 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004406 if (err < 0)
4407 return err;
4408
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004409 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06004410 return ip6_route_multipath_add(&cfg, extack);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004411 else
David Ahernacb54e32018-04-17 17:33:22 -07004412 return ip6_route_add(&cfg, GFP_KERNEL, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413}
4414
David Ahern8d1c8022018-04-17 17:33:26 -07004415static size_t rt6_nlmsg_size(struct fib6_info *rt)
Thomas Graf339bf982006-11-10 14:10:15 -08004416{
David Ahernbeb1afac52017-02-02 12:37:09 -08004417 int nexthop_len = 0;
4418
David Ahern93c2fb22018-04-18 15:38:59 -07004419 if (rt->fib6_nsiblings) {
David Ahernbeb1afac52017-02-02 12:37:09 -08004420 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
4421 + NLA_ALIGN(sizeof(struct rtnexthop))
4422 + nla_total_size(16) /* RTA_GATEWAY */
David Ahern5e670d82018-04-17 17:33:14 -07004423 + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate);
David Ahernbeb1afac52017-02-02 12:37:09 -08004424
David Ahern93c2fb22018-04-18 15:38:59 -07004425 nexthop_len *= rt->fib6_nsiblings;
David Ahernbeb1afac52017-02-02 12:37:09 -08004426 }
4427
Thomas Graf339bf982006-11-10 14:10:15 -08004428 return NLMSG_ALIGN(sizeof(struct rtmsg))
4429 + nla_total_size(16) /* RTA_SRC */
4430 + nla_total_size(16) /* RTA_DST */
4431 + nla_total_size(16) /* RTA_GATEWAY */
4432 + nla_total_size(16) /* RTA_PREFSRC */
4433 + nla_total_size(4) /* RTA_TABLE */
4434 + nla_total_size(4) /* RTA_IIF */
4435 + nla_total_size(4) /* RTA_OIF */
4436 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08004437 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01004438 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004439 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004440 + nla_total_size(1) /* RTA_PREF */
David Ahern5e670d82018-04-17 17:33:14 -07004441 + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate)
David Ahernbeb1afac52017-02-02 12:37:09 -08004442 + nexthop_len;
4443}
4444
David Ahern8d1c8022018-04-17 17:33:26 -07004445static int rt6_nexthop_info(struct sk_buff *skb, struct fib6_info *rt,
David Ahern5be083c2017-03-06 15:57:31 -08004446 unsigned int *flags, bool skip_oif)
David Ahernbeb1afac52017-02-02 12:37:09 -08004447{
David Ahern5e670d82018-04-17 17:33:14 -07004448 if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmelf9d882e2018-01-07 12:45:10 +02004449 *flags |= RTNH_F_DEAD;
4450
David Ahern5e670d82018-04-17 17:33:14 -07004451 if (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN) {
David Ahernbeb1afac52017-02-02 12:37:09 -08004452 *flags |= RTNH_F_LINKDOWN;
David Aherndcd1f572018-04-18 15:39:05 -07004453
4454 rcu_read_lock();
4455 if (fib6_ignore_linkdown(rt))
David Ahernbeb1afac52017-02-02 12:37:09 -08004456 *flags |= RTNH_F_DEAD;
David Aherndcd1f572018-04-18 15:39:05 -07004457 rcu_read_unlock();
David Ahernbeb1afac52017-02-02 12:37:09 -08004458 }
4459
David Ahern93c2fb22018-04-18 15:38:59 -07004460 if (rt->fib6_flags & RTF_GATEWAY) {
David Ahern5e670d82018-04-17 17:33:14 -07004461 if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->fib6_nh.nh_gw) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004462 goto nla_put_failure;
4463 }
4464
David Ahern5e670d82018-04-17 17:33:14 -07004465 *flags |= (rt->fib6_nh.nh_flags & RTNH_F_ONLINK);
4466 if (rt->fib6_nh.nh_flags & RTNH_F_OFFLOAD)
Ido Schimmel61e4d012017-08-03 13:28:20 +02004467 *flags |= RTNH_F_OFFLOAD;
4468
David Ahern5be083c2017-03-06 15:57:31 -08004469 /* not needed for multipath encoding b/c it has a rtnexthop struct */
David Ahern5e670d82018-04-17 17:33:14 -07004470 if (!skip_oif && rt->fib6_nh.nh_dev &&
4471 nla_put_u32(skb, RTA_OIF, rt->fib6_nh.nh_dev->ifindex))
David Ahernbeb1afac52017-02-02 12:37:09 -08004472 goto nla_put_failure;
4473
David Ahern5e670d82018-04-17 17:33:14 -07004474 if (rt->fib6_nh.nh_lwtstate &&
4475 lwtunnel_fill_encap(skb, rt->fib6_nh.nh_lwtstate) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004476 goto nla_put_failure;
4477
4478 return 0;
4479
4480nla_put_failure:
4481 return -EMSGSIZE;
4482}
4483
David Ahern5be083c2017-03-06 15:57:31 -08004484/* add multipath next hop */
David Ahern8d1c8022018-04-17 17:33:26 -07004485static int rt6_add_nexthop(struct sk_buff *skb, struct fib6_info *rt)
David Ahernbeb1afac52017-02-02 12:37:09 -08004486{
David Ahern5e670d82018-04-17 17:33:14 -07004487 const struct net_device *dev = rt->fib6_nh.nh_dev;
David Ahernbeb1afac52017-02-02 12:37:09 -08004488 struct rtnexthop *rtnh;
4489 unsigned int flags = 0;
4490
4491 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
4492 if (!rtnh)
4493 goto nla_put_failure;
4494
David Ahern5e670d82018-04-17 17:33:14 -07004495 rtnh->rtnh_hops = rt->fib6_nh.nh_weight - 1;
4496 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
David Ahernbeb1afac52017-02-02 12:37:09 -08004497
David Ahern5be083c2017-03-06 15:57:31 -08004498 if (rt6_nexthop_info(skb, rt, &flags, true) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004499 goto nla_put_failure;
4500
4501 rtnh->rtnh_flags = flags;
4502
4503 /* length of rtnetlink header + attributes */
4504 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
4505
4506 return 0;
4507
4508nla_put_failure:
4509 return -EMSGSIZE;
Thomas Graf339bf982006-11-10 14:10:15 -08004510}
4511
David Ahernd4ead6b2018-04-17 17:33:16 -07004512static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -07004513 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -07004514 struct in6_addr *dest, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004515 int iif, int type, u32 portid, u32 seq,
David Ahernf8cfe2c2017-01-17 15:51:08 -08004516 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517{
4518 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004519 struct nlmsghdr *nlh;
David Ahernd4ead6b2018-04-17 17:33:16 -07004520 long expires = 0;
4521 u32 *pmetrics;
Patrick McHardy9e762a42006-08-10 23:09:48 -07004522 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
Eric W. Biederman15e47302012-09-07 20:12:54 +00004524 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05004525 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004526 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004527
4528 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 rtm->rtm_family = AF_INET6;
David Ahern93c2fb22018-04-18 15:38:59 -07004530 rtm->rtm_dst_len = rt->fib6_dst.plen;
4531 rtm->rtm_src_len = rt->fib6_src.plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 rtm->rtm_tos = 0;
David Ahern93c2fb22018-04-18 15:38:59 -07004533 if (rt->fib6_table)
4534 table = rt->fib6_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07004535 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07004536 table = RT6_TABLE_UNSPEC;
4537 rtm->rtm_table = table;
David S. Millerc78679e2012-04-01 20:27:33 -04004538 if (nla_put_u32(skb, RTA_TABLE, table))
4539 goto nla_put_failure;
David Aherne8478e82018-04-17 17:33:13 -07004540
4541 rtm->rtm_type = rt->fib6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 rtm->rtm_flags = 0;
4543 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
David Ahern93c2fb22018-04-18 15:38:59 -07004544 rtm->rtm_protocol = rt->fib6_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545
David Ahern93c2fb22018-04-18 15:38:59 -07004546 if (rt->fib6_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 rtm->rtm_flags |= RTM_F_CLONED;
4548
David Ahernd4ead6b2018-04-17 17:33:16 -07004549 if (dest) {
4550 if (nla_put_in6_addr(skb, RTA_DST, dest))
David S. Millerc78679e2012-04-01 20:27:33 -04004551 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004552 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 } else if (rtm->rtm_dst_len)
David Ahern93c2fb22018-04-18 15:38:59 -07004554 if (nla_put_in6_addr(skb, RTA_DST, &rt->fib6_dst.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04004555 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556#ifdef CONFIG_IPV6_SUBTREES
4557 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02004558 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04004559 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004560 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04004561 } else if (rtm->rtm_src_len &&
David Ahern93c2fb22018-04-18 15:38:59 -07004562 nla_put_in6_addr(skb, RTA_SRC, &rt->fib6_src.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04004563 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004565 if (iif) {
4566#ifdef CONFIG_IPV6_MROUTE
David Ahern93c2fb22018-04-18 15:38:59 -07004567 if (ipv6_addr_is_multicast(&rt->fib6_dst.addr)) {
David Ahernfd61c6b2017-01-17 15:51:07 -08004568 int err = ip6mr_get_route(net, skb, rtm, portid);
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02004569
David Ahernfd61c6b2017-01-17 15:51:07 -08004570 if (err == 0)
4571 return 0;
4572 if (err < 0)
4573 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004574 } else
4575#endif
David S. Millerc78679e2012-04-01 20:27:33 -04004576 if (nla_put_u32(skb, RTA_IIF, iif))
4577 goto nla_put_failure;
David Ahernd4ead6b2018-04-17 17:33:16 -07004578 } else if (dest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 struct in6_addr saddr_buf;
David Ahernd4ead6b2018-04-17 17:33:16 -07004580 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02004581 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04004582 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07004584
David Ahern93c2fb22018-04-18 15:38:59 -07004585 if (rt->fib6_prefsrc.plen) {
Daniel Walterc3968a82011-04-13 21:10:57 +00004586 struct in6_addr saddr_buf;
David Ahern93c2fb22018-04-18 15:38:59 -07004587 saddr_buf = rt->fib6_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02004588 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04004589 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00004590 }
4591
David Ahernd4ead6b2018-04-17 17:33:16 -07004592 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
4593 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07004594 goto nla_put_failure;
4595
David Ahern93c2fb22018-04-18 15:38:59 -07004596 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
David S. Millerc78679e2012-04-01 20:27:33 -04004597 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00004598
David Ahernbeb1afac52017-02-02 12:37:09 -08004599 /* For multipath routes, walk the siblings list and add
4600 * each as a nexthop within RTA_MULTIPATH.
4601 */
David Ahern93c2fb22018-04-18 15:38:59 -07004602 if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07004603 struct fib6_info *sibling, *next_sibling;
David Ahernbeb1afac52017-02-02 12:37:09 -08004604 struct nlattr *mp;
4605
4606 mp = nla_nest_start(skb, RTA_MULTIPATH);
4607 if (!mp)
4608 goto nla_put_failure;
4609
4610 if (rt6_add_nexthop(skb, rt) < 0)
4611 goto nla_put_failure;
4612
4613 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07004614 &rt->fib6_siblings, fib6_siblings) {
David Ahernbeb1afac52017-02-02 12:37:09 -08004615 if (rt6_add_nexthop(skb, sibling) < 0)
4616 goto nla_put_failure;
4617 }
4618
4619 nla_nest_end(skb, mp);
4620 } else {
David Ahern5be083c2017-03-06 15:57:31 -08004621 if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004622 goto nla_put_failure;
4623 }
4624
David Ahern93c2fb22018-04-18 15:38:59 -07004625 if (rt->fib6_flags & RTF_EXPIRES) {
David Ahern14895682018-04-17 17:33:17 -07004626 expires = dst ? dst->expires : rt->expires;
4627 expires -= jiffies;
4628 }
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07004629
David Ahernd4ead6b2018-04-17 17:33:16 -07004630 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08004631 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
David Ahern93c2fb22018-04-18 15:38:59 -07004633 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->fib6_flags)))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004634 goto nla_put_failure;
4635
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004636
Johannes Berg053c0952015-01-16 22:09:00 +01004637 nlmsg_end(skb, nlh);
4638 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004639
4640nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004641 nlmsg_cancel(skb, nlh);
4642 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643}
4644
David Ahern8d1c8022018-04-17 17:33:26 -07004645int rt6_dump_route(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646{
4647 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
David Ahern1f17e2f2017-01-26 13:54:08 -08004648 struct net *net = arg->net;
4649
David Ahern421842e2018-04-17 17:33:18 -07004650 if (rt == net->ipv6.fib6_null_entry)
David Ahern1f17e2f2017-01-26 13:54:08 -08004651 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
Thomas Graf2d7202b2006-08-22 00:01:27 -07004653 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
4654 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
David Ahernf8cfe2c2017-01-17 15:51:08 -08004655
4656 /* user wants prefix routes only */
4657 if (rtm->rtm_flags & RTM_F_PREFIX &&
David Ahern93c2fb22018-04-18 15:38:59 -07004658 !(rt->fib6_flags & RTF_PREFIX_RT)) {
David Ahernf8cfe2c2017-01-17 15:51:08 -08004659 /* success since this is not a prefix route */
4660 return 1;
4661 }
4662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
David Ahernd4ead6b2018-04-17 17:33:16 -07004664 return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
4665 RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
4666 arg->cb->nlh->nlmsg_seq, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667}
4668
David Ahernc21ef3e2017-04-16 09:48:24 -07004669static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4670 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004672 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07004673 struct nlattr *tb[RTA_MAX+1];
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004674 int err, iif = 0, oif = 0;
4675 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07004677 struct sk_buff *skb;
4678 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05004679 struct flowi6 fl6;
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004680 bool fibmatch;
Thomas Grafab364a62006-08-22 00:01:47 -07004681
Johannes Bergfceb6432017-04-12 14:34:07 +02004682 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07004683 extack);
Thomas Grafab364a62006-08-22 00:01:47 -07004684 if (err < 0)
4685 goto errout;
4686
4687 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05004688 memset(&fl6, 0, sizeof(fl6));
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02004689 rtm = nlmsg_data(nlh);
4690 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004691 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
Thomas Grafab364a62006-08-22 00:01:47 -07004692
4693 if (tb[RTA_SRC]) {
4694 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4695 goto errout;
4696
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004697 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07004698 }
4699
4700 if (tb[RTA_DST]) {
4701 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4702 goto errout;
4703
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004704 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07004705 }
4706
4707 if (tb[RTA_IIF])
4708 iif = nla_get_u32(tb[RTA_IIF]);
4709
4710 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004711 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07004712
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07004713 if (tb[RTA_MARK])
4714 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
4715
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004716 if (tb[RTA_UID])
4717 fl6.flowi6_uid = make_kuid(current_user_ns(),
4718 nla_get_u32(tb[RTA_UID]));
4719 else
4720 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
4721
Thomas Grafab364a62006-08-22 00:01:47 -07004722 if (iif) {
4723 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004724 int flags = 0;
4725
Florian Westphal121622d2017-08-15 16:34:42 +02004726 rcu_read_lock();
4727
4728 dev = dev_get_by_index_rcu(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07004729 if (!dev) {
Florian Westphal121622d2017-08-15 16:34:42 +02004730 rcu_read_unlock();
Thomas Grafab364a62006-08-22 00:01:47 -07004731 err = -ENODEV;
4732 goto errout;
4733 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004734
4735 fl6.flowi6_iif = iif;
4736
4737 if (!ipv6_addr_any(&fl6.saddr))
4738 flags |= RT6_LOOKUP_F_HAS_SADDR;
4739
David Ahernb75cc8f2018-03-02 08:32:17 -08004740 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
Florian Westphal121622d2017-08-15 16:34:42 +02004741
4742 rcu_read_unlock();
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004743 } else {
4744 fl6.flowi6_oif = oif;
4745
Ido Schimmel58acfd72017-12-20 12:28:25 +02004746 dst = ip6_route_output(net, NULL, &fl6);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004747 }
4748
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004749
4750 rt = container_of(dst, struct rt6_info, dst);
4751 if (rt->dst.error) {
4752 err = rt->dst.error;
4753 ip6_rt_put(rt);
4754 goto errout;
Thomas Grafab364a62006-08-22 00:01:47 -07004755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
WANG Cong9d6acb32017-03-01 20:48:39 -08004757 if (rt == net->ipv6.ip6_null_entry) {
4758 err = rt->dst.error;
4759 ip6_rt_put(rt);
4760 goto errout;
4761 }
4762
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05004764 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00004765 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07004766 err = -ENOBUFS;
4767 goto errout;
4768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769
Changli Gaod8d1f302010-06-10 23:31:35 -07004770 skb_dst_set(skb, &rt->dst);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004771 if (fibmatch)
David Ahern93531c62018-04-17 17:33:25 -07004772 err = rt6_fill_node(net, skb, rt->from, NULL, NULL, NULL, iif,
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004773 RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
4774 nlh->nlmsg_seq, 0);
4775 else
David Ahern93531c62018-04-17 17:33:25 -07004776 err = rt6_fill_node(net, skb, rt->from, dst,
4777 &fl6.daddr, &fl6.saddr, iif, RTM_NEWROUTE,
David Ahernd4ead6b2018-04-17 17:33:16 -07004778 NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
4779 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07004781 kfree_skb(skb);
4782 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 }
4784
Eric W. Biederman15e47302012-09-07 20:12:54 +00004785 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07004786errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788}
4789
David Ahern8d1c8022018-04-17 17:33:26 -07004790void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07004791 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792{
4793 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08004794 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08004795 u32 seq;
4796 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08004798 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05004799 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07004800
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004801 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05004802 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07004803 goto errout;
4804
David Ahernd4ead6b2018-04-17 17:33:16 -07004805 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
4806 event, info->portid, seq, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08004807 if (err < 0) {
4808 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
4809 WARN_ON(err == -EMSGSIZE);
4810 kfree_skb(skb);
4811 goto errout;
4812 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004813 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004814 info->nlh, gfp_any());
4815 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07004816errout:
4817 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08004818 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819}
4820
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004821static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00004822 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004823{
Jiri Pirko351638e2013-05-28 01:30:21 +00004824 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004825 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004826
WANG Cong242d3a42017-05-08 10:12:13 -07004827 if (!(dev->flags & IFF_LOOPBACK))
4828 return NOTIFY_OK;
4829
4830 if (event == NETDEV_REGISTER) {
David Ahern421842e2018-04-17 17:33:18 -07004831 net->ipv6.fib6_null_entry->fib6_nh.nh_dev = dev;
Changli Gaod8d1f302010-06-10 23:31:35 -07004832 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004833 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
4834#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07004835 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004836 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07004837 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004838 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
4839#endif
WANG Cong76da0702017-06-20 11:42:27 -07004840 } else if (event == NETDEV_UNREGISTER &&
4841 dev->reg_state != NETREG_UNREGISTERED) {
4842 /* NETDEV_UNREGISTER could be fired for multiple times by
4843 * netdev_wait_allrefs(). Make sure we only call this once.
4844 */
Eric Dumazet12d94a82017-08-15 04:09:51 -07004845 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07004846#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric Dumazet12d94a82017-08-15 04:09:51 -07004847 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
4848 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07004849#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004850 }
4851
4852 return NOTIFY_OK;
4853}
4854
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855/*
4856 * /proc
4857 */
4858
4859#ifdef CONFIG_PROC_FS
4860
Alexey Dobriyan33120b32007-11-06 05:27:11 -08004861static const struct file_operations ipv6_route_proc_fops = {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08004862 .open = ipv6_route_open,
4863 .read = seq_read,
4864 .llseek = seq_lseek,
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02004865 .release = seq_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08004866};
4867
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868static int rt6_stats_seq_show(struct seq_file *seq, void *v)
4869{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004870 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004872 net->ipv6.rt6_stats->fib_nodes,
4873 net->ipv6.rt6_stats->fib_route_nodes,
Wei Wang81eb8442017-10-06 12:06:11 -07004874 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004875 net->ipv6.rt6_stats->fib_rt_entries,
4876 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00004877 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004878 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
4880 return 0;
4881}
4882
4883static int rt6_stats_seq_open(struct inode *inode, struct file *file)
4884{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07004885 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004886}
4887
Arjan van de Ven9a321442007-02-12 00:55:35 -08004888static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 .open = rt6_stats_seq_open,
4890 .read = seq_read,
4891 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07004892 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893};
4894#endif /* CONFIG_PROC_FS */
4895
4896#ifdef CONFIG_SYSCTL
4897
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898static
Joe Perchesfe2c6332013-06-11 23:04:25 -07004899int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 void __user *buffer, size_t *lenp, loff_t *ppos)
4901{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00004902 struct net *net;
4903 int delay;
4904 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00004906
4907 net = (struct net *)ctl->extra1;
4908 delay = net->ipv6.sysctl.flush_delay;
4909 proc_dointvec(ctl, write, buffer, lenp, ppos);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02004910 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00004911 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912}
4913
Joe Perchesfe2c6332013-06-11 23:04:25 -07004914struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004915 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08004917 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07004919 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004920 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 },
4922 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08004924 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 .maxlen = sizeof(int),
4926 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004927 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 },
4929 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08004931 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 .maxlen = sizeof(int),
4933 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004934 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 },
4936 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08004938 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 .maxlen = sizeof(int),
4940 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004941 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 },
4943 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08004945 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 .maxlen = sizeof(int),
4947 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004948 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 },
4950 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08004952 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 .maxlen = sizeof(int),
4954 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004955 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 },
4957 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08004959 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 .maxlen = sizeof(int),
4961 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07004962 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 },
4964 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08004966 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 .maxlen = sizeof(int),
4968 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004969 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 },
4971 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08004973 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 .maxlen = sizeof(int),
4975 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07004976 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 },
4978 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08004980 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 .maxlen = sizeof(int),
4982 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004983 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004985 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986};
4987
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004988struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08004989{
4990 struct ctl_table *table;
4991
4992 table = kmemdup(ipv6_route_table_template,
4993 sizeof(ipv6_route_table_template),
4994 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09004995
4996 if (table) {
4997 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00004998 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00004999 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005000 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
5001 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
5002 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
5003 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
5004 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
5005 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
5006 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08005007 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
Eric W. Biederman464dc802012-11-16 03:02:59 +00005008
5009 /* Don't export sysctls to unprivileged users */
5010 if (net->user_ns != &init_user_ns)
5011 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005012 }
5013
Daniel Lezcano760f2d02008-01-10 02:53:43 -08005014 return table;
5015}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016#endif
5017
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005018static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005019{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07005020 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005021
Alexey Dobriyan86393e52009-08-29 01:34:49 +00005022 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
5023 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005024
Eric Dumazetfc66f952010-10-08 06:37:34 +00005025 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5026 goto out_ip6_dst_ops;
5027
David Ahern421842e2018-04-17 17:33:18 -07005028 net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template,
5029 sizeof(*net->ipv6.fib6_null_entry),
5030 GFP_KERNEL);
5031 if (!net->ipv6.fib6_null_entry)
5032 goto out_ip6_dst_entries;
5033
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005034 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
5035 sizeof(*net->ipv6.ip6_null_entry),
5036 GFP_KERNEL);
5037 if (!net->ipv6.ip6_null_entry)
David Ahern421842e2018-04-17 17:33:18 -07005038 goto out_fib6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005039 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005040 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
5041 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005042
5043#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Vincent Bernatfeca7d82017-08-08 20:23:49 +02005044 net->ipv6.fib6_has_custom_rules = false;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005045 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
5046 sizeof(*net->ipv6.ip6_prohibit_entry),
5047 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005048 if (!net->ipv6.ip6_prohibit_entry)
5049 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005050 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005051 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
5052 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005053
5054 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
5055 sizeof(*net->ipv6.ip6_blk_hole_entry),
5056 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005057 if (!net->ipv6.ip6_blk_hole_entry)
5058 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005059 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005060 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
5061 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005062#endif
5063
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07005064 net->ipv6.sysctl.flush_delay = 0;
5065 net->ipv6.sysctl.ip6_rt_max_size = 4096;
5066 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5067 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5068 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5069 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5070 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5071 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
5072
Benjamin Thery6891a342008-03-04 13:49:47 -08005073 net->ipv6.ip6_rt_gc_expire = 30*HZ;
5074
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005075 ret = 0;
5076out:
5077 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005078
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005079#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5080out_ip6_prohibit_entry:
5081 kfree(net->ipv6.ip6_prohibit_entry);
5082out_ip6_null_entry:
5083 kfree(net->ipv6.ip6_null_entry);
5084#endif
David Ahern421842e2018-04-17 17:33:18 -07005085out_fib6_null_entry:
5086 kfree(net->ipv6.fib6_null_entry);
Eric Dumazetfc66f952010-10-08 06:37:34 +00005087out_ip6_dst_entries:
5088 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005089out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005090 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005091}
5092
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005093static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005094{
David Ahern421842e2018-04-17 17:33:18 -07005095 kfree(net->ipv6.fib6_null_entry);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005096 kfree(net->ipv6.ip6_null_entry);
5097#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5098 kfree(net->ipv6.ip6_prohibit_entry);
5099 kfree(net->ipv6.ip6_blk_hole_entry);
5100#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00005101 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005102}
5103
Thomas Grafd1896342012-06-18 12:08:33 +00005104static int __net_init ip6_route_net_init_late(struct net *net)
5105{
5106#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00005107 proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
Joe Perchesd6444062018-03-23 15:54:38 -07005108 proc_create("rt6_stats", 0444, net->proc_net, &rt6_stats_seq_fops);
Thomas Grafd1896342012-06-18 12:08:33 +00005109#endif
5110 return 0;
5111}
5112
5113static void __net_exit ip6_route_net_exit_late(struct net *net)
5114{
5115#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00005116 remove_proc_entry("ipv6_route", net->proc_net);
5117 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00005118#endif
5119}
5120
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005121static struct pernet_operations ip6_route_net_ops = {
5122 .init = ip6_route_net_init,
5123 .exit = ip6_route_net_exit,
5124};
5125
David S. Millerc3426b42012-06-09 16:27:05 -07005126static int __net_init ipv6_inetpeer_init(struct net *net)
5127{
5128 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5129
5130 if (!bp)
5131 return -ENOMEM;
5132 inet_peer_base_init(bp);
5133 net->ipv6.peers = bp;
5134 return 0;
5135}
5136
5137static void __net_exit ipv6_inetpeer_exit(struct net *net)
5138{
5139 struct inet_peer_base *bp = net->ipv6.peers;
5140
5141 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07005142 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07005143 kfree(bp);
5144}
5145
David S. Miller2b823f72012-06-09 19:00:16 -07005146static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07005147 .init = ipv6_inetpeer_init,
5148 .exit = ipv6_inetpeer_exit,
5149};
5150
Thomas Grafd1896342012-06-18 12:08:33 +00005151static struct pernet_operations ip6_route_net_late_ops = {
5152 .init = ip6_route_net_init_late,
5153 .exit = ip6_route_net_exit_late,
5154};
5155
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005156static struct notifier_block ip6_route_dev_notifier = {
5157 .notifier_call = ip6_route_dev_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07005158 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005159};
5160
WANG Cong2f460932017-05-03 22:07:31 -07005161void __init ip6_route_init_special_entries(void)
5162{
5163 /* Registering of the loopback is done before this portion of code,
5164 * the loopback reference in rt6_info will not be taken, do it
5165 * manually for init_net */
David Ahern421842e2018-04-17 17:33:18 -07005166 init_net.ipv6.fib6_null_entry->fib6_nh.nh_dev = init_net.loopback_dev;
WANG Cong2f460932017-05-03 22:07:31 -07005167 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
5168 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5169 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5170 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
5171 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5172 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
5173 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5174 #endif
5175}
5176
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005177int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005179 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07005180 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005181
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08005182 ret = -ENOMEM;
5183 ip6_dst_ops_template.kmem_cachep =
5184 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
5185 SLAB_HWCACHE_ALIGN, NULL);
5186 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08005187 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07005188
Eric Dumazetfc66f952010-10-08 06:37:34 +00005189 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005190 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08005191 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08005192
David S. Millerc3426b42012-06-09 16:27:05 -07005193 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
5194 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07005195 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00005196
David S. Miller7e52b332012-06-15 15:51:55 -07005197 ret = register_pernet_subsys(&ip6_route_net_ops);
5198 if (ret)
5199 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07005200
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07005201 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
5202
David S. Millere8803b62012-06-16 01:12:19 -07005203 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005204 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005205 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005206
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005207 ret = xfrm6_init();
5208 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07005209 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08005210
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005211 ret = fib6_rules_init();
5212 if (ret)
5213 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08005214
Thomas Grafd1896342012-06-18 12:08:33 +00005215 ret = register_pernet_subsys(&ip6_route_net_late_ops);
5216 if (ret)
5217 goto fib6_rules_init;
5218
Florian Westphal16feebc2017-12-02 21:44:08 +01005219 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
5220 inet6_rtm_newroute, NULL, 0);
5221 if (ret < 0)
5222 goto out_register_late_subsys;
5223
5224 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
5225 inet6_rtm_delroute, NULL, 0);
5226 if (ret < 0)
5227 goto out_register_late_subsys;
5228
5229 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
5230 inet6_rtm_getroute, NULL,
5231 RTNL_FLAG_DOIT_UNLOCKED);
5232 if (ret < 0)
Thomas Grafd1896342012-06-18 12:08:33 +00005233 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005234
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005235 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005236 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00005237 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005238
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07005239 for_each_possible_cpu(cpu) {
5240 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
5241
5242 INIT_LIST_HEAD(&ul->head);
5243 spin_lock_init(&ul->lock);
5244 }
5245
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005246out:
5247 return ret;
5248
Thomas Grafd1896342012-06-18 12:08:33 +00005249out_register_late_subsys:
Florian Westphal16feebc2017-12-02 21:44:08 +01005250 rtnl_unregister_all(PF_INET6);
Thomas Grafd1896342012-06-18 12:08:33 +00005251 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005252fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005253 fib6_rules_cleanup();
5254xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005255 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00005256out_fib6_init:
5257 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005258out_register_subsys:
5259 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07005260out_register_inetpeer:
5261 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00005262out_dst_entries:
5263 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005264out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005265 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005266 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267}
5268
5269void ip6_route_cleanup(void)
5270{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005271 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00005272 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07005273 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07005276 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005277 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00005278 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005279 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280}