2 * Copyright (c) 2008-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $FreeBSD: src/sys/netkey/key.c,v 1.16.2.13 2002/07/24 18:17:40 ume Exp $ */
30 /* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * This code is referd to RFC 2367
65 #include <machine/endian.h>
66 #include <sys/types.h>
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
71 #include <sys/domain.h>
72 #include <sys/protosw.h>
73 #include <sys/malloc.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/sysctl.h>
77 #include <sys/errno.h>
79 #include <sys/queue.h>
80 #include <sys/syslog.h>
81 #include <sys/mcache.h>
83 #include <kern/locks.h>
86 #include <net/route.h>
87 #include <net/raw_cb.h>
89 #include <netinet/in.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/ip.h>
92 #include <netinet/in_var.h>
95 #include <netinet/ip6.h>
96 #include <netinet6/in6_var.h>
97 #include <netinet6/ip6_var.h>
100 #include <net/pfkeyv2.h>
101 #include <netkey/keydb.h>
102 #include <netkey/key.h>
103 #include <netkey/keysock.h>
104 #include <netkey/key_debug.h>
106 #include <libkern/crypto/rand.h>
108 #include <netinet6/ipsec.h>
110 #include <netinet6/ipsec6.h>
112 #include <netinet6/ah.h>
114 #include <netinet6/ah6.h>
117 #include <netinet6/esp.h>
119 #include <netinet6/esp6.h>
125 #include <sys/random.h>
127 #include <net/net_osdep.h>
129 #define FULLMASK 0xff
131 lck_grp_t
*sadb_mutex_grp
;
132 lck_grp_attr_t
*sadb_mutex_grp_attr
;
133 lck_attr_t
*sadb_mutex_attr
;
134 decl_lck_mtx_data(, sadb_mutex_data
);
135 lck_mtx_t
*sadb_mutex
= &sadb_mutex_data
;
137 lck_grp_t
*pfkey_stat_mutex_grp
;
138 lck_grp_attr_t
*pfkey_stat_mutex_grp_attr
;
139 lck_attr_t
*pfkey_stat_mutex_attr
;
140 decl_lck_mtx_data(, pfkey_stat_mutex_data
);
141 lck_mtx_t
*pfkey_stat_mutex
= &pfkey_stat_mutex_data
;
144 * Note on SA reference counting:
145 * - SAs that are not in DEAD state will have (total external reference + 1)
146 * following value in reference count field. they cannot be freed and are
147 * referenced from SA header.
148 * - SAs that are in DEAD state will have (total external reference)
149 * in reference count field. they are ready to be freed. reference from
150 * SA header will be removed in key_delsav(), when the reference count
151 * field hits 0 (= no external reference other than from SA header.
154 u_int32_t key_debug_level
= 0; //### our sysctl is not dynamic
155 static int key_timehandler_running
= 0;
156 static u_int key_spi_trycnt
= 1000;
157 static u_int32_t key_spi_minval
= 0x100;
158 static u_int32_t key_spi_maxval
= 0x0fffffff; /* XXX */
159 static u_int32_t policy_id
= 0;
160 static u_int key_int_random
= 60; /*interval to initialize randseed,1(m)*/
161 static u_int key_larval_lifetime
= 30; /* interval to expire acquiring, 30(s)*/
162 static int key_blockacq_count
= 10; /* counter for blocking SADB_ACQUIRE.*/
163 static int key_blockacq_lifetime
= 20; /* lifetime for blocking SADB_ACQUIRE.*/
164 static int key_preferred_oldsa
= 0; /* preferred old sa rather than new sa.*/
165 __private_extern__
int natt_keepalive_interval
= 20; /* interval between natt keepalives.*/
166 __private_extern__
int ipsec_policy_count
= 0;
167 static int ipsec_sav_count
= 0;
169 static u_int32_t acq_seq
= 0;
170 static int key_tick_init_random
= 0;
171 static u_int64_t up_time
= 0;
172 __private_extern__ u_int64_t natt_now
= 0;
174 static LIST_HEAD(_sptree
, secpolicy
) sptree
[IPSEC_DIR_MAX
]; /* SPD */
175 static LIST_HEAD(_sahtree
, secashead
) sahtree
; /* SAD */
176 static LIST_HEAD(_regtree
, secreg
) regtree
[SADB_SATYPE_MAX
+ 1];
179 #define SPIHASHSIZE 128
180 #define SPIHASH(x) (((x) ^ ((x) >> 16)) % SPIHASHSIZE)
181 static LIST_HEAD(_spihash
, secasvar
) spihash
[SPIHASHSIZE
];
183 #ifndef IPSEC_NONBLOCK_ACQUIRE
184 static LIST_HEAD(_acqtree
, secacq
) acqtree
; /* acquiring list */
186 static LIST_HEAD(_spacqtree
, secspacq
) spacqtree
; /* SP acquiring list */
188 struct key_cb key_cb
;
190 /* search order for SAs */
191 static const u_int saorder_state_valid_prefer_old
[] = {
192 SADB_SASTATE_DYING
, SADB_SASTATE_MATURE
,
194 static const u_int saorder_state_valid_prefer_new
[] = {
195 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
197 static const u_int saorder_state_alive
[] = {
199 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
, SADB_SASTATE_LARVAL
201 static const u_int saorder_state_any
[] = {
202 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
203 SADB_SASTATE_LARVAL
, SADB_SASTATE_DEAD
206 static const int minsize
[] = {
207 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
208 sizeof(struct sadb_sa
), /* SADB_EXT_SA */
209 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
210 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
211 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
212 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_SRC */
213 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_DST */
214 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_PROXY */
215 sizeof(struct sadb_key
), /* SADB_EXT_KEY_AUTH */
216 sizeof(struct sadb_key
), /* SADB_EXT_KEY_ENCRYPT */
217 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_SRC */
218 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_DST */
219 sizeof(struct sadb_sens
), /* SADB_EXT_SENSITIVITY */
220 sizeof(struct sadb_prop
), /* SADB_EXT_PROPOSAL */
221 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_AUTH */
222 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_ENCRYPT */
223 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
224 0, /* SADB_X_EXT_KMPRIVATE */
225 sizeof(struct sadb_x_policy
), /* SADB_X_EXT_POLICY */
226 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
227 sizeof(struct sadb_session_id
), /* SADB_EXT_SESSION_ID */
228 sizeof(struct sadb_sastat
), /* SADB_EXT_SASTAT */
229 sizeof(struct sadb_x_ipsecif
), /* SADB_X_EXT_IPSECIF */
230 sizeof(struct sadb_address
), /* SADB_X_EXT_ADDR_RANGE_SRC_START */
231 sizeof(struct sadb_address
), /* SADB_X_EXT_ADDR_RANGE_SRC_END */
232 sizeof(struct sadb_address
), /* SADB_X_EXT_ADDR_RANGE_DST_START */
233 sizeof(struct sadb_address
), /* SADB_X_EXT_ADDR_RANGE_DST_END */
234 sizeof(struct sadb_address
), /* SADB_EXT_MIGRATE_ADDRESS_SRC */
235 sizeof(struct sadb_address
), /* SADB_EXT_MIGRATE_ADDRESS_DST */
236 sizeof(struct sadb_x_ipsecif
), /* SADB_X_EXT_MIGRATE_IPSECIF */
238 static const int maxsize
[] = {
239 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
240 sizeof(struct sadb_sa_2
), /* SADB_EXT_SA */
241 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
242 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
243 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
244 0, /* SADB_EXT_ADDRESS_SRC */
245 0, /* SADB_EXT_ADDRESS_DST */
246 0, /* SADB_EXT_ADDRESS_PROXY */
247 0, /* SADB_EXT_KEY_AUTH */
248 0, /* SADB_EXT_KEY_ENCRYPT */
249 0, /* SADB_EXT_IDENTITY_SRC */
250 0, /* SADB_EXT_IDENTITY_DST */
251 0, /* SADB_EXT_SENSITIVITY */
252 0, /* SADB_EXT_PROPOSAL */
253 0, /* SADB_EXT_SUPPORTED_AUTH */
254 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
255 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
256 0, /* SADB_X_EXT_KMPRIVATE */
257 0, /* SADB_X_EXT_POLICY */
258 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
259 0, /* SADB_EXT_SESSION_ID */
260 0, /* SADB_EXT_SASTAT */
261 sizeof(struct sadb_x_ipsecif
), /* SADB_X_EXT_IPSECIF */
262 0, /* SADB_X_EXT_ADDR_RANGE_SRC_START */
263 0, /* SADB_X_EXT_ADDR_RANGE_SRC_END */
264 0, /* SADB_X_EXT_ADDR_RANGE_DST_START */
265 0, /* SADB_X_EXT_ADDR_RANGE_DST_END */
266 0, /* SADB_EXT_MIGRATE_ADDRESS_SRC */
267 0, /* SADB_EXT_MIGRATE_ADDRESS_DST */
268 sizeof(struct sadb_x_ipsecif
), /* SADB_X_EXT_MIGRATE_IPSECIF */
271 static int ipsec_esp_keymin
= 256;
272 static int ipsec_esp_auth
= 0;
273 static int ipsec_ah_keymin
= 128;
275 SYSCTL_DECL(_net_key
);
276 /* Thread safe: no accumulated state */
277 SYSCTL_INT(_net_key
, KEYCTL_DEBUG_LEVEL
, debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
278 &key_debug_level
, 0, "");
281 /* max count of trial for the decision of spi value */
282 SYSCTL_INT(_net_key
, KEYCTL_SPI_TRY
, spi_trycnt
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
283 &key_spi_trycnt
, 0, "");
285 /* minimum spi value to allocate automatically. */
286 SYSCTL_INT(_net_key
, KEYCTL_SPI_MIN_VALUE
, spi_minval
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
287 &key_spi_minval
, 0, "");
289 /* maximun spi value to allocate automatically. */
290 SYSCTL_INT(_net_key
, KEYCTL_SPI_MAX_VALUE
, spi_maxval
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
291 &key_spi_maxval
, 0, "");
293 /* interval to initialize randseed */
294 SYSCTL_INT(_net_key
, KEYCTL_RANDOM_INT
, int_random
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
295 &key_int_random
, 0, "");
297 /* lifetime for larval SA; thread safe due to > compare */
298 SYSCTL_INT(_net_key
, KEYCTL_LARVAL_LIFETIME
, larval_lifetime
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
299 &key_larval_lifetime
, 0, "");
301 /* counter for blocking to send SADB_ACQUIRE to IKEd */
302 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_COUNT
, blockacq_count
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
303 &key_blockacq_count
, 0, "");
305 /* lifetime for blocking to send SADB_ACQUIRE to IKEd: Thread safe, > compare */
306 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_LIFETIME
, blockacq_lifetime
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
307 &key_blockacq_lifetime
, 0, "");
310 SYSCTL_INT(_net_key
, KEYCTL_ESP_AUTH
, esp_auth
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
311 &ipsec_esp_auth
, 0, "");
313 /* minimum ESP key length */
314 SYSCTL_INT(_net_key
, KEYCTL_ESP_KEYMIN
, esp_keymin
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
315 &ipsec_esp_keymin
, 0, "");
317 /* minimum AH key length */
318 SYSCTL_INT(_net_key
, KEYCTL_AH_KEYMIN
, ah_keymin
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
319 &ipsec_ah_keymin
, 0, "");
321 /* perfered old SA rather than new SA */
322 SYSCTL_INT(_net_key
, KEYCTL_PREFERED_OLDSA
, prefered_oldsa
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
323 &key_preferred_oldsa
, 0, "");
325 /* time between NATT keepalives in seconds, 0 disabled */
326 SYSCTL_INT(_net_key
, KEYCTL_NATT_KEEPALIVE_INTERVAL
, natt_keepalive_interval
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
327 &natt_keepalive_interval
, 0, "");
329 /* PF_KEY statistics */
330 SYSCTL_STRUCT(_net_key
, KEYCTL_PFKEYSTAT
, pfkeystat
, CTLFLAG_RD
| CTLFLAG_LOCKED
, \
331 &pfkeystat
, pfkeystat
, "");
334 #define LIST_FOREACH(elm, head, field) \
335 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
337 #define __LIST_CHAINED(elm) \
338 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
339 #define LIST_INSERT_TAIL(head, elm, type, field) \
341 struct type *curelm = LIST_FIRST(head); \
342 if (curelm == NULL) {\
343 LIST_INSERT_HEAD(head, elm, field); \
345 while (LIST_NEXT(curelm, field)) \
346 curelm = LIST_NEXT(curelm, field);\
347 LIST_INSERT_AFTER(curelm, elm, field);\
351 #define KEY_CHKSASTATE(head, sav, name) \
353 if ((head) != (sav)) { \
354 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
355 (name), (head), (sav))); \
360 #define KEY_CHKSPDIR(head, sp, name) \
362 if ((head) != (sp)) { \
363 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
364 "anyway continue.\n", \
365 (name), (head), (sp))); \
370 #define KMALLOC_WAIT(p, t, n) \
371 ((p) = (t) _MALLOC((u_int32_t)(n), M_SECA, M_WAITOK))
372 #define KMALLOC_NOWAIT(p, t, n) \
373 ((p) = (t) _MALLOC((u_int32_t)(n), M_SECA, M_NOWAIT))
375 _FREE((caddr_t)(p), M_SECA);
377 #define KMALLOC_WAIT(p, t, n) \
379 ((p) = (t)_MALLOC((u_int32_t)(n), M_SECA, M_WAITOK)); \
380 printf("%s %d: %p <- KMALLOC_WAIT(%s, %d)\n", \
381 __FILE__, __LINE__, (p), #t, n); \
383 #define KMALLOC_NOWAIT(p, t, n) \
385 ((p) = (t)_MALLOC((u_int32_t)(n), M_SECA, M_NOWAIT)); \
386 printf("%s %d: %p <- KMALLOC_NOWAIT(%s, %d)\n", \
387 __FILE__, __LINE__, (p), #t, n); \
392 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
393 _FREE((caddr_t)(p), M_SECA); \
398 * set parameters into secpolicyindex buffer.
399 * Must allocate secpolicyindex buffer passed to this function.
401 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, ifp, s_s, s_e, d_s, d_e, idx) \
403 bzero((idx), sizeof(struct secpolicyindex)); \
404 (idx)->dir = (_dir); \
405 (idx)->prefs = (ps); \
406 (idx)->prefd = (pd); \
407 (idx)->ul_proto = (ulp); \
408 (idx)->internal_if = (ifp); \
409 if (s) bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
410 if (d) bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
411 if (s_s) bcopy((s_s), &(idx)->src_range.start, ((struct sockaddr *)(s_s))->sa_len); \
412 if (s_e) bcopy((s_e), &(idx)->src_range.end, ((struct sockaddr *)(s_e))->sa_len); \
413 if (d_s) bcopy((d_s), &(idx)->dst_range.start, ((struct sockaddr *)(d_s))->sa_len); \
414 if (d_e) bcopy((d_e), &(idx)->dst_range.end, ((struct sockaddr *)(d_e))->sa_len); \
418 * set parameters into secasindex buffer.
419 * Must allocate secasindex buffer before calling this function.
421 #define KEY_SETSECASIDX(p, m, r, s, d, ifi, idx) \
423 bzero((idx), sizeof(struct secasindex)); \
424 (idx)->proto = (p); \
426 (idx)->reqid = (r); \
427 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
428 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
429 (idx)->ipsec_ifindex = (ifi); \
434 u_int32_t getspi_count
; /* the avarage of count to try to get new SPI */
438 struct sadb_msg
*msg
;
439 struct sadb_ext
*ext
[SADB_EXT_MAX
+ 1];
440 int extoff
[SADB_EXT_MAX
+ 1];
441 int extlen
[SADB_EXT_MAX
+ 1];
444 static struct secpolicy
*__key_getspbyid(u_int32_t id
);
445 static struct secasvar
*key_do_allocsa_policy(struct secashead
*, u_int
, u_int16_t
);
446 static int key_do_get_translated_port(struct secashead
*, struct secasvar
*, u_int
);
447 static void key_delsp(struct secpolicy
*);
448 static struct secpolicy
*key_getsp(struct secpolicyindex
*);
449 static u_int32_t
key_newreqid(void);
450 static struct mbuf
*key_gather_mbuf(struct mbuf
*,
451 const struct sadb_msghdr
*, int, int, int *);
452 static int key_spdadd(struct socket
*, struct mbuf
*,
453 const struct sadb_msghdr
*);
454 static u_int32_t
key_getnewspid(void);
455 static int key_spddelete(struct socket
*, struct mbuf
*,
456 const struct sadb_msghdr
*);
457 static int key_spddelete2(struct socket
*, struct mbuf
*,
458 const struct sadb_msghdr
*);
459 static int key_spdenable(struct socket
*, struct mbuf
*,
460 const struct sadb_msghdr
*);
461 static int key_spddisable(struct socket
*, struct mbuf
*,
462 const struct sadb_msghdr
*);
463 static int key_spdget(struct socket
*, struct mbuf
*,
464 const struct sadb_msghdr
*);
465 static int key_spdflush(struct socket
*, struct mbuf
*,
466 const struct sadb_msghdr
*);
467 static int key_spddump(struct socket
*, struct mbuf
*,
468 const struct sadb_msghdr
*);
469 static struct mbuf
*key_setdumpsp(struct secpolicy
*,
470 u_int8_t
, u_int32_t
, u_int32_t
);
471 static u_int
key_getspreqmsglen(struct secpolicy
*);
472 static int key_spdexpire(struct secpolicy
*);
473 static struct secashead
*key_newsah(struct secasindex
*, ifnet_t
, u_int
, u_int8_t
);
474 static struct secasvar
*key_newsav(struct mbuf
*,
475 const struct sadb_msghdr
*, struct secashead
*, int *,
477 static struct secashead
*key_getsah(struct secasindex
*);
478 static struct secasvar
*key_checkspidup(struct secasindex
*, u_int32_t
);
479 static void key_setspi
__P((struct secasvar
*, u_int32_t
));
480 static struct secasvar
*key_getsavbyspi(struct secashead
*, u_int32_t
);
481 static int key_setsaval(struct secasvar
*, struct mbuf
*,
482 const struct sadb_msghdr
*);
483 static int key_mature(struct secasvar
*);
484 static struct mbuf
*key_setdumpsa(struct secasvar
*, u_int8_t
,
485 u_int8_t
, u_int32_t
, u_int32_t
);
486 static struct mbuf
*key_setsadbmsg(u_int8_t
, u_int16_t
, u_int8_t
,
487 u_int32_t
, pid_t
, u_int16_t
);
488 static struct mbuf
*key_setsadbsa(struct secasvar
*);
489 static struct mbuf
*key_setsadbaddr(u_int16_t
,
490 struct sockaddr
*, u_int8_t
, u_int16_t
);
491 static struct mbuf
*key_setsadbipsecif(ifnet_t
, ifnet_t
, ifnet_t
, int);
493 static struct mbuf
*key_setsadbident(u_int16_t
, u_int16_t
, caddr_t
,
496 static struct mbuf
*key_setsadbxsa2(u_int8_t
, u_int32_t
, u_int32_t
, u_int16_t
);
497 static struct mbuf
*key_setsadbxpolicy(u_int16_t
, u_int8_t
,
499 static void *key_newbuf(const void *, u_int
);
501 static int key_ismyaddr6(struct sockaddr_in6
*);
503 static void key_update_natt_keepalive_timestamp(struct secasvar
*, struct secasvar
*);
505 /* flags for key_cmpsaidx() */
506 #define CMP_HEAD 0x1 /* protocol, addresses. */
507 #define CMP_PORT 0x2 /* additionally HEAD, reqid, mode. */
508 #define CMP_REQID 0x4 /* additionally HEAD, reqid. */
509 #define CMP_MODE 0x8 /* additionally mode. */
510 #define CMP_EXACTLY 0xF /* all elements. */
511 static int key_cmpsaidx(struct secasindex
*, struct secasindex
*, int);
513 static int key_cmpspidx_exactly(struct secpolicyindex
*,
514 struct secpolicyindex
*);
515 static int key_cmpspidx_withmask(struct secpolicyindex
*,
516 struct secpolicyindex
*);
517 static int key_sockaddrcmp(struct sockaddr
*, struct sockaddr
*, int);
518 static int key_is_addr_in_range(struct sockaddr_storage
*, struct secpolicyaddrrange
*);
519 static int key_bbcmp(caddr_t
, caddr_t
, u_int
);
520 static void key_srandom(void);
521 static u_int16_t
key_satype2proto(u_int8_t
);
522 static u_int8_t
key_proto2satype(u_int16_t
);
524 static int key_getspi(struct socket
*, struct mbuf
*,
525 const struct sadb_msghdr
*);
526 static u_int32_t
key_do_getnewspi(struct sadb_spirange
*, struct secasindex
*);
527 static int key_update(struct socket
*, struct mbuf
*,
528 const struct sadb_msghdr
*);
530 static struct secasvar
*key_getsavbyseq(struct secashead
*, u_int32_t
);
532 static int key_add(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
533 static int key_setident(struct secashead
*, struct mbuf
*,
534 const struct sadb_msghdr
*);
535 static struct mbuf
*key_getmsgbuf_x1(struct mbuf
*, const struct sadb_msghdr
*);
536 static int key_delete(struct socket
*, struct mbuf
*,
537 const struct sadb_msghdr
*);
538 static int key_get(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
540 static void key_getcomb_setlifetime(struct sadb_comb
*);
542 static struct mbuf
*key_getcomb_esp(void);
544 static struct mbuf
*key_getcomb_ah(void);
545 static struct mbuf
*key_getprop(const struct secasindex
*);
547 static int key_acquire(struct secasindex
*, struct secpolicy
*);
548 #ifndef IPSEC_NONBLOCK_ACQUIRE
549 static struct secacq
*key_newacq(struct secasindex
*);
550 static struct secacq
*key_getacq(struct secasindex
*);
551 static struct secacq
*key_getacqbyseq(u_int32_t
);
553 static struct secspacq
*key_newspacq(struct secpolicyindex
*);
554 static struct secspacq
*key_getspacq(struct secpolicyindex
*);
555 static int key_acquire2(struct socket
*, struct mbuf
*,
556 const struct sadb_msghdr
*);
557 static int key_register(struct socket
*, struct mbuf
*,
558 const struct sadb_msghdr
*);
559 static int key_expire(struct secasvar
*);
560 static int key_flush(struct socket
*, struct mbuf
*,
561 const struct sadb_msghdr
*);
562 static int key_dump(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
563 static int key_promisc(struct socket
*, struct mbuf
*,
564 const struct sadb_msghdr
*);
565 static int key_senderror(struct socket
*, struct mbuf
*, int);
566 static int key_validate_ext(const struct sadb_ext
*, int);
567 static int key_align(struct mbuf
*, struct sadb_msghdr
*);
568 static struct mbuf
*key_alloc_mbuf(int);
569 static int key_getsastat(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
570 static int key_migrate(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
571 static int key_setsaval2(struct secasvar
*sav
,
577 struct sadb_key
*key_auth
,
578 u_int16_t key_auth_len
,
579 struct sadb_key
*key_enc
,
580 u_int16_t key_enc_len
,
585 struct sadb_lifetime
*lifetime_hard
,
586 struct sadb_lifetime
*lifetime_soft
);
587 static void bzero_keys(const struct sadb_msghdr
*);
589 extern int ipsec_bypass
;
590 extern int esp_udp_encap_port
;
591 int ipsec_send_natt_keepalive(struct secasvar
*sav
);
592 bool ipsec_fill_offload_frame(ifnet_t ifp
, struct secasvar
*sav
, struct ifnet_keepalive_offload_frame
*frame
, size_t frame_data_offset
);
594 void key_init(struct protosw
*, struct domain
*);
598 * setup locks, call raw_init(), and then init timer and associated data
602 key_init(struct protosw
*pp
, struct domain
*dp
)
604 static int key_initialized
= 0;
607 VERIFY((pp
->pr_flags
& (PR_INITIALIZED
| PR_ATTACHED
)) == PR_ATTACHED
);
609 _CASSERT(PFKEY_ALIGN8(sizeof(struct sadb_msg
)) <= _MHLEN
);
610 _CASSERT(MAX_REPLAY_WINDOWS
== MBUF_TC_MAX
);
612 if (key_initialized
) {
617 sadb_mutex_grp_attr
= lck_grp_attr_alloc_init();
618 sadb_mutex_grp
= lck_grp_alloc_init("sadb", sadb_mutex_grp_attr
);
619 sadb_mutex_attr
= lck_attr_alloc_init();
621 lck_mtx_init(sadb_mutex
, sadb_mutex_grp
, sadb_mutex_attr
);
623 pfkey_stat_mutex_grp_attr
= lck_grp_attr_alloc_init();
624 pfkey_stat_mutex_grp
= lck_grp_alloc_init("pfkey_stat", pfkey_stat_mutex_grp_attr
);
625 pfkey_stat_mutex_attr
= lck_attr_alloc_init();
627 lck_mtx_init(pfkey_stat_mutex
, pfkey_stat_mutex_grp
, pfkey_stat_mutex_attr
);
629 for (i
= 0; i
< SPIHASHSIZE
; i
++) {
630 LIST_INIT(&spihash
[i
]);
635 bzero((caddr_t
)&key_cb
, sizeof(key_cb
));
637 for (i
= 0; i
< IPSEC_DIR_MAX
; i
++) {
638 LIST_INIT(&sptree
[i
]);
640 ipsec_policy_count
= 0;
644 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
645 LIST_INIT(®tree
[i
]);
649 #ifndef IPSEC_NONBLOCK_ACQUIRE
652 LIST_INIT(&spacqtree
);
656 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
657 ip4_def_policy
.refcnt
++; /*never reclaim this*/
660 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
661 ip6_def_policy
.refcnt
++; /*never reclaim this*/
664 key_timehandler_running
= 0;
666 /* initialize key statistics */
667 keystat
.getspi_count
= 1;
671 printf("IPsec: Initialized Security Association Processing.\n");
676 key_start_timehandler(void)
678 /* must be called while locked */
679 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
680 if (key_timehandler_running
== 0) {
681 key_timehandler_running
= 1;
682 (void)timeout((void *)key_timehandler
, (void *)0, hz
);
685 /* Turn off the ipsec bypass */
686 if (ipsec_bypass
!= 0) {
691 /* %%% IPsec policy management */
693 * allocating a SP for OUTBOUND or INBOUND packet.
694 * Must call key_freesp() later.
695 * OUT: NULL: not found
696 * others: found and return the pointer.
700 struct secpolicyindex
*spidx
,
703 struct secpolicy
*sp
;
706 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
709 panic("key_allocsp: NULL pointer is passed.\n");
712 /* check direction */
714 case IPSEC_DIR_INBOUND
:
715 case IPSEC_DIR_OUTBOUND
:
718 panic("key_allocsp: Invalid direction is passed.\n");
722 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
723 printf("*** objects\n");
724 kdebug_secpolicyindex(spidx
));
726 lck_mtx_lock(sadb_mutex
);
727 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
728 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
729 printf("*** in SPD\n");
730 kdebug_secpolicyindex(&sp
->spidx
));
732 if (sp
->state
== IPSEC_SPSTATE_DEAD
) {
736 /* If the policy is disabled, skip */
737 if (sp
->disabled
> 0) {
741 /* If the incoming spidx specifies bound if,
742 * ignore unbound policies*/
743 if (spidx
->internal_if
!= NULL
744 && (sp
->spidx
.internal_if
== NULL
|| sp
->ipsec_if
== NULL
)) {
748 if (key_cmpspidx_withmask(&sp
->spidx
, spidx
)) {
752 lck_mtx_unlock(sadb_mutex
);
757 /* found a SPD entry */
759 sp
->lastused
= tv
.tv_sec
;
761 lck_mtx_unlock(sadb_mutex
);
764 KEY_CHKSPDIR(sp
->spidx
.dir
, dir
, "key_allocsp");
765 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
766 printf("DP key_allocsp cause refcnt++:%d SP:0x%llx\n",
767 sp
->refcnt
, (uint64_t)VM_KERNEL_ADDRPERM(sp
)));
772 * return a policy that matches this particular inbound packet.
777 struct sockaddr
*osrc
,
778 struct sockaddr
*odst
,
779 struct sockaddr
*isrc
,
780 struct sockaddr
*idst
)
782 struct secpolicy
*sp
;
783 const int dir
= IPSEC_DIR_INBOUND
;
785 struct ipsecrequest
*r1
, *r2
, *p
;
786 struct sockaddr
*os
, *od
, *is
, *id
;
787 struct secpolicyindex spidx
;
789 if (isrc
->sa_family
!= idst
->sa_family
) {
790 ipseclog((LOG_ERR
, "protocol family mismatched %d != %d\n.",
791 isrc
->sa_family
, idst
->sa_family
));
795 lck_mtx_lock(sadb_mutex
);
796 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
797 if (sp
->state
== IPSEC_SPSTATE_DEAD
) {
802 for (p
= sp
->req
; p
; p
= p
->next
) {
803 if (p
->saidx
.mode
!= IPSEC_MODE_TUNNEL
) {
811 /* here we look at address matches only */
813 if (isrc
->sa_len
> sizeof(spidx
.src
) ||
814 idst
->sa_len
> sizeof(spidx
.dst
)) {
817 bcopy(isrc
, &spidx
.src
, isrc
->sa_len
);
818 bcopy(idst
, &spidx
.dst
, idst
->sa_len
);
819 if (!key_cmpspidx_withmask(&sp
->spidx
, &spidx
)) {
823 is
= (struct sockaddr
*)&r1
->saidx
.src
;
824 id
= (struct sockaddr
*)&r1
->saidx
.dst
;
825 if (key_sockaddrcmp(is
, isrc
, 0) ||
826 key_sockaddrcmp(id
, idst
, 0)) {
831 os
= (struct sockaddr
*)&r2
->saidx
.src
;
832 od
= (struct sockaddr
*)&r2
->saidx
.dst
;
833 if (key_sockaddrcmp(os
, osrc
, 0) ||
834 key_sockaddrcmp(od
, odst
, 0)) {
841 lck_mtx_unlock(sadb_mutex
);
846 sp
->lastused
= tv
.tv_sec
;
848 lck_mtx_unlock(sadb_mutex
);
853 key_alloc_outbound_sav_for_interface(ifnet_t interface
, int family
,
854 struct sockaddr
*src
,
855 struct sockaddr
*dst
)
857 struct secashead
*sah
;
858 struct secasvar
*sav
;
861 const u_int
*saorder_state_valid
;
863 struct sockaddr_in
*sin
;
867 if (interface
== NULL
) {
871 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
873 lck_mtx_lock(sadb_mutex
);
876 LIST_FOREACH(sah
, &sahtree
, chain
) {
877 if (sah
->state
== SADB_SASTATE_DEAD
) {
880 if (sah
->ipsec_if
== interface
&&
881 (family
== AF_INET6
|| family
== AF_INET
) &&
882 sah
->dir
== IPSEC_DIR_OUTBOUND
) {
884 sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
&&
885 src
!= NULL
&& dst
!= NULL
) {
886 // Validate addresses for transport mode
887 if (key_sockaddrcmp((struct sockaddr
*)&sah
->saidx
.src
, src
, 0) != 0) {
888 // Source doesn't match
892 if (key_sockaddrcmp((struct sockaddr
*)&sah
->saidx
.dst
, dst
, 0) != 0) {
893 // Destination doesn't match
898 /* This SAH is linked to the IPsec interface, and the right family. We found it! */
899 if (key_preferred_oldsa
) {
900 saorder_state_valid
= saorder_state_valid_prefer_old
;
901 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
903 saorder_state_valid
= saorder_state_valid_prefer_new
;
904 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
907 sin
= (struct sockaddr_in
*)&sah
->saidx
.dst
;
908 dstport
= sin
->sin_port
;
909 if (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
) {
910 sin
->sin_port
= IPSEC_PORT_ANY
;
913 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
914 state
= saorder_state_valid
[stateidx
];
915 sav
= key_do_allocsa_policy(sah
, state
, dstport
);
917 lck_mtx_unlock(sadb_mutex
);
926 // If we didn't find anything, try again without strict
929 // We already were on the second try, bail
934 lck_mtx_unlock(sadb_mutex
);
939 * allocating an SA entry for an *OUTBOUND* packet.
940 * checking each request entries in SP, and acquire an SA if need.
941 * OUT: 0: there are valid requests.
942 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
946 struct ipsecrequest
*isr
,
947 struct secasindex
*saidx
,
948 struct secasvar
**sav
)
952 struct sockaddr_in
*sin
;
954 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
959 if (isr
== NULL
|| saidx
== NULL
) {
960 panic("key_checkrequest: NULL pointer is passed.\n");
964 switch (saidx
->mode
) {
965 case IPSEC_MODE_TRANSPORT
:
966 case IPSEC_MODE_TUNNEL
:
970 panic("key_checkrequest: Invalid policy defined.\n");
973 /* get current level */
974 level
= ipsec_get_reqlevel(isr
);
978 * key_allocsa_policy should allocate the oldest SA available.
979 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
982 *sav
= key_allocsa_policy(saidx
);
985 /* When there is SA. */
992 * Remove dst port - used for special natt support - don't call
993 * key_acquire with it.
995 if (saidx
->mode
== IPSEC_MODE_TRANSPORT
) {
996 sin
= (struct sockaddr_in
*)&saidx
->dst
;
997 sin
->sin_port
= IPSEC_PORT_ANY
;
999 if ((error
= key_acquire(saidx
, isr
->sp
)) != 0) {
1000 /* XXX What should I do ? */
1001 ipseclog((LOG_DEBUG
, "key_checkrequest: error %d returned "
1002 "from key_acquire.\n", error
));
1006 return level
== IPSEC_LEVEL_REQUIRE
? ENOENT
: 0;
1010 * allocating a SA for policy entry from SAD.
1011 * NOTE: searching SAD of aliving state.
1012 * OUT: NULL: not found.
1013 * others: found and return the pointer.
1015 u_int32_t sah_search_calls
= 0;
1016 u_int32_t sah_search_count
= 0;
1019 struct secasindex
*saidx
)
1021 struct secashead
*sah
;
1022 struct secasvar
*sav
;
1023 u_int stateidx
, state
;
1024 const u_int
*saorder_state_valid
;
1026 struct sockaddr_in
*sin
;
1029 lck_mtx_lock(sadb_mutex
);
1031 LIST_FOREACH(sah
, &sahtree
, chain
) {
1033 if (sah
->state
== SADB_SASTATE_DEAD
) {
1036 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_MODE
| CMP_REQID
)) {
1040 lck_mtx_unlock(sadb_mutex
);
1046 * search a valid state list for outbound packet.
1047 * This search order is important.
1049 if (key_preferred_oldsa
) {
1050 saorder_state_valid
= saorder_state_valid_prefer_old
;
1051 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
1053 saorder_state_valid
= saorder_state_valid_prefer_new
;
1054 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
1058 sin
= (struct sockaddr_in
*)&saidx
->dst
;
1059 dstport
= sin
->sin_port
;
1060 if (saidx
->mode
== IPSEC_MODE_TRANSPORT
) {
1061 sin
->sin_port
= IPSEC_PORT_ANY
;
1064 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
1065 state
= saorder_state_valid
[stateidx
];
1067 sav
= key_do_allocsa_policy(sah
, state
, dstport
);
1069 lck_mtx_unlock(sadb_mutex
);
1073 lck_mtx_unlock(sadb_mutex
);
1078 key_send_delete(struct secasvar
*sav
)
1080 struct mbuf
*m
, *result
;
1083 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
1085 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0) {
1086 panic("key_do_allocsa_policy: invalid proto is passed.\n");
1089 m
= key_setsadbmsg(SADB_DELETE
, 0,
1090 satype
, 0, 0, sav
->refcnt
- 1);
1096 /* set sadb_address for saidx's. */
1097 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
1098 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
1099 sav
->sah
->saidx
.src
.ss_len
<< 3,
1106 /* set sadb_address for saidx's. */
1107 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
1108 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
1109 sav
->sah
->saidx
.src
.ss_len
<< 3,
1116 /* create SA extension */
1117 m
= key_setsadbsa(sav
);
1123 if (result
->m_len
< sizeof(struct sadb_msg
)) {
1124 result
= m_pullup(result
,
1125 sizeof(struct sadb_msg
));
1126 if (result
== NULL
) {
1131 result
->m_pkthdr
.len
= 0;
1132 for (m
= result
; m
; m
= m
->m_next
) {
1133 result
->m_pkthdr
.len
+= m
->m_len
;
1135 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
1136 PFKEY_UNIT64(result
->m_pkthdr
.len
);
1138 if (key_sendup_mbuf(NULL
, result
,
1139 KEY_SENDUP_REGISTERED
)) {
1143 key_freesav(sav
, KEY_SADB_LOCKED
);
1147 * searching SAD with direction, protocol, mode and state.
1148 * called by key_allocsa_policy().
1151 * others : found, pointer to a SA.
1153 static struct secasvar
*
1154 key_do_allocsa_policy(
1155 struct secashead
*sah
,
1159 struct secasvar
*sav
, *nextsav
, *candidate
, *natt_candidate
, *no_natt_candidate
, *d
;
1161 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1165 natt_candidate
= NULL
;
1166 no_natt_candidate
= NULL
;
1168 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
1171 nextsav
= LIST_NEXT(sav
, chain
);
1174 KEY_CHKSASTATE(sav
->state
, state
, "key_do_allocsa_policy");
1176 if (sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&& dstport
&&
1177 ((sav
->flags
& SADB_X_EXT_NATT
) != 0) &&
1178 ntohs(dstport
) != sav
->remote_ike_port
) {
1182 if (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
&&
1183 ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0) &&
1184 ntohs(dstport
) != sav
->remote_ike_port
) {
1185 continue; /* skip this one - not a match - or not UDP */
1187 if ((sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&&
1188 ((sav
->flags
& SADB_X_EXT_NATT
) != 0)) ||
1189 (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
&&
1190 ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0))) {
1191 if (natt_candidate
== NULL
) {
1192 natt_candidate
= sav
;
1195 candidate
= natt_candidate
;
1198 if (no_natt_candidate
== NULL
) {
1199 no_natt_candidate
= sav
;
1202 candidate
= no_natt_candidate
;
1206 /* Which SA is the better ? */
1208 /* sanity check 2 */
1209 if (candidate
->lft_c
== NULL
|| sav
->lft_c
== NULL
) {
1210 panic("key_do_allocsa_policy: "
1211 "lifetime_current is NULL.\n");
1214 /* What the best method is to compare ? */
1215 if (key_preferred_oldsa
) {
1216 if (candidate
->lft_c
->sadb_lifetime_addtime
>
1217 sav
->lft_c
->sadb_lifetime_addtime
) {
1218 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0) {
1219 natt_candidate
= sav
;
1221 no_natt_candidate
= sav
;
1228 /* prefered new sa rather than old sa */
1229 if (candidate
->lft_c
->sadb_lifetime_addtime
<
1230 sav
->lft_c
->sadb_lifetime_addtime
) {
1232 if ((sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&&
1233 ((sav
->flags
& SADB_X_EXT_NATT
) != 0)) ||
1234 (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
&&
1235 ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0))) {
1236 natt_candidate
= sav
;
1238 no_natt_candidate
= sav
;
1245 * prepared to delete the SA when there is more
1246 * suitable candidate and the lifetime of the SA is not
1249 if (d
->lft_c
->sadb_lifetime_addtime
!= 0) {
1254 /* choose latest if both types present */
1255 if (natt_candidate
== NULL
) {
1256 candidate
= no_natt_candidate
;
1257 } else if (no_natt_candidate
== NULL
) {
1258 candidate
= natt_candidate
;
1259 } else if (sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&& dstport
) {
1260 candidate
= natt_candidate
;
1261 } else if (natt_candidate
->lft_c
->sadb_lifetime_addtime
>
1262 no_natt_candidate
->lft_c
->sadb_lifetime_addtime
) {
1263 candidate
= natt_candidate
;
1265 candidate
= no_natt_candidate
;
1269 candidate
->refcnt
++;
1270 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1271 printf("DP allocsa_policy cause "
1272 "refcnt++:%d SA:0x%llx\n", candidate
->refcnt
,
1273 (uint64_t)VM_KERNEL_ADDRPERM(candidate
)));
1279 * allocating a SA entry for a *INBOUND* packet.
1280 * Must call key_freesav() later.
1281 * OUT: positive: pointer to a sav.
1282 * NULL: not found, or error occurred.
1284 * In the comparison, source address will be ignored for RFC2401 conformance.
1285 * To quote, from section 4.1:
1286 * A security association is uniquely identified by a triple consisting
1287 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1288 * security protocol (AH or ESP) identifier.
1289 * Note that, however, we do need to keep source address in IPsec SA.
1290 * IKE specification and PF_KEY specification do assume that we
1291 * keep source address in IPsec SA. We see a tricky situation here.
1301 return key_allocsa_extended(family
, src
, dst
, proto
, spi
, NULL
);
1305 key_allocsa_extended(u_int family
,
1312 struct secasvar
*sav
, *match
;
1313 u_int stateidx
, state
, tmpidx
, matchidx
;
1314 struct sockaddr_in sin
;
1315 struct sockaddr_in6 sin6
;
1316 const u_int
*saorder_state_valid
;
1319 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1322 if (src
== NULL
|| dst
== NULL
) {
1323 panic("key_allocsa: NULL pointer is passed.\n");
1327 * when both systems employ similar strategy to use a SA.
1328 * the search order is important even in the inbound case.
1330 if (key_preferred_oldsa
) {
1331 saorder_state_valid
= saorder_state_valid_prefer_old
;
1332 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
1334 saorder_state_valid
= saorder_state_valid_prefer_new
;
1335 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
1340 * XXX: to be checked internal IP header somewhere. Also when
1341 * IPsec tunnel packet is received. But ESP tunnel mode is
1342 * encrypted so we can't check internal IP header.
1345 * search a valid state list for inbound packet.
1346 * the search order is not important.
1349 matchidx
= arraysize
;
1350 lck_mtx_lock(sadb_mutex
);
1351 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
1352 if (sav
->spi
!= spi
) {
1355 if (interface
!= NULL
&&
1356 sav
->sah
->ipsec_if
!= interface
) {
1359 if (proto
!= sav
->sah
->saidx
.proto
) {
1362 if (family
!= sav
->sah
->saidx
.src
.ss_family
||
1363 family
!= sav
->sah
->saidx
.dst
.ss_family
) {
1367 for (stateidx
= 0; stateidx
< matchidx
; stateidx
++) {
1368 state
= saorder_state_valid
[stateidx
];
1369 if (sav
->state
== state
) {
1374 if (tmpidx
>= matchidx
) {
1378 #if 0 /* don't check src */
1379 /* check src address */
1382 bzero(&sin
, sizeof(sin
));
1383 sin
.sin_family
= AF_INET
;
1384 sin
.sin_len
= sizeof(sin
);
1385 bcopy(src
, &sin
.sin_addr
,
1386 sizeof(sin
.sin_addr
));
1387 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
1388 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0) {
1393 bzero(&sin6
, sizeof(sin6
));
1394 sin6
.sin6_family
= AF_INET6
;
1395 sin6
.sin6_len
= sizeof(sin6
);
1396 bcopy(src
, &sin6
.sin6_addr
,
1397 sizeof(sin6
.sin6_addr
));
1398 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
1399 /* kame fake scopeid */
1400 sin6
.sin6_scope_id
=
1401 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
1402 sin6
.sin6_addr
.s6_addr16
[1] = 0;
1404 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
1405 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0) {
1410 ipseclog((LOG_DEBUG
, "key_allocsa: "
1411 "unknown address family=%d.\n",
1417 /* check dst address */
1420 bzero(&sin
, sizeof(sin
));
1421 sin
.sin_family
= AF_INET
;
1422 sin
.sin_len
= sizeof(sin
);
1423 bcopy(dst
, &sin
.sin_addr
,
1424 sizeof(sin
.sin_addr
));
1425 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
1426 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0) {
1432 bzero(&sin6
, sizeof(sin6
));
1433 sin6
.sin6_family
= AF_INET6
;
1434 sin6
.sin6_len
= sizeof(sin6
);
1435 bcopy(dst
, &sin6
.sin6_addr
,
1436 sizeof(sin6
.sin6_addr
));
1437 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
1438 /* kame fake scopeid */
1439 sin6
.sin6_scope_id
=
1440 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
1441 sin6
.sin6_addr
.s6_addr16
[1] = 0;
1443 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
1444 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0) {
1449 ipseclog((LOG_DEBUG
, "key_allocsa: "
1450 "unknown address family=%d.\n", family
));
1462 lck_mtx_unlock(sadb_mutex
);
1467 lck_mtx_unlock(sadb_mutex
);
1468 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1469 printf("DP allocsa cause refcnt++:%d SA:0x%llx\n",
1470 match
->refcnt
, (uint64_t)VM_KERNEL_ADDRPERM(match
)));
1475 key_natt_get_translated_port(
1476 struct secasvar
*outsav
)
1478 struct secasindex saidx
;
1479 struct secashead
*sah
;
1480 u_int stateidx
, state
;
1481 const u_int
*saorder_state_valid
;
1484 /* get sa for incoming */
1485 saidx
.mode
= outsav
->sah
->saidx
.mode
;
1487 saidx
.proto
= outsav
->sah
->saidx
.proto
;
1488 bcopy(&outsav
->sah
->saidx
.src
, &saidx
.dst
, sizeof(struct sockaddr_in
));
1489 bcopy(&outsav
->sah
->saidx
.dst
, &saidx
.src
, sizeof(struct sockaddr_in
));
1491 lck_mtx_lock(sadb_mutex
);
1492 LIST_FOREACH(sah
, &sahtree
, chain
) {
1493 if (sah
->state
== SADB_SASTATE_DEAD
) {
1496 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_MODE
)) {
1500 lck_mtx_unlock(sadb_mutex
);
1505 * Found sah - now go thru list of SAs and find
1506 * matching remote ike port. If found - set
1507 * sav->natt_encapsulated_src_port and return the port.
1510 * search a valid state list for outbound packet.
1511 * This search order is important.
1513 if (key_preferred_oldsa
) {
1514 saorder_state_valid
= saorder_state_valid_prefer_old
;
1515 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
1517 saorder_state_valid
= saorder_state_valid_prefer_new
;
1518 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
1521 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
1522 state
= saorder_state_valid
[stateidx
];
1523 if (key_do_get_translated_port(sah
, outsav
, state
)) {
1524 lck_mtx_unlock(sadb_mutex
);
1525 return outsav
->natt_encapsulated_src_port
;
1528 lck_mtx_unlock(sadb_mutex
);
1533 key_do_get_translated_port(
1534 struct secashead
*sah
,
1535 struct secasvar
*outsav
,
1538 struct secasvar
*currsav
, *nextsav
, *candidate
;
1541 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1546 for (currsav
= LIST_FIRST(&sah
->savtree
[state
]);
1548 currsav
= nextsav
) {
1549 nextsav
= LIST_NEXT(currsav
, chain
);
1552 KEY_CHKSASTATE(currsav
->state
, state
, "key_do_get_translated_port");
1554 if ((currsav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) == 0 ||
1555 currsav
->remote_ike_port
!= outsav
->remote_ike_port
) {
1559 if (candidate
== NULL
) {
1560 candidate
= currsav
;
1564 /* Which SA is the better ? */
1566 /* sanity check 2 */
1567 if (candidate
->lft_c
== NULL
|| currsav
->lft_c
== NULL
) {
1568 panic("key_do_get_translated_port: "
1569 "lifetime_current is NULL.\n");
1572 /* What the best method is to compare ? */
1573 if (key_preferred_oldsa
) {
1574 if (candidate
->lft_c
->sadb_lifetime_addtime
>
1575 currsav
->lft_c
->sadb_lifetime_addtime
) {
1576 candidate
= currsav
;
1582 /* prefered new sa rather than old sa */
1583 if (candidate
->lft_c
->sadb_lifetime_addtime
<
1584 currsav
->lft_c
->sadb_lifetime_addtime
) {
1585 candidate
= currsav
;
1590 outsav
->natt_encapsulated_src_port
= candidate
->natt_encapsulated_src_port
;
1598 * Must be called after calling key_allocsp().
1602 struct secpolicy
*sp
,
1607 panic("key_freesp: NULL pointer is passed.\n");
1611 lck_mtx_lock(sadb_mutex
);
1613 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1616 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1617 printf("DP freesp cause refcnt--:%d SP:0x%llx\n",
1618 sp
->refcnt
, (uint64_t)VM_KERNEL_ADDRPERM(sp
)));
1620 if (sp
->refcnt
== 0) {
1624 lck_mtx_unlock(sadb_mutex
);
1630 * Must be called after calling key_allocsa().
1631 * This function is called by key_freesp() to free some SA allocated
1636 struct secasvar
*sav
,
1641 panic("key_freesav: NULL pointer is passed.\n");
1645 lck_mtx_lock(sadb_mutex
);
1647 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1650 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1651 printf("DP freesav cause refcnt--:%d SA:0x%llx SPI %u\n",
1652 sav
->refcnt
, (uint64_t)VM_KERNEL_ADDRPERM(sav
),
1653 (u_int32_t
)ntohl(sav
->spi
)));
1655 if (sav
->refcnt
== 0) {
1659 lck_mtx_unlock(sadb_mutex
);
1664 /* %%% SPD management */
1666 * free security policy entry.
1670 struct secpolicy
*sp
)
1674 panic("key_delsp: NULL pointer is passed.\n");
1677 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1678 sp
->state
= IPSEC_SPSTATE_DEAD
;
1680 if (sp
->refcnt
> 0) {
1681 return; /* can't free */
1683 /* remove from SP index */
1684 if (__LIST_CHAINED(sp
)) {
1685 LIST_REMOVE(sp
, chain
);
1686 ipsec_policy_count
--;
1689 if (sp
->spidx
.internal_if
) {
1690 ifnet_release(sp
->spidx
.internal_if
);
1691 sp
->spidx
.internal_if
= NULL
;
1695 ifnet_release(sp
->ipsec_if
);
1696 sp
->ipsec_if
= NULL
;
1699 if (sp
->outgoing_if
) {
1700 ifnet_release(sp
->outgoing_if
);
1701 sp
->outgoing_if
= NULL
;
1705 struct ipsecrequest
*isr
= sp
->req
, *nextisr
;
1707 while (isr
!= NULL
) {
1708 nextisr
= isr
->next
;
1713 keydb_delsecpolicy(sp
);
1720 * OUT: NULL : not found
1721 * others : found, pointer to a SP.
1723 static struct secpolicy
*
1725 struct secpolicyindex
*spidx
)
1727 struct secpolicy
*sp
;
1729 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1732 if (spidx
== NULL
) {
1733 panic("key_getsp: NULL pointer is passed.\n");
1736 LIST_FOREACH(sp
, &sptree
[spidx
->dir
], chain
) {
1737 if (sp
->state
== IPSEC_SPSTATE_DEAD
) {
1740 if (key_cmpspidx_exactly(spidx
, &sp
->spidx
)) {
1751 * OUT: NULL : not found
1752 * others : found, pointer to a SP.
1758 struct secpolicy
*sp
;
1760 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1762 lck_mtx_lock(sadb_mutex
);
1763 sp
= __key_getspbyid(id
);
1764 lck_mtx_unlock(sadb_mutex
);
1769 static struct secpolicy
*
1770 __key_getspbyid(u_int32_t id
)
1772 struct secpolicy
*sp
;
1774 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1776 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_INBOUND
], chain
) {
1777 if (sp
->state
== IPSEC_SPSTATE_DEAD
) {
1786 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_OUTBOUND
], chain
) {
1787 if (sp
->state
== IPSEC_SPSTATE_DEAD
) {
1802 struct secpolicy
*newsp
= NULL
;
1804 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1805 newsp
= keydb_newsecpolicy();
1817 * create secpolicy structure from sadb_x_policy structure.
1818 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1819 * so must be set properly later.
1823 struct sadb_x_policy
*xpl0
,
1827 struct secpolicy
*newsp
;
1829 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1833 panic("key_msg2sp: NULL pointer was passed.\n");
1835 if (len
< sizeof(*xpl0
)) {
1836 panic("key_msg2sp: invalid length.\n");
1838 if (len
!= PFKEY_EXTLEN(xpl0
)) {
1839 ipseclog((LOG_DEBUG
, "key_msg2sp: Invalid msg length.\n"));
1844 if ((newsp
= key_newsp()) == NULL
) {
1849 newsp
->spidx
.dir
= xpl0
->sadb_x_policy_dir
;
1850 newsp
->policy
= xpl0
->sadb_x_policy_type
;
1853 switch (xpl0
->sadb_x_policy_type
) {
1854 case IPSEC_POLICY_DISCARD
:
1855 case IPSEC_POLICY_GENERATE
:
1856 case IPSEC_POLICY_NONE
:
1857 case IPSEC_POLICY_ENTRUST
:
1858 case IPSEC_POLICY_BYPASS
:
1862 case IPSEC_POLICY_IPSEC
:
1865 struct sadb_x_ipsecrequest
*xisr
;
1866 struct ipsecrequest
**p_isr
= &newsp
->req
;
1868 /* validity check */
1869 if (PFKEY_EXTLEN(xpl0
) < sizeof(*xpl0
)) {
1870 ipseclog((LOG_DEBUG
,
1871 "key_msg2sp: Invalid msg length.\n"));
1872 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1877 tlen
= PFKEY_EXTLEN(xpl0
) - sizeof(*xpl0
);
1878 xisr
= (struct sadb_x_ipsecrequest
*)(xpl0
+ 1);
1881 if (tlen
< sizeof(*xisr
)) {
1882 ipseclog((LOG_DEBUG
, "key_msg2sp: "
1883 "invalid ipsecrequest.\n"));
1884 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1890 if (xisr
->sadb_x_ipsecrequest_len
< sizeof(*xisr
)) {
1891 ipseclog((LOG_DEBUG
, "key_msg2sp: "
1892 "invalid ipsecrequest length.\n"));
1893 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1898 /* allocate request buffer */
1899 KMALLOC_WAIT(*p_isr
, struct ipsecrequest
*, sizeof(**p_isr
));
1900 if ((*p_isr
) == NULL
) {
1901 ipseclog((LOG_DEBUG
,
1902 "key_msg2sp: No more memory.\n"));
1903 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1907 bzero(*p_isr
, sizeof(**p_isr
));
1910 (*p_isr
)->next
= NULL
;
1912 switch (xisr
->sadb_x_ipsecrequest_proto
) {
1917 ipseclog((LOG_DEBUG
,
1918 "key_msg2sp: invalid proto type=%u\n",
1919 xisr
->sadb_x_ipsecrequest_proto
));
1920 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1921 *error
= EPROTONOSUPPORT
;
1924 (*p_isr
)->saidx
.proto
= xisr
->sadb_x_ipsecrequest_proto
;
1926 switch (xisr
->sadb_x_ipsecrequest_mode
) {
1927 case IPSEC_MODE_TRANSPORT
:
1928 case IPSEC_MODE_TUNNEL
:
1930 case IPSEC_MODE_ANY
:
1932 ipseclog((LOG_DEBUG
,
1933 "key_msg2sp: invalid mode=%u\n",
1934 xisr
->sadb_x_ipsecrequest_mode
));
1935 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1939 (*p_isr
)->saidx
.mode
= xisr
->sadb_x_ipsecrequest_mode
;
1941 switch (xisr
->sadb_x_ipsecrequest_level
) {
1942 case IPSEC_LEVEL_DEFAULT
:
1943 case IPSEC_LEVEL_USE
:
1944 case IPSEC_LEVEL_REQUIRE
:
1946 case IPSEC_LEVEL_UNIQUE
:
1947 /* validity check */
1949 * If range violation of reqid, kernel will
1950 * update it, don't refuse it.
1952 if (xisr
->sadb_x_ipsecrequest_reqid
1953 > IPSEC_MANUAL_REQID_MAX
) {
1954 ipseclog((LOG_DEBUG
,
1955 "key_msg2sp: reqid=%d range "
1956 "violation, updated by kernel.\n",
1957 xisr
->sadb_x_ipsecrequest_reqid
));
1958 xisr
->sadb_x_ipsecrequest_reqid
= 0;
1961 /* allocate new reqid id if reqid is zero. */
1962 if (xisr
->sadb_x_ipsecrequest_reqid
== 0) {
1964 if ((reqid
= key_newreqid()) == 0) {
1965 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1969 (*p_isr
)->saidx
.reqid
= reqid
;
1970 xisr
->sadb_x_ipsecrequest_reqid
= reqid
;
1972 /* set it for manual keying. */
1973 (*p_isr
)->saidx
.reqid
=
1974 xisr
->sadb_x_ipsecrequest_reqid
;
1979 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid level=%u\n",
1980 xisr
->sadb_x_ipsecrequest_level
));
1981 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1985 (*p_isr
)->level
= xisr
->sadb_x_ipsecrequest_level
;
1987 /* set IP addresses if there */
1988 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
1989 struct sockaddr
*paddr
;
1991 if (tlen
< xisr
->sadb_x_ipsecrequest_len
) {
1992 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1993 "address length.\n"));
1994 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1999 paddr
= (struct sockaddr
*)(xisr
+ 1);
2000 uint8_t src_len
= paddr
->sa_len
;
2002 if (xisr
->sadb_x_ipsecrequest_len
< src_len
) {
2003 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
2004 "invalid source address length.\n"));
2005 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
2010 /* validity check */
2012 > sizeof((*p_isr
)->saidx
.src
)) {
2013 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
2014 "address length.\n"));
2015 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
2020 bcopy(paddr
, &(*p_isr
)->saidx
.src
,
2021 MIN(paddr
->sa_len
, sizeof((*p_isr
)->saidx
.src
)));
2023 paddr
= (struct sockaddr
*)((caddr_t
)paddr
+ paddr
->sa_len
);
2024 uint8_t dst_len
= paddr
->sa_len
;
2026 if (xisr
->sadb_x_ipsecrequest_len
< (src_len
+ dst_len
)) {
2027 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
2028 "invalid dest address length.\n"));
2029 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
2034 /* validity check */
2036 > sizeof((*p_isr
)->saidx
.dst
)) {
2037 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
2038 "address length.\n"));
2039 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
2044 bcopy(paddr
, &(*p_isr
)->saidx
.dst
,
2045 MIN(paddr
->sa_len
, sizeof((*p_isr
)->saidx
.dst
)));
2048 (*p_isr
)->sp
= newsp
;
2050 /* initialization for the next. */
2051 p_isr
= &(*p_isr
)->next
;
2052 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
2054 /* validity check */
2056 ipseclog((LOG_DEBUG
, "key_msg2sp: becoming tlen < 0.\n"));
2057 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
2062 xisr
= (struct sadb_x_ipsecrequest
*)(void *)
2063 ((caddr_t
)xisr
+ xisr
->sadb_x_ipsecrequest_len
);
2068 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid policy type.\n"));
2069 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
2081 lck_mtx_lock(sadb_mutex
);
2082 static u_int32_t auto_reqid
= IPSEC_MANUAL_REQID_MAX
+ 1;
2085 /* The reqid must be limited to 16 bits because the PF_KEY message format only uses
2086 * 16 bits for this field. Once it becomes larger than 16 bits - ipsec fails to
2087 * work anymore. Changing the PF_KEY message format would introduce compatibility
2088 * issues. This code now tests to see if the tentative reqid is in use */
2091 struct secpolicy
*sp
;
2092 struct ipsecrequest
*isr
;
2095 auto_reqid
= (auto_reqid
== 0xFFFF
2096 ? IPSEC_MANUAL_REQID_MAX
+ 1 : auto_reqid
+ 1);
2098 /* check for uniqueness */
2100 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2101 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2102 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2103 if (isr
->saidx
.reqid
== auto_reqid
) {
2118 lck_mtx_unlock(sadb_mutex
);
2123 * copy secpolicy struct to sadb_x_policy structure indicated.
2127 struct secpolicy
*sp
)
2129 struct sadb_x_policy
*xpl
;
2136 panic("key_sp2msg: NULL pointer was passed.\n");
2139 tlen
= key_getspreqmsglen(sp
);
2141 m
= key_alloc_mbuf(tlen
);
2142 if (!m
|| m
->m_next
) { /*XXX*/
2151 xpl
= mtod(m
, struct sadb_x_policy
*);
2154 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(tlen
);
2155 xpl
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2156 xpl
->sadb_x_policy_type
= sp
->policy
;
2157 xpl
->sadb_x_policy_dir
= sp
->spidx
.dir
;
2158 xpl
->sadb_x_policy_id
= sp
->id
;
2159 p
= (caddr_t
)xpl
+ sizeof(*xpl
);
2161 /* if is the policy for ipsec ? */
2162 if (sp
->policy
== IPSEC_POLICY_IPSEC
) {
2163 struct sadb_x_ipsecrequest
*xisr
;
2164 struct ipsecrequest
*isr
;
2166 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2167 xisr
= (struct sadb_x_ipsecrequest
*)(void *)p
;
2169 xisr
->sadb_x_ipsecrequest_proto
= isr
->saidx
.proto
;
2170 xisr
->sadb_x_ipsecrequest_mode
= isr
->saidx
.mode
;
2171 xisr
->sadb_x_ipsecrequest_level
= isr
->level
;
2172 xisr
->sadb_x_ipsecrequest_reqid
= isr
->saidx
.reqid
;
2175 bcopy(&isr
->saidx
.src
, p
, isr
->saidx
.src
.ss_len
);
2176 p
+= isr
->saidx
.src
.ss_len
;
2177 bcopy(&isr
->saidx
.dst
, p
, isr
->saidx
.dst
.ss_len
);
2178 p
+= isr
->saidx
.src
.ss_len
;
2180 xisr
->sadb_x_ipsecrequest_len
=
2181 PFKEY_ALIGN8(sizeof(*xisr
)
2182 + isr
->saidx
.src
.ss_len
2183 + isr
->saidx
.dst
.ss_len
);
2190 /* m will not be freed nor modified */
2191 static struct mbuf
*
2192 key_gather_mbuf(struct mbuf
*m
, const struct sadb_msghdr
*mhp
,
2193 int ndeep
, int nitem
, int *items
)
2197 struct mbuf
*result
= NULL
, *n
;
2200 if (m
== NULL
|| mhp
== NULL
) {
2201 panic("null pointer passed to key_gather");
2204 for (i
= 0; i
< nitem
; i
++) {
2206 if (idx
< 0 || idx
> SADB_EXT_MAX
) {
2209 /* don't attempt to pull empty extension */
2210 if (idx
== SADB_EXT_RESERVED
&& mhp
->msg
== NULL
) {
2213 if (idx
!= SADB_EXT_RESERVED
&&
2214 (mhp
->ext
[idx
] == NULL
|| mhp
->extlen
[idx
] == 0)) {
2218 if (idx
== SADB_EXT_RESERVED
) {
2219 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2220 MGETHDR(n
, M_WAITOK
, MT_DATA
); // sadb_msg len < MHLEN - enforced by _CASSERT
2226 m_copydata(m
, 0, sizeof(struct sadb_msg
),
2228 } else if (i
< ndeep
) {
2229 len
= mhp
->extlen
[idx
];
2230 n
= key_alloc_mbuf(len
);
2231 if (!n
|| n
->m_next
) { /*XXX*/
2237 m_copydata(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
2240 n
= m_copym(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
2254 if ((result
->m_flags
& M_PKTHDR
) != 0) {
2255 result
->m_pkthdr
.len
= 0;
2256 for (n
= result
; n
; n
= n
->m_next
) {
2257 result
->m_pkthdr
.len
+= n
->m_len
;
2269 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
2270 * add a entry to SP database, when received
2271 * <base, address(SD), (lifetime(H),) policy>
2273 * Adding to SP database,
2275 * <base, address(SD), (lifetime(H),) policy>
2276 * to the socket which was send.
2278 * SPDADD set a unique policy entry.
2279 * SPDSETIDX like SPDADD without a part of policy requests.
2280 * SPDUPDATE replace a unique policy entry.
2282 * m will always be freed.
2288 const struct sadb_msghdr
*mhp
)
2290 struct sadb_address
*src0
, *dst0
, *src1
= NULL
, *dst1
= NULL
;
2291 struct sadb_x_policy
*xpl0
, *xpl
;
2292 struct sadb_lifetime
*lft
= NULL
;
2293 struct secpolicyindex spidx
;
2294 struct secpolicy
*newsp
;
2296 ifnet_t internal_if
= NULL
;
2297 char *outgoing_if
= NULL
;
2298 char *ipsec_if
= NULL
;
2299 struct sadb_x_ipsecif
*ipsecifopts
= NULL
;
2301 int use_src_range
= 0;
2302 int use_dst_range
= 0;
2303 int init_disabled
= 0;
2304 int address_family
, address_len
;
2306 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2309 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
2310 panic("key_spdadd: NULL pointer is passed.\n");
2313 if (mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
] != NULL
&& mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
] != NULL
) {
2316 if (mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_START
] != NULL
&& mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_END
] != NULL
) {
2320 if ((!use_src_range
&& mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
) ||
2321 (!use_dst_range
&& mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) ||
2322 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
2323 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2324 return key_senderror(so
, m
, EINVAL
);
2326 if ((use_src_range
&& (mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_SRC_START
] < sizeof(struct sadb_address
)
2327 || mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_SRC_END
] < sizeof(struct sadb_address
))) ||
2328 (!use_src_range
&& mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
)) ||
2329 (use_dst_range
&& (mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_DST_START
] < sizeof(struct sadb_address
)
2330 || mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_DST_END
] < sizeof(struct sadb_address
))) ||
2331 (!use_dst_range
&& mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) ||
2332 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2333 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2334 return key_senderror(so
, m
, EINVAL
);
2336 if (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
) {
2337 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
]
2338 < sizeof(struct sadb_lifetime
)) {
2339 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2340 return key_senderror(so
, m
, EINVAL
);
2342 lft
= (struct sadb_lifetime
*)
2343 (void *)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
2345 if (mhp
->ext
[SADB_X_EXT_IPSECIF
] != NULL
) {
2346 if (mhp
->extlen
[SADB_X_EXT_IPSECIF
] < sizeof(struct sadb_x_ipsecif
)) {
2347 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2348 return key_senderror(so
, m
, EINVAL
);
2352 if (use_src_range
) {
2353 src0
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
];
2354 src1
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
];
2356 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
2358 if (use_dst_range
) {
2359 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_START
];
2360 dst1
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_END
];
2362 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
2364 xpl0
= (struct sadb_x_policy
*)(void *)mhp
->ext
[SADB_X_EXT_POLICY
];
2365 ipsecifopts
= (struct sadb_x_ipsecif
*)(void *)mhp
->ext
[SADB_X_EXT_IPSECIF
];
2367 /* check addresses */
2368 address_family
= ((struct sockaddr
*)(src0
+ 1))->sa_family
;
2369 address_len
= ((struct sockaddr
*)(src0
+ 1))->sa_len
;
2370 if (use_src_range
) {
2371 if (((struct sockaddr
*)(src1
+ 1))->sa_family
!= address_family
||
2372 ((struct sockaddr
*)(src1
+ 1))->sa_len
!= address_len
) {
2373 return key_senderror(so
, m
, EINVAL
);
2376 if (((struct sockaddr
*)(dst0
+ 1))->sa_family
!= address_family
||
2377 ((struct sockaddr
*)(dst0
+ 1))->sa_len
!= address_len
) {
2378 return key_senderror(so
, m
, EINVAL
);
2380 if (use_dst_range
) {
2381 if (((struct sockaddr
*)(dst1
+ 1))->sa_family
!= address_family
||
2382 ((struct sockaddr
*)(dst1
+ 1))->sa_len
!= address_len
) {
2383 return key_senderror(so
, m
, EINVAL
);
2387 /* checking the direction. */
2388 switch (xpl0
->sadb_x_policy_dir
) {
2389 case IPSEC_DIR_INBOUND
:
2390 case IPSEC_DIR_OUTBOUND
:
2393 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid SP direction.\n"));
2394 mhp
->msg
->sadb_msg_errno
= EINVAL
;
2399 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
2400 if (xpl0
->sadb_x_policy_type
== IPSEC_POLICY_ENTRUST
2401 || xpl0
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
) {
2402 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid policy type.\n"));
2403 return key_senderror(so
, m
, EINVAL
);
2406 /* policy requests are mandatory when action is ipsec. */
2407 if (mhp
->msg
->sadb_msg_type
!= SADB_X_SPDSETIDX
2408 && xpl0
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
2409 && mhp
->extlen
[SADB_X_EXT_POLICY
] <= sizeof(*xpl0
)) {
2410 ipseclog((LOG_DEBUG
, "key_spdadd: some policy requests part required.\n"));
2411 return key_senderror(so
, m
, EINVAL
);
2414 /* Process interfaces */
2415 if (ipsecifopts
!= NULL
) {
2416 if (ipsecifopts
->sadb_x_ipsecif_internal_if
[0]) {
2417 ifnet_find_by_name(ipsecifopts
->sadb_x_ipsecif_internal_if
, &internal_if
);
2419 if (ipsecifopts
->sadb_x_ipsecif_outgoing_if
[0]) {
2420 outgoing_if
= ipsecifopts
->sadb_x_ipsecif_outgoing_if
;
2422 if (ipsecifopts
->sadb_x_ipsecif_ipsec_if
[0]) {
2423 ipsec_if
= ipsecifopts
->sadb_x_ipsecif_ipsec_if
;
2425 init_disabled
= ipsecifopts
->sadb_x_ipsecif_init_disabled
;
2429 /* XXX boundary check against sa_len */
2430 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2433 src0
->sadb_address_prefixlen
,
2434 dst0
->sadb_address_prefixlen
,
2435 src0
->sadb_address_proto
,
2437 use_src_range
? src0
+ 1 : NULL
,
2438 use_src_range
? src1
+ 1 : NULL
,
2439 use_dst_range
? dst0
+ 1 : NULL
,
2440 use_dst_range
? dst1
+ 1 : NULL
,
2444 * checking there is SP already or not.
2445 * SPDUPDATE doesn't depend on whether there is a SP or not.
2446 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
2449 lck_mtx_lock(sadb_mutex
);
2450 newsp
= key_getsp(&spidx
);
2451 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
2453 newsp
->state
= IPSEC_SPSTATE_DEAD
;
2454 key_freesp(newsp
, KEY_SADB_LOCKED
);
2457 if (newsp
!= NULL
) {
2458 key_freesp(newsp
, KEY_SADB_LOCKED
);
2459 ipseclog((LOG_DEBUG
, "key_spdadd: a SP entry exists already.\n"));
2460 lck_mtx_unlock(sadb_mutex
);
2462 ifnet_release(internal_if
);
2465 return key_senderror(so
, m
, EEXIST
);
2468 lck_mtx_unlock(sadb_mutex
);
2470 /* allocation new SP entry */
2471 if ((newsp
= key_msg2sp(xpl0
, PFKEY_EXTLEN(xpl0
), &error
)) == NULL
) {
2473 ifnet_release(internal_if
);
2476 return key_senderror(so
, m
, error
);
2479 if ((newsp
->id
= key_getnewspid()) == 0) {
2480 keydb_delsecpolicy(newsp
);
2482 ifnet_release(internal_if
);
2485 return key_senderror(so
, m
, ENOBUFS
);
2488 /* XXX boundary check against sa_len */
2489 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2492 src0
->sadb_address_prefixlen
,
2493 dst0
->sadb_address_prefixlen
,
2494 src0
->sadb_address_proto
,
2496 use_src_range
? src0
+ 1 : NULL
,
2497 use_src_range
? src1
+ 1 : NULL
,
2498 use_dst_range
? dst0
+ 1 : NULL
,
2499 use_dst_range
? dst1
+ 1 : NULL
,
2504 * allow IPv6 over IPv4 or IPv4 over IPv6 tunnels using ESP -
2505 * otherwise reject if inner and outer address families not equal
2507 if (newsp
->req
&& newsp
->req
->saidx
.src
.ss_family
) {
2508 struct sockaddr
*sa
;
2509 sa
= (struct sockaddr
*)(src0
+ 1);
2510 if (sa
->sa_family
!= newsp
->req
->saidx
.src
.ss_family
) {
2511 if (newsp
->req
->saidx
.mode
!= IPSEC_MODE_TUNNEL
|| newsp
->req
->saidx
.proto
!= IPPROTO_ESP
) {
2512 keydb_delsecpolicy(newsp
);
2514 ifnet_release(internal_if
);
2517 return key_senderror(so
, m
, EINVAL
);
2521 if (newsp
->req
&& newsp
->req
->saidx
.dst
.ss_family
) {
2522 struct sockaddr
*sa
;
2523 sa
= (struct sockaddr
*)(dst0
+ 1);
2524 if (sa
->sa_family
!= newsp
->req
->saidx
.dst
.ss_family
) {
2525 if (newsp
->req
->saidx
.mode
!= IPSEC_MODE_TUNNEL
|| newsp
->req
->saidx
.proto
!= IPPROTO_ESP
) {
2526 keydb_delsecpolicy(newsp
);
2528 ifnet_release(internal_if
);
2531 return key_senderror(so
, m
, EINVAL
);
2538 newsp
->created
= tv
.tv_sec
;
2539 newsp
->lastused
= tv
.tv_sec
;
2540 newsp
->lifetime
= lft
? lft
->sadb_lifetime_addtime
: 0;
2541 newsp
->validtime
= lft
? lft
->sadb_lifetime_usetime
: 0;
2543 if (outgoing_if
!= NULL
) {
2544 ifnet_find_by_name(outgoing_if
, &newsp
->outgoing_if
);
2546 if (ipsec_if
!= NULL
) {
2547 ifnet_find_by_name(ipsec_if
, &newsp
->ipsec_if
);
2549 if (init_disabled
> 0) {
2550 newsp
->disabled
= 1;
2553 newsp
->refcnt
= 1; /* do not reclaim until I say I do */
2554 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
2555 lck_mtx_lock(sadb_mutex
);
2557 * policies of type generate should be at the end of the SPD
2558 * because they function as default discard policies
2559 * Don't start timehandler for generate policies
2561 if (newsp
->policy
== IPSEC_POLICY_GENERATE
) {
2562 LIST_INSERT_TAIL(&sptree
[newsp
->spidx
.dir
], newsp
, secpolicy
, chain
);
2563 } else { /* XXX until we have policy ordering in the kernel */
2564 struct secpolicy
*tmpsp
;
2566 LIST_FOREACH(tmpsp
, &sptree
[newsp
->spidx
.dir
], chain
)
2567 if (tmpsp
->policy
== IPSEC_POLICY_GENERATE
) {
2571 LIST_INSERT_BEFORE(tmpsp
, newsp
, chain
);
2573 LIST_INSERT_TAIL(&sptree
[newsp
->spidx
.dir
], newsp
, secpolicy
, chain
);
2575 key_start_timehandler();
2578 ipsec_policy_count
++;
2579 /* Turn off the ipsec bypass */
2580 if (ipsec_bypass
!= 0) {
2584 /* delete the entry in spacqtree */
2585 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
2586 struct secspacq
*spacq
;
2587 if ((spacq
= key_getspacq(&spidx
)) != NULL
) {
2588 /* reset counter in order to deletion by timehandler. */
2590 spacq
->created
= tv
.tv_sec
;
2594 lck_mtx_unlock(sadb_mutex
);
2597 struct mbuf
*n
, *mpolicy
;
2598 struct sadb_msg
*newmsg
;
2601 /* create new sadb_msg to reply. */
2603 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2604 SADB_EXT_LIFETIME_HARD
, SADB_EXT_ADDRESS_SRC
,
2605 SADB_EXT_ADDRESS_DST
, SADB_X_EXT_ADDR_RANGE_SRC_START
, SADB_X_EXT_ADDR_RANGE_SRC_END
,
2606 SADB_X_EXT_ADDR_RANGE_DST_START
, SADB_X_EXT_ADDR_RANGE_DST_END
};
2607 n
= key_gather_mbuf(m
, mhp
, 2, sizeof(mbufItems
) / sizeof(int), mbufItems
);
2609 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2610 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
,
2611 SADB_X_EXT_ADDR_RANGE_SRC_START
, SADB_X_EXT_ADDR_RANGE_SRC_END
,
2612 SADB_X_EXT_ADDR_RANGE_DST_START
, SADB_X_EXT_ADDR_RANGE_DST_END
};
2613 n
= key_gather_mbuf(m
, mhp
, 2, sizeof(mbufItems
) / sizeof(int), mbufItems
);
2616 return key_senderror(so
, m
, ENOBUFS
);
2619 if (n
->m_len
< sizeof(*newmsg
)) {
2620 n
= m_pullup(n
, sizeof(*newmsg
));
2622 return key_senderror(so
, m
, ENOBUFS
);
2625 newmsg
= mtod(n
, struct sadb_msg
*);
2626 newmsg
->sadb_msg_errno
= 0;
2627 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2630 mpolicy
= m_pulldown(n
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)),
2631 sizeof(*xpl
), &off
);
2632 if (mpolicy
== NULL
) {
2633 /* n is already freed */
2634 return key_senderror(so
, m
, ENOBUFS
);
2636 xpl
= (struct sadb_x_policy
*)(void *)(mtod(mpolicy
, caddr_t
) + off
);
2637 if (xpl
->sadb_x_policy_exttype
!= SADB_X_EXT_POLICY
) {
2639 return key_senderror(so
, m
, EINVAL
);
2641 xpl
->sadb_x_policy_id
= newsp
->id
;
2644 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2649 * get new policy id.
2655 key_getnewspid(void)
2657 u_int32_t newid
= 0;
2658 int count
= key_spi_trycnt
; /* XXX */
2659 struct secpolicy
*sp
;
2661 /* when requesting to allocate spi ranged */
2662 lck_mtx_lock(sadb_mutex
);
2664 newid
= (policy_id
= (policy_id
== ~0 ? 1 : policy_id
+ 1));
2666 if ((sp
= __key_getspbyid(newid
)) == NULL
) {
2670 key_freesp(sp
, KEY_SADB_LOCKED
);
2672 lck_mtx_unlock(sadb_mutex
);
2673 if (count
== 0 || newid
== 0) {
2674 ipseclog((LOG_DEBUG
, "key_getnewspid: to allocate policy id is failed.\n"));
2682 * SADB_SPDDELETE processing
2684 * <base, address(SD), policy(*)>
2685 * from the user(?), and set SADB_SASTATE_DEAD,
2687 * <base, address(SD), policy(*)>
2689 * policy(*) including direction of policy.
2691 * m will always be freed.
2697 const struct sadb_msghdr
*mhp
)
2699 struct sadb_address
*src0
, *dst0
, *src1
= NULL
, *dst1
= NULL
;
2700 struct sadb_x_policy
*xpl0
;
2701 struct secpolicyindex spidx
;
2702 struct secpolicy
*sp
;
2703 ifnet_t internal_if
= NULL
;
2704 struct sadb_x_ipsecif
*ipsecifopts
= NULL
;
2705 int use_src_range
= 0;
2706 int use_dst_range
= 0;
2708 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2711 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
2712 panic("key_spddelete: NULL pointer is passed.\n");
2715 if (mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
] != NULL
&& mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
] != NULL
) {
2718 if (mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_START
] != NULL
&& mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_END
] != NULL
) {
2722 if ((!use_src_range
&& mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
) ||
2723 (!use_dst_range
&& mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) ||
2724 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
2725 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
2726 return key_senderror(so
, m
, EINVAL
);
2728 if ((use_src_range
&& (mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_SRC_START
] < sizeof(struct sadb_address
)
2729 || mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_SRC_END
] < sizeof(struct sadb_address
))) ||
2730 (!use_src_range
&& mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
)) ||
2731 (use_dst_range
&& (mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_DST_START
] < sizeof(struct sadb_address
)
2732 || mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_DST_END
] < sizeof(struct sadb_address
))) ||
2733 (!use_dst_range
&& mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) ||
2734 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2735 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
2736 return key_senderror(so
, m
, EINVAL
);
2739 if (use_src_range
) {
2740 src0
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
];
2741 src1
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
];
2743 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
2745 if (use_dst_range
) {
2746 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_START
];
2747 dst1
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_END
];
2749 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
2751 xpl0
= (struct sadb_x_policy
*)(void *)mhp
->ext
[SADB_X_EXT_POLICY
];
2752 ipsecifopts
= (struct sadb_x_ipsecif
*)(void *)mhp
->ext
[SADB_X_EXT_IPSECIF
];
2754 /* checking the direction. */
2755 switch (xpl0
->sadb_x_policy_dir
) {
2756 case IPSEC_DIR_INBOUND
:
2757 case IPSEC_DIR_OUTBOUND
:
2760 ipseclog((LOG_DEBUG
, "key_spddelete: Invalid SP direction.\n"));
2761 return key_senderror(so
, m
, EINVAL
);
2764 /* Process interfaces */
2765 if (ipsecifopts
!= NULL
) {
2766 if (ipsecifopts
->sadb_x_ipsecif_internal_if
[0]) {
2767 ifnet_find_by_name(ipsecifopts
->sadb_x_ipsecif_internal_if
, &internal_if
);
2772 /* XXX boundary check against sa_len */
2773 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2776 src0
->sadb_address_prefixlen
,
2777 dst0
->sadb_address_prefixlen
,
2778 src0
->sadb_address_proto
,
2780 use_src_range
? src0
+ 1 : NULL
,
2781 use_src_range
? src1
+ 1 : NULL
,
2782 use_dst_range
? dst0
+ 1 : NULL
,
2783 use_dst_range
? dst1
+ 1 : NULL
,
2786 /* Is there SP in SPD ? */
2787 lck_mtx_lock(sadb_mutex
);
2788 if ((sp
= key_getsp(&spidx
)) == NULL
) {
2789 ipseclog((LOG_DEBUG
, "key_spddelete: no SP found.\n"));
2790 lck_mtx_unlock(sadb_mutex
);
2792 ifnet_release(internal_if
);
2795 return key_senderror(so
, m
, EINVAL
);
2799 ifnet_release(internal_if
);
2803 /* save policy id to buffer to be returned. */
2804 xpl0
->sadb_x_policy_id
= sp
->id
;
2806 sp
->state
= IPSEC_SPSTATE_DEAD
;
2807 key_freesp(sp
, KEY_SADB_LOCKED
);
2808 lck_mtx_unlock(sadb_mutex
);
2813 struct sadb_msg
*newmsg
;
2814 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2815 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
,
2816 SADB_X_EXT_ADDR_RANGE_SRC_START
, SADB_X_EXT_ADDR_RANGE_SRC_END
,
2817 SADB_X_EXT_ADDR_RANGE_DST_START
, SADB_X_EXT_ADDR_RANGE_DST_END
};
2819 /* create new sadb_msg to reply. */
2820 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
) / sizeof(int), mbufItems
);
2822 return key_senderror(so
, m
, ENOBUFS
);
2825 newmsg
= mtod(n
, struct sadb_msg
*);
2826 newmsg
->sadb_msg_errno
= 0;
2827 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2830 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2835 * SADB_SPDDELETE2 processing
2838 * from the user(?), and set SADB_SASTATE_DEAD,
2842 * policy(*) including direction of policy.
2844 * m will always be freed.
2850 const struct sadb_msghdr
*mhp
)
2853 struct secpolicy
*sp
;
2855 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2858 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
2859 panic("key_spddelete2: NULL pointer is passed.\n");
2862 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2863 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2864 ipseclog((LOG_DEBUG
, "key_spddelete2: invalid message is passed.\n"));
2865 key_senderror(so
, m
, EINVAL
);
2869 id
= ((struct sadb_x_policy
*)
2870 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2872 /* Is there SP in SPD ? */
2873 lck_mtx_lock(sadb_mutex
);
2874 if ((sp
= __key_getspbyid(id
)) == NULL
) {
2875 lck_mtx_unlock(sadb_mutex
);
2876 ipseclog((LOG_DEBUG
, "key_spddelete2: no SP found id:%u.\n", id
));
2877 return key_senderror(so
, m
, EINVAL
);
2880 sp
->state
= IPSEC_SPSTATE_DEAD
;
2881 key_freesp(sp
, KEY_SADB_LOCKED
);
2882 lck_mtx_unlock(sadb_mutex
);
2885 struct mbuf
*n
, *nn
;
2886 struct sadb_msg
*newmsg
;
2889 /* create new sadb_msg to reply. */
2890 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2892 if (len
> MCLBYTES
) {
2893 return key_senderror(so
, m
, ENOBUFS
);
2895 MGETHDR(n
, M_WAITOK
, MT_DATA
);
2896 if (n
&& len
> MHLEN
) {
2897 MCLGET(n
, M_WAITOK
);
2898 if ((n
->m_flags
& M_EXT
) == 0) {
2904 return key_senderror(so
, m
, ENOBUFS
);
2911 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
2912 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2916 panic("length inconsistency in key_spddelete2");
2920 n
->m_next
= m_copym(m
, mhp
->extoff
[SADB_X_EXT_POLICY
],
2921 mhp
->extlen
[SADB_X_EXT_POLICY
], M_WAITOK
);
2924 return key_senderror(so
, m
, ENOBUFS
);
2927 n
->m_pkthdr
.len
= 0;
2928 for (nn
= n
; nn
; nn
= nn
->m_next
) {
2929 n
->m_pkthdr
.len
+= nn
->m_len
;
2932 newmsg
= mtod(n
, struct sadb_msg
*);
2933 newmsg
->sadb_msg_errno
= 0;
2934 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2937 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2945 const struct sadb_msghdr
*mhp
)
2948 struct secpolicy
*sp
;
2950 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2953 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
2954 panic("key_spdenable: NULL pointer is passed.\n");
2957 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2958 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2959 ipseclog((LOG_DEBUG
, "key_spdenable: invalid message is passed.\n"));
2960 key_senderror(so
, m
, EINVAL
);
2964 id
= ((struct sadb_x_policy
*)
2965 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2967 /* Is there SP in SPD ? */
2968 lck_mtx_lock(sadb_mutex
);
2969 if ((sp
= __key_getspbyid(id
)) == NULL
) {
2970 lck_mtx_unlock(sadb_mutex
);
2971 ipseclog((LOG_DEBUG
, "key_spdenable: no SP found id:%u.\n", id
));
2972 return key_senderror(so
, m
, EINVAL
);
2976 lck_mtx_unlock(sadb_mutex
);
2980 struct sadb_msg
*newmsg
;
2981 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
};
2983 /* create new sadb_msg to reply. */
2984 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
) / sizeof(int), mbufItems
);
2986 return key_senderror(so
, m
, ENOBUFS
);
2989 if (n
->m_len
< sizeof(struct sadb_msg
)) {
2990 n
= m_pullup(n
, sizeof(struct sadb_msg
));
2992 return key_senderror(so
, m
, ENOBUFS
);
2995 newmsg
= mtod(n
, struct sadb_msg
*);
2996 newmsg
->sadb_msg_errno
= 0;
2997 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
3000 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
3008 const struct sadb_msghdr
*mhp
)
3011 struct secpolicy
*sp
;
3013 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3016 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
3017 panic("key_spddisable: NULL pointer is passed.\n");
3020 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
3021 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
3022 ipseclog((LOG_DEBUG
, "key_spddisable: invalid message is passed.\n"));
3023 key_senderror(so
, m
, EINVAL
);
3027 id
= ((struct sadb_x_policy
*)
3028 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
3030 /* Is there SP in SPD ? */
3031 lck_mtx_lock(sadb_mutex
);
3032 if ((sp
= __key_getspbyid(id
)) == NULL
) {
3033 lck_mtx_unlock(sadb_mutex
);
3034 ipseclog((LOG_DEBUG
, "key_spddisable: no SP found id:%u.\n", id
));
3035 return key_senderror(so
, m
, EINVAL
);
3039 lck_mtx_unlock(sadb_mutex
);
3043 struct sadb_msg
*newmsg
;
3044 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
};
3046 /* create new sadb_msg to reply. */
3047 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
) / sizeof(int), mbufItems
);
3049 return key_senderror(so
, m
, ENOBUFS
);
3052 if (n
->m_len
< sizeof(struct sadb_msg
)) {
3053 n
= m_pullup(n
, sizeof(struct sadb_msg
));
3055 return key_senderror(so
, m
, ENOBUFS
);
3058 newmsg
= mtod(n
, struct sadb_msg
*);
3059 newmsg
->sadb_msg_errno
= 0;
3060 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
3063 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
3068 * SADB_X_GET processing
3073 * <base, address(SD), policy>
3075 * policy(*) including direction of policy.
3077 * m will always be freed.
3083 const struct sadb_msghdr
*mhp
)
3086 struct secpolicy
*sp
;
3089 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3092 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
3093 panic("key_spdget: NULL pointer is passed.\n");
3096 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
3097 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
3098 ipseclog((LOG_DEBUG
, "key_spdget: invalid message is passed.\n"));
3099 return key_senderror(so
, m
, EINVAL
);
3102 id
= ((struct sadb_x_policy
*)
3103 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
3105 /* Is there SP in SPD ? */
3106 lck_mtx_lock(sadb_mutex
);
3107 if ((sp
= __key_getspbyid(id
)) == NULL
) {
3108 ipseclog((LOG_DEBUG
, "key_spdget: no SP found id:%u.\n", id
));
3109 lck_mtx_unlock(sadb_mutex
);
3110 return key_senderror(so
, m
, ENOENT
);
3112 lck_mtx_unlock(sadb_mutex
);
3113 n
= key_setdumpsp(sp
, SADB_X_SPDGET
, 0, mhp
->msg
->sadb_msg_pid
);
3116 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
3118 return key_senderror(so
, m
, ENOBUFS
);
3123 * SADB_X_SPDACQUIRE processing.
3124 * Acquire policy and SA(s) for a *OUTBOUND* packet.
3127 * to KMD, and expect to receive
3128 * <base> with SADB_X_SPDACQUIRE if error occurred,
3131 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
3132 * policy(*) is without policy requests.
3135 * others: error number
3139 struct secpolicy
*sp
)
3141 struct mbuf
*result
= NULL
, *m
;
3142 struct secspacq
*newspacq
;
3145 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3149 panic("key_spdacquire: NULL pointer is passed.\n");
3151 if (sp
->req
!= NULL
) {
3152 panic("key_spdacquire: called but there is request.\n");
3154 if (sp
->policy
!= IPSEC_POLICY_IPSEC
) {
3155 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
3158 /* get a entry to check whether sent message or not. */
3159 lck_mtx_lock(sadb_mutex
);
3160 if ((newspacq
= key_getspacq(&sp
->spidx
)) != NULL
) {
3161 if (key_blockacq_count
< newspacq
->count
) {
3162 /* reset counter and do send message. */
3163 newspacq
->count
= 0;
3165 /* increment counter and do nothing. */
3167 lck_mtx_unlock(sadb_mutex
);
3171 /* make new entry for blocking to send SADB_ACQUIRE. */
3172 if ((newspacq
= key_newspacq(&sp
->spidx
)) == NULL
) {
3173 lck_mtx_unlock(sadb_mutex
);
3176 /* add to acqtree */
3177 LIST_INSERT_HEAD(&spacqtree
, newspacq
, chain
);
3178 key_start_timehandler();
3180 lck_mtx_unlock(sadb_mutex
);
3181 /* create new sadb_msg to reply. */
3182 m
= key_setsadbmsg(SADB_X_SPDACQUIRE
, 0, 0, 0, 0, 0);
3189 result
->m_pkthdr
.len
= 0;
3190 for (m
= result
; m
; m
= m
->m_next
) {
3191 result
->m_pkthdr
.len
+= m
->m_len
;
3194 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3195 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3197 return key_sendup_mbuf(NULL
, m
, KEY_SENDUP_REGISTERED
);
3207 * SADB_SPDFLUSH processing
3210 * from the user, and free all entries in secpctree.
3214 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
3216 * m will always be freed.
3222 const struct sadb_msghdr
*mhp
)
3224 struct sadb_msg
*newmsg
;
3225 struct secpolicy
*sp
;
3229 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
3230 panic("key_spdflush: NULL pointer is passed.\n");
3233 if (m
->m_len
!= PFKEY_ALIGN8(sizeof(struct sadb_msg
))) {
3234 return key_senderror(so
, m
, EINVAL
);
3237 lck_mtx_lock(sadb_mutex
);
3238 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
3239 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
3240 sp
->state
= IPSEC_SPSTATE_DEAD
;
3243 lck_mtx_unlock(sadb_mutex
);
3245 if (sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
3246 ipseclog((LOG_DEBUG
, "key_spdflush: No more memory.\n"));
3247 return key_senderror(so
, m
, ENOBUFS
);
3254 m
->m_pkthdr
.len
= m
->m_len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
3255 newmsg
= mtod(m
, struct sadb_msg
*);
3256 newmsg
->sadb_msg_errno
= 0;
3257 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
3259 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
3263 * SADB_SPDDUMP processing
3266 * from the user, and dump all SP leaves
3271 * m will always be freed.
3278 const struct sadb_msghdr
*mhp
)
3280 struct secpolicy
*sp
, **spbuf
= NULL
, **sp_ptr
;
3281 int cnt
= 0, bufcount
;
3287 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
3288 panic("key_spddump: NULL pointer is passed.\n");
3291 if ((bufcount
= ipsec_policy_count
) == 0) {
3295 bufcount
+= 256; /* extra */
3296 KMALLOC_WAIT(spbuf
, struct secpolicy
**, bufcount
* sizeof(struct secpolicy
*));
3297 if (spbuf
== NULL
) {
3298 ipseclog((LOG_DEBUG
, "key_spddump: No more memory.\n"));
3302 lck_mtx_lock(sadb_mutex
);
3303 /* search SPD entry, make list. */
3305 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
3306 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
3307 if (cnt
== bufcount
) {
3308 break; /* buffer full */
3315 lck_mtx_unlock(sadb_mutex
);
3325 n
= key_setdumpsp(*sp_ptr
++, SADB_X_SPDDUMP
, cnt
,
3326 mhp
->msg
->sadb_msg_pid
);
3329 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
3333 lck_mtx_lock(sadb_mutex
);
3334 while (sp_ptr
> spbuf
) {
3335 key_freesp(*(--sp_ptr
), KEY_SADB_LOCKED
);
3337 lck_mtx_unlock(sadb_mutex
);
3344 return key_senderror(so
, m
, error
);
3351 static struct mbuf
*
3353 struct secpolicy
*sp
,
3358 struct mbuf
*result
= NULL
, *m
;
3360 m
= key_setsadbmsg(type
, 0, SADB_SATYPE_UNSPEC
, seq
, pid
, sp
->refcnt
);
3366 if (sp
->spidx
.src_range
.start
.ss_len
> 0) {
3367 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_START
,
3368 (struct sockaddr
*)&sp
->spidx
.src_range
.start
, sp
->spidx
.prefs
,
3369 sp
->spidx
.ul_proto
);
3375 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_END
,
3376 (struct sockaddr
*)&sp
->spidx
.src_range
.end
, sp
->spidx
.prefs
,
3377 sp
->spidx
.ul_proto
);
3383 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
3384 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
3385 sp
->spidx
.ul_proto
);
3392 if (sp
->spidx
.dst_range
.start
.ss_len
> 0) {
3393 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_START
,
3394 (struct sockaddr
*)&sp
->spidx
.dst_range
.start
, sp
->spidx
.prefd
,
3395 sp
->spidx
.ul_proto
);
3401 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_END
,
3402 (struct sockaddr
*)&sp
->spidx
.dst_range
.end
, sp
->spidx
.prefd
,
3403 sp
->spidx
.ul_proto
);
3409 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
3410 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
3411 sp
->spidx
.ul_proto
);
3418 if (sp
->spidx
.internal_if
|| sp
->outgoing_if
|| sp
->ipsec_if
|| sp
->disabled
) {
3419 m
= key_setsadbipsecif(sp
->spidx
.internal_if
, sp
->outgoing_if
, sp
->ipsec_if
, sp
->disabled
);
3432 if ((result
->m_flags
& M_PKTHDR
) == 0) {
3436 if (result
->m_len
< sizeof(struct sadb_msg
)) {
3437 result
= m_pullup(result
, sizeof(struct sadb_msg
));
3438 if (result
== NULL
) {
3443 result
->m_pkthdr
.len
= 0;
3444 for (m
= result
; m
; m
= m
->m_next
) {
3445 result
->m_pkthdr
.len
+= m
->m_len
;
3448 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3449 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3459 * get PFKEY message length for security policy and request.
3463 struct secpolicy
*sp
)
3467 tlen
= sizeof(struct sadb_x_policy
);
3469 /* if is the policy for ipsec ? */
3470 if (sp
->policy
!= IPSEC_POLICY_IPSEC
) {
3474 /* get length of ipsec requests */
3476 struct ipsecrequest
*isr
;
3479 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
3480 len
= sizeof(struct sadb_x_ipsecrequest
)
3481 + isr
->saidx
.src
.ss_len
3482 + isr
->saidx
.dst
.ss_len
;
3484 tlen
+= PFKEY_ALIGN8(len
);
3492 * SADB_SPDEXPIRE processing
3494 * <base, address(SD), lifetime(CH), policy>
3498 * others : error number
3502 struct secpolicy
*sp
)
3504 struct mbuf
*result
= NULL
, *m
;
3507 struct sadb_lifetime
*lt
;
3509 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3513 panic("key_spdexpire: NULL pointer is passed.\n");
3516 /* set msg header */
3517 m
= key_setsadbmsg(SADB_X_SPDEXPIRE
, 0, 0, 0, 0, 0);
3524 /* create lifetime extension (current and hard) */
3525 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
3526 m
= key_alloc_mbuf(len
);
3527 if (!m
|| m
->m_next
) { /*XXX*/
3534 bzero(mtod(m
, caddr_t
), len
);
3535 lt
= mtod(m
, struct sadb_lifetime
*);
3536 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3537 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
3538 lt
->sadb_lifetime_allocations
= 0;
3539 lt
->sadb_lifetime_bytes
= 0;
3540 lt
->sadb_lifetime_addtime
= sp
->created
;
3541 lt
->sadb_lifetime_usetime
= sp
->lastused
;
3542 lt
= (struct sadb_lifetime
*)(void *)(mtod(m
, caddr_t
) + len
/ 2);
3543 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3544 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
3545 lt
->sadb_lifetime_allocations
= 0;
3546 lt
->sadb_lifetime_bytes
= 0;
3547 lt
->sadb_lifetime_addtime
= sp
->lifetime
;
3548 lt
->sadb_lifetime_usetime
= sp
->validtime
;
3551 /* set sadb_address(es) for source */
3552 if (sp
->spidx
.src_range
.start
.ss_len
> 0) {
3553 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_START
,
3554 (struct sockaddr
*)&sp
->spidx
.src_range
.start
, sp
->spidx
.prefs
,
3555 sp
->spidx
.ul_proto
);
3562 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_END
,
3563 (struct sockaddr
*)&sp
->spidx
.src_range
.end
, sp
->spidx
.prefs
,
3564 sp
->spidx
.ul_proto
);
3571 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
3572 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
3573 sp
->spidx
.ul_proto
);
3581 /* set sadb_address(es) for dest */
3582 if (sp
->spidx
.dst_range
.start
.ss_len
> 0) {
3583 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_START
,
3584 (struct sockaddr
*)&sp
->spidx
.dst_range
.start
, sp
->spidx
.prefd
,
3585 sp
->spidx
.ul_proto
);
3592 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_END
,
3593 (struct sockaddr
*)&sp
->spidx
.dst_range
.end
, sp
->spidx
.prefd
,
3594 sp
->spidx
.ul_proto
);
3601 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
3602 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
3603 sp
->spidx
.ul_proto
);
3619 if ((result
->m_flags
& M_PKTHDR
) == 0) {
3624 if (result
->m_len
< sizeof(struct sadb_msg
)) {
3625 result
= m_pullup(result
, sizeof(struct sadb_msg
));
3626 if (result
== NULL
) {
3632 result
->m_pkthdr
.len
= 0;
3633 for (m
= result
; m
; m
= m
->m_next
) {
3634 result
->m_pkthdr
.len
+= m
->m_len
;
3637 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3638 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3640 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
3649 /* %%% SAD management */
3651 * allocating a memory for new SA head, and copy from the values of mhp.
3652 * OUT: NULL : failure due to the lack of memory.
3653 * others : pointer to new SA head.
3655 static struct secashead
*
3656 key_newsah(struct secasindex
*saidx
,
3661 struct secashead
*newsah
;
3664 if (saidx
== NULL
) {
3665 panic("key_newsaidx: NULL pointer is passed.\n");
3668 newsah
= keydb_newsecashead();
3669 if (newsah
== NULL
) {
3673 bcopy(saidx
, &newsah
->saidx
, sizeof(newsah
->saidx
));
3675 /* remove the ports */
3676 switch (saidx
->src
.ss_family
) {
3678 ((struct sockaddr_in
*)(&newsah
->saidx
.src
))->sin_port
= IPSEC_PORT_ANY
;
3681 ((struct sockaddr_in6
*)(&newsah
->saidx
.src
))->sin6_port
= IPSEC_PORT_ANY
;
3686 switch (saidx
->dst
.ss_family
) {
3688 ((struct sockaddr_in
*)(&newsah
->saidx
.dst
))->sin_port
= IPSEC_PORT_ANY
;
3691 ((struct sockaddr_in6
*)(&newsah
->saidx
.dst
))->sin6_port
= IPSEC_PORT_ANY
;
3697 newsah
->outgoing_if
= outgoing_if
;
3699 ifnet_reference(ipsec_if
);
3700 newsah
->ipsec_if
= ipsec_if
;
3703 /* add to saidxtree */
3704 newsah
->state
= SADB_SASTATE_MATURE
;
3705 LIST_INSERT_HEAD(&sahtree
, newsah
, chain
);
3706 key_start_timehandler();
3712 * delete SA index and all SA registerd.
3716 struct secashead
*sah
)
3718 struct secasvar
*sav
, *nextsav
;
3719 u_int stateidx
, state
;
3722 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3726 panic("key_delsah: NULL pointer is passed.\n");
3729 /* searching all SA registerd in the secindex. */
3731 stateidx
< _ARRAYLEN(saorder_state_any
);
3733 state
= saorder_state_any
[stateidx
];
3734 for (sav
= (struct secasvar
*)LIST_FIRST(&sah
->savtree
[state
]);
3737 nextsav
= LIST_NEXT(sav
, chain
);
3739 if (sav
->refcnt
> 0) {
3740 /* give up to delete this sa */
3746 KEY_CHKSASTATE(state
, sav
->state
, "key_delsah");
3748 key_freesav(sav
, KEY_SADB_LOCKED
);
3750 /* remove back pointer */
3756 /* don't delete sah only if there are savs. */
3761 ROUTE_RELEASE(&sah
->sa_route
);
3763 if (sah
->ipsec_if
) {
3764 ifnet_release(sah
->ipsec_if
);
3765 sah
->ipsec_if
= NULL
;
3776 /* remove from tree of SA index */
3777 if (__LIST_CHAINED(sah
)) {
3778 LIST_REMOVE(sah
, chain
);
3787 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
3788 * and copy the values of mhp into new buffer.
3789 * When SAD message type is GETSPI:
3790 * to set sequence number from acq_seq++,
3791 * to set zero to SPI.
3792 * not to call key_setsava().
3794 * others : pointer to new secasvar.
3796 * does not modify mbuf. does not free mbuf on error.
3798 static struct secasvar
*
3801 const struct sadb_msghdr
*mhp
,
3802 struct secashead
*sah
,
3806 struct secasvar
*newsav
;
3807 const struct sadb_sa
*xsa
;
3809 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3812 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
|| sah
== NULL
) {
3813 panic("key_newsa: NULL pointer is passed.\n");
3816 KMALLOC_NOWAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3817 if (newsav
== NULL
) {
3818 lck_mtx_unlock(sadb_mutex
);
3819 KMALLOC_WAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3820 lck_mtx_lock(sadb_mutex
);
3821 if (newsav
== NULL
) {
3822 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3827 bzero((caddr_t
)newsav
, sizeof(struct secasvar
));
3829 switch (mhp
->msg
->sadb_msg_type
) {
3831 key_setspi(newsav
, 0);
3833 #if IPSEC_DOSEQCHECK
3834 /* sync sequence number */
3835 if (mhp
->msg
->sadb_msg_seq
== 0) {
3837 (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
3840 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
3845 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
3847 ipseclog((LOG_DEBUG
, "key_newsa: invalid message is passed.\n"));
3851 xsa
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
3852 key_setspi(newsav
, xsa
->sadb_sa_spi
);
3853 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
3861 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
3862 if (((struct sadb_x_sa2
*)(void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_alwaysexpire
) {
3863 newsav
->always_expire
= 1;
3865 newsav
->flags2
= ((struct sadb_x_sa2
*)(void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_flags
;
3866 if (newsav
->flags2
& SADB_X_EXT_SA2_DELETE_ON_DETACH
) {
3871 /* copy sav values */
3872 if (mhp
->msg
->sadb_msg_type
!= SADB_GETSPI
) {
3873 *errp
= key_setsaval(newsav
, m
, mhp
);
3879 /* For get SPI, if has a hard lifetime, apply */
3880 const struct sadb_lifetime
*lft0
;
3883 lft0
= (struct sadb_lifetime
*)(void *)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
3885 /* make lifetime for CURRENT */
3886 KMALLOC_NOWAIT(newsav
->lft_c
, struct sadb_lifetime
*,
3887 sizeof(struct sadb_lifetime
));
3888 if (newsav
->lft_c
== NULL
) {
3889 lck_mtx_unlock(sadb_mutex
);
3890 KMALLOC_WAIT(newsav
->lft_c
, struct sadb_lifetime
*,
3891 sizeof(struct sadb_lifetime
));
3892 lck_mtx_lock(sadb_mutex
);
3893 if (newsav
->lft_c
== NULL
) {
3894 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3903 newsav
->lft_c
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3904 newsav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
3905 newsav
->lft_c
->sadb_lifetime_allocations
= 0;
3906 newsav
->lft_c
->sadb_lifetime_bytes
= 0;
3907 newsav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
3908 newsav
->lft_c
->sadb_lifetime_usetime
= 0;
3910 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
] < sizeof(*lft0
)) {
3911 ipseclog((LOG_DEBUG
, "key_newsa: invalid hard lifetime ext len.\n"));
3916 newsav
->lft_h
= (struct sadb_lifetime
*)key_newbuf(lft0
, sizeof(*lft0
));
3917 if (newsav
->lft_h
== NULL
) {
3918 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3930 newsav
->created
= tv
.tv_sec
;
3933 newsav
->pid
= mhp
->msg
->sadb_msg_pid
;
3938 newsav
->state
= SADB_SASTATE_LARVAL
;
3939 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
3942 ipsec_monitor_sleep_wake();
3948 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
3949 * and copy the values passed into new buffer.
3950 * When SAD message type is GETSPI:
3951 * to set sequence number from acq_seq++,
3952 * to set zero to SPI.
3953 * not to call key_setsava().
3955 * others : pointer to new secasvar.
3958 key_newsav2(struct secashead
*sah
,
3964 struct sadb_key
*key_auth
,
3965 u_int16_t key_auth_len
,
3966 struct sadb_key
*key_enc
,
3967 u_int16_t key_enc_len
,
3968 u_int16_t natt_port
,
3972 struct sadb_lifetime
*lifetime_hard
,
3973 struct sadb_lifetime
*lifetime_soft
)
3975 struct secasvar
*newsav
;
3977 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3981 panic("key_newsa: NULL pointer is passed.\n");
3984 KMALLOC_NOWAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3985 if (newsav
== NULL
) {
3986 lck_mtx_unlock(sadb_mutex
);
3987 KMALLOC_WAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3988 lck_mtx_lock(sadb_mutex
);
3989 if (newsav
== NULL
) {
3990 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3994 bzero((caddr_t
)newsav
, sizeof(struct secasvar
));
3996 #if IPSEC_DOSEQCHECK
3997 /* sync sequence number */
3999 newsav
->seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
4003 key_setspi(newsav
, spi
);
4005 if (key_setsaval2(newsav
,
4029 newsav
->created
= tv
.tv_sec
;
4037 if (spi
&& key_auth
&& key_auth_len
&& key_enc
&& key_enc_len
) {
4038 newsav
->state
= SADB_SASTATE_MATURE
;
4039 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_MATURE
], newsav
,
4042 newsav
->state
= SADB_SASTATE_LARVAL
;
4043 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
4052 key_migratesav(struct secasvar
*sav
,
4053 struct secashead
*newsah
)
4055 if (sav
== NULL
|| newsah
== NULL
|| sav
->state
!= SADB_SASTATE_MATURE
) {
4059 /* remove from SA header */
4060 if (__LIST_CHAINED(sav
)) {
4061 LIST_REMOVE(sav
, chain
);
4065 LIST_INSERT_TAIL(&newsah
->savtree
[SADB_SASTATE_MATURE
], sav
, secasvar
, chain
);
4070 * free() SA variable entry.
4074 struct secasvar
*sav
)
4076 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4080 panic("key_delsav: NULL pointer is passed.\n");
4083 if (sav
->refcnt
> 0) {
4084 return; /* can't free */
4086 /* remove from SA header */
4087 if (__LIST_CHAINED(sav
)) {
4088 LIST_REMOVE(sav
, chain
);
4092 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
) {
4093 LIST_REMOVE(sav
, spihash
);
4096 if (sav
->key_auth
!= NULL
) {
4097 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
4098 KFREE(sav
->key_auth
);
4099 sav
->key_auth
= NULL
;
4101 if (sav
->key_enc
!= NULL
) {
4102 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
4103 KFREE(sav
->key_enc
);
4104 sav
->key_enc
= NULL
;
4107 bzero(sav
->sched
, sav
->schedlen
);
4112 for (int i
= 0; i
< MAX_REPLAY_WINDOWS
; i
++) {
4113 if (sav
->replay
[i
] != NULL
) {
4114 keydb_delsecreplay(sav
->replay
[i
]);
4115 sav
->replay
[i
] = NULL
;
4118 if (sav
->lft_c
!= NULL
) {
4122 if (sav
->lft_h
!= NULL
) {
4126 if (sav
->lft_s
!= NULL
) {
4130 if (sav
->iv
!= NULL
) {
4144 * others : found, pointer to a SA.
4146 static struct secashead
*
4147 key_getsah(struct secasindex
*saidx
)
4149 struct secashead
*sah
;
4151 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4153 LIST_FOREACH(sah
, &sahtree
, chain
) {
4154 if (sah
->state
== SADB_SASTATE_DEAD
) {
4157 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_REQID
)) {
4166 key_newsah2(struct secasindex
*saidx
,
4169 struct secashead
*sah
;
4171 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4173 sah
= key_getsah(saidx
);
4175 return key_newsah(saidx
, NULL
, 0, dir
);
4181 * check not to be duplicated SPI.
4182 * NOTE: this function is too slow due to searching all SAD.
4185 * others : found, pointer to a SA.
4187 static struct secasvar
*
4189 struct secasindex
*saidx
,
4192 struct secasvar
*sav
;
4193 u_int stateidx
, state
;
4195 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4197 /* check address family */
4198 if (saidx
->src
.ss_family
!= saidx
->dst
.ss_family
) {
4199 ipseclog((LOG_DEBUG
, "key_checkspidup: address family mismatched.\n"));
4204 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
4205 if (sav
->spi
!= spi
) {
4209 stateidx
< _ARRAYLEN(saorder_state_alive
);
4211 state
= saorder_state_alive
[stateidx
];
4212 if (sav
->state
== state
&&
4213 key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
)) {
4224 struct secasvar
*sav
,
4227 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4229 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
) {
4230 LIST_REMOVE(sav
, spihash
);
4232 LIST_INSERT_HEAD(&spihash
[SPIHASH(spi
)], sav
, spihash
);
4237 * search SAD litmited alive SA, protocol, SPI.
4240 * others : found, pointer to a SA.
4242 static struct secasvar
*
4244 struct secashead
*sah
,
4247 struct secasvar
*sav
, *match
;
4248 u_int stateidx
, state
, matchidx
;
4250 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4252 matchidx
= _ARRAYLEN(saorder_state_alive
);
4253 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
4254 if (sav
->spi
!= spi
) {
4257 if (sav
->sah
!= sah
) {
4260 for (stateidx
= 0; stateidx
< matchidx
; stateidx
++) {
4261 state
= saorder_state_alive
[stateidx
];
4262 if (sav
->state
== state
) {
4264 matchidx
= stateidx
;
4274 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
4275 * You must update these if need.
4279 * does not modify mbuf. does not free mbuf on error.
4283 struct secasvar
*sav
,
4285 const struct sadb_msghdr
*mhp
)
4288 const struct esp_algorithm
*algo
;
4293 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4296 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
4297 panic("key_setsaval: NULL pointer is passed.\n");
4300 /* initialization */
4301 for (int i
= 0; i
< MAX_REPLAY_WINDOWS
; i
++) {
4302 sav
->replay
[i
] = NULL
;
4304 sav
->key_auth
= NULL
;
4305 sav
->key_enc
= NULL
;
4312 sav
->remote_ike_port
= 0;
4313 sav
->natt_last_activity
= natt_now
;
4314 sav
->natt_encapsulated_src_port
= 0;
4317 if (mhp
->ext
[SADB_EXT_SA
] != NULL
) {
4318 const struct sadb_sa
*sa0
;
4320 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
4321 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(*sa0
)) {
4322 ipseclog((LOG_DEBUG
, "key_setsaval: invalid message size.\n"));
4327 sav
->alg_auth
= sa0
->sadb_sa_auth
;
4328 sav
->alg_enc
= sa0
->sadb_sa_encrypt
;
4329 sav
->flags
= sa0
->sadb_sa_flags
;
4332 * Verify that a nat-traversal port was specified if
4333 * the nat-traversal flag is set.
4335 if ((sav
->flags
& SADB_X_EXT_NATT
) != 0) {
4336 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa_2
) ||
4337 ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
== 0) {
4338 ipseclog((LOG_DEBUG
, "key_setsaval: natt port not set.\n"));
4342 sav
->natt_encapsulated_src_port
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_src_port
;
4343 sav
->remote_ike_port
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
;
4344 sav
->natt_interval
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_interval
;
4345 sav
->natt_offload_interval
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_offload_interval
;
4349 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
4350 * SADB_X_EXT_NATT is set and SADB_X_EXT_NATT_KEEPALIVE is not
4351 * set (we're not behind nat) - otherwise clear it.
4353 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0) {
4354 if ((sav
->flags
& SADB_X_EXT_NATT
) == 0 ||
4355 (sav
->flags
& SADB_X_EXT_NATT_KEEPALIVE
) != 0) {
4356 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
4361 if ((sa0
->sadb_sa_flags
& SADB_X_EXT_OLD
) == 0) {
4362 if ((sav
->flags2
& SADB_X_EXT_SA2_SEQ_PER_TRAFFIC_CLASS
) ==
4363 SADB_X_EXT_SA2_SEQ_PER_TRAFFIC_CLASS
) {
4364 uint32_t range
= (1ULL << (sizeof(((struct secreplay
*)0)->count
) * 8)) / MAX_REPLAY_WINDOWS
;
4365 for (int i
= 0; i
< MAX_REPLAY_WINDOWS
; i
++) {
4366 sav
->replay
[i
] = keydb_newsecreplay(sa0
->sadb_sa_replay
);
4367 if (sav
->replay
[i
] == NULL
) {
4368 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4372 /* Allowed range for sequence per traffic class */
4373 sav
->replay
[i
]->count
= i
* range
;
4374 sav
->replay
[i
]->lastseq
= ((i
+ 1) * range
) - 1;
4377 sav
->replay
[0] = keydb_newsecreplay(sa0
->sadb_sa_replay
);
4378 if (sav
->replay
[0] == NULL
) {
4379 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4383 sav
->replay
[0]->lastseq
= ~0;
4388 /* Authentication keys */
4389 if (mhp
->ext
[SADB_EXT_KEY_AUTH
] != NULL
) {
4390 const struct sadb_key
*key0
;
4393 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_AUTH
];
4394 len
= mhp
->extlen
[SADB_EXT_KEY_AUTH
];
4397 if (len
< sizeof(*key0
)) {
4398 ipseclog((LOG_DEBUG
, "key_setsaval: invalid auth key ext len. len = %d\n", len
));
4402 switch (mhp
->msg
->sadb_msg_satype
) {
4403 case SADB_SATYPE_AH
:
4404 case SADB_SATYPE_ESP
:
4405 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
4406 sav
->alg_auth
!= SADB_X_AALG_NULL
) {
4415 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_auth values.\n"));
4419 sav
->key_auth
= (struct sadb_key
*)key_newbuf(key0
, len
);
4420 if (sav
->key_auth
== NULL
) {
4421 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4427 /* Encryption key */
4428 if (mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] != NULL
) {
4429 const struct sadb_key
*key0
;
4432 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_ENCRYPT
];
4433 len
= mhp
->extlen
[SADB_EXT_KEY_ENCRYPT
];
4436 if (len
< sizeof(*key0
)) {
4437 ipseclog((LOG_DEBUG
, "key_setsaval: invalid encryption key ext len. len = %d\n", len
));
4441 switch (mhp
->msg
->sadb_msg_satype
) {
4442 case SADB_SATYPE_ESP
:
4443 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
4444 sav
->alg_enc
!= SADB_EALG_NULL
) {
4445 ipseclog((LOG_DEBUG
, "key_setsaval: invalid ESP algorithm.\n"));
4449 sav
->key_enc
= (struct sadb_key
*)key_newbuf(key0
, len
);
4450 if (sav
->key_enc
== NULL
) {
4451 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4456 case SADB_SATYPE_AH
:
4462 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_enc value.\n"));
4470 switch (mhp
->msg
->sadb_msg_satype
) {
4471 case SADB_SATYPE_ESP
:
4473 algo
= esp_algorithm_lookup(sav
->alg_enc
);
4474 if (algo
&& algo
->ivlen
) {
4475 sav
->ivlen
= (*algo
->ivlen
)(algo
, sav
);
4477 if (sav
->ivlen
== 0) {
4480 KMALLOC_NOWAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4482 lck_mtx_unlock(sadb_mutex
);
4483 KMALLOC_WAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4484 lck_mtx_lock(sadb_mutex
);
4486 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4493 if (sav
->alg_enc
== SADB_X_EALG_AES_GCM
) {
4494 bzero(sav
->iv
, sav
->ivlen
);
4496 key_randomfill(sav
->iv
, sav
->ivlen
);
4500 case SADB_SATYPE_AH
:
4503 ipseclog((LOG_DEBUG
, "key_setsaval: invalid SA type.\n"));
4510 sav
->created
= tv
.tv_sec
;
4512 /* make lifetime for CURRENT */
4513 KMALLOC_NOWAIT(sav
->lft_c
, struct sadb_lifetime
*,
4514 sizeof(struct sadb_lifetime
));
4515 if (sav
->lft_c
== NULL
) {
4516 lck_mtx_unlock(sadb_mutex
);
4517 KMALLOC_WAIT(sav
->lft_c
, struct sadb_lifetime
*,
4518 sizeof(struct sadb_lifetime
));
4519 lck_mtx_lock(sadb_mutex
);
4520 if (sav
->lft_c
== NULL
) {
4521 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4529 sav
->lft_c
->sadb_lifetime_len
=
4530 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
4531 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
4532 sav
->lft_c
->sadb_lifetime_allocations
= 0;
4533 sav
->lft_c
->sadb_lifetime_bytes
= 0;
4534 sav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
4535 sav
->lft_c
->sadb_lifetime_usetime
= 0;
4537 /* lifetimes for HARD and SOFT */
4539 const struct sadb_lifetime
*lft0
;
4541 lft0
= (struct sadb_lifetime
*)
4542 (void *)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
4544 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
] < sizeof(*lft0
)) {
4545 ipseclog((LOG_DEBUG
, "key_setsaval: invalid hard lifetime ext len.\n"));
4549 sav
->lft_h
= (struct sadb_lifetime
*)key_newbuf(lft0
,
4551 if (sav
->lft_h
== NULL
) {
4552 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4556 /* to be initialize ? */
4559 lft0
= (struct sadb_lifetime
*)
4560 (void *)mhp
->ext
[SADB_EXT_LIFETIME_SOFT
];
4562 if (mhp
->extlen
[SADB_EXT_LIFETIME_SOFT
] < sizeof(*lft0
)) {
4563 ipseclog((LOG_DEBUG
, "key_setsaval: invalid soft lifetime ext len.\n"));
4567 sav
->lft_s
= (struct sadb_lifetime
*)key_newbuf(lft0
,
4569 if (sav
->lft_s
== NULL
) {
4570 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4574 /* to be initialize ? */
4581 /* initialization */
4582 for (int i
= 0; i
< MAX_REPLAY_WINDOWS
; i
++) {
4583 if (sav
->replay
[i
] != NULL
) {
4584 keydb_delsecreplay(sav
->replay
[i
]);
4585 sav
->replay
[i
] = NULL
;
4588 if (sav
->key_auth
!= NULL
) {
4589 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
4590 KFREE(sav
->key_auth
);
4591 sav
->key_auth
= NULL
;
4593 if (sav
->key_enc
!= NULL
) {
4594 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
4595 KFREE(sav
->key_enc
);
4596 sav
->key_enc
= NULL
;
4599 bzero(sav
->sched
, sav
->schedlen
);
4603 if (sav
->iv
!= NULL
) {
4607 if (sav
->lft_c
!= NULL
) {
4611 if (sav
->lft_h
!= NULL
) {
4615 if (sav
->lft_s
!= NULL
) {
4624 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
4625 * You must update these if need.
4629 * does not modify mbuf. does not free mbuf on error.
4632 key_setsaval2(struct secasvar
*sav
,
4638 struct sadb_key
*key_auth
,
4639 u_int16_t key_auth_len
,
4640 struct sadb_key
*key_enc
,
4641 u_int16_t key_enc_len
,
4642 u_int16_t natt_port
,
4646 struct sadb_lifetime
*lifetime_hard
,
4647 struct sadb_lifetime
*lifetime_soft
)
4650 const struct esp_algorithm
*algo
;
4655 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4657 /* initialization */
4658 for (int i
= 0; i
< MAX_REPLAY_WINDOWS
; i
++) {
4659 sav
->replay
[i
] = NULL
;
4662 sav
->key_auth
= NULL
;
4663 sav
->key_enc
= NULL
;
4670 sav
->remote_ike_port
= 0;
4671 sav
->natt_last_activity
= natt_now
;
4672 sav
->natt_encapsulated_src_port
= 0;
4674 sav
->alg_auth
= alg_auth
;
4675 sav
->alg_enc
= alg_enc
;
4679 key_setspi(sav
, htonl(spi
));
4682 * Verify that a nat-traversal port was specified if
4683 * the nat-traversal flag is set.
4685 if ((sav
->flags
& SADB_X_EXT_NATT
) != 0) {
4686 if (natt_port
== 0) {
4687 ipseclog((LOG_DEBUG
, "key_setsaval2: natt port not set.\n"));
4691 sav
->remote_ike_port
= natt_port
;
4695 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
4696 * SADB_X_EXT_NATT is set and SADB_X_EXT_NATT_KEEPALIVE is not
4697 * set (we're not behind nat) - otherwise clear it.
4699 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0) {
4700 if ((sav
->flags
& SADB_X_EXT_NATT
) == 0 ||
4701 (sav
->flags
& SADB_X_EXT_NATT_KEEPALIVE
) != 0) {
4702 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
4707 if ((flags
& SADB_X_EXT_OLD
) == 0) {
4708 if ((sav
->flags2
& SADB_X_EXT_SA2_SEQ_PER_TRAFFIC_CLASS
) ==
4709 SADB_X_EXT_SA2_SEQ_PER_TRAFFIC_CLASS
) {
4710 uint32_t range
= (1ULL << (sizeof(((struct secreplay
*)0)->count
) * 8)) / MAX_REPLAY_WINDOWS
;
4711 for (int i
= 0; i
< MAX_REPLAY_WINDOWS
; i
++) {
4712 sav
->replay
[i
] = keydb_newsecreplay(replay
);
4713 if (sav
->replay
[i
] == NULL
) {
4714 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4718 /* Allowed range for sequence per traffic class */
4719 sav
->replay
[i
]->count
= i
* range
;
4720 sav
->replay
[i
]->lastseq
= ((i
+ 1) * range
) - 1;
4723 sav
->replay
[0] = keydb_newsecreplay(replay
);
4724 if (sav
->replay
[0] == NULL
) {
4725 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4729 sav
->replay
[0]->lastseq
= ~0;
4733 /* Authentication keys */
4734 sav
->key_auth
= (__typeof__(sav
->key_auth
))key_newbuf(key_auth
, key_auth_len
);
4735 if (sav
->key_auth
== NULL
) {
4736 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4741 /* Encryption key */
4742 sav
->key_enc
= (__typeof__(sav
->key_enc
))key_newbuf(key_enc
, key_enc_len
);
4743 if (sav
->key_enc
== NULL
) {
4744 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4752 if (satype
== SADB_SATYPE_ESP
) {
4754 algo
= esp_algorithm_lookup(sav
->alg_enc
);
4755 if (algo
&& algo
->ivlen
) {
4756 sav
->ivlen
= (*algo
->ivlen
)(algo
, sav
);
4758 if (sav
->ivlen
!= 0) {
4759 KMALLOC_NOWAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4761 lck_mtx_unlock(sadb_mutex
);
4762 KMALLOC_WAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4763 lck_mtx_lock(sadb_mutex
);
4765 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4771 if (sav
->alg_enc
== SADB_X_EALG_AES_GCM
) {
4772 bzero(sav
->iv
, sav
->ivlen
);
4774 key_randomfill(sav
->iv
, sav
->ivlen
);
4782 sav
->created
= tv
.tv_sec
;
4784 /* make lifetime for CURRENT */
4785 KMALLOC_NOWAIT(sav
->lft_c
, struct sadb_lifetime
*,
4786 sizeof(struct sadb_lifetime
));
4787 if (sav
->lft_c
== NULL
) {
4788 lck_mtx_unlock(sadb_mutex
);
4789 KMALLOC_WAIT(sav
->lft_c
, struct sadb_lifetime
*,
4790 sizeof(struct sadb_lifetime
));
4791 lck_mtx_lock(sadb_mutex
);
4792 if (sav
->lft_c
== NULL
) {
4793 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4801 sav
->lft_c
->sadb_lifetime_len
=
4802 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
4803 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
4804 sav
->lft_c
->sadb_lifetime_allocations
= 0;
4805 sav
->lft_c
->sadb_lifetime_bytes
= 0;
4806 sav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
4807 sav
->lft_c
->sadb_lifetime_usetime
= 0;
4809 /* lifetimes for HARD and SOFT */
4810 sav
->lft_h
= (__typeof__(sav
->lft_h
))key_newbuf(lifetime_hard
,
4811 sizeof(*lifetime_hard
));
4812 if (sav
->lft_h
== NULL
) {
4813 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4817 sav
->lft_s
= (__typeof__(sav
->lft_s
))key_newbuf(lifetime_soft
,
4818 sizeof(*lifetime_soft
));
4819 if (sav
->lft_s
== NULL
) {
4820 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4828 /* initialization */
4829 for (int i
= 0; i
< MAX_REPLAY_WINDOWS
; i
++) {
4830 if (sav
->replay
[i
] != NULL
) {
4831 keydb_delsecreplay(sav
->replay
[i
]);
4832 sav
->replay
[i
] = NULL
;
4835 if (sav
->key_auth
!= NULL
) {
4836 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
4837 KFREE(sav
->key_auth
);
4838 sav
->key_auth
= NULL
;
4840 if (sav
->key_enc
!= NULL
) {
4841 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
4842 KFREE(sav
->key_enc
);
4843 sav
->key_enc
= NULL
;
4846 bzero(sav
->sched
, sav
->schedlen
);
4850 if (sav
->iv
!= NULL
) {
4854 if (sav
->lft_c
!= NULL
) {
4858 if (sav
->lft_h
!= NULL
) {
4862 if (sav
->lft_s
!= NULL
) {
4871 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
4877 struct secasvar
*sav
)
4880 int checkmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
4881 int mustmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
4885 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4887 /* check SPI value */
4888 switch (sav
->sah
->saidx
.proto
) {
4892 /* No reason to test if this is >= 0, because ntohl(sav->spi) is unsigned. */
4893 if (ntohl(sav
->spi
) <= 255) {
4894 ipseclog((LOG_DEBUG
,
4895 "key_mature: illegal range of SPI %u.\n",
4896 (u_int32_t
)ntohl(sav
->spi
)));
4903 switch (sav
->sah
->saidx
.proto
) {
4906 if ((sav
->flags
& SADB_X_EXT_OLD
)
4907 && (sav
->flags
& SADB_X_EXT_DERIV
)) {
4908 ipseclog((LOG_DEBUG
, "key_mature: "
4909 "invalid flag (derived) given to old-esp.\n"));
4912 if (sav
->alg_auth
== SADB_AALG_NONE
) {
4921 if (sav
->flags
& SADB_X_EXT_DERIV
) {
4922 ipseclog((LOG_DEBUG
, "key_mature: "
4923 "invalid flag (derived) given to AH SA.\n"));
4926 if (sav
->alg_enc
!= SADB_EALG_NONE
) {
4927 ipseclog((LOG_DEBUG
, "key_mature: "
4928 "protocol and algorithm mismated.\n"));
4935 ipseclog((LOG_DEBUG
, "key_mature: Invalid satype.\n"));
4936 return EPROTONOSUPPORT
;
4939 /* check authentication algorithm */
4940 if ((checkmask
& 2) != 0) {
4941 const struct ah_algorithm
*algo
;
4944 algo
= ah_algorithm_lookup(sav
->alg_auth
);
4946 ipseclog((LOG_DEBUG
, "key_mature: "
4947 "unknown authentication algorithm.\n"));
4951 /* algorithm-dependent check */
4952 if (sav
->key_auth
) {
4953 keylen
= sav
->key_auth
->sadb_key_bits
;
4957 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
4958 ipseclog((LOG_DEBUG
,
4959 "key_mature: invalid AH key length %d "
4960 "(%d-%d allowed)\n",
4961 keylen
, algo
->keymin
, algo
->keymax
));
4966 if ((*algo
->mature
)(sav
)) {
4967 /* message generated in per-algorithm function*/
4970 mature
= SADB_SATYPE_AH
;
4974 if ((mustmask
& 2) != 0 && mature
!= SADB_SATYPE_AH
) {
4975 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for AH\n"));
4980 /* check encryption algorithm */
4981 if ((checkmask
& 1) != 0) {
4983 const struct esp_algorithm
*algo
;
4986 algo
= esp_algorithm_lookup(sav
->alg_enc
);
4988 ipseclog((LOG_DEBUG
, "key_mature: unknown encryption algorithm.\n"));
4992 /* algorithm-dependent check */
4994 keylen
= sav
->key_enc
->sadb_key_bits
;
4998 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
4999 ipseclog((LOG_DEBUG
,
5000 "key_mature: invalid ESP key length %d "
5001 "(%d-%d allowed)\n",
5002 keylen
, algo
->keymin
, algo
->keymax
));
5007 if ((*algo
->mature
)(sav
)) {
5008 /* message generated in per-algorithm function*/
5011 mature
= SADB_SATYPE_ESP
;
5015 if ((mustmask
& 1) != 0 && mature
!= SADB_SATYPE_ESP
) {
5016 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for ESP\n"));
5020 ipseclog((LOG_DEBUG
, "key_mature: ESP not supported in this configuration\n"));
5025 key_sa_chgstate(sav
, SADB_SASTATE_MATURE
);
5031 * subroutine for SADB_GET and SADB_DUMP.
5033 static struct mbuf
*
5035 struct secasvar
*sav
,
5041 struct mbuf
*result
= NULL
, *tres
= NULL
, *m
;
5046 SADB_EXT_SA
, SADB_X_EXT_SA2
,
5047 SADB_EXT_LIFETIME_HARD
, SADB_EXT_LIFETIME_SOFT
,
5048 SADB_EXT_LIFETIME_CURRENT
, SADB_EXT_ADDRESS_SRC
,
5049 SADB_EXT_ADDRESS_DST
, SADB_EXT_ADDRESS_PROXY
, SADB_EXT_KEY_AUTH
,
5050 SADB_EXT_KEY_ENCRYPT
, SADB_EXT_IDENTITY_SRC
,
5051 SADB_EXT_IDENTITY_DST
, SADB_EXT_SENSITIVITY
,
5054 m
= key_setsadbmsg(type
, 0, satype
, seq
, pid
, sav
->refcnt
);
5060 for (i
= sizeof(dumporder
) / sizeof(dumporder
[0]) - 1; i
>= 0; i
--) {
5063 switch (dumporder
[i
]) {
5065 m
= key_setsadbsa(sav
);
5071 case SADB_X_EXT_SA2
:
5072 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
5073 sav
->replay
[0] ? sav
->replay
[0]->count
: 0,
5074 sav
->sah
->saidx
.reqid
,
5081 case SADB_EXT_ADDRESS_SRC
:
5082 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
5083 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
5084 FULLMASK
, IPSEC_ULPROTO_ANY
);
5090 case SADB_EXT_ADDRESS_DST
:
5091 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
5092 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
5093 FULLMASK
, IPSEC_ULPROTO_ANY
);
5099 case SADB_EXT_KEY_AUTH
:
5100 if (!sav
->key_auth
) {
5103 l
= PFKEY_UNUNIT64(sav
->key_auth
->sadb_key_len
);
5107 case SADB_EXT_KEY_ENCRYPT
:
5108 if (!sav
->key_enc
) {
5111 l
= PFKEY_UNUNIT64(sav
->key_enc
->sadb_key_len
);
5115 case SADB_EXT_LIFETIME_CURRENT
:
5119 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_c
)->sadb_ext_len
);
5123 case SADB_EXT_LIFETIME_HARD
:
5127 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_h
)->sadb_ext_len
);
5131 case SADB_EXT_LIFETIME_SOFT
:
5135 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_s
)->sadb_ext_len
);
5139 case SADB_EXT_ADDRESS_PROXY
:
5140 case SADB_EXT_IDENTITY_SRC
:
5141 case SADB_EXT_IDENTITY_DST
:
5142 /* XXX: should we brought from SPD ? */
5143 case SADB_EXT_SENSITIVITY
:
5148 if ((!m
&& !p
) || (m
&& p
)) {
5152 M_PREPEND(tres
, l
, M_WAITOK
, 1);
5156 bcopy(p
, mtod(tres
, caddr_t
), l
);
5160 m
= key_alloc_mbuf(l
);
5164 m_copyback(m
, 0, l
, p
);
5173 m_cat(result
, tres
);
5175 if (sav
->sah
&& (sav
->sah
->outgoing_if
|| sav
->sah
->ipsec_if
)) {
5176 m
= key_setsadbipsecif(NULL
, ifindex2ifnet
[sav
->sah
->outgoing_if
], sav
->sah
->ipsec_if
, 0);
5183 if (result
->m_len
< sizeof(struct sadb_msg
)) {
5184 result
= m_pullup(result
, sizeof(struct sadb_msg
));
5185 if (result
== NULL
) {
5190 result
->m_pkthdr
.len
= 0;
5191 for (m
= result
; m
; m
= m
->m_next
) {
5192 result
->m_pkthdr
.len
+= m
->m_len
;
5195 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
5196 PFKEY_UNIT64(result
->m_pkthdr
.len
);
5207 * set data into sadb_msg.
5209 static struct mbuf
*
5222 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
5223 if (len
> MCLBYTES
) {
5226 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
5227 if (m
&& len
> MHLEN
) {
5228 MCLGET(m
, M_DONTWAIT
);
5229 if ((m
->m_flags
& M_EXT
) == 0) {
5237 m
->m_pkthdr
.len
= m
->m_len
= len
;
5240 p
= mtod(m
, struct sadb_msg
*);
5243 p
->sadb_msg_version
= PF_KEY_V2
;
5244 p
->sadb_msg_type
= type
;
5245 p
->sadb_msg_errno
= 0;
5246 p
->sadb_msg_satype
= satype
;
5247 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
5248 p
->sadb_msg_reserved
= reserved
;
5249 p
->sadb_msg_seq
= seq
;
5250 p
->sadb_msg_pid
= (u_int32_t
)pid
;
5256 * copy secasvar data into sadb_address.
5258 static struct mbuf
*
5260 struct secasvar
*sav
)
5266 len
= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
5267 m
= key_alloc_mbuf(len
);
5268 if (!m
|| m
->m_next
) { /*XXX*/
5275 p
= mtod(m
, struct sadb_sa
*);
5278 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
5279 p
->sadb_sa_exttype
= SADB_EXT_SA
;
5280 p
->sadb_sa_spi
= sav
->spi
;
5281 p
->sadb_sa_replay
= (sav
->replay
[0] != NULL
? sav
->replay
[0]->wsize
: 0);
5282 p
->sadb_sa_state
= sav
->state
;
5283 p
->sadb_sa_auth
= sav
->alg_auth
;
5284 p
->sadb_sa_encrypt
= sav
->alg_enc
;
5285 p
->sadb_sa_flags
= sav
->flags
;
5291 * set data into sadb_address.
5293 static struct mbuf
*
5296 struct sockaddr
*saddr
,
5301 struct sadb_address
*p
;
5304 len
= PFKEY_ALIGN8(sizeof(struct sadb_address
)) +
5305 PFKEY_ALIGN8(saddr
->sa_len
);
5306 m
= key_alloc_mbuf(len
);
5307 if (!m
|| m
->m_next
) { /*XXX*/
5314 p
= mtod(m
, struct sadb_address
*);
5317 p
->sadb_address_len
= PFKEY_UNIT64(len
);
5318 p
->sadb_address_exttype
= exttype
;
5319 p
->sadb_address_proto
= ul_proto
;
5320 if (prefixlen
== FULLMASK
) {
5321 switch (saddr
->sa_family
) {
5323 prefixlen
= sizeof(struct in_addr
) << 3;
5326 prefixlen
= sizeof(struct in6_addr
) << 3;
5332 p
->sadb_address_prefixlen
= prefixlen
;
5333 p
->sadb_address_reserved
= 0;
5336 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_address
)),
5342 static struct mbuf
*
5343 key_setsadbipsecif(ifnet_t internal_if
,
5344 ifnet_t outgoing_if
,
5349 struct sadb_x_ipsecif
*p
;
5352 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_ipsecif
));
5353 m
= key_alloc_mbuf(len
);
5354 if (!m
|| m
->m_next
) { /*XXX*/
5361 p
= mtod(m
, struct sadb_x_ipsecif
*);
5364 p
->sadb_x_ipsecif_len
= PFKEY_UNIT64(len
);
5365 p
->sadb_x_ipsecif_exttype
= SADB_X_EXT_IPSECIF
;
5367 if (internal_if
&& internal_if
->if_xname
) {
5368 strlcpy(p
->sadb_x_ipsecif_internal_if
, internal_if
->if_xname
, IFXNAMSIZ
);
5370 if (outgoing_if
&& outgoing_if
->if_xname
) {
5371 strlcpy(p
->sadb_x_ipsecif_outgoing_if
, outgoing_if
->if_xname
, IFXNAMSIZ
);
5373 if (ipsec_if
&& ipsec_if
->if_xname
) {
5374 strlcpy(p
->sadb_x_ipsecif_ipsec_if
, ipsec_if
->if_xname
, IFXNAMSIZ
);
5377 p
->sadb_x_ipsecif_init_disabled
= init_disabled
;
5383 * set data into sadb_session_id
5385 static struct mbuf
*
5386 key_setsadbsession_id(u_int64_t session_ids
[])
5389 struct sadb_session_id
*p
;
5392 len
= PFKEY_ALIGN8(sizeof(*p
));
5393 m
= key_alloc_mbuf(len
);
5394 if (!m
|| m
->m_next
) { /*XXX*/
5401 p
= mtod(m
, __typeof__(p
));
5404 p
->sadb_session_id_len
= PFKEY_UNIT64(len
);
5405 p
->sadb_session_id_exttype
= SADB_EXT_SESSION_ID
;
5406 p
->sadb_session_id_v
[0] = session_ids
[0];
5407 p
->sadb_session_id_v
[1] = session_ids
[1];
5413 * copy stats data into sadb_sastat type.
5415 static struct mbuf
*
5416 key_setsadbsastat(u_int32_t dir
,
5417 struct sastat
*stats
,
5418 u_int32_t max_stats
)
5421 struct sadb_sastat
*p
;
5428 list_len
= sizeof(*stats
) * max_stats
;
5429 len
= PFKEY_ALIGN8(sizeof(*p
)) + PFKEY_ALIGN8(list_len
);
5430 m
= key_alloc_mbuf(len
);
5431 if (!m
|| m
->m_next
) { /*XXX*/
5438 p
= mtod(m
, __typeof__(p
));
5441 p
->sadb_sastat_len
= PFKEY_UNIT64(len
);
5442 p
->sadb_sastat_exttype
= SADB_EXT_SASTAT
;
5443 p
->sadb_sastat_dir
= dir
;
5444 p
->sadb_sastat_list_len
= max_stats
;
5447 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(*p
)),
5456 * set data into sadb_ident.
5458 static struct mbuf
*
5467 struct sadb_ident
*p
;
5470 len
= PFKEY_ALIGN8(sizeof(struct sadb_ident
)) + PFKEY_ALIGN8(stringlen
);
5471 m
= key_alloc_mbuf(len
);
5472 if (!m
|| m
->m_next
) { /*XXX*/
5479 p
= mtod(m
, struct sadb_ident
*);
5482 p
->sadb_ident_len
= PFKEY_UNIT64(len
);
5483 p
->sadb_ident_exttype
= exttype
;
5484 p
->sadb_ident_type
= idtype
;
5485 p
->sadb_ident_reserved
= 0;
5486 p
->sadb_ident_id
= id
;
5489 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_ident
)),
5497 * set data into sadb_x_sa2.
5499 static struct mbuf
*
5507 struct sadb_x_sa2
*p
;
5510 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_sa2
));
5511 m
= key_alloc_mbuf(len
);
5512 if (!m
|| m
->m_next
) { /*XXX*/
5519 p
= mtod(m
, struct sadb_x_sa2
*);
5522 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
5523 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
5524 p
->sadb_x_sa2_mode
= mode
;
5525 p
->sadb_x_sa2_reserved1
= 0;
5526 p
->sadb_x_sa2_reserved2
= 0;
5527 p
->sadb_x_sa2_sequence
= seq
;
5528 p
->sadb_x_sa2_reqid
= reqid
;
5529 p
->sadb_x_sa2_flags
= flags
;
5535 * set data into sadb_x_policy
5537 static struct mbuf
*
5544 struct sadb_x_policy
*p
;
5547 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_policy
));
5548 m
= key_alloc_mbuf(len
);
5549 if (!m
|| m
->m_next
) { /*XXX*/
5556 p
= mtod(m
, struct sadb_x_policy
*);
5559 p
->sadb_x_policy_len
= PFKEY_UNIT64(len
);
5560 p
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
5561 p
->sadb_x_policy_type
= type
;
5562 p
->sadb_x_policy_dir
= dir
;
5563 p
->sadb_x_policy_id
= id
;
5570 * copy a buffer into the new buffer allocated.
5579 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5580 KMALLOC_NOWAIT(new, caddr_t
, len
);
5582 lck_mtx_unlock(sadb_mutex
);
5583 KMALLOC_WAIT(new, caddr_t
, len
);
5584 lck_mtx_lock(sadb_mutex
);
5586 ipseclog((LOG_DEBUG
, "key_newbuf: No more memory.\n"));
5590 bcopy(src
, new, len
);
5595 /* compare my own address
5596 * OUT: 1: true, i.e. my address.
5601 struct sockaddr
*sa
)
5604 struct sockaddr_in
*sin
;
5605 struct in_ifaddr
*ia
;
5610 panic("key_ismyaddr: NULL pointer is passed.\n");
5613 switch (sa
->sa_family
) {
5616 lck_rw_lock_shared(in_ifaddr_rwlock
);
5617 sin
= (struct sockaddr_in
*)(void *)sa
;
5618 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
5619 ia
= ia
->ia_link
.tqe_next
) {
5620 IFA_LOCK_SPIN(&ia
->ia_ifa
);
5621 if (sin
->sin_family
== ia
->ia_addr
.sin_family
&&
5622 sin
->sin_len
== ia
->ia_addr
.sin_len
&&
5623 sin
->sin_addr
.s_addr
== ia
->ia_addr
.sin_addr
.s_addr
) {
5624 IFA_UNLOCK(&ia
->ia_ifa
);
5625 lck_rw_done(in_ifaddr_rwlock
);
5628 IFA_UNLOCK(&ia
->ia_ifa
);
5630 lck_rw_done(in_ifaddr_rwlock
);
5635 return key_ismyaddr6((struct sockaddr_in6
*)(void *)sa
);
5644 * compare my own address for IPv6.
5647 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
5649 #include <netinet6/in6_var.h>
5653 struct sockaddr_in6
*sin6
)
5655 struct in6_ifaddr
*ia
;
5656 struct in6_multi
*in6m
;
5658 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
5659 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
5660 IFA_LOCK(&ia
->ia_ifa
);
5661 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
5662 (struct sockaddr
*)&ia
->ia_addr
, 0) == 0) {
5663 IFA_UNLOCK(&ia
->ia_ifa
);
5664 lck_rw_done(&in6_ifaddr_rwlock
);
5667 IFA_UNLOCK(&ia
->ia_ifa
);
5671 * XXX why do we care about multlicast here while we don't care
5672 * about IPv4 multicast??
5676 in6_multihead_lock_shared();
5677 IN6_LOOKUP_MULTI(&sin6
->sin6_addr
, ia
->ia_ifp
, in6m
);
5678 in6_multihead_lock_done();
5680 lck_rw_done(&in6_ifaddr_rwlock
);
5685 lck_rw_done(&in6_ifaddr_rwlock
);
5687 /* loopback, just for safety */
5688 if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
)) {
5697 * compare two secasindex structure.
5698 * flag can specify to compare 2 saidxes.
5699 * compare two secasindex structure without both mode and reqid.
5700 * don't compare port.
5702 * saidx0: source, it can be in SAD.
5710 struct secasindex
*saidx0
,
5711 struct secasindex
*saidx1
,
5715 if (saidx0
== NULL
&& saidx1
== NULL
) {
5719 if (saidx0
== NULL
|| saidx1
== NULL
) {
5723 if (saidx0
->ipsec_ifindex
!= 0 && saidx0
->ipsec_ifindex
!= saidx1
->ipsec_ifindex
) {
5727 if (saidx0
->proto
!= saidx1
->proto
) {
5731 if (flag
== CMP_EXACTLY
) {
5732 if (saidx0
->mode
!= saidx1
->mode
) {
5735 if (saidx0
->reqid
!= saidx1
->reqid
) {
5738 if (bcmp(&saidx0
->src
, &saidx1
->src
, saidx0
->src
.ss_len
) != 0 ||
5739 bcmp(&saidx0
->dst
, &saidx1
->dst
, saidx0
->dst
.ss_len
) != 0) {
5743 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
5744 if (flag
& CMP_REQID
) {
5746 * If reqid of SPD is non-zero, unique SA is required.
5747 * The result must be of same reqid in this case.
5749 if (saidx1
->reqid
!= 0 && saidx0
->reqid
!= saidx1
->reqid
) {
5754 if (flag
& CMP_MODE
) {
5755 if (saidx0
->mode
!= IPSEC_MODE_ANY
5756 && saidx0
->mode
!= saidx1
->mode
) {
5761 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->src
,
5762 (struct sockaddr
*)&saidx1
->src
, flag
& CMP_PORT
? 1 : 0) != 0) {
5765 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->dst
,
5766 (struct sockaddr
*)&saidx1
->dst
, flag
& CMP_PORT
? 1 : 0) != 0) {
5775 * compare two secindex structure exactly.
5777 * spidx0: source, it is often in SPD.
5778 * spidx1: object, it is often from PFKEY message.
5784 key_cmpspidx_exactly(
5785 struct secpolicyindex
*spidx0
,
5786 struct secpolicyindex
*spidx1
)
5789 if (spidx0
== NULL
&& spidx1
== NULL
) {
5793 if (spidx0
== NULL
|| spidx1
== NULL
) {
5797 if (spidx0
->prefs
!= spidx1
->prefs
5798 || spidx0
->prefd
!= spidx1
->prefd
5799 || spidx0
->ul_proto
!= spidx1
->ul_proto
5800 || spidx0
->internal_if
!= spidx1
->internal_if
) {
5804 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src
,
5805 (struct sockaddr
*)&spidx1
->src
, 1) != 0) {
5808 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst
,
5809 (struct sockaddr
*)&spidx1
->dst
, 1) != 0) {
5813 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src_range
.start
,
5814 (struct sockaddr
*)&spidx1
->src_range
.start
, 1) != 0) {
5817 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src_range
.end
,
5818 (struct sockaddr
*)&spidx1
->src_range
.end
, 1) != 0) {
5821 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst_range
.start
,
5822 (struct sockaddr
*)&spidx1
->dst_range
.start
, 1) != 0) {
5825 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst_range
.end
,
5826 (struct sockaddr
*)&spidx1
->dst_range
.end
, 1) != 0) {
5834 * compare two secindex structure with mask.
5836 * spidx0: source, it is often in SPD.
5837 * spidx1: object, it is often from IP header.
5843 key_cmpspidx_withmask(
5844 struct secpolicyindex
*spidx0
,
5845 struct secpolicyindex
*spidx1
)
5847 int spidx0_src_is_range
= 0;
5848 int spidx0_dst_is_range
= 0;
5851 if (spidx0
== NULL
&& spidx1
== NULL
) {
5855 if (spidx0
== NULL
|| spidx1
== NULL
) {
5859 if (spidx0
->src_range
.start
.ss_len
> 0) {
5860 spidx0_src_is_range
= 1;
5863 if (spidx0
->dst_range
.start
.ss_len
> 0) {
5864 spidx0_dst_is_range
= 1;
5867 if ((spidx0_src_is_range
? spidx0
->src_range
.start
.ss_family
: spidx0
->src
.ss_family
) != spidx1
->src
.ss_family
||
5868 (spidx0_dst_is_range
? spidx0
->dst_range
.start
.ss_family
: spidx0
->dst
.ss_family
) != spidx1
->dst
.ss_family
||
5869 (spidx0_src_is_range
? spidx0
->src_range
.start
.ss_len
: spidx0
->src
.ss_len
) != spidx1
->src
.ss_len
||
5870 (spidx0_dst_is_range
? spidx0
->dst_range
.start
.ss_len
: spidx0
->dst
.ss_len
) != spidx1
->dst
.ss_len
) {
5874 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
5875 if (spidx0
->ul_proto
!= (u_int16_t
)IPSEC_ULPROTO_ANY
5876 && spidx0
->ul_proto
!= spidx1
->ul_proto
) {
5880 /* If spidx1 specifies interface, ignore src addr */
5881 if (spidx1
->internal_if
!= NULL
) {
5882 if (spidx0
->internal_if
== NULL
5883 || spidx0
->internal_if
!= spidx1
->internal_if
) {
5887 /* Still check ports */
5888 switch (spidx0
->src
.ss_family
) {
5890 if (spidx0_src_is_range
&&
5891 (satosin(&spidx1
->src
)->sin_port
< satosin(&spidx0
->src_range
.start
)->sin_port
5892 || satosin(&spidx1
->src
)->sin_port
> satosin(&spidx0
->src_range
.end
)->sin_port
)) {
5894 } else if (satosin(&spidx0
->src
)->sin_port
!= IPSEC_PORT_ANY
5895 && satosin(&spidx0
->src
)->sin_port
!=
5896 satosin(&spidx1
->src
)->sin_port
) {
5901 if (spidx0_src_is_range
&&
5902 (satosin6(&spidx1
->src
)->sin6_port
< satosin6(&spidx0
->src_range
.start
)->sin6_port
5903 || satosin6(&spidx1
->src
)->sin6_port
> satosin6(&spidx0
->src_range
.end
)->sin6_port
)) {
5905 } else if (satosin6(&spidx0
->src
)->sin6_port
!= IPSEC_PORT_ANY
5906 && satosin6(&spidx0
->src
)->sin6_port
!=
5907 satosin6(&spidx1
->src
)->sin6_port
) {
5914 } else if (spidx0_src_is_range
) {
5915 if (!key_is_addr_in_range(&spidx1
->src
, &spidx0
->src_range
)) {
5919 switch (spidx0
->src
.ss_family
) {
5921 if (satosin(&spidx0
->src
)->sin_port
!= IPSEC_PORT_ANY
5922 && satosin(&spidx0
->src
)->sin_port
!=
5923 satosin(&spidx1
->src
)->sin_port
) {
5926 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->src
)->sin_addr
,
5927 (caddr_t
)&satosin(&spidx1
->src
)->sin_addr
, spidx0
->prefs
)) {
5932 if (satosin6(&spidx0
->src
)->sin6_port
!= IPSEC_PORT_ANY
5933 && satosin6(&spidx0
->src
)->sin6_port
!=
5934 satosin6(&spidx1
->src
)->sin6_port
) {
5938 * scope_id check. if sin6_scope_id is 0, we regard it
5939 * as a wildcard scope, which matches any scope zone ID.
5941 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
5942 satosin6(&spidx1
->src
)->sin6_scope_id
&&
5943 satosin6(&spidx0
->src
)->sin6_scope_id
!=
5944 satosin6(&spidx1
->src
)->sin6_scope_id
) {
5947 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->src
)->sin6_addr
,
5948 (caddr_t
)&satosin6(&spidx1
->src
)->sin6_addr
, spidx0
->prefs
)) {
5954 if (bcmp(&spidx0
->src
, &spidx1
->src
, spidx0
->src
.ss_len
) != 0) {
5961 if (spidx0_dst_is_range
) {
5962 if (!key_is_addr_in_range(&spidx1
->dst
, &spidx0
->dst_range
)) {
5966 switch (spidx0
->dst
.ss_family
) {
5968 if (satosin(&spidx0
->dst
)->sin_port
!= IPSEC_PORT_ANY
5969 && satosin(&spidx0
->dst
)->sin_port
!=
5970 satosin(&spidx1
->dst
)->sin_port
) {
5973 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->dst
)->sin_addr
,
5974 (caddr_t
)&satosin(&spidx1
->dst
)->sin_addr
, spidx0
->prefd
)) {
5979 if (satosin6(&spidx0
->dst
)->sin6_port
!= IPSEC_PORT_ANY
5980 && satosin6(&spidx0
->dst
)->sin6_port
!=
5981 satosin6(&spidx1
->dst
)->sin6_port
) {
5985 * scope_id check. if sin6_scope_id is 0, we regard it
5986 * as a wildcard scope, which matches any scope zone ID.
5988 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
5989 satosin6(&spidx1
->src
)->sin6_scope_id
&&
5990 satosin6(&spidx0
->dst
)->sin6_scope_id
!=
5991 satosin6(&spidx1
->dst
)->sin6_scope_id
) {
5994 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->dst
)->sin6_addr
,
5995 (caddr_t
)&satosin6(&spidx1
->dst
)->sin6_addr
, spidx0
->prefd
)) {
6001 if (bcmp(&spidx0
->dst
, &spidx1
->dst
, spidx0
->dst
.ss_len
) != 0) {
6008 /* XXX Do we check other field ? e.g. flowinfo */
6014 key_is_addr_in_range(struct sockaddr_storage
*addr
, struct secpolicyaddrrange
*addr_range
)
6018 if (addr
== NULL
|| addr_range
== NULL
) {
6022 /* Must be greater than or equal to start */
6023 cmp
= key_sockaddrcmp((struct sockaddr
*)addr
, (struct sockaddr
*)&addr_range
->start
, 1);
6024 if (cmp
!= 0 && cmp
!= 1) {
6028 /* Must be less than or equal to end */
6029 cmp
= key_sockaddrcmp((struct sockaddr
*)addr
, (struct sockaddr
*)&addr_range
->end
, 1);
6030 if (cmp
!= 0 && cmp
!= -1) {
6042 * 2: Not comparable or error
6046 struct sockaddr
*sa1
,
6047 struct sockaddr
*sa2
,
6051 int port_result
= 0;
6053 if (sa1
->sa_family
!= sa2
->sa_family
|| sa1
->sa_len
!= sa2
->sa_len
) {
6057 if (sa1
->sa_len
== 0) {
6061 switch (sa1
->sa_family
) {
6063 if (sa1
->sa_len
!= sizeof(struct sockaddr_in
)) {
6067 result
= memcmp(&satosin(sa1
)->sin_addr
.s_addr
, &satosin(sa2
)->sin_addr
.s_addr
, sizeof(satosin(sa1
)->sin_addr
.s_addr
));
6070 if (satosin(sa1
)->sin_port
< satosin(sa2
)->sin_port
) {
6072 } else if (satosin(sa1
)->sin_port
> satosin(sa2
)->sin_port
) {
6077 result
= port_result
;
6078 } else if ((result
> 0 && port_result
< 0) || (result
< 0 && port_result
> 0)) {
6085 if (sa1
->sa_len
!= sizeof(struct sockaddr_in6
)) {
6086 return 2; /*EINVAL*/
6088 if (satosin6(sa1
)->sin6_scope_id
!=
6089 satosin6(sa2
)->sin6_scope_id
) {
6093 result
= memcmp(&satosin6(sa1
)->sin6_addr
.s6_addr
[0], &satosin6(sa2
)->sin6_addr
.s6_addr
[0], sizeof(struct in6_addr
));
6096 if (satosin6(sa1
)->sin6_port
< satosin6(sa2
)->sin6_port
) {
6098 } else if (satosin6(sa1
)->sin6_port
> satosin6(sa2
)->sin6_port
) {
6103 result
= port_result
;
6104 } else if ((result
> 0 && port_result
< 0) || (result
< 0 && port_result
> 0)) {
6111 result
= memcmp(sa1
, sa2
, sa1
->sa_len
);
6117 } else if (result
> 0) {
6125 * compare two buffers with mask.
6129 * bits: Number of bits to compare
6142 /* XXX: This could be considerably faster if we compare a word
6143 * at a time, but it is complicated on LSB Endian machines */
6145 /* Handle null pointers */
6146 if (p1
== NULL
|| p2
== NULL
) {
6151 if (*p1
++ != *p2
++) {
6158 mask
= ~((1 << (8 - bits
)) - 1);
6159 if ((*p1
& mask
) != (*p2
& mask
)) {
6163 return 1; /* Match! */
6168 * scanning SPD and SAD to check status for each entries,
6169 * and do to remove or to expire.
6170 * XXX: year 2038 problem may remain.
6172 int key_timehandler_debug
= 0;
6173 u_int32_t spd_count
= 0, sah_count
= 0, dead_sah_count
= 0, empty_sah_count
= 0, larval_sav_count
= 0, mature_sav_count
= 0, dying_sav_count
= 0, dead_sav_count
= 0;
6174 u_int64_t total_sav_count
= 0;
6176 key_timehandler(void)
6180 struct secpolicy
**spbuf
= NULL
, **spptr
= NULL
;
6181 struct secasvar
**savexbuf
= NULL
, **savexptr
= NULL
;
6182 struct secasvar
**savkabuf
= NULL
, **savkaptr
= NULL
;
6183 int spbufcount
= 0, savbufcount
= 0, spcount
= 0, savexcount
= 0, savkacount
= 0, cnt
;
6184 int stop_handler
= 1; /* stop the timehandler */
6188 /* pre-allocate buffers before taking the lock */
6189 /* if allocation failures occur - portions of the processing will be skipped */
6190 if ((spbufcount
= ipsec_policy_count
) != 0) {
6192 KMALLOC_WAIT(spbuf
, struct secpolicy
**, spbufcount
* sizeof(struct secpolicy
*));
6197 if ((savbufcount
= ipsec_sav_count
) != 0) {
6199 KMALLOC_WAIT(savexbuf
, struct secasvar
**, savbufcount
* sizeof(struct secasvar
*));
6201 savexptr
= savexbuf
;
6203 KMALLOC_WAIT(savkabuf
, struct secasvar
**, savbufcount
* sizeof(struct secasvar
*));
6205 savkaptr
= savkabuf
;
6208 lck_mtx_lock(sadb_mutex
);
6211 struct secpolicy
*sp
, *nextsp
;
6213 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
6214 for (sp
= LIST_FIRST(&sptree
[dir
]);
6217 /* don't prevent timehandler from stopping for generate policy */
6218 if (sp
->policy
!= IPSEC_POLICY_GENERATE
) {
6222 nextsp
= LIST_NEXT(sp
, chain
);
6224 if (sp
->state
== IPSEC_SPSTATE_DEAD
) {
6225 key_freesp(sp
, KEY_SADB_LOCKED
);
6229 if (sp
->lifetime
== 0 && sp
->validtime
== 0) {
6232 if (spbuf
&& spcount
< spbufcount
) {
6233 /* the deletion will occur next time */
6235 && tv
.tv_sec
- sp
->created
> sp
->lifetime
)
6237 && tv
.tv_sec
- sp
->lastused
> sp
->validtime
)) {
6238 //key_spdexpire(sp);
6239 sp
->state
= IPSEC_SPSTATE_DEAD
;
6251 struct secashead
*sah
, *nextsah
;
6252 struct secasvar
*sav
, *nextsav
;
6254 for (sah
= LIST_FIRST(&sahtree
);
6258 nextsah
= LIST_NEXT(sah
, chain
);
6260 /* if sah has been dead, then delete it and process next sah. */
6261 if (sah
->state
== SADB_SASTATE_DEAD
) {
6267 if (LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]) == NULL
&&
6268 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]) == NULL
&&
6269 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]) == NULL
&&
6270 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]) == NULL
) {
6276 if (savbufcount
== 0) {
6282 /* if LARVAL entry doesn't become MATURE, delete it. */
6283 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]);
6288 nextsav
= LIST_NEXT(sav
, chain
);
6290 if (sav
->lft_h
!= NULL
) {
6291 /* If a hard lifetime is defined for the LARVAL SA, use it */
6292 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0
6293 && tv
.tv_sec
- sav
->created
> sav
->lft_h
->sadb_lifetime_addtime
) {
6294 if (sav
->always_expire
) {
6295 key_send_delete(sav
);
6298 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6299 key_freesav(sav
, KEY_SADB_LOCKED
);
6304 if (tv
.tv_sec
- sav
->created
> key_larval_lifetime
) {
6305 key_freesav(sav
, KEY_SADB_LOCKED
);
6311 * If this is a NAT traversal SA with no activity,
6312 * we need to send a keep alive.
6314 * Performed outside of the loop before so we will
6315 * only ever send one keepalive. The first SA on
6316 * the list is the one that will be used for sending
6317 * traffic, so this is the one we use for determining
6318 * when to send the keepalive.
6320 if (savkabuf
&& savkacount
< savbufcount
) {
6321 sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]); //%%% should we check dying list if this is empty???
6322 if (sav
&& (natt_keepalive_interval
|| sav
->natt_interval
) &&
6323 (sav
->flags
& (SADB_X_EXT_NATT_KEEPALIVE
| SADB_X_EXT_ESP_KEEPALIVE
)) != 0) {
6331 * check MATURE entry to start to send expire message
6334 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]);
6339 nextsav
= LIST_NEXT(sav
, chain
);
6341 /* we don't need to check. */
6342 if (sav
->lft_s
== NULL
) {
6347 if (sav
->lft_c
== NULL
) {
6348 ipseclog((LOG_DEBUG
, "key_timehandler: "
6349 "There is no CURRENT time, why?\n"));
6353 /* check SOFT lifetime */
6354 if (sav
->lft_s
->sadb_lifetime_addtime
!= 0
6355 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
6357 * If always_expire is set, expire. Otherwise,
6358 * if the SA has not been used, delete immediately.
6360 if (sav
->lft_c
->sadb_lifetime_usetime
== 0
6361 && sav
->always_expire
== 0) {
6362 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6363 key_freesav(sav
, KEY_SADB_LOCKED
);
6365 } else if (savexbuf
&& savexcount
< savbufcount
) {
6366 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
6372 /* check SOFT lifetime by bytes */
6374 * XXX I don't know the way to delete this SA
6375 * when new SA is installed. Caution when it's
6376 * installed too big lifetime by time.
6378 else if (savexbuf
&& savexcount
< savbufcount
6379 && sav
->lft_s
->sadb_lifetime_bytes
!= 0
6380 && sav
->lft_s
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
6382 * XXX If we keep to send expire
6383 * message in the status of
6384 * DYING. Do remove below code.
6387 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
6394 /* check DYING entry to change status to DEAD. */
6395 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]);
6400 nextsav
= LIST_NEXT(sav
, chain
);
6402 /* we don't need to check. */
6403 if (sav
->lft_h
== NULL
) {
6408 if (sav
->lft_c
== NULL
) {
6409 ipseclog((LOG_DEBUG
, "key_timehandler: "
6410 "There is no CURRENT time, why?\n"));
6414 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0
6415 && tv
.tv_sec
- sav
->created
> sav
->lft_h
->sadb_lifetime_addtime
) {
6416 if (sav
->always_expire
) {
6417 key_send_delete(sav
);
6420 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6421 key_freesav(sav
, KEY_SADB_LOCKED
);
6425 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
6426 else if (savbuf
&& savexcount
< savbufcount
6427 && sav
->lft_s
!= NULL
6428 && sav
->lft_s
->sadb_lifetime_addtime
!= 0
6429 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
6431 * XXX: should be checked to be
6432 * installed the valid SA.
6436 * If there is no SA then sending
6445 /* check HARD lifetime by bytes */
6446 else if (sav
->lft_h
->sadb_lifetime_bytes
!= 0
6447 && sav
->lft_h
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
6448 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6449 key_freesav(sav
, KEY_SADB_LOCKED
);
6454 /* delete entry in DEAD */
6455 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]);
6460 nextsav
= LIST_NEXT(sav
, chain
);
6463 if (sav
->state
!= SADB_SASTATE_DEAD
) {
6464 ipseclog((LOG_DEBUG
, "key_timehandler: "
6465 "invalid sav->state "
6466 "(queue: %d SA: %d): "
6468 SADB_SASTATE_DEAD
, sav
->state
));
6472 * do not call key_freesav() here.
6473 * sav should already be freed, and sav->refcnt
6474 * shows other references to sav
6475 * (such as from SPD).
6481 if (++key_timehandler_debug
>= 300) {
6482 if (key_debug_level
) {
6483 printf("%s: total stats for %u calls\n", __FUNCTION__
, key_timehandler_debug
);
6484 printf("%s: walked %u SPDs\n", __FUNCTION__
, spd_count
);
6485 printf("%s: walked %llu SAs: LARVAL SAs %u, MATURE SAs %u, DYING SAs %u, DEAD SAs %u\n", __FUNCTION__
,
6486 total_sav_count
, larval_sav_count
, mature_sav_count
, dying_sav_count
, dead_sav_count
);
6487 printf("%s: walked %u SAHs: DEAD SAHs %u, EMPTY SAHs %u\n", __FUNCTION__
,
6488 sah_count
, dead_sah_count
, empty_sah_count
);
6489 if (sah_search_calls
) {
6490 printf("%s: SAH search cost %d iters per call\n", __FUNCTION__
,
6491 (sah_search_count
/ sah_search_calls
));
6497 empty_sah_count
= 0;
6498 larval_sav_count
= 0;
6499 mature_sav_count
= 0;
6500 dying_sav_count
= 0;
6502 total_sav_count
= 0;
6503 sah_search_count
= 0;
6504 sah_search_calls
= 0;
6505 key_timehandler_debug
= 0;
6507 #ifndef IPSEC_NONBLOCK_ACQUIRE
6510 struct secacq
*acq
, *nextacq
;
6512 for (acq
= LIST_FIRST(&acqtree
);
6516 nextacq
= LIST_NEXT(acq
, chain
);
6518 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
6519 && __LIST_CHAINED(acq
)) {
6520 LIST_REMOVE(acq
, chain
);
6529 struct secspacq
*acq
, *nextacq
;
6531 for (acq
= LIST_FIRST(&spacqtree
);
6535 nextacq
= LIST_NEXT(acq
, chain
);
6537 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
6538 && __LIST_CHAINED(acq
)) {
6539 LIST_REMOVE(acq
, chain
);
6545 /* initialize random seed */
6546 if (key_tick_init_random
++ > key_int_random
) {
6547 key_tick_init_random
= 0;
6551 uint64_t acc_sleep_time
= 0;
6552 absolutetime_to_nanoseconds(mach_absolutetime_asleep
, &acc_sleep_time
);
6553 natt_now
= ++up_time
+ (acc_sleep_time
/ NSEC_PER_SEC
);
6555 lck_mtx_unlock(sadb_mutex
);
6557 /* send messages outside of sadb_mutex */
6558 if (spbuf
&& spcount
> 0) {
6561 key_spdexpire(*(--spptr
));
6564 if (savkabuf
&& savkacount
> 0) {
6565 struct secasvar
**savkaptr_sav
= savkaptr
;
6566 int cnt_send
= savkacount
;
6568 while (cnt_send
--) {
6569 if (ipsec_send_natt_keepalive(*(--savkaptr
))) {
6570 // <rdar://6768487> iterate (all over again) and update timestamps
6571 struct secasvar
**savkaptr_update
= savkaptr_sav
;
6572 int cnt_update
= savkacount
;
6573 while (cnt_update
--) {
6574 key_update_natt_keepalive_timestamp(*savkaptr
,
6575 *(--savkaptr_update
));
6580 if (savexbuf
&& savexcount
> 0) {
6583 key_expire(*(--savexptr
));
6587 /* decrement ref counts and free buffers */
6588 lck_mtx_lock(sadb_mutex
);
6591 key_freesp(*spptr
++, KEY_SADB_LOCKED
);
6596 while (savkacount
--) {
6597 key_freesav(*savkaptr
++, KEY_SADB_LOCKED
);
6602 while (savexcount
--) {
6603 key_freesav(*savexptr
++, KEY_SADB_LOCKED
);
6609 key_timehandler_running
= 0;
6610 /* Turn on the ipsec bypass */
6613 /* do exchange to tick time !! */
6614 (void)timeout((void *)key_timehandler
, (void *)0, hz
);
6617 lck_mtx_unlock(sadb_mutex
);
6622 * to initialize a seed for random()
6628 /* Our PRNG is based on Yarrow and doesn't need to be seeded */
6635 srandom(tv
.tv_usec
);
6646 key_randomfill(&value
, sizeof(value
));
6656 cc_rand_generate(p
, l
);
6660 static int warn
= 1;
6663 n
= (size_t)read_random(p
, (u_int
)l
);
6667 bcopy(&v
, (u_int8_t
*)p
+ n
,
6668 l
- n
< sizeof(v
) ? l
- n
: sizeof(v
));
6672 printf("WARNING: pseudo-random number generator "
6673 "used for IPsec processing\n");
6681 * map SADB_SATYPE_* to IPPROTO_*.
6682 * if satype == SADB_SATYPE then satype is mapped to ~0.
6684 * 0: invalid satype.
6691 case SADB_SATYPE_UNSPEC
:
6692 return IPSEC_PROTO_ANY
;
6693 case SADB_SATYPE_AH
:
6695 case SADB_SATYPE_ESP
:
6704 * map IPPROTO_* to SADB_SATYPE_*
6706 * 0: invalid protocol type.
6714 return SADB_SATYPE_AH
;
6716 return SADB_SATYPE_ESP
;
6724 key_get_ipsec_if_from_message(const struct sadb_msghdr
*mhp
, int message_type
)
6726 struct sadb_x_ipsecif
*ipsecifopts
= NULL
;
6727 ifnet_t ipsec_if
= NULL
;
6729 ipsecifopts
= (struct sadb_x_ipsecif
*)(void *)mhp
->ext
[message_type
];
6730 if (ipsecifopts
!= NULL
) {
6731 if (ipsecifopts
->sadb_x_ipsecif_ipsec_if
[0]) {
6732 ifnet_find_by_name(ipsecifopts
->sadb_x_ipsecif_ipsec_if
, &ipsec_if
);
6740 key_get_outgoing_ifindex_from_message(const struct sadb_msghdr
*mhp
, int message_type
)
6742 struct sadb_x_ipsecif
*ipsecifopts
= NULL
;
6743 ifnet_t outgoing_if
= NULL
;
6745 ipsecifopts
= (struct sadb_x_ipsecif
*)(void *)mhp
->ext
[message_type
];
6746 if (ipsecifopts
!= NULL
) {
6747 if (ipsecifopts
->sadb_x_ipsecif_outgoing_if
[0]) {
6748 ifnet_find_by_name(ipsecifopts
->sadb_x_ipsecif_outgoing_if
, &outgoing_if
);
6752 return outgoing_if
? outgoing_if
->if_index
: 0;
6757 * SADB_GETSPI processing is to receive
6758 * <base, (SA2), src address, dst address, (SPI range)>
6759 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
6760 * tree with the status of LARVAL, and send
6761 * <base, SA(*), address(SD)>
6764 * IN: mhp: pointer to the pointer to each header.
6765 * OUT: NULL if fail.
6766 * other if success, return pointer to the message to send.
6772 const struct sadb_msghdr
*mhp
)
6774 struct sadb_address
*src0
, *dst0
;
6775 struct secasindex saidx
;
6776 struct secashead
*newsah
;
6777 struct secasvar
*newsav
;
6778 ifnet_t ipsec_if
= NULL
;
6785 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6788 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
6789 panic("key_getspi: NULL pointer is passed.\n");
6792 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6793 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
6794 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
6795 return key_senderror(so
, m
, EINVAL
);
6797 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6798 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
6799 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
6800 return key_senderror(so
, m
, EINVAL
);
6802 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
6803 mode
= ((struct sadb_x_sa2
*)
6804 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
6805 reqid
= ((struct sadb_x_sa2
*)
6806 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
6808 mode
= IPSEC_MODE_ANY
;
6812 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
6813 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
6815 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
6817 /* map satype to proto */
6818 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6819 ipseclog((LOG_DEBUG
, "key_getspi: invalid satype is passed.\n"));
6820 return key_senderror(so
, m
, EINVAL
);
6823 /* make sure if port number is zero. */
6824 switch (((struct sockaddr
*)(src0
+ 1))->sa_family
) {
6826 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
6827 sizeof(struct sockaddr_in
)) {
6828 return key_senderror(so
, m
, EINVAL
);
6830 ((struct sockaddr_in
*)(void *)(src0
+ 1))->sin_port
= 0;
6833 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
6834 sizeof(struct sockaddr_in6
)) {
6835 return key_senderror(so
, m
, EINVAL
);
6837 ((struct sockaddr_in6
*)(void *)(src0
+ 1))->sin6_port
= 0;
6842 switch (((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
6844 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
6845 sizeof(struct sockaddr_in
)) {
6846 return key_senderror(so
, m
, EINVAL
);
6848 ((struct sockaddr_in
*)(void *)(dst0
+ 1))->sin_port
= 0;
6851 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
6852 sizeof(struct sockaddr_in6
)) {
6853 return key_senderror(so
, m
, EINVAL
);
6855 ((struct sockaddr_in6
*)(void *)(dst0
+ 1))->sin6_port
= 0;
6861 /* XXX boundary check against sa_len */
6862 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
6864 lck_mtx_lock(sadb_mutex
);
6866 /* SPI allocation */
6867 spi
= key_do_getnewspi((struct sadb_spirange
*)
6868 (void *)mhp
->ext
[SADB_EXT_SPIRANGE
], &saidx
);
6870 lck_mtx_unlock(sadb_mutex
);
6871 return key_senderror(so
, m
, EINVAL
);
6874 /* get a SA index */
6875 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
6876 /* create a new SA index: key_addspi is always used for inbound spi */
6877 if ((newsah
= key_newsah(&saidx
, ipsec_if
, key_get_outgoing_ifindex_from_message(mhp
, SADB_X_EXT_IPSECIF
), IPSEC_DIR_INBOUND
)) == NULL
) {
6878 lck_mtx_unlock(sadb_mutex
);
6879 ipseclog((LOG_DEBUG
, "key_getspi: No more memory.\n"));
6880 return key_senderror(so
, m
, ENOBUFS
);
6886 newsav
= key_newsav(m
, mhp
, newsah
, &error
, so
);
6887 if (newsav
== NULL
) {
6888 /* XXX don't free new SA index allocated in above. */
6889 lck_mtx_unlock(sadb_mutex
);
6890 return key_senderror(so
, m
, error
);
6894 key_setspi(newsav
, htonl(spi
));
6896 #ifndef IPSEC_NONBLOCK_ACQUIRE
6897 /* delete the entry in acqtree */
6898 if (mhp
->msg
->sadb_msg_seq
!= 0) {
6900 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) != NULL
) {
6901 /* reset counter in order to deletion by timehandler. */
6904 acq
->created
= tv
.tv_sec
;
6910 lck_mtx_unlock(sadb_mutex
);
6913 struct mbuf
*n
, *nn
;
6914 struct sadb_sa
*m_sa
;
6915 struct sadb_msg
*newmsg
;
6918 /* create new sadb_msg to reply. */
6919 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
)) +
6920 PFKEY_ALIGN8(sizeof(struct sadb_sa
));
6921 if (len
> MCLBYTES
) {
6922 return key_senderror(so
, m
, ENOBUFS
);
6925 MGETHDR(n
, M_WAITOK
, MT_DATA
);
6926 if (n
&& len
> MHLEN
) {
6927 MCLGET(n
, M_WAITOK
);
6928 if ((n
->m_flags
& M_EXT
) == 0) {
6934 return key_senderror(so
, m
, ENOBUFS
);
6941 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
6942 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
6944 m_sa
= (struct sadb_sa
*)(void *)(mtod(n
, caddr_t
) + off
);
6945 m_sa
->sadb_sa_len
= PFKEY_UNIT64(sizeof(struct sadb_sa
));
6946 m_sa
->sadb_sa_exttype
= SADB_EXT_SA
;
6947 m_sa
->sadb_sa_spi
= htonl(spi
);
6948 off
+= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
6952 panic("length inconsistency in key_getspi");
6956 int mbufItems
[] = {SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
6957 n
->m_next
= key_gather_mbuf(m
, mhp
, 0, sizeof(mbufItems
) / sizeof(int), mbufItems
);
6960 return key_senderror(so
, m
, ENOBUFS
);
6964 if (n
->m_len
< sizeof(struct sadb_msg
)) {
6965 n
= m_pullup(n
, sizeof(struct sadb_msg
));
6967 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
6971 n
->m_pkthdr
.len
= 0;
6972 for (nn
= n
; nn
; nn
= nn
->m_next
) {
6973 n
->m_pkthdr
.len
+= nn
->m_len
;
6976 newmsg
= mtod(n
, struct sadb_msg
*);
6977 newmsg
->sadb_msg_seq
= newsav
->seq
;
6978 newmsg
->sadb_msg_errno
= 0;
6979 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
6982 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
6987 key_getspi2(struct sockaddr
*src
,
6988 struct sockaddr
*dst
,
6992 struct sadb_spirange
*spirange
)
6995 struct secasindex saidx
;
6997 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6999 /* XXX boundary check against sa_len */
7000 KEY_SETSECASIDX(proto
, mode
, reqid
, src
, dst
, 0, &saidx
);
7002 /* make sure if port number is zero. */
7003 switch (((struct sockaddr
*)&saidx
.src
)->sa_family
) {
7005 if (((struct sockaddr
*)&saidx
.src
)->sa_len
!= sizeof(struct sockaddr_in
)) {
7008 ((struct sockaddr_in
*)&saidx
.src
)->sin_port
= 0;
7011 if (((struct sockaddr
*)&saidx
.src
)->sa_len
!= sizeof(struct sockaddr_in6
)) {
7014 ((struct sockaddr_in6
*)&saidx
.src
)->sin6_port
= 0;
7019 switch (((struct sockaddr
*)&saidx
.dst
)->sa_family
) {
7021 if (((struct sockaddr
*)&saidx
.dst
)->sa_len
!= sizeof(struct sockaddr_in
)) {
7024 ((struct sockaddr_in
*)&saidx
.dst
)->sin_port
= 0;
7027 if (((struct sockaddr
*)&saidx
.dst
)->sa_len
!= sizeof(struct sockaddr_in6
)) {
7030 ((struct sockaddr_in6
*)&saidx
.dst
)->sin6_port
= 0;
7036 lck_mtx_lock(sadb_mutex
);
7038 /* SPI allocation */
7039 spi
= key_do_getnewspi(spirange
, &saidx
);
7041 lck_mtx_unlock(sadb_mutex
);
7047 * allocating new SPI
7048 * called by key_getspi() and key_getspi2().
7055 struct sadb_spirange
*spirange
,
7056 struct secasindex
*saidx
)
7059 u_int32_t keymin
, keymax
;
7060 int count
= key_spi_trycnt
;
7062 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7064 /* set spi range to allocate */
7065 if (spirange
!= NULL
) {
7066 keymin
= spirange
->sadb_spirange_min
;
7067 keymax
= spirange
->sadb_spirange_max
;
7069 keymin
= key_spi_minval
;
7070 keymax
= key_spi_maxval
;
7072 if (keymin
== keymax
) {
7073 if (key_checkspidup(saidx
, keymin
) != NULL
) {
7074 ipseclog((LOG_DEBUG
, "key_do_getnewspi: SPI %u exists already.\n", keymin
));
7078 count
--; /* taking one cost. */
7081 u_int32_t range
= keymax
- keymin
+ 1; /* overflow value of zero means full range */
7086 /* when requesting to allocate spi ranged */
7088 u_int32_t rand_val
= key_random();
7090 /* generate pseudo-random SPI value ranged. */
7091 newspi
= (range
== 0 ? rand_val
: keymin
+ (rand_val
% range
));
7093 if (key_checkspidup(saidx
, newspi
) == NULL
) {
7098 if (count
== 0 || newspi
== 0) {
7099 ipseclog((LOG_DEBUG
, "key_do_getnewspi: to allocate spi is failed.\n"));
7105 keystat
.getspi_count
=
7106 (keystat
.getspi_count
+ key_spi_trycnt
- count
) / 2;
7112 * SADB_UPDATE processing
7114 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
7115 * key(AE), (identity(SD),) (sensitivity)>
7116 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
7118 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
7119 * (identity(SD),) (sensitivity)>
7122 * m will always be freed.
7128 const struct sadb_msghdr
*mhp
)
7130 struct sadb_sa
*sa0
;
7131 struct sadb_address
*src0
, *dst0
;
7132 ifnet_t ipsec_if
= NULL
;
7133 struct secasindex saidx
;
7134 struct secashead
*sah
;
7135 struct secasvar
*sav
;
7142 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7145 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
7146 panic("key_update: NULL pointer is passed.\n");
7149 /* map satype to proto */
7150 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7151 ipseclog((LOG_DEBUG
, "key_update: invalid satype is passed.\n"));
7152 return key_senderror(so
, m
, EINVAL
);
7155 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
7156 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
7157 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
7158 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
7159 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
7160 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
7161 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
7162 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
7163 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
7164 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
7165 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
7166 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
7167 return key_senderror(so
, m
, EINVAL
);
7169 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
7170 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7171 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
7172 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
7173 return key_senderror(so
, m
, EINVAL
);
7175 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
7176 mode
= ((struct sadb_x_sa2
*)
7177 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
7178 reqid
= ((struct sadb_x_sa2
*)
7179 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
7180 flags2
= ((struct sadb_x_sa2
*)(void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_flags
;
7182 mode
= IPSEC_MODE_ANY
;
7186 /* XXX boundary checking for other extensions */
7188 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
7189 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
7190 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
7191 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
7193 /* XXX boundary check against sa_len */
7194 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
7196 lck_mtx_lock(sadb_mutex
);
7198 /* get a SA header */
7199 if ((sah
= key_getsah(&saidx
)) == NULL
) {
7200 lck_mtx_unlock(sadb_mutex
);
7201 ipseclog((LOG_DEBUG
, "key_update: no SA index found.\n"));
7202 return key_senderror(so
, m
, ENOENT
);
7205 /* set spidx if there */
7207 error
= key_setident(sah
, m
, mhp
);
7209 lck_mtx_unlock(sadb_mutex
);
7210 return key_senderror(so
, m
, error
);
7213 /* find a SA with sequence number. */
7214 #if IPSEC_DOSEQCHECK
7215 if (mhp
->msg
->sadb_msg_seq
!= 0
7216 && (sav
= key_getsavbyseq(sah
, mhp
->msg
->sadb_msg_seq
)) == NULL
) {
7217 lck_mtx_unlock(sadb_mutex
);
7218 ipseclog((LOG_DEBUG
,
7219 "key_update: no larval SA with sequence %u exists.\n",
7220 mhp
->msg
->sadb_msg_seq
));
7221 return key_senderror(so
, m
, ENOENT
);
7224 if ((sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
)) == NULL
) {
7225 lck_mtx_unlock(sadb_mutex
);
7226 ipseclog((LOG_DEBUG
,
7227 "key_update: no such a SA found (spi:%u)\n",
7228 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
7229 return key_senderror(so
, m
, EINVAL
);
7233 /* validity check */
7234 if (sav
->sah
->saidx
.proto
!= proto
) {
7235 lck_mtx_unlock(sadb_mutex
);
7236 ipseclog((LOG_DEBUG
,
7237 "key_update: protocol mismatched (DB=%u param=%u)\n",
7238 sav
->sah
->saidx
.proto
, proto
));
7239 return key_senderror(so
, m
, EINVAL
);
7241 #if IPSEC_DOSEQCHECK
7242 if (sav
->spi
!= sa0
->sadb_sa_spi
) {
7243 lck_mtx_unlock(sadb_mutex
);
7244 ipseclog((LOG_DEBUG
,
7245 "key_update: SPI mismatched (DB:%u param:%u)\n",
7246 (u_int32_t
)ntohl(sav
->spi
),
7247 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
7248 return key_senderror(so
, m
, EINVAL
);
7251 if (sav
->pid
!= mhp
->msg
->sadb_msg_pid
) {
7252 lck_mtx_unlock(sadb_mutex
);
7253 ipseclog((LOG_DEBUG
,
7254 "key_update: pid mismatched (DB:%u param:%u)\n",
7255 sav
->pid
, mhp
->msg
->sadb_msg_pid
));
7256 return key_senderror(so
, m
, EINVAL
);
7259 /* copy sav values */
7260 error
= key_setsaval(sav
, m
, mhp
);
7262 key_freesav(sav
, KEY_SADB_LOCKED
);
7263 lck_mtx_unlock(sadb_mutex
);
7264 return key_senderror(so
, m
, error
);
7267 sav
->flags2
= flags2
;
7268 if (flags2
& SADB_X_EXT_SA2_DELETE_ON_DETACH
) {
7273 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
7274 * this SA is for transport mode - otherwise clear it.
7276 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0 &&
7277 (sav
->sah
->saidx
.mode
!= IPSEC_MODE_TRANSPORT
||
7278 sav
->sah
->saidx
.src
.ss_family
!= AF_INET
)) {
7279 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
7282 /* check SA values to be mature. */
7283 if ((error
= key_mature(sav
)) != 0) {
7284 key_freesav(sav
, KEY_SADB_LOCKED
);
7285 lck_mtx_unlock(sadb_mutex
);
7286 return key_senderror(so
, m
, error
);
7289 lck_mtx_unlock(sadb_mutex
);
7294 /* set msg buf from mhp */
7295 n
= key_getmsgbuf_x1(m
, mhp
);
7297 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
7298 return key_senderror(so
, m
, ENOBUFS
);
7302 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
7307 key_migrate(struct socket
*so
,
7309 const struct sadb_msghdr
*mhp
)
7311 struct sadb_sa
*sa0
= NULL
;
7312 struct sadb_address
*src0
= NULL
;
7313 struct sadb_address
*dst0
= NULL
;
7314 struct sadb_address
*src1
= NULL
;
7315 struct sadb_address
*dst1
= NULL
;
7316 ifnet_t ipsec_if0
= NULL
;
7317 ifnet_t ipsec_if1
= NULL
;
7318 struct secasindex saidx0
;
7319 struct secasindex saidx1
;
7320 struct secashead
*sah
= NULL
;
7321 struct secashead
*newsah
= NULL
;
7322 struct secasvar
*sav
= NULL
;
7325 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7328 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
7329 panic("key_migrate: NULL pointer is passed.\n");
7332 /* map satype to proto */
7333 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7334 ipseclog((LOG_DEBUG
, "key_migrate: invalid satype is passed.\n"));
7335 return key_senderror(so
, m
, EINVAL
);
7338 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
7339 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
7340 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
7341 mhp
->ext
[SADB_EXT_MIGRATE_ADDRESS_SRC
] == NULL
||
7342 mhp
->ext
[SADB_EXT_MIGRATE_ADDRESS_DST
] == NULL
) {
7343 ipseclog((LOG_DEBUG
, "key_migrate: invalid message is passed.\n"));
7344 return key_senderror(so
, m
, EINVAL
);
7347 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
7348 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7349 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
7350 mhp
->extlen
[SADB_EXT_MIGRATE_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7351 mhp
->extlen
[SADB_EXT_MIGRATE_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
7352 ipseclog((LOG_DEBUG
, "key_migrate: invalid message is passed.\n"));
7353 return key_senderror(so
, m
, EINVAL
);
7356 lck_mtx_lock(sadb_mutex
);
7358 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
7359 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
7360 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
7361 src1
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_MIGRATE_ADDRESS_SRC
]);
7362 dst1
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_MIGRATE_ADDRESS_DST
]);
7363 ipsec_if0
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
7364 ipsec_if1
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_MIGRATE_IPSECIF
);
7366 /* Find existing SAH and SAV */
7367 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if0
? ipsec_if0
->if_index
: 0, &saidx0
);
7369 LIST_FOREACH(sah
, &sahtree
, chain
) {
7370 if (sah
->state
!= SADB_SASTATE_MATURE
) {
7373 if (key_cmpsaidx(&sah
->saidx
, &saidx0
, CMP_HEAD
) == 0) {
7377 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
7378 if (sav
&& sav
->state
== SADB_SASTATE_MATURE
) {
7383 lck_mtx_unlock(sadb_mutex
);
7384 ipseclog((LOG_DEBUG
, "key_migrate: no mature SAH found.\n"));
7385 return key_senderror(so
, m
, ENOENT
);
7389 lck_mtx_unlock(sadb_mutex
);
7390 ipseclog((LOG_DEBUG
, "key_migrate: no SA found.\n"));
7391 return key_senderror(so
, m
, ENOENT
);
7394 /* Find or create new SAH */
7395 KEY_SETSECASIDX(proto
, sah
->saidx
.mode
, sah
->saidx
.reqid
, src1
+ 1, dst1
+ 1, ipsec_if1
? ipsec_if1
->if_index
: 0, &saidx1
);
7397 if ((newsah
= key_getsah(&saidx1
)) == NULL
) {
7398 if ((newsah
= key_newsah(&saidx1
, ipsec_if1
, key_get_outgoing_ifindex_from_message(mhp
, SADB_X_EXT_MIGRATE_IPSECIF
), sah
->dir
)) == NULL
) {
7399 lck_mtx_unlock(sadb_mutex
);
7400 ipseclog((LOG_DEBUG
, "key_migrate: No more memory.\n"));
7401 return key_senderror(so
, m
, ENOBUFS
);
7405 /* Migrate SAV in to new SAH */
7406 if (key_migratesav(sav
, newsah
) != 0) {
7407 lck_mtx_unlock(sadb_mutex
);
7408 ipseclog((LOG_DEBUG
, "key_migrate: Failed to migrate SA to new SAH.\n"));
7409 return key_senderror(so
, m
, EINVAL
);
7412 /* Reset NAT values */
7413 sav
->flags
= sa0
->sadb_sa_flags
;
7414 sav
->natt_encapsulated_src_port
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_src_port
;
7415 sav
->remote_ike_port
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
;
7416 sav
->natt_interval
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_interval
;
7417 sav
->natt_offload_interval
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_offload_interval
;
7418 sav
->natt_last_activity
= natt_now
;
7421 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
7422 * SADB_X_EXT_NATT is set and SADB_X_EXT_NATT_KEEPALIVE is not
7423 * set (we're not behind nat) - otherwise clear it.
7425 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0) {
7426 if ((sav
->flags
& SADB_X_EXT_NATT
) == 0 ||
7427 (sav
->flags
& SADB_X_EXT_NATT_KEEPALIVE
) != 0) {
7428 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
7432 lck_mtx_unlock(sadb_mutex
);
7435 struct sadb_msg
*newmsg
;
7436 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
7437 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
, SADB_X_EXT_IPSECIF
,
7438 SADB_EXT_MIGRATE_ADDRESS_SRC
, SADB_EXT_MIGRATE_ADDRESS_DST
, SADB_X_EXT_MIGRATE_IPSECIF
};
7440 /* create new sadb_msg to reply. */
7441 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
) / sizeof(int), mbufItems
);
7443 return key_senderror(so
, m
, ENOBUFS
);
7446 if (n
->m_len
< sizeof(struct sadb_msg
)) {
7447 n
= m_pullup(n
, sizeof(struct sadb_msg
));
7449 return key_senderror(so
, m
, ENOBUFS
);
7452 newmsg
= mtod(n
, struct sadb_msg
*);
7453 newmsg
->sadb_msg_errno
= 0;
7454 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
7457 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
7462 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
7463 * only called by key_update().
7466 * others : found, pointer to a SA.
7468 #if IPSEC_DOSEQCHECK
7469 static struct secasvar
*
7471 struct secashead
*sah
,
7474 struct secasvar
*sav
;
7477 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7479 state
= SADB_SASTATE_LARVAL
;
7481 /* search SAD with sequence number ? */
7482 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
7483 KEY_CHKSASTATE(state
, sav
->state
, "key_getsabyseq");
7485 if (sav
->seq
== seq
) {
7487 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
7488 printf("DP key_getsavbyseq cause "
7489 "refcnt++:%d SA:0x%llx\n", sav
->refcnt
,
7490 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
7500 * SADB_ADD processing
7501 * add a entry to SA database, when received
7502 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
7503 * key(AE), (identity(SD),) (sensitivity)>
7506 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
7507 * (identity(SD),) (sensitivity)>
7510 * IGNORE identity and sensitivity messages.
7512 * m will always be freed.
7518 const struct sadb_msghdr
*mhp
)
7520 struct sadb_sa
*sa0
;
7521 struct sadb_address
*src0
, *dst0
;
7522 ifnet_t ipsec_if
= NULL
;
7523 struct secasindex saidx
;
7524 struct secashead
*newsah
;
7525 struct secasvar
*newsav
;
7531 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7534 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
7535 panic("key_add: NULL pointer is passed.\n");
7538 /* map satype to proto */
7539 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7540 ipseclog((LOG_DEBUG
, "key_add: invalid satype is passed.\n"));
7542 return key_senderror(so
, m
, EINVAL
);
7545 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
7546 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
7547 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
7548 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
7549 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
7550 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
7551 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
7552 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
7553 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
7554 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
7555 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
7556 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
7558 return key_senderror(so
, m
, EINVAL
);
7560 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
7561 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7562 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
7564 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
7566 return key_senderror(so
, m
, EINVAL
);
7568 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
7569 mode
= ((struct sadb_x_sa2
*)
7570 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
7571 reqid
= ((struct sadb_x_sa2
*)
7572 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
7574 mode
= IPSEC_MODE_ANY
;
7578 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
7579 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
7580 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
7581 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
7583 /* XXX boundary check against sa_len */
7584 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
7586 lck_mtx_lock(sadb_mutex
);
7588 /* get a SA header */
7589 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
7590 /* create a new SA header: key_addspi is always used for outbound spi */
7591 if ((newsah
= key_newsah(&saidx
, ipsec_if
, key_get_outgoing_ifindex_from_message(mhp
, SADB_X_EXT_IPSECIF
), IPSEC_DIR_OUTBOUND
)) == NULL
) {
7592 lck_mtx_unlock(sadb_mutex
);
7593 ipseclog((LOG_DEBUG
, "key_add: No more memory.\n"));
7595 return key_senderror(so
, m
, ENOBUFS
);
7599 /* set spidx if there */
7601 error
= key_setident(newsah
, m
, mhp
);
7603 lck_mtx_unlock(sadb_mutex
);
7605 return key_senderror(so
, m
, error
);
7608 /* create new SA entry. */
7609 /* We can create new SA only if SPI is different. */
7610 if (key_getsavbyspi(newsah
, sa0
->sadb_sa_spi
)) {
7611 lck_mtx_unlock(sadb_mutex
);
7612 ipseclog((LOG_DEBUG
, "key_add: SA already exists.\n"));
7614 return key_senderror(so
, m
, EEXIST
);
7616 newsav
= key_newsav(m
, mhp
, newsah
, &error
, so
);
7617 if (newsav
== NULL
) {
7618 lck_mtx_unlock(sadb_mutex
);
7620 return key_senderror(so
, m
, error
);
7624 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
7625 * this SA is for transport mode - otherwise clear it.
7627 if ((newsav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0 &&
7628 (newsah
->saidx
.mode
!= IPSEC_MODE_TRANSPORT
||
7629 newsah
->saidx
.dst
.ss_family
!= AF_INET
)) {
7630 newsav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
7633 /* check SA values to be mature. */
7634 if ((error
= key_mature(newsav
)) != 0) {
7635 key_freesav(newsav
, KEY_SADB_LOCKED
);
7636 lck_mtx_unlock(sadb_mutex
);
7638 return key_senderror(so
, m
, error
);
7641 lck_mtx_unlock(sadb_mutex
);
7644 * don't call key_freesav() here, as we would like to keep the SA
7645 * in the database on success.
7651 /* set msg buf from mhp */
7652 n
= key_getmsgbuf_x1(m
, mhp
);
7654 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
7656 return key_senderror(so
, m
, ENOBUFS
);
7659 // mh.ext points to the mbuf content.
7660 // Zero out Encryption and Integrity keys if present.
7663 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
7670 struct secashead
*sah
,
7672 const struct sadb_msghdr
*mhp
)
7674 const struct sadb_ident
*idsrc
, *iddst
;
7675 int idsrclen
, iddstlen
;
7677 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7680 if (sah
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
7681 panic("key_setident: NULL pointer is passed.\n");
7684 /* don't make buffer if not there */
7685 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
&&
7686 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
7692 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
||
7693 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
7694 ipseclog((LOG_DEBUG
, "key_setident: invalid identity.\n"));
7698 idsrc
= (const struct sadb_ident
*)
7699 (void *)mhp
->ext
[SADB_EXT_IDENTITY_SRC
];
7700 iddst
= (const struct sadb_ident
*)
7701 (void *)mhp
->ext
[SADB_EXT_IDENTITY_DST
];
7702 idsrclen
= mhp
->extlen
[SADB_EXT_IDENTITY_SRC
];
7703 iddstlen
= mhp
->extlen
[SADB_EXT_IDENTITY_DST
];
7705 /* validity check */
7706 if (idsrc
->sadb_ident_type
!= iddst
->sadb_ident_type
) {
7707 ipseclog((LOG_DEBUG
, "key_setident: ident type mismatch.\n"));
7711 switch (idsrc
->sadb_ident_type
) {
7712 case SADB_IDENTTYPE_PREFIX
:
7713 case SADB_IDENTTYPE_FQDN
:
7714 case SADB_IDENTTYPE_USERFQDN
:
7716 /* XXX do nothing */
7722 /* make structure */
7723 KMALLOC_NOWAIT(sah
->idents
, struct sadb_ident
*, idsrclen
);
7724 if (sah
->idents
== NULL
) {
7725 lck_mtx_unlock(sadb_mutex
);
7726 KMALLOC_WAIT(sah
->idents
, struct sadb_ident
*, idsrclen
);
7727 lck_mtx_lock(sadb_mutex
);
7728 if (sah
->idents
== NULL
) {
7729 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
7733 KMALLOC_NOWAIT(sah
->identd
, struct sadb_ident
*, iddstlen
);
7734 if (sah
->identd
== NULL
) {
7735 lck_mtx_unlock(sadb_mutex
);
7736 KMALLOC_WAIT(sah
->identd
, struct sadb_ident
*, iddstlen
);
7737 lck_mtx_lock(sadb_mutex
);
7738 if (sah
->identd
== NULL
) {
7741 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
7745 bcopy(idsrc
, sah
->idents
, idsrclen
);
7746 bcopy(iddst
, sah
->identd
, iddstlen
);
7752 * m will not be freed on return.
7753 * it is caller's responsibility to free the result.
7755 static struct mbuf
*
7758 const struct sadb_msghdr
*mhp
)
7761 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
7762 SADB_X_EXT_SA2
, SADB_EXT_ADDRESS_SRC
,
7763 SADB_EXT_ADDRESS_DST
, SADB_EXT_LIFETIME_HARD
,
7764 SADB_EXT_LIFETIME_SOFT
, SADB_EXT_IDENTITY_SRC
,
7765 SADB_EXT_IDENTITY_DST
};
7768 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
7769 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
7772 /* create new sadb_msg to reply. */
7773 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
) / sizeof(int), mbufItems
);
7778 if (n
->m_len
< sizeof(struct sadb_msg
)) {
7779 n
= m_pullup(n
, sizeof(struct sadb_msg
));
7784 mtod(n
, struct sadb_msg
*)->sadb_msg_errno
= 0;
7785 mtod(n
, struct sadb_msg
*)->sadb_msg_len
=
7786 PFKEY_UNIT64(n
->m_pkthdr
.len
);
7791 static int key_delete_all(struct socket
*, struct mbuf
*,
7792 const struct sadb_msghdr
*, u_int16_t
);
7795 * SADB_DELETE processing
7797 * <base, SA(*), address(SD)>
7798 * from the ikmpd, and set SADB_SASTATE_DEAD,
7800 * <base, SA(*), address(SD)>
7803 * m will always be freed.
7809 const struct sadb_msghdr
*mhp
)
7811 struct sadb_sa
*sa0
;
7812 struct sadb_address
*src0
, *dst0
;
7813 ifnet_t ipsec_if
= NULL
;
7814 struct secasindex saidx
;
7815 struct secashead
*sah
;
7816 struct secasvar
*sav
= NULL
;
7819 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7822 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
7823 panic("key_delete: NULL pointer is passed.\n");
7826 /* map satype to proto */
7827 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7828 ipseclog((LOG_DEBUG
, "key_delete: invalid satype is passed.\n"));
7829 return key_senderror(so
, m
, EINVAL
);
7832 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
7833 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
7834 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
7835 return key_senderror(so
, m
, EINVAL
);
7838 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7839 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
7840 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
7841 return key_senderror(so
, m
, EINVAL
);
7844 lck_mtx_lock(sadb_mutex
);
7846 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
7848 * Caller wants us to delete all non-LARVAL SAs
7849 * that match the src/dst. This is used during
7850 * IKE INITIAL-CONTACT.
7852 ipseclog((LOG_DEBUG
, "key_delete: doing delete all.\n"));
7853 /* key_delete_all will unlock sadb_mutex */
7854 return key_delete_all(so
, m
, mhp
, proto
);
7855 } else if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
)) {
7856 lck_mtx_unlock(sadb_mutex
);
7857 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
7858 return key_senderror(so
, m
, EINVAL
);
7861 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
7862 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
7863 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
7864 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
7866 /* XXX boundary check against sa_len */
7867 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
7869 /* get a SA header */
7870 LIST_FOREACH(sah
, &sahtree
, chain
) {
7871 if (sah
->state
== SADB_SASTATE_DEAD
) {
7874 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0) {
7878 /* get a SA with SPI. */
7879 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
7885 lck_mtx_unlock(sadb_mutex
);
7886 ipseclog((LOG_DEBUG
, "key_delete: no SA found.\n"));
7887 return key_senderror(so
, m
, ENOENT
);
7890 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
7891 key_freesav(sav
, KEY_SADB_LOCKED
);
7893 lck_mtx_unlock(sadb_mutex
);
7898 struct sadb_msg
*newmsg
;
7899 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
7900 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
7902 /* create new sadb_msg to reply. */
7903 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
) / sizeof(int), mbufItems
);
7905 return key_senderror(so
, m
, ENOBUFS
);
7908 if (n
->m_len
< sizeof(struct sadb_msg
)) {
7909 n
= m_pullup(n
, sizeof(struct sadb_msg
));
7911 return key_senderror(so
, m
, ENOBUFS
);
7914 newmsg
= mtod(n
, struct sadb_msg
*);
7915 newmsg
->sadb_msg_errno
= 0;
7916 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
7919 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
7924 * delete all SAs for src/dst. Called from key_delete().
7930 const struct sadb_msghdr
*mhp
,
7933 struct sadb_address
*src0
, *dst0
;
7934 ifnet_t ipsec_if
= NULL
;
7935 struct secasindex saidx
;
7936 struct secashead
*sah
;
7937 struct secasvar
*sav
, *nextsav
;
7938 u_int stateidx
, state
;
7940 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7942 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
7943 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
7944 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
7946 /* XXX boundary check against sa_len */
7947 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
7949 LIST_FOREACH(sah
, &sahtree
, chain
) {
7950 if (sah
->state
== SADB_SASTATE_DEAD
) {
7953 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0) {
7957 /* Delete all non-LARVAL SAs. */
7959 stateidx
< _ARRAYLEN(saorder_state_alive
);
7961 state
= saorder_state_alive
[stateidx
];
7962 if (state
== SADB_SASTATE_LARVAL
) {
7965 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
7966 sav
!= NULL
; sav
= nextsav
) {
7967 nextsav
= LIST_NEXT(sav
, chain
);
7969 if (sav
->state
!= state
) {
7970 ipseclog((LOG_DEBUG
, "key_delete_all: "
7971 "invalid sav->state "
7972 "(queue: %d SA: %d)\n",
7973 state
, sav
->state
));
7977 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
7978 key_freesav(sav
, KEY_SADB_LOCKED
);
7982 lck_mtx_unlock(sadb_mutex
);
7986 struct sadb_msg
*newmsg
;
7987 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_ADDRESS_SRC
,
7988 SADB_EXT_ADDRESS_DST
};
7990 /* create new sadb_msg to reply. */
7991 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
) / sizeof(int), mbufItems
);
7993 return key_senderror(so
, m
, ENOBUFS
);
7996 if (n
->m_len
< sizeof(struct sadb_msg
)) {
7997 n
= m_pullup(n
, sizeof(struct sadb_msg
));
7999 return key_senderror(so
, m
, ENOBUFS
);
8002 newmsg
= mtod(n
, struct sadb_msg
*);
8003 newmsg
->sadb_msg_errno
= 0;
8004 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
8007 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
8012 * SADB_GET processing
8014 * <base, SA(*), address(SD)>
8015 * from the ikmpd, and get a SP and a SA to respond,
8017 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
8018 * (identity(SD),) (sensitivity)>
8021 * m will always be freed.
8027 const struct sadb_msghdr
*mhp
)
8029 struct sadb_sa
*sa0
;
8030 struct sadb_address
*src0
, *dst0
;
8031 ifnet_t ipsec_if
= NULL
;
8032 struct secasindex saidx
;
8033 struct secashead
*sah
;
8034 struct secasvar
*sav
= NULL
;
8037 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
8040 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
8041 panic("key_get: NULL pointer is passed.\n");
8044 /* map satype to proto */
8045 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
8046 ipseclog((LOG_DEBUG
, "key_get: invalid satype is passed.\n"));
8047 return key_senderror(so
, m
, EINVAL
);
8050 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
8051 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
8052 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
8053 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
8054 return key_senderror(so
, m
, EINVAL
);
8056 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
8057 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
8058 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
8059 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
8060 return key_senderror(so
, m
, EINVAL
);
8063 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
8064 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
8065 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
8066 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
8068 /* XXX boundary check against sa_len */
8069 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
8071 lck_mtx_lock(sadb_mutex
);
8073 /* get a SA header */
8074 LIST_FOREACH(sah
, &sahtree
, chain
) {
8075 if (sah
->state
== SADB_SASTATE_DEAD
) {
8078 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0) {
8082 /* get a SA with SPI. */
8083 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
8089 lck_mtx_unlock(sadb_mutex
);
8090 ipseclog((LOG_DEBUG
, "key_get: no SA found.\n"));
8091 return key_senderror(so
, m
, ENOENT
);
8098 /* map proto to satype */
8099 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
8100 lck_mtx_unlock(sadb_mutex
);
8101 ipseclog((LOG_DEBUG
, "key_get: there was invalid proto in SAD.\n"));
8102 return key_senderror(so
, m
, EINVAL
);
8104 lck_mtx_unlock(sadb_mutex
);
8106 /* create new sadb_msg to reply. */
8107 n
= key_setdumpsa(sav
, SADB_GET
, satype
, mhp
->msg
->sadb_msg_seq
,
8108 mhp
->msg
->sadb_msg_pid
);
8113 return key_senderror(so
, m
, ENOBUFS
);
8117 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
8122 * get SA stats by spi.
8123 * OUT: -1 : not found
8124 * 0 : found, arg pointer to a SA stats is updated.
8127 key_getsastatbyspi_one(u_int32_t spi
,
8128 struct sastat
*stat
)
8130 struct secashead
*sah
;
8131 struct secasvar
*sav
= NULL
;
8133 if ((void *)stat
== NULL
) {
8137 lck_mtx_lock(sadb_mutex
);
8139 /* get a SA header */
8140 LIST_FOREACH(sah
, &sahtree
, chain
) {
8141 if (sah
->state
== SADB_SASTATE_DEAD
) {
8145 /* get a SA with SPI. */
8146 sav
= key_getsavbyspi(sah
, spi
);
8148 stat
->spi
= sav
->spi
;
8149 stat
->created
= sav
->created
;
8151 bcopy(sav
->lft_c
, &stat
->lft_c
, sizeof(stat
->lft_c
));
8153 bzero(&stat
->lft_c
, sizeof(stat
->lft_c
));
8155 lck_mtx_unlock(sadb_mutex
);
8160 lck_mtx_unlock(sadb_mutex
);
8166 * get SA stats collection by indices.
8167 * OUT: -1 : not found
8168 * 0 : found, arg pointers to a SA stats and 'maximum stats' are updated.
8171 key_getsastatbyspi(struct sastat
*stat_arg
,
8172 u_int32_t max_stat_arg
,
8173 struct sastat
*stat_res
,
8174 u_int32_t stat_res_size
,
8175 u_int32_t
*max_stat_res
)
8179 if (stat_arg
== NULL
||
8181 max_stat_res
== NULL
) {
8185 u_int32_t max_stats
= stat_res_size
/ (sizeof(struct sastat
));
8186 max_stats
= ((max_stat_arg
<= max_stats
) ? max_stat_arg
: max_stats
);
8188 for (cur
= 0; cur
< max_stats
; cur
++) {
8189 if (key_getsastatbyspi_one(stat_arg
[cur
].spi
,
8190 &stat_res
[found
]) == 0) {
8194 *max_stat_res
= found
;
8202 /* XXX make it sysctl-configurable? */
8204 key_getcomb_setlifetime(
8205 struct sadb_comb
*comb
)
8207 comb
->sadb_comb_soft_allocations
= 1;
8208 comb
->sadb_comb_hard_allocations
= 1;
8209 comb
->sadb_comb_soft_bytes
= 0;
8210 comb
->sadb_comb_hard_bytes
= 0;
8211 comb
->sadb_comb_hard_addtime
= 86400; /* 1 day */
8212 comb
->sadb_comb_soft_addtime
= comb
->sadb_comb_soft_addtime
* 80 / 100;
8213 comb
->sadb_comb_soft_usetime
= 28800; /* 8 hours */
8214 comb
->sadb_comb_hard_usetime
= comb
->sadb_comb_hard_usetime
* 80 / 100;
8219 * XXX reorder combinations by preference
8220 * XXX no idea if the user wants ESP authentication or not
8222 static struct mbuf
*
8223 key_getcomb_esp(void)
8225 struct sadb_comb
*comb
;
8226 const struct esp_algorithm
*algo
;
8227 struct mbuf
*result
= NULL
, *m
, *n
;
8231 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
8234 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
8235 algo
= esp_algorithm_lookup(i
);
8240 if (algo
->keymax
< ipsec_esp_keymin
) {
8243 if (algo
->keymin
< ipsec_esp_keymin
) {
8244 encmin
= ipsec_esp_keymin
;
8246 encmin
= algo
->keymin
;
8249 if (ipsec_esp_auth
) {
8250 m
= key_getcomb_ah();
8254 panic("assumption failed in key_getcomb_esp");
8257 MGET(m
, M_WAITOK
, MT_DATA
);
8262 bzero(mtod(m
, caddr_t
), m
->m_len
);
8270 for (n
= m
; n
; n
= n
->m_next
) {
8275 panic("assumption failed in key_getcomb_esp");
8279 for (off
= 0; off
< totlen
; off
+= l
) {
8280 n
= m_pulldown(m
, off
, l
, &o
);
8282 /* m is already freed */
8285 comb
= (struct sadb_comb
*)
8286 (void *)(mtod(n
, caddr_t
) + o
);
8287 bzero(comb
, sizeof(*comb
));
8288 key_getcomb_setlifetime(comb
);
8289 comb
->sadb_comb_encrypt
= i
;
8290 comb
->sadb_comb_encrypt_minbits
= encmin
;
8291 comb
->sadb_comb_encrypt_maxbits
= algo
->keymax
;
8312 * XXX reorder combinations by preference
8314 static struct mbuf
*
8315 key_getcomb_ah(void)
8317 struct sadb_comb
*comb
;
8318 const struct ah_algorithm
*algo
;
8322 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
8325 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
8327 /* we prefer HMAC algorithms, not old algorithms */
8328 if (i
!= SADB_AALG_SHA1HMAC
&& i
!= SADB_AALG_MD5HMAC
) {
8332 algo
= ah_algorithm_lookup(i
);
8337 if (algo
->keymax
< ipsec_ah_keymin
) {
8340 if (algo
->keymin
< ipsec_ah_keymin
) {
8341 keymin
= ipsec_ah_keymin
;
8343 keymin
= algo
->keymin
;
8349 panic("assumption failed in key_getcomb_ah");
8352 MGET(m
, M_WAITOK
, MT_DATA
);
8359 M_PREPEND(m
, l
, M_WAITOK
, 1);
8365 comb
= mtod(m
, struct sadb_comb
*);
8366 bzero(comb
, sizeof(*comb
));
8367 key_getcomb_setlifetime(comb
);
8368 comb
->sadb_comb_auth
= i
;
8369 comb
->sadb_comb_auth_minbits
= keymin
;
8370 comb
->sadb_comb_auth_maxbits
= algo
->keymax
;
8377 * XXX no way to pass mode (transport/tunnel) to userland
8378 * XXX replay checking?
8379 * XXX sysctl interface to ipsec_{ah,esp}_keymin
8381 static struct mbuf
*
8383 const struct secasindex
*saidx
)
8385 struct sadb_prop
*prop
;
8387 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_prop
));
8390 switch (saidx
->proto
) {
8393 m
= key_getcomb_esp();
8397 m
= key_getcomb_ah();
8406 M_PREPEND(m
, l
, M_WAITOK
, 1);
8412 for (n
= m
; n
; n
= n
->m_next
) {
8416 prop
= mtod(m
, struct sadb_prop
*);
8417 bzero(prop
, sizeof(*prop
));
8418 prop
->sadb_prop_len
= PFKEY_UNIT64(totlen
);
8419 prop
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
8420 prop
->sadb_prop_replay
= 32; /* XXX */
8426 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
8428 * <base, SA, address(SD), (address(P)), x_policy,
8429 * (identity(SD),) (sensitivity,) proposal>
8430 * to KMD, and expect to receive
8431 * <base> with SADB_ACQUIRE if error occurred,
8433 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
8434 * from KMD by PF_KEY.
8436 * XXX x_policy is outside of RFC2367 (KAME extension).
8437 * XXX sensitivity is not supported.
8441 * others: error number
8445 struct secasindex
*saidx
,
8446 struct secpolicy
*sp
)
8448 struct mbuf
*result
= NULL
, *m
;
8449 #ifndef IPSEC_NONBLOCK_ACQUIRE
8450 struct secacq
*newacq
;
8456 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
8459 if (saidx
== NULL
) {
8460 panic("key_acquire: NULL pointer is passed.\n");
8462 if ((satype
= key_proto2satype(saidx
->proto
)) == 0) {
8463 panic("key_acquire: invalid proto is passed.\n");
8466 #ifndef IPSEC_NONBLOCK_ACQUIRE
8468 * We never do anything about acquirng SA. There is anather
8469 * solution that kernel blocks to send SADB_ACQUIRE message until
8470 * getting something message from IKEd. In later case, to be
8471 * managed with ACQUIRING list.
8473 /* get a entry to check whether sending message or not. */
8474 lck_mtx_lock(sadb_mutex
);
8475 if ((newacq
= key_getacq(saidx
)) != NULL
) {
8476 if (key_blockacq_count
< newacq
->count
) {
8477 /* reset counter and do send message. */
8480 /* increment counter and do nothing. */
8482 lck_mtx_unlock(sadb_mutex
);
8486 /* make new entry for blocking to send SADB_ACQUIRE. */
8487 if ((newacq
= key_newacq(saidx
)) == NULL
) {
8488 lck_mtx_unlock(sadb_mutex
);
8492 /* add to acqtree */
8493 LIST_INSERT_HEAD(&acqtree
, newacq
, chain
);
8494 key_start_timehandler();
8497 lck_mtx_unlock(sadb_mutex
);
8500 seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
8502 m
= key_setsadbmsg(SADB_ACQUIRE
, 0, satype
, seq
, 0, 0);
8509 /* set sadb_address for saidx's. */
8510 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
8511 (struct sockaddr
*)&saidx
->src
, FULLMASK
, IPSEC_ULPROTO_ANY
);
8518 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
8519 (struct sockaddr
*)&saidx
->dst
, FULLMASK
, IPSEC_ULPROTO_ANY
);
8526 /* XXX proxy address (optional) */
8528 /* set sadb_x_policy */
8530 m
= key_setsadbxpolicy(sp
->policy
, sp
->spidx
.dir
, sp
->id
);
8538 /* XXX identity (optional) */
8540 if (idexttype
&& fqdn
) {
8541 /* create identity extension (FQDN) */
8542 struct sadb_ident
*id
;
8545 fqdnlen
= strlen(fqdn
) + 1; /* +1 for terminating-NUL */
8546 id
= (struct sadb_ident
*)p
;
8547 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
8548 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
8549 id
->sadb_ident_exttype
= idexttype
;
8550 id
->sadb_ident_type
= SADB_IDENTTYPE_FQDN
;
8551 bcopy(fqdn
, id
+ 1, fqdnlen
);
8552 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(fqdnlen
);
8556 /* create identity extension (USERFQDN) */
8557 struct sadb_ident
*id
;
8561 /* +1 for terminating-NUL */
8562 userfqdnlen
= strlen(userfqdn
) + 1;
8566 id
= (struct sadb_ident
*)p
;
8567 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
8568 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
8569 id
->sadb_ident_exttype
= idexttype
;
8570 id
->sadb_ident_type
= SADB_IDENTTYPE_USERFQDN
;
8571 /* XXX is it correct? */
8572 if (curproc
&& curproc
->p_cred
) {
8573 id
->sadb_ident_id
= curproc
->p_cred
->p_ruid
;
8575 if (userfqdn
&& userfqdnlen
) {
8576 bcopy(userfqdn
, id
+ 1, userfqdnlen
);
8578 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(userfqdnlen
);
8582 /* XXX sensitivity (optional) */
8584 /* create proposal/combination extension */
8585 m
= key_getprop(saidx
);
8587 * outside of spec; make proposal/combination extension optional.
8593 if ((result
->m_flags
& M_PKTHDR
) == 0) {
8598 if (result
->m_len
< sizeof(struct sadb_msg
)) {
8599 result
= m_pullup(result
, sizeof(struct sadb_msg
));
8600 if (result
== NULL
) {
8606 result
->m_pkthdr
.len
= 0;
8607 for (m
= result
; m
; m
= m
->m_next
) {
8608 result
->m_pkthdr
.len
+= m
->m_len
;
8611 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
8612 PFKEY_UNIT64(result
->m_pkthdr
.len
);
8614 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
8623 #ifndef IPSEC_NONBLOCK_ACQUIRE
8624 static struct secacq
*
8626 struct secasindex
*saidx
)
8628 struct secacq
*newacq
;
8632 KMALLOC_NOWAIT(newacq
, struct secacq
*, sizeof(struct secacq
));
8633 if (newacq
== NULL
) {
8634 lck_mtx_unlock(sadb_mutex
);
8635 KMALLOC_WAIT(newacq
, struct secacq
*, sizeof(struct secacq
));
8636 lck_mtx_lock(sadb_mutex
);
8637 if (newacq
== NULL
) {
8638 ipseclog((LOG_DEBUG
, "key_newacq: No more memory.\n"));
8642 bzero(newacq
, sizeof(*newacq
));
8645 bcopy(saidx
, &newacq
->saidx
, sizeof(newacq
->saidx
));
8646 newacq
->seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
);
8648 newacq
->created
= tv
.tv_sec
;
8654 static struct secacq
*
8656 struct secasindex
*saidx
)
8660 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
8662 LIST_FOREACH(acq
, &acqtree
, chain
) {
8663 if (key_cmpsaidx(saidx
, &acq
->saidx
, CMP_EXACTLY
)) {
8671 static struct secacq
*
8677 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
8679 LIST_FOREACH(acq
, &acqtree
, chain
) {
8680 if (acq
->seq
== seq
) {
8689 static struct secspacq
*
8691 struct secpolicyindex
*spidx
)
8693 struct secspacq
*acq
;
8697 KMALLOC_NOWAIT(acq
, struct secspacq
*, sizeof(struct secspacq
));
8699 lck_mtx_unlock(sadb_mutex
);
8700 KMALLOC_WAIT(acq
, struct secspacq
*, sizeof(struct secspacq
));
8701 lck_mtx_lock(sadb_mutex
);
8703 ipseclog((LOG_DEBUG
, "key_newspacq: No more memory.\n"));
8707 bzero(acq
, sizeof(*acq
));
8710 bcopy(spidx
, &acq
->spidx
, sizeof(acq
->spidx
));
8712 acq
->created
= tv
.tv_sec
;
8718 static struct secspacq
*
8720 struct secpolicyindex
*spidx
)
8722 struct secspacq
*acq
;
8724 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
8726 LIST_FOREACH(acq
, &spacqtree
, chain
) {
8727 if (key_cmpspidx_exactly(spidx
, &acq
->spidx
)) {
8736 * SADB_ACQUIRE processing,
8737 * in first situation, is receiving
8739 * from the ikmpd, and clear sequence of its secasvar entry.
8741 * In second situation, is receiving
8742 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
8743 * from a user land process, and return
8744 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
8747 * m will always be freed.
8753 const struct sadb_msghdr
*mhp
)
8755 const struct sadb_address
*src0
, *dst0
;
8756 ifnet_t ipsec_if
= NULL
;
8757 struct secasindex saidx
;
8758 struct secashead
*sah
;
8764 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
8765 panic("key_acquire2: NULL pointer is passed.\n");
8769 * Error message from KMd.
8770 * We assume that if error was occurred in IKEd, the length of PFKEY
8771 * message is equal to the size of sadb_msg structure.
8772 * We do not raise error even if error occurred in this function.
8774 lck_mtx_lock(sadb_mutex
);
8776 if (mhp
->msg
->sadb_msg_len
== PFKEY_UNIT64(sizeof(struct sadb_msg
))) {
8777 #ifndef IPSEC_NONBLOCK_ACQUIRE
8781 /* check sequence number */
8782 if (mhp
->msg
->sadb_msg_seq
== 0) {
8783 lck_mtx_unlock(sadb_mutex
);
8784 ipseclog((LOG_DEBUG
, "key_acquire2: must specify sequence number.\n"));
8789 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) == NULL
) {
8791 * the specified larval SA is already gone, or we got
8792 * a bogus sequence number. we can silently ignore it.
8794 lck_mtx_unlock(sadb_mutex
);
8799 /* reset acq counter in order to deletion by timehander. */
8801 acq
->created
= tv
.tv_sec
;
8804 lck_mtx_unlock(sadb_mutex
);
8810 * This message is from user land.
8813 /* map satype to proto */
8814 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
8815 lck_mtx_unlock(sadb_mutex
);
8816 ipseclog((LOG_DEBUG
, "key_acquire2: invalid satype is passed.\n"));
8817 return key_senderror(so
, m
, EINVAL
);
8820 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
8821 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
8822 mhp
->ext
[SADB_EXT_PROPOSAL
] == NULL
) {
8824 lck_mtx_unlock(sadb_mutex
);
8825 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
8826 return key_senderror(so
, m
, EINVAL
);
8828 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
8829 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
8830 mhp
->extlen
[SADB_EXT_PROPOSAL
] < sizeof(struct sadb_prop
)) {
8832 lck_mtx_unlock(sadb_mutex
);
8833 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
8834 return key_senderror(so
, m
, EINVAL
);
8837 src0
= (const struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
8838 dst0
= (const struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
8839 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
8841 /* XXX boundary check against sa_len */
8843 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
8845 /* get a SA index */
8846 LIST_FOREACH(sah
, &sahtree
, chain
) {
8847 if (sah
->state
== SADB_SASTATE_DEAD
) {
8850 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_MODE
| CMP_REQID
)) {
8855 lck_mtx_unlock(sadb_mutex
);
8856 ipseclog((LOG_DEBUG
, "key_acquire2: a SA exists already.\n"));
8857 return key_senderror(so
, m
, EEXIST
);
8859 lck_mtx_unlock(sadb_mutex
);
8860 error
= key_acquire(&saidx
, NULL
);
8862 ipseclog((LOG_DEBUG
, "key_acquire2: error %d returned "
8863 "from key_acquire.\n", mhp
->msg
->sadb_msg_errno
));
8864 return key_senderror(so
, m
, error
);
8867 return key_sendup_mbuf(so
, m
, KEY_SENDUP_REGISTERED
);
8871 * SADB_REGISTER processing.
8872 * If SATYPE_UNSPEC has been passed as satype, only return sadb_supported.
8875 * from the ikmpd, and register a socket to send PF_KEY messages,
8879 * If socket is detached, must free from regnode.
8881 * m will always be freed.
8887 const struct sadb_msghdr
*mhp
)
8889 struct secreg
*reg
, *newreg
= 0;
8892 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
8893 panic("key_register: NULL pointer is passed.\n");
8896 /* check for invalid register message */
8897 if (mhp
->msg
->sadb_msg_satype
>= sizeof(regtree
) / sizeof(regtree
[0])) {
8898 return key_senderror(so
, m
, EINVAL
);
8901 /* When SATYPE_UNSPEC is specified, only return sadb_supported. */
8902 if (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
) {
8906 /* create regnode */
8907 KMALLOC_WAIT(newreg
, struct secreg
*, sizeof(*newreg
));
8908 if (newreg
== NULL
) {
8909 ipseclog((LOG_DEBUG
, "key_register: No more memory.\n"));
8910 return key_senderror(so
, m
, ENOBUFS
);
8912 bzero((caddr_t
)newreg
, sizeof(*newreg
));
8914 lck_mtx_lock(sadb_mutex
);
8915 /* check whether existing or not */
8916 LIST_FOREACH(reg
, ®tree
[mhp
->msg
->sadb_msg_satype
], chain
) {
8917 if (reg
->so
== so
) {
8918 lck_mtx_unlock(sadb_mutex
);
8919 ipseclog((LOG_DEBUG
, "key_register: socket exists already.\n"));
8921 return key_senderror(so
, m
, EEXIST
);
8927 ((struct keycb
*)sotorawcb(so
))->kp_registered
++;
8928 socket_unlock(so
, 1);
8930 /* add regnode to regtree. */
8931 LIST_INSERT_HEAD(®tree
[mhp
->msg
->sadb_msg_satype
], newreg
, chain
);
8932 lck_mtx_unlock(sadb_mutex
);
8936 struct sadb_msg
*newmsg
;
8937 struct sadb_supported
*sup
;
8938 u_int len
, alen
, elen
;
8941 struct sadb_alg
*alg
;
8943 /* create new sadb_msg to reply. */
8945 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
8946 if (ah_algorithm_lookup(i
)) {
8947 alen
+= sizeof(struct sadb_alg
);
8951 alen
+= sizeof(struct sadb_supported
);
8955 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
8956 if (esp_algorithm_lookup(i
)) {
8957 elen
+= sizeof(struct sadb_alg
);
8961 elen
+= sizeof(struct sadb_supported
);
8965 len
= sizeof(struct sadb_msg
) + alen
+ elen
;
8967 if (len
> MCLBYTES
) {
8968 return key_senderror(so
, m
, ENOBUFS
);
8971 MGETHDR(n
, M_WAITOK
, MT_DATA
);
8972 if (n
&& len
> MHLEN
) {
8973 MCLGET(n
, M_WAITOK
);
8974 if ((n
->m_flags
& M_EXT
) == 0) {
8980 return key_senderror(so
, m
, ENOBUFS
);
8983 n
->m_pkthdr
.len
= n
->m_len
= len
;
8987 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
8988 newmsg
= mtod(n
, struct sadb_msg
*);
8989 newmsg
->sadb_msg_errno
= 0;
8990 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
8991 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
8993 /* for authentication algorithm */
8995 sup
= (struct sadb_supported
*)(void *)(mtod(n
, caddr_t
) + off
);
8996 sup
->sadb_supported_len
= PFKEY_UNIT64(alen
);
8997 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
8998 off
+= PFKEY_ALIGN8(sizeof(*sup
));
9000 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
9001 const struct ah_algorithm
*aalgo
;
9003 aalgo
= ah_algorithm_lookup(i
);
9007 alg
= (struct sadb_alg
*)
9008 (void *)(mtod(n
, caddr_t
) + off
);
9009 alg
->sadb_alg_id
= i
;
9010 alg
->sadb_alg_ivlen
= 0;
9011 alg
->sadb_alg_minbits
= aalgo
->keymin
;
9012 alg
->sadb_alg_maxbits
= aalgo
->keymax
;
9013 off
+= PFKEY_ALIGN8(sizeof(*alg
));
9018 /* for encryption algorithm */
9020 sup
= (struct sadb_supported
*)(void *)(mtod(n
, caddr_t
) + off
);
9021 sup
->sadb_supported_len
= PFKEY_UNIT64(elen
);
9022 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
9023 off
+= PFKEY_ALIGN8(sizeof(*sup
));
9025 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
9026 const struct esp_algorithm
*ealgo
;
9028 ealgo
= esp_algorithm_lookup(i
);
9032 alg
= (struct sadb_alg
*)
9033 (void *)(mtod(n
, caddr_t
) + off
);
9034 alg
->sadb_alg_id
= i
;
9035 if (ealgo
&& ealgo
->ivlen
) {
9037 * give NULL to get the value preferred by
9038 * algorithm XXX SADB_X_EXT_DERIV ?
9040 alg
->sadb_alg_ivlen
=
9041 (*ealgo
->ivlen
)(ealgo
, NULL
);
9043 alg
->sadb_alg_ivlen
= 0;
9045 alg
->sadb_alg_minbits
= ealgo
->keymin
;
9046 alg
->sadb_alg_maxbits
= ealgo
->keymax
;
9047 off
+= PFKEY_ALIGN8(sizeof(struct sadb_alg
));
9054 panic("length assumption failed in key_register");
9059 return key_sendup_mbuf(so
, n
, KEY_SENDUP_REGISTERED
);
9064 key_delete_all_for_socket(struct socket
*so
)
9066 struct secashead
*sah
, *nextsah
;
9067 struct secasvar
*sav
, *nextsav
;
9071 for (sah
= LIST_FIRST(&sahtree
);
9074 nextsah
= LIST_NEXT(sah
, chain
);
9075 for (stateidx
= 0; stateidx
< _ARRAYLEN(saorder_state_alive
); stateidx
++) {
9076 state
= saorder_state_any
[stateidx
];
9077 for (sav
= LIST_FIRST(&sah
->savtree
[state
]); sav
!= NULL
; sav
= nextsav
) {
9078 nextsav
= LIST_NEXT(sav
, chain
);
9079 if (sav
->flags2
& SADB_X_EXT_SA2_DELETE_ON_DETACH
&&
9081 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
9082 key_freesav(sav
, KEY_SADB_LOCKED
);
9090 * free secreg entry registered.
9091 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
9102 panic("key_freereg: NULL pointer is passed.\n");
9106 * check whether existing or not.
9107 * check all type of SA, because there is a potential that
9108 * one socket is registered to multiple type of SA.
9110 lck_mtx_lock(sadb_mutex
);
9111 key_delete_all_for_socket(so
);
9112 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
9113 LIST_FOREACH(reg
, ®tree
[i
], chain
) {
9115 && __LIST_CHAINED(reg
)) {
9116 LIST_REMOVE(reg
, chain
);
9122 lck_mtx_unlock(sadb_mutex
);
9127 * SADB_EXPIRE processing
9129 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
9131 * NOTE: We send only soft lifetime extension.
9134 * others : error number
9138 struct secasvar
*sav
)
9141 struct mbuf
*result
= NULL
, *m
;
9144 struct sadb_lifetime
*lt
;
9146 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
9150 panic("key_expire: NULL pointer is passed.\n");
9152 if (sav
->sah
== NULL
) {
9153 panic("key_expire: Why was SA index in SA NULL.\n");
9155 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0) {
9156 panic("key_expire: invalid proto is passed.\n");
9159 /* set msg header */
9160 m
= key_setsadbmsg(SADB_EXPIRE
, 0, satype
, sav
->seq
, 0, sav
->refcnt
);
9167 /* create SA extension */
9168 m
= key_setsadbsa(sav
);
9175 /* create SA extension */
9176 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
9177 sav
->replay
[0] ? sav
->replay
[0]->count
: 0,
9178 sav
->sah
->saidx
.reqid
,
9186 /* create lifetime extension (current and soft) */
9187 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
9188 m
= key_alloc_mbuf(len
);
9189 if (!m
|| m
->m_next
) { /*XXX*/
9196 bzero(mtod(m
, caddr_t
), len
);
9197 lt
= mtod(m
, struct sadb_lifetime
*);
9198 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
9199 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
9200 lt
->sadb_lifetime_allocations
= sav
->lft_c
->sadb_lifetime_allocations
;
9201 lt
->sadb_lifetime_bytes
= sav
->lft_c
->sadb_lifetime_bytes
;
9202 lt
->sadb_lifetime_addtime
= sav
->lft_c
->sadb_lifetime_addtime
;
9203 lt
->sadb_lifetime_usetime
= sav
->lft_c
->sadb_lifetime_usetime
;
9204 lt
= (struct sadb_lifetime
*)(void *)(mtod(m
, caddr_t
) + len
/ 2);
9205 bcopy(sav
->lft_s
, lt
, sizeof(*lt
));
9208 /* set sadb_address for source */
9209 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
9210 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
9211 FULLMASK
, IPSEC_ULPROTO_ANY
);
9218 /* set sadb_address for destination */
9219 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
9220 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
9221 FULLMASK
, IPSEC_ULPROTO_ANY
);
9228 if ((result
->m_flags
& M_PKTHDR
) == 0) {
9233 if (result
->m_len
< sizeof(struct sadb_msg
)) {
9234 result
= m_pullup(result
, sizeof(struct sadb_msg
));
9235 if (result
== NULL
) {
9241 result
->m_pkthdr
.len
= 0;
9242 for (m
= result
; m
; m
= m
->m_next
) {
9243 result
->m_pkthdr
.len
+= m
->m_len
;
9246 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
9247 PFKEY_UNIT64(result
->m_pkthdr
.len
);
9249 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
9259 * SADB_FLUSH processing
9262 * from the ikmpd, and free all entries in secastree.
9266 * NOTE: to do is only marking SADB_SASTATE_DEAD.
9268 * m will always be freed.
9274 const struct sadb_msghdr
*mhp
)
9276 struct sadb_msg
*newmsg
;
9277 struct secashead
*sah
, *nextsah
;
9278 struct secasvar
*sav
, *nextsav
;
9284 if (so
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
9285 panic("key_flush: NULL pointer is passed.\n");
9288 /* map satype to proto */
9289 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
9290 ipseclog((LOG_DEBUG
, "key_flush: invalid satype is passed.\n"));
9291 return key_senderror(so
, m
, EINVAL
);
9294 lck_mtx_lock(sadb_mutex
);
9296 /* no SATYPE specified, i.e. flushing all SA. */
9297 for (sah
= LIST_FIRST(&sahtree
);
9300 nextsah
= LIST_NEXT(sah
, chain
);
9302 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
9303 && proto
!= sah
->saidx
.proto
) {
9308 stateidx
< _ARRAYLEN(saorder_state_alive
);
9310 state
= saorder_state_any
[stateidx
];
9311 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
9314 nextsav
= LIST_NEXT(sav
, chain
);
9316 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
9317 key_freesav(sav
, KEY_SADB_LOCKED
);
9321 sah
->state
= SADB_SASTATE_DEAD
;
9323 lck_mtx_unlock(sadb_mutex
);
9325 if (m
->m_len
< sizeof(struct sadb_msg
) ||
9326 sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
9327 ipseclog((LOG_DEBUG
, "key_flush: No more memory.\n"));
9328 return key_senderror(so
, m
, ENOBUFS
);
9335 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct sadb_msg
);
9336 newmsg
= mtod(m
, struct sadb_msg
*);
9337 newmsg
->sadb_msg_errno
= 0;
9338 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
9340 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
9344 * SADB_DUMP processing
9345 * dump all entries including status of DEAD in SAD.
9348 * from the ikmpd, and dump all secasvar leaves
9353 * m will always be freed.
9356 struct sav_dump_elem
{
9357 struct secasvar
*sav
;
9365 const struct sadb_msghdr
*mhp
)
9367 struct secashead
*sah
;
9368 struct secasvar
*sav
;
9369 struct sav_dump_elem
*savbuf
= NULL
, *elem_ptr
;
9374 int cnt
= 0, cnt2
, bufcount
;
9378 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
9381 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
9382 panic("key_dump: NULL pointer is passed.\n");
9385 /* map satype to proto */
9386 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
9387 ipseclog((LOG_DEBUG
, "key_dump: invalid satype is passed.\n"));
9388 return key_senderror(so
, m
, EINVAL
);
9391 if ((bufcount
= ipsec_sav_count
) <= 0) {
9395 bufcount
+= 512; /* extra */
9396 KMALLOC_WAIT(savbuf
, struct sav_dump_elem
*, bufcount
* sizeof(struct sav_dump_elem
));
9397 if (savbuf
== NULL
) {
9398 ipseclog((LOG_DEBUG
, "key_dump: No more memory.\n"));
9403 /* count sav entries to be sent to the userland. */
9404 lck_mtx_lock(sadb_mutex
);
9406 LIST_FOREACH(sah
, &sahtree
, chain
) {
9407 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
9408 && proto
!= sah
->saidx
.proto
) {
9412 /* map proto to satype */
9413 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
9414 lck_mtx_unlock(sadb_mutex
);
9415 ipseclog((LOG_DEBUG
, "key_dump: there was invalid proto in SAD.\n"));
9421 stateidx
< _ARRAYLEN(saorder_state_any
);
9423 state
= saorder_state_any
[stateidx
];
9424 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
9425 if (cnt
== bufcount
) {
9426 break; /* out of buffer space */
9428 elem_ptr
->sav
= sav
;
9429 elem_ptr
->satype
= satype
;
9436 lck_mtx_unlock(sadb_mutex
);
9443 /* send this to the userland, one at a time. */
9447 n
= key_setdumpsa(elem_ptr
->sav
, SADB_DUMP
, elem_ptr
->satype
,
9448 --cnt2
, mhp
->msg
->sadb_msg_pid
);
9455 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
9463 lck_mtx_lock(sadb_mutex
);
9465 key_freesav((elem_ptr
++)->sav
, KEY_SADB_LOCKED
);
9467 lck_mtx_unlock(sadb_mutex
);
9473 return key_senderror(so
, m
, error
);
9481 * SADB_X_PROMISC processing
9483 * m will always be freed.
9489 const struct sadb_msghdr
*mhp
)
9494 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
9495 panic("key_promisc: NULL pointer is passed.\n");
9498 olen
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
9500 if (olen
< sizeof(struct sadb_msg
)) {
9502 return key_senderror(so
, m
, EINVAL
);
9507 } else if (olen
== sizeof(struct sadb_msg
)) {
9508 /* enable/disable promisc mode */
9512 if ((kp
= (struct keycb
*)sotorawcb(so
)) == NULL
) {
9513 return key_senderror(so
, m
, EINVAL
);
9515 mhp
->msg
->sadb_msg_errno
= 0;
9516 switch (mhp
->msg
->sadb_msg_satype
) {
9519 kp
->kp_promisc
= mhp
->msg
->sadb_msg_satype
;
9522 socket_unlock(so
, 1);
9523 return key_senderror(so
, m
, EINVAL
);
9525 socket_unlock(so
, 1);
9527 /* send the original message back to everyone */
9528 mhp
->msg
->sadb_msg_errno
= 0;
9529 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
9531 /* send packet as is */
9533 m_adj(m
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)));
9535 /* TODO: if sadb_msg_seq is specified, send to specific pid */
9536 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
9540 static int(*const key_typesw
[])(struct socket
*, struct mbuf
*,
9541 const struct sadb_msghdr
*) = {
9542 NULL
, /* SADB_RESERVED */
9543 key_getspi
, /* SADB_GETSPI */
9544 key_update
, /* SADB_UPDATE */
9545 key_add
, /* SADB_ADD */
9546 key_delete
, /* SADB_DELETE */
9547 key_get
, /* SADB_GET */
9548 key_acquire2
, /* SADB_ACQUIRE */
9549 key_register
, /* SADB_REGISTER */
9550 NULL
, /* SADB_EXPIRE */
9551 key_flush
, /* SADB_FLUSH */
9552 key_dump
, /* SADB_DUMP */
9553 key_promisc
, /* SADB_X_PROMISC */
9554 NULL
, /* SADB_X_PCHANGE */
9555 key_spdadd
, /* SADB_X_SPDUPDATE */
9556 key_spdadd
, /* SADB_X_SPDADD */
9557 key_spddelete
, /* SADB_X_SPDDELETE */
9558 key_spdget
, /* SADB_X_SPDGET */
9559 NULL
, /* SADB_X_SPDACQUIRE */
9560 key_spddump
, /* SADB_X_SPDDUMP */
9561 key_spdflush
, /* SADB_X_SPDFLUSH */
9562 key_spdadd
, /* SADB_X_SPDSETIDX */
9563 NULL
, /* SADB_X_SPDEXPIRE */
9564 key_spddelete2
, /* SADB_X_SPDDELETE2 */
9565 key_getsastat
, /* SADB_GETSASTAT */
9566 key_spdenable
, /* SADB_X_SPDENABLE */
9567 key_spddisable
, /* SADB_X_SPDDISABLE */
9568 key_migrate
, /* SADB_MIGRATE */
9572 bzero_mbuf(struct mbuf
*m
)
9574 struct mbuf
*mptr
= m
;
9575 struct sadb_msg
*msg
= NULL
;
9582 if (mptr
->m_len
>= sizeof(struct sadb_msg
)) {
9583 msg
= mtod(mptr
, struct sadb_msg
*);
9584 if (msg
->sadb_msg_type
!= SADB_ADD
&&
9585 msg
->sadb_msg_type
!= SADB_UPDATE
) {
9588 offset
= sizeof(struct sadb_msg
);
9590 bzero(mptr
->m_data
+ offset
, mptr
->m_len
- offset
);
9591 mptr
= mptr
->m_next
;
9592 while (mptr
!= NULL
) {
9593 bzero(mptr
->m_data
, mptr
->m_len
);
9594 mptr
= mptr
->m_next
;
9599 bzero_keys(const struct sadb_msghdr
*mh
)
9607 offset
= sizeof(struct sadb_key
);
9609 if (mh
->ext
[SADB_EXT_KEY_ENCRYPT
]) {
9610 struct sadb_key
*key
= (struct sadb_key
*)mh
->ext
[SADB_EXT_KEY_ENCRYPT
];
9611 extlen
= key
->sadb_key_bits
>> 3;
9613 if (mh
->extlen
[SADB_EXT_KEY_ENCRYPT
] >= offset
+ extlen
) {
9614 bzero((uint8_t *)mh
->ext
[SADB_EXT_KEY_ENCRYPT
] + offset
, extlen
);
9616 bzero(mh
->ext
[SADB_EXT_KEY_ENCRYPT
], mh
->extlen
[SADB_EXT_KEY_ENCRYPT
]);
9619 if (mh
->ext
[SADB_EXT_KEY_AUTH
]) {
9620 struct sadb_key
*key
= (struct sadb_key
*)mh
->ext
[SADB_EXT_KEY_AUTH
];
9621 extlen
= key
->sadb_key_bits
>> 3;
9623 if (mh
->extlen
[SADB_EXT_KEY_AUTH
] >= offset
+ extlen
) {
9624 bzero((uint8_t *)mh
->ext
[SADB_EXT_KEY_AUTH
] + offset
, extlen
);
9626 bzero(mh
->ext
[SADB_EXT_KEY_AUTH
], mh
->extlen
[SADB_EXT_KEY_AUTH
]);
9632 key_validate_address_pair(struct sadb_address
*src0
,
9633 struct sadb_address
*dst0
)
9637 /* check upper layer protocol */
9638 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
9639 ipseclog((LOG_DEBUG
, "key_parse: upper layer protocol mismatched.\n"));
9640 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9645 if (PFKEY_ADDR_SADDR(src0
)->sa_family
!=
9646 PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
9647 ipseclog((LOG_DEBUG
, "key_parse: address family mismatched.\n"));
9648 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9651 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
9652 PFKEY_ADDR_SADDR(dst0
)->sa_len
) {
9653 ipseclog((LOG_DEBUG
,
9654 "key_parse: address struct size mismatched.\n"));
9655 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9659 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
9661 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!= sizeof(struct sockaddr_in
)) {
9662 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9667 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!= sizeof(struct sockaddr_in6
)) {
9668 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9673 ipseclog((LOG_DEBUG
,
9674 "key_parse: unsupported address family.\n"));
9675 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9676 return EAFNOSUPPORT
;
9679 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
9681 plen
= sizeof(struct in_addr
) << 3;
9684 plen
= sizeof(struct in6_addr
) << 3;
9687 plen
= 0; /*fool gcc*/
9691 /* check max prefix length */
9692 if (src0
->sadb_address_prefixlen
> plen
||
9693 dst0
->sadb_address_prefixlen
> plen
) {
9694 ipseclog((LOG_DEBUG
,
9695 "key_parse: illegal prefixlen.\n"));
9696 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9701 * prefixlen == 0 is valid because there can be a case when
9702 * all addresses are matched.
9708 * parse sadb_msg buffer to process PFKEYv2,
9709 * and create a data to response if needed.
9710 * I think to be dealed with mbuf directly.
9712 * msgp : pointer to pointer to a received buffer pulluped.
9713 * This is rewrited to response.
9714 * so : pointer to socket.
9716 * length for buffer to send to user process.
9723 struct sadb_msg
*msg
;
9724 struct sadb_msghdr mh
;
9728 Boolean keyAligned
= FALSE
;
9730 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
9733 if (m
== NULL
|| so
== NULL
) {
9734 panic("key_parse: NULL pointer is passed.\n");
9737 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
9738 KEYDEBUG(KEYDEBUG_KEY_DUMP
,
9739 ipseclog((LOG_DEBUG
, "key_parse: passed sadb_msg\n"));
9743 if (m
->m_len
< sizeof(struct sadb_msg
)) {
9744 m
= m_pullup(m
, sizeof(struct sadb_msg
));
9749 msg
= mtod(m
, struct sadb_msg
*);
9750 orglen
= PFKEY_UNUNIT64(msg
->sadb_msg_len
);
9751 target
= KEY_SENDUP_ONE
;
9753 if ((m
->m_flags
& M_PKTHDR
) == 0 ||
9754 m
->m_pkthdr
.len
!= orglen
) {
9755 ipseclog((LOG_DEBUG
, "key_parse: invalid message length.\n"));
9756 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
9761 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
9762 ipseclog((LOG_DEBUG
,
9763 "key_parse: PF_KEY version %u is mismatched.\n",
9764 msg
->sadb_msg_version
));
9765 PFKEY_STAT_INCREMENT(pfkeystat
.out_invver
);
9770 if (msg
->sadb_msg_type
> SADB_MAX
) {
9771 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
9772 msg
->sadb_msg_type
));
9773 PFKEY_STAT_INCREMENT(pfkeystat
.out_invmsgtype
);
9778 /* for old-fashioned code - should be nuked */
9779 if (m
->m_pkthdr
.len
> MCLBYTES
) {
9786 MGETHDR(n
, M_WAITOK
, MT_DATA
);
9787 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
9788 MCLGET(n
, M_WAITOK
);
9789 if ((n
->m_flags
& M_EXT
) == 0) {
9799 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
9800 n
->m_pkthdr
.len
= n
->m_len
= m
->m_pkthdr
.len
;
9807 /* align the mbuf chain so that extensions are in contiguous region. */
9808 error
= key_align(m
, &mh
);
9813 if (m
->m_next
) { /*XXX*/
9823 switch (msg
->sadb_msg_satype
) {
9824 case SADB_SATYPE_UNSPEC
:
9825 switch (msg
->sadb_msg_type
) {
9833 ipseclog((LOG_DEBUG
, "key_parse: must specify satype "
9834 "when msg type=%u.\n", msg
->sadb_msg_type
));
9835 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
9840 case SADB_SATYPE_AH
:
9841 case SADB_SATYPE_ESP
:
9842 switch (msg
->sadb_msg_type
) {
9844 case SADB_X_SPDDELETE
:
9846 case SADB_X_SPDDUMP
:
9847 case SADB_X_SPDFLUSH
:
9848 case SADB_X_SPDSETIDX
:
9849 case SADB_X_SPDUPDATE
:
9850 case SADB_X_SPDDELETE2
:
9851 case SADB_X_SPDENABLE
:
9852 case SADB_X_SPDDISABLE
:
9853 ipseclog((LOG_DEBUG
, "key_parse: illegal satype=%u\n",
9854 msg
->sadb_msg_type
));
9855 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
9860 case SADB_SATYPE_RSVP
:
9861 case SADB_SATYPE_OSPFV2
:
9862 case SADB_SATYPE_RIPV2
:
9863 case SADB_SATYPE_MIP
:
9864 ipseclog((LOG_DEBUG
, "key_parse: type %u isn't supported.\n",
9865 msg
->sadb_msg_satype
));
9866 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
9869 case 1: /* XXX: What does it do? */
9870 if (msg
->sadb_msg_type
== SADB_X_PROMISC
) {
9875 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
9876 msg
->sadb_msg_satype
));
9877 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
9882 /* Validate address fields for matching families, lengths, etc. */
9883 void *src0
= mh
.ext
[SADB_EXT_ADDRESS_SRC
];
9884 void *dst0
= mh
.ext
[SADB_EXT_ADDRESS_DST
];
9885 if (mh
.ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
] != NULL
&&
9886 mh
.ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
] != NULL
) {
9887 error
= key_validate_address_pair((struct sadb_address
*)(mh
.ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
]),
9888 (struct sadb_address
*)(mh
.ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
]));
9894 src0
= mh
.ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
];
9897 if (mh
.ext
[SADB_X_EXT_ADDR_RANGE_DST_START
] != NULL
&&
9898 mh
.ext
[SADB_X_EXT_ADDR_RANGE_DST_END
] != NULL
) {
9899 error
= key_validate_address_pair((struct sadb_address
*)(mh
.ext
[SADB_X_EXT_ADDR_RANGE_DST_START
]),
9900 (struct sadb_address
*)(mh
.ext
[SADB_X_EXT_ADDR_RANGE_DST_END
]));
9906 dst0
= mh
.ext
[SADB_X_EXT_ADDR_RANGE_DST_START
];
9909 if (src0
!= NULL
&& dst0
!= NULL
) {
9910 error
= key_validate_address_pair((struct sadb_address
*)(src0
),
9911 (struct sadb_address
*)(dst0
));
9917 if (msg
->sadb_msg_type
>= sizeof(key_typesw
) / sizeof(key_typesw
[0]) ||
9918 key_typesw
[msg
->sadb_msg_type
] == NULL
) {
9919 PFKEY_STAT_INCREMENT(pfkeystat
.out_invmsgtype
);
9924 error
= (*key_typesw
[msg
->sadb_msg_type
])(so
, m
, &mh
);
9934 msg
->sadb_msg_errno
= error
;
9935 return key_sendup_mbuf(so
, m
, target
);
9944 struct sadb_msg
*msg
;
9946 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
9948 if (m
->m_len
< sizeof(struct sadb_msg
)) {
9949 panic("invalid mbuf passed to key_senderror");
9952 msg
= mtod(m
, struct sadb_msg
*);
9953 msg
->sadb_msg_errno
= code
;
9954 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
9958 * set the pointer to each header into message buffer.
9959 * m will be freed on error.
9960 * XXX larger-than-MCLBYTES extension?
9965 struct sadb_msghdr
*mhp
)
9968 struct sadb_ext
*ext
;
9974 if (m
== NULL
|| mhp
== NULL
) {
9975 panic("key_align: NULL pointer is passed.\n");
9977 if (m
->m_len
< sizeof(struct sadb_msg
)) {
9978 panic("invalid mbuf passed to key_align");
9982 bzero(mhp
, sizeof(*mhp
));
9984 mhp
->msg
= mtod(m
, struct sadb_msg
*);
9985 mhp
->ext
[0] = (struct sadb_ext
*)mhp
->msg
; /*XXX backward compat */
9987 end
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
9988 extlen
= end
; /*just in case extlen is not updated*/
9989 for (off
= sizeof(struct sadb_msg
); off
< end
; off
+= extlen
) {
9990 n
= m_pulldown(m
, off
, sizeof(struct sadb_ext
), &toff
);
9992 /* m is already freed */
9995 ext
= (struct sadb_ext
*)(void *)(mtod(n
, caddr_t
) + toff
);
9998 switch (ext
->sadb_ext_type
) {
10000 case SADB_EXT_ADDRESS_SRC
:
10001 case SADB_EXT_ADDRESS_DST
:
10002 case SADB_EXT_ADDRESS_PROXY
:
10003 case SADB_EXT_LIFETIME_CURRENT
:
10004 case SADB_EXT_LIFETIME_HARD
:
10005 case SADB_EXT_LIFETIME_SOFT
:
10006 case SADB_EXT_KEY_AUTH
:
10007 case SADB_EXT_KEY_ENCRYPT
:
10008 case SADB_EXT_IDENTITY_SRC
:
10009 case SADB_EXT_IDENTITY_DST
:
10010 case SADB_EXT_SENSITIVITY
:
10011 case SADB_EXT_PROPOSAL
:
10012 case SADB_EXT_SUPPORTED_AUTH
:
10013 case SADB_EXT_SUPPORTED_ENCRYPT
:
10014 case SADB_EXT_SPIRANGE
:
10015 case SADB_X_EXT_POLICY
:
10016 case SADB_X_EXT_SA2
:
10017 case SADB_EXT_SESSION_ID
:
10018 case SADB_EXT_SASTAT
:
10019 case SADB_X_EXT_IPSECIF
:
10020 case SADB_X_EXT_ADDR_RANGE_SRC_START
:
10021 case SADB_X_EXT_ADDR_RANGE_SRC_END
:
10022 case SADB_X_EXT_ADDR_RANGE_DST_START
:
10023 case SADB_X_EXT_ADDR_RANGE_DST_END
:
10024 case SADB_EXT_MIGRATE_ADDRESS_SRC
:
10025 case SADB_EXT_MIGRATE_ADDRESS_DST
:
10026 case SADB_X_EXT_MIGRATE_IPSECIF
:
10027 /* duplicate check */
10029 * XXX Are there duplication payloads of either
10030 * KEY_AUTH or KEY_ENCRYPT ?
10032 if (mhp
->ext
[ext
->sadb_ext_type
] != NULL
) {
10033 ipseclog((LOG_DEBUG
,
10034 "key_align: duplicate ext_type %u "
10035 "is passed.\n", ext
->sadb_ext_type
));
10038 PFKEY_STAT_INCREMENT(pfkeystat
.out_dupext
);
10043 ipseclog((LOG_DEBUG
,
10044 "key_align: invalid ext_type %u is passed.\n",
10045 ext
->sadb_ext_type
));
10048 PFKEY_STAT_INCREMENT(pfkeystat
.out_invexttype
);
10052 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
10054 if (key_validate_ext(ext
, extlen
)) {
10057 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
10061 n
= m_pulldown(m
, off
, extlen
, &toff
);
10063 /* m is already freed */
10066 ext
= (struct sadb_ext
*)(void *)(mtod(n
, caddr_t
) + toff
);
10068 mhp
->ext
[ext
->sadb_ext_type
] = ext
;
10069 mhp
->extoff
[ext
->sadb_ext_type
] = off
;
10070 mhp
->extlen
[ext
->sadb_ext_type
] = extlen
;
10076 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
10085 const struct sadb_ext
*ext
,
10088 struct sockaddr
*sa
;
10089 enum { NONE
, ADDR
} checktype
= NONE
;
10091 const int sal
= offsetof(struct sockaddr
, sa_len
) + sizeof(sa
->sa_len
);
10093 if (len
!= PFKEY_UNUNIT64(ext
->sadb_ext_len
)) {
10097 /* if it does not match minimum/maximum length, bail */
10098 if (ext
->sadb_ext_type
>= sizeof(minsize
) / sizeof(minsize
[0]) ||
10099 ext
->sadb_ext_type
>= sizeof(maxsize
) / sizeof(maxsize
[0])) {
10102 if (!minsize
[ext
->sadb_ext_type
] || len
< minsize
[ext
->sadb_ext_type
]) {
10105 if (maxsize
[ext
->sadb_ext_type
] && len
> maxsize
[ext
->sadb_ext_type
]) {
10109 /* more checks based on sadb_ext_type XXX need more */
10110 switch (ext
->sadb_ext_type
) {
10111 case SADB_EXT_ADDRESS_SRC
:
10112 case SADB_EXT_ADDRESS_DST
:
10113 case SADB_EXT_ADDRESS_PROXY
:
10114 case SADB_X_EXT_ADDR_RANGE_SRC_START
:
10115 case SADB_X_EXT_ADDR_RANGE_SRC_END
:
10116 case SADB_X_EXT_ADDR_RANGE_DST_START
:
10117 case SADB_X_EXT_ADDR_RANGE_DST_END
:
10118 case SADB_EXT_MIGRATE_ADDRESS_SRC
:
10119 case SADB_EXT_MIGRATE_ADDRESS_DST
:
10120 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_address
));
10123 case SADB_EXT_IDENTITY_SRC
:
10124 case SADB_EXT_IDENTITY_DST
:
10125 if (((struct sadb_ident
*)(uintptr_t)(size_t)ext
)->
10126 sadb_ident_type
== SADB_X_IDENTTYPE_ADDR
) {
10127 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_ident
));
10138 switch (checktype
) {
10142 sa
= (struct sockaddr
*)((caddr_t
)(uintptr_t)ext
+ baselen
);
10144 if (len
< baselen
+ sal
) {
10147 if (baselen
+ PFKEY_ALIGN8(sa
->sa_len
) != len
) {
10153 /* check key bits length */
10154 if (ext
->sadb_ext_type
== SADB_EXT_KEY_AUTH
||
10155 ext
->sadb_ext_type
== SADB_EXT_KEY_ENCRYPT
) {
10156 struct sadb_key
*key
= (struct sadb_key
*)(uintptr_t)ext
;
10157 if (len
< (sizeof(struct sadb_key
) + _KEYLEN(key
))) {
10166 * XXX: maybe This function is called after INBOUND IPsec processing.
10168 * Special check for tunnel-mode packets.
10169 * We must make some checks for consistency between inner and outer IP header.
10171 * xxx more checks to be provided
10174 key_checktunnelsanity(
10175 struct secasvar
*sav
,
10176 __unused u_int family
,
10177 __unused caddr_t src
,
10178 __unused caddr_t dst
)
10181 if (sav
->sah
== NULL
) {
10182 panic("sav->sah == NULL at key_checktunnelsanity");
10185 /* XXX: check inner IP header */
10190 /* record data transfer on SA, and update timestamps */
10193 struct secasvar
*sav
,
10197 panic("key_sa_recordxfer called with sav == NULL");
10200 panic("key_sa_recordxfer called with m == NULL");
10206 lck_mtx_lock(sadb_mutex
);
10208 * XXX Currently, there is a difference of bytes size
10209 * between inbound and outbound processing.
10211 sav
->lft_c
->sadb_lifetime_bytes
+= m
->m_pkthdr
.len
;
10212 /* to check bytes lifetime is done in key_timehandler(). */
10215 * We use the number of packets as the unit of
10216 * sadb_lifetime_allocations. We increment the variable
10217 * whenever {esp,ah}_{in,out}put is called.
10219 sav
->lft_c
->sadb_lifetime_allocations
++;
10220 /* XXX check for expires? */
10223 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
10224 * in seconds. HARD and SOFT lifetime are measured by the time
10225 * difference (again in seconds) from sadb_lifetime_usetime.
10229 * -----+-----+--------+---> t
10230 * <--------------> HARD
10236 sav
->lft_c
->sadb_lifetime_usetime
= tv
.tv_sec
;
10237 /* XXX check for expires? */
10239 lck_mtx_unlock(sadb_mutex
);
10246 key_sa_routechange(
10247 struct sockaddr
*dst
)
10249 struct secashead
*sah
;
10252 lck_mtx_lock(sadb_mutex
);
10253 LIST_FOREACH(sah
, &sahtree
, chain
) {
10254 ro
= (struct route
*)&sah
->sa_route
;
10255 if (ro
->ro_rt
&& dst
->sa_len
== ro
->ro_dst
.sa_len
10256 && bcmp(dst
, &ro
->ro_dst
, dst
->sa_len
) == 0) {
10260 lck_mtx_unlock(sadb_mutex
);
10267 struct secasvar
*sav
,
10271 panic("key_sa_chgstate called with sav == NULL");
10274 if (sav
->state
== state
) {
10278 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
10280 if (__LIST_CHAINED(sav
)) {
10281 LIST_REMOVE(sav
, chain
);
10284 sav
->state
= state
;
10285 LIST_INSERT_HEAD(&sav
->sah
->savtree
[state
], sav
, chain
);
10290 struct secasvar
*sav
)
10292 lck_mtx_lock(sadb_mutex
);
10294 panic("key_sa_stir_iv called with sav == NULL");
10296 key_randomfill(sav
->iv
, sav
->ivlen
);
10297 lck_mtx_unlock(sadb_mutex
);
10300 /* XXX too much? */
10301 static struct mbuf
*
10305 struct mbuf
*m
= NULL
, *n
;
10310 MGET(n
, M_DONTWAIT
, MT_DATA
);
10311 if (n
&& len
> MLEN
) {
10312 MCLGET(n
, M_DONTWAIT
);
10321 n
->m_len
= M_TRAILINGSPACE(n
);
10322 /* use the bottom of mbuf, hoping we can prepend afterwards */
10323 if (n
->m_len
> len
) {
10324 t
= (n
->m_len
- len
) & ~(sizeof(long) - 1);
10341 static struct mbuf
*
10342 key_setdumpsastats(u_int32_t dir
,
10343 struct sastat
*stats
,
10344 u_int32_t max_stats
,
10345 u_int64_t session_ids
[],
10349 struct mbuf
*result
= NULL
, *m
= NULL
;
10351 m
= key_setsadbmsg(SADB_GETSASTAT
, 0, 0, seq
, pid
, 0);
10357 m
= key_setsadbsession_id(session_ids
);
10363 m
= key_setsadbsastat(dir
,
10371 if ((result
->m_flags
& M_PKTHDR
) == 0) {
10375 if (result
->m_len
< sizeof(struct sadb_msg
)) {
10376 result
= m_pullup(result
, sizeof(struct sadb_msg
));
10377 if (result
== NULL
) {
10382 result
->m_pkthdr
.len
= 0;
10383 for (m
= result
; m
; m
= m
->m_next
) {
10384 result
->m_pkthdr
.len
+= m
->m_len
;
10387 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
10388 PFKEY_UNIT64(result
->m_pkthdr
.len
);
10400 * SADB_GETSASTAT processing
10401 * dump all stats for matching entries in SAD.
10403 * m will always be freed.
10407 key_getsastat(struct socket
*so
,
10409 const struct sadb_msghdr
*mhp
)
10411 struct sadb_session_id
*session_id
;
10412 u_int32_t bufsize
, arg_count
, res_count
;
10413 struct sadb_sastat
*sa_stats_arg
;
10414 struct sastat
*sa_stats_sav
= NULL
;
10419 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
) {
10420 panic("%s: NULL pointer is passed.\n", __FUNCTION__
);
10423 if (mhp
->ext
[SADB_EXT_SESSION_ID
] == NULL
) {
10424 printf("%s: invalid message is passed. missing session-id.\n", __FUNCTION__
);
10425 return key_senderror(so
, m
, EINVAL
);
10427 if (mhp
->extlen
[SADB_EXT_SESSION_ID
] < sizeof(struct sadb_session_id
)) {
10428 printf("%s: invalid message is passed. short session-id.\n", __FUNCTION__
);
10429 return key_senderror(so
, m
, EINVAL
);
10431 if (mhp
->ext
[SADB_EXT_SASTAT
] == NULL
) {
10432 printf("%s: invalid message is passed. missing stat args.\n", __FUNCTION__
);
10433 return key_senderror(so
, m
, EINVAL
);
10435 if (mhp
->extlen
[SADB_EXT_SASTAT
] < sizeof(*sa_stats_arg
)) {
10436 printf("%s: invalid message is passed. short stat args.\n", __FUNCTION__
);
10437 return key_senderror(so
, m
, EINVAL
);
10440 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
10442 // exit early if there are no active SAs
10443 if (ipsec_sav_count
<= 0) {
10444 printf("%s: No active SAs.\n", __FUNCTION__
);
10448 bufsize
= (ipsec_sav_count
+ 1) * sizeof(*sa_stats_sav
);
10450 KMALLOC_WAIT(sa_stats_sav
, __typeof__(sa_stats_sav
), bufsize
);
10451 if (sa_stats_sav
== NULL
) {
10452 printf("%s: No more memory.\n", __FUNCTION__
);
10456 bzero(sa_stats_sav
, bufsize
);
10458 sa_stats_arg
= (__typeof__(sa_stats_arg
))
10459 (void *)mhp
->ext
[SADB_EXT_SASTAT
];
10460 arg_count
= sa_stats_arg
->sadb_sastat_list_len
;
10461 // exit early if there are no requested SAs
10462 if (arg_count
== 0) {
10463 printf("%s: No SAs requested.\n", __FUNCTION__
);
10469 if (key_getsastatbyspi((struct sastat
*)(sa_stats_arg
+ 1),
10474 printf("%s: Error finding SAs.\n", __FUNCTION__
);
10479 printf("%s: No SAs found.\n", __FUNCTION__
);
10484 session_id
= (__typeof__(session_id
))
10485 (void *)mhp
->ext
[SADB_EXT_SESSION_ID
];
10487 /* send this to the userland. */
10488 n
= key_setdumpsastats(sa_stats_arg
->sadb_sastat_dir
,
10491 session_id
->sadb_session_id_v
,
10492 mhp
->msg
->sadb_msg_seq
,
10493 mhp
->msg
->sadb_msg_pid
);
10495 printf("%s: No bufs to dump stats.\n", __FUNCTION__
);
10500 key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
10502 if (sa_stats_sav
) {
10503 KFREE(sa_stats_sav
);
10507 return key_senderror(so
, m
, error
);
10515 key_update_natt_keepalive_timestamp(struct secasvar
*sav_sent
,
10516 struct secasvar
*sav_update
)
10518 struct secasindex saidx_swap_sent_addr
;
10520 // exit early if two SAs are identical, or if sav_update is current
10521 if (sav_sent
== sav_update
||
10522 sav_update
->natt_last_activity
== natt_now
) {
10526 // assuming that (sav_update->remote_ike_port != 0 && (esp_udp_encap_port & 0xFFFF) != 0)
10528 bzero(&saidx_swap_sent_addr
, sizeof(saidx_swap_sent_addr
));
10529 memcpy(&saidx_swap_sent_addr
.src
, &sav_sent
->sah
->saidx
.dst
, sizeof(saidx_swap_sent_addr
.src
));
10530 memcpy(&saidx_swap_sent_addr
.dst
, &sav_sent
->sah
->saidx
.src
, sizeof(saidx_swap_sent_addr
.dst
));
10531 saidx_swap_sent_addr
.proto
= sav_sent
->sah
->saidx
.proto
;
10532 saidx_swap_sent_addr
.mode
= sav_sent
->sah
->saidx
.mode
;
10533 // we ignore reqid for split-tunnel setups
10535 if (key_cmpsaidx(&sav_sent
->sah
->saidx
, &sav_update
->sah
->saidx
, CMP_MODE
| CMP_PORT
) ||
10536 key_cmpsaidx(&saidx_swap_sent_addr
, &sav_update
->sah
->saidx
, CMP_MODE
| CMP_PORT
)) {
10537 sav_update
->natt_last_activity
= natt_now
;
10542 key_send_delsp(struct secpolicy
*sp
)
10544 struct mbuf
*result
= NULL
, *m
;
10550 /* set msg header */
10551 m
= key_setsadbmsg(SADB_X_SPDDELETE
, 0, 0, 0, 0, 0);
10557 /* set sadb_address(es) for source */
10558 if (sp
->spidx
.src_range
.start
.ss_len
> 0) {
10559 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_START
,
10560 (struct sockaddr
*)&sp
->spidx
.src_range
.start
, sp
->spidx
.prefs
,
10561 sp
->spidx
.ul_proto
);
10567 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_END
,
10568 (struct sockaddr
*)&sp
->spidx
.src_range
.end
, sp
->spidx
.prefs
,
10569 sp
->spidx
.ul_proto
);
10575 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
10576 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
10577 sp
->spidx
.ul_proto
);
10584 /* set sadb_address(es) for destination */
10585 if (sp
->spidx
.dst_range
.start
.ss_len
> 0) {
10586 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_START
,
10587 (struct sockaddr
*)&sp
->spidx
.dst_range
.start
, sp
->spidx
.prefd
,
10588 sp
->spidx
.ul_proto
);
10594 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_END
,
10595 (struct sockaddr
*)&sp
->spidx
.dst_range
.end
, sp
->spidx
.prefd
,
10596 sp
->spidx
.ul_proto
);
10602 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
10603 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
10604 sp
->spidx
.ul_proto
);
10611 /* set secpolicy */
10612 m
= key_sp2msg(sp
);
10618 if ((result
->m_flags
& M_PKTHDR
) == 0) {
10622 if (result
->m_len
< sizeof(struct sadb_msg
)) {
10623 result
= m_pullup(result
, sizeof(struct sadb_msg
));
10624 if (result
== NULL
) {
10629 result
->m_pkthdr
.len
= 0;
10630 for (m
= result
; m
; m
= m
->m_next
) {
10631 result
->m_pkthdr
.len
+= m
->m_len
;
10634 mtod(result
, struct sadb_msg
*)->sadb_msg_len
= PFKEY_UNIT64(result
->m_pkthdr
.len
);
10636 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
10646 key_delsp_for_ipsec_if(ifnet_t ipsec_if
)
10648 struct secashead
*sah
;
10649 struct secasvar
*sav
, *nextsav
;
10652 struct secpolicy
*sp
, *nextsp
;
10655 if (ipsec_if
== NULL
) {
10659 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
10661 lck_mtx_lock(sadb_mutex
);
10663 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
10664 for (sp
= LIST_FIRST(&sptree
[dir
]);
10667 nextsp
= LIST_NEXT(sp
, chain
);
10669 if (sp
->ipsec_if
== ipsec_if
) {
10670 ifnet_release(sp
->ipsec_if
);
10671 sp
->ipsec_if
= NULL
;
10673 key_send_delsp(sp
);
10675 sp
->state
= IPSEC_SPSTATE_DEAD
;
10676 key_freesp(sp
, KEY_SADB_LOCKED
);
10681 LIST_FOREACH(sah
, &sahtree
, chain
) {
10682 if (sah
->ipsec_if
== ipsec_if
) {
10683 /* This SAH is linked to the IPsec interface. It now needs to close. */
10684 ifnet_release(sah
->ipsec_if
);
10685 sah
->ipsec_if
= NULL
;
10687 for (stateidx
= 0; stateidx
< _ARRAYLEN(saorder_state_alive
); stateidx
++) {
10688 state
= saorder_state_any
[stateidx
];
10689 for (sav
= LIST_FIRST(&sah
->savtree
[state
]); sav
!= NULL
; sav
= nextsav
) {
10690 nextsav
= LIST_NEXT(sav
, chain
);
10692 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
10693 key_freesav(sav
, KEY_SADB_LOCKED
);
10697 sah
->state
= SADB_SASTATE_DEAD
;
10701 lck_mtx_unlock(sadb_mutex
);
10704 __private_extern__ u_int32_t
10705 key_fill_offload_frames_for_savs(ifnet_t ifp
,
10706 struct ifnet_keepalive_offload_frame
*frames_array
,
10707 u_int32_t frames_array_count
,
10708 size_t frame_data_offset
)
10710 struct secashead
*sah
= NULL
;
10711 struct secasvar
*sav
= NULL
;
10712 struct ifnet_keepalive_offload_frame
*frame
= frames_array
;
10713 u_int32_t frame_index
= 0;
10715 if (frame
== NULL
|| frames_array_count
== 0) {
10716 return frame_index
;
10719 lck_mtx_lock(sadb_mutex
);
10720 LIST_FOREACH(sah
, &sahtree
, chain
) {
10721 LIST_FOREACH(sav
, &sah
->savtree
[SADB_SASTATE_MATURE
], chain
) {
10722 if (ipsec_fill_offload_frame(ifp
, sav
, frame
, frame_data_offset
)) {
10724 if (frame_index
>= frames_array_count
) {
10725 lck_mtx_unlock(sadb_mutex
);
10726 return frame_index
;
10728 frame
= &(frames_array
[frame_index
]);
10732 lck_mtx_unlock(sadb_mutex
);
10734 return frame_index
;