blob: 08cae7323776c5db0f1d4e86e92c6c71f88ab2ed [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{
David Ahernd4ead6b2018-04-17 17:33:16 -07002610 int err = 0;
Florian Westphale715b6d2015-01-05 23:57:44 +01002611
David Ahernd4ead6b2018-04-17 17:33:16 -07002612 if (cfg->fc_mx) {
2613 rt->fib6_metrics = kzalloc(sizeof(*rt->fib6_metrics),
2614 GFP_KERNEL);
2615 if (unlikely(!rt->fib6_metrics))
2616 return -ENOMEM;
Florian Westphale715b6d2015-01-05 23:57:44 +01002617
David Ahernd4ead6b2018-04-17 17:33:16 -07002618 refcount_set(&rt->fib6_metrics->refcnt, 1);
Florian Westphale715b6d2015-01-05 23:57:44 +01002619
David Ahernd4ead6b2018-04-17 17:33:16 -07002620 err = ip_metrics_convert(net, cfg->fc_mx, cfg->fc_mx_len,
2621 rt->fib6_metrics->metrics);
Florian Westphale715b6d2015-01-05 23:57:44 +01002622 }
2623
David Ahernd4ead6b2018-04-17 17:33:16 -07002624 return err;
Florian Westphale715b6d2015-01-05 23:57:44 +01002625}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
David Ahern8c145862016-04-24 21:26:04 -07002627static struct rt6_info *ip6_nh_lookup_table(struct net *net,
2628 struct fib6_config *cfg,
David Ahernf4797b32018-01-25 16:55:08 -08002629 const struct in6_addr *gw_addr,
2630 u32 tbid, int flags)
David Ahern8c145862016-04-24 21:26:04 -07002631{
2632 struct flowi6 fl6 = {
2633 .flowi6_oif = cfg->fc_ifindex,
2634 .daddr = *gw_addr,
2635 .saddr = cfg->fc_prefsrc,
2636 };
2637 struct fib6_table *table;
2638 struct rt6_info *rt;
David Ahern8c145862016-04-24 21:26:04 -07002639
David Ahernf4797b32018-01-25 16:55:08 -08002640 table = fib6_get_table(net, tbid);
David Ahern8c145862016-04-24 21:26:04 -07002641 if (!table)
2642 return NULL;
2643
2644 if (!ipv6_addr_any(&cfg->fc_prefsrc))
2645 flags |= RT6_LOOKUP_F_HAS_SADDR;
2646
David Ahernf4797b32018-01-25 16:55:08 -08002647 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
David Ahernb75cc8f2018-03-02 08:32:17 -08002648 rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
David Ahern8c145862016-04-24 21:26:04 -07002649
2650 /* if table lookup failed, fall back to full lookup */
2651 if (rt == net->ipv6.ip6_null_entry) {
2652 ip6_rt_put(rt);
2653 rt = NULL;
2654 }
2655
2656 return rt;
2657}
2658
David Ahernfc1e64e2018-01-25 16:55:09 -08002659static int ip6_route_check_nh_onlink(struct net *net,
2660 struct fib6_config *cfg,
David Ahern9fbb7042018-03-13 08:29:36 -07002661 const struct net_device *dev,
David Ahernfc1e64e2018-01-25 16:55:09 -08002662 struct netlink_ext_ack *extack)
2663{
David Ahern44750f82018-02-06 13:17:06 -08002664 u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
David Ahernfc1e64e2018-01-25 16:55:09 -08002665 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2666 u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
2667 struct rt6_info *grt;
2668 int err;
2669
2670 err = 0;
2671 grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
2672 if (grt) {
David Ahern58e354c2018-02-06 12:14:12 -08002673 if (!grt->dst.error &&
2674 (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
David Ahern44750f82018-02-06 13:17:06 -08002675 NL_SET_ERR_MSG(extack,
2676 "Nexthop has invalid gateway or device mismatch");
David Ahernfc1e64e2018-01-25 16:55:09 -08002677 err = -EINVAL;
2678 }
2679
2680 ip6_rt_put(grt);
2681 }
2682
2683 return err;
2684}
2685
David Ahern1edce992018-01-25 16:55:07 -08002686static int ip6_route_check_nh(struct net *net,
2687 struct fib6_config *cfg,
2688 struct net_device **_dev,
2689 struct inet6_dev **idev)
2690{
2691 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2692 struct net_device *dev = _dev ? *_dev : NULL;
2693 struct rt6_info *grt = NULL;
2694 int err = -EHOSTUNREACH;
2695
2696 if (cfg->fc_table) {
David Ahernf4797b32018-01-25 16:55:08 -08002697 int flags = RT6_LOOKUP_F_IFACE;
2698
2699 grt = ip6_nh_lookup_table(net, cfg, gw_addr,
2700 cfg->fc_table, flags);
David Ahern1edce992018-01-25 16:55:07 -08002701 if (grt) {
2702 if (grt->rt6i_flags & RTF_GATEWAY ||
2703 (dev && dev != grt->dst.dev)) {
2704 ip6_rt_put(grt);
2705 grt = NULL;
2706 }
2707 }
2708 }
2709
2710 if (!grt)
David Ahernb75cc8f2018-03-02 08:32:17 -08002711 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
David Ahern1edce992018-01-25 16:55:07 -08002712
2713 if (!grt)
2714 goto out;
2715
2716 if (dev) {
2717 if (dev != grt->dst.dev) {
2718 ip6_rt_put(grt);
2719 goto out;
2720 }
2721 } else {
2722 *_dev = dev = grt->dst.dev;
2723 *idev = grt->rt6i_idev;
2724 dev_hold(dev);
2725 in6_dev_hold(grt->rt6i_idev);
2726 }
2727
2728 if (!(grt->rt6i_flags & RTF_GATEWAY))
2729 err = 0;
2730
2731 ip6_rt_put(grt);
2732
2733out:
2734 return err;
2735}
2736
David Ahern9fbb7042018-03-13 08:29:36 -07002737static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
2738 struct net_device **_dev, struct inet6_dev **idev,
2739 struct netlink_ext_ack *extack)
2740{
2741 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2742 int gwa_type = ipv6_addr_type(gw_addr);
David Ahern232378e2018-03-13 08:29:37 -07002743 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
David Ahern9fbb7042018-03-13 08:29:36 -07002744 const struct net_device *dev = *_dev;
David Ahern232378e2018-03-13 08:29:37 -07002745 bool need_addr_check = !dev;
David Ahern9fbb7042018-03-13 08:29:36 -07002746 int err = -EINVAL;
2747
2748 /* if gw_addr is local we will fail to detect this in case
2749 * address is still TENTATIVE (DAD in progress). rt6_lookup()
2750 * will return already-added prefix route via interface that
2751 * prefix route was assigned to, which might be non-loopback.
2752 */
David Ahern232378e2018-03-13 08:29:37 -07002753 if (dev &&
2754 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2755 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
David Ahern9fbb7042018-03-13 08:29:36 -07002756 goto out;
2757 }
2758
2759 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
2760 /* IPv6 strictly inhibits using not link-local
2761 * addresses as nexthop address.
2762 * Otherwise, router will not able to send redirects.
2763 * It is very good, but in some (rare!) circumstances
2764 * (SIT, PtP, NBMA NOARP links) it is handy to allow
2765 * some exceptions. --ANK
2766 * We allow IPv4-mapped nexthops to support RFC4798-type
2767 * addressing
2768 */
2769 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
2770 NL_SET_ERR_MSG(extack, "Invalid gateway address");
2771 goto out;
2772 }
2773
2774 if (cfg->fc_flags & RTNH_F_ONLINK)
2775 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
2776 else
2777 err = ip6_route_check_nh(net, cfg, _dev, idev);
2778
2779 if (err)
2780 goto out;
2781 }
2782
2783 /* reload in case device was changed */
2784 dev = *_dev;
2785
2786 err = -EINVAL;
2787 if (!dev) {
2788 NL_SET_ERR_MSG(extack, "Egress device not specified");
2789 goto out;
2790 } else if (dev->flags & IFF_LOOPBACK) {
2791 NL_SET_ERR_MSG(extack,
2792 "Egress device can not be loopback device for this route");
2793 goto out;
2794 }
David Ahern232378e2018-03-13 08:29:37 -07002795
2796 /* if we did not check gw_addr above, do so now that the
2797 * egress device has been resolved.
2798 */
2799 if (need_addr_check &&
2800 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2801 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
2802 goto out;
2803 }
2804
David Ahern9fbb7042018-03-13 08:29:36 -07002805 err = 0;
2806out:
2807 return err;
2808}
2809
David Ahern8d1c8022018-04-17 17:33:26 -07002810static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
David Ahernacb54e32018-04-17 17:33:22 -07002811 gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06002812 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
Daniel Lezcano55786892008-03-04 13:47:47 -08002814 struct net *net = cfg->fc_nlinfo.nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07002815 struct fib6_info *rt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 struct net_device *dev = NULL;
2817 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07002818 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 int addr_type;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002820 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
David Ahern557c44b2017-04-19 14:19:43 -07002822 /* RTF_PCPU is an internal flag; can not be set by userspace */
David Ahernd5d531c2017-05-21 10:12:05 -06002823 if (cfg->fc_flags & RTF_PCPU) {
2824 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
David Ahern557c44b2017-04-19 14:19:43 -07002825 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06002826 }
David Ahern557c44b2017-04-19 14:19:43 -07002827
Wei Wang2ea23522017-10-27 17:30:12 -07002828 /* RTF_CACHE is an internal flag; can not be set by userspace */
2829 if (cfg->fc_flags & RTF_CACHE) {
2830 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
2831 goto out;
2832 }
2833
David Aherne8478e82018-04-17 17:33:13 -07002834 if (cfg->fc_type > RTN_MAX) {
2835 NL_SET_ERR_MSG(extack, "Invalid route type");
2836 goto out;
2837 }
2838
David Ahernd5d531c2017-05-21 10:12:05 -06002839 if (cfg->fc_dst_len > 128) {
2840 NL_SET_ERR_MSG(extack, "Invalid prefix length");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002841 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06002842 }
2843 if (cfg->fc_src_len > 128) {
2844 NL_SET_ERR_MSG(extack, "Invalid source address length");
2845 goto out;
2846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847#ifndef CONFIG_IPV6_SUBTREES
David Ahernd5d531c2017-05-21 10:12:05 -06002848 if (cfg->fc_src_len) {
2849 NL_SET_ERR_MSG(extack,
2850 "Specifying source address requires IPV6_SUBTREES to be enabled");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002851 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06002852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07002854 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08002856 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 if (!dev)
2858 goto out;
2859 idev = in6_dev_get(dev);
2860 if (!idev)
2861 goto out;
2862 }
2863
Thomas Graf86872cb2006-08-22 00:01:08 -07002864 if (cfg->fc_metric == 0)
2865 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
David Ahernfc1e64e2018-01-25 16:55:09 -08002867 if (cfg->fc_flags & RTNH_F_ONLINK) {
2868 if (!dev) {
2869 NL_SET_ERR_MSG(extack,
2870 "Nexthop device required for onlink");
2871 err = -ENODEV;
2872 goto out;
2873 }
2874
2875 if (!(dev->flags & IFF_UP)) {
2876 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2877 err = -ENETDOWN;
2878 goto out;
2879 }
2880 }
2881
Matti Vaittinend71314b2011-11-14 00:14:49 +00002882 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05002883 if (cfg->fc_nlinfo.nlh &&
2884 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00002885 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05002886 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00002887 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00002888 table = fib6_new_table(net, cfg->fc_table);
2889 }
2890 } else {
2891 table = fib6_new_table(net, cfg->fc_table);
2892 }
David S. Miller38308472011-12-03 18:02:47 -05002893
2894 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002895 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07002896
David Ahern93531c62018-04-17 17:33:25 -07002897 err = -ENOMEM;
2898 rt = fib6_info_alloc(gfp_flags);
2899 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 goto out;
David Ahern93531c62018-04-17 17:33:25 -07002901
2902 if (cfg->fc_flags & RTF_ADDRCONF)
2903 rt->dst_nocount = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
David Ahernd4ead6b2018-04-17 17:33:16 -07002905 err = ip6_convert_metrics(net, rt, cfg);
2906 if (err < 0)
2907 goto out;
2908
Gao feng1716a962012-04-06 00:13:10 +00002909 if (cfg->fc_flags & RTF_EXPIRES)
David Ahern14895682018-04-17 17:33:17 -07002910 fib6_set_expires(rt, jiffies +
Gao feng1716a962012-04-06 00:13:10 +00002911 clock_t_to_jiffies(cfg->fc_expires));
2912 else
David Ahern14895682018-04-17 17:33:17 -07002913 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Thomas Graf86872cb2006-08-22 00:01:08 -07002915 if (cfg->fc_protocol == RTPROT_UNSPEC)
2916 cfg->fc_protocol = RTPROT_BOOT;
David Ahern93c2fb22018-04-18 15:38:59 -07002917 rt->fib6_protocol = cfg->fc_protocol;
Thomas Graf86872cb2006-08-22 00:01:08 -07002918
2919 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002921 if (cfg->fc_encap) {
2922 struct lwtunnel_state *lwtstate;
2923
David Ahern30357d72017-01-30 12:07:37 -08002924 err = lwtunnel_build_state(cfg->fc_encap_type,
Tom Herbert127eb7c2015-08-24 09:45:41 -07002925 cfg->fc_encap, AF_INET6, cfg,
David Ahern9ae28722017-05-27 16:19:28 -06002926 &lwtstate, extack);
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002927 if (err)
2928 goto out;
David Ahern5e670d82018-04-17 17:33:14 -07002929 rt->fib6_nh.nh_lwtstate = lwtstate_get(lwtstate);
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002930 }
2931
David Ahern93c2fb22018-04-18 15:38:59 -07002932 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
2933 rt->fib6_dst.plen = cfg->fc_dst_len;
2934 if (rt->fib6_dst.plen == 128)
David Ahern3b6761d2018-04-17 17:33:20 -07002935 rt->dst_host = true;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01002936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07002938 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
2939 rt->fib6_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940#endif
2941
David Ahern93c2fb22018-04-18 15:38:59 -07002942 rt->fib6_metric = cfg->fc_metric;
David Ahern5e670d82018-04-17 17:33:14 -07002943 rt->fib6_nh.nh_weight = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
David Aherne8478e82018-04-17 17:33:13 -07002945 rt->fib6_type = cfg->fc_type;
2946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 /* We cannot add true routes via loopback here,
2948 they would result in kernel looping; promote them to reject routes
2949 */
Thomas Graf86872cb2006-08-22 00:01:08 -07002950 if ((cfg->fc_flags & RTF_REJECT) ||
David S. Miller38308472011-12-03 18:02:47 -05002951 (dev && (dev->flags & IFF_LOOPBACK) &&
2952 !(addr_type & IPV6_ADDR_LOOPBACK) &&
2953 !(cfg->fc_flags & RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08002955 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 if (dev) {
2957 dev_put(dev);
2958 in6_dev_put(idev);
2959 }
Daniel Lezcano55786892008-03-04 13:47:47 -08002960 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 dev_hold(dev);
2962 idev = in6_dev_get(dev);
2963 if (!idev) {
2964 err = -ENODEV;
2965 goto out;
2966 }
2967 }
David Ahern93c2fb22018-04-18 15:38:59 -07002968 rt->fib6_flags = RTF_REJECT|RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 goto install_route;
2970 }
2971
Thomas Graf86872cb2006-08-22 00:01:08 -07002972 if (cfg->fc_flags & RTF_GATEWAY) {
David Ahern9fbb7042018-03-13 08:29:36 -07002973 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
2974 if (err)
Florian Westphal48ed7b22015-05-21 00:25:41 +02002975 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
David Ahern93531c62018-04-17 17:33:25 -07002977 rt->fib6_nh.nh_gw = cfg->fc_gateway;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 }
2979
2980 err = -ENODEV;
David S. Miller38308472011-12-03 18:02:47 -05002981 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 goto out;
2983
Lorenzo Bianconi428604f2018-03-29 11:02:24 +02002984 if (idev->cnf.disable_ipv6) {
2985 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
2986 err = -EACCES;
2987 goto out;
2988 }
2989
David Ahern955ec4c2018-01-24 19:45:29 -08002990 if (!(dev->flags & IFF_UP)) {
2991 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2992 err = -ENETDOWN;
2993 goto out;
2994 }
2995
Daniel Walterc3968a82011-04-13 21:10:57 +00002996 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
2997 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
David Ahernd5d531c2017-05-21 10:12:05 -06002998 NL_SET_ERR_MSG(extack, "Invalid source address");
Daniel Walterc3968a82011-04-13 21:10:57 +00002999 err = -EINVAL;
3000 goto out;
3001 }
David Ahern93c2fb22018-04-18 15:38:59 -07003002 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3003 rt->fib6_prefsrc.plen = 128;
Daniel Walterc3968a82011-04-13 21:10:57 +00003004 } else
David Ahern93c2fb22018-04-18 15:38:59 -07003005 rt->fib6_prefsrc.plen = 0;
Daniel Walterc3968a82011-04-13 21:10:57 +00003006
David Ahern93c2fb22018-04-18 15:38:59 -07003007 rt->fib6_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
3009install_route:
David Ahern93c2fb22018-04-18 15:38:59 -07003010 if (!(rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
Ido Schimmel5609b802018-01-07 12:45:06 +02003011 !netif_carrier_ok(dev))
David Ahern5e670d82018-04-17 17:33:14 -07003012 rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
3013 rt->fib6_nh.nh_flags |= (cfg->fc_flags & RTNH_F_ONLINK);
David Ahern93531c62018-04-17 17:33:25 -07003014 rt->fib6_nh.nh_dev = dev;
David Ahern93c2fb22018-04-18 15:38:59 -07003015 rt->fib6_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08003016
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003017 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08003018
David Aherndcd1f572018-04-18 15:39:05 -07003019 if (idev)
3020 in6_dev_put(idev);
3021
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003022 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023out:
3024 if (dev)
3025 dev_put(dev);
3026 if (idev)
3027 in6_dev_put(idev);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003028
David Ahern93531c62018-04-17 17:33:25 -07003029 fib6_info_release(rt);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003030 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003031}
3032
David Ahernacb54e32018-04-17 17:33:22 -07003033int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3034 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003035{
David Ahern8d1c8022018-04-17 17:33:26 -07003036 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003037 int err;
3038
David Ahernacb54e32018-04-17 17:33:22 -07003039 rt = ip6_route_info_create(cfg, gfp_flags, extack);
David Ahernd4ead6b2018-04-17 17:33:16 -07003040 if (IS_ERR(rt))
3041 return PTR_ERR(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003042
David Ahernd4ead6b2018-04-17 17:33:16 -07003043 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
David Ahern93531c62018-04-17 17:33:25 -07003044 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 return err;
3047}
3048
David Ahern8d1c8022018-04-17 17:33:26 -07003049static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
David Ahernafb1d4b52018-04-17 17:33:11 -07003051 struct net *net = info->nl_net;
Thomas Grafc71099a2006-08-04 23:20:06 -07003052 struct fib6_table *table;
David Ahernafb1d4b52018-04-17 17:33:11 -07003053 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
David Ahern421842e2018-04-17 17:33:18 -07003055 if (rt == net->ipv6.fib6_null_entry) {
Gao feng6825a262012-09-19 19:25:34 +00003056 err = -ENOENT;
3057 goto out;
3058 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07003059
David Ahern93c2fb22018-04-18 15:38:59 -07003060 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003061 spin_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07003062 err = fib6_del(rt, info);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003063 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Gao feng6825a262012-09-19 19:25:34 +00003065out:
David Ahern93531c62018-04-17 17:33:25 -07003066 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 return err;
3068}
3069
David Ahern8d1c8022018-04-17 17:33:26 -07003070int ip6_del_rt(struct net *net, struct fib6_info *rt)
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003071{
David Ahernafb1d4b52018-04-17 17:33:11 -07003072 struct nl_info info = { .nl_net = net };
3073
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003074 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003075}
3076
David Ahern8d1c8022018-04-17 17:33:26 -07003077static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
David Ahern0ae81332017-02-02 12:37:08 -08003078{
3079 struct nl_info *info = &cfg->fc_nlinfo;
WANG Conge3330032017-02-27 16:07:43 -08003080 struct net *net = info->nl_net;
David Ahern16a16cd2017-02-02 12:37:11 -08003081 struct sk_buff *skb = NULL;
David Ahern0ae81332017-02-02 12:37:08 -08003082 struct fib6_table *table;
WANG Conge3330032017-02-27 16:07:43 -08003083 int err = -ENOENT;
David Ahern0ae81332017-02-02 12:37:08 -08003084
David Ahern421842e2018-04-17 17:33:18 -07003085 if (rt == net->ipv6.fib6_null_entry)
WANG Conge3330032017-02-27 16:07:43 -08003086 goto out_put;
David Ahern93c2fb22018-04-18 15:38:59 -07003087 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003088 spin_lock_bh(&table->tb6_lock);
David Ahern0ae81332017-02-02 12:37:08 -08003089
David Ahern93c2fb22018-04-18 15:38:59 -07003090 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
David Ahern8d1c8022018-04-17 17:33:26 -07003091 struct fib6_info *sibling, *next_sibling;
David Ahern0ae81332017-02-02 12:37:08 -08003092
David Ahern16a16cd2017-02-02 12:37:11 -08003093 /* prefer to send a single notification with all hops */
3094 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3095 if (skb) {
3096 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3097
David Ahernd4ead6b2018-04-17 17:33:16 -07003098 if (rt6_fill_node(net, skb, rt, NULL,
David Ahern16a16cd2017-02-02 12:37:11 -08003099 NULL, NULL, 0, RTM_DELROUTE,
3100 info->portid, seq, 0) < 0) {
3101 kfree_skb(skb);
3102 skb = NULL;
3103 } else
3104 info->skip_notify = 1;
3105 }
3106
David Ahern0ae81332017-02-02 12:37:08 -08003107 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07003108 &rt->fib6_siblings,
3109 fib6_siblings) {
David Ahern0ae81332017-02-02 12:37:08 -08003110 err = fib6_del(sibling, info);
3111 if (err)
WANG Conge3330032017-02-27 16:07:43 -08003112 goto out_unlock;
David Ahern0ae81332017-02-02 12:37:08 -08003113 }
3114 }
3115
3116 err = fib6_del(rt, info);
WANG Conge3330032017-02-27 16:07:43 -08003117out_unlock:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003118 spin_unlock_bh(&table->tb6_lock);
WANG Conge3330032017-02-27 16:07:43 -08003119out_put:
David Ahern93531c62018-04-17 17:33:25 -07003120 fib6_info_release(rt);
David Ahern16a16cd2017-02-02 12:37:11 -08003121
3122 if (skb) {
WANG Conge3330032017-02-27 16:07:43 -08003123 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
David Ahern16a16cd2017-02-02 12:37:11 -08003124 info->nlh, gfp_any());
3125 }
David Ahern0ae81332017-02-02 12:37:08 -08003126 return err;
3127}
3128
David Ahern23fb93a2018-04-17 17:33:23 -07003129static int ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
3130{
3131 int rc = -ESRCH;
3132
3133 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3134 goto out;
3135
3136 if (cfg->fc_flags & RTF_GATEWAY &&
3137 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3138 goto out;
3139 if (dst_hold_safe(&rt->dst))
3140 rc = rt6_remove_exception_rt(rt);
3141out:
3142 return rc;
3143}
3144
David Ahern333c4302017-05-21 10:12:04 -06003145static int ip6_route_del(struct fib6_config *cfg,
3146 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147{
David Ahern8d1c8022018-04-17 17:33:26 -07003148 struct rt6_info *rt_cache;
Thomas Grafc71099a2006-08-04 23:20:06 -07003149 struct fib6_table *table;
David Ahern8d1c8022018-04-17 17:33:26 -07003150 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 int err = -ESRCH;
3153
Daniel Lezcano55786892008-03-04 13:47:47 -08003154 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David Ahernd5d531c2017-05-21 10:12:05 -06003155 if (!table) {
3156 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Grafc71099a2006-08-04 23:20:06 -07003157 return err;
David Ahernd5d531c2017-05-21 10:12:05 -06003158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Wei Wang66f5d6c2017-10-06 12:06:10 -07003160 rcu_read_lock();
Thomas Grafc71099a2006-08-04 23:20:06 -07003161
3162 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07003163 &cfg->fc_dst, cfg->fc_dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07003164 &cfg->fc_src, cfg->fc_src_len,
Wei Wang2b760fc2017-10-06 12:06:03 -07003165 !(cfg->fc_flags & RTF_CACHE));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003166
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 if (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003168 for_each_fib6_node_rt_rcu(fn) {
Wei Wang2b760fc2017-10-06 12:06:03 -07003169 if (cfg->fc_flags & RTF_CACHE) {
David Ahern23fb93a2018-04-17 17:33:23 -07003170 int rc;
3171
Wei Wang2b760fc2017-10-06 12:06:03 -07003172 rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst,
3173 &cfg->fc_src);
David Ahern23fb93a2018-04-17 17:33:23 -07003174 if (rt_cache) {
3175 rc = ip6_del_cached_rt(rt_cache, cfg);
3176 if (rc != -ESRCH)
3177 return rc;
3178 }
3179 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07003180 }
Thomas Graf86872cb2006-08-22 00:01:08 -07003181 if (cfg->fc_ifindex &&
David Ahern5e670d82018-04-17 17:33:14 -07003182 (!rt->fib6_nh.nh_dev ||
3183 rt->fib6_nh.nh_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07003185 if (cfg->fc_flags & RTF_GATEWAY &&
David Ahern5e670d82018-04-17 17:33:14 -07003186 !ipv6_addr_equal(&cfg->fc_gateway, &rt->fib6_nh.nh_gw))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003188 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003190 if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol)
Mantas Mc2ed1882016-12-16 10:30:59 +02003191 continue;
David Ahern93531c62018-04-17 17:33:25 -07003192 fib6_info_hold(rt);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003193 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
David Ahern0ae81332017-02-02 12:37:08 -08003195 /* if gateway was specified only delete the one hop */
3196 if (cfg->fc_flags & RTF_GATEWAY)
3197 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3198
3199 return __ip6_del_rt_siblings(rt, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 }
3201 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003202 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
3204 return err;
3205}
3206
David S. Miller6700c272012-07-17 03:29:28 -07003207static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003208{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003209 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07003210 struct rt6_info *rt, *nrt = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07003211 struct ndisc_options ndopts;
3212 struct inet6_dev *in6_dev;
3213 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003214 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07003215 int optlen, on_link;
3216 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07003217
Simon Horman29a3cad2013-05-28 20:34:26 +00003218 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003219 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07003220
3221 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07003222 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003223 return;
3224 }
3225
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003226 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07003227
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003228 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003229 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003230 return;
3231 }
3232
David S. Miller6e157b62012-07-12 00:05:02 -07003233 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003234 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003235 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003236 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07003237 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003238 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003239 return;
3240 }
3241
3242 in6_dev = __in6_dev_get(skb->dev);
3243 if (!in6_dev)
3244 return;
3245 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3246 return;
3247
3248 /* RFC2461 8.1:
3249 * The IP source address of the Redirect MUST be the same as the current
3250 * first-hop router for the specified ICMP Destination Address.
3251 */
3252
Alexander Aringf997c552016-06-15 21:20:23 +02003253 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003254 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3255 return;
3256 }
David S. Miller6e157b62012-07-12 00:05:02 -07003257
3258 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07003259 if (ndopts.nd_opts_tgt_lladdr) {
3260 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3261 skb->dev);
3262 if (!lladdr) {
3263 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3264 return;
3265 }
3266 }
3267
David S. Miller6e157b62012-07-12 00:05:02 -07003268 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01003269 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07003270 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
3271 return;
3272 }
3273
3274 /* Redirect received -> path was valid.
3275 * Look, redirects are sent only in response to data packets,
3276 * so that this nexthop apparently is reachable. --ANK
3277 */
Julian Anastasov0dec8792017-02-06 23:14:16 +02003278 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
David S. Miller6e157b62012-07-12 00:05:02 -07003279
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003280 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07003281 if (!neigh)
3282 return;
3283
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 /*
3285 * We have finally decided to accept it.
3286 */
3287
Alexander Aringf997c552016-06-15 21:20:23 +02003288 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 NEIGH_UPDATE_F_WEAK_OVERRIDE|
3290 NEIGH_UPDATE_F_OVERRIDE|
3291 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02003292 NEIGH_UPDATE_F_ISROUTER)),
3293 NDISC_REDIRECT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
David Ahern23fb93a2018-04-17 17:33:23 -07003295 nrt = ip6_rt_cache_alloc(rt->from, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05003296 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 goto out;
3298
3299 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
3300 if (on_link)
3301 nrt->rt6i_flags &= ~RTF_GATEWAY;
3302
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003303 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Wei Wang2b760fc2017-10-06 12:06:03 -07003305 /* No need to remove rt from the exception table if rt is
3306 * a cached route because rt6_insert_exception() will
3307 * takes care of it
3308 */
David Ahernd4ead6b2018-04-17 17:33:16 -07003309 if (rt6_insert_exception(nrt, rt->from)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07003310 dst_release_immediate(&nrt->dst);
3311 goto out;
3312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
Changli Gaod8d1f302010-06-10 23:31:35 -07003314 netevent.old = &rt->dst;
3315 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003316 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00003317 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07003318 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
3319
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320out:
David S. Millere8599ff2012-07-11 23:43:53 -07003321 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07003322}
3323
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003324#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -07003325static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003326 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07003327 const struct in6_addr *gwaddr,
3328 struct net_device *dev)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003329{
David Ahern830218c2016-10-24 10:52:35 -07003330 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3331 int ifindex = dev->ifindex;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003332 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07003333 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003334 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003335
David Ahern830218c2016-10-24 10:52:35 -07003336 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05003337 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003338 return NULL;
3339
Wei Wang66f5d6c2017-10-06 12:06:10 -07003340 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07003341 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003342 if (!fn)
3343 goto out;
3344
Wei Wang66f5d6c2017-10-06 12:06:10 -07003345 for_each_fib6_node_rt_rcu(fn) {
David Ahern5e670d82018-04-17 17:33:14 -07003346 if (rt->fib6_nh.nh_dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003347 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003348 if ((rt->fib6_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003349 continue;
David Ahern5e670d82018-04-17 17:33:14 -07003350 if (!ipv6_addr_equal(&rt->fib6_nh.nh_gw, gwaddr))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003351 continue;
David Ahern8d1c8022018-04-17 17:33:26 -07003352 fib6_info_hold(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003353 break;
3354 }
3355out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003356 rcu_read_unlock();
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003357 return rt;
3358}
3359
David Ahern8d1c8022018-04-17 17:33:26 -07003360static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003361 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07003362 const struct in6_addr *gwaddr,
3363 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +00003364 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003365{
Thomas Graf86872cb2006-08-22 00:01:08 -07003366 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08003367 .fc_metric = IP6_RT_PRIO_USER,
David Ahern830218c2016-10-24 10:52:35 -07003368 .fc_ifindex = dev->ifindex,
Thomas Graf86872cb2006-08-22 00:01:08 -07003369 .fc_dst_len = prefixlen,
3370 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
3371 RTF_UP | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08003372 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07003373 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003374 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08003375 .fc_nlinfo.nlh = NULL,
3376 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07003377 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003378
David Ahern830218c2016-10-24 10:52:35 -07003379 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003380 cfg.fc_dst = *prefix;
3381 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07003382
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08003383 /* We should treat it as a default route if prefix length is 0. */
3384 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07003385 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003386
David Ahernacb54e32018-04-17 17:33:22 -07003387 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003388
David Ahern830218c2016-10-24 10:52:35 -07003389 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003390}
3391#endif
3392
David Ahern8d1c8022018-04-17 17:33:26 -07003393struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07003394 const struct in6_addr *addr,
3395 struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003396{
David Ahern830218c2016-10-24 10:52:35 -07003397 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
David Ahern8d1c8022018-04-17 17:33:26 -07003398 struct fib6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07003399 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
David Ahernafb1d4b52018-04-17 17:33:11 -07003401 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05003402 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003403 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Wei Wang66f5d6c2017-10-06 12:06:10 -07003405 rcu_read_lock();
3406 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Ahern5e670d82018-04-17 17:33:14 -07003407 if (dev == rt->fib6_nh.nh_dev &&
David Ahern93c2fb22018-04-18 15:38:59 -07003408 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Ahern5e670d82018-04-17 17:33:14 -07003409 ipv6_addr_equal(&rt->fib6_nh.nh_gw, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 break;
3411 }
3412 if (rt)
David Ahern8d1c8022018-04-17 17:33:26 -07003413 fib6_info_hold(rt);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003414 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 return rt;
3416}
3417
David Ahern8d1c8022018-04-17 17:33:26 -07003418struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07003419 const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08003420 struct net_device *dev,
3421 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422{
Thomas Graf86872cb2006-08-22 00:01:08 -07003423 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07003424 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08003425 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07003426 .fc_ifindex = dev->ifindex,
3427 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
3428 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08003429 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07003430 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003431 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08003432 .fc_nlinfo.nlh = NULL,
David Ahernafb1d4b52018-04-17 17:33:11 -07003433 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07003434 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003436 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437
David Ahernacb54e32018-04-17 17:33:22 -07003438 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
David Ahern830218c2016-10-24 10:52:35 -07003439 struct fib6_table *table;
3440
3441 table = fib6_get_table(dev_net(dev), cfg.fc_table);
3442 if (table)
3443 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
David Ahernafb1d4b52018-04-17 17:33:11 -07003446 return rt6_get_dflt_router(net, gwaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447}
3448
David Ahernafb1d4b52018-04-17 17:33:11 -07003449static void __rt6_purge_dflt_routers(struct net *net,
3450 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451{
David Ahern8d1c8022018-04-17 17:33:26 -07003452 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
3454restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003455 rcu_read_lock();
3456 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Aherndcd1f572018-04-18 15:39:05 -07003457 struct net_device *dev = fib6_info_nh_dev(rt);
3458 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
3459
David Ahern93c2fb22018-04-18 15:38:59 -07003460 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
David Aherndcd1f572018-04-18 15:39:05 -07003461 (!idev || idev->cnf.accept_ra != 2)) {
David Ahern93531c62018-04-17 17:33:25 -07003462 fib6_info_hold(rt);
3463 rcu_read_unlock();
3464 ip6_del_rt(net, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 goto restart;
3466 }
3467 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003468 rcu_read_unlock();
David Ahern830218c2016-10-24 10:52:35 -07003469
3470 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3471}
3472
3473void rt6_purge_dflt_routers(struct net *net)
3474{
3475 struct fib6_table *table;
3476 struct hlist_head *head;
3477 unsigned int h;
3478
3479 rcu_read_lock();
3480
3481 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3482 head = &net->ipv6.fib_table_hash[h];
3483 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3484 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
David Ahernafb1d4b52018-04-17 17:33:11 -07003485 __rt6_purge_dflt_routers(net, table);
David Ahern830218c2016-10-24 10:52:35 -07003486 }
3487 }
3488
3489 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490}
3491
Daniel Lezcano55786892008-03-04 13:47:47 -08003492static void rtmsg_to_fib6_config(struct net *net,
3493 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07003494 struct fib6_config *cfg)
3495{
3496 memset(cfg, 0, sizeof(*cfg));
3497
David Ahernca254492015-10-12 11:47:10 -07003498 cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
3499 : RT6_TABLE_MAIN;
Thomas Graf86872cb2006-08-22 00:01:08 -07003500 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
3501 cfg->fc_metric = rtmsg->rtmsg_metric;
3502 cfg->fc_expires = rtmsg->rtmsg_info;
3503 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
3504 cfg->fc_src_len = rtmsg->rtmsg_src_len;
3505 cfg->fc_flags = rtmsg->rtmsg_flags;
David Aherne8478e82018-04-17 17:33:13 -07003506 cfg->fc_type = rtmsg->rtmsg_type;
Thomas Graf86872cb2006-08-22 00:01:08 -07003507
Daniel Lezcano55786892008-03-04 13:47:47 -08003508 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08003509
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003510 cfg->fc_dst = rtmsg->rtmsg_dst;
3511 cfg->fc_src = rtmsg->rtmsg_src;
3512 cfg->fc_gateway = rtmsg->rtmsg_gateway;
Thomas Graf86872cb2006-08-22 00:01:08 -07003513}
3514
Daniel Lezcano55786892008-03-04 13:47:47 -08003515int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516{
Thomas Graf86872cb2006-08-22 00:01:08 -07003517 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 struct in6_rtmsg rtmsg;
3519 int err;
3520
Ian Morris67ba4152014-08-24 21:53:10 +01003521 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 case SIOCADDRT: /* Add a route */
3523 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00003524 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 return -EPERM;
3526 err = copy_from_user(&rtmsg, arg,
3527 sizeof(struct in6_rtmsg));
3528 if (err)
3529 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07003530
Daniel Lezcano55786892008-03-04 13:47:47 -08003531 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07003532
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 rtnl_lock();
3534 switch (cmd) {
3535 case SIOCADDRT:
David Ahernacb54e32018-04-17 17:33:22 -07003536 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 break;
3538 case SIOCDELRT:
David Ahern333c4302017-05-21 10:12:04 -06003539 err = ip6_route_del(&cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 break;
3541 default:
3542 err = -EINVAL;
3543 }
3544 rtnl_unlock();
3545
3546 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
3549 return -EINVAL;
3550}
3551
3552/*
3553 * Drop the packet on the floor
3554 */
3555
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07003556static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003558 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00003559 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003560 switch (ipstats_mib_noroutes) {
3561 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07003562 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00003563 if (type == IPV6_ADDR_ANY) {
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -04003564 IP6_INC_STATS(dev_net(dst->dev),
3565 __in6_dev_get_safely(skb->dev),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07003566 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003567 break;
3568 }
3569 /* FALLTHROUGH */
3570 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07003571 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
3572 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003573 break;
3574 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00003575 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 kfree_skb(skb);
3577 return 0;
3578}
3579
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003580static int ip6_pkt_discard(struct sk_buff *skb)
3581{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003582 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003583}
3584
Eric W. Biedermanede20592015-10-07 16:48:47 -05003585static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
Eric Dumazetadf30902009-06-02 05:19:30 +00003587 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003588 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589}
3590
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003591static int ip6_pkt_prohibit(struct sk_buff *skb)
3592{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003593 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003594}
3595
Eric W. Biedermanede20592015-10-07 16:48:47 -05003596static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003597{
Eric Dumazetadf30902009-06-02 05:19:30 +00003598 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003599 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003600}
3601
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602/*
3603 * Allocate a dst for local (unicast / anycast) address.
3604 */
3605
David Ahern360a9882018-04-18 15:39:00 -07003606struct fib6_info *addrconf_f6i_alloc(struct net *net,
3607 struct inet6_dev *idev,
3608 const struct in6_addr *addr,
3609 bool anycast, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610{
David Ahernca254492015-10-12 11:47:10 -07003611 u32 tb_id;
David Ahern4832c302017-08-17 12:17:20 -07003612 struct net_device *dev = idev->dev;
David Ahern360a9882018-04-18 15:39:00 -07003613 struct fib6_info *f6i;
David Ahern5f02ce242016-09-10 12:09:54 -07003614
David Ahern360a9882018-04-18 15:39:00 -07003615 f6i = fib6_info_alloc(gfp_flags);
3616 if (!f6i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 return ERR_PTR(-ENOMEM);
3618
David Ahern360a9882018-04-18 15:39:00 -07003619 f6i->dst_nocount = true;
David Ahern360a9882018-04-18 15:39:00 -07003620 f6i->dst_host = true;
3621 f6i->fib6_protocol = RTPROT_KERNEL;
3622 f6i->fib6_flags = RTF_UP | RTF_NONEXTHOP;
David Aherne8478e82018-04-17 17:33:13 -07003623 if (anycast) {
David Ahern360a9882018-04-18 15:39:00 -07003624 f6i->fib6_type = RTN_ANYCAST;
3625 f6i->fib6_flags |= RTF_ANYCAST;
David Aherne8478e82018-04-17 17:33:13 -07003626 } else {
David Ahern360a9882018-04-18 15:39:00 -07003627 f6i->fib6_type = RTN_LOCAL;
3628 f6i->fib6_flags |= RTF_LOCAL;
David Aherne8478e82018-04-17 17:33:13 -07003629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
David Ahern360a9882018-04-18 15:39:00 -07003631 f6i->fib6_nh.nh_gw = *addr;
David Ahern93531c62018-04-17 17:33:25 -07003632 dev_hold(dev);
David Ahern360a9882018-04-18 15:39:00 -07003633 f6i->fib6_nh.nh_dev = dev;
3634 f6i->fib6_dst.addr = *addr;
3635 f6i->fib6_dst.plen = 128;
David Ahernca254492015-10-12 11:47:10 -07003636 tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL;
David Ahern360a9882018-04-18 15:39:00 -07003637 f6i->fib6_table = fib6_get_table(net, tb_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
David Ahern360a9882018-04-18 15:39:00 -07003639 return f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640}
3641
Daniel Walterc3968a82011-04-13 21:10:57 +00003642/* remove deleted ip from prefsrc entries */
3643struct arg_dev_net_ip {
3644 struct net_device *dev;
3645 struct net *net;
3646 struct in6_addr *addr;
3647};
3648
David Ahern8d1c8022018-04-17 17:33:26 -07003649static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
Daniel Walterc3968a82011-04-13 21:10:57 +00003650{
3651 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3652 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3653 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3654
David Ahern5e670d82018-04-17 17:33:14 -07003655 if (((void *)rt->fib6_nh.nh_dev == dev || !dev) &&
David Ahern421842e2018-04-17 17:33:18 -07003656 rt != net->ipv6.fib6_null_entry &&
David Ahern93c2fb22018-04-18 15:38:59 -07003657 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
Wei Wang60006a42017-10-06 12:05:58 -07003658 spin_lock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00003659 /* remove prefsrc entry */
David Ahern93c2fb22018-04-18 15:38:59 -07003660 rt->fib6_prefsrc.plen = 0;
Wei Wang60006a42017-10-06 12:05:58 -07003661 /* need to update cache as well */
3662 rt6_exceptions_remove_prefsrc(rt);
3663 spin_unlock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00003664 }
3665 return 0;
3666}
3667
3668void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
3669{
3670 struct net *net = dev_net(ifp->idev->dev);
3671 struct arg_dev_net_ip adni = {
3672 .dev = ifp->idev->dev,
3673 .net = net,
3674 .addr = &ifp->addr,
3675 };
Li RongQing0c3584d2013-12-27 16:32:38 +08003676 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00003677}
3678
Duan Jiongbe7a0102014-05-15 15:56:14 +08003679#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
Duan Jiongbe7a0102014-05-15 15:56:14 +08003680
3681/* Remove routers and update dst entries when gateway turn into host. */
David Ahern8d1c8022018-04-17 17:33:26 -07003682static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
Duan Jiongbe7a0102014-05-15 15:56:14 +08003683{
3684 struct in6_addr *gateway = (struct in6_addr *)arg;
3685
David Ahern93c2fb22018-04-18 15:38:59 -07003686 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Ahern5e670d82018-04-17 17:33:14 -07003687 ipv6_addr_equal(gateway, &rt->fib6_nh.nh_gw)) {
Duan Jiongbe7a0102014-05-15 15:56:14 +08003688 return -1;
3689 }
Wei Wangb16cb452017-10-06 12:06:00 -07003690
3691 /* Further clean up cached routes in exception table.
3692 * This is needed because cached route may have a different
3693 * gateway than its 'parent' in the case of an ip redirect.
3694 */
3695 rt6_exceptions_clean_tohost(rt, gateway);
3696
Duan Jiongbe7a0102014-05-15 15:56:14 +08003697 return 0;
3698}
3699
3700void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
3701{
3702 fib6_clean_all(net, fib6_clean_tohost, gateway);
3703}
3704
Ido Schimmel2127d952018-01-07 12:45:03 +02003705struct arg_netdev_event {
3706 const struct net_device *dev;
Ido Schimmel4c981e22018-01-07 12:45:04 +02003707 union {
3708 unsigned int nh_flags;
3709 unsigned long event;
3710 };
Ido Schimmel2127d952018-01-07 12:45:03 +02003711};
3712
David Ahern8d1c8022018-04-17 17:33:26 -07003713static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003714{
David Ahern8d1c8022018-04-17 17:33:26 -07003715 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003716 struct fib6_node *fn;
3717
David Ahern93c2fb22018-04-18 15:38:59 -07003718 fn = rcu_dereference_protected(rt->fib6_node,
3719 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003720 iter = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -07003721 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003722 while (iter) {
David Ahern93c2fb22018-04-18 15:38:59 -07003723 if (iter->fib6_metric == rt->fib6_metric &&
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003724 rt6_qualify_for_ecmp(iter))
3725 return iter;
3726 iter = rcu_dereference_protected(iter->rt6_next,
David Ahern93c2fb22018-04-18 15:38:59 -07003727 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003728 }
3729
3730 return NULL;
3731}
3732
David Ahern8d1c8022018-04-17 17:33:26 -07003733static bool rt6_is_dead(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003734{
David Ahern5e670d82018-04-17 17:33:14 -07003735 if (rt->fib6_nh.nh_flags & RTNH_F_DEAD ||
3736 (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN &&
David Aherndcd1f572018-04-18 15:39:05 -07003737 fib6_ignore_linkdown(rt)))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003738 return true;
3739
3740 return false;
3741}
3742
David Ahern8d1c8022018-04-17 17:33:26 -07003743static int rt6_multipath_total_weight(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003744{
David Ahern8d1c8022018-04-17 17:33:26 -07003745 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003746 int total = 0;
3747
3748 if (!rt6_is_dead(rt))
David Ahern5e670d82018-04-17 17:33:14 -07003749 total += rt->fib6_nh.nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003750
David Ahern93c2fb22018-04-18 15:38:59 -07003751 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003752 if (!rt6_is_dead(iter))
David Ahern5e670d82018-04-17 17:33:14 -07003753 total += iter->fib6_nh.nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003754 }
3755
3756 return total;
3757}
3758
David Ahern8d1c8022018-04-17 17:33:26 -07003759static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003760{
3761 int upper_bound = -1;
3762
3763 if (!rt6_is_dead(rt)) {
David Ahern5e670d82018-04-17 17:33:14 -07003764 *weight += rt->fib6_nh.nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003765 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
3766 total) - 1;
3767 }
David Ahern5e670d82018-04-17 17:33:14 -07003768 atomic_set(&rt->fib6_nh.nh_upper_bound, upper_bound);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003769}
3770
David Ahern8d1c8022018-04-17 17:33:26 -07003771static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003772{
David Ahern8d1c8022018-04-17 17:33:26 -07003773 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003774 int weight = 0;
3775
3776 rt6_upper_bound_set(rt, &weight, total);
3777
David Ahern93c2fb22018-04-18 15:38:59 -07003778 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003779 rt6_upper_bound_set(iter, &weight, total);
3780}
3781
David Ahern8d1c8022018-04-17 17:33:26 -07003782void rt6_multipath_rebalance(struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003783{
David Ahern8d1c8022018-04-17 17:33:26 -07003784 struct fib6_info *first;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003785 int total;
3786
3787 /* In case the entire multipath route was marked for flushing,
3788 * then there is no need to rebalance upon the removal of every
3789 * sibling route.
3790 */
David Ahern93c2fb22018-04-18 15:38:59 -07003791 if (!rt->fib6_nsiblings || rt->should_flush)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003792 return;
3793
3794 /* During lookup routes are evaluated in order, so we need to
3795 * make sure upper bounds are assigned from the first sibling
3796 * onwards.
3797 */
3798 first = rt6_multipath_first_sibling(rt);
3799 if (WARN_ON_ONCE(!first))
3800 return;
3801
3802 total = rt6_multipath_total_weight(first);
3803 rt6_multipath_upper_bound_set(first, total);
3804}
3805
David Ahern8d1c8022018-04-17 17:33:26 -07003806static int fib6_ifup(struct fib6_info *rt, void *p_arg)
Ido Schimmel2127d952018-01-07 12:45:03 +02003807{
3808 const struct arg_netdev_event *arg = p_arg;
David Ahern7aef6852018-04-17 17:33:10 -07003809 struct net *net = dev_net(arg->dev);
Ido Schimmel2127d952018-01-07 12:45:03 +02003810
David Ahern421842e2018-04-17 17:33:18 -07003811 if (rt != net->ipv6.fib6_null_entry && rt->fib6_nh.nh_dev == arg->dev) {
David Ahern5e670d82018-04-17 17:33:14 -07003812 rt->fib6_nh.nh_flags &= ~arg->nh_flags;
David Ahern7aef6852018-04-17 17:33:10 -07003813 fib6_update_sernum_upto_root(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003814 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02003815 }
Ido Schimmel2127d952018-01-07 12:45:03 +02003816
3817 return 0;
3818}
3819
3820void rt6_sync_up(struct net_device *dev, unsigned int nh_flags)
3821{
3822 struct arg_netdev_event arg = {
3823 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02003824 {
3825 .nh_flags = nh_flags,
3826 },
Ido Schimmel2127d952018-01-07 12:45:03 +02003827 };
3828
3829 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
3830 arg.nh_flags |= RTNH_F_LINKDOWN;
3831
3832 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
3833}
3834
David Ahern8d1c8022018-04-17 17:33:26 -07003835static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02003836 const struct net_device *dev)
3837{
David Ahern8d1c8022018-04-17 17:33:26 -07003838 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003839
David Ahern5e670d82018-04-17 17:33:14 -07003840 if (rt->fib6_nh.nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003841 return true;
David Ahern93c2fb22018-04-18 15:38:59 -07003842 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern5e670d82018-04-17 17:33:14 -07003843 if (iter->fib6_nh.nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003844 return true;
3845
3846 return false;
3847}
3848
David Ahern8d1c8022018-04-17 17:33:26 -07003849static void rt6_multipath_flush(struct fib6_info *rt)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003850{
David Ahern8d1c8022018-04-17 17:33:26 -07003851 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003852
3853 rt->should_flush = 1;
David Ahern93c2fb22018-04-18 15:38:59 -07003854 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003855 iter->should_flush = 1;
3856}
3857
David Ahern8d1c8022018-04-17 17:33:26 -07003858static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02003859 const struct net_device *down_dev)
3860{
David Ahern8d1c8022018-04-17 17:33:26 -07003861 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003862 unsigned int dead = 0;
3863
David Ahern5e670d82018-04-17 17:33:14 -07003864 if (rt->fib6_nh.nh_dev == down_dev ||
3865 rt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003866 dead++;
David Ahern93c2fb22018-04-18 15:38:59 -07003867 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern5e670d82018-04-17 17:33:14 -07003868 if (iter->fib6_nh.nh_dev == down_dev ||
3869 iter->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003870 dead++;
3871
3872 return dead;
3873}
3874
David Ahern8d1c8022018-04-17 17:33:26 -07003875static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02003876 const struct net_device *dev,
3877 unsigned int nh_flags)
3878{
David Ahern8d1c8022018-04-17 17:33:26 -07003879 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003880
David Ahern5e670d82018-04-17 17:33:14 -07003881 if (rt->fib6_nh.nh_dev == dev)
3882 rt->fib6_nh.nh_flags |= nh_flags;
David Ahern93c2fb22018-04-18 15:38:59 -07003883 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern5e670d82018-04-17 17:33:14 -07003884 if (iter->fib6_nh.nh_dev == dev)
3885 iter->fib6_nh.nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003886}
3887
David Aherna1a22c12017-01-18 07:40:36 -08003888/* called with write lock held for table with rt */
David Ahern8d1c8022018-04-17 17:33:26 -07003889static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890{
Ido Schimmel4c981e22018-01-07 12:45:04 +02003891 const struct arg_netdev_event *arg = p_arg;
3892 const struct net_device *dev = arg->dev;
David Ahern7aef6852018-04-17 17:33:10 -07003893 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003894
David Ahern421842e2018-04-17 17:33:18 -07003895 if (rt == net->ipv6.fib6_null_entry)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003896 return 0;
3897
3898 switch (arg->event) {
3899 case NETDEV_UNREGISTER:
David Ahern5e670d82018-04-17 17:33:14 -07003900 return rt->fib6_nh.nh_dev == dev ? -1 : 0;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003901 case NETDEV_DOWN:
Ido Schimmel1de178e2018-01-07 12:45:15 +02003902 if (rt->should_flush)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003903 return -1;
David Ahern93c2fb22018-04-18 15:38:59 -07003904 if (!rt->fib6_nsiblings)
David Ahern5e670d82018-04-17 17:33:14 -07003905 return rt->fib6_nh.nh_dev == dev ? -1 : 0;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003906 if (rt6_multipath_uses_dev(rt, dev)) {
3907 unsigned int count;
3908
3909 count = rt6_multipath_dead_count(rt, dev);
David Ahern93c2fb22018-04-18 15:38:59 -07003910 if (rt->fib6_nsiblings + 1 == count) {
Ido Schimmel1de178e2018-01-07 12:45:15 +02003911 rt6_multipath_flush(rt);
3912 return -1;
3913 }
3914 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
3915 RTNH_F_LINKDOWN);
David Ahern7aef6852018-04-17 17:33:10 -07003916 fib6_update_sernum(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003917 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02003918 }
3919 return -2;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003920 case NETDEV_CHANGE:
David Ahern5e670d82018-04-17 17:33:14 -07003921 if (rt->fib6_nh.nh_dev != dev ||
David Ahern93c2fb22018-04-18 15:38:59 -07003922 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003923 break;
David Ahern5e670d82018-04-17 17:33:14 -07003924 rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003925 rt6_multipath_rebalance(rt);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003926 break;
Ido Schimmel2b241362018-01-07 12:45:02 +02003927 }
David S. Millerc159d302011-12-26 15:24:36 -05003928
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 return 0;
3930}
3931
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003932void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933{
Ido Schimmel4c981e22018-01-07 12:45:04 +02003934 struct arg_netdev_event arg = {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003935 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02003936 {
3937 .event = event,
3938 },
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003939 };
3940
Ido Schimmel4c981e22018-01-07 12:45:04 +02003941 fib6_clean_all(dev_net(dev), fib6_ifdown, &arg);
3942}
3943
3944void rt6_disable_ip(struct net_device *dev, unsigned long event)
3945{
3946 rt6_sync_down_dev(dev, event);
3947 rt6_uncached_list_flush_dev(dev_net(dev), dev);
3948 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949}
3950
Eric Dumazet95c96172012-04-15 05:58:06 +00003951struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00003953 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954};
3955
David Ahern8d1c8022018-04-17 17:33:26 -07003956static int rt6_mtu_change_route(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957{
3958 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
3959 struct inet6_dev *idev;
3960
3961 /* In IPv6 pmtu discovery is not optional,
3962 so that RTAX_MTU lock cannot disable it.
3963 We still use this lock to block changes
3964 caused by addrconf/ndisc.
3965 */
3966
3967 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05003968 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 return 0;
3970
3971 /* For administrative MTU increase, there is no way to discover
3972 IPv6 PMTU increase, so PMTU increase should be updated here.
3973 Since RFC 1981 doesn't include administrative MTU increase
3974 update PMTU increase is a MUST. (i.e. jumbo frame)
3975 */
David Ahern5e670d82018-04-17 17:33:14 -07003976 if (rt->fib6_nh.nh_dev == arg->dev &&
David Ahernd4ead6b2018-04-17 17:33:16 -07003977 !fib6_metric_locked(rt, RTAX_MTU)) {
3978 u32 mtu = rt->fib6_pmtu;
3979
3980 if (mtu >= arg->mtu ||
3981 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
3982 fib6_metric_set(rt, RTAX_MTU, arg->mtu);
3983
Wei Wangf5bbe7e2017-10-06 12:05:59 -07003984 spin_lock_bh(&rt6_exception_lock);
Stefano Brivioe9fa1492018-03-06 11:10:19 +01003985 rt6_exceptions_update_pmtu(idev, rt, arg->mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07003986 spin_unlock_bh(&rt6_exception_lock);
Simon Arlott566cfd82007-07-26 00:09:55 -07003987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 return 0;
3989}
3990
Eric Dumazet95c96172012-04-15 05:58:06 +00003991void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992{
Thomas Grafc71099a2006-08-04 23:20:06 -07003993 struct rt6_mtu_change_arg arg = {
3994 .dev = dev,
3995 .mtu = mtu,
3996 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
Li RongQing0c3584d2013-12-27 16:32:38 +08003998 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999}
4000
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004001static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07004002 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07004003 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07004004 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004005 [RTA_PRIORITY] = { .type = NLA_U32 },
4006 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004007 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004008 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004009 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4010 [RTA_ENCAP] = { .type = NLA_NESTED },
Xin Long32bc2012015-12-16 17:50:11 +08004011 [RTA_EXPIRES] = { .type = NLA_U32 },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004012 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +08004013 [RTA_MARK] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004014};
4015
4016static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
David Ahern333c4302017-05-21 10:12:04 -06004017 struct fib6_config *cfg,
4018 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
Thomas Graf86872cb2006-08-22 00:01:08 -07004020 struct rtmsg *rtm;
4021 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004022 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07004023 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
Johannes Bergfceb6432017-04-12 14:34:07 +02004025 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
4026 NULL);
Thomas Graf86872cb2006-08-22 00:01:08 -07004027 if (err < 0)
4028 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Thomas Graf86872cb2006-08-22 00:01:08 -07004030 err = -EINVAL;
4031 rtm = nlmsg_data(nlh);
4032 memset(cfg, 0, sizeof(*cfg));
4033
4034 cfg->fc_table = rtm->rtm_table;
4035 cfg->fc_dst_len = rtm->rtm_dst_len;
4036 cfg->fc_src_len = rtm->rtm_src_len;
4037 cfg->fc_flags = RTF_UP;
4038 cfg->fc_protocol = rtm->rtm_protocol;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004039 cfg->fc_type = rtm->rtm_type;
Thomas Graf86872cb2006-08-22 00:01:08 -07004040
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004041 if (rtm->rtm_type == RTN_UNREACHABLE ||
4042 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00004043 rtm->rtm_type == RTN_PROHIBIT ||
4044 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07004045 cfg->fc_flags |= RTF_REJECT;
4046
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00004047 if (rtm->rtm_type == RTN_LOCAL)
4048 cfg->fc_flags |= RTF_LOCAL;
4049
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07004050 if (rtm->rtm_flags & RTM_F_CLONED)
4051 cfg->fc_flags |= RTF_CACHE;
4052
David Ahernfc1e64e2018-01-25 16:55:09 -08004053 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4054
Eric W. Biederman15e47302012-09-07 20:12:54 +00004055 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
Thomas Graf86872cb2006-08-22 00:01:08 -07004056 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004057 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07004058
4059 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004060 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07004061 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004063
4064 if (tb[RTA_DST]) {
4065 int plen = (rtm->rtm_dst_len + 7) >> 3;
4066
4067 if (nla_len(tb[RTA_DST]) < plen)
4068 goto errout;
4069
4070 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004072
4073 if (tb[RTA_SRC]) {
4074 int plen = (rtm->rtm_src_len + 7) >> 3;
4075
4076 if (nla_len(tb[RTA_SRC]) < plen)
4077 goto errout;
4078
4079 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004081
Daniel Walterc3968a82011-04-13 21:10:57 +00004082 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02004083 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00004084
Thomas Graf86872cb2006-08-22 00:01:08 -07004085 if (tb[RTA_OIF])
4086 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4087
4088 if (tb[RTA_PRIORITY])
4089 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4090
4091 if (tb[RTA_METRICS]) {
4092 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4093 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004095
4096 if (tb[RTA_TABLE])
4097 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4098
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004099 if (tb[RTA_MULTIPATH]) {
4100 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4101 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
David Ahern9ed59592017-01-17 14:57:36 -08004102
4103 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
David Ahernc255bd62017-05-27 16:19:27 -06004104 cfg->fc_mp_len, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004105 if (err < 0)
4106 goto errout;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004107 }
4108
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004109 if (tb[RTA_PREF]) {
4110 pref = nla_get_u8(tb[RTA_PREF]);
4111 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4112 pref != ICMPV6_ROUTER_PREF_HIGH)
4113 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4114 cfg->fc_flags |= RTF_PREF(pref);
4115 }
4116
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004117 if (tb[RTA_ENCAP])
4118 cfg->fc_encap = tb[RTA_ENCAP];
4119
David Ahern9ed59592017-01-17 14:57:36 -08004120 if (tb[RTA_ENCAP_TYPE]) {
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004121 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
4122
David Ahernc255bd62017-05-27 16:19:27 -06004123 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004124 if (err < 0)
4125 goto errout;
4126 }
4127
Xin Long32bc2012015-12-16 17:50:11 +08004128 if (tb[RTA_EXPIRES]) {
4129 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
4130
4131 if (addrconf_finite_timeout(timeout)) {
4132 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
4133 cfg->fc_flags |= RTF_EXPIRES;
4134 }
4135 }
4136
Thomas Graf86872cb2006-08-22 00:01:08 -07004137 err = 0;
4138errout:
4139 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140}
4141
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004142struct rt6_nh {
David Ahern8d1c8022018-04-17 17:33:26 -07004143 struct fib6_info *fib6_info;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004144 struct fib6_config r_cfg;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004145 struct list_head next;
4146};
4147
4148static void ip6_print_replace_route_err(struct list_head *rt6_nh_list)
4149{
4150 struct rt6_nh *nh;
4151
4152 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern7d4d5062017-02-02 12:37:12 -08004153 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 -07004154 &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway,
4155 nh->r_cfg.fc_ifindex);
4156 }
4157}
4158
David Ahernd4ead6b2018-04-17 17:33:16 -07004159static int ip6_route_info_append(struct net *net,
4160 struct list_head *rt6_nh_list,
David Ahern8d1c8022018-04-17 17:33:26 -07004161 struct fib6_info *rt,
4162 struct fib6_config *r_cfg)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004163{
4164 struct rt6_nh *nh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004165 int err = -EEXIST;
4166
4167 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004168 /* check if fib6_info already exists */
4169 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004170 return err;
4171 }
4172
4173 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
4174 if (!nh)
4175 return -ENOMEM;
David Ahern8d1c8022018-04-17 17:33:26 -07004176 nh->fib6_info = rt;
David Ahernd4ead6b2018-04-17 17:33:16 -07004177 err = ip6_convert_metrics(net, rt, r_cfg);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004178 if (err) {
4179 kfree(nh);
4180 return err;
4181 }
4182 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
4183 list_add_tail(&nh->next, rt6_nh_list);
4184
4185 return 0;
4186}
4187
David Ahern8d1c8022018-04-17 17:33:26 -07004188static void ip6_route_mpath_notify(struct fib6_info *rt,
4189 struct fib6_info *rt_last,
David Ahern3b1137f2017-02-02 12:37:10 -08004190 struct nl_info *info,
4191 __u16 nlflags)
4192{
4193 /* if this is an APPEND route, then rt points to the first route
4194 * inserted and rt_last points to last route inserted. Userspace
4195 * wants a consistent dump of the route which starts at the first
4196 * nexthop. Since sibling routes are always added at the end of
4197 * the list, find the first sibling of the last route appended
4198 */
David Ahern93c2fb22018-04-18 15:38:59 -07004199 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
4200 rt = list_first_entry(&rt_last->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -07004201 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -07004202 fib6_siblings);
David Ahern3b1137f2017-02-02 12:37:10 -08004203 }
4204
4205 if (rt)
4206 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
4207}
4208
David Ahern333c4302017-05-21 10:12:04 -06004209static int ip6_route_multipath_add(struct fib6_config *cfg,
4210 struct netlink_ext_ack *extack)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004211{
David Ahern8d1c8022018-04-17 17:33:26 -07004212 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
David Ahern3b1137f2017-02-02 12:37:10 -08004213 struct nl_info *info = &cfg->fc_nlinfo;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004214 struct fib6_config r_cfg;
4215 struct rtnexthop *rtnh;
David Ahern8d1c8022018-04-17 17:33:26 -07004216 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004217 struct rt6_nh *err_nh;
4218 struct rt6_nh *nh, *nh_safe;
David Ahern3b1137f2017-02-02 12:37:10 -08004219 __u16 nlflags;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004220 int remaining;
4221 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004222 int err = 1;
4223 int nhn = 0;
4224 int replace = (cfg->fc_nlinfo.nlh &&
4225 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
4226 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004227
David Ahern3b1137f2017-02-02 12:37:10 -08004228 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
4229 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
4230 nlflags |= NLM_F_APPEND;
4231
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02004232 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004233 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004234
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004235 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
David Ahern8d1c8022018-04-17 17:33:26 -07004236 * fib6_info structs per nexthop
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004237 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004238 while (rtnh_ok(rtnh, remaining)) {
4239 memcpy(&r_cfg, cfg, sizeof(*cfg));
4240 if (rtnh->rtnh_ifindex)
4241 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
4242
4243 attrlen = rtnh_attrlen(rtnh);
4244 if (attrlen > 0) {
4245 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4246
4247 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
4248 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004249 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004250 r_cfg.fc_flags |= RTF_GATEWAY;
4251 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004252 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
4253 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
4254 if (nla)
4255 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004256 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004257
David Ahern68e2ffd2018-03-20 10:06:59 -07004258 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
David Ahernacb54e32018-04-17 17:33:22 -07004259 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07004260 if (IS_ERR(rt)) {
4261 err = PTR_ERR(rt);
4262 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004263 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07004264 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004265
David Ahern5e670d82018-04-17 17:33:14 -07004266 rt->fib6_nh.nh_weight = rtnh->rtnh_hops + 1;
Ido Schimmel398958a2018-01-09 16:40:28 +02004267
David Ahernd4ead6b2018-04-17 17:33:16 -07004268 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4269 rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004270 if (err) {
David Ahern93531c62018-04-17 17:33:25 -07004271 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004272 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004273 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004274
4275 rtnh = rtnh_next(rtnh, &remaining);
4276 }
4277
David Ahern3b1137f2017-02-02 12:37:10 -08004278 /* for add and replace send one notification with all nexthops.
4279 * Skip the notification in fib6_add_rt2node and send one with
4280 * the full route when done
4281 */
4282 info->skip_notify = 1;
4283
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004284 err_nh = NULL;
4285 list_for_each_entry(nh, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004286 rt_last = nh->fib6_info;
4287 err = __ip6_ins_rt(nh->fib6_info, info, extack);
4288 fib6_info_release(nh->fib6_info);
David Ahern93531c62018-04-17 17:33:25 -07004289
David Ahern3b1137f2017-02-02 12:37:10 -08004290 /* save reference to first route for notification */
4291 if (!rt_notif && !err)
David Ahern8d1c8022018-04-17 17:33:26 -07004292 rt_notif = nh->fib6_info;
David Ahern3b1137f2017-02-02 12:37:10 -08004293
David Ahern8d1c8022018-04-17 17:33:26 -07004294 /* nh->fib6_info is used or freed at this point, reset to NULL*/
4295 nh->fib6_info = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004296 if (err) {
4297 if (replace && nhn)
4298 ip6_print_replace_route_err(&rt6_nh_list);
4299 err_nh = nh;
4300 goto add_errout;
4301 }
4302
Nicolas Dichtel1a724182012-11-01 22:58:22 +00004303 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02004304 * these flags after the first nexthop: if there is a collision,
4305 * we have already failed to add the first nexthop:
4306 * fib6_add_rt2node() has rejected it; when replacing, old
4307 * nexthops have been replaced by first new, the rest should
4308 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00004309 */
Michal Kubeček27596472015-05-18 20:54:00 +02004310 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
4311 NLM_F_REPLACE);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004312 nhn++;
4313 }
4314
David Ahern3b1137f2017-02-02 12:37:10 -08004315 /* success ... tell user about new route */
4316 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004317 goto cleanup;
4318
4319add_errout:
David Ahern3b1137f2017-02-02 12:37:10 -08004320 /* send notification for routes that were added so that
4321 * the delete notifications sent by ip6_route_del are
4322 * coherent
4323 */
4324 if (rt_notif)
4325 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
4326
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004327 /* Delete routes that were already added */
4328 list_for_each_entry(nh, &rt6_nh_list, next) {
4329 if (err_nh == nh)
4330 break;
David Ahern333c4302017-05-21 10:12:04 -06004331 ip6_route_del(&nh->r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004332 }
4333
4334cleanup:
4335 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004336 if (nh->fib6_info)
4337 fib6_info_release(nh->fib6_info);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004338 list_del(&nh->next);
4339 kfree(nh);
4340 }
4341
4342 return err;
4343}
4344
David Ahern333c4302017-05-21 10:12:04 -06004345static int ip6_route_multipath_del(struct fib6_config *cfg,
4346 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004347{
4348 struct fib6_config r_cfg;
4349 struct rtnexthop *rtnh;
4350 int remaining;
4351 int attrlen;
4352 int err = 1, last_err = 0;
4353
4354 remaining = cfg->fc_mp_len;
4355 rtnh = (struct rtnexthop *)cfg->fc_mp;
4356
4357 /* Parse a Multipath Entry */
4358 while (rtnh_ok(rtnh, remaining)) {
4359 memcpy(&r_cfg, cfg, sizeof(*cfg));
4360 if (rtnh->rtnh_ifindex)
4361 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
4362
4363 attrlen = rtnh_attrlen(rtnh);
4364 if (attrlen > 0) {
4365 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4366
4367 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
4368 if (nla) {
4369 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
4370 r_cfg.fc_flags |= RTF_GATEWAY;
4371 }
4372 }
David Ahern333c4302017-05-21 10:12:04 -06004373 err = ip6_route_del(&r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004374 if (err)
4375 last_err = err;
4376
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004377 rtnh = rtnh_next(rtnh, &remaining);
4378 }
4379
4380 return last_err;
4381}
4382
David Ahernc21ef3e2017-04-16 09:48:24 -07004383static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4384 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385{
Thomas Graf86872cb2006-08-22 00:01:08 -07004386 struct fib6_config cfg;
4387 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
David Ahern333c4302017-05-21 10:12:04 -06004389 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004390 if (err < 0)
4391 return err;
4392
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004393 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06004394 return ip6_route_multipath_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08004395 else {
4396 cfg.fc_delete_all_nh = 1;
David Ahern333c4302017-05-21 10:12:04 -06004397 return ip6_route_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08004398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399}
4400
David Ahernc21ef3e2017-04-16 09:48:24 -07004401static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4402 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403{
Thomas Graf86872cb2006-08-22 00:01:08 -07004404 struct fib6_config cfg;
4405 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406
David Ahern333c4302017-05-21 10:12:04 -06004407 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004408 if (err < 0)
4409 return err;
4410
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004411 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06004412 return ip6_route_multipath_add(&cfg, extack);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004413 else
David Ahernacb54e32018-04-17 17:33:22 -07004414 return ip6_route_add(&cfg, GFP_KERNEL, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415}
4416
David Ahern8d1c8022018-04-17 17:33:26 -07004417static size_t rt6_nlmsg_size(struct fib6_info *rt)
Thomas Graf339bf982006-11-10 14:10:15 -08004418{
David Ahernbeb1afac52017-02-02 12:37:09 -08004419 int nexthop_len = 0;
4420
David Ahern93c2fb22018-04-18 15:38:59 -07004421 if (rt->fib6_nsiblings) {
David Ahernbeb1afac52017-02-02 12:37:09 -08004422 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
4423 + NLA_ALIGN(sizeof(struct rtnexthop))
4424 + nla_total_size(16) /* RTA_GATEWAY */
David Ahern5e670d82018-04-17 17:33:14 -07004425 + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate);
David Ahernbeb1afac52017-02-02 12:37:09 -08004426
David Ahern93c2fb22018-04-18 15:38:59 -07004427 nexthop_len *= rt->fib6_nsiblings;
David Ahernbeb1afac52017-02-02 12:37:09 -08004428 }
4429
Thomas Graf339bf982006-11-10 14:10:15 -08004430 return NLMSG_ALIGN(sizeof(struct rtmsg))
4431 + nla_total_size(16) /* RTA_SRC */
4432 + nla_total_size(16) /* RTA_DST */
4433 + nla_total_size(16) /* RTA_GATEWAY */
4434 + nla_total_size(16) /* RTA_PREFSRC */
4435 + nla_total_size(4) /* RTA_TABLE */
4436 + nla_total_size(4) /* RTA_IIF */
4437 + nla_total_size(4) /* RTA_OIF */
4438 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08004439 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01004440 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004441 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004442 + nla_total_size(1) /* RTA_PREF */
David Ahern5e670d82018-04-17 17:33:14 -07004443 + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate)
David Ahernbeb1afac52017-02-02 12:37:09 -08004444 + nexthop_len;
4445}
4446
David Ahern8d1c8022018-04-17 17:33:26 -07004447static int rt6_nexthop_info(struct sk_buff *skb, struct fib6_info *rt,
David Ahern5be083c2017-03-06 15:57:31 -08004448 unsigned int *flags, bool skip_oif)
David Ahernbeb1afac52017-02-02 12:37:09 -08004449{
David Ahern5e670d82018-04-17 17:33:14 -07004450 if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmelf9d882e2018-01-07 12:45:10 +02004451 *flags |= RTNH_F_DEAD;
4452
David Ahern5e670d82018-04-17 17:33:14 -07004453 if (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN) {
David Ahernbeb1afac52017-02-02 12:37:09 -08004454 *flags |= RTNH_F_LINKDOWN;
David Aherndcd1f572018-04-18 15:39:05 -07004455
4456 rcu_read_lock();
4457 if (fib6_ignore_linkdown(rt))
David Ahernbeb1afac52017-02-02 12:37:09 -08004458 *flags |= RTNH_F_DEAD;
David Aherndcd1f572018-04-18 15:39:05 -07004459 rcu_read_unlock();
David Ahernbeb1afac52017-02-02 12:37:09 -08004460 }
4461
David Ahern93c2fb22018-04-18 15:38:59 -07004462 if (rt->fib6_flags & RTF_GATEWAY) {
David Ahern5e670d82018-04-17 17:33:14 -07004463 if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->fib6_nh.nh_gw) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004464 goto nla_put_failure;
4465 }
4466
David Ahern5e670d82018-04-17 17:33:14 -07004467 *flags |= (rt->fib6_nh.nh_flags & RTNH_F_ONLINK);
4468 if (rt->fib6_nh.nh_flags & RTNH_F_OFFLOAD)
Ido Schimmel61e4d012017-08-03 13:28:20 +02004469 *flags |= RTNH_F_OFFLOAD;
4470
David Ahern5be083c2017-03-06 15:57:31 -08004471 /* not needed for multipath encoding b/c it has a rtnexthop struct */
David Ahern5e670d82018-04-17 17:33:14 -07004472 if (!skip_oif && rt->fib6_nh.nh_dev &&
4473 nla_put_u32(skb, RTA_OIF, rt->fib6_nh.nh_dev->ifindex))
David Ahernbeb1afac52017-02-02 12:37:09 -08004474 goto nla_put_failure;
4475
David Ahern5e670d82018-04-17 17:33:14 -07004476 if (rt->fib6_nh.nh_lwtstate &&
4477 lwtunnel_fill_encap(skb, rt->fib6_nh.nh_lwtstate) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004478 goto nla_put_failure;
4479
4480 return 0;
4481
4482nla_put_failure:
4483 return -EMSGSIZE;
4484}
4485
David Ahern5be083c2017-03-06 15:57:31 -08004486/* add multipath next hop */
David Ahern8d1c8022018-04-17 17:33:26 -07004487static int rt6_add_nexthop(struct sk_buff *skb, struct fib6_info *rt)
David Ahernbeb1afac52017-02-02 12:37:09 -08004488{
David Ahern5e670d82018-04-17 17:33:14 -07004489 const struct net_device *dev = rt->fib6_nh.nh_dev;
David Ahernbeb1afac52017-02-02 12:37:09 -08004490 struct rtnexthop *rtnh;
4491 unsigned int flags = 0;
4492
4493 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
4494 if (!rtnh)
4495 goto nla_put_failure;
4496
David Ahern5e670d82018-04-17 17:33:14 -07004497 rtnh->rtnh_hops = rt->fib6_nh.nh_weight - 1;
4498 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
David Ahernbeb1afac52017-02-02 12:37:09 -08004499
David Ahern5be083c2017-03-06 15:57:31 -08004500 if (rt6_nexthop_info(skb, rt, &flags, true) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004501 goto nla_put_failure;
4502
4503 rtnh->rtnh_flags = flags;
4504
4505 /* length of rtnetlink header + attributes */
4506 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
4507
4508 return 0;
4509
4510nla_put_failure:
4511 return -EMSGSIZE;
Thomas Graf339bf982006-11-10 14:10:15 -08004512}
4513
David Ahernd4ead6b2018-04-17 17:33:16 -07004514static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -07004515 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -07004516 struct in6_addr *dest, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004517 int iif, int type, u32 portid, u32 seq,
David Ahernf8cfe2c2017-01-17 15:51:08 -08004518 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519{
4520 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004521 struct nlmsghdr *nlh;
David Ahernd4ead6b2018-04-17 17:33:16 -07004522 long expires = 0;
4523 u32 *pmetrics;
Patrick McHardy9e762a42006-08-10 23:09:48 -07004524 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
Eric W. Biederman15e47302012-09-07 20:12:54 +00004526 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05004527 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004528 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004529
4530 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 rtm->rtm_family = AF_INET6;
David Ahern93c2fb22018-04-18 15:38:59 -07004532 rtm->rtm_dst_len = rt->fib6_dst.plen;
4533 rtm->rtm_src_len = rt->fib6_src.plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 rtm->rtm_tos = 0;
David Ahern93c2fb22018-04-18 15:38:59 -07004535 if (rt->fib6_table)
4536 table = rt->fib6_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07004537 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07004538 table = RT6_TABLE_UNSPEC;
4539 rtm->rtm_table = table;
David S. Millerc78679e2012-04-01 20:27:33 -04004540 if (nla_put_u32(skb, RTA_TABLE, table))
4541 goto nla_put_failure;
David Aherne8478e82018-04-17 17:33:13 -07004542
4543 rtm->rtm_type = rt->fib6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 rtm->rtm_flags = 0;
4545 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
David Ahern93c2fb22018-04-18 15:38:59 -07004546 rtm->rtm_protocol = rt->fib6_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
David Ahern93c2fb22018-04-18 15:38:59 -07004548 if (rt->fib6_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 rtm->rtm_flags |= RTM_F_CLONED;
4550
David Ahernd4ead6b2018-04-17 17:33:16 -07004551 if (dest) {
4552 if (nla_put_in6_addr(skb, RTA_DST, dest))
David S. Millerc78679e2012-04-01 20:27:33 -04004553 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004554 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 } else if (rtm->rtm_dst_len)
David Ahern93c2fb22018-04-18 15:38:59 -07004556 if (nla_put_in6_addr(skb, RTA_DST, &rt->fib6_dst.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04004557 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558#ifdef CONFIG_IPV6_SUBTREES
4559 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02004560 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04004561 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004562 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04004563 } else if (rtm->rtm_src_len &&
David Ahern93c2fb22018-04-18 15:38:59 -07004564 nla_put_in6_addr(skb, RTA_SRC, &rt->fib6_src.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04004565 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004567 if (iif) {
4568#ifdef CONFIG_IPV6_MROUTE
David Ahern93c2fb22018-04-18 15:38:59 -07004569 if (ipv6_addr_is_multicast(&rt->fib6_dst.addr)) {
David Ahernfd61c6b2017-01-17 15:51:07 -08004570 int err = ip6mr_get_route(net, skb, rtm, portid);
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02004571
David Ahernfd61c6b2017-01-17 15:51:07 -08004572 if (err == 0)
4573 return 0;
4574 if (err < 0)
4575 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004576 } else
4577#endif
David S. Millerc78679e2012-04-01 20:27:33 -04004578 if (nla_put_u32(skb, RTA_IIF, iif))
4579 goto nla_put_failure;
David Ahernd4ead6b2018-04-17 17:33:16 -07004580 } else if (dest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 struct in6_addr saddr_buf;
David Ahernd4ead6b2018-04-17 17:33:16 -07004582 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02004583 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04004584 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07004586
David Ahern93c2fb22018-04-18 15:38:59 -07004587 if (rt->fib6_prefsrc.plen) {
Daniel Walterc3968a82011-04-13 21:10:57 +00004588 struct in6_addr saddr_buf;
David Ahern93c2fb22018-04-18 15:38:59 -07004589 saddr_buf = rt->fib6_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02004590 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04004591 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00004592 }
4593
David Ahernd4ead6b2018-04-17 17:33:16 -07004594 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
4595 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07004596 goto nla_put_failure;
4597
David Ahern93c2fb22018-04-18 15:38:59 -07004598 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
David S. Millerc78679e2012-04-01 20:27:33 -04004599 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00004600
David Ahernbeb1afac52017-02-02 12:37:09 -08004601 /* For multipath routes, walk the siblings list and add
4602 * each as a nexthop within RTA_MULTIPATH.
4603 */
David Ahern93c2fb22018-04-18 15:38:59 -07004604 if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07004605 struct fib6_info *sibling, *next_sibling;
David Ahernbeb1afac52017-02-02 12:37:09 -08004606 struct nlattr *mp;
4607
4608 mp = nla_nest_start(skb, RTA_MULTIPATH);
4609 if (!mp)
4610 goto nla_put_failure;
4611
4612 if (rt6_add_nexthop(skb, rt) < 0)
4613 goto nla_put_failure;
4614
4615 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07004616 &rt->fib6_siblings, fib6_siblings) {
David Ahernbeb1afac52017-02-02 12:37:09 -08004617 if (rt6_add_nexthop(skb, sibling) < 0)
4618 goto nla_put_failure;
4619 }
4620
4621 nla_nest_end(skb, mp);
4622 } else {
David Ahern5be083c2017-03-06 15:57:31 -08004623 if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004624 goto nla_put_failure;
4625 }
4626
David Ahern93c2fb22018-04-18 15:38:59 -07004627 if (rt->fib6_flags & RTF_EXPIRES) {
David Ahern14895682018-04-17 17:33:17 -07004628 expires = dst ? dst->expires : rt->expires;
4629 expires -= jiffies;
4630 }
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07004631
David Ahernd4ead6b2018-04-17 17:33:16 -07004632 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08004633 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
David Ahern93c2fb22018-04-18 15:38:59 -07004635 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->fib6_flags)))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004636 goto nla_put_failure;
4637
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004638
Johannes Berg053c0952015-01-16 22:09:00 +01004639 nlmsg_end(skb, nlh);
4640 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004641
4642nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004643 nlmsg_cancel(skb, nlh);
4644 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645}
4646
David Ahern8d1c8022018-04-17 17:33:26 -07004647int rt6_dump_route(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
4649 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
David Ahern1f17e2f2017-01-26 13:54:08 -08004650 struct net *net = arg->net;
4651
David Ahern421842e2018-04-17 17:33:18 -07004652 if (rt == net->ipv6.fib6_null_entry)
David Ahern1f17e2f2017-01-26 13:54:08 -08004653 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
Thomas Graf2d7202b2006-08-22 00:01:27 -07004655 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
4656 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
David Ahernf8cfe2c2017-01-17 15:51:08 -08004657
4658 /* user wants prefix routes only */
4659 if (rtm->rtm_flags & RTM_F_PREFIX &&
David Ahern93c2fb22018-04-18 15:38:59 -07004660 !(rt->fib6_flags & RTF_PREFIX_RT)) {
David Ahernf8cfe2c2017-01-17 15:51:08 -08004661 /* success since this is not a prefix route */
4662 return 1;
4663 }
4664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
David Ahernd4ead6b2018-04-17 17:33:16 -07004666 return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
4667 RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
4668 arg->cb->nlh->nlmsg_seq, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669}
4670
David Ahernc21ef3e2017-04-16 09:48:24 -07004671static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4672 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004674 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07004675 struct nlattr *tb[RTA_MAX+1];
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004676 int err, iif = 0, oif = 0;
4677 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07004679 struct sk_buff *skb;
4680 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05004681 struct flowi6 fl6;
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004682 bool fibmatch;
Thomas Grafab364a62006-08-22 00:01:47 -07004683
Johannes Bergfceb6432017-04-12 14:34:07 +02004684 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07004685 extack);
Thomas Grafab364a62006-08-22 00:01:47 -07004686 if (err < 0)
4687 goto errout;
4688
4689 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05004690 memset(&fl6, 0, sizeof(fl6));
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02004691 rtm = nlmsg_data(nlh);
4692 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004693 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
Thomas Grafab364a62006-08-22 00:01:47 -07004694
4695 if (tb[RTA_SRC]) {
4696 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4697 goto errout;
4698
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004699 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07004700 }
4701
4702 if (tb[RTA_DST]) {
4703 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4704 goto errout;
4705
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004706 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07004707 }
4708
4709 if (tb[RTA_IIF])
4710 iif = nla_get_u32(tb[RTA_IIF]);
4711
4712 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004713 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07004714
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07004715 if (tb[RTA_MARK])
4716 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
4717
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004718 if (tb[RTA_UID])
4719 fl6.flowi6_uid = make_kuid(current_user_ns(),
4720 nla_get_u32(tb[RTA_UID]));
4721 else
4722 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
4723
Thomas Grafab364a62006-08-22 00:01:47 -07004724 if (iif) {
4725 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004726 int flags = 0;
4727
Florian Westphal121622d2017-08-15 16:34:42 +02004728 rcu_read_lock();
4729
4730 dev = dev_get_by_index_rcu(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07004731 if (!dev) {
Florian Westphal121622d2017-08-15 16:34:42 +02004732 rcu_read_unlock();
Thomas Grafab364a62006-08-22 00:01:47 -07004733 err = -ENODEV;
4734 goto errout;
4735 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004736
4737 fl6.flowi6_iif = iif;
4738
4739 if (!ipv6_addr_any(&fl6.saddr))
4740 flags |= RT6_LOOKUP_F_HAS_SADDR;
4741
David Ahernb75cc8f2018-03-02 08:32:17 -08004742 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
Florian Westphal121622d2017-08-15 16:34:42 +02004743
4744 rcu_read_unlock();
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004745 } else {
4746 fl6.flowi6_oif = oif;
4747
Ido Schimmel58acfd72017-12-20 12:28:25 +02004748 dst = ip6_route_output(net, NULL, &fl6);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004749 }
4750
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004751
4752 rt = container_of(dst, struct rt6_info, dst);
4753 if (rt->dst.error) {
4754 err = rt->dst.error;
4755 ip6_rt_put(rt);
4756 goto errout;
Thomas Grafab364a62006-08-22 00:01:47 -07004757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
WANG Cong9d6acb32017-03-01 20:48:39 -08004759 if (rt == net->ipv6.ip6_null_entry) {
4760 err = rt->dst.error;
4761 ip6_rt_put(rt);
4762 goto errout;
4763 }
4764
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05004766 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00004767 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07004768 err = -ENOBUFS;
4769 goto errout;
4770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
Changli Gaod8d1f302010-06-10 23:31:35 -07004772 skb_dst_set(skb, &rt->dst);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004773 if (fibmatch)
David Ahern93531c62018-04-17 17:33:25 -07004774 err = rt6_fill_node(net, skb, rt->from, NULL, NULL, NULL, iif,
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004775 RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
4776 nlh->nlmsg_seq, 0);
4777 else
David Ahern93531c62018-04-17 17:33:25 -07004778 err = rt6_fill_node(net, skb, rt->from, dst,
4779 &fl6.daddr, &fl6.saddr, iif, RTM_NEWROUTE,
David Ahernd4ead6b2018-04-17 17:33:16 -07004780 NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
4781 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07004783 kfree_skb(skb);
4784 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 }
4786
Eric W. Biederman15e47302012-09-07 20:12:54 +00004787 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07004788errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790}
4791
David Ahern8d1c8022018-04-17 17:33:26 -07004792void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07004793 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794{
4795 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08004796 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08004797 u32 seq;
4798 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08004800 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05004801 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07004802
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004803 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05004804 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07004805 goto errout;
4806
David Ahernd4ead6b2018-04-17 17:33:16 -07004807 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
4808 event, info->portid, seq, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08004809 if (err < 0) {
4810 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
4811 WARN_ON(err == -EMSGSIZE);
4812 kfree_skb(skb);
4813 goto errout;
4814 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004815 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004816 info->nlh, gfp_any());
4817 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07004818errout:
4819 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08004820 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821}
4822
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004823static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00004824 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004825{
Jiri Pirko351638e2013-05-28 01:30:21 +00004826 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004827 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004828
WANG Cong242d3a42017-05-08 10:12:13 -07004829 if (!(dev->flags & IFF_LOOPBACK))
4830 return NOTIFY_OK;
4831
4832 if (event == NETDEV_REGISTER) {
David Ahern421842e2018-04-17 17:33:18 -07004833 net->ipv6.fib6_null_entry->fib6_nh.nh_dev = dev;
Changli Gaod8d1f302010-06-10 23:31:35 -07004834 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004835 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
4836#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07004837 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004838 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07004839 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004840 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
4841#endif
WANG Cong76da0702017-06-20 11:42:27 -07004842 } else if (event == NETDEV_UNREGISTER &&
4843 dev->reg_state != NETREG_UNREGISTERED) {
4844 /* NETDEV_UNREGISTER could be fired for multiple times by
4845 * netdev_wait_allrefs(). Make sure we only call this once.
4846 */
Eric Dumazet12d94a82017-08-15 04:09:51 -07004847 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07004848#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric Dumazet12d94a82017-08-15 04:09:51 -07004849 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
4850 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07004851#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004852 }
4853
4854 return NOTIFY_OK;
4855}
4856
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857/*
4858 * /proc
4859 */
4860
4861#ifdef CONFIG_PROC_FS
4862
Alexey Dobriyan33120b32007-11-06 05:27:11 -08004863static const struct file_operations ipv6_route_proc_fops = {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08004864 .open = ipv6_route_open,
4865 .read = seq_read,
4866 .llseek = seq_lseek,
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02004867 .release = seq_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08004868};
4869
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870static int rt6_stats_seq_show(struct seq_file *seq, void *v)
4871{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004872 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004874 net->ipv6.rt6_stats->fib_nodes,
4875 net->ipv6.rt6_stats->fib_route_nodes,
Wei Wang81eb8442017-10-06 12:06:11 -07004876 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004877 net->ipv6.rt6_stats->fib_rt_entries,
4878 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00004879 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004880 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
4882 return 0;
4883}
4884
4885static int rt6_stats_seq_open(struct inode *inode, struct file *file)
4886{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07004887 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004888}
4889
Arjan van de Ven9a321442007-02-12 00:55:35 -08004890static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 .open = rt6_stats_seq_open,
4892 .read = seq_read,
4893 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07004894 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895};
4896#endif /* CONFIG_PROC_FS */
4897
4898#ifdef CONFIG_SYSCTL
4899
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900static
Joe Perchesfe2c6332013-06-11 23:04:25 -07004901int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 void __user *buffer, size_t *lenp, loff_t *ppos)
4903{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00004904 struct net *net;
4905 int delay;
4906 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00004908
4909 net = (struct net *)ctl->extra1;
4910 delay = net->ipv6.sysctl.flush_delay;
4911 proc_dointvec(ctl, write, buffer, lenp, ppos);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02004912 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00004913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914}
4915
Joe Perchesfe2c6332013-06-11 23:04:25 -07004916struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004917 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08004919 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07004921 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004922 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 },
4924 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08004926 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 .maxlen = sizeof(int),
4928 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004929 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 },
4931 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08004933 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 .maxlen = sizeof(int),
4935 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004936 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 },
4938 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08004940 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 .maxlen = sizeof(int),
4942 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004943 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 },
4945 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08004947 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 .maxlen = sizeof(int),
4949 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004950 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 },
4952 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08004954 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 .maxlen = sizeof(int),
4956 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004957 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 },
4959 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08004961 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 .maxlen = sizeof(int),
4963 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07004964 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 },
4966 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08004968 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 .maxlen = sizeof(int),
4970 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004971 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 },
4973 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08004975 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 .maxlen = sizeof(int),
4977 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07004978 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 },
4980 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08004982 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 .maxlen = sizeof(int),
4984 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004985 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004987 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988};
4989
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004990struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08004991{
4992 struct ctl_table *table;
4993
4994 table = kmemdup(ipv6_route_table_template,
4995 sizeof(ipv6_route_table_template),
4996 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09004997
4998 if (table) {
4999 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005000 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00005001 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005002 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
5003 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
5004 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
5005 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
5006 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
5007 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
5008 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08005009 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
Eric W. Biederman464dc802012-11-16 03:02:59 +00005010
5011 /* Don't export sysctls to unprivileged users */
5012 if (net->user_ns != &init_user_ns)
5013 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005014 }
5015
Daniel Lezcano760f2d02008-01-10 02:53:43 -08005016 return table;
5017}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018#endif
5019
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005020static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005021{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07005022 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005023
Alexey Dobriyan86393e52009-08-29 01:34:49 +00005024 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
5025 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005026
Eric Dumazetfc66f952010-10-08 06:37:34 +00005027 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5028 goto out_ip6_dst_ops;
5029
David Ahern421842e2018-04-17 17:33:18 -07005030 net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template,
5031 sizeof(*net->ipv6.fib6_null_entry),
5032 GFP_KERNEL);
5033 if (!net->ipv6.fib6_null_entry)
5034 goto out_ip6_dst_entries;
5035
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005036 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
5037 sizeof(*net->ipv6.ip6_null_entry),
5038 GFP_KERNEL);
5039 if (!net->ipv6.ip6_null_entry)
David Ahern421842e2018-04-17 17:33:18 -07005040 goto out_fib6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005041 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005042 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
5043 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005044
5045#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Vincent Bernatfeca7d82017-08-08 20:23:49 +02005046 net->ipv6.fib6_has_custom_rules = false;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005047 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
5048 sizeof(*net->ipv6.ip6_prohibit_entry),
5049 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005050 if (!net->ipv6.ip6_prohibit_entry)
5051 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005052 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005053 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
5054 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005055
5056 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
5057 sizeof(*net->ipv6.ip6_blk_hole_entry),
5058 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005059 if (!net->ipv6.ip6_blk_hole_entry)
5060 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005061 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005062 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
5063 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005064#endif
5065
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07005066 net->ipv6.sysctl.flush_delay = 0;
5067 net->ipv6.sysctl.ip6_rt_max_size = 4096;
5068 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5069 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5070 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5071 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5072 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5073 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
5074
Benjamin Thery6891a342008-03-04 13:49:47 -08005075 net->ipv6.ip6_rt_gc_expire = 30*HZ;
5076
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005077 ret = 0;
5078out:
5079 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005080
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005081#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5082out_ip6_prohibit_entry:
5083 kfree(net->ipv6.ip6_prohibit_entry);
5084out_ip6_null_entry:
5085 kfree(net->ipv6.ip6_null_entry);
5086#endif
David Ahern421842e2018-04-17 17:33:18 -07005087out_fib6_null_entry:
5088 kfree(net->ipv6.fib6_null_entry);
Eric Dumazetfc66f952010-10-08 06:37:34 +00005089out_ip6_dst_entries:
5090 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005091out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005092 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005093}
5094
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005095static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005096{
David Ahern421842e2018-04-17 17:33:18 -07005097 kfree(net->ipv6.fib6_null_entry);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005098 kfree(net->ipv6.ip6_null_entry);
5099#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5100 kfree(net->ipv6.ip6_prohibit_entry);
5101 kfree(net->ipv6.ip6_blk_hole_entry);
5102#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00005103 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005104}
5105
Thomas Grafd1896342012-06-18 12:08:33 +00005106static int __net_init ip6_route_net_init_late(struct net *net)
5107{
5108#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00005109 proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
Joe Perchesd6444062018-03-23 15:54:38 -07005110 proc_create("rt6_stats", 0444, net->proc_net, &rt6_stats_seq_fops);
Thomas Grafd1896342012-06-18 12:08:33 +00005111#endif
5112 return 0;
5113}
5114
5115static void __net_exit ip6_route_net_exit_late(struct net *net)
5116{
5117#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00005118 remove_proc_entry("ipv6_route", net->proc_net);
5119 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00005120#endif
5121}
5122
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005123static struct pernet_operations ip6_route_net_ops = {
5124 .init = ip6_route_net_init,
5125 .exit = ip6_route_net_exit,
5126};
5127
David S. Millerc3426b42012-06-09 16:27:05 -07005128static int __net_init ipv6_inetpeer_init(struct net *net)
5129{
5130 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5131
5132 if (!bp)
5133 return -ENOMEM;
5134 inet_peer_base_init(bp);
5135 net->ipv6.peers = bp;
5136 return 0;
5137}
5138
5139static void __net_exit ipv6_inetpeer_exit(struct net *net)
5140{
5141 struct inet_peer_base *bp = net->ipv6.peers;
5142
5143 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07005144 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07005145 kfree(bp);
5146}
5147
David S. Miller2b823f72012-06-09 19:00:16 -07005148static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07005149 .init = ipv6_inetpeer_init,
5150 .exit = ipv6_inetpeer_exit,
5151};
5152
Thomas Grafd1896342012-06-18 12:08:33 +00005153static struct pernet_operations ip6_route_net_late_ops = {
5154 .init = ip6_route_net_init_late,
5155 .exit = ip6_route_net_exit_late,
5156};
5157
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005158static struct notifier_block ip6_route_dev_notifier = {
5159 .notifier_call = ip6_route_dev_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07005160 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005161};
5162
WANG Cong2f460932017-05-03 22:07:31 -07005163void __init ip6_route_init_special_entries(void)
5164{
5165 /* Registering of the loopback is done before this portion of code,
5166 * the loopback reference in rt6_info will not be taken, do it
5167 * manually for init_net */
David Ahern421842e2018-04-17 17:33:18 -07005168 init_net.ipv6.fib6_null_entry->fib6_nh.nh_dev = init_net.loopback_dev;
WANG Cong2f460932017-05-03 22:07:31 -07005169 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
5170 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5171 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5172 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
5173 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5174 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
5175 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5176 #endif
5177}
5178
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005179int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005181 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07005182 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005183
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08005184 ret = -ENOMEM;
5185 ip6_dst_ops_template.kmem_cachep =
5186 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
5187 SLAB_HWCACHE_ALIGN, NULL);
5188 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08005189 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07005190
Eric Dumazetfc66f952010-10-08 06:37:34 +00005191 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005192 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08005193 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08005194
David S. Millerc3426b42012-06-09 16:27:05 -07005195 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
5196 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07005197 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00005198
David S. Miller7e52b332012-06-15 15:51:55 -07005199 ret = register_pernet_subsys(&ip6_route_net_ops);
5200 if (ret)
5201 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07005202
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07005203 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
5204
David S. Millere8803b62012-06-16 01:12:19 -07005205 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005206 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005207 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005208
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005209 ret = xfrm6_init();
5210 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07005211 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08005212
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005213 ret = fib6_rules_init();
5214 if (ret)
5215 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08005216
Thomas Grafd1896342012-06-18 12:08:33 +00005217 ret = register_pernet_subsys(&ip6_route_net_late_ops);
5218 if (ret)
5219 goto fib6_rules_init;
5220
Florian Westphal16feebc2017-12-02 21:44:08 +01005221 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
5222 inet6_rtm_newroute, NULL, 0);
5223 if (ret < 0)
5224 goto out_register_late_subsys;
5225
5226 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
5227 inet6_rtm_delroute, NULL, 0);
5228 if (ret < 0)
5229 goto out_register_late_subsys;
5230
5231 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
5232 inet6_rtm_getroute, NULL,
5233 RTNL_FLAG_DOIT_UNLOCKED);
5234 if (ret < 0)
Thomas Grafd1896342012-06-18 12:08:33 +00005235 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005236
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005237 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005238 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00005239 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005240
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07005241 for_each_possible_cpu(cpu) {
5242 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
5243
5244 INIT_LIST_HEAD(&ul->head);
5245 spin_lock_init(&ul->lock);
5246 }
5247
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005248out:
5249 return ret;
5250
Thomas Grafd1896342012-06-18 12:08:33 +00005251out_register_late_subsys:
Florian Westphal16feebc2017-12-02 21:44:08 +01005252 rtnl_unregister_all(PF_INET6);
Thomas Grafd1896342012-06-18 12:08:33 +00005253 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005254fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005255 fib6_rules_cleanup();
5256xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005257 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00005258out_fib6_init:
5259 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005260out_register_subsys:
5261 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07005262out_register_inetpeer:
5263 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00005264out_dst_entries:
5265 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005266out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005267 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005268 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269}
5270
5271void ip6_route_cleanup(void)
5272{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005273 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00005274 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07005275 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07005278 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005279 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00005280 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005281 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282}