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>
122 #include <netinet6/ipcomp.h>
124 #include <netinet6/ipcomp6.h>
129 #include <sys/random.h>
131 #include <net/net_osdep.h>
133 #define FULLMASK 0xff
135 lck_grp_t
*sadb_mutex_grp
;
136 lck_grp_attr_t
*sadb_mutex_grp_attr
;
137 lck_attr_t
*sadb_mutex_attr
;
138 decl_lck_mtx_data(, sadb_mutex_data
);
139 lck_mtx_t
*sadb_mutex
= &sadb_mutex_data
;
141 lck_grp_t
*pfkey_stat_mutex_grp
;
142 lck_grp_attr_t
*pfkey_stat_mutex_grp_attr
;
143 lck_attr_t
*pfkey_stat_mutex_attr
;
144 decl_lck_mtx_data(, pfkey_stat_mutex_data
);
145 lck_mtx_t
*pfkey_stat_mutex
= &pfkey_stat_mutex_data
;
148 * Note on SA reference counting:
149 * - SAs that are not in DEAD state will have (total external reference + 1)
150 * following value in reference count field. they cannot be freed and are
151 * referenced from SA header.
152 * - SAs that are in DEAD state will have (total external reference)
153 * in reference count field. they are ready to be freed. reference from
154 * SA header will be removed in key_delsav(), when the reference count
155 * field hits 0 (= no external reference other than from SA header.
158 u_int32_t key_debug_level
= 0; //### our sysctl is not dynamic
159 static int key_timehandler_running
= 0;
160 static u_int key_spi_trycnt
= 1000;
161 static u_int32_t key_spi_minval
= 0x100;
162 static u_int32_t key_spi_maxval
= 0x0fffffff; /* XXX */
163 static u_int32_t policy_id
= 0;
164 static u_int key_int_random
= 60; /*interval to initialize randseed,1(m)*/
165 static u_int key_larval_lifetime
= 30; /* interval to expire acquiring, 30(s)*/
166 static int key_blockacq_count
= 10; /* counter for blocking SADB_ACQUIRE.*/
167 static int key_blockacq_lifetime
= 20; /* lifetime for blocking SADB_ACQUIRE.*/
168 static int key_preferred_oldsa
= 0; /* preferred old sa rather than new sa.*/
169 __private_extern__
int natt_keepalive_interval
= 20; /* interval between natt keepalives.*/
170 __private_extern__
int ipsec_policy_count
= 0;
171 static int ipsec_sav_count
= 0;
173 static u_int32_t acq_seq
= 0;
174 static int key_tick_init_random
= 0;
175 static u_int64_t up_time
= 0;
176 __private_extern__ u_int64_t natt_now
= 0;
178 static LIST_HEAD(_sptree
, secpolicy
) sptree
[IPSEC_DIR_MAX
]; /* SPD */
179 static LIST_HEAD(_sahtree
, secashead
) sahtree
; /* SAD */
180 static LIST_HEAD(_regtree
, secreg
) regtree
[SADB_SATYPE_MAX
+ 1];
183 #define SPIHASHSIZE 128
184 #define SPIHASH(x) (((x) ^ ((x) >> 16)) % SPIHASHSIZE)
185 static LIST_HEAD(_spihash
, secasvar
) spihash
[SPIHASHSIZE
];
187 #ifndef IPSEC_NONBLOCK_ACQUIRE
188 static LIST_HEAD(_acqtree
, secacq
) acqtree
; /* acquiring list */
190 static LIST_HEAD(_spacqtree
, secspacq
) spacqtree
; /* SP acquiring list */
192 struct key_cb key_cb
;
194 /* search order for SAs */
195 static const u_int saorder_state_valid_prefer_old
[] = {
196 SADB_SASTATE_DYING
, SADB_SASTATE_MATURE
,
198 static const u_int saorder_state_valid_prefer_new
[] = {
199 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
201 static const u_int saorder_state_alive
[] = {
203 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
, SADB_SASTATE_LARVAL
205 static const u_int saorder_state_any
[] = {
206 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
207 SADB_SASTATE_LARVAL
, SADB_SASTATE_DEAD
210 static const int minsize
[] = {
211 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
212 sizeof(struct sadb_sa
), /* SADB_EXT_SA */
213 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
214 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
215 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
216 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_SRC */
217 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_DST */
218 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_PROXY */
219 sizeof(struct sadb_key
), /* SADB_EXT_KEY_AUTH */
220 sizeof(struct sadb_key
), /* SADB_EXT_KEY_ENCRYPT */
221 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_SRC */
222 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_DST */
223 sizeof(struct sadb_sens
), /* SADB_EXT_SENSITIVITY */
224 sizeof(struct sadb_prop
), /* SADB_EXT_PROPOSAL */
225 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_AUTH */
226 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_ENCRYPT */
227 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
228 0, /* SADB_X_EXT_KMPRIVATE */
229 sizeof(struct sadb_x_policy
), /* SADB_X_EXT_POLICY */
230 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
231 sizeof(struct sadb_session_id
), /* SADB_EXT_SESSION_ID */
232 sizeof(struct sadb_sastat
), /* SADB_EXT_SASTAT */
233 sizeof(struct sadb_x_ipsecif
), /* SADB_X_EXT_IPSECIF */
234 sizeof(struct sadb_address
), /* SADB_X_EXT_ADDR_RANGE_SRC_START */
235 sizeof(struct sadb_address
), /* SADB_X_EXT_ADDR_RANGE_SRC_END */
236 sizeof(struct sadb_address
), /* SADB_X_EXT_ADDR_RANGE_DST_START */
237 sizeof(struct sadb_address
), /* SADB_X_EXT_ADDR_RANGE_DST_END */
238 sizeof(struct sadb_address
), /* SADB_EXT_MIGRATE_ADDRESS_SRC */
239 sizeof(struct sadb_address
), /* SADB_EXT_MIGRATE_ADDRESS_DST */
240 sizeof(struct sadb_x_ipsecif
), /* SADB_X_EXT_MIGRATE_IPSECIF */
242 static const int maxsize
[] = {
243 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
244 sizeof(struct sadb_sa_2
), /* SADB_EXT_SA */
245 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
246 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
247 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
248 0, /* SADB_EXT_ADDRESS_SRC */
249 0, /* SADB_EXT_ADDRESS_DST */
250 0, /* SADB_EXT_ADDRESS_PROXY */
251 0, /* SADB_EXT_KEY_AUTH */
252 0, /* SADB_EXT_KEY_ENCRYPT */
253 0, /* SADB_EXT_IDENTITY_SRC */
254 0, /* SADB_EXT_IDENTITY_DST */
255 0, /* SADB_EXT_SENSITIVITY */
256 0, /* SADB_EXT_PROPOSAL */
257 0, /* SADB_EXT_SUPPORTED_AUTH */
258 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
259 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
260 0, /* SADB_X_EXT_KMPRIVATE */
261 0, /* SADB_X_EXT_POLICY */
262 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
263 0, /* SADB_EXT_SESSION_ID */
264 0, /* SADB_EXT_SASTAT */
265 sizeof(struct sadb_x_ipsecif
), /* SADB_X_EXT_IPSECIF */
266 0, /* SADB_X_EXT_ADDR_RANGE_SRC_START */
267 0, /* SADB_X_EXT_ADDR_RANGE_SRC_END */
268 0, /* SADB_X_EXT_ADDR_RANGE_DST_START */
269 0, /* SADB_X_EXT_ADDR_RANGE_DST_END */
270 0, /* SADB_EXT_MIGRATE_ADDRESS_SRC */
271 0, /* SADB_EXT_MIGRATE_ADDRESS_DST */
272 sizeof(struct sadb_x_ipsecif
), /* SADB_X_EXT_MIGRATE_IPSECIF */
275 static int ipsec_esp_keymin
= 256;
276 static int ipsec_esp_auth
= 0;
277 static int ipsec_ah_keymin
= 128;
279 SYSCTL_DECL(_net_key
);
280 /* Thread safe: no accumulated state */
281 SYSCTL_INT(_net_key
, KEYCTL_DEBUG_LEVEL
, debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
282 &key_debug_level
, 0, "");
285 /* max count of trial for the decision of spi value */
286 SYSCTL_INT(_net_key
, KEYCTL_SPI_TRY
, spi_trycnt
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
287 &key_spi_trycnt
, 0, "");
289 /* minimum spi value to allocate automatically. */
290 SYSCTL_INT(_net_key
, KEYCTL_SPI_MIN_VALUE
, spi_minval
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
291 &key_spi_minval
, 0, "");
293 /* maximun spi value to allocate automatically. */
294 SYSCTL_INT(_net_key
, KEYCTL_SPI_MAX_VALUE
, spi_maxval
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
295 &key_spi_maxval
, 0, "");
297 /* interval to initialize randseed */
298 SYSCTL_INT(_net_key
, KEYCTL_RANDOM_INT
, int_random
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
299 &key_int_random
, 0, "");
301 /* lifetime for larval SA; thread safe due to > compare */
302 SYSCTL_INT(_net_key
, KEYCTL_LARVAL_LIFETIME
, larval_lifetime
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
303 &key_larval_lifetime
, 0, "");
305 /* counter for blocking to send SADB_ACQUIRE to IKEd */
306 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_COUNT
, blockacq_count
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
307 &key_blockacq_count
, 0, "");
309 /* lifetime for blocking to send SADB_ACQUIRE to IKEd: Thread safe, > compare */
310 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_LIFETIME
, blockacq_lifetime
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
311 &key_blockacq_lifetime
, 0, "");
314 SYSCTL_INT(_net_key
, KEYCTL_ESP_AUTH
, esp_auth
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
315 &ipsec_esp_auth
, 0, "");
317 /* minimum ESP key length */
318 SYSCTL_INT(_net_key
, KEYCTL_ESP_KEYMIN
, esp_keymin
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
319 &ipsec_esp_keymin
, 0, "");
321 /* minimum AH key length */
322 SYSCTL_INT(_net_key
, KEYCTL_AH_KEYMIN
, ah_keymin
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
323 &ipsec_ah_keymin
, 0, "");
325 /* perfered old SA rather than new SA */
326 SYSCTL_INT(_net_key
, KEYCTL_PREFERED_OLDSA
, prefered_oldsa
, CTLFLAG_RW
| CTLFLAG_LOCKED
,\
327 &key_preferred_oldsa
, 0, "");
329 /* time between NATT keepalives in seconds, 0 disabled */
330 SYSCTL_INT(_net_key
, KEYCTL_NATT_KEEPALIVE_INTERVAL
, natt_keepalive_interval
, CTLFLAG_RW
| CTLFLAG_LOCKED
,\
331 &natt_keepalive_interval
, 0, "");
333 /* PF_KEY statistics */
334 SYSCTL_STRUCT(_net_key
, KEYCTL_PFKEYSTAT
, pfkeystat
, CTLFLAG_RD
| CTLFLAG_LOCKED
,\
335 &pfkeystat
, pfkeystat
, "");
338 #define LIST_FOREACH(elm, head, field) \
339 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
341 #define __LIST_CHAINED(elm) \
342 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
343 #define LIST_INSERT_TAIL(head, elm, type, field) \
345 struct type *curelm = LIST_FIRST(head); \
346 if (curelm == NULL) {\
347 LIST_INSERT_HEAD(head, elm, field); \
349 while (LIST_NEXT(curelm, field)) \
350 curelm = LIST_NEXT(curelm, field);\
351 LIST_INSERT_AFTER(curelm, elm, field);\
355 #define KEY_CHKSASTATE(head, sav, name) \
357 if ((head) != (sav)) { \
358 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
359 (name), (head), (sav))); \
364 #define KEY_CHKSPDIR(head, sp, name) \
366 if ((head) != (sp)) { \
367 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
368 "anyway continue.\n", \
369 (name), (head), (sp))); \
374 #define KMALLOC_WAIT(p, t, n) \
375 ((p) = (t) _MALLOC((u_int32_t)(n), M_SECA, M_WAITOK))
376 #define KMALLOC_NOWAIT(p, t, n) \
377 ((p) = (t) _MALLOC((u_int32_t)(n), M_SECA, M_NOWAIT))
379 _FREE((caddr_t)(p), M_SECA);
381 #define KMALLOC_WAIT(p, t, n) \
383 ((p) = (t)_MALLOC((u_int32_t)(n), M_SECA, M_WAITOK)); \
384 printf("%s %d: %p <- KMALLOC_WAIT(%s, %d)\n", \
385 __FILE__, __LINE__, (p), #t, n); \
387 #define KMALLOC_NOWAIT(p, t, n) \
389 ((p) = (t)_MALLOC((u_int32_t)(n), M_SECA, M_NOWAIT)); \
390 printf("%s %d: %p <- KMALLOC_NOWAIT(%s, %d)\n", \
391 __FILE__, __LINE__, (p), #t, n); \
396 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
397 _FREE((caddr_t)(p), M_SECA); \
402 * set parameters into secpolicyindex buffer.
403 * Must allocate secpolicyindex buffer passed to this function.
405 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, ifp, s_s, s_e, d_s, d_e, idx) \
407 bzero((idx), sizeof(struct secpolicyindex)); \
408 (idx)->dir = (_dir); \
409 (idx)->prefs = (ps); \
410 (idx)->prefd = (pd); \
411 (idx)->ul_proto = (ulp); \
412 (idx)->internal_if = (ifp); \
413 if (s) bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
414 if (d) bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
415 if (s_s) bcopy((s_s), &(idx)->src_range.start, ((struct sockaddr *)(s_s))->sa_len); \
416 if (s_e) bcopy((s_e), &(idx)->src_range.end, ((struct sockaddr *)(s_e))->sa_len); \
417 if (d_s) bcopy((d_s), &(idx)->dst_range.start, ((struct sockaddr *)(d_s))->sa_len); \
418 if (d_e) bcopy((d_e), &(idx)->dst_range.end, ((struct sockaddr *)(d_e))->sa_len); \
422 * set parameters into secasindex buffer.
423 * Must allocate secasindex buffer before calling this function.
425 #define KEY_SETSECASIDX(p, m, r, s, d, ifi, idx) \
427 bzero((idx), sizeof(struct secasindex)); \
428 (idx)->proto = (p); \
430 (idx)->reqid = (r); \
431 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
432 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
433 (idx)->ipsec_ifindex = (ifi); \
438 u_int32_t getspi_count
; /* the avarage of count to try to get new SPI */
442 struct sadb_msg
*msg
;
443 struct sadb_ext
*ext
[SADB_EXT_MAX
+ 1];
444 int extoff
[SADB_EXT_MAX
+ 1];
445 int extlen
[SADB_EXT_MAX
+ 1];
448 static struct secpolicy
*__key_getspbyid(u_int32_t id
);
449 static struct secasvar
*key_do_allocsa_policy(struct secashead
*, u_int
, u_int16_t
);
450 static int key_do_get_translated_port(struct secashead
*, struct secasvar
*, u_int
);
451 static void key_delsp(struct secpolicy
*);
452 static struct secpolicy
*key_getsp(struct secpolicyindex
*);
453 static u_int32_t
key_newreqid(void);
454 static struct mbuf
*key_gather_mbuf(struct mbuf
*,
455 const struct sadb_msghdr
*, int, int, int *);
456 static int key_spdadd(struct socket
*, struct mbuf
*,
457 const struct sadb_msghdr
*);
458 static u_int32_t
key_getnewspid(void);
459 static int key_spddelete(struct socket
*, struct mbuf
*,
460 const struct sadb_msghdr
*);
461 static int key_spddelete2(struct socket
*, struct mbuf
*,
462 const struct sadb_msghdr
*);
463 static int key_spdenable(struct socket
*, struct mbuf
*,
464 const struct sadb_msghdr
*);
465 static int key_spddisable(struct socket
*, struct mbuf
*,
466 const struct sadb_msghdr
*);
467 static int key_spdget(struct socket
*, struct mbuf
*,
468 const struct sadb_msghdr
*);
469 static int key_spdflush(struct socket
*, struct mbuf
*,
470 const struct sadb_msghdr
*);
471 static int key_spddump(struct socket
*, struct mbuf
*,
472 const struct sadb_msghdr
*);
473 static struct mbuf
*key_setdumpsp(struct secpolicy
*,
474 u_int8_t
, u_int32_t
, u_int32_t
);
475 static u_int
key_getspreqmsglen(struct secpolicy
*);
476 static int key_spdexpire(struct secpolicy
*);
477 static struct secashead
*key_newsah(struct secasindex
*, ifnet_t
, u_int
, u_int8_t
);
478 static struct secasvar
*key_newsav(struct mbuf
*,
479 const struct sadb_msghdr
*, struct secashead
*, int *,
481 static struct secashead
*key_getsah(struct secasindex
*);
482 static struct secasvar
*key_checkspidup(struct secasindex
*, u_int32_t
);
483 static void key_setspi
__P((struct secasvar
*, u_int32_t
));
484 static struct secasvar
*key_getsavbyspi(struct secashead
*, u_int32_t
);
485 static int key_setsaval(struct secasvar
*, struct mbuf
*,
486 const struct sadb_msghdr
*);
487 static int key_mature(struct secasvar
*);
488 static struct mbuf
*key_setdumpsa(struct secasvar
*, u_int8_t
,
489 u_int8_t
, u_int32_t
, u_int32_t
);
490 static struct mbuf
*key_setsadbmsg(u_int8_t
, u_int16_t
, u_int8_t
,
491 u_int32_t
, pid_t
, u_int16_t
);
492 static struct mbuf
*key_setsadbsa(struct secasvar
*);
493 static struct mbuf
*key_setsadbaddr(u_int16_t
,
494 struct sockaddr
*, u_int8_t
, u_int16_t
);
495 static struct mbuf
*key_setsadbipsecif(ifnet_t
, ifnet_t
, ifnet_t
, int);
497 static struct mbuf
*key_setsadbident(u_int16_t
, u_int16_t
, caddr_t
,
500 static struct mbuf
*key_setsadbxsa2(u_int8_t
, u_int32_t
, u_int32_t
, u_int16_t
);
501 static struct mbuf
*key_setsadbxpolicy(u_int16_t
, u_int8_t
,
503 static void *key_newbuf(const void *, u_int
);
505 static int key_ismyaddr6(struct sockaddr_in6
*);
507 static void key_update_natt_keepalive_timestamp(struct secasvar
*, struct secasvar
*);
509 /* flags for key_cmpsaidx() */
510 #define CMP_HEAD 0x1 /* protocol, addresses. */
511 #define CMP_PORT 0x2 /* additionally HEAD, reqid, mode. */
512 #define CMP_REQID 0x4 /* additionally HEAD, reqid. */
513 #define CMP_MODE 0x8 /* additionally mode. */
514 #define CMP_EXACTLY 0xF /* all elements. */
515 static int key_cmpsaidx(struct secasindex
*, struct secasindex
*, int);
517 static int key_cmpspidx_exactly(struct secpolicyindex
*,
518 struct secpolicyindex
*);
519 static int key_cmpspidx_withmask(struct secpolicyindex
*,
520 struct secpolicyindex
*);
521 static int key_sockaddrcmp(struct sockaddr
*, struct sockaddr
*, int);
522 static int key_is_addr_in_range(struct sockaddr_storage
*, struct secpolicyaddrrange
*);
523 static int key_bbcmp(caddr_t
, caddr_t
, u_int
);
524 static void key_srandom(void);
525 static u_int16_t
key_satype2proto(u_int8_t
);
526 static u_int8_t
key_proto2satype(u_int16_t
);
528 static int key_getspi(struct socket
*, struct mbuf
*,
529 const struct sadb_msghdr
*);
530 static u_int32_t
key_do_getnewspi(struct sadb_spirange
*, struct secasindex
*);
531 static int key_update(struct socket
*, struct mbuf
*,
532 const struct sadb_msghdr
*);
534 static struct secasvar
*key_getsavbyseq(struct secashead
*, u_int32_t
);
536 static int key_add(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
537 static int key_setident(struct secashead
*, struct mbuf
*,
538 const struct sadb_msghdr
*);
539 static struct mbuf
*key_getmsgbuf_x1(struct mbuf
*, const struct sadb_msghdr
*);
540 static int key_delete(struct socket
*, struct mbuf
*,
541 const struct sadb_msghdr
*);
542 static int key_get(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
544 static void key_getcomb_setlifetime(struct sadb_comb
*);
546 static struct mbuf
*key_getcomb_esp(void);
548 static struct mbuf
*key_getcomb_ah(void);
549 static struct mbuf
*key_getcomb_ipcomp(void);
550 static struct mbuf
*key_getprop(const struct secasindex
*);
552 static int key_acquire(struct secasindex
*, struct secpolicy
*);
553 #ifndef IPSEC_NONBLOCK_ACQUIRE
554 static struct secacq
*key_newacq(struct secasindex
*);
555 static struct secacq
*key_getacq(struct secasindex
*);
556 static struct secacq
*key_getacqbyseq(u_int32_t
);
558 static struct secspacq
*key_newspacq(struct secpolicyindex
*);
559 static struct secspacq
*key_getspacq(struct secpolicyindex
*);
560 static int key_acquire2(struct socket
*, struct mbuf
*,
561 const struct sadb_msghdr
*);
562 static int key_register(struct socket
*, struct mbuf
*,
563 const struct sadb_msghdr
*);
564 static int key_expire(struct secasvar
*);
565 static int key_flush(struct socket
*, struct mbuf
*,
566 const struct sadb_msghdr
*);
567 static int key_dump(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
568 static int key_promisc(struct socket
*, struct mbuf
*,
569 const struct sadb_msghdr
*);
570 static int key_senderror(struct socket
*, struct mbuf
*, int);
571 static int key_validate_ext(const struct sadb_ext
*, int);
572 static int key_align(struct mbuf
*, struct sadb_msghdr
*);
573 static struct mbuf
*key_alloc_mbuf(int);
574 static int key_getsastat (struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
575 static int key_migrate (struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
576 static int key_setsaval2(struct secasvar
*sav
,
582 struct sadb_key
*key_auth
,
583 u_int16_t key_auth_len
,
584 struct sadb_key
*key_enc
,
585 u_int16_t key_enc_len
,
590 struct sadb_lifetime
*lifetime_hard
,
591 struct sadb_lifetime
*lifetime_soft
);
592 static void bzero_keys(const struct sadb_msghdr
*);
594 extern int ipsec_bypass
;
595 extern int esp_udp_encap_port
;
596 int ipsec_send_natt_keepalive(struct secasvar
*sav
);
597 bool ipsec_fill_offload_frame(ifnet_t ifp
, struct secasvar
*sav
, struct ifnet_keepalive_offload_frame
*frame
, size_t frame_data_offset
);
599 void key_init(struct protosw
*, struct domain
*);
603 * setup locks, call raw_init(), and then init timer and associated data
607 key_init(struct protosw
*pp
, struct domain
*dp
)
609 static int key_initialized
= 0;
612 VERIFY((pp
->pr_flags
& (PR_INITIALIZED
|PR_ATTACHED
)) == PR_ATTACHED
);
614 _CASSERT(PFKEY_ALIGN8(sizeof(struct sadb_msg
)) <= _MHLEN
);
620 sadb_mutex_grp_attr
= lck_grp_attr_alloc_init();
621 sadb_mutex_grp
= lck_grp_alloc_init("sadb", sadb_mutex_grp_attr
);
622 sadb_mutex_attr
= lck_attr_alloc_init();
624 lck_mtx_init(sadb_mutex
, sadb_mutex_grp
, sadb_mutex_attr
);
626 pfkey_stat_mutex_grp_attr
= lck_grp_attr_alloc_init();
627 pfkey_stat_mutex_grp
= lck_grp_alloc_init("pfkey_stat", pfkey_stat_mutex_grp_attr
);
628 pfkey_stat_mutex_attr
= lck_attr_alloc_init();
630 lck_mtx_init(pfkey_stat_mutex
, pfkey_stat_mutex_grp
, pfkey_stat_mutex_attr
);
632 for (i
= 0; i
< SPIHASHSIZE
; i
++)
633 LIST_INIT(&spihash
[i
]);
637 bzero((caddr_t
)&key_cb
, sizeof(key_cb
));
639 for (i
= 0; i
< IPSEC_DIR_MAX
; i
++) {
640 LIST_INIT(&sptree
[i
]);
642 ipsec_policy_count
= 0;
646 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
647 LIST_INIT(®tree
[i
]);
651 #ifndef IPSEC_NONBLOCK_ACQUIRE
654 LIST_INIT(&spacqtree
);
658 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
659 ip4_def_policy
.refcnt
++; /*never reclaim this*/
662 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
663 ip6_def_policy
.refcnt
++; /*never reclaim this*/
666 key_timehandler_running
= 0;
668 /* initialize key statistics */
669 keystat
.getspi_count
= 1;
672 printf("IPsec: Initialized Security Association Processing.\n");
677 key_start_timehandler(void)
679 /* must be called while locked */
680 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
681 if (key_timehandler_running
== 0) {
682 key_timehandler_running
= 1;
683 (void)timeout((void *)key_timehandler
, (void *)0, hz
);
686 /* Turn off the ipsec bypass */
687 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");
711 /* check direction */
713 case IPSEC_DIR_INBOUND
:
714 case IPSEC_DIR_OUTBOUND
:
717 panic("key_allocsp: Invalid direction is passed.\n");
721 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
722 printf("*** objects\n");
723 kdebug_secpolicyindex(spidx
));
725 lck_mtx_lock(sadb_mutex
);
726 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
727 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
728 printf("*** in SPD\n");
729 kdebug_secpolicyindex(&sp
->spidx
));
731 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
734 /* If the policy is disabled, skip */
735 if (sp
->disabled
> 0)
738 /* If the incoming spidx specifies bound if,
739 ignore unbound policies*/
740 if (spidx
->internal_if
!= NULL
741 && (sp
->spidx
.internal_if
== NULL
|| sp
->ipsec_if
== NULL
))
744 if (key_cmpspidx_withmask(&sp
->spidx
, spidx
))
747 lck_mtx_unlock(sadb_mutex
);
752 /* found a SPD entry */
754 sp
->lastused
= tv
.tv_sec
;
756 lck_mtx_unlock(sadb_mutex
);
759 KEY_CHKSPDIR(sp
->spidx
.dir
, dir
, "key_allocsp");
760 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
761 printf("DP key_allocsp cause refcnt++:%d SP:0x%llx\n",
762 sp
->refcnt
, (uint64_t)VM_KERNEL_ADDRPERM(sp
)));
767 * return a policy that matches this particular inbound packet.
772 struct sockaddr
*osrc
,
773 struct sockaddr
*odst
,
774 struct sockaddr
*isrc
,
775 struct sockaddr
*idst
)
777 struct secpolicy
*sp
;
778 const int dir
= IPSEC_DIR_INBOUND
;
780 struct ipsecrequest
*r1
, *r2
, *p
;
781 struct sockaddr
*os
, *od
, *is
, *id
;
782 struct secpolicyindex spidx
;
784 if (isrc
->sa_family
!= idst
->sa_family
) {
785 ipseclog((LOG_ERR
, "protocol family mismatched %d != %d\n.",
786 isrc
->sa_family
, idst
->sa_family
));
790 lck_mtx_lock(sadb_mutex
);
791 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
792 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
796 for (p
= sp
->req
; p
; p
= p
->next
) {
797 if (p
->saidx
.mode
!= IPSEC_MODE_TUNNEL
)
804 /* here we look at address matches only */
806 if (isrc
->sa_len
> sizeof(spidx
.src
) ||
807 idst
->sa_len
> sizeof(spidx
.dst
))
809 bcopy(isrc
, &spidx
.src
, isrc
->sa_len
);
810 bcopy(idst
, &spidx
.dst
, idst
->sa_len
);
811 if (!key_cmpspidx_withmask(&sp
->spidx
, &spidx
))
814 is
= (struct sockaddr
*)&r1
->saidx
.src
;
815 id
= (struct sockaddr
*)&r1
->saidx
.dst
;
816 if (key_sockaddrcmp(is
, isrc
, 0) ||
817 key_sockaddrcmp(id
, idst
, 0))
821 os
= (struct sockaddr
*)&r2
->saidx
.src
;
822 od
= (struct sockaddr
*)&r2
->saidx
.dst
;
823 if (key_sockaddrcmp(os
, osrc
, 0) ||
824 key_sockaddrcmp(od
, odst
, 0))
830 lck_mtx_unlock(sadb_mutex
);
835 sp
->lastused
= tv
.tv_sec
;
837 lck_mtx_unlock(sadb_mutex
);
841 struct secasvar
*key_alloc_outbound_sav_for_interface(ifnet_t interface
, int family
)
843 struct secashead
*sah
;
844 struct secasvar
*sav
;
847 const u_int
*saorder_state_valid
;
849 struct sockaddr_in
*sin
;
852 if (interface
== NULL
)
855 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
857 lck_mtx_lock(sadb_mutex
);
859 LIST_FOREACH(sah
, &sahtree
, chain
) {
860 if (sah
->state
== SADB_SASTATE_DEAD
) {
863 if (sah
->ipsec_if
== interface
&&
864 (family
== AF_INET6
|| family
== AF_INET
) &&
865 sah
->dir
== IPSEC_DIR_OUTBOUND
) {
866 /* This SAH is linked to the IPSec interface, and the right family. We found it! */
867 if (key_preferred_oldsa
) {
868 saorder_state_valid
= saorder_state_valid_prefer_old
;
869 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
871 saorder_state_valid
= saorder_state_valid_prefer_new
;
872 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
875 sin
= (struct sockaddr_in
*)&sah
->saidx
.dst
;
876 dstport
= sin
->sin_port
;
877 if (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
)
878 sin
->sin_port
= IPSEC_PORT_ANY
;
880 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
881 state
= saorder_state_valid
[stateidx
];
882 sav
= key_do_allocsa_policy(sah
, state
, dstport
);
884 lck_mtx_unlock(sadb_mutex
);
893 lck_mtx_unlock(sadb_mutex
);
898 * allocating an SA entry for an *OUTBOUND* packet.
899 * checking each request entries in SP, and acquire an SA if need.
900 * OUT: 0: there are valid requests.
901 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
905 struct ipsecrequest
*isr
,
906 struct secasindex
*saidx
,
907 struct secasvar
**sav
)
911 struct sockaddr_in
*sin
;
913 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
918 if (isr
== NULL
|| saidx
== NULL
)
919 panic("key_checkrequest: NULL pointer is passed.\n");
922 switch (saidx
->mode
) {
923 case IPSEC_MODE_TRANSPORT
:
924 case IPSEC_MODE_TUNNEL
:
928 panic("key_checkrequest: Invalid policy defined.\n");
931 /* get current level */
932 level
= ipsec_get_reqlevel(isr
);
936 * key_allocsa_policy should allocate the oldest SA available.
937 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
940 *sav
= key_allocsa_policy(saidx
);
942 /* When there is SA. */
948 * Remove dst port - used for special natt support - don't call
949 * key_acquire with it.
951 if (saidx
->mode
== IPSEC_MODE_TRANSPORT
) {
952 sin
= (struct sockaddr_in
*)&saidx
->dst
;
953 sin
->sin_port
= IPSEC_PORT_ANY
;
955 if ((error
= key_acquire(saidx
, isr
->sp
)) != 0) {
956 /* XXX What should I do ? */
957 ipseclog((LOG_DEBUG
, "key_checkrequest: error %d returned "
958 "from key_acquire.\n", error
));
962 return level
== IPSEC_LEVEL_REQUIRE
? ENOENT
: 0;
966 * allocating a SA for policy entry from SAD.
967 * NOTE: searching SAD of aliving state.
968 * OUT: NULL: not found.
969 * others: found and return the pointer.
971 u_int32_t sah_search_calls
= 0;
972 u_int32_t sah_search_count
= 0;
975 struct secasindex
*saidx
)
977 struct secashead
*sah
;
978 struct secasvar
*sav
;
979 u_int stateidx
, state
;
980 const u_int
*saorder_state_valid
;
982 struct sockaddr_in
*sin
;
985 lck_mtx_lock(sadb_mutex
);
987 LIST_FOREACH(sah
, &sahtree
, chain
) {
989 if (sah
->state
== SADB_SASTATE_DEAD
)
991 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_MODE
| CMP_REQID
))
994 lck_mtx_unlock(sadb_mutex
);
1000 * search a valid state list for outbound packet.
1001 * This search order is important.
1003 if (key_preferred_oldsa
) {
1004 saorder_state_valid
= saorder_state_valid_prefer_old
;
1005 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
1007 saorder_state_valid
= saorder_state_valid_prefer_new
;
1008 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
1012 sin
= (struct sockaddr_in
*)&saidx
->dst
;
1013 dstport
= sin
->sin_port
;
1014 if (saidx
->mode
== IPSEC_MODE_TRANSPORT
)
1015 sin
->sin_port
= IPSEC_PORT_ANY
;
1017 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
1019 state
= saorder_state_valid
[stateidx
];
1021 sav
= key_do_allocsa_policy(sah
, state
, dstport
);
1023 lck_mtx_unlock(sadb_mutex
);
1027 lck_mtx_unlock(sadb_mutex
);
1032 key_send_delete (struct secasvar
*sav
)
1034 struct mbuf
*m
, *result
;
1037 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
1039 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0)
1040 panic("key_do_allocsa_policy: invalid proto is passed.\n");
1042 m
= key_setsadbmsg(SADB_DELETE
, 0,
1043 satype
, 0, 0, sav
->refcnt
- 1);
1048 /* set sadb_address for saidx's. */
1049 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
1050 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
1051 sav
->sah
->saidx
.src
.ss_len
<< 3,
1057 /* set sadb_address for saidx's. */
1058 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
1059 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
1060 sav
->sah
->saidx
.src
.ss_len
<< 3,
1066 /* create SA extension */
1067 m
= key_setsadbsa(sav
);
1072 if (result
->m_len
< sizeof(struct sadb_msg
)) {
1073 result
= m_pullup(result
,
1074 sizeof(struct sadb_msg
));
1079 result
->m_pkthdr
.len
= 0;
1080 for (m
= result
; m
; m
= m
->m_next
)
1081 result
->m_pkthdr
.len
+= m
->m_len
;
1082 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
1083 PFKEY_UNIT64(result
->m_pkthdr
.len
);
1085 if (key_sendup_mbuf(NULL
, result
,
1086 KEY_SENDUP_REGISTERED
))
1089 key_freesav(sav
, KEY_SADB_LOCKED
);
1093 * searching SAD with direction, protocol, mode and state.
1094 * called by key_allocsa_policy().
1097 * others : found, pointer to a SA.
1099 static struct secasvar
*
1100 key_do_allocsa_policy(
1101 struct secashead
*sah
,
1105 struct secasvar
*sav
, *nextsav
, *candidate
, *natt_candidate
, *no_natt_candidate
, *d
;
1107 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1111 natt_candidate
= NULL
;
1112 no_natt_candidate
= NULL
;
1114 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
1118 nextsav
= LIST_NEXT(sav
, chain
);
1121 KEY_CHKSASTATE(sav
->state
, state
, "key_do_allocsa_policy");
1123 if (sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&& dstport
&&
1124 ((sav
->flags
& SADB_X_EXT_NATT
) != 0) &&
1125 ntohs(dstport
) != sav
->remote_ike_port
)
1128 if (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
&&
1129 ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0) &&
1130 ntohs(dstport
) != sav
->remote_ike_port
)
1131 continue; /* skip this one - not a match - or not UDP */
1133 if ((sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&&
1134 ((sav
->flags
& SADB_X_EXT_NATT
) != 0)) ||
1135 (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
&&
1136 ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0))) {
1137 if (natt_candidate
== NULL
) {
1138 natt_candidate
= sav
;
1141 candidate
= natt_candidate
;
1143 if (no_natt_candidate
== NULL
) {
1144 no_natt_candidate
= sav
;
1147 candidate
= no_natt_candidate
;
1150 /* Which SA is the better ? */
1152 /* sanity check 2 */
1153 if (candidate
->lft_c
== NULL
|| sav
->lft_c
== NULL
)
1154 panic("key_do_allocsa_policy: "
1155 "lifetime_current is NULL.\n");
1157 /* What the best method is to compare ? */
1158 if (key_preferred_oldsa
) {
1159 if (candidate
->lft_c
->sadb_lifetime_addtime
>
1160 sav
->lft_c
->sadb_lifetime_addtime
) {
1161 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
1162 natt_candidate
= sav
;
1164 no_natt_candidate
= sav
;
1170 /* prefered new sa rather than old sa */
1171 if (candidate
->lft_c
->sadb_lifetime_addtime
<
1172 sav
->lft_c
->sadb_lifetime_addtime
) {
1174 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
1175 natt_candidate
= sav
;
1177 no_natt_candidate
= sav
;
1182 * prepared to delete the SA when there is more
1183 * suitable candidate and the lifetime of the SA is not
1186 if (d
->lft_c
->sadb_lifetime_addtime
!= 0) {
1191 /* choose latest if both types present */
1192 if (natt_candidate
== NULL
)
1193 candidate
= no_natt_candidate
;
1194 else if (no_natt_candidate
== NULL
)
1195 candidate
= natt_candidate
;
1196 else if (sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&& dstport
)
1197 candidate
= natt_candidate
;
1198 else if (natt_candidate
->lft_c
->sadb_lifetime_addtime
>
1199 no_natt_candidate
->lft_c
->sadb_lifetime_addtime
)
1200 candidate
= natt_candidate
;
1202 candidate
= no_natt_candidate
;
1205 candidate
->refcnt
++;
1206 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1207 printf("DP allocsa_policy cause "
1208 "refcnt++:%d SA:0x%llx\n", candidate
->refcnt
,
1209 (uint64_t)VM_KERNEL_ADDRPERM(candidate
)));
1215 * allocating a SA entry for a *INBOUND* packet.
1216 * Must call key_freesav() later.
1217 * OUT: positive: pointer to a sav.
1218 * NULL: not found, or error occurred.
1220 * In the comparison, source address will be ignored for RFC2401 conformance.
1221 * To quote, from section 4.1:
1222 * A security association is uniquely identified by a triple consisting
1223 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1224 * security protocol (AH or ESP) identifier.
1225 * Note that, however, we do need to keep source address in IPsec SA.
1226 * IKE specification and PF_KEY specification do assume that we
1227 * keep source address in IPsec SA. We see a tricky situation here.
1237 return key_allocsa_extended(family
, src
, dst
, proto
, spi
, NULL
);
1241 key_allocsa_extended(u_int family
,
1248 struct secasvar
*sav
, *match
;
1249 u_int stateidx
, state
, tmpidx
, matchidx
;
1250 struct sockaddr_in sin
;
1251 struct sockaddr_in6 sin6
;
1252 const u_int
*saorder_state_valid
;
1255 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1258 if (src
== NULL
|| dst
== NULL
)
1259 panic("key_allocsa: NULL pointer is passed.\n");
1262 * when both systems employ similar strategy to use a SA.
1263 * the search order is important even in the inbound case.
1265 if (key_preferred_oldsa
) {
1266 saorder_state_valid
= saorder_state_valid_prefer_old
;
1267 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
1269 saorder_state_valid
= saorder_state_valid_prefer_new
;
1270 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
1275 * XXX: to be checked internal IP header somewhere. Also when
1276 * IPsec tunnel packet is received. But ESP tunnel mode is
1277 * encrypted so we can't check internal IP header.
1280 * search a valid state list for inbound packet.
1281 * the search order is not important.
1284 matchidx
= arraysize
;
1285 lck_mtx_lock(sadb_mutex
);
1286 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
1287 if (sav
->spi
!= spi
)
1289 if (interface
!= NULL
&&
1290 sav
->sah
->ipsec_if
!= interface
) {
1293 if (proto
!= sav
->sah
->saidx
.proto
)
1295 if (family
!= sav
->sah
->saidx
.src
.ss_family
||
1296 family
!= sav
->sah
->saidx
.dst
.ss_family
)
1299 for (stateidx
= 0; stateidx
< matchidx
; stateidx
++) {
1300 state
= saorder_state_valid
[stateidx
];
1301 if (sav
->state
== state
) {
1306 if (tmpidx
>= matchidx
)
1309 #if 0 /* don't check src */
1310 /* check src address */
1313 bzero(&sin
, sizeof(sin
));
1314 sin
.sin_family
= AF_INET
;
1315 sin
.sin_len
= sizeof(sin
);
1316 bcopy(src
, &sin
.sin_addr
,
1317 sizeof(sin
.sin_addr
));
1318 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
1319 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0)
1323 bzero(&sin6
, sizeof(sin6
));
1324 sin6
.sin6_family
= AF_INET6
;
1325 sin6
.sin6_len
= sizeof(sin6
);
1326 bcopy(src
, &sin6
.sin6_addr
,
1327 sizeof(sin6
.sin6_addr
));
1328 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
1329 /* kame fake scopeid */
1330 sin6
.sin6_scope_id
=
1331 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
1332 sin6
.sin6_addr
.s6_addr16
[1] = 0;
1334 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
1335 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0)
1339 ipseclog((LOG_DEBUG
, "key_allocsa: "
1340 "unknown address family=%d.\n",
1346 /* check dst address */
1349 bzero(&sin
, sizeof(sin
));
1350 sin
.sin_family
= AF_INET
;
1351 sin
.sin_len
= sizeof(sin
);
1352 bcopy(dst
, &sin
.sin_addr
,
1353 sizeof(sin
.sin_addr
));
1354 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
1355 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0)
1360 bzero(&sin6
, sizeof(sin6
));
1361 sin6
.sin6_family
= AF_INET6
;
1362 sin6
.sin6_len
= sizeof(sin6
);
1363 bcopy(dst
, &sin6
.sin6_addr
,
1364 sizeof(sin6
.sin6_addr
));
1365 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
1366 /* kame fake scopeid */
1367 sin6
.sin6_scope_id
=
1368 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
1369 sin6
.sin6_addr
.s6_addr16
[1] = 0;
1371 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
1372 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0)
1376 ipseclog((LOG_DEBUG
, "key_allocsa: "
1377 "unknown address family=%d.\n", family
));
1388 lck_mtx_unlock(sadb_mutex
);
1393 lck_mtx_unlock(sadb_mutex
);
1394 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1395 printf("DP allocsa cause refcnt++:%d SA:0x%llx\n",
1396 match
->refcnt
, (uint64_t)VM_KERNEL_ADDRPERM(match
)));
1401 key_natt_get_translated_port(
1402 struct secasvar
*outsav
)
1405 struct secasindex saidx
;
1406 struct secashead
*sah
;
1407 u_int stateidx
, state
;
1408 const u_int
*saorder_state_valid
;
1411 /* get sa for incoming */
1412 saidx
.mode
= outsav
->sah
->saidx
.mode
;
1414 saidx
.proto
= outsav
->sah
->saidx
.proto
;
1415 bcopy(&outsav
->sah
->saidx
.src
, &saidx
.dst
, sizeof(struct sockaddr_in
));
1416 bcopy(&outsav
->sah
->saidx
.dst
, &saidx
.src
, sizeof(struct sockaddr_in
));
1418 lck_mtx_lock(sadb_mutex
);
1419 LIST_FOREACH(sah
, &sahtree
, chain
) {
1420 if (sah
->state
== SADB_SASTATE_DEAD
)
1422 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_MODE
))
1425 lck_mtx_unlock(sadb_mutex
);
1430 * Found sah - now go thru list of SAs and find
1431 * matching remote ike port. If found - set
1432 * sav->natt_encapsulated_src_port and return the port.
1435 * search a valid state list for outbound packet.
1436 * This search order is important.
1438 if (key_preferred_oldsa
) {
1439 saorder_state_valid
= saorder_state_valid_prefer_old
;
1440 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
1442 saorder_state_valid
= saorder_state_valid_prefer_new
;
1443 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
1446 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
1447 state
= saorder_state_valid
[stateidx
];
1448 if (key_do_get_translated_port(sah
, outsav
, state
)) {
1449 lck_mtx_unlock(sadb_mutex
);
1450 return outsav
->natt_encapsulated_src_port
;
1453 lck_mtx_unlock(sadb_mutex
);
1458 key_do_get_translated_port(
1459 struct secashead
*sah
,
1460 struct secasvar
*outsav
,
1463 struct secasvar
*currsav
, *nextsav
, *candidate
;
1466 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1471 for (currsav
= LIST_FIRST(&sah
->savtree
[state
]);
1473 currsav
= nextsav
) {
1475 nextsav
= LIST_NEXT(currsav
, chain
);
1478 KEY_CHKSASTATE(currsav
->state
, state
, "key_do_get_translated_port");
1480 if ((currsav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) == 0 ||
1481 currsav
->remote_ike_port
!= outsav
->remote_ike_port
)
1484 if (candidate
== NULL
) {
1485 candidate
= currsav
;
1489 /* Which SA is the better ? */
1491 /* sanity check 2 */
1492 if (candidate
->lft_c
== NULL
|| currsav
->lft_c
== NULL
)
1493 panic("key_do_get_translated_port: "
1494 "lifetime_current is NULL.\n");
1496 /* What the best method is to compare ? */
1497 if (key_preferred_oldsa
) {
1498 if (candidate
->lft_c
->sadb_lifetime_addtime
>
1499 currsav
->lft_c
->sadb_lifetime_addtime
) {
1500 candidate
= currsav
;
1506 /* prefered new sa rather than old sa */
1507 if (candidate
->lft_c
->sadb_lifetime_addtime
<
1508 currsav
->lft_c
->sadb_lifetime_addtime
)
1509 candidate
= currsav
;
1513 outsav
->natt_encapsulated_src_port
= candidate
->natt_encapsulated_src_port
;
1521 * Must be called after calling key_allocsp().
1525 struct secpolicy
*sp
,
1531 panic("key_freesp: NULL pointer is passed.\n");
1534 lck_mtx_lock(sadb_mutex
);
1536 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1538 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1539 printf("DP freesp cause refcnt--:%d SP:0x%llx\n",
1540 sp
->refcnt
, (uint64_t)VM_KERNEL_ADDRPERM(sp
)));
1542 if (sp
->refcnt
== 0)
1545 lck_mtx_unlock(sadb_mutex
);
1550 * Must be called after calling key_allocsa().
1551 * This function is called by key_freesp() to free some SA allocated
1556 struct secasvar
*sav
,
1562 panic("key_freesav: NULL pointer is passed.\n");
1565 lck_mtx_lock(sadb_mutex
);
1567 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1569 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1570 printf("DP freesav cause refcnt--:%d SA:0x%llx SPI %u\n",
1571 sav
->refcnt
, (uint64_t)VM_KERNEL_ADDRPERM(sav
),
1572 (u_int32_t
)ntohl(sav
->spi
)));
1574 if (sav
->refcnt
== 0)
1577 lck_mtx_unlock(sadb_mutex
);
1581 /* %%% SPD management */
1583 * free security policy entry.
1587 struct secpolicy
*sp
)
1592 panic("key_delsp: NULL pointer is passed.\n");
1594 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1595 sp
->state
= IPSEC_SPSTATE_DEAD
;
1598 return; /* can't free */
1600 /* remove from SP index */
1601 if (__LIST_CHAINED(sp
)) {
1602 LIST_REMOVE(sp
, chain
);
1603 ipsec_policy_count
--;
1606 if (sp
->spidx
.internal_if
) {
1607 ifnet_release(sp
->spidx
.internal_if
);
1608 sp
->spidx
.internal_if
= NULL
;
1612 ifnet_release(sp
->ipsec_if
);
1613 sp
->ipsec_if
= NULL
;
1616 if (sp
->outgoing_if
) {
1617 ifnet_release(sp
->outgoing_if
);
1618 sp
->outgoing_if
= NULL
;
1622 struct ipsecrequest
*isr
= sp
->req
, *nextisr
;
1624 while (isr
!= NULL
) {
1625 nextisr
= isr
->next
;
1630 keydb_delsecpolicy(sp
);
1637 * OUT: NULL : not found
1638 * others : found, pointer to a SP.
1640 static struct secpolicy
*
1642 struct secpolicyindex
*spidx
)
1644 struct secpolicy
*sp
;
1646 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1650 panic("key_getsp: NULL pointer is passed.\n");
1652 LIST_FOREACH(sp
, &sptree
[spidx
->dir
], chain
) {
1653 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1655 if (key_cmpspidx_exactly(spidx
, &sp
->spidx
)) {
1666 * OUT: NULL : not found
1667 * others : found, pointer to a SP.
1673 struct secpolicy
*sp
;
1675 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1677 lck_mtx_lock(sadb_mutex
);
1678 sp
= __key_getspbyid(id
);
1679 lck_mtx_unlock(sadb_mutex
);
1684 static struct secpolicy
*
1685 __key_getspbyid(u_int32_t id
)
1687 struct secpolicy
*sp
;
1689 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1691 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_INBOUND
], chain
) {
1692 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1700 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_OUTBOUND
], chain
) {
1701 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1715 struct secpolicy
*newsp
= NULL
;
1717 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1718 newsp
= keydb_newsecpolicy();
1729 * create secpolicy structure from sadb_x_policy structure.
1730 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1731 * so must be set properly later.
1735 struct sadb_x_policy
*xpl0
,
1739 struct secpolicy
*newsp
;
1741 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1745 panic("key_msg2sp: NULL pointer was passed.\n");
1746 if (len
< sizeof(*xpl0
))
1747 panic("key_msg2sp: invalid length.\n");
1748 if (len
!= PFKEY_EXTLEN(xpl0
)) {
1749 ipseclog((LOG_DEBUG
, "key_msg2sp: Invalid msg length.\n"));
1754 if ((newsp
= key_newsp()) == NULL
) {
1759 newsp
->spidx
.dir
= xpl0
->sadb_x_policy_dir
;
1760 newsp
->policy
= xpl0
->sadb_x_policy_type
;
1763 switch (xpl0
->sadb_x_policy_type
) {
1764 case IPSEC_POLICY_DISCARD
:
1765 case IPSEC_POLICY_GENERATE
:
1766 case IPSEC_POLICY_NONE
:
1767 case IPSEC_POLICY_ENTRUST
:
1768 case IPSEC_POLICY_BYPASS
:
1772 case IPSEC_POLICY_IPSEC
:
1775 struct sadb_x_ipsecrequest
*xisr
;
1776 struct ipsecrequest
**p_isr
= &newsp
->req
;
1778 /* validity check */
1779 if (PFKEY_EXTLEN(xpl0
) < sizeof(*xpl0
)) {
1780 ipseclog((LOG_DEBUG
,
1781 "key_msg2sp: Invalid msg length.\n"));
1782 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1787 tlen
= PFKEY_EXTLEN(xpl0
) - sizeof(*xpl0
);
1788 xisr
= (struct sadb_x_ipsecrequest
*)(xpl0
+ 1);
1791 if (tlen
< sizeof(*xisr
)) {
1792 ipseclog((LOG_DEBUG
, "key_msg2sp: "
1793 "invalid ipsecrequest.\n"));
1794 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1800 if (xisr
->sadb_x_ipsecrequest_len
< sizeof(*xisr
)) {
1801 ipseclog((LOG_DEBUG
, "key_msg2sp: "
1802 "invalid ipsecrequest length.\n"));
1803 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1808 /* allocate request buffer */
1809 KMALLOC_WAIT(*p_isr
, struct ipsecrequest
*, sizeof(**p_isr
));
1810 if ((*p_isr
) == NULL
) {
1811 ipseclog((LOG_DEBUG
,
1812 "key_msg2sp: No more memory.\n"));
1813 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1817 bzero(*p_isr
, sizeof(**p_isr
));
1820 (*p_isr
)->next
= NULL
;
1822 switch (xisr
->sadb_x_ipsecrequest_proto
) {
1825 case IPPROTO_IPCOMP
:
1828 ipseclog((LOG_DEBUG
,
1829 "key_msg2sp: invalid proto type=%u\n",
1830 xisr
->sadb_x_ipsecrequest_proto
));
1831 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1832 *error
= EPROTONOSUPPORT
;
1835 (*p_isr
)->saidx
.proto
= xisr
->sadb_x_ipsecrequest_proto
;
1837 switch (xisr
->sadb_x_ipsecrequest_mode
) {
1838 case IPSEC_MODE_TRANSPORT
:
1839 case IPSEC_MODE_TUNNEL
:
1841 case IPSEC_MODE_ANY
:
1843 ipseclog((LOG_DEBUG
,
1844 "key_msg2sp: invalid mode=%u\n",
1845 xisr
->sadb_x_ipsecrequest_mode
));
1846 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1850 (*p_isr
)->saidx
.mode
= xisr
->sadb_x_ipsecrequest_mode
;
1852 switch (xisr
->sadb_x_ipsecrequest_level
) {
1853 case IPSEC_LEVEL_DEFAULT
:
1854 case IPSEC_LEVEL_USE
:
1855 case IPSEC_LEVEL_REQUIRE
:
1857 case IPSEC_LEVEL_UNIQUE
:
1858 /* validity check */
1860 * If range violation of reqid, kernel will
1861 * update it, don't refuse it.
1863 if (xisr
->sadb_x_ipsecrequest_reqid
1864 > IPSEC_MANUAL_REQID_MAX
) {
1865 ipseclog((LOG_DEBUG
,
1866 "key_msg2sp: reqid=%d range "
1867 "violation, updated by kernel.\n",
1868 xisr
->sadb_x_ipsecrequest_reqid
));
1869 xisr
->sadb_x_ipsecrequest_reqid
= 0;
1872 /* allocate new reqid id if reqid is zero. */
1873 if (xisr
->sadb_x_ipsecrequest_reqid
== 0) {
1875 if ((reqid
= key_newreqid()) == 0) {
1876 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1880 (*p_isr
)->saidx
.reqid
= reqid
;
1881 xisr
->sadb_x_ipsecrequest_reqid
= reqid
;
1883 /* set it for manual keying. */
1884 (*p_isr
)->saidx
.reqid
=
1885 xisr
->sadb_x_ipsecrequest_reqid
;
1890 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid level=%u\n",
1891 xisr
->sadb_x_ipsecrequest_level
));
1892 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1896 (*p_isr
)->level
= xisr
->sadb_x_ipsecrequest_level
;
1898 /* set IP addresses if there */
1899 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
1900 struct sockaddr
*paddr
;
1902 if (tlen
< xisr
->sadb_x_ipsecrequest_len
) {
1903 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1904 "address length.\n"));
1905 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1910 paddr
= (struct sockaddr
*)(xisr
+ 1);
1911 uint8_t src_len
= paddr
->sa_len
;
1913 if (xisr
->sadb_x_ipsecrequest_len
< src_len
) {
1914 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1915 "invalid source address length.\n"));
1916 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1921 /* validity check */
1923 > sizeof((*p_isr
)->saidx
.src
)) {
1924 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1925 "address length.\n"));
1926 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1931 bcopy(paddr
, &(*p_isr
)->saidx
.src
,
1932 MIN(paddr
->sa_len
, sizeof((*p_isr
)->saidx
.src
)));
1934 paddr
= (struct sockaddr
*)((caddr_t
)paddr
+ paddr
->sa_len
);
1935 uint8_t dst_len
= paddr
->sa_len
;
1937 if (xisr
->sadb_x_ipsecrequest_len
< (src_len
+ dst_len
)) {
1938 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1939 "invalid dest address length.\n"));
1940 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1945 /* validity check */
1947 > sizeof((*p_isr
)->saidx
.dst
)) {
1948 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1949 "address length.\n"));
1950 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1955 bcopy(paddr
, &(*p_isr
)->saidx
.dst
,
1956 MIN(paddr
->sa_len
, sizeof((*p_isr
)->saidx
.dst
)));
1959 (*p_isr
)->sp
= newsp
;
1961 /* initialization for the next. */
1962 p_isr
= &(*p_isr
)->next
;
1963 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
1965 /* validity check */
1967 ipseclog((LOG_DEBUG
, "key_msg2sp: becoming tlen < 0.\n"));
1968 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1973 xisr
= (struct sadb_x_ipsecrequest
*)(void *)
1974 ((caddr_t
)xisr
+ xisr
->sadb_x_ipsecrequest_len
);
1979 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid policy type.\n"));
1980 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1992 lck_mtx_lock(sadb_mutex
);
1993 static u_int32_t auto_reqid
= IPSEC_MANUAL_REQID_MAX
+ 1;
1996 /* The reqid must be limited to 16 bits because the PF_KEY message format only uses
1997 16 bits for this field. Once it becomes larger than 16 bits - ipsec fails to
1998 work anymore. Changing the PF_KEY message format would introduce compatibility
1999 issues. This code now tests to see if the tentative reqid is in use */
2002 struct secpolicy
*sp
;
2003 struct ipsecrequest
*isr
;
2006 auto_reqid
= (auto_reqid
== 0xFFFF
2007 ? IPSEC_MANUAL_REQID_MAX
+ 1 : auto_reqid
+ 1);
2009 /* check for uniqueness */
2011 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2012 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2013 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2014 if (isr
->saidx
.reqid
== auto_reqid
) {
2027 lck_mtx_unlock(sadb_mutex
);
2032 * copy secpolicy struct to sadb_x_policy structure indicated.
2036 struct secpolicy
*sp
)
2038 struct sadb_x_policy
*xpl
;
2045 panic("key_sp2msg: NULL pointer was passed.\n");
2047 tlen
= key_getspreqmsglen(sp
);
2049 m
= key_alloc_mbuf(tlen
);
2050 if (!m
|| m
->m_next
) { /*XXX*/
2058 xpl
= mtod(m
, struct sadb_x_policy
*);
2061 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(tlen
);
2062 xpl
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2063 xpl
->sadb_x_policy_type
= sp
->policy
;
2064 xpl
->sadb_x_policy_dir
= sp
->spidx
.dir
;
2065 xpl
->sadb_x_policy_id
= sp
->id
;
2066 p
= (caddr_t
)xpl
+ sizeof(*xpl
);
2068 /* if is the policy for ipsec ? */
2069 if (sp
->policy
== IPSEC_POLICY_IPSEC
) {
2070 struct sadb_x_ipsecrequest
*xisr
;
2071 struct ipsecrequest
*isr
;
2073 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2075 xisr
= (struct sadb_x_ipsecrequest
*)(void *)p
;
2077 xisr
->sadb_x_ipsecrequest_proto
= isr
->saidx
.proto
;
2078 xisr
->sadb_x_ipsecrequest_mode
= isr
->saidx
.mode
;
2079 xisr
->sadb_x_ipsecrequest_level
= isr
->level
;
2080 xisr
->sadb_x_ipsecrequest_reqid
= isr
->saidx
.reqid
;
2083 bcopy(&isr
->saidx
.src
, p
, isr
->saidx
.src
.ss_len
);
2084 p
+= isr
->saidx
.src
.ss_len
;
2085 bcopy(&isr
->saidx
.dst
, p
, isr
->saidx
.dst
.ss_len
);
2086 p
+= isr
->saidx
.src
.ss_len
;
2088 xisr
->sadb_x_ipsecrequest_len
=
2089 PFKEY_ALIGN8(sizeof(*xisr
)
2090 + isr
->saidx
.src
.ss_len
2091 + isr
->saidx
.dst
.ss_len
);
2098 /* m will not be freed nor modified */
2099 static struct mbuf
*
2100 key_gather_mbuf(struct mbuf
*m
, const struct sadb_msghdr
*mhp
,
2101 int ndeep
, int nitem
, int *items
)
2105 struct mbuf
*result
= NULL
, *n
;
2108 if (m
== NULL
|| mhp
== NULL
)
2109 panic("null pointer passed to key_gather");
2111 for (i
= 0; i
< nitem
; i
++) {
2113 if (idx
< 0 || idx
> SADB_EXT_MAX
)
2115 /* don't attempt to pull empty extension */
2116 if (idx
== SADB_EXT_RESERVED
&& mhp
->msg
== NULL
)
2118 if (idx
!= SADB_EXT_RESERVED
&&
2119 (mhp
->ext
[idx
] == NULL
|| mhp
->extlen
[idx
] == 0))
2122 if (idx
== SADB_EXT_RESERVED
) {
2123 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2124 MGETHDR(n
, M_WAITOK
, MT_DATA
); // sadb_msg len < MHLEN - enforced by _CASSERT
2129 m_copydata(m
, 0, sizeof(struct sadb_msg
),
2131 } else if (i
< ndeep
) {
2132 len
= mhp
->extlen
[idx
];
2133 n
= key_alloc_mbuf(len
);
2134 if (!n
|| n
->m_next
) { /*XXX*/
2139 m_copydata(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
2142 n
= m_copym(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
2154 if ((result
->m_flags
& M_PKTHDR
) != 0) {
2155 result
->m_pkthdr
.len
= 0;
2156 for (n
= result
; n
; n
= n
->m_next
)
2157 result
->m_pkthdr
.len
+= n
->m_len
;
2168 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
2169 * add a entry to SP database, when received
2170 * <base, address(SD), (lifetime(H),) policy>
2172 * Adding to SP database,
2174 * <base, address(SD), (lifetime(H),) policy>
2175 * to the socket which was send.
2177 * SPDADD set a unique policy entry.
2178 * SPDSETIDX like SPDADD without a part of policy requests.
2179 * SPDUPDATE replace a unique policy entry.
2181 * m will always be freed.
2187 const struct sadb_msghdr
*mhp
)
2189 struct sadb_address
*src0
, *dst0
, *src1
= NULL
, *dst1
= NULL
;
2190 struct sadb_x_policy
*xpl0
, *xpl
;
2191 struct sadb_lifetime
*lft
= NULL
;
2192 struct secpolicyindex spidx
;
2193 struct secpolicy
*newsp
;
2195 ifnet_t internal_if
= NULL
;
2196 char *outgoing_if
= NULL
;
2197 char *ipsec_if
= NULL
;
2198 struct sadb_x_ipsecif
*ipsecifopts
= NULL
;
2200 int use_src_range
= 0;
2201 int use_dst_range
= 0;
2202 int init_disabled
= 0;
2203 int address_family
, address_len
;
2205 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2208 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2209 panic("key_spdadd: NULL pointer is passed.\n");
2211 if (mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
] != NULL
&& mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
] != NULL
) {
2214 if (mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_START
] != NULL
&& mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_END
] != NULL
) {
2218 if ((!use_src_range
&& mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
) ||
2219 (!use_dst_range
&& mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) ||
2220 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
2221 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2222 return key_senderror(so
, m
, EINVAL
);
2224 if ((use_src_range
&& (mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_SRC_START
] < sizeof(struct sadb_address
)
2225 || mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_SRC_END
] < sizeof(struct sadb_address
))) ||
2226 (!use_src_range
&& mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
)) ||
2227 (use_dst_range
&& (mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_DST_START
] < sizeof(struct sadb_address
)
2228 || mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_DST_END
] < sizeof(struct sadb_address
))) ||
2229 (!use_dst_range
&& mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) ||
2230 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2231 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2232 return key_senderror(so
, m
, EINVAL
);
2234 if (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
) {
2235 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
]
2236 < sizeof(struct sadb_lifetime
)) {
2237 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2238 return key_senderror(so
, m
, EINVAL
);
2240 lft
= (struct sadb_lifetime
*)
2241 (void *)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
2243 if (mhp
->ext
[SADB_X_EXT_IPSECIF
] != NULL
) {
2244 if (mhp
->extlen
[SADB_X_EXT_IPSECIF
] < sizeof(struct sadb_x_ipsecif
)) {
2245 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2246 return key_senderror(so
, m
, EINVAL
);
2250 if (use_src_range
) {
2251 src0
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
];
2252 src1
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
];
2254 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
2256 if (use_dst_range
) {
2257 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_START
];
2258 dst1
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_END
];
2260 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
2262 xpl0
= (struct sadb_x_policy
*)(void *)mhp
->ext
[SADB_X_EXT_POLICY
];
2263 ipsecifopts
= (struct sadb_x_ipsecif
*)(void *)mhp
->ext
[SADB_X_EXT_IPSECIF
];
2265 /* check addresses */
2266 address_family
= ((struct sockaddr
*)(src0
+ 1))->sa_family
;
2267 address_len
= ((struct sockaddr
*)(src0
+ 1))->sa_len
;
2268 if (use_src_range
) {
2269 if (((struct sockaddr
*)(src1
+ 1))->sa_family
!= address_family
||
2270 ((struct sockaddr
*)(src1
+ 1))->sa_len
!= address_len
) {
2271 return key_senderror(so
, m
, EINVAL
);
2274 if (((struct sockaddr
*)(dst0
+ 1))->sa_family
!= address_family
||
2275 ((struct sockaddr
*)(dst0
+ 1))->sa_len
!= address_len
) {
2276 return key_senderror(so
, m
, EINVAL
);
2278 if (use_dst_range
) {
2279 if (((struct sockaddr
*)(dst1
+ 1))->sa_family
!= address_family
||
2280 ((struct sockaddr
*)(dst1
+ 1))->sa_len
!= address_len
) {
2281 return key_senderror(so
, m
, EINVAL
);
2285 /* checking the direction. */
2286 switch (xpl0
->sadb_x_policy_dir
) {
2287 case IPSEC_DIR_INBOUND
:
2288 case IPSEC_DIR_OUTBOUND
:
2291 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid SP direction.\n"));
2292 mhp
->msg
->sadb_msg_errno
= EINVAL
;
2297 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
2298 if (xpl0
->sadb_x_policy_type
== IPSEC_POLICY_ENTRUST
2299 || xpl0
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
) {
2300 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid policy type.\n"));
2301 return key_senderror(so
, m
, EINVAL
);
2304 /* policy requests are mandatory when action is ipsec. */
2305 if (mhp
->msg
->sadb_msg_type
!= SADB_X_SPDSETIDX
2306 && xpl0
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
2307 && mhp
->extlen
[SADB_X_EXT_POLICY
] <= sizeof(*xpl0
)) {
2308 ipseclog((LOG_DEBUG
, "key_spdadd: some policy requests part required.\n"));
2309 return key_senderror(so
, m
, EINVAL
);
2312 /* Process interfaces */
2313 if (ipsecifopts
!= NULL
) {
2314 if (ipsecifopts
->sadb_x_ipsecif_internal_if
[0]) {
2315 ifnet_find_by_name(ipsecifopts
->sadb_x_ipsecif_internal_if
, &internal_if
);
2317 if (ipsecifopts
->sadb_x_ipsecif_outgoing_if
[0]) {
2318 outgoing_if
= ipsecifopts
->sadb_x_ipsecif_outgoing_if
;
2320 if (ipsecifopts
->sadb_x_ipsecif_ipsec_if
[0]) {
2321 ipsec_if
= ipsecifopts
->sadb_x_ipsecif_ipsec_if
;
2323 init_disabled
= ipsecifopts
->sadb_x_ipsecif_init_disabled
;
2327 /* XXX boundary check against sa_len */
2328 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2331 src0
->sadb_address_prefixlen
,
2332 dst0
->sadb_address_prefixlen
,
2333 src0
->sadb_address_proto
,
2335 use_src_range
? src0
+ 1 : NULL
,
2336 use_src_range
? src1
+ 1 : NULL
,
2337 use_dst_range
? dst0
+ 1 : NULL
,
2338 use_dst_range
? dst1
+ 1 : NULL
,
2342 * checking there is SP already or not.
2343 * SPDUPDATE doesn't depend on whether there is a SP or not.
2344 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
2347 lck_mtx_lock(sadb_mutex
);
2348 newsp
= key_getsp(&spidx
);
2349 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
2351 newsp
->state
= IPSEC_SPSTATE_DEAD
;
2352 key_freesp(newsp
, KEY_SADB_LOCKED
);
2355 if (newsp
!= NULL
) {
2356 key_freesp(newsp
, KEY_SADB_LOCKED
);
2357 ipseclog((LOG_DEBUG
, "key_spdadd: a SP entry exists already.\n"));
2358 lck_mtx_unlock(sadb_mutex
);
2360 ifnet_release(internal_if
);
2363 return key_senderror(so
, m
, EEXIST
);
2366 lck_mtx_unlock(sadb_mutex
);
2368 /* allocation new SP entry */
2369 if ((newsp
= key_msg2sp(xpl0
, PFKEY_EXTLEN(xpl0
), &error
)) == NULL
) {
2371 ifnet_release(internal_if
);
2374 return key_senderror(so
, m
, error
);
2377 if ((newsp
->id
= key_getnewspid()) == 0) {
2378 keydb_delsecpolicy(newsp
);
2380 ifnet_release(internal_if
);
2383 return key_senderror(so
, m
, ENOBUFS
);
2386 /* XXX boundary check against sa_len */
2387 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2390 src0
->sadb_address_prefixlen
,
2391 dst0
->sadb_address_prefixlen
,
2392 src0
->sadb_address_proto
,
2394 use_src_range
? src0
+ 1 : NULL
,
2395 use_src_range
? src1
+ 1 : NULL
,
2396 use_dst_range
? dst0
+ 1 : NULL
,
2397 use_dst_range
? dst1
+ 1 : NULL
,
2402 * allow IPv6 over IPv4 or IPv4 over IPv6 tunnels using ESP -
2403 * otherwise reject if inner and outer address families not equal
2405 if (newsp
->req
&& newsp
->req
->saidx
.src
.ss_family
) {
2406 struct sockaddr
*sa
;
2407 sa
= (struct sockaddr
*)(src0
+ 1);
2408 if (sa
->sa_family
!= newsp
->req
->saidx
.src
.ss_family
) {
2409 if (newsp
->req
->saidx
.mode
!= IPSEC_MODE_TUNNEL
|| newsp
->req
->saidx
.proto
!= IPPROTO_ESP
) {
2410 keydb_delsecpolicy(newsp
);
2412 ifnet_release(internal_if
);
2415 return key_senderror(so
, m
, EINVAL
);
2419 if (newsp
->req
&& newsp
->req
->saidx
.dst
.ss_family
) {
2420 struct sockaddr
*sa
;
2421 sa
= (struct sockaddr
*)(dst0
+ 1);
2422 if (sa
->sa_family
!= newsp
->req
->saidx
.dst
.ss_family
) {
2423 if (newsp
->req
->saidx
.mode
!= IPSEC_MODE_TUNNEL
|| newsp
->req
->saidx
.proto
!= IPPROTO_ESP
) {
2424 keydb_delsecpolicy(newsp
);
2426 ifnet_release(internal_if
);
2429 return key_senderror(so
, m
, EINVAL
);
2436 newsp
->created
= tv
.tv_sec
;
2437 newsp
->lastused
= tv
.tv_sec
;
2438 newsp
->lifetime
= lft
? lft
->sadb_lifetime_addtime
: 0;
2439 newsp
->validtime
= lft
? lft
->sadb_lifetime_usetime
: 0;
2441 if (outgoing_if
!= NULL
) {
2442 ifnet_find_by_name(outgoing_if
, &newsp
->outgoing_if
);
2444 if (ipsec_if
!= NULL
) {
2445 ifnet_find_by_name(ipsec_if
, &newsp
->ipsec_if
);
2447 if (init_disabled
> 0) {
2448 newsp
->disabled
= 1;
2451 newsp
->refcnt
= 1; /* do not reclaim until I say I do */
2452 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
2453 lck_mtx_lock(sadb_mutex
);
2455 * policies of type generate should be at the end of the SPD
2456 * because they function as default discard policies
2457 * Don't start timehandler for generate policies
2459 if (newsp
->policy
== IPSEC_POLICY_GENERATE
)
2460 LIST_INSERT_TAIL(&sptree
[newsp
->spidx
.dir
], newsp
, secpolicy
, chain
);
2461 else { /* XXX until we have policy ordering in the kernel */
2462 struct secpolicy
*tmpsp
;
2464 LIST_FOREACH(tmpsp
, &sptree
[newsp
->spidx
.dir
], chain
)
2465 if (tmpsp
->policy
== IPSEC_POLICY_GENERATE
)
2468 LIST_INSERT_BEFORE(tmpsp
, newsp
, chain
);
2470 LIST_INSERT_TAIL(&sptree
[newsp
->spidx
.dir
], newsp
, secpolicy
, chain
);
2471 key_start_timehandler();
2474 ipsec_policy_count
++;
2475 /* Turn off the ipsec bypass */
2476 if (ipsec_bypass
!= 0)
2479 /* delete the entry in spacqtree */
2480 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
2481 struct secspacq
*spacq
;
2482 if ((spacq
= key_getspacq(&spidx
)) != NULL
) {
2483 /* reset counter in order to deletion by timehandler. */
2485 spacq
->created
= tv
.tv_sec
;
2489 lck_mtx_unlock(sadb_mutex
);
2492 struct mbuf
*n
, *mpolicy
;
2493 struct sadb_msg
*newmsg
;
2496 /* create new sadb_msg to reply. */
2498 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2499 SADB_EXT_LIFETIME_HARD
, SADB_EXT_ADDRESS_SRC
,
2500 SADB_EXT_ADDRESS_DST
, SADB_X_EXT_ADDR_RANGE_SRC_START
, SADB_X_EXT_ADDR_RANGE_SRC_END
,
2501 SADB_X_EXT_ADDR_RANGE_DST_START
, SADB_X_EXT_ADDR_RANGE_DST_END
};
2502 n
= key_gather_mbuf(m
, mhp
, 2, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2504 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2505 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
,
2506 SADB_X_EXT_ADDR_RANGE_SRC_START
, SADB_X_EXT_ADDR_RANGE_SRC_END
,
2507 SADB_X_EXT_ADDR_RANGE_DST_START
, SADB_X_EXT_ADDR_RANGE_DST_END
};
2508 n
= key_gather_mbuf(m
, mhp
, 2, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2511 return key_senderror(so
, m
, ENOBUFS
);
2513 if (n
->m_len
< sizeof(*newmsg
)) {
2514 n
= m_pullup(n
, sizeof(*newmsg
));
2516 return key_senderror(so
, m
, ENOBUFS
);
2518 newmsg
= mtod(n
, struct sadb_msg
*);
2519 newmsg
->sadb_msg_errno
= 0;
2520 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2523 mpolicy
= m_pulldown(n
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)),
2524 sizeof(*xpl
), &off
);
2525 if (mpolicy
== NULL
) {
2526 /* n is already freed */
2527 return key_senderror(so
, m
, ENOBUFS
);
2529 xpl
= (struct sadb_x_policy
*)(void *)(mtod(mpolicy
, caddr_t
) + off
);
2530 if (xpl
->sadb_x_policy_exttype
!= SADB_X_EXT_POLICY
) {
2532 return key_senderror(so
, m
, EINVAL
);
2534 xpl
->sadb_x_policy_id
= newsp
->id
;
2537 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2542 * get new policy id.
2548 key_getnewspid(void)
2550 u_int32_t newid
= 0;
2551 int count
= key_spi_trycnt
; /* XXX */
2552 struct secpolicy
*sp
;
2554 /* when requesting to allocate spi ranged */
2555 lck_mtx_lock(sadb_mutex
);
2557 newid
= (policy_id
= (policy_id
== ~0 ? 1 : policy_id
+ 1));
2559 if ((sp
= __key_getspbyid(newid
)) == NULL
)
2562 key_freesp(sp
, KEY_SADB_LOCKED
);
2564 lck_mtx_unlock(sadb_mutex
);
2565 if (count
== 0 || newid
== 0) {
2566 ipseclog((LOG_DEBUG
, "key_getnewspid: to allocate policy id is failed.\n"));
2574 * SADB_SPDDELETE processing
2576 * <base, address(SD), policy(*)>
2577 * from the user(?), and set SADB_SASTATE_DEAD,
2579 * <base, address(SD), policy(*)>
2581 * policy(*) including direction of policy.
2583 * m will always be freed.
2589 const struct sadb_msghdr
*mhp
)
2591 struct sadb_address
*src0
, *dst0
, *src1
= NULL
, *dst1
= NULL
;
2592 struct sadb_x_policy
*xpl0
;
2593 struct secpolicyindex spidx
;
2594 struct secpolicy
*sp
;
2595 ifnet_t internal_if
= NULL
;
2596 struct sadb_x_ipsecif
*ipsecifopts
= NULL
;
2597 int use_src_range
= 0;
2598 int use_dst_range
= 0;
2600 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2603 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2604 panic("key_spddelete: NULL pointer is passed.\n");
2606 if (mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
] != NULL
&& mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
] != NULL
) {
2609 if (mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_START
] != NULL
&& mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_END
] != NULL
) {
2613 if ((!use_src_range
&& mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
) ||
2614 (!use_dst_range
&& mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) ||
2615 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
2616 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
2617 return key_senderror(so
, m
, EINVAL
);
2619 if ((use_src_range
&& (mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_SRC_START
] < sizeof(struct sadb_address
)
2620 || mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_SRC_END
] < sizeof(struct sadb_address
))) ||
2621 (!use_src_range
&& mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
)) ||
2622 (use_dst_range
&& (mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_DST_START
] < sizeof(struct sadb_address
)
2623 || mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_DST_END
] < sizeof(struct sadb_address
))) ||
2624 (!use_dst_range
&& mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) ||
2625 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2626 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
2627 return key_senderror(so
, m
, EINVAL
);
2630 if (use_src_range
) {
2631 src0
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
];
2632 src1
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
];
2634 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
2636 if (use_dst_range
) {
2637 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_START
];
2638 dst1
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_END
];
2640 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
2642 xpl0
= (struct sadb_x_policy
*)(void *)mhp
->ext
[SADB_X_EXT_POLICY
];
2643 ipsecifopts
= (struct sadb_x_ipsecif
*)(void *)mhp
->ext
[SADB_X_EXT_IPSECIF
];
2645 /* checking the direction. */
2646 switch (xpl0
->sadb_x_policy_dir
) {
2647 case IPSEC_DIR_INBOUND
:
2648 case IPSEC_DIR_OUTBOUND
:
2651 ipseclog((LOG_DEBUG
, "key_spddelete: Invalid SP direction.\n"));
2652 return key_senderror(so
, m
, EINVAL
);
2655 /* Process interfaces */
2656 if (ipsecifopts
!= NULL
) {
2657 if (ipsecifopts
->sadb_x_ipsecif_internal_if
[0]) {
2658 ifnet_find_by_name(ipsecifopts
->sadb_x_ipsecif_internal_if
, &internal_if
);
2663 /* XXX boundary check against sa_len */
2664 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2667 src0
->sadb_address_prefixlen
,
2668 dst0
->sadb_address_prefixlen
,
2669 src0
->sadb_address_proto
,
2671 use_src_range
? src0
+ 1 : NULL
,
2672 use_src_range
? src1
+ 1 : NULL
,
2673 use_dst_range
? dst0
+ 1 : NULL
,
2674 use_dst_range
? dst1
+ 1 : NULL
,
2677 /* Is there SP in SPD ? */
2678 lck_mtx_lock(sadb_mutex
);
2679 if ((sp
= key_getsp(&spidx
)) == NULL
) {
2680 ipseclog((LOG_DEBUG
, "key_spddelete: no SP found.\n"));
2681 lck_mtx_unlock(sadb_mutex
);
2683 ifnet_release(internal_if
);
2686 return key_senderror(so
, m
, EINVAL
);
2690 ifnet_release(internal_if
);
2694 /* save policy id to buffer to be returned. */
2695 xpl0
->sadb_x_policy_id
= sp
->id
;
2697 sp
->state
= IPSEC_SPSTATE_DEAD
;
2698 key_freesp(sp
, KEY_SADB_LOCKED
);
2699 lck_mtx_unlock(sadb_mutex
);
2704 struct sadb_msg
*newmsg
;
2705 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2706 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
,
2707 SADB_X_EXT_ADDR_RANGE_SRC_START
, SADB_X_EXT_ADDR_RANGE_SRC_END
,
2708 SADB_X_EXT_ADDR_RANGE_DST_START
, SADB_X_EXT_ADDR_RANGE_DST_END
};
2710 /* create new sadb_msg to reply. */
2711 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2713 return key_senderror(so
, m
, ENOBUFS
);
2715 newmsg
= mtod(n
, struct sadb_msg
*);
2716 newmsg
->sadb_msg_errno
= 0;
2717 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2720 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2725 * SADB_SPDDELETE2 processing
2728 * from the user(?), and set SADB_SASTATE_DEAD,
2732 * policy(*) including direction of policy.
2734 * m will always be freed.
2740 const struct sadb_msghdr
*mhp
)
2743 struct secpolicy
*sp
;
2745 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2748 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2749 panic("key_spddelete2: NULL pointer is passed.\n");
2751 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2752 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2753 ipseclog((LOG_DEBUG
, "key_spddelete2: invalid message is passed.\n"));
2754 key_senderror(so
, m
, EINVAL
);
2758 id
= ((struct sadb_x_policy
*)
2759 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2761 /* Is there SP in SPD ? */
2762 lck_mtx_lock(sadb_mutex
);
2763 if ((sp
= __key_getspbyid(id
)) == NULL
) {
2764 lck_mtx_unlock(sadb_mutex
);
2765 ipseclog((LOG_DEBUG
, "key_spddelete2: no SP found id:%u.\n", id
));
2766 return key_senderror(so
, m
, EINVAL
);
2769 sp
->state
= IPSEC_SPSTATE_DEAD
;
2770 key_freesp(sp
, KEY_SADB_LOCKED
);
2771 lck_mtx_unlock(sadb_mutex
);
2774 struct mbuf
*n
, *nn
;
2775 struct sadb_msg
*newmsg
;
2778 /* create new sadb_msg to reply. */
2779 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2782 return key_senderror(so
, m
, ENOBUFS
);
2783 MGETHDR(n
, M_WAITOK
, MT_DATA
);
2784 if (n
&& len
> MHLEN
) {
2785 MCLGET(n
, M_WAITOK
);
2786 if ((n
->m_flags
& M_EXT
) == 0) {
2792 return key_senderror(so
, m
, ENOBUFS
);
2798 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
2799 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2803 panic("length inconsistency in key_spddelete2");
2806 n
->m_next
= m_copym(m
, mhp
->extoff
[SADB_X_EXT_POLICY
],
2807 mhp
->extlen
[SADB_X_EXT_POLICY
], M_WAITOK
);
2810 return key_senderror(so
, m
, ENOBUFS
);
2813 n
->m_pkthdr
.len
= 0;
2814 for (nn
= n
; nn
; nn
= nn
->m_next
)
2815 n
->m_pkthdr
.len
+= nn
->m_len
;
2817 newmsg
= mtod(n
, struct sadb_msg
*);
2818 newmsg
->sadb_msg_errno
= 0;
2819 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2822 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2830 const struct sadb_msghdr
*mhp
)
2833 struct secpolicy
*sp
;
2835 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2838 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2839 panic("key_spdenable: NULL pointer is passed.\n");
2841 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2842 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2843 ipseclog((LOG_DEBUG
, "key_spdenable: invalid message is passed.\n"));
2844 key_senderror(so
, m
, EINVAL
);
2848 id
= ((struct sadb_x_policy
*)
2849 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2851 /* Is there SP in SPD ? */
2852 lck_mtx_lock(sadb_mutex
);
2853 if ((sp
= __key_getspbyid(id
)) == NULL
) {
2854 lck_mtx_unlock(sadb_mutex
);
2855 ipseclog((LOG_DEBUG
, "key_spdenable: no SP found id:%u.\n", id
));
2856 return key_senderror(so
, m
, EINVAL
);
2860 lck_mtx_unlock(sadb_mutex
);
2864 struct sadb_msg
*newmsg
;
2865 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
};
2867 /* create new sadb_msg to reply. */
2868 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2870 return key_senderror(so
, m
, ENOBUFS
);
2872 if (n
->m_len
< sizeof(struct sadb_msg
)) {
2873 n
= m_pullup(n
, sizeof(struct sadb_msg
));
2875 return key_senderror(so
, m
, ENOBUFS
);
2877 newmsg
= mtod(n
, struct sadb_msg
*);
2878 newmsg
->sadb_msg_errno
= 0;
2879 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2882 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2890 const struct sadb_msghdr
*mhp
)
2893 struct secpolicy
*sp
;
2895 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2898 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2899 panic("key_spddisable: NULL pointer is passed.\n");
2901 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2902 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2903 ipseclog((LOG_DEBUG
, "key_spddisable: invalid message is passed.\n"));
2904 key_senderror(so
, m
, EINVAL
);
2908 id
= ((struct sadb_x_policy
*)
2909 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2911 /* Is there SP in SPD ? */
2912 lck_mtx_lock(sadb_mutex
);
2913 if ((sp
= __key_getspbyid(id
)) == NULL
) {
2914 lck_mtx_unlock(sadb_mutex
);
2915 ipseclog((LOG_DEBUG
, "key_spddisable: no SP found id:%u.\n", id
));
2916 return key_senderror(so
, m
, EINVAL
);
2920 lck_mtx_unlock(sadb_mutex
);
2924 struct sadb_msg
*newmsg
;
2925 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
};
2927 /* create new sadb_msg to reply. */
2928 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2930 return key_senderror(so
, m
, ENOBUFS
);
2932 if (n
->m_len
< sizeof(struct sadb_msg
)) {
2933 n
= m_pullup(n
, sizeof(struct sadb_msg
));
2935 return key_senderror(so
, m
, ENOBUFS
);
2937 newmsg
= mtod(n
, struct sadb_msg
*);
2938 newmsg
->sadb_msg_errno
= 0;
2939 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2942 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2947 * SADB_X_GET processing
2952 * <base, address(SD), policy>
2954 * policy(*) including direction of policy.
2956 * m will always be freed.
2962 const struct sadb_msghdr
*mhp
)
2965 struct secpolicy
*sp
;
2968 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2971 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2972 panic("key_spdget: NULL pointer is passed.\n");
2974 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2975 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2976 ipseclog((LOG_DEBUG
, "key_spdget: invalid message is passed.\n"));
2977 return key_senderror(so
, m
, EINVAL
);
2980 id
= ((struct sadb_x_policy
*)
2981 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2983 /* Is there SP in SPD ? */
2984 lck_mtx_lock(sadb_mutex
);
2985 if ((sp
= __key_getspbyid(id
)) == NULL
) {
2986 ipseclog((LOG_DEBUG
, "key_spdget: no SP found id:%u.\n", id
));
2987 lck_mtx_unlock(sadb_mutex
);
2988 return key_senderror(so
, m
, ENOENT
);
2990 lck_mtx_unlock(sadb_mutex
);
2991 n
= key_setdumpsp(sp
, SADB_X_SPDGET
, 0, mhp
->msg
->sadb_msg_pid
);
2994 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
2996 return key_senderror(so
, m
, ENOBUFS
);
3000 * SADB_X_SPDACQUIRE processing.
3001 * Acquire policy and SA(s) for a *OUTBOUND* packet.
3004 * to KMD, and expect to receive
3005 * <base> with SADB_X_SPDACQUIRE if error occurred,
3008 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
3009 * policy(*) is without policy requests.
3012 * others: error number
3016 struct secpolicy
*sp
)
3018 struct mbuf
*result
= NULL
, *m
;
3019 struct secspacq
*newspacq
;
3022 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3026 panic("key_spdacquire: NULL pointer is passed.\n");
3027 if (sp
->req
!= NULL
)
3028 panic("key_spdacquire: called but there is request.\n");
3029 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
3030 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
3032 /* get a entry to check whether sent message or not. */
3033 lck_mtx_lock(sadb_mutex
);
3034 if ((newspacq
= key_getspacq(&sp
->spidx
)) != NULL
) {
3035 if (key_blockacq_count
< newspacq
->count
) {
3036 /* reset counter and do send message. */
3037 newspacq
->count
= 0;
3039 /* increment counter and do nothing. */
3041 lck_mtx_unlock(sadb_mutex
);
3045 /* make new entry for blocking to send SADB_ACQUIRE. */
3046 if ((newspacq
= key_newspacq(&sp
->spidx
)) == NULL
) {
3047 lck_mtx_unlock(sadb_mutex
);
3050 /* add to acqtree */
3051 LIST_INSERT_HEAD(&spacqtree
, newspacq
, chain
);
3052 key_start_timehandler();
3054 lck_mtx_unlock(sadb_mutex
);
3055 /* create new sadb_msg to reply. */
3056 m
= key_setsadbmsg(SADB_X_SPDACQUIRE
, 0, 0, 0, 0, 0);
3063 result
->m_pkthdr
.len
= 0;
3064 for (m
= result
; m
; m
= m
->m_next
)
3065 result
->m_pkthdr
.len
+= m
->m_len
;
3067 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3068 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3070 return key_sendup_mbuf(NULL
, m
, KEY_SENDUP_REGISTERED
);
3079 * SADB_SPDFLUSH processing
3082 * from the user, and free all entries in secpctree.
3086 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
3088 * m will always be freed.
3094 const struct sadb_msghdr
*mhp
)
3096 struct sadb_msg
*newmsg
;
3097 struct secpolicy
*sp
;
3101 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
3102 panic("key_spdflush: NULL pointer is passed.\n");
3104 if (m
->m_len
!= PFKEY_ALIGN8(sizeof(struct sadb_msg
)))
3105 return key_senderror(so
, m
, EINVAL
);
3107 lck_mtx_lock(sadb_mutex
);
3108 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
3109 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
3110 sp
->state
= IPSEC_SPSTATE_DEAD
;
3113 lck_mtx_unlock(sadb_mutex
);
3115 if (sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
3116 ipseclog((LOG_DEBUG
, "key_spdflush: No more memory.\n"));
3117 return key_senderror(so
, m
, ENOBUFS
);
3123 m
->m_pkthdr
.len
= m
->m_len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
3124 newmsg
= mtod(m
, struct sadb_msg
*);
3125 newmsg
->sadb_msg_errno
= 0;
3126 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
3128 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
3132 * SADB_SPDDUMP processing
3135 * from the user, and dump all SP leaves
3140 * m will always be freed.
3147 const struct sadb_msghdr
*mhp
)
3149 struct secpolicy
*sp
, **spbuf
= NULL
, **sp_ptr
;
3150 int cnt
= 0, bufcount
;
3156 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
3157 panic("key_spddump: NULL pointer is passed.\n");
3159 if ((bufcount
= ipsec_policy_count
) == 0) {
3163 bufcount
+= 256; /* extra */
3164 KMALLOC_WAIT(spbuf
, struct secpolicy
**, bufcount
* sizeof(struct secpolicy
*));
3165 if (spbuf
== NULL
) {
3166 ipseclog((LOG_DEBUG
, "key_spddump: No more memory.\n"));
3170 lck_mtx_lock(sadb_mutex
);
3171 /* search SPD entry, make list. */
3173 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
3174 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
3175 if (cnt
== bufcount
)
3176 break; /* buffer full */
3182 lck_mtx_unlock(sadb_mutex
);
3192 n
= key_setdumpsp(*sp_ptr
++, SADB_X_SPDDUMP
, cnt
,
3193 mhp
->msg
->sadb_msg_pid
);
3196 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
3199 lck_mtx_lock(sadb_mutex
);
3200 while (sp_ptr
> spbuf
)
3201 key_freesp(*(--sp_ptr
), KEY_SADB_LOCKED
);
3202 lck_mtx_unlock(sadb_mutex
);
3208 return key_senderror(so
, m
, error
);
3215 static struct mbuf
*
3217 struct secpolicy
*sp
,
3222 struct mbuf
*result
= NULL
, *m
;
3224 m
= key_setsadbmsg(type
, 0, SADB_SATYPE_UNSPEC
, seq
, pid
, sp
->refcnt
);
3229 if (sp
->spidx
.src_range
.start
.ss_len
> 0) {
3230 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_START
,
3231 (struct sockaddr
*)&sp
->spidx
.src_range
.start
, sp
->spidx
.prefs
,
3232 sp
->spidx
.ul_proto
);
3237 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_END
,
3238 (struct sockaddr
*)&sp
->spidx
.src_range
.end
, sp
->spidx
.prefs
,
3239 sp
->spidx
.ul_proto
);
3244 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
3245 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
3246 sp
->spidx
.ul_proto
);
3252 if (sp
->spidx
.dst_range
.start
.ss_len
> 0) {
3253 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_START
,
3254 (struct sockaddr
*)&sp
->spidx
.dst_range
.start
, sp
->spidx
.prefd
,
3255 sp
->spidx
.ul_proto
);
3260 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_END
,
3261 (struct sockaddr
*)&sp
->spidx
.dst_range
.end
, sp
->spidx
.prefd
,
3262 sp
->spidx
.ul_proto
);
3267 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
3268 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
3269 sp
->spidx
.ul_proto
);
3275 if (sp
->spidx
.internal_if
|| sp
->outgoing_if
|| sp
->ipsec_if
|| sp
->disabled
) {
3276 m
= key_setsadbipsecif(sp
->spidx
.internal_if
, sp
->outgoing_if
, sp
->ipsec_if
, sp
->disabled
);
3287 if ((result
->m_flags
& M_PKTHDR
) == 0)
3290 if (result
->m_len
< sizeof(struct sadb_msg
)) {
3291 result
= m_pullup(result
, sizeof(struct sadb_msg
));
3296 result
->m_pkthdr
.len
= 0;
3297 for (m
= result
; m
; m
= m
->m_next
)
3298 result
->m_pkthdr
.len
+= m
->m_len
;
3300 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3301 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3311 * get PFKEY message length for security policy and request.
3315 struct secpolicy
*sp
)
3319 tlen
= sizeof(struct sadb_x_policy
);
3321 /* if is the policy for ipsec ? */
3322 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
3325 /* get length of ipsec requests */
3327 struct ipsecrequest
*isr
;
3330 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
3331 len
= sizeof(struct sadb_x_ipsecrequest
)
3332 + isr
->saidx
.src
.ss_len
3333 + isr
->saidx
.dst
.ss_len
;
3335 tlen
+= PFKEY_ALIGN8(len
);
3343 * SADB_SPDEXPIRE processing
3345 * <base, address(SD), lifetime(CH), policy>
3349 * others : error number
3353 struct secpolicy
*sp
)
3355 struct mbuf
*result
= NULL
, *m
;
3358 struct sadb_lifetime
*lt
;
3360 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3364 panic("key_spdexpire: NULL pointer is passed.\n");
3366 /* set msg header */
3367 m
= key_setsadbmsg(SADB_X_SPDEXPIRE
, 0, 0, 0, 0, 0);
3374 /* create lifetime extension (current and hard) */
3375 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
3376 m
= key_alloc_mbuf(len
);
3377 if (!m
|| m
->m_next
) { /*XXX*/
3383 bzero(mtod(m
, caddr_t
), len
);
3384 lt
= mtod(m
, struct sadb_lifetime
*);
3385 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3386 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
3387 lt
->sadb_lifetime_allocations
= 0;
3388 lt
->sadb_lifetime_bytes
= 0;
3389 lt
->sadb_lifetime_addtime
= sp
->created
;
3390 lt
->sadb_lifetime_usetime
= sp
->lastused
;
3391 lt
= (struct sadb_lifetime
*)(void *)(mtod(m
, caddr_t
) + len
/ 2);
3392 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3393 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
3394 lt
->sadb_lifetime_allocations
= 0;
3395 lt
->sadb_lifetime_bytes
= 0;
3396 lt
->sadb_lifetime_addtime
= sp
->lifetime
;
3397 lt
->sadb_lifetime_usetime
= sp
->validtime
;
3400 /* set sadb_address(es) for source */
3401 if (sp
->spidx
.src_range
.start
.ss_len
> 0) {
3402 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_START
,
3403 (struct sockaddr
*)&sp
->spidx
.src_range
.start
, sp
->spidx
.prefs
,
3404 sp
->spidx
.ul_proto
);
3411 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_END
,
3412 (struct sockaddr
*)&sp
->spidx
.src_range
.end
, sp
->spidx
.prefs
,
3413 sp
->spidx
.ul_proto
);
3420 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
3421 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
3422 sp
->spidx
.ul_proto
);
3430 /* set sadb_address(es) for dest */
3431 if (sp
->spidx
.dst_range
.start
.ss_len
> 0) {
3432 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_START
,
3433 (struct sockaddr
*)&sp
->spidx
.dst_range
.start
, sp
->spidx
.prefd
,
3434 sp
->spidx
.ul_proto
);
3441 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_END
,
3442 (struct sockaddr
*)&sp
->spidx
.dst_range
.end
, sp
->spidx
.prefd
,
3443 sp
->spidx
.ul_proto
);
3450 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
3451 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
3452 sp
->spidx
.ul_proto
);
3468 if ((result
->m_flags
& M_PKTHDR
) == 0) {
3473 if (result
->m_len
< sizeof(struct sadb_msg
)) {
3474 result
= m_pullup(result
, sizeof(struct sadb_msg
));
3475 if (result
== NULL
) {
3481 result
->m_pkthdr
.len
= 0;
3482 for (m
= result
; m
; m
= m
->m_next
)
3483 result
->m_pkthdr
.len
+= m
->m_len
;
3485 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3486 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3488 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
3496 /* %%% SAD management */
3498 * allocating a memory for new SA head, and copy from the values of mhp.
3499 * OUT: NULL : failure due to the lack of memory.
3500 * others : pointer to new SA head.
3502 static struct secashead
*
3503 key_newsah(struct secasindex
*saidx
,
3508 struct secashead
*newsah
;
3512 panic("key_newsaidx: NULL pointer is passed.\n");
3514 newsah
= keydb_newsecashead();
3518 bcopy(saidx
, &newsah
->saidx
, sizeof(newsah
->saidx
));
3520 /* remove the ports */
3521 switch (saidx
->src
.ss_family
) {
3523 ((struct sockaddr_in
*)(&newsah
->saidx
.src
))->sin_port
= IPSEC_PORT_ANY
;
3526 ((struct sockaddr_in6
*)(&newsah
->saidx
.src
))->sin6_port
= IPSEC_PORT_ANY
;
3531 switch (saidx
->dst
.ss_family
) {
3533 ((struct sockaddr_in
*)(&newsah
->saidx
.dst
))->sin_port
= IPSEC_PORT_ANY
;
3536 ((struct sockaddr_in6
*)(&newsah
->saidx
.dst
))->sin6_port
= IPSEC_PORT_ANY
;
3542 newsah
->outgoing_if
= outgoing_if
;
3544 ifnet_reference(ipsec_if
);
3545 newsah
->ipsec_if
= ipsec_if
;
3548 /* add to saidxtree */
3549 newsah
->state
= SADB_SASTATE_MATURE
;
3550 LIST_INSERT_HEAD(&sahtree
, newsah
, chain
);
3551 key_start_timehandler();
3557 * delete SA index and all SA registerd.
3561 struct secashead
*sah
)
3563 struct secasvar
*sav
, *nextsav
;
3564 u_int stateidx
, state
;
3567 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3571 panic("key_delsah: NULL pointer is passed.\n");
3573 /* searching all SA registerd in the secindex. */
3575 stateidx
< _ARRAYLEN(saorder_state_any
);
3578 state
= saorder_state_any
[stateidx
];
3579 for (sav
= (struct secasvar
*)LIST_FIRST(&sah
->savtree
[state
]);
3583 nextsav
= LIST_NEXT(sav
, chain
);
3585 if (sav
->refcnt
> 0) {
3586 /* give up to delete this sa */
3592 KEY_CHKSASTATE(state
, sav
->state
, "key_delsah");
3594 key_freesav(sav
, KEY_SADB_LOCKED
);
3596 /* remove back pointer */
3602 /* don't delete sah only if there are savs. */
3606 ROUTE_RELEASE(&sah
->sa_route
);
3608 if (sah
->ipsec_if
) {
3609 ifnet_release(sah
->ipsec_if
);
3610 sah
->ipsec_if
= NULL
;
3621 /* remove from tree of SA index */
3622 if (__LIST_CHAINED(sah
))
3623 LIST_REMOVE(sah
, chain
);
3631 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
3632 * and copy the values of mhp into new buffer.
3633 * When SAD message type is GETSPI:
3634 * to set sequence number from acq_seq++,
3635 * to set zero to SPI.
3636 * not to call key_setsava().
3638 * others : pointer to new secasvar.
3640 * does not modify mbuf. does not free mbuf on error.
3642 static struct secasvar
*
3645 const struct sadb_msghdr
*mhp
,
3646 struct secashead
*sah
,
3650 struct secasvar
*newsav
;
3651 const struct sadb_sa
*xsa
;
3653 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3656 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
|| sah
== NULL
)
3657 panic("key_newsa: NULL pointer is passed.\n");
3659 KMALLOC_NOWAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3660 if (newsav
== NULL
) {
3661 lck_mtx_unlock(sadb_mutex
);
3662 KMALLOC_WAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3663 lck_mtx_lock(sadb_mutex
);
3664 if (newsav
== NULL
) {
3665 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3670 bzero((caddr_t
)newsav
, sizeof(struct secasvar
));
3672 switch (mhp
->msg
->sadb_msg_type
) {
3674 key_setspi(newsav
, 0);
3676 #if IPSEC_DOSEQCHECK
3677 /* sync sequence number */
3678 if (mhp
->msg
->sadb_msg_seq
== 0)
3680 (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
3683 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
3688 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
3690 ipseclog((LOG_DEBUG
, "key_newsa: invalid message is passed.\n"));
3694 xsa
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
3695 key_setspi(newsav
, xsa
->sadb_sa_spi
);
3696 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
3704 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
3705 if (((struct sadb_x_sa2
*)(void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_alwaysexpire
)
3706 newsav
->always_expire
= 1;
3707 newsav
->flags2
= ((struct sadb_x_sa2
*)(void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_flags
;
3708 if (newsav
->flags2
& SADB_X_EXT_SA2_DELETE_ON_DETACH
) {
3713 /* copy sav values */
3714 if (mhp
->msg
->sadb_msg_type
!= SADB_GETSPI
) {
3715 *errp
= key_setsaval(newsav
, m
, mhp
);
3721 /* For get SPI, if has a hard lifetime, apply */
3722 const struct sadb_lifetime
*lft0
;
3725 lft0
= (struct sadb_lifetime
*)(void *)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
3727 /* make lifetime for CURRENT */
3728 KMALLOC_NOWAIT(newsav
->lft_c
, struct sadb_lifetime
*,
3729 sizeof(struct sadb_lifetime
));
3730 if (newsav
->lft_c
== NULL
) {
3731 lck_mtx_unlock(sadb_mutex
);
3732 KMALLOC_WAIT(newsav
->lft_c
, struct sadb_lifetime
*,
3733 sizeof(struct sadb_lifetime
));
3734 lck_mtx_lock(sadb_mutex
);
3735 if (newsav
->lft_c
== NULL
) {
3736 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3745 newsav
->lft_c
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3746 newsav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
3747 newsav
->lft_c
->sadb_lifetime_allocations
= 0;
3748 newsav
->lft_c
->sadb_lifetime_bytes
= 0;
3749 newsav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
3750 newsav
->lft_c
->sadb_lifetime_usetime
= 0;
3752 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
] < sizeof(*lft0
)) {
3753 ipseclog((LOG_DEBUG
, "key_newsa: invalid hard lifetime ext len.\n"));
3758 newsav
->lft_h
= (struct sadb_lifetime
*)key_newbuf(lft0
, sizeof(*lft0
));
3759 if (newsav
->lft_h
== NULL
) {
3760 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3772 newsav
->created
= tv
.tv_sec
;
3775 newsav
->pid
= mhp
->msg
->sadb_msg_pid
;
3780 newsav
->state
= SADB_SASTATE_LARVAL
;
3781 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
3789 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
3790 * and copy the values passed into new buffer.
3791 * When SAD message type is GETSPI:
3792 * to set sequence number from acq_seq++,
3793 * to set zero to SPI.
3794 * not to call key_setsava().
3796 * others : pointer to new secasvar.
3799 key_newsav2(struct secashead
*sah
,
3805 struct sadb_key
*key_auth
,
3806 u_int16_t key_auth_len
,
3807 struct sadb_key
*key_enc
,
3808 u_int16_t key_enc_len
,
3809 u_int16_t natt_port
,
3813 struct sadb_lifetime
*lifetime_hard
,
3814 struct sadb_lifetime
*lifetime_soft
)
3816 struct secasvar
*newsav
;
3818 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3822 panic("key_newsa: NULL pointer is passed.\n");
3824 KMALLOC_NOWAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3825 if (newsav
== NULL
) {
3826 lck_mtx_unlock(sadb_mutex
);
3827 KMALLOC_WAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3828 lck_mtx_lock(sadb_mutex
);
3829 if (newsav
== NULL
) {
3830 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3834 bzero((caddr_t
)newsav
, sizeof(struct secasvar
));
3836 #if IPSEC_DOSEQCHECK
3837 /* sync sequence number */
3839 newsav
->seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
3843 key_setspi(newsav
, spi
);
3845 if (key_setsaval2(newsav
,
3869 newsav
->created
= tv
.tv_sec
;
3877 if (spi
&& key_auth
&& key_auth_len
&& key_enc
&& key_enc_len
) {
3878 newsav
->state
= SADB_SASTATE_MATURE
;
3879 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_MATURE
], newsav
,
3882 newsav
->state
= SADB_SASTATE_LARVAL
;
3883 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
3892 key_migratesav(struct secasvar
*sav
,
3893 struct secashead
*newsah
)
3895 if (sav
== NULL
|| newsah
== NULL
|| sav
->state
!= SADB_SASTATE_MATURE
) {
3899 /* remove from SA header */
3900 if (__LIST_CHAINED(sav
))
3901 LIST_REMOVE(sav
, chain
);
3904 LIST_INSERT_TAIL(&newsah
->savtree
[SADB_SASTATE_MATURE
], sav
, secasvar
, chain
);
3909 * free() SA variable entry.
3913 struct secasvar
*sav
)
3916 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3920 panic("key_delsav: NULL pointer is passed.\n");
3922 if (sav
->refcnt
> 0)
3923 return; /* can't free */
3925 /* remove from SA header */
3926 if (__LIST_CHAINED(sav
))
3927 LIST_REMOVE(sav
, chain
);
3930 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
)
3931 LIST_REMOVE(sav
, spihash
);
3933 if (sav
->key_auth
!= NULL
) {
3934 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
3935 KFREE(sav
->key_auth
);
3936 sav
->key_auth
= NULL
;
3938 if (sav
->key_enc
!= NULL
) {
3939 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
3940 KFREE(sav
->key_enc
);
3941 sav
->key_enc
= NULL
;
3944 bzero(sav
->sched
, sav
->schedlen
);
3948 if (sav
->replay
!= NULL
) {
3949 keydb_delsecreplay(sav
->replay
);
3952 if (sav
->lft_c
!= NULL
) {
3956 if (sav
->lft_h
!= NULL
) {
3960 if (sav
->lft_s
!= NULL
) {
3964 if (sav
->iv
!= NULL
) {
3978 * others : found, pointer to a SA.
3980 static struct secashead
*
3981 key_getsah(struct secasindex
*saidx
)
3983 struct secashead
*sah
;
3985 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3987 LIST_FOREACH(sah
, &sahtree
, chain
) {
3988 if (sah
->state
== SADB_SASTATE_DEAD
)
3990 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_REQID
))
3998 key_newsah2 (struct secasindex
*saidx
,
4001 struct secashead
*sah
;
4003 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4005 sah
= key_getsah(saidx
);
4007 return(key_newsah(saidx
, NULL
, 0, dir
));
4013 * check not to be duplicated SPI.
4014 * NOTE: this function is too slow due to searching all SAD.
4017 * others : found, pointer to a SA.
4019 static struct secasvar
*
4021 struct secasindex
*saidx
,
4024 struct secasvar
*sav
;
4025 u_int stateidx
, state
;
4027 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4029 /* check address family */
4030 if (saidx
->src
.ss_family
!= saidx
->dst
.ss_family
) {
4031 ipseclog((LOG_DEBUG
, "key_checkspidup: address family mismatched.\n"));
4036 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
4037 if (sav
->spi
!= spi
)
4040 stateidx
< _ARRAYLEN(saorder_state_alive
);
4042 state
= saorder_state_alive
[stateidx
];
4043 if (sav
->state
== state
&&
4044 key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
))
4054 struct secasvar
*sav
,
4057 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4059 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
)
4060 LIST_REMOVE(sav
, spihash
);
4061 LIST_INSERT_HEAD(&spihash
[SPIHASH(spi
)], sav
, spihash
);
4066 * search SAD litmited alive SA, protocol, SPI.
4069 * others : found, pointer to a SA.
4071 static struct secasvar
*
4073 struct secashead
*sah
,
4076 struct secasvar
*sav
, *match
;
4077 u_int stateidx
, state
, matchidx
;
4079 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4081 matchidx
= _ARRAYLEN(saorder_state_alive
);
4082 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
4083 if (sav
->spi
!= spi
)
4085 if (sav
->sah
!= sah
)
4087 for (stateidx
= 0; stateidx
< matchidx
; stateidx
++) {
4088 state
= saorder_state_alive
[stateidx
];
4089 if (sav
->state
== state
) {
4091 matchidx
= stateidx
;
4101 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
4102 * You must update these if need.
4106 * does not modify mbuf. does not free mbuf on error.
4110 struct secasvar
*sav
,
4112 const struct sadb_msghdr
*mhp
)
4115 const struct esp_algorithm
*algo
;
4120 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4123 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
4124 panic("key_setsaval: NULL pointer is passed.\n");
4126 /* initialization */
4128 sav
->key_auth
= NULL
;
4129 sav
->key_enc
= NULL
;
4136 sav
->remote_ike_port
= 0;
4137 sav
->natt_last_activity
= natt_now
;
4138 sav
->natt_encapsulated_src_port
= 0;
4141 if (mhp
->ext
[SADB_EXT_SA
] != NULL
) {
4142 const struct sadb_sa
*sa0
;
4144 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
4145 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(*sa0
)) {
4146 ipseclog((LOG_DEBUG
, "key_setsaval: invalid message size.\n"));
4151 sav
->alg_auth
= sa0
->sadb_sa_auth
;
4152 sav
->alg_enc
= sa0
->sadb_sa_encrypt
;
4153 sav
->flags
= sa0
->sadb_sa_flags
;
4156 * Verify that a nat-traversal port was specified if
4157 * the nat-traversal flag is set.
4159 if ((sav
->flags
& SADB_X_EXT_NATT
) != 0) {
4160 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa_2
) ||
4161 ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
== 0) {
4162 ipseclog((LOG_DEBUG
, "key_setsaval: natt port not set.\n"));
4166 sav
->remote_ike_port
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
;
4167 sav
->natt_interval
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_interval
;
4168 sav
->natt_offload_interval
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_offload_interval
;
4172 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
4173 * SADB_X_EXT_NATT is set and SADB_X_EXT_NATT_KEEPALIVE is not
4174 * set (we're not behind nat) - otherwise clear it.
4176 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
4177 if ((sav
->flags
& SADB_X_EXT_NATT
) == 0 ||
4178 (sav
->flags
& SADB_X_EXT_NATT_KEEPALIVE
) != 0)
4179 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
4182 if ((sa0
->sadb_sa_flags
& SADB_X_EXT_OLD
) == 0) {
4183 sav
->replay
= keydb_newsecreplay(sa0
->sadb_sa_replay
);
4184 if (sav
->replay
== NULL
) {
4185 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4192 /* Authentication keys */
4193 if (mhp
->ext
[SADB_EXT_KEY_AUTH
] != NULL
) {
4194 const struct sadb_key
*key0
;
4197 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_AUTH
];
4198 len
= mhp
->extlen
[SADB_EXT_KEY_AUTH
];
4201 if (len
< sizeof(*key0
)) {
4202 ipseclog((LOG_DEBUG
, "key_setsaval: invalid auth key ext len. len = %d\n", len
));
4206 switch (mhp
->msg
->sadb_msg_satype
) {
4207 case SADB_SATYPE_AH
:
4208 case SADB_SATYPE_ESP
:
4209 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
4210 sav
->alg_auth
!= SADB_X_AALG_NULL
)
4213 case SADB_X_SATYPE_IPCOMP
:
4219 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_auth values.\n"));
4223 sav
->key_auth
= (struct sadb_key
*)key_newbuf(key0
, len
);
4224 if (sav
->key_auth
== NULL
) {
4225 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4231 /* Encryption key */
4232 if (mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] != NULL
) {
4233 const struct sadb_key
*key0
;
4236 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_ENCRYPT
];
4237 len
= mhp
->extlen
[SADB_EXT_KEY_ENCRYPT
];
4240 if (len
< sizeof(*key0
)) {
4241 ipseclog((LOG_DEBUG
, "key_setsaval: invalid encryption key ext len. len = %d\n", len
));
4245 switch (mhp
->msg
->sadb_msg_satype
) {
4246 case SADB_SATYPE_ESP
:
4247 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
4248 sav
->alg_enc
!= SADB_EALG_NULL
) {
4249 ipseclog((LOG_DEBUG
, "key_setsaval: invalid ESP algorithm.\n"));
4253 sav
->key_enc
= (struct sadb_key
*)key_newbuf(key0
, len
);
4254 if (sav
->key_enc
== NULL
) {
4255 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4260 case SADB_X_SATYPE_IPCOMP
:
4261 if (len
!= PFKEY_ALIGN8(sizeof(struct sadb_key
)))
4263 sav
->key_enc
= NULL
; /*just in case*/
4265 case SADB_SATYPE_AH
:
4271 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_enc value.\n"));
4279 switch (mhp
->msg
->sadb_msg_satype
) {
4280 case SADB_SATYPE_ESP
:
4282 algo
= esp_algorithm_lookup(sav
->alg_enc
);
4283 if (algo
&& algo
->ivlen
)
4284 sav
->ivlen
= (*algo
->ivlen
)(algo
, sav
);
4285 if (sav
->ivlen
== 0)
4287 KMALLOC_NOWAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4289 lck_mtx_unlock(sadb_mutex
);
4290 KMALLOC_WAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4291 lck_mtx_lock(sadb_mutex
);
4293 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4300 if (sav
->alg_enc
== SADB_X_EALG_AES_GCM
) {
4301 bzero(sav
->iv
, sav
->ivlen
);
4303 key_randomfill(sav
->iv
, sav
->ivlen
);
4307 case SADB_SATYPE_AH
:
4308 case SADB_X_SATYPE_IPCOMP
:
4311 ipseclog((LOG_DEBUG
, "key_setsaval: invalid SA type.\n"));
4318 sav
->created
= tv
.tv_sec
;
4320 /* make lifetime for CURRENT */
4321 KMALLOC_NOWAIT(sav
->lft_c
, struct sadb_lifetime
*,
4322 sizeof(struct sadb_lifetime
));
4323 if (sav
->lft_c
== NULL
) {
4324 lck_mtx_unlock(sadb_mutex
);
4325 KMALLOC_WAIT(sav
->lft_c
, struct sadb_lifetime
*,
4326 sizeof(struct sadb_lifetime
));
4327 lck_mtx_lock(sadb_mutex
);
4328 if (sav
->lft_c
== NULL
) {
4329 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4337 sav
->lft_c
->sadb_lifetime_len
=
4338 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
4339 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
4340 sav
->lft_c
->sadb_lifetime_allocations
= 0;
4341 sav
->lft_c
->sadb_lifetime_bytes
= 0;
4342 sav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
4343 sav
->lft_c
->sadb_lifetime_usetime
= 0;
4345 /* lifetimes for HARD and SOFT */
4347 const struct sadb_lifetime
*lft0
;
4349 lft0
= (struct sadb_lifetime
*)
4350 (void *)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
4352 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
] < sizeof(*lft0
)) {
4353 ipseclog((LOG_DEBUG
, "key_setsaval: invalid hard lifetime ext len.\n"));
4357 sav
->lft_h
= (struct sadb_lifetime
*)key_newbuf(lft0
,
4359 if (sav
->lft_h
== NULL
) {
4360 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4364 /* to be initialize ? */
4367 lft0
= (struct sadb_lifetime
*)
4368 (void *)mhp
->ext
[SADB_EXT_LIFETIME_SOFT
];
4370 if (mhp
->extlen
[SADB_EXT_LIFETIME_SOFT
] < sizeof(*lft0
)) {
4371 ipseclog((LOG_DEBUG
, "key_setsaval: invalid soft lifetime ext len.\n"));
4375 sav
->lft_s
= (struct sadb_lifetime
*)key_newbuf(lft0
,
4377 if (sav
->lft_s
== NULL
) {
4378 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4382 /* to be initialize ? */
4389 /* initialization */
4390 if (sav
->replay
!= NULL
) {
4391 keydb_delsecreplay(sav
->replay
);
4394 if (sav
->key_auth
!= NULL
) {
4395 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
4396 KFREE(sav
->key_auth
);
4397 sav
->key_auth
= NULL
;
4399 if (sav
->key_enc
!= NULL
) {
4400 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
4401 KFREE(sav
->key_enc
);
4402 sav
->key_enc
= NULL
;
4405 bzero(sav
->sched
, sav
->schedlen
);
4409 if (sav
->iv
!= NULL
) {
4413 if (sav
->lft_c
!= NULL
) {
4417 if (sav
->lft_h
!= NULL
) {
4421 if (sav
->lft_s
!= NULL
) {
4430 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
4431 * You must update these if need.
4435 * does not modify mbuf. does not free mbuf on error.
4438 key_setsaval2(struct secasvar
*sav
,
4444 struct sadb_key
*key_auth
,
4445 u_int16_t key_auth_len
,
4446 struct sadb_key
*key_enc
,
4447 u_int16_t key_enc_len
,
4448 u_int16_t natt_port
,
4452 struct sadb_lifetime
*lifetime_hard
,
4453 struct sadb_lifetime
*lifetime_soft
)
4456 const struct esp_algorithm
*algo
;
4461 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4463 /* initialization */
4465 sav
->key_auth
= NULL
;
4466 sav
->key_enc
= NULL
;
4473 sav
->remote_ike_port
= 0;
4474 sav
->natt_last_activity
= natt_now
;
4475 sav
->natt_encapsulated_src_port
= 0;
4477 sav
->alg_auth
= alg_auth
;
4478 sav
->alg_enc
= alg_enc
;
4482 key_setspi(sav
, htonl(spi
));
4485 * Verify that a nat-traversal port was specified if
4486 * the nat-traversal flag is set.
4488 if ((sav
->flags
& SADB_X_EXT_NATT
) != 0) {
4489 if (natt_port
== 0) {
4490 ipseclog((LOG_DEBUG
, "key_setsaval2: natt port not set.\n"));
4494 sav
->remote_ike_port
= natt_port
;
4498 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
4499 * SADB_X_EXT_NATT is set and SADB_X_EXT_NATT_KEEPALIVE is not
4500 * set (we're not behind nat) - otherwise clear it.
4502 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
4503 if ((sav
->flags
& SADB_X_EXT_NATT
) == 0 ||
4504 (sav
->flags
& SADB_X_EXT_NATT_KEEPALIVE
) != 0)
4505 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
4508 if ((flags
& SADB_X_EXT_OLD
) == 0) {
4509 sav
->replay
= keydb_newsecreplay(replay
);
4510 if (sav
->replay
== NULL
) {
4511 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4517 /* Authentication keys */
4518 sav
->key_auth
= (__typeof__(sav
->key_auth
))key_newbuf(key_auth
, key_auth_len
);
4519 if (sav
->key_auth
== NULL
) {
4520 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4525 /* Encryption key */
4526 sav
->key_enc
= (__typeof__(sav
->key_enc
))key_newbuf(key_enc
, key_enc_len
);
4527 if (sav
->key_enc
== NULL
) {
4528 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4536 if (satype
== SADB_SATYPE_ESP
) {
4538 algo
= esp_algorithm_lookup(sav
->alg_enc
);
4539 if (algo
&& algo
->ivlen
)
4540 sav
->ivlen
= (*algo
->ivlen
)(algo
, sav
);
4541 if (sav
->ivlen
!= 0) {
4542 KMALLOC_NOWAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4544 lck_mtx_unlock(sadb_mutex
);
4545 KMALLOC_WAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4546 lck_mtx_lock(sadb_mutex
);
4548 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4554 if (sav
->alg_enc
== SADB_X_EALG_AES_GCM
) {
4555 bzero(sav
->iv
, sav
->ivlen
);
4557 key_randomfill(sav
->iv
, sav
->ivlen
);
4565 sav
->created
= tv
.tv_sec
;
4567 /* make lifetime for CURRENT */
4568 KMALLOC_NOWAIT(sav
->lft_c
, struct sadb_lifetime
*,
4569 sizeof(struct sadb_lifetime
));
4570 if (sav
->lft_c
== NULL
) {
4571 lck_mtx_unlock(sadb_mutex
);
4572 KMALLOC_WAIT(sav
->lft_c
, struct sadb_lifetime
*,
4573 sizeof(struct sadb_lifetime
));
4574 lck_mtx_lock(sadb_mutex
);
4575 if (sav
->lft_c
== NULL
) {
4576 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4584 sav
->lft_c
->sadb_lifetime_len
=
4585 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
4586 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
4587 sav
->lft_c
->sadb_lifetime_allocations
= 0;
4588 sav
->lft_c
->sadb_lifetime_bytes
= 0;
4589 sav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
4590 sav
->lft_c
->sadb_lifetime_usetime
= 0;
4592 /* lifetimes for HARD and SOFT */
4593 sav
->lft_h
= (__typeof__(sav
->lft_h
))key_newbuf(lifetime_hard
,
4594 sizeof(*lifetime_hard
));
4595 if (sav
->lft_h
== NULL
) {
4596 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4600 sav
->lft_s
= (__typeof__(sav
->lft_s
))key_newbuf(lifetime_soft
,
4601 sizeof(*lifetime_soft
));
4602 if (sav
->lft_s
== NULL
) {
4603 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4611 /* initialization */
4612 if (sav
->replay
!= NULL
) {
4613 keydb_delsecreplay(sav
->replay
);
4616 if (sav
->key_auth
!= NULL
) {
4617 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
4618 KFREE(sav
->key_auth
);
4619 sav
->key_auth
= NULL
;
4621 if (sav
->key_enc
!= NULL
) {
4622 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
4623 KFREE(sav
->key_enc
);
4624 sav
->key_enc
= NULL
;
4627 bzero(sav
->sched
, sav
->schedlen
);
4631 if (sav
->iv
!= NULL
) {
4635 if (sav
->lft_c
!= NULL
) {
4639 if (sav
->lft_h
!= NULL
) {
4643 if (sav
->lft_s
!= NULL
) {
4652 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
4658 struct secasvar
*sav
)
4661 int checkmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
4662 int mustmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
4666 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4668 /* check SPI value */
4669 switch (sav
->sah
->saidx
.proto
) {
4673 /* No reason to test if this is >= 0, because ntohl(sav->spi) is unsigned. */
4674 if (ntohl(sav
->spi
) <= 255) {
4675 ipseclog((LOG_DEBUG
,
4676 "key_mature: illegal range of SPI %u.\n",
4677 (u_int32_t
)ntohl(sav
->spi
)));
4684 switch (sav
->sah
->saidx
.proto
) {
4687 if ((sav
->flags
& SADB_X_EXT_OLD
)
4688 && (sav
->flags
& SADB_X_EXT_DERIV
)) {
4689 ipseclog((LOG_DEBUG
, "key_mature: "
4690 "invalid flag (derived) given to old-esp.\n"));
4693 if (sav
->alg_auth
== SADB_AALG_NONE
)
4701 if (sav
->flags
& SADB_X_EXT_DERIV
) {
4702 ipseclog((LOG_DEBUG
, "key_mature: "
4703 "invalid flag (derived) given to AH SA.\n"));
4706 if (sav
->alg_enc
!= SADB_EALG_NONE
) {
4707 ipseclog((LOG_DEBUG
, "key_mature: "
4708 "protocol and algorithm mismated.\n"));
4714 case IPPROTO_IPCOMP
:
4715 if (sav
->alg_auth
!= SADB_AALG_NONE
) {
4716 ipseclog((LOG_DEBUG
, "key_mature: "
4717 "protocol and algorithm mismated.\n"));
4720 if ((sav
->flags
& SADB_X_EXT_RAWCPI
) == 0
4721 && ntohl(sav
->spi
) >= 0x10000) {
4722 ipseclog((LOG_DEBUG
, "key_mature: invalid cpi for IPComp.\n"));
4729 ipseclog((LOG_DEBUG
, "key_mature: Invalid satype.\n"));
4730 return EPROTONOSUPPORT
;
4733 /* check authentication algorithm */
4734 if ((checkmask
& 2) != 0) {
4735 const struct ah_algorithm
*algo
;
4738 algo
= ah_algorithm_lookup(sav
->alg_auth
);
4740 ipseclog((LOG_DEBUG
,"key_mature: "
4741 "unknown authentication algorithm.\n"));
4745 /* algorithm-dependent check */
4747 keylen
= sav
->key_auth
->sadb_key_bits
;
4750 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
4751 ipseclog((LOG_DEBUG
,
4752 "key_mature: invalid AH key length %d "
4753 "(%d-%d allowed)\n",
4754 keylen
, algo
->keymin
, algo
->keymax
));
4759 if ((*algo
->mature
)(sav
)) {
4760 /* message generated in per-algorithm function*/
4763 mature
= SADB_SATYPE_AH
;
4766 if ((mustmask
& 2) != 0 && mature
!= SADB_SATYPE_AH
) {
4767 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for AH\n"));
4772 /* check encryption algorithm */
4773 if ((checkmask
& 1) != 0) {
4775 const struct esp_algorithm
*algo
;
4778 algo
= esp_algorithm_lookup(sav
->alg_enc
);
4780 ipseclog((LOG_DEBUG
, "key_mature: unknown encryption algorithm.\n"));
4784 /* algorithm-dependent check */
4786 keylen
= sav
->key_enc
->sadb_key_bits
;
4789 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
4790 ipseclog((LOG_DEBUG
,
4791 "key_mature: invalid ESP key length %d "
4792 "(%d-%d allowed)\n",
4793 keylen
, algo
->keymin
, algo
->keymax
));
4798 if ((*algo
->mature
)(sav
)) {
4799 /* message generated in per-algorithm function*/
4802 mature
= SADB_SATYPE_ESP
;
4805 if ((mustmask
& 1) != 0 && mature
!= SADB_SATYPE_ESP
) {
4806 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for ESP\n"));
4810 ipseclog((LOG_DEBUG
, "key_mature: ESP not supported in this configuration\n"));
4815 /* check compression algorithm */
4816 if ((checkmask
& 4) != 0) {
4817 const struct ipcomp_algorithm
*algo
;
4819 /* algorithm-dependent check */
4820 algo
= ipcomp_algorithm_lookup(sav
->alg_enc
);
4822 ipseclog((LOG_DEBUG
, "key_mature: unknown compression algorithm.\n"));
4827 key_sa_chgstate(sav
, SADB_SASTATE_MATURE
);
4833 * subroutine for SADB_GET and SADB_DUMP.
4835 static struct mbuf
*
4837 struct secasvar
*sav
,
4843 struct mbuf
*result
= NULL
, *tres
= NULL
, *m
;
4848 SADB_EXT_SA
, SADB_X_EXT_SA2
,
4849 SADB_EXT_LIFETIME_HARD
, SADB_EXT_LIFETIME_SOFT
,
4850 SADB_EXT_LIFETIME_CURRENT
, SADB_EXT_ADDRESS_SRC
,
4851 SADB_EXT_ADDRESS_DST
, SADB_EXT_ADDRESS_PROXY
, SADB_EXT_KEY_AUTH
,
4852 SADB_EXT_KEY_ENCRYPT
, SADB_EXT_IDENTITY_SRC
,
4853 SADB_EXT_IDENTITY_DST
, SADB_EXT_SENSITIVITY
,
4856 m
= key_setsadbmsg(type
, 0, satype
, seq
, pid
, sav
->refcnt
);
4861 for (i
= sizeof(dumporder
)/sizeof(dumporder
[0]) - 1; i
>= 0; i
--) {
4864 switch (dumporder
[i
]) {
4866 m
= key_setsadbsa(sav
);
4871 case SADB_X_EXT_SA2
:
4872 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
4873 sav
->replay
? sav
->replay
->count
: 0,
4874 sav
->sah
->saidx
.reqid
,
4880 case SADB_EXT_ADDRESS_SRC
:
4881 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
4882 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
4883 FULLMASK
, IPSEC_ULPROTO_ANY
);
4888 case SADB_EXT_ADDRESS_DST
:
4889 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
4890 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
4891 FULLMASK
, IPSEC_ULPROTO_ANY
);
4896 case SADB_EXT_KEY_AUTH
:
4899 l
= PFKEY_UNUNIT64(sav
->key_auth
->sadb_key_len
);
4903 case SADB_EXT_KEY_ENCRYPT
:
4906 l
= PFKEY_UNUNIT64(sav
->key_enc
->sadb_key_len
);
4910 case SADB_EXT_LIFETIME_CURRENT
:
4913 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_c
)->sadb_ext_len
);
4917 case SADB_EXT_LIFETIME_HARD
:
4920 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_h
)->sadb_ext_len
);
4924 case SADB_EXT_LIFETIME_SOFT
:
4927 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_s
)->sadb_ext_len
);
4931 case SADB_EXT_ADDRESS_PROXY
:
4932 case SADB_EXT_IDENTITY_SRC
:
4933 case SADB_EXT_IDENTITY_DST
:
4934 /* XXX: should we brought from SPD ? */
4935 case SADB_EXT_SENSITIVITY
:
4940 if ((!m
&& !p
) || (m
&& p
))
4943 M_PREPEND(tres
, l
, M_WAITOK
, 1);
4946 bcopy(p
, mtod(tres
, caddr_t
), l
);
4950 m
= key_alloc_mbuf(l
);
4953 m_copyback(m
, 0, l
, p
);
4961 m_cat(result
, tres
);
4963 if (sav
->sah
&& (sav
->sah
->outgoing_if
|| sav
->sah
->ipsec_if
)) {
4964 m
= key_setsadbipsecif(NULL
, ifindex2ifnet
[sav
->sah
->outgoing_if
], sav
->sah
->ipsec_if
, 0);
4970 if (result
->m_len
< sizeof(struct sadb_msg
)) {
4971 result
= m_pullup(result
, sizeof(struct sadb_msg
));
4976 result
->m_pkthdr
.len
= 0;
4977 for (m
= result
; m
; m
= m
->m_next
)
4978 result
->m_pkthdr
.len
+= m
->m_len
;
4980 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
4981 PFKEY_UNIT64(result
->m_pkthdr
.len
);
4992 * set data into sadb_msg.
4994 static struct mbuf
*
5007 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
5010 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
5011 if (m
&& len
> MHLEN
) {
5012 MCLGET(m
, M_DONTWAIT
);
5013 if ((m
->m_flags
& M_EXT
) == 0) {
5020 m
->m_pkthdr
.len
= m
->m_len
= len
;
5023 p
= mtod(m
, struct sadb_msg
*);
5026 p
->sadb_msg_version
= PF_KEY_V2
;
5027 p
->sadb_msg_type
= type
;
5028 p
->sadb_msg_errno
= 0;
5029 p
->sadb_msg_satype
= satype
;
5030 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
5031 p
->sadb_msg_reserved
= reserved
;
5032 p
->sadb_msg_seq
= seq
;
5033 p
->sadb_msg_pid
= (u_int32_t
)pid
;
5039 * copy secasvar data into sadb_address.
5041 static struct mbuf
*
5043 struct secasvar
*sav
)
5049 len
= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
5050 m
= key_alloc_mbuf(len
);
5051 if (!m
|| m
->m_next
) { /*XXX*/
5057 p
= mtod(m
, struct sadb_sa
*);
5060 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
5061 p
->sadb_sa_exttype
= SADB_EXT_SA
;
5062 p
->sadb_sa_spi
= sav
->spi
;
5063 p
->sadb_sa_replay
= (sav
->replay
!= NULL
? sav
->replay
->wsize
: 0);
5064 p
->sadb_sa_state
= sav
->state
;
5065 p
->sadb_sa_auth
= sav
->alg_auth
;
5066 p
->sadb_sa_encrypt
= sav
->alg_enc
;
5067 p
->sadb_sa_flags
= sav
->flags
;
5073 * set data into sadb_address.
5075 static struct mbuf
*
5078 struct sockaddr
*saddr
,
5083 struct sadb_address
*p
;
5086 len
= PFKEY_ALIGN8(sizeof(struct sadb_address
)) +
5087 PFKEY_ALIGN8(saddr
->sa_len
);
5088 m
= key_alloc_mbuf(len
);
5089 if (!m
|| m
->m_next
) { /*XXX*/
5095 p
= mtod(m
, struct sadb_address
*);
5098 p
->sadb_address_len
= PFKEY_UNIT64(len
);
5099 p
->sadb_address_exttype
= exttype
;
5100 p
->sadb_address_proto
= ul_proto
;
5101 if (prefixlen
== FULLMASK
) {
5102 switch (saddr
->sa_family
) {
5104 prefixlen
= sizeof(struct in_addr
) << 3;
5107 prefixlen
= sizeof(struct in6_addr
) << 3;
5113 p
->sadb_address_prefixlen
= prefixlen
;
5114 p
->sadb_address_reserved
= 0;
5117 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_address
)),
5123 static struct mbuf
*
5124 key_setsadbipsecif(ifnet_t internal_if
,
5125 ifnet_t outgoing_if
,
5130 struct sadb_x_ipsecif
*p
;
5133 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_ipsecif
));
5134 m
= key_alloc_mbuf(len
);
5135 if (!m
|| m
->m_next
) { /*XXX*/
5141 p
= mtod(m
, struct sadb_x_ipsecif
*);
5144 p
->sadb_x_ipsecif_len
= PFKEY_UNIT64(len
);
5145 p
->sadb_x_ipsecif_exttype
= SADB_X_EXT_IPSECIF
;
5147 if (internal_if
&& internal_if
->if_xname
)
5148 strlcpy(p
->sadb_x_ipsecif_internal_if
, internal_if
->if_xname
, IFXNAMSIZ
);
5149 if (outgoing_if
&& outgoing_if
->if_xname
)
5150 strlcpy(p
->sadb_x_ipsecif_outgoing_if
, outgoing_if
->if_xname
, IFXNAMSIZ
);
5151 if (ipsec_if
&& ipsec_if
->if_xname
)
5152 strlcpy(p
->sadb_x_ipsecif_ipsec_if
, ipsec_if
->if_xname
, IFXNAMSIZ
);
5154 p
->sadb_x_ipsecif_init_disabled
= init_disabled
;
5160 * set data into sadb_session_id
5162 static struct mbuf
*
5163 key_setsadbsession_id (u_int64_t session_ids
[])
5166 struct sadb_session_id
*p
;
5169 len
= PFKEY_ALIGN8(sizeof(*p
));
5170 m
= key_alloc_mbuf(len
);
5171 if (!m
|| m
->m_next
) { /*XXX*/
5177 p
= mtod(m
, __typeof__(p
));
5180 p
->sadb_session_id_len
= PFKEY_UNIT64(len
);
5181 p
->sadb_session_id_exttype
= SADB_EXT_SESSION_ID
;
5182 p
->sadb_session_id_v
[0] = session_ids
[0];
5183 p
->sadb_session_id_v
[1] = session_ids
[1];
5189 * copy stats data into sadb_sastat type.
5191 static struct mbuf
*
5192 key_setsadbsastat (u_int32_t dir
,
5193 struct sastat
*stats
,
5194 u_int32_t max_stats
)
5197 struct sadb_sastat
*p
;
5204 list_len
= sizeof(*stats
) * max_stats
;
5205 len
= PFKEY_ALIGN8(sizeof(*p
)) + PFKEY_ALIGN8(list_len
);
5206 m
= key_alloc_mbuf(len
);
5207 if (!m
|| m
->m_next
) { /*XXX*/
5213 p
= mtod(m
, __typeof__(p
));
5216 p
->sadb_sastat_len
= PFKEY_UNIT64(len
);
5217 p
->sadb_sastat_exttype
= SADB_EXT_SASTAT
;
5218 p
->sadb_sastat_dir
= dir
;
5219 p
->sadb_sastat_list_len
= max_stats
;
5222 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(*p
)),
5231 * set data into sadb_ident.
5233 static struct mbuf
*
5242 struct sadb_ident
*p
;
5245 len
= PFKEY_ALIGN8(sizeof(struct sadb_ident
)) + PFKEY_ALIGN8(stringlen
);
5246 m
= key_alloc_mbuf(len
);
5247 if (!m
|| m
->m_next
) { /*XXX*/
5253 p
= mtod(m
, struct sadb_ident
*);
5256 p
->sadb_ident_len
= PFKEY_UNIT64(len
);
5257 p
->sadb_ident_exttype
= exttype
;
5258 p
->sadb_ident_type
= idtype
;
5259 p
->sadb_ident_reserved
= 0;
5260 p
->sadb_ident_id
= id
;
5263 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_ident
)),
5271 * set data into sadb_x_sa2.
5273 static struct mbuf
*
5281 struct sadb_x_sa2
*p
;
5284 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_sa2
));
5285 m
= key_alloc_mbuf(len
);
5286 if (!m
|| m
->m_next
) { /*XXX*/
5292 p
= mtod(m
, struct sadb_x_sa2
*);
5295 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
5296 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
5297 p
->sadb_x_sa2_mode
= mode
;
5298 p
->sadb_x_sa2_reserved1
= 0;
5299 p
->sadb_x_sa2_reserved2
= 0;
5300 p
->sadb_x_sa2_sequence
= seq
;
5301 p
->sadb_x_sa2_reqid
= reqid
;
5302 p
->sadb_x_sa2_flags
= flags
;
5308 * set data into sadb_x_policy
5310 static struct mbuf
*
5317 struct sadb_x_policy
*p
;
5320 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_policy
));
5321 m
= key_alloc_mbuf(len
);
5322 if (!m
|| m
->m_next
) { /*XXX*/
5328 p
= mtod(m
, struct sadb_x_policy
*);
5331 p
->sadb_x_policy_len
= PFKEY_UNIT64(len
);
5332 p
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
5333 p
->sadb_x_policy_type
= type
;
5334 p
->sadb_x_policy_dir
= dir
;
5335 p
->sadb_x_policy_id
= id
;
5342 * copy a buffer into the new buffer allocated.
5351 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5352 KMALLOC_NOWAIT(new, caddr_t
, len
);
5354 lck_mtx_unlock(sadb_mutex
);
5355 KMALLOC_WAIT(new, caddr_t
, len
);
5356 lck_mtx_lock(sadb_mutex
);
5358 ipseclog((LOG_DEBUG
, "key_newbuf: No more memory.\n"));
5362 bcopy(src
, new, len
);
5367 /* compare my own address
5368 * OUT: 1: true, i.e. my address.
5373 struct sockaddr
*sa
)
5376 struct sockaddr_in
*sin
;
5377 struct in_ifaddr
*ia
;
5382 panic("key_ismyaddr: NULL pointer is passed.\n");
5384 switch (sa
->sa_family
) {
5387 lck_rw_lock_shared(in_ifaddr_rwlock
);
5388 sin
= (struct sockaddr_in
*)(void *)sa
;
5389 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
5390 ia
= ia
->ia_link
.tqe_next
) {
5391 IFA_LOCK_SPIN(&ia
->ia_ifa
);
5392 if (sin
->sin_family
== ia
->ia_addr
.sin_family
&&
5393 sin
->sin_len
== ia
->ia_addr
.sin_len
&&
5394 sin
->sin_addr
.s_addr
== ia
->ia_addr
.sin_addr
.s_addr
)
5396 IFA_UNLOCK(&ia
->ia_ifa
);
5397 lck_rw_done(in_ifaddr_rwlock
);
5400 IFA_UNLOCK(&ia
->ia_ifa
);
5402 lck_rw_done(in_ifaddr_rwlock
);
5407 return key_ismyaddr6((struct sockaddr_in6
*)(void *)sa
);
5416 * compare my own address for IPv6.
5419 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
5421 #include <netinet6/in6_var.h>
5425 struct sockaddr_in6
*sin6
)
5427 struct in6_ifaddr
*ia
;
5428 struct in6_multi
*in6m
;
5430 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
5431 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
5432 IFA_LOCK(&ia
->ia_ifa
);
5433 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
5434 (struct sockaddr
*)&ia
->ia_addr
, 0) == 0) {
5435 IFA_UNLOCK(&ia
->ia_ifa
);
5436 lck_rw_done(&in6_ifaddr_rwlock
);
5439 IFA_UNLOCK(&ia
->ia_ifa
);
5443 * XXX why do we care about multlicast here while we don't care
5444 * about IPv4 multicast??
5448 in6_multihead_lock_shared();
5449 IN6_LOOKUP_MULTI(&sin6
->sin6_addr
, ia
->ia_ifp
, in6m
);
5450 in6_multihead_lock_done();
5452 lck_rw_done(&in6_ifaddr_rwlock
);
5457 lck_rw_done(&in6_ifaddr_rwlock
);
5459 /* loopback, just for safety */
5460 if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
5468 * compare two secasindex structure.
5469 * flag can specify to compare 2 saidxes.
5470 * compare two secasindex structure without both mode and reqid.
5471 * don't compare port.
5473 * saidx0: source, it can be in SAD.
5481 struct secasindex
*saidx0
,
5482 struct secasindex
*saidx1
,
5486 if (saidx0
== NULL
&& saidx1
== NULL
)
5489 if (saidx0
== NULL
|| saidx1
== NULL
)
5492 if (saidx0
->ipsec_ifindex
!= 0 && saidx0
->ipsec_ifindex
!= saidx1
->ipsec_ifindex
)
5495 if (saidx0
->proto
!= saidx1
->proto
)
5498 if (flag
== CMP_EXACTLY
) {
5499 if (saidx0
->mode
!= saidx1
->mode
)
5501 if (saidx0
->reqid
!= saidx1
->reqid
)
5503 if (bcmp(&saidx0
->src
, &saidx1
->src
, saidx0
->src
.ss_len
) != 0 ||
5504 bcmp(&saidx0
->dst
, &saidx1
->dst
, saidx0
->dst
.ss_len
) != 0)
5508 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
5509 if (flag
& CMP_REQID
) {
5511 * If reqid of SPD is non-zero, unique SA is required.
5512 * The result must be of same reqid in this case.
5514 if (saidx1
->reqid
!= 0 && saidx0
->reqid
!= saidx1
->reqid
)
5518 if (flag
& CMP_MODE
) {
5519 if (saidx0
->mode
!= IPSEC_MODE_ANY
5520 && saidx0
->mode
!= saidx1
->mode
)
5524 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->src
,
5525 (struct sockaddr
*)&saidx1
->src
, flag
& CMP_PORT
? 1 : 0) != 0) {
5528 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->dst
,
5529 (struct sockaddr
*)&saidx1
->dst
, flag
& CMP_PORT
? 1 : 0) != 0) {
5538 * compare two secindex structure exactly.
5540 * spidx0: source, it is often in SPD.
5541 * spidx1: object, it is often from PFKEY message.
5547 key_cmpspidx_exactly(
5548 struct secpolicyindex
*spidx0
,
5549 struct secpolicyindex
*spidx1
)
5552 if (spidx0
== NULL
&& spidx1
== NULL
)
5555 if (spidx0
== NULL
|| spidx1
== NULL
)
5558 if (spidx0
->prefs
!= spidx1
->prefs
5559 || spidx0
->prefd
!= spidx1
->prefd
5560 || spidx0
->ul_proto
!= spidx1
->ul_proto
5561 || spidx0
->internal_if
!= spidx1
->internal_if
)
5564 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src
,
5565 (struct sockaddr
*)&spidx1
->src
, 1) != 0) {
5568 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst
,
5569 (struct sockaddr
*)&spidx1
->dst
, 1) != 0) {
5573 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src_range
.start
,
5574 (struct sockaddr
*)&spidx1
->src_range
.start
, 1) != 0) {
5577 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src_range
.end
,
5578 (struct sockaddr
*)&spidx1
->src_range
.end
, 1) != 0) {
5581 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst_range
.start
,
5582 (struct sockaddr
*)&spidx1
->dst_range
.start
, 1) != 0) {
5585 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst_range
.end
,
5586 (struct sockaddr
*)&spidx1
->dst_range
.end
, 1) != 0) {
5594 * compare two secindex structure with mask.
5596 * spidx0: source, it is often in SPD.
5597 * spidx1: object, it is often from IP header.
5603 key_cmpspidx_withmask(
5604 struct secpolicyindex
*spidx0
,
5605 struct secpolicyindex
*spidx1
)
5607 int spidx0_src_is_range
= 0;
5608 int spidx0_dst_is_range
= 0;
5611 if (spidx0
== NULL
&& spidx1
== NULL
)
5614 if (spidx0
== NULL
|| spidx1
== NULL
)
5617 if (spidx0
->src_range
.start
.ss_len
> 0)
5618 spidx0_src_is_range
= 1;
5620 if (spidx0
->dst_range
.start
.ss_len
> 0)
5621 spidx0_dst_is_range
= 1;
5623 if ((spidx0_src_is_range
? spidx0
->src_range
.start
.ss_family
: spidx0
->src
.ss_family
) != spidx1
->src
.ss_family
||
5624 (spidx0_dst_is_range
? spidx0
->dst_range
.start
.ss_family
: spidx0
->dst
.ss_family
) != spidx1
->dst
.ss_family
||
5625 (spidx0_src_is_range
? spidx0
->src_range
.start
.ss_len
: spidx0
->src
.ss_len
) != spidx1
->src
.ss_len
||
5626 (spidx0_dst_is_range
? spidx0
->dst_range
.start
.ss_len
: spidx0
->dst
.ss_len
) != spidx1
->dst
.ss_len
)
5629 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
5630 if (spidx0
->ul_proto
!= (u_int16_t
)IPSEC_ULPROTO_ANY
5631 && spidx0
->ul_proto
!= spidx1
->ul_proto
)
5634 /* If spidx1 specifies interface, ignore src addr */
5635 if (spidx1
->internal_if
!= NULL
) {
5636 if (spidx0
->internal_if
== NULL
5637 || spidx0
->internal_if
!= spidx1
->internal_if
)
5640 /* Still check ports */
5641 switch (spidx0
->src
.ss_family
) {
5643 if (spidx0_src_is_range
&&
5644 (satosin(&spidx1
->src
)->sin_port
< satosin(&spidx0
->src_range
.start
)->sin_port
5645 || satosin(&spidx1
->src
)->sin_port
> satosin(&spidx0
->src_range
.end
)->sin_port
))
5647 else if (satosin(&spidx0
->src
)->sin_port
!= IPSEC_PORT_ANY
5648 && satosin(&spidx0
->src
)->sin_port
!=
5649 satosin(&spidx1
->src
)->sin_port
)
5653 if (spidx0_src_is_range
&&
5654 (satosin6(&spidx1
->src
)->sin6_port
< satosin6(&spidx0
->src_range
.start
)->sin6_port
5655 || satosin6(&spidx1
->src
)->sin6_port
> satosin6(&spidx0
->src_range
.end
)->sin6_port
))
5657 else if (satosin6(&spidx0
->src
)->sin6_port
!= IPSEC_PORT_ANY
5658 && satosin6(&spidx0
->src
)->sin6_port
!=
5659 satosin6(&spidx1
->src
)->sin6_port
)
5665 } else if (spidx0_src_is_range
) {
5666 if (!key_is_addr_in_range(&spidx1
->src
, &spidx0
->src_range
))
5669 switch (spidx0
->src
.ss_family
) {
5671 if (satosin(&spidx0
->src
)->sin_port
!= IPSEC_PORT_ANY
5672 && satosin(&spidx0
->src
)->sin_port
!=
5673 satosin(&spidx1
->src
)->sin_port
)
5675 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->src
)->sin_addr
,
5676 (caddr_t
)&satosin(&spidx1
->src
)->sin_addr
, spidx0
->prefs
))
5680 if (satosin6(&spidx0
->src
)->sin6_port
!= IPSEC_PORT_ANY
5681 && satosin6(&spidx0
->src
)->sin6_port
!=
5682 satosin6(&spidx1
->src
)->sin6_port
)
5685 * scope_id check. if sin6_scope_id is 0, we regard it
5686 * as a wildcard scope, which matches any scope zone ID.
5688 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
5689 satosin6(&spidx1
->src
)->sin6_scope_id
&&
5690 satosin6(&spidx0
->src
)->sin6_scope_id
!=
5691 satosin6(&spidx1
->src
)->sin6_scope_id
)
5693 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->src
)->sin6_addr
,
5694 (caddr_t
)&satosin6(&spidx1
->src
)->sin6_addr
, spidx0
->prefs
))
5699 if (bcmp(&spidx0
->src
, &spidx1
->src
, spidx0
->src
.ss_len
) != 0)
5705 if (spidx0_dst_is_range
) {
5706 if (!key_is_addr_in_range(&spidx1
->dst
, &spidx0
->dst_range
))
5709 switch (spidx0
->dst
.ss_family
) {
5711 if (satosin(&spidx0
->dst
)->sin_port
!= IPSEC_PORT_ANY
5712 && satosin(&spidx0
->dst
)->sin_port
!=
5713 satosin(&spidx1
->dst
)->sin_port
)
5715 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->dst
)->sin_addr
,
5716 (caddr_t
)&satosin(&spidx1
->dst
)->sin_addr
, spidx0
->prefd
))
5720 if (satosin6(&spidx0
->dst
)->sin6_port
!= IPSEC_PORT_ANY
5721 && satosin6(&spidx0
->dst
)->sin6_port
!=
5722 satosin6(&spidx1
->dst
)->sin6_port
)
5725 * scope_id check. if sin6_scope_id is 0, we regard it
5726 * as a wildcard scope, which matches any scope zone ID.
5728 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
5729 satosin6(&spidx1
->src
)->sin6_scope_id
&&
5730 satosin6(&spidx0
->dst
)->sin6_scope_id
!=
5731 satosin6(&spidx1
->dst
)->sin6_scope_id
)
5733 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->dst
)->sin6_addr
,
5734 (caddr_t
)&satosin6(&spidx1
->dst
)->sin6_addr
, spidx0
->prefd
))
5739 if (bcmp(&spidx0
->dst
, &spidx1
->dst
, spidx0
->dst
.ss_len
) != 0)
5745 /* XXX Do we check other field ? e.g. flowinfo */
5751 key_is_addr_in_range(struct sockaddr_storage
*addr
, struct secpolicyaddrrange
*addr_range
)
5755 if (addr
== NULL
|| addr_range
== NULL
)
5758 /* Must be greater than or equal to start */
5759 cmp
= key_sockaddrcmp((struct sockaddr
*)addr
, (struct sockaddr
*)&addr_range
->start
, 1);
5760 if (cmp
!= 0 && cmp
!= 1)
5763 /* Must be less than or equal to end */
5764 cmp
= key_sockaddrcmp((struct sockaddr
*)addr
, (struct sockaddr
*)&addr_range
->end
, 1);
5765 if (cmp
!= 0 && cmp
!= -1)
5776 2: Not comparable or error
5780 struct sockaddr
*sa1
,
5781 struct sockaddr
*sa2
,
5785 int port_result
= 0;
5787 if (sa1
->sa_family
!= sa2
->sa_family
|| sa1
->sa_len
!= sa2
->sa_len
)
5790 if (sa1
->sa_len
== 0)
5793 switch (sa1
->sa_family
) {
5795 if (sa1
->sa_len
!= sizeof(struct sockaddr_in
))
5798 result
= memcmp(&satosin(sa1
)->sin_addr
.s_addr
, &satosin(sa2
)->sin_addr
.s_addr
, sizeof(satosin(sa1
)->sin_addr
.s_addr
));
5801 if (satosin(sa1
)->sin_port
< satosin(sa2
)->sin_port
) {
5803 } else if (satosin(sa1
)->sin_port
> satosin(sa2
)->sin_port
) {
5808 result
= port_result
;
5809 else if ((result
> 0 && port_result
< 0) || (result
< 0 && port_result
> 0))
5815 if (sa1
->sa_len
!= sizeof(struct sockaddr_in6
))
5816 return 2; /*EINVAL*/
5818 if (satosin6(sa1
)->sin6_scope_id
!=
5819 satosin6(sa2
)->sin6_scope_id
) {
5823 result
= memcmp(&satosin6(sa1
)->sin6_addr
.s6_addr
[0], &satosin6(sa2
)->sin6_addr
.s6_addr
[0], sizeof(struct in6_addr
));
5826 if (satosin6(sa1
)->sin6_port
< satosin6(sa2
)->sin6_port
) {
5828 } else if (satosin6(sa1
)->sin6_port
> satosin6(sa2
)->sin6_port
) {
5833 result
= port_result
;
5834 else if ((result
> 0 && port_result
< 0) || (result
< 0 && port_result
> 0))
5840 result
= memcmp(sa1
, sa2
, sa1
->sa_len
);
5844 if (result
< 0) result
= -1;
5845 else if (result
> 0) result
= 1;
5851 * compare two buffers with mask.
5855 * bits: Number of bits to compare
5868 /* XXX: This could be considerably faster if we compare a word
5869 * at a time, but it is complicated on LSB Endian machines */
5871 /* Handle null pointers */
5872 if (p1
== NULL
|| p2
== NULL
)
5882 mask
= ~((1<<(8-bits
))-1);
5883 if ((*p1
& mask
) != (*p2
& mask
))
5886 return 1; /* Match! */
5891 * scanning SPD and SAD to check status for each entries,
5892 * and do to remove or to expire.
5893 * XXX: year 2038 problem may remain.
5895 int key_timehandler_debug
= 0;
5896 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;
5897 u_int64_t total_sav_count
= 0;
5899 key_timehandler(void)
5903 struct secpolicy
**spbuf
= NULL
, **spptr
= NULL
;
5904 struct secasvar
**savexbuf
= NULL
, **savexptr
= NULL
;
5905 struct secasvar
**savkabuf
= NULL
, **savkaptr
= NULL
;
5906 int spbufcount
= 0, savbufcount
= 0, spcount
= 0, savexcount
= 0, savkacount
= 0, cnt
;
5907 int stop_handler
= 1; /* stop the timehandler */
5911 /* pre-allocate buffers before taking the lock */
5912 /* if allocation failures occur - portions of the processing will be skipped */
5913 if ((spbufcount
= ipsec_policy_count
) != 0) {
5915 KMALLOC_WAIT(spbuf
, struct secpolicy
**, spbufcount
* sizeof(struct secpolicy
*));
5919 if ((savbufcount
= ipsec_sav_count
) != 0) {
5921 KMALLOC_WAIT(savexbuf
, struct secasvar
**, savbufcount
* sizeof(struct secasvar
*));
5923 savexptr
= savexbuf
;
5924 KMALLOC_WAIT(savkabuf
, struct secasvar
**, savbufcount
* sizeof(struct secasvar
*));
5926 savkaptr
= savkabuf
;
5928 lck_mtx_lock(sadb_mutex
);
5932 struct secpolicy
*sp
, *nextsp
;
5934 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
5935 for (sp
= LIST_FIRST(&sptree
[dir
]);
5939 /* don't prevent timehandler from stopping for generate policy */
5940 if (sp
->policy
!= IPSEC_POLICY_GENERATE
)
5943 nextsp
= LIST_NEXT(sp
, chain
);
5945 if (sp
->state
== IPSEC_SPSTATE_DEAD
) {
5946 key_freesp(sp
, KEY_SADB_LOCKED
);
5950 if (sp
->lifetime
== 0 && sp
->validtime
== 0)
5952 if (spbuf
&& spcount
< spbufcount
) {
5953 /* the deletion will occur next time */
5955 && tv
.tv_sec
- sp
->created
> sp
->lifetime
)
5957 && tv
.tv_sec
- sp
->lastused
> sp
->validtime
)) {
5958 //key_spdexpire(sp);
5959 sp
->state
= IPSEC_SPSTATE_DEAD
;
5971 struct secashead
*sah
, *nextsah
;
5972 struct secasvar
*sav
, *nextsav
;
5974 for (sah
= LIST_FIRST(&sahtree
);
5979 nextsah
= LIST_NEXT(sah
, chain
);
5981 /* if sah has been dead, then delete it and process next sah. */
5982 if (sah
->state
== SADB_SASTATE_DEAD
) {
5988 if (LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]) == NULL
&&
5989 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]) == NULL
&&
5990 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]) == NULL
&&
5991 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]) == NULL
) {
5997 if (savbufcount
== 0) {
6003 /* if LARVAL entry doesn't become MATURE, delete it. */
6004 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]);
6010 nextsav
= LIST_NEXT(sav
, chain
);
6012 if (sav
->lft_h
!= NULL
) {
6013 /* If a hard lifetime is defined for the LARVAL SA, use it */
6014 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0
6015 && tv
.tv_sec
- sav
->created
> sav
->lft_h
->sadb_lifetime_addtime
) {
6016 if (sav
->always_expire
) {
6017 key_send_delete(sav
);
6020 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6021 key_freesav(sav
, KEY_SADB_LOCKED
);
6026 if (tv
.tv_sec
- sav
->created
> key_larval_lifetime
) {
6027 key_freesav(sav
, KEY_SADB_LOCKED
);
6033 * If this is a NAT traversal SA with no activity,
6034 * we need to send a keep alive.
6036 * Performed outside of the loop before so we will
6037 * only ever send one keepalive. The first SA on
6038 * the list is the one that will be used for sending
6039 * traffic, so this is the one we use for determining
6040 * when to send the keepalive.
6042 if (savkabuf
&& savkacount
< savbufcount
) {
6043 sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]); //%%% should we check dying list if this is empty???
6044 if (sav
&& (natt_keepalive_interval
|| sav
->natt_interval
) &&
6045 (sav
->flags
& (SADB_X_EXT_NATT_KEEPALIVE
| SADB_X_EXT_ESP_KEEPALIVE
)) != 0) {
6053 * check MATURE entry to start to send expire message
6056 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]);
6062 nextsav
= LIST_NEXT(sav
, chain
);
6064 /* we don't need to check. */
6065 if (sav
->lft_s
== NULL
)
6069 if (sav
->lft_c
== NULL
) {
6070 ipseclog((LOG_DEBUG
,"key_timehandler: "
6071 "There is no CURRENT time, why?\n"));
6075 /* check SOFT lifetime */
6076 if (sav
->lft_s
->sadb_lifetime_addtime
!= 0
6077 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
6079 * If always_expire is set, expire. Otherwise,
6080 * if the SA has not been used, delete immediately.
6082 if (sav
->lft_c
->sadb_lifetime_usetime
== 0
6083 && sav
->always_expire
== 0) {
6084 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6085 key_freesav(sav
, KEY_SADB_LOCKED
);
6087 } else if (savexbuf
&& savexcount
< savbufcount
) {
6088 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
6095 /* check SOFT lifetime by bytes */
6097 * XXX I don't know the way to delete this SA
6098 * when new SA is installed. Caution when it's
6099 * installed too big lifetime by time.
6101 else if (savexbuf
&& savexcount
< savbufcount
6102 && sav
->lft_s
->sadb_lifetime_bytes
!= 0
6103 && sav
->lft_s
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
6106 * XXX If we keep to send expire
6107 * message in the status of
6108 * DYING. Do remove below code.
6111 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
6118 /* check DYING entry to change status to DEAD. */
6119 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]);
6125 nextsav
= LIST_NEXT(sav
, chain
);
6127 /* we don't need to check. */
6128 if (sav
->lft_h
== NULL
)
6132 if (sav
->lft_c
== NULL
) {
6133 ipseclog((LOG_DEBUG
, "key_timehandler: "
6134 "There is no CURRENT time, why?\n"));
6138 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0
6139 && tv
.tv_sec
- sav
->created
> sav
->lft_h
->sadb_lifetime_addtime
) {
6140 if (sav
->always_expire
) {
6141 key_send_delete(sav
);
6144 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6145 key_freesav(sav
, KEY_SADB_LOCKED
);
6149 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
6150 else if (savbuf
&& savexcount
< savbufcount
6151 && sav
->lft_s
!= NULL
6152 && sav
->lft_s
->sadb_lifetime_addtime
!= 0
6153 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
6155 * XXX: should be checked to be
6156 * installed the valid SA.
6160 * If there is no SA then sending
6169 /* check HARD lifetime by bytes */
6170 else if (sav
->lft_h
->sadb_lifetime_bytes
!= 0
6171 && sav
->lft_h
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
6172 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6173 key_freesav(sav
, KEY_SADB_LOCKED
);
6178 /* delete entry in DEAD */
6179 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]);
6185 nextsav
= LIST_NEXT(sav
, chain
);
6188 if (sav
->state
!= SADB_SASTATE_DEAD
) {
6189 ipseclog((LOG_DEBUG
, "key_timehandler: "
6190 "invalid sav->state "
6191 "(queue: %d SA: %d): "
6193 SADB_SASTATE_DEAD
, sav
->state
));
6197 * do not call key_freesav() here.
6198 * sav should already be freed, and sav->refcnt
6199 * shows other references to sav
6200 * (such as from SPD).
6206 if (++key_timehandler_debug
>= 300) {
6207 if (key_debug_level
) {
6208 printf("%s: total stats for %u calls\n", __FUNCTION__
, key_timehandler_debug
);
6209 printf("%s: walked %u SPDs\n", __FUNCTION__
, spd_count
);
6210 printf("%s: walked %llu SAs: LARVAL SAs %u, MATURE SAs %u, DYING SAs %u, DEAD SAs %u\n", __FUNCTION__
,
6211 total_sav_count
, larval_sav_count
, mature_sav_count
, dying_sav_count
, dead_sav_count
);
6212 printf("%s: walked %u SAHs: DEAD SAHs %u, EMPTY SAHs %u\n", __FUNCTION__
,
6213 sah_count
, dead_sah_count
, empty_sah_count
);
6214 if (sah_search_calls
) {
6215 printf("%s: SAH search cost %d iters per call\n", __FUNCTION__
,
6216 (sah_search_count
/sah_search_calls
));
6222 empty_sah_count
= 0;
6223 larval_sav_count
= 0;
6224 mature_sav_count
= 0;
6225 dying_sav_count
= 0;
6227 total_sav_count
= 0;
6228 sah_search_count
= 0;
6229 sah_search_calls
= 0;
6230 key_timehandler_debug
= 0;
6232 #ifndef IPSEC_NONBLOCK_ACQUIRE
6235 struct secacq
*acq
, *nextacq
;
6237 for (acq
= LIST_FIRST(&acqtree
);
6242 nextacq
= LIST_NEXT(acq
, chain
);
6244 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
6245 && __LIST_CHAINED(acq
)) {
6246 LIST_REMOVE(acq
, chain
);
6255 struct secspacq
*acq
, *nextacq
;
6257 for (acq
= LIST_FIRST(&spacqtree
);
6262 nextacq
= LIST_NEXT(acq
, chain
);
6264 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
6265 && __LIST_CHAINED(acq
)) {
6266 LIST_REMOVE(acq
, chain
);
6272 /* initialize random seed */
6273 if (key_tick_init_random
++ > key_int_random
) {
6274 key_tick_init_random
= 0;
6278 uint64_t acc_sleep_time
= 0;
6279 absolutetime_to_nanoseconds(mach_absolutetime_asleep
, &acc_sleep_time
);
6280 natt_now
= ++up_time
+ (acc_sleep_time
/ NSEC_PER_SEC
);
6282 lck_mtx_unlock(sadb_mutex
);
6284 /* send messages outside of sadb_mutex */
6285 if (spbuf
&& spcount
> 0) {
6288 key_spdexpire(*(--spptr
));
6290 if (savkabuf
&& savkacount
> 0) {
6291 struct secasvar
**savkaptr_sav
= savkaptr
;
6292 int cnt_send
= savkacount
;
6294 while (cnt_send
--) {
6295 if (ipsec_send_natt_keepalive(*(--savkaptr
))) {
6296 // <rdar://6768487> iterate (all over again) and update timestamps
6297 struct secasvar
**savkaptr_update
= savkaptr_sav
;
6298 int cnt_update
= savkacount
;
6299 while (cnt_update
--) {
6300 key_update_natt_keepalive_timestamp(*savkaptr
,
6301 *(--savkaptr_update
));
6306 if (savexbuf
&& savexcount
> 0) {
6309 key_expire(*(--savexptr
));
6312 /* decrement ref counts and free buffers */
6313 lck_mtx_lock(sadb_mutex
);
6316 key_freesp(*spptr
++, KEY_SADB_LOCKED
);
6320 while (savkacount
--)
6321 key_freesav(*savkaptr
++, KEY_SADB_LOCKED
);
6325 while (savexcount
--)
6326 key_freesav(*savexptr
++, KEY_SADB_LOCKED
);
6331 key_timehandler_running
= 0;
6332 /* Turn on the ipsec bypass */
6335 /* do exchange to tick time !! */
6336 (void)timeout((void *)key_timehandler
, (void *)0, hz
);
6339 lck_mtx_unlock(sadb_mutex
);
6344 * to initialize a seed for random()
6350 /* Our PRNG is based on Yarrow and doesn't need to be seeded */
6357 srandom(tv
.tv_usec
);
6368 key_randomfill(&value
, sizeof(value
));
6378 cc_rand_generate(p
, l
);
6382 static int warn
= 1;
6385 n
= (size_t)read_random(p
, (u_int
)l
);
6389 bcopy(&v
, (u_int8_t
*)p
+ n
,
6390 l
- n
< sizeof(v
) ? l
- n
: sizeof(v
));
6394 printf("WARNING: pseudo-random number generator "
6395 "used for IPsec processing\n");
6403 * map SADB_SATYPE_* to IPPROTO_*.
6404 * if satype == SADB_SATYPE then satype is mapped to ~0.
6406 * 0: invalid satype.
6413 case SADB_SATYPE_UNSPEC
:
6414 return IPSEC_PROTO_ANY
;
6415 case SADB_SATYPE_AH
:
6417 case SADB_SATYPE_ESP
:
6419 case SADB_X_SATYPE_IPCOMP
:
6420 return IPPROTO_IPCOMP
;
6428 * map IPPROTO_* to SADB_SATYPE_*
6430 * 0: invalid protocol type.
6438 return SADB_SATYPE_AH
;
6440 return SADB_SATYPE_ESP
;
6441 case IPPROTO_IPCOMP
:
6442 return SADB_X_SATYPE_IPCOMP
;
6450 key_get_ipsec_if_from_message (const struct sadb_msghdr
*mhp
, int message_type
)
6452 struct sadb_x_ipsecif
*ipsecifopts
= NULL
;
6453 ifnet_t ipsec_if
= NULL
;
6455 ipsecifopts
= (struct sadb_x_ipsecif
*)(void *)mhp
->ext
[message_type
];
6456 if (ipsecifopts
!= NULL
) {
6457 if (ipsecifopts
->sadb_x_ipsecif_ipsec_if
[0]) {
6458 ifnet_find_by_name(ipsecifopts
->sadb_x_ipsecif_ipsec_if
, &ipsec_if
);
6466 key_get_outgoing_ifindex_from_message (const struct sadb_msghdr
*mhp
, int message_type
)
6468 struct sadb_x_ipsecif
*ipsecifopts
= NULL
;
6469 ifnet_t outgoing_if
= NULL
;
6471 ipsecifopts
= (struct sadb_x_ipsecif
*)(void *)mhp
->ext
[message_type
];
6472 if (ipsecifopts
!= NULL
) {
6473 if (ipsecifopts
->sadb_x_ipsecif_outgoing_if
[0]) {
6474 ifnet_find_by_name(ipsecifopts
->sadb_x_ipsecif_outgoing_if
, &outgoing_if
);
6478 return outgoing_if
? outgoing_if
->if_index
: 0;
6483 * SADB_GETSPI processing is to receive
6484 * <base, (SA2), src address, dst address, (SPI range)>
6485 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
6486 * tree with the status of LARVAL, and send
6487 * <base, SA(*), address(SD)>
6490 * IN: mhp: pointer to the pointer to each header.
6491 * OUT: NULL if fail.
6492 * other if success, return pointer to the message to send.
6498 const struct sadb_msghdr
*mhp
)
6500 struct sadb_address
*src0
, *dst0
;
6501 struct secasindex saidx
;
6502 struct secashead
*newsah
;
6503 struct secasvar
*newsav
;
6504 ifnet_t ipsec_if
= NULL
;
6511 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6514 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6515 panic("key_getspi: NULL pointer is passed.\n");
6517 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6518 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
6519 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
6520 return key_senderror(so
, m
, EINVAL
);
6522 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6523 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
6524 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
6525 return key_senderror(so
, m
, EINVAL
);
6527 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
6528 mode
= ((struct sadb_x_sa2
*)
6529 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
6530 reqid
= ((struct sadb_x_sa2
*)
6531 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
6533 mode
= IPSEC_MODE_ANY
;
6537 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
6538 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
6540 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
6542 /* map satype to proto */
6543 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6544 ipseclog((LOG_DEBUG
, "key_getspi: invalid satype is passed.\n"));
6545 return key_senderror(so
, m
, EINVAL
);
6548 /* make sure if port number is zero. */
6549 switch (((struct sockaddr
*)(src0
+ 1))->sa_family
) {
6551 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
6552 sizeof(struct sockaddr_in
))
6553 return key_senderror(so
, m
, EINVAL
);
6554 ((struct sockaddr_in
*)(void *)(src0
+ 1))->sin_port
= 0;
6557 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
6558 sizeof(struct sockaddr_in6
))
6559 return key_senderror(so
, m
, EINVAL
);
6560 ((struct sockaddr_in6
*)(void *)(src0
+ 1))->sin6_port
= 0;
6565 switch (((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
6567 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
6568 sizeof(struct sockaddr_in
))
6569 return key_senderror(so
, m
, EINVAL
);
6570 ((struct sockaddr_in
*)(void *)(dst0
+ 1))->sin_port
= 0;
6573 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
6574 sizeof(struct sockaddr_in6
))
6575 return key_senderror(so
, m
, EINVAL
);
6576 ((struct sockaddr_in6
*)(void *)(dst0
+ 1))->sin6_port
= 0;
6582 /* XXX boundary check against sa_len */
6583 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
6585 lck_mtx_lock(sadb_mutex
);
6587 /* SPI allocation */
6588 spi
= key_do_getnewspi((struct sadb_spirange
*)
6589 (void *)mhp
->ext
[SADB_EXT_SPIRANGE
], &saidx
);
6591 lck_mtx_unlock(sadb_mutex
);
6592 return key_senderror(so
, m
, EINVAL
);
6595 /* get a SA index */
6596 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
6597 /* create a new SA index: key_addspi is always used for inbound spi */
6598 if ((newsah
= key_newsah(&saidx
, ipsec_if
, key_get_outgoing_ifindex_from_message(mhp
, SADB_X_EXT_IPSECIF
), IPSEC_DIR_INBOUND
)) == NULL
) {
6599 lck_mtx_unlock(sadb_mutex
);
6600 ipseclog((LOG_DEBUG
, "key_getspi: No more memory.\n"));
6601 return key_senderror(so
, m
, ENOBUFS
);
6607 newsav
= key_newsav(m
, mhp
, newsah
, &error
, so
);
6608 if (newsav
== NULL
) {
6609 /* XXX don't free new SA index allocated in above. */
6610 lck_mtx_unlock(sadb_mutex
);
6611 return key_senderror(so
, m
, error
);
6615 key_setspi(newsav
, htonl(spi
));
6617 #ifndef IPSEC_NONBLOCK_ACQUIRE
6618 /* delete the entry in acqtree */
6619 if (mhp
->msg
->sadb_msg_seq
!= 0) {
6621 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) != NULL
) {
6622 /* reset counter in order to deletion by timehandler. */
6625 acq
->created
= tv
.tv_sec
;
6631 lck_mtx_unlock(sadb_mutex
);
6634 struct mbuf
*n
, *nn
;
6635 struct sadb_sa
*m_sa
;
6636 struct sadb_msg
*newmsg
;
6639 /* create new sadb_msg to reply. */
6640 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
)) +
6641 PFKEY_ALIGN8(sizeof(struct sadb_sa
));
6643 return key_senderror(so
, m
, ENOBUFS
);
6645 MGETHDR(n
, M_WAITOK
, MT_DATA
);
6646 if (n
&& len
> MHLEN
) {
6647 MCLGET(n
, M_WAITOK
);
6648 if ((n
->m_flags
& M_EXT
) == 0) {
6654 return key_senderror(so
, m
, ENOBUFS
);
6660 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
6661 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
6663 m_sa
= (struct sadb_sa
*)(void *)(mtod(n
, caddr_t
) + off
);
6664 m_sa
->sadb_sa_len
= PFKEY_UNIT64(sizeof(struct sadb_sa
));
6665 m_sa
->sadb_sa_exttype
= SADB_EXT_SA
;
6666 m_sa
->sadb_sa_spi
= htonl(spi
);
6667 off
+= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
6671 panic("length inconsistency in key_getspi");
6674 int mbufItems
[] = {SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
6675 n
->m_next
= key_gather_mbuf(m
, mhp
, 0, sizeof(mbufItems
)/sizeof(int), mbufItems
);
6678 return key_senderror(so
, m
, ENOBUFS
);
6682 if (n
->m_len
< sizeof(struct sadb_msg
)) {
6683 n
= m_pullup(n
, sizeof(struct sadb_msg
));
6685 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
6688 n
->m_pkthdr
.len
= 0;
6689 for (nn
= n
; nn
; nn
= nn
->m_next
)
6690 n
->m_pkthdr
.len
+= nn
->m_len
;
6692 newmsg
= mtod(n
, struct sadb_msg
*);
6693 newmsg
->sadb_msg_seq
= newsav
->seq
;
6694 newmsg
->sadb_msg_errno
= 0;
6695 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
6698 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
6703 key_getspi2(struct sockaddr
*src
,
6704 struct sockaddr
*dst
,
6708 struct sadb_spirange
*spirange
)
6711 struct secasindex saidx
;
6713 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6715 /* XXX boundary check against sa_len */
6716 KEY_SETSECASIDX(proto
, mode
, reqid
, src
, dst
, 0, &saidx
);
6718 /* make sure if port number is zero. */
6719 switch (((struct sockaddr
*)&saidx
.src
)->sa_family
) {
6721 if (((struct sockaddr
*)&saidx
.src
)->sa_len
!= sizeof(struct sockaddr_in
))
6723 ((struct sockaddr_in
*)&saidx
.src
)->sin_port
= 0;
6726 if (((struct sockaddr
*)&saidx
.src
)->sa_len
!= sizeof(struct sockaddr_in6
))
6728 ((struct sockaddr_in6
*)&saidx
.src
)->sin6_port
= 0;
6733 switch (((struct sockaddr
*)&saidx
.dst
)->sa_family
) {
6735 if (((struct sockaddr
*)&saidx
.dst
)->sa_len
!= sizeof(struct sockaddr_in
))
6737 ((struct sockaddr_in
*)&saidx
.dst
)->sin_port
= 0;
6740 if (((struct sockaddr
*)&saidx
.dst
)->sa_len
!= sizeof(struct sockaddr_in6
))
6742 ((struct sockaddr_in6
*)&saidx
.dst
)->sin6_port
= 0;
6748 lck_mtx_lock(sadb_mutex
);
6750 /* SPI allocation */
6751 spi
= key_do_getnewspi(spirange
, &saidx
);
6753 lck_mtx_unlock(sadb_mutex
);
6759 * allocating new SPI
6760 * called by key_getspi() and key_getspi2().
6767 struct sadb_spirange
*spirange
,
6768 struct secasindex
*saidx
)
6771 u_int32_t keymin
, keymax
;
6772 int count
= key_spi_trycnt
;
6774 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6776 /* set spi range to allocate */
6777 if (spirange
!= NULL
) {
6778 keymin
= spirange
->sadb_spirange_min
;
6779 keymax
= spirange
->sadb_spirange_max
;
6781 keymin
= key_spi_minval
;
6782 keymax
= key_spi_maxval
;
6784 /* IPCOMP needs 2-byte SPI */
6785 if (saidx
->proto
== IPPROTO_IPCOMP
) {
6787 if (keymin
>= 0x10000)
6789 if (keymax
>= 0x10000)
6791 if (keymin
> keymax
) {
6792 t
= keymin
; keymin
= keymax
; keymax
= t
;
6796 if (keymin
== keymax
) {
6797 if (key_checkspidup(saidx
, keymin
) != NULL
) {
6798 ipseclog((LOG_DEBUG
, "key_do_getnewspi: SPI %u exists already.\n", keymin
));
6802 count
--; /* taking one cost. */
6807 u_int32_t range
= keymax
- keymin
+ 1; /* overflow value of zero means full range */
6812 /* when requesting to allocate spi ranged */
6814 u_int32_t rand_val
= key_random();
6816 /* generate pseudo-random SPI value ranged. */
6817 newspi
= (range
== 0 ? rand_val
: keymin
+ (rand_val
% range
));
6819 if (key_checkspidup(saidx
, newspi
) == NULL
)
6823 if (count
== 0 || newspi
== 0) {
6824 ipseclog((LOG_DEBUG
, "key_do_getnewspi: to allocate spi is failed.\n"));
6830 keystat
.getspi_count
=
6831 (keystat
.getspi_count
+ key_spi_trycnt
- count
) / 2;
6837 * SADB_UPDATE processing
6839 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
6840 * key(AE), (identity(SD),) (sensitivity)>
6841 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
6843 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
6844 * (identity(SD),) (sensitivity)>
6847 * m will always be freed.
6853 const struct sadb_msghdr
*mhp
)
6855 struct sadb_sa
*sa0
;
6856 struct sadb_address
*src0
, *dst0
;
6857 ifnet_t ipsec_if
= NULL
;
6858 struct secasindex saidx
;
6859 struct secashead
*sah
;
6860 struct secasvar
*sav
;
6867 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6870 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6871 panic("key_update: NULL pointer is passed.\n");
6873 /* map satype to proto */
6874 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6875 ipseclog((LOG_DEBUG
, "key_update: invalid satype is passed.\n"));
6876 return key_senderror(so
, m
, EINVAL
);
6879 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
6880 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6881 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
6882 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
6883 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
6884 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
6885 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
6886 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
6887 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
6888 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
6889 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
6890 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
6891 return key_senderror(so
, m
, EINVAL
);
6893 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
6894 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6895 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
6896 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
6897 return key_senderror(so
, m
, EINVAL
);
6899 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
6900 mode
= ((struct sadb_x_sa2
*)
6901 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
6902 reqid
= ((struct sadb_x_sa2
*)
6903 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
6904 flags2
= ((struct sadb_x_sa2
*)(void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_flags
;
6906 mode
= IPSEC_MODE_ANY
;
6910 /* XXX boundary checking for other extensions */
6912 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
6913 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
6914 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
6915 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
6917 /* XXX boundary check against sa_len */
6918 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
6920 lck_mtx_lock(sadb_mutex
);
6922 /* get a SA header */
6923 if ((sah
= key_getsah(&saidx
)) == NULL
) {
6924 lck_mtx_unlock(sadb_mutex
);
6925 ipseclog((LOG_DEBUG
, "key_update: no SA index found.\n"));
6926 return key_senderror(so
, m
, ENOENT
);
6929 /* set spidx if there */
6931 error
= key_setident(sah
, m
, mhp
);
6933 lck_mtx_unlock(sadb_mutex
);
6934 return key_senderror(so
, m
, error
);
6937 /* find a SA with sequence number. */
6938 #if IPSEC_DOSEQCHECK
6939 if (mhp
->msg
->sadb_msg_seq
!= 0
6940 && (sav
= key_getsavbyseq(sah
, mhp
->msg
->sadb_msg_seq
)) == NULL
) {
6941 lck_mtx_unlock(sadb_mutex
);
6942 ipseclog((LOG_DEBUG
,
6943 "key_update: no larval SA with sequence %u exists.\n",
6944 mhp
->msg
->sadb_msg_seq
));
6945 return key_senderror(so
, m
, ENOENT
);
6948 if ((sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
)) == NULL
) {
6949 lck_mtx_unlock(sadb_mutex
);
6950 ipseclog((LOG_DEBUG
,
6951 "key_update: no such a SA found (spi:%u)\n",
6952 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
6953 return key_senderror(so
, m
, EINVAL
);
6957 /* validity check */
6958 if (sav
->sah
->saidx
.proto
!= proto
) {
6959 lck_mtx_unlock(sadb_mutex
);
6960 ipseclog((LOG_DEBUG
,
6961 "key_update: protocol mismatched (DB=%u param=%u)\n",
6962 sav
->sah
->saidx
.proto
, proto
));
6963 return key_senderror(so
, m
, EINVAL
);
6965 #if IPSEC_DOSEQCHECK
6966 if (sav
->spi
!= sa0
->sadb_sa_spi
) {
6967 lck_mtx_unlock(sadb_mutex
);
6968 ipseclog((LOG_DEBUG
,
6969 "key_update: SPI mismatched (DB:%u param:%u)\n",
6970 (u_int32_t
)ntohl(sav
->spi
),
6971 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
6972 return key_senderror(so
, m
, EINVAL
);
6975 if (sav
->pid
!= mhp
->msg
->sadb_msg_pid
) {
6976 lck_mtx_unlock(sadb_mutex
);
6977 ipseclog((LOG_DEBUG
,
6978 "key_update: pid mismatched (DB:%u param:%u)\n",
6979 sav
->pid
, mhp
->msg
->sadb_msg_pid
));
6980 return key_senderror(so
, m
, EINVAL
);
6983 /* copy sav values */
6984 error
= key_setsaval(sav
, m
, mhp
);
6986 key_freesav(sav
, KEY_SADB_LOCKED
);
6987 lck_mtx_unlock(sadb_mutex
);
6988 return key_senderror(so
, m
, error
);
6991 sav
->flags2
= flags2
;
6992 if (flags2
& SADB_X_EXT_SA2_DELETE_ON_DETACH
) {
6997 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
6998 * this SA is for transport mode - otherwise clear it.
7000 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0 &&
7001 (sav
->sah
->saidx
.mode
!= IPSEC_MODE_TRANSPORT
||
7002 sav
->sah
->saidx
.src
.ss_family
!= AF_INET
))
7003 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
7005 /* check SA values to be mature. */
7006 if ((error
= key_mature(sav
)) != 0) {
7007 key_freesav(sav
, KEY_SADB_LOCKED
);
7008 lck_mtx_unlock(sadb_mutex
);
7009 return key_senderror(so
, m
, error
);
7012 lck_mtx_unlock(sadb_mutex
);
7017 /* set msg buf from mhp */
7018 n
= key_getmsgbuf_x1(m
, mhp
);
7020 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
7021 return key_senderror(so
, m
, ENOBUFS
);
7025 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
7030 key_migrate(struct socket
*so
,
7032 const struct sadb_msghdr
*mhp
)
7034 struct sadb_sa
*sa0
= NULL
;
7035 struct sadb_address
*src0
= NULL
;
7036 struct sadb_address
*dst0
= NULL
;
7037 struct sadb_address
*src1
= NULL
;
7038 struct sadb_address
*dst1
= NULL
;
7039 ifnet_t ipsec_if0
= NULL
;
7040 ifnet_t ipsec_if1
= NULL
;
7041 struct secasindex saidx0
;
7042 struct secasindex saidx1
;
7043 struct secashead
*sah
= NULL
;
7044 struct secashead
*newsah
= NULL
;
7045 struct secasvar
*sav
= NULL
;
7048 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7051 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7052 panic("key_migrate: NULL pointer is passed.\n");
7054 /* map satype to proto */
7055 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7056 ipseclog((LOG_DEBUG
, "key_migrate: invalid satype is passed.\n"));
7057 return key_senderror(so
, m
, EINVAL
);
7060 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
7061 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
7062 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
7063 mhp
->ext
[SADB_EXT_MIGRATE_ADDRESS_SRC
] == NULL
||
7064 mhp
->ext
[SADB_EXT_MIGRATE_ADDRESS_DST
] == NULL
) {
7065 ipseclog((LOG_DEBUG
, "key_migrate: invalid message is passed.\n"));
7066 return key_senderror(so
, m
, EINVAL
);
7069 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
7070 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7071 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
7072 mhp
->extlen
[SADB_EXT_MIGRATE_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7073 mhp
->extlen
[SADB_EXT_MIGRATE_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
7074 ipseclog((LOG_DEBUG
, "key_migrate: invalid message is passed.\n"));
7075 return key_senderror(so
, m
, EINVAL
);
7078 lck_mtx_lock(sadb_mutex
);
7080 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
7081 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
7082 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
7083 src1
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_MIGRATE_ADDRESS_SRC
]);
7084 dst1
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_MIGRATE_ADDRESS_DST
]);
7085 ipsec_if0
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
7086 ipsec_if1
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_MIGRATE_IPSECIF
);
7088 /* Find existing SAH and SAV */
7089 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if0
? ipsec_if0
->if_index
: 0, &saidx0
);
7091 LIST_FOREACH(sah
, &sahtree
, chain
) {
7092 if (sah
->state
!= SADB_SASTATE_MATURE
)
7094 if (key_cmpsaidx(&sah
->saidx
, &saidx0
, CMP_HEAD
) == 0)
7097 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
7098 if (sav
&& sav
->state
== SADB_SASTATE_MATURE
)
7102 lck_mtx_unlock(sadb_mutex
);
7103 ipseclog((LOG_DEBUG
, "key_migrate: no mature SAH found.\n"));
7104 return key_senderror(so
, m
, ENOENT
);
7108 lck_mtx_unlock(sadb_mutex
);
7109 ipseclog((LOG_DEBUG
, "key_migrate: no SA found.\n"));
7110 return key_senderror(so
, m
, ENOENT
);
7113 /* Find or create new SAH */
7114 KEY_SETSECASIDX(proto
, sah
->saidx
.mode
, sah
->saidx
.reqid
, src1
+ 1, dst1
+ 1, ipsec_if1
? ipsec_if1
->if_index
: 0, &saidx1
);
7116 if ((newsah
= key_getsah(&saidx1
)) == NULL
) {
7117 if ((newsah
= key_newsah(&saidx1
, ipsec_if1
, key_get_outgoing_ifindex_from_message(mhp
, SADB_X_EXT_MIGRATE_IPSECIF
), sah
->dir
)) == NULL
) {
7118 lck_mtx_unlock(sadb_mutex
);
7119 ipseclog((LOG_DEBUG
, "key_migrate: No more memory.\n"));
7120 return key_senderror(so
, m
, ENOBUFS
);
7124 /* Migrate SAV in to new SAH */
7125 if (key_migratesav(sav
, newsah
) != 0) {
7126 lck_mtx_unlock(sadb_mutex
);
7127 ipseclog((LOG_DEBUG
, "key_migrate: Failed to migrate SA to new SAH.\n"));
7128 return key_senderror(so
, m
, EINVAL
);
7131 /* Reset NAT values */
7132 sav
->flags
= sa0
->sadb_sa_flags
;
7133 sav
->remote_ike_port
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
;
7134 sav
->natt_interval
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_interval
;
7135 sav
->natt_offload_interval
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_offload_interval
;
7136 sav
->natt_last_activity
= natt_now
;
7139 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
7140 * SADB_X_EXT_NATT is set and SADB_X_EXT_NATT_KEEPALIVE is not
7141 * set (we're not behind nat) - otherwise clear it.
7143 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
7144 if ((sav
->flags
& SADB_X_EXT_NATT
) == 0 ||
7145 (sav
->flags
& SADB_X_EXT_NATT_KEEPALIVE
) != 0)
7146 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
7148 lck_mtx_unlock(sadb_mutex
);
7151 struct sadb_msg
*newmsg
;
7152 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
7153 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
, SADB_X_EXT_IPSECIF
,
7154 SADB_EXT_MIGRATE_ADDRESS_SRC
, SADB_EXT_MIGRATE_ADDRESS_DST
, SADB_X_EXT_MIGRATE_IPSECIF
};
7156 /* create new sadb_msg to reply. */
7157 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
7159 return key_senderror(so
, m
, ENOBUFS
);
7161 if (n
->m_len
< sizeof(struct sadb_msg
)) {
7162 n
= m_pullup(n
, sizeof(struct sadb_msg
));
7164 return key_senderror(so
, m
, ENOBUFS
);
7166 newmsg
= mtod(n
, struct sadb_msg
*);
7167 newmsg
->sadb_msg_errno
= 0;
7168 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
7171 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
7176 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
7177 * only called by key_update().
7180 * others : found, pointer to a SA.
7182 #if IPSEC_DOSEQCHECK
7183 static struct secasvar
*
7185 struct secashead
*sah
,
7188 struct secasvar
*sav
;
7191 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7193 state
= SADB_SASTATE_LARVAL
;
7195 /* search SAD with sequence number ? */
7196 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
7198 KEY_CHKSASTATE(state
, sav
->state
, "key_getsabyseq");
7200 if (sav
->seq
== seq
) {
7202 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
7203 printf("DP key_getsavbyseq cause "
7204 "refcnt++:%d SA:0x%llx\n", sav
->refcnt
,
7205 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
7215 * SADB_ADD processing
7216 * add a entry to SA database, when received
7217 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
7218 * key(AE), (identity(SD),) (sensitivity)>
7221 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
7222 * (identity(SD),) (sensitivity)>
7225 * IGNORE identity and sensitivity messages.
7227 * m will always be freed.
7233 const struct sadb_msghdr
*mhp
)
7235 struct sadb_sa
*sa0
;
7236 struct sadb_address
*src0
, *dst0
;
7237 ifnet_t ipsec_if
= NULL
;
7238 struct secasindex saidx
;
7239 struct secashead
*newsah
;
7240 struct secasvar
*newsav
;
7246 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7249 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7250 panic("key_add: NULL pointer is passed.\n");
7252 /* map satype to proto */
7253 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7254 ipseclog((LOG_DEBUG
, "key_add: invalid satype is passed.\n"));
7256 return key_senderror(so
, m
, EINVAL
);
7259 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
7260 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
7261 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
7262 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
7263 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
7264 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
7265 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
7266 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
7267 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
7268 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
7269 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
7270 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
7272 return key_senderror(so
, m
, EINVAL
);
7274 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
7275 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7276 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
7278 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
7280 return key_senderror(so
, m
, EINVAL
);
7282 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
7283 mode
= ((struct sadb_x_sa2
*)
7284 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
7285 reqid
= ((struct sadb_x_sa2
*)
7286 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
7288 mode
= IPSEC_MODE_ANY
;
7292 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
7293 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
7294 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
7295 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
7297 /* XXX boundary check against sa_len */
7298 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
7300 lck_mtx_lock(sadb_mutex
);
7302 /* get a SA header */
7303 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
7304 /* create a new SA header: key_addspi is always used for outbound spi */
7305 if ((newsah
= key_newsah(&saidx
, ipsec_if
, key_get_outgoing_ifindex_from_message(mhp
, SADB_X_EXT_IPSECIF
), IPSEC_DIR_OUTBOUND
)) == NULL
) {
7306 lck_mtx_unlock(sadb_mutex
);
7307 ipseclog((LOG_DEBUG
, "key_add: No more memory.\n"));
7309 return key_senderror(so
, m
, ENOBUFS
);
7313 /* set spidx if there */
7315 error
= key_setident(newsah
, m
, mhp
);
7317 lck_mtx_unlock(sadb_mutex
);
7319 return key_senderror(so
, m
, error
);
7322 /* create new SA entry. */
7323 /* We can create new SA only if SPI is different. */
7324 if (key_getsavbyspi(newsah
, sa0
->sadb_sa_spi
)) {
7325 lck_mtx_unlock(sadb_mutex
);
7326 ipseclog((LOG_DEBUG
, "key_add: SA already exists.\n"));
7328 return key_senderror(so
, m
, EEXIST
);
7330 newsav
= key_newsav(m
, mhp
, newsah
, &error
, so
);
7331 if (newsav
== NULL
) {
7332 lck_mtx_unlock(sadb_mutex
);
7334 return key_senderror(so
, m
, error
);
7338 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
7339 * this SA is for transport mode - otherwise clear it.
7341 if ((newsav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0 &&
7342 (newsah
->saidx
.mode
!= IPSEC_MODE_TRANSPORT
||
7343 newsah
->saidx
.dst
.ss_family
!= AF_INET
))
7344 newsav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
7346 /* check SA values to be mature. */
7347 if ((error
= key_mature(newsav
)) != 0) {
7348 key_freesav(newsav
, KEY_SADB_LOCKED
);
7349 lck_mtx_unlock(sadb_mutex
);
7351 return key_senderror(so
, m
, error
);
7354 lck_mtx_unlock(sadb_mutex
);
7357 * don't call key_freesav() here, as we would like to keep the SA
7358 * in the database on success.
7364 /* set msg buf from mhp */
7365 n
= key_getmsgbuf_x1(m
, mhp
);
7367 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
7369 return key_senderror(so
, m
, ENOBUFS
);
7372 // mh.ext points to the mbuf content.
7373 // Zero out Encryption and Integrity keys if present.
7376 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
7383 struct secashead
*sah
,
7385 const struct sadb_msghdr
*mhp
)
7387 const struct sadb_ident
*idsrc
, *iddst
;
7388 int idsrclen
, iddstlen
;
7390 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7393 if (sah
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7394 panic("key_setident: NULL pointer is passed.\n");
7396 /* don't make buffer if not there */
7397 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
&&
7398 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
7404 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
||
7405 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
7406 ipseclog((LOG_DEBUG
, "key_setident: invalid identity.\n"));
7410 idsrc
= (const struct sadb_ident
*)
7411 (void *)mhp
->ext
[SADB_EXT_IDENTITY_SRC
];
7412 iddst
= (const struct sadb_ident
*)
7413 (void *)mhp
->ext
[SADB_EXT_IDENTITY_DST
];
7414 idsrclen
= mhp
->extlen
[SADB_EXT_IDENTITY_SRC
];
7415 iddstlen
= mhp
->extlen
[SADB_EXT_IDENTITY_DST
];
7417 /* validity check */
7418 if (idsrc
->sadb_ident_type
!= iddst
->sadb_ident_type
) {
7419 ipseclog((LOG_DEBUG
, "key_setident: ident type mismatch.\n"));
7423 switch (idsrc
->sadb_ident_type
) {
7424 case SADB_IDENTTYPE_PREFIX
:
7425 case SADB_IDENTTYPE_FQDN
:
7426 case SADB_IDENTTYPE_USERFQDN
:
7428 /* XXX do nothing */
7434 /* make structure */
7435 KMALLOC_NOWAIT(sah
->idents
, struct sadb_ident
*, idsrclen
);
7436 if (sah
->idents
== NULL
) {
7437 lck_mtx_unlock(sadb_mutex
);
7438 KMALLOC_WAIT(sah
->idents
, struct sadb_ident
*, idsrclen
);
7439 lck_mtx_lock(sadb_mutex
);
7440 if (sah
->idents
== NULL
) {
7441 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
7445 KMALLOC_NOWAIT(sah
->identd
, struct sadb_ident
*, iddstlen
);
7446 if (sah
->identd
== NULL
) {
7447 lck_mtx_unlock(sadb_mutex
);
7448 KMALLOC_WAIT(sah
->identd
, struct sadb_ident
*, iddstlen
);
7449 lck_mtx_lock(sadb_mutex
);
7450 if (sah
->identd
== NULL
) {
7453 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
7457 bcopy(idsrc
, sah
->idents
, idsrclen
);
7458 bcopy(iddst
, sah
->identd
, iddstlen
);
7464 * m will not be freed on return.
7465 * it is caller's responsibility to free the result.
7467 static struct mbuf
*
7470 const struct sadb_msghdr
*mhp
)
7473 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
7474 SADB_X_EXT_SA2
, SADB_EXT_ADDRESS_SRC
,
7475 SADB_EXT_ADDRESS_DST
, SADB_EXT_LIFETIME_HARD
,
7476 SADB_EXT_LIFETIME_SOFT
, SADB_EXT_IDENTITY_SRC
,
7477 SADB_EXT_IDENTITY_DST
};
7480 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7481 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
7483 /* create new sadb_msg to reply. */
7484 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
7488 if (n
->m_len
< sizeof(struct sadb_msg
)) {
7489 n
= m_pullup(n
, sizeof(struct sadb_msg
));
7493 mtod(n
, struct sadb_msg
*)->sadb_msg_errno
= 0;
7494 mtod(n
, struct sadb_msg
*)->sadb_msg_len
=
7495 PFKEY_UNIT64(n
->m_pkthdr
.len
);
7500 static int key_delete_all(struct socket
*, struct mbuf
*,
7501 const struct sadb_msghdr
*, u_int16_t
);
7504 * SADB_DELETE processing
7506 * <base, SA(*), address(SD)>
7507 * from the ikmpd, and set SADB_SASTATE_DEAD,
7509 * <base, SA(*), address(SD)>
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
*sah
;
7525 struct secasvar
*sav
= NULL
;
7528 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7531 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7532 panic("key_delete: NULL pointer is passed.\n");
7534 /* map satype to proto */
7535 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7536 ipseclog((LOG_DEBUG
, "key_delete: invalid satype is passed.\n"));
7537 return key_senderror(so
, m
, EINVAL
);
7540 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
7541 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
7542 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
7543 return key_senderror(so
, m
, EINVAL
);
7546 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7547 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
7548 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
7549 return key_senderror(so
, m
, EINVAL
);
7552 lck_mtx_lock(sadb_mutex
);
7554 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
7556 * Caller wants us to delete all non-LARVAL SAs
7557 * that match the src/dst. This is used during
7558 * IKE INITIAL-CONTACT.
7560 ipseclog((LOG_DEBUG
, "key_delete: doing delete all.\n"));
7561 /* key_delete_all will unlock sadb_mutex */
7562 return key_delete_all(so
, m
, mhp
, proto
);
7563 } else if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
)) {
7564 lck_mtx_unlock(sadb_mutex
);
7565 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
7566 return key_senderror(so
, m
, EINVAL
);
7569 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
7570 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
7571 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
7572 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
7574 /* XXX boundary check against sa_len */
7575 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
7577 /* get a SA header */
7578 LIST_FOREACH(sah
, &sahtree
, chain
) {
7579 if (sah
->state
== SADB_SASTATE_DEAD
)
7581 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
7584 /* get a SA with SPI. */
7585 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
7590 lck_mtx_unlock(sadb_mutex
);
7591 ipseclog((LOG_DEBUG
, "key_delete: no SA found.\n"));
7592 return key_senderror(so
, m
, ENOENT
);
7595 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
7596 key_freesav(sav
, KEY_SADB_LOCKED
);
7598 lck_mtx_unlock(sadb_mutex
);
7603 struct sadb_msg
*newmsg
;
7604 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
7605 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
7607 /* create new sadb_msg to reply. */
7608 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
7610 return key_senderror(so
, m
, ENOBUFS
);
7612 if (n
->m_len
< sizeof(struct sadb_msg
)) {
7613 n
= m_pullup(n
, sizeof(struct sadb_msg
));
7615 return key_senderror(so
, m
, ENOBUFS
);
7617 newmsg
= mtod(n
, struct sadb_msg
*);
7618 newmsg
->sadb_msg_errno
= 0;
7619 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
7622 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
7627 * delete all SAs for src/dst. Called from key_delete().
7633 const struct sadb_msghdr
*mhp
,
7636 struct sadb_address
*src0
, *dst0
;
7637 ifnet_t ipsec_if
= NULL
;
7638 struct secasindex saidx
;
7639 struct secashead
*sah
;
7640 struct secasvar
*sav
, *nextsav
;
7641 u_int stateidx
, state
;
7643 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7645 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
7646 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
7647 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
7649 /* XXX boundary check against sa_len */
7650 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
7652 LIST_FOREACH(sah
, &sahtree
, chain
) {
7653 if (sah
->state
== SADB_SASTATE_DEAD
)
7655 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
7658 /* Delete all non-LARVAL SAs. */
7660 stateidx
< _ARRAYLEN(saorder_state_alive
);
7662 state
= saorder_state_alive
[stateidx
];
7663 if (state
== SADB_SASTATE_LARVAL
)
7665 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
7666 sav
!= NULL
; sav
= nextsav
) {
7667 nextsav
= LIST_NEXT(sav
, chain
);
7669 if (sav
->state
!= state
) {
7670 ipseclog((LOG_DEBUG
, "key_delete_all: "
7671 "invalid sav->state "
7672 "(queue: %d SA: %d)\n",
7673 state
, sav
->state
));
7677 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
7678 key_freesav(sav
, KEY_SADB_LOCKED
);
7682 lck_mtx_unlock(sadb_mutex
);
7686 struct sadb_msg
*newmsg
;
7687 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_ADDRESS_SRC
,
7688 SADB_EXT_ADDRESS_DST
};
7690 /* create new sadb_msg to reply. */
7691 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
7693 return key_senderror(so
, m
, ENOBUFS
);
7695 if (n
->m_len
< sizeof(struct sadb_msg
)) {
7696 n
= m_pullup(n
, sizeof(struct sadb_msg
));
7698 return key_senderror(so
, m
, ENOBUFS
);
7700 newmsg
= mtod(n
, struct sadb_msg
*);
7701 newmsg
->sadb_msg_errno
= 0;
7702 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
7705 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
7710 * SADB_GET processing
7712 * <base, SA(*), address(SD)>
7713 * from the ikmpd, and get a SP and a SA to respond,
7715 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
7716 * (identity(SD),) (sensitivity)>
7719 * m will always be freed.
7725 const struct sadb_msghdr
*mhp
)
7727 struct sadb_sa
*sa0
;
7728 struct sadb_address
*src0
, *dst0
;
7729 ifnet_t ipsec_if
= NULL
;
7730 struct secasindex saidx
;
7731 struct secashead
*sah
;
7732 struct secasvar
*sav
= NULL
;
7735 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7738 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7739 panic("key_get: NULL pointer is passed.\n");
7741 /* map satype to proto */
7742 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7743 ipseclog((LOG_DEBUG
, "key_get: invalid satype is passed.\n"));
7744 return key_senderror(so
, m
, EINVAL
);
7747 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
7748 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
7749 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
7750 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
7751 return key_senderror(so
, m
, EINVAL
);
7753 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
7754 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7755 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
7756 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
7757 return key_senderror(so
, m
, EINVAL
);
7760 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
7761 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
7762 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
7763 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
7765 /* XXX boundary check against sa_len */
7766 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
7768 lck_mtx_lock(sadb_mutex
);
7770 /* get a SA header */
7771 LIST_FOREACH(sah
, &sahtree
, chain
) {
7772 if (sah
->state
== SADB_SASTATE_DEAD
)
7774 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
7777 /* get a SA with SPI. */
7778 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
7783 lck_mtx_unlock(sadb_mutex
);
7784 ipseclog((LOG_DEBUG
, "key_get: no SA found.\n"));
7785 return key_senderror(so
, m
, ENOENT
);
7792 /* map proto to satype */
7793 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
7794 lck_mtx_unlock(sadb_mutex
);
7795 ipseclog((LOG_DEBUG
, "key_get: there was invalid proto in SAD.\n"));
7796 return key_senderror(so
, m
, EINVAL
);
7798 lck_mtx_unlock(sadb_mutex
);
7800 /* create new sadb_msg to reply. */
7801 n
= key_setdumpsa(sav
, SADB_GET
, satype
, mhp
->msg
->sadb_msg_seq
,
7802 mhp
->msg
->sadb_msg_pid
);
7807 return key_senderror(so
, m
, ENOBUFS
);
7810 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
7815 * get SA stats by spi.
7816 * OUT: -1 : not found
7817 * 0 : found, arg pointer to a SA stats is updated.
7820 key_getsastatbyspi_one (u_int32_t spi
,
7821 struct sastat
*stat
)
7823 struct secashead
*sah
;
7824 struct secasvar
*sav
= NULL
;
7826 if ((void *)stat
== NULL
) {
7830 lck_mtx_lock(sadb_mutex
);
7832 /* get a SA header */
7833 LIST_FOREACH(sah
, &sahtree
, chain
) {
7834 if (sah
->state
== SADB_SASTATE_DEAD
)
7837 /* get a SA with SPI. */
7838 sav
= key_getsavbyspi(sah
, spi
);
7840 stat
->spi
= sav
->spi
;
7841 stat
->created
= sav
->created
;
7843 bcopy(sav
->lft_c
,&stat
->lft_c
, sizeof(stat
->lft_c
));
7845 bzero(&stat
->lft_c
, sizeof(stat
->lft_c
));
7847 lck_mtx_unlock(sadb_mutex
);
7852 lck_mtx_unlock(sadb_mutex
);
7858 * get SA stats collection by indices.
7859 * OUT: -1 : not found
7860 * 0 : found, arg pointers to a SA stats and 'maximum stats' are updated.
7863 key_getsastatbyspi (struct sastat
*stat_arg
,
7864 u_int32_t max_stat_arg
,
7865 struct sastat
*stat_res
,
7866 u_int32_t
*max_stat_res
)
7870 if (stat_arg
== NULL
||
7872 max_stat_res
== NULL
) {
7876 for (cur
= 0; cur
< max_stat_arg
; cur
++) {
7877 if (key_getsastatbyspi_one(stat_arg
[cur
].spi
,
7878 &stat_res
[found
]) == 0) {
7882 *max_stat_res
= found
;
7890 /* XXX make it sysctl-configurable? */
7892 key_getcomb_setlifetime(
7893 struct sadb_comb
*comb
)
7896 comb
->sadb_comb_soft_allocations
= 1;
7897 comb
->sadb_comb_hard_allocations
= 1;
7898 comb
->sadb_comb_soft_bytes
= 0;
7899 comb
->sadb_comb_hard_bytes
= 0;
7900 comb
->sadb_comb_hard_addtime
= 86400; /* 1 day */
7901 comb
->sadb_comb_soft_addtime
= comb
->sadb_comb_soft_addtime
* 80 / 100;
7902 comb
->sadb_comb_soft_usetime
= 28800; /* 8 hours */
7903 comb
->sadb_comb_hard_usetime
= comb
->sadb_comb_hard_usetime
* 80 / 100;
7908 * XXX reorder combinations by preference
7909 * XXX no idea if the user wants ESP authentication or not
7911 static struct mbuf
*
7912 key_getcomb_esp(void)
7914 struct sadb_comb
*comb
;
7915 const struct esp_algorithm
*algo
;
7916 struct mbuf
*result
= NULL
, *m
, *n
;
7920 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
7923 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
7924 algo
= esp_algorithm_lookup(i
);
7928 if (algo
->keymax
< ipsec_esp_keymin
)
7930 if (algo
->keymin
< ipsec_esp_keymin
)
7931 encmin
= ipsec_esp_keymin
;
7933 encmin
= algo
->keymin
;
7936 m
= key_getcomb_ah();
7940 panic("assumption failed in key_getcomb_esp");
7942 MGET(m
, M_WAITOK
, MT_DATA
);
7947 bzero(mtod(m
, caddr_t
), m
->m_len
);
7954 for (n
= m
; n
; n
= n
->m_next
)
7958 panic("assumption failed in key_getcomb_esp");
7961 for (off
= 0; off
< totlen
; off
+= l
) {
7962 n
= m_pulldown(m
, off
, l
, &o
);
7964 /* m is already freed */
7967 comb
= (struct sadb_comb
*)
7968 (void *)(mtod(n
, caddr_t
) + o
);
7969 bzero(comb
, sizeof(*comb
));
7970 key_getcomb_setlifetime(comb
);
7971 comb
->sadb_comb_encrypt
= i
;
7972 comb
->sadb_comb_encrypt_minbits
= encmin
;
7973 comb
->sadb_comb_encrypt_maxbits
= algo
->keymax
;
7992 * XXX reorder combinations by preference
7994 static struct mbuf
*
7995 key_getcomb_ah(void)
7997 struct sadb_comb
*comb
;
7998 const struct ah_algorithm
*algo
;
8002 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
8005 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
8007 /* we prefer HMAC algorithms, not old algorithms */
8008 if (i
!= SADB_AALG_SHA1HMAC
&& i
!= SADB_AALG_MD5HMAC
)
8011 algo
= ah_algorithm_lookup(i
);
8015 if (algo
->keymax
< ipsec_ah_keymin
)
8017 if (algo
->keymin
< ipsec_ah_keymin
)
8018 keymin
= ipsec_ah_keymin
;
8020 keymin
= algo
->keymin
;
8025 panic("assumption failed in key_getcomb_ah");
8027 MGET(m
, M_WAITOK
, MT_DATA
);
8034 M_PREPEND(m
, l
, M_WAITOK
, 1);
8038 comb
= mtod(m
, struct sadb_comb
*);
8039 bzero(comb
, sizeof(*comb
));
8040 key_getcomb_setlifetime(comb
);
8041 comb
->sadb_comb_auth
= i
;
8042 comb
->sadb_comb_auth_minbits
= keymin
;
8043 comb
->sadb_comb_auth_maxbits
= algo
->keymax
;
8050 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
8051 * XXX reorder combinations by preference
8053 static struct mbuf
*
8054 key_getcomb_ipcomp(void)
8056 struct sadb_comb
*comb
;
8057 const struct ipcomp_algorithm
*algo
;
8060 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
8063 for (i
= 1; i
<= SADB_X_CALG_MAX
; i
++) {
8064 algo
= ipcomp_algorithm_lookup(i
);
8071 panic("assumption failed in key_getcomb_ipcomp");
8073 MGET(m
, M_WAITOK
, MT_DATA
);
8080 M_PREPEND(m
, l
, M_WAITOK
, 1);
8084 comb
= mtod(m
, struct sadb_comb
*);
8085 bzero(comb
, sizeof(*comb
));
8086 key_getcomb_setlifetime(comb
);
8087 comb
->sadb_comb_encrypt
= i
;
8088 /* what should we set into sadb_comb_*_{min,max}bits? */
8095 * XXX no way to pass mode (transport/tunnel) to userland
8096 * XXX replay checking?
8097 * XXX sysctl interface to ipsec_{ah,esp}_keymin
8099 static struct mbuf
*
8101 const struct secasindex
*saidx
)
8103 struct sadb_prop
*prop
;
8105 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_prop
));
8108 switch (saidx
->proto
) {
8111 m
= key_getcomb_esp();
8115 m
= key_getcomb_ah();
8117 case IPPROTO_IPCOMP
:
8118 m
= key_getcomb_ipcomp();
8126 M_PREPEND(m
, l
, M_WAITOK
, 1);
8131 for (n
= m
; n
; n
= n
->m_next
)
8134 prop
= mtod(m
, struct sadb_prop
*);
8135 bzero(prop
, sizeof(*prop
));
8136 prop
->sadb_prop_len
= PFKEY_UNIT64(totlen
);
8137 prop
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
8138 prop
->sadb_prop_replay
= 32; /* XXX */
8144 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
8146 * <base, SA, address(SD), (address(P)), x_policy,
8147 * (identity(SD),) (sensitivity,) proposal>
8148 * to KMD, and expect to receive
8149 * <base> with SADB_ACQUIRE if error occurred,
8151 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
8152 * from KMD by PF_KEY.
8154 * XXX x_policy is outside of RFC2367 (KAME extension).
8155 * XXX sensitivity is not supported.
8156 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
8157 * see comment for key_getcomb_ipcomp().
8161 * others: error number
8165 struct secasindex
*saidx
,
8166 struct secpolicy
*sp
)
8168 struct mbuf
*result
= NULL
, *m
;
8169 #ifndef IPSEC_NONBLOCK_ACQUIRE
8170 struct secacq
*newacq
;
8176 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
8180 panic("key_acquire: NULL pointer is passed.\n");
8181 if ((satype
= key_proto2satype(saidx
->proto
)) == 0)
8182 panic("key_acquire: invalid proto is passed.\n");
8184 #ifndef IPSEC_NONBLOCK_ACQUIRE
8186 * We never do anything about acquirng SA. There is anather
8187 * solution that kernel blocks to send SADB_ACQUIRE message until
8188 * getting something message from IKEd. In later case, to be
8189 * managed with ACQUIRING list.
8191 /* get a entry to check whether sending message or not. */
8192 lck_mtx_lock(sadb_mutex
);
8193 if ((newacq
= key_getacq(saidx
)) != NULL
) {
8194 if (key_blockacq_count
< newacq
->count
) {
8195 /* reset counter and do send message. */
8198 /* increment counter and do nothing. */
8200 lck_mtx_unlock(sadb_mutex
);
8204 /* make new entry for blocking to send SADB_ACQUIRE. */
8205 if ((newacq
= key_newacq(saidx
)) == NULL
) {
8206 lck_mtx_unlock(sadb_mutex
);
8210 /* add to acqtree */
8211 LIST_INSERT_HEAD(&acqtree
, newacq
, chain
);
8212 key_start_timehandler();
8215 lck_mtx_unlock(sadb_mutex
);
8218 seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
8220 m
= key_setsadbmsg(SADB_ACQUIRE
, 0, satype
, seq
, 0, 0);
8227 /* set sadb_address for saidx's. */
8228 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
8229 (struct sockaddr
*)&saidx
->src
, FULLMASK
, IPSEC_ULPROTO_ANY
);
8236 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
8237 (struct sockaddr
*)&saidx
->dst
, FULLMASK
, IPSEC_ULPROTO_ANY
);
8244 /* XXX proxy address (optional) */
8246 /* set sadb_x_policy */
8248 m
= key_setsadbxpolicy(sp
->policy
, sp
->spidx
.dir
, sp
->id
);
8256 /* XXX identity (optional) */
8258 if (idexttype
&& fqdn
) {
8259 /* create identity extension (FQDN) */
8260 struct sadb_ident
*id
;
8263 fqdnlen
= strlen(fqdn
) + 1; /* +1 for terminating-NUL */
8264 id
= (struct sadb_ident
*)p
;
8265 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
8266 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
8267 id
->sadb_ident_exttype
= idexttype
;
8268 id
->sadb_ident_type
= SADB_IDENTTYPE_FQDN
;
8269 bcopy(fqdn
, id
+ 1, fqdnlen
);
8270 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(fqdnlen
);
8274 /* create identity extension (USERFQDN) */
8275 struct sadb_ident
*id
;
8279 /* +1 for terminating-NUL */
8280 userfqdnlen
= strlen(userfqdn
) + 1;
8283 id
= (struct sadb_ident
*)p
;
8284 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
8285 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
8286 id
->sadb_ident_exttype
= idexttype
;
8287 id
->sadb_ident_type
= SADB_IDENTTYPE_USERFQDN
;
8288 /* XXX is it correct? */
8289 if (curproc
&& curproc
->p_cred
)
8290 id
->sadb_ident_id
= curproc
->p_cred
->p_ruid
;
8291 if (userfqdn
&& userfqdnlen
)
8292 bcopy(userfqdn
, id
+ 1, userfqdnlen
);
8293 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(userfqdnlen
);
8297 /* XXX sensitivity (optional) */
8299 /* create proposal/combination extension */
8300 m
= key_getprop(saidx
);
8303 * spec conformant: always attach proposal/combination extension,
8304 * the problem is that we have no way to attach it for ipcomp,
8305 * due to the way sadb_comb is declared in RFC2367.
8314 * outside of spec; make proposal/combination extension optional.
8320 if ((result
->m_flags
& M_PKTHDR
) == 0) {
8325 if (result
->m_len
< sizeof(struct sadb_msg
)) {
8326 result
= m_pullup(result
, sizeof(struct sadb_msg
));
8327 if (result
== NULL
) {
8333 result
->m_pkthdr
.len
= 0;
8334 for (m
= result
; m
; m
= m
->m_next
)
8335 result
->m_pkthdr
.len
+= m
->m_len
;
8337 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
8338 PFKEY_UNIT64(result
->m_pkthdr
.len
);
8340 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
8348 #ifndef IPSEC_NONBLOCK_ACQUIRE
8349 static struct secacq
*
8351 struct secasindex
*saidx
)
8353 struct secacq
*newacq
;
8357 KMALLOC_NOWAIT(newacq
, struct secacq
*, sizeof(struct secacq
));
8358 if (newacq
== NULL
) {
8359 lck_mtx_unlock(sadb_mutex
);
8360 KMALLOC_WAIT(newacq
, struct secacq
*, sizeof(struct secacq
));
8361 lck_mtx_lock(sadb_mutex
);
8362 if (newacq
== NULL
) {
8363 ipseclog((LOG_DEBUG
, "key_newacq: No more memory.\n"));
8367 bzero(newacq
, sizeof(*newacq
));
8370 bcopy(saidx
, &newacq
->saidx
, sizeof(newacq
->saidx
));
8371 newacq
->seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
);
8373 newacq
->created
= tv
.tv_sec
;
8379 static struct secacq
*
8381 struct secasindex
*saidx
)
8385 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
8387 LIST_FOREACH(acq
, &acqtree
, chain
) {
8388 if (key_cmpsaidx(saidx
, &acq
->saidx
, CMP_EXACTLY
))
8395 static struct secacq
*
8401 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
8403 LIST_FOREACH(acq
, &acqtree
, chain
) {
8404 if (acq
->seq
== seq
)
8412 static struct secspacq
*
8414 struct secpolicyindex
*spidx
)
8416 struct secspacq
*acq
;
8420 KMALLOC_NOWAIT(acq
, struct secspacq
*, sizeof(struct secspacq
));
8422 lck_mtx_unlock(sadb_mutex
);
8423 KMALLOC_WAIT(acq
, struct secspacq
*, sizeof(struct secspacq
));
8424 lck_mtx_lock(sadb_mutex
);
8426 ipseclog((LOG_DEBUG
, "key_newspacq: No more memory.\n"));
8430 bzero(acq
, sizeof(*acq
));
8433 bcopy(spidx
, &acq
->spidx
, sizeof(acq
->spidx
));
8435 acq
->created
= tv
.tv_sec
;
8441 static struct secspacq
*
8443 struct secpolicyindex
*spidx
)
8445 struct secspacq
*acq
;
8447 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
8449 LIST_FOREACH(acq
, &spacqtree
, chain
) {
8450 if (key_cmpspidx_exactly(spidx
, &acq
->spidx
))
8458 * SADB_ACQUIRE processing,
8459 * in first situation, is receiving
8461 * from the ikmpd, and clear sequence of its secasvar entry.
8463 * In second situation, is receiving
8464 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
8465 * from a user land process, and return
8466 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
8469 * m will always be freed.
8475 const struct sadb_msghdr
*mhp
)
8477 const struct sadb_address
*src0
, *dst0
;
8478 ifnet_t ipsec_if
= NULL
;
8479 struct secasindex saidx
;
8480 struct secashead
*sah
;
8486 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
8487 panic("key_acquire2: NULL pointer is passed.\n");
8490 * Error message from KMd.
8491 * We assume that if error was occurred in IKEd, the length of PFKEY
8492 * message is equal to the size of sadb_msg structure.
8493 * We do not raise error even if error occurred in this function.
8495 lck_mtx_lock(sadb_mutex
);
8497 if (mhp
->msg
->sadb_msg_len
== PFKEY_UNIT64(sizeof(struct sadb_msg
))) {
8498 #ifndef IPSEC_NONBLOCK_ACQUIRE
8502 /* check sequence number */
8503 if (mhp
->msg
->sadb_msg_seq
== 0) {
8504 lck_mtx_unlock(sadb_mutex
);
8505 ipseclog((LOG_DEBUG
, "key_acquire2: must specify sequence number.\n"));
8510 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) == NULL
) {
8512 * the specified larval SA is already gone, or we got
8513 * a bogus sequence number. we can silently ignore it.
8515 lck_mtx_unlock(sadb_mutex
);
8520 /* reset acq counter in order to deletion by timehander. */
8522 acq
->created
= tv
.tv_sec
;
8525 lck_mtx_unlock(sadb_mutex
);
8531 * This message is from user land.
8534 /* map satype to proto */
8535 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
8536 lck_mtx_unlock(sadb_mutex
);
8537 ipseclog((LOG_DEBUG
, "key_acquire2: invalid satype is passed.\n"));
8538 return key_senderror(so
, m
, EINVAL
);
8541 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
8542 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
8543 mhp
->ext
[SADB_EXT_PROPOSAL
] == NULL
) {
8545 lck_mtx_unlock(sadb_mutex
);
8546 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
8547 return key_senderror(so
, m
, EINVAL
);
8549 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
8550 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
8551 mhp
->extlen
[SADB_EXT_PROPOSAL
] < sizeof(struct sadb_prop
)) {
8553 lck_mtx_unlock(sadb_mutex
);
8554 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
8555 return key_senderror(so
, m
, EINVAL
);
8558 src0
= (const struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
8559 dst0
= (const struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
8560 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
8562 /* XXX boundary check against sa_len */
8564 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
8566 /* get a SA index */
8567 LIST_FOREACH(sah
, &sahtree
, chain
) {
8568 if (sah
->state
== SADB_SASTATE_DEAD
)
8570 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_MODE
| CMP_REQID
))
8574 lck_mtx_unlock(sadb_mutex
);
8575 ipseclog((LOG_DEBUG
, "key_acquire2: a SA exists already.\n"));
8576 return key_senderror(so
, m
, EEXIST
);
8578 lck_mtx_unlock(sadb_mutex
);
8579 error
= key_acquire(&saidx
, NULL
);
8581 ipseclog((LOG_DEBUG
, "key_acquire2: error %d returned "
8582 "from key_acquire.\n", mhp
->msg
->sadb_msg_errno
));
8583 return key_senderror(so
, m
, error
);
8586 return key_sendup_mbuf(so
, m
, KEY_SENDUP_REGISTERED
);
8590 * SADB_REGISTER processing.
8591 * If SATYPE_UNSPEC has been passed as satype, only return sadb_supported.
8594 * from the ikmpd, and register a socket to send PF_KEY messages,
8598 * If socket is detached, must free from regnode.
8600 * m will always be freed.
8606 const struct sadb_msghdr
*mhp
)
8608 struct secreg
*reg
, *newreg
= 0;
8611 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
8612 panic("key_register: NULL pointer is passed.\n");
8614 /* check for invalid register message */
8615 if (mhp
->msg
->sadb_msg_satype
>= sizeof(regtree
)/sizeof(regtree
[0]))
8616 return key_senderror(so
, m
, EINVAL
);
8618 /* When SATYPE_UNSPEC is specified, only return sadb_supported. */
8619 if (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
)
8622 /* create regnode */
8623 KMALLOC_WAIT(newreg
, struct secreg
*, sizeof(*newreg
));
8624 if (newreg
== NULL
) {
8625 ipseclog((LOG_DEBUG
, "key_register: No more memory.\n"));
8626 return key_senderror(so
, m
, ENOBUFS
);
8628 bzero((caddr_t
)newreg
, sizeof(*newreg
));
8630 lck_mtx_lock(sadb_mutex
);
8631 /* check whether existing or not */
8632 LIST_FOREACH(reg
, ®tree
[mhp
->msg
->sadb_msg_satype
], chain
) {
8633 if (reg
->so
== so
) {
8634 lck_mtx_unlock(sadb_mutex
);
8635 ipseclog((LOG_DEBUG
, "key_register: socket exists already.\n"));
8637 return key_senderror(so
, m
, EEXIST
);
8643 ((struct keycb
*)sotorawcb(so
))->kp_registered
++;
8644 socket_unlock(so
, 1);
8646 /* add regnode to regtree. */
8647 LIST_INSERT_HEAD(®tree
[mhp
->msg
->sadb_msg_satype
], newreg
, chain
);
8648 lck_mtx_unlock(sadb_mutex
);
8652 struct sadb_msg
*newmsg
;
8653 struct sadb_supported
*sup
;
8654 u_int len
, alen
, elen
;
8657 struct sadb_alg
*alg
;
8659 /* create new sadb_msg to reply. */
8661 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
8662 if (ah_algorithm_lookup(i
))
8663 alen
+= sizeof(struct sadb_alg
);
8666 alen
+= sizeof(struct sadb_supported
);
8669 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
8670 if (esp_algorithm_lookup(i
))
8671 elen
+= sizeof(struct sadb_alg
);
8674 elen
+= sizeof(struct sadb_supported
);
8677 len
= sizeof(struct sadb_msg
) + alen
+ elen
;
8680 return key_senderror(so
, m
, ENOBUFS
);
8682 MGETHDR(n
, M_WAITOK
, MT_DATA
);
8683 if (n
&& len
> MHLEN
) {
8684 MCLGET(n
, M_WAITOK
);
8685 if ((n
->m_flags
& M_EXT
) == 0) {
8691 return key_senderror(so
, m
, ENOBUFS
);
8693 n
->m_pkthdr
.len
= n
->m_len
= len
;
8697 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
8698 newmsg
= mtod(n
, struct sadb_msg
*);
8699 newmsg
->sadb_msg_errno
= 0;
8700 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
8701 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
8703 /* for authentication algorithm */
8705 sup
= (struct sadb_supported
*)(void *)(mtod(n
, caddr_t
) + off
);
8706 sup
->sadb_supported_len
= PFKEY_UNIT64(alen
);
8707 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
8708 off
+= PFKEY_ALIGN8(sizeof(*sup
));
8710 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
8711 const struct ah_algorithm
*aalgo
;
8713 aalgo
= ah_algorithm_lookup(i
);
8716 alg
= (struct sadb_alg
*)
8717 (void *)(mtod(n
, caddr_t
) + off
);
8718 alg
->sadb_alg_id
= i
;
8719 alg
->sadb_alg_ivlen
= 0;
8720 alg
->sadb_alg_minbits
= aalgo
->keymin
;
8721 alg
->sadb_alg_maxbits
= aalgo
->keymax
;
8722 off
+= PFKEY_ALIGN8(sizeof(*alg
));
8727 /* for encryption algorithm */
8729 sup
= (struct sadb_supported
*)(void *)(mtod(n
, caddr_t
) + off
);
8730 sup
->sadb_supported_len
= PFKEY_UNIT64(elen
);
8731 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
8732 off
+= PFKEY_ALIGN8(sizeof(*sup
));
8734 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
8735 const struct esp_algorithm
*ealgo
;
8737 ealgo
= esp_algorithm_lookup(i
);
8740 alg
= (struct sadb_alg
*)
8741 (void *)(mtod(n
, caddr_t
) + off
);
8742 alg
->sadb_alg_id
= i
;
8743 if (ealgo
&& ealgo
->ivlen
) {
8745 * give NULL to get the value preferred by
8746 * algorithm XXX SADB_X_EXT_DERIV ?
8748 alg
->sadb_alg_ivlen
=
8749 (*ealgo
->ivlen
)(ealgo
, NULL
);
8751 alg
->sadb_alg_ivlen
= 0;
8752 alg
->sadb_alg_minbits
= ealgo
->keymin
;
8753 alg
->sadb_alg_maxbits
= ealgo
->keymax
;
8754 off
+= PFKEY_ALIGN8(sizeof(struct sadb_alg
));
8761 panic("length assumption failed in key_register");
8765 return key_sendup_mbuf(so
, n
, KEY_SENDUP_REGISTERED
);
8770 key_delete_all_for_socket (struct socket
*so
)
8772 struct secashead
*sah
, *nextsah
;
8773 struct secasvar
*sav
, *nextsav
;
8777 for (sah
= LIST_FIRST(&sahtree
);
8780 nextsah
= LIST_NEXT(sah
, chain
);
8781 for (stateidx
= 0; stateidx
< _ARRAYLEN(saorder_state_alive
); stateidx
++) {
8782 state
= saorder_state_any
[stateidx
];
8783 for (sav
= LIST_FIRST(&sah
->savtree
[state
]); sav
!= NULL
; sav
= nextsav
) {
8784 nextsav
= LIST_NEXT(sav
, chain
);
8785 if (sav
->flags2
& SADB_X_EXT_SA2_DELETE_ON_DETACH
&&
8787 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
8788 key_freesav(sav
, KEY_SADB_LOCKED
);
8796 * free secreg entry registered.
8797 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
8808 panic("key_freereg: NULL pointer is passed.\n");
8811 * check whether existing or not.
8812 * check all type of SA, because there is a potential that
8813 * one socket is registered to multiple type of SA.
8815 lck_mtx_lock(sadb_mutex
);
8816 key_delete_all_for_socket(so
);
8817 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
8818 LIST_FOREACH(reg
, ®tree
[i
], chain
) {
8820 && __LIST_CHAINED(reg
)) {
8821 LIST_REMOVE(reg
, chain
);
8827 lck_mtx_unlock(sadb_mutex
);
8832 * SADB_EXPIRE processing
8834 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
8836 * NOTE: We send only soft lifetime extension.
8839 * others : error number
8843 struct secasvar
*sav
)
8846 struct mbuf
*result
= NULL
, *m
;
8849 struct sadb_lifetime
*lt
;
8851 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
8855 panic("key_expire: NULL pointer is passed.\n");
8856 if (sav
->sah
== NULL
)
8857 panic("key_expire: Why was SA index in SA NULL.\n");
8858 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0)
8859 panic("key_expire: invalid proto is passed.\n");
8861 /* set msg header */
8862 m
= key_setsadbmsg(SADB_EXPIRE
, 0, satype
, sav
->seq
, 0, sav
->refcnt
);
8869 /* create SA extension */
8870 m
= key_setsadbsa(sav
);
8877 /* create SA extension */
8878 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
8879 sav
->replay
? sav
->replay
->count
: 0,
8880 sav
->sah
->saidx
.reqid
,
8888 /* create lifetime extension (current and soft) */
8889 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
8890 m
= key_alloc_mbuf(len
);
8891 if (!m
|| m
->m_next
) { /*XXX*/
8897 bzero(mtod(m
, caddr_t
), len
);
8898 lt
= mtod(m
, struct sadb_lifetime
*);
8899 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
8900 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
8901 lt
->sadb_lifetime_allocations
= sav
->lft_c
->sadb_lifetime_allocations
;
8902 lt
->sadb_lifetime_bytes
= sav
->lft_c
->sadb_lifetime_bytes
;
8903 lt
->sadb_lifetime_addtime
= sav
->lft_c
->sadb_lifetime_addtime
;
8904 lt
->sadb_lifetime_usetime
= sav
->lft_c
->sadb_lifetime_usetime
;
8905 lt
= (struct sadb_lifetime
*)(void *)(mtod(m
, caddr_t
) + len
/ 2);
8906 bcopy(sav
->lft_s
, lt
, sizeof(*lt
));
8909 /* set sadb_address for source */
8910 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
8911 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
8912 FULLMASK
, IPSEC_ULPROTO_ANY
);
8919 /* set sadb_address for destination */
8920 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
8921 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
8922 FULLMASK
, IPSEC_ULPROTO_ANY
);
8929 if ((result
->m_flags
& M_PKTHDR
) == 0) {
8934 if (result
->m_len
< sizeof(struct sadb_msg
)) {
8935 result
= m_pullup(result
, sizeof(struct sadb_msg
));
8936 if (result
== NULL
) {
8942 result
->m_pkthdr
.len
= 0;
8943 for (m
= result
; m
; m
= m
->m_next
)
8944 result
->m_pkthdr
.len
+= m
->m_len
;
8946 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
8947 PFKEY_UNIT64(result
->m_pkthdr
.len
);
8949 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
8958 * SADB_FLUSH processing
8961 * from the ikmpd, and free all entries in secastree.
8965 * NOTE: to do is only marking SADB_SASTATE_DEAD.
8967 * m will always be freed.
8973 const struct sadb_msghdr
*mhp
)
8975 struct sadb_msg
*newmsg
;
8976 struct secashead
*sah
, *nextsah
;
8977 struct secasvar
*sav
, *nextsav
;
8983 if (so
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
8984 panic("key_flush: NULL pointer is passed.\n");
8986 /* map satype to proto */
8987 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
8988 ipseclog((LOG_DEBUG
, "key_flush: invalid satype is passed.\n"));
8989 return key_senderror(so
, m
, EINVAL
);
8992 lck_mtx_lock(sadb_mutex
);
8994 /* no SATYPE specified, i.e. flushing all SA. */
8995 for (sah
= LIST_FIRST(&sahtree
);
8998 nextsah
= LIST_NEXT(sah
, chain
);
9000 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
9001 && proto
!= sah
->saidx
.proto
)
9005 stateidx
< _ARRAYLEN(saorder_state_alive
);
9007 state
= saorder_state_any
[stateidx
];
9008 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
9012 nextsav
= LIST_NEXT(sav
, chain
);
9014 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
9015 key_freesav(sav
, KEY_SADB_LOCKED
);
9019 sah
->state
= SADB_SASTATE_DEAD
;
9021 lck_mtx_unlock(sadb_mutex
);
9023 if (m
->m_len
< sizeof(struct sadb_msg
) ||
9024 sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
9025 ipseclog((LOG_DEBUG
, "key_flush: No more memory.\n"));
9026 return key_senderror(so
, m
, ENOBUFS
);
9032 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct sadb_msg
);
9033 newmsg
= mtod(m
, struct sadb_msg
*);
9034 newmsg
->sadb_msg_errno
= 0;
9035 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
9037 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
9041 * SADB_DUMP processing
9042 * dump all entries including status of DEAD in SAD.
9045 * from the ikmpd, and dump all secasvar leaves
9050 * m will always be freed.
9053 struct sav_dump_elem
{
9054 struct secasvar
*sav
;
9062 const struct sadb_msghdr
*mhp
)
9064 struct secashead
*sah
;
9065 struct secasvar
*sav
;
9066 struct sav_dump_elem
*savbuf
= NULL
, *elem_ptr
;
9071 int cnt
= 0, cnt2
, bufcount
;
9075 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
9078 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
9079 panic("key_dump: NULL pointer is passed.\n");
9081 /* map satype to proto */
9082 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
9083 ipseclog((LOG_DEBUG
, "key_dump: invalid satype is passed.\n"));
9084 return key_senderror(so
, m
, EINVAL
);
9087 if ((bufcount
= ipsec_sav_count
) <= 0) {
9091 bufcount
+= 512; /* extra */
9092 KMALLOC_WAIT(savbuf
, struct sav_dump_elem
*, bufcount
* sizeof(struct sav_dump_elem
));
9093 if (savbuf
== NULL
) {
9094 ipseclog((LOG_DEBUG
, "key_dump: No more memory.\n"));
9099 /* count sav entries to be sent to the userland. */
9100 lck_mtx_lock(sadb_mutex
);
9102 LIST_FOREACH(sah
, &sahtree
, chain
) {
9103 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
9104 && proto
!= sah
->saidx
.proto
)
9107 /* map proto to satype */
9108 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
9109 lck_mtx_unlock(sadb_mutex
);
9110 ipseclog((LOG_DEBUG
, "key_dump: there was invalid proto in SAD.\n"));
9116 stateidx
< _ARRAYLEN(saorder_state_any
);
9118 state
= saorder_state_any
[stateidx
];
9119 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
9120 if (cnt
== bufcount
)
9121 break; /* out of buffer space */
9122 elem_ptr
->sav
= sav
;
9123 elem_ptr
->satype
= satype
;
9130 lck_mtx_unlock(sadb_mutex
);
9137 /* send this to the userland, one at a time. */
9141 n
= key_setdumpsa(elem_ptr
->sav
, SADB_DUMP
, elem_ptr
->satype
,
9142 --cnt2
, mhp
->msg
->sadb_msg_pid
);
9149 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
9157 lck_mtx_lock(sadb_mutex
);
9159 key_freesav((elem_ptr
++)->sav
, KEY_SADB_LOCKED
);
9160 lck_mtx_unlock(sadb_mutex
);
9166 return key_senderror(so
, m
, error
);
9173 * SADB_X_PROMISC processing
9175 * m will always be freed.
9181 const struct sadb_msghdr
*mhp
)
9186 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
9187 panic("key_promisc: NULL pointer is passed.\n");
9189 olen
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
9191 if (olen
< sizeof(struct sadb_msg
)) {
9193 return key_senderror(so
, m
, EINVAL
);
9198 } else if (olen
== sizeof(struct sadb_msg
)) {
9199 /* enable/disable promisc mode */
9203 if ((kp
= (struct keycb
*)sotorawcb(so
)) == NULL
)
9204 return key_senderror(so
, m
, EINVAL
);
9205 mhp
->msg
->sadb_msg_errno
= 0;
9206 switch (mhp
->msg
->sadb_msg_satype
) {
9209 kp
->kp_promisc
= mhp
->msg
->sadb_msg_satype
;
9212 socket_unlock(so
, 1);
9213 return key_senderror(so
, m
, EINVAL
);
9215 socket_unlock(so
, 1);
9217 /* send the original message back to everyone */
9218 mhp
->msg
->sadb_msg_errno
= 0;
9219 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
9221 /* send packet as is */
9223 m_adj(m
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)));
9225 /* TODO: if sadb_msg_seq is specified, send to specific pid */
9226 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
9230 static int (*key_typesw
[])(struct socket
*, struct mbuf
*,
9231 const struct sadb_msghdr
*) = {
9232 NULL
, /* SADB_RESERVED */
9233 key_getspi
, /* SADB_GETSPI */
9234 key_update
, /* SADB_UPDATE */
9235 key_add
, /* SADB_ADD */
9236 key_delete
, /* SADB_DELETE */
9237 key_get
, /* SADB_GET */
9238 key_acquire2
, /* SADB_ACQUIRE */
9239 key_register
, /* SADB_REGISTER */
9240 NULL
, /* SADB_EXPIRE */
9241 key_flush
, /* SADB_FLUSH */
9242 key_dump
, /* SADB_DUMP */
9243 key_promisc
, /* SADB_X_PROMISC */
9244 NULL
, /* SADB_X_PCHANGE */
9245 key_spdadd
, /* SADB_X_SPDUPDATE */
9246 key_spdadd
, /* SADB_X_SPDADD */
9247 key_spddelete
, /* SADB_X_SPDDELETE */
9248 key_spdget
, /* SADB_X_SPDGET */
9249 NULL
, /* SADB_X_SPDACQUIRE */
9250 key_spddump
, /* SADB_X_SPDDUMP */
9251 key_spdflush
, /* SADB_X_SPDFLUSH */
9252 key_spdadd
, /* SADB_X_SPDSETIDX */
9253 NULL
, /* SADB_X_SPDEXPIRE */
9254 key_spddelete2
, /* SADB_X_SPDDELETE2 */
9255 key_getsastat
, /* SADB_GETSASTAT */
9256 key_spdenable
, /* SADB_X_SPDENABLE */
9257 key_spddisable
, /* SADB_X_SPDDISABLE */
9258 key_migrate
, /* SADB_MIGRATE */
9262 bzero_mbuf(struct mbuf
*m
)
9264 struct mbuf
*mptr
= m
;
9265 struct sadb_msg
*msg
= NULL
;
9272 if (mptr
->m_len
>= sizeof(struct sadb_msg
)) {
9273 msg
= mtod(mptr
, struct sadb_msg
*);
9274 if (msg
->sadb_msg_type
!= SADB_ADD
&&
9275 msg
->sadb_msg_type
!= SADB_UPDATE
) {
9278 offset
= sizeof(struct sadb_msg
);
9280 bzero(mptr
->m_data
+offset
, mptr
->m_len
-offset
);
9281 mptr
= mptr
->m_next
;
9282 while (mptr
!= NULL
) {
9283 bzero(mptr
->m_data
, mptr
->m_len
);
9284 mptr
= mptr
->m_next
;
9289 bzero_keys(const struct sadb_msghdr
*mh
)
9297 offset
= sizeof(struct sadb_key
);
9299 if (mh
->ext
[SADB_EXT_KEY_ENCRYPT
]) {
9300 struct sadb_key
*key
= (struct sadb_key
*)mh
->ext
[SADB_EXT_KEY_ENCRYPT
];
9301 extlen
= key
->sadb_key_bits
>> 3;
9303 if (mh
->extlen
[SADB_EXT_KEY_ENCRYPT
] >= offset
+ extlen
) {
9304 bzero((uint8_t *)mh
->ext
[SADB_EXT_KEY_ENCRYPT
]+offset
, extlen
);
9306 bzero(mh
->ext
[SADB_EXT_KEY_ENCRYPT
], mh
->extlen
[SADB_EXT_KEY_ENCRYPT
]);
9309 if (mh
->ext
[SADB_EXT_KEY_AUTH
]) {
9310 struct sadb_key
*key
= (struct sadb_key
*)mh
->ext
[SADB_EXT_KEY_AUTH
];
9311 extlen
= key
->sadb_key_bits
>> 3;
9313 if (mh
->extlen
[SADB_EXT_KEY_AUTH
] >= offset
+ extlen
) {
9314 bzero((uint8_t *)mh
->ext
[SADB_EXT_KEY_AUTH
]+offset
, extlen
);
9316 bzero(mh
->ext
[SADB_EXT_KEY_AUTH
], mh
->extlen
[SADB_EXT_KEY_AUTH
]);
9322 key_validate_address_pair(struct sadb_address
*src0
,
9323 struct sadb_address
*dst0
)
9327 /* check upper layer protocol */
9328 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
9329 ipseclog((LOG_DEBUG
, "key_parse: upper layer protocol mismatched.\n"));
9330 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9335 if (PFKEY_ADDR_SADDR(src0
)->sa_family
!=
9336 PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
9337 ipseclog((LOG_DEBUG
, "key_parse: address family mismatched.\n"));
9338 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9341 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
9342 PFKEY_ADDR_SADDR(dst0
)->sa_len
) {
9343 ipseclog((LOG_DEBUG
,
9344 "key_parse: address struct size mismatched.\n"));
9345 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9349 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
9351 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!= sizeof(struct sockaddr_in
)) {
9352 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9357 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!= sizeof(struct sockaddr_in6
)) {
9358 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9363 ipseclog((LOG_DEBUG
,
9364 "key_parse: unsupported address family.\n"));
9365 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9366 return (EAFNOSUPPORT
);
9369 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
9371 plen
= sizeof(struct in_addr
) << 3;
9374 plen
= sizeof(struct in6_addr
) << 3;
9377 plen
= 0; /*fool gcc*/
9381 /* check max prefix length */
9382 if (src0
->sadb_address_prefixlen
> plen
||
9383 dst0
->sadb_address_prefixlen
> plen
) {
9384 ipseclog((LOG_DEBUG
,
9385 "key_parse: illegal prefixlen.\n"));
9386 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9391 * prefixlen == 0 is valid because there can be a case when
9392 * all addresses are matched.
9398 * parse sadb_msg buffer to process PFKEYv2,
9399 * and create a data to response if needed.
9400 * I think to be dealed with mbuf directly.
9402 * msgp : pointer to pointer to a received buffer pulluped.
9403 * This is rewrited to response.
9404 * so : pointer to socket.
9406 * length for buffer to send to user process.
9413 struct sadb_msg
*msg
;
9414 struct sadb_msghdr mh
;
9418 Boolean keyAligned
= FALSE
;
9420 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
9423 if (m
== NULL
|| so
== NULL
)
9424 panic("key_parse: NULL pointer is passed.\n");
9426 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
9427 KEYDEBUG(KEYDEBUG_KEY_DUMP
,
9428 ipseclog((LOG_DEBUG
, "key_parse: passed sadb_msg\n"));
9432 if (m
->m_len
< sizeof(struct sadb_msg
)) {
9433 m
= m_pullup(m
, sizeof(struct sadb_msg
));
9437 msg
= mtod(m
, struct sadb_msg
*);
9438 orglen
= PFKEY_UNUNIT64(msg
->sadb_msg_len
);
9439 target
= KEY_SENDUP_ONE
;
9441 if ((m
->m_flags
& M_PKTHDR
) == 0 ||
9442 m
->m_pkthdr
.len
!= m
->m_pkthdr
.len
) {
9443 ipseclog((LOG_DEBUG
, "key_parse: invalid message length.\n"));
9444 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
9449 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
9450 ipseclog((LOG_DEBUG
,
9451 "key_parse: PF_KEY version %u is mismatched.\n",
9452 msg
->sadb_msg_version
));
9453 PFKEY_STAT_INCREMENT(pfkeystat
.out_invver
);
9458 if (msg
->sadb_msg_type
> SADB_MAX
) {
9459 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
9460 msg
->sadb_msg_type
));
9461 PFKEY_STAT_INCREMENT(pfkeystat
.out_invmsgtype
);
9466 /* for old-fashioned code - should be nuked */
9467 if (m
->m_pkthdr
.len
> MCLBYTES
) {
9474 MGETHDR(n
, M_WAITOK
, MT_DATA
);
9475 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
9476 MCLGET(n
, M_WAITOK
);
9477 if ((n
->m_flags
& M_EXT
) == 0) {
9487 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
9488 n
->m_pkthdr
.len
= n
->m_len
= m
->m_pkthdr
.len
;
9495 /* align the mbuf chain so that extensions are in contiguous region. */
9496 error
= key_align(m
, &mh
);
9500 if (m
->m_next
) { /*XXX*/
9510 switch (msg
->sadb_msg_satype
) {
9511 case SADB_SATYPE_UNSPEC
:
9512 switch (msg
->sadb_msg_type
) {
9520 ipseclog((LOG_DEBUG
, "key_parse: must specify satype "
9521 "when msg type=%u.\n", msg
->sadb_msg_type
));
9522 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
9527 case SADB_SATYPE_AH
:
9528 case SADB_SATYPE_ESP
:
9529 case SADB_X_SATYPE_IPCOMP
:
9530 switch (msg
->sadb_msg_type
) {
9532 case SADB_X_SPDDELETE
:
9534 case SADB_X_SPDDUMP
:
9535 case SADB_X_SPDFLUSH
:
9536 case SADB_X_SPDSETIDX
:
9537 case SADB_X_SPDUPDATE
:
9538 case SADB_X_SPDDELETE2
:
9539 case SADB_X_SPDENABLE
:
9540 case SADB_X_SPDDISABLE
:
9541 ipseclog((LOG_DEBUG
, "key_parse: illegal satype=%u\n",
9542 msg
->sadb_msg_type
));
9543 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
9548 case SADB_SATYPE_RSVP
:
9549 case SADB_SATYPE_OSPFV2
:
9550 case SADB_SATYPE_RIPV2
:
9551 case SADB_SATYPE_MIP
:
9552 ipseclog((LOG_DEBUG
, "key_parse: type %u isn't supported.\n",
9553 msg
->sadb_msg_satype
));
9554 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
9557 case 1: /* XXX: What does it do? */
9558 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
9562 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
9563 msg
->sadb_msg_satype
));
9564 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
9569 /* Validate address fields for matching families, lengths, etc. */
9570 void *src0
= mh
.ext
[SADB_EXT_ADDRESS_SRC
];
9571 void *dst0
= mh
.ext
[SADB_EXT_ADDRESS_DST
];
9572 if (mh
.ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
] != NULL
&&
9573 mh
.ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
] != NULL
) {
9575 error
= key_validate_address_pair((struct sadb_address
*)(mh
.ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
]),
9576 (struct sadb_address
*)(mh
.ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
]));
9582 src0
= mh
.ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
];
9585 if (mh
.ext
[SADB_X_EXT_ADDR_RANGE_DST_START
] != NULL
&&
9586 mh
.ext
[SADB_X_EXT_ADDR_RANGE_DST_END
] != NULL
) {
9588 error
= key_validate_address_pair((struct sadb_address
*)(mh
.ext
[SADB_X_EXT_ADDR_RANGE_DST_START
]),
9589 (struct sadb_address
*)(mh
.ext
[SADB_X_EXT_ADDR_RANGE_DST_END
]));
9595 dst0
= mh
.ext
[SADB_X_EXT_ADDR_RANGE_DST_START
];
9598 if (src0
!= NULL
&& dst0
!= NULL
) {
9599 error
= key_validate_address_pair((struct sadb_address
*)(src0
),
9600 (struct sadb_address
*)(dst0
));
9606 if (msg
->sadb_msg_type
>= sizeof(key_typesw
)/sizeof(key_typesw
[0]) ||
9607 key_typesw
[msg
->sadb_msg_type
] == NULL
) {
9608 PFKEY_STAT_INCREMENT(pfkeystat
.out_invmsgtype
);
9613 error
= (*key_typesw
[msg
->sadb_msg_type
])(so
, m
, &mh
);
9623 msg
->sadb_msg_errno
= error
;
9624 return key_sendup_mbuf(so
, m
, target
);
9633 struct sadb_msg
*msg
;
9635 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
9637 if (m
->m_len
< sizeof(struct sadb_msg
))
9638 panic("invalid mbuf passed to key_senderror");
9640 msg
= mtod(m
, struct sadb_msg
*);
9641 msg
->sadb_msg_errno
= code
;
9642 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
9646 * set the pointer to each header into message buffer.
9647 * m will be freed on error.
9648 * XXX larger-than-MCLBYTES extension?
9653 struct sadb_msghdr
*mhp
)
9656 struct sadb_ext
*ext
;
9662 if (m
== NULL
|| mhp
== NULL
)
9663 panic("key_align: NULL pointer is passed.\n");
9664 if (m
->m_len
< sizeof(struct sadb_msg
))
9665 panic("invalid mbuf passed to key_align");
9668 bzero(mhp
, sizeof(*mhp
));
9670 mhp
->msg
= mtod(m
, struct sadb_msg
*);
9671 mhp
->ext
[0] = (struct sadb_ext
*)mhp
->msg
; /*XXX backward compat */
9673 end
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
9674 extlen
= end
; /*just in case extlen is not updated*/
9675 for (off
= sizeof(struct sadb_msg
); off
< end
; off
+= extlen
) {
9676 n
= m_pulldown(m
, off
, sizeof(struct sadb_ext
), &toff
);
9678 /* m is already freed */
9681 ext
= (struct sadb_ext
*)(void *)(mtod(n
, caddr_t
) + toff
);
9684 switch (ext
->sadb_ext_type
) {
9686 case SADB_EXT_ADDRESS_SRC
:
9687 case SADB_EXT_ADDRESS_DST
:
9688 case SADB_EXT_ADDRESS_PROXY
:
9689 case SADB_EXT_LIFETIME_CURRENT
:
9690 case SADB_EXT_LIFETIME_HARD
:
9691 case SADB_EXT_LIFETIME_SOFT
:
9692 case SADB_EXT_KEY_AUTH
:
9693 case SADB_EXT_KEY_ENCRYPT
:
9694 case SADB_EXT_IDENTITY_SRC
:
9695 case SADB_EXT_IDENTITY_DST
:
9696 case SADB_EXT_SENSITIVITY
:
9697 case SADB_EXT_PROPOSAL
:
9698 case SADB_EXT_SUPPORTED_AUTH
:
9699 case SADB_EXT_SUPPORTED_ENCRYPT
:
9700 case SADB_EXT_SPIRANGE
:
9701 case SADB_X_EXT_POLICY
:
9702 case SADB_X_EXT_SA2
:
9703 case SADB_EXT_SESSION_ID
:
9704 case SADB_EXT_SASTAT
:
9705 case SADB_X_EXT_IPSECIF
:
9706 case SADB_X_EXT_ADDR_RANGE_SRC_START
:
9707 case SADB_X_EXT_ADDR_RANGE_SRC_END
:
9708 case SADB_X_EXT_ADDR_RANGE_DST_START
:
9709 case SADB_X_EXT_ADDR_RANGE_DST_END
:
9710 case SADB_EXT_MIGRATE_ADDRESS_SRC
:
9711 case SADB_EXT_MIGRATE_ADDRESS_DST
:
9712 case SADB_X_EXT_MIGRATE_IPSECIF
:
9713 /* duplicate check */
9715 * XXX Are there duplication payloads of either
9716 * KEY_AUTH or KEY_ENCRYPT ?
9718 if (mhp
->ext
[ext
->sadb_ext_type
] != NULL
) {
9719 ipseclog((LOG_DEBUG
,
9720 "key_align: duplicate ext_type %u "
9721 "is passed.\n", ext
->sadb_ext_type
));
9724 PFKEY_STAT_INCREMENT(pfkeystat
.out_dupext
);
9729 ipseclog((LOG_DEBUG
,
9730 "key_align: invalid ext_type %u is passed.\n",
9731 ext
->sadb_ext_type
));
9734 PFKEY_STAT_INCREMENT(pfkeystat
.out_invexttype
);
9738 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
9740 if (key_validate_ext(ext
, extlen
)) {
9743 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
9747 n
= m_pulldown(m
, off
, extlen
, &toff
);
9749 /* m is already freed */
9752 ext
= (struct sadb_ext
*)(void *)(mtod(n
, caddr_t
) + toff
);
9754 mhp
->ext
[ext
->sadb_ext_type
] = ext
;
9755 mhp
->extoff
[ext
->sadb_ext_type
] = off
;
9756 mhp
->extlen
[ext
->sadb_ext_type
] = extlen
;
9762 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
9771 const struct sadb_ext
*ext
,
9774 struct sockaddr
*sa
;
9775 enum { NONE
, ADDR
} checktype
= NONE
;
9777 const int sal
= offsetof(struct sockaddr
, sa_len
) + sizeof(sa
->sa_len
);
9779 if (len
!= PFKEY_UNUNIT64(ext
->sadb_ext_len
))
9782 /* if it does not match minimum/maximum length, bail */
9783 if (ext
->sadb_ext_type
>= sizeof(minsize
) / sizeof(minsize
[0]) ||
9784 ext
->sadb_ext_type
>= sizeof(maxsize
) / sizeof(maxsize
[0]))
9786 if (!minsize
[ext
->sadb_ext_type
] || len
< minsize
[ext
->sadb_ext_type
])
9788 if (maxsize
[ext
->sadb_ext_type
] && len
> maxsize
[ext
->sadb_ext_type
])
9791 /* more checks based on sadb_ext_type XXX need more */
9792 switch (ext
->sadb_ext_type
) {
9793 case SADB_EXT_ADDRESS_SRC
:
9794 case SADB_EXT_ADDRESS_DST
:
9795 case SADB_EXT_ADDRESS_PROXY
:
9796 case SADB_X_EXT_ADDR_RANGE_SRC_START
:
9797 case SADB_X_EXT_ADDR_RANGE_SRC_END
:
9798 case SADB_X_EXT_ADDR_RANGE_DST_START
:
9799 case SADB_X_EXT_ADDR_RANGE_DST_END
:
9800 case SADB_EXT_MIGRATE_ADDRESS_SRC
:
9801 case SADB_EXT_MIGRATE_ADDRESS_DST
:
9802 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_address
));
9805 case SADB_EXT_IDENTITY_SRC
:
9806 case SADB_EXT_IDENTITY_DST
:
9807 if (((struct sadb_ident
*)(uintptr_t)(size_t)ext
)->
9808 sadb_ident_type
== SADB_X_IDENTTYPE_ADDR
) {
9809 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_ident
));
9819 switch (checktype
) {
9823 sa
= (struct sockaddr
*)((caddr_t
)(uintptr_t)ext
+ baselen
);
9825 if (len
< baselen
+ sal
)
9827 if (baselen
+ PFKEY_ALIGN8(sa
->sa_len
) != len
)
9836 * XXX: maybe This function is called after INBOUND IPsec processing.
9838 * Special check for tunnel-mode packets.
9839 * We must make some checks for consistency between inner and outer IP header.
9841 * xxx more checks to be provided
9844 key_checktunnelsanity(
9845 struct secasvar
*sav
,
9846 __unused u_int family
,
9847 __unused caddr_t src
,
9848 __unused caddr_t dst
)
9852 if (sav
->sah
== NULL
)
9853 panic("sav->sah == NULL at key_checktunnelsanity");
9855 /* XXX: check inner IP header */
9860 /* record data transfer on SA, and update timestamps */
9863 struct secasvar
*sav
,
9869 panic("key_sa_recordxfer called with sav == NULL");
9871 panic("key_sa_recordxfer called with m == NULL");
9875 lck_mtx_lock(sadb_mutex
);
9877 * XXX Currently, there is a difference of bytes size
9878 * between inbound and outbound processing.
9880 sav
->lft_c
->sadb_lifetime_bytes
+= m
->m_pkthdr
.len
;
9881 /* to check bytes lifetime is done in key_timehandler(). */
9884 * We use the number of packets as the unit of
9885 * sadb_lifetime_allocations. We increment the variable
9886 * whenever {esp,ah}_{in,out}put is called.
9888 sav
->lft_c
->sadb_lifetime_allocations
++;
9889 /* XXX check for expires? */
9892 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
9893 * in seconds. HARD and SOFT lifetime are measured by the time
9894 * difference (again in seconds) from sadb_lifetime_usetime.
9898 * -----+-----+--------+---> t
9899 * <--------------> HARD
9905 sav
->lft_c
->sadb_lifetime_usetime
= tv
.tv_sec
;
9906 /* XXX check for expires? */
9908 lck_mtx_unlock(sadb_mutex
);
9916 struct sockaddr
*dst
)
9918 struct secashead
*sah
;
9921 lck_mtx_lock(sadb_mutex
);
9922 LIST_FOREACH(sah
, &sahtree
, chain
) {
9923 ro
= (struct route
*)&sah
->sa_route
;
9924 if (ro
->ro_rt
&& dst
->sa_len
== ro
->ro_dst
.sa_len
9925 && bcmp(dst
, &ro
->ro_dst
, dst
->sa_len
) == 0) {
9929 lck_mtx_unlock(sadb_mutex
);
9936 struct secasvar
*sav
,
9941 panic("key_sa_chgstate called with sav == NULL");
9943 if (sav
->state
== state
)
9946 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
9948 if (__LIST_CHAINED(sav
))
9949 LIST_REMOVE(sav
, chain
);
9952 LIST_INSERT_HEAD(&sav
->sah
->savtree
[state
], sav
, chain
);
9958 struct secasvar
*sav
)
9960 lck_mtx_lock(sadb_mutex
);
9962 panic("key_sa_stir_iv called with sav == NULL");
9963 key_randomfill(sav
->iv
, sav
->ivlen
);
9964 lck_mtx_unlock(sadb_mutex
);
9968 static struct mbuf
*
9972 struct mbuf
*m
= NULL
, *n
;
9977 MGET(n
, M_DONTWAIT
, MT_DATA
);
9978 if (n
&& len
> MLEN
)
9979 MCLGET(n
, M_DONTWAIT
);
9987 n
->m_len
= M_TRAILINGSPACE(n
);
9988 /* use the bottom of mbuf, hoping we can prepend afterwards */
9989 if (n
->m_len
> len
) {
9990 t
= (n
->m_len
- len
) & ~(sizeof(long) - 1);
10006 static struct mbuf
*
10007 key_setdumpsastats (u_int32_t dir
,
10008 struct sastat
*stats
,
10009 u_int32_t max_stats
,
10010 u_int64_t session_ids
[],
10014 struct mbuf
*result
= NULL
, *m
= NULL
;
10016 m
= key_setsadbmsg(SADB_GETSASTAT
, 0, 0, seq
, pid
, 0);
10022 m
= key_setsadbsession_id(session_ids
);
10028 m
= key_setsadbsastat(dir
,
10036 if ((result
->m_flags
& M_PKTHDR
) == 0) {
10040 if (result
->m_len
< sizeof(struct sadb_msg
)) {
10041 result
= m_pullup(result
, sizeof(struct sadb_msg
));
10042 if (result
== NULL
) {
10047 result
->m_pkthdr
.len
= 0;
10048 for (m
= result
; m
; m
= m
->m_next
) {
10049 result
->m_pkthdr
.len
+= m
->m_len
;
10052 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
10053 PFKEY_UNIT64(result
->m_pkthdr
.len
);
10065 * SADB_GETSASTAT processing
10066 * dump all stats for matching entries in SAD.
10068 * m will always be freed.
10072 key_getsastat (struct socket
*so
,
10074 const struct sadb_msghdr
*mhp
)
10076 struct sadb_session_id
*session_id
;
10077 u_int32_t bufsize
, arg_count
, res_count
;
10078 struct sadb_sastat
*sa_stats_arg
;
10079 struct sastat
*sa_stats_sav
= NULL
;
10084 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
10085 panic("%s: NULL pointer is passed.\n", __FUNCTION__
);
10087 if (mhp
->ext
[SADB_EXT_SESSION_ID
] == NULL
) {
10088 printf("%s: invalid message is passed. missing session-id.\n", __FUNCTION__
);
10089 return key_senderror(so
, m
, EINVAL
);
10091 if (mhp
->extlen
[SADB_EXT_SESSION_ID
] < sizeof(struct sadb_session_id
)) {
10092 printf("%s: invalid message is passed. short session-id.\n", __FUNCTION__
);
10093 return key_senderror(so
, m
, EINVAL
);
10095 if (mhp
->ext
[SADB_EXT_SASTAT
] == NULL
) {
10096 printf("%s: invalid message is passed. missing stat args.\n", __FUNCTION__
);
10097 return key_senderror(so
, m
, EINVAL
);
10099 if (mhp
->extlen
[SADB_EXT_SASTAT
] < sizeof(*sa_stats_arg
)) {
10100 printf("%s: invalid message is passed. short stat args.\n", __FUNCTION__
);
10101 return key_senderror(so
, m
, EINVAL
);
10104 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
10106 // exit early if there are no active SAs
10107 if (ipsec_sav_count
<= 0) {
10108 printf("%s: No active SAs.\n", __FUNCTION__
);
10112 bufsize
= (ipsec_sav_count
+ 1) * sizeof(*sa_stats_sav
);
10114 KMALLOC_WAIT(sa_stats_sav
, __typeof__(sa_stats_sav
), bufsize
);
10115 if (sa_stats_sav
== NULL
) {
10116 printf("%s: No more memory.\n", __FUNCTION__
);
10120 bzero(sa_stats_sav
, bufsize
);
10122 sa_stats_arg
= (__typeof__(sa_stats_arg
))
10123 (void *)mhp
->ext
[SADB_EXT_SASTAT
];
10124 arg_count
= sa_stats_arg
->sadb_sastat_list_len
;
10125 // exit early if there are no requested SAs
10126 if (arg_count
== 0) {
10127 printf("%s: No SAs requested.\n", __FUNCTION__
);
10133 if (key_getsastatbyspi((struct sastat
*)(sa_stats_arg
+ 1),
10137 printf("%s: Error finding SAs.\n", __FUNCTION__
);
10142 printf("%s: No SAs found.\n", __FUNCTION__
);
10147 session_id
= (__typeof__(session_id
))
10148 (void *)mhp
->ext
[SADB_EXT_SESSION_ID
];
10150 /* send this to the userland. */
10151 n
= key_setdumpsastats(sa_stats_arg
->sadb_sastat_dir
,
10154 session_id
->sadb_session_id_v
,
10155 mhp
->msg
->sadb_msg_seq
,
10156 mhp
->msg
->sadb_msg_pid
);
10158 printf("%s: No bufs to dump stats.\n", __FUNCTION__
);
10163 key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
10165 if (sa_stats_sav
) {
10166 KFREE(sa_stats_sav
);
10170 return key_senderror(so
, m
, error
);
10177 key_update_natt_keepalive_timestamp (struct secasvar
*sav_sent
,
10178 struct secasvar
*sav_update
)
10180 struct secasindex saidx_swap_sent_addr
;
10182 // exit early if two SAs are identical, or if sav_update is current
10183 if (sav_sent
== sav_update
||
10184 sav_update
->natt_last_activity
== natt_now
) {
10188 // assuming that (sav_update->remote_ike_port != 0 && (esp_udp_encap_port & 0xFFFF) != 0)
10190 bzero(&saidx_swap_sent_addr
, sizeof(saidx_swap_sent_addr
));
10191 memcpy(&saidx_swap_sent_addr
.src
, &sav_sent
->sah
->saidx
.dst
, sizeof(saidx_swap_sent_addr
.src
));
10192 memcpy(&saidx_swap_sent_addr
.dst
, &sav_sent
->sah
->saidx
.src
, sizeof(saidx_swap_sent_addr
.dst
));
10193 saidx_swap_sent_addr
.proto
= sav_sent
->sah
->saidx
.proto
;
10194 saidx_swap_sent_addr
.mode
= sav_sent
->sah
->saidx
.mode
;
10195 // we ignore reqid for split-tunnel setups
10197 if (key_cmpsaidx(&sav_sent
->sah
->saidx
, &sav_update
->sah
->saidx
, CMP_MODE
| CMP_PORT
) ||
10198 key_cmpsaidx(&saidx_swap_sent_addr
, &sav_update
->sah
->saidx
, CMP_MODE
| CMP_PORT
)) {
10199 sav_update
->natt_last_activity
= natt_now
;
10204 key_send_delsp (struct secpolicy
*sp
)
10206 struct mbuf
*result
= NULL
, *m
;
10211 /* set msg header */
10212 m
= key_setsadbmsg(SADB_X_SPDDELETE
, 0, 0, 0, 0, 0);
10218 /* set sadb_address(es) for source */
10219 if (sp
->spidx
.src_range
.start
.ss_len
> 0) {
10220 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_START
,
10221 (struct sockaddr
*)&sp
->spidx
.src_range
.start
, sp
->spidx
.prefs
,
10222 sp
->spidx
.ul_proto
);
10227 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_END
,
10228 (struct sockaddr
*)&sp
->spidx
.src_range
.end
, sp
->spidx
.prefs
,
10229 sp
->spidx
.ul_proto
);
10234 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
10235 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
10236 sp
->spidx
.ul_proto
);
10242 /* set sadb_address(es) for destination */
10243 if (sp
->spidx
.dst_range
.start
.ss_len
> 0) {
10244 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_START
,
10245 (struct sockaddr
*)&sp
->spidx
.dst_range
.start
, sp
->spidx
.prefd
,
10246 sp
->spidx
.ul_proto
);
10251 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_END
,
10252 (struct sockaddr
*)&sp
->spidx
.dst_range
.end
, sp
->spidx
.prefd
,
10253 sp
->spidx
.ul_proto
);
10258 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
10259 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
10260 sp
->spidx
.ul_proto
);
10266 /* set secpolicy */
10267 m
= key_sp2msg(sp
);
10273 if ((result
->m_flags
& M_PKTHDR
) == 0) {
10277 if (result
->m_len
< sizeof(struct sadb_msg
)) {
10278 result
= m_pullup(result
, sizeof(struct sadb_msg
));
10279 if (result
== NULL
) {
10284 result
->m_pkthdr
.len
= 0;
10285 for (m
= result
; m
; m
= m
->m_next
)
10286 result
->m_pkthdr
.len
+= m
->m_len
;
10288 mtod(result
, struct sadb_msg
*)->sadb_msg_len
= PFKEY_UNIT64(result
->m_pkthdr
.len
);
10290 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
10299 key_delsp_for_ipsec_if (ifnet_t ipsec_if
)
10301 struct secashead
*sah
;
10302 struct secasvar
*sav
, *nextsav
;
10305 struct secpolicy
*sp
, *nextsp
;
10308 if (ipsec_if
== NULL
)
10311 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
10313 lck_mtx_lock(sadb_mutex
);
10315 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
10316 for (sp
= LIST_FIRST(&sptree
[dir
]);
10320 nextsp
= LIST_NEXT(sp
, chain
);
10322 if (sp
->ipsec_if
== ipsec_if
) {
10323 ifnet_release(sp
->ipsec_if
);
10324 sp
->ipsec_if
= NULL
;
10326 key_send_delsp(sp
);
10328 sp
->state
= IPSEC_SPSTATE_DEAD
;
10329 key_freesp(sp
, KEY_SADB_LOCKED
);
10334 LIST_FOREACH(sah
, &sahtree
, chain
) {
10335 if (sah
->ipsec_if
== ipsec_if
) {
10336 /* This SAH is linked to the IPSec interface. It now needs to close. */
10337 ifnet_release(sah
->ipsec_if
);
10338 sah
->ipsec_if
= NULL
;
10340 for (stateidx
= 0; stateidx
< _ARRAYLEN(saorder_state_alive
); stateidx
++) {
10341 state
= saorder_state_any
[stateidx
];
10342 for (sav
= LIST_FIRST(&sah
->savtree
[state
]); sav
!= NULL
; sav
= nextsav
) {
10343 nextsav
= LIST_NEXT(sav
, chain
);
10345 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
10346 key_freesav(sav
, KEY_SADB_LOCKED
);
10350 sah
->state
= SADB_SASTATE_DEAD
;
10354 lck_mtx_unlock(sadb_mutex
);
10357 __private_extern__ u_int32_t
10358 key_fill_offload_frames_for_savs (ifnet_t ifp
,
10359 struct ifnet_keepalive_offload_frame
*frames_array
,
10360 u_int32_t frames_array_count
,
10361 size_t frame_data_offset
)
10363 struct secashead
*sah
= NULL
;
10364 struct secasvar
*sav
= NULL
;
10365 struct ifnet_keepalive_offload_frame
*frame
= frames_array
;
10366 u_int32_t frame_index
= 0;
10368 if (frame
== NULL
|| frames_array_count
== 0) {
10369 return (frame_index
);
10372 lck_mtx_lock(sadb_mutex
);
10373 LIST_FOREACH(sah
, &sahtree
, chain
) {
10374 LIST_FOREACH(sav
, &sah
->savtree
[SADB_SASTATE_MATURE
], chain
) {
10375 if (ipsec_fill_offload_frame(ifp
, sav
, frame
, frame_data_offset
)) {
10377 if (frame_index
>= frames_array_count
) {
10378 lck_mtx_unlock(sadb_mutex
);
10379 return (frame_index
);
10381 frame
= &(frames_array
[frame_index
]);
10385 lck_mtx_unlock(sadb_mutex
);
10387 return (frame_index
);