xref: /illumos-gate/usr/src/uts/common/inet/ipsec_info.h (revision 9ec394dbf343c1f23c6e13c39df427f238e5a369)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_INET_IPSEC_INFO_H
27 #define	_INET_IPSEC_INFO_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #ifdef	__cplusplus
32 extern "C" {
33 #endif
34 
35 #include <sys/crypto/common.h>
36 
37 /*
38  * IPsec informational messages.  These are M_CTL STREAMS messages, which
39  * convey IPsec information between various IP and related modules.  The
40  * messages come in a few flavors:
41  *
42  *	* IPSEC_{IN,OUT}  -  These show what IPsec action have been taken (for
43  *	  inbound datagrams), or need to be taken (for outbound datagrams).
44  *	  They flow between AH/ESP and IP.
45  *
46  *	* Keysock consumer interface  -  These messages are wrappers for
47  *	  PF_KEY messages.  They flow between AH/ESP and keysock.
48  *
49  * Some of these messages include pointers such as a netstack_t pointer.
50  * We do not explicitly reference count those with netstack_hold/rele,
51  * since we depend on IP's ability to discard all of the IPSEC_{IN,OUT}
52  * messages in order to handle the ipsa pointers.
53  * We have special logic when doing asynch callouts to kEF for which we
54  * verify netstack_t pointer using the netstackid_t.
55  */
56 
57 /*
58  * The IPsec M_CTL value MUST be something that will not be even close
59  * to an IPv4 or IPv6 header.  This means the first byte must not be
60  * 0x40 - 0x4f or 0x60-0x6f.  For big-endian machines, this is fixable with
61  * the IPSEC_M_CTL prefix.  For little-endian machines, the actual M_CTL
62  * _type_ must not be in the aforementioned ranges.
63  *
64  * The reason for this avoidance is because M_CTL's with a real IPv4/IPv6
65  * datagram get sent from to TCP or UDP when an ICMP datagram affects a
66  * TCP/UDP session.
67  */
68 #define	IPSEC_M_CTL	0x73706900
69 
70 /*
71  * M_CTL types for IPsec messages.  Remember, the values 0x40 - 0x4f and 0x60
72  * - 0x6f are not to be used because of potential little-endian confusion.
73  *
74  * Offsets 1-25 (decimal) are in use, spread through this file.
75  * Check for duplicates through the whole file before adding.
76  */
77 
78 /*
79  * IPSEC_{IN,OUT} policy expressors.
80  */
81 #define	IPSEC_IN	(IPSEC_M_CTL + 1)
82 #define	IPSEC_OUT	(IPSEC_M_CTL + 2)
83 
84 /*
85  * This is used for communication between IP and IPSEC (AH/ESP)
86  * for Inbound datagrams. IPSEC_IN is allocated by IP before IPSEC
87  * processing begins. On return spi fields are initialized so that
88  * IP can locate the security associations later on for doing policy
89  * checks. For loopback case, IPSEC processing is not done. But the
90  * attributes of the security are reflected in <foo>_done fields below.
91  * The code in policy check infers that it is a loopback case and
92  * would not try to get the associations.
93  *
94  * The comment below (and for other netstack_t references) refers
95  * to the fact that we only do netstack_hold in particular cases,
96  * such as the references from open streams (ill_t and conn_t's
97  * pointers). Internally within IP we rely on IP's ability to cleanup e.g.
98  * ire_t's when an ill goes away.
99  */
100 typedef struct ipsec_in_s {
101 	uint32_t ipsec_in_type;
102 	uint32_t ipsec_in_len;
103 	frtn_t ipsec_in_frtn;		/* for esballoc() callback */
104 	struct ipsa_s 	*ipsec_in_ah_sa;	/* SA for AH */
105 	struct ipsa_s 	*ipsec_in_esp_sa;	/* SA for ESP */
106 
107 	struct ipsec_policy_head_s *ipsec_in_policy;
108 	struct ipsec_action_s *ipsec_in_action; /* how we made it in.. */
109 	unsigned int
110 		ipsec_in_secure : 1,	/* Is the message attached secure ? */
111 		ipsec_in_v4 : 1,	/* Is this an ipv4 packet ? */
112 		ipsec_in_loopback : 1,	/* Is this a loopback request ? */
113 		ipsec_in_dont_check : 1, /* Used by TCP to avoid policy check */
114 
115 		ipsec_in_decaps : 1,	/* Was this packet decapsulated from */
116 					/* a matching inner packet? */
117 		ipsec_in_attach_if : 1,	/* Don't load spread this packet */
118 		ipsec_in_accelerated : 1, /* hardware accelerated packet */
119 
120 		ipsec_in_icmp_loopback : 1, /* Looped-back ICMP packet, */
121 					    /* all should trust this. */
122 		ipsec_in_pad_bits : 24;
123 
124 	int    ipsec_in_ill_index;	/* interface on which ipha_dst was */
125 					/* configured when pkt was recv'd  */
126 	int    ipsec_in_rill_index;	/* interface on which pkt was recv'd */
127 	uint32_t ipsec_in_esp_udp_ports;	/* For an ESP-in-UDP packet. */
128 	mblk_t *ipsec_in_da;		/* data attr. for accelerated pkts */
129 
130 	/*
131 	 * For call to the kernel crypto framework. State needed during
132 	 * the execution of a crypto request. Storing these here
133 	 * allow us to avoid a separate allocation before calling the
134 	 * crypto framework.
135 	 */
136 	size_t ipsec_in_skip_len;		/* len to skip for AH auth */
137 	crypto_data_t ipsec_in_crypto_data;	/* single op crypto data */
138 	crypto_dual_data_t ipsec_in_crypto_dual_data; /* for dual ops */
139 	crypto_data_t ipsec_in_crypto_mac;	/* to store the MAC */
140 
141 	zoneid_t ipsec_in_zoneid;	/* target zone for the datagram */
142 	netstack_t *ipsec_in_ns;	/* Does not have a netstack_hold */
143 	netstackid_t ipsec_in_stackid;	/* Used while waing for kEF callback */
144 } ipsec_in_t;
145 
146 #define	IPSECOUT_MAX_ADDRLEN 4	/* Max addr len. (in 32-bit words) */
147 /*
148  * This is used for communication between IP and IPSEC (AH/ESP)
149  * for Outbound datagrams. IPSEC_OUT is allocated by IP before IPSEC
150  * processing begins. On return SA fields are initialized so that
151  * IP can locate the security associations later on for doing policy
152  * checks.  The policy and the actions associated with this packet are
153  * stored in the ipsec_out_policy and ipsec_out_act fields respectively.
154  * IPSEC_OUT is also used to carry non-ipsec information when conn is
155  * absent or the conn information is lost across the calls to ARP.
156  * example: message from ARP or from ICMP error routines.
157  */
158 typedef struct ipsec_out_s {
159 	uint32_t ipsec_out_type;
160 	uint32_t ipsec_out_len;
161 	frtn_t ipsec_out_frtn;		/* for esballoc() callback */
162 	struct ipsec_policy_head_s *ipsec_out_polhead;
163 	ipsec_latch_t		*ipsec_out_latch;
164 	struct ipsec_policy_s 	*ipsec_out_policy; /* why are we here? */
165 	struct ipsec_action_s	*ipsec_out_act;	/* what do we want? */
166 	struct ipsa_s	*ipsec_out_ah_sa; /* AH SA used for the packet */
167 	struct ipsa_s	*ipsec_out_esp_sa; /* ESP SA used for the packet */
168 	/*
169 	 * NOTE: "Source" and "Dest" are w.r.t. outbound datagrams.  Ports can
170 	 *	 be zero, and the protocol number is needed to make the ports
171 	 *	 significant.
172 	 */
173 	uint16_t ipsec_out_src_port;	/* Source port number of d-gram. */
174 	uint16_t ipsec_out_dst_port;	/* Destination port number of d-gram. */
175 	uint8_t  ipsec_out_icmp_type;	/* ICMP type of d-gram */
176 	uint8_t  ipsec_out_icmp_code;	/* ICMP code of d-gram */
177 
178 	sa_family_t ipsec_out_inaf;	/* Inner address family */
179 	uint32_t ipsec_out_insrc[IPSECOUT_MAX_ADDRLEN];	/* Inner src address */
180 	uint32_t ipsec_out_indst[IPSECOUT_MAX_ADDRLEN];	/* Inner dest address */
181 	uint8_t  ipsec_out_insrcpfx;	/* Inner source prefix */
182 	uint8_t  ipsec_out_indstpfx;	/* Inner destination prefix */
183 
184 	uint_t ipsec_out_ill_index;	/* ill index used for multicast etc. */
185 	uint8_t ipsec_out_proto;	/* IP protocol number for d-gram. */
186 	unsigned int
187 		ipsec_out_tunnel : 1,	/* Tunnel mode? */
188 		ipsec_out_use_global_policy : 1, /* Inherit global policy ? */
189 		ipsec_out_secure : 1,	/* Is this secure ? */
190 		ipsec_out_proc_begin : 1, /* IPSEC processing begun */
191 		/*
192 		 * Following five values reflects the values stored
193 		 * in conn.
194 		 */
195 		ipsec_out_multicast_loop : 1,
196 		ipsec_out_dontroute : 1,
197 		ipsec_out_reserved : 1,
198 		ipsec_out_v4 : 1,
199 
200 		ipsec_out_attach_if : 1,
201 		ipsec_out_unspec_src : 1,	/* IPv6 ip6i_t info */
202 		ipsec_out_reachable : 1, 	/* NDP reachability info */
203 		ipsec_out_failed: 1,
204 
205 		ipsec_out_se_done: 1,
206 		ipsec_out_esp_done: 1,
207 		ipsec_out_ah_done: 1,
208 		ipsec_out_need_policy: 1,
209 
210 		/*
211 		 * To indicate that packet must be accelerated, i.e.
212 		 * ICV or encryption performed, by Provider.
213 		 */
214 		ipsec_out_accelerated : 1,
215 		/*
216 		 * Used by IP to tell IPsec that the outbound ill for this
217 		 * packet supports acceleration of the AH or ESP prototocol.
218 		 * If set, ipsec_out_capab_ill_index contains the
219 		 * index of the ill.
220 		 */
221 		ipsec_out_is_capab_ill : 1,
222 		/*
223 		 * Indicates ICMP message destined for self.  These
224 		 * messages are to be trusted by all receivers.
225 		 */
226 		ipsec_out_icmp_loopback: 1,
227 		ipsec_out_ip_nexthop : 1,	/* IP_NEXTHOP option is set */
228 		ipsec_out_pad_bits : 12;
229 	cred_t	*ipsec_out_cred;
230 	uint32_t ipsec_out_capab_ill_index;
231 
232 	/*
233 	 * For call to the kernel crypto framework. State needed during
234 	 * the execution of a crypto request. Storing these here
235 	 * allow us to avoid a separate allocation before calling the
236 	 * crypto framework.
237 	 */
238 	size_t ipsec_out_skip_len;		/* len to skip for AH auth */
239 	crypto_data_t ipsec_out_crypto_data;	/* single op crypto data */
240 	crypto_dual_data_t ipsec_out_crypto_dual_data; /* for dual ops */
241 	crypto_data_t ipsec_out_crypto_mac;	/* to store the MAC */
242 
243 	zoneid_t ipsec_out_zoneid;	/* source zone for the datagram */
244 	in6_addr_t ipsec_out_nexthop_v6;	/* nexthop IP address */
245 #define	ipsec_out_nexthop_addr V4_PART_OF_V6(ipsec_out_nexthop_v6)
246 	netstack_t *ipsec_out_ns;	/* Does not have a netstack_hold */
247 	netstackid_t ipsec_out_stackid;	/* Used while waing for kEF callback */
248 } ipsec_out_t;
249 
250 /*
251  * This is used to mark the ipsec_out_t *req* fields
252  * when the operation is done without affecting the
253  * requests.
254  */
255 #define	IPSEC_REQ_DONE		0x80000000
256 /*
257  * Operation could not be performed by the AH/ESP
258  * module.
259  */
260 #define	IPSEC_REQ_FAILED	0x40000000
261 
262 /*
263  * Keysock consumer interface.
264  *
265  * The driver/module keysock (which is a driver to PF_KEY sockets, but is
266  * a module to 'consumers' like AH and ESP) uses keysock consumer interface
267  * messages to pass on PF_KEY messages to consumers who process and act upon
268  * them.
269  */
270 #define	KEYSOCK_IN		(IPSEC_M_CTL + 3)
271 #define	KEYSOCK_OUT		(IPSEC_M_CTL + 4)
272 #define	KEYSOCK_OUT_ERR		(IPSEC_M_CTL + 5)
273 #define	KEYSOCK_HELLO		(IPSEC_M_CTL + 6)
274 #define	KEYSOCK_HELLO_ACK	(IPSEC_M_CTL + 7)
275 
276 /*
277  * KEYSOCK_HELLO is sent by keysock to a consumer when it is pushed on top
278  * of one (i.e. opened as a module).
279  *
280  * NOTE: Keysock_hello is simply an ipsec_info_t
281  */
282 
283 /* TUN_HELLO is just like KEYSOCK_HELLO, except for tunnels to talk with IP. */
284 #define	TUN_HELLO		KEYSOCK_HELLO
285 
286 /*
287  * KEYSOCK_HELLO_ACK is sent by a consumer to acknowledge a KEYSOCK_HELLO.
288  * It contains the PF_KEYv2 sa_type, so keysock can redirect PF_KEY messages
289  * to the right consumer.
290  */
291 typedef struct keysock_hello_ack_s {
292 	uint32_t ks_hello_type;
293 	uint32_t ks_hello_len;
294 	uint8_t ks_hello_satype;	/* PF_KEYv2 sa_type of ks client */
295 } keysock_hello_ack_t;
296 
297 #define	KS_IN_ADDR_UNKNOWN 0
298 #define	KS_IN_ADDR_NOTTHERE 1
299 #define	KS_IN_ADDR_UNSPEC 2
300 #define	KS_IN_ADDR_ME 3
301 #define	KS_IN_ADDR_NOTME 4
302 #define	KS_IN_ADDR_MBCAST 5
303 #define	KS_IN_ADDR_DONTCARE 6
304 
305 /*
306  * KEYSOCK_IN is a PF_KEY message from a PF_KEY socket destined for a consumer.
307  */
308 typedef struct keysock_in_s {
309 	uint32_t ks_in_type;
310 	uint32_t ks_in_len;
311 	/*
312 	 * NOTE:	These pointers MUST be into the M_DATA that follows
313 	 *		this M_CTL message.  If they aren't, weirdness
314 	 *		results.
315 	 */
316 	struct sadb_ext *ks_in_extv[SADB_EXT_MAX + 1];
317 	int ks_in_srctype;	/* Source address type. */
318 	int ks_in_dsttype;	/* Dest address type. */
319 	minor_t ks_in_serial;	/* Serial # of sending socket. */
320 } keysock_in_t;
321 
322 /*
323  * KEYSOCK_OUT is a PF_KEY message from a consumer destined for a PF_KEY
324  * socket.
325  */
326 typedef struct keysock_out_s {
327 	uint32_t ks_out_type;
328 	uint32_t ks_out_len;
329 	minor_t ks_out_serial;	/* Serial # of sending socket. */
330 } keysock_out_t;
331 
332 /*
333  * KEYSOCK_OUT_ERR is sent to a consumer from keysock if for some reason
334  * keysock could not find a PF_KEY socket to deliver a consumer-originated
335  * message (e.g. SADB_ACQUIRE).
336  */
337 typedef struct keysock_out_err_s {
338 	uint32_t ks_err_type;
339 	uint32_t ks_err_len;
340 	minor_t ks_err_serial;
341 	int ks_err_errno;
342 	/*
343 	 * Other, richer error information may end up going here eventually.
344 	 */
345 } keysock_out_err_t;
346 
347 /*
348  * M_CTL message type for sending inbound pkt information between IP & ULP.
349  * These are _not_ related to IPsec in any way, but are here so that there is
350  * one place where all these values are defined which makes it easier to track.
351  * The choice of this value has the same rationale as explained above.
352  */
353 #define	IN_PKTINFO		(IPSEC_M_CTL + 24)
354 
355 
356 /*
357  * IPSEC_CTL messages are used by IPsec to send control type requests
358  * to IP. Such a control message is currently used by IPsec to request
359  * that IP send the contents of an IPsec SA or the entire SADB to
360  * every IPsec hardware acceleration capable provider.
361  */
362 
363 #define	IPSEC_CTL		(IPSEC_M_CTL + 25)
364 
365 typedef struct ipsec_ctl_s {
366 	uint32_t ipsec_ctl_type;
367 	uint32_t ipsec_ctl_len;
368 	uint_t ipsec_ctl_sa_type;
369 	void *ipsec_ctl_sa;
370 } ipsec_ctl_t;
371 
372 
373 /*
374  * All IPsec informational messages are placed into the ipsec_info_t
375  * union, so that allocation can be done once, and IPsec informational
376  * messages can be recycled.
377  */
378 typedef union ipsec_info_u {
379 	struct {
380 		uint32_t ipsec_allu_type;
381 		uint32_t ipsec_allu_len;	/* In bytes */
382 	} ipsec_allu;
383 	ipsec_in_t ipsec_in;
384 	ipsec_out_t ipsec_out;
385 	keysock_hello_ack_t keysock_hello_ack;
386 	keysock_in_t keysock_in;
387 	keysock_out_t keysock_out;
388 	keysock_out_err_t keysock_out_err;
389 	ipsec_ctl_t ipsec_ctl;
390 } ipsec_info_t;
391 #define	ipsec_info_type ipsec_allu.ipsec_allu_type
392 #define	ipsec_info_len ipsec_allu.ipsec_allu_len
393 
394 #ifdef	__cplusplus
395 }
396 #endif
397 
398 #endif	/* _INET_IPSEC_INFO_H */
399