xref: /titanic_51/usr/src/uts/common/inet/sadb.h (revision ae115bc77f6fcde83175c75b4206dc2e50747966)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_INET_SADB_H
27 #define	_INET_SADB_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #ifdef	__cplusplus
32 extern "C" {
33 #endif
34 
35 #include <inet/ipsec_info.h>
36 #include <sys/crypto/common.h>
37 #include <sys/crypto/api.h>
38 
39 #define	IPSA_MAX_ADDRLEN 4	/* Max address len. (in 32-bits) for an SA. */
40 
41 /*
42  * Return codes of IPsec processing functions.
43  */
44 typedef enum {
45 	IPSEC_STATUS_SUCCESS = 1,
46 	IPSEC_STATUS_FAILED = 2,
47 	IPSEC_STATUS_PENDING = 3
48 } ipsec_status_t;
49 
50 /*
51  * IP security association.  Synchronization assumes 32-bit loads, so
52  * the 64-bit quantities can't even be be read w/o locking it down!
53  */
54 
55 /* keying info */
56 typedef struct ipsa_key_s {
57 	void *sak_key;		/* Algorithm key. */
58 	uint_t sak_keylen;	/* Algorithm key length (in bytes). */
59 	uint_t sak_keybits;	/* Algorithm key length (in bits) */
60 	uint_t sak_algid;	/* Algorithm ID number. */
61 } ipsa_key_t;
62 
63 /* the security association */
64 typedef struct ipsa_s {
65 	struct ipsa_s *ipsa_next;	/* Next in hash bucket */
66 	struct ipsa_s **ipsa_ptpn;	/* Pointer to previous next pointer. */
67 	kmutex_t *ipsa_linklock;	/* Pointer to hash-chain lock. */
68 	void (*ipsa_freefunc)(struct ipsa_s *); /* freeassoc function */
69 	/*
70 	 * NOTE: I may need more pointers, depending on future SA
71 	 * requirements.
72 	 */
73 	ipsa_key_t ipsa_authkeydata;
74 #define	ipsa_authkey ipsa_authkeydata.sak_key
75 #define	ipsa_authkeylen ipsa_authkeydata.sak_keylen
76 #define	ipsa_authkeybits ipsa_authkeydata.sak_keybits
77 #define	ipsa_auth_alg ipsa_authkeydata.sak_algid
78 	ipsa_key_t ipsa_encrkeydata;
79 #define	ipsa_encrkey ipsa_encrkeydata.sak_key
80 #define	ipsa_encrkeylen ipsa_encrkeydata.sak_keylen
81 #define	ipsa_encrkeybits ipsa_encrkeydata.sak_keybits
82 #define	ipsa_encr_alg ipsa_encrkeydata.sak_algid
83 
84 	struct ipsid_s *ipsa_src_cid;	/* Source certificate identity */
85 	struct ipsid_s *ipsa_dst_cid;	/* Destination certificate identity */
86 	uint64_t *ipsa_integ;	/* Integrity bitmap */
87 	uint64_t *ipsa_sens;	/* Sensitivity bitmap */
88 	mblk_t	*ipsa_lpkt;	/* Packet received while larval (CAS me) */
89 
90 	/*
91 	 * PF_KEYv2 supports a replay window size of 255.  Hence there is a
92 	 * need a bit vector to support a replay window of 255.  256 is a nice
93 	 * round number, so I support that.
94 	 *
95 	 * Use an array of uint64_t for best performance on 64-bit
96 	 * processors.  (And hope that 32-bit compilers can handle things
97 	 * okay.)  The " >> 6 " is to get the appropriate number of 64-bit
98 	 * ints.
99 	 */
100 #define	SADB_MAX_REPLAY 256	/* Must be 0 mod 64. */
101 	uint64_t ipsa_replay_arr[SADB_MAX_REPLAY >> 6];
102 
103 	uint64_t ipsa_unique_id;	/* Non-zero for unique SAs */
104 	uint64_t ipsa_unique_mask;	/* mask value for unique_id */
105 
106 	/*
107 	 * Reference count semantics:
108 	 *
109 	 *	An SA has a reference count of 1 if something's pointing
110 	 *	to it.  This includes being in a hash table.  So if an
111 	 *	SA is in a hash table, it has a reference count of at least 1.
112 	 *
113 	 *	When a ptr. to an IPSA is assigned, you MUST REFHOLD after
114 	 *	said assignment.  When a ptr. to an IPSA is released
115 	 *	you MUST REFRELE.  When the refcount hits 0, REFRELE
116 	 *	will free the IPSA.
117 	 */
118 	kmutex_t ipsa_lock;	/* Locks non-linkage/refcnt fields. */
119 	/* Q:  Since I may be doing refcnts differently, will I need cv? */
120 	uint_t ipsa_refcnt;	/* Reference count. */
121 
122 	/*
123 	 * The following four time fields are the ones monitored by ah_ager()
124 	 * and esp_ager() respectively.  They are all absolute wall-clock
125 	 * times.  The times of creation (i.e. add time) and first use are
126 	 * pretty straightforward.  The soft and hard expire times are
127 	 * derived from the times of first use and creation, plus the minimum
128 	 * expiration times in the fields that follow this.
129 	 *
130 	 * For example, if I had a hard add time of 30 seconds, and a hard
131 	 * use time of 15, the ipsa_hardexpiretime would be time of add, plus
132 	 * 30 seconds.  If I USE the SA such that time of first use plus 15
133 	 * seconds would be earlier than the add time plus 30 seconds, then
134 	 * ipsa_hardexpiretime would become this earlier time.
135 	 */
136 	time_t ipsa_addtime;	/* Time I was added. */
137 	time_t ipsa_usetime;	/* Time of my first use. */
138 	time_t ipsa_softexpiretime;	/* Time of my first soft expire. */
139 	time_t ipsa_hardexpiretime;	/* Time of my first hard expire. */
140 
141 	/*
142 	 * The following fields are directly reflected in PF_KEYv2 LIFETIME
143 	 * extensions.  The time_ts are in number-of-seconds, and the bytes
144 	 * are in... bytes.
145 	 */
146 	time_t ipsa_softaddlt;	/* Seconds of soft lifetime after add. */
147 	time_t ipsa_softuselt;	/* Seconds of soft lifetime after first use. */
148 	time_t ipsa_hardaddlt;	/* Seconds of hard lifetime after add. */
149 	time_t ipsa_harduselt;	/* Seconds of hard lifetime after first use. */
150 	uint64_t ipsa_softbyteslt;	/* Bytes of soft lifetime. */
151 	uint64_t ipsa_hardbyteslt;	/* Bytes of hard lifetime. */
152 	uint64_t ipsa_bytes;	/* Bytes encrypted/authed by this SA. */
153 
154 	/*
155 	 * "Allocations" are a concept mentioned in PF_KEYv2.  We do not
156 	 * support them, except to record them per the PF_KEYv2 spec.
157 	 */
158 	uint_t ipsa_softalloc;	/* Allocations allowed (soft). */
159 	uint_t ipsa_hardalloc;	/* Allocations allowed (hard). */
160 	uint_t ipsa_alloc;	/* Allocations made. */
161 
162 	uint_t ipsa_integlen;	/* Length of the integrity bitmap (bytes). */
163 	uint_t ipsa_senslen;	/* Length of the sensitivity bitmap (bytes). */
164 
165 	uint_t ipsa_type;	/* Type of security association. (AH/etc.) */
166 	uint_t ipsa_dpd;	/* Domain for sensitivity bit vectors. */
167 	uint_t ipsa_senslevel;	/* Sensitivity level. */
168 	uint_t ipsa_integlevel;	/* Integrity level. */
169 	uint_t ipsa_state;	/* State of my association. */
170 	uint_t ipsa_replay_wsize; /* Size of replay window */
171 	uint32_t ipsa_flags;	/* Flags for security association. */
172 	uint32_t ipsa_spi;	/* Security parameters index. */
173 	uint32_t ipsa_replay;	/* Highest seen replay value for this SA. */
174 	uint32_t ipsa_kmp;	/* key management proto */
175 	uint32_t ipsa_kmc;	/* key management cookie */
176 
177 	boolean_t ipsa_haspeer;		/* Has peer in another table. */
178 
179 	/*
180 	 * Address storage.
181 	 * The source address can be INADDR_ANY, IN6ADDR_ANY, etc.
182 	 *
183 	 * Address families (per sys/socket.h) guide us.  We could have just
184 	 * used sockaddr_storage
185 	 */
186 	sa_family_t ipsa_addrfam;
187 	sa_family_t ipsa_innerfam;	/* Inner AF can be != src/dst AF. */
188 
189 	uint32_t ipsa_srcaddr[IPSA_MAX_ADDRLEN];
190 	uint32_t ipsa_dstaddr[IPSA_MAX_ADDRLEN];
191 	uint32_t ipsa_innersrc[IPSA_MAX_ADDRLEN];
192 	uint32_t ipsa_innerdst[IPSA_MAX_ADDRLEN];
193 
194 	uint8_t ipsa_innersrcpfx;
195 	uint8_t ipsa_innerdstpfx;
196 
197 	/* these can only be v4 */
198 	uint32_t ipsa_natt_addr_loc[IPSA_MAX_ADDRLEN];
199 	uint32_t ipsa_natt_addr_rem[IPSA_MAX_ADDRLEN];
200 
201 	uint16_t ipsa_inbound_cksum; /* cksum correction for inbound packets */
202 	uint16_t ipsa_remote_port; /* the other port that isn't 4500 */
203 
204 	timeout_id_t ipsa_natt_ka_timer;
205 	queue_t *ipsa_natt_q;
206 	/*
207 	 * icmp type and code. *_end are to specify ranges. if only
208 	 * a single value, * and *_end are the same value.
209 	 */
210 	uint8_t ipsa_icmp_type;
211 	uint8_t ipsa_icmp_type_end;
212 	uint8_t ipsa_icmp_code;
213 	uint8_t ipsa_icmp_code_end;
214 
215 	/*
216 	 * For the kernel crypto framework.
217 	 */
218 	crypto_key_t ipsa_kcfauthkey;		/* authentication key */
219 	crypto_key_t ipsa_kcfencrkey;		/* encryption key */
220 	crypto_ctx_template_t ipsa_authtmpl;	/* auth context template */
221 	crypto_ctx_template_t ipsa_encrtmpl;	/* encr context template */
222 	crypto_mechanism_t ipsa_amech;		/* auth mech type and ICV len */
223 	crypto_mechanism_t ipsa_emech;		/* encr mech type */
224 	size_t ipsa_mac_len;			/* auth MAC length */
225 	size_t ipsa_iv_len;			/* encr IV length */
226 
227 	/*
228 	 * Input and output processing functions called from IP.
229 	 */
230 	ipsec_status_t (*ipsa_output_func)(mblk_t *);
231 	ipsec_status_t (*ipsa_input_func)(mblk_t *, void *);
232 
233 	/* MLS boxen will probably need more fields in here. */
234 
235 } ipsa_t;
236 
237 /*
238  * ipsa_t address handling macros.  We want these to be inlined, and deal
239  * with 32-bit words to avoid bcmp/bcopy calls.
240  *
241  * Assume we only have AF_INET and AF_INET6 addresses for now.  Also assume
242  * that we have 32-bit alignment on everything.
243  */
244 #define	IPSA_IS_ADDR_UNSPEC(addr, fam) ((((uint32_t *)(addr))[0] == 0) && \
245 	(((fam) == AF_INET) || (((uint32_t *)(addr))[3] == 0 && \
246 	((uint32_t *)(addr))[2] == 0 && ((uint32_t *)(addr))[1] == 0)))
247 #define	IPSA_ARE_ADDR_EQUAL(addr1, addr2, fam) \
248 	((((uint32_t *)(addr1))[0] == ((uint32_t *)(addr2))[0]) && \
249 	(((fam) == AF_INET) || \
250 	(((uint32_t *)(addr1))[3] == ((uint32_t *)(addr2))[3] && \
251 	((uint32_t *)(addr1))[2] == ((uint32_t *)(addr2))[2] && \
252 	((uint32_t *)(addr1))[1] == ((uint32_t *)(addr2))[1])))
253 #define	IPSA_COPY_ADDR(dstaddr, srcaddr, fam) { \
254 	((uint32_t *)(dstaddr))[0] = ((uint32_t *)(srcaddr))[0]; \
255 	if ((fam) == AF_INET6) {\
256 		((uint32_t *)(dstaddr))[1] = ((uint32_t *)(srcaddr))[1]; \
257 		((uint32_t *)(dstaddr))[2] = ((uint32_t *)(srcaddr))[2]; \
258 		((uint32_t *)(dstaddr))[3] = ((uint32_t *)(srcaddr))[3]; } }
259 
260 /*
261  * ipsa_t reference hold/release macros.
262  *
263  * If you have a pointer, you REFHOLD.  If you are releasing a pointer, you
264  * REFRELE.  An ipsa_t that is newly inserted into the table should have
265  * a reference count of 1 (for the table's pointer), plus 1 more for every
266  * pointer that is referencing the ipsa_t.
267  */
268 
269 #define	IPSA_REFHOLD(ipsa) {			\
270 	atomic_add_32(&(ipsa)->ipsa_refcnt, 1);	\
271 	ASSERT((ipsa)->ipsa_refcnt != 0);	\
272 }
273 
274 /*
275  * Decrement the reference count on the SA.
276  * In architectures e.g sun4u, where atomic_add_32_nv is just
277  * a cas, we need to maintain the right memory barrier semantics
278  * as that of mutex_exit i.e all the loads and stores should complete
279  * before the cas is executed. membar_exit() does that here.
280  */
281 
282 #define	IPSA_REFRELE(ipsa) {					\
283 	ASSERT((ipsa)->ipsa_refcnt != 0);			\
284 	membar_exit();						\
285 	if (atomic_add_32_nv(&(ipsa)->ipsa_refcnt, -1) == 0)	\
286 		((ipsa)->ipsa_freefunc)(ipsa);			\
287 }
288 
289 /*
290  * Security association hash macros and definitions.  For now, assume the
291  * IPsec model, and hash outbounds on destination address, and inbounds on
292  * SPI.
293  */
294 
295 #define	IPSEC_DEFAULT_HASH_SIZE 256
296 
297 #define	INBOUND_HASH(sadb, spi) ((spi) % ((sadb)->sdb_hashsize))
298 #define	OUTBOUND_HASH_V4(sadb, v4addr) ((v4addr) % ((sadb)->sdb_hashsize))
299 #define	OUTBOUND_HASH_V6(sadb, v6addr) OUTBOUND_HASH_V4((sadb), \
300 	(*(uint32_t *)&(v6addr)) ^ (*(((uint32_t *)&(v6addr)) + 1)) ^ \
301 	(*(((uint32_t *)&(v6addr)) + 2)) ^ (*(((uint32_t *)&(v6addr)) + 3)))
302 
303 /*
304  * Syntactic sugar to find the appropriate hash bucket directly.
305  */
306 
307 #define	INBOUND_BUCKET(sadb, spi) &(((sadb)->sdb_if)[INBOUND_HASH(sadb, spi)])
308 #define	OUTBOUND_BUCKET_V4(sadb, v4addr) \
309 	&(((sadb)->sdb_of)[OUTBOUND_HASH_V4(sadb, v4addr)])
310 #define	OUTBOUND_BUCKET_V6(sadb, v6addr) \
311 	&(((sadb)->sdb_of)[OUTBOUND_HASH_V6(sadb, v6addr)])
312 
313 #define	IPSA_F_PFS	SADB_SAFLAGS_PFS	/* PFS in use for this SA? */
314 #define	IPSA_F_NOREPFLD	SADB_SAFLAGS_NOREPLAY	/* No replay field, for */
315 						/* backward compat. */
316 #define	IPSA_F_USED	SADB_X_SAFLAGS_USED	/* SA has been used. */
317 #define	IPSA_F_UNIQUE	SADB_X_SAFLAGS_UNIQUE	/* SA is unique */
318 #define	IPSA_F_AALG1	SADB_X_SAFLAGS_AALG1	/* Auth alg flag 1 */
319 #define	IPSA_F_AALG2	SADB_X_SAFLAGS_AALG2	/* Auth alg flag 2 */
320 #define	IPSA_F_EALG1	SADB_X_SAFLAGS_EALG1	/* Encrypt alg flag 1 */
321 #define	IPSA_F_EALG2	SADB_X_SAFLAGS_EALG2	/* Encrypt alg flag 2 */
322 
323 #define	IPSA_F_HW	0x200000		/* hwaccel capable SA */
324 #define	IPSA_F_NATT_LOC	SADB_X_SAFLAGS_NATT_LOC
325 #define	IPSA_F_NATT_REM	SADB_X_SAFLAGS_NATT_REM
326 #define	IPSA_F_NATT	(SADB_X_SAFLAGS_NATT_LOC | SADB_X_SAFLAGS_NATT_REM)
327 #define	IPSA_F_CINVALID	0x40000		/* SA shouldn't be cached */
328 #define	IPSA_F_TUNNEL	SADB_X_SAFLAGS_TUNNEL
329 
330 /* SA states are important for handling UPDATE PF_KEY messages. */
331 #define	IPSA_STATE_LARVAL	SADB_SASTATE_LARVAL
332 #define	IPSA_STATE_MATURE	SADB_SASTATE_MATURE
333 #define	IPSA_STATE_DYING	SADB_SASTATE_DYING
334 #define	IPSA_STATE_DEAD		SADB_SASTATE_DEAD
335 
336 /*
337  * NOTE:  If the document authors do things right in defining algorithms, we'll
338  *	  probably have flags for what all is here w.r.t. replay, ESP w/HMAC,
339  *	  etc.
340  */
341 
342 #define	IPSA_T_ACQUIRE	SEC_TYPE_NONE	/* If this typed returned, sa needed */
343 #define	IPSA_T_AH	SEC_TYPE_AH	/* IPsec AH association */
344 #define	IPSA_T_ESP	SEC_TYPE_ESP	/* IPsec ESP association */
345 
346 #define	IPSA_AALG_NONE	SADB_AALG_NONE		/* No auth. algorithm */
347 #define	IPSA_AALG_MD5H	SADB_AALG_MD5HMAC	/* MD5-HMAC algorithm */
348 #define	IPSA_AALG_SHA1H	SADB_AALG_SHA1HMAC	/* SHA1-HMAC algorithm */
349 
350 #define	IPSA_EALG_NONE		SADB_EALG_NONE	/* No encryption algorithm */
351 #define	IPSA_EALG_DES_CBC	SADB_EALG_DESCBC
352 #define	IPSA_EALG_3DES		SADB_EALG_3DESCBC
353 
354 /*
355  * Protect each ipsa_t bucket (and linkage) with a lock.
356  */
357 
358 typedef struct isaf_s {
359 	ipsa_t *isaf_ipsa;
360 	kmutex_t isaf_lock;
361 	uint64_t isaf_gen;
362 } isaf_t;
363 
364 /*
365  * ACQUIRE record.  If AH/ESP/whatever cannot find an association for outbound
366  * traffic, it sends up an SADB_ACQUIRE message and create an ACQUIRE record.
367  */
368 
369 #define	IPSACQ_MAXPACKETS 4	/* Number of packets that can be queued up */
370 				/* waiting for an ACQUIRE to finish. */
371 
372 typedef struct ipsacq_s {
373 	struct ipsacq_s *ipsacq_next;
374 	struct ipsacq_s **ipsacq_ptpn;
375 	kmutex_t *ipsacq_linklock;
376 	struct ipsec_policy_s  *ipsacq_policy;
377 	struct ipsec_action_s  *ipsacq_act;
378 
379 	sa_family_t ipsacq_addrfam;	/* Address family. */
380 	sa_family_t ipsacq_inneraddrfam; /* Inner-packet address family. */
381 	int ipsacq_numpackets;		/* How many packets queued up so far. */
382 	uint32_t ipsacq_seq;		/* PF_KEY sequence number. */
383 	uint64_t ipsacq_unique_id;	/* Unique ID for SAs that need it. */
384 
385 	kmutex_t ipsacq_lock;	/* Protects non-linkage fields. */
386 	time_t ipsacq_expire;	/* Wall-clock time when this record expires. */
387 	mblk_t *ipsacq_mp;	/* List of datagrams waiting for an SA. */
388 
389 	/* These two point inside the last mblk inserted. */
390 	uint32_t *ipsacq_srcaddr;
391 	uint32_t *ipsacq_dstaddr;
392 
393 	/* Cache these instead of point so we can mask off accordingly */
394 	uint32_t ipsacq_innersrc[IPSA_MAX_ADDRLEN];
395 	uint32_t ipsacq_innerdst[IPSA_MAX_ADDRLEN];
396 
397 	/* These may change per-acquire. */
398 	uint16_t ipsacq_srcport;
399 	uint16_t ipsacq_dstport;
400 	uint8_t ipsacq_proto;
401 	uint8_t ipsacq_inner_proto;
402 	uint8_t ipsacq_innersrcpfx;
403 	uint8_t ipsacq_innerdstpfx;
404 
405 	/* icmp type and code of triggering packet (if applicable) */
406 	uint8_t	ipsacq_icmp_type;
407 	uint8_t ipsacq_icmp_code;
408 } ipsacq_t;
409 
410 /*
411  * Kernel-generated sequence numbers will be no less than 0x80000000 to
412  * forestall any cretinous problems with manual keying accidentally updating
413  * an ACQUIRE entry.
414  */
415 #define	IACQF_LOWEST_SEQ 0x80000000
416 
417 #define	SADB_AGE_INTERVAL_DEFAULT 1000
418 
419 /*
420  * ACQUIRE fanout.  Protect each linkage with a lock.
421  */
422 
423 typedef struct iacqf_s {
424 	ipsacq_t *iacqf_ipsacq;
425 	kmutex_t iacqf_lock;
426 } iacqf_t;
427 
428 /*
429  * A (network protocol, ipsec protocol) specific SADB.
430  * (i.e., one each for {ah, esp} and {v4, v6}.
431  *
432  * Keep outbound assocs about the same as ire_cache entries for now.
433  * One danger point, multiple SAs for a single dest will clog a bucket.
434  * For the future, consider two-level hashing (2nd hash on IPC?), then probe.
435  */
436 
437 typedef struct sadb_s
438 {
439 	isaf_t	*sdb_of;
440 	isaf_t	*sdb_if;
441 	iacqf_t	*sdb_acq;
442 	int	sdb_hashsize;
443 } sadb_t;
444 
445 /*
446  * A pair of SADB's (one for v4, one for v6), and related state (including
447  * acquire callbacks).
448  */
449 
450 typedef struct sadbp_s
451 {
452 	uint32_t	s_satype;
453 	queue_t		*s_ip_q;
454 	uint32_t	*s_acquire_timeout;
455 	void 		(*s_acqfn)(ipsacq_t *, mblk_t *);
456 	sadb_t		s_v4;
457 	sadb_t		s_v6;
458 } sadbp_t;
459 
460 /*
461  * Global IPsec security association databases (and all that go with them).
462  */
463 extern sadbp_t ah_sadb, esp_sadb;
464 
465 /* Pointer to an all-zeroes IPv6 address. */
466 #define	ALL_ZEROES_PTR	((uint32_t *)&ipv6_all_zeros)
467 
468 /*
469  * Form unique id from ipsec_out_t
470  */
471 
472 #define	SA_FORM_UNIQUE_ID(io)				\
473 	SA_UNIQUE_ID((io)->ipsec_out_src_port, (io)->ipsec_out_dst_port, \
474 		((io)->ipsec_out_tunnel ? ((io)->ipsec_out_inaf == AF_INET6 ? \
475 		    IPPROTO_IPV6 : IPPROTO_ENCAP) : (io)->ipsec_out_proto), \
476 		((io)->ipsec_out_tunnel ? (io)->ipsec_out_proto : 0))
477 
478 /*
479  * This macro is used to generate unique ids (along with the addresses, both
480  * inner and outer) for outbound datagrams that require unique SAs.
481  *
482  * N.B. casts and unsigned shift amounts discourage unwarranted
483  * sign extension of dstport, proto, and iproto.
484  *
485  * Unique ID is 64-bits allocated as follows (pardon my big-endian bias):
486  *
487  *   6               4      43      33              11
488  *   3               7      09      21              65              0
489  *   +---------------*-------+-------+--------------+---------------+
490  *   |  MUST-BE-ZERO |<iprot>|<proto>| <src port>   |  <dest port>  |
491  *   +---------------*-------+-------+--------------+---------------+
492  *
493  * If there are inner addresses (tunnel mode) the ports come from the
494  * inner addresses.  If there are no inner addresses, the ports come from
495  * the outer addresses (transport mode).  Tunnel mode MUST have <proto>
496  * set to either IPPROTO_ENCAP or IPPPROTO_IPV6.
497  */
498 #define	SA_UNIQUE_ID(srcport, dstport, proto, iproto) 	\
499 	((srcport) | ((uint64_t)(dstport) << 16U) | \
500 	((uint64_t)(proto) << 32U) | ((uint64_t)(iproto) << 40U))
501 
502 /*
503  * SA_UNIQUE_MASK generates a mask value to use when comparing the unique value
504  * from a packet to an SA.
505  */
506 
507 #define	SA_UNIQUE_MASK(srcport, dstport, proto, iproto) 	\
508 	SA_UNIQUE_ID((srcport != 0) ? 0xffff : 0,		\
509 		    (dstport != 0) ? 0xffff : 0,		\
510 		    (proto != 0) ? 0xff : 0,			\
511 		    (iproto != 0) ? 0xff : 0)
512 
513 /*
514  * Decompose unique id back into its original fields.
515  */
516 #define	SA_IPROTO(ipsa) ((ipsa)->ipsa_unique_id>>40)&0xff
517 #define	SA_PROTO(ipsa) ((ipsa)->ipsa_unique_id>>32)&0xff
518 #define	SA_SRCPORT(ipsa) ((ipsa)->ipsa_unique_id & 0xffff)
519 #define	SA_DSTPORT(ipsa) (((ipsa)->ipsa_unique_id >> 16) & 0xffff)
520 
521 /*
522  * All functions that return an ipsa_t will return it with IPSA_REFHOLD()
523  * already called.
524  */
525 
526 /* SA retrieval (inbound and outbound) */
527 ipsa_t *ipsec_getassocbyspi(isaf_t *, uint32_t, uint32_t *, uint32_t *,
528     sa_family_t);
529 ipsa_t *ipsec_getassocbyconn(isaf_t *, ipsec_out_t *, uint32_t *, uint32_t *,
530     sa_family_t, uint8_t);
531 
532 /* SA insertion. */
533 int sadb_insertassoc(ipsa_t *, isaf_t *);
534 
535 /* SA table construction and destruction. */
536 void sadbp_init(const char *name, sadbp_t *, int, int);
537 void sadbp_flush(sadbp_t *);
538 void sadbp_destroy(sadbp_t *);
539 
540 /* SA insertion and deletion. */
541 int sadb_insertassoc(ipsa_t *, isaf_t *);
542 void sadb_unlinkassoc(ipsa_t *);
543 
544 /* Support routines to interface a keysock consumer to PF_KEY. */
545 mblk_t *sadb_keysock_out(minor_t);
546 int sadb_hardsoftchk(sadb_lifetime_t *, sadb_lifetime_t *);
547 void sadb_pfkey_echo(queue_t *, mblk_t *, sadb_msg_t *, struct keysock_in_s *,
548     ipsa_t *);
549 void sadb_pfkey_error(queue_t *, mblk_t *, int, int, uint_t);
550 void sadb_keysock_hello(queue_t **, queue_t *, mblk_t *, void (*)(void *),
551     timeout_id_t *, int);
552 int sadb_addrcheck(queue_t *, mblk_t *, sadb_ext_t *, uint_t);
553 boolean_t sadb_addrfix(keysock_in_t *, queue_t *, mblk_t *);
554 int sadb_addrset(ire_t *);
555 int sadb_delget_sa(mblk_t *, keysock_in_t *, sadbp_t *, int *, queue_t *,
556     boolean_t);
557 #define	sadb_get_sa(m, k, s, i, q)	sadb_delget_sa(m, k, s, i, q, B_FALSE)
558 #define	sadb_del_sa(m, k, s, i, q)	sadb_delget_sa(m, k, s, i, q, B_TRUE)
559 
560 int sadb_purge_sa(mblk_t *, keysock_in_t *, sadb_t *, queue_t *, queue_t *);
561 int sadb_common_add(queue_t *, queue_t *, mblk_t *, sadb_msg_t *,
562     keysock_in_t *, isaf_t *, isaf_t *, ipsa_t *, boolean_t, boolean_t, int *);
563 void sadb_set_usetime(ipsa_t *);
564 boolean_t sadb_age_bytes(queue_t *, ipsa_t *, uint64_t, boolean_t);
565 int sadb_update_sa(mblk_t *, keysock_in_t *, sadb_t *,
566     int *, queue_t *, int (*)(mblk_t *, keysock_in_t *, int *));
567 void sadb_acquire(mblk_t *, ipsec_out_t *, boolean_t, boolean_t);
568 
569 void sadb_destroy_acquire(ipsacq_t *);
570 mblk_t *sadb_setup_acquire(ipsacq_t *, uint8_t);
571 ipsa_t *sadb_getspi(keysock_in_t *, uint32_t, int *);
572 void sadb_in_acquire(sadb_msg_t *, sadbp_t *, queue_t *);
573 boolean_t sadb_replay_check(ipsa_t *, uint32_t);
574 boolean_t sadb_replay_peek(ipsa_t *, uint32_t);
575 int sadb_dump(queue_t *, mblk_t *, minor_t, sadb_t *);
576 void sadb_replay_delete(ipsa_t *);
577 void sadb_ager(sadb_t *, queue_t *, queue_t *, int);
578 
579 timeout_id_t sadb_retimeout(hrtime_t, queue_t *, void (*)(void *),
580     uint_t *, uint_t, short);
581 void sadb_sa_refrele(void *target);
582 void sadb_set_lpkt(ipsa_t *, mblk_t *);
583 mblk_t *sadb_clear_lpkt(ipsa_t *);
584 
585 /*
586  * Hw accel-related calls (downloading sadb to driver)
587  */
588 void sadb_ill_download(ill_t *, uint_t);
589 mblk_t *sadb_fmt_sa_req(uint_t, uint_t, ipsa_t *, boolean_t);
590 /*
591  * Sub-set of the IPsec hardware acceleration capabilities functions
592  * implemented by ip_if.c
593  */
594 extern	boolean_t ipsec_capab_match(ill_t *, uint_t, boolean_t, ipsa_t *);
595 extern	void	ill_ipsec_capab_send_all(uint_t, mblk_t *, ipsa_t *);
596 
597 
598 /*
599  * One IPsec -> IP linking routine, and two IPsec rate-limiting routines.
600  */
601 extern boolean_t sadb_t_bind_req(queue_t *, int);
602 /*PRINTFLIKE5*/
603 extern void ipsec_rl_strlog(short, short, char, ushort_t, char *, ...)
604     __KPRINTFLIKE(5);
605 extern void ipsec_assocfailure(short, short, char, ushort_t, char *, uint32_t,
606     void *, int);
607 
608 /*
609  * Algorithm types.
610  */
611 
612 #define	IPSEC_NALGTYPES 	2
613 
614 typedef enum ipsec_algtype {
615 	IPSEC_ALG_AUTH = 0,
616 	IPSEC_ALG_ENCR = 1
617 } ipsec_algtype_t;
618 
619 /*
620  * Definitions as per IPsec/ISAKMP DOI.
621  */
622 
623 #define	IPSEC_MAX_ALGS		256
624 #define	PROTO_IPSEC_AH		2
625 #define	PROTO_IPSEC_ESP		3
626 
627 /*
628  * Common algorithm info.
629  */
630 typedef struct ipsec_alginfo
631 {
632 	uint8_t		alg_id;
633 	uint8_t		alg_flags;
634 	uint16_t	*alg_key_sizes;
635 	uint16_t	*alg_block_sizes;
636 	uint16_t	alg_nkey_sizes;
637 	uint16_t	alg_nblock_sizes;
638 	uint16_t	alg_minbits;
639 	uint16_t	alg_maxbits;
640 	uint16_t	alg_datalen;
641 	/*
642 	 * increment: number of bits from keysize to keysize
643 	 * default: # of increments from min to default key len
644 	 */
645 	uint16_t	alg_increment;
646 	uint16_t	alg_default;
647 	uint16_t	alg_default_bits;
648 	/*
649 	 * Min, max, and default key sizes effectively supported
650 	 * by the encryption framework.
651 	 */
652 	uint16_t	alg_ef_minbits;
653 	uint16_t	alg_ef_maxbits;
654 	uint16_t	alg_ef_default;
655 	uint16_t	alg_ef_default_bits;
656 
657 	crypto_mech_type_t alg_mech_type;	/* KCF mechanism type */
658 	crypto_mech_name_t alg_mech_name;	/* KCF mechanism name */
659 } ipsec_alginfo_t;
660 
661 #define	alg_datalen alg_block_sizes[0]
662 
663 #define	ALG_FLAG_VALID	0x01
664 #define	ALG_VALID(_alg)	((_alg)->alg_flags & ALG_FLAG_VALID)
665 
666 /*
667  * Software crypto execution mode.
668  */
669 typedef enum {
670 	IPSEC_ALGS_EXEC_SYNC = 0,
671 	IPSEC_ALGS_EXEC_ASYNC = 1
672 } ipsec_algs_exec_mode_t;
673 
674 extern uint8_t ipsec_nalgs[IPSEC_NALGTYPES];
675 extern ipsec_alginfo_t *ipsec_alglists[IPSEC_NALGTYPES][IPSEC_MAX_ALGS];
676 extern uint8_t ipsec_sortlist[IPSEC_NALGTYPES][IPSEC_MAX_ALGS];
677 extern ipsec_algs_exec_mode_t ipsec_algs_exec_mode[IPSEC_NALGTYPES];
678 
679 extern kmutex_t alg_lock;
680 
681 extern void ipsec_alg_reg(ipsec_algtype_t, ipsec_alginfo_t *);
682 extern void ipsec_alg_unreg(ipsec_algtype_t, uint8_t);
683 extern void ipsec_alg_fix_min_max(ipsec_alginfo_t *, ipsec_algtype_t);
684 extern void ipsec_alg_free(ipsec_alginfo_t *);
685 extern void ipsec_register_prov_update(void);
686 extern void sadb_alg_update(ipsec_algtype_t, uint8_t, boolean_t);
687 
688 /*
689  * Context templates management.
690  */
691 
692 #define	IPSEC_CTX_TMPL_ALLOC ((crypto_ctx_template_t)-1)
693 #define	IPSEC_CTX_TMPL(_sa, _which, _type, _tmpl) {			\
694 	if ((_tmpl = (_sa)->_which) == IPSEC_CTX_TMPL_ALLOC) {		\
695 		mutex_enter(&assoc->ipsa_lock);				\
696 		if ((_sa)->_which == IPSEC_CTX_TMPL_ALLOC) {		\
697 			mutex_enter(&alg_lock);				\
698 			(void) ipsec_create_ctx_tmpl(_sa, _type);	\
699 			mutex_exit(&alg_lock);				\
700 		}							\
701 		mutex_exit(&assoc->ipsa_lock);				\
702 		if ((_tmpl = (_sa)->_which) == IPSEC_CTX_TMPL_ALLOC)	\
703 			_tmpl = NULL;					\
704 	}								\
705 }
706 
707 extern int ipsec_create_ctx_tmpl(ipsa_t *, ipsec_algtype_t);
708 extern void ipsec_destroy_ctx_tmpl(ipsa_t *, ipsec_algtype_t);
709 
710 /* key checking */
711 extern int ipsec_check_key(crypto_mech_type_t, sadb_key_t *, boolean_t, int *);
712 
713 /* natt cleanup */
714 extern void sadb_clear_timeouts(queue_t *);
715 
716 typedef struct {
717 	kstat_named_t esp_stat_in_requests;
718 	kstat_named_t esp_stat_in_discards;
719 	kstat_named_t esp_stat_lookup_failure;
720 	kstat_named_t ah_stat_in_requests;
721 	kstat_named_t ah_stat_in_discards;
722 	kstat_named_t ah_stat_lookup_failure;
723 	kstat_named_t sadb_acquire_maxpackets;
724 	kstat_named_t sadb_acquire_qhiwater;
725 } ipsec_kstats_t;
726 
727 extern ipsec_kstats_t *ipsec_kstats;
728 extern void ipsec_kstat_init(void);
729 extern void ipsec_kstat_destroy(void);
730 
731 #define	IP_ESP_BUMP_STAT(x) (ipsec_kstats->esp_stat_ ## x).value.ui64++
732 #define	IP_AH_BUMP_STAT(x) (ipsec_kstats->ah_stat_ ## x).value.ui64++
733 #define	IP_ACQUIRE_STAT(val, new) \
734 if (((uint64_t)(new)) > (ipsec_kstats->sadb_acquire_ ## val).value.ui64) \
735 	(ipsec_kstats->sadb_acquire_ ## val).value.ui64 = ((uint64_t)(new))
736 
737 #ifdef	__cplusplus
738 }
739 #endif
740 
741 #endif /* _INET_SADB_H */
742