2 * Copyright (c) 2008-2014 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>
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 __private_extern__ u_int32_t natt_now
= 0;
177 static LIST_HEAD(_sptree
, secpolicy
) sptree
[IPSEC_DIR_MAX
]; /* SPD */
178 static LIST_HEAD(_sahtree
, secashead
) sahtree
; /* SAD */
179 static LIST_HEAD(_regtree
, secreg
) regtree
[SADB_SATYPE_MAX
+ 1];
182 #define SPIHASHSIZE 128
183 #define SPIHASH(x) (((x) ^ ((x) >> 16)) % SPIHASHSIZE)
184 static LIST_HEAD(_spihash
, secasvar
) spihash
[SPIHASHSIZE
];
186 #ifndef IPSEC_NONBLOCK_ACQUIRE
187 static LIST_HEAD(_acqtree
, secacq
) acqtree
; /* acquiring list */
189 static LIST_HEAD(_spacqtree
, secspacq
) spacqtree
; /* SP acquiring list */
191 struct key_cb key_cb
;
193 /* search order for SAs */
194 static const u_int saorder_state_valid_prefer_old
[] = {
195 SADB_SASTATE_DYING
, SADB_SASTATE_MATURE
,
197 static const u_int saorder_state_valid_prefer_new
[] = {
198 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
200 static const u_int saorder_state_alive
[] = {
202 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
, SADB_SASTATE_LARVAL
204 static const u_int saorder_state_any
[] = {
205 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
206 SADB_SASTATE_LARVAL
, SADB_SASTATE_DEAD
209 static const int minsize
[] = {
210 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
211 sizeof(struct sadb_sa
), /* SADB_EXT_SA */
212 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
213 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
214 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
215 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_SRC */
216 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_DST */
217 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_PROXY */
218 sizeof(struct sadb_key
), /* SADB_EXT_KEY_AUTH */
219 sizeof(struct sadb_key
), /* SADB_EXT_KEY_ENCRYPT */
220 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_SRC */
221 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_DST */
222 sizeof(struct sadb_sens
), /* SADB_EXT_SENSITIVITY */
223 sizeof(struct sadb_prop
), /* SADB_EXT_PROPOSAL */
224 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_AUTH */
225 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_ENCRYPT */
226 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
227 0, /* SADB_X_EXT_KMPRIVATE */
228 sizeof(struct sadb_x_policy
), /* SADB_X_EXT_POLICY */
229 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
230 sizeof(struct sadb_session_id
), /* SADB_EXT_SESSION_ID */
231 sizeof(struct sadb_sastat
), /* SADB_EXT_SASTAT */
232 sizeof(struct sadb_x_ipsecif
), /* SADB_X_EXT_IPSECIF */
233 sizeof(struct sadb_address
), /* SADB_X_EXT_ADDR_RANGE_SRC_START */
234 sizeof(struct sadb_address
), /* SADB_X_EXT_ADDR_RANGE_SRC_END */
235 sizeof(struct sadb_address
), /* SADB_X_EXT_ADDR_RANGE_DST_START */
236 sizeof(struct sadb_address
), /* SADB_X_EXT_ADDR_RANGE_DST_END */
238 static const int maxsize
[] = {
239 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
240 sizeof(struct sadb_sa_2
), /* SADB_EXT_SA */
241 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
242 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
243 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
244 0, /* SADB_EXT_ADDRESS_SRC */
245 0, /* SADB_EXT_ADDRESS_DST */
246 0, /* SADB_EXT_ADDRESS_PROXY */
247 0, /* SADB_EXT_KEY_AUTH */
248 0, /* SADB_EXT_KEY_ENCRYPT */
249 0, /* SADB_EXT_IDENTITY_SRC */
250 0, /* SADB_EXT_IDENTITY_DST */
251 0, /* SADB_EXT_SENSITIVITY */
252 0, /* SADB_EXT_PROPOSAL */
253 0, /* SADB_EXT_SUPPORTED_AUTH */
254 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
255 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
256 0, /* SADB_X_EXT_KMPRIVATE */
257 0, /* SADB_X_EXT_POLICY */
258 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
259 0, /* SADB_EXT_SESSION_ID */
260 0, /* SADB_EXT_SASTAT */
261 sizeof(struct sadb_x_ipsecif
), /* SADB_X_EXT_IPSECIF */
262 0, /* SADB_X_EXT_ADDR_RANGE_SRC_START */
263 0, /* SADB_X_EXT_ADDR_RANGE_SRC_END */
264 0, /* SADB_X_EXT_ADDR_RANGE_DST_START */
265 0, /* SADB_X_EXT_ADDR_RANGE_DST_END */
268 static int ipsec_esp_keymin
= 256;
269 static int ipsec_esp_auth
= 0;
270 static int ipsec_ah_keymin
= 128;
272 SYSCTL_DECL(_net_key
);
273 /* Thread safe: no accumulated state */
274 SYSCTL_INT(_net_key
, KEYCTL_DEBUG_LEVEL
, debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
275 &key_debug_level
, 0, "");
278 /* max count of trial for the decision of spi value */
279 SYSCTL_INT(_net_key
, KEYCTL_SPI_TRY
, spi_trycnt
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
280 &key_spi_trycnt
, 0, "");
282 /* minimum spi value to allocate automatically. */
283 SYSCTL_INT(_net_key
, KEYCTL_SPI_MIN_VALUE
, spi_minval
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
284 &key_spi_minval
, 0, "");
286 /* maximun spi value to allocate automatically. */
287 SYSCTL_INT(_net_key
, KEYCTL_SPI_MAX_VALUE
, spi_maxval
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
288 &key_spi_maxval
, 0, "");
290 /* interval to initialize randseed */
291 SYSCTL_INT(_net_key
, KEYCTL_RANDOM_INT
, int_random
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
292 &key_int_random
, 0, "");
294 /* lifetime for larval SA; thread safe due to > compare */
295 SYSCTL_INT(_net_key
, KEYCTL_LARVAL_LIFETIME
, larval_lifetime
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
296 &key_larval_lifetime
, 0, "");
298 /* counter for blocking to send SADB_ACQUIRE to IKEd */
299 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_COUNT
, blockacq_count
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
300 &key_blockacq_count
, 0, "");
302 /* lifetime for blocking to send SADB_ACQUIRE to IKEd: Thread safe, > compare */
303 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_LIFETIME
, blockacq_lifetime
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
304 &key_blockacq_lifetime
, 0, "");
307 SYSCTL_INT(_net_key
, KEYCTL_ESP_AUTH
, esp_auth
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
308 &ipsec_esp_auth
, 0, "");
310 /* minimum ESP key length */
311 SYSCTL_INT(_net_key
, KEYCTL_ESP_KEYMIN
, esp_keymin
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
312 &ipsec_esp_keymin
, 0, "");
314 /* minimum AH key length */
315 SYSCTL_INT(_net_key
, KEYCTL_AH_KEYMIN
, ah_keymin
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
316 &ipsec_ah_keymin
, 0, "");
318 /* perfered old SA rather than new SA */
319 SYSCTL_INT(_net_key
, KEYCTL_PREFERED_OLDSA
, prefered_oldsa
, CTLFLAG_RW
| CTLFLAG_LOCKED
,\
320 &key_preferred_oldsa
, 0, "");
322 /* time between NATT keepalives in seconds, 0 disabled */
323 SYSCTL_INT(_net_key
, KEYCTL_NATT_KEEPALIVE_INTERVAL
, natt_keepalive_interval
, CTLFLAG_RW
| CTLFLAG_LOCKED
,\
324 &natt_keepalive_interval
, 0, "");
326 /* PF_KEY statistics */
327 SYSCTL_STRUCT(_net_key
, KEYCTL_PFKEYSTAT
, pfkeystat
, CTLFLAG_RD
| CTLFLAG_LOCKED
,\
328 &pfkeystat
, pfkeystat
, "");
331 #define LIST_FOREACH(elm, head, field) \
332 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
334 #define __LIST_CHAINED(elm) \
335 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
336 #define LIST_INSERT_TAIL(head, elm, type, field) \
338 struct type *curelm = LIST_FIRST(head); \
339 if (curelm == NULL) {\
340 LIST_INSERT_HEAD(head, elm, field); \
342 while (LIST_NEXT(curelm, field)) \
343 curelm = LIST_NEXT(curelm, field);\
344 LIST_INSERT_AFTER(curelm, elm, field);\
348 #define KEY_CHKSASTATE(head, sav, name) \
350 if ((head) != (sav)) { \
351 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
352 (name), (head), (sav))); \
357 #define KEY_CHKSPDIR(head, sp, name) \
359 if ((head) != (sp)) { \
360 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
361 "anyway continue.\n", \
362 (name), (head), (sp))); \
367 #define KMALLOC_WAIT(p, t, n) \
368 ((p) = (t) _MALLOC((u_int32_t)(n), M_SECA, M_WAITOK))
369 #define KMALLOC_NOWAIT(p, t, n) \
370 ((p) = (t) _MALLOC((u_int32_t)(n), M_SECA, M_NOWAIT))
372 _FREE((caddr_t)(p), M_SECA);
374 #define KMALLOC_WAIT(p, t, n) \
376 ((p) = (t)_MALLOC((u_int32_t)(n), M_SECA, M_WAITOK)); \
377 printf("%s %d: %p <- KMALLOC_WAIT(%s, %d)\n", \
378 __FILE__, __LINE__, (p), #t, n); \
380 #define KMALLOC_NOWAIT(p, t, n) \
382 ((p) = (t)_MALLOC((u_int32_t)(n), M_SECA, M_NOWAIT)); \
383 printf("%s %d: %p <- KMALLOC_NOWAIT(%s, %d)\n", \
384 __FILE__, __LINE__, (p), #t, n); \
389 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
390 _FREE((caddr_t)(p), M_SECA); \
395 * set parameters into secpolicyindex buffer.
396 * Must allocate secpolicyindex buffer passed to this function.
398 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, ifp, s_s, s_e, d_s, d_e, idx) \
400 bzero((idx), sizeof(struct secpolicyindex)); \
401 (idx)->dir = (_dir); \
402 (idx)->prefs = (ps); \
403 (idx)->prefd = (pd); \
404 (idx)->ul_proto = (ulp); \
405 (idx)->internal_if = (ifp); \
406 if (s) bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
407 if (d) bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
408 if (s_s) bcopy((s_s), &(idx)->src_range.start, ((struct sockaddr *)(s_s))->sa_len); \
409 if (s_e) bcopy((s_e), &(idx)->src_range.end, ((struct sockaddr *)(s_e))->sa_len); \
410 if (d_s) bcopy((d_s), &(idx)->dst_range.start, ((struct sockaddr *)(d_s))->sa_len); \
411 if (d_e) bcopy((d_e), &(idx)->dst_range.end, ((struct sockaddr *)(d_e))->sa_len); \
415 * set parameters into secasindex buffer.
416 * Must allocate secasindex buffer before calling this function.
418 #define KEY_SETSECASIDX(p, m, r, s, d, ifi, idx) \
420 bzero((idx), sizeof(struct secasindex)); \
421 (idx)->proto = (p); \
423 (idx)->reqid = (r); \
424 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
425 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
426 (idx)->ipsec_ifindex = (ifi); \
431 u_int32_t getspi_count
; /* the avarage of count to try to get new SPI */
435 struct sadb_msg
*msg
;
436 struct sadb_ext
*ext
[SADB_EXT_MAX
+ 1];
437 int extoff
[SADB_EXT_MAX
+ 1];
438 int extlen
[SADB_EXT_MAX
+ 1];
441 static struct secpolicy
*__key_getspbyid(u_int32_t id
);
442 static struct secasvar
*key_do_allocsa_policy(struct secashead
*, u_int
, u_int16_t
);
443 static int key_do_get_translated_port(struct secashead
*, struct secasvar
*, u_int
);
444 static void key_delsp(struct secpolicy
*);
445 static struct secpolicy
*key_getsp(struct secpolicyindex
*);
446 static u_int32_t
key_newreqid(void);
447 static struct mbuf
*key_gather_mbuf(struct mbuf
*,
448 const struct sadb_msghdr
*, int, int, int *);
449 static int key_spdadd(struct socket
*, struct mbuf
*,
450 const struct sadb_msghdr
*);
451 static u_int32_t
key_getnewspid(void);
452 static int key_spddelete(struct socket
*, struct mbuf
*,
453 const struct sadb_msghdr
*);
454 static int key_spddelete2(struct socket
*, struct mbuf
*,
455 const struct sadb_msghdr
*);
456 static int key_spdenable(struct socket
*, struct mbuf
*,
457 const struct sadb_msghdr
*);
458 static int key_spddisable(struct socket
*, struct mbuf
*,
459 const struct sadb_msghdr
*);
460 static int key_spdget(struct socket
*, struct mbuf
*,
461 const struct sadb_msghdr
*);
462 static int key_spdflush(struct socket
*, struct mbuf
*,
463 const struct sadb_msghdr
*);
464 static int key_spddump(struct socket
*, struct mbuf
*,
465 const struct sadb_msghdr
*);
466 static struct mbuf
*key_setdumpsp(struct secpolicy
*,
467 u_int8_t
, u_int32_t
, u_int32_t
);
468 static u_int
key_getspreqmsglen(struct secpolicy
*);
469 static int key_spdexpire(struct secpolicy
*);
470 static struct secashead
*key_newsah(struct secasindex
*, ifnet_t
, u_int
, u_int8_t
);
471 static struct secasvar
*key_newsav(struct mbuf
*,
472 const struct sadb_msghdr
*, struct secashead
*, int *,
474 static struct secashead
*key_getsah(struct secasindex
*);
475 static struct secasvar
*key_checkspidup(struct secasindex
*, u_int32_t
);
476 static void key_setspi
__P((struct secasvar
*, u_int32_t
));
477 static struct secasvar
*key_getsavbyspi(struct secashead
*, u_int32_t
);
478 static int key_setsaval(struct secasvar
*, struct mbuf
*,
479 const struct sadb_msghdr
*);
480 static int key_mature(struct secasvar
*);
481 static struct mbuf
*key_setdumpsa(struct secasvar
*, u_int8_t
,
482 u_int8_t
, u_int32_t
, u_int32_t
);
483 static struct mbuf
*key_setsadbmsg(u_int8_t
, u_int16_t
, u_int8_t
,
484 u_int32_t
, pid_t
, u_int16_t
);
485 static struct mbuf
*key_setsadbsa(struct secasvar
*);
486 static struct mbuf
*key_setsadbaddr(u_int16_t
,
487 struct sockaddr
*, u_int8_t
, u_int16_t
);
488 static struct mbuf
*key_setsadbipsecif(ifnet_t
, ifnet_t
, ifnet_t
, int);
490 static struct mbuf
*key_setsadbident(u_int16_t
, u_int16_t
, caddr_t
,
493 static struct mbuf
*key_setsadbxsa2(u_int8_t
, u_int32_t
, u_int32_t
, u_int16_t
);
494 static struct mbuf
*key_setsadbxpolicy(u_int16_t
, u_int8_t
,
496 static void *key_newbuf(const void *, u_int
);
498 static int key_ismyaddr6(struct sockaddr_in6
*);
500 static void key_update_natt_keepalive_timestamp(struct secasvar
*, struct secasvar
*);
502 /* flags for key_cmpsaidx() */
503 #define CMP_HEAD 0x1 /* protocol, addresses. */
504 #define CMP_PORT 0x2 /* additionally HEAD, reqid, mode. */
505 #define CMP_REQID 0x4 /* additionally HEAD, reqid. */
506 #define CMP_MODE 0x8 /* additionally mode. */
507 #define CMP_EXACTLY 0xF /* all elements. */
508 static int key_cmpsaidx(struct secasindex
*, struct secasindex
*, int);
510 static int key_cmpspidx_exactly(struct secpolicyindex
*,
511 struct secpolicyindex
*);
512 static int key_cmpspidx_withmask(struct secpolicyindex
*,
513 struct secpolicyindex
*);
514 static int key_sockaddrcmp(struct sockaddr
*, struct sockaddr
*, int);
515 static int key_is_addr_in_range(struct sockaddr_storage
*, struct secpolicyaddrrange
*);
516 static int key_bbcmp(caddr_t
, caddr_t
, u_int
);
517 static void key_srandom(void);
518 static u_int16_t
key_satype2proto(u_int8_t
);
519 static u_int8_t
key_proto2satype(u_int16_t
);
521 static int key_getspi(struct socket
*, struct mbuf
*,
522 const struct sadb_msghdr
*);
523 static u_int32_t
key_do_getnewspi(struct sadb_spirange
*, struct secasindex
*);
524 static int key_update(struct socket
*, struct mbuf
*,
525 const struct sadb_msghdr
*);
527 static struct secasvar
*key_getsavbyseq(struct secashead
*, u_int32_t
);
529 static int key_add(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
530 static int key_setident(struct secashead
*, struct mbuf
*,
531 const struct sadb_msghdr
*);
532 static struct mbuf
*key_getmsgbuf_x1(struct mbuf
*, const struct sadb_msghdr
*);
533 static int key_delete(struct socket
*, struct mbuf
*,
534 const struct sadb_msghdr
*);
535 static int key_get(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
537 static void key_getcomb_setlifetime(struct sadb_comb
*);
539 static struct mbuf
*key_getcomb_esp(void);
541 static struct mbuf
*key_getcomb_ah(void);
542 static struct mbuf
*key_getcomb_ipcomp(void);
543 static struct mbuf
*key_getprop(const struct secasindex
*);
545 static int key_acquire(struct secasindex
*, struct secpolicy
*);
546 #ifndef IPSEC_NONBLOCK_ACQUIRE
547 static struct secacq
*key_newacq(struct secasindex
*);
548 static struct secacq
*key_getacq(struct secasindex
*);
549 static struct secacq
*key_getacqbyseq(u_int32_t
);
551 static struct secspacq
*key_newspacq(struct secpolicyindex
*);
552 static struct secspacq
*key_getspacq(struct secpolicyindex
*);
553 static int key_acquire2(struct socket
*, struct mbuf
*,
554 const struct sadb_msghdr
*);
555 static int key_register(struct socket
*, struct mbuf
*,
556 const struct sadb_msghdr
*);
557 static int key_expire(struct secasvar
*);
558 static int key_flush(struct socket
*, struct mbuf
*,
559 const struct sadb_msghdr
*);
560 static int key_dump(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
561 static int key_promisc(struct socket
*, struct mbuf
*,
562 const struct sadb_msghdr
*);
563 static int key_senderror(struct socket
*, struct mbuf
*, int);
564 static int key_validate_ext(const struct sadb_ext
*, int);
565 static int key_align(struct mbuf
*, struct sadb_msghdr
*);
566 static struct mbuf
*key_alloc_mbuf(int);
567 static int key_getsastat (struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
568 static int key_setsaval2(struct secasvar
*sav
,
574 struct sadb_key
*key_auth
,
575 u_int16_t key_auth_len
,
576 struct sadb_key
*key_enc
,
577 u_int16_t key_enc_len
,
582 struct sadb_lifetime
*lifetime_hard
,
583 struct sadb_lifetime
*lifetime_soft
);
585 extern int ipsec_bypass
;
586 extern int esp_udp_encap_port
;
587 int ipsec_send_natt_keepalive(struct secasvar
*sav
);
588 bool ipsec_fill_offload_frame(ifnet_t ifp
, struct secasvar
*sav
, struct ipsec_offload_frame
*frame
, size_t frame_data_offset
);
589 u_int32_t
key_fill_offload_frames_for_savs (ifnet_t ifp
, struct ipsec_offload_frame
*frames_array
, u_int32_t frames_array_count
, size_t frame_data_offset
);
591 void key_init(struct protosw
*, struct domain
*);
595 * setup locks, call raw_init(), and then init timer and associated data
599 key_init(struct protosw
*pp
, struct domain
*dp
)
601 static int key_initialized
= 0;
604 VERIFY((pp
->pr_flags
& (PR_INITIALIZED
|PR_ATTACHED
)) == PR_ATTACHED
);
606 _CASSERT(PFKEY_ALIGN8(sizeof(struct sadb_msg
)) <= _MHLEN
);
612 sadb_mutex_grp_attr
= lck_grp_attr_alloc_init();
613 sadb_mutex_grp
= lck_grp_alloc_init("sadb", sadb_mutex_grp_attr
);
614 sadb_mutex_attr
= lck_attr_alloc_init();
616 lck_mtx_init(sadb_mutex
, sadb_mutex_grp
, sadb_mutex_attr
);
618 pfkey_stat_mutex_grp_attr
= lck_grp_attr_alloc_init();
619 pfkey_stat_mutex_grp
= lck_grp_alloc_init("pfkey_stat", pfkey_stat_mutex_grp_attr
);
620 pfkey_stat_mutex_attr
= lck_attr_alloc_init();
622 lck_mtx_init(pfkey_stat_mutex
, pfkey_stat_mutex_grp
, pfkey_stat_mutex_attr
);
624 for (i
= 0; i
< SPIHASHSIZE
; i
++)
625 LIST_INIT(&spihash
[i
]);
629 bzero((caddr_t
)&key_cb
, sizeof(key_cb
));
631 for (i
= 0; i
< IPSEC_DIR_MAX
; i
++) {
632 LIST_INIT(&sptree
[i
]);
634 ipsec_policy_count
= 0;
638 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
639 LIST_INIT(®tree
[i
]);
643 #ifndef IPSEC_NONBLOCK_ACQUIRE
646 LIST_INIT(&spacqtree
);
650 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
651 ip4_def_policy
.refcnt
++; /*never reclaim this*/
654 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
655 ip6_def_policy
.refcnt
++; /*never reclaim this*/
658 key_timehandler_running
= 0;
660 /* initialize key statistics */
661 keystat
.getspi_count
= 1;
664 printf("IPsec: Initialized Security Association Processing.\n");
669 key_start_timehandler(void)
671 /* must be called while locked */
672 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
673 if (key_timehandler_running
== 0) {
674 key_timehandler_running
= 1;
675 (void)timeout((void *)key_timehandler
, (void *)0, hz
);
678 /* Turn off the ipsec bypass */
679 if (ipsec_bypass
!= 0)
683 /* %%% IPsec policy management */
685 * allocating a SP for OUTBOUND or INBOUND packet.
686 * Must call key_freesp() later.
687 * OUT: NULL: not found
688 * others: found and return the pointer.
692 struct secpolicyindex
*spidx
,
695 struct secpolicy
*sp
;
698 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
701 panic("key_allocsp: NULL pointer is passed.\n");
703 /* check direction */
705 case IPSEC_DIR_INBOUND
:
706 case IPSEC_DIR_OUTBOUND
:
709 panic("key_allocsp: Invalid direction is passed.\n");
713 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
714 printf("*** objects\n");
715 kdebug_secpolicyindex(spidx
));
717 lck_mtx_lock(sadb_mutex
);
718 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
719 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
720 printf("*** in SPD\n");
721 kdebug_secpolicyindex(&sp
->spidx
));
723 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
726 /* If the policy is disabled, skip */
727 if (sp
->disabled
> 0)
730 /* If the incoming spidx specifies bound if,
731 ignore unbound policies*/
732 if (spidx
->internal_if
!= NULL
733 && (sp
->spidx
.internal_if
== NULL
|| sp
->ipsec_if
== NULL
))
736 if (key_cmpspidx_withmask(&sp
->spidx
, spidx
))
739 lck_mtx_unlock(sadb_mutex
);
744 /* found a SPD entry */
746 sp
->lastused
= tv
.tv_sec
;
748 lck_mtx_unlock(sadb_mutex
);
751 KEY_CHKSPDIR(sp
->spidx
.dir
, dir
, "key_allocsp");
752 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
753 printf("DP key_allocsp cause refcnt++:%d SP:0x%llx\n",
754 sp
->refcnt
, (uint64_t)VM_KERNEL_ADDRPERM(sp
)));
759 * return a policy that matches this particular inbound packet.
764 struct sockaddr
*osrc
,
765 struct sockaddr
*odst
,
766 struct sockaddr
*isrc
,
767 struct sockaddr
*idst
)
769 struct secpolicy
*sp
;
770 const int dir
= IPSEC_DIR_INBOUND
;
772 struct ipsecrequest
*r1
, *r2
, *p
;
773 struct sockaddr
*os
, *od
, *is
, *id
;
774 struct secpolicyindex spidx
;
776 if (isrc
->sa_family
!= idst
->sa_family
) {
777 ipseclog((LOG_ERR
, "protocol family mismatched %d != %d\n.",
778 isrc
->sa_family
, idst
->sa_family
));
782 lck_mtx_lock(sadb_mutex
);
783 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
784 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
788 for (p
= sp
->req
; p
; p
= p
->next
) {
789 if (p
->saidx
.mode
!= IPSEC_MODE_TUNNEL
)
796 /* here we look at address matches only */
798 if (isrc
->sa_len
> sizeof(spidx
.src
) ||
799 idst
->sa_len
> sizeof(spidx
.dst
))
801 bcopy(isrc
, &spidx
.src
, isrc
->sa_len
);
802 bcopy(idst
, &spidx
.dst
, idst
->sa_len
);
803 if (!key_cmpspidx_withmask(&sp
->spidx
, &spidx
))
806 is
= (struct sockaddr
*)&r1
->saidx
.src
;
807 id
= (struct sockaddr
*)&r1
->saidx
.dst
;
808 if (key_sockaddrcmp(is
, isrc
, 0) ||
809 key_sockaddrcmp(id
, idst
, 0))
813 os
= (struct sockaddr
*)&r2
->saidx
.src
;
814 od
= (struct sockaddr
*)&r2
->saidx
.dst
;
815 if (key_sockaddrcmp(os
, osrc
, 0) ||
816 key_sockaddrcmp(od
, odst
, 0))
822 lck_mtx_unlock(sadb_mutex
);
827 sp
->lastused
= tv
.tv_sec
;
829 lck_mtx_unlock(sadb_mutex
);
833 struct secasvar
*key_alloc_outbound_sav_for_interface(ifnet_t interface
, int family
)
835 struct secashead
*sah
;
836 struct secasvar
*sav
;
839 const u_int
*saorder_state_valid
;
841 struct sockaddr_in
*sin
;
844 if (interface
== NULL
)
847 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
849 lck_mtx_lock(sadb_mutex
);
851 LIST_FOREACH(sah
, &sahtree
, chain
) {
852 if (sah
->ipsec_if
== interface
&&
853 (family
== AF_INET6
|| sah
->saidx
.dst
.ss_family
== family
) && /* IPv6 can go over IPv4 */
854 sah
->dir
== IPSEC_DIR_OUTBOUND
) {
855 /* This SAH is linked to the IPSec interface, and the right family. We found it! */
856 if (key_preferred_oldsa
) {
857 saorder_state_valid
= saorder_state_valid_prefer_old
;
858 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
860 saorder_state_valid
= saorder_state_valid_prefer_new
;
861 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
864 sin
= (struct sockaddr_in
*)&sah
->saidx
.dst
;
865 dstport
= sin
->sin_port
;
866 if (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
)
867 sin
->sin_port
= IPSEC_PORT_ANY
;
869 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
870 state
= saorder_state_valid
[stateidx
];
871 sav
= key_do_allocsa_policy(sah
, state
, dstport
);
873 lck_mtx_unlock(sadb_mutex
);
882 lck_mtx_unlock(sadb_mutex
);
887 * allocating an SA entry for an *OUTBOUND* packet.
888 * checking each request entries in SP, and acquire an SA if need.
889 * OUT: 0: there are valid requests.
890 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
894 struct ipsecrequest
*isr
,
895 struct secasindex
*saidx
,
896 struct secasvar
**sav
)
900 struct sockaddr_in
*sin
;
902 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
907 if (isr
== NULL
|| saidx
== NULL
)
908 panic("key_checkrequest: NULL pointer is passed.\n");
911 switch (saidx
->mode
) {
912 case IPSEC_MODE_TRANSPORT
:
913 case IPSEC_MODE_TUNNEL
:
917 panic("key_checkrequest: Invalid policy defined.\n");
920 /* get current level */
921 level
= ipsec_get_reqlevel(isr
);
925 * key_allocsa_policy should allocate the oldest SA available.
926 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
929 *sav
= key_allocsa_policy(saidx
);
931 /* When there is SA. */
937 * Remove dst port - used for special natt support - don't call
938 * key_acquire with it.
940 if (saidx
->mode
== IPSEC_MODE_TRANSPORT
) {
941 sin
= (struct sockaddr_in
*)&saidx
->dst
;
942 sin
->sin_port
= IPSEC_PORT_ANY
;
944 if ((error
= key_acquire(saidx
, isr
->sp
)) != 0) {
945 /* XXX What should I do ? */
946 ipseclog((LOG_DEBUG
, "key_checkrequest: error %d returned "
947 "from key_acquire.\n", error
));
951 return level
== IPSEC_LEVEL_REQUIRE
? ENOENT
: 0;
955 * allocating a SA for policy entry from SAD.
956 * NOTE: searching SAD of aliving state.
957 * OUT: NULL: not found.
958 * others: found and return the pointer.
960 u_int32_t sah_search_calls
= 0;
961 u_int32_t sah_search_count
= 0;
964 struct secasindex
*saidx
)
966 struct secashead
*sah
;
967 struct secasvar
*sav
;
968 u_int stateidx
, state
;
969 const u_int
*saorder_state_valid
;
971 struct sockaddr_in
*sin
;
974 lck_mtx_lock(sadb_mutex
);
976 LIST_FOREACH(sah
, &sahtree
, chain
) {
978 if (sah
->state
== SADB_SASTATE_DEAD
)
980 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_MODE
| CMP_REQID
))
983 lck_mtx_unlock(sadb_mutex
);
989 * search a valid state list for outbound packet.
990 * This search order is important.
992 if (key_preferred_oldsa
) {
993 saorder_state_valid
= saorder_state_valid_prefer_old
;
994 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
996 saorder_state_valid
= saorder_state_valid_prefer_new
;
997 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
1001 sin
= (struct sockaddr_in
*)&saidx
->dst
;
1002 dstport
= sin
->sin_port
;
1003 if (saidx
->mode
== IPSEC_MODE_TRANSPORT
)
1004 sin
->sin_port
= IPSEC_PORT_ANY
;
1006 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
1008 state
= saorder_state_valid
[stateidx
];
1010 sav
= key_do_allocsa_policy(sah
, state
, dstport
);
1012 lck_mtx_unlock(sadb_mutex
);
1016 lck_mtx_unlock(sadb_mutex
);
1021 key_send_delete (struct secasvar
*sav
)
1023 struct mbuf
*m
, *result
;
1026 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
1028 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0)
1029 panic("key_do_allocsa_policy: invalid proto is passed.\n");
1031 m
= key_setsadbmsg(SADB_DELETE
, 0,
1032 satype
, 0, 0, sav
->refcnt
- 1);
1037 /* set sadb_address for saidx's. */
1038 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
1039 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
1040 sav
->sah
->saidx
.src
.ss_len
<< 3,
1046 /* set sadb_address for saidx's. */
1047 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
1048 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
1049 sav
->sah
->saidx
.src
.ss_len
<< 3,
1055 /* create SA extension */
1056 m
= key_setsadbsa(sav
);
1061 if (result
->m_len
< sizeof(struct sadb_msg
)) {
1062 result
= m_pullup(result
,
1063 sizeof(struct sadb_msg
));
1068 result
->m_pkthdr
.len
= 0;
1069 for (m
= result
; m
; m
= m
->m_next
)
1070 result
->m_pkthdr
.len
+= m
->m_len
;
1071 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
1072 PFKEY_UNIT64(result
->m_pkthdr
.len
);
1074 if (key_sendup_mbuf(NULL
, result
,
1075 KEY_SENDUP_REGISTERED
))
1078 key_freesav(sav
, KEY_SADB_LOCKED
);
1082 * searching SAD with direction, protocol, mode and state.
1083 * called by key_allocsa_policy().
1086 * others : found, pointer to a SA.
1088 static struct secasvar
*
1089 key_do_allocsa_policy(
1090 struct secashead
*sah
,
1094 struct secasvar
*sav
, *nextsav
, *candidate
, *natt_candidate
, *no_natt_candidate
, *d
;
1096 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1100 natt_candidate
= NULL
;
1101 no_natt_candidate
= NULL
;
1103 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
1107 nextsav
= LIST_NEXT(sav
, chain
);
1110 KEY_CHKSASTATE(sav
->state
, state
, "key_do_allocsa_policy");
1112 if (sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&& dstport
&&
1113 ((sav
->flags
& SADB_X_EXT_NATT
) != 0) &&
1114 ntohs(dstport
) != sav
->remote_ike_port
)
1117 if (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
&&
1118 ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0) &&
1119 ntohs(dstport
) != sav
->remote_ike_port
)
1120 continue; /* skip this one - not a match - or not UDP */
1122 if ((sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&&
1123 ((sav
->flags
& SADB_X_EXT_NATT
) != 0)) ||
1124 (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
&&
1125 ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0))) {
1126 if (natt_candidate
== NULL
) {
1127 natt_candidate
= sav
;
1130 candidate
= natt_candidate
;
1132 if (no_natt_candidate
== NULL
) {
1133 no_natt_candidate
= sav
;
1136 candidate
= no_natt_candidate
;
1139 /* Which SA is the better ? */
1141 /* sanity check 2 */
1142 if (candidate
->lft_c
== NULL
|| sav
->lft_c
== NULL
)
1143 panic("key_do_allocsa_policy: "
1144 "lifetime_current is NULL.\n");
1146 /* What the best method is to compare ? */
1147 if (key_preferred_oldsa
) {
1148 if (candidate
->lft_c
->sadb_lifetime_addtime
>
1149 sav
->lft_c
->sadb_lifetime_addtime
) {
1150 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
1151 natt_candidate
= sav
;
1153 no_natt_candidate
= sav
;
1159 /* prefered new sa rather than old sa */
1160 if (candidate
->lft_c
->sadb_lifetime_addtime
<
1161 sav
->lft_c
->sadb_lifetime_addtime
) {
1163 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
1164 natt_candidate
= sav
;
1166 no_natt_candidate
= sav
;
1171 * prepared to delete the SA when there is more
1172 * suitable candidate and the lifetime of the SA is not
1175 if (d
->lft_c
->sadb_lifetime_addtime
!= 0) {
1180 /* choose latest if both types present */
1181 if (natt_candidate
== NULL
)
1182 candidate
= no_natt_candidate
;
1183 else if (no_natt_candidate
== NULL
)
1184 candidate
= natt_candidate
;
1185 else if (sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&& dstport
)
1186 candidate
= natt_candidate
;
1187 else if (natt_candidate
->lft_c
->sadb_lifetime_addtime
>
1188 no_natt_candidate
->lft_c
->sadb_lifetime_addtime
)
1189 candidate
= natt_candidate
;
1191 candidate
= no_natt_candidate
;
1194 candidate
->refcnt
++;
1195 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1196 printf("DP allocsa_policy cause "
1197 "refcnt++:%d SA:0x%llx\n", candidate
->refcnt
,
1198 (uint64_t)VM_KERNEL_ADDRPERM(candidate
)));
1204 * allocating a SA entry for a *INBOUND* packet.
1205 * Must call key_freesav() later.
1206 * OUT: positive: pointer to a sav.
1207 * NULL: not found, or error occurred.
1209 * In the comparison, source address will be ignored for RFC2401 conformance.
1210 * To quote, from section 4.1:
1211 * A security association is uniquely identified by a triple consisting
1212 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1213 * security protocol (AH or ESP) identifier.
1214 * Note that, however, we do need to keep source address in IPsec SA.
1215 * IKE specification and PF_KEY specification do assume that we
1216 * keep source address in IPsec SA. We see a tricky situation here.
1226 struct secasvar
*sav
, *match
;
1227 u_int stateidx
, state
, tmpidx
, matchidx
;
1228 struct sockaddr_in sin
;
1229 struct sockaddr_in6 sin6
;
1230 const u_int
*saorder_state_valid
;
1233 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1236 if (src
== NULL
|| dst
== NULL
)
1237 panic("key_allocsa: NULL pointer is passed.\n");
1240 * when both systems employ similar strategy to use a SA.
1241 * the search order is important even in the inbound case.
1243 if (key_preferred_oldsa
) {
1244 saorder_state_valid
= saorder_state_valid_prefer_old
;
1245 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
1247 saorder_state_valid
= saorder_state_valid_prefer_new
;
1248 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
1253 * XXX: to be checked internal IP header somewhere. Also when
1254 * IPsec tunnel packet is received. But ESP tunnel mode is
1255 * encrypted so we can't check internal IP header.
1258 * search a valid state list for inbound packet.
1259 * the search order is not important.
1262 matchidx
= arraysize
;
1263 lck_mtx_lock(sadb_mutex
);
1264 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
1265 if (sav
->spi
!= spi
)
1267 if (proto
!= sav
->sah
->saidx
.proto
)
1269 if (family
!= sav
->sah
->saidx
.src
.ss_family
||
1270 family
!= sav
->sah
->saidx
.dst
.ss_family
)
1273 for (stateidx
= 0; stateidx
< matchidx
; stateidx
++) {
1274 state
= saorder_state_valid
[stateidx
];
1275 if (sav
->state
== state
) {
1280 if (tmpidx
>= matchidx
)
1283 #if 0 /* don't check src */
1284 /* check src address */
1287 bzero(&sin
, sizeof(sin
));
1288 sin
.sin_family
= AF_INET
;
1289 sin
.sin_len
= sizeof(sin
);
1290 bcopy(src
, &sin
.sin_addr
,
1291 sizeof(sin
.sin_addr
));
1292 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
1293 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0)
1297 bzero(&sin6
, sizeof(sin6
));
1298 sin6
.sin6_family
= AF_INET6
;
1299 sin6
.sin6_len
= sizeof(sin6
);
1300 bcopy(src
, &sin6
.sin6_addr
,
1301 sizeof(sin6
.sin6_addr
));
1302 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
1303 /* kame fake scopeid */
1304 sin6
.sin6_scope_id
=
1305 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
1306 sin6
.sin6_addr
.s6_addr16
[1] = 0;
1308 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
1309 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0)
1313 ipseclog((LOG_DEBUG
, "key_allocsa: "
1314 "unknown address family=%d.\n",
1320 /* check dst address */
1323 bzero(&sin
, sizeof(sin
));
1324 sin
.sin_family
= AF_INET
;
1325 sin
.sin_len
= sizeof(sin
);
1326 bcopy(dst
, &sin
.sin_addr
,
1327 sizeof(sin
.sin_addr
));
1328 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
1329 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0)
1334 bzero(&sin6
, sizeof(sin6
));
1335 sin6
.sin6_family
= AF_INET6
;
1336 sin6
.sin6_len
= sizeof(sin6
);
1337 bcopy(dst
, &sin6
.sin6_addr
,
1338 sizeof(sin6
.sin6_addr
));
1339 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
1340 /* kame fake scopeid */
1341 sin6
.sin6_scope_id
=
1342 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
1343 sin6
.sin6_addr
.s6_addr16
[1] = 0;
1345 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
1346 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0)
1350 ipseclog((LOG_DEBUG
, "key_allocsa: "
1351 "unknown address family=%d.\n", family
));
1362 lck_mtx_unlock(sadb_mutex
);
1367 lck_mtx_unlock(sadb_mutex
);
1368 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1369 printf("DP allocsa cause refcnt++:%d SA:0x%llx\n",
1370 match
->refcnt
, (uint64_t)VM_KERNEL_ADDRPERM(match
)));
1375 key_natt_get_translated_port(
1376 struct secasvar
*outsav
)
1379 struct secasindex saidx
;
1380 struct secashead
*sah
;
1381 u_int stateidx
, state
;
1382 const u_int
*saorder_state_valid
;
1385 /* get sa for incoming */
1386 saidx
.mode
= outsav
->sah
->saidx
.mode
;
1388 saidx
.proto
= outsav
->sah
->saidx
.proto
;
1389 bcopy(&outsav
->sah
->saidx
.src
, &saidx
.dst
, sizeof(struct sockaddr_in
));
1390 bcopy(&outsav
->sah
->saidx
.dst
, &saidx
.src
, sizeof(struct sockaddr_in
));
1392 lck_mtx_lock(sadb_mutex
);
1393 LIST_FOREACH(sah
, &sahtree
, chain
) {
1394 if (sah
->state
== SADB_SASTATE_DEAD
)
1396 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_MODE
))
1399 lck_mtx_unlock(sadb_mutex
);
1404 * Found sah - now go thru list of SAs and find
1405 * matching remote ike port. If found - set
1406 * sav->natt_encapsulated_src_port and return the port.
1409 * search a valid state list for outbound packet.
1410 * This search order is important.
1412 if (key_preferred_oldsa
) {
1413 saorder_state_valid
= saorder_state_valid_prefer_old
;
1414 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
1416 saorder_state_valid
= saorder_state_valid_prefer_new
;
1417 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
1420 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
1421 state
= saorder_state_valid
[stateidx
];
1422 if (key_do_get_translated_port(sah
, outsav
, state
)) {
1423 lck_mtx_unlock(sadb_mutex
);
1424 return outsav
->natt_encapsulated_src_port
;
1427 lck_mtx_unlock(sadb_mutex
);
1432 key_do_get_translated_port(
1433 struct secashead
*sah
,
1434 struct secasvar
*outsav
,
1437 struct secasvar
*currsav
, *nextsav
, *candidate
;
1440 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1445 for (currsav
= LIST_FIRST(&sah
->savtree
[state
]);
1447 currsav
= nextsav
) {
1449 nextsav
= LIST_NEXT(currsav
, chain
);
1452 KEY_CHKSASTATE(currsav
->state
, state
, "key_do_get_translated_port");
1454 if ((currsav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) == 0 ||
1455 currsav
->remote_ike_port
!= outsav
->remote_ike_port
)
1458 if (candidate
== NULL
) {
1459 candidate
= currsav
;
1463 /* Which SA is the better ? */
1465 /* sanity check 2 */
1466 if (candidate
->lft_c
== NULL
|| currsav
->lft_c
== NULL
)
1467 panic("key_do_get_translated_port: "
1468 "lifetime_current is NULL.\n");
1470 /* What the best method is to compare ? */
1471 if (key_preferred_oldsa
) {
1472 if (candidate
->lft_c
->sadb_lifetime_addtime
>
1473 currsav
->lft_c
->sadb_lifetime_addtime
) {
1474 candidate
= currsav
;
1480 /* prefered new sa rather than old sa */
1481 if (candidate
->lft_c
->sadb_lifetime_addtime
<
1482 currsav
->lft_c
->sadb_lifetime_addtime
)
1483 candidate
= currsav
;
1487 outsav
->natt_encapsulated_src_port
= candidate
->natt_encapsulated_src_port
;
1495 * Must be called after calling key_allocsp().
1499 struct secpolicy
*sp
,
1505 panic("key_freesp: NULL pointer is passed.\n");
1508 lck_mtx_lock(sadb_mutex
);
1510 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1512 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1513 printf("DP freesp cause refcnt--:%d SP:0x%llx\n",
1514 sp
->refcnt
, (uint64_t)VM_KERNEL_ADDRPERM(sp
)));
1516 if (sp
->refcnt
== 0)
1519 lck_mtx_unlock(sadb_mutex
);
1524 * Must be called after calling key_allocsa().
1525 * This function is called by key_freesp() to free some SA allocated
1530 struct secasvar
*sav
,
1536 panic("key_freesav: NULL pointer is passed.\n");
1539 lck_mtx_lock(sadb_mutex
);
1541 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1543 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1544 printf("DP freesav cause refcnt--:%d SA:0x%llx SPI %u\n",
1545 sav
->refcnt
, (uint64_t)VM_KERNEL_ADDRPERM(sav
),
1546 (u_int32_t
)ntohl(sav
->spi
)));
1548 if (sav
->refcnt
== 0)
1551 lck_mtx_unlock(sadb_mutex
);
1555 /* %%% SPD management */
1557 * free security policy entry.
1561 struct secpolicy
*sp
)
1566 panic("key_delsp: NULL pointer is passed.\n");
1568 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1569 sp
->state
= IPSEC_SPSTATE_DEAD
;
1572 return; /* can't free */
1574 /* remove from SP index */
1575 if (__LIST_CHAINED(sp
)) {
1576 LIST_REMOVE(sp
, chain
);
1577 ipsec_policy_count
--;
1580 if (sp
->spidx
.internal_if
) {
1581 ifnet_release(sp
->spidx
.internal_if
);
1582 sp
->spidx
.internal_if
= NULL
;
1586 ifnet_release(sp
->ipsec_if
);
1587 sp
->ipsec_if
= NULL
;
1590 if (sp
->outgoing_if
) {
1591 ifnet_release(sp
->outgoing_if
);
1592 sp
->outgoing_if
= NULL
;
1596 struct ipsecrequest
*isr
= sp
->req
, *nextisr
;
1598 while (isr
!= NULL
) {
1599 nextisr
= isr
->next
;
1604 keydb_delsecpolicy(sp
);
1611 * OUT: NULL : not found
1612 * others : found, pointer to a SP.
1614 static struct secpolicy
*
1616 struct secpolicyindex
*spidx
)
1618 struct secpolicy
*sp
;
1620 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1624 panic("key_getsp: NULL pointer is passed.\n");
1626 LIST_FOREACH(sp
, &sptree
[spidx
->dir
], chain
) {
1627 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1629 if (key_cmpspidx_exactly(spidx
, &sp
->spidx
)) {
1640 * OUT: NULL : not found
1641 * others : found, pointer to a SP.
1647 struct secpolicy
*sp
;
1649 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1651 lck_mtx_lock(sadb_mutex
);
1652 sp
= __key_getspbyid(id
);
1653 lck_mtx_unlock(sadb_mutex
);
1658 static struct secpolicy
*
1659 __key_getspbyid(u_int32_t id
)
1661 struct secpolicy
*sp
;
1663 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1665 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_INBOUND
], chain
) {
1666 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1674 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_OUTBOUND
], chain
) {
1675 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1689 struct secpolicy
*newsp
= NULL
;
1691 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1692 newsp
= keydb_newsecpolicy();
1703 * create secpolicy structure from sadb_x_policy structure.
1704 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1705 * so must be set properly later.
1709 struct sadb_x_policy
*xpl0
,
1713 struct secpolicy
*newsp
;
1715 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1719 panic("key_msg2sp: NULL pointer was passed.\n");
1720 if (len
< sizeof(*xpl0
))
1721 panic("key_msg2sp: invalid length.\n");
1722 if (len
!= PFKEY_EXTLEN(xpl0
)) {
1723 ipseclog((LOG_DEBUG
, "key_msg2sp: Invalid msg length.\n"));
1728 if ((newsp
= key_newsp()) == NULL
) {
1733 newsp
->spidx
.dir
= xpl0
->sadb_x_policy_dir
;
1734 newsp
->policy
= xpl0
->sadb_x_policy_type
;
1737 switch (xpl0
->sadb_x_policy_type
) {
1738 case IPSEC_POLICY_DISCARD
:
1739 case IPSEC_POLICY_GENERATE
:
1740 case IPSEC_POLICY_NONE
:
1741 case IPSEC_POLICY_ENTRUST
:
1742 case IPSEC_POLICY_BYPASS
:
1746 case IPSEC_POLICY_IPSEC
:
1749 struct sadb_x_ipsecrequest
*xisr
;
1750 struct ipsecrequest
**p_isr
= &newsp
->req
;
1752 /* validity check */
1753 if (PFKEY_EXTLEN(xpl0
) < sizeof(*xpl0
)) {
1754 ipseclog((LOG_DEBUG
,
1755 "key_msg2sp: Invalid msg length.\n"));
1756 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1761 tlen
= PFKEY_EXTLEN(xpl0
) - sizeof(*xpl0
);
1762 xisr
= (struct sadb_x_ipsecrequest
*)(xpl0
+ 1);
1767 if (xisr
->sadb_x_ipsecrequest_len
< sizeof(*xisr
)) {
1768 ipseclog((LOG_DEBUG
, "key_msg2sp: "
1769 "invalid ipsecrequest length.\n"));
1770 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1775 /* allocate request buffer */
1776 KMALLOC_WAIT(*p_isr
, struct ipsecrequest
*, sizeof(**p_isr
));
1777 if ((*p_isr
) == NULL
) {
1778 ipseclog((LOG_DEBUG
,
1779 "key_msg2sp: No more memory.\n"));
1780 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1784 bzero(*p_isr
, sizeof(**p_isr
));
1787 (*p_isr
)->next
= NULL
;
1789 switch (xisr
->sadb_x_ipsecrequest_proto
) {
1792 case IPPROTO_IPCOMP
:
1795 ipseclog((LOG_DEBUG
,
1796 "key_msg2sp: invalid proto type=%u\n",
1797 xisr
->sadb_x_ipsecrequest_proto
));
1798 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1799 *error
= EPROTONOSUPPORT
;
1802 (*p_isr
)->saidx
.proto
= xisr
->sadb_x_ipsecrequest_proto
;
1804 switch (xisr
->sadb_x_ipsecrequest_mode
) {
1805 case IPSEC_MODE_TRANSPORT
:
1806 case IPSEC_MODE_TUNNEL
:
1808 case IPSEC_MODE_ANY
:
1810 ipseclog((LOG_DEBUG
,
1811 "key_msg2sp: invalid mode=%u\n",
1812 xisr
->sadb_x_ipsecrequest_mode
));
1813 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1817 (*p_isr
)->saidx
.mode
= xisr
->sadb_x_ipsecrequest_mode
;
1819 switch (xisr
->sadb_x_ipsecrequest_level
) {
1820 case IPSEC_LEVEL_DEFAULT
:
1821 case IPSEC_LEVEL_USE
:
1822 case IPSEC_LEVEL_REQUIRE
:
1824 case IPSEC_LEVEL_UNIQUE
:
1825 /* validity check */
1827 * If range violation of reqid, kernel will
1828 * update it, don't refuse it.
1830 if (xisr
->sadb_x_ipsecrequest_reqid
1831 > IPSEC_MANUAL_REQID_MAX
) {
1832 ipseclog((LOG_DEBUG
,
1833 "key_msg2sp: reqid=%d range "
1834 "violation, updated by kernel.\n",
1835 xisr
->sadb_x_ipsecrequest_reqid
));
1836 xisr
->sadb_x_ipsecrequest_reqid
= 0;
1839 /* allocate new reqid id if reqid is zero. */
1840 if (xisr
->sadb_x_ipsecrequest_reqid
== 0) {
1842 if ((reqid
= key_newreqid()) == 0) {
1843 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1847 (*p_isr
)->saidx
.reqid
= reqid
;
1848 xisr
->sadb_x_ipsecrequest_reqid
= reqid
;
1850 /* set it for manual keying. */
1851 (*p_isr
)->saidx
.reqid
=
1852 xisr
->sadb_x_ipsecrequest_reqid
;
1857 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid level=%u\n",
1858 xisr
->sadb_x_ipsecrequest_level
));
1859 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1863 (*p_isr
)->level
= xisr
->sadb_x_ipsecrequest_level
;
1865 /* set IP addresses if there */
1866 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
1867 struct sockaddr
*paddr
;
1869 paddr
= (struct sockaddr
*)(xisr
+ 1);
1871 /* validity check */
1873 > sizeof((*p_isr
)->saidx
.src
)) {
1874 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1875 "address length.\n"));
1876 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1880 bcopy(paddr
, &(*p_isr
)->saidx
.src
,
1883 paddr
= (struct sockaddr
*)((caddr_t
)paddr
1886 /* validity check */
1888 > sizeof((*p_isr
)->saidx
.dst
)) {
1889 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1890 "address length.\n"));
1891 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1895 bcopy(paddr
, &(*p_isr
)->saidx
.dst
,
1899 (*p_isr
)->sp
= newsp
;
1901 /* initialization for the next. */
1902 p_isr
= &(*p_isr
)->next
;
1903 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
1905 /* validity check */
1907 ipseclog((LOG_DEBUG
, "key_msg2sp: becoming tlen < 0.\n"));
1908 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1913 xisr
= (struct sadb_x_ipsecrequest
*)(void *)
1914 ((caddr_t
)xisr
+ xisr
->sadb_x_ipsecrequest_len
);
1919 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid policy type.\n"));
1920 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1932 lck_mtx_lock(sadb_mutex
);
1933 static u_int32_t auto_reqid
= IPSEC_MANUAL_REQID_MAX
+ 1;
1936 /* The reqid must be limited to 16 bits because the PF_KEY message format only uses
1937 16 bits for this field. Once it becomes larger than 16 bits - ipsec fails to
1938 work anymore. Changing the PF_KEY message format would introduce compatibility
1939 issues. This code now tests to see if the tentative reqid is in use */
1942 struct secpolicy
*sp
;
1943 struct ipsecrequest
*isr
;
1946 auto_reqid
= (auto_reqid
== 0xFFFF
1947 ? IPSEC_MANUAL_REQID_MAX
+ 1 : auto_reqid
+ 1);
1949 /* check for uniqueness */
1951 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
1952 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
1953 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1954 if (isr
->saidx
.reqid
== auto_reqid
) {
1967 lck_mtx_unlock(sadb_mutex
);
1972 * copy secpolicy struct to sadb_x_policy structure indicated.
1976 struct secpolicy
*sp
)
1978 struct sadb_x_policy
*xpl
;
1985 panic("key_sp2msg: NULL pointer was passed.\n");
1987 tlen
= key_getspreqmsglen(sp
);
1989 m
= key_alloc_mbuf(tlen
);
1990 if (!m
|| m
->m_next
) { /*XXX*/
1998 xpl
= mtod(m
, struct sadb_x_policy
*);
2001 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(tlen
);
2002 xpl
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2003 xpl
->sadb_x_policy_type
= sp
->policy
;
2004 xpl
->sadb_x_policy_dir
= sp
->spidx
.dir
;
2005 xpl
->sadb_x_policy_id
= sp
->id
;
2006 p
= (caddr_t
)xpl
+ sizeof(*xpl
);
2008 /* if is the policy for ipsec ? */
2009 if (sp
->policy
== IPSEC_POLICY_IPSEC
) {
2010 struct sadb_x_ipsecrequest
*xisr
;
2011 struct ipsecrequest
*isr
;
2013 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2015 xisr
= (struct sadb_x_ipsecrequest
*)(void *)p
;
2017 xisr
->sadb_x_ipsecrequest_proto
= isr
->saidx
.proto
;
2018 xisr
->sadb_x_ipsecrequest_mode
= isr
->saidx
.mode
;
2019 xisr
->sadb_x_ipsecrequest_level
= isr
->level
;
2020 xisr
->sadb_x_ipsecrequest_reqid
= isr
->saidx
.reqid
;
2023 bcopy(&isr
->saidx
.src
, p
, isr
->saidx
.src
.ss_len
);
2024 p
+= isr
->saidx
.src
.ss_len
;
2025 bcopy(&isr
->saidx
.dst
, p
, isr
->saidx
.dst
.ss_len
);
2026 p
+= isr
->saidx
.src
.ss_len
;
2028 xisr
->sadb_x_ipsecrequest_len
=
2029 PFKEY_ALIGN8(sizeof(*xisr
)
2030 + isr
->saidx
.src
.ss_len
2031 + isr
->saidx
.dst
.ss_len
);
2038 /* m will not be freed nor modified */
2039 static struct mbuf
*
2040 key_gather_mbuf(struct mbuf
*m
, const struct sadb_msghdr
*mhp
,
2041 int ndeep
, int nitem
, int *items
)
2045 struct mbuf
*result
= NULL
, *n
;
2048 if (m
== NULL
|| mhp
== NULL
)
2049 panic("null pointer passed to key_gather");
2051 for (i
= 0; i
< nitem
; i
++) {
2053 if (idx
< 0 || idx
> SADB_EXT_MAX
)
2055 /* don't attempt to pull empty extension */
2056 if (idx
== SADB_EXT_RESERVED
&& mhp
->msg
== NULL
)
2058 if (idx
!= SADB_EXT_RESERVED
&&
2059 (mhp
->ext
[idx
] == NULL
|| mhp
->extlen
[idx
] == 0))
2062 if (idx
== SADB_EXT_RESERVED
) {
2063 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2064 MGETHDR(n
, M_WAITOK
, MT_DATA
); // sadb_msg len < MHLEN - enforced by _CASSERT
2069 m_copydata(m
, 0, sizeof(struct sadb_msg
),
2071 } else if (i
< ndeep
) {
2072 len
= mhp
->extlen
[idx
];
2073 n
= key_alloc_mbuf(len
);
2074 if (!n
|| n
->m_next
) { /*XXX*/
2079 m_copydata(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
2082 n
= m_copym(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
2094 if ((result
->m_flags
& M_PKTHDR
) != 0) {
2095 result
->m_pkthdr
.len
= 0;
2096 for (n
= result
; n
; n
= n
->m_next
)
2097 result
->m_pkthdr
.len
+= n
->m_len
;
2108 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
2109 * add a entry to SP database, when received
2110 * <base, address(SD), (lifetime(H),) policy>
2112 * Adding to SP database,
2114 * <base, address(SD), (lifetime(H),) policy>
2115 * to the socket which was send.
2117 * SPDADD set a unique policy entry.
2118 * SPDSETIDX like SPDADD without a part of policy requests.
2119 * SPDUPDATE replace a unique policy entry.
2121 * m will always be freed.
2127 const struct sadb_msghdr
*mhp
)
2129 struct sadb_address
*src0
, *dst0
, *src1
, *dst1
;
2130 struct sadb_x_policy
*xpl0
, *xpl
;
2131 struct sadb_lifetime
*lft
= NULL
;
2132 struct secpolicyindex spidx
;
2133 struct secpolicy
*newsp
;
2135 ifnet_t internal_if
= NULL
;
2136 char *outgoing_if
= NULL
;
2137 char *ipsec_if
= NULL
;
2138 struct sadb_x_ipsecif
*ipsecifopts
= NULL
;
2140 int use_src_range
= 0;
2141 int use_dst_range
= 0;
2142 int init_disabled
= 0;
2143 int address_family
, address_len
;
2145 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2148 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2149 panic("key_spdadd: NULL pointer is passed.\n");
2151 if (mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
] != NULL
&& mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
] != NULL
) {
2154 if (mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_START
] != NULL
&& mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_END
] != NULL
) {
2158 if ((!use_src_range
&& mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
) ||
2159 (!use_dst_range
&& mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) ||
2160 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
2161 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2162 return key_senderror(so
, m
, EINVAL
);
2164 if ((use_src_range
&& (mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_SRC_START
] < sizeof(struct sadb_address
)
2165 || mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_SRC_END
] < sizeof(struct sadb_address
))) ||
2166 (!use_src_range
&& mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
)) ||
2167 (use_dst_range
&& (mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_DST_START
] < sizeof(struct sadb_address
)
2168 || mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_DST_END
] < sizeof(struct sadb_address
))) ||
2169 (!use_dst_range
&& mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) ||
2170 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2171 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2172 return key_senderror(so
, m
, EINVAL
);
2174 if (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
) {
2175 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
]
2176 < sizeof(struct sadb_lifetime
)) {
2177 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2178 return key_senderror(so
, m
, EINVAL
);
2180 lft
= (struct sadb_lifetime
*)
2181 (void *)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
2183 if (mhp
->ext
[SADB_X_EXT_IPSECIF
] != NULL
) {
2184 if (mhp
->extlen
[SADB_X_EXT_IPSECIF
] < sizeof(struct sadb_x_ipsecif
)) {
2185 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2186 return key_senderror(so
, m
, EINVAL
);
2190 if (use_src_range
) {
2191 src0
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
];
2192 src1
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
];
2194 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
2196 if (use_dst_range
) {
2197 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_START
];
2198 dst1
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_END
];
2200 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
2202 xpl0
= (struct sadb_x_policy
*)(void *)mhp
->ext
[SADB_X_EXT_POLICY
];
2203 ipsecifopts
= (struct sadb_x_ipsecif
*)(void *)mhp
->ext
[SADB_X_EXT_IPSECIF
];
2205 /* check addresses */
2206 address_family
= ((struct sockaddr
*)(src0
+ 1))->sa_family
;
2207 address_len
= ((struct sockaddr
*)(src0
+ 1))->sa_len
;
2208 if (use_src_range
) {
2209 if (((struct sockaddr
*)(src1
+ 1))->sa_family
!= address_family
||
2210 ((struct sockaddr
*)(src1
+ 1))->sa_len
!= address_len
) {
2211 return key_senderror(so
, m
, EINVAL
);
2214 if (((struct sockaddr
*)(dst0
+ 1))->sa_family
!= address_family
||
2215 ((struct sockaddr
*)(dst0
+ 1))->sa_len
!= address_len
) {
2216 return key_senderror(so
, m
, EINVAL
);
2218 if (use_dst_range
) {
2219 if (((struct sockaddr
*)(dst1
+ 1))->sa_family
!= address_family
||
2220 ((struct sockaddr
*)(dst1
+ 1))->sa_len
!= address_len
) {
2221 return key_senderror(so
, m
, EINVAL
);
2225 /* checking the direction. */
2226 switch (xpl0
->sadb_x_policy_dir
) {
2227 case IPSEC_DIR_INBOUND
:
2228 case IPSEC_DIR_OUTBOUND
:
2231 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid SP direction.\n"));
2232 mhp
->msg
->sadb_msg_errno
= EINVAL
;
2237 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
2238 if (xpl0
->sadb_x_policy_type
== IPSEC_POLICY_ENTRUST
2239 || xpl0
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
) {
2240 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid policy type.\n"));
2241 return key_senderror(so
, m
, EINVAL
);
2244 /* policy requests are mandatory when action is ipsec. */
2245 if (mhp
->msg
->sadb_msg_type
!= SADB_X_SPDSETIDX
2246 && xpl0
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
2247 && mhp
->extlen
[SADB_X_EXT_POLICY
] <= sizeof(*xpl0
)) {
2248 ipseclog((LOG_DEBUG
, "key_spdadd: some policy requests part required.\n"));
2249 return key_senderror(so
, m
, EINVAL
);
2252 /* Process interfaces */
2253 if (ipsecifopts
!= NULL
) {
2254 if (ipsecifopts
->sadb_x_ipsecif_internal_if
) {
2255 ifnet_find_by_name(ipsecifopts
->sadb_x_ipsecif_internal_if
, &internal_if
);
2257 if (ipsecifopts
->sadb_x_ipsecif_outgoing_if
) {
2258 outgoing_if
= ipsecifopts
->sadb_x_ipsecif_outgoing_if
;
2260 if (ipsecifopts
->sadb_x_ipsecif_ipsec_if
) {
2261 ipsec_if
= ipsecifopts
->sadb_x_ipsecif_ipsec_if
;
2263 init_disabled
= ipsecifopts
->sadb_x_ipsecif_init_disabled
;
2267 /* XXX boundary check against sa_len */
2268 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2271 src0
->sadb_address_prefixlen
,
2272 dst0
->sadb_address_prefixlen
,
2273 src0
->sadb_address_proto
,
2275 use_src_range
? src0
+ 1 : NULL
,
2276 use_src_range
? src1
+ 1 : NULL
,
2277 use_dst_range
? dst0
+ 1 : NULL
,
2278 use_dst_range
? dst1
+ 1 : NULL
,
2282 * checking there is SP already or not.
2283 * SPDUPDATE doesn't depend on whether there is a SP or not.
2284 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
2287 lck_mtx_lock(sadb_mutex
);
2288 newsp
= key_getsp(&spidx
);
2289 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
2291 newsp
->state
= IPSEC_SPSTATE_DEAD
;
2292 key_freesp(newsp
, KEY_SADB_LOCKED
);
2295 if (newsp
!= NULL
) {
2296 key_freesp(newsp
, KEY_SADB_LOCKED
);
2297 ipseclog((LOG_DEBUG
, "key_spdadd: a SP entry exists already.\n"));
2298 lck_mtx_unlock(sadb_mutex
);
2300 ifnet_release(internal_if
);
2303 return key_senderror(so
, m
, EEXIST
);
2306 lck_mtx_unlock(sadb_mutex
);
2308 /* allocation new SP entry */
2309 if ((newsp
= key_msg2sp(xpl0
, PFKEY_EXTLEN(xpl0
), &error
)) == NULL
) {
2311 ifnet_release(internal_if
);
2314 return key_senderror(so
, m
, error
);
2317 if ((newsp
->id
= key_getnewspid()) == 0) {
2318 keydb_delsecpolicy(newsp
);
2320 ifnet_release(internal_if
);
2323 return key_senderror(so
, m
, ENOBUFS
);
2326 /* XXX boundary check against sa_len */
2327 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2330 src0
->sadb_address_prefixlen
,
2331 dst0
->sadb_address_prefixlen
,
2332 src0
->sadb_address_proto
,
2334 use_src_range
? src0
+ 1 : NULL
,
2335 use_src_range
? src1
+ 1 : NULL
,
2336 use_dst_range
? dst0
+ 1 : NULL
,
2337 use_dst_range
? dst1
+ 1 : NULL
,
2342 * allow IPv6 over IPv4 tunnels using ESP -
2343 * otherwise reject if inner and outer address families not equal
2345 if (newsp
->req
&& newsp
->req
->saidx
.src
.ss_family
) {
2346 struct sockaddr
*sa
;
2347 sa
= (struct sockaddr
*)(src0
+ 1);
2348 if (sa
->sa_family
!= newsp
->req
->saidx
.src
.ss_family
) {
2349 if (newsp
->req
->saidx
.mode
!= IPSEC_MODE_TUNNEL
|| newsp
->req
->saidx
.proto
!= IPPROTO_ESP
2350 || sa
->sa_family
!= AF_INET6
|| newsp
->req
->saidx
.src
.ss_family
!= AF_INET
) {
2351 keydb_delsecpolicy(newsp
);
2353 ifnet_release(internal_if
);
2356 return key_senderror(so
, m
, EINVAL
);
2360 if (newsp
->req
&& newsp
->req
->saidx
.dst
.ss_family
) {
2361 struct sockaddr
*sa
;
2362 sa
= (struct sockaddr
*)(dst0
+ 1);
2363 if (sa
->sa_family
!= newsp
->req
->saidx
.dst
.ss_family
) {
2364 if (newsp
->req
->saidx
.mode
!= IPSEC_MODE_TUNNEL
|| newsp
->req
->saidx
.proto
!= IPPROTO_ESP
2365 || sa
->sa_family
!= AF_INET6
|| newsp
->req
->saidx
.dst
.ss_family
!= AF_INET
) {
2366 keydb_delsecpolicy(newsp
);
2368 ifnet_release(internal_if
);
2371 return key_senderror(so
, m
, EINVAL
);
2378 newsp
->created
= tv
.tv_sec
;
2379 newsp
->lastused
= tv
.tv_sec
;
2380 newsp
->lifetime
= lft
? lft
->sadb_lifetime_addtime
: 0;
2381 newsp
->validtime
= lft
? lft
->sadb_lifetime_usetime
: 0;
2383 if (outgoing_if
!= NULL
) {
2384 ifnet_find_by_name(outgoing_if
, &newsp
->outgoing_if
);
2386 if (ipsec_if
!= NULL
) {
2387 ifnet_find_by_name(ipsec_if
, &newsp
->ipsec_if
);
2389 if (init_disabled
> 0) {
2390 newsp
->disabled
= 1;
2393 newsp
->refcnt
= 1; /* do not reclaim until I say I do */
2394 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
2395 lck_mtx_lock(sadb_mutex
);
2397 * policies of type generate should be at the end of the SPD
2398 * because they function as default discard policies
2399 * Don't start timehandler for generate policies
2401 if (newsp
->policy
== IPSEC_POLICY_GENERATE
)
2402 LIST_INSERT_TAIL(&sptree
[newsp
->spidx
.dir
], newsp
, secpolicy
, chain
);
2403 else { /* XXX until we have policy ordering in the kernel */
2404 struct secpolicy
*tmpsp
;
2406 LIST_FOREACH(tmpsp
, &sptree
[newsp
->spidx
.dir
], chain
)
2407 if (tmpsp
->policy
== IPSEC_POLICY_GENERATE
)
2410 LIST_INSERT_BEFORE(tmpsp
, newsp
, chain
);
2412 LIST_INSERT_TAIL(&sptree
[newsp
->spidx
.dir
], newsp
, secpolicy
, chain
);
2413 key_start_timehandler();
2416 ipsec_policy_count
++;
2417 /* Turn off the ipsec bypass */
2418 if (ipsec_bypass
!= 0)
2421 /* delete the entry in spacqtree */
2422 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
2423 struct secspacq
*spacq
;
2424 if ((spacq
= key_getspacq(&spidx
)) != NULL
) {
2425 /* reset counter in order to deletion by timehandler. */
2427 spacq
->created
= tv
.tv_sec
;
2431 lck_mtx_unlock(sadb_mutex
);
2434 struct mbuf
*n
, *mpolicy
;
2435 struct sadb_msg
*newmsg
;
2438 /* create new sadb_msg to reply. */
2440 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2441 SADB_EXT_LIFETIME_HARD
, SADB_EXT_ADDRESS_SRC
,
2442 SADB_EXT_ADDRESS_DST
, SADB_X_EXT_ADDR_RANGE_SRC_START
, SADB_X_EXT_ADDR_RANGE_SRC_END
,
2443 SADB_X_EXT_ADDR_RANGE_DST_START
, SADB_X_EXT_ADDR_RANGE_DST_END
};
2444 n
= key_gather_mbuf(m
, mhp
, 2, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2446 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2447 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
,
2448 SADB_X_EXT_ADDR_RANGE_SRC_START
, SADB_X_EXT_ADDR_RANGE_SRC_END
,
2449 SADB_X_EXT_ADDR_RANGE_DST_START
, SADB_X_EXT_ADDR_RANGE_DST_END
};
2450 n
= key_gather_mbuf(m
, mhp
, 2, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2453 return key_senderror(so
, m
, ENOBUFS
);
2455 if (n
->m_len
< sizeof(*newmsg
)) {
2456 n
= m_pullup(n
, sizeof(*newmsg
));
2458 return key_senderror(so
, m
, ENOBUFS
);
2460 newmsg
= mtod(n
, struct sadb_msg
*);
2461 newmsg
->sadb_msg_errno
= 0;
2462 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2465 mpolicy
= m_pulldown(n
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)),
2466 sizeof(*xpl
), &off
);
2467 if (mpolicy
== NULL
) {
2468 /* n is already freed */
2469 return key_senderror(so
, m
, ENOBUFS
);
2471 xpl
= (struct sadb_x_policy
*)(void *)(mtod(mpolicy
, caddr_t
) + off
);
2472 if (xpl
->sadb_x_policy_exttype
!= SADB_X_EXT_POLICY
) {
2474 return key_senderror(so
, m
, EINVAL
);
2476 xpl
->sadb_x_policy_id
= newsp
->id
;
2479 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2484 * get new policy id.
2490 key_getnewspid(void)
2492 u_int32_t newid
= 0;
2493 int count
= key_spi_trycnt
; /* XXX */
2494 struct secpolicy
*sp
;
2496 /* when requesting to allocate spi ranged */
2497 lck_mtx_lock(sadb_mutex
);
2499 newid
= (policy_id
= (policy_id
== ~0 ? 1 : policy_id
+ 1));
2501 if ((sp
= __key_getspbyid(newid
)) == NULL
)
2504 key_freesp(sp
, KEY_SADB_LOCKED
);
2506 lck_mtx_unlock(sadb_mutex
);
2507 if (count
== 0 || newid
== 0) {
2508 ipseclog((LOG_DEBUG
, "key_getnewspid: to allocate policy id is failed.\n"));
2516 * SADB_SPDDELETE processing
2518 * <base, address(SD), policy(*)>
2519 * from the user(?), and set SADB_SASTATE_DEAD,
2521 * <base, address(SD), policy(*)>
2523 * policy(*) including direction of policy.
2525 * m will always be freed.
2531 const struct sadb_msghdr
*mhp
)
2533 struct sadb_address
*src0
, *dst0
, *src1
, *dst1
;
2534 struct sadb_x_policy
*xpl0
;
2535 struct secpolicyindex spidx
;
2536 struct secpolicy
*sp
;
2537 ifnet_t internal_if
= NULL
;
2538 struct sadb_x_ipsecif
*ipsecifopts
= NULL
;
2539 int use_src_range
= 0;
2540 int use_dst_range
= 0;
2542 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2545 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2546 panic("key_spddelete: NULL pointer is passed.\n");
2548 if (mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
] != NULL
&& mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
] != NULL
) {
2551 if (mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_START
] != NULL
&& mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_END
] != NULL
) {
2555 if ((!use_src_range
&& mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
) ||
2556 (!use_dst_range
&& mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) ||
2557 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
2558 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
2559 return key_senderror(so
, m
, EINVAL
);
2561 if ((use_src_range
&& (mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_SRC_START
] < sizeof(struct sadb_address
)
2562 || mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_SRC_END
] < sizeof(struct sadb_address
))) ||
2563 (!use_src_range
&& mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
)) ||
2564 (use_dst_range
&& (mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_DST_START
] < sizeof(struct sadb_address
)
2565 || mhp
->extlen
[SADB_X_EXT_ADDR_RANGE_DST_END
] < sizeof(struct sadb_address
))) ||
2566 (!use_dst_range
&& mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) ||
2567 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2568 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
2569 return key_senderror(so
, m
, EINVAL
);
2572 if (use_src_range
) {
2573 src0
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_START
];
2574 src1
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_SRC_END
];
2576 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
2578 if (use_dst_range
) {
2579 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_START
];
2580 dst1
= (struct sadb_address
*)mhp
->ext
[SADB_X_EXT_ADDR_RANGE_DST_END
];
2582 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
2584 xpl0
= (struct sadb_x_policy
*)(void *)mhp
->ext
[SADB_X_EXT_POLICY
];
2585 ipsecifopts
= (struct sadb_x_ipsecif
*)(void *)mhp
->ext
[SADB_X_EXT_IPSECIF
];
2587 /* checking the direction. */
2588 switch (xpl0
->sadb_x_policy_dir
) {
2589 case IPSEC_DIR_INBOUND
:
2590 case IPSEC_DIR_OUTBOUND
:
2593 ipseclog((LOG_DEBUG
, "key_spddelete: Invalid SP direction.\n"));
2594 return key_senderror(so
, m
, EINVAL
);
2597 /* Process interfaces */
2598 if (ipsecifopts
!= NULL
) {
2599 if (ipsecifopts
->sadb_x_ipsecif_internal_if
) {
2600 ifnet_find_by_name(ipsecifopts
->sadb_x_ipsecif_internal_if
, &internal_if
);
2605 /* XXX boundary check against sa_len */
2606 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2609 src0
->sadb_address_prefixlen
,
2610 dst0
->sadb_address_prefixlen
,
2611 src0
->sadb_address_proto
,
2613 use_src_range
? src0
+ 1 : NULL
,
2614 use_src_range
? src1
+ 1 : NULL
,
2615 use_dst_range
? dst0
+ 1 : NULL
,
2616 use_dst_range
? dst1
+ 1 : NULL
,
2619 /* Is there SP in SPD ? */
2620 lck_mtx_lock(sadb_mutex
);
2621 if ((sp
= key_getsp(&spidx
)) == NULL
) {
2622 ipseclog((LOG_DEBUG
, "key_spddelete: no SP found.\n"));
2623 lck_mtx_unlock(sadb_mutex
);
2625 ifnet_release(internal_if
);
2628 return key_senderror(so
, m
, EINVAL
);
2632 ifnet_release(internal_if
);
2636 /* save policy id to buffer to be returned. */
2637 xpl0
->sadb_x_policy_id
= sp
->id
;
2639 sp
->state
= IPSEC_SPSTATE_DEAD
;
2640 key_freesp(sp
, KEY_SADB_LOCKED
);
2641 lck_mtx_unlock(sadb_mutex
);
2646 struct sadb_msg
*newmsg
;
2647 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2648 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
,
2649 SADB_X_EXT_ADDR_RANGE_SRC_START
, SADB_X_EXT_ADDR_RANGE_SRC_END
,
2650 SADB_X_EXT_ADDR_RANGE_DST_START
, SADB_X_EXT_ADDR_RANGE_DST_END
};
2652 /* create new sadb_msg to reply. */
2653 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2655 return key_senderror(so
, m
, ENOBUFS
);
2657 newmsg
= mtod(n
, struct sadb_msg
*);
2658 newmsg
->sadb_msg_errno
= 0;
2659 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2662 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2667 * SADB_SPDDELETE2 processing
2670 * from the user(?), and set SADB_SASTATE_DEAD,
2674 * policy(*) including direction of policy.
2676 * m will always be freed.
2682 const struct sadb_msghdr
*mhp
)
2685 struct secpolicy
*sp
;
2687 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2690 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2691 panic("key_spddelete2: NULL pointer is passed.\n");
2693 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2694 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2695 ipseclog((LOG_DEBUG
, "key_spddelete2: invalid message is passed.\n"));
2696 key_senderror(so
, m
, EINVAL
);
2700 id
= ((struct sadb_x_policy
*)
2701 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2703 /* Is there SP in SPD ? */
2704 lck_mtx_lock(sadb_mutex
);
2705 if ((sp
= __key_getspbyid(id
)) == NULL
) {
2706 lck_mtx_unlock(sadb_mutex
);
2707 ipseclog((LOG_DEBUG
, "key_spddelete2: no SP found id:%u.\n", id
));
2708 return key_senderror(so
, m
, EINVAL
);
2711 sp
->state
= IPSEC_SPSTATE_DEAD
;
2712 key_freesp(sp
, KEY_SADB_LOCKED
);
2713 lck_mtx_unlock(sadb_mutex
);
2716 struct mbuf
*n
, *nn
;
2717 struct sadb_msg
*newmsg
;
2720 /* create new sadb_msg to reply. */
2721 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2724 return key_senderror(so
, m
, ENOBUFS
);
2725 MGETHDR(n
, M_WAITOK
, MT_DATA
);
2726 if (n
&& len
> MHLEN
) {
2727 MCLGET(n
, M_WAITOK
);
2728 if ((n
->m_flags
& M_EXT
) == 0) {
2734 return key_senderror(so
, m
, ENOBUFS
);
2740 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
2741 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2745 panic("length inconsistency in key_spddelete2");
2748 n
->m_next
= m_copym(m
, mhp
->extoff
[SADB_X_EXT_POLICY
],
2749 mhp
->extlen
[SADB_X_EXT_POLICY
], M_WAITOK
);
2752 return key_senderror(so
, m
, ENOBUFS
);
2755 n
->m_pkthdr
.len
= 0;
2756 for (nn
= n
; nn
; nn
= nn
->m_next
)
2757 n
->m_pkthdr
.len
+= nn
->m_len
;
2759 newmsg
= mtod(n
, struct sadb_msg
*);
2760 newmsg
->sadb_msg_errno
= 0;
2761 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2764 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2772 const struct sadb_msghdr
*mhp
)
2775 struct secpolicy
*sp
;
2777 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2780 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2781 panic("key_spdenable: NULL pointer is passed.\n");
2783 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2784 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2785 ipseclog((LOG_DEBUG
, "key_spdenable: invalid message is passed.\n"));
2786 key_senderror(so
, m
, EINVAL
);
2790 id
= ((struct sadb_x_policy
*)
2791 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2793 /* Is there SP in SPD ? */
2794 lck_mtx_lock(sadb_mutex
);
2795 if ((sp
= __key_getspbyid(id
)) == NULL
) {
2796 lck_mtx_unlock(sadb_mutex
);
2797 ipseclog((LOG_DEBUG
, "key_spdenable: no SP found id:%u.\n", id
));
2798 return key_senderror(so
, m
, EINVAL
);
2802 lck_mtx_unlock(sadb_mutex
);
2806 struct sadb_msg
*newmsg
;
2807 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
};
2809 /* create new sadb_msg to reply. */
2810 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2812 return key_senderror(so
, m
, ENOBUFS
);
2814 if (n
->m_len
< sizeof(struct sadb_msg
)) {
2815 n
= m_pullup(n
, sizeof(struct sadb_msg
));
2817 return key_senderror(so
, m
, ENOBUFS
);
2819 newmsg
= mtod(n
, struct sadb_msg
*);
2820 newmsg
->sadb_msg_errno
= 0;
2821 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2824 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2832 const struct sadb_msghdr
*mhp
)
2835 struct secpolicy
*sp
;
2837 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2840 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2841 panic("key_spddisable: NULL pointer is passed.\n");
2843 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2844 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2845 ipseclog((LOG_DEBUG
, "key_spddisable: invalid message is passed.\n"));
2846 key_senderror(so
, m
, EINVAL
);
2850 id
= ((struct sadb_x_policy
*)
2851 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2853 /* Is there SP in SPD ? */
2854 lck_mtx_lock(sadb_mutex
);
2855 if ((sp
= __key_getspbyid(id
)) == NULL
) {
2856 lck_mtx_unlock(sadb_mutex
);
2857 ipseclog((LOG_DEBUG
, "key_spddisable: no SP found id:%u.\n", id
));
2858 return key_senderror(so
, m
, EINVAL
);
2862 lck_mtx_unlock(sadb_mutex
);
2866 struct sadb_msg
*newmsg
;
2867 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
};
2869 /* create new sadb_msg to reply. */
2870 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2872 return key_senderror(so
, m
, ENOBUFS
);
2874 if (n
->m_len
< sizeof(struct sadb_msg
)) {
2875 n
= m_pullup(n
, sizeof(struct sadb_msg
));
2877 return key_senderror(so
, m
, ENOBUFS
);
2879 newmsg
= mtod(n
, struct sadb_msg
*);
2880 newmsg
->sadb_msg_errno
= 0;
2881 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2884 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2889 * SADB_X_GET processing
2894 * <base, address(SD), policy>
2896 * policy(*) including direction of policy.
2898 * m will always be freed.
2904 const struct sadb_msghdr
*mhp
)
2907 struct secpolicy
*sp
;
2910 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2913 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2914 panic("key_spdget: NULL pointer is passed.\n");
2916 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2917 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2918 ipseclog((LOG_DEBUG
, "key_spdget: invalid message is passed.\n"));
2919 return key_senderror(so
, m
, EINVAL
);
2922 id
= ((struct sadb_x_policy
*)
2923 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2925 /* Is there SP in SPD ? */
2926 lck_mtx_lock(sadb_mutex
);
2927 if ((sp
= __key_getspbyid(id
)) == NULL
) {
2928 ipseclog((LOG_DEBUG
, "key_spdget: no SP found id:%u.\n", id
));
2929 lck_mtx_unlock(sadb_mutex
);
2930 return key_senderror(so
, m
, ENOENT
);
2932 lck_mtx_unlock(sadb_mutex
);
2933 n
= key_setdumpsp(sp
, SADB_X_SPDGET
, 0, mhp
->msg
->sadb_msg_pid
);
2936 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
2938 return key_senderror(so
, m
, ENOBUFS
);
2942 * SADB_X_SPDACQUIRE processing.
2943 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2946 * to KMD, and expect to receive
2947 * <base> with SADB_X_SPDACQUIRE if error occurred,
2950 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2951 * policy(*) is without policy requests.
2954 * others: error number
2958 struct secpolicy
*sp
)
2960 struct mbuf
*result
= NULL
, *m
;
2961 struct secspacq
*newspacq
;
2964 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2968 panic("key_spdacquire: NULL pointer is passed.\n");
2969 if (sp
->req
!= NULL
)
2970 panic("key_spdacquire: called but there is request.\n");
2971 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
2972 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
2974 /* get a entry to check whether sent message or not. */
2975 lck_mtx_lock(sadb_mutex
);
2976 if ((newspacq
= key_getspacq(&sp
->spidx
)) != NULL
) {
2977 if (key_blockacq_count
< newspacq
->count
) {
2978 /* reset counter and do send message. */
2979 newspacq
->count
= 0;
2981 /* increment counter and do nothing. */
2983 lck_mtx_unlock(sadb_mutex
);
2987 /* make new entry for blocking to send SADB_ACQUIRE. */
2988 if ((newspacq
= key_newspacq(&sp
->spidx
)) == NULL
) {
2989 lck_mtx_unlock(sadb_mutex
);
2992 /* add to acqtree */
2993 LIST_INSERT_HEAD(&spacqtree
, newspacq
, chain
);
2994 key_start_timehandler();
2996 lck_mtx_unlock(sadb_mutex
);
2997 /* create new sadb_msg to reply. */
2998 m
= key_setsadbmsg(SADB_X_SPDACQUIRE
, 0, 0, 0, 0, 0);
3005 result
->m_pkthdr
.len
= 0;
3006 for (m
= result
; m
; m
= m
->m_next
)
3007 result
->m_pkthdr
.len
+= m
->m_len
;
3009 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3010 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3012 return key_sendup_mbuf(NULL
, m
, KEY_SENDUP_REGISTERED
);
3021 * SADB_SPDFLUSH processing
3024 * from the user, and free all entries in secpctree.
3028 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
3030 * m will always be freed.
3036 const struct sadb_msghdr
*mhp
)
3038 struct sadb_msg
*newmsg
;
3039 struct secpolicy
*sp
;
3043 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
3044 panic("key_spdflush: NULL pointer is passed.\n");
3046 if (m
->m_len
!= PFKEY_ALIGN8(sizeof(struct sadb_msg
)))
3047 return key_senderror(so
, m
, EINVAL
);
3049 lck_mtx_lock(sadb_mutex
);
3050 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
3051 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
3052 sp
->state
= IPSEC_SPSTATE_DEAD
;
3055 lck_mtx_unlock(sadb_mutex
);
3057 if (sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
3058 ipseclog((LOG_DEBUG
, "key_spdflush: No more memory.\n"));
3059 return key_senderror(so
, m
, ENOBUFS
);
3065 m
->m_pkthdr
.len
= m
->m_len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
3066 newmsg
= mtod(m
, struct sadb_msg
*);
3067 newmsg
->sadb_msg_errno
= 0;
3068 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
3070 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
3074 * SADB_SPDDUMP processing
3077 * from the user, and dump all SP leaves
3082 * m will always be freed.
3089 const struct sadb_msghdr
*mhp
)
3091 struct secpolicy
*sp
, **spbuf
= NULL
, **sp_ptr
;
3092 int cnt
= 0, bufcount
;
3098 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
3099 panic("key_spddump: NULL pointer is passed.\n");
3101 if ((bufcount
= ipsec_policy_count
) == 0) {
3105 bufcount
+= 256; /* extra */
3106 KMALLOC_WAIT(spbuf
, struct secpolicy
**, bufcount
* sizeof(struct secpolicy
*));
3107 if (spbuf
== NULL
) {
3108 ipseclog((LOG_DEBUG
, "key_spddump: No more memory.\n"));
3112 lck_mtx_lock(sadb_mutex
);
3113 /* search SPD entry, make list. */
3115 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
3116 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
3117 if (cnt
== bufcount
)
3118 break; /* buffer full */
3124 lck_mtx_unlock(sadb_mutex
);
3134 n
= key_setdumpsp(*sp_ptr
++, SADB_X_SPDDUMP
, cnt
,
3135 mhp
->msg
->sadb_msg_pid
);
3138 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
3141 lck_mtx_lock(sadb_mutex
);
3142 while (sp_ptr
> spbuf
)
3143 key_freesp(*(--sp_ptr
), KEY_SADB_LOCKED
);
3144 lck_mtx_unlock(sadb_mutex
);
3150 return key_senderror(so
, m
, error
);
3157 static struct mbuf
*
3159 struct secpolicy
*sp
,
3164 struct mbuf
*result
= NULL
, *m
;
3166 m
= key_setsadbmsg(type
, 0, SADB_SATYPE_UNSPEC
, seq
, pid
, sp
->refcnt
);
3171 if (sp
->spidx
.src_range
.start
.ss_len
> 0) {
3172 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_START
,
3173 (struct sockaddr
*)&sp
->spidx
.src_range
.start
, sp
->spidx
.prefs
,
3174 sp
->spidx
.ul_proto
);
3179 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_END
,
3180 (struct sockaddr
*)&sp
->spidx
.src_range
.end
, sp
->spidx
.prefs
,
3181 sp
->spidx
.ul_proto
);
3186 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
3187 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
3188 sp
->spidx
.ul_proto
);
3194 if (sp
->spidx
.dst_range
.start
.ss_len
> 0) {
3195 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_START
,
3196 (struct sockaddr
*)&sp
->spidx
.dst_range
.start
, sp
->spidx
.prefd
,
3197 sp
->spidx
.ul_proto
);
3202 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_END
,
3203 (struct sockaddr
*)&sp
->spidx
.dst_range
.end
, sp
->spidx
.prefd
,
3204 sp
->spidx
.ul_proto
);
3209 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
3210 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
3211 sp
->spidx
.ul_proto
);
3217 if (sp
->spidx
.internal_if
|| sp
->outgoing_if
|| sp
->ipsec_if
|| sp
->disabled
) {
3218 m
= key_setsadbipsecif(sp
->spidx
.internal_if
, sp
->outgoing_if
, sp
->ipsec_if
, sp
->disabled
);
3229 if ((result
->m_flags
& M_PKTHDR
) == 0)
3232 if (result
->m_len
< sizeof(struct sadb_msg
)) {
3233 result
= m_pullup(result
, sizeof(struct sadb_msg
));
3238 result
->m_pkthdr
.len
= 0;
3239 for (m
= result
; m
; m
= m
->m_next
)
3240 result
->m_pkthdr
.len
+= m
->m_len
;
3242 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3243 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3253 * get PFKEY message length for security policy and request.
3257 struct secpolicy
*sp
)
3261 tlen
= sizeof(struct sadb_x_policy
);
3263 /* if is the policy for ipsec ? */
3264 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
3267 /* get length of ipsec requests */
3269 struct ipsecrequest
*isr
;
3272 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
3273 len
= sizeof(struct sadb_x_ipsecrequest
)
3274 + isr
->saidx
.src
.ss_len
3275 + isr
->saidx
.dst
.ss_len
;
3277 tlen
+= PFKEY_ALIGN8(len
);
3285 * SADB_SPDEXPIRE processing
3287 * <base, address(SD), lifetime(CH), policy>
3291 * others : error number
3295 struct secpolicy
*sp
)
3297 struct mbuf
*result
= NULL
, *m
;
3300 struct sadb_lifetime
*lt
;
3302 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3306 panic("key_spdexpire: NULL pointer is passed.\n");
3308 /* set msg header */
3309 m
= key_setsadbmsg(SADB_X_SPDEXPIRE
, 0, 0, 0, 0, 0);
3316 /* create lifetime extension (current and hard) */
3317 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
3318 m
= key_alloc_mbuf(len
);
3319 if (!m
|| m
->m_next
) { /*XXX*/
3325 bzero(mtod(m
, caddr_t
), len
);
3326 lt
= mtod(m
, struct sadb_lifetime
*);
3327 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3328 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
3329 lt
->sadb_lifetime_allocations
= 0;
3330 lt
->sadb_lifetime_bytes
= 0;
3331 lt
->sadb_lifetime_addtime
= sp
->created
;
3332 lt
->sadb_lifetime_usetime
= sp
->lastused
;
3333 lt
= (struct sadb_lifetime
*)(void *)(mtod(m
, caddr_t
) + len
/ 2);
3334 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3335 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
3336 lt
->sadb_lifetime_allocations
= 0;
3337 lt
->sadb_lifetime_bytes
= 0;
3338 lt
->sadb_lifetime_addtime
= sp
->lifetime
;
3339 lt
->sadb_lifetime_usetime
= sp
->validtime
;
3342 /* set sadb_address(es) for source */
3343 if (sp
->spidx
.src_range
.start
.ss_len
> 0) {
3344 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_START
,
3345 (struct sockaddr
*)&sp
->spidx
.src_range
.start
, sp
->spidx
.prefs
,
3346 sp
->spidx
.ul_proto
);
3353 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_END
,
3354 (struct sockaddr
*)&sp
->spidx
.src_range
.end
, sp
->spidx
.prefs
,
3355 sp
->spidx
.ul_proto
);
3362 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
3363 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
3364 sp
->spidx
.ul_proto
);
3372 /* set sadb_address(es) for dest */
3373 if (sp
->spidx
.dst_range
.start
.ss_len
> 0) {
3374 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_START
,
3375 (struct sockaddr
*)&sp
->spidx
.dst_range
.start
, sp
->spidx
.prefd
,
3376 sp
->spidx
.ul_proto
);
3383 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_END
,
3384 (struct sockaddr
*)&sp
->spidx
.dst_range
.end
, sp
->spidx
.prefd
,
3385 sp
->spidx
.ul_proto
);
3392 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
3393 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
3394 sp
->spidx
.ul_proto
);
3410 if ((result
->m_flags
& M_PKTHDR
) == 0) {
3415 if (result
->m_len
< sizeof(struct sadb_msg
)) {
3416 result
= m_pullup(result
, sizeof(struct sadb_msg
));
3417 if (result
== NULL
) {
3423 result
->m_pkthdr
.len
= 0;
3424 for (m
= result
; m
; m
= m
->m_next
)
3425 result
->m_pkthdr
.len
+= m
->m_len
;
3427 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3428 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3430 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
3438 /* %%% SAD management */
3440 * allocating a memory for new SA head, and copy from the values of mhp.
3441 * OUT: NULL : failure due to the lack of memory.
3442 * others : pointer to new SA head.
3444 static struct secashead
*
3445 key_newsah(struct secasindex
*saidx
,
3450 struct secashead
*newsah
;
3454 panic("key_newsaidx: NULL pointer is passed.\n");
3456 newsah
= keydb_newsecashead();
3460 bcopy(saidx
, &newsah
->saidx
, sizeof(newsah
->saidx
));
3462 /* remove the ports */
3463 switch (saidx
->src
.ss_family
) {
3465 ((struct sockaddr_in
*)(&newsah
->saidx
.src
))->sin_port
= IPSEC_PORT_ANY
;
3468 ((struct sockaddr_in6
*)(&newsah
->saidx
.src
))->sin6_port
= IPSEC_PORT_ANY
;
3473 switch (saidx
->dst
.ss_family
) {
3475 ((struct sockaddr_in
*)(&newsah
->saidx
.dst
))->sin_port
= IPSEC_PORT_ANY
;
3478 ((struct sockaddr_in6
*)(&newsah
->saidx
.dst
))->sin6_port
= IPSEC_PORT_ANY
;
3484 newsah
->outgoing_if
= outgoing_if
;
3486 ifnet_reference(ipsec_if
);
3487 newsah
->ipsec_if
= ipsec_if
;
3490 /* add to saidxtree */
3491 newsah
->state
= SADB_SASTATE_MATURE
;
3492 LIST_INSERT_HEAD(&sahtree
, newsah
, chain
);
3493 key_start_timehandler();
3499 * delete SA index and all SA registerd.
3503 struct secashead
*sah
)
3505 struct secasvar
*sav
, *nextsav
;
3506 u_int stateidx
, state
;
3509 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3513 panic("key_delsah: NULL pointer is passed.\n");
3515 /* searching all SA registerd in the secindex. */
3517 stateidx
< _ARRAYLEN(saorder_state_any
);
3520 state
= saorder_state_any
[stateidx
];
3521 for (sav
= (struct secasvar
*)LIST_FIRST(&sah
->savtree
[state
]);
3525 nextsav
= LIST_NEXT(sav
, chain
);
3527 if (sav
->refcnt
> 0) {
3528 /* give up to delete this sa */
3534 KEY_CHKSASTATE(state
, sav
->state
, "key_delsah");
3536 key_freesav(sav
, KEY_SADB_LOCKED
);
3538 /* remove back pointer */
3544 /* don't delete sah only if there are savs. */
3548 ROUTE_RELEASE(&sah
->sa_route
);
3550 if (sah
->ipsec_if
) {
3551 ifnet_release(sah
->ipsec_if
);
3552 sah
->ipsec_if
= NULL
;
3563 /* remove from tree of SA index */
3564 if (__LIST_CHAINED(sah
))
3565 LIST_REMOVE(sah
, chain
);
3573 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
3574 * and copy the values of mhp into new buffer.
3575 * When SAD message type is GETSPI:
3576 * to set sequence number from acq_seq++,
3577 * to set zero to SPI.
3578 * not to call key_setsava().
3580 * others : pointer to new secasvar.
3582 * does not modify mbuf. does not free mbuf on error.
3584 static struct secasvar
*
3587 const struct sadb_msghdr
*mhp
,
3588 struct secashead
*sah
,
3592 struct secasvar
*newsav
;
3593 const struct sadb_sa
*xsa
;
3595 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3598 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
|| sah
== NULL
)
3599 panic("key_newsa: NULL pointer is passed.\n");
3601 KMALLOC_NOWAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3602 if (newsav
== NULL
) {
3603 lck_mtx_unlock(sadb_mutex
);
3604 KMALLOC_WAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3605 lck_mtx_lock(sadb_mutex
);
3606 if (newsav
== NULL
) {
3607 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3612 bzero((caddr_t
)newsav
, sizeof(struct secasvar
));
3614 switch (mhp
->msg
->sadb_msg_type
) {
3616 key_setspi(newsav
, 0);
3618 #if IPSEC_DOSEQCHECK
3619 /* sync sequence number */
3620 if (mhp
->msg
->sadb_msg_seq
== 0)
3622 (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
3625 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
3630 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
3632 ipseclog((LOG_DEBUG
, "key_newsa: invalid message is passed.\n"));
3636 xsa
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
3637 key_setspi(newsav
, xsa
->sadb_sa_spi
);
3638 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
3646 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
3647 if (((struct sadb_x_sa2
*)(void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_alwaysexpire
)
3648 newsav
->always_expire
= 1;
3649 newsav
->flags2
= ((struct sadb_x_sa2
*)(void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_flags
;
3650 if (newsav
->flags2
& SADB_X_EXT_SA2_DELETE_ON_DETACH
) {
3655 /* copy sav values */
3656 if (mhp
->msg
->sadb_msg_type
!= SADB_GETSPI
) {
3657 *errp
= key_setsaval(newsav
, m
, mhp
);
3663 /* For get SPI, if has a hard lifetime, apply */
3664 const struct sadb_lifetime
*lft0
;
3667 lft0
= (struct sadb_lifetime
*)(void *)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
3669 /* make lifetime for CURRENT */
3670 KMALLOC_NOWAIT(newsav
->lft_c
, struct sadb_lifetime
*,
3671 sizeof(struct sadb_lifetime
));
3672 if (newsav
->lft_c
== NULL
) {
3673 lck_mtx_unlock(sadb_mutex
);
3674 KMALLOC_WAIT(newsav
->lft_c
, struct sadb_lifetime
*,
3675 sizeof(struct sadb_lifetime
));
3676 lck_mtx_lock(sadb_mutex
);
3677 if (newsav
->lft_c
== NULL
) {
3678 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3687 newsav
->lft_c
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3688 newsav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
3689 newsav
->lft_c
->sadb_lifetime_allocations
= 0;
3690 newsav
->lft_c
->sadb_lifetime_bytes
= 0;
3691 newsav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
3692 newsav
->lft_c
->sadb_lifetime_usetime
= 0;
3694 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
] < sizeof(*lft0
)) {
3695 ipseclog((LOG_DEBUG
, "key_newsa: invalid hard lifetime ext len.\n"));
3700 newsav
->lft_h
= (struct sadb_lifetime
*)key_newbuf(lft0
, sizeof(*lft0
));
3701 if (newsav
->lft_h
== NULL
) {
3702 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3714 newsav
->created
= tv
.tv_sec
;
3717 newsav
->pid
= mhp
->msg
->sadb_msg_pid
;
3722 newsav
->state
= SADB_SASTATE_LARVAL
;
3723 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
3731 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
3732 * and copy the values passed into new buffer.
3733 * When SAD message type is GETSPI:
3734 * to set sequence number from acq_seq++,
3735 * to set zero to SPI.
3736 * not to call key_setsava().
3738 * others : pointer to new secasvar.
3741 key_newsav2(struct secashead
*sah
,
3747 struct sadb_key
*key_auth
,
3748 u_int16_t key_auth_len
,
3749 struct sadb_key
*key_enc
,
3750 u_int16_t key_enc_len
,
3751 u_int16_t natt_port
,
3755 struct sadb_lifetime
*lifetime_hard
,
3756 struct sadb_lifetime
*lifetime_soft
)
3758 struct secasvar
*newsav
;
3760 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3764 panic("key_newsa: NULL pointer is passed.\n");
3766 KMALLOC_NOWAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3767 if (newsav
== NULL
) {
3768 lck_mtx_unlock(sadb_mutex
);
3769 KMALLOC_WAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3770 lck_mtx_lock(sadb_mutex
);
3771 if (newsav
== NULL
) {
3772 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3776 bzero((caddr_t
)newsav
, sizeof(struct secasvar
));
3778 #if IPSEC_DOSEQCHECK
3779 /* sync sequence number */
3781 newsav
->seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
3785 key_setspi(newsav
, spi
);
3787 if (key_setsaval2(newsav
,
3811 newsav
->created
= tv
.tv_sec
;
3819 if (spi
&& key_auth
&& key_auth_len
&& key_enc
&& key_enc_len
) {
3820 newsav
->state
= SADB_SASTATE_MATURE
;
3821 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_MATURE
], newsav
,
3824 newsav
->state
= SADB_SASTATE_LARVAL
;
3825 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
3834 * free() SA variable entry.
3838 struct secasvar
*sav
)
3841 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3845 panic("key_delsav: NULL pointer is passed.\n");
3847 if (sav
->refcnt
> 0)
3848 return; /* can't free */
3850 /* remove from SA header */
3851 if (__LIST_CHAINED(sav
))
3852 LIST_REMOVE(sav
, chain
);
3855 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
)
3856 LIST_REMOVE(sav
, spihash
);
3858 if (sav
->key_auth
!= NULL
) {
3859 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
3860 KFREE(sav
->key_auth
);
3861 sav
->key_auth
= NULL
;
3863 if (sav
->key_enc
!= NULL
) {
3864 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
3865 KFREE(sav
->key_enc
);
3866 sav
->key_enc
= NULL
;
3869 bzero(sav
->sched
, sav
->schedlen
);
3873 if (sav
->replay
!= NULL
) {
3874 keydb_delsecreplay(sav
->replay
);
3877 if (sav
->lft_c
!= NULL
) {
3881 if (sav
->lft_h
!= NULL
) {
3885 if (sav
->lft_s
!= NULL
) {
3889 if (sav
->iv
!= NULL
) {
3903 * others : found, pointer to a SA.
3905 static struct secashead
*
3906 key_getsah(struct secasindex
*saidx
)
3908 struct secashead
*sah
;
3910 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3912 LIST_FOREACH(sah
, &sahtree
, chain
) {
3913 if (sah
->state
== SADB_SASTATE_DEAD
)
3915 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_REQID
))
3923 key_newsah2 (struct secasindex
*saidx
,
3926 struct secashead
*sah
;
3928 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3930 sah
= key_getsah(saidx
);
3932 return(key_newsah(saidx
, NULL
, 0, dir
));
3938 * check not to be duplicated SPI.
3939 * NOTE: this function is too slow due to searching all SAD.
3942 * others : found, pointer to a SA.
3944 static struct secasvar
*
3946 struct secasindex
*saidx
,
3949 struct secasvar
*sav
;
3950 u_int stateidx
, state
;
3952 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3954 /* check address family */
3955 if (saidx
->src
.ss_family
!= saidx
->dst
.ss_family
) {
3956 ipseclog((LOG_DEBUG
, "key_checkspidup: address family mismatched.\n"));
3961 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
3962 if (sav
->spi
!= spi
)
3965 stateidx
< _ARRAYLEN(saorder_state_alive
);
3967 state
= saorder_state_alive
[stateidx
];
3968 if (sav
->state
== state
&&
3969 key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
))
3979 struct secasvar
*sav
,
3982 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3984 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
)
3985 LIST_REMOVE(sav
, spihash
);
3986 LIST_INSERT_HEAD(&spihash
[SPIHASH(spi
)], sav
, spihash
);
3991 * search SAD litmited alive SA, protocol, SPI.
3994 * others : found, pointer to a SA.
3996 static struct secasvar
*
3998 struct secashead
*sah
,
4001 struct secasvar
*sav
, *match
;
4002 u_int stateidx
, state
, matchidx
;
4004 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4006 matchidx
= _ARRAYLEN(saorder_state_alive
);
4007 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
4008 if (sav
->spi
!= spi
)
4010 if (sav
->sah
!= sah
)
4012 for (stateidx
= 0; stateidx
< matchidx
; stateidx
++) {
4013 state
= saorder_state_alive
[stateidx
];
4014 if (sav
->state
== state
) {
4016 matchidx
= stateidx
;
4026 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
4027 * You must update these if need.
4031 * does not modify mbuf. does not free mbuf on error.
4035 struct secasvar
*sav
,
4037 const struct sadb_msghdr
*mhp
)
4040 const struct esp_algorithm
*algo
;
4045 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4048 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
4049 panic("key_setsaval: NULL pointer is passed.\n");
4051 /* initialization */
4053 sav
->key_auth
= NULL
;
4054 sav
->key_enc
= NULL
;
4061 sav
->remote_ike_port
= 0;
4062 sav
->natt_last_activity
= natt_now
;
4063 sav
->natt_encapsulated_src_port
= 0;
4066 if (mhp
->ext
[SADB_EXT_SA
] != NULL
) {
4067 const struct sadb_sa
*sa0
;
4069 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
4070 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(*sa0
)) {
4071 ipseclog((LOG_DEBUG
, "key_setsaval: invalid message size.\n"));
4076 sav
->alg_auth
= sa0
->sadb_sa_auth
;
4077 sav
->alg_enc
= sa0
->sadb_sa_encrypt
;
4078 sav
->flags
= sa0
->sadb_sa_flags
;
4081 * Verify that a nat-traversal port was specified if
4082 * the nat-traversal flag is set.
4084 if ((sav
->flags
& SADB_X_EXT_NATT
) != 0) {
4085 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa_2
) ||
4086 ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
== 0) {
4087 ipseclog((LOG_DEBUG
, "key_setsaval: natt port not set.\n"));
4091 sav
->remote_ike_port
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
;
4092 sav
->natt_interval
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_interval
;
4096 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
4097 * SADB_X_EXT_NATT is set and SADB_X_EXT_NATT_KEEPALIVE is not
4098 * set (we're not behind nat) - otherwise clear it.
4100 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
4101 if ((sav
->flags
& SADB_X_EXT_NATT
) == 0 ||
4102 (sav
->flags
& SADB_X_EXT_NATT_KEEPALIVE
) != 0)
4103 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
4106 if ((sa0
->sadb_sa_flags
& SADB_X_EXT_OLD
) == 0) {
4107 sav
->replay
= keydb_newsecreplay(sa0
->sadb_sa_replay
);
4108 if (sav
->replay
== NULL
) {
4109 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4116 /* Authentication keys */
4117 if (mhp
->ext
[SADB_EXT_KEY_AUTH
] != NULL
) {
4118 const struct sadb_key
*key0
;
4121 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_AUTH
];
4122 len
= mhp
->extlen
[SADB_EXT_KEY_AUTH
];
4125 if (len
< sizeof(*key0
)) {
4126 ipseclog((LOG_DEBUG
, "key_setsaval: invalid auth key ext len. len = %d\n", len
));
4130 switch (mhp
->msg
->sadb_msg_satype
) {
4131 case SADB_SATYPE_AH
:
4132 case SADB_SATYPE_ESP
:
4133 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
4134 sav
->alg_auth
!= SADB_X_AALG_NULL
)
4137 case SADB_X_SATYPE_IPCOMP
:
4143 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_auth values.\n"));
4147 sav
->key_auth
= (struct sadb_key
*)key_newbuf(key0
, len
);
4148 if (sav
->key_auth
== NULL
) {
4149 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4155 /* Encryption key */
4156 if (mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] != NULL
) {
4157 const struct sadb_key
*key0
;
4160 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_ENCRYPT
];
4161 len
= mhp
->extlen
[SADB_EXT_KEY_ENCRYPT
];
4164 if (len
< sizeof(*key0
)) {
4165 ipseclog((LOG_DEBUG
, "key_setsaval: invalid encryption key ext len. len = %d\n", len
));
4169 switch (mhp
->msg
->sadb_msg_satype
) {
4170 case SADB_SATYPE_ESP
:
4171 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
4172 sav
->alg_enc
!= SADB_EALG_NULL
) {
4173 ipseclog((LOG_DEBUG
, "key_setsaval: invalid ESP algorithm.\n"));
4177 sav
->key_enc
= (struct sadb_key
*)key_newbuf(key0
, len
);
4178 if (sav
->key_enc
== NULL
) {
4179 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4184 case SADB_X_SATYPE_IPCOMP
:
4185 if (len
!= PFKEY_ALIGN8(sizeof(struct sadb_key
)))
4187 sav
->key_enc
= NULL
; /*just in case*/
4189 case SADB_SATYPE_AH
:
4195 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_enc value.\n"));
4203 switch (mhp
->msg
->sadb_msg_satype
) {
4204 case SADB_SATYPE_ESP
:
4206 algo
= esp_algorithm_lookup(sav
->alg_enc
);
4207 if (algo
&& algo
->ivlen
)
4208 sav
->ivlen
= (*algo
->ivlen
)(algo
, sav
);
4209 if (sav
->ivlen
== 0)
4211 KMALLOC_NOWAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4213 lck_mtx_unlock(sadb_mutex
);
4214 KMALLOC_WAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4215 lck_mtx_lock(sadb_mutex
);
4217 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4224 key_randomfill(sav
->iv
, sav
->ivlen
);
4227 case SADB_SATYPE_AH
:
4228 case SADB_X_SATYPE_IPCOMP
:
4231 ipseclog((LOG_DEBUG
, "key_setsaval: invalid SA type.\n"));
4238 sav
->created
= tv
.tv_sec
;
4240 /* make lifetime for CURRENT */
4241 KMALLOC_NOWAIT(sav
->lft_c
, struct sadb_lifetime
*,
4242 sizeof(struct sadb_lifetime
));
4243 if (sav
->lft_c
== NULL
) {
4244 lck_mtx_unlock(sadb_mutex
);
4245 KMALLOC_WAIT(sav
->lft_c
, struct sadb_lifetime
*,
4246 sizeof(struct sadb_lifetime
));
4247 lck_mtx_lock(sadb_mutex
);
4248 if (sav
->lft_c
== NULL
) {
4249 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4257 sav
->lft_c
->sadb_lifetime_len
=
4258 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
4259 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
4260 sav
->lft_c
->sadb_lifetime_allocations
= 0;
4261 sav
->lft_c
->sadb_lifetime_bytes
= 0;
4262 sav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
4263 sav
->lft_c
->sadb_lifetime_usetime
= 0;
4265 /* lifetimes for HARD and SOFT */
4267 const struct sadb_lifetime
*lft0
;
4269 lft0
= (struct sadb_lifetime
*)
4270 (void *)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
4272 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
] < sizeof(*lft0
)) {
4273 ipseclog((LOG_DEBUG
, "key_setsaval: invalid hard lifetime ext len.\n"));
4277 sav
->lft_h
= (struct sadb_lifetime
*)key_newbuf(lft0
,
4279 if (sav
->lft_h
== NULL
) {
4280 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4284 /* to be initialize ? */
4287 lft0
= (struct sadb_lifetime
*)
4288 (void *)mhp
->ext
[SADB_EXT_LIFETIME_SOFT
];
4290 if (mhp
->extlen
[SADB_EXT_LIFETIME_SOFT
] < sizeof(*lft0
)) {
4291 ipseclog((LOG_DEBUG
, "key_setsaval: invalid soft lifetime ext len.\n"));
4295 sav
->lft_s
= (struct sadb_lifetime
*)key_newbuf(lft0
,
4297 if (sav
->lft_s
== NULL
) {
4298 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4302 /* to be initialize ? */
4309 /* initialization */
4310 if (sav
->replay
!= NULL
) {
4311 keydb_delsecreplay(sav
->replay
);
4314 if (sav
->key_auth
!= NULL
) {
4315 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
4316 KFREE(sav
->key_auth
);
4317 sav
->key_auth
= NULL
;
4319 if (sav
->key_enc
!= NULL
) {
4320 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
4321 KFREE(sav
->key_enc
);
4322 sav
->key_enc
= NULL
;
4325 bzero(sav
->sched
, sav
->schedlen
);
4329 if (sav
->iv
!= NULL
) {
4333 if (sav
->lft_c
!= NULL
) {
4337 if (sav
->lft_h
!= NULL
) {
4341 if (sav
->lft_s
!= NULL
) {
4350 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
4351 * You must update these if need.
4355 * does not modify mbuf. does not free mbuf on error.
4358 key_setsaval2(struct secasvar
*sav
,
4364 struct sadb_key
*key_auth
,
4365 u_int16_t key_auth_len
,
4366 struct sadb_key
*key_enc
,
4367 u_int16_t key_enc_len
,
4368 u_int16_t natt_port
,
4372 struct sadb_lifetime
*lifetime_hard
,
4373 struct sadb_lifetime
*lifetime_soft
)
4376 const struct esp_algorithm
*algo
;
4381 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4383 /* initialization */
4385 sav
->key_auth
= NULL
;
4386 sav
->key_enc
= NULL
;
4393 sav
->remote_ike_port
= 0;
4394 sav
->natt_last_activity
= natt_now
;
4395 sav
->natt_encapsulated_src_port
= 0;
4397 sav
->alg_auth
= alg_auth
;
4398 sav
->alg_enc
= alg_enc
;
4402 key_setspi(sav
, htonl(spi
));
4405 * Verify that a nat-traversal port was specified if
4406 * the nat-traversal flag is set.
4408 if ((sav
->flags
& SADB_X_EXT_NATT
) != 0) {
4409 if (natt_port
== 0) {
4410 ipseclog((LOG_DEBUG
, "key_setsaval2: natt port not set.\n"));
4414 sav
->remote_ike_port
= natt_port
;
4418 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
4419 * SADB_X_EXT_NATT is set and SADB_X_EXT_NATT_KEEPALIVE is not
4420 * set (we're not behind nat) - otherwise clear it.
4422 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
4423 if ((sav
->flags
& SADB_X_EXT_NATT
) == 0 ||
4424 (sav
->flags
& SADB_X_EXT_NATT_KEEPALIVE
) != 0)
4425 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
4428 if ((flags
& SADB_X_EXT_OLD
) == 0) {
4429 sav
->replay
= keydb_newsecreplay(replay
);
4430 if (sav
->replay
== NULL
) {
4431 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4437 /* Authentication keys */
4438 sav
->key_auth
= (__typeof__(sav
->key_auth
))key_newbuf(key_auth
, key_auth_len
);
4439 if (sav
->key_auth
== NULL
) {
4440 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4445 /* Encryption key */
4446 sav
->key_enc
= (__typeof__(sav
->key_enc
))key_newbuf(key_enc
, key_enc_len
);
4447 if (sav
->key_enc
== NULL
) {
4448 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4456 if (satype
== SADB_SATYPE_ESP
) {
4458 algo
= esp_algorithm_lookup(sav
->alg_enc
);
4459 if (algo
&& algo
->ivlen
)
4460 sav
->ivlen
= (*algo
->ivlen
)(algo
, sav
);
4461 if (sav
->ivlen
!= 0) {
4462 KMALLOC_NOWAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4464 lck_mtx_unlock(sadb_mutex
);
4465 KMALLOC_WAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4466 lck_mtx_lock(sadb_mutex
);
4468 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4474 key_randomfill(sav
->iv
, sav
->ivlen
);
4481 sav
->created
= tv
.tv_sec
;
4483 /* make lifetime for CURRENT */
4484 KMALLOC_NOWAIT(sav
->lft_c
, struct sadb_lifetime
*,
4485 sizeof(struct sadb_lifetime
));
4486 if (sav
->lft_c
== NULL
) {
4487 lck_mtx_unlock(sadb_mutex
);
4488 KMALLOC_WAIT(sav
->lft_c
, struct sadb_lifetime
*,
4489 sizeof(struct sadb_lifetime
));
4490 lck_mtx_lock(sadb_mutex
);
4491 if (sav
->lft_c
== NULL
) {
4492 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4500 sav
->lft_c
->sadb_lifetime_len
=
4501 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
4502 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
4503 sav
->lft_c
->sadb_lifetime_allocations
= 0;
4504 sav
->lft_c
->sadb_lifetime_bytes
= 0;
4505 sav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
4506 sav
->lft_c
->sadb_lifetime_usetime
= 0;
4508 /* lifetimes for HARD and SOFT */
4509 sav
->lft_h
= (__typeof__(sav
->lft_h
))key_newbuf(lifetime_hard
,
4510 sizeof(*lifetime_hard
));
4511 if (sav
->lft_h
== NULL
) {
4512 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4516 sav
->lft_s
= (__typeof__(sav
->lft_s
))key_newbuf(lifetime_soft
,
4517 sizeof(*lifetime_soft
));
4518 if (sav
->lft_s
== NULL
) {
4519 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4527 /* initialization */
4528 if (sav
->replay
!= NULL
) {
4529 keydb_delsecreplay(sav
->replay
);
4532 if (sav
->key_auth
!= NULL
) {
4533 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
4534 KFREE(sav
->key_auth
);
4535 sav
->key_auth
= NULL
;
4537 if (sav
->key_enc
!= NULL
) {
4538 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
4539 KFREE(sav
->key_enc
);
4540 sav
->key_enc
= NULL
;
4543 bzero(sav
->sched
, sav
->schedlen
);
4547 if (sav
->iv
!= NULL
) {
4551 if (sav
->lft_c
!= NULL
) {
4555 if (sav
->lft_h
!= NULL
) {
4559 if (sav
->lft_s
!= NULL
) {
4568 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
4574 struct secasvar
*sav
)
4577 int checkmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
4578 int mustmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
4582 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4584 /* check SPI value */
4585 switch (sav
->sah
->saidx
.proto
) {
4589 /* No reason to test if this is >= 0, because ntohl(sav->spi) is unsigned. */
4590 if (ntohl(sav
->spi
) <= 255) {
4591 ipseclog((LOG_DEBUG
,
4592 "key_mature: illegal range of SPI %u.\n",
4593 (u_int32_t
)ntohl(sav
->spi
)));
4600 switch (sav
->sah
->saidx
.proto
) {
4603 if ((sav
->flags
& SADB_X_EXT_OLD
)
4604 && (sav
->flags
& SADB_X_EXT_DERIV
)) {
4605 ipseclog((LOG_DEBUG
, "key_mature: "
4606 "invalid flag (derived) given to old-esp.\n"));
4609 if (sav
->alg_auth
== SADB_AALG_NONE
)
4617 if (sav
->flags
& SADB_X_EXT_DERIV
) {
4618 ipseclog((LOG_DEBUG
, "key_mature: "
4619 "invalid flag (derived) given to AH SA.\n"));
4622 if (sav
->alg_enc
!= SADB_EALG_NONE
) {
4623 ipseclog((LOG_DEBUG
, "key_mature: "
4624 "protocol and algorithm mismated.\n"));
4630 case IPPROTO_IPCOMP
:
4631 if (sav
->alg_auth
!= SADB_AALG_NONE
) {
4632 ipseclog((LOG_DEBUG
, "key_mature: "
4633 "protocol and algorithm mismated.\n"));
4636 if ((sav
->flags
& SADB_X_EXT_RAWCPI
) == 0
4637 && ntohl(sav
->spi
) >= 0x10000) {
4638 ipseclog((LOG_DEBUG
, "key_mature: invalid cpi for IPComp.\n"));
4645 ipseclog((LOG_DEBUG
, "key_mature: Invalid satype.\n"));
4646 return EPROTONOSUPPORT
;
4649 /* check authentication algorithm */
4650 if ((checkmask
& 2) != 0) {
4651 const struct ah_algorithm
*algo
;
4654 algo
= ah_algorithm_lookup(sav
->alg_auth
);
4656 ipseclog((LOG_DEBUG
,"key_mature: "
4657 "unknown authentication algorithm.\n"));
4661 /* algorithm-dependent check */
4663 keylen
= sav
->key_auth
->sadb_key_bits
;
4666 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
4667 ipseclog((LOG_DEBUG
,
4668 "key_mature: invalid AH key length %d "
4669 "(%d-%d allowed)\n",
4670 keylen
, algo
->keymin
, algo
->keymax
));
4675 if ((*algo
->mature
)(sav
)) {
4676 /* message generated in per-algorithm function*/
4679 mature
= SADB_SATYPE_AH
;
4682 if ((mustmask
& 2) != 0 && mature
!= SADB_SATYPE_AH
) {
4683 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for AH\n"));
4688 /* check encryption algorithm */
4689 if ((checkmask
& 1) != 0) {
4691 const struct esp_algorithm
*algo
;
4694 algo
= esp_algorithm_lookup(sav
->alg_enc
);
4696 ipseclog((LOG_DEBUG
, "key_mature: unknown encryption algorithm.\n"));
4700 /* algorithm-dependent check */
4702 keylen
= sav
->key_enc
->sadb_key_bits
;
4705 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
4706 ipseclog((LOG_DEBUG
,
4707 "key_mature: invalid ESP key length %d "
4708 "(%d-%d allowed)\n",
4709 keylen
, algo
->keymin
, algo
->keymax
));
4714 if ((*algo
->mature
)(sav
)) {
4715 /* message generated in per-algorithm function*/
4718 mature
= SADB_SATYPE_ESP
;
4721 if ((mustmask
& 1) != 0 && mature
!= SADB_SATYPE_ESP
) {
4722 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for ESP\n"));
4726 ipseclog((LOG_DEBUG
, "key_mature: ESP not supported in this configuration\n"));
4731 /* check compression algorithm */
4732 if ((checkmask
& 4) != 0) {
4733 const struct ipcomp_algorithm
*algo
;
4735 /* algorithm-dependent check */
4736 algo
= ipcomp_algorithm_lookup(sav
->alg_enc
);
4738 ipseclog((LOG_DEBUG
, "key_mature: unknown compression algorithm.\n"));
4743 key_sa_chgstate(sav
, SADB_SASTATE_MATURE
);
4749 * subroutine for SADB_GET and SADB_DUMP.
4751 static struct mbuf
*
4753 struct secasvar
*sav
,
4759 struct mbuf
*result
= NULL
, *tres
= NULL
, *m
;
4764 SADB_EXT_SA
, SADB_X_EXT_SA2
,
4765 SADB_EXT_LIFETIME_HARD
, SADB_EXT_LIFETIME_SOFT
,
4766 SADB_EXT_LIFETIME_CURRENT
, SADB_EXT_ADDRESS_SRC
,
4767 SADB_EXT_ADDRESS_DST
, SADB_EXT_ADDRESS_PROXY
, SADB_EXT_KEY_AUTH
,
4768 SADB_EXT_KEY_ENCRYPT
, SADB_EXT_IDENTITY_SRC
,
4769 SADB_EXT_IDENTITY_DST
, SADB_EXT_SENSITIVITY
,
4772 m
= key_setsadbmsg(type
, 0, satype
, seq
, pid
, sav
->refcnt
);
4777 for (i
= sizeof(dumporder
)/sizeof(dumporder
[0]) - 1; i
>= 0; i
--) {
4780 switch (dumporder
[i
]) {
4782 m
= key_setsadbsa(sav
);
4787 case SADB_X_EXT_SA2
:
4788 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
4789 sav
->replay
? sav
->replay
->count
: 0,
4790 sav
->sah
->saidx
.reqid
,
4796 case SADB_EXT_ADDRESS_SRC
:
4797 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
4798 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
4799 FULLMASK
, IPSEC_ULPROTO_ANY
);
4804 case SADB_EXT_ADDRESS_DST
:
4805 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
4806 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
4807 FULLMASK
, IPSEC_ULPROTO_ANY
);
4812 case SADB_EXT_KEY_AUTH
:
4815 l
= PFKEY_UNUNIT64(sav
->key_auth
->sadb_key_len
);
4819 case SADB_EXT_KEY_ENCRYPT
:
4822 l
= PFKEY_UNUNIT64(sav
->key_enc
->sadb_key_len
);
4826 case SADB_EXT_LIFETIME_CURRENT
:
4829 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_c
)->sadb_ext_len
);
4833 case SADB_EXT_LIFETIME_HARD
:
4836 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_h
)->sadb_ext_len
);
4840 case SADB_EXT_LIFETIME_SOFT
:
4843 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_s
)->sadb_ext_len
);
4847 case SADB_EXT_ADDRESS_PROXY
:
4848 case SADB_EXT_IDENTITY_SRC
:
4849 case SADB_EXT_IDENTITY_DST
:
4850 /* XXX: should we brought from SPD ? */
4851 case SADB_EXT_SENSITIVITY
:
4856 if ((!m
&& !p
) || (m
&& p
))
4859 M_PREPEND(tres
, l
, M_WAITOK
);
4862 bcopy(p
, mtod(tres
, caddr_t
), l
);
4866 m
= key_alloc_mbuf(l
);
4869 m_copyback(m
, 0, l
, p
);
4877 m_cat(result
, tres
);
4879 if (sav
->sah
&& (sav
->sah
->outgoing_if
|| sav
->sah
->ipsec_if
)) {
4880 m
= key_setsadbipsecif(NULL
, ifindex2ifnet
[sav
->sah
->outgoing_if
], sav
->sah
->ipsec_if
, 0);
4886 if (result
->m_len
< sizeof(struct sadb_msg
)) {
4887 result
= m_pullup(result
, sizeof(struct sadb_msg
));
4892 result
->m_pkthdr
.len
= 0;
4893 for (m
= result
; m
; m
= m
->m_next
)
4894 result
->m_pkthdr
.len
+= m
->m_len
;
4896 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
4897 PFKEY_UNIT64(result
->m_pkthdr
.len
);
4908 * set data into sadb_msg.
4910 static struct mbuf
*
4923 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
4926 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
4927 if (m
&& len
> MHLEN
) {
4928 MCLGET(m
, M_DONTWAIT
);
4929 if ((m
->m_flags
& M_EXT
) == 0) {
4936 m
->m_pkthdr
.len
= m
->m_len
= len
;
4939 p
= mtod(m
, struct sadb_msg
*);
4942 p
->sadb_msg_version
= PF_KEY_V2
;
4943 p
->sadb_msg_type
= type
;
4944 p
->sadb_msg_errno
= 0;
4945 p
->sadb_msg_satype
= satype
;
4946 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
4947 p
->sadb_msg_reserved
= reserved
;
4948 p
->sadb_msg_seq
= seq
;
4949 p
->sadb_msg_pid
= (u_int32_t
)pid
;
4955 * copy secasvar data into sadb_address.
4957 static struct mbuf
*
4959 struct secasvar
*sav
)
4965 len
= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
4966 m
= key_alloc_mbuf(len
);
4967 if (!m
|| m
->m_next
) { /*XXX*/
4973 p
= mtod(m
, struct sadb_sa
*);
4976 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
4977 p
->sadb_sa_exttype
= SADB_EXT_SA
;
4978 p
->sadb_sa_spi
= sav
->spi
;
4979 p
->sadb_sa_replay
= (sav
->replay
!= NULL
? sav
->replay
->wsize
: 0);
4980 p
->sadb_sa_state
= sav
->state
;
4981 p
->sadb_sa_auth
= sav
->alg_auth
;
4982 p
->sadb_sa_encrypt
= sav
->alg_enc
;
4983 p
->sadb_sa_flags
= sav
->flags
;
4989 * set data into sadb_address.
4991 static struct mbuf
*
4994 struct sockaddr
*saddr
,
4999 struct sadb_address
*p
;
5002 len
= PFKEY_ALIGN8(sizeof(struct sadb_address
)) +
5003 PFKEY_ALIGN8(saddr
->sa_len
);
5004 m
= key_alloc_mbuf(len
);
5005 if (!m
|| m
->m_next
) { /*XXX*/
5011 p
= mtod(m
, struct sadb_address
*);
5014 p
->sadb_address_len
= PFKEY_UNIT64(len
);
5015 p
->sadb_address_exttype
= exttype
;
5016 p
->sadb_address_proto
= ul_proto
;
5017 if (prefixlen
== FULLMASK
) {
5018 switch (saddr
->sa_family
) {
5020 prefixlen
= sizeof(struct in_addr
) << 3;
5023 prefixlen
= sizeof(struct in6_addr
) << 3;
5029 p
->sadb_address_prefixlen
= prefixlen
;
5030 p
->sadb_address_reserved
= 0;
5033 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_address
)),
5039 static struct mbuf
*
5040 key_setsadbipsecif(ifnet_t internal_if
,
5041 ifnet_t outgoing_if
,
5046 struct sadb_x_ipsecif
*p
;
5049 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_ipsecif
));
5050 m
= key_alloc_mbuf(len
);
5051 if (!m
|| m
->m_next
) { /*XXX*/
5057 p
= mtod(m
, struct sadb_x_ipsecif
*);
5060 p
->sadb_x_ipsecif_len
= PFKEY_UNIT64(len
);
5061 p
->sadb_x_ipsecif_exttype
= SADB_X_EXT_IPSECIF
;
5063 if (internal_if
&& internal_if
->if_xname
)
5064 strlcpy(p
->sadb_x_ipsecif_internal_if
, internal_if
->if_xname
, IFXNAMSIZ
);
5065 if (outgoing_if
&& outgoing_if
->if_xname
)
5066 strlcpy(p
->sadb_x_ipsecif_outgoing_if
, outgoing_if
->if_xname
, IFXNAMSIZ
);
5067 if (ipsec_if
&& ipsec_if
->if_xname
)
5068 strlcpy(p
->sadb_x_ipsecif_ipsec_if
, ipsec_if
->if_xname
, IFXNAMSIZ
);
5070 p
->sadb_x_ipsecif_init_disabled
= init_disabled
;
5076 * set data into sadb_session_id
5078 static struct mbuf
*
5079 key_setsadbsession_id (u_int64_t session_ids
[])
5082 struct sadb_session_id
*p
;
5085 len
= PFKEY_ALIGN8(sizeof(*p
));
5086 m
= key_alloc_mbuf(len
);
5087 if (!m
|| m
->m_next
) { /*XXX*/
5093 p
= mtod(m
, __typeof__(p
));
5096 p
->sadb_session_id_len
= PFKEY_UNIT64(len
);
5097 p
->sadb_session_id_exttype
= SADB_EXT_SESSION_ID
;
5098 p
->sadb_session_id_v
[0] = session_ids
[0];
5099 p
->sadb_session_id_v
[1] = session_ids
[1];
5105 * copy stats data into sadb_sastat type.
5107 static struct mbuf
*
5108 key_setsadbsastat (u_int32_t dir
,
5109 struct sastat
*stats
,
5110 u_int32_t max_stats
)
5113 struct sadb_sastat
*p
;
5120 list_len
= sizeof(*stats
) * max_stats
;
5121 len
= PFKEY_ALIGN8(sizeof(*p
)) + PFKEY_ALIGN8(list_len
);
5122 m
= key_alloc_mbuf(len
);
5123 if (!m
|| m
->m_next
) { /*XXX*/
5129 p
= mtod(m
, __typeof__(p
));
5132 p
->sadb_sastat_len
= PFKEY_UNIT64(len
);
5133 p
->sadb_sastat_exttype
= SADB_EXT_SASTAT
;
5134 p
->sadb_sastat_dir
= dir
;
5135 p
->sadb_sastat_list_len
= max_stats
;
5138 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(*p
)),
5147 * set data into sadb_ident.
5149 static struct mbuf
*
5158 struct sadb_ident
*p
;
5161 len
= PFKEY_ALIGN8(sizeof(struct sadb_ident
)) + PFKEY_ALIGN8(stringlen
);
5162 m
= key_alloc_mbuf(len
);
5163 if (!m
|| m
->m_next
) { /*XXX*/
5169 p
= mtod(m
, struct sadb_ident
*);
5172 p
->sadb_ident_len
= PFKEY_UNIT64(len
);
5173 p
->sadb_ident_exttype
= exttype
;
5174 p
->sadb_ident_type
= idtype
;
5175 p
->sadb_ident_reserved
= 0;
5176 p
->sadb_ident_id
= id
;
5179 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_ident
)),
5187 * set data into sadb_x_sa2.
5189 static struct mbuf
*
5197 struct sadb_x_sa2
*p
;
5200 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_sa2
));
5201 m
= key_alloc_mbuf(len
);
5202 if (!m
|| m
->m_next
) { /*XXX*/
5208 p
= mtod(m
, struct sadb_x_sa2
*);
5211 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
5212 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
5213 p
->sadb_x_sa2_mode
= mode
;
5214 p
->sadb_x_sa2_reserved1
= 0;
5215 p
->sadb_x_sa2_reserved2
= 0;
5216 p
->sadb_x_sa2_sequence
= seq
;
5217 p
->sadb_x_sa2_reqid
= reqid
;
5218 p
->sadb_x_sa2_flags
= flags
;
5224 * set data into sadb_x_policy
5226 static struct mbuf
*
5233 struct sadb_x_policy
*p
;
5236 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_policy
));
5237 m
= key_alloc_mbuf(len
);
5238 if (!m
|| m
->m_next
) { /*XXX*/
5244 p
= mtod(m
, struct sadb_x_policy
*);
5247 p
->sadb_x_policy_len
= PFKEY_UNIT64(len
);
5248 p
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
5249 p
->sadb_x_policy_type
= type
;
5250 p
->sadb_x_policy_dir
= dir
;
5251 p
->sadb_x_policy_id
= id
;
5258 * copy a buffer into the new buffer allocated.
5267 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5268 KMALLOC_NOWAIT(new, caddr_t
, len
);
5270 lck_mtx_unlock(sadb_mutex
);
5271 KMALLOC_WAIT(new, caddr_t
, len
);
5272 lck_mtx_lock(sadb_mutex
);
5274 ipseclog((LOG_DEBUG
, "key_newbuf: No more memory.\n"));
5278 bcopy(src
, new, len
);
5283 /* compare my own address
5284 * OUT: 1: true, i.e. my address.
5289 struct sockaddr
*sa
)
5292 struct sockaddr_in
*sin
;
5293 struct in_ifaddr
*ia
;
5298 panic("key_ismyaddr: NULL pointer is passed.\n");
5300 switch (sa
->sa_family
) {
5303 lck_rw_lock_shared(in_ifaddr_rwlock
);
5304 sin
= (struct sockaddr_in
*)(void *)sa
;
5305 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
5306 ia
= ia
->ia_link
.tqe_next
) {
5307 IFA_LOCK_SPIN(&ia
->ia_ifa
);
5308 if (sin
->sin_family
== ia
->ia_addr
.sin_family
&&
5309 sin
->sin_len
== ia
->ia_addr
.sin_len
&&
5310 sin
->sin_addr
.s_addr
== ia
->ia_addr
.sin_addr
.s_addr
)
5312 IFA_UNLOCK(&ia
->ia_ifa
);
5313 lck_rw_done(in_ifaddr_rwlock
);
5316 IFA_UNLOCK(&ia
->ia_ifa
);
5318 lck_rw_done(in_ifaddr_rwlock
);
5323 return key_ismyaddr6((struct sockaddr_in6
*)(void *)sa
);
5332 * compare my own address for IPv6.
5335 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
5337 #include <netinet6/in6_var.h>
5341 struct sockaddr_in6
*sin6
)
5343 struct in6_ifaddr
*ia
;
5344 struct in6_multi
*in6m
;
5346 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
5347 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
5348 IFA_LOCK(&ia
->ia_ifa
);
5349 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
5350 (struct sockaddr
*)&ia
->ia_addr
, 0) == 0) {
5351 IFA_UNLOCK(&ia
->ia_ifa
);
5352 lck_rw_done(&in6_ifaddr_rwlock
);
5355 IFA_UNLOCK(&ia
->ia_ifa
);
5359 * XXX why do we care about multlicast here while we don't care
5360 * about IPv4 multicast??
5364 in6_multihead_lock_shared();
5365 IN6_LOOKUP_MULTI(&sin6
->sin6_addr
, ia
->ia_ifp
, in6m
);
5366 in6_multihead_lock_done();
5368 lck_rw_done(&in6_ifaddr_rwlock
);
5373 lck_rw_done(&in6_ifaddr_rwlock
);
5375 /* loopback, just for safety */
5376 if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
5384 * compare two secasindex structure.
5385 * flag can specify to compare 2 saidxes.
5386 * compare two secasindex structure without both mode and reqid.
5387 * don't compare port.
5389 * saidx0: source, it can be in SAD.
5397 struct secasindex
*saidx0
,
5398 struct secasindex
*saidx1
,
5402 if (saidx0
== NULL
&& saidx1
== NULL
)
5405 if (saidx0
== NULL
|| saidx1
== NULL
)
5408 if (saidx0
->ipsec_ifindex
!= 0 && saidx0
->ipsec_ifindex
!= saidx1
->ipsec_ifindex
)
5411 if (saidx0
->proto
!= saidx1
->proto
)
5414 if (flag
== CMP_EXACTLY
) {
5415 if (saidx0
->mode
!= saidx1
->mode
)
5417 if (saidx0
->reqid
!= saidx1
->reqid
)
5419 if (bcmp(&saidx0
->src
, &saidx1
->src
, saidx0
->src
.ss_len
) != 0 ||
5420 bcmp(&saidx0
->dst
, &saidx1
->dst
, saidx0
->dst
.ss_len
) != 0)
5424 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
5425 if (flag
& CMP_REQID
) {
5427 * If reqid of SPD is non-zero, unique SA is required.
5428 * The result must be of same reqid in this case.
5430 if (saidx1
->reqid
!= 0 && saidx0
->reqid
!= saidx1
->reqid
)
5434 if (flag
& CMP_MODE
) {
5435 if (saidx0
->mode
!= IPSEC_MODE_ANY
5436 && saidx0
->mode
!= saidx1
->mode
)
5440 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->src
,
5441 (struct sockaddr
*)&saidx1
->src
, flag
& CMP_PORT
? 1 : 0) != 0) {
5444 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->dst
,
5445 (struct sockaddr
*)&saidx1
->dst
, flag
& CMP_PORT
? 1 : 0) != 0) {
5454 * compare two secindex structure exactly.
5456 * spidx0: source, it is often in SPD.
5457 * spidx1: object, it is often from PFKEY message.
5463 key_cmpspidx_exactly(
5464 struct secpolicyindex
*spidx0
,
5465 struct secpolicyindex
*spidx1
)
5468 if (spidx0
== NULL
&& spidx1
== NULL
)
5471 if (spidx0
== NULL
|| spidx1
== NULL
)
5474 if (spidx0
->prefs
!= spidx1
->prefs
5475 || spidx0
->prefd
!= spidx1
->prefd
5476 || spidx0
->ul_proto
!= spidx1
->ul_proto
5477 || spidx0
->internal_if
!= spidx1
->internal_if
)
5480 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src
,
5481 (struct sockaddr
*)&spidx1
->src
, 1) != 0) {
5484 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst
,
5485 (struct sockaddr
*)&spidx1
->dst
, 1) != 0) {
5489 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src_range
.start
,
5490 (struct sockaddr
*)&spidx1
->src_range
.start
, 1) != 0) {
5493 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src_range
.end
,
5494 (struct sockaddr
*)&spidx1
->src_range
.end
, 1) != 0) {
5497 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst_range
.start
,
5498 (struct sockaddr
*)&spidx1
->dst_range
.start
, 1) != 0) {
5501 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst_range
.end
,
5502 (struct sockaddr
*)&spidx1
->dst_range
.end
, 1) != 0) {
5510 * compare two secindex structure with mask.
5512 * spidx0: source, it is often in SPD.
5513 * spidx1: object, it is often from IP header.
5519 key_cmpspidx_withmask(
5520 struct secpolicyindex
*spidx0
,
5521 struct secpolicyindex
*spidx1
)
5523 int spidx0_src_is_range
= 0;
5524 int spidx0_dst_is_range
= 0;
5527 if (spidx0
== NULL
&& spidx1
== NULL
)
5530 if (spidx0
== NULL
|| spidx1
== NULL
)
5533 if (spidx0
->src_range
.start
.ss_len
> 0)
5534 spidx0_src_is_range
= 1;
5536 if (spidx0
->dst_range
.start
.ss_len
> 0)
5537 spidx0_dst_is_range
= 1;
5539 if ((spidx0_src_is_range
? spidx0
->src_range
.start
.ss_family
: spidx0
->src
.ss_family
) != spidx1
->src
.ss_family
||
5540 (spidx0_dst_is_range
? spidx0
->dst_range
.start
.ss_family
: spidx0
->dst
.ss_family
) != spidx1
->dst
.ss_family
||
5541 (spidx0_src_is_range
? spidx0
->src_range
.start
.ss_len
: spidx0
->src
.ss_len
) != spidx1
->src
.ss_len
||
5542 (spidx0_dst_is_range
? spidx0
->dst_range
.start
.ss_len
: spidx0
->dst
.ss_len
) != spidx1
->dst
.ss_len
)
5545 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
5546 if (spidx0
->ul_proto
!= (u_int16_t
)IPSEC_ULPROTO_ANY
5547 && spidx0
->ul_proto
!= spidx1
->ul_proto
)
5550 /* If spidx1 specifies interface, ignore src addr */
5551 if (spidx1
->internal_if
!= NULL
) {
5552 if (spidx0
->internal_if
== NULL
5553 || spidx0
->internal_if
!= spidx1
->internal_if
)
5556 /* Still check ports */
5557 switch (spidx0
->src
.ss_family
) {
5559 if (spidx0_src_is_range
&&
5560 (satosin(&spidx1
->src
)->sin_port
< satosin(&spidx0
->src_range
.start
)->sin_port
5561 || satosin(&spidx1
->src
)->sin_port
> satosin(&spidx0
->src_range
.end
)->sin_port
))
5563 else if (satosin(&spidx0
->src
)->sin_port
!= IPSEC_PORT_ANY
5564 && satosin(&spidx0
->src
)->sin_port
!=
5565 satosin(&spidx1
->src
)->sin_port
)
5569 if (spidx0_src_is_range
&&
5570 (satosin6(&spidx1
->src
)->sin6_port
< satosin6(&spidx0
->src_range
.start
)->sin6_port
5571 || satosin6(&spidx1
->src
)->sin6_port
> satosin6(&spidx0
->src_range
.end
)->sin6_port
))
5573 else if (satosin6(&spidx0
->src
)->sin6_port
!= IPSEC_PORT_ANY
5574 && satosin6(&spidx0
->src
)->sin6_port
!=
5575 satosin6(&spidx1
->src
)->sin6_port
)
5581 } else if (spidx0_src_is_range
) {
5582 if (!key_is_addr_in_range(&spidx1
->src
, &spidx0
->src_range
))
5585 switch (spidx0
->src
.ss_family
) {
5587 if (satosin(&spidx0
->src
)->sin_port
!= IPSEC_PORT_ANY
5588 && satosin(&spidx0
->src
)->sin_port
!=
5589 satosin(&spidx1
->src
)->sin_port
)
5591 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->src
)->sin_addr
,
5592 (caddr_t
)&satosin(&spidx1
->src
)->sin_addr
, spidx0
->prefs
))
5596 if (satosin6(&spidx0
->src
)->sin6_port
!= IPSEC_PORT_ANY
5597 && satosin6(&spidx0
->src
)->sin6_port
!=
5598 satosin6(&spidx1
->src
)->sin6_port
)
5601 * scope_id check. if sin6_scope_id is 0, we regard it
5602 * as a wildcard scope, which matches any scope zone ID.
5604 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
5605 satosin6(&spidx1
->src
)->sin6_scope_id
&&
5606 satosin6(&spidx0
->src
)->sin6_scope_id
!=
5607 satosin6(&spidx1
->src
)->sin6_scope_id
)
5609 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->src
)->sin6_addr
,
5610 (caddr_t
)&satosin6(&spidx1
->src
)->sin6_addr
, spidx0
->prefs
))
5615 if (bcmp(&spidx0
->src
, &spidx1
->src
, spidx0
->src
.ss_len
) != 0)
5621 if (spidx0_dst_is_range
) {
5622 if (!key_is_addr_in_range(&spidx1
->dst
, &spidx0
->dst_range
))
5625 switch (spidx0
->dst
.ss_family
) {
5627 if (satosin(&spidx0
->dst
)->sin_port
!= IPSEC_PORT_ANY
5628 && satosin(&spidx0
->dst
)->sin_port
!=
5629 satosin(&spidx1
->dst
)->sin_port
)
5631 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->dst
)->sin_addr
,
5632 (caddr_t
)&satosin(&spidx1
->dst
)->sin_addr
, spidx0
->prefd
))
5636 if (satosin6(&spidx0
->dst
)->sin6_port
!= IPSEC_PORT_ANY
5637 && satosin6(&spidx0
->dst
)->sin6_port
!=
5638 satosin6(&spidx1
->dst
)->sin6_port
)
5641 * scope_id check. if sin6_scope_id is 0, we regard it
5642 * as a wildcard scope, which matches any scope zone ID.
5644 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
5645 satosin6(&spidx1
->src
)->sin6_scope_id
&&
5646 satosin6(&spidx0
->dst
)->sin6_scope_id
!=
5647 satosin6(&spidx1
->dst
)->sin6_scope_id
)
5649 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->dst
)->sin6_addr
,
5650 (caddr_t
)&satosin6(&spidx1
->dst
)->sin6_addr
, spidx0
->prefd
))
5655 if (bcmp(&spidx0
->dst
, &spidx1
->dst
, spidx0
->dst
.ss_len
) != 0)
5661 /* XXX Do we check other field ? e.g. flowinfo */
5667 key_is_addr_in_range(struct sockaddr_storage
*addr
, struct secpolicyaddrrange
*addr_range
)
5671 if (addr
== NULL
|| addr_range
== NULL
)
5674 /* Must be greater than or equal to start */
5675 cmp
= key_sockaddrcmp((struct sockaddr
*)addr
, (struct sockaddr
*)&addr_range
->start
, 1);
5676 if (cmp
!= 0 && cmp
!= 1)
5679 /* Must be less than or equal to end */
5680 cmp
= key_sockaddrcmp((struct sockaddr
*)addr
, (struct sockaddr
*)&addr_range
->end
, 1);
5681 if (cmp
!= 0 && cmp
!= -1)
5692 2: Not comparable or error
5696 struct sockaddr
*sa1
,
5697 struct sockaddr
*sa2
,
5701 int port_result
= 0;
5703 if (sa1
->sa_family
!= sa2
->sa_family
|| sa1
->sa_len
!= sa2
->sa_len
)
5706 if (sa1
->sa_len
== 0)
5709 switch (sa1
->sa_family
) {
5711 if (sa1
->sa_len
!= sizeof(struct sockaddr_in
))
5714 result
= memcmp(&satosin(sa1
)->sin_addr
.s_addr
, &satosin(sa2
)->sin_addr
.s_addr
, sizeof(satosin(sa1
)->sin_addr
.s_addr
));
5717 if (satosin(sa1
)->sin_port
< satosin(sa2
)->sin_port
) {
5719 } else if (satosin(sa1
)->sin_port
> satosin(sa2
)->sin_port
) {
5724 result
= port_result
;
5725 else if ((result
> 0 && port_result
< 0) || (result
< 0 && port_result
> 0))
5731 if (sa1
->sa_len
!= sizeof(struct sockaddr_in6
))
5732 return 2; /*EINVAL*/
5734 if (satosin6(sa1
)->sin6_scope_id
!=
5735 satosin6(sa2
)->sin6_scope_id
) {
5739 result
= memcmp(&satosin6(sa1
)->sin6_addr
.s6_addr
[0], &satosin6(sa2
)->sin6_addr
.s6_addr
[0], sizeof(struct in6_addr
));
5742 if (satosin6(sa1
)->sin6_port
< satosin6(sa2
)->sin6_port
) {
5744 } else if (satosin6(sa1
)->sin6_port
> satosin6(sa2
)->sin6_port
) {
5749 result
= port_result
;
5750 else if ((result
> 0 && port_result
< 0) || (result
< 0 && port_result
> 0))
5756 result
= memcmp(sa1
, sa2
, sa1
->sa_len
);
5760 if (result
< 0) result
= -1;
5761 else if (result
> 0) result
= 1;
5767 * compare two buffers with mask.
5771 * bits: Number of bits to compare
5784 /* XXX: This could be considerably faster if we compare a word
5785 * at a time, but it is complicated on LSB Endian machines */
5787 /* Handle null pointers */
5788 if (p1
== NULL
|| p2
== NULL
)
5798 mask
= ~((1<<(8-bits
))-1);
5799 if ((*p1
& mask
) != (*p2
& mask
))
5802 return 1; /* Match! */
5807 * scanning SPD and SAD to check status for each entries,
5808 * and do to remove or to expire.
5809 * XXX: year 2038 problem may remain.
5811 int key_timehandler_debug
= 0;
5812 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;
5813 u_int64_t total_sav_count
= 0;
5815 key_timehandler(void)
5819 struct secpolicy
**spbuf
= NULL
, **spptr
= NULL
;
5820 struct secasvar
**savexbuf
= NULL
, **savexptr
= NULL
;
5821 struct secasvar
**savkabuf
= NULL
, **savkaptr
= NULL
;
5822 int spbufcount
= 0, savbufcount
= 0, spcount
= 0, savexcount
= 0, savkacount
= 0, cnt
;
5823 int stop_handler
= 1; /* stop the timehandler */
5827 /* pre-allocate buffers before taking the lock */
5828 /* if allocation failures occur - portions of the processing will be skipped */
5829 if ((spbufcount
= ipsec_policy_count
) != 0) {
5831 KMALLOC_WAIT(spbuf
, struct secpolicy
**, spbufcount
* sizeof(struct secpolicy
*));
5835 if ((savbufcount
= ipsec_sav_count
) != 0) {
5837 KMALLOC_WAIT(savexbuf
, struct secasvar
**, savbufcount
* sizeof(struct secasvar
*));
5839 savexptr
= savexbuf
;
5840 KMALLOC_WAIT(savkabuf
, struct secasvar
**, savbufcount
* sizeof(struct secasvar
*));
5842 savkaptr
= savkabuf
;
5844 lck_mtx_lock(sadb_mutex
);
5848 struct secpolicy
*sp
, *nextsp
;
5850 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
5851 for (sp
= LIST_FIRST(&sptree
[dir
]);
5855 /* don't prevent timehandler from stopping for generate policy */
5856 if (sp
->policy
!= IPSEC_POLICY_GENERATE
)
5859 nextsp
= LIST_NEXT(sp
, chain
);
5861 if (sp
->state
== IPSEC_SPSTATE_DEAD
) {
5862 key_freesp(sp
, KEY_SADB_LOCKED
);
5866 if (sp
->lifetime
== 0 && sp
->validtime
== 0)
5868 if (spbuf
&& spcount
< spbufcount
) {
5869 /* the deletion will occur next time */
5871 && tv
.tv_sec
- sp
->created
> sp
->lifetime
)
5873 && tv
.tv_sec
- sp
->lastused
> sp
->validtime
)) {
5874 //key_spdexpire(sp);
5875 sp
->state
= IPSEC_SPSTATE_DEAD
;
5887 struct secashead
*sah
, *nextsah
;
5888 struct secasvar
*sav
, *nextsav
;
5890 for (sah
= LIST_FIRST(&sahtree
);
5895 nextsah
= LIST_NEXT(sah
, chain
);
5897 /* if sah has been dead, then delete it and process next sah. */
5898 if (sah
->state
== SADB_SASTATE_DEAD
) {
5904 if (LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]) == NULL
&&
5905 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]) == NULL
&&
5906 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]) == NULL
&&
5907 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]) == NULL
) {
5913 if (savbufcount
== 0) {
5919 /* if LARVAL entry doesn't become MATURE, delete it. */
5920 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]);
5926 nextsav
= LIST_NEXT(sav
, chain
);
5928 if (sav
->lft_h
!= NULL
) {
5929 /* If a hard lifetime is defined for the LARVAL SA, use it */
5930 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0
5931 && tv
.tv_sec
- sav
->created
> sav
->lft_h
->sadb_lifetime_addtime
) {
5932 if (sav
->always_expire
) {
5933 key_send_delete(sav
);
5936 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5937 key_freesav(sav
, KEY_SADB_LOCKED
);
5942 if (tv
.tv_sec
- sav
->created
> key_larval_lifetime
) {
5943 key_freesav(sav
, KEY_SADB_LOCKED
);
5949 * If this is a NAT traversal SA with no activity,
5950 * we need to send a keep alive.
5952 * Performed outside of the loop before so we will
5953 * only ever send one keepalive. The first SA on
5954 * the list is the one that will be used for sending
5955 * traffic, so this is the one we use for determining
5956 * when to send the keepalive.
5958 if (savkabuf
&& savkacount
< savbufcount
) {
5959 sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]); //%%% should we check dying list if this is empty???
5960 if (sav
&& (natt_keepalive_interval
|| sav
->natt_interval
) &&
5961 (sav
->flags
& (SADB_X_EXT_NATT_KEEPALIVE
| SADB_X_EXT_ESP_KEEPALIVE
)) != 0) {
5969 * check MATURE entry to start to send expire message
5972 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]);
5978 nextsav
= LIST_NEXT(sav
, chain
);
5980 /* we don't need to check. */
5981 if (sav
->lft_s
== NULL
)
5985 if (sav
->lft_c
== NULL
) {
5986 ipseclog((LOG_DEBUG
,"key_timehandler: "
5987 "There is no CURRENT time, why?\n"));
5991 /* check SOFT lifetime */
5992 if (sav
->lft_s
->sadb_lifetime_addtime
!= 0
5993 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
5995 * If always_expire is set, expire. Otherwise,
5996 * if the SA has not been used, delete immediately.
5998 if (sav
->lft_c
->sadb_lifetime_usetime
== 0
5999 && sav
->always_expire
== 0) {
6000 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6001 key_freesav(sav
, KEY_SADB_LOCKED
);
6003 } else if (savexbuf
&& savexcount
< savbufcount
) {
6004 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
6011 /* check SOFT lifetime by bytes */
6013 * XXX I don't know the way to delete this SA
6014 * when new SA is installed. Caution when it's
6015 * installed too big lifetime by time.
6017 else if (savexbuf
&& savexcount
< savbufcount
6018 && sav
->lft_s
->sadb_lifetime_bytes
!= 0
6019 && sav
->lft_s
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
6022 * XXX If we keep to send expire
6023 * message in the status of
6024 * DYING. Do remove below code.
6027 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
6034 /* check DYING entry to change status to DEAD. */
6035 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]);
6041 nextsav
= LIST_NEXT(sav
, chain
);
6043 /* we don't need to check. */
6044 if (sav
->lft_h
== NULL
)
6048 if (sav
->lft_c
== NULL
) {
6049 ipseclog((LOG_DEBUG
, "key_timehandler: "
6050 "There is no CURRENT time, why?\n"));
6054 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0
6055 && tv
.tv_sec
- sav
->created
> sav
->lft_h
->sadb_lifetime_addtime
) {
6056 if (sav
->always_expire
) {
6057 key_send_delete(sav
);
6060 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6061 key_freesav(sav
, KEY_SADB_LOCKED
);
6065 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
6066 else if (savbuf
&& savexcount
< savbufcount
6067 && sav
->lft_s
!= NULL
6068 && sav
->lft_s
->sadb_lifetime_addtime
!= 0
6069 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
6071 * XXX: should be checked to be
6072 * installed the valid SA.
6076 * If there is no SA then sending
6085 /* check HARD lifetime by bytes */
6086 else if (sav
->lft_h
->sadb_lifetime_bytes
!= 0
6087 && sav
->lft_h
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
6088 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6089 key_freesav(sav
, KEY_SADB_LOCKED
);
6094 /* delete entry in DEAD */
6095 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]);
6101 nextsav
= LIST_NEXT(sav
, chain
);
6104 if (sav
->state
!= SADB_SASTATE_DEAD
) {
6105 ipseclog((LOG_DEBUG
, "key_timehandler: "
6106 "invalid sav->state "
6107 "(queue: %d SA: %d): "
6109 SADB_SASTATE_DEAD
, sav
->state
));
6113 * do not call key_freesav() here.
6114 * sav should already be freed, and sav->refcnt
6115 * shows other references to sav
6116 * (such as from SPD).
6122 if (++key_timehandler_debug
>= 300) {
6123 if (key_debug_level
) {
6124 printf("%s: total stats for %u calls\n", __FUNCTION__
, key_timehandler_debug
);
6125 printf("%s: walked %u SPDs\n", __FUNCTION__
, spd_count
);
6126 printf("%s: walked %llu SAs: LARVAL SAs %u, MATURE SAs %u, DYING SAs %u, DEAD SAs %u\n", __FUNCTION__
,
6127 total_sav_count
, larval_sav_count
, mature_sav_count
, dying_sav_count
, dead_sav_count
);
6128 printf("%s: walked %u SAHs: DEAD SAHs %u, EMPTY SAHs %u\n", __FUNCTION__
,
6129 sah_count
, dead_sah_count
, empty_sah_count
);
6130 if (sah_search_calls
) {
6131 printf("%s: SAH search cost %d iters per call\n", __FUNCTION__
,
6132 (sah_search_count
/sah_search_calls
));
6138 empty_sah_count
= 0;
6139 larval_sav_count
= 0;
6140 mature_sav_count
= 0;
6141 dying_sav_count
= 0;
6143 total_sav_count
= 0;
6144 sah_search_count
= 0;
6145 sah_search_calls
= 0;
6146 key_timehandler_debug
= 0;
6148 #ifndef IPSEC_NONBLOCK_ACQUIRE
6151 struct secacq
*acq
, *nextacq
;
6153 for (acq
= LIST_FIRST(&acqtree
);
6158 nextacq
= LIST_NEXT(acq
, chain
);
6160 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
6161 && __LIST_CHAINED(acq
)) {
6162 LIST_REMOVE(acq
, chain
);
6171 struct secspacq
*acq
, *nextacq
;
6173 for (acq
= LIST_FIRST(&spacqtree
);
6178 nextacq
= LIST_NEXT(acq
, chain
);
6180 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
6181 && __LIST_CHAINED(acq
)) {
6182 LIST_REMOVE(acq
, chain
);
6188 /* initialize random seed */
6189 if (key_tick_init_random
++ > key_int_random
) {
6190 key_tick_init_random
= 0;
6196 lck_mtx_unlock(sadb_mutex
);
6198 /* send messages outside of sadb_mutex */
6199 if (spbuf
&& spcount
> 0) {
6202 key_spdexpire(*(--spptr
));
6204 if (savkabuf
&& savkacount
> 0) {
6205 struct secasvar
**savkaptr_sav
= savkaptr
;
6206 int cnt_send
= savkacount
;
6208 while (cnt_send
--) {
6209 if (ipsec_send_natt_keepalive(*(--savkaptr
))) {
6210 // <rdar://6768487> iterate (all over again) and update timestamps
6211 struct secasvar
**savkaptr_update
= savkaptr_sav
;
6212 int cnt_update
= savkacount
;
6213 while (cnt_update
--) {
6214 key_update_natt_keepalive_timestamp(*savkaptr
,
6215 *(--savkaptr_update
));
6220 if (savexbuf
&& savexcount
> 0) {
6223 key_expire(*(--savexptr
));
6226 /* decrement ref counts and free buffers */
6227 lck_mtx_lock(sadb_mutex
);
6230 key_freesp(*spptr
++, KEY_SADB_LOCKED
);
6234 while (savkacount
--)
6235 key_freesav(*savkaptr
++, KEY_SADB_LOCKED
);
6239 while (savexcount
--)
6240 key_freesav(*savexptr
++, KEY_SADB_LOCKED
);
6245 key_timehandler_running
= 0;
6246 /* Turn on the ipsec bypass */
6249 /* do exchange to tick time !! */
6250 (void)timeout((void *)key_timehandler
, (void *)0, hz
);
6253 lck_mtx_unlock(sadb_mutex
);
6258 * to initialize a seed for random()
6264 /* Our PRNG is based on Yarrow and doesn't need to be seeded */
6271 srandom(tv
.tv_usec
);
6282 key_randomfill(&value
, sizeof(value
));
6293 read_random(p
, (u_int
)l
);
6297 static int warn
= 1;
6300 n
= (size_t)read_random(p
, (u_int
)l
);
6304 bcopy(&v
, (u_int8_t
*)p
+ n
,
6305 l
- n
< sizeof(v
) ? l
- n
: sizeof(v
));
6309 printf("WARNING: pseudo-random number generator "
6310 "used for IPsec processing\n");
6318 * map SADB_SATYPE_* to IPPROTO_*.
6319 * if satype == SADB_SATYPE then satype is mapped to ~0.
6321 * 0: invalid satype.
6328 case SADB_SATYPE_UNSPEC
:
6329 return IPSEC_PROTO_ANY
;
6330 case SADB_SATYPE_AH
:
6332 case SADB_SATYPE_ESP
:
6334 case SADB_X_SATYPE_IPCOMP
:
6335 return IPPROTO_IPCOMP
;
6344 * map IPPROTO_* to SADB_SATYPE_*
6346 * 0: invalid protocol type.
6354 return SADB_SATYPE_AH
;
6356 return SADB_SATYPE_ESP
;
6357 case IPPROTO_IPCOMP
:
6358 return SADB_X_SATYPE_IPCOMP
;
6367 key_get_ipsec_if_from_message (const struct sadb_msghdr
*mhp
)
6369 struct sadb_x_ipsecif
*ipsecifopts
= NULL
;
6370 ifnet_t ipsec_if
= NULL
;
6372 ipsecifopts
= (struct sadb_x_ipsecif
*)(void *)mhp
->ext
[SADB_X_EXT_IPSECIF
];
6373 if (ipsecifopts
!= NULL
) {
6374 if (ipsecifopts
->sadb_x_ipsecif_internal_if
) {
6375 ifnet_find_by_name(ipsecifopts
->sadb_x_ipsecif_ipsec_if
, &ipsec_if
);
6383 key_get_outgoing_ifindex_from_message (const struct sadb_msghdr
*mhp
)
6385 struct sadb_x_ipsecif
*ipsecifopts
= NULL
;
6386 ifnet_t outgoing_if
= NULL
;
6388 ipsecifopts
= (struct sadb_x_ipsecif
*)(void *)mhp
->ext
[SADB_X_EXT_IPSECIF
];
6389 if (ipsecifopts
!= NULL
) {
6390 if (ipsecifopts
->sadb_x_ipsecif_outgoing_if
) {
6391 ifnet_find_by_name(ipsecifopts
->sadb_x_ipsecif_outgoing_if
, &outgoing_if
);
6395 return outgoing_if
? outgoing_if
->if_index
: 0;
6400 * SADB_GETSPI processing is to receive
6401 * <base, (SA2), src address, dst address, (SPI range)>
6402 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
6403 * tree with the status of LARVAL, and send
6404 * <base, SA(*), address(SD)>
6407 * IN: mhp: pointer to the pointer to each header.
6408 * OUT: NULL if fail.
6409 * other if success, return pointer to the message to send.
6415 const struct sadb_msghdr
*mhp
)
6417 struct sadb_address
*src0
, *dst0
;
6418 struct secasindex saidx
;
6419 struct secashead
*newsah
;
6420 struct secasvar
*newsav
;
6421 ifnet_t ipsec_if
= NULL
;
6428 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6431 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6432 panic("key_getspi: NULL pointer is passed.\n");
6434 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6435 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
6436 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
6437 return key_senderror(so
, m
, EINVAL
);
6439 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6440 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
6441 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
6442 return key_senderror(so
, m
, EINVAL
);
6444 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
6445 mode
= ((struct sadb_x_sa2
*)
6446 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
6447 reqid
= ((struct sadb_x_sa2
*)
6448 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
6450 mode
= IPSEC_MODE_ANY
;
6454 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
6455 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
6457 ipsec_if
= key_get_ipsec_if_from_message(mhp
);
6459 /* map satype to proto */
6460 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6461 ipseclog((LOG_DEBUG
, "key_getspi: invalid satype is passed.\n"));
6462 return key_senderror(so
, m
, EINVAL
);
6465 /* make sure if port number is zero. */
6466 switch (((struct sockaddr
*)(src0
+ 1))->sa_family
) {
6468 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
6469 sizeof(struct sockaddr_in
))
6470 return key_senderror(so
, m
, EINVAL
);
6471 ((struct sockaddr_in
*)(void *)(src0
+ 1))->sin_port
= 0;
6474 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
6475 sizeof(struct sockaddr_in6
))
6476 return key_senderror(so
, m
, EINVAL
);
6477 ((struct sockaddr_in6
*)(void *)(src0
+ 1))->sin6_port
= 0;
6482 switch (((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
6484 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
6485 sizeof(struct sockaddr_in
))
6486 return key_senderror(so
, m
, EINVAL
);
6487 ((struct sockaddr_in
*)(void *)(dst0
+ 1))->sin_port
= 0;
6490 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
6491 sizeof(struct sockaddr_in6
))
6492 return key_senderror(so
, m
, EINVAL
);
6493 ((struct sockaddr_in6
*)(void *)(dst0
+ 1))->sin6_port
= 0;
6499 /* XXX boundary check against sa_len */
6500 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
6502 lck_mtx_lock(sadb_mutex
);
6504 /* SPI allocation */
6505 spi
= key_do_getnewspi((struct sadb_spirange
*)
6506 (void *)mhp
->ext
[SADB_EXT_SPIRANGE
], &saidx
);
6508 lck_mtx_unlock(sadb_mutex
);
6509 return key_senderror(so
, m
, EINVAL
);
6512 /* get a SA index */
6513 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
6514 /* create a new SA index: key_addspi is always used for inbound spi */
6515 if ((newsah
= key_newsah(&saidx
, ipsec_if
, key_get_outgoing_ifindex_from_message(mhp
), IPSEC_DIR_INBOUND
)) == NULL
) {
6516 lck_mtx_unlock(sadb_mutex
);
6517 ipseclog((LOG_DEBUG
, "key_getspi: No more memory.\n"));
6518 return key_senderror(so
, m
, ENOBUFS
);
6524 newsav
= key_newsav(m
, mhp
, newsah
, &error
, so
);
6525 if (newsav
== NULL
) {
6526 /* XXX don't free new SA index allocated in above. */
6527 lck_mtx_unlock(sadb_mutex
);
6528 return key_senderror(so
, m
, error
);
6532 key_setspi(newsav
, htonl(spi
));
6534 #ifndef IPSEC_NONBLOCK_ACQUIRE
6535 /* delete the entry in acqtree */
6536 if (mhp
->msg
->sadb_msg_seq
!= 0) {
6538 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) != NULL
) {
6539 /* reset counter in order to deletion by timehandler. */
6542 acq
->created
= tv
.tv_sec
;
6548 lck_mtx_unlock(sadb_mutex
);
6551 struct mbuf
*n
, *nn
;
6552 struct sadb_sa
*m_sa
;
6553 struct sadb_msg
*newmsg
;
6556 /* create new sadb_msg to reply. */
6557 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
)) +
6558 PFKEY_ALIGN8(sizeof(struct sadb_sa
));
6560 return key_senderror(so
, m
, ENOBUFS
);
6562 MGETHDR(n
, M_WAITOK
, MT_DATA
);
6563 if (n
&& len
> MHLEN
) {
6564 MCLGET(n
, M_WAITOK
);
6565 if ((n
->m_flags
& M_EXT
) == 0) {
6571 return key_senderror(so
, m
, ENOBUFS
);
6577 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
6578 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
6580 m_sa
= (struct sadb_sa
*)(void *)(mtod(n
, caddr_t
) + off
);
6581 m_sa
->sadb_sa_len
= PFKEY_UNIT64(sizeof(struct sadb_sa
));
6582 m_sa
->sadb_sa_exttype
= SADB_EXT_SA
;
6583 m_sa
->sadb_sa_spi
= htonl(spi
);
6584 off
+= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
6588 panic("length inconsistency in key_getspi");
6591 int mbufItems
[] = {SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
6592 n
->m_next
= key_gather_mbuf(m
, mhp
, 0, sizeof(mbufItems
)/sizeof(int), mbufItems
);
6595 return key_senderror(so
, m
, ENOBUFS
);
6599 if (n
->m_len
< sizeof(struct sadb_msg
)) {
6600 n
= m_pullup(n
, sizeof(struct sadb_msg
));
6602 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
6605 n
->m_pkthdr
.len
= 0;
6606 for (nn
= n
; nn
; nn
= nn
->m_next
)
6607 n
->m_pkthdr
.len
+= nn
->m_len
;
6609 newmsg
= mtod(n
, struct sadb_msg
*);
6610 newmsg
->sadb_msg_seq
= newsav
->seq
;
6611 newmsg
->sadb_msg_errno
= 0;
6612 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
6615 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
6620 key_getspi2(struct sockaddr
*src
,
6621 struct sockaddr
*dst
,
6625 struct sadb_spirange
*spirange
)
6628 struct secasindex saidx
;
6630 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6632 /* XXX boundary check against sa_len */
6633 KEY_SETSECASIDX(proto
, mode
, reqid
, src
, dst
, 0, &saidx
);
6635 /* make sure if port number is zero. */
6636 switch (((struct sockaddr
*)&saidx
.src
)->sa_family
) {
6638 if (((struct sockaddr
*)&saidx
.src
)->sa_len
!= sizeof(struct sockaddr_in
))
6640 ((struct sockaddr_in
*)&saidx
.src
)->sin_port
= 0;
6643 if (((struct sockaddr
*)&saidx
.src
)->sa_len
!= sizeof(struct sockaddr_in6
))
6645 ((struct sockaddr_in6
*)&saidx
.src
)->sin6_port
= 0;
6650 switch (((struct sockaddr
*)&saidx
.dst
)->sa_family
) {
6652 if (((struct sockaddr
*)&saidx
.dst
)->sa_len
!= sizeof(struct sockaddr_in
))
6654 ((struct sockaddr_in
*)&saidx
.dst
)->sin_port
= 0;
6657 if (((struct sockaddr
*)&saidx
.dst
)->sa_len
!= sizeof(struct sockaddr_in6
))
6659 ((struct sockaddr_in6
*)&saidx
.dst
)->sin6_port
= 0;
6665 lck_mtx_lock(sadb_mutex
);
6667 /* SPI allocation */
6668 spi
= key_do_getnewspi(spirange
, &saidx
);
6670 lck_mtx_unlock(sadb_mutex
);
6676 * allocating new SPI
6677 * called by key_getspi() and key_getspi2().
6684 struct sadb_spirange
*spirange
,
6685 struct secasindex
*saidx
)
6688 u_int32_t keymin
, keymax
;
6689 int count
= key_spi_trycnt
;
6691 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6693 /* set spi range to allocate */
6694 if (spirange
!= NULL
) {
6695 keymin
= spirange
->sadb_spirange_min
;
6696 keymax
= spirange
->sadb_spirange_max
;
6698 keymin
= key_spi_minval
;
6699 keymax
= key_spi_maxval
;
6701 /* IPCOMP needs 2-byte SPI */
6702 if (saidx
->proto
== IPPROTO_IPCOMP
) {
6704 if (keymin
>= 0x10000)
6706 if (keymax
>= 0x10000)
6708 if (keymin
> keymax
) {
6709 t
= keymin
; keymin
= keymax
; keymax
= t
;
6713 if (keymin
== keymax
) {
6714 if (key_checkspidup(saidx
, keymin
) != NULL
) {
6715 ipseclog((LOG_DEBUG
, "key_do_getnewspi: SPI %u exists already.\n", keymin
));
6719 count
--; /* taking one cost. */
6724 u_int32_t range
= keymax
- keymin
+ 1; /* overflow value of zero means full range */
6729 /* when requesting to allocate spi ranged */
6731 u_int32_t rand_val
= key_random();
6733 /* generate pseudo-random SPI value ranged. */
6734 newspi
= (range
== 0 ? rand_val
: keymin
+ (rand_val
% range
));
6736 if (key_checkspidup(saidx
, newspi
) == NULL
)
6740 if (count
== 0 || newspi
== 0) {
6741 ipseclog((LOG_DEBUG
, "key_do_getnewspi: to allocate spi is failed.\n"));
6747 keystat
.getspi_count
=
6748 (keystat
.getspi_count
+ key_spi_trycnt
- count
) / 2;
6754 * SADB_UPDATE processing
6756 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
6757 * key(AE), (identity(SD),) (sensitivity)>
6758 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
6760 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
6761 * (identity(SD),) (sensitivity)>
6764 * m will always be freed.
6770 const struct sadb_msghdr
*mhp
)
6772 struct sadb_sa
*sa0
;
6773 struct sadb_address
*src0
, *dst0
;
6774 ifnet_t ipsec_if
= NULL
;
6775 struct secasindex saidx
;
6776 struct secashead
*sah
;
6777 struct secasvar
*sav
;
6784 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6787 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6788 panic("key_update: NULL pointer is passed.\n");
6790 /* map satype to proto */
6791 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6792 ipseclog((LOG_DEBUG
, "key_update: invalid satype is passed.\n"));
6793 return key_senderror(so
, m
, EINVAL
);
6796 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
6797 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6798 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
6799 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
6800 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
6801 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
6802 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
6803 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
6804 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
6805 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
6806 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
6807 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
6808 return key_senderror(so
, m
, EINVAL
);
6810 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
6811 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6812 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
6813 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
6814 return key_senderror(so
, m
, EINVAL
);
6816 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
6817 mode
= ((struct sadb_x_sa2
*)
6818 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
6819 reqid
= ((struct sadb_x_sa2
*)
6820 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
6821 flags2
= ((struct sadb_x_sa2
*)(void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_flags
;
6823 mode
= IPSEC_MODE_ANY
;
6827 /* XXX boundary checking for other extensions */
6829 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
6830 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
6831 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
6832 ipsec_if
= key_get_ipsec_if_from_message(mhp
);
6834 /* XXX boundary check against sa_len */
6835 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
6837 lck_mtx_lock(sadb_mutex
);
6839 /* get a SA header */
6840 if ((sah
= key_getsah(&saidx
)) == NULL
) {
6841 lck_mtx_unlock(sadb_mutex
);
6842 ipseclog((LOG_DEBUG
, "key_update: no SA index found.\n"));
6843 return key_senderror(so
, m
, ENOENT
);
6846 /* set spidx if there */
6848 error
= key_setident(sah
, m
, mhp
);
6850 lck_mtx_unlock(sadb_mutex
);
6851 return key_senderror(so
, m
, error
);
6854 /* find a SA with sequence number. */
6855 #if IPSEC_DOSEQCHECK
6856 if (mhp
->msg
->sadb_msg_seq
!= 0
6857 && (sav
= key_getsavbyseq(sah
, mhp
->msg
->sadb_msg_seq
)) == NULL
) {
6858 lck_mtx_unlock(sadb_mutex
);
6859 ipseclog((LOG_DEBUG
,
6860 "key_update: no larval SA with sequence %u exists.\n",
6861 mhp
->msg
->sadb_msg_seq
));
6862 return key_senderror(so
, m
, ENOENT
);
6865 if ((sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
)) == NULL
) {
6866 lck_mtx_unlock(sadb_mutex
);
6867 ipseclog((LOG_DEBUG
,
6868 "key_update: no such a SA found (spi:%u)\n",
6869 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
6870 return key_senderror(so
, m
, EINVAL
);
6874 /* validity check */
6875 if (sav
->sah
->saidx
.proto
!= proto
) {
6876 lck_mtx_unlock(sadb_mutex
);
6877 ipseclog((LOG_DEBUG
,
6878 "key_update: protocol mismatched (DB=%u param=%u)\n",
6879 sav
->sah
->saidx
.proto
, proto
));
6880 return key_senderror(so
, m
, EINVAL
);
6882 #if IPSEC_DOSEQCHECK
6883 if (sav
->spi
!= sa0
->sadb_sa_spi
) {
6884 lck_mtx_unlock(sadb_mutex
);
6885 ipseclog((LOG_DEBUG
,
6886 "key_update: SPI mismatched (DB:%u param:%u)\n",
6887 (u_int32_t
)ntohl(sav
->spi
),
6888 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
6889 return key_senderror(so
, m
, EINVAL
);
6892 if (sav
->pid
!= mhp
->msg
->sadb_msg_pid
) {
6893 lck_mtx_unlock(sadb_mutex
);
6894 ipseclog((LOG_DEBUG
,
6895 "key_update: pid mismatched (DB:%u param:%u)\n",
6896 sav
->pid
, mhp
->msg
->sadb_msg_pid
));
6897 return key_senderror(so
, m
, EINVAL
);
6900 /* copy sav values */
6901 error
= key_setsaval(sav
, m
, mhp
);
6903 key_freesav(sav
, KEY_SADB_LOCKED
);
6904 lck_mtx_unlock(sadb_mutex
);
6905 return key_senderror(so
, m
, error
);
6908 sav
->flags2
= flags2
;
6909 if (flags2
& SADB_X_EXT_SA2_DELETE_ON_DETACH
) {
6914 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
6915 * this SA is for transport mode - otherwise clear it.
6917 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0 &&
6918 (sav
->sah
->saidx
.mode
!= IPSEC_MODE_TRANSPORT
||
6919 sav
->sah
->saidx
.src
.ss_family
!= AF_INET
))
6920 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
6922 /* check SA values to be mature. */
6923 if ((error
= key_mature(sav
)) != 0) {
6924 key_freesav(sav
, KEY_SADB_LOCKED
);
6925 lck_mtx_unlock(sadb_mutex
);
6926 return key_senderror(so
, m
, error
);
6929 lck_mtx_unlock(sadb_mutex
);
6934 /* set msg buf from mhp */
6935 n
= key_getmsgbuf_x1(m
, mhp
);
6937 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
6938 return key_senderror(so
, m
, ENOBUFS
);
6942 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
6947 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
6948 * only called by key_update().
6951 * others : found, pointer to a SA.
6953 #if IPSEC_DOSEQCHECK
6954 static struct secasvar
*
6956 struct secashead
*sah
,
6959 struct secasvar
*sav
;
6962 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6964 state
= SADB_SASTATE_LARVAL
;
6966 /* search SAD with sequence number ? */
6967 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
6969 KEY_CHKSASTATE(state
, sav
->state
, "key_getsabyseq");
6971 if (sav
->seq
== seq
) {
6973 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
6974 printf("DP key_getsavbyseq cause "
6975 "refcnt++:%d SA:0x%llx\n", sav
->refcnt
,
6976 (uint64_t)VM_KERNEL_ADDRPERM(sav
)));
6986 * SADB_ADD processing
6987 * add a entry to SA database, when received
6988 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
6989 * key(AE), (identity(SD),) (sensitivity)>
6992 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
6993 * (identity(SD),) (sensitivity)>
6996 * IGNORE identity and sensitivity messages.
6998 * m will always be freed.
7004 const struct sadb_msghdr
*mhp
)
7006 struct sadb_sa
*sa0
;
7007 struct sadb_address
*src0
, *dst0
;
7008 ifnet_t ipsec_if
= NULL
;
7009 struct secasindex saidx
;
7010 struct secashead
*newsah
;
7011 struct secasvar
*newsav
;
7017 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7020 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7021 panic("key_add: NULL pointer is passed.\n");
7023 /* map satype to proto */
7024 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7025 ipseclog((LOG_DEBUG
, "key_add: invalid satype is passed.\n"));
7026 return key_senderror(so
, m
, EINVAL
);
7029 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
7030 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
7031 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
7032 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
7033 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
7034 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
7035 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
7036 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
7037 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
7038 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
7039 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
7040 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
7041 return key_senderror(so
, m
, EINVAL
);
7043 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
7044 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7045 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
7047 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
7048 return key_senderror(so
, m
, EINVAL
);
7050 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
7051 mode
= ((struct sadb_x_sa2
*)
7052 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
7053 reqid
= ((struct sadb_x_sa2
*)
7054 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
7056 mode
= IPSEC_MODE_ANY
;
7060 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
7061 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
7062 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
7063 ipsec_if
= key_get_ipsec_if_from_message(mhp
);
7065 /* XXX boundary check against sa_len */
7066 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
7068 lck_mtx_lock(sadb_mutex
);
7070 /* get a SA header */
7071 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
7072 /* create a new SA header: key_addspi is always used for outbound spi */
7073 if ((newsah
= key_newsah(&saidx
, ipsec_if
, key_get_outgoing_ifindex_from_message(mhp
), IPSEC_DIR_OUTBOUND
)) == NULL
) {
7074 lck_mtx_unlock(sadb_mutex
);
7075 ipseclog((LOG_DEBUG
, "key_add: No more memory.\n"));
7076 return key_senderror(so
, m
, ENOBUFS
);
7080 /* set spidx if there */
7082 error
= key_setident(newsah
, m
, mhp
);
7084 lck_mtx_unlock(sadb_mutex
);
7085 return key_senderror(so
, m
, error
);
7088 /* create new SA entry. */
7089 /* We can create new SA only if SPI is different. */
7090 if (key_getsavbyspi(newsah
, sa0
->sadb_sa_spi
)) {
7091 lck_mtx_unlock(sadb_mutex
);
7092 ipseclog((LOG_DEBUG
, "key_add: SA already exists.\n"));
7093 return key_senderror(so
, m
, EEXIST
);
7095 newsav
= key_newsav(m
, mhp
, newsah
, &error
, so
);
7096 if (newsav
== NULL
) {
7097 lck_mtx_unlock(sadb_mutex
);
7098 return key_senderror(so
, m
, error
);
7102 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
7103 * this SA is for transport mode - otherwise clear it.
7105 if ((newsav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0 &&
7106 (newsah
->saidx
.mode
!= IPSEC_MODE_TRANSPORT
||
7107 newsah
->saidx
.dst
.ss_family
!= AF_INET
))
7108 newsav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
7110 /* check SA values to be mature. */
7111 if ((error
= key_mature(newsav
)) != 0) {
7112 key_freesav(newsav
, KEY_SADB_LOCKED
);
7113 lck_mtx_unlock(sadb_mutex
);
7114 return key_senderror(so
, m
, error
);
7117 lck_mtx_unlock(sadb_mutex
);
7120 * don't call key_freesav() here, as we would like to keep the SA
7121 * in the database on success.
7127 /* set msg buf from mhp */
7128 n
= key_getmsgbuf_x1(m
, mhp
);
7130 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
7131 return key_senderror(so
, m
, ENOBUFS
);
7135 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
7142 struct secashead
*sah
,
7144 const struct sadb_msghdr
*mhp
)
7146 const struct sadb_ident
*idsrc
, *iddst
;
7147 int idsrclen
, iddstlen
;
7149 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7152 if (sah
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7153 panic("key_setident: NULL pointer is passed.\n");
7155 /* don't make buffer if not there */
7156 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
&&
7157 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
7163 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
||
7164 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
7165 ipseclog((LOG_DEBUG
, "key_setident: invalid identity.\n"));
7169 idsrc
= (const struct sadb_ident
*)
7170 (void *)mhp
->ext
[SADB_EXT_IDENTITY_SRC
];
7171 iddst
= (const struct sadb_ident
*)
7172 (void *)mhp
->ext
[SADB_EXT_IDENTITY_DST
];
7173 idsrclen
= mhp
->extlen
[SADB_EXT_IDENTITY_SRC
];
7174 iddstlen
= mhp
->extlen
[SADB_EXT_IDENTITY_DST
];
7176 /* validity check */
7177 if (idsrc
->sadb_ident_type
!= iddst
->sadb_ident_type
) {
7178 ipseclog((LOG_DEBUG
, "key_setident: ident type mismatch.\n"));
7182 switch (idsrc
->sadb_ident_type
) {
7183 case SADB_IDENTTYPE_PREFIX
:
7184 case SADB_IDENTTYPE_FQDN
:
7185 case SADB_IDENTTYPE_USERFQDN
:
7187 /* XXX do nothing */
7193 /* make structure */
7194 KMALLOC_NOWAIT(sah
->idents
, struct sadb_ident
*, idsrclen
);
7195 if (sah
->idents
== NULL
) {
7196 lck_mtx_unlock(sadb_mutex
);
7197 KMALLOC_WAIT(sah
->idents
, struct sadb_ident
*, idsrclen
);
7198 lck_mtx_lock(sadb_mutex
);
7199 if (sah
->idents
== NULL
) {
7200 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
7204 KMALLOC_NOWAIT(sah
->identd
, struct sadb_ident
*, iddstlen
);
7205 if (sah
->identd
== NULL
) {
7206 lck_mtx_unlock(sadb_mutex
);
7207 KMALLOC_WAIT(sah
->identd
, struct sadb_ident
*, iddstlen
);
7208 lck_mtx_lock(sadb_mutex
);
7209 if (sah
->identd
== NULL
) {
7212 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
7216 bcopy(idsrc
, sah
->idents
, idsrclen
);
7217 bcopy(iddst
, sah
->identd
, iddstlen
);
7223 * m will not be freed on return.
7224 * it is caller's responsibility to free the result.
7226 static struct mbuf
*
7229 const struct sadb_msghdr
*mhp
)
7232 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
7233 SADB_X_EXT_SA2
, SADB_EXT_ADDRESS_SRC
,
7234 SADB_EXT_ADDRESS_DST
, SADB_EXT_LIFETIME_HARD
,
7235 SADB_EXT_LIFETIME_SOFT
, SADB_EXT_IDENTITY_SRC
,
7236 SADB_EXT_IDENTITY_DST
};
7239 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7240 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
7242 /* create new sadb_msg to reply. */
7243 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
7247 if (n
->m_len
< sizeof(struct sadb_msg
)) {
7248 n
= m_pullup(n
, sizeof(struct sadb_msg
));
7252 mtod(n
, struct sadb_msg
*)->sadb_msg_errno
= 0;
7253 mtod(n
, struct sadb_msg
*)->sadb_msg_len
=
7254 PFKEY_UNIT64(n
->m_pkthdr
.len
);
7259 static int key_delete_all(struct socket
*, struct mbuf
*,
7260 const struct sadb_msghdr
*, u_int16_t
);
7263 * SADB_DELETE processing
7265 * <base, SA(*), address(SD)>
7266 * from the ikmpd, and set SADB_SASTATE_DEAD,
7268 * <base, SA(*), address(SD)>
7271 * m will always be freed.
7277 const struct sadb_msghdr
*mhp
)
7279 struct sadb_sa
*sa0
;
7280 struct sadb_address
*src0
, *dst0
;
7281 ifnet_t ipsec_if
= NULL
;
7282 struct secasindex saidx
;
7283 struct secashead
*sah
;
7284 struct secasvar
*sav
= NULL
;
7287 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7290 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7291 panic("key_delete: NULL pointer is passed.\n");
7293 /* map satype to proto */
7294 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7295 ipseclog((LOG_DEBUG
, "key_delete: invalid satype is passed.\n"));
7296 return key_senderror(so
, m
, EINVAL
);
7299 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
7300 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
7301 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
7302 return key_senderror(so
, m
, EINVAL
);
7305 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7306 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
7307 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
7308 return key_senderror(so
, m
, EINVAL
);
7311 lck_mtx_lock(sadb_mutex
);
7313 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
7315 * Caller wants us to delete all non-LARVAL SAs
7316 * that match the src/dst. This is used during
7317 * IKE INITIAL-CONTACT.
7319 ipseclog((LOG_DEBUG
, "key_delete: doing delete all.\n"));
7320 /* key_delete_all will unlock sadb_mutex */
7321 return key_delete_all(so
, m
, mhp
, proto
);
7322 } else if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
)) {
7323 lck_mtx_unlock(sadb_mutex
);
7324 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
7325 return key_senderror(so
, m
, EINVAL
);
7328 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
7329 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
7330 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
7331 ipsec_if
= key_get_ipsec_if_from_message(mhp
);
7333 /* XXX boundary check against sa_len */
7334 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
7336 /* get a SA header */
7337 LIST_FOREACH(sah
, &sahtree
, chain
) {
7338 if (sah
->state
== SADB_SASTATE_DEAD
)
7340 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
7343 /* get a SA with SPI. */
7344 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
7349 lck_mtx_unlock(sadb_mutex
);
7350 ipseclog((LOG_DEBUG
, "key_delete: no SA found.\n"));
7351 return key_senderror(so
, m
, ENOENT
);
7354 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
7355 key_freesav(sav
, KEY_SADB_LOCKED
);
7357 lck_mtx_unlock(sadb_mutex
);
7362 struct sadb_msg
*newmsg
;
7363 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
7364 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
7366 /* create new sadb_msg to reply. */
7367 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
7369 return key_senderror(so
, m
, ENOBUFS
);
7371 if (n
->m_len
< sizeof(struct sadb_msg
)) {
7372 n
= m_pullup(n
, sizeof(struct sadb_msg
));
7374 return key_senderror(so
, m
, ENOBUFS
);
7376 newmsg
= mtod(n
, struct sadb_msg
*);
7377 newmsg
->sadb_msg_errno
= 0;
7378 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
7381 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
7386 * delete all SAs for src/dst. Called from key_delete().
7392 const struct sadb_msghdr
*mhp
,
7395 struct sadb_address
*src0
, *dst0
;
7396 ifnet_t ipsec_if
= NULL
;
7397 struct secasindex saidx
;
7398 struct secashead
*sah
;
7399 struct secasvar
*sav
, *nextsav
;
7400 u_int stateidx
, state
;
7402 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7404 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
7405 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
7406 ipsec_if
= key_get_ipsec_if_from_message(mhp
);
7408 /* XXX boundary check against sa_len */
7409 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
7411 LIST_FOREACH(sah
, &sahtree
, chain
) {
7412 if (sah
->state
== SADB_SASTATE_DEAD
)
7414 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
7417 /* Delete all non-LARVAL SAs. */
7419 stateidx
< _ARRAYLEN(saorder_state_alive
);
7421 state
= saorder_state_alive
[stateidx
];
7422 if (state
== SADB_SASTATE_LARVAL
)
7424 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
7425 sav
!= NULL
; sav
= nextsav
) {
7426 nextsav
= LIST_NEXT(sav
, chain
);
7428 if (sav
->state
!= state
) {
7429 ipseclog((LOG_DEBUG
, "key_delete_all: "
7430 "invalid sav->state "
7431 "(queue: %d SA: %d)\n",
7432 state
, sav
->state
));
7436 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
7437 key_freesav(sav
, KEY_SADB_LOCKED
);
7441 lck_mtx_unlock(sadb_mutex
);
7445 struct sadb_msg
*newmsg
;
7446 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_ADDRESS_SRC
,
7447 SADB_EXT_ADDRESS_DST
};
7449 /* create new sadb_msg to reply. */
7450 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
7452 return key_senderror(so
, m
, ENOBUFS
);
7454 if (n
->m_len
< sizeof(struct sadb_msg
)) {
7455 n
= m_pullup(n
, sizeof(struct sadb_msg
));
7457 return key_senderror(so
, m
, ENOBUFS
);
7459 newmsg
= mtod(n
, struct sadb_msg
*);
7460 newmsg
->sadb_msg_errno
= 0;
7461 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
7464 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
7469 * SADB_GET processing
7471 * <base, SA(*), address(SD)>
7472 * from the ikmpd, and get a SP and a SA to respond,
7474 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
7475 * (identity(SD),) (sensitivity)>
7478 * m will always be freed.
7484 const struct sadb_msghdr
*mhp
)
7486 struct sadb_sa
*sa0
;
7487 struct sadb_address
*src0
, *dst0
;
7488 ifnet_t ipsec_if
= NULL
;
7489 struct secasindex saidx
;
7490 struct secashead
*sah
;
7491 struct secasvar
*sav
= NULL
;
7494 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7497 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7498 panic("key_get: NULL pointer is passed.\n");
7500 /* map satype to proto */
7501 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7502 ipseclog((LOG_DEBUG
, "key_get: invalid satype is passed.\n"));
7503 return key_senderror(so
, m
, EINVAL
);
7506 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
7507 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
7508 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
7509 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
7510 return key_senderror(so
, m
, EINVAL
);
7512 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
7513 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7514 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
7515 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
7516 return key_senderror(so
, m
, EINVAL
);
7519 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
7520 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
7521 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
7522 ipsec_if
= key_get_ipsec_if_from_message(mhp
);
7524 /* XXX boundary check against sa_len */
7525 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
7527 lck_mtx_lock(sadb_mutex
);
7529 /* get a SA header */
7530 LIST_FOREACH(sah
, &sahtree
, chain
) {
7531 if (sah
->state
== SADB_SASTATE_DEAD
)
7533 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
7536 /* get a SA with SPI. */
7537 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
7542 lck_mtx_unlock(sadb_mutex
);
7543 ipseclog((LOG_DEBUG
, "key_get: no SA found.\n"));
7544 return key_senderror(so
, m
, ENOENT
);
7551 /* map proto to satype */
7552 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
7553 lck_mtx_unlock(sadb_mutex
);
7554 ipseclog((LOG_DEBUG
, "key_get: there was invalid proto in SAD.\n"));
7555 return key_senderror(so
, m
, EINVAL
);
7557 lck_mtx_unlock(sadb_mutex
);
7559 /* create new sadb_msg to reply. */
7560 n
= key_setdumpsa(sav
, SADB_GET
, satype
, mhp
->msg
->sadb_msg_seq
,
7561 mhp
->msg
->sadb_msg_pid
);
7566 return key_senderror(so
, m
, ENOBUFS
);
7569 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
7574 * get SA stats by spi.
7575 * OUT: -1 : not found
7576 * 0 : found, arg pointer to a SA stats is updated.
7579 key_getsastatbyspi_one (u_int32_t spi
,
7580 struct sastat
*stat
)
7582 struct secashead
*sah
;
7583 struct secasvar
*sav
= NULL
;
7585 if ((void *)stat
== NULL
) {
7589 lck_mtx_lock(sadb_mutex
);
7591 /* get a SA header */
7592 LIST_FOREACH(sah
, &sahtree
, chain
) {
7593 if (sah
->state
== SADB_SASTATE_DEAD
)
7596 /* get a SA with SPI. */
7597 sav
= key_getsavbyspi(sah
, spi
);
7599 stat
->spi
= sav
->spi
;
7600 stat
->created
= sav
->created
;
7602 bcopy(sav
->lft_c
,&stat
->lft_c
, sizeof(stat
->lft_c
));
7604 bzero(&stat
->lft_c
, sizeof(stat
->lft_c
));
7606 lck_mtx_unlock(sadb_mutex
);
7611 lck_mtx_unlock(sadb_mutex
);
7617 * get SA stats collection by indices.
7618 * OUT: -1 : not found
7619 * 0 : found, arg pointers to a SA stats and 'maximum stats' are updated.
7622 key_getsastatbyspi (struct sastat
*stat_arg
,
7623 u_int32_t max_stat_arg
,
7624 struct sastat
*stat_res
,
7625 u_int32_t
*max_stat_res
)
7629 if (stat_arg
== NULL
||
7631 max_stat_res
== NULL
) {
7635 for (cur
= 0; cur
< max_stat_arg
; cur
++) {
7636 if (key_getsastatbyspi_one(stat_arg
[cur
].spi
,
7637 &stat_res
[found
]) == 0) {
7641 *max_stat_res
= found
;
7649 /* XXX make it sysctl-configurable? */
7651 key_getcomb_setlifetime(
7652 struct sadb_comb
*comb
)
7655 comb
->sadb_comb_soft_allocations
= 1;
7656 comb
->sadb_comb_hard_allocations
= 1;
7657 comb
->sadb_comb_soft_bytes
= 0;
7658 comb
->sadb_comb_hard_bytes
= 0;
7659 comb
->sadb_comb_hard_addtime
= 86400; /* 1 day */
7660 comb
->sadb_comb_soft_addtime
= comb
->sadb_comb_soft_addtime
* 80 / 100;
7661 comb
->sadb_comb_soft_usetime
= 28800; /* 8 hours */
7662 comb
->sadb_comb_hard_usetime
= comb
->sadb_comb_hard_usetime
* 80 / 100;
7667 * XXX reorder combinations by preference
7668 * XXX no idea if the user wants ESP authentication or not
7670 static struct mbuf
*
7671 key_getcomb_esp(void)
7673 struct sadb_comb
*comb
;
7674 const struct esp_algorithm
*algo
;
7675 struct mbuf
*result
= NULL
, *m
, *n
;
7679 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
7682 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
7683 algo
= esp_algorithm_lookup(i
);
7687 if (algo
->keymax
< ipsec_esp_keymin
)
7689 if (algo
->keymin
< ipsec_esp_keymin
)
7690 encmin
= ipsec_esp_keymin
;
7692 encmin
= algo
->keymin
;
7695 m
= key_getcomb_ah();
7699 panic("assumption failed in key_getcomb_esp");
7701 MGET(m
, M_WAITOK
, MT_DATA
);
7706 bzero(mtod(m
, caddr_t
), m
->m_len
);
7713 for (n
= m
; n
; n
= n
->m_next
)
7717 panic("assumption failed in key_getcomb_esp");
7720 for (off
= 0; off
< totlen
; off
+= l
) {
7721 n
= m_pulldown(m
, off
, l
, &o
);
7723 /* m is already freed */
7726 comb
= (struct sadb_comb
*)
7727 (void *)(mtod(n
, caddr_t
) + o
);
7728 bzero(comb
, sizeof(*comb
));
7729 key_getcomb_setlifetime(comb
);
7730 comb
->sadb_comb_encrypt
= i
;
7731 comb
->sadb_comb_encrypt_minbits
= encmin
;
7732 comb
->sadb_comb_encrypt_maxbits
= algo
->keymax
;
7751 * XXX reorder combinations by preference
7753 static struct mbuf
*
7754 key_getcomb_ah(void)
7756 struct sadb_comb
*comb
;
7757 const struct ah_algorithm
*algo
;
7761 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
7764 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
7766 /* we prefer HMAC algorithms, not old algorithms */
7767 if (i
!= SADB_AALG_SHA1HMAC
&& i
!= SADB_AALG_MD5HMAC
)
7770 algo
= ah_algorithm_lookup(i
);
7774 if (algo
->keymax
< ipsec_ah_keymin
)
7776 if (algo
->keymin
< ipsec_ah_keymin
)
7777 keymin
= ipsec_ah_keymin
;
7779 keymin
= algo
->keymin
;
7784 panic("assumption failed in key_getcomb_ah");
7786 MGET(m
, M_WAITOK
, MT_DATA
);
7793 M_PREPEND(m
, l
, M_WAITOK
);
7797 comb
= mtod(m
, struct sadb_comb
*);
7798 bzero(comb
, sizeof(*comb
));
7799 key_getcomb_setlifetime(comb
);
7800 comb
->sadb_comb_auth
= i
;
7801 comb
->sadb_comb_auth_minbits
= keymin
;
7802 comb
->sadb_comb_auth_maxbits
= algo
->keymax
;
7809 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
7810 * XXX reorder combinations by preference
7812 static struct mbuf
*
7813 key_getcomb_ipcomp(void)
7815 struct sadb_comb
*comb
;
7816 const struct ipcomp_algorithm
*algo
;
7819 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
7822 for (i
= 1; i
<= SADB_X_CALG_MAX
; i
++) {
7823 algo
= ipcomp_algorithm_lookup(i
);
7830 panic("assumption failed in key_getcomb_ipcomp");
7832 MGET(m
, M_WAITOK
, MT_DATA
);
7839 M_PREPEND(m
, l
, M_WAITOK
);
7843 comb
= mtod(m
, struct sadb_comb
*);
7844 bzero(comb
, sizeof(*comb
));
7845 key_getcomb_setlifetime(comb
);
7846 comb
->sadb_comb_encrypt
= i
;
7847 /* what should we set into sadb_comb_*_{min,max}bits? */
7854 * XXX no way to pass mode (transport/tunnel) to userland
7855 * XXX replay checking?
7856 * XXX sysctl interface to ipsec_{ah,esp}_keymin
7858 static struct mbuf
*
7860 const struct secasindex
*saidx
)
7862 struct sadb_prop
*prop
;
7864 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_prop
));
7867 switch (saidx
->proto
) {
7870 m
= key_getcomb_esp();
7874 m
= key_getcomb_ah();
7876 case IPPROTO_IPCOMP
:
7877 m
= key_getcomb_ipcomp();
7885 M_PREPEND(m
, l
, M_WAITOK
);
7890 for (n
= m
; n
; n
= n
->m_next
)
7893 prop
= mtod(m
, struct sadb_prop
*);
7894 bzero(prop
, sizeof(*prop
));
7895 prop
->sadb_prop_len
= PFKEY_UNIT64(totlen
);
7896 prop
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
7897 prop
->sadb_prop_replay
= 32; /* XXX */
7903 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
7905 * <base, SA, address(SD), (address(P)), x_policy,
7906 * (identity(SD),) (sensitivity,) proposal>
7907 * to KMD, and expect to receive
7908 * <base> with SADB_ACQUIRE if error occurred,
7910 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
7911 * from KMD by PF_KEY.
7913 * XXX x_policy is outside of RFC2367 (KAME extension).
7914 * XXX sensitivity is not supported.
7915 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
7916 * see comment for key_getcomb_ipcomp().
7920 * others: error number
7924 struct secasindex
*saidx
,
7925 struct secpolicy
*sp
)
7927 struct mbuf
*result
= NULL
, *m
;
7928 #ifndef IPSEC_NONBLOCK_ACQUIRE
7929 struct secacq
*newacq
;
7935 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7939 panic("key_acquire: NULL pointer is passed.\n");
7940 if ((satype
= key_proto2satype(saidx
->proto
)) == 0)
7941 panic("key_acquire: invalid proto is passed.\n");
7943 #ifndef IPSEC_NONBLOCK_ACQUIRE
7945 * We never do anything about acquirng SA. There is anather
7946 * solution that kernel blocks to send SADB_ACQUIRE message until
7947 * getting something message from IKEd. In later case, to be
7948 * managed with ACQUIRING list.
7950 /* get a entry to check whether sending message or not. */
7951 lck_mtx_lock(sadb_mutex
);
7952 if ((newacq
= key_getacq(saidx
)) != NULL
) {
7953 if (key_blockacq_count
< newacq
->count
) {
7954 /* reset counter and do send message. */
7957 /* increment counter and do nothing. */
7959 lck_mtx_unlock(sadb_mutex
);
7963 /* make new entry for blocking to send SADB_ACQUIRE. */
7964 if ((newacq
= key_newacq(saidx
)) == NULL
) {
7965 lck_mtx_unlock(sadb_mutex
);
7969 /* add to acqtree */
7970 LIST_INSERT_HEAD(&acqtree
, newacq
, chain
);
7971 key_start_timehandler();
7974 lck_mtx_unlock(sadb_mutex
);
7977 seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
7979 m
= key_setsadbmsg(SADB_ACQUIRE
, 0, satype
, seq
, 0, 0);
7986 /* set sadb_address for saidx's. */
7987 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
7988 (struct sockaddr
*)&saidx
->src
, FULLMASK
, IPSEC_ULPROTO_ANY
);
7995 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
7996 (struct sockaddr
*)&saidx
->dst
, FULLMASK
, IPSEC_ULPROTO_ANY
);
8003 /* XXX proxy address (optional) */
8005 /* set sadb_x_policy */
8007 m
= key_setsadbxpolicy(sp
->policy
, sp
->spidx
.dir
, sp
->id
);
8015 /* XXX identity (optional) */
8017 if (idexttype
&& fqdn
) {
8018 /* create identity extension (FQDN) */
8019 struct sadb_ident
*id
;
8022 fqdnlen
= strlen(fqdn
) + 1; /* +1 for terminating-NUL */
8023 id
= (struct sadb_ident
*)p
;
8024 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
8025 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
8026 id
->sadb_ident_exttype
= idexttype
;
8027 id
->sadb_ident_type
= SADB_IDENTTYPE_FQDN
;
8028 bcopy(fqdn
, id
+ 1, fqdnlen
);
8029 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(fqdnlen
);
8033 /* create identity extension (USERFQDN) */
8034 struct sadb_ident
*id
;
8038 /* +1 for terminating-NUL */
8039 userfqdnlen
= strlen(userfqdn
) + 1;
8042 id
= (struct sadb_ident
*)p
;
8043 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
8044 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
8045 id
->sadb_ident_exttype
= idexttype
;
8046 id
->sadb_ident_type
= SADB_IDENTTYPE_USERFQDN
;
8047 /* XXX is it correct? */
8048 if (curproc
&& curproc
->p_cred
)
8049 id
->sadb_ident_id
= curproc
->p_cred
->p_ruid
;
8050 if (userfqdn
&& userfqdnlen
)
8051 bcopy(userfqdn
, id
+ 1, userfqdnlen
);
8052 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(userfqdnlen
);
8056 /* XXX sensitivity (optional) */
8058 /* create proposal/combination extension */
8059 m
= key_getprop(saidx
);
8062 * spec conformant: always attach proposal/combination extension,
8063 * the problem is that we have no way to attach it for ipcomp,
8064 * due to the way sadb_comb is declared in RFC2367.
8073 * outside of spec; make proposal/combination extension optional.
8079 if ((result
->m_flags
& M_PKTHDR
) == 0) {
8084 if (result
->m_len
< sizeof(struct sadb_msg
)) {
8085 result
= m_pullup(result
, sizeof(struct sadb_msg
));
8086 if (result
== NULL
) {
8092 result
->m_pkthdr
.len
= 0;
8093 for (m
= result
; m
; m
= m
->m_next
)
8094 result
->m_pkthdr
.len
+= m
->m_len
;
8096 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
8097 PFKEY_UNIT64(result
->m_pkthdr
.len
);
8099 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
8107 #ifndef IPSEC_NONBLOCK_ACQUIRE
8108 static struct secacq
*
8110 struct secasindex
*saidx
)
8112 struct secacq
*newacq
;
8116 KMALLOC_NOWAIT(newacq
, struct secacq
*, sizeof(struct secacq
));
8117 if (newacq
== NULL
) {
8118 lck_mtx_unlock(sadb_mutex
);
8119 KMALLOC_WAIT(newacq
, struct secacq
*, sizeof(struct secacq
));
8120 lck_mtx_lock(sadb_mutex
);
8121 if (newacq
== NULL
) {
8122 ipseclog((LOG_DEBUG
, "key_newacq: No more memory.\n"));
8126 bzero(newacq
, sizeof(*newacq
));
8129 bcopy(saidx
, &newacq
->saidx
, sizeof(newacq
->saidx
));
8130 newacq
->seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
);
8132 newacq
->created
= tv
.tv_sec
;
8138 static struct secacq
*
8140 struct secasindex
*saidx
)
8144 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
8146 LIST_FOREACH(acq
, &acqtree
, chain
) {
8147 if (key_cmpsaidx(saidx
, &acq
->saidx
, CMP_EXACTLY
))
8154 static struct secacq
*
8160 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
8162 LIST_FOREACH(acq
, &acqtree
, chain
) {
8163 if (acq
->seq
== seq
)
8171 static struct secspacq
*
8173 struct secpolicyindex
*spidx
)
8175 struct secspacq
*acq
;
8179 KMALLOC_NOWAIT(acq
, struct secspacq
*, sizeof(struct secspacq
));
8181 lck_mtx_unlock(sadb_mutex
);
8182 KMALLOC_WAIT(acq
, struct secspacq
*, sizeof(struct secspacq
));
8183 lck_mtx_lock(sadb_mutex
);
8185 ipseclog((LOG_DEBUG
, "key_newspacq: No more memory.\n"));
8189 bzero(acq
, sizeof(*acq
));
8192 bcopy(spidx
, &acq
->spidx
, sizeof(acq
->spidx
));
8194 acq
->created
= tv
.tv_sec
;
8200 static struct secspacq
*
8202 struct secpolicyindex
*spidx
)
8204 struct secspacq
*acq
;
8206 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
8208 LIST_FOREACH(acq
, &spacqtree
, chain
) {
8209 if (key_cmpspidx_exactly(spidx
, &acq
->spidx
))
8217 * SADB_ACQUIRE processing,
8218 * in first situation, is receiving
8220 * from the ikmpd, and clear sequence of its secasvar entry.
8222 * In second situation, is receiving
8223 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
8224 * from a user land process, and return
8225 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
8228 * m will always be freed.
8234 const struct sadb_msghdr
*mhp
)
8236 const struct sadb_address
*src0
, *dst0
;
8237 ifnet_t ipsec_if
= NULL
;
8238 struct secasindex saidx
;
8239 struct secashead
*sah
;
8245 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
8246 panic("key_acquire2: NULL pointer is passed.\n");
8249 * Error message from KMd.
8250 * We assume that if error was occurred in IKEd, the length of PFKEY
8251 * message is equal to the size of sadb_msg structure.
8252 * We do not raise error even if error occurred in this function.
8254 lck_mtx_lock(sadb_mutex
);
8256 if (mhp
->msg
->sadb_msg_len
== PFKEY_UNIT64(sizeof(struct sadb_msg
))) {
8257 #ifndef IPSEC_NONBLOCK_ACQUIRE
8261 /* check sequence number */
8262 if (mhp
->msg
->sadb_msg_seq
== 0) {
8263 lck_mtx_unlock(sadb_mutex
);
8264 ipseclog((LOG_DEBUG
, "key_acquire2: must specify sequence number.\n"));
8269 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) == NULL
) {
8271 * the specified larval SA is already gone, or we got
8272 * a bogus sequence number. we can silently ignore it.
8274 lck_mtx_unlock(sadb_mutex
);
8279 /* reset acq counter in order to deletion by timehander. */
8281 acq
->created
= tv
.tv_sec
;
8284 lck_mtx_unlock(sadb_mutex
);
8290 * This message is from user land.
8293 /* map satype to proto */
8294 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
8295 lck_mtx_unlock(sadb_mutex
);
8296 ipseclog((LOG_DEBUG
, "key_acquire2: invalid satype is passed.\n"));
8297 return key_senderror(so
, m
, EINVAL
);
8300 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
8301 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
8302 mhp
->ext
[SADB_EXT_PROPOSAL
] == NULL
) {
8304 lck_mtx_unlock(sadb_mutex
);
8305 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
8306 return key_senderror(so
, m
, EINVAL
);
8308 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
8309 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
8310 mhp
->extlen
[SADB_EXT_PROPOSAL
] < sizeof(struct sadb_prop
)) {
8312 lck_mtx_unlock(sadb_mutex
);
8313 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
8314 return key_senderror(so
, m
, EINVAL
);
8317 src0
= (const struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
8318 dst0
= (const struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
8319 ipsec_if
= key_get_ipsec_if_from_message(mhp
);
8321 /* XXX boundary check against sa_len */
8323 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, ipsec_if
? ipsec_if
->if_index
: 0, &saidx
);
8325 /* get a SA index */
8326 LIST_FOREACH(sah
, &sahtree
, chain
) {
8327 if (sah
->state
== SADB_SASTATE_DEAD
)
8329 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_MODE
| CMP_REQID
))
8333 lck_mtx_unlock(sadb_mutex
);
8334 ipseclog((LOG_DEBUG
, "key_acquire2: a SA exists already.\n"));
8335 return key_senderror(so
, m
, EEXIST
);
8337 lck_mtx_unlock(sadb_mutex
);
8338 error
= key_acquire(&saidx
, NULL
);
8340 ipseclog((LOG_DEBUG
, "key_acquire2: error %d returned "
8341 "from key_acquire.\n", mhp
->msg
->sadb_msg_errno
));
8342 return key_senderror(so
, m
, error
);
8345 return key_sendup_mbuf(so
, m
, KEY_SENDUP_REGISTERED
);
8349 * SADB_REGISTER processing.
8350 * If SATYPE_UNSPEC has been passed as satype, only return sadb_supported.
8353 * from the ikmpd, and register a socket to send PF_KEY messages,
8357 * If socket is detached, must free from regnode.
8359 * m will always be freed.
8365 const struct sadb_msghdr
*mhp
)
8367 struct secreg
*reg
, *newreg
= 0;
8370 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
8371 panic("key_register: NULL pointer is passed.\n");
8373 /* check for invalid register message */
8374 if (mhp
->msg
->sadb_msg_satype
>= sizeof(regtree
)/sizeof(regtree
[0]))
8375 return key_senderror(so
, m
, EINVAL
);
8377 /* When SATYPE_UNSPEC is specified, only return sadb_supported. */
8378 if (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
)
8381 /* create regnode */
8382 KMALLOC_WAIT(newreg
, struct secreg
*, sizeof(*newreg
));
8383 if (newreg
== NULL
) {
8384 ipseclog((LOG_DEBUG
, "key_register: No more memory.\n"));
8385 return key_senderror(so
, m
, ENOBUFS
);
8387 bzero((caddr_t
)newreg
, sizeof(*newreg
));
8389 lck_mtx_lock(sadb_mutex
);
8390 /* check whether existing or not */
8391 LIST_FOREACH(reg
, ®tree
[mhp
->msg
->sadb_msg_satype
], chain
) {
8392 if (reg
->so
== so
) {
8393 lck_mtx_unlock(sadb_mutex
);
8394 ipseclog((LOG_DEBUG
, "key_register: socket exists already.\n"));
8396 return key_senderror(so
, m
, EEXIST
);
8402 ((struct keycb
*)sotorawcb(so
))->kp_registered
++;
8403 socket_unlock(so
, 1);
8405 /* add regnode to regtree. */
8406 LIST_INSERT_HEAD(®tree
[mhp
->msg
->sadb_msg_satype
], newreg
, chain
);
8407 lck_mtx_unlock(sadb_mutex
);
8411 struct sadb_msg
*newmsg
;
8412 struct sadb_supported
*sup
;
8413 u_int len
, alen
, elen
;
8416 struct sadb_alg
*alg
;
8418 /* create new sadb_msg to reply. */
8420 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
8421 if (ah_algorithm_lookup(i
))
8422 alen
+= sizeof(struct sadb_alg
);
8425 alen
+= sizeof(struct sadb_supported
);
8428 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
8429 if (esp_algorithm_lookup(i
))
8430 elen
+= sizeof(struct sadb_alg
);
8433 elen
+= sizeof(struct sadb_supported
);
8436 len
= sizeof(struct sadb_msg
) + alen
+ elen
;
8439 return key_senderror(so
, m
, ENOBUFS
);
8441 MGETHDR(n
, M_WAITOK
, MT_DATA
);
8442 if (n
&& len
> MHLEN
) {
8443 MCLGET(n
, M_WAITOK
);
8444 if ((n
->m_flags
& M_EXT
) == 0) {
8450 return key_senderror(so
, m
, ENOBUFS
);
8452 n
->m_pkthdr
.len
= n
->m_len
= len
;
8456 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
8457 newmsg
= mtod(n
, struct sadb_msg
*);
8458 newmsg
->sadb_msg_errno
= 0;
8459 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
8460 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
8462 /* for authentication algorithm */
8464 sup
= (struct sadb_supported
*)(void *)(mtod(n
, caddr_t
) + off
);
8465 sup
->sadb_supported_len
= PFKEY_UNIT64(alen
);
8466 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
8467 off
+= PFKEY_ALIGN8(sizeof(*sup
));
8469 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
8470 const struct ah_algorithm
*aalgo
;
8472 aalgo
= ah_algorithm_lookup(i
);
8475 alg
= (struct sadb_alg
*)
8476 (void *)(mtod(n
, caddr_t
) + off
);
8477 alg
->sadb_alg_id
= i
;
8478 alg
->sadb_alg_ivlen
= 0;
8479 alg
->sadb_alg_minbits
= aalgo
->keymin
;
8480 alg
->sadb_alg_maxbits
= aalgo
->keymax
;
8481 off
+= PFKEY_ALIGN8(sizeof(*alg
));
8486 /* for encryption algorithm */
8488 sup
= (struct sadb_supported
*)(void *)(mtod(n
, caddr_t
) + off
);
8489 sup
->sadb_supported_len
= PFKEY_UNIT64(elen
);
8490 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
8491 off
+= PFKEY_ALIGN8(sizeof(*sup
));
8493 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
8494 const struct esp_algorithm
*ealgo
;
8496 ealgo
= esp_algorithm_lookup(i
);
8499 alg
= (struct sadb_alg
*)
8500 (void *)(mtod(n
, caddr_t
) + off
);
8501 alg
->sadb_alg_id
= i
;
8502 if (ealgo
&& ealgo
->ivlen
) {
8504 * give NULL to get the value preferred by
8505 * algorithm XXX SADB_X_EXT_DERIV ?
8507 alg
->sadb_alg_ivlen
=
8508 (*ealgo
->ivlen
)(ealgo
, NULL
);
8510 alg
->sadb_alg_ivlen
= 0;
8511 alg
->sadb_alg_minbits
= ealgo
->keymin
;
8512 alg
->sadb_alg_maxbits
= ealgo
->keymax
;
8513 off
+= PFKEY_ALIGN8(sizeof(struct sadb_alg
));
8520 panic("length assumption failed in key_register");
8524 return key_sendup_mbuf(so
, n
, KEY_SENDUP_REGISTERED
);
8529 key_delete_all_for_socket (struct socket
*so
)
8531 struct secashead
*sah
, *nextsah
;
8532 struct secasvar
*sav
, *nextsav
;
8536 for (sah
= LIST_FIRST(&sahtree
);
8539 nextsah
= LIST_NEXT(sah
, chain
);
8540 for (stateidx
= 0; stateidx
< _ARRAYLEN(saorder_state_alive
); stateidx
++) {
8541 state
= saorder_state_any
[stateidx
];
8542 for (sav
= LIST_FIRST(&sah
->savtree
[state
]); sav
!= NULL
; sav
= nextsav
) {
8543 nextsav
= LIST_NEXT(sav
, chain
);
8544 if (sav
->flags2
& SADB_X_EXT_SA2_DELETE_ON_DETACH
&&
8546 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
8547 key_freesav(sav
, KEY_SADB_LOCKED
);
8555 * free secreg entry registered.
8556 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
8567 panic("key_freereg: NULL pointer is passed.\n");
8570 * check whether existing or not.
8571 * check all type of SA, because there is a potential that
8572 * one socket is registered to multiple type of SA.
8574 lck_mtx_lock(sadb_mutex
);
8575 key_delete_all_for_socket(so
);
8576 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
8577 LIST_FOREACH(reg
, ®tree
[i
], chain
) {
8579 && __LIST_CHAINED(reg
)) {
8580 LIST_REMOVE(reg
, chain
);
8586 lck_mtx_unlock(sadb_mutex
);
8591 * SADB_EXPIRE processing
8593 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
8595 * NOTE: We send only soft lifetime extension.
8598 * others : error number
8602 struct secasvar
*sav
)
8605 struct mbuf
*result
= NULL
, *m
;
8608 struct sadb_lifetime
*lt
;
8610 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
8614 panic("key_expire: NULL pointer is passed.\n");
8615 if (sav
->sah
== NULL
)
8616 panic("key_expire: Why was SA index in SA NULL.\n");
8617 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0)
8618 panic("key_expire: invalid proto is passed.\n");
8620 /* set msg header */
8621 m
= key_setsadbmsg(SADB_EXPIRE
, 0, satype
, sav
->seq
, 0, sav
->refcnt
);
8628 /* create SA extension */
8629 m
= key_setsadbsa(sav
);
8636 /* create SA extension */
8637 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
8638 sav
->replay
? sav
->replay
->count
: 0,
8639 sav
->sah
->saidx
.reqid
,
8647 /* create lifetime extension (current and soft) */
8648 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
8649 m
= key_alloc_mbuf(len
);
8650 if (!m
|| m
->m_next
) { /*XXX*/
8656 bzero(mtod(m
, caddr_t
), len
);
8657 lt
= mtod(m
, struct sadb_lifetime
*);
8658 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
8659 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
8660 lt
->sadb_lifetime_allocations
= sav
->lft_c
->sadb_lifetime_allocations
;
8661 lt
->sadb_lifetime_bytes
= sav
->lft_c
->sadb_lifetime_bytes
;
8662 lt
->sadb_lifetime_addtime
= sav
->lft_c
->sadb_lifetime_addtime
;
8663 lt
->sadb_lifetime_usetime
= sav
->lft_c
->sadb_lifetime_usetime
;
8664 lt
= (struct sadb_lifetime
*)(void *)(mtod(m
, caddr_t
) + len
/ 2);
8665 bcopy(sav
->lft_s
, lt
, sizeof(*lt
));
8668 /* set sadb_address for source */
8669 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
8670 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
8671 FULLMASK
, IPSEC_ULPROTO_ANY
);
8678 /* set sadb_address for destination */
8679 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
8680 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
8681 FULLMASK
, IPSEC_ULPROTO_ANY
);
8688 if ((result
->m_flags
& M_PKTHDR
) == 0) {
8693 if (result
->m_len
< sizeof(struct sadb_msg
)) {
8694 result
= m_pullup(result
, sizeof(struct sadb_msg
));
8695 if (result
== NULL
) {
8701 result
->m_pkthdr
.len
= 0;
8702 for (m
= result
; m
; m
= m
->m_next
)
8703 result
->m_pkthdr
.len
+= m
->m_len
;
8705 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
8706 PFKEY_UNIT64(result
->m_pkthdr
.len
);
8708 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
8717 * SADB_FLUSH processing
8720 * from the ikmpd, and free all entries in secastree.
8724 * NOTE: to do is only marking SADB_SASTATE_DEAD.
8726 * m will always be freed.
8732 const struct sadb_msghdr
*mhp
)
8734 struct sadb_msg
*newmsg
;
8735 struct secashead
*sah
, *nextsah
;
8736 struct secasvar
*sav
, *nextsav
;
8742 if (so
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
8743 panic("key_flush: NULL pointer is passed.\n");
8745 /* map satype to proto */
8746 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
8747 ipseclog((LOG_DEBUG
, "key_flush: invalid satype is passed.\n"));
8748 return key_senderror(so
, m
, EINVAL
);
8751 lck_mtx_lock(sadb_mutex
);
8753 /* no SATYPE specified, i.e. flushing all SA. */
8754 for (sah
= LIST_FIRST(&sahtree
);
8757 nextsah
= LIST_NEXT(sah
, chain
);
8759 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
8760 && proto
!= sah
->saidx
.proto
)
8764 stateidx
< _ARRAYLEN(saorder_state_alive
);
8766 state
= saorder_state_any
[stateidx
];
8767 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
8771 nextsav
= LIST_NEXT(sav
, chain
);
8773 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
8774 key_freesav(sav
, KEY_SADB_LOCKED
);
8778 sah
->state
= SADB_SASTATE_DEAD
;
8780 lck_mtx_unlock(sadb_mutex
);
8782 if (m
->m_len
< sizeof(struct sadb_msg
) ||
8783 sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
8784 ipseclog((LOG_DEBUG
, "key_flush: No more memory.\n"));
8785 return key_senderror(so
, m
, ENOBUFS
);
8791 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct sadb_msg
);
8792 newmsg
= mtod(m
, struct sadb_msg
*);
8793 newmsg
->sadb_msg_errno
= 0;
8794 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
8796 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
8800 * SADB_DUMP processing
8801 * dump all entries including status of DEAD in SAD.
8804 * from the ikmpd, and dump all secasvar leaves
8809 * m will always be freed.
8812 struct sav_dump_elem
{
8813 struct secasvar
*sav
;
8821 const struct sadb_msghdr
*mhp
)
8823 struct secashead
*sah
;
8824 struct secasvar
*sav
;
8825 struct sav_dump_elem
*savbuf
= NULL
, *elem_ptr
;
8830 int cnt
= 0, cnt2
, bufcount
;
8834 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
8837 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
8838 panic("key_dump: NULL pointer is passed.\n");
8840 /* map satype to proto */
8841 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
8842 ipseclog((LOG_DEBUG
, "key_dump: invalid satype is passed.\n"));
8843 return key_senderror(so
, m
, EINVAL
);
8846 if ((bufcount
= ipsec_sav_count
) <= 0) {
8850 bufcount
+= 512; /* extra */
8851 KMALLOC_WAIT(savbuf
, struct sav_dump_elem
*, bufcount
* sizeof(struct sav_dump_elem
));
8852 if (savbuf
== NULL
) {
8853 ipseclog((LOG_DEBUG
, "key_dump: No more memory.\n"));
8858 /* count sav entries to be sent to the userland. */
8859 lck_mtx_lock(sadb_mutex
);
8861 LIST_FOREACH(sah
, &sahtree
, chain
) {
8862 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
8863 && proto
!= sah
->saidx
.proto
)
8866 /* map proto to satype */
8867 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
8868 lck_mtx_unlock(sadb_mutex
);
8869 ipseclog((LOG_DEBUG
, "key_dump: there was invalid proto in SAD.\n"));
8875 stateidx
< _ARRAYLEN(saorder_state_any
);
8877 state
= saorder_state_any
[stateidx
];
8878 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
8879 if (cnt
== bufcount
)
8880 break; /* out of buffer space */
8881 elem_ptr
->sav
= sav
;
8882 elem_ptr
->satype
= satype
;
8889 lck_mtx_unlock(sadb_mutex
);
8896 /* send this to the userland, one at a time. */
8900 n
= key_setdumpsa(elem_ptr
->sav
, SADB_DUMP
, elem_ptr
->satype
,
8901 --cnt2
, mhp
->msg
->sadb_msg_pid
);
8908 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
8916 lck_mtx_lock(sadb_mutex
);
8918 key_freesav((elem_ptr
++)->sav
, KEY_SADB_LOCKED
);
8919 lck_mtx_unlock(sadb_mutex
);
8925 return key_senderror(so
, m
, error
);
8932 * SADB_X_PROMISC processing
8934 * m will always be freed.
8940 const struct sadb_msghdr
*mhp
)
8945 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
8946 panic("key_promisc: NULL pointer is passed.\n");
8948 olen
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
8950 if (olen
< sizeof(struct sadb_msg
)) {
8952 return key_senderror(so
, m
, EINVAL
);
8957 } else if (olen
== sizeof(struct sadb_msg
)) {
8958 /* enable/disable promisc mode */
8962 if ((kp
= (struct keycb
*)sotorawcb(so
)) == NULL
)
8963 return key_senderror(so
, m
, EINVAL
);
8964 mhp
->msg
->sadb_msg_errno
= 0;
8965 switch (mhp
->msg
->sadb_msg_satype
) {
8968 kp
->kp_promisc
= mhp
->msg
->sadb_msg_satype
;
8971 socket_unlock(so
, 1);
8972 return key_senderror(so
, m
, EINVAL
);
8974 socket_unlock(so
, 1);
8976 /* send the original message back to everyone */
8977 mhp
->msg
->sadb_msg_errno
= 0;
8978 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
8980 /* send packet as is */
8982 m_adj(m
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)));
8984 /* TODO: if sadb_msg_seq is specified, send to specific pid */
8985 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
8989 static int (*key_typesw
[])(struct socket
*, struct mbuf
*,
8990 const struct sadb_msghdr
*) = {
8991 NULL
, /* SADB_RESERVED */
8992 key_getspi
, /* SADB_GETSPI */
8993 key_update
, /* SADB_UPDATE */
8994 key_add
, /* SADB_ADD */
8995 key_delete
, /* SADB_DELETE */
8996 key_get
, /* SADB_GET */
8997 key_acquire2
, /* SADB_ACQUIRE */
8998 key_register
, /* SADB_REGISTER */
8999 NULL
, /* SADB_EXPIRE */
9000 key_flush
, /* SADB_FLUSH */
9001 key_dump
, /* SADB_DUMP */
9002 key_promisc
, /* SADB_X_PROMISC */
9003 NULL
, /* SADB_X_PCHANGE */
9004 key_spdadd
, /* SADB_X_SPDUPDATE */
9005 key_spdadd
, /* SADB_X_SPDADD */
9006 key_spddelete
, /* SADB_X_SPDDELETE */
9007 key_spdget
, /* SADB_X_SPDGET */
9008 NULL
, /* SADB_X_SPDACQUIRE */
9009 key_spddump
, /* SADB_X_SPDDUMP */
9010 key_spdflush
, /* SADB_X_SPDFLUSH */
9011 key_spdadd
, /* SADB_X_SPDSETIDX */
9012 NULL
, /* SADB_X_SPDEXPIRE */
9013 key_spddelete2
, /* SADB_X_SPDDELETE2 */
9014 key_getsastat
, /* SADB_GETSASTAT */
9015 key_spdenable
, /* SADB_X_SPDENABLE */
9016 key_spddisable
, /* SADB_X_SPDDISABLE */
9020 * parse sadb_msg buffer to process PFKEYv2,
9021 * and create a data to response if needed.
9022 * I think to be dealed with mbuf directly.
9024 * msgp : pointer to pointer to a received buffer pulluped.
9025 * This is rewrited to response.
9026 * so : pointer to socket.
9028 * length for buffer to send to user process.
9035 struct sadb_msg
*msg
;
9036 struct sadb_msghdr mh
;
9041 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
9044 if (m
== NULL
|| so
== NULL
)
9045 panic("key_parse: NULL pointer is passed.\n");
9047 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
9048 KEYDEBUG(KEYDEBUG_KEY_DUMP
,
9049 ipseclog((LOG_DEBUG
, "key_parse: passed sadb_msg\n"));
9053 if (m
->m_len
< sizeof(struct sadb_msg
)) {
9054 m
= m_pullup(m
, sizeof(struct sadb_msg
));
9058 msg
= mtod(m
, struct sadb_msg
*);
9059 orglen
= PFKEY_UNUNIT64(msg
->sadb_msg_len
);
9060 target
= KEY_SENDUP_ONE
;
9062 if ((m
->m_flags
& M_PKTHDR
) == 0 ||
9063 m
->m_pkthdr
.len
!= m
->m_pkthdr
.len
) {
9064 ipseclog((LOG_DEBUG
, "key_parse: invalid message length.\n"));
9065 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
9070 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
9071 ipseclog((LOG_DEBUG
,
9072 "key_parse: PF_KEY version %u is mismatched.\n",
9073 msg
->sadb_msg_version
));
9074 PFKEY_STAT_INCREMENT(pfkeystat
.out_invver
);
9079 if (msg
->sadb_msg_type
> SADB_MAX
) {
9080 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
9081 msg
->sadb_msg_type
));
9082 PFKEY_STAT_INCREMENT(pfkeystat
.out_invmsgtype
);
9087 /* for old-fashioned code - should be nuked */
9088 if (m
->m_pkthdr
.len
> MCLBYTES
) {
9095 MGETHDR(n
, M_WAITOK
, MT_DATA
);
9096 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
9097 MCLGET(n
, M_WAITOK
);
9098 if ((n
->m_flags
& M_EXT
) == 0) {
9107 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
9108 n
->m_pkthdr
.len
= n
->m_len
= m
->m_pkthdr
.len
;
9114 /* align the mbuf chain so that extensions are in contiguous region. */
9115 error
= key_align(m
, &mh
);
9119 if (m
->m_next
) { /*XXX*/
9127 switch (msg
->sadb_msg_satype
) {
9128 case SADB_SATYPE_UNSPEC
:
9129 switch (msg
->sadb_msg_type
) {
9137 ipseclog((LOG_DEBUG
, "key_parse: must specify satype "
9138 "when msg type=%u.\n", msg
->sadb_msg_type
));
9139 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
9144 case SADB_SATYPE_AH
:
9145 case SADB_SATYPE_ESP
:
9146 case SADB_X_SATYPE_IPCOMP
:
9147 switch (msg
->sadb_msg_type
) {
9149 case SADB_X_SPDDELETE
:
9151 case SADB_X_SPDDUMP
:
9152 case SADB_X_SPDFLUSH
:
9153 case SADB_X_SPDSETIDX
:
9154 case SADB_X_SPDUPDATE
:
9155 case SADB_X_SPDDELETE2
:
9156 case SADB_X_SPDENABLE
:
9157 case SADB_X_SPDDISABLE
:
9158 ipseclog((LOG_DEBUG
, "key_parse: illegal satype=%u\n",
9159 msg
->sadb_msg_type
));
9160 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
9165 case SADB_SATYPE_RSVP
:
9166 case SADB_SATYPE_OSPFV2
:
9167 case SADB_SATYPE_RIPV2
:
9168 case SADB_SATYPE_MIP
:
9169 ipseclog((LOG_DEBUG
, "key_parse: type %u isn't supported.\n",
9170 msg
->sadb_msg_satype
));
9171 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
9174 case 1: /* XXX: What does it do? */
9175 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
9179 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
9180 msg
->sadb_msg_satype
));
9181 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
9186 /* check field of upper layer protocol and address family */
9187 if (mh
.ext
[SADB_EXT_ADDRESS_SRC
] != NULL
9188 && mh
.ext
[SADB_EXT_ADDRESS_DST
] != NULL
) {
9189 struct sadb_address
*src0
, *dst0
;
9192 src0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_SRC
]);
9193 dst0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_DST
]);
9195 /* check upper layer protocol */
9196 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
9197 ipseclog((LOG_DEBUG
, "key_parse: upper layer protocol mismatched.\n"));
9198 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9204 if (PFKEY_ADDR_SADDR(src0
)->sa_family
!=
9205 PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
9206 ipseclog((LOG_DEBUG
, "key_parse: address family mismatched.\n"));
9207 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9211 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
9212 PFKEY_ADDR_SADDR(dst0
)->sa_len
) {
9213 ipseclog((LOG_DEBUG
,
9214 "key_parse: address struct size mismatched.\n"));
9215 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9220 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
9222 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
9223 sizeof(struct sockaddr_in
)) {
9224 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9230 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
9231 sizeof(struct sockaddr_in6
)) {
9232 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9238 ipseclog((LOG_DEBUG
,
9239 "key_parse: unsupported address family.\n"));
9240 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9241 error
= EAFNOSUPPORT
;
9245 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
9247 plen
= sizeof(struct in_addr
) << 3;
9250 plen
= sizeof(struct in6_addr
) << 3;
9253 plen
= 0; /*fool gcc*/
9257 /* check max prefix length */
9258 if (src0
->sadb_address_prefixlen
> plen
||
9259 dst0
->sadb_address_prefixlen
> plen
) {
9260 ipseclog((LOG_DEBUG
,
9261 "key_parse: illegal prefixlen.\n"));
9262 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
9268 * prefixlen == 0 is valid because there can be a case when
9269 * all addresses are matched.
9273 if (msg
->sadb_msg_type
>= sizeof(key_typesw
)/sizeof(key_typesw
[0]) ||
9274 key_typesw
[msg
->sadb_msg_type
] == NULL
) {
9275 PFKEY_STAT_INCREMENT(pfkeystat
.out_invmsgtype
);
9280 return (*key_typesw
[msg
->sadb_msg_type
])(so
, m
, &mh
);
9283 msg
->sadb_msg_errno
= error
;
9284 return key_sendup_mbuf(so
, m
, target
);
9293 struct sadb_msg
*msg
;
9295 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
9297 if (m
->m_len
< sizeof(struct sadb_msg
))
9298 panic("invalid mbuf passed to key_senderror");
9300 msg
= mtod(m
, struct sadb_msg
*);
9301 msg
->sadb_msg_errno
= code
;
9302 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
9306 * set the pointer to each header into message buffer.
9307 * m will be freed on error.
9308 * XXX larger-than-MCLBYTES extension?
9313 struct sadb_msghdr
*mhp
)
9316 struct sadb_ext
*ext
;
9322 if (m
== NULL
|| mhp
== NULL
)
9323 panic("key_align: NULL pointer is passed.\n");
9324 if (m
->m_len
< sizeof(struct sadb_msg
))
9325 panic("invalid mbuf passed to key_align");
9328 bzero(mhp
, sizeof(*mhp
));
9330 mhp
->msg
= mtod(m
, struct sadb_msg
*);
9331 mhp
->ext
[0] = (struct sadb_ext
*)mhp
->msg
; /*XXX backward compat */
9333 end
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
9334 extlen
= end
; /*just in case extlen is not updated*/
9335 for (off
= sizeof(struct sadb_msg
); off
< end
; off
+= extlen
) {
9336 n
= m_pulldown(m
, off
, sizeof(struct sadb_ext
), &toff
);
9338 /* m is already freed */
9341 ext
= (struct sadb_ext
*)(void *)(mtod(n
, caddr_t
) + toff
);
9344 switch (ext
->sadb_ext_type
) {
9346 case SADB_EXT_ADDRESS_SRC
:
9347 case SADB_EXT_ADDRESS_DST
:
9348 case SADB_EXT_ADDRESS_PROXY
:
9349 case SADB_EXT_LIFETIME_CURRENT
:
9350 case SADB_EXT_LIFETIME_HARD
:
9351 case SADB_EXT_LIFETIME_SOFT
:
9352 case SADB_EXT_KEY_AUTH
:
9353 case SADB_EXT_KEY_ENCRYPT
:
9354 case SADB_EXT_IDENTITY_SRC
:
9355 case SADB_EXT_IDENTITY_DST
:
9356 case SADB_EXT_SENSITIVITY
:
9357 case SADB_EXT_PROPOSAL
:
9358 case SADB_EXT_SUPPORTED_AUTH
:
9359 case SADB_EXT_SUPPORTED_ENCRYPT
:
9360 case SADB_EXT_SPIRANGE
:
9361 case SADB_X_EXT_POLICY
:
9362 case SADB_X_EXT_SA2
:
9363 case SADB_EXT_SESSION_ID
:
9364 case SADB_EXT_SASTAT
:
9365 case SADB_X_EXT_IPSECIF
:
9366 case SADB_X_EXT_ADDR_RANGE_SRC_START
:
9367 case SADB_X_EXT_ADDR_RANGE_SRC_END
:
9368 case SADB_X_EXT_ADDR_RANGE_DST_START
:
9369 case SADB_X_EXT_ADDR_RANGE_DST_END
:
9370 /* duplicate check */
9372 * XXX Are there duplication payloads of either
9373 * KEY_AUTH or KEY_ENCRYPT ?
9375 if (mhp
->ext
[ext
->sadb_ext_type
] != NULL
) {
9376 ipseclog((LOG_DEBUG
,
9377 "key_align: duplicate ext_type %u "
9378 "is passed.\n", ext
->sadb_ext_type
));
9380 PFKEY_STAT_INCREMENT(pfkeystat
.out_dupext
);
9385 ipseclog((LOG_DEBUG
,
9386 "key_align: invalid ext_type %u is passed.\n",
9387 ext
->sadb_ext_type
));
9389 PFKEY_STAT_INCREMENT(pfkeystat
.out_invexttype
);
9393 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
9395 if (key_validate_ext(ext
, extlen
)) {
9397 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
9401 n
= m_pulldown(m
, off
, extlen
, &toff
);
9403 /* m is already freed */
9406 ext
= (struct sadb_ext
*)(void *)(mtod(n
, caddr_t
) + toff
);
9408 mhp
->ext
[ext
->sadb_ext_type
] = ext
;
9409 mhp
->extoff
[ext
->sadb_ext_type
] = off
;
9410 mhp
->extlen
[ext
->sadb_ext_type
] = extlen
;
9415 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
9424 const struct sadb_ext
*ext
,
9427 struct sockaddr
*sa
;
9428 enum { NONE
, ADDR
} checktype
= NONE
;
9430 const int sal
= offsetof(struct sockaddr
, sa_len
) + sizeof(sa
->sa_len
);
9432 if (len
!= PFKEY_UNUNIT64(ext
->sadb_ext_len
))
9435 /* if it does not match minimum/maximum length, bail */
9436 if (ext
->sadb_ext_type
>= sizeof(minsize
) / sizeof(minsize
[0]) ||
9437 ext
->sadb_ext_type
>= sizeof(maxsize
) / sizeof(maxsize
[0]))
9439 if (!minsize
[ext
->sadb_ext_type
] || len
< minsize
[ext
->sadb_ext_type
])
9441 if (maxsize
[ext
->sadb_ext_type
] && len
> maxsize
[ext
->sadb_ext_type
])
9444 /* more checks based on sadb_ext_type XXX need more */
9445 switch (ext
->sadb_ext_type
) {
9446 case SADB_EXT_ADDRESS_SRC
:
9447 case SADB_EXT_ADDRESS_DST
:
9448 case SADB_EXT_ADDRESS_PROXY
:
9449 case SADB_X_EXT_ADDR_RANGE_SRC_START
:
9450 case SADB_X_EXT_ADDR_RANGE_SRC_END
:
9451 case SADB_X_EXT_ADDR_RANGE_DST_START
:
9452 case SADB_X_EXT_ADDR_RANGE_DST_END
:
9453 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_address
));
9456 case SADB_EXT_IDENTITY_SRC
:
9457 case SADB_EXT_IDENTITY_DST
:
9458 if (((struct sadb_ident
*)(uintptr_t)(size_t)ext
)->
9459 sadb_ident_type
== SADB_X_IDENTTYPE_ADDR
) {
9460 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_ident
));
9470 switch (checktype
) {
9474 sa
= (struct sockaddr
*)((caddr_t
)(uintptr_t)ext
+ baselen
);
9476 if (len
< baselen
+ sal
)
9478 if (baselen
+ PFKEY_ALIGN8(sa
->sa_len
) != len
)
9487 * XXX: maybe This function is called after INBOUND IPsec processing.
9489 * Special check for tunnel-mode packets.
9490 * We must make some checks for consistency between inner and outer IP header.
9492 * xxx more checks to be provided
9495 key_checktunnelsanity(
9496 struct secasvar
*sav
,
9497 __unused u_int family
,
9498 __unused caddr_t src
,
9499 __unused caddr_t dst
)
9503 if (sav
->sah
== NULL
)
9504 panic("sav->sah == NULL at key_checktunnelsanity");
9506 /* XXX: check inner IP header */
9511 /* record data transfer on SA, and update timestamps */
9514 struct secasvar
*sav
,
9520 panic("key_sa_recordxfer called with sav == NULL");
9522 panic("key_sa_recordxfer called with m == NULL");
9526 lck_mtx_lock(sadb_mutex
);
9528 * XXX Currently, there is a difference of bytes size
9529 * between inbound and outbound processing.
9531 sav
->lft_c
->sadb_lifetime_bytes
+= m
->m_pkthdr
.len
;
9532 /* to check bytes lifetime is done in key_timehandler(). */
9535 * We use the number of packets as the unit of
9536 * sadb_lifetime_allocations. We increment the variable
9537 * whenever {esp,ah}_{in,out}put is called.
9539 sav
->lft_c
->sadb_lifetime_allocations
++;
9540 /* XXX check for expires? */
9543 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
9544 * in seconds. HARD and SOFT lifetime are measured by the time
9545 * difference (again in seconds) from sadb_lifetime_usetime.
9549 * -----+-----+--------+---> t
9550 * <--------------> HARD
9556 sav
->lft_c
->sadb_lifetime_usetime
= tv
.tv_sec
;
9557 /* XXX check for expires? */
9559 lck_mtx_unlock(sadb_mutex
);
9567 struct sockaddr
*dst
)
9569 struct secashead
*sah
;
9572 lck_mtx_lock(sadb_mutex
);
9573 LIST_FOREACH(sah
, &sahtree
, chain
) {
9574 ro
= &sah
->sa_route
;
9575 if (ro
->ro_rt
&& dst
->sa_len
== ro
->ro_dst
.sa_len
9576 && bcmp(dst
, &ro
->ro_dst
, dst
->sa_len
) == 0) {
9580 lck_mtx_unlock(sadb_mutex
);
9587 struct secasvar
*sav
,
9592 panic("key_sa_chgstate called with sav == NULL");
9594 if (sav
->state
== state
)
9597 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
9599 if (__LIST_CHAINED(sav
))
9600 LIST_REMOVE(sav
, chain
);
9603 LIST_INSERT_HEAD(&sav
->sah
->savtree
[state
], sav
, chain
);
9609 struct secasvar
*sav
)
9611 lck_mtx_lock(sadb_mutex
);
9613 panic("key_sa_stir_iv called with sav == NULL");
9614 key_randomfill(sav
->iv
, sav
->ivlen
);
9615 lck_mtx_unlock(sadb_mutex
);
9619 static struct mbuf
*
9623 struct mbuf
*m
= NULL
, *n
;
9628 MGET(n
, M_DONTWAIT
, MT_DATA
);
9629 if (n
&& len
> MLEN
)
9630 MCLGET(n
, M_DONTWAIT
);
9638 n
->m_len
= M_TRAILINGSPACE(n
);
9639 /* use the bottom of mbuf, hoping we can prepend afterwards */
9640 if (n
->m_len
> len
) {
9641 t
= (n
->m_len
- len
) & ~(sizeof(long) - 1);
9657 static struct mbuf
*
9658 key_setdumpsastats (u_int32_t dir
,
9659 struct sastat
*stats
,
9660 u_int32_t max_stats
,
9661 u_int64_t session_ids
[],
9665 struct mbuf
*result
= NULL
, *m
= NULL
;
9667 m
= key_setsadbmsg(SADB_GETSASTAT
, 0, 0, seq
, pid
, 0);
9673 m
= key_setsadbsession_id(session_ids
);
9679 m
= key_setsadbsastat(dir
,
9687 if ((result
->m_flags
& M_PKTHDR
) == 0) {
9691 if (result
->m_len
< sizeof(struct sadb_msg
)) {
9692 result
= m_pullup(result
, sizeof(struct sadb_msg
));
9693 if (result
== NULL
) {
9698 result
->m_pkthdr
.len
= 0;
9699 for (m
= result
; m
; m
= m
->m_next
) {
9700 result
->m_pkthdr
.len
+= m
->m_len
;
9703 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
9704 PFKEY_UNIT64(result
->m_pkthdr
.len
);
9716 * SADB_GETSASTAT processing
9717 * dump all stats for matching entries in SAD.
9719 * m will always be freed.
9723 key_getsastat (struct socket
*so
,
9725 const struct sadb_msghdr
*mhp
)
9727 struct sadb_session_id
*session_id
;
9728 u_int32_t bufsize
, arg_count
, res_count
;
9729 struct sadb_sastat
*sa_stats_arg
;
9730 struct sastat
*sa_stats_sav
= NULL
;
9735 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
9736 panic("%s: NULL pointer is passed.\n", __FUNCTION__
);
9738 if (mhp
->ext
[SADB_EXT_SESSION_ID
] == NULL
) {
9739 printf("%s: invalid message is passed. missing session-id.\n", __FUNCTION__
);
9740 return key_senderror(so
, m
, EINVAL
);
9742 if (mhp
->extlen
[SADB_EXT_SESSION_ID
] < sizeof(struct sadb_session_id
)) {
9743 printf("%s: invalid message is passed. short session-id.\n", __FUNCTION__
);
9744 return key_senderror(so
, m
, EINVAL
);
9746 if (mhp
->ext
[SADB_EXT_SASTAT
] == NULL
) {
9747 printf("%s: invalid message is passed. missing stat args.\n", __FUNCTION__
);
9748 return key_senderror(so
, m
, EINVAL
);
9750 if (mhp
->extlen
[SADB_EXT_SASTAT
] < sizeof(*sa_stats_arg
)) {
9751 printf("%s: invalid message is passed. short stat args.\n", __FUNCTION__
);
9752 return key_senderror(so
, m
, EINVAL
);
9755 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
9757 // exit early if there are no active SAs
9758 if (ipsec_sav_count
<= 0) {
9759 printf("%s: No active SAs.\n", __FUNCTION__
);
9763 bufsize
= (ipsec_sav_count
+ 1) * sizeof(*sa_stats_sav
);
9765 KMALLOC_WAIT(sa_stats_sav
, __typeof__(sa_stats_sav
), bufsize
);
9766 if (sa_stats_sav
== NULL
) {
9767 printf("%s: No more memory.\n", __FUNCTION__
);
9771 bzero(sa_stats_sav
, bufsize
);
9773 sa_stats_arg
= (__typeof__(sa_stats_arg
))
9774 (void *)mhp
->ext
[SADB_EXT_SASTAT
];
9775 arg_count
= sa_stats_arg
->sadb_sastat_list_len
;
9776 // exit early if there are no requested SAs
9777 if (arg_count
== 0) {
9778 printf("%s: No SAs requested.\n", __FUNCTION__
);
9784 if (key_getsastatbyspi((struct sastat
*)(sa_stats_arg
+ 1),
9788 printf("%s: Error finding SAs.\n", __FUNCTION__
);
9793 printf("%s: No SAs found.\n", __FUNCTION__
);
9798 session_id
= (__typeof__(session_id
))
9799 (void *)mhp
->ext
[SADB_EXT_SESSION_ID
];
9801 /* send this to the userland. */
9802 n
= key_setdumpsastats(sa_stats_arg
->sadb_sastat_dir
,
9805 session_id
->sadb_session_id_v
,
9806 mhp
->msg
->sadb_msg_seq
,
9807 mhp
->msg
->sadb_msg_pid
);
9809 printf("%s: No bufs to dump stats.\n", __FUNCTION__
);
9814 key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
9817 KFREE(sa_stats_sav
);
9821 return key_senderror(so
, m
, error
);
9828 key_update_natt_keepalive_timestamp (struct secasvar
*sav_sent
,
9829 struct secasvar
*sav_update
)
9831 struct secasindex saidx_swap_sent_addr
;
9833 // exit early if two SAs are identical, or if sav_update is current
9834 if (sav_sent
== sav_update
||
9835 sav_update
->natt_last_activity
== natt_now
) {
9839 // assuming that (sav_update->remote_ike_port != 0 && (esp_udp_encap_port & 0xFFFF) != 0)
9841 bzero(&saidx_swap_sent_addr
, sizeof(saidx_swap_sent_addr
));
9842 memcpy(&saidx_swap_sent_addr
.src
, &sav_sent
->sah
->saidx
.dst
, sizeof(saidx_swap_sent_addr
.src
));
9843 memcpy(&saidx_swap_sent_addr
.dst
, &sav_sent
->sah
->saidx
.src
, sizeof(saidx_swap_sent_addr
.dst
));
9844 saidx_swap_sent_addr
.proto
= sav_sent
->sah
->saidx
.proto
;
9845 saidx_swap_sent_addr
.mode
= sav_sent
->sah
->saidx
.mode
;
9846 // we ignore reqid for split-tunnel setups
9848 if (key_cmpsaidx(&sav_sent
->sah
->saidx
, &sav_update
->sah
->saidx
, CMP_MODE
| CMP_PORT
) ||
9849 key_cmpsaidx(&saidx_swap_sent_addr
, &sav_update
->sah
->saidx
, CMP_MODE
| CMP_PORT
)) {
9850 sav_update
->natt_last_activity
= natt_now
;
9855 key_send_delsp (struct secpolicy
*sp
)
9857 struct mbuf
*result
= NULL
, *m
;
9862 /* set msg header */
9863 m
= key_setsadbmsg(SADB_X_SPDDELETE
, 0, 0, 0, 0, 0);
9869 /* set sadb_address(es) for source */
9870 if (sp
->spidx
.src_range
.start
.ss_len
> 0) {
9871 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_START
,
9872 (struct sockaddr
*)&sp
->spidx
.src_range
.start
, sp
->spidx
.prefs
,
9873 sp
->spidx
.ul_proto
);
9878 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_SRC_END
,
9879 (struct sockaddr
*)&sp
->spidx
.src_range
.end
, sp
->spidx
.prefs
,
9880 sp
->spidx
.ul_proto
);
9885 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
9886 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
9887 sp
->spidx
.ul_proto
);
9893 /* set sadb_address(es) for destination */
9894 if (sp
->spidx
.dst_range
.start
.ss_len
> 0) {
9895 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_START
,
9896 (struct sockaddr
*)&sp
->spidx
.dst_range
.start
, sp
->spidx
.prefd
,
9897 sp
->spidx
.ul_proto
);
9902 m
= key_setsadbaddr(SADB_X_EXT_ADDR_RANGE_DST_END
,
9903 (struct sockaddr
*)&sp
->spidx
.dst_range
.end
, sp
->spidx
.prefd
,
9904 sp
->spidx
.ul_proto
);
9909 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
9910 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
9911 sp
->spidx
.ul_proto
);
9924 if ((result
->m_flags
& M_PKTHDR
) == 0) {
9928 if (result
->m_len
< sizeof(struct sadb_msg
)) {
9929 result
= m_pullup(result
, sizeof(struct sadb_msg
));
9930 if (result
== NULL
) {
9935 result
->m_pkthdr
.len
= 0;
9936 for (m
= result
; m
; m
= m
->m_next
)
9937 result
->m_pkthdr
.len
+= m
->m_len
;
9939 mtod(result
, struct sadb_msg
*)->sadb_msg_len
= PFKEY_UNIT64(result
->m_pkthdr
.len
);
9941 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
9950 key_delsp_for_ipsec_if (ifnet_t ipsec_if
)
9952 struct secashead
*sah
;
9953 struct secasvar
*sav
, *nextsav
;
9956 struct secpolicy
*sp
, *nextsp
;
9959 if (ipsec_if
== NULL
)
9962 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
9964 lck_mtx_lock(sadb_mutex
);
9966 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
9967 for (sp
= LIST_FIRST(&sptree
[dir
]);
9971 nextsp
= LIST_NEXT(sp
, chain
);
9973 if (sp
->ipsec_if
== ipsec_if
) {
9974 ifnet_release(sp
->ipsec_if
);
9975 sp
->ipsec_if
= NULL
;
9979 sp
->state
= IPSEC_SPSTATE_DEAD
;
9980 key_freesp(sp
, KEY_SADB_LOCKED
);
9985 LIST_FOREACH(sah
, &sahtree
, chain
) {
9986 if (sah
->ipsec_if
== ipsec_if
) {
9987 /* This SAH is linked to the IPSec interface. It now needs to close. */
9988 ifnet_release(sah
->ipsec_if
);
9989 sah
->ipsec_if
= NULL
;
9991 for (stateidx
= 0; stateidx
< _ARRAYLEN(saorder_state_alive
); stateidx
++) {
9992 state
= saorder_state_any
[stateidx
];
9993 for (sav
= LIST_FIRST(&sah
->savtree
[state
]); sav
!= NULL
; sav
= nextsav
) {
9994 nextsav
= LIST_NEXT(sav
, chain
);
9996 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
9997 key_freesav(sav
, KEY_SADB_LOCKED
);
10001 sah
->state
= SADB_SASTATE_DEAD
;
10005 lck_mtx_unlock(sadb_mutex
);
10008 __private_extern__ u_int32_t
10009 key_fill_offload_frames_for_savs (ifnet_t ifp
,
10010 struct ipsec_offload_frame
*frames_array
,
10011 u_int32_t frames_array_count
,
10012 size_t frame_data_offset
)
10014 struct secashead
*sah
= NULL
;
10015 struct secasvar
*sav
= NULL
;
10016 struct ipsec_offload_frame
*frame
= frames_array
;
10017 u_int32_t frame_index
= 0;
10019 if (frame
== NULL
|| frames_array_count
== 0) {
10020 return (frame_index
);
10023 lck_mtx_lock(sadb_mutex
);
10024 LIST_FOREACH(sah
, &sahtree
, chain
) {
10025 LIST_FOREACH(sav
, &sah
->savtree
[SADB_SASTATE_MATURE
], chain
) {
10026 if (ipsec_fill_offload_frame(ifp
, sav
, frame
, frame_data_offset
)) {
10028 if (frame_index
>= frames_array_count
) {
10029 lck_mtx_unlock(sadb_mutex
);
10030 return (frame_index
);
10032 frame
= &(frames_array
[frame_index
]);
10036 lck_mtx_unlock(sadb_mutex
);
10038 return (frame_index
);