2 * Copyright (c) 2008-2011 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>
101 #include <netinet/in_pcb.h>
104 #include <netinet6/in6_pcb.h>
107 #include <net/pfkeyv2.h>
108 #include <netkey/keydb.h>
109 #include <netkey/key.h>
110 #include <netkey/keysock.h>
111 #include <netkey/key_debug.h>
115 #include <netinet6/ipsec.h>
117 #include <netinet6/ipsec6.h>
119 #include <netinet6/ah.h>
121 #include <netinet6/ah6.h>
124 #include <netinet6/esp.h>
126 #include <netinet6/esp6.h>
129 #include <netinet6/ipcomp.h>
131 #include <netinet6/ipcomp6.h>
136 #include <sys/random.h>
138 #include <net/net_osdep.h>
140 #define FULLMASK 0xff
142 lck_grp_t
*sadb_mutex_grp
;
143 lck_grp_attr_t
*sadb_mutex_grp_attr
;
144 lck_attr_t
*sadb_mutex_attr
;
145 decl_lck_mtx_data(, sadb_mutex_data
);
146 lck_mtx_t
*sadb_mutex
= &sadb_mutex_data
;
148 lck_grp_t
*pfkey_stat_mutex_grp
;
149 lck_grp_attr_t
*pfkey_stat_mutex_grp_attr
;
150 lck_attr_t
*pfkey_stat_mutex_attr
;
151 decl_lck_mtx_data(, pfkey_stat_mutex_data
);
152 lck_mtx_t
*pfkey_stat_mutex
= &pfkey_stat_mutex_data
;
155 * Note on SA reference counting:
156 * - SAs that are not in DEAD state will have (total external reference + 1)
157 * following value in reference count field. they cannot be freed and are
158 * referenced from SA header.
159 * - SAs that are in DEAD state will have (total external reference)
160 * in reference count field. they are ready to be freed. reference from
161 * SA header will be removed in key_delsav(), when the reference count
162 * field hits 0 (= no external reference other than from SA header.
165 u_int32_t key_debug_level
= 0; //### our sysctl is not dynamic
166 static u_int key_spi_trycnt
= 1000;
167 static u_int32_t key_spi_minval
= 0x100;
168 static u_int32_t key_spi_maxval
= 0x0fffffff; /* XXX */
169 static u_int32_t policy_id
= 0;
170 static u_int key_int_random
= 60; /*interval to initialize randseed,1(m)*/
171 static u_int key_larval_lifetime
= 30; /* interval to expire acquiring, 30(s)*/
172 static int key_blockacq_count
= 10; /* counter for blocking SADB_ACQUIRE.*/
173 static int key_blockacq_lifetime
= 20; /* lifetime for blocking SADB_ACQUIRE.*/
174 static int key_preferred_oldsa
= 0; /* preferred old sa rather than new sa.*/
175 __private_extern__
int natt_keepalive_interval
= 20; /* interval between natt keepalives.*/
176 __private_extern__
int ipsec_policy_count
= 0;
177 static int ipsec_sav_count
= 0;
179 static u_int32_t acq_seq
= 0;
180 static int key_tick_init_random
= 0;
181 __private_extern__ u_int32_t natt_now
= 0;
183 static LIST_HEAD(_sptree
, secpolicy
) sptree
[IPSEC_DIR_MAX
]; /* SPD */
184 static LIST_HEAD(_sahtree
, secashead
) sahtree
; /* SAD */
185 static LIST_HEAD(_regtree
, secreg
) regtree
[SADB_SATYPE_MAX
+ 1];
188 #define SPIHASHSIZE 128
189 #define SPIHASH(x) (((x) ^ ((x) >> 16)) % SPIHASHSIZE)
190 static LIST_HEAD(_spihash
, secasvar
) spihash
[SPIHASHSIZE
];
192 #ifndef IPSEC_NONBLOCK_ACQUIRE
193 static LIST_HEAD(_acqtree
, secacq
) acqtree
; /* acquiring list */
195 static LIST_HEAD(_spacqtree
, secspacq
) spacqtree
; /* SP acquiring list */
197 struct key_cb key_cb
;
199 /* search order for SAs */
200 static const u_int saorder_state_valid_prefer_old
[] = {
201 SADB_SASTATE_DYING
, SADB_SASTATE_MATURE
,
203 static const u_int saorder_state_valid_prefer_new
[] = {
204 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
206 static const u_int saorder_state_alive
[] = {
208 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
, SADB_SASTATE_LARVAL
210 static const u_int saorder_state_any
[] = {
211 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
212 SADB_SASTATE_LARVAL
, SADB_SASTATE_DEAD
215 static const int minsize
[] = {
216 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
217 sizeof(struct sadb_sa
), /* SADB_EXT_SA */
218 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
219 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
220 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
221 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_SRC */
222 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_DST */
223 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_PROXY */
224 sizeof(struct sadb_key
), /* SADB_EXT_KEY_AUTH */
225 sizeof(struct sadb_key
), /* SADB_EXT_KEY_ENCRYPT */
226 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_SRC */
227 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_DST */
228 sizeof(struct sadb_sens
), /* SADB_EXT_SENSITIVITY */
229 sizeof(struct sadb_prop
), /* SADB_EXT_PROPOSAL */
230 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_AUTH */
231 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_ENCRYPT */
232 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
233 0, /* SADB_X_EXT_KMPRIVATE */
234 sizeof(struct sadb_x_policy
), /* SADB_X_EXT_POLICY */
235 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
236 sizeof(struct sadb_session_id
), /* SADB_EXT_SESSION_ID */
237 sizeof(struct sadb_sastat
), /* SADB_EXT_SASTAT */
239 static const int maxsize
[] = {
240 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
241 sizeof(struct sadb_sa_2
), /* SADB_EXT_SA */
242 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
243 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
244 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
245 0, /* SADB_EXT_ADDRESS_SRC */
246 0, /* SADB_EXT_ADDRESS_DST */
247 0, /* SADB_EXT_ADDRESS_PROXY */
248 0, /* SADB_EXT_KEY_AUTH */
249 0, /* SADB_EXT_KEY_ENCRYPT */
250 0, /* SADB_EXT_IDENTITY_SRC */
251 0, /* SADB_EXT_IDENTITY_DST */
252 0, /* SADB_EXT_SENSITIVITY */
253 0, /* SADB_EXT_PROPOSAL */
254 0, /* SADB_EXT_SUPPORTED_AUTH */
255 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
256 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
257 0, /* SADB_X_EXT_KMPRIVATE */
258 0, /* SADB_X_EXT_POLICY */
259 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
260 0, /* SADB_EXT_SESSION_ID */
261 0, /* SADB_EXT_SASTAT */
264 static int ipsec_esp_keymin
= 256;
265 static int ipsec_esp_auth
= 0;
266 static int ipsec_ah_keymin
= 128;
268 SYSCTL_DECL(_net_key
);
269 /* Thread safe: no accumulated state */
270 SYSCTL_INT(_net_key
, KEYCTL_DEBUG_LEVEL
, debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
271 &key_debug_level
, 0, "");
274 /* max count of trial for the decision of spi value */
275 SYSCTL_INT(_net_key
, KEYCTL_SPI_TRY
, spi_trycnt
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
276 &key_spi_trycnt
, 0, "");
278 /* minimum spi value to allocate automatically. */
279 SYSCTL_INT(_net_key
, KEYCTL_SPI_MIN_VALUE
, spi_minval
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
280 &key_spi_minval
, 0, "");
282 /* maximun spi value to allocate automatically. */
283 SYSCTL_INT(_net_key
, KEYCTL_SPI_MAX_VALUE
, spi_maxval
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
284 &key_spi_maxval
, 0, "");
286 /* interval to initialize randseed */
287 SYSCTL_INT(_net_key
, KEYCTL_RANDOM_INT
, int_random
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
288 &key_int_random
, 0, "");
290 /* lifetime for larval SA; thread safe due to > compare */
291 SYSCTL_INT(_net_key
, KEYCTL_LARVAL_LIFETIME
, larval_lifetime
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
292 &key_larval_lifetime
, 0, "");
294 /* counter for blocking to send SADB_ACQUIRE to IKEd */
295 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_COUNT
, blockacq_count
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
296 &key_blockacq_count
, 0, "");
298 /* lifetime for blocking to send SADB_ACQUIRE to IKEd: Thread safe, > compare */
299 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_LIFETIME
, blockacq_lifetime
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
300 &key_blockacq_lifetime
, 0, "");
303 SYSCTL_INT(_net_key
, KEYCTL_ESP_AUTH
, esp_auth
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
304 &ipsec_esp_auth
, 0, "");
306 /* minimum ESP key length */
307 SYSCTL_INT(_net_key
, KEYCTL_ESP_KEYMIN
, esp_keymin
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
308 &ipsec_esp_keymin
, 0, "");
310 /* minimum AH key length */
311 SYSCTL_INT(_net_key
, KEYCTL_AH_KEYMIN
, ah_keymin
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
312 &ipsec_ah_keymin
, 0, "");
314 /* perfered old SA rather than new SA */
315 SYSCTL_INT(_net_key
, KEYCTL_PREFERED_OLDSA
, prefered_oldsa
, CTLFLAG_RW
| CTLFLAG_LOCKED
,\
316 &key_preferred_oldsa
, 0, "");
318 /* time between NATT keepalives in seconds, 0 disabled */
319 SYSCTL_INT(_net_key
, KEYCTL_NATT_KEEPALIVE_INTERVAL
, natt_keepalive_interval
, CTLFLAG_RW
| CTLFLAG_LOCKED
,\
320 &natt_keepalive_interval
, 0, "");
322 /* PF_KEY statistics */
323 SYSCTL_STRUCT(_net_key
, KEYCTL_PFKEYSTAT
, pfkeystat
, CTLFLAG_RD
| CTLFLAG_LOCKED
,\
324 &pfkeystat
, pfkeystat
, "");
327 #define LIST_FOREACH(elm, head, field) \
328 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
330 #define __LIST_CHAINED(elm) \
331 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
332 #define LIST_INSERT_TAIL(head, elm, type, field) \
334 struct type *curelm = LIST_FIRST(head); \
335 if (curelm == NULL) {\
336 LIST_INSERT_HEAD(head, elm, field); \
338 while (LIST_NEXT(curelm, field)) \
339 curelm = LIST_NEXT(curelm, field);\
340 LIST_INSERT_AFTER(curelm, elm, field);\
344 #define KEY_CHKSASTATE(head, sav, name) \
346 if ((head) != (sav)) { \
347 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
348 (name), (head), (sav))); \
353 #define KEY_CHKSPDIR(head, sp, name) \
355 if ((head) != (sp)) { \
356 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
357 "anyway continue.\n", \
358 (name), (head), (sp))); \
363 #define KMALLOC_WAIT(p, t, n) \
364 ((p) = (t) _MALLOC((u_int32_t)(n), M_SECA, M_WAITOK))
365 #define KMALLOC_NOWAIT(p, t, n) \
366 ((p) = (t) _MALLOC((u_int32_t)(n), M_SECA, M_NOWAIT))
368 _FREE((caddr_t)(p), M_SECA);
370 #define KMALLOC_WAIT(p, t, n) \
372 ((p) = (t)_MALLOC((u_int32_t)(n), M_SECA, M_WAITOK)); \
373 printf("%s %d: %p <- KMALLOC_WAIT(%s, %d)\n", \
374 __FILE__, __LINE__, (p), #t, n); \
376 #define KMALLOC_NOWAIT(p, t, n) \
378 ((p) = (t)_MALLOC((u_int32_t)(n), M_SECA, M_NOWAIT)); \
379 printf("%s %d: %p <- KMALLOC_NOWAIT(%s, %d)\n", \
380 __FILE__, __LINE__, (p), #t, n); \
385 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
386 _FREE((caddr_t)(p), M_SECA); \
391 * set parameters into secpolicyindex buffer.
392 * Must allocate secpolicyindex buffer passed to this function.
394 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
396 bzero((idx), sizeof(struct secpolicyindex)); \
397 (idx)->dir = (_dir); \
398 (idx)->prefs = (ps); \
399 (idx)->prefd = (pd); \
400 (idx)->ul_proto = (ulp); \
401 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
402 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
406 * set parameters into secasindex buffer.
407 * Must allocate secasindex buffer before calling this function.
409 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
411 bzero((idx), sizeof(struct secasindex)); \
412 (idx)->proto = (p); \
414 (idx)->reqid = (r); \
415 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
416 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
421 u_int32_t getspi_count
; /* the avarage of count to try to get new SPI */
425 struct sadb_msg
*msg
;
426 struct sadb_ext
*ext
[SADB_EXT_MAX
+ 1];
427 int extoff
[SADB_EXT_MAX
+ 1];
428 int extlen
[SADB_EXT_MAX
+ 1];
431 static struct secasvar
*key_do_allocsa_policy(struct secashead
*, u_int
, u_int16_t
);
432 static int key_do_get_translated_port(struct secashead
*, struct secasvar
*, u_int
);
433 static void key_delsp(struct secpolicy
*);
434 static struct secpolicy
*key_getsp(struct secpolicyindex
*);
435 static struct secpolicy
*key_getspbyid(u_int32_t
);
436 static u_int32_t
key_newreqid(void);
437 static struct mbuf
*key_gather_mbuf(struct mbuf
*,
438 const struct sadb_msghdr
*, int, int, int *);
439 static int key_spdadd(struct socket
*, struct mbuf
*,
440 const struct sadb_msghdr
*);
441 static u_int32_t
key_getnewspid(void);
442 static int key_spddelete(struct socket
*, struct mbuf
*,
443 const struct sadb_msghdr
*);
444 static int key_spddelete2(struct socket
*, struct mbuf
*,
445 const struct sadb_msghdr
*);
446 static int key_spdget(struct socket
*, struct mbuf
*,
447 const struct sadb_msghdr
*);
448 static int key_spdflush(struct socket
*, struct mbuf
*,
449 const struct sadb_msghdr
*);
450 static int key_spddump(struct socket
*, struct mbuf
*,
451 const struct sadb_msghdr
*);
452 static struct mbuf
*key_setdumpsp(struct secpolicy
*,
453 u_int8_t
, u_int32_t
, u_int32_t
);
454 static u_int
key_getspreqmsglen(struct secpolicy
*);
455 static int key_spdexpire(struct secpolicy
*);
456 static struct secashead
*key_newsah(struct secasindex
*, u_int8_t
);
457 static struct secasvar
*key_newsav(struct mbuf
*,
458 const struct sadb_msghdr
*, struct secashead
*, int *);
459 static struct secashead
*key_getsah(struct secasindex
*);
460 static struct secasvar
*key_checkspidup(struct secasindex
*, u_int32_t
);
461 static void key_setspi
__P((struct secasvar
*, u_int32_t
));
462 static struct secasvar
*key_getsavbyspi(struct secashead
*, u_int32_t
);
463 static int key_setsaval(struct secasvar
*, struct mbuf
*,
464 const struct sadb_msghdr
*);
465 static int key_mature(struct secasvar
*);
466 static struct mbuf
*key_setdumpsa(struct secasvar
*, u_int8_t
,
467 u_int8_t
, u_int32_t
, u_int32_t
);
468 static struct mbuf
*key_setsadbmsg(u_int8_t
, u_int16_t
, u_int8_t
,
469 u_int32_t
, pid_t
, u_int16_t
);
470 static struct mbuf
*key_setsadbsa(struct secasvar
*);
471 static struct mbuf
*key_setsadbaddr(u_int16_t
,
472 struct sockaddr
*, u_int8_t
, u_int16_t
);
474 static struct mbuf
*key_setsadbident(u_int16_t
, u_int16_t
, caddr_t
,
477 static struct mbuf
*key_setsadbxsa2(u_int8_t
, u_int32_t
, u_int32_t
);
478 static struct mbuf
*key_setsadbxpolicy(u_int16_t
, u_int8_t
,
480 static void *key_newbuf(const void *, u_int
);
482 static int key_ismyaddr6(struct sockaddr_in6
*);
484 static void key_update_natt_keepalive_timestamp(struct secasvar
*, struct secasvar
*);
486 /* flags for key_cmpsaidx() */
487 #define CMP_HEAD 0x1 /* protocol, addresses. */
488 #define CMP_PORT 0x2 /* additionally HEAD, reqid, mode. */
489 #define CMP_REQID 0x4 /* additionally HEAD, reqid. */
490 #define CMP_MODE 0x8 /* additionally mode. */
491 #define CMP_EXACTLY 0xF /* all elements. */
492 static int key_cmpsaidx(struct secasindex
*, struct secasindex
*, int);
494 static int key_cmpspidx_exactly(struct secpolicyindex
*,
495 struct secpolicyindex
*);
496 static int key_cmpspidx_withmask(struct secpolicyindex
*,
497 struct secpolicyindex
*);
498 static int key_sockaddrcmp(struct sockaddr
*, struct sockaddr
*, int);
499 static int key_bbcmp(caddr_t
, caddr_t
, u_int
);
500 static void key_srandom(void);
501 static u_int16_t
key_satype2proto(u_int8_t
);
502 static u_int8_t
key_proto2satype(u_int16_t
);
504 static int key_getspi(struct socket
*, struct mbuf
*,
505 const struct sadb_msghdr
*);
506 static u_int32_t
key_do_getnewspi(struct sadb_spirange
*, struct secasindex
*);
507 static int key_update(struct socket
*, struct mbuf
*,
508 const struct sadb_msghdr
*);
510 static struct secasvar
*key_getsavbyseq(struct secashead
*, u_int32_t
);
512 static int key_add(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
513 static int key_setident(struct secashead
*, struct mbuf
*,
514 const struct sadb_msghdr
*);
515 static struct mbuf
*key_getmsgbuf_x1(struct mbuf
*, const struct sadb_msghdr
*);
516 static int key_delete(struct socket
*, struct mbuf
*,
517 const struct sadb_msghdr
*);
518 static int key_get(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
520 static void key_getcomb_setlifetime(struct sadb_comb
*);
522 static struct mbuf
*key_getcomb_esp(void);
524 static struct mbuf
*key_getcomb_ah(void);
525 static struct mbuf
*key_getcomb_ipcomp(void);
526 static struct mbuf
*key_getprop(const struct secasindex
*);
528 static int key_acquire(struct secasindex
*, struct secpolicy
*);
529 #ifndef IPSEC_NONBLOCK_ACQUIRE
530 static struct secacq
*key_newacq(struct secasindex
*);
531 static struct secacq
*key_getacq(struct secasindex
*);
532 static struct secacq
*key_getacqbyseq(u_int32_t
);
534 static struct secspacq
*key_newspacq(struct secpolicyindex
*);
535 static struct secspacq
*key_getspacq(struct secpolicyindex
*);
536 static int key_acquire2(struct socket
*, struct mbuf
*,
537 const struct sadb_msghdr
*);
538 static int key_register(struct socket
*, struct mbuf
*,
539 const struct sadb_msghdr
*);
540 static int key_expire(struct secasvar
*);
541 static int key_flush(struct socket
*, struct mbuf
*,
542 const struct sadb_msghdr
*);
543 static int key_dump(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
544 static int key_promisc(struct socket
*, struct mbuf
*,
545 const struct sadb_msghdr
*);
546 static int key_senderror(struct socket
*, struct mbuf
*, int);
547 static int key_validate_ext(const struct sadb_ext
*, int);
548 static int key_align(struct mbuf
*, struct sadb_msghdr
*);
549 static struct mbuf
*key_alloc_mbuf(int);
550 static int key_getsastat (struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
551 static int key_setsaval2(struct secasvar
*sav
,
557 struct sadb_key
*key_auth
,
558 u_int16_t key_auth_len
,
559 struct sadb_key
*key_enc
,
560 u_int16_t key_enc_len
,
565 struct sadb_lifetime
*lifetime_hard
,
566 struct sadb_lifetime
*lifetime_soft
);
568 extern int ipsec_bypass
;
569 extern int esp_udp_encap_port
;
570 int ipsec_send_natt_keepalive(struct secasvar
*sav
);
578 * setup locks, call raw_init(), and then init timer and associated data
587 sadb_mutex_grp_attr
= lck_grp_attr_alloc_init();
588 sadb_mutex_grp
= lck_grp_alloc_init("sadb", sadb_mutex_grp_attr
);
589 sadb_mutex_attr
= lck_attr_alloc_init();
591 lck_mtx_init(sadb_mutex
, sadb_mutex_grp
, sadb_mutex_attr
);
593 pfkey_stat_mutex_grp_attr
= lck_grp_attr_alloc_init();
594 pfkey_stat_mutex_grp
= lck_grp_alloc_init("pfkey_stat", pfkey_stat_mutex_grp_attr
);
595 pfkey_stat_mutex_attr
= lck_attr_alloc_init();
597 lck_mtx_init(pfkey_stat_mutex
, pfkey_stat_mutex_grp
, pfkey_stat_mutex_attr
);
599 for (i
= 0; i
< SPIHASHSIZE
; i
++)
600 LIST_INIT(&spihash
[i
]);
604 bzero((caddr_t
)&key_cb
, sizeof(key_cb
));
606 for (i
= 0; i
< IPSEC_DIR_MAX
; i
++) {
607 LIST_INIT(&sptree
[i
]);
609 ipsec_policy_count
= 0;
613 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
614 LIST_INIT(®tree
[i
]);
618 #ifndef IPSEC_NONBLOCK_ACQUIRE
621 LIST_INIT(&spacqtree
);
625 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
626 ip4_def_policy
.refcnt
++; /*never reclaim this*/
629 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
630 ip6_def_policy
.refcnt
++; /*never reclaim this*/
634 timeout((void *)key_timehandler
, (void *)0, hz
);
635 #endif /*IPSEC_DEBUG2*/
637 /* initialize key statistics */
638 keystat
.getspi_count
= 1;
641 printf("IPsec: Initialized Security Association Processing.\n");
646 /* %%% IPsec policy management */
648 * allocating a SP for OUTBOUND or INBOUND packet.
649 * Must call key_freesp() later.
650 * OUT: NULL: not found
651 * others: found and return the pointer.
655 struct secpolicyindex
*spidx
,
658 struct secpolicy
*sp
;
661 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
664 panic("key_allocsp: NULL pointer is passed.\n");
666 /* check direction */
668 case IPSEC_DIR_INBOUND
:
669 case IPSEC_DIR_OUTBOUND
:
672 panic("key_allocsp: Invalid direction is passed.\n");
676 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
677 printf("*** objects\n");
678 kdebug_secpolicyindex(spidx
));
680 lck_mtx_lock(sadb_mutex
);
681 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
682 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
683 printf("*** in SPD\n");
684 kdebug_secpolicyindex(&sp
->spidx
));
686 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
688 if (key_cmpspidx_withmask(&sp
->spidx
, spidx
))
691 lck_mtx_unlock(sadb_mutex
);
696 /* found a SPD entry */
698 sp
->lastused
= tv
.tv_sec
;
700 lck_mtx_unlock(sadb_mutex
);
703 KEY_CHKSPDIR(sp
->spidx
.dir
, dir
, "key_allocsp");
704 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
705 printf("DP key_allocsp cause refcnt++:%d SP:%p\n",
711 * return a policy that matches this particular inbound packet.
716 struct sockaddr
*osrc
,
717 struct sockaddr
*odst
,
718 struct sockaddr
*isrc
,
719 struct sockaddr
*idst
)
721 struct secpolicy
*sp
;
722 const int dir
= IPSEC_DIR_INBOUND
;
724 struct ipsecrequest
*r1
, *r2
, *p
;
725 struct sockaddr
*os
, *od
, *is
, *id
;
726 struct secpolicyindex spidx
;
728 if (isrc
->sa_family
!= idst
->sa_family
) {
729 ipseclog((LOG_ERR
, "protocol family mismatched %d != %d\n.",
730 isrc
->sa_family
, idst
->sa_family
));
734 lck_mtx_lock(sadb_mutex
);
735 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
736 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
740 for (p
= sp
->req
; p
; p
= p
->next
) {
741 if (p
->saidx
.mode
!= IPSEC_MODE_TUNNEL
)
748 /* here we look at address matches only */
750 if (isrc
->sa_len
> sizeof(spidx
.src
) ||
751 idst
->sa_len
> sizeof(spidx
.dst
))
753 bcopy(isrc
, &spidx
.src
, isrc
->sa_len
);
754 bcopy(idst
, &spidx
.dst
, idst
->sa_len
);
755 if (!key_cmpspidx_withmask(&sp
->spidx
, &spidx
))
758 is
= (struct sockaddr
*)&r1
->saidx
.src
;
759 id
= (struct sockaddr
*)&r1
->saidx
.dst
;
760 if (key_sockaddrcmp(is
, isrc
, 0) ||
761 key_sockaddrcmp(id
, idst
, 0))
765 os
= (struct sockaddr
*)&r2
->saidx
.src
;
766 od
= (struct sockaddr
*)&r2
->saidx
.dst
;
767 if (key_sockaddrcmp(os
, osrc
, 0) ||
768 key_sockaddrcmp(od
, odst
, 0))
774 lck_mtx_unlock(sadb_mutex
);
779 sp
->lastused
= tv
.tv_sec
;
781 lck_mtx_unlock(sadb_mutex
);
786 * allocating an SA entry for an *OUTBOUND* packet.
787 * checking each request entries in SP, and acquire an SA if need.
788 * OUT: 0: there are valid requests.
789 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
793 struct ipsecrequest
*isr
,
794 struct secasindex
*saidx
,
795 struct secasvar
**sav
)
799 struct sockaddr_in
*sin
;
801 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
806 if (isr
== NULL
|| saidx
== NULL
)
807 panic("key_checkrequest: NULL pointer is passed.\n");
810 switch (saidx
->mode
) {
811 case IPSEC_MODE_TRANSPORT
:
812 case IPSEC_MODE_TUNNEL
:
816 panic("key_checkrequest: Invalid policy defined.\n");
819 /* get current level */
820 level
= ipsec_get_reqlevel(isr
);
824 * key_allocsa_policy should allocate the oldest SA available.
825 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
828 *sav
= key_allocsa_policy(saidx
);
830 /* When there is SA. */
836 * Remove dst port - used for special natt support - don't call
837 * key_acquire with it.
839 if (saidx
->mode
== IPSEC_MODE_TRANSPORT
) {
840 sin
= (struct sockaddr_in
*)&saidx
->dst
;
841 sin
->sin_port
= IPSEC_PORT_ANY
;
843 if ((error
= key_acquire(saidx
, isr
->sp
)) != 0) {
844 /* XXX What should I do ? */
845 ipseclog((LOG_DEBUG
, "key_checkrequest: error %d returned "
846 "from key_acquire.\n", error
));
850 return level
== IPSEC_LEVEL_REQUIRE
? ENOENT
: 0;
854 * allocating a SA for policy entry from SAD.
855 * NOTE: searching SAD of aliving state.
856 * OUT: NULL: not found.
857 * others: found and return the pointer.
859 u_int32_t sah_search_calls
= 0;
860 u_int32_t sah_search_count
= 0;
863 struct secasindex
*saidx
)
865 struct secashead
*sah
;
866 struct secasvar
*sav
;
867 u_int stateidx
, state
;
868 const u_int
*saorder_state_valid
;
870 struct sockaddr_in
*sin
;
873 lck_mtx_lock(sadb_mutex
);
875 LIST_FOREACH(sah
, &sahtree
, chain
) {
877 if (sah
->state
== SADB_SASTATE_DEAD
)
879 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_MODE
| CMP_REQID
))
882 lck_mtx_unlock(sadb_mutex
);
888 * search a valid state list for outbound packet.
889 * This search order is important.
891 if (key_preferred_oldsa
) {
892 saorder_state_valid
= saorder_state_valid_prefer_old
;
893 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
895 saorder_state_valid
= saorder_state_valid_prefer_new
;
896 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
900 sin
= (struct sockaddr_in
*)&saidx
->dst
;
901 dstport
= sin
->sin_port
;
902 if (saidx
->mode
== IPSEC_MODE_TRANSPORT
)
903 sin
->sin_port
= IPSEC_PORT_ANY
;
905 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
907 state
= saorder_state_valid
[stateidx
];
909 sav
= key_do_allocsa_policy(sah
, state
, dstport
);
911 lck_mtx_unlock(sadb_mutex
);
915 lck_mtx_unlock(sadb_mutex
);
920 * searching SAD with direction, protocol, mode and state.
921 * called by key_allocsa_policy().
924 * others : found, pointer to a SA.
926 static struct secasvar
*
927 key_do_allocsa_policy(
928 struct secashead
*sah
,
932 struct secasvar
*sav
, *nextsav
, *candidate
, *natt_candidate
, *no_natt_candidate
, *d
;
934 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
938 natt_candidate
= NULL
;
939 no_natt_candidate
= NULL
;
941 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
945 nextsav
= LIST_NEXT(sav
, chain
);
948 KEY_CHKSASTATE(sav
->state
, state
, "key_do_allocsa_policy");
950 if (sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&& dstport
&&
951 ((sav
->flags
& SADB_X_EXT_NATT
) != 0) &&
952 ntohs(dstport
) != sav
->remote_ike_port
)
955 if (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
&&
956 ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0) &&
957 ntohs(dstport
) != sav
->remote_ike_port
)
958 continue; /* skip this one - not a match - or not UDP */
960 if ((sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&&
961 ((sav
->flags
& SADB_X_EXT_NATT
) != 0)) ||
962 (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
&&
963 ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0))) {
964 if (natt_candidate
== NULL
) {
965 natt_candidate
= sav
;
968 candidate
= natt_candidate
;
970 if (no_natt_candidate
== NULL
) {
971 no_natt_candidate
= sav
;
974 candidate
= no_natt_candidate
;
977 /* Which SA is the better ? */
980 if (candidate
->lft_c
== NULL
|| sav
->lft_c
== NULL
)
981 panic("key_do_allocsa_policy: "
982 "lifetime_current is NULL.\n");
984 /* What the best method is to compare ? */
985 if (key_preferred_oldsa
) {
986 if (candidate
->lft_c
->sadb_lifetime_addtime
>
987 sav
->lft_c
->sadb_lifetime_addtime
) {
988 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
989 natt_candidate
= sav
;
991 no_natt_candidate
= sav
;
997 /* prefered new sa rather than old sa */
998 if (candidate
->lft_c
->sadb_lifetime_addtime
<
999 sav
->lft_c
->sadb_lifetime_addtime
) {
1001 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
1002 natt_candidate
= sav
;
1004 no_natt_candidate
= sav
;
1009 * prepared to delete the SA when there is more
1010 * suitable candidate and the lifetime of the SA is not
1013 if (d
->lft_c
->sadb_lifetime_addtime
!= 0) {
1014 struct mbuf
*m
, *result
;
1016 key_sa_chgstate(d
, SADB_SASTATE_DEAD
);
1018 m
= key_setsadbmsg(SADB_DELETE
, 0,
1019 d
->sah
->saidx
.proto
, 0, 0, d
->refcnt
- 1);
1024 /* set sadb_address for saidx's. */
1025 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
1026 (struct sockaddr
*)&d
->sah
->saidx
.src
,
1027 d
->sah
->saidx
.src
.ss_len
<< 3,
1033 /* set sadb_address for saidx's. */
1034 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
1035 (struct sockaddr
*)&d
->sah
->saidx
.src
,
1036 d
->sah
->saidx
.src
.ss_len
<< 3,
1042 /* create SA extension */
1043 m
= key_setsadbsa(d
);
1048 if (result
->m_len
< sizeof(struct sadb_msg
)) {
1049 result
= m_pullup(result
,
1050 sizeof(struct sadb_msg
));
1055 result
->m_pkthdr
.len
= 0;
1056 for (m
= result
; m
; m
= m
->m_next
)
1057 result
->m_pkthdr
.len
+= m
->m_len
;
1058 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
1059 PFKEY_UNIT64(result
->m_pkthdr
.len
);
1061 if (key_sendup_mbuf(NULL
, result
,
1062 KEY_SENDUP_REGISTERED
))
1065 key_freesav(d
, KEY_SADB_LOCKED
);
1069 /* choose latest if both types present */
1070 if (natt_candidate
== NULL
)
1071 candidate
= no_natt_candidate
;
1072 else if (no_natt_candidate
== NULL
)
1073 candidate
= natt_candidate
;
1074 else if (sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&& dstport
)
1075 candidate
= natt_candidate
;
1076 else if (natt_candidate
->lft_c
->sadb_lifetime_addtime
>
1077 no_natt_candidate
->lft_c
->sadb_lifetime_addtime
)
1078 candidate
= natt_candidate
;
1080 candidate
= no_natt_candidate
;
1083 candidate
->refcnt
++;
1084 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1085 printf("DP allocsa_policy cause "
1086 "refcnt++:%d SA:%p\n",
1087 candidate
->refcnt
, candidate
));
1093 * allocating a SA entry for a *INBOUND* packet.
1094 * Must call key_freesav() later.
1095 * OUT: positive: pointer to a sav.
1096 * NULL: not found, or error occurred.
1098 * In the comparison, source address will be ignored for RFC2401 conformance.
1099 * To quote, from section 4.1:
1100 * A security association is uniquely identified by a triple consisting
1101 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1102 * security protocol (AH or ESP) identifier.
1103 * Note that, however, we do need to keep source address in IPsec SA.
1104 * IKE specification and PF_KEY specification do assume that we
1105 * keep source address in IPsec SA. We see a tricky situation here.
1115 struct secasvar
*sav
, *match
;
1116 u_int stateidx
, state
, tmpidx
, matchidx
;
1117 struct sockaddr_in sin
;
1118 struct sockaddr_in6 sin6
;
1119 const u_int
*saorder_state_valid
;
1122 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1125 if (src
== NULL
|| dst
== NULL
)
1126 panic("key_allocsa: NULL pointer is passed.\n");
1129 * when both systems employ similar strategy to use a SA.
1130 * the search order is important even in the inbound case.
1132 if (key_preferred_oldsa
) {
1133 saorder_state_valid
= saorder_state_valid_prefer_old
;
1134 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
1136 saorder_state_valid
= saorder_state_valid_prefer_new
;
1137 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
1142 * XXX: to be checked internal IP header somewhere. Also when
1143 * IPsec tunnel packet is received. But ESP tunnel mode is
1144 * encrypted so we can't check internal IP header.
1147 * search a valid state list for inbound packet.
1148 * the search order is not important.
1151 matchidx
= arraysize
;
1152 lck_mtx_lock(sadb_mutex
);
1153 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
1154 if (sav
->spi
!= spi
)
1156 if (proto
!= sav
->sah
->saidx
.proto
)
1158 if (family
!= sav
->sah
->saidx
.src
.ss_family
||
1159 family
!= sav
->sah
->saidx
.dst
.ss_family
)
1162 for (stateidx
= 0; stateidx
< matchidx
; stateidx
++) {
1163 state
= saorder_state_valid
[stateidx
];
1164 if (sav
->state
== state
) {
1169 if (tmpidx
>= matchidx
)
1172 #if 0 /* don't check src */
1173 /* check src address */
1176 bzero(&sin
, sizeof(sin
));
1177 sin
.sin_family
= AF_INET
;
1178 sin
.sin_len
= sizeof(sin
);
1179 bcopy(src
, &sin
.sin_addr
,
1180 sizeof(sin
.sin_addr
));
1181 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
1182 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0)
1186 bzero(&sin6
, sizeof(sin6
));
1187 sin6
.sin6_family
= AF_INET6
;
1188 sin6
.sin6_len
= sizeof(sin6
);
1189 bcopy(src
, &sin6
.sin6_addr
,
1190 sizeof(sin6
.sin6_addr
));
1191 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
1192 /* kame fake scopeid */
1193 sin6
.sin6_scope_id
=
1194 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
1195 sin6
.sin6_addr
.s6_addr16
[1] = 0;
1197 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
1198 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0)
1202 ipseclog((LOG_DEBUG
, "key_allocsa: "
1203 "unknown address family=%d.\n",
1209 /* check dst address */
1212 bzero(&sin
, sizeof(sin
));
1213 sin
.sin_family
= AF_INET
;
1214 sin
.sin_len
= sizeof(sin
);
1215 bcopy(dst
, &sin
.sin_addr
,
1216 sizeof(sin
.sin_addr
));
1217 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
1218 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0)
1223 bzero(&sin6
, sizeof(sin6
));
1224 sin6
.sin6_family
= AF_INET6
;
1225 sin6
.sin6_len
= sizeof(sin6
);
1226 bcopy(dst
, &sin6
.sin6_addr
,
1227 sizeof(sin6
.sin6_addr
));
1228 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
1229 /* kame fake scopeid */
1230 sin6
.sin6_scope_id
=
1231 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
1232 sin6
.sin6_addr
.s6_addr16
[1] = 0;
1234 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
1235 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0)
1239 ipseclog((LOG_DEBUG
, "key_allocsa: "
1240 "unknown address family=%d.\n", family
));
1251 lck_mtx_unlock(sadb_mutex
);
1256 lck_mtx_unlock(sadb_mutex
);
1257 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1258 printf("DP allocsa cause refcnt++:%d SA:%p\n",
1259 match
->refcnt
, match
));
1264 key_natt_get_translated_port(
1265 struct secasvar
*outsav
)
1268 struct secasindex saidx
;
1269 struct secashead
*sah
;
1270 u_int stateidx
, state
;
1271 const u_int
*saorder_state_valid
;
1274 /* get sa for incoming */
1275 saidx
.mode
= outsav
->sah
->saidx
.mode
;
1277 saidx
.proto
= outsav
->sah
->saidx
.proto
;
1278 bcopy(&outsav
->sah
->saidx
.src
, &saidx
.dst
, sizeof(struct sockaddr_in
));
1279 bcopy(&outsav
->sah
->saidx
.dst
, &saidx
.src
, sizeof(struct sockaddr_in
));
1281 lck_mtx_lock(sadb_mutex
);
1282 LIST_FOREACH(sah
, &sahtree
, chain
) {
1283 if (sah
->state
== SADB_SASTATE_DEAD
)
1285 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_MODE
))
1288 lck_mtx_unlock(sadb_mutex
);
1293 * Found sah - now go thru list of SAs and find
1294 * matching remote ike port. If found - set
1295 * sav->natt_encapsulated_src_port and return the port.
1298 * search a valid state list for outbound packet.
1299 * This search order is important.
1301 if (key_preferred_oldsa
) {
1302 saorder_state_valid
= saorder_state_valid_prefer_old
;
1303 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
1305 saorder_state_valid
= saorder_state_valid_prefer_new
;
1306 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
1309 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
1310 state
= saorder_state_valid
[stateidx
];
1311 if (key_do_get_translated_port(sah
, outsav
, state
)) {
1312 lck_mtx_unlock(sadb_mutex
);
1313 return outsav
->natt_encapsulated_src_port
;
1316 lck_mtx_unlock(sadb_mutex
);
1321 key_do_get_translated_port(
1322 struct secashead
*sah
,
1323 struct secasvar
*outsav
,
1326 struct secasvar
*currsav
, *nextsav
, *candidate
;
1329 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1334 for (currsav
= LIST_FIRST(&sah
->savtree
[state
]);
1336 currsav
= nextsav
) {
1338 nextsav
= LIST_NEXT(currsav
, chain
);
1341 KEY_CHKSASTATE(currsav
->state
, state
, "key_do_get_translated_port");
1343 if ((currsav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) == 0 ||
1344 currsav
->remote_ike_port
!= outsav
->remote_ike_port
)
1347 if (candidate
== NULL
) {
1348 candidate
= currsav
;
1352 /* Which SA is the better ? */
1354 /* sanity check 2 */
1355 if (candidate
->lft_c
== NULL
|| currsav
->lft_c
== NULL
)
1356 panic("key_do_get_translated_port: "
1357 "lifetime_current is NULL.\n");
1359 /* What the best method is to compare ? */
1360 if (key_preferred_oldsa
) {
1361 if (candidate
->lft_c
->sadb_lifetime_addtime
>
1362 currsav
->lft_c
->sadb_lifetime_addtime
) {
1363 candidate
= currsav
;
1369 /* prefered new sa rather than old sa */
1370 if (candidate
->lft_c
->sadb_lifetime_addtime
<
1371 currsav
->lft_c
->sadb_lifetime_addtime
)
1372 candidate
= currsav
;
1376 outsav
->natt_encapsulated_src_port
= candidate
->natt_encapsulated_src_port
;
1384 * Must be called after calling key_allocsp().
1385 * For both the packet without socket and key_freeso().
1389 struct secpolicy
*sp
,
1395 panic("key_freesp: NULL pointer is passed.\n");
1398 lck_mtx_lock(sadb_mutex
);
1400 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1402 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1403 printf("DP freesp cause refcnt--:%d SP:%p\n",
1406 if (sp
->refcnt
== 0)
1409 lck_mtx_unlock(sadb_mutex
);
1414 static void key_freesp_so(struct secpolicy
**);
1417 * Must be called after calling key_allocsp().
1418 * For the packet with socket.
1427 panic("key_freeso: NULL pointer is passed.\n");
1429 lck_mtx_lock(sadb_mutex
);
1430 switch (so
->so_proto
->pr_domain
->dom_family
) {
1434 struct inpcb
*pcb
= sotoinpcb(so
);
1436 /* Does it have a PCB ? */
1437 if (pcb
== NULL
|| pcb
->inp_sp
== NULL
)
1439 key_freesp_so(&pcb
->inp_sp
->sp_in
);
1440 key_freesp_so(&pcb
->inp_sp
->sp_out
);
1448 struct inpcb
*pcb
= sotoinpcb(so
);
1450 /* Does it have a PCB ? */
1451 if (pcb
== NULL
|| pcb
->inp_sp
== NULL
)
1453 key_freesp_so(&pcb
->inp_sp
->sp_in
);
1454 key_freesp_so(&pcb
->inp_sp
->sp_out
);
1456 struct in6pcb
*pcb
= sotoin6pcb(so
);
1458 /* Does it have a PCB ? */
1459 if (pcb
== NULL
|| pcb
->in6p_sp
== NULL
)
1461 key_freesp_so(&pcb
->in6p_sp
->sp_in
);
1462 key_freesp_so(&pcb
->in6p_sp
->sp_out
);
1468 ipseclog((LOG_DEBUG
, "key_freeso: unknown address family=%d.\n",
1469 so
->so_proto
->pr_domain
->dom_family
));
1473 lck_mtx_unlock(sadb_mutex
);
1480 struct secpolicy
**sp
)
1484 if (sp
== NULL
|| *sp
== NULL
)
1485 panic("key_freesp_so: sp == NULL\n");
1487 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1489 switch ((*sp
)->policy
) {
1490 case IPSEC_POLICY_IPSEC
:
1491 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1492 printf("DP freeso calls free SP:%p\n", *sp
));
1493 key_freesp(*sp
, KEY_SADB_LOCKED
);
1496 case IPSEC_POLICY_ENTRUST
:
1497 case IPSEC_POLICY_BYPASS
:
1500 panic("key_freesp_so: Invalid policy found %d", (*sp
)->policy
);
1509 * Must be called after calling key_allocsa().
1510 * This function is called by key_freesp() to free some SA allocated
1515 struct secasvar
*sav
,
1521 panic("key_freesav: NULL pointer is passed.\n");
1524 lck_mtx_lock(sadb_mutex
);
1526 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1528 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1529 printf("DP freesav cause refcnt--:%d SA:%p SPI %u\n",
1530 sav
->refcnt
, sav
, (u_int32_t
)ntohl(sav
->spi
)));
1532 if (sav
->refcnt
== 0)
1535 lck_mtx_unlock(sadb_mutex
);
1539 /* %%% SPD management */
1541 * free security policy entry.
1545 struct secpolicy
*sp
)
1550 panic("key_delsp: NULL pointer is passed.\n");
1552 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1553 sp
->state
= IPSEC_SPSTATE_DEAD
;
1556 return; /* can't free */
1558 /* remove from SP index */
1559 if (__LIST_CHAINED(sp
)) {
1560 LIST_REMOVE(sp
, chain
);
1561 ipsec_policy_count
--;
1565 struct ipsecrequest
*isr
= sp
->req
, *nextisr
;
1567 while (isr
!= NULL
) {
1568 nextisr
= isr
->next
;
1573 keydb_delsecpolicy(sp
);
1580 * OUT: NULL : not found
1581 * others : found, pointer to a SP.
1583 static struct secpolicy
*
1585 struct secpolicyindex
*spidx
)
1587 struct secpolicy
*sp
;
1589 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1593 panic("key_getsp: NULL pointer is passed.\n");
1595 LIST_FOREACH(sp
, &sptree
[spidx
->dir
], chain
) {
1596 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1598 if (key_cmpspidx_exactly(spidx
, &sp
->spidx
)) {
1609 * OUT: NULL : not found
1610 * others : found, pointer to a SP.
1612 static struct secpolicy
*
1616 struct secpolicy
*sp
;
1618 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1620 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_INBOUND
], chain
) {
1621 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1629 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_OUTBOUND
], chain
) {
1630 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1644 struct secpolicy
*newsp
= NULL
;
1646 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1647 newsp
= keydb_newsecpolicy();
1658 * create secpolicy structure from sadb_x_policy structure.
1659 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1660 * so must be set properly later.
1664 struct sadb_x_policy
*xpl0
,
1668 struct secpolicy
*newsp
;
1670 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1674 panic("key_msg2sp: NULL pointer was passed.\n");
1675 if (len
< sizeof(*xpl0
))
1676 panic("key_msg2sp: invalid length.\n");
1677 if (len
!= PFKEY_EXTLEN(xpl0
)) {
1678 ipseclog((LOG_DEBUG
, "key_msg2sp: Invalid msg length.\n"));
1683 if ((newsp
= key_newsp()) == NULL
) {
1688 newsp
->spidx
.dir
= xpl0
->sadb_x_policy_dir
;
1689 newsp
->policy
= xpl0
->sadb_x_policy_type
;
1692 switch (xpl0
->sadb_x_policy_type
) {
1693 case IPSEC_POLICY_DISCARD
:
1694 case IPSEC_POLICY_GENERATE
:
1695 case IPSEC_POLICY_NONE
:
1696 case IPSEC_POLICY_ENTRUST
:
1697 case IPSEC_POLICY_BYPASS
:
1701 case IPSEC_POLICY_IPSEC
:
1704 struct sadb_x_ipsecrequest
*xisr
;
1705 struct ipsecrequest
**p_isr
= &newsp
->req
;
1707 /* validity check */
1708 if (PFKEY_EXTLEN(xpl0
) < sizeof(*xpl0
)) {
1709 ipseclog((LOG_DEBUG
,
1710 "key_msg2sp: Invalid msg length.\n"));
1711 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1716 tlen
= PFKEY_EXTLEN(xpl0
) - sizeof(*xpl0
);
1717 xisr
= (struct sadb_x_ipsecrequest
*)(xpl0
+ 1);
1722 if (xisr
->sadb_x_ipsecrequest_len
< sizeof(*xisr
)) {
1723 ipseclog((LOG_DEBUG
, "key_msg2sp: "
1724 "invalid ipsecrequest length.\n"));
1725 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1730 /* allocate request buffer */
1731 KMALLOC_WAIT(*p_isr
, struct ipsecrequest
*, sizeof(**p_isr
));
1732 if ((*p_isr
) == NULL
) {
1733 ipseclog((LOG_DEBUG
,
1734 "key_msg2sp: No more memory.\n"));
1735 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1739 bzero(*p_isr
, sizeof(**p_isr
));
1742 (*p_isr
)->next
= NULL
;
1744 switch (xisr
->sadb_x_ipsecrequest_proto
) {
1747 case IPPROTO_IPCOMP
:
1750 ipseclog((LOG_DEBUG
,
1751 "key_msg2sp: invalid proto type=%u\n",
1752 xisr
->sadb_x_ipsecrequest_proto
));
1753 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1754 *error
= EPROTONOSUPPORT
;
1757 (*p_isr
)->saidx
.proto
= xisr
->sadb_x_ipsecrequest_proto
;
1759 switch (xisr
->sadb_x_ipsecrequest_mode
) {
1760 case IPSEC_MODE_TRANSPORT
:
1761 case IPSEC_MODE_TUNNEL
:
1763 case IPSEC_MODE_ANY
:
1765 ipseclog((LOG_DEBUG
,
1766 "key_msg2sp: invalid mode=%u\n",
1767 xisr
->sadb_x_ipsecrequest_mode
));
1768 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1772 (*p_isr
)->saidx
.mode
= xisr
->sadb_x_ipsecrequest_mode
;
1774 switch (xisr
->sadb_x_ipsecrequest_level
) {
1775 case IPSEC_LEVEL_DEFAULT
:
1776 case IPSEC_LEVEL_USE
:
1777 case IPSEC_LEVEL_REQUIRE
:
1779 case IPSEC_LEVEL_UNIQUE
:
1780 /* validity check */
1782 * If range violation of reqid, kernel will
1783 * update it, don't refuse it.
1785 if (xisr
->sadb_x_ipsecrequest_reqid
1786 > IPSEC_MANUAL_REQID_MAX
) {
1787 ipseclog((LOG_DEBUG
,
1788 "key_msg2sp: reqid=%d range "
1789 "violation, updated by kernel.\n",
1790 xisr
->sadb_x_ipsecrequest_reqid
));
1791 xisr
->sadb_x_ipsecrequest_reqid
= 0;
1794 /* allocate new reqid id if reqid is zero. */
1795 if (xisr
->sadb_x_ipsecrequest_reqid
== 0) {
1797 if ((reqid
= key_newreqid()) == 0) {
1798 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1802 (*p_isr
)->saidx
.reqid
= reqid
;
1803 xisr
->sadb_x_ipsecrequest_reqid
= reqid
;
1805 /* set it for manual keying. */
1806 (*p_isr
)->saidx
.reqid
=
1807 xisr
->sadb_x_ipsecrequest_reqid
;
1812 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid level=%u\n",
1813 xisr
->sadb_x_ipsecrequest_level
));
1814 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1818 (*p_isr
)->level
= xisr
->sadb_x_ipsecrequest_level
;
1820 /* set IP addresses if there */
1821 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
1822 struct sockaddr
*paddr
;
1824 paddr
= (struct sockaddr
*)(xisr
+ 1);
1826 /* validity check */
1828 > sizeof((*p_isr
)->saidx
.src
)) {
1829 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1830 "address length.\n"));
1831 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1835 bcopy(paddr
, &(*p_isr
)->saidx
.src
,
1838 paddr
= (struct sockaddr
*)((caddr_t
)paddr
1841 /* validity check */
1843 > sizeof((*p_isr
)->saidx
.dst
)) {
1844 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1845 "address length.\n"));
1846 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1850 bcopy(paddr
, &(*p_isr
)->saidx
.dst
,
1854 (*p_isr
)->sp
= newsp
;
1856 /* initialization for the next. */
1857 p_isr
= &(*p_isr
)->next
;
1858 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
1860 /* validity check */
1862 ipseclog((LOG_DEBUG
, "key_msg2sp: becoming tlen < 0.\n"));
1863 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1868 xisr
= (struct sadb_x_ipsecrequest
*)(void *)
1869 ((caddr_t
)xisr
+ xisr
->sadb_x_ipsecrequest_len
);
1874 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid policy type.\n"));
1875 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1887 lck_mtx_lock(sadb_mutex
);
1888 static u_int32_t auto_reqid
= IPSEC_MANUAL_REQID_MAX
+ 1;
1891 /* The reqid must be limited to 16 bits because the PF_KEY message format only uses
1892 16 bits for this field. Once it becomes larger than 16 bits - ipsec fails to
1893 work anymore. Changing the PF_KEY message format would introduce compatibility
1894 issues. This code now tests to see if the tentative reqid is in use */
1897 struct secpolicy
*sp
;
1898 struct ipsecrequest
*isr
;
1901 auto_reqid
= (auto_reqid
== 0xFFFF
1902 ? IPSEC_MANUAL_REQID_MAX
+ 1 : auto_reqid
+ 1);
1904 /* check for uniqueness */
1906 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
1907 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
1908 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1909 if (isr
->saidx
.reqid
== auto_reqid
) {
1922 lck_mtx_unlock(sadb_mutex
);
1927 * copy secpolicy struct to sadb_x_policy structure indicated.
1931 struct secpolicy
*sp
)
1933 struct sadb_x_policy
*xpl
;
1940 panic("key_sp2msg: NULL pointer was passed.\n");
1942 tlen
= key_getspreqmsglen(sp
);
1944 m
= key_alloc_mbuf(tlen
);
1945 if (!m
|| m
->m_next
) { /*XXX*/
1953 xpl
= mtod(m
, struct sadb_x_policy
*);
1956 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(tlen
);
1957 xpl
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1958 xpl
->sadb_x_policy_type
= sp
->policy
;
1959 xpl
->sadb_x_policy_dir
= sp
->spidx
.dir
;
1960 xpl
->sadb_x_policy_id
= sp
->id
;
1961 p
= (caddr_t
)xpl
+ sizeof(*xpl
);
1963 /* if is the policy for ipsec ? */
1964 if (sp
->policy
== IPSEC_POLICY_IPSEC
) {
1965 struct sadb_x_ipsecrequest
*xisr
;
1966 struct ipsecrequest
*isr
;
1968 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1970 xisr
= (struct sadb_x_ipsecrequest
*)(void *)p
;
1972 xisr
->sadb_x_ipsecrequest_proto
= isr
->saidx
.proto
;
1973 xisr
->sadb_x_ipsecrequest_mode
= isr
->saidx
.mode
;
1974 xisr
->sadb_x_ipsecrequest_level
= isr
->level
;
1975 xisr
->sadb_x_ipsecrequest_reqid
= isr
->saidx
.reqid
;
1978 bcopy(&isr
->saidx
.src
, p
, isr
->saidx
.src
.ss_len
);
1979 p
+= isr
->saidx
.src
.ss_len
;
1980 bcopy(&isr
->saidx
.dst
, p
, isr
->saidx
.dst
.ss_len
);
1981 p
+= isr
->saidx
.src
.ss_len
;
1983 xisr
->sadb_x_ipsecrequest_len
=
1984 PFKEY_ALIGN8(sizeof(*xisr
)
1985 + isr
->saidx
.src
.ss_len
1986 + isr
->saidx
.dst
.ss_len
);
1993 /* m will not be freed nor modified */
1994 static struct mbuf
*
1995 key_gather_mbuf(struct mbuf
*m
, const struct sadb_msghdr
*mhp
,
1996 int ndeep
, int nitem
, int *items
)
2000 struct mbuf
*result
= NULL
, *n
;
2003 if (m
== NULL
|| mhp
== NULL
)
2004 panic("null pointer passed to key_gather");
2006 for (i
= 0; i
< nitem
; i
++) {
2008 if (idx
< 0 || idx
> SADB_EXT_MAX
)
2010 /* don't attempt to pull empty extension */
2011 if (idx
== SADB_EXT_RESERVED
&& mhp
->msg
== NULL
)
2013 if (idx
!= SADB_EXT_RESERVED
&&
2014 (mhp
->ext
[idx
] == NULL
|| mhp
->extlen
[idx
] == 0))
2017 if (idx
== SADB_EXT_RESERVED
) {
2018 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2021 panic("assumption failed");
2023 MGETHDR(n
, M_WAITOK
, MT_DATA
);
2028 m_copydata(m
, 0, sizeof(struct sadb_msg
),
2030 } else if (i
< ndeep
) {
2031 len
= mhp
->extlen
[idx
];
2032 n
= key_alloc_mbuf(len
);
2033 if (!n
|| n
->m_next
) { /*XXX*/
2038 m_copydata(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
2041 n
= m_copym(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
2053 if ((result
->m_flags
& M_PKTHDR
) != 0) {
2054 result
->m_pkthdr
.len
= 0;
2055 for (n
= result
; n
; n
= n
->m_next
)
2056 result
->m_pkthdr
.len
+= n
->m_len
;
2067 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
2068 * add a entry to SP database, when received
2069 * <base, address(SD), (lifetime(H),) policy>
2071 * Adding to SP database,
2073 * <base, address(SD), (lifetime(H),) policy>
2074 * to the socket which was send.
2076 * SPDADD set a unique policy entry.
2077 * SPDSETIDX like SPDADD without a part of policy requests.
2078 * SPDUPDATE replace a unique policy entry.
2080 * m will always be freed.
2086 const struct sadb_msghdr
*mhp
)
2088 struct sadb_address
*src0
, *dst0
;
2089 struct sadb_x_policy
*xpl0
, *xpl
;
2090 struct sadb_lifetime
*lft
= NULL
;
2091 struct secpolicyindex spidx
;
2092 struct secpolicy
*newsp
;
2096 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2099 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2100 panic("key_spdadd: NULL pointer is passed.\n");
2102 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
2103 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
2104 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
2105 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2106 return key_senderror(so
, m
, EINVAL
);
2108 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
2109 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
2110 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2111 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2112 return key_senderror(so
, m
, EINVAL
);
2114 if (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
) {
2115 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
]
2116 < sizeof(struct sadb_lifetime
)) {
2117 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2118 return key_senderror(so
, m
, EINVAL
);
2120 lft
= (struct sadb_lifetime
*)
2121 (void *)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
2124 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
2125 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
2126 xpl0
= (struct sadb_x_policy
*)(void *)mhp
->ext
[SADB_X_EXT_POLICY
];
2129 /* XXX boundary check against sa_len */
2130 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2133 src0
->sadb_address_prefixlen
,
2134 dst0
->sadb_address_prefixlen
,
2135 src0
->sadb_address_proto
,
2138 /* checking the direciton. */
2139 switch (xpl0
->sadb_x_policy_dir
) {
2140 case IPSEC_DIR_INBOUND
:
2141 case IPSEC_DIR_OUTBOUND
:
2144 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid SP direction.\n"));
2145 mhp
->msg
->sadb_msg_errno
= EINVAL
;
2150 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
2151 if (xpl0
->sadb_x_policy_type
== IPSEC_POLICY_ENTRUST
2152 || xpl0
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
) {
2153 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid policy type.\n"));
2154 return key_senderror(so
, m
, EINVAL
);
2157 /* policy requests are mandatory when action is ipsec. */
2158 if (mhp
->msg
->sadb_msg_type
!= SADB_X_SPDSETIDX
2159 && xpl0
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
2160 && mhp
->extlen
[SADB_X_EXT_POLICY
] <= sizeof(*xpl0
)) {
2161 ipseclog((LOG_DEBUG
, "key_spdadd: some policy requests part required.\n"));
2162 return key_senderror(so
, m
, EINVAL
);
2166 * checking there is SP already or not.
2167 * SPDUPDATE doesn't depend on whether there is a SP or not.
2168 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
2171 lck_mtx_lock(sadb_mutex
);
2172 newsp
= key_getsp(&spidx
);
2173 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
2175 newsp
->state
= IPSEC_SPSTATE_DEAD
;
2176 key_freesp(newsp
, KEY_SADB_LOCKED
);
2179 if (newsp
!= NULL
) {
2180 key_freesp(newsp
, KEY_SADB_LOCKED
);
2181 ipseclog((LOG_DEBUG
, "key_spdadd: a SP entry exists already.\n"));
2182 lck_mtx_unlock(sadb_mutex
);
2183 return key_senderror(so
, m
, EEXIST
);
2186 lck_mtx_unlock(sadb_mutex
);
2187 /* allocation new SP entry */
2188 if ((newsp
= key_msg2sp(xpl0
, PFKEY_EXTLEN(xpl0
), &error
)) == NULL
) {
2189 return key_senderror(so
, m
, error
);
2192 if ((newsp
->id
= key_getnewspid()) == 0) {
2193 keydb_delsecpolicy(newsp
);
2194 return key_senderror(so
, m
, ENOBUFS
);
2197 /* XXX boundary check against sa_len */
2198 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2201 src0
->sadb_address_prefixlen
,
2202 dst0
->sadb_address_prefixlen
,
2203 src0
->sadb_address_proto
,
2206 /* sanity check on addr pair */
2207 if (((struct sockaddr
*)(src0
+ 1))->sa_family
!=
2208 ((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
2209 keydb_delsecpolicy(newsp
);
2210 return key_senderror(so
, m
, EINVAL
);
2212 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
2213 ((struct sockaddr
*)(dst0
+ 1))->sa_len
) {
2214 keydb_delsecpolicy(newsp
);
2215 return key_senderror(so
, m
, EINVAL
);
2219 * allow IPv6 over IPv4 tunnels using ESP -
2220 * otherwise reject if inner and outer address families not equal
2222 if (newsp
->req
&& newsp
->req
->saidx
.src
.ss_family
) {
2223 struct sockaddr
*sa
;
2224 sa
= (struct sockaddr
*)(src0
+ 1);
2225 if (sa
->sa_family
!= newsp
->req
->saidx
.src
.ss_family
) {
2226 if (newsp
->req
->saidx
.mode
!= IPSEC_MODE_TUNNEL
|| newsp
->req
->saidx
.proto
!= IPPROTO_ESP
2227 || sa
->sa_family
!= AF_INET6
|| newsp
->req
->saidx
.src
.ss_family
!= AF_INET
) {
2228 keydb_delsecpolicy(newsp
);
2229 return key_senderror(so
, m
, EINVAL
);
2233 if (newsp
->req
&& newsp
->req
->saidx
.dst
.ss_family
) {
2234 struct sockaddr
*sa
;
2235 sa
= (struct sockaddr
*)(dst0
+ 1);
2236 if (sa
->sa_family
!= newsp
->req
->saidx
.dst
.ss_family
) {
2237 if (newsp
->req
->saidx
.mode
!= IPSEC_MODE_TUNNEL
|| newsp
->req
->saidx
.proto
!= IPPROTO_ESP
2238 || sa
->sa_family
!= AF_INET6
|| newsp
->req
->saidx
.dst
.ss_family
!= AF_INET
) {
2239 keydb_delsecpolicy(newsp
);
2240 return key_senderror(so
, m
, EINVAL
);
2247 newsp
->created
= tv
.tv_sec
;
2248 newsp
->lastused
= tv
.tv_sec
;
2249 newsp
->lifetime
= lft
? lft
->sadb_lifetime_addtime
: 0;
2250 newsp
->validtime
= lft
? lft
->sadb_lifetime_usetime
: 0;
2252 newsp
->refcnt
= 1; /* do not reclaim until I say I do */
2253 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
2254 lck_mtx_lock(sadb_mutex
);
2256 * policies of type generate should be at the end of the SPD
2257 * because they function as default discard policies
2259 if (newsp
->policy
== IPSEC_POLICY_GENERATE
)
2260 LIST_INSERT_TAIL(&sptree
[newsp
->spidx
.dir
], newsp
, secpolicy
, chain
);
2261 else { /* XXX until we have policy ordering in the kernel */
2262 struct secpolicy
*tmpsp
;
2264 LIST_FOREACH(tmpsp
, &sptree
[newsp
->spidx
.dir
], chain
)
2265 if (tmpsp
->policy
== IPSEC_POLICY_GENERATE
)
2268 LIST_INSERT_BEFORE(tmpsp
, newsp
, chain
);
2270 LIST_INSERT_TAIL(&sptree
[newsp
->spidx
.dir
], newsp
, secpolicy
, chain
);
2273 ipsec_policy_count
++;
2274 /* Turn off the ipsec bypass */
2275 if (ipsec_bypass
!= 0)
2278 /* delete the entry in spacqtree */
2279 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
2280 struct secspacq
*spacq
;
2281 if ((spacq
= key_getspacq(&spidx
)) != NULL
) {
2282 /* reset counter in order to deletion by timehandler. */
2284 spacq
->created
= tv
.tv_sec
;
2288 lck_mtx_unlock(sadb_mutex
);
2291 struct mbuf
*n
, *mpolicy
;
2292 struct sadb_msg
*newmsg
;
2295 /* create new sadb_msg to reply. */
2297 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2298 SADB_EXT_LIFETIME_HARD
, SADB_EXT_ADDRESS_SRC
,
2299 SADB_EXT_ADDRESS_DST
};
2300 n
= key_gather_mbuf(m
, mhp
, 2, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2302 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2303 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
2304 n
= key_gather_mbuf(m
, mhp
, 2, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2307 return key_senderror(so
, m
, ENOBUFS
);
2309 if (n
->m_len
< sizeof(*newmsg
)) {
2310 n
= m_pullup(n
, sizeof(*newmsg
));
2312 return key_senderror(so
, m
, ENOBUFS
);
2314 newmsg
= mtod(n
, struct sadb_msg
*);
2315 newmsg
->sadb_msg_errno
= 0;
2316 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2319 mpolicy
= m_pulldown(n
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)),
2320 sizeof(*xpl
), &off
);
2321 if (mpolicy
== NULL
) {
2322 /* n is already freed */
2323 return key_senderror(so
, m
, ENOBUFS
);
2325 xpl
= (struct sadb_x_policy
*)(void *)(mtod(mpolicy
, caddr_t
) + off
);
2326 if (xpl
->sadb_x_policy_exttype
!= SADB_X_EXT_POLICY
) {
2328 return key_senderror(so
, m
, EINVAL
);
2330 xpl
->sadb_x_policy_id
= newsp
->id
;
2333 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2338 * get new policy id.
2344 key_getnewspid(void)
2346 u_int32_t newid
= 0;
2347 int count
= key_spi_trycnt
; /* XXX */
2348 struct secpolicy
*sp
;
2350 /* when requesting to allocate spi ranged */
2351 lck_mtx_lock(sadb_mutex
);
2353 newid
= (policy_id
= (policy_id
== ~0 ? 1 : policy_id
+ 1));
2355 if ((sp
= key_getspbyid(newid
)) == NULL
)
2358 key_freesp(sp
, KEY_SADB_LOCKED
);
2360 lck_mtx_unlock(sadb_mutex
);
2361 if (count
== 0 || newid
== 0) {
2362 ipseclog((LOG_DEBUG
, "key_getnewspid: to allocate policy id is failed.\n"));
2370 * SADB_SPDDELETE processing
2372 * <base, address(SD), policy(*)>
2373 * from the user(?), and set SADB_SASTATE_DEAD,
2375 * <base, address(SD), policy(*)>
2377 * policy(*) including direction of policy.
2379 * m will always be freed.
2385 const struct sadb_msghdr
*mhp
)
2387 struct sadb_address
*src0
, *dst0
;
2388 struct sadb_x_policy
*xpl0
;
2389 struct secpolicyindex spidx
;
2390 struct secpolicy
*sp
;
2392 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2395 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2396 panic("key_spddelete: NULL pointer is passed.\n");
2398 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
2399 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
2400 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
2401 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
2402 return key_senderror(so
, m
, EINVAL
);
2404 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
2405 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
2406 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2407 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
2408 return key_senderror(so
, m
, EINVAL
);
2411 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
2412 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
2413 xpl0
= (struct sadb_x_policy
*)(void *)mhp
->ext
[SADB_X_EXT_POLICY
];
2416 /* XXX boundary check against sa_len */
2417 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2420 src0
->sadb_address_prefixlen
,
2421 dst0
->sadb_address_prefixlen
,
2422 src0
->sadb_address_proto
,
2425 /* checking the direciton. */
2426 switch (xpl0
->sadb_x_policy_dir
) {
2427 case IPSEC_DIR_INBOUND
:
2428 case IPSEC_DIR_OUTBOUND
:
2431 ipseclog((LOG_DEBUG
, "key_spddelete: Invalid SP direction.\n"));
2432 return key_senderror(so
, m
, EINVAL
);
2435 /* Is there SP in SPD ? */
2436 lck_mtx_lock(sadb_mutex
);
2437 if ((sp
= key_getsp(&spidx
)) == NULL
) {
2438 ipseclog((LOG_DEBUG
, "key_spddelete: no SP found.\n"));
2439 lck_mtx_unlock(sadb_mutex
);
2440 return key_senderror(so
, m
, EINVAL
);
2443 /* save policy id to buffer to be returned. */
2444 xpl0
->sadb_x_policy_id
= sp
->id
;
2446 sp
->state
= IPSEC_SPSTATE_DEAD
;
2447 key_freesp(sp
, KEY_SADB_LOCKED
);
2448 lck_mtx_unlock(sadb_mutex
);
2453 struct sadb_msg
*newmsg
;
2454 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2455 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
2457 /* create new sadb_msg to reply. */
2458 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2460 return key_senderror(so
, m
, ENOBUFS
);
2462 newmsg
= mtod(n
, struct sadb_msg
*);
2463 newmsg
->sadb_msg_errno
= 0;
2464 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2467 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2472 * SADB_SPDDELETE2 processing
2475 * from the user(?), and set SADB_SASTATE_DEAD,
2479 * policy(*) including direction of policy.
2481 * m will always be freed.
2487 const struct sadb_msghdr
*mhp
)
2490 struct secpolicy
*sp
;
2492 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2495 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2496 panic("key_spddelete2: NULL pointer is passed.\n");
2498 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2499 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2500 ipseclog((LOG_DEBUG
, "key_spddelete2: invalid message is passed.\n"));
2501 key_senderror(so
, m
, EINVAL
);
2505 id
= ((struct sadb_x_policy
*)
2506 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2508 /* Is there SP in SPD ? */
2509 lck_mtx_lock(sadb_mutex
);
2510 if ((sp
= key_getspbyid(id
)) == NULL
) {
2511 lck_mtx_unlock(sadb_mutex
);
2512 ipseclog((LOG_DEBUG
, "key_spddelete2: no SP found id:%u.\n", id
));
2513 return key_senderror(so
, m
, EINVAL
);
2516 sp
->state
= IPSEC_SPSTATE_DEAD
;
2517 key_freesp(sp
, KEY_SADB_LOCKED
);
2518 lck_mtx_unlock(sadb_mutex
);
2521 struct mbuf
*n
, *nn
;
2522 struct sadb_msg
*newmsg
;
2525 /* create new sadb_msg to reply. */
2526 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2529 return key_senderror(so
, m
, ENOBUFS
);
2530 MGETHDR(n
, M_WAITOK
, MT_DATA
);
2531 if (n
&& len
> MHLEN
) {
2532 MCLGET(n
, M_WAITOK
);
2533 if ((n
->m_flags
& M_EXT
) == 0) {
2539 return key_senderror(so
, m
, ENOBUFS
);
2545 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
2546 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2550 panic("length inconsistency in key_spddelete2");
2553 n
->m_next
= m_copym(m
, mhp
->extoff
[SADB_X_EXT_POLICY
],
2554 mhp
->extlen
[SADB_X_EXT_POLICY
], M_WAITOK
);
2557 return key_senderror(so
, m
, ENOBUFS
);
2560 n
->m_pkthdr
.len
= 0;
2561 for (nn
= n
; nn
; nn
= nn
->m_next
)
2562 n
->m_pkthdr
.len
+= nn
->m_len
;
2564 newmsg
= mtod(n
, struct sadb_msg
*);
2565 newmsg
->sadb_msg_errno
= 0;
2566 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2569 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2574 * SADB_X_GET processing
2579 * <base, address(SD), policy>
2581 * policy(*) including direction of policy.
2583 * m will always be freed.
2589 const struct sadb_msghdr
*mhp
)
2592 struct secpolicy
*sp
;
2595 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2598 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2599 panic("key_spdget: NULL pointer is passed.\n");
2601 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2602 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2603 ipseclog((LOG_DEBUG
, "key_spdget: invalid message is passed.\n"));
2604 return key_senderror(so
, m
, EINVAL
);
2607 id
= ((struct sadb_x_policy
*)
2608 (void *)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2610 /* Is there SP in SPD ? */
2611 lck_mtx_lock(sadb_mutex
);
2612 if ((sp
= key_getspbyid(id
)) == NULL
) {
2613 ipseclog((LOG_DEBUG
, "key_spdget: no SP found id:%u.\n", id
));
2614 lck_mtx_unlock(sadb_mutex
);
2615 return key_senderror(so
, m
, ENOENT
);
2617 lck_mtx_unlock(sadb_mutex
);
2618 n
= key_setdumpsp(sp
, SADB_X_SPDGET
, 0, mhp
->msg
->sadb_msg_pid
);
2621 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
2623 return key_senderror(so
, m
, ENOBUFS
);
2627 * SADB_X_SPDACQUIRE processing.
2628 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2631 * to KMD, and expect to receive
2632 * <base> with SADB_X_SPDACQUIRE if error occurred,
2635 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2636 * policy(*) is without policy requests.
2639 * others: error number
2643 struct secpolicy
*sp
)
2645 struct mbuf
*result
= NULL
, *m
;
2646 struct secspacq
*newspacq
;
2649 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2653 panic("key_spdacquire: NULL pointer is passed.\n");
2654 if (sp
->req
!= NULL
)
2655 panic("key_spdacquire: called but there is request.\n");
2656 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
2657 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
2659 /* get a entry to check whether sent message or not. */
2660 lck_mtx_lock(sadb_mutex
);
2661 if ((newspacq
= key_getspacq(&sp
->spidx
)) != NULL
) {
2662 if (key_blockacq_count
< newspacq
->count
) {
2663 /* reset counter and do send message. */
2664 newspacq
->count
= 0;
2666 /* increment counter and do nothing. */
2668 lck_mtx_unlock(sadb_mutex
);
2672 /* make new entry for blocking to send SADB_ACQUIRE. */
2673 if ((newspacq
= key_newspacq(&sp
->spidx
)) == NULL
) {
2674 lck_mtx_unlock(sadb_mutex
);
2677 /* add to acqtree */
2678 LIST_INSERT_HEAD(&spacqtree
, newspacq
, chain
);
2680 lck_mtx_unlock(sadb_mutex
);
2681 /* create new sadb_msg to reply. */
2682 m
= key_setsadbmsg(SADB_X_SPDACQUIRE
, 0, 0, 0, 0, 0);
2689 result
->m_pkthdr
.len
= 0;
2690 for (m
= result
; m
; m
= m
->m_next
)
2691 result
->m_pkthdr
.len
+= m
->m_len
;
2693 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2694 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2696 return key_sendup_mbuf(NULL
, m
, KEY_SENDUP_REGISTERED
);
2705 * SADB_SPDFLUSH processing
2708 * from the user, and free all entries in secpctree.
2712 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2714 * m will always be freed.
2720 const struct sadb_msghdr
*mhp
)
2722 struct sadb_msg
*newmsg
;
2723 struct secpolicy
*sp
;
2727 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2728 panic("key_spdflush: NULL pointer is passed.\n");
2730 if (m
->m_len
!= PFKEY_ALIGN8(sizeof(struct sadb_msg
)))
2731 return key_senderror(so
, m
, EINVAL
);
2733 lck_mtx_lock(sadb_mutex
);
2734 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2735 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2736 sp
->state
= IPSEC_SPSTATE_DEAD
;
2739 lck_mtx_unlock(sadb_mutex
);
2741 if (sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
2742 ipseclog((LOG_DEBUG
, "key_spdflush: No more memory.\n"));
2743 return key_senderror(so
, m
, ENOBUFS
);
2749 m
->m_pkthdr
.len
= m
->m_len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2750 newmsg
= mtod(m
, struct sadb_msg
*);
2751 newmsg
->sadb_msg_errno
= 0;
2752 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
2754 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
2758 * SADB_SPDDUMP processing
2761 * from the user, and dump all SP leaves
2766 * m will always be freed.
2773 const struct sadb_msghdr
*mhp
)
2775 struct secpolicy
*sp
, **spbuf
= NULL
, **sp_ptr
;
2776 int cnt
= 0, bufcount
;
2782 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2783 panic("key_spddump: NULL pointer is passed.\n");
2785 if ((bufcount
= ipsec_policy_count
) == 0) {
2789 bufcount
+= 256; /* extra */
2790 KMALLOC_WAIT(spbuf
, struct secpolicy
**, bufcount
* sizeof(struct secpolicy
*));
2791 if (spbuf
== NULL
) {
2792 ipseclog((LOG_DEBUG
, "key_spddump: No more memory.\n"));
2796 lck_mtx_lock(sadb_mutex
);
2797 /* search SPD entry, make list. */
2799 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2800 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2801 if (cnt
== bufcount
)
2802 break; /* buffer full */
2808 lck_mtx_unlock(sadb_mutex
);
2818 n
= key_setdumpsp(*sp_ptr
++, SADB_X_SPDDUMP
, cnt
,
2819 mhp
->msg
->sadb_msg_pid
);
2822 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
2825 lck_mtx_lock(sadb_mutex
);
2826 while (sp_ptr
> spbuf
)
2827 key_freesp(*(--sp_ptr
), KEY_SADB_LOCKED
);
2828 lck_mtx_unlock(sadb_mutex
);
2834 return key_senderror(so
, m
, error
);
2841 static struct mbuf
*
2843 struct secpolicy
*sp
,
2848 struct mbuf
*result
= NULL
, *m
;
2850 m
= key_setsadbmsg(type
, 0, SADB_SATYPE_UNSPEC
, seq
, pid
, sp
->refcnt
);
2855 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
2856 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
2857 sp
->spidx
.ul_proto
);
2862 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
2863 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
2864 sp
->spidx
.ul_proto
);
2874 if ((result
->m_flags
& M_PKTHDR
) == 0)
2877 if (result
->m_len
< sizeof(struct sadb_msg
)) {
2878 result
= m_pullup(result
, sizeof(struct sadb_msg
));
2883 result
->m_pkthdr
.len
= 0;
2884 for (m
= result
; m
; m
= m
->m_next
)
2885 result
->m_pkthdr
.len
+= m
->m_len
;
2887 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2888 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2898 * get PFKEY message length for security policy and request.
2902 struct secpolicy
*sp
)
2906 tlen
= sizeof(struct sadb_x_policy
);
2908 /* if is the policy for ipsec ? */
2909 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
2912 /* get length of ipsec requests */
2914 struct ipsecrequest
*isr
;
2917 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2918 len
= sizeof(struct sadb_x_ipsecrequest
)
2919 + isr
->saidx
.src
.ss_len
2920 + isr
->saidx
.dst
.ss_len
;
2922 tlen
+= PFKEY_ALIGN8(len
);
2930 * SADB_SPDEXPIRE processing
2932 * <base, address(SD), lifetime(CH), policy>
2936 * others : error number
2940 struct secpolicy
*sp
)
2942 struct mbuf
*result
= NULL
, *m
;
2945 struct sadb_lifetime
*lt
;
2947 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2951 panic("key_spdexpire: NULL pointer is passed.\n");
2953 /* set msg header */
2954 m
= key_setsadbmsg(SADB_X_SPDEXPIRE
, 0, 0, 0, 0, 0);
2961 /* create lifetime extension (current and hard) */
2962 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
2963 m
= key_alloc_mbuf(len
);
2964 if (!m
|| m
->m_next
) { /*XXX*/
2970 bzero(mtod(m
, caddr_t
), len
);
2971 lt
= mtod(m
, struct sadb_lifetime
*);
2972 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
2973 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
2974 lt
->sadb_lifetime_allocations
= 0;
2975 lt
->sadb_lifetime_bytes
= 0;
2976 lt
->sadb_lifetime_addtime
= sp
->created
;
2977 lt
->sadb_lifetime_usetime
= sp
->lastused
;
2978 lt
= (struct sadb_lifetime
*)(void *)(mtod(m
, caddr_t
) + len
/ 2);
2979 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
2980 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
2981 lt
->sadb_lifetime_allocations
= 0;
2982 lt
->sadb_lifetime_bytes
= 0;
2983 lt
->sadb_lifetime_addtime
= sp
->lifetime
;
2984 lt
->sadb_lifetime_usetime
= sp
->validtime
;
2987 /* set sadb_address for source */
2988 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
2989 (struct sockaddr
*)&sp
->spidx
.src
,
2990 sp
->spidx
.prefs
, sp
->spidx
.ul_proto
);
2997 /* set sadb_address for destination */
2998 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
2999 (struct sockaddr
*)&sp
->spidx
.dst
,
3000 sp
->spidx
.prefd
, sp
->spidx
.ul_proto
);
3015 if ((result
->m_flags
& M_PKTHDR
) == 0) {
3020 if (result
->m_len
< sizeof(struct sadb_msg
)) {
3021 result
= m_pullup(result
, sizeof(struct sadb_msg
));
3022 if (result
== NULL
) {
3028 result
->m_pkthdr
.len
= 0;
3029 for (m
= result
; m
; m
= m
->m_next
)
3030 result
->m_pkthdr
.len
+= m
->m_len
;
3032 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3033 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3035 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
3043 /* %%% SAD management */
3045 * allocating a memory for new SA head, and copy from the values of mhp.
3046 * OUT: NULL : failure due to the lack of memory.
3047 * others : pointer to new SA head.
3049 static struct secashead
*
3051 struct secasindex
*saidx
,
3054 struct secashead
*newsah
;
3058 panic("key_newsaidx: NULL pointer is passed.\n");
3060 newsah
= keydb_newsecashead();
3064 bcopy(saidx
, &newsah
->saidx
, sizeof(newsah
->saidx
));
3066 /* remove the ports */
3067 switch (saidx
->src
.ss_family
) {
3069 ((struct sockaddr_in
*)(&newsah
->saidx
.src
))->sin_port
= IPSEC_PORT_ANY
;
3072 ((struct sockaddr_in6
*)(&newsah
->saidx
.src
))->sin6_port
= IPSEC_PORT_ANY
;
3077 switch (saidx
->dst
.ss_family
) {
3079 ((struct sockaddr_in
*)(&newsah
->saidx
.dst
))->sin_port
= IPSEC_PORT_ANY
;
3082 ((struct sockaddr_in6
*)(&newsah
->saidx
.dst
))->sin6_port
= IPSEC_PORT_ANY
;
3089 /* add to saidxtree */
3090 newsah
->state
= SADB_SASTATE_MATURE
;
3091 LIST_INSERT_HEAD(&sahtree
, newsah
, chain
);
3097 * delete SA index and all SA registerd.
3101 struct secashead
*sah
)
3103 struct secasvar
*sav
, *nextsav
;
3104 u_int stateidx
, state
;
3107 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3111 panic("key_delsah: NULL pointer is passed.\n");
3113 /* searching all SA registerd in the secindex. */
3115 stateidx
< _ARRAYLEN(saorder_state_any
);
3118 state
= saorder_state_any
[stateidx
];
3119 for (sav
= (struct secasvar
*)LIST_FIRST(&sah
->savtree
[state
]);
3123 nextsav
= LIST_NEXT(sav
, chain
);
3125 if (sav
->refcnt
> 0) {
3126 /* give up to delete this sa */
3132 KEY_CHKSASTATE(state
, sav
->state
, "key_delsah");
3134 key_freesav(sav
, KEY_SADB_LOCKED
);
3136 /* remove back pointer */
3142 /* don't delete sah only if there are savs. */
3146 if (sah
->sa_route
.ro_rt
) {
3147 rtfree(sah
->sa_route
.ro_rt
);
3148 sah
->sa_route
.ro_rt
= (struct rtentry
*)NULL
;
3151 /* remove from tree of SA index */
3152 if (__LIST_CHAINED(sah
))
3153 LIST_REMOVE(sah
, chain
);
3161 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
3162 * and copy the values of mhp into new buffer.
3163 * When SAD message type is GETSPI:
3164 * to set sequence number from acq_seq++,
3165 * to set zero to SPI.
3166 * not to call key_setsava().
3168 * others : pointer to new secasvar.
3170 * does not modify mbuf. does not free mbuf on error.
3172 static struct secasvar
*
3175 const struct sadb_msghdr
*mhp
,
3176 struct secashead
*sah
,
3179 struct secasvar
*newsav
;
3180 const struct sadb_sa
*xsa
;
3182 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3185 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
|| sah
== NULL
)
3186 panic("key_newsa: NULL pointer is passed.\n");
3188 KMALLOC_NOWAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3189 if (newsav
== NULL
) {
3190 lck_mtx_unlock(sadb_mutex
);
3191 KMALLOC_WAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3192 lck_mtx_lock(sadb_mutex
);
3193 if (newsav
== NULL
) {
3194 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3199 bzero((caddr_t
)newsav
, sizeof(struct secasvar
));
3201 switch (mhp
->msg
->sadb_msg_type
) {
3203 key_setspi(newsav
, 0);
3205 #if IPSEC_DOSEQCHECK
3206 /* sync sequence number */
3207 if (mhp
->msg
->sadb_msg_seq
== 0)
3209 (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
3212 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
3217 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
3219 ipseclog((LOG_DEBUG
, "key_newsa: invalid message is passed.\n"));
3223 xsa
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
3224 key_setspi(newsav
, xsa
->sadb_sa_spi
);
3225 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
3233 /* copy sav values */
3234 if (mhp
->msg
->sadb_msg_type
!= SADB_GETSPI
) {
3235 *errp
= key_setsaval(newsav
, m
, mhp
);
3237 if (newsav
->spihash
.le_prev
|| newsav
->spihash
.le_next
)
3238 LIST_REMOVE(newsav
, spihash
);
3248 newsav
->created
= tv
.tv_sec
;
3251 newsav
->pid
= mhp
->msg
->sadb_msg_pid
;
3256 newsav
->state
= SADB_SASTATE_LARVAL
;
3257 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
3265 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
3266 * and copy the values passed into new buffer.
3267 * When SAD message type is GETSPI:
3268 * to set sequence number from acq_seq++,
3269 * to set zero to SPI.
3270 * not to call key_setsava().
3272 * others : pointer to new secasvar.
3275 key_newsav2(struct secashead
*sah
,
3281 struct sadb_key
*key_auth
,
3282 u_int16_t key_auth_len
,
3283 struct sadb_key
*key_enc
,
3284 u_int16_t key_enc_len
,
3285 u_int16_t natt_port
,
3289 struct sadb_lifetime
*lifetime_hard
,
3290 struct sadb_lifetime
*lifetime_soft
)
3292 struct secasvar
*newsav
;
3294 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3298 panic("key_newsa: NULL pointer is passed.\n");
3300 KMALLOC_NOWAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3301 if (newsav
== NULL
) {
3302 lck_mtx_unlock(sadb_mutex
);
3303 KMALLOC_WAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3304 lck_mtx_lock(sadb_mutex
);
3305 if (newsav
== NULL
) {
3306 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3310 bzero((caddr_t
)newsav
, sizeof(struct secasvar
));
3312 #if IPSEC_DOSEQCHECK
3313 /* sync sequence number */
3315 newsav
->seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
3319 key_setspi(newsav
, spi
);
3321 if (key_setsaval2(newsav
,
3337 if (newsav
->spihash
.le_prev
|| newsav
->spihash
.le_next
)
3338 LIST_REMOVE(newsav
, spihash
);
3347 newsav
->created
= tv
.tv_sec
;
3355 if (spi
&& key_auth
&& key_auth_len
&& key_enc
&& key_enc_len
) {
3356 newsav
->state
= SADB_SASTATE_MATURE
;
3357 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_MATURE
], newsav
,
3360 newsav
->state
= SADB_SASTATE_LARVAL
;
3361 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
3370 * free() SA variable entry.
3374 struct secasvar
*sav
)
3377 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3381 panic("key_delsav: NULL pointer is passed.\n");
3383 if (sav
->refcnt
> 0)
3384 return; /* can't free */
3386 /* remove from SA header */
3387 if (__LIST_CHAINED(sav
))
3388 LIST_REMOVE(sav
, chain
);
3391 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
)
3392 LIST_REMOVE(sav
, spihash
);
3394 if (sav
->key_auth
!= NULL
) {
3395 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
3396 KFREE(sav
->key_auth
);
3397 sav
->key_auth
= NULL
;
3399 if (sav
->key_enc
!= NULL
) {
3400 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
3401 KFREE(sav
->key_enc
);
3402 sav
->key_enc
= NULL
;
3405 bzero(sav
->sched
, sav
->schedlen
);
3409 if (sav
->replay
!= NULL
) {
3410 keydb_delsecreplay(sav
->replay
);
3413 if (sav
->lft_c
!= NULL
) {
3417 if (sav
->lft_h
!= NULL
) {
3421 if (sav
->lft_s
!= NULL
) {
3425 if (sav
->iv
!= NULL
) {
3439 * others : found, pointer to a SA.
3441 static struct secashead
*
3443 struct secasindex
*saidx
)
3445 struct secashead
*sah
;
3447 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3449 LIST_FOREACH(sah
, &sahtree
, chain
) {
3450 if (sah
->state
== SADB_SASTATE_DEAD
)
3452 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_REQID
))
3460 key_newsah2 (struct secasindex
*saidx
,
3463 struct secashead
*sah
;
3465 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3467 sah
= key_getsah(saidx
);
3469 return(key_newsah(saidx
, dir
));
3475 * check not to be duplicated SPI.
3476 * NOTE: this function is too slow due to searching all SAD.
3479 * others : found, pointer to a SA.
3481 static struct secasvar
*
3483 struct secasindex
*saidx
,
3486 struct secasvar
*sav
;
3487 u_int stateidx
, state
;
3489 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3491 /* check address family */
3492 if (saidx
->src
.ss_family
!= saidx
->dst
.ss_family
) {
3493 ipseclog((LOG_DEBUG
, "key_checkspidup: address family mismatched.\n"));
3498 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
3499 if (sav
->spi
!= spi
)
3502 stateidx
< _ARRAYLEN(saorder_state_alive
);
3504 state
= saorder_state_alive
[stateidx
];
3505 if (sav
->state
== state
&&
3506 key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
))
3516 struct secasvar
*sav
,
3519 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3521 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
)
3522 LIST_REMOVE(sav
, spihash
);
3523 LIST_INSERT_HEAD(&spihash
[SPIHASH(spi
)], sav
, spihash
);
3528 * search SAD litmited alive SA, protocol, SPI.
3531 * others : found, pointer to a SA.
3533 static struct secasvar
*
3535 struct secashead
*sah
,
3538 struct secasvar
*sav
, *match
;
3539 u_int stateidx
, state
, matchidx
;
3541 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3543 matchidx
= _ARRAYLEN(saorder_state_alive
);
3544 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
3545 if (sav
->spi
!= spi
)
3547 if (sav
->sah
!= sah
)
3549 for (stateidx
= 0; stateidx
< matchidx
; stateidx
++) {
3550 state
= saorder_state_alive
[stateidx
];
3551 if (sav
->state
== state
) {
3553 matchidx
= stateidx
;
3563 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
3564 * You must update these if need.
3568 * does not modify mbuf. does not free mbuf on error.
3572 struct secasvar
*sav
,
3574 const struct sadb_msghdr
*mhp
)
3577 const struct esp_algorithm
*algo
;
3582 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3585 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
3586 panic("key_setsaval: NULL pointer is passed.\n");
3588 /* initialization */
3590 sav
->key_auth
= NULL
;
3591 sav
->key_enc
= NULL
;
3598 sav
->remote_ike_port
= 0;
3599 sav
->natt_last_activity
= natt_now
;
3600 sav
->natt_encapsulated_src_port
= 0;
3603 if (mhp
->ext
[SADB_EXT_SA
] != NULL
) {
3604 const struct sadb_sa
*sa0
;
3606 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
3607 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(*sa0
)) {
3608 ipseclog((LOG_DEBUG
, "key_setsaval: invalid message size.\n"));
3613 sav
->alg_auth
= sa0
->sadb_sa_auth
;
3614 sav
->alg_enc
= sa0
->sadb_sa_encrypt
;
3615 sav
->flags
= sa0
->sadb_sa_flags
;
3618 * Verify that a nat-traversal port was specified if
3619 * the nat-traversal flag is set.
3621 if ((sav
->flags
& SADB_X_EXT_NATT
) != 0) {
3622 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa_2
) ||
3623 ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
== 0) {
3624 ipseclog((LOG_DEBUG
, "key_setsaval: natt port not set.\n"));
3628 sav
->remote_ike_port
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
;
3632 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
3633 * SADB_X_EXT_NATT is set and SADB_X_EXT_NATT_KEEPALIVE is not
3634 * set (we're not behind nat) - otherwise clear it.
3636 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
3637 if ((sav
->flags
& SADB_X_EXT_NATT
) == 0 ||
3638 (sav
->flags
& SADB_X_EXT_NATT_KEEPALIVE
) != 0)
3639 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
3642 if ((sa0
->sadb_sa_flags
& SADB_X_EXT_OLD
) == 0) {
3643 sav
->replay
= keydb_newsecreplay(sa0
->sadb_sa_replay
);
3644 if (sav
->replay
== NULL
) {
3645 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3652 /* Authentication keys */
3653 if (mhp
->ext
[SADB_EXT_KEY_AUTH
] != NULL
) {
3654 const struct sadb_key
*key0
;
3657 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_AUTH
];
3658 len
= mhp
->extlen
[SADB_EXT_KEY_AUTH
];
3661 if (len
< sizeof(*key0
)) {
3662 ipseclog((LOG_DEBUG
, "key_setsaval: invalid auth key ext len. len = %d\n", len
));
3666 switch (mhp
->msg
->sadb_msg_satype
) {
3667 case SADB_SATYPE_AH
:
3668 case SADB_SATYPE_ESP
:
3669 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
3670 sav
->alg_auth
!= SADB_X_AALG_NULL
)
3673 case SADB_X_SATYPE_IPCOMP
:
3679 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_auth values.\n"));
3683 sav
->key_auth
= (struct sadb_key
*)key_newbuf(key0
, len
);
3684 if (sav
->key_auth
== NULL
) {
3685 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3691 /* Encryption key */
3692 if (mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] != NULL
) {
3693 const struct sadb_key
*key0
;
3696 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_ENCRYPT
];
3697 len
= mhp
->extlen
[SADB_EXT_KEY_ENCRYPT
];
3700 if (len
< sizeof(*key0
)) {
3701 ipseclog((LOG_DEBUG
, "key_setsaval: invalid encryption key ext len. len = %d\n", len
));
3705 switch (mhp
->msg
->sadb_msg_satype
) {
3706 case SADB_SATYPE_ESP
:
3707 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
3708 sav
->alg_enc
!= SADB_EALG_NULL
) {
3709 ipseclog((LOG_DEBUG
, "key_setsaval: invalid ESP algorithm.\n"));
3713 sav
->key_enc
= (struct sadb_key
*)key_newbuf(key0
, len
);
3714 if (sav
->key_enc
== NULL
) {
3715 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3720 case SADB_X_SATYPE_IPCOMP
:
3721 if (len
!= PFKEY_ALIGN8(sizeof(struct sadb_key
)))
3723 sav
->key_enc
= NULL
; /*just in case*/
3725 case SADB_SATYPE_AH
:
3731 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_enc value.\n"));
3739 switch (mhp
->msg
->sadb_msg_satype
) {
3740 case SADB_SATYPE_ESP
:
3742 algo
= esp_algorithm_lookup(sav
->alg_enc
);
3743 if (algo
&& algo
->ivlen
)
3744 sav
->ivlen
= (*algo
->ivlen
)(algo
, sav
);
3745 if (sav
->ivlen
== 0)
3747 KMALLOC_NOWAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
3749 lck_mtx_unlock(sadb_mutex
);
3750 KMALLOC_WAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
3751 lck_mtx_lock(sadb_mutex
);
3753 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3760 key_randomfill(sav
->iv
, sav
->ivlen
);
3763 case SADB_SATYPE_AH
:
3764 case SADB_X_SATYPE_IPCOMP
:
3767 ipseclog((LOG_DEBUG
, "key_setsaval: invalid SA type.\n"));
3774 sav
->created
= tv
.tv_sec
;
3776 /* make lifetime for CURRENT */
3777 KMALLOC_NOWAIT(sav
->lft_c
, struct sadb_lifetime
*,
3778 sizeof(struct sadb_lifetime
));
3779 if (sav
->lft_c
== NULL
) {
3780 lck_mtx_unlock(sadb_mutex
);
3781 KMALLOC_WAIT(sav
->lft_c
, struct sadb_lifetime
*,
3782 sizeof(struct sadb_lifetime
));
3783 lck_mtx_lock(sadb_mutex
);
3784 if (sav
->lft_c
== NULL
) {
3785 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3793 sav
->lft_c
->sadb_lifetime_len
=
3794 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3795 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
3796 sav
->lft_c
->sadb_lifetime_allocations
= 0;
3797 sav
->lft_c
->sadb_lifetime_bytes
= 0;
3798 sav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
3799 sav
->lft_c
->sadb_lifetime_usetime
= 0;
3801 /* lifetimes for HARD and SOFT */
3803 const struct sadb_lifetime
*lft0
;
3805 lft0
= (struct sadb_lifetime
*)
3806 (void *)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
3808 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
] < sizeof(*lft0
)) {
3809 ipseclog((LOG_DEBUG
, "key_setsaval: invalid hard lifetime ext len.\n"));
3813 sav
->lft_h
= (struct sadb_lifetime
*)key_newbuf(lft0
,
3815 if (sav
->lft_h
== NULL
) {
3816 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3820 /* to be initialize ? */
3823 lft0
= (struct sadb_lifetime
*)
3824 (void *)mhp
->ext
[SADB_EXT_LIFETIME_SOFT
];
3826 if (mhp
->extlen
[SADB_EXT_LIFETIME_SOFT
] < sizeof(*lft0
)) {
3827 ipseclog((LOG_DEBUG
, "key_setsaval: invalid soft lifetime ext len.\n"));
3831 sav
->lft_s
= (struct sadb_lifetime
*)key_newbuf(lft0
,
3833 if (sav
->lft_s
== NULL
) {
3834 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3838 /* to be initialize ? */
3845 /* initialization */
3846 if (sav
->replay
!= NULL
) {
3847 keydb_delsecreplay(sav
->replay
);
3850 if (sav
->key_auth
!= NULL
) {
3851 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
3852 KFREE(sav
->key_auth
);
3853 sav
->key_auth
= NULL
;
3855 if (sav
->key_enc
!= NULL
) {
3856 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
3857 KFREE(sav
->key_enc
);
3858 sav
->key_enc
= NULL
;
3861 bzero(sav
->sched
, sav
->schedlen
);
3865 if (sav
->iv
!= NULL
) {
3869 if (sav
->lft_c
!= NULL
) {
3873 if (sav
->lft_h
!= NULL
) {
3877 if (sav
->lft_s
!= NULL
) {
3886 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
3887 * You must update these if need.
3891 * does not modify mbuf. does not free mbuf on error.
3894 key_setsaval2(struct secasvar
*sav
,
3900 struct sadb_key
*key_auth
,
3901 u_int16_t key_auth_len
,
3902 struct sadb_key
*key_enc
,
3903 u_int16_t key_enc_len
,
3904 u_int16_t natt_port
,
3908 struct sadb_lifetime
*lifetime_hard
,
3909 struct sadb_lifetime
*lifetime_soft
)
3912 const struct esp_algorithm
*algo
;
3917 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3919 /* initialization */
3921 sav
->key_auth
= NULL
;
3922 sav
->key_enc
= NULL
;
3929 sav
->remote_ike_port
= 0;
3930 sav
->natt_last_activity
= natt_now
;
3931 sav
->natt_encapsulated_src_port
= 0;
3933 sav
->alg_auth
= alg_auth
;
3934 sav
->alg_enc
= alg_enc
;
3938 key_setspi(sav
, htonl(spi
));
3941 * Verify that a nat-traversal port was specified if
3942 * the nat-traversal flag is set.
3944 if ((sav
->flags
& SADB_X_EXT_NATT
) != 0) {
3945 if (natt_port
== 0) {
3946 ipseclog((LOG_DEBUG
, "key_setsaval2: natt port not set.\n"));
3950 sav
->remote_ike_port
= natt_port
;
3954 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
3955 * SADB_X_EXT_NATT is set and SADB_X_EXT_NATT_KEEPALIVE is not
3956 * set (we're not behind nat) - otherwise clear it.
3958 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
3959 if ((sav
->flags
& SADB_X_EXT_NATT
) == 0 ||
3960 (sav
->flags
& SADB_X_EXT_NATT_KEEPALIVE
) != 0)
3961 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
3964 if ((flags
& SADB_X_EXT_OLD
) == 0) {
3965 sav
->replay
= keydb_newsecreplay(replay
);
3966 if (sav
->replay
== NULL
) {
3967 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3973 /* Authentication keys */
3974 sav
->key_auth
= (__typeof__(sav
->key_auth
))key_newbuf(key_auth
, key_auth_len
);
3975 if (sav
->key_auth
== NULL
) {
3976 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3981 /* Encryption key */
3982 sav
->key_enc
= (__typeof__(sav
->key_enc
))key_newbuf(key_enc
, key_enc_len
);
3983 if (sav
->key_enc
== NULL
) {
3984 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3992 if (satype
== SADB_SATYPE_ESP
) {
3994 algo
= esp_algorithm_lookup(sav
->alg_enc
);
3995 if (algo
&& algo
->ivlen
)
3996 sav
->ivlen
= (*algo
->ivlen
)(algo
, sav
);
3997 if (sav
->ivlen
!= 0) {
3998 KMALLOC_NOWAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4000 lck_mtx_unlock(sadb_mutex
);
4001 KMALLOC_WAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
4002 lck_mtx_lock(sadb_mutex
);
4004 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4010 key_randomfill(sav
->iv
, sav
->ivlen
);
4017 sav
->created
= tv
.tv_sec
;
4019 /* make lifetime for CURRENT */
4020 KMALLOC_NOWAIT(sav
->lft_c
, struct sadb_lifetime
*,
4021 sizeof(struct sadb_lifetime
));
4022 if (sav
->lft_c
== NULL
) {
4023 lck_mtx_unlock(sadb_mutex
);
4024 KMALLOC_WAIT(sav
->lft_c
, struct sadb_lifetime
*,
4025 sizeof(struct sadb_lifetime
));
4026 lck_mtx_lock(sadb_mutex
);
4027 if (sav
->lft_c
== NULL
) {
4028 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4036 sav
->lft_c
->sadb_lifetime_len
=
4037 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
4038 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
4039 sav
->lft_c
->sadb_lifetime_allocations
= 0;
4040 sav
->lft_c
->sadb_lifetime_bytes
= 0;
4041 sav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
4042 sav
->lft_c
->sadb_lifetime_usetime
= 0;
4044 /* lifetimes for HARD and SOFT */
4045 sav
->lft_h
= (__typeof__(sav
->lft_h
))key_newbuf(lifetime_hard
,
4046 sizeof(*lifetime_hard
));
4047 if (sav
->lft_h
== NULL
) {
4048 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4052 sav
->lft_s
= (__typeof__(sav
->lft_s
))key_newbuf(lifetime_soft
,
4053 sizeof(*lifetime_soft
));
4054 if (sav
->lft_s
== NULL
) {
4055 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
4063 /* initialization */
4064 if (sav
->replay
!= NULL
) {
4065 keydb_delsecreplay(sav
->replay
);
4068 if (sav
->key_auth
!= NULL
) {
4069 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
4070 KFREE(sav
->key_auth
);
4071 sav
->key_auth
= NULL
;
4073 if (sav
->key_enc
!= NULL
) {
4074 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
4075 KFREE(sav
->key_enc
);
4076 sav
->key_enc
= NULL
;
4079 bzero(sav
->sched
, sav
->schedlen
);
4083 if (sav
->iv
!= NULL
) {
4087 if (sav
->lft_c
!= NULL
) {
4091 if (sav
->lft_h
!= NULL
) {
4095 if (sav
->lft_s
!= NULL
) {
4104 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
4110 struct secasvar
*sav
)
4113 int checkmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
4114 int mustmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
4118 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4120 /* check SPI value */
4121 switch (sav
->sah
->saidx
.proto
) {
4125 /* No reason to test if this is >= 0, because ntohl(sav->spi) is unsigned. */
4126 if (ntohl(sav
->spi
) <= 255) {
4127 ipseclog((LOG_DEBUG
,
4128 "key_mature: illegal range of SPI %u.\n",
4129 (u_int32_t
)ntohl(sav
->spi
)));
4136 switch (sav
->sah
->saidx
.proto
) {
4139 if ((sav
->flags
& SADB_X_EXT_OLD
)
4140 && (sav
->flags
& SADB_X_EXT_DERIV
)) {
4141 ipseclog((LOG_DEBUG
, "key_mature: "
4142 "invalid flag (derived) given to old-esp.\n"));
4145 if (sav
->alg_auth
== SADB_AALG_NONE
)
4153 if (sav
->flags
& SADB_X_EXT_DERIV
) {
4154 ipseclog((LOG_DEBUG
, "key_mature: "
4155 "invalid flag (derived) given to AH SA.\n"));
4158 if (sav
->alg_enc
!= SADB_EALG_NONE
) {
4159 ipseclog((LOG_DEBUG
, "key_mature: "
4160 "protocol and algorithm mismated.\n"));
4166 case IPPROTO_IPCOMP
:
4167 if (sav
->alg_auth
!= SADB_AALG_NONE
) {
4168 ipseclog((LOG_DEBUG
, "key_mature: "
4169 "protocol and algorithm mismated.\n"));
4172 if ((sav
->flags
& SADB_X_EXT_RAWCPI
) == 0
4173 && ntohl(sav
->spi
) >= 0x10000) {
4174 ipseclog((LOG_DEBUG
, "key_mature: invalid cpi for IPComp.\n"));
4181 ipseclog((LOG_DEBUG
, "key_mature: Invalid satype.\n"));
4182 return EPROTONOSUPPORT
;
4185 /* check authentication algorithm */
4186 if ((checkmask
& 2) != 0) {
4187 const struct ah_algorithm
*algo
;
4190 algo
= ah_algorithm_lookup(sav
->alg_auth
);
4192 ipseclog((LOG_DEBUG
,"key_mature: "
4193 "unknown authentication algorithm.\n"));
4197 /* algorithm-dependent check */
4199 keylen
= sav
->key_auth
->sadb_key_bits
;
4202 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
4203 ipseclog((LOG_DEBUG
,
4204 "key_mature: invalid AH key length %d "
4205 "(%d-%d allowed)\n",
4206 keylen
, algo
->keymin
, algo
->keymax
));
4211 if ((*algo
->mature
)(sav
)) {
4212 /* message generated in per-algorithm function*/
4215 mature
= SADB_SATYPE_AH
;
4218 if ((mustmask
& 2) != 0 && mature
!= SADB_SATYPE_AH
) {
4219 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for AH\n"));
4224 /* check encryption algorithm */
4225 if ((checkmask
& 1) != 0) {
4227 const struct esp_algorithm
*algo
;
4230 algo
= esp_algorithm_lookup(sav
->alg_enc
);
4232 ipseclog((LOG_DEBUG
, "key_mature: unknown encryption algorithm.\n"));
4236 /* algorithm-dependent check */
4238 keylen
= sav
->key_enc
->sadb_key_bits
;
4241 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
4242 ipseclog((LOG_DEBUG
,
4243 "key_mature: invalid ESP key length %d "
4244 "(%d-%d allowed)\n",
4245 keylen
, algo
->keymin
, algo
->keymax
));
4250 if ((*algo
->mature
)(sav
)) {
4251 /* message generated in per-algorithm function*/
4254 mature
= SADB_SATYPE_ESP
;
4257 if ((mustmask
& 1) != 0 && mature
!= SADB_SATYPE_ESP
) {
4258 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for ESP\n"));
4262 ipseclog((LOG_DEBUG
, "key_mature: ESP not supported in this configuration\n"));
4267 /* check compression algorithm */
4268 if ((checkmask
& 4) != 0) {
4269 const struct ipcomp_algorithm
*algo
;
4271 /* algorithm-dependent check */
4272 algo
= ipcomp_algorithm_lookup(sav
->alg_enc
);
4274 ipseclog((LOG_DEBUG
, "key_mature: unknown compression algorithm.\n"));
4279 key_sa_chgstate(sav
, SADB_SASTATE_MATURE
);
4285 * subroutine for SADB_GET and SADB_DUMP.
4287 static struct mbuf
*
4289 struct secasvar
*sav
,
4295 struct mbuf
*result
= NULL
, *tres
= NULL
, *m
;
4300 SADB_EXT_SA
, SADB_X_EXT_SA2
,
4301 SADB_EXT_LIFETIME_HARD
, SADB_EXT_LIFETIME_SOFT
,
4302 SADB_EXT_LIFETIME_CURRENT
, SADB_EXT_ADDRESS_SRC
,
4303 SADB_EXT_ADDRESS_DST
, SADB_EXT_ADDRESS_PROXY
, SADB_EXT_KEY_AUTH
,
4304 SADB_EXT_KEY_ENCRYPT
, SADB_EXT_IDENTITY_SRC
,
4305 SADB_EXT_IDENTITY_DST
, SADB_EXT_SENSITIVITY
,
4308 m
= key_setsadbmsg(type
, 0, satype
, seq
, pid
, sav
->refcnt
);
4313 for (i
= sizeof(dumporder
)/sizeof(dumporder
[0]) - 1; i
>= 0; i
--) {
4316 switch (dumporder
[i
]) {
4318 m
= key_setsadbsa(sav
);
4323 case SADB_X_EXT_SA2
:
4324 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
4325 sav
->replay
? sav
->replay
->count
: 0,
4326 sav
->sah
->saidx
.reqid
);
4331 case SADB_EXT_ADDRESS_SRC
:
4332 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
4333 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
4334 FULLMASK
, IPSEC_ULPROTO_ANY
);
4339 case SADB_EXT_ADDRESS_DST
:
4340 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
4341 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
4342 FULLMASK
, IPSEC_ULPROTO_ANY
);
4347 case SADB_EXT_KEY_AUTH
:
4350 l
= PFKEY_UNUNIT64(sav
->key_auth
->sadb_key_len
);
4354 case SADB_EXT_KEY_ENCRYPT
:
4357 l
= PFKEY_UNUNIT64(sav
->key_enc
->sadb_key_len
);
4361 case SADB_EXT_LIFETIME_CURRENT
:
4364 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_c
)->sadb_ext_len
);
4368 case SADB_EXT_LIFETIME_HARD
:
4371 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_h
)->sadb_ext_len
);
4375 case SADB_EXT_LIFETIME_SOFT
:
4378 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_s
)->sadb_ext_len
);
4382 case SADB_EXT_ADDRESS_PROXY
:
4383 case SADB_EXT_IDENTITY_SRC
:
4384 case SADB_EXT_IDENTITY_DST
:
4385 /* XXX: should we brought from SPD ? */
4386 case SADB_EXT_SENSITIVITY
:
4391 if ((!m
&& !p
) || (m
&& p
))
4394 M_PREPEND(tres
, l
, M_WAITOK
);
4397 bcopy(p
, mtod(tres
, caddr_t
), l
);
4401 m
= key_alloc_mbuf(l
);
4404 m_copyback(m
, 0, l
, p
);
4412 m_cat(result
, tres
);
4414 if (result
->m_len
< sizeof(struct sadb_msg
)) {
4415 result
= m_pullup(result
, sizeof(struct sadb_msg
));
4420 result
->m_pkthdr
.len
= 0;
4421 for (m
= result
; m
; m
= m
->m_next
)
4422 result
->m_pkthdr
.len
+= m
->m_len
;
4424 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
4425 PFKEY_UNIT64(result
->m_pkthdr
.len
);
4436 * set data into sadb_msg.
4438 static struct mbuf
*
4451 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
4454 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
4455 if (m
&& len
> MHLEN
) {
4456 MCLGET(m
, M_DONTWAIT
);
4457 if ((m
->m_flags
& M_EXT
) == 0) {
4464 m
->m_pkthdr
.len
= m
->m_len
= len
;
4467 p
= mtod(m
, struct sadb_msg
*);
4470 p
->sadb_msg_version
= PF_KEY_V2
;
4471 p
->sadb_msg_type
= type
;
4472 p
->sadb_msg_errno
= 0;
4473 p
->sadb_msg_satype
= satype
;
4474 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
4475 p
->sadb_msg_reserved
= reserved
;
4476 p
->sadb_msg_seq
= seq
;
4477 p
->sadb_msg_pid
= (u_int32_t
)pid
;
4483 * copy secasvar data into sadb_address.
4485 static struct mbuf
*
4487 struct secasvar
*sav
)
4493 len
= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
4494 m
= key_alloc_mbuf(len
);
4495 if (!m
|| m
->m_next
) { /*XXX*/
4501 p
= mtod(m
, struct sadb_sa
*);
4504 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
4505 p
->sadb_sa_exttype
= SADB_EXT_SA
;
4506 p
->sadb_sa_spi
= sav
->spi
;
4507 p
->sadb_sa_replay
= (sav
->replay
!= NULL
? sav
->replay
->wsize
: 0);
4508 p
->sadb_sa_state
= sav
->state
;
4509 p
->sadb_sa_auth
= sav
->alg_auth
;
4510 p
->sadb_sa_encrypt
= sav
->alg_enc
;
4511 p
->sadb_sa_flags
= sav
->flags
;
4517 * set data into sadb_address.
4519 static struct mbuf
*
4522 struct sockaddr
*saddr
,
4527 struct sadb_address
*p
;
4530 len
= PFKEY_ALIGN8(sizeof(struct sadb_address
)) +
4531 PFKEY_ALIGN8(saddr
->sa_len
);
4532 m
= key_alloc_mbuf(len
);
4533 if (!m
|| m
->m_next
) { /*XXX*/
4539 p
= mtod(m
, struct sadb_address
*);
4542 p
->sadb_address_len
= PFKEY_UNIT64(len
);
4543 p
->sadb_address_exttype
= exttype
;
4544 p
->sadb_address_proto
= ul_proto
;
4545 if (prefixlen
== FULLMASK
) {
4546 switch (saddr
->sa_family
) {
4548 prefixlen
= sizeof(struct in_addr
) << 3;
4551 prefixlen
= sizeof(struct in6_addr
) << 3;
4557 p
->sadb_address_prefixlen
= prefixlen
;
4558 p
->sadb_address_reserved
= 0;
4561 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_address
)),
4568 * set data into sadb_session_id
4570 static struct mbuf
*
4571 key_setsadbsession_id (u_int64_t session_ids
[])
4574 struct sadb_session_id
*p
;
4577 len
= PFKEY_ALIGN8(sizeof(*p
));
4578 m
= key_alloc_mbuf(len
);
4579 if (!m
|| m
->m_next
) { /*XXX*/
4585 p
= mtod(m
, __typeof__(p
));
4588 p
->sadb_session_id_len
= PFKEY_UNIT64(len
);
4589 p
->sadb_session_id_exttype
= SADB_EXT_SESSION_ID
;
4590 p
->sadb_session_id_v
[0] = session_ids
[0];
4591 p
->sadb_session_id_v
[1] = session_ids
[1];
4597 * copy stats data into sadb_sastat type.
4599 static struct mbuf
*
4600 key_setsadbsastat (u_int32_t dir
,
4601 struct sastat
*stats
,
4602 u_int32_t max_stats
)
4605 struct sadb_sastat
*p
;
4612 list_len
= sizeof(*stats
) * max_stats
;
4613 len
= PFKEY_ALIGN8(sizeof(*p
)) + PFKEY_ALIGN8(list_len
);
4614 m
= key_alloc_mbuf(len
);
4615 if (!m
|| m
->m_next
) { /*XXX*/
4621 p
= mtod(m
, __typeof__(p
));
4624 p
->sadb_sastat_len
= PFKEY_UNIT64(len
);
4625 p
->sadb_sastat_exttype
= SADB_EXT_SASTAT
;
4626 p
->sadb_sastat_dir
= dir
;
4627 p
->sadb_sastat_list_len
= max_stats
;
4630 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(*p
)),
4639 * set data into sadb_ident.
4641 static struct mbuf
*
4650 struct sadb_ident
*p
;
4653 len
= PFKEY_ALIGN8(sizeof(struct sadb_ident
)) + PFKEY_ALIGN8(stringlen
);
4654 m
= key_alloc_mbuf(len
);
4655 if (!m
|| m
->m_next
) { /*XXX*/
4661 p
= mtod(m
, struct sadb_ident
*);
4664 p
->sadb_ident_len
= PFKEY_UNIT64(len
);
4665 p
->sadb_ident_exttype
= exttype
;
4666 p
->sadb_ident_type
= idtype
;
4667 p
->sadb_ident_reserved
= 0;
4668 p
->sadb_ident_id
= id
;
4671 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_ident
)),
4679 * set data into sadb_x_sa2.
4681 static struct mbuf
*
4688 struct sadb_x_sa2
*p
;
4691 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_sa2
));
4692 m
= key_alloc_mbuf(len
);
4693 if (!m
|| m
->m_next
) { /*XXX*/
4699 p
= mtod(m
, struct sadb_x_sa2
*);
4702 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
4703 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
4704 p
->sadb_x_sa2_mode
= mode
;
4705 p
->sadb_x_sa2_reserved1
= 0;
4706 p
->sadb_x_sa2_reserved2
= 0;
4707 p
->sadb_x_sa2_sequence
= seq
;
4708 p
->sadb_x_sa2_reqid
= reqid
;
4714 * set data into sadb_x_policy
4716 static struct mbuf
*
4723 struct sadb_x_policy
*p
;
4726 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_policy
));
4727 m
= key_alloc_mbuf(len
);
4728 if (!m
|| m
->m_next
) { /*XXX*/
4734 p
= mtod(m
, struct sadb_x_policy
*);
4737 p
->sadb_x_policy_len
= PFKEY_UNIT64(len
);
4738 p
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
4739 p
->sadb_x_policy_type
= type
;
4740 p
->sadb_x_policy_dir
= dir
;
4741 p
->sadb_x_policy_id
= id
;
4748 * copy a buffer into the new buffer allocated.
4757 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4758 KMALLOC_NOWAIT(new, caddr_t
, len
);
4760 lck_mtx_unlock(sadb_mutex
);
4761 KMALLOC_WAIT(new, caddr_t
, len
);
4762 lck_mtx_lock(sadb_mutex
);
4764 ipseclog((LOG_DEBUG
, "key_newbuf: No more memory.\n"));
4768 bcopy(src
, new, len
);
4773 /* compare my own address
4774 * OUT: 1: true, i.e. my address.
4779 struct sockaddr
*sa
)
4782 struct sockaddr_in
*sin
;
4783 struct in_ifaddr
*ia
;
4788 panic("key_ismyaddr: NULL pointer is passed.\n");
4790 switch (sa
->sa_family
) {
4793 lck_rw_lock_shared(in_ifaddr_rwlock
);
4794 sin
= (struct sockaddr_in
*)(void *)sa
;
4795 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
4796 ia
= ia
->ia_link
.tqe_next
) {
4797 IFA_LOCK_SPIN(&ia
->ia_ifa
);
4798 if (sin
->sin_family
== ia
->ia_addr
.sin_family
&&
4799 sin
->sin_len
== ia
->ia_addr
.sin_len
&&
4800 sin
->sin_addr
.s_addr
== ia
->ia_addr
.sin_addr
.s_addr
)
4802 IFA_UNLOCK(&ia
->ia_ifa
);
4803 lck_rw_done(in_ifaddr_rwlock
);
4806 IFA_UNLOCK(&ia
->ia_ifa
);
4808 lck_rw_done(in_ifaddr_rwlock
);
4813 return key_ismyaddr6((struct sockaddr_in6
*)(void *)sa
);
4822 * compare my own address for IPv6.
4825 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
4827 #include <netinet6/in6_var.h>
4831 struct sockaddr_in6
*sin6
)
4833 struct in6_ifaddr
*ia
;
4834 struct in6_multi
*in6m
;
4836 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
4837 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
4838 IFA_LOCK(&ia
->ia_ifa
);
4839 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
4840 (struct sockaddr
*)&ia
->ia_addr
, 0) == 0) {
4841 IFA_UNLOCK(&ia
->ia_ifa
);
4842 lck_rw_done(&in6_ifaddr_rwlock
);
4845 IFA_UNLOCK(&ia
->ia_ifa
);
4849 * XXX why do we care about multlicast here while we don't care
4850 * about IPv4 multicast??
4854 in6_multihead_lock_shared();
4855 IN6_LOOKUP_MULTI(&sin6
->sin6_addr
, ia
->ia_ifp
, in6m
);
4856 in6_multihead_lock_done();
4858 lck_rw_done(&in6_ifaddr_rwlock
);
4863 lck_rw_done(&in6_ifaddr_rwlock
);
4865 /* loopback, just for safety */
4866 if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
4874 * compare two secasindex structure.
4875 * flag can specify to compare 2 saidxes.
4876 * compare two secasindex structure without both mode and reqid.
4877 * don't compare port.
4879 * saidx0: source, it can be in SAD.
4887 struct secasindex
*saidx0
,
4888 struct secasindex
*saidx1
,
4892 if (saidx0
== NULL
&& saidx1
== NULL
)
4895 if (saidx0
== NULL
|| saidx1
== NULL
)
4898 if (saidx0
->proto
!= saidx1
->proto
)
4901 if (flag
== CMP_EXACTLY
) {
4902 if (saidx0
->mode
!= saidx1
->mode
)
4904 if (saidx0
->reqid
!= saidx1
->reqid
)
4906 if (bcmp(&saidx0
->src
, &saidx1
->src
, saidx0
->src
.ss_len
) != 0 ||
4907 bcmp(&saidx0
->dst
, &saidx1
->dst
, saidx0
->dst
.ss_len
) != 0)
4911 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
4912 if (flag
& CMP_REQID
) {
4914 * If reqid of SPD is non-zero, unique SA is required.
4915 * The result must be of same reqid in this case.
4917 if (saidx1
->reqid
!= 0 && saidx0
->reqid
!= saidx1
->reqid
)
4921 if (flag
& CMP_MODE
) {
4922 if (saidx0
->mode
!= IPSEC_MODE_ANY
4923 && saidx0
->mode
!= saidx1
->mode
)
4927 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->src
,
4928 (struct sockaddr
*)&saidx1
->src
, flag
& CMP_PORT
? 1 : 0) != 0) {
4931 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->dst
,
4932 (struct sockaddr
*)&saidx1
->dst
, flag
& CMP_PORT
? 1 : 0) != 0) {
4941 * compare two secindex structure exactly.
4943 * spidx0: source, it is often in SPD.
4944 * spidx1: object, it is often from PFKEY message.
4950 key_cmpspidx_exactly(
4951 struct secpolicyindex
*spidx0
,
4952 struct secpolicyindex
*spidx1
)
4955 if (spidx0
== NULL
&& spidx1
== NULL
)
4958 if (spidx0
== NULL
|| spidx1
== NULL
)
4961 if (spidx0
->prefs
!= spidx1
->prefs
4962 || spidx0
->prefd
!= spidx1
->prefd
4963 || spidx0
->ul_proto
!= spidx1
->ul_proto
)
4966 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src
,
4967 (struct sockaddr
*)&spidx1
->src
, 1) != 0) {
4970 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst
,
4971 (struct sockaddr
*)&spidx1
->dst
, 1) != 0) {
4979 * compare two secindex structure with mask.
4981 * spidx0: source, it is often in SPD.
4982 * spidx1: object, it is often from IP header.
4988 key_cmpspidx_withmask(
4989 struct secpolicyindex
*spidx0
,
4990 struct secpolicyindex
*spidx1
)
4993 if (spidx0
== NULL
&& spidx1
== NULL
)
4996 if (spidx0
== NULL
|| spidx1
== NULL
)
4999 if (spidx0
->src
.ss_family
!= spidx1
->src
.ss_family
||
5000 spidx0
->dst
.ss_family
!= spidx1
->dst
.ss_family
||
5001 spidx0
->src
.ss_len
!= spidx1
->src
.ss_len
||
5002 spidx0
->dst
.ss_len
!= spidx1
->dst
.ss_len
)
5005 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
5006 if (spidx0
->ul_proto
!= (u_int16_t
)IPSEC_ULPROTO_ANY
5007 && spidx0
->ul_proto
!= spidx1
->ul_proto
)
5010 switch (spidx0
->src
.ss_family
) {
5012 if (satosin(&spidx0
->src
)->sin_port
!= IPSEC_PORT_ANY
5013 && satosin(&spidx0
->src
)->sin_port
!=
5014 satosin(&spidx1
->src
)->sin_port
)
5016 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->src
)->sin_addr
,
5017 (caddr_t
)&satosin(&spidx1
->src
)->sin_addr
, spidx0
->prefs
))
5021 if (satosin6(&spidx0
->src
)->sin6_port
!= IPSEC_PORT_ANY
5022 && satosin6(&spidx0
->src
)->sin6_port
!=
5023 satosin6(&spidx1
->src
)->sin6_port
)
5026 * scope_id check. if sin6_scope_id is 0, we regard it
5027 * as a wildcard scope, which matches any scope zone ID.
5029 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
5030 satosin6(&spidx1
->src
)->sin6_scope_id
&&
5031 satosin6(&spidx0
->src
)->sin6_scope_id
!=
5032 satosin6(&spidx1
->src
)->sin6_scope_id
)
5034 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->src
)->sin6_addr
,
5035 (caddr_t
)&satosin6(&spidx1
->src
)->sin6_addr
, spidx0
->prefs
))
5040 if (bcmp(&spidx0
->src
, &spidx1
->src
, spidx0
->src
.ss_len
) != 0)
5045 switch (spidx0
->dst
.ss_family
) {
5047 if (satosin(&spidx0
->dst
)->sin_port
!= IPSEC_PORT_ANY
5048 && satosin(&spidx0
->dst
)->sin_port
!=
5049 satosin(&spidx1
->dst
)->sin_port
)
5051 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->dst
)->sin_addr
,
5052 (caddr_t
)&satosin(&spidx1
->dst
)->sin_addr
, spidx0
->prefd
))
5056 if (satosin6(&spidx0
->dst
)->sin6_port
!= IPSEC_PORT_ANY
5057 && satosin6(&spidx0
->dst
)->sin6_port
!=
5058 satosin6(&spidx1
->dst
)->sin6_port
)
5061 * scope_id check. if sin6_scope_id is 0, we regard it
5062 * as a wildcard scope, which matches any scope zone ID.
5064 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
5065 satosin6(&spidx1
->src
)->sin6_scope_id
&&
5066 satosin6(&spidx0
->dst
)->sin6_scope_id
!=
5067 satosin6(&spidx1
->dst
)->sin6_scope_id
)
5069 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->dst
)->sin6_addr
,
5070 (caddr_t
)&satosin6(&spidx1
->dst
)->sin6_addr
, spidx0
->prefd
))
5075 if (bcmp(&spidx0
->dst
, &spidx1
->dst
, spidx0
->dst
.ss_len
) != 0)
5080 /* XXX Do we check other field ? e.g. flowinfo */
5085 /* returns 0 on match */
5088 struct sockaddr
*sa1
,
5089 struct sockaddr
*sa2
,
5092 if (sa1
->sa_family
!= sa2
->sa_family
|| sa1
->sa_len
!= sa2
->sa_len
)
5095 switch (sa1
->sa_family
) {
5097 if (sa1
->sa_len
!= sizeof(struct sockaddr_in
))
5099 if (satosin(sa1
)->sin_addr
.s_addr
!=
5100 satosin(sa2
)->sin_addr
.s_addr
) {
5103 if (port
&& satosin(sa1
)->sin_port
!= satosin(sa2
)->sin_port
)
5107 if (sa1
->sa_len
!= sizeof(struct sockaddr_in6
))
5108 return 1; /*EINVAL*/
5109 if (satosin6(sa1
)->sin6_scope_id
!=
5110 satosin6(sa2
)->sin6_scope_id
) {
5113 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1
)->sin6_addr
,
5114 &satosin6(sa2
)->sin6_addr
)) {
5118 satosin6(sa1
)->sin6_port
!= satosin6(sa2
)->sin6_port
) {
5123 if (bcmp(sa1
, sa2
, sa1
->sa_len
) != 0)
5132 * compare two buffers with mask.
5136 * bits: Number of bits to compare
5149 /* XXX: This could be considerably faster if we compare a word
5150 * at a time, but it is complicated on LSB Endian machines */
5152 /* Handle null pointers */
5153 if (p1
== NULL
|| p2
== NULL
)
5163 mask
= ~((1<<(8-bits
))-1);
5164 if ((*p1
& mask
) != (*p2
& mask
))
5167 return 1; /* Match! */
5172 * scanning SPD and SAD to check status for each entries,
5173 * and do to remove or to expire.
5174 * XXX: year 2038 problem may remain.
5176 int key_timehandler_debug
= 0;
5177 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;
5178 u_int64_t total_sav_count
= 0;
5180 key_timehandler(void)
5184 struct secpolicy
**spbuf
= NULL
, **spptr
= NULL
;
5185 struct secasvar
**savexbuf
= NULL
, **savexptr
= NULL
;
5186 struct secasvar
**savkabuf
= NULL
, **savkaptr
= NULL
;
5187 int spbufcount
= 0, savbufcount
= 0, spcount
= 0, savexcount
= 0, savkacount
= 0, cnt
;
5191 /* pre-allocate buffers before taking the lock */
5192 /* if allocation failures occur - portions of the processing will be skipped */
5193 if ((spbufcount
= ipsec_policy_count
) != 0) {
5195 KMALLOC_WAIT(spbuf
, struct secpolicy
**, spbufcount
* sizeof(struct secpolicy
*));
5199 if ((savbufcount
= ipsec_sav_count
) != 0) {
5201 KMALLOC_WAIT(savexbuf
, struct secasvar
**, savbufcount
* sizeof(struct secasvar
*));
5203 savexptr
= savexbuf
;
5204 KMALLOC_WAIT(savkabuf
, struct secasvar
**, savbufcount
* sizeof(struct secasvar
*));
5206 savkaptr
= savkabuf
;
5208 lck_mtx_lock(sadb_mutex
);
5212 struct secpolicy
*sp
, *nextsp
;
5214 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
5215 for (sp
= LIST_FIRST(&sptree
[dir
]);
5220 nextsp
= LIST_NEXT(sp
, chain
);
5222 if (sp
->state
== IPSEC_SPSTATE_DEAD
) {
5223 key_freesp(sp
, KEY_SADB_LOCKED
);
5227 if (sp
->lifetime
== 0 && sp
->validtime
== 0)
5229 if (spbuf
&& spcount
< spbufcount
) {
5230 /* the deletion will occur next time */
5232 && tv
.tv_sec
- sp
->created
> sp
->lifetime
)
5234 && tv
.tv_sec
- sp
->lastused
> sp
->validtime
)) {
5235 //key_spdexpire(sp);
5236 sp
->state
= IPSEC_SPSTATE_DEAD
;
5247 if (savbufcount
!= 0) {
5248 struct secashead
*sah
, *nextsah
;
5249 struct secasvar
*sav
, *nextsav
;
5251 for (sah
= LIST_FIRST(&sahtree
);
5256 nextsah
= LIST_NEXT(sah
, chain
);
5258 /* if sah has been dead, then delete it and process next sah. */
5259 if (sah
->state
== SADB_SASTATE_DEAD
) {
5265 if (LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]) == NULL
&&
5266 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]) == NULL
&&
5267 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]) == NULL
&&
5268 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]) == NULL
) {
5274 /* if LARVAL entry doesn't become MATURE, delete it. */
5275 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]);
5281 nextsav
= LIST_NEXT(sav
, chain
);
5283 if (tv
.tv_sec
- sav
->created
> key_larval_lifetime
) {
5284 key_freesav(sav
, KEY_SADB_LOCKED
);
5289 * If this is a NAT traversal SA with no activity,
5290 * we need to send a keep alive.
5292 * Performed outside of the loop before so we will
5293 * only ever send one keepalive. The first SA on
5294 * the list is the one that will be used for sending
5295 * traffic, so this is the one we use for determining
5296 * when to send the keepalive.
5298 if (savkabuf
&& savkacount
< savbufcount
) {
5299 sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]); //%%% should we check dying list if this is empty???
5300 if (natt_keepalive_interval
&& sav
&&
5301 (sav
->flags
& (SADB_X_EXT_NATT_KEEPALIVE
| SADB_X_EXT_ESP_KEEPALIVE
)) != 0) {
5309 * check MATURE entry to start to send expire message
5312 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]);
5318 nextsav
= LIST_NEXT(sav
, chain
);
5320 /* we don't need to check. */
5321 if (sav
->lft_s
== NULL
)
5325 if (sav
->lft_c
== NULL
) {
5326 ipseclog((LOG_DEBUG
,"key_timehandler: "
5327 "There is no CURRENT time, why?\n"));
5331 /* check SOFT lifetime */
5332 if (sav
->lft_s
->sadb_lifetime_addtime
!= 0
5333 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
5335 * check the SA if it has been used.
5336 * when it hasn't been used, delete it.
5337 * i don't think such SA will be used.
5339 if (sav
->lft_c
->sadb_lifetime_usetime
== 0) {
5340 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5341 key_freesav(sav
, KEY_SADB_LOCKED
);
5343 } else if (savexbuf
&& savexcount
< savbufcount
) {
5344 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
5351 /* check SOFT lifetime by bytes */
5353 * XXX I don't know the way to delete this SA
5354 * when new SA is installed. Caution when it's
5355 * installed too big lifetime by time.
5357 else if (savexbuf
&& savexcount
< savbufcount
5358 && sav
->lft_s
->sadb_lifetime_bytes
!= 0
5359 && sav
->lft_s
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
5362 * XXX If we keep to send expire
5363 * message in the status of
5364 * DYING. Do remove below code.
5367 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
5374 /* check DYING entry to change status to DEAD. */
5375 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]);
5381 nextsav
= LIST_NEXT(sav
, chain
);
5383 /* we don't need to check. */
5384 if (sav
->lft_h
== NULL
)
5388 if (sav
->lft_c
== NULL
) {
5389 ipseclog((LOG_DEBUG
, "key_timehandler: "
5390 "There is no CURRENT time, why?\n"));
5394 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0
5395 && tv
.tv_sec
- sav
->created
> sav
->lft_h
->sadb_lifetime_addtime
) {
5396 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5397 key_freesav(sav
, KEY_SADB_LOCKED
);
5400 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
5401 else if (savbuf
&& savexcount
< savbufcount
5402 && sav
->lft_s
!= NULL
5403 && sav
->lft_s
->sadb_lifetime_addtime
!= 0
5404 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
5406 * XXX: should be checked to be
5407 * installed the valid SA.
5411 * If there is no SA then sending
5420 /* check HARD lifetime by bytes */
5421 else if (sav
->lft_h
->sadb_lifetime_bytes
!= 0
5422 && sav
->lft_h
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
5423 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5424 key_freesav(sav
, KEY_SADB_LOCKED
);
5429 /* delete entry in DEAD */
5430 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]);
5436 nextsav
= LIST_NEXT(sav
, chain
);
5439 if (sav
->state
!= SADB_SASTATE_DEAD
) {
5440 ipseclog((LOG_DEBUG
, "key_timehandler: "
5441 "invalid sav->state "
5442 "(queue: %d SA: %d): "
5444 SADB_SASTATE_DEAD
, sav
->state
));
5448 * do not call key_freesav() here.
5449 * sav should already be freed, and sav->refcnt
5450 * shows other references to sav
5451 * (such as from SPD).
5457 if (++key_timehandler_debug
>= 300) {
5458 if (key_debug_level
) {
5459 printf("%s: total stats for %u calls\n", __FUNCTION__
, key_timehandler_debug
);
5460 printf("%s: walked %u SPDs\n", __FUNCTION__
, spd_count
);
5461 printf("%s: walked %llu SAs: LARVAL SAs %u, MATURE SAs %u, DYING SAs %u, DEAD SAs %u\n", __FUNCTION__
,
5462 total_sav_count
, larval_sav_count
, mature_sav_count
, dying_sav_count
, dead_sav_count
);
5463 printf("%s: walked %u SAHs: DEAD SAHs %u, EMPTY SAHs %u\n", __FUNCTION__
,
5464 sah_count
, dead_sah_count
, empty_sah_count
);
5465 if (sah_search_calls
) {
5466 printf("%s: SAH search cost %d iters per call\n", __FUNCTION__
,
5467 (sah_search_count
/sah_search_calls
));
5473 empty_sah_count
= 0;
5474 larval_sav_count
= 0;
5475 mature_sav_count
= 0;
5476 dying_sav_count
= 0;
5478 total_sav_count
= 0;
5479 sah_search_count
= 0;
5480 sah_search_calls
= 0;
5481 key_timehandler_debug
= 0;
5483 #ifndef IPSEC_NONBLOCK_ACQUIRE
5486 struct secacq
*acq
, *nextacq
;
5488 for (acq
= LIST_FIRST(&acqtree
);
5492 nextacq
= LIST_NEXT(acq
, chain
);
5494 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
5495 && __LIST_CHAINED(acq
)) {
5496 LIST_REMOVE(acq
, chain
);
5505 struct secspacq
*acq
, *nextacq
;
5507 for (acq
= LIST_FIRST(&spacqtree
);
5511 nextacq
= LIST_NEXT(acq
, chain
);
5513 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
5514 && __LIST_CHAINED(acq
)) {
5515 LIST_REMOVE(acq
, chain
);
5521 /* initialize random seed */
5522 if (key_tick_init_random
++ > key_int_random
) {
5523 key_tick_init_random
= 0;
5529 lck_mtx_unlock(sadb_mutex
);
5531 /* send messages outside of sadb_mutex */
5532 if (spbuf
&& spcount
> 0) {
5535 key_spdexpire(*(--spptr
));
5537 if (savkabuf
&& savkacount
> 0) {
5538 struct secasvar
**savkaptr_sav
= savkaptr
;
5539 int cnt_send
= savkacount
;
5541 while (cnt_send
--) {
5542 if (ipsec_send_natt_keepalive(*(--savkaptr
))) {
5543 // <rdar://6768487> iterate (all over again) and update timestamps
5544 struct secasvar
**savkaptr_update
= savkaptr_sav
;
5545 int cnt_update
= savkacount
;
5546 while (cnt_update
--) {
5547 key_update_natt_keepalive_timestamp(*savkaptr
,
5548 *(--savkaptr_update
));
5553 if (savexbuf
&& savexcount
> 0) {
5556 key_expire(*(--savexptr
));
5559 /* decrement ref counts and free buffers */
5560 lck_mtx_lock(sadb_mutex
);
5563 key_freesp(*spptr
++, KEY_SADB_LOCKED
);
5567 while (savkacount
--)
5568 key_freesav(*savkaptr
++, KEY_SADB_LOCKED
);
5572 while (savexcount
--)
5573 key_freesav(*savexptr
++, KEY_SADB_LOCKED
);
5576 lck_mtx_unlock(sadb_mutex
);
5579 #ifndef IPSEC_DEBUG2
5580 /* do exchange to tick time !! */
5581 (void)timeout((void *)key_timehandler
, (void *)0, hz
);
5582 #endif /* IPSEC_DEBUG2 */
5588 * to initialize a seed for random()
5594 /* Our PRNG is based on Yarrow and doesn't need to be seeded */
5601 srandom(tv
.tv_usec
);
5612 key_randomfill(&value
, sizeof(value
));
5623 read_random(p
, (u_int
)l
);
5627 static int warn
= 1;
5630 n
= (size_t)read_random(p
, (u_int
)l
);
5634 bcopy(&v
, (u_int8_t
*)p
+ n
,
5635 l
- n
< sizeof(v
) ? l
- n
: sizeof(v
));
5639 printf("WARNING: pseudo-random number generator "
5640 "used for IPsec processing\n");
5648 * map SADB_SATYPE_* to IPPROTO_*.
5649 * if satype == SADB_SATYPE then satype is mapped to ~0.
5651 * 0: invalid satype.
5658 case SADB_SATYPE_UNSPEC
:
5659 return IPSEC_PROTO_ANY
;
5660 case SADB_SATYPE_AH
:
5662 case SADB_SATYPE_ESP
:
5664 case SADB_X_SATYPE_IPCOMP
:
5665 return IPPROTO_IPCOMP
;
5674 * map IPPROTO_* to SADB_SATYPE_*
5676 * 0: invalid protocol type.
5684 return SADB_SATYPE_AH
;
5686 return SADB_SATYPE_ESP
;
5687 case IPPROTO_IPCOMP
:
5688 return SADB_X_SATYPE_IPCOMP
;
5698 * SADB_GETSPI processing is to receive
5699 * <base, (SA2), src address, dst address, (SPI range)>
5700 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
5701 * tree with the status of LARVAL, and send
5702 * <base, SA(*), address(SD)>
5705 * IN: mhp: pointer to the pointer to each header.
5706 * OUT: NULL if fail.
5707 * other if success, return pointer to the message to send.
5713 const struct sadb_msghdr
*mhp
)
5715 struct sadb_address
*src0
, *dst0
;
5716 struct secasindex saidx
;
5717 struct secashead
*newsah
;
5718 struct secasvar
*newsav
;
5725 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
5728 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5729 panic("key_getspi: NULL pointer is passed.\n");
5731 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5732 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
5733 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
5734 return key_senderror(so
, m
, EINVAL
);
5736 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5737 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5738 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
5739 return key_senderror(so
, m
, EINVAL
);
5741 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
5742 mode
= ((struct sadb_x_sa2
*)
5743 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
5744 reqid
= ((struct sadb_x_sa2
*)
5745 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
5747 mode
= IPSEC_MODE_ANY
;
5751 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5752 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5754 /* map satype to proto */
5755 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5756 ipseclog((LOG_DEBUG
, "key_getspi: invalid satype is passed.\n"));
5757 return key_senderror(so
, m
, EINVAL
);
5760 /* make sure if port number is zero. */
5761 switch (((struct sockaddr
*)(src0
+ 1))->sa_family
) {
5763 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
5764 sizeof(struct sockaddr_in
))
5765 return key_senderror(so
, m
, EINVAL
);
5766 ((struct sockaddr_in
*)(void *)(src0
+ 1))->sin_port
= 0;
5769 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
5770 sizeof(struct sockaddr_in6
))
5771 return key_senderror(so
, m
, EINVAL
);
5772 ((struct sockaddr_in6
*)(void *)(src0
+ 1))->sin6_port
= 0;
5777 switch (((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
5779 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
5780 sizeof(struct sockaddr_in
))
5781 return key_senderror(so
, m
, EINVAL
);
5782 ((struct sockaddr_in
*)(void *)(dst0
+ 1))->sin_port
= 0;
5785 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
5786 sizeof(struct sockaddr_in6
))
5787 return key_senderror(so
, m
, EINVAL
);
5788 ((struct sockaddr_in6
*)(void *)(dst0
+ 1))->sin6_port
= 0;
5794 /* XXX boundary check against sa_len */
5795 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
5797 lck_mtx_lock(sadb_mutex
);
5799 /* SPI allocation */
5800 spi
= key_do_getnewspi((struct sadb_spirange
*)
5801 (void *)mhp
->ext
[SADB_EXT_SPIRANGE
], &saidx
);
5803 lck_mtx_unlock(sadb_mutex
);
5804 return key_senderror(so
, m
, EINVAL
);
5807 /* get a SA index */
5808 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
5809 /* create a new SA index: key_addspi is always used for inbound spi */
5810 if ((newsah
= key_newsah(&saidx
, IPSEC_DIR_INBOUND
)) == NULL
) {
5811 lck_mtx_unlock(sadb_mutex
);
5812 ipseclog((LOG_DEBUG
, "key_getspi: No more memory.\n"));
5813 return key_senderror(so
, m
, ENOBUFS
);
5819 newsav
= key_newsav(m
, mhp
, newsah
, &error
);
5820 if (newsav
== NULL
) {
5821 /* XXX don't free new SA index allocated in above. */
5822 lck_mtx_unlock(sadb_mutex
);
5823 return key_senderror(so
, m
, error
);
5827 key_setspi(newsav
, htonl(spi
));
5829 #ifndef IPSEC_NONBLOCK_ACQUIRE
5830 /* delete the entry in acqtree */
5831 if (mhp
->msg
->sadb_msg_seq
!= 0) {
5833 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) != NULL
) {
5834 /* reset counter in order to deletion by timehandler. */
5837 acq
->created
= tv
.tv_sec
;
5843 lck_mtx_unlock(sadb_mutex
);
5846 struct mbuf
*n
, *nn
;
5847 struct sadb_sa
*m_sa
;
5848 struct sadb_msg
*newmsg
;
5851 /* create new sadb_msg to reply. */
5852 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
)) +
5853 PFKEY_ALIGN8(sizeof(struct sadb_sa
));
5855 return key_senderror(so
, m
, ENOBUFS
);
5857 MGETHDR(n
, M_WAITOK
, MT_DATA
);
5858 if (n
&& len
> MHLEN
) {
5859 MCLGET(n
, M_WAITOK
);
5860 if ((n
->m_flags
& M_EXT
) == 0) {
5866 return key_senderror(so
, m
, ENOBUFS
);
5872 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
5873 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
5875 m_sa
= (struct sadb_sa
*)(void *)(mtod(n
, caddr_t
) + off
);
5876 m_sa
->sadb_sa_len
= PFKEY_UNIT64(sizeof(struct sadb_sa
));
5877 m_sa
->sadb_sa_exttype
= SADB_EXT_SA
;
5878 m_sa
->sadb_sa_spi
= htonl(spi
);
5879 off
+= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
5883 panic("length inconsistency in key_getspi");
5886 int mbufItems
[] = {SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
5887 n
->m_next
= key_gather_mbuf(m
, mhp
, 0, sizeof(mbufItems
)/sizeof(int), mbufItems
);
5890 return key_senderror(so
, m
, ENOBUFS
);
5894 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5895 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5897 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
5900 n
->m_pkthdr
.len
= 0;
5901 for (nn
= n
; nn
; nn
= nn
->m_next
)
5902 n
->m_pkthdr
.len
+= nn
->m_len
;
5904 newmsg
= mtod(n
, struct sadb_msg
*);
5905 newmsg
->sadb_msg_seq
= newsav
->seq
;
5906 newmsg
->sadb_msg_errno
= 0;
5907 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
5910 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
5915 key_getspi2(struct sockaddr
*src
,
5916 struct sockaddr
*dst
,
5920 struct sadb_spirange
*spirange
)
5923 struct secasindex saidx
;
5925 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
5927 /* XXX boundary check against sa_len */
5928 KEY_SETSECASIDX(proto
, mode
, reqid
, src
, dst
, &saidx
);
5930 /* make sure if port number is zero. */
5931 switch (((struct sockaddr
*)&saidx
.src
)->sa_family
) {
5933 if (((struct sockaddr
*)&saidx
.src
)->sa_len
!= sizeof(struct sockaddr_in
))
5935 ((struct sockaddr_in
*)&saidx
.src
)->sin_port
= 0;
5938 if (((struct sockaddr
*)&saidx
.src
)->sa_len
!= sizeof(struct sockaddr_in6
))
5940 ((struct sockaddr_in6
*)&saidx
.src
)->sin6_port
= 0;
5945 switch (((struct sockaddr
*)&saidx
.dst
)->sa_family
) {
5947 if (((struct sockaddr
*)&saidx
.dst
)->sa_len
!= sizeof(struct sockaddr_in
))
5949 ((struct sockaddr_in
*)&saidx
.dst
)->sin_port
= 0;
5952 if (((struct sockaddr
*)&saidx
.dst
)->sa_len
!= sizeof(struct sockaddr_in6
))
5954 ((struct sockaddr_in6
*)&saidx
.dst
)->sin6_port
= 0;
5960 lck_mtx_lock(sadb_mutex
);
5962 /* SPI allocation */
5963 spi
= key_do_getnewspi(spirange
, &saidx
);
5965 lck_mtx_unlock(sadb_mutex
);
5971 * allocating new SPI
5972 * called by key_getspi() and key_getspi2().
5979 struct sadb_spirange
*spirange
,
5980 struct secasindex
*saidx
)
5983 u_int32_t keymin
, keymax
;
5984 int count
= key_spi_trycnt
;
5986 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5988 /* set spi range to allocate */
5989 if (spirange
!= NULL
) {
5990 keymin
= spirange
->sadb_spirange_min
;
5991 keymax
= spirange
->sadb_spirange_max
;
5993 keymin
= key_spi_minval
;
5994 keymax
= key_spi_maxval
;
5996 /* IPCOMP needs 2-byte SPI */
5997 if (saidx
->proto
== IPPROTO_IPCOMP
) {
5999 if (keymin
>= 0x10000)
6001 if (keymax
>= 0x10000)
6003 if (keymin
> keymax
) {
6004 t
= keymin
; keymin
= keymax
; keymax
= t
;
6008 if (keymin
== keymax
) {
6009 if (key_checkspidup(saidx
, keymin
) != NULL
) {
6010 ipseclog((LOG_DEBUG
, "key_do_getnewspi: SPI %u exists already.\n", keymin
));
6014 count
--; /* taking one cost. */
6019 u_int32_t range
= keymax
- keymin
+ 1; /* overflow value of zero means full range */
6024 /* when requesting to allocate spi ranged */
6026 u_int32_t rand_val
= key_random();
6028 /* generate pseudo-random SPI value ranged. */
6029 newspi
= (range
== 0 ? rand_val
: keymin
+ (rand_val
% range
));
6031 if (key_checkspidup(saidx
, newspi
) == NULL
)
6035 if (count
== 0 || newspi
== 0) {
6036 ipseclog((LOG_DEBUG
, "key_do_getnewspi: to allocate spi is failed.\n"));
6042 keystat
.getspi_count
=
6043 (keystat
.getspi_count
+ key_spi_trycnt
- count
) / 2;
6049 * SADB_UPDATE processing
6051 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
6052 * key(AE), (identity(SD),) (sensitivity)>
6053 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
6055 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
6056 * (identity(SD),) (sensitivity)>
6059 * m will always be freed.
6065 const struct sadb_msghdr
*mhp
)
6067 struct sadb_sa
*sa0
;
6068 struct sadb_address
*src0
, *dst0
;
6069 struct secasindex saidx
;
6070 struct secashead
*sah
;
6071 struct secasvar
*sav
;
6077 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6080 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6081 panic("key_update: NULL pointer is passed.\n");
6083 /* map satype to proto */
6084 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6085 ipseclog((LOG_DEBUG
, "key_update: invalid satype is passed.\n"));
6086 return key_senderror(so
, m
, EINVAL
);
6089 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
6090 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6091 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
6092 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
6093 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
6094 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
6095 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
6096 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
6097 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
6098 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
6099 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
6100 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
6101 return key_senderror(so
, m
, EINVAL
);
6103 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
6104 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6105 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
6106 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
6107 return key_senderror(so
, m
, EINVAL
);
6109 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
6110 mode
= ((struct sadb_x_sa2
*)
6111 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
6112 reqid
= ((struct sadb_x_sa2
*)
6113 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
6115 mode
= IPSEC_MODE_ANY
;
6118 /* XXX boundary checking for other extensions */
6120 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
6121 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
6122 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
6124 /* XXX boundary check against sa_len */
6125 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
6127 lck_mtx_lock(sadb_mutex
);
6129 /* get a SA header */
6130 if ((sah
= key_getsah(&saidx
)) == NULL
) {
6131 lck_mtx_unlock(sadb_mutex
);
6132 ipseclog((LOG_DEBUG
, "key_update: no SA index found.\n"));
6133 return key_senderror(so
, m
, ENOENT
);
6136 /* set spidx if there */
6138 error
= key_setident(sah
, m
, mhp
);
6140 lck_mtx_unlock(sadb_mutex
);
6141 return key_senderror(so
, m
, error
);
6144 /* find a SA with sequence number. */
6145 #if IPSEC_DOSEQCHECK
6146 if (mhp
->msg
->sadb_msg_seq
!= 0
6147 && (sav
= key_getsavbyseq(sah
, mhp
->msg
->sadb_msg_seq
)) == NULL
) {
6148 lck_mtx_unlock(sadb_mutex
);
6149 ipseclog((LOG_DEBUG
,
6150 "key_update: no larval SA with sequence %u exists.\n",
6151 mhp
->msg
->sadb_msg_seq
));
6152 return key_senderror(so
, m
, ENOENT
);
6155 if ((sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
)) == NULL
) {
6156 lck_mtx_unlock(sadb_mutex
);
6157 ipseclog((LOG_DEBUG
,
6158 "key_update: no such a SA found (spi:%u)\n",
6159 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
6160 return key_senderror(so
, m
, EINVAL
);
6164 /* validity check */
6165 if (sav
->sah
->saidx
.proto
!= proto
) {
6166 lck_mtx_unlock(sadb_mutex
);
6167 ipseclog((LOG_DEBUG
,
6168 "key_update: protocol mismatched (DB=%u param=%u)\n",
6169 sav
->sah
->saidx
.proto
, proto
));
6170 return key_senderror(so
, m
, EINVAL
);
6172 #if IPSEC_DOSEQCHECK
6173 if (sav
->spi
!= sa0
->sadb_sa_spi
) {
6174 lck_mtx_unlock(sadb_mutex
);
6175 ipseclog((LOG_DEBUG
,
6176 "key_update: SPI mismatched (DB:%u param:%u)\n",
6177 (u_int32_t
)ntohl(sav
->spi
),
6178 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
6179 return key_senderror(so
, m
, EINVAL
);
6182 if (sav
->pid
!= mhp
->msg
->sadb_msg_pid
) {
6183 lck_mtx_unlock(sadb_mutex
);
6184 ipseclog((LOG_DEBUG
,
6185 "key_update: pid mismatched (DB:%u param:%u)\n",
6186 sav
->pid
, mhp
->msg
->sadb_msg_pid
));
6187 return key_senderror(so
, m
, EINVAL
);
6190 /* copy sav values */
6191 error
= key_setsaval(sav
, m
, mhp
);
6193 key_freesav(sav
, KEY_SADB_LOCKED
);
6194 lck_mtx_unlock(sadb_mutex
);
6195 return key_senderror(so
, m
, error
);
6199 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
6200 * this SA is for transport mode - otherwise clear it.
6202 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0 &&
6203 (sav
->sah
->saidx
.mode
!= IPSEC_MODE_TRANSPORT
||
6204 sav
->sah
->saidx
.src
.ss_family
!= AF_INET
))
6205 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
6207 /* check SA values to be mature. */
6208 if ((error
= key_mature(sav
)) != 0) {
6209 key_freesav(sav
, KEY_SADB_LOCKED
);
6210 lck_mtx_unlock(sadb_mutex
);
6211 return key_senderror(so
, m
, error
);
6214 lck_mtx_unlock(sadb_mutex
);
6219 /* set msg buf from mhp */
6220 n
= key_getmsgbuf_x1(m
, mhp
);
6222 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
6223 return key_senderror(so
, m
, ENOBUFS
);
6227 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
6232 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
6233 * only called by key_update().
6236 * others : found, pointer to a SA.
6238 #if IPSEC_DOSEQCHECK
6239 static struct secasvar
*
6241 struct secashead
*sah
,
6244 struct secasvar
*sav
;
6247 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6249 state
= SADB_SASTATE_LARVAL
;
6251 /* search SAD with sequence number ? */
6252 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
6254 KEY_CHKSASTATE(state
, sav
->state
, "key_getsabyseq");
6256 if (sav
->seq
== seq
) {
6258 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
6259 printf("DP key_getsavbyseq cause "
6260 "refcnt++:%d SA:%p\n",
6271 * SADB_ADD processing
6272 * add a entry to SA database, when received
6273 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
6274 * key(AE), (identity(SD),) (sensitivity)>
6277 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
6278 * (identity(SD),) (sensitivity)>
6281 * IGNORE identity and sensitivity messages.
6283 * m will always be freed.
6289 const struct sadb_msghdr
*mhp
)
6291 struct sadb_sa
*sa0
;
6292 struct sadb_address
*src0
, *dst0
;
6293 struct secasindex saidx
;
6294 struct secashead
*newsah
;
6295 struct secasvar
*newsav
;
6301 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6304 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6305 panic("key_add: NULL pointer is passed.\n");
6307 /* map satype to proto */
6308 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6309 ipseclog((LOG_DEBUG
, "key_add: invalid satype is passed.\n"));
6310 return key_senderror(so
, m
, EINVAL
);
6313 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
6314 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6315 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
6316 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
6317 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
6318 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
6319 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
6320 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
6321 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
6322 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
6323 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
6324 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
6325 return key_senderror(so
, m
, EINVAL
);
6327 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
6328 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6329 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
6331 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
6332 return key_senderror(so
, m
, EINVAL
);
6334 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
6335 mode
= ((struct sadb_x_sa2
*)
6336 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
6337 reqid
= ((struct sadb_x_sa2
*)
6338 (void *)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
6340 mode
= IPSEC_MODE_ANY
;
6344 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
6345 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
6346 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
6348 /* XXX boundary check against sa_len */
6349 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
6351 lck_mtx_lock(sadb_mutex
);
6353 /* get a SA header */
6354 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
6355 /* create a new SA header: key_addspi is always used for outbound spi */
6356 if ((newsah
= key_newsah(&saidx
, IPSEC_DIR_OUTBOUND
)) == NULL
) {
6357 lck_mtx_unlock(sadb_mutex
);
6358 ipseclog((LOG_DEBUG
, "key_add: No more memory.\n"));
6359 return key_senderror(so
, m
, ENOBUFS
);
6363 /* set spidx if there */
6365 error
= key_setident(newsah
, m
, mhp
);
6367 lck_mtx_unlock(sadb_mutex
);
6368 return key_senderror(so
, m
, error
);
6371 /* create new SA entry. */
6372 /* We can create new SA only if SPI is different. */
6373 if (key_getsavbyspi(newsah
, sa0
->sadb_sa_spi
)) {
6374 lck_mtx_unlock(sadb_mutex
);
6375 ipseclog((LOG_DEBUG
, "key_add: SA already exists.\n"));
6376 return key_senderror(so
, m
, EEXIST
);
6378 newsav
= key_newsav(m
, mhp
, newsah
, &error
);
6379 if (newsav
== NULL
) {
6380 lck_mtx_unlock(sadb_mutex
);
6381 return key_senderror(so
, m
, error
);
6385 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
6386 * this SA is for transport mode - otherwise clear it.
6388 if ((newsav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0 &&
6389 (newsah
->saidx
.mode
!= IPSEC_MODE_TRANSPORT
||
6390 newsah
->saidx
.dst
.ss_family
!= AF_INET
))
6391 newsav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
6393 /* check SA values to be mature. */
6394 if ((error
= key_mature(newsav
)) != 0) {
6395 key_freesav(newsav
, KEY_SADB_LOCKED
);
6396 lck_mtx_unlock(sadb_mutex
);
6397 return key_senderror(so
, m
, error
);
6400 lck_mtx_unlock(sadb_mutex
);
6403 * don't call key_freesav() here, as we would like to keep the SA
6404 * in the database on success.
6410 /* set msg buf from mhp */
6411 n
= key_getmsgbuf_x1(m
, mhp
);
6413 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
6414 return key_senderror(so
, m
, ENOBUFS
);
6418 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
6425 struct secashead
*sah
,
6427 const struct sadb_msghdr
*mhp
)
6429 const struct sadb_ident
*idsrc
, *iddst
;
6430 int idsrclen
, iddstlen
;
6432 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6435 if (sah
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6436 panic("key_setident: NULL pointer is passed.\n");
6438 /* don't make buffer if not there */
6439 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
&&
6440 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
6446 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
||
6447 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
6448 ipseclog((LOG_DEBUG
, "key_setident: invalid identity.\n"));
6452 idsrc
= (const struct sadb_ident
*)
6453 (void *)mhp
->ext
[SADB_EXT_IDENTITY_SRC
];
6454 iddst
= (const struct sadb_ident
*)
6455 (void *)mhp
->ext
[SADB_EXT_IDENTITY_DST
];
6456 idsrclen
= mhp
->extlen
[SADB_EXT_IDENTITY_SRC
];
6457 iddstlen
= mhp
->extlen
[SADB_EXT_IDENTITY_DST
];
6459 /* validity check */
6460 if (idsrc
->sadb_ident_type
!= iddst
->sadb_ident_type
) {
6461 ipseclog((LOG_DEBUG
, "key_setident: ident type mismatch.\n"));
6465 switch (idsrc
->sadb_ident_type
) {
6466 case SADB_IDENTTYPE_PREFIX
:
6467 case SADB_IDENTTYPE_FQDN
:
6468 case SADB_IDENTTYPE_USERFQDN
:
6470 /* XXX do nothing */
6476 /* make structure */
6477 KMALLOC_NOWAIT(sah
->idents
, struct sadb_ident
*, idsrclen
);
6478 if (sah
->idents
== NULL
) {
6479 lck_mtx_unlock(sadb_mutex
);
6480 KMALLOC_WAIT(sah
->idents
, struct sadb_ident
*, idsrclen
);
6481 lck_mtx_lock(sadb_mutex
);
6482 if (sah
->idents
== NULL
) {
6483 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
6487 KMALLOC_NOWAIT(sah
->identd
, struct sadb_ident
*, iddstlen
);
6488 if (sah
->identd
== NULL
) {
6489 lck_mtx_unlock(sadb_mutex
);
6490 KMALLOC_WAIT(sah
->identd
, struct sadb_ident
*, iddstlen
);
6491 lck_mtx_lock(sadb_mutex
);
6492 if (sah
->identd
== NULL
) {
6495 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
6499 bcopy(idsrc
, sah
->idents
, idsrclen
);
6500 bcopy(iddst
, sah
->identd
, iddstlen
);
6506 * m will not be freed on return.
6507 * it is caller's responsibility to free the result.
6509 static struct mbuf
*
6512 const struct sadb_msghdr
*mhp
)
6515 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
6516 SADB_X_EXT_SA2
, SADB_EXT_ADDRESS_SRC
,
6517 SADB_EXT_ADDRESS_DST
, SADB_EXT_LIFETIME_HARD
,
6518 SADB_EXT_LIFETIME_SOFT
, SADB_EXT_IDENTITY_SRC
,
6519 SADB_EXT_IDENTITY_DST
};
6522 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6523 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
6525 /* create new sadb_msg to reply. */
6526 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
6530 if (n
->m_len
< sizeof(struct sadb_msg
)) {
6531 n
= m_pullup(n
, sizeof(struct sadb_msg
));
6535 mtod(n
, struct sadb_msg
*)->sadb_msg_errno
= 0;
6536 mtod(n
, struct sadb_msg
*)->sadb_msg_len
=
6537 PFKEY_UNIT64(n
->m_pkthdr
.len
);
6542 static int key_delete_all(struct socket
*, struct mbuf
*,
6543 const struct sadb_msghdr
*, u_int16_t
);
6546 * SADB_DELETE processing
6548 * <base, SA(*), address(SD)>
6549 * from the ikmpd, and set SADB_SASTATE_DEAD,
6551 * <base, SA(*), address(SD)>
6554 * m will always be freed.
6560 const struct sadb_msghdr
*mhp
)
6562 struct sadb_sa
*sa0
;
6563 struct sadb_address
*src0
, *dst0
;
6564 struct secasindex saidx
;
6565 struct secashead
*sah
;
6566 struct secasvar
*sav
= NULL
;
6569 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6572 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6573 panic("key_delete: NULL pointer is passed.\n");
6575 /* map satype to proto */
6576 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6577 ipseclog((LOG_DEBUG
, "key_delete: invalid satype is passed.\n"));
6578 return key_senderror(so
, m
, EINVAL
);
6581 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6582 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
6583 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
6584 return key_senderror(so
, m
, EINVAL
);
6587 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6588 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
6589 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
6590 return key_senderror(so
, m
, EINVAL
);
6593 lck_mtx_lock(sadb_mutex
);
6595 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
6597 * Caller wants us to delete all non-LARVAL SAs
6598 * that match the src/dst. This is used during
6599 * IKE INITIAL-CONTACT.
6601 ipseclog((LOG_DEBUG
, "key_delete: doing delete all.\n"));
6602 /* key_delete_all will unlock sadb_mutex */
6603 return key_delete_all(so
, m
, mhp
, proto
);
6604 } else if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
)) {
6605 lck_mtx_unlock(sadb_mutex
);
6606 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
6607 return key_senderror(so
, m
, EINVAL
);
6610 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
6611 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
6612 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
6614 /* XXX boundary check against sa_len */
6615 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
6617 /* get a SA header */
6618 LIST_FOREACH(sah
, &sahtree
, chain
) {
6619 if (sah
->state
== SADB_SASTATE_DEAD
)
6621 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
6624 /* get a SA with SPI. */
6625 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
6630 lck_mtx_unlock(sadb_mutex
);
6631 ipseclog((LOG_DEBUG
, "key_delete: no SA found.\n"));
6632 return key_senderror(so
, m
, ENOENT
);
6635 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6636 key_freesav(sav
, KEY_SADB_LOCKED
);
6638 lck_mtx_unlock(sadb_mutex
);
6643 struct sadb_msg
*newmsg
;
6644 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
6645 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
6647 /* create new sadb_msg to reply. */
6648 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
6650 return key_senderror(so
, m
, ENOBUFS
);
6652 if (n
->m_len
< sizeof(struct sadb_msg
)) {
6653 n
= m_pullup(n
, sizeof(struct sadb_msg
));
6655 return key_senderror(so
, m
, ENOBUFS
);
6657 newmsg
= mtod(n
, struct sadb_msg
*);
6658 newmsg
->sadb_msg_errno
= 0;
6659 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
6662 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
6667 * delete all SAs for src/dst. Called from key_delete().
6673 const struct sadb_msghdr
*mhp
,
6676 struct sadb_address
*src0
, *dst0
;
6677 struct secasindex saidx
;
6678 struct secashead
*sah
;
6679 struct secasvar
*sav
, *nextsav
;
6680 u_int stateidx
, state
;
6682 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6684 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
6685 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
6687 /* XXX boundary check against sa_len */
6688 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
6690 LIST_FOREACH(sah
, &sahtree
, chain
) {
6691 if (sah
->state
== SADB_SASTATE_DEAD
)
6693 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
6696 /* Delete all non-LARVAL SAs. */
6698 stateidx
< _ARRAYLEN(saorder_state_alive
);
6700 state
= saorder_state_alive
[stateidx
];
6701 if (state
== SADB_SASTATE_LARVAL
)
6703 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
6704 sav
!= NULL
; sav
= nextsav
) {
6705 nextsav
= LIST_NEXT(sav
, chain
);
6707 if (sav
->state
!= state
) {
6708 ipseclog((LOG_DEBUG
, "key_delete_all: "
6709 "invalid sav->state "
6710 "(queue: %d SA: %d)\n",
6711 state
, sav
->state
));
6715 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6716 key_freesav(sav
, KEY_SADB_LOCKED
);
6720 lck_mtx_unlock(sadb_mutex
);
6724 struct sadb_msg
*newmsg
;
6725 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_ADDRESS_SRC
,
6726 SADB_EXT_ADDRESS_DST
};
6728 /* create new sadb_msg to reply. */
6729 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
6731 return key_senderror(so
, m
, ENOBUFS
);
6733 if (n
->m_len
< sizeof(struct sadb_msg
)) {
6734 n
= m_pullup(n
, sizeof(struct sadb_msg
));
6736 return key_senderror(so
, m
, ENOBUFS
);
6738 newmsg
= mtod(n
, struct sadb_msg
*);
6739 newmsg
->sadb_msg_errno
= 0;
6740 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
6743 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
6748 * SADB_GET processing
6750 * <base, SA(*), address(SD)>
6751 * from the ikmpd, and get a SP and a SA to respond,
6753 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
6754 * (identity(SD),) (sensitivity)>
6757 * m will always be freed.
6763 const struct sadb_msghdr
*mhp
)
6765 struct sadb_sa
*sa0
;
6766 struct sadb_address
*src0
, *dst0
;
6767 struct secasindex saidx
;
6768 struct secashead
*sah
;
6769 struct secasvar
*sav
= NULL
;
6772 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6775 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6776 panic("key_get: NULL pointer is passed.\n");
6778 /* map satype to proto */
6779 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6780 ipseclog((LOG_DEBUG
, "key_get: invalid satype is passed.\n"));
6781 return key_senderror(so
, m
, EINVAL
);
6784 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
6785 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6786 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
6787 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
6788 return key_senderror(so
, m
, EINVAL
);
6790 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
6791 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6792 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
6793 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
6794 return key_senderror(so
, m
, EINVAL
);
6797 sa0
= (struct sadb_sa
*)(void *)mhp
->ext
[SADB_EXT_SA
];
6798 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
6799 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
6801 /* XXX boundary check against sa_len */
6802 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
6804 lck_mtx_lock(sadb_mutex
);
6806 /* get a SA header */
6807 LIST_FOREACH(sah
, &sahtree
, chain
) {
6808 if (sah
->state
== SADB_SASTATE_DEAD
)
6810 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
6813 /* get a SA with SPI. */
6814 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
6819 lck_mtx_unlock(sadb_mutex
);
6820 ipseclog((LOG_DEBUG
, "key_get: no SA found.\n"));
6821 return key_senderror(so
, m
, ENOENT
);
6828 /* map proto to satype */
6829 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
6830 lck_mtx_unlock(sadb_mutex
);
6831 ipseclog((LOG_DEBUG
, "key_get: there was invalid proto in SAD.\n"));
6832 return key_senderror(so
, m
, EINVAL
);
6834 lck_mtx_unlock(sadb_mutex
);
6836 /* create new sadb_msg to reply. */
6837 n
= key_setdumpsa(sav
, SADB_GET
, satype
, mhp
->msg
->sadb_msg_seq
,
6838 mhp
->msg
->sadb_msg_pid
);
6843 return key_senderror(so
, m
, ENOBUFS
);
6846 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
6851 * get SA stats by spi.
6852 * OUT: -1 : not found
6853 * 0 : found, arg pointer to a SA stats is updated.
6856 key_getsastatbyspi_one (u_int32_t spi
,
6857 struct sastat
*stat
)
6859 struct secashead
*sah
;
6860 struct secasvar
*sav
= NULL
;
6862 if ((void *)stat
== NULL
) {
6866 lck_mtx_lock(sadb_mutex
);
6868 /* get a SA header */
6869 LIST_FOREACH(sah
, &sahtree
, chain
) {
6870 if (sah
->state
== SADB_SASTATE_DEAD
)
6873 /* get a SA with SPI. */
6874 sav
= key_getsavbyspi(sah
, spi
);
6876 stat
->spi
= sav
->spi
;
6877 stat
->created
= sav
->created
;
6879 bcopy(sav
->lft_c
,&stat
->lft_c
, sizeof(stat
->lft_c
));
6881 bzero(&stat
->lft_c
, sizeof(stat
->lft_c
));
6883 lck_mtx_unlock(sadb_mutex
);
6888 lck_mtx_unlock(sadb_mutex
);
6894 * get SA stats collection by indices.
6895 * OUT: -1 : not found
6896 * 0 : found, arg pointers to a SA stats and 'maximum stats' are updated.
6899 key_getsastatbyspi (struct sastat
*stat_arg
,
6900 u_int32_t max_stat_arg
,
6901 struct sastat
*stat_res
,
6902 u_int32_t
*max_stat_res
)
6906 if (stat_arg
== NULL
||
6908 max_stat_res
== NULL
) {
6912 for (cur
= 0; cur
< max_stat_arg
; cur
++) {
6913 if (key_getsastatbyspi_one(stat_arg
[cur
].spi
,
6914 &stat_res
[found
]) == 0) {
6918 *max_stat_res
= found
;
6926 /* XXX make it sysctl-configurable? */
6928 key_getcomb_setlifetime(
6929 struct sadb_comb
*comb
)
6932 comb
->sadb_comb_soft_allocations
= 1;
6933 comb
->sadb_comb_hard_allocations
= 1;
6934 comb
->sadb_comb_soft_bytes
= 0;
6935 comb
->sadb_comb_hard_bytes
= 0;
6936 comb
->sadb_comb_hard_addtime
= 86400; /* 1 day */
6937 comb
->sadb_comb_soft_addtime
= comb
->sadb_comb_soft_addtime
* 80 / 100;
6938 comb
->sadb_comb_soft_usetime
= 28800; /* 8 hours */
6939 comb
->sadb_comb_hard_usetime
= comb
->sadb_comb_hard_usetime
* 80 / 100;
6944 * XXX reorder combinations by preference
6945 * XXX no idea if the user wants ESP authentication or not
6947 static struct mbuf
*
6948 key_getcomb_esp(void)
6950 struct sadb_comb
*comb
;
6951 const struct esp_algorithm
*algo
;
6952 struct mbuf
*result
= NULL
, *m
, *n
;
6956 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
6959 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
6960 algo
= esp_algorithm_lookup(i
);
6964 if (algo
->keymax
< ipsec_esp_keymin
)
6966 if (algo
->keymin
< ipsec_esp_keymin
)
6967 encmin
= ipsec_esp_keymin
;
6969 encmin
= algo
->keymin
;
6972 m
= key_getcomb_ah();
6976 panic("assumption failed in key_getcomb_esp");
6978 MGET(m
, M_WAITOK
, MT_DATA
);
6983 bzero(mtod(m
, caddr_t
), m
->m_len
);
6990 for (n
= m
; n
; n
= n
->m_next
)
6994 panic("assumption failed in key_getcomb_esp");
6997 for (off
= 0; off
< totlen
; off
+= l
) {
6998 n
= m_pulldown(m
, off
, l
, &o
);
7000 /* m is already freed */
7003 comb
= (struct sadb_comb
*)
7004 (void *)(mtod(n
, caddr_t
) + o
);
7005 bzero(comb
, sizeof(*comb
));
7006 key_getcomb_setlifetime(comb
);
7007 comb
->sadb_comb_encrypt
= i
;
7008 comb
->sadb_comb_encrypt_minbits
= encmin
;
7009 comb
->sadb_comb_encrypt_maxbits
= algo
->keymax
;
7028 * XXX reorder combinations by preference
7030 static struct mbuf
*
7031 key_getcomb_ah(void)
7033 struct sadb_comb
*comb
;
7034 const struct ah_algorithm
*algo
;
7038 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
7041 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
7043 /* we prefer HMAC algorithms, not old algorithms */
7044 if (i
!= SADB_AALG_SHA1HMAC
&& i
!= SADB_AALG_MD5HMAC
)
7047 algo
= ah_algorithm_lookup(i
);
7051 if (algo
->keymax
< ipsec_ah_keymin
)
7053 if (algo
->keymin
< ipsec_ah_keymin
)
7054 keymin
= ipsec_ah_keymin
;
7056 keymin
= algo
->keymin
;
7061 panic("assumption failed in key_getcomb_ah");
7063 MGET(m
, M_WAITOK
, MT_DATA
);
7070 M_PREPEND(m
, l
, M_WAITOK
);
7074 comb
= mtod(m
, struct sadb_comb
*);
7075 bzero(comb
, sizeof(*comb
));
7076 key_getcomb_setlifetime(comb
);
7077 comb
->sadb_comb_auth
= i
;
7078 comb
->sadb_comb_auth_minbits
= keymin
;
7079 comb
->sadb_comb_auth_maxbits
= algo
->keymax
;
7086 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
7087 * XXX reorder combinations by preference
7089 static struct mbuf
*
7090 key_getcomb_ipcomp(void)
7092 struct sadb_comb
*comb
;
7093 const struct ipcomp_algorithm
*algo
;
7096 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
7099 for (i
= 1; i
<= SADB_X_CALG_MAX
; i
++) {
7100 algo
= ipcomp_algorithm_lookup(i
);
7107 panic("assumption failed in key_getcomb_ipcomp");
7109 MGET(m
, M_WAITOK
, MT_DATA
);
7116 M_PREPEND(m
, l
, M_WAITOK
);
7120 comb
= mtod(m
, struct sadb_comb
*);
7121 bzero(comb
, sizeof(*comb
));
7122 key_getcomb_setlifetime(comb
);
7123 comb
->sadb_comb_encrypt
= i
;
7124 /* what should we set into sadb_comb_*_{min,max}bits? */
7131 * XXX no way to pass mode (transport/tunnel) to userland
7132 * XXX replay checking?
7133 * XXX sysctl interface to ipsec_{ah,esp}_keymin
7135 static struct mbuf
*
7137 const struct secasindex
*saidx
)
7139 struct sadb_prop
*prop
;
7141 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_prop
));
7144 switch (saidx
->proto
) {
7147 m
= key_getcomb_esp();
7151 m
= key_getcomb_ah();
7153 case IPPROTO_IPCOMP
:
7154 m
= key_getcomb_ipcomp();
7162 M_PREPEND(m
, l
, M_WAITOK
);
7167 for (n
= m
; n
; n
= n
->m_next
)
7170 prop
= mtod(m
, struct sadb_prop
*);
7171 bzero(prop
, sizeof(*prop
));
7172 prop
->sadb_prop_len
= PFKEY_UNIT64(totlen
);
7173 prop
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
7174 prop
->sadb_prop_replay
= 32; /* XXX */
7180 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
7182 * <base, SA, address(SD), (address(P)), x_policy,
7183 * (identity(SD),) (sensitivity,) proposal>
7184 * to KMD, and expect to receive
7185 * <base> with SADB_ACQUIRE if error occurred,
7187 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
7188 * from KMD by PF_KEY.
7190 * XXX x_policy is outside of RFC2367 (KAME extension).
7191 * XXX sensitivity is not supported.
7192 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
7193 * see comment for key_getcomb_ipcomp().
7197 * others: error number
7201 struct secasindex
*saidx
,
7202 struct secpolicy
*sp
)
7204 struct mbuf
*result
= NULL
, *m
;
7205 #ifndef IPSEC_NONBLOCK_ACQUIRE
7206 struct secacq
*newacq
;
7212 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7216 panic("key_acquire: NULL pointer is passed.\n");
7217 if ((satype
= key_proto2satype(saidx
->proto
)) == 0)
7218 panic("key_acquire: invalid proto is passed.\n");
7220 #ifndef IPSEC_NONBLOCK_ACQUIRE
7222 * We never do anything about acquirng SA. There is anather
7223 * solution that kernel blocks to send SADB_ACQUIRE message until
7224 * getting something message from IKEd. In later case, to be
7225 * managed with ACQUIRING list.
7227 /* get a entry to check whether sending message or not. */
7228 lck_mtx_lock(sadb_mutex
);
7229 if ((newacq
= key_getacq(saidx
)) != NULL
) {
7230 if (key_blockacq_count
< newacq
->count
) {
7231 /* reset counter and do send message. */
7234 /* increment counter and do nothing. */
7236 lck_mtx_unlock(sadb_mutex
);
7240 /* make new entry for blocking to send SADB_ACQUIRE. */
7241 if ((newacq
= key_newacq(saidx
)) == NULL
) {
7242 lck_mtx_unlock(sadb_mutex
);
7246 /* add to acqtree */
7247 LIST_INSERT_HEAD(&acqtree
, newacq
, chain
);
7250 lck_mtx_unlock(sadb_mutex
);
7253 seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
7255 m
= key_setsadbmsg(SADB_ACQUIRE
, 0, satype
, seq
, 0, 0);
7262 /* set sadb_address for saidx's. */
7263 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
7264 (struct sockaddr
*)&saidx
->src
, FULLMASK
, IPSEC_ULPROTO_ANY
);
7271 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
7272 (struct sockaddr
*)&saidx
->dst
, FULLMASK
, IPSEC_ULPROTO_ANY
);
7279 /* XXX proxy address (optional) */
7281 /* set sadb_x_policy */
7283 m
= key_setsadbxpolicy(sp
->policy
, sp
->spidx
.dir
, sp
->id
);
7291 /* XXX identity (optional) */
7293 if (idexttype
&& fqdn
) {
7294 /* create identity extension (FQDN) */
7295 struct sadb_ident
*id
;
7298 fqdnlen
= strlen(fqdn
) + 1; /* +1 for terminating-NUL */
7299 id
= (struct sadb_ident
*)p
;
7300 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
7301 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
7302 id
->sadb_ident_exttype
= idexttype
;
7303 id
->sadb_ident_type
= SADB_IDENTTYPE_FQDN
;
7304 bcopy(fqdn
, id
+ 1, fqdnlen
);
7305 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(fqdnlen
);
7309 /* create identity extension (USERFQDN) */
7310 struct sadb_ident
*id
;
7314 /* +1 for terminating-NUL */
7315 userfqdnlen
= strlen(userfqdn
) + 1;
7318 id
= (struct sadb_ident
*)p
;
7319 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
7320 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
7321 id
->sadb_ident_exttype
= idexttype
;
7322 id
->sadb_ident_type
= SADB_IDENTTYPE_USERFQDN
;
7323 /* XXX is it correct? */
7324 if (curproc
&& curproc
->p_cred
)
7325 id
->sadb_ident_id
= curproc
->p_cred
->p_ruid
;
7326 if (userfqdn
&& userfqdnlen
)
7327 bcopy(userfqdn
, id
+ 1, userfqdnlen
);
7328 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(userfqdnlen
);
7332 /* XXX sensitivity (optional) */
7334 /* create proposal/combination extension */
7335 m
= key_getprop(saidx
);
7338 * spec conformant: always attach proposal/combination extension,
7339 * the problem is that we have no way to attach it for ipcomp,
7340 * due to the way sadb_comb is declared in RFC2367.
7349 * outside of spec; make proposal/combination extension optional.
7355 if ((result
->m_flags
& M_PKTHDR
) == 0) {
7360 if (result
->m_len
< sizeof(struct sadb_msg
)) {
7361 result
= m_pullup(result
, sizeof(struct sadb_msg
));
7362 if (result
== NULL
) {
7368 result
->m_pkthdr
.len
= 0;
7369 for (m
= result
; m
; m
= m
->m_next
)
7370 result
->m_pkthdr
.len
+= m
->m_len
;
7372 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
7373 PFKEY_UNIT64(result
->m_pkthdr
.len
);
7375 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
7383 #ifndef IPSEC_NONBLOCK_ACQUIRE
7384 static struct secacq
*
7386 struct secasindex
*saidx
)
7388 struct secacq
*newacq
;
7392 KMALLOC_NOWAIT(newacq
, struct secacq
*, sizeof(struct secacq
));
7393 if (newacq
== NULL
) {
7394 lck_mtx_unlock(sadb_mutex
);
7395 KMALLOC_WAIT(newacq
, struct secacq
*, sizeof(struct secacq
));
7396 lck_mtx_lock(sadb_mutex
);
7397 if (newacq
== NULL
) {
7398 ipseclog((LOG_DEBUG
, "key_newacq: No more memory.\n"));
7402 bzero(newacq
, sizeof(*newacq
));
7405 bcopy(saidx
, &newacq
->saidx
, sizeof(newacq
->saidx
));
7406 newacq
->seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
);
7408 newacq
->created
= tv
.tv_sec
;
7414 static struct secacq
*
7416 struct secasindex
*saidx
)
7420 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7422 LIST_FOREACH(acq
, &acqtree
, chain
) {
7423 if (key_cmpsaidx(saidx
, &acq
->saidx
, CMP_EXACTLY
))
7430 static struct secacq
*
7436 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7438 LIST_FOREACH(acq
, &acqtree
, chain
) {
7439 if (acq
->seq
== seq
)
7447 static struct secspacq
*
7449 struct secpolicyindex
*spidx
)
7451 struct secspacq
*acq
;
7455 KMALLOC_NOWAIT(acq
, struct secspacq
*, sizeof(struct secspacq
));
7457 lck_mtx_unlock(sadb_mutex
);
7458 KMALLOC_WAIT(acq
, struct secspacq
*, sizeof(struct secspacq
));
7459 lck_mtx_lock(sadb_mutex
);
7461 ipseclog((LOG_DEBUG
, "key_newspacq: No more memory.\n"));
7465 bzero(acq
, sizeof(*acq
));
7468 bcopy(spidx
, &acq
->spidx
, sizeof(acq
->spidx
));
7470 acq
->created
= tv
.tv_sec
;
7476 static struct secspacq
*
7478 struct secpolicyindex
*spidx
)
7480 struct secspacq
*acq
;
7482 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7484 LIST_FOREACH(acq
, &spacqtree
, chain
) {
7485 if (key_cmpspidx_exactly(spidx
, &acq
->spidx
))
7493 * SADB_ACQUIRE processing,
7494 * in first situation, is receiving
7496 * from the ikmpd, and clear sequence of its secasvar entry.
7498 * In second situation, is receiving
7499 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
7500 * from a user land process, and return
7501 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
7504 * m will always be freed.
7510 const struct sadb_msghdr
*mhp
)
7512 const struct sadb_address
*src0
, *dst0
;
7513 struct secasindex saidx
;
7514 struct secashead
*sah
;
7520 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7521 panic("key_acquire2: NULL pointer is passed.\n");
7524 * Error message from KMd.
7525 * We assume that if error was occurred in IKEd, the length of PFKEY
7526 * message is equal to the size of sadb_msg structure.
7527 * We do not raise error even if error occurred in this function.
7529 lck_mtx_lock(sadb_mutex
);
7531 if (mhp
->msg
->sadb_msg_len
== PFKEY_UNIT64(sizeof(struct sadb_msg
))) {
7532 #ifndef IPSEC_NONBLOCK_ACQUIRE
7536 /* check sequence number */
7537 if (mhp
->msg
->sadb_msg_seq
== 0) {
7538 lck_mtx_unlock(sadb_mutex
);
7539 ipseclog((LOG_DEBUG
, "key_acquire2: must specify sequence number.\n"));
7544 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) == NULL
) {
7546 * the specified larval SA is already gone, or we got
7547 * a bogus sequence number. we can silently ignore it.
7549 lck_mtx_unlock(sadb_mutex
);
7554 /* reset acq counter in order to deletion by timehander. */
7556 acq
->created
= tv
.tv_sec
;
7559 lck_mtx_unlock(sadb_mutex
);
7565 * This message is from user land.
7568 /* map satype to proto */
7569 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7570 lck_mtx_unlock(sadb_mutex
);
7571 ipseclog((LOG_DEBUG
, "key_acquire2: invalid satype is passed.\n"));
7572 return key_senderror(so
, m
, EINVAL
);
7575 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
7576 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
7577 mhp
->ext
[SADB_EXT_PROPOSAL
] == NULL
) {
7579 lck_mtx_unlock(sadb_mutex
);
7580 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
7581 return key_senderror(so
, m
, EINVAL
);
7583 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7584 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
7585 mhp
->extlen
[SADB_EXT_PROPOSAL
] < sizeof(struct sadb_prop
)) {
7587 lck_mtx_unlock(sadb_mutex
);
7588 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
7589 return key_senderror(so
, m
, EINVAL
);
7592 src0
= (const struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
7593 dst0
= (const struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
7595 /* XXX boundary check against sa_len */
7597 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
7599 /* get a SA index */
7600 LIST_FOREACH(sah
, &sahtree
, chain
) {
7601 if (sah
->state
== SADB_SASTATE_DEAD
)
7603 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_MODE
| CMP_REQID
))
7607 lck_mtx_unlock(sadb_mutex
);
7608 ipseclog((LOG_DEBUG
, "key_acquire2: a SA exists already.\n"));
7609 return key_senderror(so
, m
, EEXIST
);
7611 lck_mtx_unlock(sadb_mutex
);
7612 error
= key_acquire(&saidx
, NULL
);
7614 ipseclog((LOG_DEBUG
, "key_acquire2: error %d returned "
7615 "from key_acquire.\n", mhp
->msg
->sadb_msg_errno
));
7616 return key_senderror(so
, m
, error
);
7619 return key_sendup_mbuf(so
, m
, KEY_SENDUP_REGISTERED
);
7623 * SADB_REGISTER processing.
7624 * If SATYPE_UNSPEC has been passed as satype, only return sadb_supported.
7627 * from the ikmpd, and register a socket to send PF_KEY messages,
7631 * If socket is detached, must free from regnode.
7633 * m will always be freed.
7639 const struct sadb_msghdr
*mhp
)
7641 struct secreg
*reg
, *newreg
= 0;
7644 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7645 panic("key_register: NULL pointer is passed.\n");
7647 /* check for invalid register message */
7648 if (mhp
->msg
->sadb_msg_satype
>= sizeof(regtree
)/sizeof(regtree
[0]))
7649 return key_senderror(so
, m
, EINVAL
);
7651 /* When SATYPE_UNSPEC is specified, only return sadb_supported. */
7652 if (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
)
7655 /* create regnode */
7656 KMALLOC_WAIT(newreg
, struct secreg
*, sizeof(*newreg
));
7657 if (newreg
== NULL
) {
7658 ipseclog((LOG_DEBUG
, "key_register: No more memory.\n"));
7659 return key_senderror(so
, m
, ENOBUFS
);
7661 bzero((caddr_t
)newreg
, sizeof(*newreg
));
7663 lck_mtx_lock(sadb_mutex
);
7664 /* check whether existing or not */
7665 LIST_FOREACH(reg
, ®tree
[mhp
->msg
->sadb_msg_satype
], chain
) {
7666 if (reg
->so
== so
) {
7667 lck_mtx_unlock(sadb_mutex
);
7668 ipseclog((LOG_DEBUG
, "key_register: socket exists already.\n"));
7670 return key_senderror(so
, m
, EEXIST
);
7676 ((struct keycb
*)sotorawcb(so
))->kp_registered
++;
7677 socket_unlock(so
, 1);
7679 /* add regnode to regtree. */
7680 LIST_INSERT_HEAD(®tree
[mhp
->msg
->sadb_msg_satype
], newreg
, chain
);
7681 lck_mtx_unlock(sadb_mutex
);
7685 struct sadb_msg
*newmsg
;
7686 struct sadb_supported
*sup
;
7687 u_int len
, alen
, elen
;
7690 struct sadb_alg
*alg
;
7692 /* create new sadb_msg to reply. */
7694 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
7695 if (ah_algorithm_lookup(i
))
7696 alen
+= sizeof(struct sadb_alg
);
7699 alen
+= sizeof(struct sadb_supported
);
7702 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
7703 if (esp_algorithm_lookup(i
))
7704 elen
+= sizeof(struct sadb_alg
);
7707 elen
+= sizeof(struct sadb_supported
);
7710 len
= sizeof(struct sadb_msg
) + alen
+ elen
;
7713 return key_senderror(so
, m
, ENOBUFS
);
7715 MGETHDR(n
, M_WAITOK
, MT_DATA
);
7716 if (n
&& len
> MHLEN
) {
7717 MCLGET(n
, M_WAITOK
);
7718 if ((n
->m_flags
& M_EXT
) == 0) {
7724 return key_senderror(so
, m
, ENOBUFS
);
7726 n
->m_pkthdr
.len
= n
->m_len
= len
;
7730 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
7731 newmsg
= mtod(n
, struct sadb_msg
*);
7732 newmsg
->sadb_msg_errno
= 0;
7733 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
7734 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
7736 /* for authentication algorithm */
7738 sup
= (struct sadb_supported
*)(void *)(mtod(n
, caddr_t
) + off
);
7739 sup
->sadb_supported_len
= PFKEY_UNIT64(alen
);
7740 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
7741 off
+= PFKEY_ALIGN8(sizeof(*sup
));
7743 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
7744 const struct ah_algorithm
*aalgo
;
7746 aalgo
= ah_algorithm_lookup(i
);
7749 alg
= (struct sadb_alg
*)
7750 (void *)(mtod(n
, caddr_t
) + off
);
7751 alg
->sadb_alg_id
= i
;
7752 alg
->sadb_alg_ivlen
= 0;
7753 alg
->sadb_alg_minbits
= aalgo
->keymin
;
7754 alg
->sadb_alg_maxbits
= aalgo
->keymax
;
7755 off
+= PFKEY_ALIGN8(sizeof(*alg
));
7760 /* for encryption algorithm */
7762 sup
= (struct sadb_supported
*)(void *)(mtod(n
, caddr_t
) + off
);
7763 sup
->sadb_supported_len
= PFKEY_UNIT64(elen
);
7764 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
7765 off
+= PFKEY_ALIGN8(sizeof(*sup
));
7767 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
7768 const struct esp_algorithm
*ealgo
;
7770 ealgo
= esp_algorithm_lookup(i
);
7773 alg
= (struct sadb_alg
*)
7774 (void *)(mtod(n
, caddr_t
) + off
);
7775 alg
->sadb_alg_id
= i
;
7776 if (ealgo
&& ealgo
->ivlen
) {
7778 * give NULL to get the value preferred by
7779 * algorithm XXX SADB_X_EXT_DERIV ?
7781 alg
->sadb_alg_ivlen
=
7782 (*ealgo
->ivlen
)(ealgo
, NULL
);
7784 alg
->sadb_alg_ivlen
= 0;
7785 alg
->sadb_alg_minbits
= ealgo
->keymin
;
7786 alg
->sadb_alg_maxbits
= ealgo
->keymax
;
7787 off
+= PFKEY_ALIGN8(sizeof(struct sadb_alg
));
7794 panic("length assumption failed in key_register");
7798 return key_sendup_mbuf(so
, n
, KEY_SENDUP_REGISTERED
);
7803 * free secreg entry registered.
7804 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
7815 panic("key_freereg: NULL pointer is passed.\n");
7818 * check whether existing or not.
7819 * check all type of SA, because there is a potential that
7820 * one socket is registered to multiple type of SA.
7822 lck_mtx_lock(sadb_mutex
);
7823 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
7824 LIST_FOREACH(reg
, ®tree
[i
], chain
) {
7826 && __LIST_CHAINED(reg
)) {
7827 LIST_REMOVE(reg
, chain
);
7833 lck_mtx_unlock(sadb_mutex
);
7838 * SADB_EXPIRE processing
7840 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
7842 * NOTE: We send only soft lifetime extension.
7845 * others : error number
7849 struct secasvar
*sav
)
7852 struct mbuf
*result
= NULL
, *m
;
7855 struct sadb_lifetime
*lt
;
7857 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7861 panic("key_expire: NULL pointer is passed.\n");
7862 if (sav
->sah
== NULL
)
7863 panic("key_expire: Why was SA index in SA NULL.\n");
7864 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0)
7865 panic("key_expire: invalid proto is passed.\n");
7867 /* set msg header */
7868 m
= key_setsadbmsg(SADB_EXPIRE
, 0, satype
, sav
->seq
, 0, sav
->refcnt
);
7875 /* create SA extension */
7876 m
= key_setsadbsa(sav
);
7883 /* create SA extension */
7884 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
7885 sav
->replay
? sav
->replay
->count
: 0,
7886 sav
->sah
->saidx
.reqid
);
7893 /* create lifetime extension (current and soft) */
7894 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
7895 m
= key_alloc_mbuf(len
);
7896 if (!m
|| m
->m_next
) { /*XXX*/
7902 bzero(mtod(m
, caddr_t
), len
);
7903 lt
= mtod(m
, struct sadb_lifetime
*);
7904 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
7905 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
7906 lt
->sadb_lifetime_allocations
= sav
->lft_c
->sadb_lifetime_allocations
;
7907 lt
->sadb_lifetime_bytes
= sav
->lft_c
->sadb_lifetime_bytes
;
7908 lt
->sadb_lifetime_addtime
= sav
->lft_c
->sadb_lifetime_addtime
;
7909 lt
->sadb_lifetime_usetime
= sav
->lft_c
->sadb_lifetime_usetime
;
7910 lt
= (struct sadb_lifetime
*)(void *)(mtod(m
, caddr_t
) + len
/ 2);
7911 bcopy(sav
->lft_s
, lt
, sizeof(*lt
));
7914 /* set sadb_address for source */
7915 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
7916 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
7917 FULLMASK
, IPSEC_ULPROTO_ANY
);
7924 /* set sadb_address for destination */
7925 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
7926 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
7927 FULLMASK
, IPSEC_ULPROTO_ANY
);
7934 if ((result
->m_flags
& M_PKTHDR
) == 0) {
7939 if (result
->m_len
< sizeof(struct sadb_msg
)) {
7940 result
= m_pullup(result
, sizeof(struct sadb_msg
));
7941 if (result
== NULL
) {
7947 result
->m_pkthdr
.len
= 0;
7948 for (m
= result
; m
; m
= m
->m_next
)
7949 result
->m_pkthdr
.len
+= m
->m_len
;
7951 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
7952 PFKEY_UNIT64(result
->m_pkthdr
.len
);
7954 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
7963 * SADB_FLUSH processing
7966 * from the ikmpd, and free all entries in secastree.
7970 * NOTE: to do is only marking SADB_SASTATE_DEAD.
7972 * m will always be freed.
7978 const struct sadb_msghdr
*mhp
)
7980 struct sadb_msg
*newmsg
;
7981 struct secashead
*sah
, *nextsah
;
7982 struct secasvar
*sav
, *nextsav
;
7988 if (so
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7989 panic("key_flush: NULL pointer is passed.\n");
7991 /* map satype to proto */
7992 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7993 ipseclog((LOG_DEBUG
, "key_flush: invalid satype is passed.\n"));
7994 return key_senderror(so
, m
, EINVAL
);
7997 lck_mtx_lock(sadb_mutex
);
7999 /* no SATYPE specified, i.e. flushing all SA. */
8000 for (sah
= LIST_FIRST(&sahtree
);
8003 nextsah
= LIST_NEXT(sah
, chain
);
8005 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
8006 && proto
!= sah
->saidx
.proto
)
8010 stateidx
< _ARRAYLEN(saorder_state_alive
);
8012 state
= saorder_state_any
[stateidx
];
8013 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
8017 nextsav
= LIST_NEXT(sav
, chain
);
8019 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
8020 key_freesav(sav
, KEY_SADB_LOCKED
);
8024 sah
->state
= SADB_SASTATE_DEAD
;
8026 lck_mtx_unlock(sadb_mutex
);
8028 if (m
->m_len
< sizeof(struct sadb_msg
) ||
8029 sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
8030 ipseclog((LOG_DEBUG
, "key_flush: No more memory.\n"));
8031 return key_senderror(so
, m
, ENOBUFS
);
8037 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct sadb_msg
);
8038 newmsg
= mtod(m
, struct sadb_msg
*);
8039 newmsg
->sadb_msg_errno
= 0;
8040 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
8042 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
8046 * SADB_DUMP processing
8047 * dump all entries including status of DEAD in SAD.
8050 * from the ikmpd, and dump all secasvar leaves
8055 * m will always be freed.
8058 struct sav_dump_elem
{
8059 struct secasvar
*sav
;
8067 const struct sadb_msghdr
*mhp
)
8069 struct secashead
*sah
;
8070 struct secasvar
*sav
;
8071 struct sav_dump_elem
*savbuf
= NULL
, *elem_ptr
;
8076 int cnt
= 0, cnt2
, bufcount
;
8080 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
8083 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
8084 panic("key_dump: NULL pointer is passed.\n");
8086 /* map satype to proto */
8087 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
8088 ipseclog((LOG_DEBUG
, "key_dump: invalid satype is passed.\n"));
8089 return key_senderror(so
, m
, EINVAL
);
8092 if ((bufcount
= ipsec_sav_count
) <= 0) {
8096 bufcount
+= 512; /* extra */
8097 KMALLOC_WAIT(savbuf
, struct sav_dump_elem
*, bufcount
* sizeof(struct sav_dump_elem
));
8098 if (savbuf
== NULL
) {
8099 ipseclog((LOG_DEBUG
, "key_dump: No more memory.\n"));
8104 /* count sav entries to be sent to the userland. */
8105 lck_mtx_lock(sadb_mutex
);
8107 LIST_FOREACH(sah
, &sahtree
, chain
) {
8108 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
8109 && proto
!= sah
->saidx
.proto
)
8112 /* map proto to satype */
8113 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
8114 lck_mtx_unlock(sadb_mutex
);
8115 ipseclog((LOG_DEBUG
, "key_dump: there was invalid proto in SAD.\n"));
8121 stateidx
< _ARRAYLEN(saorder_state_any
);
8123 state
= saorder_state_any
[stateidx
];
8124 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
8125 if (cnt
== bufcount
)
8126 break; /* out of buffer space */
8127 elem_ptr
->sav
= sav
;
8128 elem_ptr
->satype
= satype
;
8135 lck_mtx_unlock(sadb_mutex
);
8142 /* send this to the userland, one at a time. */
8146 n
= key_setdumpsa(elem_ptr
->sav
, SADB_DUMP
, elem_ptr
->satype
,
8147 --cnt2
, mhp
->msg
->sadb_msg_pid
);
8154 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
8162 lck_mtx_lock(sadb_mutex
);
8164 key_freesav((elem_ptr
++)->sav
, KEY_SADB_LOCKED
);
8165 lck_mtx_unlock(sadb_mutex
);
8171 return key_senderror(so
, m
, error
);
8178 * SADB_X_PROMISC processing
8180 * m will always be freed.
8186 const struct sadb_msghdr
*mhp
)
8191 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
8192 panic("key_promisc: NULL pointer is passed.\n");
8194 olen
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
8196 if (olen
< sizeof(struct sadb_msg
)) {
8198 return key_senderror(so
, m
, EINVAL
);
8203 } else if (olen
== sizeof(struct sadb_msg
)) {
8204 /* enable/disable promisc mode */
8208 if ((kp
= (struct keycb
*)sotorawcb(so
)) == NULL
)
8209 return key_senderror(so
, m
, EINVAL
);
8210 mhp
->msg
->sadb_msg_errno
= 0;
8211 switch (mhp
->msg
->sadb_msg_satype
) {
8214 kp
->kp_promisc
= mhp
->msg
->sadb_msg_satype
;
8217 socket_unlock(so
, 1);
8218 return key_senderror(so
, m
, EINVAL
);
8220 socket_unlock(so
, 1);
8222 /* send the original message back to everyone */
8223 mhp
->msg
->sadb_msg_errno
= 0;
8224 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
8226 /* send packet as is */
8228 m_adj(m
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)));
8230 /* TODO: if sadb_msg_seq is specified, send to specific pid */
8231 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
8235 static int (*key_typesw
[])(struct socket
*, struct mbuf
*,
8236 const struct sadb_msghdr
*) = {
8237 NULL
, /* SADB_RESERVED */
8238 key_getspi
, /* SADB_GETSPI */
8239 key_update
, /* SADB_UPDATE */
8240 key_add
, /* SADB_ADD */
8241 key_delete
, /* SADB_DELETE */
8242 key_get
, /* SADB_GET */
8243 key_acquire2
, /* SADB_ACQUIRE */
8244 key_register
, /* SADB_REGISTER */
8245 NULL
, /* SADB_EXPIRE */
8246 key_flush
, /* SADB_FLUSH */
8247 key_dump
, /* SADB_DUMP */
8248 key_promisc
, /* SADB_X_PROMISC */
8249 NULL
, /* SADB_X_PCHANGE */
8250 key_spdadd
, /* SADB_X_SPDUPDATE */
8251 key_spdadd
, /* SADB_X_SPDADD */
8252 key_spddelete
, /* SADB_X_SPDDELETE */
8253 key_spdget
, /* SADB_X_SPDGET */
8254 NULL
, /* SADB_X_SPDACQUIRE */
8255 key_spddump
, /* SADB_X_SPDDUMP */
8256 key_spdflush
, /* SADB_X_SPDFLUSH */
8257 key_spdadd
, /* SADB_X_SPDSETIDX */
8258 NULL
, /* SADB_X_SPDEXPIRE */
8259 key_spddelete2
, /* SADB_X_SPDDELETE2 */
8260 key_getsastat
, /* SADB_GETSASTAT */
8264 * parse sadb_msg buffer to process PFKEYv2,
8265 * and create a data to response if needed.
8266 * I think to be dealed with mbuf directly.
8268 * msgp : pointer to pointer to a received buffer pulluped.
8269 * This is rewrited to response.
8270 * so : pointer to socket.
8272 * length for buffer to send to user process.
8279 struct sadb_msg
*msg
;
8280 struct sadb_msghdr mh
;
8285 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
8288 if (m
== NULL
|| so
== NULL
)
8289 panic("key_parse: NULL pointer is passed.\n");
8291 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
8292 KEYDEBUG(KEYDEBUG_KEY_DUMP
,
8293 ipseclog((LOG_DEBUG
, "key_parse: passed sadb_msg\n"));
8297 if (m
->m_len
< sizeof(struct sadb_msg
)) {
8298 m
= m_pullup(m
, sizeof(struct sadb_msg
));
8302 msg
= mtod(m
, struct sadb_msg
*);
8303 orglen
= PFKEY_UNUNIT64(msg
->sadb_msg_len
);
8304 target
= KEY_SENDUP_ONE
;
8306 if ((m
->m_flags
& M_PKTHDR
) == 0 ||
8307 m
->m_pkthdr
.len
!= m
->m_pkthdr
.len
) {
8308 ipseclog((LOG_DEBUG
, "key_parse: invalid message length.\n"));
8309 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
8314 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
8315 ipseclog((LOG_DEBUG
,
8316 "key_parse: PF_KEY version %u is mismatched.\n",
8317 msg
->sadb_msg_version
));
8318 PFKEY_STAT_INCREMENT(pfkeystat
.out_invver
);
8323 if (msg
->sadb_msg_type
> SADB_MAX
) {
8324 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
8325 msg
->sadb_msg_type
));
8326 PFKEY_STAT_INCREMENT(pfkeystat
.out_invmsgtype
);
8331 /* for old-fashioned code - should be nuked */
8332 if (m
->m_pkthdr
.len
> MCLBYTES
) {
8339 MGETHDR(n
, M_WAITOK
, MT_DATA
);
8340 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
8341 MCLGET(n
, M_WAITOK
);
8342 if ((n
->m_flags
& M_EXT
) == 0) {
8351 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
8352 n
->m_pkthdr
.len
= n
->m_len
= m
->m_pkthdr
.len
;
8358 /* align the mbuf chain so that extensions are in contiguous region. */
8359 error
= key_align(m
, &mh
);
8363 if (m
->m_next
) { /*XXX*/
8371 switch (msg
->sadb_msg_satype
) {
8372 case SADB_SATYPE_UNSPEC
:
8373 switch (msg
->sadb_msg_type
) {
8381 ipseclog((LOG_DEBUG
, "key_parse: must specify satype "
8382 "when msg type=%u.\n", msg
->sadb_msg_type
));
8383 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
8388 case SADB_SATYPE_AH
:
8389 case SADB_SATYPE_ESP
:
8390 case SADB_X_SATYPE_IPCOMP
:
8391 switch (msg
->sadb_msg_type
) {
8393 case SADB_X_SPDDELETE
:
8395 case SADB_X_SPDDUMP
:
8396 case SADB_X_SPDFLUSH
:
8397 case SADB_X_SPDSETIDX
:
8398 case SADB_X_SPDUPDATE
:
8399 case SADB_X_SPDDELETE2
:
8400 ipseclog((LOG_DEBUG
, "key_parse: illegal satype=%u\n",
8401 msg
->sadb_msg_type
));
8402 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
8407 case SADB_SATYPE_RSVP
:
8408 case SADB_SATYPE_OSPFV2
:
8409 case SADB_SATYPE_RIPV2
:
8410 case SADB_SATYPE_MIP
:
8411 ipseclog((LOG_DEBUG
, "key_parse: type %u isn't supported.\n",
8412 msg
->sadb_msg_satype
));
8413 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
8416 case 1: /* XXX: What does it do? */
8417 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
8421 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
8422 msg
->sadb_msg_satype
));
8423 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
8428 /* check field of upper layer protocol and address family */
8429 if (mh
.ext
[SADB_EXT_ADDRESS_SRC
] != NULL
8430 && mh
.ext
[SADB_EXT_ADDRESS_DST
] != NULL
) {
8431 struct sadb_address
*src0
, *dst0
;
8434 src0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_SRC
]);
8435 dst0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_DST
]);
8437 /* check upper layer protocol */
8438 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
8439 ipseclog((LOG_DEBUG
, "key_parse: upper layer protocol mismatched.\n"));
8440 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
8446 if (PFKEY_ADDR_SADDR(src0
)->sa_family
!=
8447 PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
8448 ipseclog((LOG_DEBUG
, "key_parse: address family mismatched.\n"));
8449 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
8453 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
8454 PFKEY_ADDR_SADDR(dst0
)->sa_len
) {
8455 ipseclog((LOG_DEBUG
,
8456 "key_parse: address struct size mismatched.\n"));
8457 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
8462 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
8464 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
8465 sizeof(struct sockaddr_in
)) {
8466 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
8472 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
8473 sizeof(struct sockaddr_in6
)) {
8474 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
8480 ipseclog((LOG_DEBUG
,
8481 "key_parse: unsupported address family.\n"));
8482 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
8483 error
= EAFNOSUPPORT
;
8487 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
8489 plen
= sizeof(struct in_addr
) << 3;
8492 plen
= sizeof(struct in6_addr
) << 3;
8495 plen
= 0; /*fool gcc*/
8499 /* check max prefix length */
8500 if (src0
->sadb_address_prefixlen
> plen
||
8501 dst0
->sadb_address_prefixlen
> plen
) {
8502 ipseclog((LOG_DEBUG
,
8503 "key_parse: illegal prefixlen.\n"));
8504 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
8510 * prefixlen == 0 is valid because there can be a case when
8511 * all addresses are matched.
8515 if (msg
->sadb_msg_type
>= sizeof(key_typesw
)/sizeof(key_typesw
[0]) ||
8516 key_typesw
[msg
->sadb_msg_type
] == NULL
) {
8517 PFKEY_STAT_INCREMENT(pfkeystat
.out_invmsgtype
);
8522 return (*key_typesw
[msg
->sadb_msg_type
])(so
, m
, &mh
);
8525 msg
->sadb_msg_errno
= error
;
8526 return key_sendup_mbuf(so
, m
, target
);
8535 struct sadb_msg
*msg
;
8537 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
8539 if (m
->m_len
< sizeof(struct sadb_msg
))
8540 panic("invalid mbuf passed to key_senderror");
8542 msg
= mtod(m
, struct sadb_msg
*);
8543 msg
->sadb_msg_errno
= code
;
8544 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
8548 * set the pointer to each header into message buffer.
8549 * m will be freed on error.
8550 * XXX larger-than-MCLBYTES extension?
8555 struct sadb_msghdr
*mhp
)
8558 struct sadb_ext
*ext
;
8564 if (m
== NULL
|| mhp
== NULL
)
8565 panic("key_align: NULL pointer is passed.\n");
8566 if (m
->m_len
< sizeof(struct sadb_msg
))
8567 panic("invalid mbuf passed to key_align");
8570 bzero(mhp
, sizeof(*mhp
));
8572 mhp
->msg
= mtod(m
, struct sadb_msg
*);
8573 mhp
->ext
[0] = (struct sadb_ext
*)mhp
->msg
; /*XXX backward compat */
8575 end
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
8576 extlen
= end
; /*just in case extlen is not updated*/
8577 for (off
= sizeof(struct sadb_msg
); off
< end
; off
+= extlen
) {
8578 n
= m_pulldown(m
, off
, sizeof(struct sadb_ext
), &toff
);
8580 /* m is already freed */
8583 ext
= (struct sadb_ext
*)(void *)(mtod(n
, caddr_t
) + toff
);
8586 switch (ext
->sadb_ext_type
) {
8588 case SADB_EXT_ADDRESS_SRC
:
8589 case SADB_EXT_ADDRESS_DST
:
8590 case SADB_EXT_ADDRESS_PROXY
:
8591 case SADB_EXT_LIFETIME_CURRENT
:
8592 case SADB_EXT_LIFETIME_HARD
:
8593 case SADB_EXT_LIFETIME_SOFT
:
8594 case SADB_EXT_KEY_AUTH
:
8595 case SADB_EXT_KEY_ENCRYPT
:
8596 case SADB_EXT_IDENTITY_SRC
:
8597 case SADB_EXT_IDENTITY_DST
:
8598 case SADB_EXT_SENSITIVITY
:
8599 case SADB_EXT_PROPOSAL
:
8600 case SADB_EXT_SUPPORTED_AUTH
:
8601 case SADB_EXT_SUPPORTED_ENCRYPT
:
8602 case SADB_EXT_SPIRANGE
:
8603 case SADB_X_EXT_POLICY
:
8604 case SADB_X_EXT_SA2
:
8605 case SADB_EXT_SESSION_ID
:
8606 case SADB_EXT_SASTAT
:
8607 /* duplicate check */
8609 * XXX Are there duplication payloads of either
8610 * KEY_AUTH or KEY_ENCRYPT ?
8612 if (mhp
->ext
[ext
->sadb_ext_type
] != NULL
) {
8613 ipseclog((LOG_DEBUG
,
8614 "key_align: duplicate ext_type %u "
8615 "is passed.\n", ext
->sadb_ext_type
));
8617 PFKEY_STAT_INCREMENT(pfkeystat
.out_dupext
);
8622 ipseclog((LOG_DEBUG
,
8623 "key_align: invalid ext_type %u is passed.\n",
8624 ext
->sadb_ext_type
));
8626 PFKEY_STAT_INCREMENT(pfkeystat
.out_invexttype
);
8630 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
8632 if (key_validate_ext(ext
, extlen
)) {
8634 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
8638 n
= m_pulldown(m
, off
, extlen
, &toff
);
8640 /* m is already freed */
8643 ext
= (struct sadb_ext
*)(void *)(mtod(n
, caddr_t
) + toff
);
8645 mhp
->ext
[ext
->sadb_ext_type
] = ext
;
8646 mhp
->extoff
[ext
->sadb_ext_type
] = off
;
8647 mhp
->extlen
[ext
->sadb_ext_type
] = extlen
;
8652 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
8661 const struct sadb_ext
*ext
,
8664 struct sockaddr
*sa
;
8665 enum { NONE
, ADDR
} checktype
= NONE
;
8667 const int sal
= offsetof(struct sockaddr
, sa_len
) + sizeof(sa
->sa_len
);
8669 if (len
!= PFKEY_UNUNIT64(ext
->sadb_ext_len
))
8672 /* if it does not match minimum/maximum length, bail */
8673 if (ext
->sadb_ext_type
>= sizeof(minsize
) / sizeof(minsize
[0]) ||
8674 ext
->sadb_ext_type
>= sizeof(maxsize
) / sizeof(maxsize
[0]))
8676 if (!minsize
[ext
->sadb_ext_type
] || len
< minsize
[ext
->sadb_ext_type
])
8678 if (maxsize
[ext
->sadb_ext_type
] && len
> maxsize
[ext
->sadb_ext_type
])
8681 /* more checks based on sadb_ext_type XXX need more */
8682 switch (ext
->sadb_ext_type
) {
8683 case SADB_EXT_ADDRESS_SRC
:
8684 case SADB_EXT_ADDRESS_DST
:
8685 case SADB_EXT_ADDRESS_PROXY
:
8686 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_address
));
8689 case SADB_EXT_IDENTITY_SRC
:
8690 case SADB_EXT_IDENTITY_DST
:
8691 if (((struct sadb_ident
*)(uintptr_t)(size_t)ext
)->
8692 sadb_ident_type
== SADB_X_IDENTTYPE_ADDR
) {
8693 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_ident
));
8703 switch (checktype
) {
8707 sa
= (struct sockaddr
*)((caddr_t
)(uintptr_t)ext
+ baselen
);
8709 if (len
< baselen
+ sal
)
8711 if (baselen
+ PFKEY_ALIGN8(sa
->sa_len
) != len
)
8720 key_domain_init(void)
8726 * XXX: maybe This function is called after INBOUND IPsec processing.
8728 * Special check for tunnel-mode packets.
8729 * We must make some checks for consistency between inner and outer IP header.
8731 * xxx more checks to be provided
8734 key_checktunnelsanity(
8735 struct secasvar
*sav
,
8736 __unused u_int family
,
8737 __unused caddr_t src
,
8738 __unused caddr_t dst
)
8742 if (sav
->sah
== NULL
)
8743 panic("sav->sah == NULL at key_checktunnelsanity");
8745 /* XXX: check inner IP header */
8750 /* record data transfer on SA, and update timestamps */
8753 struct secasvar
*sav
,
8759 panic("key_sa_recordxfer called with sav == NULL");
8761 panic("key_sa_recordxfer called with m == NULL");
8765 lck_mtx_lock(sadb_mutex
);
8767 * XXX Currently, there is a difference of bytes size
8768 * between inbound and outbound processing.
8770 sav
->lft_c
->sadb_lifetime_bytes
+= m
->m_pkthdr
.len
;
8771 /* to check bytes lifetime is done in key_timehandler(). */
8774 * We use the number of packets as the unit of
8775 * sadb_lifetime_allocations. We increment the variable
8776 * whenever {esp,ah}_{in,out}put is called.
8778 sav
->lft_c
->sadb_lifetime_allocations
++;
8779 /* XXX check for expires? */
8782 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
8783 * in seconds. HARD and SOFT lifetime are measured by the time
8784 * difference (again in seconds) from sadb_lifetime_usetime.
8788 * -----+-----+--------+---> t
8789 * <--------------> HARD
8795 sav
->lft_c
->sadb_lifetime_usetime
= tv
.tv_sec
;
8796 /* XXX check for expires? */
8798 lck_mtx_unlock(sadb_mutex
);
8806 struct sockaddr
*dst
)
8808 struct secashead
*sah
;
8811 lck_mtx_lock(sadb_mutex
);
8812 LIST_FOREACH(sah
, &sahtree
, chain
) {
8813 ro
= &sah
->sa_route
;
8814 if (ro
->ro_rt
&& dst
->sa_len
== ro
->ro_dst
.sa_len
8815 && bcmp(dst
, &ro
->ro_dst
, dst
->sa_len
) == 0) {
8817 ro
->ro_rt
= (struct rtentry
*)NULL
;
8820 lck_mtx_unlock(sadb_mutex
);
8827 struct secasvar
*sav
,
8832 panic("key_sa_chgstate called with sav == NULL");
8834 if (sav
->state
== state
)
8837 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
8839 if (__LIST_CHAINED(sav
))
8840 LIST_REMOVE(sav
, chain
);
8843 LIST_INSERT_HEAD(&sav
->sah
->savtree
[state
], sav
, chain
);
8849 struct secasvar
*sav
)
8851 lck_mtx_lock(sadb_mutex
);
8853 panic("key_sa_stir_iv called with sav == NULL");
8854 key_randomfill(sav
->iv
, sav
->ivlen
);
8855 lck_mtx_unlock(sadb_mutex
);
8859 static struct mbuf
*
8863 struct mbuf
*m
= NULL
, *n
;
8868 MGET(n
, M_DONTWAIT
, MT_DATA
);
8869 if (n
&& len
> MLEN
)
8870 MCLGET(n
, M_DONTWAIT
);
8878 n
->m_len
= M_TRAILINGSPACE(n
);
8879 /* use the bottom of mbuf, hoping we can prepend afterwards */
8880 if (n
->m_len
> len
) {
8881 t
= (n
->m_len
- len
) & ~(sizeof(long) - 1);
8897 static struct mbuf
*
8898 key_setdumpsastats (u_int32_t dir
,
8899 struct sastat
*stats
,
8900 u_int32_t max_stats
,
8901 u_int64_t session_ids
[],
8905 struct mbuf
*result
= NULL
, *m
= NULL
;
8907 m
= key_setsadbmsg(SADB_GETSASTAT
, 0, 0, seq
, pid
, 0);
8913 m
= key_setsadbsession_id(session_ids
);
8919 m
= key_setsadbsastat(dir
,
8927 if ((result
->m_flags
& M_PKTHDR
) == 0) {
8931 if (result
->m_len
< sizeof(struct sadb_msg
)) {
8932 result
= m_pullup(result
, sizeof(struct sadb_msg
));
8933 if (result
== NULL
) {
8938 result
->m_pkthdr
.len
= 0;
8939 for (m
= result
; m
; m
= m
->m_next
) {
8940 result
->m_pkthdr
.len
+= m
->m_len
;
8943 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
8944 PFKEY_UNIT64(result
->m_pkthdr
.len
);
8956 * SADB_GETSASTAT processing
8957 * dump all stats for matching entries in SAD.
8959 * m will always be freed.
8963 key_getsastat (struct socket
*so
,
8965 const struct sadb_msghdr
*mhp
)
8967 struct sadb_session_id
*session_id
;
8968 u_int32_t bufsize
, arg_count
, res_count
;
8969 struct sadb_sastat
*sa_stats_arg
;
8970 struct sastat
*sa_stats_sav
= NULL
;
8975 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
8976 panic("%s: NULL pointer is passed.\n", __FUNCTION__
);
8978 if (mhp
->ext
[SADB_EXT_SESSION_ID
] == NULL
) {
8979 printf("%s: invalid message is passed. missing session-id.\n", __FUNCTION__
);
8980 return key_senderror(so
, m
, EINVAL
);
8982 if (mhp
->extlen
[SADB_EXT_SESSION_ID
] < sizeof(struct sadb_session_id
)) {
8983 printf("%s: invalid message is passed. short session-id.\n", __FUNCTION__
);
8984 return key_senderror(so
, m
, EINVAL
);
8986 if (mhp
->ext
[SADB_EXT_SASTAT
] == NULL
) {
8987 printf("%s: invalid message is passed. missing stat args.\n", __FUNCTION__
);
8988 return key_senderror(so
, m
, EINVAL
);
8990 if (mhp
->extlen
[SADB_EXT_SASTAT
] < sizeof(*sa_stats_arg
)) {
8991 printf("%s: invalid message is passed. short stat args.\n", __FUNCTION__
);
8992 return key_senderror(so
, m
, EINVAL
);
8995 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
8997 // exit early if there are no active SAs
8998 if (ipsec_sav_count
<= 0) {
8999 printf("%s: No active SAs.\n", __FUNCTION__
);
9003 bufsize
= (ipsec_sav_count
+ 1) * sizeof(*sa_stats_sav
);
9005 KMALLOC_WAIT(sa_stats_sav
, __typeof__(sa_stats_sav
), bufsize
);
9006 if (sa_stats_sav
== NULL
) {
9007 printf("%s: No more memory.\n", __FUNCTION__
);
9011 bzero(sa_stats_sav
, bufsize
);
9013 sa_stats_arg
= (__typeof__(sa_stats_arg
))
9014 (void *)mhp
->ext
[SADB_EXT_SASTAT
];
9015 arg_count
= sa_stats_arg
->sadb_sastat_list_len
;
9016 // exit early if there are no requested SAs
9017 if (arg_count
== 0) {
9018 printf("%s: No SAs requested.\n", __FUNCTION__
);
9024 if (key_getsastatbyspi((struct sastat
*)(sa_stats_arg
+ 1),
9028 printf("%s: Error finding SAs.\n", __FUNCTION__
);
9033 printf("%s: No SAs found.\n", __FUNCTION__
);
9038 session_id
= (__typeof__(session_id
))
9039 (void *)mhp
->ext
[SADB_EXT_SESSION_ID
];
9041 /* send this to the userland. */
9042 n
= key_setdumpsastats(sa_stats_arg
->sadb_sastat_dir
,
9045 session_id
->sadb_session_id_v
,
9046 mhp
->msg
->sadb_msg_seq
,
9047 mhp
->msg
->sadb_msg_pid
);
9049 printf("%s: No bufs to dump stats.\n", __FUNCTION__
);
9054 key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
9057 KFREE(sa_stats_sav
);
9061 return key_senderror(so
, m
, error
);
9068 key_update_natt_keepalive_timestamp (struct secasvar
*sav_sent
,
9069 struct secasvar
*sav_update
)
9071 struct secasindex saidx_swap_sent_addr
;
9073 // exit early if two SAs are identical, or if sav_update is current
9074 if (sav_sent
== sav_update
||
9075 sav_update
->natt_last_activity
== natt_now
) {
9079 // assuming that (sav_update->remote_ike_port != 0 && (esp_udp_encap_port & 0xFFFF) != 0)
9081 bzero(&saidx_swap_sent_addr
, sizeof(saidx_swap_sent_addr
));
9082 memcpy(&saidx_swap_sent_addr
.src
, &sav_sent
->sah
->saidx
.dst
, sizeof(saidx_swap_sent_addr
.src
));
9083 memcpy(&saidx_swap_sent_addr
.dst
, &sav_sent
->sah
->saidx
.src
, sizeof(saidx_swap_sent_addr
.dst
));
9084 saidx_swap_sent_addr
.proto
= sav_sent
->sah
->saidx
.proto
;
9085 saidx_swap_sent_addr
.mode
= sav_sent
->sah
->saidx
.mode
;
9086 // we ignore reqid for split-tunnel setups
9088 if (key_cmpsaidx(&sav_sent
->sah
->saidx
, &sav_update
->sah
->saidx
, CMP_MODE
| CMP_PORT
) ||
9089 key_cmpsaidx(&saidx_swap_sent_addr
, &sav_update
->sah
->saidx
, CMP_MODE
| CMP_PORT
)) {
9090 sav_update
->natt_last_activity
= natt_now
;