blob: 2931224b674e81e1608cbf5b6a08a31cb6f99415 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Linux INET6 implementation
4 * FIB front-end.
5 *
6 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09007 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10/* Changes:
11 *
12 * YOSHIFUJI Hideaki @USAGI
13 * reworked default router selection.
14 * - respect outgoing interface
15 * - select from (probably) reachable routers (i.e.
16 * routers in REACHABLE, STALE, DELAY or PROBE states).
17 * - always select the same router if it is (probably)
18 * reachable. otherwise, round-robin the list.
YOSHIFUJI Hideakic0bece92006-08-23 17:23:25 -070019 * Ville Nuorvala
20 * Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
Joe Perchesf3213832012-05-15 14:11:53 +000023#define pr_fmt(fmt) "IPv6: " fmt
24
Randy Dunlap4fc268d2006-01-11 12:17:47 -080025#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/errno.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040027#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/types.h>
29#include <linux/times.h>
30#include <linux/socket.h>
31#include <linux/sockios.h>
32#include <linux/net.h>
33#include <linux/route.h>
34#include <linux/netdevice.h>
35#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090036#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/proc_fs.h>
40#include <linux/seq_file.h>
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080041#include <linux/nsproxy.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Wei Wang35732d02017-10-06 12:05:57 -070043#include <linux/jhash.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020044#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <net/snmp.h>
46#include <net/ipv6.h>
47#include <net/ip6_fib.h>
48#include <net/ip6_route.h>
49#include <net/ndisc.h>
50#include <net/addrconf.h>
51#include <net/tcp.h>
52#include <linux/rtnetlink.h>
53#include <net/dst.h>
Jiri Benc904af042015-08-20 13:56:31 +020054#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/xfrm.h>
Tom Tucker8d717402006-07-30 20:43:36 -070056#include <net/netevent.h>
Thomas Graf21713eb2006-08-15 00:35:24 -070057#include <net/netlink.h>
David Ahern3c618c12019-04-20 09:28:20 -070058#include <net/rtnh.h>
Roopa Prabhu19e42e42015-07-21 10:43:48 +020059#include <net/lwtunnel.h>
Jiri Benc904af042015-08-20 13:56:31 +020060#include <net/ip_tunnels.h>
David Ahernca254492015-10-12 11:47:10 -070061#include <net/l3mdev.h>
Roopa Prabhueacb9382018-05-22 14:03:28 -070062#include <net/ip.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080063#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#ifdef CONFIG_SYSCTL
66#include <linux/sysctl.h>
67#endif
68
David Ahern30d444d2018-05-23 17:08:48 -070069static int ip6_rt_type_to_error(u8 fib6_type);
70
71#define CREATE_TRACE_POINTS
72#include <trace/events/fib6.h>
73EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
74#undef CREATE_TRACE_POINTS
75
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020076enum rt6_nud_state {
Jiri Benc7e980562013-12-11 13:48:20 +010077 RT6_NUD_FAIL_HARD = -3,
78 RT6_NUD_FAIL_PROBE = -2,
79 RT6_NUD_FAIL_DO_RR = -1,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020080 RT6_NUD_SUCCEED = 1
81};
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
David S. Miller0dbaee32010-12-13 12:52:14 -080084static unsigned int ip6_default_advmss(const struct dst_entry *dst);
Steffen Klassertebb762f2011-11-23 02:12:51 +000085static unsigned int ip6_mtu(const struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static struct dst_entry *ip6_negative_advice(struct dst_entry *);
87static void ip6_dst_destroy(struct dst_entry *);
88static void ip6_dst_ifdown(struct dst_entry *,
89 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080090static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92static int ip6_pkt_discard(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050093static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Kamala R7150aed2013-12-02 19:55:21 +053094static int ip6_pkt_prohibit(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050095static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static void ip6_link_failure(struct sk_buff *skb);
David S. Miller6700c272012-07-17 03:29:28 -070097static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +080098 struct sk_buff *skb, u32 mtu,
99 bool confirm_neigh);
David S. Miller6700c272012-07-17 03:29:28 -0700100static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
101 struct sk_buff *skb);
David Ahern702cea52019-04-09 14:41:13 -0700102static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
103 int strict);
David Aherna1b7a1f2019-06-08 14:53:26 -0700104static size_t rt6_nlmsg_size(struct fib6_info *f6i);
David Ahernd4ead6b2018-04-17 17:33:16 -0700105static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -0700106 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -0700107 struct in6_addr *dest, struct in6_addr *src,
David Ahern16a16cd2017-02-02 12:37:11 -0800108 int iif, int type, u32 portid, u32 seq,
109 unsigned int flags);
David Ahern7e4b5122019-04-16 14:36:00 -0700110static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
Wei Wang510e2ce2019-05-16 13:30:54 -0700111 const struct in6_addr *daddr,
112 const struct in6_addr *saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800114#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -0700115static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000116 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700117 const struct in6_addr *gwaddr,
118 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +0000119 unsigned int pref);
David Ahern8d1c8022018-04-17 17:33:26 -0700120static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000121 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700122 const struct in6_addr *gwaddr,
123 struct net_device *dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800124#endif
125
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700126struct uncached_list {
127 spinlock_t lock;
128 struct list_head head;
129};
130
131static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
132
Xin Long510c3212018-02-14 19:06:02 +0800133void rt6_uncached_list_add(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700134{
135 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
136
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700137 rt->rt6i_uncached_list = ul;
138
139 spin_lock_bh(&ul->lock);
140 list_add_tail(&rt->rt6i_uncached, &ul->head);
141 spin_unlock_bh(&ul->lock);
142}
143
Xin Long510c3212018-02-14 19:06:02 +0800144void rt6_uncached_list_del(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700145{
146 if (!list_empty(&rt->rt6i_uncached)) {
147 struct uncached_list *ul = rt->rt6i_uncached_list;
Wei Wang81eb8442017-10-06 12:06:11 -0700148 struct net *net = dev_net(rt->dst.dev);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700149
150 spin_lock_bh(&ul->lock);
151 list_del(&rt->rt6i_uncached);
Wei Wang81eb8442017-10-06 12:06:11 -0700152 atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700153 spin_unlock_bh(&ul->lock);
154 }
155}
156
157static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
158{
159 struct net_device *loopback_dev = net->loopback_dev;
160 int cpu;
161
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500162 if (dev == loopback_dev)
163 return;
164
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700165 for_each_possible_cpu(cpu) {
166 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
167 struct rt6_info *rt;
168
169 spin_lock_bh(&ul->lock);
170 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
171 struct inet6_dev *rt_idev = rt->rt6i_idev;
172 struct net_device *rt_dev = rt->dst.dev;
173
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500174 if (rt_idev->dev == dev) {
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700175 rt->rt6i_idev = in6_dev_get(loopback_dev);
176 in6_dev_put(rt_idev);
177 }
178
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500179 if (rt_dev == dev) {
Mahesh Bandewar8d7017f2019-07-01 14:38:57 -0700180 rt->dst.dev = blackhole_netdev;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700181 dev_hold(rt->dst.dev);
182 dev_put(rt_dev);
183 }
184 }
185 spin_unlock_bh(&ul->lock);
186 }
187}
188
David Ahernf8a1b432018-04-17 17:33:21 -0700189static inline const void *choose_neigh_daddr(const struct in6_addr *p,
David S. Millerf894cbf2012-07-02 21:52:24 -0700190 struct sk_buff *skb,
191 const void *daddr)
David S. Miller39232972012-01-26 15:22:32 -0500192{
David S. Millera7563f32012-01-26 16:29:16 -0500193 if (!ipv6_addr_any(p))
David S. Miller39232972012-01-26 15:22:32 -0500194 return (const void *) p;
David S. Millerf894cbf2012-07-02 21:52:24 -0700195 else if (skb)
196 return &ipv6_hdr(skb)->daddr;
David S. Miller39232972012-01-26 15:22:32 -0500197 return daddr;
198}
199
David Ahernf8a1b432018-04-17 17:33:21 -0700200struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
201 struct net_device *dev,
202 struct sk_buff *skb,
203 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700204{
David S. Miller39232972012-01-26 15:22:32 -0500205 struct neighbour *n;
206
David Ahernf8a1b432018-04-17 17:33:21 -0700207 daddr = choose_neigh_daddr(gw, skb, daddr);
208 n = __ipv6_neigh_lookup(dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500209 if (n)
210 return n;
Stefano Brivio7adf3242019-01-02 13:29:27 +0100211
212 n = neigh_create(&nd_tbl, daddr, dev);
213 return IS_ERR(n) ? NULL : n;
David Ahernf8a1b432018-04-17 17:33:21 -0700214}
215
216static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
217 struct sk_buff *skb,
218 const void *daddr)
219{
220 const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
221
Nicolas Dichtel2c6b55f2019-06-24 16:01:09 +0200222 return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
223 dst->dev, skb, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500224}
225
Julian Anastasov63fca652017-02-06 23:14:15 +0200226static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
227{
228 struct net_device *dev = dst->dev;
229 struct rt6_info *rt = (struct rt6_info *)dst;
230
Stefano Briviocbfd6892019-09-09 22:44:06 +0200231 daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +0200232 if (!daddr)
233 return;
234 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
235 return;
236 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
237 return;
238 __ipv6_confirm_neigh(dev, daddr);
239}
240
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800241static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 .gc = ip6_dst_gc,
244 .gc_thresh = 1024,
245 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800246 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000247 .mtu = ip6_mtu,
David Ahernd4ead6b2018-04-17 17:33:16 -0700248 .cow_metrics = dst_cow_metrics_generic,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .destroy = ip6_dst_destroy,
250 .ifdown = ip6_dst_ifdown,
251 .negative_advice = ip6_negative_advice,
252 .link_failure = ip6_link_failure,
253 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700254 .redirect = rt6_do_redirect,
Eric W. Biederman9f8955c2015-10-07 16:48:39 -0500255 .local_out = __ip6_local_out,
David Ahernf8a1b432018-04-17 17:33:21 -0700256 .neigh_lookup = ip6_dst_neigh_lookup,
Julian Anastasov63fca652017-02-06 23:14:15 +0200257 .confirm_neigh = ip6_confirm_neigh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258};
259
Steffen Klassertebb762f2011-11-23 02:12:51 +0000260static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800261{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000262 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
263
264 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800265}
266
David S. Miller6700c272012-07-17 03:29:28 -0700267static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +0800268 struct sk_buff *skb, u32 mtu,
269 bool confirm_neigh)
David S. Miller14e50e52007-05-24 18:17:54 -0700270{
271}
272
David S. Miller6700c272012-07-17 03:29:28 -0700273static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
274 struct sk_buff *skb)
David S. Millerb587ee32012-07-12 00:39:24 -0700275{
276}
277
David S. Miller14e50e52007-05-24 18:17:54 -0700278static struct dst_ops ip6_dst_blackhole_ops = {
279 .family = AF_INET6,
David S. Miller14e50e52007-05-24 18:17:54 -0700280 .destroy = ip6_dst_destroy,
281 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000282 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800283 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700284 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Millerb587ee32012-07-12 00:39:24 -0700285 .redirect = ip6_rt_blackhole_redirect,
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -0700286 .cow_metrics = dst_cow_metrics_generic,
David Ahernf8a1b432018-04-17 17:33:21 -0700287 .neigh_lookup = ip6_dst_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700288};
289
David S. Miller62fa8a82011-01-26 20:51:05 -0800290static const u32 ip6_template_metrics[RTAX_MAX] = {
Li RongQing14edd872012-10-24 14:01:18 +0800291 [RTAX_HOPLIMIT - 1] = 0,
David S. Miller62fa8a82011-01-26 20:51:05 -0800292};
293
David Ahern8d1c8022018-04-17 17:33:26 -0700294static const struct fib6_info fib6_null_entry_template = {
David Ahern93c2fb22018-04-18 15:38:59 -0700295 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
296 .fib6_protocol = RTPROT_KERNEL,
297 .fib6_metric = ~(u32)0,
Eric Dumazetf05713e2019-04-22 18:35:03 -0700298 .fib6_ref = REFCOUNT_INIT(1),
David Ahern421842e2018-04-17 17:33:18 -0700299 .fib6_type = RTN_UNREACHABLE,
300 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
301};
302
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000303static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700304 .dst = {
305 .__refcnt = ATOMIC_INIT(1),
306 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000307 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700308 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700309 .input = ip6_pkt_discard,
310 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 },
312 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313};
314
Thomas Graf101367c2006-08-04 03:39:02 -0700315#ifdef CONFIG_IPV6_MULTIPLE_TABLES
316
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000317static const struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700318 .dst = {
319 .__refcnt = ATOMIC_INIT(1),
320 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000321 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700322 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700323 .input = ip6_pkt_prohibit,
324 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700325 },
326 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700327};
328
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000329static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700330 .dst = {
331 .__refcnt = ATOMIC_INIT(1),
332 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000333 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700334 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700335 .input = dst_discard,
Eric W. Biedermanede20592015-10-07 16:48:47 -0500336 .output = dst_discard_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700337 },
338 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700339};
340
341#endif
342
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700343static void rt6_info_init(struct rt6_info *rt)
344{
345 struct dst_entry *dst = &rt->dst;
346
347 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700348 INIT_LIST_HEAD(&rt->rt6i_uncached);
349}
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351/* allocate dst with ip6_dst_ops */
David Ahern93531c62018-04-17 17:33:25 -0700352struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
353 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
David S. Miller97bab732012-06-09 22:36:36 -0700355 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Wei Wangb2a9c0e2017-06-17 10:42:41 -0700356 1, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700357
Wei Wang81eb8442017-10-06 12:06:11 -0700358 if (rt) {
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700359 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -0700360 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
361 }
Steffen Klassert81048912012-07-05 23:37:09 +0000362
David S. Millercf911662011-04-28 14:31:47 -0700363 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
David Ahern9ab179d2016-04-07 11:10:06 -0700365EXPORT_SYMBOL(ip6_dst_alloc);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static void ip6_dst_destroy(struct dst_entry *dst)
368{
369 struct rt6_info *rt = (struct rt6_info *)dst;
David Aherna68886a2018-04-20 15:38:02 -0700370 struct fib6_info *from;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700371 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
David Ahern1620a332018-10-04 20:07:54 -0700373 ip_dst_metrics_put(dst);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700374 rt6_uncached_list_del(rt);
375
376 idev = rt->rt6i_idev;
David S. Miller38308472011-12-03 18:02:47 -0500377 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 rt->rt6i_idev = NULL;
379 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900380 }
Gao feng1716a962012-04-06 00:13:10 +0000381
Eric Dumazet0e233872019-04-28 12:22:25 -0700382 from = xchg((__force struct fib6_info **)&rt->from, NULL);
David Ahern93531c62018-04-17 17:33:25 -0700383 fib6_info_release(from);
David S. Millerb3419362010-11-30 12:27:11 -0800384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
387 int how)
388{
389 struct rt6_info *rt = (struct rt6_info *)dst;
390 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800391 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900392 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Wei Wange5645f52017-08-14 10:44:59 -0700394 if (idev && idev->dev != loopback_dev) {
395 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
396 if (loopback_idev) {
397 rt->rt6i_idev = loopback_idev;
398 in6_dev_put(idev);
David S. Miller97cac082012-07-02 22:43:47 -0700399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401}
402
Martin KaFai Lau5973fb12015-11-11 11:51:07 -0800403static bool __rt6_check_expired(const struct rt6_info *rt)
404{
405 if (rt->rt6i_flags & RTF_EXPIRES)
406 return time_after(jiffies, rt->dst.expires);
407 else
408 return false;
409}
410
Eric Dumazeta50feda2012-05-18 18:57:34 +0000411static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
David Aherna68886a2018-04-20 15:38:02 -0700413 struct fib6_info *from;
414
415 from = rcu_dereference(rt->from);
416
Gao feng1716a962012-04-06 00:13:10 +0000417 if (rt->rt6i_flags & RTF_EXPIRES) {
418 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000419 return true;
David Aherna68886a2018-04-20 15:38:02 -0700420 } else if (from) {
Xin Long1e2ea8a2017-08-26 20:10:10 +0800421 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
David Aherna68886a2018-04-20 15:38:02 -0700422 fib6_check_expired(from);
Gao feng1716a962012-04-06 00:13:10 +0000423 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000424 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
David Ahernb1d40992019-04-16 14:35:59 -0700427void fib6_select_path(const struct net *net, struct fib6_result *res,
428 struct flowi6 *fl6, int oif, bool have_oif_match,
429 const struct sk_buff *skb, int strict)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000430{
David Ahern8d1c8022018-04-17 17:33:26 -0700431 struct fib6_info *sibling, *next_sibling;
David Ahernb1d40992019-04-16 14:35:59 -0700432 struct fib6_info *match = res->f6i;
433
David Ahernf88d8ea2019-06-03 20:19:52 -0700434 if ((!match->fib6_nsiblings && !match->nh) || have_oif_match)
David Ahernb1d40992019-04-16 14:35:59 -0700435 goto out;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000436
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200437 /* We might have already computed the hash for ICMPv6 errors. In such
438 * case it will always be non-zero. Otherwise now is the time to do it.
439 */
David Ahernf88d8ea2019-06-03 20:19:52 -0700440 if (!fl6->mp_hash &&
441 (!match->nh || nexthop_is_multipath(match->nh)))
David Ahernb4bac172018-03-02 08:32:18 -0800442 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200443
David Ahernf88d8ea2019-06-03 20:19:52 -0700444 if (unlikely(match->nh)) {
445 nexthop_path_fib6_result(res, fl6->mp_hash);
446 return;
447 }
448
David Ahern1cf844c2019-05-22 20:27:59 -0700449 if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
David Ahernb1d40992019-04-16 14:35:59 -0700450 goto out;
Ido Schimmelbbfcd772017-11-21 09:50:12 +0200451
David Ahern93c2fb22018-04-18 15:38:59 -0700452 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
453 fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -0700454 const struct fib6_nh *nh = sibling->fib6_nh;
David Ahern5e670d82018-04-17 17:33:14 -0700455 int nh_upper_bound;
456
David Ahern702cea52019-04-09 14:41:13 -0700457 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
David Ahern5e670d82018-04-17 17:33:14 -0700458 if (fl6->mp_hash > nh_upper_bound)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200459 continue;
David Ahern702cea52019-04-09 14:41:13 -0700460 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200461 break;
462 match = sibling;
463 break;
464 }
465
David Ahernb1d40992019-04-16 14:35:59 -0700466out:
467 res->f6i = match;
David Ahern1cf844c2019-05-22 20:27:59 -0700468 res->nh = match->fib6_nh;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471/*
Wei Wang66f5d6c2017-10-06 12:06:10 -0700472 * Route lookup. rcu_read_lock() should be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 */
474
David Ahern0c59d002019-04-09 14:41:18 -0700475static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
476 const struct in6_addr *saddr, int oif, int flags)
477{
478 const struct net_device *dev;
479
480 if (nh->fib_nh_flags & RTNH_F_DEAD)
481 return false;
482
483 dev = nh->fib_nh_dev;
484 if (oif) {
485 if (dev->ifindex == oif)
486 return true;
487 } else {
488 if (ipv6_chk_addr(net, saddr, dev,
489 flags & RT6_LOOKUP_F_IFACE))
490 return true;
491 }
492
493 return false;
494}
495
David Ahern962b6802019-06-08 14:53:24 -0700496struct fib6_nh_dm_arg {
497 struct net *net;
498 const struct in6_addr *saddr;
499 int oif;
500 int flags;
501 struct fib6_nh *nh;
502};
503
504static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
505{
506 struct fib6_nh_dm_arg *arg = _arg;
507
508 arg->nh = nh;
509 return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
510 arg->flags);
511}
512
513/* returns fib6_nh from nexthop or NULL */
514static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
515 struct fib6_result *res,
516 const struct in6_addr *saddr,
517 int oif, int flags)
518{
519 struct fib6_nh_dm_arg arg = {
520 .net = net,
521 .saddr = saddr,
522 .oif = oif,
523 .flags = flags,
524 };
525
526 if (nexthop_is_blackhole(nh))
527 return NULL;
528
529 if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
530 return arg.nh;
531
532 return NULL;
533}
534
David Ahern75ef7382019-04-16 14:36:07 -0700535static void rt6_device_match(struct net *net, struct fib6_result *res,
536 const struct in6_addr *saddr, int oif, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
David Ahern75ef7382019-04-16 14:36:07 -0700538 struct fib6_info *f6i = res->f6i;
539 struct fib6_info *spf6i;
540 struct fib6_nh *nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
David Ahern75ef7382019-04-16 14:36:07 -0700542 if (!oif && ipv6_addr_any(saddr)) {
David Ahernf88d8ea2019-06-03 20:19:52 -0700543 if (unlikely(f6i->nh)) {
544 nh = nexthop_fib6_nh(f6i->nh);
545 if (nexthop_is_blackhole(f6i->nh))
546 goto out_blackhole;
547 } else {
548 nh = f6i->fib6_nh;
549 }
David Ahern7d21fec2019-04-16 14:36:11 -0700550 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
551 goto out;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
David Ahern75ef7382019-04-16 14:36:07 -0700554 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
David Ahern962b6802019-06-08 14:53:24 -0700555 bool matched = false;
556
557 if (unlikely(spf6i->nh)) {
558 nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
559 oif, flags);
560 if (nh)
561 matched = true;
562 } else {
563 nh = spf6i->fib6_nh;
564 if (__rt6_device_match(net, nh, saddr, oif, flags))
565 matched = true;
566 }
567 if (matched) {
David Ahern75ef7382019-04-16 14:36:07 -0700568 res->f6i = spf6i;
David Ahern7d21fec2019-04-16 14:36:11 -0700569 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700570 }
571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
David Ahern75ef7382019-04-16 14:36:07 -0700573 if (oif && flags & RT6_LOOKUP_F_IFACE) {
574 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700575 nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700576 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700577 }
578
David Ahernf88d8ea2019-06-03 20:19:52 -0700579 if (unlikely(f6i->nh)) {
580 nh = nexthop_fib6_nh(f6i->nh);
581 if (nexthop_is_blackhole(f6i->nh))
582 goto out_blackhole;
583 } else {
584 nh = f6i->fib6_nh;
585 }
586
David Ahern7d21fec2019-04-16 14:36:11 -0700587 if (nh->fib_nh_flags & RTNH_F_DEAD) {
David Ahern75ef7382019-04-16 14:36:07 -0700588 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700589 nh = res->f6i->fib6_nh;
David Ahern75ef7382019-04-16 14:36:07 -0700590 }
David Ahern7d21fec2019-04-16 14:36:11 -0700591out:
592 res->nh = nh;
593 res->fib6_type = res->f6i->fib6_type;
594 res->fib6_flags = res->f6i->fib6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -0700595 return;
596
597out_blackhole:
598 res->fib6_flags |= RTF_REJECT;
599 res->fib6_type = RTN_BLACKHOLE;
600 res->nh = nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800603#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200604struct __rt6_probe_work {
605 struct work_struct work;
606 struct in6_addr target;
607 struct net_device *dev;
608};
609
610static void rt6_probe_deferred(struct work_struct *w)
611{
612 struct in6_addr mcaddr;
613 struct __rt6_probe_work *work =
614 container_of(w, struct __rt6_probe_work, work);
615
616 addrconf_addr_solict_mult(&work->target, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800617 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200618 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100619 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200620}
621
David Aherncc3a86c2019-04-09 14:41:12 -0700622static void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800623{
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200624 struct __rt6_probe_work *work = NULL;
David Ahern5e670d82018-04-17 17:33:14 -0700625 const struct in6_addr *nh_gw;
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800626 unsigned long last_probe;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000627 struct neighbour *neigh;
David Ahern5e670d82018-04-17 17:33:14 -0700628 struct net_device *dev;
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200629 struct inet6_dev *idev;
David Ahern5e670d82018-04-17 17:33:14 -0700630
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800631 /*
632 * Okay, this does not seem to be appropriate
633 * for now, however, we need to check if it
634 * is really so; aka Router Reachability Probing.
635 *
636 * Router Reachability Probe MUST be rate-limited
637 * to no more than one per minute.
638 */
Hangbin Liu004b3942019-11-20 15:39:06 +0800639 if (!fib6_nh->fib_nh_gw_family)
Amerigo Wangfdd66812012-09-10 02:48:44 +0000640 return;
David Ahern5e670d82018-04-17 17:33:14 -0700641
David Aherncc3a86c2019-04-09 14:41:12 -0700642 nh_gw = &fib6_nh->fib_nh_gw6;
643 dev = fib6_nh->fib_nh_dev;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000644 rcu_read_lock_bh();
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800645 last_probe = READ_ONCE(fib6_nh->last_probe);
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200646 idev = __in6_dev_get(dev);
David Ahern5e670d82018-04-17 17:33:14 -0700647 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000648 if (neigh) {
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700649 if (neigh->nud_state & NUD_VALID)
650 goto out;
651
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000652 write_lock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700653 if (!(neigh->nud_state & NUD_VALID) &&
654 time_after(jiffies,
David Aherndcd1f572018-04-18 15:39:05 -0700655 neigh->updated + idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700656 work = kmalloc(sizeof(*work), GFP_ATOMIC);
657 if (work)
658 __neigh_set_probe_once(neigh);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200659 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000660 write_unlock(&neigh->lock);
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800661 } else if (time_after(jiffies, last_probe +
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200662 idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700663 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000664 }
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700665
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800666 if (!work || cmpxchg(&fib6_nh->last_probe,
667 last_probe, jiffies) != last_probe) {
668 kfree(work);
669 } else {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700670 INIT_WORK(&work->work, rt6_probe_deferred);
David Ahern5e670d82018-04-17 17:33:14 -0700671 work->target = *nh_gw;
672 dev_hold(dev);
673 work->dev = dev;
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700674 schedule_work(&work->work);
675 }
676
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700677out:
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000678 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800679}
680#else
David Aherncc3a86c2019-04-09 14:41:12 -0700681static inline void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800682{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800683}
684#endif
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800687 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
David Ahern1ba9a892019-04-09 14:41:10 -0700689static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200691 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
David Ahern5e670d82018-04-17 17:33:14 -0700692 struct neighbour *neigh;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000693
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000694 rcu_read_lock_bh();
David Ahern1ba9a892019-04-09 14:41:10 -0700695 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
696 &fib6_nh->fib_nh_gw6);
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000697 if (neigh) {
698 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800699 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200700 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800701#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000702 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200703 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100704 else
705 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800706#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000707 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200708 } else {
709 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100710 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000711 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000712 rcu_read_unlock_bh();
713
Paul Marksa5a81f02012-12-03 10:26:54 +0000714 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800715}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
David Ahern702cea52019-04-09 14:41:13 -0700717static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
718 int strict)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800719{
David Ahern6e1809a2019-04-09 14:41:11 -0700720 int m = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900721
David Ahern6e1809a2019-04-09 14:41:11 -0700722 if (!oif || nh->fib_nh_dev->ifindex == oif)
723 m = 2;
724
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700725 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200726 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800727#ifdef CONFIG_IPV6_ROUTER_PREF
David Ahern702cea52019-04-09 14:41:13 -0700728 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800729#endif
David Ahern1ba9a892019-04-09 14:41:10 -0700730 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
David Ahern702cea52019-04-09 14:41:13 -0700731 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
David Ahern1ba9a892019-04-09 14:41:10 -0700732 int n = rt6_check_neigh(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200733 if (n < 0)
734 return n;
735 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800736 return m;
737}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
David Ahern28679ed2019-04-09 14:41:14 -0700739static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
740 int oif, int strict, int *mpri, bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800741{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200742 bool match_do_rr = false;
David Ahern28679ed2019-04-09 14:41:14 -0700743 bool rc = false;
744 int m;
Andy Gospodarek35103d12015-08-13 10:39:01 -0400745
David Ahern28679ed2019-04-09 14:41:14 -0700746 if (nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +0200747 goto out;
748
David Ahern28679ed2019-04-09 14:41:14 -0700749 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
750 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
David Ahernd5d32e42016-10-24 12:27:23 -0700751 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
Andy Gospodarek35103d12015-08-13 10:39:01 -0400752 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700753
David Ahern28679ed2019-04-09 14:41:14 -0700754 m = rt6_score_route(nh, fib6_flags, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100755 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200756 match_do_rr = true;
757 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100758 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700759 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700760 }
761
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200762 if (strict & RT6_LOOKUP_F_REACHABLE)
David Ahern28679ed2019-04-09 14:41:14 -0700763 rt6_probe(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200764
Jiri Benc7e980562013-12-11 13:48:20 +0100765 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200766 if (m > *mpri) {
767 *do_rr = match_do_rr;
768 *mpri = m;
David Ahern28679ed2019-04-09 14:41:14 -0700769 rc = true;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200770 }
David S. Millerf11e6652007-03-24 20:36:25 -0700771out:
David Ahern28679ed2019-04-09 14:41:14 -0700772 return rc;
David S. Millerf11e6652007-03-24 20:36:25 -0700773}
774
David Ahern17a59842019-06-08 14:53:25 -0700775struct fib6_nh_frl_arg {
776 u32 flags;
777 int oif;
778 int strict;
779 int *mpri;
780 bool *do_rr;
781 struct fib6_nh *nh;
782};
783
784static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
785{
786 struct fib6_nh_frl_arg *arg = _arg;
787
788 arg->nh = nh;
789 return find_match(nh, arg->flags, arg->oif, arg->strict,
790 arg->mpri, arg->do_rr);
791}
792
David Ahernb7bc4b62019-04-16 14:36:08 -0700793static void __find_rr_leaf(struct fib6_info *f6i_start,
David Ahern30c15f02019-04-09 14:41:15 -0700794 struct fib6_info *nomatch, u32 metric,
David Ahernb7bc4b62019-04-16 14:36:08 -0700795 struct fib6_result *res, struct fib6_info **cont,
David Ahern30c15f02019-04-09 14:41:15 -0700796 int oif, int strict, bool *do_rr, int *mpri)
David S. Millerf11e6652007-03-24 20:36:25 -0700797{
David Ahernb7bc4b62019-04-16 14:36:08 -0700798 struct fib6_info *f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700799
David Ahernb7bc4b62019-04-16 14:36:08 -0700800 for (f6i = f6i_start;
801 f6i && f6i != nomatch;
802 f6i = rcu_dereference(f6i->fib6_next)) {
David Ahern17a59842019-06-08 14:53:25 -0700803 bool matched = false;
David Ahern30c15f02019-04-09 14:41:15 -0700804 struct fib6_nh *nh;
805
David Ahernb7bc4b62019-04-16 14:36:08 -0700806 if (cont && f6i->fib6_metric != metric) {
807 *cont = f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700808 return;
809 }
810
David Ahernb7bc4b62019-04-16 14:36:08 -0700811 if (fib6_check_expired(f6i))
David Ahern30c15f02019-04-09 14:41:15 -0700812 continue;
813
David Ahern17a59842019-06-08 14:53:25 -0700814 if (unlikely(f6i->nh)) {
815 struct fib6_nh_frl_arg arg = {
816 .flags = f6i->fib6_flags,
817 .oif = oif,
818 .strict = strict,
819 .mpri = mpri,
820 .do_rr = do_rr
821 };
822
823 if (nexthop_is_blackhole(f6i->nh)) {
824 res->fib6_flags = RTF_REJECT;
825 res->fib6_type = RTN_BLACKHOLE;
826 res->f6i = f6i;
827 res->nh = nexthop_fib6_nh(f6i->nh);
828 return;
829 }
830 if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
831 &arg)) {
832 matched = true;
833 nh = arg.nh;
834 }
835 } else {
836 nh = f6i->fib6_nh;
837 if (find_match(nh, f6i->fib6_flags, oif, strict,
838 mpri, do_rr))
839 matched = true;
840 }
841 if (matched) {
David Ahernb7bc4b62019-04-16 14:36:08 -0700842 res->f6i = f6i;
843 res->nh = nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700844 res->fib6_flags = f6i->fib6_flags;
845 res->fib6_type = f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700846 }
David Ahern30c15f02019-04-09 14:41:15 -0700847 }
848}
849
David Ahernb7bc4b62019-04-16 14:36:08 -0700850static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
851 struct fib6_info *rr_head, int oif, int strict,
852 bool *do_rr, struct fib6_result *res)
David Ahern30c15f02019-04-09 14:41:15 -0700853{
David Ahernb7bc4b62019-04-16 14:36:08 -0700854 u32 metric = rr_head->fib6_metric;
855 struct fib6_info *cont = NULL;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800856 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
David Ahernb7bc4b62019-04-16 14:36:08 -0700858 __find_rr_leaf(rr_head, NULL, metric, res, &cont,
David Ahern30c15f02019-04-09 14:41:15 -0700859 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700860
David Ahernb7bc4b62019-04-16 14:36:08 -0700861 __find_rr_leaf(leaf, rr_head, metric, res, &cont,
David Ahern30c15f02019-04-09 14:41:15 -0700862 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700863
David Ahernb7bc4b62019-04-16 14:36:08 -0700864 if (res->f6i || !cont)
865 return;
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700866
David Ahernb7bc4b62019-04-16 14:36:08 -0700867 __find_rr_leaf(cont, NULL, metric, res, NULL,
David Ahern30c15f02019-04-09 14:41:15 -0700868 oif, strict, do_rr, &mpri);
David S. Millerf11e6652007-03-24 20:36:25 -0700869}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800870
David Ahernb7bc4b62019-04-16 14:36:08 -0700871static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
872 struct fib6_result *res, int strict)
David S. Millerf11e6652007-03-24 20:36:25 -0700873{
David Ahern8d1c8022018-04-17 17:33:26 -0700874 struct fib6_info *leaf = rcu_dereference(fn->leaf);
David Ahernb7bc4b62019-04-16 14:36:08 -0700875 struct fib6_info *rt0;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200876 bool do_rr = false;
Wei Wang17ecf592017-10-06 12:06:09 -0700877 int key_plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
David Ahernb7bc4b62019-04-16 14:36:08 -0700879 /* make sure this function or its helpers sets f6i */
880 res->f6i = NULL;
881
David Ahern421842e2018-04-17 17:33:18 -0700882 if (!leaf || leaf == net->ipv6.fib6_null_entry)
David Ahernb7bc4b62019-04-16 14:36:08 -0700883 goto out;
Wei Wang8d1040e2017-10-06 12:06:08 -0700884
Wei Wang66f5d6c2017-10-06 12:06:10 -0700885 rt0 = rcu_dereference(fn->rr_ptr);
David S. Millerf11e6652007-03-24 20:36:25 -0700886 if (!rt0)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700887 rt0 = leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Wei Wang17ecf592017-10-06 12:06:09 -0700889 /* Double check to make sure fn is not an intermediate node
890 * and fn->leaf does not points to its child's leaf
891 * (This might happen if all routes under fn are deleted from
892 * the tree and fib6_repair_tree() is called on the node.)
893 */
David Ahern93c2fb22018-04-18 15:38:59 -0700894 key_plen = rt0->fib6_dst.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700895#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -0700896 if (rt0->fib6_src.plen)
897 key_plen = rt0->fib6_src.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700898#endif
899 if (fn->fn_bit != key_plen)
David Ahernb7bc4b62019-04-16 14:36:08 -0700900 goto out;
Wei Wang17ecf592017-10-06 12:06:09 -0700901
David Ahernb7bc4b62019-04-16 14:36:08 -0700902 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200903 if (do_rr) {
David Ahern8fb11a92018-05-04 13:54:24 -0700904 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
David S. Millerf11e6652007-03-24 20:36:25 -0700905
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800906 /* no entries matched; do round-robin */
David Ahern93c2fb22018-04-18 15:38:59 -0700907 if (!next || next->fib6_metric != rt0->fib6_metric)
Wei Wang8d1040e2017-10-06 12:06:08 -0700908 next = leaf;
David S. Millerf11e6652007-03-24 20:36:25 -0700909
Wei Wang66f5d6c2017-10-06 12:06:10 -0700910 if (next != rt0) {
David Ahern93c2fb22018-04-18 15:38:59 -0700911 spin_lock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700912 /* make sure next is not being deleted from the tree */
David Ahern93c2fb22018-04-18 15:38:59 -0700913 if (next->fib6_node)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700914 rcu_assign_pointer(fn->rr_ptr, next);
David Ahern93c2fb22018-04-18 15:38:59 -0700915 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918
David Ahernb7bc4b62019-04-16 14:36:08 -0700919out:
920 if (!res->f6i) {
921 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700922 res->nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700923 res->fib6_flags = res->f6i->fib6_flags;
924 res->fib6_type = res->f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
David Ahern85bd05d2019-04-16 14:36:01 -0700928static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700929{
David Ahern85bd05d2019-04-16 14:36:01 -0700930 return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
931 res->nh->fib_nh_gw_family;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700932}
933
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800934#ifdef CONFIG_IPV6_ROUTE_INFO
935int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000936 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800937{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900938 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800939 struct route_info *rinfo = (struct route_info *) opt;
940 struct in6_addr prefix_buf, *prefix;
941 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900942 unsigned long lifetime;
David Ahern8d1c8022018-04-17 17:33:26 -0700943 struct fib6_info *rt;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800944
945 if (len < sizeof(struct route_info)) {
946 return -EINVAL;
947 }
948
949 /* Sanity check for prefix_len and length */
950 if (rinfo->length > 3) {
951 return -EINVAL;
952 } else if (rinfo->prefix_len > 128) {
953 return -EINVAL;
954 } else if (rinfo->prefix_len > 64) {
955 if (rinfo->length < 2) {
956 return -EINVAL;
957 }
958 } else if (rinfo->prefix_len > 0) {
959 if (rinfo->length < 1) {
960 return -EINVAL;
961 }
962 }
963
964 pref = rinfo->route_pref;
965 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000966 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800967
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900968 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800969
970 if (rinfo->length == 3)
971 prefix = (struct in6_addr *)rinfo->prefix;
972 else {
973 /* this function is safe */
974 ipv6_addr_prefix(&prefix_buf,
975 (struct in6_addr *)rinfo->prefix,
976 rinfo->prefix_len);
977 prefix = &prefix_buf;
978 }
979
Duan Jiongf104a562013-11-08 09:56:53 +0800980 if (rinfo->prefix_len == 0)
David Ahernafb1d4b52018-04-17 17:33:11 -0700981 rt = rt6_get_dflt_router(net, gwaddr, dev);
Duan Jiongf104a562013-11-08 09:56:53 +0800982 else
983 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
David Ahern830218c2016-10-24 10:52:35 -0700984 gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800985
986 if (rt && !lifetime) {
David Ahernafb1d4b52018-04-17 17:33:11 -0700987 ip6_del_rt(net, rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800988 rt = NULL;
989 }
990
991 if (!rt && lifetime)
David Ahern830218c2016-10-24 10:52:35 -0700992 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
993 dev, pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800994 else if (rt)
David Ahern93c2fb22018-04-18 15:38:59 -0700995 rt->fib6_flags = RTF_ROUTEINFO |
996 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800997
998 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000999 if (!addrconf_finite_timeout(lifetime))
David Ahern14895682018-04-17 17:33:17 -07001000 fib6_clean_expires(rt);
Gao feng1716a962012-04-06 00:13:10 +00001001 else
David Ahern14895682018-04-17 17:33:17 -07001002 fib6_set_expires(rt, jiffies + HZ * lifetime);
Gao feng1716a962012-04-06 00:13:10 +00001003
David Ahern93531c62018-04-17 17:33:25 -07001004 fib6_info_release(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001005 }
1006 return 0;
1007}
1008#endif
1009
David Ahernae90d862018-04-17 17:33:12 -07001010/*
1011 * Misc support functions
1012 */
1013
1014/* called with rcu_lock held */
David Ahern0d161582019-04-16 14:36:04 -07001015static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
David Ahernae90d862018-04-17 17:33:12 -07001016{
David Ahern0d161582019-04-16 14:36:04 -07001017 struct net_device *dev = res->nh->fib_nh_dev;
David Ahernae90d862018-04-17 17:33:12 -07001018
David Ahern7d21fec2019-04-16 14:36:11 -07001019 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
David Ahernae90d862018-04-17 17:33:12 -07001020 /* for copies of local routes, dst->dev needs to be the
1021 * device if it is a master device, the master device if
1022 * device is enslaved, and the loopback as the default
1023 */
1024 if (netif_is_l3_slave(dev) &&
David Ahern7d21fec2019-04-16 14:36:11 -07001025 !rt6_need_strict(&res->f6i->fib6_dst.addr))
David Ahernae90d862018-04-17 17:33:12 -07001026 dev = l3mdev_master_dev_rcu(dev);
1027 else if (!netif_is_l3_master(dev))
1028 dev = dev_net(dev)->loopback_dev;
1029 /* last case is netif_is_l3_master(dev) is true in which
1030 * case we want dev returned to be dev
1031 */
1032 }
1033
1034 return dev;
1035}
1036
David Ahern6edb3c92018-04-17 17:33:15 -07001037static const int fib6_prop[RTN_MAX + 1] = {
1038 [RTN_UNSPEC] = 0,
1039 [RTN_UNICAST] = 0,
1040 [RTN_LOCAL] = 0,
1041 [RTN_BROADCAST] = 0,
1042 [RTN_ANYCAST] = 0,
1043 [RTN_MULTICAST] = 0,
1044 [RTN_BLACKHOLE] = -EINVAL,
1045 [RTN_UNREACHABLE] = -EHOSTUNREACH,
1046 [RTN_PROHIBIT] = -EACCES,
1047 [RTN_THROW] = -EAGAIN,
1048 [RTN_NAT] = -EINVAL,
1049 [RTN_XRESOLVE] = -EINVAL,
1050};
1051
1052static int ip6_rt_type_to_error(u8 fib6_type)
1053{
1054 return fib6_prop[fib6_type];
1055}
1056
David Ahern8d1c8022018-04-17 17:33:26 -07001057static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
David Ahern3b6761d2018-04-17 17:33:20 -07001058{
1059 unsigned short flags = 0;
1060
1061 if (rt->dst_nocount)
1062 flags |= DST_NOCOUNT;
1063 if (rt->dst_nopolicy)
1064 flags |= DST_NOPOLICY;
1065 if (rt->dst_host)
1066 flags |= DST_HOST;
1067
1068 return flags;
1069}
1070
David Ahern7d21fec2019-04-16 14:36:11 -07001071static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
David Ahern6edb3c92018-04-17 17:33:15 -07001072{
David Ahern7d21fec2019-04-16 14:36:11 -07001073 rt->dst.error = ip6_rt_type_to_error(fib6_type);
David Ahern6edb3c92018-04-17 17:33:15 -07001074
David Ahern7d21fec2019-04-16 14:36:11 -07001075 switch (fib6_type) {
David Ahern6edb3c92018-04-17 17:33:15 -07001076 case RTN_BLACKHOLE:
1077 rt->dst.output = dst_discard_out;
1078 rt->dst.input = dst_discard;
1079 break;
1080 case RTN_PROHIBIT:
1081 rt->dst.output = ip6_pkt_prohibit_out;
1082 rt->dst.input = ip6_pkt_prohibit;
1083 break;
1084 case RTN_THROW:
1085 case RTN_UNREACHABLE:
1086 default:
1087 rt->dst.output = ip6_pkt_discard_out;
1088 rt->dst.input = ip6_pkt_discard;
1089 break;
1090 }
1091}
1092
David Ahern0d161582019-04-16 14:36:04 -07001093static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
David Ahern6edb3c92018-04-17 17:33:15 -07001094{
David Ahern7d21fec2019-04-16 14:36:11 -07001095 struct fib6_info *f6i = res->f6i;
David Ahern0d161582019-04-16 14:36:04 -07001096
David Ahern7d21fec2019-04-16 14:36:11 -07001097 if (res->fib6_flags & RTF_REJECT) {
1098 ip6_rt_init_dst_reject(rt, res->fib6_type);
David Ahern6edb3c92018-04-17 17:33:15 -07001099 return;
1100 }
1101
1102 rt->dst.error = 0;
1103 rt->dst.output = ip6_output;
1104
David Ahern7d21fec2019-04-16 14:36:11 -07001105 if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
David Ahern6edb3c92018-04-17 17:33:15 -07001106 rt->dst.input = ip6_input;
David Ahern7d21fec2019-04-16 14:36:11 -07001107 } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
David Ahern6edb3c92018-04-17 17:33:15 -07001108 rt->dst.input = ip6_mc_input;
1109 } else {
1110 rt->dst.input = ip6_forward;
1111 }
1112
David Ahern0d161582019-04-16 14:36:04 -07001113 if (res->nh->fib_nh_lws) {
1114 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
David Ahern6edb3c92018-04-17 17:33:15 -07001115 lwtunnel_set_redirect(&rt->dst);
1116 }
1117
1118 rt->dst.lastuse = jiffies;
1119}
1120
Wei Wange873e4b2018-07-21 20:56:32 -07001121/* Caller must already hold reference to @from */
David Ahern8d1c8022018-04-17 17:33:26 -07001122static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
David Ahernae90d862018-04-17 17:33:12 -07001123{
David Ahernae90d862018-04-17 17:33:12 -07001124 rt->rt6i_flags &= ~RTF_EXPIRES;
David Aherna68886a2018-04-20 15:38:02 -07001125 rcu_assign_pointer(rt->from, from);
David Aherne1255ed2018-10-04 20:07:53 -07001126 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
David Ahernae90d862018-04-17 17:33:12 -07001127}
1128
David Ahern0d161582019-04-16 14:36:04 -07001129/* Caller must already hold reference to f6i in result */
1130static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
David Ahernae90d862018-04-17 17:33:12 -07001131{
David Ahern0d161582019-04-16 14:36:04 -07001132 const struct fib6_nh *nh = res->nh;
1133 const struct net_device *dev = nh->fib_nh_dev;
1134 struct fib6_info *f6i = res->f6i;
David Aherndcd1f572018-04-18 15:39:05 -07001135
David Ahern0d161582019-04-16 14:36:04 -07001136 ip6_rt_init_dst(rt, res);
David Ahern6edb3c92018-04-17 17:33:15 -07001137
David Ahern0d161582019-04-16 14:36:04 -07001138 rt->rt6i_dst = f6i->fib6_dst;
David Aherndcd1f572018-04-18 15:39:05 -07001139 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
David Ahern7d21fec2019-04-16 14:36:11 -07001140 rt->rt6i_flags = res->fib6_flags;
David Ahern0d161582019-04-16 14:36:04 -07001141 if (nh->fib_nh_gw_family) {
1142 rt->rt6i_gateway = nh->fib_nh_gw6;
David Ahern2b2450c2019-03-27 20:53:52 -07001143 rt->rt6i_flags |= RTF_GATEWAY;
1144 }
David Ahern0d161582019-04-16 14:36:04 -07001145 rt6_set_from(rt, f6i);
David Ahernae90d862018-04-17 17:33:12 -07001146#ifdef CONFIG_IPV6_SUBTREES
David Ahern0d161582019-04-16 14:36:04 -07001147 rt->rt6i_src = f6i->fib6_src;
David Ahernae90d862018-04-17 17:33:12 -07001148#endif
David Ahernae90d862018-04-17 17:33:12 -07001149}
1150
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001151static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1152 struct in6_addr *saddr)
1153{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001154 struct fib6_node *pn, *sn;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001155 while (1) {
1156 if (fn->fn_flags & RTN_TL_ROOT)
1157 return NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001158 pn = rcu_dereference(fn->parent);
1159 sn = FIB6_SUBTREE(pn);
1160 if (sn && sn != fn)
David Ahern64547432018-05-09 20:34:19 -07001161 fn = fib6_node_lookup(sn, NULL, saddr);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001162 else
1163 fn = pn;
1164 if (fn->fn_flags & RTN_RTINFO)
1165 return fn;
1166 }
1167}
Thomas Grafc71099a2006-08-04 23:20:06 -07001168
David Ahern10585b42019-03-20 09:24:50 -07001169static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
Wei Wangd3843fe2017-10-06 12:06:06 -07001170{
1171 struct rt6_info *rt = *prt;
1172
1173 if (dst_hold_safe(&rt->dst))
1174 return true;
David Ahern10585b42019-03-20 09:24:50 -07001175 if (net) {
Wei Wangd3843fe2017-10-06 12:06:06 -07001176 rt = net->ipv6.ip6_null_entry;
1177 dst_hold(&rt->dst);
1178 } else {
1179 rt = NULL;
1180 }
1181 *prt = rt;
1182 return false;
1183}
1184
David Aherndec9b0e2018-04-17 17:33:19 -07001185/* called with rcu_lock held */
David Ahern9b6b35a2019-04-16 14:36:02 -07001186static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
David Aherndec9b0e2018-04-17 17:33:19 -07001187{
David Ahern9b6b35a2019-04-16 14:36:02 -07001188 struct net_device *dev = res->nh->fib_nh_dev;
1189 struct fib6_info *f6i = res->f6i;
1190 unsigned short flags;
David Aherndec9b0e2018-04-17 17:33:19 -07001191 struct rt6_info *nrt;
1192
David Ahern9b6b35a2019-04-16 14:36:02 -07001193 if (!fib6_info_hold_safe(f6i))
Xin Long1c87e792019-03-20 14:45:48 +08001194 goto fallback;
Wei Wange873e4b2018-07-21 20:56:32 -07001195
David Ahern9b6b35a2019-04-16 14:36:02 -07001196 flags = fib6_info_dst_flags(f6i);
David Ahern93531c62018-04-17 17:33:25 -07001197 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
Xin Long1c87e792019-03-20 14:45:48 +08001198 if (!nrt) {
David Ahern9b6b35a2019-04-16 14:36:02 -07001199 fib6_info_release(f6i);
Xin Long1c87e792019-03-20 14:45:48 +08001200 goto fallback;
1201 }
David Aherndec9b0e2018-04-17 17:33:19 -07001202
David Ahern0d161582019-04-16 14:36:04 -07001203 ip6_rt_copy_init(nrt, res);
Xin Long1c87e792019-03-20 14:45:48 +08001204 return nrt;
1205
1206fallback:
1207 nrt = dev_net(dev)->ipv6.ip6_null_entry;
1208 dst_hold(&nrt->dst);
David Aherndec9b0e2018-04-17 17:33:19 -07001209 return nrt;
1210}
1211
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001212static struct rt6_info *ip6_pol_route_lookup(struct net *net,
1213 struct fib6_table *table,
David Ahernb75cc8f2018-03-02 08:32:17 -08001214 struct flowi6 *fl6,
1215 const struct sk_buff *skb,
1216 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
David Ahernb1d40992019-04-16 14:35:59 -07001218 struct fib6_result res = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 struct fib6_node *fn;
David Ahern23fb93a2018-04-17 17:33:23 -07001220 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
David Ahernb6cdbc82018-03-29 17:44:57 -07001222 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1223 flags &= ~RT6_LOOKUP_F_IFACE;
1224
Wei Wang66f5d6c2017-10-06 12:06:10 -07001225 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07001226 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -07001227restart:
David Ahernb1d40992019-04-16 14:35:59 -07001228 res.f6i = rcu_dereference(fn->leaf);
1229 if (!res.f6i)
1230 res.f6i = net->ipv6.fib6_null_entry;
David Ahernaf52a522019-04-09 14:41:16 -07001231 else
David Ahern75ef7382019-04-16 14:36:07 -07001232 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1233 flags);
David Ahernaf52a522019-04-09 14:41:16 -07001234
David Ahernb1d40992019-04-16 14:35:59 -07001235 if (res.f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001236 fn = fib6_backtrack(fn, &fl6->saddr);
1237 if (fn)
1238 goto restart;
David Ahernaf52a522019-04-09 14:41:16 -07001239
1240 rt = net->ipv6.ip6_null_entry;
1241 dst_hold(&rt->dst);
1242 goto out;
David Ahernf88d8ea2019-06-03 20:19:52 -07001243 } else if (res.fib6_flags & RTF_REJECT) {
1244 goto do_create;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001245 }
Wei Wang2b760fc2017-10-06 12:06:03 -07001246
David Ahernb1d40992019-04-16 14:35:59 -07001247 fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1248 fl6->flowi6_oif != 0, skb, flags);
1249
David S. Miller4c9483b2011-03-12 16:22:43 -05001250 /* Search through exception table */
David Ahern7e4b5122019-04-16 14:36:00 -07001251 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
David Ahern23fb93a2018-04-17 17:33:23 -07001252 if (rt) {
David Ahern10585b42019-03-20 09:24:50 -07001253 if (ip6_hold_safe(net, &rt))
David Aherndec9b0e2018-04-17 17:33:19 -07001254 dst_use_noref(&rt->dst, jiffies);
David Ahern23fb93a2018-04-17 17:33:23 -07001255 } else {
David Ahernf88d8ea2019-06-03 20:19:52 -07001256do_create:
David Ahern9b6b35a2019-04-16 14:36:02 -07001257 rt = ip6_create_rt_rcu(&res);
David Aherndec9b0e2018-04-17 17:33:19 -07001258 }
Wei Wangd3843fe2017-10-06 12:06:06 -07001259
David Ahernaf52a522019-04-09 14:41:16 -07001260out:
David Ahern8ff2e5b2019-04-16 14:36:09 -07001261 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahernaf52a522019-04-09 14:41:16 -07001262
Wei Wang66f5d6c2017-10-06 12:06:10 -07001263 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08001264
Thomas Grafc71099a2006-08-04 23:20:06 -07001265 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001266}
1267
Ian Morris67ba4152014-08-24 21:53:10 +01001268struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08001269 const struct sk_buff *skb, int flags)
Florian Westphalea6e5742011-09-05 16:05:44 +02001270{
David Ahernb75cc8f2018-03-02 08:32:17 -08001271 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
Florian Westphalea6e5742011-09-05 16:05:44 +02001272}
1273EXPORT_SYMBOL_GPL(ip6_route_lookup);
1274
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001275struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
David Ahernb75cc8f2018-03-02 08:32:17 -08001276 const struct in6_addr *saddr, int oif,
1277 const struct sk_buff *skb, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -07001278{
David S. Miller4c9483b2011-03-12 16:22:43 -05001279 struct flowi6 fl6 = {
1280 .flowi6_oif = oif,
1281 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001282 };
1283 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001284 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07001285
Thomas Grafadaa70b2006-10-13 15:01:03 -07001286 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -05001287 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -07001288 flags |= RT6_LOOKUP_F_HAS_SADDR;
1289 }
1290
David Ahernb75cc8f2018-03-02 08:32:17 -08001291 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -07001292 if (dst->error == 0)
1293 return (struct rt6_info *) dst;
1294
1295 dst_release(dst);
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return NULL;
1298}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001299EXPORT_SYMBOL(rt6_lookup);
1300
Thomas Grafc71099a2006-08-04 23:20:06 -07001301/* ip6_ins_rt is called with FREE table->tb6_lock.
Wei Wang1cfb71e2017-06-17 10:42:33 -07001302 * It takes new route entry, the addition fails by any reason the
1303 * route is released.
1304 * Caller must hold dst before calling it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 */
1306
David Ahern8d1c8022018-04-17 17:33:26 -07001307static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
David Ahern333c4302017-05-21 10:12:04 -06001308 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
1310 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001311 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
David Ahern93c2fb22018-04-18 15:38:59 -07001313 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001314 spin_lock_bh(&table->tb6_lock);
David Ahernd4ead6b2018-04-17 17:33:16 -07001315 err = fib6_add(&table->tb6_root, rt, info, extack);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001316 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 return err;
1319}
1320
David Ahern8d1c8022018-04-17 17:33:26 -07001321int ip6_ins_rt(struct net *net, struct fib6_info *rt)
Thomas Graf40e22e82006-08-22 00:00:45 -07001322{
David Ahernafb1d4b52018-04-17 17:33:11 -07001323 struct nl_info info = { .nl_net = net, };
Florian Westphale715b6d2015-01-05 23:57:44 +01001324
David Ahernd4ead6b2018-04-17 17:33:16 -07001325 return __ip6_ins_rt(rt, &info, NULL);
Thomas Graf40e22e82006-08-22 00:00:45 -07001326}
1327
David Ahern85bd05d2019-04-16 14:36:01 -07001328static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001329 const struct in6_addr *daddr,
1330 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
David Ahern85bd05d2019-04-16 14:36:01 -07001332 struct fib6_info *f6i = res->f6i;
David Ahern4832c302017-08-17 12:17:20 -07001333 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 struct rt6_info *rt;
1335
1336 /*
1337 * Clone the route.
1338 */
1339
David Ahern85bd05d2019-04-16 14:36:01 -07001340 if (!fib6_info_hold_safe(f6i))
Wei Wange873e4b2018-07-21 20:56:32 -07001341 return NULL;
1342
David Ahern0d161582019-04-16 14:36:04 -07001343 dev = ip6_rt_get_dev_rcu(res);
David Ahern93531c62018-04-17 17:33:25 -07001344 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
Wei Wange873e4b2018-07-21 20:56:32 -07001345 if (!rt) {
David Ahern85bd05d2019-04-16 14:36:01 -07001346 fib6_info_release(f6i);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001347 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001348 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001349
David Ahern0d161582019-04-16 14:36:04 -07001350 ip6_rt_copy_init(rt, res);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001351 rt->rt6i_flags |= RTF_CACHE;
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001352 rt->dst.flags |= DST_HOST;
1353 rt->rt6i_dst.addr = *daddr;
1354 rt->rt6i_dst.plen = 128;
1355
David Ahern85bd05d2019-04-16 14:36:01 -07001356 if (!rt6_is_gw_or_nonexthop(res)) {
1357 if (f6i->fib6_dst.plen != 128 &&
1358 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001359 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001361 if (rt->rt6i_src.plen && saddr) {
1362 rt->rt6i_src.addr = *saddr;
1363 rt->rt6i_src.plen = 128;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001364 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001365#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001368 return rt;
1369}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
David Aherndb3fede2019-04-16 14:36:03 -07001371static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001372{
David Aherndb3fede2019-04-16 14:36:03 -07001373 struct fib6_info *f6i = res->f6i;
1374 unsigned short flags = fib6_info_dst_flags(f6i);
David Ahern4832c302017-08-17 12:17:20 -07001375 struct net_device *dev;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001376 struct rt6_info *pcpu_rt;
1377
David Aherndb3fede2019-04-16 14:36:03 -07001378 if (!fib6_info_hold_safe(f6i))
Wei Wange873e4b2018-07-21 20:56:32 -07001379 return NULL;
1380
David Ahern4832c302017-08-17 12:17:20 -07001381 rcu_read_lock();
David Ahern0d161582019-04-16 14:36:04 -07001382 dev = ip6_rt_get_dev_rcu(res);
David Ahern93531c62018-04-17 17:33:25 -07001383 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
David Ahern4832c302017-08-17 12:17:20 -07001384 rcu_read_unlock();
Wei Wange873e4b2018-07-21 20:56:32 -07001385 if (!pcpu_rt) {
David Aherndb3fede2019-04-16 14:36:03 -07001386 fib6_info_release(f6i);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001387 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001388 }
David Ahern0d161582019-04-16 14:36:04 -07001389 ip6_rt_copy_init(pcpu_rt, res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001390 pcpu_rt->rt6i_flags |= RTF_PCPU;
1391 return pcpu_rt;
1392}
1393
Wei Wang66f5d6c2017-10-06 12:06:10 -07001394/* It should be called with rcu_read_lock() acquired */
David Aherndb3fede2019-04-16 14:36:03 -07001395static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001396{
Eric Dumazetc3530712019-05-31 18:11:25 -07001397 struct rt6_info *pcpu_rt;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001398
Eric Dumazetc3530712019-05-31 18:11:25 -07001399 pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001400
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001401 return pcpu_rt;
1402}
1403
David Ahernafb1d4b52018-04-17 17:33:11 -07001404static struct rt6_info *rt6_make_pcpu_route(struct net *net,
David Aherndb3fede2019-04-16 14:36:03 -07001405 const struct fib6_result *res)
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001406{
1407 struct rt6_info *pcpu_rt, *prev, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001408
David Aherndb3fede2019-04-16 14:36:03 -07001409 pcpu_rt = ip6_rt_pcpu_alloc(res);
Wei Wang0e09edc2019-06-20 17:36:37 -07001410 if (!pcpu_rt)
1411 return NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001412
David Ahernf40b6ae2019-05-22 20:27:55 -07001413 p = this_cpu_ptr(res->nh->rt6i_pcpu);
Wei Wanga94b9362017-10-06 12:06:04 -07001414 prev = cmpxchg(p, NULL, pcpu_rt);
Eric Dumazet951f7882017-10-08 21:07:18 -07001415 BUG_ON(prev);
Wei Wanga94b9362017-10-06 12:06:04 -07001416
Eric Dumazet61fb0d02019-05-15 19:39:52 -07001417 if (res->f6i->fib6_destroying) {
1418 struct fib6_info *from;
1419
1420 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1421 fib6_info_release(from);
1422 }
1423
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001424 return pcpu_rt;
1425}
1426
Wei Wang35732d02017-10-06 12:05:57 -07001427/* exception hash table implementation
1428 */
1429static DEFINE_SPINLOCK(rt6_exception_lock);
1430
1431/* Remove rt6_ex from hash table and free the memory
1432 * Caller must hold rt6_exception_lock
1433 */
1434static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1435 struct rt6_exception *rt6_ex)
1436{
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001437 struct fib6_info *from;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001438 struct net *net;
Wei Wang81eb8442017-10-06 12:06:11 -07001439
Wei Wang35732d02017-10-06 12:05:57 -07001440 if (!bucket || !rt6_ex)
1441 return;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001442
1443 net = dev_net(rt6_ex->rt6i->dst.dev);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001444 net->ipv6.rt6_stats->fib_rt_cache--;
1445
1446 /* purge completely the exception to allow releasing the held resources:
1447 * some [sk] cache may keep the dst around for unlimited time
1448 */
Eric Dumazet0e233872019-04-28 12:22:25 -07001449 from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001450 fib6_info_release(from);
1451 dst_dev_put(&rt6_ex->rt6i->dst);
1452
Wei Wang35732d02017-10-06 12:05:57 -07001453 hlist_del_rcu(&rt6_ex->hlist);
David Ahern77634cc2018-04-17 17:33:27 -07001454 dst_release(&rt6_ex->rt6i->dst);
Wei Wang35732d02017-10-06 12:05:57 -07001455 kfree_rcu(rt6_ex, rcu);
1456 WARN_ON_ONCE(!bucket->depth);
1457 bucket->depth--;
1458}
1459
1460/* Remove oldest rt6_ex in bucket and free the memory
1461 * Caller must hold rt6_exception_lock
1462 */
1463static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1464{
1465 struct rt6_exception *rt6_ex, *oldest = NULL;
1466
1467 if (!bucket)
1468 return;
1469
1470 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1471 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1472 oldest = rt6_ex;
1473 }
1474 rt6_remove_exception(bucket, oldest);
1475}
1476
1477static u32 rt6_exception_hash(const struct in6_addr *dst,
1478 const struct in6_addr *src)
1479{
1480 static u32 seed __read_mostly;
1481 u32 val;
1482
1483 net_get_random_once(&seed, sizeof(seed));
Eric Dumazetb6b556a2019-11-03 18:24:16 -08001484 val = jhash2((const u32 *)dst, sizeof(*dst)/sizeof(u32), seed);
Wei Wang35732d02017-10-06 12:05:57 -07001485
1486#ifdef CONFIG_IPV6_SUBTREES
1487 if (src)
Eric Dumazetb6b556a2019-11-03 18:24:16 -08001488 val = jhash2((const u32 *)src, sizeof(*src)/sizeof(u32), val);
Wei Wang35732d02017-10-06 12:05:57 -07001489#endif
1490 return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1491}
1492
1493/* Helper function to find the cached rt in the hash table
1494 * and update bucket pointer to point to the bucket for this
1495 * (daddr, saddr) pair
1496 * Caller must hold rt6_exception_lock
1497 */
1498static struct rt6_exception *
1499__rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1500 const struct in6_addr *daddr,
1501 const struct in6_addr *saddr)
1502{
1503 struct rt6_exception *rt6_ex;
1504 u32 hval;
1505
1506 if (!(*bucket) || !daddr)
1507 return NULL;
1508
1509 hval = rt6_exception_hash(daddr, saddr);
1510 *bucket += hval;
1511
1512 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1513 struct rt6_info *rt6 = rt6_ex->rt6i;
1514 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1515
1516#ifdef CONFIG_IPV6_SUBTREES
1517 if (matched && saddr)
1518 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1519#endif
1520 if (matched)
1521 return rt6_ex;
1522 }
1523 return NULL;
1524}
1525
1526/* Helper function to find the cached rt in the hash table
1527 * and update bucket pointer to point to the bucket for this
1528 * (daddr, saddr) pair
1529 * Caller must hold rcu_read_lock()
1530 */
1531static struct rt6_exception *
1532__rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1533 const struct in6_addr *daddr,
1534 const struct in6_addr *saddr)
1535{
1536 struct rt6_exception *rt6_ex;
1537 u32 hval;
1538
1539 WARN_ON_ONCE(!rcu_read_lock_held());
1540
1541 if (!(*bucket) || !daddr)
1542 return NULL;
1543
1544 hval = rt6_exception_hash(daddr, saddr);
1545 *bucket += hval;
1546
1547 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1548 struct rt6_info *rt6 = rt6_ex->rt6i;
1549 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1550
1551#ifdef CONFIG_IPV6_SUBTREES
1552 if (matched && saddr)
1553 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1554#endif
1555 if (matched)
1556 return rt6_ex;
1557 }
1558 return NULL;
1559}
1560
David Ahernb748f262019-04-16 14:36:06 -07001561static unsigned int fib6_mtu(const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001562{
David Ahernb748f262019-04-16 14:36:06 -07001563 const struct fib6_nh *nh = res->nh;
David Ahernd4ead6b2018-04-17 17:33:16 -07001564 unsigned int mtu;
1565
David Ahernb748f262019-04-16 14:36:06 -07001566 if (res->f6i->fib6_pmtu) {
1567 mtu = res->f6i->fib6_pmtu;
David Aherndcd1f572018-04-18 15:39:05 -07001568 } else {
David Ahernb748f262019-04-16 14:36:06 -07001569 struct net_device *dev = nh->fib_nh_dev;
David Aherndcd1f572018-04-18 15:39:05 -07001570 struct inet6_dev *idev;
1571
1572 rcu_read_lock();
1573 idev = __in6_dev_get(dev);
1574 mtu = idev->cnf.mtu6;
1575 rcu_read_unlock();
1576 }
1577
David Ahernd4ead6b2018-04-17 17:33:16 -07001578 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1579
David Ahernb748f262019-04-16 14:36:06 -07001580 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahernd4ead6b2018-04-17 17:33:16 -07001581}
1582
David Aherncc5c0732019-05-22 20:27:58 -07001583#define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1584
1585/* used when the flushed bit is not relevant, only access to the bucket
1586 * (ie., all bucket users except rt6_insert_exception);
1587 *
1588 * called under rcu lock; sometimes called with rt6_exception_lock held
1589 */
1590static
1591struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1592 spinlock_t *lock)
1593{
1594 struct rt6_exception_bucket *bucket;
1595
1596 if (lock)
1597 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1598 lockdep_is_held(lock));
1599 else
1600 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1601
1602 /* remove bucket flushed bit if set */
1603 if (bucket) {
1604 unsigned long p = (unsigned long)bucket;
1605
1606 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1607 bucket = (struct rt6_exception_bucket *)p;
1608 }
1609
1610 return bucket;
1611}
1612
1613static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1614{
1615 unsigned long p = (unsigned long)bucket;
1616
1617 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1618}
1619
1620/* called with rt6_exception_lock held */
1621static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1622 spinlock_t *lock)
1623{
1624 struct rt6_exception_bucket *bucket;
1625 unsigned long p;
1626
1627 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1628 lockdep_is_held(lock));
1629
1630 p = (unsigned long)bucket;
1631 p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1632 bucket = (struct rt6_exception_bucket *)p;
1633 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1634}
1635
Wei Wang35732d02017-10-06 12:05:57 -07001636static int rt6_insert_exception(struct rt6_info *nrt,
David Ahern5012f0a2019-04-16 14:36:05 -07001637 const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001638{
David Ahern5e670d82018-04-17 17:33:14 -07001639 struct net *net = dev_net(nrt->dst.dev);
Wei Wang35732d02017-10-06 12:05:57 -07001640 struct rt6_exception_bucket *bucket;
David Aherncc5c0732019-05-22 20:27:58 -07001641 struct fib6_info *f6i = res->f6i;
Wei Wang35732d02017-10-06 12:05:57 -07001642 struct in6_addr *src_key = NULL;
1643 struct rt6_exception *rt6_ex;
David Aherncc5c0732019-05-22 20:27:58 -07001644 struct fib6_nh *nh = res->nh;
Wei Wang35732d02017-10-06 12:05:57 -07001645 int err = 0;
1646
Wei Wang35732d02017-10-06 12:05:57 -07001647 spin_lock_bh(&rt6_exception_lock);
1648
David Aherncc5c0732019-05-22 20:27:58 -07001649 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1650 lockdep_is_held(&rt6_exception_lock));
Wei Wang35732d02017-10-06 12:05:57 -07001651 if (!bucket) {
1652 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1653 GFP_ATOMIC);
1654 if (!bucket) {
1655 err = -ENOMEM;
1656 goto out;
1657 }
David Aherncc5c0732019-05-22 20:27:58 -07001658 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1659 } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1660 err = -EINVAL;
1661 goto out;
Wei Wang35732d02017-10-06 12:05:57 -07001662 }
1663
1664#ifdef CONFIG_IPV6_SUBTREES
David Ahern5012f0a2019-04-16 14:36:05 -07001665 /* fib6_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001666 * and exception table is indexed by a hash of
David Ahern5012f0a2019-04-16 14:36:05 -07001667 * both fib6_dst and fib6_src.
Wei Wang35732d02017-10-06 12:05:57 -07001668 * Otherwise, the exception table is indexed by
David Ahern5012f0a2019-04-16 14:36:05 -07001669 * a hash of only fib6_dst.
Wei Wang35732d02017-10-06 12:05:57 -07001670 */
David Ahern5012f0a2019-04-16 14:36:05 -07001671 if (f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001672 src_key = &nrt->rt6i_src.addr;
1673#endif
David Ahern5012f0a2019-04-16 14:36:05 -07001674 /* rt6_mtu_change() might lower mtu on f6i.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001675 * Only insert this exception route if its mtu
David Ahern5012f0a2019-04-16 14:36:05 -07001676 * is less than f6i's mtu value.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001677 */
David Ahernb748f262019-04-16 14:36:06 -07001678 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001679 err = -EINVAL;
1680 goto out;
1681 }
Wei Wang60006a42017-10-06 12:05:58 -07001682
Wei Wang35732d02017-10-06 12:05:57 -07001683 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1684 src_key);
1685 if (rt6_ex)
1686 rt6_remove_exception(bucket, rt6_ex);
1687
1688 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1689 if (!rt6_ex) {
1690 err = -ENOMEM;
1691 goto out;
1692 }
1693 rt6_ex->rt6i = nrt;
1694 rt6_ex->stamp = jiffies;
Wei Wang35732d02017-10-06 12:05:57 -07001695 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1696 bucket->depth++;
Wei Wang81eb8442017-10-06 12:06:11 -07001697 net->ipv6.rt6_stats->fib_rt_cache++;
Wei Wang35732d02017-10-06 12:05:57 -07001698
1699 if (bucket->depth > FIB6_MAX_DEPTH)
1700 rt6_exception_remove_oldest(bucket);
1701
1702out:
1703 spin_unlock_bh(&rt6_exception_lock);
1704
1705 /* Update fn->fn_sernum to invalidate all cached dst */
Paolo Abenib886d5f2017-10-19 16:07:10 +02001706 if (!err) {
David Ahern5012f0a2019-04-16 14:36:05 -07001707 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1708 fib6_update_sernum(net, f6i);
1709 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
Paolo Abenib886d5f2017-10-19 16:07:10 +02001710 fib6_force_start_gc(net);
1711 }
Wei Wang35732d02017-10-06 12:05:57 -07001712
1713 return err;
1714}
1715
David Ahernc0b220c2019-05-22 20:27:57 -07001716static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
Wei Wang35732d02017-10-06 12:05:57 -07001717{
1718 struct rt6_exception_bucket *bucket;
1719 struct rt6_exception *rt6_ex;
1720 struct hlist_node *tmp;
1721 int i;
1722
1723 spin_lock_bh(&rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001724
David Aherncc5c0732019-05-22 20:27:58 -07001725 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001726 if (!bucket)
1727 goto out;
1728
David Aherncc5c0732019-05-22 20:27:58 -07001729 /* Prevent rt6_insert_exception() to recreate the bucket list */
1730 if (!from)
1731 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1732
Wei Wang35732d02017-10-06 12:05:57 -07001733 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
David Aherncc5c0732019-05-22 20:27:58 -07001734 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1735 if (!from ||
1736 rcu_access_pointer(rt6_ex->rt6i->from) == from)
1737 rt6_remove_exception(bucket, rt6_ex);
1738 }
1739 WARN_ON_ONCE(!from && bucket->depth);
Wei Wang35732d02017-10-06 12:05:57 -07001740 bucket++;
1741 }
Wei Wang35732d02017-10-06 12:05:57 -07001742out:
1743 spin_unlock_bh(&rt6_exception_lock);
1744}
1745
David Aherne659ba32019-06-08 14:53:28 -07001746static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1747{
1748 struct fib6_info *f6i = arg;
1749
1750 fib6_nh_flush_exceptions(nh, f6i);
1751
1752 return 0;
1753}
1754
David Ahernc0b220c2019-05-22 20:27:57 -07001755void rt6_flush_exceptions(struct fib6_info *f6i)
1756{
David Aherne659ba32019-06-08 14:53:28 -07001757 if (f6i->nh)
1758 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1759 f6i);
1760 else
1761 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
David Ahernc0b220c2019-05-22 20:27:57 -07001762}
1763
Wei Wang35732d02017-10-06 12:05:57 -07001764/* Find cached rt in the hash table inside passed in rt
1765 * Caller has to hold rcu_read_lock()
1766 */
David Ahern7e4b5122019-04-16 14:36:00 -07001767static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
Wei Wang510e2ce2019-05-16 13:30:54 -07001768 const struct in6_addr *daddr,
1769 const struct in6_addr *saddr)
Wei Wang35732d02017-10-06 12:05:57 -07001770{
Wei Wang510e2ce2019-05-16 13:30:54 -07001771 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001772 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001773 struct rt6_exception *rt6_ex;
David Ahern7e4b5122019-04-16 14:36:00 -07001774 struct rt6_info *ret = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001775
Wei Wang35732d02017-10-06 12:05:57 -07001776#ifdef CONFIG_IPV6_SUBTREES
David Ahern7e4b5122019-04-16 14:36:00 -07001777 /* fib6i_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001778 * and exception table is indexed by a hash of
David Ahern7e4b5122019-04-16 14:36:00 -07001779 * both fib6_dst and fib6_src.
Wei Wang510e2ce2019-05-16 13:30:54 -07001780 * However, the src addr used to create the hash
1781 * might not be exactly the passed in saddr which
1782 * is a /128 addr from the flow.
1783 * So we need to use f6i->fib6_src to redo lookup
1784 * if the passed in saddr does not find anything.
1785 * (See the logic in ip6_rt_cache_alloc() on how
1786 * rt->rt6i_src is updated.)
Wei Wang35732d02017-10-06 12:05:57 -07001787 */
David Ahern7e4b5122019-04-16 14:36:00 -07001788 if (res->f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001789 src_key = saddr;
Wei Wang510e2ce2019-05-16 13:30:54 -07001790find_ex:
Wei Wang35732d02017-10-06 12:05:57 -07001791#endif
David Aherncc5c0732019-05-22 20:27:58 -07001792 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001793 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1794
1795 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
David Ahern7e4b5122019-04-16 14:36:00 -07001796 ret = rt6_ex->rt6i;
Wei Wang35732d02017-10-06 12:05:57 -07001797
Wei Wang510e2ce2019-05-16 13:30:54 -07001798#ifdef CONFIG_IPV6_SUBTREES
1799 /* Use fib6_src as src_key and redo lookup */
1800 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1801 src_key = &res->f6i->fib6_src.addr;
1802 goto find_ex;
1803 }
1804#endif
1805
David Ahern7e4b5122019-04-16 14:36:00 -07001806 return ret;
Wei Wang35732d02017-10-06 12:05:57 -07001807}
1808
1809/* Remove the passed in cached rt from the hash table that contains it */
David Aherncc5c0732019-05-22 20:27:58 -07001810static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001811 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001812{
David Ahernc0b220c2019-05-22 20:27:57 -07001813 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001814 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001815 struct rt6_exception *rt6_ex;
1816 int err;
1817
David Aherncc5c0732019-05-22 20:27:58 -07001818 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wang35732d02017-10-06 12:05:57 -07001819 return -ENOENT;
1820
1821 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07001822 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1823
Wei Wang35732d02017-10-06 12:05:57 -07001824#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001825 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1826 * and exception table is indexed by a hash of
1827 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001828 * Otherwise, the exception table is indexed by
1829 * a hash of only rt6i_dst.
1830 */
David Ahernc0b220c2019-05-22 20:27:57 -07001831 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001832 src_key = &rt->rt6i_src.addr;
1833#endif
1834 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1835 &rt->rt6i_dst.addr,
1836 src_key);
1837 if (rt6_ex) {
1838 rt6_remove_exception(bucket, rt6_ex);
1839 err = 0;
1840 } else {
1841 err = -ENOENT;
1842 }
1843
1844 spin_unlock_bh(&rt6_exception_lock);
1845 return err;
1846}
1847
David Aherne659ba32019-06-08 14:53:28 -07001848struct fib6_nh_excptn_arg {
1849 struct rt6_info *rt;
1850 int plen;
1851};
1852
1853static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1854{
1855 struct fib6_nh_excptn_arg *arg = _arg;
1856 int err;
1857
1858 err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1859 if (err == 0)
1860 return 1;
1861
1862 return 0;
1863}
1864
David Ahernc0b220c2019-05-22 20:27:57 -07001865static int rt6_remove_exception_rt(struct rt6_info *rt)
1866{
1867 struct fib6_info *from;
1868
1869 from = rcu_dereference(rt->from);
David Aherncc5c0732019-05-22 20:27:58 -07001870 if (!from || !(rt->rt6i_flags & RTF_CACHE))
David Ahernc0b220c2019-05-22 20:27:57 -07001871 return -EINVAL;
1872
David Aherne659ba32019-06-08 14:53:28 -07001873 if (from->nh) {
1874 struct fib6_nh_excptn_arg arg = {
1875 .rt = rt,
1876 .plen = from->fib6_src.plen
1877 };
1878 int rc;
1879
1880 /* rc = 1 means an entry was found */
1881 rc = nexthop_for_each_fib6_nh(from->nh,
1882 rt6_nh_remove_exception_rt,
1883 &arg);
1884 return rc ? 0 : -ENOENT;
1885 }
1886
David Ahern1cf844c2019-05-22 20:27:59 -07001887 return fib6_nh_remove_exception(from->fib6_nh,
David Aherncc5c0732019-05-22 20:27:58 -07001888 from->fib6_src.plen, rt);
David Ahernc0b220c2019-05-22 20:27:57 -07001889}
1890
Wei Wang35732d02017-10-06 12:05:57 -07001891/* Find rt6_ex which contains the passed in rt cache and
1892 * refresh its stamp
1893 */
David Aherncc5c0732019-05-22 20:27:58 -07001894static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001895 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001896{
David Ahernc0b220c2019-05-22 20:27:57 -07001897 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001898 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001899 struct rt6_exception *rt6_ex;
Paolo Abeni193f3682019-02-21 11:19:41 +01001900
David Aherncc5c0732019-05-22 20:27:58 -07001901 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001902#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001903 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1904 * and exception table is indexed by a hash of
1905 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001906 * Otherwise, the exception table is indexed by
1907 * a hash of only rt6i_dst.
1908 */
David Ahernc0b220c2019-05-22 20:27:57 -07001909 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001910 src_key = &rt->rt6i_src.addr;
1911#endif
David Aherncc5c0732019-05-22 20:27:58 -07001912 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
Wei Wang35732d02017-10-06 12:05:57 -07001913 if (rt6_ex)
1914 rt6_ex->stamp = jiffies;
David Ahernc0b220c2019-05-22 20:27:57 -07001915}
Wei Wang35732d02017-10-06 12:05:57 -07001916
David Aherne659ba32019-06-08 14:53:28 -07001917struct fib6_nh_match_arg {
1918 const struct net_device *dev;
1919 const struct in6_addr *gw;
1920 struct fib6_nh *match;
1921};
1922
1923/* determine if fib6_nh has given device and gateway */
1924static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1925{
1926 struct fib6_nh_match_arg *arg = _arg;
1927
1928 if (arg->dev != nh->fib_nh_dev ||
1929 (arg->gw && !nh->fib_nh_gw_family) ||
1930 (!arg->gw && nh->fib_nh_gw_family) ||
1931 (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1932 return 0;
1933
1934 arg->match = nh;
1935
1936 /* found a match, break the loop */
1937 return 1;
1938}
1939
David Ahernc0b220c2019-05-22 20:27:57 -07001940static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1941{
1942 struct fib6_info *from;
David Aherne659ba32019-06-08 14:53:28 -07001943 struct fib6_nh *fib6_nh;
David Ahernc0b220c2019-05-22 20:27:57 -07001944
1945 rcu_read_lock();
1946
1947 from = rcu_dereference(rt->from);
1948 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1949 goto unlock;
1950
David Aherne659ba32019-06-08 14:53:28 -07001951 if (from->nh) {
1952 struct fib6_nh_match_arg arg = {
1953 .dev = rt->dst.dev,
1954 .gw = &rt->rt6i_gateway,
1955 };
1956
1957 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1958
1959 if (!arg.match)
David Aherncff6a322019-08-01 14:36:35 -07001960 goto unlock;
David Aherne659ba32019-06-08 14:53:28 -07001961 fib6_nh = arg.match;
1962 } else {
1963 fib6_nh = from->fib6_nh;
1964 }
1965 fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
Paolo Abeni193f3682019-02-21 11:19:41 +01001966unlock:
Wei Wang35732d02017-10-06 12:05:57 -07001967 rcu_read_unlock();
1968}
1969
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001970static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1971 struct rt6_info *rt, int mtu)
1972{
1973 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1974 * lowest MTU in the path: always allow updating the route PMTU to
1975 * reflect PMTU decreases.
1976 *
1977 * If the new MTU is higher, and the route PMTU is equal to the local
1978 * MTU, this means the old MTU is the lowest in the path, so allow
1979 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1980 * handle this.
1981 */
1982
1983 if (dst_mtu(&rt->dst) >= mtu)
1984 return true;
1985
1986 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1987 return true;
1988
1989 return false;
1990}
1991
1992static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
David Aherncc5c0732019-05-22 20:27:58 -07001993 const struct fib6_nh *nh, int mtu)
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001994{
1995 struct rt6_exception_bucket *bucket;
1996 struct rt6_exception *rt6_ex;
1997 int i;
1998
David Aherncc5c0732019-05-22 20:27:58 -07001999 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002000 if (!bucket)
2001 return;
2002
2003 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2004 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2005 struct rt6_info *entry = rt6_ex->rt6i;
2006
2007 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
David Ahernd4ead6b2018-04-17 17:33:16 -07002008 * route), the metrics of its rt->from have already
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002009 * been updated.
2010 */
David Ahernd4ead6b2018-04-17 17:33:16 -07002011 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002012 rt6_mtu_change_route_allowed(idev, entry, mtu))
David Ahernd4ead6b2018-04-17 17:33:16 -07002013 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002014 }
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002015 bucket++;
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002016 }
2017}
2018
Wei Wangb16cb452017-10-06 12:06:00 -07002019#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2020
David Aherncc5c0732019-05-22 20:27:58 -07002021static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2022 const struct in6_addr *gateway)
Wei Wangb16cb452017-10-06 12:06:00 -07002023{
2024 struct rt6_exception_bucket *bucket;
2025 struct rt6_exception *rt6_ex;
2026 struct hlist_node *tmp;
2027 int i;
2028
David Aherncc5c0732019-05-22 20:27:58 -07002029 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangb16cb452017-10-06 12:06:00 -07002030 return;
2031
2032 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07002033 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangb16cb452017-10-06 12:06:00 -07002034 if (bucket) {
2035 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2036 hlist_for_each_entry_safe(rt6_ex, tmp,
2037 &bucket->chain, hlist) {
2038 struct rt6_info *entry = rt6_ex->rt6i;
2039
2040 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2041 RTF_CACHE_GATEWAY &&
2042 ipv6_addr_equal(gateway,
2043 &entry->rt6i_gateway)) {
2044 rt6_remove_exception(bucket, rt6_ex);
2045 }
2046 }
2047 bucket++;
2048 }
2049 }
2050
2051 spin_unlock_bh(&rt6_exception_lock);
2052}
2053
Wei Wangc757faa2017-10-06 12:06:01 -07002054static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2055 struct rt6_exception *rt6_ex,
2056 struct fib6_gc_args *gc_args,
2057 unsigned long now)
2058{
2059 struct rt6_info *rt = rt6_ex->rt6i;
2060
Paolo Abeni1859bac2017-10-19 16:07:11 +02002061 /* we are pruning and obsoleting aged-out and non gateway exceptions
2062 * even if others have still references to them, so that on next
2063 * dst_check() such references can be dropped.
2064 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2065 * expired, independently from their aging, as per RFC 8201 section 4
2066 */
Wei Wang31afeb42018-01-26 11:40:17 -08002067 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2068 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2069 RT6_TRACE("aging clone %p\n", rt);
2070 rt6_remove_exception(bucket, rt6_ex);
2071 return;
2072 }
2073 } else if (time_after(jiffies, rt->dst.expires)) {
2074 RT6_TRACE("purging expired route %p\n", rt);
Wei Wangc757faa2017-10-06 12:06:01 -07002075 rt6_remove_exception(bucket, rt6_ex);
2076 return;
Wei Wang31afeb42018-01-26 11:40:17 -08002077 }
2078
2079 if (rt->rt6i_flags & RTF_GATEWAY) {
Wei Wangc757faa2017-10-06 12:06:01 -07002080 struct neighbour *neigh;
2081 __u8 neigh_flags = 0;
2082
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002083 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
2084 if (neigh)
Wei Wangc757faa2017-10-06 12:06:01 -07002085 neigh_flags = neigh->flags;
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002086
Wei Wangc757faa2017-10-06 12:06:01 -07002087 if (!(neigh_flags & NTF_ROUTER)) {
2088 RT6_TRACE("purging route %p via non-router but gateway\n",
2089 rt);
2090 rt6_remove_exception(bucket, rt6_ex);
2091 return;
2092 }
2093 }
Wei Wang31afeb42018-01-26 11:40:17 -08002094
Wei Wangc757faa2017-10-06 12:06:01 -07002095 gc_args->more++;
2096}
2097
David Aherncc5c0732019-05-22 20:27:58 -07002098static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
David Ahernc0b220c2019-05-22 20:27:57 -07002099 struct fib6_gc_args *gc_args,
2100 unsigned long now)
Wei Wangc757faa2017-10-06 12:06:01 -07002101{
2102 struct rt6_exception_bucket *bucket;
2103 struct rt6_exception *rt6_ex;
2104 struct hlist_node *tmp;
2105 int i;
2106
David Aherncc5c0732019-05-22 20:27:58 -07002107 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangc757faa2017-10-06 12:06:01 -07002108 return;
2109
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002110 rcu_read_lock_bh();
2111 spin_lock(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07002112 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangc757faa2017-10-06 12:06:01 -07002113 if (bucket) {
2114 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2115 hlist_for_each_entry_safe(rt6_ex, tmp,
2116 &bucket->chain, hlist) {
2117 rt6_age_examine_exception(bucket, rt6_ex,
2118 gc_args, now);
2119 }
2120 bucket++;
2121 }
2122 }
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002123 spin_unlock(&rt6_exception_lock);
2124 rcu_read_unlock_bh();
Wei Wangc757faa2017-10-06 12:06:01 -07002125}
2126
David Aherne659ba32019-06-08 14:53:28 -07002127struct fib6_nh_age_excptn_arg {
2128 struct fib6_gc_args *gc_args;
2129 unsigned long now;
2130};
2131
2132static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2133{
2134 struct fib6_nh_age_excptn_arg *arg = _arg;
2135
2136 fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2137 return 0;
2138}
2139
David Aherncc5c0732019-05-22 20:27:58 -07002140void rt6_age_exceptions(struct fib6_info *f6i,
David Ahernc0b220c2019-05-22 20:27:57 -07002141 struct fib6_gc_args *gc_args,
2142 unsigned long now)
2143{
David Aherne659ba32019-06-08 14:53:28 -07002144 if (f6i->nh) {
2145 struct fib6_nh_age_excptn_arg arg = {
2146 .gc_args = gc_args,
2147 .now = now
2148 };
2149
2150 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2151 &arg);
2152 } else {
2153 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2154 }
David Ahernc0b220c2019-05-22 20:27:57 -07002155}
2156
David Ahern1d053da2018-05-09 20:34:21 -07002157/* must be called with rcu lock held */
David Aherneffda4d2019-04-16 14:36:10 -07002158int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2159 struct flowi6 *fl6, struct fib6_result *res, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002161 struct fib6_node *fn, *saved_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
David Ahern64547432018-05-09 20:34:19 -07002163 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002164 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
David Ahernca254492015-10-12 11:47:10 -07002166 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2167 oif = 0;
2168
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002169redo_rt6_select:
David Aherneffda4d2019-04-16 14:36:10 -07002170 rt6_select(net, fn, oif, res, strict);
2171 if (res->f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002172 fn = fib6_backtrack(fn, &fl6->saddr);
2173 if (fn)
2174 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002175 else if (strict & RT6_LOOKUP_F_REACHABLE) {
2176 /* also consider unreachable route */
2177 strict &= ~RT6_LOOKUP_F_REACHABLE;
2178 fn = saved_fn;
2179 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002180 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002181 }
2182
David Aherneffda4d2019-04-16 14:36:10 -07002183 trace_fib6_table_lookup(net, res, table, fl6);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -08002184
David Aherneffda4d2019-04-16 14:36:10 -07002185 return 0;
David Ahern1d053da2018-05-09 20:34:21 -07002186}
2187
2188struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2189 int oif, struct flowi6 *fl6,
2190 const struct sk_buff *skb, int flags)
2191{
David Ahernb1d40992019-04-16 14:35:59 -07002192 struct fib6_result res = {};
Wei Wang0e09edc2019-06-20 17:36:37 -07002193 struct rt6_info *rt = NULL;
David Ahern1d053da2018-05-09 20:34:21 -07002194 int strict = 0;
2195
Wei Wang0e09edc2019-06-20 17:36:37 -07002196 WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
2197 !rcu_read_lock_held());
2198
David Ahern1d053da2018-05-09 20:34:21 -07002199 strict |= flags & RT6_LOOKUP_F_IFACE;
2200 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
2201 if (net->ipv6.devconf_all->forwarding == 0)
2202 strict |= RT6_LOOKUP_F_REACHABLE;
2203
2204 rcu_read_lock();
2205
David Aherneffda4d2019-04-16 14:36:10 -07002206 fib6_table_lookup(net, table, oif, fl6, &res, strict);
Wei Wang0e09edc2019-06-20 17:36:37 -07002207 if (res.f6i == net->ipv6.fib6_null_entry)
2208 goto out;
David Ahern23fb93a2018-04-17 17:33:23 -07002209
David Ahernb1d40992019-04-16 14:35:59 -07002210 fib6_select_path(net, &res, fl6, oif, false, skb, strict);
David Ahernd83009d2019-04-09 14:41:17 -07002211
David Ahern23fb93a2018-04-17 17:33:23 -07002212 /*Search through exception table */
David Ahern7e4b5122019-04-16 14:36:00 -07002213 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
David Ahern23fb93a2018-04-17 17:33:23 -07002214 if (rt) {
Wei Wang0e09edc2019-06-20 17:36:37 -07002215 goto out;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002216 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
David Ahernb1d40992019-04-16 14:35:59 -07002217 !res.nh->fib_nh_gw_family)) {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002218 /* Create a RTF_CACHE clone which will not be
2219 * owned by the fib6 tree. It is for the special case where
2220 * the daddr in the skb during the neighbor look-up is different
2221 * from the fl6->daddr used to look-up route here.
2222 */
Wei Wang0e09edc2019-06-20 17:36:37 -07002223 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002224
Wei Wang0e09edc2019-06-20 17:36:37 -07002225 if (rt) {
2226 /* 1 refcnt is taken during ip6_rt_cache_alloc().
2227 * As rt6_uncached_list_add() does not consume refcnt,
2228 * this refcnt is always returned to the caller even
2229 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
Wei Wang1cfb71e2017-06-17 10:42:33 -07002230 */
Wei Wang0e09edc2019-06-20 17:36:37 -07002231 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002232 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Wei Wang0e09edc2019-06-20 17:36:37 -07002233 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08002234
Wei Wang0e09edc2019-06-20 17:36:37 -07002235 return rt;
2236 }
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002237 } else {
2238 /* Get a percpu copy */
Eric Dumazet951f7882017-10-08 21:07:18 -07002239 local_bh_disable();
Wei Wang0e09edc2019-06-20 17:36:37 -07002240 rt = rt6_get_pcpu_route(&res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002241
Wei Wang0e09edc2019-06-20 17:36:37 -07002242 if (!rt)
2243 rt = rt6_make_pcpu_route(net, &res);
David Ahern93531c62018-04-17 17:33:25 -07002244
Eric Dumazet951f7882017-10-08 21:07:18 -07002245 local_bh_enable();
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002246 }
Wei Wang0e09edc2019-06-20 17:36:37 -07002247out:
2248 if (!rt)
2249 rt = net->ipv6.ip6_null_entry;
2250 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
2251 ip6_hold_safe(net, &rt);
2252 rcu_read_unlock();
2253
2254 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002255}
David Ahern9ff74382016-06-13 13:44:19 -07002256EXPORT_SYMBOL_GPL(ip6_pol_route);
Thomas Grafc71099a2006-08-04 23:20:06 -07002257
David Ahernb75cc8f2018-03-02 08:32:17 -08002258static struct rt6_info *ip6_pol_route_input(struct net *net,
2259 struct fib6_table *table,
2260 struct flowi6 *fl6,
2261 const struct sk_buff *skb,
2262 int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002263{
David Ahernb75cc8f2018-03-02 08:32:17 -08002264 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002265}
2266
Mahesh Bandeward409b842016-09-16 12:59:08 -07002267struct dst_entry *ip6_route_input_lookup(struct net *net,
2268 struct net_device *dev,
David Ahernb75cc8f2018-03-02 08:32:17 -08002269 struct flowi6 *fl6,
2270 const struct sk_buff *skb,
2271 int flags)
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002272{
2273 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2274 flags |= RT6_LOOKUP_F_IFACE;
2275
David Ahernb75cc8f2018-03-02 08:32:17 -08002276 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002277}
Mahesh Bandeward409b842016-09-16 12:59:08 -07002278EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002279
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002280static void ip6_multipath_l3_keys(const struct sk_buff *skb,
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002281 struct flow_keys *keys,
2282 struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002283{
2284 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2285 const struct ipv6hdr *key_iph = outer_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002286 struct flow_keys *_flkeys = flkeys;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002287 const struct ipv6hdr *inner_iph;
2288 const struct icmp6hdr *icmph;
2289 struct ipv6hdr _inner_iph;
Eric Dumazetcea67a22018-04-29 09:54:59 -07002290 struct icmp6hdr _icmph;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002291
2292 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2293 goto out;
2294
Eric Dumazetcea67a22018-04-29 09:54:59 -07002295 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2296 sizeof(_icmph), &_icmph);
2297 if (!icmph)
2298 goto out;
2299
Matteo Croce54074f12019-11-02 01:12:04 +01002300 if (!icmpv6_is_err(icmph->icmp6_type))
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002301 goto out;
2302
2303 inner_iph = skb_header_pointer(skb,
2304 skb_transport_offset(skb) + sizeof(*icmph),
2305 sizeof(_inner_iph), &_inner_iph);
2306 if (!inner_iph)
2307 goto out;
2308
2309 key_iph = inner_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002310 _flkeys = NULL;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002311out:
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002312 if (_flkeys) {
2313 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2314 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2315 keys->tags.flow_label = _flkeys->tags.flow_label;
2316 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2317 } else {
2318 keys->addrs.v6addrs.src = key_iph->saddr;
2319 keys->addrs.v6addrs.dst = key_iph->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002320 keys->tags.flow_label = ip6_flowlabel(key_iph);
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002321 keys->basic.ip_proto = key_iph->nexthdr;
2322 }
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002323}
2324
2325/* if skb is set it will be used and fl6 can be NULL */
David Ahernb4bac172018-03-02 08:32:18 -08002326u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2327 const struct sk_buff *skb, struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002328{
2329 struct flow_keys hash_keys;
David Ahern9a2a5372018-03-02 08:32:15 -08002330 u32 mhash;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002331
David S. Millerbbfa0472018-03-12 11:09:33 -04002332 switch (ip6_multipath_hash_policy(net)) {
David Ahernb4bac172018-03-02 08:32:18 -08002333 case 0:
2334 memset(&hash_keys, 0, sizeof(hash_keys));
2335 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2336 if (skb) {
2337 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2338 } else {
2339 hash_keys.addrs.v6addrs.src = fl6->saddr;
2340 hash_keys.addrs.v6addrs.dst = fl6->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002341 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
David Ahernb4bac172018-03-02 08:32:18 -08002342 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2343 }
2344 break;
2345 case 1:
2346 if (skb) {
2347 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2348 struct flow_keys keys;
2349
2350 /* short-circuit if we already have L4 hash present */
2351 if (skb->l4_hash)
2352 return skb_get_hash_raw(skb) >> 1;
2353
2354 memset(&hash_keys, 0, sizeof(hash_keys));
2355
2356 if (!flkeys) {
2357 skb_flow_dissect_flow_keys(skb, &keys, flag);
2358 flkeys = &keys;
2359 }
2360 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2361 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2362 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2363 hash_keys.ports.src = flkeys->ports.src;
2364 hash_keys.ports.dst = flkeys->ports.dst;
2365 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2366 } else {
2367 memset(&hash_keys, 0, sizeof(hash_keys));
2368 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2369 hash_keys.addrs.v6addrs.src = fl6->saddr;
2370 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2371 hash_keys.ports.src = fl6->fl6_sport;
2372 hash_keys.ports.dst = fl6->fl6_dport;
2373 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2374 }
2375 break;
Stephen Suryaputrad8f74f02019-07-06 10:55:18 -04002376 case 2:
2377 memset(&hash_keys, 0, sizeof(hash_keys));
2378 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2379 if (skb) {
2380 struct flow_keys keys;
2381
2382 if (!flkeys) {
2383 skb_flow_dissect_flow_keys(skb, &keys, 0);
2384 flkeys = &keys;
2385 }
2386
2387 /* Inner can be v4 or v6 */
2388 if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2389 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2390 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2391 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2392 } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2393 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2394 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2395 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2396 hash_keys.tags.flow_label = flkeys->tags.flow_label;
2397 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2398 } else {
2399 /* Same as case 0 */
2400 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2401 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2402 }
2403 } else {
2404 /* Same as case 0 */
2405 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2406 hash_keys.addrs.v6addrs.src = fl6->saddr;
2407 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2408 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2409 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2410 }
2411 break;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002412 }
David Ahern9a2a5372018-03-02 08:32:15 -08002413 mhash = flow_hash_from_keys(&hash_keys);
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002414
David Ahern9a2a5372018-03-02 08:32:15 -08002415 return mhash >> 1;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002416}
2417
Wei Wang67f415d2019-06-20 17:36:40 -07002418/* Called with rcu held */
Thomas Grafc71099a2006-08-04 23:20:06 -07002419void ip6_route_input(struct sk_buff *skb)
2420{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002421 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002422 struct net *net = dev_net(skb->dev);
Wei Wang67f415d2019-06-20 17:36:40 -07002423 int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
Jiri Benc904af042015-08-20 13:56:31 +02002424 struct ip_tunnel_info *tun_info;
David S. Miller4c9483b2011-03-12 16:22:43 -05002425 struct flowi6 fl6 = {
David Aherne0d56fd2016-09-10 12:09:57 -07002426 .flowi6_iif = skb->dev->ifindex,
David S. Miller4c9483b2011-03-12 16:22:43 -05002427 .daddr = iph->daddr,
2428 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002429 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05002430 .flowi6_mark = skb->mark,
2431 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07002432 };
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002433 struct flow_keys *flkeys = NULL, _flkeys;
Thomas Grafadaa70b2006-10-13 15:01:03 -07002434
Jiri Benc904af042015-08-20 13:56:31 +02002435 tun_info = skb_tunnel_info(skb);
Jiri Benc46fa0622015-08-28 20:48:19 +02002436 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
Jiri Benc904af042015-08-20 13:56:31 +02002437 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002438
2439 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2440 flkeys = &_flkeys;
2441
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002442 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
David Ahernb4bac172018-03-02 08:32:18 -08002443 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
Jiri Benc06e9d042015-08-20 13:56:26 +02002444 skb_dst_drop(skb);
Wei Wang67f415d2019-06-20 17:36:40 -07002445 skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
2446 &fl6, skb, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07002447}
2448
David Ahernb75cc8f2018-03-02 08:32:17 -08002449static struct rt6_info *ip6_pol_route_output(struct net *net,
2450 struct fib6_table *table,
2451 struct flowi6 *fl6,
2452 const struct sk_buff *skb,
2453 int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002454{
David Ahernb75cc8f2018-03-02 08:32:17 -08002455 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07002456}
2457
Wei Wang7d9e5f42019-06-20 17:36:41 -07002458struct dst_entry *ip6_route_output_flags_noref(struct net *net,
2459 const struct sock *sk,
2460 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002461{
David Ahernd46a9d62015-10-21 08:42:22 -07002462 bool any_src;
Thomas Grafc71099a2006-08-04 23:20:06 -07002463
Robert Shearman3ede0bb2018-09-19 13:56:53 +01002464 if (ipv6_addr_type(&fl6->daddr) &
2465 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
David Ahern4c1feac2016-09-10 12:09:56 -07002466 struct dst_entry *dst;
2467
Wei Wang7d9e5f42019-06-20 17:36:41 -07002468 /* This function does not take refcnt on the dst */
David Ahern4c1feac2016-09-10 12:09:56 -07002469 dst = l3mdev_link_scope_lookup(net, fl6);
2470 if (dst)
2471 return dst;
2472 }
David Ahernca254492015-10-12 11:47:10 -07002473
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002474 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00002475
Wei Wang7d9e5f42019-06-20 17:36:41 -07002476 flags |= RT6_LOOKUP_F_DST_NOREF;
David Ahernd46a9d62015-10-21 08:42:22 -07002477 any_src = ipv6_addr_any(&fl6->saddr);
David Ahern741a11d2015-09-28 10:12:13 -07002478 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
David Ahernd46a9d62015-10-21 08:42:22 -07002479 (fl6->flowi6_oif && any_src))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07002480 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07002481
David Ahernd46a9d62015-10-21 08:42:22 -07002482 if (!any_src)
Thomas Grafadaa70b2006-10-13 15:01:03 -07002483 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00002484 else if (sk)
2485 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002486
David Ahernb75cc8f2018-03-02 08:32:17 -08002487 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488}
Wei Wang7d9e5f42019-06-20 17:36:41 -07002489EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref);
2490
2491struct dst_entry *ip6_route_output_flags(struct net *net,
2492 const struct sock *sk,
2493 struct flowi6 *fl6,
2494 int flags)
2495{
2496 struct dst_entry *dst;
2497 struct rt6_info *rt6;
2498
2499 rcu_read_lock();
2500 dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
2501 rt6 = (struct rt6_info *)dst;
2502 /* For dst cached in uncached_list, refcnt is already taken. */
2503 if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) {
2504 dst = &net->ipv6.ip6_null_entry->dst;
2505 dst_hold(dst);
2506 }
2507 rcu_read_unlock();
2508
2509 return dst;
2510}
Paolo Abeni6f21c962016-01-29 12:30:19 +01002511EXPORT_SYMBOL_GPL(ip6_route_output_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
David S. Miller2774c132011-03-01 14:59:04 -08002513struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07002514{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002515 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
Wei Wang1dbe32522017-06-17 10:42:26 -07002516 struct net_device *loopback_dev = net->loopback_dev;
David S. Miller14e50e52007-05-24 18:17:54 -07002517 struct dst_entry *new = NULL;
2518
Wei Wang1dbe32522017-06-17 10:42:26 -07002519 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
Steffen Klassert62cf27e2017-10-09 08:39:43 +02002520 DST_OBSOLETE_DEAD, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07002521 if (rt) {
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002522 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002523 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002524
Changli Gaod8d1f302010-06-10 23:31:35 -07002525 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07002526 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08002527 new->input = dst_discard;
Eric W. Biedermanede20592015-10-07 16:48:47 -05002528 new->output = dst_discard_out;
David S. Miller14e50e52007-05-24 18:17:54 -07002529
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002530 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07002531
Wei Wang1dbe32522017-06-17 10:42:26 -07002532 rt->rt6i_idev = in6_dev_get(loopback_dev);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002533 rt->rt6i_gateway = ort->rt6i_gateway;
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002534 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
David S. Miller14e50e52007-05-24 18:17:54 -07002535
2536 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2537#ifdef CONFIG_IPV6_SUBTREES
2538 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2539#endif
David S. Miller14e50e52007-05-24 18:17:54 -07002540 }
2541
David S. Miller69ead7a2011-03-01 14:45:33 -08002542 dst_release(dst_orig);
2543 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07002544}
David S. Miller14e50e52007-05-24 18:17:54 -07002545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546/*
2547 * Destination cache support functions
2548 */
2549
David Ahern8d1c8022018-04-17 17:33:26 -07002550static bool fib6_check(struct fib6_info *f6i, u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002551{
Steffen Klassert36143642017-08-25 09:05:42 +02002552 u32 rt_cookie = 0;
Wei Wangc5cff852017-08-21 09:47:10 -07002553
David Ahern8ae86972018-04-20 15:38:03 -07002554 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
David Ahern93531c62018-04-17 17:33:25 -07002555 return false;
2556
2557 if (fib6_check_expired(f6i))
2558 return false;
2559
2560 return true;
2561}
2562
David Aherna68886a2018-04-20 15:38:02 -07002563static struct dst_entry *rt6_check(struct rt6_info *rt,
2564 struct fib6_info *from,
2565 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002566{
Wei Wangc5cff852017-08-21 09:47:10 -07002567 u32 rt_cookie = 0;
2568
David Ahern49d05fe2019-07-17 15:08:43 -07002569 if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
David Ahern93531c62018-04-17 17:33:25 -07002570 rt_cookie != cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002571 return NULL;
2572
2573 if (rt6_check_expired(rt))
2574 return NULL;
2575
2576 return &rt->dst;
2577}
2578
David Aherna68886a2018-04-20 15:38:02 -07002579static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2580 struct fib6_info *from,
2581 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002582{
Martin KaFai Lau5973fb12015-11-11 11:51:07 -08002583 if (!__rt6_check_expired(rt) &&
2584 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
David Aherna68886a2018-04-20 15:38:02 -07002585 fib6_check(from, cookie))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002586 return &rt->dst;
2587 else
2588 return NULL;
2589}
2590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2592{
David Aherna87b7dc2018-04-20 15:38:00 -07002593 struct dst_entry *dst_ret;
David Aherna68886a2018-04-20 15:38:02 -07002594 struct fib6_info *from;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 struct rt6_info *rt;
2596
David Aherna87b7dc2018-04-20 15:38:00 -07002597 rt = container_of(dst, struct rt6_info, dst);
2598
2599 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00002601 /* All IPV6 dsts are created with ->obsolete set to the value
2602 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2603 * into this function always.
2604 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02002605
David Aherna68886a2018-04-20 15:38:02 -07002606 from = rcu_dereference(rt->from);
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002607
David Aherna68886a2018-04-20 15:38:02 -07002608 if (from && (rt->rt6i_flags & RTF_PCPU ||
2609 unlikely(!list_empty(&rt->rt6i_uncached))))
2610 dst_ret = rt6_dst_from_check(rt, from, cookie);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002611 else
David Aherna68886a2018-04-20 15:38:02 -07002612 dst_ret = rt6_check(rt, from, cookie);
David Aherna87b7dc2018-04-20 15:38:00 -07002613
2614 rcu_read_unlock();
2615
2616 return dst_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
2619static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2620{
2621 struct rt6_info *rt = (struct rt6_info *) dst;
2622
2623 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002624 if (rt->rt6i_flags & RTF_CACHE) {
David Ahernc3c14da2018-04-23 11:32:06 -07002625 rcu_read_lock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002626 if (rt6_check_expired(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07002627 rt6_remove_exception_rt(rt);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002628 dst = NULL;
2629 }
David Ahernc3c14da2018-04-23 11:32:06 -07002630 rcu_read_unlock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002631 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002633 dst = NULL;
2634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002636 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637}
2638
2639static void ip6_link_failure(struct sk_buff *skb)
2640{
2641 struct rt6_info *rt;
2642
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002643 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Eric Dumazetadf30902009-06-02 05:19:30 +00002645 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 if (rt) {
David Ahern8a14e462018-04-23 11:32:07 -07002647 rcu_read_lock();
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002648 if (rt->rt6i_flags & RTF_CACHE) {
Xin Long761f6022018-11-14 00:48:28 +08002649 rt6_remove_exception_rt(rt);
Wei Wangc5cff852017-08-21 09:47:10 -07002650 } else {
David Aherna68886a2018-04-20 15:38:02 -07002651 struct fib6_info *from;
Wei Wangc5cff852017-08-21 09:47:10 -07002652 struct fib6_node *fn;
2653
David Aherna68886a2018-04-20 15:38:02 -07002654 from = rcu_dereference(rt->from);
2655 if (from) {
2656 fn = rcu_dereference(from->fib6_node);
2657 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2658 fn->fn_sernum = -1;
2659 }
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002660 }
David Ahern8a14e462018-04-23 11:32:07 -07002661 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
2663}
2664
David Ahern6a3e0302018-04-20 15:37:57 -07002665static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2666{
David Aherna68886a2018-04-20 15:38:02 -07002667 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2668 struct fib6_info *from;
2669
2670 rcu_read_lock();
2671 from = rcu_dereference(rt0->from);
2672 if (from)
2673 rt0->dst.expires = from->expires;
2674 rcu_read_unlock();
2675 }
David Ahern6a3e0302018-04-20 15:37:57 -07002676
2677 dst_set_expires(&rt0->dst, timeout);
2678 rt0->rt6i_flags |= RTF_EXPIRES;
2679}
2680
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002681static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2682{
2683 struct net *net = dev_net(rt->dst.dev);
2684
David Ahernd4ead6b2018-04-17 17:33:16 -07002685 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002686 rt->rt6i_flags |= RTF_MODIFIED;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002687 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2688}
2689
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002690static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2691{
2692 return !(rt->rt6i_flags & RTF_CACHE) &&
Paolo Abeni1490ed22019-02-15 18:15:37 +01002693 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002694}
2695
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002696static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +08002697 const struct ipv6hdr *iph, u32 mtu,
2698 bool confirm_neigh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
Julian Anastasov0dec8792017-02-06 23:14:16 +02002700 const struct in6_addr *daddr, *saddr;
Ian Morris67ba4152014-08-24 21:53:10 +01002701 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Xin Long19bda362016-10-28 18:18:01 +08002703 if (dst_metric_locked(dst, RTAX_MTU))
2704 return;
2705
Julian Anastasov0dec8792017-02-06 23:14:16 +02002706 if (iph) {
2707 daddr = &iph->daddr;
2708 saddr = &iph->saddr;
2709 } else if (sk) {
2710 daddr = &sk->sk_v6_daddr;
2711 saddr = &inet6_sk(sk)->saddr;
2712 } else {
2713 daddr = NULL;
2714 saddr = NULL;
2715 }
Hangbin Liubd085ef2019-12-22 10:51:09 +08002716
2717 if (confirm_neigh)
2718 dst_confirm_neigh(dst, daddr);
2719
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002720 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2721 if (mtu >= dst_mtu(dst))
2722 return;
David S. Miller81aded22012-06-15 14:54:11 -07002723
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002724 if (!rt6_cache_allowed_for_pmtu(rt6)) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002725 rt6_do_update_pmtu(rt6, mtu);
Wei Wang2b760fc2017-10-06 12:06:03 -07002726 /* update rt6_ex->stamp for cache */
2727 if (rt6->rt6i_flags & RTF_CACHE)
2728 rt6_update_exception_stamp_rt(rt6);
Julian Anastasov0dec8792017-02-06 23:14:16 +02002729 } else if (daddr) {
David Ahern85bd05d2019-04-16 14:36:01 -07002730 struct fib6_result res = {};
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002731 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01002732
David Ahern4d85cd02018-04-20 15:37:59 -07002733 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07002734 res.f6i = rcu_dereference(rt6->from);
David Ahern43a4b602019-08-01 15:18:08 -07002735 if (!res.f6i)
2736 goto out_unlock;
2737
David Ahern7d21fec2019-04-16 14:36:11 -07002738 res.fib6_flags = res.f6i->fib6_flags;
2739 res.fib6_type = res.f6i->fib6_type;
2740
David Ahern2d442342019-06-08 14:53:31 -07002741 if (res.f6i->nh) {
2742 struct fib6_nh_match_arg arg = {
2743 .dev = dst->dev,
2744 .gw = &rt6->rt6i_gateway,
2745 };
2746
2747 nexthop_for_each_fib6_nh(res.f6i->nh,
2748 fib6_nh_find_match, &arg);
2749
2750 /* fib6_info uses a nexthop that does not have fib6_nh
2751 * using the dst->dev + gw. Should be impossible.
2752 */
David Ahern43a4b602019-08-01 15:18:08 -07002753 if (!arg.match)
2754 goto out_unlock;
David Ahern2d442342019-06-08 14:53:31 -07002755
2756 res.nh = arg.match;
2757 } else {
2758 res.nh = res.f6i->fib6_nh;
2759 }
2760
David Ahern85bd05d2019-04-16 14:36:01 -07002761 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002762 if (nrt6) {
2763 rt6_do_update_pmtu(nrt6, mtu);
David Ahern5012f0a2019-04-16 14:36:05 -07002764 if (rt6_insert_exception(nrt6, &res))
Wei Wang2b760fc2017-10-06 12:06:03 -07002765 dst_release_immediate(&nrt6->dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002766 }
David Ahern43a4b602019-08-01 15:18:08 -07002767out_unlock:
David Aherna68886a2018-04-20 15:38:02 -07002768 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 }
2770}
2771
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002772static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +08002773 struct sk_buff *skb, u32 mtu,
2774 bool confirm_neigh)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002775{
Hangbin Liubd085ef2019-12-22 10:51:09 +08002776 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2777 confirm_neigh);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002778}
2779
David S. Miller42ae66c2012-06-15 20:01:57 -07002780void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002781 int oif, u32 mark, kuid_t uid)
David S. Miller81aded22012-06-15 14:54:11 -07002782{
2783 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2784 struct dst_entry *dst;
Maciej Żenczykowskidc920952018-09-29 23:44:51 -07002785 struct flowi6 fl6 = {
2786 .flowi6_oif = oif,
2787 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2788 .daddr = iph->daddr,
2789 .saddr = iph->saddr,
2790 .flowlabel = ip6_flowinfo(iph),
2791 .flowi6_uid = uid,
2792 };
David S. Miller81aded22012-06-15 14:54:11 -07002793
2794 dst = ip6_route_output(net, NULL, &fl6);
2795 if (!dst->error)
Hangbin Liubd085ef2019-12-22 10:51:09 +08002796 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
David S. Miller81aded22012-06-15 14:54:11 -07002797 dst_release(dst);
2798}
2799EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2800
2801void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2802{
David Ahern7ddacfa2018-11-18 10:45:30 -08002803 int oif = sk->sk_bound_dev_if;
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002804 struct dst_entry *dst;
2805
David Ahern7ddacfa2018-11-18 10:45:30 -08002806 if (!oif && skb->dev)
2807 oif = l3mdev_master_ifindex(skb->dev);
2808
2809 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002810
2811 dst = __sk_dst_get(sk);
2812 if (!dst || !dst->obsolete ||
2813 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2814 return;
2815
2816 bh_lock_sock(sk);
2817 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2818 ip6_datagram_dst_update(sk, false);
2819 bh_unlock_sock(sk);
David S. Miller81aded22012-06-15 14:54:11 -07002820}
2821EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2822
Alexey Kodanev7d6850f2018-04-03 15:00:07 +03002823void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2824 const struct flowi6 *fl6)
2825{
2826#ifdef CONFIG_IPV6_SUBTREES
2827 struct ipv6_pinfo *np = inet6_sk(sk);
2828#endif
2829
2830 ip6_dst_store(sk, dst,
2831 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2832 &sk->sk_v6_daddr : NULL,
2833#ifdef CONFIG_IPV6_SUBTREES
2834 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2835 &np->saddr :
2836#endif
2837 NULL);
2838}
2839
David Ahern9b6b35a2019-04-16 14:36:02 -07002840static bool ip6_redirect_nh_match(const struct fib6_result *res,
David Ahern0b34eb02019-04-09 14:41:19 -07002841 struct flowi6 *fl6,
2842 const struct in6_addr *gw,
2843 struct rt6_info **ret)
2844{
David Ahern9b6b35a2019-04-16 14:36:02 -07002845 const struct fib6_nh *nh = res->nh;
2846
David Ahern0b34eb02019-04-09 14:41:19 -07002847 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
2848 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
2849 return false;
2850
2851 /* rt_cache's gateway might be different from its 'parent'
2852 * in the case of an ip redirect.
2853 * So we keep searching in the exception table if the gateway
2854 * is different.
2855 */
2856 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
2857 struct rt6_info *rt_cache;
2858
David Ahern9b6b35a2019-04-16 14:36:02 -07002859 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
David Ahern0b34eb02019-04-09 14:41:19 -07002860 if (rt_cache &&
2861 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
2862 *ret = rt_cache;
2863 return true;
2864 }
2865 return false;
2866 }
2867 return true;
2868}
2869
David Ahernc55c8982019-06-08 14:53:29 -07002870struct fib6_nh_rd_arg {
2871 struct fib6_result *res;
2872 struct flowi6 *fl6;
2873 const struct in6_addr *gw;
2874 struct rt6_info **ret;
2875};
2876
2877static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
2878{
2879 struct fib6_nh_rd_arg *arg = _arg;
2880
2881 arg->res->nh = nh;
2882 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
2883}
2884
Duan Jiongb55b76b2013-09-04 19:44:21 +08002885/* Handle redirects */
2886struct ip6rd_flowi {
2887 struct flowi6 fl6;
2888 struct in6_addr gateway;
2889};
2890
2891static struct rt6_info *__ip6_route_redirect(struct net *net,
2892 struct fib6_table *table,
2893 struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08002894 const struct sk_buff *skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002895 int flags)
2896{
2897 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
David Ahern0b34eb02019-04-09 14:41:19 -07002898 struct rt6_info *ret = NULL;
David Ahern9b6b35a2019-04-16 14:36:02 -07002899 struct fib6_result res = {};
David Ahernc55c8982019-06-08 14:53:29 -07002900 struct fib6_nh_rd_arg arg = {
2901 .res = &res,
2902 .fl6 = fl6,
2903 .gw = &rdfl->gateway,
2904 .ret = &ret
2905 };
David Ahern8d1c8022018-04-17 17:33:26 -07002906 struct fib6_info *rt;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002907 struct fib6_node *fn;
2908
David Ahern31680ac2019-05-22 15:12:18 -07002909 /* l3mdev_update_flow overrides oif if the device is enslaved; in
2910 * this case we must match on the real ingress device, so reset it
2911 */
2912 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2913 fl6->flowi6_oif = skb->dev->ifindex;
2914
Duan Jiongb55b76b2013-09-04 19:44:21 +08002915 /* Get the "current" route for this destination and
Alexander Alemayhu67c408c2017-01-07 23:53:00 +01002916 * check if the redirect has come from appropriate router.
Duan Jiongb55b76b2013-09-04 19:44:21 +08002917 *
2918 * RFC 4861 specifies that redirects should only be
2919 * accepted if they come from the nexthop to the target.
2920 * Due to the way the routes are chosen, this notion
2921 * is a bit fuzzy and one might need to check all possible
2922 * routes.
2923 */
2924
Wei Wang66f5d6c2017-10-06 12:06:10 -07002925 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07002926 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002927restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002928 for_each_fib6_node_rt_rcu(fn) {
David Ahern9b6b35a2019-04-16 14:36:02 -07002929 res.f6i = rt;
David Ahern14895682018-04-17 17:33:17 -07002930 if (fib6_check_expired(rt))
Duan Jiongb55b76b2013-09-04 19:44:21 +08002931 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002932 if (rt->fib6_flags & RTF_REJECT)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002933 break;
David Ahernc55c8982019-06-08 14:53:29 -07002934 if (unlikely(rt->nh)) {
2935 if (nexthop_is_blackhole(rt->nh))
2936 continue;
2937 /* on match, res->nh is filled in and potentially ret */
2938 if (nexthop_for_each_fib6_nh(rt->nh,
2939 fib6_nh_redirect_match,
2940 &arg))
2941 goto out;
2942 } else {
2943 res.nh = rt->fib6_nh;
2944 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
2945 &ret))
2946 goto out;
2947 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002948 }
2949
2950 if (!rt)
David Ahern421842e2018-04-17 17:33:18 -07002951 rt = net->ipv6.fib6_null_entry;
David Ahern93c2fb22018-04-18 15:38:59 -07002952 else if (rt->fib6_flags & RTF_REJECT) {
David Ahern23fb93a2018-04-17 17:33:23 -07002953 ret = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002954 goto out;
2955 }
2956
David Ahern421842e2018-04-17 17:33:18 -07002957 if (rt == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002958 fn = fib6_backtrack(fn, &fl6->saddr);
2959 if (fn)
2960 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002961 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002962
David Ahern9b6b35a2019-04-16 14:36:02 -07002963 res.f6i = rt;
David Ahern1cf844c2019-05-22 20:27:59 -07002964 res.nh = rt->fib6_nh;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002965out:
David Ahern7d21fec2019-04-16 14:36:11 -07002966 if (ret) {
David Ahern10585b42019-03-20 09:24:50 -07002967 ip6_hold_safe(net, &ret);
David Ahern7d21fec2019-04-16 14:36:11 -07002968 } else {
2969 res.fib6_flags = res.f6i->fib6_flags;
2970 res.fib6_type = res.f6i->fib6_type;
David Ahern9b6b35a2019-04-16 14:36:02 -07002971 ret = ip6_create_rt_rcu(&res);
David Ahern7d21fec2019-04-16 14:36:11 -07002972 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002973
Wei Wang66f5d6c2017-10-06 12:06:10 -07002974 rcu_read_unlock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08002975
David Ahern8ff2e5b2019-04-16 14:36:09 -07002976 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahern23fb93a2018-04-17 17:33:23 -07002977 return ret;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002978};
2979
2980static struct dst_entry *ip6_route_redirect(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08002981 const struct flowi6 *fl6,
2982 const struct sk_buff *skb,
2983 const struct in6_addr *gateway)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002984{
2985 int flags = RT6_LOOKUP_F_HAS_SADDR;
2986 struct ip6rd_flowi rdfl;
2987
2988 rdfl.fl6 = *fl6;
2989 rdfl.gateway = *gateway;
2990
David Ahernb75cc8f2018-03-02 08:32:17 -08002991 return fib6_rule_lookup(net, &rdfl.fl6, skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002992 flags, __ip6_route_redirect);
2993}
2994
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002995void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2996 kuid_t uid)
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002997{
2998 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2999 struct dst_entry *dst;
Maciej Żenczykowski1f7f10a2018-09-29 23:44:48 -07003000 struct flowi6 fl6 = {
3001 .flowi6_iif = LOOPBACK_IFINDEX,
3002 .flowi6_oif = oif,
3003 .flowi6_mark = mark,
3004 .daddr = iph->daddr,
3005 .saddr = iph->saddr,
3006 .flowlabel = ip6_flowinfo(iph),
3007 .flowi6_uid = uid,
3008 };
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003009
David Ahernb75cc8f2018-03-02 08:32:17 -08003010 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08003011 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003012 dst_release(dst);
3013}
3014EXPORT_SYMBOL_GPL(ip6_redirect);
3015
Maciej Żenczykowskid4563362018-09-29 23:44:50 -07003016void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
Duan Jiongc92a59e2013-08-22 12:07:35 +08003017{
3018 const struct ipv6hdr *iph = ipv6_hdr(skb);
3019 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3020 struct dst_entry *dst;
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07003021 struct flowi6 fl6 = {
3022 .flowi6_iif = LOOPBACK_IFINDEX,
3023 .flowi6_oif = oif,
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07003024 .daddr = msg->dest,
3025 .saddr = iph->daddr,
3026 .flowi6_uid = sock_net_uid(net, NULL),
3027 };
Duan Jiongc92a59e2013-08-22 12:07:35 +08003028
David Ahernb75cc8f2018-03-02 08:32:17 -08003029 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08003030 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08003031 dst_release(dst);
3032}
3033
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003034void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
3035{
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09003036 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
3037 sk->sk_uid);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003038}
3039EXPORT_SYMBOL_GPL(ip6_sk_redirect);
3040
David S. Miller0dbaee32010-12-13 12:52:14 -08003041static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042{
David S. Miller0dbaee32010-12-13 12:52:14 -08003043 struct net_device *dev = dst->dev;
3044 unsigned int mtu = dst_mtu(dst);
3045 struct net *net = dev_net(dev);
3046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
3048
Daniel Lezcano55786892008-03-04 13:47:47 -08003049 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
3050 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
3052 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003053 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3054 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3055 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 * rely only on pmtu discovery"
3057 */
3058 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
3059 mtu = IPV6_MAXPLEN;
3060 return mtu;
3061}
3062
Steffen Klassertebb762f2011-11-23 02:12:51 +00003063static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08003064{
David S. Millerd33e4552010-12-14 13:01:14 -08003065 struct inet6_dev *idev;
David Ahernd4ead6b2018-04-17 17:33:16 -07003066 unsigned int mtu;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003067
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07003068 mtu = dst_metric_raw(dst, RTAX_MTU);
3069 if (mtu)
3070 goto out;
3071
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003072 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08003073
3074 rcu_read_lock();
3075 idev = __in6_dev_get(dst->dev);
3076 if (idev)
3077 mtu = idev->cnf.mtu6;
3078 rcu_read_unlock();
3079
Eric Dumazet30f78d82014-04-10 21:23:36 -07003080out:
Roopa Prabhu14972cb2016-08-24 20:10:43 -07003081 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3082
3083 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
David S. Millerd33e4552010-12-14 13:01:14 -08003084}
3085
David Ahern901731b2018-05-21 09:08:14 -07003086/* MTU selection:
3087 * 1. mtu on route is locked - use it
3088 * 2. mtu from nexthop exception
3089 * 3. mtu from egress device
3090 *
3091 * based on ip6_dst_mtu_forward and exception logic of
3092 * rt6_find_cached_rt; called with rcu_read_lock
3093 */
David Ahernb748f262019-04-16 14:36:06 -07003094u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3095 const struct in6_addr *daddr,
3096 const struct in6_addr *saddr)
David Ahern901731b2018-05-21 09:08:14 -07003097{
David Ahernb748f262019-04-16 14:36:06 -07003098 const struct fib6_nh *nh = res->nh;
3099 struct fib6_info *f6i = res->f6i;
David Ahern901731b2018-05-21 09:08:14 -07003100 struct inet6_dev *idev;
Wei Wang510e2ce2019-05-16 13:30:54 -07003101 struct rt6_info *rt;
David Ahern901731b2018-05-21 09:08:14 -07003102 u32 mtu = 0;
3103
3104 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3105 mtu = f6i->fib6_pmtu;
3106 if (mtu)
3107 goto out;
3108 }
3109
Wei Wang510e2ce2019-05-16 13:30:54 -07003110 rt = rt6_find_cached_rt(res, daddr, saddr);
3111 if (unlikely(rt)) {
3112 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3113 } else {
David Ahernb748f262019-04-16 14:36:06 -07003114 struct net_device *dev = nh->fib_nh_dev;
David Ahern901731b2018-05-21 09:08:14 -07003115
3116 mtu = IPV6_MIN_MTU;
3117 idev = __in6_dev_get(dev);
3118 if (idev && idev->cnf.mtu6 > mtu)
3119 mtu = idev->cnf.mtu6;
3120 }
3121
3122 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3123out:
David Ahernb748f262019-04-16 14:36:06 -07003124 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahern901731b2018-05-21 09:08:14 -07003125}
3126
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08003127struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05003128 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
David S. Miller87a11572011-12-06 17:04:13 -05003130 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 struct rt6_info *rt;
3132 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003133 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
David S. Miller38308472011-12-03 18:02:47 -05003135 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00003136 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
Martin KaFai Lauad706862015-08-14 11:05:52 -07003138 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05003139 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05003141 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 goto out;
3143 }
3144
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003145 rt->dst.flags |= DST_HOST;
Brendan McGrath588753f2017-12-13 22:14:57 +11003146 rt->dst.input = ip6_input;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003147 rt->dst.output = ip6_output;
Julian Anastasov550bab42013-10-20 15:43:04 +03003148 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05003149 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003150 rt->rt6i_dst.plen = 128;
3151 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08003152 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Ido Schimmel4c981e22018-01-07 12:45:04 +02003154 /* Add this dst into uncached_list so that rt6_disable_ip() can
Wei Wang587fea72017-06-17 10:42:36 -07003155 * do proper release of the net_device
3156 */
3157 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07003158 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
David S. Miller87a11572011-12-06 17:04:13 -05003160 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3161
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162out:
David S. Miller87a11572011-12-06 17:04:13 -05003163 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164}
3165
Daniel Lezcano569d3642008-01-18 03:56:57 -08003166static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003168 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08003169 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3170 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
3171 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3172 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3173 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003174 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
Eric Dumazetfc66f952010-10-08 06:37:34 +00003176 entries = dst_entries_get_fast(ops);
Michal Kubeček49a18d82013-08-01 10:04:24 +02003177 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00003178 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 goto out;
3180
Benjamin Thery6891a342008-03-04 13:49:47 -08003181 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08003182 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003183 entries = dst_entries_get_slow(ops);
3184 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08003185 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08003187 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003188 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189}
3190
David Ahernb2c709c2019-06-24 13:44:51 -07003191static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3192 const struct in6_addr *gw_addr, u32 tbid,
3193 int flags, struct fib6_result *res)
David Ahern8c145862016-04-24 21:26:04 -07003194{
3195 struct flowi6 fl6 = {
3196 .flowi6_oif = cfg->fc_ifindex,
3197 .daddr = *gw_addr,
3198 .saddr = cfg->fc_prefsrc,
3199 };
3200 struct fib6_table *table;
David Ahernb2c709c2019-06-24 13:44:51 -07003201 int err;
David Ahern8c145862016-04-24 21:26:04 -07003202
David Ahernf4797b32018-01-25 16:55:08 -08003203 table = fib6_get_table(net, tbid);
David Ahern8c145862016-04-24 21:26:04 -07003204 if (!table)
David Ahernb2c709c2019-06-24 13:44:51 -07003205 return -EINVAL;
David Ahern8c145862016-04-24 21:26:04 -07003206
3207 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3208 flags |= RT6_LOOKUP_F_HAS_SADDR;
3209
David Ahernf4797b32018-01-25 16:55:08 -08003210 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
David Ahern8c145862016-04-24 21:26:04 -07003211
David Ahernb2c709c2019-06-24 13:44:51 -07003212 err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3213 if (!err && res->f6i != net->ipv6.fib6_null_entry)
3214 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3215 cfg->fc_ifindex != 0, NULL, flags);
David Ahern8c145862016-04-24 21:26:04 -07003216
David Ahernb2c709c2019-06-24 13:44:51 -07003217 return err;
David Ahern8c145862016-04-24 21:26:04 -07003218}
3219
David Ahernfc1e64e2018-01-25 16:55:09 -08003220static int ip6_route_check_nh_onlink(struct net *net,
3221 struct fib6_config *cfg,
David Ahern9fbb7042018-03-13 08:29:36 -07003222 const struct net_device *dev,
David Ahernfc1e64e2018-01-25 16:55:09 -08003223 struct netlink_ext_ack *extack)
3224{
David Ahernb2c709c2019-06-24 13:44:51 -07003225 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
David Ahernfc1e64e2018-01-25 16:55:09 -08003226 const struct in6_addr *gw_addr = &cfg->fc_gateway;
David Ahernb2c709c2019-06-24 13:44:51 -07003227 struct fib6_result res = {};
David Ahernfc1e64e2018-01-25 16:55:09 -08003228 int err;
3229
David Ahernb2c709c2019-06-24 13:44:51 -07003230 err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3231 if (!err && !(res.fib6_flags & RTF_REJECT) &&
3232 /* ignore match if it is the default route */
3233 !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3234 (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
3235 NL_SET_ERR_MSG(extack,
3236 "Nexthop has invalid gateway or device mismatch");
3237 err = -EINVAL;
David Ahernfc1e64e2018-01-25 16:55:09 -08003238 }
3239
3240 return err;
3241}
3242
David Ahern1edce992018-01-25 16:55:07 -08003243static int ip6_route_check_nh(struct net *net,
3244 struct fib6_config *cfg,
3245 struct net_device **_dev,
3246 struct inet6_dev **idev)
3247{
3248 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3249 struct net_device *dev = _dev ? *_dev : NULL;
David Ahernb2c709c2019-06-24 13:44:51 -07003250 int flags = RT6_LOOKUP_F_IFACE;
3251 struct fib6_result res = {};
David Ahern1edce992018-01-25 16:55:07 -08003252 int err = -EHOSTUNREACH;
3253
3254 if (cfg->fc_table) {
David Ahernb2c709c2019-06-24 13:44:51 -07003255 err = ip6_nh_lookup_table(net, cfg, gw_addr,
3256 cfg->fc_table, flags, &res);
3257 /* gw_addr can not require a gateway or resolve to a reject
3258 * route. If a device is given, it must match the result.
3259 */
3260 if (err || res.fib6_flags & RTF_REJECT ||
3261 res.nh->fib_nh_gw_family ||
3262 (dev && dev != res.nh->fib_nh_dev))
3263 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003264 }
3265
David Ahernb2c709c2019-06-24 13:44:51 -07003266 if (err < 0) {
3267 struct flowi6 fl6 = {
3268 .flowi6_oif = cfg->fc_ifindex,
3269 .daddr = *gw_addr,
3270 };
David Ahern1edce992018-01-25 16:55:07 -08003271
David Ahernb2c709c2019-06-24 13:44:51 -07003272 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3273 if (err || res.fib6_flags & RTF_REJECT ||
3274 res.nh->fib_nh_gw_family)
3275 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003276
David Ahernb2c709c2019-06-24 13:44:51 -07003277 if (err)
3278 return err;
3279
3280 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3281 cfg->fc_ifindex != 0, NULL, flags);
3282 }
3283
3284 err = 0;
David Ahern1edce992018-01-25 16:55:07 -08003285 if (dev) {
David Ahernb2c709c2019-06-24 13:44:51 -07003286 if (dev != res.nh->fib_nh_dev)
3287 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003288 } else {
David Ahernb2c709c2019-06-24 13:44:51 -07003289 *_dev = dev = res.nh->fib_nh_dev;
David Ahern1edce992018-01-25 16:55:07 -08003290 dev_hold(dev);
David Ahernb2c709c2019-06-24 13:44:51 -07003291 *idev = in6_dev_get(dev);
David Ahern1edce992018-01-25 16:55:07 -08003292 }
3293
David Ahern1edce992018-01-25 16:55:07 -08003294 return err;
3295}
3296
David Ahern9fbb7042018-03-13 08:29:36 -07003297static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3298 struct net_device **_dev, struct inet6_dev **idev,
3299 struct netlink_ext_ack *extack)
3300{
3301 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3302 int gwa_type = ipv6_addr_type(gw_addr);
David Ahern232378e2018-03-13 08:29:37 -07003303 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
David Ahern9fbb7042018-03-13 08:29:36 -07003304 const struct net_device *dev = *_dev;
David Ahern232378e2018-03-13 08:29:37 -07003305 bool need_addr_check = !dev;
David Ahern9fbb7042018-03-13 08:29:36 -07003306 int err = -EINVAL;
3307
3308 /* if gw_addr is local we will fail to detect this in case
3309 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3310 * will return already-added prefix route via interface that
3311 * prefix route was assigned to, which might be non-loopback.
3312 */
David Ahern232378e2018-03-13 08:29:37 -07003313 if (dev &&
3314 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3315 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
David Ahern9fbb7042018-03-13 08:29:36 -07003316 goto out;
3317 }
3318
3319 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3320 /* IPv6 strictly inhibits using not link-local
3321 * addresses as nexthop address.
3322 * Otherwise, router will not able to send redirects.
3323 * It is very good, but in some (rare!) circumstances
3324 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3325 * some exceptions. --ANK
3326 * We allow IPv4-mapped nexthops to support RFC4798-type
3327 * addressing
3328 */
3329 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3330 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3331 goto out;
3332 }
3333
David Ahernb2c709c2019-06-24 13:44:51 -07003334 rcu_read_lock();
3335
David Ahern9fbb7042018-03-13 08:29:36 -07003336 if (cfg->fc_flags & RTNH_F_ONLINK)
3337 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3338 else
3339 err = ip6_route_check_nh(net, cfg, _dev, idev);
3340
David Ahernb2c709c2019-06-24 13:44:51 -07003341 rcu_read_unlock();
3342
David Ahern9fbb7042018-03-13 08:29:36 -07003343 if (err)
3344 goto out;
3345 }
3346
3347 /* reload in case device was changed */
3348 dev = *_dev;
3349
3350 err = -EINVAL;
3351 if (!dev) {
3352 NL_SET_ERR_MSG(extack, "Egress device not specified");
3353 goto out;
3354 } else if (dev->flags & IFF_LOOPBACK) {
3355 NL_SET_ERR_MSG(extack,
3356 "Egress device can not be loopback device for this route");
3357 goto out;
3358 }
David Ahern232378e2018-03-13 08:29:37 -07003359
3360 /* if we did not check gw_addr above, do so now that the
3361 * egress device has been resolved.
3362 */
3363 if (need_addr_check &&
3364 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3365 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3366 goto out;
3367 }
3368
David Ahern9fbb7042018-03-13 08:29:36 -07003369 err = 0;
3370out:
3371 return err;
3372}
3373
David Ahern83c442512019-03-27 20:53:50 -07003374static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3375{
3376 if ((flags & RTF_REJECT) ||
3377 (dev && (dev->flags & IFF_LOOPBACK) &&
3378 !(addr_type & IPV6_ADDR_LOOPBACK) &&
3379 !(flags & RTF_LOCAL)))
3380 return true;
3381
3382 return false;
3383}
3384
3385int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3386 struct fib6_config *cfg, gfp_t gfp_flags,
3387 struct netlink_ext_ack *extack)
3388{
3389 struct net_device *dev = NULL;
3390 struct inet6_dev *idev = NULL;
3391 int addr_type;
3392 int err;
3393
David Ahernf1741732019-03-27 20:53:57 -07003394 fib6_nh->fib_nh_family = AF_INET6;
Eric Dumazet1bef4c22019-11-07 09:26:19 -08003395#ifdef CONFIG_IPV6_ROUTER_PREF
3396 fib6_nh->last_probe = jiffies;
3397#endif
David Ahernf1741732019-03-27 20:53:57 -07003398
David Ahern83c442512019-03-27 20:53:50 -07003399 err = -ENODEV;
3400 if (cfg->fc_ifindex) {
3401 dev = dev_get_by_index(net, cfg->fc_ifindex);
3402 if (!dev)
3403 goto out;
3404 idev = in6_dev_get(dev);
3405 if (!idev)
3406 goto out;
3407 }
3408
3409 if (cfg->fc_flags & RTNH_F_ONLINK) {
3410 if (!dev) {
3411 NL_SET_ERR_MSG(extack,
3412 "Nexthop device required for onlink");
3413 goto out;
3414 }
3415
3416 if (!(dev->flags & IFF_UP)) {
3417 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3418 err = -ENETDOWN;
3419 goto out;
3420 }
3421
David Ahernad1601a2019-03-27 20:53:56 -07003422 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
David Ahern83c442512019-03-27 20:53:50 -07003423 }
3424
David Ahernad1601a2019-03-27 20:53:56 -07003425 fib6_nh->fib_nh_weight = 1;
David Ahern83c442512019-03-27 20:53:50 -07003426
3427 /* We cannot add true routes via loopback here,
3428 * they would result in kernel looping; promote them to reject routes
3429 */
3430 addr_type = ipv6_addr_type(&cfg->fc_dst);
3431 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3432 /* hold loopback dev/idev if we haven't done so. */
3433 if (dev != net->loopback_dev) {
3434 if (dev) {
3435 dev_put(dev);
3436 in6_dev_put(idev);
3437 }
3438 dev = net->loopback_dev;
3439 dev_hold(dev);
3440 idev = in6_dev_get(dev);
3441 if (!idev) {
3442 err = -ENODEV;
3443 goto out;
3444 }
3445 }
David Ahern7dd73162019-06-03 18:37:03 -07003446 goto pcpu_alloc;
David Ahern83c442512019-03-27 20:53:50 -07003447 }
3448
3449 if (cfg->fc_flags & RTF_GATEWAY) {
3450 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3451 if (err)
3452 goto out;
3453
David Ahernad1601a2019-03-27 20:53:56 -07003454 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
David Ahernbdf00462019-04-05 16:30:26 -07003455 fib6_nh->fib_nh_gw_family = AF_INET6;
David Ahern83c442512019-03-27 20:53:50 -07003456 }
3457
3458 err = -ENODEV;
3459 if (!dev)
3460 goto out;
3461
3462 if (idev->cnf.disable_ipv6) {
3463 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3464 err = -EACCES;
3465 goto out;
3466 }
3467
3468 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3469 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3470 err = -ENETDOWN;
3471 goto out;
3472 }
3473
3474 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3475 !netif_carrier_ok(dev))
David Ahernad1601a2019-03-27 20:53:56 -07003476 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
David Ahern83c442512019-03-27 20:53:50 -07003477
David Ahern7dd73162019-06-03 18:37:03 -07003478 err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap,
3479 cfg->fc_encap_type, cfg, gfp_flags, extack);
3480 if (err)
3481 goto out;
3482
3483pcpu_alloc:
David Ahernf40b6ae2019-05-22 20:27:55 -07003484 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3485 if (!fib6_nh->rt6i_pcpu) {
3486 err = -ENOMEM;
3487 goto out;
3488 }
3489
David Ahernad1601a2019-03-27 20:53:56 -07003490 fib6_nh->fib_nh_dev = dev;
David Ahernf1741732019-03-27 20:53:57 -07003491 fib6_nh->fib_nh_oif = dev->ifindex;
David Ahern83c442512019-03-27 20:53:50 -07003492 err = 0;
3493out:
3494 if (idev)
3495 in6_dev_put(idev);
3496
3497 if (err) {
David Ahernad1601a2019-03-27 20:53:56 -07003498 lwtstate_put(fib6_nh->fib_nh_lws);
3499 fib6_nh->fib_nh_lws = NULL;
David Ahern83c442512019-03-27 20:53:50 -07003500 if (dev)
3501 dev_put(dev);
3502 }
3503
3504 return err;
3505}
3506
David Aherndac7d0f2019-03-27 20:53:51 -07003507void fib6_nh_release(struct fib6_nh *fib6_nh)
3508{
David Aherncc5c0732019-05-22 20:27:58 -07003509 struct rt6_exception_bucket *bucket;
3510
3511 rcu_read_lock();
3512
3513 fib6_nh_flush_exceptions(fib6_nh, NULL);
3514 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3515 if (bucket) {
3516 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3517 kfree(bucket);
3518 }
3519
3520 rcu_read_unlock();
3521
David Ahernf40b6ae2019-05-22 20:27:55 -07003522 if (fib6_nh->rt6i_pcpu) {
3523 int cpu;
3524
3525 for_each_possible_cpu(cpu) {
3526 struct rt6_info **ppcpu_rt;
3527 struct rt6_info *pcpu_rt;
3528
3529 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3530 pcpu_rt = *ppcpu_rt;
3531 if (pcpu_rt) {
3532 dst_dev_put(&pcpu_rt->dst);
3533 dst_release(&pcpu_rt->dst);
3534 *ppcpu_rt = NULL;
3535 }
3536 }
3537
3538 free_percpu(fib6_nh->rt6i_pcpu);
3539 }
3540
David Ahern979e2762019-03-27 20:53:58 -07003541 fib_nh_common_release(&fib6_nh->nh_common);
David Aherndac7d0f2019-03-27 20:53:51 -07003542}
3543
David Ahern8d1c8022018-04-17 17:33:26 -07003544static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
David Ahernacb54e32018-04-17 17:33:22 -07003545 gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003546 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547{
Daniel Lezcano55786892008-03-04 13:47:47 -08003548 struct net *net = cfg->fc_nlinfo.nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07003549 struct fib6_info *rt = NULL;
David Ahernf88d8ea2019-06-03 20:19:52 -07003550 struct nexthop *nh = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003551 struct fib6_table *table;
David Ahernf88d8ea2019-06-03 20:19:52 -07003552 struct fib6_nh *fib6_nh;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003553 int err = -EINVAL;
David Ahern83c442512019-03-27 20:53:50 -07003554 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
David Ahern557c44b2017-04-19 14:19:43 -07003556 /* RTF_PCPU is an internal flag; can not be set by userspace */
David Ahernd5d531c2017-05-21 10:12:05 -06003557 if (cfg->fc_flags & RTF_PCPU) {
3558 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
David Ahern557c44b2017-04-19 14:19:43 -07003559 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003560 }
David Ahern557c44b2017-04-19 14:19:43 -07003561
Wei Wang2ea23522017-10-27 17:30:12 -07003562 /* RTF_CACHE is an internal flag; can not be set by userspace */
3563 if (cfg->fc_flags & RTF_CACHE) {
3564 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3565 goto out;
3566 }
3567
David Aherne8478e82018-04-17 17:33:13 -07003568 if (cfg->fc_type > RTN_MAX) {
3569 NL_SET_ERR_MSG(extack, "Invalid route type");
3570 goto out;
3571 }
3572
David Ahernd5d531c2017-05-21 10:12:05 -06003573 if (cfg->fc_dst_len > 128) {
3574 NL_SET_ERR_MSG(extack, "Invalid prefix length");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003575 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003576 }
3577 if (cfg->fc_src_len > 128) {
3578 NL_SET_ERR_MSG(extack, "Invalid source address length");
3579 goto out;
3580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581#ifndef CONFIG_IPV6_SUBTREES
David Ahernd5d531c2017-05-21 10:12:05 -06003582 if (cfg->fc_src_len) {
3583 NL_SET_ERR_MSG(extack,
3584 "Specifying source address requires IPV6_SUBTREES to be enabled");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003585 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587#endif
David Ahern5b983242019-06-08 14:53:34 -07003588 if (cfg->fc_nh_id) {
3589 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3590 if (!nh) {
3591 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
3592 goto out;
3593 }
3594 err = fib6_check_nexthop(nh, cfg, extack);
3595 if (err)
3596 goto out;
3597 }
David Ahernfc1e64e2018-01-25 16:55:09 -08003598
Matti Vaittinend71314b2011-11-14 00:14:49 +00003599 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05003600 if (cfg->fc_nlinfo.nlh &&
3601 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00003602 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05003603 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00003604 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00003605 table = fib6_new_table(net, cfg->fc_table);
3606 }
3607 } else {
3608 table = fib6_new_table(net, cfg->fc_table);
3609 }
David S. Miller38308472011-12-03 18:02:47 -05003610
3611 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003612 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07003613
David Ahern93531c62018-04-17 17:33:25 -07003614 err = -ENOMEM;
David Ahernf88d8ea2019-06-03 20:19:52 -07003615 rt = fib6_info_alloc(gfp_flags, !nh);
David Ahern93531c62018-04-17 17:33:25 -07003616 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 goto out;
David Ahern93531c62018-04-17 17:33:25 -07003618
David Ahernd7e774f2018-11-06 12:51:15 -08003619 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3620 extack);
David Ahern767a2212018-10-04 20:07:51 -07003621 if (IS_ERR(rt->fib6_metrics)) {
3622 err = PTR_ERR(rt->fib6_metrics);
Eric Dumazetfda21d42018-10-05 09:17:50 -07003623 /* Do not leave garbage there. */
3624 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
David Ahern767a2212018-10-04 20:07:51 -07003625 goto out;
3626 }
3627
David Ahern93531c62018-04-17 17:33:25 -07003628 if (cfg->fc_flags & RTF_ADDRCONF)
3629 rt->dst_nocount = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
Gao feng1716a962012-04-06 00:13:10 +00003631 if (cfg->fc_flags & RTF_EXPIRES)
David Ahern14895682018-04-17 17:33:17 -07003632 fib6_set_expires(rt, jiffies +
Gao feng1716a962012-04-06 00:13:10 +00003633 clock_t_to_jiffies(cfg->fc_expires));
3634 else
David Ahern14895682018-04-17 17:33:17 -07003635 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
Thomas Graf86872cb2006-08-22 00:01:08 -07003637 if (cfg->fc_protocol == RTPROT_UNSPEC)
3638 cfg->fc_protocol = RTPROT_BOOT;
David Ahern93c2fb22018-04-18 15:38:59 -07003639 rt->fib6_protocol = cfg->fc_protocol;
Thomas Graf86872cb2006-08-22 00:01:08 -07003640
David Ahern83c442512019-03-27 20:53:50 -07003641 rt->fib6_table = table;
3642 rt->fib6_metric = cfg->fc_metric;
David Ahernc7036d92019-06-19 10:50:24 -07003643 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
David Ahern2b2450c2019-03-27 20:53:52 -07003644 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003645
David Ahern93c2fb22018-04-18 15:38:59 -07003646 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3647 rt->fib6_dst.plen = cfg->fc_dst_len;
3648 if (rt->fib6_dst.plen == 128)
David Ahern3b6761d2018-04-17 17:33:20 -07003649 rt->dst_host = true;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01003650
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07003652 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3653 rt->fib6_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654#endif
David Ahernf88d8ea2019-06-03 20:19:52 -07003655 if (nh) {
3656 if (!nexthop_get(nh)) {
3657 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3658 goto out;
3659 }
3660 if (rt->fib6_src.plen) {
Colin Ian King4daa95a2019-06-06 09:40:39 +01003661 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
David Ahernf88d8ea2019-06-03 20:19:52 -07003662 goto out;
3663 }
3664 rt->nh = nh;
3665 fib6_nh = nexthop_fib6_nh(rt->nh);
3666 } else {
3667 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3668 if (err)
3669 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
David Ahernf88d8ea2019-06-03 20:19:52 -07003671 fib6_nh = rt->fib6_nh;
3672
3673 /* We cannot add true routes via loopback here, they would
3674 * result in kernel looping; promote them to reject routes
3675 */
3676 addr_type = ipv6_addr_type(&cfg->fc_dst);
3677 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3678 addr_type))
3679 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3680 }
David Ahern955ec4c2018-01-24 19:45:29 -08003681
Daniel Walterc3968a82011-04-13 21:10:57 +00003682 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
David Ahernf88d8ea2019-06-03 20:19:52 -07003683 struct net_device *dev = fib6_nh->fib_nh_dev;
David Ahern83c442512019-03-27 20:53:50 -07003684
Daniel Walterc3968a82011-04-13 21:10:57 +00003685 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
David Ahernd5d531c2017-05-21 10:12:05 -06003686 NL_SET_ERR_MSG(extack, "Invalid source address");
Daniel Walterc3968a82011-04-13 21:10:57 +00003687 err = -EINVAL;
3688 goto out;
3689 }
David Ahern93c2fb22018-04-18 15:38:59 -07003690 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3691 rt->fib6_prefsrc.plen = 128;
Daniel Walterc3968a82011-04-13 21:10:57 +00003692 } else
David Ahern93c2fb22018-04-18 15:38:59 -07003693 rt->fib6_prefsrc.plen = 0;
Daniel Walterc3968a82011-04-13 21:10:57 +00003694
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003695 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696out:
David Ahern93531c62018-04-17 17:33:25 -07003697 fib6_info_release(rt);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003698 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003699}
3700
David Ahernacb54e32018-04-17 17:33:22 -07003701int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003702 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003703{
David Ahern8d1c8022018-04-17 17:33:26 -07003704 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003705 int err;
3706
David Ahernacb54e32018-04-17 17:33:22 -07003707 rt = ip6_route_info_create(cfg, gfp_flags, extack);
David Ahernd4ead6b2018-04-17 17:33:16 -07003708 if (IS_ERR(rt))
3709 return PTR_ERR(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003710
David Ahernd4ead6b2018-04-17 17:33:16 -07003711 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
David Ahern93531c62018-04-17 17:33:25 -07003712 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003713
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 return err;
3715}
3716
David Ahern8d1c8022018-04-17 17:33:26 -07003717static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718{
David Ahernafb1d4b52018-04-17 17:33:11 -07003719 struct net *net = info->nl_net;
Thomas Grafc71099a2006-08-04 23:20:06 -07003720 struct fib6_table *table;
David Ahernafb1d4b52018-04-17 17:33:11 -07003721 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
David Ahern421842e2018-04-17 17:33:18 -07003723 if (rt == net->ipv6.fib6_null_entry) {
Gao feng6825a262012-09-19 19:25:34 +00003724 err = -ENOENT;
3725 goto out;
3726 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07003727
David Ahern93c2fb22018-04-18 15:38:59 -07003728 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003729 spin_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07003730 err = fib6_del(rt, info);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003731 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
Gao feng6825a262012-09-19 19:25:34 +00003733out:
David Ahern93531c62018-04-17 17:33:25 -07003734 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 return err;
3736}
3737
David Ahern8d1c8022018-04-17 17:33:26 -07003738int ip6_del_rt(struct net *net, struct fib6_info *rt)
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003739{
David Ahernafb1d4b52018-04-17 17:33:11 -07003740 struct nl_info info = { .nl_net = net };
3741
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003742 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003743}
3744
David Ahern8d1c8022018-04-17 17:33:26 -07003745static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
David Ahern0ae81332017-02-02 12:37:08 -08003746{
3747 struct nl_info *info = &cfg->fc_nlinfo;
WANG Conge3330032017-02-27 16:07:43 -08003748 struct net *net = info->nl_net;
David Ahern16a16cd2017-02-02 12:37:11 -08003749 struct sk_buff *skb = NULL;
David Ahern0ae81332017-02-02 12:37:08 -08003750 struct fib6_table *table;
WANG Conge3330032017-02-27 16:07:43 -08003751 int err = -ENOENT;
David Ahern0ae81332017-02-02 12:37:08 -08003752
David Ahern421842e2018-04-17 17:33:18 -07003753 if (rt == net->ipv6.fib6_null_entry)
WANG Conge3330032017-02-27 16:07:43 -08003754 goto out_put;
David Ahern93c2fb22018-04-18 15:38:59 -07003755 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003756 spin_lock_bh(&table->tb6_lock);
David Ahern0ae81332017-02-02 12:37:08 -08003757
David Ahern93c2fb22018-04-18 15:38:59 -07003758 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
David Ahern8d1c8022018-04-17 17:33:26 -07003759 struct fib6_info *sibling, *next_sibling;
Ido Schimmel02846962019-12-23 15:28:18 +02003760 struct fib6_node *fn;
David Ahern0ae81332017-02-02 12:37:08 -08003761
David Ahern16a16cd2017-02-02 12:37:11 -08003762 /* prefer to send a single notification with all hops */
3763 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3764 if (skb) {
3765 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3766
David Ahernd4ead6b2018-04-17 17:33:16 -07003767 if (rt6_fill_node(net, skb, rt, NULL,
David Ahern16a16cd2017-02-02 12:37:11 -08003768 NULL, NULL, 0, RTM_DELROUTE,
3769 info->portid, seq, 0) < 0) {
3770 kfree_skb(skb);
3771 skb = NULL;
3772 } else
3773 info->skip_notify = 1;
3774 }
3775
Ido Schimmel02846962019-12-23 15:28:18 +02003776 /* 'rt' points to the first sibling route. If it is not the
3777 * leaf, then we do not need to send a notification. Otherwise,
3778 * we need to check if the last sibling has a next route or not
3779 * and emit a replace or delete notification, respectively.
3780 */
Ido Schimmel2881fd62019-06-18 18:12:49 +03003781 info->skip_notify_kernel = 1;
Ido Schimmel02846962019-12-23 15:28:18 +02003782 fn = rcu_dereference_protected(rt->fib6_node,
3783 lockdep_is_held(&table->tb6_lock));
3784 if (rcu_access_pointer(fn->leaf) == rt) {
3785 struct fib6_info *last_sibling, *replace_rt;
3786
3787 last_sibling = list_last_entry(&rt->fib6_siblings,
3788 struct fib6_info,
3789 fib6_siblings);
3790 replace_rt = rcu_dereference_protected(
3791 last_sibling->fib6_next,
3792 lockdep_is_held(&table->tb6_lock));
3793 if (replace_rt)
3794 call_fib6_entry_notifiers_replace(net,
3795 replace_rt);
3796 else
3797 call_fib6_multipath_entry_notifiers(net,
Ido Schimmelcaafb252019-12-23 15:28:20 +02003798 FIB_EVENT_ENTRY_DEL,
Ido Schimmel02846962019-12-23 15:28:18 +02003799 rt, rt->fib6_nsiblings,
3800 NULL);
3801 }
David Ahern0ae81332017-02-02 12:37:08 -08003802 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07003803 &rt->fib6_siblings,
3804 fib6_siblings) {
David Ahern0ae81332017-02-02 12:37:08 -08003805 err = fib6_del(sibling, info);
3806 if (err)
WANG Conge3330032017-02-27 16:07:43 -08003807 goto out_unlock;
David Ahern0ae81332017-02-02 12:37:08 -08003808 }
3809 }
3810
3811 err = fib6_del(rt, info);
WANG Conge3330032017-02-27 16:07:43 -08003812out_unlock:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003813 spin_unlock_bh(&table->tb6_lock);
WANG Conge3330032017-02-27 16:07:43 -08003814out_put:
David Ahern93531c62018-04-17 17:33:25 -07003815 fib6_info_release(rt);
David Ahern16a16cd2017-02-02 12:37:11 -08003816
3817 if (skb) {
WANG Conge3330032017-02-27 16:07:43 -08003818 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
David Ahern16a16cd2017-02-02 12:37:11 -08003819 info->nlh, gfp_any());
3820 }
David Ahern0ae81332017-02-02 12:37:08 -08003821 return err;
3822}
3823
David Ahern0fa6efc2019-05-22 20:28:00 -07003824static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
David Ahern23fb93a2018-04-17 17:33:23 -07003825{
3826 int rc = -ESRCH;
3827
3828 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3829 goto out;
3830
3831 if (cfg->fc_flags & RTF_GATEWAY &&
3832 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3833 goto out;
Xin Long761f6022018-11-14 00:48:28 +08003834
3835 rc = rt6_remove_exception_rt(rt);
David Ahern23fb93a2018-04-17 17:33:23 -07003836out:
3837 return rc;
3838}
3839
David Ahern0fa6efc2019-05-22 20:28:00 -07003840static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3841 struct fib6_nh *nh)
3842{
3843 struct fib6_result res = {
3844 .f6i = rt,
3845 .nh = nh,
3846 };
3847 struct rt6_info *rt_cache;
3848
3849 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3850 if (rt_cache)
3851 return __ip6_del_cached_rt(rt_cache, cfg);
3852
3853 return 0;
3854}
3855
David Ahern5b983242019-06-08 14:53:34 -07003856struct fib6_nh_del_cached_rt_arg {
3857 struct fib6_config *cfg;
3858 struct fib6_info *f6i;
3859};
3860
3861static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
3862{
3863 struct fib6_nh_del_cached_rt_arg *arg = _arg;
3864 int rc;
3865
3866 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
3867 return rc != -ESRCH ? rc : 0;
3868}
3869
3870static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
3871{
3872 struct fib6_nh_del_cached_rt_arg arg = {
3873 .cfg = cfg,
3874 .f6i = f6i
3875 };
3876
3877 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
3878}
3879
David Ahern333c4302017-05-21 10:12:04 -06003880static int ip6_route_del(struct fib6_config *cfg,
3881 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882{
Thomas Grafc71099a2006-08-04 23:20:06 -07003883 struct fib6_table *table;
David Ahern8d1c8022018-04-17 17:33:26 -07003884 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 int err = -ESRCH;
3887
Daniel Lezcano55786892008-03-04 13:47:47 -08003888 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David Ahernd5d531c2017-05-21 10:12:05 -06003889 if (!table) {
3890 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Grafc71099a2006-08-04 23:20:06 -07003891 return err;
David Ahernd5d531c2017-05-21 10:12:05 -06003892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
Wei Wang66f5d6c2017-10-06 12:06:10 -07003894 rcu_read_lock();
Thomas Grafc71099a2006-08-04 23:20:06 -07003895
3896 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07003897 &cfg->fc_dst, cfg->fc_dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07003898 &cfg->fc_src, cfg->fc_src_len,
Wei Wang2b760fc2017-10-06 12:06:03 -07003899 !(cfg->fc_flags & RTF_CACHE));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003900
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 if (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003902 for_each_fib6_node_rt_rcu(fn) {
David Ahernad1601a2019-03-27 20:53:56 -07003903 struct fib6_nh *nh;
3904
Stefano Brivio3401bfb2019-06-21 17:45:25 +02003905 if (rt->nh && cfg->fc_nh_id &&
3906 rt->nh->id != cfg->fc_nh_id)
David Ahern5b983242019-06-08 14:53:34 -07003907 continue;
David Ahern23fb93a2018-04-17 17:33:23 -07003908
David Ahern5b983242019-06-08 14:53:34 -07003909 if (cfg->fc_flags & RTF_CACHE) {
3910 int rc = 0;
3911
3912 if (rt->nh) {
3913 rc = ip6_del_cached_rt_nh(cfg, rt);
3914 } else if (cfg->fc_nh_id) {
3915 continue;
3916 } else {
3917 nh = rt->fib6_nh;
3918 rc = ip6_del_cached_rt(cfg, rt, nh);
3919 }
David Ahern0fa6efc2019-05-22 20:28:00 -07003920 if (rc != -ESRCH) {
3921 rcu_read_unlock();
3922 return rc;
David Ahern23fb93a2018-04-17 17:33:23 -07003923 }
3924 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07003925 }
David Ahernad1601a2019-03-27 20:53:56 -07003926
David Ahern5b983242019-06-08 14:53:34 -07003927 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
3928 continue;
3929 if (cfg->fc_protocol &&
3930 cfg->fc_protocol != rt->fib6_protocol)
3931 continue;
3932
3933 if (rt->nh) {
3934 if (!fib6_info_hold_safe(rt))
3935 continue;
3936 rcu_read_unlock();
3937
3938 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3939 }
3940 if (cfg->fc_nh_id)
3941 continue;
3942
3943 nh = rt->fib6_nh;
Thomas Graf86872cb2006-08-22 00:01:08 -07003944 if (cfg->fc_ifindex &&
David Ahernad1601a2019-03-27 20:53:56 -07003945 (!nh->fib_nh_dev ||
3946 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07003948 if (cfg->fc_flags & RTF_GATEWAY &&
David Ahernad1601a2019-03-27 20:53:56 -07003949 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07003951 if (!fib6_info_hold_safe(rt))
3952 continue;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003953 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
David Ahern0ae81332017-02-02 12:37:08 -08003955 /* if gateway was specified only delete the one hop */
3956 if (cfg->fc_flags & RTF_GATEWAY)
3957 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3958
3959 return __ip6_del_rt_siblings(rt, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 }
3961 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003962 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
3964 return err;
3965}
3966
David S. Miller6700c272012-07-17 03:29:28 -07003967static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003968{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003969 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07003970 struct rt6_info *rt, *nrt = NULL;
David Ahern85bd05d2019-04-16 14:36:01 -07003971 struct fib6_result res = {};
David S. Millere8599ff2012-07-11 23:43:53 -07003972 struct ndisc_options ndopts;
3973 struct inet6_dev *in6_dev;
3974 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003975 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07003976 int optlen, on_link;
3977 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07003978
Simon Horman29a3cad2013-05-28 20:34:26 +00003979 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003980 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07003981
3982 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07003983 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003984 return;
3985 }
3986
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003987 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07003988
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003989 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003990 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003991 return;
3992 }
3993
David S. Miller6e157b62012-07-12 00:05:02 -07003994 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003995 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003996 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003997 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07003998 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003999 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004000 return;
4001 }
4002
4003 in6_dev = __in6_dev_get(skb->dev);
4004 if (!in6_dev)
4005 return;
4006 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
4007 return;
4008
4009 /* RFC2461 8.1:
4010 * The IP source address of the Redirect MUST be the same as the current
4011 * first-hop router for the specified ICMP Destination Address.
4012 */
4013
Alexander Aringf997c552016-06-15 21:20:23 +02004014 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07004015 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4016 return;
4017 }
David S. Miller6e157b62012-07-12 00:05:02 -07004018
4019 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07004020 if (ndopts.nd_opts_tgt_lladdr) {
4021 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4022 skb->dev);
4023 if (!lladdr) {
4024 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4025 return;
4026 }
4027 }
4028
David S. Miller6e157b62012-07-12 00:05:02 -07004029 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01004030 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07004031 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4032 return;
4033 }
4034
4035 /* Redirect received -> path was valid.
4036 * Look, redirects are sent only in response to data packets,
4037 * so that this nexthop apparently is reachable. --ANK
4038 */
Julian Anastasov0dec8792017-02-06 23:14:16 +02004039 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
David S. Miller6e157b62012-07-12 00:05:02 -07004040
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004041 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07004042 if (!neigh)
4043 return;
4044
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 /*
4046 * We have finally decided to accept it.
4047 */
4048
Alexander Aringf997c552016-06-15 21:20:23 +02004049 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 NEIGH_UPDATE_F_WEAK_OVERRIDE|
4051 NEIGH_UPDATE_F_OVERRIDE|
4052 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02004053 NEIGH_UPDATE_F_ISROUTER)),
4054 NDISC_REDIRECT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
David Ahern4d85cd02018-04-20 15:37:59 -07004056 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07004057 res.f6i = rcu_dereference(rt->from);
David S. Millerff24e492019-05-02 22:14:21 -04004058 if (!res.f6i)
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004059 goto out;
David Ahern8a14e462018-04-23 11:32:07 -07004060
David Ahern49d5b8e2019-06-08 14:53:30 -07004061 if (res.f6i->nh) {
4062 struct fib6_nh_match_arg arg = {
4063 .dev = dst->dev,
4064 .gw = &rt->rt6i_gateway,
4065 };
4066
4067 nexthop_for_each_fib6_nh(res.f6i->nh,
4068 fib6_nh_find_match, &arg);
4069
4070 /* fib6_info uses a nexthop that does not have fib6_nh
4071 * using the dst->dev. Should be impossible
4072 */
4073 if (!arg.match)
4074 goto out;
4075 res.nh = arg.match;
4076 } else {
4077 res.nh = res.f6i->fib6_nh;
4078 }
4079
David Ahern7d21fec2019-04-16 14:36:11 -07004080 res.fib6_flags = res.f6i->fib6_flags;
4081 res.fib6_type = res.f6i->fib6_type;
David Ahern85bd05d2019-04-16 14:36:01 -07004082 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05004083 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 goto out;
4085
4086 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4087 if (on_link)
4088 nrt->rt6i_flags &= ~RTF_GATEWAY;
4089
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004090 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004092 /* rt6_insert_exception() will take care of duplicated exceptions */
David Ahern5012f0a2019-04-16 14:36:05 -07004093 if (rt6_insert_exception(nrt, &res)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07004094 dst_release_immediate(&nrt->dst);
4095 goto out;
4096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
Changli Gaod8d1f302010-06-10 23:31:35 -07004098 netevent.old = &rt->dst;
4099 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004100 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00004101 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07004102 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4103
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104out:
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004105 rcu_read_unlock();
David S. Millere8599ff2012-07-11 23:43:53 -07004106 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07004107}
4108
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004109#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -07004110static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004111 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004112 const struct in6_addr *gwaddr,
4113 struct net_device *dev)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004114{
David Ahern830218c2016-10-24 10:52:35 -07004115 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4116 int ifindex = dev->ifindex;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004117 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07004118 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07004119 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004120
David Ahern830218c2016-10-24 10:52:35 -07004121 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004122 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004123 return NULL;
4124
Wei Wang66f5d6c2017-10-06 12:06:10 -07004125 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07004126 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004127 if (!fn)
4128 goto out;
4129
Wei Wang66f5d6c2017-10-06 12:06:10 -07004130 for_each_fib6_node_rt_rcu(fn) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004131 /* these routes do not use nexthops */
4132 if (rt->nh)
4133 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004134 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004135 continue;
David Ahern2b2450c2019-03-27 20:53:52 -07004136 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
David Ahern1cf844c2019-05-22 20:27:59 -07004137 !rt->fib6_nh->fib_nh_gw_family)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004138 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004139 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004140 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07004141 if (!fib6_info_hold_safe(rt))
4142 continue;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004143 break;
4144 }
4145out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004146 rcu_read_unlock();
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004147 return rt;
4148}
4149
David Ahern8d1c8022018-04-17 17:33:26 -07004150static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004151 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004152 const struct in6_addr *gwaddr,
4153 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +00004154 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004155{
Thomas Graf86872cb2006-08-22 00:01:08 -07004156 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08004157 .fc_metric = IP6_RT_PRIO_USER,
David Ahern830218c2016-10-24 10:52:35 -07004158 .fc_ifindex = dev->ifindex,
Thomas Graf86872cb2006-08-22 00:01:08 -07004159 .fc_dst_len = prefixlen,
4160 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4161 RTF_UP | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004162 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004163 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004164 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08004165 .fc_nlinfo.nlh = NULL,
4166 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004167 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004168
David Ahern830218c2016-10-24 10:52:35 -07004169 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004170 cfg.fc_dst = *prefix;
4171 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07004172
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08004173 /* We should treat it as a default route if prefix length is 0. */
4174 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07004175 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004176
David Ahernacb54e32018-04-17 17:33:22 -07004177 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004178
David Ahern830218c2016-10-24 10:52:35 -07004179 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004180}
4181#endif
4182
David Ahern8d1c8022018-04-17 17:33:26 -07004183struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004184 const struct in6_addr *addr,
4185 struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004186{
David Ahern830218c2016-10-24 10:52:35 -07004187 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
David Ahern8d1c8022018-04-17 17:33:26 -07004188 struct fib6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07004189 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
David Ahernafb1d4b52018-04-17 17:33:11 -07004191 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004192 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004193 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
Wei Wang66f5d6c2017-10-06 12:06:10 -07004195 rcu_read_lock();
4196 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004197 struct fib6_nh *nh;
David Ahernad1601a2019-03-27 20:53:56 -07004198
David Ahernf88d8ea2019-06-03 20:19:52 -07004199 /* RA routes do not use nexthops */
4200 if (rt->nh)
4201 continue;
4202
4203 nh = rt->fib6_nh;
David Ahernad1601a2019-03-27 20:53:56 -07004204 if (dev == nh->fib_nh_dev &&
David Ahern93c2fb22018-04-18 15:38:59 -07004205 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Ahernad1601a2019-03-27 20:53:56 -07004206 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 break;
4208 }
Wei Wange873e4b2018-07-21 20:56:32 -07004209 if (rt && !fib6_info_hold_safe(rt))
4210 rt = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07004211 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 return rt;
4213}
4214
David Ahern8d1c8022018-04-17 17:33:26 -07004215struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004216 const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08004217 struct net_device *dev,
4218 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219{
Thomas Graf86872cb2006-08-22 00:01:08 -07004220 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07004221 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08004222 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07004223 .fc_ifindex = dev->ifindex,
4224 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4225 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004226 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004227 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004228 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08004229 .fc_nlinfo.nlh = NULL,
David Ahernafb1d4b52018-04-17 17:33:11 -07004230 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004231 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004233 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
David Ahernacb54e32018-04-17 17:33:22 -07004235 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
David Ahern830218c2016-10-24 10:52:35 -07004236 struct fib6_table *table;
4237
4238 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4239 if (table)
4240 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242
David Ahernafb1d4b52018-04-17 17:33:11 -07004243 return rt6_get_dflt_router(net, gwaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244}
4245
David Ahernafb1d4b52018-04-17 17:33:11 -07004246static void __rt6_purge_dflt_routers(struct net *net,
4247 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248{
David Ahern8d1c8022018-04-17 17:33:26 -07004249 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
4251restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004252 rcu_read_lock();
4253 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Aherndcd1f572018-04-18 15:39:05 -07004254 struct net_device *dev = fib6_info_nh_dev(rt);
4255 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4256
David Ahern93c2fb22018-04-18 15:38:59 -07004257 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
Wei Wange873e4b2018-07-21 20:56:32 -07004258 (!idev || idev->cnf.accept_ra != 2) &&
4259 fib6_info_hold_safe(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07004260 rcu_read_unlock();
4261 ip6_del_rt(net, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 goto restart;
4263 }
4264 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07004265 rcu_read_unlock();
David Ahern830218c2016-10-24 10:52:35 -07004266
4267 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4268}
4269
4270void rt6_purge_dflt_routers(struct net *net)
4271{
4272 struct fib6_table *table;
4273 struct hlist_head *head;
4274 unsigned int h;
4275
4276 rcu_read_lock();
4277
4278 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4279 head = &net->ipv6.fib_table_hash[h];
4280 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4281 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
David Ahernafb1d4b52018-04-17 17:33:11 -07004282 __rt6_purge_dflt_routers(net, table);
David Ahern830218c2016-10-24 10:52:35 -07004283 }
4284 }
4285
4286 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287}
4288
Daniel Lezcano55786892008-03-04 13:47:47 -08004289static void rtmsg_to_fib6_config(struct net *net,
4290 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07004291 struct fib6_config *cfg)
4292{
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004293 *cfg = (struct fib6_config){
4294 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4295 : RT6_TABLE_MAIN,
4296 .fc_ifindex = rtmsg->rtmsg_ifindex,
David Ahern67f69512019-03-21 05:21:34 -07004297 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004298 .fc_expires = rtmsg->rtmsg_info,
4299 .fc_dst_len = rtmsg->rtmsg_dst_len,
4300 .fc_src_len = rtmsg->rtmsg_src_len,
4301 .fc_flags = rtmsg->rtmsg_flags,
4302 .fc_type = rtmsg->rtmsg_type,
Thomas Graf86872cb2006-08-22 00:01:08 -07004303
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004304 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004305
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004306 .fc_dst = rtmsg->rtmsg_dst,
4307 .fc_src = rtmsg->rtmsg_src,
4308 .fc_gateway = rtmsg->rtmsg_gateway,
4309 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004310}
4311
Daniel Lezcano55786892008-03-04 13:47:47 -08004312int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
Thomas Graf86872cb2006-08-22 00:01:08 -07004314 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 struct in6_rtmsg rtmsg;
4316 int err;
4317
Ian Morris67ba4152014-08-24 21:53:10 +01004318 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 case SIOCADDRT: /* Add a route */
4320 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00004321 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 return -EPERM;
4323 err = copy_from_user(&rtmsg, arg,
4324 sizeof(struct in6_rtmsg));
4325 if (err)
4326 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07004327
Daniel Lezcano55786892008-03-04 13:47:47 -08004328 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07004329
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 rtnl_lock();
4331 switch (cmd) {
4332 case SIOCADDRT:
David Ahernacb54e32018-04-17 17:33:22 -07004333 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 break;
4335 case SIOCDELRT:
David Ahern333c4302017-05-21 10:12:04 -06004336 err = ip6_route_del(&cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 break;
4338 default:
4339 err = -EINVAL;
4340 }
4341 rtnl_unlock();
4342
4343 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
4346 return -EINVAL;
4347}
4348
4349/*
4350 * Drop the packet on the floor
4351 */
4352
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07004353static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354{
Eric Dumazetadf30902009-06-02 05:19:30 +00004355 struct dst_entry *dst = skb_dst(skb);
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004356 struct net *net = dev_net(dst->dev);
4357 struct inet6_dev *idev;
4358 int type;
4359
4360 if (netif_is_l3_master(skb->dev) &&
4361 dst->dev == net->loopback_dev)
4362 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4363 else
4364 idev = ip6_dst_idev(dst);
4365
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004366 switch (ipstats_mib_noroutes) {
4367 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07004368 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00004369 if (type == IPV6_ADDR_ANY) {
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004370 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004371 break;
4372 }
4373 /* FALLTHROUGH */
4374 case IPSTATS_MIB_OUTNOROUTES:
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004375 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004376 break;
4377 }
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004378
4379 /* Start over by dropping the dst for l3mdev case */
4380 if (netif_is_l3_master(skb->dev))
4381 skb_dst_drop(skb);
4382
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00004383 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 kfree_skb(skb);
4385 return 0;
4386}
4387
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004388static int ip6_pkt_discard(struct sk_buff *skb)
4389{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004390 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004391}
4392
Eric W. Biedermanede20592015-10-07 16:48:47 -05004393static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394{
Eric Dumazetadf30902009-06-02 05:19:30 +00004395 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004396 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397}
4398
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004399static int ip6_pkt_prohibit(struct sk_buff *skb)
4400{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004401 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004402}
4403
Eric W. Biedermanede20592015-10-07 16:48:47 -05004404static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004405{
Eric Dumazetadf30902009-06-02 05:19:30 +00004406 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004407 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004408}
4409
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410/*
4411 * Allocate a dst for local (unicast / anycast) address.
4412 */
4413
David Ahern360a9882018-04-18 15:39:00 -07004414struct fib6_info *addrconf_f6i_alloc(struct net *net,
4415 struct inet6_dev *idev,
4416 const struct in6_addr *addr,
4417 bool anycast, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418{
David Ahernc7a1ce32019-03-21 05:21:35 -07004419 struct fib6_config cfg = {
4420 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4421 .fc_ifindex = idev->dev->ifindex,
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004422 .fc_flags = RTF_UP | RTF_NONEXTHOP,
David Ahernc7a1ce32019-03-21 05:21:35 -07004423 .fc_dst = *addr,
4424 .fc_dst_len = 128,
4425 .fc_protocol = RTPROT_KERNEL,
4426 .fc_nlinfo.nl_net = net,
4427 .fc_ignore_dev_down = true,
4428 };
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004429 struct fib6_info *f6i;
David Ahern5f02ce242016-09-10 12:09:54 -07004430
David Aherne8478e82018-04-17 17:33:13 -07004431 if (anycast) {
David Ahernc7a1ce32019-03-21 05:21:35 -07004432 cfg.fc_type = RTN_ANYCAST;
4433 cfg.fc_flags |= RTF_ANYCAST;
David Aherne8478e82018-04-17 17:33:13 -07004434 } else {
David Ahernc7a1ce32019-03-21 05:21:35 -07004435 cfg.fc_type = RTN_LOCAL;
4436 cfg.fc_flags |= RTF_LOCAL;
David Aherne8478e82018-04-17 17:33:13 -07004437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004439 f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
Maciej Żenczykowski8652f172019-09-05 20:56:37 -07004440 if (!IS_ERR(f6i))
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004441 f6i->dst_nocount = true;
4442 return f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443}
4444
Daniel Walterc3968a82011-04-13 21:10:57 +00004445/* remove deleted ip from prefsrc entries */
4446struct arg_dev_net_ip {
4447 struct net_device *dev;
4448 struct net *net;
4449 struct in6_addr *addr;
4450};
4451
David Ahern8d1c8022018-04-17 17:33:26 -07004452static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
Daniel Walterc3968a82011-04-13 21:10:57 +00004453{
4454 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4455 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4456 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4457
David Ahernf88d8ea2019-06-03 20:19:52 -07004458 if (!rt->nh &&
4459 ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
David Ahern421842e2018-04-17 17:33:18 -07004460 rt != net->ipv6.fib6_null_entry &&
David Ahern93c2fb22018-04-18 15:38:59 -07004461 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
Wei Wang60006a42017-10-06 12:05:58 -07004462 spin_lock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004463 /* remove prefsrc entry */
David Ahern93c2fb22018-04-18 15:38:59 -07004464 rt->fib6_prefsrc.plen = 0;
Wei Wang60006a42017-10-06 12:05:58 -07004465 spin_unlock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004466 }
4467 return 0;
4468}
4469
4470void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4471{
4472 struct net *net = dev_net(ifp->idev->dev);
4473 struct arg_dev_net_ip adni = {
4474 .dev = ifp->idev->dev,
4475 .net = net,
4476 .addr = &ifp->addr,
4477 };
Li RongQing0c3584d2013-12-27 16:32:38 +08004478 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00004479}
4480
David Ahern2b2450c2019-03-27 20:53:52 -07004481#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004482
4483/* Remove routers and update dst entries when gateway turn into host. */
David Ahern8d1c8022018-04-17 17:33:26 -07004484static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004485{
4486 struct in6_addr *gateway = (struct in6_addr *)arg;
David Ahernf88d8ea2019-06-03 20:19:52 -07004487 struct fib6_nh *nh;
Duan Jiongbe7a0102014-05-15 15:56:14 +08004488
David Ahernf88d8ea2019-06-03 20:19:52 -07004489 /* RA routes do not use nexthops */
4490 if (rt->nh)
4491 return 0;
4492
4493 nh = rt->fib6_nh;
David Ahern93c2fb22018-04-18 15:38:59 -07004494 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Aherncc5c0732019-05-22 20:27:58 -07004495 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
Duan Jiongbe7a0102014-05-15 15:56:14 +08004496 return -1;
Wei Wangb16cb452017-10-06 12:06:00 -07004497
4498 /* Further clean up cached routes in exception table.
4499 * This is needed because cached route may have a different
4500 * gateway than its 'parent' in the case of an ip redirect.
4501 */
David Aherncc5c0732019-05-22 20:27:58 -07004502 fib6_nh_exceptions_clean_tohost(nh, gateway);
Wei Wangb16cb452017-10-06 12:06:00 -07004503
Duan Jiongbe7a0102014-05-15 15:56:14 +08004504 return 0;
4505}
4506
4507void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4508{
4509 fib6_clean_all(net, fib6_clean_tohost, gateway);
4510}
4511
Ido Schimmel2127d952018-01-07 12:45:03 +02004512struct arg_netdev_event {
4513 const struct net_device *dev;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004514 union {
David Ahernecc56632019-04-23 08:48:09 -07004515 unsigned char nh_flags;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004516 unsigned long event;
4517 };
Ido Schimmel2127d952018-01-07 12:45:03 +02004518};
4519
David Ahern8d1c8022018-04-17 17:33:26 -07004520static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004521{
David Ahern8d1c8022018-04-17 17:33:26 -07004522 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004523 struct fib6_node *fn;
4524
David Ahern93c2fb22018-04-18 15:38:59 -07004525 fn = rcu_dereference_protected(rt->fib6_node,
4526 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004527 iter = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -07004528 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004529 while (iter) {
David Ahern93c2fb22018-04-18 15:38:59 -07004530 if (iter->fib6_metric == rt->fib6_metric &&
David Ahern33bd5ac2018-07-03 14:36:21 -07004531 rt6_qualify_for_ecmp(iter))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004532 return iter;
David Ahern8fb11a92018-05-04 13:54:24 -07004533 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -07004534 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004535 }
4536
4537 return NULL;
4538}
4539
David Ahernf88d8ea2019-06-03 20:19:52 -07004540/* only called for fib entries with builtin fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004541static bool rt6_is_dead(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004542{
David Ahern1cf844c2019-05-22 20:27:59 -07004543 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4544 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4545 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004546 return true;
4547
4548 return false;
4549}
4550
David Ahern8d1c8022018-04-17 17:33:26 -07004551static int rt6_multipath_total_weight(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004552{
David Ahern8d1c8022018-04-17 17:33:26 -07004553 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004554 int total = 0;
4555
4556 if (!rt6_is_dead(rt))
David Ahern1cf844c2019-05-22 20:27:59 -07004557 total += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004558
David Ahern93c2fb22018-04-18 15:38:59 -07004559 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004560 if (!rt6_is_dead(iter))
David Ahern1cf844c2019-05-22 20:27:59 -07004561 total += iter->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004562 }
4563
4564 return total;
4565}
4566
David Ahern8d1c8022018-04-17 17:33:26 -07004567static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004568{
4569 int upper_bound = -1;
4570
4571 if (!rt6_is_dead(rt)) {
David Ahern1cf844c2019-05-22 20:27:59 -07004572 *weight += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004573 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4574 total) - 1;
4575 }
David Ahern1cf844c2019-05-22 20:27:59 -07004576 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004577}
4578
David Ahern8d1c8022018-04-17 17:33:26 -07004579static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004580{
David Ahern8d1c8022018-04-17 17:33:26 -07004581 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004582 int weight = 0;
4583
4584 rt6_upper_bound_set(rt, &weight, total);
4585
David Ahern93c2fb22018-04-18 15:38:59 -07004586 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004587 rt6_upper_bound_set(iter, &weight, total);
4588}
4589
David Ahern8d1c8022018-04-17 17:33:26 -07004590void rt6_multipath_rebalance(struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004591{
David Ahern8d1c8022018-04-17 17:33:26 -07004592 struct fib6_info *first;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004593 int total;
4594
4595 /* In case the entire multipath route was marked for flushing,
4596 * then there is no need to rebalance upon the removal of every
4597 * sibling route.
4598 */
David Ahern93c2fb22018-04-18 15:38:59 -07004599 if (!rt->fib6_nsiblings || rt->should_flush)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004600 return;
4601
4602 /* During lookup routes are evaluated in order, so we need to
4603 * make sure upper bounds are assigned from the first sibling
4604 * onwards.
4605 */
4606 first = rt6_multipath_first_sibling(rt);
4607 if (WARN_ON_ONCE(!first))
4608 return;
4609
4610 total = rt6_multipath_total_weight(first);
4611 rt6_multipath_upper_bound_set(first, total);
4612}
4613
David Ahern8d1c8022018-04-17 17:33:26 -07004614static int fib6_ifup(struct fib6_info *rt, void *p_arg)
Ido Schimmel2127d952018-01-07 12:45:03 +02004615{
4616 const struct arg_netdev_event *arg = p_arg;
David Ahern7aef6852018-04-17 17:33:10 -07004617 struct net *net = dev_net(arg->dev);
Ido Schimmel2127d952018-01-07 12:45:03 +02004618
David Ahernf88d8ea2019-06-03 20:19:52 -07004619 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
David Ahern1cf844c2019-05-22 20:27:59 -07004620 rt->fib6_nh->fib_nh_dev == arg->dev) {
4621 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
David Ahern7aef6852018-04-17 17:33:10 -07004622 fib6_update_sernum_upto_root(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004623 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004624 }
Ido Schimmel2127d952018-01-07 12:45:03 +02004625
4626 return 0;
4627}
4628
David Ahernecc56632019-04-23 08:48:09 -07004629void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
Ido Schimmel2127d952018-01-07 12:45:03 +02004630{
4631 struct arg_netdev_event arg = {
4632 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004633 {
4634 .nh_flags = nh_flags,
4635 },
Ido Schimmel2127d952018-01-07 12:45:03 +02004636 };
4637
4638 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4639 arg.nh_flags |= RTNH_F_LINKDOWN;
4640
4641 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4642}
4643
David Ahernf88d8ea2019-06-03 20:19:52 -07004644/* only called for fib entries with inline fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004645static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004646 const struct net_device *dev)
4647{
David Ahern8d1c8022018-04-17 17:33:26 -07004648 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004649
David Ahern1cf844c2019-05-22 20:27:59 -07004650 if (rt->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004651 return true;
David Ahern93c2fb22018-04-18 15:38:59 -07004652 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004653 if (iter->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004654 return true;
4655
4656 return false;
4657}
4658
David Ahern8d1c8022018-04-17 17:33:26 -07004659static void rt6_multipath_flush(struct fib6_info *rt)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004660{
David Ahern8d1c8022018-04-17 17:33:26 -07004661 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004662
4663 rt->should_flush = 1;
David Ahern93c2fb22018-04-18 15:38:59 -07004664 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004665 iter->should_flush = 1;
4666}
4667
David Ahern8d1c8022018-04-17 17:33:26 -07004668static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004669 const struct net_device *down_dev)
4670{
David Ahern8d1c8022018-04-17 17:33:26 -07004671 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004672 unsigned int dead = 0;
4673
David Ahern1cf844c2019-05-22 20:27:59 -07004674 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4675 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004676 dead++;
David Ahern93c2fb22018-04-18 15:38:59 -07004677 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004678 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4679 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004680 dead++;
4681
4682 return dead;
4683}
4684
David Ahern8d1c8022018-04-17 17:33:26 -07004685static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004686 const struct net_device *dev,
David Ahernecc56632019-04-23 08:48:09 -07004687 unsigned char nh_flags)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004688{
David Ahern8d1c8022018-04-17 17:33:26 -07004689 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004690
David Ahern1cf844c2019-05-22 20:27:59 -07004691 if (rt->fib6_nh->fib_nh_dev == dev)
4692 rt->fib6_nh->fib_nh_flags |= nh_flags;
David Ahern93c2fb22018-04-18 15:38:59 -07004693 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004694 if (iter->fib6_nh->fib_nh_dev == dev)
4695 iter->fib6_nh->fib_nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004696}
4697
David Aherna1a22c12017-01-18 07:40:36 -08004698/* called with write lock held for table with rt */
David Ahern8d1c8022018-04-17 17:33:26 -07004699static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004701 const struct arg_netdev_event *arg = p_arg;
4702 const struct net_device *dev = arg->dev;
David Ahern7aef6852018-04-17 17:33:10 -07004703 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004704
David Ahernf88d8ea2019-06-03 20:19:52 -07004705 if (rt == net->ipv6.fib6_null_entry || rt->nh)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004706 return 0;
4707
4708 switch (arg->event) {
4709 case NETDEV_UNREGISTER:
David Ahern1cf844c2019-05-22 20:27:59 -07004710 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004711 case NETDEV_DOWN:
Ido Schimmel1de178e2018-01-07 12:45:15 +02004712 if (rt->should_flush)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004713 return -1;
David Ahern93c2fb22018-04-18 15:38:59 -07004714 if (!rt->fib6_nsiblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004715 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004716 if (rt6_multipath_uses_dev(rt, dev)) {
4717 unsigned int count;
4718
4719 count = rt6_multipath_dead_count(rt, dev);
David Ahern93c2fb22018-04-18 15:38:59 -07004720 if (rt->fib6_nsiblings + 1 == count) {
Ido Schimmel1de178e2018-01-07 12:45:15 +02004721 rt6_multipath_flush(rt);
4722 return -1;
4723 }
4724 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4725 RTNH_F_LINKDOWN);
David Ahern7aef6852018-04-17 17:33:10 -07004726 fib6_update_sernum(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004727 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004728 }
4729 return -2;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004730 case NETDEV_CHANGE:
David Ahern1cf844c2019-05-22 20:27:59 -07004731 if (rt->fib6_nh->fib_nh_dev != dev ||
David Ahern93c2fb22018-04-18 15:38:59 -07004732 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004733 break;
David Ahern1cf844c2019-05-22 20:27:59 -07004734 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004735 rt6_multipath_rebalance(rt);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004736 break;
Ido Schimmel2b241362018-01-07 12:45:02 +02004737 }
David S. Millerc159d302011-12-26 15:24:36 -05004738
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 return 0;
4740}
4741
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004742void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004744 struct arg_netdev_event arg = {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004745 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004746 {
4747 .event = event,
4748 },
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004749 };
David Ahern7c6bb7d2018-10-11 20:17:21 -07004750 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004751
David Ahern7c6bb7d2018-10-11 20:17:21 -07004752 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4753 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4754 else
4755 fib6_clean_all(net, fib6_ifdown, &arg);
Ido Schimmel4c981e22018-01-07 12:45:04 +02004756}
4757
4758void rt6_disable_ip(struct net_device *dev, unsigned long event)
4759{
4760 rt6_sync_down_dev(dev, event);
4761 rt6_uncached_list_flush_dev(dev_net(dev), dev);
4762 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763}
4764
Eric Dumazet95c96172012-04-15 05:58:06 +00004765struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00004767 unsigned int mtu;
David Ahernc0b220c2019-05-22 20:27:57 -07004768 struct fib6_info *f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769};
4770
David Aherncc5c0732019-05-22 20:27:58 -07004771static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
David Ahernc0b220c2019-05-22 20:27:57 -07004772{
4773 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
David Aherncc5c0732019-05-22 20:27:58 -07004774 struct fib6_info *f6i = arg->f6i;
David Ahernc0b220c2019-05-22 20:27:57 -07004775
4776 /* For administrative MTU increase, there is no way to discover
4777 * IPv6 PMTU increase, so PMTU increase should be updated here.
4778 * Since RFC 1981 doesn't include administrative MTU increase
4779 * update PMTU increase is a MUST. (i.e. jumbo frame)
4780 */
4781 if (nh->fib_nh_dev == arg->dev) {
4782 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4783 u32 mtu = f6i->fib6_pmtu;
4784
4785 if (mtu >= arg->mtu ||
4786 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4787 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4788
4789 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07004790 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
David Ahernc0b220c2019-05-22 20:27:57 -07004791 spin_unlock_bh(&rt6_exception_lock);
4792 }
4793
4794 return 0;
4795}
4796
4797static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798{
4799 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4800 struct inet6_dev *idev;
4801
4802 /* In IPv6 pmtu discovery is not optional,
4803 so that RTAX_MTU lock cannot disable it.
4804 We still use this lock to block changes
4805 caused by addrconf/ndisc.
4806 */
4807
4808 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05004809 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 return 0;
4811
David Ahernc0b220c2019-05-22 20:27:57 -07004812 if (fib6_metric_locked(f6i, RTAX_MTU))
4813 return 0;
David Ahernd4ead6b2018-04-17 17:33:16 -07004814
David Ahernc0b220c2019-05-22 20:27:57 -07004815 arg->f6i = f6i;
David Ahern2d442342019-06-08 14:53:31 -07004816 if (f6i->nh) {
4817 /* fib6_nh_mtu_change only returns 0, so this is safe */
4818 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4819 arg);
4820 }
4821
David Ahern1cf844c2019-05-22 20:27:59 -07004822 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823}
4824
Eric Dumazet95c96172012-04-15 05:58:06 +00004825void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826{
Thomas Grafc71099a2006-08-04 23:20:06 -07004827 struct rt6_mtu_change_arg arg = {
4828 .dev = dev,
4829 .mtu = mtu,
4830 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831
Li RongQing0c3584d2013-12-27 16:32:38 +08004832 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833}
4834
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004835static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
David Ahern75425652019-05-22 12:07:43 -07004836 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
Thomas Graf5176f912006-08-26 20:13:18 -07004837 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004838 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07004839 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07004840 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004841 [RTA_PRIORITY] = { .type = NLA_U32 },
4842 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004843 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004844 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004845 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4846 [RTA_ENCAP] = { .type = NLA_NESTED },
Xin Long32bc2012015-12-16 17:50:11 +08004847 [RTA_EXPIRES] = { .type = NLA_U32 },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004848 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +08004849 [RTA_MARK] = { .type = NLA_U32 },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004850 [RTA_TABLE] = { .type = NLA_U32 },
Roopa Prabhueacb9382018-05-22 14:03:28 -07004851 [RTA_IP_PROTO] = { .type = NLA_U8 },
4852 [RTA_SPORT] = { .type = NLA_U16 },
4853 [RTA_DPORT] = { .type = NLA_U16 },
David Ahern5b983242019-06-08 14:53:34 -07004854 [RTA_NH_ID] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004855};
4856
4857static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
David Ahern333c4302017-05-21 10:12:04 -06004858 struct fib6_config *cfg,
4859 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860{
Thomas Graf86872cb2006-08-22 00:01:08 -07004861 struct rtmsg *rtm;
4862 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004863 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07004864 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
Johannes Berg8cb08172019-04-26 14:07:28 +02004866 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
4867 rtm_ipv6_policy, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004868 if (err < 0)
4869 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870
Thomas Graf86872cb2006-08-22 00:01:08 -07004871 err = -EINVAL;
4872 rtm = nlmsg_data(nlh);
Thomas Graf86872cb2006-08-22 00:01:08 -07004873
Maciej Żenczykowski84db8402018-09-29 23:44:53 -07004874 *cfg = (struct fib6_config){
4875 .fc_table = rtm->rtm_table,
4876 .fc_dst_len = rtm->rtm_dst_len,
4877 .fc_src_len = rtm->rtm_src_len,
4878 .fc_flags = RTF_UP,
4879 .fc_protocol = rtm->rtm_protocol,
4880 .fc_type = rtm->rtm_type,
4881
4882 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
4883 .fc_nlinfo.nlh = nlh,
4884 .fc_nlinfo.nl_net = sock_net(skb->sk),
4885 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004886
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004887 if (rtm->rtm_type == RTN_UNREACHABLE ||
4888 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00004889 rtm->rtm_type == RTN_PROHIBIT ||
4890 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07004891 cfg->fc_flags |= RTF_REJECT;
4892
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00004893 if (rtm->rtm_type == RTN_LOCAL)
4894 cfg->fc_flags |= RTF_LOCAL;
4895
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07004896 if (rtm->rtm_flags & RTM_F_CLONED)
4897 cfg->fc_flags |= RTF_CACHE;
4898
David Ahernfc1e64e2018-01-25 16:55:09 -08004899 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4900
David Ahern5b983242019-06-08 14:53:34 -07004901 if (tb[RTA_NH_ID]) {
4902 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
4903 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
4904 NL_SET_ERR_MSG(extack,
4905 "Nexthop specification and nexthop id are mutually exclusive");
4906 goto errout;
4907 }
4908 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
4909 }
4910
Thomas Graf86872cb2006-08-22 00:01:08 -07004911 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004912 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07004913 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 }
David Aherne3818542019-02-26 09:00:03 -08004915 if (tb[RTA_VIA]) {
4916 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4917 goto errout;
4918 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004919
4920 if (tb[RTA_DST]) {
4921 int plen = (rtm->rtm_dst_len + 7) >> 3;
4922
4923 if (nla_len(tb[RTA_DST]) < plen)
4924 goto errout;
4925
4926 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004928
4929 if (tb[RTA_SRC]) {
4930 int plen = (rtm->rtm_src_len + 7) >> 3;
4931
4932 if (nla_len(tb[RTA_SRC]) < plen)
4933 goto errout;
4934
4935 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004937
Daniel Walterc3968a82011-04-13 21:10:57 +00004938 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02004939 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00004940
Thomas Graf86872cb2006-08-22 00:01:08 -07004941 if (tb[RTA_OIF])
4942 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4943
4944 if (tb[RTA_PRIORITY])
4945 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4946
4947 if (tb[RTA_METRICS]) {
4948 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4949 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004951
4952 if (tb[RTA_TABLE])
4953 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4954
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004955 if (tb[RTA_MULTIPATH]) {
4956 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4957 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
David Ahern9ed59592017-01-17 14:57:36 -08004958
4959 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
David Ahernc255bd62017-05-27 16:19:27 -06004960 cfg->fc_mp_len, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004961 if (err < 0)
4962 goto errout;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004963 }
4964
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004965 if (tb[RTA_PREF]) {
4966 pref = nla_get_u8(tb[RTA_PREF]);
4967 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4968 pref != ICMPV6_ROUTER_PREF_HIGH)
4969 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4970 cfg->fc_flags |= RTF_PREF(pref);
4971 }
4972
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004973 if (tb[RTA_ENCAP])
4974 cfg->fc_encap = tb[RTA_ENCAP];
4975
David Ahern9ed59592017-01-17 14:57:36 -08004976 if (tb[RTA_ENCAP_TYPE]) {
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004977 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
4978
David Ahernc255bd62017-05-27 16:19:27 -06004979 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004980 if (err < 0)
4981 goto errout;
4982 }
4983
Xin Long32bc2012015-12-16 17:50:11 +08004984 if (tb[RTA_EXPIRES]) {
4985 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
4986
4987 if (addrconf_finite_timeout(timeout)) {
4988 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
4989 cfg->fc_flags |= RTF_EXPIRES;
4990 }
4991 }
4992
Thomas Graf86872cb2006-08-22 00:01:08 -07004993 err = 0;
4994errout:
4995 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996}
4997
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004998struct rt6_nh {
David Ahern8d1c8022018-04-17 17:33:26 -07004999 struct fib6_info *fib6_info;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005000 struct fib6_config r_cfg;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005001 struct list_head next;
5002};
5003
David Ahernd4ead6b2018-04-17 17:33:16 -07005004static int ip6_route_info_append(struct net *net,
5005 struct list_head *rt6_nh_list,
David Ahern8d1c8022018-04-17 17:33:26 -07005006 struct fib6_info *rt,
5007 struct fib6_config *r_cfg)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005008{
5009 struct rt6_nh *nh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005010 int err = -EEXIST;
5011
5012 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005013 /* check if fib6_info already exists */
5014 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005015 return err;
5016 }
5017
5018 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
5019 if (!nh)
5020 return -ENOMEM;
David Ahern8d1c8022018-04-17 17:33:26 -07005021 nh->fib6_info = rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005022 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
5023 list_add_tail(&nh->next, rt6_nh_list);
5024
5025 return 0;
5026}
5027
David Ahern8d1c8022018-04-17 17:33:26 -07005028static void ip6_route_mpath_notify(struct fib6_info *rt,
5029 struct fib6_info *rt_last,
David Ahern3b1137f2017-02-02 12:37:10 -08005030 struct nl_info *info,
5031 __u16 nlflags)
5032{
5033 /* if this is an APPEND route, then rt points to the first route
5034 * inserted and rt_last points to last route inserted. Userspace
5035 * wants a consistent dump of the route which starts at the first
5036 * nexthop. Since sibling routes are always added at the end of
5037 * the list, find the first sibling of the last route appended
5038 */
David Ahern93c2fb22018-04-18 15:38:59 -07005039 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
5040 rt = list_first_entry(&rt_last->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -07005041 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -07005042 fib6_siblings);
David Ahern3b1137f2017-02-02 12:37:10 -08005043 }
5044
5045 if (rt)
5046 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5047}
5048
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005049static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
5050{
5051 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
5052 bool should_notify = false;
5053 struct fib6_info *leaf;
5054 struct fib6_node *fn;
5055
5056 rcu_read_lock();
5057 fn = rcu_dereference(rt->fib6_node);
5058 if (!fn)
5059 goto out;
5060
5061 leaf = rcu_dereference(fn->leaf);
5062 if (!leaf)
5063 goto out;
5064
5065 if (rt == leaf ||
5066 (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
5067 rt6_qualify_for_ecmp(leaf)))
5068 should_notify = true;
5069out:
5070 rcu_read_unlock();
5071
5072 return should_notify;
5073}
5074
David Ahern333c4302017-05-21 10:12:04 -06005075static int ip6_route_multipath_add(struct fib6_config *cfg,
5076 struct netlink_ext_ack *extack)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005077{
David Ahern8d1c8022018-04-17 17:33:26 -07005078 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
David Ahern3b1137f2017-02-02 12:37:10 -08005079 struct nl_info *info = &cfg->fc_nlinfo;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005080 struct fib6_config r_cfg;
5081 struct rtnexthop *rtnh;
David Ahern8d1c8022018-04-17 17:33:26 -07005082 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005083 struct rt6_nh *err_nh;
5084 struct rt6_nh *nh, *nh_safe;
David Ahern3b1137f2017-02-02 12:37:10 -08005085 __u16 nlflags;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005086 int remaining;
5087 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005088 int err = 1;
5089 int nhn = 0;
5090 int replace = (cfg->fc_nlinfo.nlh &&
5091 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5092 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005093
David Ahern3b1137f2017-02-02 12:37:10 -08005094 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5095 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5096 nlflags |= NLM_F_APPEND;
5097
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02005098 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005099 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005100
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005101 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
David Ahern8d1c8022018-04-17 17:33:26 -07005102 * fib6_info structs per nexthop
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005103 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005104 while (rtnh_ok(rtnh, remaining)) {
5105 memcpy(&r_cfg, cfg, sizeof(*cfg));
5106 if (rtnh->rtnh_ifindex)
5107 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5108
5109 attrlen = rtnh_attrlen(rtnh);
5110 if (attrlen > 0) {
5111 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5112
5113 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5114 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02005115 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005116 r_cfg.fc_flags |= RTF_GATEWAY;
5117 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005118 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5119 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5120 if (nla)
5121 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005122 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005123
David Ahern68e2ffd2018-03-20 10:06:59 -07005124 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
David Ahernacb54e32018-04-17 17:33:22 -07005125 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005126 if (IS_ERR(rt)) {
5127 err = PTR_ERR(rt);
5128 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005129 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005130 }
David Ahernb5d2d752018-07-15 09:35:19 -07005131 if (!rt6_qualify_for_ecmp(rt)) {
5132 err = -EINVAL;
5133 NL_SET_ERR_MSG(extack,
5134 "Device only routes can not be added for IPv6 using the multipath API.");
5135 fib6_info_release(rt);
5136 goto cleanup;
5137 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005138
David Ahern1cf844c2019-05-22 20:27:59 -07005139 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
Ido Schimmel398958a2018-01-09 16:40:28 +02005140
David Ahernd4ead6b2018-04-17 17:33:16 -07005141 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5142 rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005143 if (err) {
David Ahern93531c62018-04-17 17:33:25 -07005144 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005145 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005146 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005147
5148 rtnh = rtnh_next(rtnh, &remaining);
5149 }
5150
Ido Schimmel9eee3b42019-06-20 12:10:21 +03005151 if (list_empty(&rt6_nh_list)) {
5152 NL_SET_ERR_MSG(extack,
5153 "Invalid nexthop configuration - no valid nexthops");
5154 return -EINVAL;
5155 }
5156
David Ahern3b1137f2017-02-02 12:37:10 -08005157 /* for add and replace send one notification with all nexthops.
5158 * Skip the notification in fib6_add_rt2node and send one with
5159 * the full route when done
5160 */
5161 info->skip_notify = 1;
5162
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005163 /* For add and replace, send one notification with all nexthops. For
5164 * append, send one notification with all appended nexthops.
5165 */
5166 info->skip_notify_kernel = 1;
5167
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005168 err_nh = NULL;
5169 list_for_each_entry(nh, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005170 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5171 fib6_info_release(nh->fib6_info);
David Ahern3b1137f2017-02-02 12:37:10 -08005172
David Ahernf7225172018-06-04 13:41:42 -07005173 if (!err) {
5174 /* save reference to last route successfully inserted */
5175 rt_last = nh->fib6_info;
5176
5177 /* save reference to first route for notification */
5178 if (!rt_notif)
5179 rt_notif = nh->fib6_info;
5180 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005181
David Ahern8d1c8022018-04-17 17:33:26 -07005182 /* nh->fib6_info is used or freed at this point, reset to NULL*/
5183 nh->fib6_info = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005184 if (err) {
5185 if (replace && nhn)
Jakub Kicinskia5a82d82019-01-14 10:52:45 -08005186 NL_SET_ERR_MSG_MOD(extack,
5187 "multipath route replace failed (check consistency of installed routes)");
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005188 err_nh = nh;
5189 goto add_errout;
5190 }
5191
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005192 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02005193 * these flags after the first nexthop: if there is a collision,
5194 * we have already failed to add the first nexthop:
5195 * fib6_add_rt2node() has rejected it; when replacing, old
5196 * nexthops have been replaced by first new, the rest should
5197 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005198 */
Michal Kubeček27596472015-05-18 20:54:00 +02005199 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5200 NLM_F_REPLACE);
Benjamin Poirierafecdb32020-02-12 10:41:07 +09005201 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005202 nhn++;
5203 }
5204
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005205 /* An in-kernel notification should only be sent in case the new
5206 * multipath route is added as the first route in the node, or if
5207 * it was appended to it. We pass 'rt_notif' since it is the first
5208 * sibling and might allow us to skip some checks in the replace case.
5209 */
5210 if (ip6_route_mpath_should_notify(rt_notif)) {
5211 enum fib_event_type fib_event;
5212
5213 if (rt_notif->fib6_nsiblings != nhn - 1)
5214 fib_event = FIB_EVENT_ENTRY_APPEND;
5215 else
Ido Schimmelcaafb252019-12-23 15:28:20 +02005216 fib_event = FIB_EVENT_ENTRY_REPLACE;
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005217
5218 err = call_fib6_multipath_entry_notifiers(info->nl_net,
5219 fib_event, rt_notif,
5220 nhn - 1, extack);
5221 if (err) {
5222 /* Delete all the siblings that were just added */
5223 err_nh = NULL;
5224 goto add_errout;
5225 }
5226 }
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005227
David Ahern3b1137f2017-02-02 12:37:10 -08005228 /* success ... tell user about new route */
5229 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005230 goto cleanup;
5231
5232add_errout:
David Ahern3b1137f2017-02-02 12:37:10 -08005233 /* send notification for routes that were added so that
5234 * the delete notifications sent by ip6_route_del are
5235 * coherent
5236 */
5237 if (rt_notif)
5238 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5239
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005240 /* Delete routes that were already added */
5241 list_for_each_entry(nh, &rt6_nh_list, next) {
5242 if (err_nh == nh)
5243 break;
David Ahern333c4302017-05-21 10:12:04 -06005244 ip6_route_del(&nh->r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005245 }
5246
5247cleanup:
5248 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005249 if (nh->fib6_info)
5250 fib6_info_release(nh->fib6_info);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005251 list_del(&nh->next);
5252 kfree(nh);
5253 }
5254
5255 return err;
5256}
5257
David Ahern333c4302017-05-21 10:12:04 -06005258static int ip6_route_multipath_del(struct fib6_config *cfg,
5259 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005260{
5261 struct fib6_config r_cfg;
5262 struct rtnexthop *rtnh;
5263 int remaining;
5264 int attrlen;
5265 int err = 1, last_err = 0;
5266
5267 remaining = cfg->fc_mp_len;
5268 rtnh = (struct rtnexthop *)cfg->fc_mp;
5269
5270 /* Parse a Multipath Entry */
5271 while (rtnh_ok(rtnh, remaining)) {
5272 memcpy(&r_cfg, cfg, sizeof(*cfg));
5273 if (rtnh->rtnh_ifindex)
5274 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5275
5276 attrlen = rtnh_attrlen(rtnh);
5277 if (attrlen > 0) {
5278 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5279
5280 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5281 if (nla) {
5282 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
5283 r_cfg.fc_flags |= RTF_GATEWAY;
5284 }
5285 }
David Ahern333c4302017-05-21 10:12:04 -06005286 err = ip6_route_del(&r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005287 if (err)
5288 last_err = err;
5289
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005290 rtnh = rtnh_next(rtnh, &remaining);
5291 }
5292
5293 return last_err;
5294}
5295
David Ahernc21ef3e2017-04-16 09:48:24 -07005296static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5297 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298{
Thomas Graf86872cb2006-08-22 00:01:08 -07005299 struct fib6_config cfg;
5300 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301
David Ahern333c4302017-05-21 10:12:04 -06005302 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005303 if (err < 0)
5304 return err;
5305
David Ahern5b983242019-06-08 14:53:34 -07005306 if (cfg.fc_nh_id &&
5307 !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5308 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5309 return -EINVAL;
5310 }
5311
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005312 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005313 return ip6_route_multipath_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005314 else {
5315 cfg.fc_delete_all_nh = 1;
David Ahern333c4302017-05-21 10:12:04 -06005316 return ip6_route_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318}
5319
David Ahernc21ef3e2017-04-16 09:48:24 -07005320static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5321 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322{
Thomas Graf86872cb2006-08-22 00:01:08 -07005323 struct fib6_config cfg;
5324 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
David Ahern333c4302017-05-21 10:12:04 -06005326 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005327 if (err < 0)
5328 return err;
5329
David Ahern67f69512019-03-21 05:21:34 -07005330 if (cfg.fc_metric == 0)
5331 cfg.fc_metric = IP6_RT_PRIO_USER;
5332
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005333 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005334 return ip6_route_multipath_add(&cfg, extack);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005335 else
David Ahernacb54e32018-04-17 17:33:22 -07005336 return ip6_route_add(&cfg, GFP_KERNEL, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337}
5338
David Aherna1b7a1f2019-06-08 14:53:26 -07005339/* add the overhead of this fib6_nh to nexthop_len */
5340static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
Thomas Graf339bf982006-11-10 14:10:15 -08005341{
David Aherna1b7a1f2019-06-08 14:53:26 -07005342 int *nexthop_len = arg;
David Ahernbeb1afac52017-02-02 12:37:09 -08005343
David Aherna1b7a1f2019-06-08 14:53:26 -07005344 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5345 + NLA_ALIGN(sizeof(struct rtnexthop))
5346 + nla_total_size(16); /* RTA_GATEWAY */
David Ahernf88d8ea2019-06-03 20:19:52 -07005347
David Aherna1b7a1f2019-06-08 14:53:26 -07005348 if (nh->fib_nh_lws) {
5349 /* RTA_ENCAP_TYPE */
5350 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5351 /* RTA_ENCAP */
5352 *nexthop_len += nla_total_size(2);
5353 }
David Ahernbeb1afac52017-02-02 12:37:09 -08005354
David Aherna1b7a1f2019-06-08 14:53:26 -07005355 return 0;
5356}
5357
5358static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5359{
5360 int nexthop_len;
5361
5362 if (f6i->nh) {
5363 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5364 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5365 &nexthop_len);
5366 } else {
5367 struct fib6_nh *nh = f6i->fib6_nh;
5368
5369 nexthop_len = 0;
5370 if (f6i->fib6_nsiblings) {
5371 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
5372 + NLA_ALIGN(sizeof(struct rtnexthop))
5373 + nla_total_size(16) /* RTA_GATEWAY */
5374 + lwtunnel_get_encap_size(nh->fib_nh_lws);
5375
5376 nexthop_len *= f6i->fib6_nsiblings;
5377 }
5378 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
David Ahernbeb1afac52017-02-02 12:37:09 -08005379 }
5380
Thomas Graf339bf982006-11-10 14:10:15 -08005381 return NLMSG_ALIGN(sizeof(struct rtmsg))
5382 + nla_total_size(16) /* RTA_SRC */
5383 + nla_total_size(16) /* RTA_DST */
5384 + nla_total_size(16) /* RTA_GATEWAY */
5385 + nla_total_size(16) /* RTA_PREFSRC */
5386 + nla_total_size(4) /* RTA_TABLE */
5387 + nla_total_size(4) /* RTA_IIF */
5388 + nla_total_size(4) /* RTA_OIF */
5389 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08005390 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01005391 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005392 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005393 + nla_total_size(1) /* RTA_PREF */
David Ahernbeb1afac52017-02-02 12:37:09 -08005394 + nexthop_len;
5395}
5396
David Ahernf88d8ea2019-06-03 20:19:52 -07005397static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5398 unsigned char *flags)
5399{
5400 if (nexthop_is_multipath(nh)) {
5401 struct nlattr *mp;
5402
David Ahern4255ff02019-09-03 15:22:12 -07005403 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernf88d8ea2019-06-03 20:19:52 -07005404 if (!mp)
5405 goto nla_put_failure;
5406
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005407 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
David Ahernf88d8ea2019-06-03 20:19:52 -07005408 goto nla_put_failure;
5409
5410 nla_nest_end(skb, mp);
5411 } else {
5412 struct fib6_nh *fib6_nh;
5413
5414 fib6_nh = nexthop_fib6_nh(nh);
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005415 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
David Ahernf88d8ea2019-06-03 20:19:52 -07005416 flags, false) < 0)
5417 goto nla_put_failure;
5418 }
5419
5420 return 0;
5421
5422nla_put_failure:
5423 return -EMSGSIZE;
5424}
5425
David Ahernd4ead6b2018-04-17 17:33:16 -07005426static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -07005427 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -07005428 struct in6_addr *dest, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005429 int iif, int type, u32 portid, u32 seq,
David Ahernf8cfe2c2017-01-17 15:51:08 -08005430 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431{
Xin Long22d0bd82018-09-11 14:33:58 +08005432 struct rt6_info *rt6 = (struct rt6_info *)dst;
5433 struct rt6key *rt6_dst, *rt6_src;
5434 u32 *pmetrics, table, rt6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -07005435 unsigned char nh_flags = 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005436 struct nlmsghdr *nlh;
Xin Long22d0bd82018-09-11 14:33:58 +08005437 struct rtmsg *rtm;
David Ahernd4ead6b2018-04-17 17:33:16 -07005438 long expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439
Eric W. Biederman15e47302012-09-07 20:12:54 +00005440 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05005441 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005442 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005443
Xin Long22d0bd82018-09-11 14:33:58 +08005444 if (rt6) {
5445 rt6_dst = &rt6->rt6i_dst;
5446 rt6_src = &rt6->rt6i_src;
5447 rt6_flags = rt6->rt6i_flags;
5448 } else {
5449 rt6_dst = &rt->fib6_dst;
5450 rt6_src = &rt->fib6_src;
5451 rt6_flags = rt->fib6_flags;
5452 }
5453
Thomas Graf2d7202b2006-08-22 00:01:27 -07005454 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 rtm->rtm_family = AF_INET6;
Xin Long22d0bd82018-09-11 14:33:58 +08005456 rtm->rtm_dst_len = rt6_dst->plen;
5457 rtm->rtm_src_len = rt6_src->plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 rtm->rtm_tos = 0;
David Ahern93c2fb22018-04-18 15:38:59 -07005459 if (rt->fib6_table)
5460 table = rt->fib6_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07005461 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07005462 table = RT6_TABLE_UNSPEC;
Kalash Nainwal97f00822019-02-20 16:23:04 -08005463 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
David S. Millerc78679e2012-04-01 20:27:33 -04005464 if (nla_put_u32(skb, RTA_TABLE, table))
5465 goto nla_put_failure;
David Aherne8478e82018-04-17 17:33:13 -07005466
5467 rtm->rtm_type = rt->fib6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 rtm->rtm_flags = 0;
5469 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
David Ahern93c2fb22018-04-18 15:38:59 -07005470 rtm->rtm_protocol = rt->fib6_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471
Xin Long22d0bd82018-09-11 14:33:58 +08005472 if (rt6_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 rtm->rtm_flags |= RTM_F_CLONED;
5474
David Ahernd4ead6b2018-04-17 17:33:16 -07005475 if (dest) {
5476 if (nla_put_in6_addr(skb, RTA_DST, dest))
David S. Millerc78679e2012-04-01 20:27:33 -04005477 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005478 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 } else if (rtm->rtm_dst_len)
Xin Long22d0bd82018-09-11 14:33:58 +08005480 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005481 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482#ifdef CONFIG_IPV6_SUBTREES
5483 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02005484 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04005485 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005486 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04005487 } else if (rtm->rtm_src_len &&
Xin Long22d0bd82018-09-11 14:33:58 +08005488 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005489 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005491 if (iif) {
5492#ifdef CONFIG_IPV6_MROUTE
Xin Long22d0bd82018-09-11 14:33:58 +08005493 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
David Ahernfd61c6b2017-01-17 15:51:07 -08005494 int err = ip6mr_get_route(net, skb, rtm, portid);
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02005495
David Ahernfd61c6b2017-01-17 15:51:07 -08005496 if (err == 0)
5497 return 0;
5498 if (err < 0)
5499 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005500 } else
5501#endif
David S. Millerc78679e2012-04-01 20:27:33 -04005502 if (nla_put_u32(skb, RTA_IIF, iif))
5503 goto nla_put_failure;
David Ahernd4ead6b2018-04-17 17:33:16 -07005504 } else if (dest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 struct in6_addr saddr_buf;
David Ahernd4ead6b2018-04-17 17:33:16 -07005506 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02005507 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005508 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07005510
David Ahern93c2fb22018-04-18 15:38:59 -07005511 if (rt->fib6_prefsrc.plen) {
Daniel Walterc3968a82011-04-13 21:10:57 +00005512 struct in6_addr saddr_buf;
David Ahern93c2fb22018-04-18 15:38:59 -07005513 saddr_buf = rt->fib6_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02005514 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005515 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00005516 }
5517
David Ahernd4ead6b2018-04-17 17:33:16 -07005518 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5519 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07005520 goto nla_put_failure;
5521
David Ahern93c2fb22018-04-18 15:38:59 -07005522 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
David S. Millerc78679e2012-04-01 20:27:33 -04005523 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00005524
David Ahernbeb1afac52017-02-02 12:37:09 -08005525 /* For multipath routes, walk the siblings list and add
5526 * each as a nexthop within RTA_MULTIPATH.
5527 */
Xin Long22d0bd82018-09-11 14:33:58 +08005528 if (rt6) {
5529 if (rt6_flags & RTF_GATEWAY &&
5530 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5531 goto nla_put_failure;
5532
5533 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5534 goto nla_put_failure;
5535 } else if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07005536 struct fib6_info *sibling, *next_sibling;
David Ahernbeb1afac52017-02-02 12:37:09 -08005537 struct nlattr *mp;
5538
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005539 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernbeb1afac52017-02-02 12:37:09 -08005540 if (!mp)
5541 goto nla_put_failure;
5542
David Ahern1cf844c2019-05-22 20:27:59 -07005543 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005544 rt->fib6_nh->fib_nh_weight, AF_INET6) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005545 goto nla_put_failure;
5546
5547 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07005548 &rt->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005549 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005550 sibling->fib6_nh->fib_nh_weight,
5551 AF_INET6) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005552 goto nla_put_failure;
5553 }
5554
5555 nla_nest_end(skb, mp);
David Ahernf88d8ea2019-06-03 20:19:52 -07005556 } else if (rt->nh) {
5557 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5558 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005559
David Ahernf88d8ea2019-06-03 20:19:52 -07005560 if (nexthop_is_blackhole(rt->nh))
5561 rtm->rtm_type = RTN_BLACKHOLE;
5562
5563 if (rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
5564 goto nla_put_failure;
5565
5566 rtm->rtm_flags |= nh_flags;
5567 } else {
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005568 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
David Ahernecc56632019-04-23 08:48:09 -07005569 &nh_flags, false) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005570 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005571
5572 rtm->rtm_flags |= nh_flags;
David Ahernbeb1afac52017-02-02 12:37:09 -08005573 }
5574
Xin Long22d0bd82018-09-11 14:33:58 +08005575 if (rt6_flags & RTF_EXPIRES) {
David Ahern14895682018-04-17 17:33:17 -07005576 expires = dst ? dst->expires : rt->expires;
5577 expires -= jiffies;
5578 }
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07005579
Ido Schimmelbb3c4ab2020-01-14 13:23:12 +02005580 if (!dst) {
5581 if (rt->offload)
5582 rtm->rtm_flags |= RTM_F_OFFLOAD;
5583 if (rt->trap)
5584 rtm->rtm_flags |= RTM_F_TRAP;
5585 }
5586
David Ahernd4ead6b2018-04-17 17:33:16 -07005587 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08005588 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589
Xin Long22d0bd82018-09-11 14:33:58 +08005590 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005591 goto nla_put_failure;
5592
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005593
Johannes Berg053c0952015-01-16 22:09:00 +01005594 nlmsg_end(skb, nlh);
5595 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005596
5597nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005598 nlmsg_cancel(skb, nlh);
5599 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600}
5601
David Ahern2c170e02019-06-08 14:53:27 -07005602static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5603{
5604 const struct net_device *dev = arg;
5605
5606 if (nh->fib_nh_dev == dev)
5607 return 1;
5608
5609 return 0;
5610}
5611
David Ahern13e38902018-10-15 18:56:44 -07005612static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5613 const struct net_device *dev)
5614{
David Ahern2c170e02019-06-08 14:53:27 -07005615 if (f6i->nh) {
5616 struct net_device *_dev = (struct net_device *)dev;
5617
5618 return !!nexthop_for_each_fib6_nh(f6i->nh,
5619 fib6_info_nh_uses_dev,
5620 _dev);
5621 }
5622
David Ahern1cf844c2019-05-22 20:27:59 -07005623 if (f6i->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005624 return true;
5625
5626 if (f6i->fib6_nsiblings) {
5627 struct fib6_info *sibling, *next_sibling;
5628
5629 list_for_each_entry_safe(sibling, next_sibling,
5630 &f6i->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005631 if (sibling->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005632 return true;
5633 }
5634 }
5635
5636 return false;
5637}
5638
Stefano Brivio1e47b482019-06-21 17:45:27 +02005639struct fib6_nh_exception_dump_walker {
5640 struct rt6_rtnl_dump_arg *dump;
5641 struct fib6_info *rt;
5642 unsigned int flags;
5643 unsigned int skip;
5644 unsigned int count;
5645};
5646
5647static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
5648{
5649 struct fib6_nh_exception_dump_walker *w = arg;
5650 struct rt6_rtnl_dump_arg *dump = w->dump;
5651 struct rt6_exception_bucket *bucket;
5652 struct rt6_exception *rt6_ex;
5653 int i, err;
5654
5655 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
5656 if (!bucket)
5657 return 0;
5658
5659 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
5660 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
5661 if (w->skip) {
5662 w->skip--;
5663 continue;
5664 }
5665
5666 /* Expiration of entries doesn't bump sernum, insertion
5667 * does. Removal is triggered by insertion, so we can
5668 * rely on the fact that if entries change between two
5669 * partial dumps, this node is scanned again completely,
5670 * see rt6_insert_exception() and fib6_dump_table().
5671 *
5672 * Count expired entries we go through as handled
5673 * entries that we'll skip next time, in case of partial
5674 * node dump. Otherwise, if entries expire meanwhile,
5675 * we'll skip the wrong amount.
5676 */
5677 if (rt6_check_expired(rt6_ex->rt6i)) {
5678 w->count++;
5679 continue;
5680 }
5681
5682 err = rt6_fill_node(dump->net, dump->skb, w->rt,
5683 &rt6_ex->rt6i->dst, NULL, NULL, 0,
5684 RTM_NEWROUTE,
5685 NETLINK_CB(dump->cb->skb).portid,
5686 dump->cb->nlh->nlmsg_seq, w->flags);
5687 if (err)
5688 return err;
5689
5690 w->count++;
5691 }
5692 bucket++;
5693 }
5694
5695 return 0;
5696}
5697
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005698/* Return -1 if done with node, number of handled routes on partial dump */
Stefano Brivio1e47b482019-06-21 17:45:27 +02005699int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700{
5701 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
David Ahern13e38902018-10-15 18:56:44 -07005702 struct fib_dump_filter *filter = &arg->filter;
5703 unsigned int flags = NLM_F_MULTI;
David Ahern1f17e2f2017-01-26 13:54:08 -08005704 struct net *net = arg->net;
Stefano Brivio1e47b482019-06-21 17:45:27 +02005705 int count = 0;
David Ahern1f17e2f2017-01-26 13:54:08 -08005706
David Ahern421842e2018-04-17 17:33:18 -07005707 if (rt == net->ipv6.fib6_null_entry)
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005708 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
David Ahern13e38902018-10-15 18:56:44 -07005710 if ((filter->flags & RTM_F_PREFIX) &&
5711 !(rt->fib6_flags & RTF_PREFIX_RT)) {
5712 /* success since this is not a prefix route */
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005713 return -1;
David Ahern13e38902018-10-15 18:56:44 -07005714 }
Stefano Brivio1e47b482019-06-21 17:45:27 +02005715 if (filter->filter_set &&
5716 ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5717 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5718 (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5719 return -1;
5720 }
5721
5722 if (filter->filter_set ||
5723 !filter->dump_routes || !filter->dump_exceptions) {
David Ahern13e38902018-10-15 18:56:44 -07005724 flags |= NLM_F_DUMP_FILTERED;
David Ahernf8cfe2c2017-01-17 15:51:08 -08005725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726
Stefano Brivio1e47b482019-06-21 17:45:27 +02005727 if (filter->dump_routes) {
5728 if (skip) {
5729 skip--;
5730 } else {
5731 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
5732 0, RTM_NEWROUTE,
5733 NETLINK_CB(arg->cb->skb).portid,
5734 arg->cb->nlh->nlmsg_seq, flags)) {
5735 return 0;
5736 }
5737 count++;
5738 }
5739 }
5740
5741 if (filter->dump_exceptions) {
5742 struct fib6_nh_exception_dump_walker w = { .dump = arg,
5743 .rt = rt,
5744 .flags = flags,
5745 .skip = skip,
5746 .count = 0 };
5747 int err;
5748
Eric Dumazet3b525692019-06-26 03:05:28 -07005749 rcu_read_lock();
Stefano Brivio1e47b482019-06-21 17:45:27 +02005750 if (rt->nh) {
5751 err = nexthop_for_each_fib6_nh(rt->nh,
5752 rt6_nh_dump_exceptions,
5753 &w);
5754 } else {
5755 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
5756 }
Eric Dumazet3b525692019-06-26 03:05:28 -07005757 rcu_read_unlock();
Stefano Brivio1e47b482019-06-21 17:45:27 +02005758
5759 if (err)
5760 return count += w.count;
5761 }
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005762
5763 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764}
5765
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005766static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5767 const struct nlmsghdr *nlh,
5768 struct nlattr **tb,
5769 struct netlink_ext_ack *extack)
5770{
5771 struct rtmsg *rtm;
5772 int i, err;
5773
5774 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5775 NL_SET_ERR_MSG_MOD(extack,
5776 "Invalid header for get route request");
5777 return -EINVAL;
5778 }
5779
5780 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02005781 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5782 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005783
5784 rtm = nlmsg_data(nlh);
5785 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5786 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5787 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5788 rtm->rtm_type) {
5789 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5790 return -EINVAL;
5791 }
5792 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5793 NL_SET_ERR_MSG_MOD(extack,
5794 "Invalid flags for get route request");
5795 return -EINVAL;
5796 }
5797
Johannes Berg8cb08172019-04-26 14:07:28 +02005798 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5799 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005800 if (err)
5801 return err;
5802
5803 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5804 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5805 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5806 return -EINVAL;
5807 }
5808
5809 for (i = 0; i <= RTA_MAX; i++) {
5810 if (!tb[i])
5811 continue;
5812
5813 switch (i) {
5814 case RTA_SRC:
5815 case RTA_DST:
5816 case RTA_IIF:
5817 case RTA_OIF:
5818 case RTA_MARK:
5819 case RTA_UID:
5820 case RTA_SPORT:
5821 case RTA_DPORT:
5822 case RTA_IP_PROTO:
5823 break;
5824 default:
5825 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
5826 return -EINVAL;
5827 }
5828 }
5829
5830 return 0;
5831}
5832
David Ahernc21ef3e2017-04-16 09:48:24 -07005833static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5834 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005836 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07005837 struct nlattr *tb[RTA_MAX+1];
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005838 int err, iif = 0, oif = 0;
David Aherna68886a2018-04-20 15:38:02 -07005839 struct fib6_info *from;
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005840 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07005842 struct sk_buff *skb;
5843 struct rtmsg *rtm;
Maciej Żenczykowski744486d2018-09-29 23:44:54 -07005844 struct flowi6 fl6 = {};
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005845 bool fibmatch;
Thomas Grafab364a62006-08-22 00:01:47 -07005846
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005847 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Thomas Grafab364a62006-08-22 00:01:47 -07005848 if (err < 0)
5849 goto errout;
5850
5851 err = -EINVAL;
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02005852 rtm = nlmsg_data(nlh);
5853 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005854 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
Thomas Grafab364a62006-08-22 00:01:47 -07005855
5856 if (tb[RTA_SRC]) {
5857 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5858 goto errout;
5859
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005860 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07005861 }
5862
5863 if (tb[RTA_DST]) {
5864 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5865 goto errout;
5866
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005867 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07005868 }
5869
5870 if (tb[RTA_IIF])
5871 iif = nla_get_u32(tb[RTA_IIF]);
5872
5873 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005874 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07005875
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07005876 if (tb[RTA_MARK])
5877 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
5878
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09005879 if (tb[RTA_UID])
5880 fl6.flowi6_uid = make_kuid(current_user_ns(),
5881 nla_get_u32(tb[RTA_UID]));
5882 else
5883 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5884
Roopa Prabhueacb9382018-05-22 14:03:28 -07005885 if (tb[RTA_SPORT])
5886 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5887
5888 if (tb[RTA_DPORT])
5889 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5890
5891 if (tb[RTA_IP_PROTO]) {
5892 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
Hangbin Liu5e1a99e2019-02-27 16:15:29 +08005893 &fl6.flowi6_proto, AF_INET6,
5894 extack);
Roopa Prabhueacb9382018-05-22 14:03:28 -07005895 if (err)
5896 goto errout;
5897 }
5898
Thomas Grafab364a62006-08-22 00:01:47 -07005899 if (iif) {
5900 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005901 int flags = 0;
5902
Florian Westphal121622d2017-08-15 16:34:42 +02005903 rcu_read_lock();
5904
5905 dev = dev_get_by_index_rcu(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07005906 if (!dev) {
Florian Westphal121622d2017-08-15 16:34:42 +02005907 rcu_read_unlock();
Thomas Grafab364a62006-08-22 00:01:47 -07005908 err = -ENODEV;
5909 goto errout;
5910 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005911
5912 fl6.flowi6_iif = iif;
5913
5914 if (!ipv6_addr_any(&fl6.saddr))
5915 flags |= RT6_LOOKUP_F_HAS_SADDR;
5916
David Ahernb75cc8f2018-03-02 08:32:17 -08005917 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
Florian Westphal121622d2017-08-15 16:34:42 +02005918
5919 rcu_read_unlock();
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005920 } else {
5921 fl6.flowi6_oif = oif;
5922
Ido Schimmel58acfd72017-12-20 12:28:25 +02005923 dst = ip6_route_output(net, NULL, &fl6);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005924 }
5925
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005926
5927 rt = container_of(dst, struct rt6_info, dst);
5928 if (rt->dst.error) {
5929 err = rt->dst.error;
5930 ip6_rt_put(rt);
5931 goto errout;
Thomas Grafab364a62006-08-22 00:01:47 -07005932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933
WANG Cong9d6acb32017-03-01 20:48:39 -08005934 if (rt == net->ipv6.ip6_null_entry) {
5935 err = rt->dst.error;
5936 ip6_rt_put(rt);
5937 goto errout;
5938 }
5939
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05005941 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00005942 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07005943 err = -ENOBUFS;
5944 goto errout;
5945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946
Changli Gaod8d1f302010-06-10 23:31:35 -07005947 skb_dst_set(skb, &rt->dst);
David Aherna68886a2018-04-20 15:38:02 -07005948
5949 rcu_read_lock();
5950 from = rcu_dereference(rt->from);
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07005951 if (from) {
5952 if (fibmatch)
5953 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5954 iif, RTM_NEWROUTE,
5955 NETLINK_CB(in_skb).portid,
5956 nlh->nlmsg_seq, 0);
5957 else
5958 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5959 &fl6.saddr, iif, RTM_NEWROUTE,
5960 NETLINK_CB(in_skb).portid,
5961 nlh->nlmsg_seq, 0);
5962 } else {
5963 err = -ENETUNREACH;
5964 }
David Aherna68886a2018-04-20 15:38:02 -07005965 rcu_read_unlock();
5966
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07005968 kfree_skb(skb);
5969 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 }
5971
Eric W. Biederman15e47302012-09-07 20:12:54 +00005972 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07005973errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975}
5976
David Ahern8d1c8022018-04-17 17:33:26 -07005977void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07005978 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979{
5980 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08005981 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08005982 u32 seq;
5983 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08005985 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05005986 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07005987
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005988 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05005989 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07005990 goto errout;
5991
David Ahernd4ead6b2018-04-17 17:33:16 -07005992 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5993 event, info->portid, seq, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08005994 if (err < 0) {
5995 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
5996 WARN_ON(err == -EMSGSIZE);
5997 kfree_skb(skb);
5998 goto errout;
5999 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00006000 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08006001 info->nlh, gfp_any());
6002 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07006003errout:
6004 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08006005 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006}
6007
David Ahern19a3b7e2019-05-22 12:04:41 -07006008void fib6_rt_update(struct net *net, struct fib6_info *rt,
6009 struct nl_info *info)
6010{
6011 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6012 struct sk_buff *skb;
6013 int err = -ENOBUFS;
6014
6015 /* call_fib6_entry_notifiers will be removed when in-kernel notifier
6016 * is implemented and supported for nexthop objects
6017 */
6018 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
6019
6020 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6021 if (!skb)
6022 goto errout;
6023
6024 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6025 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
6026 if (err < 0) {
6027 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6028 WARN_ON(err == -EMSGSIZE);
6029 kfree_skb(skb);
6030 goto errout;
6031 }
6032 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6033 info->nlh, gfp_any());
6034 return;
6035errout:
6036 if (err < 0)
6037 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6038}
6039
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006040static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00006041 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006042{
Jiri Pirko351638e2013-05-28 01:30:21 +00006043 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09006044 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006045
WANG Cong242d3a42017-05-08 10:12:13 -07006046 if (!(dev->flags & IFF_LOOPBACK))
6047 return NOTIFY_OK;
6048
6049 if (event == NETDEV_REGISTER) {
David Ahern1cf844c2019-05-22 20:27:59 -07006050 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
Changli Gaod8d1f302010-06-10 23:31:35 -07006051 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006052 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
6053#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07006054 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006055 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07006056 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006057 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
6058#endif
WANG Cong76da0702017-06-20 11:42:27 -07006059 } else if (event == NETDEV_UNREGISTER &&
6060 dev->reg_state != NETREG_UNREGISTERED) {
6061 /* NETDEV_UNREGISTER could be fired for multiple times by
6062 * netdev_wait_allrefs(). Make sure we only call this once.
6063 */
Eric Dumazet12d94a82017-08-15 04:09:51 -07006064 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07006065#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric Dumazet12d94a82017-08-15 04:09:51 -07006066 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
6067 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07006068#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006069 }
6070
6071 return NOTIFY_OK;
6072}
6073
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074/*
6075 * /proc
6076 */
6077
6078#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6080{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006081 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006083 net->ipv6.rt6_stats->fib_nodes,
6084 net->ipv6.rt6_stats->fib_route_nodes,
Wei Wang81eb8442017-10-06 12:06:11 -07006085 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006086 net->ipv6.rt6_stats->fib_rt_entries,
6087 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00006088 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006089 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090
6091 return 0;
6092}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093#endif /* CONFIG_PROC_FS */
6094
6095#ifdef CONFIG_SYSCTL
6096
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097static
Joe Perchesfe2c6332013-06-11 23:04:25 -07006098int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 void __user *buffer, size_t *lenp, loff_t *ppos)
6100{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006101 struct net *net;
6102 int delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06006103 int ret;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006104 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006106
6107 net = (struct net *)ctl->extra1;
6108 delay = net->ipv6.sysctl.flush_delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06006109 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6110 if (ret)
6111 return ret;
6112
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02006113 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115}
6116
David Aherned792e22018-10-08 14:06:34 -07006117static struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006118 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08006120 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07006122 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006123 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 },
6125 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006127 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 .maxlen = sizeof(int),
6129 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006130 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 },
6132 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08006134 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 .maxlen = sizeof(int),
6136 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006137 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 },
6139 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08006141 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 .maxlen = sizeof(int),
6143 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006144 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 },
6146 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08006148 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 .maxlen = sizeof(int),
6150 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006151 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 },
6153 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08006155 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 .maxlen = sizeof(int),
6157 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006158 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 },
6160 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08006162 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 .maxlen = sizeof(int),
6164 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07006165 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 },
6167 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08006169 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 .maxlen = sizeof(int),
6171 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006172 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 },
6174 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08006176 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 .maxlen = sizeof(int),
6178 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07006179 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180 },
6181 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08006183 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 .maxlen = sizeof(int),
6185 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006186 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 },
David Ahern7c6bb7d2018-10-11 20:17:21 -07006188 {
6189 .procname = "skip_notify_on_dev_down",
6190 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
6191 .maxlen = sizeof(int),
6192 .mode = 0644,
Eiichi Tsukatab8e8a862019-06-25 12:08:01 +09006193 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07006194 .extra1 = SYSCTL_ZERO,
6195 .extra2 = SYSCTL_ONE,
David Ahern7c6bb7d2018-10-11 20:17:21 -07006196 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006197 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198};
6199
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006200struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08006201{
6202 struct ctl_table *table;
6203
6204 table = kmemdup(ipv6_route_table_template,
6205 sizeof(ipv6_route_table_template),
6206 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006207
6208 if (table) {
6209 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006210 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00006211 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006212 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
6213 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6214 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6215 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6216 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6217 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6218 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08006219 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
David Ahern7c6bb7d2018-10-11 20:17:21 -07006220 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
Eric W. Biederman464dc802012-11-16 03:02:59 +00006221
6222 /* Don't export sysctls to unprivileged users */
6223 if (net->user_ns != &init_user_ns)
6224 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006225 }
6226
Daniel Lezcano760f2d02008-01-10 02:53:43 -08006227 return table;
6228}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229#endif
6230
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006231static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006232{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07006233 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006234
Alexey Dobriyan86393e52009-08-29 01:34:49 +00006235 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6236 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006237
Eric Dumazetfc66f952010-10-08 06:37:34 +00006238 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6239 goto out_ip6_dst_ops;
6240
David Ahern1cf844c2019-05-22 20:27:59 -07006241 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
David Ahern421842e2018-04-17 17:33:18 -07006242 if (!net->ipv6.fib6_null_entry)
6243 goto out_ip6_dst_entries;
David Ahern1cf844c2019-05-22 20:27:59 -07006244 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6245 sizeof(*net->ipv6.fib6_null_entry));
David Ahern421842e2018-04-17 17:33:18 -07006246
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006247 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6248 sizeof(*net->ipv6.ip6_null_entry),
6249 GFP_KERNEL);
6250 if (!net->ipv6.ip6_null_entry)
David Ahern421842e2018-04-17 17:33:18 -07006251 goto out_fib6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006252 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006253 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6254 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006255 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006256
6257#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Vincent Bernatfeca7d82017-08-08 20:23:49 +02006258 net->ipv6.fib6_has_custom_rules = false;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006259 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6260 sizeof(*net->ipv6.ip6_prohibit_entry),
6261 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006262 if (!net->ipv6.ip6_prohibit_entry)
6263 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006264 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006265 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6266 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006267 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006268
6269 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6270 sizeof(*net->ipv6.ip6_blk_hole_entry),
6271 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006272 if (!net->ipv6.ip6_blk_hole_entry)
6273 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006274 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006275 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6276 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006277 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
Paolo Abenib9b33e72019-11-20 13:47:34 +01006278#ifdef CONFIG_IPV6_SUBTREES
6279 net->ipv6.fib6_routes_require_src = 0;
6280#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006281#endif
6282
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006283 net->ipv6.sysctl.flush_delay = 0;
6284 net->ipv6.sysctl.ip6_rt_max_size = 4096;
6285 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6286 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6287 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6288 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6289 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6290 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
David Ahern7c6bb7d2018-10-11 20:17:21 -07006291 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006292
Benjamin Thery6891a342008-03-04 13:49:47 -08006293 net->ipv6.ip6_rt_gc_expire = 30*HZ;
6294
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006295 ret = 0;
6296out:
6297 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006298
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006299#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6300out_ip6_prohibit_entry:
6301 kfree(net->ipv6.ip6_prohibit_entry);
6302out_ip6_null_entry:
6303 kfree(net->ipv6.ip6_null_entry);
6304#endif
David Ahern421842e2018-04-17 17:33:18 -07006305out_fib6_null_entry:
6306 kfree(net->ipv6.fib6_null_entry);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006307out_ip6_dst_entries:
6308 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006309out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006310 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006311}
6312
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006313static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006314{
David Ahern421842e2018-04-17 17:33:18 -07006315 kfree(net->ipv6.fib6_null_entry);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006316 kfree(net->ipv6.ip6_null_entry);
6317#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6318 kfree(net->ipv6.ip6_prohibit_entry);
6319 kfree(net->ipv6.ip6_blk_hole_entry);
6320#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006321 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006322}
6323
Thomas Grafd1896342012-06-18 12:08:33 +00006324static int __net_init ip6_route_net_init_late(struct net *net)
6325{
6326#ifdef CONFIG_PROC_FS
Christoph Hellwigc3506372018-04-10 19:42:55 +02006327 proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
6328 sizeof(struct ipv6_route_iter));
Christoph Hellwig3617d942018-04-13 20:38:35 +02006329 proc_create_net_single("rt6_stats", 0444, net->proc_net,
6330 rt6_stats_seq_show, NULL);
Thomas Grafd1896342012-06-18 12:08:33 +00006331#endif
6332 return 0;
6333}
6334
6335static void __net_exit ip6_route_net_exit_late(struct net *net)
6336{
6337#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00006338 remove_proc_entry("ipv6_route", net->proc_net);
6339 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00006340#endif
6341}
6342
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006343static struct pernet_operations ip6_route_net_ops = {
6344 .init = ip6_route_net_init,
6345 .exit = ip6_route_net_exit,
6346};
6347
David S. Millerc3426b42012-06-09 16:27:05 -07006348static int __net_init ipv6_inetpeer_init(struct net *net)
6349{
6350 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6351
6352 if (!bp)
6353 return -ENOMEM;
6354 inet_peer_base_init(bp);
6355 net->ipv6.peers = bp;
6356 return 0;
6357}
6358
6359static void __net_exit ipv6_inetpeer_exit(struct net *net)
6360{
6361 struct inet_peer_base *bp = net->ipv6.peers;
6362
6363 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07006364 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07006365 kfree(bp);
6366}
6367
David S. Miller2b823f72012-06-09 19:00:16 -07006368static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07006369 .init = ipv6_inetpeer_init,
6370 .exit = ipv6_inetpeer_exit,
6371};
6372
Thomas Grafd1896342012-06-18 12:08:33 +00006373static struct pernet_operations ip6_route_net_late_ops = {
6374 .init = ip6_route_net_init_late,
6375 .exit = ip6_route_net_exit_late,
6376};
6377
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006378static struct notifier_block ip6_route_dev_notifier = {
6379 .notifier_call = ip6_route_dev_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07006380 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006381};
6382
WANG Cong2f460932017-05-03 22:07:31 -07006383void __init ip6_route_init_special_entries(void)
6384{
6385 /* Registering of the loopback is done before this portion of code,
6386 * the loopback reference in rt6_info will not be taken, do it
6387 * manually for init_net */
David Ahern1cf844c2019-05-22 20:27:59 -07006388 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
WANG Cong2f460932017-05-03 22:07:31 -07006389 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6390 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6391 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6392 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6393 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6394 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6395 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6396 #endif
6397}
6398
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006399int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006401 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006402 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006403
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006404 ret = -ENOMEM;
6405 ip6_dst_ops_template.kmem_cachep =
6406 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6407 SLAB_HWCACHE_ALIGN, NULL);
6408 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08006409 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07006410
Eric Dumazetfc66f952010-10-08 06:37:34 +00006411 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006412 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006413 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006414
David S. Millerc3426b42012-06-09 16:27:05 -07006415 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6416 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006417 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00006418
David S. Miller7e52b332012-06-15 15:51:55 -07006419 ret = register_pernet_subsys(&ip6_route_net_ops);
6420 if (ret)
6421 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07006422
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07006423 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6424
David S. Millere8803b62012-06-16 01:12:19 -07006425 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006426 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006427 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006428
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006429 ret = xfrm6_init();
6430 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006431 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08006432
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006433 ret = fib6_rules_init();
6434 if (ret)
6435 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08006436
Thomas Grafd1896342012-06-18 12:08:33 +00006437 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6438 if (ret)
6439 goto fib6_rules_init;
6440
Florian Westphal16feebc2017-12-02 21:44:08 +01006441 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6442 inet6_rtm_newroute, NULL, 0);
6443 if (ret < 0)
6444 goto out_register_late_subsys;
6445
6446 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6447 inet6_rtm_delroute, NULL, 0);
6448 if (ret < 0)
6449 goto out_register_late_subsys;
6450
6451 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6452 inet6_rtm_getroute, NULL,
6453 RTNL_FLAG_DOIT_UNLOCKED);
6454 if (ret < 0)
Thomas Grafd1896342012-06-18 12:08:33 +00006455 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006456
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006457 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006458 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00006459 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006460
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006461 for_each_possible_cpu(cpu) {
6462 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6463
6464 INIT_LIST_HEAD(&ul->head);
6465 spin_lock_init(&ul->lock);
6466 }
6467
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006468out:
6469 return ret;
6470
Thomas Grafd1896342012-06-18 12:08:33 +00006471out_register_late_subsys:
Florian Westphal16feebc2017-12-02 21:44:08 +01006472 rtnl_unregister_all(PF_INET6);
Thomas Grafd1896342012-06-18 12:08:33 +00006473 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006474fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006475 fib6_rules_cleanup();
6476xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006477 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00006478out_fib6_init:
6479 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006480out_register_subsys:
6481 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07006482out_register_inetpeer:
6483 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006484out_dst_entries:
6485 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006486out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006487 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006488 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489}
6490
6491void ip6_route_cleanup(void)
6492{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006493 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00006494 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07006495 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07006498 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006499 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006500 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006501 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502}