blob: d16c2c9bfebd3dadd4c8dbc4f14836574bb52bbe [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * net/core/dst.c Protocol independent destination cache.
4 *
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 *
7 */
8
9#include <linux/bitops.h>
10#include <linux/errno.h>
11#include <linux/init.h>
12#include <linux/kernel.h>
Eric Dumazet86bba262007-09-12 14:29:01 +020013#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/mm.h>
15#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/skbuff.h>
19#include <linux/string.h>
20#include <linux/types.h>
Eric W. Biedermane9dc8652007-09-12 13:02:17 +020021#include <net/net_namespace.h>
Eric Dumazet2fc1b5d2010-02-08 15:00:39 -080022#include <linux/sched.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070023#include <linux/prefetch.h>
Jiri Benc61adedf2015-08-20 13:56:25 +020024#include <net/lwtunnel.h>
David Millerb6ca8bd2017-11-28 15:45:44 -050025#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <net/dst.h>
Thomas Graff38a9eb2015-07-21 10:43:56 +020028#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Eric W. Biedermanede20592015-10-07 16:48:47 -050030int dst_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 kfree_skb(skb);
33 return 0;
34}
Eric W. Biedermanede20592015-10-07 16:48:47 -050035EXPORT_SYMBOL(dst_discard_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Eric Dumazet3fb07da2017-05-25 14:27:35 -070037const struct dst_metrics dst_default_metrics = {
Eric Dumazeta37e6e32012-08-07 10:55:45 +000038 /* This initializer is needed to force linker to place this variable
39 * into const section. Otherwise it might end into bss section.
40 * We really want to avoid false sharing on this variable, and catch
41 * any writes on it.
42 */
Eric Dumazet9620fef2017-08-18 12:08:07 -070043 .refcnt = REFCOUNT_INIT(1),
Eric Dumazeta37e6e32012-08-07 10:55:45 +000044};
David Ahernd4ead6b2018-04-17 17:33:16 -070045EXPORT_SYMBOL(dst_default_metrics);
Eric Dumazeta37e6e32012-08-07 10:55:45 +000046
Thomas Graff38a9eb2015-07-21 10:43:56 +020047void dst_init(struct dst_entry *dst, struct dst_ops *ops,
48 struct net_device *dev, int initial_ref, int initial_obsolete,
49 unsigned short flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
David S. Miller5c1e6aa2011-04-28 14:13:38 -070051 dst->dev = dev;
Eric Dumazet9038c322021-12-04 20:22:03 -080052 dev_hold_track(dev, &dst->dev_tracker, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 dst->ops = ops;
Eric Dumazet3fb07da2017-05-25 14:27:35 -070054 dst_init_metrics(dst, dst_default_metrics.metrics, true);
David S. Millercf911662011-04-28 14:31:47 -070055 dst->expires = 0UL;
David S. Millercf911662011-04-28 14:31:47 -070056#ifdef CONFIG_XFRM
57 dst->xfrm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
David S. Miller5c1e6aa2011-04-28 14:13:38 -070059 dst->input = dst_discard;
Eric W. Biedermanede20592015-10-07 16:48:47 -050060 dst->output = dst_discard_out;
David S. Millercf911662011-04-28 14:31:47 -070061 dst->error = 0;
David S. Miller5c1e6aa2011-04-28 14:13:38 -070062 dst->obsolete = initial_obsolete;
David S. Millercf911662011-04-28 14:31:47 -070063 dst->header_len = 0;
64 dst->trailer_len = 0;
65#ifdef CONFIG_IP_ROUTE_CLASSID
66 dst->tclassid = 0;
67#endif
Jiri Benc61adedf2015-08-20 13:56:25 +020068 dst->lwtstate = NULL;
David S. Miller5c1e6aa2011-04-28 14:13:38 -070069 atomic_set(&dst->__refcnt, initial_ref);
David S. Millercf911662011-04-28 14:31:47 -070070 dst->__use = 0;
David S. Miller5c1e6aa2011-04-28 14:13:38 -070071 dst->lastuse = jiffies;
72 dst->flags = flags;
David S. Miller957c6652011-06-24 15:25:00 -070073 if (!(flags & DST_NOCOUNT))
74 dst_entries_add(ops, 1);
Thomas Graff38a9eb2015-07-21 10:43:56 +020075}
76EXPORT_SYMBOL(dst_init);
77
78void *dst_alloc(struct dst_ops *ops, struct net_device *dev,
79 int initial_ref, int initial_obsolete, unsigned short flags)
80{
81 struct dst_entry *dst;
82
Eric Dumazetcf86a082020-05-07 18:58:10 -070083 if (ops->gc &&
84 !(flags & DST_NOCOUNT) &&
85 dst_entries_get_fast(ops) > ops->gc_thresh) {
Peter Oskolkov22c2ad62019-01-16 08:50:28 -080086 if (ops->gc(ops)) {
Eric Dumazetcf86a082020-05-07 18:58:10 -070087 pr_notice_ratelimited("Route cache is full: consider increasing sysctl net.ipv6.route.max_size.\n");
Thomas Graff38a9eb2015-07-21 10:43:56 +020088 return NULL;
Peter Oskolkov22c2ad62019-01-16 08:50:28 -080089 }
Thomas Graff38a9eb2015-07-21 10:43:56 +020090 }
91
92 dst = kmem_cache_alloc(ops->kmem_cachep, GFP_ATOMIC);
93 if (!dst)
94 return NULL;
95
96 dst_init(dst, ops, dev, initial_ref, initial_obsolete, flags);
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return dst;
99}
laurent chavey598ed932010-03-29 10:41:36 +0000100EXPORT_SYMBOL(dst_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102struct dst_entry *dst_destroy(struct dst_entry * dst)
103{
David Millerb92cf4a2017-11-28 15:40:22 -0500104 struct dst_entry *child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 smp_rmb();
107
David Millerb92cf4a2017-11-28 15:40:22 -0500108#ifdef CONFIG_XFRM
David Millerb6ca8bd2017-11-28 15:45:44 -0500109 if (dst->xfrm) {
110 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
111
112 child = xdst->child;
113 }
David Millerb92cf4a2017-11-28 15:40:22 -0500114#endif
David S. Miller957c6652011-06-24 15:25:00 -0700115 if (!(dst->flags & DST_NOCOUNT))
116 dst_entries_add(dst->ops, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 if (dst->ops->destroy)
119 dst->ops->destroy(dst);
Eric Dumazet9038c322021-12-04 20:22:03 -0800120 dev_put_track(dst->dev, &dst->dev_tracker);
Thomas Graff38a9eb2015-07-21 10:43:56 +0200121
WANG Conge252b3d2015-08-25 10:38:53 -0700122 lwtstate_put(dst->lwtstate);
123
Thomas Graff38a9eb2015-07-21 10:43:56 +0200124 if (dst->flags & DST_METADATA)
Paolo Abenid71785f2016-02-12 15:43:57 +0100125 metadata_dst_free((struct metadata_dst *)dst);
Thomas Graff38a9eb2015-07-21 10:43:56 +0200126 else
127 kmem_cache_free(dst->ops->kmem_cachep, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 dst = child;
Wei Wang52df1572017-06-17 10:42:38 -0700130 if (dst)
131 dst_release_immediate(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return NULL;
133}
laurent chavey598ed932010-03-29 10:41:36 +0000134EXPORT_SYMBOL(dst_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Eric Dumazetf8864972014-06-24 10:05:11 -0700136static void dst_destroy_rcu(struct rcu_head *head)
137{
138 struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
139
140 dst = dst_destroy(dst);
Eric Dumazetf8864972014-06-24 10:05:11 -0700141}
142
Wei Wang4a6ce2b2017-06-17 10:42:28 -0700143/* Operations to mark dst as DEAD and clean up the net device referenced
144 * by dst:
Miaohe Lin1be107d2020-09-10 04:41:53 -0400145 * 1. put the dst under blackhole interface and discard all tx/rx packets
Wei Wang4a6ce2b2017-06-17 10:42:28 -0700146 * on this route.
147 * 2. release the net_device
148 * This function should be called when removing routes from the fib tree
149 * in preparation for a NETDEV_DOWN/NETDEV_UNREGISTER event and also to
150 * make the next dst_ops->check() fail.
151 */
152void dst_dev_put(struct dst_entry *dst)
153{
154 struct net_device *dev = dst->dev;
155
156 dst->obsolete = DST_OBSOLETE_DEAD;
157 if (dst->ops->ifdown)
158 dst->ops->ifdown(dst, dev, true);
159 dst->input = dst_discard;
160 dst->output = dst_discard_out;
Mahesh Bandewar8d7017f2019-07-01 14:38:57 -0700161 dst->dev = blackhole_netdev;
Eric Dumazet9038c322021-12-04 20:22:03 -0800162 dev_replace_track(dev, blackhole_netdev, &dst->dev_tracker,
163 GFP_ATOMIC);
Wei Wang4a6ce2b2017-06-17 10:42:28 -0700164}
165EXPORT_SYMBOL(dst_dev_put);
166
Ilpo Järvinen8d330862008-03-27 17:53:31 -0700167void dst_release(struct dst_entry *dst)
168{
169 if (dst) {
laurent chavey598ed932010-03-29 10:41:36 +0000170 int newrefcnt;
Eric Dumazetef711cf2008-11-14 00:53:54 -0800171
laurent chavey598ed932010-03-29 10:41:36 +0000172 newrefcnt = atomic_dec_return(&dst->__refcnt);
Jason A. Donenfeldadecda52019-09-24 11:09:37 +0200173 if (WARN_ONCE(newrefcnt < 0, "dst_release underflow"))
Konstantin Khlebnikov8bf4ada2015-07-17 14:01:11 +0300174 net_warn_ratelimited("%s: dst:%p refcnt:%d\n",
175 __func__, dst, newrefcnt);
Wei Wangb2a9c0e2017-06-17 10:42:41 -0700176 if (!newrefcnt)
Eric Dumazetf8864972014-06-24 10:05:11 -0700177 call_rcu(&dst->rcu_head, dst_destroy_rcu);
Ilpo Järvinen8d330862008-03-27 17:53:31 -0700178 }
179}
180EXPORT_SYMBOL(dst_release);
181
Wei Wang5f56f402017-06-17 10:42:27 -0700182void dst_release_immediate(struct dst_entry *dst)
183{
184 if (dst) {
185 int newrefcnt;
186
187 newrefcnt = atomic_dec_return(&dst->__refcnt);
Jason A. Donenfeldadecda52019-09-24 11:09:37 +0200188 if (WARN_ONCE(newrefcnt < 0, "dst_release_immediate underflow"))
Wei Wang5f56f402017-06-17 10:42:27 -0700189 net_warn_ratelimited("%s: dst:%p refcnt:%d\n",
190 __func__, dst, newrefcnt);
191 if (!newrefcnt)
192 dst_destroy(dst);
193 }
194}
195EXPORT_SYMBOL(dst_release_immediate);
196
David S. Miller62fa8a82011-01-26 20:51:05 -0800197u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old)
198{
Eric Dumazet3fb07da2017-05-25 14:27:35 -0700199 struct dst_metrics *p = kmalloc(sizeof(*p), GFP_ATOMIC);
David S. Miller62fa8a82011-01-26 20:51:05 -0800200
201 if (p) {
Eric Dumazet3fb07da2017-05-25 14:27:35 -0700202 struct dst_metrics *old_p = (struct dst_metrics *)__DST_METRICS_PTR(old);
David S. Miller62fa8a82011-01-26 20:51:05 -0800203 unsigned long prev, new;
204
Eric Dumazet9620fef2017-08-18 12:08:07 -0700205 refcount_set(&p->refcnt, 1);
Eric Dumazet3fb07da2017-05-25 14:27:35 -0700206 memcpy(p->metrics, old_p->metrics, sizeof(p->metrics));
David S. Miller62fa8a82011-01-26 20:51:05 -0800207
208 new = (unsigned long) p;
209 prev = cmpxchg(&dst->_metrics, old, new);
210
211 if (prev != old) {
212 kfree(p);
Eric Dumazet3fb07da2017-05-25 14:27:35 -0700213 p = (struct dst_metrics *)__DST_METRICS_PTR(prev);
David S. Miller62fa8a82011-01-26 20:51:05 -0800214 if (prev & DST_METRICS_READ_ONLY)
215 p = NULL;
Eric Dumazet3fb07da2017-05-25 14:27:35 -0700216 } else if (prev & DST_METRICS_REFCOUNTED) {
Eric Dumazet9620fef2017-08-18 12:08:07 -0700217 if (refcount_dec_and_test(&old_p->refcnt))
Eric Dumazet3fb07da2017-05-25 14:27:35 -0700218 kfree(old_p);
David S. Miller62fa8a82011-01-26 20:51:05 -0800219 }
220 }
Eric Dumazet3fb07da2017-05-25 14:27:35 -0700221 BUILD_BUG_ON(offsetof(struct dst_metrics, metrics) != 0);
222 return (u32 *)p;
David S. Miller62fa8a82011-01-26 20:51:05 -0800223}
224EXPORT_SYMBOL(dst_cow_metrics_generic);
225
226/* Caller asserts that dst_metrics_read_only(dst) is false. */
227void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old)
228{
229 unsigned long prev, new;
230
Eric Dumazet3fb07da2017-05-25 14:27:35 -0700231 new = ((unsigned long) &dst_default_metrics) | DST_METRICS_READ_ONLY;
David S. Miller62fa8a82011-01-26 20:51:05 -0800232 prev = cmpxchg(&dst->_metrics, old, new);
233 if (prev == old)
234 kfree(__DST_METRICS_PTR(old));
235}
236EXPORT_SYMBOL(__dst_destroy_metrics_generic);
237
Daniel Borkmannc4c877b2021-03-10 01:38:09 +0100238struct dst_entry *dst_blackhole_check(struct dst_entry *dst, u32 cookie)
239{
240 return NULL;
241}
242
243u32 *dst_blackhole_cow_metrics(struct dst_entry *dst, unsigned long old)
244{
245 return NULL;
246}
247
248struct neighbour *dst_blackhole_neigh_lookup(const struct dst_entry *dst,
249 struct sk_buff *skb,
250 const void *daddr)
251{
252 return NULL;
253}
254
255void dst_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
256 struct sk_buff *skb, u32 mtu,
257 bool confirm_neigh)
258{
259}
260EXPORT_SYMBOL_GPL(dst_blackhole_update_pmtu);
261
262void dst_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
263 struct sk_buff *skb)
264{
265}
266EXPORT_SYMBOL_GPL(dst_blackhole_redirect);
267
268unsigned int dst_blackhole_mtu(const struct dst_entry *dst)
269{
270 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
271
272 return mtu ? : dst->dev->mtu;
273}
274EXPORT_SYMBOL_GPL(dst_blackhole_mtu);
275
Daniel Borkmanna188bb52021-03-10 01:38:10 +0100276static struct dst_ops dst_blackhole_ops = {
277 .family = AF_UNSPEC,
278 .neigh_lookup = dst_blackhole_neigh_lookup,
279 .check = dst_blackhole_check,
280 .cow_metrics = dst_blackhole_cow_metrics,
281 .update_pmtu = dst_blackhole_update_pmtu,
282 .redirect = dst_blackhole_redirect,
283 .mtu = dst_blackhole_mtu,
Thomas Graff38a9eb2015-07-21 10:43:56 +0200284};
285
Jakub Kicinski3fcece12017-06-23 22:11:58 +0200286static void __metadata_dst_init(struct metadata_dst *md_dst,
287 enum metadata_type type, u8 optslen)
Thomas Graff38a9eb2015-07-21 10:43:56 +0200288{
Thomas Graff38a9eb2015-07-21 10:43:56 +0200289 struct dst_entry *dst;
290
Thomas Graff38a9eb2015-07-21 10:43:56 +0200291 dst = &md_dst->dst;
Daniel Borkmanna188bb52021-03-10 01:38:10 +0100292 dst_init(dst, &dst_blackhole_ops, NULL, 1, DST_OBSOLETE_NONE,
Wei Wanga4c2fd72017-06-17 10:42:42 -0700293 DST_METADATA | DST_NOCOUNT);
Thomas Graff38a9eb2015-07-21 10:43:56 +0200294 memset(dst + 1, 0, sizeof(*md_dst) + optslen - sizeof(*dst));
Jakub Kicinski3fcece12017-06-23 22:11:58 +0200295 md_dst->type = type;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -0700296}
297
Jakub Kicinski3fcece12017-06-23 22:11:58 +0200298struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
299 gfp_t flags)
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -0700300{
301 struct metadata_dst *md_dst;
302
303 md_dst = kmalloc(sizeof(*md_dst) + optslen, flags);
304 if (!md_dst)
305 return NULL;
306
Jakub Kicinski3fcece12017-06-23 22:11:58 +0200307 __metadata_dst_init(md_dst, type, optslen);
Thomas Graff38a9eb2015-07-21 10:43:56 +0200308
309 return md_dst;
310}
311EXPORT_SYMBOL_GPL(metadata_dst_alloc);
312
Paolo Abenid71785f2016-02-12 15:43:57 +0100313void metadata_dst_free(struct metadata_dst *md_dst)
314{
315#ifdef CONFIG_DST_CACHE
David Lampartere65a4952017-08-18 14:31:35 +0200316 if (md_dst->type == METADATA_IP_TUNNEL)
317 dst_cache_destroy(&md_dst->u.tun_info.dst_cache);
Paolo Abenid71785f2016-02-12 15:43:57 +0100318#endif
319 kfree(md_dst);
320}
Pablo Neira Ayusoaf308b92018-08-02 20:51:39 +0200321EXPORT_SYMBOL_GPL(metadata_dst_free);
Paolo Abenid71785f2016-02-12 15:43:57 +0100322
Jakub Kicinski3fcece12017-06-23 22:11:58 +0200323struct metadata_dst __percpu *
324metadata_dst_alloc_percpu(u8 optslen, enum metadata_type type, gfp_t flags)
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -0700325{
326 int cpu;
327 struct metadata_dst __percpu *md_dst;
328
329 md_dst = __alloc_percpu_gfp(sizeof(struct metadata_dst) + optslen,
330 __alignof__(struct metadata_dst), flags);
331 if (!md_dst)
332 return NULL;
333
334 for_each_possible_cpu(cpu)
Jakub Kicinski3fcece12017-06-23 22:11:58 +0200335 __metadata_dst_init(per_cpu_ptr(md_dst, cpu), type, optslen);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -0700336
337 return md_dst;
338}
339EXPORT_SYMBOL_GPL(metadata_dst_alloc_percpu);
Jakub Kicinskid66f2b92017-10-09 10:30:14 -0700340
341void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst)
342{
Jakub Kicinski833e0e22017-10-10 15:05:39 -0700343#ifdef CONFIG_DST_CACHE
Jakub Kicinskid66f2b92017-10-09 10:30:14 -0700344 int cpu;
345
Jakub Kicinskid66f2b92017-10-09 10:30:14 -0700346 for_each_possible_cpu(cpu) {
347 struct metadata_dst *one_md_dst = per_cpu_ptr(md_dst, cpu);
348
349 if (one_md_dst->type == METADATA_IP_TUNNEL)
350 dst_cache_destroy(&one_md_dst->u.tun_info.dst_cache);
351 }
352#endif
353 free_percpu(md_dst);
354}
355EXPORT_SYMBOL_GPL(metadata_dst_free_percpu);