2 * Copyright (c) 2008-2010 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>
141 #define satosin(s) ((struct sockaddr_in *)s)
144 #define FULLMASK 0xff
146 lck_grp_t
*sadb_mutex_grp
;
147 lck_grp_attr_t
*sadb_mutex_grp_attr
;
148 lck_attr_t
*sadb_mutex_attr
;
149 lck_mtx_t
*sadb_mutex
;
151 lck_grp_t
*pfkey_stat_mutex_grp
;
152 lck_grp_attr_t
*pfkey_stat_mutex_grp_attr
;
153 lck_attr_t
*pfkey_stat_mutex_attr
;
154 lck_mtx_t
*pfkey_stat_mutex
;
157 * Note on SA reference counting:
158 * - SAs that are not in DEAD state will have (total external reference + 1)
159 * following value in reference count field. they cannot be freed and are
160 * referenced from SA header.
161 * - SAs that are in DEAD state will have (total external reference)
162 * in reference count field. they are ready to be freed. reference from
163 * SA header will be removed in key_delsav(), when the reference count
164 * field hits 0 (= no external reference other than from SA header.
167 u_int32_t key_debug_level
= 0; //### our sysctl is not dynamic
168 static u_int key_spi_trycnt
= 1000;
169 static u_int32_t key_spi_minval
= 0x100;
170 static u_int32_t key_spi_maxval
= 0x0fffffff; /* XXX */
171 static u_int32_t policy_id
= 0;
172 static u_int key_int_random
= 60; /*interval to initialize randseed,1(m)*/
173 static u_int key_larval_lifetime
= 30; /* interval to expire acquiring, 30(s)*/
174 static int key_blockacq_count
= 10; /* counter for blocking SADB_ACQUIRE.*/
175 static int key_blockacq_lifetime
= 20; /* lifetime for blocking SADB_ACQUIRE.*/
176 static int key_preferred_oldsa
= 0; /* preferred old sa rather than new sa.*/
177 __private_extern__
int natt_keepalive_interval
= 20; /* interval between natt keepalives.*/
178 static int ipsec_policy_count
= 0;
179 static int ipsec_sav_count
= 0;
181 static u_int32_t acq_seq
= 0;
182 static int key_tick_init_random
= 0;
183 __private_extern__ u_int32_t natt_now
= 0;
185 static LIST_HEAD(_sptree
, secpolicy
) sptree
[IPSEC_DIR_MAX
]; /* SPD */
186 static LIST_HEAD(_sahtree
, secashead
) sahtree
; /* SAD */
187 static LIST_HEAD(_regtree
, secreg
) regtree
[SADB_SATYPE_MAX
+ 1];
190 #define SPIHASHSIZE 128
191 #define SPIHASH(x) (((x) ^ ((x) >> 16)) % SPIHASHSIZE)
192 static LIST_HEAD(_spihash
, secasvar
) spihash
[SPIHASHSIZE
];
194 #ifndef IPSEC_NONBLOCK_ACQUIRE
195 static LIST_HEAD(_acqtree
, secacq
) acqtree
; /* acquiring list */
197 static LIST_HEAD(_spacqtree
, secspacq
) spacqtree
; /* SP acquiring list */
199 struct key_cb key_cb
;
201 /* search order for SAs */
202 static const u_int saorder_state_valid_prefer_old
[] = {
203 SADB_SASTATE_DYING
, SADB_SASTATE_MATURE
,
205 static const u_int saorder_state_valid_prefer_new
[] = {
206 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
208 static const u_int saorder_state_alive
[] = {
210 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
, SADB_SASTATE_LARVAL
212 static const u_int saorder_state_any
[] = {
213 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
214 SADB_SASTATE_LARVAL
, SADB_SASTATE_DEAD
217 static const int minsize
[] = {
218 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
219 sizeof(struct sadb_sa
), /* SADB_EXT_SA */
220 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
221 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
222 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
223 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_SRC */
224 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_DST */
225 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_PROXY */
226 sizeof(struct sadb_key
), /* SADB_EXT_KEY_AUTH */
227 sizeof(struct sadb_key
), /* SADB_EXT_KEY_ENCRYPT */
228 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_SRC */
229 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_DST */
230 sizeof(struct sadb_sens
), /* SADB_EXT_SENSITIVITY */
231 sizeof(struct sadb_prop
), /* SADB_EXT_PROPOSAL */
232 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_AUTH */
233 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_ENCRYPT */
234 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
235 0, /* SADB_X_EXT_KMPRIVATE */
236 sizeof(struct sadb_x_policy
), /* SADB_X_EXT_POLICY */
237 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
238 sizeof(struct sadb_session_id
), /* SADB_EXT_SESSION_ID */
239 sizeof(struct sadb_sastat
), /* SADB_EXT_SASTAT */
241 static const int maxsize
[] = {
242 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
243 sizeof(struct sadb_sa_2
), /* SADB_EXT_SA */
244 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
245 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
246 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
247 0, /* SADB_EXT_ADDRESS_SRC */
248 0, /* SADB_EXT_ADDRESS_DST */
249 0, /* SADB_EXT_ADDRESS_PROXY */
250 0, /* SADB_EXT_KEY_AUTH */
251 0, /* SADB_EXT_KEY_ENCRYPT */
252 0, /* SADB_EXT_IDENTITY_SRC */
253 0, /* SADB_EXT_IDENTITY_DST */
254 0, /* SADB_EXT_SENSITIVITY */
255 0, /* SADB_EXT_PROPOSAL */
256 0, /* SADB_EXT_SUPPORTED_AUTH */
257 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
258 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
259 0, /* SADB_X_EXT_KMPRIVATE */
260 0, /* SADB_X_EXT_POLICY */
261 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
262 0, /* SADB_EXT_SESSION_ID */
263 0, /* SADB_EXT_SASTAT */
266 static int ipsec_esp_keymin
= 256;
267 static int ipsec_esp_auth
= 0;
268 static int ipsec_ah_keymin
= 128;
270 SYSCTL_DECL(_net_key
);
271 /* Thread safe: no accumulated state */
272 SYSCTL_INT(_net_key
, KEYCTL_DEBUG_LEVEL
, debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
273 &key_debug_level
, 0, "");
276 /* max count of trial for the decision of spi value */
277 SYSCTL_INT(_net_key
, KEYCTL_SPI_TRY
, spi_trycnt
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
278 &key_spi_trycnt
, 0, "");
280 /* minimum spi value to allocate automatically. */
281 SYSCTL_INT(_net_key
, KEYCTL_SPI_MIN_VALUE
, spi_minval
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
282 &key_spi_minval
, 0, "");
284 /* maximun spi value to allocate automatically. */
285 SYSCTL_INT(_net_key
, KEYCTL_SPI_MAX_VALUE
, spi_maxval
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
286 &key_spi_maxval
, 0, "");
288 /* interval to initialize randseed */
289 SYSCTL_INT(_net_key
, KEYCTL_RANDOM_INT
, int_random
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
290 &key_int_random
, 0, "");
292 /* lifetime for larval SA; thread safe due to > compare */
293 SYSCTL_INT(_net_key
, KEYCTL_LARVAL_LIFETIME
, larval_lifetime
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
294 &key_larval_lifetime
, 0, "");
296 /* counter for blocking to send SADB_ACQUIRE to IKEd */
297 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_COUNT
, blockacq_count
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
298 &key_blockacq_count
, 0, "");
300 /* lifetime for blocking to send SADB_ACQUIRE to IKEd: Thread safe, > compare */
301 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_LIFETIME
, blockacq_lifetime
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
302 &key_blockacq_lifetime
, 0, "");
305 SYSCTL_INT(_net_key
, KEYCTL_ESP_AUTH
, esp_auth
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
306 &ipsec_esp_auth
, 0, "");
308 /* minimum ESP key length */
309 SYSCTL_INT(_net_key
, KEYCTL_ESP_KEYMIN
, esp_keymin
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
310 &ipsec_esp_keymin
, 0, "");
312 /* minimum AH key length */
313 SYSCTL_INT(_net_key
, KEYCTL_AH_KEYMIN
, ah_keymin
, CTLFLAG_RW
| CTLFLAG_LOCKED
, \
314 &ipsec_ah_keymin
, 0, "");
316 /* perfered old SA rather than new SA */
317 SYSCTL_INT(_net_key
, KEYCTL_PREFERED_OLDSA
, prefered_oldsa
, CTLFLAG_RW
| CTLFLAG_LOCKED
,\
318 &key_preferred_oldsa
, 0, "");
320 /* time between NATT keepalives in seconds, 0 disabled */
321 SYSCTL_INT(_net_key
, KEYCTL_NATT_KEEPALIVE_INTERVAL
, natt_keepalive_interval
, CTLFLAG_RW
| CTLFLAG_LOCKED
,\
322 &natt_keepalive_interval
, 0, "");
324 /* PF_KEY statistics */
325 SYSCTL_STRUCT(_net_key
, KEYCTL_PFKEYSTAT
, pfkeystat
, CTLFLAG_RD
| CTLFLAG_LOCKED
,\
326 &pfkeystat
, pfkeystat
, "");
329 #define LIST_FOREACH(elm, head, field) \
330 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
332 #define __LIST_CHAINED(elm) \
333 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
334 #define LIST_INSERT_TAIL(head, elm, type, field) \
336 struct type *curelm = LIST_FIRST(head); \
337 if (curelm == NULL) {\
338 LIST_INSERT_HEAD(head, elm, field); \
340 while (LIST_NEXT(curelm, field)) \
341 curelm = LIST_NEXT(curelm, field);\
342 LIST_INSERT_AFTER(curelm, elm, field);\
346 #define KEY_CHKSASTATE(head, sav, name) \
348 if ((head) != (sav)) { \
349 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
350 (name), (head), (sav))); \
355 #define KEY_CHKSPDIR(head, sp, name) \
357 if ((head) != (sp)) { \
358 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
359 "anyway continue.\n", \
360 (name), (head), (sp))); \
365 #define KMALLOC_WAIT(p, t, n) \
366 ((p) = (t) _MALLOC((u_int32_t)(n), M_SECA, M_WAITOK))
367 #define KMALLOC_NOWAIT(p, t, n) \
368 ((p) = (t) _MALLOC((u_int32_t)(n), M_SECA, M_NOWAIT))
370 _FREE((caddr_t)(p), M_SECA);
372 #define KMALLOC_WAIT(p, t, n) \
374 ((p) = (t)_MALLOC((u_int32_t)(n), M_SECA, M_WAITOK)); \
375 printf("%s %d: %p <- KMALLOC_WAIT(%s, %d)\n", \
376 __FILE__, __LINE__, (p), #t, n); \
378 #define KMALLOC_NOWAIT(p, t, n) \
380 ((p) = (t)_MALLOC((u_int32_t)(n), M_SECA, M_NOWAIT)); \
381 printf("%s %d: %p <- KMALLOC_NOWAIT(%s, %d)\n", \
382 __FILE__, __LINE__, (p), #t, n); \
387 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
388 _FREE((caddr_t)(p), M_SECA); \
393 * set parameters into secpolicyindex buffer.
394 * Must allocate secpolicyindex buffer passed to this function.
396 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
398 bzero((idx), sizeof(struct secpolicyindex)); \
399 (idx)->dir = (_dir); \
400 (idx)->prefs = (ps); \
401 (idx)->prefd = (pd); \
402 (idx)->ul_proto = (ulp); \
403 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
404 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
408 * set parameters into secasindex buffer.
409 * Must allocate secasindex buffer before calling this function.
411 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
413 bzero((idx), sizeof(struct secasindex)); \
414 (idx)->proto = (p); \
416 (idx)->reqid = (r); \
417 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
418 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
423 u_int32_t getspi_count
; /* the avarage of count to try to get new SPI */
427 struct sadb_msg
*msg
;
428 struct sadb_ext
*ext
[SADB_EXT_MAX
+ 1];
429 int extoff
[SADB_EXT_MAX
+ 1];
430 int extlen
[SADB_EXT_MAX
+ 1];
433 static struct secasvar
*key_do_allocsa_policy(struct secashead
*, u_int
, u_int16_t
);
434 static int key_do_get_translated_port(struct secashead
*, struct secasvar
*, u_int
);
435 static void key_delsp(struct secpolicy
*);
436 static struct secpolicy
*key_getsp(struct secpolicyindex
*);
437 static struct secpolicy
*key_getspbyid(u_int32_t
);
438 static u_int32_t
key_newreqid(void);
439 static struct mbuf
*key_gather_mbuf(struct mbuf
*,
440 const struct sadb_msghdr
*, int, int, int *);
441 static int key_spdadd(struct socket
*, struct mbuf
*,
442 const struct sadb_msghdr
*);
443 static u_int32_t
key_getnewspid(void);
444 static int key_spddelete(struct socket
*, struct mbuf
*,
445 const struct sadb_msghdr
*);
446 static int key_spddelete2(struct socket
*, struct mbuf
*,
447 const struct sadb_msghdr
*);
448 static int key_spdget(struct socket
*, struct mbuf
*,
449 const struct sadb_msghdr
*);
450 static int key_spdflush(struct socket
*, struct mbuf
*,
451 const struct sadb_msghdr
*);
452 static int key_spddump(struct socket
*, struct mbuf
*,
453 const struct sadb_msghdr
*);
454 static struct mbuf
*key_setdumpsp(struct secpolicy
*,
455 u_int8_t
, u_int32_t
, u_int32_t
);
456 static u_int
key_getspreqmsglen(struct secpolicy
*);
457 static int key_spdexpire(struct secpolicy
*);
458 static struct secashead
*key_newsah(struct secasindex
*, u_int8_t
);
459 static void key_delsah(struct secashead
*);
460 static struct secasvar
*key_newsav(struct mbuf
*,
461 const struct sadb_msghdr
*, struct secashead
*, int *);
462 static void key_delsav(struct secasvar
*);
463 static struct secashead
*key_getsah(struct secasindex
*);
464 static struct secasvar
*key_checkspidup(struct secasindex
*, u_int32_t
);
465 static void key_setspi
__P((struct secasvar
*, u_int32_t
));
466 static struct secasvar
*key_getsavbyspi(struct secashead
*, u_int32_t
);
467 static int key_setsaval(struct secasvar
*, struct mbuf
*,
468 const struct sadb_msghdr
*);
469 static int key_mature(struct secasvar
*);
470 static struct mbuf
*key_setdumpsa(struct secasvar
*, u_int8_t
,
471 u_int8_t
, u_int32_t
, u_int32_t
);
472 static struct mbuf
*key_setsadbmsg(u_int8_t
, u_int16_t
, u_int8_t
,
473 u_int32_t
, pid_t
, u_int16_t
);
474 static struct mbuf
*key_setsadbsa(struct secasvar
*);
475 static struct mbuf
*key_setsadbaddr(u_int16_t
,
476 struct sockaddr
*, u_int8_t
, u_int16_t
);
478 static struct mbuf
*key_setsadbident(u_int16_t
, u_int16_t
, caddr_t
,
481 static struct mbuf
*key_setsadbxsa2(u_int8_t
, u_int32_t
, u_int32_t
);
482 static struct mbuf
*key_setsadbxpolicy(u_int16_t
, u_int8_t
,
484 static void *key_newbuf(const void *, u_int
);
486 static int key_ismyaddr6(struct sockaddr_in6
*);
488 static void key_update_natt_keepalive_timestamp(struct secasvar
*, struct secasvar
*);
490 /* flags for key_cmpsaidx() */
491 #define CMP_HEAD 0x1 /* protocol, addresses. */
492 #define CMP_PORT 0x2 /* additionally HEAD, reqid, mode. */
493 #define CMP_REQID 0x4 /* additionally HEAD, reqid. */
494 #define CMP_MODE 0x8 /* additionally mode. */
495 #define CMP_EXACTLY 0xF /* all elements. */
496 static int key_cmpsaidx(struct secasindex
*, struct secasindex
*, int);
498 static int key_cmpspidx_exactly(struct secpolicyindex
*,
499 struct secpolicyindex
*);
500 static int key_cmpspidx_withmask(struct secpolicyindex
*,
501 struct secpolicyindex
*);
502 static int key_sockaddrcmp(struct sockaddr
*, struct sockaddr
*, int);
503 static int key_bbcmp(caddr_t
, caddr_t
, u_int
);
504 static void key_srandom(void);
505 static u_int16_t
key_satype2proto(u_int8_t
);
506 static u_int8_t
key_proto2satype(u_int16_t
);
508 static int key_getspi(struct socket
*, struct mbuf
*,
509 const struct sadb_msghdr
*);
510 static u_int32_t
key_do_getnewspi(struct sadb_spirange
*, struct secasindex
*);
511 static int key_update(struct socket
*, struct mbuf
*,
512 const struct sadb_msghdr
*);
514 static struct secasvar
*key_getsavbyseq(struct secashead
*, u_int32_t
);
516 static int key_add(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
517 static int key_setident(struct secashead
*, struct mbuf
*,
518 const struct sadb_msghdr
*);
519 static struct mbuf
*key_getmsgbuf_x1(struct mbuf
*, const struct sadb_msghdr
*);
520 static int key_delete(struct socket
*, struct mbuf
*,
521 const struct sadb_msghdr
*);
522 static int key_get(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
524 static void key_getcomb_setlifetime(struct sadb_comb
*);
526 static struct mbuf
*key_getcomb_esp(void);
528 static struct mbuf
*key_getcomb_ah(void);
529 static struct mbuf
*key_getcomb_ipcomp(void);
530 static struct mbuf
*key_getprop(const struct secasindex
*);
532 static int key_acquire(struct secasindex
*, struct secpolicy
*);
533 #ifndef IPSEC_NONBLOCK_ACQUIRE
534 static struct secacq
*key_newacq(struct secasindex
*);
535 static struct secacq
*key_getacq(struct secasindex
*);
536 static struct secacq
*key_getacqbyseq(u_int32_t
);
538 static struct secspacq
*key_newspacq(struct secpolicyindex
*);
539 static struct secspacq
*key_getspacq(struct secpolicyindex
*);
540 static int key_acquire2(struct socket
*, struct mbuf
*,
541 const struct sadb_msghdr
*);
542 static int key_register(struct socket
*, struct mbuf
*,
543 const struct sadb_msghdr
*);
544 static int key_expire(struct secasvar
*);
545 static int key_flush(struct socket
*, struct mbuf
*,
546 const struct sadb_msghdr
*);
547 static int key_dump(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
548 static int key_promisc(struct socket
*, struct mbuf
*,
549 const struct sadb_msghdr
*);
550 static int key_senderror(struct socket
*, struct mbuf
*, int);
551 static int key_validate_ext(const struct sadb_ext
*, int);
552 static int key_align(struct mbuf
*, struct sadb_msghdr
*);
553 static void key_sa_chgstate(struct secasvar
*, u_int8_t
);
554 static struct mbuf
*key_alloc_mbuf(int);
555 static int key_getsastat (struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
557 extern int ipsec_bypass
;
558 extern int esp_udp_encap_port
;
559 int ipsec_send_natt_keepalive(struct secasvar
*sav
);
567 * setup locks, call raw_init(), and then init timer and associated data
576 sadb_mutex_grp_attr
= lck_grp_attr_alloc_init();
577 sadb_mutex_grp
= lck_grp_alloc_init("sadb", sadb_mutex_grp_attr
);
578 sadb_mutex_attr
= lck_attr_alloc_init();
580 if ((sadb_mutex
= lck_mtx_alloc_init(sadb_mutex_grp
, sadb_mutex_attr
)) == NULL
) {
581 printf("key_init: can't alloc sadb_mutex\n");
585 pfkey_stat_mutex_grp_attr
= lck_grp_attr_alloc_init();
586 pfkey_stat_mutex_grp
= lck_grp_alloc_init("pfkey_stat", pfkey_stat_mutex_grp_attr
);
587 pfkey_stat_mutex_attr
= lck_attr_alloc_init();
589 if ((pfkey_stat_mutex
= lck_mtx_alloc_init(pfkey_stat_mutex_grp
, pfkey_stat_mutex_attr
)) == NULL
) {
590 printf("key_init: can't alloc pfkey_stat_mutex\n");
594 for (i
= 0; i
< SPIHASHSIZE
; i
++)
595 LIST_INIT(&spihash
[i
]);
599 bzero((caddr_t
)&key_cb
, sizeof(key_cb
));
601 for (i
= 0; i
< IPSEC_DIR_MAX
; i
++) {
602 LIST_INIT(&sptree
[i
]);
604 ipsec_policy_count
= 0;
608 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
609 LIST_INIT(®tree
[i
]);
613 #ifndef IPSEC_NONBLOCK_ACQUIRE
616 LIST_INIT(&spacqtree
);
620 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
621 ip4_def_policy
.refcnt
++; /*never reclaim this*/
624 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
625 ip6_def_policy
.refcnt
++; /*never reclaim this*/
629 timeout((void *)key_timehandler
, (void *)0, hz
);
630 #endif /*IPSEC_DEBUG2*/
632 /* initialize key statistics */
633 keystat
.getspi_count
= 1;
636 printf("IPsec: Initialized Security Association Processing.\n");
641 /* %%% IPsec policy management */
643 * allocating a SP for OUTBOUND or INBOUND packet.
644 * Must call key_freesp() later.
645 * OUT: NULL: not found
646 * others: found and return the pointer.
650 struct secpolicyindex
*spidx
,
653 struct secpolicy
*sp
;
656 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
659 panic("key_allocsp: NULL pointer is passed.\n");
661 /* check direction */
663 case IPSEC_DIR_INBOUND
:
664 case IPSEC_DIR_OUTBOUND
:
667 panic("key_allocsp: Invalid direction is passed.\n");
671 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
672 printf("*** objects\n");
673 kdebug_secpolicyindex(spidx
));
675 lck_mtx_lock(sadb_mutex
);
676 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
677 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
678 printf("*** in SPD\n");
679 kdebug_secpolicyindex(&sp
->spidx
));
681 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
683 if (key_cmpspidx_withmask(&sp
->spidx
, spidx
))
686 lck_mtx_unlock(sadb_mutex
);
691 /* found a SPD entry */
693 sp
->lastused
= tv
.tv_sec
;
695 lck_mtx_unlock(sadb_mutex
);
698 KEY_CHKSPDIR(sp
->spidx
.dir
, dir
, "key_allocsp");
699 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
700 printf("DP key_allocsp cause refcnt++:%d SP:%p\n",
706 * return a policy that matches this particular inbound packet.
711 struct sockaddr
*osrc
,
712 struct sockaddr
*odst
,
713 struct sockaddr
*isrc
,
714 struct sockaddr
*idst
)
716 struct secpolicy
*sp
;
717 const int dir
= IPSEC_DIR_INBOUND
;
719 struct ipsecrequest
*r1
, *r2
, *p
;
720 struct sockaddr
*os
, *od
, *is
, *id
;
721 struct secpolicyindex spidx
;
723 if (isrc
->sa_family
!= idst
->sa_family
) {
724 ipseclog((LOG_ERR
, "protocol family mismatched %d != %d\n.",
725 isrc
->sa_family
, idst
->sa_family
));
729 lck_mtx_lock(sadb_mutex
);
730 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
731 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
735 for (p
= sp
->req
; p
; p
= p
->next
) {
736 if (p
->saidx
.mode
!= IPSEC_MODE_TUNNEL
)
743 /* here we look at address matches only */
745 if (isrc
->sa_len
> sizeof(spidx
.src
) ||
746 idst
->sa_len
> sizeof(spidx
.dst
))
748 bcopy(isrc
, &spidx
.src
, isrc
->sa_len
);
749 bcopy(idst
, &spidx
.dst
, idst
->sa_len
);
750 if (!key_cmpspidx_withmask(&sp
->spidx
, &spidx
))
753 is
= (struct sockaddr
*)&r1
->saidx
.src
;
754 id
= (struct sockaddr
*)&r1
->saidx
.dst
;
755 if (key_sockaddrcmp(is
, isrc
, 0) ||
756 key_sockaddrcmp(id
, idst
, 0))
760 os
= (struct sockaddr
*)&r2
->saidx
.src
;
761 od
= (struct sockaddr
*)&r2
->saidx
.dst
;
762 if (key_sockaddrcmp(os
, osrc
, 0) ||
763 key_sockaddrcmp(od
, odst
, 0))
769 lck_mtx_unlock(sadb_mutex
);
774 sp
->lastused
= tv
.tv_sec
;
776 lck_mtx_unlock(sadb_mutex
);
781 * allocating an SA entry for an *OUTBOUND* packet.
782 * checking each request entries in SP, and acquire an SA if need.
783 * OUT: 0: there are valid requests.
784 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
788 struct ipsecrequest
*isr
,
789 struct secasindex
*saidx
,
790 struct secasvar
**sav
)
794 struct sockaddr_in
*sin
;
796 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
801 if (isr
== NULL
|| saidx
== NULL
)
802 panic("key_checkrequest: NULL pointer is passed.\n");
805 switch (saidx
->mode
) {
806 case IPSEC_MODE_TRANSPORT
:
807 case IPSEC_MODE_TUNNEL
:
811 panic("key_checkrequest: Invalid policy defined.\n");
814 /* get current level */
815 level
= ipsec_get_reqlevel(isr
);
819 * key_allocsa_policy should allocate the oldest SA available.
820 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
823 *sav
= key_allocsa_policy(saidx
);
825 /* When there is SA. */
831 * Remove dst port - used for special natt support - don't call
832 * key_acquire with it.
834 if (saidx
->mode
== IPSEC_MODE_TRANSPORT
) {
835 sin
= (struct sockaddr_in
*)&saidx
->dst
;
836 sin
->sin_port
= IPSEC_PORT_ANY
;
838 if ((error
= key_acquire(saidx
, isr
->sp
)) != 0) {
839 /* XXX What should I do ? */
840 ipseclog((LOG_DEBUG
, "key_checkrequest: error %d returned "
841 "from key_acquire.\n", error
));
845 return level
== IPSEC_LEVEL_REQUIRE
? ENOENT
: 0;
849 * allocating a SA for policy entry from SAD.
850 * NOTE: searching SAD of aliving state.
851 * OUT: NULL: not found.
852 * others: found and return the pointer.
854 u_int32_t sah_search_calls
= 0;
855 u_int32_t sah_search_count
= 0;
858 struct secasindex
*saidx
)
860 struct secashead
*sah
;
861 struct secasvar
*sav
;
862 u_int stateidx
, state
;
863 const u_int
*saorder_state_valid
;
865 struct sockaddr_in
*sin
;
868 lck_mtx_lock(sadb_mutex
);
870 LIST_FOREACH(sah
, &sahtree
, chain
) {
872 if (sah
->state
== SADB_SASTATE_DEAD
)
874 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_MODE
| CMP_REQID
))
877 lck_mtx_unlock(sadb_mutex
);
883 * search a valid state list for outbound packet.
884 * This search order is important.
886 if (key_preferred_oldsa
) {
887 saorder_state_valid
= saorder_state_valid_prefer_old
;
888 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
890 saorder_state_valid
= saorder_state_valid_prefer_new
;
891 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
895 sin
= (struct sockaddr_in
*)&saidx
->dst
;
896 dstport
= sin
->sin_port
;
897 if (saidx
->mode
== IPSEC_MODE_TRANSPORT
)
898 sin
->sin_port
= IPSEC_PORT_ANY
;
900 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
902 state
= saorder_state_valid
[stateidx
];
904 sav
= key_do_allocsa_policy(sah
, state
, dstport
);
906 lck_mtx_unlock(sadb_mutex
);
910 lck_mtx_unlock(sadb_mutex
);
915 * searching SAD with direction, protocol, mode and state.
916 * called by key_allocsa_policy().
919 * others : found, pointer to a SA.
921 static struct secasvar
*
922 key_do_allocsa_policy(
923 struct secashead
*sah
,
927 struct secasvar
*sav
, *nextsav
, *candidate
, *natt_candidate
, *no_natt_candidate
, *d
;
929 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
933 natt_candidate
= NULL
;
934 no_natt_candidate
= NULL
;
936 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
940 nextsav
= LIST_NEXT(sav
, chain
);
943 KEY_CHKSASTATE(sav
->state
, state
, "key_do_allocsa_policy");
945 if (sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&& dstport
&&
946 ((sav
->flags
& SADB_X_EXT_NATT
) != 0) &&
947 ntohs(dstport
) != sav
->remote_ike_port
)
950 if (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
&&
951 ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0) &&
952 ntohs(dstport
) != sav
->remote_ike_port
)
953 continue; /* skip this one - not a match - or not UDP */
955 if ((sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&&
956 ((sav
->flags
& SADB_X_EXT_NATT
) != 0)) ||
957 (sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
&&
958 ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0))) {
959 if (natt_candidate
== NULL
) {
960 natt_candidate
= sav
;
963 candidate
= natt_candidate
;
965 if (no_natt_candidate
== NULL
) {
966 no_natt_candidate
= sav
;
969 candidate
= no_natt_candidate
;
972 /* Which SA is the better ? */
975 if (candidate
->lft_c
== NULL
|| sav
->lft_c
== NULL
)
976 panic("key_do_allocsa_policy: "
977 "lifetime_current is NULL.\n");
979 /* What the best method is to compare ? */
980 if (key_preferred_oldsa
) {
981 if (candidate
->lft_c
->sadb_lifetime_addtime
>
982 sav
->lft_c
->sadb_lifetime_addtime
) {
983 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
984 natt_candidate
= sav
;
986 no_natt_candidate
= sav
;
992 /* prefered new sa rather than old sa */
993 if (candidate
->lft_c
->sadb_lifetime_addtime
<
994 sav
->lft_c
->sadb_lifetime_addtime
) {
996 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
997 natt_candidate
= sav
;
999 no_natt_candidate
= sav
;
1004 * prepared to delete the SA when there is more
1005 * suitable candidate and the lifetime of the SA is not
1008 if (d
->lft_c
->sadb_lifetime_addtime
!= 0) {
1009 struct mbuf
*m
, *result
;
1011 key_sa_chgstate(d
, SADB_SASTATE_DEAD
);
1013 m
= key_setsadbmsg(SADB_DELETE
, 0,
1014 d
->sah
->saidx
.proto
, 0, 0, d
->refcnt
- 1);
1019 /* set sadb_address for saidx's. */
1020 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
1021 (struct sockaddr
*)&d
->sah
->saidx
.src
,
1022 d
->sah
->saidx
.src
.ss_len
<< 3,
1028 /* set sadb_address for saidx's. */
1029 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
1030 (struct sockaddr
*)&d
->sah
->saidx
.src
,
1031 d
->sah
->saidx
.src
.ss_len
<< 3,
1037 /* create SA extension */
1038 m
= key_setsadbsa(d
);
1043 if (result
->m_len
< sizeof(struct sadb_msg
)) {
1044 result
= m_pullup(result
,
1045 sizeof(struct sadb_msg
));
1050 result
->m_pkthdr
.len
= 0;
1051 for (m
= result
; m
; m
= m
->m_next
)
1052 result
->m_pkthdr
.len
+= m
->m_len
;
1053 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
1054 PFKEY_UNIT64(result
->m_pkthdr
.len
);
1056 if (key_sendup_mbuf(NULL
, result
,
1057 KEY_SENDUP_REGISTERED
))
1060 key_freesav(d
, KEY_SADB_LOCKED
);
1064 /* choose latest if both types present */
1065 if (natt_candidate
== NULL
)
1066 candidate
= no_natt_candidate
;
1067 else if (no_natt_candidate
== NULL
)
1068 candidate
= natt_candidate
;
1069 else if (sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
&& dstport
)
1070 candidate
= natt_candidate
;
1071 else if (natt_candidate
->lft_c
->sadb_lifetime_addtime
>
1072 no_natt_candidate
->lft_c
->sadb_lifetime_addtime
)
1073 candidate
= natt_candidate
;
1075 candidate
= no_natt_candidate
;
1078 candidate
->refcnt
++;
1079 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1080 printf("DP allocsa_policy cause "
1081 "refcnt++:%d SA:%p\n",
1082 candidate
->refcnt
, candidate
));
1088 * allocating a SA entry for a *INBOUND* packet.
1089 * Must call key_freesav() later.
1090 * OUT: positive: pointer to a sav.
1091 * NULL: not found, or error occurred.
1093 * In the comparison, source address will be ignored for RFC2401 conformance.
1094 * To quote, from section 4.1:
1095 * A security association is uniquely identified by a triple consisting
1096 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1097 * security protocol (AH or ESP) identifier.
1098 * Note that, however, we do need to keep source address in IPsec SA.
1099 * IKE specification and PF_KEY specification do assume that we
1100 * keep source address in IPsec SA. We see a tricky situation here.
1110 struct secasvar
*sav
, *match
;
1111 u_int stateidx
, state
, tmpidx
, matchidx
;
1112 struct sockaddr_in sin
;
1113 struct sockaddr_in6 sin6
;
1114 const u_int
*saorder_state_valid
;
1117 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1120 if (src
== NULL
|| dst
== NULL
)
1121 panic("key_allocsa: NULL pointer is passed.\n");
1124 * when both systems employ similar strategy to use a SA.
1125 * the search order is important even in the inbound case.
1127 if (key_preferred_oldsa
) {
1128 saorder_state_valid
= saorder_state_valid_prefer_old
;
1129 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
1131 saorder_state_valid
= saorder_state_valid_prefer_new
;
1132 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
1137 * XXX: to be checked internal IP header somewhere. Also when
1138 * IPsec tunnel packet is received. But ESP tunnel mode is
1139 * encrypted so we can't check internal IP header.
1142 * search a valid state list for inbound packet.
1143 * the search order is not important.
1146 matchidx
= arraysize
;
1147 lck_mtx_lock(sadb_mutex
);
1148 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
1149 if (sav
->spi
!= spi
)
1151 if (proto
!= sav
->sah
->saidx
.proto
)
1153 if (family
!= sav
->sah
->saidx
.src
.ss_family
||
1154 family
!= sav
->sah
->saidx
.dst
.ss_family
)
1157 for (stateidx
= 0; stateidx
< matchidx
; stateidx
++) {
1158 state
= saorder_state_valid
[stateidx
];
1159 if (sav
->state
== state
) {
1164 if (tmpidx
>= matchidx
)
1167 #if 0 /* don't check src */
1168 /* check src address */
1171 bzero(&sin
, sizeof(sin
));
1172 sin
.sin_family
= AF_INET
;
1173 sin
.sin_len
= sizeof(sin
);
1174 bcopy(src
, &sin
.sin_addr
,
1175 sizeof(sin
.sin_addr
));
1176 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
1177 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0)
1181 bzero(&sin6
, sizeof(sin6
));
1182 sin6
.sin6_family
= AF_INET6
;
1183 sin6
.sin6_len
= sizeof(sin6
);
1184 bcopy(src
, &sin6
.sin6_addr
,
1185 sizeof(sin6
.sin6_addr
));
1186 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
1187 /* kame fake scopeid */
1188 sin6
.sin6_scope_id
=
1189 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
1190 sin6
.sin6_addr
.s6_addr16
[1] = 0;
1192 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
1193 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0)
1197 ipseclog((LOG_DEBUG
, "key_allocsa: "
1198 "unknown address family=%d.\n",
1204 /* check dst address */
1207 bzero(&sin
, sizeof(sin
));
1208 sin
.sin_family
= AF_INET
;
1209 sin
.sin_len
= sizeof(sin
);
1210 bcopy(dst
, &sin
.sin_addr
,
1211 sizeof(sin
.sin_addr
));
1212 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
1213 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0)
1218 bzero(&sin6
, sizeof(sin6
));
1219 sin6
.sin6_family
= AF_INET6
;
1220 sin6
.sin6_len
= sizeof(sin6
);
1221 bcopy(dst
, &sin6
.sin6_addr
,
1222 sizeof(sin6
.sin6_addr
));
1223 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
1224 /* kame fake scopeid */
1225 sin6
.sin6_scope_id
=
1226 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
1227 sin6
.sin6_addr
.s6_addr16
[1] = 0;
1229 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
1230 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0)
1234 ipseclog((LOG_DEBUG
, "key_allocsa: "
1235 "unknown address family=%d.\n", family
));
1246 lck_mtx_unlock(sadb_mutex
);
1251 lck_mtx_unlock(sadb_mutex
);
1252 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1253 printf("DP allocsa cause refcnt++:%d SA:%p\n",
1254 match
->refcnt
, match
));
1259 key_natt_get_translated_port(
1260 struct secasvar
*outsav
)
1263 struct secasindex saidx
;
1264 struct secashead
*sah
;
1265 u_int stateidx
, state
;
1266 const u_int
*saorder_state_valid
;
1269 /* get sa for incoming */
1270 saidx
.mode
= outsav
->sah
->saidx
.mode
;
1272 saidx
.proto
= outsav
->sah
->saidx
.proto
;
1273 bcopy(&outsav
->sah
->saidx
.src
, &saidx
.dst
, sizeof(struct sockaddr_in
));
1274 bcopy(&outsav
->sah
->saidx
.dst
, &saidx
.src
, sizeof(struct sockaddr_in
));
1276 lck_mtx_lock(sadb_mutex
);
1277 LIST_FOREACH(sah
, &sahtree
, chain
) {
1278 if (sah
->state
== SADB_SASTATE_DEAD
)
1280 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_MODE
))
1283 lck_mtx_unlock(sadb_mutex
);
1288 * Found sah - now go thru list of SAs and find
1289 * matching remote ike port. If found - set
1290 * sav->natt_encapsulated_src_port and return the port.
1293 * search a valid state list for outbound packet.
1294 * This search order is important.
1296 if (key_preferred_oldsa
) {
1297 saorder_state_valid
= saorder_state_valid_prefer_old
;
1298 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
1300 saorder_state_valid
= saorder_state_valid_prefer_new
;
1301 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
1304 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
1305 state
= saorder_state_valid
[stateidx
];
1306 if (key_do_get_translated_port(sah
, outsav
, state
)) {
1307 lck_mtx_unlock(sadb_mutex
);
1308 return outsav
->natt_encapsulated_src_port
;
1311 lck_mtx_unlock(sadb_mutex
);
1316 key_do_get_translated_port(
1317 struct secashead
*sah
,
1318 struct secasvar
*outsav
,
1321 struct secasvar
*currsav
, *nextsav
, *candidate
;
1324 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1329 for (currsav
= LIST_FIRST(&sah
->savtree
[state
]);
1331 currsav
= nextsav
) {
1333 nextsav
= LIST_NEXT(currsav
, chain
);
1336 KEY_CHKSASTATE(currsav
->state
, state
, "key_do_get_translated_port");
1338 if ((currsav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) == 0 ||
1339 currsav
->remote_ike_port
!= outsav
->remote_ike_port
)
1342 if (candidate
== NULL
) {
1343 candidate
= currsav
;
1347 /* Which SA is the better ? */
1349 /* sanity check 2 */
1350 if (candidate
->lft_c
== NULL
|| currsav
->lft_c
== NULL
)
1351 panic("key_do_get_translated_port: "
1352 "lifetime_current is NULL.\n");
1354 /* What the best method is to compare ? */
1355 if (key_preferred_oldsa
) {
1356 if (candidate
->lft_c
->sadb_lifetime_addtime
>
1357 currsav
->lft_c
->sadb_lifetime_addtime
) {
1358 candidate
= currsav
;
1364 /* prefered new sa rather than old sa */
1365 if (candidate
->lft_c
->sadb_lifetime_addtime
<
1366 currsav
->lft_c
->sadb_lifetime_addtime
)
1367 candidate
= currsav
;
1371 outsav
->natt_encapsulated_src_port
= candidate
->natt_encapsulated_src_port
;
1379 * Must be called after calling key_allocsp().
1380 * For both the packet without socket and key_freeso().
1384 struct secpolicy
*sp
,
1390 panic("key_freesp: NULL pointer is passed.\n");
1393 lck_mtx_lock(sadb_mutex
);
1395 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1397 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1398 printf("DP freesp cause refcnt--:%d SP:%p\n",
1401 if (sp
->refcnt
== 0)
1404 lck_mtx_unlock(sadb_mutex
);
1409 static void key_freesp_so(struct secpolicy
**);
1412 * Must be called after calling key_allocsp().
1413 * For the packet with socket.
1422 panic("key_freeso: NULL pointer is passed.\n");
1424 lck_mtx_lock(sadb_mutex
);
1425 switch (so
->so_proto
->pr_domain
->dom_family
) {
1429 struct inpcb
*pcb
= sotoinpcb(so
);
1431 /* Does it have a PCB ? */
1432 if (pcb
== NULL
|| pcb
->inp_sp
== NULL
)
1434 key_freesp_so(&pcb
->inp_sp
->sp_in
);
1435 key_freesp_so(&pcb
->inp_sp
->sp_out
);
1443 struct inpcb
*pcb
= sotoinpcb(so
);
1445 /* Does it have a PCB ? */
1446 if (pcb
== NULL
|| pcb
->inp_sp
== NULL
)
1448 key_freesp_so(&pcb
->inp_sp
->sp_in
);
1449 key_freesp_so(&pcb
->inp_sp
->sp_out
);
1451 struct in6pcb
*pcb
= sotoin6pcb(so
);
1453 /* Does it have a PCB ? */
1454 if (pcb
== NULL
|| pcb
->in6p_sp
== NULL
)
1456 key_freesp_so(&pcb
->in6p_sp
->sp_in
);
1457 key_freesp_so(&pcb
->in6p_sp
->sp_out
);
1463 ipseclog((LOG_DEBUG
, "key_freeso: unknown address family=%d.\n",
1464 so
->so_proto
->pr_domain
->dom_family
));
1468 lck_mtx_unlock(sadb_mutex
);
1475 struct secpolicy
**sp
)
1479 if (sp
== NULL
|| *sp
== NULL
)
1480 panic("key_freesp_so: sp == NULL\n");
1482 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1484 switch ((*sp
)->policy
) {
1485 case IPSEC_POLICY_IPSEC
:
1486 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1487 printf("DP freeso calls free SP:%p\n", *sp
));
1488 key_freesp(*sp
, KEY_SADB_LOCKED
);
1491 case IPSEC_POLICY_ENTRUST
:
1492 case IPSEC_POLICY_BYPASS
:
1495 panic("key_freesp_so: Invalid policy found %d", (*sp
)->policy
);
1504 * Must be called after calling key_allocsa().
1505 * This function is called by key_freesp() to free some SA allocated
1510 struct secasvar
*sav
,
1516 panic("key_freesav: NULL pointer is passed.\n");
1519 lck_mtx_lock(sadb_mutex
);
1521 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1523 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1524 printf("DP freesav cause refcnt--:%d SA:%p SPI %u\n",
1525 sav
->refcnt
, sav
, (u_int32_t
)ntohl(sav
->spi
)));
1527 if (sav
->refcnt
== 0)
1530 lck_mtx_unlock(sadb_mutex
);
1534 /* %%% SPD management */
1536 * free security policy entry.
1540 struct secpolicy
*sp
)
1545 panic("key_delsp: NULL pointer is passed.\n");
1547 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1548 sp
->state
= IPSEC_SPSTATE_DEAD
;
1551 return; /* can't free */
1553 /* remove from SP index */
1554 if (__LIST_CHAINED(sp
)) {
1555 LIST_REMOVE(sp
, chain
);
1556 ipsec_policy_count
--;
1560 struct ipsecrequest
*isr
= sp
->req
, *nextisr
;
1562 while (isr
!= NULL
) {
1563 nextisr
= isr
->next
;
1568 keydb_delsecpolicy(sp
);
1575 * OUT: NULL : not found
1576 * others : found, pointer to a SP.
1578 static struct secpolicy
*
1580 struct secpolicyindex
*spidx
)
1582 struct secpolicy
*sp
;
1584 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1588 panic("key_getsp: NULL pointer is passed.\n");
1590 LIST_FOREACH(sp
, &sptree
[spidx
->dir
], chain
) {
1591 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1593 if (key_cmpspidx_exactly(spidx
, &sp
->spidx
)) {
1604 * OUT: NULL : not found
1605 * others : found, pointer to a SP.
1607 static struct secpolicy
*
1611 struct secpolicy
*sp
;
1613 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1615 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_INBOUND
], chain
) {
1616 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1624 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_OUTBOUND
], chain
) {
1625 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1639 struct secpolicy
*newsp
= NULL
;
1641 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1642 newsp
= keydb_newsecpolicy();
1653 * create secpolicy structure from sadb_x_policy structure.
1654 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1655 * so must be set properly later.
1659 struct sadb_x_policy
*xpl0
,
1663 struct secpolicy
*newsp
;
1665 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1669 panic("key_msg2sp: NULL pointer was passed.\n");
1670 if (len
< sizeof(*xpl0
))
1671 panic("key_msg2sp: invalid length.\n");
1672 if (len
!= PFKEY_EXTLEN(xpl0
)) {
1673 ipseclog((LOG_DEBUG
, "key_msg2sp: Invalid msg length.\n"));
1678 if ((newsp
= key_newsp()) == NULL
) {
1683 newsp
->spidx
.dir
= xpl0
->sadb_x_policy_dir
;
1684 newsp
->policy
= xpl0
->sadb_x_policy_type
;
1687 switch (xpl0
->sadb_x_policy_type
) {
1688 case IPSEC_POLICY_DISCARD
:
1689 case IPSEC_POLICY_GENERATE
:
1690 case IPSEC_POLICY_NONE
:
1691 case IPSEC_POLICY_ENTRUST
:
1692 case IPSEC_POLICY_BYPASS
:
1696 case IPSEC_POLICY_IPSEC
:
1699 struct sadb_x_ipsecrequest
*xisr
;
1700 struct ipsecrequest
**p_isr
= &newsp
->req
;
1702 /* validity check */
1703 if (PFKEY_EXTLEN(xpl0
) < sizeof(*xpl0
)) {
1704 ipseclog((LOG_DEBUG
,
1705 "key_msg2sp: Invalid msg length.\n"));
1706 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1711 tlen
= PFKEY_EXTLEN(xpl0
) - sizeof(*xpl0
);
1712 xisr
= (struct sadb_x_ipsecrequest
*)(xpl0
+ 1);
1717 if (xisr
->sadb_x_ipsecrequest_len
< sizeof(*xisr
)) {
1718 ipseclog((LOG_DEBUG
, "key_msg2sp: "
1719 "invalid ipsecrequest length.\n"));
1720 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1725 /* allocate request buffer */
1726 KMALLOC_WAIT(*p_isr
, struct ipsecrequest
*, sizeof(**p_isr
));
1727 if ((*p_isr
) == NULL
) {
1728 ipseclog((LOG_DEBUG
,
1729 "key_msg2sp: No more memory.\n"));
1730 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1734 bzero(*p_isr
, sizeof(**p_isr
));
1737 (*p_isr
)->next
= NULL
;
1739 switch (xisr
->sadb_x_ipsecrequest_proto
) {
1742 case IPPROTO_IPCOMP
:
1745 ipseclog((LOG_DEBUG
,
1746 "key_msg2sp: invalid proto type=%u\n",
1747 xisr
->sadb_x_ipsecrequest_proto
));
1748 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1749 *error
= EPROTONOSUPPORT
;
1752 (*p_isr
)->saidx
.proto
= xisr
->sadb_x_ipsecrequest_proto
;
1754 switch (xisr
->sadb_x_ipsecrequest_mode
) {
1755 case IPSEC_MODE_TRANSPORT
:
1756 case IPSEC_MODE_TUNNEL
:
1758 case IPSEC_MODE_ANY
:
1760 ipseclog((LOG_DEBUG
,
1761 "key_msg2sp: invalid mode=%u\n",
1762 xisr
->sadb_x_ipsecrequest_mode
));
1763 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1767 (*p_isr
)->saidx
.mode
= xisr
->sadb_x_ipsecrequest_mode
;
1769 switch (xisr
->sadb_x_ipsecrequest_level
) {
1770 case IPSEC_LEVEL_DEFAULT
:
1771 case IPSEC_LEVEL_USE
:
1772 case IPSEC_LEVEL_REQUIRE
:
1774 case IPSEC_LEVEL_UNIQUE
:
1775 /* validity check */
1777 * If range violation of reqid, kernel will
1778 * update it, don't refuse it.
1780 if (xisr
->sadb_x_ipsecrequest_reqid
1781 > IPSEC_MANUAL_REQID_MAX
) {
1782 ipseclog((LOG_DEBUG
,
1783 "key_msg2sp: reqid=%d range "
1784 "violation, updated by kernel.\n",
1785 xisr
->sadb_x_ipsecrequest_reqid
));
1786 xisr
->sadb_x_ipsecrequest_reqid
= 0;
1789 /* allocate new reqid id if reqid is zero. */
1790 if (xisr
->sadb_x_ipsecrequest_reqid
== 0) {
1792 if ((reqid
= key_newreqid()) == 0) {
1793 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1797 (*p_isr
)->saidx
.reqid
= reqid
;
1798 xisr
->sadb_x_ipsecrequest_reqid
= reqid
;
1800 /* set it for manual keying. */
1801 (*p_isr
)->saidx
.reqid
=
1802 xisr
->sadb_x_ipsecrequest_reqid
;
1807 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid level=%u\n",
1808 xisr
->sadb_x_ipsecrequest_level
));
1809 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1813 (*p_isr
)->level
= xisr
->sadb_x_ipsecrequest_level
;
1815 /* set IP addresses if there */
1816 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
1817 struct sockaddr
*paddr
;
1819 paddr
= (struct sockaddr
*)(xisr
+ 1);
1821 /* validity check */
1823 > sizeof((*p_isr
)->saidx
.src
)) {
1824 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1825 "address length.\n"));
1826 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1830 bcopy(paddr
, &(*p_isr
)->saidx
.src
,
1833 paddr
= (struct sockaddr
*)((caddr_t
)paddr
1836 /* validity check */
1838 > sizeof((*p_isr
)->saidx
.dst
)) {
1839 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1840 "address length.\n"));
1841 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1845 bcopy(paddr
, &(*p_isr
)->saidx
.dst
,
1849 (*p_isr
)->sp
= newsp
;
1851 /* initialization for the next. */
1852 p_isr
= &(*p_isr
)->next
;
1853 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
1855 /* validity check */
1857 ipseclog((LOG_DEBUG
, "key_msg2sp: becoming tlen < 0.\n"));
1858 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1863 xisr
= (struct sadb_x_ipsecrequest
*)((caddr_t
)xisr
1864 + xisr
->sadb_x_ipsecrequest_len
);
1869 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid policy type.\n"));
1870 key_freesp(newsp
, KEY_SADB_UNLOCKED
);
1882 lck_mtx_lock(sadb_mutex
);
1883 static u_int32_t auto_reqid
= IPSEC_MANUAL_REQID_MAX
+ 1;
1885 auto_reqid
= (auto_reqid
== ~0
1886 ? IPSEC_MANUAL_REQID_MAX
+ 1 : auto_reqid
+ 1);
1887 lck_mtx_unlock(sadb_mutex
);
1889 /* XXX should be unique check */
1895 * copy secpolicy struct to sadb_x_policy structure indicated.
1899 struct secpolicy
*sp
)
1901 struct sadb_x_policy
*xpl
;
1908 panic("key_sp2msg: NULL pointer was passed.\n");
1910 tlen
= key_getspreqmsglen(sp
);
1912 m
= key_alloc_mbuf(tlen
);
1913 if (!m
|| m
->m_next
) { /*XXX*/
1921 xpl
= mtod(m
, struct sadb_x_policy
*);
1924 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(tlen
);
1925 xpl
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1926 xpl
->sadb_x_policy_type
= sp
->policy
;
1927 xpl
->sadb_x_policy_dir
= sp
->spidx
.dir
;
1928 xpl
->sadb_x_policy_id
= sp
->id
;
1929 p
= (caddr_t
)xpl
+ sizeof(*xpl
);
1931 /* if is the policy for ipsec ? */
1932 if (sp
->policy
== IPSEC_POLICY_IPSEC
) {
1933 struct sadb_x_ipsecrequest
*xisr
;
1934 struct ipsecrequest
*isr
;
1936 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1938 xisr
= (struct sadb_x_ipsecrequest
*)p
;
1940 xisr
->sadb_x_ipsecrequest_proto
= isr
->saidx
.proto
;
1941 xisr
->sadb_x_ipsecrequest_mode
= isr
->saidx
.mode
;
1942 xisr
->sadb_x_ipsecrequest_level
= isr
->level
;
1943 xisr
->sadb_x_ipsecrequest_reqid
= isr
->saidx
.reqid
;
1946 bcopy(&isr
->saidx
.src
, p
, isr
->saidx
.src
.ss_len
);
1947 p
+= isr
->saidx
.src
.ss_len
;
1948 bcopy(&isr
->saidx
.dst
, p
, isr
->saidx
.dst
.ss_len
);
1949 p
+= isr
->saidx
.src
.ss_len
;
1951 xisr
->sadb_x_ipsecrequest_len
=
1952 PFKEY_ALIGN8(sizeof(*xisr
)
1953 + isr
->saidx
.src
.ss_len
1954 + isr
->saidx
.dst
.ss_len
);
1961 /* m will not be freed nor modified */
1962 static struct mbuf
*
1963 key_gather_mbuf(struct mbuf
*m
, const struct sadb_msghdr
*mhp
,
1964 int ndeep
, int nitem
, int *items
)
1968 struct mbuf
*result
= NULL
, *n
;
1971 if (m
== NULL
|| mhp
== NULL
)
1972 panic("null pointer passed to key_gather");
1974 for (i
= 0; i
< nitem
; i
++) {
1976 if (idx
< 0 || idx
> SADB_EXT_MAX
)
1978 /* don't attempt to pull empty extension */
1979 if (idx
== SADB_EXT_RESERVED
&& mhp
->msg
== NULL
)
1981 if (idx
!= SADB_EXT_RESERVED
&&
1982 (mhp
->ext
[idx
] == NULL
|| mhp
->extlen
[idx
] == 0))
1985 if (idx
== SADB_EXT_RESERVED
) {
1986 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
1989 panic("assumption failed");
1991 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
1996 m_copydata(m
, 0, sizeof(struct sadb_msg
),
1998 } else if (i
< ndeep
) {
1999 len
= mhp
->extlen
[idx
];
2000 n
= key_alloc_mbuf(len
);
2001 if (!n
|| n
->m_next
) { /*XXX*/
2006 m_copydata(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
2009 n
= m_copym(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
2021 if ((result
->m_flags
& M_PKTHDR
) != 0) {
2022 result
->m_pkthdr
.len
= 0;
2023 for (n
= result
; n
; n
= n
->m_next
)
2024 result
->m_pkthdr
.len
+= n
->m_len
;
2035 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
2036 * add a entry to SP database, when received
2037 * <base, address(SD), (lifetime(H),) policy>
2039 * Adding to SP database,
2041 * <base, address(SD), (lifetime(H),) policy>
2042 * to the socket which was send.
2044 * SPDADD set a unique policy entry.
2045 * SPDSETIDX like SPDADD without a part of policy requests.
2046 * SPDUPDATE replace a unique policy entry.
2048 * m will always be freed.
2054 const struct sadb_msghdr
*mhp
)
2056 struct sadb_address
*src0
, *dst0
;
2057 struct sadb_x_policy
*xpl0
, *xpl
;
2058 struct sadb_lifetime
*lft
= NULL
;
2059 struct secpolicyindex spidx
;
2060 struct secpolicy
*newsp
;
2064 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2067 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2068 panic("key_spdadd: NULL pointer is passed.\n");
2070 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
2071 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
2072 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
2073 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2074 return key_senderror(so
, m
, EINVAL
);
2076 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
2077 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
2078 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2079 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2080 return key_senderror(so
, m
, EINVAL
);
2082 if (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
) {
2083 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
]
2084 < sizeof(struct sadb_lifetime
)) {
2085 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
2086 return key_senderror(so
, m
, EINVAL
);
2088 lft
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
2091 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
2092 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
2093 xpl0
= (struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
];
2096 /* XXX boundary check against sa_len */
2097 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2100 src0
->sadb_address_prefixlen
,
2101 dst0
->sadb_address_prefixlen
,
2102 src0
->sadb_address_proto
,
2105 /* checking the direciton. */
2106 switch (xpl0
->sadb_x_policy_dir
) {
2107 case IPSEC_DIR_INBOUND
:
2108 case IPSEC_DIR_OUTBOUND
:
2111 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid SP direction.\n"));
2112 mhp
->msg
->sadb_msg_errno
= EINVAL
;
2117 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
2118 if (xpl0
->sadb_x_policy_type
== IPSEC_POLICY_ENTRUST
2119 || xpl0
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
) {
2120 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid policy type.\n"));
2121 return key_senderror(so
, m
, EINVAL
);
2124 /* policy requests are mandatory when action is ipsec. */
2125 if (mhp
->msg
->sadb_msg_type
!= SADB_X_SPDSETIDX
2126 && xpl0
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
2127 && mhp
->extlen
[SADB_X_EXT_POLICY
] <= sizeof(*xpl0
)) {
2128 ipseclog((LOG_DEBUG
, "key_spdadd: some policy requests part required.\n"));
2129 return key_senderror(so
, m
, EINVAL
);
2133 * checking there is SP already or not.
2134 * SPDUPDATE doesn't depend on whether there is a SP or not.
2135 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
2138 lck_mtx_lock(sadb_mutex
);
2139 newsp
= key_getsp(&spidx
);
2140 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
2142 newsp
->state
= IPSEC_SPSTATE_DEAD
;
2143 key_freesp(newsp
, KEY_SADB_LOCKED
);
2146 if (newsp
!= NULL
) {
2147 key_freesp(newsp
, KEY_SADB_LOCKED
);
2148 ipseclog((LOG_DEBUG
, "key_spdadd: a SP entry exists already.\n"));
2149 lck_mtx_unlock(sadb_mutex
);
2150 return key_senderror(so
, m
, EEXIST
);
2153 lck_mtx_unlock(sadb_mutex
);
2154 /* allocation new SP entry */
2155 if ((newsp
= key_msg2sp(xpl0
, PFKEY_EXTLEN(xpl0
), &error
)) == NULL
) {
2156 return key_senderror(so
, m
, error
);
2159 if ((newsp
->id
= key_getnewspid()) == 0) {
2160 keydb_delsecpolicy(newsp
);
2161 return key_senderror(so
, m
, ENOBUFS
);
2164 /* XXX boundary check against sa_len */
2165 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2168 src0
->sadb_address_prefixlen
,
2169 dst0
->sadb_address_prefixlen
,
2170 src0
->sadb_address_proto
,
2173 /* sanity check on addr pair */
2174 if (((struct sockaddr
*)(src0
+ 1))->sa_family
!=
2175 ((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
2176 keydb_delsecpolicy(newsp
);
2177 return key_senderror(so
, m
, EINVAL
);
2179 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
2180 ((struct sockaddr
*)(dst0
+ 1))->sa_len
) {
2181 keydb_delsecpolicy(newsp
);
2182 return key_senderror(so
, m
, EINVAL
);
2186 * allow IPv6 over IPv4 tunnels using ESP -
2187 * otherwise reject if inner and outer address families not equal
2189 if (newsp
->req
&& newsp
->req
->saidx
.src
.ss_family
) {
2190 struct sockaddr
*sa
;
2191 sa
= (struct sockaddr
*)(src0
+ 1);
2192 if (sa
->sa_family
!= newsp
->req
->saidx
.src
.ss_family
) {
2193 if (newsp
->req
->saidx
.mode
!= IPSEC_MODE_TUNNEL
|| newsp
->req
->saidx
.proto
!= IPPROTO_ESP
2194 || sa
->sa_family
!= AF_INET6
|| newsp
->req
->saidx
.src
.ss_family
!= AF_INET
) {
2195 keydb_delsecpolicy(newsp
);
2196 return key_senderror(so
, m
, EINVAL
);
2200 if (newsp
->req
&& newsp
->req
->saidx
.dst
.ss_family
) {
2201 struct sockaddr
*sa
;
2202 sa
= (struct sockaddr
*)(dst0
+ 1);
2203 if (sa
->sa_family
!= newsp
->req
->saidx
.dst
.ss_family
) {
2204 if (newsp
->req
->saidx
.mode
!= IPSEC_MODE_TUNNEL
|| newsp
->req
->saidx
.proto
!= IPPROTO_ESP
2205 || sa
->sa_family
!= AF_INET6
|| newsp
->req
->saidx
.dst
.ss_family
!= AF_INET
) {
2206 keydb_delsecpolicy(newsp
);
2207 return key_senderror(so
, m
, EINVAL
);
2214 newsp
->created
= tv
.tv_sec
;
2215 newsp
->lastused
= tv
.tv_sec
;
2216 newsp
->lifetime
= lft
? lft
->sadb_lifetime_addtime
: 0;
2217 newsp
->validtime
= lft
? lft
->sadb_lifetime_usetime
: 0;
2219 newsp
->refcnt
= 1; /* do not reclaim until I say I do */
2220 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
2221 lck_mtx_lock(sadb_mutex
);
2223 * policies of type generate should be at the end of the SPD
2224 * because they function as default discard policies
2226 if (newsp
->policy
== IPSEC_POLICY_GENERATE
)
2227 LIST_INSERT_TAIL(&sptree
[newsp
->spidx
.dir
], newsp
, secpolicy
, chain
);
2228 else { /* XXX until we have policy ordering in the kernel */
2229 struct secpolicy
*tmpsp
;
2231 LIST_FOREACH(tmpsp
, &sptree
[newsp
->spidx
.dir
], chain
)
2232 if (tmpsp
->policy
== IPSEC_POLICY_GENERATE
)
2235 LIST_INSERT_BEFORE(tmpsp
, newsp
, chain
);
2237 LIST_INSERT_TAIL(&sptree
[newsp
->spidx
.dir
], newsp
, secpolicy
, chain
);
2240 ipsec_policy_count
++;
2241 /* Turn off the ipsec bypass */
2242 if (ipsec_bypass
!= 0)
2245 /* delete the entry in spacqtree */
2246 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
2247 struct secspacq
*spacq
;
2248 if ((spacq
= key_getspacq(&spidx
)) != NULL
) {
2249 /* reset counter in order to deletion by timehandler. */
2251 spacq
->created
= tv
.tv_sec
;
2255 lck_mtx_unlock(sadb_mutex
);
2258 struct mbuf
*n
, *mpolicy
;
2259 struct sadb_msg
*newmsg
;
2262 /* create new sadb_msg to reply. */
2264 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2265 SADB_EXT_LIFETIME_HARD
, SADB_EXT_ADDRESS_SRC
,
2266 SADB_EXT_ADDRESS_DST
};
2267 n
= key_gather_mbuf(m
, mhp
, 2, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2269 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2270 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
2271 n
= key_gather_mbuf(m
, mhp
, 2, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2274 return key_senderror(so
, m
, ENOBUFS
);
2276 if (n
->m_len
< sizeof(*newmsg
)) {
2277 n
= m_pullup(n
, sizeof(*newmsg
));
2279 return key_senderror(so
, m
, ENOBUFS
);
2281 newmsg
= mtod(n
, struct sadb_msg
*);
2282 newmsg
->sadb_msg_errno
= 0;
2283 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2286 mpolicy
= m_pulldown(n
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)),
2287 sizeof(*xpl
), &off
);
2288 if (mpolicy
== NULL
) {
2289 /* n is already freed */
2290 return key_senderror(so
, m
, ENOBUFS
);
2292 xpl
= (struct sadb_x_policy
*)(mtod(mpolicy
, caddr_t
) + off
);
2293 if (xpl
->sadb_x_policy_exttype
!= SADB_X_EXT_POLICY
) {
2295 return key_senderror(so
, m
, EINVAL
);
2297 xpl
->sadb_x_policy_id
= newsp
->id
;
2300 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2305 * get new policy id.
2311 key_getnewspid(void)
2313 u_int32_t newid
= 0;
2314 int count
= key_spi_trycnt
; /* XXX */
2315 struct secpolicy
*sp
;
2317 /* when requesting to allocate spi ranged */
2318 lck_mtx_lock(sadb_mutex
);
2320 newid
= (policy_id
= (policy_id
== ~0 ? 1 : policy_id
+ 1));
2322 if ((sp
= key_getspbyid(newid
)) == NULL
)
2325 key_freesp(sp
, KEY_SADB_LOCKED
);
2327 lck_mtx_unlock(sadb_mutex
);
2328 if (count
== 0 || newid
== 0) {
2329 ipseclog((LOG_DEBUG
, "key_getnewspid: to allocate policy id is failed.\n"));
2337 * SADB_SPDDELETE processing
2339 * <base, address(SD), policy(*)>
2340 * from the user(?), and set SADB_SASTATE_DEAD,
2342 * <base, address(SD), policy(*)>
2344 * policy(*) including direction of policy.
2346 * m will always be freed.
2352 const struct sadb_msghdr
*mhp
)
2354 struct sadb_address
*src0
, *dst0
;
2355 struct sadb_x_policy
*xpl0
;
2356 struct secpolicyindex spidx
;
2357 struct secpolicy
*sp
;
2359 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2362 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2363 panic("key_spddelete: NULL pointer is passed.\n");
2365 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
2366 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
2367 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
2368 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
2369 return key_senderror(so
, m
, EINVAL
);
2371 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
2372 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
2373 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2374 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
2375 return key_senderror(so
, m
, EINVAL
);
2378 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
2379 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
2380 xpl0
= (struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
];
2383 /* XXX boundary check against sa_len */
2384 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2387 src0
->sadb_address_prefixlen
,
2388 dst0
->sadb_address_prefixlen
,
2389 src0
->sadb_address_proto
,
2392 /* checking the direciton. */
2393 switch (xpl0
->sadb_x_policy_dir
) {
2394 case IPSEC_DIR_INBOUND
:
2395 case IPSEC_DIR_OUTBOUND
:
2398 ipseclog((LOG_DEBUG
, "key_spddelete: Invalid SP direction.\n"));
2399 return key_senderror(so
, m
, EINVAL
);
2402 /* Is there SP in SPD ? */
2403 lck_mtx_lock(sadb_mutex
);
2404 if ((sp
= key_getsp(&spidx
)) == NULL
) {
2405 ipseclog((LOG_DEBUG
, "key_spddelete: no SP found.\n"));
2406 lck_mtx_unlock(sadb_mutex
);
2407 return key_senderror(so
, m
, EINVAL
);
2410 /* save policy id to buffer to be returned. */
2411 xpl0
->sadb_x_policy_id
= sp
->id
;
2413 sp
->state
= IPSEC_SPSTATE_DEAD
;
2414 key_freesp(sp
, KEY_SADB_LOCKED
);
2415 lck_mtx_unlock(sadb_mutex
);
2420 struct sadb_msg
*newmsg
;
2421 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2422 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
2424 /* create new sadb_msg to reply. */
2425 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2427 return key_senderror(so
, m
, ENOBUFS
);
2429 newmsg
= mtod(n
, struct sadb_msg
*);
2430 newmsg
->sadb_msg_errno
= 0;
2431 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2434 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2439 * SADB_SPDDELETE2 processing
2442 * from the user(?), and set SADB_SASTATE_DEAD,
2446 * policy(*) including direction of policy.
2448 * m will always be freed.
2454 const struct sadb_msghdr
*mhp
)
2457 struct secpolicy
*sp
;
2459 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2462 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2463 panic("key_spddelete2: NULL pointer is passed.\n");
2465 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2466 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2467 ipseclog((LOG_DEBUG
, "key_spddelete2: invalid message is passed.\n"));
2468 key_senderror(so
, m
, EINVAL
);
2472 id
= ((struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2474 /* Is there SP in SPD ? */
2475 lck_mtx_lock(sadb_mutex
);
2476 if ((sp
= key_getspbyid(id
)) == NULL
) {
2477 lck_mtx_unlock(sadb_mutex
);
2478 ipseclog((LOG_DEBUG
, "key_spddelete2: no SP found id:%u.\n", id
));
2479 return key_senderror(so
, m
, EINVAL
);
2482 sp
->state
= IPSEC_SPSTATE_DEAD
;
2483 key_freesp(sp
, KEY_SADB_LOCKED
);
2484 lck_mtx_unlock(sadb_mutex
);
2487 struct mbuf
*n
, *nn
;
2488 struct sadb_msg
*newmsg
;
2491 /* create new sadb_msg to reply. */
2492 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2495 return key_senderror(so
, m
, ENOBUFS
);
2496 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
2497 if (n
&& len
> MHLEN
) {
2498 MCLGET(n
, M_DONTWAIT
);
2499 if ((n
->m_flags
& M_EXT
) == 0) {
2505 return key_senderror(so
, m
, ENOBUFS
);
2511 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
2512 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2516 panic("length inconsistency in key_spddelete2");
2519 n
->m_next
= m_copym(m
, mhp
->extoff
[SADB_X_EXT_POLICY
],
2520 mhp
->extlen
[SADB_X_EXT_POLICY
], M_DONTWAIT
);
2523 return key_senderror(so
, m
, ENOBUFS
);
2526 n
->m_pkthdr
.len
= 0;
2527 for (nn
= n
; nn
; nn
= nn
->m_next
)
2528 n
->m_pkthdr
.len
+= nn
->m_len
;
2530 newmsg
= mtod(n
, struct sadb_msg
*);
2531 newmsg
->sadb_msg_errno
= 0;
2532 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2535 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2540 * SADB_X_GET processing
2545 * <base, address(SD), policy>
2547 * policy(*) including direction of policy.
2549 * m will always be freed.
2555 const struct sadb_msghdr
*mhp
)
2558 struct secpolicy
*sp
;
2561 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2564 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2565 panic("key_spdget: NULL pointer is passed.\n");
2567 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2568 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2569 ipseclog((LOG_DEBUG
, "key_spdget: invalid message is passed.\n"));
2570 return key_senderror(so
, m
, EINVAL
);
2573 id
= ((struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2575 /* Is there SP in SPD ? */
2576 lck_mtx_lock(sadb_mutex
);
2577 if ((sp
= key_getspbyid(id
)) == NULL
) {
2578 ipseclog((LOG_DEBUG
, "key_spdget: no SP found id:%u.\n", id
));
2579 lck_mtx_unlock(sadb_mutex
);
2580 return key_senderror(so
, m
, ENOENT
);
2582 lck_mtx_unlock(sadb_mutex
);
2583 n
= key_setdumpsp(sp
, SADB_X_SPDGET
, 0, mhp
->msg
->sadb_msg_pid
);
2586 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
2588 return key_senderror(so
, m
, ENOBUFS
);
2592 * SADB_X_SPDACQUIRE processing.
2593 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2596 * to KMD, and expect to receive
2597 * <base> with SADB_X_SPDACQUIRE if error occurred,
2600 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2601 * policy(*) is without policy requests.
2604 * others: error number
2608 struct secpolicy
*sp
)
2610 struct mbuf
*result
= NULL
, *m
;
2611 struct secspacq
*newspacq
;
2614 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2618 panic("key_spdacquire: NULL pointer is passed.\n");
2619 if (sp
->req
!= NULL
)
2620 panic("key_spdacquire: called but there is request.\n");
2621 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
2622 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
2624 /* get a entry to check whether sent message or not. */
2625 lck_mtx_lock(sadb_mutex
);
2626 if ((newspacq
= key_getspacq(&sp
->spidx
)) != NULL
) {
2627 if (key_blockacq_count
< newspacq
->count
) {
2628 /* reset counter and do send message. */
2629 newspacq
->count
= 0;
2631 /* increment counter and do nothing. */
2633 lck_mtx_unlock(sadb_mutex
);
2637 /* make new entry for blocking to send SADB_ACQUIRE. */
2638 if ((newspacq
= key_newspacq(&sp
->spidx
)) == NULL
) {
2639 lck_mtx_unlock(sadb_mutex
);
2642 /* add to acqtree */
2643 LIST_INSERT_HEAD(&spacqtree
, newspacq
, chain
);
2645 lck_mtx_unlock(sadb_mutex
);
2646 /* create new sadb_msg to reply. */
2647 m
= key_setsadbmsg(SADB_X_SPDACQUIRE
, 0, 0, 0, 0, 0);
2654 result
->m_pkthdr
.len
= 0;
2655 for (m
= result
; m
; m
= m
->m_next
)
2656 result
->m_pkthdr
.len
+= m
->m_len
;
2658 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2659 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2661 return key_sendup_mbuf(NULL
, m
, KEY_SENDUP_REGISTERED
);
2670 * SADB_SPDFLUSH processing
2673 * from the user, and free all entries in secpctree.
2677 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2679 * m will always be freed.
2685 const struct sadb_msghdr
*mhp
)
2687 struct sadb_msg
*newmsg
;
2688 struct secpolicy
*sp
;
2692 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2693 panic("key_spdflush: NULL pointer is passed.\n");
2695 if (m
->m_len
!= PFKEY_ALIGN8(sizeof(struct sadb_msg
)))
2696 return key_senderror(so
, m
, EINVAL
);
2698 lck_mtx_lock(sadb_mutex
);
2699 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2700 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2701 sp
->state
= IPSEC_SPSTATE_DEAD
;
2704 lck_mtx_unlock(sadb_mutex
);
2706 if (sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
2707 ipseclog((LOG_DEBUG
, "key_spdflush: No more memory.\n"));
2708 return key_senderror(so
, m
, ENOBUFS
);
2714 m
->m_pkthdr
.len
= m
->m_len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2715 newmsg
= mtod(m
, struct sadb_msg
*);
2716 newmsg
->sadb_msg_errno
= 0;
2717 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
2719 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
2723 * SADB_SPDDUMP processing
2726 * from the user, and dump all SP leaves
2731 * m will always be freed.
2738 const struct sadb_msghdr
*mhp
)
2740 struct secpolicy
*sp
, **spbuf
= NULL
, **sp_ptr
;
2741 int cnt
= 0, bufcount
;
2747 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2748 panic("key_spddump: NULL pointer is passed.\n");
2750 if ((bufcount
= ipsec_policy_count
) == 0) {
2754 bufcount
+= 256; /* extra */
2755 KMALLOC_WAIT(spbuf
, struct secpolicy
**, bufcount
* sizeof(struct secpolicy
*));
2756 if (spbuf
== NULL
) {
2757 ipseclog((LOG_DEBUG
, "key_spddump: No more memory.\n"));
2761 lck_mtx_lock(sadb_mutex
);
2762 /* search SPD entry, make list. */
2764 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2765 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2766 if (cnt
== bufcount
)
2767 break; /* buffer full */
2773 lck_mtx_unlock(sadb_mutex
);
2783 n
= key_setdumpsp(*sp_ptr
++, SADB_X_SPDDUMP
, cnt
,
2784 mhp
->msg
->sadb_msg_pid
);
2787 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
2790 lck_mtx_lock(sadb_mutex
);
2791 while (sp_ptr
> spbuf
)
2792 key_freesp(*(--sp_ptr
), KEY_SADB_LOCKED
);
2793 lck_mtx_unlock(sadb_mutex
);
2799 return key_senderror(so
, m
, error
);
2806 static struct mbuf
*
2808 struct secpolicy
*sp
,
2813 struct mbuf
*result
= NULL
, *m
;
2815 m
= key_setsadbmsg(type
, 0, SADB_SATYPE_UNSPEC
, seq
, pid
, sp
->refcnt
);
2820 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
2821 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
2822 sp
->spidx
.ul_proto
);
2827 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
2828 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
2829 sp
->spidx
.ul_proto
);
2839 if ((result
->m_flags
& M_PKTHDR
) == 0)
2842 if (result
->m_len
< sizeof(struct sadb_msg
)) {
2843 result
= m_pullup(result
, sizeof(struct sadb_msg
));
2848 result
->m_pkthdr
.len
= 0;
2849 for (m
= result
; m
; m
= m
->m_next
)
2850 result
->m_pkthdr
.len
+= m
->m_len
;
2852 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2853 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2863 * get PFKEY message length for security policy and request.
2867 struct secpolicy
*sp
)
2871 tlen
= sizeof(struct sadb_x_policy
);
2873 /* if is the policy for ipsec ? */
2874 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
2877 /* get length of ipsec requests */
2879 struct ipsecrequest
*isr
;
2882 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2883 len
= sizeof(struct sadb_x_ipsecrequest
)
2884 + isr
->saidx
.src
.ss_len
2885 + isr
->saidx
.dst
.ss_len
;
2887 tlen
+= PFKEY_ALIGN8(len
);
2895 * SADB_SPDEXPIRE processing
2897 * <base, address(SD), lifetime(CH), policy>
2901 * others : error number
2905 struct secpolicy
*sp
)
2907 struct mbuf
*result
= NULL
, *m
;
2910 struct sadb_lifetime
*lt
;
2912 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2916 panic("key_spdexpire: NULL pointer is passed.\n");
2918 /* set msg header */
2919 m
= key_setsadbmsg(SADB_X_SPDEXPIRE
, 0, 0, 0, 0, 0);
2926 /* create lifetime extension (current and hard) */
2927 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
2928 m
= key_alloc_mbuf(len
);
2929 if (!m
|| m
->m_next
) { /*XXX*/
2935 bzero(mtod(m
, caddr_t
), len
);
2936 lt
= mtod(m
, struct sadb_lifetime
*);
2937 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
2938 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
2939 lt
->sadb_lifetime_allocations
= 0;
2940 lt
->sadb_lifetime_bytes
= 0;
2941 lt
->sadb_lifetime_addtime
= sp
->created
;
2942 lt
->sadb_lifetime_usetime
= sp
->lastused
;
2943 lt
= (struct sadb_lifetime
*)(mtod(m
, caddr_t
) + len
/ 2);
2944 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
2945 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
2946 lt
->sadb_lifetime_allocations
= 0;
2947 lt
->sadb_lifetime_bytes
= 0;
2948 lt
->sadb_lifetime_addtime
= sp
->lifetime
;
2949 lt
->sadb_lifetime_usetime
= sp
->validtime
;
2952 /* set sadb_address for source */
2953 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
2954 (struct sockaddr
*)&sp
->spidx
.src
,
2955 sp
->spidx
.prefs
, sp
->spidx
.ul_proto
);
2962 /* set sadb_address for destination */
2963 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
2964 (struct sockaddr
*)&sp
->spidx
.dst
,
2965 sp
->spidx
.prefd
, sp
->spidx
.ul_proto
);
2980 if ((result
->m_flags
& M_PKTHDR
) == 0) {
2985 if (result
->m_len
< sizeof(struct sadb_msg
)) {
2986 result
= m_pullup(result
, sizeof(struct sadb_msg
));
2987 if (result
== NULL
) {
2993 result
->m_pkthdr
.len
= 0;
2994 for (m
= result
; m
; m
= m
->m_next
)
2995 result
->m_pkthdr
.len
+= m
->m_len
;
2997 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2998 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3000 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
3008 /* %%% SAD management */
3010 * allocating a memory for new SA head, and copy from the values of mhp.
3011 * OUT: NULL : failure due to the lack of memory.
3012 * others : pointer to new SA head.
3014 static struct secashead
*
3016 struct secasindex
*saidx
,
3019 struct secashead
*newsah
;
3023 panic("key_newsaidx: NULL pointer is passed.\n");
3025 newsah
= keydb_newsecashead();
3029 bcopy(saidx
, &newsah
->saidx
, sizeof(newsah
->saidx
));
3031 /* remove the ports */
3032 switch (saidx
->src
.ss_family
) {
3034 ((struct sockaddr_in
*)(&newsah
->saidx
.src
))->sin_port
= IPSEC_PORT_ANY
;
3037 ((struct sockaddr_in6
*)(&newsah
->saidx
.src
))->sin6_port
= IPSEC_PORT_ANY
;
3042 switch (saidx
->dst
.ss_family
) {
3044 ((struct sockaddr_in
*)(&newsah
->saidx
.dst
))->sin_port
= IPSEC_PORT_ANY
;
3047 ((struct sockaddr_in6
*)(&newsah
->saidx
.dst
))->sin6_port
= IPSEC_PORT_ANY
;
3054 /* add to saidxtree */
3055 newsah
->state
= SADB_SASTATE_MATURE
;
3056 LIST_INSERT_HEAD(&sahtree
, newsah
, chain
);
3062 * delete SA index and all SA registerd.
3066 struct secashead
*sah
)
3068 struct secasvar
*sav
, *nextsav
;
3069 u_int stateidx
, state
;
3072 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3076 panic("key_delsah: NULL pointer is passed.\n");
3078 /* searching all SA registerd in the secindex. */
3080 stateidx
< _ARRAYLEN(saorder_state_any
);
3083 state
= saorder_state_any
[stateidx
];
3084 for (sav
= (struct secasvar
*)LIST_FIRST(&sah
->savtree
[state
]);
3088 nextsav
= LIST_NEXT(sav
, chain
);
3090 if (sav
->refcnt
> 0) {
3091 /* give up to delete this sa */
3097 KEY_CHKSASTATE(state
, sav
->state
, "key_delsah");
3099 key_freesav(sav
, KEY_SADB_LOCKED
);
3101 /* remove back pointer */
3107 /* don't delete sah only if there are savs. */
3111 if (sah
->sa_route
.ro_rt
) {
3112 rtfree(sah
->sa_route
.ro_rt
);
3113 sah
->sa_route
.ro_rt
= (struct rtentry
*)NULL
;
3116 /* remove from tree of SA index */
3117 if (__LIST_CHAINED(sah
))
3118 LIST_REMOVE(sah
, chain
);
3126 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
3127 * and copy the values of mhp into new buffer.
3128 * When SAD message type is GETSPI:
3129 * to set sequence number from acq_seq++,
3130 * to set zero to SPI.
3131 * not to call key_setsava().
3133 * others : pointer to new secasvar.
3135 * does not modify mbuf. does not free mbuf on error.
3137 static struct secasvar
*
3140 const struct sadb_msghdr
*mhp
,
3141 struct secashead
*sah
,
3144 struct secasvar
*newsav
;
3145 const struct sadb_sa
*xsa
;
3147 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3150 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
|| sah
== NULL
)
3151 panic("key_newsa: NULL pointer is passed.\n");
3153 KMALLOC_NOWAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3154 if (newsav
== NULL
) {
3155 lck_mtx_unlock(sadb_mutex
);
3156 KMALLOC_WAIT(newsav
, struct secasvar
*, sizeof(struct secasvar
));
3157 lck_mtx_lock(sadb_mutex
);
3158 if (newsav
== NULL
) {
3159 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
3164 bzero((caddr_t
)newsav
, sizeof(struct secasvar
));
3166 switch (mhp
->msg
->sadb_msg_type
) {
3168 key_setspi(newsav
, 0);
3170 #if IPSEC_DOSEQCHECK
3171 /* sync sequence number */
3172 if (mhp
->msg
->sadb_msg_seq
== 0)
3174 (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
3177 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
3182 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
3184 ipseclog((LOG_DEBUG
, "key_newsa: invalid message is passed.\n"));
3188 xsa
= (const struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
3189 key_setspi(newsav
, xsa
->sadb_sa_spi
);
3190 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
3198 /* copy sav values */
3199 if (mhp
->msg
->sadb_msg_type
!= SADB_GETSPI
) {
3200 *errp
= key_setsaval(newsav
, m
, mhp
);
3202 if (newsav
->spihash
.le_prev
|| newsav
->spihash
.le_next
)
3203 LIST_REMOVE(newsav
, spihash
);
3213 newsav
->created
= tv
.tv_sec
;
3216 newsav
->pid
= mhp
->msg
->sadb_msg_pid
;
3221 newsav
->state
= SADB_SASTATE_LARVAL
;
3222 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
3230 * free() SA variable entry.
3234 struct secasvar
*sav
)
3237 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3241 panic("key_delsav: NULL pointer is passed.\n");
3243 if (sav
->refcnt
> 0)
3244 return; /* can't free */
3246 /* remove from SA header */
3247 if (__LIST_CHAINED(sav
))
3248 LIST_REMOVE(sav
, chain
);
3251 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
)
3252 LIST_REMOVE(sav
, spihash
);
3254 if (sav
->key_auth
!= NULL
) {
3255 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
3256 KFREE(sav
->key_auth
);
3257 sav
->key_auth
= NULL
;
3259 if (sav
->key_enc
!= NULL
) {
3260 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
3261 KFREE(sav
->key_enc
);
3262 sav
->key_enc
= NULL
;
3265 bzero(sav
->sched
, sav
->schedlen
);
3269 if (sav
->replay
!= NULL
) {
3270 keydb_delsecreplay(sav
->replay
);
3273 if (sav
->lft_c
!= NULL
) {
3277 if (sav
->lft_h
!= NULL
) {
3281 if (sav
->lft_s
!= NULL
) {
3285 if (sav
->iv
!= NULL
) {
3299 * others : found, pointer to a SA.
3301 static struct secashead
*
3303 struct secasindex
*saidx
)
3305 struct secashead
*sah
;
3307 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3309 LIST_FOREACH(sah
, &sahtree
, chain
) {
3310 if (sah
->state
== SADB_SASTATE_DEAD
)
3312 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_REQID
))
3320 * check not to be duplicated SPI.
3321 * NOTE: this function is too slow due to searching all SAD.
3324 * others : found, pointer to a SA.
3326 static struct secasvar
*
3328 struct secasindex
*saidx
,
3331 struct secasvar
*sav
;
3332 u_int stateidx
, state
;
3334 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3336 /* check address family */
3337 if (saidx
->src
.ss_family
!= saidx
->dst
.ss_family
) {
3338 ipseclog((LOG_DEBUG
, "key_checkspidup: address family mismatched.\n"));
3343 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
3344 if (sav
->spi
!= spi
)
3347 stateidx
< _ARRAYLEN(saorder_state_alive
);
3349 state
= saorder_state_alive
[stateidx
];
3350 if (sav
->state
== state
&&
3351 key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
))
3361 struct secasvar
*sav
,
3364 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3366 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
)
3367 LIST_REMOVE(sav
, spihash
);
3368 LIST_INSERT_HEAD(&spihash
[SPIHASH(spi
)], sav
, spihash
);
3373 * search SAD litmited alive SA, protocol, SPI.
3376 * others : found, pointer to a SA.
3378 static struct secasvar
*
3380 struct secashead
*sah
,
3383 struct secasvar
*sav
, *match
;
3384 u_int stateidx
, state
, matchidx
;
3386 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3388 matchidx
= _ARRAYLEN(saorder_state_alive
);
3389 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
3390 if (sav
->spi
!= spi
)
3392 if (sav
->sah
!= sah
)
3394 for (stateidx
= 0; stateidx
< matchidx
; stateidx
++) {
3395 state
= saorder_state_alive
[stateidx
];
3396 if (sav
->state
== state
) {
3398 matchidx
= stateidx
;
3408 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
3409 * You must update these if need.
3413 * does not modify mbuf. does not free mbuf on error.
3417 struct secasvar
*sav
,
3419 const struct sadb_msghdr
*mhp
)
3422 const struct esp_algorithm
*algo
;
3427 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3430 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
3431 panic("key_setsaval: NULL pointer is passed.\n");
3433 /* initialization */
3435 sav
->key_auth
= NULL
;
3436 sav
->key_enc
= NULL
;
3443 sav
->remote_ike_port
= 0;
3444 sav
->natt_last_activity
= natt_now
;
3445 sav
->natt_encapsulated_src_port
= 0;
3448 if (mhp
->ext
[SADB_EXT_SA
] != NULL
) {
3449 const struct sadb_sa
*sa0
;
3451 sa0
= (const struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
3452 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(*sa0
)) {
3453 ipseclog((LOG_DEBUG
, "key_setsaval: invalid message size.\n"));
3458 sav
->alg_auth
= sa0
->sadb_sa_auth
;
3459 sav
->alg_enc
= sa0
->sadb_sa_encrypt
;
3460 sav
->flags
= sa0
->sadb_sa_flags
;
3463 * Verify that a nat-traversal port was specified if
3464 * the nat-traversal flag is set.
3466 if ((sav
->flags
& SADB_X_EXT_NATT
) != 0) {
3467 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa_2
) ||
3468 ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
== 0) {
3469 ipseclog((LOG_DEBUG
, "key_setsaval: natt port not set.\n"));
3473 sav
->remote_ike_port
= ((const struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
;
3477 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
3478 * SADB_X_EXT_NATT is set and SADB_X_EXT_NATT_KEEPALIVE is not
3479 * set (we're not behind nat) - otherwise clear it.
3481 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0)
3482 if ((sav
->flags
& SADB_X_EXT_NATT
) == 0 ||
3483 (sav
->flags
& SADB_X_EXT_NATT_KEEPALIVE
) != 0)
3484 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
3487 if ((sa0
->sadb_sa_flags
& SADB_X_EXT_OLD
) == 0) {
3488 sav
->replay
= keydb_newsecreplay(sa0
->sadb_sa_replay
);
3489 if (sav
->replay
== NULL
) {
3490 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3497 /* Authentication keys */
3498 if (mhp
->ext
[SADB_EXT_KEY_AUTH
] != NULL
) {
3499 const struct sadb_key
*key0
;
3502 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_AUTH
];
3503 len
= mhp
->extlen
[SADB_EXT_KEY_AUTH
];
3506 if (len
< sizeof(*key0
)) {
3507 ipseclog((LOG_DEBUG
, "key_setsaval: invalid auth key ext len. len = %d\n", len
));
3511 switch (mhp
->msg
->sadb_msg_satype
) {
3512 case SADB_SATYPE_AH
:
3513 case SADB_SATYPE_ESP
:
3514 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
3515 sav
->alg_auth
!= SADB_X_AALG_NULL
)
3518 case SADB_X_SATYPE_IPCOMP
:
3524 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_auth values.\n"));
3528 sav
->key_auth
= (struct sadb_key
*)key_newbuf(key0
, len
);
3529 if (sav
->key_auth
== NULL
) {
3530 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3536 /* Encryption key */
3537 if (mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] != NULL
) {
3538 const struct sadb_key
*key0
;
3541 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_ENCRYPT
];
3542 len
= mhp
->extlen
[SADB_EXT_KEY_ENCRYPT
];
3545 if (len
< sizeof(*key0
)) {
3546 ipseclog((LOG_DEBUG
, "key_setsaval: invalid encryption key ext len. len = %d\n", len
));
3550 switch (mhp
->msg
->sadb_msg_satype
) {
3551 case SADB_SATYPE_ESP
:
3552 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
3553 sav
->alg_enc
!= SADB_EALG_NULL
) {
3554 ipseclog((LOG_DEBUG
, "key_setsaval: invalid ESP algorithm.\n"));
3558 sav
->key_enc
= (struct sadb_key
*)key_newbuf(key0
, len
);
3559 if (sav
->key_enc
== NULL
) {
3560 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3565 case SADB_X_SATYPE_IPCOMP
:
3566 if (len
!= PFKEY_ALIGN8(sizeof(struct sadb_key
)))
3568 sav
->key_enc
= NULL
; /*just in case*/
3570 case SADB_SATYPE_AH
:
3576 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_enc value.\n"));
3584 switch (mhp
->msg
->sadb_msg_satype
) {
3585 case SADB_SATYPE_ESP
:
3587 algo
= esp_algorithm_lookup(sav
->alg_enc
);
3588 if (algo
&& algo
->ivlen
)
3589 sav
->ivlen
= (*algo
->ivlen
)(algo
, sav
);
3590 if (sav
->ivlen
== 0)
3592 KMALLOC_NOWAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
3594 lck_mtx_unlock(sadb_mutex
);
3595 KMALLOC_WAIT(sav
->iv
, caddr_t
, sav
->ivlen
);
3596 lck_mtx_lock(sadb_mutex
);
3598 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3605 key_randomfill(sav
->iv
, sav
->ivlen
);
3608 case SADB_SATYPE_AH
:
3609 case SADB_X_SATYPE_IPCOMP
:
3612 ipseclog((LOG_DEBUG
, "key_setsaval: invalid SA type.\n"));
3619 sav
->created
= tv
.tv_sec
;
3621 /* make lifetime for CURRENT */
3622 KMALLOC_NOWAIT(sav
->lft_c
, struct sadb_lifetime
*,
3623 sizeof(struct sadb_lifetime
));
3624 if (sav
->lft_c
== NULL
) {
3625 lck_mtx_unlock(sadb_mutex
);
3626 KMALLOC_WAIT(sav
->lft_c
, struct sadb_lifetime
*,
3627 sizeof(struct sadb_lifetime
));
3628 lck_mtx_lock(sadb_mutex
);
3629 if (sav
->lft_c
== NULL
) {
3630 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3638 sav
->lft_c
->sadb_lifetime_len
=
3639 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3640 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
3641 sav
->lft_c
->sadb_lifetime_allocations
= 0;
3642 sav
->lft_c
->sadb_lifetime_bytes
= 0;
3643 sav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
3644 sav
->lft_c
->sadb_lifetime_usetime
= 0;
3646 /* lifetimes for HARD and SOFT */
3648 const struct sadb_lifetime
*lft0
;
3650 lft0
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
3652 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
] < sizeof(*lft0
)) {
3653 ipseclog((LOG_DEBUG
, "key_setsaval: invalid hard lifetime ext len.\n"));
3657 sav
->lft_h
= (struct sadb_lifetime
*)key_newbuf(lft0
,
3659 if (sav
->lft_h
== NULL
) {
3660 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3664 /* to be initialize ? */
3667 lft0
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_SOFT
];
3669 if (mhp
->extlen
[SADB_EXT_LIFETIME_SOFT
] < sizeof(*lft0
)) {
3670 ipseclog((LOG_DEBUG
, "key_setsaval: invalid soft lifetime ext len.\n"));
3674 sav
->lft_s
= (struct sadb_lifetime
*)key_newbuf(lft0
,
3676 if (sav
->lft_s
== NULL
) {
3677 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3681 /* to be initialize ? */
3688 /* initialization */
3689 if (sav
->replay
!= NULL
) {
3690 keydb_delsecreplay(sav
->replay
);
3693 if (sav
->key_auth
!= NULL
) {
3694 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
3695 KFREE(sav
->key_auth
);
3696 sav
->key_auth
= NULL
;
3698 if (sav
->key_enc
!= NULL
) {
3699 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
3700 KFREE(sav
->key_enc
);
3701 sav
->key_enc
= NULL
;
3704 bzero(sav
->sched
, sav
->schedlen
);
3708 if (sav
->iv
!= NULL
) {
3712 if (sav
->lft_c
!= NULL
) {
3716 if (sav
->lft_h
!= NULL
) {
3720 if (sav
->lft_s
!= NULL
) {
3729 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3735 struct secasvar
*sav
)
3738 int checkmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3739 int mustmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3743 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3745 /* check SPI value */
3746 switch (sav
->sah
->saidx
.proto
) {
3750 /* No reason to test if this is >= 0, because ntohl(sav->spi) is unsigned. */
3751 if (ntohl(sav
->spi
) <= 255) {
3752 ipseclog((LOG_DEBUG
,
3753 "key_mature: illegal range of SPI %u.\n",
3754 (u_int32_t
)ntohl(sav
->spi
)));
3761 switch (sav
->sah
->saidx
.proto
) {
3764 if ((sav
->flags
& SADB_X_EXT_OLD
)
3765 && (sav
->flags
& SADB_X_EXT_DERIV
)) {
3766 ipseclog((LOG_DEBUG
, "key_mature: "
3767 "invalid flag (derived) given to old-esp.\n"));
3770 if (sav
->alg_auth
== SADB_AALG_NONE
)
3778 if (sav
->flags
& SADB_X_EXT_DERIV
) {
3779 ipseclog((LOG_DEBUG
, "key_mature: "
3780 "invalid flag (derived) given to AH SA.\n"));
3783 if (sav
->alg_enc
!= SADB_EALG_NONE
) {
3784 ipseclog((LOG_DEBUG
, "key_mature: "
3785 "protocol and algorithm mismated.\n"));
3791 case IPPROTO_IPCOMP
:
3792 if (sav
->alg_auth
!= SADB_AALG_NONE
) {
3793 ipseclog((LOG_DEBUG
, "key_mature: "
3794 "protocol and algorithm mismated.\n"));
3797 if ((sav
->flags
& SADB_X_EXT_RAWCPI
) == 0
3798 && ntohl(sav
->spi
) >= 0x10000) {
3799 ipseclog((LOG_DEBUG
, "key_mature: invalid cpi for IPComp.\n"));
3806 ipseclog((LOG_DEBUG
, "key_mature: Invalid satype.\n"));
3807 return EPROTONOSUPPORT
;
3810 /* check authentication algorithm */
3811 if ((checkmask
& 2) != 0) {
3812 const struct ah_algorithm
*algo
;
3815 algo
= ah_algorithm_lookup(sav
->alg_auth
);
3817 ipseclog((LOG_DEBUG
,"key_mature: "
3818 "unknown authentication algorithm.\n"));
3822 /* algorithm-dependent check */
3824 keylen
= sav
->key_auth
->sadb_key_bits
;
3827 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
3828 ipseclog((LOG_DEBUG
,
3829 "key_mature: invalid AH key length %d "
3830 "(%d-%d allowed)\n",
3831 keylen
, algo
->keymin
, algo
->keymax
));
3836 if ((*algo
->mature
)(sav
)) {
3837 /* message generated in per-algorithm function*/
3840 mature
= SADB_SATYPE_AH
;
3843 if ((mustmask
& 2) != 0 && mature
!= SADB_SATYPE_AH
) {
3844 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for AH\n"));
3849 /* check encryption algorithm */
3850 if ((checkmask
& 1) != 0) {
3852 const struct esp_algorithm
*algo
;
3855 algo
= esp_algorithm_lookup(sav
->alg_enc
);
3857 ipseclog((LOG_DEBUG
, "key_mature: unknown encryption algorithm.\n"));
3861 /* algorithm-dependent check */
3863 keylen
= sav
->key_enc
->sadb_key_bits
;
3866 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
3867 ipseclog((LOG_DEBUG
,
3868 "key_mature: invalid ESP key length %d "
3869 "(%d-%d allowed)\n",
3870 keylen
, algo
->keymin
, algo
->keymax
));
3875 if ((*algo
->mature
)(sav
)) {
3876 /* message generated in per-algorithm function*/
3879 mature
= SADB_SATYPE_ESP
;
3882 if ((mustmask
& 1) != 0 && mature
!= SADB_SATYPE_ESP
) {
3883 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for ESP\n"));
3887 ipseclog((LOG_DEBUG
, "key_mature: ESP not supported in this configuration\n"));
3892 /* check compression algorithm */
3893 if ((checkmask
& 4) != 0) {
3894 const struct ipcomp_algorithm
*algo
;
3896 /* algorithm-dependent check */
3897 algo
= ipcomp_algorithm_lookup(sav
->alg_enc
);
3899 ipseclog((LOG_DEBUG
, "key_mature: unknown compression algorithm.\n"));
3904 key_sa_chgstate(sav
, SADB_SASTATE_MATURE
);
3910 * subroutine for SADB_GET and SADB_DUMP.
3912 static struct mbuf
*
3914 struct secasvar
*sav
,
3920 struct mbuf
*result
= NULL
, *tres
= NULL
, *m
;
3925 SADB_EXT_SA
, SADB_X_EXT_SA2
,
3926 SADB_EXT_LIFETIME_HARD
, SADB_EXT_LIFETIME_SOFT
,
3927 SADB_EXT_LIFETIME_CURRENT
, SADB_EXT_ADDRESS_SRC
,
3928 SADB_EXT_ADDRESS_DST
, SADB_EXT_ADDRESS_PROXY
, SADB_EXT_KEY_AUTH
,
3929 SADB_EXT_KEY_ENCRYPT
, SADB_EXT_IDENTITY_SRC
,
3930 SADB_EXT_IDENTITY_DST
, SADB_EXT_SENSITIVITY
,
3933 m
= key_setsadbmsg(type
, 0, satype
, seq
, pid
, sav
->refcnt
);
3938 for (i
= sizeof(dumporder
)/sizeof(dumporder
[0]) - 1; i
>= 0; i
--) {
3941 switch (dumporder
[i
]) {
3943 m
= key_setsadbsa(sav
);
3948 case SADB_X_EXT_SA2
:
3949 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
3950 sav
->replay
? sav
->replay
->count
: 0,
3951 sav
->sah
->saidx
.reqid
);
3956 case SADB_EXT_ADDRESS_SRC
:
3957 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
3958 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
3959 FULLMASK
, IPSEC_ULPROTO_ANY
);
3964 case SADB_EXT_ADDRESS_DST
:
3965 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
3966 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
3967 FULLMASK
, IPSEC_ULPROTO_ANY
);
3972 case SADB_EXT_KEY_AUTH
:
3975 l
= PFKEY_UNUNIT64(sav
->key_auth
->sadb_key_len
);
3979 case SADB_EXT_KEY_ENCRYPT
:
3982 l
= PFKEY_UNUNIT64(sav
->key_enc
->sadb_key_len
);
3986 case SADB_EXT_LIFETIME_CURRENT
:
3989 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_c
)->sadb_ext_len
);
3993 case SADB_EXT_LIFETIME_HARD
:
3996 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_h
)->sadb_ext_len
);
4000 case SADB_EXT_LIFETIME_SOFT
:
4003 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_s
)->sadb_ext_len
);
4007 case SADB_EXT_ADDRESS_PROXY
:
4008 case SADB_EXT_IDENTITY_SRC
:
4009 case SADB_EXT_IDENTITY_DST
:
4010 /* XXX: should we brought from SPD ? */
4011 case SADB_EXT_SENSITIVITY
:
4016 if ((!m
&& !p
) || (m
&& p
))
4019 M_PREPEND(tres
, l
, M_DONTWAIT
);
4022 bcopy(p
, mtod(tres
, caddr_t
), l
);
4026 m
= key_alloc_mbuf(l
);
4029 m_copyback(m
, 0, l
, p
);
4037 m_cat(result
, tres
);
4039 if (result
->m_len
< sizeof(struct sadb_msg
)) {
4040 result
= m_pullup(result
, sizeof(struct sadb_msg
));
4045 result
->m_pkthdr
.len
= 0;
4046 for (m
= result
; m
; m
= m
->m_next
)
4047 result
->m_pkthdr
.len
+= m
->m_len
;
4049 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
4050 PFKEY_UNIT64(result
->m_pkthdr
.len
);
4061 * set data into sadb_msg.
4063 static struct mbuf
*
4076 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
4079 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
4080 if (m
&& len
> MHLEN
) {
4081 MCLGET(m
, M_DONTWAIT
);
4082 if ((m
->m_flags
& M_EXT
) == 0) {
4089 m
->m_pkthdr
.len
= m
->m_len
= len
;
4092 p
= mtod(m
, struct sadb_msg
*);
4095 p
->sadb_msg_version
= PF_KEY_V2
;
4096 p
->sadb_msg_type
= type
;
4097 p
->sadb_msg_errno
= 0;
4098 p
->sadb_msg_satype
= satype
;
4099 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
4100 p
->sadb_msg_reserved
= reserved
;
4101 p
->sadb_msg_seq
= seq
;
4102 p
->sadb_msg_pid
= (u_int32_t
)pid
;
4108 * copy secasvar data into sadb_address.
4110 static struct mbuf
*
4112 struct secasvar
*sav
)
4118 len
= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
4119 m
= key_alloc_mbuf(len
);
4120 if (!m
|| m
->m_next
) { /*XXX*/
4126 p
= mtod(m
, struct sadb_sa
*);
4129 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
4130 p
->sadb_sa_exttype
= SADB_EXT_SA
;
4131 p
->sadb_sa_spi
= sav
->spi
;
4132 p
->sadb_sa_replay
= (sav
->replay
!= NULL
? sav
->replay
->wsize
: 0);
4133 p
->sadb_sa_state
= sav
->state
;
4134 p
->sadb_sa_auth
= sav
->alg_auth
;
4135 p
->sadb_sa_encrypt
= sav
->alg_enc
;
4136 p
->sadb_sa_flags
= sav
->flags
;
4142 * set data into sadb_address.
4144 static struct mbuf
*
4147 struct sockaddr
*saddr
,
4152 struct sadb_address
*p
;
4155 len
= PFKEY_ALIGN8(sizeof(struct sadb_address
)) +
4156 PFKEY_ALIGN8(saddr
->sa_len
);
4157 m
= key_alloc_mbuf(len
);
4158 if (!m
|| m
->m_next
) { /*XXX*/
4164 p
= mtod(m
, struct sadb_address
*);
4167 p
->sadb_address_len
= PFKEY_UNIT64(len
);
4168 p
->sadb_address_exttype
= exttype
;
4169 p
->sadb_address_proto
= ul_proto
;
4170 if (prefixlen
== FULLMASK
) {
4171 switch (saddr
->sa_family
) {
4173 prefixlen
= sizeof(struct in_addr
) << 3;
4176 prefixlen
= sizeof(struct in6_addr
) << 3;
4182 p
->sadb_address_prefixlen
= prefixlen
;
4183 p
->sadb_address_reserved
= 0;
4186 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_address
)),
4193 * set data into sadb_session_id
4195 static struct mbuf
*
4196 key_setsadbsession_id (u_int64_t session_ids
[])
4199 struct sadb_session_id
*p
;
4202 len
= PFKEY_ALIGN8(sizeof(*p
));
4203 m
= key_alloc_mbuf(len
);
4204 if (!m
|| m
->m_next
) { /*XXX*/
4210 p
= mtod(m
, __typeof__(p
));
4213 p
->sadb_session_id_len
= PFKEY_UNIT64(len
);
4214 p
->sadb_session_id_exttype
= SADB_EXT_SESSION_ID
;
4215 p
->sadb_session_id_v
[0] = session_ids
[0];
4216 p
->sadb_session_id_v
[1] = session_ids
[1];
4222 * copy stats data into sadb_sastat type.
4224 static struct mbuf
*
4225 key_setsadbsastat (u_int32_t dir
,
4226 struct sastat
*stats
,
4227 u_int32_t max_stats
)
4230 struct sadb_sastat
*p
;
4237 list_len
= sizeof(*stats
) * max_stats
;
4238 len
= PFKEY_ALIGN8(sizeof(*p
)) + PFKEY_ALIGN8(list_len
);
4239 m
= key_alloc_mbuf(len
);
4240 if (!m
|| m
->m_next
) { /*XXX*/
4246 p
= mtod(m
, __typeof__(p
));
4249 p
->sadb_sastat_len
= PFKEY_UNIT64(len
);
4250 p
->sadb_sastat_exttype
= SADB_EXT_SASTAT
;
4251 p
->sadb_sastat_dir
= dir
;
4252 p
->sadb_sastat_list_len
= max_stats
;
4255 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(*p
)),
4264 * set data into sadb_ident.
4266 static struct mbuf
*
4275 struct sadb_ident
*p
;
4278 len
= PFKEY_ALIGN8(sizeof(struct sadb_ident
)) + PFKEY_ALIGN8(stringlen
);
4279 m
= key_alloc_mbuf(len
);
4280 if (!m
|| m
->m_next
) { /*XXX*/
4286 p
= mtod(m
, struct sadb_ident
*);
4289 p
->sadb_ident_len
= PFKEY_UNIT64(len
);
4290 p
->sadb_ident_exttype
= exttype
;
4291 p
->sadb_ident_type
= idtype
;
4292 p
->sadb_ident_reserved
= 0;
4293 p
->sadb_ident_id
= id
;
4296 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_ident
)),
4304 * set data into sadb_x_sa2.
4306 static struct mbuf
*
4313 struct sadb_x_sa2
*p
;
4316 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_sa2
));
4317 m
= key_alloc_mbuf(len
);
4318 if (!m
|| m
->m_next
) { /*XXX*/
4324 p
= mtod(m
, struct sadb_x_sa2
*);
4327 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
4328 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
4329 p
->sadb_x_sa2_mode
= mode
;
4330 p
->sadb_x_sa2_reserved1
= 0;
4331 p
->sadb_x_sa2_reserved2
= 0;
4332 p
->sadb_x_sa2_sequence
= seq
;
4333 p
->sadb_x_sa2_reqid
= reqid
;
4339 * set data into sadb_x_policy
4341 static struct mbuf
*
4348 struct sadb_x_policy
*p
;
4351 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_policy
));
4352 m
= key_alloc_mbuf(len
);
4353 if (!m
|| m
->m_next
) { /*XXX*/
4359 p
= mtod(m
, struct sadb_x_policy
*);
4362 p
->sadb_x_policy_len
= PFKEY_UNIT64(len
);
4363 p
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
4364 p
->sadb_x_policy_type
= type
;
4365 p
->sadb_x_policy_dir
= dir
;
4366 p
->sadb_x_policy_id
= id
;
4373 * copy a buffer into the new buffer allocated.
4382 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4383 KMALLOC_NOWAIT(new, caddr_t
, len
);
4385 lck_mtx_unlock(sadb_mutex
);
4386 KMALLOC_WAIT(new, caddr_t
, len
);
4387 lck_mtx_lock(sadb_mutex
);
4389 ipseclog((LOG_DEBUG
, "key_newbuf: No more memory.\n"));
4393 bcopy(src
, new, len
);
4398 /* compare my own address
4399 * OUT: 1: true, i.e. my address.
4404 struct sockaddr
*sa
)
4407 struct sockaddr_in
*sin
;
4408 struct in_ifaddr
*ia
;
4413 panic("key_ismyaddr: NULL pointer is passed.\n");
4415 switch (sa
->sa_family
) {
4418 lck_rw_lock_shared(in_ifaddr_rwlock
);
4419 sin
= (struct sockaddr_in
*)sa
;
4420 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
4421 ia
= ia
->ia_link
.tqe_next
) {
4422 IFA_LOCK_SPIN(&ia
->ia_ifa
);
4423 if (sin
->sin_family
== ia
->ia_addr
.sin_family
&&
4424 sin
->sin_len
== ia
->ia_addr
.sin_len
&&
4425 sin
->sin_addr
.s_addr
== ia
->ia_addr
.sin_addr
.s_addr
)
4427 IFA_UNLOCK(&ia
->ia_ifa
);
4428 lck_rw_done(in_ifaddr_rwlock
);
4431 IFA_UNLOCK(&ia
->ia_ifa
);
4433 lck_rw_done(in_ifaddr_rwlock
);
4438 return key_ismyaddr6((struct sockaddr_in6
*)sa
);
4447 * compare my own address for IPv6.
4450 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
4452 #include <netinet6/in6_var.h>
4456 struct sockaddr_in6
*sin6
)
4458 struct in6_ifaddr
*ia
;
4459 struct in6_multi
*in6m
;
4461 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
4462 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
4463 IFA_LOCK(&ia
->ia_ifa
);
4464 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
4465 (struct sockaddr
*)&ia
->ia_addr
, 0) == 0) {
4466 IFA_UNLOCK(&ia
->ia_ifa
);
4467 lck_rw_done(&in6_ifaddr_rwlock
);
4470 IFA_UNLOCK(&ia
->ia_ifa
);
4474 * XXX why do we care about multlicast here while we don't care
4475 * about IPv4 multicast??
4479 in6_multihead_lock_shared();
4480 IN6_LOOKUP_MULTI(&sin6
->sin6_addr
, ia
->ia_ifp
, in6m
);
4481 in6_multihead_lock_done();
4483 lck_rw_done(&in6_ifaddr_rwlock
);
4488 lck_rw_done(&in6_ifaddr_rwlock
);
4490 /* loopback, just for safety */
4491 if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
4499 * compare two secasindex structure.
4500 * flag can specify to compare 2 saidxes.
4501 * compare two secasindex structure without both mode and reqid.
4502 * don't compare port.
4504 * saidx0: source, it can be in SAD.
4512 struct secasindex
*saidx0
,
4513 struct secasindex
*saidx1
,
4517 if (saidx0
== NULL
&& saidx1
== NULL
)
4520 if (saidx0
== NULL
|| saidx1
== NULL
)
4523 if (saidx0
->proto
!= saidx1
->proto
)
4526 if (flag
== CMP_EXACTLY
) {
4527 if (saidx0
->mode
!= saidx1
->mode
)
4529 if (saidx0
->reqid
!= saidx1
->reqid
)
4531 if (bcmp(&saidx0
->src
, &saidx1
->src
, saidx0
->src
.ss_len
) != 0 ||
4532 bcmp(&saidx0
->dst
, &saidx1
->dst
, saidx0
->dst
.ss_len
) != 0)
4536 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
4537 if (flag
& CMP_REQID
) {
4539 * If reqid of SPD is non-zero, unique SA is required.
4540 * The result must be of same reqid in this case.
4542 if (saidx1
->reqid
!= 0 && saidx0
->reqid
!= saidx1
->reqid
)
4546 if (flag
& CMP_MODE
) {
4547 if (saidx0
->mode
!= IPSEC_MODE_ANY
4548 && saidx0
->mode
!= saidx1
->mode
)
4552 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->src
,
4553 (struct sockaddr
*)&saidx1
->src
, flag
& CMP_PORT
? 1 : 0) != 0) {
4556 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->dst
,
4557 (struct sockaddr
*)&saidx1
->dst
, flag
& CMP_PORT
? 1 : 0) != 0) {
4566 * compare two secindex structure exactly.
4568 * spidx0: source, it is often in SPD.
4569 * spidx1: object, it is often from PFKEY message.
4575 key_cmpspidx_exactly(
4576 struct secpolicyindex
*spidx0
,
4577 struct secpolicyindex
*spidx1
)
4580 if (spidx0
== NULL
&& spidx1
== NULL
)
4583 if (spidx0
== NULL
|| spidx1
== NULL
)
4586 if (spidx0
->prefs
!= spidx1
->prefs
4587 || spidx0
->prefd
!= spidx1
->prefd
4588 || spidx0
->ul_proto
!= spidx1
->ul_proto
)
4591 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src
,
4592 (struct sockaddr
*)&spidx1
->src
, 1) != 0) {
4595 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst
,
4596 (struct sockaddr
*)&spidx1
->dst
, 1) != 0) {
4604 * compare two secindex structure with mask.
4606 * spidx0: source, it is often in SPD.
4607 * spidx1: object, it is often from IP header.
4613 key_cmpspidx_withmask(
4614 struct secpolicyindex
*spidx0
,
4615 struct secpolicyindex
*spidx1
)
4618 if (spidx0
== NULL
&& spidx1
== NULL
)
4621 if (spidx0
== NULL
|| spidx1
== NULL
)
4624 if (spidx0
->src
.ss_family
!= spidx1
->src
.ss_family
||
4625 spidx0
->dst
.ss_family
!= spidx1
->dst
.ss_family
||
4626 spidx0
->src
.ss_len
!= spidx1
->src
.ss_len
||
4627 spidx0
->dst
.ss_len
!= spidx1
->dst
.ss_len
)
4630 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4631 if (spidx0
->ul_proto
!= (u_int16_t
)IPSEC_ULPROTO_ANY
4632 && spidx0
->ul_proto
!= spidx1
->ul_proto
)
4635 switch (spidx0
->src
.ss_family
) {
4637 if (satosin(&spidx0
->src
)->sin_port
!= IPSEC_PORT_ANY
4638 && satosin(&spidx0
->src
)->sin_port
!=
4639 satosin(&spidx1
->src
)->sin_port
)
4641 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->src
)->sin_addr
,
4642 (caddr_t
)&satosin(&spidx1
->src
)->sin_addr
, spidx0
->prefs
))
4646 if (satosin6(&spidx0
->src
)->sin6_port
!= IPSEC_PORT_ANY
4647 && satosin6(&spidx0
->src
)->sin6_port
!=
4648 satosin6(&spidx1
->src
)->sin6_port
)
4651 * scope_id check. if sin6_scope_id is 0, we regard it
4652 * as a wildcard scope, which matches any scope zone ID.
4654 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
4655 satosin6(&spidx1
->src
)->sin6_scope_id
&&
4656 satosin6(&spidx0
->src
)->sin6_scope_id
!=
4657 satosin6(&spidx1
->src
)->sin6_scope_id
)
4659 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->src
)->sin6_addr
,
4660 (caddr_t
)&satosin6(&spidx1
->src
)->sin6_addr
, spidx0
->prefs
))
4665 if (bcmp(&spidx0
->src
, &spidx1
->src
, spidx0
->src
.ss_len
) != 0)
4670 switch (spidx0
->dst
.ss_family
) {
4672 if (satosin(&spidx0
->dst
)->sin_port
!= IPSEC_PORT_ANY
4673 && satosin(&spidx0
->dst
)->sin_port
!=
4674 satosin(&spidx1
->dst
)->sin_port
)
4676 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->dst
)->sin_addr
,
4677 (caddr_t
)&satosin(&spidx1
->dst
)->sin_addr
, spidx0
->prefd
))
4681 if (satosin6(&spidx0
->dst
)->sin6_port
!= IPSEC_PORT_ANY
4682 && satosin6(&spidx0
->dst
)->sin6_port
!=
4683 satosin6(&spidx1
->dst
)->sin6_port
)
4686 * scope_id check. if sin6_scope_id is 0, we regard it
4687 * as a wildcard scope, which matches any scope zone ID.
4689 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
4690 satosin6(&spidx1
->src
)->sin6_scope_id
&&
4691 satosin6(&spidx0
->dst
)->sin6_scope_id
!=
4692 satosin6(&spidx1
->dst
)->sin6_scope_id
)
4694 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->dst
)->sin6_addr
,
4695 (caddr_t
)&satosin6(&spidx1
->dst
)->sin6_addr
, spidx0
->prefd
))
4700 if (bcmp(&spidx0
->dst
, &spidx1
->dst
, spidx0
->dst
.ss_len
) != 0)
4705 /* XXX Do we check other field ? e.g. flowinfo */
4710 /* returns 0 on match */
4713 struct sockaddr
*sa1
,
4714 struct sockaddr
*sa2
,
4717 if (sa1
->sa_family
!= sa2
->sa_family
|| sa1
->sa_len
!= sa2
->sa_len
)
4720 switch (sa1
->sa_family
) {
4722 if (sa1
->sa_len
!= sizeof(struct sockaddr_in
))
4724 if (satosin(sa1
)->sin_addr
.s_addr
!=
4725 satosin(sa2
)->sin_addr
.s_addr
) {
4728 if (port
&& satosin(sa1
)->sin_port
!= satosin(sa2
)->sin_port
)
4732 if (sa1
->sa_len
!= sizeof(struct sockaddr_in6
))
4733 return 1; /*EINVAL*/
4734 if (satosin6(sa1
)->sin6_scope_id
!=
4735 satosin6(sa2
)->sin6_scope_id
) {
4738 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1
)->sin6_addr
,
4739 &satosin6(sa2
)->sin6_addr
)) {
4743 satosin6(sa1
)->sin6_port
!= satosin6(sa2
)->sin6_port
) {
4748 if (bcmp(sa1
, sa2
, sa1
->sa_len
) != 0)
4757 * compare two buffers with mask.
4761 * bits: Number of bits to compare
4774 /* XXX: This could be considerably faster if we compare a word
4775 * at a time, but it is complicated on LSB Endian machines */
4777 /* Handle null pointers */
4778 if (p1
== NULL
|| p2
== NULL
)
4788 mask
= ~((1<<(8-bits
))-1);
4789 if ((*p1
& mask
) != (*p2
& mask
))
4792 return 1; /* Match! */
4797 * scanning SPD and SAD to check status for each entries,
4798 * and do to remove or to expire.
4799 * XXX: year 2038 problem may remain.
4801 int key_timehandler_debug
= 0;
4802 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;
4803 u_int64_t total_sav_count
= 0;
4805 key_timehandler(void)
4809 struct secpolicy
**spbuf
= NULL
, **spptr
= NULL
;
4810 struct secasvar
**savexbuf
= NULL
, **savexptr
= NULL
;
4811 struct secasvar
**savkabuf
= NULL
, **savkaptr
= NULL
;
4812 int spbufcount
= 0, savbufcount
= 0, spcount
= 0, savexcount
= 0, savkacount
= 0, cnt
;
4816 /* pre-allocate buffers before taking the lock */
4817 /* if allocation failures occur - portions of the processing will be skipped */
4818 if ((spbufcount
= ipsec_policy_count
) != 0) {
4820 KMALLOC_WAIT(spbuf
, struct secpolicy
**, spbufcount
* sizeof(struct secpolicy
*));
4824 if ((savbufcount
= ipsec_sav_count
) != 0) {
4826 KMALLOC_WAIT(savexbuf
, struct secasvar
**, savbufcount
* sizeof(struct secasvar
*));
4828 savexptr
= savexbuf
;
4829 KMALLOC_WAIT(savkabuf
, struct secasvar
**, savbufcount
* sizeof(struct secasvar
*));
4831 savkaptr
= savkabuf
;
4833 lck_mtx_lock(sadb_mutex
);
4837 struct secpolicy
*sp
, *nextsp
;
4839 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
4840 for (sp
= LIST_FIRST(&sptree
[dir
]);
4845 nextsp
= LIST_NEXT(sp
, chain
);
4847 if (sp
->state
== IPSEC_SPSTATE_DEAD
) {
4848 key_freesp(sp
, KEY_SADB_LOCKED
);
4852 if (sp
->lifetime
== 0 && sp
->validtime
== 0)
4854 if (spbuf
&& spcount
< spbufcount
) {
4855 /* the deletion will occur next time */
4857 && tv
.tv_sec
- sp
->created
> sp
->lifetime
)
4859 && tv
.tv_sec
- sp
->lastused
> sp
->validtime
)) {
4860 //key_spdexpire(sp);
4861 sp
->state
= IPSEC_SPSTATE_DEAD
;
4872 if (savbufcount
!= 0) {
4873 struct secashead
*sah
, *nextsah
;
4874 struct secasvar
*sav
, *nextsav
;
4876 for (sah
= LIST_FIRST(&sahtree
);
4881 nextsah
= LIST_NEXT(sah
, chain
);
4883 /* if sah has been dead, then delete it and process next sah. */
4884 if (sah
->state
== SADB_SASTATE_DEAD
) {
4890 if (LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]) == NULL
&&
4891 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]) == NULL
&&
4892 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]) == NULL
&&
4893 LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]) == NULL
) {
4899 /* if LARVAL entry doesn't become MATURE, delete it. */
4900 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]);
4906 nextsav
= LIST_NEXT(sav
, chain
);
4908 if (tv
.tv_sec
- sav
->created
> key_larval_lifetime
) {
4909 key_freesav(sav
, KEY_SADB_LOCKED
);
4914 * If this is a NAT traversal SA with no activity,
4915 * we need to send a keep alive.
4917 * Performed outside of the loop before so we will
4918 * only ever send one keepalive. The first SA on
4919 * the list is the one that will be used for sending
4920 * traffic, so this is the one we use for determining
4921 * when to send the keepalive.
4923 if (savkabuf
&& savkacount
< savbufcount
) {
4924 sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]); //%%% should we check dying list if this is empty???
4925 if (natt_keepalive_interval
&& sav
&& (sav
->flags
& SADB_X_EXT_NATT_KEEPALIVE
) != 0) {
4933 * check MATURE entry to start to send expire message
4936 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]);
4942 nextsav
= LIST_NEXT(sav
, chain
);
4944 /* we don't need to check. */
4945 if (sav
->lft_s
== NULL
)
4949 if (sav
->lft_c
== NULL
) {
4950 ipseclog((LOG_DEBUG
,"key_timehandler: "
4951 "There is no CURRENT time, why?\n"));
4955 /* check SOFT lifetime */
4956 if (sav
->lft_s
->sadb_lifetime_addtime
!= 0
4957 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
4959 * check the SA if it has been used.
4960 * when it hasn't been used, delete it.
4961 * i don't think such SA will be used.
4963 if (sav
->lft_c
->sadb_lifetime_usetime
== 0) {
4964 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4965 key_freesav(sav
, KEY_SADB_LOCKED
);
4967 } else if (savexbuf
&& savexcount
< savbufcount
) {
4968 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
4975 /* check SOFT lifetime by bytes */
4977 * XXX I don't know the way to delete this SA
4978 * when new SA is installed. Caution when it's
4979 * installed too big lifetime by time.
4981 else if (savexbuf
&& savexcount
< savbufcount
4982 && sav
->lft_s
->sadb_lifetime_bytes
!= 0
4983 && sav
->lft_s
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
4986 * XXX If we keep to send expire
4987 * message in the status of
4988 * DYING. Do remove below code.
4991 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
4998 /* check DYING entry to change status to DEAD. */
4999 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]);
5005 nextsav
= LIST_NEXT(sav
, chain
);
5007 /* we don't need to check. */
5008 if (sav
->lft_h
== NULL
)
5012 if (sav
->lft_c
== NULL
) {
5013 ipseclog((LOG_DEBUG
, "key_timehandler: "
5014 "There is no CURRENT time, why?\n"));
5018 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0
5019 && tv
.tv_sec
- sav
->created
> sav
->lft_h
->sadb_lifetime_addtime
) {
5020 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5021 key_freesav(sav
, KEY_SADB_LOCKED
);
5024 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
5025 else if (savbuf
&& savexcount
< savbufcount
5026 && sav
->lft_s
!= NULL
5027 && sav
->lft_s
->sadb_lifetime_addtime
!= 0
5028 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
5030 * XXX: should be checked to be
5031 * installed the valid SA.
5035 * If there is no SA then sending
5044 /* check HARD lifetime by bytes */
5045 else if (sav
->lft_h
->sadb_lifetime_bytes
!= 0
5046 && sav
->lft_h
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
5047 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5048 key_freesav(sav
, KEY_SADB_LOCKED
);
5053 /* delete entry in DEAD */
5054 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]);
5060 nextsav
= LIST_NEXT(sav
, chain
);
5063 if (sav
->state
!= SADB_SASTATE_DEAD
) {
5064 ipseclog((LOG_DEBUG
, "key_timehandler: "
5065 "invalid sav->state "
5066 "(queue: %d SA: %d): "
5068 SADB_SASTATE_DEAD
, sav
->state
));
5072 * do not call key_freesav() here.
5073 * sav should already be freed, and sav->refcnt
5074 * shows other references to sav
5075 * (such as from SPD).
5081 if (++key_timehandler_debug
>= 300) {
5082 if (key_debug_level
) {
5083 printf("%s: total stats for %u calls\n", __FUNCTION__
, key_timehandler_debug
);
5084 printf("%s: walked %u SPDs\n", __FUNCTION__
, spd_count
);
5085 printf("%s: walked %llu SAs: LARVAL SAs %u, MATURE SAs %u, DYING SAs %u, DEAD SAs %u\n", __FUNCTION__
,
5086 total_sav_count
, larval_sav_count
, mature_sav_count
, dying_sav_count
, dead_sav_count
);
5087 printf("%s: walked %u SAHs: DEAD SAHs %u, EMPTY SAHs %u\n", __FUNCTION__
,
5088 sah_count
, dead_sah_count
, empty_sah_count
);
5089 if (sah_search_calls
) {
5090 printf("%s: SAH search cost %d iters per call\n", __FUNCTION__
,
5091 (sah_search_count
/sah_search_calls
));
5097 empty_sah_count
= 0;
5098 larval_sav_count
= 0;
5099 mature_sav_count
= 0;
5100 dying_sav_count
= 0;
5102 total_sav_count
= 0;
5103 sah_search_count
= 0;
5104 sah_search_calls
= 0;
5105 key_timehandler_debug
= 0;
5107 #ifndef IPSEC_NONBLOCK_ACQUIRE
5110 struct secacq
*acq
, *nextacq
;
5112 for (acq
= LIST_FIRST(&acqtree
);
5116 nextacq
= LIST_NEXT(acq
, chain
);
5118 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
5119 && __LIST_CHAINED(acq
)) {
5120 LIST_REMOVE(acq
, chain
);
5129 struct secspacq
*acq
, *nextacq
;
5131 for (acq
= LIST_FIRST(&spacqtree
);
5135 nextacq
= LIST_NEXT(acq
, chain
);
5137 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
5138 && __LIST_CHAINED(acq
)) {
5139 LIST_REMOVE(acq
, chain
);
5145 /* initialize random seed */
5146 if (key_tick_init_random
++ > key_int_random
) {
5147 key_tick_init_random
= 0;
5153 lck_mtx_unlock(sadb_mutex
);
5155 /* send messages outside of sadb_mutex */
5156 if (spbuf
&& spcount
> 0) {
5159 key_spdexpire(*(--spptr
));
5161 if (savkabuf
&& savkacount
> 0) {
5162 struct secasvar
**savkaptr_sav
= savkaptr
;
5163 int cnt_send
= savkacount
;
5165 while (cnt_send
--) {
5166 if (ipsec_send_natt_keepalive(*(--savkaptr
))) {
5167 // <rdar://6768487> iterate (all over again) and update timestamps
5168 struct secasvar
**savkaptr_update
= savkaptr_sav
;
5169 int cnt_update
= savkacount
;
5170 while (cnt_update
--) {
5171 key_update_natt_keepalive_timestamp(*savkaptr
,
5172 *(--savkaptr_update
));
5177 if (savexbuf
&& savexcount
> 0) {
5180 key_expire(*(--savexptr
));
5183 /* decrement ref counts and free buffers */
5184 lck_mtx_lock(sadb_mutex
);
5187 key_freesp(*spptr
++, KEY_SADB_LOCKED
);
5191 while (savkacount
--)
5192 key_freesav(*savkaptr
++, KEY_SADB_LOCKED
);
5196 while (savexcount
--)
5197 key_freesav(*savexptr
++, KEY_SADB_LOCKED
);
5200 lck_mtx_unlock(sadb_mutex
);
5203 #ifndef IPSEC_DEBUG2
5204 /* do exchange to tick time !! */
5205 (void)timeout((void *)key_timehandler
, (void *)0, hz
);
5206 #endif /* IPSEC_DEBUG2 */
5212 * to initialize a seed for random()
5218 /* Our PRNG is based on Yarrow and doesn't need to be seeded */
5225 srandom(tv
.tv_usec
);
5236 key_randomfill(&value
, sizeof(value
));
5247 read_random(p
, (u_int
)l
);
5251 static int warn
= 1;
5254 n
= (size_t)read_random(p
, (u_int
)l
);
5258 bcopy(&v
, (u_int8_t
*)p
+ n
,
5259 l
- n
< sizeof(v
) ? l
- n
: sizeof(v
));
5263 printf("WARNING: pseudo-random number generator "
5264 "used for IPsec processing\n");
5272 * map SADB_SATYPE_* to IPPROTO_*.
5273 * if satype == SADB_SATYPE then satype is mapped to ~0.
5275 * 0: invalid satype.
5282 case SADB_SATYPE_UNSPEC
:
5283 return IPSEC_PROTO_ANY
;
5284 case SADB_SATYPE_AH
:
5286 case SADB_SATYPE_ESP
:
5288 case SADB_X_SATYPE_IPCOMP
:
5289 return IPPROTO_IPCOMP
;
5298 * map IPPROTO_* to SADB_SATYPE_*
5300 * 0: invalid protocol type.
5308 return SADB_SATYPE_AH
;
5310 return SADB_SATYPE_ESP
;
5311 case IPPROTO_IPCOMP
:
5312 return SADB_X_SATYPE_IPCOMP
;
5322 * SADB_GETSPI processing is to receive
5323 * <base, (SA2), src address, dst address, (SPI range)>
5324 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
5325 * tree with the status of LARVAL, and send
5326 * <base, SA(*), address(SD)>
5329 * IN: mhp: pointer to the pointer to each header.
5330 * OUT: NULL if fail.
5331 * other if success, return pointer to the message to send.
5337 const struct sadb_msghdr
*mhp
)
5339 struct sadb_address
*src0
, *dst0
;
5340 struct secasindex saidx
;
5341 struct secashead
*newsah
;
5342 struct secasvar
*newsav
;
5349 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
5352 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5353 panic("key_getspi: NULL pointer is passed.\n");
5355 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5356 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
5357 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
5358 return key_senderror(so
, m
, EINVAL
);
5360 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5361 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5362 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
5363 return key_senderror(so
, m
, EINVAL
);
5365 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
5366 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
5367 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
5369 mode
= IPSEC_MODE_ANY
;
5373 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5374 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5376 /* map satype to proto */
5377 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5378 ipseclog((LOG_DEBUG
, "key_getspi: invalid satype is passed.\n"));
5379 return key_senderror(so
, m
, EINVAL
);
5382 /* make sure if port number is zero. */
5383 switch (((struct sockaddr
*)(src0
+ 1))->sa_family
) {
5385 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
5386 sizeof(struct sockaddr_in
))
5387 return key_senderror(so
, m
, EINVAL
);
5388 ((struct sockaddr_in
*)(src0
+ 1))->sin_port
= 0;
5391 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
5392 sizeof(struct sockaddr_in6
))
5393 return key_senderror(so
, m
, EINVAL
);
5394 ((struct sockaddr_in6
*)(src0
+ 1))->sin6_port
= 0;
5399 switch (((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
5401 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
5402 sizeof(struct sockaddr_in
))
5403 return key_senderror(so
, m
, EINVAL
);
5404 ((struct sockaddr_in
*)(dst0
+ 1))->sin_port
= 0;
5407 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
5408 sizeof(struct sockaddr_in6
))
5409 return key_senderror(so
, m
, EINVAL
);
5410 ((struct sockaddr_in6
*)(dst0
+ 1))->sin6_port
= 0;
5416 /* XXX boundary check against sa_len */
5417 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
5419 lck_mtx_lock(sadb_mutex
);
5421 /* SPI allocation */
5422 spi
= key_do_getnewspi((struct sadb_spirange
*)mhp
->ext
[SADB_EXT_SPIRANGE
],
5425 lck_mtx_unlock(sadb_mutex
);
5426 return key_senderror(so
, m
, EINVAL
);
5429 /* get a SA index */
5430 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
5431 /* create a new SA index: key_addspi is always used for inbound spi */
5432 if ((newsah
= key_newsah(&saidx
, IPSEC_DIR_INBOUND
)) == NULL
) {
5433 lck_mtx_unlock(sadb_mutex
);
5434 ipseclog((LOG_DEBUG
, "key_getspi: No more memory.\n"));
5435 return key_senderror(so
, m
, ENOBUFS
);
5441 newsav
= key_newsav(m
, mhp
, newsah
, &error
);
5442 if (newsav
== NULL
) {
5443 /* XXX don't free new SA index allocated in above. */
5444 lck_mtx_unlock(sadb_mutex
);
5445 return key_senderror(so
, m
, error
);
5449 key_setspi(newsav
, htonl(spi
));
5451 #ifndef IPSEC_NONBLOCK_ACQUIRE
5452 /* delete the entry in acqtree */
5453 if (mhp
->msg
->sadb_msg_seq
!= 0) {
5455 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) != NULL
) {
5456 /* reset counter in order to deletion by timehandler. */
5459 acq
->created
= tv
.tv_sec
;
5465 lck_mtx_unlock(sadb_mutex
);
5468 struct mbuf
*n
, *nn
;
5469 struct sadb_sa
*m_sa
;
5470 struct sadb_msg
*newmsg
;
5473 /* create new sadb_msg to reply. */
5474 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
)) +
5475 PFKEY_ALIGN8(sizeof(struct sadb_sa
));
5477 return key_senderror(so
, m
, ENOBUFS
);
5479 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
5481 MCLGET(n
, M_DONTWAIT
);
5482 if ((n
->m_flags
& M_EXT
) == 0) {
5488 return key_senderror(so
, m
, ENOBUFS
);
5494 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
5495 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
5497 m_sa
= (struct sadb_sa
*)(mtod(n
, caddr_t
) + off
);
5498 m_sa
->sadb_sa_len
= PFKEY_UNIT64(sizeof(struct sadb_sa
));
5499 m_sa
->sadb_sa_exttype
= SADB_EXT_SA
;
5500 m_sa
->sadb_sa_spi
= htonl(spi
);
5501 off
+= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
5505 panic("length inconsistency in key_getspi");
5508 int mbufItems
[] = {SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
5509 n
->m_next
= key_gather_mbuf(m
, mhp
, 0, sizeof(mbufItems
)/sizeof(int), mbufItems
);
5512 return key_senderror(so
, m
, ENOBUFS
);
5516 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5517 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5519 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
5522 n
->m_pkthdr
.len
= 0;
5523 for (nn
= n
; nn
; nn
= nn
->m_next
)
5524 n
->m_pkthdr
.len
+= nn
->m_len
;
5526 newmsg
= mtod(n
, struct sadb_msg
*);
5527 newmsg
->sadb_msg_seq
= newsav
->seq
;
5528 newmsg
->sadb_msg_errno
= 0;
5529 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
5532 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
5537 * allocating new SPI
5538 * called by key_getspi().
5545 struct sadb_spirange
*spirange
,
5546 struct secasindex
*saidx
)
5549 u_int32_t keymin
, keymax
;
5550 int count
= key_spi_trycnt
;
5552 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5554 /* set spi range to allocate */
5555 if (spirange
!= NULL
) {
5556 keymin
= spirange
->sadb_spirange_min
;
5557 keymax
= spirange
->sadb_spirange_max
;
5559 keymin
= key_spi_minval
;
5560 keymax
= key_spi_maxval
;
5562 /* IPCOMP needs 2-byte SPI */
5563 if (saidx
->proto
== IPPROTO_IPCOMP
) {
5565 if (keymin
>= 0x10000)
5567 if (keymax
>= 0x10000)
5569 if (keymin
> keymax
) {
5570 t
= keymin
; keymin
= keymax
; keymax
= t
;
5574 if (keymin
== keymax
) {
5575 if (key_checkspidup(saidx
, keymin
) != NULL
) {
5576 ipseclog((LOG_DEBUG
, "key_do_getnewspi: SPI %u exists already.\n", keymin
));
5580 count
--; /* taking one cost. */
5585 u_int32_t range
= keymax
- keymin
+ 1; /* overflow value of zero means full range */
5590 /* when requesting to allocate spi ranged */
5592 u_int32_t rand_val
= key_random();
5594 /* generate pseudo-random SPI value ranged. */
5595 newspi
= (range
== 0 ? rand_val
: keymin
+ (rand_val
% range
));
5597 if (key_checkspidup(saidx
, newspi
) == NULL
)
5601 if (count
== 0 || newspi
== 0) {
5602 ipseclog((LOG_DEBUG
, "key_do_getnewspi: to allocate spi is failed.\n"));
5608 keystat
.getspi_count
=
5609 (keystat
.getspi_count
+ key_spi_trycnt
- count
) / 2;
5615 * SADB_UPDATE processing
5617 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5618 * key(AE), (identity(SD),) (sensitivity)>
5619 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
5621 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5622 * (identity(SD),) (sensitivity)>
5625 * m will always be freed.
5631 const struct sadb_msghdr
*mhp
)
5633 struct sadb_sa
*sa0
;
5634 struct sadb_address
*src0
, *dst0
;
5635 struct secasindex saidx
;
5636 struct secashead
*sah
;
5637 struct secasvar
*sav
;
5643 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
5646 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5647 panic("key_update: NULL pointer is passed.\n");
5649 /* map satype to proto */
5650 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5651 ipseclog((LOG_DEBUG
, "key_update: invalid satype is passed.\n"));
5652 return key_senderror(so
, m
, EINVAL
);
5655 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
5656 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5657 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
5658 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
5659 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
5660 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
5661 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
5662 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
5663 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
5664 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
5665 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
5666 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
5667 return key_senderror(so
, m
, EINVAL
);
5669 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
5670 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5671 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5672 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
5673 return key_senderror(so
, m
, EINVAL
);
5675 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
5676 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
5677 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
5679 mode
= IPSEC_MODE_ANY
;
5682 /* XXX boundary checking for other extensions */
5684 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5685 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5686 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5688 /* XXX boundary check against sa_len */
5689 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
5691 lck_mtx_lock(sadb_mutex
);
5693 /* get a SA header */
5694 if ((sah
= key_getsah(&saidx
)) == NULL
) {
5695 lck_mtx_unlock(sadb_mutex
);
5696 ipseclog((LOG_DEBUG
, "key_update: no SA index found.\n"));
5697 return key_senderror(so
, m
, ENOENT
);
5700 /* set spidx if there */
5702 error
= key_setident(sah
, m
, mhp
);
5704 lck_mtx_unlock(sadb_mutex
);
5705 return key_senderror(so
, m
, error
);
5708 /* find a SA with sequence number. */
5709 #if IPSEC_DOSEQCHECK
5710 if (mhp
->msg
->sadb_msg_seq
!= 0
5711 && (sav
= key_getsavbyseq(sah
, mhp
->msg
->sadb_msg_seq
)) == NULL
) {
5712 lck_mtx_unlock(sadb_mutex
);
5713 ipseclog((LOG_DEBUG
,
5714 "key_update: no larval SA with sequence %u exists.\n",
5715 mhp
->msg
->sadb_msg_seq
));
5716 return key_senderror(so
, m
, ENOENT
);
5719 if ((sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
)) == NULL
) {
5720 lck_mtx_unlock(sadb_mutex
);
5721 ipseclog((LOG_DEBUG
,
5722 "key_update: no such a SA found (spi:%u)\n",
5723 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
5724 return key_senderror(so
, m
, EINVAL
);
5728 /* validity check */
5729 if (sav
->sah
->saidx
.proto
!= proto
) {
5730 lck_mtx_unlock(sadb_mutex
);
5731 ipseclog((LOG_DEBUG
,
5732 "key_update: protocol mismatched (DB=%u param=%u)\n",
5733 sav
->sah
->saidx
.proto
, proto
));
5734 return key_senderror(so
, m
, EINVAL
);
5736 #if IPSEC_DOSEQCHECK
5737 if (sav
->spi
!= sa0
->sadb_sa_spi
) {
5738 lck_mtx_unlock(sadb_mutex
);
5739 ipseclog((LOG_DEBUG
,
5740 "key_update: SPI mismatched (DB:%u param:%u)\n",
5741 (u_int32_t
)ntohl(sav
->spi
),
5742 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
5743 return key_senderror(so
, m
, EINVAL
);
5746 if (sav
->pid
!= mhp
->msg
->sadb_msg_pid
) {
5747 lck_mtx_unlock(sadb_mutex
);
5748 ipseclog((LOG_DEBUG
,
5749 "key_update: pid mismatched (DB:%u param:%u)\n",
5750 sav
->pid
, mhp
->msg
->sadb_msg_pid
));
5751 return key_senderror(so
, m
, EINVAL
);
5754 /* copy sav values */
5755 error
= key_setsaval(sav
, m
, mhp
);
5757 key_freesav(sav
, KEY_SADB_LOCKED
);
5758 lck_mtx_unlock(sadb_mutex
);
5759 return key_senderror(so
, m
, error
);
5763 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
5764 * this SA is for transport mode - otherwise clear it.
5766 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0 &&
5767 (sav
->sah
->saidx
.mode
!= IPSEC_MODE_TRANSPORT
||
5768 sav
->sah
->saidx
.src
.ss_family
!= AF_INET
))
5769 sav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
5771 /* check SA values to be mature. */
5772 if ((error
= key_mature(sav
)) != 0) {
5773 key_freesav(sav
, KEY_SADB_LOCKED
);
5774 lck_mtx_unlock(sadb_mutex
);
5775 return key_senderror(so
, m
, error
);
5778 lck_mtx_unlock(sadb_mutex
);
5783 /* set msg buf from mhp */
5784 n
= key_getmsgbuf_x1(m
, mhp
);
5786 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
5787 return key_senderror(so
, m
, ENOBUFS
);
5791 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5796 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
5797 * only called by key_update().
5800 * others : found, pointer to a SA.
5802 #if IPSEC_DOSEQCHECK
5803 static struct secasvar
*
5805 struct secashead
*sah
,
5808 struct secasvar
*sav
;
5811 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5813 state
= SADB_SASTATE_LARVAL
;
5815 /* search SAD with sequence number ? */
5816 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
5818 KEY_CHKSASTATE(state
, sav
->state
, "key_getsabyseq");
5820 if (sav
->seq
== seq
) {
5822 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
5823 printf("DP key_getsavbyseq cause "
5824 "refcnt++:%d SA:%p\n",
5835 * SADB_ADD processing
5836 * add a entry to SA database, when received
5837 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5838 * key(AE), (identity(SD),) (sensitivity)>
5841 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5842 * (identity(SD),) (sensitivity)>
5845 * IGNORE identity and sensitivity messages.
5847 * m will always be freed.
5853 const struct sadb_msghdr
*mhp
)
5855 struct sadb_sa
*sa0
;
5856 struct sadb_address
*src0
, *dst0
;
5857 struct secasindex saidx
;
5858 struct secashead
*newsah
;
5859 struct secasvar
*newsav
;
5865 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
5868 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5869 panic("key_add: NULL pointer is passed.\n");
5871 /* map satype to proto */
5872 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5873 ipseclog((LOG_DEBUG
, "key_add: invalid satype is passed.\n"));
5874 return key_senderror(so
, m
, EINVAL
);
5877 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
5878 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5879 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
5880 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
5881 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
5882 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
5883 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
5884 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
5885 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
5886 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
5887 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
5888 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
5889 return key_senderror(so
, m
, EINVAL
);
5891 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
5892 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5893 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5895 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
5896 return key_senderror(so
, m
, EINVAL
);
5898 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
5899 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
5900 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
5902 mode
= IPSEC_MODE_ANY
;
5906 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5907 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
5908 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
5910 /* XXX boundary check against sa_len */
5911 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
5913 lck_mtx_lock(sadb_mutex
);
5915 /* get a SA header */
5916 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
5917 /* create a new SA header: key_addspi is always used for outbound spi */
5918 if ((newsah
= key_newsah(&saidx
, IPSEC_DIR_OUTBOUND
)) == NULL
) {
5919 lck_mtx_unlock(sadb_mutex
);
5920 ipseclog((LOG_DEBUG
, "key_add: No more memory.\n"));
5921 return key_senderror(so
, m
, ENOBUFS
);
5925 /* set spidx if there */
5927 error
= key_setident(newsah
, m
, mhp
);
5929 lck_mtx_unlock(sadb_mutex
);
5930 return key_senderror(so
, m
, error
);
5933 /* create new SA entry. */
5934 /* We can create new SA only if SPI is different. */
5935 if (key_getsavbyspi(newsah
, sa0
->sadb_sa_spi
)) {
5936 lck_mtx_unlock(sadb_mutex
);
5937 ipseclog((LOG_DEBUG
, "key_add: SA already exists.\n"));
5938 return key_senderror(so
, m
, EEXIST
);
5940 newsav
= key_newsav(m
, mhp
, newsah
, &error
);
5941 if (newsav
== NULL
) {
5942 lck_mtx_unlock(sadb_mutex
);
5943 return key_senderror(so
, m
, error
);
5947 * Verify if SADB_X_EXT_NATT_MULTIPLEUSERS flag is set that
5948 * this SA is for transport mode - otherwise clear it.
5950 if ((newsav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0 &&
5951 (newsah
->saidx
.mode
!= IPSEC_MODE_TRANSPORT
||
5952 newsah
->saidx
.dst
.ss_family
!= AF_INET
))
5953 newsav
->flags
&= ~SADB_X_EXT_NATT_MULTIPLEUSERS
;
5955 /* check SA values to be mature. */
5956 if ((error
= key_mature(newsav
)) != 0) {
5957 key_freesav(newsav
, KEY_SADB_LOCKED
);
5958 lck_mtx_unlock(sadb_mutex
);
5959 return key_senderror(so
, m
, error
);
5962 lck_mtx_unlock(sadb_mutex
);
5965 * don't call key_freesav() here, as we would like to keep the SA
5966 * in the database on success.
5972 /* set msg buf from mhp */
5973 n
= key_getmsgbuf_x1(m
, mhp
);
5975 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
5976 return key_senderror(so
, m
, ENOBUFS
);
5980 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5987 struct secashead
*sah
,
5989 const struct sadb_msghdr
*mhp
)
5991 const struct sadb_ident
*idsrc
, *iddst
;
5992 int idsrclen
, iddstlen
;
5994 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5997 if (sah
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5998 panic("key_setident: NULL pointer is passed.\n");
6000 /* don't make buffer if not there */
6001 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
&&
6002 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
6008 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
||
6009 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
6010 ipseclog((LOG_DEBUG
, "key_setident: invalid identity.\n"));
6014 idsrc
= (const struct sadb_ident
*)mhp
->ext
[SADB_EXT_IDENTITY_SRC
];
6015 iddst
= (const struct sadb_ident
*)mhp
->ext
[SADB_EXT_IDENTITY_DST
];
6016 idsrclen
= mhp
->extlen
[SADB_EXT_IDENTITY_SRC
];
6017 iddstlen
= mhp
->extlen
[SADB_EXT_IDENTITY_DST
];
6019 /* validity check */
6020 if (idsrc
->sadb_ident_type
!= iddst
->sadb_ident_type
) {
6021 ipseclog((LOG_DEBUG
, "key_setident: ident type mismatch.\n"));
6025 switch (idsrc
->sadb_ident_type
) {
6026 case SADB_IDENTTYPE_PREFIX
:
6027 case SADB_IDENTTYPE_FQDN
:
6028 case SADB_IDENTTYPE_USERFQDN
:
6030 /* XXX do nothing */
6036 /* make structure */
6037 KMALLOC_NOWAIT(sah
->idents
, struct sadb_ident
*, idsrclen
);
6038 if (sah
->idents
== NULL
) {
6039 lck_mtx_unlock(sadb_mutex
);
6040 KMALLOC_WAIT(sah
->idents
, struct sadb_ident
*, idsrclen
);
6041 lck_mtx_lock(sadb_mutex
);
6042 if (sah
->idents
== NULL
) {
6043 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
6047 KMALLOC_NOWAIT(sah
->identd
, struct sadb_ident
*, iddstlen
);
6048 if (sah
->identd
== NULL
) {
6049 lck_mtx_unlock(sadb_mutex
);
6050 KMALLOC_WAIT(sah
->identd
, struct sadb_ident
*, iddstlen
);
6051 lck_mtx_lock(sadb_mutex
);
6052 if (sah
->identd
== NULL
) {
6055 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
6059 bcopy(idsrc
, sah
->idents
, idsrclen
);
6060 bcopy(iddst
, sah
->identd
, iddstlen
);
6066 * m will not be freed on return.
6067 * it is caller's responsibility to free the result.
6069 static struct mbuf
*
6072 const struct sadb_msghdr
*mhp
)
6075 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
6076 SADB_X_EXT_SA2
, SADB_EXT_ADDRESS_SRC
,
6077 SADB_EXT_ADDRESS_DST
, SADB_EXT_LIFETIME_HARD
,
6078 SADB_EXT_LIFETIME_SOFT
, SADB_EXT_IDENTITY_SRC
,
6079 SADB_EXT_IDENTITY_DST
};
6082 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6083 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
6085 /* create new sadb_msg to reply. */
6086 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
6090 if (n
->m_len
< sizeof(struct sadb_msg
)) {
6091 n
= m_pullup(n
, sizeof(struct sadb_msg
));
6095 mtod(n
, struct sadb_msg
*)->sadb_msg_errno
= 0;
6096 mtod(n
, struct sadb_msg
*)->sadb_msg_len
=
6097 PFKEY_UNIT64(n
->m_pkthdr
.len
);
6102 static int key_delete_all(struct socket
*, struct mbuf
*,
6103 const struct sadb_msghdr
*, u_int16_t
);
6106 * SADB_DELETE processing
6108 * <base, SA(*), address(SD)>
6109 * from the ikmpd, and set SADB_SASTATE_DEAD,
6111 * <base, SA(*), address(SD)>
6114 * m will always be freed.
6120 const struct sadb_msghdr
*mhp
)
6122 struct sadb_sa
*sa0
;
6123 struct sadb_address
*src0
, *dst0
;
6124 struct secasindex saidx
;
6125 struct secashead
*sah
;
6126 struct secasvar
*sav
= NULL
;
6129 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6132 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6133 panic("key_delete: NULL pointer is passed.\n");
6135 /* map satype to proto */
6136 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6137 ipseclog((LOG_DEBUG
, "key_delete: invalid satype is passed.\n"));
6138 return key_senderror(so
, m
, EINVAL
);
6141 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6142 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
6143 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
6144 return key_senderror(so
, m
, EINVAL
);
6147 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6148 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
6149 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
6150 return key_senderror(so
, m
, EINVAL
);
6153 lck_mtx_lock(sadb_mutex
);
6155 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
6157 * Caller wants us to delete all non-LARVAL SAs
6158 * that match the src/dst. This is used during
6159 * IKE INITIAL-CONTACT.
6161 ipseclog((LOG_DEBUG
, "key_delete: doing delete all.\n"));
6162 /* key_delete_all will unlock sadb_mutex */
6163 return key_delete_all(so
, m
, mhp
, proto
);
6164 } else if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
)) {
6165 lck_mtx_unlock(sadb_mutex
);
6166 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
6167 return key_senderror(so
, m
, EINVAL
);
6170 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
6171 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
6172 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
6174 /* XXX boundary check against sa_len */
6175 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
6177 /* get a SA header */
6178 LIST_FOREACH(sah
, &sahtree
, chain
) {
6179 if (sah
->state
== SADB_SASTATE_DEAD
)
6181 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
6184 /* get a SA with SPI. */
6185 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
6190 lck_mtx_unlock(sadb_mutex
);
6191 ipseclog((LOG_DEBUG
, "key_delete: no SA found.\n"));
6192 return key_senderror(so
, m
, ENOENT
);
6195 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6196 key_freesav(sav
, KEY_SADB_LOCKED
);
6198 lck_mtx_unlock(sadb_mutex
);
6203 struct sadb_msg
*newmsg
;
6204 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
6205 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
6207 /* create new sadb_msg to reply. */
6208 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
6210 return key_senderror(so
, m
, ENOBUFS
);
6212 if (n
->m_len
< sizeof(struct sadb_msg
)) {
6213 n
= m_pullup(n
, sizeof(struct sadb_msg
));
6215 return key_senderror(so
, m
, ENOBUFS
);
6217 newmsg
= mtod(n
, struct sadb_msg
*);
6218 newmsg
->sadb_msg_errno
= 0;
6219 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
6222 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
6227 * delete all SAs for src/dst. Called from key_delete().
6233 const struct sadb_msghdr
*mhp
,
6236 struct sadb_address
*src0
, *dst0
;
6237 struct secasindex saidx
;
6238 struct secashead
*sah
;
6239 struct secasvar
*sav
, *nextsav
;
6240 u_int stateidx
, state
;
6242 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6244 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
6245 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
6247 /* XXX boundary check against sa_len */
6248 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
6250 LIST_FOREACH(sah
, &sahtree
, chain
) {
6251 if (sah
->state
== SADB_SASTATE_DEAD
)
6253 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
6256 /* Delete all non-LARVAL SAs. */
6258 stateidx
< _ARRAYLEN(saorder_state_alive
);
6260 state
= saorder_state_alive
[stateidx
];
6261 if (state
== SADB_SASTATE_LARVAL
)
6263 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
6264 sav
!= NULL
; sav
= nextsav
) {
6265 nextsav
= LIST_NEXT(sav
, chain
);
6267 if (sav
->state
!= state
) {
6268 ipseclog((LOG_DEBUG
, "key_delete_all: "
6269 "invalid sav->state "
6270 "(queue: %d SA: %d)\n",
6271 state
, sav
->state
));
6275 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6276 key_freesav(sav
, KEY_SADB_LOCKED
);
6280 lck_mtx_unlock(sadb_mutex
);
6284 struct sadb_msg
*newmsg
;
6285 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_ADDRESS_SRC
,
6286 SADB_EXT_ADDRESS_DST
};
6288 /* create new sadb_msg to reply. */
6289 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
6291 return key_senderror(so
, m
, ENOBUFS
);
6293 if (n
->m_len
< sizeof(struct sadb_msg
)) {
6294 n
= m_pullup(n
, sizeof(struct sadb_msg
));
6296 return key_senderror(so
, m
, ENOBUFS
);
6298 newmsg
= mtod(n
, struct sadb_msg
*);
6299 newmsg
->sadb_msg_errno
= 0;
6300 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
6303 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
6308 * SADB_GET processing
6310 * <base, SA(*), address(SD)>
6311 * from the ikmpd, and get a SP and a SA to respond,
6313 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
6314 * (identity(SD),) (sensitivity)>
6317 * m will always be freed.
6323 const struct sadb_msghdr
*mhp
)
6325 struct sadb_sa
*sa0
;
6326 struct sadb_address
*src0
, *dst0
;
6327 struct secasindex saidx
;
6328 struct secashead
*sah
;
6329 struct secasvar
*sav
= NULL
;
6332 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6335 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6336 panic("key_get: NULL pointer is passed.\n");
6338 /* map satype to proto */
6339 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6340 ipseclog((LOG_DEBUG
, "key_get: invalid satype is passed.\n"));
6341 return key_senderror(so
, m
, EINVAL
);
6344 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
6345 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6346 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
6347 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
6348 return key_senderror(so
, m
, EINVAL
);
6350 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
6351 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6352 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
6353 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
6354 return key_senderror(so
, m
, EINVAL
);
6357 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
6358 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
6359 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
6361 /* XXX boundary check against sa_len */
6362 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
6364 lck_mtx_lock(sadb_mutex
);
6366 /* get a SA header */
6367 LIST_FOREACH(sah
, &sahtree
, chain
) {
6368 if (sah
->state
== SADB_SASTATE_DEAD
)
6370 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
6373 /* get a SA with SPI. */
6374 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
6379 lck_mtx_unlock(sadb_mutex
);
6380 ipseclog((LOG_DEBUG
, "key_get: no SA found.\n"));
6381 return key_senderror(so
, m
, ENOENT
);
6388 /* map proto to satype */
6389 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
6390 lck_mtx_unlock(sadb_mutex
);
6391 ipseclog((LOG_DEBUG
, "key_get: there was invalid proto in SAD.\n"));
6392 return key_senderror(so
, m
, EINVAL
);
6394 lck_mtx_unlock(sadb_mutex
);
6396 /* create new sadb_msg to reply. */
6397 n
= key_setdumpsa(sav
, SADB_GET
, satype
, mhp
->msg
->sadb_msg_seq
,
6398 mhp
->msg
->sadb_msg_pid
);
6403 return key_senderror(so
, m
, ENOBUFS
);
6406 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
6411 * get SA stats by spi.
6412 * OUT: -1 : not found
6413 * 0 : found, arg pointer to a SA stats is updated.
6416 key_getsastatbyspi_one (u_int32_t spi
,
6417 struct sastat
*stat
)
6419 struct secashead
*sah
;
6420 struct secasvar
*sav
= NULL
;
6422 if ((void *)stat
== NULL
) {
6426 lck_mtx_lock(sadb_mutex
);
6428 /* get a SA header */
6429 LIST_FOREACH(sah
, &sahtree
, chain
) {
6430 if (sah
->state
== SADB_SASTATE_DEAD
)
6433 /* get a SA with SPI. */
6434 sav
= key_getsavbyspi(sah
, spi
);
6436 stat
->spi
= sav
->spi
;
6437 stat
->created
= sav
->created
;
6439 bcopy(sav
->lft_c
,&stat
->lft_c
, sizeof(stat
->lft_c
));
6441 bzero(&stat
->lft_c
, sizeof(stat
->lft_c
));
6443 lck_mtx_unlock(sadb_mutex
);
6448 lck_mtx_unlock(sadb_mutex
);
6454 * get SA stats collection by indices.
6455 * OUT: -1 : not found
6456 * 0 : found, arg pointers to a SA stats and 'maximum stats' are updated.
6459 key_getsastatbyspi (struct sastat
*stat_arg
,
6460 u_int32_t max_stat_arg
,
6461 struct sastat
*stat_res
,
6462 u_int32_t
*max_stat_res
)
6466 if (stat_arg
== NULL
||
6468 max_stat_res
== NULL
) {
6472 for (cur
= 0; cur
< max_stat_arg
; cur
++) {
6473 if (key_getsastatbyspi_one(stat_arg
[cur
].spi
,
6474 &stat_res
[found
]) == 0) {
6478 *max_stat_res
= found
;
6486 /* XXX make it sysctl-configurable? */
6488 key_getcomb_setlifetime(
6489 struct sadb_comb
*comb
)
6492 comb
->sadb_comb_soft_allocations
= 1;
6493 comb
->sadb_comb_hard_allocations
= 1;
6494 comb
->sadb_comb_soft_bytes
= 0;
6495 comb
->sadb_comb_hard_bytes
= 0;
6496 comb
->sadb_comb_hard_addtime
= 86400; /* 1 day */
6497 comb
->sadb_comb_soft_addtime
= comb
->sadb_comb_soft_addtime
* 80 / 100;
6498 comb
->sadb_comb_soft_usetime
= 28800; /* 8 hours */
6499 comb
->sadb_comb_hard_usetime
= comb
->sadb_comb_hard_usetime
* 80 / 100;
6504 * XXX reorder combinations by preference
6505 * XXX no idea if the user wants ESP authentication or not
6507 static struct mbuf
*
6508 key_getcomb_esp(void)
6510 struct sadb_comb
*comb
;
6511 const struct esp_algorithm
*algo
;
6512 struct mbuf
*result
= NULL
, *m
, *n
;
6516 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
6519 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
6520 algo
= esp_algorithm_lookup(i
);
6524 if (algo
->keymax
< ipsec_esp_keymin
)
6526 if (algo
->keymin
< ipsec_esp_keymin
)
6527 encmin
= ipsec_esp_keymin
;
6529 encmin
= algo
->keymin
;
6532 m
= key_getcomb_ah();
6536 panic("assumption failed in key_getcomb_esp");
6538 MGET(m
, M_DONTWAIT
, MT_DATA
);
6543 bzero(mtod(m
, caddr_t
), m
->m_len
);
6550 for (n
= m
; n
; n
= n
->m_next
)
6554 panic("assumption failed in key_getcomb_esp");
6557 for (off
= 0; off
< totlen
; off
+= l
) {
6558 n
= m_pulldown(m
, off
, l
, &o
);
6560 /* m is already freed */
6563 comb
= (struct sadb_comb
*)(mtod(n
, caddr_t
) + o
);
6564 bzero(comb
, sizeof(*comb
));
6565 key_getcomb_setlifetime(comb
);
6566 comb
->sadb_comb_encrypt
= i
;
6567 comb
->sadb_comb_encrypt_minbits
= encmin
;
6568 comb
->sadb_comb_encrypt_maxbits
= algo
->keymax
;
6587 * XXX reorder combinations by preference
6589 static struct mbuf
*
6590 key_getcomb_ah(void)
6592 struct sadb_comb
*comb
;
6593 const struct ah_algorithm
*algo
;
6597 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
6600 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
6602 /* we prefer HMAC algorithms, not old algorithms */
6603 if (i
!= SADB_AALG_SHA1HMAC
&& i
!= SADB_AALG_MD5HMAC
)
6606 algo
= ah_algorithm_lookup(i
);
6610 if (algo
->keymax
< ipsec_ah_keymin
)
6612 if (algo
->keymin
< ipsec_ah_keymin
)
6613 keymin
= ipsec_ah_keymin
;
6615 keymin
= algo
->keymin
;
6620 panic("assumption failed in key_getcomb_ah");
6622 MGET(m
, M_DONTWAIT
, MT_DATA
);
6629 M_PREPEND(m
, l
, M_DONTWAIT
);
6633 comb
= mtod(m
, struct sadb_comb
*);
6634 bzero(comb
, sizeof(*comb
));
6635 key_getcomb_setlifetime(comb
);
6636 comb
->sadb_comb_auth
= i
;
6637 comb
->sadb_comb_auth_minbits
= keymin
;
6638 comb
->sadb_comb_auth_maxbits
= algo
->keymax
;
6645 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
6646 * XXX reorder combinations by preference
6648 static struct mbuf
*
6649 key_getcomb_ipcomp(void)
6651 struct sadb_comb
*comb
;
6652 const struct ipcomp_algorithm
*algo
;
6655 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
6658 for (i
= 1; i
<= SADB_X_CALG_MAX
; i
++) {
6659 algo
= ipcomp_algorithm_lookup(i
);
6666 panic("assumption failed in key_getcomb_ipcomp");
6668 MGET(m
, M_DONTWAIT
, MT_DATA
);
6675 M_PREPEND(m
, l
, M_DONTWAIT
);
6679 comb
= mtod(m
, struct sadb_comb
*);
6680 bzero(comb
, sizeof(*comb
));
6681 key_getcomb_setlifetime(comb
);
6682 comb
->sadb_comb_encrypt
= i
;
6683 /* what should we set into sadb_comb_*_{min,max}bits? */
6690 * XXX no way to pass mode (transport/tunnel) to userland
6691 * XXX replay checking?
6692 * XXX sysctl interface to ipsec_{ah,esp}_keymin
6694 static struct mbuf
*
6696 const struct secasindex
*saidx
)
6698 struct sadb_prop
*prop
;
6700 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_prop
));
6703 switch (saidx
->proto
) {
6706 m
= key_getcomb_esp();
6710 m
= key_getcomb_ah();
6712 case IPPROTO_IPCOMP
:
6713 m
= key_getcomb_ipcomp();
6721 M_PREPEND(m
, l
, M_DONTWAIT
);
6726 for (n
= m
; n
; n
= n
->m_next
)
6729 prop
= mtod(m
, struct sadb_prop
*);
6730 bzero(prop
, sizeof(*prop
));
6731 prop
->sadb_prop_len
= PFKEY_UNIT64(totlen
);
6732 prop
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
6733 prop
->sadb_prop_replay
= 32; /* XXX */
6739 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
6741 * <base, SA, address(SD), (address(P)), x_policy,
6742 * (identity(SD),) (sensitivity,) proposal>
6743 * to KMD, and expect to receive
6744 * <base> with SADB_ACQUIRE if error occurred,
6746 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
6747 * from KMD by PF_KEY.
6749 * XXX x_policy is outside of RFC2367 (KAME extension).
6750 * XXX sensitivity is not supported.
6751 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
6752 * see comment for key_getcomb_ipcomp().
6756 * others: error number
6760 struct secasindex
*saidx
,
6761 struct secpolicy
*sp
)
6763 struct mbuf
*result
= NULL
, *m
;
6764 #ifndef IPSEC_NONBLOCK_ACQUIRE
6765 struct secacq
*newacq
;
6771 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
6775 panic("key_acquire: NULL pointer is passed.\n");
6776 if ((satype
= key_proto2satype(saidx
->proto
)) == 0)
6777 panic("key_acquire: invalid proto is passed.\n");
6779 #ifndef IPSEC_NONBLOCK_ACQUIRE
6781 * We never do anything about acquirng SA. There is anather
6782 * solution that kernel blocks to send SADB_ACQUIRE message until
6783 * getting something message from IKEd. In later case, to be
6784 * managed with ACQUIRING list.
6786 /* get a entry to check whether sending message or not. */
6787 lck_mtx_lock(sadb_mutex
);
6788 if ((newacq
= key_getacq(saidx
)) != NULL
) {
6789 if (key_blockacq_count
< newacq
->count
) {
6790 /* reset counter and do send message. */
6793 /* increment counter and do nothing. */
6795 lck_mtx_unlock(sadb_mutex
);
6799 /* make new entry for blocking to send SADB_ACQUIRE. */
6800 if ((newacq
= key_newacq(saidx
)) == NULL
) {
6801 lck_mtx_unlock(sadb_mutex
);
6805 /* add to acqtree */
6806 LIST_INSERT_HEAD(&acqtree
, newacq
, chain
);
6809 lck_mtx_unlock(sadb_mutex
);
6812 seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
6814 m
= key_setsadbmsg(SADB_ACQUIRE
, 0, satype
, seq
, 0, 0);
6821 /* set sadb_address for saidx's. */
6822 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
6823 (struct sockaddr
*)&saidx
->src
, FULLMASK
, IPSEC_ULPROTO_ANY
);
6830 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
6831 (struct sockaddr
*)&saidx
->dst
, FULLMASK
, IPSEC_ULPROTO_ANY
);
6838 /* XXX proxy address (optional) */
6840 /* set sadb_x_policy */
6842 m
= key_setsadbxpolicy(sp
->policy
, sp
->spidx
.dir
, sp
->id
);
6850 /* XXX identity (optional) */
6852 if (idexttype
&& fqdn
) {
6853 /* create identity extension (FQDN) */
6854 struct sadb_ident
*id
;
6857 fqdnlen
= strlen(fqdn
) + 1; /* +1 for terminating-NUL */
6858 id
= (struct sadb_ident
*)p
;
6859 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
6860 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
6861 id
->sadb_ident_exttype
= idexttype
;
6862 id
->sadb_ident_type
= SADB_IDENTTYPE_FQDN
;
6863 bcopy(fqdn
, id
+ 1, fqdnlen
);
6864 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(fqdnlen
);
6868 /* create identity extension (USERFQDN) */
6869 struct sadb_ident
*id
;
6873 /* +1 for terminating-NUL */
6874 userfqdnlen
= strlen(userfqdn
) + 1;
6877 id
= (struct sadb_ident
*)p
;
6878 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
6879 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
6880 id
->sadb_ident_exttype
= idexttype
;
6881 id
->sadb_ident_type
= SADB_IDENTTYPE_USERFQDN
;
6882 /* XXX is it correct? */
6883 if (curproc
&& curproc
->p_cred
)
6884 id
->sadb_ident_id
= curproc
->p_cred
->p_ruid
;
6885 if (userfqdn
&& userfqdnlen
)
6886 bcopy(userfqdn
, id
+ 1, userfqdnlen
);
6887 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(userfqdnlen
);
6891 /* XXX sensitivity (optional) */
6893 /* create proposal/combination extension */
6894 m
= key_getprop(saidx
);
6897 * spec conformant: always attach proposal/combination extension,
6898 * the problem is that we have no way to attach it for ipcomp,
6899 * due to the way sadb_comb is declared in RFC2367.
6908 * outside of spec; make proposal/combination extension optional.
6914 if ((result
->m_flags
& M_PKTHDR
) == 0) {
6919 if (result
->m_len
< sizeof(struct sadb_msg
)) {
6920 result
= m_pullup(result
, sizeof(struct sadb_msg
));
6921 if (result
== NULL
) {
6927 result
->m_pkthdr
.len
= 0;
6928 for (m
= result
; m
; m
= m
->m_next
)
6929 result
->m_pkthdr
.len
+= m
->m_len
;
6931 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
6932 PFKEY_UNIT64(result
->m_pkthdr
.len
);
6934 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
6942 #ifndef IPSEC_NONBLOCK_ACQUIRE
6943 static struct secacq
*
6945 struct secasindex
*saidx
)
6947 struct secacq
*newacq
;
6951 KMALLOC_NOWAIT(newacq
, struct secacq
*, sizeof(struct secacq
));
6952 if (newacq
== NULL
) {
6953 lck_mtx_unlock(sadb_mutex
);
6954 KMALLOC_WAIT(newacq
, struct secacq
*, sizeof(struct secacq
));
6955 lck_mtx_lock(sadb_mutex
);
6956 if (newacq
== NULL
) {
6957 ipseclog((LOG_DEBUG
, "key_newacq: No more memory.\n"));
6961 bzero(newacq
, sizeof(*newacq
));
6964 bcopy(saidx
, &newacq
->saidx
, sizeof(newacq
->saidx
));
6965 newacq
->seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
);
6967 newacq
->created
= tv
.tv_sec
;
6973 static struct secacq
*
6975 struct secasindex
*saidx
)
6979 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6981 LIST_FOREACH(acq
, &acqtree
, chain
) {
6982 if (key_cmpsaidx(saidx
, &acq
->saidx
, CMP_EXACTLY
))
6989 static struct secacq
*
6995 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6997 LIST_FOREACH(acq
, &acqtree
, chain
) {
6998 if (acq
->seq
== seq
)
7006 static struct secspacq
*
7008 struct secpolicyindex
*spidx
)
7010 struct secspacq
*acq
;
7014 KMALLOC_NOWAIT(acq
, struct secspacq
*, sizeof(struct secspacq
));
7016 lck_mtx_unlock(sadb_mutex
);
7017 KMALLOC_WAIT(acq
, struct secspacq
*, sizeof(struct secspacq
));
7018 lck_mtx_lock(sadb_mutex
);
7020 ipseclog((LOG_DEBUG
, "key_newspacq: No more memory.\n"));
7024 bzero(acq
, sizeof(*acq
));
7027 bcopy(spidx
, &acq
->spidx
, sizeof(acq
->spidx
));
7029 acq
->created
= tv
.tv_sec
;
7035 static struct secspacq
*
7037 struct secpolicyindex
*spidx
)
7039 struct secspacq
*acq
;
7041 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7043 LIST_FOREACH(acq
, &spacqtree
, chain
) {
7044 if (key_cmpspidx_exactly(spidx
, &acq
->spidx
))
7052 * SADB_ACQUIRE processing,
7053 * in first situation, is receiving
7055 * from the ikmpd, and clear sequence of its secasvar entry.
7057 * In second situation, is receiving
7058 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
7059 * from a user land process, and return
7060 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
7063 * m will always be freed.
7069 const struct sadb_msghdr
*mhp
)
7071 const struct sadb_address
*src0
, *dst0
;
7072 struct secasindex saidx
;
7073 struct secashead
*sah
;
7079 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7080 panic("key_acquire2: NULL pointer is passed.\n");
7083 * Error message from KMd.
7084 * We assume that if error was occurred in IKEd, the length of PFKEY
7085 * message is equal to the size of sadb_msg structure.
7086 * We do not raise error even if error occurred in this function.
7088 lck_mtx_lock(sadb_mutex
);
7090 if (mhp
->msg
->sadb_msg_len
== PFKEY_UNIT64(sizeof(struct sadb_msg
))) {
7091 #ifndef IPSEC_NONBLOCK_ACQUIRE
7095 /* check sequence number */
7096 if (mhp
->msg
->sadb_msg_seq
== 0) {
7097 lck_mtx_unlock(sadb_mutex
);
7098 ipseclog((LOG_DEBUG
, "key_acquire2: must specify sequence number.\n"));
7103 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) == NULL
) {
7105 * the specified larval SA is already gone, or we got
7106 * a bogus sequence number. we can silently ignore it.
7108 lck_mtx_unlock(sadb_mutex
);
7113 /* reset acq counter in order to deletion by timehander. */
7115 acq
->created
= tv
.tv_sec
;
7118 lck_mtx_unlock(sadb_mutex
);
7124 * This message is from user land.
7127 /* map satype to proto */
7128 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7129 lck_mtx_unlock(sadb_mutex
);
7130 ipseclog((LOG_DEBUG
, "key_acquire2: invalid satype is passed.\n"));
7131 return key_senderror(so
, m
, EINVAL
);
7134 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
7135 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
7136 mhp
->ext
[SADB_EXT_PROPOSAL
] == NULL
) {
7138 lck_mtx_unlock(sadb_mutex
);
7139 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
7140 return key_senderror(so
, m
, EINVAL
);
7142 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
7143 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
7144 mhp
->extlen
[SADB_EXT_PROPOSAL
] < sizeof(struct sadb_prop
)) {
7146 lck_mtx_unlock(sadb_mutex
);
7147 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
7148 return key_senderror(so
, m
, EINVAL
);
7151 src0
= (const struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
7152 dst0
= (const struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
7154 /* XXX boundary check against sa_len */
7156 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
7158 /* get a SA index */
7159 LIST_FOREACH(sah
, &sahtree
, chain
) {
7160 if (sah
->state
== SADB_SASTATE_DEAD
)
7162 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_MODE
| CMP_REQID
))
7166 lck_mtx_unlock(sadb_mutex
);
7167 ipseclog((LOG_DEBUG
, "key_acquire2: a SA exists already.\n"));
7168 return key_senderror(so
, m
, EEXIST
);
7170 lck_mtx_unlock(sadb_mutex
);
7171 error
= key_acquire(&saidx
, NULL
);
7173 ipseclog((LOG_DEBUG
, "key_acquire2: error %d returned "
7174 "from key_acquire.\n", mhp
->msg
->sadb_msg_errno
));
7175 return key_senderror(so
, m
, error
);
7178 return key_sendup_mbuf(so
, m
, KEY_SENDUP_REGISTERED
);
7182 * SADB_REGISTER processing.
7183 * If SATYPE_UNSPEC has been passed as satype, only return sadb_supported.
7186 * from the ikmpd, and register a socket to send PF_KEY messages,
7190 * If socket is detached, must free from regnode.
7192 * m will always be freed.
7198 const struct sadb_msghdr
*mhp
)
7200 struct secreg
*reg
, *newreg
= 0;
7203 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7204 panic("key_register: NULL pointer is passed.\n");
7206 /* check for invalid register message */
7207 if (mhp
->msg
->sadb_msg_satype
>= sizeof(regtree
)/sizeof(regtree
[0]))
7208 return key_senderror(so
, m
, EINVAL
);
7210 /* When SATYPE_UNSPEC is specified, only return sadb_supported. */
7211 if (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
)
7214 /* create regnode */
7215 KMALLOC_WAIT(newreg
, struct secreg
*, sizeof(*newreg
));
7216 if (newreg
== NULL
) {
7217 ipseclog((LOG_DEBUG
, "key_register: No more memory.\n"));
7218 return key_senderror(so
, m
, ENOBUFS
);
7220 bzero((caddr_t
)newreg
, sizeof(*newreg
));
7222 lck_mtx_lock(sadb_mutex
);
7223 /* check whether existing or not */
7224 LIST_FOREACH(reg
, ®tree
[mhp
->msg
->sadb_msg_satype
], chain
) {
7225 if (reg
->so
== so
) {
7226 lck_mtx_unlock(sadb_mutex
);
7227 ipseclog((LOG_DEBUG
, "key_register: socket exists already.\n"));
7229 return key_senderror(so
, m
, EEXIST
);
7235 ((struct keycb
*)sotorawcb(so
))->kp_registered
++;
7236 socket_unlock(so
, 1);
7238 /* add regnode to regtree. */
7239 LIST_INSERT_HEAD(®tree
[mhp
->msg
->sadb_msg_satype
], newreg
, chain
);
7240 lck_mtx_unlock(sadb_mutex
);
7244 struct sadb_msg
*newmsg
;
7245 struct sadb_supported
*sup
;
7246 u_int len
, alen
, elen
;
7249 struct sadb_alg
*alg
;
7251 /* create new sadb_msg to reply. */
7253 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
7254 if (ah_algorithm_lookup(i
))
7255 alen
+= sizeof(struct sadb_alg
);
7258 alen
+= sizeof(struct sadb_supported
);
7261 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
7262 if (esp_algorithm_lookup(i
))
7263 elen
+= sizeof(struct sadb_alg
);
7266 elen
+= sizeof(struct sadb_supported
);
7269 len
= sizeof(struct sadb_msg
) + alen
+ elen
;
7272 return key_senderror(so
, m
, ENOBUFS
);
7274 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
7276 MCLGET(n
, M_DONTWAIT
);
7277 if ((n
->m_flags
& M_EXT
) == 0) {
7283 return key_senderror(so
, m
, ENOBUFS
);
7285 n
->m_pkthdr
.len
= n
->m_len
= len
;
7289 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
7290 newmsg
= mtod(n
, struct sadb_msg
*);
7291 newmsg
->sadb_msg_errno
= 0;
7292 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
7293 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
7295 /* for authentication algorithm */
7297 sup
= (struct sadb_supported
*)(mtod(n
, caddr_t
) + off
);
7298 sup
->sadb_supported_len
= PFKEY_UNIT64(alen
);
7299 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
7300 off
+= PFKEY_ALIGN8(sizeof(*sup
));
7302 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
7303 const struct ah_algorithm
*aalgo
;
7305 aalgo
= ah_algorithm_lookup(i
);
7308 alg
= (struct sadb_alg
*)(mtod(n
, caddr_t
) + off
);
7309 alg
->sadb_alg_id
= i
;
7310 alg
->sadb_alg_ivlen
= 0;
7311 alg
->sadb_alg_minbits
= aalgo
->keymin
;
7312 alg
->sadb_alg_maxbits
= aalgo
->keymax
;
7313 off
+= PFKEY_ALIGN8(sizeof(*alg
));
7318 /* for encryption algorithm */
7320 sup
= (struct sadb_supported
*)(mtod(n
, caddr_t
) + off
);
7321 sup
->sadb_supported_len
= PFKEY_UNIT64(elen
);
7322 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
7323 off
+= PFKEY_ALIGN8(sizeof(*sup
));
7325 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
7326 const struct esp_algorithm
*ealgo
;
7328 ealgo
= esp_algorithm_lookup(i
);
7331 alg
= (struct sadb_alg
*)(mtod(n
, caddr_t
) + off
);
7332 alg
->sadb_alg_id
= i
;
7333 if (ealgo
&& ealgo
->ivlen
) {
7335 * give NULL to get the value preferred by
7336 * algorithm XXX SADB_X_EXT_DERIV ?
7338 alg
->sadb_alg_ivlen
=
7339 (*ealgo
->ivlen
)(ealgo
, NULL
);
7341 alg
->sadb_alg_ivlen
= 0;
7342 alg
->sadb_alg_minbits
= ealgo
->keymin
;
7343 alg
->sadb_alg_maxbits
= ealgo
->keymax
;
7344 off
+= PFKEY_ALIGN8(sizeof(struct sadb_alg
));
7351 panic("length assumption failed in key_register");
7355 return key_sendup_mbuf(so
, n
, KEY_SENDUP_REGISTERED
);
7360 * free secreg entry registered.
7361 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
7372 panic("key_freereg: NULL pointer is passed.\n");
7375 * check whether existing or not.
7376 * check all type of SA, because there is a potential that
7377 * one socket is registered to multiple type of SA.
7379 lck_mtx_lock(sadb_mutex
);
7380 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
7381 LIST_FOREACH(reg
, ®tree
[i
], chain
) {
7383 && __LIST_CHAINED(reg
)) {
7384 LIST_REMOVE(reg
, chain
);
7390 lck_mtx_unlock(sadb_mutex
);
7395 * SADB_EXPIRE processing
7397 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
7399 * NOTE: We send only soft lifetime extension.
7402 * others : error number
7406 struct secasvar
*sav
)
7409 struct mbuf
*result
= NULL
, *m
;
7412 struct sadb_lifetime
*lt
;
7414 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7418 panic("key_expire: NULL pointer is passed.\n");
7419 if (sav
->sah
== NULL
)
7420 panic("key_expire: Why was SA index in SA NULL.\n");
7421 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0)
7422 panic("key_expire: invalid proto is passed.\n");
7424 /* set msg header */
7425 m
= key_setsadbmsg(SADB_EXPIRE
, 0, satype
, sav
->seq
, 0, sav
->refcnt
);
7432 /* create SA extension */
7433 m
= key_setsadbsa(sav
);
7440 /* create SA extension */
7441 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
7442 sav
->replay
? sav
->replay
->count
: 0,
7443 sav
->sah
->saidx
.reqid
);
7450 /* create lifetime extension (current and soft) */
7451 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
7452 m
= key_alloc_mbuf(len
);
7453 if (!m
|| m
->m_next
) { /*XXX*/
7459 bzero(mtod(m
, caddr_t
), len
);
7460 lt
= mtod(m
, struct sadb_lifetime
*);
7461 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
7462 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
7463 lt
->sadb_lifetime_allocations
= sav
->lft_c
->sadb_lifetime_allocations
;
7464 lt
->sadb_lifetime_bytes
= sav
->lft_c
->sadb_lifetime_bytes
;
7465 lt
->sadb_lifetime_addtime
= sav
->lft_c
->sadb_lifetime_addtime
;
7466 lt
->sadb_lifetime_usetime
= sav
->lft_c
->sadb_lifetime_usetime
;
7467 lt
= (struct sadb_lifetime
*)(mtod(m
, caddr_t
) + len
/ 2);
7468 bcopy(sav
->lft_s
, lt
, sizeof(*lt
));
7471 /* set sadb_address for source */
7472 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
7473 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
7474 FULLMASK
, IPSEC_ULPROTO_ANY
);
7481 /* set sadb_address for destination */
7482 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
7483 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
7484 FULLMASK
, IPSEC_ULPROTO_ANY
);
7491 if ((result
->m_flags
& M_PKTHDR
) == 0) {
7496 if (result
->m_len
< sizeof(struct sadb_msg
)) {
7497 result
= m_pullup(result
, sizeof(struct sadb_msg
));
7498 if (result
== NULL
) {
7504 result
->m_pkthdr
.len
= 0;
7505 for (m
= result
; m
; m
= m
->m_next
)
7506 result
->m_pkthdr
.len
+= m
->m_len
;
7508 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
7509 PFKEY_UNIT64(result
->m_pkthdr
.len
);
7511 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
7520 * SADB_FLUSH processing
7523 * from the ikmpd, and free all entries in secastree.
7527 * NOTE: to do is only marking SADB_SASTATE_DEAD.
7529 * m will always be freed.
7535 const struct sadb_msghdr
*mhp
)
7537 struct sadb_msg
*newmsg
;
7538 struct secashead
*sah
, *nextsah
;
7539 struct secasvar
*sav
, *nextsav
;
7545 if (so
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7546 panic("key_flush: NULL pointer is passed.\n");
7548 /* map satype to proto */
7549 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7550 ipseclog((LOG_DEBUG
, "key_flush: invalid satype is passed.\n"));
7551 return key_senderror(so
, m
, EINVAL
);
7554 lck_mtx_lock(sadb_mutex
);
7556 /* no SATYPE specified, i.e. flushing all SA. */
7557 for (sah
= LIST_FIRST(&sahtree
);
7560 nextsah
= LIST_NEXT(sah
, chain
);
7562 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
7563 && proto
!= sah
->saidx
.proto
)
7567 stateidx
< _ARRAYLEN(saorder_state_alive
);
7569 state
= saorder_state_any
[stateidx
];
7570 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
7574 nextsav
= LIST_NEXT(sav
, chain
);
7576 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
7577 key_freesav(sav
, KEY_SADB_LOCKED
);
7581 sah
->state
= SADB_SASTATE_DEAD
;
7583 lck_mtx_unlock(sadb_mutex
);
7585 if (m
->m_len
< sizeof(struct sadb_msg
) ||
7586 sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
7587 ipseclog((LOG_DEBUG
, "key_flush: No more memory.\n"));
7588 return key_senderror(so
, m
, ENOBUFS
);
7594 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct sadb_msg
);
7595 newmsg
= mtod(m
, struct sadb_msg
*);
7596 newmsg
->sadb_msg_errno
= 0;
7597 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
7599 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
7603 * SADB_DUMP processing
7604 * dump all entries including status of DEAD in SAD.
7607 * from the ikmpd, and dump all secasvar leaves
7612 * m will always be freed.
7615 struct sav_dump_elem
{
7616 struct secasvar
*sav
;
7624 const struct sadb_msghdr
*mhp
)
7626 struct secashead
*sah
;
7627 struct secasvar
*sav
;
7628 struct sav_dump_elem
*savbuf
= NULL
, *elem_ptr
;
7633 int cnt
= 0, cnt2
, bufcount
;
7637 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7640 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7641 panic("key_dump: NULL pointer is passed.\n");
7643 /* map satype to proto */
7644 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
7645 ipseclog((LOG_DEBUG
, "key_dump: invalid satype is passed.\n"));
7646 return key_senderror(so
, m
, EINVAL
);
7649 if ((bufcount
= ipsec_sav_count
) <= 0) {
7653 bufcount
+= 512; /* extra */
7654 KMALLOC_WAIT(savbuf
, struct sav_dump_elem
*, bufcount
* sizeof(struct sav_dump_elem
));
7655 if (savbuf
== NULL
) {
7656 ipseclog((LOG_DEBUG
, "key_dump: No more memory.\n"));
7661 /* count sav entries to be sent to the userland. */
7662 lck_mtx_lock(sadb_mutex
);
7664 LIST_FOREACH(sah
, &sahtree
, chain
) {
7665 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
7666 && proto
!= sah
->saidx
.proto
)
7669 /* map proto to satype */
7670 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
7671 lck_mtx_unlock(sadb_mutex
);
7672 ipseclog((LOG_DEBUG
, "key_dump: there was invalid proto in SAD.\n"));
7678 stateidx
< _ARRAYLEN(saorder_state_any
);
7680 state
= saorder_state_any
[stateidx
];
7681 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
7682 if (cnt
== bufcount
)
7683 break; /* out of buffer space */
7684 elem_ptr
->sav
= sav
;
7685 elem_ptr
->satype
= satype
;
7692 lck_mtx_unlock(sadb_mutex
);
7699 /* send this to the userland, one at a time. */
7703 n
= key_setdumpsa(elem_ptr
->sav
, SADB_DUMP
, elem_ptr
->satype
,
7704 --cnt2
, mhp
->msg
->sadb_msg_pid
);
7711 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
7719 lck_mtx_lock(sadb_mutex
);
7721 key_freesav((elem_ptr
++)->sav
, KEY_SADB_LOCKED
);
7722 lck_mtx_unlock(sadb_mutex
);
7728 return key_senderror(so
, m
, error
);
7735 * SADB_X_PROMISC processing
7737 * m will always be freed.
7743 const struct sadb_msghdr
*mhp
)
7748 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
7749 panic("key_promisc: NULL pointer is passed.\n");
7751 olen
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
7753 if (olen
< sizeof(struct sadb_msg
)) {
7755 return key_senderror(so
, m
, EINVAL
);
7760 } else if (olen
== sizeof(struct sadb_msg
)) {
7761 /* enable/disable promisc mode */
7765 if ((kp
= (struct keycb
*)sotorawcb(so
)) == NULL
)
7766 return key_senderror(so
, m
, EINVAL
);
7767 mhp
->msg
->sadb_msg_errno
= 0;
7768 switch (mhp
->msg
->sadb_msg_satype
) {
7771 kp
->kp_promisc
= mhp
->msg
->sadb_msg_satype
;
7774 socket_unlock(so
, 1);
7775 return key_senderror(so
, m
, EINVAL
);
7777 socket_unlock(so
, 1);
7779 /* send the original message back to everyone */
7780 mhp
->msg
->sadb_msg_errno
= 0;
7781 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
7783 /* send packet as is */
7785 m_adj(m
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)));
7787 /* TODO: if sadb_msg_seq is specified, send to specific pid */
7788 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
7792 static int (*key_typesw
[])(struct socket
*, struct mbuf
*,
7793 const struct sadb_msghdr
*) = {
7794 NULL
, /* SADB_RESERVED */
7795 key_getspi
, /* SADB_GETSPI */
7796 key_update
, /* SADB_UPDATE */
7797 key_add
, /* SADB_ADD */
7798 key_delete
, /* SADB_DELETE */
7799 key_get
, /* SADB_GET */
7800 key_acquire2
, /* SADB_ACQUIRE */
7801 key_register
, /* SADB_REGISTER */
7802 NULL
, /* SADB_EXPIRE */
7803 key_flush
, /* SADB_FLUSH */
7804 key_dump
, /* SADB_DUMP */
7805 key_promisc
, /* SADB_X_PROMISC */
7806 NULL
, /* SADB_X_PCHANGE */
7807 key_spdadd
, /* SADB_X_SPDUPDATE */
7808 key_spdadd
, /* SADB_X_SPDADD */
7809 key_spddelete
, /* SADB_X_SPDDELETE */
7810 key_spdget
, /* SADB_X_SPDGET */
7811 NULL
, /* SADB_X_SPDACQUIRE */
7812 key_spddump
, /* SADB_X_SPDDUMP */
7813 key_spdflush
, /* SADB_X_SPDFLUSH */
7814 key_spdadd
, /* SADB_X_SPDSETIDX */
7815 NULL
, /* SADB_X_SPDEXPIRE */
7816 key_spddelete2
, /* SADB_X_SPDDELETE2 */
7817 key_getsastat
, /* SADB_GETSASTAT */
7821 * parse sadb_msg buffer to process PFKEYv2,
7822 * and create a data to response if needed.
7823 * I think to be dealed with mbuf directly.
7825 * msgp : pointer to pointer to a received buffer pulluped.
7826 * This is rewrited to response.
7827 * so : pointer to socket.
7829 * length for buffer to send to user process.
7836 struct sadb_msg
*msg
;
7837 struct sadb_msghdr mh
;
7842 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
7845 if (m
== NULL
|| so
== NULL
)
7846 panic("key_parse: NULL pointer is passed.\n");
7848 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
7849 KEYDEBUG(KEYDEBUG_KEY_DUMP
,
7850 ipseclog((LOG_DEBUG
, "key_parse: passed sadb_msg\n"));
7854 if (m
->m_len
< sizeof(struct sadb_msg
)) {
7855 m
= m_pullup(m
, sizeof(struct sadb_msg
));
7859 msg
= mtod(m
, struct sadb_msg
*);
7860 orglen
= PFKEY_UNUNIT64(msg
->sadb_msg_len
);
7861 target
= KEY_SENDUP_ONE
;
7863 if ((m
->m_flags
& M_PKTHDR
) == 0 ||
7864 m
->m_pkthdr
.len
!= m
->m_pkthdr
.len
) {
7865 ipseclog((LOG_DEBUG
, "key_parse: invalid message length.\n"));
7866 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
7871 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
7872 ipseclog((LOG_DEBUG
,
7873 "key_parse: PF_KEY version %u is mismatched.\n",
7874 msg
->sadb_msg_version
));
7875 PFKEY_STAT_INCREMENT(pfkeystat
.out_invver
);
7880 if (msg
->sadb_msg_type
> SADB_MAX
) {
7881 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
7882 msg
->sadb_msg_type
));
7883 PFKEY_STAT_INCREMENT(pfkeystat
.out_invmsgtype
);
7888 /* for old-fashioned code - should be nuked */
7889 if (m
->m_pkthdr
.len
> MCLBYTES
) {
7896 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
7897 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
7898 MCLGET(n
, M_DONTWAIT
);
7899 if ((n
->m_flags
& M_EXT
) == 0) {
7908 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
7909 n
->m_pkthdr
.len
= n
->m_len
= m
->m_pkthdr
.len
;
7915 /* align the mbuf chain so that extensions are in contiguous region. */
7916 error
= key_align(m
, &mh
);
7920 if (m
->m_next
) { /*XXX*/
7928 switch (msg
->sadb_msg_satype
) {
7929 case SADB_SATYPE_UNSPEC
:
7930 switch (msg
->sadb_msg_type
) {
7938 ipseclog((LOG_DEBUG
, "key_parse: must specify satype "
7939 "when msg type=%u.\n", msg
->sadb_msg_type
));
7940 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
7945 case SADB_SATYPE_AH
:
7946 case SADB_SATYPE_ESP
:
7947 case SADB_X_SATYPE_IPCOMP
:
7948 switch (msg
->sadb_msg_type
) {
7950 case SADB_X_SPDDELETE
:
7952 case SADB_X_SPDDUMP
:
7953 case SADB_X_SPDFLUSH
:
7954 case SADB_X_SPDSETIDX
:
7955 case SADB_X_SPDUPDATE
:
7956 case SADB_X_SPDDELETE2
:
7957 ipseclog((LOG_DEBUG
, "key_parse: illegal satype=%u\n",
7958 msg
->sadb_msg_type
));
7959 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
7964 case SADB_SATYPE_RSVP
:
7965 case SADB_SATYPE_OSPFV2
:
7966 case SADB_SATYPE_RIPV2
:
7967 case SADB_SATYPE_MIP
:
7968 ipseclog((LOG_DEBUG
, "key_parse: type %u isn't supported.\n",
7969 msg
->sadb_msg_satype
));
7970 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
7973 case 1: /* XXX: What does it do? */
7974 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
7978 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
7979 msg
->sadb_msg_satype
));
7980 PFKEY_STAT_INCREMENT(pfkeystat
.out_invsatype
);
7985 /* check field of upper layer protocol and address family */
7986 if (mh
.ext
[SADB_EXT_ADDRESS_SRC
] != NULL
7987 && mh
.ext
[SADB_EXT_ADDRESS_DST
] != NULL
) {
7988 struct sadb_address
*src0
, *dst0
;
7991 src0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_SRC
]);
7992 dst0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_DST
]);
7994 /* check upper layer protocol */
7995 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
7996 ipseclog((LOG_DEBUG
, "key_parse: upper layer protocol mismatched.\n"));
7997 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
8003 if (PFKEY_ADDR_SADDR(src0
)->sa_family
!=
8004 PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
8005 ipseclog((LOG_DEBUG
, "key_parse: address family mismatched.\n"));
8006 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
8010 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
8011 PFKEY_ADDR_SADDR(dst0
)->sa_len
) {
8012 ipseclog((LOG_DEBUG
,
8013 "key_parse: address struct size mismatched.\n"));
8014 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
8019 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
8021 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
8022 sizeof(struct sockaddr_in
)) {
8023 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
8029 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
8030 sizeof(struct sockaddr_in6
)) {
8031 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
8037 ipseclog((LOG_DEBUG
,
8038 "key_parse: unsupported address family.\n"));
8039 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
8040 error
= EAFNOSUPPORT
;
8044 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
8046 plen
= sizeof(struct in_addr
) << 3;
8049 plen
= sizeof(struct in6_addr
) << 3;
8052 plen
= 0; /*fool gcc*/
8056 /* check max prefix length */
8057 if (src0
->sadb_address_prefixlen
> plen
||
8058 dst0
->sadb_address_prefixlen
> plen
) {
8059 ipseclog((LOG_DEBUG
,
8060 "key_parse: illegal prefixlen.\n"));
8061 PFKEY_STAT_INCREMENT(pfkeystat
.out_invaddr
);
8067 * prefixlen == 0 is valid because there can be a case when
8068 * all addresses are matched.
8072 if (msg
->sadb_msg_type
>= sizeof(key_typesw
)/sizeof(key_typesw
[0]) ||
8073 key_typesw
[msg
->sadb_msg_type
] == NULL
) {
8074 PFKEY_STAT_INCREMENT(pfkeystat
.out_invmsgtype
);
8079 return (*key_typesw
[msg
->sadb_msg_type
])(so
, m
, &mh
);
8082 msg
->sadb_msg_errno
= error
;
8083 return key_sendup_mbuf(so
, m
, target
);
8092 struct sadb_msg
*msg
;
8094 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
8096 if (m
->m_len
< sizeof(struct sadb_msg
))
8097 panic("invalid mbuf passed to key_senderror");
8099 msg
= mtod(m
, struct sadb_msg
*);
8100 msg
->sadb_msg_errno
= code
;
8101 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
8105 * set the pointer to each header into message buffer.
8106 * m will be freed on error.
8107 * XXX larger-than-MCLBYTES extension?
8112 struct sadb_msghdr
*mhp
)
8115 struct sadb_ext
*ext
;
8121 if (m
== NULL
|| mhp
== NULL
)
8122 panic("key_align: NULL pointer is passed.\n");
8123 if (m
->m_len
< sizeof(struct sadb_msg
))
8124 panic("invalid mbuf passed to key_align");
8127 bzero(mhp
, sizeof(*mhp
));
8129 mhp
->msg
= mtod(m
, struct sadb_msg
*);
8130 mhp
->ext
[0] = (struct sadb_ext
*)mhp
->msg
; /*XXX backward compat */
8132 end
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
8133 extlen
= end
; /*just in case extlen is not updated*/
8134 for (off
= sizeof(struct sadb_msg
); off
< end
; off
+= extlen
) {
8135 n
= m_pulldown(m
, off
, sizeof(struct sadb_ext
), &toff
);
8137 /* m is already freed */
8140 ext
= (struct sadb_ext
*)(mtod(n
, caddr_t
) + toff
);
8143 switch (ext
->sadb_ext_type
) {
8145 case SADB_EXT_ADDRESS_SRC
:
8146 case SADB_EXT_ADDRESS_DST
:
8147 case SADB_EXT_ADDRESS_PROXY
:
8148 case SADB_EXT_LIFETIME_CURRENT
:
8149 case SADB_EXT_LIFETIME_HARD
:
8150 case SADB_EXT_LIFETIME_SOFT
:
8151 case SADB_EXT_KEY_AUTH
:
8152 case SADB_EXT_KEY_ENCRYPT
:
8153 case SADB_EXT_IDENTITY_SRC
:
8154 case SADB_EXT_IDENTITY_DST
:
8155 case SADB_EXT_SENSITIVITY
:
8156 case SADB_EXT_PROPOSAL
:
8157 case SADB_EXT_SUPPORTED_AUTH
:
8158 case SADB_EXT_SUPPORTED_ENCRYPT
:
8159 case SADB_EXT_SPIRANGE
:
8160 case SADB_X_EXT_POLICY
:
8161 case SADB_X_EXT_SA2
:
8162 case SADB_EXT_SESSION_ID
:
8163 case SADB_EXT_SASTAT
:
8164 /* duplicate check */
8166 * XXX Are there duplication payloads of either
8167 * KEY_AUTH or KEY_ENCRYPT ?
8169 if (mhp
->ext
[ext
->sadb_ext_type
] != NULL
) {
8170 ipseclog((LOG_DEBUG
,
8171 "key_align: duplicate ext_type %u "
8172 "is passed.\n", ext
->sadb_ext_type
));
8174 PFKEY_STAT_INCREMENT(pfkeystat
.out_dupext
);
8179 ipseclog((LOG_DEBUG
,
8180 "key_align: invalid ext_type %u is passed.\n",
8181 ext
->sadb_ext_type
));
8183 PFKEY_STAT_INCREMENT(pfkeystat
.out_invexttype
);
8187 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
8189 if (key_validate_ext(ext
, extlen
)) {
8191 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
8195 n
= m_pulldown(m
, off
, extlen
, &toff
);
8197 /* m is already freed */
8200 ext
= (struct sadb_ext
*)(mtod(n
, caddr_t
) + toff
);
8202 mhp
->ext
[ext
->sadb_ext_type
] = ext
;
8203 mhp
->extoff
[ext
->sadb_ext_type
] = off
;
8204 mhp
->extlen
[ext
->sadb_ext_type
] = extlen
;
8209 PFKEY_STAT_INCREMENT(pfkeystat
.out_invlen
);
8218 const struct sadb_ext
*ext
,
8221 struct sockaddr
*sa
;
8222 enum { NONE
, ADDR
} checktype
= NONE
;
8224 const int sal
= offsetof(struct sockaddr
, sa_len
) + sizeof(sa
->sa_len
);
8226 if (len
!= PFKEY_UNUNIT64(ext
->sadb_ext_len
))
8229 /* if it does not match minimum/maximum length, bail */
8230 if (ext
->sadb_ext_type
>= sizeof(minsize
) / sizeof(minsize
[0]) ||
8231 ext
->sadb_ext_type
>= sizeof(maxsize
) / sizeof(maxsize
[0]))
8233 if (!minsize
[ext
->sadb_ext_type
] || len
< minsize
[ext
->sadb_ext_type
])
8235 if (maxsize
[ext
->sadb_ext_type
] && len
> maxsize
[ext
->sadb_ext_type
])
8238 /* more checks based on sadb_ext_type XXX need more */
8239 switch (ext
->sadb_ext_type
) {
8240 case SADB_EXT_ADDRESS_SRC
:
8241 case SADB_EXT_ADDRESS_DST
:
8242 case SADB_EXT_ADDRESS_PROXY
:
8243 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_address
));
8246 case SADB_EXT_IDENTITY_SRC
:
8247 case SADB_EXT_IDENTITY_DST
:
8248 if (((const struct sadb_ident
*)ext
)->sadb_ident_type
==
8249 SADB_X_IDENTTYPE_ADDR
) {
8250 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_ident
));
8260 switch (checktype
) {
8264 sa
= (struct sockaddr
*)((caddr_t
)(uintptr_t)ext
+ baselen
);
8266 if (len
< baselen
+ sal
)
8268 if (baselen
+ PFKEY_ALIGN8(sa
->sa_len
) != len
)
8277 key_domain_init(void)
8283 * XXX: maybe This function is called after INBOUND IPsec processing.
8285 * Special check for tunnel-mode packets.
8286 * We must make some checks for consistency between inner and outer IP header.
8288 * xxx more checks to be provided
8291 key_checktunnelsanity(
8292 struct secasvar
*sav
,
8293 __unused u_int family
,
8294 __unused caddr_t src
,
8295 __unused caddr_t dst
)
8299 if (sav
->sah
== NULL
)
8300 panic("sav->sah == NULL at key_checktunnelsanity");
8302 /* XXX: check inner IP header */
8307 /* record data transfer on SA, and update timestamps */
8310 struct secasvar
*sav
,
8316 panic("key_sa_recordxfer called with sav == NULL");
8318 panic("key_sa_recordxfer called with m == NULL");
8322 lck_mtx_lock(sadb_mutex
);
8324 * XXX Currently, there is a difference of bytes size
8325 * between inbound and outbound processing.
8327 sav
->lft_c
->sadb_lifetime_bytes
+= m
->m_pkthdr
.len
;
8328 /* to check bytes lifetime is done in key_timehandler(). */
8331 * We use the number of packets as the unit of
8332 * sadb_lifetime_allocations. We increment the variable
8333 * whenever {esp,ah}_{in,out}put is called.
8335 sav
->lft_c
->sadb_lifetime_allocations
++;
8336 /* XXX check for expires? */
8339 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
8340 * in seconds. HARD and SOFT lifetime are measured by the time
8341 * difference (again in seconds) from sadb_lifetime_usetime.
8345 * -----+-----+--------+---> t
8346 * <--------------> HARD
8352 sav
->lft_c
->sadb_lifetime_usetime
= tv
.tv_sec
;
8353 /* XXX check for expires? */
8355 lck_mtx_unlock(sadb_mutex
);
8363 struct sockaddr
*dst
)
8365 struct secashead
*sah
;
8368 lck_mtx_lock(sadb_mutex
);
8369 LIST_FOREACH(sah
, &sahtree
, chain
) {
8370 ro
= &sah
->sa_route
;
8371 if (ro
->ro_rt
&& dst
->sa_len
== ro
->ro_dst
.sa_len
8372 && bcmp(dst
, &ro
->ro_dst
, dst
->sa_len
) == 0) {
8374 ro
->ro_rt
= (struct rtentry
*)NULL
;
8377 lck_mtx_unlock(sadb_mutex
);
8384 struct secasvar
*sav
,
8389 panic("key_sa_chgstate called with sav == NULL");
8391 if (sav
->state
== state
)
8394 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
8396 if (__LIST_CHAINED(sav
))
8397 LIST_REMOVE(sav
, chain
);
8400 LIST_INSERT_HEAD(&sav
->sah
->savtree
[state
], sav
, chain
);
8406 struct secasvar
*sav
)
8408 lck_mtx_lock(sadb_mutex
);
8410 panic("key_sa_stir_iv called with sav == NULL");
8411 key_randomfill(sav
->iv
, sav
->ivlen
);
8412 lck_mtx_unlock(sadb_mutex
);
8416 static struct mbuf
*
8420 struct mbuf
*m
= NULL
, *n
;
8425 MGET(n
, M_DONTWAIT
, MT_DATA
);
8426 if (n
&& len
> MLEN
)
8427 MCLGET(n
, M_DONTWAIT
);
8435 n
->m_len
= M_TRAILINGSPACE(n
);
8436 /* use the bottom of mbuf, hoping we can prepend afterwards */
8437 if (n
->m_len
> len
) {
8438 t
= (n
->m_len
- len
) & ~(sizeof(long) - 1);
8454 static struct mbuf
*
8455 key_setdumpsastats (u_int32_t dir
,
8456 struct sastat
*stats
,
8457 u_int32_t max_stats
,
8458 u_int64_t session_ids
[],
8462 struct mbuf
*result
= NULL
, *m
= NULL
;
8464 m
= key_setsadbmsg(SADB_GETSASTAT
, 0, 0, seq
, pid
, 0);
8470 m
= key_setsadbsession_id(session_ids
);
8476 m
= key_setsadbsastat(dir
,
8484 if ((result
->m_flags
& M_PKTHDR
) == 0) {
8488 if (result
->m_len
< sizeof(struct sadb_msg
)) {
8489 result
= m_pullup(result
, sizeof(struct sadb_msg
));
8490 if (result
== NULL
) {
8495 result
->m_pkthdr
.len
= 0;
8496 for (m
= result
; m
; m
= m
->m_next
) {
8497 result
->m_pkthdr
.len
+= m
->m_len
;
8500 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
8501 PFKEY_UNIT64(result
->m_pkthdr
.len
);
8513 * SADB_GETSASTAT processing
8514 * dump all stats for matching entries in SAD.
8516 * m will always be freed.
8520 key_getsastat (struct socket
*so
,
8522 const struct sadb_msghdr
*mhp
)
8524 struct sadb_session_id
*session_id
;
8525 u_int32_t bufsize
, arg_count
, res_count
;
8526 struct sadb_sastat
*sa_stats_arg
;
8527 struct sastat
*sa_stats_sav
= NULL
;
8532 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
8533 panic("%s: NULL pointer is passed.\n", __FUNCTION__
);
8535 if (mhp
->ext
[SADB_EXT_SESSION_ID
] == NULL
) {
8536 printf("%s: invalid message is passed. missing session-id.\n", __FUNCTION__
);
8537 return key_senderror(so
, m
, EINVAL
);
8539 if (mhp
->extlen
[SADB_EXT_SESSION_ID
] < sizeof(struct sadb_session_id
)) {
8540 printf("%s: invalid message is passed. short session-id.\n", __FUNCTION__
);
8541 return key_senderror(so
, m
, EINVAL
);
8543 if (mhp
->ext
[SADB_EXT_SASTAT
] == NULL
) {
8544 printf("%s: invalid message is passed. missing stat args.\n", __FUNCTION__
);
8545 return key_senderror(so
, m
, EINVAL
);
8547 if (mhp
->extlen
[SADB_EXT_SASTAT
] < sizeof(*sa_stats_arg
)) {
8548 printf("%s: invalid message is passed. short stat args.\n", __FUNCTION__
);
8549 return key_senderror(so
, m
, EINVAL
);
8552 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
8554 // exit early if there are no active SAs
8555 if (ipsec_sav_count
<= 0) {
8556 printf("%s: No active SAs.\n", __FUNCTION__
);
8560 bufsize
= (ipsec_sav_count
+ 1) * sizeof(*sa_stats_sav
);
8562 KMALLOC_WAIT(sa_stats_sav
, __typeof__(sa_stats_sav
), bufsize
);
8563 if (sa_stats_sav
== NULL
) {
8564 printf("%s: No more memory.\n", __FUNCTION__
);
8568 bzero(sa_stats_sav
, bufsize
);
8570 sa_stats_arg
= (__typeof__(sa_stats_arg
))mhp
->ext
[SADB_EXT_SASTAT
];
8571 arg_count
= sa_stats_arg
->sadb_sastat_list_len
;
8572 // exit early if there are no requested SAs
8573 if (arg_count
== 0) {
8574 printf("%s: No SAs requested.\n", __FUNCTION__
);
8580 if (key_getsastatbyspi((struct sastat
*)(sa_stats_arg
+ 1),
8584 printf("%s: Error finding SAs.\n", __FUNCTION__
);
8589 printf("%s: No SAs found.\n", __FUNCTION__
);
8594 session_id
= (__typeof__(session_id
))mhp
->ext
[SADB_EXT_SESSION_ID
];
8596 /* send this to the userland. */
8597 n
= key_setdumpsastats(sa_stats_arg
->sadb_sastat_dir
,
8600 session_id
->sadb_session_id_v
,
8601 mhp
->msg
->sadb_msg_seq
,
8602 mhp
->msg
->sadb_msg_pid
);
8604 printf("%s: No bufs to dump stats.\n", __FUNCTION__
);
8609 key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
8612 KFREE(sa_stats_sav
);
8616 return key_senderror(so
, m
, error
);
8623 key_update_natt_keepalive_timestamp (struct secasvar
*sav_sent
,
8624 struct secasvar
*sav_update
)
8626 struct secasindex saidx_swap_sent_addr
;
8628 // exit early if two SAs are identical, or if sav_update is current
8629 if (sav_sent
== sav_update
||
8630 sav_update
->natt_last_activity
== natt_now
) {
8634 // assuming that (sav_update->remote_ike_port != 0 && (esp_udp_encap_port & 0xFFFF) != 0)
8636 bzero(&saidx_swap_sent_addr
, sizeof(saidx_swap_sent_addr
));
8637 memcpy(&saidx_swap_sent_addr
.src
, &sav_sent
->sah
->saidx
.dst
, sizeof(saidx_swap_sent_addr
.src
));
8638 memcpy(&saidx_swap_sent_addr
.dst
, &sav_sent
->sah
->saidx
.src
, sizeof(saidx_swap_sent_addr
.dst
));
8639 saidx_swap_sent_addr
.proto
= sav_sent
->sah
->saidx
.proto
;
8640 saidx_swap_sent_addr
.mode
= sav_sent
->sah
->saidx
.mode
;
8641 // we ignore reqid for split-tunnel setups
8643 if (key_cmpsaidx(&sav_sent
->sah
->saidx
, &sav_update
->sah
->saidx
, CMP_MODE
| CMP_PORT
) ||
8644 key_cmpsaidx(&saidx_swap_sent_addr
, &sav_update
->sah
->saidx
, CMP_MODE
| CMP_PORT
)) {
8645 sav_update
->natt_last_activity
= natt_now
;