blob: f0d29fcb20945b5a48097c89dc57daedeed9d177 [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 Dumazet47853052021-08-25 16:17:28 -070044#include <linux/siphash.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020045#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <net/snmp.h>
47#include <net/ipv6.h>
48#include <net/ip6_fib.h>
49#include <net/ip6_route.h>
50#include <net/ndisc.h>
51#include <net/addrconf.h>
52#include <net/tcp.h>
53#include <linux/rtnetlink.h>
54#include <net/dst.h>
Jiri Benc904af042015-08-20 13:56:31 +020055#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <net/xfrm.h>
Tom Tucker8d717402006-07-30 20:43:36 -070057#include <net/netevent.h>
Thomas Graf21713eb2006-08-15 00:35:24 -070058#include <net/netlink.h>
David Ahern3c618c12019-04-20 09:28:20 -070059#include <net/rtnh.h>
Roopa Prabhu19e42e42015-07-21 10:43:48 +020060#include <net/lwtunnel.h>
Jiri Benc904af042015-08-20 13:56:31 +020061#include <net/ip_tunnels.h>
David Ahernca254492015-10-12 11:47:10 -070062#include <net/l3mdev.h>
Roopa Prabhueacb9382018-05-22 14:03:28 -070063#include <net/ip.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080064#include <linux/uaccess.h>
Yonghong Song951cf362020-07-20 09:34:03 -070065#include <linux/btf_ids.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67#ifdef CONFIG_SYSCTL
68#include <linux/sysctl.h>
69#endif
70
David Ahern30d444d2018-05-23 17:08:48 -070071static int ip6_rt_type_to_error(u8 fib6_type);
72
73#define CREATE_TRACE_POINTS
74#include <trace/events/fib6.h>
75EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
76#undef CREATE_TRACE_POINTS
77
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020078enum rt6_nud_state {
Jiri Benc7e980562013-12-11 13:48:20 +010079 RT6_NUD_FAIL_HARD = -3,
80 RT6_NUD_FAIL_PROBE = -2,
81 RT6_NUD_FAIL_DO_RR = -1,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020082 RT6_NUD_SUCCEED = 1
83};
84
Brian Vazquezbbd807d2021-02-01 17:41:32 +000085INDIRECT_CALLABLE_SCOPE
86struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
David S. Miller0dbaee32010-12-13 12:52:14 -080087static unsigned int ip6_default_advmss(const struct dst_entry *dst);
Brian Vazquezf67fbea2021-02-01 17:41:31 +000088INDIRECT_CALLABLE_SCOPE
89unsigned int ip6_mtu(const struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static struct dst_entry *ip6_negative_advice(struct dst_entry *);
91static void ip6_dst_destroy(struct dst_entry *);
92static void ip6_dst_ifdown(struct dst_entry *,
93 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080094static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96static int ip6_pkt_discard(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050097static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Kamala R7150aed2013-12-02 19:55:21 +053098static int ip6_pkt_prohibit(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050099static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static void ip6_link_failure(struct sk_buff *skb);
David S. Miller6700c272012-07-17 03:29:28 -0700101static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +0800102 struct sk_buff *skb, u32 mtu,
103 bool confirm_neigh);
David S. Miller6700c272012-07-17 03:29:28 -0700104static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
105 struct sk_buff *skb);
David Ahern702cea52019-04-09 14:41:13 -0700106static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
107 int strict);
David Aherna1b7a1f2019-06-08 14:53:26 -0700108static size_t rt6_nlmsg_size(struct fib6_info *f6i);
David Ahernd4ead6b2018-04-17 17:33:16 -0700109static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -0700110 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -0700111 struct in6_addr *dest, struct in6_addr *src,
David Ahern16a16cd2017-02-02 12:37:11 -0800112 int iif, int type, u32 portid, u32 seq,
113 unsigned int flags);
David Ahern7e4b5122019-04-16 14:36:00 -0700114static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
Wei Wang510e2ce2019-05-16 13:30:54 -0700115 const struct in6_addr *daddr,
116 const struct in6_addr *saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800118#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -0700119static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000120 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700121 const struct in6_addr *gwaddr,
122 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +0000123 unsigned int pref);
David Ahern8d1c8022018-04-17 17:33:26 -0700124static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000125 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700126 const struct in6_addr *gwaddr,
127 struct net_device *dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800128#endif
129
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700130struct uncached_list {
131 spinlock_t lock;
132 struct list_head head;
133};
134
135static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
136
Xin Long510c3212018-02-14 19:06:02 +0800137void rt6_uncached_list_add(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700138{
139 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
140
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700141 rt->rt6i_uncached_list = ul;
142
143 spin_lock_bh(&ul->lock);
144 list_add_tail(&rt->rt6i_uncached, &ul->head);
145 spin_unlock_bh(&ul->lock);
146}
147
Xin Long510c3212018-02-14 19:06:02 +0800148void rt6_uncached_list_del(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700149{
150 if (!list_empty(&rt->rt6i_uncached)) {
151 struct uncached_list *ul = rt->rt6i_uncached_list;
Wei Wang81eb8442017-10-06 12:06:11 -0700152 struct net *net = dev_net(rt->dst.dev);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700153
154 spin_lock_bh(&ul->lock);
155 list_del(&rt->rt6i_uncached);
Wei Wang81eb8442017-10-06 12:06:11 -0700156 atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700157 spin_unlock_bh(&ul->lock);
158 }
159}
160
161static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
162{
163 struct net_device *loopback_dev = net->loopback_dev;
164 int cpu;
165
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500166 if (dev == loopback_dev)
167 return;
168
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700169 for_each_possible_cpu(cpu) {
170 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
171 struct rt6_info *rt;
172
173 spin_lock_bh(&ul->lock);
174 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
175 struct inet6_dev *rt_idev = rt->rt6i_idev;
176 struct net_device *rt_dev = rt->dst.dev;
177
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500178 if (rt_idev->dev == dev) {
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700179 rt->rt6i_idev = in6_dev_get(loopback_dev);
180 in6_dev_put(rt_idev);
181 }
182
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500183 if (rt_dev == dev) {
Mahesh Bandewar8d7017f2019-07-01 14:38:57 -0700184 rt->dst.dev = blackhole_netdev;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700185 dev_hold(rt->dst.dev);
186 dev_put(rt_dev);
187 }
188 }
189 spin_unlock_bh(&ul->lock);
190 }
191}
192
David Ahernf8a1b432018-04-17 17:33:21 -0700193static inline const void *choose_neigh_daddr(const struct in6_addr *p,
David S. Millerf894cbf2012-07-02 21:52:24 -0700194 struct sk_buff *skb,
195 const void *daddr)
David S. Miller39232972012-01-26 15:22:32 -0500196{
David S. Millera7563f32012-01-26 16:29:16 -0500197 if (!ipv6_addr_any(p))
David S. Miller39232972012-01-26 15:22:32 -0500198 return (const void *) p;
David S. Millerf894cbf2012-07-02 21:52:24 -0700199 else if (skb)
200 return &ipv6_hdr(skb)->daddr;
David S. Miller39232972012-01-26 15:22:32 -0500201 return daddr;
202}
203
David Ahernf8a1b432018-04-17 17:33:21 -0700204struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
205 struct net_device *dev,
206 struct sk_buff *skb,
207 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700208{
David S. Miller39232972012-01-26 15:22:32 -0500209 struct neighbour *n;
210
David Ahernf8a1b432018-04-17 17:33:21 -0700211 daddr = choose_neigh_daddr(gw, skb, daddr);
212 n = __ipv6_neigh_lookup(dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500213 if (n)
214 return n;
Stefano Brivio7adf3242019-01-02 13:29:27 +0100215
216 n = neigh_create(&nd_tbl, daddr, dev);
217 return IS_ERR(n) ? NULL : n;
David Ahernf8a1b432018-04-17 17:33:21 -0700218}
219
220static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
221 struct sk_buff *skb,
222 const void *daddr)
223{
224 const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
225
Nicolas Dichtel2c6b55f2019-06-24 16:01:09 +0200226 return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
227 dst->dev, skb, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500228}
229
Julian Anastasov63fca652017-02-06 23:14:15 +0200230static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
231{
232 struct net_device *dev = dst->dev;
233 struct rt6_info *rt = (struct rt6_info *)dst;
234
Stefano Briviocbfd6892019-09-09 22:44:06 +0200235 daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +0200236 if (!daddr)
237 return;
238 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
239 return;
240 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
241 return;
242 __ipv6_confirm_neigh(dev, daddr);
243}
244
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800245static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 .gc = ip6_dst_gc,
248 .gc_thresh = 1024,
249 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800250 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000251 .mtu = ip6_mtu,
David Ahernd4ead6b2018-04-17 17:33:16 -0700252 .cow_metrics = dst_cow_metrics_generic,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 .destroy = ip6_dst_destroy,
254 .ifdown = ip6_dst_ifdown,
255 .negative_advice = ip6_negative_advice,
256 .link_failure = ip6_link_failure,
257 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700258 .redirect = rt6_do_redirect,
Eric W. Biederman9f8955c2015-10-07 16:48:39 -0500259 .local_out = __ip6_local_out,
David Ahernf8a1b432018-04-17 17:33:21 -0700260 .neigh_lookup = ip6_dst_neigh_lookup,
Julian Anastasov63fca652017-02-06 23:14:15 +0200261 .confirm_neigh = ip6_confirm_neigh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262};
263
David S. Miller14e50e52007-05-24 18:17:54 -0700264static struct dst_ops ip6_dst_blackhole_ops = {
Daniel Borkmannc4c877b2021-03-10 01:38:09 +0100265 .family = AF_INET6,
266 .default_advmss = ip6_default_advmss,
267 .neigh_lookup = ip6_dst_neigh_lookup,
268 .check = ip6_dst_check,
269 .destroy = ip6_dst_destroy,
270 .cow_metrics = dst_cow_metrics_generic,
271 .update_pmtu = dst_blackhole_update_pmtu,
272 .redirect = dst_blackhole_redirect,
273 .mtu = dst_blackhole_mtu,
David S. Miller14e50e52007-05-24 18:17:54 -0700274};
275
David S. Miller62fa8a82011-01-26 20:51:05 -0800276static const u32 ip6_template_metrics[RTAX_MAX] = {
Li RongQing14edd872012-10-24 14:01:18 +0800277 [RTAX_HOPLIMIT - 1] = 0,
David S. Miller62fa8a82011-01-26 20:51:05 -0800278};
279
David Ahern8d1c8022018-04-17 17:33:26 -0700280static const struct fib6_info fib6_null_entry_template = {
David Ahern93c2fb22018-04-18 15:38:59 -0700281 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
282 .fib6_protocol = RTPROT_KERNEL,
283 .fib6_metric = ~(u32)0,
Eric Dumazetf05713e2019-04-22 18:35:03 -0700284 .fib6_ref = REFCOUNT_INIT(1),
David Ahern421842e2018-04-17 17:33:18 -0700285 .fib6_type = RTN_UNREACHABLE,
286 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
287};
288
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000289static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700290 .dst = {
291 .__refcnt = ATOMIC_INIT(1),
292 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000293 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700294 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700295 .input = ip6_pkt_discard,
296 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 },
298 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299};
300
Thomas Graf101367c2006-08-04 03:39:02 -0700301#ifdef CONFIG_IPV6_MULTIPLE_TABLES
302
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000303static const struct rt6_info ip6_prohibit_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 = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700309 .input = ip6_pkt_prohibit,
310 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700311 },
312 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700313};
314
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000315static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700316 .dst = {
317 .__refcnt = ATOMIC_INIT(1),
318 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000319 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700320 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700321 .input = dst_discard,
Eric W. Biedermanede20592015-10-07 16:48:47 -0500322 .output = dst_discard_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700323 },
324 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700325};
326
327#endif
328
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700329static void rt6_info_init(struct rt6_info *rt)
330{
Kees Cook8f2a83b2021-11-18 12:32:41 -0800331 memset_after(rt, 0, dst);
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700332 INIT_LIST_HEAD(&rt->rt6i_uncached);
333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/* allocate dst with ip6_dst_ops */
David Ahern93531c62018-04-17 17:33:25 -0700336struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
337 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
David S. Miller97bab732012-06-09 22:36:36 -0700339 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Wei Wangb2a9c0e2017-06-17 10:42:41 -0700340 1, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700341
Wei Wang81eb8442017-10-06 12:06:11 -0700342 if (rt) {
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700343 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -0700344 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
345 }
Steffen Klassert81048912012-07-05 23:37:09 +0000346
David S. Millercf911662011-04-28 14:31:47 -0700347 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
David Ahern9ab179d2016-04-07 11:10:06 -0700349EXPORT_SYMBOL(ip6_dst_alloc);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351static void ip6_dst_destroy(struct dst_entry *dst)
352{
353 struct rt6_info *rt = (struct rt6_info *)dst;
David Aherna68886a2018-04-20 15:38:02 -0700354 struct fib6_info *from;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700355 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
David Ahern1620a332018-10-04 20:07:54 -0700357 ip_dst_metrics_put(dst);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700358 rt6_uncached_list_del(rt);
359
360 idev = rt->rt6i_idev;
David S. Miller38308472011-12-03 18:02:47 -0500361 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 rt->rt6i_idev = NULL;
363 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900364 }
Gao feng1716a962012-04-06 00:13:10 +0000365
Eric Dumazet0e233872019-04-28 12:22:25 -0700366 from = xchg((__force struct fib6_info **)&rt->from, NULL);
David Ahern93531c62018-04-17 17:33:25 -0700367 fib6_info_release(from);
David S. Millerb3419362010-11-30 12:27:11 -0800368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
371 int how)
372{
373 struct rt6_info *rt = (struct rt6_info *)dst;
374 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800375 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900376 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Wei Wange5645f52017-08-14 10:44:59 -0700378 if (idev && idev->dev != loopback_dev) {
379 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
380 if (loopback_idev) {
381 rt->rt6i_idev = loopback_idev;
382 in6_dev_put(idev);
David S. Miller97cac082012-07-02 22:43:47 -0700383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385}
386
Martin KaFai Lau5973fb12015-11-11 11:51:07 -0800387static bool __rt6_check_expired(const struct rt6_info *rt)
388{
389 if (rt->rt6i_flags & RTF_EXPIRES)
390 return time_after(jiffies, rt->dst.expires);
391 else
392 return false;
393}
394
Eric Dumazeta50feda2012-05-18 18:57:34 +0000395static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
David Aherna68886a2018-04-20 15:38:02 -0700397 struct fib6_info *from;
398
399 from = rcu_dereference(rt->from);
400
Gao feng1716a962012-04-06 00:13:10 +0000401 if (rt->rt6i_flags & RTF_EXPIRES) {
402 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000403 return true;
David Aherna68886a2018-04-20 15:38:02 -0700404 } else if (from) {
Xin Long1e2ea8a2017-08-26 20:10:10 +0800405 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
David Aherna68886a2018-04-20 15:38:02 -0700406 fib6_check_expired(from);
Gao feng1716a962012-04-06 00:13:10 +0000407 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000408 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
David Ahernb1d40992019-04-16 14:35:59 -0700411void fib6_select_path(const struct net *net, struct fib6_result *res,
412 struct flowi6 *fl6, int oif, bool have_oif_match,
413 const struct sk_buff *skb, int strict)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000414{
David Ahern8d1c8022018-04-17 17:33:26 -0700415 struct fib6_info *sibling, *next_sibling;
David Ahernb1d40992019-04-16 14:35:59 -0700416 struct fib6_info *match = res->f6i;
417
David Ahern34fe5a12020-07-06 11:45:07 -0600418 if (!match->nh && (!match->fib6_nsiblings || have_oif_match))
David Ahernb1d40992019-04-16 14:35:59 -0700419 goto out;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000420
David Ahern34fe5a12020-07-06 11:45:07 -0600421 if (match->nh && have_oif_match && res->nh)
422 return;
423
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200424 /* We might have already computed the hash for ICMPv6 errors. In such
425 * case it will always be non-zero. Otherwise now is the time to do it.
426 */
David Ahernf88d8ea2019-06-03 20:19:52 -0700427 if (!fl6->mp_hash &&
428 (!match->nh || nexthop_is_multipath(match->nh)))
David Ahernb4bac172018-03-02 08:32:18 -0800429 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200430
David Ahernf88d8ea2019-06-03 20:19:52 -0700431 if (unlikely(match->nh)) {
432 nexthop_path_fib6_result(res, fl6->mp_hash);
433 return;
434 }
435
David Ahern1cf844c2019-05-22 20:27:59 -0700436 if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
David Ahernb1d40992019-04-16 14:35:59 -0700437 goto out;
Ido Schimmelbbfcd772017-11-21 09:50:12 +0200438
David Ahern93c2fb22018-04-18 15:38:59 -0700439 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
440 fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -0700441 const struct fib6_nh *nh = sibling->fib6_nh;
David Ahern5e670d82018-04-17 17:33:14 -0700442 int nh_upper_bound;
443
David Ahern702cea52019-04-09 14:41:13 -0700444 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
David Ahern5e670d82018-04-17 17:33:14 -0700445 if (fl6->mp_hash > nh_upper_bound)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200446 continue;
David Ahern702cea52019-04-09 14:41:13 -0700447 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200448 break;
449 match = sibling;
450 break;
451 }
452
David Ahernb1d40992019-04-16 14:35:59 -0700453out:
454 res->f6i = match;
David Ahern1cf844c2019-05-22 20:27:59 -0700455 res->nh = match->fib6_nh;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/*
Wei Wang66f5d6c2017-10-06 12:06:10 -0700459 * Route lookup. rcu_read_lock() should be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 */
461
David Ahern0c59d002019-04-09 14:41:18 -0700462static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
463 const struct in6_addr *saddr, int oif, int flags)
464{
465 const struct net_device *dev;
466
467 if (nh->fib_nh_flags & RTNH_F_DEAD)
468 return false;
469
470 dev = nh->fib_nh_dev;
471 if (oif) {
472 if (dev->ifindex == oif)
473 return true;
474 } else {
475 if (ipv6_chk_addr(net, saddr, dev,
476 flags & RT6_LOOKUP_F_IFACE))
477 return true;
478 }
479
480 return false;
481}
482
David Ahern962b6802019-06-08 14:53:24 -0700483struct fib6_nh_dm_arg {
484 struct net *net;
485 const struct in6_addr *saddr;
486 int oif;
487 int flags;
488 struct fib6_nh *nh;
489};
490
491static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
492{
493 struct fib6_nh_dm_arg *arg = _arg;
494
495 arg->nh = nh;
496 return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
497 arg->flags);
498}
499
500/* returns fib6_nh from nexthop or NULL */
501static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
502 struct fib6_result *res,
503 const struct in6_addr *saddr,
504 int oif, int flags)
505{
506 struct fib6_nh_dm_arg arg = {
507 .net = net,
508 .saddr = saddr,
509 .oif = oif,
510 .flags = flags,
511 };
512
513 if (nexthop_is_blackhole(nh))
514 return NULL;
515
516 if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
517 return arg.nh;
518
519 return NULL;
520}
521
David Ahern75ef7382019-04-16 14:36:07 -0700522static void rt6_device_match(struct net *net, struct fib6_result *res,
523 const struct in6_addr *saddr, int oif, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
David Ahern75ef7382019-04-16 14:36:07 -0700525 struct fib6_info *f6i = res->f6i;
526 struct fib6_info *spf6i;
527 struct fib6_nh *nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
David Ahern75ef7382019-04-16 14:36:07 -0700529 if (!oif && ipv6_addr_any(saddr)) {
David Ahernf88d8ea2019-06-03 20:19:52 -0700530 if (unlikely(f6i->nh)) {
531 nh = nexthop_fib6_nh(f6i->nh);
532 if (nexthop_is_blackhole(f6i->nh))
533 goto out_blackhole;
534 } else {
535 nh = f6i->fib6_nh;
536 }
David Ahern7d21fec2019-04-16 14:36:11 -0700537 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
538 goto out;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
David Ahern75ef7382019-04-16 14:36:07 -0700541 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
David Ahern962b6802019-06-08 14:53:24 -0700542 bool matched = false;
543
544 if (unlikely(spf6i->nh)) {
545 nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
546 oif, flags);
547 if (nh)
548 matched = true;
549 } else {
550 nh = spf6i->fib6_nh;
551 if (__rt6_device_match(net, nh, saddr, oif, flags))
552 matched = true;
553 }
554 if (matched) {
David Ahern75ef7382019-04-16 14:36:07 -0700555 res->f6i = spf6i;
David Ahern7d21fec2019-04-16 14:36:11 -0700556 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700557 }
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
David Ahern75ef7382019-04-16 14:36:07 -0700560 if (oif && flags & RT6_LOOKUP_F_IFACE) {
561 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700562 nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700563 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700564 }
565
David Ahernf88d8ea2019-06-03 20:19:52 -0700566 if (unlikely(f6i->nh)) {
567 nh = nexthop_fib6_nh(f6i->nh);
568 if (nexthop_is_blackhole(f6i->nh))
569 goto out_blackhole;
570 } else {
571 nh = f6i->fib6_nh;
572 }
573
David Ahern7d21fec2019-04-16 14:36:11 -0700574 if (nh->fib_nh_flags & RTNH_F_DEAD) {
David Ahern75ef7382019-04-16 14:36:07 -0700575 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700576 nh = res->f6i->fib6_nh;
David Ahern75ef7382019-04-16 14:36:07 -0700577 }
David Ahern7d21fec2019-04-16 14:36:11 -0700578out:
579 res->nh = nh;
580 res->fib6_type = res->f6i->fib6_type;
581 res->fib6_flags = res->f6i->fib6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -0700582 return;
583
584out_blackhole:
585 res->fib6_flags |= RTF_REJECT;
586 res->fib6_type = RTN_BLACKHOLE;
587 res->nh = nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800590#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200591struct __rt6_probe_work {
592 struct work_struct work;
593 struct in6_addr target;
594 struct net_device *dev;
595};
596
597static void rt6_probe_deferred(struct work_struct *w)
598{
599 struct in6_addr mcaddr;
600 struct __rt6_probe_work *work =
601 container_of(w, struct __rt6_probe_work, work);
602
603 addrconf_addr_solict_mult(&work->target, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800604 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200605 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100606 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200607}
608
David Aherncc3a86c2019-04-09 14:41:12 -0700609static void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800610{
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200611 struct __rt6_probe_work *work = NULL;
David Ahern5e670d82018-04-17 17:33:14 -0700612 const struct in6_addr *nh_gw;
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800613 unsigned long last_probe;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000614 struct neighbour *neigh;
David Ahern5e670d82018-04-17 17:33:14 -0700615 struct net_device *dev;
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200616 struct inet6_dev *idev;
David Ahern5e670d82018-04-17 17:33:14 -0700617
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800618 /*
619 * Okay, this does not seem to be appropriate
620 * for now, however, we need to check if it
621 * is really so; aka Router Reachability Probing.
622 *
623 * Router Reachability Probe MUST be rate-limited
624 * to no more than one per minute.
625 */
Hangbin Liu004b3942019-11-20 15:39:06 +0800626 if (!fib6_nh->fib_nh_gw_family)
Amerigo Wangfdd66812012-09-10 02:48:44 +0000627 return;
David Ahern5e670d82018-04-17 17:33:14 -0700628
David Aherncc3a86c2019-04-09 14:41:12 -0700629 nh_gw = &fib6_nh->fib_nh_gw6;
630 dev = fib6_nh->fib_nh_dev;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000631 rcu_read_lock_bh();
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800632 last_probe = READ_ONCE(fib6_nh->last_probe);
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200633 idev = __in6_dev_get(dev);
David Ahern5e670d82018-04-17 17:33:14 -0700634 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000635 if (neigh) {
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700636 if (neigh->nud_state & NUD_VALID)
637 goto out;
638
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000639 write_lock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700640 if (!(neigh->nud_state & NUD_VALID) &&
641 time_after(jiffies,
David Aherndcd1f572018-04-18 15:39:05 -0700642 neigh->updated + idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700643 work = kmalloc(sizeof(*work), GFP_ATOMIC);
644 if (work)
645 __neigh_set_probe_once(neigh);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200646 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000647 write_unlock(&neigh->lock);
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800648 } else if (time_after(jiffies, last_probe +
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200649 idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700650 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000651 }
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700652
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800653 if (!work || cmpxchg(&fib6_nh->last_probe,
654 last_probe, jiffies) != last_probe) {
655 kfree(work);
656 } else {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700657 INIT_WORK(&work->work, rt6_probe_deferred);
David Ahern5e670d82018-04-17 17:33:14 -0700658 work->target = *nh_gw;
659 dev_hold(dev);
660 work->dev = dev;
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700661 schedule_work(&work->work);
662 }
663
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700664out:
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000665 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800666}
667#else
David Aherncc3a86c2019-04-09 14:41:12 -0700668static inline void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800669{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800670}
671#endif
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800674 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 */
David Ahern1ba9a892019-04-09 14:41:10 -0700676static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200678 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
David Ahern5e670d82018-04-17 17:33:14 -0700679 struct neighbour *neigh;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000680
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000681 rcu_read_lock_bh();
David Ahern1ba9a892019-04-09 14:41:10 -0700682 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
683 &fib6_nh->fib_nh_gw6);
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000684 if (neigh) {
685 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800686 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200687 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800688#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000689 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200690 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100691 else
692 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800693#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000694 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200695 } else {
696 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100697 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000698 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000699 rcu_read_unlock_bh();
700
Paul Marksa5a81f02012-12-03 10:26:54 +0000701 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800702}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
David Ahern702cea52019-04-09 14:41:13 -0700704static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
705 int strict)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800706{
David Ahern6e1809a2019-04-09 14:41:11 -0700707 int m = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900708
David Ahern6e1809a2019-04-09 14:41:11 -0700709 if (!oif || nh->fib_nh_dev->ifindex == oif)
710 m = 2;
711
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700712 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200713 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800714#ifdef CONFIG_IPV6_ROUTER_PREF
David Ahern702cea52019-04-09 14:41:13 -0700715 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800716#endif
David Ahern1ba9a892019-04-09 14:41:10 -0700717 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
David Ahern702cea52019-04-09 14:41:13 -0700718 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
David Ahern1ba9a892019-04-09 14:41:10 -0700719 int n = rt6_check_neigh(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200720 if (n < 0)
721 return n;
722 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800723 return m;
724}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
David Ahern28679ed2019-04-09 14:41:14 -0700726static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
727 int oif, int strict, int *mpri, bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800728{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200729 bool match_do_rr = false;
David Ahern28679ed2019-04-09 14:41:14 -0700730 bool rc = false;
731 int m;
Andy Gospodarek35103d12015-08-13 10:39:01 -0400732
David Ahern28679ed2019-04-09 14:41:14 -0700733 if (nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +0200734 goto out;
735
David Ahern28679ed2019-04-09 14:41:14 -0700736 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
737 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
David Ahernd5d32e42016-10-24 12:27:23 -0700738 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
Andy Gospodarek35103d12015-08-13 10:39:01 -0400739 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700740
David Ahern28679ed2019-04-09 14:41:14 -0700741 m = rt6_score_route(nh, fib6_flags, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100742 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200743 match_do_rr = true;
744 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100745 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700746 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700747 }
748
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200749 if (strict & RT6_LOOKUP_F_REACHABLE)
David Ahern28679ed2019-04-09 14:41:14 -0700750 rt6_probe(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200751
Jiri Benc7e980562013-12-11 13:48:20 +0100752 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200753 if (m > *mpri) {
754 *do_rr = match_do_rr;
755 *mpri = m;
David Ahern28679ed2019-04-09 14:41:14 -0700756 rc = true;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200757 }
David S. Millerf11e6652007-03-24 20:36:25 -0700758out:
David Ahern28679ed2019-04-09 14:41:14 -0700759 return rc;
David S. Millerf11e6652007-03-24 20:36:25 -0700760}
761
David Ahern17a59842019-06-08 14:53:25 -0700762struct fib6_nh_frl_arg {
763 u32 flags;
764 int oif;
765 int strict;
766 int *mpri;
767 bool *do_rr;
768 struct fib6_nh *nh;
769};
770
771static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
772{
773 struct fib6_nh_frl_arg *arg = _arg;
774
775 arg->nh = nh;
776 return find_match(nh, arg->flags, arg->oif, arg->strict,
777 arg->mpri, arg->do_rr);
778}
779
David Ahernb7bc4b62019-04-16 14:36:08 -0700780static void __find_rr_leaf(struct fib6_info *f6i_start,
David Ahern30c15f02019-04-09 14:41:15 -0700781 struct fib6_info *nomatch, u32 metric,
David Ahernb7bc4b62019-04-16 14:36:08 -0700782 struct fib6_result *res, struct fib6_info **cont,
David Ahern30c15f02019-04-09 14:41:15 -0700783 int oif, int strict, bool *do_rr, int *mpri)
David S. Millerf11e6652007-03-24 20:36:25 -0700784{
David Ahernb7bc4b62019-04-16 14:36:08 -0700785 struct fib6_info *f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700786
David Ahernb7bc4b62019-04-16 14:36:08 -0700787 for (f6i = f6i_start;
788 f6i && f6i != nomatch;
789 f6i = rcu_dereference(f6i->fib6_next)) {
David Ahern17a59842019-06-08 14:53:25 -0700790 bool matched = false;
David Ahern30c15f02019-04-09 14:41:15 -0700791 struct fib6_nh *nh;
792
David Ahernb7bc4b62019-04-16 14:36:08 -0700793 if (cont && f6i->fib6_metric != metric) {
794 *cont = f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700795 return;
796 }
797
David Ahernb7bc4b62019-04-16 14:36:08 -0700798 if (fib6_check_expired(f6i))
David Ahern30c15f02019-04-09 14:41:15 -0700799 continue;
800
David Ahern17a59842019-06-08 14:53:25 -0700801 if (unlikely(f6i->nh)) {
802 struct fib6_nh_frl_arg arg = {
803 .flags = f6i->fib6_flags,
804 .oif = oif,
805 .strict = strict,
806 .mpri = mpri,
807 .do_rr = do_rr
808 };
809
810 if (nexthop_is_blackhole(f6i->nh)) {
811 res->fib6_flags = RTF_REJECT;
812 res->fib6_type = RTN_BLACKHOLE;
813 res->f6i = f6i;
814 res->nh = nexthop_fib6_nh(f6i->nh);
815 return;
816 }
817 if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
818 &arg)) {
819 matched = true;
820 nh = arg.nh;
821 }
822 } else {
823 nh = f6i->fib6_nh;
824 if (find_match(nh, f6i->fib6_flags, oif, strict,
825 mpri, do_rr))
826 matched = true;
827 }
828 if (matched) {
David Ahernb7bc4b62019-04-16 14:36:08 -0700829 res->f6i = f6i;
830 res->nh = nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700831 res->fib6_flags = f6i->fib6_flags;
832 res->fib6_type = f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700833 }
David Ahern30c15f02019-04-09 14:41:15 -0700834 }
835}
836
David Ahernb7bc4b62019-04-16 14:36:08 -0700837static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
838 struct fib6_info *rr_head, int oif, int strict,
839 bool *do_rr, struct fib6_result *res)
David Ahern30c15f02019-04-09 14:41:15 -0700840{
David Ahernb7bc4b62019-04-16 14:36:08 -0700841 u32 metric = rr_head->fib6_metric;
842 struct fib6_info *cont = NULL;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800843 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
David Ahernb7bc4b62019-04-16 14:36:08 -0700845 __find_rr_leaf(rr_head, NULL, metric, res, &cont,
David Ahern30c15f02019-04-09 14:41:15 -0700846 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700847
David Ahernb7bc4b62019-04-16 14:36:08 -0700848 __find_rr_leaf(leaf, rr_head, metric, res, &cont,
David Ahern30c15f02019-04-09 14:41:15 -0700849 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700850
David Ahernb7bc4b62019-04-16 14:36:08 -0700851 if (res->f6i || !cont)
852 return;
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700853
David Ahernb7bc4b62019-04-16 14:36:08 -0700854 __find_rr_leaf(cont, NULL, metric, res, NULL,
David Ahern30c15f02019-04-09 14:41:15 -0700855 oif, strict, do_rr, &mpri);
David S. Millerf11e6652007-03-24 20:36:25 -0700856}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800857
David Ahernb7bc4b62019-04-16 14:36:08 -0700858static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
859 struct fib6_result *res, int strict)
David S. Millerf11e6652007-03-24 20:36:25 -0700860{
David Ahern8d1c8022018-04-17 17:33:26 -0700861 struct fib6_info *leaf = rcu_dereference(fn->leaf);
David Ahernb7bc4b62019-04-16 14:36:08 -0700862 struct fib6_info *rt0;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200863 bool do_rr = false;
Wei Wang17ecf592017-10-06 12:06:09 -0700864 int key_plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
David Ahernb7bc4b62019-04-16 14:36:08 -0700866 /* make sure this function or its helpers sets f6i */
867 res->f6i = NULL;
868
David Ahern421842e2018-04-17 17:33:18 -0700869 if (!leaf || leaf == net->ipv6.fib6_null_entry)
David Ahernb7bc4b62019-04-16 14:36:08 -0700870 goto out;
Wei Wang8d1040e2017-10-06 12:06:08 -0700871
Wei Wang66f5d6c2017-10-06 12:06:10 -0700872 rt0 = rcu_dereference(fn->rr_ptr);
David S. Millerf11e6652007-03-24 20:36:25 -0700873 if (!rt0)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700874 rt0 = leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Wei Wang17ecf592017-10-06 12:06:09 -0700876 /* Double check to make sure fn is not an intermediate node
877 * and fn->leaf does not points to its child's leaf
878 * (This might happen if all routes under fn are deleted from
879 * the tree and fib6_repair_tree() is called on the node.)
880 */
David Ahern93c2fb22018-04-18 15:38:59 -0700881 key_plen = rt0->fib6_dst.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700882#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -0700883 if (rt0->fib6_src.plen)
884 key_plen = rt0->fib6_src.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700885#endif
886 if (fn->fn_bit != key_plen)
David Ahernb7bc4b62019-04-16 14:36:08 -0700887 goto out;
Wei Wang17ecf592017-10-06 12:06:09 -0700888
David Ahernb7bc4b62019-04-16 14:36:08 -0700889 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200890 if (do_rr) {
David Ahern8fb11a92018-05-04 13:54:24 -0700891 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
David S. Millerf11e6652007-03-24 20:36:25 -0700892
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800893 /* no entries matched; do round-robin */
David Ahern93c2fb22018-04-18 15:38:59 -0700894 if (!next || next->fib6_metric != rt0->fib6_metric)
Wei Wang8d1040e2017-10-06 12:06:08 -0700895 next = leaf;
David S. Millerf11e6652007-03-24 20:36:25 -0700896
Wei Wang66f5d6c2017-10-06 12:06:10 -0700897 if (next != rt0) {
David Ahern93c2fb22018-04-18 15:38:59 -0700898 spin_lock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700899 /* make sure next is not being deleted from the tree */
David Ahern93c2fb22018-04-18 15:38:59 -0700900 if (next->fib6_node)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700901 rcu_assign_pointer(fn->rr_ptr, next);
David Ahern93c2fb22018-04-18 15:38:59 -0700902 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905
David Ahernb7bc4b62019-04-16 14:36:08 -0700906out:
907 if (!res->f6i) {
908 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700909 res->nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700910 res->fib6_flags = res->f6i->fib6_flags;
911 res->fib6_type = res->f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
David Ahern85bd05d2019-04-16 14:36:01 -0700915static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700916{
David Ahern85bd05d2019-04-16 14:36:01 -0700917 return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
918 res->nh->fib_nh_gw_family;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700919}
920
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800921#ifdef CONFIG_IPV6_ROUTE_INFO
922int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000923 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800924{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900925 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800926 struct route_info *rinfo = (struct route_info *) opt;
927 struct in6_addr prefix_buf, *prefix;
928 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900929 unsigned long lifetime;
David Ahern8d1c8022018-04-17 17:33:26 -0700930 struct fib6_info *rt;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800931
932 if (len < sizeof(struct route_info)) {
933 return -EINVAL;
934 }
935
936 /* Sanity check for prefix_len and length */
937 if (rinfo->length > 3) {
938 return -EINVAL;
939 } else if (rinfo->prefix_len > 128) {
940 return -EINVAL;
941 } else if (rinfo->prefix_len > 64) {
942 if (rinfo->length < 2) {
943 return -EINVAL;
944 }
945 } else if (rinfo->prefix_len > 0) {
946 if (rinfo->length < 1) {
947 return -EINVAL;
948 }
949 }
950
951 pref = rinfo->route_pref;
952 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000953 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800954
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900955 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800956
957 if (rinfo->length == 3)
958 prefix = (struct in6_addr *)rinfo->prefix;
959 else {
960 /* this function is safe */
961 ipv6_addr_prefix(&prefix_buf,
962 (struct in6_addr *)rinfo->prefix,
963 rinfo->prefix_len);
964 prefix = &prefix_buf;
965 }
966
Duan Jiongf104a562013-11-08 09:56:53 +0800967 if (rinfo->prefix_len == 0)
David Ahernafb1d4b52018-04-17 17:33:11 -0700968 rt = rt6_get_dflt_router(net, gwaddr, dev);
Duan Jiongf104a562013-11-08 09:56:53 +0800969 else
970 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
David Ahern830218c2016-10-24 10:52:35 -0700971 gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800972
973 if (rt && !lifetime) {
Roopa Prabhu11dd74b2020-04-27 13:56:45 -0700974 ip6_del_rt(net, rt, false);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800975 rt = NULL;
976 }
977
978 if (!rt && lifetime)
David Ahern830218c2016-10-24 10:52:35 -0700979 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
980 dev, pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800981 else if (rt)
David Ahern93c2fb22018-04-18 15:38:59 -0700982 rt->fib6_flags = RTF_ROUTEINFO |
983 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800984
985 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000986 if (!addrconf_finite_timeout(lifetime))
David Ahern14895682018-04-17 17:33:17 -0700987 fib6_clean_expires(rt);
Gao feng1716a962012-04-06 00:13:10 +0000988 else
David Ahern14895682018-04-17 17:33:17 -0700989 fib6_set_expires(rt, jiffies + HZ * lifetime);
Gao feng1716a962012-04-06 00:13:10 +0000990
David Ahern93531c62018-04-17 17:33:25 -0700991 fib6_info_release(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800992 }
993 return 0;
994}
995#endif
996
David Ahernae90d862018-04-17 17:33:12 -0700997/*
998 * Misc support functions
999 */
1000
1001/* called with rcu_lock held */
David Ahern0d161582019-04-16 14:36:04 -07001002static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
David Ahernae90d862018-04-17 17:33:12 -07001003{
David Ahern0d161582019-04-16 14:36:04 -07001004 struct net_device *dev = res->nh->fib_nh_dev;
David Ahernae90d862018-04-17 17:33:12 -07001005
David Ahern7d21fec2019-04-16 14:36:11 -07001006 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
David Ahernae90d862018-04-17 17:33:12 -07001007 /* for copies of local routes, dst->dev needs to be the
1008 * device if it is a master device, the master device if
1009 * device is enslaved, and the loopback as the default
1010 */
1011 if (netif_is_l3_slave(dev) &&
David Ahern7d21fec2019-04-16 14:36:11 -07001012 !rt6_need_strict(&res->f6i->fib6_dst.addr))
David Ahernae90d862018-04-17 17:33:12 -07001013 dev = l3mdev_master_dev_rcu(dev);
1014 else if (!netif_is_l3_master(dev))
1015 dev = dev_net(dev)->loopback_dev;
1016 /* last case is netif_is_l3_master(dev) is true in which
1017 * case we want dev returned to be dev
1018 */
1019 }
1020
1021 return dev;
1022}
1023
David Ahern6edb3c92018-04-17 17:33:15 -07001024static const int fib6_prop[RTN_MAX + 1] = {
1025 [RTN_UNSPEC] = 0,
1026 [RTN_UNICAST] = 0,
1027 [RTN_LOCAL] = 0,
1028 [RTN_BROADCAST] = 0,
1029 [RTN_ANYCAST] = 0,
1030 [RTN_MULTICAST] = 0,
1031 [RTN_BLACKHOLE] = -EINVAL,
1032 [RTN_UNREACHABLE] = -EHOSTUNREACH,
1033 [RTN_PROHIBIT] = -EACCES,
1034 [RTN_THROW] = -EAGAIN,
1035 [RTN_NAT] = -EINVAL,
1036 [RTN_XRESOLVE] = -EINVAL,
1037};
1038
1039static int ip6_rt_type_to_error(u8 fib6_type)
1040{
1041 return fib6_prop[fib6_type];
1042}
1043
David Ahern8d1c8022018-04-17 17:33:26 -07001044static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
David Ahern3b6761d2018-04-17 17:33:20 -07001045{
1046 unsigned short flags = 0;
1047
1048 if (rt->dst_nocount)
1049 flags |= DST_NOCOUNT;
1050 if (rt->dst_nopolicy)
1051 flags |= DST_NOPOLICY;
David Ahern3b6761d2018-04-17 17:33:20 -07001052
1053 return flags;
1054}
1055
David Ahern7d21fec2019-04-16 14:36:11 -07001056static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
David Ahern6edb3c92018-04-17 17:33:15 -07001057{
David Ahern7d21fec2019-04-16 14:36:11 -07001058 rt->dst.error = ip6_rt_type_to_error(fib6_type);
David Ahern6edb3c92018-04-17 17:33:15 -07001059
David Ahern7d21fec2019-04-16 14:36:11 -07001060 switch (fib6_type) {
David Ahern6edb3c92018-04-17 17:33:15 -07001061 case RTN_BLACKHOLE:
1062 rt->dst.output = dst_discard_out;
1063 rt->dst.input = dst_discard;
1064 break;
1065 case RTN_PROHIBIT:
1066 rt->dst.output = ip6_pkt_prohibit_out;
1067 rt->dst.input = ip6_pkt_prohibit;
1068 break;
1069 case RTN_THROW:
1070 case RTN_UNREACHABLE:
1071 default:
1072 rt->dst.output = ip6_pkt_discard_out;
1073 rt->dst.input = ip6_pkt_discard;
1074 break;
1075 }
1076}
1077
David Ahern0d161582019-04-16 14:36:04 -07001078static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
David Ahern6edb3c92018-04-17 17:33:15 -07001079{
David Ahern7d21fec2019-04-16 14:36:11 -07001080 struct fib6_info *f6i = res->f6i;
David Ahern0d161582019-04-16 14:36:04 -07001081
David Ahern7d21fec2019-04-16 14:36:11 -07001082 if (res->fib6_flags & RTF_REJECT) {
1083 ip6_rt_init_dst_reject(rt, res->fib6_type);
David Ahern6edb3c92018-04-17 17:33:15 -07001084 return;
1085 }
1086
1087 rt->dst.error = 0;
1088 rt->dst.output = ip6_output;
1089
David Ahern7d21fec2019-04-16 14:36:11 -07001090 if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
David Ahern6edb3c92018-04-17 17:33:15 -07001091 rt->dst.input = ip6_input;
David Ahern7d21fec2019-04-16 14:36:11 -07001092 } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
David Ahern6edb3c92018-04-17 17:33:15 -07001093 rt->dst.input = ip6_mc_input;
1094 } else {
1095 rt->dst.input = ip6_forward;
1096 }
1097
David Ahern0d161582019-04-16 14:36:04 -07001098 if (res->nh->fib_nh_lws) {
1099 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
David Ahern6edb3c92018-04-17 17:33:15 -07001100 lwtunnel_set_redirect(&rt->dst);
1101 }
1102
1103 rt->dst.lastuse = jiffies;
1104}
1105
Wei Wange873e4b2018-07-21 20:56:32 -07001106/* Caller must already hold reference to @from */
David Ahern8d1c8022018-04-17 17:33:26 -07001107static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
David Ahernae90d862018-04-17 17:33:12 -07001108{
David Ahernae90d862018-04-17 17:33:12 -07001109 rt->rt6i_flags &= ~RTF_EXPIRES;
David Aherna68886a2018-04-20 15:38:02 -07001110 rcu_assign_pointer(rt->from, from);
David Aherne1255ed2018-10-04 20:07:53 -07001111 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
David Ahernae90d862018-04-17 17:33:12 -07001112}
1113
David Ahern0d161582019-04-16 14:36:04 -07001114/* Caller must already hold reference to f6i in result */
1115static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
David Ahernae90d862018-04-17 17:33:12 -07001116{
David Ahern0d161582019-04-16 14:36:04 -07001117 const struct fib6_nh *nh = res->nh;
1118 const struct net_device *dev = nh->fib_nh_dev;
1119 struct fib6_info *f6i = res->f6i;
David Aherndcd1f572018-04-18 15:39:05 -07001120
David Ahern0d161582019-04-16 14:36:04 -07001121 ip6_rt_init_dst(rt, res);
David Ahern6edb3c92018-04-17 17:33:15 -07001122
David Ahern0d161582019-04-16 14:36:04 -07001123 rt->rt6i_dst = f6i->fib6_dst;
David Aherndcd1f572018-04-18 15:39:05 -07001124 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
David Ahern7d21fec2019-04-16 14:36:11 -07001125 rt->rt6i_flags = res->fib6_flags;
David Ahern0d161582019-04-16 14:36:04 -07001126 if (nh->fib_nh_gw_family) {
1127 rt->rt6i_gateway = nh->fib_nh_gw6;
David Ahern2b2450c2019-03-27 20:53:52 -07001128 rt->rt6i_flags |= RTF_GATEWAY;
1129 }
David Ahern0d161582019-04-16 14:36:04 -07001130 rt6_set_from(rt, f6i);
David Ahernae90d862018-04-17 17:33:12 -07001131#ifdef CONFIG_IPV6_SUBTREES
David Ahern0d161582019-04-16 14:36:04 -07001132 rt->rt6i_src = f6i->fib6_src;
David Ahernae90d862018-04-17 17:33:12 -07001133#endif
David Ahernae90d862018-04-17 17:33:12 -07001134}
1135
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001136static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1137 struct in6_addr *saddr)
1138{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001139 struct fib6_node *pn, *sn;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001140 while (1) {
1141 if (fn->fn_flags & RTN_TL_ROOT)
1142 return NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001143 pn = rcu_dereference(fn->parent);
1144 sn = FIB6_SUBTREE(pn);
1145 if (sn && sn != fn)
David Ahern64547432018-05-09 20:34:19 -07001146 fn = fib6_node_lookup(sn, NULL, saddr);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001147 else
1148 fn = pn;
1149 if (fn->fn_flags & RTN_RTINFO)
1150 return fn;
1151 }
1152}
Thomas Grafc71099a2006-08-04 23:20:06 -07001153
David Ahern10585b42019-03-20 09:24:50 -07001154static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
Wei Wangd3843fe2017-10-06 12:06:06 -07001155{
1156 struct rt6_info *rt = *prt;
1157
1158 if (dst_hold_safe(&rt->dst))
1159 return true;
David Ahern10585b42019-03-20 09:24:50 -07001160 if (net) {
Wei Wangd3843fe2017-10-06 12:06:06 -07001161 rt = net->ipv6.ip6_null_entry;
1162 dst_hold(&rt->dst);
1163 } else {
1164 rt = NULL;
1165 }
1166 *prt = rt;
1167 return false;
1168}
1169
David Aherndec9b0e2018-04-17 17:33:19 -07001170/* called with rcu_lock held */
David Ahern9b6b35a2019-04-16 14:36:02 -07001171static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
David Aherndec9b0e2018-04-17 17:33:19 -07001172{
David Ahern9b6b35a2019-04-16 14:36:02 -07001173 struct net_device *dev = res->nh->fib_nh_dev;
1174 struct fib6_info *f6i = res->f6i;
1175 unsigned short flags;
David Aherndec9b0e2018-04-17 17:33:19 -07001176 struct rt6_info *nrt;
1177
David Ahern9b6b35a2019-04-16 14:36:02 -07001178 if (!fib6_info_hold_safe(f6i))
Xin Long1c87e792019-03-20 14:45:48 +08001179 goto fallback;
Wei Wange873e4b2018-07-21 20:56:32 -07001180
David Ahern9b6b35a2019-04-16 14:36:02 -07001181 flags = fib6_info_dst_flags(f6i);
David Ahern93531c62018-04-17 17:33:25 -07001182 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
Xin Long1c87e792019-03-20 14:45:48 +08001183 if (!nrt) {
David Ahern9b6b35a2019-04-16 14:36:02 -07001184 fib6_info_release(f6i);
Xin Long1c87e792019-03-20 14:45:48 +08001185 goto fallback;
1186 }
David Aherndec9b0e2018-04-17 17:33:19 -07001187
David Ahern0d161582019-04-16 14:36:04 -07001188 ip6_rt_copy_init(nrt, res);
Xin Long1c87e792019-03-20 14:45:48 +08001189 return nrt;
1190
1191fallback:
1192 nrt = dev_net(dev)->ipv6.ip6_null_entry;
1193 dst_hold(&nrt->dst);
David Aherndec9b0e2018-04-17 17:33:19 -07001194 return nrt;
1195}
1196
Brian Vazquez55cced42020-06-23 09:42:32 -07001197INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001198 struct fib6_table *table,
David Ahernb75cc8f2018-03-02 08:32:17 -08001199 struct flowi6 *fl6,
1200 const struct sk_buff *skb,
1201 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
David Ahernb1d40992019-04-16 14:35:59 -07001203 struct fib6_result res = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 struct fib6_node *fn;
David Ahern23fb93a2018-04-17 17:33:23 -07001205 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
David Ahernb6cdbc82018-03-29 17:44:57 -07001207 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1208 flags &= ~RT6_LOOKUP_F_IFACE;
1209
Wei Wang66f5d6c2017-10-06 12:06:10 -07001210 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07001211 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -07001212restart:
David Ahernb1d40992019-04-16 14:35:59 -07001213 res.f6i = rcu_dereference(fn->leaf);
1214 if (!res.f6i)
1215 res.f6i = net->ipv6.fib6_null_entry;
David Ahernaf52a522019-04-09 14:41:16 -07001216 else
David Ahern75ef7382019-04-16 14:36:07 -07001217 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1218 flags);
David Ahernaf52a522019-04-09 14:41:16 -07001219
David Ahernb1d40992019-04-16 14:35:59 -07001220 if (res.f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001221 fn = fib6_backtrack(fn, &fl6->saddr);
1222 if (fn)
1223 goto restart;
David Ahernaf52a522019-04-09 14:41:16 -07001224
1225 rt = net->ipv6.ip6_null_entry;
1226 dst_hold(&rt->dst);
1227 goto out;
David Ahernf88d8ea2019-06-03 20:19:52 -07001228 } else if (res.fib6_flags & RTF_REJECT) {
1229 goto do_create;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001230 }
Wei Wang2b760fc2017-10-06 12:06:03 -07001231
David Ahernb1d40992019-04-16 14:35:59 -07001232 fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1233 fl6->flowi6_oif != 0, skb, flags);
1234
David S. Miller4c9483b2011-03-12 16:22:43 -05001235 /* Search through exception table */
David Ahern7e4b5122019-04-16 14:36:00 -07001236 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
David Ahern23fb93a2018-04-17 17:33:23 -07001237 if (rt) {
David Ahern10585b42019-03-20 09:24:50 -07001238 if (ip6_hold_safe(net, &rt))
David Aherndec9b0e2018-04-17 17:33:19 -07001239 dst_use_noref(&rt->dst, jiffies);
David Ahern23fb93a2018-04-17 17:33:23 -07001240 } else {
David Ahernf88d8ea2019-06-03 20:19:52 -07001241do_create:
David Ahern9b6b35a2019-04-16 14:36:02 -07001242 rt = ip6_create_rt_rcu(&res);
David Aherndec9b0e2018-04-17 17:33:19 -07001243 }
Wei Wangd3843fe2017-10-06 12:06:06 -07001244
David Ahernaf52a522019-04-09 14:41:16 -07001245out:
David Ahern8ff2e5b2019-04-16 14:36:09 -07001246 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahernaf52a522019-04-09 14:41:16 -07001247
Wei Wang66f5d6c2017-10-06 12:06:10 -07001248 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08001249
Thomas Grafc71099a2006-08-04 23:20:06 -07001250 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001251}
1252
Ian Morris67ba4152014-08-24 21:53:10 +01001253struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08001254 const struct sk_buff *skb, int flags)
Florian Westphalea6e5742011-09-05 16:05:44 +02001255{
David Ahernb75cc8f2018-03-02 08:32:17 -08001256 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
Florian Westphalea6e5742011-09-05 16:05:44 +02001257}
1258EXPORT_SYMBOL_GPL(ip6_route_lookup);
1259
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001260struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
David Ahernb75cc8f2018-03-02 08:32:17 -08001261 const struct in6_addr *saddr, int oif,
1262 const struct sk_buff *skb, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -07001263{
David S. Miller4c9483b2011-03-12 16:22:43 -05001264 struct flowi6 fl6 = {
1265 .flowi6_oif = oif,
1266 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001267 };
1268 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001269 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07001270
Thomas Grafadaa70b2006-10-13 15:01:03 -07001271 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -05001272 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -07001273 flags |= RT6_LOOKUP_F_HAS_SADDR;
1274 }
1275
David Ahernb75cc8f2018-03-02 08:32:17 -08001276 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -07001277 if (dst->error == 0)
1278 return (struct rt6_info *) dst;
1279
1280 dst_release(dst);
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return NULL;
1283}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001284EXPORT_SYMBOL(rt6_lookup);
1285
Thomas Grafc71099a2006-08-04 23:20:06 -07001286/* ip6_ins_rt is called with FREE table->tb6_lock.
Wei Wang1cfb71e2017-06-17 10:42:33 -07001287 * It takes new route entry, the addition fails by any reason the
1288 * route is released.
1289 * Caller must hold dst before calling it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 */
1291
David Ahern8d1c8022018-04-17 17:33:26 -07001292static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
David Ahern333c4302017-05-21 10:12:04 -06001293 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
1295 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001296 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
David Ahern93c2fb22018-04-18 15:38:59 -07001298 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001299 spin_lock_bh(&table->tb6_lock);
David Ahernd4ead6b2018-04-17 17:33:16 -07001300 err = fib6_add(&table->tb6_root, rt, info, extack);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001301 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 return err;
1304}
1305
David Ahern8d1c8022018-04-17 17:33:26 -07001306int ip6_ins_rt(struct net *net, struct fib6_info *rt)
Thomas Graf40e22e82006-08-22 00:00:45 -07001307{
David Ahernafb1d4b52018-04-17 17:33:11 -07001308 struct nl_info info = { .nl_net = net, };
Florian Westphale715b6d2015-01-05 23:57:44 +01001309
David Ahernd4ead6b2018-04-17 17:33:16 -07001310 return __ip6_ins_rt(rt, &info, NULL);
Thomas Graf40e22e82006-08-22 00:00:45 -07001311}
1312
David Ahern85bd05d2019-04-16 14:36:01 -07001313static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001314 const struct in6_addr *daddr,
1315 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
David Ahern85bd05d2019-04-16 14:36:01 -07001317 struct fib6_info *f6i = res->f6i;
David Ahern4832c302017-08-17 12:17:20 -07001318 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 struct rt6_info *rt;
1320
1321 /*
1322 * Clone the route.
1323 */
1324
David Ahern85bd05d2019-04-16 14:36:01 -07001325 if (!fib6_info_hold_safe(f6i))
Wei Wange873e4b2018-07-21 20:56:32 -07001326 return NULL;
1327
David Ahern0d161582019-04-16 14:36:04 -07001328 dev = ip6_rt_get_dev_rcu(res);
David Ahern93531c62018-04-17 17:33:25 -07001329 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
Wei Wange873e4b2018-07-21 20:56:32 -07001330 if (!rt) {
David Ahern85bd05d2019-04-16 14:36:01 -07001331 fib6_info_release(f6i);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001332 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001333 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001334
David Ahern0d161582019-04-16 14:36:04 -07001335 ip6_rt_copy_init(rt, res);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001336 rt->rt6i_flags |= RTF_CACHE;
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001337 rt->rt6i_dst.addr = *daddr;
1338 rt->rt6i_dst.plen = 128;
1339
David Ahern85bd05d2019-04-16 14:36:01 -07001340 if (!rt6_is_gw_or_nonexthop(res)) {
1341 if (f6i->fib6_dst.plen != 128 &&
1342 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001343 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001345 if (rt->rt6i_src.plen && saddr) {
1346 rt->rt6i_src.addr = *saddr;
1347 rt->rt6i_src.plen = 128;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001348 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001349#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001352 return rt;
1353}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
David Aherndb3fede2019-04-16 14:36:03 -07001355static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001356{
David Aherndb3fede2019-04-16 14:36:03 -07001357 struct fib6_info *f6i = res->f6i;
1358 unsigned short flags = fib6_info_dst_flags(f6i);
David Ahern4832c302017-08-17 12:17:20 -07001359 struct net_device *dev;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001360 struct rt6_info *pcpu_rt;
1361
David Aherndb3fede2019-04-16 14:36:03 -07001362 if (!fib6_info_hold_safe(f6i))
Wei Wange873e4b2018-07-21 20:56:32 -07001363 return NULL;
1364
David Ahern4832c302017-08-17 12:17:20 -07001365 rcu_read_lock();
David Ahern0d161582019-04-16 14:36:04 -07001366 dev = ip6_rt_get_dev_rcu(res);
Eric Dumazetd88829352020-05-08 07:34:14 -07001367 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT);
David Ahern4832c302017-08-17 12:17:20 -07001368 rcu_read_unlock();
Wei Wange873e4b2018-07-21 20:56:32 -07001369 if (!pcpu_rt) {
David Aherndb3fede2019-04-16 14:36:03 -07001370 fib6_info_release(f6i);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001371 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001372 }
David Ahern0d161582019-04-16 14:36:04 -07001373 ip6_rt_copy_init(pcpu_rt, res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001374 pcpu_rt->rt6i_flags |= RTF_PCPU;
David Ahern8f34e532020-05-01 08:53:08 -06001375
1376 if (f6i->nh)
1377 pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
1378
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001379 return pcpu_rt;
1380}
1381
David Ahern8f34e532020-05-01 08:53:08 -06001382static bool rt6_is_valid(const struct rt6_info *rt6)
1383{
1384 return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
1385}
1386
Wei Wang66f5d6c2017-10-06 12:06:10 -07001387/* It should be called with rcu_read_lock() acquired */
David Aherndb3fede2019-04-16 14:36:03 -07001388static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001389{
Eric Dumazetc3530712019-05-31 18:11:25 -07001390 struct rt6_info *pcpu_rt;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001391
Eric Dumazetc3530712019-05-31 18:11:25 -07001392 pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001393
David Ahern8f34e532020-05-01 08:53:08 -06001394 if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
1395 struct rt6_info *prev, **p;
1396
1397 p = this_cpu_ptr(res->nh->rt6i_pcpu);
1398 prev = xchg(p, NULL);
1399 if (prev) {
1400 dst_dev_put(&prev->dst);
1401 dst_release(&prev->dst);
1402 }
1403
1404 pcpu_rt = NULL;
1405 }
1406
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001407 return pcpu_rt;
1408}
1409
David Ahernafb1d4b52018-04-17 17:33:11 -07001410static struct rt6_info *rt6_make_pcpu_route(struct net *net,
David Aherndb3fede2019-04-16 14:36:03 -07001411 const struct fib6_result *res)
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001412{
1413 struct rt6_info *pcpu_rt, *prev, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001414
David Aherndb3fede2019-04-16 14:36:03 -07001415 pcpu_rt = ip6_rt_pcpu_alloc(res);
Wei Wang0e09edc2019-06-20 17:36:37 -07001416 if (!pcpu_rt)
1417 return NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001418
David Ahernf40b6ae2019-05-22 20:27:55 -07001419 p = this_cpu_ptr(res->nh->rt6i_pcpu);
Wei Wanga94b9362017-10-06 12:06:04 -07001420 prev = cmpxchg(p, NULL, pcpu_rt);
Eric Dumazet951f7882017-10-08 21:07:18 -07001421 BUG_ON(prev);
Wei Wanga94b9362017-10-06 12:06:04 -07001422
Eric Dumazet61fb0d02019-05-15 19:39:52 -07001423 if (res->f6i->fib6_destroying) {
1424 struct fib6_info *from;
1425
1426 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1427 fib6_info_release(from);
1428 }
1429
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001430 return pcpu_rt;
1431}
1432
Wei Wang35732d02017-10-06 12:05:57 -07001433/* exception hash table implementation
1434 */
1435static DEFINE_SPINLOCK(rt6_exception_lock);
1436
1437/* Remove rt6_ex from hash table and free the memory
1438 * Caller must hold rt6_exception_lock
1439 */
1440static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1441 struct rt6_exception *rt6_ex)
1442{
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001443 struct fib6_info *from;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001444 struct net *net;
Wei Wang81eb8442017-10-06 12:06:11 -07001445
Wei Wang35732d02017-10-06 12:05:57 -07001446 if (!bucket || !rt6_ex)
1447 return;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001448
1449 net = dev_net(rt6_ex->rt6i->dst.dev);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001450 net->ipv6.rt6_stats->fib_rt_cache--;
1451
1452 /* purge completely the exception to allow releasing the held resources:
1453 * some [sk] cache may keep the dst around for unlimited time
1454 */
Eric Dumazet0e233872019-04-28 12:22:25 -07001455 from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001456 fib6_info_release(from);
1457 dst_dev_put(&rt6_ex->rt6i->dst);
1458
Wei Wang35732d02017-10-06 12:05:57 -07001459 hlist_del_rcu(&rt6_ex->hlist);
David Ahern77634cc2018-04-17 17:33:27 -07001460 dst_release(&rt6_ex->rt6i->dst);
Wei Wang35732d02017-10-06 12:05:57 -07001461 kfree_rcu(rt6_ex, rcu);
1462 WARN_ON_ONCE(!bucket->depth);
1463 bucket->depth--;
1464}
1465
1466/* Remove oldest rt6_ex in bucket and free the memory
1467 * Caller must hold rt6_exception_lock
1468 */
1469static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1470{
1471 struct rt6_exception *rt6_ex, *oldest = NULL;
1472
1473 if (!bucket)
1474 return;
1475
1476 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1477 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1478 oldest = rt6_ex;
1479 }
1480 rt6_remove_exception(bucket, oldest);
1481}
1482
1483static u32 rt6_exception_hash(const struct in6_addr *dst,
1484 const struct in6_addr *src)
1485{
Eric Dumazet49ecc2e92021-11-15 09:23:03 -08001486 static siphash_aligned_key_t rt6_exception_key;
Eric Dumazet47853052021-08-25 16:17:28 -07001487 struct {
1488 struct in6_addr dst;
1489 struct in6_addr src;
1490 } __aligned(SIPHASH_ALIGNMENT) combined = {
1491 .dst = *dst,
1492 };
1493 u64 val;
Wei Wang35732d02017-10-06 12:05:57 -07001494
Eric Dumazet47853052021-08-25 16:17:28 -07001495 net_get_random_once(&rt6_exception_key, sizeof(rt6_exception_key));
Wei Wang35732d02017-10-06 12:05:57 -07001496
1497#ifdef CONFIG_IPV6_SUBTREES
1498 if (src)
Eric Dumazet47853052021-08-25 16:17:28 -07001499 combined.src = *src;
Wei Wang35732d02017-10-06 12:05:57 -07001500#endif
Eric Dumazet47853052021-08-25 16:17:28 -07001501 val = siphash(&combined, sizeof(combined), &rt6_exception_key);
1502
1503 return hash_64(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
Wei Wang35732d02017-10-06 12:05:57 -07001504}
1505
1506/* Helper function to find the cached rt in the hash table
1507 * and update bucket pointer to point to the bucket for this
1508 * (daddr, saddr) pair
1509 * Caller must hold rt6_exception_lock
1510 */
1511static struct rt6_exception *
1512__rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1513 const struct in6_addr *daddr,
1514 const struct in6_addr *saddr)
1515{
1516 struct rt6_exception *rt6_ex;
1517 u32 hval;
1518
1519 if (!(*bucket) || !daddr)
1520 return NULL;
1521
1522 hval = rt6_exception_hash(daddr, saddr);
1523 *bucket += hval;
1524
1525 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1526 struct rt6_info *rt6 = rt6_ex->rt6i;
1527 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1528
1529#ifdef CONFIG_IPV6_SUBTREES
1530 if (matched && saddr)
1531 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1532#endif
1533 if (matched)
1534 return rt6_ex;
1535 }
1536 return NULL;
1537}
1538
1539/* Helper function to find the cached rt in the hash table
1540 * and update bucket pointer to point to the bucket for this
1541 * (daddr, saddr) pair
1542 * Caller must hold rcu_read_lock()
1543 */
1544static struct rt6_exception *
1545__rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1546 const struct in6_addr *daddr,
1547 const struct in6_addr *saddr)
1548{
1549 struct rt6_exception *rt6_ex;
1550 u32 hval;
1551
1552 WARN_ON_ONCE(!rcu_read_lock_held());
1553
1554 if (!(*bucket) || !daddr)
1555 return NULL;
1556
1557 hval = rt6_exception_hash(daddr, saddr);
1558 *bucket += hval;
1559
1560 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1561 struct rt6_info *rt6 = rt6_ex->rt6i;
1562 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1563
1564#ifdef CONFIG_IPV6_SUBTREES
1565 if (matched && saddr)
1566 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1567#endif
1568 if (matched)
1569 return rt6_ex;
1570 }
1571 return NULL;
1572}
1573
David Ahernb748f262019-04-16 14:36:06 -07001574static unsigned int fib6_mtu(const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001575{
David Ahernb748f262019-04-16 14:36:06 -07001576 const struct fib6_nh *nh = res->nh;
David Ahernd4ead6b2018-04-17 17:33:16 -07001577 unsigned int mtu;
1578
David Ahernb748f262019-04-16 14:36:06 -07001579 if (res->f6i->fib6_pmtu) {
1580 mtu = res->f6i->fib6_pmtu;
David Aherndcd1f572018-04-18 15:39:05 -07001581 } else {
David Ahernb748f262019-04-16 14:36:06 -07001582 struct net_device *dev = nh->fib_nh_dev;
David Aherndcd1f572018-04-18 15:39:05 -07001583 struct inet6_dev *idev;
1584
1585 rcu_read_lock();
1586 idev = __in6_dev_get(dev);
1587 mtu = idev->cnf.mtu6;
1588 rcu_read_unlock();
1589 }
1590
David Ahernd4ead6b2018-04-17 17:33:16 -07001591 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1592
David Ahernb748f262019-04-16 14:36:06 -07001593 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahernd4ead6b2018-04-17 17:33:16 -07001594}
1595
David Aherncc5c0732019-05-22 20:27:58 -07001596#define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1597
1598/* used when the flushed bit is not relevant, only access to the bucket
1599 * (ie., all bucket users except rt6_insert_exception);
1600 *
1601 * called under rcu lock; sometimes called with rt6_exception_lock held
1602 */
1603static
1604struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1605 spinlock_t *lock)
1606{
1607 struct rt6_exception_bucket *bucket;
1608
1609 if (lock)
1610 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1611 lockdep_is_held(lock));
1612 else
1613 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1614
1615 /* remove bucket flushed bit if set */
1616 if (bucket) {
1617 unsigned long p = (unsigned long)bucket;
1618
1619 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1620 bucket = (struct rt6_exception_bucket *)p;
1621 }
1622
1623 return bucket;
1624}
1625
1626static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1627{
1628 unsigned long p = (unsigned long)bucket;
1629
1630 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1631}
1632
1633/* called with rt6_exception_lock held */
1634static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1635 spinlock_t *lock)
1636{
1637 struct rt6_exception_bucket *bucket;
1638 unsigned long p;
1639
1640 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1641 lockdep_is_held(lock));
1642
1643 p = (unsigned long)bucket;
1644 p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1645 bucket = (struct rt6_exception_bucket *)p;
1646 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1647}
1648
Wei Wang35732d02017-10-06 12:05:57 -07001649static int rt6_insert_exception(struct rt6_info *nrt,
David Ahern5012f0a2019-04-16 14:36:05 -07001650 const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001651{
David Ahern5e670d82018-04-17 17:33:14 -07001652 struct net *net = dev_net(nrt->dst.dev);
Wei Wang35732d02017-10-06 12:05:57 -07001653 struct rt6_exception_bucket *bucket;
David Aherncc5c0732019-05-22 20:27:58 -07001654 struct fib6_info *f6i = res->f6i;
Wei Wang35732d02017-10-06 12:05:57 -07001655 struct in6_addr *src_key = NULL;
1656 struct rt6_exception *rt6_ex;
David Aherncc5c0732019-05-22 20:27:58 -07001657 struct fib6_nh *nh = res->nh;
Eric Dumazeta00df2c2021-08-29 15:16:14 -07001658 int max_depth;
Wei Wang35732d02017-10-06 12:05:57 -07001659 int err = 0;
1660
Wei Wang35732d02017-10-06 12:05:57 -07001661 spin_lock_bh(&rt6_exception_lock);
1662
David Aherncc5c0732019-05-22 20:27:58 -07001663 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1664 lockdep_is_held(&rt6_exception_lock));
Wei Wang35732d02017-10-06 12:05:57 -07001665 if (!bucket) {
1666 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1667 GFP_ATOMIC);
1668 if (!bucket) {
1669 err = -ENOMEM;
1670 goto out;
1671 }
David Aherncc5c0732019-05-22 20:27:58 -07001672 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1673 } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1674 err = -EINVAL;
1675 goto out;
Wei Wang35732d02017-10-06 12:05:57 -07001676 }
1677
1678#ifdef CONFIG_IPV6_SUBTREES
David Ahern5012f0a2019-04-16 14:36:05 -07001679 /* fib6_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001680 * and exception table is indexed by a hash of
David Ahern5012f0a2019-04-16 14:36:05 -07001681 * both fib6_dst and fib6_src.
Wei Wang35732d02017-10-06 12:05:57 -07001682 * Otherwise, the exception table is indexed by
David Ahern5012f0a2019-04-16 14:36:05 -07001683 * a hash of only fib6_dst.
Wei Wang35732d02017-10-06 12:05:57 -07001684 */
David Ahern5012f0a2019-04-16 14:36:05 -07001685 if (f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001686 src_key = &nrt->rt6i_src.addr;
1687#endif
David Ahern5012f0a2019-04-16 14:36:05 -07001688 /* rt6_mtu_change() might lower mtu on f6i.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001689 * Only insert this exception route if its mtu
David Ahern5012f0a2019-04-16 14:36:05 -07001690 * is less than f6i's mtu value.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001691 */
David Ahernb748f262019-04-16 14:36:06 -07001692 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001693 err = -EINVAL;
1694 goto out;
1695 }
Wei Wang60006a42017-10-06 12:05:58 -07001696
Wei Wang35732d02017-10-06 12:05:57 -07001697 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1698 src_key);
1699 if (rt6_ex)
1700 rt6_remove_exception(bucket, rt6_ex);
1701
1702 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1703 if (!rt6_ex) {
1704 err = -ENOMEM;
1705 goto out;
1706 }
1707 rt6_ex->rt6i = nrt;
1708 rt6_ex->stamp = jiffies;
Wei Wang35732d02017-10-06 12:05:57 -07001709 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1710 bucket->depth++;
Wei Wang81eb8442017-10-06 12:06:11 -07001711 net->ipv6.rt6_stats->fib_rt_cache++;
Wei Wang35732d02017-10-06 12:05:57 -07001712
Eric Dumazeta00df2c2021-08-29 15:16:14 -07001713 /* Randomize max depth to avoid some side channels attacks. */
1714 max_depth = FIB6_MAX_DEPTH + prandom_u32_max(FIB6_MAX_DEPTH);
1715 while (bucket->depth > max_depth)
Wei Wang35732d02017-10-06 12:05:57 -07001716 rt6_exception_remove_oldest(bucket);
1717
1718out:
1719 spin_unlock_bh(&rt6_exception_lock);
1720
1721 /* Update fn->fn_sernum to invalidate all cached dst */
Paolo Abenib886d5f2017-10-19 16:07:10 +02001722 if (!err) {
David Ahern5012f0a2019-04-16 14:36:05 -07001723 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1724 fib6_update_sernum(net, f6i);
1725 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
Paolo Abenib886d5f2017-10-19 16:07:10 +02001726 fib6_force_start_gc(net);
1727 }
Wei Wang35732d02017-10-06 12:05:57 -07001728
1729 return err;
1730}
1731
David Ahernc0b220c2019-05-22 20:27:57 -07001732static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
Wei Wang35732d02017-10-06 12:05:57 -07001733{
1734 struct rt6_exception_bucket *bucket;
1735 struct rt6_exception *rt6_ex;
1736 struct hlist_node *tmp;
1737 int i;
1738
1739 spin_lock_bh(&rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001740
David Aherncc5c0732019-05-22 20:27:58 -07001741 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001742 if (!bucket)
1743 goto out;
1744
David Aherncc5c0732019-05-22 20:27:58 -07001745 /* Prevent rt6_insert_exception() to recreate the bucket list */
1746 if (!from)
1747 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1748
Wei Wang35732d02017-10-06 12:05:57 -07001749 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
David Aherncc5c0732019-05-22 20:27:58 -07001750 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1751 if (!from ||
1752 rcu_access_pointer(rt6_ex->rt6i->from) == from)
1753 rt6_remove_exception(bucket, rt6_ex);
1754 }
1755 WARN_ON_ONCE(!from && bucket->depth);
Wei Wang35732d02017-10-06 12:05:57 -07001756 bucket++;
1757 }
Wei Wang35732d02017-10-06 12:05:57 -07001758out:
1759 spin_unlock_bh(&rt6_exception_lock);
1760}
1761
David Aherne659ba32019-06-08 14:53:28 -07001762static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1763{
1764 struct fib6_info *f6i = arg;
1765
1766 fib6_nh_flush_exceptions(nh, f6i);
1767
1768 return 0;
1769}
1770
David Ahernc0b220c2019-05-22 20:27:57 -07001771void rt6_flush_exceptions(struct fib6_info *f6i)
1772{
David Aherne659ba32019-06-08 14:53:28 -07001773 if (f6i->nh)
1774 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1775 f6i);
1776 else
1777 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
David Ahernc0b220c2019-05-22 20:27:57 -07001778}
1779
Wei Wang35732d02017-10-06 12:05:57 -07001780/* Find cached rt in the hash table inside passed in rt
1781 * Caller has to hold rcu_read_lock()
1782 */
David Ahern7e4b5122019-04-16 14:36:00 -07001783static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
Wei Wang510e2ce2019-05-16 13:30:54 -07001784 const struct in6_addr *daddr,
1785 const struct in6_addr *saddr)
Wei Wang35732d02017-10-06 12:05:57 -07001786{
Wei Wang510e2ce2019-05-16 13:30:54 -07001787 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001788 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001789 struct rt6_exception *rt6_ex;
David Ahern7e4b5122019-04-16 14:36:00 -07001790 struct rt6_info *ret = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001791
Wei Wang35732d02017-10-06 12:05:57 -07001792#ifdef CONFIG_IPV6_SUBTREES
David Ahern7e4b5122019-04-16 14:36:00 -07001793 /* fib6i_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001794 * and exception table is indexed by a hash of
David Ahern7e4b5122019-04-16 14:36:00 -07001795 * both fib6_dst and fib6_src.
Wei Wang510e2ce2019-05-16 13:30:54 -07001796 * However, the src addr used to create the hash
1797 * might not be exactly the passed in saddr which
1798 * is a /128 addr from the flow.
1799 * So we need to use f6i->fib6_src to redo lookup
1800 * if the passed in saddr does not find anything.
1801 * (See the logic in ip6_rt_cache_alloc() on how
1802 * rt->rt6i_src is updated.)
Wei Wang35732d02017-10-06 12:05:57 -07001803 */
David Ahern7e4b5122019-04-16 14:36:00 -07001804 if (res->f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001805 src_key = saddr;
Wei Wang510e2ce2019-05-16 13:30:54 -07001806find_ex:
Wei Wang35732d02017-10-06 12:05:57 -07001807#endif
David Aherncc5c0732019-05-22 20:27:58 -07001808 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001809 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1810
1811 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
David Ahern7e4b5122019-04-16 14:36:00 -07001812 ret = rt6_ex->rt6i;
Wei Wang35732d02017-10-06 12:05:57 -07001813
Wei Wang510e2ce2019-05-16 13:30:54 -07001814#ifdef CONFIG_IPV6_SUBTREES
1815 /* Use fib6_src as src_key and redo lookup */
1816 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1817 src_key = &res->f6i->fib6_src.addr;
1818 goto find_ex;
1819 }
1820#endif
1821
David Ahern7e4b5122019-04-16 14:36:00 -07001822 return ret;
Wei Wang35732d02017-10-06 12:05:57 -07001823}
1824
1825/* Remove the passed in cached rt from the hash table that contains it */
David Aherncc5c0732019-05-22 20:27:58 -07001826static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001827 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001828{
David Ahernc0b220c2019-05-22 20:27:57 -07001829 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001830 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001831 struct rt6_exception *rt6_ex;
1832 int err;
1833
David Aherncc5c0732019-05-22 20:27:58 -07001834 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wang35732d02017-10-06 12:05:57 -07001835 return -ENOENT;
1836
1837 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07001838 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1839
Wei Wang35732d02017-10-06 12:05:57 -07001840#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001841 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1842 * and exception table is indexed by a hash of
1843 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001844 * Otherwise, the exception table is indexed by
1845 * a hash of only rt6i_dst.
1846 */
David Ahernc0b220c2019-05-22 20:27:57 -07001847 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001848 src_key = &rt->rt6i_src.addr;
1849#endif
1850 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1851 &rt->rt6i_dst.addr,
1852 src_key);
1853 if (rt6_ex) {
1854 rt6_remove_exception(bucket, rt6_ex);
1855 err = 0;
1856 } else {
1857 err = -ENOENT;
1858 }
1859
1860 spin_unlock_bh(&rt6_exception_lock);
1861 return err;
1862}
1863
David Aherne659ba32019-06-08 14:53:28 -07001864struct fib6_nh_excptn_arg {
1865 struct rt6_info *rt;
1866 int plen;
1867};
1868
1869static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1870{
1871 struct fib6_nh_excptn_arg *arg = _arg;
1872 int err;
1873
1874 err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1875 if (err == 0)
1876 return 1;
1877
1878 return 0;
1879}
1880
David Ahernc0b220c2019-05-22 20:27:57 -07001881static int rt6_remove_exception_rt(struct rt6_info *rt)
1882{
1883 struct fib6_info *from;
1884
1885 from = rcu_dereference(rt->from);
David Aherncc5c0732019-05-22 20:27:58 -07001886 if (!from || !(rt->rt6i_flags & RTF_CACHE))
David Ahernc0b220c2019-05-22 20:27:57 -07001887 return -EINVAL;
1888
David Aherne659ba32019-06-08 14:53:28 -07001889 if (from->nh) {
1890 struct fib6_nh_excptn_arg arg = {
1891 .rt = rt,
1892 .plen = from->fib6_src.plen
1893 };
1894 int rc;
1895
1896 /* rc = 1 means an entry was found */
1897 rc = nexthop_for_each_fib6_nh(from->nh,
1898 rt6_nh_remove_exception_rt,
1899 &arg);
1900 return rc ? 0 : -ENOENT;
1901 }
1902
David Ahern1cf844c2019-05-22 20:27:59 -07001903 return fib6_nh_remove_exception(from->fib6_nh,
David Aherncc5c0732019-05-22 20:27:58 -07001904 from->fib6_src.plen, rt);
David Ahernc0b220c2019-05-22 20:27:57 -07001905}
1906
Wei Wang35732d02017-10-06 12:05:57 -07001907/* Find rt6_ex which contains the passed in rt cache and
1908 * refresh its stamp
1909 */
David Aherncc5c0732019-05-22 20:27:58 -07001910static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001911 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001912{
David Ahernc0b220c2019-05-22 20:27:57 -07001913 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001914 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001915 struct rt6_exception *rt6_ex;
Paolo Abeni193f3682019-02-21 11:19:41 +01001916
David Aherncc5c0732019-05-22 20:27:58 -07001917 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001918#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001919 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1920 * and exception table is indexed by a hash of
1921 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001922 * Otherwise, the exception table is indexed by
1923 * a hash of only rt6i_dst.
1924 */
David Ahernc0b220c2019-05-22 20:27:57 -07001925 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001926 src_key = &rt->rt6i_src.addr;
1927#endif
David Aherncc5c0732019-05-22 20:27:58 -07001928 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
Wei Wang35732d02017-10-06 12:05:57 -07001929 if (rt6_ex)
1930 rt6_ex->stamp = jiffies;
David Ahernc0b220c2019-05-22 20:27:57 -07001931}
Wei Wang35732d02017-10-06 12:05:57 -07001932
David Aherne659ba32019-06-08 14:53:28 -07001933struct fib6_nh_match_arg {
1934 const struct net_device *dev;
1935 const struct in6_addr *gw;
1936 struct fib6_nh *match;
1937};
1938
1939/* determine if fib6_nh has given device and gateway */
1940static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1941{
1942 struct fib6_nh_match_arg *arg = _arg;
1943
1944 if (arg->dev != nh->fib_nh_dev ||
1945 (arg->gw && !nh->fib_nh_gw_family) ||
1946 (!arg->gw && nh->fib_nh_gw_family) ||
1947 (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1948 return 0;
1949
1950 arg->match = nh;
1951
1952 /* found a match, break the loop */
1953 return 1;
1954}
1955
David Ahernc0b220c2019-05-22 20:27:57 -07001956static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1957{
1958 struct fib6_info *from;
David Aherne659ba32019-06-08 14:53:28 -07001959 struct fib6_nh *fib6_nh;
David Ahernc0b220c2019-05-22 20:27:57 -07001960
1961 rcu_read_lock();
1962
1963 from = rcu_dereference(rt->from);
1964 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1965 goto unlock;
1966
David Aherne659ba32019-06-08 14:53:28 -07001967 if (from->nh) {
1968 struct fib6_nh_match_arg arg = {
1969 .dev = rt->dst.dev,
1970 .gw = &rt->rt6i_gateway,
1971 };
1972
1973 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1974
1975 if (!arg.match)
David Aherncff6a322019-08-01 14:36:35 -07001976 goto unlock;
David Aherne659ba32019-06-08 14:53:28 -07001977 fib6_nh = arg.match;
1978 } else {
1979 fib6_nh = from->fib6_nh;
1980 }
1981 fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
Paolo Abeni193f3682019-02-21 11:19:41 +01001982unlock:
Wei Wang35732d02017-10-06 12:05:57 -07001983 rcu_read_unlock();
1984}
1985
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001986static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1987 struct rt6_info *rt, int mtu)
1988{
1989 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1990 * lowest MTU in the path: always allow updating the route PMTU to
1991 * reflect PMTU decreases.
1992 *
1993 * If the new MTU is higher, and the route PMTU is equal to the local
1994 * MTU, this means the old MTU is the lowest in the path, so allow
1995 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1996 * handle this.
1997 */
1998
1999 if (dst_mtu(&rt->dst) >= mtu)
2000 return true;
2001
2002 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
2003 return true;
2004
2005 return false;
2006}
2007
2008static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
David Aherncc5c0732019-05-22 20:27:58 -07002009 const struct fib6_nh *nh, int mtu)
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002010{
2011 struct rt6_exception_bucket *bucket;
2012 struct rt6_exception *rt6_ex;
2013 int i;
2014
David Aherncc5c0732019-05-22 20:27:58 -07002015 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002016 if (!bucket)
2017 return;
2018
2019 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2020 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2021 struct rt6_info *entry = rt6_ex->rt6i;
2022
2023 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
David Ahernd4ead6b2018-04-17 17:33:16 -07002024 * route), the metrics of its rt->from have already
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002025 * been updated.
2026 */
David Ahernd4ead6b2018-04-17 17:33:16 -07002027 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002028 rt6_mtu_change_route_allowed(idev, entry, mtu))
David Ahernd4ead6b2018-04-17 17:33:16 -07002029 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002030 }
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002031 bucket++;
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002032 }
2033}
2034
Wei Wangb16cb452017-10-06 12:06:00 -07002035#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2036
David Aherncc5c0732019-05-22 20:27:58 -07002037static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2038 const struct in6_addr *gateway)
Wei Wangb16cb452017-10-06 12:06:00 -07002039{
2040 struct rt6_exception_bucket *bucket;
2041 struct rt6_exception *rt6_ex;
2042 struct hlist_node *tmp;
2043 int i;
2044
David Aherncc5c0732019-05-22 20:27:58 -07002045 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangb16cb452017-10-06 12:06:00 -07002046 return;
2047
2048 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07002049 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangb16cb452017-10-06 12:06:00 -07002050 if (bucket) {
2051 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2052 hlist_for_each_entry_safe(rt6_ex, tmp,
2053 &bucket->chain, hlist) {
2054 struct rt6_info *entry = rt6_ex->rt6i;
2055
2056 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2057 RTF_CACHE_GATEWAY &&
2058 ipv6_addr_equal(gateway,
2059 &entry->rt6i_gateway)) {
2060 rt6_remove_exception(bucket, rt6_ex);
2061 }
2062 }
2063 bucket++;
2064 }
2065 }
2066
2067 spin_unlock_bh(&rt6_exception_lock);
2068}
2069
Wei Wangc757faa2017-10-06 12:06:01 -07002070static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2071 struct rt6_exception *rt6_ex,
2072 struct fib6_gc_args *gc_args,
2073 unsigned long now)
2074{
2075 struct rt6_info *rt = rt6_ex->rt6i;
2076
Paolo Abeni1859bac2017-10-19 16:07:11 +02002077 /* we are pruning and obsoleting aged-out and non gateway exceptions
2078 * even if others have still references to them, so that on next
2079 * dst_check() such references can be dropped.
2080 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2081 * expired, independently from their aging, as per RFC 8201 section 4
2082 */
Wei Wang31afeb42018-01-26 11:40:17 -08002083 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2084 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2085 RT6_TRACE("aging clone %p\n", rt);
2086 rt6_remove_exception(bucket, rt6_ex);
2087 return;
2088 }
2089 } else if (time_after(jiffies, rt->dst.expires)) {
2090 RT6_TRACE("purging expired route %p\n", rt);
Wei Wangc757faa2017-10-06 12:06:01 -07002091 rt6_remove_exception(bucket, rt6_ex);
2092 return;
Wei Wang31afeb42018-01-26 11:40:17 -08002093 }
2094
2095 if (rt->rt6i_flags & RTF_GATEWAY) {
Wei Wangc757faa2017-10-06 12:06:01 -07002096 struct neighbour *neigh;
Wei Wangc757faa2017-10-06 12:06:01 -07002097
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002098 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002099
Xu Jiab7a320c2021-04-01 11:22:23 +08002100 if (!(neigh && (neigh->flags & NTF_ROUTER))) {
Wei Wangc757faa2017-10-06 12:06:01 -07002101 RT6_TRACE("purging route %p via non-router but gateway\n",
2102 rt);
2103 rt6_remove_exception(bucket, rt6_ex);
2104 return;
2105 }
2106 }
Wei Wang31afeb42018-01-26 11:40:17 -08002107
Wei Wangc757faa2017-10-06 12:06:01 -07002108 gc_args->more++;
2109}
2110
David Aherncc5c0732019-05-22 20:27:58 -07002111static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
David Ahernc0b220c2019-05-22 20:27:57 -07002112 struct fib6_gc_args *gc_args,
2113 unsigned long now)
Wei Wangc757faa2017-10-06 12:06:01 -07002114{
2115 struct rt6_exception_bucket *bucket;
2116 struct rt6_exception *rt6_ex;
2117 struct hlist_node *tmp;
2118 int i;
2119
David Aherncc5c0732019-05-22 20:27:58 -07002120 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangc757faa2017-10-06 12:06:01 -07002121 return;
2122
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002123 rcu_read_lock_bh();
2124 spin_lock(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07002125 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangc757faa2017-10-06 12:06:01 -07002126 if (bucket) {
2127 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2128 hlist_for_each_entry_safe(rt6_ex, tmp,
2129 &bucket->chain, hlist) {
2130 rt6_age_examine_exception(bucket, rt6_ex,
2131 gc_args, now);
2132 }
2133 bucket++;
2134 }
2135 }
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002136 spin_unlock(&rt6_exception_lock);
2137 rcu_read_unlock_bh();
Wei Wangc757faa2017-10-06 12:06:01 -07002138}
2139
David Aherne659ba32019-06-08 14:53:28 -07002140struct fib6_nh_age_excptn_arg {
2141 struct fib6_gc_args *gc_args;
2142 unsigned long now;
2143};
2144
2145static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2146{
2147 struct fib6_nh_age_excptn_arg *arg = _arg;
2148
2149 fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2150 return 0;
2151}
2152
David Aherncc5c0732019-05-22 20:27:58 -07002153void rt6_age_exceptions(struct fib6_info *f6i,
David Ahernc0b220c2019-05-22 20:27:57 -07002154 struct fib6_gc_args *gc_args,
2155 unsigned long now)
2156{
David Aherne659ba32019-06-08 14:53:28 -07002157 if (f6i->nh) {
2158 struct fib6_nh_age_excptn_arg arg = {
2159 .gc_args = gc_args,
2160 .now = now
2161 };
2162
2163 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2164 &arg);
2165 } else {
2166 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2167 }
David Ahernc0b220c2019-05-22 20:27:57 -07002168}
2169
David Ahern1d053da2018-05-09 20:34:21 -07002170/* must be called with rcu lock held */
David Aherneffda4d2019-04-16 14:36:10 -07002171int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2172 struct flowi6 *fl6, struct fib6_result *res, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002174 struct fib6_node *fn, *saved_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
David Ahern64547432018-05-09 20:34:19 -07002176 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002177 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
David Ahernca254492015-10-12 11:47:10 -07002179 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2180 oif = 0;
2181
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002182redo_rt6_select:
David Aherneffda4d2019-04-16 14:36:10 -07002183 rt6_select(net, fn, oif, res, strict);
2184 if (res->f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002185 fn = fib6_backtrack(fn, &fl6->saddr);
2186 if (fn)
2187 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002188 else if (strict & RT6_LOOKUP_F_REACHABLE) {
2189 /* also consider unreachable route */
2190 strict &= ~RT6_LOOKUP_F_REACHABLE;
2191 fn = saved_fn;
2192 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002193 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002194 }
2195
David Aherneffda4d2019-04-16 14:36:10 -07002196 trace_fib6_table_lookup(net, res, table, fl6);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -08002197
David Aherneffda4d2019-04-16 14:36:10 -07002198 return 0;
David Ahern1d053da2018-05-09 20:34:21 -07002199}
2200
2201struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2202 int oif, struct flowi6 *fl6,
2203 const struct sk_buff *skb, int flags)
2204{
David Ahernb1d40992019-04-16 14:35:59 -07002205 struct fib6_result res = {};
Wei Wang0e09edc2019-06-20 17:36:37 -07002206 struct rt6_info *rt = NULL;
David Ahern1d053da2018-05-09 20:34:21 -07002207 int strict = 0;
2208
Wei Wang0e09edc2019-06-20 17:36:37 -07002209 WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
2210 !rcu_read_lock_held());
2211
David Ahern1d053da2018-05-09 20:34:21 -07002212 strict |= flags & RT6_LOOKUP_F_IFACE;
2213 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
2214 if (net->ipv6.devconf_all->forwarding == 0)
2215 strict |= RT6_LOOKUP_F_REACHABLE;
2216
2217 rcu_read_lock();
2218
David Aherneffda4d2019-04-16 14:36:10 -07002219 fib6_table_lookup(net, table, oif, fl6, &res, strict);
Wei Wang0e09edc2019-06-20 17:36:37 -07002220 if (res.f6i == net->ipv6.fib6_null_entry)
2221 goto out;
David Ahern23fb93a2018-04-17 17:33:23 -07002222
David Ahernb1d40992019-04-16 14:35:59 -07002223 fib6_select_path(net, &res, fl6, oif, false, skb, strict);
David Ahernd83009d2019-04-09 14:41:17 -07002224
David Ahern23fb93a2018-04-17 17:33:23 -07002225 /*Search through exception table */
David Ahern7e4b5122019-04-16 14:36:00 -07002226 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
David Ahern23fb93a2018-04-17 17:33:23 -07002227 if (rt) {
Wei Wang0e09edc2019-06-20 17:36:37 -07002228 goto out;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002229 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
David Ahernb1d40992019-04-16 14:35:59 -07002230 !res.nh->fib_nh_gw_family)) {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002231 /* Create a RTF_CACHE clone which will not be
2232 * owned by the fib6 tree. It is for the special case where
2233 * the daddr in the skb during the neighbor look-up is different
2234 * from the fl6->daddr used to look-up route here.
2235 */
Wei Wang0e09edc2019-06-20 17:36:37 -07002236 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002237
Wei Wang0e09edc2019-06-20 17:36:37 -07002238 if (rt) {
2239 /* 1 refcnt is taken during ip6_rt_cache_alloc().
2240 * As rt6_uncached_list_add() does not consume refcnt,
2241 * this refcnt is always returned to the caller even
2242 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
Wei Wang1cfb71e2017-06-17 10:42:33 -07002243 */
Wei Wang0e09edc2019-06-20 17:36:37 -07002244 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002245 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Wei Wang0e09edc2019-06-20 17:36:37 -07002246 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08002247
Wei Wang0e09edc2019-06-20 17:36:37 -07002248 return rt;
2249 }
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002250 } else {
2251 /* Get a percpu copy */
Eric Dumazet951f7882017-10-08 21:07:18 -07002252 local_bh_disable();
Wei Wang0e09edc2019-06-20 17:36:37 -07002253 rt = rt6_get_pcpu_route(&res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002254
Wei Wang0e09edc2019-06-20 17:36:37 -07002255 if (!rt)
2256 rt = rt6_make_pcpu_route(net, &res);
David Ahern93531c62018-04-17 17:33:25 -07002257
Eric Dumazet951f7882017-10-08 21:07:18 -07002258 local_bh_enable();
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002259 }
Wei Wang0e09edc2019-06-20 17:36:37 -07002260out:
2261 if (!rt)
2262 rt = net->ipv6.ip6_null_entry;
2263 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
2264 ip6_hold_safe(net, &rt);
2265 rcu_read_unlock();
2266
2267 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002268}
David Ahern9ff74382016-06-13 13:44:19 -07002269EXPORT_SYMBOL_GPL(ip6_pol_route);
Thomas Grafc71099a2006-08-04 23:20:06 -07002270
Brian Vazquez55cced42020-06-23 09:42:32 -07002271INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_input(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08002272 struct fib6_table *table,
2273 struct flowi6 *fl6,
2274 const struct sk_buff *skb,
2275 int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002276{
David Ahernb75cc8f2018-03-02 08:32:17 -08002277 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002278}
2279
Mahesh Bandeward409b842016-09-16 12:59:08 -07002280struct dst_entry *ip6_route_input_lookup(struct net *net,
2281 struct net_device *dev,
David Ahernb75cc8f2018-03-02 08:32:17 -08002282 struct flowi6 *fl6,
2283 const struct sk_buff *skb,
2284 int flags)
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002285{
2286 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2287 flags |= RT6_LOOKUP_F_IFACE;
2288
David Ahernb75cc8f2018-03-02 08:32:17 -08002289 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002290}
Mahesh Bandeward409b842016-09-16 12:59:08 -07002291EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002292
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002293static void ip6_multipath_l3_keys(const struct sk_buff *skb,
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002294 struct flow_keys *keys,
2295 struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002296{
2297 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2298 const struct ipv6hdr *key_iph = outer_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002299 struct flow_keys *_flkeys = flkeys;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002300 const struct ipv6hdr *inner_iph;
2301 const struct icmp6hdr *icmph;
2302 struct ipv6hdr _inner_iph;
Eric Dumazetcea67a22018-04-29 09:54:59 -07002303 struct icmp6hdr _icmph;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002304
2305 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2306 goto out;
2307
Eric Dumazetcea67a22018-04-29 09:54:59 -07002308 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2309 sizeof(_icmph), &_icmph);
2310 if (!icmph)
2311 goto out;
2312
Matteo Croce54074f12019-11-02 01:12:04 +01002313 if (!icmpv6_is_err(icmph->icmp6_type))
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002314 goto out;
2315
2316 inner_iph = skb_header_pointer(skb,
2317 skb_transport_offset(skb) + sizeof(*icmph),
2318 sizeof(_inner_iph), &_inner_iph);
2319 if (!inner_iph)
2320 goto out;
2321
2322 key_iph = inner_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002323 _flkeys = NULL;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002324out:
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002325 if (_flkeys) {
2326 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2327 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2328 keys->tags.flow_label = _flkeys->tags.flow_label;
2329 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2330 } else {
2331 keys->addrs.v6addrs.src = key_iph->saddr;
2332 keys->addrs.v6addrs.dst = key_iph->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002333 keys->tags.flow_label = ip6_flowlabel(key_iph);
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002334 keys->basic.ip_proto = key_iph->nexthdr;
2335 }
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002336}
2337
Ido Schimmel73c2c5c2021-05-17 21:15:23 +03002338static u32 rt6_multipath_custom_hash_outer(const struct net *net,
2339 const struct sk_buff *skb,
2340 bool *p_has_inner)
2341{
2342 u32 hash_fields = ip6_multipath_hash_fields(net);
2343 struct flow_keys keys, hash_keys;
2344
2345 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
2346 return 0;
2347
2348 memset(&hash_keys, 0, sizeof(hash_keys));
2349 skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_ENCAP);
2350
2351 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2352 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
2353 hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
2354 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
2355 hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
2356 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
2357 hash_keys.basic.ip_proto = keys.basic.ip_proto;
2358 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL)
2359 hash_keys.tags.flow_label = keys.tags.flow_label;
2360 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
2361 hash_keys.ports.src = keys.ports.src;
2362 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
2363 hash_keys.ports.dst = keys.ports.dst;
2364
2365 *p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION);
2366 return flow_hash_from_keys(&hash_keys);
2367}
2368
2369static u32 rt6_multipath_custom_hash_inner(const struct net *net,
2370 const struct sk_buff *skb,
2371 bool has_inner)
2372{
2373 u32 hash_fields = ip6_multipath_hash_fields(net);
2374 struct flow_keys keys, hash_keys;
2375
2376 /* We assume the packet carries an encapsulation, but if none was
2377 * encountered during dissection of the outer flow, then there is no
2378 * point in calling the flow dissector again.
2379 */
2380 if (!has_inner)
2381 return 0;
2382
2383 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_MASK))
2384 return 0;
2385
2386 memset(&hash_keys, 0, sizeof(hash_keys));
2387 skb_flow_dissect_flow_keys(skb, &keys, 0);
2388
2389 if (!(keys.control.flags & FLOW_DIS_ENCAPSULATION))
2390 return 0;
2391
2392 if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2393 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2394 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
2395 hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
2396 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
2397 hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
2398 } else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2399 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2400 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
2401 hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
2402 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
2403 hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
2404 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL)
2405 hash_keys.tags.flow_label = keys.tags.flow_label;
2406 }
2407
2408 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO)
2409 hash_keys.basic.ip_proto = keys.basic.ip_proto;
2410 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT)
2411 hash_keys.ports.src = keys.ports.src;
2412 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
2413 hash_keys.ports.dst = keys.ports.dst;
2414
2415 return flow_hash_from_keys(&hash_keys);
2416}
2417
2418static u32 rt6_multipath_custom_hash_skb(const struct net *net,
2419 const struct sk_buff *skb)
2420{
2421 u32 mhash, mhash_inner;
2422 bool has_inner = true;
2423
2424 mhash = rt6_multipath_custom_hash_outer(net, skb, &has_inner);
2425 mhash_inner = rt6_multipath_custom_hash_inner(net, skb, has_inner);
2426
2427 return jhash_2words(mhash, mhash_inner, 0);
2428}
2429
2430static u32 rt6_multipath_custom_hash_fl6(const struct net *net,
2431 const struct flowi6 *fl6)
2432{
2433 u32 hash_fields = ip6_multipath_hash_fields(net);
2434 struct flow_keys hash_keys;
2435
2436 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
2437 return 0;
2438
2439 memset(&hash_keys, 0, sizeof(hash_keys));
2440 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2441 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
2442 hash_keys.addrs.v6addrs.src = fl6->saddr;
2443 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
2444 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2445 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
2446 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2447 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL)
2448 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2449 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
2450 hash_keys.ports.src = fl6->fl6_sport;
2451 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
2452 hash_keys.ports.dst = fl6->fl6_dport;
2453
2454 return flow_hash_from_keys(&hash_keys);
2455}
2456
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002457/* if skb is set it will be used and fl6 can be NULL */
David Ahernb4bac172018-03-02 08:32:18 -08002458u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2459 const struct sk_buff *skb, struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002460{
2461 struct flow_keys hash_keys;
Ido Schimmelb95b6e02021-05-17 21:15:21 +03002462 u32 mhash = 0;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002463
David S. Millerbbfa0472018-03-12 11:09:33 -04002464 switch (ip6_multipath_hash_policy(net)) {
David Ahernb4bac172018-03-02 08:32:18 -08002465 case 0:
2466 memset(&hash_keys, 0, sizeof(hash_keys));
2467 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2468 if (skb) {
2469 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2470 } else {
2471 hash_keys.addrs.v6addrs.src = fl6->saddr;
2472 hash_keys.addrs.v6addrs.dst = fl6->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002473 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
David Ahernb4bac172018-03-02 08:32:18 -08002474 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2475 }
Ido Schimmelb95b6e02021-05-17 21:15:21 +03002476 mhash = flow_hash_from_keys(&hash_keys);
David Ahernb4bac172018-03-02 08:32:18 -08002477 break;
2478 case 1:
2479 if (skb) {
2480 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2481 struct flow_keys keys;
2482
2483 /* short-circuit if we already have L4 hash present */
2484 if (skb->l4_hash)
2485 return skb_get_hash_raw(skb) >> 1;
2486
2487 memset(&hash_keys, 0, sizeof(hash_keys));
2488
Shubhankar Kuranagatti13fdb942021-03-11 02:03:14 +05302489 if (!flkeys) {
David Ahernb4bac172018-03-02 08:32:18 -08002490 skb_flow_dissect_flow_keys(skb, &keys, flag);
2491 flkeys = &keys;
2492 }
2493 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2494 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2495 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2496 hash_keys.ports.src = flkeys->ports.src;
2497 hash_keys.ports.dst = flkeys->ports.dst;
2498 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2499 } else {
2500 memset(&hash_keys, 0, sizeof(hash_keys));
2501 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2502 hash_keys.addrs.v6addrs.src = fl6->saddr;
2503 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2504 hash_keys.ports.src = fl6->fl6_sport;
2505 hash_keys.ports.dst = fl6->fl6_dport;
2506 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2507 }
Ido Schimmelb95b6e02021-05-17 21:15:21 +03002508 mhash = flow_hash_from_keys(&hash_keys);
David Ahernb4bac172018-03-02 08:32:18 -08002509 break;
Stephen Suryaputrad8f74f02019-07-06 10:55:18 -04002510 case 2:
2511 memset(&hash_keys, 0, sizeof(hash_keys));
2512 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2513 if (skb) {
2514 struct flow_keys keys;
2515
2516 if (!flkeys) {
2517 skb_flow_dissect_flow_keys(skb, &keys, 0);
2518 flkeys = &keys;
2519 }
2520
2521 /* Inner can be v4 or v6 */
2522 if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2523 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2524 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2525 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2526 } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2527 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2528 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2529 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2530 hash_keys.tags.flow_label = flkeys->tags.flow_label;
2531 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2532 } else {
2533 /* Same as case 0 */
2534 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2535 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2536 }
2537 } else {
2538 /* Same as case 0 */
2539 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2540 hash_keys.addrs.v6addrs.src = fl6->saddr;
2541 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2542 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2543 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2544 }
Ido Schimmelb95b6e02021-05-17 21:15:21 +03002545 mhash = flow_hash_from_keys(&hash_keys);
Stephen Suryaputrad8f74f02019-07-06 10:55:18 -04002546 break;
Ido Schimmel73c2c5c2021-05-17 21:15:23 +03002547 case 3:
2548 if (skb)
2549 mhash = rt6_multipath_custom_hash_skb(net, skb);
2550 else
2551 mhash = rt6_multipath_custom_hash_fl6(net, fl6);
2552 break;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002553 }
2554
David Ahern9a2a5372018-03-02 08:32:15 -08002555 return mhash >> 1;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002556}
2557
Wei Wang67f415d2019-06-20 17:36:40 -07002558/* Called with rcu held */
Thomas Grafc71099a2006-08-04 23:20:06 -07002559void ip6_route_input(struct sk_buff *skb)
2560{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002561 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002562 struct net *net = dev_net(skb->dev);
Wei Wang67f415d2019-06-20 17:36:40 -07002563 int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
Jiri Benc904af042015-08-20 13:56:31 +02002564 struct ip_tunnel_info *tun_info;
David S. Miller4c9483b2011-03-12 16:22:43 -05002565 struct flowi6 fl6 = {
David Aherne0d56fd2016-09-10 12:09:57 -07002566 .flowi6_iif = skb->dev->ifindex,
David S. Miller4c9483b2011-03-12 16:22:43 -05002567 .daddr = iph->daddr,
2568 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002569 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05002570 .flowi6_mark = skb->mark,
2571 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07002572 };
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002573 struct flow_keys *flkeys = NULL, _flkeys;
Thomas Grafadaa70b2006-10-13 15:01:03 -07002574
Jiri Benc904af042015-08-20 13:56:31 +02002575 tun_info = skb_tunnel_info(skb);
Jiri Benc46fa0622015-08-28 20:48:19 +02002576 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
Jiri Benc904af042015-08-20 13:56:31 +02002577 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002578
2579 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2580 flkeys = &_flkeys;
2581
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002582 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
David Ahernb4bac172018-03-02 08:32:18 -08002583 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
Jiri Benc06e9d042015-08-20 13:56:26 +02002584 skb_dst_drop(skb);
Wei Wang67f415d2019-06-20 17:36:40 -07002585 skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
2586 &fl6, skb, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07002587}
2588
Brian Vazquez55cced42020-06-23 09:42:32 -07002589INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_output(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08002590 struct fib6_table *table,
2591 struct flowi6 *fl6,
2592 const struct sk_buff *skb,
2593 int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002594{
David Ahernb75cc8f2018-03-02 08:32:17 -08002595 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07002596}
2597
Wei Wang7d9e5f42019-06-20 17:36:41 -07002598struct dst_entry *ip6_route_output_flags_noref(struct net *net,
2599 const struct sock *sk,
2600 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002601{
David Ahernd46a9d62015-10-21 08:42:22 -07002602 bool any_src;
Thomas Grafc71099a2006-08-04 23:20:06 -07002603
Robert Shearman3ede0bb2018-09-19 13:56:53 +01002604 if (ipv6_addr_type(&fl6->daddr) &
2605 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
David Ahern4c1feac2016-09-10 12:09:56 -07002606 struct dst_entry *dst;
2607
Wei Wang7d9e5f42019-06-20 17:36:41 -07002608 /* This function does not take refcnt on the dst */
David Ahern4c1feac2016-09-10 12:09:56 -07002609 dst = l3mdev_link_scope_lookup(net, fl6);
2610 if (dst)
2611 return dst;
2612 }
David Ahernca254492015-10-12 11:47:10 -07002613
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002614 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00002615
Wei Wang7d9e5f42019-06-20 17:36:41 -07002616 flags |= RT6_LOOKUP_F_DST_NOREF;
David Ahernd46a9d62015-10-21 08:42:22 -07002617 any_src = ipv6_addr_any(&fl6->saddr);
David Ahern741a11d2015-09-28 10:12:13 -07002618 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
David Ahernd46a9d62015-10-21 08:42:22 -07002619 (fl6->flowi6_oif && any_src))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07002620 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07002621
David Ahernd46a9d62015-10-21 08:42:22 -07002622 if (!any_src)
Thomas Grafadaa70b2006-10-13 15:01:03 -07002623 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00002624 else if (sk)
2625 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002626
David Ahernb75cc8f2018-03-02 08:32:17 -08002627 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628}
Wei Wang7d9e5f42019-06-20 17:36:41 -07002629EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref);
2630
2631struct dst_entry *ip6_route_output_flags(struct net *net,
2632 const struct sock *sk,
2633 struct flowi6 *fl6,
2634 int flags)
2635{
Shubhankar Kuranagatti13fdb942021-03-11 02:03:14 +05302636 struct dst_entry *dst;
2637 struct rt6_info *rt6;
Wei Wang7d9e5f42019-06-20 17:36:41 -07002638
Shubhankar Kuranagatti13fdb942021-03-11 02:03:14 +05302639 rcu_read_lock();
2640 dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
2641 rt6 = (struct rt6_info *)dst;
2642 /* For dst cached in uncached_list, refcnt is already taken. */
2643 if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) {
2644 dst = &net->ipv6.ip6_null_entry->dst;
2645 dst_hold(dst);
2646 }
2647 rcu_read_unlock();
Wei Wang7d9e5f42019-06-20 17:36:41 -07002648
Shubhankar Kuranagatti13fdb942021-03-11 02:03:14 +05302649 return dst;
Wei Wang7d9e5f42019-06-20 17:36:41 -07002650}
Paolo Abeni6f21c962016-01-29 12:30:19 +01002651EXPORT_SYMBOL_GPL(ip6_route_output_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
David S. Miller2774c132011-03-01 14:59:04 -08002653struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07002654{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002655 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
Wei Wang1dbe32522017-06-17 10:42:26 -07002656 struct net_device *loopback_dev = net->loopback_dev;
David S. Miller14e50e52007-05-24 18:17:54 -07002657 struct dst_entry *new = NULL;
2658
Wei Wang1dbe32522017-06-17 10:42:26 -07002659 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
Steffen Klassert62cf27e2017-10-09 08:39:43 +02002660 DST_OBSOLETE_DEAD, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07002661 if (rt) {
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002662 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002663 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002664
Changli Gaod8d1f302010-06-10 23:31:35 -07002665 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07002666 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08002667 new->input = dst_discard;
Eric W. Biedermanede20592015-10-07 16:48:47 -05002668 new->output = dst_discard_out;
David S. Miller14e50e52007-05-24 18:17:54 -07002669
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002670 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07002671
Wei Wang1dbe32522017-06-17 10:42:26 -07002672 rt->rt6i_idev = in6_dev_get(loopback_dev);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002673 rt->rt6i_gateway = ort->rt6i_gateway;
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002674 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
David S. Miller14e50e52007-05-24 18:17:54 -07002675
2676 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2677#ifdef CONFIG_IPV6_SUBTREES
2678 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2679#endif
David S. Miller14e50e52007-05-24 18:17:54 -07002680 }
2681
David S. Miller69ead7a2011-03-01 14:45:33 -08002682 dst_release(dst_orig);
2683 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07002684}
David S. Miller14e50e52007-05-24 18:17:54 -07002685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686/*
2687 * Destination cache support functions
2688 */
2689
David Ahern8d1c8022018-04-17 17:33:26 -07002690static bool fib6_check(struct fib6_info *f6i, u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002691{
Steffen Klassert36143642017-08-25 09:05:42 +02002692 u32 rt_cookie = 0;
Wei Wangc5cff852017-08-21 09:47:10 -07002693
David Ahern8ae86972018-04-20 15:38:03 -07002694 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
David Ahern93531c62018-04-17 17:33:25 -07002695 return false;
2696
2697 if (fib6_check_expired(f6i))
2698 return false;
2699
2700 return true;
2701}
2702
David Aherna68886a2018-04-20 15:38:02 -07002703static struct dst_entry *rt6_check(struct rt6_info *rt,
2704 struct fib6_info *from,
2705 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002706{
Wei Wangc5cff852017-08-21 09:47:10 -07002707 u32 rt_cookie = 0;
2708
David Ahern49d05fe2019-07-17 15:08:43 -07002709 if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
David Ahern93531c62018-04-17 17:33:25 -07002710 rt_cookie != cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002711 return NULL;
2712
2713 if (rt6_check_expired(rt))
2714 return NULL;
2715
2716 return &rt->dst;
2717}
2718
David Aherna68886a2018-04-20 15:38:02 -07002719static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2720 struct fib6_info *from,
2721 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002722{
Martin KaFai Lau5973fb12015-11-11 11:51:07 -08002723 if (!__rt6_check_expired(rt) &&
2724 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
David Aherna68886a2018-04-20 15:38:02 -07002725 fib6_check(from, cookie))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002726 return &rt->dst;
2727 else
2728 return NULL;
2729}
2730
Brian Vazquezbbd807d2021-02-01 17:41:32 +00002731INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst,
2732 u32 cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
David Aherna87b7dc2018-04-20 15:38:00 -07002734 struct dst_entry *dst_ret;
David Aherna68886a2018-04-20 15:38:02 -07002735 struct fib6_info *from;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 struct rt6_info *rt;
2737
David Aherna87b7dc2018-04-20 15:38:00 -07002738 rt = container_of(dst, struct rt6_info, dst);
2739
David Ahern8f34e532020-05-01 08:53:08 -06002740 if (rt->sernum)
2741 return rt6_is_valid(rt) ? dst : NULL;
2742
David Aherna87b7dc2018-04-20 15:38:00 -07002743 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00002745 /* All IPV6 dsts are created with ->obsolete set to the value
2746 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2747 * into this function always.
2748 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02002749
David Aherna68886a2018-04-20 15:38:02 -07002750 from = rcu_dereference(rt->from);
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002751
David Aherna68886a2018-04-20 15:38:02 -07002752 if (from && (rt->rt6i_flags & RTF_PCPU ||
2753 unlikely(!list_empty(&rt->rt6i_uncached))))
2754 dst_ret = rt6_dst_from_check(rt, from, cookie);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002755 else
David Aherna68886a2018-04-20 15:38:02 -07002756 dst_ret = rt6_check(rt, from, cookie);
David Aherna87b7dc2018-04-20 15:38:00 -07002757
2758 rcu_read_unlock();
2759
2760 return dst_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
Brian Vazquez9c979212021-02-04 18:18:39 +00002762EXPORT_INDIRECT_CALLABLE(ip6_dst_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
2764static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2765{
2766 struct rt6_info *rt = (struct rt6_info *) dst;
2767
2768 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002769 if (rt->rt6i_flags & RTF_CACHE) {
David Ahernc3c14da2018-04-23 11:32:06 -07002770 rcu_read_lock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002771 if (rt6_check_expired(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07002772 rt6_remove_exception_rt(rt);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002773 dst = NULL;
2774 }
David Ahernc3c14da2018-04-23 11:32:06 -07002775 rcu_read_unlock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002776 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002778 dst = NULL;
2779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002781 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782}
2783
2784static void ip6_link_failure(struct sk_buff *skb)
2785{
2786 struct rt6_info *rt;
2787
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002788 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Eric Dumazetadf30902009-06-02 05:19:30 +00002790 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (rt) {
David Ahern8a14e462018-04-23 11:32:07 -07002792 rcu_read_lock();
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002793 if (rt->rt6i_flags & RTF_CACHE) {
Xin Long761f6022018-11-14 00:48:28 +08002794 rt6_remove_exception_rt(rt);
Wei Wangc5cff852017-08-21 09:47:10 -07002795 } else {
David Aherna68886a2018-04-20 15:38:02 -07002796 struct fib6_info *from;
Wei Wangc5cff852017-08-21 09:47:10 -07002797 struct fib6_node *fn;
2798
David Aherna68886a2018-04-20 15:38:02 -07002799 from = rcu_dereference(rt->from);
2800 if (from) {
2801 fn = rcu_dereference(from->fib6_node);
2802 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2803 fn->fn_sernum = -1;
2804 }
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002805 }
David Ahern8a14e462018-04-23 11:32:07 -07002806 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 }
2808}
2809
David Ahern6a3e0302018-04-20 15:37:57 -07002810static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2811{
David Aherna68886a2018-04-20 15:38:02 -07002812 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2813 struct fib6_info *from;
2814
2815 rcu_read_lock();
2816 from = rcu_dereference(rt0->from);
2817 if (from)
2818 rt0->dst.expires = from->expires;
2819 rcu_read_unlock();
2820 }
David Ahern6a3e0302018-04-20 15:37:57 -07002821
2822 dst_set_expires(&rt0->dst, timeout);
2823 rt0->rt6i_flags |= RTF_EXPIRES;
2824}
2825
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002826static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2827{
2828 struct net *net = dev_net(rt->dst.dev);
2829
David Ahernd4ead6b2018-04-17 17:33:16 -07002830 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002831 rt->rt6i_flags |= RTF_MODIFIED;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002832 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2833}
2834
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002835static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2836{
2837 return !(rt->rt6i_flags & RTF_CACHE) &&
Paolo Abeni1490ed22019-02-15 18:15:37 +01002838 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002839}
2840
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002841static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +08002842 const struct ipv6hdr *iph, u32 mtu,
2843 bool confirm_neigh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
Julian Anastasov0dec8792017-02-06 23:14:16 +02002845 const struct in6_addr *daddr, *saddr;
Ian Morris67ba4152014-08-24 21:53:10 +01002846 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Maciej Żenczykowski09454fd2020-05-05 11:57:23 -07002848 /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU)
2849 * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it.
2850 * [see also comment in rt6_mtu_change_route()]
2851 */
Xin Long19bda362016-10-28 18:18:01 +08002852
Julian Anastasov0dec8792017-02-06 23:14:16 +02002853 if (iph) {
2854 daddr = &iph->daddr;
2855 saddr = &iph->saddr;
2856 } else if (sk) {
2857 daddr = &sk->sk_v6_daddr;
2858 saddr = &inet6_sk(sk)->saddr;
2859 } else {
2860 daddr = NULL;
2861 saddr = NULL;
2862 }
Hangbin Liubd085ef2019-12-22 10:51:09 +08002863
2864 if (confirm_neigh)
2865 dst_confirm_neigh(dst, daddr);
2866
Georg Kohmann4a65dff2020-10-07 14:53:02 +02002867 if (mtu < IPV6_MIN_MTU)
2868 return;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002869 if (mtu >= dst_mtu(dst))
2870 return;
David S. Miller81aded22012-06-15 14:54:11 -07002871
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002872 if (!rt6_cache_allowed_for_pmtu(rt6)) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002873 rt6_do_update_pmtu(rt6, mtu);
Wei Wang2b760fc2017-10-06 12:06:03 -07002874 /* update rt6_ex->stamp for cache */
2875 if (rt6->rt6i_flags & RTF_CACHE)
2876 rt6_update_exception_stamp_rt(rt6);
Julian Anastasov0dec8792017-02-06 23:14:16 +02002877 } else if (daddr) {
David Ahern85bd05d2019-04-16 14:36:01 -07002878 struct fib6_result res = {};
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002879 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01002880
David Ahern4d85cd02018-04-20 15:37:59 -07002881 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07002882 res.f6i = rcu_dereference(rt6->from);
David Ahern43a4b602019-08-01 15:18:08 -07002883 if (!res.f6i)
2884 goto out_unlock;
2885
David Ahern7d21fec2019-04-16 14:36:11 -07002886 res.fib6_flags = res.f6i->fib6_flags;
2887 res.fib6_type = res.f6i->fib6_type;
2888
David Ahern2d442342019-06-08 14:53:31 -07002889 if (res.f6i->nh) {
2890 struct fib6_nh_match_arg arg = {
2891 .dev = dst->dev,
2892 .gw = &rt6->rt6i_gateway,
2893 };
2894
2895 nexthop_for_each_fib6_nh(res.f6i->nh,
2896 fib6_nh_find_match, &arg);
2897
2898 /* fib6_info uses a nexthop that does not have fib6_nh
2899 * using the dst->dev + gw. Should be impossible.
2900 */
David Ahern43a4b602019-08-01 15:18:08 -07002901 if (!arg.match)
2902 goto out_unlock;
David Ahern2d442342019-06-08 14:53:31 -07002903
2904 res.nh = arg.match;
2905 } else {
2906 res.nh = res.f6i->fib6_nh;
2907 }
2908
David Ahern85bd05d2019-04-16 14:36:01 -07002909 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002910 if (nrt6) {
2911 rt6_do_update_pmtu(nrt6, mtu);
David Ahern5012f0a2019-04-16 14:36:05 -07002912 if (rt6_insert_exception(nrt6, &res))
Wei Wang2b760fc2017-10-06 12:06:03 -07002913 dst_release_immediate(&nrt6->dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002914 }
David Ahern43a4b602019-08-01 15:18:08 -07002915out_unlock:
David Aherna68886a2018-04-20 15:38:02 -07002916 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 }
2918}
2919
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002920static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +08002921 struct sk_buff *skb, u32 mtu,
2922 bool confirm_neigh)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002923{
Hangbin Liubd085ef2019-12-22 10:51:09 +08002924 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2925 confirm_neigh);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002926}
2927
David S. Miller42ae66c2012-06-15 20:01:57 -07002928void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002929 int oif, u32 mark, kuid_t uid)
David S. Miller81aded22012-06-15 14:54:11 -07002930{
2931 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2932 struct dst_entry *dst;
Maciej Żenczykowskidc920952018-09-29 23:44:51 -07002933 struct flowi6 fl6 = {
2934 .flowi6_oif = oif,
2935 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2936 .daddr = iph->daddr,
2937 .saddr = iph->saddr,
2938 .flowlabel = ip6_flowinfo(iph),
2939 .flowi6_uid = uid,
2940 };
David S. Miller81aded22012-06-15 14:54:11 -07002941
2942 dst = ip6_route_output(net, NULL, &fl6);
2943 if (!dst->error)
Hangbin Liubd085ef2019-12-22 10:51:09 +08002944 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
David S. Miller81aded22012-06-15 14:54:11 -07002945 dst_release(dst);
2946}
2947EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2948
2949void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2950{
David Ahern7ddacfa2018-11-18 10:45:30 -08002951 int oif = sk->sk_bound_dev_if;
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002952 struct dst_entry *dst;
2953
David Ahern7ddacfa2018-11-18 10:45:30 -08002954 if (!oif && skb->dev)
2955 oif = l3mdev_master_ifindex(skb->dev);
2956
2957 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002958
2959 dst = __sk_dst_get(sk);
2960 if (!dst || !dst->obsolete ||
2961 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2962 return;
2963
2964 bh_lock_sock(sk);
2965 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2966 ip6_datagram_dst_update(sk, false);
2967 bh_unlock_sock(sk);
David S. Miller81aded22012-06-15 14:54:11 -07002968}
2969EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2970
Alexey Kodanev7d6850f2018-04-03 15:00:07 +03002971void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2972 const struct flowi6 *fl6)
2973{
2974#ifdef CONFIG_IPV6_SUBTREES
2975 struct ipv6_pinfo *np = inet6_sk(sk);
2976#endif
2977
2978 ip6_dst_store(sk, dst,
2979 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2980 &sk->sk_v6_daddr : NULL,
2981#ifdef CONFIG_IPV6_SUBTREES
2982 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2983 &np->saddr :
2984#endif
2985 NULL);
2986}
2987
David Ahern9b6b35a2019-04-16 14:36:02 -07002988static bool ip6_redirect_nh_match(const struct fib6_result *res,
David Ahern0b34eb02019-04-09 14:41:19 -07002989 struct flowi6 *fl6,
2990 const struct in6_addr *gw,
2991 struct rt6_info **ret)
2992{
David Ahern9b6b35a2019-04-16 14:36:02 -07002993 const struct fib6_nh *nh = res->nh;
2994
David Ahern0b34eb02019-04-09 14:41:19 -07002995 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
2996 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
2997 return false;
2998
2999 /* rt_cache's gateway might be different from its 'parent'
3000 * in the case of an ip redirect.
3001 * So we keep searching in the exception table if the gateway
3002 * is different.
3003 */
3004 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
3005 struct rt6_info *rt_cache;
3006
David Ahern9b6b35a2019-04-16 14:36:02 -07003007 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
David Ahern0b34eb02019-04-09 14:41:19 -07003008 if (rt_cache &&
3009 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
3010 *ret = rt_cache;
3011 return true;
3012 }
3013 return false;
3014 }
3015 return true;
3016}
3017
David Ahernc55c8982019-06-08 14:53:29 -07003018struct fib6_nh_rd_arg {
3019 struct fib6_result *res;
3020 struct flowi6 *fl6;
3021 const struct in6_addr *gw;
3022 struct rt6_info **ret;
3023};
3024
3025static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
3026{
3027 struct fib6_nh_rd_arg *arg = _arg;
3028
3029 arg->res->nh = nh;
3030 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
3031}
3032
Duan Jiongb55b76b2013-09-04 19:44:21 +08003033/* Handle redirects */
3034struct ip6rd_flowi {
3035 struct flowi6 fl6;
3036 struct in6_addr gateway;
3037};
3038
Brian Vazquez55cced42020-06-23 09:42:32 -07003039INDIRECT_CALLABLE_SCOPE struct rt6_info *__ip6_route_redirect(struct net *net,
Duan Jiongb55b76b2013-09-04 19:44:21 +08003040 struct fib6_table *table,
3041 struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08003042 const struct sk_buff *skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08003043 int flags)
3044{
3045 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
David Ahern0b34eb02019-04-09 14:41:19 -07003046 struct rt6_info *ret = NULL;
David Ahern9b6b35a2019-04-16 14:36:02 -07003047 struct fib6_result res = {};
David Ahernc55c8982019-06-08 14:53:29 -07003048 struct fib6_nh_rd_arg arg = {
3049 .res = &res,
3050 .fl6 = fl6,
3051 .gw = &rdfl->gateway,
3052 .ret = &ret
3053 };
David Ahern8d1c8022018-04-17 17:33:26 -07003054 struct fib6_info *rt;
Duan Jiongb55b76b2013-09-04 19:44:21 +08003055 struct fib6_node *fn;
3056
David Ahern31680ac2019-05-22 15:12:18 -07003057 /* l3mdev_update_flow overrides oif if the device is enslaved; in
3058 * this case we must match on the real ingress device, so reset it
3059 */
3060 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
3061 fl6->flowi6_oif = skb->dev->ifindex;
3062
Duan Jiongb55b76b2013-09-04 19:44:21 +08003063 /* Get the "current" route for this destination and
Alexander Alemayhu67c408c2017-01-07 23:53:00 +01003064 * check if the redirect has come from appropriate router.
Duan Jiongb55b76b2013-09-04 19:44:21 +08003065 *
3066 * RFC 4861 specifies that redirects should only be
3067 * accepted if they come from the nexthop to the target.
3068 * Due to the way the routes are chosen, this notion
3069 * is a bit fuzzy and one might need to check all possible
3070 * routes.
3071 */
3072
Wei Wang66f5d6c2017-10-06 12:06:10 -07003073 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07003074 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08003075restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003076 for_each_fib6_node_rt_rcu(fn) {
David Ahern9b6b35a2019-04-16 14:36:02 -07003077 res.f6i = rt;
David Ahern14895682018-04-17 17:33:17 -07003078 if (fib6_check_expired(rt))
Duan Jiongb55b76b2013-09-04 19:44:21 +08003079 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07003080 if (rt->fib6_flags & RTF_REJECT)
Duan Jiongb55b76b2013-09-04 19:44:21 +08003081 break;
David Ahernc55c8982019-06-08 14:53:29 -07003082 if (unlikely(rt->nh)) {
3083 if (nexthop_is_blackhole(rt->nh))
3084 continue;
3085 /* on match, res->nh is filled in and potentially ret */
3086 if (nexthop_for_each_fib6_nh(rt->nh,
3087 fib6_nh_redirect_match,
3088 &arg))
3089 goto out;
3090 } else {
3091 res.nh = rt->fib6_nh;
3092 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
3093 &ret))
3094 goto out;
3095 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08003096 }
3097
3098 if (!rt)
David Ahern421842e2018-04-17 17:33:18 -07003099 rt = net->ipv6.fib6_null_entry;
David Ahern93c2fb22018-04-18 15:38:59 -07003100 else if (rt->fib6_flags & RTF_REJECT) {
David Ahern23fb93a2018-04-17 17:33:23 -07003101 ret = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08003102 goto out;
3103 }
3104
David Ahern421842e2018-04-17 17:33:18 -07003105 if (rt == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07003106 fn = fib6_backtrack(fn, &fl6->saddr);
3107 if (fn)
3108 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08003109 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07003110
David Ahern9b6b35a2019-04-16 14:36:02 -07003111 res.f6i = rt;
David Ahern1cf844c2019-05-22 20:27:59 -07003112 res.nh = rt->fib6_nh;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08003113out:
David Ahern7d21fec2019-04-16 14:36:11 -07003114 if (ret) {
David Ahern10585b42019-03-20 09:24:50 -07003115 ip6_hold_safe(net, &ret);
David Ahern7d21fec2019-04-16 14:36:11 -07003116 } else {
3117 res.fib6_flags = res.f6i->fib6_flags;
3118 res.fib6_type = res.f6i->fib6_type;
David Ahern9b6b35a2019-04-16 14:36:02 -07003119 ret = ip6_create_rt_rcu(&res);
David Ahern7d21fec2019-04-16 14:36:11 -07003120 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08003121
Wei Wang66f5d6c2017-10-06 12:06:10 -07003122 rcu_read_unlock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08003123
David Ahern8ff2e5b2019-04-16 14:36:09 -07003124 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahern23fb93a2018-04-17 17:33:23 -07003125 return ret;
Duan Jiongb55b76b2013-09-04 19:44:21 +08003126};
3127
3128static struct dst_entry *ip6_route_redirect(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08003129 const struct flowi6 *fl6,
3130 const struct sk_buff *skb,
3131 const struct in6_addr *gateway)
Duan Jiongb55b76b2013-09-04 19:44:21 +08003132{
3133 int flags = RT6_LOOKUP_F_HAS_SADDR;
3134 struct ip6rd_flowi rdfl;
3135
3136 rdfl.fl6 = *fl6;
3137 rdfl.gateway = *gateway;
3138
David Ahernb75cc8f2018-03-02 08:32:17 -08003139 return fib6_rule_lookup(net, &rdfl.fl6, skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08003140 flags, __ip6_route_redirect);
3141}
3142
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09003143void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
3144 kuid_t uid)
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003145{
3146 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
3147 struct dst_entry *dst;
Maciej Żenczykowski1f7f10a2018-09-29 23:44:48 -07003148 struct flowi6 fl6 = {
3149 .flowi6_iif = LOOPBACK_IFINDEX,
3150 .flowi6_oif = oif,
3151 .flowi6_mark = mark,
3152 .daddr = iph->daddr,
3153 .saddr = iph->saddr,
3154 .flowlabel = ip6_flowinfo(iph),
3155 .flowi6_uid = uid,
3156 };
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003157
David Ahernb75cc8f2018-03-02 08:32:17 -08003158 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08003159 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003160 dst_release(dst);
3161}
3162EXPORT_SYMBOL_GPL(ip6_redirect);
3163
Maciej Żenczykowskid4563362018-09-29 23:44:50 -07003164void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
Duan Jiongc92a59e2013-08-22 12:07:35 +08003165{
3166 const struct ipv6hdr *iph = ipv6_hdr(skb);
3167 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3168 struct dst_entry *dst;
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07003169 struct flowi6 fl6 = {
3170 .flowi6_iif = LOOPBACK_IFINDEX,
3171 .flowi6_oif = oif,
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07003172 .daddr = msg->dest,
3173 .saddr = iph->daddr,
3174 .flowi6_uid = sock_net_uid(net, NULL),
3175 };
Duan Jiongc92a59e2013-08-22 12:07:35 +08003176
David Ahernb75cc8f2018-03-02 08:32:17 -08003177 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08003178 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08003179 dst_release(dst);
3180}
3181
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003182void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
3183{
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09003184 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
3185 sk->sk_uid);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003186}
3187EXPORT_SYMBOL_GPL(ip6_sk_redirect);
3188
David S. Miller0dbaee32010-12-13 12:52:14 -08003189static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
David S. Miller0dbaee32010-12-13 12:52:14 -08003191 struct net_device *dev = dst->dev;
3192 unsigned int mtu = dst_mtu(dst);
3193 struct net *net = dev_net(dev);
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
3196
Daniel Lezcano55786892008-03-04 13:47:47 -08003197 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
3198 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
3200 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003201 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3202 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3203 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 * rely only on pmtu discovery"
3205 */
3206 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
3207 mtu = IPV6_MAXPLEN;
3208 return mtu;
3209}
3210
Brian Vazquezf67fbea2021-02-01 17:41:31 +00003211INDIRECT_CALLABLE_SCOPE unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08003212{
Vadim Fedorenko427faee12021-07-20 23:06:27 +03003213 return ip6_dst_mtu_maybe_forward(dst, false);
David S. Millerd33e4552010-12-14 13:01:14 -08003214}
Brian Vazquez9c979212021-02-04 18:18:39 +00003215EXPORT_INDIRECT_CALLABLE(ip6_mtu);
David S. Millerd33e4552010-12-14 13:01:14 -08003216
David Ahern901731b2018-05-21 09:08:14 -07003217/* MTU selection:
3218 * 1. mtu on route is locked - use it
3219 * 2. mtu from nexthop exception
3220 * 3. mtu from egress device
3221 *
3222 * based on ip6_dst_mtu_forward and exception logic of
3223 * rt6_find_cached_rt; called with rcu_read_lock
3224 */
David Ahernb748f262019-04-16 14:36:06 -07003225u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3226 const struct in6_addr *daddr,
3227 const struct in6_addr *saddr)
David Ahern901731b2018-05-21 09:08:14 -07003228{
David Ahernb748f262019-04-16 14:36:06 -07003229 const struct fib6_nh *nh = res->nh;
3230 struct fib6_info *f6i = res->f6i;
David Ahern901731b2018-05-21 09:08:14 -07003231 struct inet6_dev *idev;
Wei Wang510e2ce2019-05-16 13:30:54 -07003232 struct rt6_info *rt;
David Ahern901731b2018-05-21 09:08:14 -07003233 u32 mtu = 0;
3234
3235 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3236 mtu = f6i->fib6_pmtu;
3237 if (mtu)
3238 goto out;
3239 }
3240
Wei Wang510e2ce2019-05-16 13:30:54 -07003241 rt = rt6_find_cached_rt(res, daddr, saddr);
3242 if (unlikely(rt)) {
3243 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3244 } else {
David Ahernb748f262019-04-16 14:36:06 -07003245 struct net_device *dev = nh->fib_nh_dev;
David Ahern901731b2018-05-21 09:08:14 -07003246
3247 mtu = IPV6_MIN_MTU;
3248 idev = __in6_dev_get(dev);
3249 if (idev && idev->cnf.mtu6 > mtu)
3250 mtu = idev->cnf.mtu6;
3251 }
3252
3253 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3254out:
David Ahernb748f262019-04-16 14:36:06 -07003255 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahern901731b2018-05-21 09:08:14 -07003256}
3257
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08003258struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05003259 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260{
David S. Miller87a11572011-12-06 17:04:13 -05003261 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 struct rt6_info *rt;
3263 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003264 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
David S. Miller38308472011-12-03 18:02:47 -05003266 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00003267 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Martin KaFai Lauad706862015-08-14 11:05:52 -07003269 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05003270 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05003272 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 goto out;
3274 }
3275
Brendan McGrath588753f2017-12-13 22:14:57 +11003276 rt->dst.input = ip6_input;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003277 rt->dst.output = ip6_output;
Julian Anastasov550bab42013-10-20 15:43:04 +03003278 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05003279 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003280 rt->rt6i_dst.plen = 128;
3281 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08003282 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Ido Schimmel4c981e22018-01-07 12:45:04 +02003284 /* Add this dst into uncached_list so that rt6_disable_ip() can
Wei Wang587fea72017-06-17 10:42:36 -07003285 * do proper release of the net_device
3286 */
3287 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07003288 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
David S. Miller87a11572011-12-06 17:04:13 -05003290 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3291
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292out:
David S. Miller87a11572011-12-06 17:04:13 -05003293 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
Daniel Lezcano569d3642008-01-18 03:56:57 -08003296static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003298 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08003299 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3300 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
3301 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3302 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3303 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003304 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Eric Dumazetfc66f952010-10-08 06:37:34 +00003306 entries = dst_entries_get_fast(ops);
Eric Dumazetcf86a082020-05-07 18:58:10 -07003307 if (entries > rt_max_size)
3308 entries = dst_entries_get_slow(ops);
3309
Michal Kubeček49a18d82013-08-01 10:04:24 +02003310 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00003311 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 goto out;
3313
Benjamin Thery6891a342008-03-04 13:49:47 -08003314 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08003315 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003316 entries = dst_entries_get_slow(ops);
3317 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08003318 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08003320 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003321 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322}
3323
David Ahernb2c709c2019-06-24 13:44:51 -07003324static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3325 const struct in6_addr *gw_addr, u32 tbid,
3326 int flags, struct fib6_result *res)
David Ahern8c145862016-04-24 21:26:04 -07003327{
3328 struct flowi6 fl6 = {
3329 .flowi6_oif = cfg->fc_ifindex,
3330 .daddr = *gw_addr,
3331 .saddr = cfg->fc_prefsrc,
3332 };
3333 struct fib6_table *table;
David Ahernb2c709c2019-06-24 13:44:51 -07003334 int err;
David Ahern8c145862016-04-24 21:26:04 -07003335
David Ahernf4797b32018-01-25 16:55:08 -08003336 table = fib6_get_table(net, tbid);
David Ahern8c145862016-04-24 21:26:04 -07003337 if (!table)
David Ahernb2c709c2019-06-24 13:44:51 -07003338 return -EINVAL;
David Ahern8c145862016-04-24 21:26:04 -07003339
3340 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3341 flags |= RT6_LOOKUP_F_HAS_SADDR;
3342
David Ahernf4797b32018-01-25 16:55:08 -08003343 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
David Ahern8c145862016-04-24 21:26:04 -07003344
David Ahernb2c709c2019-06-24 13:44:51 -07003345 err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3346 if (!err && res->f6i != net->ipv6.fib6_null_entry)
3347 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3348 cfg->fc_ifindex != 0, NULL, flags);
David Ahern8c145862016-04-24 21:26:04 -07003349
David Ahernb2c709c2019-06-24 13:44:51 -07003350 return err;
David Ahern8c145862016-04-24 21:26:04 -07003351}
3352
David Ahernfc1e64e2018-01-25 16:55:09 -08003353static int ip6_route_check_nh_onlink(struct net *net,
3354 struct fib6_config *cfg,
David Ahern9fbb7042018-03-13 08:29:36 -07003355 const struct net_device *dev,
David Ahernfc1e64e2018-01-25 16:55:09 -08003356 struct netlink_ext_ack *extack)
3357{
David Ahernb2c709c2019-06-24 13:44:51 -07003358 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
David Ahernfc1e64e2018-01-25 16:55:09 -08003359 const struct in6_addr *gw_addr = &cfg->fc_gateway;
David Ahernb2c709c2019-06-24 13:44:51 -07003360 struct fib6_result res = {};
David Ahernfc1e64e2018-01-25 16:55:09 -08003361 int err;
3362
David Ahernb2c709c2019-06-24 13:44:51 -07003363 err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3364 if (!err && !(res.fib6_flags & RTF_REJECT) &&
3365 /* ignore match if it is the default route */
3366 !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3367 (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
3368 NL_SET_ERR_MSG(extack,
3369 "Nexthop has invalid gateway or device mismatch");
3370 err = -EINVAL;
David Ahernfc1e64e2018-01-25 16:55:09 -08003371 }
3372
3373 return err;
3374}
3375
David Ahern1edce992018-01-25 16:55:07 -08003376static int ip6_route_check_nh(struct net *net,
3377 struct fib6_config *cfg,
3378 struct net_device **_dev,
3379 struct inet6_dev **idev)
3380{
3381 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3382 struct net_device *dev = _dev ? *_dev : NULL;
David Ahernb2c709c2019-06-24 13:44:51 -07003383 int flags = RT6_LOOKUP_F_IFACE;
3384 struct fib6_result res = {};
David Ahern1edce992018-01-25 16:55:07 -08003385 int err = -EHOSTUNREACH;
3386
3387 if (cfg->fc_table) {
David Ahernb2c709c2019-06-24 13:44:51 -07003388 err = ip6_nh_lookup_table(net, cfg, gw_addr,
3389 cfg->fc_table, flags, &res);
3390 /* gw_addr can not require a gateway or resolve to a reject
3391 * route. If a device is given, it must match the result.
3392 */
3393 if (err || res.fib6_flags & RTF_REJECT ||
3394 res.nh->fib_nh_gw_family ||
3395 (dev && dev != res.nh->fib_nh_dev))
3396 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003397 }
3398
David Ahernb2c709c2019-06-24 13:44:51 -07003399 if (err < 0) {
3400 struct flowi6 fl6 = {
3401 .flowi6_oif = cfg->fc_ifindex,
3402 .daddr = *gw_addr,
3403 };
David Ahern1edce992018-01-25 16:55:07 -08003404
David Ahernb2c709c2019-06-24 13:44:51 -07003405 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3406 if (err || res.fib6_flags & RTF_REJECT ||
3407 res.nh->fib_nh_gw_family)
3408 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003409
David Ahernb2c709c2019-06-24 13:44:51 -07003410 if (err)
3411 return err;
3412
3413 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3414 cfg->fc_ifindex != 0, NULL, flags);
3415 }
3416
3417 err = 0;
David Ahern1edce992018-01-25 16:55:07 -08003418 if (dev) {
David Ahernb2c709c2019-06-24 13:44:51 -07003419 if (dev != res.nh->fib_nh_dev)
3420 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003421 } else {
David Ahernb2c709c2019-06-24 13:44:51 -07003422 *_dev = dev = res.nh->fib_nh_dev;
David Ahern1edce992018-01-25 16:55:07 -08003423 dev_hold(dev);
David Ahernb2c709c2019-06-24 13:44:51 -07003424 *idev = in6_dev_get(dev);
David Ahern1edce992018-01-25 16:55:07 -08003425 }
3426
David Ahern1edce992018-01-25 16:55:07 -08003427 return err;
3428}
3429
David Ahern9fbb7042018-03-13 08:29:36 -07003430static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3431 struct net_device **_dev, struct inet6_dev **idev,
3432 struct netlink_ext_ack *extack)
3433{
3434 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3435 int gwa_type = ipv6_addr_type(gw_addr);
David Ahern232378e2018-03-13 08:29:37 -07003436 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
David Ahern9fbb7042018-03-13 08:29:36 -07003437 const struct net_device *dev = *_dev;
David Ahern232378e2018-03-13 08:29:37 -07003438 bool need_addr_check = !dev;
David Ahern9fbb7042018-03-13 08:29:36 -07003439 int err = -EINVAL;
3440
3441 /* if gw_addr is local we will fail to detect this in case
3442 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3443 * will return already-added prefix route via interface that
3444 * prefix route was assigned to, which might be non-loopback.
3445 */
David Ahern232378e2018-03-13 08:29:37 -07003446 if (dev &&
3447 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3448 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
David Ahern9fbb7042018-03-13 08:29:36 -07003449 goto out;
3450 }
3451
3452 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3453 /* IPv6 strictly inhibits using not link-local
3454 * addresses as nexthop address.
3455 * Otherwise, router will not able to send redirects.
3456 * It is very good, but in some (rare!) circumstances
3457 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3458 * some exceptions. --ANK
3459 * We allow IPv4-mapped nexthops to support RFC4798-type
3460 * addressing
3461 */
3462 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3463 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3464 goto out;
3465 }
3466
David Ahernb2c709c2019-06-24 13:44:51 -07003467 rcu_read_lock();
3468
David Ahern9fbb7042018-03-13 08:29:36 -07003469 if (cfg->fc_flags & RTNH_F_ONLINK)
3470 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3471 else
3472 err = ip6_route_check_nh(net, cfg, _dev, idev);
3473
David Ahernb2c709c2019-06-24 13:44:51 -07003474 rcu_read_unlock();
3475
David Ahern9fbb7042018-03-13 08:29:36 -07003476 if (err)
3477 goto out;
3478 }
3479
3480 /* reload in case device was changed */
3481 dev = *_dev;
3482
3483 err = -EINVAL;
3484 if (!dev) {
3485 NL_SET_ERR_MSG(extack, "Egress device not specified");
3486 goto out;
3487 } else if (dev->flags & IFF_LOOPBACK) {
3488 NL_SET_ERR_MSG(extack,
3489 "Egress device can not be loopback device for this route");
3490 goto out;
3491 }
David Ahern232378e2018-03-13 08:29:37 -07003492
3493 /* if we did not check gw_addr above, do so now that the
3494 * egress device has been resolved.
3495 */
3496 if (need_addr_check &&
3497 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3498 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3499 goto out;
3500 }
3501
David Ahern9fbb7042018-03-13 08:29:36 -07003502 err = 0;
3503out:
3504 return err;
3505}
3506
David Ahern83c442512019-03-27 20:53:50 -07003507static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3508{
3509 if ((flags & RTF_REJECT) ||
3510 (dev && (dev->flags & IFF_LOOPBACK) &&
3511 !(addr_type & IPV6_ADDR_LOOPBACK) &&
David Ahernaea23c322020-07-07 07:39:24 -06003512 !(flags & (RTF_ANYCAST | RTF_LOCAL))))
David Ahern83c442512019-03-27 20:53:50 -07003513 return true;
3514
3515 return false;
3516}
3517
3518int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3519 struct fib6_config *cfg, gfp_t gfp_flags,
3520 struct netlink_ext_ack *extack)
3521{
3522 struct net_device *dev = NULL;
3523 struct inet6_dev *idev = NULL;
3524 int addr_type;
3525 int err;
3526
David Ahernf1741732019-03-27 20:53:57 -07003527 fib6_nh->fib_nh_family = AF_INET6;
Eric Dumazet1bef4c22019-11-07 09:26:19 -08003528#ifdef CONFIG_IPV6_ROUTER_PREF
3529 fib6_nh->last_probe = jiffies;
3530#endif
Roopa Prabhu38428d62020-05-21 22:26:13 -07003531 if (cfg->fc_is_fdb) {
3532 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3533 fib6_nh->fib_nh_gw_family = AF_INET6;
3534 return 0;
3535 }
David Ahernf1741732019-03-27 20:53:57 -07003536
David Ahern83c442512019-03-27 20:53:50 -07003537 err = -ENODEV;
3538 if (cfg->fc_ifindex) {
3539 dev = dev_get_by_index(net, cfg->fc_ifindex);
3540 if (!dev)
3541 goto out;
3542 idev = in6_dev_get(dev);
3543 if (!idev)
3544 goto out;
3545 }
3546
3547 if (cfg->fc_flags & RTNH_F_ONLINK) {
3548 if (!dev) {
3549 NL_SET_ERR_MSG(extack,
3550 "Nexthop device required for onlink");
3551 goto out;
3552 }
3553
3554 if (!(dev->flags & IFF_UP)) {
3555 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3556 err = -ENETDOWN;
3557 goto out;
3558 }
3559
David Ahernad1601a2019-03-27 20:53:56 -07003560 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
David Ahern83c442512019-03-27 20:53:50 -07003561 }
3562
David Ahernad1601a2019-03-27 20:53:56 -07003563 fib6_nh->fib_nh_weight = 1;
David Ahern83c442512019-03-27 20:53:50 -07003564
3565 /* We cannot add true routes via loopback here,
3566 * they would result in kernel looping; promote them to reject routes
3567 */
3568 addr_type = ipv6_addr_type(&cfg->fc_dst);
3569 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3570 /* hold loopback dev/idev if we haven't done so. */
3571 if (dev != net->loopback_dev) {
3572 if (dev) {
3573 dev_put(dev);
3574 in6_dev_put(idev);
3575 }
3576 dev = net->loopback_dev;
3577 dev_hold(dev);
3578 idev = in6_dev_get(dev);
3579 if (!idev) {
3580 err = -ENODEV;
3581 goto out;
3582 }
3583 }
David Ahern7dd73162019-06-03 18:37:03 -07003584 goto pcpu_alloc;
David Ahern83c442512019-03-27 20:53:50 -07003585 }
3586
3587 if (cfg->fc_flags & RTF_GATEWAY) {
3588 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3589 if (err)
3590 goto out;
3591
David Ahernad1601a2019-03-27 20:53:56 -07003592 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
David Ahernbdf00462019-04-05 16:30:26 -07003593 fib6_nh->fib_nh_gw_family = AF_INET6;
David Ahern83c442512019-03-27 20:53:50 -07003594 }
3595
3596 err = -ENODEV;
3597 if (!dev)
3598 goto out;
3599
3600 if (idev->cnf.disable_ipv6) {
3601 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3602 err = -EACCES;
3603 goto out;
3604 }
3605
3606 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3607 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3608 err = -ENETDOWN;
3609 goto out;
3610 }
3611
3612 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3613 !netif_carrier_ok(dev))
David Ahernad1601a2019-03-27 20:53:56 -07003614 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
David Ahern83c442512019-03-27 20:53:50 -07003615
Alexander Aringfaee6762020-03-27 18:00:21 -04003616 err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
David Ahern7dd73162019-06-03 18:37:03 -07003617 cfg->fc_encap_type, cfg, gfp_flags, extack);
3618 if (err)
3619 goto out;
3620
3621pcpu_alloc:
David Ahernf40b6ae2019-05-22 20:27:55 -07003622 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3623 if (!fib6_nh->rt6i_pcpu) {
3624 err = -ENOMEM;
3625 goto out;
3626 }
3627
David Ahernad1601a2019-03-27 20:53:56 -07003628 fib6_nh->fib_nh_dev = dev;
David Ahernf1741732019-03-27 20:53:57 -07003629 fib6_nh->fib_nh_oif = dev->ifindex;
David Ahern83c442512019-03-27 20:53:50 -07003630 err = 0;
3631out:
3632 if (idev)
3633 in6_dev_put(idev);
3634
3635 if (err) {
David Ahernad1601a2019-03-27 20:53:56 -07003636 lwtstate_put(fib6_nh->fib_nh_lws);
3637 fib6_nh->fib_nh_lws = NULL;
Yajun Deng1160dfa2021-08-05 19:55:27 +08003638 dev_put(dev);
David Ahern83c442512019-03-27 20:53:50 -07003639 }
3640
3641 return err;
3642}
3643
David Aherndac7d0f2019-03-27 20:53:51 -07003644void fib6_nh_release(struct fib6_nh *fib6_nh)
3645{
David Aherncc5c0732019-05-22 20:27:58 -07003646 struct rt6_exception_bucket *bucket;
3647
3648 rcu_read_lock();
3649
3650 fib6_nh_flush_exceptions(fib6_nh, NULL);
3651 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3652 if (bucket) {
3653 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3654 kfree(bucket);
3655 }
3656
3657 rcu_read_unlock();
3658
Nikolay Aleksandrov61308052021-11-29 17:44:11 +02003659 fib6_nh_release_dsts(fib6_nh);
3660 free_percpu(fib6_nh->rt6i_pcpu);
David Ahernf40b6ae2019-05-22 20:27:55 -07003661
David Ahern979e2762019-03-27 20:53:58 -07003662 fib_nh_common_release(&fib6_nh->nh_common);
David Aherndac7d0f2019-03-27 20:53:51 -07003663}
3664
Nikolay Aleksandrov8837cbb2021-11-22 17:15:12 +02003665void fib6_nh_release_dsts(struct fib6_nh *fib6_nh)
3666{
3667 int cpu;
3668
3669 if (!fib6_nh->rt6i_pcpu)
3670 return;
3671
3672 for_each_possible_cpu(cpu) {
3673 struct rt6_info *pcpu_rt, **ppcpu_rt;
3674
3675 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3676 pcpu_rt = xchg(ppcpu_rt, NULL);
3677 if (pcpu_rt) {
3678 dst_dev_put(&pcpu_rt->dst);
3679 dst_release(&pcpu_rt->dst);
3680 }
3681 }
3682}
3683
David Ahern8d1c8022018-04-17 17:33:26 -07003684static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
David Ahernacb54e32018-04-17 17:33:22 -07003685 gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003686 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687{
Daniel Lezcano55786892008-03-04 13:47:47 -08003688 struct net *net = cfg->fc_nlinfo.nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07003689 struct fib6_info *rt = NULL;
David Ahernf88d8ea2019-06-03 20:19:52 -07003690 struct nexthop *nh = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003691 struct fib6_table *table;
David Ahernf88d8ea2019-06-03 20:19:52 -07003692 struct fib6_nh *fib6_nh;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003693 int err = -EINVAL;
David Ahern83c442512019-03-27 20:53:50 -07003694 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
David Ahern557c44b2017-04-19 14:19:43 -07003696 /* RTF_PCPU is an internal flag; can not be set by userspace */
David Ahernd5d531c2017-05-21 10:12:05 -06003697 if (cfg->fc_flags & RTF_PCPU) {
3698 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
David Ahern557c44b2017-04-19 14:19:43 -07003699 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003700 }
David Ahern557c44b2017-04-19 14:19:43 -07003701
Wei Wang2ea23522017-10-27 17:30:12 -07003702 /* RTF_CACHE is an internal flag; can not be set by userspace */
3703 if (cfg->fc_flags & RTF_CACHE) {
3704 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3705 goto out;
3706 }
3707
David Aherne8478e82018-04-17 17:33:13 -07003708 if (cfg->fc_type > RTN_MAX) {
3709 NL_SET_ERR_MSG(extack, "Invalid route type");
3710 goto out;
3711 }
3712
David Ahernd5d531c2017-05-21 10:12:05 -06003713 if (cfg->fc_dst_len > 128) {
3714 NL_SET_ERR_MSG(extack, "Invalid prefix length");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003715 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003716 }
3717 if (cfg->fc_src_len > 128) {
3718 NL_SET_ERR_MSG(extack, "Invalid source address length");
3719 goto out;
3720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721#ifndef CONFIG_IPV6_SUBTREES
David Ahernd5d531c2017-05-21 10:12:05 -06003722 if (cfg->fc_src_len) {
3723 NL_SET_ERR_MSG(extack,
3724 "Specifying source address requires IPV6_SUBTREES to be enabled");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003725 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727#endif
David Ahern5b983242019-06-08 14:53:34 -07003728 if (cfg->fc_nh_id) {
3729 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3730 if (!nh) {
3731 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
3732 goto out;
3733 }
3734 err = fib6_check_nexthop(nh, cfg, extack);
3735 if (err)
3736 goto out;
3737 }
David Ahernfc1e64e2018-01-25 16:55:09 -08003738
Matti Vaittinend71314b2011-11-14 00:14:49 +00003739 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05003740 if (cfg->fc_nlinfo.nlh &&
3741 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00003742 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05003743 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00003744 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00003745 table = fib6_new_table(net, cfg->fc_table);
3746 }
3747 } else {
3748 table = fib6_new_table(net, cfg->fc_table);
3749 }
David S. Miller38308472011-12-03 18:02:47 -05003750
3751 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003752 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07003753
David Ahern93531c62018-04-17 17:33:25 -07003754 err = -ENOMEM;
David Ahernf88d8ea2019-06-03 20:19:52 -07003755 rt = fib6_info_alloc(gfp_flags, !nh);
David Ahern93531c62018-04-17 17:33:25 -07003756 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 goto out;
David Ahern93531c62018-04-17 17:33:25 -07003758
David Ahernd7e774f2018-11-06 12:51:15 -08003759 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3760 extack);
David Ahern767a2212018-10-04 20:07:51 -07003761 if (IS_ERR(rt->fib6_metrics)) {
3762 err = PTR_ERR(rt->fib6_metrics);
Eric Dumazetfda21d42018-10-05 09:17:50 -07003763 /* Do not leave garbage there. */
3764 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
Paolo Abeni8fb47922021-07-20 15:08:40 +02003765 goto out_free;
David Ahern767a2212018-10-04 20:07:51 -07003766 }
3767
David Ahern93531c62018-04-17 17:33:25 -07003768 if (cfg->fc_flags & RTF_ADDRCONF)
3769 rt->dst_nocount = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Gao feng1716a962012-04-06 00:13:10 +00003771 if (cfg->fc_flags & RTF_EXPIRES)
David Ahern14895682018-04-17 17:33:17 -07003772 fib6_set_expires(rt, jiffies +
Gao feng1716a962012-04-06 00:13:10 +00003773 clock_t_to_jiffies(cfg->fc_expires));
3774 else
David Ahern14895682018-04-17 17:33:17 -07003775 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Thomas Graf86872cb2006-08-22 00:01:08 -07003777 if (cfg->fc_protocol == RTPROT_UNSPEC)
3778 cfg->fc_protocol = RTPROT_BOOT;
David Ahern93c2fb22018-04-18 15:38:59 -07003779 rt->fib6_protocol = cfg->fc_protocol;
Thomas Graf86872cb2006-08-22 00:01:08 -07003780
David Ahern83c442512019-03-27 20:53:50 -07003781 rt->fib6_table = table;
3782 rt->fib6_metric = cfg->fc_metric;
David Ahernc7036d92019-06-19 10:50:24 -07003783 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
David Ahern2b2450c2019-03-27 20:53:52 -07003784 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003785
David Ahern93c2fb22018-04-18 15:38:59 -07003786 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3787 rt->fib6_dst.plen = cfg->fc_dst_len;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01003788
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07003790 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3791 rt->fib6_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792#endif
David Ahernf88d8ea2019-06-03 20:19:52 -07003793 if (nh) {
David Ahernf88d8ea2019-06-03 20:19:52 -07003794 if (rt->fib6_src.plen) {
Colin Ian King4daa95a2019-06-06 09:40:39 +01003795 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
Coco Li821bbf72021-06-03 07:32:58 +00003796 goto out_free;
David Ahernf88d8ea2019-06-03 20:19:52 -07003797 }
Xiyu Yang706ec912020-07-25 16:02:18 +08003798 if (!nexthop_get(nh)) {
3799 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
Coco Li821bbf72021-06-03 07:32:58 +00003800 goto out_free;
Xiyu Yang706ec912020-07-25 16:02:18 +08003801 }
David Ahernf88d8ea2019-06-03 20:19:52 -07003802 rt->nh = nh;
3803 fib6_nh = nexthop_fib6_nh(rt->nh);
3804 } else {
3805 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3806 if (err)
3807 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
David Ahernf88d8ea2019-06-03 20:19:52 -07003809 fib6_nh = rt->fib6_nh;
3810
3811 /* We cannot add true routes via loopback here, they would
3812 * result in kernel looping; promote them to reject routes
3813 */
3814 addr_type = ipv6_addr_type(&cfg->fc_dst);
3815 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3816 addr_type))
3817 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3818 }
David Ahern955ec4c2018-01-24 19:45:29 -08003819
Daniel Walterc3968a82011-04-13 21:10:57 +00003820 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
David Ahernf88d8ea2019-06-03 20:19:52 -07003821 struct net_device *dev = fib6_nh->fib_nh_dev;
David Ahern83c442512019-03-27 20:53:50 -07003822
Daniel Walterc3968a82011-04-13 21:10:57 +00003823 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
David Ahernd5d531c2017-05-21 10:12:05 -06003824 NL_SET_ERR_MSG(extack, "Invalid source address");
Daniel Walterc3968a82011-04-13 21:10:57 +00003825 err = -EINVAL;
3826 goto out;
3827 }
David Ahern93c2fb22018-04-18 15:38:59 -07003828 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3829 rt->fib6_prefsrc.plen = 128;
Daniel Walterc3968a82011-04-13 21:10:57 +00003830 } else
David Ahern93c2fb22018-04-18 15:38:59 -07003831 rt->fib6_prefsrc.plen = 0;
Daniel Walterc3968a82011-04-13 21:10:57 +00003832
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003833 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834out:
David Ahern93531c62018-04-17 17:33:25 -07003835 fib6_info_release(rt);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003836 return ERR_PTR(err);
Coco Li821bbf72021-06-03 07:32:58 +00003837out_free:
3838 ip_fib_metrics_put(rt->fib6_metrics);
3839 kfree(rt);
3840 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003841}
3842
David Ahernacb54e32018-04-17 17:33:22 -07003843int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003844 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003845{
David Ahern8d1c8022018-04-17 17:33:26 -07003846 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003847 int err;
3848
David Ahernacb54e32018-04-17 17:33:22 -07003849 rt = ip6_route_info_create(cfg, gfp_flags, extack);
David Ahernd4ead6b2018-04-17 17:33:16 -07003850 if (IS_ERR(rt))
3851 return PTR_ERR(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003852
David Ahernd4ead6b2018-04-17 17:33:16 -07003853 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
David Ahern93531c62018-04-17 17:33:25 -07003854 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003855
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 return err;
3857}
3858
David Ahern8d1c8022018-04-17 17:33:26 -07003859static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860{
David Ahernafb1d4b52018-04-17 17:33:11 -07003861 struct net *net = info->nl_net;
Thomas Grafc71099a2006-08-04 23:20:06 -07003862 struct fib6_table *table;
David Ahernafb1d4b52018-04-17 17:33:11 -07003863 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
David Ahern421842e2018-04-17 17:33:18 -07003865 if (rt == net->ipv6.fib6_null_entry) {
Gao feng6825a262012-09-19 19:25:34 +00003866 err = -ENOENT;
3867 goto out;
3868 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07003869
David Ahern93c2fb22018-04-18 15:38:59 -07003870 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003871 spin_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07003872 err = fib6_del(rt, info);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003873 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Gao feng6825a262012-09-19 19:25:34 +00003875out:
David Ahern93531c62018-04-17 17:33:25 -07003876 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 return err;
3878}
3879
Roopa Prabhu11dd74b2020-04-27 13:56:45 -07003880int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify)
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003881{
Roopa Prabhu11dd74b2020-04-27 13:56:45 -07003882 struct nl_info info = {
3883 .nl_net = net,
3884 .skip_notify = skip_notify
3885 };
David Ahernafb1d4b52018-04-17 17:33:11 -07003886
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003887 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003888}
3889
David Ahern8d1c8022018-04-17 17:33:26 -07003890static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
David Ahern0ae81332017-02-02 12:37:08 -08003891{
3892 struct nl_info *info = &cfg->fc_nlinfo;
WANG Conge3330032017-02-27 16:07:43 -08003893 struct net *net = info->nl_net;
David Ahern16a16cd2017-02-02 12:37:11 -08003894 struct sk_buff *skb = NULL;
David Ahern0ae81332017-02-02 12:37:08 -08003895 struct fib6_table *table;
WANG Conge3330032017-02-27 16:07:43 -08003896 int err = -ENOENT;
David Ahern0ae81332017-02-02 12:37:08 -08003897
David Ahern421842e2018-04-17 17:33:18 -07003898 if (rt == net->ipv6.fib6_null_entry)
WANG Conge3330032017-02-27 16:07:43 -08003899 goto out_put;
David Ahern93c2fb22018-04-18 15:38:59 -07003900 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003901 spin_lock_bh(&table->tb6_lock);
David Ahern0ae81332017-02-02 12:37:08 -08003902
David Ahern93c2fb22018-04-18 15:38:59 -07003903 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
David Ahern8d1c8022018-04-17 17:33:26 -07003904 struct fib6_info *sibling, *next_sibling;
Ido Schimmel02846962019-12-23 15:28:18 +02003905 struct fib6_node *fn;
David Ahern0ae81332017-02-02 12:37:08 -08003906
David Ahern16a16cd2017-02-02 12:37:11 -08003907 /* prefer to send a single notification with all hops */
3908 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3909 if (skb) {
3910 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3911
David Ahernd4ead6b2018-04-17 17:33:16 -07003912 if (rt6_fill_node(net, skb, rt, NULL,
David Ahern16a16cd2017-02-02 12:37:11 -08003913 NULL, NULL, 0, RTM_DELROUTE,
3914 info->portid, seq, 0) < 0) {
3915 kfree_skb(skb);
3916 skb = NULL;
3917 } else
3918 info->skip_notify = 1;
3919 }
3920
Ido Schimmel02846962019-12-23 15:28:18 +02003921 /* 'rt' points to the first sibling route. If it is not the
3922 * leaf, then we do not need to send a notification. Otherwise,
3923 * we need to check if the last sibling has a next route or not
3924 * and emit a replace or delete notification, respectively.
3925 */
Ido Schimmel2881fd62019-06-18 18:12:49 +03003926 info->skip_notify_kernel = 1;
Ido Schimmel02846962019-12-23 15:28:18 +02003927 fn = rcu_dereference_protected(rt->fib6_node,
3928 lockdep_is_held(&table->tb6_lock));
3929 if (rcu_access_pointer(fn->leaf) == rt) {
3930 struct fib6_info *last_sibling, *replace_rt;
3931
3932 last_sibling = list_last_entry(&rt->fib6_siblings,
3933 struct fib6_info,
3934 fib6_siblings);
3935 replace_rt = rcu_dereference_protected(
3936 last_sibling->fib6_next,
3937 lockdep_is_held(&table->tb6_lock));
3938 if (replace_rt)
3939 call_fib6_entry_notifiers_replace(net,
3940 replace_rt);
3941 else
3942 call_fib6_multipath_entry_notifiers(net,
Ido Schimmelcaafb252019-12-23 15:28:20 +02003943 FIB_EVENT_ENTRY_DEL,
Ido Schimmel02846962019-12-23 15:28:18 +02003944 rt, rt->fib6_nsiblings,
3945 NULL);
3946 }
David Ahern0ae81332017-02-02 12:37:08 -08003947 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07003948 &rt->fib6_siblings,
3949 fib6_siblings) {
David Ahern0ae81332017-02-02 12:37:08 -08003950 err = fib6_del(sibling, info);
3951 if (err)
WANG Conge3330032017-02-27 16:07:43 -08003952 goto out_unlock;
David Ahern0ae81332017-02-02 12:37:08 -08003953 }
3954 }
3955
3956 err = fib6_del(rt, info);
WANG Conge3330032017-02-27 16:07:43 -08003957out_unlock:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003958 spin_unlock_bh(&table->tb6_lock);
WANG Conge3330032017-02-27 16:07:43 -08003959out_put:
David Ahern93531c62018-04-17 17:33:25 -07003960 fib6_info_release(rt);
David Ahern16a16cd2017-02-02 12:37:11 -08003961
3962 if (skb) {
WANG Conge3330032017-02-27 16:07:43 -08003963 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
David Ahern16a16cd2017-02-02 12:37:11 -08003964 info->nlh, gfp_any());
3965 }
David Ahern0ae81332017-02-02 12:37:08 -08003966 return err;
3967}
3968
David Ahern0fa6efc2019-05-22 20:28:00 -07003969static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
David Ahern23fb93a2018-04-17 17:33:23 -07003970{
3971 int rc = -ESRCH;
3972
3973 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3974 goto out;
3975
3976 if (cfg->fc_flags & RTF_GATEWAY &&
3977 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3978 goto out;
Xin Long761f6022018-11-14 00:48:28 +08003979
3980 rc = rt6_remove_exception_rt(rt);
David Ahern23fb93a2018-04-17 17:33:23 -07003981out:
3982 return rc;
3983}
3984
David Ahern0fa6efc2019-05-22 20:28:00 -07003985static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3986 struct fib6_nh *nh)
3987{
3988 struct fib6_result res = {
3989 .f6i = rt,
3990 .nh = nh,
3991 };
3992 struct rt6_info *rt_cache;
3993
3994 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3995 if (rt_cache)
3996 return __ip6_del_cached_rt(rt_cache, cfg);
3997
3998 return 0;
3999}
4000
David Ahern5b983242019-06-08 14:53:34 -07004001struct fib6_nh_del_cached_rt_arg {
4002 struct fib6_config *cfg;
4003 struct fib6_info *f6i;
4004};
4005
4006static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
4007{
4008 struct fib6_nh_del_cached_rt_arg *arg = _arg;
4009 int rc;
4010
4011 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
4012 return rc != -ESRCH ? rc : 0;
4013}
4014
4015static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
4016{
4017 struct fib6_nh_del_cached_rt_arg arg = {
4018 .cfg = cfg,
4019 .f6i = f6i
4020 };
4021
4022 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
4023}
4024
David Ahern333c4302017-05-21 10:12:04 -06004025static int ip6_route_del(struct fib6_config *cfg,
4026 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027{
Thomas Grafc71099a2006-08-04 23:20:06 -07004028 struct fib6_table *table;
David Ahern8d1c8022018-04-17 17:33:26 -07004029 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 int err = -ESRCH;
4032
Daniel Lezcano55786892008-03-04 13:47:47 -08004033 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David Ahernd5d531c2017-05-21 10:12:05 -06004034 if (!table) {
4035 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Grafc71099a2006-08-04 23:20:06 -07004036 return err;
David Ahernd5d531c2017-05-21 10:12:05 -06004037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
Wei Wang66f5d6c2017-10-06 12:06:10 -07004039 rcu_read_lock();
Thomas Grafc71099a2006-08-04 23:20:06 -07004040
4041 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07004042 &cfg->fc_dst, cfg->fc_dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07004043 &cfg->fc_src, cfg->fc_src_len,
Wei Wang2b760fc2017-10-06 12:06:03 -07004044 !(cfg->fc_flags & RTF_CACHE));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004045
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 if (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07004047 for_each_fib6_node_rt_rcu(fn) {
David Ahernad1601a2019-03-27 20:53:56 -07004048 struct fib6_nh *nh;
4049
Stefano Brivio3401bfb2019-06-21 17:45:25 +02004050 if (rt->nh && cfg->fc_nh_id &&
4051 rt->nh->id != cfg->fc_nh_id)
David Ahern5b983242019-06-08 14:53:34 -07004052 continue;
David Ahern23fb93a2018-04-17 17:33:23 -07004053
David Ahern5b983242019-06-08 14:53:34 -07004054 if (cfg->fc_flags & RTF_CACHE) {
4055 int rc = 0;
4056
4057 if (rt->nh) {
4058 rc = ip6_del_cached_rt_nh(cfg, rt);
4059 } else if (cfg->fc_nh_id) {
4060 continue;
4061 } else {
4062 nh = rt->fib6_nh;
4063 rc = ip6_del_cached_rt(cfg, rt, nh);
4064 }
David Ahern0fa6efc2019-05-22 20:28:00 -07004065 if (rc != -ESRCH) {
4066 rcu_read_unlock();
4067 return rc;
David Ahern23fb93a2018-04-17 17:33:23 -07004068 }
4069 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07004070 }
David Ahernad1601a2019-03-27 20:53:56 -07004071
David Ahern5b983242019-06-08 14:53:34 -07004072 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
4073 continue;
4074 if (cfg->fc_protocol &&
4075 cfg->fc_protocol != rt->fib6_protocol)
4076 continue;
4077
4078 if (rt->nh) {
4079 if (!fib6_info_hold_safe(rt))
4080 continue;
4081 rcu_read_unlock();
4082
4083 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
4084 }
4085 if (cfg->fc_nh_id)
4086 continue;
4087
4088 nh = rt->fib6_nh;
Thomas Graf86872cb2006-08-22 00:01:08 -07004089 if (cfg->fc_ifindex &&
David Ahernad1601a2019-03-27 20:53:56 -07004090 (!nh->fib_nh_dev ||
4091 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07004093 if (cfg->fc_flags & RTF_GATEWAY &&
David Ahernad1601a2019-03-27 20:53:56 -07004094 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07004096 if (!fib6_info_hold_safe(rt))
4097 continue;
Wei Wang66f5d6c2017-10-06 12:06:10 -07004098 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
David Ahern0ae81332017-02-02 12:37:08 -08004100 /* if gateway was specified only delete the one hop */
4101 if (cfg->fc_flags & RTF_GATEWAY)
4102 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
4103
4104 return __ip6_del_rt_siblings(rt, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 }
4106 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07004107 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
4109 return err;
4110}
4111
David S. Miller6700c272012-07-17 03:29:28 -07004112static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07004113{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07004114 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07004115 struct rt6_info *rt, *nrt = NULL;
David Ahern85bd05d2019-04-16 14:36:01 -07004116 struct fib6_result res = {};
David S. Millere8599ff2012-07-11 23:43:53 -07004117 struct ndisc_options ndopts;
4118 struct inet6_dev *in6_dev;
4119 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004120 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07004121 int optlen, on_link;
4122 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07004123
Simon Horman29a3cad2013-05-28 20:34:26 +00004124 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004125 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07004126
4127 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07004128 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004129 return;
4130 }
4131
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004132 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07004133
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004134 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07004135 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004136 return;
4137 }
4138
David S. Miller6e157b62012-07-12 00:05:02 -07004139 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004140 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07004141 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004142 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07004143 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07004144 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004145 return;
4146 }
4147
4148 in6_dev = __in6_dev_get(skb->dev);
4149 if (!in6_dev)
4150 return;
4151 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
4152 return;
4153
4154 /* RFC2461 8.1:
4155 * The IP source address of the Redirect MUST be the same as the current
4156 * first-hop router for the specified ICMP Destination Address.
4157 */
4158
Alexander Aringf997c552016-06-15 21:20:23 +02004159 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07004160 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4161 return;
4162 }
David S. Miller6e157b62012-07-12 00:05:02 -07004163
4164 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07004165 if (ndopts.nd_opts_tgt_lladdr) {
4166 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4167 skb->dev);
4168 if (!lladdr) {
4169 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4170 return;
4171 }
4172 }
4173
David S. Miller6e157b62012-07-12 00:05:02 -07004174 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01004175 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07004176 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4177 return;
4178 }
4179
4180 /* Redirect received -> path was valid.
4181 * Look, redirects are sent only in response to data packets,
4182 * so that this nexthop apparently is reachable. --ANK
4183 */
Julian Anastasov0dec8792017-02-06 23:14:16 +02004184 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
David S. Miller6e157b62012-07-12 00:05:02 -07004185
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004186 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07004187 if (!neigh)
4188 return;
4189
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 /*
4191 * We have finally decided to accept it.
4192 */
4193
Alexander Aringf997c552016-06-15 21:20:23 +02004194 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 NEIGH_UPDATE_F_WEAK_OVERRIDE|
4196 NEIGH_UPDATE_F_OVERRIDE|
4197 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02004198 NEIGH_UPDATE_F_ISROUTER)),
4199 NDISC_REDIRECT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
David Ahern4d85cd02018-04-20 15:37:59 -07004201 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07004202 res.f6i = rcu_dereference(rt->from);
David S. Millerff24e492019-05-02 22:14:21 -04004203 if (!res.f6i)
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004204 goto out;
David Ahern8a14e462018-04-23 11:32:07 -07004205
David Ahern49d5b8e2019-06-08 14:53:30 -07004206 if (res.f6i->nh) {
4207 struct fib6_nh_match_arg arg = {
4208 .dev = dst->dev,
4209 .gw = &rt->rt6i_gateway,
4210 };
4211
4212 nexthop_for_each_fib6_nh(res.f6i->nh,
4213 fib6_nh_find_match, &arg);
4214
4215 /* fib6_info uses a nexthop that does not have fib6_nh
4216 * using the dst->dev. Should be impossible
4217 */
4218 if (!arg.match)
4219 goto out;
4220 res.nh = arg.match;
4221 } else {
4222 res.nh = res.f6i->fib6_nh;
4223 }
4224
David Ahern7d21fec2019-04-16 14:36:11 -07004225 res.fib6_flags = res.f6i->fib6_flags;
4226 res.fib6_type = res.f6i->fib6_type;
David Ahern85bd05d2019-04-16 14:36:01 -07004227 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05004228 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 goto out;
4230
4231 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4232 if (on_link)
4233 nrt->rt6i_flags &= ~RTF_GATEWAY;
4234
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004235 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004237 /* rt6_insert_exception() will take care of duplicated exceptions */
David Ahern5012f0a2019-04-16 14:36:05 -07004238 if (rt6_insert_exception(nrt, &res)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07004239 dst_release_immediate(&nrt->dst);
4240 goto out;
4241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242
Changli Gaod8d1f302010-06-10 23:31:35 -07004243 netevent.old = &rt->dst;
4244 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004245 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00004246 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07004247 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4248
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249out:
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004250 rcu_read_unlock();
David S. Millere8599ff2012-07-11 23:43:53 -07004251 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07004252}
4253
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004254#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -07004255static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004256 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004257 const struct in6_addr *gwaddr,
4258 struct net_device *dev)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004259{
David Ahern830218c2016-10-24 10:52:35 -07004260 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4261 int ifindex = dev->ifindex;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004262 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07004263 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07004264 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004265
David Ahern830218c2016-10-24 10:52:35 -07004266 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004267 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004268 return NULL;
4269
Wei Wang66f5d6c2017-10-06 12:06:10 -07004270 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07004271 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004272 if (!fn)
4273 goto out;
4274
Wei Wang66f5d6c2017-10-06 12:06:10 -07004275 for_each_fib6_node_rt_rcu(fn) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004276 /* these routes do not use nexthops */
4277 if (rt->nh)
4278 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004279 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004280 continue;
David Ahern2b2450c2019-03-27 20:53:52 -07004281 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
David Ahern1cf844c2019-05-22 20:27:59 -07004282 !rt->fib6_nh->fib_nh_gw_family)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004283 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004284 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004285 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07004286 if (!fib6_info_hold_safe(rt))
4287 continue;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004288 break;
4289 }
4290out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004291 rcu_read_unlock();
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004292 return rt;
4293}
4294
David Ahern8d1c8022018-04-17 17:33:26 -07004295static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004296 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004297 const struct in6_addr *gwaddr,
4298 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +00004299 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004300{
Thomas Graf86872cb2006-08-22 00:01:08 -07004301 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08004302 .fc_metric = IP6_RT_PRIO_USER,
David Ahern830218c2016-10-24 10:52:35 -07004303 .fc_ifindex = dev->ifindex,
Thomas Graf86872cb2006-08-22 00:01:08 -07004304 .fc_dst_len = prefixlen,
4305 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4306 RTF_UP | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004307 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004308 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004309 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08004310 .fc_nlinfo.nlh = NULL,
4311 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004312 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004313
Xu Wang91b2c9a2020-09-21 06:38:56 +00004314 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004315 cfg.fc_dst = *prefix;
4316 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07004317
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08004318 /* We should treat it as a default route if prefix length is 0. */
4319 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07004320 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004321
David Ahernacb54e32018-04-17 17:33:22 -07004322 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004323
David Ahern830218c2016-10-24 10:52:35 -07004324 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004325}
4326#endif
4327
David Ahern8d1c8022018-04-17 17:33:26 -07004328struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004329 const struct in6_addr *addr,
4330 struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004331{
David Ahern830218c2016-10-24 10:52:35 -07004332 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
David Ahern8d1c8022018-04-17 17:33:26 -07004333 struct fib6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07004334 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
David Ahernafb1d4b52018-04-17 17:33:11 -07004336 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004337 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004338 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Wei Wang66f5d6c2017-10-06 12:06:10 -07004340 rcu_read_lock();
4341 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004342 struct fib6_nh *nh;
David Ahernad1601a2019-03-27 20:53:56 -07004343
David Ahernf88d8ea2019-06-03 20:19:52 -07004344 /* RA routes do not use nexthops */
4345 if (rt->nh)
4346 continue;
4347
4348 nh = rt->fib6_nh;
David Ahernad1601a2019-03-27 20:53:56 -07004349 if (dev == nh->fib_nh_dev &&
David Ahern93c2fb22018-04-18 15:38:59 -07004350 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Ahernad1601a2019-03-27 20:53:56 -07004351 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 break;
4353 }
Wei Wange873e4b2018-07-21 20:56:32 -07004354 if (rt && !fib6_info_hold_safe(rt))
4355 rt = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07004356 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 return rt;
4358}
4359
David Ahern8d1c8022018-04-17 17:33:26 -07004360struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004361 const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08004362 struct net_device *dev,
Praveen Chaudhary6b2e04b2021-01-25 13:44:30 -08004363 unsigned int pref,
4364 u32 defrtr_usr_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365{
Thomas Graf86872cb2006-08-22 00:01:08 -07004366 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07004367 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Praveen Chaudhary6b2e04b2021-01-25 13:44:30 -08004368 .fc_metric = defrtr_usr_metric,
Thomas Graf86872cb2006-08-22 00:01:08 -07004369 .fc_ifindex = dev->ifindex,
4370 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4371 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004372 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004373 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004374 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08004375 .fc_nlinfo.nlh = NULL,
David Ahernafb1d4b52018-04-17 17:33:11 -07004376 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004377 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004379 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380
David Ahernacb54e32018-04-17 17:33:22 -07004381 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
David Ahern830218c2016-10-24 10:52:35 -07004382 struct fib6_table *table;
4383
4384 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4385 if (table)
4386 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
David Ahernafb1d4b52018-04-17 17:33:11 -07004389 return rt6_get_dflt_router(net, gwaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390}
4391
David Ahernafb1d4b52018-04-17 17:33:11 -07004392static void __rt6_purge_dflt_routers(struct net *net,
4393 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394{
David Ahern8d1c8022018-04-17 17:33:26 -07004395 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
4397restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004398 rcu_read_lock();
4399 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Aherndcd1f572018-04-18 15:39:05 -07004400 struct net_device *dev = fib6_info_nh_dev(rt);
4401 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4402
David Ahern93c2fb22018-04-18 15:38:59 -07004403 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
Wei Wange873e4b2018-07-21 20:56:32 -07004404 (!idev || idev->cnf.accept_ra != 2) &&
4405 fib6_info_hold_safe(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07004406 rcu_read_unlock();
Roopa Prabhu11dd74b2020-04-27 13:56:45 -07004407 ip6_del_rt(net, rt, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 goto restart;
4409 }
4410 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07004411 rcu_read_unlock();
David Ahern830218c2016-10-24 10:52:35 -07004412
4413 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4414}
4415
4416void rt6_purge_dflt_routers(struct net *net)
4417{
4418 struct fib6_table *table;
4419 struct hlist_head *head;
4420 unsigned int h;
4421
4422 rcu_read_lock();
4423
4424 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4425 head = &net->ipv6.fib_table_hash[h];
4426 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4427 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
David Ahernafb1d4b52018-04-17 17:33:11 -07004428 __rt6_purge_dflt_routers(net, table);
David Ahern830218c2016-10-24 10:52:35 -07004429 }
4430 }
4431
4432 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433}
4434
Daniel Lezcano55786892008-03-04 13:47:47 -08004435static void rtmsg_to_fib6_config(struct net *net,
4436 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07004437 struct fib6_config *cfg)
4438{
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004439 *cfg = (struct fib6_config){
4440 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4441 : RT6_TABLE_MAIN,
4442 .fc_ifindex = rtmsg->rtmsg_ifindex,
David Ahern67f69512019-03-21 05:21:34 -07004443 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004444 .fc_expires = rtmsg->rtmsg_info,
4445 .fc_dst_len = rtmsg->rtmsg_dst_len,
4446 .fc_src_len = rtmsg->rtmsg_src_len,
4447 .fc_flags = rtmsg->rtmsg_flags,
4448 .fc_type = rtmsg->rtmsg_type,
Thomas Graf86872cb2006-08-22 00:01:08 -07004449
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004450 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004451
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004452 .fc_dst = rtmsg->rtmsg_dst,
4453 .fc_src = rtmsg->rtmsg_src,
4454 .fc_gateway = rtmsg->rtmsg_gateway,
4455 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004456}
4457
Christoph Hellwig7c1552da2020-05-18 08:28:05 +02004458int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459{
Thomas Graf86872cb2006-08-22 00:01:08 -07004460 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 int err;
4462
Christoph Hellwig7c1552da2020-05-18 08:28:05 +02004463 if (cmd != SIOCADDRT && cmd != SIOCDELRT)
4464 return -EINVAL;
4465 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4466 return -EPERM;
4467
4468 rtmsg_to_fib6_config(net, rtmsg, &cfg);
4469
4470 rtnl_lock();
Ian Morris67ba4152014-08-24 21:53:10 +01004471 switch (cmd) {
Christoph Hellwig7c1552da2020-05-18 08:28:05 +02004472 case SIOCADDRT:
4473 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
4474 break;
4475 case SIOCDELRT:
4476 err = ip6_route_del(&cfg, NULL);
4477 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004478 }
Christoph Hellwig7c1552da2020-05-18 08:28:05 +02004479 rtnl_unlock();
4480 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481}
4482
4483/*
4484 * Drop the packet on the floor
4485 */
4486
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07004487static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488{
Eric Dumazetadf30902009-06-02 05:19:30 +00004489 struct dst_entry *dst = skb_dst(skb);
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004490 struct net *net = dev_net(dst->dev);
4491 struct inet6_dev *idev;
4492 int type;
4493
4494 if (netif_is_l3_master(skb->dev) &&
4495 dst->dev == net->loopback_dev)
4496 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4497 else
4498 idev = ip6_dst_idev(dst);
4499
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004500 switch (ipstats_mib_noroutes) {
4501 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07004502 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00004503 if (type == IPV6_ADDR_ANY) {
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004504 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004505 break;
4506 }
Joe Perchesa8eceea2020-03-12 15:50:22 -07004507 fallthrough;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004508 case IPSTATS_MIB_OUTNOROUTES:
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004509 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004510 break;
4511 }
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004512
4513 /* Start over by dropping the dst for l3mdev case */
4514 if (netif_is_l3_master(skb->dev))
4515 skb_dst_drop(skb);
4516
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00004517 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 kfree_skb(skb);
4519 return 0;
4520}
4521
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004522static int ip6_pkt_discard(struct sk_buff *skb)
4523{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004524 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004525}
4526
Eric W. Biedermanede20592015-10-07 16:48:47 -05004527static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528{
Eric Dumazetadf30902009-06-02 05:19:30 +00004529 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004530 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531}
4532
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004533static int ip6_pkt_prohibit(struct sk_buff *skb)
4534{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004535 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004536}
4537
Eric W. Biedermanede20592015-10-07 16:48:47 -05004538static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004539{
Eric Dumazetadf30902009-06-02 05:19:30 +00004540 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004541 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004542}
4543
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544/*
4545 * Allocate a dst for local (unicast / anycast) address.
4546 */
4547
David Ahern360a9882018-04-18 15:39:00 -07004548struct fib6_info *addrconf_f6i_alloc(struct net *net,
4549 struct inet6_dev *idev,
4550 const struct in6_addr *addr,
4551 bool anycast, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552{
David Ahernc7a1ce32019-03-21 05:21:35 -07004553 struct fib6_config cfg = {
4554 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4555 .fc_ifindex = idev->dev->ifindex,
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004556 .fc_flags = RTF_UP | RTF_NONEXTHOP,
David Ahernc7a1ce32019-03-21 05:21:35 -07004557 .fc_dst = *addr,
4558 .fc_dst_len = 128,
4559 .fc_protocol = RTPROT_KERNEL,
4560 .fc_nlinfo.nl_net = net,
4561 .fc_ignore_dev_down = true,
4562 };
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004563 struct fib6_info *f6i;
David Ahern5f02ce242016-09-10 12:09:54 -07004564
David Aherne8478e82018-04-17 17:33:13 -07004565 if (anycast) {
David Ahernc7a1ce32019-03-21 05:21:35 -07004566 cfg.fc_type = RTN_ANYCAST;
4567 cfg.fc_flags |= RTF_ANYCAST;
David Aherne8478e82018-04-17 17:33:13 -07004568 } else {
David Ahernc7a1ce32019-03-21 05:21:35 -07004569 cfg.fc_type = RTN_LOCAL;
4570 cfg.fc_flags |= RTF_LOCAL;
David Aherne8478e82018-04-17 17:33:13 -07004571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004573 f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
Maciej Żenczykowski8652f172019-09-05 20:56:37 -07004574 if (!IS_ERR(f6i))
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004575 f6i->dst_nocount = true;
4576 return f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577}
4578
Daniel Walterc3968a82011-04-13 21:10:57 +00004579/* remove deleted ip from prefsrc entries */
4580struct arg_dev_net_ip {
4581 struct net_device *dev;
4582 struct net *net;
4583 struct in6_addr *addr;
4584};
4585
David Ahern8d1c8022018-04-17 17:33:26 -07004586static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
Daniel Walterc3968a82011-04-13 21:10:57 +00004587{
4588 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4589 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4590 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4591
David Ahernf88d8ea2019-06-03 20:19:52 -07004592 if (!rt->nh &&
4593 ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
David Ahern421842e2018-04-17 17:33:18 -07004594 rt != net->ipv6.fib6_null_entry &&
David Ahern93c2fb22018-04-18 15:38:59 -07004595 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
Wei Wang60006a42017-10-06 12:05:58 -07004596 spin_lock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004597 /* remove prefsrc entry */
David Ahern93c2fb22018-04-18 15:38:59 -07004598 rt->fib6_prefsrc.plen = 0;
Wei Wang60006a42017-10-06 12:05:58 -07004599 spin_unlock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004600 }
4601 return 0;
4602}
4603
4604void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4605{
4606 struct net *net = dev_net(ifp->idev->dev);
4607 struct arg_dev_net_ip adni = {
4608 .dev = ifp->idev->dev,
4609 .net = net,
4610 .addr = &ifp->addr,
4611 };
Li RongQing0c3584d2013-12-27 16:32:38 +08004612 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00004613}
4614
David Ahern2b2450c2019-03-27 20:53:52 -07004615#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004616
4617/* Remove routers and update dst entries when gateway turn into host. */
David Ahern8d1c8022018-04-17 17:33:26 -07004618static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004619{
4620 struct in6_addr *gateway = (struct in6_addr *)arg;
David Ahernf88d8ea2019-06-03 20:19:52 -07004621 struct fib6_nh *nh;
Duan Jiongbe7a0102014-05-15 15:56:14 +08004622
David Ahernf88d8ea2019-06-03 20:19:52 -07004623 /* RA routes do not use nexthops */
4624 if (rt->nh)
4625 return 0;
4626
4627 nh = rt->fib6_nh;
David Ahern93c2fb22018-04-18 15:38:59 -07004628 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Aherncc5c0732019-05-22 20:27:58 -07004629 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
Duan Jiongbe7a0102014-05-15 15:56:14 +08004630 return -1;
Wei Wangb16cb452017-10-06 12:06:00 -07004631
4632 /* Further clean up cached routes in exception table.
4633 * This is needed because cached route may have a different
4634 * gateway than its 'parent' in the case of an ip redirect.
4635 */
David Aherncc5c0732019-05-22 20:27:58 -07004636 fib6_nh_exceptions_clean_tohost(nh, gateway);
Wei Wangb16cb452017-10-06 12:06:00 -07004637
Duan Jiongbe7a0102014-05-15 15:56:14 +08004638 return 0;
4639}
4640
4641void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4642{
4643 fib6_clean_all(net, fib6_clean_tohost, gateway);
4644}
4645
Ido Schimmel2127d952018-01-07 12:45:03 +02004646struct arg_netdev_event {
4647 const struct net_device *dev;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004648 union {
David Ahernecc56632019-04-23 08:48:09 -07004649 unsigned char nh_flags;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004650 unsigned long event;
4651 };
Ido Schimmel2127d952018-01-07 12:45:03 +02004652};
4653
David Ahern8d1c8022018-04-17 17:33:26 -07004654static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004655{
David Ahern8d1c8022018-04-17 17:33:26 -07004656 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004657 struct fib6_node *fn;
4658
David Ahern93c2fb22018-04-18 15:38:59 -07004659 fn = rcu_dereference_protected(rt->fib6_node,
4660 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004661 iter = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -07004662 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004663 while (iter) {
David Ahern93c2fb22018-04-18 15:38:59 -07004664 if (iter->fib6_metric == rt->fib6_metric &&
David Ahern33bd5ac2018-07-03 14:36:21 -07004665 rt6_qualify_for_ecmp(iter))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004666 return iter;
David Ahern8fb11a92018-05-04 13:54:24 -07004667 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -07004668 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004669 }
4670
4671 return NULL;
4672}
4673
David Ahernf88d8ea2019-06-03 20:19:52 -07004674/* only called for fib entries with builtin fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004675static bool rt6_is_dead(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004676{
David Ahern1cf844c2019-05-22 20:27:59 -07004677 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4678 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4679 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004680 return true;
4681
4682 return false;
4683}
4684
David Ahern8d1c8022018-04-17 17:33:26 -07004685static int rt6_multipath_total_weight(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004686{
David Ahern8d1c8022018-04-17 17:33:26 -07004687 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004688 int total = 0;
4689
4690 if (!rt6_is_dead(rt))
David Ahern1cf844c2019-05-22 20:27:59 -07004691 total += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004692
David Ahern93c2fb22018-04-18 15:38:59 -07004693 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004694 if (!rt6_is_dead(iter))
David Ahern1cf844c2019-05-22 20:27:59 -07004695 total += iter->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004696 }
4697
4698 return total;
4699}
4700
David Ahern8d1c8022018-04-17 17:33:26 -07004701static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004702{
4703 int upper_bound = -1;
4704
4705 if (!rt6_is_dead(rt)) {
David Ahern1cf844c2019-05-22 20:27:59 -07004706 *weight += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004707 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4708 total) - 1;
4709 }
David Ahern1cf844c2019-05-22 20:27:59 -07004710 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004711}
4712
David Ahern8d1c8022018-04-17 17:33:26 -07004713static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004714{
David Ahern8d1c8022018-04-17 17:33:26 -07004715 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004716 int weight = 0;
4717
4718 rt6_upper_bound_set(rt, &weight, total);
4719
David Ahern93c2fb22018-04-18 15:38:59 -07004720 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004721 rt6_upper_bound_set(iter, &weight, total);
4722}
4723
David Ahern8d1c8022018-04-17 17:33:26 -07004724void rt6_multipath_rebalance(struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004725{
David Ahern8d1c8022018-04-17 17:33:26 -07004726 struct fib6_info *first;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004727 int total;
4728
4729 /* In case the entire multipath route was marked for flushing,
4730 * then there is no need to rebalance upon the removal of every
4731 * sibling route.
4732 */
David Ahern93c2fb22018-04-18 15:38:59 -07004733 if (!rt->fib6_nsiblings || rt->should_flush)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004734 return;
4735
4736 /* During lookup routes are evaluated in order, so we need to
4737 * make sure upper bounds are assigned from the first sibling
4738 * onwards.
4739 */
4740 first = rt6_multipath_first_sibling(rt);
4741 if (WARN_ON_ONCE(!first))
4742 return;
4743
4744 total = rt6_multipath_total_weight(first);
4745 rt6_multipath_upper_bound_set(first, total);
4746}
4747
David Ahern8d1c8022018-04-17 17:33:26 -07004748static int fib6_ifup(struct fib6_info *rt, void *p_arg)
Ido Schimmel2127d952018-01-07 12:45:03 +02004749{
4750 const struct arg_netdev_event *arg = p_arg;
David Ahern7aef6852018-04-17 17:33:10 -07004751 struct net *net = dev_net(arg->dev);
Ido Schimmel2127d952018-01-07 12:45:03 +02004752
David Ahernf88d8ea2019-06-03 20:19:52 -07004753 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
David Ahern1cf844c2019-05-22 20:27:59 -07004754 rt->fib6_nh->fib_nh_dev == arg->dev) {
4755 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
David Ahern7aef6852018-04-17 17:33:10 -07004756 fib6_update_sernum_upto_root(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004757 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004758 }
Ido Schimmel2127d952018-01-07 12:45:03 +02004759
4760 return 0;
4761}
4762
David Ahernecc56632019-04-23 08:48:09 -07004763void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
Ido Schimmel2127d952018-01-07 12:45:03 +02004764{
4765 struct arg_netdev_event arg = {
4766 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004767 {
4768 .nh_flags = nh_flags,
4769 },
Ido Schimmel2127d952018-01-07 12:45:03 +02004770 };
4771
4772 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4773 arg.nh_flags |= RTNH_F_LINKDOWN;
4774
4775 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4776}
4777
David Ahernf88d8ea2019-06-03 20:19:52 -07004778/* only called for fib entries with inline fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004779static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004780 const struct net_device *dev)
4781{
David Ahern8d1c8022018-04-17 17:33:26 -07004782 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004783
David Ahern1cf844c2019-05-22 20:27:59 -07004784 if (rt->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004785 return true;
David Ahern93c2fb22018-04-18 15:38:59 -07004786 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004787 if (iter->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004788 return true;
4789
4790 return false;
4791}
4792
David Ahern8d1c8022018-04-17 17:33:26 -07004793static void rt6_multipath_flush(struct fib6_info *rt)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004794{
David Ahern8d1c8022018-04-17 17:33:26 -07004795 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004796
4797 rt->should_flush = 1;
David Ahern93c2fb22018-04-18 15:38:59 -07004798 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004799 iter->should_flush = 1;
4800}
4801
David Ahern8d1c8022018-04-17 17:33:26 -07004802static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004803 const struct net_device *down_dev)
4804{
David Ahern8d1c8022018-04-17 17:33:26 -07004805 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004806 unsigned int dead = 0;
4807
David Ahern1cf844c2019-05-22 20:27:59 -07004808 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4809 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004810 dead++;
David Ahern93c2fb22018-04-18 15:38:59 -07004811 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004812 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4813 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004814 dead++;
4815
4816 return dead;
4817}
4818
David Ahern8d1c8022018-04-17 17:33:26 -07004819static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004820 const struct net_device *dev,
David Ahernecc56632019-04-23 08:48:09 -07004821 unsigned char nh_flags)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004822{
David Ahern8d1c8022018-04-17 17:33:26 -07004823 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004824
David Ahern1cf844c2019-05-22 20:27:59 -07004825 if (rt->fib6_nh->fib_nh_dev == dev)
4826 rt->fib6_nh->fib_nh_flags |= nh_flags;
David Ahern93c2fb22018-04-18 15:38:59 -07004827 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004828 if (iter->fib6_nh->fib_nh_dev == dev)
4829 iter->fib6_nh->fib_nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004830}
4831
David Aherna1a22c12017-01-18 07:40:36 -08004832/* called with write lock held for table with rt */
David Ahern8d1c8022018-04-17 17:33:26 -07004833static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004835 const struct arg_netdev_event *arg = p_arg;
4836 const struct net_device *dev = arg->dev;
David Ahern7aef6852018-04-17 17:33:10 -07004837 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004838
David Ahernf88d8ea2019-06-03 20:19:52 -07004839 if (rt == net->ipv6.fib6_null_entry || rt->nh)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004840 return 0;
4841
4842 switch (arg->event) {
4843 case NETDEV_UNREGISTER:
David Ahern1cf844c2019-05-22 20:27:59 -07004844 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004845 case NETDEV_DOWN:
Ido Schimmel1de178e2018-01-07 12:45:15 +02004846 if (rt->should_flush)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004847 return -1;
David Ahern93c2fb22018-04-18 15:38:59 -07004848 if (!rt->fib6_nsiblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004849 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004850 if (rt6_multipath_uses_dev(rt, dev)) {
4851 unsigned int count;
4852
4853 count = rt6_multipath_dead_count(rt, dev);
David Ahern93c2fb22018-04-18 15:38:59 -07004854 if (rt->fib6_nsiblings + 1 == count) {
Ido Schimmel1de178e2018-01-07 12:45:15 +02004855 rt6_multipath_flush(rt);
4856 return -1;
4857 }
4858 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4859 RTNH_F_LINKDOWN);
David Ahern7aef6852018-04-17 17:33:10 -07004860 fib6_update_sernum(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004861 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004862 }
4863 return -2;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004864 case NETDEV_CHANGE:
David Ahern1cf844c2019-05-22 20:27:59 -07004865 if (rt->fib6_nh->fib_nh_dev != dev ||
David Ahern93c2fb22018-04-18 15:38:59 -07004866 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004867 break;
David Ahern1cf844c2019-05-22 20:27:59 -07004868 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004869 rt6_multipath_rebalance(rt);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004870 break;
Ido Schimmel2b241362018-01-07 12:45:02 +02004871 }
David S. Millerc159d302011-12-26 15:24:36 -05004872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 return 0;
4874}
4875
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004876void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004878 struct arg_netdev_event arg = {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004879 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004880 {
4881 .event = event,
4882 },
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004883 };
David Ahern7c6bb7d2018-10-11 20:17:21 -07004884 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004885
David Ahern7c6bb7d2018-10-11 20:17:21 -07004886 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4887 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4888 else
4889 fib6_clean_all(net, fib6_ifdown, &arg);
Ido Schimmel4c981e22018-01-07 12:45:04 +02004890}
4891
4892void rt6_disable_ip(struct net_device *dev, unsigned long event)
4893{
4894 rt6_sync_down_dev(dev, event);
4895 rt6_uncached_list_flush_dev(dev_net(dev), dev);
4896 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897}
4898
Eric Dumazet95c96172012-04-15 05:58:06 +00004899struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00004901 unsigned int mtu;
David Ahernc0b220c2019-05-22 20:27:57 -07004902 struct fib6_info *f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903};
4904
David Aherncc5c0732019-05-22 20:27:58 -07004905static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
David Ahernc0b220c2019-05-22 20:27:57 -07004906{
4907 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
David Aherncc5c0732019-05-22 20:27:58 -07004908 struct fib6_info *f6i = arg->f6i;
David Ahernc0b220c2019-05-22 20:27:57 -07004909
4910 /* For administrative MTU increase, there is no way to discover
4911 * IPv6 PMTU increase, so PMTU increase should be updated here.
4912 * Since RFC 1981 doesn't include administrative MTU increase
4913 * update PMTU increase is a MUST. (i.e. jumbo frame)
4914 */
4915 if (nh->fib_nh_dev == arg->dev) {
4916 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4917 u32 mtu = f6i->fib6_pmtu;
4918
4919 if (mtu >= arg->mtu ||
4920 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4921 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4922
4923 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07004924 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
David Ahernc0b220c2019-05-22 20:27:57 -07004925 spin_unlock_bh(&rt6_exception_lock);
4926 }
4927
4928 return 0;
4929}
4930
4931static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932{
4933 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4934 struct inet6_dev *idev;
4935
4936 /* In IPv6 pmtu discovery is not optional,
4937 so that RTAX_MTU lock cannot disable it.
4938 We still use this lock to block changes
4939 caused by addrconf/ndisc.
4940 */
4941
4942 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05004943 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 return 0;
4945
David Ahernc0b220c2019-05-22 20:27:57 -07004946 if (fib6_metric_locked(f6i, RTAX_MTU))
4947 return 0;
David Ahernd4ead6b2018-04-17 17:33:16 -07004948
David Ahernc0b220c2019-05-22 20:27:57 -07004949 arg->f6i = f6i;
David Ahern2d442342019-06-08 14:53:31 -07004950 if (f6i->nh) {
4951 /* fib6_nh_mtu_change only returns 0, so this is safe */
4952 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4953 arg);
4954 }
4955
David Ahern1cf844c2019-05-22 20:27:59 -07004956 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957}
4958
Eric Dumazet95c96172012-04-15 05:58:06 +00004959void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960{
Thomas Grafc71099a2006-08-04 23:20:06 -07004961 struct rt6_mtu_change_arg arg = {
4962 .dev = dev,
4963 .mtu = mtu,
4964 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965
Li RongQing0c3584d2013-12-27 16:32:38 +08004966 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967}
4968
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004969static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
David Ahern75425652019-05-22 12:07:43 -07004970 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
Thomas Graf5176f912006-08-26 20:13:18 -07004971 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004972 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07004973 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07004974 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004975 [RTA_PRIORITY] = { .type = NLA_U32 },
4976 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004977 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004978 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004979 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4980 [RTA_ENCAP] = { .type = NLA_NESTED },
Xin Long32bc2012015-12-16 17:50:11 +08004981 [RTA_EXPIRES] = { .type = NLA_U32 },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004982 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +08004983 [RTA_MARK] = { .type = NLA_U32 },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004984 [RTA_TABLE] = { .type = NLA_U32 },
Roopa Prabhueacb9382018-05-22 14:03:28 -07004985 [RTA_IP_PROTO] = { .type = NLA_U8 },
4986 [RTA_SPORT] = { .type = NLA_U16 },
4987 [RTA_DPORT] = { .type = NLA_U16 },
David Ahern5b983242019-06-08 14:53:34 -07004988 [RTA_NH_ID] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004989};
4990
4991static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
David Ahern333c4302017-05-21 10:12:04 -06004992 struct fib6_config *cfg,
4993 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994{
Thomas Graf86872cb2006-08-22 00:01:08 -07004995 struct rtmsg *rtm;
4996 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004997 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07004998 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999
Johannes Berg8cb08172019-04-26 14:07:28 +02005000 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5001 rtm_ipv6_policy, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005002 if (err < 0)
5003 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
Thomas Graf86872cb2006-08-22 00:01:08 -07005005 err = -EINVAL;
5006 rtm = nlmsg_data(nlh);
Thomas Graf86872cb2006-08-22 00:01:08 -07005007
Maciej Żenczykowski84db8402018-09-29 23:44:53 -07005008 *cfg = (struct fib6_config){
5009 .fc_table = rtm->rtm_table,
5010 .fc_dst_len = rtm->rtm_dst_len,
5011 .fc_src_len = rtm->rtm_src_len,
5012 .fc_flags = RTF_UP,
5013 .fc_protocol = rtm->rtm_protocol,
5014 .fc_type = rtm->rtm_type,
5015
5016 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
5017 .fc_nlinfo.nlh = nlh,
5018 .fc_nlinfo.nl_net = sock_net(skb->sk),
5019 };
Thomas Graf86872cb2006-08-22 00:01:08 -07005020
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00005021 if (rtm->rtm_type == RTN_UNREACHABLE ||
5022 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00005023 rtm->rtm_type == RTN_PROHIBIT ||
5024 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07005025 cfg->fc_flags |= RTF_REJECT;
5026
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00005027 if (rtm->rtm_type == RTN_LOCAL)
5028 cfg->fc_flags |= RTF_LOCAL;
5029
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07005030 if (rtm->rtm_flags & RTM_F_CLONED)
5031 cfg->fc_flags |= RTF_CACHE;
5032
David Ahernfc1e64e2018-01-25 16:55:09 -08005033 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
5034
David Ahern5b983242019-06-08 14:53:34 -07005035 if (tb[RTA_NH_ID]) {
5036 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
5037 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
5038 NL_SET_ERR_MSG(extack,
5039 "Nexthop specification and nexthop id are mutually exclusive");
5040 goto errout;
5041 }
5042 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
5043 }
5044
Thomas Graf86872cb2006-08-22 00:01:08 -07005045 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02005046 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07005047 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 }
David Aherne3818542019-02-26 09:00:03 -08005049 if (tb[RTA_VIA]) {
5050 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
5051 goto errout;
5052 }
Thomas Graf86872cb2006-08-22 00:01:08 -07005053
5054 if (tb[RTA_DST]) {
5055 int plen = (rtm->rtm_dst_len + 7) >> 3;
5056
5057 if (nla_len(tb[RTA_DST]) < plen)
5058 goto errout;
5059
5060 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 }
Thomas Graf86872cb2006-08-22 00:01:08 -07005062
5063 if (tb[RTA_SRC]) {
5064 int plen = (rtm->rtm_src_len + 7) >> 3;
5065
5066 if (nla_len(tb[RTA_SRC]) < plen)
5067 goto errout;
5068
5069 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 }
Thomas Graf86872cb2006-08-22 00:01:08 -07005071
Daniel Walterc3968a82011-04-13 21:10:57 +00005072 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02005073 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00005074
Thomas Graf86872cb2006-08-22 00:01:08 -07005075 if (tb[RTA_OIF])
5076 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
5077
5078 if (tb[RTA_PRIORITY])
5079 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
5080
5081 if (tb[RTA_METRICS]) {
5082 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
5083 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 }
Thomas Graf86872cb2006-08-22 00:01:08 -07005085
5086 if (tb[RTA_TABLE])
5087 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
5088
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005089 if (tb[RTA_MULTIPATH]) {
5090 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
5091 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
David Ahern9ed59592017-01-17 14:57:36 -08005092
5093 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
David Ahernc255bd62017-05-27 16:19:27 -06005094 cfg->fc_mp_len, extack);
David Ahern9ed59592017-01-17 14:57:36 -08005095 if (err < 0)
5096 goto errout;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005097 }
5098
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005099 if (tb[RTA_PREF]) {
5100 pref = nla_get_u8(tb[RTA_PREF]);
5101 if (pref != ICMPV6_ROUTER_PREF_LOW &&
5102 pref != ICMPV6_ROUTER_PREF_HIGH)
5103 pref = ICMPV6_ROUTER_PREF_MEDIUM;
5104 cfg->fc_flags |= RTF_PREF(pref);
5105 }
5106
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005107 if (tb[RTA_ENCAP])
5108 cfg->fc_encap = tb[RTA_ENCAP];
5109
David Ahern9ed59592017-01-17 14:57:36 -08005110 if (tb[RTA_ENCAP_TYPE]) {
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005111 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
5112
David Ahernc255bd62017-05-27 16:19:27 -06005113 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
David Ahern9ed59592017-01-17 14:57:36 -08005114 if (err < 0)
5115 goto errout;
5116 }
5117
Xin Long32bc2012015-12-16 17:50:11 +08005118 if (tb[RTA_EXPIRES]) {
5119 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
5120
5121 if (addrconf_finite_timeout(timeout)) {
5122 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
5123 cfg->fc_flags |= RTF_EXPIRES;
5124 }
5125 }
5126
Thomas Graf86872cb2006-08-22 00:01:08 -07005127 err = 0;
5128errout:
5129 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130}
5131
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005132struct rt6_nh {
David Ahern8d1c8022018-04-17 17:33:26 -07005133 struct fib6_info *fib6_info;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005134 struct fib6_config r_cfg;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005135 struct list_head next;
5136};
5137
David Ahernd4ead6b2018-04-17 17:33:16 -07005138static int ip6_route_info_append(struct net *net,
5139 struct list_head *rt6_nh_list,
David Ahern8d1c8022018-04-17 17:33:26 -07005140 struct fib6_info *rt,
5141 struct fib6_config *r_cfg)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005142{
5143 struct rt6_nh *nh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005144 int err = -EEXIST;
5145
5146 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005147 /* check if fib6_info already exists */
5148 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005149 return err;
5150 }
5151
5152 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
5153 if (!nh)
5154 return -ENOMEM;
David Ahern8d1c8022018-04-17 17:33:26 -07005155 nh->fib6_info = rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005156 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
5157 list_add_tail(&nh->next, rt6_nh_list);
5158
5159 return 0;
5160}
5161
David Ahern8d1c8022018-04-17 17:33:26 -07005162static void ip6_route_mpath_notify(struct fib6_info *rt,
5163 struct fib6_info *rt_last,
David Ahern3b1137f2017-02-02 12:37:10 -08005164 struct nl_info *info,
5165 __u16 nlflags)
5166{
5167 /* if this is an APPEND route, then rt points to the first route
5168 * inserted and rt_last points to last route inserted. Userspace
5169 * wants a consistent dump of the route which starts at the first
5170 * nexthop. Since sibling routes are always added at the end of
5171 * the list, find the first sibling of the last route appended
5172 */
David Ahern93c2fb22018-04-18 15:38:59 -07005173 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
5174 rt = list_first_entry(&rt_last->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -07005175 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -07005176 fib6_siblings);
David Ahern3b1137f2017-02-02 12:37:10 -08005177 }
5178
5179 if (rt)
5180 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5181}
5182
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005183static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
5184{
5185 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
5186 bool should_notify = false;
5187 struct fib6_info *leaf;
5188 struct fib6_node *fn;
5189
5190 rcu_read_lock();
5191 fn = rcu_dereference(rt->fib6_node);
5192 if (!fn)
5193 goto out;
5194
5195 leaf = rcu_dereference(fn->leaf);
5196 if (!leaf)
5197 goto out;
5198
5199 if (rt == leaf ||
5200 (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
5201 rt6_qualify_for_ecmp(leaf)))
5202 should_notify = true;
5203out:
5204 rcu_read_unlock();
5205
5206 return should_notify;
5207}
5208
David Ahern333c4302017-05-21 10:12:04 -06005209static int ip6_route_multipath_add(struct fib6_config *cfg,
5210 struct netlink_ext_ack *extack)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005211{
David Ahern8d1c8022018-04-17 17:33:26 -07005212 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
David Ahern3b1137f2017-02-02 12:37:10 -08005213 struct nl_info *info = &cfg->fc_nlinfo;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005214 struct fib6_config r_cfg;
5215 struct rtnexthop *rtnh;
David Ahern8d1c8022018-04-17 17:33:26 -07005216 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005217 struct rt6_nh *err_nh;
5218 struct rt6_nh *nh, *nh_safe;
David Ahern3b1137f2017-02-02 12:37:10 -08005219 __u16 nlflags;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005220 int remaining;
5221 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005222 int err = 1;
5223 int nhn = 0;
5224 int replace = (cfg->fc_nlinfo.nlh &&
5225 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5226 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005227
David Ahern3b1137f2017-02-02 12:37:10 -08005228 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5229 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5230 nlflags |= NLM_F_APPEND;
5231
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02005232 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005233 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005234
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005235 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
David Ahern8d1c8022018-04-17 17:33:26 -07005236 * fib6_info structs per nexthop
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005237 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005238 while (rtnh_ok(rtnh, remaining)) {
5239 memcpy(&r_cfg, cfg, sizeof(*cfg));
5240 if (rtnh->rtnh_ifindex)
5241 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5242
5243 attrlen = rtnh_attrlen(rtnh);
5244 if (attrlen > 0) {
5245 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5246
5247 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5248 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02005249 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005250 r_cfg.fc_flags |= RTF_GATEWAY;
5251 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005252 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5253 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5254 if (nla)
5255 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005256 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005257
David Ahern68e2ffd2018-03-20 10:06:59 -07005258 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
David Ahernacb54e32018-04-17 17:33:22 -07005259 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005260 if (IS_ERR(rt)) {
5261 err = PTR_ERR(rt);
5262 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005263 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005264 }
David Ahernb5d2d752018-07-15 09:35:19 -07005265 if (!rt6_qualify_for_ecmp(rt)) {
5266 err = -EINVAL;
5267 NL_SET_ERR_MSG(extack,
5268 "Device only routes can not be added for IPv6 using the multipath API.");
5269 fib6_info_release(rt);
5270 goto cleanup;
5271 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005272
David Ahern1cf844c2019-05-22 20:27:59 -07005273 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
Ido Schimmel398958a2018-01-09 16:40:28 +02005274
David Ahernd4ead6b2018-04-17 17:33:16 -07005275 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5276 rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005277 if (err) {
David Ahern93531c62018-04-17 17:33:25 -07005278 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005279 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005280 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005281
5282 rtnh = rtnh_next(rtnh, &remaining);
5283 }
5284
Ido Schimmel9eee3b42019-06-20 12:10:21 +03005285 if (list_empty(&rt6_nh_list)) {
5286 NL_SET_ERR_MSG(extack,
5287 "Invalid nexthop configuration - no valid nexthops");
5288 return -EINVAL;
5289 }
5290
David Ahern3b1137f2017-02-02 12:37:10 -08005291 /* for add and replace send one notification with all nexthops.
5292 * Skip the notification in fib6_add_rt2node and send one with
5293 * the full route when done
5294 */
5295 info->skip_notify = 1;
5296
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005297 /* For add and replace, send one notification with all nexthops. For
5298 * append, send one notification with all appended nexthops.
5299 */
5300 info->skip_notify_kernel = 1;
5301
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005302 err_nh = NULL;
5303 list_for_each_entry(nh, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005304 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5305 fib6_info_release(nh->fib6_info);
David Ahern3b1137f2017-02-02 12:37:10 -08005306
David Ahernf7225172018-06-04 13:41:42 -07005307 if (!err) {
5308 /* save reference to last route successfully inserted */
5309 rt_last = nh->fib6_info;
5310
5311 /* save reference to first route for notification */
5312 if (!rt_notif)
5313 rt_notif = nh->fib6_info;
5314 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005315
David Ahern8d1c8022018-04-17 17:33:26 -07005316 /* nh->fib6_info is used or freed at this point, reset to NULL*/
5317 nh->fib6_info = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005318 if (err) {
5319 if (replace && nhn)
Jakub Kicinskia5a82d82019-01-14 10:52:45 -08005320 NL_SET_ERR_MSG_MOD(extack,
5321 "multipath route replace failed (check consistency of installed routes)");
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005322 err_nh = nh;
5323 goto add_errout;
5324 }
5325
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005326 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02005327 * these flags after the first nexthop: if there is a collision,
5328 * we have already failed to add the first nexthop:
5329 * fib6_add_rt2node() has rejected it; when replacing, old
5330 * nexthops have been replaced by first new, the rest should
5331 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005332 */
Muhammad Usama Anjum864db232021-04-09 03:01:29 +05005333 if (cfg->fc_nlinfo.nlh) {
5334 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5335 NLM_F_REPLACE);
5336 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
5337 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005338 nhn++;
5339 }
5340
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005341 /* An in-kernel notification should only be sent in case the new
5342 * multipath route is added as the first route in the node, or if
5343 * it was appended to it. We pass 'rt_notif' since it is the first
5344 * sibling and might allow us to skip some checks in the replace case.
5345 */
5346 if (ip6_route_mpath_should_notify(rt_notif)) {
5347 enum fib_event_type fib_event;
5348
5349 if (rt_notif->fib6_nsiblings != nhn - 1)
5350 fib_event = FIB_EVENT_ENTRY_APPEND;
5351 else
Ido Schimmelcaafb252019-12-23 15:28:20 +02005352 fib_event = FIB_EVENT_ENTRY_REPLACE;
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005353
5354 err = call_fib6_multipath_entry_notifiers(info->nl_net,
5355 fib_event, rt_notif,
5356 nhn - 1, extack);
5357 if (err) {
5358 /* Delete all the siblings that were just added */
5359 err_nh = NULL;
5360 goto add_errout;
5361 }
5362 }
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005363
David Ahern3b1137f2017-02-02 12:37:10 -08005364 /* success ... tell user about new route */
5365 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005366 goto cleanup;
5367
5368add_errout:
David Ahern3b1137f2017-02-02 12:37:10 -08005369 /* send notification for routes that were added so that
5370 * the delete notifications sent by ip6_route_del are
5371 * coherent
5372 */
5373 if (rt_notif)
5374 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5375
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005376 /* Delete routes that were already added */
5377 list_for_each_entry(nh, &rt6_nh_list, next) {
5378 if (err_nh == nh)
5379 break;
David Ahern333c4302017-05-21 10:12:04 -06005380 ip6_route_del(&nh->r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005381 }
5382
5383cleanup:
5384 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005385 if (nh->fib6_info)
5386 fib6_info_release(nh->fib6_info);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005387 list_del(&nh->next);
5388 kfree(nh);
5389 }
5390
5391 return err;
5392}
5393
David Ahern333c4302017-05-21 10:12:04 -06005394static int ip6_route_multipath_del(struct fib6_config *cfg,
5395 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005396{
5397 struct fib6_config r_cfg;
5398 struct rtnexthop *rtnh;
Colin Ian King22912672020-09-11 11:35:09 +01005399 int last_err = 0;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005400 int remaining;
5401 int attrlen;
Colin Ian King22912672020-09-11 11:35:09 +01005402 int err;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005403
5404 remaining = cfg->fc_mp_len;
5405 rtnh = (struct rtnexthop *)cfg->fc_mp;
5406
5407 /* Parse a Multipath Entry */
5408 while (rtnh_ok(rtnh, remaining)) {
5409 memcpy(&r_cfg, cfg, sizeof(*cfg));
5410 if (rtnh->rtnh_ifindex)
5411 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5412
5413 attrlen = rtnh_attrlen(rtnh);
5414 if (attrlen > 0) {
5415 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5416
5417 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5418 if (nla) {
5419 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
5420 r_cfg.fc_flags |= RTF_GATEWAY;
5421 }
5422 }
David Ahern333c4302017-05-21 10:12:04 -06005423 err = ip6_route_del(&r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005424 if (err)
5425 last_err = err;
5426
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005427 rtnh = rtnh_next(rtnh, &remaining);
5428 }
5429
5430 return last_err;
5431}
5432
David Ahernc21ef3e2017-04-16 09:48:24 -07005433static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5434 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435{
Thomas Graf86872cb2006-08-22 00:01:08 -07005436 struct fib6_config cfg;
5437 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438
David Ahern333c4302017-05-21 10:12:04 -06005439 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005440 if (err < 0)
5441 return err;
5442
David Ahern5b983242019-06-08 14:53:34 -07005443 if (cfg.fc_nh_id &&
5444 !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5445 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5446 return -EINVAL;
5447 }
5448
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005449 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005450 return ip6_route_multipath_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005451 else {
5452 cfg.fc_delete_all_nh = 1;
David Ahern333c4302017-05-21 10:12:04 -06005453 return ip6_route_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455}
5456
David Ahernc21ef3e2017-04-16 09:48:24 -07005457static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5458 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459{
Thomas Graf86872cb2006-08-22 00:01:08 -07005460 struct fib6_config cfg;
5461 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
David Ahern333c4302017-05-21 10:12:04 -06005463 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005464 if (err < 0)
5465 return err;
5466
David Ahern67f69512019-03-21 05:21:34 -07005467 if (cfg.fc_metric == 0)
5468 cfg.fc_metric = IP6_RT_PRIO_USER;
5469
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005470 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005471 return ip6_route_multipath_add(&cfg, extack);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005472 else
David Ahernacb54e32018-04-17 17:33:22 -07005473 return ip6_route_add(&cfg, GFP_KERNEL, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474}
5475
David Aherna1b7a1f2019-06-08 14:53:26 -07005476/* add the overhead of this fib6_nh to nexthop_len */
5477static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
Thomas Graf339bf982006-11-10 14:10:15 -08005478{
David Aherna1b7a1f2019-06-08 14:53:26 -07005479 int *nexthop_len = arg;
David Ahernbeb1afac52017-02-02 12:37:09 -08005480
David Aherna1b7a1f2019-06-08 14:53:26 -07005481 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5482 + NLA_ALIGN(sizeof(struct rtnexthop))
5483 + nla_total_size(16); /* RTA_GATEWAY */
David Ahernf88d8ea2019-06-03 20:19:52 -07005484
David Aherna1b7a1f2019-06-08 14:53:26 -07005485 if (nh->fib_nh_lws) {
5486 /* RTA_ENCAP_TYPE */
5487 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5488 /* RTA_ENCAP */
5489 *nexthop_len += nla_total_size(2);
5490 }
David Ahernbeb1afac52017-02-02 12:37:09 -08005491
David Aherna1b7a1f2019-06-08 14:53:26 -07005492 return 0;
5493}
5494
5495static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5496{
5497 int nexthop_len;
5498
5499 if (f6i->nh) {
5500 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5501 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5502 &nexthop_len);
5503 } else {
5504 struct fib6_nh *nh = f6i->fib6_nh;
5505
5506 nexthop_len = 0;
5507 if (f6i->fib6_nsiblings) {
5508 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
5509 + NLA_ALIGN(sizeof(struct rtnexthop))
5510 + nla_total_size(16) /* RTA_GATEWAY */
5511 + lwtunnel_get_encap_size(nh->fib_nh_lws);
5512
5513 nexthop_len *= f6i->fib6_nsiblings;
5514 }
5515 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
David Ahernbeb1afac52017-02-02 12:37:09 -08005516 }
5517
Thomas Graf339bf982006-11-10 14:10:15 -08005518 return NLMSG_ALIGN(sizeof(struct rtmsg))
5519 + nla_total_size(16) /* RTA_SRC */
5520 + nla_total_size(16) /* RTA_DST */
5521 + nla_total_size(16) /* RTA_GATEWAY */
5522 + nla_total_size(16) /* RTA_PREFSRC */
5523 + nla_total_size(4) /* RTA_TABLE */
5524 + nla_total_size(4) /* RTA_IIF */
5525 + nla_total_size(4) /* RTA_OIF */
5526 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08005527 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01005528 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005529 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005530 + nla_total_size(1) /* RTA_PREF */
David Ahernbeb1afac52017-02-02 12:37:09 -08005531 + nexthop_len;
5532}
5533
David Ahernf88d8ea2019-06-03 20:19:52 -07005534static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5535 unsigned char *flags)
5536{
5537 if (nexthop_is_multipath(nh)) {
5538 struct nlattr *mp;
5539
David Ahern4255ff02019-09-03 15:22:12 -07005540 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernf88d8ea2019-06-03 20:19:52 -07005541 if (!mp)
5542 goto nla_put_failure;
5543
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005544 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
David Ahernf88d8ea2019-06-03 20:19:52 -07005545 goto nla_put_failure;
5546
5547 nla_nest_end(skb, mp);
5548 } else {
5549 struct fib6_nh *fib6_nh;
5550
5551 fib6_nh = nexthop_fib6_nh(nh);
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005552 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
David Ahernf88d8ea2019-06-03 20:19:52 -07005553 flags, false) < 0)
5554 goto nla_put_failure;
5555 }
5556
5557 return 0;
5558
5559nla_put_failure:
5560 return -EMSGSIZE;
5561}
5562
David Ahernd4ead6b2018-04-17 17:33:16 -07005563static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -07005564 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -07005565 struct in6_addr *dest, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005566 int iif, int type, u32 portid, u32 seq,
David Ahernf8cfe2c2017-01-17 15:51:08 -08005567 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568{
Xin Long22d0bd82018-09-11 14:33:58 +08005569 struct rt6_info *rt6 = (struct rt6_info *)dst;
5570 struct rt6key *rt6_dst, *rt6_src;
5571 u32 *pmetrics, table, rt6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -07005572 unsigned char nh_flags = 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005573 struct nlmsghdr *nlh;
Xin Long22d0bd82018-09-11 14:33:58 +08005574 struct rtmsg *rtm;
David Ahernd4ead6b2018-04-17 17:33:16 -07005575 long expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
Eric W. Biederman15e47302012-09-07 20:12:54 +00005577 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05005578 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005579 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005580
Xin Long22d0bd82018-09-11 14:33:58 +08005581 if (rt6) {
5582 rt6_dst = &rt6->rt6i_dst;
5583 rt6_src = &rt6->rt6i_src;
5584 rt6_flags = rt6->rt6i_flags;
5585 } else {
5586 rt6_dst = &rt->fib6_dst;
5587 rt6_src = &rt->fib6_src;
5588 rt6_flags = rt->fib6_flags;
5589 }
5590
Thomas Graf2d7202b2006-08-22 00:01:27 -07005591 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 rtm->rtm_family = AF_INET6;
Xin Long22d0bd82018-09-11 14:33:58 +08005593 rtm->rtm_dst_len = rt6_dst->plen;
5594 rtm->rtm_src_len = rt6_src->plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 rtm->rtm_tos = 0;
David Ahern93c2fb22018-04-18 15:38:59 -07005596 if (rt->fib6_table)
5597 table = rt->fib6_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07005598 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07005599 table = RT6_TABLE_UNSPEC;
Kalash Nainwal97f00822019-02-20 16:23:04 -08005600 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
David S. Millerc78679e2012-04-01 20:27:33 -04005601 if (nla_put_u32(skb, RTA_TABLE, table))
5602 goto nla_put_failure;
David Aherne8478e82018-04-17 17:33:13 -07005603
5604 rtm->rtm_type = rt->fib6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 rtm->rtm_flags = 0;
5606 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
David Ahern93c2fb22018-04-18 15:38:59 -07005607 rtm->rtm_protocol = rt->fib6_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
Xin Long22d0bd82018-09-11 14:33:58 +08005609 if (rt6_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 rtm->rtm_flags |= RTM_F_CLONED;
5611
David Ahernd4ead6b2018-04-17 17:33:16 -07005612 if (dest) {
5613 if (nla_put_in6_addr(skb, RTA_DST, dest))
David S. Millerc78679e2012-04-01 20:27:33 -04005614 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005615 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 } else if (rtm->rtm_dst_len)
Xin Long22d0bd82018-09-11 14:33:58 +08005617 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005618 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619#ifdef CONFIG_IPV6_SUBTREES
5620 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02005621 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04005622 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005623 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04005624 } else if (rtm->rtm_src_len &&
Xin Long22d0bd82018-09-11 14:33:58 +08005625 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005626 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005628 if (iif) {
5629#ifdef CONFIG_IPV6_MROUTE
Xin Long22d0bd82018-09-11 14:33:58 +08005630 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
David Ahernfd61c6b2017-01-17 15:51:07 -08005631 int err = ip6mr_get_route(net, skb, rtm, portid);
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02005632
David Ahernfd61c6b2017-01-17 15:51:07 -08005633 if (err == 0)
5634 return 0;
5635 if (err < 0)
5636 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005637 } else
5638#endif
David S. Millerc78679e2012-04-01 20:27:33 -04005639 if (nla_put_u32(skb, RTA_IIF, iif))
5640 goto nla_put_failure;
David Ahernd4ead6b2018-04-17 17:33:16 -07005641 } else if (dest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 struct in6_addr saddr_buf;
David Ahernd4ead6b2018-04-17 17:33:16 -07005643 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02005644 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005645 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07005647
David Ahern93c2fb22018-04-18 15:38:59 -07005648 if (rt->fib6_prefsrc.plen) {
Daniel Walterc3968a82011-04-13 21:10:57 +00005649 struct in6_addr saddr_buf;
David Ahern93c2fb22018-04-18 15:38:59 -07005650 saddr_buf = rt->fib6_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02005651 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005652 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00005653 }
5654
David Ahernd4ead6b2018-04-17 17:33:16 -07005655 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5656 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07005657 goto nla_put_failure;
5658
David Ahern93c2fb22018-04-18 15:38:59 -07005659 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
David S. Millerc78679e2012-04-01 20:27:33 -04005660 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00005661
David Ahernbeb1afac52017-02-02 12:37:09 -08005662 /* For multipath routes, walk the siblings list and add
5663 * each as a nexthop within RTA_MULTIPATH.
5664 */
Xin Long22d0bd82018-09-11 14:33:58 +08005665 if (rt6) {
5666 if (rt6_flags & RTF_GATEWAY &&
5667 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5668 goto nla_put_failure;
5669
5670 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5671 goto nla_put_failure;
Oliver Herms6b13d8f2020-11-19 00:06:51 +01005672
5673 if (dst->lwtstate &&
5674 lwtunnel_fill_encap(skb, dst->lwtstate, RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
5675 goto nla_put_failure;
Xin Long22d0bd82018-09-11 14:33:58 +08005676 } else if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07005677 struct fib6_info *sibling, *next_sibling;
David Ahernbeb1afac52017-02-02 12:37:09 -08005678 struct nlattr *mp;
5679
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005680 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernbeb1afac52017-02-02 12:37:09 -08005681 if (!mp)
5682 goto nla_put_failure;
5683
David Ahern1cf844c2019-05-22 20:27:59 -07005684 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
Xiao Liang597aa162021-09-23 23:03:19 +08005685 rt->fib6_nh->fib_nh_weight, AF_INET6,
5686 0) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005687 goto nla_put_failure;
5688
5689 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07005690 &rt->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005691 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005692 sibling->fib6_nh->fib_nh_weight,
Xiao Liang597aa162021-09-23 23:03:19 +08005693 AF_INET6, 0) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005694 goto nla_put_failure;
5695 }
5696
5697 nla_nest_end(skb, mp);
David Ahernf88d8ea2019-06-03 20:19:52 -07005698 } else if (rt->nh) {
5699 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5700 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005701
David Ahernf88d8ea2019-06-03 20:19:52 -07005702 if (nexthop_is_blackhole(rt->nh))
5703 rtm->rtm_type = RTN_BLACKHOLE;
5704
Roopa Prabhu4f801162020-04-27 13:56:46 -07005705 if (net->ipv4.sysctl_nexthop_compat_mode &&
5706 rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
David Ahernf88d8ea2019-06-03 20:19:52 -07005707 goto nla_put_failure;
5708
5709 rtm->rtm_flags |= nh_flags;
5710 } else {
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005711 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
David Ahernecc56632019-04-23 08:48:09 -07005712 &nh_flags, false) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005713 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005714
5715 rtm->rtm_flags |= nh_flags;
David Ahernbeb1afac52017-02-02 12:37:09 -08005716 }
5717
Xin Long22d0bd82018-09-11 14:33:58 +08005718 if (rt6_flags & RTF_EXPIRES) {
David Ahern14895682018-04-17 17:33:17 -07005719 expires = dst ? dst->expires : rt->expires;
5720 expires -= jiffies;
5721 }
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07005722
Ido Schimmelbb3c4ab2020-01-14 13:23:12 +02005723 if (!dst) {
5724 if (rt->offload)
5725 rtm->rtm_flags |= RTM_F_OFFLOAD;
5726 if (rt->trap)
5727 rtm->rtm_flags |= RTM_F_TRAP;
Amit Cohen0c5fcf92021-02-07 10:22:52 +02005728 if (rt->offload_failed)
5729 rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
Ido Schimmelbb3c4ab2020-01-14 13:23:12 +02005730 }
5731
David Ahernd4ead6b2018-04-17 17:33:16 -07005732 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08005733 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734
Xin Long22d0bd82018-09-11 14:33:58 +08005735 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005736 goto nla_put_failure;
5737
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005738
Johannes Berg053c0952015-01-16 22:09:00 +01005739 nlmsg_end(skb, nlh);
5740 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005741
5742nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005743 nlmsg_cancel(skb, nlh);
5744 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745}
5746
David Ahern2c170e02019-06-08 14:53:27 -07005747static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5748{
5749 const struct net_device *dev = arg;
5750
5751 if (nh->fib_nh_dev == dev)
5752 return 1;
5753
5754 return 0;
5755}
5756
David Ahern13e38902018-10-15 18:56:44 -07005757static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5758 const struct net_device *dev)
5759{
David Ahern2c170e02019-06-08 14:53:27 -07005760 if (f6i->nh) {
5761 struct net_device *_dev = (struct net_device *)dev;
5762
5763 return !!nexthop_for_each_fib6_nh(f6i->nh,
5764 fib6_info_nh_uses_dev,
5765 _dev);
5766 }
5767
David Ahern1cf844c2019-05-22 20:27:59 -07005768 if (f6i->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005769 return true;
5770
5771 if (f6i->fib6_nsiblings) {
5772 struct fib6_info *sibling, *next_sibling;
5773
5774 list_for_each_entry_safe(sibling, next_sibling,
5775 &f6i->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005776 if (sibling->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005777 return true;
5778 }
5779 }
5780
5781 return false;
5782}
5783
Stefano Brivio1e47b482019-06-21 17:45:27 +02005784struct fib6_nh_exception_dump_walker {
5785 struct rt6_rtnl_dump_arg *dump;
5786 struct fib6_info *rt;
5787 unsigned int flags;
5788 unsigned int skip;
5789 unsigned int count;
5790};
5791
5792static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
5793{
5794 struct fib6_nh_exception_dump_walker *w = arg;
5795 struct rt6_rtnl_dump_arg *dump = w->dump;
5796 struct rt6_exception_bucket *bucket;
5797 struct rt6_exception *rt6_ex;
5798 int i, err;
5799
5800 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
5801 if (!bucket)
5802 return 0;
5803
5804 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
5805 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
5806 if (w->skip) {
5807 w->skip--;
5808 continue;
5809 }
5810
5811 /* Expiration of entries doesn't bump sernum, insertion
5812 * does. Removal is triggered by insertion, so we can
5813 * rely on the fact that if entries change between two
5814 * partial dumps, this node is scanned again completely,
5815 * see rt6_insert_exception() and fib6_dump_table().
5816 *
5817 * Count expired entries we go through as handled
5818 * entries that we'll skip next time, in case of partial
5819 * node dump. Otherwise, if entries expire meanwhile,
5820 * we'll skip the wrong amount.
5821 */
5822 if (rt6_check_expired(rt6_ex->rt6i)) {
5823 w->count++;
5824 continue;
5825 }
5826
5827 err = rt6_fill_node(dump->net, dump->skb, w->rt,
5828 &rt6_ex->rt6i->dst, NULL, NULL, 0,
5829 RTM_NEWROUTE,
5830 NETLINK_CB(dump->cb->skb).portid,
5831 dump->cb->nlh->nlmsg_seq, w->flags);
5832 if (err)
5833 return err;
5834
5835 w->count++;
5836 }
5837 bucket++;
5838 }
5839
5840 return 0;
5841}
5842
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005843/* Return -1 if done with node, number of handled routes on partial dump */
Stefano Brivio1e47b482019-06-21 17:45:27 +02005844int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845{
5846 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
David Ahern13e38902018-10-15 18:56:44 -07005847 struct fib_dump_filter *filter = &arg->filter;
5848 unsigned int flags = NLM_F_MULTI;
David Ahern1f17e2f2017-01-26 13:54:08 -08005849 struct net *net = arg->net;
Stefano Brivio1e47b482019-06-21 17:45:27 +02005850 int count = 0;
David Ahern1f17e2f2017-01-26 13:54:08 -08005851
David Ahern421842e2018-04-17 17:33:18 -07005852 if (rt == net->ipv6.fib6_null_entry)
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005853 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854
David Ahern13e38902018-10-15 18:56:44 -07005855 if ((filter->flags & RTM_F_PREFIX) &&
5856 !(rt->fib6_flags & RTF_PREFIX_RT)) {
5857 /* success since this is not a prefix route */
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005858 return -1;
David Ahern13e38902018-10-15 18:56:44 -07005859 }
Stefano Brivio1e47b482019-06-21 17:45:27 +02005860 if (filter->filter_set &&
5861 ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5862 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5863 (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5864 return -1;
5865 }
5866
5867 if (filter->filter_set ||
5868 !filter->dump_routes || !filter->dump_exceptions) {
David Ahern13e38902018-10-15 18:56:44 -07005869 flags |= NLM_F_DUMP_FILTERED;
David Ahernf8cfe2c2017-01-17 15:51:08 -08005870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871
Stefano Brivio1e47b482019-06-21 17:45:27 +02005872 if (filter->dump_routes) {
5873 if (skip) {
5874 skip--;
5875 } else {
5876 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
5877 0, RTM_NEWROUTE,
5878 NETLINK_CB(arg->cb->skb).portid,
5879 arg->cb->nlh->nlmsg_seq, flags)) {
5880 return 0;
5881 }
5882 count++;
5883 }
5884 }
5885
5886 if (filter->dump_exceptions) {
5887 struct fib6_nh_exception_dump_walker w = { .dump = arg,
5888 .rt = rt,
5889 .flags = flags,
5890 .skip = skip,
5891 .count = 0 };
5892 int err;
5893
Eric Dumazet3b525692019-06-26 03:05:28 -07005894 rcu_read_lock();
Stefano Brivio1e47b482019-06-21 17:45:27 +02005895 if (rt->nh) {
5896 err = nexthop_for_each_fib6_nh(rt->nh,
5897 rt6_nh_dump_exceptions,
5898 &w);
5899 } else {
5900 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
5901 }
Eric Dumazet3b525692019-06-26 03:05:28 -07005902 rcu_read_unlock();
Stefano Brivio1e47b482019-06-21 17:45:27 +02005903
5904 if (err)
5905 return count += w.count;
5906 }
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005907
5908 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909}
5910
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005911static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5912 const struct nlmsghdr *nlh,
5913 struct nlattr **tb,
5914 struct netlink_ext_ack *extack)
5915{
5916 struct rtmsg *rtm;
5917 int i, err;
5918
5919 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5920 NL_SET_ERR_MSG_MOD(extack,
5921 "Invalid header for get route request");
5922 return -EINVAL;
5923 }
5924
5925 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02005926 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5927 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005928
5929 rtm = nlmsg_data(nlh);
5930 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5931 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5932 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5933 rtm->rtm_type) {
5934 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5935 return -EINVAL;
5936 }
5937 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5938 NL_SET_ERR_MSG_MOD(extack,
5939 "Invalid flags for get route request");
5940 return -EINVAL;
5941 }
5942
Johannes Berg8cb08172019-04-26 14:07:28 +02005943 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5944 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005945 if (err)
5946 return err;
5947
5948 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5949 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5950 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5951 return -EINVAL;
5952 }
5953
5954 for (i = 0; i <= RTA_MAX; i++) {
5955 if (!tb[i])
5956 continue;
5957
5958 switch (i) {
5959 case RTA_SRC:
5960 case RTA_DST:
5961 case RTA_IIF:
5962 case RTA_OIF:
5963 case RTA_MARK:
5964 case RTA_UID:
5965 case RTA_SPORT:
5966 case RTA_DPORT:
5967 case RTA_IP_PROTO:
5968 break;
5969 default:
5970 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
5971 return -EINVAL;
5972 }
5973 }
5974
5975 return 0;
5976}
5977
David Ahernc21ef3e2017-04-16 09:48:24 -07005978static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5979 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005981 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07005982 struct nlattr *tb[RTA_MAX+1];
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005983 int err, iif = 0, oif = 0;
David Aherna68886a2018-04-20 15:38:02 -07005984 struct fib6_info *from;
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005985 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07005987 struct sk_buff *skb;
5988 struct rtmsg *rtm;
Maciej Żenczykowski744486d2018-09-29 23:44:54 -07005989 struct flowi6 fl6 = {};
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005990 bool fibmatch;
Thomas Grafab364a62006-08-22 00:01:47 -07005991
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005992 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Thomas Grafab364a62006-08-22 00:01:47 -07005993 if (err < 0)
5994 goto errout;
5995
5996 err = -EINVAL;
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02005997 rtm = nlmsg_data(nlh);
5998 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005999 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
Thomas Grafab364a62006-08-22 00:01:47 -07006000
6001 if (tb[RTA_SRC]) {
6002 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
6003 goto errout;
6004
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00006005 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07006006 }
6007
6008 if (tb[RTA_DST]) {
6009 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
6010 goto errout;
6011
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00006012 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07006013 }
6014
6015 if (tb[RTA_IIF])
6016 iif = nla_get_u32(tb[RTA_IIF]);
6017
6018 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00006019 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07006020
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07006021 if (tb[RTA_MARK])
6022 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
6023
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09006024 if (tb[RTA_UID])
6025 fl6.flowi6_uid = make_kuid(current_user_ns(),
6026 nla_get_u32(tb[RTA_UID]));
6027 else
6028 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
6029
Roopa Prabhueacb9382018-05-22 14:03:28 -07006030 if (tb[RTA_SPORT])
6031 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
6032
6033 if (tb[RTA_DPORT])
6034 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
6035
6036 if (tb[RTA_IP_PROTO]) {
6037 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
Hangbin Liu5e1a99e2019-02-27 16:15:29 +08006038 &fl6.flowi6_proto, AF_INET6,
6039 extack);
Roopa Prabhueacb9382018-05-22 14:03:28 -07006040 if (err)
6041 goto errout;
6042 }
6043
Thomas Grafab364a62006-08-22 00:01:47 -07006044 if (iif) {
6045 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00006046 int flags = 0;
6047
Florian Westphal121622d2017-08-15 16:34:42 +02006048 rcu_read_lock();
6049
6050 dev = dev_get_by_index_rcu(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07006051 if (!dev) {
Florian Westphal121622d2017-08-15 16:34:42 +02006052 rcu_read_unlock();
Thomas Grafab364a62006-08-22 00:01:47 -07006053 err = -ENODEV;
6054 goto errout;
6055 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00006056
6057 fl6.flowi6_iif = iif;
6058
6059 if (!ipv6_addr_any(&fl6.saddr))
6060 flags |= RT6_LOOKUP_F_HAS_SADDR;
6061
David Ahernb75cc8f2018-03-02 08:32:17 -08006062 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
Florian Westphal121622d2017-08-15 16:34:42 +02006063
6064 rcu_read_unlock();
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00006065 } else {
6066 fl6.flowi6_oif = oif;
6067
Ido Schimmel58acfd72017-12-20 12:28:25 +02006068 dst = ip6_route_output(net, NULL, &fl6);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07006069 }
6070
Roopa Prabhu18c3a612017-05-25 10:42:40 -07006071
6072 rt = container_of(dst, struct rt6_info, dst);
6073 if (rt->dst.error) {
6074 err = rt->dst.error;
6075 ip6_rt_put(rt);
6076 goto errout;
Thomas Grafab364a62006-08-22 00:01:47 -07006077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078
WANG Cong9d6acb32017-03-01 20:48:39 -08006079 if (rt == net->ipv6.ip6_null_entry) {
6080 err = rt->dst.error;
6081 ip6_rt_put(rt);
6082 goto errout;
6083 }
6084
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05006086 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00006087 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07006088 err = -ENOBUFS;
6089 goto errout;
6090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091
Changli Gaod8d1f302010-06-10 23:31:35 -07006092 skb_dst_set(skb, &rt->dst);
David Aherna68886a2018-04-20 15:38:02 -07006093
6094 rcu_read_lock();
6095 from = rcu_dereference(rt->from);
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07006096 if (from) {
6097 if (fibmatch)
6098 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
6099 iif, RTM_NEWROUTE,
6100 NETLINK_CB(in_skb).portid,
6101 nlh->nlmsg_seq, 0);
6102 else
6103 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
6104 &fl6.saddr, iif, RTM_NEWROUTE,
6105 NETLINK_CB(in_skb).portid,
6106 nlh->nlmsg_seq, 0);
6107 } else {
6108 err = -ENETUNREACH;
6109 }
David Aherna68886a2018-04-20 15:38:02 -07006110 rcu_read_unlock();
6111
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07006113 kfree_skb(skb);
6114 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 }
6116
Eric W. Biederman15e47302012-09-07 20:12:54 +00006117 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07006118errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120}
6121
David Ahern8d1c8022018-04-17 17:33:26 -07006122void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07006123 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124{
6125 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08006126 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08006127 u32 seq;
6128 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08006130 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05006131 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07006132
Roopa Prabhu19e42e42015-07-21 10:43:48 +02006133 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05006134 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07006135 goto errout;
6136
David Ahernd4ead6b2018-04-17 17:33:16 -07006137 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6138 event, info->portid, seq, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08006139 if (err < 0) {
6140 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6141 WARN_ON(err == -EMSGSIZE);
6142 kfree_skb(skb);
6143 goto errout;
6144 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00006145 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08006146 info->nlh, gfp_any());
6147 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07006148errout:
6149 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08006150 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151}
6152
David Ahern19a3b7e2019-05-22 12:04:41 -07006153void fib6_rt_update(struct net *net, struct fib6_info *rt,
6154 struct nl_info *info)
6155{
6156 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6157 struct sk_buff *skb;
6158 int err = -ENOBUFS;
6159
David Ahern19a3b7e2019-05-22 12:04:41 -07006160 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6161 if (!skb)
6162 goto errout;
6163
6164 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6165 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
6166 if (err < 0) {
6167 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6168 WARN_ON(err == -EMSGSIZE);
6169 kfree_skb(skb);
6170 goto errout;
6171 }
6172 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6173 info->nlh, gfp_any());
6174 return;
6175errout:
6176 if (err < 0)
6177 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6178}
6179
Amit Cohen907eea42021-02-01 21:47:55 +02006180void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i,
Amit Cohen0c5fcf92021-02-07 10:22:52 +02006181 bool offload, bool trap, bool offload_failed)
Amit Cohen907eea42021-02-01 21:47:55 +02006182{
6183 struct sk_buff *skb;
6184 int err;
6185
Amit Cohen0c5fcf92021-02-07 10:22:52 +02006186 if (f6i->offload == offload && f6i->trap == trap &&
6187 f6i->offload_failed == offload_failed)
Amit Cohen907eea42021-02-01 21:47:55 +02006188 return;
6189
6190 f6i->offload = offload;
6191 f6i->trap = trap;
Amit Cohen6fad3612021-02-07 10:22:53 +02006192
6193 /* 2 means send notifications only if offload_failed was changed. */
6194 if (net->ipv6.sysctl.fib_notify_on_flag_change == 2 &&
6195 f6i->offload_failed == offload_failed)
6196 return;
6197
Amit Cohen0c5fcf92021-02-07 10:22:52 +02006198 f6i->offload_failed = offload_failed;
Amit Cohen907eea42021-02-01 21:47:55 +02006199
6200 if (!rcu_access_pointer(f6i->fib6_node))
6201 /* The route was removed from the tree, do not send
Bhaskar Chowdhury89e83472021-03-27 04:42:41 +05306202 * notification.
Amit Cohen907eea42021-02-01 21:47:55 +02006203 */
6204 return;
6205
6206 if (!net->ipv6.sysctl.fib_notify_on_flag_change)
6207 return;
6208
6209 skb = nlmsg_new(rt6_nlmsg_size(f6i), GFP_KERNEL);
6210 if (!skb) {
6211 err = -ENOBUFS;
6212 goto errout;
6213 }
6214
6215 err = rt6_fill_node(net, skb, f6i, NULL, NULL, NULL, 0, RTM_NEWROUTE, 0,
6216 0, 0);
6217 if (err < 0) {
6218 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6219 WARN_ON(err == -EMSGSIZE);
6220 kfree_skb(skb);
6221 goto errout;
6222 }
6223
6224 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_ROUTE, NULL, GFP_KERNEL);
6225 return;
6226
6227errout:
6228 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6229}
6230EXPORT_SYMBOL(fib6_info_hw_flags_set);
6231
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006232static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00006233 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006234{
Jiri Pirko351638e2013-05-28 01:30:21 +00006235 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09006236 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006237
WANG Cong242d3a42017-05-08 10:12:13 -07006238 if (!(dev->flags & IFF_LOOPBACK))
6239 return NOTIFY_OK;
6240
6241 if (event == NETDEV_REGISTER) {
David Ahern1cf844c2019-05-22 20:27:59 -07006242 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
Changli Gaod8d1f302010-06-10 23:31:35 -07006243 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006244 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
6245#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07006246 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006247 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07006248 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006249 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
6250#endif
WANG Cong76da0702017-06-20 11:42:27 -07006251 } else if (event == NETDEV_UNREGISTER &&
6252 dev->reg_state != NETREG_UNREGISTERED) {
6253 /* NETDEV_UNREGISTER could be fired for multiple times by
6254 * netdev_wait_allrefs(). Make sure we only call this once.
6255 */
Eric Dumazet12d94a82017-08-15 04:09:51 -07006256 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07006257#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric Dumazet12d94a82017-08-15 04:09:51 -07006258 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
6259 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07006260#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006261 }
6262
6263 return NOTIFY_OK;
6264}
6265
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266/*
6267 * /proc
6268 */
6269
6270#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6272{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006273 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006275 net->ipv6.rt6_stats->fib_nodes,
6276 net->ipv6.rt6_stats->fib_route_nodes,
Wei Wang81eb8442017-10-06 12:06:11 -07006277 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006278 net->ipv6.rt6_stats->fib_rt_entries,
6279 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00006280 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006281 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282
6283 return 0;
6284}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285#endif /* CONFIG_PROC_FS */
6286
6287#ifdef CONFIG_SYSCTL
6288
Christoph Hellwig32927392020-04-24 08:43:38 +02006289static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
6290 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006292 struct net *net;
6293 int delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06006294 int ret;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006295 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006297
6298 net = (struct net *)ctl->extra1;
6299 delay = net->ipv6.sysctl.flush_delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06006300 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6301 if (ret)
6302 return ret;
6303
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02006304 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306}
6307
David Aherned792e22018-10-08 14:06:34 -07006308static struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006309 {
Alexander Kuznetsov06e6c882021-10-27 11:00:08 +03006310 .procname = "max_size",
6311 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 .maxlen = sizeof(int),
Alexander Kuznetsov06e6c882021-10-27 11:00:08 +03006313 .mode = 0644,
6314 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315 },
6316 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006318 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 .maxlen = sizeof(int),
6320 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006321 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 },
6323 {
Alexander Kuznetsov06e6c882021-10-27 11:00:08 +03006324 .procname = "flush",
6325 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 .maxlen = sizeof(int),
Alexander Kuznetsov06e6c882021-10-27 11:00:08 +03006327 .mode = 0200,
6328 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329 },
6330 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08006332 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 .maxlen = sizeof(int),
6334 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006335 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 },
6337 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08006339 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 .maxlen = sizeof(int),
6341 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006342 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 },
6344 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08006346 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 .maxlen = sizeof(int),
6348 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006349 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 },
6351 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08006353 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 .maxlen = sizeof(int),
6355 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07006356 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 },
6358 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08006360 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 .maxlen = sizeof(int),
6362 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006363 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364 },
6365 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08006367 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368 .maxlen = sizeof(int),
6369 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07006370 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 },
6372 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08006374 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 .maxlen = sizeof(int),
6376 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006377 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378 },
David Ahern7c6bb7d2018-10-11 20:17:21 -07006379 {
6380 .procname = "skip_notify_on_dev_down",
6381 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
6382 .maxlen = sizeof(int),
6383 .mode = 0644,
Eiichi Tsukatab8e8a862019-06-25 12:08:01 +09006384 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07006385 .extra1 = SYSCTL_ZERO,
6386 .extra2 = SYSCTL_ONE,
David Ahern7c6bb7d2018-10-11 20:17:21 -07006387 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006388 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389};
6390
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006391struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08006392{
6393 struct ctl_table *table;
6394
6395 table = kmemdup(ipv6_route_table_template,
6396 sizeof(ipv6_route_table_template),
6397 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006398
6399 if (table) {
Alexander Kuznetsov06e6c882021-10-27 11:00:08 +03006400 table[0].data = &net->ipv6.sysctl.ip6_rt_max_size;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00006401 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
Alexander Kuznetsov06e6c882021-10-27 11:00:08 +03006402 table[2].data = &net->ipv6.sysctl.flush_delay;
6403 table[2].extra1 = net;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006404 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6405 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6406 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6407 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6408 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6409 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08006410 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
David Ahern7c6bb7d2018-10-11 20:17:21 -07006411 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
Eric W. Biederman464dc802012-11-16 03:02:59 +00006412
6413 /* Don't export sysctls to unprivileged users */
6414 if (net->user_ns != &init_user_ns)
Alexander Kuznetsov06e6c882021-10-27 11:00:08 +03006415 table[1].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006416 }
6417
Daniel Lezcano760f2d02008-01-10 02:53:43 -08006418 return table;
6419}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420#endif
6421
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006422static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006423{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07006424 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006425
Alexey Dobriyan86393e52009-08-29 01:34:49 +00006426 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6427 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006428
Eric Dumazetfc66f952010-10-08 06:37:34 +00006429 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6430 goto out_ip6_dst_ops;
6431
David Ahern1cf844c2019-05-22 20:27:59 -07006432 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
David Ahern421842e2018-04-17 17:33:18 -07006433 if (!net->ipv6.fib6_null_entry)
6434 goto out_ip6_dst_entries;
David Ahern1cf844c2019-05-22 20:27:59 -07006435 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6436 sizeof(*net->ipv6.fib6_null_entry));
David Ahern421842e2018-04-17 17:33:18 -07006437
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006438 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6439 sizeof(*net->ipv6.ip6_null_entry),
6440 GFP_KERNEL);
6441 if (!net->ipv6.ip6_null_entry)
David Ahern421842e2018-04-17 17:33:18 -07006442 goto out_fib6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006443 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006444 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6445 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006446 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006447
6448#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Vincent Bernatfeca7d82017-08-08 20:23:49 +02006449 net->ipv6.fib6_has_custom_rules = false;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006450 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6451 sizeof(*net->ipv6.ip6_prohibit_entry),
6452 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006453 if (!net->ipv6.ip6_prohibit_entry)
6454 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006455 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006456 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6457 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006458 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006459
6460 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6461 sizeof(*net->ipv6.ip6_blk_hole_entry),
6462 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006463 if (!net->ipv6.ip6_blk_hole_entry)
6464 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006465 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006466 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6467 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006468 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
Paolo Abenib9b33e72019-11-20 13:47:34 +01006469#ifdef CONFIG_IPV6_SUBTREES
6470 net->ipv6.fib6_routes_require_src = 0;
6471#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006472#endif
6473
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006474 net->ipv6.sysctl.flush_delay = 0;
6475 net->ipv6.sysctl.ip6_rt_max_size = 4096;
6476 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6477 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6478 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6479 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6480 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6481 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
David Ahern7c6bb7d2018-10-11 20:17:21 -07006482 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006483
Benjamin Thery6891a342008-03-04 13:49:47 -08006484 net->ipv6.ip6_rt_gc_expire = 30*HZ;
6485
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006486 ret = 0;
6487out:
6488 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006489
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006490#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6491out_ip6_prohibit_entry:
6492 kfree(net->ipv6.ip6_prohibit_entry);
6493out_ip6_null_entry:
6494 kfree(net->ipv6.ip6_null_entry);
6495#endif
David Ahern421842e2018-04-17 17:33:18 -07006496out_fib6_null_entry:
6497 kfree(net->ipv6.fib6_null_entry);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006498out_ip6_dst_entries:
6499 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006500out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006501 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006502}
6503
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006504static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006505{
David Ahern421842e2018-04-17 17:33:18 -07006506 kfree(net->ipv6.fib6_null_entry);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006507 kfree(net->ipv6.ip6_null_entry);
6508#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6509 kfree(net->ipv6.ip6_prohibit_entry);
6510 kfree(net->ipv6.ip6_blk_hole_entry);
6511#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006512 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006513}
6514
Thomas Grafd1896342012-06-18 12:08:33 +00006515static int __net_init ip6_route_net_init_late(struct net *net)
6516{
6517#ifdef CONFIG_PROC_FS
Christoph Hellwigc3506372018-04-10 19:42:55 +02006518 proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
6519 sizeof(struct ipv6_route_iter));
Christoph Hellwig3617d942018-04-13 20:38:35 +02006520 proc_create_net_single("rt6_stats", 0444, net->proc_net,
6521 rt6_stats_seq_show, NULL);
Thomas Grafd1896342012-06-18 12:08:33 +00006522#endif
6523 return 0;
6524}
6525
6526static void __net_exit ip6_route_net_exit_late(struct net *net)
6527{
6528#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00006529 remove_proc_entry("ipv6_route", net->proc_net);
6530 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00006531#endif
6532}
6533
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006534static struct pernet_operations ip6_route_net_ops = {
6535 .init = ip6_route_net_init,
6536 .exit = ip6_route_net_exit,
6537};
6538
David S. Millerc3426b42012-06-09 16:27:05 -07006539static int __net_init ipv6_inetpeer_init(struct net *net)
6540{
6541 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6542
6543 if (!bp)
6544 return -ENOMEM;
6545 inet_peer_base_init(bp);
6546 net->ipv6.peers = bp;
6547 return 0;
6548}
6549
6550static void __net_exit ipv6_inetpeer_exit(struct net *net)
6551{
6552 struct inet_peer_base *bp = net->ipv6.peers;
6553
6554 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07006555 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07006556 kfree(bp);
6557}
6558
David S. Miller2b823f72012-06-09 19:00:16 -07006559static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07006560 .init = ipv6_inetpeer_init,
6561 .exit = ipv6_inetpeer_exit,
6562};
6563
Thomas Grafd1896342012-06-18 12:08:33 +00006564static struct pernet_operations ip6_route_net_late_ops = {
6565 .init = ip6_route_net_init_late,
6566 .exit = ip6_route_net_exit_late,
6567};
6568
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006569static struct notifier_block ip6_route_dev_notifier = {
6570 .notifier_call = ip6_route_dev_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07006571 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006572};
6573
WANG Cong2f460932017-05-03 22:07:31 -07006574void __init ip6_route_init_special_entries(void)
6575{
6576 /* Registering of the loopback is done before this portion of code,
6577 * the loopback reference in rt6_info will not be taken, do it
6578 * manually for init_net */
David Ahern1cf844c2019-05-22 20:27:59 -07006579 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
WANG Cong2f460932017-05-03 22:07:31 -07006580 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6581 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6582 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6583 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6584 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6585 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6586 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6587 #endif
6588}
6589
Yonghong Song138d0be2020-05-09 10:59:10 -07006590#if IS_BUILTIN(CONFIG_IPV6)
6591#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6592DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
6593
Yonghong Song951cf362020-07-20 09:34:03 -07006594BTF_ID_LIST(btf_fib6_info_id)
6595BTF_ID(struct, fib6_info)
6596
Yonghong Song14fc6bd62020-07-23 11:41:09 -07006597static const struct bpf_iter_seq_info ipv6_route_seq_info = {
Yonghong Song15172a42020-05-13 11:02:19 -07006598 .seq_ops = &ipv6_route_seq_ops,
6599 .init_seq_private = bpf_iter_init_seq_net,
6600 .fini_seq_private = bpf_iter_fini_seq_net,
6601 .seq_priv_size = sizeof(struct ipv6_route_iter),
Yonghong Song14fc6bd62020-07-23 11:41:09 -07006602};
6603
6604static struct bpf_iter_reg ipv6_route_reg_info = {
6605 .target = "ipv6_route",
Yonghong Song3c32cc12020-05-13 11:02:21 -07006606 .ctx_arg_info_size = 1,
6607 .ctx_arg_info = {
6608 { offsetof(struct bpf_iter__ipv6_route, rt),
6609 PTR_TO_BTF_ID_OR_NULL },
6610 },
Yonghong Song14fc6bd62020-07-23 11:41:09 -07006611 .seq_info = &ipv6_route_seq_info,
Yonghong Song15172a42020-05-13 11:02:19 -07006612};
6613
Yonghong Song138d0be2020-05-09 10:59:10 -07006614static int __init bpf_iter_register(void)
6615{
Yonghong Song951cf362020-07-20 09:34:03 -07006616 ipv6_route_reg_info.ctx_arg_info[0].btf_id = *btf_fib6_info_id;
Yonghong Song15172a42020-05-13 11:02:19 -07006617 return bpf_iter_reg_target(&ipv6_route_reg_info);
Yonghong Song138d0be2020-05-09 10:59:10 -07006618}
6619
6620static void bpf_iter_unregister(void)
6621{
Yonghong Songab2ee4f2020-05-13 11:02:20 -07006622 bpf_iter_unreg_target(&ipv6_route_reg_info);
Yonghong Song138d0be2020-05-09 10:59:10 -07006623}
6624#endif
6625#endif
6626
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006627int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006629 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006630 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006631
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006632 ret = -ENOMEM;
6633 ip6_dst_ops_template.kmem_cachep =
6634 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
Vasily Averin61268912021-07-19 13:44:31 +03006635 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006636 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08006637 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07006638
Eric Dumazetfc66f952010-10-08 06:37:34 +00006639 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006640 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006641 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006642
David S. Millerc3426b42012-06-09 16:27:05 -07006643 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6644 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006645 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00006646
David S. Miller7e52b332012-06-15 15:51:55 -07006647 ret = register_pernet_subsys(&ip6_route_net_ops);
6648 if (ret)
6649 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07006650
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07006651 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6652
David S. Millere8803b62012-06-16 01:12:19 -07006653 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006654 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006655 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006656
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006657 ret = xfrm6_init();
6658 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006659 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08006660
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006661 ret = fib6_rules_init();
6662 if (ret)
6663 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08006664
Thomas Grafd1896342012-06-18 12:08:33 +00006665 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6666 if (ret)
6667 goto fib6_rules_init;
6668
Florian Westphal16feebc2017-12-02 21:44:08 +01006669 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6670 inet6_rtm_newroute, NULL, 0);
6671 if (ret < 0)
6672 goto out_register_late_subsys;
6673
6674 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6675 inet6_rtm_delroute, NULL, 0);
6676 if (ret < 0)
6677 goto out_register_late_subsys;
6678
6679 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6680 inet6_rtm_getroute, NULL,
6681 RTNL_FLAG_DOIT_UNLOCKED);
6682 if (ret < 0)
Thomas Grafd1896342012-06-18 12:08:33 +00006683 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006684
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006685 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006686 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00006687 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006688
Yonghong Song138d0be2020-05-09 10:59:10 -07006689#if IS_BUILTIN(CONFIG_IPV6)
6690#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6691 ret = bpf_iter_register();
6692 if (ret)
6693 goto out_register_late_subsys;
6694#endif
6695#endif
6696
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006697 for_each_possible_cpu(cpu) {
6698 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6699
6700 INIT_LIST_HEAD(&ul->head);
6701 spin_lock_init(&ul->lock);
6702 }
6703
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006704out:
6705 return ret;
6706
Thomas Grafd1896342012-06-18 12:08:33 +00006707out_register_late_subsys:
Florian Westphal16feebc2017-12-02 21:44:08 +01006708 rtnl_unregister_all(PF_INET6);
Thomas Grafd1896342012-06-18 12:08:33 +00006709 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006710fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006711 fib6_rules_cleanup();
6712xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006713 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00006714out_fib6_init:
6715 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006716out_register_subsys:
6717 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07006718out_register_inetpeer:
6719 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006720out_dst_entries:
6721 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006722out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006723 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006724 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725}
6726
6727void ip6_route_cleanup(void)
6728{
Yonghong Song138d0be2020-05-09 10:59:10 -07006729#if IS_BUILTIN(CONFIG_IPV6)
6730#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6731 bpf_iter_unregister();
6732#endif
6733#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006734 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00006735 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07006736 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07006739 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006740 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006741 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006742 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743}