xref: /openbmc/linux/include/net/dst.h (revision 6c1c5097781f563b70a81683ea6fdac21637573b)
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>
191da177e4SLinus Torvalds #include <net/neighbour.h>
201da177e4SLinus Torvalds #include <asm/processor.h>
21e43b2190SBrian Vazquez #include <linux/indirect_call_wrapper.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds struct sk_buff;
241da177e4SLinus Torvalds 
25fd2c3ef7SEric Dumazet struct dst_entry {
2666727145SAlexey Dobriyan 	struct net_device       *dev;
2762fa8a84SDavid S. Miller 	struct  dst_ops	        *ops;
2862fa8a84SDavid S. Miller 	unsigned long		_metrics;
291da177e4SLinus Torvalds 	unsigned long           expires;
30def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
311da177e4SLinus Torvalds 	struct xfrm_state	*xfrm;
325635c10dSEric Dumazet #else
335635c10dSEric Dumazet 	void			*__pad1;
34def8b4faSAlexey Dobriyan #endif
351da177e4SLinus Torvalds 	int			(*input)(struct sk_buff *);
36ede2059dSEric W. Biederman 	int			(*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
371da177e4SLinus Torvalds 
385110effeSDavid S. Miller 	unsigned short		flags;
39f6b72b62SDavid S. Miller #define DST_NOXFRM		0x0002
40f6b72b62SDavid S. Miller #define DST_NOPOLICY		0x0004
411eb04e7cSWei Wang #define DST_NOCOUNT		0x0008
421eb04e7cSWei Wang #define DST_FAKE_RTABLE		0x0010
431eb04e7cSWei Wang #define DST_XFRM_TUNNEL		0x0020
441eb04e7cSWei Wang #define DST_XFRM_QUEUE		0x0040
451eb04e7cSWei Wang #define DST_METADATA		0x0080
46f6b72b62SDavid S. Miller 
47f5b0a874SDavid S. Miller 	/* A non-zero value of dst->obsolete forces by-hand validation
48f5b0a874SDavid S. Miller 	 * of the route entry.  Positive values are set by the generic
49f5b0a874SDavid S. Miller 	 * dst layer to indicate that the entry has been forcefully
50f5b0a874SDavid S. Miller 	 * destroyed.
51f5b0a874SDavid S. Miller 	 *
52f5b0a874SDavid S. Miller 	 * Negative values are used by the implementation layer code to
53f5b0a874SDavid S. Miller 	 * force invocation of the dst_ops->check() method.
54f5b0a874SDavid S. Miller 	 */
5562fa8a84SDavid S. Miller 	short			obsolete;
56f5b0a874SDavid S. Miller #define DST_OBSOLETE_NONE	0
57f5b0a874SDavid S. Miller #define DST_OBSOLETE_DEAD	2
58f5b0a874SDavid S. Miller #define DST_OBSOLETE_FORCE_CHK	-1
59ceb33206SDavid S. Miller #define DST_OBSOLETE_KILL	-2
6062fa8a84SDavid S. Miller 	unsigned short		header_len;	/* more space at head required */
6162fa8a84SDavid S. Miller 	unsigned short		trailer_len;	/* space to reserve at tail */
6251ce8bd4SJulian Anastasov 
63f1dd9c37SZhang Yanmin 	/*
64f1dd9c37SZhang Yanmin 	 * __refcnt wants to be on a different cache line from
65f1dd9c37SZhang Yanmin 	 * input/output/ops or performance tanks badly
66f1dd9c37SZhang Yanmin 	 */
678b207e73SDavid Miller #ifdef CONFIG_64BIT
688b207e73SDavid Miller 	atomic_t		__refcnt;	/* 64-bit offset 64 */
698b207e73SDavid Miller #endif
701e19e02cSEric Dumazet 	int			__use;
71f1dd9c37SZhang Yanmin 	unsigned long		lastuse;
72751a587aSJiri Benc 	struct lwtunnel_state   *lwtstate;
738b207e73SDavid Miller 	struct rcu_head		rcu_head;
748b207e73SDavid Miller 	short			error;
758b207e73SDavid Miller 	short			__pad;
768b207e73SDavid Miller 	__u32			tclassid;
778b207e73SDavid Miller #ifndef CONFIG_64BIT
788b207e73SDavid Miller 	atomic_t		__refcnt;	/* 32-bit offset 64 */
798b207e73SDavid Miller #endif
809038c320SEric Dumazet 	netdevice_tracker	dev_tracker;
811da177e4SLinus Torvalds };
821da177e4SLinus Torvalds 
833fb07dafSEric Dumazet struct dst_metrics {
843fb07dafSEric Dumazet 	u32		metrics[RTAX_MAX];
859620fef2SEric Dumazet 	refcount_t	refcnt;
86258a980dSGeert Uytterhoeven } __aligned(4);		/* Low pointer bits contain DST_METRICS_FLAGS */
873fb07dafSEric Dumazet extern const struct dst_metrics dst_default_metrics;
883fb07dafSEric Dumazet 
89a4023dd0SJoe Perches u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
9062fa8a84SDavid S. Miller 
9162fa8a84SDavid S. Miller #define DST_METRICS_READ_ONLY		0x1UL
923fb07dafSEric Dumazet #define DST_METRICS_REFCOUNTED		0x2UL
93e5fd387aSMichal Kubeček #define DST_METRICS_FLAGS		0x3UL
9462fa8a84SDavid S. Miller #define __DST_METRICS_PTR(Y)	\
95e5fd387aSMichal Kubeček 	((u32 *)((Y) & ~DST_METRICS_FLAGS))
9662fa8a84SDavid S. Miller #define DST_METRICS_PTR(X)	__DST_METRICS_PTR((X)->_metrics)
9762fa8a84SDavid S. Miller 
9862fa8a84SDavid S. Miller static inline bool dst_metrics_read_only(const struct dst_entry *dst)
9962fa8a84SDavid S. Miller {
10062fa8a84SDavid S. Miller 	return dst->_metrics & DST_METRICS_READ_ONLY;
10162fa8a84SDavid S. Miller }
10262fa8a84SDavid S. Miller 
103a4023dd0SJoe Perches void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old);
10462fa8a84SDavid S. Miller 
10562fa8a84SDavid S. Miller static inline void dst_destroy_metrics_generic(struct dst_entry *dst)
10662fa8a84SDavid S. Miller {
10762fa8a84SDavid S. Miller 	unsigned long val = dst->_metrics;
10862fa8a84SDavid S. Miller 	if (!(val & DST_METRICS_READ_ONLY))
10962fa8a84SDavid S. Miller 		__dst_destroy_metrics_generic(dst, val);
11062fa8a84SDavid S. Miller }
11162fa8a84SDavid S. Miller 
11262fa8a84SDavid S. Miller static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst)
11362fa8a84SDavid S. Miller {
11462fa8a84SDavid S. Miller 	unsigned long p = dst->_metrics;
11562fa8a84SDavid S. Miller 
1161f37070dSStephen Hemminger 	BUG_ON(!p);
1171f37070dSStephen Hemminger 
11862fa8a84SDavid S. Miller 	if (p & DST_METRICS_READ_ONLY)
11962fa8a84SDavid S. Miller 		return dst->ops->cow_metrics(dst, p);
12062fa8a84SDavid S. Miller 	return __DST_METRICS_PTR(p);
12162fa8a84SDavid S. Miller }
12262fa8a84SDavid S. Miller 
12362fa8a84SDavid S. Miller /* This may only be invoked before the entry has reached global
12462fa8a84SDavid S. Miller  * visibility.
12562fa8a84SDavid S. Miller  */
12662fa8a84SDavid S. Miller static inline void dst_init_metrics(struct dst_entry *dst,
12762fa8a84SDavid S. Miller 				    const u32 *src_metrics,
12862fa8a84SDavid S. Miller 				    bool read_only)
12962fa8a84SDavid S. Miller {
13062fa8a84SDavid S. Miller 	dst->_metrics = ((unsigned long) src_metrics) |
13162fa8a84SDavid S. Miller 		(read_only ? DST_METRICS_READ_ONLY : 0);
13262fa8a84SDavid S. Miller }
13362fa8a84SDavid S. Miller 
13462fa8a84SDavid S. Miller static inline void dst_copy_metrics(struct dst_entry *dest, const struct dst_entry *src)
13562fa8a84SDavid S. Miller {
13662fa8a84SDavid S. Miller 	u32 *dst_metrics = dst_metrics_write_ptr(dest);
13762fa8a84SDavid S. Miller 
13862fa8a84SDavid S. Miller 	if (dst_metrics) {
13962fa8a84SDavid S. Miller 		u32 *src_metrics = DST_METRICS_PTR(src);
14062fa8a84SDavid S. Miller 
14162fa8a84SDavid S. Miller 		memcpy(dst_metrics, src_metrics, RTAX_MAX * sizeof(u32));
14262fa8a84SDavid S. Miller 	}
14362fa8a84SDavid S. Miller }
14462fa8a84SDavid S. Miller 
14562fa8a84SDavid S. Miller static inline u32 *dst_metrics_ptr(struct dst_entry *dst)
14662fa8a84SDavid S. Miller {
14762fa8a84SDavid S. Miller 	return DST_METRICS_PTR(dst);
14862fa8a84SDavid S. Miller }
14962fa8a84SDavid S. Miller 
1501da177e4SLinus Torvalds static inline u32
1515170ae82SDavid S. Miller dst_metric_raw(const struct dst_entry *dst, const int metric)
1521da177e4SLinus Torvalds {
15362fa8a84SDavid S. Miller 	u32 *p = DST_METRICS_PTR(dst);
15462fa8a84SDavid S. Miller 
15562fa8a84SDavid S. Miller 	return p[metric-1];
156defb3519SDavid S. Miller }
157defb3519SDavid S. Miller 
1585170ae82SDavid S. Miller static inline u32
1595170ae82SDavid S. Miller dst_metric(const struct dst_entry *dst, const int metric)
1605170ae82SDavid S. Miller {
1610dbaee3bSDavid S. Miller 	WARN_ON_ONCE(metric == RTAX_HOPLIMIT ||
162d33e4553SDavid S. Miller 		     metric == RTAX_ADVMSS ||
163d33e4553SDavid S. Miller 		     metric == RTAX_MTU);
1645170ae82SDavid S. Miller 	return dst_metric_raw(dst, metric);
1655170ae82SDavid S. Miller }
1665170ae82SDavid S. Miller 
1670dbaee3bSDavid S. Miller static inline u32
1680dbaee3bSDavid S. Miller dst_metric_advmss(const struct dst_entry *dst)
1690dbaee3bSDavid S. Miller {
1700dbaee3bSDavid S. Miller 	u32 advmss = dst_metric_raw(dst, RTAX_ADVMSS);
1710dbaee3bSDavid S. Miller 
1720dbaee3bSDavid S. Miller 	if (!advmss)
1730dbaee3bSDavid S. Miller 		advmss = dst->ops->default_advmss(dst);
1740dbaee3bSDavid S. Miller 
1750dbaee3bSDavid S. Miller 	return advmss;
1760dbaee3bSDavid S. Miller }
1770dbaee3bSDavid S. Miller 
178defb3519SDavid S. Miller static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
179defb3519SDavid S. Miller {
18062fa8a84SDavid S. Miller 	u32 *p = dst_metrics_write_ptr(dst);
181defb3519SDavid S. Miller 
18262fa8a84SDavid S. Miller 	if (p)
18362fa8a84SDavid S. Miller 		p[metric-1] = val;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
186c3a8d947SDaniel Borkmann /* Kernel-internal feature bits that are unallocated in user space. */
18740f6a2cbSVandana BN #define DST_FEATURE_ECN_CA	(1U << 31)
188c3a8d947SDaniel Borkmann 
189c3a8d947SDaniel Borkmann #define DST_FEATURE_MASK	(DST_FEATURE_ECN_CA)
190c3a8d947SDaniel Borkmann #define DST_FEATURE_ECN_MASK	(DST_FEATURE_ECN_CA | RTAX_FEATURE_ECN)
191c3a8d947SDaniel Borkmann 
1920c3adfb8SGilad Ben-Yossef static inline u32
1930c3adfb8SGilad Ben-Yossef dst_feature(const struct dst_entry *dst, u32 feature)
1940c3adfb8SGilad Ben-Yossef {
195bb5b7c11SDavid S. Miller 	return dst_metric(dst, RTAX_FEATURES) & feature;
1960c3adfb8SGilad Ben-Yossef }
1970c3adfb8SGilad Ben-Yossef 
198f67fbeaeSBrian Vazquez INDIRECT_CALLABLE_DECLARE(unsigned int ip6_mtu(const struct dst_entry *));
199f67fbeaeSBrian Vazquez INDIRECT_CALLABLE_DECLARE(unsigned int ipv4_mtu(const struct dst_entry *));
2001da177e4SLinus Torvalds static inline u32 dst_mtu(const struct dst_entry *dst)
2011da177e4SLinus Torvalds {
202f67fbeaeSBrian Vazquez 	return INDIRECT_CALL_INET(dst->ops->mtu, ip6_mtu, ipv4_mtu, dst);
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
205c1e20f7cSStephen Hemminger /* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
206c1e20f7cSStephen Hemminger static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric)
207c1e20f7cSStephen Hemminger {
208c1e20f7cSStephen Hemminger 	return msecs_to_jiffies(dst_metric(dst, metric));
209c1e20f7cSStephen Hemminger }
210c1e20f7cSStephen Hemminger 
2111da177e4SLinus Torvalds static inline u32
2121da177e4SLinus Torvalds dst_allfrag(const struct dst_entry *dst)
2131da177e4SLinus Torvalds {
2140c3adfb8SGilad Ben-Yossef 	int ret = dst_feature(dst,  RTAX_FEATURE_ALLFRAG);
2151da177e4SLinus Torvalds 	return ret;
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds static inline int
219d33e4553SDavid S. Miller dst_metric_locked(const struct dst_entry *dst, int metric)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	return dst_metric(dst, RTAX_LOCK) & (1 << metric);
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds static inline void dst_hold(struct dst_entry *dst)
2251da177e4SLinus Torvalds {
2265635c10dSEric Dumazet 	/*
2275635c10dSEric Dumazet 	 * If your kernel compilation stops here, please check
2288b207e73SDavid Miller 	 * the placement of __refcnt in struct dst_entry
2295635c10dSEric Dumazet 	 */
2305635c10dSEric Dumazet 	BUILD_BUG_ON(offsetof(struct dst_entry, __refcnt) & 63);
23144ebe791SWei Wang 	WARN_ON(atomic_inc_not_zero(&dst->__refcnt) == 0);
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
2340da4af00SWei Wang static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
23503f49f34SPavel Emelyanov {
23632d18ab1SPaolo Abeni 	if (unlikely(time != dst->lastuse)) {
23703f49f34SPavel Emelyanov 		dst->__use++;
23803f49f34SPavel Emelyanov 		dst->lastuse = time;
23903f49f34SPavel Emelyanov 	}
2400da4af00SWei Wang }
24103f49f34SPavel Emelyanov 
2427f95e188SEldad Zack static inline struct dst_entry *dst_clone(struct dst_entry *dst)
2431da177e4SLinus Torvalds {
2441da177e4SLinus Torvalds 	if (dst)
245222d7dbdSEric Dumazet 		dst_hold(dst);
2461da177e4SLinus Torvalds 	return dst;
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
249a4023dd0SJoe Perches void dst_release(struct dst_entry *dst);
2507fee226aSEric Dumazet 
2515f56f409SWei Wang void dst_release_immediate(struct dst_entry *dst);
2525f56f409SWei Wang 
2537fee226aSEric Dumazet static inline void refdst_drop(unsigned long refdst)
2547fee226aSEric Dumazet {
2557fee226aSEric Dumazet 	if (!(refdst & SKB_DST_NOREF))
2567fee226aSEric Dumazet 		dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
2577fee226aSEric Dumazet }
2587fee226aSEric Dumazet 
2597fee226aSEric Dumazet /**
2607fee226aSEric Dumazet  * skb_dst_drop - drops skb dst
2617fee226aSEric Dumazet  * @skb: buffer
2627fee226aSEric Dumazet  *
2637fee226aSEric Dumazet  * Drops dst reference count if a reference was taken.
2647fee226aSEric Dumazet  */
265adf30907SEric Dumazet static inline void skb_dst_drop(struct sk_buff *skb)
266adf30907SEric Dumazet {
2677fee226aSEric Dumazet 	if (skb->_skb_refdst) {
2687fee226aSEric Dumazet 		refdst_drop(skb->_skb_refdst);
2697fee226aSEric Dumazet 		skb->_skb_refdst = 0UL;
2707fee226aSEric Dumazet 	}
2717fee226aSEric Dumazet }
2727fee226aSEric Dumazet 
273e79e2595SJoe Stringer static inline void __skb_dst_copy(struct sk_buff *nskb, unsigned long refdst)
2747fee226aSEric Dumazet {
2758a886b14SPaolo Abeni 	nskb->slow_gro |= !!refdst;
276e79e2595SJoe Stringer 	nskb->_skb_refdst = refdst;
2777fee226aSEric Dumazet 	if (!(nskb->_skb_refdst & SKB_DST_NOREF))
2787fee226aSEric Dumazet 		dst_clone(skb_dst(nskb));
2797fee226aSEric Dumazet }
2807fee226aSEric Dumazet 
281e79e2595SJoe Stringer static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
282e79e2595SJoe Stringer {
283e79e2595SJoe Stringer 	__skb_dst_copy(nskb, oskb->_skb_refdst);
284e79e2595SJoe Stringer }
285e79e2595SJoe Stringer 
2867fee226aSEric Dumazet /**
2875037e9efSEric Dumazet  * dst_hold_safe - Take a reference on a dst if possible
2885037e9efSEric Dumazet  * @dst: pointer to dst entry
2895037e9efSEric Dumazet  *
2905037e9efSEric Dumazet  * This helper returns false if it could not safely
2915037e9efSEric Dumazet  * take a reference on a dst.
2925037e9efSEric Dumazet  */
2935037e9efSEric Dumazet static inline bool dst_hold_safe(struct dst_entry *dst)
2945037e9efSEric Dumazet {
2955037e9efSEric Dumazet 	return atomic_inc_not_zero(&dst->__refcnt);
2965037e9efSEric Dumazet }
2975037e9efSEric Dumazet 
2985037e9efSEric Dumazet /**
299222d7dbdSEric Dumazet  * skb_dst_force - makes sure skb dst is refcounted
3005037e9efSEric Dumazet  * @skb: buffer
3015037e9efSEric Dumazet  *
3025037e9efSEric Dumazet  * If dst is not yet refcounted and not destroyed, grab a ref on it.
303b60a7738SFlorian Westphal  * Returns true if dst is refcounted.
3045037e9efSEric Dumazet  */
305b60a7738SFlorian Westphal static inline bool skb_dst_force(struct sk_buff *skb)
3065037e9efSEric Dumazet {
3075037e9efSEric Dumazet 	if (skb_dst_is_noref(skb)) {
3085037e9efSEric Dumazet 		struct dst_entry *dst = skb_dst(skb);
3095037e9efSEric Dumazet 
310222d7dbdSEric Dumazet 		WARN_ON(!rcu_read_lock_held());
3115037e9efSEric Dumazet 		if (!dst_hold_safe(dst))
3125037e9efSEric Dumazet 			dst = NULL;
3135037e9efSEric Dumazet 
3145037e9efSEric Dumazet 		skb->_skb_refdst = (unsigned long)dst;
3158a886b14SPaolo Abeni 		skb->slow_gro |= !!dst;
3165037e9efSEric Dumazet 	}
317b60a7738SFlorian Westphal 
318b60a7738SFlorian Westphal 	return skb->_skb_refdst != 0UL;
3195037e9efSEric Dumazet }
3205037e9efSEric Dumazet 
321d19d56ddSEric Dumazet 
322d19d56ddSEric Dumazet /**
323290b895eSEric Dumazet  *	__skb_tunnel_rx - prepare skb for rx reinsert
324290b895eSEric Dumazet  *	@skb: buffer
325290b895eSEric Dumazet  *	@dev: tunnel device
326ea23192eSNicolas Dichtel  *	@net: netns for packet i/o
327290b895eSEric Dumazet  *
328290b895eSEric Dumazet  *	After decapsulation, packet is going to re-enter (netif_rx()) our stack,
329290b895eSEric Dumazet  *	so make some cleanups. (no accounting done)
330290b895eSEric Dumazet  */
331ea23192eSNicolas Dichtel static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
332ea23192eSNicolas Dichtel 				   struct net *net)
333290b895eSEric Dumazet {
334290b895eSEric Dumazet 	skb->dev = dev;
335bdeab991STom Herbert 
336bdeab991STom Herbert 	/*
3377539fadcSTom Herbert 	 * Clear hash so that we can recalulate the hash for the
338bdeab991STom Herbert 	 * encapsulated packet, unless we have already determine the hash
339bdeab991STom Herbert 	 * over the L4 4-tuple.
340bdeab991STom Herbert 	 */
3417539fadcSTom Herbert 	skb_clear_hash_if_not_l4(skb);
342290b895eSEric Dumazet 	skb_set_queue_mapping(skb, 0);
343ea23192eSNicolas Dichtel 	skb_scrub_packet(skb, !net_eq(net, dev_net(dev)));
344290b895eSEric Dumazet }
345290b895eSEric Dumazet 
346290b895eSEric Dumazet /**
347d19d56ddSEric Dumazet  *	skb_tunnel_rx - prepare skb for rx reinsert
348d19d56ddSEric Dumazet  *	@skb: buffer
349d19d56ddSEric Dumazet  *	@dev: tunnel device
3508eb1a859SJonathan Neuschäfer  *	@net: netns for packet i/o
351d19d56ddSEric Dumazet  *
352d19d56ddSEric Dumazet  *	After decapsulation, packet is going to re-enter (netif_rx()) our stack,
353d19d56ddSEric Dumazet  *	so make some cleanups, and perform accounting.
354290b895eSEric Dumazet  *	Note: this accounting is not SMP safe.
355d19d56ddSEric Dumazet  */
356ea23192eSNicolas Dichtel static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
357ea23192eSNicolas Dichtel 				 struct net *net)
358d19d56ddSEric Dumazet {
359*6c1c5097SEric Dumazet 	DEV_STATS_INC(dev, rx_packets);
360*6c1c5097SEric Dumazet 	DEV_STATS_ADD(dev, rx_bytes, skb->len);
361ea23192eSNicolas Dichtel 	__skb_tunnel_rx(skb, dev, net);
362d19d56ddSEric Dumazet }
363d19d56ddSEric Dumazet 
364808c1b69SDaniel Borkmann static inline u32 dst_tclassid(const struct sk_buff *skb)
365808c1b69SDaniel Borkmann {
366808c1b69SDaniel Borkmann #ifdef CONFIG_IP_ROUTE_CLASSID
367808c1b69SDaniel Borkmann 	const struct dst_entry *dst;
368808c1b69SDaniel Borkmann 
369808c1b69SDaniel Borkmann 	dst = skb_dst(skb);
370808c1b69SDaniel Borkmann 	if (dst)
371808c1b69SDaniel Borkmann 		return dst->tclassid;
372808c1b69SDaniel Borkmann #endif
373808c1b69SDaniel Borkmann 	return 0;
374808c1b69SDaniel Borkmann }
375808c1b69SDaniel Borkmann 
376ede2059dSEric W. Biederman int dst_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
377aad88724SEric Dumazet static inline int dst_discard(struct sk_buff *skb)
378aad88724SEric Dumazet {
379ede2059dSEric W. Biederman 	return dst_discard_out(&init_net, skb->sk, skb);
380aad88724SEric Dumazet }
381a4023dd0SJoe Perches void *dst_alloc(struct dst_ops *ops, struct net_device *dev, int initial_ref,
382a4023dd0SJoe Perches 		int initial_obsolete, unsigned short flags);
383f38a9eb1SThomas Graf void dst_init(struct dst_entry *dst, struct dst_ops *ops,
384f38a9eb1SThomas Graf 	      struct net_device *dev, int initial_ref, int initial_obsolete,
385f38a9eb1SThomas Graf 	      unsigned short flags);
386a4023dd0SJoe Perches struct dst_entry *dst_destroy(struct dst_entry *dst);
3874a6ce2b6SWei Wang void dst_dev_put(struct dst_entry *dst);
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds static inline void dst_confirm(struct dst_entry *dst)
3901da177e4SLinus Torvalds {
39169cce1d1SDavid S. Miller }
3925110effeSDavid S. Miller 
393d3aaeb38SDavid S. Miller static inline struct neighbour *dst_neigh_lookup(const struct dst_entry *dst, const void *daddr)
394d3aaeb38SDavid S. Miller {
395aaa0c23cSZhouyi Zhou 	struct neighbour *n = dst->ops->neigh_lookup(dst, NULL, daddr);
396aaa0c23cSZhouyi Zhou 	return IS_ERR(n) ? NULL : n;
397f894cbf8SDavid S. Miller }
398f894cbf8SDavid S. Miller 
399f894cbf8SDavid S. Miller static inline struct neighbour *dst_neigh_lookup_skb(const struct dst_entry *dst,
400f894cbf8SDavid S. Miller 						     struct sk_buff *skb)
401f894cbf8SDavid S. Miller {
4020992d67bSGuillaume Nault 	struct neighbour *n;
403394de110SMartin Varghese 
4040992d67bSGuillaume Nault 	if (WARN_ON_ONCE(!dst->ops->neigh_lookup))
4050992d67bSGuillaume Nault 		return NULL;
4060992d67bSGuillaume Nault 
407394de110SMartin Varghese 	n = dst->ops->neigh_lookup(dst, skb, NULL);
408394de110SMartin Varghese 
409aaa0c23cSZhouyi Zhou 	return IS_ERR(n) ? NULL : n;
410d3aaeb38SDavid S. Miller }
411d3aaeb38SDavid S. Miller 
41263fca65dSJulian Anastasov static inline void dst_confirm_neigh(const struct dst_entry *dst,
41363fca65dSJulian Anastasov 				     const void *daddr)
41463fca65dSJulian Anastasov {
41563fca65dSJulian Anastasov 	if (dst->ops->confirm_neigh)
41663fca65dSJulian Anastasov 		dst->ops->confirm_neigh(dst, daddr);
41763fca65dSJulian Anastasov }
41863fca65dSJulian Anastasov 
4191da177e4SLinus Torvalds static inline void dst_link_failure(struct sk_buff *skb)
4201da177e4SLinus Torvalds {
421adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
4221da177e4SLinus Torvalds 	if (dst && dst->ops && dst->ops->link_failure)
4231da177e4SLinus Torvalds 		dst->ops->link_failure(skb);
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds static inline void dst_set_expires(struct dst_entry *dst, int timeout)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	unsigned long expires = jiffies + timeout;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	if (expires == 0)
4311da177e4SLinus Torvalds 		expires = 1;
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	if (dst->expires == 0 || time_before(expires, dst->expires))
4341da177e4SLinus Torvalds 		dst->expires = expires;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
4376585d7dcSBrian Vazquez INDIRECT_CALLABLE_DECLARE(int ip6_output(struct net *, struct sock *,
4386585d7dcSBrian Vazquez 					 struct sk_buff *));
4396585d7dcSBrian Vazquez INDIRECT_CALLABLE_DECLARE(int ip_output(struct net *, struct sock *,
4406585d7dcSBrian Vazquez 					 struct sk_buff *));
4411da177e4SLinus Torvalds /* Output packet to network from transport.  */
44213206b6bSEric W. Biederman static inline int dst_output(struct net *net, struct sock *sk, struct sk_buff *skb)
443aad88724SEric Dumazet {
4446585d7dcSBrian Vazquez 	return INDIRECT_CALL_INET(skb_dst(skb)->output,
4456585d7dcSBrian Vazquez 				  ip6_output, ip_output,
4466585d7dcSBrian Vazquez 				  net, sk, skb);
447aad88724SEric Dumazet }
4481da177e4SLinus Torvalds 
449e43b2190SBrian Vazquez INDIRECT_CALLABLE_DECLARE(int ip6_input(struct sk_buff *));
450e43b2190SBrian Vazquez INDIRECT_CALLABLE_DECLARE(int ip_local_deliver(struct sk_buff *));
4511da177e4SLinus Torvalds /* Input packet from network to transport.  */
4521da177e4SLinus Torvalds static inline int dst_input(struct sk_buff *skb)
4531da177e4SLinus Torvalds {
454e43b2190SBrian Vazquez 	return INDIRECT_CALL_INET(skb_dst(skb)->input,
455e43b2190SBrian Vazquez 				  ip6_input, ip_local_deliver, skb);
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds 
458bbd807dfSBrian Vazquez INDIRECT_CALLABLE_DECLARE(struct dst_entry *ip6_dst_check(struct dst_entry *,
459bbd807dfSBrian Vazquez 							  u32));
460bbd807dfSBrian Vazquez INDIRECT_CALLABLE_DECLARE(struct dst_entry *ipv4_dst_check(struct dst_entry *,
461bbd807dfSBrian Vazquez 							   u32));
4621da177e4SLinus Torvalds static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
4631da177e4SLinus Torvalds {
4641da177e4SLinus Torvalds 	if (dst->obsolete)
465bbd807dfSBrian Vazquez 		dst = INDIRECT_CALL_INET(dst->ops->check, ip6_dst_check,
466bbd807dfSBrian Vazquez 					 ipv4_dst_check, dst, cookie);
4671da177e4SLinus Torvalds 	return dst;
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
470815f4e57SHerbert Xu /* Flags for xfrm_lookup flags argument. */
471815f4e57SHerbert Xu enum {
47280c0bc9eSDavid S. Miller 	XFRM_LOOKUP_ICMP = 1 << 0,
473b8c203b2SSteffen Klassert 	XFRM_LOOKUP_QUEUE = 1 << 1,
474ac37e251Shuaibin Wang 	XFRM_LOOKUP_KEEP_DST_REF = 1 << 2,
475815f4e57SHerbert Xu };
476815f4e57SHerbert Xu 
4771da177e4SLinus Torvalds struct flowi;
4781da177e4SLinus Torvalds #ifndef CONFIG_XFRM
479452edd59SDavid S. Miller static inline struct dst_entry *xfrm_lookup(struct net *net,
480452edd59SDavid S. Miller 					    struct dst_entry *dst_orig,
4816f9c9615SEric Dumazet 					    const struct flowi *fl,
4826f9c9615SEric Dumazet 					    const struct sock *sk,
483dee9f4bcSDavid S. Miller 					    int flags)
4841da177e4SLinus Torvalds {
485452edd59SDavid S. Miller 	return dst_orig;
4861da177e4SLinus Torvalds }
487e87b3998SVlad Yasevich 
488bc56b334SBenedict Wong static inline struct dst_entry *
489bc56b334SBenedict Wong xfrm_lookup_with_ifid(struct net *net, struct dst_entry *dst_orig,
490bc56b334SBenedict Wong 		      const struct flowi *fl, const struct sock *sk,
491bc56b334SBenedict Wong 		      int flags, u32 if_id)
492bc56b334SBenedict Wong {
493bc56b334SBenedict Wong 	return dst_orig;
494bc56b334SBenedict Wong }
495bc56b334SBenedict Wong 
496f92ee619SSteffen Klassert static inline struct dst_entry *xfrm_lookup_route(struct net *net,
497f92ee619SSteffen Klassert 						  struct dst_entry *dst_orig,
498f92ee619SSteffen Klassert 						  const struct flowi *fl,
4996f9c9615SEric Dumazet 						  const struct sock *sk,
500f92ee619SSteffen Klassert 						  int flags)
501f92ee619SSteffen Klassert {
502f92ee619SSteffen Klassert 	return dst_orig;
503f92ee619SSteffen Klassert }
504f92ee619SSteffen Klassert 
505e87b3998SVlad Yasevich static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
506e87b3998SVlad Yasevich {
507e87b3998SVlad Yasevich 	return NULL;
508e87b3998SVlad Yasevich }
509e87b3998SVlad Yasevich 
5101da177e4SLinus Torvalds #else
511a4023dd0SJoe Perches struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
5126f9c9615SEric Dumazet 			      const struct flowi *fl, const struct sock *sk,
513452edd59SDavid S. Miller 			      int flags);
514e87b3998SVlad Yasevich 
515bc56b334SBenedict Wong struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
516bc56b334SBenedict Wong 					struct dst_entry *dst_orig,
517bc56b334SBenedict Wong 					const struct flowi *fl,
518bc56b334SBenedict Wong 					const struct sock *sk, int flags,
519bc56b334SBenedict Wong 					u32 if_id);
520bc56b334SBenedict Wong 
521f92ee619SSteffen Klassert struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
5226f9c9615SEric Dumazet 				    const struct flowi *fl, const struct sock *sk,
523f92ee619SSteffen Klassert 				    int flags);
524f92ee619SSteffen Klassert 
525e87b3998SVlad Yasevich /* skb attached with this dst needs transformation if dst->xfrm is valid */
526e87b3998SVlad Yasevich static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
527e87b3998SVlad Yasevich {
528e87b3998SVlad Yasevich 	return dst->xfrm;
529e87b3998SVlad Yasevich }
5301da177e4SLinus Torvalds #endif
5311da177e4SLinus Torvalds 
532f15ca723SNicolas Dichtel static inline void skb_dst_update_pmtu(struct sk_buff *skb, u32 mtu)
533f15ca723SNicolas Dichtel {
534f15ca723SNicolas Dichtel 	struct dst_entry *dst = skb_dst(skb);
535f15ca723SNicolas Dichtel 
536f15ca723SNicolas Dichtel 	if (dst && dst->ops->update_pmtu)
537bd085ef6SHangbin Liu 		dst->ops->update_pmtu(dst, NULL, skb, mtu, true);
538f15ca723SNicolas Dichtel }
539f15ca723SNicolas Dichtel 
54007dc35c6SHangbin Liu /* update dst pmtu but not do neighbor confirm */
54107dc35c6SHangbin Liu static inline void skb_dst_update_pmtu_no_confirm(struct sk_buff *skb, u32 mtu)
54207dc35c6SHangbin Liu {
54307dc35c6SHangbin Liu 	struct dst_entry *dst = skb_dst(skb);
54407dc35c6SHangbin Liu 
54507dc35c6SHangbin Liu 	if (dst && dst->ops->update_pmtu)
54607dc35c6SHangbin Liu 		dst->ops->update_pmtu(dst, NULL, skb, mtu, false);
54707dc35c6SHangbin Liu }
54807dc35c6SHangbin Liu 
549c4c877b2SDaniel Borkmann struct dst_entry *dst_blackhole_check(struct dst_entry *dst, u32 cookie);
550c4c877b2SDaniel Borkmann void dst_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
551c4c877b2SDaniel Borkmann 			       struct sk_buff *skb, u32 mtu, bool confirm_neigh);
552c4c877b2SDaniel Borkmann void dst_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
553c4c877b2SDaniel Borkmann 			    struct sk_buff *skb);
554c4c877b2SDaniel Borkmann u32 *dst_blackhole_cow_metrics(struct dst_entry *dst, unsigned long old);
555c4c877b2SDaniel Borkmann struct neighbour *dst_blackhole_neigh_lookup(const struct dst_entry *dst,
556c4c877b2SDaniel Borkmann 					     struct sk_buff *skb,
557c4c877b2SDaniel Borkmann 					     const void *daddr);
558c4c877b2SDaniel Borkmann unsigned int dst_blackhole_mtu(const struct dst_entry *dst);
559c4c877b2SDaniel Borkmann 
5601da177e4SLinus Torvalds #endif /* _NET_DST_H */
561