xref: /openbmc/linux/include/net/xfrm.h (revision 3dca3f38)
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 
378d511337aSJoe Perches void xfrm_state_delete_tunnel(struct xfrm_state *x);
3791da177e4SLinus Torvalds 
380fd2c3ef7SEric Dumazet struct xfrm_type {
3811da177e4SLinus Torvalds 	char			*description;
3821da177e4SLinus Torvalds 	struct module		*owner;
383a6337463Sjamal 	u8			proto;
384a6337463Sjamal 	u8			flags;
3851b5c2299SMasahide NAKAMURA #define XFRM_TYPE_NON_FRAGMENT	1
386436a0a40SHerbert Xu #define XFRM_TYPE_REPLAY_PROT	2
387f04e7e8dSHerbert Xu #define XFRM_TYPE_LOCAL_COADDR	4
388f04e7e8dSHerbert Xu #define XFRM_TYPE_REMOTE_COADDR	8
3891da177e4SLinus Torvalds 
39072cb6962SHerbert Xu 	int			(*init_state)(struct xfrm_state *x);
3911da177e4SLinus Torvalds 	void			(*destructor)(struct xfrm_state *);
392e695633eSHerbert Xu 	int			(*input)(struct xfrm_state *, struct sk_buff *skb);
3931da177e4SLinus Torvalds 	int			(*output)(struct xfrm_state *, struct sk_buff *pskb);
3948f029de2SDavid S. Miller 	int			(*reject)(struct xfrm_state *, struct sk_buff *,
3958f029de2SDavid S. Miller 					  const struct flowi *);
396aee5adb4SMasahide NAKAMURA 	int			(*hdr_offset)(struct xfrm_state *, struct sk_buff *, u8 **);
3971da177e4SLinus Torvalds 	/* Estimate maximal size of result of transformation of a dgram */
398c5c25238SPatrick McHardy 	u32			(*get_mtu)(struct xfrm_state *, int size);
3991da177e4SLinus Torvalds };
4001da177e4SLinus Torvalds 
401d511337aSJoe Perches int xfrm_register_type(const struct xfrm_type *type, unsigned short family);
402d511337aSJoe Perches int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family);
4031da177e4SLinus Torvalds 
4049d389d7fSSteffen Klassert struct xfrm_type_offload {
4059d389d7fSSteffen Klassert 	char		*description;
4069d389d7fSSteffen Klassert 	struct module	*owner;
4079d389d7fSSteffen Klassert 	u8		proto;
4089d389d7fSSteffen Klassert 	void		(*encap)(struct xfrm_state *, struct sk_buff *pskb);
4099d389d7fSSteffen Klassert 	int		(*input_tail)(struct xfrm_state *x, struct sk_buff *skb);
4109d389d7fSSteffen Klassert 	int		(*xmit)(struct xfrm_state *, struct sk_buff *pskb, netdev_features_t features);
4119d389d7fSSteffen Klassert };
4129d389d7fSSteffen Klassert 
4139d389d7fSSteffen Klassert int xfrm_register_type_offload(const struct xfrm_type_offload *type, unsigned short family);
4149d389d7fSSteffen Klassert int xfrm_unregister_type_offload(const struct xfrm_type_offload *type, unsigned short family);
4159d389d7fSSteffen Klassert 
416b59f45d0SHerbert Xu struct xfrm_mode {
417227620e2SHerbert Xu 	/*
418227620e2SHerbert Xu 	 * Remove encapsulation header.
419227620e2SHerbert Xu 	 *
420227620e2SHerbert Xu 	 * The IP header will be moved over the top of the encapsulation
421227620e2SHerbert Xu 	 * header.
422227620e2SHerbert Xu 	 *
423227620e2SHerbert Xu 	 * On entry, the transport header shall point to where the IP header
424227620e2SHerbert Xu 	 * should be and the network header shall be set to where the IP
425227620e2SHerbert Xu 	 * header currently is.  skb->data shall point to the start of the
426227620e2SHerbert Xu 	 * payload.
427227620e2SHerbert Xu 	 */
428227620e2SHerbert Xu 	int (*input2)(struct xfrm_state *x, struct sk_buff *skb);
429227620e2SHerbert Xu 
430227620e2SHerbert Xu 	/*
431227620e2SHerbert Xu 	 * This is the actual input entry point.
432227620e2SHerbert Xu 	 *
433227620e2SHerbert Xu 	 * For transport mode and equivalent this would be identical to
434227620e2SHerbert Xu 	 * input2 (which does not need to be set).  While tunnel mode
435227620e2SHerbert Xu 	 * and equivalent would set this to the tunnel encapsulation function
436227620e2SHerbert Xu 	 * xfrm4_prepare_input that would in turn call input2.
437227620e2SHerbert Xu 	 */
438b59f45d0SHerbert Xu 	int (*input)(struct xfrm_state *x, struct sk_buff *skb);
43937fedd3aSHerbert Xu 
44037fedd3aSHerbert Xu 	/*
44137fedd3aSHerbert Xu 	 * Add encapsulation header.
44237fedd3aSHerbert Xu 	 *
44337fedd3aSHerbert Xu 	 * On exit, the transport header will be set to the start of the
44437fedd3aSHerbert Xu 	 * encapsulation header to be filled in by x->type->output and
44537fedd3aSHerbert Xu 	 * the mac header will be set to the nextheader (protocol for
44637fedd3aSHerbert Xu 	 * IPv4) field of the extension header directly preceding the
44737fedd3aSHerbert Xu 	 * encapsulation header, or in its absence, that of the top IP
44837fedd3aSHerbert Xu 	 * header.  The value of the network header will always point
44937fedd3aSHerbert Xu 	 * to the top IP header while skb->data will point to the payload.
45037fedd3aSHerbert Xu 	 */
45136cf9acfSHerbert Xu 	int (*output2)(struct xfrm_state *x,struct sk_buff *skb);
45236cf9acfSHerbert Xu 
45336cf9acfSHerbert Xu 	/*
45436cf9acfSHerbert Xu 	 * This is the actual output entry point.
45536cf9acfSHerbert Xu 	 *
45636cf9acfSHerbert Xu 	 * For transport mode and equivalent this would be identical to
45736cf9acfSHerbert Xu 	 * output2 (which does not need to be set).  While tunnel mode
45836cf9acfSHerbert Xu 	 * and equivalent would set this to a tunnel encapsulation function
45936cf9acfSHerbert Xu 	 * (xfrm4_prepare_output or xfrm6_prepare_output) that would in turn
46036cf9acfSHerbert Xu 	 * call output2.
46136cf9acfSHerbert Xu 	 */
462eb878e84SJamal Hadi Salim 	int (*output)(struct xfrm_state *x, struct sk_buff *skb);
463b59f45d0SHerbert Xu 
464c35fe410SSteffen Klassert 	/*
465c35fe410SSteffen Klassert 	 * Adjust pointers into the packet and do GSO segmentation.
466c35fe410SSteffen Klassert 	 */
467c35fe410SSteffen Klassert 	struct sk_buff *(*gso_segment)(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features);
468c35fe410SSteffen Klassert 
469c35fe410SSteffen Klassert 	/*
470c35fe410SSteffen Klassert 	 * Adjust pointers into the packet when IPsec is done at layer2.
471c35fe410SSteffen Klassert 	 */
472c35fe410SSteffen Klassert 	void (*xmit)(struct xfrm_state *x, struct sk_buff *skb);
473c35fe410SSteffen Klassert 
47417c2a42aSHerbert Xu 	struct xfrm_state_afinfo *afinfo;
475b59f45d0SHerbert Xu 	struct module *owner;
476b59f45d0SHerbert Xu 	unsigned int encap;
4771bfcb10fSHerbert Xu 	int flags;
4781bfcb10fSHerbert Xu };
4791bfcb10fSHerbert Xu 
4801bfcb10fSHerbert Xu /* Flags for xfrm_mode. */
4811bfcb10fSHerbert Xu enum {
4821bfcb10fSHerbert Xu 	XFRM_MODE_FLAG_TUNNEL = 1,
483b59f45d0SHerbert Xu };
484b59f45d0SHerbert Xu 
485d511337aSJoe Perches int xfrm_register_mode(struct xfrm_mode *mode, int family);
486d511337aSJoe Perches int xfrm_unregister_mode(struct xfrm_mode *mode, int family);
487b59f45d0SHerbert Xu 
488df9dcb45SKazunori MIYAZAWA static inline int xfrm_af2proto(unsigned int family)
489df9dcb45SKazunori MIYAZAWA {
490df9dcb45SKazunori MIYAZAWA 	switch(family) {
491df9dcb45SKazunori MIYAZAWA 	case AF_INET:
492df9dcb45SKazunori MIYAZAWA 		return IPPROTO_IPIP;
493df9dcb45SKazunori MIYAZAWA 	case AF_INET6:
494df9dcb45SKazunori MIYAZAWA 		return IPPROTO_IPV6;
495df9dcb45SKazunori MIYAZAWA 	default:
496df9dcb45SKazunori MIYAZAWA 		return 0;
497df9dcb45SKazunori MIYAZAWA 	}
498df9dcb45SKazunori MIYAZAWA }
499df9dcb45SKazunori MIYAZAWA 
500df9dcb45SKazunori MIYAZAWA static inline struct xfrm_mode *xfrm_ip2inner_mode(struct xfrm_state *x, int ipproto)
501df9dcb45SKazunori MIYAZAWA {
502df9dcb45SKazunori MIYAZAWA 	if ((ipproto == IPPROTO_IPIP && x->props.family == AF_INET) ||
503df9dcb45SKazunori MIYAZAWA 	    (ipproto == IPPROTO_IPV6 && x->props.family == AF_INET6))
504df9dcb45SKazunori MIYAZAWA 		return x->inner_mode;
505df9dcb45SKazunori MIYAZAWA 	else
506df9dcb45SKazunori MIYAZAWA 		return x->inner_mode_iaf;
507df9dcb45SKazunori MIYAZAWA }
508df9dcb45SKazunori MIYAZAWA 
509fd2c3ef7SEric Dumazet struct xfrm_tmpl {
5101da177e4SLinus Torvalds /* id in template is interpreted as:
5111da177e4SLinus Torvalds  * daddr - destination of tunnel, may be zero for transport mode.
5121da177e4SLinus Torvalds  * spi   - zero to acquire spi. Not zero if spi is static, then
5131da177e4SLinus Torvalds  *	   daddr must be fixed too.
5141da177e4SLinus Torvalds  * proto - AH/ESP/IPCOMP
5151da177e4SLinus Torvalds  */
5161da177e4SLinus Torvalds 	struct xfrm_id		id;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds /* Source address of tunnel. Ignored, if it is not a tunnel. */
5191da177e4SLinus Torvalds 	xfrm_address_t		saddr;
5201da177e4SLinus Torvalds 
52176b3f055SMiika Komu 	unsigned short		encap_family;
52276b3f055SMiika Komu 
523a6337463Sjamal 	u32			reqid;
5241da177e4SLinus Torvalds 
5257e49e6deSMasahide NAKAMURA /* Mode: transport, tunnel etc. */
526a6337463Sjamal 	u8			mode;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds /* Sharing mode: unique, this session only, this user only etc. */
529a6337463Sjamal 	u8			share;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds /* May skip this transfomration if no SA is found */
532a6337463Sjamal 	u8			optional;
5331da177e4SLinus Torvalds 
534c5d18e98SHerbert Xu /* Skip aalgos/ealgos/calgos checks. */
535a6337463Sjamal 	u8			allalgs;
536c5d18e98SHerbert Xu 
5371da177e4SLinus Torvalds /* Bit mask of algos allowed for acquisition */
538a6337463Sjamal 	u32			aalgos;
539a6337463Sjamal 	u32			ealgos;
540a6337463Sjamal 	u32			calgos;
5411da177e4SLinus Torvalds };
5421da177e4SLinus Torvalds 
543622dc828SMasahide NAKAMURA #define XFRM_MAX_DEPTH		6
54454ef207aSSteffen Klassert #define XFRM_MAX_OFFLOAD_DEPTH	1
5451da177e4SLinus Torvalds 
54612a169e7SHerbert Xu struct xfrm_policy_walk_entry {
54712a169e7SHerbert Xu 	struct list_head	all;
54812a169e7SHerbert Xu 	u8			dead;
54912a169e7SHerbert Xu };
55012a169e7SHerbert Xu 
55112a169e7SHerbert Xu struct xfrm_policy_walk {
55212a169e7SHerbert Xu 	struct xfrm_policy_walk_entry walk;
55312a169e7SHerbert Xu 	u8 type;
55412a169e7SHerbert Xu 	u32 seq;
55512a169e7SHerbert Xu };
55612a169e7SHerbert Xu 
557a0073fe1SSteffen Klassert struct xfrm_policy_queue {
558a0073fe1SSteffen Klassert 	struct sk_buff_head	hold_queue;
559a0073fe1SSteffen Klassert 	struct timer_list	hold_timer;
560a0073fe1SSteffen Klassert 	unsigned long		timeout;
561a0073fe1SSteffen Klassert };
562a0073fe1SSteffen Klassert 
563fd2c3ef7SEric Dumazet struct xfrm_policy {
5640c5c9fb5SEric W. Biederman 	possible_net_t		xp_net;
5652518c7c2SDavid S. Miller 	struct hlist_node	bydst;
5662518c7c2SDavid S. Miller 	struct hlist_node	byidx;
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	/* This lock only affects elements except for entry. */
5691da177e4SLinus Torvalds 	rwlock_t		lock;
570850a6212SReshetova, Elena 	refcount_t		refcnt;
5711da177e4SLinus Torvalds 	struct timer_list	timer;
5721da177e4SLinus Torvalds 
57380c802f3STimo Teräs 	atomic_t		genid;
5741da177e4SLinus Torvalds 	u32			priority;
5751da177e4SLinus Torvalds 	u32			index;
576bf825f81SJamal Hadi Salim 	struct xfrm_mark	mark;
5771da177e4SLinus Torvalds 	struct xfrm_selector	selector;
5781da177e4SLinus Torvalds 	struct xfrm_lifetime_cfg lft;
5791da177e4SLinus Torvalds 	struct xfrm_lifetime_cur curlft;
58012a169e7SHerbert Xu 	struct xfrm_policy_walk_entry walk;
581a0073fe1SSteffen Klassert 	struct xfrm_policy_queue polq;
58246ca5f5dSArnaldo Carvalho de Melo 	u8			type;
58346ca5f5dSArnaldo Carvalho de Melo 	u8			action;
58446ca5f5dSArnaldo Carvalho de Melo 	u8			flags;
58546ca5f5dSArnaldo Carvalho de Melo 	u8			xfrm_nr;
58612a169e7SHerbert Xu 	u16			family;
587df71837dSTrent Jaeger 	struct xfrm_sec_ctx	*security;
5881da177e4SLinus Torvalds 	struct xfrm_tmpl       	xfrm_vec[XFRM_MAX_DEPTH];
58956f04730SEric Dumazet 	struct rcu_head		rcu;
5901da177e4SLinus Torvalds };
5911da177e4SLinus Torvalds 
59263eb23f5SDavid S. Miller static inline struct net *xp_net(const struct xfrm_policy *xp)
5930331b1f3SAlexey Dobriyan {
5940331b1f3SAlexey Dobriyan 	return read_pnet(&xp->xp_net);
5950331b1f3SAlexey Dobriyan }
5960331b1f3SAlexey Dobriyan 
59713c1d189SArnaud Ebalard struct xfrm_kmaddress {
59813c1d189SArnaud Ebalard 	xfrm_address_t          local;
59913c1d189SArnaud Ebalard 	xfrm_address_t          remote;
60013c1d189SArnaud Ebalard 	u32			reserved;
60113c1d189SArnaud Ebalard 	u16			family;
60213c1d189SArnaud Ebalard };
60313c1d189SArnaud Ebalard 
60480c9abaaSShinta Sugimoto struct xfrm_migrate {
60580c9abaaSShinta Sugimoto 	xfrm_address_t		old_daddr;
60680c9abaaSShinta Sugimoto 	xfrm_address_t		old_saddr;
60780c9abaaSShinta Sugimoto 	xfrm_address_t		new_daddr;
60880c9abaaSShinta Sugimoto 	xfrm_address_t		new_saddr;
60980c9abaaSShinta Sugimoto 	u8			proto;
61080c9abaaSShinta Sugimoto 	u8			mode;
61180c9abaaSShinta Sugimoto 	u16			reserved;
61280c9abaaSShinta Sugimoto 	u32			reqid;
61380c9abaaSShinta Sugimoto 	u16			old_family;
61480c9abaaSShinta Sugimoto 	u16			new_family;
61580c9abaaSShinta Sugimoto };
61680c9abaaSShinta Sugimoto 
6171da177e4SLinus Torvalds #define XFRM_KM_TIMEOUT                30
618f8cd5488SJamal Hadi Salim /* what happened */
619f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_UPDATE	XFRM_AE_CR
620f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_TIMEOUT	XFRM_AE_CE
621f8cd5488SJamal Hadi Salim 
622f8cd5488SJamal Hadi Salim /* default aevent timeout in units of 100ms */
623f8cd5488SJamal Hadi Salim #define XFRM_AE_ETIME			10
624f8cd5488SJamal Hadi Salim /* Async Event timer multiplier */
625f8cd5488SJamal Hadi Salim #define XFRM_AE_ETH_M			10
626f8cd5488SJamal Hadi Salim /* default seq threshold size */
627f8cd5488SJamal Hadi Salim #define XFRM_AE_SEQT_SIZE		2
6281da177e4SLinus Torvalds 
629fd2c3ef7SEric Dumazet struct xfrm_mgr {
6301da177e4SLinus Torvalds 	struct list_head	list;
631214e005bSDavid S. Miller 	int			(*notify)(struct xfrm_state *x, const struct km_event *c);
63265e0736bSFan Du 	int			(*acquire)(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *xp);
633cb969f07SVenkat Yekkirala 	struct xfrm_policy	*(*compile_policy)(struct sock *sk, int opt, u8 *data, int len, int *dir);
6345d36b180SAl Viro 	int			(*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport);
635214e005bSDavid S. Miller 	int			(*notify_policy)(struct xfrm_policy *x, int dir, const struct km_event *c);
636db983c11SAlexey Dobriyan 	int			(*report)(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr);
637183cad12SDavid S. Miller 	int			(*migrate)(const struct xfrm_selector *sel,
638183cad12SDavid S. Miller 					   u8 dir, u8 type,
639183cad12SDavid S. Miller 					   const struct xfrm_migrate *m,
640183cad12SDavid S. Miller 					   int num_bundles,
6418bafd730SAntony Antony 					   const struct xfrm_kmaddress *k,
6428bafd730SAntony Antony 					   const struct xfrm_encap_tmpl *encap);
6430f24558eSHoria Geanta 	bool			(*is_alive)(const struct km_event *c);
6441da177e4SLinus Torvalds };
6451da177e4SLinus Torvalds 
646d511337aSJoe Perches int xfrm_register_km(struct xfrm_mgr *km);
647d511337aSJoe Perches int xfrm_unregister_km(struct xfrm_mgr *km);
6481da177e4SLinus Torvalds 
64970be6c91SSteffen Klassert struct xfrm_tunnel_skb_cb {
65070be6c91SSteffen Klassert 	union {
65170be6c91SSteffen Klassert 		struct inet_skb_parm h4;
65270be6c91SSteffen Klassert 		struct inet6_skb_parm h6;
65370be6c91SSteffen Klassert 	} header;
65470be6c91SSteffen Klassert 
65570be6c91SSteffen Klassert 	union {
65670be6c91SSteffen Klassert 		struct ip_tunnel *ip4;
65770be6c91SSteffen Klassert 		struct ip6_tnl *ip6;
65870be6c91SSteffen Klassert 	} tunnel;
65970be6c91SSteffen Klassert };
66070be6c91SSteffen Klassert 
66170be6c91SSteffen Klassert #define XFRM_TUNNEL_SKB_CB(__skb) ((struct xfrm_tunnel_skb_cb *)&((__skb)->cb[0]))
66270be6c91SSteffen Klassert 
663436a0a40SHerbert Xu /*
664436a0a40SHerbert Xu  * This structure is used for the duration where packets are being
665436a0a40SHerbert Xu  * transformed by IPsec.  As soon as the packet leaves IPsec the
666436a0a40SHerbert Xu  * area beyond the generic IP part may be overwritten.
667436a0a40SHerbert Xu  */
668436a0a40SHerbert Xu struct xfrm_skb_cb {
66970be6c91SSteffen Klassert 	struct xfrm_tunnel_skb_cb header;
670436a0a40SHerbert Xu 
671436a0a40SHerbert Xu         /* Sequence number for replay protection. */
672b318e0e4SHerbert Xu 	union {
6731ce3644aSSteffen Klassert 		struct {
6741ce3644aSSteffen Klassert 			__u32 low;
6751ce3644aSSteffen Klassert 			__u32 hi;
6761ce3644aSSteffen Klassert 		} output;
6771ce3644aSSteffen Klassert 		struct {
6781ce3644aSSteffen Klassert 			__be32 low;
6791ce3644aSSteffen Klassert 			__be32 hi;
6801ce3644aSSteffen Klassert 		} input;
681b318e0e4SHerbert Xu 	} seq;
682436a0a40SHerbert Xu };
683436a0a40SHerbert Xu 
684436a0a40SHerbert Xu #define XFRM_SKB_CB(__skb) ((struct xfrm_skb_cb *)&((__skb)->cb[0]))
685436a0a40SHerbert Xu 
68636cf9acfSHerbert Xu /*
68736cf9acfSHerbert Xu  * This structure is used by the afinfo prepare_input/prepare_output functions
68836cf9acfSHerbert Xu  * to transmit header information to the mode input/output functions.
68936cf9acfSHerbert Xu  */
69036cf9acfSHerbert Xu struct xfrm_mode_skb_cb {
69170be6c91SSteffen Klassert 	struct xfrm_tunnel_skb_cb header;
69236cf9acfSHerbert Xu 
69336cf9acfSHerbert Xu 	/* Copied from header for IPv4, always set to zero and DF for IPv6. */
69436cf9acfSHerbert Xu 	__be16 id;
69536cf9acfSHerbert Xu 	__be16 frag_off;
69636cf9acfSHerbert Xu 
697732c8bd5SHerbert Xu 	/* IP header length (excluding options or extension headers). */
698732c8bd5SHerbert Xu 	u8 ihl;
699732c8bd5SHerbert Xu 
70036cf9acfSHerbert Xu 	/* TOS for IPv4, class for IPv6. */
70136cf9acfSHerbert Xu 	u8 tos;
70236cf9acfSHerbert Xu 
70336cf9acfSHerbert Xu 	/* TTL for IPv4, hop limitfor IPv6. */
70436cf9acfSHerbert Xu 	u8 ttl;
70536cf9acfSHerbert Xu 
70636cf9acfSHerbert Xu 	/* Protocol for IPv4, NH for IPv6. */
70736cf9acfSHerbert Xu 	u8 protocol;
70836cf9acfSHerbert Xu 
709732c8bd5SHerbert Xu 	/* Option length for IPv4, zero for IPv6. */
710732c8bd5SHerbert Xu 	u8 optlen;
711732c8bd5SHerbert Xu 
71236cf9acfSHerbert Xu 	/* Used by IPv6 only, zero for IPv4. */
71336cf9acfSHerbert Xu 	u8 flow_lbl[3];
71436cf9acfSHerbert Xu };
71536cf9acfSHerbert Xu 
71636cf9acfSHerbert Xu #define XFRM_MODE_SKB_CB(__skb) ((struct xfrm_mode_skb_cb *)&((__skb)->cb[0]))
71736cf9acfSHerbert Xu 
718716062fdSHerbert Xu /*
719716062fdSHerbert Xu  * This structure is used by the input processing to locate the SPI and
720716062fdSHerbert Xu  * related information.
721716062fdSHerbert Xu  */
722716062fdSHerbert Xu struct xfrm_spi_skb_cb {
72370be6c91SSteffen Klassert 	struct xfrm_tunnel_skb_cb header;
724716062fdSHerbert Xu 
725716062fdSHerbert Xu 	unsigned int daddroff;
7262fcb45b6SHerbert Xu 	unsigned int family;
7277785bba2SSteffen Klassert 	__be32 seq;
728716062fdSHerbert Xu };
729716062fdSHerbert Xu 
730716062fdSHerbert Xu #define XFRM_SPI_SKB_CB(__skb) ((struct xfrm_spi_skb_cb *)&((__skb)->cb[0]))
731716062fdSHerbert Xu 
732c9204d9cSJoy Latten #ifdef CONFIG_AUDITSYSCALL
733afeb14b4SPaul Moore static inline struct audit_buffer *xfrm_audit_start(const char *op)
734ab5f5e8bSJoy Latten {
735ab5f5e8bSJoy Latten 	struct audit_buffer *audit_buf = NULL;
736ab5f5e8bSJoy Latten 
737afeb14b4SPaul Moore 	if (audit_enabled == 0)
738afeb14b4SPaul Moore 		return NULL;
739ab5f5e8bSJoy Latten 	audit_buf = audit_log_start(current->audit_context, GFP_ATOMIC,
740ab5f5e8bSJoy Latten 				    AUDIT_MAC_IPSEC_EVENT);
741ab5f5e8bSJoy Latten 	if (audit_buf == NULL)
742ab5f5e8bSJoy Latten 		return NULL;
743afeb14b4SPaul Moore 	audit_log_format(audit_buf, "op=%s", op);
744afeb14b4SPaul Moore 	return audit_buf;
745afeb14b4SPaul Moore }
746afeb14b4SPaul Moore 
7472e71029eSTetsuo Handa static inline void xfrm_audit_helper_usrinfo(bool task_valid,
748afeb14b4SPaul Moore 					     struct audit_buffer *audit_buf)
749afeb14b4SPaul Moore {
7502e71029eSTetsuo Handa 	const unsigned int auid = from_kuid(&init_user_ns, task_valid ?
7512e71029eSTetsuo Handa 					    audit_get_loginuid(current) :
7522e71029eSTetsuo Handa 					    INVALID_UID);
7532e71029eSTetsuo Handa 	const unsigned int ses = task_valid ? audit_get_sessionid(current) :
7542e71029eSTetsuo Handa 		(unsigned int) -1;
7552e71029eSTetsuo Handa 
7562e71029eSTetsuo Handa 	audit_log_format(audit_buf, " auid=%u ses=%u", auid, ses);
757ab5f5e8bSJoy Latten 	audit_log_task_context(audit_buf);
758ab5f5e8bSJoy Latten }
759ab5f5e8bSJoy Latten 
7602e71029eSTetsuo Handa void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid);
7612e71029eSTetsuo Handa void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
7622e71029eSTetsuo Handa 			      bool task_valid);
7632e71029eSTetsuo Handa void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid);
7642e71029eSTetsuo Handa void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid);
765d511337aSJoe Perches void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
766afeb14b4SPaul Moore 				      struct sk_buff *skb);
767d511337aSJoe Perches void xfrm_audit_state_replay(struct xfrm_state *x, struct sk_buff *skb,
768d511337aSJoe Perches 			     __be32 net_seq);
769d511337aSJoe Perches void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family);
770d511337aSJoe Perches void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family, __be32 net_spi,
771d511337aSJoe Perches 			       __be32 net_seq);
772d511337aSJoe Perches void xfrm_audit_state_icvfail(struct xfrm_state *x, struct sk_buff *skb,
773d511337aSJoe Perches 			      u8 proto);
774c9204d9cSJoy Latten #else
77541fef0eeSMarcin Slusarz 
77641fef0eeSMarcin Slusarz static inline void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
7772e71029eSTetsuo Handa 					 bool task_valid)
77841fef0eeSMarcin Slusarz {
77941fef0eeSMarcin Slusarz }
78041fef0eeSMarcin Slusarz 
78141fef0eeSMarcin Slusarz static inline void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
7822e71029eSTetsuo Handa 					    bool task_valid)
78341fef0eeSMarcin Slusarz {
78441fef0eeSMarcin Slusarz }
78541fef0eeSMarcin Slusarz 
78641fef0eeSMarcin Slusarz static inline void xfrm_audit_state_add(struct xfrm_state *x, int result,
7872e71029eSTetsuo Handa 					bool task_valid)
78841fef0eeSMarcin Slusarz {
78941fef0eeSMarcin Slusarz }
79041fef0eeSMarcin Slusarz 
79141fef0eeSMarcin Slusarz static inline void xfrm_audit_state_delete(struct xfrm_state *x, int result,
7922e71029eSTetsuo Handa 					   bool task_valid)
79341fef0eeSMarcin Slusarz {
79441fef0eeSMarcin Slusarz }
79541fef0eeSMarcin Slusarz 
79641fef0eeSMarcin Slusarz static inline void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
79741fef0eeSMarcin Slusarz 					     struct sk_buff *skb)
79841fef0eeSMarcin Slusarz {
79941fef0eeSMarcin Slusarz }
80041fef0eeSMarcin Slusarz 
8019fdc4883SSteffen Klassert static inline void xfrm_audit_state_replay(struct xfrm_state *x,
8029fdc4883SSteffen Klassert 					   struct sk_buff *skb, __be32 net_seq)
8039fdc4883SSteffen Klassert {
8049fdc4883SSteffen Klassert }
8059fdc4883SSteffen Klassert 
80641fef0eeSMarcin Slusarz static inline void xfrm_audit_state_notfound_simple(struct sk_buff *skb,
80741fef0eeSMarcin Slusarz 				      u16 family)
80841fef0eeSMarcin Slusarz {
80941fef0eeSMarcin Slusarz }
81041fef0eeSMarcin Slusarz 
81141fef0eeSMarcin Slusarz static inline void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
81241fef0eeSMarcin Slusarz 				      __be32 net_spi, __be32 net_seq)
81341fef0eeSMarcin Slusarz {
81441fef0eeSMarcin Slusarz }
81541fef0eeSMarcin Slusarz 
81641fef0eeSMarcin Slusarz static inline void xfrm_audit_state_icvfail(struct xfrm_state *x,
81741fef0eeSMarcin Slusarz 				     struct sk_buff *skb, u8 proto)
81841fef0eeSMarcin Slusarz {
81941fef0eeSMarcin Slusarz }
820c9204d9cSJoy Latten #endif /* CONFIG_AUDITSYSCALL */
821161a09e7SJoy Latten 
8221da177e4SLinus Torvalds static inline void xfrm_pol_hold(struct xfrm_policy *policy)
8231da177e4SLinus Torvalds {
8241da177e4SLinus Torvalds 	if (likely(policy != NULL))
825850a6212SReshetova, Elena 		refcount_inc(&policy->refcnt);
8261da177e4SLinus Torvalds }
8271da177e4SLinus Torvalds 
828d511337aSJoe Perches void xfrm_policy_destroy(struct xfrm_policy *policy);
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds static inline void xfrm_pol_put(struct xfrm_policy *policy)
8311da177e4SLinus Torvalds {
832850a6212SReshetova, Elena 	if (refcount_dec_and_test(&policy->refcnt))
83364c31b3fSWANG Cong 		xfrm_policy_destroy(policy);
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
8364e81bb83SMasahide NAKAMURA static inline void xfrm_pols_put(struct xfrm_policy **pols, int npols)
8374e81bb83SMasahide NAKAMURA {
8384e81bb83SMasahide NAKAMURA 	int i;
8394e81bb83SMasahide NAKAMURA 	for (i = npols - 1; i >= 0; --i)
8404e81bb83SMasahide NAKAMURA 		xfrm_pol_put(pols[i]);
8414e81bb83SMasahide NAKAMURA }
8424e81bb83SMasahide NAKAMURA 
843d511337aSJoe Perches void __xfrm_state_destroy(struct xfrm_state *);
8441da177e4SLinus Torvalds 
84521380b81SHerbert Xu static inline void __xfrm_state_put(struct xfrm_state *x)
84621380b81SHerbert Xu {
84788755e9cSReshetova, Elena 	refcount_dec(&x->refcnt);
84821380b81SHerbert Xu }
84921380b81SHerbert Xu 
8501da177e4SLinus Torvalds static inline void xfrm_state_put(struct xfrm_state *x)
8511da177e4SLinus Torvalds {
85288755e9cSReshetova, Elena 	if (refcount_dec_and_test(&x->refcnt))
8531da177e4SLinus Torvalds 		__xfrm_state_destroy(x);
8541da177e4SLinus Torvalds }
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds static inline void xfrm_state_hold(struct xfrm_state *x)
8571da177e4SLinus Torvalds {
85888755e9cSReshetova, Elena 	refcount_inc(&x->refcnt);
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
8611744a8feSDavid S. Miller static inline bool addr_match(const void *token1, const void *token2,
862e1b0048eSAlexey Dobriyan 			      unsigned int prefixlen)
8631da177e4SLinus Torvalds {
8641744a8feSDavid S. Miller 	const __be32 *a1 = token1;
8651744a8feSDavid S. Miller 	const __be32 *a2 = token2;
866e1b0048eSAlexey Dobriyan 	unsigned int pdw;
867e1b0048eSAlexey Dobriyan 	unsigned int pbi;
8681da177e4SLinus Torvalds 
869a6337463Sjamal 	pdw = prefixlen >> 5;	  /* num of whole u32 in prefix */
8701da177e4SLinus Torvalds 	pbi = prefixlen &  0x1f;  /* num of bits in incomplete u32 in prefix */
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	if (pdw)
8731da177e4SLinus Torvalds 		if (memcmp(a1, a2, pdw << 2))
8741744a8feSDavid S. Miller 			return false;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	if (pbi) {
8775f19343fSAl Viro 		__be32 mask;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 		mask = htonl((0xffffffff) << (32 - pbi));
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 		if ((a1[pdw] ^ a2[pdw]) & mask)
8821744a8feSDavid S. Miller 			return false;
8831da177e4SLinus Torvalds 	}
8841da177e4SLinus Torvalds 
8851744a8feSDavid S. Miller 	return true;
8861da177e4SLinus Torvalds }
8871da177e4SLinus Torvalds 
88826bff940SAlexey Dobriyan static inline bool addr4_match(__be32 a1, __be32 a2, u8 prefixlen)
88926bff940SAlexey Dobriyan {
89026bff940SAlexey Dobriyan 	/* C99 6.5.7 (3): u32 << 32 is undefined behaviour */
8916c786bcbSAlexey Dobriyan 	if (sizeof(long) == 4 && prefixlen == 0)
89226bff940SAlexey Dobriyan 		return true;
8936c786bcbSAlexey Dobriyan 	return !((a1 ^ a2) & htonl(~0UL << (32 - prefixlen)));
89426bff940SAlexey Dobriyan }
89526bff940SAlexey Dobriyan 
8961da177e4SLinus Torvalds static __inline__
8976281dcc9SDavid S. Miller __be16 xfrm_flowi_sport(const struct flowi *fl, const union flowi_uli *uli)
8981da177e4SLinus Torvalds {
899f9d07e41SAl Viro 	__be16 port;
9001d28f42cSDavid S. Miller 	switch(fl->flowi_proto) {
9011da177e4SLinus Torvalds 	case IPPROTO_TCP:
9021da177e4SLinus Torvalds 	case IPPROTO_UDP:
903ba4e58ecSGerrit Renker 	case IPPROTO_UDPLITE:
9041da177e4SLinus Torvalds 	case IPPROTO_SCTP:
9056281dcc9SDavid S. Miller 		port = uli->ports.sport;
9061da177e4SLinus Torvalds 		break;
9071da177e4SLinus Torvalds 	case IPPROTO_ICMP:
9081da177e4SLinus Torvalds 	case IPPROTO_ICMPV6:
9096281dcc9SDavid S. Miller 		port = htons(uli->icmpt.type);
9101da177e4SLinus Torvalds 		break;
9112ce4272aSMasahide NAKAMURA 	case IPPROTO_MH:
9126281dcc9SDavid S. Miller 		port = htons(uli->mht.type);
9132ce4272aSMasahide NAKAMURA 		break;
914cc9ff19dSTimo Teräs 	case IPPROTO_GRE:
9156281dcc9SDavid S. Miller 		port = htons(ntohl(uli->gre_key) >> 16);
916cc9ff19dSTimo Teräs 		break;
9171da177e4SLinus Torvalds 	default:
9181da177e4SLinus Torvalds 		port = 0;	/*XXX*/
9191da177e4SLinus Torvalds 	}
9201da177e4SLinus Torvalds 	return port;
9211da177e4SLinus Torvalds }
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds static __inline__
9246281dcc9SDavid S. Miller __be16 xfrm_flowi_dport(const struct flowi *fl, const union flowi_uli *uli)
9251da177e4SLinus Torvalds {
926f9d07e41SAl Viro 	__be16 port;
9271d28f42cSDavid S. Miller 	switch(fl->flowi_proto) {
9281da177e4SLinus Torvalds 	case IPPROTO_TCP:
9291da177e4SLinus Torvalds 	case IPPROTO_UDP:
930ba4e58ecSGerrit Renker 	case IPPROTO_UDPLITE:
9311da177e4SLinus Torvalds 	case IPPROTO_SCTP:
9326281dcc9SDavid S. Miller 		port = uli->ports.dport;
9331da177e4SLinus Torvalds 		break;
9341da177e4SLinus Torvalds 	case IPPROTO_ICMP:
9351da177e4SLinus Torvalds 	case IPPROTO_ICMPV6:
9366281dcc9SDavid S. Miller 		port = htons(uli->icmpt.code);
9371da177e4SLinus Torvalds 		break;
938cc9ff19dSTimo Teräs 	case IPPROTO_GRE:
9396281dcc9SDavid S. Miller 		port = htons(ntohl(uli->gre_key) & 0xffff);
940cc9ff19dSTimo Teräs 		break;
9411da177e4SLinus Torvalds 	default:
9421da177e4SLinus Torvalds 		port = 0;	/*XXX*/
9431da177e4SLinus Torvalds 	}
9441da177e4SLinus Torvalds 	return port;
9451da177e4SLinus Torvalds }
9461da177e4SLinus Torvalds 
947d511337aSJoe Perches bool xfrm_selector_match(const struct xfrm_selector *sel,
948d511337aSJoe Perches 			 const struct flowi *fl, unsigned short family);
9491da177e4SLinus Torvalds 
950df71837dSTrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
951df71837dSTrent Jaeger /*	If neither has a context --> match
952df71837dSTrent Jaeger  * 	Otherwise, both must have a context and the sids, doi, alg must match
953df71837dSTrent Jaeger  */
954bc9b35adSDavid S. Miller static inline bool xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
955df71837dSTrent Jaeger {
956df71837dSTrent Jaeger 	return ((!s1 && !s2) ||
957df71837dSTrent Jaeger 		(s1 && s2 &&
958df71837dSTrent Jaeger 		 (s1->ctx_sid == s2->ctx_sid) &&
959df71837dSTrent Jaeger 		 (s1->ctx_doi == s2->ctx_doi) &&
960df71837dSTrent Jaeger 		 (s1->ctx_alg == s2->ctx_alg)));
961df71837dSTrent Jaeger }
962df71837dSTrent Jaeger #else
963bc9b35adSDavid S. Miller static inline bool xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
964df71837dSTrent Jaeger {
965bc9b35adSDavid S. Miller 	return true;
966df71837dSTrent Jaeger }
967df71837dSTrent Jaeger #endif
968df71837dSTrent Jaeger 
9691da177e4SLinus Torvalds /* A struct encoding bundle of transformations to apply to some set of flow.
9701da177e4SLinus Torvalds  *
971b6ca8bd5SDavid Miller  * xdst->child points to the next element of bundle.
9721da177e4SLinus Torvalds  * dst->xfrm  points to an instanse of transformer.
9731da177e4SLinus Torvalds  *
9741da177e4SLinus Torvalds  * Due to unfortunate limitations of current routing cache, which we
9751da177e4SLinus Torvalds  * have no time to fix, it mirrors struct rtable and bound to the same
9761da177e4SLinus Torvalds  * routing key, including saddr,daddr. However, we can have many of
9771da177e4SLinus Torvalds  * bundles differing by session id. All the bundles grow from a parent
9781da177e4SLinus Torvalds  * policy rule.
9791da177e4SLinus Torvalds  */
980fd2c3ef7SEric Dumazet struct xfrm_dst {
9811da177e4SLinus Torvalds 	union {
9821da177e4SLinus Torvalds 		struct dst_entry	dst;
9831da177e4SLinus Torvalds 		struct rtable		rt;
9841da177e4SLinus Torvalds 		struct rt6_info		rt6;
9851da177e4SLinus Torvalds 	} u;
9861da177e4SLinus Torvalds 	struct dst_entry *route;
987b6ca8bd5SDavid Miller 	struct dst_entry *child;
9880f6c480fSDavid Miller 	struct dst_entry *path;
98980c802f3STimo Teräs 	struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
99080c802f3STimo Teräs 	int num_pols, num_xfrms;
99180c802f3STimo Teräs 	u32 xfrm_genid;
99280c802f3STimo Teräs 	u32 policy_genid;
9931da177e4SLinus Torvalds 	u32 route_mtu_cached;
9941da177e4SLinus Torvalds 	u32 child_mtu_cached;
99592d63decSHideaki YOSHIFUJI 	u32 route_cookie;
99692d63decSHideaki YOSHIFUJI 	u32 path_cookie;
9971da177e4SLinus Torvalds };
9981da177e4SLinus Torvalds 
9990f6c480fSDavid Miller static inline struct dst_entry *xfrm_dst_path(const struct dst_entry *dst)
10000f6c480fSDavid Miller {
10010f6c480fSDavid Miller #ifdef CONFIG_XFRM
10020f6c480fSDavid Miller 	if (dst->xfrm) {
10030f6c480fSDavid Miller 		const struct xfrm_dst *xdst = (const struct xfrm_dst *) dst;
10040f6c480fSDavid Miller 
10050f6c480fSDavid Miller 		return xdst->path;
10060f6c480fSDavid Miller 	}
10070f6c480fSDavid Miller #endif
10080f6c480fSDavid Miller 	return (struct dst_entry *) dst;
10090f6c480fSDavid Miller }
10100f6c480fSDavid Miller 
1011b92cf4aaSDavid Miller static inline struct dst_entry *xfrm_dst_child(const struct dst_entry *dst)
1012b92cf4aaSDavid Miller {
1013b92cf4aaSDavid Miller #ifdef CONFIG_XFRM
1014b6ca8bd5SDavid Miller 	if (dst->xfrm) {
1015b6ca8bd5SDavid Miller 		struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
1016b6ca8bd5SDavid Miller 		return xdst->child;
1017b6ca8bd5SDavid Miller 	}
1018b92cf4aaSDavid Miller #endif
1019b92cf4aaSDavid Miller 	return NULL;
1020b92cf4aaSDavid Miller }
1021b92cf4aaSDavid Miller 
1022def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
102345b018beSDavid Miller static inline void xfrm_dst_set_child(struct xfrm_dst *xdst, struct dst_entry *child)
102445b018beSDavid Miller {
1025b6ca8bd5SDavid Miller 	xdst->child = child;
102645b018beSDavid Miller }
102745b018beSDavid Miller 
1028aabc9761SHerbert Xu static inline void xfrm_dst_destroy(struct xfrm_dst *xdst)
1029aabc9761SHerbert Xu {
103080c802f3STimo Teräs 	xfrm_pols_put(xdst->pols, xdst->num_pols);
1031aabc9761SHerbert Xu 	dst_release(xdst->route);
1032aabc9761SHerbert Xu 	if (likely(xdst->u.dst.xfrm))
1033aabc9761SHerbert Xu 		xfrm_state_put(xdst->u.dst.xfrm);
1034aabc9761SHerbert Xu }
1035def8b4faSAlexey Dobriyan #endif
1036aabc9761SHerbert Xu 
1037d511337aSJoe Perches void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev);
1038aabc9761SHerbert Xu 
103954ef207aSSteffen Klassert struct xfrm_offload {
104054ef207aSSteffen Klassert 	/* Output sequence number for replay protection on offloading. */
104154ef207aSSteffen Klassert 	struct {
104254ef207aSSteffen Klassert 		__u32 low;
104354ef207aSSteffen Klassert 		__u32 hi;
104454ef207aSSteffen Klassert 	} seq;
104554ef207aSSteffen Klassert 
104654ef207aSSteffen Klassert 	__u32			flags;
104754ef207aSSteffen Klassert #define	SA_DELETE_REQ		1
104854ef207aSSteffen Klassert #define	CRYPTO_DONE		2
104954ef207aSSteffen Klassert #define	CRYPTO_NEXT_DONE	4
105054ef207aSSteffen Klassert #define	CRYPTO_FALLBACK		8
105154ef207aSSteffen Klassert #define	XFRM_GSO_SEGMENT	16
105254ef207aSSteffen Klassert #define	XFRM_GRO		32
105347ebcc0bSYossi Kuperman #define	XFRM_ESP_NO_TRAILER	64
105454ef207aSSteffen Klassert 
105554ef207aSSteffen Klassert 	__u32			status;
105654ef207aSSteffen Klassert #define CRYPTO_SUCCESS				1
105754ef207aSSteffen Klassert #define CRYPTO_GENERIC_ERROR			2
105854ef207aSSteffen Klassert #define CRYPTO_TRANSPORT_AH_AUTH_FAILED		4
105954ef207aSSteffen Klassert #define CRYPTO_TRANSPORT_ESP_AUTH_FAILED	8
106054ef207aSSteffen Klassert #define CRYPTO_TUNNEL_AH_AUTH_FAILED		16
106154ef207aSSteffen Klassert #define CRYPTO_TUNNEL_ESP_AUTH_FAILED		32
106254ef207aSSteffen Klassert #define CRYPTO_INVALID_PACKET_SYNTAX		64
106354ef207aSSteffen Klassert #define CRYPTO_INVALID_PROTOCOL			128
106454ef207aSSteffen Klassert 
106554ef207aSSteffen Klassert 	__u8			proto;
106654ef207aSSteffen Klassert };
106754ef207aSSteffen Klassert 
1068fd2c3ef7SEric Dumazet struct sec_path {
106955eabed6SReshetova, Elena 	refcount_t		refcnt;
10701da177e4SLinus Torvalds 	int			len;
107154ef207aSSteffen Klassert 	int			olen;
107254ef207aSSteffen Klassert 
1073dbe5b4aaSHerbert Xu 	struct xfrm_state	*xvec[XFRM_MAX_DEPTH];
107454ef207aSSteffen Klassert 	struct xfrm_offload	ovec[XFRM_MAX_OFFLOAD_DEPTH];
10751da177e4SLinus Torvalds };
10761da177e4SLinus Torvalds 
1077990078afSMichael Smith static inline int secpath_exists(struct sk_buff *skb)
1078990078afSMichael Smith {
1079990078afSMichael Smith #ifdef CONFIG_XFRM
1080990078afSMichael Smith 	return skb->sp != NULL;
1081990078afSMichael Smith #else
1082990078afSMichael Smith 	return 0;
1083990078afSMichael Smith #endif
1084990078afSMichael Smith }
1085990078afSMichael Smith 
10861da177e4SLinus Torvalds static inline struct sec_path *
10871da177e4SLinus Torvalds secpath_get(struct sec_path *sp)
10881da177e4SLinus Torvalds {
10891da177e4SLinus Torvalds 	if (sp)
109055eabed6SReshetova, Elena 		refcount_inc(&sp->refcnt);
10911da177e4SLinus Torvalds 	return sp;
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
1094d511337aSJoe Perches void __secpath_destroy(struct sec_path *sp);
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds static inline void
10971da177e4SLinus Torvalds secpath_put(struct sec_path *sp)
10981da177e4SLinus Torvalds {
109955eabed6SReshetova, Elena 	if (sp && refcount_dec_and_test(&sp->refcnt))
11001da177e4SLinus Torvalds 		__secpath_destroy(sp);
11011da177e4SLinus Torvalds }
11021da177e4SLinus Torvalds 
1103d511337aSJoe Perches struct sec_path *secpath_dup(struct sec_path *src);
1104b0fcee82SSteffen Klassert int secpath_set(struct sk_buff *skb);
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds static inline void
11071da177e4SLinus Torvalds secpath_reset(struct sk_buff *skb)
11081da177e4SLinus Torvalds {
11091da177e4SLinus Torvalds #ifdef CONFIG_XFRM
11101da177e4SLinus Torvalds 	secpath_put(skb->sp);
11111da177e4SLinus Torvalds 	skb->sp = NULL;
11121da177e4SLinus Torvalds #endif
11131da177e4SLinus Torvalds }
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds static inline int
11166cc32961SDavid S. Miller xfrm_addr_any(const xfrm_address_t *addr, unsigned short family)
1117a1e59abfSPatrick McHardy {
1118a1e59abfSPatrick McHardy 	switch (family) {
1119a1e59abfSPatrick McHardy 	case AF_INET:
1120a1e59abfSPatrick McHardy 		return addr->a4 == 0;
1121a1e59abfSPatrick McHardy 	case AF_INET6:
112215e318bdSJiri Benc 		return ipv6_addr_any(&addr->in6);
1123a1e59abfSPatrick McHardy 	}
1124a1e59abfSPatrick McHardy 	return 0;
1125a1e59abfSPatrick McHardy }
1126a1e59abfSPatrick McHardy 
1127a1e59abfSPatrick McHardy static inline int
112821eddb5cSDavid S. Miller __xfrm4_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x)
11291da177e4SLinus Torvalds {
11301da177e4SLinus Torvalds 	return	(tmpl->saddr.a4 &&
11311da177e4SLinus Torvalds 		 tmpl->saddr.a4 != x->props.saddr.a4);
11321da177e4SLinus Torvalds }
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds static inline int
113521eddb5cSDavid S. Miller __xfrm6_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x)
11361da177e4SLinus Torvalds {
11371da177e4SLinus Torvalds 	return	(!ipv6_addr_any((struct in6_addr*)&tmpl->saddr) &&
1138ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 		 !ipv6_addr_equal((struct in6_addr *)&tmpl->saddr, (struct in6_addr*)&x->props.saddr));
11391da177e4SLinus Torvalds }
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds static inline int
114221eddb5cSDavid S. Miller xfrm_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x, unsigned short family)
11431da177e4SLinus Torvalds {
11441da177e4SLinus Torvalds 	switch (family) {
11451da177e4SLinus Torvalds 	case AF_INET:
11461da177e4SLinus Torvalds 		return __xfrm4_state_addr_cmp(tmpl, x);
11471da177e4SLinus Torvalds 	case AF_INET6:
11481da177e4SLinus Torvalds 		return __xfrm6_state_addr_cmp(tmpl, x);
11491da177e4SLinus Torvalds 	}
11501da177e4SLinus Torvalds 	return !0;
11511da177e4SLinus Torvalds }
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds #ifdef CONFIG_XFRM
1154d511337aSJoe Perches int __xfrm_policy_check(struct sock *, int dir, struct sk_buff *skb,
1155d511337aSJoe Perches 			unsigned short family);
11561da177e4SLinus Torvalds 
1157d5422efeSHerbert Xu static inline int __xfrm_policy_check2(struct sock *sk, int dir,
1158d5422efeSHerbert Xu 				       struct sk_buff *skb,
1159d5422efeSHerbert Xu 				       unsigned int family, int reverse)
11601da177e4SLinus Torvalds {
1161f6e1e25dSAlexey Dobriyan 	struct net *net = dev_net(skb->dev);
1162d5422efeSHerbert Xu 	int ndir = dir | (reverse ? XFRM_POLICY_MASK + 1 : 0);
1163d5422efeSHerbert Xu 
11641da177e4SLinus Torvalds 	if (sk && sk->sk_policy[XFRM_POLICY_IN])
1165d5422efeSHerbert Xu 		return __xfrm_policy_check(sk, ndir, skb, family);
11661da177e4SLinus Torvalds 
1167f6e1e25dSAlexey Dobriyan 	return	(!net->xfrm.policy_count[dir] && !skb->sp) ||
1168adf30907SEric Dumazet 		(skb_dst(skb)->flags & DST_NOPOLICY) ||
1169d5422efeSHerbert Xu 		__xfrm_policy_check(sk, ndir, skb, family);
1170d5422efeSHerbert Xu }
1171d5422efeSHerbert Xu 
1172d5422efeSHerbert Xu static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
1173d5422efeSHerbert Xu {
1174d5422efeSHerbert Xu 	return __xfrm_policy_check2(sk, dir, skb, family, 0);
11751da177e4SLinus Torvalds }
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
11781da177e4SLinus Torvalds {
11791da177e4SLinus Torvalds 	return xfrm_policy_check(sk, dir, skb, AF_INET);
11801da177e4SLinus Torvalds }
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
11831da177e4SLinus Torvalds {
11841da177e4SLinus Torvalds 	return xfrm_policy_check(sk, dir, skb, AF_INET6);
11851da177e4SLinus Torvalds }
11861da177e4SLinus Torvalds 
1187d5422efeSHerbert Xu static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir,
1188d5422efeSHerbert Xu 					     struct sk_buff *skb)
1189d5422efeSHerbert Xu {
1190d5422efeSHerbert Xu 	return __xfrm_policy_check2(sk, dir, skb, AF_INET, 1);
1191d5422efeSHerbert Xu }
1192d5422efeSHerbert Xu 
1193d5422efeSHerbert Xu static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir,
1194d5422efeSHerbert Xu 					     struct sk_buff *skb)
1195d5422efeSHerbert Xu {
1196d5422efeSHerbert Xu 	return __xfrm_policy_check2(sk, dir, skb, AF_INET6, 1);
1197d5422efeSHerbert Xu }
1198d5422efeSHerbert Xu 
1199d511337aSJoe Perches int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1200d5422efeSHerbert Xu 			  unsigned int family, int reverse);
1201d5422efeSHerbert Xu 
1202d5422efeSHerbert Xu static inline int xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1203d5422efeSHerbert Xu 				      unsigned int family)
1204d5422efeSHerbert Xu {
1205d5422efeSHerbert Xu 	return __xfrm_decode_session(skb, fl, family, 0);
1206d5422efeSHerbert Xu }
1207d5422efeSHerbert Xu 
1208d5422efeSHerbert Xu static inline int xfrm_decode_session_reverse(struct sk_buff *skb,
1209d5422efeSHerbert Xu 					      struct flowi *fl,
1210d5422efeSHerbert Xu 					      unsigned int family)
1211d5422efeSHerbert Xu {
1212d5422efeSHerbert Xu 	return __xfrm_decode_session(skb, fl, family, 1);
1213d5422efeSHerbert Xu }
1214d5422efeSHerbert Xu 
1215d511337aSJoe Perches int __xfrm_route_forward(struct sk_buff *skb, unsigned short family);
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds static inline int xfrm_route_forward(struct sk_buff *skb, unsigned short family)
12181da177e4SLinus Torvalds {
121999a66657SAlexey Dobriyan 	struct net *net = dev_net(skb->dev);
122099a66657SAlexey Dobriyan 
122199a66657SAlexey Dobriyan 	return	!net->xfrm.policy_count[XFRM_POLICY_OUT] ||
1222adf30907SEric Dumazet 		(skb_dst(skb)->flags & DST_NOXFRM) ||
12231da177e4SLinus Torvalds 		__xfrm_route_forward(skb, family);
12241da177e4SLinus Torvalds }
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds static inline int xfrm4_route_forward(struct sk_buff *skb)
12271da177e4SLinus Torvalds {
12281da177e4SLinus Torvalds 	return xfrm_route_forward(skb, AF_INET);
12291da177e4SLinus Torvalds }
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds static inline int xfrm6_route_forward(struct sk_buff *skb)
12321da177e4SLinus Torvalds {
12331da177e4SLinus Torvalds 	return xfrm_route_forward(skb, AF_INET6);
12341da177e4SLinus Torvalds }
12351da177e4SLinus Torvalds 
1236d188ba86SEric Dumazet int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk);
12371da177e4SLinus Torvalds 
1238d188ba86SEric Dumazet static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
12391da177e4SLinus Torvalds {
1240d188ba86SEric Dumazet 	sk->sk_policy[0] = NULL;
1241d188ba86SEric Dumazet 	sk->sk_policy[1] = NULL;
1242d188ba86SEric Dumazet 	if (unlikely(osk->sk_policy[0] || osk->sk_policy[1]))
1243d188ba86SEric Dumazet 		return __xfrm_sk_clone_policy(sk, osk);
12441da177e4SLinus Torvalds 	return 0;
12451da177e4SLinus Torvalds }
12461da177e4SLinus Torvalds 
1247d511337aSJoe Perches int xfrm_policy_delete(struct xfrm_policy *pol, int dir);
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds static inline void xfrm_sk_free_policy(struct sock *sk)
12501da177e4SLinus Torvalds {
1251d188ba86SEric Dumazet 	struct xfrm_policy *pol;
1252d188ba86SEric Dumazet 
1253d188ba86SEric Dumazet 	pol = rcu_dereference_protected(sk->sk_policy[0], 1);
1254d188ba86SEric Dumazet 	if (unlikely(pol != NULL)) {
1255d188ba86SEric Dumazet 		xfrm_policy_delete(pol, XFRM_POLICY_MAX);
12561da177e4SLinus Torvalds 		sk->sk_policy[0] = NULL;
12571da177e4SLinus Torvalds 	}
1258d188ba86SEric Dumazet 	pol = rcu_dereference_protected(sk->sk_policy[1], 1);
1259d188ba86SEric Dumazet 	if (unlikely(pol != NULL)) {
1260d188ba86SEric Dumazet 		xfrm_policy_delete(pol, XFRM_POLICY_MAX+1);
12611da177e4SLinus Torvalds 		sk->sk_policy[1] = NULL;
12621da177e4SLinus Torvalds 	}
12631da177e4SLinus Torvalds }
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds #else
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds static inline void xfrm_sk_free_policy(struct sock *sk) {}
1268d188ba86SEric Dumazet static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk) { return 0; }
12691da177e4SLinus Torvalds static inline int xfrm6_route_forward(struct sk_buff *skb) { return 1; }
12701da177e4SLinus Torvalds static inline int xfrm4_route_forward(struct sk_buff *skb) { return 1; }
12711da177e4SLinus Torvalds static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
12721da177e4SLinus Torvalds {
12731da177e4SLinus Torvalds 	return 1;
12741da177e4SLinus Torvalds }
12751da177e4SLinus Torvalds static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
12761da177e4SLinus Torvalds {
12771da177e4SLinus Torvalds 	return 1;
12781da177e4SLinus Torvalds }
12791da177e4SLinus Torvalds static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
12801da177e4SLinus Torvalds {
12811da177e4SLinus Torvalds 	return 1;
12821da177e4SLinus Torvalds }
1283d5422efeSHerbert Xu static inline int xfrm_decode_session_reverse(struct sk_buff *skb,
1284d5422efeSHerbert Xu 					      struct flowi *fl,
1285d5422efeSHerbert Xu 					      unsigned int family)
1286d5422efeSHerbert Xu {
1287d5422efeSHerbert Xu 	return -ENOSYS;
1288d5422efeSHerbert Xu }
1289d5422efeSHerbert Xu static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir,
1290d5422efeSHerbert Xu 					     struct sk_buff *skb)
1291d5422efeSHerbert Xu {
1292d5422efeSHerbert Xu 	return 1;
1293d5422efeSHerbert Xu }
1294d5422efeSHerbert Xu static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir,
1295d5422efeSHerbert Xu 					     struct sk_buff *skb)
1296d5422efeSHerbert Xu {
1297d5422efeSHerbert Xu 	return 1;
1298d5422efeSHerbert Xu }
12991da177e4SLinus Torvalds #endif
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds static __inline__
1302e8a4e377SDavid S. Miller xfrm_address_t *xfrm_flowi_daddr(const struct flowi *fl, unsigned short family)
13031da177e4SLinus Torvalds {
13041da177e4SLinus Torvalds 	switch (family){
13051da177e4SLinus Torvalds 	case AF_INET:
13067e1dc7b6SDavid S. Miller 		return (xfrm_address_t *)&fl->u.ip4.daddr;
13071da177e4SLinus Torvalds 	case AF_INET6:
13087e1dc7b6SDavid S. Miller 		return (xfrm_address_t *)&fl->u.ip6.daddr;
13091da177e4SLinus Torvalds 	}
13101da177e4SLinus Torvalds 	return NULL;
13111da177e4SLinus Torvalds }
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds static __inline__
1314e8a4e377SDavid S. Miller xfrm_address_t *xfrm_flowi_saddr(const struct flowi *fl, unsigned short family)
13151da177e4SLinus Torvalds {
13161da177e4SLinus Torvalds 	switch (family){
13171da177e4SLinus Torvalds 	case AF_INET:
13187e1dc7b6SDavid S. Miller 		return (xfrm_address_t *)&fl->u.ip4.saddr;
13191da177e4SLinus Torvalds 	case AF_INET6:
13207e1dc7b6SDavid S. Miller 		return (xfrm_address_t *)&fl->u.ip6.saddr;
13211da177e4SLinus Torvalds 	}
13221da177e4SLinus Torvalds 	return NULL;
13231da177e4SLinus Torvalds }
13241da177e4SLinus Torvalds 
13259bb182a7SYOSHIFUJI Hideaki static __inline__
1326e8a4e377SDavid S. Miller void xfrm_flowi_addr_get(const struct flowi *fl,
13279bb182a7SYOSHIFUJI Hideaki 			 xfrm_address_t *saddr, xfrm_address_t *daddr,
13289bb182a7SYOSHIFUJI Hideaki 			 unsigned short family)
13299bb182a7SYOSHIFUJI Hideaki {
13309bb182a7SYOSHIFUJI Hideaki 	switch(family) {
13319bb182a7SYOSHIFUJI Hideaki 	case AF_INET:
13327e1dc7b6SDavid S. Miller 		memcpy(&saddr->a4, &fl->u.ip4.saddr, sizeof(saddr->a4));
13337e1dc7b6SDavid S. Miller 		memcpy(&daddr->a4, &fl->u.ip4.daddr, sizeof(daddr->a4));
13349bb182a7SYOSHIFUJI Hideaki 		break;
13359bb182a7SYOSHIFUJI Hideaki 	case AF_INET6:
133615e318bdSJiri Benc 		saddr->in6 = fl->u.ip6.saddr;
133715e318bdSJiri Benc 		daddr->in6 = fl->u.ip6.daddr;
13389bb182a7SYOSHIFUJI Hideaki 		break;
13399bb182a7SYOSHIFUJI Hideaki 	}
13409bb182a7SYOSHIFUJI Hideaki }
13419bb182a7SYOSHIFUJI Hideaki 
13421da177e4SLinus Torvalds static __inline__ int
1343f8848067SDavid S. Miller __xfrm4_state_addr_check(const struct xfrm_state *x,
1344f8848067SDavid S. Miller 			 const xfrm_address_t *daddr, const xfrm_address_t *saddr)
13451da177e4SLinus Torvalds {
13461da177e4SLinus Torvalds 	if (daddr->a4 == x->id.daddr.a4 &&
13471da177e4SLinus Torvalds 	    (saddr->a4 == x->props.saddr.a4 || !saddr->a4 || !x->props.saddr.a4))
13481da177e4SLinus Torvalds 		return 1;
13491da177e4SLinus Torvalds 	return 0;
13501da177e4SLinus Torvalds }
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds static __inline__ int
1353f8848067SDavid S. Miller __xfrm6_state_addr_check(const struct xfrm_state *x,
1354f8848067SDavid S. Miller 			 const xfrm_address_t *daddr, const xfrm_address_t *saddr)
13551da177e4SLinus Torvalds {
1356ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal((struct in6_addr *)daddr, (struct in6_addr *)&x->id.daddr) &&
1357ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 	    (ipv6_addr_equal((struct in6_addr *)saddr, (struct in6_addr *)&x->props.saddr) ||
13581da177e4SLinus Torvalds 	     ipv6_addr_any((struct in6_addr *)saddr) ||
13591da177e4SLinus Torvalds 	     ipv6_addr_any((struct in6_addr *)&x->props.saddr)))
13601da177e4SLinus Torvalds 		return 1;
13611da177e4SLinus Torvalds 	return 0;
13621da177e4SLinus Torvalds }
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds static __inline__ int
1365f8848067SDavid S. Miller xfrm_state_addr_check(const struct xfrm_state *x,
1366f8848067SDavid S. Miller 		      const xfrm_address_t *daddr, const xfrm_address_t *saddr,
13671da177e4SLinus Torvalds 		      unsigned short family)
13681da177e4SLinus Torvalds {
13691da177e4SLinus Torvalds 	switch (family) {
13701da177e4SLinus Torvalds 	case AF_INET:
13711da177e4SLinus Torvalds 		return __xfrm4_state_addr_check(x, daddr, saddr);
13721da177e4SLinus Torvalds 	case AF_INET6:
13731da177e4SLinus Torvalds 		return __xfrm6_state_addr_check(x, daddr, saddr);
13741da177e4SLinus Torvalds 	}
13751da177e4SLinus Torvalds 	return 0;
13761da177e4SLinus Torvalds }
13771da177e4SLinus Torvalds 
1378e53820deSMasahide NAKAMURA static __inline__ int
1379f8848067SDavid S. Miller xfrm_state_addr_flow_check(const struct xfrm_state *x, const struct flowi *fl,
1380e53820deSMasahide NAKAMURA 			   unsigned short family)
1381e53820deSMasahide NAKAMURA {
1382e53820deSMasahide NAKAMURA 	switch (family) {
1383e53820deSMasahide NAKAMURA 	case AF_INET:
1384e53820deSMasahide NAKAMURA 		return __xfrm4_state_addr_check(x,
13857e1dc7b6SDavid S. Miller 						(const xfrm_address_t *)&fl->u.ip4.daddr,
13867e1dc7b6SDavid S. Miller 						(const xfrm_address_t *)&fl->u.ip4.saddr);
1387e53820deSMasahide NAKAMURA 	case AF_INET6:
1388e53820deSMasahide NAKAMURA 		return __xfrm6_state_addr_check(x,
13897e1dc7b6SDavid S. Miller 						(const xfrm_address_t *)&fl->u.ip6.daddr,
13907e1dc7b6SDavid S. Miller 						(const xfrm_address_t *)&fl->u.ip6.saddr);
1391e53820deSMasahide NAKAMURA 	}
1392e53820deSMasahide NAKAMURA 	return 0;
1393e53820deSMasahide NAKAMURA }
1394e53820deSMasahide NAKAMURA 
1395f8848067SDavid S. Miller static inline int xfrm_state_kern(const struct xfrm_state *x)
13961da177e4SLinus Torvalds {
13971da177e4SLinus Torvalds 	return atomic_read(&x->tunnel_users);
13981da177e4SLinus Torvalds }
13991da177e4SLinus Torvalds 
14005794708fSMasahide NAKAMURA static inline int xfrm_id_proto_match(u8 proto, u8 userproto)
14015794708fSMasahide NAKAMURA {
1402dc00a525SMasahide NAKAMURA 	return (!userproto || proto == userproto ||
1403dc00a525SMasahide NAKAMURA 		(userproto == IPSEC_PROTO_ANY && (proto == IPPROTO_AH ||
1404dc00a525SMasahide NAKAMURA 						  proto == IPPROTO_ESP ||
1405dc00a525SMasahide NAKAMURA 						  proto == IPPROTO_COMP)));
14065794708fSMasahide NAKAMURA }
14075794708fSMasahide NAKAMURA 
14081da177e4SLinus Torvalds /*
14091da177e4SLinus Torvalds  * xfrm algorithm information
14101da177e4SLinus Torvalds  */
14111a6509d9SHerbert Xu struct xfrm_algo_aead_info {
1412165ecc63SHerbert Xu 	char *geniv;
14131a6509d9SHerbert Xu 	u16 icv_truncbits;
14141a6509d9SHerbert Xu };
14151a6509d9SHerbert Xu 
14161da177e4SLinus Torvalds struct xfrm_algo_auth_info {
14171da177e4SLinus Torvalds 	u16 icv_truncbits;
14181da177e4SLinus Torvalds 	u16 icv_fullbits;
14191da177e4SLinus Torvalds };
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds struct xfrm_algo_encr_info {
1422165ecc63SHerbert Xu 	char *geniv;
14231da177e4SLinus Torvalds 	u16 blockbits;
14241da177e4SLinus Torvalds 	u16 defkeybits;
14251da177e4SLinus Torvalds };
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds struct xfrm_algo_comp_info {
14281da177e4SLinus Torvalds 	u16 threshold;
14291da177e4SLinus Torvalds };
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds struct xfrm_algo_desc {
14321da177e4SLinus Torvalds 	char *name;
143304ff1260SHerbert Xu 	char *compat;
14341da177e4SLinus Torvalds 	u8 available:1;
14357e50f84cSJussi Kivilinna 	u8 pfkey_supported:1;
14361da177e4SLinus Torvalds 	union {
14371a6509d9SHerbert Xu 		struct xfrm_algo_aead_info aead;
14381da177e4SLinus Torvalds 		struct xfrm_algo_auth_info auth;
14391da177e4SLinus Torvalds 		struct xfrm_algo_encr_info encr;
14401da177e4SLinus Torvalds 		struct xfrm_algo_comp_info comp;
14411da177e4SLinus Torvalds 	} uinfo;
14421da177e4SLinus Torvalds 	struct sadb_alg desc;
14431da177e4SLinus Torvalds };
14441da177e4SLinus Torvalds 
14453328715eSSteffen Klassert /* XFRM protocol handlers.  */
14463328715eSSteffen Klassert struct xfrm4_protocol {
14473328715eSSteffen Klassert 	int (*handler)(struct sk_buff *skb);
14483328715eSSteffen Klassert 	int (*input_handler)(struct sk_buff *skb, int nexthdr, __be32 spi,
14493328715eSSteffen Klassert 			     int encap_type);
14503328715eSSteffen Klassert 	int (*cb_handler)(struct sk_buff *skb, int err);
14513328715eSSteffen Klassert 	int (*err_handler)(struct sk_buff *skb, u32 info);
14523328715eSSteffen Klassert 
14533328715eSSteffen Klassert 	struct xfrm4_protocol __rcu *next;
14543328715eSSteffen Klassert 	int priority;
14553328715eSSteffen Klassert };
14563328715eSSteffen Klassert 
14577e14ea15SSteffen Klassert struct xfrm6_protocol {
14587e14ea15SSteffen Klassert 	int (*handler)(struct sk_buff *skb);
14597e14ea15SSteffen Klassert 	int (*cb_handler)(struct sk_buff *skb, int err);
14607e14ea15SSteffen Klassert 	int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
14617e14ea15SSteffen Klassert 			   u8 type, u8 code, int offset, __be32 info);
14627e14ea15SSteffen Klassert 
14637e14ea15SSteffen Klassert 	struct xfrm6_protocol __rcu *next;
14647e14ea15SSteffen Klassert 	int priority;
14657e14ea15SSteffen Klassert };
14667e14ea15SSteffen Klassert 
14671da177e4SLinus Torvalds /* XFRM tunnel handlers.  */
14681da177e4SLinus Torvalds struct xfrm_tunnel {
14691da177e4SLinus Torvalds 	int (*handler)(struct sk_buff *skb);
1470a6337463Sjamal 	int (*err_handler)(struct sk_buff *skb, u32 info);
1471d2acc347SHerbert Xu 
1472b33eab08SEric Dumazet 	struct xfrm_tunnel __rcu *next;
1473d2acc347SHerbert Xu 	int priority;
14741da177e4SLinus Torvalds };
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds struct xfrm6_tunnel {
1477d2acc347SHerbert Xu 	int (*handler)(struct sk_buff *skb);
1478d2acc347SHerbert Xu 	int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
1479d5fdd6baSBrian Haley 			   u8 type, u8 code, int offset, __be32 info);
14806f0bcf15SEric Dumazet 	struct xfrm6_tunnel __rcu *next;
1481d2acc347SHerbert Xu 	int priority;
14821da177e4SLinus Torvalds };
14831da177e4SLinus Torvalds 
1484d511337aSJoe Perches void xfrm_init(void);
1485d511337aSJoe Perches void xfrm4_init(void);
1486d511337aSJoe Perches int xfrm_state_init(struct net *net);
1487d511337aSJoe Perches void xfrm_state_fini(struct net *net);
1488d511337aSJoe Perches void xfrm4_state_init(void);
14892f32b51bSSteffen Klassert void xfrm4_protocol_init(void);
1490c35b7e72SDaniel Lezcano #ifdef CONFIG_XFRM
1491d511337aSJoe Perches int xfrm6_init(void);
1492d511337aSJoe Perches void xfrm6_fini(void);
1493d511337aSJoe Perches int xfrm6_state_init(void);
1494d511337aSJoe Perches void xfrm6_state_fini(void);
14957e14ea15SSteffen Klassert int xfrm6_protocol_init(void);
14967e14ea15SSteffen Klassert void xfrm6_protocol_fini(void);
1497c35b7e72SDaniel Lezcano #else
1498c35b7e72SDaniel Lezcano static inline int xfrm6_init(void)
1499c35b7e72SDaniel Lezcano {
1500c35b7e72SDaniel Lezcano 	return 0;
1501c35b7e72SDaniel Lezcano }
1502c35b7e72SDaniel Lezcano static inline void xfrm6_fini(void)
1503c35b7e72SDaniel Lezcano {
1504c35b7e72SDaniel Lezcano 	;
1505c35b7e72SDaniel Lezcano }
1506c35b7e72SDaniel Lezcano #endif
15071da177e4SLinus Torvalds 
1508558f82efSMasahide NAKAMURA #ifdef CONFIG_XFRM_STATISTICS
1509d511337aSJoe Perches int xfrm_proc_init(struct net *net);
1510d511337aSJoe Perches void xfrm_proc_fini(struct net *net);
1511558f82efSMasahide NAKAMURA #endif
1512558f82efSMasahide NAKAMURA 
1513d511337aSJoe Perches int xfrm_sysctl_init(struct net *net);
1514b27aeadbSAlexey Dobriyan #ifdef CONFIG_SYSCTL
1515d511337aSJoe Perches void xfrm_sysctl_fini(struct net *net);
1516b27aeadbSAlexey Dobriyan #else
1517b27aeadbSAlexey Dobriyan static inline void xfrm_sysctl_fini(struct net *net)
1518b27aeadbSAlexey Dobriyan {
1519b27aeadbSAlexey Dobriyan }
1520b27aeadbSAlexey Dobriyan #endif
1521b27aeadbSAlexey Dobriyan 
1522d3623099SNicolas Dichtel void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
1523870a2df4SNicolas Dichtel 			  struct xfrm_address_filter *filter);
1524d511337aSJoe Perches int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
15254c563f76STimo Teras 		    int (*func)(struct xfrm_state *, int, void*), void *);
1526283bc9f3SFan Du void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net);
1527d511337aSJoe Perches struct xfrm_state *xfrm_state_alloc(struct net *net);
1528d511337aSJoe Perches struct xfrm_state *xfrm_state_find(const xfrm_address_t *daddr,
152933765d06SDavid S. Miller 				   const xfrm_address_t *saddr,
1530b520e9f6SDavid S. Miller 				   const struct flowi *fl,
1531b520e9f6SDavid S. Miller 				   struct xfrm_tmpl *tmpl,
15321da177e4SLinus Torvalds 				   struct xfrm_policy *pol, int *err,
15331da177e4SLinus Torvalds 				   unsigned short family);
1534d511337aSJoe Perches struct xfrm_state *xfrm_stateonly_find(struct net *net, u32 mark,
15355447c5e4SAlexey Dobriyan 				       xfrm_address_t *daddr,
1536628529b6SJamal Hadi Salim 				       xfrm_address_t *saddr,
1537628529b6SJamal Hadi Salim 				       unsigned short family,
1538628529b6SJamal Hadi Salim 				       u8 mode, u8 proto, u32 reqid);
1539c454997eSFan Du struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1540c454997eSFan Du 					      unsigned short family);
1541d511337aSJoe Perches int xfrm_state_check_expire(struct xfrm_state *x);
1542d511337aSJoe Perches void xfrm_state_insert(struct xfrm_state *x);
1543d511337aSJoe Perches int xfrm_state_add(struct xfrm_state *x);
1544d511337aSJoe Perches int xfrm_state_update(struct xfrm_state *x);
1545d511337aSJoe Perches struct xfrm_state *xfrm_state_lookup(struct net *net, u32 mark,
1546a70486f0SDavid S. Miller 				     const xfrm_address_t *daddr, __be32 spi,
1547bd55775cSJamal Hadi Salim 				     u8 proto, unsigned short family);
1548d511337aSJoe Perches struct xfrm_state *xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1549a70486f0SDavid S. Miller 					    const xfrm_address_t *daddr,
1550a70486f0SDavid S. Miller 					    const xfrm_address_t *saddr,
1551bd55775cSJamal Hadi Salim 					    u8 proto,
1552bd55775cSJamal Hadi Salim 					    unsigned short family);
155341a49cc3SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1554d511337aSJoe Perches int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1555283bc9f3SFan Du 		   unsigned short family, struct net *net);
1556d511337aSJoe Perches int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1557d511337aSJoe Perches 		    unsigned short family);
155841a49cc3SMasahide NAKAMURA #else
155941a49cc3SMasahide NAKAMURA static inline int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src,
1560283bc9f3SFan Du 				 int n, unsigned short family, struct net *net)
156141a49cc3SMasahide NAKAMURA {
156241a49cc3SMasahide NAKAMURA 	return -ENOSYS;
156341a49cc3SMasahide NAKAMURA }
156441a49cc3SMasahide NAKAMURA 
156541a49cc3SMasahide NAKAMURA static inline int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src,
156641a49cc3SMasahide NAKAMURA 				  int n, unsigned short family)
156741a49cc3SMasahide NAKAMURA {
156841a49cc3SMasahide NAKAMURA 	return -ENOSYS;
156941a49cc3SMasahide NAKAMURA }
157041a49cc3SMasahide NAKAMURA #endif
1571af11e316SJamal Hadi Salim 
1572af11e316SJamal Hadi Salim struct xfrmk_sadinfo {
1573af11e316SJamal Hadi Salim 	u32 sadhcnt; /* current hash bkts */
1574af11e316SJamal Hadi Salim 	u32 sadhmcnt; /* max allowed hash bkts */
1575af11e316SJamal Hadi Salim 	u32 sadcnt; /* current running count */
1576af11e316SJamal Hadi Salim };
1577af11e316SJamal Hadi Salim 
15785a6d3416SJamal Hadi Salim struct xfrmk_spdinfo {
15795a6d3416SJamal Hadi Salim 	u32 incnt;
15805a6d3416SJamal Hadi Salim 	u32 outcnt;
15815a6d3416SJamal Hadi Salim 	u32 fwdcnt;
15825a6d3416SJamal Hadi Salim 	u32 inscnt;
15835a6d3416SJamal Hadi Salim 	u32 outscnt;
15845a6d3416SJamal Hadi Salim 	u32 fwdscnt;
15855a6d3416SJamal Hadi Salim 	u32 spdhcnt;
15865a6d3416SJamal Hadi Salim 	u32 spdhmcnt;
15875a6d3416SJamal Hadi Salim };
15885a6d3416SJamal Hadi Salim 
1589d511337aSJoe Perches struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
1590d511337aSJoe Perches int xfrm_state_delete(struct xfrm_state *x);
15912e71029eSTetsuo Handa int xfrm_state_flush(struct net *net, u8 proto, bool task_valid);
1592d77e38e6SSteffen Klassert int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid);
1593d511337aSJoe Perches void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si);
1594d511337aSJoe Perches void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si);
1595d511337aSJoe Perches u32 xfrm_replay_seqhi(struct xfrm_state *x, __be32 net_seq);
1596d511337aSJoe Perches int xfrm_init_replay(struct xfrm_state *x);
1597d511337aSJoe Perches int xfrm_state_mtu(struct xfrm_state *x, int mtu);
1598ffdb5211SIlan Tayari int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload);
1599d511337aSJoe Perches int xfrm_init_state(struct xfrm_state *x);
1600d511337aSJoe Perches int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb);
1601d511337aSJoe Perches int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type);
1602d511337aSJoe Perches int xfrm_input_resume(struct sk_buff *skb, int nexthdr);
1603d511337aSJoe Perches int xfrm_output_resume(struct sk_buff *skb, int err);
16047026b1ddSDavid Miller int xfrm_output(struct sock *sk, struct sk_buff *skb);
1605d511337aSJoe Perches int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb);
1606d511337aSJoe Perches void xfrm_local_error(struct sk_buff *skb, int mtu);
1607d511337aSJoe Perches int xfrm4_extract_header(struct sk_buff *skb);
1608d511337aSJoe Perches int xfrm4_extract_input(struct xfrm_state *x, struct sk_buff *skb);
1609d511337aSJoe Perches int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
1610716062fdSHerbert Xu 		    int encap_type);
1611d511337aSJoe Perches int xfrm4_transport_finish(struct sk_buff *skb, int async);
1612d511337aSJoe Perches int xfrm4_rcv(struct sk_buff *skb);
16131e295370SSteffen Klassert int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq);
1614c4541b41SHerbert Xu 
1615c4541b41SHerbert Xu static inline int xfrm4_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi)
1616c4541b41SHerbert Xu {
161770be6c91SSteffen Klassert 	XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
16183328715eSSteffen Klassert 	XFRM_SPI_SKB_CB(skb)->family = AF_INET;
16193328715eSSteffen Klassert 	XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
16203328715eSSteffen Klassert 	return xfrm_input(skb, nexthdr, spi, 0);
1621c4541b41SHerbert Xu }
1622c4541b41SHerbert Xu 
1623d511337aSJoe Perches int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb);
1624d511337aSJoe Perches int xfrm4_prepare_output(struct xfrm_state *x, struct sk_buff *skb);
1625ede2059dSEric W. Biederman int xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb);
16267026b1ddSDavid Miller int xfrm4_output_finish(struct sock *sk, struct sk_buff *skb);
16273328715eSSteffen Klassert int xfrm4_rcv_cb(struct sk_buff *skb, u8 protocol, int err);
16283328715eSSteffen Klassert int xfrm4_protocol_register(struct xfrm4_protocol *handler, unsigned char protocol);
16293328715eSSteffen Klassert int xfrm4_protocol_deregister(struct xfrm4_protocol *handler, unsigned char protocol);
1630d511337aSJoe Perches int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family);
1631d511337aSJoe Perches int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family);
1632d511337aSJoe Perches void xfrm4_local_error(struct sk_buff *skb, u32 mtu);
1633d511337aSJoe Perches int xfrm6_extract_header(struct sk_buff *skb);
1634d511337aSJoe Perches int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb);
163563c43787SNicolas Dichtel int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
163663c43787SNicolas Dichtel 		  struct ip6_tnl *t);
1637d511337aSJoe Perches int xfrm6_transport_finish(struct sk_buff *skb, int async);
163863c43787SNicolas Dichtel int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t);
1639d511337aSJoe Perches int xfrm6_rcv(struct sk_buff *skb);
1640d511337aSJoe Perches int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
1641fbd9a5b4SMasahide NAKAMURA 		     xfrm_address_t *saddr, u8 proto);
16427b77d161SDavid S. Miller void xfrm6_local_error(struct sk_buff *skb, u32 mtu);
16437e14ea15SSteffen Klassert int xfrm6_rcv_cb(struct sk_buff *skb, u8 protocol, int err);
16447e14ea15SSteffen Klassert int xfrm6_protocol_register(struct xfrm6_protocol *handler, unsigned char protocol);
16457e14ea15SSteffen Klassert int xfrm6_protocol_deregister(struct xfrm6_protocol *handler, unsigned char protocol);
1646d511337aSJoe Perches int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family);
16477b77d161SDavid S. Miller int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family);
1648d511337aSJoe Perches __be32 xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr);
1649d511337aSJoe Perches __be32 xfrm6_tunnel_spi_lookup(struct net *net, const xfrm_address_t *saddr);
1650d511337aSJoe Perches int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb);
1651d511337aSJoe Perches int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb);
1652ede2059dSEric W. Biederman int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb);
16537026b1ddSDavid Miller int xfrm6_output_finish(struct sock *sk, struct sk_buff *skb);
1654d511337aSJoe Perches int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb,
1655aee5adb4SMasahide NAKAMURA 			  u8 **prevhdr);
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds #ifdef CONFIG_XFRM
1658d511337aSJoe Perches int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb);
1659d511337aSJoe Perches int xfrm_user_policy(struct sock *sk, int optname,
1660d511337aSJoe Perches 		     u8 __user *optval, int optlen);
16611da177e4SLinus Torvalds #else
16621da177e4SLinus Torvalds static inline int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
16631da177e4SLinus Torvalds {
16641da177e4SLinus Torvalds  	return -ENOPROTOOPT;
16651da177e4SLinus Torvalds }
16661da177e4SLinus Torvalds 
1667067b207bSJames Chapman static inline int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
16681da177e4SLinus Torvalds {
16691da177e4SLinus Torvalds  	/* should not happen */
16701da177e4SLinus Torvalds  	kfree_skb(skb);
16711da177e4SLinus Torvalds 	return 0;
16721da177e4SLinus Torvalds }
16731da177e4SLinus Torvalds #endif
16741da177e4SLinus Torvalds 
1675d77e38e6SSteffen Klassert struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,
1676d77e38e6SSteffen Klassert 				    const xfrm_address_t *saddr,
1677d77e38e6SSteffen Klassert 				    const xfrm_address_t *daddr,
1678077fbac4SLorenzo Colitti 				    int family, u32 mark);
1679d77e38e6SSteffen Klassert 
16800331b1f3SAlexey Dobriyan struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp);
16814c563f76STimo Teras 
1682d511337aSJoe Perches void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type);
1683d511337aSJoe Perches int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
1684d511337aSJoe Perches 		     int (*func)(struct xfrm_policy *, int, int, void*),
1685d511337aSJoe Perches 		     void *);
1686283bc9f3SFan Du void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net);
16871da177e4SLinus Torvalds int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl);
16888ca2e93bSJamal Hadi Salim struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark,
16898ca2e93bSJamal Hadi Salim 					  u8 type, int dir,
16904e81bb83SMasahide NAKAMURA 					  struct xfrm_selector *sel,
1691ef41aaa0SEric Paris 					  struct xfrm_sec_ctx *ctx, int delete,
1692ef41aaa0SEric Paris 					  int *err);
1693d511337aSJoe Perches struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8, int dir,
1694d511337aSJoe Perches 				     u32 id, int delete, int *err);
16952e71029eSTetsuo Handa int xfrm_policy_flush(struct net *net, u8 type, bool task_valid);
1696880a6fabSChristophe Gouault void xfrm_policy_hash_rebuild(struct net *net);
16971da177e4SLinus Torvalds u32 xfrm_get_acqseq(void);
1698776e9dd9SFan Du int verify_spi_info(u8 proto, u32 min, u32 max);
1699d511337aSJoe Perches int xfrm_alloc_spi(struct xfrm_state *x, u32 minspi, u32 maxspi);
1700e473fcb4SMathias Krause struct xfrm_state *xfrm_find_acq(struct net *net, const struct xfrm_mark *mark,
1701bd55775cSJamal Hadi Salim 				 u8 mode, u32 reqid, u8 proto,
1702a70486f0SDavid S. Miller 				 const xfrm_address_t *daddr,
1703a70486f0SDavid S. Miller 				 const xfrm_address_t *saddr, int create,
1704bd55775cSJamal Hadi Salim 				 unsigned short family);
1705d511337aSJoe Perches int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol);
17061da177e4SLinus Torvalds 
170780c9abaaSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
1708d511337aSJoe Perches int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1709183cad12SDavid S. Miller 	       const struct xfrm_migrate *m, int num_bundles,
17108bafd730SAntony Antony 	       const struct xfrm_kmaddress *k,
17118bafd730SAntony Antony 	       const struct xfrm_encap_tmpl *encap);
1712283bc9f3SFan Du struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net);
1713d511337aSJoe Perches struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
17144ab47d47SAntony Antony 				      struct xfrm_migrate *m,
17154ab47d47SAntony Antony 				      struct xfrm_encap_tmpl *encap);
1716d511337aSJoe Perches int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
171713c1d189SArnaud Ebalard 		 struct xfrm_migrate *m, int num_bundles,
17184ab47d47SAntony Antony 		 struct xfrm_kmaddress *k, struct net *net,
17194ab47d47SAntony Antony 		 struct xfrm_encap_tmpl *encap);
172080c9abaaSShinta Sugimoto #endif
172180c9abaaSShinta Sugimoto 
1722d511337aSJoe Perches int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport);
1723d511337aSJoe Perches void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid);
1724d511337aSJoe Perches int km_report(struct net *net, u8 proto, struct xfrm_selector *sel,
1725d511337aSJoe Perches 	      xfrm_address_t *addr);
17261da177e4SLinus Torvalds 
1727d511337aSJoe Perches void xfrm_input_init(void);
1728d511337aSJoe Perches int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq);
17291da177e4SLinus Torvalds 
1730d511337aSJoe Perches void xfrm_probe_algs(void);
1731d511337aSJoe Perches int xfrm_count_pfkey_auth_supported(void);
1732d511337aSJoe Perches int xfrm_count_pfkey_enc_supported(void);
1733d511337aSJoe Perches struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx);
1734d511337aSJoe Perches struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx);
1735d511337aSJoe Perches struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id);
1736d511337aSJoe Perches struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id);
1737d511337aSJoe Perches struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id);
1738d511337aSJoe Perches struct xfrm_algo_desc *xfrm_aalg_get_byname(const char *name, int probe);
1739d511337aSJoe Perches struct xfrm_algo_desc *xfrm_ealg_get_byname(const char *name, int probe);
1740d511337aSJoe Perches struct xfrm_algo_desc *xfrm_calg_get_byname(const char *name, int probe);
1741d511337aSJoe Perches struct xfrm_algo_desc *xfrm_aead_get_byname(const char *name, int icv_len,
17421a6509d9SHerbert Xu 					    int probe);
17431da177e4SLinus Torvalds 
1744ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 static inline bool xfrm6_addr_equal(const xfrm_address_t *a,
1745ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 				    const xfrm_address_t *b)
1746ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 {
1747ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 	return ipv6_addr_equal((const struct in6_addr *)a,
1748ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 			       (const struct in6_addr *)b);
1749ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 }
1750ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 
175170e94e66SYOSHIFUJI Hideaki / 吉藤英明 static inline bool xfrm_addr_equal(const xfrm_address_t *a,
175270e94e66SYOSHIFUJI Hideaki / 吉藤英明 				   const xfrm_address_t *b,
175370e94e66SYOSHIFUJI Hideaki / 吉藤英明 				   sa_family_t family)
175470e94e66SYOSHIFUJI Hideaki / 吉藤英明 {
175570e94e66SYOSHIFUJI Hideaki / 吉藤英明 	switch (family) {
175670e94e66SYOSHIFUJI Hideaki / 吉藤英明 	default:
175770e94e66SYOSHIFUJI Hideaki / 吉藤英明 	case AF_INET:
175870e94e66SYOSHIFUJI Hideaki / 吉藤英明 		return ((__force u32)a->a4 ^ (__force u32)b->a4) == 0;
175970e94e66SYOSHIFUJI Hideaki / 吉藤英明 	case AF_INET6:
176070e94e66SYOSHIFUJI Hideaki / 吉藤英明 		return xfrm6_addr_equal(a, b);
176170e94e66SYOSHIFUJI Hideaki / 吉藤英明 	}
176270e94e66SYOSHIFUJI Hideaki / 吉藤英明 }
176370e94e66SYOSHIFUJI Hideaki / 吉藤英明 
176477d8d7a6SHerbert Xu static inline int xfrm_policy_id2dir(u32 index)
176577d8d7a6SHerbert Xu {
176677d8d7a6SHerbert Xu 	return index & 7;
176777d8d7a6SHerbert Xu }
176877d8d7a6SHerbert Xu 
1769a6483b79SAlexey Dobriyan #ifdef CONFIG_XFRM
1770a6483b79SAlexey Dobriyan static inline int xfrm_aevent_is_on(struct net *net)
1771f8cd5488SJamal Hadi Salim {
1772be33690dSPatrick McHardy 	struct sock *nlsk;
1773be33690dSPatrick McHardy 	int ret = 0;
1774be33690dSPatrick McHardy 
1775be33690dSPatrick McHardy 	rcu_read_lock();
1776a6483b79SAlexey Dobriyan 	nlsk = rcu_dereference(net->xfrm.nlsk);
1777be33690dSPatrick McHardy 	if (nlsk)
1778be33690dSPatrick McHardy 		ret = netlink_has_listeners(nlsk, XFRMNLGRP_AEVENTS);
1779be33690dSPatrick McHardy 	rcu_read_unlock();
1780be33690dSPatrick McHardy 	return ret;
1781f8cd5488SJamal Hadi Salim }
17820f24558eSHoria Geanta 
17830f24558eSHoria Geanta static inline int xfrm_acquire_is_on(struct net *net)
17840f24558eSHoria Geanta {
17850f24558eSHoria Geanta 	struct sock *nlsk;
17860f24558eSHoria Geanta 	int ret = 0;
17870f24558eSHoria Geanta 
17880f24558eSHoria Geanta 	rcu_read_lock();
17890f24558eSHoria Geanta 	nlsk = rcu_dereference(net->xfrm.nlsk);
17900f24558eSHoria Geanta 	if (nlsk)
17910f24558eSHoria Geanta 		ret = netlink_has_listeners(nlsk, XFRMNLGRP_ACQUIRE);
17920f24558eSHoria Geanta 	rcu_read_unlock();
17930f24558eSHoria Geanta 
17940f24558eSHoria Geanta 	return ret;
17950f24558eSHoria Geanta }
1796a6483b79SAlexey Dobriyan #endif
1797f8cd5488SJamal Hadi Salim 
1798373b8eebSAlexey Dobriyan static inline unsigned int aead_len(struct xfrm_algo_aead *alg)
1799ee5c2317SSteffen Klassert {
1800ee5c2317SSteffen Klassert 	return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
1801ee5c2317SSteffen Klassert }
1802ee5c2317SSteffen Klassert 
180306cd22f8SAlexey Dobriyan static inline unsigned int xfrm_alg_len(const struct xfrm_algo *alg)
18040f99be0dSEric Dumazet {
18050f99be0dSEric Dumazet 	return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
18060f99be0dSEric Dumazet }
18070f99be0dSEric Dumazet 
18081bd963a7SAlexey Dobriyan static inline unsigned int xfrm_alg_auth_len(const struct xfrm_algo_auth *alg)
18094447bb33SMartin Willi {
18104447bb33SMartin Willi 	return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
18114447bb33SMartin Willi }
18124447bb33SMartin Willi 
18135e708e47SAlexey Dobriyan static inline unsigned int xfrm_replay_state_esn_len(struct xfrm_replay_state_esn *replay_esn)
18149736acf3SSteffen Klassert {
18159736acf3SSteffen Klassert 	return sizeof(*replay_esn) + replay_esn->bmp_len * sizeof(__u32);
18169736acf3SSteffen Klassert }
18179736acf3SSteffen Klassert 
181880c9abaaSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
1819af2f464eSSteffen Klassert static inline int xfrm_replay_clone(struct xfrm_state *x,
1820af2f464eSSteffen Klassert 				     struct xfrm_state *orig)
1821af2f464eSSteffen Klassert {
1822af2f464eSSteffen Klassert 	x->replay_esn = kzalloc(xfrm_replay_state_esn_len(orig->replay_esn),
1823af2f464eSSteffen Klassert 				GFP_KERNEL);
1824af2f464eSSteffen Klassert 	if (!x->replay_esn)
1825af2f464eSSteffen Klassert 		return -ENOMEM;
1826af2f464eSSteffen Klassert 
1827af2f464eSSteffen Klassert 	x->replay_esn->bmp_len = orig->replay_esn->bmp_len;
1828af2f464eSSteffen Klassert 	x->replay_esn->replay_window = orig->replay_esn->replay_window;
1829af2f464eSSteffen Klassert 
1830af2f464eSSteffen Klassert 	x->preplay_esn = kmemdup(x->replay_esn,
1831af2f464eSSteffen Klassert 				 xfrm_replay_state_esn_len(x->replay_esn),
1832af2f464eSSteffen Klassert 				 GFP_KERNEL);
1833af2f464eSSteffen Klassert 	if (!x->preplay_esn) {
1834af2f464eSSteffen Klassert 		kfree(x->replay_esn);
1835af2f464eSSteffen Klassert 		return -ENOMEM;
1836af2f464eSSteffen Klassert 	}
1837af2f464eSSteffen Klassert 
1838af2f464eSSteffen Klassert 	return 0;
1839af2f464eSSteffen Klassert }
1840af2f464eSSteffen Klassert 
1841ee5c2317SSteffen Klassert static inline struct xfrm_algo_aead *xfrm_algo_aead_clone(struct xfrm_algo_aead *orig)
1842ee5c2317SSteffen Klassert {
1843ee5c2317SSteffen Klassert 	return kmemdup(orig, aead_len(orig), GFP_KERNEL);
1844ee5c2317SSteffen Klassert }
1845ee5c2317SSteffen Klassert 
1846ee5c2317SSteffen Klassert 
184780c9abaaSShinta Sugimoto static inline struct xfrm_algo *xfrm_algo_clone(struct xfrm_algo *orig)
184880c9abaaSShinta Sugimoto {
18490f99be0dSEric Dumazet 	return kmemdup(orig, xfrm_alg_len(orig), GFP_KERNEL);
185080c9abaaSShinta Sugimoto }
185180c9abaaSShinta Sugimoto 
18524447bb33SMartin Willi static inline struct xfrm_algo_auth *xfrm_algo_auth_clone(struct xfrm_algo_auth *orig)
18534447bb33SMartin Willi {
18544447bb33SMartin Willi 	return kmemdup(orig, xfrm_alg_auth_len(orig), GFP_KERNEL);
18554447bb33SMartin Willi }
18564447bb33SMartin Willi 
185780c9abaaSShinta Sugimoto static inline void xfrm_states_put(struct xfrm_state **states, int n)
185880c9abaaSShinta Sugimoto {
185980c9abaaSShinta Sugimoto 	int i;
186080c9abaaSShinta Sugimoto 	for (i = 0; i < n; i++)
186180c9abaaSShinta Sugimoto 		xfrm_state_put(*(states + i));
186280c9abaaSShinta Sugimoto }
186380c9abaaSShinta Sugimoto 
186480c9abaaSShinta Sugimoto static inline void xfrm_states_delete(struct xfrm_state **states, int n)
186580c9abaaSShinta Sugimoto {
186680c9abaaSShinta Sugimoto 	int i;
186780c9abaaSShinta Sugimoto 	for (i = 0; i < n; i++)
186880c9abaaSShinta Sugimoto 		xfrm_state_delete(*(states + i));
186980c9abaaSShinta Sugimoto }
187080c9abaaSShinta Sugimoto #endif
1871f8cd5488SJamal Hadi Salim 
1872def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
187300501121SHerbert Xu static inline struct xfrm_state *xfrm_input_state(struct sk_buff *skb)
187400501121SHerbert Xu {
187500501121SHerbert Xu 	return skb->sp->xvec[skb->sp->len - 1];
187600501121SHerbert Xu }
187754ef207aSSteffen Klassert static inline struct xfrm_offload *xfrm_offload(struct sk_buff *skb)
187854ef207aSSteffen Klassert {
187954ef207aSSteffen Klassert 	struct sec_path *sp = skb->sp;
188054ef207aSSteffen Klassert 
188154ef207aSSteffen Klassert 	if (!sp || !sp->olen || sp->len != sp->olen)
188254ef207aSSteffen Klassert 		return NULL;
188354ef207aSSteffen Klassert 
188454ef207aSSteffen Klassert 	return &sp->ovec[sp->olen - 1];
188554ef207aSSteffen Klassert }
1886def8b4faSAlexey Dobriyan #endif
188700501121SHerbert Xu 
1888d77e38e6SSteffen Klassert void __net_init xfrm_dev_init(void);
1889b81f884aSHangbin Liu 
1890b81f884aSHangbin Liu #ifdef CONFIG_XFRM_OFFLOAD
18913dca3f38SSteffen Klassert struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features);
1892d77e38e6SSteffen Klassert int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
1893d77e38e6SSteffen Klassert 		       struct xfrm_user_offload *xuo);
1894d77e38e6SSteffen Klassert bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x);
1895d77e38e6SSteffen Klassert 
1896f70f250aSSteffen Klassert static inline bool xfrm_dst_offload_ok(struct dst_entry *dst)
1897f70f250aSSteffen Klassert {
1898f70f250aSSteffen Klassert 	struct xfrm_state *x = dst->xfrm;
1899b6ca8bd5SDavid Miller 	struct xfrm_dst *xdst;
1900f70f250aSSteffen Klassert 
1901f70f250aSSteffen Klassert 	if (!x || !x->type_offload)
1902f70f250aSSteffen Klassert 		return false;
1903f70f250aSSteffen Klassert 
1904b6ca8bd5SDavid Miller 	xdst = (struct xfrm_dst *) dst;
19050f6c480fSDavid Miller 	if (x->xso.offload_handle && (x->xso.dev == xfrm_dst_path(dst)->dev) &&
1906b6ca8bd5SDavid Miller 	    !xdst->child->xfrm)
1907f70f250aSSteffen Klassert 		return true;
1908f70f250aSSteffen Klassert 
1909f70f250aSSteffen Klassert 	return false;
1910f70f250aSSteffen Klassert }
1911f70f250aSSteffen Klassert 
1912d77e38e6SSteffen Klassert static inline void xfrm_dev_state_delete(struct xfrm_state *x)
1913d77e38e6SSteffen Klassert {
1914d77e38e6SSteffen Klassert 	struct xfrm_state_offload *xso = &x->xso;
1915d77e38e6SSteffen Klassert 
1916d77e38e6SSteffen Klassert 	if (xso->dev)
1917d77e38e6SSteffen Klassert 		xso->dev->xfrmdev_ops->xdo_dev_state_delete(x);
1918d77e38e6SSteffen Klassert }
1919d77e38e6SSteffen Klassert 
1920d77e38e6SSteffen Klassert static inline void xfrm_dev_state_free(struct xfrm_state *x)
1921d77e38e6SSteffen Klassert {
1922d77e38e6SSteffen Klassert 	struct xfrm_state_offload *xso = &x->xso;
1923d77e38e6SSteffen Klassert 	 struct net_device *dev = xso->dev;
1924d77e38e6SSteffen Klassert 
1925d77e38e6SSteffen Klassert 	if (dev && dev->xfrmdev_ops) {
1926d77e38e6SSteffen Klassert 		dev->xfrmdev_ops->xdo_dev_state_free(x);
1927d77e38e6SSteffen Klassert 		xso->dev = NULL;
1928d77e38e6SSteffen Klassert 		dev_put(dev);
1929d77e38e6SSteffen Klassert 	}
1930d77e38e6SSteffen Klassert }
1931d77e38e6SSteffen Klassert #else
19323dca3f38SSteffen Klassert static inline struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features)
1933f6e27114SSteffen Klassert {
19343dca3f38SSteffen Klassert 	return skb;
1935f6e27114SSteffen Klassert }
1936f6e27114SSteffen Klassert 
1937d77e38e6SSteffen Klassert static inline int xfrm_dev_state_add(struct net *net, struct xfrm_state *x, struct xfrm_user_offload *xuo)
1938d77e38e6SSteffen Klassert {
1939d77e38e6SSteffen Klassert 	return 0;
1940d77e38e6SSteffen Klassert }
1941d77e38e6SSteffen Klassert 
1942d77e38e6SSteffen Klassert static inline void xfrm_dev_state_delete(struct xfrm_state *x)
1943d77e38e6SSteffen Klassert {
1944d77e38e6SSteffen Klassert }
1945d77e38e6SSteffen Klassert 
1946d77e38e6SSteffen Klassert static inline void xfrm_dev_state_free(struct xfrm_state *x)
1947d77e38e6SSteffen Klassert {
1948d77e38e6SSteffen Klassert }
1949d77e38e6SSteffen Klassert 
1950d77e38e6SSteffen Klassert static inline bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
1951d77e38e6SSteffen Klassert {
1952d77e38e6SSteffen Klassert 	return false;
1953d77e38e6SSteffen Klassert }
1954f70f250aSSteffen Klassert 
1955f70f250aSSteffen Klassert static inline bool xfrm_dst_offload_ok(struct dst_entry *dst)
1956f70f250aSSteffen Klassert {
1957f70f250aSSteffen Klassert 	return false;
1958f70f250aSSteffen Klassert }
1959d77e38e6SSteffen Klassert #endif
1960d77e38e6SSteffen Klassert 
1961bf825f81SJamal Hadi Salim static inline int xfrm_mark_get(struct nlattr **attrs, struct xfrm_mark *m)
1962bf825f81SJamal Hadi Salim {
1963bf825f81SJamal Hadi Salim 	if (attrs[XFRMA_MARK])
19644efd7e83SAndreas Steffen 		memcpy(m, nla_data(attrs[XFRMA_MARK]), sizeof(struct xfrm_mark));
1965bf825f81SJamal Hadi Salim 	else
1966bf825f81SJamal Hadi Salim 		m->v = m->m = 0;
1967bf825f81SJamal Hadi Salim 
1968bf825f81SJamal Hadi Salim 	return m->v & m->m;
1969bf825f81SJamal Hadi Salim }
1970bf825f81SJamal Hadi Salim 
1971e3dfa389SDavid S. Miller static inline int xfrm_mark_put(struct sk_buff *skb, const struct xfrm_mark *m)
1972bf825f81SJamal Hadi Salim {
19731d1e34ddSDavid S. Miller 	int ret = 0;
1974bf825f81SJamal Hadi Salim 
19751d1e34ddSDavid S. Miller 	if (m->m | m->v)
19761d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_MARK, sizeof(struct xfrm_mark), m);
19771d1e34ddSDavid S. Miller 	return ret;
1978bf825f81SJamal Hadi Salim }
1979bf825f81SJamal Hadi Salim 
198070be6c91SSteffen Klassert static inline int xfrm_tunnel_check(struct sk_buff *skb, struct xfrm_state *x,
198170be6c91SSteffen Klassert 				    unsigned int family)
198270be6c91SSteffen Klassert {
198370be6c91SSteffen Klassert 	bool tunnel = false;
198470be6c91SSteffen Klassert 
198570be6c91SSteffen Klassert 	switch(family) {
198670be6c91SSteffen Klassert 	case AF_INET:
198770be6c91SSteffen Klassert 		if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
198870be6c91SSteffen Klassert 			tunnel = true;
198970be6c91SSteffen Klassert 		break;
199070be6c91SSteffen Klassert 	case AF_INET6:
199170be6c91SSteffen Klassert 		if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
199270be6c91SSteffen Klassert 			tunnel = true;
199370be6c91SSteffen Klassert 		break;
199470be6c91SSteffen Klassert 	}
199570be6c91SSteffen Klassert 	if (tunnel && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL))
199670be6c91SSteffen Klassert 		return -EINVAL;
199770be6c91SSteffen Klassert 
199870be6c91SSteffen Klassert 	return 0;
199970be6c91SSteffen Klassert }
20001da177e4SLinus Torvalds #endif	/* _NET_XFRM_H */
2001