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
);
2853 if (newsav
->spihash
.le_prev
|| newsav
->spihash
.le_next
)
2854 LIST_REMOVE(newsav
, spihash
);
2864 newsav
->created
= tv
.tv_sec
;
2867 newsav
->pid
= mhp
->msg
->sadb_msg_pid
;
2872 newsav
->state
= SADB_SASTATE_LARVAL
;
2873 LIST_INSERT_TAIL(&sah
->savtree
[SADB_SASTATE_LARVAL
], newsav
,
2880 * free() SA variable entry.
2884 struct secasvar
*sav
;
2886 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2890 panic("key_delsav: NULL pointer is passed.\n");
2892 if (sav
->refcnt
> 0)
2893 return; /* can't free */
2895 /* remove from SA header */
2896 if (__LIST_CHAINED(sav
))
2897 LIST_REMOVE(sav
, chain
);
2899 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
)
2900 LIST_REMOVE(sav
, spihash
);
2902 if (sav
->key_auth
!= NULL
) {
2903 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
2904 KFREE(sav
->key_auth
);
2905 sav
->key_auth
= NULL
;
2907 if (sav
->key_enc
!= NULL
) {
2908 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
2909 KFREE(sav
->key_enc
);
2910 sav
->key_enc
= NULL
;
2913 bzero(sav
->sched
, sav
->schedlen
);
2917 if (sav
->replay
!= NULL
) {
2918 keydb_delsecreplay(sav
->replay
);
2921 if (sav
->lft_c
!= NULL
) {
2925 if (sav
->lft_h
!= NULL
) {
2929 if (sav
->lft_s
!= NULL
) {
2933 if (sav
->iv
!= NULL
) {
2947 * others : found, pointer to a SA.
2949 static struct secashead
*
2951 struct secasindex
*saidx
;
2953 struct secashead
*sah
;
2955 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2957 LIST_FOREACH(sah
, &sahtree
, chain
) {
2958 if (sah
->state
== SADB_SASTATE_DEAD
)
2960 if (key_cmpsaidx(&sah
->saidx
, saidx
, CMP_REQID
))
2968 * check not to be duplicated SPI.
2969 * NOTE: this function is too slow due to searching all SAD.
2972 * others : found, pointer to a SA.
2974 static struct secasvar
*
2975 key_checkspidup(saidx
, spi
)
2976 struct secasindex
*saidx
;
2979 struct secasvar
*sav
;
2980 u_int stateidx
, state
;
2982 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2984 /* check address family */
2985 if (saidx
->src
.ss_family
!= saidx
->dst
.ss_family
) {
2986 ipseclog((LOG_DEBUG
, "key_checkspidup: address family mismatched.\n"));
2991 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
2992 if (sav
->spi
!= spi
)
2995 stateidx
< _ARRAYLEN(saorder_state_alive
);
2997 state
= saorder_state_alive
[stateidx
];
2998 if (sav
->state
== state
&&
2999 key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
))
3008 key_setspi(sav
, spi
)
3009 struct secasvar
*sav
;
3014 if (sav
->spihash
.le_prev
|| sav
->spihash
.le_next
)
3015 LIST_REMOVE(sav
, spihash
);
3016 LIST_INSERT_HEAD(&spihash
[SPIHASH(spi
)], sav
, spihash
);
3021 * search SAD litmited alive SA, protocol, SPI.
3024 * others : found, pointer to a SA.
3026 static struct secasvar
*
3027 key_getsavbyspi(sah
, spi
)
3028 struct secashead
*sah
;
3031 struct secasvar
*sav
, *match
;
3032 u_int stateidx
, state
, matchidx
;
3035 matchidx
= _ARRAYLEN(saorder_state_alive
);
3036 LIST_FOREACH(sav
, &spihash
[SPIHASH(spi
)], spihash
) {
3037 if (sav
->spi
!= spi
)
3039 if (sav
->sah
!= sah
)
3041 for (stateidx
= 0; stateidx
< matchidx
; stateidx
++) {
3042 state
= saorder_state_alive
[stateidx
];
3043 if (sav
->state
== state
) {
3045 matchidx
= stateidx
;
3055 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
3056 * You must update these if need.
3060 * does not modify mbuf. does not free mbuf on error.
3063 key_setsaval(sav
, m
, mhp
)
3064 struct secasvar
*sav
;
3066 const struct sadb_msghdr
*mhp
;
3069 const struct esp_algorithm
*algo
;
3074 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3077 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
3078 panic("key_setsaval: NULL pointer is passed.\n");
3080 /* initialization */
3082 sav
->key_auth
= NULL
;
3083 sav
->key_enc
= NULL
;
3090 sav
->remote_ike_port
= 0;
3091 sav
->natt_last_activity
= natt_now
;
3094 if (mhp
->ext
[SADB_EXT_SA
] != NULL
) {
3095 const struct sadb_sa
*sa0
;
3097 sa0
= (const struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
3098 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(*sa0
)) {
3099 ipseclog((LOG_DEBUG
, "key_setsaval: invalid message size.\n"));
3104 sav
->alg_auth
= sa0
->sadb_sa_auth
;
3105 sav
->alg_enc
= sa0
->sadb_sa_encrypt
;
3106 sav
->flags
= sa0
->sadb_sa_flags
;
3109 * Verify that a nat-traversal port was specified if
3110 * the nat-traversal flag is set.
3112 if ((sav
->flags
& SADB_X_EXT_NATT
) != 0) {
3113 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa_2
) ||
3114 ((struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
== 0) {
3115 ipseclog((LOG_DEBUG
, "key_setsaval: natt port not set.\n"));
3119 sav
->remote_ike_port
= ((struct sadb_sa_2
*)(sa0
))->sadb_sa_natt_port
;
3123 if ((sa0
->sadb_sa_flags
& SADB_X_EXT_OLD
) == 0) {
3124 sav
->replay
= keydb_newsecreplay(sa0
->sadb_sa_replay
);
3125 if (sav
->replay
== NULL
) {
3126 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3133 /* Authentication keys */
3134 if (mhp
->ext
[SADB_EXT_KEY_AUTH
] != NULL
) {
3135 const struct sadb_key
*key0
;
3138 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_AUTH
];
3139 len
= mhp
->extlen
[SADB_EXT_KEY_AUTH
];
3142 if (len
< sizeof(*key0
)) {
3143 ipseclog((LOG_DEBUG
, "key_setsaval: invalid auth key ext len. len = %d\n", len
));
3147 switch (mhp
->msg
->sadb_msg_satype
) {
3148 case SADB_SATYPE_AH
:
3149 case SADB_SATYPE_ESP
:
3150 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
3151 sav
->alg_auth
!= SADB_X_AALG_NULL
)
3154 case SADB_X_SATYPE_IPCOMP
:
3160 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_auth values.\n"));
3164 sav
->key_auth
= (struct sadb_key
*)key_newbuf(key0
, len
);
3165 if (sav
->key_auth
== NULL
) {
3166 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3172 /* Encryption key */
3173 if (mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] != NULL
) {
3174 const struct sadb_key
*key0
;
3177 key0
= (const struct sadb_key
*)mhp
->ext
[SADB_EXT_KEY_ENCRYPT
];
3178 len
= mhp
->extlen
[SADB_EXT_KEY_ENCRYPT
];
3181 if (len
< sizeof(*key0
)) {
3182 ipseclog((LOG_DEBUG
, "key_setsaval: invalid encryption key ext len. len = %d\n", len
));
3186 switch (mhp
->msg
->sadb_msg_satype
) {
3187 case SADB_SATYPE_ESP
:
3188 if (len
== PFKEY_ALIGN8(sizeof(struct sadb_key
)) &&
3189 sav
->alg_enc
!= SADB_EALG_NULL
) {
3190 ipseclog((LOG_DEBUG
, "key_setsaval: invalid ESP algorithm.\n"));
3194 sav
->key_enc
= (struct sadb_key
*)key_newbuf(key0
, len
);
3195 if (sav
->key_enc
== NULL
) {
3196 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3201 case SADB_X_SATYPE_IPCOMP
:
3202 if (len
!= PFKEY_ALIGN8(sizeof(struct sadb_key
)))
3204 sav
->key_enc
= NULL
; /*just in case*/
3206 case SADB_SATYPE_AH
:
3212 ipseclog((LOG_DEBUG
, "key_setsaval: invalid key_enc value.\n"));
3220 switch (mhp
->msg
->sadb_msg_satype
) {
3221 case SADB_SATYPE_ESP
:
3223 algo
= esp_algorithm_lookup(sav
->alg_enc
);
3224 if (algo
&& algo
->ivlen
)
3225 sav
->ivlen
= (*algo
->ivlen
)(algo
, sav
);
3226 if (sav
->ivlen
== 0)
3228 KMALLOC(sav
->iv
, caddr_t
, sav
->ivlen
);
3230 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3236 key_randomfill(sav
->iv
, sav
->ivlen
);
3239 case SADB_SATYPE_AH
:
3240 case SADB_X_SATYPE_IPCOMP
:
3243 ipseclog((LOG_DEBUG
, "key_setsaval: invalid SA type.\n"));
3250 sav
->created
= tv
.tv_sec
;
3252 /* make lifetime for CURRENT */
3253 KMALLOC(sav
->lft_c
, struct sadb_lifetime
*,
3254 sizeof(struct sadb_lifetime
));
3255 if (sav
->lft_c
== NULL
) {
3256 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3263 sav
->lft_c
->sadb_lifetime_len
=
3264 PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
3265 sav
->lft_c
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
3266 sav
->lft_c
->sadb_lifetime_allocations
= 0;
3267 sav
->lft_c
->sadb_lifetime_bytes
= 0;
3268 sav
->lft_c
->sadb_lifetime_addtime
= tv
.tv_sec
;
3269 sav
->lft_c
->sadb_lifetime_usetime
= 0;
3271 /* lifetimes for HARD and SOFT */
3273 const struct sadb_lifetime
*lft0
;
3275 lft0
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_HARD
];
3277 if (mhp
->extlen
[SADB_EXT_LIFETIME_HARD
] < sizeof(*lft0
)) {
3278 ipseclog((LOG_DEBUG
, "key_setsaval: invalid hard lifetime ext len.\n"));
3282 sav
->lft_h
= (struct sadb_lifetime
*)key_newbuf(lft0
,
3284 if (sav
->lft_h
== NULL
) {
3285 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3289 /* to be initialize ? */
3292 lft0
= (struct sadb_lifetime
*)mhp
->ext
[SADB_EXT_LIFETIME_SOFT
];
3294 if (mhp
->extlen
[SADB_EXT_LIFETIME_SOFT
] < sizeof(*lft0
)) {
3295 ipseclog((LOG_DEBUG
, "key_setsaval: invalid soft lifetime ext len.\n"));
3299 sav
->lft_s
= (struct sadb_lifetime
*)key_newbuf(lft0
,
3301 if (sav
->lft_s
== NULL
) {
3302 ipseclog((LOG_DEBUG
, "key_setsaval: No more memory.\n"));
3306 /* to be initialize ? */
3313 /* initialization */
3314 if (sav
->replay
!= NULL
) {
3315 keydb_delsecreplay(sav
->replay
);
3318 if (sav
->key_auth
!= NULL
) {
3319 bzero(_KEYBUF(sav
->key_auth
), _KEYLEN(sav
->key_auth
));
3320 KFREE(sav
->key_auth
);
3321 sav
->key_auth
= NULL
;
3323 if (sav
->key_enc
!= NULL
) {
3324 bzero(_KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
));
3325 KFREE(sav
->key_enc
);
3326 sav
->key_enc
= NULL
;
3329 bzero(sav
->sched
, sav
->schedlen
);
3333 if (sav
->iv
!= NULL
) {
3337 if (sav
->lft_c
!= NULL
) {
3341 if (sav
->lft_h
!= NULL
) {
3345 if (sav
->lft_s
!= NULL
) {
3354 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3360 struct secasvar
*sav
;
3363 int checkmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3364 int mustmask
= 0; /* 2^0: ealg 2^1: aalg 2^2: calg */
3368 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3370 /* check SPI value */
3371 switch (sav
->sah
->saidx
.proto
) {
3374 if (ntohl(sav
->spi
) >= 0 && ntohl(sav
->spi
) <= 255) {
3375 ipseclog((LOG_DEBUG
,
3376 "key_mature: illegal range of SPI %u.\n",
3377 (u_int32_t
)ntohl(sav
->spi
)));
3384 switch (sav
->sah
->saidx
.proto
) {
3387 if ((sav
->flags
& SADB_X_EXT_OLD
)
3388 && (sav
->flags
& SADB_X_EXT_DERIV
)) {
3389 ipseclog((LOG_DEBUG
, "key_mature: "
3390 "invalid flag (derived) given to old-esp.\n"));
3393 if (sav
->alg_auth
== SADB_AALG_NONE
)
3401 if (sav
->flags
& SADB_X_EXT_DERIV
) {
3402 ipseclog((LOG_DEBUG
, "key_mature: "
3403 "invalid flag (derived) given to AH SA.\n"));
3406 if (sav
->alg_enc
!= SADB_EALG_NONE
) {
3407 ipseclog((LOG_DEBUG
, "key_mature: "
3408 "protocol and algorithm mismated.\n"));
3414 case IPPROTO_IPCOMP
:
3415 if (sav
->alg_auth
!= SADB_AALG_NONE
) {
3416 ipseclog((LOG_DEBUG
, "key_mature: "
3417 "protocol and algorithm mismated.\n"));
3420 if ((sav
->flags
& SADB_X_EXT_RAWCPI
) == 0
3421 && ntohl(sav
->spi
) >= 0x10000) {
3422 ipseclog((LOG_DEBUG
, "key_mature: invalid cpi for IPComp.\n"));
3429 ipseclog((LOG_DEBUG
, "key_mature: Invalid satype.\n"));
3430 return EPROTONOSUPPORT
;
3433 /* check authentication algorithm */
3434 if ((checkmask
& 2) != 0) {
3435 const struct ah_algorithm
*algo
;
3438 algo
= ah_algorithm_lookup(sav
->alg_auth
);
3440 ipseclog((LOG_DEBUG
,"key_mature: "
3441 "unknown authentication algorithm.\n"));
3445 /* algorithm-dependent check */
3447 keylen
= sav
->key_auth
->sadb_key_bits
;
3450 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
3451 ipseclog((LOG_DEBUG
,
3452 "key_mature: invalid AH key length %d "
3453 "(%d-%d allowed)\n",
3454 keylen
, algo
->keymin
, algo
->keymax
));
3459 if ((*algo
->mature
)(sav
)) {
3460 /* message generated in per-algorithm function*/
3463 mature
= SADB_SATYPE_AH
;
3466 if ((mustmask
& 2) != 0 && mature
!= SADB_SATYPE_AH
) {
3467 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for AH\n"));
3472 /* check encryption algorithm */
3473 if ((checkmask
& 1) != 0) {
3475 const struct esp_algorithm
*algo
;
3478 algo
= esp_algorithm_lookup(sav
->alg_enc
);
3480 ipseclog((LOG_DEBUG
, "key_mature: unknown encryption algorithm.\n"));
3484 /* algorithm-dependent check */
3486 keylen
= sav
->key_enc
->sadb_key_bits
;
3489 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
3490 ipseclog((LOG_DEBUG
,
3491 "key_mature: invalid ESP key length %d "
3492 "(%d-%d allowed)\n",
3493 keylen
, algo
->keymin
, algo
->keymax
));
3498 if ((*algo
->mature
)(sav
)) {
3499 /* message generated in per-algorithm function*/
3502 mature
= SADB_SATYPE_ESP
;
3505 if ((mustmask
& 1) != 0 && mature
!= SADB_SATYPE_ESP
) {
3506 ipseclog((LOG_DEBUG
, "key_mature: no satisfy algorithm for ESP\n"));
3510 ipseclog((LOG_DEBUG
, "key_mature: ESP not supported in this configuration\n"));
3515 /* check compression algorithm */
3516 if ((checkmask
& 4) != 0) {
3517 const struct ipcomp_algorithm
*algo
;
3519 /* algorithm-dependent check */
3520 algo
= ipcomp_algorithm_lookup(sav
->alg_enc
);
3522 ipseclog((LOG_DEBUG
, "key_mature: unknown compression algorithm.\n"));
3527 key_sa_chgstate(sav
, SADB_SASTATE_MATURE
);
3533 * subroutine for SADB_GET and SADB_DUMP.
3535 static struct mbuf
*
3536 key_setdumpsa(sav
, type
, satype
, seq
, pid
)
3537 struct secasvar
*sav
;
3538 u_int8_t type
, satype
;
3541 struct mbuf
*result
= NULL
, *tres
= NULL
, *m
;
3546 SADB_EXT_SA
, SADB_X_EXT_SA2
,
3547 SADB_EXT_LIFETIME_HARD
, SADB_EXT_LIFETIME_SOFT
,
3548 SADB_EXT_LIFETIME_CURRENT
, SADB_EXT_ADDRESS_SRC
,
3549 SADB_EXT_ADDRESS_DST
, SADB_EXT_ADDRESS_PROXY
, SADB_EXT_KEY_AUTH
,
3550 SADB_EXT_KEY_ENCRYPT
, SADB_EXT_IDENTITY_SRC
,
3551 SADB_EXT_IDENTITY_DST
, SADB_EXT_SENSITIVITY
,
3554 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3556 m
= key_setsadbmsg(type
, 0, satype
, seq
, pid
, sav
->refcnt
);
3561 for (i
= sizeof(dumporder
)/sizeof(dumporder
[0]) - 1; i
>= 0; i
--) {
3564 switch (dumporder
[i
]) {
3566 m
= key_setsadbsa(sav
);
3571 case SADB_X_EXT_SA2
:
3572 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
3573 sav
->replay
? sav
->replay
->count
: 0,
3574 sav
->sah
->saidx
.reqid
);
3579 case SADB_EXT_ADDRESS_SRC
:
3580 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
3581 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
3582 FULLMASK
, IPSEC_ULPROTO_ANY
);
3587 case SADB_EXT_ADDRESS_DST
:
3588 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
3589 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
3590 FULLMASK
, IPSEC_ULPROTO_ANY
);
3595 case SADB_EXT_KEY_AUTH
:
3598 l
= PFKEY_UNUNIT64(sav
->key_auth
->sadb_key_len
);
3602 case SADB_EXT_KEY_ENCRYPT
:
3605 l
= PFKEY_UNUNIT64(sav
->key_enc
->sadb_key_len
);
3609 case SADB_EXT_LIFETIME_CURRENT
:
3612 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_c
)->sadb_ext_len
);
3616 case SADB_EXT_LIFETIME_HARD
:
3619 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_h
)->sadb_ext_len
);
3623 case SADB_EXT_LIFETIME_SOFT
:
3626 l
= PFKEY_UNUNIT64(((struct sadb_ext
*)sav
->lft_s
)->sadb_ext_len
);
3630 case SADB_EXT_ADDRESS_PROXY
:
3631 case SADB_EXT_IDENTITY_SRC
:
3632 case SADB_EXT_IDENTITY_DST
:
3633 /* XXX: should we brought from SPD ? */
3634 case SADB_EXT_SENSITIVITY
:
3639 if ((!m
&& !p
) || (m
&& p
))
3642 M_PREPEND(tres
, l
, M_DONTWAIT
);
3645 bcopy(p
, mtod(tres
, caddr_t
), l
);
3649 m
= key_alloc_mbuf(l
);
3652 m_copyback(m
, 0, l
, p
);
3660 m_cat(result
, tres
);
3662 if (result
->m_len
< sizeof(struct sadb_msg
)) {
3663 result
= m_pullup(result
, sizeof(struct sadb_msg
));
3668 result
->m_pkthdr
.len
= 0;
3669 for (m
= result
; m
; m
= m
->m_next
)
3670 result
->m_pkthdr
.len
+= m
->m_len
;
3672 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
3673 PFKEY_UNIT64(result
->m_pkthdr
.len
);
3684 * set data into sadb_msg.
3686 static struct mbuf
*
3687 key_setsadbmsg(type
, tlen
, satype
, seq
, pid
, reserved
)
3688 u_int8_t type
, satype
;
3698 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
3701 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
3702 if (m
&& len
> MHLEN
) {
3703 MCLGET(m
, M_DONTWAIT
);
3704 if ((m
->m_flags
& M_EXT
) == 0) {
3711 m
->m_pkthdr
.len
= m
->m_len
= len
;
3714 p
= mtod(m
, struct sadb_msg
*);
3717 p
->sadb_msg_version
= PF_KEY_V2
;
3718 p
->sadb_msg_type
= type
;
3719 p
->sadb_msg_errno
= 0;
3720 p
->sadb_msg_satype
= satype
;
3721 p
->sadb_msg_len
= PFKEY_UNIT64(tlen
);
3722 p
->sadb_msg_reserved
= reserved
;
3723 p
->sadb_msg_seq
= seq
;
3724 p
->sadb_msg_pid
= (u_int32_t
)pid
;
3730 * copy secasvar data into sadb_address.
3732 static struct mbuf
*
3734 struct secasvar
*sav
;
3740 len
= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
3741 m
= key_alloc_mbuf(len
);
3742 if (!m
|| m
->m_next
) { /*XXX*/
3748 p
= mtod(m
, struct sadb_sa
*);
3751 p
->sadb_sa_len
= PFKEY_UNIT64(len
);
3752 p
->sadb_sa_exttype
= SADB_EXT_SA
;
3753 p
->sadb_sa_spi
= sav
->spi
;
3754 p
->sadb_sa_replay
= (sav
->replay
!= NULL
? sav
->replay
->wsize
: 0);
3755 p
->sadb_sa_state
= sav
->state
;
3756 p
->sadb_sa_auth
= sav
->alg_auth
;
3757 p
->sadb_sa_encrypt
= sav
->alg_enc
;
3758 p
->sadb_sa_flags
= sav
->flags
;
3764 * set data into sadb_address.
3766 static struct mbuf
*
3767 key_setsadbaddr(exttype
, saddr
, prefixlen
, ul_proto
)
3769 struct sockaddr
*saddr
;
3774 struct sadb_address
*p
;
3777 len
= PFKEY_ALIGN8(sizeof(struct sadb_address
)) +
3778 PFKEY_ALIGN8(saddr
->sa_len
);
3779 m
= key_alloc_mbuf(len
);
3780 if (!m
|| m
->m_next
) { /*XXX*/
3786 p
= mtod(m
, struct sadb_address
*);
3789 p
->sadb_address_len
= PFKEY_UNIT64(len
);
3790 p
->sadb_address_exttype
= exttype
;
3791 p
->sadb_address_proto
= ul_proto
;
3792 if (prefixlen
== FULLMASK
) {
3793 switch (saddr
->sa_family
) {
3795 prefixlen
= sizeof(struct in_addr
) << 3;
3798 prefixlen
= sizeof(struct in6_addr
) << 3;
3804 p
->sadb_address_prefixlen
= prefixlen
;
3805 p
->sadb_address_reserved
= 0;
3808 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_address
)),
3816 * set data into sadb_ident.
3818 static struct mbuf
*
3819 key_setsadbident(exttype
, idtype
, string
, stringlen
, id
)
3820 u_int16_t exttype
, idtype
;
3826 struct sadb_ident
*p
;
3829 len
= PFKEY_ALIGN8(sizeof(struct sadb_ident
)) + PFKEY_ALIGN8(stringlen
);
3830 m
= key_alloc_mbuf(len
);
3831 if (!m
|| m
->m_next
) { /*XXX*/
3837 p
= mtod(m
, struct sadb_ident
*);
3840 p
->sadb_ident_len
= PFKEY_UNIT64(len
);
3841 p
->sadb_ident_exttype
= exttype
;
3842 p
->sadb_ident_type
= idtype
;
3843 p
->sadb_ident_reserved
= 0;
3844 p
->sadb_ident_id
= id
;
3847 mtod(m
, caddr_t
) + PFKEY_ALIGN8(sizeof(struct sadb_ident
)),
3855 * set data into sadb_x_sa2.
3857 static struct mbuf
*
3858 key_setsadbxsa2(mode
, seq
, reqid
)
3860 u_int32_t seq
, reqid
;
3863 struct sadb_x_sa2
*p
;
3866 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_sa2
));
3867 m
= key_alloc_mbuf(len
);
3868 if (!m
|| m
->m_next
) { /*XXX*/
3874 p
= mtod(m
, struct sadb_x_sa2
*);
3877 p
->sadb_x_sa2_len
= PFKEY_UNIT64(len
);
3878 p
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
3879 p
->sadb_x_sa2_mode
= mode
;
3880 p
->sadb_x_sa2_reserved1
= 0;
3881 p
->sadb_x_sa2_reserved2
= 0;
3882 p
->sadb_x_sa2_sequence
= seq
;
3883 p
->sadb_x_sa2_reqid
= reqid
;
3889 * set data into sadb_x_policy
3891 static struct mbuf
*
3892 key_setsadbxpolicy(type
, dir
, id
)
3898 struct sadb_x_policy
*p
;
3901 len
= PFKEY_ALIGN8(sizeof(struct sadb_x_policy
));
3902 m
= key_alloc_mbuf(len
);
3903 if (!m
|| m
->m_next
) { /*XXX*/
3909 p
= mtod(m
, struct sadb_x_policy
*);
3912 p
->sadb_x_policy_len
= PFKEY_UNIT64(len
);
3913 p
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3914 p
->sadb_x_policy_type
= type
;
3915 p
->sadb_x_policy_dir
= dir
;
3916 p
->sadb_x_policy_id
= id
;
3923 * copy a buffer into the new buffer allocated.
3926 key_newbuf(src
, len
)
3932 KMALLOC(new, caddr_t
, len
);
3934 ipseclog((LOG_DEBUG
, "key_newbuf: No more memory.\n"));
3937 bcopy(src
, new, len
);
3942 /* compare my own address
3943 * OUT: 1: true, i.e. my address.
3948 struct sockaddr
*sa
;
3951 struct sockaddr_in
*sin
;
3952 struct in_ifaddr
*ia
;
3957 panic("key_ismyaddr: NULL pointer is passed.\n");
3959 switch (sa
->sa_family
) {
3962 lck_mtx_lock(rt_mtx
);
3963 sin
= (struct sockaddr_in
*)sa
;
3964 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
3965 ia
= ia
->ia_link
.tqe_next
)
3967 if (sin
->sin_family
== ia
->ia_addr
.sin_family
&&
3968 sin
->sin_len
== ia
->ia_addr
.sin_len
&&
3969 sin
->sin_addr
.s_addr
== ia
->ia_addr
.sin_addr
.s_addr
)
3971 lck_mtx_unlock(rt_mtx
);
3975 lck_mtx_unlock(rt_mtx
);
3980 return key_ismyaddr6((struct sockaddr_in6
*)sa
);
3989 * compare my own address for IPv6.
3992 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3994 #include <netinet6/in6_var.h>
3998 struct sockaddr_in6
*sin6
;
4000 struct in6_ifaddr
*ia
;
4001 struct in6_multi
*in6m
;
4003 lck_mtx_lock(nd6_mutex
);
4004 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
4005 if (key_sockaddrcmp((struct sockaddr
*)&sin6
,
4006 (struct sockaddr
*)&ia
->ia_addr
, 0) == 0) {
4007 lck_mtx_unlock(nd6_mutex
);
4013 * XXX why do we care about multlicast here while we don't care
4014 * about IPv4 multicast??
4018 IN6_LOOKUP_MULTI(sin6
->sin6_addr
, ia
->ia_ifp
, in6m
);
4020 lck_mtx_unlock(nd6_mutex
);
4024 lck_mtx_unlock(nd6_mutex
);
4026 /* loopback, just for safety */
4027 if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
4035 * compare two secasindex structure.
4036 * flag can specify to compare 2 saidxes.
4037 * compare two secasindex structure without both mode and reqid.
4038 * don't compare port.
4040 * saidx0: source, it can be in SAD.
4047 key_cmpsaidx(saidx0
, saidx1
, flag
)
4048 struct secasindex
*saidx0
, *saidx1
;
4052 if (saidx0
== NULL
&& saidx1
== NULL
)
4055 if (saidx0
== NULL
|| saidx1
== NULL
)
4058 if (saidx0
->proto
!= saidx1
->proto
)
4061 if (flag
== CMP_EXACTLY
) {
4062 if (saidx0
->mode
!= saidx1
->mode
)
4064 if (saidx0
->reqid
!= saidx1
->reqid
)
4066 if (bcmp(&saidx0
->src
, &saidx1
->src
, saidx0
->src
.ss_len
) != 0 ||
4067 bcmp(&saidx0
->dst
, &saidx1
->dst
, saidx0
->dst
.ss_len
) != 0)
4071 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
4072 if (flag
== CMP_MODE_REQID
4073 ||flag
== CMP_REQID
) {
4075 * If reqid of SPD is non-zero, unique SA is required.
4076 * The result must be of same reqid in this case.
4078 if (saidx1
->reqid
!= 0 && saidx0
->reqid
!= saidx1
->reqid
)
4082 if (flag
== CMP_MODE_REQID
) {
4083 if (saidx0
->mode
!= IPSEC_MODE_ANY
4084 && saidx0
->mode
!= saidx1
->mode
)
4088 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->src
,
4089 (struct sockaddr
*)&saidx1
->src
, 0) != 0) {
4092 if (key_sockaddrcmp((struct sockaddr
*)&saidx0
->dst
,
4093 (struct sockaddr
*)&saidx1
->dst
, 0) != 0) {
4102 * compare two secindex structure exactly.
4104 * spidx0: source, it is often in SPD.
4105 * spidx1: object, it is often from PFKEY message.
4111 key_cmpspidx_exactly(spidx0
, spidx1
)
4112 struct secpolicyindex
*spidx0
, *spidx1
;
4115 if (spidx0
== NULL
&& spidx1
== NULL
)
4118 if (spidx0
== NULL
|| spidx1
== NULL
)
4121 if (spidx0
->prefs
!= spidx1
->prefs
4122 || spidx0
->prefd
!= spidx1
->prefd
4123 || spidx0
->ul_proto
!= spidx1
->ul_proto
)
4126 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->src
,
4127 (struct sockaddr
*)&spidx1
->src
, 1) != 0) {
4130 if (key_sockaddrcmp((struct sockaddr
*)&spidx0
->dst
,
4131 (struct sockaddr
*)&spidx1
->dst
, 1) != 0) {
4139 * compare two secindex structure with mask.
4141 * spidx0: source, it is often in SPD.
4142 * spidx1: object, it is often from IP header.
4148 key_cmpspidx_withmask(spidx0
, spidx1
)
4149 struct secpolicyindex
*spidx0
, *spidx1
;
4152 if (spidx0
== NULL
&& spidx1
== NULL
)
4155 if (spidx0
== NULL
|| spidx1
== NULL
)
4158 if (spidx0
->src
.ss_family
!= spidx1
->src
.ss_family
||
4159 spidx0
->dst
.ss_family
!= spidx1
->dst
.ss_family
||
4160 spidx0
->src
.ss_len
!= spidx1
->src
.ss_len
||
4161 spidx0
->dst
.ss_len
!= spidx1
->dst
.ss_len
)
4164 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4165 if (spidx0
->ul_proto
!= (u_int16_t
)IPSEC_ULPROTO_ANY
4166 && spidx0
->ul_proto
!= spidx1
->ul_proto
)
4169 switch (spidx0
->src
.ss_family
) {
4171 if (satosin(&spidx0
->src
)->sin_port
!= IPSEC_PORT_ANY
4172 && satosin(&spidx0
->src
)->sin_port
!=
4173 satosin(&spidx1
->src
)->sin_port
)
4175 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->src
)->sin_addr
,
4176 (caddr_t
)&satosin(&spidx1
->src
)->sin_addr
, spidx0
->prefs
))
4180 if (satosin6(&spidx0
->src
)->sin6_port
!= IPSEC_PORT_ANY
4181 && satosin6(&spidx0
->src
)->sin6_port
!=
4182 satosin6(&spidx1
->src
)->sin6_port
)
4185 * scope_id check. if sin6_scope_id is 0, we regard it
4186 * as a wildcard scope, which matches any scope zone ID.
4188 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
4189 satosin6(&spidx1
->src
)->sin6_scope_id
&&
4190 satosin6(&spidx0
->src
)->sin6_scope_id
!=
4191 satosin6(&spidx1
->src
)->sin6_scope_id
)
4193 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->src
)->sin6_addr
,
4194 (caddr_t
)&satosin6(&spidx1
->src
)->sin6_addr
, spidx0
->prefs
))
4199 if (bcmp(&spidx0
->src
, &spidx1
->src
, spidx0
->src
.ss_len
) != 0)
4204 switch (spidx0
->dst
.ss_family
) {
4206 if (satosin(&spidx0
->dst
)->sin_port
!= IPSEC_PORT_ANY
4207 && satosin(&spidx0
->dst
)->sin_port
!=
4208 satosin(&spidx1
->dst
)->sin_port
)
4210 if (!key_bbcmp((caddr_t
)&satosin(&spidx0
->dst
)->sin_addr
,
4211 (caddr_t
)&satosin(&spidx1
->dst
)->sin_addr
, spidx0
->prefd
))
4215 if (satosin6(&spidx0
->dst
)->sin6_port
!= IPSEC_PORT_ANY
4216 && satosin6(&spidx0
->dst
)->sin6_port
!=
4217 satosin6(&spidx1
->dst
)->sin6_port
)
4220 * scope_id check. if sin6_scope_id is 0, we regard it
4221 * as a wildcard scope, which matches any scope zone ID.
4223 if (satosin6(&spidx0
->src
)->sin6_scope_id
&&
4224 satosin6(&spidx1
->src
)->sin6_scope_id
&&
4225 satosin6(&spidx0
->dst
)->sin6_scope_id
!=
4226 satosin6(&spidx1
->dst
)->sin6_scope_id
)
4228 if (!key_bbcmp((caddr_t
)&satosin6(&spidx0
->dst
)->sin6_addr
,
4229 (caddr_t
)&satosin6(&spidx1
->dst
)->sin6_addr
, spidx0
->prefd
))
4234 if (bcmp(&spidx0
->dst
, &spidx1
->dst
, spidx0
->dst
.ss_len
) != 0)
4239 /* XXX Do we check other field ? e.g. flowinfo */
4244 /* returns 0 on match */
4246 key_sockaddrcmp(sa1
, sa2
, port
)
4247 struct sockaddr
*sa1
;
4248 struct sockaddr
*sa2
;
4251 if (sa1
->sa_family
!= sa2
->sa_family
|| sa1
->sa_len
!= sa2
->sa_len
)
4254 switch (sa1
->sa_family
) {
4256 if (sa1
->sa_len
!= sizeof(struct sockaddr_in
))
4258 if (satosin(sa1
)->sin_addr
.s_addr
!=
4259 satosin(sa2
)->sin_addr
.s_addr
) {
4262 if (port
&& satosin(sa1
)->sin_port
!= satosin(sa2
)->sin_port
)
4266 if (sa1
->sa_len
!= sizeof(struct sockaddr_in6
))
4267 return 1; /*EINVAL*/
4268 if (satosin6(sa1
)->sin6_scope_id
!=
4269 satosin6(sa2
)->sin6_scope_id
) {
4272 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1
)->sin6_addr
,
4273 &satosin6(sa2
)->sin6_addr
)) {
4277 satosin6(sa1
)->sin6_port
!= satosin6(sa2
)->sin6_port
) {
4281 if (bcmp(sa1
, sa2
, sa1
->sa_len
) != 0)
4290 * compare two buffers with mask.
4294 * bits: Number of bits to compare
4300 key_bbcmp(p1
, p2
, bits
)
4306 /* XXX: This could be considerably faster if we compare a word
4307 * at a time, but it is complicated on LSB Endian machines */
4309 /* Handle null pointers */
4310 if (p1
== NULL
|| p2
== NULL
)
4320 mask
= ~((1<<(8-bits
))-1);
4321 if ((*p1
& mask
) != (*p2
& mask
))
4324 return 1; /* Match! */
4329 * scanning SPD and SAD to check status for each entries,
4330 * and do to remove or to expire.
4331 * XXX: year 2038 problem may remain.
4335 key_timehandler(void)
4343 lck_mtx_lock(sadb_mutex
);
4346 struct secpolicy
*sp
, *nextsp
;
4348 for (dir
= 0; dir
< IPSEC_DIR_MAX
; dir
++) {
4349 for (sp
= LIST_FIRST(&sptree
[dir
]);
4353 nextsp
= LIST_NEXT(sp
, chain
);
4355 if (sp
->state
== IPSEC_SPSTATE_DEAD
) {
4360 if (sp
->lifetime
== 0 && sp
->validtime
== 0)
4363 /* the deletion will occur next time */
4365 && tv
.tv_sec
- sp
->created
> sp
->lifetime
)
4367 && tv
.tv_sec
- sp
->lastused
> sp
->validtime
)) {
4368 sp
->state
= IPSEC_SPSTATE_DEAD
;
4378 struct secashead
*sah
, *nextsah
;
4379 struct secasvar
*sav
, *nextsav
;
4381 for (sah
= LIST_FIRST(&sahtree
);
4385 nextsah
= LIST_NEXT(sah
, chain
);
4387 /* if sah has been dead, then delete it and process next sah. */
4388 if (sah
->state
== SADB_SASTATE_DEAD
) {
4393 /* if LARVAL entry doesn't become MATURE, delete it. */
4394 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_LARVAL
]);
4398 nextsav
= LIST_NEXT(sav
, chain
);
4400 if (tv
.tv_sec
- sav
->created
> key_larval_lifetime
) {
4406 * If this is a NAT traversal SA with no activity,
4407 * we need to send a keep alive.
4409 * Performed outside of the loop before so we will
4410 * only ever send one keepalive. The first SA on
4411 * the list is the one that will be used for sending
4412 * traffic, so this is the one we use for determining
4413 * when to send the keepalive.
4415 sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]);
4416 if (natt_keepalive_interval
&& sav
&& (sav
->flags
& SADB_X_EXT_NATT_KEEPALIVE
) != 0 &&
4417 (natt_now
- sav
->natt_last_activity
) >= natt_keepalive_interval
) {
4418 ipsec_send_natt_keepalive(sav
);
4422 * check MATURE entry to start to send expire message
4425 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_MATURE
]);
4429 nextsav
= LIST_NEXT(sav
, chain
);
4431 /* we don't need to check. */
4432 if (sav
->lft_s
== NULL
)
4436 if (sav
->lft_c
== NULL
) {
4437 ipseclog((LOG_DEBUG
,"key_timehandler: "
4438 "There is no CURRENT time, why?\n"));
4442 /* check SOFT lifetime */
4443 if (sav
->lft_s
->sadb_lifetime_addtime
!= 0
4444 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
4446 * check the SA if it has been used.
4447 * when it hasn't been used, delete it.
4448 * i don't think such SA will be used.
4450 if (sav
->lft_c
->sadb_lifetime_usetime
== 0) {
4451 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4455 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
4457 * XXX If we keep to send expire
4458 * message in the status of
4459 * DYING. Do remove below code.
4465 /* check SOFT lifetime by bytes */
4467 * XXX I don't know the way to delete this SA
4468 * when new SA is installed. Caution when it's
4469 * installed too big lifetime by time.
4471 else if (sav
->lft_s
->sadb_lifetime_bytes
!= 0
4472 && sav
->lft_s
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
4474 key_sa_chgstate(sav
, SADB_SASTATE_DYING
);
4476 * XXX If we keep to send expire
4477 * message in the status of
4478 * DYING. Do remove below code.
4484 /* check DYING entry to change status to DEAD. */
4485 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DYING
]);
4489 nextsav
= LIST_NEXT(sav
, chain
);
4491 /* we don't need to check. */
4492 if (sav
->lft_h
== NULL
)
4496 if (sav
->lft_c
== NULL
) {
4497 ipseclog((LOG_DEBUG
, "key_timehandler: "
4498 "There is no CURRENT time, why?\n"));
4502 if (sav
->lft_h
->sadb_lifetime_addtime
!= 0
4503 && tv
.tv_sec
- sav
->created
> sav
->lft_h
->sadb_lifetime_addtime
) {
4504 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4508 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4509 else if (sav
->lft_s
!= NULL
4510 && sav
->lft_s
->sadb_lifetime_addtime
!= 0
4511 && tv
.tv_sec
- sav
->created
> sav
->lft_s
->sadb_lifetime_addtime
) {
4513 * XXX: should be checked to be
4514 * installed the valid SA.
4518 * If there is no SA then sending
4524 /* check HARD lifetime by bytes */
4525 else if (sav
->lft_h
->sadb_lifetime_bytes
!= 0
4526 && sav
->lft_h
->sadb_lifetime_bytes
< sav
->lft_c
->sadb_lifetime_bytes
) {
4527 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
4533 /* delete entry in DEAD */
4534 for (sav
= LIST_FIRST(&sah
->savtree
[SADB_SASTATE_DEAD
]);
4538 nextsav
= LIST_NEXT(sav
, chain
);
4541 if (sav
->state
!= SADB_SASTATE_DEAD
) {
4542 ipseclog((LOG_DEBUG
, "key_timehandler: "
4543 "invalid sav->state "
4544 "(queue: %d SA: %d): "
4546 SADB_SASTATE_DEAD
, sav
->state
));
4550 * do not call key_freesav() here.
4551 * sav should already be freed, and sav->refcnt
4552 * shows other references to sav
4553 * (such as from SPD).
4559 #ifndef IPSEC_NONBLOCK_ACQUIRE
4562 struct secacq
*acq
, *nextacq
;
4564 for (acq
= LIST_FIRST(&acqtree
);
4568 nextacq
= LIST_NEXT(acq
, chain
);
4570 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
4571 && __LIST_CHAINED(acq
)) {
4572 LIST_REMOVE(acq
, chain
);
4581 struct secspacq
*acq
, *nextacq
;
4583 for (acq
= LIST_FIRST(&spacqtree
);
4587 nextacq
= LIST_NEXT(acq
, chain
);
4589 if (tv
.tv_sec
- acq
->created
> key_blockacq_lifetime
4590 && __LIST_CHAINED(acq
)) {
4591 LIST_REMOVE(acq
, chain
);
4597 /* initialize random seed */
4598 if (key_tick_init_random
++ > key_int_random
) {
4599 key_tick_init_random
= 0;
4605 lck_mtx_unlock(sadb_mutex
);
4606 #ifndef IPSEC_DEBUG2
4607 /* do exchange to tick time !! */
4608 (void)timeout((void *)key_timehandler
, (void *)0, hz
);
4609 #endif /* IPSEC_DEBUG2 */
4615 * to initialize a seed for random()
4621 /* Our PRNG is based on Yarrow and doesn't need to be seeded */
4628 srandom(tv
.tv_usec
);
4639 key_randomfill(&value
, sizeof(value
));
4644 key_randomfill(p
, l
)
4650 static int warn
= 1;
4653 read_random(p
, (u_int
)l
);
4656 n
= (size_t)read_random(p
, (u_int
)l
);
4660 bcopy(&v
, (u_int8_t
*)p
+ n
,
4661 l
- n
< sizeof(v
) ? l
- n
: sizeof(v
));
4665 printf("WARNING: pseudo-random number generator "
4666 "used for IPsec processing\n");
4674 * map SADB_SATYPE_* to IPPROTO_*.
4675 * if satype == SADB_SATYPE then satype is mapped to ~0.
4677 * 0: invalid satype.
4680 key_satype2proto(satype
)
4684 case SADB_SATYPE_UNSPEC
:
4685 return IPSEC_PROTO_ANY
;
4686 case SADB_SATYPE_AH
:
4688 case SADB_SATYPE_ESP
:
4690 case SADB_X_SATYPE_IPCOMP
:
4691 return IPPROTO_IPCOMP
;
4700 * map IPPROTO_* to SADB_SATYPE_*
4702 * 0: invalid protocol type.
4705 key_proto2satype(proto
)
4710 return SADB_SATYPE_AH
;
4712 return SADB_SATYPE_ESP
;
4713 case IPPROTO_IPCOMP
:
4714 return SADB_X_SATYPE_IPCOMP
;
4724 * SADB_GETSPI processing is to receive
4725 * <base, (SA2), src address, dst address, (SPI range)>
4726 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4727 * tree with the status of LARVAL, and send
4728 * <base, SA(*), address(SD)>
4731 * IN: mhp: pointer to the pointer to each header.
4732 * OUT: NULL if fail.
4733 * other if success, return pointer to the message to send.
4736 key_getspi(so
, m
, mhp
)
4739 const struct sadb_msghdr
*mhp
;
4741 struct sadb_address
*src0
, *dst0
;
4742 struct secasindex saidx
;
4743 struct secashead
*newsah
;
4744 struct secasvar
*newsav
;
4751 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4754 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
4755 panic("key_getspi: NULL pointer is passed.\n");
4757 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
4758 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
4759 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
4760 return key_senderror(so
, m
, EINVAL
);
4762 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
4763 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
4764 ipseclog((LOG_DEBUG
, "key_getspi: invalid message is passed.\n"));
4765 return key_senderror(so
, m
, EINVAL
);
4767 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
4768 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
4769 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
4771 mode
= IPSEC_MODE_ANY
;
4775 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
4776 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
4778 /* map satype to proto */
4779 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
4780 ipseclog((LOG_DEBUG
, "key_getspi: invalid satype is passed.\n"));
4781 return key_senderror(so
, m
, EINVAL
);
4784 /* make sure if port number is zero. */
4785 switch (((struct sockaddr
*)(src0
+ 1))->sa_family
) {
4787 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
4788 sizeof(struct sockaddr_in
))
4789 return key_senderror(so
, m
, EINVAL
);
4790 ((struct sockaddr_in
*)(src0
+ 1))->sin_port
= 0;
4793 if (((struct sockaddr
*)(src0
+ 1))->sa_len
!=
4794 sizeof(struct sockaddr_in6
))
4795 return key_senderror(so
, m
, EINVAL
);
4796 ((struct sockaddr_in6
*)(src0
+ 1))->sin6_port
= 0;
4801 switch (((struct sockaddr
*)(dst0
+ 1))->sa_family
) {
4803 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
4804 sizeof(struct sockaddr_in
))
4805 return key_senderror(so
, m
, EINVAL
);
4806 ((struct sockaddr_in
*)(dst0
+ 1))->sin_port
= 0;
4809 if (((struct sockaddr
*)(dst0
+ 1))->sa_len
!=
4810 sizeof(struct sockaddr_in6
))
4811 return key_senderror(so
, m
, EINVAL
);
4812 ((struct sockaddr_in6
*)(dst0
+ 1))->sin6_port
= 0;
4818 /* XXX boundary check against sa_len */
4819 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
4821 /* SPI allocation */
4822 spi
= key_do_getnewspi((struct sadb_spirange
*)mhp
->ext
[SADB_EXT_SPIRANGE
],
4825 return key_senderror(so
, m
, EINVAL
);
4827 /* get a SA index */
4828 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
4829 /* create a new SA index */
4830 if ((newsah
= key_newsah(&saidx
)) == NULL
) {
4831 ipseclog((LOG_DEBUG
, "key_getspi: No more memory.\n"));
4832 return key_senderror(so
, m
, ENOBUFS
);
4838 newsav
= key_newsav(m
, mhp
, newsah
, &error
);
4839 if (newsav
== NULL
) {
4840 /* XXX don't free new SA index allocated in above. */
4841 return key_senderror(so
, m
, error
);
4845 key_setspi(newsav
, htonl(spi
));
4847 #ifndef IPSEC_NONBLOCK_ACQUIRE
4848 /* delete the entry in acqtree */
4849 if (mhp
->msg
->sadb_msg_seq
!= 0) {
4851 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) != NULL
) {
4852 /* reset counter in order to deletion by timehandler. */
4855 acq
->created
= tv
.tv_sec
;
4862 struct mbuf
*n
, *nn
;
4863 struct sadb_sa
*m_sa
;
4864 struct sadb_msg
*newmsg
;
4867 /* create new sadb_msg to reply. */
4868 len
= PFKEY_ALIGN8(sizeof(struct sadb_msg
)) +
4869 PFKEY_ALIGN8(sizeof(struct sadb_sa
));
4871 return key_senderror(so
, m
, ENOBUFS
);
4873 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
4875 MCLGET(n
, M_DONTWAIT
);
4876 if ((n
->m_flags
& M_EXT
) == 0) {
4882 return key_senderror(so
, m
, ENOBUFS
);
4888 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
4889 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
4891 m_sa
= (struct sadb_sa
*)(mtod(n
, caddr_t
) + off
);
4892 m_sa
->sadb_sa_len
= PFKEY_UNIT64(sizeof(struct sadb_sa
));
4893 m_sa
->sadb_sa_exttype
= SADB_EXT_SA
;
4894 m_sa
->sadb_sa_spi
= htonl(spi
);
4895 off
+= PFKEY_ALIGN8(sizeof(struct sadb_sa
));
4899 panic("length inconsistency in key_getspi");
4902 int mbufItems
[] = {SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
4903 n
->m_next
= key_gather_mbuf(m
, mhp
, 0, sizeof(mbufItems
)/sizeof(int), mbufItems
);
4906 return key_senderror(so
, m
, ENOBUFS
);
4910 if (n
->m_len
< sizeof(struct sadb_msg
)) {
4911 n
= m_pullup(n
, sizeof(struct sadb_msg
));
4913 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
4916 n
->m_pkthdr
.len
= 0;
4917 for (nn
= n
; nn
; nn
= nn
->m_next
)
4918 n
->m_pkthdr
.len
+= nn
->m_len
;
4920 newmsg
= mtod(n
, struct sadb_msg
*);
4921 newmsg
->sadb_msg_seq
= newsav
->seq
;
4922 newmsg
->sadb_msg_errno
= 0;
4923 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
4926 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
4931 * allocating new SPI
4932 * called by key_getspi().
4938 key_do_getnewspi(spirange
, saidx
)
4939 struct sadb_spirange
*spirange
;
4940 struct secasindex
*saidx
;
4944 int count
= key_spi_trycnt
;
4946 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
4948 /* set spi range to allocate */
4949 if (spirange
!= NULL
) {
4950 min
= spirange
->sadb_spirange_min
;
4951 max
= spirange
->sadb_spirange_max
;
4953 min
= key_spi_minval
;
4954 max
= key_spi_maxval
;
4956 /* IPCOMP needs 2-byte SPI */
4957 if (saidx
->proto
== IPPROTO_IPCOMP
) {
4964 t
= min
; min
= max
; max
= t
;
4969 if (key_checkspidup(saidx
, min
) != NULL
) {
4970 ipseclog((LOG_DEBUG
, "key_do_getnewspi: SPI %u exists already.\n", min
));
4974 count
--; /* taking one cost. */
4982 /* when requesting to allocate spi ranged */
4984 /* generate pseudo-random SPI value ranged. */
4985 newspi
= min
+ (key_random() % (max
- min
+ 1));
4987 if (key_checkspidup(saidx
, newspi
) == NULL
)
4991 if (count
== 0 || newspi
== 0) {
4992 ipseclog((LOG_DEBUG
, "key_do_getnewspi: to allocate spi is failed.\n"));
4998 keystat
.getspi_count
=
4999 (keystat
.getspi_count
+ key_spi_trycnt
- count
) / 2;
5005 * SADB_UPDATE processing
5007 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5008 * key(AE), (identity(SD),) (sensitivity)>
5009 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
5011 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5012 * (identity(SD),) (sensitivity)>
5015 * m will always be freed.
5018 key_update(so
, m
, mhp
)
5021 const struct sadb_msghdr
*mhp
;
5023 struct sadb_sa
*sa0
;
5024 struct sadb_address
*src0
, *dst0
;
5025 struct secasindex saidx
;
5026 struct secashead
*sah
;
5027 struct secasvar
*sav
;
5033 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5036 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5037 panic("key_update: NULL pointer is passed.\n");
5039 /* map satype to proto */
5040 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5041 ipseclog((LOG_DEBUG
, "key_update: invalid satype is passed.\n"));
5042 return key_senderror(so
, m
, EINVAL
);
5045 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
5046 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5047 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
5048 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
5049 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
5050 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
5051 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
5052 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
5053 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
5054 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
5055 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
5056 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
5057 return key_senderror(so
, m
, EINVAL
);
5059 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
5060 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5061 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5062 ipseclog((LOG_DEBUG
, "key_update: invalid message is passed.\n"));
5063 return key_senderror(so
, m
, EINVAL
);
5065 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
5066 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
5067 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
5069 mode
= IPSEC_MODE_ANY
;
5072 /* XXX boundary checking for other extensions */
5074 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5075 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5076 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5078 /* XXX boundary check against sa_len */
5079 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
5081 /* get a SA header */
5082 if ((sah
= key_getsah(&saidx
)) == NULL
) {
5083 ipseclog((LOG_DEBUG
, "key_update: no SA index found.\n"));
5084 return key_senderror(so
, m
, ENOENT
);
5087 /* set spidx if there */
5089 error
= key_setident(sah
, m
, mhp
);
5091 return key_senderror(so
, m
, error
);
5093 /* find a SA with sequence number. */
5094 #if IPSEC_DOSEQCHECK
5095 if (mhp
->msg
->sadb_msg_seq
!= 0
5096 && (sav
= key_getsavbyseq(sah
, mhp
->msg
->sadb_msg_seq
)) == NULL
) {
5097 ipseclog((LOG_DEBUG
,
5098 "key_update: no larval SA with sequence %u exists.\n",
5099 mhp
->msg
->sadb_msg_seq
));
5100 return key_senderror(so
, m
, ENOENT
);
5103 if ((sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
)) == NULL
) {
5104 ipseclog((LOG_DEBUG
,
5105 "key_update: no such a SA found (spi:%u)\n",
5106 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
5107 return key_senderror(so
, m
, EINVAL
);
5111 /* validity check */
5112 if (sav
->sah
->saidx
.proto
!= proto
) {
5113 ipseclog((LOG_DEBUG
,
5114 "key_update: protocol mismatched (DB=%u param=%u)\n",
5115 sav
->sah
->saidx
.proto
, proto
));
5116 return key_senderror(so
, m
, EINVAL
);
5118 #if IPSEC_DOSEQCHECK
5119 if (sav
->spi
!= sa0
->sadb_sa_spi
) {
5120 ipseclog((LOG_DEBUG
,
5121 "key_update: SPI mismatched (DB:%u param:%u)\n",
5122 (u_int32_t
)ntohl(sav
->spi
),
5123 (u_int32_t
)ntohl(sa0
->sadb_sa_spi
)));
5124 return key_senderror(so
, m
, EINVAL
);
5127 if (sav
->pid
!= mhp
->msg
->sadb_msg_pid
) {
5128 ipseclog((LOG_DEBUG
,
5129 "key_update: pid mismatched (DB:%u param:%u)\n",
5130 sav
->pid
, mhp
->msg
->sadb_msg_pid
));
5131 return key_senderror(so
, m
, EINVAL
);
5134 /* copy sav values */
5135 error
= key_setsaval(sav
, m
, mhp
);
5138 return key_senderror(so
, m
, error
);
5141 /* check SA values to be mature. */
5142 if ((error
= key_mature(sav
)) != 0) {
5144 return key_senderror(so
, m
, error
);
5150 /* set msg buf from mhp */
5151 n
= key_getmsgbuf_x1(m
, mhp
);
5153 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
5154 return key_senderror(so
, m
, ENOBUFS
);
5158 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5163 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
5164 * only called by key_update().
5167 * others : found, pointer to a SA.
5169 #if IPSEC_DOSEQCHECK
5170 static struct secasvar
*
5171 key_getsavbyseq(sah
, seq
)
5172 struct secashead
*sah
;
5175 struct secasvar
*sav
;
5178 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5180 state
= SADB_SASTATE_LARVAL
;
5182 /* search SAD with sequence number ? */
5183 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
5185 KEY_CHKSASTATE(state
, sav
->state
, "key_getsabyseq");
5187 if (sav
->seq
== seq
) {
5189 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
5190 printf("DP key_getsavbyseq cause "
5191 "refcnt++:%d SA:%p\n",
5202 * SADB_ADD processing
5203 * add a entry to SA database, when received
5204 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5205 * key(AE), (identity(SD),) (sensitivity)>
5208 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5209 * (identity(SD),) (sensitivity)>
5212 * IGNORE identity and sensitivity messages.
5214 * m will always be freed.
5220 const struct sadb_msghdr
*mhp
;
5222 struct sadb_sa
*sa0
;
5223 struct sadb_address
*src0
, *dst0
;
5224 struct secasindex saidx
;
5225 struct secashead
*newsah
;
5226 struct secasvar
*newsav
;
5232 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5235 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5236 panic("key_add: NULL pointer is passed.\n");
5238 /* map satype to proto */
5239 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5240 ipseclog((LOG_DEBUG
, "key_add: invalid satype is passed.\n"));
5241 return key_senderror(so
, m
, EINVAL
);
5244 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
5245 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5246 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
5247 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
5248 mhp
->ext
[SADB_EXT_KEY_ENCRYPT
] == NULL
) ||
5249 (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_AH
&&
5250 mhp
->ext
[SADB_EXT_KEY_AUTH
] == NULL
) ||
5251 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] != NULL
&&
5252 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] == NULL
) ||
5253 (mhp
->ext
[SADB_EXT_LIFETIME_HARD
] == NULL
&&
5254 mhp
->ext
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
5255 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
5256 return key_senderror(so
, m
, EINVAL
);
5258 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
5259 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5260 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5262 ipseclog((LOG_DEBUG
, "key_add: invalid message is passed.\n"));
5263 return key_senderror(so
, m
, EINVAL
);
5265 if (mhp
->ext
[SADB_X_EXT_SA2
] != NULL
) {
5266 mode
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
5267 reqid
= ((struct sadb_x_sa2
*)mhp
->ext
[SADB_X_EXT_SA2
])->sadb_x_sa2_reqid
;
5269 mode
= IPSEC_MODE_ANY
;
5273 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5274 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
5275 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
5277 /* XXX boundary check against sa_len */
5278 KEY_SETSECASIDX(proto
, mode
, reqid
, src0
+ 1, dst0
+ 1, &saidx
);
5280 /* get a SA header */
5281 if ((newsah
= key_getsah(&saidx
)) == NULL
) {
5282 /* create a new SA header */
5283 if ((newsah
= key_newsah(&saidx
)) == NULL
) {
5284 ipseclog((LOG_DEBUG
, "key_add: No more memory.\n"));
5285 return key_senderror(so
, m
, ENOBUFS
);
5289 /* set spidx if there */
5291 error
= key_setident(newsah
, m
, mhp
);
5293 return key_senderror(so
, m
, error
);
5296 /* create new SA entry. */
5297 /* We can create new SA only if SPI is differenct. */
5298 if (key_getsavbyspi(newsah
, sa0
->sadb_sa_spi
)) {
5299 ipseclog((LOG_DEBUG
, "key_add: SA already exists.\n"));
5300 return key_senderror(so
, m
, EEXIST
);
5302 newsav
= key_newsav(m
, mhp
, newsah
, &error
);
5303 if (newsav
== NULL
) {
5304 return key_senderror(so
, m
, error
);
5307 /* check SA values to be mature. */
5308 if ((error
= key_mature(newsav
)) != 0) {
5309 key_freesav(newsav
);
5310 return key_senderror(so
, m
, error
);
5314 * don't call key_freesav() here, as we would like to keep the SA
5315 * in the database on success.
5321 /* set msg buf from mhp */
5322 n
= key_getmsgbuf_x1(m
, mhp
);
5324 ipseclog((LOG_DEBUG
, "key_update: No more memory.\n"));
5325 return key_senderror(so
, m
, ENOBUFS
);
5329 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5335 key_setident(sah
, m
, mhp
)
5336 struct secashead
*sah
;
5338 const struct sadb_msghdr
*mhp
;
5340 const struct sadb_ident
*idsrc
, *iddst
;
5341 int idsrclen
, iddstlen
;
5343 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5346 if (sah
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5347 panic("key_setident: NULL pointer is passed.\n");
5349 /* don't make buffer if not there */
5350 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
&&
5351 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
5357 if (mhp
->ext
[SADB_EXT_IDENTITY_SRC
] == NULL
||
5358 mhp
->ext
[SADB_EXT_IDENTITY_DST
] == NULL
) {
5359 ipseclog((LOG_DEBUG
, "key_setident: invalid identity.\n"));
5363 idsrc
= (const struct sadb_ident
*)mhp
->ext
[SADB_EXT_IDENTITY_SRC
];
5364 iddst
= (const struct sadb_ident
*)mhp
->ext
[SADB_EXT_IDENTITY_DST
];
5365 idsrclen
= mhp
->extlen
[SADB_EXT_IDENTITY_SRC
];
5366 iddstlen
= mhp
->extlen
[SADB_EXT_IDENTITY_DST
];
5368 /* validity check */
5369 if (idsrc
->sadb_ident_type
!= iddst
->sadb_ident_type
) {
5370 ipseclog((LOG_DEBUG
, "key_setident: ident type mismatch.\n"));
5374 switch (idsrc
->sadb_ident_type
) {
5375 case SADB_IDENTTYPE_PREFIX
:
5376 case SADB_IDENTTYPE_FQDN
:
5377 case SADB_IDENTTYPE_USERFQDN
:
5379 /* XXX do nothing */
5385 /* make structure */
5386 KMALLOC(sah
->idents
, struct sadb_ident
*, idsrclen
);
5387 if (sah
->idents
== NULL
) {
5388 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
5391 KMALLOC(sah
->identd
, struct sadb_ident
*, iddstlen
);
5392 if (sah
->identd
== NULL
) {
5395 ipseclog((LOG_DEBUG
, "key_setident: No more memory.\n"));
5398 bcopy(idsrc
, sah
->idents
, idsrclen
);
5399 bcopy(iddst
, sah
->identd
, iddstlen
);
5405 * m will not be freed on return.
5406 * it is caller's responsibility to free the result.
5408 static struct mbuf
*
5409 key_getmsgbuf_x1(m
, mhp
)
5411 const struct sadb_msghdr
*mhp
;
5414 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
5415 SADB_X_EXT_SA2
, SADB_EXT_ADDRESS_SRC
,
5416 SADB_EXT_ADDRESS_DST
, SADB_EXT_LIFETIME_HARD
,
5417 SADB_EXT_LIFETIME_SOFT
, SADB_EXT_IDENTITY_SRC
,
5418 SADB_EXT_IDENTITY_DST
};
5421 if (m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5422 panic("key_getmsgbuf_x1: NULL pointer is passed.\n");
5424 /* create new sadb_msg to reply. */
5425 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
5429 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5430 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5434 mtod(n
, struct sadb_msg
*)->sadb_msg_errno
= 0;
5435 mtod(n
, struct sadb_msg
*)->sadb_msg_len
=
5436 PFKEY_UNIT64(n
->m_pkthdr
.len
);
5441 static int key_delete_all(struct socket
*, struct mbuf
*,
5442 const struct sadb_msghdr
*, u_int16_t
);
5445 * SADB_DELETE processing
5447 * <base, SA(*), address(SD)>
5448 * from the ikmpd, and set SADB_SASTATE_DEAD,
5450 * <base, SA(*), address(SD)>
5453 * m will always be freed.
5456 key_delete(so
, m
, mhp
)
5459 const struct sadb_msghdr
*mhp
;
5461 struct sadb_sa
*sa0
;
5462 struct sadb_address
*src0
, *dst0
;
5463 struct secasindex saidx
;
5464 struct secashead
*sah
;
5465 struct secasvar
*sav
= NULL
;
5468 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5471 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5472 panic("key_delete: NULL pointer is passed.\n");
5474 /* map satype to proto */
5475 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5476 ipseclog((LOG_DEBUG
, "key_delete: invalid satype is passed.\n"));
5477 return key_senderror(so
, m
, EINVAL
);
5480 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5481 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
5482 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
5483 return key_senderror(so
, m
, EINVAL
);
5486 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5487 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5488 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
5489 return key_senderror(so
, m
, EINVAL
);
5492 if (mhp
->ext
[SADB_EXT_SA
] == NULL
) {
5494 * Caller wants us to delete all non-LARVAL SAs
5495 * that match the src/dst. This is used during
5496 * IKE INITIAL-CONTACT.
5498 ipseclog((LOG_DEBUG
, "key_delete: doing delete all.\n"));
5499 return key_delete_all(so
, m
, mhp
, proto
);
5500 } else if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
)) {
5501 ipseclog((LOG_DEBUG
, "key_delete: invalid message is passed.\n"));
5502 return key_senderror(so
, m
, EINVAL
);
5505 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5506 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5507 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5509 /* XXX boundary check against sa_len */
5510 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
5512 /* get a SA header */
5513 LIST_FOREACH(sah
, &sahtree
, chain
) {
5514 if (sah
->state
== SADB_SASTATE_DEAD
)
5516 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
5519 /* get a SA with SPI. */
5520 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
5525 ipseclog((LOG_DEBUG
, "key_delete: no SA found.\n"));
5526 return key_senderror(so
, m
, ENOENT
);
5529 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5535 struct sadb_msg
*newmsg
;
5536 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_SA
,
5537 SADB_EXT_ADDRESS_SRC
, SADB_EXT_ADDRESS_DST
};
5539 /* create new sadb_msg to reply. */
5540 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
5542 return key_senderror(so
, m
, ENOBUFS
);
5544 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5545 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5547 return key_senderror(so
, m
, ENOBUFS
);
5549 newmsg
= mtod(n
, struct sadb_msg
*);
5550 newmsg
->sadb_msg_errno
= 0;
5551 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
5554 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5559 * delete all SAs for src/dst. Called from key_delete().
5562 key_delete_all(so
, m
, mhp
, proto
)
5565 const struct sadb_msghdr
*mhp
;
5568 struct sadb_address
*src0
, *dst0
;
5569 struct secasindex saidx
;
5570 struct secashead
*sah
;
5571 struct secasvar
*sav
, *nextsav
;
5572 u_int stateidx
, state
;
5574 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5576 src0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_SRC
]);
5577 dst0
= (struct sadb_address
*)(mhp
->ext
[SADB_EXT_ADDRESS_DST
]);
5579 /* XXX boundary check against sa_len */
5580 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
5582 LIST_FOREACH(sah
, &sahtree
, chain
) {
5583 if (sah
->state
== SADB_SASTATE_DEAD
)
5585 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
5588 /* Delete all non-LARVAL SAs. */
5590 stateidx
< _ARRAYLEN(saorder_state_alive
);
5592 state
= saorder_state_alive
[stateidx
];
5593 if (state
== SADB_SASTATE_LARVAL
)
5595 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
5596 sav
!= NULL
; sav
= nextsav
) {
5597 nextsav
= LIST_NEXT(sav
, chain
);
5599 if (sav
->state
!= state
) {
5600 ipseclog((LOG_DEBUG
, "key_delete_all: "
5601 "invalid sav->state "
5602 "(queue: %d SA: %d)\n",
5603 state
, sav
->state
));
5607 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
5614 struct sadb_msg
*newmsg
;
5615 int mbufItems
[] = {SADB_EXT_RESERVED
, SADB_EXT_ADDRESS_SRC
,
5616 SADB_EXT_ADDRESS_DST
};
5618 /* create new sadb_msg to reply. */
5619 n
= key_gather_mbuf(m
, mhp
, 1, sizeof(mbufItems
)/sizeof(int), mbufItems
);
5621 return key_senderror(so
, m
, ENOBUFS
);
5623 if (n
->m_len
< sizeof(struct sadb_msg
)) {
5624 n
= m_pullup(n
, sizeof(struct sadb_msg
));
5626 return key_senderror(so
, m
, ENOBUFS
);
5628 newmsg
= mtod(n
, struct sadb_msg
*);
5629 newmsg
->sadb_msg_errno
= 0;
5630 newmsg
->sadb_msg_len
= PFKEY_UNIT64(n
->m_pkthdr
.len
);
5633 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ALL
);
5638 * SADB_GET processing
5640 * <base, SA(*), address(SD)>
5641 * from the ikmpd, and get a SP and a SA to respond,
5643 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5644 * (identity(SD),) (sensitivity)>
5647 * m will always be freed.
5653 const struct sadb_msghdr
*mhp
;
5655 struct sadb_sa
*sa0
;
5656 struct sadb_address
*src0
, *dst0
;
5657 struct secasindex saidx
;
5658 struct secashead
*sah
;
5659 struct secasvar
*sav
= NULL
;
5662 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
5665 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
5666 panic("key_get: NULL pointer is passed.\n");
5668 /* map satype to proto */
5669 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
5670 ipseclog((LOG_DEBUG
, "key_get: invalid satype is passed.\n"));
5671 return key_senderror(so
, m
, EINVAL
);
5674 if (mhp
->ext
[SADB_EXT_SA
] == NULL
||
5675 mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
5676 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
) {
5677 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
5678 return key_senderror(so
, m
, EINVAL
);
5680 if (mhp
->extlen
[SADB_EXT_SA
] < sizeof(struct sadb_sa
) ||
5681 mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
5682 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
)) {
5683 ipseclog((LOG_DEBUG
, "key_get: invalid message is passed.\n"));
5684 return key_senderror(so
, m
, EINVAL
);
5687 sa0
= (struct sadb_sa
*)mhp
->ext
[SADB_EXT_SA
];
5688 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
5689 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
5691 /* XXX boundary check against sa_len */
5692 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
5694 /* get a SA header */
5695 LIST_FOREACH(sah
, &sahtree
, chain
) {
5696 if (sah
->state
== SADB_SASTATE_DEAD
)
5698 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_HEAD
) == 0)
5701 /* get a SA with SPI. */
5702 sav
= key_getsavbyspi(sah
, sa0
->sadb_sa_spi
);
5707 ipseclog((LOG_DEBUG
, "key_get: no SA found.\n"));
5708 return key_senderror(so
, m
, ENOENT
);
5715 /* map proto to satype */
5716 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
5717 ipseclog((LOG_DEBUG
, "key_get: there was invalid proto in SAD.\n"));
5718 return key_senderror(so
, m
, EINVAL
);
5721 /* create new sadb_msg to reply. */
5722 n
= key_setdumpsa(sav
, SADB_GET
, satype
, mhp
->msg
->sadb_msg_seq
,
5723 mhp
->msg
->sadb_msg_pid
);
5725 return key_senderror(so
, m
, ENOBUFS
);
5728 return key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
5732 /* XXX make it sysctl-configurable? */
5734 key_getcomb_setlifetime(comb
)
5735 struct sadb_comb
*comb
;
5738 comb
->sadb_comb_soft_allocations
= 1;
5739 comb
->sadb_comb_hard_allocations
= 1;
5740 comb
->sadb_comb_soft_bytes
= 0;
5741 comb
->sadb_comb_hard_bytes
= 0;
5742 comb
->sadb_comb_hard_addtime
= 86400; /* 1 day */
5743 comb
->sadb_comb_soft_addtime
= comb
->sadb_comb_soft_addtime
* 80 / 100;
5744 comb
->sadb_comb_soft_usetime
= 28800; /* 8 hours */
5745 comb
->sadb_comb_hard_usetime
= comb
->sadb_comb_hard_usetime
* 80 / 100;
5750 * XXX reorder combinations by preference
5751 * XXX no idea if the user wants ESP authentication or not
5753 static struct mbuf
*
5756 struct sadb_comb
*comb
;
5757 const struct esp_algorithm
*algo
;
5758 struct mbuf
*result
= NULL
, *m
, *n
;
5762 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
5765 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
5766 algo
= esp_algorithm_lookup(i
);
5770 if (algo
->keymax
< ipsec_esp_keymin
)
5772 if (algo
->keymin
< ipsec_esp_keymin
)
5773 encmin
= ipsec_esp_keymin
;
5775 encmin
= algo
->keymin
;
5778 m
= key_getcomb_ah();
5782 panic("assumption failed in key_getcomb_esp");
5784 MGET(m
, M_DONTWAIT
, MT_DATA
);
5789 bzero(mtod(m
, caddr_t
), m
->m_len
);
5796 for (n
= m
; n
; n
= n
->m_next
)
5800 panic("assumption failed in key_getcomb_esp");
5803 for (off
= 0; off
< totlen
; off
+= l
) {
5804 n
= m_pulldown(m
, off
, l
, &o
);
5806 /* m is already freed */
5809 comb
= (struct sadb_comb
*)(mtod(n
, caddr_t
) + o
);
5810 bzero(comb
, sizeof(*comb
));
5811 key_getcomb_setlifetime(comb
);
5812 comb
->sadb_comb_encrypt
= i
;
5813 comb
->sadb_comb_encrypt_minbits
= encmin
;
5814 comb
->sadb_comb_encrypt_maxbits
= algo
->keymax
;
5833 * XXX reorder combinations by preference
5835 static struct mbuf
*
5838 struct sadb_comb
*comb
;
5839 const struct ah_algorithm
*algo
;
5843 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
5846 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
5848 /* we prefer HMAC algorithms, not old algorithms */
5849 if (i
!= SADB_AALG_SHA1HMAC
&& i
!= SADB_AALG_MD5HMAC
)
5852 algo
= ah_algorithm_lookup(i
);
5856 if (algo
->keymax
< ipsec_ah_keymin
)
5858 if (algo
->keymin
< ipsec_ah_keymin
)
5859 min
= ipsec_ah_keymin
;
5866 panic("assumption failed in key_getcomb_ah");
5868 MGET(m
, M_DONTWAIT
, MT_DATA
);
5875 M_PREPEND(m
, l
, M_DONTWAIT
);
5879 comb
= mtod(m
, struct sadb_comb
*);
5880 bzero(comb
, sizeof(*comb
));
5881 key_getcomb_setlifetime(comb
);
5882 comb
->sadb_comb_auth
= i
;
5883 comb
->sadb_comb_auth_minbits
= min
;
5884 comb
->sadb_comb_auth_maxbits
= algo
->keymax
;
5891 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
5892 * XXX reorder combinations by preference
5894 static struct mbuf
*
5895 key_getcomb_ipcomp()
5897 struct sadb_comb
*comb
;
5898 const struct ipcomp_algorithm
*algo
;
5901 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_comb
));
5904 for (i
= 1; i
<= SADB_X_CALG_MAX
; i
++) {
5905 algo
= ipcomp_algorithm_lookup(i
);
5912 panic("assumption failed in key_getcomb_ipcomp");
5914 MGET(m
, M_DONTWAIT
, MT_DATA
);
5921 M_PREPEND(m
, l
, M_DONTWAIT
);
5925 comb
= mtod(m
, struct sadb_comb
*);
5926 bzero(comb
, sizeof(*comb
));
5927 key_getcomb_setlifetime(comb
);
5928 comb
->sadb_comb_encrypt
= i
;
5929 /* what should we set into sadb_comb_*_{min,max}bits? */
5936 * XXX no way to pass mode (transport/tunnel) to userland
5937 * XXX replay checking?
5938 * XXX sysctl interface to ipsec_{ah,esp}_keymin
5940 static struct mbuf
*
5942 const struct secasindex
*saidx
;
5944 struct sadb_prop
*prop
;
5946 const int l
= PFKEY_ALIGN8(sizeof(struct sadb_prop
));
5949 switch (saidx
->proto
) {
5952 m
= key_getcomb_esp();
5956 m
= key_getcomb_ah();
5958 case IPPROTO_IPCOMP
:
5959 m
= key_getcomb_ipcomp();
5967 M_PREPEND(m
, l
, M_DONTWAIT
);
5972 for (n
= m
; n
; n
= n
->m_next
)
5975 prop
= mtod(m
, struct sadb_prop
*);
5976 bzero(prop
, sizeof(*prop
));
5977 prop
->sadb_prop_len
= PFKEY_UNIT64(totlen
);
5978 prop
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
5979 prop
->sadb_prop_replay
= 32; /* XXX */
5985 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5987 * <base, SA, address(SD), (address(P)), x_policy,
5988 * (identity(SD),) (sensitivity,) proposal>
5989 * to KMD, and expect to receive
5990 * <base> with SADB_ACQUIRE if error occurred,
5992 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5993 * from KMD by PF_KEY.
5995 * XXX x_policy is outside of RFC2367 (KAME extension).
5996 * XXX sensitivity is not supported.
5997 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
5998 * see comment for key_getcomb_ipcomp().
6002 * others: error number
6005 key_acquire(saidx
, sp
)
6006 struct secasindex
*saidx
;
6007 struct secpolicy
*sp
;
6009 struct mbuf
*result
= NULL
, *m
;
6010 #ifndef IPSEC_NONBLOCK_ACQUIRE
6011 struct secacq
*newacq
;
6017 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6021 panic("key_acquire: NULL pointer is passed.\n");
6022 if ((satype
= key_proto2satype(saidx
->proto
)) == 0)
6023 panic("key_acquire: invalid proto is passed.\n");
6025 #ifndef IPSEC_NONBLOCK_ACQUIRE
6027 * We never do anything about acquirng SA. There is anather
6028 * solution that kernel blocks to send SADB_ACQUIRE message until
6029 * getting something message from IKEd. In later case, to be
6030 * managed with ACQUIRING list.
6032 /* get a entry to check whether sending message or not. */
6033 if ((newacq
= key_getacq(saidx
)) != NULL
) {
6034 if (key_blockacq_count
< newacq
->count
) {
6035 /* reset counter and do send message. */
6038 /* increment counter and do nothing. */
6043 /* make new entry for blocking to send SADB_ACQUIRE. */
6044 if ((newacq
= key_newacq(saidx
)) == NULL
)
6047 /* add to acqtree */
6048 LIST_INSERT_HEAD(&acqtree
, newacq
, chain
);
6053 #ifndef IPSEC_NONBLOCK_ACQUIRE
6056 seq
= (acq_seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
));
6058 m
= key_setsadbmsg(SADB_ACQUIRE
, 0, satype
, seq
, 0, 0);
6065 /* set sadb_address for saidx's. */
6066 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
6067 (struct sockaddr
*)&saidx
->src
, FULLMASK
, IPSEC_ULPROTO_ANY
);
6074 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
6075 (struct sockaddr
*)&saidx
->dst
, FULLMASK
, IPSEC_ULPROTO_ANY
);
6082 /* XXX proxy address (optional) */
6084 /* set sadb_x_policy */
6086 m
= key_setsadbxpolicy(sp
->policy
, sp
->spidx
.dir
, sp
->id
);
6094 /* XXX identity (optional) */
6096 if (idexttype
&& fqdn
) {
6097 /* create identity extension (FQDN) */
6098 struct sadb_ident
*id
;
6101 fqdnlen
= strlen(fqdn
) + 1; /* +1 for terminating-NUL */
6102 id
= (struct sadb_ident
*)p
;
6103 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
6104 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(fqdnlen
));
6105 id
->sadb_ident_exttype
= idexttype
;
6106 id
->sadb_ident_type
= SADB_IDENTTYPE_FQDN
;
6107 bcopy(fqdn
, id
+ 1, fqdnlen
);
6108 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(fqdnlen
);
6112 /* create identity extension (USERFQDN) */
6113 struct sadb_ident
*id
;
6117 /* +1 for terminating-NUL */
6118 userfqdnlen
= strlen(userfqdn
) + 1;
6121 id
= (struct sadb_ident
*)p
;
6122 bzero(id
, sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
6123 id
->sadb_ident_len
= PFKEY_UNIT64(sizeof(*id
) + PFKEY_ALIGN8(userfqdnlen
));
6124 id
->sadb_ident_exttype
= idexttype
;
6125 id
->sadb_ident_type
= SADB_IDENTTYPE_USERFQDN
;
6126 /* XXX is it correct? */
6127 if (curproc
&& curproc
->p_cred
)
6128 id
->sadb_ident_id
= curproc
->p_cred
->p_ruid
;
6129 if (userfqdn
&& userfqdnlen
)
6130 bcopy(userfqdn
, id
+ 1, userfqdnlen
);
6131 p
+= sizeof(struct sadb_ident
) + PFKEY_ALIGN8(userfqdnlen
);
6135 /* XXX sensitivity (optional) */
6137 /* create proposal/combination extension */
6138 m
= key_getprop(saidx
);
6141 * spec conformant: always attach proposal/combination extension,
6142 * the problem is that we have no way to attach it for ipcomp,
6143 * due to the way sadb_comb is declared in RFC2367.
6152 * outside of spec; make proposal/combination extension optional.
6158 if ((result
->m_flags
& M_PKTHDR
) == 0) {
6163 if (result
->m_len
< sizeof(struct sadb_msg
)) {
6164 result
= m_pullup(result
, sizeof(struct sadb_msg
));
6165 if (result
== NULL
) {
6171 result
->m_pkthdr
.len
= 0;
6172 for (m
= result
; m
; m
= m
->m_next
)
6173 result
->m_pkthdr
.len
+= m
->m_len
;
6175 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
6176 PFKEY_UNIT64(result
->m_pkthdr
.len
);
6178 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
6186 #ifndef IPSEC_NONBLOCK_ACQUIRE
6187 static struct secacq
*
6189 struct secasindex
*saidx
;
6191 struct secacq
*newacq
;
6195 KMALLOC(newacq
, struct secacq
*, sizeof(struct secacq
));
6196 if (newacq
== NULL
) {
6197 ipseclog((LOG_DEBUG
, "key_newacq: No more memory.\n"));
6200 bzero(newacq
, sizeof(*newacq
));
6203 bcopy(saidx
, &newacq
->saidx
, sizeof(newacq
->saidx
));
6204 newacq
->seq
= (acq_seq
== ~0 ? 1 : ++acq_seq
);
6206 newacq
->created
= tv
.tv_sec
;
6212 static struct secacq
*
6214 struct secasindex
*saidx
;
6218 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6220 LIST_FOREACH(acq
, &acqtree
, chain
) {
6221 if (key_cmpsaidx(saidx
, &acq
->saidx
, CMP_EXACTLY
))
6228 static struct secacq
*
6229 key_getacqbyseq(seq
)
6234 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6236 LIST_FOREACH(acq
, &acqtree
, chain
) {
6237 if (acq
->seq
== seq
)
6245 static struct secspacq
*
6247 struct secpolicyindex
*spidx
;
6249 struct secspacq
*acq
;
6253 KMALLOC(acq
, struct secspacq
*, sizeof(struct secspacq
));
6255 ipseclog((LOG_DEBUG
, "key_newspacq: No more memory.\n"));
6258 bzero(acq
, sizeof(*acq
));
6261 bcopy(spidx
, &acq
->spidx
, sizeof(acq
->spidx
));
6263 acq
->created
= tv
.tv_sec
;
6269 static struct secspacq
*
6271 struct secpolicyindex
*spidx
;
6273 struct secspacq
*acq
;
6275 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6277 LIST_FOREACH(acq
, &spacqtree
, chain
) {
6278 if (key_cmpspidx_exactly(spidx
, &acq
->spidx
))
6286 * SADB_ACQUIRE processing,
6287 * in first situation, is receiving
6289 * from the ikmpd, and clear sequence of its secasvar entry.
6291 * In second situation, is receiving
6292 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6293 * from a user land process, and return
6294 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6297 * m will always be freed.
6300 key_acquire2(so
, m
, mhp
)
6303 const struct sadb_msghdr
*mhp
;
6305 const struct sadb_address
*src0
, *dst0
;
6306 struct secasindex saidx
;
6307 struct secashead
*sah
;
6311 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6314 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6315 panic("key_acquire2: NULL pointer is passed.\n");
6318 * Error message from KMd.
6319 * We assume that if error was occurred in IKEd, the length of PFKEY
6320 * message is equal to the size of sadb_msg structure.
6321 * We do not raise error even if error occurred in this function.
6323 if (mhp
->msg
->sadb_msg_len
== PFKEY_UNIT64(sizeof(struct sadb_msg
))) {
6324 #ifndef IPSEC_NONBLOCK_ACQUIRE
6328 /* check sequence number */
6329 if (mhp
->msg
->sadb_msg_seq
== 0) {
6330 ipseclog((LOG_DEBUG
, "key_acquire2: must specify sequence number.\n"));
6335 if ((acq
= key_getacqbyseq(mhp
->msg
->sadb_msg_seq
)) == NULL
) {
6337 * the specified larval SA is already gone, or we got
6338 * a bogus sequence number. we can silently ignore it.
6344 /* reset acq counter in order to deletion by timehander. */
6346 acq
->created
= tv
.tv_sec
;
6354 * This message is from user land.
6357 /* map satype to proto */
6358 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6359 ipseclog((LOG_DEBUG
, "key_acquire2: invalid satype is passed.\n"));
6360 return key_senderror(so
, m
, EINVAL
);
6363 if (mhp
->ext
[SADB_EXT_ADDRESS_SRC
] == NULL
||
6364 mhp
->ext
[SADB_EXT_ADDRESS_DST
] == NULL
||
6365 mhp
->ext
[SADB_EXT_PROPOSAL
] == NULL
) {
6367 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
6368 return key_senderror(so
, m
, EINVAL
);
6370 if (mhp
->extlen
[SADB_EXT_ADDRESS_SRC
] < sizeof(struct sadb_address
) ||
6371 mhp
->extlen
[SADB_EXT_ADDRESS_DST
] < sizeof(struct sadb_address
) ||
6372 mhp
->extlen
[SADB_EXT_PROPOSAL
] < sizeof(struct sadb_prop
)) {
6374 ipseclog((LOG_DEBUG
, "key_acquire2: invalid message is passed.\n"));
6375 return key_senderror(so
, m
, EINVAL
);
6378 src0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_SRC
];
6379 dst0
= (struct sadb_address
*)mhp
->ext
[SADB_EXT_ADDRESS_DST
];
6381 /* XXX boundary check against sa_len */
6382 KEY_SETSECASIDX(proto
, IPSEC_MODE_ANY
, 0, src0
+ 1, dst0
+ 1, &saidx
);
6384 /* get a SA index */
6385 LIST_FOREACH(sah
, &sahtree
, chain
) {
6386 if (sah
->state
== SADB_SASTATE_DEAD
)
6388 if (key_cmpsaidx(&sah
->saidx
, &saidx
, CMP_MODE_REQID
))
6392 ipseclog((LOG_DEBUG
, "key_acquire2: a SA exists already.\n"));
6393 return key_senderror(so
, m
, EEXIST
);
6396 error
= key_acquire(&saidx
, NULL
);
6398 ipseclog((LOG_DEBUG
, "key_acquire2: error %d returned "
6399 "from key_acquire.\n", mhp
->msg
->sadb_msg_errno
));
6400 return key_senderror(so
, m
, error
);
6403 return key_sendup_mbuf(so
, m
, KEY_SENDUP_REGISTERED
);
6407 * SADB_REGISTER processing.
6408 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6411 * from the ikmpd, and register a socket to send PF_KEY messages,
6415 * If socket is detached, must free from regnode.
6417 * m will always be freed.
6420 key_register(so
, m
, mhp
)
6423 const struct sadb_msghdr
*mhp
;
6425 struct secreg
*reg
, *newreg
= 0;
6427 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6430 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6431 panic("key_register: NULL pointer is passed.\n");
6433 /* check for invalid register message */
6434 if (mhp
->msg
->sadb_msg_satype
>= sizeof(regtree
)/sizeof(regtree
[0]))
6435 return key_senderror(so
, m
, EINVAL
);
6437 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6438 if (mhp
->msg
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
)
6441 /* check whether existing or not */
6442 LIST_FOREACH(reg
, ®tree
[mhp
->msg
->sadb_msg_satype
], chain
) {
6443 if (reg
->so
== so
) {
6444 ipseclog((LOG_DEBUG
, "key_register: socket exists already.\n"));
6445 return key_senderror(so
, m
, EEXIST
);
6449 /* create regnode */
6450 KMALLOC(newreg
, struct secreg
*, sizeof(*newreg
));
6451 if (newreg
== NULL
) {
6452 ipseclog((LOG_DEBUG
, "key_register: No more memory.\n"));
6453 return key_senderror(so
, m
, ENOBUFS
);
6455 bzero((caddr_t
)newreg
, sizeof(*newreg
));
6459 ((struct keycb
*)sotorawcb(so
))->kp_registered
++;
6460 socket_unlock(so
, 1);
6462 /* add regnode to regtree. */
6463 LIST_INSERT_HEAD(®tree
[mhp
->msg
->sadb_msg_satype
], newreg
, chain
);
6468 struct sadb_msg
*newmsg
;
6469 struct sadb_supported
*sup
;
6470 u_int len
, alen
, elen
;
6473 struct sadb_alg
*alg
;
6475 /* create new sadb_msg to reply. */
6477 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
6478 if (ah_algorithm_lookup(i
))
6479 alen
+= sizeof(struct sadb_alg
);
6482 alen
+= sizeof(struct sadb_supported
);
6485 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
6486 if (esp_algorithm_lookup(i
))
6487 elen
+= sizeof(struct sadb_alg
);
6490 elen
+= sizeof(struct sadb_supported
);
6493 len
= sizeof(struct sadb_msg
) + alen
+ elen
;
6496 return key_senderror(so
, m
, ENOBUFS
);
6498 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
6500 MCLGET(n
, M_DONTWAIT
);
6501 if ((n
->m_flags
& M_EXT
) == 0) {
6507 return key_senderror(so
, m
, ENOBUFS
);
6509 n
->m_pkthdr
.len
= n
->m_len
= len
;
6513 m_copydata(m
, 0, sizeof(struct sadb_msg
), mtod(n
, caddr_t
) + off
);
6514 newmsg
= mtod(n
, struct sadb_msg
*);
6515 newmsg
->sadb_msg_errno
= 0;
6516 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
6517 off
+= PFKEY_ALIGN8(sizeof(struct sadb_msg
));
6519 /* for authentication algorithm */
6521 sup
= (struct sadb_supported
*)(mtod(n
, caddr_t
) + off
);
6522 sup
->sadb_supported_len
= PFKEY_UNIT64(alen
);
6523 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
6524 off
+= PFKEY_ALIGN8(sizeof(*sup
));
6526 for (i
= 1; i
<= SADB_AALG_MAX
; i
++) {
6527 const struct ah_algorithm
*aalgo
;
6529 aalgo
= ah_algorithm_lookup(i
);
6532 alg
= (struct sadb_alg
*)(mtod(n
, caddr_t
) + off
);
6533 alg
->sadb_alg_id
= i
;
6534 alg
->sadb_alg_ivlen
= 0;
6535 alg
->sadb_alg_minbits
= aalgo
->keymin
;
6536 alg
->sadb_alg_maxbits
= aalgo
->keymax
;
6537 off
+= PFKEY_ALIGN8(sizeof(*alg
));
6542 /* for encryption algorithm */
6544 sup
= (struct sadb_supported
*)(mtod(n
, caddr_t
) + off
);
6545 sup
->sadb_supported_len
= PFKEY_UNIT64(elen
);
6546 sup
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
6547 off
+= PFKEY_ALIGN8(sizeof(*sup
));
6549 for (i
= 1; i
<= SADB_EALG_MAX
; i
++) {
6550 const struct esp_algorithm
*ealgo
;
6552 ealgo
= esp_algorithm_lookup(i
);
6555 alg
= (struct sadb_alg
*)(mtod(n
, caddr_t
) + off
);
6556 alg
->sadb_alg_id
= i
;
6557 if (ealgo
&& ealgo
->ivlen
) {
6559 * give NULL to get the value preferred by
6560 * algorithm XXX SADB_X_EXT_DERIV ?
6562 alg
->sadb_alg_ivlen
=
6563 (*ealgo
->ivlen
)(ealgo
, NULL
);
6565 alg
->sadb_alg_ivlen
= 0;
6566 alg
->sadb_alg_minbits
= ealgo
->keymin
;
6567 alg
->sadb_alg_maxbits
= ealgo
->keymax
;
6568 off
+= PFKEY_ALIGN8(sizeof(struct sadb_alg
));
6575 panic("length assumption failed in key_register");
6579 return key_sendup_mbuf(so
, n
, KEY_SENDUP_REGISTERED
);
6584 * free secreg entry registered.
6585 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6594 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6598 panic("key_freereg: NULL pointer is passed.\n");
6601 * check whether existing or not.
6602 * check all type of SA, because there is a potential that
6603 * one socket is registered to multiple type of SA.
6605 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
6606 LIST_FOREACH(reg
, ®tree
[i
], chain
) {
6608 && __LIST_CHAINED(reg
)) {
6609 LIST_REMOVE(reg
, chain
);
6620 * SADB_EXPIRE processing
6622 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6624 * NOTE: We send only soft lifetime extension.
6627 * others : error number
6631 struct secasvar
*sav
;
6635 struct mbuf
*result
= NULL
, *m
;
6638 struct sadb_lifetime
*lt
;
6640 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6644 panic("key_expire: NULL pointer is passed.\n");
6645 if (sav
->sah
== NULL
)
6646 panic("key_expire: Why was SA index in SA NULL.\n");
6647 if ((satype
= key_proto2satype(sav
->sah
->saidx
.proto
)) == 0)
6648 panic("key_expire: invalid proto is passed.\n");
6650 /* set msg header */
6651 m
= key_setsadbmsg(SADB_EXPIRE
, 0, satype
, sav
->seq
, 0, sav
->refcnt
);
6658 /* create SA extension */
6659 m
= key_setsadbsa(sav
);
6666 /* create SA extension */
6667 m
= key_setsadbxsa2(sav
->sah
->saidx
.mode
,
6668 sav
->replay
? sav
->replay
->count
: 0,
6669 sav
->sah
->saidx
.reqid
);
6676 /* create lifetime extension (current and soft) */
6677 len
= PFKEY_ALIGN8(sizeof(*lt
)) * 2;
6678 m
= key_alloc_mbuf(len
);
6679 if (!m
|| m
->m_next
) { /*XXX*/
6685 bzero(mtod(m
, caddr_t
), len
);
6686 lt
= mtod(m
, struct sadb_lifetime
*);
6687 lt
->sadb_lifetime_len
= PFKEY_UNIT64(sizeof(struct sadb_lifetime
));
6688 lt
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
6689 lt
->sadb_lifetime_allocations
= sav
->lft_c
->sadb_lifetime_allocations
;
6690 lt
->sadb_lifetime_bytes
= sav
->lft_c
->sadb_lifetime_bytes
;
6691 lt
->sadb_lifetime_addtime
= sav
->lft_c
->sadb_lifetime_addtime
;
6692 lt
->sadb_lifetime_usetime
= sav
->lft_c
->sadb_lifetime_usetime
;
6693 lt
= (struct sadb_lifetime
*)(mtod(m
, caddr_t
) + len
/ 2);
6694 bcopy(sav
->lft_s
, lt
, sizeof(*lt
));
6697 /* set sadb_address for source */
6698 m
= key_setsadbaddr(SADB_EXT_ADDRESS_SRC
,
6699 (struct sockaddr
*)&sav
->sah
->saidx
.src
,
6700 FULLMASK
, IPSEC_ULPROTO_ANY
);
6707 /* set sadb_address for destination */
6708 m
= key_setsadbaddr(SADB_EXT_ADDRESS_DST
,
6709 (struct sockaddr
*)&sav
->sah
->saidx
.dst
,
6710 FULLMASK
, IPSEC_ULPROTO_ANY
);
6717 if ((result
->m_flags
& M_PKTHDR
) == 0) {
6722 if (result
->m_len
< sizeof(struct sadb_msg
)) {
6723 result
= m_pullup(result
, sizeof(struct sadb_msg
));
6724 if (result
== NULL
) {
6730 result
->m_pkthdr
.len
= 0;
6731 for (m
= result
; m
; m
= m
->m_next
)
6732 result
->m_pkthdr
.len
+= m
->m_len
;
6734 mtod(result
, struct sadb_msg
*)->sadb_msg_len
=
6735 PFKEY_UNIT64(result
->m_pkthdr
.len
);
6738 return key_sendup_mbuf(NULL
, result
, KEY_SENDUP_REGISTERED
);
6748 * SADB_FLUSH processing
6751 * from the ikmpd, and free all entries in secastree.
6755 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6757 * m will always be freed.
6760 key_flush(so
, m
, mhp
)
6763 const struct sadb_msghdr
*mhp
;
6765 struct sadb_msg
*newmsg
;
6766 struct secashead
*sah
, *nextsah
;
6767 struct secasvar
*sav
, *nextsav
;
6772 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6775 if (so
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6776 panic("key_flush: NULL pointer is passed.\n");
6778 /* map satype to proto */
6779 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6780 ipseclog((LOG_DEBUG
, "key_flush: invalid satype is passed.\n"));
6781 return key_senderror(so
, m
, EINVAL
);
6784 /* no SATYPE specified, i.e. flushing all SA. */
6785 for (sah
= LIST_FIRST(&sahtree
);
6788 nextsah
= LIST_NEXT(sah
, chain
);
6790 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
6791 && proto
!= sah
->saidx
.proto
)
6795 stateidx
< _ARRAYLEN(saorder_state_alive
);
6797 state
= saorder_state_any
[stateidx
];
6798 for (sav
= LIST_FIRST(&sah
->savtree
[state
]);
6802 nextsav
= LIST_NEXT(sav
, chain
);
6804 key_sa_chgstate(sav
, SADB_SASTATE_DEAD
);
6809 sah
->state
= SADB_SASTATE_DEAD
;
6812 if (m
->m_len
< sizeof(struct sadb_msg
) ||
6813 sizeof(struct sadb_msg
) > m
->m_len
+ M_TRAILINGSPACE(m
)) {
6814 ipseclog((LOG_DEBUG
, "key_flush: No more memory.\n"));
6815 return key_senderror(so
, m
, ENOBUFS
);
6821 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct sadb_msg
);
6822 newmsg
= mtod(m
, struct sadb_msg
*);
6823 newmsg
->sadb_msg_errno
= 0;
6824 newmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
6826 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
6830 * SADB_DUMP processing
6831 * dump all entries including status of DEAD in SAD.
6834 * from the ikmpd, and dump all secasvar leaves
6839 * m will always be freed.
6842 key_dump(so
, m
, mhp
)
6845 const struct sadb_msghdr
*mhp
;
6847 struct secashead
*sah
;
6848 struct secasvar
*sav
;
6854 struct sadb_msg
*newmsg
;
6857 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6860 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6861 panic("key_dump: NULL pointer is passed.\n");
6863 /* map satype to proto */
6864 if ((proto
= key_satype2proto(mhp
->msg
->sadb_msg_satype
)) == 0) {
6865 ipseclog((LOG_DEBUG
, "key_dump: invalid satype is passed.\n"));
6866 return key_senderror(so
, m
, EINVAL
);
6869 /* count sav entries to be sent to the userland. */
6871 LIST_FOREACH(sah
, &sahtree
, chain
) {
6872 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
6873 && proto
!= sah
->saidx
.proto
)
6877 stateidx
< _ARRAYLEN(saorder_state_any
);
6879 state
= saorder_state_any
[stateidx
];
6880 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
6887 return key_senderror(so
, m
, ENOENT
);
6889 /* send this to the userland, one at a time. */
6891 LIST_FOREACH(sah
, &sahtree
, chain
) {
6892 if (mhp
->msg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
6893 && proto
!= sah
->saidx
.proto
)
6896 /* map proto to satype */
6897 if ((satype
= key_proto2satype(sah
->saidx
.proto
)) == 0) {
6898 ipseclog((LOG_DEBUG
, "key_dump: there was invalid proto in SAD.\n"));
6899 return key_senderror(so
, m
, EINVAL
);
6903 stateidx
< _ARRAYLEN(saorder_state_any
);
6905 state
= saorder_state_any
[stateidx
];
6906 LIST_FOREACH(sav
, &sah
->savtree
[state
], chain
) {
6907 n
= key_setdumpsa(sav
, SADB_DUMP
, satype
,
6908 --cnt
, mhp
->msg
->sadb_msg_pid
);
6910 return key_senderror(so
, m
, ENOBUFS
);
6912 key_sendup_mbuf(so
, n
, KEY_SENDUP_ONE
);
6922 * SADB_X_PROMISC processing
6924 * m will always be freed.
6927 key_promisc(so
, m
, mhp
)
6930 const struct sadb_msghdr
*mhp
;
6934 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
6937 if (so
== NULL
|| m
== NULL
|| mhp
== NULL
|| mhp
->msg
== NULL
)
6938 panic("key_promisc: NULL pointer is passed.\n");
6940 olen
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
6942 if (olen
< sizeof(struct sadb_msg
)) {
6944 return key_senderror(so
, m
, EINVAL
);
6949 } else if (olen
== sizeof(struct sadb_msg
)) {
6950 /* enable/disable promisc mode */
6954 if ((kp
= (struct keycb
*)sotorawcb(so
)) == NULL
)
6955 return key_senderror(so
, m
, EINVAL
);
6956 mhp
->msg
->sadb_msg_errno
= 0;
6957 switch (mhp
->msg
->sadb_msg_satype
) {
6960 kp
->kp_promisc
= mhp
->msg
->sadb_msg_satype
;
6963 socket_unlock(so
, 1);
6964 return key_senderror(so
, m
, EINVAL
);
6966 socket_unlock(so
, 1);
6968 /* send the original message back to everyone */
6969 mhp
->msg
->sadb_msg_errno
= 0;
6970 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
6972 /* send packet as is */
6974 m_adj(m
, PFKEY_ALIGN8(sizeof(struct sadb_msg
)));
6976 /* TODO: if sadb_msg_seq is specified, send to specific pid */
6977 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ALL
);
6981 static int (*key_typesw
[])(struct socket
*, struct mbuf
*,
6982 const struct sadb_msghdr
*) = {
6983 NULL
, /* SADB_RESERVED */
6984 key_getspi
, /* SADB_GETSPI */
6985 key_update
, /* SADB_UPDATE */
6986 key_add
, /* SADB_ADD */
6987 key_delete
, /* SADB_DELETE */
6988 key_get
, /* SADB_GET */
6989 key_acquire2
, /* SADB_ACQUIRE */
6990 key_register
, /* SADB_REGISTER */
6991 NULL
, /* SADB_EXPIRE */
6992 key_flush
, /* SADB_FLUSH */
6993 key_dump
, /* SADB_DUMP */
6994 key_promisc
, /* SADB_X_PROMISC */
6995 NULL
, /* SADB_X_PCHANGE */
6996 key_spdadd
, /* SADB_X_SPDUPDATE */
6997 key_spdadd
, /* SADB_X_SPDADD */
6998 key_spddelete
, /* SADB_X_SPDDELETE */
6999 key_spdget
, /* SADB_X_SPDGET */
7000 NULL
, /* SADB_X_SPDACQUIRE */
7001 key_spddump
, /* SADB_X_SPDDUMP */
7002 key_spdflush
, /* SADB_X_SPDFLUSH */
7003 key_spdadd
, /* SADB_X_SPDSETIDX */
7004 NULL
, /* SADB_X_SPDEXPIRE */
7005 key_spddelete2
, /* SADB_X_SPDDELETE2 */
7009 * parse sadb_msg buffer to process PFKEYv2,
7010 * and create a data to response if needed.
7011 * I think to be dealed with mbuf directly.
7013 * msgp : pointer to pointer to a received buffer pulluped.
7014 * This is rewrited to response.
7015 * so : pointer to socket.
7017 * length for buffer to send to user process.
7024 struct sadb_msg
*msg
;
7025 struct sadb_msghdr mh
;
7030 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7033 if (m
== NULL
|| so
== NULL
)
7034 panic("key_parse: NULL pointer is passed.\n");
7036 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
7037 KEYDEBUG(KEYDEBUG_KEY_DUMP
,
7038 ipseclog((LOG_DEBUG
, "key_parse: passed sadb_msg\n"));
7042 if (m
->m_len
< sizeof(struct sadb_msg
)) {
7043 m
= m_pullup(m
, sizeof(struct sadb_msg
));
7047 msg
= mtod(m
, struct sadb_msg
*);
7048 orglen
= PFKEY_UNUNIT64(msg
->sadb_msg_len
);
7049 target
= KEY_SENDUP_ONE
;
7051 if ((m
->m_flags
& M_PKTHDR
) == 0 ||
7052 m
->m_pkthdr
.len
!= m
->m_pkthdr
.len
) {
7053 ipseclog((LOG_DEBUG
, "key_parse: invalid message length.\n"));
7054 pfkeystat
.out_invlen
++;
7059 if (msg
->sadb_msg_version
!= PF_KEY_V2
) {
7060 ipseclog((LOG_DEBUG
,
7061 "key_parse: PF_KEY version %u is mismatched.\n",
7062 msg
->sadb_msg_version
));
7063 pfkeystat
.out_invver
++;
7068 if (msg
->sadb_msg_type
> SADB_MAX
) {
7069 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
7070 msg
->sadb_msg_type
));
7071 pfkeystat
.out_invmsgtype
++;
7076 /* for old-fashioned code - should be nuked */
7077 if (m
->m_pkthdr
.len
> MCLBYTES
) {
7084 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
7085 if (n
&& m
->m_pkthdr
.len
> MHLEN
) {
7086 MCLGET(n
, M_DONTWAIT
);
7087 if ((n
->m_flags
& M_EXT
) == 0) {
7096 m_copydata(m
, 0, m
->m_pkthdr
.len
, mtod(n
, caddr_t
));
7097 n
->m_pkthdr
.len
= n
->m_len
= m
->m_pkthdr
.len
;
7103 /* align the mbuf chain so that extensions are in contiguous region. */
7104 error
= key_align(m
, &mh
);
7108 if (m
->m_next
) { /*XXX*/
7116 switch (msg
->sadb_msg_satype
) {
7117 case SADB_SATYPE_UNSPEC
:
7118 switch (msg
->sadb_msg_type
) {
7126 ipseclog((LOG_DEBUG
, "key_parse: must specify satype "
7127 "when msg type=%u.\n", msg
->sadb_msg_type
));
7128 pfkeystat
.out_invsatype
++;
7133 case SADB_SATYPE_AH
:
7134 case SADB_SATYPE_ESP
:
7135 case SADB_X_SATYPE_IPCOMP
:
7136 switch (msg
->sadb_msg_type
) {
7138 case SADB_X_SPDDELETE
:
7140 case SADB_X_SPDDUMP
:
7141 case SADB_X_SPDFLUSH
:
7142 case SADB_X_SPDSETIDX
:
7143 case SADB_X_SPDUPDATE
:
7144 case SADB_X_SPDDELETE2
:
7145 ipseclog((LOG_DEBUG
, "key_parse: illegal satype=%u\n",
7146 msg
->sadb_msg_type
));
7147 pfkeystat
.out_invsatype
++;
7152 case SADB_SATYPE_RSVP
:
7153 case SADB_SATYPE_OSPFV2
:
7154 case SADB_SATYPE_RIPV2
:
7155 case SADB_SATYPE_MIP
:
7156 ipseclog((LOG_DEBUG
, "key_parse: type %u isn't supported.\n",
7157 msg
->sadb_msg_satype
));
7158 pfkeystat
.out_invsatype
++;
7161 case 1: /* XXX: What does it do? */
7162 if (msg
->sadb_msg_type
== SADB_X_PROMISC
)
7166 ipseclog((LOG_DEBUG
, "key_parse: invalid type %u is passed.\n",
7167 msg
->sadb_msg_satype
));
7168 pfkeystat
.out_invsatype
++;
7173 /* check field of upper layer protocol and address family */
7174 if (mh
.ext
[SADB_EXT_ADDRESS_SRC
] != NULL
7175 && mh
.ext
[SADB_EXT_ADDRESS_DST
] != NULL
) {
7176 struct sadb_address
*src0
, *dst0
;
7179 src0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_SRC
]);
7180 dst0
= (struct sadb_address
*)(mh
.ext
[SADB_EXT_ADDRESS_DST
]);
7182 /* check upper layer protocol */
7183 if (src0
->sadb_address_proto
!= dst0
->sadb_address_proto
) {
7184 ipseclog((LOG_DEBUG
, "key_parse: upper layer protocol mismatched.\n"));
7185 pfkeystat
.out_invaddr
++;
7191 if (PFKEY_ADDR_SADDR(src0
)->sa_family
!=
7192 PFKEY_ADDR_SADDR(dst0
)->sa_family
) {
7193 ipseclog((LOG_DEBUG
, "key_parse: address family mismatched.\n"));
7194 pfkeystat
.out_invaddr
++;
7198 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
7199 PFKEY_ADDR_SADDR(dst0
)->sa_len
) {
7200 ipseclog((LOG_DEBUG
,
7201 "key_parse: address struct size mismatched.\n"));
7202 pfkeystat
.out_invaddr
++;
7207 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
7209 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
7210 sizeof(struct sockaddr_in
)) {
7211 pfkeystat
.out_invaddr
++;
7217 if (PFKEY_ADDR_SADDR(src0
)->sa_len
!=
7218 sizeof(struct sockaddr_in6
)) {
7219 pfkeystat
.out_invaddr
++;
7225 ipseclog((LOG_DEBUG
,
7226 "key_parse: unsupported address family.\n"));
7227 pfkeystat
.out_invaddr
++;
7228 error
= EAFNOSUPPORT
;
7232 switch (PFKEY_ADDR_SADDR(src0
)->sa_family
) {
7234 plen
= sizeof(struct in_addr
) << 3;
7237 plen
= sizeof(struct in6_addr
) << 3;
7240 plen
= 0; /*fool gcc*/
7244 /* check max prefix length */
7245 if (src0
->sadb_address_prefixlen
> plen
||
7246 dst0
->sadb_address_prefixlen
> plen
) {
7247 ipseclog((LOG_DEBUG
,
7248 "key_parse: illegal prefixlen.\n"));
7249 pfkeystat
.out_invaddr
++;
7255 * prefixlen == 0 is valid because there can be a case when
7256 * all addresses are matched.
7260 if (msg
->sadb_msg_type
>= sizeof(key_typesw
)/sizeof(key_typesw
[0]) ||
7261 key_typesw
[msg
->sadb_msg_type
] == NULL
) {
7262 pfkeystat
.out_invmsgtype
++;
7267 return (*key_typesw
[msg
->sadb_msg_type
])(so
, m
, &mh
);
7270 msg
->sadb_msg_errno
= error
;
7271 return key_sendup_mbuf(so
, m
, target
);
7275 key_senderror(so
, m
, code
)
7280 struct sadb_msg
*msg
;
7282 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7284 if (m
->m_len
< sizeof(struct sadb_msg
))
7285 panic("invalid mbuf passed to key_senderror");
7287 msg
= mtod(m
, struct sadb_msg
*);
7288 msg
->sadb_msg_errno
= code
;
7289 return key_sendup_mbuf(so
, m
, KEY_SENDUP_ONE
);
7293 * set the pointer to each header into message buffer.
7294 * m will be freed on error.
7295 * XXX larger-than-MCLBYTES extension?
7300 struct sadb_msghdr
*mhp
;
7303 struct sadb_ext
*ext
;
7309 if (m
== NULL
|| mhp
== NULL
)
7310 panic("key_align: NULL pointer is passed.\n");
7311 if (m
->m_len
< sizeof(struct sadb_msg
))
7312 panic("invalid mbuf passed to key_align");
7315 bzero(mhp
, sizeof(*mhp
));
7317 mhp
->msg
= mtod(m
, struct sadb_msg
*);
7318 mhp
->ext
[0] = (struct sadb_ext
*)mhp
->msg
; /*XXX backward compat */
7320 end
= PFKEY_UNUNIT64(mhp
->msg
->sadb_msg_len
);
7321 extlen
= end
; /*just in case extlen is not updated*/
7322 for (off
= sizeof(struct sadb_msg
); off
< end
; off
+= extlen
) {
7323 n
= m_pulldown(m
, off
, sizeof(struct sadb_ext
), &toff
);
7325 /* m is already freed */
7328 ext
= (struct sadb_ext
*)(mtod(n
, caddr_t
) + toff
);
7331 switch (ext
->sadb_ext_type
) {
7333 case SADB_EXT_ADDRESS_SRC
:
7334 case SADB_EXT_ADDRESS_DST
:
7335 case SADB_EXT_ADDRESS_PROXY
:
7336 case SADB_EXT_LIFETIME_CURRENT
:
7337 case SADB_EXT_LIFETIME_HARD
:
7338 case SADB_EXT_LIFETIME_SOFT
:
7339 case SADB_EXT_KEY_AUTH
:
7340 case SADB_EXT_KEY_ENCRYPT
:
7341 case SADB_EXT_IDENTITY_SRC
:
7342 case SADB_EXT_IDENTITY_DST
:
7343 case SADB_EXT_SENSITIVITY
:
7344 case SADB_EXT_PROPOSAL
:
7345 case SADB_EXT_SUPPORTED_AUTH
:
7346 case SADB_EXT_SUPPORTED_ENCRYPT
:
7347 case SADB_EXT_SPIRANGE
:
7348 case SADB_X_EXT_POLICY
:
7349 case SADB_X_EXT_SA2
:
7350 /* duplicate check */
7352 * XXX Are there duplication payloads of either
7353 * KEY_AUTH or KEY_ENCRYPT ?
7355 if (mhp
->ext
[ext
->sadb_ext_type
] != NULL
) {
7356 ipseclog((LOG_DEBUG
,
7357 "key_align: duplicate ext_type %u "
7358 "is passed.\n", ext
->sadb_ext_type
));
7360 pfkeystat
.out_dupext
++;
7365 ipseclog((LOG_DEBUG
,
7366 "key_align: invalid ext_type %u is passed.\n",
7367 ext
->sadb_ext_type
));
7369 pfkeystat
.out_invexttype
++;
7373 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
7375 if (key_validate_ext(ext
, extlen
)) {
7377 pfkeystat
.out_invlen
++;
7381 n
= m_pulldown(m
, off
, extlen
, &toff
);
7383 /* m is already freed */
7386 ext
= (struct sadb_ext
*)(mtod(n
, caddr_t
) + toff
);
7388 mhp
->ext
[ext
->sadb_ext_type
] = ext
;
7389 mhp
->extoff
[ext
->sadb_ext_type
] = off
;
7390 mhp
->extlen
[ext
->sadb_ext_type
] = extlen
;
7395 pfkeystat
.out_invlen
++;
7403 key_validate_ext(ext
, len
)
7404 const struct sadb_ext
*ext
;
7407 struct sockaddr
*sa
;
7408 enum { NONE
, ADDR
} checktype
= NONE
;
7410 const int sal
= offsetof(struct sockaddr
, sa_len
) + sizeof(sa
->sa_len
);
7412 if (len
!= PFKEY_UNUNIT64(ext
->sadb_ext_len
))
7415 /* if it does not match minimum/maximum length, bail */
7416 if (ext
->sadb_ext_type
>= sizeof(minsize
) / sizeof(minsize
[0]) ||
7417 ext
->sadb_ext_type
>= sizeof(maxsize
) / sizeof(maxsize
[0]))
7419 if (!minsize
[ext
->sadb_ext_type
] || len
< minsize
[ext
->sadb_ext_type
])
7421 if (maxsize
[ext
->sadb_ext_type
] && len
> maxsize
[ext
->sadb_ext_type
])
7424 /* more checks based on sadb_ext_type XXX need more */
7425 switch (ext
->sadb_ext_type
) {
7426 case SADB_EXT_ADDRESS_SRC
:
7427 case SADB_EXT_ADDRESS_DST
:
7428 case SADB_EXT_ADDRESS_PROXY
:
7429 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_address
));
7432 case SADB_EXT_IDENTITY_SRC
:
7433 case SADB_EXT_IDENTITY_DST
:
7434 if (((struct sadb_ident
*)ext
)->sadb_ident_type
==
7435 SADB_X_IDENTTYPE_ADDR
) {
7436 baselen
= PFKEY_ALIGN8(sizeof(struct sadb_ident
));
7446 switch (checktype
) {
7450 sa
= (struct sockaddr
*)((caddr_t
)ext
+ baselen
);
7451 if (len
< baselen
+ sal
)
7453 if (baselen
+ PFKEY_ALIGN8(sa
->sa_len
) != len
)
7466 bzero((caddr_t
)&key_cb
, sizeof(key_cb
));
7468 for (i
= 0; i
< IPSEC_DIR_MAX
; i
++) {
7469 LIST_INIT(&sptree
[i
]);
7472 LIST_INIT(&sahtree
);
7474 for (i
= 0; i
<= SADB_SATYPE_MAX
; i
++) {
7475 LIST_INIT(®tree
[i
]);
7478 #ifndef IPSEC_NONBLOCK_ACQUIRE
7479 LIST_INIT(&acqtree
);
7481 LIST_INIT(&spacqtree
);
7483 /* system default */
7485 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
7486 ip4_def_policy
.refcnt
++; /*never reclaim this*/
7489 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
7490 ip6_def_policy
.refcnt
++; /*never reclaim this*/
7493 #ifndef IPSEC_DEBUG2
7494 timeout((void *)key_timehandler
, (void *)0, hz
);
7495 #endif /*IPSEC_DEBUG2*/
7497 /* initialize key statistics */
7498 keystat
.getspi_count
= 1;
7501 printf("IPsec: Initialized Security Association Processing.\n");
7508 * XXX: maybe This function is called after INBOUND IPsec processing.
7510 * Special check for tunnel-mode packets.
7511 * We must make some checks for consistency between inner and outer IP header.
7513 * xxx more checks to be provided
7516 key_checktunnelsanity(sav
, family
, src
, dst
)
7517 struct secasvar
*sav
;
7522 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7525 if (sav
->sah
== NULL
)
7526 panic("sav->sah == NULL at key_checktunnelsanity");
7528 /* XXX: check inner IP header */
7534 #define hostnamelen strlen(hostname)
7537 * Get FQDN for the host.
7538 * If the administrator configured hostname (by hostname(1)) without
7539 * domain name, returns nothing.
7546 static char fqdn
[MAXHOSTNAMELEN
+ 1];
7551 /* check if it comes with domain name. */
7553 for (i
= 0; i
< hostnamelen
; i
++) {
7554 if (hostname
[i
] == '.')
7560 /* NOTE: hostname may not be NUL-terminated. */
7561 bzero(fqdn
, sizeof(fqdn
));
7562 bcopy(hostname
, fqdn
, hostnamelen
);
7563 fqdn
[hostnamelen
] = '\0';
7568 * get username@FQDN for the host/user.
7574 static char userfqdn
[MAXHOSTNAMELEN
+ MAXLOGNAME
+ 2];
7575 struct proc
*p
= curproc
;
7578 if (!p
|| !p
->p_pgrp
|| !p
->p_pgrp
->pg_session
)
7580 if (!(host
= key_getfqdn()))
7583 /* NOTE: s_login may not be-NUL terminated. */
7584 bzero(userfqdn
, sizeof(userfqdn
));
7585 bcopy(p
->p_pgrp
->pg_session
->s_login
, userfqdn
, MAXLOGNAME
);
7586 userfqdn
[MAXLOGNAME
] = '\0'; /* safeguard */
7587 q
= userfqdn
+ strlen(userfqdn
);
7589 bcopy(host
, q
, strlen(host
));
7597 /* record data transfer on SA, and update timestamps */
7599 key_sa_recordxfer(sav
, m
)
7600 struct secasvar
*sav
;
7603 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7606 panic("key_sa_recordxfer called with sav == NULL");
7608 panic("key_sa_recordxfer called with m == NULL");
7613 * XXX Currently, there is a difference of bytes size
7614 * between inbound and outbound processing.
7616 sav
->lft_c
->sadb_lifetime_bytes
+= m
->m_pkthdr
.len
;
7617 /* to check bytes lifetime is done in key_timehandler(). */
7620 * We use the number of packets as the unit of
7621 * sadb_lifetime_allocations. We increment the variable
7622 * whenever {esp,ah}_{in,out}put is called.
7624 sav
->lft_c
->sadb_lifetime_allocations
++;
7625 /* XXX check for expires? */
7628 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7629 * in seconds. HARD and SOFT lifetime are measured by the time
7630 * difference (again in seconds) from sadb_lifetime_usetime.
7634 * -----+-----+--------+---> t
7635 * <--------------> HARD
7641 sav
->lft_c
->sadb_lifetime_usetime
= tv
.tv_sec
;
7642 /* XXX check for expires? */
7650 key_sa_routechange(dst
)
7651 struct sockaddr
*dst
;
7653 struct secashead
*sah
;
7656 lck_mtx_lock(sadb_mutex
);
7657 LIST_FOREACH(sah
, &sahtree
, chain
) {
7658 ro
= &sah
->sa_route
;
7659 if (ro
->ro_rt
&& dst
->sa_len
== ro
->ro_dst
.sa_len
7660 && bcmp(dst
, &ro
->ro_dst
, dst
->sa_len
) == 0) {
7662 ro
->ro_rt
= (struct rtentry
*)NULL
;
7665 lck_mtx_unlock(sadb_mutex
);
7671 key_sa_chgstate(sav
, state
)
7672 struct secasvar
*sav
;
7675 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7678 panic("key_sa_chgstate called with sav == NULL");
7680 if (sav
->state
== state
)
7683 if (__LIST_CHAINED(sav
))
7684 LIST_REMOVE(sav
, chain
);
7687 LIST_INSERT_HEAD(&sav
->sah
->savtree
[state
], sav
, chain
);
7692 struct secasvar
*sav
;
7695 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
7698 panic("key_sa_stir_iv called with sav == NULL");
7699 key_randomfill(sav
->iv
, sav
->ivlen
);
7703 static struct mbuf
*
7707 struct mbuf
*m
= NULL
, *n
;
7712 MGET(n
, M_DONTWAIT
, MT_DATA
);
7713 if (n
&& len
> MLEN
)
7714 MCLGET(n
, M_DONTWAIT
);
7722 n
->m_len
= M_TRAILINGSPACE(n
);
7723 /* use the bottom of mbuf, hoping we can prepend afterwards */
7724 if (n
->m_len
> len
) {
7725 t
= (n
->m_len
- len
) & ~(sizeof(long) - 1);