xref: /openbmc/linux/include/net/xfrm.h (revision c35fe410)
11da177e4SLinus Torvalds #ifndef _NET_XFRM_H
21da177e4SLinus Torvalds #define _NET_XFRM_H
31da177e4SLinus Torvalds 
4aabc9761SHerbert Xu #include <linux/compiler.h>
51da177e4SLinus Torvalds #include <linux/xfrm.h>
61da177e4SLinus Torvalds #include <linux/spinlock.h>
71da177e4SLinus Torvalds #include <linux/list.h>
81da177e4SLinus Torvalds #include <linux/skbuff.h>
914c85021SArnaldo Carvalho de Melo #include <linux/socket.h>
101da177e4SLinus Torvalds #include <linux/pfkeyv2.h>
115794708fSMasahide NAKAMURA #include <linux/ipsec.h>
121da177e4SLinus Torvalds #include <linux/in6.h>
134a3e2f71SArjan van de Ven #include <linux/mutex.h>
14ab5f5e8bSJoy Latten #include <linux/audit.h>
155a0e3ad6STejun Heo #include <linux/slab.h>
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <net/sock.h>
181da177e4SLinus Torvalds #include <net/dst.h>
19436a0a40SHerbert Xu #include <net/ip.h>
201da177e4SLinus Torvalds #include <net/route.h>
211da177e4SLinus Torvalds #include <net/ipv6.h>
221da177e4SLinus Torvalds #include <net/ip6_fib.h>
23fe1a5f03STimo Teräs #include <net/flow.h>
249e0d57fdSYury Polyanskiy 
259e0d57fdSYury Polyanskiy #include <linux/interrupt.h>
269e0d57fdSYury Polyanskiy 
27558f82efSMasahide NAKAMURA #ifdef CONFIG_XFRM_STATISTICS
28558f82efSMasahide NAKAMURA #include <net/snmp.h>
29558f82efSMasahide NAKAMURA #endif
301da177e4SLinus Torvalds 
31d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_ESP		50
32d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_AH		51
33d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_COMP		108
34d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_IPIP		4
35d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_IPV6		41
36d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_ROUTING	IPPROTO_ROUTING
37d3d6dd3aSMasahide NAKAMURA #define XFRM_PROTO_DSTOPTS	IPPROTO_DSTOPTS
38d3d6dd3aSMasahide NAKAMURA 
39fa9921e4SNicolas Dichtel #define XFRM_ALIGN4(len)	(((len) + 3) & ~3)
401da177e4SLinus Torvalds #define XFRM_ALIGN8(len)	(((len) + 7) & ~7)
41b59f45d0SHerbert Xu #define MODULE_ALIAS_XFRM_MODE(family, encap) \
42b59f45d0SHerbert Xu 	MODULE_ALIAS("xfrm-mode-" __stringify(family) "-" __stringify(encap))
43d3d6dd3aSMasahide NAKAMURA #define MODULE_ALIAS_XFRM_TYPE(family, proto) \
44d3d6dd3aSMasahide NAKAMURA 	MODULE_ALIAS("xfrm-type-" __stringify(family) "-" __stringify(proto))
451da177e4SLinus Torvalds 
46558f82efSMasahide NAKAMURA #ifdef CONFIG_XFRM_STATISTICS
4759c9940eSAlexey Dobriyan #define XFRM_INC_STATS(net, field)	SNMP_INC_STATS((net)->mib.xfrm_statistics, field)
48558f82efSMasahide NAKAMURA #else
4959c9940eSAlexey Dobriyan #define XFRM_INC_STATS(net, field)	((void)(net))
50558f82efSMasahide NAKAMURA #endif
51558f82efSMasahide NAKAMURA 
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /* Organization of SPD aka "XFRM rules"
541da177e4SLinus Torvalds    ------------------------------------
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds    Basic objects:
571da177e4SLinus Torvalds    - policy rule, struct xfrm_policy (=SPD entry)
581da177e4SLinus Torvalds    - bundle of transformations, struct dst_entry == struct xfrm_dst (=SA bundle)
591da177e4SLinus Torvalds    - instance of a transformer, struct xfrm_state (=SA)
601da177e4SLinus Torvalds    - template to clone xfrm_state, struct xfrm_tmpl
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds    SPD is plain linear list of xfrm_policy rules, ordered by priority.
631da177e4SLinus Torvalds    (To be compatible with existing pfkeyv2 implementations,
641da177e4SLinus Torvalds    many rules with priority of 0x7fffffff are allowed to exist and
651da177e4SLinus Torvalds    such rules are ordered in an unpredictable way, thanks to bsd folks.)
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds    Lookup is plain linear search until the first match with selector.
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds    If "action" is "block", then we prohibit the flow, otherwise:
701da177e4SLinus Torvalds    if "xfrms_nr" is zero, the flow passes untransformed. Otherwise,
711da177e4SLinus Torvalds    policy entry has list of up to XFRM_MAX_DEPTH transformations,
721da177e4SLinus Torvalds    described by templates xfrm_tmpl. Each template is resolved
731da177e4SLinus Torvalds    to a complete xfrm_state (see below) and we pack bundle of transformations
741da177e4SLinus Torvalds    to a dst_entry returned to requestor.
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds    dst -. xfrm  .-> xfrm_state #1
771da177e4SLinus Torvalds     |---. child .-> dst -. xfrm .-> xfrm_state #2
781da177e4SLinus Torvalds                      |---. child .-> dst -. xfrm .-> xfrm_state #3
791da177e4SLinus Torvalds                                       |---. child .-> NULL
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds    Bundles are cached at xrfm_policy struct (field ->bundles).
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds    Resolution of xrfm_tmpl
851da177e4SLinus Torvalds    -----------------------
861da177e4SLinus Torvalds    Template contains:
871da177e4SLinus Torvalds    1. ->mode		Mode: transport or tunnel
881da177e4SLinus Torvalds    2. ->id.proto	Protocol: AH/ESP/IPCOMP
891da177e4SLinus Torvalds    3. ->id.daddr	Remote tunnel endpoint, ignored for transport mode.
901da177e4SLinus Torvalds       Q: allow to resolve security gateway?
911da177e4SLinus Torvalds    4. ->id.spi          If not zero, static SPI.
921da177e4SLinus Torvalds    5. ->saddr		Local tunnel endpoint, ignored for transport mode.
931da177e4SLinus Torvalds    6. ->algos		List of allowed algos. Plain bitmask now.
941da177e4SLinus Torvalds       Q: ealgos, aalgos, calgos. What a mess...
951da177e4SLinus Torvalds    7. ->share		Sharing mode.
961da177e4SLinus Torvalds       Q: how to implement private sharing mode? To add struct sock* to
971da177e4SLinus Torvalds       flow id?
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds    Having this template we search through SAD searching for entries
1001da177e4SLinus Torvalds    with appropriate mode/proto/algo, permitted by selector.
1011da177e4SLinus Torvalds    If no appropriate entry found, it is requested from key manager.
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds    PROBLEMS:
1041da177e4SLinus Torvalds    Q: How to find all the bundles referring to a physical path for
1051da177e4SLinus Torvalds       PMTU discovery? Seems, dst should contain list of all parents...
1061da177e4SLinus Torvalds       and enter to infinite locking hierarchy disaster.
1071da177e4SLinus Torvalds       No! It is easier, we will not search for them, let them find us.
1081da177e4SLinus Torvalds       We add genid to each dst plus pointer to genid of raw IP route,
1091da177e4SLinus Torvalds       pmtu disc will update pmtu on raw IP route and increase its genid.
1101da177e4SLinus Torvalds       dst_check() will see this for top level and trigger resyncing
1111da177e4SLinus Torvalds       metrics. Plus, it will be made via sk->sk_dst_cache. Solved.
1121da177e4SLinus Torvalds  */
1131da177e4SLinus Torvalds 
11412a169e7SHerbert Xu struct xfrm_state_walk {
11512a169e7SHerbert Xu 	struct list_head	all;
11612a169e7SHerbert Xu 	u8			state;
11712a169e7SHerbert Xu 	u8			dying;
11812a169e7SHerbert Xu 	u8			proto;
11912a169e7SHerbert Xu 	u32			seq;
120870a2df4SNicolas Dichtel 	struct xfrm_address_filter *filter;
12112a169e7SHerbert Xu };
12212a169e7SHerbert Xu 
1231da177e4SLinus Torvalds /* Full description of state of transformer. */
124fd2c3ef7SEric Dumazet struct xfrm_state {
1250c5c9fb5SEric W. Biederman 	possible_net_t		xs_net;
126abb81c4fSHerbert Xu 	union {
12712a169e7SHerbert Xu 		struct hlist_node	gclist;
1288f126e37SDavid S. Miller 		struct hlist_node	bydst;
129abb81c4fSHerbert Xu 	};
1308f126e37SDavid S. Miller 	struct hlist_node	bysrc;
1318f126e37SDavid S. Miller 	struct hlist_node	byspi;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	atomic_t		refcnt;
1341da177e4SLinus Torvalds 	spinlock_t		lock;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	struct xfrm_id		id;
1371da177e4SLinus Torvalds 	struct xfrm_selector	sel;
138bf825f81SJamal Hadi Salim 	struct xfrm_mark	mark;
13935d2856bSMartin Willi 	u32			tfcpad;
1401da177e4SLinus Torvalds 
1419d4a706dSDavid S. Miller 	u32			genid;
1429d4a706dSDavid S. Miller 
14312a169e7SHerbert Xu 	/* Key manager bits */
14412a169e7SHerbert Xu 	struct xfrm_state_walk	km;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	/* Parameters of this state. */
1471da177e4SLinus Torvalds 	struct {
1481da177e4SLinus Torvalds 		u32		reqid;
1491da177e4SLinus Torvalds 		u8		mode;
1501da177e4SLinus Torvalds 		u8		replay_window;
1511da177e4SLinus Torvalds 		u8		aalgo, ealgo, calgo;
1521da177e4SLinus Torvalds 		u8		flags;
1531da177e4SLinus Torvalds 		u16		family;
1541da177e4SLinus Torvalds 		xfrm_address_t	saddr;
1551da177e4SLinus Torvalds 		int		header_len;
1561da177e4SLinus Torvalds 		int		trailer_len;
157a947b0a9SNicolas Dichtel 		u32		extra_flags;
1581da177e4SLinus Torvalds 	} props;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	struct xfrm_lifetime_cfg lft;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	/* Data for transformer */
1634447bb33SMartin Willi 	struct xfrm_algo_auth	*aalg;
1641da177e4SLinus Torvalds 	struct xfrm_algo	*ealg;
1651da177e4SLinus Torvalds 	struct xfrm_algo	*calg;
1661a6509d9SHerbert Xu 	struct xfrm_algo_aead	*aead;
16769b0137fSHerbert Xu 	const char		*geniv;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	/* Data for encapsulator */
1701da177e4SLinus Torvalds 	struct xfrm_encap_tmpl	*encap;
1711da177e4SLinus Torvalds 
172060f02a3SNoriaki TAKAMIYA 	/* Data for care-of address */
173060f02a3SNoriaki TAKAMIYA 	xfrm_address_t	*coaddr;
174060f02a3SNoriaki TAKAMIYA 
1751da177e4SLinus Torvalds 	/* IPComp needs an IPIP tunnel for handling uncompressed packets */
1761da177e4SLinus Torvalds 	struct xfrm_state	*tunnel;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	/* If a tunnel, number of users + 1 */
1791da177e4SLinus Torvalds 	atomic_t		tunnel_users;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	/* State for replay detection */
1821da177e4SLinus Torvalds 	struct xfrm_replay_state replay;
1839736acf3SSteffen Klassert 	struct xfrm_replay_state_esn *replay_esn;
1841da177e4SLinus Torvalds 
185f8cd5488SJamal Hadi Salim 	/* Replay detection state at the time we sent the last notification */
186f8cd5488SJamal Hadi Salim 	struct xfrm_replay_state preplay;
1879736acf3SSteffen Klassert 	struct xfrm_replay_state_esn *preplay_esn;
188f8cd5488SJamal Hadi Salim 
1899fdc4883SSteffen Klassert 	/* The functions for replay detection. */
190e45a8a9eSJulia Lawall 	const struct xfrm_replay *repl;
1919fdc4883SSteffen Klassert 
1922717096aSJamal Hadi Salim 	/* internal flag that only holds state for delayed aevent at the
1932717096aSJamal Hadi Salim 	 * moment
1942717096aSJamal Hadi Salim 	*/
1952717096aSJamal Hadi Salim 	u32			xflags;
1962717096aSJamal Hadi Salim 
197f8cd5488SJamal Hadi Salim 	/* Replay detection notification settings */
198f8cd5488SJamal Hadi Salim 	u32			replay_maxage;
199f8cd5488SJamal Hadi Salim 	u32			replay_maxdiff;
200f8cd5488SJamal Hadi Salim 
201f8cd5488SJamal Hadi Salim 	/* Replay detection notification timer */
202f8cd5488SJamal Hadi Salim 	struct timer_list	rtimer;
203f8cd5488SJamal Hadi Salim 
2041da177e4SLinus Torvalds 	/* Statistics */
2051da177e4SLinus Torvalds 	struct xfrm_stats	stats;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	struct xfrm_lifetime_cur curlft;
2089e0d57fdSYury Polyanskiy 	struct tasklet_hrtimer	mtimer;
2091da177e4SLinus Torvalds 
210e3c0d047SFan Du 	/* used to fix curlft->add_time when changing date */
211e3c0d047SFan Du 	long		saved_tmo;
212e3c0d047SFan Du 
2139afaca05SMasahide NAKAMURA 	/* Last used time */
214d26f3984SHerbert Xu 	unsigned long		lastused;
2159afaca05SMasahide NAKAMURA 
216cac2661cSSteffen Klassert 	struct page_frag xfrag;
217cac2661cSSteffen Klassert 
2181da177e4SLinus Torvalds 	/* Reference to data common to all the instances of this
2191da177e4SLinus Torvalds 	 * transformer. */
220533cb5b0SEric Dumazet 	const struct xfrm_type	*type;
22113996378SHerbert Xu 	struct xfrm_mode	*inner_mode;
222df9dcb45SKazunori MIYAZAWA 	struct xfrm_mode	*inner_mode_iaf;
22313996378SHerbert Xu 	struct xfrm_mode	*outer_mode;
2241da177e4SLinus Torvalds 
2259d389d7fSSteffen Klassert 	const struct xfrm_type_offload	*type_offload;
2269d389d7fSSteffen Klassert 
227df71837dSTrent Jaeger 	/* Security context */
228df71837dSTrent Jaeger 	struct xfrm_sec_ctx	*security;
229df71837dSTrent Jaeger 
2301da177e4SLinus Torvalds 	/* Private data of this transformer, format is opaque,
2311da177e4SLinus Torvalds 	 * interpreted by xfrm_type methods. */
2321da177e4SLinus Torvalds 	void			*data;
2331da177e4SLinus Torvalds };
2341da177e4SLinus Torvalds 
235673c09beSAlexey Dobriyan static inline struct net *xs_net(struct xfrm_state *x)
236673c09beSAlexey Dobriyan {
237673c09beSAlexey Dobriyan 	return read_pnet(&x->xs_net);
238673c09beSAlexey Dobriyan }
239673c09beSAlexey Dobriyan 
2402717096aSJamal Hadi Salim /* xflags - make enum if more show up */
2412717096aSJamal Hadi Salim #define XFRM_TIME_DEFER	1
242e3c0d047SFan Du #define XFRM_SOFT_EXPIRE 2
2432717096aSJamal Hadi Salim 
2441da177e4SLinus Torvalds enum {
2451da177e4SLinus Torvalds 	XFRM_STATE_VOID,
2461da177e4SLinus Torvalds 	XFRM_STATE_ACQ,
2471da177e4SLinus Torvalds 	XFRM_STATE_VALID,
2481da177e4SLinus Torvalds 	XFRM_STATE_ERROR,
2491da177e4SLinus Torvalds 	XFRM_STATE_EXPIRED,
2501da177e4SLinus Torvalds 	XFRM_STATE_DEAD
2511da177e4SLinus Torvalds };
2521da177e4SLinus Torvalds 
25326b15dadSJamal Hadi Salim /* callback structure passed from either netlink or pfkey */
254fd2c3ef7SEric Dumazet struct km_event {
255bf08867fSHerbert Xu 	union {
256bf08867fSHerbert Xu 		u32 hard;
257bf08867fSHerbert Xu 		u32 proto;
258bf08867fSHerbert Xu 		u32 byid;
259f8cd5488SJamal Hadi Salim 		u32 aevent;
260f7b6983fSMasahide NAKAMURA 		u32 type;
261bf08867fSHerbert Xu 	} data;
262bf08867fSHerbert Xu 
26326b15dadSJamal Hadi Salim 	u32	seq;
26415e47304SEric W. Biederman 	u32	portid;
26526b15dadSJamal Hadi Salim 	u32	event;
2667067802eSAlexey Dobriyan 	struct net *net;
26726b15dadSJamal Hadi Salim };
26826b15dadSJamal Hadi Salim 
2699fdc4883SSteffen Klassert struct xfrm_replay {
2709fdc4883SSteffen Klassert 	void	(*advance)(struct xfrm_state *x, __be32 net_seq);
2719fdc4883SSteffen Klassert 	int	(*check)(struct xfrm_state *x,
2729fdc4883SSteffen Klassert 			 struct sk_buff *skb,
2739fdc4883SSteffen Klassert 			 __be32 net_seq);
2743b59df46SSteffen Klassert 	int	(*recheck)(struct xfrm_state *x,
2753b59df46SSteffen Klassert 			   struct sk_buff *skb,
2763b59df46SSteffen Klassert 			   __be32 net_seq);
2779fdc4883SSteffen Klassert 	void	(*notify)(struct xfrm_state *x, int event);
2789fdc4883SSteffen Klassert 	int	(*overflow)(struct xfrm_state *x, struct sk_buff *skb);
2799fdc4883SSteffen Klassert };
2809fdc4883SSteffen Klassert 
28125ee3286SHerbert Xu struct net_device;
2821da177e4SLinus Torvalds struct xfrm_type;
2831da177e4SLinus Torvalds struct xfrm_dst;
2841da177e4SLinus Torvalds struct xfrm_policy_afinfo {
2851da177e4SLinus Torvalds 	struct dst_ops		*dst_ops;
28642a7b32bSDavid Ahern 	struct dst_entry	*(*dst_lookup)(struct net *net,
28742a7b32bSDavid Ahern 					       int tos, int oif,
2885e6b930fSDavid S. Miller 					       const xfrm_address_t *saddr,
2895e6b930fSDavid S. Miller 					       const xfrm_address_t *daddr);
29042a7b32bSDavid Ahern 	int			(*get_saddr)(struct net *net, int oif,
29142a7b32bSDavid Ahern 					     xfrm_address_t *saddr,
29242a7b32bSDavid Ahern 					     xfrm_address_t *daddr);
2931da177e4SLinus Torvalds 	void			(*decode_session)(struct sk_buff *skb,
294d5422efeSHerbert Xu 						  struct flowi *fl,
295d5422efeSHerbert Xu 						  int reverse);
29605d84025SDavid S. Miller 	int			(*get_tos)(const struct flowi *fl);
297a1b05140SMasahide NAKAMURA 	int			(*init_path)(struct xfrm_dst *path,
298a1b05140SMasahide NAKAMURA 					     struct dst_entry *dst,
299a1b05140SMasahide NAKAMURA 					     int nfheader_len);
30025ee3286SHerbert Xu 	int			(*fill_dst)(struct xfrm_dst *xdst,
30187c1e12bSHerbert Xu 					    struct net_device *dev,
3020c7b3eefSDavid S. Miller 					    const struct flowi *fl);
3032774c131SDavid S. Miller 	struct dst_entry	*(*blackhole_route)(struct net *net, struct dst_entry *orig);
3041da177e4SLinus Torvalds };
3051da177e4SLinus Torvalds 
306a2817d8bSFlorian Westphal int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family);
307a2817d8bSFlorian Westphal void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo);
308d511337aSJoe Perches void km_policy_notify(struct xfrm_policy *xp, int dir,
309d511337aSJoe Perches 		      const struct km_event *c);
310d511337aSJoe Perches void km_state_notify(struct xfrm_state *x, const struct km_event *c);
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds struct xfrm_tmpl;
313d511337aSJoe Perches int km_query(struct xfrm_state *x, struct xfrm_tmpl *t,
314d511337aSJoe Perches 	     struct xfrm_policy *pol);
315d511337aSJoe Perches void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
316d511337aSJoe Perches int __xfrm_state_delete(struct xfrm_state *x);
31753bc6b4dSJamal Hadi Salim 
3181da177e4SLinus Torvalds struct xfrm_state_afinfo {
31917c2a42aSHerbert Xu 	unsigned int			family;
32036cf9acfSHerbert Xu 	unsigned int			proto;
3218e3d716cSAl Viro 	__be16				eth_proto;
32217c2a42aSHerbert Xu 	struct module			*owner;
323533cb5b0SEric Dumazet 	const struct xfrm_type		*type_map[IPPROTO_MAX];
3249d389d7fSSteffen Klassert 	const struct xfrm_type_offload	*type_offload_map[IPPROTO_MAX];
325aa5d62ccSHerbert Xu 	struct xfrm_mode		*mode_map[XFRM_MODE_MAX];
3269d389d7fSSteffen Klassert 
327d094cd83SHerbert Xu 	int			(*init_flags)(struct xfrm_state *x);
32873e5ebb2SDavid S. Miller 	void			(*init_tempsel)(struct xfrm_selector *sel,
32973e5ebb2SDavid S. Miller 						const struct flowi *fl);
33019bd6244SDavid S. Miller 	void			(*init_temprop)(struct xfrm_state *x,
33119bd6244SDavid S. Miller 						const struct xfrm_tmpl *tmpl,
33219bd6244SDavid S. Miller 						const xfrm_address_t *daddr,
33319bd6244SDavid S. Miller 						const xfrm_address_t *saddr);
33441a49cc3SMasahide NAKAMURA 	int			(*tmpl_sort)(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n);
33541a49cc3SMasahide NAKAMURA 	int			(*state_sort)(struct xfrm_state **dst, struct xfrm_state **src, int n);
336ede2059dSEric W. Biederman 	int			(*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
3377026b1ddSDavid Miller 	int			(*output_finish)(struct sock *sk, struct sk_buff *skb);
338227620e2SHerbert Xu 	int			(*extract_input)(struct xfrm_state *x,
339227620e2SHerbert Xu 						 struct sk_buff *skb);
34036cf9acfSHerbert Xu 	int			(*extract_output)(struct xfrm_state *x,
34136cf9acfSHerbert Xu 						  struct sk_buff *skb);
342716062fdSHerbert Xu 	int			(*transport_finish)(struct sk_buff *skb,
343716062fdSHerbert Xu 						    int async);
344628e341fSHannes Frederic Sowa 	void			(*local_error)(struct sk_buff *skb, u32 mtu);
3451da177e4SLinus Torvalds };
3461da177e4SLinus Torvalds 
347d511337aSJoe Perches int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo);
348d511337aSJoe Perches int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo);
349d511337aSJoe Perches struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
350711059b9SFlorian Westphal struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family);
3511da177e4SLinus Torvalds 
3522f32b51bSSteffen Klassert struct xfrm_input_afinfo {
3532f32b51bSSteffen Klassert 	unsigned int		family;
3542f32b51bSSteffen Klassert 	int			(*callback)(struct sk_buff *skb, u8 protocol,
3552f32b51bSSteffen Klassert 					    int err);
3562f32b51bSSteffen Klassert };
3572f32b51bSSteffen Klassert 
358960fdfdeSFlorian Westphal int xfrm_input_register_afinfo(const struct xfrm_input_afinfo *afinfo);
359960fdfdeSFlorian Westphal int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo *afinfo);
3602f32b51bSSteffen Klassert 
361d511337aSJoe Perches void xfrm_state_delete_tunnel(struct xfrm_state *x);
3621da177e4SLinus Torvalds 
363fd2c3ef7SEric Dumazet struct xfrm_type {
3641da177e4SLinus Torvalds 	char			*description;
3651da177e4SLinus Torvalds 	struct module		*owner;
366a6337463Sjamal 	u8			proto;
367a6337463Sjamal 	u8			flags;
3681b5c2299SMasahide NAKAMURA #define XFRM_TYPE_NON_FRAGMENT	1
369436a0a40SHerbert Xu #define XFRM_TYPE_REPLAY_PROT	2
370f04e7e8dSHerbert Xu #define XFRM_TYPE_LOCAL_COADDR	4
371f04e7e8dSHerbert Xu #define XFRM_TYPE_REMOTE_COADDR	8
3721da177e4SLinus Torvalds 
37372cb6962SHerbert Xu 	int			(*init_state)(struct xfrm_state *x);
3741da177e4SLinus Torvalds 	void			(*destructor)(struct xfrm_state *);
375e695633eSHerbert Xu 	int			(*input)(struct xfrm_state *, struct sk_buff *skb);
3761da177e4SLinus Torvalds 	int			(*output)(struct xfrm_state *, struct sk_buff *pskb);
3778f029de2SDavid S. Miller 	int			(*reject)(struct xfrm_state *, struct sk_buff *,
3788f029de2SDavid S. Miller 					  const struct flowi *);
379aee5adb4SMasahide NAKAMURA 	int			(*hdr_offset)(struct xfrm_state *, struct sk_buff *, u8 **);
3801da177e4SLinus Torvalds 	/* Estimate maximal size of result of transformation of a dgram */
381c5c25238SPatrick McHardy 	u32			(*get_mtu)(struct xfrm_state *, int size);
3821da177e4SLinus Torvalds };
3831da177e4SLinus Torvalds 
384d511337aSJoe Perches int xfrm_register_type(const struct xfrm_type *type, unsigned short family);
385d511337aSJoe Perches int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family);
3861da177e4SLinus Torvalds 
3879d389d7fSSteffen Klassert struct xfrm_type_offload {
3889d389d7fSSteffen Klassert 	char		*description;
3899d389d7fSSteffen Klassert 	struct module	*owner;
3909d389d7fSSteffen Klassert 	u8		proto;
3919d389d7fSSteffen Klassert 	void		(*encap)(struct xfrm_state *, struct sk_buff *pskb);
3929d389d7fSSteffen Klassert 	int		(*input_tail)(struct xfrm_state *x, struct sk_buff *skb);
3939d389d7fSSteffen Klassert 	int		(*xmit)(struct xfrm_state *, struct sk_buff *pskb, netdev_features_t features);
3949d389d7fSSteffen Klassert };
3959d389d7fSSteffen Klassert 
3969d389d7fSSteffen Klassert int xfrm_register_type_offload(const struct xfrm_type_offload *type, unsigned short family);
3979d389d7fSSteffen Klassert int xfrm_unregister_type_offload(const struct xfrm_type_offload *type, unsigned short family);
3989d389d7fSSteffen Klassert 
399b59f45d0SHerbert Xu struct xfrm_mode {
400227620e2SHerbert Xu 	/*
401227620e2SHerbert Xu 	 * Remove encapsulation header.
402227620e2SHerbert Xu 	 *
403227620e2SHerbert Xu 	 * The IP header will be moved over the top of the encapsulation
404227620e2SHerbert Xu 	 * header.
405227620e2SHerbert Xu 	 *
406227620e2SHerbert Xu 	 * On entry, the transport header shall point to where the IP header
407227620e2SHerbert Xu 	 * should be and the network header shall be set to where the IP
408227620e2SHerbert Xu 	 * header currently is.  skb->data shall point to the start of the
409227620e2SHerbert Xu 	 * payload.
410227620e2SHerbert Xu 	 */
411227620e2SHerbert Xu 	int (*input2)(struct xfrm_state *x, struct sk_buff *skb);
412227620e2SHerbert Xu 
413227620e2SHerbert Xu 	/*
414227620e2SHerbert Xu 	 * This is the actual input entry point.
415227620e2SHerbert Xu 	 *
416227620e2SHerbert Xu 	 * For transport mode and equivalent this would be identical to
417227620e2SHerbert Xu 	 * input2 (which does not need to be set).  While tunnel mode
418227620e2SHerbert Xu 	 * and equivalent would set this to the tunnel encapsulation function
419227620e2SHerbert Xu 	 * xfrm4_prepare_input that would in turn call input2.
420227620e2SHerbert Xu 	 */
421b59f45d0SHerbert Xu 	int (*input)(struct xfrm_state *x, struct sk_buff *skb);
42237fedd3aSHerbert Xu 
42337fedd3aSHerbert Xu 	/*
42437fedd3aSHerbert Xu 	 * Add encapsulation header.
42537fedd3aSHerbert Xu 	 *
42637fedd3aSHerbert Xu 	 * On exit, the transport header will be set to the start of the
42737fedd3aSHerbert Xu 	 * encapsulation header to be filled in by x->type->output and
42837fedd3aSHerbert Xu 	 * the mac header will be set to the nextheader (protocol for
42937fedd3aSHerbert Xu 	 * IPv4) field of the extension header directly preceding the
43037fedd3aSHerbert Xu 	 * encapsulation header, or in its absence, that of the top IP
43137fedd3aSHerbert Xu 	 * header.  The value of the network header will always point
43237fedd3aSHerbert Xu 	 * to the top IP header while skb->data will point to the payload.
43337fedd3aSHerbert Xu 	 */
43436cf9acfSHerbert Xu 	int (*output2)(struct xfrm_state *x,struct sk_buff *skb);
43536cf9acfSHerbert Xu 
43636cf9acfSHerbert Xu 	/*
43736cf9acfSHerbert Xu 	 * This is the actual output entry point.
43836cf9acfSHerbert Xu 	 *
43936cf9acfSHerbert Xu 	 * For transport mode and equivalent this would be identical to
44036cf9acfSHerbert Xu 	 * output2 (which does not need to be set).  While tunnel mode
44136cf9acfSHerbert Xu 	 * and equivalent would set this to a tunnel encapsulation function
44236cf9acfSHerbert Xu 	 * (xfrm4_prepare_output or xfrm6_prepare_output) that would in turn
44336cf9acfSHerbert Xu 	 * call output2.
44436cf9acfSHerbert Xu 	 */
445eb878e84SJamal Hadi Salim 	int (*output)(struct xfrm_state *x, struct sk_buff *skb);
446b59f45d0SHerbert Xu 
447c35fe410SSteffen Klassert 	/*
448c35fe410SSteffen Klassert 	 * Adjust pointers into the packet and do GSO segmentation.
449c35fe410SSteffen Klassert 	 */
450c35fe410SSteffen Klassert 	struct sk_buff *(*gso_segment)(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features);
451c35fe410SSteffen Klassert 
452c35fe410SSteffen Klassert 	/*
453c35fe410SSteffen Klassert 	 * Adjust pointers into the packet when IPsec is done at layer2.
454c35fe410SSteffen Klassert 	 */
455c35fe410SSteffen Klassert 	void (*xmit)(struct xfrm_state *x, struct sk_buff *skb);
456c35fe410SSteffen Klassert 
45717c2a42aSHerbert Xu 	struct xfrm_state_afinfo *afinfo;
458b59f45d0SHerbert Xu 	struct module *owner;
459b59f45d0SHerbert Xu 	unsigned int encap;
4601bfcb10fSHerbert Xu 	int flags;
4611bfcb10fSHerbert Xu };
4621bfcb10fSHerbert Xu 
4631bfcb10fSHerbert Xu /* Flags for xfrm_mode. */
4641bfcb10fSHerbert Xu enum {
4651bfcb10fSHerbert Xu 	XFRM_MODE_FLAG_TUNNEL = 1,
466b59f45d0SHerbert Xu };
467b59f45d0SHerbert Xu 
468d511337aSJoe Perches int xfrm_register_mode(struct xfrm_mode *mode, int family);
469d511337aSJoe Perches int xfrm_unregister_mode(struct xfrm_mode *mode, int family);
470b59f45d0SHerbert Xu 
471df9dcb45SKazunori MIYAZAWA static inline int xfrm_af2proto(unsigned int family)
472df9dcb45SKazunori MIYAZAWA {
473df9dcb45SKazunori MIYAZAWA 	switch(family) {
474df9dcb45SKazunori MIYAZAWA 	case AF_INET:
475df9dcb45SKazunori MIYAZAWA 		return IPPROTO_IPIP;
476df9dcb45SKazunori MIYAZAWA 	case AF_INET6:
477df9dcb45SKazunori MIYAZAWA 		return IPPROTO_IPV6;
478df9dcb45SKazunori MIYAZAWA 	default:
479df9dcb45SKazunori MIYAZAWA 		return 0;
480df9dcb45SKazunori MIYAZAWA 	}
481df9dcb45SKazunori MIYAZAWA }
482df9dcb45SKazunori MIYAZAWA 
483df9dcb45SKazunori MIYAZAWA static inline struct xfrm_mode *xfrm_ip2inner_mode(struct xfrm_state *x, int ipproto)
484df9dcb45SKazunori MIYAZAWA {
485df9dcb45SKazunori MIYAZAWA 	if ((ipproto == IPPROTO_IPIP && x->props.family == AF_INET) ||
486df9dcb45SKazunori MIYAZAWA 	    (ipproto == IPPROTO_IPV6 && x->props.family == AF_INET6))
487df9dcb45SKazunori MIYAZAWA 		return x->inner_mode;
488df9dcb45SKazunori MIYAZAWA 	else
489df9dcb45SKazunori MIYAZAWA 		return x->inner_mode_iaf;
490df9dcb45SKazunori MIYAZAWA }
491df9dcb45SKazunori MIYAZAWA 
492fd2c3ef7SEric Dumazet struct xfrm_tmpl {
4931da177e4SLinus Torvalds /* id in template is interpreted as:
4941da177e4SLinus Torvalds  * daddr - destination of tunnel, may be zero for transport mode.
4951da177e4SLinus Torvalds  * spi   - zero to acquire spi. Not zero if spi is static, then
4961da177e4SLinus Torvalds  *	   daddr must be fixed too.
4971da177e4SLinus Torvalds  * proto - AH/ESP/IPCOMP
4981da177e4SLinus Torvalds  */
4991da177e4SLinus Torvalds 	struct xfrm_id		id;
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds /* Source address of tunnel. Ignored, if it is not a tunnel. */
5021da177e4SLinus Torvalds 	xfrm_address_t		saddr;
5031da177e4SLinus Torvalds 
50476b3f055SMiika Komu 	unsigned short		encap_family;
50576b3f055SMiika Komu 
506a6337463Sjamal 	u32			reqid;
5071da177e4SLinus Torvalds 
5087e49e6deSMasahide NAKAMURA /* Mode: transport, tunnel etc. */
509a6337463Sjamal 	u8			mode;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds /* Sharing mode: unique, this session only, this user only etc. */
512a6337463Sjamal 	u8			share;
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds /* May skip this transfomration if no SA is found */
515a6337463Sjamal 	u8			optional;
5161da177e4SLinus Torvalds 
517c5d18e98SHerbert Xu /* Skip aalgos/ealgos/calgos checks. */
518a6337463Sjamal 	u8			allalgs;
519c5d18e98SHerbert Xu 
5201da177e4SLinus Torvalds /* Bit mask of algos allowed for acquisition */
521a6337463Sjamal 	u32			aalgos;
522a6337463Sjamal 	u32			ealgos;
523a6337463Sjamal 	u32			calgos;
5241da177e4SLinus Torvalds };
5251da177e4SLinus Torvalds 
526622dc828SMasahide NAKAMURA #define XFRM_MAX_DEPTH		6
52754ef207aSSteffen Klassert #define XFRM_MAX_OFFLOAD_DEPTH	1
5281da177e4SLinus Torvalds 
52912a169e7SHerbert Xu struct xfrm_policy_walk_entry {
53012a169e7SHerbert Xu 	struct list_head	all;
53112a169e7SHerbert Xu 	u8			dead;
53212a169e7SHerbert Xu };
53312a169e7SHerbert Xu 
53412a169e7SHerbert Xu struct xfrm_policy_walk {
53512a169e7SHerbert Xu 	struct xfrm_policy_walk_entry walk;
53612a169e7SHerbert Xu 	u8 type;
53712a169e7SHerbert Xu 	u32 seq;
53812a169e7SHerbert Xu };
53912a169e7SHerbert Xu 
540a0073fe1SSteffen Klassert struct xfrm_policy_queue {
541a0073fe1SSteffen Klassert 	struct sk_buff_head	hold_queue;
542a0073fe1SSteffen Klassert 	struct timer_list	hold_timer;
543a0073fe1SSteffen Klassert 	unsigned long		timeout;
544a0073fe1SSteffen Klassert };
545a0073fe1SSteffen Klassert 
546fd2c3ef7SEric Dumazet struct xfrm_policy {
5470c5c9fb5SEric W. Biederman 	possible_net_t		xp_net;
5482518c7c2SDavid S. Miller 	struct hlist_node	bydst;
5492518c7c2SDavid S. Miller 	struct hlist_node	byidx;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	/* This lock only affects elements except for entry. */
5521da177e4SLinus Torvalds 	rwlock_t		lock;
5531da177e4SLinus Torvalds 	atomic_t		refcnt;
5541da177e4SLinus Torvalds 	struct timer_list	timer;
5551da177e4SLinus Torvalds 
556fe1a5f03STimo Teräs 	struct flow_cache_object flo;
55780c802f3STimo Teräs 	atomic_t		genid;
5581da177e4SLinus Torvalds 	u32			priority;
5591da177e4SLinus Torvalds 	u32			index;
560bf825f81SJamal Hadi Salim 	struct xfrm_mark	mark;
5611da177e4SLinus Torvalds 	struct xfrm_selector	selector;
5621da177e4SLinus Torvalds 	struct xfrm_lifetime_cfg lft;
5631da177e4SLinus Torvalds 	struct xfrm_lifetime_cur curlft;
56412a169e7SHerbert Xu 	struct xfrm_policy_walk_entry walk;
565a0073fe1SSteffen Klassert 	struct xfrm_policy_queue polq;
56646ca5f5dSArnaldo Carvalho de Melo 	u8			type;
56746ca5f5dSArnaldo Carvalho de Melo 	u8			action;
56846ca5f5dSArnaldo Carvalho de Melo 	u8			flags;
56946ca5f5dSArnaldo Carvalho de Melo 	u8			xfrm_nr;
57012a169e7SHerbert Xu 	u16			family;
571df71837dSTrent Jaeger 	struct xfrm_sec_ctx	*security;
5721da177e4SLinus Torvalds 	struct xfrm_tmpl       	xfrm_vec[XFRM_MAX_DEPTH];
57356f04730SEric Dumazet 	struct rcu_head		rcu;
5741da177e4SLinus Torvalds };
5751da177e4SLinus Torvalds 
57663eb23f5SDavid S. Miller static inline struct net *xp_net(const struct xfrm_policy *xp)
5770331b1f3SAlexey Dobriyan {
5780331b1f3SAlexey Dobriyan 	return read_pnet(&xp->xp_net);
5790331b1f3SAlexey Dobriyan }
5800331b1f3SAlexey Dobriyan 
58113c1d189SArnaud Ebalard struct xfrm_kmaddress {
58213c1d189SArnaud Ebalard 	xfrm_address_t          local;
58313c1d189SArnaud Ebalard 	xfrm_address_t          remote;
58413c1d189SArnaud Ebalard 	u32			reserved;
58513c1d189SArnaud Ebalard 	u16			family;
58613c1d189SArnaud Ebalard };
58713c1d189SArnaud Ebalard 
58880c9abaaSShinta Sugimoto struct xfrm_migrate {
58980c9abaaSShinta Sugimoto 	xfrm_address_t		old_daddr;
59080c9abaaSShinta Sugimoto 	xfrm_address_t		old_saddr;
59180c9abaaSShinta Sugimoto 	xfrm_address_t		new_daddr;
59280c9abaaSShinta Sugimoto 	xfrm_address_t		new_saddr;
59380c9abaaSShinta Sugimoto 	u8			proto;
59480c9abaaSShinta Sugimoto 	u8			mode;
59580c9abaaSShinta Sugimoto 	u16			reserved;
59680c9abaaSShinta Sugimoto 	u32			reqid;
59780c9abaaSShinta Sugimoto 	u16			old_family;
59880c9abaaSShinta Sugimoto 	u16			new_family;
59980c9abaaSShinta Sugimoto };
60080c9abaaSShinta Sugimoto 
6011da177e4SLinus Torvalds #define XFRM_KM_TIMEOUT                30
602f8cd5488SJamal Hadi Salim /* what happened */
603f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_UPDATE	XFRM_AE_CR
604f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_TIMEOUT	XFRM_AE_CE
605f8cd5488SJamal Hadi Salim 
606f8cd5488SJamal Hadi Salim /* default aevent timeout in units of 100ms */
607f8cd5488SJamal Hadi Salim #define XFRM_AE_ETIME			10
608f8cd5488SJamal Hadi Salim /* Async Event timer multiplier */
609f8cd5488SJamal Hadi Salim #define XFRM_AE_ETH_M			10
610f8cd5488SJamal Hadi Salim /* default seq threshold size */
611f8cd5488SJamal Hadi Salim #define XFRM_AE_SEQT_SIZE		2
6121da177e4SLinus Torvalds 
613fd2c3ef7SEric Dumazet struct xfrm_mgr {
6141da177e4SLinus Torvalds 	struct list_head	list;
615214e005bSDavid S. Miller 	int			(*notify)(struct xfrm_state *x, const struct km_event *c);
61665e0736bSFan Du 	int			(*acquire)(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *xp);
617cb969f07SVenkat Yekkirala 	struct xfrm_policy	*(*compile_policy)(struct sock *sk, int opt, u8 *data, int len, int *dir);
6185d36b180SAl Viro 	int			(*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport);
619214e005bSDavid S. Miller 	int			(*notify_policy)(struct xfrm_policy *x, int dir, const struct km_event *c);
620db983c11SAlexey Dobriyan 	int			(*report)(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr);
621183cad12SDavid S. Miller 	int			(*migrate)(const struct xfrm_selector *sel,
622183cad12SDavid S. Miller 					   u8 dir, u8 type,
623183cad12SDavid S. Miller 					   const struct xfrm_migrate *m,
624183cad12SDavid S. Miller 					   int num_bundles,
625183cad12SDavid S. Miller 					   const struct xfrm_kmaddress *k);
6260f24558eSHoria Geanta 	bool			(*is_alive)(const struct km_event *c);
6271da177e4SLinus Torvalds };
6281da177e4SLinus Torvalds 
629d511337aSJoe Perches int xfrm_register_km(struct xfrm_mgr *km);
630d511337aSJoe Perches int xfrm_unregister_km(struct xfrm_mgr *km);
6311da177e4SLinus Torvalds 
63270be6c91SSteffen Klassert struct xfrm_tunnel_skb_cb {
63370be6c91SSteffen Klassert 	union {
63470be6c91SSteffen Klassert 		struct inet_skb_parm h4;
63570be6c91SSteffen Klassert 		struct inet6_skb_parm h6;
63670be6c91SSteffen Klassert 	} header;
63770be6c91SSteffen Klassert 
63870be6c91SSteffen Klassert 	union {
63970be6c91SSteffen Klassert 		struct ip_tunnel *ip4;
64070be6c91SSteffen Klassert 		struct ip6_tnl *ip6;
64170be6c91SSteffen Klassert 	} tunnel;
64270be6c91SSteffen Klassert };
64370be6c91SSteffen Klassert 
64470be6c91SSteffen Klassert #define XFRM_TUNNEL_SKB_CB(__skb) ((struct xfrm_tunnel_skb_cb *)&((__skb)->cb[0]))
64570be6c91SSteffen Klassert 
646436a0a40SHerbert Xu /*
647436a0a40SHerbert Xu  * This structure is used for the duration where packets are being
648436a0a40SHerbert Xu  * transformed by IPsec.  As soon as the packet leaves IPsec the
649436a0a40SHerbert Xu  * area beyond the generic IP part may be overwritten.
650436a0a40SHerbert Xu  */
651436a0a40SHerbert Xu struct xfrm_skb_cb {
65270be6c91SSteffen Klassert 	struct xfrm_tunnel_skb_cb header;
653436a0a40SHerbert Xu 
654436a0a40SHerbert Xu         /* Sequence number for replay protection. */
655b318e0e4SHerbert Xu 	union {
6561ce3644aSSteffen Klassert 		struct {
6571ce3644aSSteffen Klassert 			__u32 low;
6581ce3644aSSteffen Klassert 			__u32 hi;
6591ce3644aSSteffen Klassert 		} output;
6601ce3644aSSteffen Klassert 		struct {
6611ce3644aSSteffen Klassert 			__be32 low;
6621ce3644aSSteffen Klassert 			__be32 hi;
6631ce3644aSSteffen Klassert 		} input;
664b318e0e4SHerbert Xu 	} seq;
665436a0a40SHerbert Xu };
666436a0a40SHerbert Xu 
667436a0a40SHerbert Xu #define XFRM_SKB_CB(__skb) ((struct xfrm_skb_cb *)&((__skb)->cb[0]))
668436a0a40SHerbert Xu 
66936cf9acfSHerbert Xu /*
67036cf9acfSHerbert Xu  * This structure is used by the afinfo prepare_input/prepare_output functions
67136cf9acfSHerbert Xu  * to transmit header information to the mode input/output functions.
67236cf9acfSHerbert Xu  */
67336cf9acfSHerbert Xu struct xfrm_mode_skb_cb {
67470be6c91SSteffen Klassert 	struct xfrm_tunnel_skb_cb header;
67536cf9acfSHerbert Xu 
67636cf9acfSHerbert Xu 	/* Copied from header for IPv4, always set to zero and DF for IPv6. */
67736cf9acfSHerbert Xu 	__be16 id;
67836cf9acfSHerbert Xu 	__be16 frag_off;
67936cf9acfSHerbert Xu 
680732c8bd5SHerbert Xu 	/* IP header length (excluding options or extension headers). */
681732c8bd5SHerbert Xu 	u8 ihl;
682732c8bd5SHerbert Xu 
68336cf9acfSHerbert Xu 	/* TOS for IPv4, class for IPv6. */
68436cf9acfSHerbert Xu 	u8 tos;
68536cf9acfSHerbert Xu 
68636cf9acfSHerbert Xu 	/* TTL for IPv4, hop limitfor IPv6. */
68736cf9acfSHerbert Xu 	u8 ttl;
68836cf9acfSHerbert Xu 
68936cf9acfSHerbert Xu 	/* Protocol for IPv4, NH for IPv6. */
69036cf9acfSHerbert Xu 	u8 protocol;
69136cf9acfSHerbert Xu 
692732c8bd5SHerbert Xu 	/* Option length for IPv4, zero for IPv6. */
693732c8bd5SHerbert Xu 	u8 optlen;
694732c8bd5SHerbert Xu 
69536cf9acfSHerbert Xu 	/* Used by IPv6 only, zero for IPv4. */
69636cf9acfSHerbert Xu 	u8 flow_lbl[3];
69736cf9acfSHerbert Xu };
69836cf9acfSHerbert Xu 
69936cf9acfSHerbert Xu #define XFRM_MODE_SKB_CB(__skb) ((struct xfrm_mode_skb_cb *)&((__skb)->cb[0]))
70036cf9acfSHerbert Xu 
701716062fdSHerbert Xu /*
702716062fdSHerbert Xu  * This structure is used by the input processing to locate the SPI and
703716062fdSHerbert Xu  * related information.
704716062fdSHerbert Xu  */
705716062fdSHerbert Xu struct xfrm_spi_skb_cb {
70670be6c91SSteffen Klassert 	struct xfrm_tunnel_skb_cb header;
707716062fdSHerbert Xu 
708716062fdSHerbert Xu 	unsigned int daddroff;
7092fcb45b6SHerbert Xu 	unsigned int family;
7107785bba2SSteffen Klassert 	__be32 seq;
711716062fdSHerbert Xu };
712716062fdSHerbert Xu 
713716062fdSHerbert Xu #define XFRM_SPI_SKB_CB(__skb) ((struct xfrm_spi_skb_cb *)&((__skb)->cb[0]))
714716062fdSHerbert Xu 
715c9204d9cSJoy Latten #ifdef CONFIG_AUDITSYSCALL
716afeb14b4SPaul Moore static inline struct audit_buffer *xfrm_audit_start(const char *op)
717ab5f5e8bSJoy Latten {
718ab5f5e8bSJoy Latten 	struct audit_buffer *audit_buf = NULL;
719ab5f5e8bSJoy Latten 
720afeb14b4SPaul Moore 	if (audit_enabled == 0)
721afeb14b4SPaul Moore 		return NULL;
722ab5f5e8bSJoy Latten 	audit_buf = audit_log_start(current->audit_context, GFP_ATOMIC,
723ab5f5e8bSJoy Latten 				    AUDIT_MAC_IPSEC_EVENT);
724ab5f5e8bSJoy Latten 	if (audit_buf == NULL)
725ab5f5e8bSJoy Latten 		return NULL;
726afeb14b4SPaul Moore 	audit_log_format(audit_buf, "op=%s", op);
727afeb14b4SPaul Moore 	return audit_buf;
728afeb14b4SPaul Moore }
729afeb14b4SPaul Moore 
7302e71029eSTetsuo Handa static inline void xfrm_audit_helper_usrinfo(bool task_valid,
731afeb14b4SPaul Moore 					     struct audit_buffer *audit_buf)
732afeb14b4SPaul Moore {
7332e71029eSTetsuo Handa 	const unsigned int auid = from_kuid(&init_user_ns, task_valid ?
7342e71029eSTetsuo Handa 					    audit_get_loginuid(current) :
7352e71029eSTetsuo Handa 					    INVALID_UID);
7362e71029eSTetsuo Handa 	const unsigned int ses = task_valid ? audit_get_sessionid(current) :
7372e71029eSTetsuo Handa 		(unsigned int) -1;
7382e71029eSTetsuo Handa 
7392e71029eSTetsuo Handa 	audit_log_format(audit_buf, " auid=%u ses=%u", auid, ses);
740ab5f5e8bSJoy Latten 	audit_log_task_context(audit_buf);
741ab5f5e8bSJoy Latten }
742ab5f5e8bSJoy Latten 
7432e71029eSTetsuo Handa void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid);
7442e71029eSTetsuo Handa void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
7452e71029eSTetsuo Handa 			      bool task_valid);
7462e71029eSTetsuo Handa void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid);
7472e71029eSTetsuo Handa void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid);
748d511337aSJoe Perches void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
749afeb14b4SPaul Moore 				      struct sk_buff *skb);
750d511337aSJoe Perches void xfrm_audit_state_replay(struct xfrm_state *x, struct sk_buff *skb,
751d511337aSJoe Perches 			     __be32 net_seq);
752d511337aSJoe Perches void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family);
753d511337aSJoe Perches void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family, __be32 net_spi,
754d511337aSJoe Perches 			       __be32 net_seq);
755d511337aSJoe Perches void xfrm_audit_state_icvfail(struct xfrm_state *x, struct sk_buff *skb,
756d511337aSJoe Perches 			      u8 proto);
757c9204d9cSJoy Latten #else
75841fef0eeSMarcin Slusarz 
75941fef0eeSMarcin Slusarz static inline void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
7602e71029eSTetsuo Handa 					 bool task_valid)
76141fef0eeSMarcin Slusarz {
76241fef0eeSMarcin Slusarz }
76341fef0eeSMarcin Slusarz 
76441fef0eeSMarcin Slusarz static inline void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
7652e71029eSTetsuo Handa 					    bool task_valid)
76641fef0eeSMarcin Slusarz {
76741fef0eeSMarcin Slusarz }
76841fef0eeSMarcin Slusarz 
76941fef0eeSMarcin Slusarz static inline void xfrm_audit_state_add(struct xfrm_state *x, int result,
7702e71029eSTetsuo Handa 					bool task_valid)
77141fef0eeSMarcin Slusarz {
77241fef0eeSMarcin Slusarz }
77341fef0eeSMarcin Slusarz 
77441fef0eeSMarcin Slusarz static inline void xfrm_audit_state_delete(struct xfrm_state *x, int result,
7752e71029eSTetsuo Handa 					   bool task_valid)
77641fef0eeSMarcin Slusarz {
77741fef0eeSMarcin Slusarz }
77841fef0eeSMarcin Slusarz 
77941fef0eeSMarcin Slusarz static inline void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
78041fef0eeSMarcin Slusarz 					     struct sk_buff *skb)
78141fef0eeSMarcin Slusarz {
78241fef0eeSMarcin Slusarz }
78341fef0eeSMarcin Slusarz 
7849fdc4883SSteffen Klassert static inline void xfrm_audit_state_replay(struct xfrm_state *x,
7859fdc4883SSteffen Klassert 					   struct sk_buff *skb, __be32 net_seq)
7869fdc4883SSteffen Klassert {
7879fdc4883SSteffen Klassert }
7889fdc4883SSteffen Klassert 
78941fef0eeSMarcin Slusarz static inline void xfrm_audit_state_notfound_simple(struct sk_buff *skb,
79041fef0eeSMarcin Slusarz 				      u16 family)
79141fef0eeSMarcin Slusarz {
79241fef0eeSMarcin Slusarz }
79341fef0eeSMarcin Slusarz 
79441fef0eeSMarcin Slusarz static inline void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
79541fef0eeSMarcin Slusarz 				      __be32 net_spi, __be32 net_seq)
79641fef0eeSMarcin Slusarz {
79741fef0eeSMarcin Slusarz }
79841fef0eeSMarcin Slusarz 
79941fef0eeSMarcin Slusarz static inline void xfrm_audit_state_icvfail(struct xfrm_state *x,
80041fef0eeSMarcin Slusarz 				     struct sk_buff *skb, u8 proto)
80141fef0eeSMarcin Slusarz {
80241fef0eeSMarcin Slusarz }
803c9204d9cSJoy Latten #endif /* CONFIG_AUDITSYSCALL */
804161a09e7SJoy Latten 
8051da177e4SLinus Torvalds static inline void xfrm_pol_hold(struct xfrm_policy *policy)
8061da177e4SLinus Torvalds {
8071da177e4SLinus Torvalds 	if (likely(policy != NULL))
8081da177e4SLinus Torvalds 		atomic_inc(&policy->refcnt);
8091da177e4SLinus Torvalds }
8101da177e4SLinus Torvalds 
811d511337aSJoe Perches void xfrm_policy_destroy(struct xfrm_policy *policy);
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds static inline void xfrm_pol_put(struct xfrm_policy *policy)
8141da177e4SLinus Torvalds {
8151da177e4SLinus Torvalds 	if (atomic_dec_and_test(&policy->refcnt))
81664c31b3fSWANG Cong 		xfrm_policy_destroy(policy);
8171da177e4SLinus Torvalds }
8181da177e4SLinus Torvalds 
8194e81bb83SMasahide NAKAMURA static inline void xfrm_pols_put(struct xfrm_policy **pols, int npols)
8204e81bb83SMasahide NAKAMURA {
8214e81bb83SMasahide NAKAMURA 	int i;
8224e81bb83SMasahide NAKAMURA 	for (i = npols - 1; i >= 0; --i)
8234e81bb83SMasahide NAKAMURA 		xfrm_pol_put(pols[i]);
8244e81bb83SMasahide NAKAMURA }
8254e81bb83SMasahide NAKAMURA 
826d511337aSJoe Perches void __xfrm_state_destroy(struct xfrm_state *);
8271da177e4SLinus Torvalds 
82821380b81SHerbert Xu static inline void __xfrm_state_put(struct xfrm_state *x)
82921380b81SHerbert Xu {
83021380b81SHerbert Xu 	atomic_dec(&x->refcnt);
83121380b81SHerbert Xu }
83221380b81SHerbert Xu 
8331da177e4SLinus Torvalds static inline void xfrm_state_put(struct xfrm_state *x)
8341da177e4SLinus Torvalds {
8351da177e4SLinus Torvalds 	if (atomic_dec_and_test(&x->refcnt))
8361da177e4SLinus Torvalds 		__xfrm_state_destroy(x);
8371da177e4SLinus Torvalds }
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds static inline void xfrm_state_hold(struct xfrm_state *x)
8401da177e4SLinus Torvalds {
8411da177e4SLinus Torvalds 	atomic_inc(&x->refcnt);
8421da177e4SLinus Torvalds }
8431da177e4SLinus Torvalds 
8441744a8feSDavid S. Miller static inline bool addr_match(const void *token1, const void *token2,
845e1b0048eSAlexey Dobriyan 			      unsigned int prefixlen)
8461da177e4SLinus Torvalds {
8471744a8feSDavid S. Miller 	const __be32 *a1 = token1;
8481744a8feSDavid S. Miller 	const __be32 *a2 = token2;
849e1b0048eSAlexey Dobriyan 	unsigned int pdw;
850e1b0048eSAlexey Dobriyan 	unsigned int pbi;
8511da177e4SLinus Torvalds 
852a6337463Sjamal 	pdw = prefixlen >> 5;	  /* num of whole u32 in prefix */
8531da177e4SLinus Torvalds 	pbi = prefixlen &  0x1f;  /* num of bits in incomplete u32 in prefix */
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	if (pdw)
8561da177e4SLinus Torvalds 		if (memcmp(a1, a2, pdw << 2))
8571744a8feSDavid S. Miller 			return false;
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	if (pbi) {
8605f19343fSAl Viro 		__be32 mask;
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 		mask = htonl((0xffffffff) << (32 - pbi));
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 		if ((a1[pdw] ^ a2[pdw]) & mask)
8651744a8feSDavid S. Miller 			return false;
8661da177e4SLinus Torvalds 	}
8671da177e4SLinus Torvalds 
8681744a8feSDavid S. Miller 	return true;
8691da177e4SLinus Torvalds }
8701da177e4SLinus Torvalds 
87126bff940SAlexey Dobriyan static inline bool addr4_match(__be32 a1, __be32 a2, u8 prefixlen)
87226bff940SAlexey Dobriyan {
87326bff940SAlexey Dobriyan 	/* C99 6.5.7 (3): u32 << 32 is undefined behaviour */
8746c786bcbSAlexey Dobriyan 	if (sizeof(long) == 4 && prefixlen == 0)
87526bff940SAlexey Dobriyan 		return true;
8766c786bcbSAlexey Dobriyan 	return !((a1 ^ a2) & htonl(~0UL << (32 - prefixlen)));
87726bff940SAlexey Dobriyan }
87826bff940SAlexey Dobriyan 
8791da177e4SLinus Torvalds static __inline__
8806281dcc9SDavid S. Miller __be16 xfrm_flowi_sport(const struct flowi *fl, const union flowi_uli *uli)
8811da177e4SLinus Torvalds {
882f9d07e41SAl Viro 	__be16 port;
8831d28f42cSDavid S. Miller 	switch(fl->flowi_proto) {
8841da177e4SLinus Torvalds 	case IPPROTO_TCP:
8851da177e4SLinus Torvalds 	case IPPROTO_UDP:
886ba4e58ecSGerrit Renker 	case IPPROTO_UDPLITE:
8871da177e4SLinus Torvalds 	case IPPROTO_SCTP:
8886281dcc9SDavid S. Miller 		port = uli->ports.sport;
8891da177e4SLinus Torvalds 		break;
8901da177e4SLinus Torvalds 	case IPPROTO_ICMP:
8911da177e4SLinus Torvalds 	case IPPROTO_ICMPV6:
8926281dcc9SDavid S. Miller 		port = htons(uli->icmpt.type);
8931da177e4SLinus Torvalds 		break;
8942ce4272aSMasahide NAKAMURA 	case IPPROTO_MH:
8956281dcc9SDavid S. Miller 		port = htons(uli->mht.type);
8962ce4272aSMasahide NAKAMURA 		break;
897cc9ff19dSTimo Teräs 	case IPPROTO_GRE:
8986281dcc9SDavid S. Miller 		port = htons(ntohl(uli->gre_key) >> 16);
899cc9ff19dSTimo Teräs 		break;
9001da177e4SLinus Torvalds 	default:
9011da177e4SLinus Torvalds 		port = 0;	/*XXX*/
9021da177e4SLinus Torvalds 	}
9031da177e4SLinus Torvalds 	return port;
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds static __inline__
9076281dcc9SDavid S. Miller __be16 xfrm_flowi_dport(const struct flowi *fl, const union flowi_uli *uli)
9081da177e4SLinus Torvalds {
909f9d07e41SAl Viro 	__be16 port;
9101d28f42cSDavid S. Miller 	switch(fl->flowi_proto) {
9111da177e4SLinus Torvalds 	case IPPROTO_TCP:
9121da177e4SLinus Torvalds 	case IPPROTO_UDP:
913ba4e58ecSGerrit Renker 	case IPPROTO_UDPLITE:
9141da177e4SLinus Torvalds 	case IPPROTO_SCTP:
9156281dcc9SDavid S. Miller 		port = uli->ports.dport;
9161da177e4SLinus Torvalds 		break;
9171da177e4SLinus Torvalds 	case IPPROTO_ICMP:
9181da177e4SLinus Torvalds 	case IPPROTO_ICMPV6:
9196281dcc9SDavid S. Miller 		port = htons(uli->icmpt.code);
9201da177e4SLinus Torvalds 		break;
921cc9ff19dSTimo Teräs 	case IPPROTO_GRE:
9226281dcc9SDavid S. Miller 		port = htons(ntohl(uli->gre_key) & 0xffff);
923cc9ff19dSTimo Teräs 		break;
9241da177e4SLinus Torvalds 	default:
9251da177e4SLinus Torvalds 		port = 0;	/*XXX*/
9261da177e4SLinus Torvalds 	}
9271da177e4SLinus Torvalds 	return port;
9281da177e4SLinus Torvalds }
9291da177e4SLinus Torvalds 
930d511337aSJoe Perches bool xfrm_selector_match(const struct xfrm_selector *sel,
931d511337aSJoe Perches 			 const struct flowi *fl, unsigned short family);
9321da177e4SLinus Torvalds 
933df71837dSTrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
934df71837dSTrent Jaeger /*	If neither has a context --> match
935df71837dSTrent Jaeger  * 	Otherwise, both must have a context and the sids, doi, alg must match
936df71837dSTrent Jaeger  */
937bc9b35adSDavid S. Miller static inline bool xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
938df71837dSTrent Jaeger {
939df71837dSTrent Jaeger 	return ((!s1 && !s2) ||
940df71837dSTrent Jaeger 		(s1 && s2 &&
941df71837dSTrent Jaeger 		 (s1->ctx_sid == s2->ctx_sid) &&
942df71837dSTrent Jaeger 		 (s1->ctx_doi == s2->ctx_doi) &&
943df71837dSTrent Jaeger 		 (s1->ctx_alg == s2->ctx_alg)));
944df71837dSTrent Jaeger }
945df71837dSTrent Jaeger #else
946bc9b35adSDavid S. Miller static inline bool xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
947df71837dSTrent Jaeger {
948bc9b35adSDavid S. Miller 	return true;
949df71837dSTrent Jaeger }
950df71837dSTrent Jaeger #endif
951df71837dSTrent Jaeger 
9521da177e4SLinus Torvalds /* A struct encoding bundle of transformations to apply to some set of flow.
9531da177e4SLinus Torvalds  *
9541da177e4SLinus Torvalds  * dst->child points to the next element of bundle.
9551da177e4SLinus Torvalds  * dst->xfrm  points to an instanse of transformer.
9561da177e4SLinus Torvalds  *
9571da177e4SLinus Torvalds  * Due to unfortunate limitations of current routing cache, which we
9581da177e4SLinus Torvalds  * have no time to fix, it mirrors struct rtable and bound to the same
9591da177e4SLinus Torvalds  * routing key, including saddr,daddr. However, we can have many of
9601da177e4SLinus Torvalds  * bundles differing by session id. All the bundles grow from a parent
9611da177e4SLinus Torvalds  * policy rule.
9621da177e4SLinus Torvalds  */
963fd2c3ef7SEric Dumazet struct xfrm_dst {
9641da177e4SLinus Torvalds 	union {
9651da177e4SLinus Torvalds 		struct dst_entry	dst;
9661da177e4SLinus Torvalds 		struct rtable		rt;
9671da177e4SLinus Torvalds 		struct rt6_info		rt6;
9681da177e4SLinus Torvalds 	} u;
9691da177e4SLinus Torvalds 	struct dst_entry *route;
97080c802f3STimo Teräs 	struct flow_cache_object flo;
97180c802f3STimo Teräs 	struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
97280c802f3STimo Teräs 	int num_pols, num_xfrms;
973157bfc25SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
974157bfc25SMasahide NAKAMURA 	struct flowi *origin;
975157bfc25SMasahide NAKAMURA 	struct xfrm_selector *partner;
976157bfc25SMasahide NAKAMURA #endif
97780c802f3STimo Teräs 	u32 xfrm_genid;
97880c802f3STimo Teräs 	u32 policy_genid;
9791da177e4SLinus Torvalds 	u32 route_mtu_cached;
9801da177e4SLinus Torvalds 	u32 child_mtu_cached;
98192d63decSHideaki YOSHIFUJI 	u32 route_cookie;
98292d63decSHideaki YOSHIFUJI 	u32 path_cookie;
9831da177e4SLinus Torvalds };
9841da177e4SLinus Torvalds 
985def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
986aabc9761SHerbert Xu static inline void xfrm_dst_destroy(struct xfrm_dst *xdst)
987aabc9761SHerbert Xu {
98880c802f3STimo Teräs 	xfrm_pols_put(xdst->pols, xdst->num_pols);
989aabc9761SHerbert Xu 	dst_release(xdst->route);
990aabc9761SHerbert Xu 	if (likely(xdst->u.dst.xfrm))
991aabc9761SHerbert Xu 		xfrm_state_put(xdst->u.dst.xfrm);
992157bfc25SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
993157bfc25SMasahide NAKAMURA 	kfree(xdst->origin);
994157bfc25SMasahide NAKAMURA 	xdst->origin = NULL;
995157bfc25SMasahide NAKAMURA 	kfree(xdst->partner);
996157bfc25SMasahide NAKAMURA 	xdst->partner = NULL;
997157bfc25SMasahide NAKAMURA #endif
998aabc9761SHerbert Xu }
999def8b4faSAlexey Dobriyan #endif
1000aabc9761SHerbert Xu 
1001d511337aSJoe Perches void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev);
1002aabc9761SHerbert Xu 
100354ef207aSSteffen Klassert struct xfrm_offload {
100454ef207aSSteffen Klassert 	/* Output sequence number for replay protection on offloading. */
100554ef207aSSteffen Klassert 	struct {
100654ef207aSSteffen Klassert 		__u32 low;
100754ef207aSSteffen Klassert 		__u32 hi;
100854ef207aSSteffen Klassert 	} seq;
100954ef207aSSteffen Klassert 
101054ef207aSSteffen Klassert 	__u32			flags;
101154ef207aSSteffen Klassert #define	SA_DELETE_REQ		1
101254ef207aSSteffen Klassert #define	CRYPTO_DONE		2
101354ef207aSSteffen Klassert #define	CRYPTO_NEXT_DONE	4
101454ef207aSSteffen Klassert #define	CRYPTO_FALLBACK		8
101554ef207aSSteffen Klassert #define	XFRM_GSO_SEGMENT	16
101654ef207aSSteffen Klassert #define	XFRM_GRO		32
101754ef207aSSteffen Klassert 
101854ef207aSSteffen Klassert 	__u32			status;
101954ef207aSSteffen Klassert #define CRYPTO_SUCCESS				1
102054ef207aSSteffen Klassert #define CRYPTO_GENERIC_ERROR			2
102154ef207aSSteffen Klassert #define CRYPTO_TRANSPORT_AH_AUTH_FAILED		4
102254ef207aSSteffen Klassert #define CRYPTO_TRANSPORT_ESP_AUTH_FAILED	8
102354ef207aSSteffen Klassert #define CRYPTO_TUNNEL_AH_AUTH_FAILED		16
102454ef207aSSteffen Klassert #define CRYPTO_TUNNEL_ESP_AUTH_FAILED		32
102554ef207aSSteffen Klassert #define CRYPTO_INVALID_PACKET_SYNTAX		64
102654ef207aSSteffen Klassert #define CRYPTO_INVALID_PROTOCOL			128
102754ef207aSSteffen Klassert 
102854ef207aSSteffen Klassert 	__u8			proto;
102954ef207aSSteffen Klassert };
103054ef207aSSteffen Klassert 
1031fd2c3ef7SEric Dumazet struct sec_path {
10321da177e4SLinus Torvalds 	atomic_t		refcnt;
10331da177e4SLinus Torvalds 	int			len;
103454ef207aSSteffen Klassert 	int			olen;
103554ef207aSSteffen Klassert 
1036dbe5b4aaSHerbert Xu 	struct xfrm_state	*xvec[XFRM_MAX_DEPTH];
103754ef207aSSteffen Klassert 	struct xfrm_offload	ovec[XFRM_MAX_OFFLOAD_DEPTH];
10381da177e4SLinus Torvalds };
10391da177e4SLinus Torvalds 
1040990078afSMichael Smith static inline int secpath_exists(struct sk_buff *skb)
1041990078afSMichael Smith {
1042990078afSMichael Smith #ifdef CONFIG_XFRM
1043990078afSMichael Smith 	return skb->sp != NULL;
1044990078afSMichael Smith #else
1045990078afSMichael Smith 	return 0;
1046990078afSMichael Smith #endif
1047990078afSMichael Smith }
1048990078afSMichael Smith 
10491da177e4SLinus Torvalds static inline struct sec_path *
10501da177e4SLinus Torvalds secpath_get(struct sec_path *sp)
10511da177e4SLinus Torvalds {
10521da177e4SLinus Torvalds 	if (sp)
10531da177e4SLinus Torvalds 		atomic_inc(&sp->refcnt);
10541da177e4SLinus Torvalds 	return sp;
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
1057d511337aSJoe Perches void __secpath_destroy(struct sec_path *sp);
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds static inline void
10601da177e4SLinus Torvalds secpath_put(struct sec_path *sp)
10611da177e4SLinus Torvalds {
10621da177e4SLinus Torvalds 	if (sp && atomic_dec_and_test(&sp->refcnt))
10631da177e4SLinus Torvalds 		__secpath_destroy(sp);
10641da177e4SLinus Torvalds }
10651da177e4SLinus Torvalds 
1066d511337aSJoe Perches struct sec_path *secpath_dup(struct sec_path *src);
1067b0fcee82SSteffen Klassert int secpath_set(struct sk_buff *skb);
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds static inline void
10701da177e4SLinus Torvalds secpath_reset(struct sk_buff *skb)
10711da177e4SLinus Torvalds {
10721da177e4SLinus Torvalds #ifdef CONFIG_XFRM
10731da177e4SLinus Torvalds 	secpath_put(skb->sp);
10741da177e4SLinus Torvalds 	skb->sp = NULL;
10751da177e4SLinus Torvalds #endif
10761da177e4SLinus Torvalds }
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds static inline int
10796cc32961SDavid S. Miller xfrm_addr_any(const xfrm_address_t *addr, unsigned short family)
1080a1e59abfSPatrick McHardy {
1081a1e59abfSPatrick McHardy 	switch (family) {
1082a1e59abfSPatrick McHardy 	case AF_INET:
1083a1e59abfSPatrick McHardy 		return addr->a4 == 0;
1084a1e59abfSPatrick McHardy 	case AF_INET6:
108515e318bdSJiri Benc 		return ipv6_addr_any(&addr->in6);
1086a1e59abfSPatrick McHardy 	}
1087a1e59abfSPatrick McHardy 	return 0;
1088a1e59abfSPatrick McHardy }
1089a1e59abfSPatrick McHardy 
1090a1e59abfSPatrick McHardy static inline int
109121eddb5cSDavid S. Miller __xfrm4_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x)
10921da177e4SLinus Torvalds {
10931da177e4SLinus Torvalds 	return	(tmpl->saddr.a4 &&
10941da177e4SLinus Torvalds 		 tmpl->saddr.a4 != x->props.saddr.a4);
10951da177e4SLinus Torvalds }
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds static inline int
109821eddb5cSDavid S. Miller __xfrm6_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x)
10991da177e4SLinus Torvalds {
11001da177e4SLinus Torvalds 	return	(!ipv6_addr_any((struct in6_addr*)&tmpl->saddr) &&
1101ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 		 !ipv6_addr_equal((struct in6_addr *)&tmpl->saddr, (struct in6_addr*)&x->props.saddr));
11021da177e4SLinus Torvalds }
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds static inline int
110521eddb5cSDavid S. Miller xfrm_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x, unsigned short family)
11061da177e4SLinus Torvalds {
11071da177e4SLinus Torvalds 	switch (family) {
11081da177e4SLinus Torvalds 	case AF_INET:
11091da177e4SLinus Torvalds 		return __xfrm4_state_addr_cmp(tmpl, x);
11101da177e4SLinus Torvalds 	case AF_INET6:
11111da177e4SLinus Torvalds 		return __xfrm6_state_addr_cmp(tmpl, x);
11121da177e4SLinus Torvalds 	}
11131da177e4SLinus Torvalds 	return !0;
11141da177e4SLinus Torvalds }
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds #ifdef CONFIG_XFRM
1117d511337aSJoe Perches int __xfrm_policy_check(struct sock *, int dir, struct sk_buff *skb,
1118d511337aSJoe Perches 			unsigned short family);
11191da177e4SLinus Torvalds 
1120d5422efeSHerbert Xu static inline int __xfrm_policy_check2(struct sock *sk, int dir,
1121d5422efeSHerbert Xu 				       struct sk_buff *skb,
1122d5422efeSHerbert Xu 				       unsigned int family, int reverse)
11231da177e4SLinus Torvalds {
1124f6e1e25dSAlexey Dobriyan 	struct net *net = dev_net(skb->dev);
1125d5422efeSHerbert Xu 	int ndir = dir | (reverse ? XFRM_POLICY_MASK + 1 : 0);
1126d5422efeSHerbert Xu 
11271da177e4SLinus Torvalds 	if (sk && sk->sk_policy[XFRM_POLICY_IN])
1128d5422efeSHerbert Xu 		return __xfrm_policy_check(sk, ndir, skb, family);
11291da177e4SLinus Torvalds 
1130f6e1e25dSAlexey Dobriyan 	return	(!net->xfrm.policy_count[dir] && !skb->sp) ||
1131adf30907SEric Dumazet 		(skb_dst(skb)->flags & DST_NOPOLICY) ||
1132d5422efeSHerbert Xu 		__xfrm_policy_check(sk, ndir, skb, family);
1133d5422efeSHerbert Xu }
1134d5422efeSHerbert Xu 
1135d5422efeSHerbert Xu static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
1136d5422efeSHerbert Xu {
1137d5422efeSHerbert Xu 	return __xfrm_policy_check2(sk, dir, skb, family, 0);
11381da177e4SLinus Torvalds }
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
11411da177e4SLinus Torvalds {
11421da177e4SLinus Torvalds 	return xfrm_policy_check(sk, dir, skb, AF_INET);
11431da177e4SLinus Torvalds }
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
11461da177e4SLinus Torvalds {
11471da177e4SLinus Torvalds 	return xfrm_policy_check(sk, dir, skb, AF_INET6);
11481da177e4SLinus Torvalds }
11491da177e4SLinus Torvalds 
1150d5422efeSHerbert Xu static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir,
1151d5422efeSHerbert Xu 					     struct sk_buff *skb)
1152d5422efeSHerbert Xu {
1153d5422efeSHerbert Xu 	return __xfrm_policy_check2(sk, dir, skb, AF_INET, 1);
1154d5422efeSHerbert Xu }
1155d5422efeSHerbert Xu 
1156d5422efeSHerbert Xu static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir,
1157d5422efeSHerbert Xu 					     struct sk_buff *skb)
1158d5422efeSHerbert Xu {
1159d5422efeSHerbert Xu 	return __xfrm_policy_check2(sk, dir, skb, AF_INET6, 1);
1160d5422efeSHerbert Xu }
1161d5422efeSHerbert Xu 
1162d511337aSJoe Perches int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1163d5422efeSHerbert Xu 			  unsigned int family, int reverse);
1164d5422efeSHerbert Xu 
1165d5422efeSHerbert Xu static inline int xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1166d5422efeSHerbert Xu 				      unsigned int family)
1167d5422efeSHerbert Xu {
1168d5422efeSHerbert Xu 	return __xfrm_decode_session(skb, fl, family, 0);
1169d5422efeSHerbert Xu }
1170d5422efeSHerbert Xu 
1171d5422efeSHerbert Xu static inline int xfrm_decode_session_reverse(struct sk_buff *skb,
1172d5422efeSHerbert Xu 					      struct flowi *fl,
1173d5422efeSHerbert Xu 					      unsigned int family)
1174d5422efeSHerbert Xu {
1175d5422efeSHerbert Xu 	return __xfrm_decode_session(skb, fl, family, 1);
1176d5422efeSHerbert Xu }
1177d5422efeSHerbert Xu 
1178d511337aSJoe Perches int __xfrm_route_forward(struct sk_buff *skb, unsigned short family);
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds static inline int xfrm_route_forward(struct sk_buff *skb, unsigned short family)
11811da177e4SLinus Torvalds {
118299a66657SAlexey Dobriyan 	struct net *net = dev_net(skb->dev);
118399a66657SAlexey Dobriyan 
118499a66657SAlexey Dobriyan 	return	!net->xfrm.policy_count[XFRM_POLICY_OUT] ||
1185adf30907SEric Dumazet 		(skb_dst(skb)->flags & DST_NOXFRM) ||
11861da177e4SLinus Torvalds 		__xfrm_route_forward(skb, family);
11871da177e4SLinus Torvalds }
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds static inline int xfrm4_route_forward(struct sk_buff *skb)
11901da177e4SLinus Torvalds {
11911da177e4SLinus Torvalds 	return xfrm_route_forward(skb, AF_INET);
11921da177e4SLinus Torvalds }
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds static inline int xfrm6_route_forward(struct sk_buff *skb)
11951da177e4SLinus Torvalds {
11961da177e4SLinus Torvalds 	return xfrm_route_forward(skb, AF_INET6);
11971da177e4SLinus Torvalds }
11981da177e4SLinus Torvalds 
1199d188ba86SEric Dumazet int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk);
12001da177e4SLinus Torvalds 
1201d188ba86SEric Dumazet static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
12021da177e4SLinus Torvalds {
1203d188ba86SEric Dumazet 	sk->sk_policy[0] = NULL;
1204d188ba86SEric Dumazet 	sk->sk_policy[1] = NULL;
1205d188ba86SEric Dumazet 	if (unlikely(osk->sk_policy[0] || osk->sk_policy[1]))
1206d188ba86SEric Dumazet 		return __xfrm_sk_clone_policy(sk, osk);
12071da177e4SLinus Torvalds 	return 0;
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds 
1210d511337aSJoe Perches int xfrm_policy_delete(struct xfrm_policy *pol, int dir);
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds static inline void xfrm_sk_free_policy(struct sock *sk)
12131da177e4SLinus Torvalds {
1214d188ba86SEric Dumazet 	struct xfrm_policy *pol;
1215d188ba86SEric Dumazet 
1216d188ba86SEric Dumazet 	pol = rcu_dereference_protected(sk->sk_policy[0], 1);
1217d188ba86SEric Dumazet 	if (unlikely(pol != NULL)) {
1218d188ba86SEric Dumazet 		xfrm_policy_delete(pol, XFRM_POLICY_MAX);
12191da177e4SLinus Torvalds 		sk->sk_policy[0] = NULL;
12201da177e4SLinus Torvalds 	}
1221d188ba86SEric Dumazet 	pol = rcu_dereference_protected(sk->sk_policy[1], 1);
1222d188ba86SEric Dumazet 	if (unlikely(pol != NULL)) {
1223d188ba86SEric Dumazet 		xfrm_policy_delete(pol, XFRM_POLICY_MAX+1);
12241da177e4SLinus Torvalds 		sk->sk_policy[1] = NULL;
12251da177e4SLinus Torvalds 	}
12261da177e4SLinus Torvalds }
12271da177e4SLinus Torvalds 
1228d511337aSJoe Perches void xfrm_garbage_collect(struct net *net);
12293d7d25a6SFlorian Westphal void xfrm_garbage_collect_deferred(struct net *net);
1230e4c17216SPaul Moore 
12311da177e4SLinus Torvalds #else
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds static inline void xfrm_sk_free_policy(struct sock *sk) {}
1234d188ba86SEric Dumazet static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk) { return 0; }
12351da177e4SLinus Torvalds static inline int xfrm6_route_forward(struct sk_buff *skb) { return 1; }
12361da177e4SLinus Torvalds static inline int xfrm4_route_forward(struct sk_buff *skb) { return 1; }
12371da177e4SLinus Torvalds static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
12381da177e4SLinus Torvalds {
12391da177e4SLinus Torvalds 	return 1;
12401da177e4SLinus Torvalds }
12411da177e4SLinus Torvalds static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
12421da177e4SLinus Torvalds {
12431da177e4SLinus Torvalds 	return 1;
12441da177e4SLinus Torvalds }
12451da177e4SLinus Torvalds static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
12461da177e4SLinus Torvalds {
12471da177e4SLinus Torvalds 	return 1;
12481da177e4SLinus Torvalds }
1249d5422efeSHerbert Xu static inline int xfrm_decode_session_reverse(struct sk_buff *skb,
1250d5422efeSHerbert Xu 					      struct flowi *fl,
1251d5422efeSHerbert Xu 					      unsigned int family)
1252d5422efeSHerbert Xu {
1253d5422efeSHerbert Xu 	return -ENOSYS;
1254d5422efeSHerbert Xu }
1255d5422efeSHerbert Xu static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir,
1256d5422efeSHerbert Xu 					     struct sk_buff *skb)
1257d5422efeSHerbert Xu {
1258d5422efeSHerbert Xu 	return 1;
1259d5422efeSHerbert Xu }
1260d5422efeSHerbert Xu static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir,
1261d5422efeSHerbert Xu 					     struct sk_buff *skb)
1262d5422efeSHerbert Xu {
1263d5422efeSHerbert Xu 	return 1;
1264d5422efeSHerbert Xu }
1265e4c17216SPaul Moore static inline void xfrm_garbage_collect(struct net *net)
1266e4c17216SPaul Moore {
1267e4c17216SPaul Moore }
12681da177e4SLinus Torvalds #endif
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds static __inline__
1271e8a4e377SDavid S. Miller xfrm_address_t *xfrm_flowi_daddr(const struct flowi *fl, unsigned short family)
12721da177e4SLinus Torvalds {
12731da177e4SLinus Torvalds 	switch (family){
12741da177e4SLinus Torvalds 	case AF_INET:
12757e1dc7b6SDavid S. Miller 		return (xfrm_address_t *)&fl->u.ip4.daddr;
12761da177e4SLinus Torvalds 	case AF_INET6:
12777e1dc7b6SDavid S. Miller 		return (xfrm_address_t *)&fl->u.ip6.daddr;
12781da177e4SLinus Torvalds 	}
12791da177e4SLinus Torvalds 	return NULL;
12801da177e4SLinus Torvalds }
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds static __inline__
1283e8a4e377SDavid S. Miller xfrm_address_t *xfrm_flowi_saddr(const struct flowi *fl, unsigned short family)
12841da177e4SLinus Torvalds {
12851da177e4SLinus Torvalds 	switch (family){
12861da177e4SLinus Torvalds 	case AF_INET:
12877e1dc7b6SDavid S. Miller 		return (xfrm_address_t *)&fl->u.ip4.saddr;
12881da177e4SLinus Torvalds 	case AF_INET6:
12897e1dc7b6SDavid S. Miller 		return (xfrm_address_t *)&fl->u.ip6.saddr;
12901da177e4SLinus Torvalds 	}
12911da177e4SLinus Torvalds 	return NULL;
12921da177e4SLinus Torvalds }
12931da177e4SLinus Torvalds 
12949bb182a7SYOSHIFUJI Hideaki static __inline__
1295e8a4e377SDavid S. Miller void xfrm_flowi_addr_get(const struct flowi *fl,
12969bb182a7SYOSHIFUJI Hideaki 			 xfrm_address_t *saddr, xfrm_address_t *daddr,
12979bb182a7SYOSHIFUJI Hideaki 			 unsigned short family)
12989bb182a7SYOSHIFUJI Hideaki {
12999bb182a7SYOSHIFUJI Hideaki 	switch(family) {
13009bb182a7SYOSHIFUJI Hideaki 	case AF_INET:
13017e1dc7b6SDavid S. Miller 		memcpy(&saddr->a4, &fl->u.ip4.saddr, sizeof(saddr->a4));
13027e1dc7b6SDavid S. Miller 		memcpy(&daddr->a4, &fl->u.ip4.daddr, sizeof(daddr->a4));
13039bb182a7SYOSHIFUJI Hideaki 		break;
13049bb182a7SYOSHIFUJI Hideaki 	case AF_INET6:
130515e318bdSJiri Benc 		saddr->in6 = fl->u.ip6.saddr;
130615e318bdSJiri Benc 		daddr->in6 = fl->u.ip6.daddr;
13079bb182a7SYOSHIFUJI Hideaki 		break;
13089bb182a7SYOSHIFUJI Hideaki 	}
13099bb182a7SYOSHIFUJI Hideaki }
13109bb182a7SYOSHIFUJI Hideaki 
13111da177e4SLinus Torvalds static __inline__ int
1312f8848067SDavid S. Miller __xfrm4_state_addr_check(const struct xfrm_state *x,
1313f8848067SDavid S. Miller 			 const xfrm_address_t *daddr, const xfrm_address_t *saddr)
13141da177e4SLinus Torvalds {
13151da177e4SLinus Torvalds 	if (daddr->a4 == x->id.daddr.a4 &&
13161da177e4SLinus Torvalds 	    (saddr->a4 == x->props.saddr.a4 || !saddr->a4 || !x->props.saddr.a4))
13171da177e4SLinus Torvalds 		return 1;
13181da177e4SLinus Torvalds 	return 0;
13191da177e4SLinus Torvalds }
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds static __inline__ int
1322f8848067SDavid S. Miller __xfrm6_state_addr_check(const struct xfrm_state *x,
1323f8848067SDavid S. Miller 			 const xfrm_address_t *daddr, const xfrm_address_t *saddr)
13241da177e4SLinus Torvalds {
1325ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal((struct in6_addr *)daddr, (struct in6_addr *)&x->id.daddr) &&
1326ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 	    (ipv6_addr_equal((struct in6_addr *)saddr, (struct in6_addr *)&x->props.saddr) ||
13271da177e4SLinus Torvalds 	     ipv6_addr_any((struct in6_addr *)saddr) ||
13281da177e4SLinus Torvalds 	     ipv6_addr_any((struct in6_addr *)&x->props.saddr)))
13291da177e4SLinus Torvalds 		return 1;
13301da177e4SLinus Torvalds 	return 0;
13311da177e4SLinus Torvalds }
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds static __inline__ int
1334f8848067SDavid S. Miller xfrm_state_addr_check(const struct xfrm_state *x,
1335f8848067SDavid S. Miller 		      const xfrm_address_t *daddr, const xfrm_address_t *saddr,
13361da177e4SLinus Torvalds 		      unsigned short family)
13371da177e4SLinus Torvalds {
13381da177e4SLinus Torvalds 	switch (family) {
13391da177e4SLinus Torvalds 	case AF_INET:
13401da177e4SLinus Torvalds 		return __xfrm4_state_addr_check(x, daddr, saddr);
13411da177e4SLinus Torvalds 	case AF_INET6:
13421da177e4SLinus Torvalds 		return __xfrm6_state_addr_check(x, daddr, saddr);
13431da177e4SLinus Torvalds 	}
13441da177e4SLinus Torvalds 	return 0;
13451da177e4SLinus Torvalds }
13461da177e4SLinus Torvalds 
1347e53820deSMasahide NAKAMURA static __inline__ int
1348f8848067SDavid S. Miller xfrm_state_addr_flow_check(const struct xfrm_state *x, const struct flowi *fl,
1349e53820deSMasahide NAKAMURA 			   unsigned short family)
1350e53820deSMasahide NAKAMURA {
1351e53820deSMasahide NAKAMURA 	switch (family) {
1352e53820deSMasahide NAKAMURA 	case AF_INET:
1353e53820deSMasahide NAKAMURA 		return __xfrm4_state_addr_check(x,
13547e1dc7b6SDavid S. Miller 						(const xfrm_address_t *)&fl->u.ip4.daddr,
13557e1dc7b6SDavid S. Miller 						(const xfrm_address_t *)&fl->u.ip4.saddr);
1356e53820deSMasahide NAKAMURA 	case AF_INET6:
1357e53820deSMasahide NAKAMURA 		return __xfrm6_state_addr_check(x,
13587e1dc7b6SDavid S. Miller 						(const xfrm_address_t *)&fl->u.ip6.daddr,
13597e1dc7b6SDavid S. Miller 						(const xfrm_address_t *)&fl->u.ip6.saddr);
1360e53820deSMasahide NAKAMURA 	}
1361e53820deSMasahide NAKAMURA 	return 0;
1362e53820deSMasahide NAKAMURA }
1363e53820deSMasahide NAKAMURA 
1364f8848067SDavid S. Miller static inline int xfrm_state_kern(const struct xfrm_state *x)
13651da177e4SLinus Torvalds {
13661da177e4SLinus Torvalds 	return atomic_read(&x->tunnel_users);
13671da177e4SLinus Torvalds }
13681da177e4SLinus Torvalds 
13695794708fSMasahide NAKAMURA static inline int xfrm_id_proto_match(u8 proto, u8 userproto)
13705794708fSMasahide NAKAMURA {
1371dc00a525SMasahide NAKAMURA 	return (!userproto || proto == userproto ||
1372dc00a525SMasahide NAKAMURA 		(userproto == IPSEC_PROTO_ANY && (proto == IPPROTO_AH ||
1373dc00a525SMasahide NAKAMURA 						  proto == IPPROTO_ESP ||
1374dc00a525SMasahide NAKAMURA 						  proto == IPPROTO_COMP)));
13755794708fSMasahide NAKAMURA }
13765794708fSMasahide NAKAMURA 
13771da177e4SLinus Torvalds /*
13781da177e4SLinus Torvalds  * xfrm algorithm information
13791da177e4SLinus Torvalds  */
13801a6509d9SHerbert Xu struct xfrm_algo_aead_info {
1381165ecc63SHerbert Xu 	char *geniv;
13821a6509d9SHerbert Xu 	u16 icv_truncbits;
13831a6509d9SHerbert Xu };
13841a6509d9SHerbert Xu 
13851da177e4SLinus Torvalds struct xfrm_algo_auth_info {
13861da177e4SLinus Torvalds 	u16 icv_truncbits;
13871da177e4SLinus Torvalds 	u16 icv_fullbits;
13881da177e4SLinus Torvalds };
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds struct xfrm_algo_encr_info {
1391165ecc63SHerbert Xu 	char *geniv;
13921da177e4SLinus Torvalds 	u16 blockbits;
13931da177e4SLinus Torvalds 	u16 defkeybits;
13941da177e4SLinus Torvalds };
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds struct xfrm_algo_comp_info {
13971da177e4SLinus Torvalds 	u16 threshold;
13981da177e4SLinus Torvalds };
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds struct xfrm_algo_desc {
14011da177e4SLinus Torvalds 	char *name;
140204ff1260SHerbert Xu 	char *compat;
14031da177e4SLinus Torvalds 	u8 available:1;
14047e50f84cSJussi Kivilinna 	u8 pfkey_supported:1;
14051da177e4SLinus Torvalds 	union {
14061a6509d9SHerbert Xu 		struct xfrm_algo_aead_info aead;
14071da177e4SLinus Torvalds 		struct xfrm_algo_auth_info auth;
14081da177e4SLinus Torvalds 		struct xfrm_algo_encr_info encr;
14091da177e4SLinus Torvalds 		struct xfrm_algo_comp_info comp;
14101da177e4SLinus Torvalds 	} uinfo;
14111da177e4SLinus Torvalds 	struct sadb_alg desc;
14121da177e4SLinus Torvalds };
14131da177e4SLinus Torvalds 
14143328715eSSteffen Klassert /* XFRM protocol handlers.  */
14153328715eSSteffen Klassert struct xfrm4_protocol {
14163328715eSSteffen Klassert 	int (*handler)(struct sk_buff *skb);
14173328715eSSteffen Klassert 	int (*input_handler)(struct sk_buff *skb, int nexthdr, __be32 spi,
14183328715eSSteffen Klassert 			     int encap_type);
14193328715eSSteffen Klassert 	int (*cb_handler)(struct sk_buff *skb, int err);
14203328715eSSteffen Klassert 	int (*err_handler)(struct sk_buff *skb, u32 info);
14213328715eSSteffen Klassert 
14223328715eSSteffen Klassert 	struct xfrm4_protocol __rcu *next;
14233328715eSSteffen Klassert 	int priority;
14243328715eSSteffen Klassert };
14253328715eSSteffen Klassert 
14267e14ea15SSteffen Klassert struct xfrm6_protocol {
14277e14ea15SSteffen Klassert 	int (*handler)(struct sk_buff *skb);
14287e14ea15SSteffen Klassert 	int (*cb_handler)(struct sk_buff *skb, int err);
14297e14ea15SSteffen Klassert 	int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
14307e14ea15SSteffen Klassert 			   u8 type, u8 code, int offset, __be32 info);
14317e14ea15SSteffen Klassert 
14327e14ea15SSteffen Klassert 	struct xfrm6_protocol __rcu *next;
14337e14ea15SSteffen Klassert 	int priority;
14347e14ea15SSteffen Klassert };
14357e14ea15SSteffen Klassert 
14361da177e4SLinus Torvalds /* XFRM tunnel handlers.  */
14371da177e4SLinus Torvalds struct xfrm_tunnel {
14381da177e4SLinus Torvalds 	int (*handler)(struct sk_buff *skb);
1439a6337463Sjamal 	int (*err_handler)(struct sk_buff *skb, u32 info);
1440d2acc347SHerbert Xu 
1441b33eab08SEric Dumazet 	struct xfrm_tunnel __rcu *next;
1442d2acc347SHerbert Xu 	int priority;
14431da177e4SLinus Torvalds };
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds struct xfrm6_tunnel {
1446d2acc347SHerbert Xu 	int (*handler)(struct sk_buff *skb);
1447d2acc347SHerbert Xu 	int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
1448d5fdd6baSBrian Haley 			   u8 type, u8 code, int offset, __be32 info);
14496f0bcf15SEric Dumazet 	struct xfrm6_tunnel __rcu *next;
1450d2acc347SHerbert Xu 	int priority;
14511da177e4SLinus Torvalds };
14521da177e4SLinus Torvalds 
1453d511337aSJoe Perches void xfrm_init(void);
1454d511337aSJoe Perches void xfrm4_init(void);
1455d511337aSJoe Perches int xfrm_state_init(struct net *net);
145621f42cc9SSteffen Klassert void xfrm_dev_init(void);
1457d511337aSJoe Perches void xfrm_state_fini(struct net *net);
1458d511337aSJoe Perches void xfrm4_state_init(void);
14592f32b51bSSteffen Klassert void xfrm4_protocol_init(void);
1460c35b7e72SDaniel Lezcano #ifdef CONFIG_XFRM
1461d511337aSJoe Perches int xfrm6_init(void);
1462d511337aSJoe Perches void xfrm6_fini(void);
1463d511337aSJoe Perches int xfrm6_state_init(void);
1464d511337aSJoe Perches void xfrm6_state_fini(void);
14657e14ea15SSteffen Klassert int xfrm6_protocol_init(void);
14667e14ea15SSteffen Klassert void xfrm6_protocol_fini(void);
1467c35b7e72SDaniel Lezcano #else
1468c35b7e72SDaniel Lezcano static inline int xfrm6_init(void)
1469c35b7e72SDaniel Lezcano {
1470c35b7e72SDaniel Lezcano 	return 0;
1471c35b7e72SDaniel Lezcano }
1472c35b7e72SDaniel Lezcano static inline void xfrm6_fini(void)
1473c35b7e72SDaniel Lezcano {
1474c35b7e72SDaniel Lezcano 	;
1475c35b7e72SDaniel Lezcano }
1476c35b7e72SDaniel Lezcano #endif
14771da177e4SLinus Torvalds 
1478558f82efSMasahide NAKAMURA #ifdef CONFIG_XFRM_STATISTICS
1479d511337aSJoe Perches int xfrm_proc_init(struct net *net);
1480d511337aSJoe Perches void xfrm_proc_fini(struct net *net);
1481558f82efSMasahide NAKAMURA #endif
1482558f82efSMasahide NAKAMURA 
1483d511337aSJoe Perches int xfrm_sysctl_init(struct net *net);
1484b27aeadbSAlexey Dobriyan #ifdef CONFIG_SYSCTL
1485d511337aSJoe Perches void xfrm_sysctl_fini(struct net *net);
1486b27aeadbSAlexey Dobriyan #else
1487b27aeadbSAlexey Dobriyan static inline void xfrm_sysctl_fini(struct net *net)
1488b27aeadbSAlexey Dobriyan {
1489b27aeadbSAlexey Dobriyan }
1490b27aeadbSAlexey Dobriyan #endif
1491b27aeadbSAlexey Dobriyan 
1492d3623099SNicolas Dichtel void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
1493870a2df4SNicolas Dichtel 			  struct xfrm_address_filter *filter);
1494d511337aSJoe Perches int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
14954c563f76STimo Teras 		    int (*func)(struct xfrm_state *, int, void*), void *);
1496283bc9f3SFan Du void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net);
1497d511337aSJoe Perches struct xfrm_state *xfrm_state_alloc(struct net *net);
1498d511337aSJoe Perches struct xfrm_state *xfrm_state_find(const xfrm_address_t *daddr,
149933765d06SDavid S. Miller 				   const xfrm_address_t *saddr,
1500b520e9f6SDavid S. Miller 				   const struct flowi *fl,
1501b520e9f6SDavid S. Miller 				   struct xfrm_tmpl *tmpl,
15021da177e4SLinus Torvalds 				   struct xfrm_policy *pol, int *err,
15031da177e4SLinus Torvalds 				   unsigned short family);
1504d511337aSJoe Perches struct xfrm_state *xfrm_stateonly_find(struct net *net, u32 mark,
15055447c5e4SAlexey Dobriyan 				       xfrm_address_t *daddr,
1506628529b6SJamal Hadi Salim 				       xfrm_address_t *saddr,
1507628529b6SJamal Hadi Salim 				       unsigned short family,
1508628529b6SJamal Hadi Salim 				       u8 mode, u8 proto, u32 reqid);
1509c454997eSFan Du struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1510c454997eSFan Du 					      unsigned short family);
1511d511337aSJoe Perches int xfrm_state_check_expire(struct xfrm_state *x);
1512d511337aSJoe Perches void xfrm_state_insert(struct xfrm_state *x);
1513d511337aSJoe Perches int xfrm_state_add(struct xfrm_state *x);
1514d511337aSJoe Perches int xfrm_state_update(struct xfrm_state *x);
1515d511337aSJoe Perches struct xfrm_state *xfrm_state_lookup(struct net *net, u32 mark,
1516a70486f0SDavid S. Miller 				     const xfrm_address_t *daddr, __be32 spi,
1517bd55775cSJamal Hadi Salim 				     u8 proto, unsigned short family);
1518d511337aSJoe Perches struct xfrm_state *xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1519a70486f0SDavid S. Miller 					    const xfrm_address_t *daddr,
1520a70486f0SDavid S. Miller 					    const xfrm_address_t *saddr,
1521bd55775cSJamal Hadi Salim 					    u8 proto,
1522bd55775cSJamal Hadi Salim 					    unsigned short family);
152341a49cc3SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
1524d511337aSJoe Perches int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1525283bc9f3SFan Du 		   unsigned short family, struct net *net);
1526d511337aSJoe Perches int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1527d511337aSJoe Perches 		    unsigned short family);
152841a49cc3SMasahide NAKAMURA #else
152941a49cc3SMasahide NAKAMURA static inline int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src,
1530283bc9f3SFan Du 				 int n, unsigned short family, struct net *net)
153141a49cc3SMasahide NAKAMURA {
153241a49cc3SMasahide NAKAMURA 	return -ENOSYS;
153341a49cc3SMasahide NAKAMURA }
153441a49cc3SMasahide NAKAMURA 
153541a49cc3SMasahide NAKAMURA static inline int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src,
153641a49cc3SMasahide NAKAMURA 				  int n, unsigned short family)
153741a49cc3SMasahide NAKAMURA {
153841a49cc3SMasahide NAKAMURA 	return -ENOSYS;
153941a49cc3SMasahide NAKAMURA }
154041a49cc3SMasahide NAKAMURA #endif
1541af11e316SJamal Hadi Salim 
1542af11e316SJamal Hadi Salim struct xfrmk_sadinfo {
1543af11e316SJamal Hadi Salim 	u32 sadhcnt; /* current hash bkts */
1544af11e316SJamal Hadi Salim 	u32 sadhmcnt; /* max allowed hash bkts */
1545af11e316SJamal Hadi Salim 	u32 sadcnt; /* current running count */
1546af11e316SJamal Hadi Salim };
1547af11e316SJamal Hadi Salim 
15485a6d3416SJamal Hadi Salim struct xfrmk_spdinfo {
15495a6d3416SJamal Hadi Salim 	u32 incnt;
15505a6d3416SJamal Hadi Salim 	u32 outcnt;
15515a6d3416SJamal Hadi Salim 	u32 fwdcnt;
15525a6d3416SJamal Hadi Salim 	u32 inscnt;
15535a6d3416SJamal Hadi Salim 	u32 outscnt;
15545a6d3416SJamal Hadi Salim 	u32 fwdscnt;
15555a6d3416SJamal Hadi Salim 	u32 spdhcnt;
15565a6d3416SJamal Hadi Salim 	u32 spdhmcnt;
15575a6d3416SJamal Hadi Salim };
15585a6d3416SJamal Hadi Salim 
1559d511337aSJoe Perches struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
1560d511337aSJoe Perches int xfrm_state_delete(struct xfrm_state *x);
15612e71029eSTetsuo Handa int xfrm_state_flush(struct net *net, u8 proto, bool task_valid);
1562d511337aSJoe Perches void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si);
1563d511337aSJoe Perches void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si);
1564d511337aSJoe Perches u32 xfrm_replay_seqhi(struct xfrm_state *x, __be32 net_seq);
1565d511337aSJoe Perches int xfrm_init_replay(struct xfrm_state *x);
1566d511337aSJoe Perches int xfrm_state_mtu(struct xfrm_state *x, int mtu);
1567d511337aSJoe Perches int __xfrm_init_state(struct xfrm_state *x, bool init_replay);
1568d511337aSJoe Perches int xfrm_init_state(struct xfrm_state *x);
1569d511337aSJoe Perches int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb);
1570d511337aSJoe Perches int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type);
1571d511337aSJoe Perches int xfrm_input_resume(struct sk_buff *skb, int nexthdr);
1572d511337aSJoe Perches int xfrm_output_resume(struct sk_buff *skb, int err);
15737026b1ddSDavid Miller int xfrm_output(struct sock *sk, struct sk_buff *skb);
1574d511337aSJoe Perches int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb);
1575d511337aSJoe Perches void xfrm_local_error(struct sk_buff *skb, int mtu);
1576d511337aSJoe Perches int xfrm4_extract_header(struct sk_buff *skb);
1577d511337aSJoe Perches int xfrm4_extract_input(struct xfrm_state *x, struct sk_buff *skb);
1578d511337aSJoe Perches int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
1579716062fdSHerbert Xu 		    int encap_type);
1580d511337aSJoe Perches int xfrm4_transport_finish(struct sk_buff *skb, int async);
1581d511337aSJoe Perches int xfrm4_rcv(struct sk_buff *skb);
15821e295370SSteffen Klassert int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq);
1583c4541b41SHerbert Xu 
1584c4541b41SHerbert Xu static inline int xfrm4_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi)
1585c4541b41SHerbert Xu {
158670be6c91SSteffen Klassert 	XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
15873328715eSSteffen Klassert 	XFRM_SPI_SKB_CB(skb)->family = AF_INET;
15883328715eSSteffen Klassert 	XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
15893328715eSSteffen Klassert 	return xfrm_input(skb, nexthdr, spi, 0);
1590c4541b41SHerbert Xu }
1591c4541b41SHerbert Xu 
1592d511337aSJoe Perches int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb);
1593d511337aSJoe Perches int xfrm4_prepare_output(struct xfrm_state *x, struct sk_buff *skb);
1594ede2059dSEric W. Biederman int xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb);
15957026b1ddSDavid Miller int xfrm4_output_finish(struct sock *sk, struct sk_buff *skb);
15963328715eSSteffen Klassert int xfrm4_rcv_cb(struct sk_buff *skb, u8 protocol, int err);
15973328715eSSteffen Klassert int xfrm4_protocol_register(struct xfrm4_protocol *handler, unsigned char protocol);
15983328715eSSteffen Klassert int xfrm4_protocol_deregister(struct xfrm4_protocol *handler, unsigned char protocol);
1599d511337aSJoe Perches int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family);
1600d511337aSJoe Perches int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family);
1601d511337aSJoe Perches void xfrm4_local_error(struct sk_buff *skb, u32 mtu);
1602d511337aSJoe Perches int xfrm6_extract_header(struct sk_buff *skb);
1603d511337aSJoe Perches int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb);
160463c43787SNicolas Dichtel int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
160563c43787SNicolas Dichtel 		  struct ip6_tnl *t);
1606d511337aSJoe Perches int xfrm6_transport_finish(struct sk_buff *skb, int async);
160763c43787SNicolas Dichtel int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t);
1608d511337aSJoe Perches int xfrm6_rcv(struct sk_buff *skb);
1609d511337aSJoe Perches int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
1610fbd9a5b4SMasahide NAKAMURA 		     xfrm_address_t *saddr, u8 proto);
16117b77d161SDavid S. Miller void xfrm6_local_error(struct sk_buff *skb, u32 mtu);
16127e14ea15SSteffen Klassert int xfrm6_rcv_cb(struct sk_buff *skb, u8 protocol, int err);
16137e14ea15SSteffen Klassert int xfrm6_protocol_register(struct xfrm6_protocol *handler, unsigned char protocol);
16147e14ea15SSteffen Klassert int xfrm6_protocol_deregister(struct xfrm6_protocol *handler, unsigned char protocol);
1615d511337aSJoe Perches int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family);
16167b77d161SDavid S. Miller int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family);
1617d511337aSJoe Perches __be32 xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr);
1618d511337aSJoe Perches __be32 xfrm6_tunnel_spi_lookup(struct net *net, const xfrm_address_t *saddr);
1619d511337aSJoe Perches int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb);
1620d511337aSJoe Perches int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb);
1621ede2059dSEric W. Biederman int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb);
16227026b1ddSDavid Miller int xfrm6_output_finish(struct sock *sk, struct sk_buff *skb);
1623d511337aSJoe Perches int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb,
1624aee5adb4SMasahide NAKAMURA 			  u8 **prevhdr);
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds #ifdef CONFIG_XFRM
1627d511337aSJoe Perches int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb);
1628d511337aSJoe Perches int xfrm_user_policy(struct sock *sk, int optname,
1629d511337aSJoe Perches 		     u8 __user *optval, int optlen);
16301da177e4SLinus Torvalds #else
16311da177e4SLinus Torvalds static inline int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
16321da177e4SLinus Torvalds {
16331da177e4SLinus Torvalds  	return -ENOPROTOOPT;
16341da177e4SLinus Torvalds }
16351da177e4SLinus Torvalds 
1636067b207bSJames Chapman static inline int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
16371da177e4SLinus Torvalds {
16381da177e4SLinus Torvalds  	/* should not happen */
16391da177e4SLinus Torvalds  	kfree_skb(skb);
16401da177e4SLinus Torvalds 	return 0;
16411da177e4SLinus Torvalds }
16421da177e4SLinus Torvalds #endif
16431da177e4SLinus Torvalds 
16440331b1f3SAlexey Dobriyan struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp);
16454c563f76STimo Teras 
1646d511337aSJoe Perches void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type);
1647d511337aSJoe Perches int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
1648d511337aSJoe Perches 		     int (*func)(struct xfrm_policy *, int, int, void*),
1649d511337aSJoe Perches 		     void *);
1650283bc9f3SFan Du void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net);
16511da177e4SLinus Torvalds int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl);
16528ca2e93bSJamal Hadi Salim struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark,
16538ca2e93bSJamal Hadi Salim 					  u8 type, int dir,
16544e81bb83SMasahide NAKAMURA 					  struct xfrm_selector *sel,
1655ef41aaa0SEric Paris 					  struct xfrm_sec_ctx *ctx, int delete,
1656ef41aaa0SEric Paris 					  int *err);
1657d511337aSJoe Perches struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8, int dir,
1658d511337aSJoe Perches 				     u32 id, int delete, int *err);
16592e71029eSTetsuo Handa int xfrm_policy_flush(struct net *net, u8 type, bool task_valid);
1660880a6fabSChristophe Gouault void xfrm_policy_hash_rebuild(struct net *net);
16611da177e4SLinus Torvalds u32 xfrm_get_acqseq(void);
1662776e9dd9SFan Du int verify_spi_info(u8 proto, u32 min, u32 max);
1663d511337aSJoe Perches int xfrm_alloc_spi(struct xfrm_state *x, u32 minspi, u32 maxspi);
1664e473fcb4SMathias Krause struct xfrm_state *xfrm_find_acq(struct net *net, const struct xfrm_mark *mark,
1665bd55775cSJamal Hadi Salim 				 u8 mode, u32 reqid, u8 proto,
1666a70486f0SDavid S. Miller 				 const xfrm_address_t *daddr,
1667a70486f0SDavid S. Miller 				 const xfrm_address_t *saddr, int create,
1668bd55775cSJamal Hadi Salim 				 unsigned short family);
1669d511337aSJoe Perches int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol);
16701da177e4SLinus Torvalds 
167180c9abaaSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
1672d511337aSJoe Perches int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1673183cad12SDavid S. Miller 	       const struct xfrm_migrate *m, int num_bundles,
1674183cad12SDavid S. Miller 	       const struct xfrm_kmaddress *k);
1675283bc9f3SFan Du struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net);
1676d511337aSJoe Perches struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
167780c9abaaSShinta Sugimoto 				      struct xfrm_migrate *m);
1678d511337aSJoe Perches int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
167913c1d189SArnaud Ebalard 		 struct xfrm_migrate *m, int num_bundles,
16808d549c4fSFan Du 		 struct xfrm_kmaddress *k, struct net *net);
168180c9abaaSShinta Sugimoto #endif
168280c9abaaSShinta Sugimoto 
1683d511337aSJoe Perches int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport);
1684d511337aSJoe Perches void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid);
1685d511337aSJoe Perches int km_report(struct net *net, u8 proto, struct xfrm_selector *sel,
1686d511337aSJoe Perches 	      xfrm_address_t *addr);
16871da177e4SLinus Torvalds 
1688d511337aSJoe Perches void xfrm_input_init(void);
1689d511337aSJoe Perches int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq);
16901da177e4SLinus Torvalds 
1691d511337aSJoe Perches void xfrm_probe_algs(void);
1692d511337aSJoe Perches int xfrm_count_pfkey_auth_supported(void);
1693d511337aSJoe Perches int xfrm_count_pfkey_enc_supported(void);
1694d511337aSJoe Perches struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx);
1695d511337aSJoe Perches struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx);
1696d511337aSJoe Perches struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id);
1697d511337aSJoe Perches struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id);
1698d511337aSJoe Perches struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id);
1699d511337aSJoe Perches struct xfrm_algo_desc *xfrm_aalg_get_byname(const char *name, int probe);
1700d511337aSJoe Perches struct xfrm_algo_desc *xfrm_ealg_get_byname(const char *name, int probe);
1701d511337aSJoe Perches struct xfrm_algo_desc *xfrm_calg_get_byname(const char *name, int probe);
1702d511337aSJoe Perches struct xfrm_algo_desc *xfrm_aead_get_byname(const char *name, int icv_len,
17031a6509d9SHerbert Xu 					    int probe);
17041da177e4SLinus Torvalds 
1705ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 static inline bool xfrm6_addr_equal(const xfrm_address_t *a,
1706ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 				    const xfrm_address_t *b)
1707ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 {
1708ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 	return ipv6_addr_equal((const struct in6_addr *)a,
1709ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 			       (const struct in6_addr *)b);
1710ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 }
1711ff88b30cSYOSHIFUJI Hideaki / 吉藤英明 
171270e94e66SYOSHIFUJI Hideaki / 吉藤英明 static inline bool xfrm_addr_equal(const xfrm_address_t *a,
171370e94e66SYOSHIFUJI Hideaki / 吉藤英明 				   const xfrm_address_t *b,
171470e94e66SYOSHIFUJI Hideaki / 吉藤英明 				   sa_family_t family)
171570e94e66SYOSHIFUJI Hideaki / 吉藤英明 {
171670e94e66SYOSHIFUJI Hideaki / 吉藤英明 	switch (family) {
171770e94e66SYOSHIFUJI Hideaki / 吉藤英明 	default:
171870e94e66SYOSHIFUJI Hideaki / 吉藤英明 	case AF_INET:
171970e94e66SYOSHIFUJI Hideaki / 吉藤英明 		return ((__force u32)a->a4 ^ (__force u32)b->a4) == 0;
172070e94e66SYOSHIFUJI Hideaki / 吉藤英明 	case AF_INET6:
172170e94e66SYOSHIFUJI Hideaki / 吉藤英明 		return xfrm6_addr_equal(a, b);
172270e94e66SYOSHIFUJI Hideaki / 吉藤英明 	}
172370e94e66SYOSHIFUJI Hideaki / 吉藤英明 }
172470e94e66SYOSHIFUJI Hideaki / 吉藤英明 
172577d8d7a6SHerbert Xu static inline int xfrm_policy_id2dir(u32 index)
172677d8d7a6SHerbert Xu {
172777d8d7a6SHerbert Xu 	return index & 7;
172877d8d7a6SHerbert Xu }
172977d8d7a6SHerbert Xu 
1730a6483b79SAlexey Dobriyan #ifdef CONFIG_XFRM
1731a6483b79SAlexey Dobriyan static inline int xfrm_aevent_is_on(struct net *net)
1732f8cd5488SJamal Hadi Salim {
1733be33690dSPatrick McHardy 	struct sock *nlsk;
1734be33690dSPatrick McHardy 	int ret = 0;
1735be33690dSPatrick McHardy 
1736be33690dSPatrick McHardy 	rcu_read_lock();
1737a6483b79SAlexey Dobriyan 	nlsk = rcu_dereference(net->xfrm.nlsk);
1738be33690dSPatrick McHardy 	if (nlsk)
1739be33690dSPatrick McHardy 		ret = netlink_has_listeners(nlsk, XFRMNLGRP_AEVENTS);
1740be33690dSPatrick McHardy 	rcu_read_unlock();
1741be33690dSPatrick McHardy 	return ret;
1742f8cd5488SJamal Hadi Salim }
17430f24558eSHoria Geanta 
17440f24558eSHoria Geanta static inline int xfrm_acquire_is_on(struct net *net)
17450f24558eSHoria Geanta {
17460f24558eSHoria Geanta 	struct sock *nlsk;
17470f24558eSHoria Geanta 	int ret = 0;
17480f24558eSHoria Geanta 
17490f24558eSHoria Geanta 	rcu_read_lock();
17500f24558eSHoria Geanta 	nlsk = rcu_dereference(net->xfrm.nlsk);
17510f24558eSHoria Geanta 	if (nlsk)
17520f24558eSHoria Geanta 		ret = netlink_has_listeners(nlsk, XFRMNLGRP_ACQUIRE);
17530f24558eSHoria Geanta 	rcu_read_unlock();
17540f24558eSHoria Geanta 
17550f24558eSHoria Geanta 	return ret;
17560f24558eSHoria Geanta }
1757a6483b79SAlexey Dobriyan #endif
1758f8cd5488SJamal Hadi Salim 
1759ee5c2317SSteffen Klassert static inline int aead_len(struct xfrm_algo_aead *alg)
1760ee5c2317SSteffen Klassert {
1761ee5c2317SSteffen Klassert 	return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
1762ee5c2317SSteffen Klassert }
1763ee5c2317SSteffen Klassert 
176485158621SDavid S. Miller static inline int xfrm_alg_len(const struct xfrm_algo *alg)
17650f99be0dSEric Dumazet {
17660f99be0dSEric Dumazet 	return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
17670f99be0dSEric Dumazet }
17680f99be0dSEric Dumazet 
176985158621SDavid S. Miller static inline int xfrm_alg_auth_len(const struct xfrm_algo_auth *alg)
17704447bb33SMartin Willi {
17714447bb33SMartin Willi 	return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
17724447bb33SMartin Willi }
17734447bb33SMartin Willi 
17749736acf3SSteffen Klassert static inline int xfrm_replay_state_esn_len(struct xfrm_replay_state_esn *replay_esn)
17759736acf3SSteffen Klassert {
17769736acf3SSteffen Klassert 	return sizeof(*replay_esn) + replay_esn->bmp_len * sizeof(__u32);
17779736acf3SSteffen Klassert }
17789736acf3SSteffen Klassert 
177980c9abaaSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
1780af2f464eSSteffen Klassert static inline int xfrm_replay_clone(struct xfrm_state *x,
1781af2f464eSSteffen Klassert 				     struct xfrm_state *orig)
1782af2f464eSSteffen Klassert {
1783af2f464eSSteffen Klassert 	x->replay_esn = kzalloc(xfrm_replay_state_esn_len(orig->replay_esn),
1784af2f464eSSteffen Klassert 				GFP_KERNEL);
1785af2f464eSSteffen Klassert 	if (!x->replay_esn)
1786af2f464eSSteffen Klassert 		return -ENOMEM;
1787af2f464eSSteffen Klassert 
1788af2f464eSSteffen Klassert 	x->replay_esn->bmp_len = orig->replay_esn->bmp_len;
1789af2f464eSSteffen Klassert 	x->replay_esn->replay_window = orig->replay_esn->replay_window;
1790af2f464eSSteffen Klassert 
1791af2f464eSSteffen Klassert 	x->preplay_esn = kmemdup(x->replay_esn,
1792af2f464eSSteffen Klassert 				 xfrm_replay_state_esn_len(x->replay_esn),
1793af2f464eSSteffen Klassert 				 GFP_KERNEL);
1794af2f464eSSteffen Klassert 	if (!x->preplay_esn) {
1795af2f464eSSteffen Klassert 		kfree(x->replay_esn);
1796af2f464eSSteffen Klassert 		return -ENOMEM;
1797af2f464eSSteffen Klassert 	}
1798af2f464eSSteffen Klassert 
1799af2f464eSSteffen Klassert 	return 0;
1800af2f464eSSteffen Klassert }
1801af2f464eSSteffen Klassert 
1802ee5c2317SSteffen Klassert static inline struct xfrm_algo_aead *xfrm_algo_aead_clone(struct xfrm_algo_aead *orig)
1803ee5c2317SSteffen Klassert {
1804ee5c2317SSteffen Klassert 	return kmemdup(orig, aead_len(orig), GFP_KERNEL);
1805ee5c2317SSteffen Klassert }
1806ee5c2317SSteffen Klassert 
1807ee5c2317SSteffen Klassert 
180880c9abaaSShinta Sugimoto static inline struct xfrm_algo *xfrm_algo_clone(struct xfrm_algo *orig)
180980c9abaaSShinta Sugimoto {
18100f99be0dSEric Dumazet 	return kmemdup(orig, xfrm_alg_len(orig), GFP_KERNEL);
181180c9abaaSShinta Sugimoto }
181280c9abaaSShinta Sugimoto 
18134447bb33SMartin Willi static inline struct xfrm_algo_auth *xfrm_algo_auth_clone(struct xfrm_algo_auth *orig)
18144447bb33SMartin Willi {
18154447bb33SMartin Willi 	return kmemdup(orig, xfrm_alg_auth_len(orig), GFP_KERNEL);
18164447bb33SMartin Willi }
18174447bb33SMartin Willi 
181880c9abaaSShinta Sugimoto static inline void xfrm_states_put(struct xfrm_state **states, int n)
181980c9abaaSShinta Sugimoto {
182080c9abaaSShinta Sugimoto 	int i;
182180c9abaaSShinta Sugimoto 	for (i = 0; i < n; i++)
182280c9abaaSShinta Sugimoto 		xfrm_state_put(*(states + i));
182380c9abaaSShinta Sugimoto }
182480c9abaaSShinta Sugimoto 
182580c9abaaSShinta Sugimoto static inline void xfrm_states_delete(struct xfrm_state **states, int n)
182680c9abaaSShinta Sugimoto {
182780c9abaaSShinta Sugimoto 	int i;
182880c9abaaSShinta Sugimoto 	for (i = 0; i < n; i++)
182980c9abaaSShinta Sugimoto 		xfrm_state_delete(*(states + i));
183080c9abaaSShinta Sugimoto }
183180c9abaaSShinta Sugimoto #endif
1832f8cd5488SJamal Hadi Salim 
1833def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
183400501121SHerbert Xu static inline struct xfrm_state *xfrm_input_state(struct sk_buff *skb)
183500501121SHerbert Xu {
183600501121SHerbert Xu 	return skb->sp->xvec[skb->sp->len - 1];
183700501121SHerbert Xu }
183854ef207aSSteffen Klassert static inline struct xfrm_offload *xfrm_offload(struct sk_buff *skb)
183954ef207aSSteffen Klassert {
184054ef207aSSteffen Klassert 	struct sec_path *sp = skb->sp;
184154ef207aSSteffen Klassert 
184254ef207aSSteffen Klassert 	if (!sp || !sp->olen || sp->len != sp->olen)
184354ef207aSSteffen Klassert 		return NULL;
184454ef207aSSteffen Klassert 
184554ef207aSSteffen Klassert 	return &sp->ovec[sp->olen - 1];
184654ef207aSSteffen Klassert }
1847def8b4faSAlexey Dobriyan #endif
184800501121SHerbert Xu 
1849bf825f81SJamal Hadi Salim static inline int xfrm_mark_get(struct nlattr **attrs, struct xfrm_mark *m)
1850bf825f81SJamal Hadi Salim {
1851bf825f81SJamal Hadi Salim 	if (attrs[XFRMA_MARK])
18524efd7e83SAndreas Steffen 		memcpy(m, nla_data(attrs[XFRMA_MARK]), sizeof(struct xfrm_mark));
1853bf825f81SJamal Hadi Salim 	else
1854bf825f81SJamal Hadi Salim 		m->v = m->m = 0;
1855bf825f81SJamal Hadi Salim 
1856bf825f81SJamal Hadi Salim 	return m->v & m->m;
1857bf825f81SJamal Hadi Salim }
1858bf825f81SJamal Hadi Salim 
1859e3dfa389SDavid S. Miller static inline int xfrm_mark_put(struct sk_buff *skb, const struct xfrm_mark *m)
1860bf825f81SJamal Hadi Salim {
18611d1e34ddSDavid S. Miller 	int ret = 0;
1862bf825f81SJamal Hadi Salim 
18631d1e34ddSDavid S. Miller 	if (m->m | m->v)
18641d1e34ddSDavid S. Miller 		ret = nla_put(skb, XFRMA_MARK, sizeof(struct xfrm_mark), m);
18651d1e34ddSDavid S. Miller 	return ret;
1866bf825f81SJamal Hadi Salim }
1867bf825f81SJamal Hadi Salim 
186870be6c91SSteffen Klassert static inline int xfrm_tunnel_check(struct sk_buff *skb, struct xfrm_state *x,
186970be6c91SSteffen Klassert 				    unsigned int family)
187070be6c91SSteffen Klassert {
187170be6c91SSteffen Klassert 	bool tunnel = false;
187270be6c91SSteffen Klassert 
187370be6c91SSteffen Klassert 	switch(family) {
187470be6c91SSteffen Klassert 	case AF_INET:
187570be6c91SSteffen Klassert 		if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
187670be6c91SSteffen Klassert 			tunnel = true;
187770be6c91SSteffen Klassert 		break;
187870be6c91SSteffen Klassert 	case AF_INET6:
187970be6c91SSteffen Klassert 		if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
188070be6c91SSteffen Klassert 			tunnel = true;
188170be6c91SSteffen Klassert 		break;
188270be6c91SSteffen Klassert 	}
188370be6c91SSteffen Klassert 	if (tunnel && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL))
188470be6c91SSteffen Klassert 		return -EINVAL;
188570be6c91SSteffen Klassert 
188670be6c91SSteffen Klassert 	return 0;
188770be6c91SSteffen Klassert }
18881da177e4SLinus Torvalds #endif	/* _NET_XFRM_H */
1889