xref: /openbmc/linux/include/net/dst.h (revision 51ce8bd4d17a761e1a90a34a1b5c9b762cce7553)
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>
171da177e4SLinus Torvalds #include <net/neighbour.h>
181da177e4SLinus Torvalds #include <asm/processor.h>
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #define DST_GC_MIN	(HZ/10)
211da177e4SLinus Torvalds #define DST_GC_INC	(HZ/2)
221da177e4SLinus Torvalds #define DST_GC_MAX	(120*HZ)
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds /* Each dst_entry has reference count and sits in some parent list(s).
251da177e4SLinus Torvalds  * When it is removed from parent list, it is "freed" (dst_free).
261da177e4SLinus Torvalds  * After this it enters dead state (dst->obsolete > 0) and if its refcnt
271da177e4SLinus Torvalds  * is zero, it can be destroyed immediately, otherwise it is added
281da177e4SLinus Torvalds  * to gc list and garbage collector periodically checks the refcnt.
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds struct sk_buff;
321da177e4SLinus Torvalds 
33fd2c3ef7SEric Dumazet struct dst_entry {
341e19e02cSEric Dumazet 	struct rcu_head		rcu_head;
351da177e4SLinus Torvalds 	struct dst_entry	*child;
361da177e4SLinus Torvalds 	struct net_device       *dev;
3762fa8a84SDavid S. Miller 	struct  dst_ops	        *ops;
3862fa8a84SDavid S. Miller 	unsigned long		_metrics;
391da177e4SLinus Torvalds 	unsigned long           expires;
40f1dd9c37SZhang Yanmin 	struct dst_entry	*path;
41ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	struct dst_entry	*from;
42def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
431da177e4SLinus Torvalds 	struct xfrm_state	*xfrm;
445635c10dSEric Dumazet #else
455635c10dSEric Dumazet 	void			*__pad1;
46def8b4faSAlexey Dobriyan #endif
471da177e4SLinus Torvalds 	int			(*input)(struct sk_buff *);
48ede2059dSEric W. Biederman 	int			(*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
491da177e4SLinus Torvalds 
505110effeSDavid S. Miller 	unsigned short		flags;
51f6b72b62SDavid S. Miller #define DST_HOST		0x0001
52f6b72b62SDavid S. Miller #define DST_NOXFRM		0x0002
53f6b72b62SDavid S. Miller #define DST_NOPOLICY		0x0004
54f6b72b62SDavid S. Miller #define DST_NOHASH		0x0008
55f6b72b62SDavid S. Miller #define DST_NOCACHE		0x0010
56f6b72b62SDavid S. Miller #define DST_NOCOUNT		0x0020
57e90c1483SHannes Frederic Sowa #define DST_FAKE_RTABLE		0x0040
58e90c1483SHannes Frederic Sowa #define DST_XFRM_TUNNEL		0x0080
59e90c1483SHannes Frederic Sowa #define DST_XFRM_QUEUE		0x0100
60f38a9eb1SThomas Graf #define DST_METADATA		0x0200
61f6b72b62SDavid S. Miller 
6262fa8a84SDavid S. Miller 	short			error;
63f5b0a874SDavid S. Miller 
64f5b0a874SDavid S. Miller 	/* A non-zero value of dst->obsolete forces by-hand validation
65f5b0a874SDavid S. Miller 	 * of the route entry.  Positive values are set by the generic
66f5b0a874SDavid S. Miller 	 * dst layer to indicate that the entry has been forcefully
67f5b0a874SDavid S. Miller 	 * destroyed.
68f5b0a874SDavid S. Miller 	 *
69f5b0a874SDavid S. Miller 	 * Negative values are used by the implementation layer code to
70f5b0a874SDavid S. Miller 	 * force invocation of the dst_ops->check() method.
71f5b0a874SDavid S. Miller 	 */
7262fa8a84SDavid S. Miller 	short			obsolete;
73f5b0a874SDavid S. Miller #define DST_OBSOLETE_NONE	0
74f5b0a874SDavid S. Miller #define DST_OBSOLETE_DEAD	2
75f5b0a874SDavid S. Miller #define DST_OBSOLETE_FORCE_CHK	-1
76ceb33206SDavid S. Miller #define DST_OBSOLETE_KILL	-2
7762fa8a84SDavid S. Miller 	unsigned short		header_len;	/* more space at head required */
7862fa8a84SDavid S. Miller 	unsigned short		trailer_len;	/* space to reserve at tail */
79*51ce8bd4SJulian Anastasov 	unsigned short		__pad3;
80*51ce8bd4SJulian Anastasov 
81c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
82f1dd9c37SZhang Yanmin 	__u32			tclassid;
835635c10dSEric Dumazet #else
845635c10dSEric Dumazet 	__u32			__pad2;
85f1dd9c37SZhang Yanmin #endif
86f1dd9c37SZhang Yanmin 
87751a587aSJiri Benc #ifdef CONFIG_64BIT
885635c10dSEric Dumazet 	/*
895635c10dSEric Dumazet 	 * Align __refcnt to a 64 bytes alignment
905635c10dSEric Dumazet 	 * (L1_CACHE_SIZE would be too much)
915635c10dSEric Dumazet 	 */
920868e253SJiri Benc 	long			__pad_to_align_refcnt[2];
935635c10dSEric Dumazet #endif
94f1dd9c37SZhang Yanmin 	/*
95f1dd9c37SZhang Yanmin 	 * __refcnt wants to be on a different cache line from
96f1dd9c37SZhang Yanmin 	 * input/output/ops or performance tanks badly
97f1dd9c37SZhang Yanmin 	 */
981e19e02cSEric Dumazet 	atomic_t		__refcnt;	/* client references	*/
991e19e02cSEric Dumazet 	int			__use;
100f1dd9c37SZhang Yanmin 	unsigned long		lastuse;
101751a587aSJiri Benc 	struct lwtunnel_state   *lwtstate;
1021e19e02cSEric Dumazet 	union {
1031e19e02cSEric Dumazet 		struct dst_entry	*next;
1041c31720aSEric Dumazet 		struct rtable __rcu	*rt_next;
1051e19e02cSEric Dumazet 		struct rt6_info		*rt6_next;
106fc766e4cSEric Dumazet 		struct dn_route __rcu	*dn_next;
1071e19e02cSEric Dumazet 	};
1081da177e4SLinus Torvalds };
1091da177e4SLinus Torvalds 
110a4023dd0SJoe Perches u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
111a37e6e34SEric Dumazet extern const u32 dst_default_metrics[];
11262fa8a84SDavid S. Miller 
11362fa8a84SDavid S. Miller #define DST_METRICS_READ_ONLY		0x1UL
114e5fd387aSMichal Kubeček #define DST_METRICS_FLAGS		0x3UL
11562fa8a84SDavid S. Miller #define __DST_METRICS_PTR(Y)	\
116e5fd387aSMichal Kubeček 	((u32 *)((Y) & ~DST_METRICS_FLAGS))
11762fa8a84SDavid S. Miller #define DST_METRICS_PTR(X)	__DST_METRICS_PTR((X)->_metrics)
11862fa8a84SDavid S. Miller 
11962fa8a84SDavid S. Miller static inline bool dst_metrics_read_only(const struct dst_entry *dst)
12062fa8a84SDavid S. Miller {
12162fa8a84SDavid S. Miller 	return dst->_metrics & DST_METRICS_READ_ONLY;
12262fa8a84SDavid S. Miller }
12362fa8a84SDavid S. Miller 
124a4023dd0SJoe Perches void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old);
12562fa8a84SDavid S. Miller 
12662fa8a84SDavid S. Miller static inline void dst_destroy_metrics_generic(struct dst_entry *dst)
12762fa8a84SDavid S. Miller {
12862fa8a84SDavid S. Miller 	unsigned long val = dst->_metrics;
12962fa8a84SDavid S. Miller 	if (!(val & DST_METRICS_READ_ONLY))
13062fa8a84SDavid S. Miller 		__dst_destroy_metrics_generic(dst, val);
13162fa8a84SDavid S. Miller }
13262fa8a84SDavid S. Miller 
13362fa8a84SDavid S. Miller static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst)
13462fa8a84SDavid S. Miller {
13562fa8a84SDavid S. Miller 	unsigned long p = dst->_metrics;
13662fa8a84SDavid S. Miller 
1371f37070dSStephen Hemminger 	BUG_ON(!p);
1381f37070dSStephen Hemminger 
13962fa8a84SDavid S. Miller 	if (p & DST_METRICS_READ_ONLY)
14062fa8a84SDavid S. Miller 		return dst->ops->cow_metrics(dst, p);
14162fa8a84SDavid S. Miller 	return __DST_METRICS_PTR(p);
14262fa8a84SDavid S. Miller }
14362fa8a84SDavid S. Miller 
14462fa8a84SDavid S. Miller /* This may only be invoked before the entry has reached global
14562fa8a84SDavid S. Miller  * visibility.
14662fa8a84SDavid S. Miller  */
14762fa8a84SDavid S. Miller static inline void dst_init_metrics(struct dst_entry *dst,
14862fa8a84SDavid S. Miller 				    const u32 *src_metrics,
14962fa8a84SDavid S. Miller 				    bool read_only)
15062fa8a84SDavid S. Miller {
15162fa8a84SDavid S. Miller 	dst->_metrics = ((unsigned long) src_metrics) |
15262fa8a84SDavid S. Miller 		(read_only ? DST_METRICS_READ_ONLY : 0);
15362fa8a84SDavid S. Miller }
15462fa8a84SDavid S. Miller 
15562fa8a84SDavid S. Miller static inline void dst_copy_metrics(struct dst_entry *dest, const struct dst_entry *src)
15662fa8a84SDavid S. Miller {
15762fa8a84SDavid S. Miller 	u32 *dst_metrics = dst_metrics_write_ptr(dest);
15862fa8a84SDavid S. Miller 
15962fa8a84SDavid S. Miller 	if (dst_metrics) {
16062fa8a84SDavid S. Miller 		u32 *src_metrics = DST_METRICS_PTR(src);
16162fa8a84SDavid S. Miller 
16262fa8a84SDavid S. Miller 		memcpy(dst_metrics, src_metrics, RTAX_MAX * sizeof(u32));
16362fa8a84SDavid S. Miller 	}
16462fa8a84SDavid S. Miller }
16562fa8a84SDavid S. Miller 
16662fa8a84SDavid S. Miller static inline u32 *dst_metrics_ptr(struct dst_entry *dst)
16762fa8a84SDavid S. Miller {
16862fa8a84SDavid S. Miller 	return DST_METRICS_PTR(dst);
16962fa8a84SDavid S. Miller }
17062fa8a84SDavid S. Miller 
1711da177e4SLinus Torvalds static inline u32
1725170ae82SDavid S. Miller dst_metric_raw(const struct dst_entry *dst, const int metric)
1731da177e4SLinus Torvalds {
17462fa8a84SDavid S. Miller 	u32 *p = DST_METRICS_PTR(dst);
17562fa8a84SDavid S. Miller 
17662fa8a84SDavid S. Miller 	return p[metric-1];
177defb3519SDavid S. Miller }
178defb3519SDavid S. Miller 
1795170ae82SDavid S. Miller static inline u32
1805170ae82SDavid S. Miller dst_metric(const struct dst_entry *dst, const int metric)
1815170ae82SDavid S. Miller {
1820dbaee3bSDavid S. Miller 	WARN_ON_ONCE(metric == RTAX_HOPLIMIT ||
183d33e4553SDavid S. Miller 		     metric == RTAX_ADVMSS ||
184d33e4553SDavid S. Miller 		     metric == RTAX_MTU);
1855170ae82SDavid S. Miller 	return dst_metric_raw(dst, metric);
1865170ae82SDavid S. Miller }
1875170ae82SDavid S. Miller 
1880dbaee3bSDavid S. Miller static inline u32
1890dbaee3bSDavid S. Miller dst_metric_advmss(const struct dst_entry *dst)
1900dbaee3bSDavid S. Miller {
1910dbaee3bSDavid S. Miller 	u32 advmss = dst_metric_raw(dst, RTAX_ADVMSS);
1920dbaee3bSDavid S. Miller 
1930dbaee3bSDavid S. Miller 	if (!advmss)
1940dbaee3bSDavid S. Miller 		advmss = dst->ops->default_advmss(dst);
1950dbaee3bSDavid S. Miller 
1960dbaee3bSDavid S. Miller 	return advmss;
1970dbaee3bSDavid S. Miller }
1980dbaee3bSDavid S. Miller 
199defb3519SDavid S. Miller static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
200defb3519SDavid S. Miller {
20162fa8a84SDavid S. Miller 	u32 *p = dst_metrics_write_ptr(dst);
202defb3519SDavid S. Miller 
20362fa8a84SDavid S. Miller 	if (p)
20462fa8a84SDavid S. Miller 		p[metric-1] = val;
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
207c3a8d947SDaniel Borkmann /* Kernel-internal feature bits that are unallocated in user space. */
208c3a8d947SDaniel Borkmann #define DST_FEATURE_ECN_CA	(1 << 31)
209c3a8d947SDaniel Borkmann 
210c3a8d947SDaniel Borkmann #define DST_FEATURE_MASK	(DST_FEATURE_ECN_CA)
211c3a8d947SDaniel Borkmann #define DST_FEATURE_ECN_MASK	(DST_FEATURE_ECN_CA | RTAX_FEATURE_ECN)
212c3a8d947SDaniel Borkmann 
2130c3adfb8SGilad Ben-Yossef static inline u32
2140c3adfb8SGilad Ben-Yossef dst_feature(const struct dst_entry *dst, u32 feature)
2150c3adfb8SGilad Ben-Yossef {
216bb5b7c11SDavid S. Miller 	return dst_metric(dst, RTAX_FEATURES) & feature;
2170c3adfb8SGilad Ben-Yossef }
2180c3adfb8SGilad Ben-Yossef 
2191da177e4SLinus Torvalds static inline u32 dst_mtu(const struct dst_entry *dst)
2201da177e4SLinus Torvalds {
221618f9bc7SSteffen Klassert 	return dst->ops->mtu(dst);
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
224c1e20f7cSStephen Hemminger /* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
225c1e20f7cSStephen Hemminger static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric)
226c1e20f7cSStephen Hemminger {
227c1e20f7cSStephen Hemminger 	return msecs_to_jiffies(dst_metric(dst, metric));
228c1e20f7cSStephen Hemminger }
229c1e20f7cSStephen Hemminger 
2301da177e4SLinus Torvalds static inline u32
2311da177e4SLinus Torvalds dst_allfrag(const struct dst_entry *dst)
2321da177e4SLinus Torvalds {
2330c3adfb8SGilad Ben-Yossef 	int ret = dst_feature(dst,  RTAX_FEATURE_ALLFRAG);
2341da177e4SLinus Torvalds 	return ret;
2351da177e4SLinus Torvalds }
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds static inline int
238d33e4553SDavid S. Miller dst_metric_locked(const struct dst_entry *dst, int metric)
2391da177e4SLinus Torvalds {
2401da177e4SLinus Torvalds 	return dst_metric(dst, RTAX_LOCK) & (1<<metric);
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds static inline void dst_hold(struct dst_entry *dst)
2441da177e4SLinus Torvalds {
2455635c10dSEric Dumazet 	/*
2465635c10dSEric Dumazet 	 * If your kernel compilation stops here, please check
2475635c10dSEric Dumazet 	 * __pad_to_align_refcnt declaration in struct dst_entry
2485635c10dSEric Dumazet 	 */
2495635c10dSEric Dumazet 	BUILD_BUG_ON(offsetof(struct dst_entry, __refcnt) & 63);
2501da177e4SLinus Torvalds 	atomic_inc(&dst->__refcnt);
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
25303f49f34SPavel Emelyanov static inline void dst_use(struct dst_entry *dst, unsigned long time)
25403f49f34SPavel Emelyanov {
25503f49f34SPavel Emelyanov 	dst_hold(dst);
25603f49f34SPavel Emelyanov 	dst->__use++;
25703f49f34SPavel Emelyanov 	dst->lastuse = time;
25803f49f34SPavel Emelyanov }
25903f49f34SPavel Emelyanov 
2607fee226aSEric Dumazet static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
2617fee226aSEric Dumazet {
2627fee226aSEric Dumazet 	dst->__use++;
2637fee226aSEric Dumazet 	dst->lastuse = time;
2647fee226aSEric Dumazet }
2657fee226aSEric Dumazet 
2667f95e188SEldad Zack static inline struct dst_entry *dst_clone(struct dst_entry *dst)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds 	if (dst)
2691da177e4SLinus Torvalds 		atomic_inc(&dst->__refcnt);
2701da177e4SLinus Torvalds 	return dst;
2711da177e4SLinus Torvalds }
2721da177e4SLinus Torvalds 
273a4023dd0SJoe Perches void dst_release(struct dst_entry *dst);
2747fee226aSEric Dumazet 
2757fee226aSEric Dumazet static inline void refdst_drop(unsigned long refdst)
2767fee226aSEric Dumazet {
2777fee226aSEric Dumazet 	if (!(refdst & SKB_DST_NOREF))
2787fee226aSEric Dumazet 		dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
2797fee226aSEric Dumazet }
2807fee226aSEric Dumazet 
2817fee226aSEric Dumazet /**
2827fee226aSEric Dumazet  * skb_dst_drop - drops skb dst
2837fee226aSEric Dumazet  * @skb: buffer
2847fee226aSEric Dumazet  *
2857fee226aSEric Dumazet  * Drops dst reference count if a reference was taken.
2867fee226aSEric Dumazet  */
287adf30907SEric Dumazet static inline void skb_dst_drop(struct sk_buff *skb)
288adf30907SEric Dumazet {
2897fee226aSEric Dumazet 	if (skb->_skb_refdst) {
2907fee226aSEric Dumazet 		refdst_drop(skb->_skb_refdst);
2917fee226aSEric Dumazet 		skb->_skb_refdst = 0UL;
2927fee226aSEric Dumazet 	}
2937fee226aSEric Dumazet }
2947fee226aSEric Dumazet 
295e79e2595SJoe Stringer static inline void __skb_dst_copy(struct sk_buff *nskb, unsigned long refdst)
2967fee226aSEric Dumazet {
297e79e2595SJoe Stringer 	nskb->_skb_refdst = refdst;
2987fee226aSEric Dumazet 	if (!(nskb->_skb_refdst & SKB_DST_NOREF))
2997fee226aSEric Dumazet 		dst_clone(skb_dst(nskb));
3007fee226aSEric Dumazet }
3017fee226aSEric Dumazet 
302e79e2595SJoe Stringer static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
303e79e2595SJoe Stringer {
304e79e2595SJoe Stringer 	__skb_dst_copy(nskb, oskb->_skb_refdst);
305e79e2595SJoe Stringer }
306e79e2595SJoe Stringer 
3077fee226aSEric Dumazet /**
3087fee226aSEric Dumazet  * skb_dst_force - makes sure skb dst is refcounted
3097fee226aSEric Dumazet  * @skb: buffer
3107fee226aSEric Dumazet  *
3117fee226aSEric Dumazet  * If dst is not yet refcounted, let's do it
3127fee226aSEric Dumazet  */
3137fee226aSEric Dumazet static inline void skb_dst_force(struct sk_buff *skb)
3147fee226aSEric Dumazet {
3157fee226aSEric Dumazet 	if (skb_dst_is_noref(skb)) {
3167fee226aSEric Dumazet 		WARN_ON(!rcu_read_lock_held());
3177fee226aSEric Dumazet 		skb->_skb_refdst &= ~SKB_DST_NOREF;
3187fee226aSEric Dumazet 		dst_clone(skb_dst(skb));
3197fee226aSEric Dumazet 	}
320adf30907SEric Dumazet }
3211da177e4SLinus Torvalds 
3225037e9efSEric Dumazet /**
3235037e9efSEric Dumazet  * dst_hold_safe - Take a reference on a dst if possible
3245037e9efSEric Dumazet  * @dst: pointer to dst entry
3255037e9efSEric Dumazet  *
3265037e9efSEric Dumazet  * This helper returns false if it could not safely
3275037e9efSEric Dumazet  * take a reference on a dst.
3285037e9efSEric Dumazet  */
3295037e9efSEric Dumazet static inline bool dst_hold_safe(struct dst_entry *dst)
3305037e9efSEric Dumazet {
3315037e9efSEric Dumazet 	if (dst->flags & DST_NOCACHE)
3325037e9efSEric Dumazet 		return atomic_inc_not_zero(&dst->__refcnt);
3335037e9efSEric Dumazet 	dst_hold(dst);
3345037e9efSEric Dumazet 	return true;
3355037e9efSEric Dumazet }
3365037e9efSEric Dumazet 
3375037e9efSEric Dumazet /**
3385037e9efSEric Dumazet  * skb_dst_force_safe - makes sure skb dst is refcounted
3395037e9efSEric Dumazet  * @skb: buffer
3405037e9efSEric Dumazet  *
3415037e9efSEric Dumazet  * If dst is not yet refcounted and not destroyed, grab a ref on it.
3425037e9efSEric Dumazet  */
3435037e9efSEric Dumazet static inline void skb_dst_force_safe(struct sk_buff *skb)
3445037e9efSEric Dumazet {
3455037e9efSEric Dumazet 	if (skb_dst_is_noref(skb)) {
3465037e9efSEric Dumazet 		struct dst_entry *dst = skb_dst(skb);
3475037e9efSEric Dumazet 
3485037e9efSEric Dumazet 		if (!dst_hold_safe(dst))
3495037e9efSEric Dumazet 			dst = NULL;
3505037e9efSEric Dumazet 
3515037e9efSEric Dumazet 		skb->_skb_refdst = (unsigned long)dst;
3525037e9efSEric Dumazet 	}
3535037e9efSEric Dumazet }
3545037e9efSEric Dumazet 
355d19d56ddSEric Dumazet 
356d19d56ddSEric Dumazet /**
357290b895eSEric Dumazet  *	__skb_tunnel_rx - prepare skb for rx reinsert
358290b895eSEric Dumazet  *	@skb: buffer
359290b895eSEric Dumazet  *	@dev: tunnel device
360ea23192eSNicolas Dichtel  *	@net: netns for packet i/o
361290b895eSEric Dumazet  *
362290b895eSEric Dumazet  *	After decapsulation, packet is going to re-enter (netif_rx()) our stack,
363290b895eSEric Dumazet  *	so make some cleanups. (no accounting done)
364290b895eSEric Dumazet  */
365ea23192eSNicolas Dichtel static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
366ea23192eSNicolas Dichtel 				   struct net *net)
367290b895eSEric Dumazet {
368290b895eSEric Dumazet 	skb->dev = dev;
369bdeab991STom Herbert 
370bdeab991STom Herbert 	/*
3717539fadcSTom Herbert 	 * Clear hash so that we can recalulate the hash for the
372bdeab991STom Herbert 	 * encapsulated packet, unless we have already determine the hash
373bdeab991STom Herbert 	 * over the L4 4-tuple.
374bdeab991STom Herbert 	 */
3757539fadcSTom Herbert 	skb_clear_hash_if_not_l4(skb);
376290b895eSEric Dumazet 	skb_set_queue_mapping(skb, 0);
377ea23192eSNicolas Dichtel 	skb_scrub_packet(skb, !net_eq(net, dev_net(dev)));
378290b895eSEric Dumazet }
379290b895eSEric Dumazet 
380290b895eSEric Dumazet /**
381d19d56ddSEric Dumazet  *	skb_tunnel_rx - prepare skb for rx reinsert
382d19d56ddSEric Dumazet  *	@skb: buffer
383d19d56ddSEric Dumazet  *	@dev: tunnel device
384d19d56ddSEric Dumazet  *
385d19d56ddSEric Dumazet  *	After decapsulation, packet is going to re-enter (netif_rx()) our stack,
386d19d56ddSEric Dumazet  *	so make some cleanups, and perform accounting.
387290b895eSEric Dumazet  *	Note: this accounting is not SMP safe.
388d19d56ddSEric Dumazet  */
389ea23192eSNicolas Dichtel static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
390ea23192eSNicolas Dichtel 				 struct net *net)
391d19d56ddSEric Dumazet {
392d19d56ddSEric Dumazet 	/* TODO : stats should be SMP safe */
393d19d56ddSEric Dumazet 	dev->stats.rx_packets++;
394d19d56ddSEric Dumazet 	dev->stats.rx_bytes += skb->len;
395ea23192eSNicolas Dichtel 	__skb_tunnel_rx(skb, dev, net);
396d19d56ddSEric Dumazet }
397d19d56ddSEric Dumazet 
398808c1b69SDaniel Borkmann static inline u32 dst_tclassid(const struct sk_buff *skb)
399808c1b69SDaniel Borkmann {
400808c1b69SDaniel Borkmann #ifdef CONFIG_IP_ROUTE_CLASSID
401808c1b69SDaniel Borkmann 	const struct dst_entry *dst;
402808c1b69SDaniel Borkmann 
403808c1b69SDaniel Borkmann 	dst = skb_dst(skb);
404808c1b69SDaniel Borkmann 	if (dst)
405808c1b69SDaniel Borkmann 		return dst->tclassid;
406808c1b69SDaniel Borkmann #endif
407808c1b69SDaniel Borkmann 	return 0;
408808c1b69SDaniel Borkmann }
409808c1b69SDaniel Borkmann 
410ede2059dSEric W. Biederman int dst_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
411aad88724SEric Dumazet static inline int dst_discard(struct sk_buff *skb)
412aad88724SEric Dumazet {
413ede2059dSEric W. Biederman 	return dst_discard_out(&init_net, skb->sk, skb);
414aad88724SEric Dumazet }
415a4023dd0SJoe Perches void *dst_alloc(struct dst_ops *ops, struct net_device *dev, int initial_ref,
416a4023dd0SJoe Perches 		int initial_obsolete, unsigned short flags);
417f38a9eb1SThomas Graf void dst_init(struct dst_entry *dst, struct dst_ops *ops,
418f38a9eb1SThomas Graf 	      struct net_device *dev, int initial_ref, int initial_obsolete,
419f38a9eb1SThomas Graf 	      unsigned short flags);
420a4023dd0SJoe Perches void __dst_free(struct dst_entry *dst);
421a4023dd0SJoe Perches struct dst_entry *dst_destroy(struct dst_entry *dst);
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds static inline void dst_free(struct dst_entry *dst)
4241da177e4SLinus Torvalds {
425f5b0a874SDavid S. Miller 	if (dst->obsolete > 0)
4261da177e4SLinus Torvalds 		return;
4271da177e4SLinus Torvalds 	if (!atomic_read(&dst->__refcnt)) {
4281da177e4SLinus Torvalds 		dst = dst_destroy(dst);
4291da177e4SLinus Torvalds 		if (!dst)
4301da177e4SLinus Torvalds 			return;
4311da177e4SLinus Torvalds 	}
4321da177e4SLinus Torvalds 	__dst_free(dst);
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds static inline void dst_rcu_free(struct rcu_head *head)
4361da177e4SLinus Torvalds {
4371da177e4SLinus Torvalds 	struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
4381da177e4SLinus Torvalds 	dst_free(dst);
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds static inline void dst_confirm(struct dst_entry *dst)
4421da177e4SLinus Torvalds {
44369cce1d1SDavid S. Miller }
4445110effeSDavid S. Miller 
4455110effeSDavid S. Miller static inline int dst_neigh_output(struct dst_entry *dst, struct neighbour *n,
4465110effeSDavid S. Miller 				   struct sk_buff *skb)
4475110effeSDavid S. Miller {
448425f09abSEric Dumazet 	const struct hh_cache *hh;
4495110effeSDavid S. Miller 
4505110effeSDavid S. Miller 	hh = &n->hh;
4515110effeSDavid S. Miller 	if ((n->nud_state & NUD_CONNECTED) && hh->hh_len)
4525110effeSDavid S. Miller 		return neigh_hh_output(hh, skb);
4535110effeSDavid S. Miller 	else
4545110effeSDavid S. Miller 		return n->output(n, skb);
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds 
457d3aaeb38SDavid S. Miller static inline struct neighbour *dst_neigh_lookup(const struct dst_entry *dst, const void *daddr)
458d3aaeb38SDavid S. Miller {
459aaa0c23cSZhouyi Zhou 	struct neighbour *n = dst->ops->neigh_lookup(dst, NULL, daddr);
460aaa0c23cSZhouyi Zhou 	return IS_ERR(n) ? NULL : n;
461f894cbf8SDavid S. Miller }
462f894cbf8SDavid S. Miller 
463f894cbf8SDavid S. Miller static inline struct neighbour *dst_neigh_lookup_skb(const struct dst_entry *dst,
464f894cbf8SDavid S. Miller 						     struct sk_buff *skb)
465f894cbf8SDavid S. Miller {
466aaa0c23cSZhouyi Zhou 	struct neighbour *n =  dst->ops->neigh_lookup(dst, skb, NULL);
467aaa0c23cSZhouyi Zhou 	return IS_ERR(n) ? NULL : n;
468d3aaeb38SDavid S. Miller }
469d3aaeb38SDavid S. Miller 
47063fca65dSJulian Anastasov static inline void dst_confirm_neigh(const struct dst_entry *dst,
47163fca65dSJulian Anastasov 				     const void *daddr)
47263fca65dSJulian Anastasov {
47363fca65dSJulian Anastasov 	if (dst->ops->confirm_neigh)
47463fca65dSJulian Anastasov 		dst->ops->confirm_neigh(dst, daddr);
47563fca65dSJulian Anastasov }
47663fca65dSJulian Anastasov 
4771da177e4SLinus Torvalds static inline void dst_link_failure(struct sk_buff *skb)
4781da177e4SLinus Torvalds {
479adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
4801da177e4SLinus Torvalds 	if (dst && dst->ops && dst->ops->link_failure)
4811da177e4SLinus Torvalds 		dst->ops->link_failure(skb);
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds static inline void dst_set_expires(struct dst_entry *dst, int timeout)
4851da177e4SLinus Torvalds {
4861da177e4SLinus Torvalds 	unsigned long expires = jiffies + timeout;
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	if (expires == 0)
4891da177e4SLinus Torvalds 		expires = 1;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	if (dst->expires == 0 || time_before(expires, dst->expires))
4921da177e4SLinus Torvalds 		dst->expires = expires;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds /* Output packet to network from transport.  */
49613206b6bSEric W. Biederman static inline int dst_output(struct net *net, struct sock *sk, struct sk_buff *skb)
497aad88724SEric Dumazet {
498ede2059dSEric W. Biederman 	return skb_dst(skb)->output(net, sk, skb);
499aad88724SEric Dumazet }
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds /* Input packet from network to transport.  */
5021da177e4SLinus Torvalds static inline int dst_input(struct sk_buff *skb)
5031da177e4SLinus Torvalds {
504adf30907SEric Dumazet 	return skb_dst(skb)->input(skb);
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
5081da177e4SLinus Torvalds {
5091da177e4SLinus Torvalds 	if (dst->obsolete)
5101da177e4SLinus Torvalds 		dst = dst->ops->check(dst, cookie);
5111da177e4SLinus Torvalds 	return dst;
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
514f38a9eb1SThomas Graf void dst_subsys_init(void);
5151da177e4SLinus Torvalds 
516815f4e57SHerbert Xu /* Flags for xfrm_lookup flags argument. */
517815f4e57SHerbert Xu enum {
51880c0bc9eSDavid S. Miller 	XFRM_LOOKUP_ICMP = 1 << 0,
519b8c203b2SSteffen Klassert 	XFRM_LOOKUP_QUEUE = 1 << 1,
520ac37e251Shuaibin Wang 	XFRM_LOOKUP_KEEP_DST_REF = 1 << 2,
521815f4e57SHerbert Xu };
522815f4e57SHerbert Xu 
5231da177e4SLinus Torvalds struct flowi;
5241da177e4SLinus Torvalds #ifndef CONFIG_XFRM
525452edd59SDavid S. Miller static inline struct dst_entry *xfrm_lookup(struct net *net,
526452edd59SDavid S. Miller 					    struct dst_entry *dst_orig,
5276f9c9615SEric Dumazet 					    const struct flowi *fl,
5286f9c9615SEric Dumazet 					    const struct sock *sk,
529dee9f4bcSDavid S. Miller 					    int flags)
5301da177e4SLinus Torvalds {
531452edd59SDavid S. Miller 	return dst_orig;
5321da177e4SLinus Torvalds }
533e87b3998SVlad Yasevich 
534f92ee619SSteffen Klassert static inline struct dst_entry *xfrm_lookup_route(struct net *net,
535f92ee619SSteffen Klassert 						  struct dst_entry *dst_orig,
536f92ee619SSteffen Klassert 						  const struct flowi *fl,
5376f9c9615SEric Dumazet 						  const struct sock *sk,
538f92ee619SSteffen Klassert 						  int flags)
539f92ee619SSteffen Klassert {
540f92ee619SSteffen Klassert 	return dst_orig;
541f92ee619SSteffen Klassert }
542f92ee619SSteffen Klassert 
543e87b3998SVlad Yasevich static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
544e87b3998SVlad Yasevich {
545e87b3998SVlad Yasevich 	return NULL;
546e87b3998SVlad Yasevich }
547e87b3998SVlad Yasevich 
5481da177e4SLinus Torvalds #else
549a4023dd0SJoe Perches struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
5506f9c9615SEric Dumazet 			      const struct flowi *fl, const struct sock *sk,
551452edd59SDavid S. Miller 			      int flags);
552e87b3998SVlad Yasevich 
553f92ee619SSteffen Klassert struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
5546f9c9615SEric Dumazet 				    const struct flowi *fl, const struct sock *sk,
555f92ee619SSteffen Klassert 				    int flags);
556f92ee619SSteffen Klassert 
557e87b3998SVlad Yasevich /* skb attached with this dst needs transformation if dst->xfrm is valid */
558e87b3998SVlad Yasevich static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
559e87b3998SVlad Yasevich {
560e87b3998SVlad Yasevich 	return dst->xfrm;
561e87b3998SVlad Yasevich }
5621da177e4SLinus Torvalds #endif
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds #endif /* _NET_DST_H */
565