xref: /openbmc/linux/include/net/dst.h (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
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>
19*bc9d3a9fSThomas 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 	/*
65*bc9d3a9fSThomas 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
69*bc9d3a9fSThomas 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;
79*bc9d3a9fSThomas 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
86*bc9d3a9fSThomas Gleixner 	 * __rcuref under contention of __rcuref. This also puts the
87d288a162SWangyang Guo 	 * frequently accessed members of rtable and rt6_info out of the
88*bc9d3a9fSThomas 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 u32
dst_allfrag(const struct dst_entry * dst)2261da177e4SLinus Torvalds dst_allfrag(const struct dst_entry *dst)
2271da177e4SLinus Torvalds {
2280c3adfb8SGilad Ben-Yossef 	int ret = dst_feature(dst,  RTAX_FEATURE_ALLFRAG);
2291da177e4SLinus Torvalds 	return ret;
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds static inline int
dst_metric_locked(const struct dst_entry * dst,int metric)233d33e4553SDavid S. Miller dst_metric_locked(const struct dst_entry *dst, int metric)
2341da177e4SLinus Torvalds {
2351da177e4SLinus Torvalds 	return dst_metric(dst, RTAX_LOCK) & (1 << metric);
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
dst_hold(struct dst_entry * dst)2381da177e4SLinus Torvalds static inline void dst_hold(struct dst_entry *dst)
2391da177e4SLinus Torvalds {
2405635c10dSEric Dumazet 	/*
2415635c10dSEric Dumazet 	 * If your kernel compilation stops here, please check
242*bc9d3a9fSThomas Gleixner 	 * the placement of __rcuref in struct dst_entry
2435635c10dSEric Dumazet 	 */
244*bc9d3a9fSThomas Gleixner 	BUILD_BUG_ON(offsetof(struct dst_entry, __rcuref) & 63);
245*bc9d3a9fSThomas Gleixner 	WARN_ON(!rcuref_get(&dst->__rcuref));
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds 
dst_use_noref(struct dst_entry * dst,unsigned long time)2480da4af00SWei Wang static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
24903f49f34SPavel Emelyanov {
25032d18ab1SPaolo Abeni 	if (unlikely(time != dst->lastuse)) {
25103f49f34SPavel Emelyanov 		dst->__use++;
25203f49f34SPavel Emelyanov 		dst->lastuse = time;
25303f49f34SPavel Emelyanov 	}
2540da4af00SWei Wang }
25503f49f34SPavel Emelyanov 
dst_clone(struct dst_entry * dst)2567f95e188SEldad Zack static inline struct dst_entry *dst_clone(struct dst_entry *dst)
2571da177e4SLinus Torvalds {
2581da177e4SLinus Torvalds 	if (dst)
259222d7dbdSEric Dumazet 		dst_hold(dst);
2601da177e4SLinus Torvalds 	return dst;
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds 
263a4023dd0SJoe Perches void dst_release(struct dst_entry *dst);
2647fee226aSEric Dumazet 
2655f56f409SWei Wang void dst_release_immediate(struct dst_entry *dst);
2665f56f409SWei Wang 
refdst_drop(unsigned long refdst)2677fee226aSEric Dumazet static inline void refdst_drop(unsigned long refdst)
2687fee226aSEric Dumazet {
2697fee226aSEric Dumazet 	if (!(refdst & SKB_DST_NOREF))
2707fee226aSEric Dumazet 		dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
2717fee226aSEric Dumazet }
2727fee226aSEric Dumazet 
2737fee226aSEric Dumazet /**
2747fee226aSEric Dumazet  * skb_dst_drop - drops skb dst
2757fee226aSEric Dumazet  * @skb: buffer
2767fee226aSEric Dumazet  *
2777fee226aSEric Dumazet  * Drops dst reference count if a reference was taken.
2787fee226aSEric Dumazet  */
skb_dst_drop(struct sk_buff * skb)279adf30907SEric Dumazet static inline void skb_dst_drop(struct sk_buff *skb)
280adf30907SEric Dumazet {
2817fee226aSEric Dumazet 	if (skb->_skb_refdst) {
2827fee226aSEric Dumazet 		refdst_drop(skb->_skb_refdst);
2837fee226aSEric Dumazet 		skb->_skb_refdst = 0UL;
2847fee226aSEric Dumazet 	}
2857fee226aSEric Dumazet }
2867fee226aSEric Dumazet 
__skb_dst_copy(struct sk_buff * nskb,unsigned long refdst)287e79e2595SJoe Stringer static inline void __skb_dst_copy(struct sk_buff *nskb, unsigned long refdst)
2887fee226aSEric Dumazet {
2898a886b14SPaolo Abeni 	nskb->slow_gro |= !!refdst;
290e79e2595SJoe Stringer 	nskb->_skb_refdst = refdst;
2917fee226aSEric Dumazet 	if (!(nskb->_skb_refdst & SKB_DST_NOREF))
2927fee226aSEric Dumazet 		dst_clone(skb_dst(nskb));
2937fee226aSEric Dumazet }
2947fee226aSEric Dumazet 
skb_dst_copy(struct sk_buff * nskb,const struct sk_buff * oskb)295e79e2595SJoe Stringer static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
296e79e2595SJoe Stringer {
297e79e2595SJoe Stringer 	__skb_dst_copy(nskb, oskb->_skb_refdst);
298e79e2595SJoe Stringer }
299e79e2595SJoe Stringer 
3007fee226aSEric Dumazet /**
3015037e9efSEric Dumazet  * dst_hold_safe - Take a reference on a dst if possible
3025037e9efSEric Dumazet  * @dst: pointer to dst entry
3035037e9efSEric Dumazet  *
3045037e9efSEric Dumazet  * This helper returns false if it could not safely
3055037e9efSEric Dumazet  * take a reference on a dst.
3065037e9efSEric Dumazet  */
dst_hold_safe(struct dst_entry * dst)3075037e9efSEric Dumazet static inline bool dst_hold_safe(struct dst_entry *dst)
3085037e9efSEric Dumazet {
309*bc9d3a9fSThomas Gleixner 	return rcuref_get(&dst->__rcuref);
3105037e9efSEric Dumazet }
3115037e9efSEric Dumazet 
3125037e9efSEric Dumazet /**
313222d7dbdSEric Dumazet  * skb_dst_force - makes sure skb dst is refcounted
3145037e9efSEric Dumazet  * @skb: buffer
3155037e9efSEric Dumazet  *
3165037e9efSEric Dumazet  * If dst is not yet refcounted and not destroyed, grab a ref on it.
317b60a7738SFlorian Westphal  * Returns true if dst is refcounted.
3185037e9efSEric Dumazet  */
skb_dst_force(struct sk_buff * skb)319b60a7738SFlorian Westphal static inline bool skb_dst_force(struct sk_buff *skb)
3205037e9efSEric Dumazet {
3215037e9efSEric Dumazet 	if (skb_dst_is_noref(skb)) {
3225037e9efSEric Dumazet 		struct dst_entry *dst = skb_dst(skb);
3235037e9efSEric Dumazet 
324222d7dbdSEric Dumazet 		WARN_ON(!rcu_read_lock_held());
3255037e9efSEric Dumazet 		if (!dst_hold_safe(dst))
3265037e9efSEric Dumazet 			dst = NULL;
3275037e9efSEric Dumazet 
3285037e9efSEric Dumazet 		skb->_skb_refdst = (unsigned long)dst;
3298a886b14SPaolo Abeni 		skb->slow_gro |= !!dst;
3305037e9efSEric Dumazet 	}
331b60a7738SFlorian Westphal 
332b60a7738SFlorian Westphal 	return skb->_skb_refdst != 0UL;
3335037e9efSEric Dumazet }
3345037e9efSEric Dumazet 
335d19d56ddSEric Dumazet 
336d19d56ddSEric Dumazet /**
337290b895eSEric Dumazet  *	__skb_tunnel_rx - prepare skb for rx reinsert
338290b895eSEric Dumazet  *	@skb: buffer
339290b895eSEric Dumazet  *	@dev: tunnel device
340ea23192eSNicolas Dichtel  *	@net: netns for packet i/o
341290b895eSEric Dumazet  *
342290b895eSEric Dumazet  *	After decapsulation, packet is going to re-enter (netif_rx()) our stack,
343290b895eSEric Dumazet  *	so make some cleanups. (no accounting done)
344290b895eSEric Dumazet  */
__skb_tunnel_rx(struct sk_buff * skb,struct net_device * dev,struct net * net)345ea23192eSNicolas Dichtel static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
346ea23192eSNicolas Dichtel 				   struct net *net)
347290b895eSEric Dumazet {
348290b895eSEric Dumazet 	skb->dev = dev;
349bdeab991STom Herbert 
350bdeab991STom Herbert 	/*
3517539fadcSTom Herbert 	 * Clear hash so that we can recalulate the hash for the
352bdeab991STom Herbert 	 * encapsulated packet, unless we have already determine the hash
353bdeab991STom Herbert 	 * over the L4 4-tuple.
354bdeab991STom Herbert 	 */
3557539fadcSTom Herbert 	skb_clear_hash_if_not_l4(skb);
356290b895eSEric Dumazet 	skb_set_queue_mapping(skb, 0);
357ea23192eSNicolas Dichtel 	skb_scrub_packet(skb, !net_eq(net, dev_net(dev)));
358290b895eSEric Dumazet }
359290b895eSEric Dumazet 
360290b895eSEric Dumazet /**
361d19d56ddSEric Dumazet  *	skb_tunnel_rx - prepare skb for rx reinsert
362d19d56ddSEric Dumazet  *	@skb: buffer
363d19d56ddSEric Dumazet  *	@dev: tunnel device
3648eb1a859SJonathan Neuschäfer  *	@net: netns for packet i/o
365d19d56ddSEric Dumazet  *
366d19d56ddSEric Dumazet  *	After decapsulation, packet is going to re-enter (netif_rx()) our stack,
367d19d56ddSEric Dumazet  *	so make some cleanups, and perform accounting.
368290b895eSEric Dumazet  *	Note: this accounting is not SMP safe.
369d19d56ddSEric Dumazet  */
skb_tunnel_rx(struct sk_buff * skb,struct net_device * dev,struct net * net)370ea23192eSNicolas Dichtel static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
371ea23192eSNicolas Dichtel 				 struct net *net)
372d19d56ddSEric Dumazet {
3736c1c5097SEric Dumazet 	DEV_STATS_INC(dev, rx_packets);
3746c1c5097SEric Dumazet 	DEV_STATS_ADD(dev, rx_bytes, skb->len);
375ea23192eSNicolas Dichtel 	__skb_tunnel_rx(skb, dev, net);
376d19d56ddSEric Dumazet }
377d19d56ddSEric Dumazet 
dst_tclassid(const struct sk_buff * skb)378808c1b69SDaniel Borkmann static inline u32 dst_tclassid(const struct sk_buff *skb)
379808c1b69SDaniel Borkmann {
380808c1b69SDaniel Borkmann #ifdef CONFIG_IP_ROUTE_CLASSID
381808c1b69SDaniel Borkmann 	const struct dst_entry *dst;
382808c1b69SDaniel Borkmann 
383808c1b69SDaniel Borkmann 	dst = skb_dst(skb);
384808c1b69SDaniel Borkmann 	if (dst)
385808c1b69SDaniel Borkmann 		return dst->tclassid;
386808c1b69SDaniel Borkmann #endif
387808c1b69SDaniel Borkmann 	return 0;
388808c1b69SDaniel Borkmann }
389808c1b69SDaniel Borkmann 
390ede2059dSEric W. Biederman int dst_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
dst_discard(struct sk_buff * skb)391aad88724SEric Dumazet static inline int dst_discard(struct sk_buff *skb)
392aad88724SEric Dumazet {
393ede2059dSEric W. Biederman 	return dst_discard_out(&init_net, skb->sk, skb);
394aad88724SEric Dumazet }
395a4023dd0SJoe Perches void *dst_alloc(struct dst_ops *ops, struct net_device *dev, int initial_ref,
396a4023dd0SJoe Perches 		int initial_obsolete, unsigned short flags);
397f38a9eb1SThomas Graf void dst_init(struct dst_entry *dst, struct dst_ops *ops,
398f38a9eb1SThomas Graf 	      struct net_device *dev, int initial_ref, int initial_obsolete,
399f38a9eb1SThomas Graf 	      unsigned short flags);
400a4023dd0SJoe Perches struct dst_entry *dst_destroy(struct dst_entry *dst);
4014a6ce2b6SWei Wang void dst_dev_put(struct dst_entry *dst);
4021da177e4SLinus Torvalds 
dst_confirm(struct dst_entry * dst)4031da177e4SLinus Torvalds static inline void dst_confirm(struct dst_entry *dst)
4041da177e4SLinus Torvalds {
40569cce1d1SDavid S. Miller }
4065110effeSDavid S. Miller 
dst_neigh_lookup(const struct dst_entry * dst,const void * daddr)407d3aaeb38SDavid S. Miller static inline struct neighbour *dst_neigh_lookup(const struct dst_entry *dst, const void *daddr)
408d3aaeb38SDavid S. Miller {
409aaa0c23cSZhouyi Zhou 	struct neighbour *n = dst->ops->neigh_lookup(dst, NULL, daddr);
410aaa0c23cSZhouyi Zhou 	return IS_ERR(n) ? NULL : n;
411f894cbf8SDavid S. Miller }
412f894cbf8SDavid S. Miller 
dst_neigh_lookup_skb(const struct dst_entry * dst,struct sk_buff * skb)413f894cbf8SDavid S. Miller static inline struct neighbour *dst_neigh_lookup_skb(const struct dst_entry *dst,
414f894cbf8SDavid S. Miller 						     struct sk_buff *skb)
415f894cbf8SDavid S. Miller {
4160992d67bSGuillaume Nault 	struct neighbour *n;
417394de110SMartin Varghese 
4180992d67bSGuillaume Nault 	if (WARN_ON_ONCE(!dst->ops->neigh_lookup))
4190992d67bSGuillaume Nault 		return NULL;
4200992d67bSGuillaume Nault 
421394de110SMartin Varghese 	n = dst->ops->neigh_lookup(dst, skb, NULL);
422394de110SMartin Varghese 
423aaa0c23cSZhouyi Zhou 	return IS_ERR(n) ? NULL : n;
424d3aaeb38SDavid S. Miller }
425d3aaeb38SDavid S. Miller 
dst_confirm_neigh(const struct dst_entry * dst,const void * daddr)42663fca65dSJulian Anastasov static inline void dst_confirm_neigh(const struct dst_entry *dst,
42763fca65dSJulian Anastasov 				     const void *daddr)
42863fca65dSJulian Anastasov {
42963fca65dSJulian Anastasov 	if (dst->ops->confirm_neigh)
43063fca65dSJulian Anastasov 		dst->ops->confirm_neigh(dst, daddr);
43163fca65dSJulian Anastasov }
43263fca65dSJulian Anastasov 
dst_link_failure(struct sk_buff * skb)4331da177e4SLinus Torvalds static inline void dst_link_failure(struct sk_buff *skb)
4341da177e4SLinus Torvalds {
435adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
4361da177e4SLinus Torvalds 	if (dst && dst->ops && dst->ops->link_failure)
4371da177e4SLinus Torvalds 		dst->ops->link_failure(skb);
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
dst_set_expires(struct dst_entry * dst,int timeout)4401da177e4SLinus Torvalds static inline void dst_set_expires(struct dst_entry *dst, int timeout)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds 	unsigned long expires = jiffies + timeout;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	if (expires == 0)
4451da177e4SLinus Torvalds 		expires = 1;
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	if (dst->expires == 0 || time_before(expires, dst->expires))
4481da177e4SLinus Torvalds 		dst->expires = expires;
4491da177e4SLinus Torvalds }
4501da177e4SLinus Torvalds 
4516585d7dcSBrian Vazquez INDIRECT_CALLABLE_DECLARE(int ip6_output(struct net *, struct sock *,
4526585d7dcSBrian Vazquez 					 struct sk_buff *));
4536585d7dcSBrian Vazquez INDIRECT_CALLABLE_DECLARE(int ip_output(struct net *, struct sock *,
4546585d7dcSBrian Vazquez 					 struct sk_buff *));
4551da177e4SLinus Torvalds /* Output packet to network from transport.  */
dst_output(struct net * net,struct sock * sk,struct sk_buff * skb)45613206b6bSEric W. Biederman static inline int dst_output(struct net *net, struct sock *sk, struct sk_buff *skb)
457aad88724SEric Dumazet {
4586585d7dcSBrian Vazquez 	return INDIRECT_CALL_INET(skb_dst(skb)->output,
4596585d7dcSBrian Vazquez 				  ip6_output, ip_output,
4606585d7dcSBrian Vazquez 				  net, sk, skb);
461aad88724SEric Dumazet }
4621da177e4SLinus Torvalds 
463e43b2190SBrian Vazquez INDIRECT_CALLABLE_DECLARE(int ip6_input(struct sk_buff *));
464e43b2190SBrian Vazquez INDIRECT_CALLABLE_DECLARE(int ip_local_deliver(struct sk_buff *));
4651da177e4SLinus Torvalds /* Input packet from network to transport.  */
dst_input(struct sk_buff * skb)4661da177e4SLinus Torvalds static inline int dst_input(struct sk_buff *skb)
4671da177e4SLinus Torvalds {
468e43b2190SBrian Vazquez 	return INDIRECT_CALL_INET(skb_dst(skb)->input,
469e43b2190SBrian Vazquez 				  ip6_input, ip_local_deliver, skb);
4701da177e4SLinus Torvalds }
4711da177e4SLinus Torvalds 
472bbd807dfSBrian Vazquez INDIRECT_CALLABLE_DECLARE(struct dst_entry *ip6_dst_check(struct dst_entry *,
473bbd807dfSBrian Vazquez 							  u32));
474bbd807dfSBrian Vazquez INDIRECT_CALLABLE_DECLARE(struct dst_entry *ipv4_dst_check(struct dst_entry *,
475bbd807dfSBrian Vazquez 							   u32));
dst_check(struct dst_entry * dst,u32 cookie)4761da177e4SLinus Torvalds static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
4771da177e4SLinus Torvalds {
4781da177e4SLinus Torvalds 	if (dst->obsolete)
479bbd807dfSBrian Vazquez 		dst = INDIRECT_CALL_INET(dst->ops->check, ip6_dst_check,
480bbd807dfSBrian Vazquez 					 ipv4_dst_check, dst, cookie);
4811da177e4SLinus Torvalds 	return dst;
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
484815f4e57SHerbert Xu /* Flags for xfrm_lookup flags argument. */
485815f4e57SHerbert Xu enum {
48680c0bc9eSDavid S. Miller 	XFRM_LOOKUP_ICMP = 1 << 0,
487b8c203b2SSteffen Klassert 	XFRM_LOOKUP_QUEUE = 1 << 1,
488ac37e251Shuaibin Wang 	XFRM_LOOKUP_KEEP_DST_REF = 1 << 2,
489815f4e57SHerbert Xu };
490815f4e57SHerbert Xu 
4911da177e4SLinus Torvalds struct flowi;
4921da177e4SLinus Torvalds #ifndef CONFIG_XFRM
xfrm_lookup(struct net * net,struct dst_entry * dst_orig,const struct flowi * fl,const struct sock * sk,int flags)493452edd59SDavid S. Miller static inline struct dst_entry *xfrm_lookup(struct net *net,
494452edd59SDavid S. Miller 					    struct dst_entry *dst_orig,
4956f9c9615SEric Dumazet 					    const struct flowi *fl,
4966f9c9615SEric Dumazet 					    const struct sock *sk,
497dee9f4bcSDavid S. Miller 					    int flags)
4981da177e4SLinus Torvalds {
499452edd59SDavid S. Miller 	return dst_orig;
5001da177e4SLinus Torvalds }
501e87b3998SVlad Yasevich 
502bc56b334SBenedict 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)503bc56b334SBenedict Wong xfrm_lookup_with_ifid(struct net *net, struct dst_entry *dst_orig,
504bc56b334SBenedict Wong 		      const struct flowi *fl, const struct sock *sk,
505bc56b334SBenedict Wong 		      int flags, u32 if_id)
506bc56b334SBenedict Wong {
507bc56b334SBenedict Wong 	return dst_orig;
508bc56b334SBenedict Wong }
509bc56b334SBenedict Wong 
xfrm_lookup_route(struct net * net,struct dst_entry * dst_orig,const struct flowi * fl,const struct sock * sk,int flags)510f92ee619SSteffen Klassert static inline struct dst_entry *xfrm_lookup_route(struct net *net,
511f92ee619SSteffen Klassert 						  struct dst_entry *dst_orig,
512f92ee619SSteffen Klassert 						  const struct flowi *fl,
5136f9c9615SEric Dumazet 						  const struct sock *sk,
514f92ee619SSteffen Klassert 						  int flags)
515f92ee619SSteffen Klassert {
516f92ee619SSteffen Klassert 	return dst_orig;
517f92ee619SSteffen Klassert }
518f92ee619SSteffen Klassert 
dst_xfrm(const struct dst_entry * dst)519e87b3998SVlad Yasevich static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
520e87b3998SVlad Yasevich {
521e87b3998SVlad Yasevich 	return NULL;
522e87b3998SVlad Yasevich }
523e87b3998SVlad Yasevich 
5241da177e4SLinus Torvalds #else
525a4023dd0SJoe Perches struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
5266f9c9615SEric Dumazet 			      const struct flowi *fl, const struct sock *sk,
527452edd59SDavid S. Miller 			      int flags);
528e87b3998SVlad Yasevich 
529bc56b334SBenedict Wong struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
530bc56b334SBenedict Wong 					struct dst_entry *dst_orig,
531bc56b334SBenedict Wong 					const struct flowi *fl,
532bc56b334SBenedict Wong 					const struct sock *sk, int flags,
533bc56b334SBenedict Wong 					u32 if_id);
534bc56b334SBenedict Wong 
535f92ee619SSteffen Klassert struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
5366f9c9615SEric Dumazet 				    const struct flowi *fl, const struct sock *sk,
537f92ee619SSteffen Klassert 				    int flags);
538f92ee619SSteffen Klassert 
539e87b3998SVlad Yasevich /* skb attached with this dst needs transformation if dst->xfrm is valid */
dst_xfrm(const struct dst_entry * dst)540e87b3998SVlad Yasevich static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
541e87b3998SVlad Yasevich {
542e87b3998SVlad Yasevich 	return dst->xfrm;
543e87b3998SVlad Yasevich }
5441da177e4SLinus Torvalds #endif
5451da177e4SLinus Torvalds 
skb_dst_update_pmtu(struct sk_buff * skb,u32 mtu)546f15ca723SNicolas Dichtel static inline void skb_dst_update_pmtu(struct sk_buff *skb, u32 mtu)
547f15ca723SNicolas Dichtel {
548f15ca723SNicolas Dichtel 	struct dst_entry *dst = skb_dst(skb);
549f15ca723SNicolas Dichtel 
550f15ca723SNicolas Dichtel 	if (dst && dst->ops->update_pmtu)
551bd085ef6SHangbin Liu 		dst->ops->update_pmtu(dst, NULL, skb, mtu, true);
552f15ca723SNicolas Dichtel }
553f15ca723SNicolas Dichtel 
55407dc35c6SHangbin Liu /* update dst pmtu but not do neighbor confirm */
skb_dst_update_pmtu_no_confirm(struct sk_buff * skb,u32 mtu)55507dc35c6SHangbin Liu static inline void skb_dst_update_pmtu_no_confirm(struct sk_buff *skb, u32 mtu)
55607dc35c6SHangbin Liu {
55707dc35c6SHangbin Liu 	struct dst_entry *dst = skb_dst(skb);
55807dc35c6SHangbin Liu 
55907dc35c6SHangbin Liu 	if (dst && dst->ops->update_pmtu)
56007dc35c6SHangbin Liu 		dst->ops->update_pmtu(dst, NULL, skb, mtu, false);
56107dc35c6SHangbin Liu }
56207dc35c6SHangbin Liu 
563c4c877b2SDaniel Borkmann struct dst_entry *dst_blackhole_check(struct dst_entry *dst, u32 cookie);
564c4c877b2SDaniel Borkmann void dst_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
565c4c877b2SDaniel Borkmann 			       struct sk_buff *skb, u32 mtu, bool confirm_neigh);
566c4c877b2SDaniel Borkmann void dst_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
567c4c877b2SDaniel Borkmann 			    struct sk_buff *skb);
568c4c877b2SDaniel Borkmann u32 *dst_blackhole_cow_metrics(struct dst_entry *dst, unsigned long old);
569c4c877b2SDaniel Borkmann struct neighbour *dst_blackhole_neigh_lookup(const struct dst_entry *dst,
570c4c877b2SDaniel Borkmann 					     struct sk_buff *skb,
571c4c877b2SDaniel Borkmann 					     const void *daddr);
572c4c877b2SDaniel Borkmann unsigned int dst_blackhole_mtu(const struct dst_entry *dst);
573c4c877b2SDaniel Borkmann 
5741da177e4SLinus Torvalds #endif /* _NET_DST_H */
575