xref: /openbmc/linux/include/net/xfrm.h (revision 6281dcc9)
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)
4859c9940eSAlexey Dobriyan #define XFRM_INC_STATS_BH(net, field)	SNMP_INC_STATS_BH((net)->mib.xfrm_statistics, field)
4959c9940eSAlexey Dobriyan #define XFRM_INC_STATS_USER(net, field)	SNMP_INC_STATS_USER((net)-mib.xfrm_statistics, field)
50558f82efSMasahide NAKAMURA #else
5159c9940eSAlexey Dobriyan #define XFRM_INC_STATS(net, field)	((void)(net))
5259c9940eSAlexey Dobriyan #define XFRM_INC_STATS_BH(net, field)	((void)(net))
5359c9940eSAlexey Dobriyan #define XFRM_INC_STATS_USER(net, field)	((void)(net))
54558f82efSMasahide NAKAMURA #endif
55558f82efSMasahide NAKAMURA 
564a3e2f71SArjan van de Ven extern struct mutex xfrm_cfg_mutex;
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds /* Organization of SPD aka "XFRM rules"
591da177e4SLinus Torvalds    ------------------------------------
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds    Basic objects:
621da177e4SLinus Torvalds    - policy rule, struct xfrm_policy (=SPD entry)
631da177e4SLinus Torvalds    - bundle of transformations, struct dst_entry == struct xfrm_dst (=SA bundle)
641da177e4SLinus Torvalds    - instance of a transformer, struct xfrm_state (=SA)
651da177e4SLinus Torvalds    - template to clone xfrm_state, struct xfrm_tmpl
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds    SPD is plain linear list of xfrm_policy rules, ordered by priority.
681da177e4SLinus Torvalds    (To be compatible with existing pfkeyv2 implementations,
691da177e4SLinus Torvalds    many rules with priority of 0x7fffffff are allowed to exist and
701da177e4SLinus Torvalds    such rules are ordered in an unpredictable way, thanks to bsd folks.)
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds    Lookup is plain linear search until the first match with selector.
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds    If "action" is "block", then we prohibit the flow, otherwise:
751da177e4SLinus Torvalds    if "xfrms_nr" is zero, the flow passes untransformed. Otherwise,
761da177e4SLinus Torvalds    policy entry has list of up to XFRM_MAX_DEPTH transformations,
771da177e4SLinus Torvalds    described by templates xfrm_tmpl. Each template is resolved
781da177e4SLinus Torvalds    to a complete xfrm_state (see below) and we pack bundle of transformations
791da177e4SLinus Torvalds    to a dst_entry returned to requestor.
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds    dst -. xfrm  .-> xfrm_state #1
821da177e4SLinus Torvalds     |---. child .-> dst -. xfrm .-> xfrm_state #2
831da177e4SLinus Torvalds                      |---. child .-> dst -. xfrm .-> xfrm_state #3
841da177e4SLinus Torvalds                                       |---. child .-> NULL
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds    Bundles are cached at xrfm_policy struct (field ->bundles).
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds    Resolution of xrfm_tmpl
901da177e4SLinus Torvalds    -----------------------
911da177e4SLinus Torvalds    Template contains:
921da177e4SLinus Torvalds    1. ->mode		Mode: transport or tunnel
931da177e4SLinus Torvalds    2. ->id.proto	Protocol: AH/ESP/IPCOMP
941da177e4SLinus Torvalds    3. ->id.daddr	Remote tunnel endpoint, ignored for transport mode.
951da177e4SLinus Torvalds       Q: allow to resolve security gateway?
961da177e4SLinus Torvalds    4. ->id.spi          If not zero, static SPI.
971da177e4SLinus Torvalds    5. ->saddr		Local tunnel endpoint, ignored for transport mode.
981da177e4SLinus Torvalds    6. ->algos		List of allowed algos. Plain bitmask now.
991da177e4SLinus Torvalds       Q: ealgos, aalgos, calgos. What a mess...
1001da177e4SLinus Torvalds    7. ->share		Sharing mode.
1011da177e4SLinus Torvalds       Q: how to implement private sharing mode? To add struct sock* to
1021da177e4SLinus Torvalds       flow id?
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds    Having this template we search through SAD searching for entries
1051da177e4SLinus Torvalds    with appropriate mode/proto/algo, permitted by selector.
1061da177e4SLinus Torvalds    If no appropriate entry found, it is requested from key manager.
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds    PROBLEMS:
1091da177e4SLinus Torvalds    Q: How to find all the bundles referring to a physical path for
1101da177e4SLinus Torvalds       PMTU discovery? Seems, dst should contain list of all parents...
1111da177e4SLinus Torvalds       and enter to infinite locking hierarchy disaster.
1121da177e4SLinus Torvalds       No! It is easier, we will not search for them, let them find us.
1131da177e4SLinus Torvalds       We add genid to each dst plus pointer to genid of raw IP route,
1141da177e4SLinus Torvalds       pmtu disc will update pmtu on raw IP route and increase its genid.
1151da177e4SLinus Torvalds       dst_check() will see this for top level and trigger resyncing
1161da177e4SLinus Torvalds       metrics. Plus, it will be made via sk->sk_dst_cache. Solved.
1171da177e4SLinus Torvalds  */
1181da177e4SLinus Torvalds 
11912a169e7SHerbert Xu struct xfrm_state_walk {
12012a169e7SHerbert Xu 	struct list_head	all;
12112a169e7SHerbert Xu 	u8			state;
12212a169e7SHerbert Xu 	union {
12312a169e7SHerbert Xu 		u8		dying;
12412a169e7SHerbert Xu 		u8		proto;
12512a169e7SHerbert Xu 	};
12612a169e7SHerbert Xu 	u32			seq;
12712a169e7SHerbert Xu };
12812a169e7SHerbert Xu 
1291da177e4SLinus Torvalds /* Full description of state of transformer. */
130fd2c3ef7SEric Dumazet struct xfrm_state {
131673c09beSAlexey Dobriyan #ifdef CONFIG_NET_NS
132673c09beSAlexey Dobriyan 	struct net		*xs_net;
133673c09beSAlexey Dobriyan #endif
134abb81c4fSHerbert Xu 	union {
13512a169e7SHerbert Xu 		struct hlist_node	gclist;
1368f126e37SDavid S. Miller 		struct hlist_node	bydst;
137abb81c4fSHerbert Xu 	};
1388f126e37SDavid S. Miller 	struct hlist_node	bysrc;
1398f126e37SDavid S. Miller 	struct hlist_node	byspi;
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	atomic_t		refcnt;
1421da177e4SLinus Torvalds 	spinlock_t		lock;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	struct xfrm_id		id;
1451da177e4SLinus Torvalds 	struct xfrm_selector	sel;
146bf825f81SJamal Hadi Salim 	struct xfrm_mark	mark;
14735d2856bSMartin Willi 	u32			tfcpad;
1481da177e4SLinus Torvalds 
1499d4a706dSDavid S. Miller 	u32			genid;
1509d4a706dSDavid S. Miller 
15112a169e7SHerbert Xu 	/* Key manager bits */
15212a169e7SHerbert Xu 	struct xfrm_state_walk	km;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	/* Parameters of this state. */
1551da177e4SLinus Torvalds 	struct {
1561da177e4SLinus Torvalds 		u32		reqid;
1571da177e4SLinus Torvalds 		u8		mode;
1581da177e4SLinus Torvalds 		u8		replay_window;
1591da177e4SLinus Torvalds 		u8		aalgo, ealgo, calgo;
1601da177e4SLinus Torvalds 		u8		flags;
1611da177e4SLinus Torvalds 		u16		family;
1621da177e4SLinus Torvalds 		xfrm_address_t	saddr;
1631da177e4SLinus Torvalds 		int		header_len;
1641da177e4SLinus Torvalds 		int		trailer_len;
1651da177e4SLinus Torvalds 	} props;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	struct xfrm_lifetime_cfg lft;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	/* Data for transformer */
1704447bb33SMartin Willi 	struct xfrm_algo_auth	*aalg;
1711da177e4SLinus Torvalds 	struct xfrm_algo	*ealg;
1721da177e4SLinus Torvalds 	struct xfrm_algo	*calg;
1731a6509d9SHerbert Xu 	struct xfrm_algo_aead	*aead;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	/* Data for encapsulator */
1761da177e4SLinus Torvalds 	struct xfrm_encap_tmpl	*encap;
1771da177e4SLinus Torvalds 
178060f02a3SNoriaki TAKAMIYA 	/* Data for care-of address */
179060f02a3SNoriaki TAKAMIYA 	xfrm_address_t	*coaddr;
180060f02a3SNoriaki TAKAMIYA 
1811da177e4SLinus Torvalds 	/* IPComp needs an IPIP tunnel for handling uncompressed packets */
1821da177e4SLinus Torvalds 	struct xfrm_state	*tunnel;
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	/* If a tunnel, number of users + 1 */
1851da177e4SLinus Torvalds 	atomic_t		tunnel_users;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	/* State for replay detection */
1881da177e4SLinus Torvalds 	struct xfrm_replay_state replay;
1891da177e4SLinus Torvalds 
190f8cd5488SJamal Hadi Salim 	/* Replay detection state at the time we sent the last notification */
191f8cd5488SJamal Hadi Salim 	struct xfrm_replay_state preplay;
192f8cd5488SJamal Hadi Salim 
1932717096aSJamal Hadi Salim 	/* internal flag that only holds state for delayed aevent at the
1942717096aSJamal Hadi Salim 	 * moment
1952717096aSJamal Hadi Salim 	*/
1962717096aSJamal Hadi Salim 	u32			xflags;
1972717096aSJamal Hadi Salim 
198f8cd5488SJamal Hadi Salim 	/* Replay detection notification settings */
199f8cd5488SJamal Hadi Salim 	u32			replay_maxage;
200f8cd5488SJamal Hadi Salim 	u32			replay_maxdiff;
201f8cd5488SJamal Hadi Salim 
202f8cd5488SJamal Hadi Salim 	/* Replay detection notification timer */
203f8cd5488SJamal Hadi Salim 	struct timer_list	rtimer;
204f8cd5488SJamal Hadi Salim 
2051da177e4SLinus Torvalds 	/* Statistics */
2061da177e4SLinus Torvalds 	struct xfrm_stats	stats;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	struct xfrm_lifetime_cur curlft;
2099e0d57fdSYury Polyanskiy 	struct tasklet_hrtimer	mtimer;
2101da177e4SLinus Torvalds 
2119afaca05SMasahide NAKAMURA 	/* Last used time */
212d26f3984SHerbert Xu 	unsigned long		lastused;
2139afaca05SMasahide NAKAMURA 
2141da177e4SLinus Torvalds 	/* Reference to data common to all the instances of this
2151da177e4SLinus Torvalds 	 * transformer. */
216533cb5b0SEric Dumazet 	const struct xfrm_type	*type;
21713996378SHerbert Xu 	struct xfrm_mode	*inner_mode;
218df9dcb45SKazunori MIYAZAWA 	struct xfrm_mode	*inner_mode_iaf;
21913996378SHerbert Xu 	struct xfrm_mode	*outer_mode;
2201da177e4SLinus Torvalds 
221df71837dSTrent Jaeger 	/* Security context */
222df71837dSTrent Jaeger 	struct xfrm_sec_ctx	*security;
223df71837dSTrent Jaeger 
2241da177e4SLinus Torvalds 	/* Private data of this transformer, format is opaque,
2251da177e4SLinus Torvalds 	 * interpreted by xfrm_type methods. */
2261da177e4SLinus Torvalds 	void			*data;
2271da177e4SLinus Torvalds };
2281da177e4SLinus Torvalds 
229673c09beSAlexey Dobriyan static inline struct net *xs_net(struct xfrm_state *x)
230673c09beSAlexey Dobriyan {
231673c09beSAlexey Dobriyan 	return read_pnet(&x->xs_net);
232673c09beSAlexey Dobriyan }
233673c09beSAlexey Dobriyan 
2342717096aSJamal Hadi Salim /* xflags - make enum if more show up */
2352717096aSJamal Hadi Salim #define XFRM_TIME_DEFER	1
2362717096aSJamal Hadi Salim 
2371da177e4SLinus Torvalds enum {
2381da177e4SLinus Torvalds 	XFRM_STATE_VOID,
2391da177e4SLinus Torvalds 	XFRM_STATE_ACQ,
2401da177e4SLinus Torvalds 	XFRM_STATE_VALID,
2411da177e4SLinus Torvalds 	XFRM_STATE_ERROR,
2421da177e4SLinus Torvalds 	XFRM_STATE_EXPIRED,
2431da177e4SLinus Torvalds 	XFRM_STATE_DEAD
2441da177e4SLinus Torvalds };
2451da177e4SLinus Torvalds 
24626b15dadSJamal Hadi Salim /* callback structure passed from either netlink or pfkey */
247fd2c3ef7SEric Dumazet struct km_event {
248bf08867fSHerbert Xu 	union {
249bf08867fSHerbert Xu 		u32 hard;
250bf08867fSHerbert Xu 		u32 proto;
251bf08867fSHerbert Xu 		u32 byid;
252f8cd5488SJamal Hadi Salim 		u32 aevent;
253f7b6983fSMasahide NAKAMURA 		u32 type;
254bf08867fSHerbert Xu 	} data;
255bf08867fSHerbert Xu 
25626b15dadSJamal Hadi Salim 	u32	seq;
25726b15dadSJamal Hadi Salim 	u32	pid;
25826b15dadSJamal Hadi Salim 	u32	event;
2597067802eSAlexey Dobriyan 	struct net *net;
26026b15dadSJamal Hadi Salim };
26126b15dadSJamal Hadi Salim 
26225ee3286SHerbert Xu struct net_device;
2631da177e4SLinus Torvalds struct xfrm_type;
2641da177e4SLinus Torvalds struct xfrm_dst;
2651da177e4SLinus Torvalds struct xfrm_policy_afinfo {
2661da177e4SLinus Torvalds 	unsigned short		family;
2671da177e4SLinus Torvalds 	struct dst_ops		*dst_ops;
268ddcfd796SAlexey Dobriyan 	void			(*garbage_collect)(struct net *net);
269c5b3cf46SAlexey Dobriyan 	struct dst_entry	*(*dst_lookup)(struct net *net, int tos,
2705e6b930fSDavid S. Miller 					       const xfrm_address_t *saddr,
2715e6b930fSDavid S. Miller 					       const xfrm_address_t *daddr);
272fbda33b2SAlexey Dobriyan 	int			(*get_saddr)(struct net *net, xfrm_address_t *saddr, xfrm_address_t *daddr);
2731da177e4SLinus Torvalds 	void			(*decode_session)(struct sk_buff *skb,
274d5422efeSHerbert Xu 						  struct flowi *fl,
275d5422efeSHerbert Xu 						  int reverse);
27605d84025SDavid S. Miller 	int			(*get_tos)(const struct flowi *fl);
277a1b05140SMasahide NAKAMURA 	int			(*init_path)(struct xfrm_dst *path,
278a1b05140SMasahide NAKAMURA 					     struct dst_entry *dst,
279a1b05140SMasahide NAKAMURA 					     int nfheader_len);
28025ee3286SHerbert Xu 	int			(*fill_dst)(struct xfrm_dst *xdst,
28187c1e12bSHerbert Xu 					    struct net_device *dev,
2820c7b3eefSDavid S. Miller 					    const struct flowi *fl);
2832774c131SDavid S. Miller 	struct dst_entry	*(*blackhole_route)(struct net *net, struct dst_entry *orig);
2841da177e4SLinus Torvalds };
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds extern int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo);
2871da177e4SLinus Torvalds extern int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo);
288214e005bSDavid S. Miller extern void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c);
289214e005bSDavid S. Miller extern void km_state_notify(struct xfrm_state *x, const struct km_event *c);
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds struct xfrm_tmpl;
292980ebd25SJamal Hadi Salim extern int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
29353bc6b4dSJamal Hadi Salim extern void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
29453bc6b4dSJamal Hadi Salim extern int __xfrm_state_delete(struct xfrm_state *x);
29553bc6b4dSJamal Hadi Salim 
2961da177e4SLinus Torvalds struct xfrm_state_afinfo {
29717c2a42aSHerbert Xu 	unsigned int		family;
29836cf9acfSHerbert Xu 	unsigned int		proto;
2998e3d716cSAl Viro 	__be16			eth_proto;
30017c2a42aSHerbert Xu 	struct module		*owner;
301533cb5b0SEric Dumazet 	const struct xfrm_type	*type_map[IPPROTO_MAX];
302aa5d62ccSHerbert Xu 	struct xfrm_mode	*mode_map[XFRM_MODE_MAX];
303d094cd83SHerbert Xu 	int			(*init_flags)(struct xfrm_state *x);
30473e5ebb2SDavid S. Miller 	void			(*init_tempsel)(struct xfrm_selector *sel,
30573e5ebb2SDavid S. Miller 						const struct flowi *fl);
30619bd6244SDavid S. Miller 	void			(*init_temprop)(struct xfrm_state *x,
30719bd6244SDavid S. Miller 						const struct xfrm_tmpl *tmpl,
30819bd6244SDavid S. Miller 						const xfrm_address_t *daddr,
30919bd6244SDavid S. Miller 						const xfrm_address_t *saddr);
31041a49cc3SMasahide NAKAMURA 	int			(*tmpl_sort)(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n);
31141a49cc3SMasahide NAKAMURA 	int			(*state_sort)(struct xfrm_state **dst, struct xfrm_state **src, int n);
312cdca7265SMiika Komu 	int			(*output)(struct sk_buff *skb);
313227620e2SHerbert Xu 	int			(*extract_input)(struct xfrm_state *x,
314227620e2SHerbert Xu 						 struct sk_buff *skb);
31536cf9acfSHerbert Xu 	int			(*extract_output)(struct xfrm_state *x,
31636cf9acfSHerbert Xu 						  struct sk_buff *skb);
317716062fdSHerbert Xu 	int			(*transport_finish)(struct sk_buff *skb,
318716062fdSHerbert Xu 						    int async);
3191da177e4SLinus Torvalds };
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds extern int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo);
3221da177e4SLinus Torvalds extern int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo);
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds extern void xfrm_state_delete_tunnel(struct xfrm_state *x);
3251da177e4SLinus Torvalds 
326fd2c3ef7SEric Dumazet struct xfrm_type {
3271da177e4SLinus Torvalds 	char			*description;
3281da177e4SLinus Torvalds 	struct module		*owner;
329a6337463Sjamal 	u8			proto;
330a6337463Sjamal 	u8			flags;
3311b5c2299SMasahide NAKAMURA #define XFRM_TYPE_NON_FRAGMENT	1
332436a0a40SHerbert Xu #define XFRM_TYPE_REPLAY_PROT	2
333f04e7e8dSHerbert Xu #define XFRM_TYPE_LOCAL_COADDR	4
334f04e7e8dSHerbert Xu #define XFRM_TYPE_REMOTE_COADDR	8
3351da177e4SLinus Torvalds 
33672cb6962SHerbert Xu 	int			(*init_state)(struct xfrm_state *x);
3371da177e4SLinus Torvalds 	void			(*destructor)(struct xfrm_state *);
338e695633eSHerbert Xu 	int			(*input)(struct xfrm_state *, struct sk_buff *skb);
3391da177e4SLinus Torvalds 	int			(*output)(struct xfrm_state *, struct sk_buff *pskb);
3408f029de2SDavid S. Miller 	int			(*reject)(struct xfrm_state *, struct sk_buff *,
3418f029de2SDavid S. Miller 					  const struct flowi *);
342aee5adb4SMasahide NAKAMURA 	int			(*hdr_offset)(struct xfrm_state *, struct sk_buff *, u8 **);
3431da177e4SLinus Torvalds 	/* Estimate maximal size of result of transformation of a dgram */
344c5c25238SPatrick McHardy 	u32			(*get_mtu)(struct xfrm_state *, int size);
3451da177e4SLinus Torvalds };
3461da177e4SLinus Torvalds 
347533cb5b0SEric Dumazet extern int xfrm_register_type(const struct xfrm_type *type, unsigned short family);
348533cb5b0SEric Dumazet extern int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family);
3491da177e4SLinus Torvalds 
350b59f45d0SHerbert Xu struct xfrm_mode {
351227620e2SHerbert Xu 	/*
352227620e2SHerbert Xu 	 * Remove encapsulation header.
353227620e2SHerbert Xu 	 *
354227620e2SHerbert Xu 	 * The IP header will be moved over the top of the encapsulation
355227620e2SHerbert Xu 	 * header.
356227620e2SHerbert Xu 	 *
357227620e2SHerbert Xu 	 * On entry, the transport header shall point to where the IP header
358227620e2SHerbert Xu 	 * should be and the network header shall be set to where the IP
359227620e2SHerbert Xu 	 * header currently is.  skb->data shall point to the start of the
360227620e2SHerbert Xu 	 * payload.
361227620e2SHerbert Xu 	 */
362227620e2SHerbert Xu 	int (*input2)(struct xfrm_state *x, struct sk_buff *skb);
363227620e2SHerbert Xu 
364227620e2SHerbert Xu 	/*
365227620e2SHerbert Xu 	 * This is the actual input entry point.
366227620e2SHerbert Xu 	 *
367227620e2SHerbert Xu 	 * For transport mode and equivalent this would be identical to
368227620e2SHerbert Xu 	 * input2 (which does not need to be set).  While tunnel mode
369227620e2SHerbert Xu 	 * and equivalent would set this to the tunnel encapsulation function
370227620e2SHerbert Xu 	 * xfrm4_prepare_input that would in turn call input2.
371227620e2SHerbert Xu 	 */
372b59f45d0SHerbert Xu 	int (*input)(struct xfrm_state *x, struct sk_buff *skb);
37337fedd3aSHerbert Xu 
37437fedd3aSHerbert Xu 	/*
37537fedd3aSHerbert Xu 	 * Add encapsulation header.
37637fedd3aSHerbert Xu 	 *
37737fedd3aSHerbert Xu 	 * On exit, the transport header will be set to the start of the
37837fedd3aSHerbert Xu 	 * encapsulation header to be filled in by x->type->output and
37937fedd3aSHerbert Xu 	 * the mac header will be set to the nextheader (protocol for
38037fedd3aSHerbert Xu 	 * IPv4) field of the extension header directly preceding the
38137fedd3aSHerbert Xu 	 * encapsulation header, or in its absence, that of the top IP
38237fedd3aSHerbert Xu 	 * header.  The value of the network header will always point
38337fedd3aSHerbert Xu 	 * to the top IP header while skb->data will point to the payload.
38437fedd3aSHerbert Xu 	 */
38536cf9acfSHerbert Xu 	int (*output2)(struct xfrm_state *x,struct sk_buff *skb);
38636cf9acfSHerbert Xu 
38736cf9acfSHerbert Xu 	/*
38836cf9acfSHerbert Xu 	 * This is the actual output entry point.
38936cf9acfSHerbert Xu 	 *
39036cf9acfSHerbert Xu 	 * For transport mode and equivalent this would be identical to
39136cf9acfSHerbert Xu 	 * output2 (which does not need to be set).  While tunnel mode
39236cf9acfSHerbert Xu 	 * and equivalent would set this to a tunnel encapsulation function
39336cf9acfSHerbert Xu 	 * (xfrm4_prepare_output or xfrm6_prepare_output) that would in turn
39436cf9acfSHerbert Xu 	 * call output2.
39536cf9acfSHerbert Xu 	 */
396eb878e84SJamal Hadi Salim 	int (*output)(struct xfrm_state *x, struct sk_buff *skb);
397b59f45d0SHerbert Xu 
39817c2a42aSHerbert Xu 	struct xfrm_state_afinfo *afinfo;
399b59f45d0SHerbert Xu 	struct module *owner;
400b59f45d0SHerbert Xu 	unsigned int encap;
4011bfcb10fSHerbert Xu 	int flags;
4021bfcb10fSHerbert Xu };
4031bfcb10fSHerbert Xu 
4041bfcb10fSHerbert Xu /* Flags for xfrm_mode. */
4051bfcb10fSHerbert Xu enum {
4061bfcb10fSHerbert Xu 	XFRM_MODE_FLAG_TUNNEL = 1,
407b59f45d0SHerbert Xu };
408b59f45d0SHerbert Xu 
409b59f45d0SHerbert Xu extern int xfrm_register_mode(struct xfrm_mode *mode, int family);
410b59f45d0SHerbert Xu extern int xfrm_unregister_mode(struct xfrm_mode *mode, int family);
411b59f45d0SHerbert Xu 
412df9dcb45SKazunori MIYAZAWA static inline int xfrm_af2proto(unsigned int family)
413df9dcb45SKazunori MIYAZAWA {
414df9dcb45SKazunori MIYAZAWA 	switch(family) {
415df9dcb45SKazunori MIYAZAWA 	case AF_INET:
416df9dcb45SKazunori MIYAZAWA 		return IPPROTO_IPIP;
417df9dcb45SKazunori MIYAZAWA 	case AF_INET6:
418df9dcb45SKazunori MIYAZAWA 		return IPPROTO_IPV6;
419df9dcb45SKazunori MIYAZAWA 	default:
420df9dcb45SKazunori MIYAZAWA 		return 0;
421df9dcb45SKazunori MIYAZAWA 	}
422df9dcb45SKazunori MIYAZAWA }
423df9dcb45SKazunori MIYAZAWA 
424df9dcb45SKazunori MIYAZAWA static inline struct xfrm_mode *xfrm_ip2inner_mode(struct xfrm_state *x, int ipproto)
425df9dcb45SKazunori MIYAZAWA {
426df9dcb45SKazunori MIYAZAWA 	if ((ipproto == IPPROTO_IPIP && x->props.family == AF_INET) ||
427df9dcb45SKazunori MIYAZAWA 	    (ipproto == IPPROTO_IPV6 && x->props.family == AF_INET6))
428df9dcb45SKazunori MIYAZAWA 		return x->inner_mode;
429df9dcb45SKazunori MIYAZAWA 	else
430df9dcb45SKazunori MIYAZAWA 		return x->inner_mode_iaf;
431df9dcb45SKazunori MIYAZAWA }
432df9dcb45SKazunori MIYAZAWA 
433fd2c3ef7SEric Dumazet struct xfrm_tmpl {
4341da177e4SLinus Torvalds /* id in template is interpreted as:
4351da177e4SLinus Torvalds  * daddr - destination of tunnel, may be zero for transport mode.
4361da177e4SLinus Torvalds  * spi   - zero to acquire spi. Not zero if spi is static, then
4371da177e4SLinus Torvalds  *	   daddr must be fixed too.
4381da177e4SLinus Torvalds  * proto - AH/ESP/IPCOMP
4391da177e4SLinus Torvalds  */
4401da177e4SLinus Torvalds 	struct xfrm_id		id;
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds /* Source address of tunnel. Ignored, if it is not a tunnel. */
4431da177e4SLinus Torvalds 	xfrm_address_t		saddr;
4441da177e4SLinus Torvalds 
44576b3f055SMiika Komu 	unsigned short		encap_family;
44676b3f055SMiika Komu 
447a6337463Sjamal 	u32			reqid;
4481da177e4SLinus Torvalds 
4497e49e6deSMasahide NAKAMURA /* Mode: transport, tunnel etc. */
450a6337463Sjamal 	u8			mode;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds /* Sharing mode: unique, this session only, this user only etc. */
453a6337463Sjamal 	u8			share;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds /* May skip this transfomration if no SA is found */
456a6337463Sjamal 	u8			optional;
4571da177e4SLinus Torvalds 
458c5d18e98SHerbert Xu /* Skip aalgos/ealgos/calgos checks. */
459a6337463Sjamal 	u8			allalgs;
460c5d18e98SHerbert Xu 
4611da177e4SLinus Torvalds /* Bit mask of algos allowed for acquisition */
462a6337463Sjamal 	u32			aalgos;
463a6337463Sjamal 	u32			ealgos;
464a6337463Sjamal 	u32			calgos;
4651da177e4SLinus Torvalds };
4661da177e4SLinus Torvalds 
467622dc828SMasahide NAKAMURA #define XFRM_MAX_DEPTH		6
4681da177e4SLinus Torvalds 
46912a169e7SHerbert Xu struct xfrm_policy_walk_entry {
47012a169e7SHerbert Xu 	struct list_head	all;
47112a169e7SHerbert Xu 	u8			dead;
47212a169e7SHerbert Xu };
47312a169e7SHerbert Xu 
47412a169e7SHerbert Xu struct xfrm_policy_walk {
47512a169e7SHerbert Xu 	struct xfrm_policy_walk_entry walk;
47612a169e7SHerbert Xu 	u8 type;
47712a169e7SHerbert Xu 	u32 seq;
47812a169e7SHerbert Xu };
47912a169e7SHerbert Xu 
480fd2c3ef7SEric Dumazet struct xfrm_policy {
4810331b1f3SAlexey Dobriyan #ifdef CONFIG_NET_NS
4820331b1f3SAlexey Dobriyan 	struct net		*xp_net;
4830331b1f3SAlexey Dobriyan #endif
4842518c7c2SDavid S. Miller 	struct hlist_node	bydst;
4852518c7c2SDavid S. Miller 	struct hlist_node	byidx;
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	/* This lock only affects elements except for entry. */
4881da177e4SLinus Torvalds 	rwlock_t		lock;
4891da177e4SLinus Torvalds 	atomic_t		refcnt;
4901da177e4SLinus Torvalds 	struct timer_list	timer;
4911da177e4SLinus Torvalds 
492fe1a5f03STimo Teräs 	struct flow_cache_object flo;
49380c802f3STimo Teräs 	atomic_t		genid;
4941da177e4SLinus Torvalds 	u32			priority;
4951da177e4SLinus Torvalds 	u32			index;
496bf825f81SJamal Hadi Salim 	struct xfrm_mark	mark;
4971da177e4SLinus Torvalds 	struct xfrm_selector	selector;
4981da177e4SLinus Torvalds 	struct xfrm_lifetime_cfg lft;
4991da177e4SLinus Torvalds 	struct xfrm_lifetime_cur curlft;
50012a169e7SHerbert Xu 	struct xfrm_policy_walk_entry walk;
50146ca5f5dSArnaldo Carvalho de Melo 	u8			type;
50246ca5f5dSArnaldo Carvalho de Melo 	u8			action;
50346ca5f5dSArnaldo Carvalho de Melo 	u8			flags;
50446ca5f5dSArnaldo Carvalho de Melo 	u8			xfrm_nr;
50512a169e7SHerbert Xu 	u16			family;
506df71837dSTrent Jaeger 	struct xfrm_sec_ctx	*security;
5071da177e4SLinus Torvalds 	struct xfrm_tmpl       	xfrm_vec[XFRM_MAX_DEPTH];
5081da177e4SLinus Torvalds };
5091da177e4SLinus Torvalds 
51063eb23f5SDavid S. Miller static inline struct net *xp_net(const struct xfrm_policy *xp)
5110331b1f3SAlexey Dobriyan {
5120331b1f3SAlexey Dobriyan 	return read_pnet(&xp->xp_net);
5130331b1f3SAlexey Dobriyan }
5140331b1f3SAlexey Dobriyan 
51513c1d189SArnaud Ebalard struct xfrm_kmaddress {
51613c1d189SArnaud Ebalard 	xfrm_address_t          local;
51713c1d189SArnaud Ebalard 	xfrm_address_t          remote;
51813c1d189SArnaud Ebalard 	u32			reserved;
51913c1d189SArnaud Ebalard 	u16			family;
52013c1d189SArnaud Ebalard };
52113c1d189SArnaud Ebalard 
52280c9abaaSShinta Sugimoto struct xfrm_migrate {
52380c9abaaSShinta Sugimoto 	xfrm_address_t		old_daddr;
52480c9abaaSShinta Sugimoto 	xfrm_address_t		old_saddr;
52580c9abaaSShinta Sugimoto 	xfrm_address_t		new_daddr;
52680c9abaaSShinta Sugimoto 	xfrm_address_t		new_saddr;
52780c9abaaSShinta Sugimoto 	u8			proto;
52880c9abaaSShinta Sugimoto 	u8			mode;
52980c9abaaSShinta Sugimoto 	u16			reserved;
53080c9abaaSShinta Sugimoto 	u32			reqid;
53180c9abaaSShinta Sugimoto 	u16			old_family;
53280c9abaaSShinta Sugimoto 	u16			new_family;
53380c9abaaSShinta Sugimoto };
53480c9abaaSShinta Sugimoto 
5351da177e4SLinus Torvalds #define XFRM_KM_TIMEOUT                30
536f8cd5488SJamal Hadi Salim /* which seqno */
537f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_SEQ		1
538f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_OSEQ	2
539f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_SEQ_MASK	3
540f8cd5488SJamal Hadi Salim /* what happened */
541f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_UPDATE	XFRM_AE_CR
542f8cd5488SJamal Hadi Salim #define XFRM_REPLAY_TIMEOUT	XFRM_AE_CE
543f8cd5488SJamal Hadi Salim 
544f8cd5488SJamal Hadi Salim /* default aevent timeout in units of 100ms */
545f8cd5488SJamal Hadi Salim #define XFRM_AE_ETIME			10
546f8cd5488SJamal Hadi Salim /* Async Event timer multiplier */
547f8cd5488SJamal Hadi Salim #define XFRM_AE_ETH_M			10
548f8cd5488SJamal Hadi Salim /* default seq threshold size */
549f8cd5488SJamal Hadi Salim #define XFRM_AE_SEQT_SIZE		2
5501da177e4SLinus Torvalds 
551fd2c3ef7SEric Dumazet struct xfrm_mgr {
5521da177e4SLinus Torvalds 	struct list_head	list;
5531da177e4SLinus Torvalds 	char			*id;
554214e005bSDavid S. Miller 	int			(*notify)(struct xfrm_state *x, const struct km_event *c);
5551da177e4SLinus Torvalds 	int			(*acquire)(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *xp, int dir);
556cb969f07SVenkat Yekkirala 	struct xfrm_policy	*(*compile_policy)(struct sock *sk, int opt, u8 *data, int len, int *dir);
5575d36b180SAl Viro 	int			(*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport);
558214e005bSDavid S. Miller 	int			(*notify_policy)(struct xfrm_policy *x, int dir, const struct km_event *c);
559db983c11SAlexey Dobriyan 	int			(*report)(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr);
560183cad12SDavid S. Miller 	int			(*migrate)(const struct xfrm_selector *sel,
561183cad12SDavid S. Miller 					   u8 dir, u8 type,
562183cad12SDavid S. Miller 					   const struct xfrm_migrate *m,
563183cad12SDavid S. Miller 					   int num_bundles,
564183cad12SDavid S. Miller 					   const struct xfrm_kmaddress *k);
5651da177e4SLinus Torvalds };
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds extern int xfrm_register_km(struct xfrm_mgr *km);
5681da177e4SLinus Torvalds extern int xfrm_unregister_km(struct xfrm_mgr *km);
5691da177e4SLinus Torvalds 
570436a0a40SHerbert Xu /*
571436a0a40SHerbert Xu  * This structure is used for the duration where packets are being
572436a0a40SHerbert Xu  * transformed by IPsec.  As soon as the packet leaves IPsec the
573436a0a40SHerbert Xu  * area beyond the generic IP part may be overwritten.
574436a0a40SHerbert Xu  */
575436a0a40SHerbert Xu struct xfrm_skb_cb {
576436a0a40SHerbert Xu 	union {
577436a0a40SHerbert Xu 		struct inet_skb_parm h4;
578436a0a40SHerbert Xu 		struct inet6_skb_parm h6;
579436a0a40SHerbert Xu         } header;
580436a0a40SHerbert Xu 
581436a0a40SHerbert Xu         /* Sequence number for replay protection. */
582b318e0e4SHerbert Xu 	union {
583b318e0e4SHerbert Xu 		u64 output;
584b318e0e4SHerbert Xu 		__be32 input;
585b318e0e4SHerbert Xu 	} seq;
586436a0a40SHerbert Xu };
587436a0a40SHerbert Xu 
588436a0a40SHerbert Xu #define XFRM_SKB_CB(__skb) ((struct xfrm_skb_cb *)&((__skb)->cb[0]))
589436a0a40SHerbert Xu 
59036cf9acfSHerbert Xu /*
59136cf9acfSHerbert Xu  * This structure is used by the afinfo prepare_input/prepare_output functions
59236cf9acfSHerbert Xu  * to transmit header information to the mode input/output functions.
59336cf9acfSHerbert Xu  */
59436cf9acfSHerbert Xu struct xfrm_mode_skb_cb {
59536cf9acfSHerbert Xu 	union {
59636cf9acfSHerbert Xu 		struct inet_skb_parm h4;
59736cf9acfSHerbert Xu 		struct inet6_skb_parm h6;
59836cf9acfSHerbert Xu 	} header;
59936cf9acfSHerbert Xu 
60036cf9acfSHerbert Xu 	/* Copied from header for IPv4, always set to zero and DF for IPv6. */
60136cf9acfSHerbert Xu 	__be16 id;
60236cf9acfSHerbert Xu 	__be16 frag_off;
60336cf9acfSHerbert Xu 
604732c8bd5SHerbert Xu 	/* IP header length (excluding options or extension headers). */
605732c8bd5SHerbert Xu 	u8 ihl;
606732c8bd5SHerbert Xu 
60736cf9acfSHerbert Xu 	/* TOS for IPv4, class for IPv6. */
60836cf9acfSHerbert Xu 	u8 tos;
60936cf9acfSHerbert Xu 
61036cf9acfSHerbert Xu 	/* TTL for IPv4, hop limitfor IPv6. */
61136cf9acfSHerbert Xu 	u8 ttl;
61236cf9acfSHerbert Xu 
61336cf9acfSHerbert Xu 	/* Protocol for IPv4, NH for IPv6. */
61436cf9acfSHerbert Xu 	u8 protocol;
61536cf9acfSHerbert Xu 
616732c8bd5SHerbert Xu 	/* Option length for IPv4, zero for IPv6. */
617732c8bd5SHerbert Xu 	u8 optlen;
618732c8bd5SHerbert Xu 
61936cf9acfSHerbert Xu 	/* Used by IPv6 only, zero for IPv4. */
62036cf9acfSHerbert Xu 	u8 flow_lbl[3];
62136cf9acfSHerbert Xu };
62236cf9acfSHerbert Xu 
62336cf9acfSHerbert Xu #define XFRM_MODE_SKB_CB(__skb) ((struct xfrm_mode_skb_cb *)&((__skb)->cb[0]))
62436cf9acfSHerbert Xu 
625716062fdSHerbert Xu /*
626716062fdSHerbert Xu  * This structure is used by the input processing to locate the SPI and
627716062fdSHerbert Xu  * related information.
628716062fdSHerbert Xu  */
629716062fdSHerbert Xu struct xfrm_spi_skb_cb {
630716062fdSHerbert Xu 	union {
631716062fdSHerbert Xu 		struct inet_skb_parm h4;
632716062fdSHerbert Xu 		struct inet6_skb_parm h6;
633716062fdSHerbert Xu 	} header;
634716062fdSHerbert Xu 
635716062fdSHerbert Xu 	unsigned int daddroff;
6362fcb45b6SHerbert Xu 	unsigned int family;
637716062fdSHerbert Xu };
638716062fdSHerbert Xu 
639716062fdSHerbert Xu #define XFRM_SPI_SKB_CB(__skb) ((struct xfrm_spi_skb_cb *)&((__skb)->cb[0]))
640716062fdSHerbert Xu 
641161a09e7SJoy Latten /* Audit Information */
642fd2c3ef7SEric Dumazet struct xfrm_audit {
643161a09e7SJoy Latten 	u32	secid;
6442532386fSEric Paris 	uid_t	loginuid;
6452532386fSEric Paris 	u32	sessionid;
646161a09e7SJoy Latten };
647c9204d9cSJoy Latten 
648c9204d9cSJoy Latten #ifdef CONFIG_AUDITSYSCALL
649afeb14b4SPaul Moore static inline struct audit_buffer *xfrm_audit_start(const char *op)
650ab5f5e8bSJoy Latten {
651ab5f5e8bSJoy Latten 	struct audit_buffer *audit_buf = NULL;
652ab5f5e8bSJoy Latten 
653afeb14b4SPaul Moore 	if (audit_enabled == 0)
654afeb14b4SPaul Moore 		return NULL;
655ab5f5e8bSJoy Latten 	audit_buf = audit_log_start(current->audit_context, GFP_ATOMIC,
656ab5f5e8bSJoy Latten 				    AUDIT_MAC_IPSEC_EVENT);
657ab5f5e8bSJoy Latten 	if (audit_buf == NULL)
658ab5f5e8bSJoy Latten 		return NULL;
659afeb14b4SPaul Moore 	audit_log_format(audit_buf, "op=%s", op);
660afeb14b4SPaul Moore 	return audit_buf;
661afeb14b4SPaul Moore }
662afeb14b4SPaul Moore 
6632532386fSEric Paris static inline void xfrm_audit_helper_usrinfo(uid_t auid, u32 ses, u32 secid,
664afeb14b4SPaul Moore 					     struct audit_buffer *audit_buf)
665afeb14b4SPaul Moore {
666afeb14b4SPaul Moore 	char *secctx;
667afeb14b4SPaul Moore 	u32 secctx_len;
668ab5f5e8bSJoy Latten 
6692532386fSEric Paris 	audit_log_format(audit_buf, " auid=%u ses=%u", auid, ses);
67068277accSPaul Moore 	if (secid != 0 &&
67168277accSPaul Moore 	    security_secid_to_secctx(secid, &secctx, &secctx_len) == 0) {
672ab5f5e8bSJoy Latten 		audit_log_format(audit_buf, " subj=%s", secctx);
673ab5f5e8bSJoy Latten 		security_release_secctx(secctx, secctx_len);
674ab5f5e8bSJoy Latten 	} else
675ab5f5e8bSJoy Latten 		audit_log_task_context(audit_buf);
676ab5f5e8bSJoy Latten }
677ab5f5e8bSJoy Latten 
678ab5f5e8bSJoy Latten extern void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
6792532386fSEric Paris 				  u32 auid, u32 ses, u32 secid);
680ab5f5e8bSJoy Latten extern void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
6812532386fSEric Paris 				  u32 auid, u32 ses, u32 secid);
682ab5f5e8bSJoy Latten extern void xfrm_audit_state_add(struct xfrm_state *x, int result,
6832532386fSEric Paris 				 u32 auid, u32 ses, u32 secid);
684ab5f5e8bSJoy Latten extern void xfrm_audit_state_delete(struct xfrm_state *x, int result,
6852532386fSEric Paris 				    u32 auid, u32 ses, u32 secid);
686afeb14b4SPaul Moore extern void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
687afeb14b4SPaul Moore 					     struct sk_buff *skb);
688afeb14b4SPaul Moore extern void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family);
689afeb14b4SPaul Moore extern void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
690afeb14b4SPaul Moore 				      __be32 net_spi, __be32 net_seq);
691afeb14b4SPaul Moore extern void xfrm_audit_state_icvfail(struct xfrm_state *x,
692afeb14b4SPaul Moore 				     struct sk_buff *skb, u8 proto);
693c9204d9cSJoy Latten #else
69441fef0eeSMarcin Slusarz 
69541fef0eeSMarcin Slusarz static inline void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
69641fef0eeSMarcin Slusarz 				  u32 auid, u32 ses, u32 secid)
69741fef0eeSMarcin Slusarz {
69841fef0eeSMarcin Slusarz }
69941fef0eeSMarcin Slusarz 
70041fef0eeSMarcin Slusarz static inline void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
70141fef0eeSMarcin Slusarz 				  u32 auid, u32 ses, u32 secid)
70241fef0eeSMarcin Slusarz {
70341fef0eeSMarcin Slusarz }
70441fef0eeSMarcin Slusarz 
70541fef0eeSMarcin Slusarz static inline void xfrm_audit_state_add(struct xfrm_state *x, int result,
70641fef0eeSMarcin Slusarz 				 u32 auid, u32 ses, u32 secid)
70741fef0eeSMarcin Slusarz {
70841fef0eeSMarcin Slusarz }
70941fef0eeSMarcin Slusarz 
71041fef0eeSMarcin Slusarz static inline void xfrm_audit_state_delete(struct xfrm_state *x, int result,
71141fef0eeSMarcin Slusarz 				    u32 auid, u32 ses, u32 secid)
71241fef0eeSMarcin Slusarz {
71341fef0eeSMarcin Slusarz }
71441fef0eeSMarcin Slusarz 
71541fef0eeSMarcin Slusarz static inline void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
71641fef0eeSMarcin Slusarz 					     struct sk_buff *skb)
71741fef0eeSMarcin Slusarz {
71841fef0eeSMarcin Slusarz }
71941fef0eeSMarcin Slusarz 
72041fef0eeSMarcin Slusarz static inline void xfrm_audit_state_notfound_simple(struct sk_buff *skb,
72141fef0eeSMarcin Slusarz 				      u16 family)
72241fef0eeSMarcin Slusarz {
72341fef0eeSMarcin Slusarz }
72441fef0eeSMarcin Slusarz 
72541fef0eeSMarcin Slusarz static inline void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
72641fef0eeSMarcin Slusarz 				      __be32 net_spi, __be32 net_seq)
72741fef0eeSMarcin Slusarz {
72841fef0eeSMarcin Slusarz }
72941fef0eeSMarcin Slusarz 
73041fef0eeSMarcin Slusarz static inline void xfrm_audit_state_icvfail(struct xfrm_state *x,
73141fef0eeSMarcin Slusarz 				     struct sk_buff *skb, u8 proto)
73241fef0eeSMarcin Slusarz {
73341fef0eeSMarcin Slusarz }
734c9204d9cSJoy Latten #endif /* CONFIG_AUDITSYSCALL */
735161a09e7SJoy Latten 
7361da177e4SLinus Torvalds static inline void xfrm_pol_hold(struct xfrm_policy *policy)
7371da177e4SLinus Torvalds {
7381da177e4SLinus Torvalds 	if (likely(policy != NULL))
7391da177e4SLinus Torvalds 		atomic_inc(&policy->refcnt);
7401da177e4SLinus Torvalds }
7411da177e4SLinus Torvalds 
74264c31b3fSWANG Cong extern void xfrm_policy_destroy(struct xfrm_policy *policy);
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds static inline void xfrm_pol_put(struct xfrm_policy *policy)
7451da177e4SLinus Torvalds {
7461da177e4SLinus Torvalds 	if (atomic_dec_and_test(&policy->refcnt))
74764c31b3fSWANG Cong 		xfrm_policy_destroy(policy);
7481da177e4SLinus Torvalds }
7491da177e4SLinus Torvalds 
7504e81bb83SMasahide NAKAMURA static inline void xfrm_pols_put(struct xfrm_policy **pols, int npols)
7514e81bb83SMasahide NAKAMURA {
7524e81bb83SMasahide NAKAMURA 	int i;
7534e81bb83SMasahide NAKAMURA 	for (i = npols - 1; i >= 0; --i)
7544e81bb83SMasahide NAKAMURA 		xfrm_pol_put(pols[i]);
7554e81bb83SMasahide NAKAMURA }
7564e81bb83SMasahide NAKAMURA 
7571da177e4SLinus Torvalds extern void __xfrm_state_destroy(struct xfrm_state *);
7581da177e4SLinus Torvalds 
75921380b81SHerbert Xu static inline void __xfrm_state_put(struct xfrm_state *x)
76021380b81SHerbert Xu {
76121380b81SHerbert Xu 	atomic_dec(&x->refcnt);
76221380b81SHerbert Xu }
76321380b81SHerbert Xu 
7641da177e4SLinus Torvalds static inline void xfrm_state_put(struct xfrm_state *x)
7651da177e4SLinus Torvalds {
7661da177e4SLinus Torvalds 	if (atomic_dec_and_test(&x->refcnt))
7671da177e4SLinus Torvalds 		__xfrm_state_destroy(x);
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds static inline void xfrm_state_hold(struct xfrm_state *x)
7711da177e4SLinus Torvalds {
7721da177e4SLinus Torvalds 	atomic_inc(&x->refcnt);
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751744a8feSDavid S. Miller static inline bool addr_match(const void *token1, const void *token2,
7761744a8feSDavid S. Miller 			      int prefixlen)
7771da177e4SLinus Torvalds {
7781744a8feSDavid S. Miller 	const __be32 *a1 = token1;
7791744a8feSDavid S. Miller 	const __be32 *a2 = token2;
7801da177e4SLinus Torvalds 	int pdw;
7811da177e4SLinus Torvalds 	int pbi;
7821da177e4SLinus Torvalds 
783a6337463Sjamal 	pdw = prefixlen >> 5;	  /* num of whole u32 in prefix */
7841da177e4SLinus Torvalds 	pbi = prefixlen &  0x1f;  /* num of bits in incomplete u32 in prefix */
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	if (pdw)
7871da177e4SLinus Torvalds 		if (memcmp(a1, a2, pdw << 2))
7881744a8feSDavid S. Miller 			return false;
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	if (pbi) {
7915f19343fSAl Viro 		__be32 mask;
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 		mask = htonl((0xffffffff) << (32 - pbi));
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 		if ((a1[pdw] ^ a2[pdw]) & mask)
7961744a8feSDavid S. Miller 			return false;
7971da177e4SLinus Torvalds 	}
7981da177e4SLinus Torvalds 
7991744a8feSDavid S. Miller 	return true;
8001da177e4SLinus Torvalds }
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds static __inline__
8036281dcc9SDavid S. Miller __be16 xfrm_flowi_sport(const struct flowi *fl, const union flowi_uli *uli)
8041da177e4SLinus Torvalds {
805f9d07e41SAl Viro 	__be16 port;
8061d28f42cSDavid S. Miller 	switch(fl->flowi_proto) {
8071da177e4SLinus Torvalds 	case IPPROTO_TCP:
8081da177e4SLinus Torvalds 	case IPPROTO_UDP:
809ba4e58ecSGerrit Renker 	case IPPROTO_UDPLITE:
8101da177e4SLinus Torvalds 	case IPPROTO_SCTP:
8116281dcc9SDavid S. Miller 		port = uli->ports.sport;
8121da177e4SLinus Torvalds 		break;
8131da177e4SLinus Torvalds 	case IPPROTO_ICMP:
8141da177e4SLinus Torvalds 	case IPPROTO_ICMPV6:
8156281dcc9SDavid S. Miller 		port = htons(uli->icmpt.type);
8161da177e4SLinus Torvalds 		break;
8172ce4272aSMasahide NAKAMURA 	case IPPROTO_MH:
8186281dcc9SDavid S. Miller 		port = htons(uli->mht.type);
8192ce4272aSMasahide NAKAMURA 		break;
820cc9ff19dSTimo Teräs 	case IPPROTO_GRE:
8216281dcc9SDavid S. Miller 		port = htons(ntohl(uli->gre_key) >> 16);
822cc9ff19dSTimo Teräs 		break;
8231da177e4SLinus Torvalds 	default:
8241da177e4SLinus Torvalds 		port = 0;	/*XXX*/
8251da177e4SLinus Torvalds 	}
8261da177e4SLinus Torvalds 	return port;
8271da177e4SLinus Torvalds }
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds static __inline__
8306281dcc9SDavid S. Miller __be16 xfrm_flowi_dport(const struct flowi *fl, const union flowi_uli *uli)
8311da177e4SLinus Torvalds {
832f9d07e41SAl Viro 	__be16 port;
8331d28f42cSDavid S. Miller 	switch(fl->flowi_proto) {
8341da177e4SLinus Torvalds 	case IPPROTO_TCP:
8351da177e4SLinus Torvalds 	case IPPROTO_UDP:
836ba4e58ecSGerrit Renker 	case IPPROTO_UDPLITE:
8371da177e4SLinus Torvalds 	case IPPROTO_SCTP:
8386281dcc9SDavid S. Miller 		port = uli->ports.dport;
8391da177e4SLinus Torvalds 		break;
8401da177e4SLinus Torvalds 	case IPPROTO_ICMP:
8411da177e4SLinus Torvalds 	case IPPROTO_ICMPV6:
8426281dcc9SDavid S. Miller 		port = htons(uli->icmpt.code);
8431da177e4SLinus Torvalds 		break;
844cc9ff19dSTimo Teräs 	case IPPROTO_GRE:
8456281dcc9SDavid S. Miller 		port = htons(ntohl(uli->gre_key) & 0xffff);
846cc9ff19dSTimo Teräs 		break;
8471da177e4SLinus Torvalds 	default:
8481da177e4SLinus Torvalds 		port = 0;	/*XXX*/
8491da177e4SLinus Torvalds 	}
8501da177e4SLinus Torvalds 	return port;
8511da177e4SLinus Torvalds }
8521da177e4SLinus Torvalds 
853200ce96eSDavid S. Miller extern int xfrm_selector_match(const struct xfrm_selector *sel,
854e1ad2ab2SDavid S. Miller 			       const struct flowi *fl,
85577681021SAndrew Morton 			       unsigned short family);
8561da177e4SLinus Torvalds 
857df71837dSTrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
858df71837dSTrent Jaeger /*	If neither has a context --> match
859df71837dSTrent Jaeger  * 	Otherwise, both must have a context and the sids, doi, alg must match
860df71837dSTrent Jaeger  */
861df71837dSTrent Jaeger static inline int xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
862df71837dSTrent Jaeger {
863df71837dSTrent Jaeger 	return ((!s1 && !s2) ||
864df71837dSTrent Jaeger 		(s1 && s2 &&
865df71837dSTrent Jaeger 		 (s1->ctx_sid == s2->ctx_sid) &&
866df71837dSTrent Jaeger 		 (s1->ctx_doi == s2->ctx_doi) &&
867df71837dSTrent Jaeger 		 (s1->ctx_alg == s2->ctx_alg)));
868df71837dSTrent Jaeger }
869df71837dSTrent Jaeger #else
870df71837dSTrent Jaeger static inline int xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
871df71837dSTrent Jaeger {
872df71837dSTrent Jaeger 	return 1;
873df71837dSTrent Jaeger }
874df71837dSTrent Jaeger #endif
875df71837dSTrent Jaeger 
8761da177e4SLinus Torvalds /* A struct encoding bundle of transformations to apply to some set of flow.
8771da177e4SLinus Torvalds  *
8781da177e4SLinus Torvalds  * dst->child points to the next element of bundle.
8791da177e4SLinus Torvalds  * dst->xfrm  points to an instanse of transformer.
8801da177e4SLinus Torvalds  *
8811da177e4SLinus Torvalds  * Due to unfortunate limitations of current routing cache, which we
8821da177e4SLinus Torvalds  * have no time to fix, it mirrors struct rtable and bound to the same
8831da177e4SLinus Torvalds  * routing key, including saddr,daddr. However, we can have many of
8841da177e4SLinus Torvalds  * bundles differing by session id. All the bundles grow from a parent
8851da177e4SLinus Torvalds  * policy rule.
8861da177e4SLinus Torvalds  */
887fd2c3ef7SEric Dumazet struct xfrm_dst {
8881da177e4SLinus Torvalds 	union {
8891da177e4SLinus Torvalds 		struct dst_entry	dst;
8901da177e4SLinus Torvalds 		struct rtable		rt;
8911da177e4SLinus Torvalds 		struct rt6_info		rt6;
8921da177e4SLinus Torvalds 	} u;
8931da177e4SLinus Torvalds 	struct dst_entry *route;
89480c802f3STimo Teräs 	struct flow_cache_object flo;
89580c802f3STimo Teräs 	struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
89680c802f3STimo Teräs 	int num_pols, num_xfrms;
897157bfc25SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
898157bfc25SMasahide NAKAMURA 	struct flowi *origin;
899157bfc25SMasahide NAKAMURA 	struct xfrm_selector *partner;
900157bfc25SMasahide NAKAMURA #endif
90180c802f3STimo Teräs 	u32 xfrm_genid;
90280c802f3STimo Teräs 	u32 policy_genid;
9031da177e4SLinus Torvalds 	u32 route_mtu_cached;
9041da177e4SLinus Torvalds 	u32 child_mtu_cached;
90592d63decSHideaki YOSHIFUJI 	u32 route_cookie;
90692d63decSHideaki YOSHIFUJI 	u32 path_cookie;
9071da177e4SLinus Torvalds };
9081da177e4SLinus Torvalds 
909def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
910aabc9761SHerbert Xu static inline void xfrm_dst_destroy(struct xfrm_dst *xdst)
911aabc9761SHerbert Xu {
91280c802f3STimo Teräs 	xfrm_pols_put(xdst->pols, xdst->num_pols);
913aabc9761SHerbert Xu 	dst_release(xdst->route);
914aabc9761SHerbert Xu 	if (likely(xdst->u.dst.xfrm))
915aabc9761SHerbert Xu 		xfrm_state_put(xdst->u.dst.xfrm);
916157bfc25SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
917157bfc25SMasahide NAKAMURA 	kfree(xdst->origin);
918157bfc25SMasahide NAKAMURA 	xdst->origin = NULL;
919157bfc25SMasahide NAKAMURA 	kfree(xdst->partner);
920157bfc25SMasahide NAKAMURA 	xdst->partner = NULL;
921157bfc25SMasahide NAKAMURA #endif
922aabc9761SHerbert Xu }
923def8b4faSAlexey Dobriyan #endif
924aabc9761SHerbert Xu 
925aabc9761SHerbert Xu extern void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev);
926aabc9761SHerbert Xu 
927fd2c3ef7SEric Dumazet struct sec_path {
9281da177e4SLinus Torvalds 	atomic_t		refcnt;
9291da177e4SLinus Torvalds 	int			len;
930dbe5b4aaSHerbert Xu 	struct xfrm_state	*xvec[XFRM_MAX_DEPTH];
9311da177e4SLinus Torvalds };
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds static inline struct sec_path *
9341da177e4SLinus Torvalds secpath_get(struct sec_path *sp)
9351da177e4SLinus Torvalds {
9361da177e4SLinus Torvalds 	if (sp)
9371da177e4SLinus Torvalds 		atomic_inc(&sp->refcnt);
9381da177e4SLinus Torvalds 	return sp;
9391da177e4SLinus Torvalds }
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds extern void __secpath_destroy(struct sec_path *sp);
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds static inline void
9441da177e4SLinus Torvalds secpath_put(struct sec_path *sp)
9451da177e4SLinus Torvalds {
9461da177e4SLinus Torvalds 	if (sp && atomic_dec_and_test(&sp->refcnt))
9471da177e4SLinus Torvalds 		__secpath_destroy(sp);
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds extern struct sec_path *secpath_dup(struct sec_path *src);
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds static inline void
9531da177e4SLinus Torvalds secpath_reset(struct sk_buff *skb)
9541da177e4SLinus Torvalds {
9551da177e4SLinus Torvalds #ifdef CONFIG_XFRM
9561da177e4SLinus Torvalds 	secpath_put(skb->sp);
9571da177e4SLinus Torvalds 	skb->sp = NULL;
9581da177e4SLinus Torvalds #endif
9591da177e4SLinus Torvalds }
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds static inline int
9626cc32961SDavid S. Miller xfrm_addr_any(const xfrm_address_t *addr, unsigned short family)
963a1e59abfSPatrick McHardy {
964a1e59abfSPatrick McHardy 	switch (family) {
965a1e59abfSPatrick McHardy 	case AF_INET:
966a1e59abfSPatrick McHardy 		return addr->a4 == 0;
967a1e59abfSPatrick McHardy 	case AF_INET6:
968a1e59abfSPatrick McHardy 		return ipv6_addr_any((struct in6_addr *)&addr->a6);
969a1e59abfSPatrick McHardy 	}
970a1e59abfSPatrick McHardy 	return 0;
971a1e59abfSPatrick McHardy }
972a1e59abfSPatrick McHardy 
973a1e59abfSPatrick McHardy static inline int
97421eddb5cSDavid S. Miller __xfrm4_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x)
9751da177e4SLinus Torvalds {
9761da177e4SLinus Torvalds 	return	(tmpl->saddr.a4 &&
9771da177e4SLinus Torvalds 		 tmpl->saddr.a4 != x->props.saddr.a4);
9781da177e4SLinus Torvalds }
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds static inline int
98121eddb5cSDavid S. Miller __xfrm6_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x)
9821da177e4SLinus Torvalds {
9831da177e4SLinus Torvalds 	return	(!ipv6_addr_any((struct in6_addr*)&tmpl->saddr) &&
9841da177e4SLinus Torvalds 		 ipv6_addr_cmp((struct in6_addr *)&tmpl->saddr, (struct in6_addr*)&x->props.saddr));
9851da177e4SLinus Torvalds }
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds static inline int
98821eddb5cSDavid S. Miller xfrm_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x, unsigned short family)
9891da177e4SLinus Torvalds {
9901da177e4SLinus Torvalds 	switch (family) {
9911da177e4SLinus Torvalds 	case AF_INET:
9921da177e4SLinus Torvalds 		return __xfrm4_state_addr_cmp(tmpl, x);
9931da177e4SLinus Torvalds 	case AF_INET6:
9941da177e4SLinus Torvalds 		return __xfrm6_state_addr_cmp(tmpl, x);
9951da177e4SLinus Torvalds 	}
9961da177e4SLinus Torvalds 	return !0;
9971da177e4SLinus Torvalds }
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds #ifdef CONFIG_XFRM
10001da177e4SLinus Torvalds extern int __xfrm_policy_check(struct sock *, int dir, struct sk_buff *skb, unsigned short family);
10011da177e4SLinus Torvalds 
1002d5422efeSHerbert Xu static inline int __xfrm_policy_check2(struct sock *sk, int dir,
1003d5422efeSHerbert Xu 				       struct sk_buff *skb,
1004d5422efeSHerbert Xu 				       unsigned int family, int reverse)
10051da177e4SLinus Torvalds {
1006f6e1e25dSAlexey Dobriyan 	struct net *net = dev_net(skb->dev);
1007d5422efeSHerbert Xu 	int ndir = dir | (reverse ? XFRM_POLICY_MASK + 1 : 0);
1008d5422efeSHerbert Xu 
10091da177e4SLinus Torvalds 	if (sk && sk->sk_policy[XFRM_POLICY_IN])
1010d5422efeSHerbert Xu 		return __xfrm_policy_check(sk, ndir, skb, family);
10111da177e4SLinus Torvalds 
1012f6e1e25dSAlexey Dobriyan 	return	(!net->xfrm.policy_count[dir] && !skb->sp) ||
1013adf30907SEric Dumazet 		(skb_dst(skb)->flags & DST_NOPOLICY) ||
1014d5422efeSHerbert Xu 		__xfrm_policy_check(sk, ndir, skb, family);
1015d5422efeSHerbert Xu }
1016d5422efeSHerbert Xu 
1017d5422efeSHerbert Xu static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
1018d5422efeSHerbert Xu {
1019d5422efeSHerbert Xu 	return __xfrm_policy_check2(sk, dir, skb, family, 0);
10201da177e4SLinus Torvalds }
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
10231da177e4SLinus Torvalds {
10241da177e4SLinus Torvalds 	return xfrm_policy_check(sk, dir, skb, AF_INET);
10251da177e4SLinus Torvalds }
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
10281da177e4SLinus Torvalds {
10291da177e4SLinus Torvalds 	return xfrm_policy_check(sk, dir, skb, AF_INET6);
10301da177e4SLinus Torvalds }
10311da177e4SLinus Torvalds 
1032d5422efeSHerbert Xu static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir,
1033d5422efeSHerbert Xu 					     struct sk_buff *skb)
1034d5422efeSHerbert Xu {
1035d5422efeSHerbert Xu 	return __xfrm_policy_check2(sk, dir, skb, AF_INET, 1);
1036d5422efeSHerbert Xu }
1037d5422efeSHerbert Xu 
1038d5422efeSHerbert Xu static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir,
1039d5422efeSHerbert Xu 					     struct sk_buff *skb)
1040d5422efeSHerbert Xu {
1041d5422efeSHerbert Xu 	return __xfrm_policy_check2(sk, dir, skb, AF_INET6, 1);
1042d5422efeSHerbert Xu }
1043d5422efeSHerbert Xu 
1044d5422efeSHerbert Xu extern int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1045d5422efeSHerbert Xu 				 unsigned int family, int reverse);
1046d5422efeSHerbert Xu 
1047d5422efeSHerbert Xu static inline int xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1048d5422efeSHerbert Xu 				      unsigned int family)
1049d5422efeSHerbert Xu {
1050d5422efeSHerbert Xu 	return __xfrm_decode_session(skb, fl, family, 0);
1051d5422efeSHerbert Xu }
1052d5422efeSHerbert Xu 
1053d5422efeSHerbert Xu static inline int xfrm_decode_session_reverse(struct sk_buff *skb,
1054d5422efeSHerbert Xu 					      struct flowi *fl,
1055d5422efeSHerbert Xu 					      unsigned int family)
1056d5422efeSHerbert Xu {
1057d5422efeSHerbert Xu 	return __xfrm_decode_session(skb, fl, family, 1);
1058d5422efeSHerbert Xu }
1059d5422efeSHerbert Xu 
10601da177e4SLinus Torvalds extern int __xfrm_route_forward(struct sk_buff *skb, unsigned short family);
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds static inline int xfrm_route_forward(struct sk_buff *skb, unsigned short family)
10631da177e4SLinus Torvalds {
106499a66657SAlexey Dobriyan 	struct net *net = dev_net(skb->dev);
106599a66657SAlexey Dobriyan 
106699a66657SAlexey Dobriyan 	return	!net->xfrm.policy_count[XFRM_POLICY_OUT] ||
1067adf30907SEric Dumazet 		(skb_dst(skb)->flags & DST_NOXFRM) ||
10681da177e4SLinus Torvalds 		__xfrm_route_forward(skb, family);
10691da177e4SLinus Torvalds }
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds static inline int xfrm4_route_forward(struct sk_buff *skb)
10721da177e4SLinus Torvalds {
10731da177e4SLinus Torvalds 	return xfrm_route_forward(skb, AF_INET);
10741da177e4SLinus Torvalds }
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds static inline int xfrm6_route_forward(struct sk_buff *skb)
10771da177e4SLinus Torvalds {
10781da177e4SLinus Torvalds 	return xfrm_route_forward(skb, AF_INET6);
10791da177e4SLinus Torvalds }
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds extern int __xfrm_sk_clone_policy(struct sock *sk);
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds static inline int xfrm_sk_clone_policy(struct sock *sk)
10841da177e4SLinus Torvalds {
10851da177e4SLinus Torvalds 	if (unlikely(sk->sk_policy[0] || sk->sk_policy[1]))
10861da177e4SLinus Torvalds 		return __xfrm_sk_clone_policy(sk);
10871da177e4SLinus Torvalds 	return 0;
10881da177e4SLinus Torvalds }
10891da177e4SLinus Torvalds 
10904666faabSHerbert Xu extern int xfrm_policy_delete(struct xfrm_policy *pol, int dir);
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds static inline void xfrm_sk_free_policy(struct sock *sk)
10931da177e4SLinus Torvalds {
10941da177e4SLinus Torvalds 	if (unlikely(sk->sk_policy[0] != NULL)) {
10951da177e4SLinus Torvalds 		xfrm_policy_delete(sk->sk_policy[0], XFRM_POLICY_MAX);
10961da177e4SLinus Torvalds 		sk->sk_policy[0] = NULL;
10971da177e4SLinus Torvalds 	}
10981da177e4SLinus Torvalds 	if (unlikely(sk->sk_policy[1] != NULL)) {
10991da177e4SLinus Torvalds 		xfrm_policy_delete(sk->sk_policy[1], XFRM_POLICY_MAX+1);
11001da177e4SLinus Torvalds 		sk->sk_policy[1] = NULL;
11011da177e4SLinus Torvalds 	}
11021da177e4SLinus Torvalds }
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds #else
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds static inline void xfrm_sk_free_policy(struct sock *sk) {}
11071da177e4SLinus Torvalds static inline int xfrm_sk_clone_policy(struct sock *sk) { return 0; }
11081da177e4SLinus Torvalds static inline int xfrm6_route_forward(struct sk_buff *skb) { return 1; }
11091da177e4SLinus Torvalds static inline int xfrm4_route_forward(struct sk_buff *skb) { return 1; }
11101da177e4SLinus Torvalds static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
11111da177e4SLinus Torvalds {
11121da177e4SLinus Torvalds 	return 1;
11131da177e4SLinus Torvalds }
11141da177e4SLinus Torvalds static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
11151da177e4SLinus Torvalds {
11161da177e4SLinus Torvalds 	return 1;
11171da177e4SLinus Torvalds }
11181da177e4SLinus Torvalds static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
11191da177e4SLinus Torvalds {
11201da177e4SLinus Torvalds 	return 1;
11211da177e4SLinus Torvalds }
1122d5422efeSHerbert Xu static inline int xfrm_decode_session_reverse(struct sk_buff *skb,
1123d5422efeSHerbert Xu 					      struct flowi *fl,
1124d5422efeSHerbert Xu 					      unsigned int family)
1125d5422efeSHerbert Xu {
1126d5422efeSHerbert Xu 	return -ENOSYS;
1127d5422efeSHerbert Xu }
1128d5422efeSHerbert Xu static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir,
1129d5422efeSHerbert Xu 					     struct sk_buff *skb)
1130d5422efeSHerbert Xu {
1131d5422efeSHerbert Xu 	return 1;
1132d5422efeSHerbert Xu }
1133d5422efeSHerbert Xu static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir,
1134d5422efeSHerbert Xu 					     struct sk_buff *skb)
1135d5422efeSHerbert Xu {
1136d5422efeSHerbert Xu 	return 1;
1137d5422efeSHerbert Xu }
11381da177e4SLinus Torvalds #endif
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds static __inline__
1141e8a4e377SDavid S. Miller xfrm_address_t *xfrm_flowi_daddr(const struct flowi *fl, unsigned short family)
11421da177e4SLinus Torvalds {
11431da177e4SLinus Torvalds 	switch (family){
11441da177e4SLinus Torvalds 	case AF_INET:
11451da177e4SLinus Torvalds 		return (xfrm_address_t *)&fl->fl4_dst;
11461da177e4SLinus Torvalds 	case AF_INET6:
11471da177e4SLinus Torvalds 		return (xfrm_address_t *)&fl->fl6_dst;
11481da177e4SLinus Torvalds 	}
11491da177e4SLinus Torvalds 	return NULL;
11501da177e4SLinus Torvalds }
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds static __inline__
1153e8a4e377SDavid S. Miller xfrm_address_t *xfrm_flowi_saddr(const struct flowi *fl, unsigned short family)
11541da177e4SLinus Torvalds {
11551da177e4SLinus Torvalds 	switch (family){
11561da177e4SLinus Torvalds 	case AF_INET:
11571da177e4SLinus Torvalds 		return (xfrm_address_t *)&fl->fl4_src;
11581da177e4SLinus Torvalds 	case AF_INET6:
11591da177e4SLinus Torvalds 		return (xfrm_address_t *)&fl->fl6_src;
11601da177e4SLinus Torvalds 	}
11611da177e4SLinus Torvalds 	return NULL;
11621da177e4SLinus Torvalds }
11631da177e4SLinus Torvalds 
11649bb182a7SYOSHIFUJI Hideaki static __inline__
1165e8a4e377SDavid S. Miller void xfrm_flowi_addr_get(const struct flowi *fl,
11669bb182a7SYOSHIFUJI Hideaki 			 xfrm_address_t *saddr, xfrm_address_t *daddr,
11679bb182a7SYOSHIFUJI Hideaki 			 unsigned short family)
11689bb182a7SYOSHIFUJI Hideaki {
11699bb182a7SYOSHIFUJI Hideaki 	switch(family) {
11709bb182a7SYOSHIFUJI Hideaki 	case AF_INET:
11719bb182a7SYOSHIFUJI Hideaki 		memcpy(&saddr->a4, &fl->fl4_src, sizeof(saddr->a4));
11729bb182a7SYOSHIFUJI Hideaki 		memcpy(&daddr->a4, &fl->fl4_dst, sizeof(daddr->a4));
11739bb182a7SYOSHIFUJI Hideaki 		break;
11749bb182a7SYOSHIFUJI Hideaki 	case AF_INET6:
11759bb182a7SYOSHIFUJI Hideaki 		ipv6_addr_copy((struct in6_addr *)&saddr->a6, &fl->fl6_src);
11769bb182a7SYOSHIFUJI Hideaki 		ipv6_addr_copy((struct in6_addr *)&daddr->a6, &fl->fl6_dst);
11779bb182a7SYOSHIFUJI Hideaki 		break;
11789bb182a7SYOSHIFUJI Hideaki 	}
11799bb182a7SYOSHIFUJI Hideaki }
11809bb182a7SYOSHIFUJI Hideaki 
11811da177e4SLinus Torvalds static __inline__ int
1182f8848067SDavid S. Miller __xfrm4_state_addr_check(const struct xfrm_state *x,
1183f8848067SDavid S. Miller 			 const xfrm_address_t *daddr, const xfrm_address_t *saddr)
11841da177e4SLinus Torvalds {
11851da177e4SLinus Torvalds 	if (daddr->a4 == x->id.daddr.a4 &&
11861da177e4SLinus Torvalds 	    (saddr->a4 == x->props.saddr.a4 || !saddr->a4 || !x->props.saddr.a4))
11871da177e4SLinus Torvalds 		return 1;
11881da177e4SLinus Torvalds 	return 0;
11891da177e4SLinus Torvalds }
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds static __inline__ int
1192f8848067SDavid S. Miller __xfrm6_state_addr_check(const struct xfrm_state *x,
1193f8848067SDavid S. Miller 			 const xfrm_address_t *daddr, const xfrm_address_t *saddr)
11941da177e4SLinus Torvalds {
11951da177e4SLinus Torvalds 	if (!ipv6_addr_cmp((struct in6_addr *)daddr, (struct in6_addr *)&x->id.daddr) &&
11961da177e4SLinus Torvalds 	    (!ipv6_addr_cmp((struct in6_addr *)saddr, (struct in6_addr *)&x->props.saddr)||
11971da177e4SLinus Torvalds 	     ipv6_addr_any((struct in6_addr *)saddr) ||
11981da177e4SLinus Torvalds 	     ipv6_addr_any((struct in6_addr *)&x->props.saddr)))
11991da177e4SLinus Torvalds 		return 1;
12001da177e4SLinus Torvalds 	return 0;
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds static __inline__ int
1204f8848067SDavid S. Miller xfrm_state_addr_check(const struct xfrm_state *x,
1205f8848067SDavid S. Miller 		      const xfrm_address_t *daddr, const xfrm_address_t *saddr,
12061da177e4SLinus Torvalds 		      unsigned short family)
12071da177e4SLinus Torvalds {
12081da177e4SLinus Torvalds 	switch (family) {
12091da177e4SLinus Torvalds 	case AF_INET:
12101da177e4SLinus Torvalds 		return __xfrm4_state_addr_check(x, daddr, saddr);
12111da177e4SLinus Torvalds 	case AF_INET6:
12121da177e4SLinus Torvalds 		return __xfrm6_state_addr_check(x, daddr, saddr);
12131da177e4SLinus Torvalds 	}
12141da177e4SLinus Torvalds 	return 0;
12151da177e4SLinus Torvalds }
12161da177e4SLinus Torvalds 
1217e53820deSMasahide NAKAMURA static __inline__ int
1218f8848067SDavid S. Miller xfrm_state_addr_flow_check(const struct xfrm_state *x, const struct flowi *fl,
1219e53820deSMasahide NAKAMURA 			   unsigned short family)
1220e53820deSMasahide NAKAMURA {
1221e53820deSMasahide NAKAMURA 	switch (family) {
1222e53820deSMasahide NAKAMURA 	case AF_INET:
1223e53820deSMasahide NAKAMURA 		return __xfrm4_state_addr_check(x,
1224f8848067SDavid S. Miller 						(const xfrm_address_t *)&fl->fl4_dst,
1225f8848067SDavid S. Miller 						(const xfrm_address_t *)&fl->fl4_src);
1226e53820deSMasahide NAKAMURA 	case AF_INET6:
1227e53820deSMasahide NAKAMURA 		return __xfrm6_state_addr_check(x,
1228f8848067SDavid S. Miller 						(const xfrm_address_t *)&fl->fl6_dst,
1229f8848067SDavid S. Miller 						(const xfrm_address_t *)&fl->fl6_src);
1230e53820deSMasahide NAKAMURA 	}
1231e53820deSMasahide NAKAMURA 	return 0;
1232e53820deSMasahide NAKAMURA }
1233e53820deSMasahide NAKAMURA 
1234f8848067SDavid S. Miller static inline int xfrm_state_kern(const struct xfrm_state *x)
12351da177e4SLinus Torvalds {
12361da177e4SLinus Torvalds 	return atomic_read(&x->tunnel_users);
12371da177e4SLinus Torvalds }
12381da177e4SLinus Torvalds 
12395794708fSMasahide NAKAMURA static inline int xfrm_id_proto_match(u8 proto, u8 userproto)
12405794708fSMasahide NAKAMURA {
1241dc00a525SMasahide NAKAMURA 	return (!userproto || proto == userproto ||
1242dc00a525SMasahide NAKAMURA 		(userproto == IPSEC_PROTO_ANY && (proto == IPPROTO_AH ||
1243dc00a525SMasahide NAKAMURA 						  proto == IPPROTO_ESP ||
1244dc00a525SMasahide NAKAMURA 						  proto == IPPROTO_COMP)));
12455794708fSMasahide NAKAMURA }
12465794708fSMasahide NAKAMURA 
12471da177e4SLinus Torvalds /*
12481da177e4SLinus Torvalds  * xfrm algorithm information
12491da177e4SLinus Torvalds  */
12501a6509d9SHerbert Xu struct xfrm_algo_aead_info {
12511a6509d9SHerbert Xu 	u16 icv_truncbits;
12521a6509d9SHerbert Xu };
12531a6509d9SHerbert Xu 
12541da177e4SLinus Torvalds struct xfrm_algo_auth_info {
12551da177e4SLinus Torvalds 	u16 icv_truncbits;
12561da177e4SLinus Torvalds 	u16 icv_fullbits;
12571da177e4SLinus Torvalds };
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds struct xfrm_algo_encr_info {
12601da177e4SLinus Torvalds 	u16 blockbits;
12611da177e4SLinus Torvalds 	u16 defkeybits;
12621da177e4SLinus Torvalds };
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds struct xfrm_algo_comp_info {
12651da177e4SLinus Torvalds 	u16 threshold;
12661da177e4SLinus Torvalds };
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds struct xfrm_algo_desc {
12691da177e4SLinus Torvalds 	char *name;
127004ff1260SHerbert Xu 	char *compat;
12711da177e4SLinus Torvalds 	u8 available:1;
12721da177e4SLinus Torvalds 	union {
12731a6509d9SHerbert Xu 		struct xfrm_algo_aead_info aead;
12741da177e4SLinus Torvalds 		struct xfrm_algo_auth_info auth;
12751da177e4SLinus Torvalds 		struct xfrm_algo_encr_info encr;
12761da177e4SLinus Torvalds 		struct xfrm_algo_comp_info comp;
12771da177e4SLinus Torvalds 	} uinfo;
12781da177e4SLinus Torvalds 	struct sadb_alg desc;
12791da177e4SLinus Torvalds };
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds /* XFRM tunnel handlers.  */
12821da177e4SLinus Torvalds struct xfrm_tunnel {
12831da177e4SLinus Torvalds 	int (*handler)(struct sk_buff *skb);
1284a6337463Sjamal 	int (*err_handler)(struct sk_buff *skb, u32 info);
1285d2acc347SHerbert Xu 
1286b33eab08SEric Dumazet 	struct xfrm_tunnel __rcu *next;
1287d2acc347SHerbert Xu 	int priority;
12881da177e4SLinus Torvalds };
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds struct xfrm6_tunnel {
1291d2acc347SHerbert Xu 	int (*handler)(struct sk_buff *skb);
1292d2acc347SHerbert Xu 	int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
1293d5fdd6baSBrian Haley 			   u8 type, u8 code, int offset, __be32 info);
12946f0bcf15SEric Dumazet 	struct xfrm6_tunnel __rcu *next;
1295d2acc347SHerbert Xu 	int priority;
12961da177e4SLinus Torvalds };
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds extern void xfrm_init(void);
1299a33bc5c1SNeil Horman extern void xfrm4_init(int rt_hash_size);
1300d62ddc21SAlexey Dobriyan extern int xfrm_state_init(struct net *net);
1301d62ddc21SAlexey Dobriyan extern void xfrm_state_fini(struct net *net);
13021da177e4SLinus Torvalds extern void xfrm4_state_init(void);
1303c35b7e72SDaniel Lezcano #ifdef CONFIG_XFRM
1304c35b7e72SDaniel Lezcano extern int xfrm6_init(void);
1305c35b7e72SDaniel Lezcano extern void xfrm6_fini(void);
13060013cabaSDaniel Lezcano extern int xfrm6_state_init(void);
13071da177e4SLinus Torvalds extern void xfrm6_state_fini(void);
1308c35b7e72SDaniel Lezcano #else
1309c35b7e72SDaniel Lezcano static inline int xfrm6_init(void)
1310c35b7e72SDaniel Lezcano {
1311c35b7e72SDaniel Lezcano 	return 0;
1312c35b7e72SDaniel Lezcano }
1313c35b7e72SDaniel Lezcano static inline void xfrm6_fini(void)
1314c35b7e72SDaniel Lezcano {
1315c35b7e72SDaniel Lezcano 	;
1316c35b7e72SDaniel Lezcano }
1317c35b7e72SDaniel Lezcano #endif
13181da177e4SLinus Torvalds 
1319558f82efSMasahide NAKAMURA #ifdef CONFIG_XFRM_STATISTICS
1320c68cd1a0SAlexey Dobriyan extern int xfrm_proc_init(struct net *net);
1321c68cd1a0SAlexey Dobriyan extern void xfrm_proc_fini(struct net *net);
1322558f82efSMasahide NAKAMURA #endif
1323558f82efSMasahide NAKAMURA 
1324b27aeadbSAlexey Dobriyan extern int xfrm_sysctl_init(struct net *net);
1325b27aeadbSAlexey Dobriyan #ifdef CONFIG_SYSCTL
1326b27aeadbSAlexey Dobriyan extern void xfrm_sysctl_fini(struct net *net);
1327b27aeadbSAlexey Dobriyan #else
1328b27aeadbSAlexey Dobriyan static inline void xfrm_sysctl_fini(struct net *net)
1329b27aeadbSAlexey Dobriyan {
1330b27aeadbSAlexey Dobriyan }
1331b27aeadbSAlexey Dobriyan #endif
1332b27aeadbSAlexey Dobriyan 
13335c182458SHerbert Xu extern void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto);
1334284fa7daSAlexey Dobriyan extern int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
13354c563f76STimo Teras 			   int (*func)(struct xfrm_state *, int, void*), void *);
1336abb81c4fSHerbert Xu extern void xfrm_state_walk_done(struct xfrm_state_walk *walk);
1337673c09beSAlexey Dobriyan extern struct xfrm_state *xfrm_state_alloc(struct net *net);
133833765d06SDavid S. Miller extern struct xfrm_state *xfrm_state_find(const xfrm_address_t *daddr,
133933765d06SDavid S. Miller 					  const xfrm_address_t *saddr,
1340b520e9f6SDavid S. Miller 					  const struct flowi *fl,
1341b520e9f6SDavid S. Miller 					  struct xfrm_tmpl *tmpl,
13421da177e4SLinus Torvalds 					  struct xfrm_policy *pol, int *err,
13431da177e4SLinus Torvalds 					  unsigned short family);
1344bd55775cSJamal Hadi Salim extern struct xfrm_state *xfrm_stateonly_find(struct net *net, u32 mark,
13455447c5e4SAlexey Dobriyan 					       xfrm_address_t *daddr,
1346628529b6SJamal Hadi Salim 					       xfrm_address_t *saddr,
1347628529b6SJamal Hadi Salim 					       unsigned short family,
1348628529b6SJamal Hadi Salim 					       u8 mode, u8 proto, u32 reqid);
13491da177e4SLinus Torvalds extern int xfrm_state_check_expire(struct xfrm_state *x);
13501da177e4SLinus Torvalds extern void xfrm_state_insert(struct xfrm_state *x);
13511da177e4SLinus Torvalds extern int xfrm_state_add(struct xfrm_state *x);
13521da177e4SLinus Torvalds extern int xfrm_state_update(struct xfrm_state *x);
1353bd55775cSJamal Hadi Salim extern struct xfrm_state *xfrm_state_lookup(struct net *net, u32 mark,
1354a70486f0SDavid S. Miller 					    const xfrm_address_t *daddr, __be32 spi,
1355bd55775cSJamal Hadi Salim 					    u8 proto, unsigned short family);
1356bd55775cSJamal Hadi Salim extern struct xfrm_state *xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1357a70486f0SDavid S. Miller 						   const xfrm_address_t *daddr,
1358a70486f0SDavid S. Miller 						   const xfrm_address_t *saddr,
1359bd55775cSJamal Hadi Salim 						   u8 proto,
1360bd55775cSJamal Hadi Salim 						   unsigned short family);
136141a49cc3SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
136241a49cc3SMasahide NAKAMURA extern int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src,
136341a49cc3SMasahide NAKAMURA 			  int n, unsigned short family);
136441a49cc3SMasahide NAKAMURA extern int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src,
136541a49cc3SMasahide NAKAMURA 			   int n, unsigned short family);
136641a49cc3SMasahide NAKAMURA #else
136741a49cc3SMasahide NAKAMURA static inline int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src,
136841a49cc3SMasahide NAKAMURA 				 int n, unsigned short family)
136941a49cc3SMasahide NAKAMURA {
137041a49cc3SMasahide NAKAMURA 	return -ENOSYS;
137141a49cc3SMasahide NAKAMURA }
137241a49cc3SMasahide NAKAMURA 
137341a49cc3SMasahide NAKAMURA static inline int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src,
137441a49cc3SMasahide NAKAMURA 				  int n, unsigned short family)
137541a49cc3SMasahide NAKAMURA {
137641a49cc3SMasahide NAKAMURA 	return -ENOSYS;
137741a49cc3SMasahide NAKAMURA }
137841a49cc3SMasahide NAKAMURA #endif
1379af11e316SJamal Hadi Salim 
1380af11e316SJamal Hadi Salim struct xfrmk_sadinfo {
1381af11e316SJamal Hadi Salim 	u32 sadhcnt; /* current hash bkts */
1382af11e316SJamal Hadi Salim 	u32 sadhmcnt; /* max allowed hash bkts */
1383af11e316SJamal Hadi Salim 	u32 sadcnt; /* current running count */
1384af11e316SJamal Hadi Salim };
1385af11e316SJamal Hadi Salim 
13865a6d3416SJamal Hadi Salim struct xfrmk_spdinfo {
13875a6d3416SJamal Hadi Salim 	u32 incnt;
13885a6d3416SJamal Hadi Salim 	u32 outcnt;
13895a6d3416SJamal Hadi Salim 	u32 fwdcnt;
13905a6d3416SJamal Hadi Salim 	u32 inscnt;
13915a6d3416SJamal Hadi Salim 	u32 outscnt;
13925a6d3416SJamal Hadi Salim 	u32 fwdscnt;
13935a6d3416SJamal Hadi Salim 	u32 spdhcnt;
13945a6d3416SJamal Hadi Salim 	u32 spdhmcnt;
13955a6d3416SJamal Hadi Salim };
13965a6d3416SJamal Hadi Salim 
1397bd55775cSJamal Hadi Salim extern struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark,
1398bd55775cSJamal Hadi Salim 					      u32 seq);
139926b15dadSJamal Hadi Salim extern int xfrm_state_delete(struct xfrm_state *x);
14000e602451SAlexey Dobriyan extern int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info);
1401e071041bSAlexey Dobriyan extern void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si);
1402e071041bSAlexey Dobriyan extern void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si);
1403afeb14b4SPaul Moore extern int xfrm_replay_check(struct xfrm_state *x,
1404afeb14b4SPaul Moore 			     struct sk_buff *skb, __be32 seq);
140561f4627bSAl Viro extern void xfrm_replay_advance(struct xfrm_state *x, __be32 seq);
1406f8cd5488SJamal Hadi Salim extern void xfrm_replay_notify(struct xfrm_state *x, int event);
14071da177e4SLinus Torvalds extern int xfrm_state_mtu(struct xfrm_state *x, int mtu);
140872cb6962SHerbert Xu extern int xfrm_init_state(struct xfrm_state *x);
1409227620e2SHerbert Xu extern int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb);
1410716062fdSHerbert Xu extern int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi,
1411716062fdSHerbert Xu 		      int encap_type);
14121bf06cd2SHerbert Xu extern int xfrm_input_resume(struct sk_buff *skb, int nexthdr);
1413c6581a45SHerbert Xu extern int xfrm_output_resume(struct sk_buff *skb, int err);
1414406ef77cSHerbert Xu extern int xfrm_output(struct sk_buff *skb);
1415df9dcb45SKazunori MIYAZAWA extern int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb);
141636cf9acfSHerbert Xu extern int xfrm4_extract_header(struct sk_buff *skb);
1417227620e2SHerbert Xu extern int xfrm4_extract_input(struct xfrm_state *x, struct sk_buff *skb);
1418c4541b41SHerbert Xu extern int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
1419c4541b41SHerbert Xu 			   int encap_type);
1420716062fdSHerbert Xu extern int xfrm4_transport_finish(struct sk_buff *skb, int async);
14211da177e4SLinus Torvalds extern int xfrm4_rcv(struct sk_buff *skb);
1422c4541b41SHerbert Xu 
1423c4541b41SHerbert Xu static inline int xfrm4_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi)
1424c4541b41SHerbert Xu {
1425c4541b41SHerbert Xu 	return xfrm4_rcv_encap(skb, nexthdr, spi, 0);
1426c4541b41SHerbert Xu }
1427c4541b41SHerbert Xu 
142836cf9acfSHerbert Xu extern int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb);
142936cf9acfSHerbert Xu extern int xfrm4_prepare_output(struct xfrm_state *x, struct sk_buff *skb);
14301da177e4SLinus Torvalds extern int xfrm4_output(struct sk_buff *skb);
1431c0d56408SKazunori MIYAZAWA extern int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family);
1432c0d56408SKazunori MIYAZAWA extern int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family);
143336cf9acfSHerbert Xu extern int xfrm6_extract_header(struct sk_buff *skb);
1434227620e2SHerbert Xu extern int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb);
143533b5ecb8SHerbert Xu extern int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi);
1436716062fdSHerbert Xu extern int xfrm6_transport_finish(struct sk_buff *skb, int async);
1437e5bbef20SHerbert Xu extern int xfrm6_rcv(struct sk_buff *skb);
1438fbd9a5b4SMasahide NAKAMURA extern int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
1439fbd9a5b4SMasahide NAKAMURA 			    xfrm_address_t *saddr, u8 proto);
144073d605d1SKazunori MIYAZAWA extern int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family);
144173d605d1SKazunori MIYAZAWA extern int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family);
1442a1664773SAlexey Dobriyan extern __be32 xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr);
1443a1664773SAlexey Dobriyan extern __be32 xfrm6_tunnel_spi_lookup(struct net *net, xfrm_address_t *saddr);
144436cf9acfSHerbert Xu extern int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb);
144536cf9acfSHerbert Xu extern int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb);
14461da177e4SLinus Torvalds extern int xfrm6_output(struct sk_buff *skb);
1447aee5adb4SMasahide NAKAMURA extern int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb,
1448aee5adb4SMasahide NAKAMURA 				 u8 **prevhdr);
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds #ifdef CONFIG_XFRM
1451067b207bSJames Chapman extern int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb);
14521da177e4SLinus Torvalds extern int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen);
14531da177e4SLinus Torvalds #else
14541da177e4SLinus Torvalds static inline int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
14551da177e4SLinus Torvalds {
14561da177e4SLinus Torvalds  	return -ENOPROTOOPT;
14571da177e4SLinus Torvalds }
14581da177e4SLinus Torvalds 
1459067b207bSJames Chapman static inline int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
14601da177e4SLinus Torvalds {
14611da177e4SLinus Torvalds  	/* should not happen */
14621da177e4SLinus Torvalds  	kfree_skb(skb);
14631da177e4SLinus Torvalds 	return 0;
14641da177e4SLinus Torvalds }
14651da177e4SLinus Torvalds #endif
14661da177e4SLinus Torvalds 
14670331b1f3SAlexey Dobriyan struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp);
14684c563f76STimo Teras 
146912a169e7SHerbert Xu extern void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type);
1470cdcbca7cSAlexey Dobriyan extern int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
14714c563f76STimo Teras 	int (*func)(struct xfrm_policy *, int, int, void*), void *);
147212a169e7SHerbert Xu extern void xfrm_policy_walk_done(struct xfrm_policy_walk *walk);
14731da177e4SLinus Torvalds int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl);
14748ca2e93bSJamal Hadi Salim struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark,
14758ca2e93bSJamal Hadi Salim 					  u8 type, int dir,
14764e81bb83SMasahide NAKAMURA 					  struct xfrm_selector *sel,
1477ef41aaa0SEric Paris 					  struct xfrm_sec_ctx *ctx, int delete,
1478ef41aaa0SEric Paris 					  int *err);
14798ca2e93bSJamal Hadi Salim struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8, int dir, u32 id, int delete, int *err);
148033ffbbd5SAlexey Dobriyan int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info);
14811da177e4SLinus Torvalds u32 xfrm_get_acqseq(void);
1482658b219eSHerbert Xu extern int xfrm_alloc_spi(struct xfrm_state *x, u32 minspi, u32 maxspi);
1483bd55775cSJamal Hadi Salim struct xfrm_state *xfrm_find_acq(struct net *net, struct xfrm_mark *mark,
1484bd55775cSJamal Hadi Salim 				 u8 mode, u32 reqid, u8 proto,
1485a70486f0SDavid S. Miller 				 const xfrm_address_t *daddr,
1486a70486f0SDavid S. Miller 				 const xfrm_address_t *saddr, int create,
1487bd55775cSJamal Hadi Salim 				 unsigned short family);
14881da177e4SLinus Torvalds extern int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol);
14891da177e4SLinus Torvalds 
149080c9abaaSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
1491183cad12SDavid S. Miller extern int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1492183cad12SDavid S. Miller 		      const struct xfrm_migrate *m, int num_bundles,
1493183cad12SDavid S. Miller 		      const struct xfrm_kmaddress *k);
149480c9abaaSShinta Sugimoto extern struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m);
149580c9abaaSShinta Sugimoto extern struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
149680c9abaaSShinta Sugimoto 					      struct xfrm_migrate *m);
1497b4b7c0b3SDavid S. Miller extern int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
149813c1d189SArnaud Ebalard 			struct xfrm_migrate *m, int num_bundles,
149913c1d189SArnaud Ebalard 			struct xfrm_kmaddress *k);
150080c9abaaSShinta Sugimoto #endif
150180c9abaaSShinta Sugimoto 
15025d36b180SAl Viro extern int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport);
15036c5c8ca7SJamal Hadi Salim extern void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid);
1504db983c11SAlexey Dobriyan extern int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr);
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds extern void xfrm_input_init(void);
15076067b2baSAl Viro extern int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq);
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds extern void xfrm_probe_algs(void);
15101da177e4SLinus Torvalds extern int xfrm_count_auth_supported(void);
15111da177e4SLinus Torvalds extern int xfrm_count_enc_supported(void);
15121da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx);
15131da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx);
15141da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id);
15151da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id);
15161da177e4SLinus Torvalds extern struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id);
15176f2f19edSDavid S. Miller extern struct xfrm_algo_desc *xfrm_aalg_get_byname(const char *name, int probe);
15186f2f19edSDavid S. Miller extern struct xfrm_algo_desc *xfrm_ealg_get_byname(const char *name, int probe);
15196f2f19edSDavid S. Miller extern struct xfrm_algo_desc *xfrm_calg_get_byname(const char *name, int probe);
15206f2f19edSDavid S. Miller extern struct xfrm_algo_desc *xfrm_aead_get_byname(const char *name, int icv_len,
15211a6509d9SHerbert Xu 						   int probe);
15221da177e4SLinus Torvalds 
152307d4ee58SHerbert Xu struct hash_desc;
15249409f38aSHerbert Xu struct scatterlist;
152507d4ee58SHerbert Xu typedef int (icv_update_fn_t)(struct hash_desc *, struct scatterlist *,
152607d4ee58SHerbert Xu 			      unsigned int);
15271da177e4SLinus Torvalds 
1528ff6acd16SDavid S. Miller static inline int xfrm_addr_cmp(const xfrm_address_t *a,
1529ff6acd16SDavid S. Miller 				const xfrm_address_t *b,
15301da177e4SLinus Torvalds 				int family)
15311da177e4SLinus Torvalds {
15321da177e4SLinus Torvalds 	switch (family) {
15331da177e4SLinus Torvalds 	default:
15341da177e4SLinus Torvalds 	case AF_INET:
1535a6337463Sjamal 		return (__force u32)a->a4 - (__force u32)b->a4;
15361da177e4SLinus Torvalds 	case AF_INET6:
15371da177e4SLinus Torvalds 		return ipv6_addr_cmp((struct in6_addr *)a,
15381da177e4SLinus Torvalds 				     (struct in6_addr *)b);
15391da177e4SLinus Torvalds 	}
15401da177e4SLinus Torvalds }
15411da177e4SLinus Torvalds 
154277d8d7a6SHerbert Xu static inline int xfrm_policy_id2dir(u32 index)
154377d8d7a6SHerbert Xu {
154477d8d7a6SHerbert Xu 	return index & 7;
154577d8d7a6SHerbert Xu }
154677d8d7a6SHerbert Xu 
1547a6483b79SAlexey Dobriyan #ifdef CONFIG_XFRM
1548a6483b79SAlexey Dobriyan static inline int xfrm_aevent_is_on(struct net *net)
1549f8cd5488SJamal Hadi Salim {
1550be33690dSPatrick McHardy 	struct sock *nlsk;
1551be33690dSPatrick McHardy 	int ret = 0;
1552be33690dSPatrick McHardy 
1553be33690dSPatrick McHardy 	rcu_read_lock();
1554a6483b79SAlexey Dobriyan 	nlsk = rcu_dereference(net->xfrm.nlsk);
1555be33690dSPatrick McHardy 	if (nlsk)
1556be33690dSPatrick McHardy 		ret = netlink_has_listeners(nlsk, XFRMNLGRP_AEVENTS);
1557be33690dSPatrick McHardy 	rcu_read_unlock();
1558be33690dSPatrick McHardy 	return ret;
1559f8cd5488SJamal Hadi Salim }
1560a6483b79SAlexey Dobriyan #endif
1561f8cd5488SJamal Hadi Salim 
156285158621SDavid S. Miller static inline int xfrm_alg_len(const struct xfrm_algo *alg)
15630f99be0dSEric Dumazet {
15640f99be0dSEric Dumazet 	return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
15650f99be0dSEric Dumazet }
15660f99be0dSEric Dumazet 
156785158621SDavid S. Miller static inline int xfrm_alg_auth_len(const struct xfrm_algo_auth *alg)
15684447bb33SMartin Willi {
15694447bb33SMartin Willi 	return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
15704447bb33SMartin Willi }
15714447bb33SMartin Willi 
157280c9abaaSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
157380c9abaaSShinta Sugimoto static inline struct xfrm_algo *xfrm_algo_clone(struct xfrm_algo *orig)
157480c9abaaSShinta Sugimoto {
15750f99be0dSEric Dumazet 	return kmemdup(orig, xfrm_alg_len(orig), GFP_KERNEL);
157680c9abaaSShinta Sugimoto }
157780c9abaaSShinta Sugimoto 
15784447bb33SMartin Willi static inline struct xfrm_algo_auth *xfrm_algo_auth_clone(struct xfrm_algo_auth *orig)
15794447bb33SMartin Willi {
15804447bb33SMartin Willi 	return kmemdup(orig, xfrm_alg_auth_len(orig), GFP_KERNEL);
15814447bb33SMartin Willi }
15824447bb33SMartin Willi 
158380c9abaaSShinta Sugimoto static inline void xfrm_states_put(struct xfrm_state **states, int n)
158480c9abaaSShinta Sugimoto {
158580c9abaaSShinta Sugimoto 	int i;
158680c9abaaSShinta Sugimoto 	for (i = 0; i < n; i++)
158780c9abaaSShinta Sugimoto 		xfrm_state_put(*(states + i));
158880c9abaaSShinta Sugimoto }
158980c9abaaSShinta Sugimoto 
159080c9abaaSShinta Sugimoto static inline void xfrm_states_delete(struct xfrm_state **states, int n)
159180c9abaaSShinta Sugimoto {
159280c9abaaSShinta Sugimoto 	int i;
159380c9abaaSShinta Sugimoto 	for (i = 0; i < n; i++)
159480c9abaaSShinta Sugimoto 		xfrm_state_delete(*(states + i));
159580c9abaaSShinta Sugimoto }
159680c9abaaSShinta Sugimoto #endif
1597f8cd5488SJamal Hadi Salim 
1598def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
159900501121SHerbert Xu static inline struct xfrm_state *xfrm_input_state(struct sk_buff *skb)
160000501121SHerbert Xu {
160100501121SHerbert Xu 	return skb->sp->xvec[skb->sp->len - 1];
160200501121SHerbert Xu }
1603def8b4faSAlexey Dobriyan #endif
160400501121SHerbert Xu 
1605bf825f81SJamal Hadi Salim static inline int xfrm_mark_get(struct nlattr **attrs, struct xfrm_mark *m)
1606bf825f81SJamal Hadi Salim {
1607bf825f81SJamal Hadi Salim 	if (attrs[XFRMA_MARK])
16084efd7e83SAndreas Steffen 		memcpy(m, nla_data(attrs[XFRMA_MARK]), sizeof(struct xfrm_mark));
1609bf825f81SJamal Hadi Salim 	else
1610bf825f81SJamal Hadi Salim 		m->v = m->m = 0;
1611bf825f81SJamal Hadi Salim 
1612bf825f81SJamal Hadi Salim 	return m->v & m->m;
1613bf825f81SJamal Hadi Salim }
1614bf825f81SJamal Hadi Salim 
1615e3dfa389SDavid S. Miller static inline int xfrm_mark_put(struct sk_buff *skb, const struct xfrm_mark *m)
1616bf825f81SJamal Hadi Salim {
1617bf825f81SJamal Hadi Salim 	if (m->m | m->v)
1618bf825f81SJamal Hadi Salim 		NLA_PUT(skb, XFRMA_MARK, sizeof(struct xfrm_mark), m);
1619bf825f81SJamal Hadi Salim 	return 0;
1620bf825f81SJamal Hadi Salim 
1621bf825f81SJamal Hadi Salim nla_put_failure:
1622bf825f81SJamal Hadi Salim 	return -1;
1623bf825f81SJamal Hadi Salim }
1624bf825f81SJamal Hadi Salim 
16251da177e4SLinus Torvalds #endif	/* _NET_XFRM_H */
1626