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 2007 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 	mblk_t *ipsec_in_da;		/* data attr. for accelerated pkts */
128 
129 	/*
130 	 * For call to the kernel crypto framework. State needed during
131 	 * the execution of a crypto request. Storing these here
132 	 * allow us to avoid a separate allocation before calling the
133 	 * crypto framework.
134 	 */
135 	size_t ipsec_in_skip_len;		/* len to skip for AH auth */
136 	crypto_data_t ipsec_in_crypto_data;	/* single op crypto data */
137 	crypto_dual_data_t ipsec_in_crypto_dual_data; /* for dual ops */
138 	crypto_data_t ipsec_in_crypto_mac;	/* to store the MAC */
139 
140 	zoneid_t ipsec_in_zoneid;	/* target zone for the datagram */
141 	netstack_t *ipsec_in_ns;	/* Does not have a netstack_hold */
142 	netstackid_t ipsec_in_stackid;	/* Used while waing for kEF callback */
143 } ipsec_in_t;
144 
145 #define	IPSECOUT_MAX_ADDRLEN 4	/* Max addr len. (in 32-bit words) */
146 /*
147  * This is used for communication between IP and IPSEC (AH/ESP)
148  * for Outbound datagrams. IPSEC_OUT is allocated by IP before IPSEC
149  * processing begins. On return SA fields are initialized so that
150  * IP can locate the security associations later on for doing policy
151  * checks.  The policy and the actions associated with this packet are
152  * stored in the ipsec_out_policy and ipsec_out_act fields respectively.
153  * IPSEC_OUT is also used to carry non-ipsec information when conn is
154  * absent or the conn information is lost across the calls to ARP.
155  * example: message from ARP or from ICMP error routines.
156  */
157 typedef struct ipsec_out_s {
158 	uint32_t ipsec_out_type;
159 	uint32_t ipsec_out_len;
160 	frtn_t ipsec_out_frtn;		/* for esballoc() callback */
161 	struct ipsec_policy_head_s *ipsec_out_polhead;
162 	ipsec_latch_t		*ipsec_out_latch;
163 	struct ipsec_policy_s 	*ipsec_out_policy; /* why are we here? */
164 	struct ipsec_action_s	*ipsec_out_act;	/* what do we want? */
165 	struct ipsa_s	*ipsec_out_ah_sa; /* AH SA used for the packet */
166 	struct ipsa_s	*ipsec_out_esp_sa; /* ESP SA used for the packet */
167 	/*
168 	 * NOTE: "Source" and "Dest" are w.r.t. outbound datagrams.  Ports can
169 	 *	 be zero, and the protocol number is needed to make the ports
170 	 *	 significant.
171 	 */
172 	uint16_t ipsec_out_src_port;	/* Source port number of d-gram. */
173 	uint16_t ipsec_out_dst_port;	/* Destination port number of d-gram. */
174 	uint8_t  ipsec_out_icmp_type;	/* ICMP type of d-gram */
175 	uint8_t  ipsec_out_icmp_code;	/* ICMP code of d-gram */
176 
177 	sa_family_t ipsec_out_inaf;	/* Inner address family */
178 	uint32_t ipsec_out_insrc[IPSECOUT_MAX_ADDRLEN];	/* Inner src address */
179 	uint32_t ipsec_out_indst[IPSECOUT_MAX_ADDRLEN];	/* Inner dest address */
180 	uint8_t  ipsec_out_insrcpfx;	/* Inner source prefix */
181 	uint8_t  ipsec_out_indstpfx;	/* Inner destination prefix */
182 
183 	uint_t ipsec_out_ill_index;	/* ill index used for multicast etc. */
184 	uint8_t ipsec_out_proto;	/* IP protocol number for d-gram. */
185 	unsigned int
186 		ipsec_out_tunnel : 1,	/* Tunnel mode? */
187 		ipsec_out_use_global_policy : 1, /* Inherit global policy ? */
188 		ipsec_out_secure : 1,	/* Is this secure ? */
189 		ipsec_out_proc_begin : 1, /* IPSEC processing begun */
190 		/*
191 		 * Following five values reflects the values stored
192 		 * in conn.
193 		 */
194 		ipsec_out_multicast_loop : 1,
195 		ipsec_out_dontroute : 1,
196 		ipsec_out_reserved : 1,
197 		ipsec_out_v4 : 1,
198 
199 		ipsec_out_attach_if : 1,
200 		ipsec_out_unspec_src : 1,	/* IPv6 ip6i_t info */
201 		ipsec_out_reachable : 1, 	/* NDP reachability info */
202 		ipsec_out_failed: 1,
203 
204 		ipsec_out_se_done: 1,
205 		ipsec_out_esp_done: 1,
206 		ipsec_out_ah_done: 1,
207 		ipsec_out_need_policy: 1,
208 
209 		/*
210 		 * To indicate that packet must be accelerated, i.e.
211 		 * ICV or encryption performed, by Provider.
212 		 */
213 		ipsec_out_accelerated : 1,
214 		/*
215 		 * Used by IP to tell IPsec that the outbound ill for this
216 		 * packet supports acceleration of the AH or ESP prototocol.
217 		 * If set, ipsec_out_capab_ill_index contains the
218 		 * index of the ill.
219 		 */
220 		ipsec_out_is_capab_ill : 1,
221 		/*
222 		 * Indicates ICMP message destined for self.  These
223 		 * messages are to be trusted by all receivers.
224 		 */
225 		ipsec_out_icmp_loopback: 1,
226 		ipsec_out_ip_nexthop : 1,	/* IP_NEXTHOP option is set */
227 		ipsec_out_pad_bits : 12;
228 	cred_t	*ipsec_out_cred;
229 	uint32_t ipsec_out_capab_ill_index;
230 
231 	/*
232 	 * For call to the kernel crypto framework. State needed during
233 	 * the execution of a crypto request. Storing these here
234 	 * allow us to avoid a separate allocation before calling the
235 	 * crypto framework.
236 	 */
237 	size_t ipsec_out_skip_len;		/* len to skip for AH auth */
238 	crypto_data_t ipsec_out_crypto_data;	/* single op crypto data */
239 	crypto_dual_data_t ipsec_out_crypto_dual_data; /* for dual ops */
240 	crypto_data_t ipsec_out_crypto_mac;	/* to store the MAC */
241 
242 	zoneid_t ipsec_out_zoneid;	/* source zone for the datagram */
243 	in6_addr_t ipsec_out_nexthop_v6;	/* nexthop IP address */
244 #define	ipsec_out_nexthop_addr V4_PART_OF_V6(ipsec_out_nexthop_v6)
245 	netstack_t *ipsec_out_ns;	/* Does not have a netstack_hold */
246 	netstackid_t ipsec_out_stackid;	/* Used while waing for kEF callback */
247 } ipsec_out_t;
248 
249 /*
250  * This is used to mark the ipsec_out_t *req* fields
251  * when the operation is done without affecting the
252  * requests.
253  */
254 #define	IPSEC_REQ_DONE		0x80000000
255 /*
256  * Operation could not be performed by the AH/ESP
257  * module.
258  */
259 #define	IPSEC_REQ_FAILED	0x40000000
260 
261 /*
262  * Keysock consumer interface.
263  *
264  * The driver/module keysock (which is a driver to PF_KEY sockets, but is
265  * a module to 'consumers' like AH and ESP) uses keysock consumer interface
266  * messages to pass on PF_KEY messages to consumers who process and act upon
267  * them.
268  */
269 #define	KEYSOCK_IN		(IPSEC_M_CTL + 3)
270 #define	KEYSOCK_OUT		(IPSEC_M_CTL + 4)
271 #define	KEYSOCK_OUT_ERR		(IPSEC_M_CTL + 5)
272 #define	KEYSOCK_HELLO		(IPSEC_M_CTL + 6)
273 #define	KEYSOCK_HELLO_ACK	(IPSEC_M_CTL + 7)
274 
275 /*
276  * KEYSOCK_HELLO is sent by keysock to a consumer when it is pushed on top
277  * of one (i.e. opened as a module).
278  *
279  * NOTE: Keysock_hello is simply an ipsec_info_t
280  */
281 
282 /* TUN_HELLO is just like KEYSOCK_HELLO, except for tunnels to talk with IP. */
283 #define	TUN_HELLO		KEYSOCK_HELLO
284 
285 /*
286  * KEYSOCK_HELLO_ACK is sent by a consumer to acknowledge a KEYSOCK_HELLO.
287  * It contains the PF_KEYv2 sa_type, so keysock can redirect PF_KEY messages
288  * to the right consumer.
289  */
290 typedef struct keysock_hello_ack_s {
291 	uint32_t ks_hello_type;
292 	uint32_t ks_hello_len;
293 	uint8_t ks_hello_satype;	/* PF_KEYv2 sa_type of ks client */
294 } keysock_hello_ack_t;
295 
296 #define	KS_IN_ADDR_UNKNOWN 0
297 #define	KS_IN_ADDR_NOTTHERE 1
298 #define	KS_IN_ADDR_UNSPEC 2
299 #define	KS_IN_ADDR_ME 3
300 #define	KS_IN_ADDR_NOTME 4
301 #define	KS_IN_ADDR_MBCAST 5
302 #define	KS_IN_ADDR_DONTCARE 6
303 
304 /*
305  * KEYSOCK_IN is a PF_KEY message from a PF_KEY socket destined for a consumer.
306  */
307 typedef struct keysock_in_s {
308 	uint32_t ks_in_type;
309 	uint32_t ks_in_len;
310 	/*
311 	 * NOTE:	These pointers MUST be into the M_DATA that follows
312 	 *		this M_CTL message.  If they aren't, weirdness
313 	 *		results.
314 	 */
315 	struct sadb_ext *ks_in_extv[SADB_EXT_MAX + 1];
316 	int ks_in_srctype;	/* Source address type. */
317 	int ks_in_dsttype;	/* Dest address type. */
318 	minor_t ks_in_serial;	/* Serial # of sending socket. */
319 } keysock_in_t;
320 
321 /*
322  * KEYSOCK_OUT is a PF_KEY message from a consumer destined for a PF_KEY
323  * socket.
324  */
325 typedef struct keysock_out_s {
326 	uint32_t ks_out_type;
327 	uint32_t ks_out_len;
328 	minor_t ks_out_serial;	/* Serial # of sending socket. */
329 } keysock_out_t;
330 
331 /*
332  * KEYSOCK_OUT_ERR is sent to a consumer from keysock if for some reason
333  * keysock could not find a PF_KEY socket to deliver a consumer-originated
334  * message (e.g. SADB_ACQUIRE).
335  */
336 typedef struct keysock_out_err_s {
337 	uint32_t ks_err_type;
338 	uint32_t ks_err_len;
339 	minor_t ks_err_serial;
340 	int ks_err_errno;
341 	/*
342 	 * Other, richer error information may end up going here eventually.
343 	 */
344 } keysock_out_err_t;
345 
346 /*
347  * M_CTL message type for sending inbound pkt information between IP & ULP.
348  * These are _not_ related to IPsec in any way, but are here so that there is
349  * one place where all these values are defined which makes it easier to track.
350  * The choice of this value has the same rationale as explained above.
351  */
352 #define	IN_PKTINFO		(IPSEC_M_CTL + 24)
353 
354 
355 /*
356  * IPSEC_CTL messages are used by IPsec to send control type requests
357  * to IP. Such a control message is currently used by IPsec to request
358  * that IP send the contents of an IPsec SA or the entire SADB to
359  * every IPsec hardware acceleration capable provider.
360  */
361 
362 #define	IPSEC_CTL		(IPSEC_M_CTL + 25)
363 
364 typedef struct ipsec_ctl_s {
365 	uint32_t ipsec_ctl_type;
366 	uint32_t ipsec_ctl_len;
367 	uint_t ipsec_ctl_sa_type;
368 	void *ipsec_ctl_sa;
369 } ipsec_ctl_t;
370 
371 
372 /*
373  * All IPsec informational messages are placed into the ipsec_info_t
374  * union, so that allocation can be done once, and IPsec informational
375  * messages can be recycled.
376  */
377 typedef union ipsec_info_u {
378 	struct {
379 		uint32_t ipsec_allu_type;
380 		uint32_t ipsec_allu_len;	/* In bytes */
381 	} ipsec_allu;
382 	ipsec_in_t ipsec_in;
383 	ipsec_out_t ipsec_out;
384 	keysock_hello_ack_t keysock_hello_ack;
385 	keysock_in_t keysock_in;
386 	keysock_out_t keysock_out;
387 	keysock_out_err_t keysock_out_err;
388 	ipsec_ctl_t ipsec_ctl;
389 } ipsec_info_t;
390 #define	ipsec_info_type ipsec_allu.ipsec_allu_type
391 #define	ipsec_info_len ipsec_allu.ipsec_allu_len
392 
393 #ifdef	__cplusplus
394 }
395 #endif
396 
397 #endif	/* _INET_IPSEC_INFO_H */
398