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
,
842 struct sockaddr
*src
,
843 struct sockaddr
*dst
)
845 struct secashead
*sah
;
846 struct secasvar
*sav
;
849 const u_int
*saorder_state_valid
;
851 struct sockaddr_in
*sin
;
855 if (interface
== NULL
) {
859 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
861 lck_mtx_lock(sadb_mutex
);
864 LIST_FOREACH(sah
, &sahtree
, chain
) {
865 if (sah
->state
== SADB_SASTATE_DEAD
) {
868 if (sah
->ipsec_if
== interface
&&
869 (family
== AF_INET6
|| family
== AF_INET
) &&
870 sah
->dir
== IPSEC_DIR_OUTBOUND
) {
873 sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
&&
874 src
!= NULL
&& dst
!= NULL
) {
875 // Validate addresses for transport mode
876 if (key_sockaddrcmp((struct sockaddr
*)&sah
->saidx
.src
, src
, 0) != 0) {
877 // Source doesn't match
881 if (key_sockaddrcmp((struct sockaddr
*)&sah
->saidx
.dst
, dst
, 0) != 0) {
882 // Destination doesn't match
887 /* This SAH is linked to the IPSec interface, and the right family. We found it! */
888 if (key_preferred_oldsa
) {
889 saorder_state_valid
= saorder_state_valid_prefer_old
;
890 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
892 saorder_state_valid
= saorder_state_valid_prefer_new
;
893 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
896 sin
= (struct sockaddr_in
*)&sah
->saidx
.dst
;
897 dstport
= sin
->sin_port
;
898 if (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
) {
899 sin
->sin_port
= IPSEC_PORT_ANY
;
902 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
903 state
= saorder_state_valid
[stateidx
];
904 sav
= key_do_allocsa_policy(sah
, state
, dstport
);
906 lck_mtx_unlock(sadb_mutex
);
915 // If we didn't find anything, try again without strict
918 // We already were on the second try, bail
923 lck_mtx_unlock(sadb_mutex
);
928 * allocating an SA entry for an *OUTBOUND* packet.
929 * checking each request entries in SP, and acquire an SA if need.
930 * OUT: 0: there are valid requests.
931 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
935 struct ipsecrequest
*isr
,
936 struct secasindex
*saidx
,
937 struct secasvar
**sav
)
941 struct sockaddr_in
*sin
;
943 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
948 if (isr
== NULL
|| saidx
== NULL
)
949 panic("key_checkrequest: NULL pointer is passed.\n");
952 switch (saidx
->mode
) {
953 case IPSEC_MODE_TRANSPORT
:
954 case IPSEC_MODE_TUNNEL
:
958 panic("key_checkrequest: Invalid policy defined.\n");
961 /* get current level */
962 level
= ipsec_get_reqlevel(isr
);
966 * key_allocsa_policy should allocate the oldest SA available.
967 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
970 *sav
= key_allocsa_policy(saidx
);
972 /* When there is SA. */
978 * Remove dst port - used for special natt support - don't call
979 * key_acquire with it.
981 if (saidx
->mode
== IPSEC_MODE_TRANSPORT
) {
982 sin
= (struct sockaddr_in
*)&saidx
->dst
;
983 sin
->sin_port
= IPSEC_PORT_ANY
;
985 if ((error
= key_acquire(saidx
, isr
->sp
)) != 0) {
986 /* XXX What should I do ? */
987 ipseclog((LOG_DEBUG
, "key_checkrequest: error %d returned "
988 "from key_acquire.\n", error
));
992 return level
== IPSEC_LEVEL_REQUIRE
? ENOENT
: 0;
996 * allocating a SA for policy entry from SAD.
997 * NOTE: searching SAD of aliving state.
998 * OUT: NULL: not found.
999 * others: found and return the pointer.
1001 u_int32_t sah_search_calls
= 0;
1002 u_int32_t sah_search_count
= 0;
1005 struct secasindex
*saidx
)
1007 struct secashead
*sah
;
1008 struct secasvar
*sav
;
1009 u_int stateidx
, state
;
1010 const u_int
*saorder_state_valid
;
1012 struct sockaddr_in
*sin
;
1015 lck_mtx_lock(sadb_mutex
);
1017 LIST_FOREACH(sah
, &sahtree
, chain
) {
1019 if (sah
->state
== SADB_SASTATE_DEAD
)
1021 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_MODE
| CMP_REQID
))
1024 lck_mtx_unlock(sadb_mutex
);
1030 * search a valid state list for outbound packet.
1031 * This search order is important.
1033 if (key_preferred_oldsa
) {
1034 saorder_state_valid
= saorder_state_valid_prefer_old
;
1035 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
1037 saorder_state_valid
= saorder_state_valid_prefer_new
;
1038 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
1042 sin
= (struct sockaddr_in
*)&saidx
->dst
;
1043 dstport
= sin
->sin_port
;
1044 if (saidx
->mode
== IPSEC_MODE_TRANSPORT
)
1045 sin
->sin_port
= IPSEC_PORT_ANY
;
1047 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
1049 state
= saorder_state_valid
[stateidx
];
1051 sav
= key_do_allocsa_policy(sah
, state
, dstport
);
1053 lck_mtx_unlock(sadb_mutex
);
1057 lck_mtx_unlock(sadb_mutex
);
1062 key_send_delete (struct secasvar
*sav
)
1064 struct mbuf
*m
, *result
;
1067 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
1069 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0)
1070 panic("key_do_allocsa_policy: invalid proto is passed.\n");
1072 m
= key_setsadbmsg(SADB_DELETE
, 0,
1073 satype
, 0, 0, sav
->refcnt
- 1);
1078 /* set sadb_address for saidx's. */
1079 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
1080 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
1081 sav
->sah
->saidx
.src
.ss_len
<< 3,
1087 /* set sadb_address for saidx's. */
1088 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
1089 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
1090 sav
->sah
->saidx
.src
.ss_len
<< 3,
1096 /* create SA extension */
1097 m
= key_setsadbsa(sav
);
1102 if (result
->m_len
< sizeof(struct sadb_msg
)) {
1103 result
= m_pullup(result
,
1104 sizeof(struct sadb_msg
));
1109 result
->m_pkthdr
.len
= 0;
1110 for (m
= result
; m
; m
= m
->m_next
)
1111 result
->m_pkthdr
.len
+= m
->m_len
;
1112 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
1113 PFKEY_UNIT64(result
->m_pkthdr
.len
);
1115 if (key_sendup_mbuf(NULL
, result
,
1116 KEY_SENDUP_REGISTERED
))
1119 key_freesav(sav
, KEY_SADB_LOCKED
);
1123 * searching SAD with direction, protocol, mode and state.
1124 * called by key_allocsa_policy().
1127 * others : found, pointer to a SA.
1129 static struct secasvar
*
1130 key_do_allocsa_policy(
1131 struct secashead
*sah
,
1135 struct secasvar
*sav
, *nextsav
, *candidate
, *natt_candidate
, *no_natt_candidate
, *d
;
1137 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1141 natt_candidate
= NULL
;
1142 no_natt_candidate
= NULL
;
1144 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
1148 nextsav
= LIST_NEXT(sav
, chain
);
1151 KEY_CHKSASTATE(sav
->state
, state
, "key_do_allocsa_policy");
1153 if (sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&& dstport
&&
1154 ((sav
->flags
& SADB_X_EXT_NATT
) != 0) &&
1155 ntohs(dstport
) != sav
->remote_ike_port
)
1158 if (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
&&
1159 ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0) &&
1160 ntohs(dstport
) != sav
->remote_ike_port
)
1161 continue; /* skip this one - not a match - or not UDP */
1163 if ((sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&&
1164 ((sav
->flags
& SADB_X_EXT_NATT
) != 0)) ||
1165 (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
&&
1166 ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0))) {
1167 if (natt_candidate
== NULL
) {
1168 natt_candidate
= sav
;
1171 candidate
= natt_candidate
;
1173 if (no_natt_candidate
== NULL
) {
1174 no_natt_candidate
= sav
;
1177 candidate
= no_natt_candidate
;
1180 /* Which SA is the better ? */
1182 /* sanity check 2 */
1183 if (candidate
->lft_c
== NULL
|| sav
->lft_c
== NULL
)
1184 panic("key_do_allocsa_policy: "
1185 "lifetime_current is NULL.\n");
1187 /* What the best method is to compare ? */
1188 if (key_preferred_oldsa
) {
1189 if (candidate
->lft_c
->sadb_lifetime_addtime
>
1190 sav
->lft_c
->sadb_lifetime_addtime
) {
1191 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
1192 natt_candidate
= sav
;
1194 no_natt_candidate
= sav
;
1200 /* prefered new sa rather than old sa */
1201 if (candidate
->lft_c
->sadb_lifetime_addtime
<
1202 sav
->lft_c
->sadb_lifetime_addtime
) {
1204 if ((sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&&
1205 ((sav
->flags
& SADB_X_EXT_NATT
) != 0)) ||
1206 (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
&&
1207 ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0))) {
1208 natt_candidate
= sav
;
1210 no_natt_candidate
= sav
;
1217 * prepared to delete the SA when there is more
1218 * suitable candidate and the lifetime of the SA is not
1221 if (d
->lft_c
->sadb_lifetime_addtime
!= 0) {
1226 /* choose latest if both types present */
1227 if (natt_candidate
== NULL
)
1228 candidate
= no_natt_candidate
;
1229 else if (no_natt_candidate
== NULL
)
1230 candidate
= natt_candidate
;
1231 else if (sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&& dstport
)
1232 candidate
= natt_candidate
;
1233 else if (natt_candidate
->lft_c
->sadb_lifetime_addtime
>
1234 no_natt_candidate
->lft_c
->sadb_lifetime_addtime
)
1235 candidate
= natt_candidate
;
1237 candidate
= no_natt_candidate
;
1240 candidate
->refcnt
++;
1241 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1242 printf("DP allocsa_policy cause "
1243 "refcnt++:%d SA:0x%llx\n", candidate
->refcnt
,
1244 (uint64_t)VM_KERNEL_ADDRPERM(candidate
)));
1250 * allocating a SA entry for a *INBOUND* packet.
1251 * Must call key_freesav() later.
1252 * OUT: positive: pointer to a sav.
1253 * NULL: not found, or error occurred.
1255 * In the comparison, source address will be ignored for RFC2401 conformance.
1256 * To quote, from section 4.1:
1257 * A security association is uniquely identified by a triple consisting
1258 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1259 * security protocol (AH or ESP) identifier.
1260 * Note that, however, we do need to keep source address in IPsec SA.
1261 * IKE specification and PF_KEY specification do assume that we
1262 * keep source address in IPsec SA. We see a tricky situation here.
1272 return key_allocsa_extended(family
, src
, dst
, proto
, spi
, NULL
);
1276 key_allocsa_extended(u_int family
,
1283 struct secasvar
*sav
, *match
;
1284 u_int stateidx
, state
, tmpidx
, matchidx
;
1285 struct sockaddr_in sin
;
1286 struct sockaddr_in6 sin6
;
1287 const u_int
*saorder_state_valid
;
1290 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1293 if (src
== NULL
|| dst
== NULL
)
1294 panic("key_allocsa: NULL pointer is passed.\n");
1297 * when both systems employ similar strategy to use a SA.
1298 * the search order is important even in the inbound case.
1300 if (key_preferred_oldsa
) {
1301 saorder_state_valid
= saorder_state_valid_prefer_old
;
1302 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
1304 saorder_state_valid
= saorder_state_valid_prefer_new
;
1305 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
1310 * XXX: to be checked internal IP header somewhere. Also when
1311 * IPsec tunnel packet is received. But ESP tunnel mode is
1312 * encrypted so we can't check internal IP header.
1315 * search a valid state list for inbound packet.
1316 * the search order is not important.
1319 matchidx
= arraysize
;
1320 lck_mtx_lock(sadb_mutex
);
1321 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
1322 if (sav
->spi
!= spi
)
1324 if (interface
!= NULL
&&
1325 sav
->sah
->ipsec_if
!= interface
) {
1328 if (proto
!= sav
->sah
->saidx
.proto
)
1330 if (family
!= sav
->sah
->saidx
.src
.ss_family
||
1331 family
!= sav
->sah
->saidx
.dst
.ss_family
)
1334 for (stateidx
= 0; stateidx
< matchidx
; stateidx
++) {
1335 state
= saorder_state_valid
[stateidx
];
1336 if (sav
->state
== state
) {
1341 if (tmpidx
>= matchidx
)
1344 #if 0 /* don't check src */
1345 /* check src address */
1348 bzero(&sin
, sizeof(sin
));
1349 sin
.sin_family
= AF_INET
;
1350 sin
.sin_len
= sizeof(sin
);
1351 bcopy(src
, &sin
.sin_addr
,
1352 sizeof(sin
.sin_addr
));
1353 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
1354 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0)
1358 bzero(&sin6
, sizeof(sin6
));
1359 sin6
.sin6_family
= AF_INET6
;
1360 sin6
.sin6_len
= sizeof(sin6
);
1361 bcopy(src
, &sin6
.sin6_addr
,
1362 sizeof(sin6
.sin6_addr
));
1363 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
1364 /* kame fake scopeid */
1365 sin6
.sin6_scope_id
=
1366 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
1367 sin6
.sin6_addr
.s6_addr16
[1] = 0;
1369 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
1370 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0)
1374 ipseclog((LOG_DEBUG
, "key_allocsa: "
1375 "unknown address family=%d.\n",
1381 /* check dst address */
1384 bzero(&sin
, sizeof(sin
));
1385 sin
.sin_family
= AF_INET
;
1386 sin
.sin_len
= sizeof(sin
);
1387 bcopy(dst
, &sin
.sin_addr
,
1388 sizeof(sin
.sin_addr
));
1389 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
1390 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0)
1395 bzero(&sin6
, sizeof(sin6
));
1396 sin6
.sin6_family
= AF_INET6
;
1397 sin6
.sin6_len
= sizeof(sin6
);
1398 bcopy(dst
, &sin6
.sin6_addr
,
1399 sizeof(sin6
.sin6_addr
));
1400 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
1401 /* kame fake scopeid */
1402 sin6
.sin6_scope_id
=
1403 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
1404 sin6
.sin6_addr
.s6_addr16
[1] = 0;
1406 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
1407 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0)
1411 ipseclog((LOG_DEBUG
, "key_allocsa: "
1412 "unknown address family=%d.\n", family
));
1423 lck_mtx_unlock(sadb_mutex
);
1428 lck_mtx_unlock(sadb_mutex
);
1429 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1430 printf("DP allocsa cause refcnt++:%d SA:0x%llx\n",
1431 match
->refcnt
, (uint64_t)VM_KERNEL_ADDRPERM(match
)));
1436 key_natt_get_translated_port(
1437 struct secasvar
*outsav
)
1440 struct secasindex saidx
;
1441 struct secashead
*sah
;
1442 u_int stateidx
, state
;
1443 const u_int
*saorder_state_valid
;
1446 /* get sa for incoming */
1447 saidx
.mode
= outsav
->sah
->saidx
.mode
;
1449 saidx
.proto
= outsav
->sah
->saidx
.proto
;
1450 bcopy(&outsav
->sah
->saidx
.src
, &saidx
.dst
, sizeof(struct sockaddr_in
));
1451 bcopy(&outsav
->sah
->saidx
.dst
, &saidx
.src
, sizeof(struct sockaddr_in
));
1453 lck_mtx_lock(sadb_mutex
);
1454 LIST_FOREACH(sah
, &sahtree
, chain
) {
1455 if (sah
->state
== SADB_SASTATE_DEAD
)
1457 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_MODE
))
1460 lck_mtx_unlock(sadb_mutex
);
1465 * Found sah - now go thru list of SAs and find
1466 * matching remote ike port. If found - set
1467 * sav->natt_encapsulated_src_port and return the port.
1470 * search a valid state list for outbound packet.
1471 * This search order is important.
1473 if (key_preferred_oldsa
) {
1474 saorder_state_valid
= saorder_state_valid_prefer_old
;
1475 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
1477 saorder_state_valid
= saorder_state_valid_prefer_new
;
1478 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
1481 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
1482 state
= saorder_state_valid
[stateidx
];
1483 if (key_do_get_translated_port(sah
, outsav
, state
)) {
1484 lck_mtx_unlock(sadb_mutex
);
1485 return outsav
->natt_encapsulated_src_port
;
1488 lck_mtx_unlock(sadb_mutex
);
1493 key_do_get_translated_port(
1494 struct secashead
*sah
,
1495 struct secasvar
*outsav
,
1498 struct secasvar
*currsav
, *nextsav
, *candidate
;
1501 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1506 for (currsav
= LIST_FIRST(&sah
->savtree
[state
]);
1508 currsav
= nextsav
) {
1510 nextsav
= LIST_NEXT(currsav
, chain
);
1513 KEY_CHKSASTATE(currsav
->state
, state
, "key_do_get_translated_port");
1515 if ((currsav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) == 0 ||
1516 currsav
->remote_ike_port
!= outsav
->remote_ike_port
)
1519 if (candidate
== NULL
) {
1520 candidate
= currsav
;
1524 /* Which SA is the better ? */
1526 /* sanity check 2 */
1527 if (candidate
->lft_c
== NULL
|| currsav
->lft_c
== NULL
)
1528 panic("key_do_get_translated_port: "
1529 "lifetime_current is NULL.\n");
1531 /* What the best method is to compare ? */
1532 if (key_preferred_oldsa
) {
1533 if (candidate
->lft_c
->sadb_lifetime_addtime
>
1534 currsav
->lft_c
->sadb_lifetime_addtime
) {
1535 candidate
= currsav
;
1541 /* prefered new sa rather than old sa */
1542 if (candidate
->lft_c
->sadb_lifetime_addtime
<
1543 currsav
->lft_c
->sadb_lifetime_addtime
)
1544 candidate
= currsav
;
1548 outsav
->natt_encapsulated_src_port
= candidate
->natt_encapsulated_src_port
;
1556 * Must be called after calling key_allocsp().
1560 struct secpolicy
*sp
,
1566 panic("key_freesp: NULL pointer is passed.\n");
1569 lck_mtx_lock(sadb_mutex
);
1571 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1573 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1574 printf("DP freesp cause refcnt--:%d SP:0x%llx\n",
1575 sp
->refcnt
, (uint64_t)VM_KERNEL_ADDRPERM(sp
)));
1577 if (sp
->refcnt
== 0)
1580 lck_mtx_unlock(sadb_mutex
);
1585 * Must be called after calling key_allocsa().
1586 * This function is called by key_freesp() to free some SA allocated
1591 struct secasvar
*sav
,
1597 panic("key_freesav: NULL pointer is passed.\n");
1600 lck_mtx_lock(sadb_mutex
);
1602 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1604 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1605 printf("DP freesav cause refcnt--:%d SA:0x%llx SPI %u\n",
1606 sav
->refcnt
, (uint64_t)VM_KERNEL_ADDRPERM(sav
),
1607 (u_int32_t
)ntohl(sav
->spi
)));
1609 if (sav
->refcnt
== 0)
1612 lck_mtx_unlock(sadb_mutex
);
1616 /* %%% SPD management */
1618 * free security policy entry.
1622 struct secpolicy
*sp
)
1627 panic("key_delsp: NULL pointer is passed.\n");
1629 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1630 sp
->state
= IPSEC_SPSTATE_DEAD
;
1633 return; /* can't free */
1635 /* remove from SP index */
1636 if (__LIST_CHAINED(sp
)) {
1637 LIST_REMOVE(sp
, chain
);
1638 ipsec_policy_count
--;
1641 if (sp
->spidx
.internal_if
) {
1642 ifnet_release(sp
->spidx
.internal_if
);
1643 sp
->spidx
.internal_if
= NULL
;
1647 ifnet_release(sp
->ipsec_if
);
1648 sp
->ipsec_if
= NULL
;
1651 if (sp
->outgoing_if
) {
1652 ifnet_release(sp
->outgoing_if
);
1653 sp
->outgoing_if
= NULL
;
1657 struct ipsecrequest
*isr
= sp
->req
, *nextisr
;
1659 while (isr
!= NULL
) {
1660 nextisr
= isr
->next
;
1665 keydb_delsecpolicy(sp
);
1672 * OUT: NULL : not found
1673 * others : found, pointer to a SP.
1675 static struct secpolicy
*
1677 struct secpolicyindex
*spidx
)
1679 struct secpolicy
*sp
;
1681 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1685 panic("key_getsp: NULL pointer is passed.\n");
1687 LIST_FOREACH(sp
, &sptree
[spidx
->dir
], chain
) {
1688 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1690 if (key_cmpspidx_exactly(spidx
, &sp
->spidx
)) {
1701 * OUT: NULL : not found
1702 * others : found, pointer to a SP.
1708 struct secpolicy
*sp
;
1710 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1712 lck_mtx_lock(sadb_mutex
);
1713 sp
= __key_getspbyid(id
);
1714 lck_mtx_unlock(sadb_mutex
);
1719 static struct secpolicy
*
1720 __key_getspbyid(u_int32_t id
)
1722 struct secpolicy
*sp
;
1724 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1726 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_INBOUND
], chain
) {
1727 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1735 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_OUTBOUND
], chain
) {
1736 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1750 struct secpolicy
*newsp
= NULL
;
1752 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1753 newsp
= keydb_newsecpolicy();
1764 * create secpolicy structure from sadb_x_policy structure.
1765 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1766 * so must be set properly later.
1770 struct sadb_x_policy
*xpl0
,
1774 struct secpolicy
*newsp
;
1776 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1780 panic("key_msg2sp: NULL pointer was passed.\n");
1781 if (len
< sizeof(*xpl0
))
1782 panic("key_msg2sp: invalid length.\n");
1783 if (len
!= PFKEY_EXTLEN(xpl0
)) {
1784 ipseclog((LOG_DEBUG
, "key_msg2sp: Invalid msg length.\n"));
1789 if ((newsp
= key_newsp()) == NULL
) {
1794 newsp
->spidx
.dir
= xpl0
->sadb_x_policy_dir
;
1795 newsp
->policy
= xpl0
->sadb_x_policy_type
;
1798 switch (xpl0
->sadb_x_policy_type
) {
1799 case IPSEC_POLICY_DISCARD
:
1800 case IPSEC_POLICY_GENERATE
:
1801 case IPSEC_POLICY_NONE
:
1802 case IPSEC_POLICY_ENTRUST
:
1803 case IPSEC_POLICY_BYPASS
:
1807 case IPSEC_POLICY_IPSEC
:
1810 struct sadb_x_ipsecrequest
*xisr
;
1811 struct ipsecrequest
**p_isr
= &newsp
->req
;
1813 /* validity check */
1814 if (PFKEY_EXTLEN(xpl0
) < sizeof(*xpl0
)) {
1815 ipseclog((LOG_DEBUG
,
1816 "key_msg2sp: Invalid msg length.\n"));
1817 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1822 tlen
= PFKEY_EXTLEN(xpl0
) - sizeof(*xpl0
);
1823 xisr
= (struct sadb_x_ipsecrequest
*)(xpl0
+ 1);
1826 if (tlen
< sizeof(*xisr
)) {
1827 ipseclog((LOG_DEBUG
, "key_msg2sp: "
1828 "invalid ipsecrequest.\n"));
1829 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1835 if (xisr
->sadb_x_ipsecrequest_len
< sizeof(*xisr
)) {
1836 ipseclog((LOG_DEBUG
, "key_msg2sp: "
1837 "invalid ipsecrequest length.\n"));
1838 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1843 /* allocate request buffer */
1844 KMALLOC_WAIT(*p_isr
, struct ipsecrequest
*, sizeof(**p_isr
));
1845 if ((*p_isr
) == NULL
) {
1846 ipseclog((LOG_DEBUG
,
1847 "key_msg2sp: No more memory.\n"));
1848 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1852 bzero(*p_isr
, sizeof(**p_isr
));
1855 (*p_isr
)->next
= NULL
;
1857 switch (xisr
->sadb_x_ipsecrequest_proto
) {
1860 case IPPROTO_IPCOMP
:
1863 ipseclog((LOG_DEBUG
,
1864 "key_msg2sp: invalid proto type=%u\n",
1865 xisr
->sadb_x_ipsecrequest_proto
));
1866 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1867 *error
= EPROTONOSUPPORT
;
1870 (*p_isr
)->saidx
.proto
= xisr
->sadb_x_ipsecrequest_proto
;
1872 switch (xisr
->sadb_x_ipsecrequest_mode
) {
1873 case IPSEC_MODE_TRANSPORT
:
1874 case IPSEC_MODE_TUNNEL
:
1876 case IPSEC_MODE_ANY
:
1878 ipseclog((LOG_DEBUG
,
1879 "key_msg2sp: invalid mode=%u\n",
1880 xisr
->sadb_x_ipsecrequest_mode
));
1881 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1885 (*p_isr
)->saidx
.mode
= xisr
->sadb_x_ipsecrequest_mode
;
1887 switch (xisr
->sadb_x_ipsecrequest_level
) {
1888 case IPSEC_LEVEL_DEFAULT
:
1889 case IPSEC_LEVEL_USE
:
1890 case IPSEC_LEVEL_REQUIRE
:
1892 case IPSEC_LEVEL_UNIQUE
:
1893 /* validity check */
1895 * If range violation of reqid, kernel will
1896 * update it, don't refuse it.
1898 if (xisr
->sadb_x_ipsecrequest_reqid
1899 > IPSEC_MANUAL_REQID_MAX
) {
1900 ipseclog((LOG_DEBUG
,
1901 "key_msg2sp: reqid=%d range "
1902 "violation, updated by kernel.\n",
1903 xisr
->sadb_x_ipsecrequest_reqid
));
1904 xisr
->sadb_x_ipsecrequest_reqid
= 0;
1907 /* allocate new reqid id if reqid is zero. */
1908 if (xisr
->sadb_x_ipsecrequest_reqid
== 0) {
1910 if ((reqid
= key_newreqid()) == 0) {
1911 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1915 (*p_isr
)->saidx
.reqid
= reqid
;
1916 xisr
->sadb_x_ipsecrequest_reqid
= reqid
;
1918 /* set it for manual keying. */
1919 (*p_isr
)->saidx
.reqid
=
1920 xisr
->sadb_x_ipsecrequest_reqid
;
1925 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid level=%u\n",
1926 xisr
->sadb_x_ipsecrequest_level
));
1927 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1931 (*p_isr
)->level
= xisr
->sadb_x_ipsecrequest_level
;
1933 /* set IP addresses if there */
1934 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
1935 struct sockaddr
*paddr
;
1937 if (tlen
< xisr
->sadb_x_ipsecrequest_len
) {
1938 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1939 "address length.\n"));
1940 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1945 paddr
= (struct sockaddr
*)(xisr
+ 1);
1946 uint8_t src_len
= paddr
->sa_len
;
1948 if (xisr
->sadb_x_ipsecrequest_len
< src_len
) {
1949 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1950 "invalid source address length.\n"));
1951 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1956 /* validity check */
1958 > sizeof((*p_isr
)->saidx
.src
)) {
1959 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1960 "address length.\n"));
1961 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1966 bcopy(paddr
, &(*p_isr
)->saidx
.src
,
1967 MIN(paddr
->sa_len
, sizeof((*p_isr
)->saidx
.src
)));
1969 paddr
= (struct sockaddr
*)((caddr_t
)paddr
+ paddr
->sa_len
);
1970 uint8_t dst_len
= paddr
->sa_len
;
1972 if (xisr
->sadb_x_ipsecrequest_len
< (src_len
+ dst_len
)) {
1973 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1974 "invalid dest address length.\n"));
1975 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1980 /* validity check */
1982 > sizeof((*p_isr
)->saidx
.dst
)) {
1983 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1984 "address length.\n"));
1985 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1990 bcopy(paddr
, &(*p_isr
)->saidx
.dst
,
1991 MIN(paddr
->sa_len
, sizeof((*p_isr
)->saidx
.dst
)));
1994 (*p_isr
)->sp
= newsp
;
1996 /* initialization for the next. */
1997 p_isr
= &(*p_isr
)->next
;
1998 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
2000 /* validity check */
2002 ipseclog((LOG_DEBUG
, "key_msg2sp: becoming tlen < 0.\n"));
2003 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
2008 xisr
= (struct sadb_x_ipsecrequest
*)(void *)
2009 ((caddr_t
)xisr
+ xisr
->sadb_x_ipsecrequest_len
);
2014 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid policy type.\n"));
2015 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
2027 lck_mtx_lock(sadb_mutex
);
2028 static u_int32_t auto_reqid
= IPSEC_MANUAL_REQID_MAX
+ 1;
2031 /* The reqid must be limited to 16 bits because the PF_KEY message format only uses
2032 16 bits for this field. Once it becomes larger than 16 bits - ipsec fails to
2033 work anymore. Changing the PF_KEY message format would introduce compatibility
2034 issues. This code now tests to see if the tentative reqid is in use */
2037 struct secpolicy
*sp
;
2038 struct ipsecrequest
*isr
;
2041 auto_reqid
= (auto_reqid
== 0xFFFF
2042 ? IPSEC_MANUAL_REQID_MAX
+ 1 : auto_reqid
+ 1);
2044 /* check for uniqueness */
2046 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2047 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2048 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2049 if (isr
->saidx
.reqid
== auto_reqid
) {
2062 lck_mtx_unlock(sadb_mutex
);
2067 * copy secpolicy struct to sadb_x_policy structure indicated.
2071 struct secpolicy
*sp
)
2073 struct sadb_x_policy
*xpl
;
2080 panic("key_sp2msg: NULL pointer was passed.\n");
2082 tlen
= key_getspreqmsglen(sp
);
2084 m
= key_alloc_mbuf(tlen
);
2085 if (!m
|| m
->m_next
) { /*XXX*/
2093 xpl
= mtod(m
, struct sadb_x_policy
*);
2096 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(tlen
);
2097 xpl
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2098 xpl
->sadb_x_policy_type
= sp
->policy
;
2099 xpl
->sadb_x_policy_dir
= sp
->spidx
.dir
;
2100 xpl
->sadb_x_policy_id
= sp
->id
;
2101 p
= (caddr_t
)xpl
+ sizeof(*xpl
);
2103 /* if is the policy for ipsec ? */
2104 if (sp
->policy
== IPSEC_POLICY_IPSEC
) {
2105 struct sadb_x_ipsecrequest
*xisr
;
2106 struct ipsecrequest
*isr
;
2108 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2110 xisr
= (struct sadb_x_ipsecrequest
*)(void *)p
;
2112 xisr
->sadb_x_ipsecrequest_proto
= isr
->saidx
.proto
;
2113 xisr
->sadb_x_ipsecrequest_mode
= isr
->saidx
.mode
;
2114 xisr
->sadb_x_ipsecrequest_level
= isr
->level
;
2115 xisr
->sadb_x_ipsecrequest_reqid
= isr
->saidx
.reqid
;
2118 bcopy(&isr
->saidx
.src
, p
, isr
->saidx
.src
.ss_len
);
2119 p
+= isr
->saidx
.src
.ss_len
;
2120 bcopy(&isr
->saidx
.dst
, p
, isr
->saidx
.dst
.ss_len
);
2121 p
+= isr
->saidx
.src
.ss_len
;
2123 xisr
->sadb_x_ipsecrequest_len
=
2124 PFKEY_ALIGN8(sizeof(*xisr
)
2125 + isr
->saidx
.src
.ss_len
2126 + isr
->saidx
.dst
.ss_len
);
2133 /* m will not be freed nor modified */
2134 static struct mbuf
*
2135 key_gather_mbuf(struct mbuf
*m
, const struct sadb_msghdr
*mhp
,
2136 int ndeep
, int nitem
, int *items
)
2140 struct mbuf
*result
= NULL
, *n
;
2143 if (m
== NULL
|| mhp
== NULL
)
2144 panic("null pointer passed to key_gather");
2146 for (i
= 0; i
< nitem
; i
++) {
2148 if (idx
< 0 || idx
> SADB_EXT_MAX
)
2150 /* don't attempt to pull empty extension */
2151 if (idx
== SADB_EXT_RESERVED
&& mhp
->msg
== NULL
)
2153 if (idx
!= SADB_EXT_RESERVED
&&
2154 (mhp
->ext
[idx
] == NULL
|| mhp
->extlen
[idx
] == 0))
2157 if (idx
== SADB_EXT_RESERVED
) {
2158 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2159 MGETHDR(n
, M_WAITOK
, MT_DATA
); // sadb_msg len < MHLEN - enforced by _CASSERT
2164 m_copydata(m
, 0, sizeof(struct sadb_msg
),
2166 } else if (i
< ndeep
) {
2167 len
= mhp
->extlen
[idx
];
2168 n
= key_alloc_mbuf(len
);
2169 if (!n
|| n
->m_next
) { /*XXX*/
2174 m_copydata(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
2177 n
= m_copym(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
2189 if ((result
->m_flags
& M_PKTHDR
) != 0) {
2190 result
->m_pkthdr
.len
= 0;
2191 for (n
= result
; n
; n
= n
->m_next
)
2192 result
->m_pkthdr
.len
+= n
->m_len
;
2203 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
2204 * add a entry to SP database, when received
2205 * <base, address(SD), (lifetime(H),) policy>
2207 * Adding to SP database,
2209 * <base, address(SD), (lifetime(H),) policy>
2210 * to the socket which was send.
2212 * SPDADD set a unique policy entry.
2213 * SPDSETIDX like SPDADD without a part of policy requests.
2214 * SPDUPDATE replace a unique policy entry.
2216 * m will always be freed.
2222 const struct sadb_msghdr
*mhp
)
2224 struct sadb_address
*src0
, *dst0
, *src1
= NULL
, *dst1
= NULL
;
2225 struct sadb_x_policy
*xpl0
, *xpl
;
2226 struct sadb_lifetime
*lft
= NULL
;
2227 struct secpolicyindex spidx
;
2228 struct secpolicy
*newsp
;
2230 ifnet_t internal_if
= NULL
;
2231 char *outgoing_if
= NULL
;
2232 char *ipsec_if
= NULL
;
2233 struct sadb_x_ipsecif
*ipsecifopts
= NULL
;
2235 int use_src_range
= 0;
2236 int use_dst_range
= 0;
2237 int init_disabled
= 0;
2238 int address_family
, address_len
;
2240 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2243 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2244 panic("key_spdadd: NULL pointer is passed.\n");
2246 if (mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
] != NULL
&& mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
] != NULL
) {
2249 if (mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_START
] != NULL
&& mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_END
] != NULL
) {
2253 if ((!use_src_range
&& mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
) ||
2254 (!use_dst_range
&& mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) ||
2255 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
2256 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2257 return key_senderror(so
, m
, EINVAL
);
2259 if ((use_src_range
&& (mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_SRC_START
] < sizeof(struct sadb_address
)
2260 || mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_SRC_END
] < sizeof(struct sadb_address
))) ||
2261 (!use_src_range
&& mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
)) ||
2262 (use_dst_range
&& (mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_DST_START
] < sizeof(struct sadb_address
)
2263 || mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_DST_END
] < sizeof(struct sadb_address
))) ||
2264 (!use_dst_range
&& mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) ||
2265 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2266 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2267 return key_senderror(so
, m
, EINVAL
);
2269 if (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
) {
2270 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
]
2271 < sizeof(struct sadb_lifetime
)) {
2272 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2273 return key_senderror(so
, m
, EINVAL
);
2275 lft
= (struct sadb_lifetime
*)
2276 (void *)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
2278 if (mhp
->ext
[SADB_X_EXT_IPSECIF
] != NULL
) {
2279 if (mhp
->extlen
[SADB_X_EXT_IPSECIF
] < sizeof(struct sadb_x_ipsecif
)) {
2280 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2281 return key_senderror(so
, m
, EINVAL
);
2285 if (use_src_range
) {
2286 src0
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
];
2287 src1
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
];
2289 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
2291 if (use_dst_range
) {
2292 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_START
];
2293 dst1
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_END
];
2295 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
2297 xpl0
= (struct sadb_x_policy
*)(void *)mhp
->ext
[SADB_X_EXT_POLICY
];
2298 ipsecifopts
= (struct sadb_x_ipsecif
*)(void *)mhp
->ext
[SADB_X_EXT_IPSECIF
];
2300 /* check addresses */
2301 address_family
= ((struct sockaddr
*)(src0
+ 1))->sa_family
;
2302 address_len
= ((struct sockaddr
*)(src0
+ 1))->sa_len
;
2303 if (use_src_range
) {
2304 if (((struct sockaddr
*)(src1
+ 1))->sa_family
!= address_family
||
2305 ((struct sockaddr
*)(src1
+ 1))->sa_len
!= address_len
) {
2306 return key_senderror(so
, m
, EINVAL
);
2309 if (((struct sockaddr
*)(dst0
+ 1))->sa_family
!= address_family
||
2310 ((struct sockaddr
*)(dst0
+ 1))->sa_len
!= address_len
) {
2311 return key_senderror(so
, m
, EINVAL
);
2313 if (use_dst_range
) {
2314 if (((struct sockaddr
*)(dst1
+ 1))->sa_family
!= address_family
||
2315 ((struct sockaddr
*)(dst1
+ 1))->sa_len
!= address_len
) {
2316 return key_senderror(so
, m
, EINVAL
);
2320 /* checking the direction. */
2321 switch (xpl0
->sadb_x_policy_dir
) {
2322 case IPSEC_DIR_INBOUND
:
2323 case IPSEC_DIR_OUTBOUND
:
2326 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid SP direction.\n"));
2327 mhp
->msg
->sadb_msg_errno
= EINVAL
;
2332 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
2333 if (xpl0
->sadb_x_policy_type
== IPSEC_POLICY_ENTRUST
2334 || xpl0
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
) {
2335 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid policy type.\n"));
2336 return key_senderror(so
, m
, EINVAL
);
2339 /* policy requests are mandatory when action is ipsec. */
2340 if (mhp
->msg
->sadb_msg_type
!= SADB_X_SPDSETIDX
2341 && xpl0
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
2342 && mhp
->extlen
[SADB_X_EXT_POLICY
] <= sizeof(*xpl0
)) {
2343 ipseclog((LOG_DEBUG
, "key_spdadd: some policy requests part required.\n"));
2344 return key_senderror(so
, m
, EINVAL
);
2347 /* Process interfaces */
2348 if (ipsecifopts
!= NULL
) {
2349 if (ipsecifopts
->sadb_x_ipsecif_internal_if
[0]) {
2350 ifnet_find_by_name(ipsecifopts
->sadb_x_ipsecif_internal_if
, &internal_if
);
2352 if (ipsecifopts
->sadb_x_ipsecif_outgoing_if
[0]) {
2353 outgoing_if
= ipsecifopts
->sadb_x_ipsecif_outgoing_if
;
2355 if (ipsecifopts
->sadb_x_ipsecif_ipsec_if
[0]) {
2356 ipsec_if
= ipsecifopts
->sadb_x_ipsecif_ipsec_if
;
2358 init_disabled
= ipsecifopts
->sadb_x_ipsecif_init_disabled
;
2362 /* XXX boundary check against sa_len */
2363 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2366 src0
->sadb_address_prefixlen
,
2367 dst0
->sadb_address_prefixlen
,
2368 src0
->sadb_address_proto
,
2370 use_src_range
? src0
+ 1 : NULL
,
2371 use_src_range
? src1
+ 1 : NULL
,
2372 use_dst_range
? dst0
+ 1 : NULL
,
2373 use_dst_range
? dst1
+ 1 : NULL
,
2377 * checking there is SP already or not.
2378 * SPDUPDATE doesn't depend on whether there is a SP or not.
2379 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
2382 lck_mtx_lock(sadb_mutex
);
2383 newsp
= key_getsp(&spidx
);
2384 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
2386 newsp
->state
= IPSEC_SPSTATE_DEAD
;
2387 key_freesp(newsp
, KEY_SADB_LOCKED
);
2390 if (newsp
!= NULL
) {
2391 key_freesp(newsp
, KEY_SADB_LOCKED
);
2392 ipseclog((LOG_DEBUG
, "key_spdadd: a SP entry exists already.\n"));
2393 lck_mtx_unlock(sadb_mutex
);
2395 ifnet_release(internal_if
);
2398 return key_senderror(so
, m
, EEXIST
);
2401 lck_mtx_unlock(sadb_mutex
);
2403 /* allocation new SP entry */
2404 if ((newsp
= key_msg2sp(xpl0
, PFKEY_EXTLEN(xpl0
), &error
)) == NULL
) {
2406 ifnet_release(internal_if
);
2409 return key_senderror(so
, m
, error
);
2412 if ((newsp
->id
= key_getnewspid()) == 0) {
2413 keydb_delsecpolicy(newsp
);
2415 ifnet_release(internal_if
);
2418 return key_senderror(so
, m
, ENOBUFS
);
2421 /* XXX boundary check against sa_len */
2422 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2425 src0
->sadb_address_prefixlen
,
2426 dst0
->sadb_address_prefixlen
,
2427 src0
->sadb_address_proto
,
2429 use_src_range
? src0
+ 1 : NULL
,
2430 use_src_range
? src1
+ 1 : NULL
,
2431 use_dst_range
? dst0
+ 1 : NULL
,
2432 use_dst_range
? dst1
+ 1 : NULL
,
2437 * allow IPv6 over IPv4 or IPv4 over IPv6 tunnels using ESP -
2438 * otherwise reject if inner and outer address families not equal
2440 if (newsp
->req
&& newsp
->req
->saidx
.src
.ss_family
) {
2441 struct sockaddr
*sa
;
2442 sa
= (struct sockaddr
*)(src0
+ 1);
2443 if (sa
->sa_family
!= newsp
->req
->saidx
.src
.ss_family
) {
2444 if (newsp
->req
->saidx
.mode
!= IPSEC_MODE_TUNNEL
|| newsp
->req
->saidx
.proto
!= IPPROTO_ESP
) {
2445 keydb_delsecpolicy(newsp
);
2447 ifnet_release(internal_if
);
2450 return key_senderror(so
, m
, EINVAL
);
2454 if (newsp
->req
&& newsp
->req
->saidx
.dst
.ss_family
) {
2455 struct sockaddr
*sa
;
2456 sa
= (struct sockaddr
*)(dst0
+ 1);
2457 if (sa
->sa_family
!= newsp
->req
->saidx
.dst
.ss_family
) {
2458 if (newsp
->req
->saidx
.mode
!= IPSEC_MODE_TUNNEL
|| newsp
->req
->saidx
.proto
!= IPPROTO_ESP
) {
2459 keydb_delsecpolicy(newsp
);
2461 ifnet_release(internal_if
);
2464 return key_senderror(so
, m
, EINVAL
);
2471 newsp
->created
= tv
.tv_sec
;
2472 newsp
->lastused
= tv
.tv_sec
;
2473 newsp
->lifetime
= lft
? lft
->sadb_lifetime_addtime
: 0;
2474 newsp
->validtime
= lft
? lft
->sadb_lifetime_usetime
: 0;
2476 if (outgoing_if
!= NULL
) {
2477 ifnet_find_by_name(outgoing_if
, &newsp
->outgoing_if
);
2479 if (ipsec_if
!= NULL
) {
2480 ifnet_find_by_name(ipsec_if
, &newsp
->ipsec_if
);
2482 if (init_disabled
> 0) {
2483 newsp
->disabled
= 1;
2486 newsp
->refcnt
= 1; /* do not reclaim until I say I do */
2487 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
2488 lck_mtx_lock(sadb_mutex
);
2490 * policies of type generate should be at the end of the SPD
2491 * because they function as default discard policies
2492 * Don't start timehandler for generate policies
2494 if (newsp
->policy
== IPSEC_POLICY_GENERATE
)
2495 LIST_INSERT_TAIL(&sptree
[newsp
->spidx
.dir
], newsp
, secpolicy
, chain
);
2496 else { /* XXX until we have policy ordering in the kernel */
2497 struct secpolicy
*tmpsp
;
2499 LIST_FOREACH(tmpsp
, &sptree
[newsp
->spidx
.dir
], chain
)
2500 if (tmpsp
->policy
== IPSEC_POLICY_GENERATE
)
2503 LIST_INSERT_BEFORE(tmpsp
, newsp
, chain
);
2505 LIST_INSERT_TAIL(&sptree
[newsp
->spidx
.dir
], newsp
, secpolicy
, chain
);
2506 key_start_timehandler();
2509 ipsec_policy_count
++;
2510 /* Turn off the ipsec bypass */
2511 if (ipsec_bypass
!= 0)
2514 /* delete the entry in spacqtree */
2515 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
2516 struct secspacq
*spacq
;
2517 if ((spacq
= key_getspacq(&spidx
)) != NULL
) {
2518 /* reset counter in order to deletion by timehandler. */
2520 spacq
->created
= tv
.tv_sec
;
2524 lck_mtx_unlock(sadb_mutex
);
2527 struct mbuf
*n
, *mpolicy
;
2528 struct sadb_msg
*newmsg
;
2531 /* create new sadb_msg to reply. */
2533 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2534 SADB_EXT_LIFETIME_HARD
, SADB_EXT_ADDRESS_SRC
,
2535 SADB_EXT_ADDRESS_DST
, SADB_X_EXT_ADDR_RANGE_SRC_START
, SADB_X_EXT_ADDR_RANGE_SRC_END
,
2536 SADB_X_EXT_ADDR_RANGE_DST_START
, SADB_X_EXT_ADDR_RANGE_DST_END
};
2537 n
= key_gather_mbuf(m
, mhp
, 2, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2539 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2540 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
,
2541 SADB_X_EXT_ADDR_RANGE_SRC_START
, SADB_X_EXT_ADDR_RANGE_SRC_END
,
2542 SADB_X_EXT_ADDR_RANGE_DST_START
, SADB_X_EXT_ADDR_RANGE_DST_END
};
2543 n
= key_gather_mbuf(m
, mhp
, 2, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2546 return key_senderror(so
, m
, ENOBUFS
);
2548 if (n
->m_len
< sizeof(*newmsg
)) {
2549 n
= m_pullup(n
, sizeof(*newmsg
));
2551 return key_senderror(so
, m
, ENOBUFS
);
2553 newmsg
= mtod(n
, struct sadb_msg
*);
2554 newmsg
->sadb_msg_errno
= 0;
2555 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2558 mpolicy
= m_pulldown(n
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)),
2559 sizeof(*xpl
), &off
);
2560 if (mpolicy
== NULL
) {
2561 /* n is already freed */
2562 return key_senderror(so
, m
, ENOBUFS
);
2564 xpl
= (struct sadb_x_policy
*)(void *)(mtod(mpolicy
, caddr_t
) + off
);
2565 if (xpl
->sadb_x_policy_exttype
!= SADB_X_EXT_POLICY
) {
2567 return key_senderror(so
, m
, EINVAL
);
2569 xpl
->sadb_x_policy_id
= newsp
->id
;
2572 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2577 * get new policy id.
2583 key_getnewspid(void)
2585 u_int32_t newid
= 0;
2586 int count
= key_spi_trycnt
; /* XXX */
2587 struct secpolicy
*sp
;
2589 /* when requesting to allocate spi ranged */
2590 lck_mtx_lock(sadb_mutex
);
2592 newid
= (policy_id
= (policy_id
== ~0 ? 1 : policy_id
+ 1));
2594 if ((sp
= __key_getspbyid(newid
)) == NULL
)
2597 key_freesp(sp
, KEY_SADB_LOCKED
);
2599 lck_mtx_unlock(sadb_mutex
);
2600 if (count
== 0 || newid
== 0) {
2601 ipseclog((LOG_DEBUG
, "key_getnewspid: to allocate policy id is failed.\n"));
2609 * SADB_SPDDELETE processing
2611 * <base, address(SD), policy(*)>
2612 * from the user(?), and set SADB_SASTATE_DEAD,
2614 * <base, address(SD), policy(*)>
2616 * policy(*) including direction of policy.
2618 * m will always be freed.
2624 const struct sadb_msghdr
*mhp
)
2626 struct sadb_address
*src0
, *dst0
, *src1
= NULL
, *dst1
= NULL
;
2627 struct sadb_x_policy
*xpl0
;
2628 struct secpolicyindex spidx
;
2629 struct secpolicy
*sp
;
2630 ifnet_t internal_if
= NULL
;
2631 struct sadb_x_ipsecif
*ipsecifopts
= NULL
;
2632 int use_src_range
= 0;
2633 int use_dst_range
= 0;
2635 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2638 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2639 panic("key_spddelete: NULL pointer is passed.\n");
2641 if (mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
] != NULL
&& mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
] != NULL
) {
2644 if (mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_START
] != NULL
&& mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_END
] != NULL
) {
2648 if ((!use_src_range
&& mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
) ||
2649 (!use_dst_range
&& mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) ||
2650 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
2651 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
2652 return key_senderror(so
, m
, EINVAL
);
2654 if ((use_src_range
&& (mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_SRC_START
] < sizeof(struct sadb_address
)
2655 || mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_SRC_END
] < sizeof(struct sadb_address
))) ||
2656 (!use_src_range
&& mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
)) ||
2657 (use_dst_range
&& (mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_DST_START
] < sizeof(struct sadb_address
)
2658 || mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_DST_END
] < sizeof(struct sadb_address
))) ||
2659 (!use_dst_range
&& mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) ||
2660 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2661 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
2662 return key_senderror(so
, m
, EINVAL
);
2665 if (use_src_range
) {
2666 src0
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
];
2667 src1
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
];
2669 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
2671 if (use_dst_range
) {
2672 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_START
];
2673 dst1
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_END
];
2675 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
2677 xpl0
= (struct sadb_x_policy
*)(void *)mhp
->ext
[SADB_X_EXT_POLICY
];
2678 ipsecifopts
= (struct sadb_x_ipsecif
*)(void *)mhp
->ext
[SADB_X_EXT_IPSECIF
];
2680 /* checking the direction. */
2681 switch (xpl0
->sadb_x_policy_dir
) {
2682 case IPSEC_DIR_INBOUND
:
2683 case IPSEC_DIR_OUTBOUND
:
2686 ipseclog((LOG_DEBUG
, "key_spddelete: Invalid SP direction.\n"));
2687 return key_senderror(so
, m
, EINVAL
);
2690 /* Process interfaces */
2691 if (ipsecifopts
!= NULL
) {
2692 if (ipsecifopts
->sadb_x_ipsecif_internal_if
[0]) {
2693 ifnet_find_by_name(ipsecifopts
->sadb_x_ipsecif_internal_if
, &internal_if
);
2698 /* XXX boundary check against sa_len */
2699 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2702 src0
->sadb_address_prefixlen
,
2703 dst0
->sadb_address_prefixlen
,
2704 src0
->sadb_address_proto
,
2706 use_src_range
? src0
+ 1 : NULL
,
2707 use_src_range
? src1
+ 1 : NULL
,
2708 use_dst_range
? dst0
+ 1 : NULL
,
2709 use_dst_range
? dst1
+ 1 : NULL
,
2712 /* Is there SP in SPD ? */
2713 lck_mtx_lock(sadb_mutex
);
2714 if ((sp
= key_getsp(&spidx
)) == NULL
) {
2715 ipseclog((LOG_DEBUG
, "key_spddelete: no SP found.\n"));
2716 lck_mtx_unlock(sadb_mutex
);
2718 ifnet_release(internal_if
);
2721 return key_senderror(so
, m
, EINVAL
);
2725 ifnet_release(internal_if
);
2729 /* save policy id to buffer to be returned. */
2730 xpl0
->sadb_x_policy_id
= sp
->id
;
2732 sp
->state
= IPSEC_SPSTATE_DEAD
;
2733 key_freesp(sp
, KEY_SADB_LOCKED
);
2734 lck_mtx_unlock(sadb_mutex
);
2739 struct sadb_msg
*newmsg
;
2740 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2741 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
,
2742 SADB_X_EXT_ADDR_RANGE_SRC_START
, SADB_X_EXT_ADDR_RANGE_SRC_END
,
2743 SADB_X_EXT_ADDR_RANGE_DST_START
, SADB_X_EXT_ADDR_RANGE_DST_END
};
2745 /* create new sadb_msg to reply. */
2746 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2748 return key_senderror(so
, m
, ENOBUFS
);
2750 newmsg
= mtod(n
, struct sadb_msg
*);
2751 newmsg
->sadb_msg_errno
= 0;
2752 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2755 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2760 * SADB_SPDDELETE2 processing
2763 * from the user(?), and set SADB_SASTATE_DEAD,
2767 * policy(*) including direction of policy.
2769 * m will always be freed.
2775 const struct sadb_msghdr
*mhp
)
2778 struct secpolicy
*sp
;
2780 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2783 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2784 panic("key_spddelete2: NULL pointer is passed.\n");
2786 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2787 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2788 ipseclog((LOG_DEBUG
, "key_spddelete2: invalid message is passed.\n"));
2789 key_senderror(so
, m
, EINVAL
);
2793 id
= ((struct sadb_x_policy
*)
2794 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2796 /* Is there SP in SPD ? */
2797 lck_mtx_lock(sadb_mutex
);
2798 if ((sp
= __key_getspbyid(id
)) == NULL
) {
2799 lck_mtx_unlock(sadb_mutex
);
2800 ipseclog((LOG_DEBUG
, "key_spddelete2: no SP found id:%u.\n", id
));
2801 return key_senderror(so
, m
, EINVAL
);
2804 sp
->state
= IPSEC_SPSTATE_DEAD
;
2805 key_freesp(sp
, KEY_SADB_LOCKED
);
2806 lck_mtx_unlock(sadb_mutex
);
2809 struct mbuf
*n
, *nn
;
2810 struct sadb_msg
*newmsg
;
2813 /* create new sadb_msg to reply. */
2814 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2817 return key_senderror(so
, m
, ENOBUFS
);
2818 MGETHDR(n
, M_WAITOK
, MT_DATA
);
2819 if (n
&& len
> MHLEN
) {
2820 MCLGET(n
, M_WAITOK
);
2821 if ((n
->m_flags
& M_EXT
) == 0) {
2827 return key_senderror(so
, m
, ENOBUFS
);
2833 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
2834 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2838 panic("length inconsistency in key_spddelete2");
2841 n
->m_next
= m_copym(m
, mhp
->extoff
[SADB_X_EXT_POLICY
],
2842 mhp
->extlen
[SADB_X_EXT_POLICY
], M_WAITOK
);
2845 return key_senderror(so
, m
, ENOBUFS
);
2848 n
->m_pkthdr
.len
= 0;
2849 for (nn
= n
; nn
; nn
= nn
->m_next
)
2850 n
->m_pkthdr
.len
+= nn
->m_len
;
2852 newmsg
= mtod(n
, struct sadb_msg
*);
2853 newmsg
->sadb_msg_errno
= 0;
2854 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2857 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2865 const struct sadb_msghdr
*mhp
)
2868 struct secpolicy
*sp
;
2870 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2873 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2874 panic("key_spdenable: NULL pointer is passed.\n");
2876 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2877 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2878 ipseclog((LOG_DEBUG
, "key_spdenable: invalid message is passed.\n"));
2879 key_senderror(so
, m
, EINVAL
);
2883 id
= ((struct sadb_x_policy
*)
2884 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2886 /* Is there SP in SPD ? */
2887 lck_mtx_lock(sadb_mutex
);
2888 if ((sp
= __key_getspbyid(id
)) == NULL
) {
2889 lck_mtx_unlock(sadb_mutex
);
2890 ipseclog((LOG_DEBUG
, "key_spdenable: no SP found id:%u.\n", id
));
2891 return key_senderror(so
, m
, EINVAL
);
2895 lck_mtx_unlock(sadb_mutex
);
2899 struct sadb_msg
*newmsg
;
2900 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
};
2902 /* create new sadb_msg to reply. */
2903 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2905 return key_senderror(so
, m
, ENOBUFS
);
2907 if (n
->m_len
< sizeof(struct sadb_msg
)) {
2908 n
= m_pullup(n
, sizeof(struct sadb_msg
));
2910 return key_senderror(so
, m
, ENOBUFS
);
2912 newmsg
= mtod(n
, struct sadb_msg
*);
2913 newmsg
->sadb_msg_errno
= 0;
2914 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2917 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2925 const struct sadb_msghdr
*mhp
)
2928 struct secpolicy
*sp
;
2930 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2933 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2934 panic("key_spddisable: NULL pointer is passed.\n");
2936 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2937 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2938 ipseclog((LOG_DEBUG
, "key_spddisable: invalid message is passed.\n"));
2939 key_senderror(so
, m
, EINVAL
);
2943 id
= ((struct sadb_x_policy
*)
2944 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2946 /* Is there SP in SPD ? */
2947 lck_mtx_lock(sadb_mutex
);
2948 if ((sp
= __key_getspbyid(id
)) == NULL
) {
2949 lck_mtx_unlock(sadb_mutex
);
2950 ipseclog((LOG_DEBUG
, "key_spddisable: no SP found id:%u.\n", id
));
2951 return key_senderror(so
, m
, EINVAL
);
2955 lck_mtx_unlock(sadb_mutex
);
2959 struct sadb_msg
*newmsg
;
2960 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
};
2962 /* create new sadb_msg to reply. */
2963 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2965 return key_senderror(so
, m
, ENOBUFS
);
2967 if (n
->m_len
< sizeof(struct sadb_msg
)) {
2968 n
= m_pullup(n
, sizeof(struct sadb_msg
));
2970 return key_senderror(so
, m
, ENOBUFS
);
2972 newmsg
= mtod(n
, struct sadb_msg
*);
2973 newmsg
->sadb_msg_errno
= 0;
2974 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2977 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2982 * SADB_X_GET processing
2987 * <base, address(SD), policy>
2989 * policy(*) including direction of policy.
2991 * m will always be freed.
2997 const struct sadb_msghdr
*mhp
)
3000 struct secpolicy
*sp
;
3003 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3006 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
3007 panic("key_spdget: NULL pointer is passed.\n");
3009 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
3010 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
3011 ipseclog((LOG_DEBUG
, "key_spdget: invalid message is passed.\n"));
3012 return key_senderror(so
, m
, EINVAL
);
3015 id
= ((struct sadb_x_policy
*)
3016 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
3018 /* Is there SP in SPD ? */
3019 lck_mtx_lock(sadb_mutex
);
3020 if ((sp
= __key_getspbyid(id
)) == NULL
) {
3021 ipseclog((LOG_DEBUG
, "key_spdget: no SP found id:%u.\n", id
));
3022 lck_mtx_unlock(sadb_mutex
);
3023 return key_senderror(so
, m
, ENOENT
);
3025 lck_mtx_unlock(sadb_mutex
);
3026 n
= key_setdumpsp(sp
, SADB_X_SPDGET
, 0, mhp
->msg
->sadb_msg_pid
);
3029 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
3031 return key_senderror(so
, m
, ENOBUFS
);
3035 * SADB_X_SPDACQUIRE processing.
3036 * Acquire policy and SA(s) for a *OUTBOUND* packet.
3039 * to KMD, and expect to receive
3040 * <base> with SADB_X_SPDACQUIRE if error occurred,
3043 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
3044 * policy(*) is without policy requests.
3047 * others: error number
3051 struct secpolicy
*sp
)
3053 struct mbuf
*result
= NULL
, *m
;
3054 struct secspacq
*newspacq
;
3057 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3061 panic("key_spdacquire: NULL pointer is passed.\n");
3062 if (sp
->req
!= NULL
)
3063 panic("key_spdacquire: called but there is request.\n");
3064 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
3065 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
3067 /* get a entry to check whether sent message or not. */
3068 lck_mtx_lock(sadb_mutex
);
3069 if ((newspacq
= key_getspacq(&sp
->spidx
)) != NULL
) {
3070 if (key_blockacq_count
< newspacq
->count
) {
3071 /* reset counter and do send message. */
3072 newspacq
->count
= 0;
3074 /* increment counter and do nothing. */
3076 lck_mtx_unlock(sadb_mutex
);
3080 /* make new entry for blocking to send SADB_ACQUIRE. */
3081 if ((newspacq
= key_newspacq(&sp
->spidx
)) == NULL
) {
3082 lck_mtx_unlock(sadb_mutex
);
3085 /* add to acqtree */
3086 LIST_INSERT_HEAD(&spacqtree
, newspacq
, chain
);
3087 key_start_timehandler();
3089 lck_mtx_unlock(sadb_mutex
);
3090 /* create new sadb_msg to reply. */
3091 m
= key_setsadbmsg(SADB_X_SPDACQUIRE
, 0, 0, 0, 0, 0);
3098 result
->m_pkthdr
.len
= 0;
3099 for (m
= result
; m
; m
= m
->m_next
)
3100 result
->m_pkthdr
.len
+= m
->m_len
;
3102 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3103 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3105 return key_sendup_mbuf(NULL
, m
, KEY_SENDUP_REGISTERED
);
3114 * SADB_SPDFLUSH processing
3117 * from the user, and free all entries in secpctree.
3121 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
3123 * m will always be freed.
3129 const struct sadb_msghdr
*mhp
)
3131 struct sadb_msg
*newmsg
;
3132 struct secpolicy
*sp
;
3136 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
3137 panic("key_spdflush: NULL pointer is passed.\n");
3139 if (m
->m_len
!= PFKEY_ALIGN8(sizeof(struct sadb_msg
)))
3140 return key_senderror(so
, m
, EINVAL
);
3142 lck_mtx_lock(sadb_mutex
);
3143 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
3144 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
3145 sp
->state
= IPSEC_SPSTATE_DEAD
;
3148 lck_mtx_unlock(sadb_mutex
);
3150 if (sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
3151 ipseclog((LOG_DEBUG
, "key_spdflush: No more memory.\n"));
3152 return key_senderror(so
, m
, ENOBUFS
);
3158 m
->m_pkthdr
.len
= m
->m_len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
3159 newmsg
= mtod(m
, struct sadb_msg
*);
3160 newmsg
->sadb_msg_errno
= 0;
3161 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
3163 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
3167 * SADB_SPDDUMP processing
3170 * from the user, and dump all SP leaves
3175 * m will always be freed.
3182 const struct sadb_msghdr
*mhp
)
3184 struct secpolicy
*sp
, **spbuf
= NULL
, **sp_ptr
;
3185 int cnt
= 0, bufcount
;
3191 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
3192 panic("key_spddump: NULL pointer is passed.\n");
3194 if ((bufcount
= ipsec_policy_count
) == 0) {
3198 bufcount
+= 256; /* extra */
3199 KMALLOC_WAIT(spbuf
, struct secpolicy
**, bufcount
* sizeof(struct secpolicy
*));
3200 if (spbuf
== NULL
) {
3201 ipseclog((LOG_DEBUG
, "key_spddump: No more memory.\n"));
3205 lck_mtx_lock(sadb_mutex
);
3206 /* search SPD entry, make list. */
3208 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
3209 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
3210 if (cnt
== bufcount
)
3211 break; /* buffer full */
3217 lck_mtx_unlock(sadb_mutex
);
3227 n
= key_setdumpsp(*sp_ptr
++, SADB_X_SPDDUMP
, cnt
,
3228 mhp
->msg
->sadb_msg_pid
);
3231 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
3234 lck_mtx_lock(sadb_mutex
);
3235 while (sp_ptr
> spbuf
)
3236 key_freesp(*(--sp_ptr
), KEY_SADB_LOCKED
);
3237 lck_mtx_unlock(sadb_mutex
);
3243 return key_senderror(so
, m
, error
);
3250 static struct mbuf
*
3252 struct secpolicy
*sp
,
3257 struct mbuf
*result
= NULL
, *m
;
3259 m
= key_setsadbmsg(type
, 0, SADB_SATYPE_UNSPEC
, seq
, pid
, sp
->refcnt
);
3264 if (sp
->spidx
.src_range
.start
.ss_len
> 0) {
3265 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_START
,
3266 (struct sockaddr
*)&sp
->spidx
.src_range
.start
, sp
->spidx
.prefs
,
3267 sp
->spidx
.ul_proto
);
3272 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_END
,
3273 (struct sockaddr
*)&sp
->spidx
.src_range
.end
, sp
->spidx
.prefs
,
3274 sp
->spidx
.ul_proto
);
3279 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
3280 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
3281 sp
->spidx
.ul_proto
);
3287 if (sp
->spidx
.dst_range
.start
.ss_len
> 0) {
3288 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_START
,
3289 (struct sockaddr
*)&sp
->spidx
.dst_range
.start
, sp
->spidx
.prefd
,
3290 sp
->spidx
.ul_proto
);
3295 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_END
,
3296 (struct sockaddr
*)&sp
->spidx
.dst_range
.end
, sp
->spidx
.prefd
,
3297 sp
->spidx
.ul_proto
);
3302 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
3303 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
3304 sp
->spidx
.ul_proto
);
3310 if (sp
->spidx
.internal_if
|| sp
->outgoing_if
|| sp
->ipsec_if
|| sp
->disabled
) {
3311 m
= key_setsadbipsecif(sp
->spidx
.internal_if
, sp
->outgoing_if
, sp
->ipsec_if
, sp
->disabled
);
3322 if ((result
->m_flags
& M_PKTHDR
) == 0)
3325 if (result
->m_len
< sizeof(struct sadb_msg
)) {
3326 result
= m_pullup(result
, sizeof(struct sadb_msg
));
3331 result
->m_pkthdr
.len
= 0;
3332 for (m
= result
; m
; m
= m
->m_next
)
3333 result
->m_pkthdr
.len
+= m
->m_len
;
3335 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3336 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3346 * get PFKEY message length for security policy and request.
3350 struct secpolicy
*sp
)
3354 tlen
= sizeof(struct sadb_x_policy
);
3356 /* if is the policy for ipsec ? */
3357 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
3360 /* get length of ipsec requests */
3362 struct ipsecrequest
*isr
;
3365 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
3366 len
= sizeof(struct sadb_x_ipsecrequest
)
3367 + isr
->saidx
.src
.ss_len
3368 + isr
->saidx
.dst
.ss_len
;
3370 tlen
+= PFKEY_ALIGN8(len
);
3378 * SADB_SPDEXPIRE processing
3380 * <base, address(SD), lifetime(CH), policy>
3384 * others : error number
3388 struct secpolicy
*sp
)
3390 struct mbuf
*result
= NULL
, *m
;
3393 struct sadb_lifetime
*lt
;
3395 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3399 panic("key_spdexpire: NULL pointer is passed.\n");
3401 /* set msg header */
3402 m
= key_setsadbmsg(SADB_X_SPDEXPIRE
, 0, 0, 0, 0, 0);
3409 /* create lifetime extension (current and hard) */
3410 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
3411 m
= key_alloc_mbuf(len
);
3412 if (!m
|| m
->m_next
) { /*XXX*/
3418 bzero(mtod(m
, caddr_t
), len
);
3419 lt
= mtod(m
, struct sadb_lifetime
*);
3420 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3421 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
3422 lt
->sadb_lifetime_allocations
= 0;
3423 lt
->sadb_lifetime_bytes
= 0;
3424 lt
->sadb_lifetime_addtime
= sp
->created
;
3425 lt
->sadb_lifetime_usetime
= sp
->lastused
;
3426 lt
= (struct sadb_lifetime
*)(void *)(mtod(m
, caddr_t
) + len
/ 2);
3427 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3428 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
3429 lt
->sadb_lifetime_allocations
= 0;
3430 lt
->sadb_lifetime_bytes
= 0;
3431 lt
->sadb_lifetime_addtime
= sp
->lifetime
;
3432 lt
->sadb_lifetime_usetime
= sp
->validtime
;
3435 /* set sadb_address(es) for source */
3436 if (sp
->spidx
.src_range
.start
.ss_len
> 0) {
3437 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_START
,
3438 (struct sockaddr
*)&sp
->spidx
.src_range
.start
, sp
->spidx
.prefs
,
3439 sp
->spidx
.ul_proto
);
3446 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_END
,
3447 (struct sockaddr
*)&sp
->spidx
.src_range
.end
, sp
->spidx
.prefs
,
3448 sp
->spidx
.ul_proto
);
3455 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
3456 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
3457 sp
->spidx
.ul_proto
);
3465 /* set sadb_address(es) for dest */
3466 if (sp
->spidx
.dst_range
.start
.ss_len
> 0) {
3467 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_START
,
3468 (struct sockaddr
*)&sp
->spidx
.dst_range
.start
, sp
->spidx
.prefd
,
3469 sp
->spidx
.ul_proto
);
3476 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_END
,
3477 (struct sockaddr
*)&sp
->spidx
.dst_range
.end
, sp
->spidx
.prefd
,
3478 sp
->spidx
.ul_proto
);
3485 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
3486 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
3487 sp
->spidx
.ul_proto
);
3503 if ((result
->m_flags
& M_PKTHDR
) == 0) {
3508 if (result
->m_len
< sizeof(struct sadb_msg
)) {
3509 result
= m_pullup(result
, sizeof(struct sadb_msg
));
3510 if (result
== NULL
) {
3516 result
->m_pkthdr
.len
= 0;
3517 for (m
= result
; m
; m
= m
->m_next
)
3518 result
->m_pkthdr
.len
+= m
->m_len
;
3520 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3521 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3523 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
3531 /* %%% SAD management */
3533 * allocating a memory for new SA head, and copy from the values of mhp.
3534 * OUT: NULL : failure due to the lack of memory.
3535 * others : pointer to new SA head.
3537 static struct secashead
*
3538 key_newsah(struct secasindex
*saidx
,
3543 struct secashead
*newsah
;
3547 panic("key_newsaidx: NULL pointer is passed.\n");
3549 newsah
= keydb_newsecashead();
3553 bcopy(saidx
, &newsah
->saidx
, sizeof(newsah
->saidx
));
3555 /* remove the ports */
3556 switch (saidx
->src
.ss_family
) {
3558 ((struct sockaddr_in
*)(&newsah
->saidx
.src
))->sin_port
= IPSEC_PORT_ANY
;
3561 ((struct sockaddr_in6
*)(&newsah
->saidx
.src
))->sin6_port
= IPSEC_PORT_ANY
;
3566 switch (saidx
->dst
.ss_family
) {
3568 ((struct sockaddr_in
*)(&newsah
->saidx
.dst
))->sin_port
= IPSEC_PORT_ANY
;
3571 ((struct sockaddr_in6
*)(&newsah
->saidx
.dst
))->sin6_port
= IPSEC_PORT_ANY
;
3577 newsah
->outgoing_if
= outgoing_if
;
3579 ifnet_reference(ipsec_if
);
3580 newsah
->ipsec_if
= ipsec_if
;
3583 /* add to saidxtree */
3584 newsah
->state
= SADB_SASTATE_MATURE
;
3585 LIST_INSERT_HEAD(&sahtree
, newsah
, chain
);
3586 key_start_timehandler();
3592 * delete SA index and all SA registerd.
3596 struct secashead
*sah
)
3598 struct secasvar
*sav
, *nextsav
;
3599 u_int stateidx
, state
;
3602 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3606 panic("key_delsah: NULL pointer is passed.\n");
3608 /* searching all SA registerd in the secindex. */
3610 stateidx
< _ARRAYLEN(saorder_state_any
);
3613 state
= saorder_state_any
[stateidx
];
3614 for (sav
= (struct secasvar
*)LIST_FIRST(&sah
->savtree
[state
]);
3618 nextsav
= LIST_NEXT(sav
, chain
);
3620 if (sav
->refcnt
> 0) {
3621 /* give up to delete this sa */
3627 KEY_CHKSASTATE(state
, sav
->state
, "key_delsah");
3629 key_freesav(sav
, KEY_SADB_LOCKED
);
3631 /* remove back pointer */
3637 /* don't delete sah only if there are savs. */
3641 ROUTE_RELEASE(&sah
->sa_route
);
3643 if (sah
->ipsec_if
) {
3644 ifnet_release(sah
->ipsec_if
);
3645 sah
->ipsec_if
= NULL
;
3656 /* remove from tree of SA index */
3657 if (__LIST_CHAINED(sah
))
3658 LIST_REMOVE(sah
, chain
);
3666 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
3667 * and copy the values of mhp into new buffer.
3668 * When SAD message type is GETSPI:
3669 * to set sequence number from acq_seq++,
3670 * to set zero to SPI.
3671 * not to call key_setsava().
3673 * others : pointer to new secasvar.
3675 * does not modify mbuf. does not free mbuf on error.
3677 static struct secasvar
*
3680 const struct sadb_msghdr
*mhp
,
3681 struct secashead
*sah
,
3685 struct secasvar
*newsav
;
3686 const struct sadb_sa
*xsa
;
3688 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3691 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
|| sah
== NULL
)
3692 panic("key_newsa: NULL pointer is passed.\n");
3694 KMALLOC_NOWAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3695 if (newsav
== NULL
) {
3696 lck_mtx_unlock(sadb_mutex
);
3697 KMALLOC_WAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3698 lck_mtx_lock(sadb_mutex
);
3699 if (newsav
== NULL
) {
3700 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3705 bzero((caddr_t
)newsav
, sizeof(struct secasvar
));
3707 switch (mhp
->msg
->sadb_msg_type
) {
3709 key_setspi(newsav
, 0);
3711 #if IPSEC_DOSEQCHECK
3712 /* sync sequence number */
3713 if (mhp
->msg
->sadb_msg_seq
== 0)
3715 (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
3718 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
3723 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
3725 ipseclog((LOG_DEBUG
, "key_newsa: invalid message is passed.\n"));
3729 xsa
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
3730 key_setspi(newsav
, xsa
->sadb_sa_spi
);
3731 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
3739 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
3740 if (((struct sadb_x_sa2
*)(void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_alwaysexpire
)
3741 newsav
->always_expire
= 1;
3742 newsav
->flags2
= ((struct sadb_x_sa2
*)(void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_flags
;
3743 if (newsav
->flags2
& SADB_X_EXT_SA2_DELETE_ON_DETACH
) {
3748 /* copy sav values */
3749 if (mhp
->msg
->sadb_msg_type
!= SADB_GETSPI
) {
3750 *errp
= key_setsaval(newsav
, m
, mhp
);
3756 /* For get SPI, if has a hard lifetime, apply */
3757 const struct sadb_lifetime
*lft0
;
3760 lft0
= (struct sadb_lifetime
*)(void *)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
3762 /* make lifetime for CURRENT */
3763 KMALLOC_NOWAIT(newsav
->lft_c
, struct sadb_lifetime
*,
3764 sizeof(struct sadb_lifetime
));
3765 if (newsav
->lft_c
== NULL
) {
3766 lck_mtx_unlock(sadb_mutex
);
3767 KMALLOC_WAIT(newsav
->lft_c
, struct sadb_lifetime
*,
3768 sizeof(struct sadb_lifetime
));
3769 lck_mtx_lock(sadb_mutex
);
3770 if (newsav
->lft_c
== NULL
) {
3771 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3780 newsav
->lft_c
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3781 newsav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
3782 newsav
->lft_c
->sadb_lifetime_allocations
= 0;
3783 newsav
->lft_c
->sadb_lifetime_bytes
= 0;
3784 newsav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
3785 newsav
->lft_c
->sadb_lifetime_usetime
= 0;
3787 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
] < sizeof(*lft0
)) {
3788 ipseclog((LOG_DEBUG
, "key_newsa: invalid hard lifetime ext len.\n"));
3793 newsav
->lft_h
= (struct sadb_lifetime
*)key_newbuf(lft0
, sizeof(*lft0
));
3794 if (newsav
->lft_h
== NULL
) {
3795 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3807 newsav
->created
= tv
.tv_sec
;
3810 newsav
->pid
= mhp
->msg
->sadb_msg_pid
;
3815 newsav
->state
= SADB_SASTATE_LARVAL
;
3816 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
3824 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
3825 * and copy the values passed into new buffer.
3826 * When SAD message type is GETSPI:
3827 * to set sequence number from acq_seq++,
3828 * to set zero to SPI.
3829 * not to call key_setsava().
3831 * others : pointer to new secasvar.
3834 key_newsav2(struct secashead
*sah
,
3840 struct sadb_key
*key_auth
,
3841 u_int16_t key_auth_len
,
3842 struct sadb_key
*key_enc
,
3843 u_int16_t key_enc_len
,
3844 u_int16_t natt_port
,
3848 struct sadb_lifetime
*lifetime_hard
,
3849 struct sadb_lifetime
*lifetime_soft
)
3851 struct secasvar
*newsav
;
3853 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3857 panic("key_newsa: NULL pointer is passed.\n");
3859 KMALLOC_NOWAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3860 if (newsav
== NULL
) {
3861 lck_mtx_unlock(sadb_mutex
);
3862 KMALLOC_WAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3863 lck_mtx_lock(sadb_mutex
);
3864 if (newsav
== NULL
) {
3865 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3869 bzero((caddr_t
)newsav
, sizeof(struct secasvar
));
3871 #if IPSEC_DOSEQCHECK
3872 /* sync sequence number */
3874 newsav
->seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
3878 key_setspi(newsav
, spi
);
3880 if (key_setsaval2(newsav
,
3904 newsav
->created
= tv
.tv_sec
;
3912 if (spi
&& key_auth
&& key_auth_len
&& key_enc
&& key_enc_len
) {
3913 newsav
->state
= SADB_SASTATE_MATURE
;
3914 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_MATURE
], newsav
,
3917 newsav
->state
= SADB_SASTATE_LARVAL
;
3918 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
3927 key_migratesav(struct secasvar
*sav
,
3928 struct secashead
*newsah
)
3930 if (sav
== NULL
|| newsah
== NULL
|| sav
->state
!= SADB_SASTATE_MATURE
) {
3934 /* remove from SA header */
3935 if (__LIST_CHAINED(sav
))
3936 LIST_REMOVE(sav
, chain
);
3939 LIST_INSERT_TAIL(&newsah
->savtree
[SADB_SASTATE_MATURE
], sav
, secasvar
, chain
);
3944 * free() SA variable entry.
3948 struct secasvar
*sav
)
3951 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3955 panic("key_delsav: NULL pointer is passed.\n");
3957 if (sav
->refcnt
> 0)
3958 return; /* can't free */
3960 /* remove from SA header */
3961 if (__LIST_CHAINED(sav
))
3962 LIST_REMOVE(sav
, chain
);
3965 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
)
3966 LIST_REMOVE(sav
, spihash
);
3968 if (sav
->key_auth
!= NULL
) {
3969 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
3970 KFREE(sav
->key_auth
);
3971 sav
->key_auth
= NULL
;
3973 if (sav
->key_enc
!= NULL
) {
3974 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
3975 KFREE(sav
->key_enc
);
3976 sav
->key_enc
= NULL
;
3979 bzero(sav
->sched
, sav
->schedlen
);
3983 if (sav
->replay
!= NULL
) {
3984 keydb_delsecreplay(sav
->replay
);
3987 if (sav
->lft_c
!= NULL
) {
3991 if (sav
->lft_h
!= NULL
) {
3995 if (sav
->lft_s
!= NULL
) {
3999 if (sav
->iv
!= NULL
) {
4013 * others : found, pointer to a SA.
4015 static struct secashead
*
4016 key_getsah(struct secasindex
*saidx
)
4018 struct secashead
*sah
;
4020 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4022 LIST_FOREACH(sah
, &sahtree
, chain
) {
4023 if (sah
->state
== SADB_SASTATE_DEAD
)
4025 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_REQID
))
4033 key_newsah2 (struct secasindex
*saidx
,
4036 struct secashead
*sah
;
4038 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4040 sah
= key_getsah(saidx
);
4042 return(key_newsah(saidx
, NULL
, 0, dir
));
4048 * check not to be duplicated SPI.
4049 * NOTE: this function is too slow due to searching all SAD.
4052 * others : found, pointer to a SA.
4054 static struct secasvar
*
4056 struct secasindex
*saidx
,
4059 struct secasvar
*sav
;
4060 u_int stateidx
, state
;
4062 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4064 /* check address family */
4065 if (saidx
->src
.ss_family
!= saidx
->dst
.ss_family
) {
4066 ipseclog((LOG_DEBUG
, "key_checkspidup: address family mismatched.\n"));
4071 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
4072 if (sav
->spi
!= spi
)
4075 stateidx
< _ARRAYLEN(saorder_state_alive
);
4077 state
= saorder_state_alive
[stateidx
];
4078 if (sav
->state
== state
&&
4079 key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
))
4089 struct secasvar
*sav
,
4092 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4094 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
)
4095 LIST_REMOVE(sav
, spihash
);
4096 LIST_INSERT_HEAD(&spihash
[SPIHASH(spi
)], sav
, spihash
);
4101 * search SAD litmited alive SA, protocol, SPI.
4104 * others : found, pointer to a SA.
4106 static struct secasvar
*
4108 struct secashead
*sah
,
4111 struct secasvar
*sav
, *match
;
4112 u_int stateidx
, state
, matchidx
;
4114 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4116 matchidx
= _ARRAYLEN(saorder_state_alive
);
4117 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
4118 if (sav
->spi
!= spi
)
4120 if (sav
->sah
!= sah
)
4122 for (stateidx
= 0; stateidx
< matchidx
; stateidx
++) {
4123 state
= saorder_state_alive
[stateidx
];
4124 if (sav
->state
== state
) {
4126 matchidx
= stateidx
;
4136 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
4137 * You must update these if need.
4141 * does not modify mbuf. does not free mbuf on error.
4145 struct secasvar
*sav
,
4147 const struct sadb_msghdr
*mhp
)
4150 const struct esp_algorithm
*algo
;
4155 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4158 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
4159 panic("key_setsaval: NULL pointer is passed.\n");
4161 /* initialization */
4163 sav
->key_auth
= NULL
;
4164 sav
->key_enc
= NULL
;
4171 sav
->remote_ike_port
= 0;
4172 sav
->natt_last_activity
= natt_now
;
4173 sav
->natt_encapsulated_src_port
= 0;
4176 if (mhp
->ext
[SADB_EXT_SA
] != NULL
) {
4177 const struct sadb_sa
*sa0
;
4179 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
4180 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(*sa0
)) {
4181 ipseclog((LOG_DEBUG
, "key_setsaval: invalid message size.\n"));
4186 sav
->alg_auth
= sa0
->sadb_sa_auth
;
4187 sav
->alg_enc
= sa0
->sadb_sa_encrypt
;
4188 sav
->flags
= sa0
->sadb_sa_flags
;
4191 * Verify that a nat-traversal port was specified if
4192 * the nat-traversal flag is set.
4194 if ((sav
->flags
& SADB_X_EXT_NATT
) != 0) {
4195 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa_2
) ||
4196 ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
== 0) {
4197 ipseclog((LOG_DEBUG
, "key_setsaval: natt port not set.\n"));
4201 sav
->remote_ike_port
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
;
4202 sav
->natt_interval
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_interval
;
4203 sav
->natt_offload_interval
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_offload_interval
;
4207 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
4208 * SADB_X_EXT_NATT is set and SADB_X_EXT_NATT_KEEPALIVE is not
4209 * set (we're not behind nat) - otherwise clear it.
4211 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
4212 if ((sav
->flags
& SADB_X_EXT_NATT
) == 0 ||
4213 (sav
->flags
& SADB_X_EXT_NATT_KEEPALIVE
) != 0)
4214 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
4217 if ((sa0
->sadb_sa_flags
& SADB_X_EXT_OLD
) == 0) {
4218 sav
->replay
= keydb_newsecreplay(sa0
->sadb_sa_replay
);
4219 if (sav
->replay
== NULL
) {
4220 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4227 /* Authentication keys */
4228 if (mhp
->ext
[SADB_EXT_KEY_AUTH
] != NULL
) {
4229 const struct sadb_key
*key0
;
4232 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_AUTH
];
4233 len
= mhp
->extlen
[SADB_EXT_KEY_AUTH
];
4236 if (len
< sizeof(*key0
)) {
4237 ipseclog((LOG_DEBUG
, "key_setsaval: invalid auth key ext len. len = %d\n", len
));
4241 switch (mhp
->msg
->sadb_msg_satype
) {
4242 case SADB_SATYPE_AH
:
4243 case SADB_SATYPE_ESP
:
4244 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
4245 sav
->alg_auth
!= SADB_X_AALG_NULL
)
4248 case SADB_X_SATYPE_IPCOMP
:
4254 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_auth values.\n"));
4258 sav
->key_auth
= (struct sadb_key
*)key_newbuf(key0
, len
);
4259 if (sav
->key_auth
== NULL
) {
4260 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4266 /* Encryption key */
4267 if (mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] != NULL
) {
4268 const struct sadb_key
*key0
;
4271 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_ENCRYPT
];
4272 len
= mhp
->extlen
[SADB_EXT_KEY_ENCRYPT
];
4275 if (len
< sizeof(*key0
)) {
4276 ipseclog((LOG_DEBUG
, "key_setsaval: invalid encryption key ext len. len = %d\n", len
));
4280 switch (mhp
->msg
->sadb_msg_satype
) {
4281 case SADB_SATYPE_ESP
:
4282 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
4283 sav
->alg_enc
!= SADB_EALG_NULL
) {
4284 ipseclog((LOG_DEBUG
, "key_setsaval: invalid ESP algorithm.\n"));
4288 sav
->key_enc
= (struct sadb_key
*)key_newbuf(key0
, len
);
4289 if (sav
->key_enc
== NULL
) {
4290 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4295 case SADB_X_SATYPE_IPCOMP
:
4296 if (len
!= PFKEY_ALIGN8(sizeof(struct sadb_key
)))
4298 sav
->key_enc
= NULL
; /*just in case*/
4300 case SADB_SATYPE_AH
:
4306 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_enc value.\n"));
4314 switch (mhp
->msg
->sadb_msg_satype
) {
4315 case SADB_SATYPE_ESP
:
4317 algo
= esp_algorithm_lookup(sav
->alg_enc
);
4318 if (algo
&& algo
->ivlen
)
4319 sav
->ivlen
= (*algo
->ivlen
)(algo
, sav
);
4320 if (sav
->ivlen
== 0)
4322 KMALLOC_NOWAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4324 lck_mtx_unlock(sadb_mutex
);
4325 KMALLOC_WAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4326 lck_mtx_lock(sadb_mutex
);
4328 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4335 if (sav
->alg_enc
== SADB_X_EALG_AES_GCM
) {
4336 bzero(sav
->iv
, sav
->ivlen
);
4338 key_randomfill(sav
->iv
, sav
->ivlen
);
4342 case SADB_SATYPE_AH
:
4343 case SADB_X_SATYPE_IPCOMP
:
4346 ipseclog((LOG_DEBUG
, "key_setsaval: invalid SA type.\n"));
4353 sav
->created
= tv
.tv_sec
;
4355 /* make lifetime for CURRENT */
4356 KMALLOC_NOWAIT(sav
->lft_c
, struct sadb_lifetime
*,
4357 sizeof(struct sadb_lifetime
));
4358 if (sav
->lft_c
== NULL
) {
4359 lck_mtx_unlock(sadb_mutex
);
4360 KMALLOC_WAIT(sav
->lft_c
, struct sadb_lifetime
*,
4361 sizeof(struct sadb_lifetime
));
4362 lck_mtx_lock(sadb_mutex
);
4363 if (sav
->lft_c
== NULL
) {
4364 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4372 sav
->lft_c
->sadb_lifetime_len
=
4373 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
4374 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
4375 sav
->lft_c
->sadb_lifetime_allocations
= 0;
4376 sav
->lft_c
->sadb_lifetime_bytes
= 0;
4377 sav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
4378 sav
->lft_c
->sadb_lifetime_usetime
= 0;
4380 /* lifetimes for HARD and SOFT */
4382 const struct sadb_lifetime
*lft0
;
4384 lft0
= (struct sadb_lifetime
*)
4385 (void *)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
4387 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
] < sizeof(*lft0
)) {
4388 ipseclog((LOG_DEBUG
, "key_setsaval: invalid hard lifetime ext len.\n"));
4392 sav
->lft_h
= (struct sadb_lifetime
*)key_newbuf(lft0
,
4394 if (sav
->lft_h
== NULL
) {
4395 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4399 /* to be initialize ? */
4402 lft0
= (struct sadb_lifetime
*)
4403 (void *)mhp
->ext
[SADB_EXT_LIFETIME_SOFT
];
4405 if (mhp
->extlen
[SADB_EXT_LIFETIME_SOFT
] < sizeof(*lft0
)) {
4406 ipseclog((LOG_DEBUG
, "key_setsaval: invalid soft lifetime ext len.\n"));
4410 sav
->lft_s
= (struct sadb_lifetime
*)key_newbuf(lft0
,
4412 if (sav
->lft_s
== NULL
) {
4413 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4417 /* to be initialize ? */
4424 /* initialization */
4425 if (sav
->replay
!= NULL
) {
4426 keydb_delsecreplay(sav
->replay
);
4429 if (sav
->key_auth
!= NULL
) {
4430 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
4431 KFREE(sav
->key_auth
);
4432 sav
->key_auth
= NULL
;
4434 if (sav
->key_enc
!= NULL
) {
4435 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
4436 KFREE(sav
->key_enc
);
4437 sav
->key_enc
= NULL
;
4440 bzero(sav
->sched
, sav
->schedlen
);
4444 if (sav
->iv
!= NULL
) {
4448 if (sav
->lft_c
!= NULL
) {
4452 if (sav
->lft_h
!= NULL
) {
4456 if (sav
->lft_s
!= NULL
) {
4465 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
4466 * You must update these if need.
4470 * does not modify mbuf. does not free mbuf on error.
4473 key_setsaval2(struct secasvar
*sav
,
4479 struct sadb_key
*key_auth
,
4480 u_int16_t key_auth_len
,
4481 struct sadb_key
*key_enc
,
4482 u_int16_t key_enc_len
,
4483 u_int16_t natt_port
,
4487 struct sadb_lifetime
*lifetime_hard
,
4488 struct sadb_lifetime
*lifetime_soft
)
4491 const struct esp_algorithm
*algo
;
4496 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4498 /* initialization */
4500 sav
->key_auth
= NULL
;
4501 sav
->key_enc
= NULL
;
4508 sav
->remote_ike_port
= 0;
4509 sav
->natt_last_activity
= natt_now
;
4510 sav
->natt_encapsulated_src_port
= 0;
4512 sav
->alg_auth
= alg_auth
;
4513 sav
->alg_enc
= alg_enc
;
4517 key_setspi(sav
, htonl(spi
));
4520 * Verify that a nat-traversal port was specified if
4521 * the nat-traversal flag is set.
4523 if ((sav
->flags
& SADB_X_EXT_NATT
) != 0) {
4524 if (natt_port
== 0) {
4525 ipseclog((LOG_DEBUG
, "key_setsaval2: natt port not set.\n"));
4529 sav
->remote_ike_port
= natt_port
;
4533 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
4534 * SADB_X_EXT_NATT is set and SADB_X_EXT_NATT_KEEPALIVE is not
4535 * set (we're not behind nat) - otherwise clear it.
4537 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
4538 if ((sav
->flags
& SADB_X_EXT_NATT
) == 0 ||
4539 (sav
->flags
& SADB_X_EXT_NATT_KEEPALIVE
) != 0)
4540 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
4543 if ((flags
& SADB_X_EXT_OLD
) == 0) {
4544 sav
->replay
= keydb_newsecreplay(replay
);
4545 if (sav
->replay
== NULL
) {
4546 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4552 /* Authentication keys */
4553 sav
->key_auth
= (__typeof__(sav
->key_auth
))key_newbuf(key_auth
, key_auth_len
);
4554 if (sav
->key_auth
== NULL
) {
4555 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4560 /* Encryption key */
4561 sav
->key_enc
= (__typeof__(sav
->key_enc
))key_newbuf(key_enc
, key_enc_len
);
4562 if (sav
->key_enc
== NULL
) {
4563 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4571 if (satype
== SADB_SATYPE_ESP
) {
4573 algo
= esp_algorithm_lookup(sav
->alg_enc
);
4574 if (algo
&& algo
->ivlen
)
4575 sav
->ivlen
= (*algo
->ivlen
)(algo
, sav
);
4576 if (sav
->ivlen
!= 0) {
4577 KMALLOC_NOWAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4579 lck_mtx_unlock(sadb_mutex
);
4580 KMALLOC_WAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4581 lck_mtx_lock(sadb_mutex
);
4583 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4589 if (sav
->alg_enc
== SADB_X_EALG_AES_GCM
) {
4590 bzero(sav
->iv
, sav
->ivlen
);
4592 key_randomfill(sav
->iv
, sav
->ivlen
);
4600 sav
->created
= tv
.tv_sec
;
4602 /* make lifetime for CURRENT */
4603 KMALLOC_NOWAIT(sav
->lft_c
, struct sadb_lifetime
*,
4604 sizeof(struct sadb_lifetime
));
4605 if (sav
->lft_c
== NULL
) {
4606 lck_mtx_unlock(sadb_mutex
);
4607 KMALLOC_WAIT(sav
->lft_c
, struct sadb_lifetime
*,
4608 sizeof(struct sadb_lifetime
));
4609 lck_mtx_lock(sadb_mutex
);
4610 if (sav
->lft_c
== NULL
) {
4611 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4619 sav
->lft_c
->sadb_lifetime_len
=
4620 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
4621 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
4622 sav
->lft_c
->sadb_lifetime_allocations
= 0;
4623 sav
->lft_c
->sadb_lifetime_bytes
= 0;
4624 sav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
4625 sav
->lft_c
->sadb_lifetime_usetime
= 0;
4627 /* lifetimes for HARD and SOFT */
4628 sav
->lft_h
= (__typeof__(sav
->lft_h
))key_newbuf(lifetime_hard
,
4629 sizeof(*lifetime_hard
));
4630 if (sav
->lft_h
== NULL
) {
4631 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4635 sav
->lft_s
= (__typeof__(sav
->lft_s
))key_newbuf(lifetime_soft
,
4636 sizeof(*lifetime_soft
));
4637 if (sav
->lft_s
== NULL
) {
4638 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4646 /* initialization */
4647 if (sav
->replay
!= NULL
) {
4648 keydb_delsecreplay(sav
->replay
);
4651 if (sav
->key_auth
!= NULL
) {
4652 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
4653 KFREE(sav
->key_auth
);
4654 sav
->key_auth
= NULL
;
4656 if (sav
->key_enc
!= NULL
) {
4657 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
4658 KFREE(sav
->key_enc
);
4659 sav
->key_enc
= NULL
;
4662 bzero(sav
->sched
, sav
->schedlen
);
4666 if (sav
->iv
!= NULL
) {
4670 if (sav
->lft_c
!= NULL
) {
4674 if (sav
->lft_h
!= NULL
) {
4678 if (sav
->lft_s
!= NULL
) {
4687 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
4693 struct secasvar
*sav
)
4696 int checkmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
4697 int mustmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
4701 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4703 /* check SPI value */
4704 switch (sav
->sah
->saidx
.proto
) {
4708 /* No reason to test if this is >= 0, because ntohl(sav->spi) is unsigned. */
4709 if (ntohl(sav
->spi
) <= 255) {
4710 ipseclog((LOG_DEBUG
,
4711 "key_mature: illegal range of SPI %u.\n",
4712 (u_int32_t
)ntohl(sav
->spi
)));
4719 switch (sav
->sah
->saidx
.proto
) {
4722 if ((sav
->flags
& SADB_X_EXT_OLD
)
4723 && (sav
->flags
& SADB_X_EXT_DERIV
)) {
4724 ipseclog((LOG_DEBUG
, "key_mature: "
4725 "invalid flag (derived) given to old-esp.\n"));
4728 if (sav
->alg_auth
== SADB_AALG_NONE
)
4736 if (sav
->flags
& SADB_X_EXT_DERIV
) {
4737 ipseclog((LOG_DEBUG
, "key_mature: "
4738 "invalid flag (derived) given to AH SA.\n"));
4741 if (sav
->alg_enc
!= SADB_EALG_NONE
) {
4742 ipseclog((LOG_DEBUG
, "key_mature: "
4743 "protocol and algorithm mismated.\n"));
4749 case IPPROTO_IPCOMP
:
4750 if (sav
->alg_auth
!= SADB_AALG_NONE
) {
4751 ipseclog((LOG_DEBUG
, "key_mature: "
4752 "protocol and algorithm mismated.\n"));
4755 if ((sav
->flags
& SADB_X_EXT_RAWCPI
) == 0
4756 && ntohl(sav
->spi
) >= 0x10000) {
4757 ipseclog((LOG_DEBUG
, "key_mature: invalid cpi for IPComp.\n"));
4764 ipseclog((LOG_DEBUG
, "key_mature: Invalid satype.\n"));
4765 return EPROTONOSUPPORT
;
4768 /* check authentication algorithm */
4769 if ((checkmask
& 2) != 0) {
4770 const struct ah_algorithm
*algo
;
4773 algo
= ah_algorithm_lookup(sav
->alg_auth
);
4775 ipseclog((LOG_DEBUG
,"key_mature: "
4776 "unknown authentication algorithm.\n"));
4780 /* algorithm-dependent check */
4782 keylen
= sav
->key_auth
->sadb_key_bits
;
4785 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
4786 ipseclog((LOG_DEBUG
,
4787 "key_mature: invalid AH key length %d "
4788 "(%d-%d allowed)\n",
4789 keylen
, algo
->keymin
, algo
->keymax
));
4794 if ((*algo
->mature
)(sav
)) {
4795 /* message generated in per-algorithm function*/
4798 mature
= SADB_SATYPE_AH
;
4801 if ((mustmask
& 2) != 0 && mature
!= SADB_SATYPE_AH
) {
4802 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for AH\n"));
4807 /* check encryption algorithm */
4808 if ((checkmask
& 1) != 0) {
4810 const struct esp_algorithm
*algo
;
4813 algo
= esp_algorithm_lookup(sav
->alg_enc
);
4815 ipseclog((LOG_DEBUG
, "key_mature: unknown encryption algorithm.\n"));
4819 /* algorithm-dependent check */
4821 keylen
= sav
->key_enc
->sadb_key_bits
;
4824 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
4825 ipseclog((LOG_DEBUG
,
4826 "key_mature: invalid ESP key length %d "
4827 "(%d-%d allowed)\n",
4828 keylen
, algo
->keymin
, algo
->keymax
));
4833 if ((*algo
->mature
)(sav
)) {
4834 /* message generated in per-algorithm function*/
4837 mature
= SADB_SATYPE_ESP
;
4840 if ((mustmask
& 1) != 0 && mature
!= SADB_SATYPE_ESP
) {
4841 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for ESP\n"));
4845 ipseclog((LOG_DEBUG
, "key_mature: ESP not supported in this configuration\n"));
4850 /* check compression algorithm */
4851 if ((checkmask
& 4) != 0) {
4852 const struct ipcomp_algorithm
*algo
;
4854 /* algorithm-dependent check */
4855 algo
= ipcomp_algorithm_lookup(sav
->alg_enc
);
4857 ipseclog((LOG_DEBUG
, "key_mature: unknown compression algorithm.\n"));
4862 key_sa_chgstate(sav
, SADB_SASTATE_MATURE
);
4868 * subroutine for SADB_GET and SADB_DUMP.
4870 static struct mbuf
*
4872 struct secasvar
*sav
,
4878 struct mbuf
*result
= NULL
, *tres
= NULL
, *m
;
4883 SADB_EXT_SA
, SADB_X_EXT_SA2
,
4884 SADB_EXT_LIFETIME_HARD
, SADB_EXT_LIFETIME_SOFT
,
4885 SADB_EXT_LIFETIME_CURRENT
, SADB_EXT_ADDRESS_SRC
,
4886 SADB_EXT_ADDRESS_DST
, SADB_EXT_ADDRESS_PROXY
, SADB_EXT_KEY_AUTH
,
4887 SADB_EXT_KEY_ENCRYPT
, SADB_EXT_IDENTITY_SRC
,
4888 SADB_EXT_IDENTITY_DST
, SADB_EXT_SENSITIVITY
,
4891 m
= key_setsadbmsg(type
, 0, satype
, seq
, pid
, sav
->refcnt
);
4896 for (i
= sizeof(dumporder
)/sizeof(dumporder
[0]) - 1; i
>= 0; i
--) {
4899 switch (dumporder
[i
]) {
4901 m
= key_setsadbsa(sav
);
4906 case SADB_X_EXT_SA2
:
4907 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
4908 sav
->replay
? sav
->replay
->count
: 0,
4909 sav
->sah
->saidx
.reqid
,
4915 case SADB_EXT_ADDRESS_SRC
:
4916 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
4917 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
4918 FULLMASK
, IPSEC_ULPROTO_ANY
);
4923 case SADB_EXT_ADDRESS_DST
:
4924 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
4925 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
4926 FULLMASK
, IPSEC_ULPROTO_ANY
);
4931 case SADB_EXT_KEY_AUTH
:
4934 l
= PFKEY_UNUNIT64(sav
->key_auth
->sadb_key_len
);
4938 case SADB_EXT_KEY_ENCRYPT
:
4941 l
= PFKEY_UNUNIT64(sav
->key_enc
->sadb_key_len
);
4945 case SADB_EXT_LIFETIME_CURRENT
:
4948 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_c
)->sadb_ext_len
);
4952 case SADB_EXT_LIFETIME_HARD
:
4955 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_h
)->sadb_ext_len
);
4959 case SADB_EXT_LIFETIME_SOFT
:
4962 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_s
)->sadb_ext_len
);
4966 case SADB_EXT_ADDRESS_PROXY
:
4967 case SADB_EXT_IDENTITY_SRC
:
4968 case SADB_EXT_IDENTITY_DST
:
4969 /* XXX: should we brought from SPD ? */
4970 case SADB_EXT_SENSITIVITY
:
4975 if ((!m
&& !p
) || (m
&& p
))
4978 M_PREPEND(tres
, l
, M_WAITOK
, 1);
4981 bcopy(p
, mtod(tres
, caddr_t
), l
);
4985 m
= key_alloc_mbuf(l
);
4988 m_copyback(m
, 0, l
, p
);
4996 m_cat(result
, tres
);
4998 if (sav
->sah
&& (sav
->sah
->outgoing_if
|| sav
->sah
->ipsec_if
)) {
4999 m
= key_setsadbipsecif(NULL
, ifindex2ifnet
[sav
->sah
->outgoing_if
], sav
->sah
->ipsec_if
, 0);
5005 if (result
->m_len
< sizeof(struct sadb_msg
)) {
5006 result
= m_pullup(result
, sizeof(struct sadb_msg
));
5011 result
->m_pkthdr
.len
= 0;
5012 for (m
= result
; m
; m
= m
->m_next
)
5013 result
->m_pkthdr
.len
+= m
->m_len
;
5015 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
5016 PFKEY_UNIT64(result
->m_pkthdr
.len
);
5027 * set data into sadb_msg.
5029 static struct mbuf
*
5042 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
5045 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
5046 if (m
&& len
> MHLEN
) {
5047 MCLGET(m
, M_DONTWAIT
);
5048 if ((m
->m_flags
& M_EXT
) == 0) {
5055 m
->m_pkthdr
.len
= m
->m_len
= len
;
5058 p
= mtod(m
, struct sadb_msg
*);
5061 p
->sadb_msg_version
= PF_KEY_V2
;
5062 p
->sadb_msg_type
= type
;
5063 p
->sadb_msg_errno
= 0;
5064 p
->sadb_msg_satype
= satype
;
5065 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
5066 p
->sadb_msg_reserved
= reserved
;
5067 p
->sadb_msg_seq
= seq
;
5068 p
->sadb_msg_pid
= (u_int32_t
)pid
;
5074 * copy secasvar data into sadb_address.
5076 static struct mbuf
*
5078 struct secasvar
*sav
)
5084 len
= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
5085 m
= key_alloc_mbuf(len
);
5086 if (!m
|| m
->m_next
) { /*XXX*/
5092 p
= mtod(m
, struct sadb_sa
*);
5095 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
5096 p
->sadb_sa_exttype
= SADB_EXT_SA
;
5097 p
->sadb_sa_spi
= sav
->spi
;
5098 p
->sadb_sa_replay
= (sav
->replay
!= NULL
? sav
->replay
->wsize
: 0);
5099 p
->sadb_sa_state
= sav
->state
;
5100 p
->sadb_sa_auth
= sav
->alg_auth
;
5101 p
->sadb_sa_encrypt
= sav
->alg_enc
;
5102 p
->sadb_sa_flags
= sav
->flags
;
5108 * set data into sadb_address.
5110 static struct mbuf
*
5113 struct sockaddr
*saddr
,
5118 struct sadb_address
*p
;
5121 len
= PFKEY_ALIGN8(sizeof(struct sadb_address
)) +
5122 PFKEY_ALIGN8(saddr
->sa_len
);
5123 m
= key_alloc_mbuf(len
);
5124 if (!m
|| m
->m_next
) { /*XXX*/
5130 p
= mtod(m
, struct sadb_address
*);
5133 p
->sadb_address_len
= PFKEY_UNIT64(len
);
5134 p
->sadb_address_exttype
= exttype
;
5135 p
->sadb_address_proto
= ul_proto
;
5136 if (prefixlen
== FULLMASK
) {
5137 switch (saddr
->sa_family
) {
5139 prefixlen
= sizeof(struct in_addr
) << 3;
5142 prefixlen
= sizeof(struct in6_addr
) << 3;
5148 p
->sadb_address_prefixlen
= prefixlen
;
5149 p
->sadb_address_reserved
= 0;
5152 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_address
)),
5158 static struct mbuf
*
5159 key_setsadbipsecif(ifnet_t internal_if
,
5160 ifnet_t outgoing_if
,
5165 struct sadb_x_ipsecif
*p
;
5168 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_ipsecif
));
5169 m
= key_alloc_mbuf(len
);
5170 if (!m
|| m
->m_next
) { /*XXX*/
5176 p
= mtod(m
, struct sadb_x_ipsecif
*);
5179 p
->sadb_x_ipsecif_len
= PFKEY_UNIT64(len
);
5180 p
->sadb_x_ipsecif_exttype
= SADB_X_EXT_IPSECIF
;
5182 if (internal_if
&& internal_if
->if_xname
)
5183 strlcpy(p
->sadb_x_ipsecif_internal_if
, internal_if
->if_xname
, IFXNAMSIZ
);
5184 if (outgoing_if
&& outgoing_if
->if_xname
)
5185 strlcpy(p
->sadb_x_ipsecif_outgoing_if
, outgoing_if
->if_xname
, IFXNAMSIZ
);
5186 if (ipsec_if
&& ipsec_if
->if_xname
)
5187 strlcpy(p
->sadb_x_ipsecif_ipsec_if
, ipsec_if
->if_xname
, IFXNAMSIZ
);
5189 p
->sadb_x_ipsecif_init_disabled
= init_disabled
;
5195 * set data into sadb_session_id
5197 static struct mbuf
*
5198 key_setsadbsession_id (u_int64_t session_ids
[])
5201 struct sadb_session_id
*p
;
5204 len
= PFKEY_ALIGN8(sizeof(*p
));
5205 m
= key_alloc_mbuf(len
);
5206 if (!m
|| m
->m_next
) { /*XXX*/
5212 p
= mtod(m
, __typeof__(p
));
5215 p
->sadb_session_id_len
= PFKEY_UNIT64(len
);
5216 p
->sadb_session_id_exttype
= SADB_EXT_SESSION_ID
;
5217 p
->sadb_session_id_v
[0] = session_ids
[0];
5218 p
->sadb_session_id_v
[1] = session_ids
[1];
5224 * copy stats data into sadb_sastat type.
5226 static struct mbuf
*
5227 key_setsadbsastat (u_int32_t dir
,
5228 struct sastat
*stats
,
5229 u_int32_t max_stats
)
5232 struct sadb_sastat
*p
;
5239 list_len
= sizeof(*stats
) * max_stats
;
5240 len
= PFKEY_ALIGN8(sizeof(*p
)) + PFKEY_ALIGN8(list_len
);
5241 m
= key_alloc_mbuf(len
);
5242 if (!m
|| m
->m_next
) { /*XXX*/
5248 p
= mtod(m
, __typeof__(p
));
5251 p
->sadb_sastat_len
= PFKEY_UNIT64(len
);
5252 p
->sadb_sastat_exttype
= SADB_EXT_SASTAT
;
5253 p
->sadb_sastat_dir
= dir
;
5254 p
->sadb_sastat_list_len
= max_stats
;
5257 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(*p
)),
5266 * set data into sadb_ident.
5268 static struct mbuf
*
5277 struct sadb_ident
*p
;
5280 len
= PFKEY_ALIGN8(sizeof(struct sadb_ident
)) + PFKEY_ALIGN8(stringlen
);
5281 m
= key_alloc_mbuf(len
);
5282 if (!m
|| m
->m_next
) { /*XXX*/
5288 p
= mtod(m
, struct sadb_ident
*);
5291 p
->sadb_ident_len
= PFKEY_UNIT64(len
);
5292 p
->sadb_ident_exttype
= exttype
;
5293 p
->sadb_ident_type
= idtype
;
5294 p
->sadb_ident_reserved
= 0;
5295 p
->sadb_ident_id
= id
;
5298 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_ident
)),
5306 * set data into sadb_x_sa2.
5308 static struct mbuf
*
5316 struct sadb_x_sa2
*p
;
5319 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_sa2
));
5320 m
= key_alloc_mbuf(len
);
5321 if (!m
|| m
->m_next
) { /*XXX*/
5327 p
= mtod(m
, struct sadb_x_sa2
*);
5330 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
5331 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
5332 p
->sadb_x_sa2_mode
= mode
;
5333 p
->sadb_x_sa2_reserved1
= 0;
5334 p
->sadb_x_sa2_reserved2
= 0;
5335 p
->sadb_x_sa2_sequence
= seq
;
5336 p
->sadb_x_sa2_reqid
= reqid
;
5337 p
->sadb_x_sa2_flags
= flags
;
5343 * set data into sadb_x_policy
5345 static struct mbuf
*
5352 struct sadb_x_policy
*p
;
5355 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_policy
));
5356 m
= key_alloc_mbuf(len
);
5357 if (!m
|| m
->m_next
) { /*XXX*/
5363 p
= mtod(m
, struct sadb_x_policy
*);
5366 p
->sadb_x_policy_len
= PFKEY_UNIT64(len
);
5367 p
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
5368 p
->sadb_x_policy_type
= type
;
5369 p
->sadb_x_policy_dir
= dir
;
5370 p
->sadb_x_policy_id
= id
;
5377 * copy a buffer into the new buffer allocated.
5386 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5387 KMALLOC_NOWAIT(new, caddr_t
, len
);
5389 lck_mtx_unlock(sadb_mutex
);
5390 KMALLOC_WAIT(new, caddr_t
, len
);
5391 lck_mtx_lock(sadb_mutex
);
5393 ipseclog((LOG_DEBUG
, "key_newbuf: No more memory.\n"));
5397 bcopy(src
, new, len
);
5402 /* compare my own address
5403 * OUT: 1: true, i.e. my address.
5408 struct sockaddr
*sa
)
5411 struct sockaddr_in
*sin
;
5412 struct in_ifaddr
*ia
;
5417 panic("key_ismyaddr: NULL pointer is passed.\n");
5419 switch (sa
->sa_family
) {
5422 lck_rw_lock_shared(in_ifaddr_rwlock
);
5423 sin
= (struct sockaddr_in
*)(void *)sa
;
5424 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
5425 ia
= ia
->ia_link
.tqe_next
) {
5426 IFA_LOCK_SPIN(&ia
->ia_ifa
);
5427 if (sin
->sin_family
== ia
->ia_addr
.sin_family
&&
5428 sin
->sin_len
== ia
->ia_addr
.sin_len
&&
5429 sin
->sin_addr
.s_addr
== ia
->ia_addr
.sin_addr
.s_addr
)
5431 IFA_UNLOCK(&ia
->ia_ifa
);
5432 lck_rw_done(in_ifaddr_rwlock
);
5435 IFA_UNLOCK(&ia
->ia_ifa
);
5437 lck_rw_done(in_ifaddr_rwlock
);
5442 return key_ismyaddr6((struct sockaddr_in6
*)(void *)sa
);
5451 * compare my own address for IPv6.
5454 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
5456 #include <netinet6/in6_var.h>
5460 struct sockaddr_in6
*sin6
)
5462 struct in6_ifaddr
*ia
;
5463 struct in6_multi
*in6m
;
5465 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
5466 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
5467 IFA_LOCK(&ia
->ia_ifa
);
5468 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
5469 (struct sockaddr
*)&ia
->ia_addr
, 0) == 0) {
5470 IFA_UNLOCK(&ia
->ia_ifa
);
5471 lck_rw_done(&in6_ifaddr_rwlock
);
5474 IFA_UNLOCK(&ia
->ia_ifa
);
5478 * XXX why do we care about multlicast here while we don't care
5479 * about IPv4 multicast??
5483 in6_multihead_lock_shared();
5484 IN6_LOOKUP_MULTI(&sin6
->sin6_addr
, ia
->ia_ifp
, in6m
);
5485 in6_multihead_lock_done();
5487 lck_rw_done(&in6_ifaddr_rwlock
);
5492 lck_rw_done(&in6_ifaddr_rwlock
);
5494 /* loopback, just for safety */
5495 if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
5503 * compare two secasindex structure.
5504 * flag can specify to compare 2 saidxes.
5505 * compare two secasindex structure without both mode and reqid.
5506 * don't compare port.
5508 * saidx0: source, it can be in SAD.
5516 struct secasindex
*saidx0
,
5517 struct secasindex
*saidx1
,
5521 if (saidx0
== NULL
&& saidx1
== NULL
)
5524 if (saidx0
== NULL
|| saidx1
== NULL
)
5527 if (saidx0
->ipsec_ifindex
!= 0 && saidx0
->ipsec_ifindex
!= saidx1
->ipsec_ifindex
)
5530 if (saidx0
->proto
!= saidx1
->proto
)
5533 if (flag
== CMP_EXACTLY
) {
5534 if (saidx0
->mode
!= saidx1
->mode
)
5536 if (saidx0
->reqid
!= saidx1
->reqid
)
5538 if (bcmp(&saidx0
->src
, &saidx1
->src
, saidx0
->src
.ss_len
) != 0 ||
5539 bcmp(&saidx0
->dst
, &saidx1
->dst
, saidx0
->dst
.ss_len
) != 0)
5543 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
5544 if (flag
& CMP_REQID
) {
5546 * If reqid of SPD is non-zero, unique SA is required.
5547 * The result must be of same reqid in this case.
5549 if (saidx1
->reqid
!= 0 && saidx0
->reqid
!= saidx1
->reqid
)
5553 if (flag
& CMP_MODE
) {
5554 if (saidx0
->mode
!= IPSEC_MODE_ANY
5555 && saidx0
->mode
!= saidx1
->mode
)
5559 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->src
,
5560 (struct sockaddr
*)&saidx1
->src
, flag
& CMP_PORT
? 1 : 0) != 0) {
5563 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->dst
,
5564 (struct sockaddr
*)&saidx1
->dst
, flag
& CMP_PORT
? 1 : 0) != 0) {
5573 * compare two secindex structure exactly.
5575 * spidx0: source, it is often in SPD.
5576 * spidx1: object, it is often from PFKEY message.
5582 key_cmpspidx_exactly(
5583 struct secpolicyindex
*spidx0
,
5584 struct secpolicyindex
*spidx1
)
5587 if (spidx0
== NULL
&& spidx1
== NULL
)
5590 if (spidx0
== NULL
|| spidx1
== NULL
)
5593 if (spidx0
->prefs
!= spidx1
->prefs
5594 || spidx0
->prefd
!= spidx1
->prefd
5595 || spidx0
->ul_proto
!= spidx1
->ul_proto
5596 || spidx0
->internal_if
!= spidx1
->internal_if
)
5599 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src
,
5600 (struct sockaddr
*)&spidx1
->src
, 1) != 0) {
5603 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst
,
5604 (struct sockaddr
*)&spidx1
->dst
, 1) != 0) {
5608 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src_range
.start
,
5609 (struct sockaddr
*)&spidx1
->src_range
.start
, 1) != 0) {
5612 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src_range
.end
,
5613 (struct sockaddr
*)&spidx1
->src_range
.end
, 1) != 0) {
5616 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst_range
.start
,
5617 (struct sockaddr
*)&spidx1
->dst_range
.start
, 1) != 0) {
5620 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst_range
.end
,
5621 (struct sockaddr
*)&spidx1
->dst_range
.end
, 1) != 0) {
5629 * compare two secindex structure with mask.
5631 * spidx0: source, it is often in SPD.
5632 * spidx1: object, it is often from IP header.
5638 key_cmpspidx_withmask(
5639 struct secpolicyindex
*spidx0
,
5640 struct secpolicyindex
*spidx1
)
5642 int spidx0_src_is_range
= 0;
5643 int spidx0_dst_is_range
= 0;
5646 if (spidx0
== NULL
&& spidx1
== NULL
)
5649 if (spidx0
== NULL
|| spidx1
== NULL
)
5652 if (spidx0
->src_range
.start
.ss_len
> 0)
5653 spidx0_src_is_range
= 1;
5655 if (spidx0
->dst_range
.start
.ss_len
> 0)
5656 spidx0_dst_is_range
= 1;
5658 if ((spidx0_src_is_range
? spidx0
->src_range
.start
.ss_family
: spidx0
->src
.ss_family
) != spidx1
->src
.ss_family
||
5659 (spidx0_dst_is_range
? spidx0
->dst_range
.start
.ss_family
: spidx0
->dst
.ss_family
) != spidx1
->dst
.ss_family
||
5660 (spidx0_src_is_range
? spidx0
->src_range
.start
.ss_len
: spidx0
->src
.ss_len
) != spidx1
->src
.ss_len
||
5661 (spidx0_dst_is_range
? spidx0
->dst_range
.start
.ss_len
: spidx0
->dst
.ss_len
) != spidx1
->dst
.ss_len
)
5664 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
5665 if (spidx0
->ul_proto
!= (u_int16_t
)IPSEC_ULPROTO_ANY
5666 && spidx0
->ul_proto
!= spidx1
->ul_proto
)
5669 /* If spidx1 specifies interface, ignore src addr */
5670 if (spidx1
->internal_if
!= NULL
) {
5671 if (spidx0
->internal_if
== NULL
5672 || spidx0
->internal_if
!= spidx1
->internal_if
)
5675 /* Still check ports */
5676 switch (spidx0
->src
.ss_family
) {
5678 if (spidx0_src_is_range
&&
5679 (satosin(&spidx1
->src
)->sin_port
< satosin(&spidx0
->src_range
.start
)->sin_port
5680 || satosin(&spidx1
->src
)->sin_port
> satosin(&spidx0
->src_range
.end
)->sin_port
))
5682 else if (satosin(&spidx0
->src
)->sin_port
!= IPSEC_PORT_ANY
5683 && satosin(&spidx0
->src
)->sin_port
!=
5684 satosin(&spidx1
->src
)->sin_port
)
5688 if (spidx0_src_is_range
&&
5689 (satosin6(&spidx1
->src
)->sin6_port
< satosin6(&spidx0
->src_range
.start
)->sin6_port
5690 || satosin6(&spidx1
->src
)->sin6_port
> satosin6(&spidx0
->src_range
.end
)->sin6_port
))
5692 else if (satosin6(&spidx0
->src
)->sin6_port
!= IPSEC_PORT_ANY
5693 && satosin6(&spidx0
->src
)->sin6_port
!=
5694 satosin6(&spidx1
->src
)->sin6_port
)
5700 } else if (spidx0_src_is_range
) {
5701 if (!key_is_addr_in_range(&spidx1
->src
, &spidx0
->src_range
))
5704 switch (spidx0
->src
.ss_family
) {
5706 if (satosin(&spidx0
->src
)->sin_port
!= IPSEC_PORT_ANY
5707 && satosin(&spidx0
->src
)->sin_port
!=
5708 satosin(&spidx1
->src
)->sin_port
)
5710 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->src
)->sin_addr
,
5711 (caddr_t
)&satosin(&spidx1
->src
)->sin_addr
, spidx0
->prefs
))
5715 if (satosin6(&spidx0
->src
)->sin6_port
!= IPSEC_PORT_ANY
5716 && satosin6(&spidx0
->src
)->sin6_port
!=
5717 satosin6(&spidx1
->src
)->sin6_port
)
5720 * scope_id check. if sin6_scope_id is 0, we regard it
5721 * as a wildcard scope, which matches any scope zone ID.
5723 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
5724 satosin6(&spidx1
->src
)->sin6_scope_id
&&
5725 satosin6(&spidx0
->src
)->sin6_scope_id
!=
5726 satosin6(&spidx1
->src
)->sin6_scope_id
)
5728 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->src
)->sin6_addr
,
5729 (caddr_t
)&satosin6(&spidx1
->src
)->sin6_addr
, spidx0
->prefs
))
5734 if (bcmp(&spidx0
->src
, &spidx1
->src
, spidx0
->src
.ss_len
) != 0)
5740 if (spidx0_dst_is_range
) {
5741 if (!key_is_addr_in_range(&spidx1
->dst
, &spidx0
->dst_range
))
5744 switch (spidx0
->dst
.ss_family
) {
5746 if (satosin(&spidx0
->dst
)->sin_port
!= IPSEC_PORT_ANY
5747 && satosin(&spidx0
->dst
)->sin_port
!=
5748 satosin(&spidx1
->dst
)->sin_port
)
5750 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->dst
)->sin_addr
,
5751 (caddr_t
)&satosin(&spidx1
->dst
)->sin_addr
, spidx0
->prefd
))
5755 if (satosin6(&spidx0
->dst
)->sin6_port
!= IPSEC_PORT_ANY
5756 && satosin6(&spidx0
->dst
)->sin6_port
!=
5757 satosin6(&spidx1
->dst
)->sin6_port
)
5760 * scope_id check. if sin6_scope_id is 0, we regard it
5761 * as a wildcard scope, which matches any scope zone ID.
5763 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
5764 satosin6(&spidx1
->src
)->sin6_scope_id
&&
5765 satosin6(&spidx0
->dst
)->sin6_scope_id
!=
5766 satosin6(&spidx1
->dst
)->sin6_scope_id
)
5768 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->dst
)->sin6_addr
,
5769 (caddr_t
)&satosin6(&spidx1
->dst
)->sin6_addr
, spidx0
->prefd
))
5774 if (bcmp(&spidx0
->dst
, &spidx1
->dst
, spidx0
->dst
.ss_len
) != 0)
5780 /* XXX Do we check other field ? e.g. flowinfo */
5786 key_is_addr_in_range(struct sockaddr_storage
*addr
, struct secpolicyaddrrange
*addr_range
)
5790 if (addr
== NULL
|| addr_range
== NULL
)
5793 /* Must be greater than or equal to start */
5794 cmp
= key_sockaddrcmp((struct sockaddr
*)addr
, (struct sockaddr
*)&addr_range
->start
, 1);
5795 if (cmp
!= 0 && cmp
!= 1)
5798 /* Must be less than or equal to end */
5799 cmp
= key_sockaddrcmp((struct sockaddr
*)addr
, (struct sockaddr
*)&addr_range
->end
, 1);
5800 if (cmp
!= 0 && cmp
!= -1)
5811 2: Not comparable or error
5815 struct sockaddr
*sa1
,
5816 struct sockaddr
*sa2
,
5820 int port_result
= 0;
5822 if (sa1
->sa_family
!= sa2
->sa_family
|| sa1
->sa_len
!= sa2
->sa_len
)
5825 if (sa1
->sa_len
== 0)
5828 switch (sa1
->sa_family
) {
5830 if (sa1
->sa_len
!= sizeof(struct sockaddr_in
))
5833 result
= memcmp(&satosin(sa1
)->sin_addr
.s_addr
, &satosin(sa2
)->sin_addr
.s_addr
, sizeof(satosin(sa1
)->sin_addr
.s_addr
));
5836 if (satosin(sa1
)->sin_port
< satosin(sa2
)->sin_port
) {
5838 } else if (satosin(sa1
)->sin_port
> satosin(sa2
)->sin_port
) {
5843 result
= port_result
;
5844 else if ((result
> 0 && port_result
< 0) || (result
< 0 && port_result
> 0))
5850 if (sa1
->sa_len
!= sizeof(struct sockaddr_in6
))
5851 return 2; /*EINVAL*/
5853 if (satosin6(sa1
)->sin6_scope_id
!=
5854 satosin6(sa2
)->sin6_scope_id
) {
5858 result
= memcmp(&satosin6(sa1
)->sin6_addr
.s6_addr
[0], &satosin6(sa2
)->sin6_addr
.s6_addr
[0], sizeof(struct in6_addr
));
5861 if (satosin6(sa1
)->sin6_port
< satosin6(sa2
)->sin6_port
) {
5863 } else if (satosin6(sa1
)->sin6_port
> satosin6(sa2
)->sin6_port
) {
5868 result
= port_result
;
5869 else if ((result
> 0 && port_result
< 0) || (result
< 0 && port_result
> 0))
5875 result
= memcmp(sa1
, sa2
, sa1
->sa_len
);
5879 if (result
< 0) result
= -1;
5880 else if (result
> 0) result
= 1;
5886 * compare two buffers with mask.
5890 * bits: Number of bits to compare
5903 /* XXX: This could be considerably faster if we compare a word
5904 * at a time, but it is complicated on LSB Endian machines */
5906 /* Handle null pointers */
5907 if (p1
== NULL
|| p2
== NULL
)
5917 mask
= ~((1<<(8-bits
))-1);
5918 if ((*p1
& mask
) != (*p2
& mask
))
5921 return 1; /* Match! */
5926 * scanning SPD and SAD to check status for each entries,
5927 * and do to remove or to expire.
5928 * XXX: year 2038 problem may remain.
5930 int key_timehandler_debug
= 0;
5931 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;
5932 u_int64_t total_sav_count
= 0;
5934 key_timehandler(void)
5938 struct secpolicy
**spbuf
= NULL
, **spptr
= NULL
;
5939 struct secasvar
**savexbuf
= NULL
, **savexptr
= NULL
;
5940 struct secasvar
**savkabuf
= NULL
, **savkaptr
= NULL
;
5941 int spbufcount
= 0, savbufcount
= 0, spcount
= 0, savexcount
= 0, savkacount
= 0, cnt
;
5942 int stop_handler
= 1; /* stop the timehandler */
5946 /* pre-allocate buffers before taking the lock */
5947 /* if allocation failures occur - portions of the processing will be skipped */
5948 if ((spbufcount
= ipsec_policy_count
) != 0) {
5950 KMALLOC_WAIT(spbuf
, struct secpolicy
**, spbufcount
* sizeof(struct secpolicy
*));
5954 if ((savbufcount
= ipsec_sav_count
) != 0) {
5956 KMALLOC_WAIT(savexbuf
, struct secasvar
**, savbufcount
* sizeof(struct secasvar
*));
5958 savexptr
= savexbuf
;
5959 KMALLOC_WAIT(savkabuf
, struct secasvar
**, savbufcount
* sizeof(struct secasvar
*));
5961 savkaptr
= savkabuf
;
5963 lck_mtx_lock(sadb_mutex
);
5967 struct secpolicy
*sp
, *nextsp
;
5969 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
5970 for (sp
= LIST_FIRST(&sptree
[dir
]);
5974 /* don't prevent timehandler from stopping for generate policy */
5975 if (sp
->policy
!= IPSEC_POLICY_GENERATE
)
5978 nextsp
= LIST_NEXT(sp
, chain
);
5980 if (sp
->state
== IPSEC_SPSTATE_DEAD
) {
5981 key_freesp(sp
, KEY_SADB_LOCKED
);
5985 if (sp
->lifetime
== 0 && sp
->validtime
== 0)
5987 if (spbuf
&& spcount
< spbufcount
) {
5988 /* the deletion will occur next time */
5990 && tv
.tv_sec
- sp
->created
> sp
->lifetime
)
5992 && tv
.tv_sec
- sp
->lastused
> sp
->validtime
)) {
5993 //key_spdexpire(sp);
5994 sp
->state
= IPSEC_SPSTATE_DEAD
;
6006 struct secashead
*sah
, *nextsah
;
6007 struct secasvar
*sav
, *nextsav
;
6009 for (sah
= LIST_FIRST(&sahtree
);
6014 nextsah
= LIST_NEXT(sah
, chain
);
6016 /* if sah has been dead, then delete it and process next sah. */
6017 if (sah
->state
== SADB_SASTATE_DEAD
) {
6023 if (LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]) == NULL
&&
6024 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]) == NULL
&&
6025 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]) == NULL
&&
6026 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]) == NULL
) {
6032 if (savbufcount
== 0) {
6038 /* if LARVAL entry doesn't become MATURE, delete it. */
6039 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]);
6045 nextsav
= LIST_NEXT(sav
, chain
);
6047 if (sav
->lft_h
!= NULL
) {
6048 /* If a hard lifetime is defined for the LARVAL SA, use it */
6049 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0
6050 && tv
.tv_sec
- sav
->created
> sav
->lft_h
->sadb_lifetime_addtime
) {
6051 if (sav
->always_expire
) {
6052 key_send_delete(sav
);
6055 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6056 key_freesav(sav
, KEY_SADB_LOCKED
);
6061 if (tv
.tv_sec
- sav
->created
> key_larval_lifetime
) {
6062 key_freesav(sav
, KEY_SADB_LOCKED
);
6068 * If this is a NAT traversal SA with no activity,
6069 * we need to send a keep alive.
6071 * Performed outside of the loop before so we will
6072 * only ever send one keepalive. The first SA on
6073 * the list is the one that will be used for sending
6074 * traffic, so this is the one we use for determining
6075 * when to send the keepalive.
6077 if (savkabuf
&& savkacount
< savbufcount
) {
6078 sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]); //%%% should we check dying list if this is empty???
6079 if (sav
&& (natt_keepalive_interval
|| sav
->natt_interval
) &&
6080 (sav
->flags
& (SADB_X_EXT_NATT_KEEPALIVE
| SADB_X_EXT_ESP_KEEPALIVE
)) != 0) {
6088 * check MATURE entry to start to send expire message
6091 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]);
6097 nextsav
= LIST_NEXT(sav
, chain
);
6099 /* we don't need to check. */
6100 if (sav
->lft_s
== NULL
)
6104 if (sav
->lft_c
== NULL
) {
6105 ipseclog((LOG_DEBUG
,"key_timehandler: "
6106 "There is no CURRENT time, why?\n"));
6110 /* check SOFT lifetime */
6111 if (sav
->lft_s
->sadb_lifetime_addtime
!= 0
6112 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
6114 * If always_expire is set, expire. Otherwise,
6115 * if the SA has not been used, delete immediately.
6117 if (sav
->lft_c
->sadb_lifetime_usetime
== 0
6118 && sav
->always_expire
== 0) {
6119 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6120 key_freesav(sav
, KEY_SADB_LOCKED
);
6122 } else if (savexbuf
&& savexcount
< savbufcount
) {
6123 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
6130 /* check SOFT lifetime by bytes */
6132 * XXX I don't know the way to delete this SA
6133 * when new SA is installed. Caution when it's
6134 * installed too big lifetime by time.
6136 else if (savexbuf
&& savexcount
< savbufcount
6137 && sav
->lft_s
->sadb_lifetime_bytes
!= 0
6138 && sav
->lft_s
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
6141 * XXX If we keep to send expire
6142 * message in the status of
6143 * DYING. Do remove below code.
6146 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
6153 /* check DYING entry to change status to DEAD. */
6154 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]);
6160 nextsav
= LIST_NEXT(sav
, chain
);
6162 /* we don't need to check. */
6163 if (sav
->lft_h
== NULL
)
6167 if (sav
->lft_c
== NULL
) {
6168 ipseclog((LOG_DEBUG
, "key_timehandler: "
6169 "There is no CURRENT time, why?\n"));
6173 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0
6174 && tv
.tv_sec
- sav
->created
> sav
->lft_h
->sadb_lifetime_addtime
) {
6175 if (sav
->always_expire
) {
6176 key_send_delete(sav
);
6179 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6180 key_freesav(sav
, KEY_SADB_LOCKED
);
6184 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
6185 else if (savbuf
&& savexcount
< savbufcount
6186 && sav
->lft_s
!= NULL
6187 && sav
->lft_s
->sadb_lifetime_addtime
!= 0
6188 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
6190 * XXX: should be checked to be
6191 * installed the valid SA.
6195 * If there is no SA then sending
6204 /* check HARD lifetime by bytes */
6205 else if (sav
->lft_h
->sadb_lifetime_bytes
!= 0
6206 && sav
->lft_h
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
6207 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6208 key_freesav(sav
, KEY_SADB_LOCKED
);
6213 /* delete entry in DEAD */
6214 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]);
6220 nextsav
= LIST_NEXT(sav
, chain
);
6223 if (sav
->state
!= SADB_SASTATE_DEAD
) {
6224 ipseclog((LOG_DEBUG
, "key_timehandler: "
6225 "invalid sav->state "
6226 "(queue: %d SA: %d): "
6228 SADB_SASTATE_DEAD
, sav
->state
));
6232 * do not call key_freesav() here.
6233 * sav should already be freed, and sav->refcnt
6234 * shows other references to sav
6235 * (such as from SPD).
6241 if (++key_timehandler_debug
>= 300) {
6242 if (key_debug_level
) {
6243 printf("%s: total stats for %u calls\n", __FUNCTION__
, key_timehandler_debug
);
6244 printf("%s: walked %u SPDs\n", __FUNCTION__
, spd_count
);
6245 printf("%s: walked %llu SAs: LARVAL SAs %u, MATURE SAs %u, DYING SAs %u, DEAD SAs %u\n", __FUNCTION__
,
6246 total_sav_count
, larval_sav_count
, mature_sav_count
, dying_sav_count
, dead_sav_count
);
6247 printf("%s: walked %u SAHs: DEAD SAHs %u, EMPTY SAHs %u\n", __FUNCTION__
,
6248 sah_count
, dead_sah_count
, empty_sah_count
);
6249 if (sah_search_calls
) {
6250 printf("%s: SAH search cost %d iters per call\n", __FUNCTION__
,
6251 (sah_search_count
/sah_search_calls
));
6257 empty_sah_count
= 0;
6258 larval_sav_count
= 0;
6259 mature_sav_count
= 0;
6260 dying_sav_count
= 0;
6262 total_sav_count
= 0;
6263 sah_search_count
= 0;
6264 sah_search_calls
= 0;
6265 key_timehandler_debug
= 0;
6267 #ifndef IPSEC_NONBLOCK_ACQUIRE
6270 struct secacq
*acq
, *nextacq
;
6272 for (acq
= LIST_FIRST(&acqtree
);
6277 nextacq
= LIST_NEXT(acq
, chain
);
6279 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
6280 && __LIST_CHAINED(acq
)) {
6281 LIST_REMOVE(acq
, chain
);
6290 struct secspacq
*acq
, *nextacq
;
6292 for (acq
= LIST_FIRST(&spacqtree
);
6297 nextacq
= LIST_NEXT(acq
, chain
);
6299 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
6300 && __LIST_CHAINED(acq
)) {
6301 LIST_REMOVE(acq
, chain
);
6307 /* initialize random seed */
6308 if (key_tick_init_random
++ > key_int_random
) {
6309 key_tick_init_random
= 0;
6313 uint64_t acc_sleep_time
= 0;
6314 absolutetime_to_nanoseconds(mach_absolutetime_asleep
, &acc_sleep_time
);
6315 natt_now
= ++up_time
+ (acc_sleep_time
/ NSEC_PER_SEC
);
6317 lck_mtx_unlock(sadb_mutex
);
6319 /* send messages outside of sadb_mutex */
6320 if (spbuf
&& spcount
> 0) {
6323 key_spdexpire(*(--spptr
));
6325 if (savkabuf
&& savkacount
> 0) {
6326 struct secasvar
**savkaptr_sav
= savkaptr
;
6327 int cnt_send
= savkacount
;
6329 while (cnt_send
--) {
6330 if (ipsec_send_natt_keepalive(*(--savkaptr
))) {
6331 // <rdar://6768487> iterate (all over again) and update timestamps
6332 struct secasvar
**savkaptr_update
= savkaptr_sav
;
6333 int cnt_update
= savkacount
;
6334 while (cnt_update
--) {
6335 key_update_natt_keepalive_timestamp(*savkaptr
,
6336 *(--savkaptr_update
));
6341 if (savexbuf
&& savexcount
> 0) {
6344 key_expire(*(--savexptr
));
6347 /* decrement ref counts and free buffers */
6348 lck_mtx_lock(sadb_mutex
);
6351 key_freesp(*spptr
++, KEY_SADB_LOCKED
);
6355 while (savkacount
--)
6356 key_freesav(*savkaptr
++, KEY_SADB_LOCKED
);
6360 while (savexcount
--)
6361 key_freesav(*savexptr
++, KEY_SADB_LOCKED
);
6366 key_timehandler_running
= 0;
6367 /* Turn on the ipsec bypass */
6370 /* do exchange to tick time !! */
6371 (void)timeout((void *)key_timehandler
, (void *)0, hz
);
6374 lck_mtx_unlock(sadb_mutex
);
6379 * to initialize a seed for random()
6385 /* Our PRNG is based on Yarrow and doesn't need to be seeded */
6392 srandom(tv
.tv_usec
);
6403 key_randomfill(&value
, sizeof(value
));
6413 cc_rand_generate(p
, l
);
6417 static int warn
= 1;
6420 n
= (size_t)read_random(p
, (u_int
)l
);
6424 bcopy(&v
, (u_int8_t
*)p
+ n
,
6425 l
- n
< sizeof(v
) ? l
- n
: sizeof(v
));
6429 printf("WARNING: pseudo-random number generator "
6430 "used for IPsec processing\n");
6438 * map SADB_SATYPE_* to IPPROTO_*.
6439 * if satype == SADB_SATYPE then satype is mapped to ~0.
6441 * 0: invalid satype.
6448 case SADB_SATYPE_UNSPEC
:
6449 return IPSEC_PROTO_ANY
;
6450 case SADB_SATYPE_AH
:
6452 case SADB_SATYPE_ESP
:
6454 case SADB_X_SATYPE_IPCOMP
:
6455 return IPPROTO_IPCOMP
;
6463 * map IPPROTO_* to SADB_SATYPE_*
6465 * 0: invalid protocol type.
6473 return SADB_SATYPE_AH
;
6475 return SADB_SATYPE_ESP
;
6476 case IPPROTO_IPCOMP
:
6477 return SADB_X_SATYPE_IPCOMP
;
6485 key_get_ipsec_if_from_message (const struct sadb_msghdr
*mhp
, int message_type
)
6487 struct sadb_x_ipsecif
*ipsecifopts
= NULL
;
6488 ifnet_t ipsec_if
= NULL
;
6490 ipsecifopts
= (struct sadb_x_ipsecif
*)(void *)mhp
->ext
[message_type
];
6491 if (ipsecifopts
!= NULL
) {
6492 if (ipsecifopts
->sadb_x_ipsecif_ipsec_if
[0]) {
6493 ifnet_find_by_name(ipsecifopts
->sadb_x_ipsecif_ipsec_if
, &ipsec_if
);
6501 key_get_outgoing_ifindex_from_message (const struct sadb_msghdr
*mhp
, int message_type
)
6503 struct sadb_x_ipsecif
*ipsecifopts
= NULL
;
6504 ifnet_t outgoing_if
= NULL
;
6506 ipsecifopts
= (struct sadb_x_ipsecif
*)(void *)mhp
->ext
[message_type
];
6507 if (ipsecifopts
!= NULL
) {
6508 if (ipsecifopts
->sadb_x_ipsecif_outgoing_if
[0]) {
6509 ifnet_find_by_name(ipsecifopts
->sadb_x_ipsecif_outgoing_if
, &outgoing_if
);
6513 return outgoing_if
? outgoing_if
->if_index
: 0;
6518 * SADB_GETSPI processing is to receive
6519 * <base, (SA2), src address, dst address, (SPI range)>
6520 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
6521 * tree with the status of LARVAL, and send
6522 * <base, SA(*), address(SD)>
6525 * IN: mhp: pointer to the pointer to each header.
6526 * OUT: NULL if fail.
6527 * other if success, return pointer to the message to send.
6533 const struct sadb_msghdr
*mhp
)
6535 struct sadb_address
*src0
, *dst0
;
6536 struct secasindex saidx
;
6537 struct secashead
*newsah
;
6538 struct secasvar
*newsav
;
6539 ifnet_t ipsec_if
= NULL
;
6546 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6549 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6550 panic("key_getspi: NULL pointer is passed.\n");
6552 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6553 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
6554 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
6555 return key_senderror(so
, m
, EINVAL
);
6557 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6558 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
6559 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
6560 return key_senderror(so
, m
, EINVAL
);
6562 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
6563 mode
= ((struct sadb_x_sa2
*)
6564 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
6565 reqid
= ((struct sadb_x_sa2
*)
6566 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
6568 mode
= IPSEC_MODE_ANY
;
6572 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
6573 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
6575 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
6577 /* map satype to proto */
6578 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6579 ipseclog((LOG_DEBUG
, "key_getspi: invalid satype is passed.\n"));
6580 return key_senderror(so
, m
, EINVAL
);
6583 /* make sure if port number is zero. */
6584 switch (((struct sockaddr
*)(src0
+ 1))->sa_family
) {
6586 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
6587 sizeof(struct sockaddr_in
))
6588 return key_senderror(so
, m
, EINVAL
);
6589 ((struct sockaddr_in
*)(void *)(src0
+ 1))->sin_port
= 0;
6592 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
6593 sizeof(struct sockaddr_in6
))
6594 return key_senderror(so
, m
, EINVAL
);
6595 ((struct sockaddr_in6
*)(void *)(src0
+ 1))->sin6_port
= 0;
6600 switch (((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
6602 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
6603 sizeof(struct sockaddr_in
))
6604 return key_senderror(so
, m
, EINVAL
);
6605 ((struct sockaddr_in
*)(void *)(dst0
+ 1))->sin_port
= 0;
6608 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
6609 sizeof(struct sockaddr_in6
))
6610 return key_senderror(so
, m
, EINVAL
);
6611 ((struct sockaddr_in6
*)(void *)(dst0
+ 1))->sin6_port
= 0;
6617 /* XXX boundary check against sa_len */
6618 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
6620 lck_mtx_lock(sadb_mutex
);
6622 /* SPI allocation */
6623 spi
= key_do_getnewspi((struct sadb_spirange
*)
6624 (void *)mhp
->ext
[SADB_EXT_SPIRANGE
], &saidx
);
6626 lck_mtx_unlock(sadb_mutex
);
6627 return key_senderror(so
, m
, EINVAL
);
6630 /* get a SA index */
6631 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
6632 /* create a new SA index: key_addspi is always used for inbound spi */
6633 if ((newsah
= key_newsah(&saidx
, ipsec_if
, key_get_outgoing_ifindex_from_message(mhp
, SADB_X_EXT_IPSECIF
), IPSEC_DIR_INBOUND
)) == NULL
) {
6634 lck_mtx_unlock(sadb_mutex
);
6635 ipseclog((LOG_DEBUG
, "key_getspi: No more memory.\n"));
6636 return key_senderror(so
, m
, ENOBUFS
);
6642 newsav
= key_newsav(m
, mhp
, newsah
, &error
, so
);
6643 if (newsav
== NULL
) {
6644 /* XXX don't free new SA index allocated in above. */
6645 lck_mtx_unlock(sadb_mutex
);
6646 return key_senderror(so
, m
, error
);
6650 key_setspi(newsav
, htonl(spi
));
6652 #ifndef IPSEC_NONBLOCK_ACQUIRE
6653 /* delete the entry in acqtree */
6654 if (mhp
->msg
->sadb_msg_seq
!= 0) {
6656 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) != NULL
) {
6657 /* reset counter in order to deletion by timehandler. */
6660 acq
->created
= tv
.tv_sec
;
6666 lck_mtx_unlock(sadb_mutex
);
6669 struct mbuf
*n
, *nn
;
6670 struct sadb_sa
*m_sa
;
6671 struct sadb_msg
*newmsg
;
6674 /* create new sadb_msg to reply. */
6675 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
)) +
6676 PFKEY_ALIGN8(sizeof(struct sadb_sa
));
6678 return key_senderror(so
, m
, ENOBUFS
);
6680 MGETHDR(n
, M_WAITOK
, MT_DATA
);
6681 if (n
&& len
> MHLEN
) {
6682 MCLGET(n
, M_WAITOK
);
6683 if ((n
->m_flags
& M_EXT
) == 0) {
6689 return key_senderror(so
, m
, ENOBUFS
);
6695 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
6696 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
6698 m_sa
= (struct sadb_sa
*)(void *)(mtod(n
, caddr_t
) + off
);
6699 m_sa
->sadb_sa_len
= PFKEY_UNIT64(sizeof(struct sadb_sa
));
6700 m_sa
->sadb_sa_exttype
= SADB_EXT_SA
;
6701 m_sa
->sadb_sa_spi
= htonl(spi
);
6702 off
+= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
6706 panic("length inconsistency in key_getspi");
6709 int mbufItems
[] = {SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
6710 n
->m_next
= key_gather_mbuf(m
, mhp
, 0, sizeof(mbufItems
)/sizeof(int), mbufItems
);
6713 return key_senderror(so
, m
, ENOBUFS
);
6717 if (n
->m_len
< sizeof(struct sadb_msg
)) {
6718 n
= m_pullup(n
, sizeof(struct sadb_msg
));
6720 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
6723 n
->m_pkthdr
.len
= 0;
6724 for (nn
= n
; nn
; nn
= nn
->m_next
)
6725 n
->m_pkthdr
.len
+= nn
->m_len
;
6727 newmsg
= mtod(n
, struct sadb_msg
*);
6728 newmsg
->sadb_msg_seq
= newsav
->seq
;
6729 newmsg
->sadb_msg_errno
= 0;
6730 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
6733 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
6738 key_getspi2(struct sockaddr
*src
,
6739 struct sockaddr
*dst
,
6743 struct sadb_spirange
*spirange
)
6746 struct secasindex saidx
;
6748 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6750 /* XXX boundary check against sa_len */
6751 KEY_SETSECASIDX(proto
, mode
, reqid
, src
, dst
, 0, &saidx
);
6753 /* make sure if port number is zero. */
6754 switch (((struct sockaddr
*)&saidx
.src
)->sa_family
) {
6756 if (((struct sockaddr
*)&saidx
.src
)->sa_len
!= sizeof(struct sockaddr_in
))
6758 ((struct sockaddr_in
*)&saidx
.src
)->sin_port
= 0;
6761 if (((struct sockaddr
*)&saidx
.src
)->sa_len
!= sizeof(struct sockaddr_in6
))
6763 ((struct sockaddr_in6
*)&saidx
.src
)->sin6_port
= 0;
6768 switch (((struct sockaddr
*)&saidx
.dst
)->sa_family
) {
6770 if (((struct sockaddr
*)&saidx
.dst
)->sa_len
!= sizeof(struct sockaddr_in
))
6772 ((struct sockaddr_in
*)&saidx
.dst
)->sin_port
= 0;
6775 if (((struct sockaddr
*)&saidx
.dst
)->sa_len
!= sizeof(struct sockaddr_in6
))
6777 ((struct sockaddr_in6
*)&saidx
.dst
)->sin6_port
= 0;
6783 lck_mtx_lock(sadb_mutex
);
6785 /* SPI allocation */
6786 spi
= key_do_getnewspi(spirange
, &saidx
);
6788 lck_mtx_unlock(sadb_mutex
);
6794 * allocating new SPI
6795 * called by key_getspi() and key_getspi2().
6802 struct sadb_spirange
*spirange
,
6803 struct secasindex
*saidx
)
6806 u_int32_t keymin
, keymax
;
6807 int count
= key_spi_trycnt
;
6809 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6811 /* set spi range to allocate */
6812 if (spirange
!= NULL
) {
6813 keymin
= spirange
->sadb_spirange_min
;
6814 keymax
= spirange
->sadb_spirange_max
;
6816 keymin
= key_spi_minval
;
6817 keymax
= key_spi_maxval
;
6819 /* IPCOMP needs 2-byte SPI */
6820 if (saidx
->proto
== IPPROTO_IPCOMP
) {
6822 if (keymin
>= 0x10000)
6824 if (keymax
>= 0x10000)
6826 if (keymin
> keymax
) {
6827 t
= keymin
; keymin
= keymax
; keymax
= t
;
6831 if (keymin
== keymax
) {
6832 if (key_checkspidup(saidx
, keymin
) != NULL
) {
6833 ipseclog((LOG_DEBUG
, "key_do_getnewspi: SPI %u exists already.\n", keymin
));
6837 count
--; /* taking one cost. */
6842 u_int32_t range
= keymax
- keymin
+ 1; /* overflow value of zero means full range */
6847 /* when requesting to allocate spi ranged */
6849 u_int32_t rand_val
= key_random();
6851 /* generate pseudo-random SPI value ranged. */
6852 newspi
= (range
== 0 ? rand_val
: keymin
+ (rand_val
% range
));
6854 if (key_checkspidup(saidx
, newspi
) == NULL
)
6858 if (count
== 0 || newspi
== 0) {
6859 ipseclog((LOG_DEBUG
, "key_do_getnewspi: to allocate spi is failed.\n"));
6865 keystat
.getspi_count
=
6866 (keystat
.getspi_count
+ key_spi_trycnt
- count
) / 2;
6872 * SADB_UPDATE processing
6874 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
6875 * key(AE), (identity(SD),) (sensitivity)>
6876 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
6878 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
6879 * (identity(SD),) (sensitivity)>
6882 * m will always be freed.
6888 const struct sadb_msghdr
*mhp
)
6890 struct sadb_sa
*sa0
;
6891 struct sadb_address
*src0
, *dst0
;
6892 ifnet_t ipsec_if
= NULL
;
6893 struct secasindex saidx
;
6894 struct secashead
*sah
;
6895 struct secasvar
*sav
;
6902 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6905 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6906 panic("key_update: NULL pointer is passed.\n");
6908 /* map satype to proto */
6909 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6910 ipseclog((LOG_DEBUG
, "key_update: invalid satype is passed.\n"));
6911 return key_senderror(so
, m
, EINVAL
);
6914 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
6915 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6916 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
6917 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
6918 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
6919 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
6920 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
6921 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
6922 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
6923 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
6924 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
6925 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
6926 return key_senderror(so
, m
, EINVAL
);
6928 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
6929 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6930 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
6931 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
6932 return key_senderror(so
, m
, EINVAL
);
6934 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
6935 mode
= ((struct sadb_x_sa2
*)
6936 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
6937 reqid
= ((struct sadb_x_sa2
*)
6938 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
6939 flags2
= ((struct sadb_x_sa2
*)(void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_flags
;
6941 mode
= IPSEC_MODE_ANY
;
6945 /* XXX boundary checking for other extensions */
6947 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
6948 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
6949 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
6950 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
6952 /* XXX boundary check against sa_len */
6953 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
6955 lck_mtx_lock(sadb_mutex
);
6957 /* get a SA header */
6958 if ((sah
= key_getsah(&saidx
)) == NULL
) {
6959 lck_mtx_unlock(sadb_mutex
);
6960 ipseclog((LOG_DEBUG
, "key_update: no SA index found.\n"));
6961 return key_senderror(so
, m
, ENOENT
);
6964 /* set spidx if there */
6966 error
= key_setident(sah
, m
, mhp
);
6968 lck_mtx_unlock(sadb_mutex
);
6969 return key_senderror(so
, m
, error
);
6972 /* find a SA with sequence number. */
6973 #if IPSEC_DOSEQCHECK
6974 if (mhp
->msg
->sadb_msg_seq
!= 0
6975 && (sav
= key_getsavbyseq(sah
, mhp
->msg
->sadb_msg_seq
)) == NULL
) {
6976 lck_mtx_unlock(sadb_mutex
);
6977 ipseclog((LOG_DEBUG
,
6978 "key_update: no larval SA with sequence %u exists.\n",
6979 mhp
->msg
->sadb_msg_seq
));
6980 return key_senderror(so
, m
, ENOENT
);
6983 if ((sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
)) == NULL
) {
6984 lck_mtx_unlock(sadb_mutex
);
6985 ipseclog((LOG_DEBUG
,
6986 "key_update: no such a SA found (spi:%u)\n",
6987 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
6988 return key_senderror(so
, m
, EINVAL
);
6992 /* validity check */
6993 if (sav
->sah
->saidx
.proto
!= proto
) {
6994 lck_mtx_unlock(sadb_mutex
);
6995 ipseclog((LOG_DEBUG
,
6996 "key_update: protocol mismatched (DB=%u param=%u)\n",
6997 sav
->sah
->saidx
.proto
, proto
));
6998 return key_senderror(so
, m
, EINVAL
);
7000 #if IPSEC_DOSEQCHECK
7001 if (sav
->spi
!= sa0
->sadb_sa_spi
) {
7002 lck_mtx_unlock(sadb_mutex
);
7003 ipseclog((LOG_DEBUG
,
7004 "key_update: SPI mismatched (DB:%u param:%u)\n",
7005 (u_int32_t
)ntohl(sav
->spi
),
7006 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
7007 return key_senderror(so
, m
, EINVAL
);
7010 if (sav
->pid
!= mhp
->msg
->sadb_msg_pid
) {
7011 lck_mtx_unlock(sadb_mutex
);
7012 ipseclog((LOG_DEBUG
,
7013 "key_update: pid mismatched (DB:%u param:%u)\n",
7014 sav
->pid
, mhp
->msg
->sadb_msg_pid
));
7015 return key_senderror(so
, m
, EINVAL
);
7018 /* copy sav values */
7019 error
= key_setsaval(sav
, m
, mhp
);
7021 key_freesav(sav
, KEY_SADB_LOCKED
);
7022 lck_mtx_unlock(sadb_mutex
);
7023 return key_senderror(so
, m
, error
);
7026 sav
->flags2
= flags2
;
7027 if (flags2
& SADB_X_EXT_SA2_DELETE_ON_DETACH
) {
7032 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
7033 * this SA is for transport mode - otherwise clear it.
7035 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0 &&
7036 (sav
->sah
->saidx
.mode
!= IPSEC_MODE_TRANSPORT
||
7037 sav
->sah
->saidx
.src
.ss_family
!= AF_INET
))
7038 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
7040 /* check SA values to be mature. */
7041 if ((error
= key_mature(sav
)) != 0) {
7042 key_freesav(sav
, KEY_SADB_LOCKED
);
7043 lck_mtx_unlock(sadb_mutex
);
7044 return key_senderror(so
, m
, error
);
7047 lck_mtx_unlock(sadb_mutex
);
7052 /* set msg buf from mhp */
7053 n
= key_getmsgbuf_x1(m
, mhp
);
7055 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
7056 return key_senderror(so
, m
, ENOBUFS
);
7060 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
7065 key_migrate(struct socket
*so
,
7067 const struct sadb_msghdr
*mhp
)
7069 struct sadb_sa
*sa0
= NULL
;
7070 struct sadb_address
*src0
= NULL
;
7071 struct sadb_address
*dst0
= NULL
;
7072 struct sadb_address
*src1
= NULL
;
7073 struct sadb_address
*dst1
= NULL
;
7074 ifnet_t ipsec_if0
= NULL
;
7075 ifnet_t ipsec_if1
= NULL
;
7076 struct secasindex saidx0
;
7077 struct secasindex saidx1
;
7078 struct secashead
*sah
= NULL
;
7079 struct secashead
*newsah
= NULL
;
7080 struct secasvar
*sav
= NULL
;
7083 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7086 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7087 panic("key_migrate: NULL pointer is passed.\n");
7089 /* map satype to proto */
7090 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7091 ipseclog((LOG_DEBUG
, "key_migrate: invalid satype is passed.\n"));
7092 return key_senderror(so
, m
, EINVAL
);
7095 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
7096 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
7097 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
7098 mhp
->ext
[SADB_EXT_MIGRATE_ADDRESS_SRC
] == NULL
||
7099 mhp
->ext
[SADB_EXT_MIGRATE_ADDRESS_DST
] == NULL
) {
7100 ipseclog((LOG_DEBUG
, "key_migrate: invalid message is passed.\n"));
7101 return key_senderror(so
, m
, EINVAL
);
7104 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
7105 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7106 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
7107 mhp
->extlen
[SADB_EXT_MIGRATE_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7108 mhp
->extlen
[SADB_EXT_MIGRATE_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
7109 ipseclog((LOG_DEBUG
, "key_migrate: invalid message is passed.\n"));
7110 return key_senderror(so
, m
, EINVAL
);
7113 lck_mtx_lock(sadb_mutex
);
7115 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
7116 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
7117 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
7118 src1
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_MIGRATE_ADDRESS_SRC
]);
7119 dst1
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_MIGRATE_ADDRESS_DST
]);
7120 ipsec_if0
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
7121 ipsec_if1
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_MIGRATE_IPSECIF
);
7123 /* Find existing SAH and SAV */
7124 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if0
? ipsec_if0
->if_index
: 0, &saidx0
);
7126 LIST_FOREACH(sah
, &sahtree
, chain
) {
7127 if (sah
->state
!= SADB_SASTATE_MATURE
)
7129 if (key_cmpsaidx(&sah
->saidx
, &saidx0
, CMP_HEAD
) == 0)
7132 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
7133 if (sav
&& sav
->state
== SADB_SASTATE_MATURE
)
7137 lck_mtx_unlock(sadb_mutex
);
7138 ipseclog((LOG_DEBUG
, "key_migrate: no mature SAH found.\n"));
7139 return key_senderror(so
, m
, ENOENT
);
7143 lck_mtx_unlock(sadb_mutex
);
7144 ipseclog((LOG_DEBUG
, "key_migrate: no SA found.\n"));
7145 return key_senderror(so
, m
, ENOENT
);
7148 /* Find or create new SAH */
7149 KEY_SETSECASIDX(proto
, sah
->saidx
.mode
, sah
->saidx
.reqid
, src1
+ 1, dst1
+ 1, ipsec_if1
? ipsec_if1
->if_index
: 0, &saidx1
);
7151 if ((newsah
= key_getsah(&saidx1
)) == NULL
) {
7152 if ((newsah
= key_newsah(&saidx1
, ipsec_if1
, key_get_outgoing_ifindex_from_message(mhp
, SADB_X_EXT_MIGRATE_IPSECIF
), sah
->dir
)) == NULL
) {
7153 lck_mtx_unlock(sadb_mutex
);
7154 ipseclog((LOG_DEBUG
, "key_migrate: No more memory.\n"));
7155 return key_senderror(so
, m
, ENOBUFS
);
7159 /* Migrate SAV in to new SAH */
7160 if (key_migratesav(sav
, newsah
) != 0) {
7161 lck_mtx_unlock(sadb_mutex
);
7162 ipseclog((LOG_DEBUG
, "key_migrate: Failed to migrate SA to new SAH.\n"));
7163 return key_senderror(so
, m
, EINVAL
);
7166 /* Reset NAT values */
7167 sav
->flags
= sa0
->sadb_sa_flags
;
7168 sav
->remote_ike_port
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
;
7169 sav
->natt_interval
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_interval
;
7170 sav
->natt_offload_interval
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_offload_interval
;
7171 sav
->natt_last_activity
= natt_now
;
7174 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
7175 * SADB_X_EXT_NATT is set and SADB_X_EXT_NATT_KEEPALIVE is not
7176 * set (we're not behind nat) - otherwise clear it.
7178 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
7179 if ((sav
->flags
& SADB_X_EXT_NATT
) == 0 ||
7180 (sav
->flags
& SADB_X_EXT_NATT_KEEPALIVE
) != 0)
7181 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
7183 lck_mtx_unlock(sadb_mutex
);
7186 struct sadb_msg
*newmsg
;
7187 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
7188 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
, SADB_X_EXT_IPSECIF
,
7189 SADB_EXT_MIGRATE_ADDRESS_SRC
, SADB_EXT_MIGRATE_ADDRESS_DST
, SADB_X_EXT_MIGRATE_IPSECIF
};
7191 /* create new sadb_msg to reply. */
7192 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
7194 return key_senderror(so
, m
, ENOBUFS
);
7196 if (n
->m_len
< sizeof(struct sadb_msg
)) {
7197 n
= m_pullup(n
, sizeof(struct sadb_msg
));
7199 return key_senderror(so
, m
, ENOBUFS
);
7201 newmsg
= mtod(n
, struct sadb_msg
*);
7202 newmsg
->sadb_msg_errno
= 0;
7203 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
7206 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
7211 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
7212 * only called by key_update().
7215 * others : found, pointer to a SA.
7217 #if IPSEC_DOSEQCHECK
7218 static struct secasvar
*
7220 struct secashead
*sah
,
7223 struct secasvar
*sav
;
7226 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7228 state
= SADB_SASTATE_LARVAL
;
7230 /* search SAD with sequence number ? */
7231 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
7233 KEY_CHKSASTATE(state
, sav
->state
, "key_getsabyseq");
7235 if (sav
->seq
== seq
) {
7237 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
7238 printf("DP key_getsavbyseq cause "
7239 "refcnt++:%d SA:0x%llx\n", sav
->refcnt
,
7240 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
7250 * SADB_ADD processing
7251 * add a entry to SA database, when received
7252 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
7253 * key(AE), (identity(SD),) (sensitivity)>
7256 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
7257 * (identity(SD),) (sensitivity)>
7260 * IGNORE identity and sensitivity messages.
7262 * m will always be freed.
7268 const struct sadb_msghdr
*mhp
)
7270 struct sadb_sa
*sa0
;
7271 struct sadb_address
*src0
, *dst0
;
7272 ifnet_t ipsec_if
= NULL
;
7273 struct secasindex saidx
;
7274 struct secashead
*newsah
;
7275 struct secasvar
*newsav
;
7281 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7284 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7285 panic("key_add: NULL pointer is passed.\n");
7287 /* map satype to proto */
7288 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7289 ipseclog((LOG_DEBUG
, "key_add: invalid satype is passed.\n"));
7291 return key_senderror(so
, m
, EINVAL
);
7294 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
7295 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
7296 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
7297 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
7298 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
7299 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
7300 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
7301 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
7302 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
7303 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
7304 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
7305 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
7307 return key_senderror(so
, m
, EINVAL
);
7309 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
7310 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7311 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
7313 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
7315 return key_senderror(so
, m
, EINVAL
);
7317 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
7318 mode
= ((struct sadb_x_sa2
*)
7319 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
7320 reqid
= ((struct sadb_x_sa2
*)
7321 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
7323 mode
= IPSEC_MODE_ANY
;
7327 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
7328 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
7329 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
7330 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
7332 /* XXX boundary check against sa_len */
7333 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
7335 lck_mtx_lock(sadb_mutex
);
7337 /* get a SA header */
7338 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
7339 /* create a new SA header: key_addspi is always used for outbound spi */
7340 if ((newsah
= key_newsah(&saidx
, ipsec_if
, key_get_outgoing_ifindex_from_message(mhp
, SADB_X_EXT_IPSECIF
), IPSEC_DIR_OUTBOUND
)) == NULL
) {
7341 lck_mtx_unlock(sadb_mutex
);
7342 ipseclog((LOG_DEBUG
, "key_add: No more memory.\n"));
7344 return key_senderror(so
, m
, ENOBUFS
);
7348 /* set spidx if there */
7350 error
= key_setident(newsah
, m
, mhp
);
7352 lck_mtx_unlock(sadb_mutex
);
7354 return key_senderror(so
, m
, error
);
7357 /* create new SA entry. */
7358 /* We can create new SA only if SPI is different. */
7359 if (key_getsavbyspi(newsah
, sa0
->sadb_sa_spi
)) {
7360 lck_mtx_unlock(sadb_mutex
);
7361 ipseclog((LOG_DEBUG
, "key_add: SA already exists.\n"));
7363 return key_senderror(so
, m
, EEXIST
);
7365 newsav
= key_newsav(m
, mhp
, newsah
, &error
, so
);
7366 if (newsav
== NULL
) {
7367 lck_mtx_unlock(sadb_mutex
);
7369 return key_senderror(so
, m
, error
);
7373 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
7374 * this SA is for transport mode - otherwise clear it.
7376 if ((newsav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0 &&
7377 (newsah
->saidx
.mode
!= IPSEC_MODE_TRANSPORT
||
7378 newsah
->saidx
.dst
.ss_family
!= AF_INET
))
7379 newsav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
7381 /* check SA values to be mature. */
7382 if ((error
= key_mature(newsav
)) != 0) {
7383 key_freesav(newsav
, KEY_SADB_LOCKED
);
7384 lck_mtx_unlock(sadb_mutex
);
7386 return key_senderror(so
, m
, error
);
7389 lck_mtx_unlock(sadb_mutex
);
7392 * don't call key_freesav() here, as we would like to keep the SA
7393 * in the database on success.
7399 /* set msg buf from mhp */
7400 n
= key_getmsgbuf_x1(m
, mhp
);
7402 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
7404 return key_senderror(so
, m
, ENOBUFS
);
7407 // mh.ext points to the mbuf content.
7408 // Zero out Encryption and Integrity keys if present.
7411 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
7418 struct secashead
*sah
,
7420 const struct sadb_msghdr
*mhp
)
7422 const struct sadb_ident
*idsrc
, *iddst
;
7423 int idsrclen
, iddstlen
;
7425 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7428 if (sah
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7429 panic("key_setident: NULL pointer is passed.\n");
7431 /* don't make buffer if not there */
7432 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
&&
7433 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
7439 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
||
7440 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
7441 ipseclog((LOG_DEBUG
, "key_setident: invalid identity.\n"));
7445 idsrc
= (const struct sadb_ident
*)
7446 (void *)mhp
->ext
[SADB_EXT_IDENTITY_SRC
];
7447 iddst
= (const struct sadb_ident
*)
7448 (void *)mhp
->ext
[SADB_EXT_IDENTITY_DST
];
7449 idsrclen
= mhp
->extlen
[SADB_EXT_IDENTITY_SRC
];
7450 iddstlen
= mhp
->extlen
[SADB_EXT_IDENTITY_DST
];
7452 /* validity check */
7453 if (idsrc
->sadb_ident_type
!= iddst
->sadb_ident_type
) {
7454 ipseclog((LOG_DEBUG
, "key_setident: ident type mismatch.\n"));
7458 switch (idsrc
->sadb_ident_type
) {
7459 case SADB_IDENTTYPE_PREFIX
:
7460 case SADB_IDENTTYPE_FQDN
:
7461 case SADB_IDENTTYPE_USERFQDN
:
7463 /* XXX do nothing */
7469 /* make structure */
7470 KMALLOC_NOWAIT(sah
->idents
, struct sadb_ident
*, idsrclen
);
7471 if (sah
->idents
== NULL
) {
7472 lck_mtx_unlock(sadb_mutex
);
7473 KMALLOC_WAIT(sah
->idents
, struct sadb_ident
*, idsrclen
);
7474 lck_mtx_lock(sadb_mutex
);
7475 if (sah
->idents
== NULL
) {
7476 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
7480 KMALLOC_NOWAIT(sah
->identd
, struct sadb_ident
*, iddstlen
);
7481 if (sah
->identd
== NULL
) {
7482 lck_mtx_unlock(sadb_mutex
);
7483 KMALLOC_WAIT(sah
->identd
, struct sadb_ident
*, iddstlen
);
7484 lck_mtx_lock(sadb_mutex
);
7485 if (sah
->identd
== NULL
) {
7488 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
7492 bcopy(idsrc
, sah
->idents
, idsrclen
);
7493 bcopy(iddst
, sah
->identd
, iddstlen
);
7499 * m will not be freed on return.
7500 * it is caller's responsibility to free the result.
7502 static struct mbuf
*
7505 const struct sadb_msghdr
*mhp
)
7508 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
7509 SADB_X_EXT_SA2
, SADB_EXT_ADDRESS_SRC
,
7510 SADB_EXT_ADDRESS_DST
, SADB_EXT_LIFETIME_HARD
,
7511 SADB_EXT_LIFETIME_SOFT
, SADB_EXT_IDENTITY_SRC
,
7512 SADB_EXT_IDENTITY_DST
};
7515 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7516 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
7518 /* create new sadb_msg to reply. */
7519 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
7523 if (n
->m_len
< sizeof(struct sadb_msg
)) {
7524 n
= m_pullup(n
, sizeof(struct sadb_msg
));
7528 mtod(n
, struct sadb_msg
*)->sadb_msg_errno
= 0;
7529 mtod(n
, struct sadb_msg
*)->sadb_msg_len
=
7530 PFKEY_UNIT64(n
->m_pkthdr
.len
);
7535 static int key_delete_all(struct socket
*, struct mbuf
*,
7536 const struct sadb_msghdr
*, u_int16_t
);
7539 * SADB_DELETE processing
7541 * <base, SA(*), address(SD)>
7542 * from the ikmpd, and set SADB_SASTATE_DEAD,
7544 * <base, SA(*), address(SD)>
7547 * m will always be freed.
7553 const struct sadb_msghdr
*mhp
)
7555 struct sadb_sa
*sa0
;
7556 struct sadb_address
*src0
, *dst0
;
7557 ifnet_t ipsec_if
= NULL
;
7558 struct secasindex saidx
;
7559 struct secashead
*sah
;
7560 struct secasvar
*sav
= NULL
;
7563 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7566 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7567 panic("key_delete: NULL pointer is passed.\n");
7569 /* map satype to proto */
7570 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7571 ipseclog((LOG_DEBUG
, "key_delete: invalid satype is passed.\n"));
7572 return key_senderror(so
, m
, EINVAL
);
7575 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
7576 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
7577 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
7578 return key_senderror(so
, m
, EINVAL
);
7581 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7582 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
7583 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
7584 return key_senderror(so
, m
, EINVAL
);
7587 lck_mtx_lock(sadb_mutex
);
7589 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
7591 * Caller wants us to delete all non-LARVAL SAs
7592 * that match the src/dst. This is used during
7593 * IKE INITIAL-CONTACT.
7595 ipseclog((LOG_DEBUG
, "key_delete: doing delete all.\n"));
7596 /* key_delete_all will unlock sadb_mutex */
7597 return key_delete_all(so
, m
, mhp
, proto
);
7598 } else if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
)) {
7599 lck_mtx_unlock(sadb_mutex
);
7600 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
7601 return key_senderror(so
, m
, EINVAL
);
7604 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
7605 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
7606 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
7607 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
7609 /* XXX boundary check against sa_len */
7610 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
7612 /* get a SA header */
7613 LIST_FOREACH(sah
, &sahtree
, chain
) {
7614 if (sah
->state
== SADB_SASTATE_DEAD
)
7616 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
7619 /* get a SA with SPI. */
7620 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
7625 lck_mtx_unlock(sadb_mutex
);
7626 ipseclog((LOG_DEBUG
, "key_delete: no SA found.\n"));
7627 return key_senderror(so
, m
, ENOENT
);
7630 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
7631 key_freesav(sav
, KEY_SADB_LOCKED
);
7633 lck_mtx_unlock(sadb_mutex
);
7638 struct sadb_msg
*newmsg
;
7639 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
7640 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
7642 /* create new sadb_msg to reply. */
7643 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
7645 return key_senderror(so
, m
, ENOBUFS
);
7647 if (n
->m_len
< sizeof(struct sadb_msg
)) {
7648 n
= m_pullup(n
, sizeof(struct sadb_msg
));
7650 return key_senderror(so
, m
, ENOBUFS
);
7652 newmsg
= mtod(n
, struct sadb_msg
*);
7653 newmsg
->sadb_msg_errno
= 0;
7654 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
7657 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
7662 * delete all SAs for src/dst. Called from key_delete().
7668 const struct sadb_msghdr
*mhp
,
7671 struct sadb_address
*src0
, *dst0
;
7672 ifnet_t ipsec_if
= NULL
;
7673 struct secasindex saidx
;
7674 struct secashead
*sah
;
7675 struct secasvar
*sav
, *nextsav
;
7676 u_int stateidx
, state
;
7678 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7680 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
7681 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
7682 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
7684 /* XXX boundary check against sa_len */
7685 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
7687 LIST_FOREACH(sah
, &sahtree
, chain
) {
7688 if (sah
->state
== SADB_SASTATE_DEAD
)
7690 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
7693 /* Delete all non-LARVAL SAs. */
7695 stateidx
< _ARRAYLEN(saorder_state_alive
);
7697 state
= saorder_state_alive
[stateidx
];
7698 if (state
== SADB_SASTATE_LARVAL
)
7700 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
7701 sav
!= NULL
; sav
= nextsav
) {
7702 nextsav
= LIST_NEXT(sav
, chain
);
7704 if (sav
->state
!= state
) {
7705 ipseclog((LOG_DEBUG
, "key_delete_all: "
7706 "invalid sav->state "
7707 "(queue: %d SA: %d)\n",
7708 state
, sav
->state
));
7712 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
7713 key_freesav(sav
, KEY_SADB_LOCKED
);
7717 lck_mtx_unlock(sadb_mutex
);
7721 struct sadb_msg
*newmsg
;
7722 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_ADDRESS_SRC
,
7723 SADB_EXT_ADDRESS_DST
};
7725 /* create new sadb_msg to reply. */
7726 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
7728 return key_senderror(so
, m
, ENOBUFS
);
7730 if (n
->m_len
< sizeof(struct sadb_msg
)) {
7731 n
= m_pullup(n
, sizeof(struct sadb_msg
));
7733 return key_senderror(so
, m
, ENOBUFS
);
7735 newmsg
= mtod(n
, struct sadb_msg
*);
7736 newmsg
->sadb_msg_errno
= 0;
7737 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
7740 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
7745 * SADB_GET processing
7747 * <base, SA(*), address(SD)>
7748 * from the ikmpd, and get a SP and a SA to respond,
7750 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
7751 * (identity(SD),) (sensitivity)>
7754 * m will always be freed.
7760 const struct sadb_msghdr
*mhp
)
7762 struct sadb_sa
*sa0
;
7763 struct sadb_address
*src0
, *dst0
;
7764 ifnet_t ipsec_if
= NULL
;
7765 struct secasindex saidx
;
7766 struct secashead
*sah
;
7767 struct secasvar
*sav
= NULL
;
7770 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7773 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7774 panic("key_get: NULL pointer is passed.\n");
7776 /* map satype to proto */
7777 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7778 ipseclog((LOG_DEBUG
, "key_get: invalid satype is passed.\n"));
7779 return key_senderror(so
, m
, EINVAL
);
7782 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
7783 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
7784 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
7785 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
7786 return key_senderror(so
, m
, EINVAL
);
7788 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
7789 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7790 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
7791 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
7792 return key_senderror(so
, m
, EINVAL
);
7795 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
7796 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
7797 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
7798 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
7800 /* XXX boundary check against sa_len */
7801 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
7803 lck_mtx_lock(sadb_mutex
);
7805 /* get a SA header */
7806 LIST_FOREACH(sah
, &sahtree
, chain
) {
7807 if (sah
->state
== SADB_SASTATE_DEAD
)
7809 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
7812 /* get a SA with SPI. */
7813 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
7818 lck_mtx_unlock(sadb_mutex
);
7819 ipseclog((LOG_DEBUG
, "key_get: no SA found.\n"));
7820 return key_senderror(so
, m
, ENOENT
);
7827 /* map proto to satype */
7828 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
7829 lck_mtx_unlock(sadb_mutex
);
7830 ipseclog((LOG_DEBUG
, "key_get: there was invalid proto in SAD.\n"));
7831 return key_senderror(so
, m
, EINVAL
);
7833 lck_mtx_unlock(sadb_mutex
);
7835 /* create new sadb_msg to reply. */
7836 n
= key_setdumpsa(sav
, SADB_GET
, satype
, mhp
->msg
->sadb_msg_seq
,
7837 mhp
->msg
->sadb_msg_pid
);
7842 return key_senderror(so
, m
, ENOBUFS
);
7845 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
7850 * get SA stats by spi.
7851 * OUT: -1 : not found
7852 * 0 : found, arg pointer to a SA stats is updated.
7855 key_getsastatbyspi_one (u_int32_t spi
,
7856 struct sastat
*stat
)
7858 struct secashead
*sah
;
7859 struct secasvar
*sav
= NULL
;
7861 if ((void *)stat
== NULL
) {
7865 lck_mtx_lock(sadb_mutex
);
7867 /* get a SA header */
7868 LIST_FOREACH(sah
, &sahtree
, chain
) {
7869 if (sah
->state
== SADB_SASTATE_DEAD
)
7872 /* get a SA with SPI. */
7873 sav
= key_getsavbyspi(sah
, spi
);
7875 stat
->spi
= sav
->spi
;
7876 stat
->created
= sav
->created
;
7878 bcopy(sav
->lft_c
,&stat
->lft_c
, sizeof(stat
->lft_c
));
7880 bzero(&stat
->lft_c
, sizeof(stat
->lft_c
));
7882 lck_mtx_unlock(sadb_mutex
);
7887 lck_mtx_unlock(sadb_mutex
);
7893 * get SA stats collection by indices.
7894 * OUT: -1 : not found
7895 * 0 : found, arg pointers to a SA stats and 'maximum stats' are updated.
7898 key_getsastatbyspi (struct sastat
*stat_arg
,
7899 u_int32_t max_stat_arg
,
7900 struct sastat
*stat_res
,
7901 u_int32_t
*max_stat_res
)
7905 if (stat_arg
== NULL
||
7907 max_stat_res
== NULL
) {
7911 for (cur
= 0; cur
< max_stat_arg
; cur
++) {
7912 if (key_getsastatbyspi_one(stat_arg
[cur
].spi
,
7913 &stat_res
[found
]) == 0) {
7917 *max_stat_res
= found
;
7925 /* XXX make it sysctl-configurable? */
7927 key_getcomb_setlifetime(
7928 struct sadb_comb
*comb
)
7931 comb
->sadb_comb_soft_allocations
= 1;
7932 comb
->sadb_comb_hard_allocations
= 1;
7933 comb
->sadb_comb_soft_bytes
= 0;
7934 comb
->sadb_comb_hard_bytes
= 0;
7935 comb
->sadb_comb_hard_addtime
= 86400; /* 1 day */
7936 comb
->sadb_comb_soft_addtime
= comb
->sadb_comb_soft_addtime
* 80 / 100;
7937 comb
->sadb_comb_soft_usetime
= 28800; /* 8 hours */
7938 comb
->sadb_comb_hard_usetime
= comb
->sadb_comb_hard_usetime
* 80 / 100;
7943 * XXX reorder combinations by preference
7944 * XXX no idea if the user wants ESP authentication or not
7946 static struct mbuf
*
7947 key_getcomb_esp(void)
7949 struct sadb_comb
*comb
;
7950 const struct esp_algorithm
*algo
;
7951 struct mbuf
*result
= NULL
, *m
, *n
;
7955 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
7958 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
7959 algo
= esp_algorithm_lookup(i
);
7963 if (algo
->keymax
< ipsec_esp_keymin
)
7965 if (algo
->keymin
< ipsec_esp_keymin
)
7966 encmin
= ipsec_esp_keymin
;
7968 encmin
= algo
->keymin
;
7971 m
= key_getcomb_ah();
7975 panic("assumption failed in key_getcomb_esp");
7977 MGET(m
, M_WAITOK
, MT_DATA
);
7982 bzero(mtod(m
, caddr_t
), m
->m_len
);
7989 for (n
= m
; n
; n
= n
->m_next
)
7993 panic("assumption failed in key_getcomb_esp");
7996 for (off
= 0; off
< totlen
; off
+= l
) {
7997 n
= m_pulldown(m
, off
, l
, &o
);
7999 /* m is already freed */
8002 comb
= (struct sadb_comb
*)
8003 (void *)(mtod(n
, caddr_t
) + o
);
8004 bzero(comb
, sizeof(*comb
));
8005 key_getcomb_setlifetime(comb
);
8006 comb
->sadb_comb_encrypt
= i
;
8007 comb
->sadb_comb_encrypt_minbits
= encmin
;
8008 comb
->sadb_comb_encrypt_maxbits
= algo
->keymax
;
8027 * XXX reorder combinations by preference
8029 static struct mbuf
*
8030 key_getcomb_ah(void)
8032 struct sadb_comb
*comb
;
8033 const struct ah_algorithm
*algo
;
8037 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
8040 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
8042 /* we prefer HMAC algorithms, not old algorithms */
8043 if (i
!= SADB_AALG_SHA1HMAC
&& i
!= SADB_AALG_MD5HMAC
)
8046 algo
= ah_algorithm_lookup(i
);
8050 if (algo
->keymax
< ipsec_ah_keymin
)
8052 if (algo
->keymin
< ipsec_ah_keymin
)
8053 keymin
= ipsec_ah_keymin
;
8055 keymin
= algo
->keymin
;
8060 panic("assumption failed in key_getcomb_ah");
8062 MGET(m
, M_WAITOK
, MT_DATA
);
8069 M_PREPEND(m
, l
, M_WAITOK
, 1);
8073 comb
= mtod(m
, struct sadb_comb
*);
8074 bzero(comb
, sizeof(*comb
));
8075 key_getcomb_setlifetime(comb
);
8076 comb
->sadb_comb_auth
= i
;
8077 comb
->sadb_comb_auth_minbits
= keymin
;
8078 comb
->sadb_comb_auth_maxbits
= algo
->keymax
;
8085 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
8086 * XXX reorder combinations by preference
8088 static struct mbuf
*
8089 key_getcomb_ipcomp(void)
8091 struct sadb_comb
*comb
;
8092 const struct ipcomp_algorithm
*algo
;
8095 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
8098 for (i
= 1; i
<= SADB_X_CALG_MAX
; i
++) {
8099 algo
= ipcomp_algorithm_lookup(i
);
8106 panic("assumption failed in key_getcomb_ipcomp");
8108 MGET(m
, M_WAITOK
, MT_DATA
);
8115 M_PREPEND(m
, l
, M_WAITOK
, 1);
8119 comb
= mtod(m
, struct sadb_comb
*);
8120 bzero(comb
, sizeof(*comb
));
8121 key_getcomb_setlifetime(comb
);
8122 comb
->sadb_comb_encrypt
= i
;
8123 /* what should we set into sadb_comb_*_{min,max}bits? */
8130 * XXX no way to pass mode (transport/tunnel) to userland
8131 * XXX replay checking?
8132 * XXX sysctl interface to ipsec_{ah,esp}_keymin
8134 static struct mbuf
*
8136 const struct secasindex
*saidx
)
8138 struct sadb_prop
*prop
;
8140 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_prop
));
8143 switch (saidx
->proto
) {
8146 m
= key_getcomb_esp();
8150 m
= key_getcomb_ah();
8152 case IPPROTO_IPCOMP
:
8153 m
= key_getcomb_ipcomp();
8161 M_PREPEND(m
, l
, M_WAITOK
, 1);
8166 for (n
= m
; n
; n
= n
->m_next
)
8169 prop
= mtod(m
, struct sadb_prop
*);
8170 bzero(prop
, sizeof(*prop
));
8171 prop
->sadb_prop_len
= PFKEY_UNIT64(totlen
);
8172 prop
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
8173 prop
->sadb_prop_replay
= 32; /* XXX */
8179 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
8181 * <base, SA, address(SD), (address(P)), x_policy,
8182 * (identity(SD),) (sensitivity,) proposal>
8183 * to KMD, and expect to receive
8184 * <base> with SADB_ACQUIRE if error occurred,
8186 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
8187 * from KMD by PF_KEY.
8189 * XXX x_policy is outside of RFC2367 (KAME extension).
8190 * XXX sensitivity is not supported.
8191 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
8192 * see comment for key_getcomb_ipcomp().
8196 * others: error number
8200 struct secasindex
*saidx
,
8201 struct secpolicy
*sp
)
8203 struct mbuf
*result
= NULL
, *m
;
8204 #ifndef IPSEC_NONBLOCK_ACQUIRE
8205 struct secacq
*newacq
;
8211 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
8215 panic("key_acquire: NULL pointer is passed.\n");
8216 if ((satype
= key_proto2satype(saidx
->proto
)) == 0)
8217 panic("key_acquire: invalid proto is passed.\n");
8219 #ifndef IPSEC_NONBLOCK_ACQUIRE
8221 * We never do anything about acquirng SA. There is anather
8222 * solution that kernel blocks to send SADB_ACQUIRE message until
8223 * getting something message from IKEd. In later case, to be
8224 * managed with ACQUIRING list.
8226 /* get a entry to check whether sending message or not. */
8227 lck_mtx_lock(sadb_mutex
);
8228 if ((newacq
= key_getacq(saidx
)) != NULL
) {
8229 if (key_blockacq_count
< newacq
->count
) {
8230 /* reset counter and do send message. */
8233 /* increment counter and do nothing. */
8235 lck_mtx_unlock(sadb_mutex
);
8239 /* make new entry for blocking to send SADB_ACQUIRE. */
8240 if ((newacq
= key_newacq(saidx
)) == NULL
) {
8241 lck_mtx_unlock(sadb_mutex
);
8245 /* add to acqtree */
8246 LIST_INSERT_HEAD(&acqtree
, newacq
, chain
);
8247 key_start_timehandler();
8250 lck_mtx_unlock(sadb_mutex
);
8253 seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
8255 m
= key_setsadbmsg(SADB_ACQUIRE
, 0, satype
, seq
, 0, 0);
8262 /* set sadb_address for saidx's. */
8263 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
8264 (struct sockaddr
*)&saidx
->src
, FULLMASK
, IPSEC_ULPROTO_ANY
);
8271 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
8272 (struct sockaddr
*)&saidx
->dst
, FULLMASK
, IPSEC_ULPROTO_ANY
);
8279 /* XXX proxy address (optional) */
8281 /* set sadb_x_policy */
8283 m
= key_setsadbxpolicy(sp
->policy
, sp
->spidx
.dir
, sp
->id
);
8291 /* XXX identity (optional) */
8293 if (idexttype
&& fqdn
) {
8294 /* create identity extension (FQDN) */
8295 struct sadb_ident
*id
;
8298 fqdnlen
= strlen(fqdn
) + 1; /* +1 for terminating-NUL */
8299 id
= (struct sadb_ident
*)p
;
8300 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
8301 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
8302 id
->sadb_ident_exttype
= idexttype
;
8303 id
->sadb_ident_type
= SADB_IDENTTYPE_FQDN
;
8304 bcopy(fqdn
, id
+ 1, fqdnlen
);
8305 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(fqdnlen
);
8309 /* create identity extension (USERFQDN) */
8310 struct sadb_ident
*id
;
8314 /* +1 for terminating-NUL */
8315 userfqdnlen
= strlen(userfqdn
) + 1;
8318 id
= (struct sadb_ident
*)p
;
8319 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
8320 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
8321 id
->sadb_ident_exttype
= idexttype
;
8322 id
->sadb_ident_type
= SADB_IDENTTYPE_USERFQDN
;
8323 /* XXX is it correct? */
8324 if (curproc
&& curproc
->p_cred
)
8325 id
->sadb_ident_id
= curproc
->p_cred
->p_ruid
;
8326 if (userfqdn
&& userfqdnlen
)
8327 bcopy(userfqdn
, id
+ 1, userfqdnlen
);
8328 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(userfqdnlen
);
8332 /* XXX sensitivity (optional) */
8334 /* create proposal/combination extension */
8335 m
= key_getprop(saidx
);
8338 * spec conformant: always attach proposal/combination extension,
8339 * the problem is that we have no way to attach it for ipcomp,
8340 * due to the way sadb_comb is declared in RFC2367.
8349 * outside of spec; make proposal/combination extension optional.
8355 if ((result
->m_flags
& M_PKTHDR
) == 0) {
8360 if (result
->m_len
< sizeof(struct sadb_msg
)) {
8361 result
= m_pullup(result
, sizeof(struct sadb_msg
));
8362 if (result
== NULL
) {
8368 result
->m_pkthdr
.len
= 0;
8369 for (m
= result
; m
; m
= m
->m_next
)
8370 result
->m_pkthdr
.len
+= m
->m_len
;
8372 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
8373 PFKEY_UNIT64(result
->m_pkthdr
.len
);
8375 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
8383 #ifndef IPSEC_NONBLOCK_ACQUIRE
8384 static struct secacq
*
8386 struct secasindex
*saidx
)
8388 struct secacq
*newacq
;
8392 KMALLOC_NOWAIT(newacq
, struct secacq
*, sizeof(struct secacq
));
8393 if (newacq
== NULL
) {
8394 lck_mtx_unlock(sadb_mutex
);
8395 KMALLOC_WAIT(newacq
, struct secacq
*, sizeof(struct secacq
));
8396 lck_mtx_lock(sadb_mutex
);
8397 if (newacq
== NULL
) {
8398 ipseclog((LOG_DEBUG
, "key_newacq: No more memory.\n"));
8402 bzero(newacq
, sizeof(*newacq
));
8405 bcopy(saidx
, &newacq
->saidx
, sizeof(newacq
->saidx
));
8406 newacq
->seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
);
8408 newacq
->created
= tv
.tv_sec
;
8414 static struct secacq
*
8416 struct secasindex
*saidx
)
8420 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
8422 LIST_FOREACH(acq
, &acqtree
, chain
) {
8423 if (key_cmpsaidx(saidx
, &acq
->saidx
, CMP_EXACTLY
))
8430 static struct secacq
*
8436 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
8438 LIST_FOREACH(acq
, &acqtree
, chain
) {
8439 if (acq
->seq
== seq
)
8447 static struct secspacq
*
8449 struct secpolicyindex
*spidx
)
8451 struct secspacq
*acq
;
8455 KMALLOC_NOWAIT(acq
, struct secspacq
*, sizeof(struct secspacq
));
8457 lck_mtx_unlock(sadb_mutex
);
8458 KMALLOC_WAIT(acq
, struct secspacq
*, sizeof(struct secspacq
));
8459 lck_mtx_lock(sadb_mutex
);
8461 ipseclog((LOG_DEBUG
, "key_newspacq: No more memory.\n"));
8465 bzero(acq
, sizeof(*acq
));
8468 bcopy(spidx
, &acq
->spidx
, sizeof(acq
->spidx
));
8470 acq
->created
= tv
.tv_sec
;
8476 static struct secspacq
*
8478 struct secpolicyindex
*spidx
)
8480 struct secspacq
*acq
;
8482 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
8484 LIST_FOREACH(acq
, &spacqtree
, chain
) {
8485 if (key_cmpspidx_exactly(spidx
, &acq
->spidx
))
8493 * SADB_ACQUIRE processing,
8494 * in first situation, is receiving
8496 * from the ikmpd, and clear sequence of its secasvar entry.
8498 * In second situation, is receiving
8499 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
8500 * from a user land process, and return
8501 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
8504 * m will always be freed.
8510 const struct sadb_msghdr
*mhp
)
8512 const struct sadb_address
*src0
, *dst0
;
8513 ifnet_t ipsec_if
= NULL
;
8514 struct secasindex saidx
;
8515 struct secashead
*sah
;
8521 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
8522 panic("key_acquire2: NULL pointer is passed.\n");
8525 * Error message from KMd.
8526 * We assume that if error was occurred in IKEd, the length of PFKEY
8527 * message is equal to the size of sadb_msg structure.
8528 * We do not raise error even if error occurred in this function.
8530 lck_mtx_lock(sadb_mutex
);
8532 if (mhp
->msg
->sadb_msg_len
== PFKEY_UNIT64(sizeof(struct sadb_msg
))) {
8533 #ifndef IPSEC_NONBLOCK_ACQUIRE
8537 /* check sequence number */
8538 if (mhp
->msg
->sadb_msg_seq
== 0) {
8539 lck_mtx_unlock(sadb_mutex
);
8540 ipseclog((LOG_DEBUG
, "key_acquire2: must specify sequence number.\n"));
8545 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) == NULL
) {
8547 * the specified larval SA is already gone, or we got
8548 * a bogus sequence number. we can silently ignore it.
8550 lck_mtx_unlock(sadb_mutex
);
8555 /* reset acq counter in order to deletion by timehander. */
8557 acq
->created
= tv
.tv_sec
;
8560 lck_mtx_unlock(sadb_mutex
);
8566 * This message is from user land.
8569 /* map satype to proto */
8570 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
8571 lck_mtx_unlock(sadb_mutex
);
8572 ipseclog((LOG_DEBUG
, "key_acquire2: invalid satype is passed.\n"));
8573 return key_senderror(so
, m
, EINVAL
);
8576 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
8577 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
8578 mhp
->ext
[SADB_EXT_PROPOSAL
] == NULL
) {
8580 lck_mtx_unlock(sadb_mutex
);
8581 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
8582 return key_senderror(so
, m
, EINVAL
);
8584 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
8585 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
8586 mhp
->extlen
[SADB_EXT_PROPOSAL
] < sizeof(struct sadb_prop
)) {
8588 lck_mtx_unlock(sadb_mutex
);
8589 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
8590 return key_senderror(so
, m
, EINVAL
);
8593 src0
= (const struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
8594 dst0
= (const struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
8595 ipsec_if
= key_get_ipsec_if_from_message(mhp
, SADB_X_EXT_IPSECIF
);
8597 /* XXX boundary check against sa_len */
8599 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
8601 /* get a SA index */
8602 LIST_FOREACH(sah
, &sahtree
, chain
) {
8603 if (sah
->state
== SADB_SASTATE_DEAD
)
8605 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_MODE
| CMP_REQID
))
8609 lck_mtx_unlock(sadb_mutex
);
8610 ipseclog((LOG_DEBUG
, "key_acquire2: a SA exists already.\n"));
8611 return key_senderror(so
, m
, EEXIST
);
8613 lck_mtx_unlock(sadb_mutex
);
8614 error
= key_acquire(&saidx
, NULL
);
8616 ipseclog((LOG_DEBUG
, "key_acquire2: error %d returned "
8617 "from key_acquire.\n", mhp
->msg
->sadb_msg_errno
));
8618 return key_senderror(so
, m
, error
);
8621 return key_sendup_mbuf(so
, m
, KEY_SENDUP_REGISTERED
);
8625 * SADB_REGISTER processing.
8626 * If SATYPE_UNSPEC has been passed as satype, only return sadb_supported.
8629 * from the ikmpd, and register a socket to send PF_KEY messages,
8633 * If socket is detached, must free from regnode.
8635 * m will always be freed.
8641 const struct sadb_msghdr
*mhp
)
8643 struct secreg
*reg
, *newreg
= 0;
8646 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
8647 panic("key_register: NULL pointer is passed.\n");
8649 /* check for invalid register message */
8650 if (mhp
->msg
->sadb_msg_satype
>= sizeof(regtree
)/sizeof(regtree
[0]))
8651 return key_senderror(so
, m
, EINVAL
);
8653 /* When SATYPE_UNSPEC is specified, only return sadb_supported. */
8654 if (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
)
8657 /* create regnode */
8658 KMALLOC_WAIT(newreg
, struct secreg
*, sizeof(*newreg
));
8659 if (newreg
== NULL
) {
8660 ipseclog((LOG_DEBUG
, "key_register: No more memory.\n"));
8661 return key_senderror(so
, m
, ENOBUFS
);
8663 bzero((caddr_t
)newreg
, sizeof(*newreg
));
8665 lck_mtx_lock(sadb_mutex
);
8666 /* check whether existing or not */
8667 LIST_FOREACH(reg
, ®tree
[mhp
->msg
->sadb_msg_satype
], chain
) {
8668 if (reg
->so
== so
) {
8669 lck_mtx_unlock(sadb_mutex
);
8670 ipseclog((LOG_DEBUG
, "key_register: socket exists already.\n"));
8672 return key_senderror(so
, m
, EEXIST
);
8678 ((struct keycb
*)sotorawcb(so
))->kp_registered
++;
8679 socket_unlock(so
, 1);
8681 /* add regnode to regtree. */
8682 LIST_INSERT_HEAD(®tree
[mhp
->msg
->sadb_msg_satype
], newreg
, chain
);
8683 lck_mtx_unlock(sadb_mutex
);
8687 struct sadb_msg
*newmsg
;
8688 struct sadb_supported
*sup
;
8689 u_int len
, alen
, elen
;
8692 struct sadb_alg
*alg
;
8694 /* create new sadb_msg to reply. */
8696 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
8697 if (ah_algorithm_lookup(i
))
8698 alen
+= sizeof(struct sadb_alg
);
8701 alen
+= sizeof(struct sadb_supported
);
8704 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
8705 if (esp_algorithm_lookup(i
))
8706 elen
+= sizeof(struct sadb_alg
);
8709 elen
+= sizeof(struct sadb_supported
);
8712 len
= sizeof(struct sadb_msg
) + alen
+ elen
;
8715 return key_senderror(so
, m
, ENOBUFS
);
8717 MGETHDR(n
, M_WAITOK
, MT_DATA
);
8718 if (n
&& len
> MHLEN
) {
8719 MCLGET(n
, M_WAITOK
);
8720 if ((n
->m_flags
& M_EXT
) == 0) {
8726 return key_senderror(so
, m
, ENOBUFS
);
8728 n
->m_pkthdr
.len
= n
->m_len
= len
;
8732 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
8733 newmsg
= mtod(n
, struct sadb_msg
*);
8734 newmsg
->sadb_msg_errno
= 0;
8735 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
8736 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
8738 /* for authentication algorithm */
8740 sup
= (struct sadb_supported
*)(void *)(mtod(n
, caddr_t
) + off
);
8741 sup
->sadb_supported_len
= PFKEY_UNIT64(alen
);
8742 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
8743 off
+= PFKEY_ALIGN8(sizeof(*sup
));
8745 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
8746 const struct ah_algorithm
*aalgo
;
8748 aalgo
= ah_algorithm_lookup(i
);
8751 alg
= (struct sadb_alg
*)
8752 (void *)(mtod(n
, caddr_t
) + off
);
8753 alg
->sadb_alg_id
= i
;
8754 alg
->sadb_alg_ivlen
= 0;
8755 alg
->sadb_alg_minbits
= aalgo
->keymin
;
8756 alg
->sadb_alg_maxbits
= aalgo
->keymax
;
8757 off
+= PFKEY_ALIGN8(sizeof(*alg
));
8762 /* for encryption algorithm */
8764 sup
= (struct sadb_supported
*)(void *)(mtod(n
, caddr_t
) + off
);
8765 sup
->sadb_supported_len
= PFKEY_UNIT64(elen
);
8766 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
8767 off
+= PFKEY_ALIGN8(sizeof(*sup
));
8769 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
8770 const struct esp_algorithm
*ealgo
;
8772 ealgo
= esp_algorithm_lookup(i
);
8775 alg
= (struct sadb_alg
*)
8776 (void *)(mtod(n
, caddr_t
) + off
);
8777 alg
->sadb_alg_id
= i
;
8778 if (ealgo
&& ealgo
->ivlen
) {
8780 * give NULL to get the value preferred by
8781 * algorithm XXX SADB_X_EXT_DERIV ?
8783 alg
->sadb_alg_ivlen
=
8784 (*ealgo
->ivlen
)(ealgo
, NULL
);
8786 alg
->sadb_alg_ivlen
= 0;
8787 alg
->sadb_alg_minbits
= ealgo
->keymin
;
8788 alg
->sadb_alg_maxbits
= ealgo
->keymax
;
8789 off
+= PFKEY_ALIGN8(sizeof(struct sadb_alg
));
8796 panic("length assumption failed in key_register");
8800 return key_sendup_mbuf(so
, n
, KEY_SENDUP_REGISTERED
);
8805 key_delete_all_for_socket (struct socket
*so
)
8807 struct secashead
*sah
, *nextsah
;
8808 struct secasvar
*sav
, *nextsav
;
8812 for (sah
= LIST_FIRST(&sahtree
);
8815 nextsah
= LIST_NEXT(sah
, chain
);
8816 for (stateidx
= 0; stateidx
< _ARRAYLEN(saorder_state_alive
); stateidx
++) {
8817 state
= saorder_state_any
[stateidx
];
8818 for (sav
= LIST_FIRST(&sah
->savtree
[state
]); sav
!= NULL
; sav
= nextsav
) {
8819 nextsav
= LIST_NEXT(sav
, chain
);
8820 if (sav
->flags2
& SADB_X_EXT_SA2_DELETE_ON_DETACH
&&
8822 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
8823 key_freesav(sav
, KEY_SADB_LOCKED
);
8831 * free secreg entry registered.
8832 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
8843 panic("key_freereg: NULL pointer is passed.\n");
8846 * check whether existing or not.
8847 * check all type of SA, because there is a potential that
8848 * one socket is registered to multiple type of SA.
8850 lck_mtx_lock(sadb_mutex
);
8851 key_delete_all_for_socket(so
);
8852 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
8853 LIST_FOREACH(reg
, ®tree
[i
], chain
) {
8855 && __LIST_CHAINED(reg
)) {
8856 LIST_REMOVE(reg
, chain
);
8862 lck_mtx_unlock(sadb_mutex
);
8867 * SADB_EXPIRE processing
8869 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
8871 * NOTE: We send only soft lifetime extension.
8874 * others : error number
8878 struct secasvar
*sav
)
8881 struct mbuf
*result
= NULL
, *m
;
8884 struct sadb_lifetime
*lt
;
8886 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
8890 panic("key_expire: NULL pointer is passed.\n");
8891 if (sav
->sah
== NULL
)
8892 panic("key_expire: Why was SA index in SA NULL.\n");
8893 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0)
8894 panic("key_expire: invalid proto is passed.\n");
8896 /* set msg header */
8897 m
= key_setsadbmsg(SADB_EXPIRE
, 0, satype
, sav
->seq
, 0, sav
->refcnt
);
8904 /* create SA extension */
8905 m
= key_setsadbsa(sav
);
8912 /* create SA extension */
8913 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
8914 sav
->replay
? sav
->replay
->count
: 0,
8915 sav
->sah
->saidx
.reqid
,
8923 /* create lifetime extension (current and soft) */
8924 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
8925 m
= key_alloc_mbuf(len
);
8926 if (!m
|| m
->m_next
) { /*XXX*/
8932 bzero(mtod(m
, caddr_t
), len
);
8933 lt
= mtod(m
, struct sadb_lifetime
*);
8934 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
8935 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
8936 lt
->sadb_lifetime_allocations
= sav
->lft_c
->sadb_lifetime_allocations
;
8937 lt
->sadb_lifetime_bytes
= sav
->lft_c
->sadb_lifetime_bytes
;
8938 lt
->sadb_lifetime_addtime
= sav
->lft_c
->sadb_lifetime_addtime
;
8939 lt
->sadb_lifetime_usetime
= sav
->lft_c
->sadb_lifetime_usetime
;
8940 lt
= (struct sadb_lifetime
*)(void *)(mtod(m
, caddr_t
) + len
/ 2);
8941 bcopy(sav
->lft_s
, lt
, sizeof(*lt
));
8944 /* set sadb_address for source */
8945 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
8946 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
8947 FULLMASK
, IPSEC_ULPROTO_ANY
);
8954 /* set sadb_address for destination */
8955 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
8956 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
8957 FULLMASK
, IPSEC_ULPROTO_ANY
);
8964 if ((result
->m_flags
& M_PKTHDR
) == 0) {
8969 if (result
->m_len
< sizeof(struct sadb_msg
)) {
8970 result
= m_pullup(result
, sizeof(struct sadb_msg
));
8971 if (result
== NULL
) {
8977 result
->m_pkthdr
.len
= 0;
8978 for (m
= result
; m
; m
= m
->m_next
)
8979 result
->m_pkthdr
.len
+= m
->m_len
;
8981 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
8982 PFKEY_UNIT64(result
->m_pkthdr
.len
);
8984 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
8993 * SADB_FLUSH processing
8996 * from the ikmpd, and free all entries in secastree.
9000 * NOTE: to do is only marking SADB_SASTATE_DEAD.
9002 * m will always be freed.
9008 const struct sadb_msghdr
*mhp
)
9010 struct sadb_msg
*newmsg
;
9011 struct secashead
*sah
, *nextsah
;
9012 struct secasvar
*sav
, *nextsav
;
9018 if (so
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
9019 panic("key_flush: NULL pointer is passed.\n");
9021 /* map satype to proto */
9022 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
9023 ipseclog((LOG_DEBUG
, "key_flush: invalid satype is passed.\n"));
9024 return key_senderror(so
, m
, EINVAL
);
9027 lck_mtx_lock(sadb_mutex
);
9029 /* no SATYPE specified, i.e. flushing all SA. */
9030 for (sah
= LIST_FIRST(&sahtree
);
9033 nextsah
= LIST_NEXT(sah
, chain
);
9035 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
9036 && proto
!= sah
->saidx
.proto
)
9040 stateidx
< _ARRAYLEN(saorder_state_alive
);
9042 state
= saorder_state_any
[stateidx
];
9043 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
9047 nextsav
= LIST_NEXT(sav
, chain
);
9049 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
9050 key_freesav(sav
, KEY_SADB_LOCKED
);
9054 sah
->state
= SADB_SASTATE_DEAD
;
9056 lck_mtx_unlock(sadb_mutex
);
9058 if (m
->m_len
< sizeof(struct sadb_msg
) ||
9059 sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
9060 ipseclog((LOG_DEBUG
, "key_flush: No more memory.\n"));
9061 return key_senderror(so
, m
, ENOBUFS
);
9067 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct sadb_msg
);
9068 newmsg
= mtod(m
, struct sadb_msg
*);
9069 newmsg
->sadb_msg_errno
= 0;
9070 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
9072 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
9076 * SADB_DUMP processing
9077 * dump all entries including status of DEAD in SAD.
9080 * from the ikmpd, and dump all secasvar leaves
9085 * m will always be freed.
9088 struct sav_dump_elem
{
9089 struct secasvar
*sav
;
9097 const struct sadb_msghdr
*mhp
)
9099 struct secashead
*sah
;
9100 struct secasvar
*sav
;
9101 struct sav_dump_elem
*savbuf
= NULL
, *elem_ptr
;
9106 int cnt
= 0, cnt2
, bufcount
;
9110 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
9113 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
9114 panic("key_dump: NULL pointer is passed.\n");
9116 /* map satype to proto */
9117 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
9118 ipseclog((LOG_DEBUG
, "key_dump: invalid satype is passed.\n"));
9119 return key_senderror(so
, m
, EINVAL
);
9122 if ((bufcount
= ipsec_sav_count
) <= 0) {
9126 bufcount
+= 512; /* extra */
9127 KMALLOC_WAIT(savbuf
, struct sav_dump_elem
*, bufcount
* sizeof(struct sav_dump_elem
));
9128 if (savbuf
== NULL
) {
9129 ipseclog((LOG_DEBUG
, "key_dump: No more memory.\n"));
9134 /* count sav entries to be sent to the userland. */
9135 lck_mtx_lock(sadb_mutex
);
9137 LIST_FOREACH(sah
, &sahtree
, chain
) {
9138 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
9139 && proto
!= sah
->saidx
.proto
)
9142 /* map proto to satype */
9143 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
9144 lck_mtx_unlock(sadb_mutex
);
9145 ipseclog((LOG_DEBUG
, "key_dump: there was invalid proto in SAD.\n"));
9151 stateidx
< _ARRAYLEN(saorder_state_any
);
9153 state
= saorder_state_any
[stateidx
];
9154 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
9155 if (cnt
== bufcount
)
9156 break; /* out of buffer space */
9157 elem_ptr
->sav
= sav
;
9158 elem_ptr
->satype
= satype
;
9165 lck_mtx_unlock(sadb_mutex
);
9172 /* send this to the userland, one at a time. */
9176 n
= key_setdumpsa(elem_ptr
->sav
, SADB_DUMP
, elem_ptr
->satype
,
9177 --cnt2
, mhp
->msg
->sadb_msg_pid
);
9184 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
9192 lck_mtx_lock(sadb_mutex
);
9194 key_freesav((elem_ptr
++)->sav
, KEY_SADB_LOCKED
);
9195 lck_mtx_unlock(sadb_mutex
);
9201 return key_senderror(so
, m
, error
);
9208 * SADB_X_PROMISC processing
9210 * m will always be freed.
9216 const struct sadb_msghdr
*mhp
)
9221 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
9222 panic("key_promisc: NULL pointer is passed.\n");
9224 olen
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
9226 if (olen
< sizeof(struct sadb_msg
)) {
9228 return key_senderror(so
, m
, EINVAL
);
9233 } else if (olen
== sizeof(struct sadb_msg
)) {
9234 /* enable/disable promisc mode */
9238 if ((kp
= (struct keycb
*)sotorawcb(so
)) == NULL
)
9239 return key_senderror(so
, m
, EINVAL
);
9240 mhp
->msg
->sadb_msg_errno
= 0;
9241 switch (mhp
->msg
->sadb_msg_satype
) {
9244 kp
->kp_promisc
= mhp
->msg
->sadb_msg_satype
;
9247 socket_unlock(so
, 1);
9248 return key_senderror(so
, m
, EINVAL
);
9250 socket_unlock(so
, 1);
9252 /* send the original message back to everyone */
9253 mhp
->msg
->sadb_msg_errno
= 0;
9254 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
9256 /* send packet as is */
9258 m_adj(m
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)));
9260 /* TODO: if sadb_msg_seq is specified, send to specific pid */
9261 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
9265 static int (*const key_typesw
[])(struct socket
*, struct mbuf
*,
9266 const struct sadb_msghdr
*) = {
9267 NULL
, /* SADB_RESERVED */
9268 key_getspi
, /* SADB_GETSPI */
9269 key_update
, /* SADB_UPDATE */
9270 key_add
, /* SADB_ADD */
9271 key_delete
, /* SADB_DELETE */
9272 key_get
, /* SADB_GET */
9273 key_acquire2
, /* SADB_ACQUIRE */
9274 key_register
, /* SADB_REGISTER */
9275 NULL
, /* SADB_EXPIRE */
9276 key_flush
, /* SADB_FLUSH */
9277 key_dump
, /* SADB_DUMP */
9278 key_promisc
, /* SADB_X_PROMISC */
9279 NULL
, /* SADB_X_PCHANGE */
9280 key_spdadd
, /* SADB_X_SPDUPDATE */
9281 key_spdadd
, /* SADB_X_SPDADD */
9282 key_spddelete
, /* SADB_X_SPDDELETE */
9283 key_spdget
, /* SADB_X_SPDGET */
9284 NULL
, /* SADB_X_SPDACQUIRE */
9285 key_spddump
, /* SADB_X_SPDDUMP */
9286 key_spdflush
, /* SADB_X_SPDFLUSH */
9287 key_spdadd
, /* SADB_X_SPDSETIDX */
9288 NULL
, /* SADB_X_SPDEXPIRE */
9289 key_spddelete2
, /* SADB_X_SPDDELETE2 */
9290 key_getsastat
, /* SADB_GETSASTAT */
9291 key_spdenable
, /* SADB_X_SPDENABLE */
9292 key_spddisable
, /* SADB_X_SPDDISABLE */
9293 key_migrate
, /* SADB_MIGRATE */
9297 bzero_mbuf(struct mbuf
*m
)
9299 struct mbuf
*mptr
= m
;
9300 struct sadb_msg
*msg
= NULL
;
9307 if (mptr
->m_len
>= sizeof(struct sadb_msg
)) {
9308 msg
= mtod(mptr
, struct sadb_msg
*);
9309 if (msg
->sadb_msg_type
!= SADB_ADD
&&
9310 msg
->sadb_msg_type
!= SADB_UPDATE
) {
9313 offset
= sizeof(struct sadb_msg
);
9315 bzero(mptr
->m_data
+offset
, mptr
->m_len
-offset
);
9316 mptr
= mptr
->m_next
;
9317 while (mptr
!= NULL
) {
9318 bzero(mptr
->m_data
, mptr
->m_len
);
9319 mptr
= mptr
->m_next
;
9324 bzero_keys(const struct sadb_msghdr
*mh
)
9332 offset
= sizeof(struct sadb_key
);
9334 if (mh
->ext
[SADB_EXT_KEY_ENCRYPT
]) {
9335 struct sadb_key
*key
= (struct sadb_key
*)mh
->ext
[SADB_EXT_KEY_ENCRYPT
];
9336 extlen
= key
->sadb_key_bits
>> 3;
9338 if (mh
->extlen
[SADB_EXT_KEY_ENCRYPT
] >= offset
+ extlen
) {
9339 bzero((uint8_t *)mh
->ext
[SADB_EXT_KEY_ENCRYPT
]+offset
, extlen
);
9341 bzero(mh
->ext
[SADB_EXT_KEY_ENCRYPT
], mh
->extlen
[SADB_EXT_KEY_ENCRYPT
]);
9344 if (mh
->ext
[SADB_EXT_KEY_AUTH
]) {
9345 struct sadb_key
*key
= (struct sadb_key
*)mh
->ext
[SADB_EXT_KEY_AUTH
];
9346 extlen
= key
->sadb_key_bits
>> 3;
9348 if (mh
->extlen
[SADB_EXT_KEY_AUTH
] >= offset
+ extlen
) {
9349 bzero((uint8_t *)mh
->ext
[SADB_EXT_KEY_AUTH
]+offset
, extlen
);
9351 bzero(mh
->ext
[SADB_EXT_KEY_AUTH
], mh
->extlen
[SADB_EXT_KEY_AUTH
]);
9357 key_validate_address_pair(struct sadb_address
*src0
,
9358 struct sadb_address
*dst0
)
9362 /* check upper layer protocol */
9363 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
9364 ipseclog((LOG_DEBUG
, "key_parse: upper layer protocol mismatched.\n"));
9365 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9370 if (PFKEY_ADDR_SADDR(src0
)->sa_family
!=
9371 PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
9372 ipseclog((LOG_DEBUG
, "key_parse: address family mismatched.\n"));
9373 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9376 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
9377 PFKEY_ADDR_SADDR(dst0
)->sa_len
) {
9378 ipseclog((LOG_DEBUG
,
9379 "key_parse: address struct size mismatched.\n"));
9380 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9384 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
9386 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!= sizeof(struct sockaddr_in
)) {
9387 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9392 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!= sizeof(struct sockaddr_in6
)) {
9393 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9398 ipseclog((LOG_DEBUG
,
9399 "key_parse: unsupported address family.\n"));
9400 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9401 return (EAFNOSUPPORT
);
9404 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
9406 plen
= sizeof(struct in_addr
) << 3;
9409 plen
= sizeof(struct in6_addr
) << 3;
9412 plen
= 0; /*fool gcc*/
9416 /* check max prefix length */
9417 if (src0
->sadb_address_prefixlen
> plen
||
9418 dst0
->sadb_address_prefixlen
> plen
) {
9419 ipseclog((LOG_DEBUG
,
9420 "key_parse: illegal prefixlen.\n"));
9421 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9426 * prefixlen == 0 is valid because there can be a case when
9427 * all addresses are matched.
9433 * parse sadb_msg buffer to process PFKEYv2,
9434 * and create a data to response if needed.
9435 * I think to be dealed with mbuf directly.
9437 * msgp : pointer to pointer to a received buffer pulluped.
9438 * This is rewrited to response.
9439 * so : pointer to socket.
9441 * length for buffer to send to user process.
9448 struct sadb_msg
*msg
;
9449 struct sadb_msghdr mh
;
9453 Boolean keyAligned
= FALSE
;
9455 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
9458 if (m
== NULL
|| so
== NULL
)
9459 panic("key_parse: NULL pointer is passed.\n");
9461 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
9462 KEYDEBUG(KEYDEBUG_KEY_DUMP
,
9463 ipseclog((LOG_DEBUG
, "key_parse: passed sadb_msg\n"));
9467 if (m
->m_len
< sizeof(struct sadb_msg
)) {
9468 m
= m_pullup(m
, sizeof(struct sadb_msg
));
9472 msg
= mtod(m
, struct sadb_msg
*);
9473 orglen
= PFKEY_UNUNIT64(msg
->sadb_msg_len
);
9474 target
= KEY_SENDUP_ONE
;
9476 if ((m
->m_flags
& M_PKTHDR
) == 0 ||
9477 m
->m_pkthdr
.len
!= m
->m_pkthdr
.len
) {
9478 ipseclog((LOG_DEBUG
, "key_parse: invalid message length.\n"));
9479 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
9484 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
9485 ipseclog((LOG_DEBUG
,
9486 "key_parse: PF_KEY version %u is mismatched.\n",
9487 msg
->sadb_msg_version
));
9488 PFKEY_STAT_INCREMENT(pfkeystat
.out_invver
);
9493 if (msg
->sadb_msg_type
> SADB_MAX
) {
9494 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
9495 msg
->sadb_msg_type
));
9496 PFKEY_STAT_INCREMENT(pfkeystat
.out_invmsgtype
);
9501 /* for old-fashioned code - should be nuked */
9502 if (m
->m_pkthdr
.len
> MCLBYTES
) {
9509 MGETHDR(n
, M_WAITOK
, MT_DATA
);
9510 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
9511 MCLGET(n
, M_WAITOK
);
9512 if ((n
->m_flags
& M_EXT
) == 0) {
9522 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
9523 n
->m_pkthdr
.len
= n
->m_len
= m
->m_pkthdr
.len
;
9530 /* align the mbuf chain so that extensions are in contiguous region. */
9531 error
= key_align(m
, &mh
);
9535 if (m
->m_next
) { /*XXX*/
9545 switch (msg
->sadb_msg_satype
) {
9546 case SADB_SATYPE_UNSPEC
:
9547 switch (msg
->sadb_msg_type
) {
9555 ipseclog((LOG_DEBUG
, "key_parse: must specify satype "
9556 "when msg type=%u.\n", msg
->sadb_msg_type
));
9557 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
9562 case SADB_SATYPE_AH
:
9563 case SADB_SATYPE_ESP
:
9564 case SADB_X_SATYPE_IPCOMP
:
9565 switch (msg
->sadb_msg_type
) {
9567 case SADB_X_SPDDELETE
:
9569 case SADB_X_SPDDUMP
:
9570 case SADB_X_SPDFLUSH
:
9571 case SADB_X_SPDSETIDX
:
9572 case SADB_X_SPDUPDATE
:
9573 case SADB_X_SPDDELETE2
:
9574 case SADB_X_SPDENABLE
:
9575 case SADB_X_SPDDISABLE
:
9576 ipseclog((LOG_DEBUG
, "key_parse: illegal satype=%u\n",
9577 msg
->sadb_msg_type
));
9578 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
9583 case SADB_SATYPE_RSVP
:
9584 case SADB_SATYPE_OSPFV2
:
9585 case SADB_SATYPE_RIPV2
:
9586 case SADB_SATYPE_MIP
:
9587 ipseclog((LOG_DEBUG
, "key_parse: type %u isn't supported.\n",
9588 msg
->sadb_msg_satype
));
9589 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
9592 case 1: /* XXX: What does it do? */
9593 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
9597 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
9598 msg
->sadb_msg_satype
));
9599 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
9604 /* Validate address fields for matching families, lengths, etc. */
9605 void *src0
= mh
.ext
[SADB_EXT_ADDRESS_SRC
];
9606 void *dst0
= mh
.ext
[SADB_EXT_ADDRESS_DST
];
9607 if (mh
.ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
] != NULL
&&
9608 mh
.ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
] != NULL
) {
9610 error
= key_validate_address_pair((struct sadb_address
*)(mh
.ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
]),
9611 (struct sadb_address
*)(mh
.ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
]));
9617 src0
= mh
.ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
];
9620 if (mh
.ext
[SADB_X_EXT_ADDR_RANGE_DST_START
] != NULL
&&
9621 mh
.ext
[SADB_X_EXT_ADDR_RANGE_DST_END
] != NULL
) {
9623 error
= key_validate_address_pair((struct sadb_address
*)(mh
.ext
[SADB_X_EXT_ADDR_RANGE_DST_START
]),
9624 (struct sadb_address
*)(mh
.ext
[SADB_X_EXT_ADDR_RANGE_DST_END
]));
9630 dst0
= mh
.ext
[SADB_X_EXT_ADDR_RANGE_DST_START
];
9633 if (src0
!= NULL
&& dst0
!= NULL
) {
9634 error
= key_validate_address_pair((struct sadb_address
*)(src0
),
9635 (struct sadb_address
*)(dst0
));
9641 if (msg
->sadb_msg_type
>= sizeof(key_typesw
)/sizeof(key_typesw
[0]) ||
9642 key_typesw
[msg
->sadb_msg_type
] == NULL
) {
9643 PFKEY_STAT_INCREMENT(pfkeystat
.out_invmsgtype
);
9648 error
= (*key_typesw
[msg
->sadb_msg_type
])(so
, m
, &mh
);
9658 msg
->sadb_msg_errno
= error
;
9659 return key_sendup_mbuf(so
, m
, target
);
9668 struct sadb_msg
*msg
;
9670 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
9672 if (m
->m_len
< sizeof(struct sadb_msg
))
9673 panic("invalid mbuf passed to key_senderror");
9675 msg
= mtod(m
, struct sadb_msg
*);
9676 msg
->sadb_msg_errno
= code
;
9677 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
9681 * set the pointer to each header into message buffer.
9682 * m will be freed on error.
9683 * XXX larger-than-MCLBYTES extension?
9688 struct sadb_msghdr
*mhp
)
9691 struct sadb_ext
*ext
;
9697 if (m
== NULL
|| mhp
== NULL
)
9698 panic("key_align: NULL pointer is passed.\n");
9699 if (m
->m_len
< sizeof(struct sadb_msg
))
9700 panic("invalid mbuf passed to key_align");
9703 bzero(mhp
, sizeof(*mhp
));
9705 mhp
->msg
= mtod(m
, struct sadb_msg
*);
9706 mhp
->ext
[0] = (struct sadb_ext
*)mhp
->msg
; /*XXX backward compat */
9708 end
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
9709 extlen
= end
; /*just in case extlen is not updated*/
9710 for (off
= sizeof(struct sadb_msg
); off
< end
; off
+= extlen
) {
9711 n
= m_pulldown(m
, off
, sizeof(struct sadb_ext
), &toff
);
9713 /* m is already freed */
9716 ext
= (struct sadb_ext
*)(void *)(mtod(n
, caddr_t
) + toff
);
9719 switch (ext
->sadb_ext_type
) {
9721 case SADB_EXT_ADDRESS_SRC
:
9722 case SADB_EXT_ADDRESS_DST
:
9723 case SADB_EXT_ADDRESS_PROXY
:
9724 case SADB_EXT_LIFETIME_CURRENT
:
9725 case SADB_EXT_LIFETIME_HARD
:
9726 case SADB_EXT_LIFETIME_SOFT
:
9727 case SADB_EXT_KEY_AUTH
:
9728 case SADB_EXT_KEY_ENCRYPT
:
9729 case SADB_EXT_IDENTITY_SRC
:
9730 case SADB_EXT_IDENTITY_DST
:
9731 case SADB_EXT_SENSITIVITY
:
9732 case SADB_EXT_PROPOSAL
:
9733 case SADB_EXT_SUPPORTED_AUTH
:
9734 case SADB_EXT_SUPPORTED_ENCRYPT
:
9735 case SADB_EXT_SPIRANGE
:
9736 case SADB_X_EXT_POLICY
:
9737 case SADB_X_EXT_SA2
:
9738 case SADB_EXT_SESSION_ID
:
9739 case SADB_EXT_SASTAT
:
9740 case SADB_X_EXT_IPSECIF
:
9741 case SADB_X_EXT_ADDR_RANGE_SRC_START
:
9742 case SADB_X_EXT_ADDR_RANGE_SRC_END
:
9743 case SADB_X_EXT_ADDR_RANGE_DST_START
:
9744 case SADB_X_EXT_ADDR_RANGE_DST_END
:
9745 case SADB_EXT_MIGRATE_ADDRESS_SRC
:
9746 case SADB_EXT_MIGRATE_ADDRESS_DST
:
9747 case SADB_X_EXT_MIGRATE_IPSECIF
:
9748 /* duplicate check */
9750 * XXX Are there duplication payloads of either
9751 * KEY_AUTH or KEY_ENCRYPT ?
9753 if (mhp
->ext
[ext
->sadb_ext_type
] != NULL
) {
9754 ipseclog((LOG_DEBUG
,
9755 "key_align: duplicate ext_type %u "
9756 "is passed.\n", ext
->sadb_ext_type
));
9759 PFKEY_STAT_INCREMENT(pfkeystat
.out_dupext
);
9764 ipseclog((LOG_DEBUG
,
9765 "key_align: invalid ext_type %u is passed.\n",
9766 ext
->sadb_ext_type
));
9769 PFKEY_STAT_INCREMENT(pfkeystat
.out_invexttype
);
9773 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
9775 if (key_validate_ext(ext
, extlen
)) {
9778 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
9782 n
= m_pulldown(m
, off
, extlen
, &toff
);
9784 /* m is already freed */
9787 ext
= (struct sadb_ext
*)(void *)(mtod(n
, caddr_t
) + toff
);
9789 mhp
->ext
[ext
->sadb_ext_type
] = ext
;
9790 mhp
->extoff
[ext
->sadb_ext_type
] = off
;
9791 mhp
->extlen
[ext
->sadb_ext_type
] = extlen
;
9797 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
9806 const struct sadb_ext
*ext
,
9809 struct sockaddr
*sa
;
9810 enum { NONE
, ADDR
} checktype
= NONE
;
9812 const int sal
= offsetof(struct sockaddr
, sa_len
) + sizeof(sa
->sa_len
);
9814 if (len
!= PFKEY_UNUNIT64(ext
->sadb_ext_len
))
9817 /* if it does not match minimum/maximum length, bail */
9818 if (ext
->sadb_ext_type
>= sizeof(minsize
) / sizeof(minsize
[0]) ||
9819 ext
->sadb_ext_type
>= sizeof(maxsize
) / sizeof(maxsize
[0]))
9821 if (!minsize
[ext
->sadb_ext_type
] || len
< minsize
[ext
->sadb_ext_type
])
9823 if (maxsize
[ext
->sadb_ext_type
] && len
> maxsize
[ext
->sadb_ext_type
])
9826 /* more checks based on sadb_ext_type XXX need more */
9827 switch (ext
->sadb_ext_type
) {
9828 case SADB_EXT_ADDRESS_SRC
:
9829 case SADB_EXT_ADDRESS_DST
:
9830 case SADB_EXT_ADDRESS_PROXY
:
9831 case SADB_X_EXT_ADDR_RANGE_SRC_START
:
9832 case SADB_X_EXT_ADDR_RANGE_SRC_END
:
9833 case SADB_X_EXT_ADDR_RANGE_DST_START
:
9834 case SADB_X_EXT_ADDR_RANGE_DST_END
:
9835 case SADB_EXT_MIGRATE_ADDRESS_SRC
:
9836 case SADB_EXT_MIGRATE_ADDRESS_DST
:
9837 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_address
));
9840 case SADB_EXT_IDENTITY_SRC
:
9841 case SADB_EXT_IDENTITY_DST
:
9842 if (((struct sadb_ident
*)(uintptr_t)(size_t)ext
)->
9843 sadb_ident_type
== SADB_X_IDENTTYPE_ADDR
) {
9844 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_ident
));
9854 switch (checktype
) {
9858 sa
= (struct sockaddr
*)((caddr_t
)(uintptr_t)ext
+ baselen
);
9860 if (len
< baselen
+ sal
)
9862 if (baselen
+ PFKEY_ALIGN8(sa
->sa_len
) != len
)
9871 * XXX: maybe This function is called after INBOUND IPsec processing.
9873 * Special check for tunnel-mode packets.
9874 * We must make some checks for consistency between inner and outer IP header.
9876 * xxx more checks to be provided
9879 key_checktunnelsanity(
9880 struct secasvar
*sav
,
9881 __unused u_int family
,
9882 __unused caddr_t src
,
9883 __unused caddr_t dst
)
9887 if (sav
->sah
== NULL
)
9888 panic("sav->sah == NULL at key_checktunnelsanity");
9890 /* XXX: check inner IP header */
9895 /* record data transfer on SA, and update timestamps */
9898 struct secasvar
*sav
,
9904 panic("key_sa_recordxfer called with sav == NULL");
9906 panic("key_sa_recordxfer called with m == NULL");
9910 lck_mtx_lock(sadb_mutex
);
9912 * XXX Currently, there is a difference of bytes size
9913 * between inbound and outbound processing.
9915 sav
->lft_c
->sadb_lifetime_bytes
+= m
->m_pkthdr
.len
;
9916 /* to check bytes lifetime is done in key_timehandler(). */
9919 * We use the number of packets as the unit of
9920 * sadb_lifetime_allocations. We increment the variable
9921 * whenever {esp,ah}_{in,out}put is called.
9923 sav
->lft_c
->sadb_lifetime_allocations
++;
9924 /* XXX check for expires? */
9927 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
9928 * in seconds. HARD and SOFT lifetime are measured by the time
9929 * difference (again in seconds) from sadb_lifetime_usetime.
9933 * -----+-----+--------+---> t
9934 * <--------------> HARD
9940 sav
->lft_c
->sadb_lifetime_usetime
= tv
.tv_sec
;
9941 /* XXX check for expires? */
9943 lck_mtx_unlock(sadb_mutex
);
9951 struct sockaddr
*dst
)
9953 struct secashead
*sah
;
9956 lck_mtx_lock(sadb_mutex
);
9957 LIST_FOREACH(sah
, &sahtree
, chain
) {
9958 ro
= (struct route
*)&sah
->sa_route
;
9959 if (ro
->ro_rt
&& dst
->sa_len
== ro
->ro_dst
.sa_len
9960 && bcmp(dst
, &ro
->ro_dst
, dst
->sa_len
) == 0) {
9964 lck_mtx_unlock(sadb_mutex
);
9971 struct secasvar
*sav
,
9976 panic("key_sa_chgstate called with sav == NULL");
9978 if (sav
->state
== state
)
9981 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
9983 if (__LIST_CHAINED(sav
))
9984 LIST_REMOVE(sav
, chain
);
9987 LIST_INSERT_HEAD(&sav
->sah
->savtree
[state
], sav
, chain
);
9993 struct secasvar
*sav
)
9995 lck_mtx_lock(sadb_mutex
);
9997 panic("key_sa_stir_iv called with sav == NULL");
9998 key_randomfill(sav
->iv
, sav
->ivlen
);
9999 lck_mtx_unlock(sadb_mutex
);
10002 /* XXX too much? */
10003 static struct mbuf
*
10007 struct mbuf
*m
= NULL
, *n
;
10012 MGET(n
, M_DONTWAIT
, MT_DATA
);
10013 if (n
&& len
> MLEN
)
10014 MCLGET(n
, M_DONTWAIT
);
10022 n
->m_len
= M_TRAILINGSPACE(n
);
10023 /* use the bottom of mbuf, hoping we can prepend afterwards */
10024 if (n
->m_len
> len
) {
10025 t
= (n
->m_len
- len
) & ~(sizeof(long) - 1);
10041 static struct mbuf
*
10042 key_setdumpsastats (u_int32_t dir
,
10043 struct sastat
*stats
,
10044 u_int32_t max_stats
,
10045 u_int64_t session_ids
[],
10049 struct mbuf
*result
= NULL
, *m
= NULL
;
10051 m
= key_setsadbmsg(SADB_GETSASTAT
, 0, 0, seq
, pid
, 0);
10057 m
= key_setsadbsession_id(session_ids
);
10063 m
= key_setsadbsastat(dir
,
10071 if ((result
->m_flags
& M_PKTHDR
) == 0) {
10075 if (result
->m_len
< sizeof(struct sadb_msg
)) {
10076 result
= m_pullup(result
, sizeof(struct sadb_msg
));
10077 if (result
== NULL
) {
10082 result
->m_pkthdr
.len
= 0;
10083 for (m
= result
; m
; m
= m
->m_next
) {
10084 result
->m_pkthdr
.len
+= m
->m_len
;
10087 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
10088 PFKEY_UNIT64(result
->m_pkthdr
.len
);
10100 * SADB_GETSASTAT processing
10101 * dump all stats for matching entries in SAD.
10103 * m will always be freed.
10107 key_getsastat (struct socket
*so
,
10109 const struct sadb_msghdr
*mhp
)
10111 struct sadb_session_id
*session_id
;
10112 u_int32_t bufsize
, arg_count
, res_count
;
10113 struct sadb_sastat
*sa_stats_arg
;
10114 struct sastat
*sa_stats_sav
= NULL
;
10119 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
10120 panic("%s: NULL pointer is passed.\n", __FUNCTION__
);
10122 if (mhp
->ext
[SADB_EXT_SESSION_ID
] == NULL
) {
10123 printf("%s: invalid message is passed. missing session-id.\n", __FUNCTION__
);
10124 return key_senderror(so
, m
, EINVAL
);
10126 if (mhp
->extlen
[SADB_EXT_SESSION_ID
] < sizeof(struct sadb_session_id
)) {
10127 printf("%s: invalid message is passed. short session-id.\n", __FUNCTION__
);
10128 return key_senderror(so
, m
, EINVAL
);
10130 if (mhp
->ext
[SADB_EXT_SASTAT
] == NULL
) {
10131 printf("%s: invalid message is passed. missing stat args.\n", __FUNCTION__
);
10132 return key_senderror(so
, m
, EINVAL
);
10134 if (mhp
->extlen
[SADB_EXT_SASTAT
] < sizeof(*sa_stats_arg
)) {
10135 printf("%s: invalid message is passed. short stat args.\n", __FUNCTION__
);
10136 return key_senderror(so
, m
, EINVAL
);
10139 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
10141 // exit early if there are no active SAs
10142 if (ipsec_sav_count
<= 0) {
10143 printf("%s: No active SAs.\n", __FUNCTION__
);
10147 bufsize
= (ipsec_sav_count
+ 1) * sizeof(*sa_stats_sav
);
10149 KMALLOC_WAIT(sa_stats_sav
, __typeof__(sa_stats_sav
), bufsize
);
10150 if (sa_stats_sav
== NULL
) {
10151 printf("%s: No more memory.\n", __FUNCTION__
);
10155 bzero(sa_stats_sav
, bufsize
);
10157 sa_stats_arg
= (__typeof__(sa_stats_arg
))
10158 (void *)mhp
->ext
[SADB_EXT_SASTAT
];
10159 arg_count
= sa_stats_arg
->sadb_sastat_list_len
;
10160 // exit early if there are no requested SAs
10161 if (arg_count
== 0) {
10162 printf("%s: No SAs requested.\n", __FUNCTION__
);
10168 if (key_getsastatbyspi((struct sastat
*)(sa_stats_arg
+ 1),
10172 printf("%s: Error finding SAs.\n", __FUNCTION__
);
10177 printf("%s: No SAs found.\n", __FUNCTION__
);
10182 session_id
= (__typeof__(session_id
))
10183 (void *)mhp
->ext
[SADB_EXT_SESSION_ID
];
10185 /* send this to the userland. */
10186 n
= key_setdumpsastats(sa_stats_arg
->sadb_sastat_dir
,
10189 session_id
->sadb_session_id_v
,
10190 mhp
->msg
->sadb_msg_seq
,
10191 mhp
->msg
->sadb_msg_pid
);
10193 printf("%s: No bufs to dump stats.\n", __FUNCTION__
);
10198 key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
10200 if (sa_stats_sav
) {
10201 KFREE(sa_stats_sav
);
10205 return key_senderror(so
, m
, error
);
10212 key_update_natt_keepalive_timestamp (struct secasvar
*sav_sent
,
10213 struct secasvar
*sav_update
)
10215 struct secasindex saidx_swap_sent_addr
;
10217 // exit early if two SAs are identical, or if sav_update is current
10218 if (sav_sent
== sav_update
||
10219 sav_update
->natt_last_activity
== natt_now
) {
10223 // assuming that (sav_update->remote_ike_port != 0 && (esp_udp_encap_port & 0xFFFF) != 0)
10225 bzero(&saidx_swap_sent_addr
, sizeof(saidx_swap_sent_addr
));
10226 memcpy(&saidx_swap_sent_addr
.src
, &sav_sent
->sah
->saidx
.dst
, sizeof(saidx_swap_sent_addr
.src
));
10227 memcpy(&saidx_swap_sent_addr
.dst
, &sav_sent
->sah
->saidx
.src
, sizeof(saidx_swap_sent_addr
.dst
));
10228 saidx_swap_sent_addr
.proto
= sav_sent
->sah
->saidx
.proto
;
10229 saidx_swap_sent_addr
.mode
= sav_sent
->sah
->saidx
.mode
;
10230 // we ignore reqid for split-tunnel setups
10232 if (key_cmpsaidx(&sav_sent
->sah
->saidx
, &sav_update
->sah
->saidx
, CMP_MODE
| CMP_PORT
) ||
10233 key_cmpsaidx(&saidx_swap_sent_addr
, &sav_update
->sah
->saidx
, CMP_MODE
| CMP_PORT
)) {
10234 sav_update
->natt_last_activity
= natt_now
;
10239 key_send_delsp (struct secpolicy
*sp
)
10241 struct mbuf
*result
= NULL
, *m
;
10246 /* set msg header */
10247 m
= key_setsadbmsg(SADB_X_SPDDELETE
, 0, 0, 0, 0, 0);
10253 /* set sadb_address(es) for source */
10254 if (sp
->spidx
.src_range
.start
.ss_len
> 0) {
10255 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_START
,
10256 (struct sockaddr
*)&sp
->spidx
.src_range
.start
, sp
->spidx
.prefs
,
10257 sp
->spidx
.ul_proto
);
10262 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_END
,
10263 (struct sockaddr
*)&sp
->spidx
.src_range
.end
, sp
->spidx
.prefs
,
10264 sp
->spidx
.ul_proto
);
10269 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
10270 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
10271 sp
->spidx
.ul_proto
);
10277 /* set sadb_address(es) for destination */
10278 if (sp
->spidx
.dst_range
.start
.ss_len
> 0) {
10279 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_START
,
10280 (struct sockaddr
*)&sp
->spidx
.dst_range
.start
, sp
->spidx
.prefd
,
10281 sp
->spidx
.ul_proto
);
10286 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_END
,
10287 (struct sockaddr
*)&sp
->spidx
.dst_range
.end
, sp
->spidx
.prefd
,
10288 sp
->spidx
.ul_proto
);
10293 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
10294 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
10295 sp
->spidx
.ul_proto
);
10301 /* set secpolicy */
10302 m
= key_sp2msg(sp
);
10308 if ((result
->m_flags
& M_PKTHDR
) == 0) {
10312 if (result
->m_len
< sizeof(struct sadb_msg
)) {
10313 result
= m_pullup(result
, sizeof(struct sadb_msg
));
10314 if (result
== NULL
) {
10319 result
->m_pkthdr
.len
= 0;
10320 for (m
= result
; m
; m
= m
->m_next
)
10321 result
->m_pkthdr
.len
+= m
->m_len
;
10323 mtod(result
, struct sadb_msg
*)->sadb_msg_len
= PFKEY_UNIT64(result
->m_pkthdr
.len
);
10325 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
10334 key_delsp_for_ipsec_if (ifnet_t ipsec_if
)
10336 struct secashead
*sah
;
10337 struct secasvar
*sav
, *nextsav
;
10340 struct secpolicy
*sp
, *nextsp
;
10343 if (ipsec_if
== NULL
)
10346 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
10348 lck_mtx_lock(sadb_mutex
);
10350 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
10351 for (sp
= LIST_FIRST(&sptree
[dir
]);
10355 nextsp
= LIST_NEXT(sp
, chain
);
10357 if (sp
->ipsec_if
== ipsec_if
) {
10358 ifnet_release(sp
->ipsec_if
);
10359 sp
->ipsec_if
= NULL
;
10361 key_send_delsp(sp
);
10363 sp
->state
= IPSEC_SPSTATE_DEAD
;
10364 key_freesp(sp
, KEY_SADB_LOCKED
);
10369 LIST_FOREACH(sah
, &sahtree
, chain
) {
10370 if (sah
->ipsec_if
== ipsec_if
) {
10371 /* This SAH is linked to the IPSec interface. It now needs to close. */
10372 ifnet_release(sah
->ipsec_if
);
10373 sah
->ipsec_if
= NULL
;
10375 for (stateidx
= 0; stateidx
< _ARRAYLEN(saorder_state_alive
); stateidx
++) {
10376 state
= saorder_state_any
[stateidx
];
10377 for (sav
= LIST_FIRST(&sah
->savtree
[state
]); sav
!= NULL
; sav
= nextsav
) {
10378 nextsav
= LIST_NEXT(sav
, chain
);
10380 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
10381 key_freesav(sav
, KEY_SADB_LOCKED
);
10385 sah
->state
= SADB_SASTATE_DEAD
;
10389 lck_mtx_unlock(sadb_mutex
);
10392 __private_extern__ u_int32_t
10393 key_fill_offload_frames_for_savs (ifnet_t ifp
,
10394 struct ifnet_keepalive_offload_frame
*frames_array
,
10395 u_int32_t frames_array_count
,
10396 size_t frame_data_offset
)
10398 struct secashead
*sah
= NULL
;
10399 struct secasvar
*sav
= NULL
;
10400 struct ifnet_keepalive_offload_frame
*frame
= frames_array
;
10401 u_int32_t frame_index
= 0;
10403 if (frame
== NULL
|| frames_array_count
== 0) {
10404 return (frame_index
);
10407 lck_mtx_lock(sadb_mutex
);
10408 LIST_FOREACH(sah
, &sahtree
, chain
) {
10409 LIST_FOREACH(sav
, &sah
->savtree
[SADB_SASTATE_MATURE
], chain
) {
10410 if (ipsec_fill_offload_frame(ifp
, sav
, frame
, frame_data_offset
)) {
10412 if (frame_index
>= frames_array_count
) {
10413 lck_mtx_unlock(sadb_mutex
);
10414 return (frame_index
);
10416 frame
= &(frames_array
[frame_index
]);
10420 lck_mtx_unlock(sadb_mutex
);
10422 return (frame_index
);