xref: /openbmc/linux/include/net/xfrm.h (revision b48c05ab)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _NET_XFRM_H
31da177e4SLinus Torvalds #define _NET_XFRM_H
41da177e4SLinus Torvalds 
5aabc9761SHerbert Xu #include <linux/compiler.h>
61da177e4SLinus Torvalds #include <linux/xfrm.h>
71da177e4SLinus Torvalds #include <linux/spinlock.h>
81da177e4SLinus Torvalds #include <linux/list.h>
91da177e4SLinus Torvalds #include <linux/skbuff.h>
1014c85021SArnaldo Carvalho de Melo #include <linux/socket.h>
111da177e4SLinus Torvalds #include <linux/pfkeyv2.h>
125794708fSMasahide NAKAMURA #include <linux/ipsec.h>
131da177e4SLinus Torvalds #include <linux/in6.h>
144a3e2f71SArjan van de Ven #include <linux/mutex.h>
15ab5f5e8bSJoy Latten #include <linux/audit.h>
165a0e3ad6STejun Heo #include <linux/slab.h>
1788755e9cSReshetova, Elena #include <linux/refcount.h>
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <net/sock.h>
201da177e4SLinus Torvalds #include <net/dst.h>
21436a0a40SHerbert Xu #include <net/ip.h>
221da177e4SLinus Torvalds #include <net/route.h>
231da177e4SLinus Torvalds #include <net/ipv6.h>
241da177e4SLinus Torvalds #include <net/ip6_fib.h>
25fe1a5f03STimo Teräs #include <net/flow.h>
269e0d57fdSYury Polyanskiy 
279e0d57fdSYury Polyanskiy #include <linux/interrupt.h>
289e0d57fdSYury Polyanskiy 
29558f82efSMasahide NAKAMURA #ifdef CONFIG_XFRM_STATISTICS
30558f82efSMasahide NAKAMURA #include <net/snmp.h>
31558f82efSMasahide NAKAMURA #endif
321da177e4SLinus Torvalds 
33d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_ESP		50
34d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_AH		51
35d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_COMP		108
36d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_IPIP		4
37d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_IPV6		41
38d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_ROUTING	IPPROTO_ROUTING
39d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_DSTOPTS	IPPROTO_DSTOPTS
40d3d6dd3aSMasahide NAKAMURA 
41fa9921e4SNicolas Dichtel #define XFRM_ALIGN4(len)	(((len) + 3) & ~3)
421da177e4SLinus Torvalds #define XFRM_ALIGN8(len)	(((len) + 7) & ~7)
43b59f45d0SHerbert Xu #define MODULE_ALIAS_XFRM_MODE(family, encap) \
44b59f45d0SHerbert Xu 	MODULE_ALIAS("xfrm-mode-" __stringify(family) "-" __stringify(encap))
45d3d6dd3aSMasahide NAKAMURA #define MODULE_ALIAS_XFRM_TYPE(family, proto) \
46d3d6dd3aSMasahide NAKAMURA 	MODULE_ALIAS("xfrm-type-" __stringify(family) "-" __stringify(proto))
47ffdb5211SIlan Tayari #define MODULE_ALIAS_XFRM_OFFLOAD_TYPE(family, proto) \
48ffdb5211SIlan Tayari 	MODULE_ALIAS("xfrm-offload-" __stringify(family) "-" __stringify(proto))
491da177e4SLinus Torvalds 
50558f82efSMasahide NAKAMURA #ifdef CONFIG_XFRM_STATISTICS
5159c9940eSAlexey Dobriyan #define XFRM_INC_STATS(net, field)	SNMP_INC_STATS((net)->mib.xfrm_statistics, field)
52558f82efSMasahide NAKAMURA #else
5359c9940eSAlexey Dobriyan #define XFRM_INC_STATS(net, field)	((void)(net))
54558f82efSMasahide NAKAMURA #endif
55558f82efSMasahide NAKAMURA 
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /* Organization of SPD aka "XFRM rules"
581da177e4SLinus Torvalds    ------------------------------------
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds    Basic objects:
611da177e4SLinus Torvalds    - policy rule, struct xfrm_policy (=SPD entry)
621da177e4SLinus Torvalds    - bundle of transformations, struct dst_entry == struct xfrm_dst (=SA bundle)
631da177e4SLinus Torvalds    - instance of a transformer, struct xfrm_state (=SA)
641da177e4SLinus Torvalds    - template to clone xfrm_state, struct xfrm_tmpl
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds    SPD is plain linear list of xfrm_policy rules, ordered by priority.
671da177e4SLinus Torvalds    (To be compatible with existing pfkeyv2 implementations,
681da177e4SLinus Torvalds    many rules with priority of 0x7fffffff are allowed to exist and
691da177e4SLinus Torvalds    such rules are ordered in an unpredictable way, thanks to bsd folks.)
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds    Lookup is plain linear search until the first match with selector.
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds    If "action" is "block", then we prohibit the flow, otherwise:
741da177e4SLinus Torvalds    if "xfrms_nr" is zero, the flow passes untransformed. Otherwise,
751da177e4SLinus Torvalds    policy entry has list of up to XFRM_MAX_DEPTH transformations,
761da177e4SLinus Torvalds    described by templates xfrm_tmpl. Each template is resolved
771da177e4SLinus Torvalds    to a complete xfrm_state (see below) and we pack bundle of transformations
781da177e4SLinus Torvalds    to a dst_entry returned to requestor.
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds    dst -. xfrm  .-> xfrm_state #1
811da177e4SLinus Torvalds     |---. child .-> dst -. xfrm .-> xfrm_state #2
821da177e4SLinus Torvalds                      |---. child .-> dst -. xfrm .-> xfrm_state #3
831da177e4SLinus Torvalds                                       |---. child .-> NULL
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds    Bundles are cached at xrfm_policy struct (field ->bundles).
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds    Resolution of xrfm_tmpl
891da177e4SLinus Torvalds    -----------------------
901da177e4SLinus Torvalds    Template contains:
911da177e4SLinus Torvalds    1. ->mode		Mode: transport or tunnel
921da177e4SLinus Torvalds    2. ->id.proto	Protocol: AH/ESP/IPCOMP
931da177e4SLinus Torvalds    3. ->id.daddr	Remote tunnel endpoint, ignored for transport mode.
941da177e4SLinus Torvalds       Q: allow to resolve security gateway?
951da177e4SLinus Torvalds    4. ->id.spi          If not zero, static SPI.
961da177e4SLinus Torvalds    5. ->saddr		Local tunnel endpoint, ignored for transport mode.
971da177e4SLinus Torvalds    6. ->algos		List of allowed algos. Plain bitmask now.
981da177e4SLinus Torvalds       Q: ealgos, aalgos, calgos. What a mess...
991da177e4SLinus Torvalds    7. ->share		Sharing mode.
1001da177e4SLinus Torvalds       Q: how to implement private sharing mode? To add struct sock* to
1011da177e4SLinus Torvalds       flow id?
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds    Having this template we search through SAD searching for entries
1041da177e4SLinus Torvalds    with appropriate mode/proto/algo, permitted by selector.
1051da177e4SLinus Torvalds    If no appropriate entry found, it is requested from key manager.
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds    PROBLEMS:
1081da177e4SLinus Torvalds    Q: How to find all the bundles referring to a physical path for
1091da177e4SLinus Torvalds       PMTU discovery? Seems, dst should contain list of all parents...
1101da177e4SLinus Torvalds       and enter to infinite locking hierarchy disaster.
1111da177e4SLinus Torvalds       No! It is easier, we will not search for them, let them find us.
1121da177e4SLinus Torvalds       We add genid to each dst plus pointer to genid of raw IP route,
1131da177e4SLinus Torvalds       pmtu disc will update pmtu on raw IP route and increase its genid.
1141da177e4SLinus Torvalds       dst_check() will see this for top level and trigger resyncing
1151da177e4SLinus Torvalds       metrics. Plus, it will be made via sk->sk_dst_cache. Solved.
1161da177e4SLinus Torvalds  */
1171da177e4SLinus Torvalds 
11812a169e7SHerbert Xu struct xfrm_state_walk {
11912a169e7SHerbert Xu 	struct list_head	all;
12012a169e7SHerbert Xu 	u8			state;
12112a169e7SHerbert Xu 	u8			dying;
12212a169e7SHerbert Xu 	u8			proto;
12312a169e7SHerbert Xu 	u32			seq;
124870a2df4SNicolas Dichtel 	struct xfrm_address_filter *filter;
12512a169e7SHerbert Xu };
12612a169e7SHerbert Xu 
127d77e38e6SSteffen Klassert struct xfrm_state_offload {
128d77e38e6SSteffen Klassert 	struct net_device	*dev;
129d77e38e6SSteffen Klassert 	unsigned long		offload_handle;
130d77e38e6SSteffen Klassert 	unsigned int		num_exthdrs;
131d77e38e6SSteffen Klassert 	u8			flags;
132d77e38e6SSteffen Klassert };
133d77e38e6SSteffen Klassert 
1341da177e4SLinus Torvalds /* Full description of state of transformer. */
135fd2c3ef7SEric Dumazet struct xfrm_state {
1360c5c9fb5SEric W. Biederman 	possible_net_t		xs_net;
137abb81c4fSHerbert Xu 	union {
13812a169e7SHerbert Xu 		struct hlist_node	gclist;
1398f126e37SDavid S. Miller 		struct hlist_node	bydst;
140abb81c4fSHerbert Xu 	};
1418f126e37SDavid S. Miller 	struct hlist_node	bysrc;
1428f126e37SDavid S. Miller 	struct hlist_node	byspi;
1431da177e4SLinus Torvalds 
14488755e9cSReshetova, Elena 	refcount_t		refcnt;
1451da177e4SLinus Torvalds 	spinlock_t		lock;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	struct xfrm_id		id;
1481da177e4SLinus Torvalds 	struct xfrm_selector	sel;
149bf825f81SJamal Hadi Salim 	struct xfrm_mark	mark;
15035d2856bSMartin Willi 	u32			tfcpad;
1511da177e4SLinus Torvalds 
1529d4a706dSDavid S. Miller 	u32			genid;
1539d4a706dSDavid S. Miller 
15412a169e7SHerbert Xu 	/* Key manager bits */
15512a169e7SHerbert Xu 	struct xfrm_state_walk	km;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	/* Parameters of this state. */
1581da177e4SLinus Torvalds 	struct {
1591da177e4SLinus Torvalds 		u32		reqid;
1601da177e4SLinus Torvalds 		u8		mode;
1611da177e4SLinus Torvalds 		u8		replay_window;
1621da177e4SLinus Torvalds 		u8		aalgo, ealgo, calgo;
1631da177e4SLinus Torvalds 		u8		flags;
1641da177e4SLinus Torvalds 		u16		family;
1651da177e4SLinus Torvalds 		xfrm_address_t	saddr;
1661da177e4SLinus Torvalds 		int		header_len;
1671da177e4SLinus Torvalds 		int		trailer_len;
168a947b0a9SNicolas Dichtel 		u32		extra_flags;
169077fbac4SLorenzo Colitti 		u32		output_mark;
1701da177e4SLinus Torvalds 	} props;
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	struct xfrm_lifetime_cfg lft;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	/* Data for transformer */
1754447bb33SMartin Willi 	struct xfrm_algo_auth	*aalg;
1761da177e4SLinus Torvalds 	struct xfrm_algo	*ealg;
1771da177e4SLinus Torvalds 	struct xfrm_algo	*calg;
1781a6509d9SHerbert Xu 	struct xfrm_algo_aead	*aead;
17969b0137fSHerbert Xu 	const char		*geniv;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	/* Data for encapsulator */
1821da177e4SLinus Torvalds 	struct xfrm_encap_tmpl	*encap;
1831da177e4SLinus Torvalds 
184060f02a3SNoriaki TAKAMIYA 	/* Data for care-of address */
185060f02a3SNoriaki TAKAMIYA 	xfrm_address_t	*coaddr;
186060f02a3SNoriaki TAKAMIYA 
1871da177e4SLinus Torvalds 	/* IPComp needs an IPIP tunnel for handling uncompressed packets */
1881da177e4SLinus Torvalds 	struct xfrm_state	*tunnel;
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	/* If a tunnel, number of users + 1 */
1911da177e4SLinus Torvalds 	atomic_t		tunnel_users;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	/* State for replay detection */
1941da177e4SLinus Torvalds 	struct xfrm_replay_state replay;
1959736acf3SSteffen Klassert 	struct xfrm_replay_state_esn *replay_esn;
1961da177e4SLinus Torvalds 
197f8cd5488SJamal Hadi Salim 	/* Replay detection state at the time we sent the last notification */
198f8cd5488SJamal Hadi Salim 	struct xfrm_replay_state preplay;
1999736acf3SSteffen Klassert 	struct xfrm_replay_state_esn *preplay_esn;
200f8cd5488SJamal Hadi Salim 
2019fdc4883SSteffen Klassert 	/* The functions for replay detection. */
202e45a8a9eSJulia Lawall 	const struct xfrm_replay *repl;
2039fdc4883SSteffen Klassert 
2042717096aSJamal Hadi Salim 	/* internal flag that only holds state for delayed aevent at the
2052717096aSJamal Hadi Salim 	 * moment
2062717096aSJamal Hadi Salim 	*/
2072717096aSJamal Hadi Salim 	u32			xflags;
2082717096aSJamal Hadi Salim 
209f8cd5488SJamal Hadi Salim 	/* Replay detection notification settings */
210f8cd5488SJamal Hadi Salim 	u32			replay_maxage;
211f8cd5488SJamal Hadi Salim 	u32			replay_maxdiff;
212f8cd5488SJamal Hadi Salim 
213f8cd5488SJamal Hadi Salim 	/* Replay detection notification timer */
214f8cd5488SJamal Hadi Salim 	struct timer_list	rtimer;
215f8cd5488SJamal Hadi Salim 
2161da177e4SLinus Torvalds 	/* Statistics */
2171da177e4SLinus Torvalds 	struct xfrm_stats	stats;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	struct xfrm_lifetime_cur curlft;
2209e0d57fdSYury Polyanskiy 	struct tasklet_hrtimer	mtimer;
2211da177e4SLinus Torvalds 
222d77e38e6SSteffen Klassert 	struct xfrm_state_offload xso;
223d77e38e6SSteffen Klassert 
224e3c0d047SFan Du 	/* used to fix curlft->add_time when changing date */
225e3c0d047SFan Du 	long		saved_tmo;
226e3c0d047SFan Du 
2279afaca05SMasahide NAKAMURA 	/* Last used time */
228d26f3984SHerbert Xu 	unsigned long		lastused;
2299afaca05SMasahide NAKAMURA 
230cac2661cSSteffen Klassert 	struct page_frag xfrag;
231cac2661cSSteffen Klassert 
2321da177e4SLinus Torvalds 	/* Reference to data common to all the instances of this
2331da177e4SLinus Torvalds 	 * transformer. */
234533cb5b0SEric Dumazet 	const struct xfrm_type	*type;
23513996378SHerbert Xu 	struct xfrm_mode	*inner_mode;
236df9dcb45SKazunori MIYAZAWA 	struct xfrm_mode	*inner_mode_iaf;
23713996378SHerbert Xu 	struct xfrm_mode	*outer_mode;
2381da177e4SLinus Torvalds 
2399d389d7fSSteffen Klassert 	const struct xfrm_type_offload	*type_offload;
2409d389d7fSSteffen Klassert 
241df71837dSTrent Jaeger 	/* Security context */
242df71837dSTrent Jaeger 	struct xfrm_sec_ctx	*security;
243df71837dSTrent Jaeger 
2441da177e4SLinus Torvalds 	/* Private data of this transformer, format is opaque,
2451da177e4SLinus Torvalds 	 * interpreted by xfrm_type methods. */
2461da177e4SLinus Torvalds 	void			*data;
2471da177e4SLinus Torvalds };
2481da177e4SLinus Torvalds 
249673c09beSAlexey Dobriyan static inline struct net *xs_net(struct xfrm_state *x)
250673c09beSAlexey Dobriyan {
251673c09beSAlexey Dobriyan 	return read_pnet(&x->xs_net);
252673c09beSAlexey Dobriyan }
253673c09beSAlexey Dobriyan 
2542717096aSJamal Hadi Salim /* xflags - make enum if more show up */
2552717096aSJamal Hadi Salim #define XFRM_TIME_DEFER	1
256e3c0d047SFan Du #define XFRM_SOFT_EXPIRE 2
2572717096aSJamal Hadi Salim 
2581da177e4SLinus Torvalds enum {
2591da177e4SLinus Torvalds 	XFRM_STATE_VOID,
2601da177e4SLinus Torvalds 	XFRM_STATE_ACQ,
2611da177e4SLinus Torvalds 	XFRM_STATE_VALID,
2621da177e4SLinus Torvalds 	XFRM_STATE_ERROR,
2631da177e4SLinus Torvalds 	XFRM_STATE_EXPIRED,
2641da177e4SLinus Torvalds 	XFRM_STATE_DEAD
2651da177e4SLinus Torvalds };
2661da177e4SLinus Torvalds 
26726b15dadSJamal Hadi Salim /* callback structure passed from either netlink or pfkey */
268fd2c3ef7SEric Dumazet struct km_event {
269bf08867fSHerbert Xu 	union {
270bf08867fSHerbert Xu 		u32 hard;
271bf08867fSHerbert Xu 		u32 proto;
272bf08867fSHerbert Xu 		u32 byid;
273f8cd5488SJamal Hadi Salim 		u32 aevent;
274f7b6983fSMasahide NAKAMURA 		u32 type;
275bf08867fSHerbert Xu 	} data;
276bf08867fSHerbert Xu 
27726b15dadSJamal Hadi Salim 	u32	seq;
27815e47304SEric W. Biederman 	u32	portid;
27926b15dadSJamal Hadi Salim 	u32	event;
2807067802eSAlexey Dobriyan 	struct net *net;
28126b15dadSJamal Hadi Salim };
28226b15dadSJamal Hadi Salim 
2839fdc4883SSteffen Klassert struct xfrm_replay {
2849fdc4883SSteffen Klassert 	void	(*advance)(struct xfrm_state *x, __be32 net_seq);
2859fdc4883SSteffen Klassert 	int	(*check)(struct xfrm_state *x,
2869fdc4883SSteffen Klassert 			 struct sk_buff *skb,
2879fdc4883SSteffen Klassert 			 __be32 net_seq);
2883b59df46SSteffen Klassert 	int	(*recheck)(struct xfrm_state *x,
2893b59df46SSteffen Klassert 			   struct sk_buff *skb,
2903b59df46SSteffen Klassert 			   __be32 net_seq);
2919fdc4883SSteffen Klassert 	void	(*notify)(struct xfrm_state *x, int event);
2929fdc4883SSteffen Klassert 	int	(*overflow)(struct xfrm_state *x, struct sk_buff *skb);
2939fdc4883SSteffen Klassert };
2949fdc4883SSteffen Klassert 
29525ee3286SHerbert Xu struct net_device;
2961da177e4SLinus Torvalds struct xfrm_type;
2971da177e4SLinus Torvalds struct xfrm_dst;
2981da177e4SLinus Torvalds struct xfrm_policy_afinfo {
2991da177e4SLinus Torvalds 	struct dst_ops		*dst_ops;
30042a7b32bSDavid Ahern 	struct dst_entry	*(*dst_lookup)(struct net *net,
30142a7b32bSDavid Ahern 					       int tos, int oif,
3025e6b930fSDavid S. Miller 					       const xfrm_address_t *saddr,
303077fbac4SLorenzo Colitti 					       const xfrm_address_t *daddr,
304077fbac4SLorenzo Colitti 					       u32 mark);
30542a7b32bSDavid Ahern 	int			(*get_saddr)(struct net *net, int oif,
30642a7b32bSDavid Ahern 					     xfrm_address_t *saddr,
307077fbac4SLorenzo Colitti 					     xfrm_address_t *daddr,
308077fbac4SLorenzo Colitti 					     u32 mark);
3091da177e4SLinus Torvalds 	void			(*decode_session)(struct sk_buff *skb,
310d5422efeSHerbert Xu 						  struct flowi *fl,
311d5422efeSHerbert Xu 						  int reverse);
31205d84025SDavid S. Miller 	int			(*get_tos)(const struct flowi *fl);
313a1b05140SMasahide NAKAMURA 	int			(*init_path)(struct xfrm_dst *path,
314a1b05140SMasahide NAKAMURA 					     struct dst_entry *dst,
315a1b05140SMasahide NAKAMURA 					     int nfheader_len);
31625ee3286SHerbert Xu 	int			(*fill_dst)(struct xfrm_dst *xdst,
31787c1e12bSHerbert Xu 					    struct net_device *dev,
3180c7b3eefSDavid S. Miller 					    const struct flowi *fl);
3192774c131SDavid S. Miller 	struct dst_entry	*(*blackhole_route)(struct net *net, struct dst_entry *orig);
3201da177e4SLinus Torvalds };
3211da177e4SLinus Torvalds 
322a2817d8bSFlorian Westphal int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family);
323a2817d8bSFlorian Westphal void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo);
324d511337aSJoe Perches void km_policy_notify(struct xfrm_policy *xp, int dir,
325d511337aSJoe Perches 		      const struct km_event *c);
326ec30d78cSFlorian Westphal void xfrm_policy_cache_flush(void);
327d511337aSJoe Perches void km_state_notify(struct xfrm_state *x, const struct km_event *c);
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds struct xfrm_tmpl;
330d511337aSJoe Perches int km_query(struct xfrm_state *x, struct xfrm_tmpl *t,
331d511337aSJoe Perches 	     struct xfrm_policy *pol);
332d511337aSJoe Perches void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
333d511337aSJoe Perches int __xfrm_state_delete(struct xfrm_state *x);
33453bc6b4dSJamal Hadi Salim 
3351da177e4SLinus Torvalds struct xfrm_state_afinfo {
33617c2a42aSHerbert Xu 	unsigned int			family;
33736cf9acfSHerbert Xu 	unsigned int			proto;
3388e3d716cSAl Viro 	__be16				eth_proto;
33917c2a42aSHerbert Xu 	struct module			*owner;
340533cb5b0SEric Dumazet 	const struct xfrm_type		*type_map[IPPROTO_MAX];
3419d389d7fSSteffen Klassert 	const struct xfrm_type_offload	*type_offload_map[IPPROTO_MAX];
342aa5d62ccSHerbert Xu 	struct xfrm_mode		*mode_map[XFRM_MODE_MAX];
3439d389d7fSSteffen Klassert 
344d094cd83SHerbert Xu 	int			(*init_flags)(struct xfrm_state *x);
34573e5ebb2SDavid S. Miller 	void			(*init_tempsel)(struct xfrm_selector *sel,
34673e5ebb2SDavid S. Miller 						const struct flowi *fl);
34719bd6244SDavid S. Miller 	void			(*init_temprop)(struct xfrm_state *x,
34819bd6244SDavid S. Miller 						const struct xfrm_tmpl *tmpl,
34919bd6244SDavid S. Miller 						const xfrm_address_t *daddr,
35019bd6244SDavid S. Miller 						const xfrm_address_t *saddr);
35141a49cc3SMasahide NAKAMURA 	int			(*tmpl_sort)(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n);
35241a49cc3SMasahide NAKAMURA 	int			(*state_sort)(struct xfrm_state **dst, struct xfrm_state **src, int n);
353ede2059dSEric W. Biederman 	int			(*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
3547026b1ddSDavid Miller 	int			(*output_finish)(struct sock *sk, struct sk_buff *skb);
355227620e2SHerbert Xu 	int			(*extract_input)(struct xfrm_state *x,
356227620e2SHerbert Xu 						 struct sk_buff *skb);
35736cf9acfSHerbert Xu 	int			(*extract_output)(struct xfrm_state *x,
35836cf9acfSHerbert Xu 						  struct sk_buff *skb);
359716062fdSHerbert Xu 	int			(*transport_finish)(struct sk_buff *skb,
360716062fdSHerbert Xu 						    int async);
361628e341fSHannes Frederic Sowa 	void			(*local_error)(struct sk_buff *skb, u32 mtu);
3621da177e4SLinus Torvalds };
3631da177e4SLinus Torvalds 
364d511337aSJoe Perches int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo);
365d511337aSJoe Perches int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo);
366d511337aSJoe Perches struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
367711059b9SFlorian Westphal struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family);
3681da177e4SLinus Torvalds 
3692f32b51bSSteffen Klassert struct xfrm_input_afinfo {
3702f32b51bSSteffen Klassert 	unsigned int		family;
3712f32b51bSSteffen Klassert 	int			(*callback)(struct sk_buff *skb, u8 protocol,
3722f32b51bSSteffen Klassert 					    int err);
3732f32b51bSSteffen Klassert };
3742f32b51bSSteffen Klassert 
375960fdfdeSFlorian Westphal int xfrm_input_register_afinfo(const struct xfrm_input_afinfo *afinfo);
376960fdfdeSFlorian Westphal int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo *afinfo);
3772f32b51bSSteffen Klassert 
378b48c05abSSteffen Klassert void xfrm_flush_gc(void);
379d511337aSJoe Perches void xfrm_state_delete_tunnel(struct xfrm_state *x);
3801da177e4SLinus Torvalds 
381fd2c3ef7SEric Dumazet struct xfrm_type {
3821da177e4SLinus Torvalds 	char			*description;
3831da177e4SLinus Torvalds 	struct module		*owner;
384a6337463Sjamal 	u8			proto;
385a6337463Sjamal 	u8			flags;
3861b5c2299SMasahide NAKAMURA #define XFRM_TYPE_NON_FRAGMENT	1
387436a0a40SHerbert Xu #define XFRM_TYPE_REPLAY_PROT	2
388f04e7e8dSHerbert Xu #define XFRM_TYPE_LOCAL_COADDR	4
389f04e7e8dSHerbert Xu #define XFRM_TYPE_REMOTE_COADDR	8
3901da177e4SLinus Torvalds 
39172cb6962SHerbert Xu 	int			(*init_state)(struct xfrm_state *x);
3921da177e4SLinus Torvalds 	void			(*destructor)(struct xfrm_state *);
393e695633eSHerbert Xu 	int			(*input)(struct xfrm_state *, struct sk_buff *skb);
3941da177e4SLinus Torvalds 	int			(*output)(struct xfrm_state *, struct sk_buff *pskb);
3958f029de2SDavid S. Miller 	int			(*reject)(struct xfrm_state *, struct sk_buff *,
3968f029de2SDavid S. Miller 					  const struct flowi *);
397aee5adb4SMasahide NAKAMURA 	int			(*hdr_offset)(struct xfrm_state *, struct sk_buff *, u8 **);
3981da177e4SLinus Torvalds 	/* Estimate maximal size of result of transformation of a dgram */
399c5c25238SPatrick McHardy 	u32			(*get_mtu)(struct xfrm_state *, int size);
4001da177e4SLinus Torvalds };
4011da177e4SLinus Torvalds 
402d511337aSJoe Perches int xfrm_register_type(const struct xfrm_type *type, unsigned short family);
403d511337aSJoe Perches int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family);
4041da177e4SLinus Torvalds 
4059d389d7fSSteffen Klassert struct xfrm_type_offload {
4069d389d7fSSteffen Klassert 	char		*description;
4079d389d7fSSteffen Klassert 	struct module	*owner;
4089d389d7fSSteffen Klassert 	u8		proto;
4099d389d7fSSteffen Klassert 	void		(*encap)(struct xfrm_state *, struct sk_buff *pskb);
4109d389d7fSSteffen Klassert 	int		(*input_tail)(struct xfrm_state *x, struct sk_buff *skb);
4119d389d7fSSteffen Klassert 	int		(*xmit)(struct xfrm_state *, struct sk_buff *pskb, netdev_features_t features);
4129d389d7fSSteffen Klassert };
4139d389d7fSSteffen Klassert 
4149d389d7fSSteffen Klassert int xfrm_register_type_offload(const struct xfrm_type_offload *type, unsigned short family);
4159d389d7fSSteffen Klassert int xfrm_unregister_type_offload(const struct xfrm_type_offload *type, unsigned short family);
4169d389d7fSSteffen Klassert 
417b59f45d0SHerbert Xu struct xfrm_mode {
418227620e2SHerbert Xu 	/*
419227620e2SHerbert Xu 	 * Remove encapsulation header.
420227620e2SHerbert Xu 	 *
421227620e2SHerbert Xu 	 * The IP header will be moved over the top of the encapsulation
422227620e2SHerbert Xu 	 * header.
423227620e2SHerbert Xu 	 *
424227620e2SHerbert Xu 	 * On entry, the transport header shall point to where the IP header
425227620e2SHerbert Xu 	 * should be and the network header shall be set to where the IP
426227620e2SHerbert Xu 	 * header currently is.  skb->data shall point to the start of the
427227620e2SHerbert Xu 	 * payload.
428227620e2SHerbert Xu 	 */
429227620e2SHerbert Xu 	int (*input2)(struct xfrm_state *x, struct sk_buff *skb);
430227620e2SHerbert Xu 
431227620e2SHerbert Xu 	/*
432227620e2SHerbert Xu 	 * This is the actual input entry point.
433227620e2SHerbert Xu 	 *
434227620e2SHerbert Xu 	 * For transport mode and equivalent this would be identical to
435227620e2SHerbert Xu 	 * input2 (which does not need to be set).  While tunnel mode
436227620e2SHerbert Xu 	 * and equivalent would set this to the tunnel encapsulation function
437227620e2SHerbert Xu 	 * xfrm4_prepare_input that would in turn call input2.
438227620e2SHerbert Xu 	 */
439b59f45d0SHerbert Xu 	int (*input)(struct xfrm_state *x, struct sk_buff *skb);
44037fedd3aSHerbert Xu 
44137fedd3aSHerbert Xu 	/*
44237fedd3aSHerbert Xu 	 * Add encapsulation header.
44337fedd3aSHerbert Xu 	 *
44437fedd3aSHerbert Xu 	 * On exit, the transport header will be set to the start of the
44537fedd3aSHerbert Xu 	 * encapsulation header to be filled in by x->type->output and
44637fedd3aSHerbert Xu 	 * the mac header will be set to the nextheader (protocol for
44737fedd3aSHerbert Xu 	 * IPv4) field of the extension header directly preceding the
44837fedd3aSHerbert Xu 	 * encapsulation header, or in its absence, that of the top IP
44937fedd3aSHerbert Xu 	 * header.  The value of the network header will always point
45037fedd3aSHerbert Xu 	 * to the top IP header while skb->data will point to the payload.
45137fedd3aSHerbert Xu 	 */
45236cf9acfSHerbert Xu 	int (*output2)(struct xfrm_state *x,struct sk_buff *skb);
45336cf9acfSHerbert Xu 
45436cf9acfSHerbert Xu 	/*
45536cf9acfSHerbert Xu 	 * This is the actual output entry point.
45636cf9acfSHerbert Xu 	 *
45736cf9acfSHerbert Xu 	 * For transport mode and equivalent this would be identical to
45836cf9acfSHerbert Xu 	 * output2 (which does not need to be set).  While tunnel mode
45936cf9acfSHerbert Xu 	 * and equivalent would set this to a tunnel encapsulation function
46036cf9acfSHerbert Xu 	 * (xfrm4_prepare_output or xfrm6_prepare_output) that would in turn
46136cf9acfSHerbert Xu 	 * call output2.
46236cf9acfSHerbert Xu 	 */
463eb878e84SJamal Hadi Salim 	int (*output)(struct xfrm_state *x, struct sk_buff *skb);
464b59f45d0SHerbert Xu 
465c35fe410SSteffen Klassert 	/*
466c35fe410SSteffen Klassert 	 * Adjust pointers into the packet and do GSO segmentation.
467c35fe410SSteffen Klassert 	 */
468c35fe410SSteffen Klassert 	struct sk_buff *(*gso_segment)(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features);
469c35fe410SSteffen Klassert 
470c35fe410SSteffen Klassert 	/*
471c35fe410SSteffen Klassert 	 * Adjust pointers into the packet when IPsec is done at layer2.
472c35fe410SSteffen Klassert 	 */
473c35fe410SSteffen Klassert 	void (*xmit)(struct xfrm_state *x, struct sk_buff *skb);
474c35fe410SSteffen Klassert 
47517c2a42aSHerbert Xu 	struct xfrm_state_afinfo *afinfo;
476b59f45d0SHerbert Xu 	struct module *owner;
477b59f45d0SHerbert Xu 	unsigned int encap;
4781bfcb10fSHerbert Xu 	int flags;
4791bfcb10fSHerbert Xu };
4801bfcb10fSHerbert Xu 
4811bfcb10fSHerbert Xu /* Flags for xfrm_mode. */
4821bfcb10fSHerbert Xu enum {
4831bfcb10fSHerbert Xu 	XFRM_MODE_FLAG_TUNNEL = 1,
484b59f45d0SHerbert Xu };
485b59f45d0SHerbert Xu 
486d511337aSJoe Perches int xfrm_register_mode(struct xfrm_mode *mode, int family);
487d511337aSJoe Perches int xfrm_unregister_mode(struct xfrm_mode *mode, int family);
488b59f45d0SHerbert Xu 
489df9dcb45SKazunori MIYAZAWA static inline int xfrm_af2proto(unsigned int family)
490df9dcb45SKazunori MIYAZAWA {
491df9dcb45SKazunori MIYAZAWA 	switch(family) {
492df9dcb45SKazunori MIYAZAWA 	case AF_INET:
493df9dcb45SKazunori MIYAZAWA 		return IPPROTO_IPIP;
494df9dcb45SKazunori MIYAZAWA 	case AF_INET6:
495df9dcb45SKazunori MIYAZAWA 		return IPPROTO_IPV6;
496df9dcb45SKazunori MIYAZAWA 	default:
497df9dcb45SKazunori MIYAZAWA 		return 0;
498df9dcb45SKazunori MIYAZAWA 	}
499df9dcb45SKazunori MIYAZAWA }
500df9dcb45SKazunori MIYAZAWA 
501df9dcb45SKazunori MIYAZAWA static inline struct xfrm_mode *xfrm_ip2inner_mode(struct xfrm_state *x, int ipproto)
502df9dcb45SKazunori MIYAZAWA {
503df9dcb45SKazunori MIYAZAWA 	if ((ipproto == IPPROTO_IPIP && x->props.family == AF_INET) ||
504df9dcb45SKazunori MIYAZAWA 	    (ipproto == IPPROTO_IPV6 && x->props.family == AF_INET6))
505df9dcb45SKazunori MIYAZAWA 		return x->inner_mode;
506df9dcb45SKazunori MIYAZAWA 	else
507df9dcb45SKazunori MIYAZAWA 		return x->inner_mode_iaf;
508df9dcb45SKazunori MIYAZAWA }
509df9dcb45SKazunori MIYAZAWA 
510fd2c3ef7SEric Dumazet struct xfrm_tmpl {
5111da177e4SLinus Torvalds /* id in template is interpreted as:
5121da177e4SLinus Torvalds  * daddr - destination of tunnel, may be zero for transport mode.
5131da177e4SLinus Torvalds  * spi   - zero to acquire spi. Not zero if spi is static, then
5141da177e4SLinus Torvalds  *	   daddr must be fixed too.
5151da177e4SLinus Torvalds  * proto - AH/ESP/IPCOMP
5161da177e4SLinus Torvalds  */
5171da177e4SLinus Torvalds 	struct xfrm_id		id;
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds /* Source address of tunnel. Ignored, if it is not a tunnel. */
5201da177e4SLinus Torvalds 	xfrm_address_t		saddr;
5211da177e4SLinus Torvalds 
52276b3f055SMiika Komu 	unsigned short		encap_family;
52376b3f055SMiika Komu 
524a6337463Sjamal 	u32			reqid;
5251da177e4SLinus Torvalds 
5267e49e6deSMasahide NAKAMURA /* Mode: transport, tunnel etc. */
527a6337463Sjamal 	u8			mode;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds /* Sharing mode: unique, this session only, this user only etc. */
530a6337463Sjamal 	u8			share;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds /* May skip this transfomration if no SA is found */
533a6337463Sjamal 	u8			optional;
5341da177e4SLinus Torvalds 
535c5d18e98SHerbert Xu /* Skip aalgos/ealgos/calgos checks. */
536a6337463Sjamal 	u8			allalgs;
537c5d18e98SHerbert Xu 
5381da177e4SLinus Torvalds /* Bit mask of algos allowed for acquisition */
539a6337463Sjamal 	u32			aalgos;
540a6337463Sjamal 	u32			ealgos;
541a6337463Sjamal 	u32			calgos;
5421da177e4SLinus Torvalds };
5431da177e4SLinus Torvalds 
544622dc828SMasahide NAKAMURA #define XFRM_MAX_DEPTH		6
54554ef207aSSteffen Klassert #define XFRM_MAX_OFFLOAD_DEPTH	1
5461da177e4SLinus Torvalds 
54712a169e7SHerbert Xu struct xfrm_policy_walk_entry {
54812a169e7SHerbert Xu 	struct list_head	all;
54912a169e7SHerbert Xu 	u8			dead;
55012a169e7SHerbert Xu };
55112a169e7SHerbert Xu 
55212a169e7SHerbert Xu struct xfrm_policy_walk {
55312a169e7SHerbert Xu 	struct xfrm_policy_walk_entry walk;
55412a169e7SHerbert Xu 	u8 type;
55512a169e7SHerbert Xu 	u32 seq;
55612a169e7SHerbert Xu };
55712a169e7SHerbert Xu 
558a0073fe1SSteffen Klassert struct xfrm_policy_queue {
559a0073fe1SSteffen Klassert 	struct sk_buff_head	hold_queue;
560a0073fe1SSteffen Klassert 	struct timer_list	hold_timer;
561a0073fe1SSteffen Klassert 	unsigned long		timeout;
562a0073fe1SSteffen Klassert };
563a0073fe1SSteffen Klassert 
564fd2c3ef7SEric Dumazet struct xfrm_policy {
5650c5c9fb5SEric W. Biederman 	possible_net_t		xp_net;
5662518c7c2SDavid S. Miller 	struct hlist_node	bydst;
5672518c7c2SDavid S. Miller 	struct hlist_node	byidx;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	/* This lock only affects elements except for entry. */
5701da177e4SLinus Torvalds 	rwlock_t		lock;
571850a6212SReshetova, Elena 	refcount_t		refcnt;
5721da177e4SLinus Torvalds 	struct timer_list	timer;
5731da177e4SLinus Torvalds 
57480c802f3STimo Teräs 	atomic_t		genid;
5751da177e4SLinus Torvalds 	u32			priority;
5761da177e4SLinus Torvalds 	u32			index;
577bf825f81SJamal Hadi Salim 	struct xfrm_mark	mark;
5781da177e4SLinus Torvalds 	struct xfrm_selector	selector;
5791da177e4SLinus Torvalds 	struct xfrm_lifetime_cfg lft;
5801da177e4SLinus Torvalds 	struct xfrm_lifetime_cur curlft;
58112a169e7SHerbert Xu 	struct xfrm_policy_walk_entry walk;
582a0073fe1SSteffen Klassert 	struct xfrm_policy_queue polq;
58346ca5f5dSArnaldo Carvalho de Melo 	u8			type;
58446ca5f5dSArnaldo Carvalho de Melo 	u8			action;
58546ca5f5dSArnaldo Carvalho de Melo 	u8			flags;
58646ca5f5dSArnaldo Carvalho de Melo 	u8			xfrm_nr;
58712a169e7SHerbert Xu 	u16			family;
588df71837dSTrent Jaeger 	struct xfrm_sec_ctx	*security;
5891da177e4SLinus Torvalds 	struct xfrm_tmpl       	xfrm_vec[XFRM_MAX_DEPTH];
59056f04730SEric Dumazet 	struct rcu_head		rcu;
5911da177e4SLinus Torvalds };
5921da177e4SLinus Torvalds 
59363eb23f5SDavid S. Miller static inline struct net *xp_net(const struct xfrm_policy *xp)
5940331b1f3SAlexey Dobriyan {
5950331b1f3SAlexey Dobriyan 	return read_pnet(&xp->xp_net);
5960331b1f3SAlexey Dobriyan }
5970331b1f3SAlexey Dobriyan 
59813c1d189SArnaud Ebalard struct xfrm_kmaddress {
59913c1d189SArnaud Ebalard 	xfrm_address_t          local;
60013c1d189SArnaud Ebalard 	xfrm_address_t          remote;
60113c1d189SArnaud Ebalard 	u32			reserved;
60213c1d189SArnaud Ebalard 	u16			family;
60313c1d189SArnaud Ebalard };
60413c1d189SArnaud Ebalard 
60580c9abaaSShinta Sugimoto struct xfrm_migrate {
60680c9abaaSShinta Sugimoto 	xfrm_address_t		old_daddr;
60780c9abaaSShinta Sugimoto 	xfrm_address_t		old_saddr;
60880c9abaaSShinta Sugimoto 	xfrm_address_t		new_daddr;
60980c9abaaSShinta Sugimoto 	xfrm_address_t		new_saddr;
61080c9abaaSShinta Sugimoto 	u8			proto;
61180c9abaaSShinta Sugimoto 	u8			mode;
61280c9abaaSShinta Sugimoto 	u16			reserved;
61380c9abaaSShinta Sugimoto 	u32			reqid;
61480c9abaaSShinta Sugimoto 	u16			old_family;
61580c9abaaSShinta Sugimoto 	u16			new_family;
61680c9abaaSShinta Sugimoto };
61780c9abaaSShinta Sugimoto 
6181da177e4SLinus Torvalds #define XFRM_KM_TIMEOUT                30
619f8cd5488SJamal Hadi Salim /* what happened */
620f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_UPDATE	XFRM_AE_CR
621f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_TIMEOUT	XFRM_AE_CE
622f8cd5488SJamal Hadi Salim 
623f8cd5488SJamal Hadi Salim /* default aevent timeout in units of 100ms */
624f8cd5488SJamal Hadi Salim #define XFRM_AE_ETIME			10
625f8cd5488SJamal Hadi Salim /* Async Event timer multiplier */
626f8cd5488SJamal Hadi Salim #define XFRM_AE_ETH_M			10
627f8cd5488SJamal Hadi Salim /* default seq threshold size */
628f8cd5488SJamal Hadi Salim #define XFRM_AE_SEQT_SIZE		2
6291da177e4SLinus Torvalds 
630fd2c3ef7SEric Dumazet struct xfrm_mgr {
6311da177e4SLinus Torvalds 	struct list_head	list;
632214e005bSDavid S. Miller 	int			(*notify)(struct xfrm_state *x, const struct km_event *c);
63365e0736bSFan Du 	int			(*acquire)(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *xp);
634cb969f07SVenkat Yekkirala 	struct xfrm_policy	*(*compile_policy)(struct sock *sk, int opt, u8 *data, int len, int *dir);
6355d36b180SAl Viro 	int			(*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport);
636214e005bSDavid S. Miller 	int			(*notify_policy)(struct xfrm_policy *x, int dir, const struct km_event *c);
637db983c11SAlexey Dobriyan 	int			(*report)(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr);
638183cad12SDavid S. Miller 	int			(*migrate)(const struct xfrm_selector *sel,
639183cad12SDavid S. Miller 					   u8 dir, u8 type,
640183cad12SDavid S. Miller 					   const struct xfrm_migrate *m,
641183cad12SDavid S. Miller 					   int num_bundles,
6428bafd730SAntony Antony 					   const struct xfrm_kmaddress *k,
6438bafd730SAntony Antony 					   const struct xfrm_encap_tmpl *encap);
6440f24558eSHoria Geanta 	bool			(*is_alive)(const struct km_event *c);
6451da177e4SLinus Torvalds };
6461da177e4SLinus Torvalds 
647d511337aSJoe Perches int xfrm_register_km(struct xfrm_mgr *km);
648d511337aSJoe Perches int xfrm_unregister_km(struct xfrm_mgr *km);
6491da177e4SLinus Torvalds 
65070be6c91SSteffen Klassert struct xfrm_tunnel_skb_cb {
65170be6c91SSteffen Klassert 	union {
65270be6c91SSteffen Klassert 		struct inet_skb_parm h4;
65370be6c91SSteffen Klassert 		struct inet6_skb_parm h6;
65470be6c91SSteffen Klassert 	} header;
65570be6c91SSteffen Klassert 
65670be6c91SSteffen Klassert 	union {
65770be6c91SSteffen Klassert 		struct ip_tunnel *ip4;
65870be6c91SSteffen Klassert 		struct ip6_tnl *ip6;
65970be6c91SSteffen Klassert 	} tunnel;
66070be6c91SSteffen Klassert };
66170be6c91SSteffen Klassert 
66270be6c91SSteffen Klassert #define XFRM_TUNNEL_SKB_CB(__skb) ((struct xfrm_tunnel_skb_cb *)&((__skb)->cb[0]))
66370be6c91SSteffen Klassert 
664436a0a40SHerbert Xu /*
665436a0a40SHerbert Xu  * This structure is used for the duration where packets are being
666436a0a40SHerbert Xu  * transformed by IPsec.  As soon as the packet leaves IPsec the
667436a0a40SHerbert Xu  * area beyond the generic IP part may be overwritten.
668436a0a40SHerbert Xu  */
669436a0a40SHerbert Xu struct xfrm_skb_cb {
67070be6c91SSteffen Klassert 	struct xfrm_tunnel_skb_cb header;
671436a0a40SHerbert Xu 
672436a0a40SHerbert Xu         /* Sequence number for replay protection. */
673b318e0e4SHerbert Xu 	union {
6741ce3644aSSteffen Klassert 		struct {
6751ce3644aSSteffen Klassert 			__u32 low;
6761ce3644aSSteffen Klassert 			__u32 hi;
6771ce3644aSSteffen Klassert 		} output;
6781ce3644aSSteffen Klassert 		struct {
6791ce3644aSSteffen Klassert 			__be32 low;
6801ce3644aSSteffen Klassert 			__be32 hi;
6811ce3644aSSteffen Klassert 		} input;
682b318e0e4SHerbert Xu 	} seq;
683436a0a40SHerbert Xu };
684436a0a40SHerbert Xu 
685436a0a40SHerbert Xu #define XFRM_SKB_CB(__skb) ((struct xfrm_skb_cb *)&((__skb)->cb[0]))
686436a0a40SHerbert Xu 
68736cf9acfSHerbert Xu /*
68836cf9acfSHerbert Xu  * This structure is used by the afinfo prepare_input/prepare_output functions
68936cf9acfSHerbert Xu  * to transmit header information to the mode input/output functions.
69036cf9acfSHerbert Xu  */
69136cf9acfSHerbert Xu struct xfrm_mode_skb_cb {
69270be6c91SSteffen Klassert 	struct xfrm_tunnel_skb_cb header;
69336cf9acfSHerbert Xu 
69436cf9acfSHerbert Xu 	/* Copied from header for IPv4, always set to zero and DF for IPv6. */
69536cf9acfSHerbert Xu 	__be16 id;
69636cf9acfSHerbert Xu 	__be16 frag_off;
69736cf9acfSHerbert Xu 
698732c8bd5SHerbert Xu 	/* IP header length (excluding options or extension headers). */
699732c8bd5SHerbert Xu 	u8 ihl;
700732c8bd5SHerbert Xu 
70136cf9acfSHerbert Xu 	/* TOS for IPv4, class for IPv6. */
70236cf9acfSHerbert Xu 	u8 tos;
70336cf9acfSHerbert Xu 
70436cf9acfSHerbert Xu 	/* TTL for IPv4, hop limitfor IPv6. */
70536cf9acfSHerbert Xu 	u8 ttl;
70636cf9acfSHerbert Xu 
70736cf9acfSHerbert Xu 	/* Protocol for IPv4, NH for IPv6. */
70836cf9acfSHerbert Xu 	u8 protocol;
70936cf9acfSHerbert Xu 
710732c8bd5SHerbert Xu 	/* Option length for IPv4, zero for IPv6. */
711732c8bd5SHerbert Xu 	u8 optlen;
712732c8bd5SHerbert Xu 
71336cf9acfSHerbert Xu 	/* Used by IPv6 only, zero for IPv4. */
71436cf9acfSHerbert Xu 	u8 flow_lbl[3];
71536cf9acfSHerbert Xu };
71636cf9acfSHerbert Xu 
71736cf9acfSHerbert Xu #define XFRM_MODE_SKB_CB(__skb) ((struct xfrm_mode_skb_cb *)&((__skb)->cb[0]))
71836cf9acfSHerbert Xu 
719716062fdSHerbert Xu /*
720716062fdSHerbert Xu  * This structure is used by the input processing to locate the SPI and
721716062fdSHerbert Xu  * related information.
722716062fdSHerbert Xu  */
723716062fdSHerbert Xu struct xfrm_spi_skb_cb {
72470be6c91SSteffen Klassert 	struct xfrm_tunnel_skb_cb header;
725716062fdSHerbert Xu 
726716062fdSHerbert Xu 	unsigned int daddroff;
7272fcb45b6SHerbert Xu 	unsigned int family;
7287785bba2SSteffen Klassert 	__be32 seq;
729716062fdSHerbert Xu };
730716062fdSHerbert Xu 
731716062fdSHerbert Xu #define XFRM_SPI_SKB_CB(__skb) ((struct xfrm_spi_skb_cb *)&((__skb)->cb[0]))
732716062fdSHerbert Xu 
733c9204d9cSJoy Latten #ifdef CONFIG_AUDITSYSCALL
734afeb14b4SPaul Moore static inline struct audit_buffer *xfrm_audit_start(const char *op)
735ab5f5e8bSJoy Latten {
736ab5f5e8bSJoy Latten 	struct audit_buffer *audit_buf = NULL;
737ab5f5e8bSJoy Latten 
738afeb14b4SPaul Moore 	if (audit_enabled == 0)
739afeb14b4SPaul Moore 		return NULL;
740ab5f5e8bSJoy Latten 	audit_buf = audit_log_start(current->audit_context, GFP_ATOMIC,
741ab5f5e8bSJoy Latten 				    AUDIT_MAC_IPSEC_EVENT);
742ab5f5e8bSJoy Latten 	if (audit_buf == NULL)
743ab5f5e8bSJoy Latten 		return NULL;
744afeb14b4SPaul Moore 	audit_log_format(audit_buf, "op=%s", op);
745afeb14b4SPaul Moore 	return audit_buf;
746afeb14b4SPaul Moore }
747afeb14b4SPaul Moore 
7482e71029eSTetsuo Handa static inline void xfrm_audit_helper_usrinfo(bool task_valid,
749afeb14b4SPaul Moore 					     struct audit_buffer *audit_buf)
750afeb14b4SPaul Moore {
7512e71029eSTetsuo Handa 	const unsigned int auid = from_kuid(&init_user_ns, task_valid ?
7522e71029eSTetsuo Handa 					    audit_get_loginuid(current) :
7532e71029eSTetsuo Handa 					    INVALID_UID);
7542e71029eSTetsuo Handa 	const unsigned int ses = task_valid ? audit_get_sessionid(current) :
7552e71029eSTetsuo Handa 		(unsigned int) -1;
7562e71029eSTetsuo Handa 
7572e71029eSTetsuo Handa 	audit_log_format(audit_buf, " auid=%u ses=%u", auid, ses);
758ab5f5e8bSJoy Latten 	audit_log_task_context(audit_buf);
759ab5f5e8bSJoy Latten }
760ab5f5e8bSJoy Latten 
7612e71029eSTetsuo Handa void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid);
7622e71029eSTetsuo Handa void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
7632e71029eSTetsuo Handa 			      bool task_valid);
7642e71029eSTetsuo Handa void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid);
7652e71029eSTetsuo Handa void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid);
766d511337aSJoe Perches void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
767afeb14b4SPaul Moore 				      struct sk_buff *skb);
768d511337aSJoe Perches void xfrm_audit_state_replay(struct xfrm_state *x, struct sk_buff *skb,
769d511337aSJoe Perches 			     __be32 net_seq);
770d511337aSJoe Perches void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family);
771d511337aSJoe Perches void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family, __be32 net_spi,
772d511337aSJoe Perches 			       __be32 net_seq);
773d511337aSJoe Perches void xfrm_audit_state_icvfail(struct xfrm_state *x, struct sk_buff *skb,
774d511337aSJoe Perches 			      u8 proto);
775c9204d9cSJoy Latten #else
77641fef0eeSMarcin Slusarz 
77741fef0eeSMarcin Slusarz static inline void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
7782e71029eSTetsuo Handa 					 bool task_valid)
77941fef0eeSMarcin Slusarz {
78041fef0eeSMarcin Slusarz }
78141fef0eeSMarcin Slusarz 
78241fef0eeSMarcin Slusarz static inline void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
7832e71029eSTetsuo Handa 					    bool task_valid)
78441fef0eeSMarcin Slusarz {
78541fef0eeSMarcin Slusarz }
78641fef0eeSMarcin Slusarz 
78741fef0eeSMarcin Slusarz static inline void xfrm_audit_state_add(struct xfrm_state *x, int result,
7882e71029eSTetsuo Handa 					bool task_valid)
78941fef0eeSMarcin Slusarz {
79041fef0eeSMarcin Slusarz }
79141fef0eeSMarcin Slusarz 
79241fef0eeSMarcin Slusarz static inline void xfrm_audit_state_delete(struct xfrm_state *x, int result,
7932e71029eSTetsuo Handa 					   bool task_valid)
79441fef0eeSMarcin Slusarz {
79541fef0eeSMarcin Slusarz }
79641fef0eeSMarcin Slusarz 
79741fef0eeSMarcin Slusarz static inline void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
79841fef0eeSMarcin Slusarz 					     struct sk_buff *skb)
79941fef0eeSMarcin Slusarz {
80041fef0eeSMarcin Slusarz }
80141fef0eeSMarcin Slusarz 
8029fdc4883SSteffen Klassert static inline void xfrm_audit_state_replay(struct xfrm_state *x,
8039fdc4883SSteffen Klassert 					   struct sk_buff *skb, __be32 net_seq)
8049fdc4883SSteffen Klassert {
8059fdc4883SSteffen Klassert }
8069fdc4883SSteffen Klassert 
80741fef0eeSMarcin Slusarz static inline void xfrm_audit_state_notfound_simple(struct sk_buff *skb,
80841fef0eeSMarcin Slusarz 				      u16 family)
80941fef0eeSMarcin Slusarz {
81041fef0eeSMarcin Slusarz }
81141fef0eeSMarcin Slusarz 
81241fef0eeSMarcin Slusarz static inline void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
81341fef0eeSMarcin Slusarz 				      __be32 net_spi, __be32 net_seq)
81441fef0eeSMarcin Slusarz {
81541fef0eeSMarcin Slusarz }
81641fef0eeSMarcin Slusarz 
81741fef0eeSMarcin Slusarz static inline void xfrm_audit_state_icvfail(struct xfrm_state *x,
81841fef0eeSMarcin Slusarz 				     struct sk_buff *skb, u8 proto)
81941fef0eeSMarcin Slusarz {
82041fef0eeSMarcin Slusarz }
821c9204d9cSJoy Latten #endif /* CONFIG_AUDITSYSCALL */
822161a09e7SJoy Latten 
8231da177e4SLinus Torvalds static inline void xfrm_pol_hold(struct xfrm_policy *policy)
8241da177e4SLinus Torvalds {
8251da177e4SLinus Torvalds 	if (likely(policy != NULL))
826850a6212SReshetova, Elena 		refcount_inc(&policy->refcnt);
8271da177e4SLinus Torvalds }
8281da177e4SLinus Torvalds 
829d511337aSJoe Perches void xfrm_policy_destroy(struct xfrm_policy *policy);
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds static inline void xfrm_pol_put(struct xfrm_policy *policy)
8321da177e4SLinus Torvalds {
833850a6212SReshetova, Elena 	if (refcount_dec_and_test(&policy->refcnt))
83464c31b3fSWANG Cong 		xfrm_policy_destroy(policy);
8351da177e4SLinus Torvalds }
8361da177e4SLinus Torvalds 
8374e81bb83SMasahide NAKAMURA static inline void xfrm_pols_put(struct xfrm_policy **pols, int npols)
8384e81bb83SMasahide NAKAMURA {
8394e81bb83SMasahide NAKAMURA 	int i;
8404e81bb83SMasahide NAKAMURA 	for (i = npols - 1; i >= 0; --i)
8414e81bb83SMasahide NAKAMURA 		xfrm_pol_put(pols[i]);
8424e81bb83SMasahide NAKAMURA }
8434e81bb83SMasahide NAKAMURA 
844d511337aSJoe Perches void __xfrm_state_destroy(struct xfrm_state *);
8451da177e4SLinus Torvalds 
84621380b81SHerbert Xu static inline void __xfrm_state_put(struct xfrm_state *x)
84721380b81SHerbert Xu {
84888755e9cSReshetova, Elena 	refcount_dec(&x->refcnt);
84921380b81SHerbert Xu }
85021380b81SHerbert Xu 
8511da177e4SLinus Torvalds static inline void xfrm_state_put(struct xfrm_state *x)
8521da177e4SLinus Torvalds {
85388755e9cSReshetova, Elena 	if (refcount_dec_and_test(&x->refcnt))
8541da177e4SLinus Torvalds 		__xfrm_state_destroy(x);
8551da177e4SLinus Torvalds }
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds static inline void xfrm_state_hold(struct xfrm_state *x)
8581da177e4SLinus Torvalds {
85988755e9cSReshetova, Elena 	refcount_inc(&x->refcnt);
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds 
8621744a8feSDavid S. Miller static inline bool addr_match(const void *token1, const void *token2,
863e1b0048eSAlexey Dobriyan 			      unsigned int prefixlen)
8641da177e4SLinus Torvalds {
8651744a8feSDavid S. Miller 	const __be32 *a1 = token1;
8661744a8feSDavid S. Miller 	const __be32 *a2 = token2;
867e1b0048eSAlexey Dobriyan 	unsigned int pdw;
868e1b0048eSAlexey Dobriyan 	unsigned int pbi;
8691da177e4SLinus Torvalds 
870a6337463Sjamal 	pdw = prefixlen >> 5;	  /* num of whole u32 in prefix */
8711da177e4SLinus Torvalds 	pbi = prefixlen &  0x1f;  /* num of bits in incomplete u32 in prefix */
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	if (pdw)
8741da177e4SLinus Torvalds 		if (memcmp(a1, a2, pdw << 2))
8751744a8feSDavid S. Miller 			return false;
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	if (pbi) {
8785f19343fSAl Viro 		__be32 mask;
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 		mask = htonl((0xffffffff) << (32 - pbi));
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 		if ((a1[pdw] ^ a2[pdw]) & mask)
8831744a8feSDavid S. Miller 			return false;
8841da177e4SLinus Torvalds 	}
8851da177e4SLinus Torvalds 
8861744a8feSDavid S. Miller 	return true;
8871da177e4SLinus Torvalds }
8881da177e4SLinus Torvalds 
88926bff940SAlexey Dobriyan static inline bool addr4_match(__be32 a1, __be32 a2, u8 prefixlen)
89026bff940SAlexey Dobriyan {
89126bff940SAlexey Dobriyan 	/* C99 6.5.7 (3): u32 << 32 is undefined behaviour */
8926c786bcbSAlexey Dobriyan 	if (sizeof(long) == 4 && prefixlen == 0)
89326bff940SAlexey Dobriyan 		return true;
8946c786bcbSAlexey Dobriyan 	return !((a1 ^ a2) & htonl(~0UL << (32 - prefixlen)));
89526bff940SAlexey Dobriyan }
89626bff940SAlexey Dobriyan 
8971da177e4SLinus Torvalds static __inline__
8986281dcc9SDavid S. Miller __be16 xfrm_flowi_sport(const struct flowi *fl, const union flowi_uli *uli)
8991da177e4SLinus Torvalds {
900f9d07e41SAl Viro 	__be16 port;
9011d28f42cSDavid S. Miller 	switch(fl->flowi_proto) {
9021da177e4SLinus Torvalds 	case IPPROTO_TCP:
9031da177e4SLinus Torvalds 	case IPPROTO_UDP:
904ba4e58ecSGerrit Renker 	case IPPROTO_UDPLITE:
9051da177e4SLinus Torvalds 	case IPPROTO_SCTP:
9066281dcc9SDavid S. Miller 		port = uli->ports.sport;
9071da177e4SLinus Torvalds 		break;
9081da177e4SLinus Torvalds 	case IPPROTO_ICMP:
9091da177e4SLinus Torvalds 	case IPPROTO_ICMPV6:
9106281dcc9SDavid S. Miller 		port = htons(uli->icmpt.type);
9111da177e4SLinus Torvalds 		break;
9122ce4272aSMasahide NAKAMURA 	case IPPROTO_MH:
9136281dcc9SDavid S. Miller 		port = htons(uli->mht.type);
9142ce4272aSMasahide NAKAMURA 		break;
915cc9ff19dSTimo Teräs 	case IPPROTO_GRE:
9166281dcc9SDavid S. Miller 		port = htons(ntohl(uli->gre_key) >> 16);
917cc9ff19dSTimo Teräs 		break;
9181da177e4SLinus Torvalds 	default:
9191da177e4SLinus Torvalds 		port = 0;	/*XXX*/
9201da177e4SLinus Torvalds 	}
9211da177e4SLinus Torvalds 	return port;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds static __inline__
9256281dcc9SDavid S. Miller __be16 xfrm_flowi_dport(const struct flowi *fl, const union flowi_uli *uli)
9261da177e4SLinus Torvalds {
927f9d07e41SAl Viro 	__be16 port;
9281d28f42cSDavid S. Miller 	switch(fl->flowi_proto) {
9291da177e4SLinus Torvalds 	case IPPROTO_TCP:
9301da177e4SLinus Torvalds 	case IPPROTO_UDP:
931ba4e58ecSGerrit Renker 	case IPPROTO_UDPLITE:
9321da177e4SLinus Torvalds 	case IPPROTO_SCTP:
9336281dcc9SDavid S. Miller 		port = uli->ports.dport;
9341da177e4SLinus Torvalds 		break;
9351da177e4SLinus Torvalds 	case IPPROTO_ICMP:
9361da177e4SLinus Torvalds 	case IPPROTO_ICMPV6:
9376281dcc9SDavid S. Miller 		port = htons(uli->icmpt.code);
9381da177e4SLinus Torvalds 		break;
939cc9ff19dSTimo Teräs 	case IPPROTO_GRE:
9406281dcc9SDavid S. Miller 		port = htons(ntohl(uli->gre_key) & 0xffff);
941cc9ff19dSTimo Teräs 		break;
9421da177e4SLinus Torvalds 	default:
9431da177e4SLinus Torvalds 		port = 0;	/*XXX*/
9441da177e4SLinus Torvalds 	}
9451da177e4SLinus Torvalds 	return port;
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
948d511337aSJoe Perches bool xfrm_selector_match(const struct xfrm_selector *sel,
949d511337aSJoe Perches 			 const struct flowi *fl, unsigned short family);
9501da177e4SLinus Torvalds 
951df71837dSTrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
952df71837dSTrent Jaeger /*	If neither has a context --> match
953df71837dSTrent Jaeger  * 	Otherwise, both must have a context and the sids, doi, alg must match
954df71837dSTrent Jaeger  */
955bc9b35adSDavid S. Miller static inline bool xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
956df71837dSTrent Jaeger {
957df71837dSTrent Jaeger 	return ((!s1 && !s2) ||
958df71837dSTrent Jaeger 		(s1 && s2 &&
959df71837dSTrent Jaeger 		 (s1->ctx_sid == s2->ctx_sid) &&
960df71837dSTrent Jaeger 		 (s1->ctx_doi == s2->ctx_doi) &&
961df71837dSTrent Jaeger 		 (s1->ctx_alg == s2->ctx_alg)));
962df71837dSTrent Jaeger }
963df71837dSTrent Jaeger #else
964bc9b35adSDavid S. Miller static inline bool xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
965df71837dSTrent Jaeger {
966bc9b35adSDavid S. Miller 	return true;
967df71837dSTrent Jaeger }
968df71837dSTrent Jaeger #endif
969df71837dSTrent Jaeger 
9701da177e4SLinus Torvalds /* A struct encoding bundle of transformations to apply to some set of flow.
9711da177e4SLinus Torvalds  *
972b6ca8bd5SDavid Miller  * xdst->child points to the next element of bundle.
9731da177e4SLinus Torvalds  * dst->xfrm  points to an instanse of transformer.
9741da177e4SLinus Torvalds  *
9751da177e4SLinus Torvalds  * Due to unfortunate limitations of current routing cache, which we
9761da177e4SLinus Torvalds  * have no time to fix, it mirrors struct rtable and bound to the same
9771da177e4SLinus Torvalds  * routing key, including saddr,daddr. However, we can have many of
9781da177e4SLinus Torvalds  * bundles differing by session id. All the bundles grow from a parent
9791da177e4SLinus Torvalds  * policy rule.
9801da177e4SLinus Torvalds  */
981fd2c3ef7SEric Dumazet struct xfrm_dst {
9821da177e4SLinus Torvalds 	union {
9831da177e4SLinus Torvalds 		struct dst_entry	dst;
9841da177e4SLinus Torvalds 		struct rtable		rt;
9851da177e4SLinus Torvalds 		struct rt6_info		rt6;
9861da177e4SLinus Torvalds 	} u;
9871da177e4SLinus Torvalds 	struct dst_entry *route;
988b6ca8bd5SDavid Miller 	struct dst_entry *child;
9890f6c480fSDavid Miller 	struct dst_entry *path;
99080c802f3STimo Teräs 	struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
99180c802f3STimo Teräs 	int num_pols, num_xfrms;
99280c802f3STimo Teräs 	u32 xfrm_genid;
99380c802f3STimo Teräs 	u32 policy_genid;
9941da177e4SLinus Torvalds 	u32 route_mtu_cached;
9951da177e4SLinus Torvalds 	u32 child_mtu_cached;
99692d63decSHideaki YOSHIFUJI 	u32 route_cookie;
99792d63decSHideaki YOSHIFUJI 	u32 path_cookie;
9981da177e4SLinus Torvalds };
9991da177e4SLinus Torvalds 
10000f6c480fSDavid Miller static inline struct dst_entry *xfrm_dst_path(const struct dst_entry *dst)
10010f6c480fSDavid Miller {
10020f6c480fSDavid Miller #ifdef CONFIG_XFRM
10030f6c480fSDavid Miller 	if (dst->xfrm) {
10040f6c480fSDavid Miller 		const struct xfrm_dst *xdst = (const struct xfrm_dst *) dst;
10050f6c480fSDavid Miller 
10060f6c480fSDavid Miller 		return xdst->path;
10070f6c480fSDavid Miller 	}
10080f6c480fSDavid Miller #endif
10090f6c480fSDavid Miller 	return (struct dst_entry *) dst;
10100f6c480fSDavid Miller }
10110f6c480fSDavid Miller 
1012b92cf4aaSDavid Miller static inline struct dst_entry *xfrm_dst_child(const struct dst_entry *dst)
1013b92cf4aaSDavid Miller {
1014b92cf4aaSDavid Miller #ifdef CONFIG_XFRM
1015b6ca8bd5SDavid Miller 	if (dst->xfrm) {
1016b6ca8bd5SDavid Miller 		struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
1017b6ca8bd5SDavid Miller 		return xdst->child;
1018b6ca8bd5SDavid Miller 	}
1019b92cf4aaSDavid Miller #endif
1020b92cf4aaSDavid Miller 	return NULL;
1021b92cf4aaSDavid Miller }
1022b92cf4aaSDavid Miller 
1023def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
102445b018beSDavid Miller static inline void xfrm_dst_set_child(struct xfrm_dst *xdst, struct dst_entry *child)
102545b018beSDavid Miller {
1026b6ca8bd5SDavid Miller 	xdst->child = child;
102745b018beSDavid Miller }
102845b018beSDavid Miller 
1029aabc9761SHerbert Xu static inline void xfrm_dst_destroy(struct xfrm_dst *xdst)
1030aabc9761SHerbert Xu {
103180c802f3STimo Teräs 	xfrm_pols_put(xdst->pols, xdst->num_pols);
1032aabc9761SHerbert Xu 	dst_release(xdst->route);
1033aabc9761SHerbert Xu 	if (likely(xdst->u.dst.xfrm))
1034aabc9761SHerbert Xu 		xfrm_state_put(xdst->u.dst.xfrm);
1035aabc9761SHerbert Xu }
1036def8b4faSAlexey Dobriyan #endif
1037aabc9761SHerbert Xu 
1038d511337aSJoe Perches void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev);
1039aabc9761SHerbert Xu 
104054ef207aSSteffen Klassert struct xfrm_offload {
104154ef207aSSteffen Klassert 	/* Output sequence number for replay protection on offloading. */
104254ef207aSSteffen Klassert 	struct {
104354ef207aSSteffen Klassert 		__u32 low;
104454ef207aSSteffen Klassert 		__u32 hi;
104554ef207aSSteffen Klassert 	} seq;
104654ef207aSSteffen Klassert 
104754ef207aSSteffen Klassert 	__u32			flags;
104854ef207aSSteffen Klassert #define	SA_DELETE_REQ		1
104954ef207aSSteffen Klassert #define	CRYPTO_DONE		2
105054ef207aSSteffen Klassert #define	CRYPTO_NEXT_DONE	4
105154ef207aSSteffen Klassert #define	CRYPTO_FALLBACK		8
105254ef207aSSteffen Klassert #define	XFRM_GSO_SEGMENT	16
105354ef207aSSteffen Klassert #define	XFRM_GRO		32
105447ebcc0bSYossi Kuperman #define	XFRM_ESP_NO_TRAILER	64
1055f53c7239SSteffen Klassert #define	XFRM_DEV_RESUME		128
105654ef207aSSteffen Klassert 
105754ef207aSSteffen Klassert 	__u32			status;
105854ef207aSSteffen Klassert #define CRYPTO_SUCCESS				1
105954ef207aSSteffen Klassert #define CRYPTO_GENERIC_ERROR			2
106054ef207aSSteffen Klassert #define CRYPTO_TRANSPORT_AH_AUTH_FAILED		4
106154ef207aSSteffen Klassert #define CRYPTO_TRANSPORT_ESP_AUTH_FAILED	8
106254ef207aSSteffen Klassert #define CRYPTO_TUNNEL_AH_AUTH_FAILED		16
106354ef207aSSteffen Klassert #define CRYPTO_TUNNEL_ESP_AUTH_FAILED		32
106454ef207aSSteffen Klassert #define CRYPTO_INVALID_PACKET_SYNTAX		64
106554ef207aSSteffen Klassert #define CRYPTO_INVALID_PROTOCOL			128
106654ef207aSSteffen Klassert 
106754ef207aSSteffen Klassert 	__u8			proto;
106854ef207aSSteffen Klassert };
106954ef207aSSteffen Klassert 
1070fd2c3ef7SEric Dumazet struct sec_path {
107155eabed6SReshetova, Elena 	refcount_t		refcnt;
10721da177e4SLinus Torvalds 	int			len;
107354ef207aSSteffen Klassert 	int			olen;
107454ef207aSSteffen Klassert 
1075dbe5b4aaSHerbert Xu 	struct xfrm_state	*xvec[XFRM_MAX_DEPTH];
107654ef207aSSteffen Klassert 	struct xfrm_offload	ovec[XFRM_MAX_OFFLOAD_DEPTH];
10771da177e4SLinus Torvalds };
10781da177e4SLinus Torvalds 
1079990078afSMichael Smith static inline int secpath_exists(struct sk_buff *skb)
1080990078afSMichael Smith {
1081990078afSMichael Smith #ifdef CONFIG_XFRM
1082990078afSMichael Smith 	return skb->sp != NULL;
1083990078afSMichael Smith #else
1084990078afSMichael Smith 	return 0;
1085990078afSMichael Smith #endif
1086990078afSMichael Smith }
1087990078afSMichael Smith 
10881da177e4SLinus Torvalds static inline struct sec_path *
10891da177e4SLinus Torvalds secpath_get(struct sec_path *sp)
10901da177e4SLinus Torvalds {
10911da177e4SLinus Torvalds 	if (sp)
109255eabed6SReshetova, Elena 		refcount_inc(&sp->refcnt);
10931da177e4SLinus Torvalds 	return sp;
10941da177e4SLinus Torvalds }
10951da177e4SLinus Torvalds 
1096d511337aSJoe Perches void __secpath_destroy(struct sec_path *sp);
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds static inline void
10991da177e4SLinus Torvalds secpath_put(struct sec_path *sp)
11001da177e4SLinus Torvalds {
110155eabed6SReshetova, Elena 	if (sp && refcount_dec_and_test(&sp->refcnt))
11021da177e4SLinus Torvalds 		__secpath_destroy(sp);
11031da177e4SLinus Torvalds }
11041da177e4SLinus Torvalds 
1105d511337aSJoe Perches struct sec_path *secpath_dup(struct sec_path *src);
1106b0fcee82SSteffen Klassert int secpath_set(struct sk_buff *skb);
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds static inline void
11091da177e4SLinus Torvalds secpath_reset(struct sk_buff *skb)
11101da177e4SLinus Torvalds {
11111da177e4SLinus Torvalds #ifdef CONFIG_XFRM
11121da177e4SLinus Torvalds 	secpath_put(skb->sp);
11131da177e4SLinus Torvalds 	skb->sp = NULL;
11141da177e4SLinus Torvalds #endif
11151da177e4SLinus Torvalds }
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds static inline int
11186cc32961SDavid S. Miller xfrm_addr_any(const xfrm_address_t *addr, unsigned short family)
1119a1e59abfSPatrick McHardy {
1120a1e59abfSPatrick McHardy 	switch (family) {
1121a1e59abfSPatrick McHardy 	case AF_INET:
1122a1e59abfSPatrick McHardy 		return addr->a4 == 0;
1123a1e59abfSPatrick McHardy 	case AF_INET6:
112415e318bdSJiri Benc 		return ipv6_addr_any(&addr->in6);
1125a1e59abfSPatrick McHardy 	}
1126a1e59abfSPatrick McHardy 	return 0;
1127a1e59abfSPatrick McHardy }
1128a1e59abfSPatrick McHardy 
1129a1e59abfSPatrick McHardy static inline int
113021eddb5cSDavid S. Miller __xfrm4_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x)
11311da177e4SLinus Torvalds {
11321da177e4SLinus Torvalds 	return	(tmpl->saddr.a4 &&
11331da177e4SLinus Torvalds 		 tmpl->saddr.a4 != x->props.saddr.a4);
11341da177e4SLinus Torvalds }
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds static inline int
113721eddb5cSDavid S. Miller __xfrm6_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x)
11381da177e4SLinus Torvalds {
11391da177e4SLinus Torvalds 	return	(!ipv6_addr_any((struct in6_addr*)&tmpl->saddr) &&
1140ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 		 !ipv6_addr_equal((struct in6_addr *)&tmpl->saddr, (struct in6_addr*)&x->props.saddr));
11411da177e4SLinus Torvalds }
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds static inline int
114421eddb5cSDavid S. Miller xfrm_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x, unsigned short family)
11451da177e4SLinus Torvalds {
11461da177e4SLinus Torvalds 	switch (family) {
11471da177e4SLinus Torvalds 	case AF_INET:
11481da177e4SLinus Torvalds 		return __xfrm4_state_addr_cmp(tmpl, x);
11491da177e4SLinus Torvalds 	case AF_INET6:
11501da177e4SLinus Torvalds 		return __xfrm6_state_addr_cmp(tmpl, x);
11511da177e4SLinus Torvalds 	}
11521da177e4SLinus Torvalds 	return !0;
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds #ifdef CONFIG_XFRM
1156d511337aSJoe Perches int __xfrm_policy_check(struct sock *, int dir, struct sk_buff *skb,
1157d511337aSJoe Perches 			unsigned short family);
11581da177e4SLinus Torvalds 
1159d5422efeSHerbert Xu static inline int __xfrm_policy_check2(struct sock *sk, int dir,
1160d5422efeSHerbert Xu 				       struct sk_buff *skb,
1161d5422efeSHerbert Xu 				       unsigned int family, int reverse)
11621da177e4SLinus Torvalds {
1163f6e1e25dSAlexey Dobriyan 	struct net *net = dev_net(skb->dev);
1164d5422efeSHerbert Xu 	int ndir = dir | (reverse ? XFRM_POLICY_MASK + 1 : 0);
1165d5422efeSHerbert Xu 
11661da177e4SLinus Torvalds 	if (sk && sk->sk_policy[XFRM_POLICY_IN])
1167d5422efeSHerbert Xu 		return __xfrm_policy_check(sk, ndir, skb, family);
11681da177e4SLinus Torvalds 
1169f6e1e25dSAlexey Dobriyan 	return	(!net->xfrm.policy_count[dir] && !skb->sp) ||
1170adf30907SEric Dumazet 		(skb_dst(skb)->flags & DST_NOPOLICY) ||
1171d5422efeSHerbert Xu 		__xfrm_policy_check(sk, ndir, skb, family);
1172d5422efeSHerbert Xu }
1173d5422efeSHerbert Xu 
1174d5422efeSHerbert Xu static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
1175d5422efeSHerbert Xu {
1176d5422efeSHerbert Xu 	return __xfrm_policy_check2(sk, dir, skb, family, 0);
11771da177e4SLinus Torvalds }
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
11801da177e4SLinus Torvalds {
11811da177e4SLinus Torvalds 	return xfrm_policy_check(sk, dir, skb, AF_INET);
11821da177e4SLinus Torvalds }
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
11851da177e4SLinus Torvalds {
11861da177e4SLinus Torvalds 	return xfrm_policy_check(sk, dir, skb, AF_INET6);
11871da177e4SLinus Torvalds }
11881da177e4SLinus Torvalds 
1189d5422efeSHerbert Xu static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir,
1190d5422efeSHerbert Xu 					     struct sk_buff *skb)
1191d5422efeSHerbert Xu {
1192d5422efeSHerbert Xu 	return __xfrm_policy_check2(sk, dir, skb, AF_INET, 1);
1193d5422efeSHerbert Xu }
1194d5422efeSHerbert Xu 
1195d5422efeSHerbert Xu static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir,
1196d5422efeSHerbert Xu 					     struct sk_buff *skb)
1197d5422efeSHerbert Xu {
1198d5422efeSHerbert Xu 	return __xfrm_policy_check2(sk, dir, skb, AF_INET6, 1);
1199d5422efeSHerbert Xu }
1200d5422efeSHerbert Xu 
1201d511337aSJoe Perches int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1202d5422efeSHerbert Xu 			  unsigned int family, int reverse);
1203d5422efeSHerbert Xu 
1204d5422efeSHerbert Xu static inline int xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1205d5422efeSHerbert Xu 				      unsigned int family)
1206d5422efeSHerbert Xu {
1207d5422efeSHerbert Xu 	return __xfrm_decode_session(skb, fl, family, 0);
1208d5422efeSHerbert Xu }
1209d5422efeSHerbert Xu 
1210d5422efeSHerbert Xu static inline int xfrm_decode_session_reverse(struct sk_buff *skb,
1211d5422efeSHerbert Xu 					      struct flowi *fl,
1212d5422efeSHerbert Xu 					      unsigned int family)
1213d5422efeSHerbert Xu {
1214d5422efeSHerbert Xu 	return __xfrm_decode_session(skb, fl, family, 1);
1215d5422efeSHerbert Xu }
1216d5422efeSHerbert Xu 
1217d511337aSJoe Perches int __xfrm_route_forward(struct sk_buff *skb, unsigned short family);
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds static inline int xfrm_route_forward(struct sk_buff *skb, unsigned short family)
12201da177e4SLinus Torvalds {
122199a66657SAlexey Dobriyan 	struct net *net = dev_net(skb->dev);
122299a66657SAlexey Dobriyan 
122399a66657SAlexey Dobriyan 	return	!net->xfrm.policy_count[XFRM_POLICY_OUT] ||
1224adf30907SEric Dumazet 		(skb_dst(skb)->flags & DST_NOXFRM) ||
12251da177e4SLinus Torvalds 		__xfrm_route_forward(skb, family);
12261da177e4SLinus Torvalds }
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds static inline int xfrm4_route_forward(struct sk_buff *skb)
12291da177e4SLinus Torvalds {
12301da177e4SLinus Torvalds 	return xfrm_route_forward(skb, AF_INET);
12311da177e4SLinus Torvalds }
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds static inline int xfrm6_route_forward(struct sk_buff *skb)
12341da177e4SLinus Torvalds {
12351da177e4SLinus Torvalds 	return xfrm_route_forward(skb, AF_INET6);
12361da177e4SLinus Torvalds }
12371da177e4SLinus Torvalds 
1238d188ba86SEric Dumazet int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk);
12391da177e4SLinus Torvalds 
1240d188ba86SEric Dumazet static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
12411da177e4SLinus Torvalds {
1242d188ba86SEric Dumazet 	sk->sk_policy[0] = NULL;
1243d188ba86SEric Dumazet 	sk->sk_policy[1] = NULL;
1244d188ba86SEric Dumazet 	if (unlikely(osk->sk_policy[0] || osk->sk_policy[1]))
1245d188ba86SEric Dumazet 		return __xfrm_sk_clone_policy(sk, osk);
12461da177e4SLinus Torvalds 	return 0;
12471da177e4SLinus Torvalds }
12481da177e4SLinus Torvalds 
1249d511337aSJoe Perches int xfrm_policy_delete(struct xfrm_policy *pol, int dir);
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds static inline void xfrm_sk_free_policy(struct sock *sk)
12521da177e4SLinus Torvalds {
1253d188ba86SEric Dumazet 	struct xfrm_policy *pol;
1254d188ba86SEric Dumazet 
1255d188ba86SEric Dumazet 	pol = rcu_dereference_protected(sk->sk_policy[0], 1);
1256d188ba86SEric Dumazet 	if (unlikely(pol != NULL)) {
1257d188ba86SEric Dumazet 		xfrm_policy_delete(pol, XFRM_POLICY_MAX);
12581da177e4SLinus Torvalds 		sk->sk_policy[0] = NULL;
12591da177e4SLinus Torvalds 	}
1260d188ba86SEric Dumazet 	pol = rcu_dereference_protected(sk->sk_policy[1], 1);
1261d188ba86SEric Dumazet 	if (unlikely(pol != NULL)) {
1262d188ba86SEric Dumazet 		xfrm_policy_delete(pol, XFRM_POLICY_MAX+1);
12631da177e4SLinus Torvalds 		sk->sk_policy[1] = NULL;
12641da177e4SLinus Torvalds 	}
12651da177e4SLinus Torvalds }
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds #else
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds static inline void xfrm_sk_free_policy(struct sock *sk) {}
1270d188ba86SEric Dumazet static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk) { return 0; }
12711da177e4SLinus Torvalds static inline int xfrm6_route_forward(struct sk_buff *skb) { return 1; }
12721da177e4SLinus Torvalds static inline int xfrm4_route_forward(struct sk_buff *skb) { return 1; }
12731da177e4SLinus Torvalds static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
12741da177e4SLinus Torvalds {
12751da177e4SLinus Torvalds 	return 1;
12761da177e4SLinus Torvalds }
12771da177e4SLinus Torvalds static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
12781da177e4SLinus Torvalds {
12791da177e4SLinus Torvalds 	return 1;
12801da177e4SLinus Torvalds }
12811da177e4SLinus Torvalds static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
12821da177e4SLinus Torvalds {
12831da177e4SLinus Torvalds 	return 1;
12841da177e4SLinus Torvalds }
1285d5422efeSHerbert Xu static inline int xfrm_decode_session_reverse(struct sk_buff *skb,
1286d5422efeSHerbert Xu 					      struct flowi *fl,
1287d5422efeSHerbert Xu 					      unsigned int family)
1288d5422efeSHerbert Xu {
1289d5422efeSHerbert Xu 	return -ENOSYS;
1290d5422efeSHerbert Xu }
1291d5422efeSHerbert Xu static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir,
1292d5422efeSHerbert Xu 					     struct sk_buff *skb)
1293d5422efeSHerbert Xu {
1294d5422efeSHerbert Xu 	return 1;
1295d5422efeSHerbert Xu }
1296d5422efeSHerbert Xu static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir,
1297d5422efeSHerbert Xu 					     struct sk_buff *skb)
1298d5422efeSHerbert Xu {
1299d5422efeSHerbert Xu 	return 1;
1300d5422efeSHerbert Xu }
13011da177e4SLinus Torvalds #endif
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds static __inline__
1304e8a4e377SDavid S. Miller xfrm_address_t *xfrm_flowi_daddr(const struct flowi *fl, unsigned short family)
13051da177e4SLinus Torvalds {
13061da177e4SLinus Torvalds 	switch (family){
13071da177e4SLinus Torvalds 	case AF_INET:
13087e1dc7b6SDavid S. Miller 		return (xfrm_address_t *)&fl->u.ip4.daddr;
13091da177e4SLinus Torvalds 	case AF_INET6:
13107e1dc7b6SDavid S. Miller 		return (xfrm_address_t *)&fl->u.ip6.daddr;
13111da177e4SLinus Torvalds 	}
13121da177e4SLinus Torvalds 	return NULL;
13131da177e4SLinus Torvalds }
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds static __inline__
1316e8a4e377SDavid S. Miller xfrm_address_t *xfrm_flowi_saddr(const struct flowi *fl, unsigned short family)
13171da177e4SLinus Torvalds {
13181da177e4SLinus Torvalds 	switch (family){
13191da177e4SLinus Torvalds 	case AF_INET:
13207e1dc7b6SDavid S. Miller 		return (xfrm_address_t *)&fl->u.ip4.saddr;
13211da177e4SLinus Torvalds 	case AF_INET6:
13227e1dc7b6SDavid S. Miller 		return (xfrm_address_t *)&fl->u.ip6.saddr;
13231da177e4SLinus Torvalds 	}
13241da177e4SLinus Torvalds 	return NULL;
13251da177e4SLinus Torvalds }
13261da177e4SLinus Torvalds 
13279bb182a7SYOSHIFUJI Hideaki static __inline__
1328e8a4e377SDavid S. Miller void xfrm_flowi_addr_get(const struct flowi *fl,
13299bb182a7SYOSHIFUJI Hideaki 			 xfrm_address_t *saddr, xfrm_address_t *daddr,
13309bb182a7SYOSHIFUJI Hideaki 			 unsigned short family)
13319bb182a7SYOSHIFUJI Hideaki {
13329bb182a7SYOSHIFUJI Hideaki 	switch(family) {
13339bb182a7SYOSHIFUJI Hideaki 	case AF_INET:
13347e1dc7b6SDavid S. Miller 		memcpy(&saddr->a4, &fl->u.ip4.saddr, sizeof(saddr->a4));
13357e1dc7b6SDavid S. Miller 		memcpy(&daddr->a4, &fl->u.ip4.daddr, sizeof(daddr->a4));
13369bb182a7SYOSHIFUJI Hideaki 		break;
13379bb182a7SYOSHIFUJI Hideaki 	case AF_INET6:
133815e318bdSJiri Benc 		saddr->in6 = fl->u.ip6.saddr;
133915e318bdSJiri Benc 		daddr->in6 = fl->u.ip6.daddr;
13409bb182a7SYOSHIFUJI Hideaki 		break;
13419bb182a7SYOSHIFUJI Hideaki 	}
13429bb182a7SYOSHIFUJI Hideaki }
13439bb182a7SYOSHIFUJI Hideaki 
13441da177e4SLinus Torvalds static __inline__ int
1345f8848067SDavid S. Miller __xfrm4_state_addr_check(const struct xfrm_state *x,
1346f8848067SDavid S. Miller 			 const xfrm_address_t *daddr, const xfrm_address_t *saddr)
13471da177e4SLinus Torvalds {
13481da177e4SLinus Torvalds 	if (daddr->a4 == x->id.daddr.a4 &&
13491da177e4SLinus Torvalds 	    (saddr->a4 == x->props.saddr.a4 || !saddr->a4 || !x->props.saddr.a4))
13501da177e4SLinus Torvalds 		return 1;
13511da177e4SLinus Torvalds 	return 0;
13521da177e4SLinus Torvalds }
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds static __inline__ int
1355f8848067SDavid S. Miller __xfrm6_state_addr_check(const struct xfrm_state *x,
1356f8848067SDavid S. Miller 			 const xfrm_address_t *daddr, const xfrm_address_t *saddr)
13571da177e4SLinus Torvalds {
1358ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal((struct in6_addr *)daddr, (struct in6_addr *)&x->id.daddr) &&
1359ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 	    (ipv6_addr_equal((struct in6_addr *)saddr, (struct in6_addr *)&x->props.saddr) ||
13601da177e4SLinus Torvalds 	     ipv6_addr_any((struct in6_addr *)saddr) ||
13611da177e4SLinus Torvalds 	     ipv6_addr_any((struct in6_addr *)&x->props.saddr)))
13621da177e4SLinus Torvalds 		return 1;
13631da177e4SLinus Torvalds 	return 0;
13641da177e4SLinus Torvalds }
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds static __inline__ int
1367f8848067SDavid S. Miller xfrm_state_addr_check(const struct xfrm_state *x,
1368f8848067SDavid S. Miller 		      const xfrm_address_t *daddr, const xfrm_address_t *saddr,
13691da177e4SLinus Torvalds 		      unsigned short family)
13701da177e4SLinus Torvalds {
13711da177e4SLinus Torvalds 	switch (family) {
13721da177e4SLinus Torvalds 	case AF_INET:
13731da177e4SLinus Torvalds 		return __xfrm4_state_addr_check(x, daddr, saddr);
13741da177e4SLinus Torvalds 	case AF_INET6:
13751da177e4SLinus Torvalds 		return __xfrm6_state_addr_check(x, daddr, saddr);
13761da177e4SLinus Torvalds 	}
13771da177e4SLinus Torvalds 	return 0;
13781da177e4SLinus Torvalds }
13791da177e4SLinus Torvalds 
1380e53820deSMasahide NAKAMURA static __inline__ int
1381f8848067SDavid S. Miller xfrm_state_addr_flow_check(const struct xfrm_state *x, const struct flowi *fl,
1382e53820deSMasahide NAKAMURA 			   unsigned short family)
1383e53820deSMasahide NAKAMURA {
1384e53820deSMasahide NAKAMURA 	switch (family) {
1385e53820deSMasahide NAKAMURA 	case AF_INET:
1386e53820deSMasahide NAKAMURA 		return __xfrm4_state_addr_check(x,
13877e1dc7b6SDavid S. Miller 						(const xfrm_address_t *)&fl->u.ip4.daddr,
13887e1dc7b6SDavid S. Miller 						(const xfrm_address_t *)&fl->u.ip4.saddr);
1389e53820deSMasahide NAKAMURA 	case AF_INET6:
1390e53820deSMasahide NAKAMURA 		return __xfrm6_state_addr_check(x,
13917e1dc7b6SDavid S. Miller 						(const xfrm_address_t *)&fl->u.ip6.daddr,
13927e1dc7b6SDavid S. Miller 						(const xfrm_address_t *)&fl->u.ip6.saddr);
1393e53820deSMasahide NAKAMURA 	}
1394e53820deSMasahide NAKAMURA 	return 0;
1395e53820deSMasahide NAKAMURA }
1396e53820deSMasahide NAKAMURA 
1397f8848067SDavid S. Miller static inline int xfrm_state_kern(const struct xfrm_state *x)
13981da177e4SLinus Torvalds {
13991da177e4SLinus Torvalds 	return atomic_read(&x->tunnel_users);
14001da177e4SLinus Torvalds }
14011da177e4SLinus Torvalds 
14025794708fSMasahide NAKAMURA static inline int xfrm_id_proto_match(u8 proto, u8 userproto)
14035794708fSMasahide NAKAMURA {
1404dc00a525SMasahide NAKAMURA 	return (!userproto || proto == userproto ||
1405dc00a525SMasahide NAKAMURA 		(userproto == IPSEC_PROTO_ANY && (proto == IPPROTO_AH ||
1406dc00a525SMasahide NAKAMURA 						  proto == IPPROTO_ESP ||
1407dc00a525SMasahide NAKAMURA 						  proto == IPPROTO_COMP)));
14085794708fSMasahide NAKAMURA }
14095794708fSMasahide NAKAMURA 
14101da177e4SLinus Torvalds /*
14111da177e4SLinus Torvalds  * xfrm algorithm information
14121da177e4SLinus Torvalds  */
14131a6509d9SHerbert Xu struct xfrm_algo_aead_info {
1414165ecc63SHerbert Xu 	char *geniv;
14151a6509d9SHerbert Xu 	u16 icv_truncbits;
14161a6509d9SHerbert Xu };
14171a6509d9SHerbert Xu 
14181da177e4SLinus Torvalds struct xfrm_algo_auth_info {
14191da177e4SLinus Torvalds 	u16 icv_truncbits;
14201da177e4SLinus Torvalds 	u16 icv_fullbits;
14211da177e4SLinus Torvalds };
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds struct xfrm_algo_encr_info {
1424165ecc63SHerbert Xu 	char *geniv;
14251da177e4SLinus Torvalds 	u16 blockbits;
14261da177e4SLinus Torvalds 	u16 defkeybits;
14271da177e4SLinus Torvalds };
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds struct xfrm_algo_comp_info {
14301da177e4SLinus Torvalds 	u16 threshold;
14311da177e4SLinus Torvalds };
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds struct xfrm_algo_desc {
14341da177e4SLinus Torvalds 	char *name;
143504ff1260SHerbert Xu 	char *compat;
14361da177e4SLinus Torvalds 	u8 available:1;
14377e50f84cSJussi Kivilinna 	u8 pfkey_supported:1;
14381da177e4SLinus Torvalds 	union {
14391a6509d9SHerbert Xu 		struct xfrm_algo_aead_info aead;
14401da177e4SLinus Torvalds 		struct xfrm_algo_auth_info auth;
14411da177e4SLinus Torvalds 		struct xfrm_algo_encr_info encr;
14421da177e4SLinus Torvalds 		struct xfrm_algo_comp_info comp;
14431da177e4SLinus Torvalds 	} uinfo;
14441da177e4SLinus Torvalds 	struct sadb_alg desc;
14451da177e4SLinus Torvalds };
14461da177e4SLinus Torvalds 
14473328715eSSteffen Klassert /* XFRM protocol handlers.  */
14483328715eSSteffen Klassert struct xfrm4_protocol {
14493328715eSSteffen Klassert 	int (*handler)(struct sk_buff *skb);
14503328715eSSteffen Klassert 	int (*input_handler)(struct sk_buff *skb, int nexthdr, __be32 spi,
14513328715eSSteffen Klassert 			     int encap_type);
14523328715eSSteffen Klassert 	int (*cb_handler)(struct sk_buff *skb, int err);
14533328715eSSteffen Klassert 	int (*err_handler)(struct sk_buff *skb, u32 info);
14543328715eSSteffen Klassert 
14553328715eSSteffen Klassert 	struct xfrm4_protocol __rcu *next;
14563328715eSSteffen Klassert 	int priority;
14573328715eSSteffen Klassert };
14583328715eSSteffen Klassert 
14597e14ea15SSteffen Klassert struct xfrm6_protocol {
14607e14ea15SSteffen Klassert 	int (*handler)(struct sk_buff *skb);
14617e14ea15SSteffen Klassert 	int (*cb_handler)(struct sk_buff *skb, int err);
14627e14ea15SSteffen Klassert 	int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
14637e14ea15SSteffen Klassert 			   u8 type, u8 code, int offset, __be32 info);
14647e14ea15SSteffen Klassert 
14657e14ea15SSteffen Klassert 	struct xfrm6_protocol __rcu *next;
14667e14ea15SSteffen Klassert 	int priority;
14677e14ea15SSteffen Klassert };
14687e14ea15SSteffen Klassert 
14691da177e4SLinus Torvalds /* XFRM tunnel handlers.  */
14701da177e4SLinus Torvalds struct xfrm_tunnel {
14711da177e4SLinus Torvalds 	int (*handler)(struct sk_buff *skb);
1472a6337463Sjamal 	int (*err_handler)(struct sk_buff *skb, u32 info);
1473d2acc347SHerbert Xu 
1474b33eab08SEric Dumazet 	struct xfrm_tunnel __rcu *next;
1475d2acc347SHerbert Xu 	int priority;
14761da177e4SLinus Torvalds };
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds struct xfrm6_tunnel {
1479d2acc347SHerbert Xu 	int (*handler)(struct sk_buff *skb);
1480d2acc347SHerbert Xu 	int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
1481d5fdd6baSBrian Haley 			   u8 type, u8 code, int offset, __be32 info);
14826f0bcf15SEric Dumazet 	struct xfrm6_tunnel __rcu *next;
1483d2acc347SHerbert Xu 	int priority;
14841da177e4SLinus Torvalds };
14851da177e4SLinus Torvalds 
1486d511337aSJoe Perches void xfrm_init(void);
1487d511337aSJoe Perches void xfrm4_init(void);
1488d511337aSJoe Perches int xfrm_state_init(struct net *net);
1489d511337aSJoe Perches void xfrm_state_fini(struct net *net);
1490d511337aSJoe Perches void xfrm4_state_init(void);
14912f32b51bSSteffen Klassert void xfrm4_protocol_init(void);
1492c35b7e72SDaniel Lezcano #ifdef CONFIG_XFRM
1493d511337aSJoe Perches int xfrm6_init(void);
1494d511337aSJoe Perches void xfrm6_fini(void);
1495d511337aSJoe Perches int xfrm6_state_init(void);
1496d511337aSJoe Perches void xfrm6_state_fini(void);
14977e14ea15SSteffen Klassert int xfrm6_protocol_init(void);
14987e14ea15SSteffen Klassert void xfrm6_protocol_fini(void);
1499c35b7e72SDaniel Lezcano #else
1500c35b7e72SDaniel Lezcano static inline int xfrm6_init(void)
1501c35b7e72SDaniel Lezcano {
1502c35b7e72SDaniel Lezcano 	return 0;
1503c35b7e72SDaniel Lezcano }
1504c35b7e72SDaniel Lezcano static inline void xfrm6_fini(void)
1505c35b7e72SDaniel Lezcano {
1506c35b7e72SDaniel Lezcano 	;
1507c35b7e72SDaniel Lezcano }
1508c35b7e72SDaniel Lezcano #endif
15091da177e4SLinus Torvalds 
1510558f82efSMasahide NAKAMURA #ifdef CONFIG_XFRM_STATISTICS
1511d511337aSJoe Perches int xfrm_proc_init(struct net *net);
1512d511337aSJoe Perches void xfrm_proc_fini(struct net *net);
1513558f82efSMasahide NAKAMURA #endif
1514558f82efSMasahide NAKAMURA 
1515d511337aSJoe Perches int xfrm_sysctl_init(struct net *net);
1516b27aeadbSAlexey Dobriyan #ifdef CONFIG_SYSCTL
1517d511337aSJoe Perches void xfrm_sysctl_fini(struct net *net);
1518b27aeadbSAlexey Dobriyan #else
1519b27aeadbSAlexey Dobriyan static inline void xfrm_sysctl_fini(struct net *net)
1520b27aeadbSAlexey Dobriyan {
1521b27aeadbSAlexey Dobriyan }
1522b27aeadbSAlexey Dobriyan #endif
1523b27aeadbSAlexey Dobriyan 
1524d3623099SNicolas Dichtel void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
1525870a2df4SNicolas Dichtel 			  struct xfrm_address_filter *filter);
1526d511337aSJoe Perches int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
15274c563f76STimo Teras 		    int (*func)(struct xfrm_state *, int, void*), void *);
1528283bc9f3SFan Du void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net);
1529d511337aSJoe Perches struct xfrm_state *xfrm_state_alloc(struct net *net);
1530d511337aSJoe Perches struct xfrm_state *xfrm_state_find(const xfrm_address_t *daddr,
153133765d06SDavid S. Miller 				   const xfrm_address_t *saddr,
1532b520e9f6SDavid S. Miller 				   const struct flowi *fl,
1533b520e9f6SDavid S. Miller 				   struct xfrm_tmpl *tmpl,
15341da177e4SLinus Torvalds 				   struct xfrm_policy *pol, int *err,
15351da177e4SLinus Torvalds 				   unsigned short family);
1536d511337aSJoe Perches struct xfrm_state *xfrm_stateonly_find(struct net *net, u32 mark,
15375447c5e4SAlexey Dobriyan 				       xfrm_address_t *daddr,
1538628529b6SJamal Hadi Salim 				       xfrm_address_t *saddr,
1539628529b6SJamal Hadi Salim 				       unsigned short family,
1540628529b6SJamal Hadi Salim 				       u8 mode, u8 proto, u32 reqid);
1541c454997eSFan Du struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1542c454997eSFan Du 					      unsigned short family);
1543d511337aSJoe Perches int xfrm_state_check_expire(struct xfrm_state *x);
1544d511337aSJoe Perches void xfrm_state_insert(struct xfrm_state *x);
1545d511337aSJoe Perches int xfrm_state_add(struct xfrm_state *x);
1546d511337aSJoe Perches int xfrm_state_update(struct xfrm_state *x);
1547d511337aSJoe Perches struct xfrm_state *xfrm_state_lookup(struct net *net, u32 mark,
1548a70486f0SDavid S. Miller 				     const xfrm_address_t *daddr, __be32 spi,
1549bd55775cSJamal Hadi Salim 				     u8 proto, unsigned short family);
1550d511337aSJoe Perches struct xfrm_state *xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1551a70486f0SDavid S. Miller 					    const xfrm_address_t *daddr,
1552a70486f0SDavid S. Miller 					    const xfrm_address_t *saddr,
1553bd55775cSJamal Hadi Salim 					    u8 proto,
1554bd55775cSJamal Hadi Salim 					    unsigned short family);
155541a49cc3SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1556d511337aSJoe Perches int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1557283bc9f3SFan Du 		   unsigned short family, struct net *net);
1558d511337aSJoe Perches int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1559d511337aSJoe Perches 		    unsigned short family);
156041a49cc3SMasahide NAKAMURA #else
156141a49cc3SMasahide NAKAMURA static inline int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src,
1562283bc9f3SFan Du 				 int n, unsigned short family, struct net *net)
156341a49cc3SMasahide NAKAMURA {
156441a49cc3SMasahide NAKAMURA 	return -ENOSYS;
156541a49cc3SMasahide NAKAMURA }
156641a49cc3SMasahide NAKAMURA 
156741a49cc3SMasahide NAKAMURA static inline int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src,
156841a49cc3SMasahide NAKAMURA 				  int n, unsigned short family)
156941a49cc3SMasahide NAKAMURA {
157041a49cc3SMasahide NAKAMURA 	return -ENOSYS;
157141a49cc3SMasahide NAKAMURA }
157241a49cc3SMasahide NAKAMURA #endif
1573af11e316SJamal Hadi Salim 
1574af11e316SJamal Hadi Salim struct xfrmk_sadinfo {
1575af11e316SJamal Hadi Salim 	u32 sadhcnt; /* current hash bkts */
1576af11e316SJamal Hadi Salim 	u32 sadhmcnt; /* max allowed hash bkts */
1577af11e316SJamal Hadi Salim 	u32 sadcnt; /* current running count */
1578af11e316SJamal Hadi Salim };
1579af11e316SJamal Hadi Salim 
15805a6d3416SJamal Hadi Salim struct xfrmk_spdinfo {
15815a6d3416SJamal Hadi Salim 	u32 incnt;
15825a6d3416SJamal Hadi Salim 	u32 outcnt;
15835a6d3416SJamal Hadi Salim 	u32 fwdcnt;
15845a6d3416SJamal Hadi Salim 	u32 inscnt;
15855a6d3416SJamal Hadi Salim 	u32 outscnt;
15865a6d3416SJamal Hadi Salim 	u32 fwdscnt;
15875a6d3416SJamal Hadi Salim 	u32 spdhcnt;
15885a6d3416SJamal Hadi Salim 	u32 spdhmcnt;
15895a6d3416SJamal Hadi Salim };
15905a6d3416SJamal Hadi Salim 
1591d511337aSJoe Perches struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
1592d511337aSJoe Perches int xfrm_state_delete(struct xfrm_state *x);
15932e71029eSTetsuo Handa int xfrm_state_flush(struct net *net, u8 proto, bool task_valid);
1594d77e38e6SSteffen Klassert int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid);
1595d511337aSJoe Perches void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si);
1596d511337aSJoe Perches void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si);
1597d511337aSJoe Perches u32 xfrm_replay_seqhi(struct xfrm_state *x, __be32 net_seq);
1598d511337aSJoe Perches int xfrm_init_replay(struct xfrm_state *x);
1599d511337aSJoe Perches int xfrm_state_mtu(struct xfrm_state *x, int mtu);
1600ffdb5211SIlan Tayari int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload);
1601d511337aSJoe Perches int xfrm_init_state(struct xfrm_state *x);
1602d511337aSJoe Perches int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb);
1603d511337aSJoe Perches int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type);
1604d511337aSJoe Perches int xfrm_input_resume(struct sk_buff *skb, int nexthdr);
1605acf568eeSHerbert Xu int xfrm_trans_queue(struct sk_buff *skb,
1606acf568eeSHerbert Xu 		     int (*finish)(struct net *, struct sock *,
1607acf568eeSHerbert Xu 				   struct sk_buff *));
1608d511337aSJoe Perches int xfrm_output_resume(struct sk_buff *skb, int err);
16097026b1ddSDavid Miller int xfrm_output(struct sock *sk, struct sk_buff *skb);
1610d511337aSJoe Perches int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb);
1611d511337aSJoe Perches void xfrm_local_error(struct sk_buff *skb, int mtu);
1612d511337aSJoe Perches int xfrm4_extract_header(struct sk_buff *skb);
1613d511337aSJoe Perches int xfrm4_extract_input(struct xfrm_state *x, struct sk_buff *skb);
1614d511337aSJoe Perches int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
1615716062fdSHerbert Xu 		    int encap_type);
1616d511337aSJoe Perches int xfrm4_transport_finish(struct sk_buff *skb, int async);
1617d511337aSJoe Perches int xfrm4_rcv(struct sk_buff *skb);
16181e295370SSteffen Klassert int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq);
1619c4541b41SHerbert Xu 
1620c4541b41SHerbert Xu static inline int xfrm4_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi)
1621c4541b41SHerbert Xu {
162270be6c91SSteffen Klassert 	XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
16233328715eSSteffen Klassert 	XFRM_SPI_SKB_CB(skb)->family = AF_INET;
16243328715eSSteffen Klassert 	XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
16253328715eSSteffen Klassert 	return xfrm_input(skb, nexthdr, spi, 0);
1626c4541b41SHerbert Xu }
1627c4541b41SHerbert Xu 
1628d511337aSJoe Perches int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb);
1629d511337aSJoe Perches int xfrm4_prepare_output(struct xfrm_state *x, struct sk_buff *skb);
1630ede2059dSEric W. Biederman int xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb);
16317026b1ddSDavid Miller int xfrm4_output_finish(struct sock *sk, struct sk_buff *skb);
16323328715eSSteffen Klassert int xfrm4_rcv_cb(struct sk_buff *skb, u8 protocol, int err);
16333328715eSSteffen Klassert int xfrm4_protocol_register(struct xfrm4_protocol *handler, unsigned char protocol);
16343328715eSSteffen Klassert int xfrm4_protocol_deregister(struct xfrm4_protocol *handler, unsigned char protocol);
1635d511337aSJoe Perches int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family);
1636d511337aSJoe Perches int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family);
1637d511337aSJoe Perches void xfrm4_local_error(struct sk_buff *skb, u32 mtu);
1638d511337aSJoe Perches int xfrm6_extract_header(struct sk_buff *skb);
1639d511337aSJoe Perches int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb);
164063c43787SNicolas Dichtel int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
164163c43787SNicolas Dichtel 		  struct ip6_tnl *t);
1642d511337aSJoe Perches int xfrm6_transport_finish(struct sk_buff *skb, int async);
164363c43787SNicolas Dichtel int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t);
1644d511337aSJoe Perches int xfrm6_rcv(struct sk_buff *skb);
1645d511337aSJoe Perches int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
1646fbd9a5b4SMasahide NAKAMURA 		     xfrm_address_t *saddr, u8 proto);
16477b77d161SDavid S. Miller void xfrm6_local_error(struct sk_buff *skb, u32 mtu);
16487e14ea15SSteffen Klassert int xfrm6_rcv_cb(struct sk_buff *skb, u8 protocol, int err);
16497e14ea15SSteffen Klassert int xfrm6_protocol_register(struct xfrm6_protocol *handler, unsigned char protocol);
16507e14ea15SSteffen Klassert int xfrm6_protocol_deregister(struct xfrm6_protocol *handler, unsigned char protocol);
1651d511337aSJoe Perches int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family);
16527b77d161SDavid S. Miller int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family);
1653d511337aSJoe Perches __be32 xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr);
1654d511337aSJoe Perches __be32 xfrm6_tunnel_spi_lookup(struct net *net, const xfrm_address_t *saddr);
1655d511337aSJoe Perches int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb);
1656d511337aSJoe Perches int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb);
1657ede2059dSEric W. Biederman int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb);
16587026b1ddSDavid Miller int xfrm6_output_finish(struct sock *sk, struct sk_buff *skb);
1659d511337aSJoe Perches int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb,
1660aee5adb4SMasahide NAKAMURA 			  u8 **prevhdr);
16611da177e4SLinus Torvalds 
16621da177e4SLinus Torvalds #ifdef CONFIG_XFRM
1663d511337aSJoe Perches int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb);
1664d511337aSJoe Perches int xfrm_user_policy(struct sock *sk, int optname,
1665d511337aSJoe Perches 		     u8 __user *optval, int optlen);
16661da177e4SLinus Torvalds #else
16671da177e4SLinus Torvalds static inline int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
16681da177e4SLinus Torvalds {
16691da177e4SLinus Torvalds  	return -ENOPROTOOPT;
16701da177e4SLinus Torvalds }
16711da177e4SLinus Torvalds 
1672067b207bSJames Chapman static inline int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
16731da177e4SLinus Torvalds {
16741da177e4SLinus Torvalds  	/* should not happen */
16751da177e4SLinus Torvalds  	kfree_skb(skb);
16761da177e4SLinus Torvalds 	return 0;
16771da177e4SLinus Torvalds }
16781da177e4SLinus Torvalds #endif
16791da177e4SLinus Torvalds 
1680d77e38e6SSteffen Klassert struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,
1681d77e38e6SSteffen Klassert 				    const xfrm_address_t *saddr,
1682d77e38e6SSteffen Klassert 				    const xfrm_address_t *daddr,
1683077fbac4SLorenzo Colitti 				    int family, u32 mark);
1684d77e38e6SSteffen Klassert 
16850331b1f3SAlexey Dobriyan struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp);
16864c563f76STimo Teras 
1687d511337aSJoe Perches void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type);
1688d511337aSJoe Perches int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
1689d511337aSJoe Perches 		     int (*func)(struct xfrm_policy *, int, int, void*),
1690d511337aSJoe Perches 		     void *);
1691283bc9f3SFan Du void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net);
16921da177e4SLinus Torvalds int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl);
16938ca2e93bSJamal Hadi Salim struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark,
16948ca2e93bSJamal Hadi Salim 					  u8 type, int dir,
16954e81bb83SMasahide NAKAMURA 					  struct xfrm_selector *sel,
1696ef41aaa0SEric Paris 					  struct xfrm_sec_ctx *ctx, int delete,
1697ef41aaa0SEric Paris 					  int *err);
1698d511337aSJoe Perches struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8, int dir,
1699d511337aSJoe Perches 				     u32 id, int delete, int *err);
17002e71029eSTetsuo Handa int xfrm_policy_flush(struct net *net, u8 type, bool task_valid);
1701880a6fabSChristophe Gouault void xfrm_policy_hash_rebuild(struct net *net);
17021da177e4SLinus Torvalds u32 xfrm_get_acqseq(void);
1703776e9dd9SFan Du int verify_spi_info(u8 proto, u32 min, u32 max);
1704d511337aSJoe Perches int xfrm_alloc_spi(struct xfrm_state *x, u32 minspi, u32 maxspi);
1705e473fcb4SMathias Krause struct xfrm_state *xfrm_find_acq(struct net *net, const struct xfrm_mark *mark,
1706bd55775cSJamal Hadi Salim 				 u8 mode, u32 reqid, u8 proto,
1707a70486f0SDavid S. Miller 				 const xfrm_address_t *daddr,
1708a70486f0SDavid S. Miller 				 const xfrm_address_t *saddr, int create,
1709bd55775cSJamal Hadi Salim 				 unsigned short family);
1710d511337aSJoe Perches int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol);
17111da177e4SLinus Torvalds 
171280c9abaaSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
1713d511337aSJoe Perches int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1714183cad12SDavid S. Miller 	       const struct xfrm_migrate *m, int num_bundles,
17158bafd730SAntony Antony 	       const struct xfrm_kmaddress *k,
17168bafd730SAntony Antony 	       const struct xfrm_encap_tmpl *encap);
1717283bc9f3SFan Du struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net);
1718d511337aSJoe Perches struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
17194ab47d47SAntony Antony 				      struct xfrm_migrate *m,
17204ab47d47SAntony Antony 				      struct xfrm_encap_tmpl *encap);
1721d511337aSJoe Perches int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
172213c1d189SArnaud Ebalard 		 struct xfrm_migrate *m, int num_bundles,
17234ab47d47SAntony Antony 		 struct xfrm_kmaddress *k, struct net *net,
17244ab47d47SAntony Antony 		 struct xfrm_encap_tmpl *encap);
172580c9abaaSShinta Sugimoto #endif
172680c9abaaSShinta Sugimoto 
1727d511337aSJoe Perches int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport);
1728d511337aSJoe Perches void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid);
1729d511337aSJoe Perches int km_report(struct net *net, u8 proto, struct xfrm_selector *sel,
1730d511337aSJoe Perches 	      xfrm_address_t *addr);
17311da177e4SLinus Torvalds 
1732d511337aSJoe Perches void xfrm_input_init(void);
1733d511337aSJoe Perches int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq);
17341da177e4SLinus Torvalds 
1735d511337aSJoe Perches void xfrm_probe_algs(void);
1736d511337aSJoe Perches int xfrm_count_pfkey_auth_supported(void);
1737d511337aSJoe Perches int xfrm_count_pfkey_enc_supported(void);
1738d511337aSJoe Perches struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx);
1739d511337aSJoe Perches struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx);
1740d511337aSJoe Perches struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id);
1741d511337aSJoe Perches struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id);
1742d511337aSJoe Perches struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id);
1743d511337aSJoe Perches struct xfrm_algo_desc *xfrm_aalg_get_byname(const char *name, int probe);
1744d511337aSJoe Perches struct xfrm_algo_desc *xfrm_ealg_get_byname(const char *name, int probe);
1745d511337aSJoe Perches struct xfrm_algo_desc *xfrm_calg_get_byname(const char *name, int probe);
1746d511337aSJoe Perches struct xfrm_algo_desc *xfrm_aead_get_byname(const char *name, int icv_len,
17471a6509d9SHerbert Xu 					    int probe);
17481da177e4SLinus Torvalds 
1749ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 static inline bool xfrm6_addr_equal(const xfrm_address_t *a,
1750ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 				    const xfrm_address_t *b)
1751ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 {
1752ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 	return ipv6_addr_equal((const struct in6_addr *)a,
1753ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 			       (const struct in6_addr *)b);
1754ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 }
1755ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 
175670e94e66SYOSHIFUJI Hideaki / 吉藤英明 static inline bool xfrm_addr_equal(const xfrm_address_t *a,
175770e94e66SYOSHIFUJI Hideaki / 吉藤英明 				   const xfrm_address_t *b,
175870e94e66SYOSHIFUJI Hideaki / 吉藤英明 				   sa_family_t family)
175970e94e66SYOSHIFUJI Hideaki / 吉藤英明 {
176070e94e66SYOSHIFUJI Hideaki / 吉藤英明 	switch (family) {
176170e94e66SYOSHIFUJI Hideaki / 吉藤英明 	default:
176270e94e66SYOSHIFUJI Hideaki / 吉藤英明 	case AF_INET:
176370e94e66SYOSHIFUJI Hideaki / 吉藤英明 		return ((__force u32)a->a4 ^ (__force u32)b->a4) == 0;
176470e94e66SYOSHIFUJI Hideaki / 吉藤英明 	case AF_INET6:
176570e94e66SYOSHIFUJI Hideaki / 吉藤英明 		return xfrm6_addr_equal(a, b);
176670e94e66SYOSHIFUJI Hideaki / 吉藤英明 	}
176770e94e66SYOSHIFUJI Hideaki / 吉藤英明 }
176870e94e66SYOSHIFUJI Hideaki / 吉藤英明 
176977d8d7a6SHerbert Xu static inline int xfrm_policy_id2dir(u32 index)
177077d8d7a6SHerbert Xu {
177177d8d7a6SHerbert Xu 	return index & 7;
177277d8d7a6SHerbert Xu }
177377d8d7a6SHerbert Xu 
1774a6483b79SAlexey Dobriyan #ifdef CONFIG_XFRM
1775a6483b79SAlexey Dobriyan static inline int xfrm_aevent_is_on(struct net *net)
1776f8cd5488SJamal Hadi Salim {
1777be33690dSPatrick McHardy 	struct sock *nlsk;
1778be33690dSPatrick McHardy 	int ret = 0;
1779be33690dSPatrick McHardy 
1780be33690dSPatrick McHardy 	rcu_read_lock();
1781a6483b79SAlexey Dobriyan 	nlsk = rcu_dereference(net->xfrm.nlsk);
1782be33690dSPatrick McHardy 	if (nlsk)
1783be33690dSPatrick McHardy 		ret = netlink_has_listeners(nlsk, XFRMNLGRP_AEVENTS);
1784be33690dSPatrick McHardy 	rcu_read_unlock();
1785be33690dSPatrick McHardy 	return ret;
1786f8cd5488SJamal Hadi Salim }
17870f24558eSHoria Geanta 
17880f24558eSHoria Geanta static inline int xfrm_acquire_is_on(struct net *net)
17890f24558eSHoria Geanta {
17900f24558eSHoria Geanta 	struct sock *nlsk;
17910f24558eSHoria Geanta 	int ret = 0;
17920f24558eSHoria Geanta 
17930f24558eSHoria Geanta 	rcu_read_lock();
17940f24558eSHoria Geanta 	nlsk = rcu_dereference(net->xfrm.nlsk);
17950f24558eSHoria Geanta 	if (nlsk)
17960f24558eSHoria Geanta 		ret = netlink_has_listeners(nlsk, XFRMNLGRP_ACQUIRE);
17970f24558eSHoria Geanta 	rcu_read_unlock();
17980f24558eSHoria Geanta 
17990f24558eSHoria Geanta 	return ret;
18000f24558eSHoria Geanta }
1801a6483b79SAlexey Dobriyan #endif
1802f8cd5488SJamal Hadi Salim 
1803373b8eebSAlexey Dobriyan static inline unsigned int aead_len(struct xfrm_algo_aead *alg)
1804ee5c2317SSteffen Klassert {
1805ee5c2317SSteffen Klassert 	return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
1806ee5c2317SSteffen Klassert }
1807ee5c2317SSteffen Klassert 
180806cd22f8SAlexey Dobriyan static inline unsigned int xfrm_alg_len(const struct xfrm_algo *alg)
18090f99be0dSEric Dumazet {
18100f99be0dSEric Dumazet 	return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
18110f99be0dSEric Dumazet }
18120f99be0dSEric Dumazet 
18131bd963a7SAlexey Dobriyan static inline unsigned int xfrm_alg_auth_len(const struct xfrm_algo_auth *alg)
18144447bb33SMartin Willi {
18154447bb33SMartin Willi 	return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
18164447bb33SMartin Willi }
18174447bb33SMartin Willi 
18185e708e47SAlexey Dobriyan static inline unsigned int xfrm_replay_state_esn_len(struct xfrm_replay_state_esn *replay_esn)
18199736acf3SSteffen Klassert {
18209736acf3SSteffen Klassert 	return sizeof(*replay_esn) + replay_esn->bmp_len * sizeof(__u32);
18219736acf3SSteffen Klassert }
18229736acf3SSteffen Klassert 
182380c9abaaSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
1824af2f464eSSteffen Klassert static inline int xfrm_replay_clone(struct xfrm_state *x,
1825af2f464eSSteffen Klassert 				     struct xfrm_state *orig)
1826af2f464eSSteffen Klassert {
1827af2f464eSSteffen Klassert 	x->replay_esn = kzalloc(xfrm_replay_state_esn_len(orig->replay_esn),
1828af2f464eSSteffen Klassert 				GFP_KERNEL);
1829af2f464eSSteffen Klassert 	if (!x->replay_esn)
1830af2f464eSSteffen Klassert 		return -ENOMEM;
1831af2f464eSSteffen Klassert 
1832af2f464eSSteffen Klassert 	x->replay_esn->bmp_len = orig->replay_esn->bmp_len;
1833af2f464eSSteffen Klassert 	x->replay_esn->replay_window = orig->replay_esn->replay_window;
1834af2f464eSSteffen Klassert 
1835af2f464eSSteffen Klassert 	x->preplay_esn = kmemdup(x->replay_esn,
1836af2f464eSSteffen Klassert 				 xfrm_replay_state_esn_len(x->replay_esn),
1837af2f464eSSteffen Klassert 				 GFP_KERNEL);
1838af2f464eSSteffen Klassert 	if (!x->preplay_esn) {
1839af2f464eSSteffen Klassert 		kfree(x->replay_esn);
1840af2f464eSSteffen Klassert 		return -ENOMEM;
1841af2f464eSSteffen Klassert 	}
1842af2f464eSSteffen Klassert 
1843af2f464eSSteffen Klassert 	return 0;
1844af2f464eSSteffen Klassert }
1845af2f464eSSteffen Klassert 
1846ee5c2317SSteffen Klassert static inline struct xfrm_algo_aead *xfrm_algo_aead_clone(struct xfrm_algo_aead *orig)
1847ee5c2317SSteffen Klassert {
1848ee5c2317SSteffen Klassert 	return kmemdup(orig, aead_len(orig), GFP_KERNEL);
1849ee5c2317SSteffen Klassert }
1850ee5c2317SSteffen Klassert 
1851ee5c2317SSteffen Klassert 
185280c9abaaSShinta Sugimoto static inline struct xfrm_algo *xfrm_algo_clone(struct xfrm_algo *orig)
185380c9abaaSShinta Sugimoto {
18540f99be0dSEric Dumazet 	return kmemdup(orig, xfrm_alg_len(orig), GFP_KERNEL);
185580c9abaaSShinta Sugimoto }
185680c9abaaSShinta Sugimoto 
18574447bb33SMartin Willi static inline struct xfrm_algo_auth *xfrm_algo_auth_clone(struct xfrm_algo_auth *orig)
18584447bb33SMartin Willi {
18594447bb33SMartin Willi 	return kmemdup(orig, xfrm_alg_auth_len(orig), GFP_KERNEL);
18604447bb33SMartin Willi }
18614447bb33SMartin Willi 
186280c9abaaSShinta Sugimoto static inline void xfrm_states_put(struct xfrm_state **states, int n)
186380c9abaaSShinta Sugimoto {
186480c9abaaSShinta Sugimoto 	int i;
186580c9abaaSShinta Sugimoto 	for (i = 0; i < n; i++)
186680c9abaaSShinta Sugimoto 		xfrm_state_put(*(states + i));
186780c9abaaSShinta Sugimoto }
186880c9abaaSShinta Sugimoto 
186980c9abaaSShinta Sugimoto static inline void xfrm_states_delete(struct xfrm_state **states, int n)
187080c9abaaSShinta Sugimoto {
187180c9abaaSShinta Sugimoto 	int i;
187280c9abaaSShinta Sugimoto 	for (i = 0; i < n; i++)
187380c9abaaSShinta Sugimoto 		xfrm_state_delete(*(states + i));
187480c9abaaSShinta Sugimoto }
187580c9abaaSShinta Sugimoto #endif
1876f8cd5488SJamal Hadi Salim 
1877def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
187800501121SHerbert Xu static inline struct xfrm_state *xfrm_input_state(struct sk_buff *skb)
187900501121SHerbert Xu {
188000501121SHerbert Xu 	return skb->sp->xvec[skb->sp->len - 1];
188100501121SHerbert Xu }
1882f53c7239SSteffen Klassert #endif
1883f53c7239SSteffen Klassert 
188454ef207aSSteffen Klassert static inline struct xfrm_offload *xfrm_offload(struct sk_buff *skb)
188554ef207aSSteffen Klassert {
1886f53c7239SSteffen Klassert #ifdef CONFIG_XFRM
188754ef207aSSteffen Klassert 	struct sec_path *sp = skb->sp;
188854ef207aSSteffen Klassert 
188954ef207aSSteffen Klassert 	if (!sp || !sp->olen || sp->len != sp->olen)
189054ef207aSSteffen Klassert 		return NULL;
189154ef207aSSteffen Klassert 
189254ef207aSSteffen Klassert 	return &sp->ovec[sp->olen - 1];
1893f53c7239SSteffen Klassert #else
1894f53c7239SSteffen Klassert 	return NULL;
1895def8b4faSAlexey Dobriyan #endif
1896f53c7239SSteffen Klassert }
189700501121SHerbert Xu 
1898e9a441b6SKirill Tkhai void __init xfrm_dev_init(void);
1899b81f884aSHangbin Liu 
1900b81f884aSHangbin Liu #ifdef CONFIG_XFRM_OFFLOAD
1901f53c7239SSteffen Klassert void xfrm_dev_resume(struct sk_buff *skb);
1902f53c7239SSteffen Klassert void xfrm_dev_backlog(struct softnet_data *sd);
1903f53c7239SSteffen Klassert struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again);
1904d77e38e6SSteffen Klassert int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
1905d77e38e6SSteffen Klassert 		       struct xfrm_user_offload *xuo);
1906d77e38e6SSteffen Klassert bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x);
1907d77e38e6SSteffen Klassert 
190850bd870aSYossef Efraim static inline void xfrm_dev_state_advance_esn(struct xfrm_state *x)
190950bd870aSYossef Efraim {
191050bd870aSYossef Efraim 	struct xfrm_state_offload *xso = &x->xso;
191150bd870aSYossef Efraim 
191250bd870aSYossef Efraim 	if (xso->dev && xso->dev->xfrmdev_ops->xdo_dev_state_advance_esn)
191350bd870aSYossef Efraim 		xso->dev->xfrmdev_ops->xdo_dev_state_advance_esn(x);
191450bd870aSYossef Efraim }
191550bd870aSYossef Efraim 
1916f70f250aSSteffen Klassert static inline bool xfrm_dst_offload_ok(struct dst_entry *dst)
1917f70f250aSSteffen Klassert {
1918f70f250aSSteffen Klassert 	struct xfrm_state *x = dst->xfrm;
1919b6ca8bd5SDavid Miller 	struct xfrm_dst *xdst;
1920f70f250aSSteffen Klassert 
1921f70f250aSSteffen Klassert 	if (!x || !x->type_offload)
1922f70f250aSSteffen Klassert 		return false;
1923f70f250aSSteffen Klassert 
1924b6ca8bd5SDavid Miller 	xdst = (struct xfrm_dst *) dst;
19252271d519SSteffen Klassert 	if (!x->xso.offload_handle && !xdst->child->xfrm)
19262271d519SSteffen Klassert 		return true;
19270f6c480fSDavid Miller 	if (x->xso.offload_handle && (x->xso.dev == xfrm_dst_path(dst)->dev) &&
1928b6ca8bd5SDavid Miller 	    !xdst->child->xfrm)
1929f70f250aSSteffen Klassert 		return true;
1930f70f250aSSteffen Klassert 
1931f70f250aSSteffen Klassert 	return false;
1932f70f250aSSteffen Klassert }
1933f70f250aSSteffen Klassert 
1934d77e38e6SSteffen Klassert static inline void xfrm_dev_state_delete(struct xfrm_state *x)
1935d77e38e6SSteffen Klassert {
1936d77e38e6SSteffen Klassert 	struct xfrm_state_offload *xso = &x->xso;
1937d77e38e6SSteffen Klassert 
1938d77e38e6SSteffen Klassert 	if (xso->dev)
1939d77e38e6SSteffen Klassert 		xso->dev->xfrmdev_ops->xdo_dev_state_delete(x);
1940d77e38e6SSteffen Klassert }
1941d77e38e6SSteffen Klassert 
1942d77e38e6SSteffen Klassert static inline void xfrm_dev_state_free(struct xfrm_state *x)
1943d77e38e6SSteffen Klassert {
1944d77e38e6SSteffen Klassert 	struct xfrm_state_offload *xso = &x->xso;
1945d77e38e6SSteffen Klassert 	 struct net_device *dev = xso->dev;
1946d77e38e6SSteffen Klassert 
1947d77e38e6SSteffen Klassert 	if (dev && dev->xfrmdev_ops) {
19487f05b467SShannon Nelson 		if (dev->xfrmdev_ops->xdo_dev_state_free)
1949d77e38e6SSteffen Klassert 			dev->xfrmdev_ops->xdo_dev_state_free(x);
1950d77e38e6SSteffen Klassert 		xso->dev = NULL;
1951d77e38e6SSteffen Klassert 		dev_put(dev);
1952d77e38e6SSteffen Klassert 	}
1953d77e38e6SSteffen Klassert }
1954d77e38e6SSteffen Klassert #else
1955f53c7239SSteffen Klassert static inline void xfrm_dev_resume(struct sk_buff *skb)
1956f53c7239SSteffen Klassert {
1957f53c7239SSteffen Klassert }
1958f53c7239SSteffen Klassert 
1959f53c7239SSteffen Klassert static inline void xfrm_dev_backlog(struct softnet_data *sd)
1960f53c7239SSteffen Klassert {
1961f53c7239SSteffen Klassert }
1962f53c7239SSteffen Klassert 
1963f53c7239SSteffen Klassert static inline struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again)
1964f6e27114SSteffen Klassert {
19653dca3f38SSteffen Klassert 	return skb;
1966f6e27114SSteffen Klassert }
1967f6e27114SSteffen Klassert 
1968d77e38e6SSteffen Klassert static inline int xfrm_dev_state_add(struct net *net, struct xfrm_state *x, struct xfrm_user_offload *xuo)
1969d77e38e6SSteffen Klassert {
1970d77e38e6SSteffen Klassert 	return 0;
1971d77e38e6SSteffen Klassert }
1972d77e38e6SSteffen Klassert 
1973d77e38e6SSteffen Klassert static inline void xfrm_dev_state_delete(struct xfrm_state *x)
1974d77e38e6SSteffen Klassert {
1975d77e38e6SSteffen Klassert }
1976d77e38e6SSteffen Klassert 
1977d77e38e6SSteffen Klassert static inline void xfrm_dev_state_free(struct xfrm_state *x)
1978d77e38e6SSteffen Klassert {
1979d77e38e6SSteffen Klassert }
1980d77e38e6SSteffen Klassert 
1981d77e38e6SSteffen Klassert static inline bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
1982d77e38e6SSteffen Klassert {
1983d77e38e6SSteffen Klassert 	return false;
1984d77e38e6SSteffen Klassert }
1985f70f250aSSteffen Klassert 
198650bd870aSYossef Efraim static inline void xfrm_dev_state_advance_esn(struct xfrm_state *x)
198750bd870aSYossef Efraim {
198850bd870aSYossef Efraim }
198950bd870aSYossef Efraim 
1990f70f250aSSteffen Klassert static inline bool xfrm_dst_offload_ok(struct dst_entry *dst)
1991f70f250aSSteffen Klassert {
1992f70f250aSSteffen Klassert 	return false;
1993f70f250aSSteffen Klassert }
1994d77e38e6SSteffen Klassert #endif
1995d77e38e6SSteffen Klassert 
1996bf825f81SJamal Hadi Salim static inline int xfrm_mark_get(struct nlattr **attrs, struct xfrm_mark *m)
1997bf825f81SJamal Hadi Salim {
1998bf825f81SJamal Hadi Salim 	if (attrs[XFRMA_MARK])
19994efd7e83SAndreas Steffen 		memcpy(m, nla_data(attrs[XFRMA_MARK]), sizeof(struct xfrm_mark));
2000bf825f81SJamal Hadi Salim 	else
2001bf825f81SJamal Hadi Salim 		m->v = m->m = 0;
2002bf825f81SJamal Hadi Salim 
2003bf825f81SJamal Hadi Salim 	return m->v & m->m;
2004bf825f81SJamal Hadi Salim }
2005bf825f81SJamal Hadi Salim 
2006e3dfa389SDavid S. Miller static inline int xfrm_mark_put(struct sk_buff *skb, const struct xfrm_mark *m)
2007bf825f81SJamal Hadi Salim {
20081d1e34ddSDavid S. Miller 	int ret = 0;
2009bf825f81SJamal Hadi Salim 
20101d1e34ddSDavid S. Miller 	if (m->m | m->v)
20111d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_MARK, sizeof(struct xfrm_mark), m);
20121d1e34ddSDavid S. Miller 	return ret;
2013bf825f81SJamal Hadi Salim }
2014bf825f81SJamal Hadi Salim 
201570be6c91SSteffen Klassert static inline int xfrm_tunnel_check(struct sk_buff *skb, struct xfrm_state *x,
201670be6c91SSteffen Klassert 				    unsigned int family)
201770be6c91SSteffen Klassert {
201870be6c91SSteffen Klassert 	bool tunnel = false;
201970be6c91SSteffen Klassert 
202070be6c91SSteffen Klassert 	switch(family) {
202170be6c91SSteffen Klassert 	case AF_INET:
202270be6c91SSteffen Klassert 		if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
202370be6c91SSteffen Klassert 			tunnel = true;
202470be6c91SSteffen Klassert 		break;
202570be6c91SSteffen Klassert 	case AF_INET6:
202670be6c91SSteffen Klassert 		if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
202770be6c91SSteffen Klassert 			tunnel = true;
202870be6c91SSteffen Klassert 		break;
202970be6c91SSteffen Klassert 	}
203070be6c91SSteffen Klassert 	if (tunnel && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL))
203170be6c91SSteffen Klassert 		return -EINVAL;
203270be6c91SSteffen Klassert 
203370be6c91SSteffen Klassert 	return 0;
203470be6c91SSteffen Klassert }
20351da177e4SLinus Torvalds #endif	/* _NET_XFRM_H */
2036