blob: f6ca55d21fac35b8b10879a91cf350fffcff811b [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);
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +020099static int rt6_score_route(struct rt6_info *rt, int oif, int strict);
David Ahern16a16cd2017-02-02 12:37:11 -0800100static size_t rt6_nlmsg_size(struct rt6_info *rt);
David Ahernd4ead6b2018-04-17 17:33:16 -0700101static int rt6_fill_node(struct net *net, struct sk_buff *skb,
102 struct rt6_info *rt, struct dst_entry *dst,
103 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);
Wei Wang35732d02017-10-06 12:05:57 -0700106static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt,
107 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
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800111static struct rt6_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);
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -0800116static struct rt6_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 S. Millerf894cbf2012-07-02 21:52:24 -0700185static inline const void *choose_neigh_daddr(struct rt6_info *rt,
186 struct sk_buff *skb,
187 const void *daddr)
David S. Miller39232972012-01-26 15:22:32 -0500188{
189 struct in6_addr *p = &rt->rt6i_gateway;
190
David S. Millera7563f32012-01-26 16:29:16 -0500191 if (!ipv6_addr_any(p))
David S. Miller39232972012-01-26 15:22:32 -0500192 return (const void *) p;
David S. Millerf894cbf2012-07-02 21:52:24 -0700193 else if (skb)
194 return &ipv6_hdr(skb)->daddr;
David S. Miller39232972012-01-26 15:22:32 -0500195 return daddr;
196}
197
David S. Millerf894cbf2012-07-02 21:52:24 -0700198static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
199 struct sk_buff *skb,
200 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700201{
David S. Miller39232972012-01-26 15:22:32 -0500202 struct rt6_info *rt = (struct rt6_info *) dst;
203 struct neighbour *n;
204
David S. Millerf894cbf2012-07-02 21:52:24 -0700205 daddr = choose_neigh_daddr(rt, skb, daddr);
YOSHIFUJI Hideaki / 吉藤英明8e022ee2013-01-17 12:53:09 +0000206 n = __ipv6_neigh_lookup(dst->dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500207 if (n)
208 return n;
209 return neigh_create(&nd_tbl, daddr, dst->dev);
210}
211
Julian Anastasov63fca652017-02-06 23:14:15 +0200212static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
213{
214 struct net_device *dev = dst->dev;
215 struct rt6_info *rt = (struct rt6_info *)dst;
216
217 daddr = choose_neigh_daddr(rt, NULL, daddr);
218 if (!daddr)
219 return;
220 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
221 return;
222 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
223 return;
224 __ipv6_confirm_neigh(dev, daddr);
225}
226
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800227static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 .gc = ip6_dst_gc,
230 .gc_thresh = 1024,
231 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800232 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000233 .mtu = ip6_mtu,
David Ahernd4ead6b2018-04-17 17:33:16 -0700234 .cow_metrics = dst_cow_metrics_generic,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 .destroy = ip6_dst_destroy,
236 .ifdown = ip6_dst_ifdown,
237 .negative_advice = ip6_negative_advice,
238 .link_failure = ip6_link_failure,
239 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700240 .redirect = rt6_do_redirect,
Eric W. Biederman9f8955c2015-10-07 16:48:39 -0500241 .local_out = __ip6_local_out,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700242 .neigh_lookup = ip6_neigh_lookup,
Julian Anastasov63fca652017-02-06 23:14:15 +0200243 .confirm_neigh = ip6_confirm_neigh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
Steffen Klassertebb762f2011-11-23 02:12:51 +0000246static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800247{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000248 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
249
250 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800251}
252
David S. Miller6700c272012-07-17 03:29:28 -0700253static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
254 struct sk_buff *skb, u32 mtu)
David S. Miller14e50e52007-05-24 18:17:54 -0700255{
256}
257
David S. Miller6700c272012-07-17 03:29:28 -0700258static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
259 struct sk_buff *skb)
David S. Millerb587ee32012-07-12 00:39:24 -0700260{
261}
262
David S. Miller14e50e52007-05-24 18:17:54 -0700263static struct dst_ops ip6_dst_blackhole_ops = {
264 .family = AF_INET6,
David S. Miller14e50e52007-05-24 18:17:54 -0700265 .destroy = ip6_dst_destroy,
266 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000267 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800268 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700269 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Millerb587ee32012-07-12 00:39:24 -0700270 .redirect = ip6_rt_blackhole_redirect,
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -0700271 .cow_metrics = dst_cow_metrics_generic,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700272 .neigh_lookup = ip6_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700273};
274
David S. Miller62fa8a82011-01-26 20:51:05 -0800275static const u32 ip6_template_metrics[RTAX_MAX] = {
Li RongQing14edd872012-10-24 14:01:18 +0800276 [RTAX_HOPLIMIT - 1] = 0,
David S. Miller62fa8a82011-01-26 20:51:05 -0800277};
278
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000279static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700280 .dst = {
281 .__refcnt = ATOMIC_INIT(1),
282 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000283 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700284 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700285 .input = ip6_pkt_discard,
286 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 },
288 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700289 .rt6i_protocol = RTPROT_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 .rt6i_metric = ~(u32) 0,
291 .rt6i_ref = ATOMIC_INIT(1),
David Aherne8478e82018-04-17 17:33:13 -0700292 .fib6_type = RTN_UNREACHABLE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293};
294
Thomas Graf101367c2006-08-04 03:39:02 -0700295#ifdef CONFIG_IPV6_MULTIPLE_TABLES
296
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000297static const struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700298 .dst = {
299 .__refcnt = ATOMIC_INIT(1),
300 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000301 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700302 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700303 .input = ip6_pkt_prohibit,
304 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700305 },
306 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700307 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700308 .rt6i_metric = ~(u32) 0,
309 .rt6i_ref = ATOMIC_INIT(1),
David Aherne8478e82018-04-17 17:33:13 -0700310 .fib6_type = RTN_PROHIBIT,
Thomas Graf101367c2006-08-04 03:39:02 -0700311};
312
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000313static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700314 .dst = {
315 .__refcnt = ATOMIC_INIT(1),
316 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000317 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700318 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700319 .input = dst_discard,
Eric W. Biedermanede20592015-10-07 16:48:47 -0500320 .output = dst_discard_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700321 },
322 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Jean-Mickael Guerin4f724272009-05-20 17:38:59 -0700323 .rt6i_protocol = RTPROT_KERNEL,
Thomas Graf101367c2006-08-04 03:39:02 -0700324 .rt6i_metric = ~(u32) 0,
325 .rt6i_ref = ATOMIC_INIT(1),
David Aherne8478e82018-04-17 17:33:13 -0700326 .fib6_type = RTN_BLACKHOLE,
Thomas Graf101367c2006-08-04 03:39:02 -0700327};
328
329#endif
330
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700331static void rt6_info_init(struct rt6_info *rt)
332{
333 struct dst_entry *dst = &rt->dst;
334
335 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
336 INIT_LIST_HEAD(&rt->rt6i_siblings);
337 INIT_LIST_HEAD(&rt->rt6i_uncached);
David Ahernd4ead6b2018-04-17 17:33:16 -0700338 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700339}
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341/* allocate dst with ip6_dst_ops */
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700342static struct rt6_info *__ip6_dst_alloc(struct net *net,
343 struct net_device *dev,
Martin KaFai Lauad706862015-08-14 11:05:52 -0700344 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
David S. Miller97bab732012-06-09 22:36:36 -0700346 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Wei Wangb2a9c0e2017-06-17 10:42:41 -0700347 1, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700348
Wei Wang81eb8442017-10-06 12:06:11 -0700349 if (rt) {
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700350 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -0700351 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
352 }
Steffen Klassert81048912012-07-05 23:37:09 +0000353
David S. Millercf911662011-04-28 14:31:47 -0700354 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
David Ahern9ab179d2016-04-07 11:10:06 -0700357struct rt6_info *ip6_dst_alloc(struct net *net,
358 struct net_device *dev,
359 int flags)
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700360{
Martin KaFai Lauad706862015-08-14 11:05:52 -0700361 struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700362
363 if (rt) {
364 rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC);
Eric Dumazetbfd8e5a2017-10-09 06:01:37 -0700365 if (!rt->rt6i_pcpu) {
Wei Wang587fea72017-06-17 10:42:36 -0700366 dst_release_immediate(&rt->dst);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700367 return NULL;
368 }
369 }
370
371 return rt;
372}
David Ahern9ab179d2016-04-07 11:10:06 -0700373EXPORT_SYMBOL(ip6_dst_alloc);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375static void ip6_dst_destroy(struct dst_entry *dst)
376{
377 struct rt6_info *rt = (struct rt6_info *)dst;
Wei Wang35732d02017-10-06 12:05:57 -0700378 struct rt6_exception_bucket *bucket;
David Miller3a2232e2017-11-28 15:40:40 -0500379 struct rt6_info *from = rt->from;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700380 struct inet6_dev *idev;
David Ahernd4ead6b2018-04-17 17:33:16 -0700381 struct dst_metrics *m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -0700383 dst_destroy_metrics_generic(dst);
Markus Elfring87775312015-07-02 16:30:24 +0200384 free_percpu(rt->rt6i_pcpu);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700385 rt6_uncached_list_del(rt);
386
387 idev = rt->rt6i_idev;
David S. Miller38308472011-12-03 18:02:47 -0500388 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 rt->rt6i_idev = NULL;
390 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900391 }
Wei Wang35732d02017-10-06 12:05:57 -0700392 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1);
393 if (bucket) {
394 rt->rt6i_exception_bucket = NULL;
395 kfree(bucket);
396 }
Gao feng1716a962012-04-06 00:13:10 +0000397
David Ahernd4ead6b2018-04-17 17:33:16 -0700398 m = rt->fib6_metrics;
399 if (m != &dst_default_metrics && refcount_dec_and_test(&m->refcnt))
400 kfree(m);
401
David Miller3a2232e2017-11-28 15:40:40 -0500402 rt->from = NULL;
403 dst_release(&from->dst);
David S. Millerb3419362010-11-30 12:27:11 -0800404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
407 int how)
408{
409 struct rt6_info *rt = (struct rt6_info *)dst;
410 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800411 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900412 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Wei Wange5645f52017-08-14 10:44:59 -0700414 if (idev && idev->dev != loopback_dev) {
415 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
416 if (loopback_idev) {
417 rt->rt6i_idev = loopback_idev;
418 in6_dev_put(idev);
David S. Miller97cac082012-07-02 22:43:47 -0700419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421}
422
Martin KaFai Lau5973fb12015-11-11 11:51:07 -0800423static bool __rt6_check_expired(const struct rt6_info *rt)
424{
425 if (rt->rt6i_flags & RTF_EXPIRES)
426 return time_after(jiffies, rt->dst.expires);
427 else
428 return false;
429}
430
Eric Dumazeta50feda2012-05-18 18:57:34 +0000431static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Gao feng1716a962012-04-06 00:13:10 +0000433 if (rt->rt6i_flags & RTF_EXPIRES) {
434 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000435 return true;
David Miller3a2232e2017-11-28 15:40:40 -0500436 } else if (rt->from) {
Xin Long1e2ea8a2017-08-26 20:10:10 +0800437 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
David Ahern14895682018-04-17 17:33:17 -0700438 fib6_check_expired(rt->from);
Gao feng1716a962012-04-06 00:13:10 +0000439 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000440 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
David Ahernb4bac172018-03-02 08:32:18 -0800443static struct rt6_info *rt6_multipath_select(const struct net *net,
444 struct rt6_info *match,
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200445 struct flowi6 *fl6, int oif,
David Ahernb75cc8f2018-03-02 08:32:17 -0800446 const struct sk_buff *skb,
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +0200447 int strict)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000448{
449 struct rt6_info *sibling, *next_sibling;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000450
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200451 /* We might have already computed the hash for ICMPv6 errors. In such
452 * case it will always be non-zero. Otherwise now is the time to do it.
453 */
454 if (!fl6->mp_hash)
David Ahernb4bac172018-03-02 08:32:18 -0800455 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200456
David Ahern5e670d82018-04-17 17:33:14 -0700457 if (fl6->mp_hash <= atomic_read(&match->fib6_nh.nh_upper_bound))
Ido Schimmel3d709f62018-01-09 16:40:27 +0200458 return match;
Ido Schimmelbbfcd772017-11-21 09:50:12 +0200459
Ido Schimmel3d709f62018-01-09 16:40:27 +0200460 list_for_each_entry_safe(sibling, next_sibling, &match->rt6i_siblings,
461 rt6i_siblings) {
David Ahern5e670d82018-04-17 17:33:14 -0700462 int nh_upper_bound;
463
464 nh_upper_bound = atomic_read(&sibling->fib6_nh.nh_upper_bound);
465 if (fl6->mp_hash > nh_upper_bound)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200466 continue;
467 if (rt6_score_route(sibling, oif, strict) < 0)
468 break;
469 match = sibling;
470 break;
471 }
472
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000473 return match;
474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476/*
Wei Wang66f5d6c2017-10-06 12:06:10 -0700477 * Route lookup. rcu_read_lock() should be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 */
479
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800480static inline struct rt6_info *rt6_device_match(struct net *net,
481 struct rt6_info *rt,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000482 const struct in6_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 int oif,
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700484 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct rt6_info *local = NULL;
487 struct rt6_info *sprt;
488
David Ahern5e670d82018-04-17 17:33:14 -0700489 if (!oif && ipv6_addr_any(saddr) &&
490 !(rt->fib6_nh.nh_flags & RTNH_F_DEAD))
Ido Schimmel8067bb82018-01-07 12:45:09 +0200491 return rt;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900492
David Miller071fb372017-11-28 15:40:15 -0500493 for (sprt = rt; sprt; sprt = rcu_dereference(sprt->rt6_next)) {
David Ahern5e670d82018-04-17 17:33:14 -0700494 const struct net_device *dev = sprt->fib6_nh.nh_dev;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900495
David Ahern5e670d82018-04-17 17:33:14 -0700496 if (sprt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +0200497 continue;
498
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900499 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (dev->ifindex == oif)
501 return sprt;
502 if (dev->flags & IFF_LOOPBACK) {
David S. Miller38308472011-12-03 18:02:47 -0500503 if (!sprt->rt6i_idev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 sprt->rt6i_idev->dev->ifindex != oif) {
David Ahern17fb0b22015-09-25 15:22:54 -0600505 if (flags & RT6_LOOKUP_F_IFACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 continue;
David Ahern17fb0b22015-09-25 15:22:54 -0600507 if (local &&
508 local->rt6i_idev->dev->ifindex == oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 continue;
510 }
511 local = sprt;
512 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900513 } else {
514 if (ipv6_chk_addr(net, saddr, dev,
515 flags & RT6_LOOKUP_F_IFACE))
516 return sprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900520 if (oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (local)
522 return local;
523
YOSHIFUJI Hideakid4208952008-06-27 20:14:54 -0700524 if (flags & RT6_LOOKUP_F_IFACE)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800525 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
Ido Schimmel8067bb82018-01-07 12:45:09 +0200527
David Ahern5e670d82018-04-17 17:33:14 -0700528 return rt->fib6_nh.nh_flags & RTNH_F_DEAD ? net->ipv6.ip6_null_entry : rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800531#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200532struct __rt6_probe_work {
533 struct work_struct work;
534 struct in6_addr target;
535 struct net_device *dev;
536};
537
538static void rt6_probe_deferred(struct work_struct *w)
539{
540 struct in6_addr mcaddr;
541 struct __rt6_probe_work *work =
542 container_of(w, struct __rt6_probe_work, work);
543
544 addrconf_addr_solict_mult(&work->target, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800545 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200546 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100547 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200548}
549
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800550static void rt6_probe(struct rt6_info *rt)
551{
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700552 struct __rt6_probe_work *work;
David Ahern5e670d82018-04-17 17:33:14 -0700553 const struct in6_addr *nh_gw;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000554 struct neighbour *neigh;
David Ahern5e670d82018-04-17 17:33:14 -0700555 struct net_device *dev;
556
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800557 /*
558 * Okay, this does not seem to be appropriate
559 * for now, however, we need to check if it
560 * is really so; aka Router Reachability Probing.
561 *
562 * Router Reachability Probe MUST be rate-limited
563 * to no more than one per minute.
564 */
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000565 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
Amerigo Wangfdd66812012-09-10 02:48:44 +0000566 return;
David Ahern5e670d82018-04-17 17:33:14 -0700567
568 nh_gw = &rt->fib6_nh.nh_gw;
569 dev = rt->fib6_nh.nh_dev;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000570 rcu_read_lock_bh();
David Ahern5e670d82018-04-17 17:33:14 -0700571 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000572 if (neigh) {
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700573 if (neigh->nud_state & NUD_VALID)
574 goto out;
575
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700576 work = NULL;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000577 write_lock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700578 if (!(neigh->nud_state & NUD_VALID) &&
579 time_after(jiffies,
580 neigh->updated +
581 rt->rt6i_idev->cnf.rtr_probe_interval)) {
582 work = kmalloc(sizeof(*work), GFP_ATOMIC);
583 if (work)
584 __neigh_set_probe_once(neigh);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200585 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000586 write_unlock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700587 } else {
588 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000589 }
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700590
591 if (work) {
592 INIT_WORK(&work->work, rt6_probe_deferred);
David Ahern5e670d82018-04-17 17:33:14 -0700593 work->target = *nh_gw;
594 dev_hold(dev);
595 work->dev = dev;
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700596 schedule_work(&work->work);
597 }
598
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700599out:
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000600 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800601}
602#else
603static inline void rt6_probe(struct rt6_info *rt)
604{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800605}
606#endif
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800609 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
Dave Jonesb6f99a22007-03-22 12:27:49 -0700611static inline int rt6_check_dev(struct rt6_info *rt, int oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
David Ahern5e670d82018-04-17 17:33:14 -0700613 const struct net_device *dev = rt->fib6_nh.nh_dev;
614
David S. Miller161980f2007-04-06 11:42:27 -0700615 if (!oif || dev->ifindex == oif)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800616 return 2;
David S. Miller161980f2007-04-06 11:42:27 -0700617 if ((dev->flags & IFF_LOOPBACK) &&
618 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
619 return 1;
620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200623static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200625 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
David Ahern5e670d82018-04-17 17:33:14 -0700626 struct neighbour *neigh;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000627
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700628 if (rt->rt6i_flags & RTF_NONEXTHOP ||
629 !(rt->rt6i_flags & RTF_GATEWAY))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200630 return RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000631
632 rcu_read_lock_bh();
David Ahern5e670d82018-04-17 17:33:14 -0700633 neigh = __ipv6_neigh_lookup_noref(rt->fib6_nh.nh_dev,
634 &rt->fib6_nh.nh_gw);
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000635 if (neigh) {
636 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800637 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200638 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800639#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000640 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200641 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100642 else
643 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800644#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000645 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200646 } else {
647 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100648 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000649 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000650 rcu_read_unlock_bh();
651
Paul Marksa5a81f02012-12-03 10:26:54 +0000652 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800653}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800655static int rt6_score_route(struct rt6_info *rt, int oif,
656 int strict)
657{
Paul Marksa5a81f02012-12-03 10:26:54 +0000658 int m;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900659
YOSHIFUJI Hideaki4d0c5912006-05-26 13:23:41 -0700660 m = rt6_check_dev(rt, oif);
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700661 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200662 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800663#ifdef CONFIG_IPV6_ROUTER_PREF
664 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
665#endif
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200666 if (strict & RT6_LOOKUP_F_REACHABLE) {
667 int n = rt6_check_neigh(rt);
668 if (n < 0)
669 return n;
670 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800671 return m;
672}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
David S. Millerf11e6652007-03-24 20:36:25 -0700674static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200675 int *mpri, struct rt6_info *match,
676 bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800677{
David S. Millerf11e6652007-03-24 20:36:25 -0700678 int m;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200679 bool match_do_rr = false;
Andy Gospodarek35103d12015-08-13 10:39:01 -0400680 struct inet6_dev *idev = rt->rt6i_idev;
Andy Gospodarek35103d12015-08-13 10:39:01 -0400681
David Ahern5e670d82018-04-17 17:33:14 -0700682 if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +0200683 goto out;
684
Ido Schimmel14c52062018-01-07 12:45:07 +0200685 if (idev->cnf.ignore_routes_with_linkdown &&
David Ahern5e670d82018-04-17 17:33:14 -0700686 rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN &&
David Ahernd5d32e42016-10-24 12:27:23 -0700687 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
Andy Gospodarek35103d12015-08-13 10:39:01 -0400688 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700689
David Ahern14895682018-04-17 17:33:17 -0700690 if (fib6_check_expired(rt))
David S. Millerf11e6652007-03-24 20:36:25 -0700691 goto out;
692
693 m = rt6_score_route(rt, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100694 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200695 match_do_rr = true;
696 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100697 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700698 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700699 }
700
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200701 if (strict & RT6_LOOKUP_F_REACHABLE)
702 rt6_probe(rt);
703
Jiri Benc7e980562013-12-11 13:48:20 +0100704 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200705 if (m > *mpri) {
706 *do_rr = match_do_rr;
707 *mpri = m;
708 match = rt;
709 }
David S. Millerf11e6652007-03-24 20:36:25 -0700710out:
711 return match;
712}
713
714static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
Wei Wang8d1040e2017-10-06 12:06:08 -0700715 struct rt6_info *leaf,
David S. Millerf11e6652007-03-24 20:36:25 -0700716 struct rt6_info *rr_head,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200717 u32 metric, int oif, int strict,
718 bool *do_rr)
David S. Millerf11e6652007-03-24 20:36:25 -0700719{
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700720 struct rt6_info *rt, *match, *cont;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800721 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
David S. Millerf11e6652007-03-24 20:36:25 -0700723 match = NULL;
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700724 cont = NULL;
David Miller071fb372017-11-28 15:40:15 -0500725 for (rt = rr_head; rt; rt = rcu_dereference(rt->rt6_next)) {
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700726 if (rt->rt6i_metric != metric) {
727 cont = rt;
728 break;
729 }
730
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200731 match = find_match(rt, oif, strict, &mpri, match, do_rr);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700732 }
733
Wei Wang66f5d6c2017-10-06 12:06:10 -0700734 for (rt = leaf; rt && rt != rr_head;
David Miller071fb372017-11-28 15:40:15 -0500735 rt = rcu_dereference(rt->rt6_next)) {
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700736 if (rt->rt6i_metric != metric) {
737 cont = rt;
738 break;
739 }
740
741 match = find_match(rt, oif, strict, &mpri, match, do_rr);
742 }
743
744 if (match || !cont)
745 return match;
746
David Miller071fb372017-11-28 15:40:15 -0500747 for (rt = cont; rt; rt = rcu_dereference(rt->rt6_next))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200748 match = find_match(rt, oif, strict, &mpri, match, do_rr);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800749
David S. Millerf11e6652007-03-24 20:36:25 -0700750 return match;
751}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800752
Wei Wang8d1040e2017-10-06 12:06:08 -0700753static struct rt6_info *rt6_select(struct net *net, struct fib6_node *fn,
754 int oif, int strict)
David S. Millerf11e6652007-03-24 20:36:25 -0700755{
Wei Wang66f5d6c2017-10-06 12:06:10 -0700756 struct rt6_info *leaf = rcu_dereference(fn->leaf);
David S. Millerf11e6652007-03-24 20:36:25 -0700757 struct rt6_info *match, *rt0;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200758 bool do_rr = false;
Wei Wang17ecf592017-10-06 12:06:09 -0700759 int key_plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Wei Wang87b1af82017-10-23 14:59:35 -0700761 if (!leaf || leaf == net->ipv6.ip6_null_entry)
Wei Wang8d1040e2017-10-06 12:06:08 -0700762 return net->ipv6.ip6_null_entry;
763
Wei Wang66f5d6c2017-10-06 12:06:10 -0700764 rt0 = rcu_dereference(fn->rr_ptr);
David S. Millerf11e6652007-03-24 20:36:25 -0700765 if (!rt0)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700766 rt0 = leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Wei Wang17ecf592017-10-06 12:06:09 -0700768 /* Double check to make sure fn is not an intermediate node
769 * and fn->leaf does not points to its child's leaf
770 * (This might happen if all routes under fn are deleted from
771 * the tree and fib6_repair_tree() is called on the node.)
772 */
773 key_plen = rt0->rt6i_dst.plen;
774#ifdef CONFIG_IPV6_SUBTREES
775 if (rt0->rt6i_src.plen)
776 key_plen = rt0->rt6i_src.plen;
777#endif
778 if (fn->fn_bit != key_plen)
779 return net->ipv6.ip6_null_entry;
780
Wei Wang8d1040e2017-10-06 12:06:08 -0700781 match = find_rr_leaf(fn, leaf, rt0, rt0->rt6i_metric, oif, strict,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200782 &do_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200784 if (do_rr) {
David Miller071fb372017-11-28 15:40:15 -0500785 struct rt6_info *next = rcu_dereference(rt0->rt6_next);
David S. Millerf11e6652007-03-24 20:36:25 -0700786
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800787 /* no entries matched; do round-robin */
David S. Millerf11e6652007-03-24 20:36:25 -0700788 if (!next || next->rt6i_metric != rt0->rt6i_metric)
Wei Wang8d1040e2017-10-06 12:06:08 -0700789 next = leaf;
David S. Millerf11e6652007-03-24 20:36:25 -0700790
Wei Wang66f5d6c2017-10-06 12:06:10 -0700791 if (next != rt0) {
792 spin_lock_bh(&leaf->rt6i_table->tb6_lock);
793 /* make sure next is not being deleted from the tree */
794 if (next->rt6i_node)
795 rcu_assign_pointer(fn->rr_ptr, next);
796 spin_unlock_bh(&leaf->rt6i_table->tb6_lock);
797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799
Eric Dumazeta02cec22010-09-22 20:43:57 +0000800 return match ? match : net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700803static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt)
804{
805 return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
806}
807
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800808#ifdef CONFIG_IPV6_ROUTE_INFO
809int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000810 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800811{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900812 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800813 struct route_info *rinfo = (struct route_info *) opt;
814 struct in6_addr prefix_buf, *prefix;
815 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900816 unsigned long lifetime;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800817 struct rt6_info *rt;
818
819 if (len < sizeof(struct route_info)) {
820 return -EINVAL;
821 }
822
823 /* Sanity check for prefix_len and length */
824 if (rinfo->length > 3) {
825 return -EINVAL;
826 } else if (rinfo->prefix_len > 128) {
827 return -EINVAL;
828 } else if (rinfo->prefix_len > 64) {
829 if (rinfo->length < 2) {
830 return -EINVAL;
831 }
832 } else if (rinfo->prefix_len > 0) {
833 if (rinfo->length < 1) {
834 return -EINVAL;
835 }
836 }
837
838 pref = rinfo->route_pref;
839 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000840 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800841
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900842 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800843
844 if (rinfo->length == 3)
845 prefix = (struct in6_addr *)rinfo->prefix;
846 else {
847 /* this function is safe */
848 ipv6_addr_prefix(&prefix_buf,
849 (struct in6_addr *)rinfo->prefix,
850 rinfo->prefix_len);
851 prefix = &prefix_buf;
852 }
853
Duan Jiongf104a562013-11-08 09:56:53 +0800854 if (rinfo->prefix_len == 0)
David Ahernafb1d4b52018-04-17 17:33:11 -0700855 rt = rt6_get_dflt_router(net, gwaddr, dev);
Duan Jiongf104a562013-11-08 09:56:53 +0800856 else
857 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
David Ahern830218c2016-10-24 10:52:35 -0700858 gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800859
860 if (rt && !lifetime) {
David Ahernafb1d4b52018-04-17 17:33:11 -0700861 ip6_del_rt(net, rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800862 rt = NULL;
863 }
864
865 if (!rt && lifetime)
David Ahern830218c2016-10-24 10:52:35 -0700866 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
867 dev, pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800868 else if (rt)
869 rt->rt6i_flags = RTF_ROUTEINFO |
870 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
871
872 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000873 if (!addrconf_finite_timeout(lifetime))
David Ahern14895682018-04-17 17:33:17 -0700874 fib6_clean_expires(rt);
Gao feng1716a962012-04-06 00:13:10 +0000875 else
David Ahern14895682018-04-17 17:33:17 -0700876 fib6_set_expires(rt, jiffies + HZ * lifetime);
Gao feng1716a962012-04-06 00:13:10 +0000877
Amerigo Wang94e187c2012-10-29 00:13:19 +0000878 ip6_rt_put(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800879 }
880 return 0;
881}
882#endif
883
David Ahernae90d862018-04-17 17:33:12 -0700884/*
885 * Misc support functions
886 */
887
888/* called with rcu_lock held */
889static struct net_device *ip6_rt_get_dev_rcu(struct rt6_info *rt)
890{
David Ahern5e670d82018-04-17 17:33:14 -0700891 struct net_device *dev = rt->fib6_nh.nh_dev;
David Ahernae90d862018-04-17 17:33:12 -0700892
893 if (rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) {
894 /* for copies of local routes, dst->dev needs to be the
895 * device if it is a master device, the master device if
896 * device is enslaved, and the loopback as the default
897 */
898 if (netif_is_l3_slave(dev) &&
899 !rt6_need_strict(&rt->rt6i_dst.addr))
900 dev = l3mdev_master_dev_rcu(dev);
901 else if (!netif_is_l3_master(dev))
902 dev = dev_net(dev)->loopback_dev;
903 /* last case is netif_is_l3_master(dev) is true in which
904 * case we want dev returned to be dev
905 */
906 }
907
908 return dev;
909}
910
David Ahern6edb3c92018-04-17 17:33:15 -0700911static const int fib6_prop[RTN_MAX + 1] = {
912 [RTN_UNSPEC] = 0,
913 [RTN_UNICAST] = 0,
914 [RTN_LOCAL] = 0,
915 [RTN_BROADCAST] = 0,
916 [RTN_ANYCAST] = 0,
917 [RTN_MULTICAST] = 0,
918 [RTN_BLACKHOLE] = -EINVAL,
919 [RTN_UNREACHABLE] = -EHOSTUNREACH,
920 [RTN_PROHIBIT] = -EACCES,
921 [RTN_THROW] = -EAGAIN,
922 [RTN_NAT] = -EINVAL,
923 [RTN_XRESOLVE] = -EINVAL,
924};
925
926static int ip6_rt_type_to_error(u8 fib6_type)
927{
928 return fib6_prop[fib6_type];
929}
930
931static void ip6_rt_init_dst_reject(struct rt6_info *rt, struct rt6_info *ort)
932{
933 rt->dst.error = ip6_rt_type_to_error(ort->fib6_type);
934
935 switch (ort->fib6_type) {
936 case RTN_BLACKHOLE:
937 rt->dst.output = dst_discard_out;
938 rt->dst.input = dst_discard;
939 break;
940 case RTN_PROHIBIT:
941 rt->dst.output = ip6_pkt_prohibit_out;
942 rt->dst.input = ip6_pkt_prohibit;
943 break;
944 case RTN_THROW:
945 case RTN_UNREACHABLE:
946 default:
947 rt->dst.output = ip6_pkt_discard_out;
948 rt->dst.input = ip6_pkt_discard;
949 break;
950 }
951}
952
953static void ip6_rt_init_dst(struct rt6_info *rt, struct rt6_info *ort)
954{
955 if (ort->rt6i_flags & RTF_REJECT) {
956 ip6_rt_init_dst_reject(rt, ort);
957 return;
958 }
959
960 rt->dst.error = 0;
961 rt->dst.output = ip6_output;
962
963 if (ort->fib6_type == RTN_LOCAL) {
964 rt->dst.flags |= DST_HOST;
965 rt->dst.input = ip6_input;
966 } else if (ipv6_addr_type(&ort->rt6i_dst.addr) & IPV6_ADDR_MULTICAST) {
967 rt->dst.input = ip6_mc_input;
968 } else {
969 rt->dst.input = ip6_forward;
970 }
971
972 if (ort->fib6_nh.nh_lwtstate) {
973 rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.nh_lwtstate);
974 lwtunnel_set_redirect(&rt->dst);
975 }
976
977 rt->dst.lastuse = jiffies;
978}
979
David Ahernae90d862018-04-17 17:33:12 -0700980static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
981{
982 BUG_ON(from->from);
983
984 rt->rt6i_flags &= ~RTF_EXPIRES;
985 dst_hold(&from->dst);
986 rt->from = from;
David Ahernd4ead6b2018-04-17 17:33:16 -0700987 dst_init_metrics(&rt->dst, from->fib6_metrics->metrics, true);
988 if (from->fib6_metrics != &dst_default_metrics) {
989 rt->dst._metrics |= DST_METRICS_REFCOUNTED;
990 refcount_inc(&from->fib6_metrics->refcnt);
991 }
David Ahernae90d862018-04-17 17:33:12 -0700992}
993
994static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort)
995{
David Ahern6edb3c92018-04-17 17:33:15 -0700996 ip6_rt_init_dst(rt, ort);
997
David Ahernae90d862018-04-17 17:33:12 -0700998 rt->rt6i_dst = ort->rt6i_dst;
David Ahernae90d862018-04-17 17:33:12 -0700999 rt->rt6i_idev = ort->rt6i_idev;
1000 if (rt->rt6i_idev)
1001 in6_dev_hold(rt->rt6i_idev);
David Ahern5e670d82018-04-17 17:33:14 -07001002 rt->rt6i_gateway = ort->fib6_nh.nh_gw;
David Ahernae90d862018-04-17 17:33:12 -07001003 rt->rt6i_flags = ort->rt6i_flags;
1004 rt6_set_from(rt, ort);
1005 rt->rt6i_metric = ort->rt6i_metric;
1006#ifdef CONFIG_IPV6_SUBTREES
1007 rt->rt6i_src = ort->rt6i_src;
1008#endif
1009 rt->rt6i_prefsrc = ort->rt6i_prefsrc;
1010 rt->rt6i_table = ort->rt6i_table;
David Ahern5e670d82018-04-17 17:33:14 -07001011 rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.nh_lwtstate);
David Ahernae90d862018-04-17 17:33:12 -07001012}
1013
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001014static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1015 struct in6_addr *saddr)
1016{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001017 struct fib6_node *pn, *sn;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001018 while (1) {
1019 if (fn->fn_flags & RTN_TL_ROOT)
1020 return NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001021 pn = rcu_dereference(fn->parent);
1022 sn = FIB6_SUBTREE(pn);
1023 if (sn && sn != fn)
1024 fn = fib6_lookup(sn, NULL, saddr);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001025 else
1026 fn = pn;
1027 if (fn->fn_flags & RTN_RTINFO)
1028 return fn;
1029 }
1030}
Thomas Grafc71099a2006-08-04 23:20:06 -07001031
Wei Wangd3843fe2017-10-06 12:06:06 -07001032static bool ip6_hold_safe(struct net *net, struct rt6_info **prt,
1033 bool null_fallback)
1034{
1035 struct rt6_info *rt = *prt;
1036
1037 if (dst_hold_safe(&rt->dst))
1038 return true;
1039 if (null_fallback) {
1040 rt = net->ipv6.ip6_null_entry;
1041 dst_hold(&rt->dst);
1042 } else {
1043 rt = NULL;
1044 }
1045 *prt = rt;
1046 return false;
1047}
1048
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001049static struct rt6_info *ip6_pol_route_lookup(struct net *net,
1050 struct fib6_table *table,
David Ahernb75cc8f2018-03-02 08:32:17 -08001051 struct flowi6 *fl6,
1052 const struct sk_buff *skb,
1053 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Wei Wang2b760fc2017-10-06 12:06:03 -07001055 struct rt6_info *rt, *rt_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
David Ahernb6cdbc82018-03-29 17:44:57 -07001058 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1059 flags &= ~RT6_LOOKUP_F_IFACE;
1060
Wei Wang66f5d6c2017-10-06 12:06:10 -07001061 rcu_read_lock();
David S. Miller4c9483b2011-03-12 16:22:43 -05001062 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -07001063restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07001064 rt = rcu_dereference(fn->leaf);
1065 if (!rt) {
1066 rt = net->ipv6.ip6_null_entry;
1067 } else {
1068 rt = rt6_device_match(net, rt, &fl6->saddr,
1069 fl6->flowi6_oif, flags);
1070 if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
David Ahernb4bac172018-03-02 08:32:18 -08001071 rt = rt6_multipath_select(net, rt, fl6, fl6->flowi6_oif,
David Ahernb75cc8f2018-03-02 08:32:17 -08001072 skb, flags);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001073 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001074 if (rt == net->ipv6.ip6_null_entry) {
1075 fn = fib6_backtrack(fn, &fl6->saddr);
1076 if (fn)
1077 goto restart;
1078 }
Wei Wang2b760fc2017-10-06 12:06:03 -07001079 /* Search through exception table */
1080 rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr);
1081 if (rt_cache)
1082 rt = rt_cache;
1083
Wei Wangd3843fe2017-10-06 12:06:06 -07001084 if (ip6_hold_safe(net, &rt, true))
1085 dst_use_noref(&rt->dst, jiffies);
1086
Wei Wang66f5d6c2017-10-06 12:06:10 -07001087 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08001088
Paolo Abenib65f1642017-10-19 09:31:43 +02001089 trace_fib6_table_lookup(net, rt, table, fl6);
David Ahernb8115802015-11-19 12:24:22 -08001090
Thomas Grafc71099a2006-08-04 23:20:06 -07001091 return rt;
1092
1093}
1094
Ian Morris67ba4152014-08-24 21:53:10 +01001095struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08001096 const struct sk_buff *skb, int flags)
Florian Westphalea6e5742011-09-05 16:05:44 +02001097{
David Ahernb75cc8f2018-03-02 08:32:17 -08001098 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
Florian Westphalea6e5742011-09-05 16:05:44 +02001099}
1100EXPORT_SYMBOL_GPL(ip6_route_lookup);
1101
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001102struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
David Ahernb75cc8f2018-03-02 08:32:17 -08001103 const struct in6_addr *saddr, int oif,
1104 const struct sk_buff *skb, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -07001105{
David S. Miller4c9483b2011-03-12 16:22:43 -05001106 struct flowi6 fl6 = {
1107 .flowi6_oif = oif,
1108 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001109 };
1110 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001111 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07001112
Thomas Grafadaa70b2006-10-13 15:01:03 -07001113 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -05001114 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -07001115 flags |= RT6_LOOKUP_F_HAS_SADDR;
1116 }
1117
David Ahernb75cc8f2018-03-02 08:32:17 -08001118 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -07001119 if (dst->error == 0)
1120 return (struct rt6_info *) dst;
1121
1122 dst_release(dst);
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return NULL;
1125}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001126EXPORT_SYMBOL(rt6_lookup);
1127
Thomas Grafc71099a2006-08-04 23:20:06 -07001128/* ip6_ins_rt is called with FREE table->tb6_lock.
Wei Wang1cfb71e2017-06-17 10:42:33 -07001129 * It takes new route entry, the addition fails by any reason the
1130 * route is released.
1131 * Caller must hold dst before calling it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 */
1133
Michal Kubečeke5fd3872014-03-27 13:04:08 +01001134static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info,
David Ahern333c4302017-05-21 10:12:04 -06001135 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001138 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Thomas Grafc71099a2006-08-04 23:20:06 -07001140 table = rt->rt6i_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001141 spin_lock_bh(&table->tb6_lock);
David Ahernd4ead6b2018-04-17 17:33:16 -07001142 err = fib6_add(&table->tb6_root, rt, info, extack);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001143 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 return err;
1146}
1147
David Ahernafb1d4b52018-04-17 17:33:11 -07001148int ip6_ins_rt(struct net *net, struct rt6_info *rt)
Thomas Graf40e22e82006-08-22 00:00:45 -07001149{
David Ahernafb1d4b52018-04-17 17:33:11 -07001150 struct nl_info info = { .nl_net = net, };
Florian Westphale715b6d2015-01-05 23:57:44 +01001151
Wei Wang1cfb71e2017-06-17 10:42:33 -07001152 /* Hold dst to account for the reference from the fib6 tree */
1153 dst_hold(&rt->dst);
David Ahernd4ead6b2018-04-17 17:33:16 -07001154 return __ip6_ins_rt(rt, &info, NULL);
Thomas Graf40e22e82006-08-22 00:00:45 -07001155}
1156
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001157static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort,
1158 const struct in6_addr *daddr,
1159 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
David Ahern4832c302017-08-17 12:17:20 -07001161 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 struct rt6_info *rt;
1163
1164 /*
1165 * Clone the route.
1166 */
1167
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001168 if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU))
David Miller3a2232e2017-11-28 15:40:40 -05001169 ort = ort->from;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
David Ahern4832c302017-08-17 12:17:20 -07001171 rcu_read_lock();
1172 dev = ip6_rt_get_dev_rcu(ort);
1173 rt = __ip6_dst_alloc(dev_net(dev), dev, 0);
1174 rcu_read_unlock();
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001175 if (!rt)
1176 return NULL;
1177
1178 ip6_rt_copy_init(rt, ort);
1179 rt->rt6i_flags |= RTF_CACHE;
1180 rt->rt6i_metric = 0;
1181 rt->dst.flags |= DST_HOST;
1182 rt->rt6i_dst.addr = *daddr;
1183 rt->rt6i_dst.plen = 128;
1184
1185 if (!rt6_is_gw_or_nonexthop(ort)) {
1186 if (ort->rt6i_dst.plen != 128 &&
1187 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
1188 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001190 if (rt->rt6i_src.plen && saddr) {
1191 rt->rt6i_src.addr = *saddr;
1192 rt->rt6i_src.plen = 128;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001193 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001194#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001197 return rt;
1198}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001200static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt)
1201{
David Ahern4832c302017-08-17 12:17:20 -07001202 struct net_device *dev;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001203 struct rt6_info *pcpu_rt;
1204
David Ahern4832c302017-08-17 12:17:20 -07001205 rcu_read_lock();
1206 dev = ip6_rt_get_dev_rcu(rt);
1207 pcpu_rt = __ip6_dst_alloc(dev_net(dev), dev, rt->dst.flags);
1208 rcu_read_unlock();
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001209 if (!pcpu_rt)
1210 return NULL;
1211 ip6_rt_copy_init(pcpu_rt, rt);
1212 pcpu_rt->rt6i_protocol = rt->rt6i_protocol;
1213 pcpu_rt->rt6i_flags |= RTF_PCPU;
1214 return pcpu_rt;
1215}
1216
Wei Wang66f5d6c2017-10-06 12:06:10 -07001217/* It should be called with rcu_read_lock() acquired */
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001218static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt)
1219{
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001220 struct rt6_info *pcpu_rt, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001221
1222 p = this_cpu_ptr(rt->rt6i_pcpu);
1223 pcpu_rt = *p;
1224
David Ahernd4ead6b2018-04-17 17:33:16 -07001225 if (pcpu_rt)
1226 ip6_hold_safe(NULL, &pcpu_rt, false);
Wei Wangd3843fe2017-10-06 12:06:06 -07001227
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001228 return pcpu_rt;
1229}
1230
David Ahernafb1d4b52018-04-17 17:33:11 -07001231static struct rt6_info *rt6_make_pcpu_route(struct net *net,
1232 struct rt6_info *rt)
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001233{
1234 struct rt6_info *pcpu_rt, *prev, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001235
1236 pcpu_rt = ip6_rt_pcpu_alloc(rt);
1237 if (!pcpu_rt) {
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001238 dst_hold(&net->ipv6.ip6_null_entry->dst);
1239 return net->ipv6.ip6_null_entry;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001240 }
1241
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001242 dst_hold(&pcpu_rt->dst);
Wei Wanga94b9362017-10-06 12:06:04 -07001243 p = this_cpu_ptr(rt->rt6i_pcpu);
1244 prev = cmpxchg(p, NULL, pcpu_rt);
Eric Dumazet951f7882017-10-08 21:07:18 -07001245 BUG_ON(prev);
Wei Wanga94b9362017-10-06 12:06:04 -07001246
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001247 return pcpu_rt;
1248}
1249
Wei Wang35732d02017-10-06 12:05:57 -07001250/* exception hash table implementation
1251 */
1252static DEFINE_SPINLOCK(rt6_exception_lock);
1253
1254/* Remove rt6_ex from hash table and free the memory
1255 * Caller must hold rt6_exception_lock
1256 */
1257static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1258 struct rt6_exception *rt6_ex)
1259{
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001260 struct net *net;
Wei Wang81eb8442017-10-06 12:06:11 -07001261
Wei Wang35732d02017-10-06 12:05:57 -07001262 if (!bucket || !rt6_ex)
1263 return;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001264
1265 net = dev_net(rt6_ex->rt6i->dst.dev);
Wei Wang35732d02017-10-06 12:05:57 -07001266 rt6_ex->rt6i->rt6i_node = NULL;
1267 hlist_del_rcu(&rt6_ex->hlist);
1268 rt6_release(rt6_ex->rt6i);
1269 kfree_rcu(rt6_ex, rcu);
1270 WARN_ON_ONCE(!bucket->depth);
1271 bucket->depth--;
Wei Wang81eb8442017-10-06 12:06:11 -07001272 net->ipv6.rt6_stats->fib_rt_cache--;
Wei Wang35732d02017-10-06 12:05:57 -07001273}
1274
1275/* Remove oldest rt6_ex in bucket and free the memory
1276 * Caller must hold rt6_exception_lock
1277 */
1278static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1279{
1280 struct rt6_exception *rt6_ex, *oldest = NULL;
1281
1282 if (!bucket)
1283 return;
1284
1285 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1286 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1287 oldest = rt6_ex;
1288 }
1289 rt6_remove_exception(bucket, oldest);
1290}
1291
1292static u32 rt6_exception_hash(const struct in6_addr *dst,
1293 const struct in6_addr *src)
1294{
1295 static u32 seed __read_mostly;
1296 u32 val;
1297
1298 net_get_random_once(&seed, sizeof(seed));
1299 val = jhash(dst, sizeof(*dst), seed);
1300
1301#ifdef CONFIG_IPV6_SUBTREES
1302 if (src)
1303 val = jhash(src, sizeof(*src), val);
1304#endif
1305 return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1306}
1307
1308/* Helper function to find the cached rt in the hash table
1309 * and update bucket pointer to point to the bucket for this
1310 * (daddr, saddr) pair
1311 * Caller must hold rt6_exception_lock
1312 */
1313static struct rt6_exception *
1314__rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1315 const struct in6_addr *daddr,
1316 const struct in6_addr *saddr)
1317{
1318 struct rt6_exception *rt6_ex;
1319 u32 hval;
1320
1321 if (!(*bucket) || !daddr)
1322 return NULL;
1323
1324 hval = rt6_exception_hash(daddr, saddr);
1325 *bucket += hval;
1326
1327 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1328 struct rt6_info *rt6 = rt6_ex->rt6i;
1329 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1330
1331#ifdef CONFIG_IPV6_SUBTREES
1332 if (matched && saddr)
1333 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1334#endif
1335 if (matched)
1336 return rt6_ex;
1337 }
1338 return NULL;
1339}
1340
1341/* Helper function to find the cached rt in the hash table
1342 * and update bucket pointer to point to the bucket for this
1343 * (daddr, saddr) pair
1344 * Caller must hold rcu_read_lock()
1345 */
1346static struct rt6_exception *
1347__rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1348 const struct in6_addr *daddr,
1349 const struct in6_addr *saddr)
1350{
1351 struct rt6_exception *rt6_ex;
1352 u32 hval;
1353
1354 WARN_ON_ONCE(!rcu_read_lock_held());
1355
1356 if (!(*bucket) || !daddr)
1357 return NULL;
1358
1359 hval = rt6_exception_hash(daddr, saddr);
1360 *bucket += hval;
1361
1362 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1363 struct rt6_info *rt6 = rt6_ex->rt6i;
1364 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1365
1366#ifdef CONFIG_IPV6_SUBTREES
1367 if (matched && saddr)
1368 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1369#endif
1370 if (matched)
1371 return rt6_ex;
1372 }
1373 return NULL;
1374}
1375
David Ahernd4ead6b2018-04-17 17:33:16 -07001376static unsigned int fib6_mtu(const struct rt6_info *rt)
1377{
1378 unsigned int mtu;
1379
1380 mtu = rt->fib6_pmtu ? : rt->rt6i_idev->cnf.mtu6;
1381 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1382
1383 return mtu - lwtunnel_headroom(rt->fib6_nh.nh_lwtstate, mtu);
1384}
1385
Wei Wang35732d02017-10-06 12:05:57 -07001386static int rt6_insert_exception(struct rt6_info *nrt,
1387 struct rt6_info *ort)
1388{
David Ahern5e670d82018-04-17 17:33:14 -07001389 struct net *net = dev_net(nrt->dst.dev);
Wei Wang35732d02017-10-06 12:05:57 -07001390 struct rt6_exception_bucket *bucket;
1391 struct in6_addr *src_key = NULL;
1392 struct rt6_exception *rt6_ex;
1393 int err = 0;
1394
1395 /* ort can't be a cache or pcpu route */
1396 if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU))
David Miller3a2232e2017-11-28 15:40:40 -05001397 ort = ort->from;
Wei Wang35732d02017-10-06 12:05:57 -07001398 WARN_ON_ONCE(ort->rt6i_flags & (RTF_CACHE | RTF_PCPU));
1399
1400 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 */
1426 if (ort->rt6i_src.plen)
1427 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 */
1433 nrt->rt6i_prefsrc = ort->rt6i_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;
1455 atomic_inc(&nrt->rt6i_ref);
1456 nrt->rt6i_node = ort->rt6i_node;
1457 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1458 bucket->depth++;
Wei Wang81eb8442017-10-06 12:06:11 -07001459 net->ipv6.rt6_stats->fib_rt_cache++;
Wei Wang35732d02017-10-06 12:05:57 -07001460
1461 if (bucket->depth > FIB6_MAX_DEPTH)
1462 rt6_exception_remove_oldest(bucket);
1463
1464out:
1465 spin_unlock_bh(&rt6_exception_lock);
1466
1467 /* Update fn->fn_sernum to invalidate all cached dst */
Paolo Abenib886d5f2017-10-19 16:07:10 +02001468 if (!err) {
Ido Schimmel922c2ac2018-01-07 12:45:14 +02001469 spin_lock_bh(&ort->rt6i_table->tb6_lock);
David Ahern7aef6852018-04-17 17:33:10 -07001470 fib6_update_sernum(net, ort);
Ido Schimmel922c2ac2018-01-07 12:45:14 +02001471 spin_unlock_bh(&ort->rt6i_table->tb6_lock);
Paolo Abenib886d5f2017-10-19 16:07:10 +02001472 fib6_force_start_gc(net);
1473 }
Wei Wang35732d02017-10-06 12:05:57 -07001474
1475 return err;
1476}
1477
1478void rt6_flush_exceptions(struct rt6_info *rt)
1479{
1480 struct rt6_exception_bucket *bucket;
1481 struct rt6_exception *rt6_ex;
1482 struct hlist_node *tmp;
1483 int i;
1484
1485 spin_lock_bh(&rt6_exception_lock);
1486 /* Prevent rt6_insert_exception() to recreate the bucket list */
1487 rt->exception_bucket_flushed = 1;
1488
1489 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1490 lockdep_is_held(&rt6_exception_lock));
1491 if (!bucket)
1492 goto out;
1493
1494 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1495 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
1496 rt6_remove_exception(bucket, rt6_ex);
1497 WARN_ON_ONCE(bucket->depth);
1498 bucket++;
1499 }
1500
1501out:
1502 spin_unlock_bh(&rt6_exception_lock);
1503}
1504
1505/* Find cached rt in the hash table inside passed in rt
1506 * Caller has to hold rcu_read_lock()
1507 */
1508static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt,
1509 struct in6_addr *daddr,
1510 struct in6_addr *saddr)
1511{
1512 struct rt6_exception_bucket *bucket;
1513 struct in6_addr *src_key = NULL;
1514 struct rt6_exception *rt6_ex;
1515 struct rt6_info *res = NULL;
1516
1517 bucket = rcu_dereference(rt->rt6i_exception_bucket);
1518
1519#ifdef CONFIG_IPV6_SUBTREES
1520 /* rt6i_src.plen != 0 indicates rt is in subtree
1521 * and exception table is indexed by a hash of
1522 * both rt6i_dst and rt6i_src.
1523 * Otherwise, the exception table is indexed by
1524 * a hash of only rt6i_dst.
1525 */
1526 if (rt->rt6i_src.plen)
1527 src_key = saddr;
1528#endif
1529 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1530
1531 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
1532 res = rt6_ex->rt6i;
1533
1534 return res;
1535}
1536
1537/* Remove the passed in cached rt from the hash table that contains it */
1538int rt6_remove_exception_rt(struct rt6_info *rt)
1539{
Wei Wang35732d02017-10-06 12:05:57 -07001540 struct rt6_exception_bucket *bucket;
David Miller3a2232e2017-11-28 15:40:40 -05001541 struct rt6_info *from = rt->from;
Wei Wang35732d02017-10-06 12:05:57 -07001542 struct in6_addr *src_key = NULL;
1543 struct rt6_exception *rt6_ex;
1544 int err;
1545
1546 if (!from ||
Colin Ian King442d7132017-10-10 19:10:30 +01001547 !(rt->rt6i_flags & RTF_CACHE))
Wei Wang35732d02017-10-06 12:05:57 -07001548 return -EINVAL;
1549
1550 if (!rcu_access_pointer(from->rt6i_exception_bucket))
1551 return -ENOENT;
1552
1553 spin_lock_bh(&rt6_exception_lock);
1554 bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
1555 lockdep_is_held(&rt6_exception_lock));
1556#ifdef CONFIG_IPV6_SUBTREES
1557 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1558 * and exception table is indexed by a hash of
1559 * both rt6i_dst and rt6i_src.
1560 * Otherwise, the exception table is indexed by
1561 * a hash of only rt6i_dst.
1562 */
1563 if (from->rt6i_src.plen)
1564 src_key = &rt->rt6i_src.addr;
1565#endif
1566 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1567 &rt->rt6i_dst.addr,
1568 src_key);
1569 if (rt6_ex) {
1570 rt6_remove_exception(bucket, rt6_ex);
1571 err = 0;
1572 } else {
1573 err = -ENOENT;
1574 }
1575
1576 spin_unlock_bh(&rt6_exception_lock);
1577 return err;
1578}
1579
1580/* Find rt6_ex which contains the passed in rt cache and
1581 * refresh its stamp
1582 */
1583static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1584{
Wei Wang35732d02017-10-06 12:05:57 -07001585 struct rt6_exception_bucket *bucket;
David Miller3a2232e2017-11-28 15:40:40 -05001586 struct rt6_info *from = rt->from;
Wei Wang35732d02017-10-06 12:05:57 -07001587 struct in6_addr *src_key = NULL;
1588 struct rt6_exception *rt6_ex;
1589
1590 if (!from ||
Colin Ian King442d7132017-10-10 19:10:30 +01001591 !(rt->rt6i_flags & RTF_CACHE))
Wei Wang35732d02017-10-06 12:05:57 -07001592 return;
1593
1594 rcu_read_lock();
1595 bucket = rcu_dereference(from->rt6i_exception_bucket);
1596
1597#ifdef CONFIG_IPV6_SUBTREES
1598 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1599 * and exception table is indexed by a hash of
1600 * both rt6i_dst and rt6i_src.
1601 * Otherwise, the exception table is indexed by
1602 * a hash of only rt6i_dst.
1603 */
1604 if (from->rt6i_src.plen)
1605 src_key = &rt->rt6i_src.addr;
1606#endif
1607 rt6_ex = __rt6_find_exception_rcu(&bucket,
1608 &rt->rt6i_dst.addr,
1609 src_key);
1610 if (rt6_ex)
1611 rt6_ex->stamp = jiffies;
1612
1613 rcu_read_unlock();
1614}
1615
Wei Wang60006a42017-10-06 12:05:58 -07001616static void rt6_exceptions_remove_prefsrc(struct rt6_info *rt)
1617{
1618 struct rt6_exception_bucket *bucket;
1619 struct rt6_exception *rt6_ex;
1620 int i;
1621
1622 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1623 lockdep_is_held(&rt6_exception_lock));
1624
1625 if (bucket) {
1626 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1627 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1628 rt6_ex->rt6i->rt6i_prefsrc.plen = 0;
1629 }
1630 bucket++;
1631 }
1632 }
1633}
1634
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001635static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1636 struct rt6_info *rt, int mtu)
1637{
1638 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1639 * lowest MTU in the path: always allow updating the route PMTU to
1640 * reflect PMTU decreases.
1641 *
1642 * If the new MTU is higher, and the route PMTU is equal to the local
1643 * MTU, this means the old MTU is the lowest in the path, so allow
1644 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1645 * handle this.
1646 */
1647
1648 if (dst_mtu(&rt->dst) >= mtu)
1649 return true;
1650
1651 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1652 return true;
1653
1654 return false;
1655}
1656
1657static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
1658 struct rt6_info *rt, int mtu)
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001659{
1660 struct rt6_exception_bucket *bucket;
1661 struct rt6_exception *rt6_ex;
1662 int i;
1663
1664 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1665 lockdep_is_held(&rt6_exception_lock));
1666
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001667 if (!bucket)
1668 return;
1669
1670 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1671 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1672 struct rt6_info *entry = rt6_ex->rt6i;
1673
1674 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
David Ahernd4ead6b2018-04-17 17:33:16 -07001675 * route), the metrics of its rt->from have already
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001676 * been updated.
1677 */
David Ahernd4ead6b2018-04-17 17:33:16 -07001678 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001679 rt6_mtu_change_route_allowed(idev, entry, mtu))
David Ahernd4ead6b2018-04-17 17:33:16 -07001680 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001681 }
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001682 bucket++;
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001683 }
1684}
1685
Wei Wangb16cb452017-10-06 12:06:00 -07001686#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
1687
1688static void rt6_exceptions_clean_tohost(struct rt6_info *rt,
1689 struct in6_addr *gateway)
1690{
1691 struct rt6_exception_bucket *bucket;
1692 struct rt6_exception *rt6_ex;
1693 struct hlist_node *tmp;
1694 int i;
1695
1696 if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1697 return;
1698
1699 spin_lock_bh(&rt6_exception_lock);
1700 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1701 lockdep_is_held(&rt6_exception_lock));
1702
1703 if (bucket) {
1704 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1705 hlist_for_each_entry_safe(rt6_ex, tmp,
1706 &bucket->chain, hlist) {
1707 struct rt6_info *entry = rt6_ex->rt6i;
1708
1709 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1710 RTF_CACHE_GATEWAY &&
1711 ipv6_addr_equal(gateway,
1712 &entry->rt6i_gateway)) {
1713 rt6_remove_exception(bucket, rt6_ex);
1714 }
1715 }
1716 bucket++;
1717 }
1718 }
1719
1720 spin_unlock_bh(&rt6_exception_lock);
1721}
1722
Wei Wangc757faa2017-10-06 12:06:01 -07001723static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1724 struct rt6_exception *rt6_ex,
1725 struct fib6_gc_args *gc_args,
1726 unsigned long now)
1727{
1728 struct rt6_info *rt = rt6_ex->rt6i;
1729
Paolo Abeni1859bac2017-10-19 16:07:11 +02001730 /* we are pruning and obsoleting aged-out and non gateway exceptions
1731 * even if others have still references to them, so that on next
1732 * dst_check() such references can be dropped.
1733 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
1734 * expired, independently from their aging, as per RFC 8201 section 4
1735 */
Wei Wang31afeb42018-01-26 11:40:17 -08001736 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
1737 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1738 RT6_TRACE("aging clone %p\n", rt);
1739 rt6_remove_exception(bucket, rt6_ex);
1740 return;
1741 }
1742 } else if (time_after(jiffies, rt->dst.expires)) {
1743 RT6_TRACE("purging expired route %p\n", rt);
Wei Wangc757faa2017-10-06 12:06:01 -07001744 rt6_remove_exception(bucket, rt6_ex);
1745 return;
Wei Wang31afeb42018-01-26 11:40:17 -08001746 }
1747
1748 if (rt->rt6i_flags & RTF_GATEWAY) {
Wei Wangc757faa2017-10-06 12:06:01 -07001749 struct neighbour *neigh;
1750 __u8 neigh_flags = 0;
1751
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001752 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
1753 if (neigh)
Wei Wangc757faa2017-10-06 12:06:01 -07001754 neigh_flags = neigh->flags;
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001755
Wei Wangc757faa2017-10-06 12:06:01 -07001756 if (!(neigh_flags & NTF_ROUTER)) {
1757 RT6_TRACE("purging route %p via non-router but gateway\n",
1758 rt);
1759 rt6_remove_exception(bucket, rt6_ex);
1760 return;
1761 }
1762 }
Wei Wang31afeb42018-01-26 11:40:17 -08001763
Wei Wangc757faa2017-10-06 12:06:01 -07001764 gc_args->more++;
1765}
1766
1767void rt6_age_exceptions(struct rt6_info *rt,
1768 struct fib6_gc_args *gc_args,
1769 unsigned long now)
1770{
1771 struct rt6_exception_bucket *bucket;
1772 struct rt6_exception *rt6_ex;
1773 struct hlist_node *tmp;
1774 int i;
1775
1776 if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1777 return;
1778
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001779 rcu_read_lock_bh();
1780 spin_lock(&rt6_exception_lock);
Wei Wangc757faa2017-10-06 12:06:01 -07001781 bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1782 lockdep_is_held(&rt6_exception_lock));
1783
1784 if (bucket) {
1785 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1786 hlist_for_each_entry_safe(rt6_ex, tmp,
1787 &bucket->chain, hlist) {
1788 rt6_age_examine_exception(bucket, rt6_ex,
1789 gc_args, now);
1790 }
1791 bucket++;
1792 }
1793 }
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07001794 spin_unlock(&rt6_exception_lock);
1795 rcu_read_unlock_bh();
Wei Wangc757faa2017-10-06 12:06:01 -07001796}
1797
David Ahern9ff74382016-06-13 13:44:19 -07001798struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
David Ahernb75cc8f2018-03-02 08:32:17 -08001799 int oif, struct flowi6 *fl6,
1800 const struct sk_buff *skb, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001802 struct fib6_node *fn, *saved_fn;
Wei Wang2b760fc2017-10-06 12:06:03 -07001803 struct rt6_info *rt, *rt_cache;
Thomas Grafc71099a2006-08-04 23:20:06 -07001804 int strict = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001806 strict |= flags & RT6_LOOKUP_F_IFACE;
David Ahernd5d32e42016-10-24 12:27:23 -07001807 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001808 if (net->ipv6.devconf_all->forwarding == 0)
1809 strict |= RT6_LOOKUP_F_REACHABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Wei Wang66f5d6c2017-10-06 12:06:10 -07001811 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
David S. Miller4c9483b2011-03-12 16:22:43 -05001813 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001814 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
David Ahernca254492015-10-12 11:47:10 -07001816 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1817 oif = 0;
1818
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001819redo_rt6_select:
Wei Wang8d1040e2017-10-06 12:06:08 -07001820 rt = rt6_select(net, fn, oif, strict);
Nicolas Dichtel52bd4c02013-06-28 17:35:48 +02001821 if (rt->rt6i_nsiblings)
David Ahernb4bac172018-03-02 08:32:18 -08001822 rt = rt6_multipath_select(net, rt, fl6, oif, skb, strict);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001823 if (rt == net->ipv6.ip6_null_entry) {
1824 fn = fib6_backtrack(fn, &fl6->saddr);
1825 if (fn)
1826 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001827 else if (strict & RT6_LOOKUP_F_REACHABLE) {
1828 /* also consider unreachable route */
1829 strict &= ~RT6_LOOKUP_F_REACHABLE;
1830 fn = saved_fn;
1831 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07001832 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001833 }
1834
Wei Wang2b760fc2017-10-06 12:06:03 -07001835 /*Search through exception table */
1836 rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr);
1837 if (rt_cache)
1838 rt = rt_cache;
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -08001839
Wei Wangd3843fe2017-10-06 12:06:06 -07001840 if (rt == net->ipv6.ip6_null_entry) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001841 rcu_read_unlock();
Wei Wangd3843fe2017-10-06 12:06:06 -07001842 dst_hold(&rt->dst);
Paolo Abenib65f1642017-10-19 09:31:43 +02001843 trace_fib6_table_lookup(net, rt, table, fl6);
Wei Wangd3843fe2017-10-06 12:06:06 -07001844 return rt;
1845 } else if (rt->rt6i_flags & RTF_CACHE) {
David Ahernd4ead6b2018-04-17 17:33:16 -07001846 if (ip6_hold_safe(net, &rt, true))
Wei Wangd3843fe2017-10-06 12:06:06 -07001847 dst_use_noref(&rt->dst, jiffies);
David Ahernd4ead6b2018-04-17 17:33:16 -07001848
Wei Wang66f5d6c2017-10-06 12:06:10 -07001849 rcu_read_unlock();
Paolo Abenib65f1642017-10-19 09:31:43 +02001850 trace_fib6_table_lookup(net, rt, table, fl6);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001851 return rt;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001852 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
1853 !(rt->rt6i_flags & RTF_GATEWAY))) {
1854 /* Create a RTF_CACHE clone which will not be
1855 * owned by the fib6 tree. It is for the special case where
1856 * the daddr in the skb during the neighbor look-up is different
1857 * from the fl6->daddr used to look-up route here.
1858 */
Thomas Grafc71099a2006-08-04 23:20:06 -07001859
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001860 struct rt6_info *uncached_rt;
1861
Wei Wangd3843fe2017-10-06 12:06:06 -07001862 if (ip6_hold_safe(net, &rt, true)) {
1863 dst_use_noref(&rt->dst, jiffies);
1864 } else {
Wei Wang66f5d6c2017-10-06 12:06:10 -07001865 rcu_read_unlock();
Wei Wangd3843fe2017-10-06 12:06:06 -07001866 uncached_rt = rt;
1867 goto uncached_rt_out;
1868 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07001869 rcu_read_unlock();
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001870
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001871 uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL);
1872 dst_release(&rt->dst);
1873
Wei Wang1cfb71e2017-06-17 10:42:33 -07001874 if (uncached_rt) {
1875 /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
1876 * No need for another dst_hold()
1877 */
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07001878 rt6_uncached_list_add(uncached_rt);
Wei Wang81eb8442017-10-06 12:06:11 -07001879 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Wei Wang1cfb71e2017-06-17 10:42:33 -07001880 } else {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001881 uncached_rt = net->ipv6.ip6_null_entry;
Wei Wang1cfb71e2017-06-17 10:42:33 -07001882 dst_hold(&uncached_rt->dst);
1883 }
David Ahernb8115802015-11-19 12:24:22 -08001884
Wei Wangd3843fe2017-10-06 12:06:06 -07001885uncached_rt_out:
Paolo Abenib65f1642017-10-19 09:31:43 +02001886 trace_fib6_table_lookup(net, uncached_rt, table, fl6);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001887 return uncached_rt;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07001888
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001889 } else {
1890 /* Get a percpu copy */
1891
1892 struct rt6_info *pcpu_rt;
1893
Wei Wangd3843fe2017-10-06 12:06:06 -07001894 dst_use_noref(&rt->dst, jiffies);
Eric Dumazet951f7882017-10-08 21:07:18 -07001895 local_bh_disable();
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001896 pcpu_rt = rt6_get_pcpu_route(rt);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001897
Eric Dumazet951f7882017-10-08 21:07:18 -07001898 if (!pcpu_rt) {
Wei Wanga94b9362017-10-06 12:06:04 -07001899 /* atomic_inc_not_zero() is needed when using rcu */
1900 if (atomic_inc_not_zero(&rt->rt6i_ref)) {
Eric Dumazet951f7882017-10-08 21:07:18 -07001901 /* No dst_hold() on rt is needed because grabbing
Wei Wanga94b9362017-10-06 12:06:04 -07001902 * rt->rt6i_ref makes sure rt can't be released.
1903 */
David Ahernafb1d4b52018-04-17 17:33:11 -07001904 pcpu_rt = rt6_make_pcpu_route(net, rt);
Wei Wanga94b9362017-10-06 12:06:04 -07001905 rt6_release(rt);
1906 } else {
1907 /* rt is already removed from tree */
Wei Wanga94b9362017-10-06 12:06:04 -07001908 pcpu_rt = net->ipv6.ip6_null_entry;
1909 dst_hold(&pcpu_rt->dst);
1910 }
Martin KaFai Lau9c7370a2015-08-14 11:05:54 -07001911 }
Eric Dumazet951f7882017-10-08 21:07:18 -07001912 local_bh_enable();
1913 rcu_read_unlock();
Paolo Abenib65f1642017-10-19 09:31:43 +02001914 trace_fib6_table_lookup(net, pcpu_rt, table, fl6);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001915 return pcpu_rt;
1916 }
Thomas Grafc71099a2006-08-04 23:20:06 -07001917}
David Ahern9ff74382016-06-13 13:44:19 -07001918EXPORT_SYMBOL_GPL(ip6_pol_route);
Thomas Grafc71099a2006-08-04 23:20:06 -07001919
David Ahernb75cc8f2018-03-02 08:32:17 -08001920static struct rt6_info *ip6_pol_route_input(struct net *net,
1921 struct fib6_table *table,
1922 struct flowi6 *fl6,
1923 const struct sk_buff *skb,
1924 int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -07001925{
David Ahernb75cc8f2018-03-02 08:32:17 -08001926 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -07001927}
1928
Mahesh Bandeward409b842016-09-16 12:59:08 -07001929struct dst_entry *ip6_route_input_lookup(struct net *net,
1930 struct net_device *dev,
David Ahernb75cc8f2018-03-02 08:32:17 -08001931 struct flowi6 *fl6,
1932 const struct sk_buff *skb,
1933 int flags)
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001934{
1935 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
1936 flags |= RT6_LOOKUP_F_IFACE;
1937
David Ahernb75cc8f2018-03-02 08:32:17 -08001938 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001939}
Mahesh Bandeward409b842016-09-16 12:59:08 -07001940EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00001941
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001942static void ip6_multipath_l3_keys(const struct sk_buff *skb,
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001943 struct flow_keys *keys,
1944 struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001945{
1946 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
1947 const struct ipv6hdr *key_iph = outer_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001948 struct flow_keys *_flkeys = flkeys;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001949 const struct ipv6hdr *inner_iph;
1950 const struct icmp6hdr *icmph;
1951 struct ipv6hdr _inner_iph;
1952
1953 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
1954 goto out;
1955
1956 icmph = icmp6_hdr(skb);
1957 if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
1958 icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
1959 icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
1960 icmph->icmp6_type != ICMPV6_PARAMPROB)
1961 goto out;
1962
1963 inner_iph = skb_header_pointer(skb,
1964 skb_transport_offset(skb) + sizeof(*icmph),
1965 sizeof(_inner_iph), &_inner_iph);
1966 if (!inner_iph)
1967 goto out;
1968
1969 key_iph = inner_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001970 _flkeys = NULL;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001971out:
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05001972 if (_flkeys) {
1973 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
1974 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
1975 keys->tags.flow_label = _flkeys->tags.flow_label;
1976 keys->basic.ip_proto = _flkeys->basic.ip_proto;
1977 } else {
1978 keys->addrs.v6addrs.src = key_iph->saddr;
1979 keys->addrs.v6addrs.dst = key_iph->daddr;
1980 keys->tags.flow_label = ip6_flowinfo(key_iph);
1981 keys->basic.ip_proto = key_iph->nexthdr;
1982 }
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001983}
1984
1985/* if skb is set it will be used and fl6 can be NULL */
David Ahernb4bac172018-03-02 08:32:18 -08001986u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
1987 const struct sk_buff *skb, struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001988{
1989 struct flow_keys hash_keys;
David Ahern9a2a5372018-03-02 08:32:15 -08001990 u32 mhash;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02001991
David S. Millerbbfa0472018-03-12 11:09:33 -04001992 switch (ip6_multipath_hash_policy(net)) {
David Ahernb4bac172018-03-02 08:32:18 -08001993 case 0:
1994 memset(&hash_keys, 0, sizeof(hash_keys));
1995 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1996 if (skb) {
1997 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
1998 } else {
1999 hash_keys.addrs.v6addrs.src = fl6->saddr;
2000 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2001 hash_keys.tags.flow_label = (__force u32)fl6->flowlabel;
2002 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2003 }
2004 break;
2005 case 1:
2006 if (skb) {
2007 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2008 struct flow_keys keys;
2009
2010 /* short-circuit if we already have L4 hash present */
2011 if (skb->l4_hash)
2012 return skb_get_hash_raw(skb) >> 1;
2013
2014 memset(&hash_keys, 0, sizeof(hash_keys));
2015
2016 if (!flkeys) {
2017 skb_flow_dissect_flow_keys(skb, &keys, flag);
2018 flkeys = &keys;
2019 }
2020 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2021 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2022 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2023 hash_keys.ports.src = flkeys->ports.src;
2024 hash_keys.ports.dst = flkeys->ports.dst;
2025 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2026 } else {
2027 memset(&hash_keys, 0, sizeof(hash_keys));
2028 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2029 hash_keys.addrs.v6addrs.src = fl6->saddr;
2030 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2031 hash_keys.ports.src = fl6->fl6_sport;
2032 hash_keys.ports.dst = fl6->fl6_dport;
2033 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2034 }
2035 break;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002036 }
David Ahern9a2a5372018-03-02 08:32:15 -08002037 mhash = flow_hash_from_keys(&hash_keys);
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002038
David Ahern9a2a5372018-03-02 08:32:15 -08002039 return mhash >> 1;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002040}
2041
Thomas Grafc71099a2006-08-04 23:20:06 -07002042void ip6_route_input(struct sk_buff *skb)
2043{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002044 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002045 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002046 int flags = RT6_LOOKUP_F_HAS_SADDR;
Jiri Benc904af042015-08-20 13:56:31 +02002047 struct ip_tunnel_info *tun_info;
David S. Miller4c9483b2011-03-12 16:22:43 -05002048 struct flowi6 fl6 = {
David Aherne0d56fd2016-09-10 12:09:57 -07002049 .flowi6_iif = skb->dev->ifindex,
David S. Miller4c9483b2011-03-12 16:22:43 -05002050 .daddr = iph->daddr,
2051 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002052 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05002053 .flowi6_mark = skb->mark,
2054 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07002055 };
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002056 struct flow_keys *flkeys = NULL, _flkeys;
Thomas Grafadaa70b2006-10-13 15:01:03 -07002057
Jiri Benc904af042015-08-20 13:56:31 +02002058 tun_info = skb_tunnel_info(skb);
Jiri Benc46fa0622015-08-28 20:48:19 +02002059 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
Jiri Benc904af042015-08-20 13:56:31 +02002060 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002061
2062 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2063 flkeys = &_flkeys;
2064
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002065 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
David Ahernb4bac172018-03-02 08:32:18 -08002066 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
Jiri Benc06e9d042015-08-20 13:56:26 +02002067 skb_dst_drop(skb);
David Ahernb75cc8f2018-03-02 08:32:17 -08002068 skb_dst_set(skb,
2069 ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07002070}
2071
David Ahernb75cc8f2018-03-02 08:32:17 -08002072static struct rt6_info *ip6_pol_route_output(struct net *net,
2073 struct fib6_table *table,
2074 struct flowi6 *fl6,
2075 const struct sk_buff *skb,
2076 int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002077{
David Ahernb75cc8f2018-03-02 08:32:17 -08002078 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07002079}
2080
Paolo Abeni6f21c962016-01-29 12:30:19 +01002081struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
2082 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002083{
David Ahernd46a9d62015-10-21 08:42:22 -07002084 bool any_src;
Thomas Grafc71099a2006-08-04 23:20:06 -07002085
David Ahern4c1feac2016-09-10 12:09:56 -07002086 if (rt6_need_strict(&fl6->daddr)) {
2087 struct dst_entry *dst;
2088
2089 dst = l3mdev_link_scope_lookup(net, fl6);
2090 if (dst)
2091 return dst;
2092 }
David Ahernca254492015-10-12 11:47:10 -07002093
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002094 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00002095
David Ahernd46a9d62015-10-21 08:42:22 -07002096 any_src = ipv6_addr_any(&fl6->saddr);
David Ahern741a11d2015-09-28 10:12:13 -07002097 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
David Ahernd46a9d62015-10-21 08:42:22 -07002098 (fl6->flowi6_oif && any_src))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07002099 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07002100
David Ahernd46a9d62015-10-21 08:42:22 -07002101 if (!any_src)
Thomas Grafadaa70b2006-10-13 15:01:03 -07002102 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00002103 else if (sk)
2104 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002105
David Ahernb75cc8f2018-03-02 08:32:17 -08002106 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
Paolo Abeni6f21c962016-01-29 12:30:19 +01002108EXPORT_SYMBOL_GPL(ip6_route_output_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
David S. Miller2774c132011-03-01 14:59:04 -08002110struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07002111{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002112 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
Wei Wang1dbe32522017-06-17 10:42:26 -07002113 struct net_device *loopback_dev = net->loopback_dev;
David S. Miller14e50e52007-05-24 18:17:54 -07002114 struct dst_entry *new = NULL;
2115
Wei Wang1dbe32522017-06-17 10:42:26 -07002116 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
Steffen Klassert62cf27e2017-10-09 08:39:43 +02002117 DST_OBSOLETE_DEAD, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07002118 if (rt) {
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002119 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002120 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002121
Changli Gaod8d1f302010-06-10 23:31:35 -07002122 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07002123 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08002124 new->input = dst_discard;
Eric W. Biedermanede20592015-10-07 16:48:47 -05002125 new->output = dst_discard_out;
David S. Miller14e50e52007-05-24 18:17:54 -07002126
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002127 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07002128
Wei Wang1dbe32522017-06-17 10:42:26 -07002129 rt->rt6i_idev = in6_dev_get(loopback_dev);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002130 rt->rt6i_gateway = ort->rt6i_gateway;
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002131 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
David S. Miller14e50e52007-05-24 18:17:54 -07002132 rt->rt6i_metric = 0;
2133
2134 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2135#ifdef CONFIG_IPV6_SUBTREES
2136 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2137#endif
David S. Miller14e50e52007-05-24 18:17:54 -07002138 }
2139
David S. Miller69ead7a2011-03-01 14:45:33 -08002140 dst_release(dst_orig);
2141 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07002142}
David S. Miller14e50e52007-05-24 18:17:54 -07002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144/*
2145 * Destination cache support functions
2146 */
2147
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002148static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie)
2149{
Steffen Klassert36143642017-08-25 09:05:42 +02002150 u32 rt_cookie = 0;
Wei Wangc5cff852017-08-21 09:47:10 -07002151
2152 if (!rt6_get_cookie_safe(rt, &rt_cookie) || rt_cookie != cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002153 return NULL;
2154
2155 if (rt6_check_expired(rt))
2156 return NULL;
2157
2158 return &rt->dst;
2159}
2160
2161static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie)
2162{
Martin KaFai Lau5973fb12015-11-11 11:51:07 -08002163 if (!__rt6_check_expired(rt) &&
2164 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
David Miller3a2232e2017-11-28 15:40:40 -05002165 rt6_check(rt->from, cookie))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002166 return &rt->dst;
2167 else
2168 return NULL;
2169}
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2172{
2173 struct rt6_info *rt;
2174
2175 rt = (struct rt6_info *) dst;
2176
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00002177 /* All IPV6 dsts are created with ->obsolete set to the value
2178 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2179 * into this function always.
2180 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02002181
Martin KaFai Lau02bcf4e2015-11-11 11:51:08 -08002182 if (rt->rt6i_flags & RTF_PCPU ||
David Miller3a2232e2017-11-28 15:40:40 -05002183 (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->from))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002184 return rt6_dst_from_check(rt, cookie);
2185 else
2186 return rt6_check(rt, cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
2188
2189static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2190{
2191 struct rt6_info *rt = (struct rt6_info *) dst;
2192
2193 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002194 if (rt->rt6i_flags & RTF_CACHE) {
2195 if (rt6_check_expired(rt)) {
David Ahernafb1d4b52018-04-17 17:33:11 -07002196 ip6_del_rt(dev_net(dst->dev), rt);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002197 dst = NULL;
2198 }
2199 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002201 dst = NULL;
2202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002204 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
2206
2207static void ip6_link_failure(struct sk_buff *skb)
2208{
2209 struct rt6_info *rt;
2210
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002211 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Eric Dumazetadf30902009-06-02 05:19:30 +00002213 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (rt) {
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002215 if (rt->rt6i_flags & RTF_CACHE) {
Wei Wangad65a2f2017-06-17 10:42:35 -07002216 if (dst_hold_safe(&rt->dst))
David Ahernafb1d4b52018-04-17 17:33:11 -07002217 ip6_del_rt(dev_net(rt->dst.dev), rt);
Wei Wangc5cff852017-08-21 09:47:10 -07002218 } else {
2219 struct fib6_node *fn;
2220
2221 rcu_read_lock();
2222 fn = rcu_dereference(rt->rt6i_node);
2223 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2224 fn->fn_sernum = -1;
2225 rcu_read_unlock();
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 }
2228}
2229
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002230static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2231{
2232 struct net *net = dev_net(rt->dst.dev);
2233
David Ahernd4ead6b2018-04-17 17:33:16 -07002234 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002235 rt->rt6i_flags |= RTF_MODIFIED;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002236 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2237}
2238
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002239static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2240{
2241 return !(rt->rt6i_flags & RTF_CACHE) &&
Wei Wang4e587ea2017-08-25 15:03:10 -07002242 (rt->rt6i_flags & RTF_PCPU ||
2243 rcu_access_pointer(rt->rt6i_node));
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002244}
2245
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002246static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2247 const struct ipv6hdr *iph, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Julian Anastasov0dec8792017-02-06 23:14:16 +02002249 const struct in6_addr *daddr, *saddr;
Ian Morris67ba4152014-08-24 21:53:10 +01002250 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002252 if (rt6->rt6i_flags & RTF_LOCAL)
2253 return;
2254
Xin Long19bda362016-10-28 18:18:01 +08002255 if (dst_metric_locked(dst, RTAX_MTU))
2256 return;
2257
Julian Anastasov0dec8792017-02-06 23:14:16 +02002258 if (iph) {
2259 daddr = &iph->daddr;
2260 saddr = &iph->saddr;
2261 } else if (sk) {
2262 daddr = &sk->sk_v6_daddr;
2263 saddr = &inet6_sk(sk)->saddr;
2264 } else {
2265 daddr = NULL;
2266 saddr = NULL;
2267 }
2268 dst_confirm_neigh(dst, daddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002269 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2270 if (mtu >= dst_mtu(dst))
2271 return;
David S. Miller81aded22012-06-15 14:54:11 -07002272
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002273 if (!rt6_cache_allowed_for_pmtu(rt6)) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002274 rt6_do_update_pmtu(rt6, mtu);
Wei Wang2b760fc2017-10-06 12:06:03 -07002275 /* update rt6_ex->stamp for cache */
2276 if (rt6->rt6i_flags & RTF_CACHE)
2277 rt6_update_exception_stamp_rt(rt6);
Julian Anastasov0dec8792017-02-06 23:14:16 +02002278 } else if (daddr) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002279 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01002280
David Ahernd4ead6b2018-04-17 17:33:16 -07002281 nrt6 = ip6_rt_cache_alloc(rt6->from, daddr, saddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002282 if (nrt6) {
2283 rt6_do_update_pmtu(nrt6, mtu);
David Ahernd4ead6b2018-04-17 17:33:16 -07002284 if (rt6_insert_exception(nrt6, rt6->from))
Wei Wang2b760fc2017-10-06 12:06:03 -07002285 dst_release_immediate(&nrt6->dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 }
2288}
2289
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002290static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2291 struct sk_buff *skb, u32 mtu)
2292{
2293 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
2294}
2295
David S. Miller42ae66c2012-06-15 20:01:57 -07002296void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002297 int oif, u32 mark, kuid_t uid)
David S. Miller81aded22012-06-15 14:54:11 -07002298{
2299 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2300 struct dst_entry *dst;
2301 struct flowi6 fl6;
2302
2303 memset(&fl6, 0, sizeof(fl6));
2304 fl6.flowi6_oif = oif;
Lorenzo Colitti1b3c61d2014-05-13 10:17:34 -07002305 fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
David S. Miller81aded22012-06-15 14:54:11 -07002306 fl6.daddr = iph->daddr;
2307 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002308 fl6.flowlabel = ip6_flowinfo(iph);
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002309 fl6.flowi6_uid = uid;
David S. Miller81aded22012-06-15 14:54:11 -07002310
2311 dst = ip6_route_output(net, NULL, &fl6);
2312 if (!dst->error)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002313 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
David S. Miller81aded22012-06-15 14:54:11 -07002314 dst_release(dst);
2315}
2316EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2317
2318void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2319{
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002320 struct dst_entry *dst;
2321
David S. Miller81aded22012-06-15 14:54:11 -07002322 ip6_update_pmtu(skb, sock_net(sk), mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002323 sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid);
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002324
2325 dst = __sk_dst_get(sk);
2326 if (!dst || !dst->obsolete ||
2327 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2328 return;
2329
2330 bh_lock_sock(sk);
2331 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2332 ip6_datagram_dst_update(sk, false);
2333 bh_unlock_sock(sk);
David S. Miller81aded22012-06-15 14:54:11 -07002334}
2335EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2336
Alexey Kodanev7d6850f2018-04-03 15:00:07 +03002337void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2338 const struct flowi6 *fl6)
2339{
2340#ifdef CONFIG_IPV6_SUBTREES
2341 struct ipv6_pinfo *np = inet6_sk(sk);
2342#endif
2343
2344 ip6_dst_store(sk, dst,
2345 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2346 &sk->sk_v6_daddr : NULL,
2347#ifdef CONFIG_IPV6_SUBTREES
2348 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2349 &np->saddr :
2350#endif
2351 NULL);
2352}
2353
Duan Jiongb55b76b2013-09-04 19:44:21 +08002354/* Handle redirects */
2355struct ip6rd_flowi {
2356 struct flowi6 fl6;
2357 struct in6_addr gateway;
2358};
2359
2360static struct rt6_info *__ip6_route_redirect(struct net *net,
2361 struct fib6_table *table,
2362 struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08002363 const struct sk_buff *skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002364 int flags)
2365{
2366 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
Wei Wang2b760fc2017-10-06 12:06:03 -07002367 struct rt6_info *rt, *rt_cache;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002368 struct fib6_node *fn;
2369
2370 /* Get the "current" route for this destination and
Alexander Alemayhu67c408c2017-01-07 23:53:00 +01002371 * check if the redirect has come from appropriate router.
Duan Jiongb55b76b2013-09-04 19:44:21 +08002372 *
2373 * RFC 4861 specifies that redirects should only be
2374 * accepted if they come from the nexthop to the target.
2375 * Due to the way the routes are chosen, this notion
2376 * is a bit fuzzy and one might need to check all possible
2377 * routes.
2378 */
2379
Wei Wang66f5d6c2017-10-06 12:06:10 -07002380 rcu_read_lock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08002381 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2382restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002383 for_each_fib6_node_rt_rcu(fn) {
David Ahern5e670d82018-04-17 17:33:14 -07002384 if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +02002385 continue;
David Ahern14895682018-04-17 17:33:17 -07002386 if (fib6_check_expired(rt))
Duan Jiongb55b76b2013-09-04 19:44:21 +08002387 continue;
David Ahern6edb3c92018-04-17 17:33:15 -07002388 if (rt->rt6i_flags & RTF_REJECT)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002389 break;
2390 if (!(rt->rt6i_flags & RTF_GATEWAY))
2391 continue;
David Ahern5e670d82018-04-17 17:33:14 -07002392 if (fl6->flowi6_oif != rt->fib6_nh.nh_dev->ifindex)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002393 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07002394 /* rt_cache's gateway might be different from its 'parent'
2395 * in the case of an ip redirect.
2396 * So we keep searching in the exception table if the gateway
2397 * is different.
2398 */
David Ahern5e670d82018-04-17 17:33:14 -07002399 if (!ipv6_addr_equal(&rdfl->gateway, &rt->fib6_nh.nh_gw)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07002400 rt_cache = rt6_find_cached_rt(rt,
2401 &fl6->daddr,
2402 &fl6->saddr);
2403 if (rt_cache &&
2404 ipv6_addr_equal(&rdfl->gateway,
2405 &rt_cache->rt6i_gateway)) {
2406 rt = rt_cache;
2407 break;
2408 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002409 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07002410 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002411 break;
2412 }
2413
2414 if (!rt)
2415 rt = net->ipv6.ip6_null_entry;
David Ahern6edb3c92018-04-17 17:33:15 -07002416 else if (rt->rt6i_flags & RTF_REJECT) {
Duan Jiongb55b76b2013-09-04 19:44:21 +08002417 rt = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002418 goto out;
2419 }
2420
2421 if (rt == net->ipv6.ip6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002422 fn = fib6_backtrack(fn, &fl6->saddr);
2423 if (fn)
2424 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002425 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002426
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002427out:
Wei Wangd3843fe2017-10-06 12:06:06 -07002428 ip6_hold_safe(net, &rt, true);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002429
Wei Wang66f5d6c2017-10-06 12:06:10 -07002430 rcu_read_unlock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08002431
Paolo Abenib65f1642017-10-19 09:31:43 +02002432 trace_fib6_table_lookup(net, rt, table, fl6);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002433 return rt;
2434};
2435
2436static struct dst_entry *ip6_route_redirect(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08002437 const struct flowi6 *fl6,
2438 const struct sk_buff *skb,
2439 const struct in6_addr *gateway)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002440{
2441 int flags = RT6_LOOKUP_F_HAS_SADDR;
2442 struct ip6rd_flowi rdfl;
2443
2444 rdfl.fl6 = *fl6;
2445 rdfl.gateway = *gateway;
2446
David Ahernb75cc8f2018-03-02 08:32:17 -08002447 return fib6_rule_lookup(net, &rdfl.fl6, skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002448 flags, __ip6_route_redirect);
2449}
2450
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002451void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2452 kuid_t uid)
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002453{
2454 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2455 struct dst_entry *dst;
2456 struct flowi6 fl6;
2457
2458 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03002459 fl6.flowi6_iif = LOOPBACK_IFINDEX;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002460 fl6.flowi6_oif = oif;
2461 fl6.flowi6_mark = mark;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002462 fl6.daddr = iph->daddr;
2463 fl6.saddr = iph->saddr;
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002464 fl6.flowlabel = ip6_flowinfo(iph);
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002465 fl6.flowi6_uid = uid;
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002466
David Ahernb75cc8f2018-03-02 08:32:17 -08002467 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002468 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002469 dst_release(dst);
2470}
2471EXPORT_SYMBOL_GPL(ip6_redirect);
2472
Duan Jiongc92a59e2013-08-22 12:07:35 +08002473void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
2474 u32 mark)
2475{
2476 const struct ipv6hdr *iph = ipv6_hdr(skb);
2477 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2478 struct dst_entry *dst;
2479 struct flowi6 fl6;
2480
2481 memset(&fl6, 0, sizeof(fl6));
Julian Anastasove374c612014-04-28 10:51:56 +03002482 fl6.flowi6_iif = LOOPBACK_IFINDEX;
Duan Jiongc92a59e2013-08-22 12:07:35 +08002483 fl6.flowi6_oif = oif;
2484 fl6.flowi6_mark = mark;
Duan Jiongc92a59e2013-08-22 12:07:35 +08002485 fl6.daddr = msg->dest;
2486 fl6.saddr = iph->daddr;
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002487 fl6.flowi6_uid = sock_net_uid(net, NULL);
Duan Jiongc92a59e2013-08-22 12:07:35 +08002488
David Ahernb75cc8f2018-03-02 08:32:17 -08002489 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002490 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08002491 dst_release(dst);
2492}
2493
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002494void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
2495{
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002496 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2497 sk->sk_uid);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002498}
2499EXPORT_SYMBOL_GPL(ip6_sk_redirect);
2500
David S. Miller0dbaee32010-12-13 12:52:14 -08002501static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502{
David S. Miller0dbaee32010-12-13 12:52:14 -08002503 struct net_device *dev = dst->dev;
2504 unsigned int mtu = dst_mtu(dst);
2505 struct net *net = dev_net(dev);
2506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
2508
Daniel Lezcano55786892008-03-04 13:47:47 -08002509 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
2510 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002513 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
2514 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
2515 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 * rely only on pmtu discovery"
2517 */
2518 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
2519 mtu = IPV6_MAXPLEN;
2520 return mtu;
2521}
2522
Steffen Klassertebb762f2011-11-23 02:12:51 +00002523static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002524{
David S. Millerd33e4552010-12-14 13:01:14 -08002525 struct inet6_dev *idev;
David Ahernd4ead6b2018-04-17 17:33:16 -07002526 unsigned int mtu;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002527
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002528 mtu = dst_metric_raw(dst, RTAX_MTU);
2529 if (mtu)
2530 goto out;
2531
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002532 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08002533
2534 rcu_read_lock();
2535 idev = __in6_dev_get(dst->dev);
2536 if (idev)
2537 mtu = idev->cnf.mtu6;
2538 rcu_read_unlock();
2539
Eric Dumazet30f78d82014-04-10 21:23:36 -07002540out:
Roopa Prabhu14972cb2016-08-24 20:10:43 -07002541 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2542
2543 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
David S. Millerd33e4552010-12-14 13:01:14 -08002544}
2545
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08002546struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05002547 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
David S. Miller87a11572011-12-06 17:04:13 -05002549 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 struct rt6_info *rt;
2551 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002552 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
David S. Miller38308472011-12-03 18:02:47 -05002554 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00002555 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Martin KaFai Lauad706862015-08-14 11:05:52 -07002557 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05002558 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05002560 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 goto out;
2562 }
2563
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002564 rt->dst.flags |= DST_HOST;
Brendan McGrath588753f2017-12-13 22:14:57 +11002565 rt->dst.input = ip6_input;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002566 rt->dst.output = ip6_output;
Julian Anastasov550bab42013-10-20 15:43:04 +03002567 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05002568 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00002569 rt->rt6i_dst.plen = 128;
2570 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08002571 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Ido Schimmel4c981e22018-01-07 12:45:04 +02002573 /* Add this dst into uncached_list so that rt6_disable_ip() can
Wei Wang587fea72017-06-17 10:42:36 -07002574 * do proper release of the net_device
2575 */
2576 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002577 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
David S. Miller87a11572011-12-06 17:04:13 -05002579 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
2580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581out:
David S. Miller87a11572011-12-06 17:04:13 -05002582 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
Daniel Lezcano569d3642008-01-18 03:56:57 -08002585static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00002587 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08002588 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
2589 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
2590 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
2591 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
2592 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00002593 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Eric Dumazetfc66f952010-10-08 06:37:34 +00002595 entries = dst_entries_get_fast(ops);
Michal Kubeček49a18d82013-08-01 10:04:24 +02002596 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00002597 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 goto out;
2599
Benjamin Thery6891a342008-03-04 13:49:47 -08002600 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08002601 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00002602 entries = dst_entries_get_slow(ops);
2603 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08002604 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08002606 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00002607 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
2609
David Ahernd4ead6b2018-04-17 17:33:16 -07002610static int ip6_convert_metrics(struct net *net, struct rt6_info *rt,
2611 struct fib6_config *cfg)
Florian Westphale715b6d2015-01-05 23:57:44 +01002612{
David Ahernd4ead6b2018-04-17 17:33:16 -07002613 int err = 0;
Florian Westphale715b6d2015-01-05 23:57:44 +01002614
David Ahernd4ead6b2018-04-17 17:33:16 -07002615 if (cfg->fc_mx) {
2616 rt->fib6_metrics = kzalloc(sizeof(*rt->fib6_metrics),
2617 GFP_KERNEL);
2618 if (unlikely(!rt->fib6_metrics))
2619 return -ENOMEM;
Florian Westphale715b6d2015-01-05 23:57:44 +01002620
David Ahernd4ead6b2018-04-17 17:33:16 -07002621 refcount_set(&rt->fib6_metrics->refcnt, 1);
Florian Westphale715b6d2015-01-05 23:57:44 +01002622
David Ahernd4ead6b2018-04-17 17:33:16 -07002623 err = ip_metrics_convert(net, cfg->fc_mx, cfg->fc_mx_len,
2624 rt->fib6_metrics->metrics);
Florian Westphale715b6d2015-01-05 23:57:44 +01002625 }
2626
David Ahernd4ead6b2018-04-17 17:33:16 -07002627 return err;
Florian Westphale715b6d2015-01-05 23:57:44 +01002628}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
David Ahern8c145862016-04-24 21:26:04 -07002630static struct rt6_info *ip6_nh_lookup_table(struct net *net,
2631 struct fib6_config *cfg,
David Ahernf4797b32018-01-25 16:55:08 -08002632 const struct in6_addr *gw_addr,
2633 u32 tbid, int flags)
David Ahern8c145862016-04-24 21:26:04 -07002634{
2635 struct flowi6 fl6 = {
2636 .flowi6_oif = cfg->fc_ifindex,
2637 .daddr = *gw_addr,
2638 .saddr = cfg->fc_prefsrc,
2639 };
2640 struct fib6_table *table;
2641 struct rt6_info *rt;
David Ahern8c145862016-04-24 21:26:04 -07002642
David Ahernf4797b32018-01-25 16:55:08 -08002643 table = fib6_get_table(net, tbid);
David Ahern8c145862016-04-24 21:26:04 -07002644 if (!table)
2645 return NULL;
2646
2647 if (!ipv6_addr_any(&cfg->fc_prefsrc))
2648 flags |= RT6_LOOKUP_F_HAS_SADDR;
2649
David Ahernf4797b32018-01-25 16:55:08 -08002650 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
David Ahernb75cc8f2018-03-02 08:32:17 -08002651 rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
David Ahern8c145862016-04-24 21:26:04 -07002652
2653 /* if table lookup failed, fall back to full lookup */
2654 if (rt == net->ipv6.ip6_null_entry) {
2655 ip6_rt_put(rt);
2656 rt = NULL;
2657 }
2658
2659 return rt;
2660}
2661
David Ahernfc1e64e2018-01-25 16:55:09 -08002662static int ip6_route_check_nh_onlink(struct net *net,
2663 struct fib6_config *cfg,
David Ahern9fbb7042018-03-13 08:29:36 -07002664 const struct net_device *dev,
David Ahernfc1e64e2018-01-25 16:55:09 -08002665 struct netlink_ext_ack *extack)
2666{
David Ahern44750f82018-02-06 13:17:06 -08002667 u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
David Ahernfc1e64e2018-01-25 16:55:09 -08002668 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2669 u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
2670 struct rt6_info *grt;
2671 int err;
2672
2673 err = 0;
2674 grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
2675 if (grt) {
David Ahern58e354c2018-02-06 12:14:12 -08002676 if (!grt->dst.error &&
2677 (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
David Ahern44750f82018-02-06 13:17:06 -08002678 NL_SET_ERR_MSG(extack,
2679 "Nexthop has invalid gateway or device mismatch");
David Ahernfc1e64e2018-01-25 16:55:09 -08002680 err = -EINVAL;
2681 }
2682
2683 ip6_rt_put(grt);
2684 }
2685
2686 return err;
2687}
2688
David Ahern1edce992018-01-25 16:55:07 -08002689static int ip6_route_check_nh(struct net *net,
2690 struct fib6_config *cfg,
2691 struct net_device **_dev,
2692 struct inet6_dev **idev)
2693{
2694 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2695 struct net_device *dev = _dev ? *_dev : NULL;
2696 struct rt6_info *grt = NULL;
2697 int err = -EHOSTUNREACH;
2698
2699 if (cfg->fc_table) {
David Ahernf4797b32018-01-25 16:55:08 -08002700 int flags = RT6_LOOKUP_F_IFACE;
2701
2702 grt = ip6_nh_lookup_table(net, cfg, gw_addr,
2703 cfg->fc_table, flags);
David Ahern1edce992018-01-25 16:55:07 -08002704 if (grt) {
2705 if (grt->rt6i_flags & RTF_GATEWAY ||
2706 (dev && dev != grt->dst.dev)) {
2707 ip6_rt_put(grt);
2708 grt = NULL;
2709 }
2710 }
2711 }
2712
2713 if (!grt)
David Ahernb75cc8f2018-03-02 08:32:17 -08002714 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
David Ahern1edce992018-01-25 16:55:07 -08002715
2716 if (!grt)
2717 goto out;
2718
2719 if (dev) {
2720 if (dev != grt->dst.dev) {
2721 ip6_rt_put(grt);
2722 goto out;
2723 }
2724 } else {
2725 *_dev = dev = grt->dst.dev;
2726 *idev = grt->rt6i_idev;
2727 dev_hold(dev);
2728 in6_dev_hold(grt->rt6i_idev);
2729 }
2730
2731 if (!(grt->rt6i_flags & RTF_GATEWAY))
2732 err = 0;
2733
2734 ip6_rt_put(grt);
2735
2736out:
2737 return err;
2738}
2739
David Ahern9fbb7042018-03-13 08:29:36 -07002740static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
2741 struct net_device **_dev, struct inet6_dev **idev,
2742 struct netlink_ext_ack *extack)
2743{
2744 const struct in6_addr *gw_addr = &cfg->fc_gateway;
2745 int gwa_type = ipv6_addr_type(gw_addr);
David Ahern232378e2018-03-13 08:29:37 -07002746 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
David Ahern9fbb7042018-03-13 08:29:36 -07002747 const struct net_device *dev = *_dev;
David Ahern232378e2018-03-13 08:29:37 -07002748 bool need_addr_check = !dev;
David Ahern9fbb7042018-03-13 08:29:36 -07002749 int err = -EINVAL;
2750
2751 /* if gw_addr is local we will fail to detect this in case
2752 * address is still TENTATIVE (DAD in progress). rt6_lookup()
2753 * will return already-added prefix route via interface that
2754 * prefix route was assigned to, which might be non-loopback.
2755 */
David Ahern232378e2018-03-13 08:29:37 -07002756 if (dev &&
2757 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2758 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
David Ahern9fbb7042018-03-13 08:29:36 -07002759 goto out;
2760 }
2761
2762 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
2763 /* IPv6 strictly inhibits using not link-local
2764 * addresses as nexthop address.
2765 * Otherwise, router will not able to send redirects.
2766 * It is very good, but in some (rare!) circumstances
2767 * (SIT, PtP, NBMA NOARP links) it is handy to allow
2768 * some exceptions. --ANK
2769 * We allow IPv4-mapped nexthops to support RFC4798-type
2770 * addressing
2771 */
2772 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
2773 NL_SET_ERR_MSG(extack, "Invalid gateway address");
2774 goto out;
2775 }
2776
2777 if (cfg->fc_flags & RTNH_F_ONLINK)
2778 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
2779 else
2780 err = ip6_route_check_nh(net, cfg, _dev, idev);
2781
2782 if (err)
2783 goto out;
2784 }
2785
2786 /* reload in case device was changed */
2787 dev = *_dev;
2788
2789 err = -EINVAL;
2790 if (!dev) {
2791 NL_SET_ERR_MSG(extack, "Egress device not specified");
2792 goto out;
2793 } else if (dev->flags & IFF_LOOPBACK) {
2794 NL_SET_ERR_MSG(extack,
2795 "Egress device can not be loopback device for this route");
2796 goto out;
2797 }
David Ahern232378e2018-03-13 08:29:37 -07002798
2799 /* if we did not check gw_addr above, do so now that the
2800 * egress device has been resolved.
2801 */
2802 if (need_addr_check &&
2803 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2804 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
2805 goto out;
2806 }
2807
David Ahern9fbb7042018-03-13 08:29:36 -07002808 err = 0;
2809out:
2810 return err;
2811}
2812
David Ahern333c4302017-05-21 10:12:04 -06002813static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg,
2814 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
Daniel Lezcano55786892008-03-04 13:47:47 -08002816 struct net *net = cfg->fc_nlinfo.nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 struct rt6_info *rt = NULL;
2818 struct net_device *dev = NULL;
2819 struct inet6_dev *idev = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07002820 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 int addr_type;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002822 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
David Ahern557c44b2017-04-19 14:19:43 -07002824 /* RTF_PCPU is an internal flag; can not be set by userspace */
David Ahernd5d531c2017-05-21 10:12:05 -06002825 if (cfg->fc_flags & RTF_PCPU) {
2826 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
David Ahern557c44b2017-04-19 14:19:43 -07002827 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06002828 }
David Ahern557c44b2017-04-19 14:19:43 -07002829
Wei Wang2ea23522017-10-27 17:30:12 -07002830 /* RTF_CACHE is an internal flag; can not be set by userspace */
2831 if (cfg->fc_flags & RTF_CACHE) {
2832 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
2833 goto out;
2834 }
2835
David Aherne8478e82018-04-17 17:33:13 -07002836 if (cfg->fc_type > RTN_MAX) {
2837 NL_SET_ERR_MSG(extack, "Invalid route type");
2838 goto out;
2839 }
2840
David Ahernd5d531c2017-05-21 10:12:05 -06002841 if (cfg->fc_dst_len > 128) {
2842 NL_SET_ERR_MSG(extack, "Invalid prefix length");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002843 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06002844 }
2845 if (cfg->fc_src_len > 128) {
2846 NL_SET_ERR_MSG(extack, "Invalid source address length");
2847 goto out;
2848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849#ifndef CONFIG_IPV6_SUBTREES
David Ahernd5d531c2017-05-21 10:12:05 -06002850 if (cfg->fc_src_len) {
2851 NL_SET_ERR_MSG(extack,
2852 "Specifying source address requires IPV6_SUBTREES to be enabled");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07002853 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06002854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855#endif
Thomas Graf86872cb2006-08-22 00:01:08 -07002856 if (cfg->fc_ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 err = -ENODEV;
Daniel Lezcano55786892008-03-04 13:47:47 -08002858 dev = dev_get_by_index(net, cfg->fc_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 if (!dev)
2860 goto out;
2861 idev = in6_dev_get(dev);
2862 if (!idev)
2863 goto out;
2864 }
2865
Thomas Graf86872cb2006-08-22 00:01:08 -07002866 if (cfg->fc_metric == 0)
2867 cfg->fc_metric = IP6_RT_PRIO_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
David Ahernfc1e64e2018-01-25 16:55:09 -08002869 if (cfg->fc_flags & RTNH_F_ONLINK) {
2870 if (!dev) {
2871 NL_SET_ERR_MSG(extack,
2872 "Nexthop device required for onlink");
2873 err = -ENODEV;
2874 goto out;
2875 }
2876
2877 if (!(dev->flags & IFF_UP)) {
2878 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2879 err = -ENETDOWN;
2880 goto out;
2881 }
2882 }
2883
Matti Vaittinend71314b2011-11-14 00:14:49 +00002884 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05002885 if (cfg->fc_nlinfo.nlh &&
2886 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00002887 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05002888 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00002889 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00002890 table = fib6_new_table(net, cfg->fc_table);
2891 }
2892 } else {
2893 table = fib6_new_table(net, cfg->fc_table);
2894 }
David S. Miller38308472011-12-03 18:02:47 -05002895
2896 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07002897 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07002898
Martin KaFai Lauad706862015-08-14 11:05:52 -07002899 rt = ip6_dst_alloc(net, NULL,
2900 (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
David S. Miller38308472011-12-03 18:02:47 -05002902 if (!rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 err = -ENOMEM;
2904 goto out;
2905 }
2906
David Ahernd4ead6b2018-04-17 17:33:16 -07002907 err = ip6_convert_metrics(net, rt, cfg);
2908 if (err < 0)
2909 goto out;
2910
Gao feng1716a962012-04-06 00:13:10 +00002911 if (cfg->fc_flags & RTF_EXPIRES)
David Ahern14895682018-04-17 17:33:17 -07002912 fib6_set_expires(rt, jiffies +
Gao feng1716a962012-04-06 00:13:10 +00002913 clock_t_to_jiffies(cfg->fc_expires));
2914 else
David Ahern14895682018-04-17 17:33:17 -07002915 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Thomas Graf86872cb2006-08-22 00:01:08 -07002917 if (cfg->fc_protocol == RTPROT_UNSPEC)
2918 cfg->fc_protocol = RTPROT_BOOT;
2919 rt->rt6i_protocol = cfg->fc_protocol;
2920
2921 addr_type = ipv6_addr_type(&cfg->fc_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002923 if (cfg->fc_encap) {
2924 struct lwtunnel_state *lwtstate;
2925
David Ahern30357d72017-01-30 12:07:37 -08002926 err = lwtunnel_build_state(cfg->fc_encap_type,
Tom Herbert127eb7c2015-08-24 09:45:41 -07002927 cfg->fc_encap, AF_INET6, cfg,
David Ahern9ae28722017-05-27 16:19:28 -06002928 &lwtstate, extack);
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002929 if (err)
2930 goto out;
David Ahern5e670d82018-04-17 17:33:14 -07002931 rt->fib6_nh.nh_lwtstate = lwtstate_get(lwtstate);
Roopa Prabhu19e42e42015-07-21 10:43:48 +02002932 }
2933
Thomas Graf86872cb2006-08-22 00:01:08 -07002934 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
2935 rt->rt6i_dst.plen = cfg->fc_dst_len;
Martin KaFai Lauafc4eef2015-04-28 13:03:07 -07002936 if (rt->rt6i_dst.plen == 128)
Michal Kubečeke5fd3872014-03-27 13:04:08 +01002937 rt->dst.flags |= DST_HOST;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01002938
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939#ifdef CONFIG_IPV6_SUBTREES
Thomas Graf86872cb2006-08-22 00:01:08 -07002940 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
2941 rt->rt6i_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942#endif
2943
Thomas Graf86872cb2006-08-22 00:01:08 -07002944 rt->rt6i_metric = cfg->fc_metric;
David Ahern5e670d82018-04-17 17:33:14 -07002945 rt->fib6_nh.nh_weight = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
David Aherne8478e82018-04-17 17:33:13 -07002947 rt->fib6_type = cfg->fc_type;
2948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 /* We cannot add true routes via loopback here,
2950 they would result in kernel looping; promote them to reject routes
2951 */
Thomas Graf86872cb2006-08-22 00:01:08 -07002952 if ((cfg->fc_flags & RTF_REJECT) ||
David S. Miller38308472011-12-03 18:02:47 -05002953 (dev && (dev->flags & IFF_LOOPBACK) &&
2954 !(addr_type & IPV6_ADDR_LOOPBACK) &&
2955 !(cfg->fc_flags & RTF_LOCAL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 /* hold loopback dev/idev if we haven't done so. */
Daniel Lezcano55786892008-03-04 13:47:47 -08002957 if (dev != net->loopback_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 if (dev) {
2959 dev_put(dev);
2960 in6_dev_put(idev);
2961 }
Daniel Lezcano55786892008-03-04 13:47:47 -08002962 dev = net->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 dev_hold(dev);
2964 idev = in6_dev_get(dev);
2965 if (!idev) {
2966 err = -ENODEV;
2967 goto out;
2968 }
2969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
2971 goto install_route;
2972 }
2973
Thomas Graf86872cb2006-08-22 00:01:08 -07002974 if (cfg->fc_flags & RTF_GATEWAY) {
David Ahern9fbb7042018-03-13 08:29:36 -07002975 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
2976 if (err)
Florian Westphal48ed7b22015-05-21 00:25:41 +02002977 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
David Ahern5e670d82018-04-17 17:33:14 -07002979 rt->fib6_nh.nh_gw = rt->rt6i_gateway = cfg->fc_gateway;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 }
2981
2982 err = -ENODEV;
David S. Miller38308472011-12-03 18:02:47 -05002983 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 goto out;
2985
Lorenzo Bianconi428604f2018-03-29 11:02:24 +02002986 if (idev->cnf.disable_ipv6) {
2987 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
2988 err = -EACCES;
2989 goto out;
2990 }
2991
David Ahern955ec4c2018-01-24 19:45:29 -08002992 if (!(dev->flags & IFF_UP)) {
2993 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2994 err = -ENETDOWN;
2995 goto out;
2996 }
2997
Daniel Walterc3968a82011-04-13 21:10:57 +00002998 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
2999 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
David Ahernd5d531c2017-05-21 10:12:05 -06003000 NL_SET_ERR_MSG(extack, "Invalid source address");
Daniel Walterc3968a82011-04-13 21:10:57 +00003001 err = -EINVAL;
3002 goto out;
3003 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003004 rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
Daniel Walterc3968a82011-04-13 21:10:57 +00003005 rt->rt6i_prefsrc.plen = 128;
3006 } else
3007 rt->rt6i_prefsrc.plen = 0;
3008
Thomas Graf86872cb2006-08-22 00:01:08 -07003009 rt->rt6i_flags = cfg->fc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
3011install_route:
Ido Schimmel5609b802018-01-07 12:45:06 +02003012 if (!(rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3013 !netif_carrier_ok(dev))
David Ahern5e670d82018-04-17 17:33:14 -07003014 rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
3015 rt->fib6_nh.nh_flags |= (cfg->fc_flags & RTNH_F_ONLINK);
3016 rt->fib6_nh.nh_dev = rt->dst.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 rt->rt6i_idev = idev;
Thomas Grafc71099a2006-08-04 23:20:06 -07003018 rt->rt6i_table = table;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08003019
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003020 cfg->fc_nlinfo.nl_net = dev_net(dev);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08003021
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);
Wei Wang587fea72017-06-17 10:42:36 -07003028 if (rt)
3029 dst_release_immediate(&rt->dst);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003030
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003031 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003032}
3033
David Ahernd4ead6b2018-04-17 17:33:16 -07003034int ip6_route_add(struct fib6_config *cfg, struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003035{
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003036 struct rt6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003037 int err;
3038
David Ahern333c4302017-05-21 10:12:04 -06003039 rt = ip6_route_info_create(cfg, 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);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003044
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 return err;
3046}
3047
Thomas Graf86872cb2006-08-22 00:01:08 -07003048static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
David Ahernafb1d4b52018-04-17 17:33:11 -07003050 struct net *net = info->nl_net;
Thomas Grafc71099a2006-08-04 23:20:06 -07003051 struct fib6_table *table;
David Ahernafb1d4b52018-04-17 17:33:11 -07003052 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Wei Wanga4c2fd72017-06-17 10:42:42 -07003054 if (rt == net->ipv6.ip6_null_entry) {
Gao feng6825a262012-09-19 19:25:34 +00003055 err = -ENOENT;
3056 goto out;
3057 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07003058
Thomas Grafc71099a2006-08-04 23:20:06 -07003059 table = rt->rt6i_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003060 spin_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07003061 err = fib6_del(rt, info);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003062 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Gao feng6825a262012-09-19 19:25:34 +00003064out:
Amerigo Wang94e187c2012-10-29 00:13:19 +00003065 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 return err;
3067}
3068
David Ahernafb1d4b52018-04-17 17:33:11 -07003069int ip6_del_rt(struct net *net, struct rt6_info *rt)
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003070{
David Ahernafb1d4b52018-04-17 17:33:11 -07003071 struct nl_info info = { .nl_net = net };
3072
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003073 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003074}
3075
David Ahern0ae81332017-02-02 12:37:08 -08003076static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg)
3077{
3078 struct nl_info *info = &cfg->fc_nlinfo;
WANG Conge3330032017-02-27 16:07:43 -08003079 struct net *net = info->nl_net;
David Ahern16a16cd2017-02-02 12:37:11 -08003080 struct sk_buff *skb = NULL;
David Ahern0ae81332017-02-02 12:37:08 -08003081 struct fib6_table *table;
WANG Conge3330032017-02-27 16:07:43 -08003082 int err = -ENOENT;
David Ahern0ae81332017-02-02 12:37:08 -08003083
WANG Conge3330032017-02-27 16:07:43 -08003084 if (rt == net->ipv6.ip6_null_entry)
3085 goto out_put;
David Ahern0ae81332017-02-02 12:37:08 -08003086 table = rt->rt6i_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003087 spin_lock_bh(&table->tb6_lock);
David Ahern0ae81332017-02-02 12:37:08 -08003088
3089 if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) {
3090 struct rt6_info *sibling, *next_sibling;
3091
David Ahern16a16cd2017-02-02 12:37:11 -08003092 /* prefer to send a single notification with all hops */
3093 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3094 if (skb) {
3095 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3096
David Ahernd4ead6b2018-04-17 17:33:16 -07003097 if (rt6_fill_node(net, skb, rt, NULL,
David Ahern16a16cd2017-02-02 12:37:11 -08003098 NULL, NULL, 0, RTM_DELROUTE,
3099 info->portid, seq, 0) < 0) {
3100 kfree_skb(skb);
3101 skb = NULL;
3102 } else
3103 info->skip_notify = 1;
3104 }
3105
David Ahern0ae81332017-02-02 12:37:08 -08003106 list_for_each_entry_safe(sibling, next_sibling,
3107 &rt->rt6i_siblings,
3108 rt6i_siblings) {
3109 err = fib6_del(sibling, info);
3110 if (err)
WANG Conge3330032017-02-27 16:07:43 -08003111 goto out_unlock;
David Ahern0ae81332017-02-02 12:37:08 -08003112 }
3113 }
3114
3115 err = fib6_del(rt, info);
WANG Conge3330032017-02-27 16:07:43 -08003116out_unlock:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003117 spin_unlock_bh(&table->tb6_lock);
WANG Conge3330032017-02-27 16:07:43 -08003118out_put:
David Ahern0ae81332017-02-02 12:37:08 -08003119 ip6_rt_put(rt);
David Ahern16a16cd2017-02-02 12:37:11 -08003120
3121 if (skb) {
WANG Conge3330032017-02-27 16:07:43 -08003122 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
David Ahern16a16cd2017-02-02 12:37:11 -08003123 info->nlh, gfp_any());
3124 }
David Ahern0ae81332017-02-02 12:37:08 -08003125 return err;
3126}
3127
David Ahern333c4302017-05-21 10:12:04 -06003128static int ip6_route_del(struct fib6_config *cfg,
3129 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
Wei Wang2b760fc2017-10-06 12:06:03 -07003131 struct rt6_info *rt, *rt_cache;
Thomas Grafc71099a2006-08-04 23:20:06 -07003132 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 int err = -ESRCH;
3135
Daniel Lezcano55786892008-03-04 13:47:47 -08003136 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David Ahernd5d531c2017-05-21 10:12:05 -06003137 if (!table) {
3138 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Grafc71099a2006-08-04 23:20:06 -07003139 return err;
David Ahernd5d531c2017-05-21 10:12:05 -06003140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Wei Wang66f5d6c2017-10-06 12:06:10 -07003142 rcu_read_lock();
Thomas Grafc71099a2006-08-04 23:20:06 -07003143
3144 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07003145 &cfg->fc_dst, cfg->fc_dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07003146 &cfg->fc_src, cfg->fc_src_len,
Wei Wang2b760fc2017-10-06 12:06:03 -07003147 !(cfg->fc_flags & RTF_CACHE));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003148
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 if (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003150 for_each_fib6_node_rt_rcu(fn) {
Wei Wang2b760fc2017-10-06 12:06:03 -07003151 if (cfg->fc_flags & RTF_CACHE) {
3152 rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst,
3153 &cfg->fc_src);
3154 if (!rt_cache)
3155 continue;
3156 rt = rt_cache;
3157 }
Thomas Graf86872cb2006-08-22 00:01:08 -07003158 if (cfg->fc_ifindex &&
David Ahern5e670d82018-04-17 17:33:14 -07003159 (!rt->fib6_nh.nh_dev ||
3160 rt->fib6_nh.nh_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07003162 if (cfg->fc_flags & RTF_GATEWAY &&
David Ahern5e670d82018-04-17 17:33:14 -07003163 !ipv6_addr_equal(&cfg->fc_gateway, &rt->fib6_nh.nh_gw))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07003165 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 continue;
Mantas Mc2ed1882016-12-16 10:30:59 +02003167 if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol)
3168 continue;
Wei Wangd3843fe2017-10-06 12:06:06 -07003169 if (!dst_hold_safe(&rt->dst))
3170 break;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003171 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
David Ahern0ae81332017-02-02 12:37:08 -08003173 /* if gateway was specified only delete the one hop */
3174 if (cfg->fc_flags & RTF_GATEWAY)
3175 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3176
3177 return __ip6_del_rt_siblings(rt, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 }
3179 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003180 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
3182 return err;
3183}
3184
David S. Miller6700c272012-07-17 03:29:28 -07003185static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003186{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003187 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07003188 struct rt6_info *rt, *nrt = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07003189 struct ndisc_options ndopts;
3190 struct inet6_dev *in6_dev;
3191 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003192 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07003193 int optlen, on_link;
3194 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07003195
Simon Horman29a3cad2013-05-28 20:34:26 +00003196 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003197 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07003198
3199 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07003200 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003201 return;
3202 }
3203
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003204 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07003205
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003206 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003207 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003208 return;
3209 }
3210
David S. Miller6e157b62012-07-12 00:05:02 -07003211 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003212 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003213 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003214 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07003215 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003216 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003217 return;
3218 }
3219
3220 in6_dev = __in6_dev_get(skb->dev);
3221 if (!in6_dev)
3222 return;
3223 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3224 return;
3225
3226 /* RFC2461 8.1:
3227 * The IP source address of the Redirect MUST be the same as the current
3228 * first-hop router for the specified ICMP Destination Address.
3229 */
3230
Alexander Aringf997c552016-06-15 21:20:23 +02003231 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003232 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3233 return;
3234 }
David S. Miller6e157b62012-07-12 00:05:02 -07003235
3236 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07003237 if (ndopts.nd_opts_tgt_lladdr) {
3238 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3239 skb->dev);
3240 if (!lladdr) {
3241 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3242 return;
3243 }
3244 }
3245
David S. Miller6e157b62012-07-12 00:05:02 -07003246 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01003247 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07003248 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
3249 return;
3250 }
3251
3252 /* Redirect received -> path was valid.
3253 * Look, redirects are sent only in response to data packets,
3254 * so that this nexthop apparently is reachable. --ANK
3255 */
Julian Anastasov0dec8792017-02-06 23:14:16 +02003256 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
David S. Miller6e157b62012-07-12 00:05:02 -07003257
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003258 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07003259 if (!neigh)
3260 return;
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 /*
3263 * We have finally decided to accept it.
3264 */
3265
Alexander Aringf997c552016-06-15 21:20:23 +02003266 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 NEIGH_UPDATE_F_WEAK_OVERRIDE|
3268 NEIGH_UPDATE_F_OVERRIDE|
3269 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02003270 NEIGH_UPDATE_F_ISROUTER)),
3271 NDISC_REDIRECT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07003273 nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05003274 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 goto out;
3276
3277 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
3278 if (on_link)
3279 nrt->rt6i_flags &= ~RTF_GATEWAY;
3280
Xin Longb91d5322017-08-03 14:13:46 +08003281 nrt->rt6i_protocol = RTPROT_REDIRECT;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003282 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Wei Wang2b760fc2017-10-06 12:06:03 -07003284 /* No need to remove rt from the exception table if rt is
3285 * a cached route because rt6_insert_exception() will
3286 * takes care of it
3287 */
David Ahernd4ead6b2018-04-17 17:33:16 -07003288 if (rt6_insert_exception(nrt, rt->from)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07003289 dst_release_immediate(&nrt->dst);
3290 goto out;
3291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Changli Gaod8d1f302010-06-10 23:31:35 -07003293 netevent.old = &rt->dst;
3294 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003295 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00003296 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07003297 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
3298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299out:
David S. Millere8599ff2012-07-11 23:43:53 -07003300 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07003301}
3302
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003303#ifdef CONFIG_IPV6_ROUTE_INFO
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08003304static struct rt6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003305 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07003306 const struct in6_addr *gwaddr,
3307 struct net_device *dev)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003308{
David Ahern830218c2016-10-24 10:52:35 -07003309 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3310 int ifindex = dev->ifindex;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003311 struct fib6_node *fn;
3312 struct rt6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003313 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003314
David Ahern830218c2016-10-24 10:52:35 -07003315 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05003316 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003317 return NULL;
3318
Wei Wang66f5d6c2017-10-06 12:06:10 -07003319 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07003320 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003321 if (!fn)
3322 goto out;
3323
Wei Wang66f5d6c2017-10-06 12:06:10 -07003324 for_each_fib6_node_rt_rcu(fn) {
David Ahern5e670d82018-04-17 17:33:14 -07003325 if (rt->fib6_nh.nh_dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003326 continue;
3327 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
3328 continue;
David Ahern5e670d82018-04-17 17:33:14 -07003329 if (!ipv6_addr_equal(&rt->fib6_nh.nh_gw, gwaddr))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003330 continue;
Wei Wangd3843fe2017-10-06 12:06:06 -07003331 ip6_hold_safe(NULL, &rt, false);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003332 break;
3333 }
3334out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003335 rcu_read_unlock();
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003336 return rt;
3337}
3338
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08003339static struct rt6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003340 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07003341 const struct in6_addr *gwaddr,
3342 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +00003343 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003344{
Thomas Graf86872cb2006-08-22 00:01:08 -07003345 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08003346 .fc_metric = IP6_RT_PRIO_USER,
David Ahern830218c2016-10-24 10:52:35 -07003347 .fc_ifindex = dev->ifindex,
Thomas Graf86872cb2006-08-22 00:01:08 -07003348 .fc_dst_len = prefixlen,
3349 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
3350 RTF_UP | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08003351 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07003352 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003353 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08003354 .fc_nlinfo.nlh = NULL,
3355 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07003356 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003357
David Ahern830218c2016-10-24 10:52:35 -07003358 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003359 cfg.fc_dst = *prefix;
3360 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07003361
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08003362 /* We should treat it as a default route if prefix length is 0. */
3363 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07003364 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003365
David Ahern333c4302017-05-21 10:12:04 -06003366 ip6_route_add(&cfg, NULL);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003367
David Ahern830218c2016-10-24 10:52:35 -07003368 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08003369}
3370#endif
3371
David Ahernafb1d4b52018-04-17 17:33:11 -07003372struct rt6_info *rt6_get_dflt_router(struct net *net,
3373 const struct in6_addr *addr,
3374 struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003375{
David Ahern830218c2016-10-24 10:52:35 -07003376 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 struct rt6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07003378 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
David Ahernafb1d4b52018-04-17 17:33:11 -07003380 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05003381 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003382 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Wei Wang66f5d6c2017-10-06 12:06:10 -07003384 rcu_read_lock();
3385 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Ahern5e670d82018-04-17 17:33:14 -07003386 if (dev == rt->fib6_nh.nh_dev &&
YOSHIFUJI Hideaki045927f2006-03-20 17:00:48 -08003387 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Ahern5e670d82018-04-17 17:33:14 -07003388 ipv6_addr_equal(&rt->fib6_nh.nh_gw, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 break;
3390 }
3391 if (rt)
Wei Wangd3843fe2017-10-06 12:06:06 -07003392 ip6_hold_safe(NULL, &rt, false);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003393 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 return rt;
3395}
3396
David Ahernafb1d4b52018-04-17 17:33:11 -07003397struct rt6_info *rt6_add_dflt_router(struct net *net,
3398 const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08003399 struct net_device *dev,
3400 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401{
Thomas Graf86872cb2006-08-22 00:01:08 -07003402 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07003403 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08003404 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07003405 .fc_ifindex = dev->ifindex,
3406 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
3407 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08003408 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07003409 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003410 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08003411 .fc_nlinfo.nlh = NULL,
David Ahernafb1d4b52018-04-17 17:33:11 -07003412 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07003413 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003415 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
David Ahern333c4302017-05-21 10:12:04 -06003417 if (!ip6_route_add(&cfg, NULL)) {
David Ahern830218c2016-10-24 10:52:35 -07003418 struct fib6_table *table;
3419
3420 table = fib6_get_table(dev_net(dev), cfg.fc_table);
3421 if (table)
3422 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
David Ahernafb1d4b52018-04-17 17:33:11 -07003425 return rt6_get_dflt_router(net, gwaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
David Ahernafb1d4b52018-04-17 17:33:11 -07003428static void __rt6_purge_dflt_routers(struct net *net,
3429 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430{
3431 struct rt6_info *rt;
3432
3433restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003434 rcu_read_lock();
3435 for_each_fib6_node_rt_rcu(&table->tb6_root) {
Lorenzo Colitti3e8b0ac2013-03-03 20:46:46 +00003436 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
3437 (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
Wei Wangd3843fe2017-10-06 12:06:06 -07003438 if (dst_hold_safe(&rt->dst)) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003439 rcu_read_unlock();
David Ahernafb1d4b52018-04-17 17:33:11 -07003440 ip6_del_rt(net, rt);
Wei Wangd3843fe2017-10-06 12:06:06 -07003441 } else {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003442 rcu_read_unlock();
Wei Wangd3843fe2017-10-06 12:06:06 -07003443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 goto restart;
3445 }
3446 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003447 rcu_read_unlock();
David Ahern830218c2016-10-24 10:52:35 -07003448
3449 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3450}
3451
3452void rt6_purge_dflt_routers(struct net *net)
3453{
3454 struct fib6_table *table;
3455 struct hlist_head *head;
3456 unsigned int h;
3457
3458 rcu_read_lock();
3459
3460 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3461 head = &net->ipv6.fib_table_hash[h];
3462 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3463 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
David Ahernafb1d4b52018-04-17 17:33:11 -07003464 __rt6_purge_dflt_routers(net, table);
David Ahern830218c2016-10-24 10:52:35 -07003465 }
3466 }
3467
3468 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469}
3470
Daniel Lezcano55786892008-03-04 13:47:47 -08003471static void rtmsg_to_fib6_config(struct net *net,
3472 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07003473 struct fib6_config *cfg)
3474{
3475 memset(cfg, 0, sizeof(*cfg));
3476
David Ahernca254492015-10-12 11:47:10 -07003477 cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
3478 : RT6_TABLE_MAIN;
Thomas Graf86872cb2006-08-22 00:01:08 -07003479 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
3480 cfg->fc_metric = rtmsg->rtmsg_metric;
3481 cfg->fc_expires = rtmsg->rtmsg_info;
3482 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
3483 cfg->fc_src_len = rtmsg->rtmsg_src_len;
3484 cfg->fc_flags = rtmsg->rtmsg_flags;
David Aherne8478e82018-04-17 17:33:13 -07003485 cfg->fc_type = rtmsg->rtmsg_type;
Thomas Graf86872cb2006-08-22 00:01:08 -07003486
Daniel Lezcano55786892008-03-04 13:47:47 -08003487 cfg->fc_nlinfo.nl_net = net;
Benjamin Theryf1243c22008-02-26 18:10:03 -08003488
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003489 cfg->fc_dst = rtmsg->rtmsg_dst;
3490 cfg->fc_src = rtmsg->rtmsg_src;
3491 cfg->fc_gateway = rtmsg->rtmsg_gateway;
Thomas Graf86872cb2006-08-22 00:01:08 -07003492}
3493
Daniel Lezcano55786892008-03-04 13:47:47 -08003494int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495{
Thomas Graf86872cb2006-08-22 00:01:08 -07003496 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 struct in6_rtmsg rtmsg;
3498 int err;
3499
Ian Morris67ba4152014-08-24 21:53:10 +01003500 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 case SIOCADDRT: /* Add a route */
3502 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00003503 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 return -EPERM;
3505 err = copy_from_user(&rtmsg, arg,
3506 sizeof(struct in6_rtmsg));
3507 if (err)
3508 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07003509
Daniel Lezcano55786892008-03-04 13:47:47 -08003510 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07003511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 rtnl_lock();
3513 switch (cmd) {
3514 case SIOCADDRT:
David Ahern333c4302017-05-21 10:12:04 -06003515 err = ip6_route_add(&cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 break;
3517 case SIOCDELRT:
David Ahern333c4302017-05-21 10:12:04 -06003518 err = ip6_route_del(&cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 break;
3520 default:
3521 err = -EINVAL;
3522 }
3523 rtnl_unlock();
3524
3525 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
3528 return -EINVAL;
3529}
3530
3531/*
3532 * Drop the packet on the floor
3533 */
3534
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07003535static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003537 int type;
Eric Dumazetadf30902009-06-02 05:19:30 +00003538 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003539 switch (ipstats_mib_noroutes) {
3540 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07003541 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00003542 if (type == IPV6_ADDR_ANY) {
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -04003543 IP6_INC_STATS(dev_net(dst->dev),
3544 __in6_dev_get_safely(skb->dev),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07003545 IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003546 break;
3547 }
3548 /* FALLTHROUGH */
3549 case IPSTATS_MIB_OUTNOROUTES:
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07003550 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
3551 ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003552 break;
3553 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00003554 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 kfree_skb(skb);
3556 return 0;
3557}
3558
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003559static int ip6_pkt_discard(struct sk_buff *skb)
3560{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003561 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003562}
3563
Eric W. Biedermanede20592015-10-07 16:48:47 -05003564static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565{
Eric Dumazetadf30902009-06-02 05:19:30 +00003566 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003567 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568}
3569
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003570static int ip6_pkt_prohibit(struct sk_buff *skb)
3571{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003572 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003573}
3574
Eric W. Biedermanede20592015-10-07 16:48:47 -05003575static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003576{
Eric Dumazetadf30902009-06-02 05:19:30 +00003577 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07003578 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07003579}
3580
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581/*
3582 * Allocate a dst for local (unicast / anycast) address.
3583 */
3584
David Ahernafb1d4b52018-04-17 17:33:11 -07003585struct rt6_info *addrconf_dst_alloc(struct net *net,
3586 struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 const struct in6_addr *addr,
David S. Miller8f031512011-12-06 16:48:14 -05003588 bool anycast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589{
David Ahernca254492015-10-12 11:47:10 -07003590 u32 tb_id;
David Ahern4832c302017-08-17 12:17:20 -07003591 struct net_device *dev = idev->dev;
David Ahern5f02ce242016-09-10 12:09:54 -07003592 struct rt6_info *rt;
3593
David Ahern5f02ce242016-09-10 12:09:54 -07003594 rt = ip6_dst_alloc(net, dev, DST_NOCOUNT);
Hannes Frederic Sowaa3300ef2013-12-07 03:33:45 +01003595 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 return ERR_PTR(-ENOMEM);
3597
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 in6_dev_hold(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 rt->rt6i_idev = idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
David Ahern6edb3c92018-04-17 17:33:15 -07003601 rt->dst.flags |= DST_HOST;
David Ahern94b5e0f2017-02-02 08:52:21 -08003602 rt->rt6i_protocol = RTPROT_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
David Aherne8478e82018-04-17 17:33:13 -07003604 if (anycast) {
3605 rt->fib6_type = RTN_ANYCAST;
YOSHIFUJI Hideaki58c4fb82005-12-21 22:56:42 +09003606 rt->rt6i_flags |= RTF_ANYCAST;
David Aherne8478e82018-04-17 17:33:13 -07003607 } else {
3608 rt->fib6_type = RTN_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 rt->rt6i_flags |= RTF_LOCAL;
David Aherne8478e82018-04-17 17:33:13 -07003610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
David Ahern5e670d82018-04-17 17:33:14 -07003612 rt->fib6_nh.nh_gw = *addr;
3613 rt->fib6_nh.nh_dev = dev;
Julian Anastasov550bab42013-10-20 15:43:04 +03003614 rt->rt6i_gateway = *addr;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003615 rt->rt6i_dst.addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 rt->rt6i_dst.plen = 128;
David Ahernca254492015-10-12 11:47:10 -07003617 tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL;
3618 rt->rt6i_table = fib6_get_table(net, tb_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 return rt;
3621}
3622
Daniel Walterc3968a82011-04-13 21:10:57 +00003623/* remove deleted ip from prefsrc entries */
3624struct arg_dev_net_ip {
3625 struct net_device *dev;
3626 struct net *net;
3627 struct in6_addr *addr;
3628};
3629
3630static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
3631{
3632 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3633 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3634 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3635
David Ahern5e670d82018-04-17 17:33:14 -07003636 if (((void *)rt->fib6_nh.nh_dev == dev || !dev) &&
Daniel Walterc3968a82011-04-13 21:10:57 +00003637 rt != net->ipv6.ip6_null_entry &&
3638 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
Wei Wang60006a42017-10-06 12:05:58 -07003639 spin_lock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00003640 /* remove prefsrc entry */
3641 rt->rt6i_prefsrc.plen = 0;
Wei Wang60006a42017-10-06 12:05:58 -07003642 /* need to update cache as well */
3643 rt6_exceptions_remove_prefsrc(rt);
3644 spin_unlock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00003645 }
3646 return 0;
3647}
3648
3649void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
3650{
3651 struct net *net = dev_net(ifp->idev->dev);
3652 struct arg_dev_net_ip adni = {
3653 .dev = ifp->idev->dev,
3654 .net = net,
3655 .addr = &ifp->addr,
3656 };
Li RongQing0c3584d2013-12-27 16:32:38 +08003657 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00003658}
3659
Duan Jiongbe7a0102014-05-15 15:56:14 +08003660#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
Duan Jiongbe7a0102014-05-15 15:56:14 +08003661
3662/* Remove routers and update dst entries when gateway turn into host. */
3663static int fib6_clean_tohost(struct rt6_info *rt, void *arg)
3664{
3665 struct in6_addr *gateway = (struct in6_addr *)arg;
3666
Wei Wang2b760fc2017-10-06 12:06:03 -07003667 if (((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Ahern5e670d82018-04-17 17:33:14 -07003668 ipv6_addr_equal(gateway, &rt->fib6_nh.nh_gw)) {
Duan Jiongbe7a0102014-05-15 15:56:14 +08003669 return -1;
3670 }
Wei Wangb16cb452017-10-06 12:06:00 -07003671
3672 /* Further clean up cached routes in exception table.
3673 * This is needed because cached route may have a different
3674 * gateway than its 'parent' in the case of an ip redirect.
3675 */
3676 rt6_exceptions_clean_tohost(rt, gateway);
3677
Duan Jiongbe7a0102014-05-15 15:56:14 +08003678 return 0;
3679}
3680
3681void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
3682{
3683 fib6_clean_all(net, fib6_clean_tohost, gateway);
3684}
3685
Ido Schimmel2127d952018-01-07 12:45:03 +02003686struct arg_netdev_event {
3687 const struct net_device *dev;
Ido Schimmel4c981e22018-01-07 12:45:04 +02003688 union {
3689 unsigned int nh_flags;
3690 unsigned long event;
3691 };
Ido Schimmel2127d952018-01-07 12:45:03 +02003692};
3693
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003694static struct rt6_info *rt6_multipath_first_sibling(const struct rt6_info *rt)
3695{
3696 struct rt6_info *iter;
3697 struct fib6_node *fn;
3698
3699 fn = rcu_dereference_protected(rt->rt6i_node,
3700 lockdep_is_held(&rt->rt6i_table->tb6_lock));
3701 iter = rcu_dereference_protected(fn->leaf,
3702 lockdep_is_held(&rt->rt6i_table->tb6_lock));
3703 while (iter) {
3704 if (iter->rt6i_metric == rt->rt6i_metric &&
3705 rt6_qualify_for_ecmp(iter))
3706 return iter;
3707 iter = rcu_dereference_protected(iter->rt6_next,
3708 lockdep_is_held(&rt->rt6i_table->tb6_lock));
3709 }
3710
3711 return NULL;
3712}
3713
3714static bool rt6_is_dead(const struct rt6_info *rt)
3715{
David Ahern5e670d82018-04-17 17:33:14 -07003716 if (rt->fib6_nh.nh_flags & RTNH_F_DEAD ||
3717 (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN &&
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003718 rt->rt6i_idev->cnf.ignore_routes_with_linkdown))
3719 return true;
3720
3721 return false;
3722}
3723
3724static int rt6_multipath_total_weight(const struct rt6_info *rt)
3725{
3726 struct rt6_info *iter;
3727 int total = 0;
3728
3729 if (!rt6_is_dead(rt))
David Ahern5e670d82018-04-17 17:33:14 -07003730 total += rt->fib6_nh.nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003731
3732 list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) {
3733 if (!rt6_is_dead(iter))
David Ahern5e670d82018-04-17 17:33:14 -07003734 total += iter->fib6_nh.nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003735 }
3736
3737 return total;
3738}
3739
3740static void rt6_upper_bound_set(struct rt6_info *rt, int *weight, int total)
3741{
3742 int upper_bound = -1;
3743
3744 if (!rt6_is_dead(rt)) {
David Ahern5e670d82018-04-17 17:33:14 -07003745 *weight += rt->fib6_nh.nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003746 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
3747 total) - 1;
3748 }
David Ahern5e670d82018-04-17 17:33:14 -07003749 atomic_set(&rt->fib6_nh.nh_upper_bound, upper_bound);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003750}
3751
3752static void rt6_multipath_upper_bound_set(struct rt6_info *rt, int total)
3753{
3754 struct rt6_info *iter;
3755 int weight = 0;
3756
3757 rt6_upper_bound_set(rt, &weight, total);
3758
3759 list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings)
3760 rt6_upper_bound_set(iter, &weight, total);
3761}
3762
3763void rt6_multipath_rebalance(struct rt6_info *rt)
3764{
3765 struct rt6_info *first;
3766 int total;
3767
3768 /* In case the entire multipath route was marked for flushing,
3769 * then there is no need to rebalance upon the removal of every
3770 * sibling route.
3771 */
3772 if (!rt->rt6i_nsiblings || rt->should_flush)
3773 return;
3774
3775 /* During lookup routes are evaluated in order, so we need to
3776 * make sure upper bounds are assigned from the first sibling
3777 * onwards.
3778 */
3779 first = rt6_multipath_first_sibling(rt);
3780 if (WARN_ON_ONCE(!first))
3781 return;
3782
3783 total = rt6_multipath_total_weight(first);
3784 rt6_multipath_upper_bound_set(first, total);
3785}
3786
Ido Schimmel2127d952018-01-07 12:45:03 +02003787static int fib6_ifup(struct rt6_info *rt, void *p_arg)
3788{
3789 const struct arg_netdev_event *arg = p_arg;
David Ahern7aef6852018-04-17 17:33:10 -07003790 struct net *net = dev_net(arg->dev);
Ido Schimmel2127d952018-01-07 12:45:03 +02003791
David Ahern5e670d82018-04-17 17:33:14 -07003792 if (rt != net->ipv6.ip6_null_entry && rt->fib6_nh.nh_dev == arg->dev) {
3793 rt->fib6_nh.nh_flags &= ~arg->nh_flags;
David Ahern7aef6852018-04-17 17:33:10 -07003794 fib6_update_sernum_upto_root(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003795 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02003796 }
Ido Schimmel2127d952018-01-07 12:45:03 +02003797
3798 return 0;
3799}
3800
3801void rt6_sync_up(struct net_device *dev, unsigned int nh_flags)
3802{
3803 struct arg_netdev_event arg = {
3804 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02003805 {
3806 .nh_flags = nh_flags,
3807 },
Ido Schimmel2127d952018-01-07 12:45:03 +02003808 };
3809
3810 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
3811 arg.nh_flags |= RTNH_F_LINKDOWN;
3812
3813 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
3814}
3815
Ido Schimmel1de178e2018-01-07 12:45:15 +02003816static bool rt6_multipath_uses_dev(const struct rt6_info *rt,
3817 const struct net_device *dev)
3818{
3819 struct rt6_info *iter;
3820
David Ahern5e670d82018-04-17 17:33:14 -07003821 if (rt->fib6_nh.nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003822 return true;
3823 list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings)
David Ahern5e670d82018-04-17 17:33:14 -07003824 if (iter->fib6_nh.nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003825 return true;
3826
3827 return false;
3828}
3829
3830static void rt6_multipath_flush(struct rt6_info *rt)
3831{
3832 struct rt6_info *iter;
3833
3834 rt->should_flush = 1;
3835 list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings)
3836 iter->should_flush = 1;
3837}
3838
3839static unsigned int rt6_multipath_dead_count(const struct rt6_info *rt,
3840 const struct net_device *down_dev)
3841{
3842 struct rt6_info *iter;
3843 unsigned int dead = 0;
3844
David Ahern5e670d82018-04-17 17:33:14 -07003845 if (rt->fib6_nh.nh_dev == down_dev ||
3846 rt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003847 dead++;
3848 list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings)
David Ahern5e670d82018-04-17 17:33:14 -07003849 if (iter->fib6_nh.nh_dev == down_dev ||
3850 iter->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02003851 dead++;
3852
3853 return dead;
3854}
3855
3856static void rt6_multipath_nh_flags_set(struct rt6_info *rt,
3857 const struct net_device *dev,
3858 unsigned int nh_flags)
3859{
3860 struct rt6_info *iter;
3861
David Ahern5e670d82018-04-17 17:33:14 -07003862 if (rt->fib6_nh.nh_dev == dev)
3863 rt->fib6_nh.nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003864 list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings)
David Ahern5e670d82018-04-17 17:33:14 -07003865 if (iter->fib6_nh.nh_dev == dev)
3866 iter->fib6_nh.nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003867}
3868
David Aherna1a22c12017-01-18 07:40:36 -08003869/* called with write lock held for table with rt */
Ido Schimmel4c981e22018-01-07 12:45:04 +02003870static int fib6_ifdown(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871{
Ido Schimmel4c981e22018-01-07 12:45:04 +02003872 const struct arg_netdev_event *arg = p_arg;
3873 const struct net_device *dev = arg->dev;
David Ahern7aef6852018-04-17 17:33:10 -07003874 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003875
Ido Schimmel1de178e2018-01-07 12:45:15 +02003876 if (rt == net->ipv6.ip6_null_entry)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003877 return 0;
3878
3879 switch (arg->event) {
3880 case NETDEV_UNREGISTER:
David Ahern5e670d82018-04-17 17:33:14 -07003881 return rt->fib6_nh.nh_dev == dev ? -1 : 0;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003882 case NETDEV_DOWN:
Ido Schimmel1de178e2018-01-07 12:45:15 +02003883 if (rt->should_flush)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003884 return -1;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003885 if (!rt->rt6i_nsiblings)
David Ahern5e670d82018-04-17 17:33:14 -07003886 return rt->fib6_nh.nh_dev == dev ? -1 : 0;
Ido Schimmel1de178e2018-01-07 12:45:15 +02003887 if (rt6_multipath_uses_dev(rt, dev)) {
3888 unsigned int count;
3889
3890 count = rt6_multipath_dead_count(rt, dev);
3891 if (rt->rt6i_nsiblings + 1 == count) {
3892 rt6_multipath_flush(rt);
3893 return -1;
3894 }
3895 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
3896 RTNH_F_LINKDOWN);
David Ahern7aef6852018-04-17 17:33:10 -07003897 fib6_update_sernum(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003898 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02003899 }
3900 return -2;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003901 case NETDEV_CHANGE:
David Ahern5e670d82018-04-17 17:33:14 -07003902 if (rt->fib6_nh.nh_dev != dev ||
Ido Schimmel1de178e2018-01-07 12:45:15 +02003903 rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003904 break;
David Ahern5e670d82018-04-17 17:33:14 -07003905 rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02003906 rt6_multipath_rebalance(rt);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003907 break;
Ido Schimmel2b241362018-01-07 12:45:02 +02003908 }
David S. Millerc159d302011-12-26 15:24:36 -05003909
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 return 0;
3911}
3912
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003913void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914{
Ido Schimmel4c981e22018-01-07 12:45:04 +02003915 struct arg_netdev_event arg = {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003916 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02003917 {
3918 .event = event,
3919 },
Daniel Lezcano8ed67782008-03-04 13:48:30 -08003920 };
3921
Ido Schimmel4c981e22018-01-07 12:45:04 +02003922 fib6_clean_all(dev_net(dev), fib6_ifdown, &arg);
3923}
3924
3925void rt6_disable_ip(struct net_device *dev, unsigned long event)
3926{
3927 rt6_sync_down_dev(dev, event);
3928 rt6_uncached_list_flush_dev(dev_net(dev), dev);
3929 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930}
3931
Eric Dumazet95c96172012-04-15 05:58:06 +00003932struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00003934 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935};
3936
3937static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
3938{
3939 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
3940 struct inet6_dev *idev;
3941
3942 /* In IPv6 pmtu discovery is not optional,
3943 so that RTAX_MTU lock cannot disable it.
3944 We still use this lock to block changes
3945 caused by addrconf/ndisc.
3946 */
3947
3948 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05003949 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 return 0;
3951
3952 /* For administrative MTU increase, there is no way to discover
3953 IPv6 PMTU increase, so PMTU increase should be updated here.
3954 Since RFC 1981 doesn't include administrative MTU increase
3955 update PMTU increase is a MUST. (i.e. jumbo frame)
3956 */
David Ahern5e670d82018-04-17 17:33:14 -07003957 if (rt->fib6_nh.nh_dev == arg->dev &&
David Ahernd4ead6b2018-04-17 17:33:16 -07003958 !fib6_metric_locked(rt, RTAX_MTU)) {
3959 u32 mtu = rt->fib6_pmtu;
3960
3961 if (mtu >= arg->mtu ||
3962 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
3963 fib6_metric_set(rt, RTAX_MTU, arg->mtu);
3964
Wei Wangf5bbe7e2017-10-06 12:05:59 -07003965 spin_lock_bh(&rt6_exception_lock);
Stefano Brivioe9fa1492018-03-06 11:10:19 +01003966 rt6_exceptions_update_pmtu(idev, rt, arg->mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07003967 spin_unlock_bh(&rt6_exception_lock);
Simon Arlott566cfd82007-07-26 00:09:55 -07003968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 return 0;
3970}
3971
Eric Dumazet95c96172012-04-15 05:58:06 +00003972void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973{
Thomas Grafc71099a2006-08-04 23:20:06 -07003974 struct rt6_mtu_change_arg arg = {
3975 .dev = dev,
3976 .mtu = mtu,
3977 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
Li RongQing0c3584d2013-12-27 16:32:38 +08003979 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980}
3981
Patrick McHardyef7c79e2007-06-05 12:38:30 -07003982static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07003983 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07003984 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07003985 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07003986 [RTA_PRIORITY] = { .type = NLA_U32 },
3987 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00003988 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01003989 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003990 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
3991 [RTA_ENCAP] = { .type = NLA_NESTED },
Xin Long32bc2012015-12-16 17:50:11 +08003992 [RTA_EXPIRES] = { .type = NLA_U32 },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09003993 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +08003994 [RTA_MARK] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07003995};
3996
3997static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
David Ahern333c4302017-05-21 10:12:04 -06003998 struct fib6_config *cfg,
3999 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000{
Thomas Graf86872cb2006-08-22 00:01:08 -07004001 struct rtmsg *rtm;
4002 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004003 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07004004 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Johannes Bergfceb6432017-04-12 14:34:07 +02004006 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
4007 NULL);
Thomas Graf86872cb2006-08-22 00:01:08 -07004008 if (err < 0)
4009 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
Thomas Graf86872cb2006-08-22 00:01:08 -07004011 err = -EINVAL;
4012 rtm = nlmsg_data(nlh);
4013 memset(cfg, 0, sizeof(*cfg));
4014
4015 cfg->fc_table = rtm->rtm_table;
4016 cfg->fc_dst_len = rtm->rtm_dst_len;
4017 cfg->fc_src_len = rtm->rtm_src_len;
4018 cfg->fc_flags = RTF_UP;
4019 cfg->fc_protocol = rtm->rtm_protocol;
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004020 cfg->fc_type = rtm->rtm_type;
Thomas Graf86872cb2006-08-22 00:01:08 -07004021
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004022 if (rtm->rtm_type == RTN_UNREACHABLE ||
4023 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00004024 rtm->rtm_type == RTN_PROHIBIT ||
4025 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07004026 cfg->fc_flags |= RTF_REJECT;
4027
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00004028 if (rtm->rtm_type == RTN_LOCAL)
4029 cfg->fc_flags |= RTF_LOCAL;
4030
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07004031 if (rtm->rtm_flags & RTM_F_CLONED)
4032 cfg->fc_flags |= RTF_CACHE;
4033
David Ahernfc1e64e2018-01-25 16:55:09 -08004034 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4035
Eric W. Biederman15e47302012-09-07 20:12:54 +00004036 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
Thomas Graf86872cb2006-08-22 00:01:08 -07004037 cfg->fc_nlinfo.nlh = nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004038 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
Thomas Graf86872cb2006-08-22 00:01:08 -07004039
4040 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004041 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07004042 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004044
4045 if (tb[RTA_DST]) {
4046 int plen = (rtm->rtm_dst_len + 7) >> 3;
4047
4048 if (nla_len(tb[RTA_DST]) < plen)
4049 goto errout;
4050
4051 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004053
4054 if (tb[RTA_SRC]) {
4055 int plen = (rtm->rtm_src_len + 7) >> 3;
4056
4057 if (nla_len(tb[RTA_SRC]) < plen)
4058 goto errout;
4059
4060 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004062
Daniel Walterc3968a82011-04-13 21:10:57 +00004063 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02004064 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00004065
Thomas Graf86872cb2006-08-22 00:01:08 -07004066 if (tb[RTA_OIF])
4067 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4068
4069 if (tb[RTA_PRIORITY])
4070 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4071
4072 if (tb[RTA_METRICS]) {
4073 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4074 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004076
4077 if (tb[RTA_TABLE])
4078 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4079
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004080 if (tb[RTA_MULTIPATH]) {
4081 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4082 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
David Ahern9ed59592017-01-17 14:57:36 -08004083
4084 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
David Ahernc255bd62017-05-27 16:19:27 -06004085 cfg->fc_mp_len, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004086 if (err < 0)
4087 goto errout;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004088 }
4089
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004090 if (tb[RTA_PREF]) {
4091 pref = nla_get_u8(tb[RTA_PREF]);
4092 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4093 pref != ICMPV6_ROUTER_PREF_HIGH)
4094 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4095 cfg->fc_flags |= RTF_PREF(pref);
4096 }
4097
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004098 if (tb[RTA_ENCAP])
4099 cfg->fc_encap = tb[RTA_ENCAP];
4100
David Ahern9ed59592017-01-17 14:57:36 -08004101 if (tb[RTA_ENCAP_TYPE]) {
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004102 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
4103
David Ahernc255bd62017-05-27 16:19:27 -06004104 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004105 if (err < 0)
4106 goto errout;
4107 }
4108
Xin Long32bc2012015-12-16 17:50:11 +08004109 if (tb[RTA_EXPIRES]) {
4110 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
4111
4112 if (addrconf_finite_timeout(timeout)) {
4113 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
4114 cfg->fc_flags |= RTF_EXPIRES;
4115 }
4116 }
4117
Thomas Graf86872cb2006-08-22 00:01:08 -07004118 err = 0;
4119errout:
4120 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121}
4122
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004123struct rt6_nh {
4124 struct rt6_info *rt6_info;
4125 struct fib6_config r_cfg;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004126 struct list_head next;
4127};
4128
4129static void ip6_print_replace_route_err(struct list_head *rt6_nh_list)
4130{
4131 struct rt6_nh *nh;
4132
4133 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern7d4d5062017-02-02 12:37:12 -08004134 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 -07004135 &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway,
4136 nh->r_cfg.fc_ifindex);
4137 }
4138}
4139
David Ahernd4ead6b2018-04-17 17:33:16 -07004140static int ip6_route_info_append(struct net *net,
4141 struct list_head *rt6_nh_list,
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004142 struct rt6_info *rt, struct fib6_config *r_cfg)
4143{
4144 struct rt6_nh *nh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004145 int err = -EEXIST;
4146
4147 list_for_each_entry(nh, rt6_nh_list, next) {
4148 /* check if rt6_info already exists */
David Ahernf06b7542017-07-05 14:41:46 -06004149 if (rt6_duplicate_nexthop(nh->rt6_info, rt))
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004150 return err;
4151 }
4152
4153 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
4154 if (!nh)
4155 return -ENOMEM;
4156 nh->rt6_info = rt;
David Ahernd4ead6b2018-04-17 17:33:16 -07004157 err = ip6_convert_metrics(net, rt, r_cfg);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004158 if (err) {
4159 kfree(nh);
4160 return err;
4161 }
4162 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
4163 list_add_tail(&nh->next, rt6_nh_list);
4164
4165 return 0;
4166}
4167
David Ahern3b1137f2017-02-02 12:37:10 -08004168static void ip6_route_mpath_notify(struct rt6_info *rt,
4169 struct rt6_info *rt_last,
4170 struct nl_info *info,
4171 __u16 nlflags)
4172{
4173 /* if this is an APPEND route, then rt points to the first route
4174 * inserted and rt_last points to last route inserted. Userspace
4175 * wants a consistent dump of the route which starts at the first
4176 * nexthop. Since sibling routes are always added at the end of
4177 * the list, find the first sibling of the last route appended
4178 */
4179 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) {
4180 rt = list_first_entry(&rt_last->rt6i_siblings,
4181 struct rt6_info,
4182 rt6i_siblings);
4183 }
4184
4185 if (rt)
4186 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
4187}
4188
David Ahern333c4302017-05-21 10:12:04 -06004189static int ip6_route_multipath_add(struct fib6_config *cfg,
4190 struct netlink_ext_ack *extack)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004191{
David Ahern3b1137f2017-02-02 12:37:10 -08004192 struct rt6_info *rt_notif = NULL, *rt_last = NULL;
4193 struct nl_info *info = &cfg->fc_nlinfo;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004194 struct fib6_config r_cfg;
4195 struct rtnexthop *rtnh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004196 struct rt6_info *rt;
4197 struct rt6_nh *err_nh;
4198 struct rt6_nh *nh, *nh_safe;
David Ahern3b1137f2017-02-02 12:37:10 -08004199 __u16 nlflags;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004200 int remaining;
4201 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004202 int err = 1;
4203 int nhn = 0;
4204 int replace = (cfg->fc_nlinfo.nlh &&
4205 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
4206 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004207
David Ahern3b1137f2017-02-02 12:37:10 -08004208 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
4209 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
4210 nlflags |= NLM_F_APPEND;
4211
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02004212 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004213 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004214
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004215 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
4216 * rt6_info structs per nexthop
4217 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004218 while (rtnh_ok(rtnh, remaining)) {
4219 memcpy(&r_cfg, cfg, sizeof(*cfg));
4220 if (rtnh->rtnh_ifindex)
4221 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
4222
4223 attrlen = rtnh_attrlen(rtnh);
4224 if (attrlen > 0) {
4225 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4226
4227 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
4228 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004229 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004230 r_cfg.fc_flags |= RTF_GATEWAY;
4231 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004232 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
4233 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
4234 if (nla)
4235 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004236 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004237
David Ahern68e2ffd2018-03-20 10:06:59 -07004238 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
David Ahern333c4302017-05-21 10:12:04 -06004239 rt = ip6_route_info_create(&r_cfg, extack);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07004240 if (IS_ERR(rt)) {
4241 err = PTR_ERR(rt);
4242 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004243 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07004244 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004245
David Ahern5e670d82018-04-17 17:33:14 -07004246 rt->fib6_nh.nh_weight = rtnh->rtnh_hops + 1;
Ido Schimmel398958a2018-01-09 16:40:28 +02004247
David Ahernd4ead6b2018-04-17 17:33:16 -07004248 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4249 rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004250 if (err) {
Wei Wang587fea72017-06-17 10:42:36 -07004251 dst_release_immediate(&rt->dst);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004252 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004253 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004254
4255 rtnh = rtnh_next(rtnh, &remaining);
4256 }
4257
David Ahern3b1137f2017-02-02 12:37:10 -08004258 /* for add and replace send one notification with all nexthops.
4259 * Skip the notification in fib6_add_rt2node and send one with
4260 * the full route when done
4261 */
4262 info->skip_notify = 1;
4263
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004264 err_nh = NULL;
4265 list_for_each_entry(nh, &rt6_nh_list, next) {
David Ahern3b1137f2017-02-02 12:37:10 -08004266 rt_last = nh->rt6_info;
David Ahernd4ead6b2018-04-17 17:33:16 -07004267 err = __ip6_ins_rt(nh->rt6_info, info, extack);
David Ahern3b1137f2017-02-02 12:37:10 -08004268 /* save reference to first route for notification */
4269 if (!rt_notif && !err)
4270 rt_notif = nh->rt6_info;
4271
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004272 /* nh->rt6_info is used or freed at this point, reset to NULL*/
4273 nh->rt6_info = NULL;
4274 if (err) {
4275 if (replace && nhn)
4276 ip6_print_replace_route_err(&rt6_nh_list);
4277 err_nh = nh;
4278 goto add_errout;
4279 }
4280
Nicolas Dichtel1a724182012-11-01 22:58:22 +00004281 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02004282 * these flags after the first nexthop: if there is a collision,
4283 * we have already failed to add the first nexthop:
4284 * fib6_add_rt2node() has rejected it; when replacing, old
4285 * nexthops have been replaced by first new, the rest should
4286 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00004287 */
Michal Kubeček27596472015-05-18 20:54:00 +02004288 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
4289 NLM_F_REPLACE);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004290 nhn++;
4291 }
4292
David Ahern3b1137f2017-02-02 12:37:10 -08004293 /* success ... tell user about new route */
4294 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004295 goto cleanup;
4296
4297add_errout:
David Ahern3b1137f2017-02-02 12:37:10 -08004298 /* send notification for routes that were added so that
4299 * the delete notifications sent by ip6_route_del are
4300 * coherent
4301 */
4302 if (rt_notif)
4303 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
4304
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004305 /* Delete routes that were already added */
4306 list_for_each_entry(nh, &rt6_nh_list, next) {
4307 if (err_nh == nh)
4308 break;
David Ahern333c4302017-05-21 10:12:04 -06004309 ip6_route_del(&nh->r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004310 }
4311
4312cleanup:
4313 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
Wei Wang587fea72017-06-17 10:42:36 -07004314 if (nh->rt6_info)
4315 dst_release_immediate(&nh->rt6_info->dst);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004316 list_del(&nh->next);
4317 kfree(nh);
4318 }
4319
4320 return err;
4321}
4322
David Ahern333c4302017-05-21 10:12:04 -06004323static int ip6_route_multipath_del(struct fib6_config *cfg,
4324 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004325{
4326 struct fib6_config r_cfg;
4327 struct rtnexthop *rtnh;
4328 int remaining;
4329 int attrlen;
4330 int err = 1, last_err = 0;
4331
4332 remaining = cfg->fc_mp_len;
4333 rtnh = (struct rtnexthop *)cfg->fc_mp;
4334
4335 /* Parse a Multipath Entry */
4336 while (rtnh_ok(rtnh, remaining)) {
4337 memcpy(&r_cfg, cfg, sizeof(*cfg));
4338 if (rtnh->rtnh_ifindex)
4339 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
4340
4341 attrlen = rtnh_attrlen(rtnh);
4342 if (attrlen > 0) {
4343 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4344
4345 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
4346 if (nla) {
4347 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
4348 r_cfg.fc_flags |= RTF_GATEWAY;
4349 }
4350 }
David Ahern333c4302017-05-21 10:12:04 -06004351 err = ip6_route_del(&r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004352 if (err)
4353 last_err = err;
4354
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004355 rtnh = rtnh_next(rtnh, &remaining);
4356 }
4357
4358 return last_err;
4359}
4360
David Ahernc21ef3e2017-04-16 09:48:24 -07004361static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4362 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363{
Thomas Graf86872cb2006-08-22 00:01:08 -07004364 struct fib6_config cfg;
4365 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
David Ahern333c4302017-05-21 10:12:04 -06004367 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004368 if (err < 0)
4369 return err;
4370
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004371 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06004372 return ip6_route_multipath_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08004373 else {
4374 cfg.fc_delete_all_nh = 1;
David Ahern333c4302017-05-21 10:12:04 -06004375 return ip6_route_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08004376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377}
4378
David Ahernc21ef3e2017-04-16 09:48:24 -07004379static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4380 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
Thomas Graf86872cb2006-08-22 00:01:08 -07004382 struct fib6_config cfg;
4383 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
David Ahern333c4302017-05-21 10:12:04 -06004385 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004386 if (err < 0)
4387 return err;
4388
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004389 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06004390 return ip6_route_multipath_add(&cfg, extack);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004391 else
David Ahern333c4302017-05-21 10:12:04 -06004392 return ip6_route_add(&cfg, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393}
4394
David Ahernbeb1afac52017-02-02 12:37:09 -08004395static size_t rt6_nlmsg_size(struct rt6_info *rt)
Thomas Graf339bf982006-11-10 14:10:15 -08004396{
David Ahernbeb1afac52017-02-02 12:37:09 -08004397 int nexthop_len = 0;
4398
4399 if (rt->rt6i_nsiblings) {
4400 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
4401 + NLA_ALIGN(sizeof(struct rtnexthop))
4402 + nla_total_size(16) /* RTA_GATEWAY */
David Ahern5e670d82018-04-17 17:33:14 -07004403 + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate);
David Ahernbeb1afac52017-02-02 12:37:09 -08004404
4405 nexthop_len *= rt->rt6i_nsiblings;
4406 }
4407
Thomas Graf339bf982006-11-10 14:10:15 -08004408 return NLMSG_ALIGN(sizeof(struct rtmsg))
4409 + nla_total_size(16) /* RTA_SRC */
4410 + nla_total_size(16) /* RTA_DST */
4411 + nla_total_size(16) /* RTA_GATEWAY */
4412 + nla_total_size(16) /* RTA_PREFSRC */
4413 + nla_total_size(4) /* RTA_TABLE */
4414 + nla_total_size(4) /* RTA_IIF */
4415 + nla_total_size(4) /* RTA_OIF */
4416 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08004417 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01004418 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004419 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004420 + nla_total_size(1) /* RTA_PREF */
David Ahern5e670d82018-04-17 17:33:14 -07004421 + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate)
David Ahernbeb1afac52017-02-02 12:37:09 -08004422 + nexthop_len;
4423}
4424
4425static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt,
David Ahern5be083c2017-03-06 15:57:31 -08004426 unsigned int *flags, bool skip_oif)
David Ahernbeb1afac52017-02-02 12:37:09 -08004427{
David Ahern5e670d82018-04-17 17:33:14 -07004428 if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
Ido Schimmelf9d882e2018-01-07 12:45:10 +02004429 *flags |= RTNH_F_DEAD;
4430
David Ahern5e670d82018-04-17 17:33:14 -07004431 if (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN) {
David Ahernbeb1afac52017-02-02 12:37:09 -08004432 *flags |= RTNH_F_LINKDOWN;
4433 if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown)
4434 *flags |= RTNH_F_DEAD;
4435 }
4436
4437 if (rt->rt6i_flags & RTF_GATEWAY) {
David Ahern5e670d82018-04-17 17:33:14 -07004438 if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->fib6_nh.nh_gw) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004439 goto nla_put_failure;
4440 }
4441
David Ahern5e670d82018-04-17 17:33:14 -07004442 *flags |= (rt->fib6_nh.nh_flags & RTNH_F_ONLINK);
4443 if (rt->fib6_nh.nh_flags & RTNH_F_OFFLOAD)
Ido Schimmel61e4d012017-08-03 13:28:20 +02004444 *flags |= RTNH_F_OFFLOAD;
4445
David Ahern5be083c2017-03-06 15:57:31 -08004446 /* not needed for multipath encoding b/c it has a rtnexthop struct */
David Ahern5e670d82018-04-17 17:33:14 -07004447 if (!skip_oif && rt->fib6_nh.nh_dev &&
4448 nla_put_u32(skb, RTA_OIF, rt->fib6_nh.nh_dev->ifindex))
David Ahernbeb1afac52017-02-02 12:37:09 -08004449 goto nla_put_failure;
4450
David Ahern5e670d82018-04-17 17:33:14 -07004451 if (rt->fib6_nh.nh_lwtstate &&
4452 lwtunnel_fill_encap(skb, rt->fib6_nh.nh_lwtstate) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004453 goto nla_put_failure;
4454
4455 return 0;
4456
4457nla_put_failure:
4458 return -EMSGSIZE;
4459}
4460
David Ahern5be083c2017-03-06 15:57:31 -08004461/* add multipath next hop */
David Ahernbeb1afac52017-02-02 12:37:09 -08004462static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt)
4463{
David Ahern5e670d82018-04-17 17:33:14 -07004464 const struct net_device *dev = rt->fib6_nh.nh_dev;
David Ahernbeb1afac52017-02-02 12:37:09 -08004465 struct rtnexthop *rtnh;
4466 unsigned int flags = 0;
4467
4468 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
4469 if (!rtnh)
4470 goto nla_put_failure;
4471
David Ahern5e670d82018-04-17 17:33:14 -07004472 rtnh->rtnh_hops = rt->fib6_nh.nh_weight - 1;
4473 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
David Ahernbeb1afac52017-02-02 12:37:09 -08004474
David Ahern5be083c2017-03-06 15:57:31 -08004475 if (rt6_nexthop_info(skb, rt, &flags, true) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004476 goto nla_put_failure;
4477
4478 rtnh->rtnh_flags = flags;
4479
4480 /* length of rtnetlink header + attributes */
4481 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
4482
4483 return 0;
4484
4485nla_put_failure:
4486 return -EMSGSIZE;
Thomas Graf339bf982006-11-10 14:10:15 -08004487}
4488
David Ahernd4ead6b2018-04-17 17:33:16 -07004489static int rt6_fill_node(struct net *net, struct sk_buff *skb,
4490 struct rt6_info *rt, struct dst_entry *dst,
4491 struct in6_addr *dest, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004492 int iif, int type, u32 portid, u32 seq,
David Ahernf8cfe2c2017-01-17 15:51:08 -08004493 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
4495 struct rtmsg *rtm;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004496 struct nlmsghdr *nlh;
David Ahernd4ead6b2018-04-17 17:33:16 -07004497 long expires = 0;
4498 u32 *pmetrics;
Patrick McHardy9e762a42006-08-10 23:09:48 -07004499 u32 table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
Eric W. Biederman15e47302012-09-07 20:12:54 +00004501 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05004502 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004503 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004504
4505 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 rtm->rtm_family = AF_INET6;
4507 rtm->rtm_dst_len = rt->rt6i_dst.plen;
4508 rtm->rtm_src_len = rt->rt6i_src.plen;
4509 rtm->rtm_tos = 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07004510 if (rt->rt6i_table)
Patrick McHardy9e762a42006-08-10 23:09:48 -07004511 table = rt->rt6i_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07004512 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07004513 table = RT6_TABLE_UNSPEC;
4514 rtm->rtm_table = table;
David S. Millerc78679e2012-04-01 20:27:33 -04004515 if (nla_put_u32(skb, RTA_TABLE, table))
4516 goto nla_put_failure;
David Aherne8478e82018-04-17 17:33:13 -07004517
4518 rtm->rtm_type = rt->fib6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 rtm->rtm_flags = 0;
4520 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
4521 rtm->rtm_protocol = rt->rt6i_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
David S. Miller38308472011-12-03 18:02:47 -05004523 if (rt->rt6i_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 rtm->rtm_flags |= RTM_F_CLONED;
4525
David Ahernd4ead6b2018-04-17 17:33:16 -07004526 if (dest) {
4527 if (nla_put_in6_addr(skb, RTA_DST, dest))
David S. Millerc78679e2012-04-01 20:27:33 -04004528 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004529 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 } else if (rtm->rtm_dst_len)
Jiri Benc930345e2015-03-29 16:59:25 +02004531 if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04004532 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533#ifdef CONFIG_IPV6_SUBTREES
4534 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02004535 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04004536 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004537 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04004538 } else if (rtm->rtm_src_len &&
Jiri Benc930345e2015-03-29 16:59:25 +02004539 nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr))
David S. Millerc78679e2012-04-01 20:27:33 -04004540 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004542 if (iif) {
4543#ifdef CONFIG_IPV6_MROUTE
4544 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
David Ahernfd61c6b2017-01-17 15:51:07 -08004545 int err = ip6mr_get_route(net, skb, rtm, portid);
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02004546
David Ahernfd61c6b2017-01-17 15:51:07 -08004547 if (err == 0)
4548 return 0;
4549 if (err < 0)
4550 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004551 } else
4552#endif
David S. Millerc78679e2012-04-01 20:27:33 -04004553 if (nla_put_u32(skb, RTA_IIF, iif))
4554 goto nla_put_failure;
David Ahernd4ead6b2018-04-17 17:33:16 -07004555 } else if (dest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 struct in6_addr saddr_buf;
David Ahernd4ead6b2018-04-17 17:33:16 -07004557 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02004558 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04004559 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07004561
Daniel Walterc3968a82011-04-13 21:10:57 +00004562 if (rt->rt6i_prefsrc.plen) {
4563 struct in6_addr saddr_buf;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004564 saddr_buf = rt->rt6i_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02004565 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04004566 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00004567 }
4568
David Ahernd4ead6b2018-04-17 17:33:16 -07004569 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
4570 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07004571 goto nla_put_failure;
4572
David S. Millerc78679e2012-04-01 20:27:33 -04004573 if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
4574 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00004575
David Ahernbeb1afac52017-02-02 12:37:09 -08004576 /* For multipath routes, walk the siblings list and add
4577 * each as a nexthop within RTA_MULTIPATH.
4578 */
4579 if (rt->rt6i_nsiblings) {
4580 struct rt6_info *sibling, *next_sibling;
4581 struct nlattr *mp;
4582
4583 mp = nla_nest_start(skb, RTA_MULTIPATH);
4584 if (!mp)
4585 goto nla_put_failure;
4586
4587 if (rt6_add_nexthop(skb, rt) < 0)
4588 goto nla_put_failure;
4589
4590 list_for_each_entry_safe(sibling, next_sibling,
4591 &rt->rt6i_siblings, rt6i_siblings) {
4592 if (rt6_add_nexthop(skb, sibling) < 0)
4593 goto nla_put_failure;
4594 }
4595
4596 nla_nest_end(skb, mp);
4597 } else {
David Ahern5be083c2017-03-06 15:57:31 -08004598 if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08004599 goto nla_put_failure;
4600 }
4601
David Ahern14895682018-04-17 17:33:17 -07004602 if (rt->rt6i_flags & RTF_EXPIRES) {
4603 expires = dst ? dst->expires : rt->expires;
4604 expires -= jiffies;
4605 }
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07004606
David Ahernd4ead6b2018-04-17 17:33:16 -07004607 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08004608 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004610 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags)))
4611 goto nla_put_failure;
4612
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004613
Johannes Berg053c0952015-01-16 22:09:00 +01004614 nlmsg_end(skb, nlh);
4615 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07004616
4617nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004618 nlmsg_cancel(skb, nlh);
4619 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620}
4621
Patrick McHardy1b43af52006-08-10 23:11:17 -07004622int rt6_dump_route(struct rt6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623{
4624 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
David Ahern1f17e2f2017-01-26 13:54:08 -08004625 struct net *net = arg->net;
4626
4627 if (rt == net->ipv6.ip6_null_entry)
4628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
Thomas Graf2d7202b2006-08-22 00:01:27 -07004630 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
4631 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
David Ahernf8cfe2c2017-01-17 15:51:08 -08004632
4633 /* user wants prefix routes only */
4634 if (rtm->rtm_flags & RTM_F_PREFIX &&
4635 !(rt->rt6i_flags & RTF_PREFIX_RT)) {
4636 /* success since this is not a prefix route */
4637 return 1;
4638 }
4639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
David Ahernd4ead6b2018-04-17 17:33:16 -07004641 return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
4642 RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
4643 arg->cb->nlh->nlmsg_seq, NLM_F_MULTI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644}
4645
David Ahernc21ef3e2017-04-16 09:48:24 -07004646static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4647 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004649 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07004650 struct nlattr *tb[RTA_MAX+1];
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004651 int err, iif = 0, oif = 0;
4652 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07004654 struct sk_buff *skb;
4655 struct rtmsg *rtm;
David S. Miller4c9483b2011-03-12 16:22:43 -05004656 struct flowi6 fl6;
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004657 bool fibmatch;
Thomas Grafab364a62006-08-22 00:01:47 -07004658
Johannes Bergfceb6432017-04-12 14:34:07 +02004659 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07004660 extack);
Thomas Grafab364a62006-08-22 00:01:47 -07004661 if (err < 0)
4662 goto errout;
4663
4664 err = -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -05004665 memset(&fl6, 0, sizeof(fl6));
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02004666 rtm = nlmsg_data(nlh);
4667 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004668 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
Thomas Grafab364a62006-08-22 00:01:47 -07004669
4670 if (tb[RTA_SRC]) {
4671 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4672 goto errout;
4673
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004674 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07004675 }
4676
4677 if (tb[RTA_DST]) {
4678 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4679 goto errout;
4680
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004681 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07004682 }
4683
4684 if (tb[RTA_IIF])
4685 iif = nla_get_u32(tb[RTA_IIF]);
4686
4687 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004688 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07004689
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07004690 if (tb[RTA_MARK])
4691 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
4692
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004693 if (tb[RTA_UID])
4694 fl6.flowi6_uid = make_kuid(current_user_ns(),
4695 nla_get_u32(tb[RTA_UID]));
4696 else
4697 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
4698
Thomas Grafab364a62006-08-22 00:01:47 -07004699 if (iif) {
4700 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004701 int flags = 0;
4702
Florian Westphal121622d2017-08-15 16:34:42 +02004703 rcu_read_lock();
4704
4705 dev = dev_get_by_index_rcu(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07004706 if (!dev) {
Florian Westphal121622d2017-08-15 16:34:42 +02004707 rcu_read_unlock();
Thomas Grafab364a62006-08-22 00:01:47 -07004708 err = -ENODEV;
4709 goto errout;
4710 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004711
4712 fl6.flowi6_iif = iif;
4713
4714 if (!ipv6_addr_any(&fl6.saddr))
4715 flags |= RT6_LOOKUP_F_HAS_SADDR;
4716
David Ahernb75cc8f2018-03-02 08:32:17 -08004717 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
Florian Westphal121622d2017-08-15 16:34:42 +02004718
4719 rcu_read_unlock();
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00004720 } else {
4721 fl6.flowi6_oif = oif;
4722
Ido Schimmel58acfd72017-12-20 12:28:25 +02004723 dst = ip6_route_output(net, NULL, &fl6);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004724 }
4725
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004726
4727 rt = container_of(dst, struct rt6_info, dst);
4728 if (rt->dst.error) {
4729 err = rt->dst.error;
4730 ip6_rt_put(rt);
4731 goto errout;
Thomas Grafab364a62006-08-22 00:01:47 -07004732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
WANG Cong9d6acb32017-03-01 20:48:39 -08004734 if (rt == net->ipv6.ip6_null_entry) {
4735 err = rt->dst.error;
4736 ip6_rt_put(rt);
4737 goto errout;
4738 }
4739
David S. Millerfba961a2017-12-22 11:16:31 -05004740 if (fibmatch && rt->from) {
4741 struct rt6_info *ort = rt->from;
Ido Schimmel58acfd72017-12-20 12:28:25 +02004742
4743 dst_hold(&ort->dst);
4744 ip6_rt_put(rt);
4745 rt = ort;
4746 }
4747
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05004749 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00004750 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07004751 err = -ENOBUFS;
4752 goto errout;
4753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
Changli Gaod8d1f302010-06-10 23:31:35 -07004755 skb_dst_set(skb, &rt->dst);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004756 if (fibmatch)
David Ahernd4ead6b2018-04-17 17:33:16 -07004757 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, iif,
Roopa Prabhu18c3a612017-05-25 10:42:40 -07004758 RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
4759 nlh->nlmsg_seq, 0);
4760 else
David Ahernd4ead6b2018-04-17 17:33:16 -07004761 err = rt6_fill_node(net, skb, rt, dst, &fl6.daddr, &fl6.saddr,
4762 iif, RTM_NEWROUTE,
4763 NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
4764 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07004766 kfree_skb(skb);
4767 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 }
4769
Eric W. Biederman15e47302012-09-07 20:12:54 +00004770 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07004771errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773}
4774
Roopa Prabhu37a1d362015-09-13 10:18:33 -07004775void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info,
4776 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777{
4778 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08004779 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08004780 u32 seq;
4781 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08004783 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05004784 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07004785
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004786 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05004787 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07004788 goto errout;
4789
David Ahernd4ead6b2018-04-17 17:33:16 -07004790 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
4791 event, info->portid, seq, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08004792 if (err < 0) {
4793 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
4794 WARN_ON(err == -EMSGSIZE);
4795 kfree_skb(skb);
4796 goto errout;
4797 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004798 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004799 info->nlh, gfp_any());
4800 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07004801errout:
4802 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08004803 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804}
4805
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004806static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00004807 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004808{
Jiri Pirko351638e2013-05-28 01:30:21 +00004809 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004810 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004811
WANG Cong242d3a42017-05-08 10:12:13 -07004812 if (!(dev->flags & IFF_LOOPBACK))
4813 return NOTIFY_OK;
4814
4815 if (event == NETDEV_REGISTER) {
Changli Gaod8d1f302010-06-10 23:31:35 -07004816 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004817 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
4818#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07004819 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004820 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07004821 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004822 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
4823#endif
WANG Cong76da0702017-06-20 11:42:27 -07004824 } else if (event == NETDEV_UNREGISTER &&
4825 dev->reg_state != NETREG_UNREGISTERED) {
4826 /* NETDEV_UNREGISTER could be fired for multiple times by
4827 * netdev_wait_allrefs(). Make sure we only call this once.
4828 */
Eric Dumazet12d94a82017-08-15 04:09:51 -07004829 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07004830#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric Dumazet12d94a82017-08-15 04:09:51 -07004831 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
4832 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07004833#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004834 }
4835
4836 return NOTIFY_OK;
4837}
4838
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839/*
4840 * /proc
4841 */
4842
4843#ifdef CONFIG_PROC_FS
4844
Alexey Dobriyan33120b32007-11-06 05:27:11 -08004845static const struct file_operations ipv6_route_proc_fops = {
Alexey Dobriyan33120b32007-11-06 05:27:11 -08004846 .open = ipv6_route_open,
4847 .read = seq_read,
4848 .llseek = seq_lseek,
Hannes Frederic Sowa8d2ca1d2013-09-21 16:55:59 +02004849 .release = seq_release_net,
Alexey Dobriyan33120b32007-11-06 05:27:11 -08004850};
4851
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852static int rt6_stats_seq_show(struct seq_file *seq, void *v)
4853{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004854 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004856 net->ipv6.rt6_stats->fib_nodes,
4857 net->ipv6.rt6_stats->fib_route_nodes,
Wei Wang81eb8442017-10-06 12:06:11 -07004858 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004859 net->ipv6.rt6_stats->fib_rt_entries,
4860 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00004861 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004862 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
4864 return 0;
4865}
4866
4867static int rt6_stats_seq_open(struct inode *inode, struct file *file)
4868{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07004869 return single_open_net(inode, file, rt6_stats_seq_show);
Daniel Lezcano69ddb802008-03-04 13:46:23 -08004870}
4871
Arjan van de Ven9a321442007-02-12 00:55:35 -08004872static const struct file_operations rt6_stats_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 .open = rt6_stats_seq_open,
4874 .read = seq_read,
4875 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07004876 .release = single_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877};
4878#endif /* CONFIG_PROC_FS */
4879
4880#ifdef CONFIG_SYSCTL
4881
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882static
Joe Perchesfe2c6332013-06-11 23:04:25 -07004883int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 void __user *buffer, size_t *lenp, loff_t *ppos)
4885{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00004886 struct net *net;
4887 int delay;
4888 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00004890
4891 net = (struct net *)ctl->extra1;
4892 delay = net->ipv6.sysctl.flush_delay;
4893 proc_dointvec(ctl, write, buffer, lenp, ppos);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02004894 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00004895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896}
4897
Joe Perchesfe2c6332013-06-11 23:04:25 -07004898struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004899 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08004901 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07004903 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004904 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 },
4906 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08004908 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 .maxlen = sizeof(int),
4910 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004911 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 },
4913 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08004915 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 .maxlen = sizeof(int),
4917 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004918 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 },
4920 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08004922 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 .maxlen = sizeof(int),
4924 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004925 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 },
4927 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08004929 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 .maxlen = sizeof(int),
4931 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004932 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 },
4934 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08004936 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 .maxlen = sizeof(int),
4938 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004939 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 },
4941 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08004943 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 .maxlen = sizeof(int),
4945 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07004946 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 },
4948 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08004950 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 .maxlen = sizeof(int),
4952 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004953 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 },
4955 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08004957 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 .maxlen = sizeof(int),
4959 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07004960 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 },
4962 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08004964 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 .maxlen = sizeof(int),
4966 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08004967 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004969 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970};
4971
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004972struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08004973{
4974 struct ctl_table *table;
4975
4976 table = kmemdup(ipv6_route_table_template,
4977 sizeof(ipv6_route_table_template),
4978 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09004979
4980 if (table) {
4981 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00004982 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00004983 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09004984 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
4985 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
4986 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
4987 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
4988 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
4989 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
4990 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08004991 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
Eric W. Biederman464dc802012-11-16 03:02:59 +00004992
4993 /* Don't export sysctls to unprivileged users */
4994 if (net->user_ns != &init_user_ns)
4995 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09004996 }
4997
Daniel Lezcano760f2d02008-01-10 02:53:43 -08004998 return table;
4999}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000#endif
5001
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005002static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005003{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07005004 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005005
Alexey Dobriyan86393e52009-08-29 01:34:49 +00005006 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
5007 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005008
Eric Dumazetfc66f952010-10-08 06:37:34 +00005009 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5010 goto out_ip6_dst_ops;
5011
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005012 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
5013 sizeof(*net->ipv6.ip6_null_entry),
5014 GFP_KERNEL);
5015 if (!net->ipv6.ip6_null_entry)
Eric Dumazetfc66f952010-10-08 06:37:34 +00005016 goto out_ip6_dst_entries;
Changli Gaod8d1f302010-06-10 23:31:35 -07005017 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005018 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
5019 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005020
5021#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Vincent Bernatfeca7d82017-08-08 20:23:49 +02005022 net->ipv6.fib6_has_custom_rules = false;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005023 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
5024 sizeof(*net->ipv6.ip6_prohibit_entry),
5025 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005026 if (!net->ipv6.ip6_prohibit_entry)
5027 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005028 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005029 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
5030 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005031
5032 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
5033 sizeof(*net->ipv6.ip6_blk_hole_entry),
5034 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005035 if (!net->ipv6.ip6_blk_hole_entry)
5036 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07005037 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08005038 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
5039 ip6_template_metrics, true);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005040#endif
5041
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07005042 net->ipv6.sysctl.flush_delay = 0;
5043 net->ipv6.sysctl.ip6_rt_max_size = 4096;
5044 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5045 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5046 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5047 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5048 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5049 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
5050
Benjamin Thery6891a342008-03-04 13:49:47 -08005051 net->ipv6.ip6_rt_gc_expire = 30*HZ;
5052
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005053 ret = 0;
5054out:
5055 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005056
Peter Zijlstra68fffc62008-10-07 14:12:10 -07005057#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5058out_ip6_prohibit_entry:
5059 kfree(net->ipv6.ip6_prohibit_entry);
5060out_ip6_null_entry:
5061 kfree(net->ipv6.ip6_null_entry);
5062#endif
Eric Dumazetfc66f952010-10-08 06:37:34 +00005063out_ip6_dst_entries:
5064 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005065out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005066 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005067}
5068
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005069static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005070{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005071 kfree(net->ipv6.ip6_null_entry);
5072#ifdef CONFIG_IPV6_MULTIPLE_TABLES
5073 kfree(net->ipv6.ip6_prohibit_entry);
5074 kfree(net->ipv6.ip6_blk_hole_entry);
5075#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00005076 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005077}
5078
Thomas Grafd1896342012-06-18 12:08:33 +00005079static int __net_init ip6_route_net_init_late(struct net *net)
5080{
5081#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00005082 proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
Joe Perchesd6444062018-03-23 15:54:38 -07005083 proc_create("rt6_stats", 0444, net->proc_net, &rt6_stats_seq_fops);
Thomas Grafd1896342012-06-18 12:08:33 +00005084#endif
5085 return 0;
5086}
5087
5088static void __net_exit ip6_route_net_exit_late(struct net *net)
5089{
5090#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00005091 remove_proc_entry("ipv6_route", net->proc_net);
5092 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00005093#endif
5094}
5095
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005096static struct pernet_operations ip6_route_net_ops = {
5097 .init = ip6_route_net_init,
5098 .exit = ip6_route_net_exit,
5099};
5100
David S. Millerc3426b42012-06-09 16:27:05 -07005101static int __net_init ipv6_inetpeer_init(struct net *net)
5102{
5103 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5104
5105 if (!bp)
5106 return -ENOMEM;
5107 inet_peer_base_init(bp);
5108 net->ipv6.peers = bp;
5109 return 0;
5110}
5111
5112static void __net_exit ipv6_inetpeer_exit(struct net *net)
5113{
5114 struct inet_peer_base *bp = net->ipv6.peers;
5115
5116 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07005117 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07005118 kfree(bp);
5119}
5120
David S. Miller2b823f72012-06-09 19:00:16 -07005121static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07005122 .init = ipv6_inetpeer_init,
5123 .exit = ipv6_inetpeer_exit,
5124};
5125
Thomas Grafd1896342012-06-18 12:08:33 +00005126static struct pernet_operations ip6_route_net_late_ops = {
5127 .init = ip6_route_net_init_late,
5128 .exit = ip6_route_net_exit_late,
5129};
5130
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005131static struct notifier_block ip6_route_dev_notifier = {
5132 .notifier_call = ip6_route_dev_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07005133 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005134};
5135
WANG Cong2f460932017-05-03 22:07:31 -07005136void __init ip6_route_init_special_entries(void)
5137{
5138 /* Registering of the loopback is done before this portion of code,
5139 * the loopback reference in rt6_info will not be taken, do it
5140 * manually for init_net */
5141 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
5142 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5143 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5144 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
5145 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5146 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
5147 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
5148 #endif
5149}
5150
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005151int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005153 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07005154 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005155
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08005156 ret = -ENOMEM;
5157 ip6_dst_ops_template.kmem_cachep =
5158 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
5159 SLAB_HWCACHE_ALIGN, NULL);
5160 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08005161 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07005162
Eric Dumazetfc66f952010-10-08 06:37:34 +00005163 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005164 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08005165 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08005166
David S. Millerc3426b42012-06-09 16:27:05 -07005167 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
5168 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07005169 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00005170
David S. Miller7e52b332012-06-15 15:51:55 -07005171 ret = register_pernet_subsys(&ip6_route_net_ops);
5172 if (ret)
5173 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07005174
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07005175 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
5176
David S. Millere8803b62012-06-16 01:12:19 -07005177 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005178 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005179 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005180
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005181 ret = xfrm6_init();
5182 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07005183 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08005184
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005185 ret = fib6_rules_init();
5186 if (ret)
5187 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08005188
Thomas Grafd1896342012-06-18 12:08:33 +00005189 ret = register_pernet_subsys(&ip6_route_net_late_ops);
5190 if (ret)
5191 goto fib6_rules_init;
5192
Florian Westphal16feebc2017-12-02 21:44:08 +01005193 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
5194 inet6_rtm_newroute, NULL, 0);
5195 if (ret < 0)
5196 goto out_register_late_subsys;
5197
5198 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
5199 inet6_rtm_delroute, NULL, 0);
5200 if (ret < 0)
5201 goto out_register_late_subsys;
5202
5203 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
5204 inet6_rtm_getroute, NULL,
5205 RTNL_FLAG_DOIT_UNLOCKED);
5206 if (ret < 0)
Thomas Grafd1896342012-06-18 12:08:33 +00005207 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005208
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005209 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005210 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00005211 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005212
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07005213 for_each_possible_cpu(cpu) {
5214 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
5215
5216 INIT_LIST_HEAD(&ul->head);
5217 spin_lock_init(&ul->lock);
5218 }
5219
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005220out:
5221 return ret;
5222
Thomas Grafd1896342012-06-18 12:08:33 +00005223out_register_late_subsys:
Florian Westphal16feebc2017-12-02 21:44:08 +01005224 rtnl_unregister_all(PF_INET6);
Thomas Grafd1896342012-06-18 12:08:33 +00005225 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005226fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005227 fib6_rules_cleanup();
5228xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005229 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00005230out_fib6_init:
5231 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005232out_register_subsys:
5233 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07005234out_register_inetpeer:
5235 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00005236out_dst_entries:
5237 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005238out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005239 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08005240 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241}
5242
5243void ip6_route_cleanup(void)
5244{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005245 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00005246 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07005247 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07005250 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005251 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00005252 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08005253 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254}