xref: /openbmc/linux/include/net/dst.h (revision 9620fef27ed2cdb37bf6fd028f32bea2ef5119a8)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * net/dst.h	Protocol independent destination cache definitions.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #ifndef _NET_DST_H
91da177e4SLinus Torvalds #define _NET_DST_H
101da177e4SLinus Torvalds 
1186393e52SAlexey Dobriyan #include <net/dst_ops.h>
1214c85021SArnaldo Carvalho de Melo #include <linux/netdevice.h>
131da177e4SLinus Torvalds #include <linux/rtnetlink.h>
141da177e4SLinus Torvalds #include <linux/rcupdate.h>
15187f1882SPaul Gortmaker #include <linux/bug.h>
161da177e4SLinus Torvalds #include <linux/jiffies.h>
17*9620fef2SEric Dumazet #include <linux/refcount.h>
181da177e4SLinus Torvalds #include <net/neighbour.h>
191da177e4SLinus Torvalds #include <asm/processor.h>
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #define DST_GC_MIN	(HZ/10)
221da177e4SLinus Torvalds #define DST_GC_INC	(HZ/2)
231da177e4SLinus Torvalds #define DST_GC_MAX	(120*HZ)
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds /* Each dst_entry has reference count and sits in some parent list(s).
261da177e4SLinus Torvalds  * When it is removed from parent list, it is "freed" (dst_free).
271da177e4SLinus Torvalds  * After this it enters dead state (dst->obsolete > 0) and if its refcnt
281da177e4SLinus Torvalds  * is zero, it can be destroyed immediately, otherwise it is added
291da177e4SLinus Torvalds  * to gc list and garbage collector periodically checks the refcnt.
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds struct sk_buff;
331da177e4SLinus Torvalds 
34fd2c3ef7SEric Dumazet struct dst_entry {
3566727145SAlexey Dobriyan 	struct net_device       *dev;
361e19e02cSEric Dumazet 	struct rcu_head		rcu_head;
371da177e4SLinus Torvalds 	struct dst_entry	*child;
3862fa8a84SDavid S. Miller 	struct  dst_ops	        *ops;
3962fa8a84SDavid S. Miller 	unsigned long		_metrics;
401da177e4SLinus Torvalds 	unsigned long           expires;
41f1dd9c37SZhang Yanmin 	struct dst_entry	*path;
42ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	struct dst_entry	*from;
43def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
441da177e4SLinus Torvalds 	struct xfrm_state	*xfrm;
455635c10dSEric Dumazet #else
465635c10dSEric Dumazet 	void			*__pad1;
47def8b4faSAlexey Dobriyan #endif
481da177e4SLinus Torvalds 	int			(*input)(struct sk_buff *);
49ede2059dSEric W. Biederman 	int			(*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
501da177e4SLinus Torvalds 
515110effeSDavid S. Miller 	unsigned short		flags;
52f6b72b62SDavid S. Miller #define DST_HOST		0x0001
53f6b72b62SDavid S. Miller #define DST_NOXFRM		0x0002
54f6b72b62SDavid S. Miller #define DST_NOPOLICY		0x0004
551eb04e7cSWei Wang #define DST_NOCOUNT		0x0008
561eb04e7cSWei Wang #define DST_FAKE_RTABLE		0x0010
571eb04e7cSWei Wang #define DST_XFRM_TUNNEL		0x0020
581eb04e7cSWei Wang #define DST_XFRM_QUEUE		0x0040
591eb04e7cSWei Wang #define DST_METADATA		0x0080
60f6b72b62SDavid S. Miller 
6162fa8a84SDavid S. Miller 	short			error;
62f5b0a874SDavid S. Miller 
63f5b0a874SDavid S. Miller 	/* A non-zero value of dst->obsolete forces by-hand validation
64f5b0a874SDavid S. Miller 	 * of the route entry.  Positive values are set by the generic
65f5b0a874SDavid S. Miller 	 * dst layer to indicate that the entry has been forcefully
66f5b0a874SDavid S. Miller 	 * destroyed.
67f5b0a874SDavid S. Miller 	 *
68f5b0a874SDavid S. Miller 	 * Negative values are used by the implementation layer code to
69f5b0a874SDavid S. Miller 	 * force invocation of the dst_ops->check() method.
70f5b0a874SDavid S. Miller 	 */
7162fa8a84SDavid S. Miller 	short			obsolete;
72f5b0a874SDavid S. Miller #define DST_OBSOLETE_NONE	0
73f5b0a874SDavid S. Miller #define DST_OBSOLETE_DEAD	2
74f5b0a874SDavid S. Miller #define DST_OBSOLETE_FORCE_CHK	-1
75ceb33206SDavid S. Miller #define DST_OBSOLETE_KILL	-2
7662fa8a84SDavid S. Miller 	unsigned short		header_len;	/* more space at head required */
7762fa8a84SDavid S. Miller 	unsigned short		trailer_len;	/* space to reserve at tail */
7851ce8bd4SJulian Anastasov 	unsigned short		__pad3;
7951ce8bd4SJulian Anastasov 
80c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
81f1dd9c37SZhang Yanmin 	__u32			tclassid;
825635c10dSEric Dumazet #else
835635c10dSEric Dumazet 	__u32			__pad2;
84f1dd9c37SZhang Yanmin #endif
85f1dd9c37SZhang Yanmin 
86751a587aSJiri Benc #ifdef CONFIG_64BIT
875635c10dSEric Dumazet 	/*
885635c10dSEric Dumazet 	 * Align __refcnt to a 64 bytes alignment
895635c10dSEric Dumazet 	 * (L1_CACHE_SIZE would be too much)
905635c10dSEric Dumazet 	 */
910868e253SJiri Benc 	long			__pad_to_align_refcnt[2];
925635c10dSEric Dumazet #endif
93f1dd9c37SZhang Yanmin 	/*
94f1dd9c37SZhang Yanmin 	 * __refcnt wants to be on a different cache line from
95f1dd9c37SZhang Yanmin 	 * input/output/ops or performance tanks badly
96f1dd9c37SZhang Yanmin 	 */
971e19e02cSEric Dumazet 	atomic_t		__refcnt;	/* client references	*/
981e19e02cSEric Dumazet 	int			__use;
99f1dd9c37SZhang Yanmin 	unsigned long		lastuse;
100751a587aSJiri Benc 	struct lwtunnel_state   *lwtstate;
1011e19e02cSEric Dumazet 	union {
1021e19e02cSEric Dumazet 		struct dst_entry	*next;
1031c31720aSEric Dumazet 		struct rtable __rcu	*rt_next;
1041e19e02cSEric Dumazet 		struct rt6_info		*rt6_next;
105fc766e4cSEric Dumazet 		struct dn_route __rcu	*dn_next;
1061e19e02cSEric Dumazet 	};
1071da177e4SLinus Torvalds };
1081da177e4SLinus Torvalds 
1093fb07dafSEric Dumazet struct dst_metrics {
1103fb07dafSEric Dumazet 	u32		metrics[RTAX_MAX];
111*9620fef2SEric Dumazet 	refcount_t	refcnt;
1123fb07dafSEric Dumazet };
1133fb07dafSEric Dumazet extern const struct dst_metrics dst_default_metrics;
1143fb07dafSEric Dumazet 
115a4023dd0SJoe Perches u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
11662fa8a84SDavid S. Miller 
11762fa8a84SDavid S. Miller #define DST_METRICS_READ_ONLY		0x1UL
1183fb07dafSEric Dumazet #define DST_METRICS_REFCOUNTED		0x2UL
119e5fd387aSMichal Kubeček #define DST_METRICS_FLAGS		0x3UL
12062fa8a84SDavid S. Miller #define __DST_METRICS_PTR(Y)	\
121e5fd387aSMichal Kubeček 	((u32 *)((Y) & ~DST_METRICS_FLAGS))
12262fa8a84SDavid S. Miller #define DST_METRICS_PTR(X)	__DST_METRICS_PTR((X)->_metrics)
12362fa8a84SDavid S. Miller 
12462fa8a84SDavid S. Miller static inline bool dst_metrics_read_only(const struct dst_entry *dst)
12562fa8a84SDavid S. Miller {
12662fa8a84SDavid S. Miller 	return dst->_metrics & DST_METRICS_READ_ONLY;
12762fa8a84SDavid S. Miller }
12862fa8a84SDavid S. Miller 
129a4023dd0SJoe Perches void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old);
13062fa8a84SDavid S. Miller 
13162fa8a84SDavid S. Miller static inline void dst_destroy_metrics_generic(struct dst_entry *dst)
13262fa8a84SDavid S. Miller {
13362fa8a84SDavid S. Miller 	unsigned long val = dst->_metrics;
13462fa8a84SDavid S. Miller 	if (!(val & DST_METRICS_READ_ONLY))
13562fa8a84SDavid S. Miller 		__dst_destroy_metrics_generic(dst, val);
13662fa8a84SDavid S. Miller }
13762fa8a84SDavid S. Miller 
13862fa8a84SDavid S. Miller static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst)
13962fa8a84SDavid S. Miller {
14062fa8a84SDavid S. Miller 	unsigned long p = dst->_metrics;
14162fa8a84SDavid S. Miller 
1421f37070dSStephen Hemminger 	BUG_ON(!p);
1431f37070dSStephen Hemminger 
14462fa8a84SDavid S. Miller 	if (p & DST_METRICS_READ_ONLY)
14562fa8a84SDavid S. Miller 		return dst->ops->cow_metrics(dst, p);
14662fa8a84SDavid S. Miller 	return __DST_METRICS_PTR(p);
14762fa8a84SDavid S. Miller }
14862fa8a84SDavid S. Miller 
14962fa8a84SDavid S. Miller /* This may only be invoked before the entry has reached global
15062fa8a84SDavid S. Miller  * visibility.
15162fa8a84SDavid S. Miller  */
15262fa8a84SDavid S. Miller static inline void dst_init_metrics(struct dst_entry *dst,
15362fa8a84SDavid S. Miller 				    const u32 *src_metrics,
15462fa8a84SDavid S. Miller 				    bool read_only)
15562fa8a84SDavid S. Miller {
15662fa8a84SDavid S. Miller 	dst->_metrics = ((unsigned long) src_metrics) |
15762fa8a84SDavid S. Miller 		(read_only ? DST_METRICS_READ_ONLY : 0);
15862fa8a84SDavid S. Miller }
15962fa8a84SDavid S. Miller 
16062fa8a84SDavid S. Miller static inline void dst_copy_metrics(struct dst_entry *dest, const struct dst_entry *src)
16162fa8a84SDavid S. Miller {
16262fa8a84SDavid S. Miller 	u32 *dst_metrics = dst_metrics_write_ptr(dest);
16362fa8a84SDavid S. Miller 
16462fa8a84SDavid S. Miller 	if (dst_metrics) {
16562fa8a84SDavid S. Miller 		u32 *src_metrics = DST_METRICS_PTR(src);
16662fa8a84SDavid S. Miller 
16762fa8a84SDavid S. Miller 		memcpy(dst_metrics, src_metrics, RTAX_MAX * sizeof(u32));
16862fa8a84SDavid S. Miller 	}
16962fa8a84SDavid S. Miller }
17062fa8a84SDavid S. Miller 
17162fa8a84SDavid S. Miller static inline u32 *dst_metrics_ptr(struct dst_entry *dst)
17262fa8a84SDavid S. Miller {
17362fa8a84SDavid S. Miller 	return DST_METRICS_PTR(dst);
17462fa8a84SDavid S. Miller }
17562fa8a84SDavid S. Miller 
1761da177e4SLinus Torvalds static inline u32
1775170ae82SDavid S. Miller dst_metric_raw(const struct dst_entry *dst, const int metric)
1781da177e4SLinus Torvalds {
17962fa8a84SDavid S. Miller 	u32 *p = DST_METRICS_PTR(dst);
18062fa8a84SDavid S. Miller 
18162fa8a84SDavid S. Miller 	return p[metric-1];
182defb3519SDavid S. Miller }
183defb3519SDavid S. Miller 
1845170ae82SDavid S. Miller static inline u32
1855170ae82SDavid S. Miller dst_metric(const struct dst_entry *dst, const int metric)
1865170ae82SDavid S. Miller {
1870dbaee3bSDavid S. Miller 	WARN_ON_ONCE(metric == RTAX_HOPLIMIT ||
188d33e4553SDavid S. Miller 		     metric == RTAX_ADVMSS ||
189d33e4553SDavid S. Miller 		     metric == RTAX_MTU);
1905170ae82SDavid S. Miller 	return dst_metric_raw(dst, metric);
1915170ae82SDavid S. Miller }
1925170ae82SDavid S. Miller 
1930dbaee3bSDavid S. Miller static inline u32
1940dbaee3bSDavid S. Miller dst_metric_advmss(const struct dst_entry *dst)
1950dbaee3bSDavid S. Miller {
1960dbaee3bSDavid S. Miller 	u32 advmss = dst_metric_raw(dst, RTAX_ADVMSS);
1970dbaee3bSDavid S. Miller 
1980dbaee3bSDavid S. Miller 	if (!advmss)
1990dbaee3bSDavid S. Miller 		advmss = dst->ops->default_advmss(dst);
2000dbaee3bSDavid S. Miller 
2010dbaee3bSDavid S. Miller 	return advmss;
2020dbaee3bSDavid S. Miller }
2030dbaee3bSDavid S. Miller 
204defb3519SDavid S. Miller static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
205defb3519SDavid S. Miller {
20662fa8a84SDavid S. Miller 	u32 *p = dst_metrics_write_ptr(dst);
207defb3519SDavid S. Miller 
20862fa8a84SDavid S. Miller 	if (p)
20962fa8a84SDavid S. Miller 		p[metric-1] = val;
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
212c3a8d947SDaniel Borkmann /* Kernel-internal feature bits that are unallocated in user space. */
213c3a8d947SDaniel Borkmann #define DST_FEATURE_ECN_CA	(1 << 31)
214c3a8d947SDaniel Borkmann 
215c3a8d947SDaniel Borkmann #define DST_FEATURE_MASK	(DST_FEATURE_ECN_CA)
216c3a8d947SDaniel Borkmann #define DST_FEATURE_ECN_MASK	(DST_FEATURE_ECN_CA | RTAX_FEATURE_ECN)
217c3a8d947SDaniel Borkmann 
2180c3adfb8SGilad Ben-Yossef static inline u32
2190c3adfb8SGilad Ben-Yossef dst_feature(const struct dst_entry *dst, u32 feature)
2200c3adfb8SGilad Ben-Yossef {
221bb5b7c11SDavid S. Miller 	return dst_metric(dst, RTAX_FEATURES) & feature;
2220c3adfb8SGilad Ben-Yossef }
2230c3adfb8SGilad Ben-Yossef 
2241da177e4SLinus Torvalds static inline u32 dst_mtu(const struct dst_entry *dst)
2251da177e4SLinus Torvalds {
226618f9bc7SSteffen Klassert 	return dst->ops->mtu(dst);
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
229c1e20f7cSStephen Hemminger /* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
230c1e20f7cSStephen Hemminger static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric)
231c1e20f7cSStephen Hemminger {
232c1e20f7cSStephen Hemminger 	return msecs_to_jiffies(dst_metric(dst, metric));
233c1e20f7cSStephen Hemminger }
234c1e20f7cSStephen Hemminger 
2351da177e4SLinus Torvalds static inline u32
2361da177e4SLinus Torvalds dst_allfrag(const struct dst_entry *dst)
2371da177e4SLinus Torvalds {
2380c3adfb8SGilad Ben-Yossef 	int ret = dst_feature(dst,  RTAX_FEATURE_ALLFRAG);
2391da177e4SLinus Torvalds 	return ret;
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds static inline int
243d33e4553SDavid S. Miller dst_metric_locked(const struct dst_entry *dst, int metric)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds 	return dst_metric(dst, RTAX_LOCK) & (1<<metric);
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds static inline void dst_hold(struct dst_entry *dst)
2491da177e4SLinus Torvalds {
2505635c10dSEric Dumazet 	/*
2515635c10dSEric Dumazet 	 * If your kernel compilation stops here, please check
2525635c10dSEric Dumazet 	 * __pad_to_align_refcnt declaration in struct dst_entry
2535635c10dSEric Dumazet 	 */
2545635c10dSEric Dumazet 	BUILD_BUG_ON(offsetof(struct dst_entry, __refcnt) & 63);
25544ebe791SWei Wang 	WARN_ON(atomic_inc_not_zero(&dst->__refcnt) == 0);
2561da177e4SLinus Torvalds }
2571da177e4SLinus Torvalds 
25803f49f34SPavel Emelyanov static inline void dst_use(struct dst_entry *dst, unsigned long time)
25903f49f34SPavel Emelyanov {
26003f49f34SPavel Emelyanov 	dst_hold(dst);
26103f49f34SPavel Emelyanov 	dst->__use++;
26203f49f34SPavel Emelyanov 	dst->lastuse = time;
26303f49f34SPavel Emelyanov }
26403f49f34SPavel Emelyanov 
2657fee226aSEric Dumazet static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
2667fee226aSEric Dumazet {
2677fee226aSEric Dumazet 	dst->__use++;
2687fee226aSEric Dumazet 	dst->lastuse = time;
2697fee226aSEric Dumazet }
2707fee226aSEric Dumazet 
2717f95e188SEldad Zack static inline struct dst_entry *dst_clone(struct dst_entry *dst)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds 	if (dst)
2741da177e4SLinus Torvalds 		atomic_inc(&dst->__refcnt);
2751da177e4SLinus Torvalds 	return dst;
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
278a4023dd0SJoe Perches void dst_release(struct dst_entry *dst);
2797fee226aSEric Dumazet 
2805f56f409SWei Wang void dst_release_immediate(struct dst_entry *dst);
2815f56f409SWei Wang 
2827fee226aSEric Dumazet static inline void refdst_drop(unsigned long refdst)
2837fee226aSEric Dumazet {
2847fee226aSEric Dumazet 	if (!(refdst & SKB_DST_NOREF))
2857fee226aSEric Dumazet 		dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
2867fee226aSEric Dumazet }
2877fee226aSEric Dumazet 
2887fee226aSEric Dumazet /**
2897fee226aSEric Dumazet  * skb_dst_drop - drops skb dst
2907fee226aSEric Dumazet  * @skb: buffer
2917fee226aSEric Dumazet  *
2927fee226aSEric Dumazet  * Drops dst reference count if a reference was taken.
2937fee226aSEric Dumazet  */
294adf30907SEric Dumazet static inline void skb_dst_drop(struct sk_buff *skb)
295adf30907SEric Dumazet {
2967fee226aSEric Dumazet 	if (skb->_skb_refdst) {
2977fee226aSEric Dumazet 		refdst_drop(skb->_skb_refdst);
2987fee226aSEric Dumazet 		skb->_skb_refdst = 0UL;
2997fee226aSEric Dumazet 	}
3007fee226aSEric Dumazet }
3017fee226aSEric Dumazet 
302e79e2595SJoe Stringer static inline void __skb_dst_copy(struct sk_buff *nskb, unsigned long refdst)
3037fee226aSEric Dumazet {
304e79e2595SJoe Stringer 	nskb->_skb_refdst = refdst;
3057fee226aSEric Dumazet 	if (!(nskb->_skb_refdst & SKB_DST_NOREF))
3067fee226aSEric Dumazet 		dst_clone(skb_dst(nskb));
3077fee226aSEric Dumazet }
3087fee226aSEric Dumazet 
309e79e2595SJoe Stringer static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
310e79e2595SJoe Stringer {
311e79e2595SJoe Stringer 	__skb_dst_copy(nskb, oskb->_skb_refdst);
312e79e2595SJoe Stringer }
313e79e2595SJoe Stringer 
3147fee226aSEric Dumazet /**
3157fee226aSEric Dumazet  * skb_dst_force - makes sure skb dst is refcounted
3167fee226aSEric Dumazet  * @skb: buffer
3177fee226aSEric Dumazet  *
3187fee226aSEric Dumazet  * If dst is not yet refcounted, let's do it
3197fee226aSEric Dumazet  */
3207fee226aSEric Dumazet static inline void skb_dst_force(struct sk_buff *skb)
3217fee226aSEric Dumazet {
3227fee226aSEric Dumazet 	if (skb_dst_is_noref(skb)) {
3237fee226aSEric Dumazet 		WARN_ON(!rcu_read_lock_held());
3247fee226aSEric Dumazet 		skb->_skb_refdst &= ~SKB_DST_NOREF;
3257fee226aSEric Dumazet 		dst_clone(skb_dst(skb));
3267fee226aSEric Dumazet 	}
327adf30907SEric Dumazet }
3281da177e4SLinus Torvalds 
3295037e9efSEric Dumazet /**
3305037e9efSEric Dumazet  * dst_hold_safe - Take a reference on a dst if possible
3315037e9efSEric Dumazet  * @dst: pointer to dst entry
3325037e9efSEric Dumazet  *
3335037e9efSEric Dumazet  * This helper returns false if it could not safely
3345037e9efSEric Dumazet  * take a reference on a dst.
3355037e9efSEric Dumazet  */
3365037e9efSEric Dumazet static inline bool dst_hold_safe(struct dst_entry *dst)
3375037e9efSEric Dumazet {
3385037e9efSEric Dumazet 	return atomic_inc_not_zero(&dst->__refcnt);
3395037e9efSEric Dumazet }
3405037e9efSEric Dumazet 
3415037e9efSEric Dumazet /**
3425037e9efSEric Dumazet  * skb_dst_force_safe - makes sure skb dst is refcounted
3435037e9efSEric Dumazet  * @skb: buffer
3445037e9efSEric Dumazet  *
3455037e9efSEric Dumazet  * If dst is not yet refcounted and not destroyed, grab a ref on it.
3465037e9efSEric Dumazet  */
3475037e9efSEric Dumazet static inline void skb_dst_force_safe(struct sk_buff *skb)
3485037e9efSEric Dumazet {
3495037e9efSEric Dumazet 	if (skb_dst_is_noref(skb)) {
3505037e9efSEric Dumazet 		struct dst_entry *dst = skb_dst(skb);
3515037e9efSEric Dumazet 
3525037e9efSEric Dumazet 		if (!dst_hold_safe(dst))
3535037e9efSEric Dumazet 			dst = NULL;
3545037e9efSEric Dumazet 
3555037e9efSEric Dumazet 		skb->_skb_refdst = (unsigned long)dst;
3565037e9efSEric Dumazet 	}
3575037e9efSEric Dumazet }
3585037e9efSEric Dumazet 
359d19d56ddSEric Dumazet 
360d19d56ddSEric Dumazet /**
361290b895eSEric Dumazet  *	__skb_tunnel_rx - prepare skb for rx reinsert
362290b895eSEric Dumazet  *	@skb: buffer
363290b895eSEric Dumazet  *	@dev: tunnel device
364ea23192eSNicolas Dichtel  *	@net: netns for packet i/o
365290b895eSEric Dumazet  *
366290b895eSEric Dumazet  *	After decapsulation, packet is going to re-enter (netif_rx()) our stack,
367290b895eSEric Dumazet  *	so make some cleanups. (no accounting done)
368290b895eSEric Dumazet  */
369ea23192eSNicolas Dichtel static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
370ea23192eSNicolas Dichtel 				   struct net *net)
371290b895eSEric Dumazet {
372290b895eSEric Dumazet 	skb->dev = dev;
373bdeab991STom Herbert 
374bdeab991STom Herbert 	/*
3757539fadcSTom Herbert 	 * Clear hash so that we can recalulate the hash for the
376bdeab991STom Herbert 	 * encapsulated packet, unless we have already determine the hash
377bdeab991STom Herbert 	 * over the L4 4-tuple.
378bdeab991STom Herbert 	 */
3797539fadcSTom Herbert 	skb_clear_hash_if_not_l4(skb);
380290b895eSEric Dumazet 	skb_set_queue_mapping(skb, 0);
381ea23192eSNicolas Dichtel 	skb_scrub_packet(skb, !net_eq(net, dev_net(dev)));
382290b895eSEric Dumazet }
383290b895eSEric Dumazet 
384290b895eSEric Dumazet /**
385d19d56ddSEric Dumazet  *	skb_tunnel_rx - prepare skb for rx reinsert
386d19d56ddSEric Dumazet  *	@skb: buffer
387d19d56ddSEric Dumazet  *	@dev: tunnel device
388d19d56ddSEric Dumazet  *
389d19d56ddSEric Dumazet  *	After decapsulation, packet is going to re-enter (netif_rx()) our stack,
390d19d56ddSEric Dumazet  *	so make some cleanups, and perform accounting.
391290b895eSEric Dumazet  *	Note: this accounting is not SMP safe.
392d19d56ddSEric Dumazet  */
393ea23192eSNicolas Dichtel static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
394ea23192eSNicolas Dichtel 				 struct net *net)
395d19d56ddSEric Dumazet {
396d19d56ddSEric Dumazet 	/* TODO : stats should be SMP safe */
397d19d56ddSEric Dumazet 	dev->stats.rx_packets++;
398d19d56ddSEric Dumazet 	dev->stats.rx_bytes += skb->len;
399ea23192eSNicolas Dichtel 	__skb_tunnel_rx(skb, dev, net);
400d19d56ddSEric Dumazet }
401d19d56ddSEric Dumazet 
402808c1b69SDaniel Borkmann static inline u32 dst_tclassid(const struct sk_buff *skb)
403808c1b69SDaniel Borkmann {
404808c1b69SDaniel Borkmann #ifdef CONFIG_IP_ROUTE_CLASSID
405808c1b69SDaniel Borkmann 	const struct dst_entry *dst;
406808c1b69SDaniel Borkmann 
407808c1b69SDaniel Borkmann 	dst = skb_dst(skb);
408808c1b69SDaniel Borkmann 	if (dst)
409808c1b69SDaniel Borkmann 		return dst->tclassid;
410808c1b69SDaniel Borkmann #endif
411808c1b69SDaniel Borkmann 	return 0;
412808c1b69SDaniel Borkmann }
413808c1b69SDaniel Borkmann 
414ede2059dSEric W. Biederman int dst_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
415aad88724SEric Dumazet static inline int dst_discard(struct sk_buff *skb)
416aad88724SEric Dumazet {
417ede2059dSEric W. Biederman 	return dst_discard_out(&init_net, skb->sk, skb);
418aad88724SEric Dumazet }
419a4023dd0SJoe Perches void *dst_alloc(struct dst_ops *ops, struct net_device *dev, int initial_ref,
420a4023dd0SJoe Perches 		int initial_obsolete, unsigned short flags);
421f38a9eb1SThomas Graf void dst_init(struct dst_entry *dst, struct dst_ops *ops,
422f38a9eb1SThomas Graf 	      struct net_device *dev, int initial_ref, int initial_obsolete,
423f38a9eb1SThomas Graf 	      unsigned short flags);
424a4023dd0SJoe Perches struct dst_entry *dst_destroy(struct dst_entry *dst);
4254a6ce2b6SWei Wang void dst_dev_put(struct dst_entry *dst);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds static inline void dst_confirm(struct dst_entry *dst)
4281da177e4SLinus Torvalds {
42969cce1d1SDavid S. Miller }
4305110effeSDavid S. Miller 
431d3aaeb38SDavid S. Miller static inline struct neighbour *dst_neigh_lookup(const struct dst_entry *dst, const void *daddr)
432d3aaeb38SDavid S. Miller {
433aaa0c23cSZhouyi Zhou 	struct neighbour *n = dst->ops->neigh_lookup(dst, NULL, daddr);
434aaa0c23cSZhouyi Zhou 	return IS_ERR(n) ? NULL : n;
435f894cbf8SDavid S. Miller }
436f894cbf8SDavid S. Miller 
437f894cbf8SDavid S. Miller static inline struct neighbour *dst_neigh_lookup_skb(const struct dst_entry *dst,
438f894cbf8SDavid S. Miller 						     struct sk_buff *skb)
439f894cbf8SDavid S. Miller {
440aaa0c23cSZhouyi Zhou 	struct neighbour *n =  dst->ops->neigh_lookup(dst, skb, NULL);
441aaa0c23cSZhouyi Zhou 	return IS_ERR(n) ? NULL : n;
442d3aaeb38SDavid S. Miller }
443d3aaeb38SDavid S. Miller 
44463fca65dSJulian Anastasov static inline void dst_confirm_neigh(const struct dst_entry *dst,
44563fca65dSJulian Anastasov 				     const void *daddr)
44663fca65dSJulian Anastasov {
44763fca65dSJulian Anastasov 	if (dst->ops->confirm_neigh)
44863fca65dSJulian Anastasov 		dst->ops->confirm_neigh(dst, daddr);
44963fca65dSJulian Anastasov }
45063fca65dSJulian Anastasov 
4511da177e4SLinus Torvalds static inline void dst_link_failure(struct sk_buff *skb)
4521da177e4SLinus Torvalds {
453adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
4541da177e4SLinus Torvalds 	if (dst && dst->ops && dst->ops->link_failure)
4551da177e4SLinus Torvalds 		dst->ops->link_failure(skb);
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds static inline void dst_set_expires(struct dst_entry *dst, int timeout)
4591da177e4SLinus Torvalds {
4601da177e4SLinus Torvalds 	unsigned long expires = jiffies + timeout;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	if (expires == 0)
4631da177e4SLinus Torvalds 		expires = 1;
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	if (dst->expires == 0 || time_before(expires, dst->expires))
4661da177e4SLinus Torvalds 		dst->expires = expires;
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds /* Output packet to network from transport.  */
47013206b6bSEric W. Biederman static inline int dst_output(struct net *net, struct sock *sk, struct sk_buff *skb)
471aad88724SEric Dumazet {
472ede2059dSEric W. Biederman 	return skb_dst(skb)->output(net, sk, skb);
473aad88724SEric Dumazet }
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds /* Input packet from network to transport.  */
4761da177e4SLinus Torvalds static inline int dst_input(struct sk_buff *skb)
4771da177e4SLinus Torvalds {
478adf30907SEric Dumazet 	return skb_dst(skb)->input(skb);
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
4821da177e4SLinus Torvalds {
4831da177e4SLinus Torvalds 	if (dst->obsolete)
4841da177e4SLinus Torvalds 		dst = dst->ops->check(dst, cookie);
4851da177e4SLinus Torvalds 	return dst;
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
488815f4e57SHerbert Xu /* Flags for xfrm_lookup flags argument. */
489815f4e57SHerbert Xu enum {
49080c0bc9eSDavid S. Miller 	XFRM_LOOKUP_ICMP = 1 << 0,
491b8c203b2SSteffen Klassert 	XFRM_LOOKUP_QUEUE = 1 << 1,
492ac37e251Shuaibin Wang 	XFRM_LOOKUP_KEEP_DST_REF = 1 << 2,
493815f4e57SHerbert Xu };
494815f4e57SHerbert Xu 
4951da177e4SLinus Torvalds struct flowi;
4961da177e4SLinus Torvalds #ifndef CONFIG_XFRM
497452edd59SDavid S. Miller static inline struct dst_entry *xfrm_lookup(struct net *net,
498452edd59SDavid S. Miller 					    struct dst_entry *dst_orig,
4996f9c9615SEric Dumazet 					    const struct flowi *fl,
5006f9c9615SEric Dumazet 					    const struct sock *sk,
501dee9f4bcSDavid S. Miller 					    int flags)
5021da177e4SLinus Torvalds {
503452edd59SDavid S. Miller 	return dst_orig;
5041da177e4SLinus Torvalds }
505e87b3998SVlad Yasevich 
506f92ee619SSteffen Klassert static inline struct dst_entry *xfrm_lookup_route(struct net *net,
507f92ee619SSteffen Klassert 						  struct dst_entry *dst_orig,
508f92ee619SSteffen Klassert 						  const struct flowi *fl,
5096f9c9615SEric Dumazet 						  const struct sock *sk,
510f92ee619SSteffen Klassert 						  int flags)
511f92ee619SSteffen Klassert {
512f92ee619SSteffen Klassert 	return dst_orig;
513f92ee619SSteffen Klassert }
514f92ee619SSteffen Klassert 
515e87b3998SVlad Yasevich static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
516e87b3998SVlad Yasevich {
517e87b3998SVlad Yasevich 	return NULL;
518e87b3998SVlad Yasevich }
519e87b3998SVlad Yasevich 
5201da177e4SLinus Torvalds #else
521a4023dd0SJoe Perches struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
5226f9c9615SEric Dumazet 			      const struct flowi *fl, const struct sock *sk,
523452edd59SDavid S. Miller 			      int flags);
524e87b3998SVlad Yasevich 
525f92ee619SSteffen Klassert struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
5266f9c9615SEric Dumazet 				    const struct flowi *fl, const struct sock *sk,
527f92ee619SSteffen Klassert 				    int flags);
528f92ee619SSteffen Klassert 
529e87b3998SVlad Yasevich /* skb attached with this dst needs transformation if dst->xfrm is valid */
530e87b3998SVlad Yasevich static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
531e87b3998SVlad Yasevich {
532e87b3998SVlad Yasevich 	return dst->xfrm;
533e87b3998SVlad Yasevich }
5341da177e4SLinus Torvalds #endif
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds #endif /* _NET_DST_H */
537