1 /* $FreeBSD: src/sys/netkey/key.c,v 1.16.2.13 2002/07/24 18:17:40 ume Exp $ */
2 /* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * This code is referd to RFC 2367
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/domain.h>
43 #include <sys/protosw.h>
44 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/sysctl.h>
48 #include <sys/errno.h>
50 #include <sys/queue.h>
51 #include <sys/syslog.h>
53 #include <kern/locks.h>
56 #include <net/route.h>
57 #include <net/raw_cb.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
62 #include <netinet/in_var.h>
65 #include <netinet/ip6.h>
66 #include <netinet6/in6_var.h>
67 #include <netinet6/ip6_var.h>
71 #include <netinet/in_pcb.h>
74 #include <netinet6/in6_pcb.h>
77 #include <net/pfkeyv2.h>
78 #include <netkey/keydb.h>
79 #include <netkey/key.h>
80 #include <netkey/keysock.h>
81 #include <netkey/key_debug.h>
85 #include <netinet6/ipsec.h>
87 #include <netinet6/ipsec6.h>
89 #include <netinet6/ah.h>
91 #include <netinet6/ah6.h>
94 #include <netinet6/esp.h>
96 #include <netinet6/esp6.h>
99 #include <netinet6/ipcomp.h>
101 #include <netinet6/ipcomp6.h>
106 #include <sys/random.h>
108 #include <net/net_osdep.h>
111 #define satosin(s) ((struct sockaddr_in *)s)
114 #define FULLMASK 0xff
116 lck_grp_t
*sadb_mutex_grp
;
117 lck_grp_attr_t
*sadb_mutex_grp_attr
;
118 lck_attr_t
*sadb_mutex_attr
;
119 lck_mtx_t
*sadb_mutex
;
120 extern lck_mtx_t
*nd6_mutex
;
123 * Note on SA reference counting:
124 * - SAs that are not in DEAD state will have (total external reference + 1)
125 * following value in reference count field. they cannot be freed and are
126 * referenced from SA header.
127 * - SAs that are in DEAD state will have (total external reference)
128 * in reference count field. they are ready to be freed. reference from
129 * SA header will be removed in key_delsav(), when the reference count
130 * field hits 0 (= no external reference other than from SA header.
133 u_int32_t key_debug_level
= 0; //### our sysctl is not dynamic
134 static u_int key_spi_trycnt
= 1000;
135 static u_int32_t key_spi_minval
= 0x100;
136 static u_int32_t key_spi_maxval
= 0x0fffffff; /* XXX */
137 static u_int32_t policy_id
= 0;
138 static u_int key_int_random
= 60; /*interval to initialize randseed,1(m)*/
139 static u_int key_larval_lifetime
= 30; /* interval to expire acquiring, 30(s)*/
140 static int key_blockacq_count
= 10; /* counter for blocking SADB_ACQUIRE.*/
141 static int key_blockacq_lifetime
= 20; /* lifetime for blocking SADB_ACQUIRE.*/
142 static int key_preferred_oldsa
= 0; /* preferred old sa rather than new sa.*/
143 static int natt_keepalive_interval
= 20; /* interval between natt keepalives.*/
145 static u_int32_t acq_seq
= 0;
146 static int key_tick_init_random
= 0;
147 __private_extern__ u_int32_t natt_now
= 0;
149 static LIST_HEAD(_sptree
, secpolicy
) sptree
[IPSEC_DIR_MAX
]; /* SPD */
150 static LIST_HEAD(_sahtree
, secashead
) sahtree
; /* SAD */
151 static LIST_HEAD(_regtree
, secreg
) regtree
[SADB_SATYPE_MAX
+ 1];
154 #define SPIHASHSIZE 128
155 #define SPIHASH(x) (((x) ^ ((x) >> 16)) % SPIHASHSIZE)
156 static LIST_HEAD(_spihash
, secasvar
) spihash
[SPIHASHSIZE
];
158 #ifndef IPSEC_NONBLOCK_ACQUIRE
159 static LIST_HEAD(_acqtree
, secacq
) acqtree
; /* acquiring list */
161 static LIST_HEAD(_spacqtree
, secspacq
) spacqtree
; /* SP acquiring list */
163 struct key_cb key_cb
;
165 /* search order for SAs */
166 static const u_int saorder_state_valid_prefer_old
[] = {
167 SADB_SASTATE_DYING
, SADB_SASTATE_MATURE
,
169 static const u_int saorder_state_valid_prefer_new
[] = {
170 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
172 static const u_int saorder_state_alive
[] = {
174 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
, SADB_SASTATE_LARVAL
176 static const u_int saorder_state_any
[] = {
177 SADB_SASTATE_MATURE
, SADB_SASTATE_DYING
,
178 SADB_SASTATE_LARVAL
, SADB_SASTATE_DEAD
181 static const int minsize
[] = {
182 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
183 sizeof(struct sadb_sa
), /* SADB_EXT_SA */
184 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
185 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
186 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
187 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_SRC */
188 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_DST */
189 sizeof(struct sadb_address
), /* SADB_EXT_ADDRESS_PROXY */
190 sizeof(struct sadb_key
), /* SADB_EXT_KEY_AUTH */
191 sizeof(struct sadb_key
), /* SADB_EXT_KEY_ENCRYPT */
192 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_SRC */
193 sizeof(struct sadb_ident
), /* SADB_EXT_IDENTITY_DST */
194 sizeof(struct sadb_sens
), /* SADB_EXT_SENSITIVITY */
195 sizeof(struct sadb_prop
), /* SADB_EXT_PROPOSAL */
196 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_AUTH */
197 sizeof(struct sadb_supported
), /* SADB_EXT_SUPPORTED_ENCRYPT */
198 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
199 0, /* SADB_X_EXT_KMPRIVATE */
200 sizeof(struct sadb_x_policy
), /* SADB_X_EXT_POLICY */
201 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
203 static const int maxsize
[] = {
204 sizeof(struct sadb_msg
), /* SADB_EXT_RESERVED */
205 sizeof(struct sadb_sa_2
), /* SADB_EXT_SA */
206 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_CURRENT */
207 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_HARD */
208 sizeof(struct sadb_lifetime
), /* SADB_EXT_LIFETIME_SOFT */
209 0, /* SADB_EXT_ADDRESS_SRC */
210 0, /* SADB_EXT_ADDRESS_DST */
211 0, /* SADB_EXT_ADDRESS_PROXY */
212 0, /* SADB_EXT_KEY_AUTH */
213 0, /* SADB_EXT_KEY_ENCRYPT */
214 0, /* SADB_EXT_IDENTITY_SRC */
215 0, /* SADB_EXT_IDENTITY_DST */
216 0, /* SADB_EXT_SENSITIVITY */
217 0, /* SADB_EXT_PROPOSAL */
218 0, /* SADB_EXT_SUPPORTED_AUTH */
219 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
220 sizeof(struct sadb_spirange
), /* SADB_EXT_SPIRANGE */
221 0, /* SADB_X_EXT_KMPRIVATE */
222 0, /* SADB_X_EXT_POLICY */
223 sizeof(struct sadb_x_sa2
), /* SADB_X_SA2 */
226 static int ipsec_esp_keymin
= 256;
227 static int ipsec_esp_auth
= 0;
228 static int ipsec_ah_keymin
= 128;
230 SYSCTL_DECL(_net_key
);
232 SYSCTL_INT(_net_key
, KEYCTL_DEBUG_LEVEL
, debug
, CTLFLAG_RW
, \
233 &key_debug_level
, 0, "");
236 /* max count of trial for the decision of spi value */
237 SYSCTL_INT(_net_key
, KEYCTL_SPI_TRY
, spi_trycnt
, CTLFLAG_RW
, \
238 &key_spi_trycnt
, 0, "");
240 /* minimum spi value to allocate automatically. */
241 SYSCTL_INT(_net_key
, KEYCTL_SPI_MIN_VALUE
, spi_minval
, CTLFLAG_RW
, \
242 &key_spi_minval
, 0, "");
244 /* maximun spi value to allocate automatically. */
245 SYSCTL_INT(_net_key
, KEYCTL_SPI_MAX_VALUE
, spi_maxval
, CTLFLAG_RW
, \
246 &key_spi_maxval
, 0, "");
248 /* interval to initialize randseed */
249 SYSCTL_INT(_net_key
, KEYCTL_RANDOM_INT
, int_random
, CTLFLAG_RW
, \
250 &key_int_random
, 0, "");
252 /* lifetime for larval SA */
253 SYSCTL_INT(_net_key
, KEYCTL_LARVAL_LIFETIME
, larval_lifetime
, CTLFLAG_RW
, \
254 &key_larval_lifetime
, 0, "");
256 /* counter for blocking to send SADB_ACQUIRE to IKEd */
257 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_COUNT
, blockacq_count
, CTLFLAG_RW
, \
258 &key_blockacq_count
, 0, "");
260 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
261 SYSCTL_INT(_net_key
, KEYCTL_BLOCKACQ_LIFETIME
, blockacq_lifetime
, CTLFLAG_RW
, \
262 &key_blockacq_lifetime
, 0, "");
265 SYSCTL_INT(_net_key
, KEYCTL_ESP_AUTH
, esp_auth
, CTLFLAG_RW
, \
266 &ipsec_esp_auth
, 0, "");
268 /* minimum ESP key length */
269 SYSCTL_INT(_net_key
, KEYCTL_ESP_KEYMIN
, esp_keymin
, CTLFLAG_RW
, \
270 &ipsec_esp_keymin
, 0, "");
272 /* minimum AH key length */
273 SYSCTL_INT(_net_key
, KEYCTL_AH_KEYMIN
, ah_keymin
, CTLFLAG_RW
, \
274 &ipsec_ah_keymin
, 0, "");
276 /* perfered old SA rather than new SA */
277 SYSCTL_INT(_net_key
, KEYCTL_PREFERED_OLDSA
, prefered_oldsa
, CTLFLAG_RW
,\
278 &key_preferred_oldsa
, 0, "");
280 /* time between NATT keepalives in seconds, 0 disabled */
281 SYSCTL_INT(_net_key
, KEYCTL_NATT_KEEPALIVE_INTERVAL
, natt_keepalive_interval
, CTLFLAG_RW
,\
282 &natt_keepalive_interval
, 0, "");
284 /* PF_KEY statistics */
285 SYSCTL_STRUCT(_net_key
, KEYCTL_PFKEYSTAT
, pfkeystat
, CTLFLAG_RD
,\
286 &pfkeystat
, pfkeystat
, "");
289 #define LIST_FOREACH(elm, head, field) \
290 for (elm = LIST_FIRST(head); elm; elm = LIST_NEXT(elm, field))
292 #define __LIST_CHAINED(elm) \
293 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
294 #define LIST_INSERT_TAIL(head, elm, type, field) \
296 struct type *curelm = LIST_FIRST(head); \
297 if (curelm == NULL) {\
298 LIST_INSERT_HEAD(head, elm, field); \
300 while (LIST_NEXT(curelm, field)) \
301 curelm = LIST_NEXT(curelm, field);\
302 LIST_INSERT_AFTER(curelm, elm, field);\
306 #define KEY_CHKSASTATE(head, sav, name) \
308 if ((head) != (sav)) { \
309 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
310 (name), (head), (sav))); \
315 #define KEY_CHKSPDIR(head, sp, name) \
317 if ((head) != (sp)) { \
318 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
319 "anyway continue.\n", \
320 (name), (head), (sp))); \
325 #define KMALLOC(p, t, n) \
326 ((p) = (t) _MALLOC((unsigned long)(n), M_SECA, M_NOWAIT))
328 _FREE((caddr_t)(p), M_SECA);
330 #define KMALLOC(p, t, n) \
332 ((p) = (t)_MALLOC((unsigned long)(n), M_SECA, M_NOWAIT)); \
333 printf("%s %d: %p <- KMALLOC(%s, %d)\n", \
334 __FILE__, __LINE__, (p), #t, n); \
339 printf("%s %d: %p -> KFREE()\n", __FILE__, __LINE__, (p)); \
340 _FREE((caddr_t)(p), M_SECA); \
345 * set parameters into secpolicyindex buffer.
346 * Must allocate secpolicyindex buffer passed to this function.
348 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
350 bzero((idx), sizeof(struct secpolicyindex)); \
351 (idx)->dir = (_dir); \
352 (idx)->prefs = (ps); \
353 (idx)->prefd = (pd); \
354 (idx)->ul_proto = (ulp); \
355 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
356 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
360 * set parameters into secasindex buffer.
361 * Must allocate secasindex buffer before calling this function.
363 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
365 bzero((idx), sizeof(struct secasindex)); \
366 (idx)->proto = (p); \
368 (idx)->reqid = (r); \
369 bcopy((s), &(idx)->src, ((struct sockaddr *)(s))->sa_len); \
370 bcopy((d), &(idx)->dst, ((struct sockaddr *)(d))->sa_len); \
375 u_long getspi_count
; /* the avarage of count to try to get new SPI */
379 struct sadb_msg
*msg
;
380 struct sadb_ext
*ext
[SADB_EXT_MAX
+ 1];
381 int extoff
[SADB_EXT_MAX
+ 1];
382 int extlen
[SADB_EXT_MAX
+ 1];
385 static struct secasvar
*key_allocsa_policy(struct secasindex
*);
386 static void key_freesp_so(struct secpolicy
**);
387 static struct secasvar
*key_do_allocsa_policy(struct secashead
*, u_int
);
388 static void key_delsp(struct secpolicy
*);
389 static struct secpolicy
*key_getsp(struct secpolicyindex
*);
390 static struct secpolicy
*key_getspbyid(u_int32_t
);
391 static u_int32_t
key_newreqid(void);
392 static struct mbuf
*key_gather_mbuf(struct mbuf
*,
393 const struct sadb_msghdr
*, int, int, int *);
394 static int key_spdadd(struct socket
*, struct mbuf
*,
395 const struct sadb_msghdr
*);
396 static u_int32_t
key_getnewspid(void);
397 static int key_spddelete(struct socket
*, struct mbuf
*,
398 const struct sadb_msghdr
*);
399 static int key_spddelete2(struct socket
*, struct mbuf
*,
400 const struct sadb_msghdr
*);
401 static int key_spdget(struct socket
*, struct mbuf
*,
402 const struct sadb_msghdr
*);
403 static int key_spdflush(struct socket
*, struct mbuf
*,
404 const struct sadb_msghdr
*);
405 static int key_spddump(struct socket
*, struct mbuf
*,
406 const struct sadb_msghdr
*);
407 static struct mbuf
*key_setdumpsp(struct secpolicy
*,
408 u_int8_t
, u_int32_t
, u_int32_t
);
409 static u_int
key_getspreqmsglen(struct secpolicy
*);
410 static int key_spdexpire(struct secpolicy
*);
411 static struct secashead
*key_newsah(struct secasindex
*);
412 static void key_delsah(struct secashead
*);
413 static struct secasvar
*key_newsav(struct mbuf
*,
414 const struct sadb_msghdr
*, struct secashead
*, int *);
415 static void key_delsav(struct secasvar
*);
416 static struct secashead
*key_getsah(struct secasindex
*);
417 static struct secasvar
*key_checkspidup(struct secasindex
*, u_int32_t
);
418 static void key_setspi
__P((struct secasvar
*, u_int32_t
));
419 static struct secasvar
*key_getsavbyspi(struct secashead
*, u_int32_t
);
420 static int key_setsaval(struct secasvar
*, struct mbuf
*,
421 const struct sadb_msghdr
*);
422 static int key_mature(struct secasvar
*);
423 static struct mbuf
*key_setdumpsa(struct secasvar
*, u_int8_t
,
424 u_int8_t
, u_int32_t
, u_int32_t
);
425 static struct mbuf
*key_setsadbmsg(u_int8_t
, u_int16_t
, u_int8_t
,
426 u_int32_t
, pid_t
, u_int16_t
);
427 static struct mbuf
*key_setsadbsa(struct secasvar
*);
428 static struct mbuf
*key_setsadbaddr(u_int16_t
,
429 struct sockaddr
*, u_int8_t
, u_int16_t
);
431 static struct mbuf
*key_setsadbident(u_int16_t
, u_int16_t
, caddr_t
,
434 static struct mbuf
*key_setsadbxsa2(u_int8_t
, u_int32_t
, u_int32_t
);
435 static struct mbuf
*key_setsadbxpolicy(u_int16_t
, u_int8_t
,
437 static void *key_newbuf(const void *, u_int
);
439 static int key_ismyaddr6(struct sockaddr_in6
*);
442 /* flags for key_cmpsaidx() */
443 #define CMP_HEAD 1 /* protocol, addresses. */
444 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
445 #define CMP_REQID 3 /* additionally HEAD, reaid. */
446 #define CMP_EXACTLY 4 /* all elements. */
447 static int key_cmpsaidx(struct secasindex
*, struct secasindex
*, int);
449 static int key_cmpspidx_exactly(struct secpolicyindex
*,
450 struct secpolicyindex
*);
451 static int key_cmpspidx_withmask(struct secpolicyindex
*,
452 struct secpolicyindex
*);
453 static int key_sockaddrcmp(struct sockaddr
*, struct sockaddr
*, int);
454 static int key_bbcmp(caddr_t
, caddr_t
, u_int
);
455 static void key_srandom(void);
456 static u_int16_t
key_satype2proto(u_int8_t
);
457 static u_int8_t
key_proto2satype(u_int16_t
);
459 static int key_getspi(struct socket
*, struct mbuf
*,
460 const struct sadb_msghdr
*);
461 static u_int32_t
key_do_getnewspi(struct sadb_spirange
*, struct secasindex
*);
462 static int key_update(struct socket
*, struct mbuf
*,
463 const struct sadb_msghdr
*);
465 static struct secasvar
*key_getsavbyseq(struct secashead
*, u_int32_t
);
467 static int key_add(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
468 static int key_setident(struct secashead
*, struct mbuf
*,
469 const struct sadb_msghdr
*);
470 static struct mbuf
*key_getmsgbuf_x1(struct mbuf
*, const struct sadb_msghdr
*);
471 static int key_delete(struct socket
*, struct mbuf
*,
472 const struct sadb_msghdr
*);
473 static int key_get(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
475 static void key_getcomb_setlifetime(struct sadb_comb
*);
477 static struct mbuf
*key_getcomb_esp(void);
479 static struct mbuf
*key_getcomb_ah(void);
480 static struct mbuf
*key_getcomb_ipcomp(void);
481 static struct mbuf
*key_getprop(const struct secasindex
*);
483 static int key_acquire(struct secasindex
*, struct secpolicy
*);
484 #ifndef IPSEC_NONBLOCK_ACQUIRE
485 static struct secacq
*key_newacq(struct secasindex
*);
486 static struct secacq
*key_getacq(struct secasindex
*);
487 static struct secacq
*key_getacqbyseq(u_int32_t
);
489 static struct secspacq
*key_newspacq(struct secpolicyindex
*);
490 static struct secspacq
*key_getspacq(struct secpolicyindex
*);
491 static int key_acquire2(struct socket
*, struct mbuf
*,
492 const struct sadb_msghdr
*);
493 static int key_register(struct socket
*, struct mbuf
*,
494 const struct sadb_msghdr
*);
495 static int key_expire(struct secasvar
*);
496 static int key_flush(struct socket
*, struct mbuf
*,
497 const struct sadb_msghdr
*);
498 static int key_dump(struct socket
*, struct mbuf
*, const struct sadb_msghdr
*);
499 static int key_promisc(struct socket
*, struct mbuf
*,
500 const struct sadb_msghdr
*);
501 static int key_senderror(struct socket
*, struct mbuf
*, int);
502 static int key_validate_ext(const struct sadb_ext
*, int);
503 static int key_align(struct mbuf
*, struct sadb_msghdr
*);
505 static const char *key_getfqdn(void);
506 static const char *key_getuserfqdn(void);
508 static void key_sa_chgstate(struct secasvar
*, u_int8_t
);
509 static struct mbuf
*key_alloc_mbuf(int);
511 extern int ipsec_bypass
;
512 void ipsec_send_natt_keepalive(struct secasvar
*sav
);
517 * setup locks and call raw_init()
526 sadb_mutex_grp_attr
= lck_grp_attr_alloc_init();
527 sadb_mutex_grp
= lck_grp_alloc_init("sadb", sadb_mutex_grp_attr
);
528 sadb_mutex_attr
= lck_attr_alloc_init();
529 lck_attr_setdefault(sadb_mutex_attr
);
531 if ((sadb_mutex
= lck_mtx_alloc_init(sadb_mutex_grp
, sadb_mutex_attr
)) == NULL
) {
532 printf("key_init: can't alloc sadb_mutex\n");
536 for (i
= 0; i
< SPIHASHSIZE
; i
++)
537 LIST_INIT(&spihash
[i
]);
543 /* %%% IPsec policy management */
545 * allocating a SP for OUTBOUND or INBOUND packet.
546 * Must call key_freesp() later.
547 * OUT: NULL: not found
548 * others: found and return the pointer.
551 key_allocsp(spidx
, dir
)
552 struct secpolicyindex
*spidx
;
555 struct secpolicy
*sp
;
559 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
562 panic("key_allocsp: NULL pointer is passed.\n");
564 /* check direction */
566 case IPSEC_DIR_INBOUND
:
567 case IPSEC_DIR_OUTBOUND
:
570 panic("key_allocsp: Invalid direction is passed.\n");
574 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
575 printf("*** objects\n");
576 kdebug_secpolicyindex(spidx
));
578 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
579 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
580 printf("*** in SPD\n");
581 kdebug_secpolicyindex(&sp
->spidx
));
583 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
585 if (key_cmpspidx_withmask(&sp
->spidx
, spidx
))
593 KEY_CHKSPDIR(sp
->spidx
.dir
, dir
, "key_allocsp");
595 /* found a SPD entry */
597 sp
->lastused
= tv
.tv_sec
;
599 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
600 printf("DP key_allocsp cause refcnt++:%d SP:%p\n",
607 * return a policy that matches this particular inbound packet.
611 key_gettunnel(osrc
, odst
, isrc
, idst
)
612 struct sockaddr
*osrc
, *odst
, *isrc
, *idst
;
614 struct secpolicy
*sp
;
615 const int dir
= IPSEC_DIR_INBOUND
;
618 struct ipsecrequest
*r1
, *r2
, *p
;
619 struct sockaddr
*os
, *od
, *is
, *id
;
620 struct secpolicyindex spidx
;
622 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
624 if (isrc
->sa_family
!= idst
->sa_family
) {
625 ipseclog((LOG_ERR
, "protocol family mismatched %d != %d\n.",
626 isrc
->sa_family
, idst
->sa_family
));
630 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
631 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
635 for (p
= sp
->req
; p
; p
= p
->next
) {
636 if (p
->saidx
.mode
!= IPSEC_MODE_TUNNEL
)
643 /* here we look at address matches only */
645 if (isrc
->sa_len
> sizeof(spidx
.src
) ||
646 idst
->sa_len
> sizeof(spidx
.dst
))
648 bcopy(isrc
, &spidx
.src
, isrc
->sa_len
);
649 bcopy(idst
, &spidx
.dst
, idst
->sa_len
);
650 if (!key_cmpspidx_withmask(&sp
->spidx
, &spidx
))
653 is
= (struct sockaddr
*)&r1
->saidx
.src
;
654 id
= (struct sockaddr
*)&r1
->saidx
.dst
;
655 if (key_sockaddrcmp(is
, isrc
, 0) ||
656 key_sockaddrcmp(id
, idst
, 0))
660 os
= (struct sockaddr
*)&r2
->saidx
.src
;
661 od
= (struct sockaddr
*)&r2
->saidx
.dst
;
662 if (key_sockaddrcmp(os
, osrc
, 0) ||
663 key_sockaddrcmp(od
, odst
, 0))
674 sp
->lastused
= tv
.tv_sec
;
680 * allocating an SA entry for an *OUTBOUND* packet.
681 * checking each request entries in SP, and acquire an SA if need.
682 * OUT: 0: there are valid requests.
683 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
686 key_checkrequest(isr
, saidx
)
687 struct ipsecrequest
*isr
;
688 struct secasindex
*saidx
;
693 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
696 if (isr
== NULL
|| saidx
== NULL
)
697 panic("key_checkrequest: NULL pointer is passed.\n");
700 switch (saidx
->mode
) {
701 case IPSEC_MODE_TRANSPORT
:
702 case IPSEC_MODE_TUNNEL
:
706 panic("key_checkrequest: Invalid policy defined.\n");
709 /* get current level */
710 level
= ipsec_get_reqlevel(isr
);
714 * We do allocate new SA only if the state of SA in the holder is
715 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
717 if (isr
->sav
!= NULL
) {
718 if (isr
->sav
->sah
== NULL
)
719 panic("key_checkrequest: sah is null.\n");
720 if (isr
->sav
== (struct secasvar
*)LIST_FIRST(
721 &isr
->sav
->sah
->savtree
[SADB_SASTATE_DEAD
])) {
722 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
723 printf("DP checkrequest calls free SA:%p\n",
725 key_freesav(isr
->sav
);
731 * we free any SA stashed in the IPsec request because a different
732 * SA may be involved each time this request is checked, either
733 * because new SAs are being configured, or this request is
734 * associated with an unconnected datagram socket, or this request
735 * is associated with a system default policy.
737 * The operation may have negative impact to performance. We may
738 * want to check cached SA carefully, rather than picking new SA
741 if (isr
->sav
!= NULL
) {
742 key_freesav(isr
->sav
);
748 * new SA allocation if no SA found.
749 * key_allocsa_policy should allocate the oldest SA available.
750 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
752 if (isr
->sav
== NULL
)
753 isr
->sav
= key_allocsa_policy(saidx
);
755 /* When there is SA. */
756 if (isr
->sav
!= NULL
)
760 if ((error
= key_acquire(saidx
, isr
->sp
)) != 0) {
761 /* XXX What should I do ? */
762 ipseclog((LOG_DEBUG
, "key_checkrequest: error %d returned "
763 "from key_acquire.\n", error
));
767 return level
== IPSEC_LEVEL_REQUIRE
? ENOENT
: 0;
771 * allocating a SA for policy entry from SAD.
772 * NOTE: searching SAD of aliving state.
773 * OUT: NULL: not found.
774 * others: found and return the pointer.
776 static struct secasvar
*
777 key_allocsa_policy(saidx
)
778 struct secasindex
*saidx
;
780 struct secashead
*sah
;
781 struct secasvar
*sav
;
782 u_int stateidx
, state
;
783 const u_int
*saorder_state_valid
;
786 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
788 LIST_FOREACH(sah
, &sahtree
, chain
) {
789 if (sah
->state
== SADB_SASTATE_DEAD
)
791 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_MODE_REQID
))
800 * search a valid state list for outbound packet.
801 * This search order is important.
803 if (key_preferred_oldsa
) {
804 saorder_state_valid
= saorder_state_valid_prefer_old
;
805 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
807 saorder_state_valid
= saorder_state_valid_prefer_new
;
808 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
811 for (stateidx
= 0; stateidx
< arraysize
; stateidx
++) {
813 state
= saorder_state_valid
[stateidx
];
815 sav
= key_do_allocsa_policy(sah
, state
);
824 * searching SAD with direction, protocol, mode and state.
825 * called by key_allocsa_policy().
828 * others : found, pointer to a SA.
830 static struct secasvar
*
831 key_do_allocsa_policy(sah
, state
)
832 struct secashead
*sah
;
835 struct secasvar
*sav
, *nextsav
, *candidate
, *d
;
837 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
842 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
846 nextsav
= LIST_NEXT(sav
, chain
);
849 KEY_CHKSASTATE(sav
->state
, state
, "key_do_allocsa_policy");
852 if (candidate
== NULL
) {
857 /* Which SA is the better ? */
860 if (candidate
->lft_c
== NULL
|| sav
->lft_c
== NULL
)
861 panic("key_do_allocsa_policy: "
862 "lifetime_current is NULL.\n");
864 /* What the best method is to compare ? */
865 if (key_preferred_oldsa
) {
866 if (candidate
->lft_c
->sadb_lifetime_addtime
>
867 sav
->lft_c
->sadb_lifetime_addtime
) {
874 /* prefered new sa rather than old sa */
875 if (candidate
->lft_c
->sadb_lifetime_addtime
<
876 sav
->lft_c
->sadb_lifetime_addtime
) {
883 * prepared to delete the SA when there is more
884 * suitable candidate and the lifetime of the SA is not
887 if (d
->lft_c
->sadb_lifetime_addtime
!= 0) {
888 struct mbuf
*m
, *result
;
890 key_sa_chgstate(d
, SADB_SASTATE_DEAD
);
892 m
= key_setsadbmsg(SADB_DELETE
, 0,
893 d
->sah
->saidx
.proto
, 0, 0, d
->refcnt
- 1);
898 /* set sadb_address for saidx's. */
899 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
900 (struct sockaddr
*)&d
->sah
->saidx
.src
,
901 d
->sah
->saidx
.src
.ss_len
<< 3,
907 /* set sadb_address for saidx's. */
908 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
909 (struct sockaddr
*)&d
->sah
->saidx
.src
,
910 d
->sah
->saidx
.src
.ss_len
<< 3,
916 /* create SA extension */
917 m
= key_setsadbsa(d
);
922 if (result
->m_len
< sizeof(struct sadb_msg
)) {
923 result
= m_pullup(result
,
924 sizeof(struct sadb_msg
));
929 result
->m_pkthdr
.len
= 0;
930 for (m
= result
; m
; m
= m
->m_next
)
931 result
->m_pkthdr
.len
+= m
->m_len
;
932 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
933 PFKEY_UNIT64(result
->m_pkthdr
.len
);
935 if (key_sendup_mbuf(NULL
, result
,
936 KEY_SENDUP_REGISTERED
))
945 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
946 printf("DP allocsa_policy cause "
947 "refcnt++:%d SA:%p\n",
948 candidate
->refcnt
, candidate
));
954 * allocating a SA entry for a *INBOUND* packet.
955 * Must call key_freesav() later.
956 * OUT: positive: pointer to a sav.
957 * NULL: not found, or error occurred.
959 * In the comparison, source address will be ignored for RFC2401 conformance.
960 * To quote, from section 4.1:
961 * A security association is uniquely identified by a triple consisting
962 * of a Security Parameter Index (SPI), an IP Destination Address, and a
963 * security protocol (AH or ESP) identifier.
964 * Note that, however, we do need to keep source address in IPsec SA.
965 * IKE specification and PF_KEY specification do assume that we
966 * keep source address in IPsec SA. We see a tricky situation here.
969 key_allocsa(family
, src
, dst
, proto
, spi
)
974 struct secasvar
*sav
, *match
;
975 u_int stateidx
, state
, tmpidx
, matchidx
;
976 struct sockaddr_in sin
;
977 struct sockaddr_in6 sin6
;
979 const u_int
*saorder_state_valid
;
982 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
985 if (src
== NULL
|| dst
== NULL
)
986 panic("key_allocsa: NULL pointer is passed.\n");
989 * when both systems employ similar strategy to use a SA.
990 * the search order is important even in the inbound case.
992 if (key_preferred_oldsa
) {
993 saorder_state_valid
= saorder_state_valid_prefer_old
;
994 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_old
);
996 saorder_state_valid
= saorder_state_valid_prefer_new
;
997 arraysize
= _ARRAYLEN(saorder_state_valid_prefer_new
);
1002 * XXX: to be checked internal IP header somewhere. Also when
1003 * IPsec tunnel packet is received. But ESP tunnel mode is
1004 * encrypted so we can't check internal IP header.
1007 * search a valid state list for inbound packet.
1008 * the search order is not important.
1011 matchidx
= arraysize
;
1012 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
1013 if (sav
->spi
!= spi
)
1015 if (proto
!= sav
->sah
->saidx
.proto
)
1017 if (family
!= sav
->sah
->saidx
.src
.ss_family
||
1018 family
!= sav
->sah
->saidx
.dst
.ss_family
)
1021 for (stateidx
= 0; stateidx
< matchidx
; stateidx
++) {
1022 state
= saorder_state_valid
[stateidx
];
1023 if (sav
->state
== state
) {
1028 if (tmpidx
>= matchidx
)
1031 #if 0 /* don't check src */
1032 /* check src address */
1035 bzero(&sin
, sizeof(sin
));
1036 sin
.sin_family
= AF_INET
;
1037 sin
.sin_len
= sizeof(sin
);
1038 bcopy(src
, &sin
.sin_addr
,
1039 sizeof(sin
.sin_addr
));
1040 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
1041 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0)
1045 bzero(&sin6
, sizeof(sin6
));
1046 sin6
.sin6_family
= AF_INET6
;
1047 sin6
.sin6_len
= sizeof(sin6
);
1048 bcopy(src
, &sin6
.sin6_addr
,
1049 sizeof(sin6
.sin6_addr
));
1050 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
1051 /* kame fake scopeid */
1052 sin6
.sin6_scope_id
=
1053 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
1054 sin6
.sin6_addr
.s6_addr16
[1] = 0;
1056 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
1057 (struct sockaddr
*)&sav
->sah
->saidx
.src
, 0) != 0)
1061 ipseclog((LOG_DEBUG
, "key_allocsa: "
1062 "unknown address family=%d.\n",
1068 /* check dst address */
1071 bzero(&sin
, sizeof(sin
));
1072 sin
.sin_family
= AF_INET
;
1073 sin
.sin_len
= sizeof(sin
);
1074 bcopy(dst
, &sin
.sin_addr
,
1075 sizeof(sin
.sin_addr
));
1076 if (key_sockaddrcmp((struct sockaddr
*)&sin
,
1077 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0)
1082 bzero(&sin6
, sizeof(sin6
));
1083 sin6
.sin6_family
= AF_INET6
;
1084 sin6
.sin6_len
= sizeof(sin6
);
1085 bcopy(dst
, &sin6
.sin6_addr
,
1086 sizeof(sin6
.sin6_addr
));
1087 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
.sin6_addr
)) {
1088 /* kame fake scopeid */
1089 sin6
.sin6_scope_id
=
1090 ntohs(sin6
.sin6_addr
.s6_addr16
[1]);
1091 sin6
.sin6_addr
.s6_addr16
[1] = 0;
1093 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
1094 (struct sockaddr
*)&sav
->sah
->saidx
.dst
, 0) != 0)
1098 ipseclog((LOG_DEBUG
, "key_allocsa: "
1099 "unknown address family=%d.\n", family
));
1114 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1115 printf("DP allocsa cause refcnt++:%d SA:%p\n",
1116 match
->refcnt
, match
));
1121 * Must be called after calling key_allocsp().
1122 * For both the packet without socket and key_freeso().
1126 struct secpolicy
*sp
;
1128 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1132 panic("key_freesp: NULL pointer is passed.\n");
1135 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1136 printf("DP freesp cause refcnt--:%d SP:%p\n",
1139 if (sp
->refcnt
== 0)
1147 * Must be called after calling key_allocsp().
1148 * For the packet with socket.
1154 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1158 panic("key_freeso: NULL pointer is passed.\n");
1160 switch (so
->so_proto
->pr_domain
->dom_family
) {
1164 struct inpcb
*pcb
= sotoinpcb(so
);
1166 /* Does it have a PCB ? */
1167 if (pcb
== NULL
|| pcb
->inp_sp
== NULL
)
1169 key_freesp_so(&pcb
->inp_sp
->sp_in
);
1170 key_freesp_so(&pcb
->inp_sp
->sp_out
);
1178 struct inpcb
*pcb
= sotoinpcb(so
);
1180 /* Does it have a PCB ? */
1181 if (pcb
== NULL
|| pcb
->inp_sp
== NULL
)
1183 key_freesp_so(&pcb
->inp_sp
->sp_in
);
1184 key_freesp_so(&pcb
->inp_sp
->sp_out
);
1186 struct in6pcb
*pcb
= sotoin6pcb(so
);
1188 /* Does it have a PCB ? */
1189 if (pcb
== NULL
|| pcb
->in6p_sp
== NULL
)
1191 key_freesp_so(&pcb
->in6p_sp
->sp_in
);
1192 key_freesp_so(&pcb
->in6p_sp
->sp_out
);
1198 ipseclog((LOG_DEBUG
, "key_freeso: unknown address family=%d.\n",
1199 so
->so_proto
->pr_domain
->dom_family
));
1209 struct secpolicy
**sp
;
1212 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1215 if (sp
== NULL
|| *sp
== NULL
)
1216 panic("key_freesp_so: sp == NULL\n");
1218 switch ((*sp
)->policy
) {
1219 case IPSEC_POLICY_IPSEC
:
1220 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1221 printf("DP freeso calls free SP:%p\n", *sp
));
1225 case IPSEC_POLICY_ENTRUST
:
1226 case IPSEC_POLICY_BYPASS
:
1229 panic("key_freesp_so: Invalid policy found %d", (*sp
)->policy
);
1236 * Must be called after calling key_allocsa().
1237 * This function is called by key_freesp() to free some SA allocated
1242 struct secasvar
*sav
;
1244 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1248 panic("key_freesav: NULL pointer is passed.\n");
1251 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1252 printf("DP freesav cause refcnt--:%d SA:%p SPI %u\n",
1253 sav
->refcnt
, sav
, (u_int32_t
)ntohl(sav
->spi
)));
1255 if (sav
->refcnt
== 0)
1261 /* %%% SPD management */
1263 * free security policy entry.
1267 struct secpolicy
*sp
;
1271 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1275 panic("key_delsp: NULL pointer is passed.\n");
1277 sp
->state
= IPSEC_SPSTATE_DEAD
;
1280 return; /* can't free */
1282 s
= splnet(); /*called from softclock()*/
1283 /* remove from SP index */
1284 if (__LIST_CHAINED(sp
))
1285 LIST_REMOVE(sp
, chain
);
1288 struct ipsecrequest
*isr
= sp
->req
, *nextisr
;
1290 while (isr
!= NULL
) {
1291 if (isr
->sav
!= NULL
) {
1292 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1293 printf("DP delsp calls free SA:%p\n",
1295 key_freesav(isr
->sav
);
1299 nextisr
= isr
->next
;
1305 keydb_delsecpolicy(sp
);
1314 * OUT: NULL : not found
1315 * others : found, pointer to a SP.
1317 static struct secpolicy
*
1319 struct secpolicyindex
*spidx
;
1321 struct secpolicy
*sp
;
1323 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1327 panic("key_getsp: NULL pointer is passed.\n");
1329 LIST_FOREACH(sp
, &sptree
[spidx
->dir
], chain
) {
1330 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1332 if (key_cmpspidx_exactly(spidx
, &sp
->spidx
)) {
1343 * OUT: NULL : not found
1344 * others : found, pointer to a SP.
1346 static struct secpolicy
*
1350 struct secpolicy
*sp
;
1352 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1354 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_INBOUND
], chain
) {
1355 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1363 LIST_FOREACH(sp
, &sptree
[IPSEC_DIR_OUTBOUND
], chain
) {
1364 if (sp
->state
== IPSEC_SPSTATE_DEAD
)
1378 struct secpolicy
*newsp
= NULL
;
1380 newsp
= keydb_newsecpolicy();
1391 * create secpolicy structure from sadb_x_policy structure.
1392 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1393 * so must be set properly later.
1396 key_msg2sp(xpl0
, len
, error
)
1397 struct sadb_x_policy
*xpl0
;
1401 struct secpolicy
*newsp
;
1403 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1407 panic("key_msg2sp: NULL pointer was passed.\n");
1408 if (len
< sizeof(*xpl0
))
1409 panic("key_msg2sp: invalid length.\n");
1410 if (len
!= PFKEY_EXTLEN(xpl0
)) {
1411 ipseclog((LOG_DEBUG
, "key_msg2sp: Invalid msg length.\n"));
1416 if ((newsp
= key_newsp()) == NULL
) {
1421 newsp
->spidx
.dir
= xpl0
->sadb_x_policy_dir
;
1422 newsp
->policy
= xpl0
->sadb_x_policy_type
;
1425 switch (xpl0
->sadb_x_policy_type
) {
1426 case IPSEC_POLICY_DISCARD
:
1427 case IPSEC_POLICY_NONE
:
1428 case IPSEC_POLICY_ENTRUST
:
1429 case IPSEC_POLICY_BYPASS
:
1433 case IPSEC_POLICY_IPSEC
:
1436 struct sadb_x_ipsecrequest
*xisr
;
1437 struct ipsecrequest
**p_isr
= &newsp
->req
;
1439 /* validity check */
1440 if (PFKEY_EXTLEN(xpl0
) < sizeof(*xpl0
)) {
1441 ipseclog((LOG_DEBUG
,
1442 "key_msg2sp: Invalid msg length.\n"));
1448 tlen
= PFKEY_EXTLEN(xpl0
) - sizeof(*xpl0
);
1449 xisr
= (struct sadb_x_ipsecrequest
*)(xpl0
+ 1);
1454 if (xisr
->sadb_x_ipsecrequest_len
< sizeof(*xisr
)) {
1455 ipseclog((LOG_DEBUG
, "key_msg2sp: "
1456 "invalid ipsecrequest length.\n"));
1462 /* allocate request buffer */
1463 KMALLOC(*p_isr
, struct ipsecrequest
*, sizeof(**p_isr
));
1464 if ((*p_isr
) == NULL
) {
1465 ipseclog((LOG_DEBUG
,
1466 "key_msg2sp: No more memory.\n"));
1471 bzero(*p_isr
, sizeof(**p_isr
));
1474 (*p_isr
)->next
= NULL
;
1476 switch (xisr
->sadb_x_ipsecrequest_proto
) {
1479 case IPPROTO_IPCOMP
:
1482 ipseclog((LOG_DEBUG
,
1483 "key_msg2sp: invalid proto type=%u\n",
1484 xisr
->sadb_x_ipsecrequest_proto
));
1486 *error
= EPROTONOSUPPORT
;
1489 (*p_isr
)->saidx
.proto
= xisr
->sadb_x_ipsecrequest_proto
;
1491 switch (xisr
->sadb_x_ipsecrequest_mode
) {
1492 case IPSEC_MODE_TRANSPORT
:
1493 case IPSEC_MODE_TUNNEL
:
1495 case IPSEC_MODE_ANY
:
1497 ipseclog((LOG_DEBUG
,
1498 "key_msg2sp: invalid mode=%u\n",
1499 xisr
->sadb_x_ipsecrequest_mode
));
1504 (*p_isr
)->saidx
.mode
= xisr
->sadb_x_ipsecrequest_mode
;
1506 switch (xisr
->sadb_x_ipsecrequest_level
) {
1507 case IPSEC_LEVEL_DEFAULT
:
1508 case IPSEC_LEVEL_USE
:
1509 case IPSEC_LEVEL_REQUIRE
:
1511 case IPSEC_LEVEL_UNIQUE
:
1512 /* validity check */
1514 * If range violation of reqid, kernel will
1515 * update it, don't refuse it.
1517 if (xisr
->sadb_x_ipsecrequest_reqid
1518 > IPSEC_MANUAL_REQID_MAX
) {
1519 ipseclog((LOG_DEBUG
,
1520 "key_msg2sp: reqid=%d range "
1521 "violation, updated by kernel.\n",
1522 xisr
->sadb_x_ipsecrequest_reqid
));
1523 xisr
->sadb_x_ipsecrequest_reqid
= 0;
1526 /* allocate new reqid id if reqid is zero. */
1527 if (xisr
->sadb_x_ipsecrequest_reqid
== 0) {
1529 if ((reqid
= key_newreqid()) == 0) {
1534 (*p_isr
)->saidx
.reqid
= reqid
;
1535 xisr
->sadb_x_ipsecrequest_reqid
= reqid
;
1537 /* set it for manual keying. */
1538 (*p_isr
)->saidx
.reqid
=
1539 xisr
->sadb_x_ipsecrequest_reqid
;
1544 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid level=%u\n",
1545 xisr
->sadb_x_ipsecrequest_level
));
1550 (*p_isr
)->level
= xisr
->sadb_x_ipsecrequest_level
;
1552 /* set IP addresses if there */
1553 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
1554 struct sockaddr
*paddr
;
1556 paddr
= (struct sockaddr
*)(xisr
+ 1);
1558 /* validity check */
1560 > sizeof((*p_isr
)->saidx
.src
)) {
1561 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1562 "address length.\n"));
1567 bcopy(paddr
, &(*p_isr
)->saidx
.src
,
1570 paddr
= (struct sockaddr
*)((caddr_t
)paddr
1573 /* validity check */
1575 > sizeof((*p_isr
)->saidx
.dst
)) {
1576 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid request "
1577 "address length.\n"));
1582 bcopy(paddr
, &(*p_isr
)->saidx
.dst
,
1586 (*p_isr
)->sav
= NULL
;
1587 (*p_isr
)->sp
= newsp
;
1589 /* initialization for the next. */
1590 p_isr
= &(*p_isr
)->next
;
1591 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
1593 /* validity check */
1595 ipseclog((LOG_DEBUG
, "key_msg2sp: becoming tlen < 0.\n"));
1601 xisr
= (struct sadb_x_ipsecrequest
*)((caddr_t
)xisr
1602 + xisr
->sadb_x_ipsecrequest_len
);
1607 ipseclog((LOG_DEBUG
, "key_msg2sp: invalid policy type.\n"));
1620 static u_int32_t auto_reqid
= IPSEC_MANUAL_REQID_MAX
+ 1;
1622 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1624 auto_reqid
= (auto_reqid
== ~0
1625 ? IPSEC_MANUAL_REQID_MAX
+ 1 : auto_reqid
+ 1);
1627 /* XXX should be unique check */
1633 * copy secpolicy struct to sadb_x_policy structure indicated.
1637 struct secpolicy
*sp
;
1639 struct sadb_x_policy
*xpl
;
1644 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1648 panic("key_sp2msg: NULL pointer was passed.\n");
1650 tlen
= key_getspreqmsglen(sp
);
1652 m
= key_alloc_mbuf(tlen
);
1653 if (!m
|| m
->m_next
) { /*XXX*/
1661 xpl
= mtod(m
, struct sadb_x_policy
*);
1664 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(tlen
);
1665 xpl
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1666 xpl
->sadb_x_policy_type
= sp
->policy
;
1667 xpl
->sadb_x_policy_dir
= sp
->spidx
.dir
;
1668 xpl
->sadb_x_policy_id
= sp
->id
;
1669 p
= (caddr_t
)xpl
+ sizeof(*xpl
);
1671 /* if is the policy for ipsec ? */
1672 if (sp
->policy
== IPSEC_POLICY_IPSEC
) {
1673 struct sadb_x_ipsecrequest
*xisr
;
1674 struct ipsecrequest
*isr
;
1676 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1678 xisr
= (struct sadb_x_ipsecrequest
*)p
;
1680 xisr
->sadb_x_ipsecrequest_proto
= isr
->saidx
.proto
;
1681 xisr
->sadb_x_ipsecrequest_mode
= isr
->saidx
.mode
;
1682 xisr
->sadb_x_ipsecrequest_level
= isr
->level
;
1683 xisr
->sadb_x_ipsecrequest_reqid
= isr
->saidx
.reqid
;
1686 bcopy(&isr
->saidx
.src
, p
, isr
->saidx
.src
.ss_len
);
1687 p
+= isr
->saidx
.src
.ss_len
;
1688 bcopy(&isr
->saidx
.dst
, p
, isr
->saidx
.dst
.ss_len
);
1689 p
+= isr
->saidx
.src
.ss_len
;
1691 xisr
->sadb_x_ipsecrequest_len
=
1692 PFKEY_ALIGN8(sizeof(*xisr
)
1693 + isr
->saidx
.src
.ss_len
1694 + isr
->saidx
.dst
.ss_len
);
1701 /* m will not be freed nor modified */
1702 static struct mbuf
*
1703 key_gather_mbuf(struct mbuf
*m
, const struct sadb_msghdr
*mhp
,
1704 int ndeep
, int nitem
, int *items
)
1708 struct mbuf
*result
= NULL
, *n
;
1711 if (m
== NULL
|| mhp
== NULL
)
1712 panic("null pointer passed to key_gather");
1714 for (i
= 0; i
< nitem
; i
++) {
1716 if (idx
< 0 || idx
> SADB_EXT_MAX
)
1718 /* don't attempt to pull empty extension */
1719 if (idx
== SADB_EXT_RESERVED
&& mhp
->msg
== NULL
)
1721 if (idx
!= SADB_EXT_RESERVED
&&
1722 (mhp
->ext
[idx
] == NULL
|| mhp
->extlen
[idx
] == 0))
1725 if (idx
== SADB_EXT_RESERVED
) {
1726 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
1729 panic("assumption failed");
1731 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
1736 m_copydata(m
, 0, sizeof(struct sadb_msg
),
1738 } else if (i
< ndeep
) {
1739 len
= mhp
->extlen
[idx
];
1740 n
= key_alloc_mbuf(len
);
1741 if (!n
|| n
->m_next
) { /*XXX*/
1746 m_copydata(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
1749 n
= m_copym(m
, mhp
->extoff
[idx
], mhp
->extlen
[idx
],
1761 if ((result
->m_flags
& M_PKTHDR
) != 0) {
1762 result
->m_pkthdr
.len
= 0;
1763 for (n
= result
; n
; n
= n
->m_next
)
1764 result
->m_pkthdr
.len
+= n
->m_len
;
1775 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1776 * add a entry to SP database, when received
1777 * <base, address(SD), (lifetime(H),) policy>
1779 * Adding to SP database,
1781 * <base, address(SD), (lifetime(H),) policy>
1782 * to the socket which was send.
1784 * SPDADD set a unique policy entry.
1785 * SPDSETIDX like SPDADD without a part of policy requests.
1786 * SPDUPDATE replace a unique policy entry.
1788 * m will always be freed.
1791 key_spdadd(so
, m
, mhp
)
1794 const struct sadb_msghdr
*mhp
;
1796 struct sadb_address
*src0
, *dst0
;
1797 struct sadb_x_policy
*xpl0
, *xpl
;
1798 struct sadb_lifetime
*lft
= NULL
;
1799 struct secpolicyindex spidx
;
1800 struct secpolicy
*newsp
;
1804 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1807 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
1808 panic("key_spdadd: NULL pointer is passed.\n");
1810 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
1811 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
1812 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
1813 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
1814 return key_senderror(so
, m
, EINVAL
);
1816 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
1817 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
1818 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
1819 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
1820 return key_senderror(so
, m
, EINVAL
);
1822 if (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
) {
1823 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
]
1824 < sizeof(struct sadb_lifetime
)) {
1825 ipseclog((LOG_DEBUG
, "key_spdadd: invalid message is passed.\n"));
1826 return key_senderror(so
, m
, EINVAL
);
1828 lft
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
1831 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
1832 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
1833 xpl0
= (struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
];
1836 /* XXX boundary check against sa_len */
1837 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
1840 src0
->sadb_address_prefixlen
,
1841 dst0
->sadb_address_prefixlen
,
1842 src0
->sadb_address_proto
,
1845 /* checking the direciton. */
1846 switch (xpl0
->sadb_x_policy_dir
) {
1847 case IPSEC_DIR_INBOUND
:
1848 case IPSEC_DIR_OUTBOUND
:
1851 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid SP direction.\n"));
1852 mhp
->msg
->sadb_msg_errno
= EINVAL
;
1857 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1858 if (xpl0
->sadb_x_policy_type
== IPSEC_POLICY_ENTRUST
1859 || xpl0
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
) {
1860 ipseclog((LOG_DEBUG
, "key_spdadd: Invalid policy type.\n"));
1861 return key_senderror(so
, m
, EINVAL
);
1864 /* policy requests are mandatory when action is ipsec. */
1865 if (mhp
->msg
->sadb_msg_type
!= SADB_X_SPDSETIDX
1866 && xpl0
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
1867 && mhp
->extlen
[SADB_X_EXT_POLICY
] <= sizeof(*xpl0
)) {
1868 ipseclog((LOG_DEBUG
, "key_spdadd: some policy requests part required.\n"));
1869 return key_senderror(so
, m
, EINVAL
);
1873 * checking there is SP already or not.
1874 * SPDUPDATE doesn't depend on whether there is a SP or not.
1875 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1878 newsp
= key_getsp(&spidx
);
1879 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
1881 newsp
->state
= IPSEC_SPSTATE_DEAD
;
1885 if (newsp
!= NULL
) {
1887 ipseclog((LOG_DEBUG
, "key_spdadd: a SP entry exists already.\n"));
1888 return key_senderror(so
, m
, EEXIST
);
1892 /* allocation new SP entry */
1893 if ((newsp
= key_msg2sp(xpl0
, PFKEY_EXTLEN(xpl0
), &error
)) == NULL
) {
1894 return key_senderror(so
, m
, error
);
1897 if ((newsp
->id
= key_getnewspid()) == 0) {
1898 keydb_delsecpolicy(newsp
);
1899 return key_senderror(so
, m
, ENOBUFS
);
1902 /* XXX boundary check against sa_len */
1903 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
1906 src0
->sadb_address_prefixlen
,
1907 dst0
->sadb_address_prefixlen
,
1908 src0
->sadb_address_proto
,
1911 /* sanity check on addr pair */
1912 if (((struct sockaddr
*)(src0
+ 1))->sa_family
!=
1913 ((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
1914 keydb_delsecpolicy(newsp
);
1915 return key_senderror(so
, m
, EINVAL
);
1917 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
1918 ((struct sockaddr
*)(dst0
+ 1))->sa_len
) {
1919 keydb_delsecpolicy(newsp
);
1920 return key_senderror(so
, m
, EINVAL
);
1923 if (newsp
->req
&& newsp
->req
->saidx
.src
.ss_family
) {
1924 struct sockaddr
*sa
;
1925 sa
= (struct sockaddr
*)(src0
+ 1);
1926 if (sa
->sa_family
!= newsp
->req
->saidx
.src
.ss_family
) {
1927 keydb_delsecpolicy(newsp
);
1928 return key_senderror(so
, m
, EINVAL
);
1931 if (newsp
->req
&& newsp
->req
->saidx
.dst
.ss_family
) {
1932 struct sockaddr
*sa
;
1933 sa
= (struct sockaddr
*)(dst0
+ 1);
1934 if (sa
->sa_family
!= newsp
->req
->saidx
.dst
.ss_family
) {
1935 keydb_delsecpolicy(newsp
);
1936 return key_senderror(so
, m
, EINVAL
);
1942 newsp
->created
= tv
.tv_sec
;
1943 newsp
->lastused
= tv
.tv_sec
;
1944 newsp
->lifetime
= lft
? lft
->sadb_lifetime_addtime
: 0;
1945 newsp
->validtime
= lft
? lft
->sadb_lifetime_usetime
: 0;
1947 newsp
->refcnt
= 1; /* do not reclaim until I say I do */
1948 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
1949 LIST_INSERT_TAIL(&sptree
[newsp
->spidx
.dir
], newsp
, secpolicy
, chain
);
1951 /* Turn off the ipsec bypass */
1952 if (ipsec_bypass
!= 0)
1955 /* delete the entry in spacqtree */
1956 if (mhp
->msg
->sadb_msg_type
== SADB_X_SPDUPDATE
) {
1957 struct secspacq
*spacq
;
1958 if ((spacq
= key_getspacq(&spidx
)) != NULL
) {
1959 /* reset counter in order to deletion by timehandler. */
1961 spacq
->created
= tv
.tv_sec
;
1967 struct mbuf
*n
, *mpolicy
;
1968 struct sadb_msg
*newmsg
;
1971 /* create new sadb_msg to reply. */
1973 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
1974 SADB_EXT_LIFETIME_HARD
, SADB_EXT_ADDRESS_SRC
,
1975 SADB_EXT_ADDRESS_DST
};
1976 n
= key_gather_mbuf(m
, mhp
, 2, sizeof(mbufItems
)/sizeof(int), mbufItems
);
1978 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
1979 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
1980 n
= key_gather_mbuf(m
, mhp
, 2, sizeof(mbufItems
)/sizeof(int), mbufItems
);
1983 return key_senderror(so
, m
, ENOBUFS
);
1985 if (n
->m_len
< sizeof(*newmsg
)) {
1986 n
= m_pullup(n
, sizeof(*newmsg
));
1988 return key_senderror(so
, m
, ENOBUFS
);
1990 newmsg
= mtod(n
, struct sadb_msg
*);
1991 newmsg
->sadb_msg_errno
= 0;
1992 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
1995 mpolicy
= m_pulldown(n
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)),
1996 sizeof(*xpl
), &off
);
1997 if (mpolicy
== NULL
) {
1998 /* n is already freed */
1999 return key_senderror(so
, m
, ENOBUFS
);
2001 xpl
= (struct sadb_x_policy
*)(mtod(mpolicy
, caddr_t
) + off
);
2002 if (xpl
->sadb_x_policy_exttype
!= SADB_X_EXT_POLICY
) {
2004 return key_senderror(so
, m
, EINVAL
);
2006 xpl
->sadb_x_policy_id
= newsp
->id
;
2009 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2014 * get new policy id.
2022 u_int32_t newid
= 0;
2023 int count
= key_spi_trycnt
; /* XXX */
2024 struct secpolicy
*sp
;
2026 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2028 /* when requesting to allocate spi ranged */
2030 newid
= (policy_id
= (policy_id
== ~0 ? 1 : policy_id
+ 1));
2032 if ((sp
= key_getspbyid(newid
)) == NULL
)
2038 if (count
== 0 || newid
== 0) {
2039 ipseclog((LOG_DEBUG
, "key_getnewspid: to allocate policy id is failed.\n"));
2047 * SADB_SPDDELETE processing
2049 * <base, address(SD), policy(*)>
2050 * from the user(?), and set SADB_SASTATE_DEAD,
2052 * <base, address(SD), policy(*)>
2054 * policy(*) including direction of policy.
2056 * m will always be freed.
2059 key_spddelete(so
, m
, mhp
)
2062 const struct sadb_msghdr
*mhp
;
2064 struct sadb_address
*src0
, *dst0
;
2065 struct sadb_x_policy
*xpl0
;
2066 struct secpolicyindex spidx
;
2067 struct secpolicy
*sp
;
2069 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2072 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2073 panic("key_spddelete: NULL pointer is passed.\n");
2075 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
2076 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
2077 mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
) {
2078 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
2079 return key_senderror(so
, m
, EINVAL
);
2081 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
2082 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
2083 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2084 ipseclog((LOG_DEBUG
, "key_spddelete: invalid message is passed.\n"));
2085 return key_senderror(so
, m
, EINVAL
);
2088 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
2089 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
2090 xpl0
= (struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
];
2093 /* XXX boundary check against sa_len */
2094 KEY_SETSECSPIDX(xpl0
->sadb_x_policy_dir
,
2097 src0
->sadb_address_prefixlen
,
2098 dst0
->sadb_address_prefixlen
,
2099 src0
->sadb_address_proto
,
2102 /* checking the direciton. */
2103 switch (xpl0
->sadb_x_policy_dir
) {
2104 case IPSEC_DIR_INBOUND
:
2105 case IPSEC_DIR_OUTBOUND
:
2108 ipseclog((LOG_DEBUG
, "key_spddelete: Invalid SP direction.\n"));
2109 return key_senderror(so
, m
, EINVAL
);
2112 /* Is there SP in SPD ? */
2113 if ((sp
= key_getsp(&spidx
)) == NULL
) {
2114 ipseclog((LOG_DEBUG
, "key_spddelete: no SP found.\n"));
2115 return key_senderror(so
, m
, EINVAL
);
2118 /* save policy id to buffer to be returned. */
2119 xpl0
->sadb_x_policy_id
= sp
->id
;
2121 sp
->state
= IPSEC_SPSTATE_DEAD
;
2126 struct sadb_msg
*newmsg
;
2127 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_X_EXT_POLICY
,
2128 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
2130 /* create new sadb_msg to reply. */
2131 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
2133 return key_senderror(so
, m
, ENOBUFS
);
2135 newmsg
= mtod(n
, struct sadb_msg
*);
2136 newmsg
->sadb_msg_errno
= 0;
2137 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2140 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2145 * SADB_SPDDELETE2 processing
2148 * from the user(?), and set SADB_SASTATE_DEAD,
2152 * policy(*) including direction of policy.
2154 * m will always be freed.
2157 key_spddelete2(so
, m
, mhp
)
2160 const struct sadb_msghdr
*mhp
;
2163 struct secpolicy
*sp
;
2165 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2168 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2169 panic("key_spddelete2: NULL pointer is passed.\n");
2171 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2172 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2173 ipseclog((LOG_DEBUG
, "key_spddelete2: invalid message is passed.\n"));
2174 key_senderror(so
, m
, EINVAL
);
2178 id
= ((struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2180 /* Is there SP in SPD ? */
2181 if ((sp
= key_getspbyid(id
)) == NULL
) {
2182 ipseclog((LOG_DEBUG
, "key_spddelete2: no SP found id:%u.\n", id
));
2183 key_senderror(so
, m
, EINVAL
);
2186 sp
->state
= IPSEC_SPSTATE_DEAD
;
2190 struct mbuf
*n
, *nn
;
2191 struct sadb_msg
*newmsg
;
2194 /* create new sadb_msg to reply. */
2195 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2198 return key_senderror(so
, m
, ENOBUFS
);
2199 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
2200 if (n
&& len
> MHLEN
) {
2201 MCLGET(n
, M_DONTWAIT
);
2202 if ((n
->m_flags
& M_EXT
) == 0) {
2208 return key_senderror(so
, m
, ENOBUFS
);
2214 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
2215 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2219 panic("length inconsistency in key_spddelete2");
2222 n
->m_next
= m_copym(m
, mhp
->extoff
[SADB_X_EXT_POLICY
],
2223 mhp
->extlen
[SADB_X_EXT_POLICY
], M_DONTWAIT
);
2226 return key_senderror(so
, m
, ENOBUFS
);
2229 n
->m_pkthdr
.len
= 0;
2230 for (nn
= n
; nn
; nn
= nn
->m_next
)
2231 n
->m_pkthdr
.len
+= nn
->m_len
;
2233 newmsg
= mtod(n
, struct sadb_msg
*);
2234 newmsg
->sadb_msg_errno
= 0;
2235 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
2238 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
2243 * SADB_X_GET processing
2248 * <base, address(SD), policy>
2250 * policy(*) including direction of policy.
2252 * m will always be freed.
2255 key_spdget(so
, m
, mhp
)
2258 const struct sadb_msghdr
*mhp
;
2261 struct secpolicy
*sp
;
2264 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2267 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2268 panic("key_spdget: NULL pointer is passed.\n");
2270 if (mhp
->ext
[SADB_X_EXT_POLICY
] == NULL
||
2271 mhp
->extlen
[SADB_X_EXT_POLICY
] < sizeof(struct sadb_x_policy
)) {
2272 ipseclog((LOG_DEBUG
, "key_spdget: invalid message is passed.\n"));
2273 return key_senderror(so
, m
, EINVAL
);
2276 id
= ((struct sadb_x_policy
*)mhp
->ext
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;
2278 /* Is there SP in SPD ? */
2279 if ((sp
= key_getspbyid(id
)) == NULL
) {
2280 ipseclog((LOG_DEBUG
, "key_spdget: no SP found id:%u.\n", id
));
2281 return key_senderror(so
, m
, ENOENT
);
2284 n
= key_setdumpsp(sp
, SADB_X_SPDGET
, 0, mhp
->msg
->sadb_msg_pid
);
2287 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
2289 return key_senderror(so
, m
, ENOBUFS
);
2293 * SADB_X_SPDACQUIRE processing.
2294 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2297 * to KMD, and expect to receive
2298 * <base> with SADB_X_SPDACQUIRE if error occurred,
2301 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2302 * policy(*) is without policy requests.
2305 * others: error number
2309 struct secpolicy
*sp
;
2311 struct mbuf
*result
= NULL
, *m
;
2312 struct secspacq
*newspacq
;
2315 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2319 panic("key_spdacquire: NULL pointer is passed.\n");
2320 if (sp
->req
!= NULL
)
2321 panic("key_spdacquire: called but there is request.\n");
2322 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
2323 panic("key_spdacquire: policy mismathed. IPsec is expected.\n");
2325 /* get a entry to check whether sent message or not. */
2326 if ((newspacq
= key_getspacq(&sp
->spidx
)) != NULL
) {
2327 if (key_blockacq_count
< newspacq
->count
) {
2328 /* reset counter and do send message. */
2329 newspacq
->count
= 0;
2331 /* increment counter and do nothing. */
2336 /* make new entry for blocking to send SADB_ACQUIRE. */
2337 if ((newspacq
= key_newspacq(&sp
->spidx
)) == NULL
)
2340 /* add to acqtree */
2341 LIST_INSERT_HEAD(&spacqtree
, newspacq
, chain
);
2344 /* create new sadb_msg to reply. */
2345 m
= key_setsadbmsg(SADB_X_SPDACQUIRE
, 0, 0, 0, 0, 0);
2352 result
->m_pkthdr
.len
= 0;
2353 for (m
= result
; m
; m
= m
->m_next
)
2354 result
->m_pkthdr
.len
+= m
->m_len
;
2356 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2357 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2359 return key_sendup_mbuf(NULL
, m
, KEY_SENDUP_REGISTERED
);
2368 * SADB_SPDFLUSH processing
2371 * from the user, and free all entries in secpctree.
2375 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2377 * m will always be freed.
2380 key_spdflush(so
, m
, mhp
)
2383 const struct sadb_msghdr
*mhp
;
2385 struct sadb_msg
*newmsg
;
2386 struct secpolicy
*sp
;
2389 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2392 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2393 panic("key_spdflush: NULL pointer is passed.\n");
2395 if (m
->m_len
!= PFKEY_ALIGN8(sizeof(struct sadb_msg
)))
2396 return key_senderror(so
, m
, EINVAL
);
2398 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2399 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2400 sp
->state
= IPSEC_SPSTATE_DEAD
;
2404 if (sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
2405 ipseclog((LOG_DEBUG
, "key_spdflush: No more memory.\n"));
2406 return key_senderror(so
, m
, ENOBUFS
);
2412 m
->m_pkthdr
.len
= m
->m_len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
2413 newmsg
= mtod(m
, struct sadb_msg
*);
2414 newmsg
->sadb_msg_errno
= 0;
2415 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
2417 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
2421 * SADB_SPDDUMP processing
2424 * from the user, and dump all SP leaves
2429 * m will always be freed.
2432 key_spddump(so
, m
, mhp
)
2435 const struct sadb_msghdr
*mhp
;
2437 struct secpolicy
*sp
;
2442 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2445 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
2446 panic("key_spddump: NULL pointer is passed.\n");
2448 /* search SPD entry and get buffer size. */
2450 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2451 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2457 return key_senderror(so
, m
, ENOENT
);
2459 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
2460 LIST_FOREACH(sp
, &sptree
[dir
], chain
) {
2462 n
= key_setdumpsp(sp
, SADB_X_SPDDUMP
, cnt
,
2463 mhp
->msg
->sadb_msg_pid
);
2466 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
2474 static struct mbuf
*
2475 key_setdumpsp(sp
, type
, seq
, pid
)
2476 struct secpolicy
*sp
;
2480 struct mbuf
*result
= NULL
, *m
;
2482 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2484 m
= key_setsadbmsg(type
, 0, SADB_SATYPE_UNSPEC
, seq
, pid
, sp
->refcnt
);
2489 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
2490 (struct sockaddr
*)&sp
->spidx
.src
, sp
->spidx
.prefs
,
2491 sp
->spidx
.ul_proto
);
2496 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
2497 (struct sockaddr
*)&sp
->spidx
.dst
, sp
->spidx
.prefd
,
2498 sp
->spidx
.ul_proto
);
2508 if ((result
->m_flags
& M_PKTHDR
) == 0)
2511 if (result
->m_len
< sizeof(struct sadb_msg
)) {
2512 result
= m_pullup(result
, sizeof(struct sadb_msg
));
2517 result
->m_pkthdr
.len
= 0;
2518 for (m
= result
; m
; m
= m
->m_next
)
2519 result
->m_pkthdr
.len
+= m
->m_len
;
2521 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2522 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2532 * get PFKEY message length for security policy and request.
2535 key_getspreqmsglen(sp
)
2536 struct secpolicy
*sp
;
2540 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2542 tlen
= sizeof(struct sadb_x_policy
);
2544 /* if is the policy for ipsec ? */
2545 if (sp
->policy
!= IPSEC_POLICY_IPSEC
)
2548 /* get length of ipsec requests */
2550 struct ipsecrequest
*isr
;
2553 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2554 len
= sizeof(struct sadb_x_ipsecrequest
)
2555 + isr
->saidx
.src
.ss_len
2556 + isr
->saidx
.dst
.ss_len
;
2558 tlen
+= PFKEY_ALIGN8(len
);
2566 * SADB_SPDEXPIRE processing
2568 * <base, address(SD), lifetime(CH), policy>
2572 * others : error number
2576 struct secpolicy
*sp
;
2579 struct mbuf
*result
= NULL
, *m
;
2582 struct sadb_lifetime
*lt
;
2584 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2588 panic("key_spdexpire: NULL pointer is passed.\n");
2590 /* set msg header */
2591 m
= key_setsadbmsg(SADB_X_SPDEXPIRE
, 0, 0, 0, 0, 0);
2598 /* create lifetime extension (current and hard) */
2599 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
2600 m
= key_alloc_mbuf(len
);
2601 if (!m
|| m
->m_next
) { /*XXX*/
2607 bzero(mtod(m
, caddr_t
), len
);
2608 lt
= mtod(m
, struct sadb_lifetime
*);
2609 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
2610 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
2611 lt
->sadb_lifetime_allocations
= 0;
2612 lt
->sadb_lifetime_bytes
= 0;
2613 lt
->sadb_lifetime_addtime
= sp
->created
;
2614 lt
->sadb_lifetime_usetime
= sp
->lastused
;
2615 lt
= (struct sadb_lifetime
*)(mtod(m
, caddr_t
) + len
/ 2);
2616 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
2617 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
2618 lt
->sadb_lifetime_allocations
= 0;
2619 lt
->sadb_lifetime_bytes
= 0;
2620 lt
->sadb_lifetime_addtime
= sp
->lifetime
;
2621 lt
->sadb_lifetime_usetime
= sp
->validtime
;
2624 /* set sadb_address for source */
2625 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
2626 (struct sockaddr
*)&sp
->spidx
.src
,
2627 sp
->spidx
.prefs
, sp
->spidx
.ul_proto
);
2634 /* set sadb_address for destination */
2635 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
2636 (struct sockaddr
*)&sp
->spidx
.dst
,
2637 sp
->spidx
.prefd
, sp
->spidx
.ul_proto
);
2652 if ((result
->m_flags
& M_PKTHDR
) == 0) {
2657 if (result
->m_len
< sizeof(struct sadb_msg
)) {
2658 result
= m_pullup(result
, sizeof(struct sadb_msg
));
2659 if (result
== NULL
) {
2665 result
->m_pkthdr
.len
= 0;
2666 for (m
= result
; m
; m
= m
->m_next
)
2667 result
->m_pkthdr
.len
+= m
->m_len
;
2669 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
2670 PFKEY_UNIT64(result
->m_pkthdr
.len
);
2672 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
2680 /* %%% SAD management */
2682 * allocating a memory for new SA head, and copy from the values of mhp.
2683 * OUT: NULL : failure due to the lack of memory.
2684 * others : pointer to new SA head.
2686 static struct secashead
*
2688 struct secasindex
*saidx
;
2690 struct secashead
*newsah
;
2692 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2696 panic("key_newsaidx: NULL pointer is passed.\n");
2698 newsah
= keydb_newsecashead();
2702 bcopy(saidx
, &newsah
->saidx
, sizeof(newsah
->saidx
));
2704 /* add to saidxtree */
2705 newsah
->state
= SADB_SASTATE_MATURE
;
2706 LIST_INSERT_HEAD(&sahtree
, newsah
, chain
);
2712 * delete SA index and all SA registerd.
2716 struct secashead
*sah
;
2718 struct secasvar
*sav
, *nextsav
;
2719 u_int stateidx
, state
;
2723 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2727 panic("key_delsah: NULL pointer is passed.\n");
2729 s
= splnet(); /*called from softclock()*/
2731 /* searching all SA registerd in the secindex. */
2733 stateidx
< _ARRAYLEN(saorder_state_any
);
2736 state
= saorder_state_any
[stateidx
];
2737 for (sav
= (struct secasvar
*)LIST_FIRST(&sah
->savtree
[state
]);
2741 nextsav
= LIST_NEXT(sav
, chain
);
2743 if (sav
->refcnt
> 0) {
2744 /* give up to delete this sa */
2750 KEY_CHKSASTATE(state
, sav
->state
, "key_delsah");
2754 /* remove back pointer */
2760 /* don't delete sah only if there are savs. */
2766 if (sah
->sa_route
.ro_rt
) {
2767 rtfree(sah
->sa_route
.ro_rt
);
2768 sah
->sa_route
.ro_rt
= (struct rtentry
*)NULL
;
2771 /* remove from tree of SA index */
2772 if (__LIST_CHAINED(sah
))
2773 LIST_REMOVE(sah
, chain
);
2782 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2783 * and copy the values of mhp into new buffer.
2784 * When SAD message type is GETSPI:
2785 * to set sequence number from acq_seq++,
2786 * to set zero to SPI.
2787 * not to call key_setsava().
2789 * others : pointer to new secasvar.
2791 * does not modify mbuf. does not free mbuf on error.
2793 static struct secasvar
*
2794 key_newsav(m
, mhp
, sah
, errp
)
2796 const struct sadb_msghdr
*mhp
;
2797 struct secashead
*sah
;
2800 struct secasvar
*newsav
;
2801 const struct sadb_sa
*xsa
;
2803 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2806 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
|| sah
== NULL
)
2807 panic("key_newsa: NULL pointer is passed.\n");
2809 KMALLOC(newsav
, struct secasvar
*, sizeof(struct secasvar
));
2810 if (newsav
== NULL
) {
2811 ipseclog((LOG_DEBUG
, "key_newsa: No more memory.\n"));
2815 bzero((caddr_t
)newsav
, sizeof(struct secasvar
));
2817 switch (mhp
->msg
->sadb_msg_type
) {
2819 key_setspi(newsav
, 0);
2821 #if IPSEC_DOSEQCHECK
2822 /* sync sequence number */
2823 if (mhp
->msg
->sadb_msg_seq
== 0)
2825 (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
2828 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
2833 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
2835 ipseclog((LOG_DEBUG
, "key_newsa: invalid message is passed.\n"));
2839 xsa
= (const struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
2840 key_setspi(newsav
, xsa
->sadb_sa_spi
);
2841 newsav
->seq
= mhp
->msg
->sadb_msg_seq
;
2849 /* copy sav values */
2850 if (mhp
->msg
->sadb_msg_type
!= SADB_GETSPI
) {
2851 *errp
= key_setsaval(newsav
, m
, mhp
);
2862 newsav
->created
= tv
.tv_sec
;
2865 newsav
->pid
= mhp
->msg
->sadb_msg_pid
;
2870 newsav
->state
= SADB_SASTATE_LARVAL
;
2871 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
2878 * free() SA variable entry.
2882 struct secasvar
*sav
;
2884 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2888 panic("key_delsav: NULL pointer is passed.\n");
2890 if (sav
->refcnt
> 0)
2891 return; /* can't free */
2893 /* remove from SA header */
2894 if (__LIST_CHAINED(sav
))
2895 LIST_REMOVE(sav
, chain
);
2897 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
)
2898 LIST_REMOVE(sav
, spihash
);
2900 if (sav
->key_auth
!= NULL
) {
2901 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
2902 KFREE(sav
->key_auth
);
2903 sav
->key_auth
= NULL
;
2905 if (sav
->key_enc
!= NULL
) {
2906 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
2907 KFREE(sav
->key_enc
);
2908 sav
->key_enc
= NULL
;
2911 bzero(sav
->sched
, sav
->schedlen
);
2915 if (sav
->replay
!= NULL
) {
2916 keydb_delsecreplay(sav
->replay
);
2919 if (sav
->lft_c
!= NULL
) {
2923 if (sav
->lft_h
!= NULL
) {
2927 if (sav
->lft_s
!= NULL
) {
2931 if (sav
->iv
!= NULL
) {
2945 * others : found, pointer to a SA.
2947 static struct secashead
*
2949 struct secasindex
*saidx
;
2951 struct secashead
*sah
;
2953 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2955 LIST_FOREACH(sah
, &sahtree
, chain
) {
2956 if (sah
->state
== SADB_SASTATE_DEAD
)
2958 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_REQID
))
2966 * check not to be duplicated SPI.
2967 * NOTE: this function is too slow due to searching all SAD.
2970 * others : found, pointer to a SA.
2972 static struct secasvar
*
2973 key_checkspidup(saidx
, spi
)
2974 struct secasindex
*saidx
;
2977 struct secasvar
*sav
;
2978 u_int stateidx
, state
;
2980 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2982 /* check address family */
2983 if (saidx
->src
.ss_family
!= saidx
->dst
.ss_family
) {
2984 ipseclog((LOG_DEBUG
, "key_checkspidup: address family mismatched.\n"));
2989 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
2990 if (sav
->spi
!= spi
)
2993 stateidx
< _ARRAYLEN(saorder_state_alive
);
2995 state
= saorder_state_alive
[stateidx
];
2996 if (sav
->state
== state
&&
2997 key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
))
3006 key_setspi(sav
, spi
)
3007 struct secasvar
*sav
;
3012 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
)
3013 LIST_REMOVE(sav
, spihash
);
3014 LIST_INSERT_HEAD(&spihash
[SPIHASH(spi
)], sav
, spihash
);
3019 * search SAD litmited alive SA, protocol, SPI.
3022 * others : found, pointer to a SA.
3024 static struct secasvar
*
3025 key_getsavbyspi(sah
, spi
)
3026 struct secashead
*sah
;
3029 struct secasvar
*sav
, *match
;
3030 u_int stateidx
, state
, matchidx
;
3033 matchidx
= _ARRAYLEN(saorder_state_alive
);
3034 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
3035 if (sav
->spi
!= spi
)
3037 if (sav
->sah
!= sah
)
3039 for (stateidx
= 0; stateidx
< matchidx
; stateidx
++) {
3040 state
= saorder_state_alive
[stateidx
];
3041 if (sav
->state
== state
) {
3043 matchidx
= stateidx
;
3053 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
3054 * You must update these if need.
3058 * does not modify mbuf. does not free mbuf on error.
3061 key_setsaval(sav
, m
, mhp
)
3062 struct secasvar
*sav
;
3064 const struct sadb_msghdr
*mhp
;
3067 const struct esp_algorithm
*algo
;
3072 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3075 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
3076 panic("key_setsaval: NULL pointer is passed.\n");
3078 /* initialization */
3080 sav
->key_auth
= NULL
;
3081 sav
->key_enc
= NULL
;
3088 sav
->remote_ike_port
= 0;
3089 sav
->natt_last_activity
= natt_now
;
3092 if (mhp
->ext
[SADB_EXT_SA
] != NULL
) {
3093 const struct sadb_sa
*sa0
;
3095 sa0
= (const struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
3096 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(*sa0
)) {
3101 sav
->alg_auth
= sa0
->sadb_sa_auth
;
3102 sav
->alg_enc
= sa0
->sadb_sa_encrypt
;
3103 sav
->flags
= sa0
->sadb_sa_flags
;
3106 * Verify that a nat-traversal port was specified if
3107 * the nat-traversal flag is set.
3109 if ((sav
->flags
& SADB_X_EXT_NATT
) != 0) {
3110 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa_2
) ||
3111 ((struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
== 0) {
3115 sav
->remote_ike_port
= ((struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
;
3119 if ((sa0
->sadb_sa_flags
& SADB_X_EXT_OLD
) == 0) {
3120 sav
->replay
= keydb_newsecreplay(sa0
->sadb_sa_replay
);
3121 if (sav
->replay
== NULL
) {
3122 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3129 /* Authentication keys */
3130 if (mhp
->ext
[SADB_EXT_KEY_AUTH
] != NULL
) {
3131 const struct sadb_key
*key0
;
3134 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_AUTH
];
3135 len
= mhp
->extlen
[SADB_EXT_KEY_AUTH
];
3138 if (len
< sizeof(*key0
)) {
3142 switch (mhp
->msg
->sadb_msg_satype
) {
3143 case SADB_SATYPE_AH
:
3144 case SADB_SATYPE_ESP
:
3145 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
3146 sav
->alg_auth
!= SADB_X_AALG_NULL
)
3149 case SADB_X_SATYPE_IPCOMP
:
3155 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_auth values.\n"));
3159 sav
->key_auth
= (struct sadb_key
*)key_newbuf(key0
, len
);
3160 if (sav
->key_auth
== NULL
) {
3161 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3167 /* Encryption key */
3168 if (mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] != NULL
) {
3169 const struct sadb_key
*key0
;
3172 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_ENCRYPT
];
3173 len
= mhp
->extlen
[SADB_EXT_KEY_ENCRYPT
];
3176 if (len
< sizeof(*key0
)) {
3180 switch (mhp
->msg
->sadb_msg_satype
) {
3181 case SADB_SATYPE_ESP
:
3182 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
3183 sav
->alg_enc
!= SADB_EALG_NULL
) {
3187 sav
->key_enc
= (struct sadb_key
*)key_newbuf(key0
, len
);
3188 if (sav
->key_enc
== NULL
) {
3189 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3194 case SADB_X_SATYPE_IPCOMP
:
3195 if (len
!= PFKEY_ALIGN8(sizeof(struct sadb_key
)))
3197 sav
->key_enc
= NULL
; /*just in case*/
3199 case SADB_SATYPE_AH
:
3205 ipseclog((LOG_DEBUG
, "key_setsatval: invalid key_enc value.\n"));
3213 switch (mhp
->msg
->sadb_msg_satype
) {
3214 case SADB_SATYPE_ESP
:
3216 algo
= esp_algorithm_lookup(sav
->alg_enc
);
3217 if (algo
&& algo
->ivlen
)
3218 sav
->ivlen
= (*algo
->ivlen
)(algo
, sav
);
3219 if (sav
->ivlen
== 0)
3221 KMALLOC(sav
->iv
, caddr_t
, sav
->ivlen
);
3223 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3229 key_randomfill(sav
->iv
, sav
->ivlen
);
3232 case SADB_SATYPE_AH
:
3233 case SADB_X_SATYPE_IPCOMP
:
3236 ipseclog((LOG_DEBUG
, "key_setsaval: invalid SA type.\n"));
3243 sav
->created
= tv
.tv_sec
;
3245 /* make lifetime for CURRENT */
3246 KMALLOC(sav
->lft_c
, struct sadb_lifetime
*,
3247 sizeof(struct sadb_lifetime
));
3248 if (sav
->lft_c
== NULL
) {
3249 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3256 sav
->lft_c
->sadb_lifetime_len
=
3257 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3258 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
3259 sav
->lft_c
->sadb_lifetime_allocations
= 0;
3260 sav
->lft_c
->sadb_lifetime_bytes
= 0;
3261 sav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
3262 sav
->lft_c
->sadb_lifetime_usetime
= 0;
3264 /* lifetimes for HARD and SOFT */
3266 const struct sadb_lifetime
*lft0
;
3268 lft0
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
3270 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
] < sizeof(*lft0
)) {
3274 sav
->lft_h
= (struct sadb_lifetime
*)key_newbuf(lft0
,
3276 if (sav
->lft_h
== NULL
) {
3277 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3281 /* to be initialize ? */
3284 lft0
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_SOFT
];
3286 if (mhp
->extlen
[SADB_EXT_LIFETIME_SOFT
] < sizeof(*lft0
)) {
3290 sav
->lft_s
= (struct sadb_lifetime
*)key_newbuf(lft0
,
3292 if (sav
->lft_s
== NULL
) {
3293 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3297 /* to be initialize ? */
3304 /* initialization */
3305 if (sav
->replay
!= NULL
) {
3306 keydb_delsecreplay(sav
->replay
);
3309 if (sav
->key_auth
!= NULL
) {
3310 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
3311 KFREE(sav
->key_auth
);
3312 sav
->key_auth
= NULL
;
3314 if (sav
->key_enc
!= NULL
) {
3315 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
3316 KFREE(sav
->key_enc
);
3317 sav
->key_enc
= NULL
;
3320 bzero(sav
->sched
, sav
->schedlen
);
3324 if (sav
->iv
!= NULL
) {
3328 if (sav
->lft_c
!= NULL
) {
3332 if (sav
->lft_h
!= NULL
) {
3336 if (sav
->lft_s
!= NULL
) {
3345 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3351 struct secasvar
*sav
;
3354 int checkmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3355 int mustmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3359 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3361 /* check SPI value */
3362 switch (sav
->sah
->saidx
.proto
) {
3365 if (ntohl(sav
->spi
) >= 0 && ntohl(sav
->spi
) <= 255) {
3366 ipseclog((LOG_DEBUG
,
3367 "key_mature: illegal range of SPI %u.\n",
3368 (u_int32_t
)ntohl(sav
->spi
)));
3375 switch (sav
->sah
->saidx
.proto
) {
3378 if ((sav
->flags
& SADB_X_EXT_OLD
)
3379 && (sav
->flags
& SADB_X_EXT_DERIV
)) {
3380 ipseclog((LOG_DEBUG
, "key_mature: "
3381 "invalid flag (derived) given to old-esp.\n"));
3384 if (sav
->alg_auth
== SADB_AALG_NONE
)
3392 if (sav
->flags
& SADB_X_EXT_DERIV
) {
3393 ipseclog((LOG_DEBUG
, "key_mature: "
3394 "invalid flag (derived) given to AH SA.\n"));
3397 if (sav
->alg_enc
!= SADB_EALG_NONE
) {
3398 ipseclog((LOG_DEBUG
, "key_mature: "
3399 "protocol and algorithm mismated.\n"));
3405 case IPPROTO_IPCOMP
:
3406 if (sav
->alg_auth
!= SADB_AALG_NONE
) {
3407 ipseclog((LOG_DEBUG
, "key_mature: "
3408 "protocol and algorithm mismated.\n"));
3411 if ((sav
->flags
& SADB_X_EXT_RAWCPI
) == 0
3412 && ntohl(sav
->spi
) >= 0x10000) {
3413 ipseclog((LOG_DEBUG
, "key_mature: invalid cpi for IPComp.\n"));
3420 ipseclog((LOG_DEBUG
, "key_mature: Invalid satype.\n"));
3421 return EPROTONOSUPPORT
;
3424 /* check authentication algorithm */
3425 if ((checkmask
& 2) != 0) {
3426 const struct ah_algorithm
*algo
;
3429 algo
= ah_algorithm_lookup(sav
->alg_auth
);
3431 ipseclog((LOG_DEBUG
,"key_mature: "
3432 "unknown authentication algorithm.\n"));
3436 /* algorithm-dependent check */
3438 keylen
= sav
->key_auth
->sadb_key_bits
;
3441 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
3442 ipseclog((LOG_DEBUG
,
3443 "key_mature: invalid AH key length %d "
3444 "(%d-%d allowed)\n",
3445 keylen
, algo
->keymin
, algo
->keymax
));
3450 if ((*algo
->mature
)(sav
)) {
3451 /* message generated in per-algorithm function*/
3454 mature
= SADB_SATYPE_AH
;
3457 if ((mustmask
& 2) != 0 && mature
!= SADB_SATYPE_AH
) {
3458 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for AH\n"));
3463 /* check encryption algorithm */
3464 if ((checkmask
& 1) != 0) {
3466 const struct esp_algorithm
*algo
;
3469 algo
= esp_algorithm_lookup(sav
->alg_enc
);
3471 ipseclog((LOG_DEBUG
, "key_mature: unknown encryption algorithm.\n"));
3475 /* algorithm-dependent check */
3477 keylen
= sav
->key_enc
->sadb_key_bits
;
3480 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
3481 ipseclog((LOG_DEBUG
,
3482 "key_mature: invalid ESP key length %d "
3483 "(%d-%d allowed)\n",
3484 keylen
, algo
->keymin
, algo
->keymax
));
3489 if ((*algo
->mature
)(sav
)) {
3490 /* message generated in per-algorithm function*/
3493 mature
= SADB_SATYPE_ESP
;
3496 if ((mustmask
& 1) != 0 && mature
!= SADB_SATYPE_ESP
) {
3497 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for ESP\n"));
3501 ipseclog((LOG_DEBUG
, "key_mature: ESP not supported in this configuration\n"));
3506 /* check compression algorithm */
3507 if ((checkmask
& 4) != 0) {
3508 const struct ipcomp_algorithm
*algo
;
3510 /* algorithm-dependent check */
3511 algo
= ipcomp_algorithm_lookup(sav
->alg_enc
);
3513 ipseclog((LOG_DEBUG
, "key_mature: unknown compression algorithm.\n"));
3518 key_sa_chgstate(sav
, SADB_SASTATE_MATURE
);
3524 * subroutine for SADB_GET and SADB_DUMP.
3526 static struct mbuf
*
3527 key_setdumpsa(sav
, type
, satype
, seq
, pid
)
3528 struct secasvar
*sav
;
3529 u_int8_t type
, satype
;
3532 struct mbuf
*result
= NULL
, *tres
= NULL
, *m
;
3537 SADB_EXT_SA
, SADB_X_EXT_SA2
,
3538 SADB_EXT_LIFETIME_HARD
, SADB_EXT_LIFETIME_SOFT
,
3539 SADB_EXT_LIFETIME_CURRENT
, SADB_EXT_ADDRESS_SRC
,
3540 SADB_EXT_ADDRESS_DST
, SADB_EXT_ADDRESS_PROXY
, SADB_EXT_KEY_AUTH
,
3541 SADB_EXT_KEY_ENCRYPT
, SADB_EXT_IDENTITY_SRC
,
3542 SADB_EXT_IDENTITY_DST
, SADB_EXT_SENSITIVITY
,
3545 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3547 m
= key_setsadbmsg(type
, 0, satype
, seq
, pid
, sav
->refcnt
);
3552 for (i
= sizeof(dumporder
)/sizeof(dumporder
[0]) - 1; i
>= 0; i
--) {
3555 switch (dumporder
[i
]) {
3557 m
= key_setsadbsa(sav
);
3562 case SADB_X_EXT_SA2
:
3563 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
3564 sav
->replay
? sav
->replay
->count
: 0,
3565 sav
->sah
->saidx
.reqid
);
3570 case SADB_EXT_ADDRESS_SRC
:
3571 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
3572 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
3573 FULLMASK
, IPSEC_ULPROTO_ANY
);
3578 case SADB_EXT_ADDRESS_DST
:
3579 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
3580 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
3581 FULLMASK
, IPSEC_ULPROTO_ANY
);
3586 case SADB_EXT_KEY_AUTH
:
3589 l
= PFKEY_UNUNIT64(sav
->key_auth
->sadb_key_len
);
3593 case SADB_EXT_KEY_ENCRYPT
:
3596 l
= PFKEY_UNUNIT64(sav
->key_enc
->sadb_key_len
);
3600 case SADB_EXT_LIFETIME_CURRENT
:
3603 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_c
)->sadb_ext_len
);
3607 case SADB_EXT_LIFETIME_HARD
:
3610 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_h
)->sadb_ext_len
);
3614 case SADB_EXT_LIFETIME_SOFT
:
3617 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_s
)->sadb_ext_len
);
3621 case SADB_EXT_ADDRESS_PROXY
:
3622 case SADB_EXT_IDENTITY_SRC
:
3623 case SADB_EXT_IDENTITY_DST
:
3624 /* XXX: should we brought from SPD ? */
3625 case SADB_EXT_SENSITIVITY
:
3630 if ((!m
&& !p
) || (m
&& p
))
3633 M_PREPEND(tres
, l
, M_DONTWAIT
);
3636 bcopy(p
, mtod(tres
, caddr_t
), l
);
3640 m
= key_alloc_mbuf(l
);
3643 m_copyback(m
, 0, l
, p
);
3651 m_cat(result
, tres
);
3653 if (result
->m_len
< sizeof(struct sadb_msg
)) {
3654 result
= m_pullup(result
, sizeof(struct sadb_msg
));
3659 result
->m_pkthdr
.len
= 0;
3660 for (m
= result
; m
; m
= m
->m_next
)
3661 result
->m_pkthdr
.len
+= m
->m_len
;
3663 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3664 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3675 * set data into sadb_msg.
3677 static struct mbuf
*
3678 key_setsadbmsg(type
, tlen
, satype
, seq
, pid
, reserved
)
3679 u_int8_t type
, satype
;
3689 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
3692 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
3693 if (m
&& len
> MHLEN
) {
3694 MCLGET(m
, M_DONTWAIT
);
3695 if ((m
->m_flags
& M_EXT
) == 0) {
3702 m
->m_pkthdr
.len
= m
->m_len
= len
;
3705 p
= mtod(m
, struct sadb_msg
*);
3708 p
->sadb_msg_version
= PF_KEY_V2
;
3709 p
->sadb_msg_type
= type
;
3710 p
->sadb_msg_errno
= 0;
3711 p
->sadb_msg_satype
= satype
;
3712 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
3713 p
->sadb_msg_reserved
= reserved
;
3714 p
->sadb_msg_seq
= seq
;
3715 p
->sadb_msg_pid
= (u_int32_t
)pid
;
3721 * copy secasvar data into sadb_address.
3723 static struct mbuf
*
3725 struct secasvar
*sav
;
3731 len
= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
3732 m
= key_alloc_mbuf(len
);
3733 if (!m
|| m
->m_next
) { /*XXX*/
3739 p
= mtod(m
, struct sadb_sa
*);
3742 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
3743 p
->sadb_sa_exttype
= SADB_EXT_SA
;
3744 p
->sadb_sa_spi
= sav
->spi
;
3745 p
->sadb_sa_replay
= (sav
->replay
!= NULL
? sav
->replay
->wsize
: 0);
3746 p
->sadb_sa_state
= sav
->state
;
3747 p
->sadb_sa_auth
= sav
->alg_auth
;
3748 p
->sadb_sa_encrypt
= sav
->alg_enc
;
3749 p
->sadb_sa_flags
= sav
->flags
;
3755 * set data into sadb_address.
3757 static struct mbuf
*
3758 key_setsadbaddr(exttype
, saddr
, prefixlen
, ul_proto
)
3760 struct sockaddr
*saddr
;
3765 struct sadb_address
*p
;
3768 len
= PFKEY_ALIGN8(sizeof(struct sadb_address
)) +
3769 PFKEY_ALIGN8(saddr
->sa_len
);
3770 m
= key_alloc_mbuf(len
);
3771 if (!m
|| m
->m_next
) { /*XXX*/
3777 p
= mtod(m
, struct sadb_address
*);
3780 p
->sadb_address_len
= PFKEY_UNIT64(len
);
3781 p
->sadb_address_exttype
= exttype
;
3782 p
->sadb_address_proto
= ul_proto
;
3783 if (prefixlen
== FULLMASK
) {
3784 switch (saddr
->sa_family
) {
3786 prefixlen
= sizeof(struct in_addr
) << 3;
3789 prefixlen
= sizeof(struct in6_addr
) << 3;
3795 p
->sadb_address_prefixlen
= prefixlen
;
3796 p
->sadb_address_reserved
= 0;
3799 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_address
)),
3807 * set data into sadb_ident.
3809 static struct mbuf
*
3810 key_setsadbident(exttype
, idtype
, string
, stringlen
, id
)
3811 u_int16_t exttype
, idtype
;
3817 struct sadb_ident
*p
;
3820 len
= PFKEY_ALIGN8(sizeof(struct sadb_ident
)) + PFKEY_ALIGN8(stringlen
);
3821 m
= key_alloc_mbuf(len
);
3822 if (!m
|| m
->m_next
) { /*XXX*/
3828 p
= mtod(m
, struct sadb_ident
*);
3831 p
->sadb_ident_len
= PFKEY_UNIT64(len
);
3832 p
->sadb_ident_exttype
= exttype
;
3833 p
->sadb_ident_type
= idtype
;
3834 p
->sadb_ident_reserved
= 0;
3835 p
->sadb_ident_id
= id
;
3838 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_ident
)),
3846 * set data into sadb_x_sa2.
3848 static struct mbuf
*
3849 key_setsadbxsa2(mode
, seq
, reqid
)
3851 u_int32_t seq
, reqid
;
3854 struct sadb_x_sa2
*p
;
3857 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_sa2
));
3858 m
= key_alloc_mbuf(len
);
3859 if (!m
|| m
->m_next
) { /*XXX*/
3865 p
= mtod(m
, struct sadb_x_sa2
*);
3868 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
3869 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
3870 p
->sadb_x_sa2_mode
= mode
;
3871 p
->sadb_x_sa2_reserved1
= 0;
3872 p
->sadb_x_sa2_reserved2
= 0;
3873 p
->sadb_x_sa2_sequence
= seq
;
3874 p
->sadb_x_sa2_reqid
= reqid
;
3880 * set data into sadb_x_policy
3882 static struct mbuf
*
3883 key_setsadbxpolicy(type
, dir
, id
)
3889 struct sadb_x_policy
*p
;
3892 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_policy
));
3893 m
= key_alloc_mbuf(len
);
3894 if (!m
|| m
->m_next
) { /*XXX*/
3900 p
= mtod(m
, struct sadb_x_policy
*);
3903 p
->sadb_x_policy_len
= PFKEY_UNIT64(len
);
3904 p
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3905 p
->sadb_x_policy_type
= type
;
3906 p
->sadb_x_policy_dir
= dir
;
3907 p
->sadb_x_policy_id
= id
;
3914 * copy a buffer into the new buffer allocated.
3917 key_newbuf(src
, len
)
3923 KMALLOC(new, caddr_t
, len
);
3925 ipseclog((LOG_DEBUG
, "key_newbuf: No more memory.\n"));
3928 bcopy(src
, new, len
);
3933 /* compare my own address
3934 * OUT: 1: true, i.e. my address.
3939 struct sockaddr
*sa
;
3942 struct sockaddr_in
*sin
;
3943 struct in_ifaddr
*ia
;
3948 panic("key_ismyaddr: NULL pointer is passed.\n");
3950 switch (sa
->sa_family
) {
3953 lck_mtx_lock(rt_mtx
);
3954 sin
= (struct sockaddr_in
*)sa
;
3955 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
3956 ia
= ia
->ia_link
.tqe_next
)
3958 if (sin
->sin_family
== ia
->ia_addr
.sin_family
&&
3959 sin
->sin_len
== ia
->ia_addr
.sin_len
&&
3960 sin
->sin_addr
.s_addr
== ia
->ia_addr
.sin_addr
.s_addr
)
3962 lck_mtx_unlock(rt_mtx
);
3966 lck_mtx_unlock(rt_mtx
);
3971 return key_ismyaddr6((struct sockaddr_in6
*)sa
);
3980 * compare my own address for IPv6.
3983 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3985 #include <netinet6/in6_var.h>
3989 struct sockaddr_in6
*sin6
;
3991 struct in6_ifaddr
*ia
;
3992 struct in6_multi
*in6m
;
3994 lck_mtx_lock(nd6_mutex
);
3995 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
3996 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
3997 (struct sockaddr
*)&ia
->ia_addr
, 0) == 0) {
3998 lck_mtx_unlock(nd6_mutex
);
4004 * XXX why do we care about multlicast here while we don't care
4005 * about IPv4 multicast??
4009 IN6_LOOKUP_MULTI(sin6
->sin6_addr
, ia
->ia_ifp
, in6m
);
4011 lck_mtx_unlock(nd6_mutex
);
4015 lck_mtx_unlock(nd6_mutex
);
4017 /* loopback, just for safety */
4018 if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
4026 * compare two secasindex structure.
4027 * flag can specify to compare 2 saidxes.
4028 * compare two secasindex structure without both mode and reqid.
4029 * don't compare port.
4031 * saidx0: source, it can be in SAD.
4038 key_cmpsaidx(saidx0
, saidx1
, flag
)
4039 struct secasindex
*saidx0
, *saidx1
;
4043 if (saidx0
== NULL
&& saidx1
== NULL
)
4046 if (saidx0
== NULL
|| saidx1
== NULL
)
4049 if (saidx0
->proto
!= saidx1
->proto
)
4052 if (flag
== CMP_EXACTLY
) {
4053 if (saidx0
->mode
!= saidx1
->mode
)
4055 if (saidx0
->reqid
!= saidx1
->reqid
)
4057 if (bcmp(&saidx0
->src
, &saidx1
->src
, saidx0
->src
.ss_len
) != 0 ||
4058 bcmp(&saidx0
->dst
, &saidx1
->dst
, saidx0
->dst
.ss_len
) != 0)
4062 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
4063 if (flag
== CMP_MODE_REQID
4064 ||flag
== CMP_REQID
) {
4066 * If reqid of SPD is non-zero, unique SA is required.
4067 * The result must be of same reqid in this case.
4069 if (saidx1
->reqid
!= 0 && saidx0
->reqid
!= saidx1
->reqid
)
4073 if (flag
== CMP_MODE_REQID
) {
4074 if (saidx0
->mode
!= IPSEC_MODE_ANY
4075 && saidx0
->mode
!= saidx1
->mode
)
4079 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->src
,
4080 (struct sockaddr
*)&saidx1
->src
, 0) != 0) {
4083 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->dst
,
4084 (struct sockaddr
*)&saidx1
->dst
, 0) != 0) {
4093 * compare two secindex structure exactly.
4095 * spidx0: source, it is often in SPD.
4096 * spidx1: object, it is often from PFKEY message.
4102 key_cmpspidx_exactly(spidx0
, spidx1
)
4103 struct secpolicyindex
*spidx0
, *spidx1
;
4106 if (spidx0
== NULL
&& spidx1
== NULL
)
4109 if (spidx0
== NULL
|| spidx1
== NULL
)
4112 if (spidx0
->prefs
!= spidx1
->prefs
4113 || spidx0
->prefd
!= spidx1
->prefd
4114 || spidx0
->ul_proto
!= spidx1
->ul_proto
)
4117 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src
,
4118 (struct sockaddr
*)&spidx1
->src
, 1) != 0) {
4121 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst
,
4122 (struct sockaddr
*)&spidx1
->dst
, 1) != 0) {
4130 * compare two secindex structure with mask.
4132 * spidx0: source, it is often in SPD.
4133 * spidx1: object, it is often from IP header.
4139 key_cmpspidx_withmask(spidx0
, spidx1
)
4140 struct secpolicyindex
*spidx0
, *spidx1
;
4143 if (spidx0
== NULL
&& spidx1
== NULL
)
4146 if (spidx0
== NULL
|| spidx1
== NULL
)
4149 if (spidx0
->src
.ss_family
!= spidx1
->src
.ss_family
||
4150 spidx0
->dst
.ss_family
!= spidx1
->dst
.ss_family
||
4151 spidx0
->src
.ss_len
!= spidx1
->src
.ss_len
||
4152 spidx0
->dst
.ss_len
!= spidx1
->dst
.ss_len
)
4155 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4156 if (spidx0
->ul_proto
!= (u_int16_t
)IPSEC_ULPROTO_ANY
4157 && spidx0
->ul_proto
!= spidx1
->ul_proto
)
4160 switch (spidx0
->src
.ss_family
) {
4162 if (satosin(&spidx0
->src
)->sin_port
!= IPSEC_PORT_ANY
4163 && satosin(&spidx0
->src
)->sin_port
!=
4164 satosin(&spidx1
->src
)->sin_port
)
4166 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->src
)->sin_addr
,
4167 (caddr_t
)&satosin(&spidx1
->src
)->sin_addr
, spidx0
->prefs
))
4171 if (satosin6(&spidx0
->src
)->sin6_port
!= IPSEC_PORT_ANY
4172 && satosin6(&spidx0
->src
)->sin6_port
!=
4173 satosin6(&spidx1
->src
)->sin6_port
)
4176 * scope_id check. if sin6_scope_id is 0, we regard it
4177 * as a wildcard scope, which matches any scope zone ID.
4179 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
4180 satosin6(&spidx1
->src
)->sin6_scope_id
&&
4181 satosin6(&spidx0
->src
)->sin6_scope_id
!=
4182 satosin6(&spidx1
->src
)->sin6_scope_id
)
4184 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->src
)->sin6_addr
,
4185 (caddr_t
)&satosin6(&spidx1
->src
)->sin6_addr
, spidx0
->prefs
))
4190 if (bcmp(&spidx0
->src
, &spidx1
->src
, spidx0
->src
.ss_len
) != 0)
4195 switch (spidx0
->dst
.ss_family
) {
4197 if (satosin(&spidx0
->dst
)->sin_port
!= IPSEC_PORT_ANY
4198 && satosin(&spidx0
->dst
)->sin_port
!=
4199 satosin(&spidx1
->dst
)->sin_port
)
4201 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->dst
)->sin_addr
,
4202 (caddr_t
)&satosin(&spidx1
->dst
)->sin_addr
, spidx0
->prefd
))
4206 if (satosin6(&spidx0
->dst
)->sin6_port
!= IPSEC_PORT_ANY
4207 && satosin6(&spidx0
->dst
)->sin6_port
!=
4208 satosin6(&spidx1
->dst
)->sin6_port
)
4211 * scope_id check. if sin6_scope_id is 0, we regard it
4212 * as a wildcard scope, which matches any scope zone ID.
4214 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
4215 satosin6(&spidx1
->src
)->sin6_scope_id
&&
4216 satosin6(&spidx0
->dst
)->sin6_scope_id
!=
4217 satosin6(&spidx1
->dst
)->sin6_scope_id
)
4219 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->dst
)->sin6_addr
,
4220 (caddr_t
)&satosin6(&spidx1
->dst
)->sin6_addr
, spidx0
->prefd
))
4225 if (bcmp(&spidx0
->dst
, &spidx1
->dst
, spidx0
->dst
.ss_len
) != 0)
4230 /* XXX Do we check other field ? e.g. flowinfo */
4235 /* returns 0 on match */
4237 key_sockaddrcmp(sa1
, sa2
, port
)
4238 struct sockaddr
*sa1
;
4239 struct sockaddr
*sa2
;
4242 if (sa1
->sa_family
!= sa2
->sa_family
|| sa1
->sa_len
!= sa2
->sa_len
)
4245 switch (sa1
->sa_family
) {
4247 if (sa1
->sa_len
!= sizeof(struct sockaddr_in
))
4249 if (satosin(sa1
)->sin_addr
.s_addr
!=
4250 satosin(sa2
)->sin_addr
.s_addr
) {
4253 if (port
&& satosin(sa1
)->sin_port
!= satosin(sa2
)->sin_port
)
4257 if (sa1
->sa_len
!= sizeof(struct sockaddr_in6
))
4258 return 1; /*EINVAL*/
4259 if (satosin6(sa1
)->sin6_scope_id
!=
4260 satosin6(sa2
)->sin6_scope_id
) {
4263 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1
)->sin6_addr
,
4264 &satosin6(sa2
)->sin6_addr
)) {
4268 satosin6(sa1
)->sin6_port
!= satosin6(sa2
)->sin6_port
) {
4272 if (bcmp(sa1
, sa2
, sa1
->sa_len
) != 0)
4281 * compare two buffers with mask.
4285 * bits: Number of bits to compare
4291 key_bbcmp(p1
, p2
, bits
)
4297 /* XXX: This could be considerably faster if we compare a word
4298 * at a time, but it is complicated on LSB Endian machines */
4300 /* Handle null pointers */
4301 if (p1
== NULL
|| p2
== NULL
)
4311 mask
= ~((1<<(8-bits
))-1);
4312 if ((*p1
& mask
) != (*p2
& mask
))
4315 return 1; /* Match! */
4320 * scanning SPD and SAD to check status for each entries,
4321 * and do to remove or to expire.
4322 * XXX: year 2038 problem may remain.
4326 key_timehandler(void)
4334 lck_mtx_lock(sadb_mutex
);
4337 struct secpolicy
*sp
, *nextsp
;
4339 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
4340 for (sp
= LIST_FIRST(&sptree
[dir
]);
4344 nextsp
= LIST_NEXT(sp
, chain
);
4346 if (sp
->state
== IPSEC_SPSTATE_DEAD
) {
4351 if (sp
->lifetime
== 0 && sp
->validtime
== 0)
4354 /* the deletion will occur next time */
4356 && tv
.tv_sec
- sp
->created
> sp
->lifetime
)
4358 && tv
.tv_sec
- sp
->lastused
> sp
->validtime
)) {
4359 sp
->state
= IPSEC_SPSTATE_DEAD
;
4369 struct secashead
*sah
, *nextsah
;
4370 struct secasvar
*sav
, *nextsav
;
4372 for (sah
= LIST_FIRST(&sahtree
);
4376 nextsah
= LIST_NEXT(sah
, chain
);
4378 /* if sah has been dead, then delete it and process next sah. */
4379 if (sah
->state
== SADB_SASTATE_DEAD
) {
4384 /* if LARVAL entry doesn't become MATURE, delete it. */
4385 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]);
4389 nextsav
= LIST_NEXT(sav
, chain
);
4391 if (tv
.tv_sec
- sav
->created
> key_larval_lifetime
) {
4397 * If this is a NAT traversal SA with no activity,
4398 * we need to send a keep alive.
4400 * Performed outside of the loop before so we will
4401 * only ever send one keepalive. The first SA on
4402 * the list is the one that will be used for sending
4403 * traffic, so this is the one we use for determining
4404 * when to send the keepalive.
4406 sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]);
4407 if (natt_keepalive_interval
&& sav
&& (sav
->flags
& SADB_X_EXT_NATT_KEEPALIVE
) != 0 &&
4408 (natt_now
- sav
->natt_last_activity
) >= natt_keepalive_interval
) {
4409 ipsec_send_natt_keepalive(sav
);
4413 * check MATURE entry to start to send expire message
4416 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]);
4420 nextsav
= LIST_NEXT(sav
, chain
);
4422 /* we don't need to check. */
4423 if (sav
->lft_s
== NULL
)
4427 if (sav
->lft_c
== NULL
) {
4428 ipseclog((LOG_DEBUG
,"key_timehandler: "
4429 "There is no CURRENT time, why?\n"));
4433 /* check SOFT lifetime */
4434 if (sav
->lft_s
->sadb_lifetime_addtime
!= 0
4435 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
4437 * check the SA if it has been used.
4438 * when it hasn't been used, delete it.
4439 * i don't think such SA will be used.
4441 if (sav
->lft_c
->sadb_lifetime_usetime
== 0) {
4442 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4446 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
4448 * XXX If we keep to send expire
4449 * message in the status of
4450 * DYING. Do remove below code.
4456 /* check SOFT lifetime by bytes */
4458 * XXX I don't know the way to delete this SA
4459 * when new SA is installed. Caution when it's
4460 * installed too big lifetime by time.
4462 else if (sav
->lft_s
->sadb_lifetime_bytes
!= 0
4463 && sav
->lft_s
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
4465 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
4467 * XXX If we keep to send expire
4468 * message in the status of
4469 * DYING. Do remove below code.
4475 /* check DYING entry to change status to DEAD. */
4476 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]);
4480 nextsav
= LIST_NEXT(sav
, chain
);
4482 /* we don't need to check. */
4483 if (sav
->lft_h
== NULL
)
4487 if (sav
->lft_c
== NULL
) {
4488 ipseclog((LOG_DEBUG
, "key_timehandler: "
4489 "There is no CURRENT time, why?\n"));
4493 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0
4494 && tv
.tv_sec
- sav
->created
> sav
->lft_h
->sadb_lifetime_addtime
) {
4495 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4499 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4500 else if (sav
->lft_s
!= NULL
4501 && sav
->lft_s
->sadb_lifetime_addtime
!= 0
4502 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
4504 * XXX: should be checked to be
4505 * installed the valid SA.
4509 * If there is no SA then sending
4515 /* check HARD lifetime by bytes */
4516 else if (sav
->lft_h
->sadb_lifetime_bytes
!= 0
4517 && sav
->lft_h
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
4518 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4524 /* delete entry in DEAD */
4525 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]);
4529 nextsav
= LIST_NEXT(sav
, chain
);
4532 if (sav
->state
!= SADB_SASTATE_DEAD
) {
4533 ipseclog((LOG_DEBUG
, "key_timehandler: "
4534 "invalid sav->state "
4535 "(queue: %d SA: %d): "
4537 SADB_SASTATE_DEAD
, sav
->state
));
4541 * do not call key_freesav() here.
4542 * sav should already be freed, and sav->refcnt
4543 * shows other references to sav
4544 * (such as from SPD).
4550 #ifndef IPSEC_NONBLOCK_ACQUIRE
4553 struct secacq
*acq
, *nextacq
;
4555 for (acq
= LIST_FIRST(&acqtree
);
4559 nextacq
= LIST_NEXT(acq
, chain
);
4561 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
4562 && __LIST_CHAINED(acq
)) {
4563 LIST_REMOVE(acq
, chain
);
4572 struct secspacq
*acq
, *nextacq
;
4574 for (acq
= LIST_FIRST(&spacqtree
);
4578 nextacq
= LIST_NEXT(acq
, chain
);
4580 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
4581 && __LIST_CHAINED(acq
)) {
4582 LIST_REMOVE(acq
, chain
);
4588 /* initialize random seed */
4589 if (key_tick_init_random
++ > key_int_random
) {
4590 key_tick_init_random
= 0;
4596 lck_mtx_unlock(sadb_mutex
);
4597 #ifndef IPSEC_DEBUG2
4598 /* do exchange to tick time !! */
4599 (void)timeout((void *)key_timehandler
, (void *)0, hz
);
4600 #endif /* IPSEC_DEBUG2 */
4606 * to initialize a seed for random()
4612 /* Our PRNG is based on Yarrow and doesn't need to be seeded */
4619 srandom(tv
.tv_usec
);
4630 key_randomfill(&value
, sizeof(value
));
4635 key_randomfill(p
, l
)
4641 static int warn
= 1;
4644 read_random(p
, (u_int
)l
);
4647 n
= (size_t)read_random(p
, (u_int
)l
);
4651 bcopy(&v
, (u_int8_t
*)p
+ n
,
4652 l
- n
< sizeof(v
) ? l
- n
: sizeof(v
));
4656 printf("WARNING: pseudo-random number generator "
4657 "used for IPsec processing\n");
4665 * map SADB_SATYPE_* to IPPROTO_*.
4666 * if satype == SADB_SATYPE then satype is mapped to ~0.
4668 * 0: invalid satype.
4671 key_satype2proto(satype
)
4675 case SADB_SATYPE_UNSPEC
:
4676 return IPSEC_PROTO_ANY
;
4677 case SADB_SATYPE_AH
:
4679 case SADB_SATYPE_ESP
:
4681 case SADB_X_SATYPE_IPCOMP
:
4682 return IPPROTO_IPCOMP
;
4691 * map IPPROTO_* to SADB_SATYPE_*
4693 * 0: invalid protocol type.
4696 key_proto2satype(proto
)
4701 return SADB_SATYPE_AH
;
4703 return SADB_SATYPE_ESP
;
4704 case IPPROTO_IPCOMP
:
4705 return SADB_X_SATYPE_IPCOMP
;
4715 * SADB_GETSPI processing is to receive
4716 * <base, (SA2), src address, dst address, (SPI range)>
4717 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4718 * tree with the status of LARVAL, and send
4719 * <base, SA(*), address(SD)>
4722 * IN: mhp: pointer to the pointer to each header.
4723 * OUT: NULL if fail.
4724 * other if success, return pointer to the message to send.
4727 key_getspi(so
, m
, mhp
)
4730 const struct sadb_msghdr
*mhp
;
4732 struct sadb_address
*src0
, *dst0
;
4733 struct secasindex saidx
;
4734 struct secashead
*newsah
;
4735 struct secasvar
*newsav
;
4742 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4745 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
4746 panic("key_getspi: NULL pointer is passed.\n");
4748 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
4749 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
4750 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
4751 return key_senderror(so
, m
, EINVAL
);
4753 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
4754 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
4755 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
4756 return key_senderror(so
, m
, EINVAL
);
4758 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
4759 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
4760 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
4762 mode
= IPSEC_MODE_ANY
;
4766 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
4767 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
4769 /* map satype to proto */
4770 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
4771 ipseclog((LOG_DEBUG
, "key_getspi: invalid satype is passed.\n"));
4772 return key_senderror(so
, m
, EINVAL
);
4775 /* make sure if port number is zero. */
4776 switch (((struct sockaddr
*)(src0
+ 1))->sa_family
) {
4778 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
4779 sizeof(struct sockaddr_in
))
4780 return key_senderror(so
, m
, EINVAL
);
4781 ((struct sockaddr_in
*)(src0
+ 1))->sin_port
= 0;
4784 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
4785 sizeof(struct sockaddr_in6
))
4786 return key_senderror(so
, m
, EINVAL
);
4787 ((struct sockaddr_in6
*)(src0
+ 1))->sin6_port
= 0;
4792 switch (((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
4794 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
4795 sizeof(struct sockaddr_in
))
4796 return key_senderror(so
, m
, EINVAL
);
4797 ((struct sockaddr_in
*)(dst0
+ 1))->sin_port
= 0;
4800 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
4801 sizeof(struct sockaddr_in6
))
4802 return key_senderror(so
, m
, EINVAL
);
4803 ((struct sockaddr_in6
*)(dst0
+ 1))->sin6_port
= 0;
4809 /* XXX boundary check against sa_len */
4810 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
4812 /* SPI allocation */
4813 spi
= key_do_getnewspi((struct sadb_spirange
*)mhp
->ext
[SADB_EXT_SPIRANGE
],
4816 return key_senderror(so
, m
, EINVAL
);
4818 /* get a SA index */
4819 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
4820 /* create a new SA index */
4821 if ((newsah
= key_newsah(&saidx
)) == NULL
) {
4822 ipseclog((LOG_DEBUG
, "key_getspi: No more memory.\n"));
4823 return key_senderror(so
, m
, ENOBUFS
);
4829 newsav
= key_newsav(m
, mhp
, newsah
, &error
);
4830 if (newsav
== NULL
) {
4831 /* XXX don't free new SA index allocated in above. */
4832 return key_senderror(so
, m
, error
);
4836 key_setspi(newsav
, htonl(spi
));
4838 #ifndef IPSEC_NONBLOCK_ACQUIRE
4839 /* delete the entry in acqtree */
4840 if (mhp
->msg
->sadb_msg_seq
!= 0) {
4842 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) != NULL
) {
4843 /* reset counter in order to deletion by timehandler. */
4846 acq
->created
= tv
.tv_sec
;
4853 struct mbuf
*n
, *nn
;
4854 struct sadb_sa
*m_sa
;
4855 struct sadb_msg
*newmsg
;
4858 /* create new sadb_msg to reply. */
4859 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
)) +
4860 PFKEY_ALIGN8(sizeof(struct sadb_sa
));
4862 return key_senderror(so
, m
, ENOBUFS
);
4864 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
4866 MCLGET(n
, M_DONTWAIT
);
4867 if ((n
->m_flags
& M_EXT
) == 0) {
4873 return key_senderror(so
, m
, ENOBUFS
);
4879 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
4880 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
4882 m_sa
= (struct sadb_sa
*)(mtod(n
, caddr_t
) + off
);
4883 m_sa
->sadb_sa_len
= PFKEY_UNIT64(sizeof(struct sadb_sa
));
4884 m_sa
->sadb_sa_exttype
= SADB_EXT_SA
;
4885 m_sa
->sadb_sa_spi
= htonl(spi
);
4886 off
+= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
4890 panic("length inconsistency in key_getspi");
4893 int mbufItems
[] = {SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
4894 n
->m_next
= key_gather_mbuf(m
, mhp
, 0, sizeof(mbufItems
)/sizeof(int), mbufItems
);
4897 return key_senderror(so
, m
, ENOBUFS
);
4901 if (n
->m_len
< sizeof(struct sadb_msg
)) {
4902 n
= m_pullup(n
, sizeof(struct sadb_msg
));
4904 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
4907 n
->m_pkthdr
.len
= 0;
4908 for (nn
= n
; nn
; nn
= nn
->m_next
)
4909 n
->m_pkthdr
.len
+= nn
->m_len
;
4911 newmsg
= mtod(n
, struct sadb_msg
*);
4912 newmsg
->sadb_msg_seq
= newsav
->seq
;
4913 newmsg
->sadb_msg_errno
= 0;
4914 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
4917 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
4922 * allocating new SPI
4923 * called by key_getspi().
4929 key_do_getnewspi(spirange
, saidx
)
4930 struct sadb_spirange
*spirange
;
4931 struct secasindex
*saidx
;
4935 int count
= key_spi_trycnt
;
4937 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4939 /* set spi range to allocate */
4940 if (spirange
!= NULL
) {
4941 min
= spirange
->sadb_spirange_min
;
4942 max
= spirange
->sadb_spirange_max
;
4944 min
= key_spi_minval
;
4945 max
= key_spi_maxval
;
4947 /* IPCOMP needs 2-byte SPI */
4948 if (saidx
->proto
== IPPROTO_IPCOMP
) {
4955 t
= min
; min
= max
; max
= t
;
4960 if (key_checkspidup(saidx
, min
) != NULL
) {
4961 ipseclog((LOG_DEBUG
, "key_do_getnewspi: SPI %u exists already.\n", min
));
4965 count
--; /* taking one cost. */
4973 /* when requesting to allocate spi ranged */
4975 /* generate pseudo-random SPI value ranged. */
4976 newspi
= min
+ (key_random() % (max
- min
+ 1));
4978 if (key_checkspidup(saidx
, newspi
) == NULL
)
4982 if (count
== 0 || newspi
== 0) {
4983 ipseclog((LOG_DEBUG
, "key_do_getnewspi: to allocate spi is failed.\n"));
4989 keystat
.getspi_count
=
4990 (keystat
.getspi_count
+ key_spi_trycnt
- count
) / 2;
4996 * SADB_UPDATE processing
4998 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4999 * key(AE), (identity(SD),) (sensitivity)>
5000 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
5002 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5003 * (identity(SD),) (sensitivity)>
5006 * m will always be freed.
5009 key_update(so
, m
, mhp
)
5012 const struct sadb_msghdr
*mhp
;
5014 struct sadb_sa
*sa0
;
5015 struct sadb_address
*src0
, *dst0
;
5016 struct secasindex saidx
;
5017 struct secashead
*sah
;
5018 struct secasvar
*sav
;
5024 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5027 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5028 panic("key_update: NULL pointer is passed.\n");
5030 /* map satype to proto */
5031 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5032 ipseclog((LOG_DEBUG
, "key_update: invalid satype is passed.\n"));
5033 return key_senderror(so
, m
, EINVAL
);
5036 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
5037 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5038 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
5039 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
5040 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
5041 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
5042 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
5043 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
5044 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
5045 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
5046 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
5047 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
5048 return key_senderror(so
, m
, EINVAL
);
5050 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
5051 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5052 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5053 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
5054 return key_senderror(so
, m
, EINVAL
);
5056 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
5057 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
5058 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
5060 mode
= IPSEC_MODE_ANY
;
5063 /* XXX boundary checking for other extensions */
5065 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5066 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5067 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5069 /* XXX boundary check against sa_len */
5070 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
5072 /* get a SA header */
5073 if ((sah
= key_getsah(&saidx
)) == NULL
) {
5074 ipseclog((LOG_DEBUG
, "key_update: no SA index found.\n"));
5075 return key_senderror(so
, m
, ENOENT
);
5078 /* set spidx if there */
5080 error
= key_setident(sah
, m
, mhp
);
5082 return key_senderror(so
, m
, error
);
5084 /* find a SA with sequence number. */
5085 #if IPSEC_DOSEQCHECK
5086 if (mhp
->msg
->sadb_msg_seq
!= 0
5087 && (sav
= key_getsavbyseq(sah
, mhp
->msg
->sadb_msg_seq
)) == NULL
) {
5088 ipseclog((LOG_DEBUG
,
5089 "key_update: no larval SA with sequence %u exists.\n",
5090 mhp
->msg
->sadb_msg_seq
));
5091 return key_senderror(so
, m
, ENOENT
);
5094 if ((sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
)) == NULL
) {
5095 ipseclog((LOG_DEBUG
,
5096 "key_update: no such a SA found (spi:%u)\n",
5097 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
5098 return key_senderror(so
, m
, EINVAL
);
5102 /* validity check */
5103 if (sav
->sah
->saidx
.proto
!= proto
) {
5104 ipseclog((LOG_DEBUG
,
5105 "key_update: protocol mismatched (DB=%u param=%u)\n",
5106 sav
->sah
->saidx
.proto
, proto
));
5107 return key_senderror(so
, m
, EINVAL
);
5109 #if IPSEC_DOSEQCHECK
5110 if (sav
->spi
!= sa0
->sadb_sa_spi
) {
5111 ipseclog((LOG_DEBUG
,
5112 "key_update: SPI mismatched (DB:%u param:%u)\n",
5113 (u_int32_t
)ntohl(sav
->spi
),
5114 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
5115 return key_senderror(so
, m
, EINVAL
);
5118 if (sav
->pid
!= mhp
->msg
->sadb_msg_pid
) {
5119 ipseclog((LOG_DEBUG
,
5120 "key_update: pid mismatched (DB:%u param:%u)\n",
5121 sav
->pid
, mhp
->msg
->sadb_msg_pid
));
5122 return key_senderror(so
, m
, EINVAL
);
5125 /* copy sav values */
5126 error
= key_setsaval(sav
, m
, mhp
);
5129 return key_senderror(so
, m
, error
);
5132 /* check SA values to be mature. */
5133 if ((error
= key_mature(sav
)) != 0) {
5135 return key_senderror(so
, m
, error
);
5141 /* set msg buf from mhp */
5142 n
= key_getmsgbuf_x1(m
, mhp
);
5144 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
5145 return key_senderror(so
, m
, ENOBUFS
);
5149 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5154 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
5155 * only called by key_update().
5158 * others : found, pointer to a SA.
5160 #if IPSEC_DOSEQCHECK
5161 static struct secasvar
*
5162 key_getsavbyseq(sah
, seq
)
5163 struct secashead
*sah
;
5166 struct secasvar
*sav
;
5169 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5171 state
= SADB_SASTATE_LARVAL
;
5173 /* search SAD with sequence number ? */
5174 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
5176 KEY_CHKSASTATE(state
, sav
->state
, "key_getsabyseq");
5178 if (sav
->seq
== seq
) {
5180 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
5181 printf("DP key_getsavbyseq cause "
5182 "refcnt++:%d SA:%p\n",
5193 * SADB_ADD processing
5194 * add a entry to SA database, when received
5195 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5196 * key(AE), (identity(SD),) (sensitivity)>
5199 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5200 * (identity(SD),) (sensitivity)>
5203 * IGNORE identity and sensitivity messages.
5205 * m will always be freed.
5211 const struct sadb_msghdr
*mhp
;
5213 struct sadb_sa
*sa0
;
5214 struct sadb_address
*src0
, *dst0
;
5215 struct secasindex saidx
;
5216 struct secashead
*newsah
;
5217 struct secasvar
*newsav
;
5223 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5226 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5227 panic("key_add: NULL pointer is passed.\n");
5229 /* map satype to proto */
5230 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5231 ipseclog((LOG_DEBUG
, "key_add: invalid satype is passed.\n"));
5232 return key_senderror(so
, m
, EINVAL
);
5235 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
5236 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5237 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
5238 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
5239 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
5240 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
5241 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
5242 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
5243 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
5244 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
5245 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
5246 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
5247 return key_senderror(so
, m
, EINVAL
);
5249 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
5250 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5251 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5253 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
5254 return key_senderror(so
, m
, EINVAL
);
5256 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
5257 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
5258 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
5260 mode
= IPSEC_MODE_ANY
;
5264 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5265 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
5266 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
5268 /* XXX boundary check against sa_len */
5269 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
5271 /* get a SA header */
5272 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
5273 /* create a new SA header */
5274 if ((newsah
= key_newsah(&saidx
)) == NULL
) {
5275 ipseclog((LOG_DEBUG
, "key_add: No more memory.\n"));
5276 return key_senderror(so
, m
, ENOBUFS
);
5280 /* set spidx if there */
5282 error
= key_setident(newsah
, m
, mhp
);
5284 return key_senderror(so
, m
, error
);
5287 /* create new SA entry. */
5288 /* We can create new SA only if SPI is differenct. */
5289 if (key_getsavbyspi(newsah
, sa0
->sadb_sa_spi
)) {
5290 ipseclog((LOG_DEBUG
, "key_add: SA already exists.\n"));
5291 return key_senderror(so
, m
, EEXIST
);
5293 newsav
= key_newsav(m
, mhp
, newsah
, &error
);
5294 if (newsav
== NULL
) {
5295 return key_senderror(so
, m
, error
);
5298 /* check SA values to be mature. */
5299 if ((error
= key_mature(newsav
)) != 0) {
5300 key_freesav(newsav
);
5301 return key_senderror(so
, m
, error
);
5305 * don't call key_freesav() here, as we would like to keep the SA
5306 * in the database on success.
5312 /* set msg buf from mhp */
5313 n
= key_getmsgbuf_x1(m
, mhp
);
5315 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
5316 return key_senderror(so
, m
, ENOBUFS
);
5320 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5326 key_setident(sah
, m
, mhp
)
5327 struct secashead
*sah
;
5329 const struct sadb_msghdr
*mhp
;
5331 const struct sadb_ident
*idsrc
, *iddst
;
5332 int idsrclen
, iddstlen
;
5334 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5337 if (sah
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5338 panic("key_setident: NULL pointer is passed.\n");
5340 /* don't make buffer if not there */
5341 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
&&
5342 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
5348 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
||
5349 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
5350 ipseclog((LOG_DEBUG
, "key_setident: invalid identity.\n"));
5354 idsrc
= (const struct sadb_ident
*)mhp
->ext
[SADB_EXT_IDENTITY_SRC
];
5355 iddst
= (const struct sadb_ident
*)mhp
->ext
[SADB_EXT_IDENTITY_DST
];
5356 idsrclen
= mhp
->extlen
[SADB_EXT_IDENTITY_SRC
];
5357 iddstlen
= mhp
->extlen
[SADB_EXT_IDENTITY_DST
];
5359 /* validity check */
5360 if (idsrc
->sadb_ident_type
!= iddst
->sadb_ident_type
) {
5361 ipseclog((LOG_DEBUG
, "key_setident: ident type mismatch.\n"));
5365 switch (idsrc
->sadb_ident_type
) {
5366 case SADB_IDENTTYPE_PREFIX
:
5367 case SADB_IDENTTYPE_FQDN
:
5368 case SADB_IDENTTYPE_USERFQDN
:
5370 /* XXX do nothing */
5376 /* make structure */
5377 KMALLOC(sah
->idents
, struct sadb_ident
*, idsrclen
);
5378 if (sah
->idents
== NULL
) {
5379 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
5382 KMALLOC(sah
->identd
, struct sadb_ident
*, iddstlen
);
5383 if (sah
->identd
== NULL
) {
5386 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
5389 bcopy(idsrc
, sah
->idents
, idsrclen
);
5390 bcopy(iddst
, sah
->identd
, iddstlen
);
5396 * m will not be freed on return.
5397 * it is caller's responsibility to free the result.
5399 static struct mbuf
*
5400 key_getmsgbuf_x1(m
, mhp
)
5402 const struct sadb_msghdr
*mhp
;
5405 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
5406 SADB_X_EXT_SA2
, SADB_EXT_ADDRESS_SRC
,
5407 SADB_EXT_ADDRESS_DST
, SADB_EXT_LIFETIME_HARD
,
5408 SADB_EXT_LIFETIME_SOFT
, SADB_EXT_IDENTITY_SRC
,
5409 SADB_EXT_IDENTITY_DST
};
5412 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5413 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
5415 /* create new sadb_msg to reply. */
5416 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
5420 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5421 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5425 mtod(n
, struct sadb_msg
*)->sadb_msg_errno
= 0;
5426 mtod(n
, struct sadb_msg
*)->sadb_msg_len
=
5427 PFKEY_UNIT64(n
->m_pkthdr
.len
);
5432 static int key_delete_all(struct socket
*, struct mbuf
*,
5433 const struct sadb_msghdr
*, u_int16_t
);
5436 * SADB_DELETE processing
5438 * <base, SA(*), address(SD)>
5439 * from the ikmpd, and set SADB_SASTATE_DEAD,
5441 * <base, SA(*), address(SD)>
5444 * m will always be freed.
5447 key_delete(so
, m
, mhp
)
5450 const struct sadb_msghdr
*mhp
;
5452 struct sadb_sa
*sa0
;
5453 struct sadb_address
*src0
, *dst0
;
5454 struct secasindex saidx
;
5455 struct secashead
*sah
;
5456 struct secasvar
*sav
= NULL
;
5459 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5462 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5463 panic("key_delete: NULL pointer is passed.\n");
5465 /* map satype to proto */
5466 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5467 ipseclog((LOG_DEBUG
, "key_delete: invalid satype is passed.\n"));
5468 return key_senderror(so
, m
, EINVAL
);
5471 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5472 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
5473 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
5474 return key_senderror(so
, m
, EINVAL
);
5477 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5478 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5479 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
5480 return key_senderror(so
, m
, EINVAL
);
5483 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
5485 * Caller wants us to delete all non-LARVAL SAs
5486 * that match the src/dst. This is used during
5487 * IKE INITIAL-CONTACT.
5489 ipseclog((LOG_DEBUG
, "key_delete: doing delete all.\n"));
5490 return key_delete_all(so
, m
, mhp
, proto
);
5491 } else if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
)) {
5492 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
5493 return key_senderror(so
, m
, EINVAL
);
5496 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5497 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5498 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5500 /* XXX boundary check against sa_len */
5501 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
5503 /* get a SA header */
5504 LIST_FOREACH(sah
, &sahtree
, chain
) {
5505 if (sah
->state
== SADB_SASTATE_DEAD
)
5507 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
5510 /* get a SA with SPI. */
5511 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
5516 ipseclog((LOG_DEBUG
, "key_delete: no SA found.\n"));
5517 return key_senderror(so
, m
, ENOENT
);
5520 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5526 struct sadb_msg
*newmsg
;
5527 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
5528 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
5530 /* create new sadb_msg to reply. */
5531 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
5533 return key_senderror(so
, m
, ENOBUFS
);
5535 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5536 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5538 return key_senderror(so
, m
, ENOBUFS
);
5540 newmsg
= mtod(n
, struct sadb_msg
*);
5541 newmsg
->sadb_msg_errno
= 0;
5542 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
5545 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5550 * delete all SAs for src/dst. Called from key_delete().
5553 key_delete_all(so
, m
, mhp
, proto
)
5556 const struct sadb_msghdr
*mhp
;
5559 struct sadb_address
*src0
, *dst0
;
5560 struct secasindex saidx
;
5561 struct secashead
*sah
;
5562 struct secasvar
*sav
, *nextsav
;
5563 u_int stateidx
, state
;
5565 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5567 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5568 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5570 /* XXX boundary check against sa_len */
5571 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
5573 LIST_FOREACH(sah
, &sahtree
, chain
) {
5574 if (sah
->state
== SADB_SASTATE_DEAD
)
5576 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
5579 /* Delete all non-LARVAL SAs. */
5581 stateidx
< _ARRAYLEN(saorder_state_alive
);
5583 state
= saorder_state_alive
[stateidx
];
5584 if (state
== SADB_SASTATE_LARVAL
)
5586 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
5587 sav
!= NULL
; sav
= nextsav
) {
5588 nextsav
= LIST_NEXT(sav
, chain
);
5590 if (sav
->state
!= state
) {
5591 ipseclog((LOG_DEBUG
, "key_delete_all: "
5592 "invalid sav->state "
5593 "(queue: %d SA: %d)\n",
5594 state
, sav
->state
));
5598 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5605 struct sadb_msg
*newmsg
;
5606 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_ADDRESS_SRC
,
5607 SADB_EXT_ADDRESS_DST
};
5609 /* create new sadb_msg to reply. */
5610 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
5612 return key_senderror(so
, m
, ENOBUFS
);
5614 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5615 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5617 return key_senderror(so
, m
, ENOBUFS
);
5619 newmsg
= mtod(n
, struct sadb_msg
*);
5620 newmsg
->sadb_msg_errno
= 0;
5621 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
5624 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5629 * SADB_GET processing
5631 * <base, SA(*), address(SD)>
5632 * from the ikmpd, and get a SP and a SA to respond,
5634 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5635 * (identity(SD),) (sensitivity)>
5638 * m will always be freed.
5644 const struct sadb_msghdr
*mhp
;
5646 struct sadb_sa
*sa0
;
5647 struct sadb_address
*src0
, *dst0
;
5648 struct secasindex saidx
;
5649 struct secashead
*sah
;
5650 struct secasvar
*sav
= NULL
;
5653 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5656 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5657 panic("key_get: NULL pointer is passed.\n");
5659 /* map satype to proto */
5660 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5661 ipseclog((LOG_DEBUG
, "key_get: invalid satype is passed.\n"));
5662 return key_senderror(so
, m
, EINVAL
);
5665 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
5666 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5667 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
5668 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
5669 return key_senderror(so
, m
, EINVAL
);
5671 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
5672 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5673 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5674 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
5675 return key_senderror(so
, m
, EINVAL
);
5678 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5679 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
5680 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
5682 /* XXX boundary check against sa_len */
5683 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
5685 /* get a SA header */
5686 LIST_FOREACH(sah
, &sahtree
, chain
) {
5687 if (sah
->state
== SADB_SASTATE_DEAD
)
5689 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
5692 /* get a SA with SPI. */
5693 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
5698 ipseclog((LOG_DEBUG
, "key_get: no SA found.\n"));
5699 return key_senderror(so
, m
, ENOENT
);
5706 /* map proto to satype */
5707 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
5708 ipseclog((LOG_DEBUG
, "key_get: there was invalid proto in SAD.\n"));
5709 return key_senderror(so
, m
, EINVAL
);
5712 /* create new sadb_msg to reply. */
5713 n
= key_setdumpsa(sav
, SADB_GET
, satype
, mhp
->msg
->sadb_msg_seq
,
5714 mhp
->msg
->sadb_msg_pid
);
5716 return key_senderror(so
, m
, ENOBUFS
);
5719 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
5723 /* XXX make it sysctl-configurable? */
5725 key_getcomb_setlifetime(comb
)
5726 struct sadb_comb
*comb
;
5729 comb
->sadb_comb_soft_allocations
= 1;
5730 comb
->sadb_comb_hard_allocations
= 1;
5731 comb
->sadb_comb_soft_bytes
= 0;
5732 comb
->sadb_comb_hard_bytes
= 0;
5733 comb
->sadb_comb_hard_addtime
= 86400; /* 1 day */
5734 comb
->sadb_comb_soft_addtime
= comb
->sadb_comb_soft_addtime
* 80 / 100;
5735 comb
->sadb_comb_soft_usetime
= 28800; /* 8 hours */
5736 comb
->sadb_comb_hard_usetime
= comb
->sadb_comb_hard_usetime
* 80 / 100;
5741 * XXX reorder combinations by preference
5742 * XXX no idea if the user wants ESP authentication or not
5744 static struct mbuf
*
5747 struct sadb_comb
*comb
;
5748 const struct esp_algorithm
*algo
;
5749 struct mbuf
*result
= NULL
, *m
, *n
;
5753 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
5756 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
5757 algo
= esp_algorithm_lookup(i
);
5761 if (algo
->keymax
< ipsec_esp_keymin
)
5763 if (algo
->keymin
< ipsec_esp_keymin
)
5764 encmin
= ipsec_esp_keymin
;
5766 encmin
= algo
->keymin
;
5769 m
= key_getcomb_ah();
5773 panic("assumption failed in key_getcomb_esp");
5775 MGET(m
, M_DONTWAIT
, MT_DATA
);
5780 bzero(mtod(m
, caddr_t
), m
->m_len
);
5787 for (n
= m
; n
; n
= n
->m_next
)
5791 panic("assumption failed in key_getcomb_esp");
5794 for (off
= 0; off
< totlen
; off
+= l
) {
5795 n
= m_pulldown(m
, off
, l
, &o
);
5797 /* m is already freed */
5800 comb
= (struct sadb_comb
*)(mtod(n
, caddr_t
) + o
);
5801 bzero(comb
, sizeof(*comb
));
5802 key_getcomb_setlifetime(comb
);
5803 comb
->sadb_comb_encrypt
= i
;
5804 comb
->sadb_comb_encrypt_minbits
= encmin
;
5805 comb
->sadb_comb_encrypt_maxbits
= algo
->keymax
;
5824 * XXX reorder combinations by preference
5826 static struct mbuf
*
5829 struct sadb_comb
*comb
;
5830 const struct ah_algorithm
*algo
;
5834 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
5837 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
5839 /* we prefer HMAC algorithms, not old algorithms */
5840 if (i
!= SADB_AALG_SHA1HMAC
&& i
!= SADB_AALG_MD5HMAC
)
5843 algo
= ah_algorithm_lookup(i
);
5847 if (algo
->keymax
< ipsec_ah_keymin
)
5849 if (algo
->keymin
< ipsec_ah_keymin
)
5850 min
= ipsec_ah_keymin
;
5857 panic("assumption failed in key_getcomb_ah");
5859 MGET(m
, M_DONTWAIT
, MT_DATA
);
5866 M_PREPEND(m
, l
, M_DONTWAIT
);
5870 comb
= mtod(m
, struct sadb_comb
*);
5871 bzero(comb
, sizeof(*comb
));
5872 key_getcomb_setlifetime(comb
);
5873 comb
->sadb_comb_auth
= i
;
5874 comb
->sadb_comb_auth_minbits
= min
;
5875 comb
->sadb_comb_auth_maxbits
= algo
->keymax
;
5882 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
5883 * XXX reorder combinations by preference
5885 static struct mbuf
*
5886 key_getcomb_ipcomp()
5888 struct sadb_comb
*comb
;
5889 const struct ipcomp_algorithm
*algo
;
5892 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
5895 for (i
= 1; i
<= SADB_X_CALG_MAX
; i
++) {
5896 algo
= ipcomp_algorithm_lookup(i
);
5903 panic("assumption failed in key_getcomb_ipcomp");
5905 MGET(m
, M_DONTWAIT
, MT_DATA
);
5912 M_PREPEND(m
, l
, M_DONTWAIT
);
5916 comb
= mtod(m
, struct sadb_comb
*);
5917 bzero(comb
, sizeof(*comb
));
5918 key_getcomb_setlifetime(comb
);
5919 comb
->sadb_comb_encrypt
= i
;
5920 /* what should we set into sadb_comb_*_{min,max}bits? */
5927 * XXX no way to pass mode (transport/tunnel) to userland
5928 * XXX replay checking?
5929 * XXX sysctl interface to ipsec_{ah,esp}_keymin
5931 static struct mbuf
*
5933 const struct secasindex
*saidx
;
5935 struct sadb_prop
*prop
;
5937 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_prop
));
5940 switch (saidx
->proto
) {
5943 m
= key_getcomb_esp();
5947 m
= key_getcomb_ah();
5949 case IPPROTO_IPCOMP
:
5950 m
= key_getcomb_ipcomp();
5958 M_PREPEND(m
, l
, M_DONTWAIT
);
5963 for (n
= m
; n
; n
= n
->m_next
)
5966 prop
= mtod(m
, struct sadb_prop
*);
5967 bzero(prop
, sizeof(*prop
));
5968 prop
->sadb_prop_len
= PFKEY_UNIT64(totlen
);
5969 prop
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
5970 prop
->sadb_prop_replay
= 32; /* XXX */
5976 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5978 * <base, SA, address(SD), (address(P)), x_policy,
5979 * (identity(SD),) (sensitivity,) proposal>
5980 * to KMD, and expect to receive
5981 * <base> with SADB_ACQUIRE if error occurred,
5983 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5984 * from KMD by PF_KEY.
5986 * XXX x_policy is outside of RFC2367 (KAME extension).
5987 * XXX sensitivity is not supported.
5988 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
5989 * see comment for key_getcomb_ipcomp().
5993 * others: error number
5996 key_acquire(saidx
, sp
)
5997 struct secasindex
*saidx
;
5998 struct secpolicy
*sp
;
6000 struct mbuf
*result
= NULL
, *m
;
6001 #ifndef IPSEC_NONBLOCK_ACQUIRE
6002 struct secacq
*newacq
;
6008 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6012 panic("key_acquire: NULL pointer is passed.\n");
6013 if ((satype
= key_proto2satype(saidx
->proto
)) == 0)
6014 panic("key_acquire: invalid proto is passed.\n");
6016 #ifndef IPSEC_NONBLOCK_ACQUIRE
6018 * We never do anything about acquirng SA. There is anather
6019 * solution that kernel blocks to send SADB_ACQUIRE message until
6020 * getting something message from IKEd. In later case, to be
6021 * managed with ACQUIRING list.
6023 /* get a entry to check whether sending message or not. */
6024 if ((newacq
= key_getacq(saidx
)) != NULL
) {
6025 if (key_blockacq_count
< newacq
->count
) {
6026 /* reset counter and do send message. */
6029 /* increment counter and do nothing. */
6034 /* make new entry for blocking to send SADB_ACQUIRE. */
6035 if ((newacq
= key_newacq(saidx
)) == NULL
)
6038 /* add to acqtree */
6039 LIST_INSERT_HEAD(&acqtree
, newacq
, chain
);
6044 #ifndef IPSEC_NONBLOCK_ACQUIRE
6047 seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
6049 m
= key_setsadbmsg(SADB_ACQUIRE
, 0, satype
, seq
, 0, 0);
6056 /* set sadb_address for saidx's. */
6057 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
6058 (struct sockaddr
*)&saidx
->src
, FULLMASK
, IPSEC_ULPROTO_ANY
);
6065 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
6066 (struct sockaddr
*)&saidx
->dst
, FULLMASK
, IPSEC_ULPROTO_ANY
);
6073 /* XXX proxy address (optional) */
6075 /* set sadb_x_policy */
6077 m
= key_setsadbxpolicy(sp
->policy
, sp
->spidx
.dir
, sp
->id
);
6085 /* XXX identity (optional) */
6087 if (idexttype
&& fqdn
) {
6088 /* create identity extension (FQDN) */
6089 struct sadb_ident
*id
;
6092 fqdnlen
= strlen(fqdn
) + 1; /* +1 for terminating-NUL */
6093 id
= (struct sadb_ident
*)p
;
6094 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
6095 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
6096 id
->sadb_ident_exttype
= idexttype
;
6097 id
->sadb_ident_type
= SADB_IDENTTYPE_FQDN
;
6098 bcopy(fqdn
, id
+ 1, fqdnlen
);
6099 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(fqdnlen
);
6103 /* create identity extension (USERFQDN) */
6104 struct sadb_ident
*id
;
6108 /* +1 for terminating-NUL */
6109 userfqdnlen
= strlen(userfqdn
) + 1;
6112 id
= (struct sadb_ident
*)p
;
6113 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
6114 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
6115 id
->sadb_ident_exttype
= idexttype
;
6116 id
->sadb_ident_type
= SADB_IDENTTYPE_USERFQDN
;
6117 /* XXX is it correct? */
6118 if (curproc
&& curproc
->p_cred
)
6119 id
->sadb_ident_id
= curproc
->p_cred
->p_ruid
;
6120 if (userfqdn
&& userfqdnlen
)
6121 bcopy(userfqdn
, id
+ 1, userfqdnlen
);
6122 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(userfqdnlen
);
6126 /* XXX sensitivity (optional) */
6128 /* create proposal/combination extension */
6129 m
= key_getprop(saidx
);
6132 * spec conformant: always attach proposal/combination extension,
6133 * the problem is that we have no way to attach it for ipcomp,
6134 * due to the way sadb_comb is declared in RFC2367.
6143 * outside of spec; make proposal/combination extension optional.
6149 if ((result
->m_flags
& M_PKTHDR
) == 0) {
6154 if (result
->m_len
< sizeof(struct sadb_msg
)) {
6155 result
= m_pullup(result
, sizeof(struct sadb_msg
));
6156 if (result
== NULL
) {
6162 result
->m_pkthdr
.len
= 0;
6163 for (m
= result
; m
; m
= m
->m_next
)
6164 result
->m_pkthdr
.len
+= m
->m_len
;
6166 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
6167 PFKEY_UNIT64(result
->m_pkthdr
.len
);
6169 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
6177 #ifndef IPSEC_NONBLOCK_ACQUIRE
6178 static struct secacq
*
6180 struct secasindex
*saidx
;
6182 struct secacq
*newacq
;
6186 KMALLOC(newacq
, struct secacq
*, sizeof(struct secacq
));
6187 if (newacq
== NULL
) {
6188 ipseclog((LOG_DEBUG
, "key_newacq: No more memory.\n"));
6191 bzero(newacq
, sizeof(*newacq
));
6194 bcopy(saidx
, &newacq
->saidx
, sizeof(newacq
->saidx
));
6195 newacq
->seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
);
6197 newacq
->created
= tv
.tv_sec
;
6203 static struct secacq
*
6205 struct secasindex
*saidx
;
6209 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6211 LIST_FOREACH(acq
, &acqtree
, chain
) {
6212 if (key_cmpsaidx(saidx
, &acq
->saidx
, CMP_EXACTLY
))
6219 static struct secacq
*
6220 key_getacqbyseq(seq
)
6225 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6227 LIST_FOREACH(acq
, &acqtree
, chain
) {
6228 if (acq
->seq
== seq
)
6236 static struct secspacq
*
6238 struct secpolicyindex
*spidx
;
6240 struct secspacq
*acq
;
6244 KMALLOC(acq
, struct secspacq
*, sizeof(struct secspacq
));
6246 ipseclog((LOG_DEBUG
, "key_newspacq: No more memory.\n"));
6249 bzero(acq
, sizeof(*acq
));
6252 bcopy(spidx
, &acq
->spidx
, sizeof(acq
->spidx
));
6254 acq
->created
= tv
.tv_sec
;
6260 static struct secspacq
*
6262 struct secpolicyindex
*spidx
;
6264 struct secspacq
*acq
;
6266 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6268 LIST_FOREACH(acq
, &spacqtree
, chain
) {
6269 if (key_cmpspidx_exactly(spidx
, &acq
->spidx
))
6277 * SADB_ACQUIRE processing,
6278 * in first situation, is receiving
6280 * from the ikmpd, and clear sequence of its secasvar entry.
6282 * In second situation, is receiving
6283 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6284 * from a user land process, and return
6285 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6288 * m will always be freed.
6291 key_acquire2(so
, m
, mhp
)
6294 const struct sadb_msghdr
*mhp
;
6296 const struct sadb_address
*src0
, *dst0
;
6297 struct secasindex saidx
;
6298 struct secashead
*sah
;
6302 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6305 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6306 panic("key_acquire2: NULL pointer is passed.\n");
6309 * Error message from KMd.
6310 * We assume that if error was occurred in IKEd, the length of PFKEY
6311 * message is equal to the size of sadb_msg structure.
6312 * We do not raise error even if error occurred in this function.
6314 if (mhp
->msg
->sadb_msg_len
== PFKEY_UNIT64(sizeof(struct sadb_msg
))) {
6315 #ifndef IPSEC_NONBLOCK_ACQUIRE
6319 /* check sequence number */
6320 if (mhp
->msg
->sadb_msg_seq
== 0) {
6321 ipseclog((LOG_DEBUG
, "key_acquire2: must specify sequence number.\n"));
6326 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) == NULL
) {
6328 * the specified larval SA is already gone, or we got
6329 * a bogus sequence number. we can silently ignore it.
6335 /* reset acq counter in order to deletion by timehander. */
6337 acq
->created
= tv
.tv_sec
;
6345 * This message is from user land.
6348 /* map satype to proto */
6349 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6350 ipseclog((LOG_DEBUG
, "key_acquire2: invalid satype is passed.\n"));
6351 return key_senderror(so
, m
, EINVAL
);
6354 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6355 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
6356 mhp
->ext
[SADB_EXT_PROPOSAL
] == NULL
) {
6358 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
6359 return key_senderror(so
, m
, EINVAL
);
6361 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6362 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
6363 mhp
->extlen
[SADB_EXT_PROPOSAL
] < sizeof(struct sadb_prop
)) {
6365 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
6366 return key_senderror(so
, m
, EINVAL
);
6369 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
6370 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
6372 /* XXX boundary check against sa_len */
6373 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
6375 /* get a SA index */
6376 LIST_FOREACH(sah
, &sahtree
, chain
) {
6377 if (sah
->state
== SADB_SASTATE_DEAD
)
6379 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_MODE_REQID
))
6383 ipseclog((LOG_DEBUG
, "key_acquire2: a SA exists already.\n"));
6384 return key_senderror(so
, m
, EEXIST
);
6387 error
= key_acquire(&saidx
, NULL
);
6389 ipseclog((LOG_DEBUG
, "key_acquire2: error %d returned "
6390 "from key_acquire.\n", mhp
->msg
->sadb_msg_errno
));
6391 return key_senderror(so
, m
, error
);
6394 return key_sendup_mbuf(so
, m
, KEY_SENDUP_REGISTERED
);
6398 * SADB_REGISTER processing.
6399 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6402 * from the ikmpd, and register a socket to send PF_KEY messages,
6406 * If socket is detached, must free from regnode.
6408 * m will always be freed.
6411 key_register(so
, m
, mhp
)
6414 const struct sadb_msghdr
*mhp
;
6416 struct secreg
*reg
, *newreg
= 0;
6418 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6421 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6422 panic("key_register: NULL pointer is passed.\n");
6424 /* check for invalid register message */
6425 if (mhp
->msg
->sadb_msg_satype
>= sizeof(regtree
)/sizeof(regtree
[0]))
6426 return key_senderror(so
, m
, EINVAL
);
6428 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6429 if (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
)
6432 /* check whether existing or not */
6433 LIST_FOREACH(reg
, ®tree
[mhp
->msg
->sadb_msg_satype
], chain
) {
6434 if (reg
->so
== so
) {
6435 ipseclog((LOG_DEBUG
, "key_register: socket exists already.\n"));
6436 return key_senderror(so
, m
, EEXIST
);
6440 /* create regnode */
6441 KMALLOC(newreg
, struct secreg
*, sizeof(*newreg
));
6442 if (newreg
== NULL
) {
6443 ipseclog((LOG_DEBUG
, "key_register: No more memory.\n"));
6444 return key_senderror(so
, m
, ENOBUFS
);
6446 bzero((caddr_t
)newreg
, sizeof(*newreg
));
6450 ((struct keycb
*)sotorawcb(so
))->kp_registered
++;
6451 socket_unlock(so
, 1);
6453 /* add regnode to regtree. */
6454 LIST_INSERT_HEAD(®tree
[mhp
->msg
->sadb_msg_satype
], newreg
, chain
);
6459 struct sadb_msg
*newmsg
;
6460 struct sadb_supported
*sup
;
6461 u_int len
, alen
, elen
;
6464 struct sadb_alg
*alg
;
6466 /* create new sadb_msg to reply. */
6468 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
6469 if (ah_algorithm_lookup(i
))
6470 alen
+= sizeof(struct sadb_alg
);
6473 alen
+= sizeof(struct sadb_supported
);
6476 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
6477 if (esp_algorithm_lookup(i
))
6478 elen
+= sizeof(struct sadb_alg
);
6481 elen
+= sizeof(struct sadb_supported
);
6484 len
= sizeof(struct sadb_msg
) + alen
+ elen
;
6487 return key_senderror(so
, m
, ENOBUFS
);
6489 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
6491 MCLGET(n
, M_DONTWAIT
);
6492 if ((n
->m_flags
& M_EXT
) == 0) {
6498 return key_senderror(so
, m
, ENOBUFS
);
6500 n
->m_pkthdr
.len
= n
->m_len
= len
;
6504 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
6505 newmsg
= mtod(n
, struct sadb_msg
*);
6506 newmsg
->sadb_msg_errno
= 0;
6507 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
6508 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
6510 /* for authentication algorithm */
6512 sup
= (struct sadb_supported
*)(mtod(n
, caddr_t
) + off
);
6513 sup
->sadb_supported_len
= PFKEY_UNIT64(alen
);
6514 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
6515 off
+= PFKEY_ALIGN8(sizeof(*sup
));
6517 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
6518 const struct ah_algorithm
*aalgo
;
6520 aalgo
= ah_algorithm_lookup(i
);
6523 alg
= (struct sadb_alg
*)(mtod(n
, caddr_t
) + off
);
6524 alg
->sadb_alg_id
= i
;
6525 alg
->sadb_alg_ivlen
= 0;
6526 alg
->sadb_alg_minbits
= aalgo
->keymin
;
6527 alg
->sadb_alg_maxbits
= aalgo
->keymax
;
6528 off
+= PFKEY_ALIGN8(sizeof(*alg
));
6533 /* for encryption algorithm */
6535 sup
= (struct sadb_supported
*)(mtod(n
, caddr_t
) + off
);
6536 sup
->sadb_supported_len
= PFKEY_UNIT64(elen
);
6537 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
6538 off
+= PFKEY_ALIGN8(sizeof(*sup
));
6540 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
6541 const struct esp_algorithm
*ealgo
;
6543 ealgo
= esp_algorithm_lookup(i
);
6546 alg
= (struct sadb_alg
*)(mtod(n
, caddr_t
) + off
);
6547 alg
->sadb_alg_id
= i
;
6548 if (ealgo
&& ealgo
->ivlen
) {
6550 * give NULL to get the value preferred by
6551 * algorithm XXX SADB_X_EXT_DERIV ?
6553 alg
->sadb_alg_ivlen
=
6554 (*ealgo
->ivlen
)(ealgo
, NULL
);
6556 alg
->sadb_alg_ivlen
= 0;
6557 alg
->sadb_alg_minbits
= ealgo
->keymin
;
6558 alg
->sadb_alg_maxbits
= ealgo
->keymax
;
6559 off
+= PFKEY_ALIGN8(sizeof(struct sadb_alg
));
6566 panic("length assumption failed in key_register");
6570 return key_sendup_mbuf(so
, n
, KEY_SENDUP_REGISTERED
);
6575 * free secreg entry registered.
6576 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6585 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6589 panic("key_freereg: NULL pointer is passed.\n");
6592 * check whether existing or not.
6593 * check all type of SA, because there is a potential that
6594 * one socket is registered to multiple type of SA.
6596 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
6597 LIST_FOREACH(reg
, ®tree
[i
], chain
) {
6599 && __LIST_CHAINED(reg
)) {
6600 LIST_REMOVE(reg
, chain
);
6611 * SADB_EXPIRE processing
6613 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6615 * NOTE: We send only soft lifetime extension.
6618 * others : error number
6622 struct secasvar
*sav
;
6626 struct mbuf
*result
= NULL
, *m
;
6629 struct sadb_lifetime
*lt
;
6631 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6635 panic("key_expire: NULL pointer is passed.\n");
6636 if (sav
->sah
== NULL
)
6637 panic("key_expire: Why was SA index in SA NULL.\n");
6638 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0)
6639 panic("key_expire: invalid proto is passed.\n");
6641 /* set msg header */
6642 m
= key_setsadbmsg(SADB_EXPIRE
, 0, satype
, sav
->seq
, 0, sav
->refcnt
);
6649 /* create SA extension */
6650 m
= key_setsadbsa(sav
);
6657 /* create SA extension */
6658 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
6659 sav
->replay
? sav
->replay
->count
: 0,
6660 sav
->sah
->saidx
.reqid
);
6667 /* create lifetime extension (current and soft) */
6668 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
6669 m
= key_alloc_mbuf(len
);
6670 if (!m
|| m
->m_next
) { /*XXX*/
6676 bzero(mtod(m
, caddr_t
), len
);
6677 lt
= mtod(m
, struct sadb_lifetime
*);
6678 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
6679 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
6680 lt
->sadb_lifetime_allocations
= sav
->lft_c
->sadb_lifetime_allocations
;
6681 lt
->sadb_lifetime_bytes
= sav
->lft_c
->sadb_lifetime_bytes
;
6682 lt
->sadb_lifetime_addtime
= sav
->lft_c
->sadb_lifetime_addtime
;
6683 lt
->sadb_lifetime_usetime
= sav
->lft_c
->sadb_lifetime_usetime
;
6684 lt
= (struct sadb_lifetime
*)(mtod(m
, caddr_t
) + len
/ 2);
6685 bcopy(sav
->lft_s
, lt
, sizeof(*lt
));
6688 /* set sadb_address for source */
6689 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
6690 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
6691 FULLMASK
, IPSEC_ULPROTO_ANY
);
6698 /* set sadb_address for destination */
6699 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
6700 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
6701 FULLMASK
, IPSEC_ULPROTO_ANY
);
6708 if ((result
->m_flags
& M_PKTHDR
) == 0) {
6713 if (result
->m_len
< sizeof(struct sadb_msg
)) {
6714 result
= m_pullup(result
, sizeof(struct sadb_msg
));
6715 if (result
== NULL
) {
6721 result
->m_pkthdr
.len
= 0;
6722 for (m
= result
; m
; m
= m
->m_next
)
6723 result
->m_pkthdr
.len
+= m
->m_len
;
6725 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
6726 PFKEY_UNIT64(result
->m_pkthdr
.len
);
6729 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
6739 * SADB_FLUSH processing
6742 * from the ikmpd, and free all entries in secastree.
6746 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6748 * m will always be freed.
6751 key_flush(so
, m
, mhp
)
6754 const struct sadb_msghdr
*mhp
;
6756 struct sadb_msg
*newmsg
;
6757 struct secashead
*sah
, *nextsah
;
6758 struct secasvar
*sav
, *nextsav
;
6763 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6766 if (so
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6767 panic("key_flush: NULL pointer is passed.\n");
6769 /* map satype to proto */
6770 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6771 ipseclog((LOG_DEBUG
, "key_flush: invalid satype is passed.\n"));
6772 return key_senderror(so
, m
, EINVAL
);
6775 /* no SATYPE specified, i.e. flushing all SA. */
6776 for (sah
= LIST_FIRST(&sahtree
);
6779 nextsah
= LIST_NEXT(sah
, chain
);
6781 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
6782 && proto
!= sah
->saidx
.proto
)
6786 stateidx
< _ARRAYLEN(saorder_state_alive
);
6788 state
= saorder_state_any
[stateidx
];
6789 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
6793 nextsav
= LIST_NEXT(sav
, chain
);
6795 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6800 sah
->state
= SADB_SASTATE_DEAD
;
6803 if (m
->m_len
< sizeof(struct sadb_msg
) ||
6804 sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
6805 ipseclog((LOG_DEBUG
, "key_flush: No more memory.\n"));
6806 return key_senderror(so
, m
, ENOBUFS
);
6812 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct sadb_msg
);
6813 newmsg
= mtod(m
, struct sadb_msg
*);
6814 newmsg
->sadb_msg_errno
= 0;
6815 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
6817 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
6821 * SADB_DUMP processing
6822 * dump all entries including status of DEAD in SAD.
6825 * from the ikmpd, and dump all secasvar leaves
6830 * m will always be freed.
6833 key_dump(so
, m
, mhp
)
6836 const struct sadb_msghdr
*mhp
;
6838 struct secashead
*sah
;
6839 struct secasvar
*sav
;
6845 struct sadb_msg
*newmsg
;
6848 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6851 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6852 panic("key_dump: NULL pointer is passed.\n");
6854 /* map satype to proto */
6855 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6856 ipseclog((LOG_DEBUG
, "key_dump: invalid satype is passed.\n"));
6857 return key_senderror(so
, m
, EINVAL
);
6860 /* count sav entries to be sent to the userland. */
6862 LIST_FOREACH(sah
, &sahtree
, chain
) {
6863 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
6864 && proto
!= sah
->saidx
.proto
)
6868 stateidx
< _ARRAYLEN(saorder_state_any
);
6870 state
= saorder_state_any
[stateidx
];
6871 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
6878 return key_senderror(so
, m
, ENOENT
);
6880 /* send this to the userland, one at a time. */
6882 LIST_FOREACH(sah
, &sahtree
, chain
) {
6883 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
6884 && proto
!= sah
->saidx
.proto
)
6887 /* map proto to satype */
6888 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
6889 ipseclog((LOG_DEBUG
, "key_dump: there was invalid proto in SAD.\n"));
6890 return key_senderror(so
, m
, EINVAL
);
6894 stateidx
< _ARRAYLEN(saorder_state_any
);
6896 state
= saorder_state_any
[stateidx
];
6897 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
6898 n
= key_setdumpsa(sav
, SADB_DUMP
, satype
,
6899 --cnt
, mhp
->msg
->sadb_msg_pid
);
6901 return key_senderror(so
, m
, ENOBUFS
);
6903 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
6913 * SADB_X_PROMISC processing
6915 * m will always be freed.
6918 key_promisc(so
, m
, mhp
)
6921 const struct sadb_msghdr
*mhp
;
6925 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6928 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6929 panic("key_promisc: NULL pointer is passed.\n");
6931 olen
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
6933 if (olen
< sizeof(struct sadb_msg
)) {
6935 return key_senderror(so
, m
, EINVAL
);
6940 } else if (olen
== sizeof(struct sadb_msg
)) {
6941 /* enable/disable promisc mode */
6945 if ((kp
= (struct keycb
*)sotorawcb(so
)) == NULL
)
6946 return key_senderror(so
, m
, EINVAL
);
6947 mhp
->msg
->sadb_msg_errno
= 0;
6948 switch (mhp
->msg
->sadb_msg_satype
) {
6951 kp
->kp_promisc
= mhp
->msg
->sadb_msg_satype
;
6954 socket_unlock(so
, 1);
6955 return key_senderror(so
, m
, EINVAL
);
6957 socket_unlock(so
, 1);
6959 /* send the original message back to everyone */
6960 mhp
->msg
->sadb_msg_errno
= 0;
6961 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
6963 /* send packet as is */
6965 m_adj(m
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)));
6967 /* TODO: if sadb_msg_seq is specified, send to specific pid */
6968 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
6972 static int (*key_typesw
[])(struct socket
*, struct mbuf
*,
6973 const struct sadb_msghdr
*) = {
6974 NULL
, /* SADB_RESERVED */
6975 key_getspi
, /* SADB_GETSPI */
6976 key_update
, /* SADB_UPDATE */
6977 key_add
, /* SADB_ADD */
6978 key_delete
, /* SADB_DELETE */
6979 key_get
, /* SADB_GET */
6980 key_acquire2
, /* SADB_ACQUIRE */
6981 key_register
, /* SADB_REGISTER */
6982 NULL
, /* SADB_EXPIRE */
6983 key_flush
, /* SADB_FLUSH */
6984 key_dump
, /* SADB_DUMP */
6985 key_promisc
, /* SADB_X_PROMISC */
6986 NULL
, /* SADB_X_PCHANGE */
6987 key_spdadd
, /* SADB_X_SPDUPDATE */
6988 key_spdadd
, /* SADB_X_SPDADD */
6989 key_spddelete
, /* SADB_X_SPDDELETE */
6990 key_spdget
, /* SADB_X_SPDGET */
6991 NULL
, /* SADB_X_SPDACQUIRE */
6992 key_spddump
, /* SADB_X_SPDDUMP */
6993 key_spdflush
, /* SADB_X_SPDFLUSH */
6994 key_spdadd
, /* SADB_X_SPDSETIDX */
6995 NULL
, /* SADB_X_SPDEXPIRE */
6996 key_spddelete2
, /* SADB_X_SPDDELETE2 */
7000 * parse sadb_msg buffer to process PFKEYv2,
7001 * and create a data to response if needed.
7002 * I think to be dealed with mbuf directly.
7004 * msgp : pointer to pointer to a received buffer pulluped.
7005 * This is rewrited to response.
7006 * so : pointer to socket.
7008 * length for buffer to send to user process.
7015 struct sadb_msg
*msg
;
7016 struct sadb_msghdr mh
;
7021 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7024 if (m
== NULL
|| so
== NULL
)
7025 panic("key_parse: NULL pointer is passed.\n");
7027 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
7028 KEYDEBUG(KEYDEBUG_KEY_DUMP
,
7029 ipseclog((LOG_DEBUG
, "key_parse: passed sadb_msg\n"));
7033 if (m
->m_len
< sizeof(struct sadb_msg
)) {
7034 m
= m_pullup(m
, sizeof(struct sadb_msg
));
7038 msg
= mtod(m
, struct sadb_msg
*);
7039 orglen
= PFKEY_UNUNIT64(msg
->sadb_msg_len
);
7040 target
= KEY_SENDUP_ONE
;
7042 if ((m
->m_flags
& M_PKTHDR
) == 0 ||
7043 m
->m_pkthdr
.len
!= m
->m_pkthdr
.len
) {
7044 ipseclog((LOG_DEBUG
, "key_parse: invalid message length.\n"));
7045 pfkeystat
.out_invlen
++;
7050 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
7051 ipseclog((LOG_DEBUG
,
7052 "key_parse: PF_KEY version %u is mismatched.\n",
7053 msg
->sadb_msg_version
));
7054 pfkeystat
.out_invver
++;
7059 if (msg
->sadb_msg_type
> SADB_MAX
) {
7060 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
7061 msg
->sadb_msg_type
));
7062 pfkeystat
.out_invmsgtype
++;
7067 /* for old-fashioned code - should be nuked */
7068 if (m
->m_pkthdr
.len
> MCLBYTES
) {
7075 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
7076 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
7077 MCLGET(n
, M_DONTWAIT
);
7078 if ((n
->m_flags
& M_EXT
) == 0) {
7087 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
7088 n
->m_pkthdr
.len
= n
->m_len
= m
->m_pkthdr
.len
;
7094 /* align the mbuf chain so that extensions are in contiguous region. */
7095 error
= key_align(m
, &mh
);
7099 if (m
->m_next
) { /*XXX*/
7107 switch (msg
->sadb_msg_satype
) {
7108 case SADB_SATYPE_UNSPEC
:
7109 switch (msg
->sadb_msg_type
) {
7117 ipseclog((LOG_DEBUG
, "key_parse: must specify satype "
7118 "when msg type=%u.\n", msg
->sadb_msg_type
));
7119 pfkeystat
.out_invsatype
++;
7124 case SADB_SATYPE_AH
:
7125 case SADB_SATYPE_ESP
:
7126 case SADB_X_SATYPE_IPCOMP
:
7127 switch (msg
->sadb_msg_type
) {
7129 case SADB_X_SPDDELETE
:
7131 case SADB_X_SPDDUMP
:
7132 case SADB_X_SPDFLUSH
:
7133 case SADB_X_SPDSETIDX
:
7134 case SADB_X_SPDUPDATE
:
7135 case SADB_X_SPDDELETE2
:
7136 ipseclog((LOG_DEBUG
, "key_parse: illegal satype=%u\n",
7137 msg
->sadb_msg_type
));
7138 pfkeystat
.out_invsatype
++;
7143 case SADB_SATYPE_RSVP
:
7144 case SADB_SATYPE_OSPFV2
:
7145 case SADB_SATYPE_RIPV2
:
7146 case SADB_SATYPE_MIP
:
7147 ipseclog((LOG_DEBUG
, "key_parse: type %u isn't supported.\n",
7148 msg
->sadb_msg_satype
));
7149 pfkeystat
.out_invsatype
++;
7152 case 1: /* XXX: What does it do? */
7153 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
7157 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
7158 msg
->sadb_msg_satype
));
7159 pfkeystat
.out_invsatype
++;
7164 /* check field of upper layer protocol and address family */
7165 if (mh
.ext
[SADB_EXT_ADDRESS_SRC
] != NULL
7166 && mh
.ext
[SADB_EXT_ADDRESS_DST
] != NULL
) {
7167 struct sadb_address
*src0
, *dst0
;
7170 src0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_SRC
]);
7171 dst0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_DST
]);
7173 /* check upper layer protocol */
7174 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
7175 ipseclog((LOG_DEBUG
, "key_parse: upper layer protocol mismatched.\n"));
7176 pfkeystat
.out_invaddr
++;
7182 if (PFKEY_ADDR_SADDR(src0
)->sa_family
!=
7183 PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
7184 ipseclog((LOG_DEBUG
, "key_parse: address family mismatched.\n"));
7185 pfkeystat
.out_invaddr
++;
7189 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
7190 PFKEY_ADDR_SADDR(dst0
)->sa_len
) {
7191 ipseclog((LOG_DEBUG
,
7192 "key_parse: address struct size mismatched.\n"));
7193 pfkeystat
.out_invaddr
++;
7198 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
7200 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
7201 sizeof(struct sockaddr_in
)) {
7202 pfkeystat
.out_invaddr
++;
7208 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
7209 sizeof(struct sockaddr_in6
)) {
7210 pfkeystat
.out_invaddr
++;
7216 ipseclog((LOG_DEBUG
,
7217 "key_parse: unsupported address family.\n"));
7218 pfkeystat
.out_invaddr
++;
7219 error
= EAFNOSUPPORT
;
7223 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
7225 plen
= sizeof(struct in_addr
) << 3;
7228 plen
= sizeof(struct in6_addr
) << 3;
7231 plen
= 0; /*fool gcc*/
7235 /* check max prefix length */
7236 if (src0
->sadb_address_prefixlen
> plen
||
7237 dst0
->sadb_address_prefixlen
> plen
) {
7238 ipseclog((LOG_DEBUG
,
7239 "key_parse: illegal prefixlen.\n"));
7240 pfkeystat
.out_invaddr
++;
7246 * prefixlen == 0 is valid because there can be a case when
7247 * all addresses are matched.
7251 if (msg
->sadb_msg_type
>= sizeof(key_typesw
)/sizeof(key_typesw
[0]) ||
7252 key_typesw
[msg
->sadb_msg_type
] == NULL
) {
7253 pfkeystat
.out_invmsgtype
++;
7258 return (*key_typesw
[msg
->sadb_msg_type
])(so
, m
, &mh
);
7261 msg
->sadb_msg_errno
= error
;
7262 return key_sendup_mbuf(so
, m
, target
);
7266 key_senderror(so
, m
, code
)
7271 struct sadb_msg
*msg
;
7273 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7275 if (m
->m_len
< sizeof(struct sadb_msg
))
7276 panic("invalid mbuf passed to key_senderror");
7278 msg
= mtod(m
, struct sadb_msg
*);
7279 msg
->sadb_msg_errno
= code
;
7280 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
7284 * set the pointer to each header into message buffer.
7285 * m will be freed on error.
7286 * XXX larger-than-MCLBYTES extension?
7291 struct sadb_msghdr
*mhp
;
7294 struct sadb_ext
*ext
;
7300 if (m
== NULL
|| mhp
== NULL
)
7301 panic("key_align: NULL pointer is passed.\n");
7302 if (m
->m_len
< sizeof(struct sadb_msg
))
7303 panic("invalid mbuf passed to key_align");
7306 bzero(mhp
, sizeof(*mhp
));
7308 mhp
->msg
= mtod(m
, struct sadb_msg
*);
7309 mhp
->ext
[0] = (struct sadb_ext
*)mhp
->msg
; /*XXX backward compat */
7311 end
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
7312 extlen
= end
; /*just in case extlen is not updated*/
7313 for (off
= sizeof(struct sadb_msg
); off
< end
; off
+= extlen
) {
7314 n
= m_pulldown(m
, off
, sizeof(struct sadb_ext
), &toff
);
7316 /* m is already freed */
7319 ext
= (struct sadb_ext
*)(mtod(n
, caddr_t
) + toff
);
7322 switch (ext
->sadb_ext_type
) {
7324 case SADB_EXT_ADDRESS_SRC
:
7325 case SADB_EXT_ADDRESS_DST
:
7326 case SADB_EXT_ADDRESS_PROXY
:
7327 case SADB_EXT_LIFETIME_CURRENT
:
7328 case SADB_EXT_LIFETIME_HARD
:
7329 case SADB_EXT_LIFETIME_SOFT
:
7330 case SADB_EXT_KEY_AUTH
:
7331 case SADB_EXT_KEY_ENCRYPT
:
7332 case SADB_EXT_IDENTITY_SRC
:
7333 case SADB_EXT_IDENTITY_DST
:
7334 case SADB_EXT_SENSITIVITY
:
7335 case SADB_EXT_PROPOSAL
:
7336 case SADB_EXT_SUPPORTED_AUTH
:
7337 case SADB_EXT_SUPPORTED_ENCRYPT
:
7338 case SADB_EXT_SPIRANGE
:
7339 case SADB_X_EXT_POLICY
:
7340 case SADB_X_EXT_SA2
:
7341 /* duplicate check */
7343 * XXX Are there duplication payloads of either
7344 * KEY_AUTH or KEY_ENCRYPT ?
7346 if (mhp
->ext
[ext
->sadb_ext_type
] != NULL
) {
7347 ipseclog((LOG_DEBUG
,
7348 "key_align: duplicate ext_type %u "
7349 "is passed.\n", ext
->sadb_ext_type
));
7351 pfkeystat
.out_dupext
++;
7356 ipseclog((LOG_DEBUG
,
7357 "key_align: invalid ext_type %u is passed.\n",
7358 ext
->sadb_ext_type
));
7360 pfkeystat
.out_invexttype
++;
7364 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
7366 if (key_validate_ext(ext
, extlen
)) {
7368 pfkeystat
.out_invlen
++;
7372 n
= m_pulldown(m
, off
, extlen
, &toff
);
7374 /* m is already freed */
7377 ext
= (struct sadb_ext
*)(mtod(n
, caddr_t
) + toff
);
7379 mhp
->ext
[ext
->sadb_ext_type
] = ext
;
7380 mhp
->extoff
[ext
->sadb_ext_type
] = off
;
7381 mhp
->extlen
[ext
->sadb_ext_type
] = extlen
;
7386 pfkeystat
.out_invlen
++;
7394 key_validate_ext(ext
, len
)
7395 const struct sadb_ext
*ext
;
7398 struct sockaddr
*sa
;
7399 enum { NONE
, ADDR
} checktype
= NONE
;
7401 const int sal
= offsetof(struct sockaddr
, sa_len
) + sizeof(sa
->sa_len
);
7403 if (len
!= PFKEY_UNUNIT64(ext
->sadb_ext_len
))
7406 /* if it does not match minimum/maximum length, bail */
7407 if (ext
->sadb_ext_type
>= sizeof(minsize
) / sizeof(minsize
[0]) ||
7408 ext
->sadb_ext_type
>= sizeof(maxsize
) / sizeof(maxsize
[0]))
7410 if (!minsize
[ext
->sadb_ext_type
] || len
< minsize
[ext
->sadb_ext_type
])
7412 if (maxsize
[ext
->sadb_ext_type
] && len
> maxsize
[ext
->sadb_ext_type
])
7415 /* more checks based on sadb_ext_type XXX need more */
7416 switch (ext
->sadb_ext_type
) {
7417 case SADB_EXT_ADDRESS_SRC
:
7418 case SADB_EXT_ADDRESS_DST
:
7419 case SADB_EXT_ADDRESS_PROXY
:
7420 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_address
));
7423 case SADB_EXT_IDENTITY_SRC
:
7424 case SADB_EXT_IDENTITY_DST
:
7425 if (((struct sadb_ident
*)ext
)->sadb_ident_type
==
7426 SADB_X_IDENTTYPE_ADDR
) {
7427 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_ident
));
7437 switch (checktype
) {
7441 sa
= (struct sockaddr
*)((caddr_t
)ext
+ baselen
);
7442 if (len
< baselen
+ sal
)
7444 if (baselen
+ PFKEY_ALIGN8(sa
->sa_len
) != len
)
7457 bzero((caddr_t
)&key_cb
, sizeof(key_cb
));
7459 for (i
= 0; i
< IPSEC_DIR_MAX
; i
++) {
7460 LIST_INIT(&sptree
[i
]);
7463 LIST_INIT(&sahtree
);
7465 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
7466 LIST_INIT(®tree
[i
]);
7469 #ifndef IPSEC_NONBLOCK_ACQUIRE
7470 LIST_INIT(&acqtree
);
7472 LIST_INIT(&spacqtree
);
7474 /* system default */
7476 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
7477 ip4_def_policy
.refcnt
++; /*never reclaim this*/
7480 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
7481 ip6_def_policy
.refcnt
++; /*never reclaim this*/
7484 #ifndef IPSEC_DEBUG2
7485 timeout((void *)key_timehandler
, (void *)0, hz
);
7486 #endif /*IPSEC_DEBUG2*/
7488 /* initialize key statistics */
7489 keystat
.getspi_count
= 1;
7492 printf("IPsec: Initialized Security Association Processing.\n");
7499 * XXX: maybe This function is called after INBOUND IPsec processing.
7501 * Special check for tunnel-mode packets.
7502 * We must make some checks for consistency between inner and outer IP header.
7504 * xxx more checks to be provided
7507 key_checktunnelsanity(sav
, family
, src
, dst
)
7508 struct secasvar
*sav
;
7513 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7516 if (sav
->sah
== NULL
)
7517 panic("sav->sah == NULL at key_checktunnelsanity");
7519 /* XXX: check inner IP header */
7525 #define hostnamelen strlen(hostname)
7528 * Get FQDN for the host.
7529 * If the administrator configured hostname (by hostname(1)) without
7530 * domain name, returns nothing.
7537 static char fqdn
[MAXHOSTNAMELEN
+ 1];
7542 /* check if it comes with domain name. */
7544 for (i
= 0; i
< hostnamelen
; i
++) {
7545 if (hostname
[i
] == '.')
7551 /* NOTE: hostname may not be NUL-terminated. */
7552 bzero(fqdn
, sizeof(fqdn
));
7553 bcopy(hostname
, fqdn
, hostnamelen
);
7554 fqdn
[hostnamelen
] = '\0';
7559 * get username@FQDN for the host/user.
7565 static char userfqdn
[MAXHOSTNAMELEN
+ MAXLOGNAME
+ 2];
7566 struct proc
*p
= curproc
;
7569 if (!p
|| !p
->p_pgrp
|| !p
->p_pgrp
->pg_session
)
7571 if (!(host
= key_getfqdn()))
7574 /* NOTE: s_login may not be-NUL terminated. */
7575 bzero(userfqdn
, sizeof(userfqdn
));
7576 bcopy(p
->p_pgrp
->pg_session
->s_login
, userfqdn
, MAXLOGNAME
);
7577 userfqdn
[MAXLOGNAME
] = '\0'; /* safeguard */
7578 q
= userfqdn
+ strlen(userfqdn
);
7580 bcopy(host
, q
, strlen(host
));
7588 /* record data transfer on SA, and update timestamps */
7590 key_sa_recordxfer(sav
, m
)
7591 struct secasvar
*sav
;
7594 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7597 panic("key_sa_recordxfer called with sav == NULL");
7599 panic("key_sa_recordxfer called with m == NULL");
7604 * XXX Currently, there is a difference of bytes size
7605 * between inbound and outbound processing.
7607 sav
->lft_c
->sadb_lifetime_bytes
+= m
->m_pkthdr
.len
;
7608 /* to check bytes lifetime is done in key_timehandler(). */
7611 * We use the number of packets as the unit of
7612 * sadb_lifetime_allocations. We increment the variable
7613 * whenever {esp,ah}_{in,out}put is called.
7615 sav
->lft_c
->sadb_lifetime_allocations
++;
7616 /* XXX check for expires? */
7619 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7620 * in seconds. HARD and SOFT lifetime are measured by the time
7621 * difference (again in seconds) from sadb_lifetime_usetime.
7625 * -----+-----+--------+---> t
7626 * <--------------> HARD
7632 sav
->lft_c
->sadb_lifetime_usetime
= tv
.tv_sec
;
7633 /* XXX check for expires? */
7641 key_sa_routechange(dst
)
7642 struct sockaddr
*dst
;
7644 struct secashead
*sah
;
7647 lck_mtx_lock(sadb_mutex
);
7648 LIST_FOREACH(sah
, &sahtree
, chain
) {
7649 ro
= &sah
->sa_route
;
7650 if (ro
->ro_rt
&& dst
->sa_len
== ro
->ro_dst
.sa_len
7651 && bcmp(dst
, &ro
->ro_dst
, dst
->sa_len
) == 0) {
7653 ro
->ro_rt
= (struct rtentry
*)NULL
;
7656 lck_mtx_unlock(sadb_mutex
);
7662 key_sa_chgstate(sav
, state
)
7663 struct secasvar
*sav
;
7666 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7669 panic("key_sa_chgstate called with sav == NULL");
7671 if (sav
->state
== state
)
7674 if (__LIST_CHAINED(sav
))
7675 LIST_REMOVE(sav
, chain
);
7678 LIST_INSERT_HEAD(&sav
->sah
->savtree
[state
], sav
, chain
);
7683 struct secasvar
*sav
;
7686 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7689 panic("key_sa_stir_iv called with sav == NULL");
7690 key_randomfill(sav
->iv
, sav
->ivlen
);
7694 static struct mbuf
*
7698 struct mbuf
*m
= NULL
, *n
;
7703 MGET(n
, M_DONTWAIT
, MT_DATA
);
7704 if (n
&& len
> MLEN
)
7705 MCLGET(n
, M_DONTWAIT
);
7713 n
->m_len
= M_TRAILINGSPACE(n
);
7714 /* use the bottom of mbuf, hoping we can prepend afterwards */
7715 if (n
->m_len
> len
) {
7716 t
= (n
->m_len
- len
) & ~(sizeof(long) - 1);