xref: /openbmc/linux/include/net/dst.h (revision a0073fe18e718a1c815fe8b0120f1ac3c60284ba)
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;
391716a961SGao feng 	union {
401da177e4SLinus Torvalds 		unsigned long           expires;
411716a961SGao feng 		/* point to where the dst_entry copied from */
421716a961SGao feng 		struct dst_entry        *from;
431716a961SGao feng 	};
44f1dd9c37SZhang Yanmin 	struct dst_entry	*path;
4536bdbcaeSDavid S. Miller 	void			*__pad0;
46def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
471da177e4SLinus Torvalds 	struct xfrm_state	*xfrm;
485635c10dSEric Dumazet #else
495635c10dSEric Dumazet 	void			*__pad1;
50def8b4faSAlexey Dobriyan #endif
511da177e4SLinus Torvalds 	int			(*input)(struct sk_buff *);
521da177e4SLinus Torvalds 	int			(*output)(struct sk_buff *);
531da177e4SLinus Torvalds 
545110effeSDavid S. Miller 	unsigned short		flags;
55f6b72b62SDavid S. Miller #define DST_HOST		0x0001
56f6b72b62SDavid S. Miller #define DST_NOXFRM		0x0002
57f6b72b62SDavid S. Miller #define DST_NOPOLICY		0x0004
58f6b72b62SDavid S. Miller #define DST_NOHASH		0x0008
59f6b72b62SDavid S. Miller #define DST_NOCACHE		0x0010
60f6b72b62SDavid S. Miller #define DST_NOCOUNT		0x0020
61e688a604SEric Dumazet #define DST_NOPEER		0x0040
62a881e963SPeter Huang (Peng) #define DST_FAKE_RTABLE		0x0080
630c183379SGao feng #define DST_XFRM_TUNNEL		0x0100
64*a0073fe1SSteffen Klassert #define DST_XFRM_QUEUE		0x0200
65f6b72b62SDavid S. Miller 
665110effeSDavid S. Miller 	unsigned short		pending_confirm;
675110effeSDavid S. Miller 
6862fa8a84SDavid S. Miller 	short			error;
69f5b0a874SDavid S. Miller 
70f5b0a874SDavid S. Miller 	/* A non-zero value of dst->obsolete forces by-hand validation
71f5b0a874SDavid S. Miller 	 * of the route entry.  Positive values are set by the generic
72f5b0a874SDavid S. Miller 	 * dst layer to indicate that the entry has been forcefully
73f5b0a874SDavid S. Miller 	 * destroyed.
74f5b0a874SDavid S. Miller 	 *
75f5b0a874SDavid S. Miller 	 * Negative values are used by the implementation layer code to
76f5b0a874SDavid S. Miller 	 * force invocation of the dst_ops->check() method.
77f5b0a874SDavid S. Miller 	 */
7862fa8a84SDavid S. Miller 	short			obsolete;
79f5b0a874SDavid S. Miller #define DST_OBSOLETE_NONE	0
80f5b0a874SDavid S. Miller #define DST_OBSOLETE_DEAD	2
81f5b0a874SDavid S. Miller #define DST_OBSOLETE_FORCE_CHK	-1
82ceb33206SDavid S. Miller #define DST_OBSOLETE_KILL	-2
8362fa8a84SDavid S. Miller 	unsigned short		header_len;	/* more space at head required */
8462fa8a84SDavid S. Miller 	unsigned short		trailer_len;	/* space to reserve at tail */
85c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
86f1dd9c37SZhang Yanmin 	__u32			tclassid;
875635c10dSEric Dumazet #else
885635c10dSEric Dumazet 	__u32			__pad2;
89f1dd9c37SZhang Yanmin #endif
90f1dd9c37SZhang Yanmin 
915635c10dSEric Dumazet 	/*
925635c10dSEric Dumazet 	 * Align __refcnt to a 64 bytes alignment
935635c10dSEric Dumazet 	 * (L1_CACHE_SIZE would be too much)
945635c10dSEric Dumazet 	 */
955635c10dSEric Dumazet #ifdef CONFIG_64BIT
96f6b72b62SDavid S. Miller 	long			__pad_to_align_refcnt[2];
975635c10dSEric Dumazet #endif
98f1dd9c37SZhang Yanmin 	/*
99f1dd9c37SZhang Yanmin 	 * __refcnt wants to be on a different cache line from
100f1dd9c37SZhang Yanmin 	 * input/output/ops or performance tanks badly
101f1dd9c37SZhang Yanmin 	 */
1021e19e02cSEric Dumazet 	atomic_t		__refcnt;	/* client references	*/
1031e19e02cSEric Dumazet 	int			__use;
104f1dd9c37SZhang Yanmin 	unsigned long		lastuse;
1051e19e02cSEric Dumazet 	union {
1061e19e02cSEric Dumazet 		struct dst_entry	*next;
1071c31720aSEric Dumazet 		struct rtable __rcu	*rt_next;
1081e19e02cSEric Dumazet 		struct rt6_info		*rt6_next;
109fc766e4cSEric Dumazet 		struct dn_route __rcu	*dn_next;
1101e19e02cSEric Dumazet 	};
1111da177e4SLinus Torvalds };
1121da177e4SLinus Torvalds 
11362fa8a84SDavid S. Miller extern u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
114a37e6e34SEric Dumazet extern const u32 dst_default_metrics[];
11562fa8a84SDavid S. Miller 
11662fa8a84SDavid S. Miller #define DST_METRICS_READ_ONLY	0x1UL
11762fa8a84SDavid S. Miller #define __DST_METRICS_PTR(Y)	\
11862fa8a84SDavid S. Miller 	((u32 *)((Y) & ~DST_METRICS_READ_ONLY))
11962fa8a84SDavid S. Miller #define DST_METRICS_PTR(X)	__DST_METRICS_PTR((X)->_metrics)
12062fa8a84SDavid S. Miller 
12162fa8a84SDavid S. Miller static inline bool dst_metrics_read_only(const struct dst_entry *dst)
12262fa8a84SDavid S. Miller {
12362fa8a84SDavid S. Miller 	return dst->_metrics & DST_METRICS_READ_ONLY;
12462fa8a84SDavid S. Miller }
12562fa8a84SDavid S. Miller 
12662fa8a84SDavid S. Miller extern void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old);
12762fa8a84SDavid S. Miller 
12862fa8a84SDavid S. Miller static inline void dst_destroy_metrics_generic(struct dst_entry *dst)
12962fa8a84SDavid S. Miller {
13062fa8a84SDavid S. Miller 	unsigned long val = dst->_metrics;
13162fa8a84SDavid S. Miller 	if (!(val & DST_METRICS_READ_ONLY))
13262fa8a84SDavid S. Miller 		__dst_destroy_metrics_generic(dst, val);
13362fa8a84SDavid S. Miller }
13462fa8a84SDavid S. Miller 
13562fa8a84SDavid S. Miller static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst)
13662fa8a84SDavid S. Miller {
13762fa8a84SDavid S. Miller 	unsigned long p = dst->_metrics;
13862fa8a84SDavid S. Miller 
1391f37070dSStephen Hemminger 	BUG_ON(!p);
1401f37070dSStephen Hemminger 
14162fa8a84SDavid S. Miller 	if (p & DST_METRICS_READ_ONLY)
14262fa8a84SDavid S. Miller 		return dst->ops->cow_metrics(dst, p);
14362fa8a84SDavid S. Miller 	return __DST_METRICS_PTR(p);
14462fa8a84SDavid S. Miller }
14562fa8a84SDavid S. Miller 
14662fa8a84SDavid S. Miller /* This may only be invoked before the entry has reached global
14762fa8a84SDavid S. Miller  * visibility.
14862fa8a84SDavid S. Miller  */
14962fa8a84SDavid S. Miller static inline void dst_init_metrics(struct dst_entry *dst,
15062fa8a84SDavid S. Miller 				    const u32 *src_metrics,
15162fa8a84SDavid S. Miller 				    bool read_only)
15262fa8a84SDavid S. Miller {
15362fa8a84SDavid S. Miller 	dst->_metrics = ((unsigned long) src_metrics) |
15462fa8a84SDavid S. Miller 		(read_only ? DST_METRICS_READ_ONLY : 0);
15562fa8a84SDavid S. Miller }
15662fa8a84SDavid S. Miller 
15762fa8a84SDavid S. Miller static inline void dst_copy_metrics(struct dst_entry *dest, const struct dst_entry *src)
15862fa8a84SDavid S. Miller {
15962fa8a84SDavid S. Miller 	u32 *dst_metrics = dst_metrics_write_ptr(dest);
16062fa8a84SDavid S. Miller 
16162fa8a84SDavid S. Miller 	if (dst_metrics) {
16262fa8a84SDavid S. Miller 		u32 *src_metrics = DST_METRICS_PTR(src);
16362fa8a84SDavid S. Miller 
16462fa8a84SDavid S. Miller 		memcpy(dst_metrics, src_metrics, RTAX_MAX * sizeof(u32));
16562fa8a84SDavid S. Miller 	}
16662fa8a84SDavid S. Miller }
16762fa8a84SDavid S. Miller 
16862fa8a84SDavid S. Miller static inline u32 *dst_metrics_ptr(struct dst_entry *dst)
16962fa8a84SDavid S. Miller {
17062fa8a84SDavid S. Miller 	return DST_METRICS_PTR(dst);
17162fa8a84SDavid S. Miller }
17262fa8a84SDavid S. Miller 
1731da177e4SLinus Torvalds static inline u32
1745170ae82SDavid S. Miller dst_metric_raw(const struct dst_entry *dst, const int metric)
1751da177e4SLinus Torvalds {
17662fa8a84SDavid S. Miller 	u32 *p = DST_METRICS_PTR(dst);
17762fa8a84SDavid S. Miller 
17862fa8a84SDavid S. Miller 	return p[metric-1];
179defb3519SDavid S. Miller }
180defb3519SDavid S. Miller 
1815170ae82SDavid S. Miller static inline u32
1825170ae82SDavid S. Miller dst_metric(const struct dst_entry *dst, const int metric)
1835170ae82SDavid S. Miller {
1840dbaee3bSDavid S. Miller 	WARN_ON_ONCE(metric == RTAX_HOPLIMIT ||
185d33e4553SDavid S. Miller 		     metric == RTAX_ADVMSS ||
186d33e4553SDavid S. Miller 		     metric == RTAX_MTU);
1875170ae82SDavid S. Miller 	return dst_metric_raw(dst, metric);
1885170ae82SDavid S. Miller }
1895170ae82SDavid S. Miller 
1900dbaee3bSDavid S. Miller static inline u32
1910dbaee3bSDavid S. Miller dst_metric_advmss(const struct dst_entry *dst)
1920dbaee3bSDavid S. Miller {
1930dbaee3bSDavid S. Miller 	u32 advmss = dst_metric_raw(dst, RTAX_ADVMSS);
1940dbaee3bSDavid S. Miller 
1950dbaee3bSDavid S. Miller 	if (!advmss)
1960dbaee3bSDavid S. Miller 		advmss = dst->ops->default_advmss(dst);
1970dbaee3bSDavid S. Miller 
1980dbaee3bSDavid S. Miller 	return advmss;
1990dbaee3bSDavid S. Miller }
2000dbaee3bSDavid S. Miller 
201defb3519SDavid S. Miller static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
202defb3519SDavid S. Miller {
20362fa8a84SDavid S. Miller 	u32 *p = dst_metrics_write_ptr(dst);
204defb3519SDavid S. Miller 
20562fa8a84SDavid S. Miller 	if (p)
20662fa8a84SDavid S. Miller 		p[metric-1] = val;
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
2090c3adfb8SGilad Ben-Yossef static inline u32
2100c3adfb8SGilad Ben-Yossef dst_feature(const struct dst_entry *dst, u32 feature)
2110c3adfb8SGilad Ben-Yossef {
212bb5b7c11SDavid S. Miller 	return dst_metric(dst, RTAX_FEATURES) & feature;
2130c3adfb8SGilad Ben-Yossef }
2140c3adfb8SGilad Ben-Yossef 
2151da177e4SLinus Torvalds static inline u32 dst_mtu(const struct dst_entry *dst)
2161da177e4SLinus Torvalds {
217618f9bc7SSteffen Klassert 	return dst->ops->mtu(dst);
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
220c1e20f7cSStephen Hemminger /* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
221c1e20f7cSStephen Hemminger static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric)
222c1e20f7cSStephen Hemminger {
223c1e20f7cSStephen Hemminger 	return msecs_to_jiffies(dst_metric(dst, metric));
224c1e20f7cSStephen Hemminger }
225c1e20f7cSStephen Hemminger 
2261da177e4SLinus Torvalds static inline u32
2271da177e4SLinus Torvalds dst_allfrag(const struct dst_entry *dst)
2281da177e4SLinus Torvalds {
2290c3adfb8SGilad Ben-Yossef 	int ret = dst_feature(dst,  RTAX_FEATURE_ALLFRAG);
2301da177e4SLinus Torvalds 	return ret;
2311da177e4SLinus Torvalds }
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds static inline int
234d33e4553SDavid S. Miller dst_metric_locked(const struct dst_entry *dst, int metric)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds 	return dst_metric(dst, RTAX_LOCK) & (1<<metric);
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds static inline void dst_hold(struct dst_entry *dst)
2401da177e4SLinus Torvalds {
2415635c10dSEric Dumazet 	/*
2425635c10dSEric Dumazet 	 * If your kernel compilation stops here, please check
2435635c10dSEric Dumazet 	 * __pad_to_align_refcnt declaration in struct dst_entry
2445635c10dSEric Dumazet 	 */
2455635c10dSEric Dumazet 	BUILD_BUG_ON(offsetof(struct dst_entry, __refcnt) & 63);
2461da177e4SLinus Torvalds 	atomic_inc(&dst->__refcnt);
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
24903f49f34SPavel Emelyanov static inline void dst_use(struct dst_entry *dst, unsigned long time)
25003f49f34SPavel Emelyanov {
25103f49f34SPavel Emelyanov 	dst_hold(dst);
25203f49f34SPavel Emelyanov 	dst->__use++;
25303f49f34SPavel Emelyanov 	dst->lastuse = time;
25403f49f34SPavel Emelyanov }
25503f49f34SPavel Emelyanov 
2567fee226aSEric Dumazet static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
2577fee226aSEric Dumazet {
2587fee226aSEric Dumazet 	dst->__use++;
2597fee226aSEric Dumazet 	dst->lastuse = time;
2607fee226aSEric Dumazet }
2617fee226aSEric Dumazet 
2627f95e188SEldad Zack static inline struct dst_entry *dst_clone(struct dst_entry *dst)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds 	if (dst)
2651da177e4SLinus Torvalds 		atomic_inc(&dst->__refcnt);
2661da177e4SLinus Torvalds 	return dst;
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds 
2698d330868SIlpo Järvinen extern void dst_release(struct dst_entry *dst);
2707fee226aSEric Dumazet 
2717fee226aSEric Dumazet static inline void refdst_drop(unsigned long refdst)
2727fee226aSEric Dumazet {
2737fee226aSEric Dumazet 	if (!(refdst & SKB_DST_NOREF))
2747fee226aSEric Dumazet 		dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
2757fee226aSEric Dumazet }
2767fee226aSEric Dumazet 
2777fee226aSEric Dumazet /**
2787fee226aSEric Dumazet  * skb_dst_drop - drops skb dst
2797fee226aSEric Dumazet  * @skb: buffer
2807fee226aSEric Dumazet  *
2817fee226aSEric Dumazet  * Drops dst reference count if a reference was taken.
2827fee226aSEric Dumazet  */
283adf30907SEric Dumazet static inline void skb_dst_drop(struct sk_buff *skb)
284adf30907SEric Dumazet {
2857fee226aSEric Dumazet 	if (skb->_skb_refdst) {
2867fee226aSEric Dumazet 		refdst_drop(skb->_skb_refdst);
2877fee226aSEric Dumazet 		skb->_skb_refdst = 0UL;
2887fee226aSEric Dumazet 	}
2897fee226aSEric Dumazet }
2907fee226aSEric Dumazet 
2917fee226aSEric Dumazet static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
2927fee226aSEric Dumazet {
2937fee226aSEric Dumazet 	nskb->_skb_refdst = oskb->_skb_refdst;
2947fee226aSEric Dumazet 	if (!(nskb->_skb_refdst & SKB_DST_NOREF))
2957fee226aSEric Dumazet 		dst_clone(skb_dst(nskb));
2967fee226aSEric Dumazet }
2977fee226aSEric Dumazet 
2987fee226aSEric Dumazet /**
2997fee226aSEric Dumazet  * skb_dst_force - makes sure skb dst is refcounted
3007fee226aSEric Dumazet  * @skb: buffer
3017fee226aSEric Dumazet  *
3027fee226aSEric Dumazet  * If dst is not yet refcounted, let's do it
3037fee226aSEric Dumazet  */
3047fee226aSEric Dumazet static inline void skb_dst_force(struct sk_buff *skb)
3057fee226aSEric Dumazet {
3067fee226aSEric Dumazet 	if (skb_dst_is_noref(skb)) {
3077fee226aSEric Dumazet 		WARN_ON(!rcu_read_lock_held());
3087fee226aSEric Dumazet 		skb->_skb_refdst &= ~SKB_DST_NOREF;
3097fee226aSEric Dumazet 		dst_clone(skb_dst(skb));
3107fee226aSEric Dumazet 	}
311adf30907SEric Dumazet }
3121da177e4SLinus Torvalds 
313d19d56ddSEric Dumazet 
314d19d56ddSEric Dumazet /**
315290b895eSEric Dumazet  *	__skb_tunnel_rx - prepare skb for rx reinsert
316290b895eSEric Dumazet  *	@skb: buffer
317290b895eSEric Dumazet  *	@dev: tunnel device
318290b895eSEric Dumazet  *
319290b895eSEric Dumazet  *	After decapsulation, packet is going to re-enter (netif_rx()) our stack,
320290b895eSEric Dumazet  *	so make some cleanups. (no accounting done)
321290b895eSEric Dumazet  */
322290b895eSEric Dumazet static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev)
323290b895eSEric Dumazet {
324290b895eSEric Dumazet 	skb->dev = dev;
325bdeab991STom Herbert 
326bdeab991STom Herbert 	/*
327bdeab991STom Herbert 	 * Clear rxhash so that we can recalulate the hash for the
328bdeab991STom Herbert 	 * encapsulated packet, unless we have already determine the hash
329bdeab991STom Herbert 	 * over the L4 4-tuple.
330bdeab991STom Herbert 	 */
331bdeab991STom Herbert 	if (!skb->l4_rxhash)
332290b895eSEric Dumazet 		skb->rxhash = 0;
333290b895eSEric Dumazet 	skb_set_queue_mapping(skb, 0);
334290b895eSEric Dumazet 	skb_dst_drop(skb);
335290b895eSEric Dumazet 	nf_reset(skb);
336290b895eSEric Dumazet }
337290b895eSEric Dumazet 
338290b895eSEric Dumazet /**
339d19d56ddSEric Dumazet  *	skb_tunnel_rx - prepare skb for rx reinsert
340d19d56ddSEric Dumazet  *	@skb: buffer
341d19d56ddSEric Dumazet  *	@dev: tunnel device
342d19d56ddSEric Dumazet  *
343d19d56ddSEric Dumazet  *	After decapsulation, packet is going to re-enter (netif_rx()) our stack,
344d19d56ddSEric Dumazet  *	so make some cleanups, and perform accounting.
345290b895eSEric Dumazet  *	Note: this accounting is not SMP safe.
346d19d56ddSEric Dumazet  */
347d19d56ddSEric Dumazet static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev)
348d19d56ddSEric Dumazet {
349d19d56ddSEric Dumazet 	/* TODO : stats should be SMP safe */
350d19d56ddSEric Dumazet 	dev->stats.rx_packets++;
351d19d56ddSEric Dumazet 	dev->stats.rx_bytes += skb->len;
352290b895eSEric Dumazet 	__skb_tunnel_rx(skb, dev);
353d19d56ddSEric Dumazet }
354d19d56ddSEric Dumazet 
3551da177e4SLinus Torvalds /* Children define the path of the packet through the
3561da177e4SLinus Torvalds  * Linux networking.  Thus, destinations are stackable.
3571da177e4SLinus Torvalds  */
3581da177e4SLinus Torvalds 
3598764ab2cSSteffen Klassert static inline struct dst_entry *skb_dst_pop(struct sk_buff *skb)
3601da177e4SLinus Torvalds {
361e433430aSSteffen Klassert 	struct dst_entry *child = dst_clone(skb_dst(skb)->child);
3621da177e4SLinus Torvalds 
3638764ab2cSSteffen Klassert 	skb_dst_drop(skb);
3641da177e4SLinus Torvalds 	return child;
3651da177e4SLinus Torvalds }
3661da177e4SLinus Torvalds 
367352e512cSHerbert Xu extern int dst_discard(struct sk_buff *skb);
3685c1e6aa3SDavid S. Miller extern void *dst_alloc(struct dst_ops *ops, struct net_device *dev,
3695110effeSDavid S. Miller 		       int initial_ref, int initial_obsolete,
3705110effeSDavid S. Miller 		       unsigned short flags);
3711da177e4SLinus Torvalds extern void __dst_free(struct dst_entry *dst);
3721da177e4SLinus Torvalds extern struct dst_entry *dst_destroy(struct dst_entry *dst);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds static inline void dst_free(struct dst_entry *dst)
3751da177e4SLinus Torvalds {
376f5b0a874SDavid S. Miller 	if (dst->obsolete > 0)
3771da177e4SLinus Torvalds 		return;
3781da177e4SLinus Torvalds 	if (!atomic_read(&dst->__refcnt)) {
3791da177e4SLinus Torvalds 		dst = dst_destroy(dst);
3801da177e4SLinus Torvalds 		if (!dst)
3811da177e4SLinus Torvalds 			return;
3821da177e4SLinus Torvalds 	}
3831da177e4SLinus Torvalds 	__dst_free(dst);
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds static inline void dst_rcu_free(struct rcu_head *head)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
3891da177e4SLinus Torvalds 	dst_free(dst);
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds static inline void dst_confirm(struct dst_entry *dst)
3931da177e4SLinus Torvalds {
3945110effeSDavid S. Miller 	dst->pending_confirm = 1;
39569cce1d1SDavid S. Miller }
3965110effeSDavid S. Miller 
3975110effeSDavid S. Miller static inline int dst_neigh_output(struct dst_entry *dst, struct neighbour *n,
3985110effeSDavid S. Miller 				   struct sk_buff *skb)
3995110effeSDavid S. Miller {
400425f09abSEric Dumazet 	const struct hh_cache *hh;
4015110effeSDavid S. Miller 
402425f09abSEric Dumazet 	if (dst->pending_confirm) {
403425f09abSEric Dumazet 		unsigned long now = jiffies;
404425f09abSEric Dumazet 
4055110effeSDavid S. Miller 		dst->pending_confirm = 0;
406425f09abSEric Dumazet 		/* avoid dirtying neighbour */
407425f09abSEric Dumazet 		if (n->confirmed != now)
408425f09abSEric Dumazet 			n->confirmed = now;
4095110effeSDavid S. Miller 	}
4105110effeSDavid S. Miller 
4115110effeSDavid S. Miller 	hh = &n->hh;
4125110effeSDavid S. Miller 	if ((n->nud_state & NUD_CONNECTED) && hh->hh_len)
4135110effeSDavid S. Miller 		return neigh_hh_output(hh, skb);
4145110effeSDavid S. Miller 	else
4155110effeSDavid S. Miller 		return n->output(n, skb);
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
418d3aaeb38SDavid S. Miller static inline struct neighbour *dst_neigh_lookup(const struct dst_entry *dst, const void *daddr)
419d3aaeb38SDavid S. Miller {
420f894cbf8SDavid S. Miller 	return dst->ops->neigh_lookup(dst, NULL, daddr);
421f894cbf8SDavid S. Miller }
422f894cbf8SDavid S. Miller 
423f894cbf8SDavid S. Miller static inline struct neighbour *dst_neigh_lookup_skb(const struct dst_entry *dst,
424f894cbf8SDavid S. Miller 						     struct sk_buff *skb)
425f894cbf8SDavid S. Miller {
426f894cbf8SDavid S. Miller 	return dst->ops->neigh_lookup(dst, skb, NULL);
427d3aaeb38SDavid S. Miller }
428d3aaeb38SDavid S. Miller 
4291da177e4SLinus Torvalds static inline void dst_link_failure(struct sk_buff *skb)
4301da177e4SLinus Torvalds {
431adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
4321da177e4SLinus Torvalds 	if (dst && dst->ops && dst->ops->link_failure)
4331da177e4SLinus Torvalds 		dst->ops->link_failure(skb);
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds static inline void dst_set_expires(struct dst_entry *dst, int timeout)
4371da177e4SLinus Torvalds {
4381da177e4SLinus Torvalds 	unsigned long expires = jiffies + timeout;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	if (expires == 0)
4411da177e4SLinus Torvalds 		expires = 1;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	if (dst->expires == 0 || time_before(expires, dst->expires))
4441da177e4SLinus Torvalds 		dst->expires = expires;
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds /* Output packet to network from transport.  */
4481da177e4SLinus Torvalds static inline int dst_output(struct sk_buff *skb)
4491da177e4SLinus Torvalds {
450adf30907SEric Dumazet 	return skb_dst(skb)->output(skb);
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds /* Input packet from network to transport.  */
4541da177e4SLinus Torvalds static inline int dst_input(struct sk_buff *skb)
4551da177e4SLinus Torvalds {
456adf30907SEric Dumazet 	return skb_dst(skb)->input(skb);
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
4601da177e4SLinus Torvalds {
4611da177e4SLinus Torvalds 	if (dst->obsolete)
4621da177e4SLinus Torvalds 		dst = dst->ops->check(dst, cookie);
4631da177e4SLinus Torvalds 	return dst;
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds extern void		dst_init(void);
4671da177e4SLinus Torvalds 
468815f4e57SHerbert Xu /* Flags for xfrm_lookup flags argument. */
469815f4e57SHerbert Xu enum {
47080c0bc9eSDavid S. Miller 	XFRM_LOOKUP_ICMP = 1 << 0,
471815f4e57SHerbert Xu };
472815f4e57SHerbert Xu 
4731da177e4SLinus Torvalds struct flowi;
4741da177e4SLinus Torvalds #ifndef CONFIG_XFRM
475452edd59SDavid S. Miller static inline struct dst_entry *xfrm_lookup(struct net *net,
476452edd59SDavid S. Miller 					    struct dst_entry *dst_orig,
477dee9f4bcSDavid S. Miller 					    const struct flowi *fl, struct sock *sk,
478dee9f4bcSDavid S. Miller 					    int flags)
4791da177e4SLinus Torvalds {
480452edd59SDavid S. Miller 	return dst_orig;
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds #else
483452edd59SDavid S. Miller extern struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
484452edd59SDavid S. Miller 				     const struct flowi *fl, struct sock *sk,
485452edd59SDavid S. Miller 				     int flags);
4861da177e4SLinus Torvalds #endif
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds #endif /* _NET_DST_H */
489