1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * net/dst.h	Protocol independent destination cache definitions.
4  *
5  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6  *
7  */
8 
9 #ifndef _NET_DST_H
10 #define _NET_DST_H
11 
12 #include <net/dst_ops.h>
13 #include <linux/netdevice.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/rcupdate.h>
16 #include <linux/bug.h>
17 #include <linux/jiffies.h>
18 #include <linux/refcount.h>
19 #include <net/neighbour.h>
20 #include <asm/processor.h>
21 
22 #define DST_GC_MIN	(HZ/10)
23 #define DST_GC_INC	(HZ/2)
24 #define DST_GC_MAX	(120*HZ)
25 
26 /* Each dst_entry has reference count and sits in some parent list(s).
27  * When it is removed from parent list, it is "freed" (dst_free).
28  * After this it enters dead state (dst->obsolete > 0) and if its refcnt
29  * is zero, it can be destroyed immediately, otherwise it is added
30  * to gc list and garbage collector periodically checks the refcnt.
31  */
32 
33 struct sk_buff;
34 
35 struct dst_entry {
36 	struct net_device       *dev;
37 	struct  dst_ops	        *ops;
38 	unsigned long		_metrics;
39 	unsigned long           expires;
40 #ifdef CONFIG_XFRM
41 	struct xfrm_state	*xfrm;
42 #else
43 	void			*__pad1;
44 #endif
45 	int			(*input)(struct sk_buff *);
46 	int			(*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
47 
48 	unsigned short		flags;
49 #define DST_HOST		0x0001
50 #define DST_NOXFRM		0x0002
51 #define DST_NOPOLICY		0x0004
52 #define DST_NOCOUNT		0x0008
53 #define DST_FAKE_RTABLE		0x0010
54 #define DST_XFRM_TUNNEL		0x0020
55 #define DST_XFRM_QUEUE		0x0040
56 #define DST_METADATA		0x0080
57 
58 	/* A non-zero value of dst->obsolete forces by-hand validation
59 	 * of the route entry.  Positive values are set by the generic
60 	 * dst layer to indicate that the entry has been forcefully
61 	 * destroyed.
62 	 *
63 	 * Negative values are used by the implementation layer code to
64 	 * force invocation of the dst_ops->check() method.
65 	 */
66 	short			obsolete;
67 #define DST_OBSOLETE_NONE	0
68 #define DST_OBSOLETE_DEAD	2
69 #define DST_OBSOLETE_FORCE_CHK	-1
70 #define DST_OBSOLETE_KILL	-2
71 	unsigned short		header_len;	/* more space at head required */
72 	unsigned short		trailer_len;	/* space to reserve at tail */
73 
74 	/*
75 	 * __refcnt wants to be on a different cache line from
76 	 * input/output/ops or performance tanks badly
77 	 */
78 #ifdef CONFIG_64BIT
79 	atomic_t		__refcnt;	/* 64-bit offset 64 */
80 #endif
81 	int			__use;
82 	unsigned long		lastuse;
83 	struct lwtunnel_state   *lwtstate;
84 	struct rcu_head		rcu_head;
85 	short			error;
86 	short			__pad;
87 	__u32			tclassid;
88 #ifndef CONFIG_64BIT
89 	atomic_t		__refcnt;	/* 32-bit offset 64 */
90 #endif
91 };
92 
93 struct dst_metrics {
94 	u32		metrics[RTAX_MAX];
95 	refcount_t	refcnt;
96 } __aligned(4);		/* Low pointer bits contain DST_METRICS_FLAGS */
97 extern const struct dst_metrics dst_default_metrics;
98 
99 u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
100 
101 #define DST_METRICS_READ_ONLY		0x1UL
102 #define DST_METRICS_REFCOUNTED		0x2UL
103 #define DST_METRICS_FLAGS		0x3UL
104 #define __DST_METRICS_PTR(Y)	\
105 	((u32 *)((Y) & ~DST_METRICS_FLAGS))
106 #define DST_METRICS_PTR(X)	__DST_METRICS_PTR((X)->_metrics)
107 
dst_metrics_read_only(const struct dst_entry * dst)108 static inline bool dst_metrics_read_only(const struct dst_entry *dst)
109 {
110 	return dst->_metrics & DST_METRICS_READ_ONLY;
111 }
112 
113 void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old);
114 
dst_destroy_metrics_generic(struct dst_entry * dst)115 static inline void dst_destroy_metrics_generic(struct dst_entry *dst)
116 {
117 	unsigned long val = dst->_metrics;
118 	if (!(val & DST_METRICS_READ_ONLY))
119 		__dst_destroy_metrics_generic(dst, val);
120 }
121 
dst_metrics_write_ptr(struct dst_entry * dst)122 static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst)
123 {
124 	unsigned long p = dst->_metrics;
125 
126 	BUG_ON(!p);
127 
128 	if (p & DST_METRICS_READ_ONLY)
129 		return dst->ops->cow_metrics(dst, p);
130 	return __DST_METRICS_PTR(p);
131 }
132 
133 /* This may only be invoked before the entry has reached global
134  * visibility.
135  */
dst_init_metrics(struct dst_entry * dst,const u32 * src_metrics,bool read_only)136 static inline void dst_init_metrics(struct dst_entry *dst,
137 				    const u32 *src_metrics,
138 				    bool read_only)
139 {
140 	dst->_metrics = ((unsigned long) src_metrics) |
141 		(read_only ? DST_METRICS_READ_ONLY : 0);
142 }
143 
dst_copy_metrics(struct dst_entry * dest,const struct dst_entry * src)144 static inline void dst_copy_metrics(struct dst_entry *dest, const struct dst_entry *src)
145 {
146 	u32 *dst_metrics = dst_metrics_write_ptr(dest);
147 
148 	if (dst_metrics) {
149 		u32 *src_metrics = DST_METRICS_PTR(src);
150 
151 		memcpy(dst_metrics, src_metrics, RTAX_MAX * sizeof(u32));
152 	}
153 }
154 
dst_metrics_ptr(struct dst_entry * dst)155 static inline u32 *dst_metrics_ptr(struct dst_entry *dst)
156 {
157 	return DST_METRICS_PTR(dst);
158 }
159 
160 static inline u32
dst_metric_raw(const struct dst_entry * dst,const int metric)161 dst_metric_raw(const struct dst_entry *dst, const int metric)
162 {
163 	u32 *p = DST_METRICS_PTR(dst);
164 
165 	return p[metric-1];
166 }
167 
168 static inline u32
dst_metric(const struct dst_entry * dst,const int metric)169 dst_metric(const struct dst_entry *dst, const int metric)
170 {
171 	WARN_ON_ONCE(metric == RTAX_HOPLIMIT ||
172 		     metric == RTAX_ADVMSS ||
173 		     metric == RTAX_MTU);
174 	return dst_metric_raw(dst, metric);
175 }
176 
177 static inline u32
dst_metric_advmss(const struct dst_entry * dst)178 dst_metric_advmss(const struct dst_entry *dst)
179 {
180 	u32 advmss = dst_metric_raw(dst, RTAX_ADVMSS);
181 
182 	if (!advmss)
183 		advmss = dst->ops->default_advmss(dst);
184 
185 	return advmss;
186 }
187 
dst_metric_set(struct dst_entry * dst,int metric,u32 val)188 static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
189 {
190 	u32 *p = dst_metrics_write_ptr(dst);
191 
192 	if (p)
193 		p[metric-1] = val;
194 }
195 
196 /* Kernel-internal feature bits that are unallocated in user space. */
197 #define DST_FEATURE_ECN_CA	(1 << 31)
198 
199 #define DST_FEATURE_MASK	(DST_FEATURE_ECN_CA)
200 #define DST_FEATURE_ECN_MASK	(DST_FEATURE_ECN_CA | RTAX_FEATURE_ECN)
201 
202 static inline u32
dst_feature(const struct dst_entry * dst,u32 feature)203 dst_feature(const struct dst_entry *dst, u32 feature)
204 {
205 	return dst_metric(dst, RTAX_FEATURES) & feature;
206 }
207 
dst_mtu(const struct dst_entry * dst)208 static inline u32 dst_mtu(const struct dst_entry *dst)
209 {
210 	return dst->ops->mtu(dst);
211 }
212 
213 /* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
dst_metric_rtt(const struct dst_entry * dst,int metric)214 static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric)
215 {
216 	return msecs_to_jiffies(dst_metric(dst, metric));
217 }
218 
219 static inline u32
dst_allfrag(const struct dst_entry * dst)220 dst_allfrag(const struct dst_entry *dst)
221 {
222 	int ret = dst_feature(dst,  RTAX_FEATURE_ALLFRAG);
223 	return ret;
224 }
225 
226 static inline int
dst_metric_locked(const struct dst_entry * dst,int metric)227 dst_metric_locked(const struct dst_entry *dst, int metric)
228 {
229 	return dst_metric(dst, RTAX_LOCK) & (1<<metric);
230 }
231 
dst_hold(struct dst_entry * dst)232 static inline void dst_hold(struct dst_entry *dst)
233 {
234 	/*
235 	 * If your kernel compilation stops here, please check
236 	 * the placement of __refcnt in struct dst_entry
237 	 */
238 	BUILD_BUG_ON(offsetof(struct dst_entry, __refcnt) & 63);
239 	WARN_ON(atomic_inc_not_zero(&dst->__refcnt) == 0);
240 }
241 
dst_use_noref(struct dst_entry * dst,unsigned long time)242 static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
243 {
244 	if (unlikely(time != dst->lastuse)) {
245 		dst->__use++;
246 		dst->lastuse = time;
247 	}
248 }
249 
dst_clone(struct dst_entry * dst)250 static inline struct dst_entry *dst_clone(struct dst_entry *dst)
251 {
252 	if (dst)
253 		dst_hold(dst);
254 	return dst;
255 }
256 
257 void dst_release(struct dst_entry *dst);
258 
259 void dst_release_immediate(struct dst_entry *dst);
260 
refdst_drop(unsigned long refdst)261 static inline void refdst_drop(unsigned long refdst)
262 {
263 	if (!(refdst & SKB_DST_NOREF))
264 		dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
265 }
266 
267 /**
268  * skb_dst_drop - drops skb dst
269  * @skb: buffer
270  *
271  * Drops dst reference count if a reference was taken.
272  */
skb_dst_drop(struct sk_buff * skb)273 static inline void skb_dst_drop(struct sk_buff *skb)
274 {
275 	if (skb->_skb_refdst) {
276 		refdst_drop(skb->_skb_refdst);
277 		skb->_skb_refdst = 0UL;
278 	}
279 }
280 
__skb_dst_copy(struct sk_buff * nskb,unsigned long refdst)281 static inline void __skb_dst_copy(struct sk_buff *nskb, unsigned long refdst)
282 {
283 	nskb->_skb_refdst = refdst;
284 	if (!(nskb->_skb_refdst & SKB_DST_NOREF))
285 		dst_clone(skb_dst(nskb));
286 }
287 
skb_dst_copy(struct sk_buff * nskb,const struct sk_buff * oskb)288 static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
289 {
290 	__skb_dst_copy(nskb, oskb->_skb_refdst);
291 }
292 
293 /**
294  * dst_hold_safe - Take a reference on a dst if possible
295  * @dst: pointer to dst entry
296  *
297  * This helper returns false if it could not safely
298  * take a reference on a dst.
299  */
dst_hold_safe(struct dst_entry * dst)300 static inline bool dst_hold_safe(struct dst_entry *dst)
301 {
302 	return atomic_inc_not_zero(&dst->__refcnt);
303 }
304 
305 /**
306  * skb_dst_force - makes sure skb dst is refcounted
307  * @skb: buffer
308  *
309  * If dst is not yet refcounted and not destroyed, grab a ref on it.
310  * Returns true if dst is refcounted.
311  */
skb_dst_force(struct sk_buff * skb)312 static inline bool skb_dst_force(struct sk_buff *skb)
313 {
314 	if (skb_dst_is_noref(skb)) {
315 		struct dst_entry *dst = skb_dst(skb);
316 
317 		WARN_ON(!rcu_read_lock_held());
318 		if (!dst_hold_safe(dst))
319 			dst = NULL;
320 
321 		skb->_skb_refdst = (unsigned long)dst;
322 	}
323 
324 	return skb->_skb_refdst != 0UL;
325 }
326 
327 
328 /**
329  *	__skb_tunnel_rx - prepare skb for rx reinsert
330  *	@skb: buffer
331  *	@dev: tunnel device
332  *	@net: netns for packet i/o
333  *
334  *	After decapsulation, packet is going to re-enter (netif_rx()) our stack,
335  *	so make some cleanups. (no accounting done)
336  */
__skb_tunnel_rx(struct sk_buff * skb,struct net_device * dev,struct net * net)337 static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
338 				   struct net *net)
339 {
340 	skb->dev = dev;
341 
342 	/*
343 	 * Clear hash so that we can recalulate the hash for the
344 	 * encapsulated packet, unless we have already determine the hash
345 	 * over the L4 4-tuple.
346 	 */
347 	skb_clear_hash_if_not_l4(skb);
348 	skb_set_queue_mapping(skb, 0);
349 	skb_scrub_packet(skb, !net_eq(net, dev_net(dev)));
350 }
351 
352 /**
353  *	skb_tunnel_rx - prepare skb for rx reinsert
354  *	@skb: buffer
355  *	@dev: tunnel device
356  *	@net: netns for packet i/o
357  *
358  *	After decapsulation, packet is going to re-enter (netif_rx()) our stack,
359  *	so make some cleanups, and perform accounting.
360  *	Note: this accounting is not SMP safe.
361  */
skb_tunnel_rx(struct sk_buff * skb,struct net_device * dev,struct net * net)362 static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
363 				 struct net *net)
364 {
365 	DEV_STATS_INC(dev, rx_packets);
366 	DEV_STATS_ADD(dev, rx_bytes, skb->len);
367 	__skb_tunnel_rx(skb, dev, net);
368 }
369 
dst_tclassid(const struct sk_buff * skb)370 static inline u32 dst_tclassid(const struct sk_buff *skb)
371 {
372 #ifdef CONFIG_IP_ROUTE_CLASSID
373 	const struct dst_entry *dst;
374 
375 	dst = skb_dst(skb);
376 	if (dst)
377 		return dst->tclassid;
378 #endif
379 	return 0;
380 }
381 
382 int dst_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
dst_discard(struct sk_buff * skb)383 static inline int dst_discard(struct sk_buff *skb)
384 {
385 	return dst_discard_out(&init_net, skb->sk, skb);
386 }
387 void *dst_alloc(struct dst_ops *ops, struct net_device *dev, int initial_ref,
388 		int initial_obsolete, unsigned short flags);
389 void dst_init(struct dst_entry *dst, struct dst_ops *ops,
390 	      struct net_device *dev, int initial_ref, int initial_obsolete,
391 	      unsigned short flags);
392 struct dst_entry *dst_destroy(struct dst_entry *dst);
393 void dst_dev_put(struct dst_entry *dst);
394 
dst_confirm(struct dst_entry * dst)395 static inline void dst_confirm(struct dst_entry *dst)
396 {
397 }
398 
dst_neigh_lookup(const struct dst_entry * dst,const void * daddr)399 static inline struct neighbour *dst_neigh_lookup(const struct dst_entry *dst, const void *daddr)
400 {
401 	struct neighbour *n = dst->ops->neigh_lookup(dst, NULL, daddr);
402 	return IS_ERR(n) ? NULL : n;
403 }
404 
dst_neigh_lookup_skb(const struct dst_entry * dst,struct sk_buff * skb)405 static inline struct neighbour *dst_neigh_lookup_skb(const struct dst_entry *dst,
406 						     struct sk_buff *skb)
407 {
408 	struct neighbour *n = NULL;
409 
410 	/* The packets from tunnel devices (eg bareudp) may have only
411 	 * metadata in the dst pointer of skb. Hence a pointer check of
412 	 * neigh_lookup is needed.
413 	 */
414 	if (dst->ops->neigh_lookup)
415 		n = dst->ops->neigh_lookup(dst, skb, NULL);
416 
417 	return IS_ERR(n) ? NULL : n;
418 }
419 
dst_confirm_neigh(const struct dst_entry * dst,const void * daddr)420 static inline void dst_confirm_neigh(const struct dst_entry *dst,
421 				     const void *daddr)
422 {
423 	if (dst->ops->confirm_neigh)
424 		dst->ops->confirm_neigh(dst, daddr);
425 }
426 
dst_link_failure(struct sk_buff * skb)427 static inline void dst_link_failure(struct sk_buff *skb)
428 {
429 	struct dst_entry *dst = skb_dst(skb);
430 	if (dst && dst->ops && dst->ops->link_failure)
431 		dst->ops->link_failure(skb);
432 }
433 
dst_set_expires(struct dst_entry * dst,int timeout)434 static inline void dst_set_expires(struct dst_entry *dst, int timeout)
435 {
436 	unsigned long expires = jiffies + timeout;
437 
438 	if (expires == 0)
439 		expires = 1;
440 
441 	if (dst->expires == 0 || time_before(expires, dst->expires))
442 		dst->expires = expires;
443 }
444 
445 /* Output packet to network from transport.  */
dst_output(struct net * net,struct sock * sk,struct sk_buff * skb)446 static inline int dst_output(struct net *net, struct sock *sk, struct sk_buff *skb)
447 {
448 	return skb_dst(skb)->output(net, sk, skb);
449 }
450 
451 /* Input packet from network to transport.  */
dst_input(struct sk_buff * skb)452 static inline int dst_input(struct sk_buff *skb)
453 {
454 	return skb_dst(skb)->input(skb);
455 }
456 
dst_check(struct dst_entry * dst,u32 cookie)457 static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
458 {
459 	if (dst->obsolete)
460 		dst = dst->ops->check(dst, cookie);
461 	return dst;
462 }
463 
464 /* Flags for xfrm_lookup flags argument. */
465 enum {
466 	XFRM_LOOKUP_ICMP = 1 << 0,
467 	XFRM_LOOKUP_QUEUE = 1 << 1,
468 	XFRM_LOOKUP_KEEP_DST_REF = 1 << 2,
469 };
470 
471 struct flowi;
472 #ifndef CONFIG_XFRM
xfrm_lookup(struct net * net,struct dst_entry * dst_orig,const struct flowi * fl,const struct sock * sk,int flags)473 static inline struct dst_entry *xfrm_lookup(struct net *net,
474 					    struct dst_entry *dst_orig,
475 					    const struct flowi *fl,
476 					    const struct sock *sk,
477 					    int flags)
478 {
479 	return dst_orig;
480 }
481 
482 static inline struct dst_entry *
xfrm_lookup_with_ifid(struct net * net,struct dst_entry * dst_orig,const struct flowi * fl,const struct sock * sk,int flags,u32 if_id)483 xfrm_lookup_with_ifid(struct net *net, struct dst_entry *dst_orig,
484 		      const struct flowi *fl, const struct sock *sk,
485 		      int flags, u32 if_id)
486 {
487 	return dst_orig;
488 }
489 
xfrm_lookup_route(struct net * net,struct dst_entry * dst_orig,const struct flowi * fl,const struct sock * sk,int flags)490 static inline struct dst_entry *xfrm_lookup_route(struct net *net,
491 						  struct dst_entry *dst_orig,
492 						  const struct flowi *fl,
493 						  const struct sock *sk,
494 						  int flags)
495 {
496 	return dst_orig;
497 }
498 
dst_xfrm(const struct dst_entry * dst)499 static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
500 {
501 	return NULL;
502 }
503 
504 #else
505 struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
506 			      const struct flowi *fl, const struct sock *sk,
507 			      int flags);
508 
509 struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
510 					struct dst_entry *dst_orig,
511 					const struct flowi *fl,
512 					const struct sock *sk, int flags,
513 					u32 if_id);
514 
515 struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
516 				    const struct flowi *fl, const struct sock *sk,
517 				    int flags);
518 
519 /* skb attached with this dst needs transformation if dst->xfrm is valid */
dst_xfrm(const struct dst_entry * dst)520 static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
521 {
522 	return dst->xfrm;
523 }
524 #endif
525 
skb_dst_update_pmtu(struct sk_buff * skb,u32 mtu)526 static inline void skb_dst_update_pmtu(struct sk_buff *skb, u32 mtu)
527 {
528 	struct dst_entry *dst = skb_dst(skb);
529 
530 	if (dst && dst->ops->update_pmtu)
531 		dst->ops->update_pmtu(dst, NULL, skb, mtu, true);
532 }
533 
534 /* update dst pmtu but not do neighbor confirm */
skb_dst_update_pmtu_no_confirm(struct sk_buff * skb,u32 mtu)535 static inline void skb_dst_update_pmtu_no_confirm(struct sk_buff *skb, u32 mtu)
536 {
537 	struct dst_entry *dst = skb_dst(skb);
538 
539 	if (dst && dst->ops->update_pmtu)
540 		dst->ops->update_pmtu(dst, NULL, skb, mtu, false);
541 }
542 
skb_tunnel_check_pmtu(struct sk_buff * skb,struct dst_entry * encap_dst,int headroom)543 static inline void skb_tunnel_check_pmtu(struct sk_buff *skb,
544 					 struct dst_entry *encap_dst,
545 					 int headroom)
546 {
547 	u32 encap_mtu = dst_mtu(encap_dst);
548 
549 	if (skb->len > encap_mtu - headroom)
550 		skb_dst_update_pmtu_no_confirm(skb, encap_mtu - headroom);
551 }
552 
553 #endif /* _NET_DST_H */
554