xref: /linux/include/net/dst.h (revision 9410645520e9b820069761f3450ef6661418e279)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * net/dst.h	Protocol independent destination cache definitions.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #ifndef _NET_DST_H
101da177e4SLinus Torvalds #define _NET_DST_H
111da177e4SLinus Torvalds 
1286393e52SAlexey Dobriyan #include <net/dst_ops.h>
1314c85021SArnaldo Carvalho de Melo #include <linux/netdevice.h>
141da177e4SLinus Torvalds #include <linux/rtnetlink.h>
151da177e4SLinus Torvalds #include <linux/rcupdate.h>
16187f1882SPaul Gortmaker #include <linux/bug.h>
171da177e4SLinus Torvalds #include <linux/jiffies.h>
189620fef2SEric Dumazet #include <linux/refcount.h>
19bc9d3a9fSThomas Gleixner #include <linux/rcuref.h>
201da177e4SLinus Torvalds #include <net/neighbour.h>
211da177e4SLinus Torvalds #include <asm/processor.h>
22e43b2190SBrian Vazquez #include <linux/indirect_call_wrapper.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds struct sk_buff;
251da177e4SLinus Torvalds 
26fd2c3ef7SEric Dumazet struct dst_entry {
2766727145SAlexey Dobriyan 	struct net_device       *dev;
2862fa8a84SDavid S. Miller 	struct  dst_ops	        *ops;
2962fa8a84SDavid S. Miller 	unsigned long		_metrics;
301da177e4SLinus Torvalds 	unsigned long           expires;
31def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
321da177e4SLinus Torvalds 	struct xfrm_state	*xfrm;
335635c10dSEric Dumazet #else
345635c10dSEric Dumazet 	void			*__pad1;
35def8b4faSAlexey Dobriyan #endif
361da177e4SLinus Torvalds 	int			(*input)(struct sk_buff *);
37ede2059dSEric W. Biederman 	int			(*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
381da177e4SLinus Torvalds 
395110effeSDavid S. Miller 	unsigned short		flags;
40f6b72b62SDavid S. Miller #define DST_NOXFRM		0x0002
41f6b72b62SDavid S. Miller #define DST_NOPOLICY		0x0004
421eb04e7cSWei Wang #define DST_NOCOUNT		0x0008
431eb04e7cSWei Wang #define DST_FAKE_RTABLE		0x0010
441eb04e7cSWei Wang #define DST_XFRM_TUNNEL		0x0020
451eb04e7cSWei Wang #define DST_XFRM_QUEUE		0x0040
461eb04e7cSWei Wang #define DST_METADATA		0x0080
47f6b72b62SDavid S. Miller 
48f5b0a874SDavid S. Miller 	/* A non-zero value of dst->obsolete forces by-hand validation
49f5b0a874SDavid S. Miller 	 * of the route entry.  Positive values are set by the generic
50f5b0a874SDavid S. Miller 	 * dst layer to indicate that the entry has been forcefully
51f5b0a874SDavid S. Miller 	 * destroyed.
52f5b0a874SDavid S. Miller 	 *
53f5b0a874SDavid S. Miller 	 * Negative values are used by the implementation layer code to
54f5b0a874SDavid S. Miller 	 * force invocation of the dst_ops->check() method.
55f5b0a874SDavid S. Miller 	 */
5662fa8a84SDavid S. Miller 	short			obsolete;
57f5b0a874SDavid S. Miller #define DST_OBSOLETE_NONE	0
58f5b0a874SDavid S. Miller #define DST_OBSOLETE_DEAD	2
59f5b0a874SDavid S. Miller #define DST_OBSOLETE_FORCE_CHK	-1
60ceb33206SDavid S. Miller #define DST_OBSOLETE_KILL	-2
6162fa8a84SDavid S. Miller 	unsigned short		header_len;	/* more space at head required */
6262fa8a84SDavid S. Miller 	unsigned short		trailer_len;	/* space to reserve at tail */
6351ce8bd4SJulian Anastasov 
64f1dd9c37SZhang Yanmin 	/*
65bc9d3a9fSThomas Gleixner 	 * __rcuref wants to be on a different cache line from
66f1dd9c37SZhang Yanmin 	 * input/output/ops or performance tanks badly
67f1dd9c37SZhang Yanmin 	 */
688b207e73SDavid Miller #ifdef CONFIG_64BIT
69bc9d3a9fSThomas Gleixner 	rcuref_t		__rcuref;	/* 64-bit offset 64 */
708b207e73SDavid Miller #endif
711e19e02cSEric Dumazet 	int			__use;
72f1dd9c37SZhang Yanmin 	unsigned long		lastuse;
738b207e73SDavid Miller 	struct rcu_head		rcu_head;
748b207e73SDavid Miller 	short			error;
758b207e73SDavid Miller 	short			__pad;
768b207e73SDavid Miller 	__u32			tclassid;
778b207e73SDavid Miller #ifndef CONFIG_64BIT
78d288a162SWangyang Guo 	struct lwtunnel_state   *lwtstate;
79bc9d3a9fSThomas Gleixner 	rcuref_t		__rcuref;	/* 32-bit offset 64 */
808b207e73SDavid Miller #endif
819038c320SEric Dumazet 	netdevice_tracker	dev_tracker;
82d288a162SWangyang Guo 
83d288a162SWangyang Guo 	/*
84d288a162SWangyang Guo 	 * Used by rtable and rt6_info. Moves lwtstate into the next cache
85d288a162SWangyang Guo 	 * line on 64bit so that lwtstate does not cause false sharing with
86bc9d3a9fSThomas Gleixner 	 * __rcuref under contention of __rcuref. This also puts the
87d288a162SWangyang Guo 	 * frequently accessed members of rtable and rt6_info out of the
88bc9d3a9fSThomas Gleixner 	 * __rcuref cache line.
89d288a162SWangyang Guo 	 */
90d288a162SWangyang Guo 	struct list_head	rt_uncached;
91d288a162SWangyang Guo 	struct uncached_list	*rt_uncached_list;
92d288a162SWangyang Guo #ifdef CONFIG_64BIT
93d288a162SWangyang Guo 	struct lwtunnel_state   *lwtstate;
94d288a162SWangyang Guo #endif
951da177e4SLinus Torvalds };
961da177e4SLinus Torvalds 
973fb07dafSEric Dumazet struct dst_metrics {
983fb07dafSEric Dumazet 	u32		metrics[RTAX_MAX];
999620fef2SEric Dumazet 	refcount_t	refcnt;
100258a980dSGeert Uytterhoeven } __aligned(4);		/* Low pointer bits contain DST_METRICS_FLAGS */
1013fb07dafSEric Dumazet extern const struct dst_metrics dst_default_metrics;
1023fb07dafSEric Dumazet 
103a4023dd0SJoe Perches u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
10462fa8a84SDavid S. Miller 
10562fa8a84SDavid S. Miller #define DST_METRICS_READ_ONLY		0x1UL
1063fb07dafSEric Dumazet #define DST_METRICS_REFCOUNTED		0x2UL
107e5fd387aSMichal Kubeček #define DST_METRICS_FLAGS		0x3UL
10862fa8a84SDavid S. Miller #define __DST_METRICS_PTR(Y)	\
109e5fd387aSMichal Kubeček 	((u32 *)((Y) & ~DST_METRICS_FLAGS))
11062fa8a84SDavid S. Miller #define DST_METRICS_PTR(X)	__DST_METRICS_PTR((X)->_metrics)
11162fa8a84SDavid S. Miller 
dst_metrics_read_only(const struct dst_entry * dst)11262fa8a84SDavid S. Miller static inline bool dst_metrics_read_only(const struct dst_entry *dst)
11362fa8a84SDavid S. Miller {
11462fa8a84SDavid S. Miller 	return dst->_metrics & DST_METRICS_READ_ONLY;
11562fa8a84SDavid S. Miller }
11662fa8a84SDavid S. Miller 
117a4023dd0SJoe Perches void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old);
11862fa8a84SDavid S. Miller 
dst_destroy_metrics_generic(struct dst_entry * dst)11962fa8a84SDavid S. Miller static inline void dst_destroy_metrics_generic(struct dst_entry *dst)
12062fa8a84SDavid S. Miller {
12162fa8a84SDavid S. Miller 	unsigned long val = dst->_metrics;
12262fa8a84SDavid S. Miller 	if (!(val & DST_METRICS_READ_ONLY))
12362fa8a84SDavid S. Miller 		__dst_destroy_metrics_generic(dst, val);
12462fa8a84SDavid S. Miller }
12562fa8a84SDavid S. Miller 
dst_metrics_write_ptr(struct dst_entry * dst)12662fa8a84SDavid S. Miller static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst)
12762fa8a84SDavid S. Miller {
12862fa8a84SDavid S. Miller 	unsigned long p = dst->_metrics;
12962fa8a84SDavid S. Miller 
1301f37070dSStephen Hemminger 	BUG_ON(!p);
1311f37070dSStephen Hemminger 
13262fa8a84SDavid S. Miller 	if (p & DST_METRICS_READ_ONLY)
13362fa8a84SDavid S. Miller 		return dst->ops->cow_metrics(dst, p);
13462fa8a84SDavid S. Miller 	return __DST_METRICS_PTR(p);
13562fa8a84SDavid S. Miller }
13662fa8a84SDavid S. Miller 
13762fa8a84SDavid S. Miller /* This may only be invoked before the entry has reached global
13862fa8a84SDavid S. Miller  * visibility.
13962fa8a84SDavid S. Miller  */
dst_init_metrics(struct dst_entry * dst,const u32 * src_metrics,bool read_only)14062fa8a84SDavid S. Miller static inline void dst_init_metrics(struct dst_entry *dst,
14162fa8a84SDavid S. Miller 				    const u32 *src_metrics,
14262fa8a84SDavid S. Miller 				    bool read_only)
14362fa8a84SDavid S. Miller {
14462fa8a84SDavid S. Miller 	dst->_metrics = ((unsigned long) src_metrics) |
14562fa8a84SDavid S. Miller 		(read_only ? DST_METRICS_READ_ONLY : 0);
14662fa8a84SDavid S. Miller }
14762fa8a84SDavid S. Miller 
dst_copy_metrics(struct dst_entry * dest,const struct dst_entry * src)14862fa8a84SDavid S. Miller static inline void dst_copy_metrics(struct dst_entry *dest, const struct dst_entry *src)
14962fa8a84SDavid S. Miller {
15062fa8a84SDavid S. Miller 	u32 *dst_metrics = dst_metrics_write_ptr(dest);
15162fa8a84SDavid S. Miller 
15262fa8a84SDavid S. Miller 	if (dst_metrics) {
15362fa8a84SDavid S. Miller 		u32 *src_metrics = DST_METRICS_PTR(src);
15462fa8a84SDavid S. Miller 
15562fa8a84SDavid S. Miller 		memcpy(dst_metrics, src_metrics, RTAX_MAX * sizeof(u32));
15662fa8a84SDavid S. Miller 	}
15762fa8a84SDavid S. Miller }
15862fa8a84SDavid S. Miller 
dst_metrics_ptr(struct dst_entry * dst)15962fa8a84SDavid S. Miller static inline u32 *dst_metrics_ptr(struct dst_entry *dst)
16062fa8a84SDavid S. Miller {
16162fa8a84SDavid S. Miller 	return DST_METRICS_PTR(dst);
16262fa8a84SDavid S. Miller }
16362fa8a84SDavid S. Miller 
1641da177e4SLinus Torvalds static inline u32
dst_metric_raw(const struct dst_entry * dst,const int metric)1655170ae82SDavid S. Miller dst_metric_raw(const struct dst_entry *dst, const int metric)
1661da177e4SLinus Torvalds {
16762fa8a84SDavid S. Miller 	u32 *p = DST_METRICS_PTR(dst);
16862fa8a84SDavid S. Miller 
16962fa8a84SDavid S. Miller 	return p[metric-1];
170defb3519SDavid S. Miller }
171defb3519SDavid S. Miller 
1725170ae82SDavid S. Miller static inline u32
dst_metric(const struct dst_entry * dst,const int metric)1735170ae82SDavid S. Miller dst_metric(const struct dst_entry *dst, const int metric)
1745170ae82SDavid S. Miller {
1750dbaee3bSDavid S. Miller 	WARN_ON_ONCE(metric == RTAX_HOPLIMIT ||
176d33e4553SDavid S. Miller 		     metric == RTAX_ADVMSS ||
177d33e4553SDavid S. Miller 		     metric == RTAX_MTU);
1785170ae82SDavid S. Miller 	return dst_metric_raw(dst, metric);
1795170ae82SDavid S. Miller }
1805170ae82SDavid S. Miller 
1810dbaee3bSDavid S. Miller static inline u32
dst_metric_advmss(const struct dst_entry * dst)1820dbaee3bSDavid S. Miller dst_metric_advmss(const struct dst_entry *dst)
1830dbaee3bSDavid S. Miller {
1840dbaee3bSDavid S. Miller 	u32 advmss = dst_metric_raw(dst, RTAX_ADVMSS);
1850dbaee3bSDavid S. Miller 
1860dbaee3bSDavid S. Miller 	if (!advmss)
1870dbaee3bSDavid S. Miller 		advmss = dst->ops->default_advmss(dst);
1880dbaee3bSDavid S. Miller 
1890dbaee3bSDavid S. Miller 	return advmss;
1900dbaee3bSDavid S. Miller }
1910dbaee3bSDavid S. Miller 
dst_metric_set(struct dst_entry * dst,int metric,u32 val)192defb3519SDavid S. Miller static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
193defb3519SDavid S. Miller {
19462fa8a84SDavid S. Miller 	u32 *p = dst_metrics_write_ptr(dst);
195defb3519SDavid S. Miller 
19662fa8a84SDavid S. Miller 	if (p)
19762fa8a84SDavid S. Miller 		p[metric-1] = val;
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
200c3a8d947SDaniel Borkmann /* Kernel-internal feature bits that are unallocated in user space. */
20140f6a2cbSVandana BN #define DST_FEATURE_ECN_CA	(1U << 31)
202c3a8d947SDaniel Borkmann 
203c3a8d947SDaniel Borkmann #define DST_FEATURE_MASK	(DST_FEATURE_ECN_CA)
204c3a8d947SDaniel Borkmann #define DST_FEATURE_ECN_MASK	(DST_FEATURE_ECN_CA | RTAX_FEATURE_ECN)
205c3a8d947SDaniel Borkmann 
2060c3adfb8SGilad Ben-Yossef static inline u32
dst_feature(const struct dst_entry * dst,u32 feature)2070c3adfb8SGilad Ben-Yossef dst_feature(const struct dst_entry *dst, u32 feature)
2080c3adfb8SGilad Ben-Yossef {
209bb5b7c11SDavid S. Miller 	return dst_metric(dst, RTAX_FEATURES) & feature;
2100c3adfb8SGilad Ben-Yossef }
2110c3adfb8SGilad Ben-Yossef 
212f67fbeaeSBrian Vazquez INDIRECT_CALLABLE_DECLARE(unsigned int ip6_mtu(const struct dst_entry *));
213f67fbeaeSBrian Vazquez INDIRECT_CALLABLE_DECLARE(unsigned int ipv4_mtu(const struct dst_entry *));
dst_mtu(const struct dst_entry * dst)2141da177e4SLinus Torvalds static inline u32 dst_mtu(const struct dst_entry *dst)
2151da177e4SLinus Torvalds {
216f67fbeaeSBrian Vazquez 	return INDIRECT_CALL_INET(dst->ops->mtu, ip6_mtu, ipv4_mtu, dst);
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
219c1e20f7cSStephen Hemminger /* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
dst_metric_rtt(const struct dst_entry * dst,int metric)220c1e20f7cSStephen Hemminger static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric)
221c1e20f7cSStephen Hemminger {
222c1e20f7cSStephen Hemminger 	return msecs_to_jiffies(dst_metric(dst, metric));
223c1e20f7cSStephen Hemminger }
224c1e20f7cSStephen Hemminger 
2251da177e4SLinus Torvalds static inline int
dst_metric_locked(const struct dst_entry * dst,int metric)226d33e4553SDavid S. Miller dst_metric_locked(const struct dst_entry *dst, int metric)
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds 	return dst_metric(dst, RTAX_LOCK) & (1 << metric);
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
dst_hold(struct dst_entry * dst)2311da177e4SLinus Torvalds static inline void dst_hold(struct dst_entry *dst)
2321da177e4SLinus Torvalds {
2335635c10dSEric Dumazet 	/*
2345635c10dSEric Dumazet 	 * If your kernel compilation stops here, please check
235bc9d3a9fSThomas Gleixner 	 * the placement of __rcuref in struct dst_entry
2365635c10dSEric Dumazet 	 */
237bc9d3a9fSThomas Gleixner 	BUILD_BUG_ON(offsetof(struct dst_entry, __rcuref) & 63);
238bc9d3a9fSThomas Gleixner 	WARN_ON(!rcuref_get(&dst->__rcuref));
2391da177e4SLinus Torvalds }
2401da177e4SLinus Torvalds 
dst_use_noref(struct dst_entry * dst,unsigned long time)2410da4af00SWei Wang static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
24203f49f34SPavel Emelyanov {
24332d18ab1SPaolo Abeni 	if (unlikely(time != dst->lastuse)) {
24403f49f34SPavel Emelyanov 		dst->__use++;
24503f49f34SPavel Emelyanov 		dst->lastuse = time;
24603f49f34SPavel Emelyanov 	}
2470da4af00SWei Wang }
24803f49f34SPavel Emelyanov 
dst_clone(struct dst_entry * dst)2497f95e188SEldad Zack static inline struct dst_entry *dst_clone(struct dst_entry *dst)
2501da177e4SLinus Torvalds {
2511da177e4SLinus Torvalds 	if (dst)
252222d7dbdSEric Dumazet 		dst_hold(dst);
2531da177e4SLinus Torvalds 	return dst;
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds 
256a4023dd0SJoe Perches void dst_release(struct dst_entry *dst);
2577fee226aSEric Dumazet 
2585f56f409SWei Wang void dst_release_immediate(struct dst_entry *dst);
2595f56f409SWei Wang 
refdst_drop(unsigned long refdst)2607fee226aSEric Dumazet static inline void refdst_drop(unsigned long refdst)
2617fee226aSEric Dumazet {
2627fee226aSEric Dumazet 	if (!(refdst & SKB_DST_NOREF))
2637fee226aSEric Dumazet 		dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
2647fee226aSEric Dumazet }
2657fee226aSEric Dumazet 
2667fee226aSEric Dumazet /**
2677fee226aSEric Dumazet  * skb_dst_drop - drops skb dst
2687fee226aSEric Dumazet  * @skb: buffer
2697fee226aSEric Dumazet  *
2707fee226aSEric Dumazet  * Drops dst reference count if a reference was taken.
2717fee226aSEric Dumazet  */
skb_dst_drop(struct sk_buff * skb)272adf30907SEric Dumazet static inline void skb_dst_drop(struct sk_buff *skb)
273adf30907SEric Dumazet {
2747fee226aSEric Dumazet 	if (skb->_skb_refdst) {
2757fee226aSEric Dumazet 		refdst_drop(skb->_skb_refdst);
2767fee226aSEric Dumazet 		skb->_skb_refdst = 0UL;
2777fee226aSEric Dumazet 	}
2787fee226aSEric Dumazet }
2797fee226aSEric Dumazet 
__skb_dst_copy(struct sk_buff * nskb,unsigned long refdst)280e79e2595SJoe Stringer static inline void __skb_dst_copy(struct sk_buff *nskb, unsigned long refdst)
2817fee226aSEric Dumazet {
2828a886b14SPaolo Abeni 	nskb->slow_gro |= !!refdst;
283e79e2595SJoe Stringer 	nskb->_skb_refdst = refdst;
2847fee226aSEric Dumazet 	if (!(nskb->_skb_refdst & SKB_DST_NOREF))
2857fee226aSEric Dumazet 		dst_clone(skb_dst(nskb));
2867fee226aSEric Dumazet }
2877fee226aSEric Dumazet 
skb_dst_copy(struct sk_buff * nskb,const struct sk_buff * oskb)288e79e2595SJoe Stringer static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
289e79e2595SJoe Stringer {
290e79e2595SJoe Stringer 	__skb_dst_copy(nskb, oskb->_skb_refdst);
291e79e2595SJoe Stringer }
292e79e2595SJoe Stringer 
2937fee226aSEric Dumazet /**
2945037e9efSEric Dumazet  * dst_hold_safe - Take a reference on a dst if possible
2955037e9efSEric Dumazet  * @dst: pointer to dst entry
2965037e9efSEric Dumazet  *
2975037e9efSEric Dumazet  * This helper returns false if it could not safely
2985037e9efSEric Dumazet  * take a reference on a dst.
2995037e9efSEric Dumazet  */
dst_hold_safe(struct dst_entry * dst)3005037e9efSEric Dumazet static inline bool dst_hold_safe(struct dst_entry *dst)
3015037e9efSEric Dumazet {
302bc9d3a9fSThomas Gleixner 	return rcuref_get(&dst->__rcuref);
3035037e9efSEric Dumazet }
3045037e9efSEric Dumazet 
3055037e9efSEric Dumazet /**
306222d7dbdSEric Dumazet  * skb_dst_force - makes sure skb dst is refcounted
3075037e9efSEric Dumazet  * @skb: buffer
3085037e9efSEric Dumazet  *
3095037e9efSEric Dumazet  * If dst is not yet refcounted and not destroyed, grab a ref on it.
310b60a7738SFlorian Westphal  * Returns true if dst is refcounted.
3115037e9efSEric Dumazet  */
skb_dst_force(struct sk_buff * skb)312b60a7738SFlorian Westphal static inline bool skb_dst_force(struct sk_buff *skb)
3135037e9efSEric Dumazet {
3145037e9efSEric Dumazet 	if (skb_dst_is_noref(skb)) {
3155037e9efSEric Dumazet 		struct dst_entry *dst = skb_dst(skb);
3165037e9efSEric Dumazet 
317222d7dbdSEric Dumazet 		WARN_ON(!rcu_read_lock_held());
3185037e9efSEric Dumazet 		if (!dst_hold_safe(dst))
3195037e9efSEric Dumazet 			dst = NULL;
3205037e9efSEric Dumazet 
3215037e9efSEric Dumazet 		skb->_skb_refdst = (unsigned long)dst;
3228a886b14SPaolo Abeni 		skb->slow_gro |= !!dst;
3235037e9efSEric Dumazet 	}
324b60a7738SFlorian Westphal 
325b60a7738SFlorian Westphal 	return skb->_skb_refdst != 0UL;
3265037e9efSEric Dumazet }
3275037e9efSEric Dumazet 
328d19d56ddSEric Dumazet 
329d19d56ddSEric Dumazet /**
330290b895eSEric Dumazet  *	__skb_tunnel_rx - prepare skb for rx reinsert
331290b895eSEric Dumazet  *	@skb: buffer
332290b895eSEric Dumazet  *	@dev: tunnel device
333ea23192eSNicolas Dichtel  *	@net: netns for packet i/o
334290b895eSEric Dumazet  *
335290b895eSEric Dumazet  *	After decapsulation, packet is going to re-enter (netif_rx()) our stack,
336290b895eSEric Dumazet  *	so make some cleanups. (no accounting done)
337290b895eSEric Dumazet  */
__skb_tunnel_rx(struct sk_buff * skb,struct net_device * dev,struct net * net)338ea23192eSNicolas Dichtel static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
339ea23192eSNicolas Dichtel 				   struct net *net)
340290b895eSEric Dumazet {
341290b895eSEric Dumazet 	skb->dev = dev;
342bdeab991STom Herbert 
343bdeab991STom Herbert 	/*
344*70d0bb45SSimon Horman 	 * Clear hash so that we can recalculate the hash for the
345bdeab991STom Herbert 	 * encapsulated packet, unless we have already determine the hash
346bdeab991STom Herbert 	 * over the L4 4-tuple.
347bdeab991STom Herbert 	 */
3487539fadcSTom Herbert 	skb_clear_hash_if_not_l4(skb);
349290b895eSEric Dumazet 	skb_set_queue_mapping(skb, 0);
350ea23192eSNicolas Dichtel 	skb_scrub_packet(skb, !net_eq(net, dev_net(dev)));
351290b895eSEric Dumazet }
352290b895eSEric Dumazet 
353290b895eSEric Dumazet /**
354d19d56ddSEric Dumazet  *	skb_tunnel_rx - prepare skb for rx reinsert
355d19d56ddSEric Dumazet  *	@skb: buffer
356d19d56ddSEric Dumazet  *	@dev: tunnel device
3578eb1a859SJonathan Neuschäfer  *	@net: netns for packet i/o
358d19d56ddSEric Dumazet  *
359d19d56ddSEric Dumazet  *	After decapsulation, packet is going to re-enter (netif_rx()) our stack,
360d19d56ddSEric Dumazet  *	so make some cleanups, and perform accounting.
361290b895eSEric Dumazet  *	Note: this accounting is not SMP safe.
362d19d56ddSEric Dumazet  */
skb_tunnel_rx(struct sk_buff * skb,struct net_device * dev,struct net * net)363ea23192eSNicolas Dichtel static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
364ea23192eSNicolas Dichtel 				 struct net *net)
365d19d56ddSEric Dumazet {
3666c1c5097SEric Dumazet 	DEV_STATS_INC(dev, rx_packets);
3676c1c5097SEric Dumazet 	DEV_STATS_ADD(dev, rx_bytes, skb->len);
368ea23192eSNicolas Dichtel 	__skb_tunnel_rx(skb, dev, net);
369d19d56ddSEric Dumazet }
370d19d56ddSEric Dumazet 
dst_tclassid(const struct sk_buff * skb)371808c1b69SDaniel Borkmann static inline u32 dst_tclassid(const struct sk_buff *skb)
372808c1b69SDaniel Borkmann {
373808c1b69SDaniel Borkmann #ifdef CONFIG_IP_ROUTE_CLASSID
374808c1b69SDaniel Borkmann 	const struct dst_entry *dst;
375808c1b69SDaniel Borkmann 
376808c1b69SDaniel Borkmann 	dst = skb_dst(skb);
377808c1b69SDaniel Borkmann 	if (dst)
378808c1b69SDaniel Borkmann 		return dst->tclassid;
379808c1b69SDaniel Borkmann #endif
380808c1b69SDaniel Borkmann 	return 0;
381808c1b69SDaniel Borkmann }
382808c1b69SDaniel Borkmann 
383ede2059dSEric W. Biederman int dst_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
dst_discard(struct sk_buff * skb)384aad88724SEric Dumazet static inline int dst_discard(struct sk_buff *skb)
385aad88724SEric Dumazet {
386ede2059dSEric W. Biederman 	return dst_discard_out(&init_net, skb->sk, skb);
387aad88724SEric Dumazet }
388762c8dc7SZhengchao Shao void *dst_alloc(struct dst_ops *ops, struct net_device *dev,
389a4023dd0SJoe Perches 		int initial_obsolete, unsigned short flags);
390f38a9eb1SThomas Graf void dst_init(struct dst_entry *dst, struct dst_ops *ops,
391762c8dc7SZhengchao Shao 	      struct net_device *dev, int initial_obsolete,
392f38a9eb1SThomas Graf 	      unsigned short flags);
3934a6ce2b6SWei Wang void dst_dev_put(struct dst_entry *dst);
3941da177e4SLinus Torvalds 
dst_confirm(struct dst_entry * dst)3951da177e4SLinus Torvalds static inline void dst_confirm(struct dst_entry *dst)
3961da177e4SLinus Torvalds {
39769cce1d1SDavid S. Miller }
3985110effeSDavid S. Miller 
dst_neigh_lookup(const struct dst_entry * dst,const void * daddr)399d3aaeb38SDavid S. Miller static inline struct neighbour *dst_neigh_lookup(const struct dst_entry *dst, const void *daddr)
400d3aaeb38SDavid S. Miller {
401aaa0c23cSZhouyi Zhou 	struct neighbour *n = dst->ops->neigh_lookup(dst, NULL, daddr);
402aaa0c23cSZhouyi Zhou 	return IS_ERR(n) ? NULL : n;
403f894cbf8SDavid S. Miller }
404f894cbf8SDavid S. Miller 
dst_neigh_lookup_skb(const struct dst_entry * dst,struct sk_buff * skb)405f894cbf8SDavid S. Miller static inline struct neighbour *dst_neigh_lookup_skb(const struct dst_entry *dst,
406f894cbf8SDavid S. Miller 						     struct sk_buff *skb)
407f894cbf8SDavid S. Miller {
4080992d67bSGuillaume Nault 	struct neighbour *n;
409394de110SMartin Varghese 
4100992d67bSGuillaume Nault 	if (WARN_ON_ONCE(!dst->ops->neigh_lookup))
4110992d67bSGuillaume Nault 		return NULL;
4120992d67bSGuillaume Nault 
413394de110SMartin Varghese 	n = dst->ops->neigh_lookup(dst, skb, NULL);
414394de110SMartin Varghese 
415aaa0c23cSZhouyi Zhou 	return IS_ERR(n) ? NULL : n;
416d3aaeb38SDavid S. Miller }
417d3aaeb38SDavid S. Miller 
dst_confirm_neigh(const struct dst_entry * dst,const void * daddr)41863fca65dSJulian Anastasov static inline void dst_confirm_neigh(const struct dst_entry *dst,
41963fca65dSJulian Anastasov 				     const void *daddr)
42063fca65dSJulian Anastasov {
42163fca65dSJulian Anastasov 	if (dst->ops->confirm_neigh)
42263fca65dSJulian Anastasov 		dst->ops->confirm_neigh(dst, daddr);
42363fca65dSJulian Anastasov }
42463fca65dSJulian Anastasov 
dst_link_failure(struct sk_buff * skb)4251da177e4SLinus Torvalds static inline void dst_link_failure(struct sk_buff *skb)
4261da177e4SLinus Torvalds {
427adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
4281da177e4SLinus Torvalds 	if (dst && dst->ops && dst->ops->link_failure)
4291da177e4SLinus Torvalds 		dst->ops->link_failure(skb);
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds 
dst_set_expires(struct dst_entry * dst,int timeout)4321da177e4SLinus Torvalds static inline void dst_set_expires(struct dst_entry *dst, int timeout)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds 	unsigned long expires = jiffies + timeout;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	if (expires == 0)
4371da177e4SLinus Torvalds 		expires = 1;
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	if (dst->expires == 0 || time_before(expires, dst->expires))
4401da177e4SLinus Torvalds 		dst->expires = expires;
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
4436585d7dcSBrian Vazquez INDIRECT_CALLABLE_DECLARE(int ip6_output(struct net *, struct sock *,
4446585d7dcSBrian Vazquez 					 struct sk_buff *));
4456585d7dcSBrian Vazquez INDIRECT_CALLABLE_DECLARE(int ip_output(struct net *, struct sock *,
4466585d7dcSBrian Vazquez 					 struct sk_buff *));
4471da177e4SLinus Torvalds /* Output packet to network from transport.  */
dst_output(struct net * net,struct sock * sk,struct sk_buff * skb)44813206b6bSEric W. Biederman static inline int dst_output(struct net *net, struct sock *sk, struct sk_buff *skb)
449aad88724SEric Dumazet {
4506585d7dcSBrian Vazquez 	return INDIRECT_CALL_INET(skb_dst(skb)->output,
4516585d7dcSBrian Vazquez 				  ip6_output, ip_output,
4526585d7dcSBrian Vazquez 				  net, sk, skb);
453aad88724SEric Dumazet }
4541da177e4SLinus Torvalds 
455e43b2190SBrian Vazquez INDIRECT_CALLABLE_DECLARE(int ip6_input(struct sk_buff *));
456e43b2190SBrian Vazquez INDIRECT_CALLABLE_DECLARE(int ip_local_deliver(struct sk_buff *));
4571da177e4SLinus Torvalds /* Input packet from network to transport.  */
dst_input(struct sk_buff * skb)4581da177e4SLinus Torvalds static inline int dst_input(struct sk_buff *skb)
4591da177e4SLinus Torvalds {
460e43b2190SBrian Vazquez 	return INDIRECT_CALL_INET(skb_dst(skb)->input,
461e43b2190SBrian Vazquez 				  ip6_input, ip_local_deliver, skb);
4621da177e4SLinus Torvalds }
4631da177e4SLinus Torvalds 
464bbd807dfSBrian Vazquez INDIRECT_CALLABLE_DECLARE(struct dst_entry *ip6_dst_check(struct dst_entry *,
465bbd807dfSBrian Vazquez 							  u32));
466bbd807dfSBrian Vazquez INDIRECT_CALLABLE_DECLARE(struct dst_entry *ipv4_dst_check(struct dst_entry *,
467bbd807dfSBrian Vazquez 							   u32));
dst_check(struct dst_entry * dst,u32 cookie)4681da177e4SLinus Torvalds static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
4691da177e4SLinus Torvalds {
4701da177e4SLinus Torvalds 	if (dst->obsolete)
471bbd807dfSBrian Vazquez 		dst = INDIRECT_CALL_INET(dst->ops->check, ip6_dst_check,
472bbd807dfSBrian Vazquez 					 ipv4_dst_check, dst, cookie);
4731da177e4SLinus Torvalds 	return dst;
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds 
476815f4e57SHerbert Xu /* Flags for xfrm_lookup flags argument. */
477815f4e57SHerbert Xu enum {
47880c0bc9eSDavid S. Miller 	XFRM_LOOKUP_ICMP = 1 << 0,
479b8c203b2SSteffen Klassert 	XFRM_LOOKUP_QUEUE = 1 << 1,
480ac37e251Shuaibin Wang 	XFRM_LOOKUP_KEEP_DST_REF = 1 << 2,
481815f4e57SHerbert Xu };
482815f4e57SHerbert Xu 
4831da177e4SLinus Torvalds struct flowi;
4841da177e4SLinus Torvalds #ifndef CONFIG_XFRM
xfrm_lookup(struct net * net,struct dst_entry * dst_orig,const struct flowi * fl,const struct sock * sk,int flags)485452edd59SDavid S. Miller static inline struct dst_entry *xfrm_lookup(struct net *net,
486452edd59SDavid S. Miller 					    struct dst_entry *dst_orig,
4876f9c9615SEric Dumazet 					    const struct flowi *fl,
4886f9c9615SEric Dumazet 					    const struct sock *sk,
489dee9f4bcSDavid S. Miller 					    int flags)
4901da177e4SLinus Torvalds {
491452edd59SDavid S. Miller 	return dst_orig;
4921da177e4SLinus Torvalds }
493e87b3998SVlad Yasevich 
494bc56b334SBenedict Wong 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)495bc56b334SBenedict Wong xfrm_lookup_with_ifid(struct net *net, struct dst_entry *dst_orig,
496bc56b334SBenedict Wong 		      const struct flowi *fl, const struct sock *sk,
497bc56b334SBenedict Wong 		      int flags, u32 if_id)
498bc56b334SBenedict Wong {
499bc56b334SBenedict Wong 	return dst_orig;
500bc56b334SBenedict Wong }
501bc56b334SBenedict Wong 
xfrm_lookup_route(struct net * net,struct dst_entry * dst_orig,const struct flowi * fl,const struct sock * sk,int flags)502f92ee619SSteffen Klassert static inline struct dst_entry *xfrm_lookup_route(struct net *net,
503f92ee619SSteffen Klassert 						  struct dst_entry *dst_orig,
504f92ee619SSteffen Klassert 						  const struct flowi *fl,
5056f9c9615SEric Dumazet 						  const struct sock *sk,
506f92ee619SSteffen Klassert 						  int flags)
507f92ee619SSteffen Klassert {
508f92ee619SSteffen Klassert 	return dst_orig;
509f92ee619SSteffen Klassert }
510f92ee619SSteffen Klassert 
dst_xfrm(const struct dst_entry * dst)511e87b3998SVlad Yasevich static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
512e87b3998SVlad Yasevich {
513e87b3998SVlad Yasevich 	return NULL;
514e87b3998SVlad Yasevich }
515e87b3998SVlad Yasevich 
5161da177e4SLinus Torvalds #else
517a4023dd0SJoe Perches struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
5186f9c9615SEric Dumazet 			      const struct flowi *fl, const struct sock *sk,
519452edd59SDavid S. Miller 			      int flags);
520e87b3998SVlad Yasevich 
521bc56b334SBenedict Wong struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
522bc56b334SBenedict Wong 					struct dst_entry *dst_orig,
523bc56b334SBenedict Wong 					const struct flowi *fl,
524bc56b334SBenedict Wong 					const struct sock *sk, int flags,
525bc56b334SBenedict Wong 					u32 if_id);
526bc56b334SBenedict Wong 
527f92ee619SSteffen Klassert struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
5286f9c9615SEric Dumazet 				    const struct flowi *fl, const struct sock *sk,
529f92ee619SSteffen Klassert 				    int flags);
530f92ee619SSteffen Klassert 
531e87b3998SVlad Yasevich /* skb attached with this dst needs transformation if dst->xfrm is valid */
dst_xfrm(const struct dst_entry * dst)532e87b3998SVlad Yasevich static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
533e87b3998SVlad Yasevich {
534e87b3998SVlad Yasevich 	return dst->xfrm;
535e87b3998SVlad Yasevich }
5361da177e4SLinus Torvalds #endif
5371da177e4SLinus Torvalds 
skb_dst_update_pmtu(struct sk_buff * skb,u32 mtu)538f15ca723SNicolas Dichtel static inline void skb_dst_update_pmtu(struct sk_buff *skb, u32 mtu)
539f15ca723SNicolas Dichtel {
540f15ca723SNicolas Dichtel 	struct dst_entry *dst = skb_dst(skb);
541f15ca723SNicolas Dichtel 
542f15ca723SNicolas Dichtel 	if (dst && dst->ops->update_pmtu)
543bd085ef6SHangbin Liu 		dst->ops->update_pmtu(dst, NULL, skb, mtu, true);
544f15ca723SNicolas Dichtel }
545f15ca723SNicolas Dichtel 
54607dc35c6SHangbin Liu /* update dst pmtu but not do neighbor confirm */
skb_dst_update_pmtu_no_confirm(struct sk_buff * skb,u32 mtu)54707dc35c6SHangbin Liu static inline void skb_dst_update_pmtu_no_confirm(struct sk_buff *skb, u32 mtu)
54807dc35c6SHangbin Liu {
54907dc35c6SHangbin Liu 	struct dst_entry *dst = skb_dst(skb);
55007dc35c6SHangbin Liu 
55107dc35c6SHangbin Liu 	if (dst && dst->ops->update_pmtu)
55207dc35c6SHangbin Liu 		dst->ops->update_pmtu(dst, NULL, skb, mtu, false);
55307dc35c6SHangbin Liu }
55407dc35c6SHangbin Liu 
555c4c877b2SDaniel Borkmann struct dst_entry *dst_blackhole_check(struct dst_entry *dst, u32 cookie);
556c4c877b2SDaniel Borkmann void dst_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
557c4c877b2SDaniel Borkmann 			       struct sk_buff *skb, u32 mtu, bool confirm_neigh);
558c4c877b2SDaniel Borkmann void dst_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
559c4c877b2SDaniel Borkmann 			    struct sk_buff *skb);
560c4c877b2SDaniel Borkmann u32 *dst_blackhole_cow_metrics(struct dst_entry *dst, unsigned long old);
561c4c877b2SDaniel Borkmann struct neighbour *dst_blackhole_neigh_lookup(const struct dst_entry *dst,
562c4c877b2SDaniel Borkmann 					     struct sk_buff *skb,
563c4c877b2SDaniel Borkmann 					     const void *daddr);
564c4c877b2SDaniel Borkmann unsigned int dst_blackhole_mtu(const struct dst_entry *dst);
565c4c877b2SDaniel Borkmann 
5661da177e4SLinus Torvalds #endif /* _NET_DST_H */
567