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
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/mutex.h>
48 #include <sys/domain.h>
49 #include <sys/protosw.h>
50 #include <sys/malloc.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/errno.h>
56 #include <sys/queue.h>
57 #include <sys/refcount.h>
58 #include <sys/syslog.h>
61 #include <net/route.h>
62 #include <net/raw_cb.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <netinet/in_var.h>
70 #include <netinet/ip6.h>
71 #include <netinet6/in6_var.h>
72 #include <netinet6/ip6_var.h>
76 #include <netinet/in_pcb.h>
79 #include <netinet6/in6_pcb.h>
82 #include <net/pfkeyv2.h>
83 #include <netipsec/keydb.h>
84 #include <netipsec/key.h>
85 #include <netipsec/keysock.h>
86 #include <netipsec/key_debug.h>
88 #include <netipsec/ipsec.h>
90 #include <netipsec/ipsec6.h>
93 #include <netipsec/xform.h>
95 #include <machine/stdarg.h>
98 #include <sys/random.h>
100 #define FULLMASK 0xff
101 #define _BITS(bytes) ((bytes) << 3)
104 * Note on SA reference counting:
105 * - SAs that are not in DEAD state will have (total external reference + 1)
106 * following value in reference count field. they cannot be freed and are
107 * referenced from SA header.
108 * - SAs that are in DEAD state will have (total external reference)
109 * in reference count field. they are ready to be freed. reference from
110 * SA header will be removed in key_delsav(), when the reference count
111 * field hits 0 (= no external reference other than from SA header.
114 u_int32_t key_debug_level = 0;
115 static u_int key_spi_trycnt = 1000;
116 static u_int32_t key_spi_minval = 0x100;
117 static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */
118 static u_int32_t policy_id = 0;
119 static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/
120 static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/
121 static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/
122 static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/
123 static int key_preferred_oldsa = 1; /* preferred old sa rather than new sa.*/
125 static u_int32_t acq_seq = 0;
127 static LIST_HEAD(_sptree, secpolicy) sptree[IPSEC_DIR_MAX]; /* SPD */
128 static struct mtx sptree_lock;
129 #define SPTREE_LOCK_INIT() \
130 mtx_init(&sptree_lock, "sptree", \
131 "fast ipsec security policy database", MTX_DEF)
132 #define SPTREE_LOCK_DESTROY() mtx_destroy(&sptree_lock)
133 #define SPTREE_LOCK() mtx_lock(&sptree_lock)
134 #define SPTREE_UNLOCK() mtx_unlock(&sptree_lock)
135 #define SPTREE_LOCK_ASSERT() mtx_assert(&sptree_lock, MA_OWNED)
137 static LIST_HEAD(_sahtree, secashead) sahtree; /* SAD */
138 static struct mtx sahtree_lock;
139 #define SAHTREE_LOCK_INIT() \
140 mtx_init(&sahtree_lock, "sahtree", \
141 "fast ipsec security association database", MTX_DEF)
142 #define SAHTREE_LOCK_DESTROY() mtx_destroy(&sahtree_lock)
143 #define SAHTREE_LOCK() mtx_lock(&sahtree_lock)
144 #define SAHTREE_UNLOCK() mtx_unlock(&sahtree_lock)
145 #define SAHTREE_LOCK_ASSERT() mtx_assert(&sahtree_lock, MA_OWNED)
148 static LIST_HEAD(_regtree, secreg) regtree[SADB_SATYPE_MAX + 1];
149 static struct mtx regtree_lock;
150 #define REGTREE_LOCK_INIT() \
151 mtx_init(®tree_lock, "regtree", "fast ipsec regtree", MTX_DEF)
152 #define REGTREE_LOCK_DESTROY() mtx_destroy(®tree_lock)
153 #define REGTREE_LOCK() mtx_lock(®tree_lock)
154 #define REGTREE_UNLOCK() mtx_unlock(®tree_lock)
155 #define REGTREE_LOCK_ASSERT() mtx_assert(®tree_lock, MA_OWNED)
157 static LIST_HEAD(_acqtree, secacq) acqtree; /* acquiring list */
158 static struct mtx acq_lock;
159 #define ACQ_LOCK_INIT() \
160 mtx_init(&acq_lock, "acqtree", "fast ipsec acquire list", MTX_DEF)
161 #define ACQ_LOCK_DESTROY() mtx_destroy(&acq_lock)
162 #define ACQ_LOCK() mtx_lock(&acq_lock)
163 #define ACQ_UNLOCK() mtx_unlock(&acq_lock)
164 #define ACQ_LOCK_ASSERT() mtx_assert(&acq_lock, MA_OWNED)
166 static LIST_HEAD(_spacqtree, secspacq) spacqtree; /* SP acquiring list */
167 static struct mtx spacq_lock;
168 #define SPACQ_LOCK_INIT() \
169 mtx_init(&spacq_lock, "spacqtree", \
170 "fast ipsec security policy acquire list", MTX_DEF)
171 #define SPACQ_LOCK_DESTROY() mtx_destroy(&spacq_lock)
172 #define SPACQ_LOCK() mtx_lock(&spacq_lock)
173 #define SPACQ_UNLOCK() mtx_unlock(&spacq_lock)
174 #define SPACQ_LOCK_ASSERT() mtx_assert(&spacq_lock, MA_OWNED)
176 /* search order for SAs */
177 static const u_int saorder_state_valid_prefer_old[] = {
178 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
180 static const u_int saorder_state_valid_prefer_new[] = {
181 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
183 static u_int saorder_state_alive[] = {
185 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
187 static u_int saorder_state_any[] = {
188 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
189 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
192 static const int minsize[] = {
193 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
194 sizeof(struct sadb_sa), /* SADB_EXT_SA */
195 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
196 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
197 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
198 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
199 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
200 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
201 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
202 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
203 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
204 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
205 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
206 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
207 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
208 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
209 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
210 0, /* SADB_X_EXT_KMPRIVATE */
211 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
212 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
214 static const int maxsize[] = {
215 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
216 sizeof(struct sadb_sa), /* SADB_EXT_SA */
217 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
218 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
219 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
220 0, /* SADB_EXT_ADDRESS_SRC */
221 0, /* SADB_EXT_ADDRESS_DST */
222 0, /* SADB_EXT_ADDRESS_PROXY */
223 0, /* SADB_EXT_KEY_AUTH */
224 0, /* SADB_EXT_KEY_ENCRYPT */
225 0, /* SADB_EXT_IDENTITY_SRC */
226 0, /* SADB_EXT_IDENTITY_DST */
227 0, /* SADB_EXT_SENSITIVITY */
228 0, /* SADB_EXT_PROPOSAL */
229 0, /* SADB_EXT_SUPPORTED_AUTH */
230 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
231 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
232 0, /* SADB_X_EXT_KMPRIVATE */
233 0, /* SADB_X_EXT_POLICY */
234 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
237 static int ipsec_esp_keymin = 256;
238 static int ipsec_esp_auth = 0;
239 static int ipsec_ah_keymin = 128;
242 SYSCTL_DECL(_net_key);
245 SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW, \
246 &key_debug_level, 0, "");
248 /* max count of trial for the decision of spi value */
249 SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW, \
250 &key_spi_trycnt, 0, "");
252 /* minimum spi value to allocate automatically. */
253 SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW, \
254 &key_spi_minval, 0, "");
256 /* maximun spi value to allocate automatically. */
257 SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW, \
258 &key_spi_maxval, 0, "");
260 /* interval to initialize randseed */
261 SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW, \
262 &key_int_random, 0, "");
264 /* lifetime for larval SA */
265 SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW, \
266 &key_larval_lifetime, 0, "");
268 /* counter for blocking to send SADB_ACQUIRE to IKEd */
269 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW, \
270 &key_blockacq_count, 0, "");
272 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
273 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW, \
274 &key_blockacq_lifetime, 0, "");
277 SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth, CTLFLAG_RW, \
278 &ipsec_esp_auth, 0, "");
280 /* minimum ESP key length */
281 SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin, CTLFLAG_RW, \
282 &ipsec_esp_keymin, 0, "");
284 /* minimum AH key length */
285 SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin, CTLFLAG_RW, \
286 &ipsec_ah_keymin, 0, "");
288 /* perfered old SA rather than new SA */
289 SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, preferred_oldsa, CTLFLAG_RW,\
290 &key_preferred_oldsa, 0, "");
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))); \
324 MALLOC_DEFINE(M_IPSEC_SA, "secasvar", "ipsec security association");
325 MALLOC_DEFINE(M_IPSEC_SAH, "sahead", "ipsec sa head");
326 MALLOC_DEFINE(M_IPSEC_SP, "ipsecpolicy", "ipsec security policy");
327 MALLOC_DEFINE(M_IPSEC_SR, "ipsecrequest", "ipsec security request");
328 MALLOC_DEFINE(M_IPSEC_MISC, "ipsec-misc", "ipsec miscellaneous");
329 MALLOC_DEFINE(M_IPSEC_SAQ, "ipsec-saq", "ipsec sa acquire");
330 MALLOC_DEFINE(M_IPSEC_SAR, "ipsec-reg", "ipsec sa acquire");
333 * set parameters into secpolicyindex buffer.
334 * Must allocate secpolicyindex buffer passed to this function.
336 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
338 bzero((idx), sizeof(struct secpolicyindex)); \
339 (idx)->dir = (_dir); \
340 (idx)->prefs = (ps); \
341 (idx)->prefd = (pd); \
342 (idx)->ul_proto = (ulp); \
343 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
344 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
348 * set parameters into secasindex buffer.
349 * Must allocate secasindex buffer before calling this function.
351 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
353 bzero((idx), sizeof(struct secasindex)); \
354 (idx)->proto = (p); \
356 (idx)->reqid = (r); \
357 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
358 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
363 u_long getspi_count; /* the avarage of count to try to get new SPI */
367 struct sadb_msg *msg;
368 struct sadb_ext *ext[SADB_EXT_MAX + 1];
369 int extoff[SADB_EXT_MAX + 1];
370 int extlen[SADB_EXT_MAX + 1];
373 static struct secasvar *key_allocsa_policy __P((const struct secasindex *));
374 static void key_freesp_so __P((struct secpolicy **));
375 static struct secasvar *key_do_allocsa_policy __P((struct secashead *, u_int));
376 static void key_delsp __P((struct secpolicy *));
377 static struct secpolicy *key_getsp __P((struct secpolicyindex *));
378 static void _key_delsp(struct secpolicy *sp);
379 static struct secpolicy *key_getspbyid __P((u_int32_t));
380 static u_int32_t key_newreqid __P((void));
381 static struct mbuf *key_gather_mbuf __P((struct mbuf *,
382 const struct sadb_msghdr *, int, int, ...));
383 static int key_spdadd __P((struct socket *, struct mbuf *,
384 const struct sadb_msghdr *));
385 static u_int32_t key_getnewspid __P((void));
386 static int key_spddelete __P((struct socket *, struct mbuf *,
387 const struct sadb_msghdr *));
388 static int key_spddelete2 __P((struct socket *, struct mbuf *,
389 const struct sadb_msghdr *));
390 static int key_spdget __P((struct socket *, struct mbuf *,
391 const struct sadb_msghdr *));
392 static int key_spdflush __P((struct socket *, struct mbuf *,
393 const struct sadb_msghdr *));
394 static int key_spddump __P((struct socket *, struct mbuf *,
395 const struct sadb_msghdr *));
396 static struct mbuf *key_setdumpsp __P((struct secpolicy *,
397 u_int8_t, u_int32_t, u_int32_t));
398 static u_int key_getspreqmsglen __P((struct secpolicy *));
399 static int key_spdexpire __P((struct secpolicy *));
400 static struct secashead *key_newsah __P((struct secasindex *));
401 static void key_delsah __P((struct secashead *));
402 static struct secasvar *key_newsav __P((struct mbuf *,
403 const struct sadb_msghdr *, struct secashead *, int *,
405 #define KEY_NEWSAV(m, sadb, sah, e) \
406 key_newsav(m, sadb, sah, e, __FILE__, __LINE__)
407 static void key_delsav __P((struct secasvar *));
408 static struct secashead *key_getsah __P((struct secasindex *));
409 static struct secasvar *key_checkspidup __P((struct secasindex *, u_int32_t));
410 static struct secasvar *key_getsavbyspi __P((struct secashead *, u_int32_t));
411 static int key_setsaval __P((struct secasvar *, struct mbuf *,
412 const struct sadb_msghdr *));
413 static int key_mature __P((struct secasvar *));
414 static struct mbuf *key_setdumpsa __P((struct secasvar *, u_int8_t,
415 u_int8_t, u_int32_t, u_int32_t));
416 static struct mbuf *key_setsadbmsg __P((u_int8_t, u_int16_t, u_int8_t,
417 u_int32_t, pid_t, u_int16_t));
418 static struct mbuf *key_setsadbsa __P((struct secasvar *));
419 static struct mbuf *key_setsadbaddr __P((u_int16_t,
420 const struct sockaddr *, u_int8_t, u_int16_t));
421 static struct mbuf *key_setsadbxsa2 __P((u_int8_t, u_int32_t, u_int32_t));
422 static struct mbuf *key_setsadbxpolicy __P((u_int16_t, u_int8_t,
424 static void *key_dup(const void *, u_int, struct malloc_type *);
426 static int key_ismyaddr6 __P((struct sockaddr_in6 *));
429 /* flags for key_cmpsaidx() */
430 #define CMP_HEAD 1 /* protocol, addresses. */
431 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
432 #define CMP_REQID 3 /* additionally HEAD, reaid. */
433 #define CMP_EXACTLY 4 /* all elements. */
434 static int key_cmpsaidx
435 __P((const struct secasindex *, const struct secasindex *, int));
437 static int key_cmpspidx_exactly
438 __P((struct secpolicyindex *, struct secpolicyindex *));
439 static int key_cmpspidx_withmask
440 __P((struct secpolicyindex *, struct secpolicyindex *));
441 static int key_sockaddrcmp __P((const struct sockaddr *, const struct sockaddr *, int));
442 static int key_bbcmp __P((const void *, const void *, u_int));
443 static u_int16_t key_satype2proto __P((u_int8_t));
444 static u_int8_t key_proto2satype __P((u_int16_t));
446 static int key_getspi __P((struct socket *, struct mbuf *,
447 const struct sadb_msghdr *));
448 static u_int32_t key_do_getnewspi __P((struct sadb_spirange *,
449 struct secasindex *));
450 static int key_update __P((struct socket *, struct mbuf *,
451 const struct sadb_msghdr *));
452 #ifdef IPSEC_DOSEQCHECK
453 static struct secasvar *key_getsavbyseq __P((struct secashead *, u_int32_t));
455 static int key_add __P((struct socket *, struct mbuf *,
456 const struct sadb_msghdr *));
457 static int key_setident __P((struct secashead *, struct mbuf *,
458 const struct sadb_msghdr *));
459 static struct mbuf *key_getmsgbuf_x1 __P((struct mbuf *,
460 const struct sadb_msghdr *));
461 static int key_delete __P((struct socket *, struct mbuf *,
462 const struct sadb_msghdr *));
463 static int key_get __P((struct socket *, struct mbuf *,
464 const struct sadb_msghdr *));
466 static void key_getcomb_setlifetime __P((struct sadb_comb *));
467 static struct mbuf *key_getcomb_esp __P((void));
468 static struct mbuf *key_getcomb_ah __P((void));
469 static struct mbuf *key_getcomb_ipcomp __P((void));
470 static struct mbuf *key_getprop __P((const struct secasindex *));
472 static int key_acquire __P((const struct secasindex *, struct secpolicy *));
473 static struct secacq *key_newacq __P((const struct secasindex *));
474 static struct secacq *key_getacq __P((const struct secasindex *));
475 static struct secacq *key_getacqbyseq __P((u_int32_t));
476 static struct secspacq *key_newspacq __P((struct secpolicyindex *));
477 static struct secspacq *key_getspacq __P((struct secpolicyindex *));
478 static int key_acquire2 __P((struct socket *, struct mbuf *,
479 const struct sadb_msghdr *));
480 static int key_register __P((struct socket *, struct mbuf *,
481 const struct sadb_msghdr *));
482 static int key_expire __P((struct secasvar *));
483 static int key_flush __P((struct socket *, struct mbuf *,
484 const struct sadb_msghdr *));
485 static int key_dump __P((struct socket *, struct mbuf *,
486 const struct sadb_msghdr *));
487 static int key_promisc __P((struct socket *, struct mbuf *,
488 const struct sadb_msghdr *));
489 static int key_senderror __P((struct socket *, struct mbuf *, int));
490 static int key_validate_ext __P((const struct sadb_ext *, int));
491 static int key_align __P((struct mbuf *, struct sadb_msghdr *));
493 static const char *key_getfqdn __P((void));
494 static const char *key_getuserfqdn __P((void));
496 static void key_sa_chgstate __P((struct secasvar *, u_int8_t));
497 static struct mbuf *key_alloc_mbuf __P((int));
500 sa_initref(struct secasvar *sav)
503 refcount_init(&sav->refcnt, 1);
506 sa_addref(struct secasvar *sav)
509 refcount_acquire(&sav->refcnt);
510 IPSEC_ASSERT(sav->refcnt != 0, ("SA refcnt overflow"));
513 sa_delref(struct secasvar *sav)
516 IPSEC_ASSERT(sav->refcnt > 0, ("SA refcnt underflow"));
517 return (refcount_release(&sav->refcnt));
520 #define SP_ADDREF(p) do { \
522 IPSEC_ASSERT((p)->refcnt != 0, ("SP refcnt overflow")); \
524 #define SP_DELREF(p) do { \
525 IPSEC_ASSERT((p)->refcnt > 0, ("SP refcnt underflow")); \
531 * Update the refcnt while holding the SPTREE lock.
534 key_addref(struct secpolicy *sp)
542 * Return 0 when there are known to be no SP's for the specified
543 * direction. Otherwise return 1. This is used by IPsec code
544 * to optimize performance.
547 key_havesp(u_int dir)
549 return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ?
550 LIST_FIRST(&sptree[dir]) != NULL : 1);
553 /* %%% IPsec policy management */
555 * allocating a SP for OUTBOUND or INBOUND packet.
556 * Must call key_freesp() later.
557 * OUT: NULL: not found
558 * others: found and return the pointer.
561 key_allocsp(struct secpolicyindex *spidx, u_int dir, const char* where, int tag)
563 struct secpolicy *sp;
565 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
566 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
567 ("invalid direction %u", dir));
569 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
570 printf("DP %s from %s:%u\n", __func__, where, tag));
573 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
574 printf("*** objects\n");
575 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))
592 KEY_CHKSPDIR(sp->spidx.dir, dir, __func__);
594 /* found a SPD entry */
595 sp->lastused = time_second;
600 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
601 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
602 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
607 * allocating a SP for OUTBOUND or INBOUND packet.
608 * Must call key_freesp() later.
609 * OUT: NULL: not found
610 * others: found and return the pointer.
613 key_allocsp2(u_int32_t spi,
614 union sockaddr_union *dst,
617 const char* where, int tag)
619 struct secpolicy *sp;
621 IPSEC_ASSERT(dst != NULL, ("null dst"));
622 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
623 ("invalid direction %u", dir));
625 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
626 printf("DP %s from %s:%u\n", __func__, where, tag));
629 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
630 printf("*** objects\n");
631 printf("spi %u proto %u dir %u\n", spi, proto, dir);
632 kdebug_sockaddr(&dst->sa));
635 LIST_FOREACH(sp, &sptree[dir], chain) {
636 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
637 printf("*** in SPD\n");
638 kdebug_secpolicyindex(&sp->spidx));
640 if (sp->state == IPSEC_SPSTATE_DEAD)
642 /* compare simple values, then dst address */
643 if (sp->spidx.ul_proto != proto)
645 /* NB: spi's must exist and match */
646 if (!sp->req || !sp->req->sav || sp->req->sav->spi != spi)
648 if (key_sockaddrcmp(&sp->spidx.dst.sa, &dst->sa, 1) == 0)
655 KEY_CHKSPDIR(sp->spidx.dir, dir, __func__);
657 /* found a SPD entry */
658 sp->lastused = time_second;
663 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
664 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
665 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
670 * return a policy that matches this particular inbound packet.
674 key_gettunnel(const struct sockaddr *osrc,
675 const struct sockaddr *odst,
676 const struct sockaddr *isrc,
677 const struct sockaddr *idst,
678 const char* where, int tag)
680 struct secpolicy *sp;
681 const int dir = IPSEC_DIR_INBOUND;
682 struct ipsecrequest *r1, *r2, *p;
683 struct secpolicyindex spidx;
685 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
686 printf("DP %s from %s:%u\n", __func__, where, tag));
688 if (isrc->sa_family != idst->sa_family) {
689 ipseclog((LOG_ERR, "%s: protocol family mismatched %d != %d\n.",
690 __func__, isrc->sa_family, idst->sa_family));
696 LIST_FOREACH(sp, &sptree[dir], chain) {
697 if (sp->state == IPSEC_SPSTATE_DEAD)
701 for (p = sp->req; p; p = p->next) {
702 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
709 /* here we look at address matches only */
711 if (isrc->sa_len > sizeof(spidx.src) ||
712 idst->sa_len > sizeof(spidx.dst))
714 bcopy(isrc, &spidx.src, isrc->sa_len);
715 bcopy(idst, &spidx.dst, idst->sa_len);
716 if (!key_cmpspidx_withmask(&sp->spidx, &spidx))
719 if (key_sockaddrcmp(&r1->saidx.src.sa, isrc, 0) ||
720 key_sockaddrcmp(&r1->saidx.dst.sa, idst, 0))
724 if (key_sockaddrcmp(&r2->saidx.src.sa, osrc, 0) ||
725 key_sockaddrcmp(&r2->saidx.dst.sa, odst, 0))
734 sp->lastused = time_second;
739 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
740 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
741 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
746 * allocating an SA entry for an *OUTBOUND* packet.
747 * checking each request entries in SP, and acquire an SA if need.
748 * OUT: 0: there are valid requests.
749 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
752 key_checkrequest(struct ipsecrequest *isr, const struct secasindex *saidx)
757 IPSEC_ASSERT(isr != NULL, ("null isr"));
758 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
759 IPSEC_ASSERT(saidx->mode == IPSEC_MODE_TRANSPORT ||
760 saidx->mode == IPSEC_MODE_TUNNEL,
761 ("unexpected policy %u", saidx->mode));
764 * XXX guard against protocol callbacks from the crypto
765 * thread as they reference ipsecrequest.sav which we
766 * temporarily null out below. Need to rethink how we
767 * handle bundled SA's in the callback thread.
769 IPSECREQUEST_LOCK_ASSERT(isr);
771 /* get current level */
772 level = ipsec_get_reqlevel(isr);
775 * We do allocate new SA only if the state of SA in the holder is
776 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
778 if (isr->sav != NULL) {
779 if (isr->sav->sah == NULL)
780 panic("%s: sah is null.\n", __func__);
781 if (isr->sav == (struct secasvar *)LIST_FIRST(
782 &isr->sav->sah->savtree[SADB_SASTATE_DEAD])) {
783 KEY_FREESAV(&isr->sav);
789 * we free any SA stashed in the IPsec request because a different
790 * SA may be involved each time this request is checked, either
791 * because new SAs are being configured, or this request is
792 * associated with an unconnected datagram socket, or this request
793 * is associated with a system default policy.
795 * The operation may have negative impact to performance. We may
796 * want to check cached SA carefully, rather than picking new SA
799 if (isr->sav != NULL) {
800 KEY_FREESAV(&isr->sav);
806 * new SA allocation if no SA found.
807 * key_allocsa_policy should allocate the oldest SA available.
808 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
810 if (isr->sav == NULL)
811 isr->sav = key_allocsa_policy(saidx);
813 /* When there is SA. */
814 if (isr->sav != NULL) {
815 if (isr->sav->state != SADB_SASTATE_MATURE &&
816 isr->sav->state != SADB_SASTATE_DYING)
822 error = key_acquire(saidx, isr->sp);
824 /* XXX What should I do ? */
825 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
830 if (level != IPSEC_LEVEL_REQUIRE) {
831 /* XXX sigh, the interface to this routine is botched */
832 IPSEC_ASSERT(isr->sav == NULL, ("unexpected SA"));
840 * allocating a SA for policy entry from SAD.
841 * NOTE: searching SAD of aliving state.
842 * OUT: NULL: not found.
843 * others: found and return the pointer.
845 static struct secasvar *
846 key_allocsa_policy(const struct secasindex *saidx)
848 #define N(a) _ARRAYLEN(a)
849 struct secashead *sah;
850 struct secasvar *sav;
851 u_int stateidx, arraysize;
852 const u_int *state_valid;
855 LIST_FOREACH(sah, &sahtree, chain) {
856 if (sah->state == SADB_SASTATE_DEAD)
858 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID)) {
859 if (key_preferred_oldsa) {
860 state_valid = saorder_state_valid_prefer_old;
861 arraysize = N(saorder_state_valid_prefer_old);
863 state_valid = saorder_state_valid_prefer_new;
864 arraysize = N(saorder_state_valid_prefer_new);
875 /* search valid state */
876 for (stateidx = 0; stateidx < arraysize; stateidx++) {
877 sav = key_do_allocsa_policy(sah, state_valid[stateidx]);
887 * searching SAD with direction, protocol, mode and state.
888 * called by key_allocsa_policy().
891 * others : found, pointer to a SA.
893 static struct secasvar *
894 key_do_allocsa_policy(struct secashead *sah, u_int state)
896 struct secasvar *sav, *nextsav, *candidate, *d;
902 for (sav = LIST_FIRST(&sah->savtree[state]);
906 nextsav = LIST_NEXT(sav, chain);
909 KEY_CHKSASTATE(sav->state, state, __func__);
912 if (candidate == NULL) {
917 /* Which SA is the better ? */
919 IPSEC_ASSERT(candidate->lft_c != NULL,
920 ("null candidate lifetime"));
921 IPSEC_ASSERT(sav->lft_c != NULL, ("null sav lifetime"));
923 /* What the best method is to compare ? */
924 if (key_preferred_oldsa) {
925 if (candidate->lft_c->sadb_lifetime_addtime >
926 sav->lft_c->sadb_lifetime_addtime) {
933 /* preferred new sa rather than old sa */
934 if (candidate->lft_c->sadb_lifetime_addtime <
935 sav->lft_c->sadb_lifetime_addtime) {
942 * prepared to delete the SA when there is more
943 * suitable candidate and the lifetime of the SA is not
946 if (d->lft_h->sadb_lifetime_addtime != 0) {
947 struct mbuf *m, *result;
950 key_sa_chgstate(d, SADB_SASTATE_DEAD);
952 IPSEC_ASSERT(d->refcnt > 0, ("bogus ref count"));
954 satype = key_proto2satype(d->sah->saidx.proto);
958 m = key_setsadbmsg(SADB_DELETE, 0,
959 satype, 0, 0, d->refcnt - 1);
964 /* set sadb_address for saidx's. */
965 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
966 &d->sah->saidx.src.sa,
967 d->sah->saidx.src.sa.sa_len << 3,
973 /* set sadb_address for saidx's. */
974 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
975 &d->sah->saidx.dst.sa,
976 d->sah->saidx.dst.sa.sa_len << 3,
982 /* create SA extension */
983 m = key_setsadbsa(d);
988 if (result->m_len < sizeof(struct sadb_msg)) {
989 result = m_pullup(result,
990 sizeof(struct sadb_msg));
995 result->m_pkthdr.len = 0;
996 for (m = result; m; m = m->m_next)
997 result->m_pkthdr.len += m->m_len;
998 mtod(result, struct sadb_msg *)->sadb_msg_len =
999 PFKEY_UNIT64(result->m_pkthdr.len);
1001 if (key_sendup_mbuf(NULL, result,
1002 KEY_SENDUP_REGISTERED))
1009 sa_addref(candidate);
1010 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1011 printf("DP %s cause refcnt++:%d SA:%p\n",
1012 __func__, candidate->refcnt, candidate));
1020 * allocating a usable SA entry for a *INBOUND* packet.
1021 * Must call key_freesav() later.
1022 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
1023 * NULL: not found, or error occured.
1025 * In the comparison, no source address is used--for RFC2401 conformance.
1026 * To quote, from section 4.1:
1027 * A security association is uniquely identified by a triple consisting
1028 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1029 * security protocol (AH or ESP) identifier.
1030 * Note that, however, we do need to keep source address in IPsec SA.
1031 * IKE specification and PF_KEY specification do assume that we
1032 * keep source address in IPsec SA. We see a tricky situation here.
1036 union sockaddr_union *dst,
1039 const char* where, int tag)
1041 struct secashead *sah;
1042 struct secasvar *sav;
1043 u_int stateidx, arraysize, state;
1044 const u_int *saorder_state_valid;
1046 IPSEC_ASSERT(dst != NULL, ("null dst address"));
1048 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1049 printf("DP %s from %s:%u\n", __func__, where, tag));
1053 * XXX: to be checked internal IP header somewhere. Also when
1054 * IPsec tunnel packet is received. But ESP tunnel mode is
1055 * encrypted so we can't check internal IP header.
1058 if (key_preferred_oldsa) {
1059 saorder_state_valid = saorder_state_valid_prefer_old;
1060 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
1062 saorder_state_valid = saorder_state_valid_prefer_new;
1063 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
1065 LIST_FOREACH(sah, &sahtree, chain) {
1066 /* search valid state */
1067 for (stateidx = 0; stateidx < arraysize; stateidx++) {
1068 state = saorder_state_valid[stateidx];
1069 LIST_FOREACH(sav, &sah->savtree[state], chain) {
1071 KEY_CHKSASTATE(sav->state, state, __func__);
1072 /* do not return entries w/ unusable state */
1073 if (sav->state != SADB_SASTATE_MATURE &&
1074 sav->state != SADB_SASTATE_DYING)
1076 if (proto != sav->sah->saidx.proto)
1078 if (spi != sav->spi)
1080 #if 0 /* don't check src */
1081 /* check src address */
1082 if (key_sockaddrcmp(&src->sa, &sav->sah->saidx.src.sa, 0) != 0)
1085 /* check dst address */
1086 if (key_sockaddrcmp(&dst->sa, &sav->sah->saidx.dst.sa, 0) != 0)
1097 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1098 printf("DP %s return SA:%p; refcnt %u\n", __func__,
1099 sav, sav ? sav->refcnt : 0));
1104 * Must be called after calling key_allocsp().
1105 * For both the packet without socket and key_freeso().
1108 _key_freesp(struct secpolicy **spp, const char* where, int tag)
1110 struct secpolicy *sp = *spp;
1112 IPSEC_ASSERT(sp != NULL, ("null sp"));
1117 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1118 printf("DP %s SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1119 __func__, sp, sp->id, where, tag, sp->refcnt));
1121 if (sp->refcnt == 0) {
1129 * Must be called after calling key_allocsp().
1130 * For the packet with socket.
1133 key_freeso(struct socket *so)
1135 IPSEC_ASSERT(so != NULL, ("null so"));
1137 switch (so->so_proto->pr_domain->dom_family) {
1141 struct inpcb *pcb = sotoinpcb(so);
1143 /* Does it have a PCB ? */
1146 key_freesp_so(&pcb->inp_sp->sp_in);
1147 key_freesp_so(&pcb->inp_sp->sp_out);
1154 #ifdef HAVE_NRL_INPCB
1155 struct inpcb *pcb = sotoinpcb(so);
1157 /* Does it have a PCB ? */
1160 key_freesp_so(&pcb->inp_sp->sp_in);
1161 key_freesp_so(&pcb->inp_sp->sp_out);
1163 struct in6pcb *pcb = sotoin6pcb(so);
1165 /* Does it have a PCB ? */
1168 key_freesp_so(&pcb->in6p_sp->sp_in);
1169 key_freesp_so(&pcb->in6p_sp->sp_out);
1175 ipseclog((LOG_DEBUG, "%s: unknown address family=%d.\n",
1176 __func__, so->so_proto->pr_domain->dom_family));
1182 key_freesp_so(struct secpolicy **sp)
1184 IPSEC_ASSERT(sp != NULL && *sp != NULL, ("null sp"));
1186 if ((*sp)->policy == IPSEC_POLICY_ENTRUST ||
1187 (*sp)->policy == IPSEC_POLICY_BYPASS)
1190 IPSEC_ASSERT((*sp)->policy == IPSEC_POLICY_IPSEC,
1191 ("invalid policy %u", (*sp)->policy));
1196 * Must be called after calling key_allocsa().
1197 * This function is called by key_freesp() to free some SA allocated
1201 key_freesav(struct secasvar **psav, const char* where, int tag)
1203 struct secasvar *sav = *psav;
1205 IPSEC_ASSERT(sav != NULL, ("null sav"));
1207 if (sa_delref(sav)) {
1208 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1209 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1210 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1214 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1215 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1216 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1220 /* %%% SPD management */
1222 * free security policy entry.
1225 key_delsp(struct secpolicy *sp)
1227 struct ipsecrequest *isr, *nextisr;
1229 IPSEC_ASSERT(sp != NULL, ("null sp"));
1230 SPTREE_LOCK_ASSERT();
1232 sp->state = IPSEC_SPSTATE_DEAD;
1234 IPSEC_ASSERT(sp->refcnt == 0,
1235 ("SP with references deleted (refcnt %u)", sp->refcnt));
1237 /* remove from SP index */
1238 if (__LIST_CHAINED(sp))
1239 LIST_REMOVE(sp, chain);
1241 for (isr = sp->req; isr != NULL; isr = nextisr) {
1242 if (isr->sav != NULL) {
1243 KEY_FREESAV(&isr->sav);
1247 nextisr = isr->next;
1255 * OUT: NULL : not found
1256 * others : found, pointer to a SP.
1258 static struct secpolicy *
1259 key_getsp(struct secpolicyindex *spidx)
1261 struct secpolicy *sp;
1263 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
1266 LIST_FOREACH(sp, &sptree[spidx->dir], chain) {
1267 if (sp->state == IPSEC_SPSTATE_DEAD)
1269 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1281 * OUT: NULL : not found
1282 * others : found, pointer to a SP.
1284 static struct secpolicy *
1285 key_getspbyid(u_int32_t id)
1287 struct secpolicy *sp;
1290 LIST_FOREACH(sp, &sptree[IPSEC_DIR_INBOUND], chain) {
1291 if (sp->state == IPSEC_SPSTATE_DEAD)
1299 LIST_FOREACH(sp, &sptree[IPSEC_DIR_OUTBOUND], chain) {
1300 if (sp->state == IPSEC_SPSTATE_DEAD)
1314 key_newsp(const char* where, int tag)
1316 struct secpolicy *newsp = NULL;
1318 newsp = (struct secpolicy *)
1319 malloc(sizeof(struct secpolicy), M_IPSEC_SP, M_NOWAIT|M_ZERO);
1321 SECPOLICY_LOCK_INIT(newsp);
1326 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1327 printf("DP %s from %s:%u return SP:%p\n", __func__,
1328 where, tag, newsp));
1333 _key_delsp(struct secpolicy *sp)
1335 SECPOLICY_LOCK_DESTROY(sp);
1336 free(sp, M_IPSEC_SP);
1340 * create secpolicy structure from sadb_x_policy structure.
1341 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1342 * so must be set properly later.
1345 key_msg2sp(xpl0, len, error)
1346 struct sadb_x_policy *xpl0;
1350 struct secpolicy *newsp;
1352 IPSEC_ASSERT(xpl0 != NULL, ("null xpl0"));
1353 IPSEC_ASSERT(len >= sizeof(*xpl0), ("policy too short: %zu", len));
1355 if (len != PFKEY_EXTLEN(xpl0)) {
1356 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n", __func__));
1361 if ((newsp = KEY_NEWSP()) == NULL) {
1366 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1367 newsp->policy = xpl0->sadb_x_policy_type;
1370 switch (xpl0->sadb_x_policy_type) {
1371 case IPSEC_POLICY_DISCARD:
1372 case IPSEC_POLICY_NONE:
1373 case IPSEC_POLICY_ENTRUST:
1374 case IPSEC_POLICY_BYPASS:
1378 case IPSEC_POLICY_IPSEC:
1381 struct sadb_x_ipsecrequest *xisr;
1382 struct ipsecrequest **p_isr = &newsp->req;
1384 /* validity check */
1385 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1386 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n",
1393 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1394 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1398 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1399 ipseclog((LOG_DEBUG, "%s: invalid ipsecrequest "
1400 "length.\n", __func__));
1406 /* allocate request buffer */
1407 /* NB: data structure is zero'd */
1408 *p_isr = ipsec_newisr();
1409 if ((*p_isr) == NULL) {
1410 ipseclog((LOG_DEBUG,
1411 "%s: No more memory.\n", __func__));
1418 switch (xisr->sadb_x_ipsecrequest_proto) {
1421 case IPPROTO_IPCOMP:
1424 ipseclog((LOG_DEBUG,
1425 "%s: invalid proto type=%u\n", __func__,
1426 xisr->sadb_x_ipsecrequest_proto));
1428 *error = EPROTONOSUPPORT;
1431 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1433 switch (xisr->sadb_x_ipsecrequest_mode) {
1434 case IPSEC_MODE_TRANSPORT:
1435 case IPSEC_MODE_TUNNEL:
1437 case IPSEC_MODE_ANY:
1439 ipseclog((LOG_DEBUG,
1440 "%s: invalid mode=%u\n", __func__,
1441 xisr->sadb_x_ipsecrequest_mode));
1446 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1448 switch (xisr->sadb_x_ipsecrequest_level) {
1449 case IPSEC_LEVEL_DEFAULT:
1450 case IPSEC_LEVEL_USE:
1451 case IPSEC_LEVEL_REQUIRE:
1453 case IPSEC_LEVEL_UNIQUE:
1454 /* validity check */
1456 * If range violation of reqid, kernel will
1457 * update it, don't refuse it.
1459 if (xisr->sadb_x_ipsecrequest_reqid
1460 > IPSEC_MANUAL_REQID_MAX) {
1461 ipseclog((LOG_DEBUG,
1462 "%s: reqid=%d range "
1463 "violation, updated by kernel.\n",
1465 xisr->sadb_x_ipsecrequest_reqid));
1466 xisr->sadb_x_ipsecrequest_reqid = 0;
1469 /* allocate new reqid id if reqid is zero. */
1470 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1472 if ((reqid = key_newreqid()) == 0) {
1477 (*p_isr)->saidx.reqid = reqid;
1478 xisr->sadb_x_ipsecrequest_reqid = reqid;
1480 /* set it for manual keying. */
1481 (*p_isr)->saidx.reqid =
1482 xisr->sadb_x_ipsecrequest_reqid;
1487 ipseclog((LOG_DEBUG, "%s: invalid level=%u\n",
1489 xisr->sadb_x_ipsecrequest_level));
1494 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1496 /* set IP addresses if there */
1497 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1498 struct sockaddr *paddr;
1500 paddr = (struct sockaddr *)(xisr + 1);
1502 /* validity check */
1504 > sizeof((*p_isr)->saidx.src)) {
1505 ipseclog((LOG_DEBUG, "%s: invalid "
1506 "request address length.\n",
1512 bcopy(paddr, &(*p_isr)->saidx.src,
1515 paddr = (struct sockaddr *)((caddr_t)paddr
1518 /* validity check */
1520 > sizeof((*p_isr)->saidx.dst)) {
1521 ipseclog((LOG_DEBUG, "%s: invalid "
1522 "request address length.\n",
1528 bcopy(paddr, &(*p_isr)->saidx.dst,
1532 (*p_isr)->sp = newsp;
1534 /* initialization for the next. */
1535 p_isr = &(*p_isr)->next;
1536 tlen -= xisr->sadb_x_ipsecrequest_len;
1538 /* validity check */
1540 ipseclog((LOG_DEBUG, "%s: becoming tlen < 0.\n",
1547 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1548 + xisr->sadb_x_ipsecrequest_len);
1553 ipseclog((LOG_DEBUG, "%s: invalid policy type.\n", __func__));
1566 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1568 auto_reqid = (auto_reqid == ~0
1569 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1571 /* XXX should be unique check */
1577 * copy secpolicy struct to sadb_x_policy structure indicated.
1581 struct secpolicy *sp;
1583 struct sadb_x_policy *xpl;
1588 IPSEC_ASSERT(sp != NULL, ("null policy"));
1590 tlen = key_getspreqmsglen(sp);
1592 m = key_alloc_mbuf(tlen);
1593 if (!m || m->m_next) { /*XXX*/
1601 xpl = mtod(m, struct sadb_x_policy *);
1604 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1605 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1606 xpl->sadb_x_policy_type = sp->policy;
1607 xpl->sadb_x_policy_dir = sp->spidx.dir;
1608 xpl->sadb_x_policy_id = sp->id;
1609 p = (caddr_t)xpl + sizeof(*xpl);
1611 /* if is the policy for ipsec ? */
1612 if (sp->policy == IPSEC_POLICY_IPSEC) {
1613 struct sadb_x_ipsecrequest *xisr;
1614 struct ipsecrequest *isr;
1616 for (isr = sp->req; isr != NULL; isr = isr->next) {
1618 xisr = (struct sadb_x_ipsecrequest *)p;
1620 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1621 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1622 xisr->sadb_x_ipsecrequest_level = isr->level;
1623 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1626 bcopy(&isr->saidx.src, p, isr->saidx.src.sa.sa_len);
1627 p += isr->saidx.src.sa.sa_len;
1628 bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len);
1629 p += isr->saidx.src.sa.sa_len;
1631 xisr->sadb_x_ipsecrequest_len =
1632 PFKEY_ALIGN8(sizeof(*xisr)
1633 + isr->saidx.src.sa.sa_len
1634 + isr->saidx.dst.sa.sa_len);
1641 /* m will not be freed nor modified */
1642 static struct mbuf *
1644 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1645 int ndeep, int nitem, ...)
1647 key_gather_mbuf(m, mhp, ndeep, nitem, va_alist)
1649 const struct sadb_msghdr *mhp;
1658 struct mbuf *result = NULL, *n;
1661 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1662 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1664 va_start(ap, nitem);
1665 for (i = 0; i < nitem; i++) {
1666 idx = va_arg(ap, int);
1667 if (idx < 0 || idx > SADB_EXT_MAX)
1669 /* don't attempt to pull empty extension */
1670 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1672 if (idx != SADB_EXT_RESERVED &&
1673 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1676 if (idx == SADB_EXT_RESERVED) {
1677 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1679 IPSEC_ASSERT(len <= MHLEN, ("header too big %u", len));
1681 MGETHDR(n, M_DONTWAIT, MT_DATA);
1686 m_copydata(m, 0, sizeof(struct sadb_msg),
1688 } else if (i < ndeep) {
1689 len = mhp->extlen[idx];
1690 n = key_alloc_mbuf(len);
1691 if (!n || n->m_next) { /*XXX*/
1696 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1699 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1712 if ((result->m_flags & M_PKTHDR) != 0) {
1713 result->m_pkthdr.len = 0;
1714 for (n = result; n; n = n->m_next)
1715 result->m_pkthdr.len += n->m_len;
1726 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1727 * add an entry to SP database, when received
1728 * <base, address(SD), (lifetime(H),) policy>
1730 * Adding to SP database,
1732 * <base, address(SD), (lifetime(H),) policy>
1733 * to the socket which was send.
1735 * SPDADD set a unique policy entry.
1736 * SPDSETIDX like SPDADD without a part of policy requests.
1737 * SPDUPDATE replace a unique policy entry.
1739 * m will always be freed.
1742 key_spdadd(so, m, mhp)
1745 const struct sadb_msghdr *mhp;
1747 struct sadb_address *src0, *dst0;
1748 struct sadb_x_policy *xpl0, *xpl;
1749 struct sadb_lifetime *lft = NULL;
1750 struct secpolicyindex spidx;
1751 struct secpolicy *newsp;
1754 IPSEC_ASSERT(so != NULL, ("null socket"));
1755 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1756 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1757 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
1759 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1760 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1761 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1762 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1763 return key_senderror(so, m, EINVAL);
1765 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1766 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1767 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1768 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1770 return key_senderror(so, m, EINVAL);
1772 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1773 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1774 < sizeof(struct sadb_lifetime)) {
1775 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1777 return key_senderror(so, m, EINVAL);
1779 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1782 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1783 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1784 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1787 /* XXX boundary check against sa_len */
1788 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1791 src0->sadb_address_prefixlen,
1792 dst0->sadb_address_prefixlen,
1793 src0->sadb_address_proto,
1796 /* checking the direciton. */
1797 switch (xpl0->sadb_x_policy_dir) {
1798 case IPSEC_DIR_INBOUND:
1799 case IPSEC_DIR_OUTBOUND:
1802 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
1803 mhp->msg->sadb_msg_errno = EINVAL;
1808 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1809 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1810 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1811 ipseclog((LOG_DEBUG, "%s: Invalid policy type.\n", __func__));
1812 return key_senderror(so, m, EINVAL);
1815 /* policy requests are mandatory when action is ipsec. */
1816 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1817 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1818 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1819 ipseclog((LOG_DEBUG, "%s: some policy requests part required\n",
1821 return key_senderror(so, m, EINVAL);
1825 * checking there is SP already or not.
1826 * SPDUPDATE doesn't depend on whether there is a SP or not.
1827 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1830 newsp = key_getsp(&spidx);
1831 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1833 newsp->state = IPSEC_SPSTATE_DEAD;
1837 if (newsp != NULL) {
1839 ipseclog((LOG_DEBUG, "%s: a SP entry exists already.\n",
1841 return key_senderror(so, m, EEXIST);
1845 /* allocation new SP entry */
1846 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1847 return key_senderror(so, m, error);
1850 if ((newsp->id = key_getnewspid()) == 0) {
1852 return key_senderror(so, m, ENOBUFS);
1855 /* XXX boundary check against sa_len */
1856 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1859 src0->sadb_address_prefixlen,
1860 dst0->sadb_address_prefixlen,
1861 src0->sadb_address_proto,
1864 /* sanity check on addr pair */
1865 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1866 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1868 return key_senderror(so, m, EINVAL);
1870 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1871 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1873 return key_senderror(so, m, EINVAL);
1876 if (newsp->req && newsp->req->saidx.src.sa.sa_family) {
1877 struct sockaddr *sa;
1878 sa = (struct sockaddr *)(src0 + 1);
1879 if (sa->sa_family != newsp->req->saidx.src.sa.sa_family) {
1881 return key_senderror(so, m, EINVAL);
1884 if (newsp->req && newsp->req->saidx.dst.sa.sa_family) {
1885 struct sockaddr *sa;
1886 sa = (struct sockaddr *)(dst0 + 1);
1887 if (sa->sa_family != newsp->req->saidx.dst.sa.sa_family) {
1889 return key_senderror(so, m, EINVAL);
1894 newsp->created = time_second;
1895 newsp->lastused = newsp->created;
1896 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1897 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1899 newsp->refcnt = 1; /* do not reclaim until I say I do */
1900 newsp->state = IPSEC_SPSTATE_ALIVE;
1901 LIST_INSERT_TAIL(&sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1903 /* delete the entry in spacqtree */
1904 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1905 struct secspacq *spacq = key_getspacq(&spidx);
1906 if (spacq != NULL) {
1907 /* reset counter in order to deletion by timehandler. */
1908 spacq->created = time_second;
1915 struct mbuf *n, *mpolicy;
1916 struct sadb_msg *newmsg;
1919 /* create new sadb_msg to reply. */
1921 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1922 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1923 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1925 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1927 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1930 return key_senderror(so, m, ENOBUFS);
1932 if (n->m_len < sizeof(*newmsg)) {
1933 n = m_pullup(n, sizeof(*newmsg));
1935 return key_senderror(so, m, ENOBUFS);
1937 newmsg = mtod(n, struct sadb_msg *);
1938 newmsg->sadb_msg_errno = 0;
1939 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1942 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
1943 sizeof(*xpl), &off);
1944 if (mpolicy == NULL) {
1945 /* n is already freed */
1946 return key_senderror(so, m, ENOBUFS);
1948 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
1949 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
1951 return key_senderror(so, m, EINVAL);
1953 xpl->sadb_x_policy_id = newsp->id;
1956 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
1961 * get new policy id.
1969 u_int32_t newid = 0;
1970 int count = key_spi_trycnt; /* XXX */
1971 struct secpolicy *sp;
1973 /* when requesting to allocate spi ranged */
1975 newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1));
1977 if ((sp = key_getspbyid(newid)) == NULL)
1983 if (count == 0 || newid == 0) {
1984 ipseclog((LOG_DEBUG, "%s: to allocate policy id is failed.\n",
1993 * SADB_SPDDELETE processing
1995 * <base, address(SD), policy(*)>
1996 * from the user(?), and set SADB_SASTATE_DEAD,
1998 * <base, address(SD), policy(*)>
2000 * policy(*) including direction of policy.
2002 * m will always be freed.
2005 key_spddelete(so, m, mhp)
2008 const struct sadb_msghdr *mhp;
2010 struct sadb_address *src0, *dst0;
2011 struct sadb_x_policy *xpl0;
2012 struct secpolicyindex spidx;
2013 struct secpolicy *sp;
2015 IPSEC_ASSERT(so != NULL, ("null so"));
2016 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2017 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2018 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2020 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
2021 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
2022 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
2023 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2025 return key_senderror(so, m, EINVAL);
2027 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
2028 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
2029 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2030 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2032 return key_senderror(so, m, EINVAL);
2035 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2036 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2037 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2040 /* XXX boundary check against sa_len */
2041 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2044 src0->sadb_address_prefixlen,
2045 dst0->sadb_address_prefixlen,
2046 src0->sadb_address_proto,
2049 /* checking the direciton. */
2050 switch (xpl0->sadb_x_policy_dir) {
2051 case IPSEC_DIR_INBOUND:
2052 case IPSEC_DIR_OUTBOUND:
2055 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
2056 return key_senderror(so, m, EINVAL);
2059 /* Is there SP in SPD ? */
2060 if ((sp = key_getsp(&spidx)) == NULL) {
2061 ipseclog((LOG_DEBUG, "%s: no SP found.\n", __func__));
2062 return key_senderror(so, m, EINVAL);
2065 /* save policy id to buffer to be returned. */
2066 xpl0->sadb_x_policy_id = sp->id;
2068 sp->state = IPSEC_SPSTATE_DEAD;
2073 struct sadb_msg *newmsg;
2075 /* create new sadb_msg to reply. */
2076 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2077 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2079 return key_senderror(so, m, ENOBUFS);
2081 newmsg = mtod(n, struct sadb_msg *);
2082 newmsg->sadb_msg_errno = 0;
2083 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2086 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2091 * SADB_SPDDELETE2 processing
2094 * from the user(?), and set SADB_SASTATE_DEAD,
2098 * policy(*) including direction of policy.
2100 * m will always be freed.
2103 key_spddelete2(so, m, mhp)
2106 const struct sadb_msghdr *mhp;
2109 struct secpolicy *sp;
2111 IPSEC_ASSERT(so != NULL, ("null socket"));
2112 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2113 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2114 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2116 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2117 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2118 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", __func__));
2119 return key_senderror(so, m, EINVAL);
2122 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2124 /* Is there SP in SPD ? */
2125 if ((sp = key_getspbyid(id)) == NULL) {
2126 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2127 return key_senderror(so, m, EINVAL);
2130 sp->state = IPSEC_SPSTATE_DEAD;
2134 struct mbuf *n, *nn;
2135 struct sadb_msg *newmsg;
2138 /* create new sadb_msg to reply. */
2139 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2142 return key_senderror(so, m, ENOBUFS);
2143 MGETHDR(n, M_DONTWAIT, MT_DATA);
2144 if (n && len > MHLEN) {
2145 MCLGET(n, M_DONTWAIT);
2146 if ((n->m_flags & M_EXT) == 0) {
2152 return key_senderror(so, m, ENOBUFS);
2158 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2159 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2161 IPSEC_ASSERT(off == len, ("length inconsistency (off %u len %u)",
2164 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2165 mhp->extlen[SADB_X_EXT_POLICY], M_DONTWAIT);
2168 return key_senderror(so, m, ENOBUFS);
2171 n->m_pkthdr.len = 0;
2172 for (nn = n; nn; nn = nn->m_next)
2173 n->m_pkthdr.len += nn->m_len;
2175 newmsg = mtod(n, struct sadb_msg *);
2176 newmsg->sadb_msg_errno = 0;
2177 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2180 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2185 * SADB_X_GET processing
2190 * <base, address(SD), policy>
2192 * policy(*) including direction of policy.
2194 * m will always be freed.
2197 key_spdget(so, m, mhp)
2200 const struct sadb_msghdr *mhp;
2203 struct secpolicy *sp;
2206 IPSEC_ASSERT(so != NULL, ("null socket"));
2207 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2208 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2209 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2211 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2212 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2213 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2215 return key_senderror(so, m, EINVAL);
2218 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2220 /* Is there SP in SPD ? */
2221 if ((sp = key_getspbyid(id)) == NULL) {
2222 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2223 return key_senderror(so, m, ENOENT);
2226 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2229 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2231 return key_senderror(so, m, ENOBUFS);
2235 * SADB_X_SPDACQUIRE processing.
2236 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2239 * to KMD, and expect to receive
2240 * <base> with SADB_X_SPDACQUIRE if error occured,
2243 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2244 * policy(*) is without policy requests.
2247 * others: error number
2251 struct secpolicy *sp;
2253 struct mbuf *result = NULL, *m;
2254 struct secspacq *newspacq;
2256 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2257 IPSEC_ASSERT(sp->req == NULL, ("policy exists"));
2258 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
2259 ("policy not IPSEC %u", sp->policy));
2261 /* Get an entry to check whether sent message or not. */
2262 newspacq = key_getspacq(&sp->spidx);
2263 if (newspacq != NULL) {
2264 if (key_blockacq_count < newspacq->count) {
2265 /* reset counter and do send message. */
2266 newspacq->count = 0;
2268 /* increment counter and do nothing. */
2274 /* make new entry for blocking to send SADB_ACQUIRE. */
2275 newspacq = key_newspacq(&sp->spidx);
2276 if (newspacq == NULL)
2280 /* create new sadb_msg to reply. */
2281 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2287 result->m_pkthdr.len = 0;
2288 for (m = result; m; m = m->m_next)
2289 result->m_pkthdr.len += m->m_len;
2291 mtod(result, struct sadb_msg *)->sadb_msg_len =
2292 PFKEY_UNIT64(result->m_pkthdr.len);
2294 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2298 * SADB_SPDFLUSH processing
2301 * from the user, and free all entries in secpctree.
2305 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2307 * m will always be freed.
2310 key_spdflush(so, m, mhp)
2313 const struct sadb_msghdr *mhp;
2315 struct sadb_msg *newmsg;
2316 struct secpolicy *sp;
2319 IPSEC_ASSERT(so != NULL, ("null socket"));
2320 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2321 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2322 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2324 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2325 return key_senderror(so, m, EINVAL);
2327 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2329 LIST_FOREACH(sp, &sptree[dir], chain)
2330 sp->state = IPSEC_SPSTATE_DEAD;
2334 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2335 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2336 return key_senderror(so, m, ENOBUFS);
2342 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2343 newmsg = mtod(m, struct sadb_msg *);
2344 newmsg->sadb_msg_errno = 0;
2345 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2347 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2351 * SADB_SPDDUMP processing
2354 * from the user, and dump all SP leaves
2359 * m will always be freed.
2362 key_spddump(so, m, mhp)
2365 const struct sadb_msghdr *mhp;
2367 struct secpolicy *sp;
2372 IPSEC_ASSERT(so != NULL, ("null socket"));
2373 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2374 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2375 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2377 /* search SPD entry and get buffer size. */
2379 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2380 LIST_FOREACH(sp, &sptree[dir], chain) {
2386 return key_senderror(so, m, ENOENT);
2388 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2389 LIST_FOREACH(sp, &sptree[dir], chain) {
2391 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2392 mhp->msg->sadb_msg_pid);
2395 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2403 static struct mbuf *
2404 key_setdumpsp(sp, type, seq, pid)
2405 struct secpolicy *sp;
2409 struct mbuf *result = NULL, *m;
2411 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2416 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2417 &sp->spidx.src.sa, sp->spidx.prefs,
2418 sp->spidx.ul_proto);
2423 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2424 &sp->spidx.dst.sa, sp->spidx.prefd,
2425 sp->spidx.ul_proto);
2435 if ((result->m_flags & M_PKTHDR) == 0)
2438 if (result->m_len < sizeof(struct sadb_msg)) {
2439 result = m_pullup(result, sizeof(struct sadb_msg));
2444 result->m_pkthdr.len = 0;
2445 for (m = result; m; m = m->m_next)
2446 result->m_pkthdr.len += m->m_len;
2448 mtod(result, struct sadb_msg *)->sadb_msg_len =
2449 PFKEY_UNIT64(result->m_pkthdr.len);
2459 * get PFKEY message length for security policy and request.
2462 key_getspreqmsglen(sp)
2463 struct secpolicy *sp;
2467 tlen = sizeof(struct sadb_x_policy);
2469 /* if is the policy for ipsec ? */
2470 if (sp->policy != IPSEC_POLICY_IPSEC)
2473 /* get length of ipsec requests */
2475 struct ipsecrequest *isr;
2478 for (isr = sp->req; isr != NULL; isr = isr->next) {
2479 len = sizeof(struct sadb_x_ipsecrequest)
2480 + isr->saidx.src.sa.sa_len
2481 + isr->saidx.dst.sa.sa_len;
2483 tlen += PFKEY_ALIGN8(len);
2491 * SADB_SPDEXPIRE processing
2493 * <base, address(SD), lifetime(CH), policy>
2497 * others : error number
2501 struct secpolicy *sp;
2503 struct mbuf *result = NULL, *m;
2506 struct sadb_lifetime *lt;
2508 /* XXX: Why do we lock ? */
2510 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2512 /* set msg header */
2513 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2520 /* create lifetime extension (current and hard) */
2521 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2522 m = key_alloc_mbuf(len);
2523 if (!m || m->m_next) { /*XXX*/
2529 bzero(mtod(m, caddr_t), len);
2530 lt = mtod(m, struct sadb_lifetime *);
2531 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2532 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2533 lt->sadb_lifetime_allocations = 0;
2534 lt->sadb_lifetime_bytes = 0;
2535 lt->sadb_lifetime_addtime = sp->created;
2536 lt->sadb_lifetime_usetime = sp->lastused;
2537 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2538 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2539 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2540 lt->sadb_lifetime_allocations = 0;
2541 lt->sadb_lifetime_bytes = 0;
2542 lt->sadb_lifetime_addtime = sp->lifetime;
2543 lt->sadb_lifetime_usetime = sp->validtime;
2546 /* set sadb_address for source */
2547 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2549 sp->spidx.prefs, sp->spidx.ul_proto);
2556 /* set sadb_address for destination */
2557 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2559 sp->spidx.prefd, sp->spidx.ul_proto);
2574 if ((result->m_flags & M_PKTHDR) == 0) {
2579 if (result->m_len < sizeof(struct sadb_msg)) {
2580 result = m_pullup(result, sizeof(struct sadb_msg));
2581 if (result == NULL) {
2587 result->m_pkthdr.len = 0;
2588 for (m = result; m; m = m->m_next)
2589 result->m_pkthdr.len += m->m_len;
2591 mtod(result, struct sadb_msg *)->sadb_msg_len =
2592 PFKEY_UNIT64(result->m_pkthdr.len);
2594 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2602 /* %%% SAD management */
2604 * allocating a memory for new SA head, and copy from the values of mhp.
2605 * OUT: NULL : failure due to the lack of memory.
2606 * others : pointer to new SA head.
2608 static struct secashead *
2610 struct secasindex *saidx;
2612 struct secashead *newsah;
2614 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
2616 newsah = malloc(sizeof(struct secashead), M_IPSEC_SAH, M_NOWAIT|M_ZERO);
2617 if (newsah != NULL) {
2619 for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++)
2620 LIST_INIT(&newsah->savtree[i]);
2621 newsah->saidx = *saidx;
2623 /* add to saidxtree */
2624 newsah->state = SADB_SASTATE_MATURE;
2627 LIST_INSERT_HEAD(&sahtree, newsah, chain);
2634 * delete SA index and all SA registerd.
2638 struct secashead *sah;
2640 struct secasvar *sav, *nextsav;
2644 IPSEC_ASSERT(sah != NULL, ("NULL sah"));
2645 SAHTREE_LOCK_ASSERT();
2647 /* searching all SA registerd in the secindex. */
2649 stateidx < _ARRAYLEN(saorder_state_any);
2651 u_int state = saorder_state_any[stateidx];
2652 LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain, nextsav) {
2653 if (sav->refcnt == 0) {
2655 KEY_CHKSASTATE(state, sav->state, __func__);
2658 /* give up to delete this sa */
2663 if (!zombie) { /* delete only if there are savs */
2664 /* remove from tree of SA index */
2665 if (__LIST_CHAINED(sah))
2666 LIST_REMOVE(sah, chain);
2667 if (sah->sa_route.ro_rt) {
2668 RTFREE(sah->sa_route.ro_rt);
2669 sah->sa_route.ro_rt = (struct rtentry *)NULL;
2671 free(sah, M_IPSEC_SAH);
2676 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2677 * and copy the values of mhp into new buffer.
2678 * When SAD message type is GETSPI:
2679 * to set sequence number from acq_seq++,
2680 * to set zero to SPI.
2681 * not to call key_setsava().
2683 * others : pointer to new secasvar.
2685 * does not modify mbuf. does not free mbuf on error.
2687 static struct secasvar *
2688 key_newsav(m, mhp, sah, errp, where, tag)
2690 const struct sadb_msghdr *mhp;
2691 struct secashead *sah;
2696 struct secasvar *newsav;
2697 const struct sadb_sa *xsa;
2699 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2700 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2701 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2702 IPSEC_ASSERT(sah != NULL, ("null secashead"));
2704 newsav = malloc(sizeof(struct secasvar), M_IPSEC_SA, M_NOWAIT|M_ZERO);
2705 if (newsav == NULL) {
2706 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2711 switch (mhp->msg->sadb_msg_type) {
2715 #ifdef IPSEC_DOSEQCHECK
2716 /* sync sequence number */
2717 if (mhp->msg->sadb_msg_seq == 0)
2719 (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
2722 newsav->seq = mhp->msg->sadb_msg_seq;
2727 if (mhp->ext[SADB_EXT_SA] == NULL) {
2728 free(newsav, M_IPSEC_SA);
2730 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2735 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2736 newsav->spi = xsa->sadb_sa_spi;
2737 newsav->seq = mhp->msg->sadb_msg_seq;
2740 free(newsav, M_IPSEC_SA);
2747 /* copy sav values */
2748 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2749 *errp = key_setsaval(newsav, m, mhp);
2751 free(newsav, M_IPSEC_SA);
2757 SECASVAR_LOCK_INIT(newsav);
2760 newsav->created = time_second;
2761 newsav->pid = mhp->msg->sadb_msg_pid;
2766 newsav->state = SADB_SASTATE_LARVAL;
2768 /* XXX locking??? */
2769 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2772 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
2773 printf("DP %s from %s:%u return SP:%p\n", __func__,
2774 where, tag, newsav));
2780 * free() SA variable entry.
2783 key_cleansav(struct secasvar *sav)
2786 * Cleanup xform state. Note that zeroize'ing causes the
2787 * keys to be cleared; otherwise we must do it ourself.
2789 if (sav->tdb_xform != NULL) {
2790 sav->tdb_xform->xf_zeroize(sav);
2791 sav->tdb_xform = NULL;
2793 KASSERT(sav->iv == NULL, ("iv but no xform"));
2794 if (sav->key_auth != NULL)
2795 bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
2796 if (sav->key_enc != NULL)
2797 bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
2799 if (sav->key_auth != NULL) {
2800 free(sav->key_auth, M_IPSEC_MISC);
2801 sav->key_auth = NULL;
2803 if (sav->key_enc != NULL) {
2804 free(sav->key_enc, M_IPSEC_MISC);
2805 sav->key_enc = NULL;
2808 bzero(sav->sched, sav->schedlen);
2809 free(sav->sched, M_IPSEC_MISC);
2812 if (sav->replay != NULL) {
2813 free(sav->replay, M_IPSEC_MISC);
2816 if (sav->lft_c != NULL) {
2817 free(sav->lft_c, M_IPSEC_MISC);
2820 if (sav->lft_h != NULL) {
2821 free(sav->lft_h, M_IPSEC_MISC);
2824 if (sav->lft_s != NULL) {
2825 free(sav->lft_s, M_IPSEC_MISC);
2831 * free() SA variable entry.
2835 struct secasvar *sav;
2837 IPSEC_ASSERT(sav != NULL, ("null sav"));
2838 IPSEC_ASSERT(sav->refcnt == 0, ("reference count %u > 0", sav->refcnt));
2840 /* remove from SA header */
2841 if (__LIST_CHAINED(sav))
2842 LIST_REMOVE(sav, chain);
2844 SECASVAR_LOCK_DESTROY(sav);
2845 free(sav, M_IPSEC_SA);
2852 * others : found, pointer to a SA.
2854 static struct secashead *
2856 struct secasindex *saidx;
2858 struct secashead *sah;
2861 LIST_FOREACH(sah, &sahtree, chain) {
2862 if (sah->state == SADB_SASTATE_DEAD)
2864 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2873 * check not to be duplicated SPI.
2874 * NOTE: this function is too slow due to searching all SAD.
2877 * others : found, pointer to a SA.
2879 static struct secasvar *
2880 key_checkspidup(saidx, spi)
2881 struct secasindex *saidx;
2884 struct secashead *sah;
2885 struct secasvar *sav;
2887 /* check address family */
2888 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
2889 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
2897 LIST_FOREACH(sah, &sahtree, chain) {
2898 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
2900 sav = key_getsavbyspi(sah, spi);
2910 * search SAD litmited alive SA, protocol, SPI.
2913 * others : found, pointer to a SA.
2915 static struct secasvar *
2916 key_getsavbyspi(sah, spi)
2917 struct secashead *sah;
2920 struct secasvar *sav;
2921 u_int stateidx, state;
2924 SAHTREE_LOCK_ASSERT();
2925 /* search all status */
2927 stateidx < _ARRAYLEN(saorder_state_alive);
2930 state = saorder_state_alive[stateidx];
2931 LIST_FOREACH(sav, &sah->savtree[state], chain) {
2934 if (sav->state != state) {
2935 ipseclog((LOG_DEBUG, "%s: "
2936 "invalid sav->state (queue: %d SA: %d)\n",
2937 __func__, state, sav->state));
2941 if (sav->spi == spi)
2950 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
2951 * You must update these if need.
2955 * does not modify mbuf. does not free mbuf on error.
2958 key_setsaval(sav, m, mhp)
2959 struct secasvar *sav;
2961 const struct sadb_msghdr *mhp;
2965 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2966 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2967 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2969 /* initialization */
2971 sav->key_auth = NULL;
2972 sav->key_enc = NULL;
2979 sav->tdb_xform = NULL; /* transform */
2980 sav->tdb_encalgxform = NULL; /* encoding algorithm */
2981 sav->tdb_authalgxform = NULL; /* authentication algorithm */
2982 sav->tdb_compalgxform = NULL; /* compression algorithm */
2985 if (mhp->ext[SADB_EXT_SA] != NULL) {
2986 const struct sadb_sa *sa0;
2988 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2989 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
2994 sav->alg_auth = sa0->sadb_sa_auth;
2995 sav->alg_enc = sa0->sadb_sa_encrypt;
2996 sav->flags = sa0->sadb_sa_flags;
2999 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
3000 sav->replay = (struct secreplay *)
3001 malloc(sizeof(struct secreplay)+sa0->sadb_sa_replay, M_IPSEC_MISC, M_NOWAIT|M_ZERO);
3002 if (sav->replay == NULL) {
3003 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3008 if (sa0->sadb_sa_replay != 0)
3009 sav->replay->bitmap = (caddr_t)(sav->replay+1);
3010 sav->replay->wsize = sa0->sadb_sa_replay;
3014 /* Authentication keys */
3015 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
3016 const struct sadb_key *key0;
3019 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
3020 len = mhp->extlen[SADB_EXT_KEY_AUTH];
3023 if (len < sizeof(*key0)) {
3027 switch (mhp->msg->sadb_msg_satype) {
3028 case SADB_SATYPE_AH:
3029 case SADB_SATYPE_ESP:
3030 case SADB_X_SATYPE_TCPSIGNATURE:
3031 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3032 sav->alg_auth != SADB_X_AALG_NULL)
3035 case SADB_X_SATYPE_IPCOMP:
3041 ipseclog((LOG_DEBUG, "%s: invalid key_auth values.\n",
3046 sav->key_auth = key_dup(key0, len, M_IPSEC_MISC);
3047 if (sav->key_auth == NULL) {
3048 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3054 /* Encryption key */
3055 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3056 const struct sadb_key *key0;
3059 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3060 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3063 if (len < sizeof(*key0)) {
3067 switch (mhp->msg->sadb_msg_satype) {
3068 case SADB_SATYPE_ESP:
3069 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3070 sav->alg_enc != SADB_EALG_NULL) {
3074 sav->key_enc = key_dup(key0, len, M_IPSEC_MISC);
3075 if (sav->key_enc == NULL) {
3076 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3082 case SADB_X_SATYPE_IPCOMP:
3083 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3085 sav->key_enc = NULL; /*just in case*/
3087 case SADB_SATYPE_AH:
3088 case SADB_X_SATYPE_TCPSIGNATURE:
3094 ipseclog((LOG_DEBUG, "%s: invalid key_enc value.\n",
3103 switch (mhp->msg->sadb_msg_satype) {
3104 case SADB_SATYPE_AH:
3105 error = xform_init(sav, XF_AH);
3107 case SADB_SATYPE_ESP:
3108 error = xform_init(sav, XF_ESP);
3110 case SADB_X_SATYPE_IPCOMP:
3111 error = xform_init(sav, XF_IPCOMP);
3113 case SADB_X_SATYPE_TCPSIGNATURE:
3114 error = xform_init(sav, XF_TCPSIGNATURE);
3118 ipseclog((LOG_DEBUG, "%s: unable to initialize SA type %u.\n",
3119 __func__, mhp->msg->sadb_msg_satype));
3124 sav->created = time_second;
3126 /* make lifetime for CURRENT */
3127 sav->lft_c = malloc(sizeof(struct sadb_lifetime), M_IPSEC_MISC, M_NOWAIT);
3128 if (sav->lft_c == NULL) {
3129 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3134 sav->lft_c->sadb_lifetime_len =
3135 PFKEY_UNIT64(sizeof(struct sadb_lifetime));
3136 sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
3137 sav->lft_c->sadb_lifetime_allocations = 0;
3138 sav->lft_c->sadb_lifetime_bytes = 0;
3139 sav->lft_c->sadb_lifetime_addtime = time_second;
3140 sav->lft_c->sadb_lifetime_usetime = 0;
3142 /* lifetimes for HARD and SOFT */
3144 const struct sadb_lifetime *lft0;
3146 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3148 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3152 sav->lft_h = key_dup(lft0, sizeof(*lft0), M_IPSEC_MISC);
3153 if (sav->lft_h == NULL) {
3154 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3158 /* to be initialize ? */
3161 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3163 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3167 sav->lft_s = key_dup(lft0, sizeof(*lft0), M_IPSEC_MISC);
3168 if (sav->lft_s == NULL) {
3169 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3173 /* to be initialize ? */
3180 /* initialization */
3187 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3192 key_mature(struct secasvar *sav)
3196 /* check SPI value */
3197 switch (sav->sah->saidx.proto) {
3201 * RFC 4302, 2.4. Security Parameters Index (SPI), SPI values
3202 * 1-255 reserved by IANA for future use,
3203 * 0 for implementation specific, local use.
3205 if (ntohl(sav->spi) <= 255) {
3206 ipseclog((LOG_DEBUG, "%s: illegal range of SPI %u.\n",
3207 __func__, (u_int32_t)ntohl(sav->spi)));
3214 switch (sav->sah->saidx.proto) {
3217 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3218 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3219 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3220 "given to old-esp.\n", __func__));
3223 error = xform_init(sav, XF_ESP);
3227 if (sav->flags & SADB_X_EXT_DERIV) {
3228 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3229 "given to AH SA.\n", __func__));
3232 if (sav->alg_enc != SADB_EALG_NONE) {
3233 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3234 "mismated.\n", __func__));
3237 error = xform_init(sav, XF_AH);
3239 case IPPROTO_IPCOMP:
3240 if (sav->alg_auth != SADB_AALG_NONE) {
3241 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3242 "mismated.\n", __func__));
3245 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3246 && ntohl(sav->spi) >= 0x10000) {
3247 ipseclog((LOG_DEBUG, "%s: invalid cpi for IPComp.\n",
3251 error = xform_init(sav, XF_IPCOMP);
3254 if (sav->alg_enc != SADB_EALG_NONE) {
3255 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3256 "mismated.\n", __func__));
3259 error = xform_init(sav, XF_TCPSIGNATURE);
3262 ipseclog((LOG_DEBUG, "%s: Invalid satype.\n", __func__));
3263 error = EPROTONOSUPPORT;
3268 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3275 * subroutine for SADB_GET and SADB_DUMP.
3277 static struct mbuf *
3278 key_setdumpsa(sav, type, satype, seq, pid)
3279 struct secasvar *sav;
3280 u_int8_t type, satype;
3283 struct mbuf *result = NULL, *tres = NULL, *m;
3288 SADB_EXT_SA, SADB_X_EXT_SA2,
3289 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3290 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3291 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3292 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3293 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3296 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3301 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3304 switch (dumporder[i]) {
3306 m = key_setsadbsa(sav);
3311 case SADB_X_EXT_SA2:
3312 m = key_setsadbxsa2(sav->sah->saidx.mode,
3313 sav->replay ? sav->replay->count : 0,
3314 sav->sah->saidx.reqid);
3319 case SADB_EXT_ADDRESS_SRC:
3320 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3321 &sav->sah->saidx.src.sa,
3322 FULLMASK, IPSEC_ULPROTO_ANY);
3327 case SADB_EXT_ADDRESS_DST:
3328 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3329 &sav->sah->saidx.dst.sa,
3330 FULLMASK, IPSEC_ULPROTO_ANY);
3335 case SADB_EXT_KEY_AUTH:
3338 l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len);
3342 case SADB_EXT_KEY_ENCRYPT:
3345 l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len);
3349 case SADB_EXT_LIFETIME_CURRENT:
3352 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len);
3356 case SADB_EXT_LIFETIME_HARD:
3359 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len);
3363 case SADB_EXT_LIFETIME_SOFT:
3366 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len);
3370 case SADB_EXT_ADDRESS_PROXY:
3371 case SADB_EXT_IDENTITY_SRC:
3372 case SADB_EXT_IDENTITY_DST:
3373 /* XXX: should we brought from SPD ? */
3374 case SADB_EXT_SENSITIVITY:
3379 if ((!m && !p) || (m && p))
3382 M_PREPEND(tres, l, M_DONTWAIT);
3385 bcopy(p, mtod(tres, caddr_t), l);
3389 m = key_alloc_mbuf(l);
3392 m_copyback(m, 0, l, p);
3400 m_cat(result, tres);
3402 if (result->m_len < sizeof(struct sadb_msg)) {
3403 result = m_pullup(result, sizeof(struct sadb_msg));
3408 result->m_pkthdr.len = 0;
3409 for (m = result; m; m = m->m_next)
3410 result->m_pkthdr.len += m->m_len;
3412 mtod(result, struct sadb_msg *)->sadb_msg_len =
3413 PFKEY_UNIT64(result->m_pkthdr.len);
3424 * set data into sadb_msg.
3426 static struct mbuf *
3427 key_setsadbmsg(type, tlen, satype, seq, pid, reserved)
3428 u_int8_t type, satype;
3438 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3441 MGETHDR(m, M_DONTWAIT, MT_DATA);
3442 if (m && len > MHLEN) {
3443 MCLGET(m, M_DONTWAIT);
3444 if ((m->m_flags & M_EXT) == 0) {
3451 m->m_pkthdr.len = m->m_len = len;
3454 p = mtod(m, struct sadb_msg *);
3457 p->sadb_msg_version = PF_KEY_V2;
3458 p->sadb_msg_type = type;
3459 p->sadb_msg_errno = 0;
3460 p->sadb_msg_satype = satype;
3461 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3462 p->sadb_msg_reserved = reserved;
3463 p->sadb_msg_seq = seq;
3464 p->sadb_msg_pid = (u_int32_t)pid;
3470 * copy secasvar data into sadb_address.
3472 static struct mbuf *
3474 struct secasvar *sav;
3480 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3481 m = key_alloc_mbuf(len);
3482 if (!m || m->m_next) { /*XXX*/
3488 p = mtod(m, struct sadb_sa *);
3491 p->sadb_sa_len = PFKEY_UNIT64(len);
3492 p->sadb_sa_exttype = SADB_EXT_SA;
3493 p->sadb_sa_spi = sav->spi;
3494 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3495 p->sadb_sa_state = sav->state;
3496 p->sadb_sa_auth = sav->alg_auth;
3497 p->sadb_sa_encrypt = sav->alg_enc;
3498 p->sadb_sa_flags = sav->flags;
3504 * set data into sadb_address.
3506 static struct mbuf *
3507 key_setsadbaddr(exttype, saddr, prefixlen, ul_proto)
3509 const struct sockaddr *saddr;
3514 struct sadb_address *p;
3517 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3518 PFKEY_ALIGN8(saddr->sa_len);
3519 m = key_alloc_mbuf(len);
3520 if (!m || m->m_next) { /*XXX*/
3526 p = mtod(m, struct sadb_address *);
3529 p->sadb_address_len = PFKEY_UNIT64(len);
3530 p->sadb_address_exttype = exttype;
3531 p->sadb_address_proto = ul_proto;
3532 if (prefixlen == FULLMASK) {
3533 switch (saddr->sa_family) {
3535 prefixlen = sizeof(struct in_addr) << 3;
3538 prefixlen = sizeof(struct in6_addr) << 3;
3544 p->sadb_address_prefixlen = prefixlen;
3545 p->sadb_address_reserved = 0;
3548 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3555 * set data into sadb_x_sa2.
3557 static struct mbuf *
3558 key_setsadbxsa2(mode, seq, reqid)
3560 u_int32_t seq, reqid;
3563 struct sadb_x_sa2 *p;
3566 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3567 m = key_alloc_mbuf(len);
3568 if (!m || m->m_next) { /*XXX*/
3574 p = mtod(m, struct sadb_x_sa2 *);
3577 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3578 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3579 p->sadb_x_sa2_mode = mode;
3580 p->sadb_x_sa2_reserved1 = 0;
3581 p->sadb_x_sa2_reserved2 = 0;
3582 p->sadb_x_sa2_sequence = seq;
3583 p->sadb_x_sa2_reqid = reqid;
3589 * set data into sadb_x_policy
3591 static struct mbuf *
3592 key_setsadbxpolicy(type, dir, id)
3598 struct sadb_x_policy *p;
3601 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3602 m = key_alloc_mbuf(len);
3603 if (!m || m->m_next) { /*XXX*/
3609 p = mtod(m, struct sadb_x_policy *);
3612 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3613 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3614 p->sadb_x_policy_type = type;
3615 p->sadb_x_policy_dir = dir;
3616 p->sadb_x_policy_id = id;
3623 * copy a buffer into the new buffer allocated.
3626 key_dup(const void *src, u_int len, struct malloc_type *type)
3630 copy = malloc(len, type, M_NOWAIT);
3633 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3635 bcopy(src, copy, len);
3639 /* compare my own address
3640 * OUT: 1: true, i.e. my address.
3645 struct sockaddr *sa;
3648 struct sockaddr_in *sin;
3649 struct in_ifaddr *ia;
3652 IPSEC_ASSERT(sa != NULL, ("null sockaddr"));
3654 switch (sa->sa_family) {
3657 sin = (struct sockaddr_in *)sa;
3658 for (ia = in_ifaddrhead.tqh_first; ia;
3659 ia = ia->ia_link.tqe_next)
3661 if (sin->sin_family == ia->ia_addr.sin_family &&
3662 sin->sin_len == ia->ia_addr.sin_len &&
3663 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3672 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3681 * compare my own address for IPv6.
3684 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3686 #include <netinet6/in6_var.h>
3690 struct sockaddr_in6 *sin6;
3692 struct in6_ifaddr *ia;
3693 struct in6_multi *in6m;
3695 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
3696 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3697 (struct sockaddr *)&ia->ia_addr, 0) == 0)
3702 * XXX why do we care about multlicast here while we don't care
3703 * about IPv4 multicast??
3707 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3712 /* loopback, just for safety */
3713 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
3721 * compare two secasindex structure.
3722 * flag can specify to compare 2 saidxes.
3723 * compare two secasindex structure without both mode and reqid.
3724 * don't compare port.
3726 * saidx0: source, it can be in SAD.
3734 const struct secasindex *saidx0,
3735 const struct secasindex *saidx1,
3739 if (saidx0 == NULL && saidx1 == NULL)
3742 if (saidx0 == NULL || saidx1 == NULL)
3745 if (saidx0->proto != saidx1->proto)
3748 if (flag == CMP_EXACTLY) {
3749 if (saidx0->mode != saidx1->mode)
3751 if (saidx0->reqid != saidx1->reqid)
3753 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
3754 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
3758 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
3759 if (flag == CMP_MODE_REQID
3760 ||flag == CMP_REQID) {
3762 * If reqid of SPD is non-zero, unique SA is required.
3763 * The result must be of same reqid in this case.
3765 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
3769 if (flag == CMP_MODE_REQID) {
3770 if (saidx0->mode != IPSEC_MODE_ANY
3771 && saidx0->mode != saidx1->mode)
3775 if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, 0) != 0) {
3778 if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, 0) != 0) {
3787 * compare two secindex structure exactly.
3789 * spidx0: source, it is often in SPD.
3790 * spidx1: object, it is often from PFKEY message.
3796 key_cmpspidx_exactly(
3797 struct secpolicyindex *spidx0,
3798 struct secpolicyindex *spidx1)
3801 if (spidx0 == NULL && spidx1 == NULL)
3804 if (spidx0 == NULL || spidx1 == NULL)
3807 if (spidx0->prefs != spidx1->prefs
3808 || spidx0->prefd != spidx1->prefd
3809 || spidx0->ul_proto != spidx1->ul_proto)
3812 return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
3813 key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
3817 * compare two secindex structure with mask.
3819 * spidx0: source, it is often in SPD.
3820 * spidx1: object, it is often from IP header.
3826 key_cmpspidx_withmask(
3827 struct secpolicyindex *spidx0,
3828 struct secpolicyindex *spidx1)
3831 if (spidx0 == NULL && spidx1 == NULL)
3834 if (spidx0 == NULL || spidx1 == NULL)
3837 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
3838 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
3839 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
3840 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
3843 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
3844 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
3845 && spidx0->ul_proto != spidx1->ul_proto)
3848 switch (spidx0->src.sa.sa_family) {
3850 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
3851 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
3853 if (!key_bbcmp(&spidx0->src.sin.sin_addr,
3854 &spidx1->src.sin.sin_addr, spidx0->prefs))
3858 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
3859 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
3862 * scope_id check. if sin6_scope_id is 0, we regard it
3863 * as a wildcard scope, which matches any scope zone ID.
3865 if (spidx0->src.sin6.sin6_scope_id &&
3866 spidx1->src.sin6.sin6_scope_id &&
3867 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
3869 if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
3870 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
3875 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
3880 switch (spidx0->dst.sa.sa_family) {
3882 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
3883 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
3885 if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
3886 &spidx1->dst.sin.sin_addr, spidx0->prefd))
3890 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
3891 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
3894 * scope_id check. if sin6_scope_id is 0, we regard it
3895 * as a wildcard scope, which matches any scope zone ID.
3897 if (spidx0->dst.sin6.sin6_scope_id &&
3898 spidx1->dst.sin6.sin6_scope_id &&
3899 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
3901 if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
3902 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
3907 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
3912 /* XXX Do we check other field ? e.g. flowinfo */
3917 /* returns 0 on match */
3920 const struct sockaddr *sa1,
3921 const struct sockaddr *sa2,
3927 #define satosin(s) ((const struct sockaddr_in *)s)
3931 #define satosin6(s) ((const struct sockaddr_in6 *)s)
3932 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
3935 switch (sa1->sa_family) {
3937 if (sa1->sa_len != sizeof(struct sockaddr_in))
3939 if (satosin(sa1)->sin_addr.s_addr !=
3940 satosin(sa2)->sin_addr.s_addr) {
3943 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
3947 if (sa1->sa_len != sizeof(struct sockaddr_in6))
3948 return 1; /*EINVAL*/
3949 if (satosin6(sa1)->sin6_scope_id !=
3950 satosin6(sa2)->sin6_scope_id) {
3953 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
3954 &satosin6(sa2)->sin6_addr)) {
3958 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
3963 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
3974 * compare two buffers with mask.
3978 * bits: Number of bits to compare
3984 key_bbcmp(const void *a1, const void *a2, u_int bits)
3986 const unsigned char *p1 = a1;
3987 const unsigned char *p2 = a2;
3989 /* XXX: This could be considerably faster if we compare a word
3990 * at a time, but it is complicated on LSB Endian machines */
3992 /* Handle null pointers */
3993 if (p1 == NULL || p2 == NULL)
4003 u_int8_t mask = ~((1<<(8-bits))-1);
4004 if ((*p1 & mask) != (*p2 & mask))
4007 return 1; /* Match! */
4011 key_flush_spd(time_t now)
4013 static u_int16_t sptree_scangen = 0;
4014 u_int16_t gen = sptree_scangen++;
4015 struct secpolicy *sp;
4019 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4022 LIST_FOREACH(sp, &sptree[dir], chain) {
4023 if (sp->scangen == gen) /* previously handled */
4026 if (sp->state == IPSEC_SPSTATE_DEAD) {
4027 /* NB: clean entries created by key_spdflush */
4032 if (sp->lifetime == 0 && sp->validtime == 0)
4034 if ((sp->lifetime && now - sp->created > sp->lifetime)
4035 || (sp->validtime && now - sp->lastused > sp->validtime)) {
4036 sp->state = IPSEC_SPSTATE_DEAD;
4048 key_flush_sad(time_t now)
4050 struct secashead *sah, *nextsah;
4051 struct secasvar *sav, *nextsav;
4055 LIST_FOREACH_SAFE(sah, &sahtree, chain, nextsah) {
4056 /* if sah has been dead, then delete it and process next sah. */
4057 if (sah->state == SADB_SASTATE_DEAD) {
4062 /* if LARVAL entry doesn't become MATURE, delete it. */
4063 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_LARVAL], chain, nextsav) {
4064 if (now - sav->created > key_larval_lifetime)
4069 * check MATURE entry to start to send expire message
4072 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_MATURE], chain, nextsav) {
4073 /* we don't need to check. */
4074 if (sav->lft_s == NULL)
4078 if (sav->lft_c == NULL) {
4079 ipseclog((LOG_DEBUG,"%s: there is no CURRENT "
4080 "time, why?\n", __func__));
4084 /* check SOFT lifetime */
4085 if (sav->lft_s->sadb_lifetime_addtime != 0 &&
4086 now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4088 * check SA to be used whether or not.
4089 * when SA hasn't been used, delete it.
4091 if (sav->lft_c->sadb_lifetime_usetime == 0) {
4092 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4095 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4097 * XXX If we keep to send expire
4098 * message in the status of
4099 * DYING. Do remove below code.
4104 /* check SOFT lifetime by bytes */
4106 * XXX I don't know the way to delete this SA
4107 * when new SA is installed. Caution when it's
4108 * installed too big lifetime by time.
4110 else if (sav->lft_s->sadb_lifetime_bytes != 0 &&
4111 sav->lft_s->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4113 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4115 * XXX If we keep to send expire
4116 * message in the status of
4117 * DYING. Do remove below code.
4123 /* check DYING entry to change status to DEAD. */
4124 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DYING], chain, nextsav) {
4125 /* we don't need to check. */
4126 if (sav->lft_h == NULL)
4130 if (sav->lft_c == NULL) {
4131 ipseclog((LOG_DEBUG, "%s: there is no CURRENT "
4132 "time, why?\n", __func__));
4136 if (sav->lft_h->sadb_lifetime_addtime != 0 &&
4137 now - sav->created > sav->lft_h->sadb_lifetime_addtime) {
4138 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4141 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4142 else if (sav->lft_s != NULL
4143 && sav->lft_s->sadb_lifetime_addtime != 0
4144 && now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4146 * XXX: should be checked to be
4147 * installed the valid SA.
4151 * If there is no SA then sending
4157 /* check HARD lifetime by bytes */
4158 else if (sav->lft_h->sadb_lifetime_bytes != 0 &&
4159 sav->lft_h->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4160 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4165 /* delete entry in DEAD */
4166 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DEAD], chain, nextsav) {
4168 if (sav->state != SADB_SASTATE_DEAD) {
4169 ipseclog((LOG_DEBUG, "%s: invalid sav->state "
4170 "(queue: %d SA: %d): kill it anyway\n",
4172 SADB_SASTATE_DEAD, sav->state));
4175 * do not call key_freesav() here.
4176 * sav should already be freed, and sav->refcnt
4177 * shows other references to sav
4178 * (such as from SPD).
4186 key_flush_acq(time_t now)
4188 struct secacq *acq, *nextacq;
4192 for (acq = LIST_FIRST(&acqtree); acq != NULL; acq = nextacq) {
4193 nextacq = LIST_NEXT(acq, chain);
4194 if (now - acq->created > key_blockacq_lifetime
4195 && __LIST_CHAINED(acq)) {
4196 LIST_REMOVE(acq, chain);
4197 free(acq, M_IPSEC_SAQ);
4204 key_flush_spacq(time_t now)
4206 struct secspacq *acq, *nextacq;
4210 for (acq = LIST_FIRST(&spacqtree); acq != NULL; acq = nextacq) {
4211 nextacq = LIST_NEXT(acq, chain);
4212 if (now - acq->created > key_blockacq_lifetime
4213 && __LIST_CHAINED(acq)) {
4214 LIST_REMOVE(acq, chain);
4215 free(acq, M_IPSEC_SAQ);
4223 * scanning SPD and SAD to check status for each entries,
4224 * and do to remove or to expire.
4225 * XXX: year 2038 problem may remain.
4228 key_timehandler(void)
4230 time_t now = time_second;
4235 key_flush_spacq(now);
4237 #ifndef IPSEC_DEBUG2
4238 /* do exchange to tick time !! */
4239 (void)timeout((void *)key_timehandler, (void *)0, hz);
4240 #endif /* IPSEC_DEBUG2 */
4248 key_randomfill(&value, sizeof(value));
4253 key_randomfill(p, l)
4259 static int warn = 1;
4262 n = (size_t)read_random(p, (u_int)l);
4266 bcopy(&v, (u_int8_t *)p + n,
4267 l - n < sizeof(v) ? l - n : sizeof(v));
4271 printf("WARNING: pseudo-random number generator "
4272 "used for IPsec processing\n");
4279 * map SADB_SATYPE_* to IPPROTO_*.
4280 * if satype == SADB_SATYPE then satype is mapped to ~0.
4282 * 0: invalid satype.
4285 key_satype2proto(satype)
4289 case SADB_SATYPE_UNSPEC:
4290 return IPSEC_PROTO_ANY;
4291 case SADB_SATYPE_AH:
4293 case SADB_SATYPE_ESP:
4295 case SADB_X_SATYPE_IPCOMP:
4296 return IPPROTO_IPCOMP;
4297 case SADB_X_SATYPE_TCPSIGNATURE:
4306 * map IPPROTO_* to SADB_SATYPE_*
4308 * 0: invalid protocol type.
4311 key_proto2satype(proto)
4316 return SADB_SATYPE_AH;
4318 return SADB_SATYPE_ESP;
4319 case IPPROTO_IPCOMP:
4320 return SADB_X_SATYPE_IPCOMP;
4322 return SADB_X_SATYPE_TCPSIGNATURE;
4331 * SADB_GETSPI processing is to receive
4332 * <base, (SA2), src address, dst address, (SPI range)>
4333 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4334 * tree with the status of LARVAL, and send
4335 * <base, SA(*), address(SD)>
4338 * IN: mhp: pointer to the pointer to each header.
4339 * OUT: NULL if fail.
4340 * other if success, return pointer to the message to send.
4343 key_getspi(so, m, mhp)
4346 const struct sadb_msghdr *mhp;
4348 struct sadb_address *src0, *dst0;
4349 struct secasindex saidx;
4350 struct secashead *newsah;
4351 struct secasvar *newsav;
4358 IPSEC_ASSERT(so != NULL, ("null socket"));
4359 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4360 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4361 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4363 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4364 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4365 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4367 return key_senderror(so, m, EINVAL);
4369 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4370 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4371 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4373 return key_senderror(so, m, EINVAL);
4375 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4376 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4377 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4379 mode = IPSEC_MODE_ANY;
4383 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4384 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4386 /* map satype to proto */
4387 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4388 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4390 return key_senderror(so, m, EINVAL);
4393 /* make sure if port number is zero. */
4394 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4396 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4397 sizeof(struct sockaddr_in))
4398 return key_senderror(so, m, EINVAL);
4399 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4402 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4403 sizeof(struct sockaddr_in6))
4404 return key_senderror(so, m, EINVAL);
4405 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4410 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4412 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4413 sizeof(struct sockaddr_in))
4414 return key_senderror(so, m, EINVAL);
4415 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4418 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4419 sizeof(struct sockaddr_in6))
4420 return key_senderror(so, m, EINVAL);
4421 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4427 /* XXX boundary check against sa_len */
4428 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4430 /* SPI allocation */
4431 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4434 return key_senderror(so, m, EINVAL);
4436 /* get a SA index */
4437 if ((newsah = key_getsah(&saidx)) == NULL) {
4438 /* create a new SA index */
4439 if ((newsah = key_newsah(&saidx)) == NULL) {
4440 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
4441 return key_senderror(so, m, ENOBUFS);
4447 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4448 if (newsav == NULL) {
4449 /* XXX don't free new SA index allocated in above. */
4450 return key_senderror(so, m, error);
4454 newsav->spi = htonl(spi);
4456 /* delete the entry in acqtree */
4457 if (mhp->msg->sadb_msg_seq != 0) {
4459 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4460 /* reset counter in order to deletion by timehandler. */
4461 acq->created = time_second;
4467 struct mbuf *n, *nn;
4468 struct sadb_sa *m_sa;
4469 struct sadb_msg *newmsg;
4472 /* create new sadb_msg to reply. */
4473 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4474 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4476 return key_senderror(so, m, ENOBUFS);
4478 MGETHDR(n, M_DONTWAIT, MT_DATA);
4480 MCLGET(n, M_DONTWAIT);
4481 if ((n->m_flags & M_EXT) == 0) {
4487 return key_senderror(so, m, ENOBUFS);
4493 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4494 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4496 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4497 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4498 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4499 m_sa->sadb_sa_spi = htonl(spi);
4500 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4502 IPSEC_ASSERT(off == len,
4503 ("length inconsistency (off %u len %u)", off, len));
4505 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4506 SADB_EXT_ADDRESS_DST);
4509 return key_senderror(so, m, ENOBUFS);
4512 if (n->m_len < sizeof(struct sadb_msg)) {
4513 n = m_pullup(n, sizeof(struct sadb_msg));
4515 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4518 n->m_pkthdr.len = 0;
4519 for (nn = n; nn; nn = nn->m_next)
4520 n->m_pkthdr.len += nn->m_len;
4522 newmsg = mtod(n, struct sadb_msg *);
4523 newmsg->sadb_msg_seq = newsav->seq;
4524 newmsg->sadb_msg_errno = 0;
4525 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4528 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4533 * allocating new SPI
4534 * called by key_getspi().
4540 key_do_getnewspi(spirange, saidx)
4541 struct sadb_spirange *spirange;
4542 struct secasindex *saidx;
4546 int count = key_spi_trycnt;
4548 /* set spi range to allocate */
4549 if (spirange != NULL) {
4550 min = spirange->sadb_spirange_min;
4551 max = spirange->sadb_spirange_max;
4553 min = key_spi_minval;
4554 max = key_spi_maxval;
4556 /* IPCOMP needs 2-byte SPI */
4557 if (saidx->proto == IPPROTO_IPCOMP) {
4564 t = min; min = max; max = t;
4569 if (key_checkspidup(saidx, min) != NULL) {
4570 ipseclog((LOG_DEBUG, "%s: SPI %u exists already.\n",
4575 count--; /* taking one cost. */
4583 /* when requesting to allocate spi ranged */
4585 /* generate pseudo-random SPI value ranged. */
4586 newspi = min + (key_random() % (max - min + 1));
4588 if (key_checkspidup(saidx, newspi) == NULL)
4592 if (count == 0 || newspi == 0) {
4593 ipseclog((LOG_DEBUG, "%s: to allocate spi is failed.\n",
4600 keystat.getspi_count =
4601 (keystat.getspi_count + key_spi_trycnt - count) / 2;
4607 * SADB_UPDATE processing
4609 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4610 * key(AE), (identity(SD),) (sensitivity)>
4611 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4613 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4614 * (identity(SD),) (sensitivity)>
4617 * m will always be freed.
4620 key_update(so, m, mhp)
4623 const struct sadb_msghdr *mhp;
4625 struct sadb_sa *sa0;
4626 struct sadb_address *src0, *dst0;
4627 struct secasindex saidx;
4628 struct secashead *sah;
4629 struct secasvar *sav;
4635 IPSEC_ASSERT(so != NULL, ("null socket"));
4636 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4637 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4638 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4640 /* map satype to proto */
4641 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4642 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4644 return key_senderror(so, m, EINVAL);
4647 if (mhp->ext[SADB_EXT_SA] == NULL ||
4648 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4649 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4650 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4651 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4652 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4653 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4654 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4655 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4656 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4657 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4658 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4660 return key_senderror(so, m, EINVAL);
4662 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4663 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4664 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4665 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4667 return key_senderror(so, m, EINVAL);
4669 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4670 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4671 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4673 mode = IPSEC_MODE_ANY;
4676 /* XXX boundary checking for other extensions */
4678 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4679 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4680 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4682 /* XXX boundary check against sa_len */
4683 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4685 /* get a SA header */
4686 if ((sah = key_getsah(&saidx)) == NULL) {
4687 ipseclog((LOG_DEBUG, "%s: no SA index found.\n", __func__));
4688 return key_senderror(so, m, ENOENT);
4691 /* set spidx if there */
4693 error = key_setident(sah, m, mhp);
4695 return key_senderror(so, m, error);
4697 /* find a SA with sequence number. */
4698 #ifdef IPSEC_DOSEQCHECK
4699 if (mhp->msg->sadb_msg_seq != 0
4700 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
4701 ipseclog((LOG_DEBUG, "%s: no larval SA with sequence %u "
4702 "exists.\n", __func__, mhp->msg->sadb_msg_seq));
4703 return key_senderror(so, m, ENOENT);
4707 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
4710 ipseclog((LOG_DEBUG, "%s: no such a SA found (spi:%u)\n",
4711 __func__, (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4712 return key_senderror(so, m, EINVAL);
4716 /* validity check */
4717 if (sav->sah->saidx.proto != proto) {
4718 ipseclog((LOG_DEBUG, "%s: protocol mismatched "
4719 "(DB=%u param=%u)\n", __func__,
4720 sav->sah->saidx.proto, proto));
4721 return key_senderror(so, m, EINVAL);
4723 #ifdef IPSEC_DOSEQCHECK
4724 if (sav->spi != sa0->sadb_sa_spi) {
4725 ipseclog((LOG_DEBUG, "%s: SPI mismatched (DB:%u param:%u)\n",
4727 (u_int32_t)ntohl(sav->spi),
4728 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4729 return key_senderror(so, m, EINVAL);
4732 if (sav->pid != mhp->msg->sadb_msg_pid) {
4733 ipseclog((LOG_DEBUG, "%s: pid mismatched (DB:%u param:%u)\n",
4734 __func__, sav->pid, mhp->msg->sadb_msg_pid));
4735 return key_senderror(so, m, EINVAL);
4738 /* copy sav values */
4739 error = key_setsaval(sav, m, mhp);
4742 return key_senderror(so, m, error);
4745 /* check SA values to be mature. */
4746 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
4748 return key_senderror(so, m, 0);
4754 /* set msg buf from mhp */
4755 n = key_getmsgbuf_x1(m, mhp);
4757 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
4758 return key_senderror(so, m, ENOBUFS);
4762 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4767 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
4768 * only called by key_update().
4771 * others : found, pointer to a SA.
4773 #ifdef IPSEC_DOSEQCHECK
4774 static struct secasvar *
4775 key_getsavbyseq(sah, seq)
4776 struct secashead *sah;
4779 struct secasvar *sav;
4782 state = SADB_SASTATE_LARVAL;
4784 /* search SAD with sequence number ? */
4785 LIST_FOREACH(sav, &sah->savtree[state], chain) {
4787 KEY_CHKSASTATE(state, sav->state, __func__);
4789 if (sav->seq == seq) {
4791 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
4792 printf("DP %s cause refcnt++:%d SA:%p\n",
4793 __func__, sav->refcnt, sav));
4803 * SADB_ADD processing
4804 * add an entry to SA database, when received
4805 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4806 * key(AE), (identity(SD),) (sensitivity)>
4809 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4810 * (identity(SD),) (sensitivity)>
4813 * IGNORE identity and sensitivity messages.
4815 * m will always be freed.
4821 const struct sadb_msghdr *mhp;
4823 struct sadb_sa *sa0;
4824 struct sadb_address *src0, *dst0;
4825 struct secasindex saidx;
4826 struct secashead *newsah;
4827 struct secasvar *newsav;
4833 IPSEC_ASSERT(so != NULL, ("null socket"));
4834 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4835 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4836 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4838 /* map satype to proto */
4839 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4840 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4842 return key_senderror(so, m, EINVAL);
4845 if (mhp->ext[SADB_EXT_SA] == NULL ||
4846 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4847 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4848 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4849 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4850 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4851 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4852 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4853 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4854 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4855 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4856 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4858 return key_senderror(so, m, EINVAL);
4860 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4861 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4862 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4864 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4866 return key_senderror(so, m, EINVAL);
4868 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4869 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4870 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4872 mode = IPSEC_MODE_ANY;
4876 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4877 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
4878 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
4880 /* XXX boundary check against sa_len */
4881 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4883 /* get a SA header */
4884 if ((newsah = key_getsah(&saidx)) == NULL) {
4885 /* create a new SA header */
4886 if ((newsah = key_newsah(&saidx)) == NULL) {
4887 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
4888 return key_senderror(so, m, ENOBUFS);
4892 /* set spidx if there */
4894 error = key_setident(newsah, m, mhp);
4896 return key_senderror(so, m, error);
4899 /* create new SA entry. */
4900 /* We can create new SA only if SPI is differenct. */
4902 newsav = key_getsavbyspi(newsah, sa0->sadb_sa_spi);
4904 if (newsav != NULL) {
4905 ipseclog((LOG_DEBUG, "%s: SA already exists.\n", __func__));
4906 return key_senderror(so, m, EEXIST);
4908 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4909 if (newsav == NULL) {
4910 return key_senderror(so, m, error);
4913 /* check SA values to be mature. */
4914 if ((error = key_mature(newsav)) != 0) {
4915 KEY_FREESAV(&newsav);
4916 return key_senderror(so, m, error);
4920 * don't call key_freesav() here, as we would like to keep the SA
4921 * in the database on success.
4927 /* set msg buf from mhp */
4928 n = key_getmsgbuf_x1(m, mhp);
4930 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
4931 return key_senderror(so, m, ENOBUFS);
4935 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4941 key_setident(sah, m, mhp)
4942 struct secashead *sah;
4944 const struct sadb_msghdr *mhp;
4946 const struct sadb_ident *idsrc, *iddst;
4947 int idsrclen, iddstlen;
4949 IPSEC_ASSERT(sah != NULL, ("null secashead"));
4950 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4951 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4952 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4954 /* don't make buffer if not there */
4955 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
4956 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
4962 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
4963 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
4964 ipseclog((LOG_DEBUG, "%s: invalid identity.\n", __func__));
4968 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
4969 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
4970 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
4971 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
4973 /* validity check */
4974 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
4975 ipseclog((LOG_DEBUG, "%s: ident type mismatch.\n", __func__));
4979 switch (idsrc->sadb_ident_type) {
4980 case SADB_IDENTTYPE_PREFIX:
4981 case SADB_IDENTTYPE_FQDN:
4982 case SADB_IDENTTYPE_USERFQDN:
4984 /* XXX do nothing */
4990 /* make structure */
4991 sah->idents = malloc(idsrclen, M_IPSEC_MISC, M_NOWAIT);
4992 if (sah->idents == NULL) {
4993 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
4996 sah->identd = malloc(iddstlen, M_IPSEC_MISC, M_NOWAIT);
4997 if (sah->identd == NULL) {
4998 free(sah->idents, M_IPSEC_MISC);
5000 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5003 bcopy(idsrc, sah->idents, idsrclen);
5004 bcopy(iddst, sah->identd, iddstlen);
5010 * m will not be freed on return.
5011 * it is caller's responsibility to free the result.
5013 static struct mbuf *
5014 key_getmsgbuf_x1(m, mhp)
5016 const struct sadb_msghdr *mhp;
5020 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5021 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5022 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5024 /* create new sadb_msg to reply. */
5025 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5026 SADB_EXT_SA, SADB_X_EXT_SA2,
5027 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5028 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5029 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5033 if (n->m_len < sizeof(struct sadb_msg)) {
5034 n = m_pullup(n, sizeof(struct sadb_msg));
5038 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5039 mtod(n, struct sadb_msg *)->sadb_msg_len =
5040 PFKEY_UNIT64(n->m_pkthdr.len);
5045 static int key_delete_all __P((struct socket *, struct mbuf *,
5046 const struct sadb_msghdr *, u_int16_t));
5049 * SADB_DELETE processing
5051 * <base, SA(*), address(SD)>
5052 * from the ikmpd, and set SADB_SASTATE_DEAD,
5054 * <base, SA(*), address(SD)>
5057 * m will always be freed.
5060 key_delete(so, m, mhp)
5063 const struct sadb_msghdr *mhp;
5065 struct sadb_sa *sa0;
5066 struct sadb_address *src0, *dst0;
5067 struct secasindex saidx;
5068 struct secashead *sah;
5069 struct secasvar *sav = NULL;
5072 IPSEC_ASSERT(so != NULL, ("null socket"));
5073 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5074 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5075 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5077 /* map satype to proto */
5078 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5079 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5081 return key_senderror(so, m, EINVAL);
5084 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5085 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5086 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5088 return key_senderror(so, m, EINVAL);
5091 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5092 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5093 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5095 return key_senderror(so, m, EINVAL);
5098 if (mhp->ext[SADB_EXT_SA] == NULL) {
5100 * Caller wants us to delete all non-LARVAL SAs
5101 * that match the src/dst. This is used during
5102 * IKE INITIAL-CONTACT.
5104 ipseclog((LOG_DEBUG, "%s: doing delete all.\n", __func__));
5105 return key_delete_all(so, m, mhp, proto);
5106 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5107 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5109 return key_senderror(so, m, EINVAL);
5112 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5113 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5114 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5116 /* XXX boundary check against sa_len */
5117 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5119 /* get a SA header */
5121 LIST_FOREACH(sah, &sahtree, chain) {
5122 if (sah->state == SADB_SASTATE_DEAD)
5124 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5127 /* get a SA with SPI. */
5128 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5134 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5135 return key_senderror(so, m, ENOENT);
5138 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5144 struct sadb_msg *newmsg;
5146 /* create new sadb_msg to reply. */
5147 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5148 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5150 return key_senderror(so, m, ENOBUFS);
5152 if (n->m_len < sizeof(struct sadb_msg)) {
5153 n = m_pullup(n, sizeof(struct sadb_msg));
5155 return key_senderror(so, m, ENOBUFS);
5157 newmsg = mtod(n, struct sadb_msg *);
5158 newmsg->sadb_msg_errno = 0;
5159 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5162 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5167 * delete all SAs for src/dst. Called from key_delete().
5170 key_delete_all(so, m, mhp, proto)
5173 const struct sadb_msghdr *mhp;
5176 struct sadb_address *src0, *dst0;
5177 struct secasindex saidx;
5178 struct secashead *sah;
5179 struct secasvar *sav, *nextsav;
5180 u_int stateidx, state;
5182 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5183 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5185 /* XXX boundary check against sa_len */
5186 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5189 LIST_FOREACH(sah, &sahtree, chain) {
5190 if (sah->state == SADB_SASTATE_DEAD)
5192 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5195 /* Delete all non-LARVAL SAs. */
5197 stateidx < _ARRAYLEN(saorder_state_alive);
5199 state = saorder_state_alive[stateidx];
5200 if (state == SADB_SASTATE_LARVAL)
5202 for (sav = LIST_FIRST(&sah->savtree[state]);
5203 sav != NULL; sav = nextsav) {
5204 nextsav = LIST_NEXT(sav, chain);
5206 if (sav->state != state) {
5207 ipseclog((LOG_DEBUG, "%s: invalid "
5208 "sav->state (queue %d SA %d)\n",
5209 __func__, state, sav->state));
5213 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5221 struct sadb_msg *newmsg;
5223 /* create new sadb_msg to reply. */
5224 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5225 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5227 return key_senderror(so, m, ENOBUFS);
5229 if (n->m_len < sizeof(struct sadb_msg)) {
5230 n = m_pullup(n, sizeof(struct sadb_msg));
5232 return key_senderror(so, m, ENOBUFS);
5234 newmsg = mtod(n, struct sadb_msg *);
5235 newmsg->sadb_msg_errno = 0;
5236 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5239 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5244 * SADB_GET processing
5246 * <base, SA(*), address(SD)>
5247 * from the ikmpd, and get a SP and a SA to respond,
5249 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5250 * (identity(SD),) (sensitivity)>
5253 * m will always be freed.
5259 const struct sadb_msghdr *mhp;
5261 struct sadb_sa *sa0;
5262 struct sadb_address *src0, *dst0;
5263 struct secasindex saidx;
5264 struct secashead *sah;
5265 struct secasvar *sav = NULL;
5268 IPSEC_ASSERT(so != NULL, ("null socket"));
5269 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5270 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5271 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5273 /* map satype to proto */
5274 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5275 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5277 return key_senderror(so, m, EINVAL);
5280 if (mhp->ext[SADB_EXT_SA] == NULL ||
5281 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5282 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5283 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5285 return key_senderror(so, m, EINVAL);
5287 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5288 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5289 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5290 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5292 return key_senderror(so, m, EINVAL);
5295 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5296 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5297 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5299 /* XXX boundary check against sa_len */
5300 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5302 /* get a SA header */
5304 LIST_FOREACH(sah, &sahtree, chain) {
5305 if (sah->state == SADB_SASTATE_DEAD)
5307 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5310 /* get a SA with SPI. */
5311 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5317 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5318 return key_senderror(so, m, ENOENT);
5325 /* map proto to satype */
5326 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5327 ipseclog((LOG_DEBUG, "%s: there was invalid proto in SAD.\n",
5329 return key_senderror(so, m, EINVAL);
5332 /* create new sadb_msg to reply. */
5333 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5334 mhp->msg->sadb_msg_pid);
5336 return key_senderror(so, m, ENOBUFS);
5339 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5343 /* XXX make it sysctl-configurable? */
5345 key_getcomb_setlifetime(comb)
5346 struct sadb_comb *comb;
5349 comb->sadb_comb_soft_allocations = 1;
5350 comb->sadb_comb_hard_allocations = 1;
5351 comb->sadb_comb_soft_bytes = 0;
5352 comb->sadb_comb_hard_bytes = 0;
5353 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5354 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5355 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5356 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
5360 * XXX reorder combinations by preference
5361 * XXX no idea if the user wants ESP authentication or not
5363 static struct mbuf *
5366 struct sadb_comb *comb;
5367 struct enc_xform *algo;
5368 struct mbuf *result = NULL, *m, *n;
5372 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5375 for (i = 1; i <= SADB_EALG_MAX; i++) {
5376 algo = esp_algorithm_lookup(i);
5380 /* discard algorithms with key size smaller than system min */
5381 if (_BITS(algo->maxkey) < ipsec_esp_keymin)
5383 if (_BITS(algo->minkey) < ipsec_esp_keymin)
5384 encmin = ipsec_esp_keymin;
5386 encmin = _BITS(algo->minkey);
5389 m = key_getcomb_ah();
5391 IPSEC_ASSERT(l <= MLEN,
5392 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
5393 MGET(m, M_DONTWAIT, MT_DATA);
5398 bzero(mtod(m, caddr_t), m->m_len);
5405 for (n = m; n; n = n->m_next)
5407 IPSEC_ASSERT((totlen % l) == 0, ("totlen=%u, l=%u", totlen, l));
5409 for (off = 0; off < totlen; off += l) {
5410 n = m_pulldown(m, off, l, &o);
5412 /* m is already freed */
5415 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
5416 bzero(comb, sizeof(*comb));
5417 key_getcomb_setlifetime(comb);
5418 comb->sadb_comb_encrypt = i;
5419 comb->sadb_comb_encrypt_minbits = encmin;
5420 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
5439 const struct auth_hash *ah,
5444 *min = *max = ah->keysize;
5445 if (ah->keysize == 0) {
5447 * Transform takes arbitrary key size but algorithm
5448 * key size is restricted. Enforce this here.
5451 case SADB_X_AALG_MD5: *min = *max = 16; break;
5452 case SADB_X_AALG_SHA: *min = *max = 20; break;
5453 case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
5455 DPRINTF(("%s: unknown AH algorithm %u\n",
5463 * XXX reorder combinations by preference
5465 static struct mbuf *
5468 struct sadb_comb *comb;
5469 struct auth_hash *algo;
5471 u_int16_t minkeysize, maxkeysize;
5473 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5476 for (i = 1; i <= SADB_AALG_MAX; i++) {
5478 /* we prefer HMAC algorithms, not old algorithms */
5479 if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC)
5482 algo = ah_algorithm_lookup(i);
5485 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
5486 /* discard algorithms with key size smaller than system min */
5487 if (_BITS(minkeysize) < ipsec_ah_keymin)
5491 IPSEC_ASSERT(l <= MLEN,
5492 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
5493 MGET(m, M_DONTWAIT, MT_DATA);
5500 M_PREPEND(m, l, M_DONTWAIT);
5504 comb = mtod(m, struct sadb_comb *);
5505 bzero(comb, sizeof(*comb));
5506 key_getcomb_setlifetime(comb);
5507 comb->sadb_comb_auth = i;
5508 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
5509 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
5516 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
5517 * XXX reorder combinations by preference
5519 static struct mbuf *
5520 key_getcomb_ipcomp()
5522 struct sadb_comb *comb;
5523 struct comp_algo *algo;
5526 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5529 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
5530 algo = ipcomp_algorithm_lookup(i);
5535 IPSEC_ASSERT(l <= MLEN,
5536 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
5537 MGET(m, M_DONTWAIT, MT_DATA);
5544 M_PREPEND(m, l, M_DONTWAIT);
5548 comb = mtod(m, struct sadb_comb *);
5549 bzero(comb, sizeof(*comb));
5550 key_getcomb_setlifetime(comb);
5551 comb->sadb_comb_encrypt = i;
5552 /* what should we set into sadb_comb_*_{min,max}bits? */
5559 * XXX no way to pass mode (transport/tunnel) to userland
5560 * XXX replay checking?
5561 * XXX sysctl interface to ipsec_{ah,esp}_keymin
5563 static struct mbuf *
5565 const struct secasindex *saidx;
5567 struct sadb_prop *prop;
5569 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
5572 switch (saidx->proto) {
5574 m = key_getcomb_esp();
5577 m = key_getcomb_ah();
5579 case IPPROTO_IPCOMP:
5580 m = key_getcomb_ipcomp();
5588 M_PREPEND(m, l, M_DONTWAIT);
5593 for (n = m; n; n = n->m_next)
5596 prop = mtod(m, struct sadb_prop *);
5597 bzero(prop, sizeof(*prop));
5598 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
5599 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
5600 prop->sadb_prop_replay = 32; /* XXX */
5606 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5608 * <base, SA, address(SD), (address(P)), x_policy,
5609 * (identity(SD),) (sensitivity,) proposal>
5610 * to KMD, and expect to receive
5611 * <base> with SADB_ACQUIRE if error occured,
5613 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5614 * from KMD by PF_KEY.
5616 * XXX x_policy is outside of RFC2367 (KAME extension).
5617 * XXX sensitivity is not supported.
5618 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
5619 * see comment for key_getcomb_ipcomp().
5623 * others: error number
5626 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
5628 struct mbuf *result = NULL, *m;
5629 struct secacq *newacq;
5634 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
5635 satype = key_proto2satype(saidx->proto);
5636 IPSEC_ASSERT(satype != 0, ("null satype, protocol %u", saidx->proto));
5639 * We never do anything about acquirng SA. There is anather
5640 * solution that kernel blocks to send SADB_ACQUIRE message until
5641 * getting something message from IKEd. In later case, to be
5642 * managed with ACQUIRING list.
5644 /* Get an entry to check whether sending message or not. */
5645 if ((newacq = key_getacq(saidx)) != NULL) {
5646 if (key_blockacq_count < newacq->count) {
5647 /* reset counter and do send message. */
5650 /* increment counter and do nothing. */
5655 /* make new entry for blocking to send SADB_ACQUIRE. */
5656 if ((newacq = key_newacq(saidx)) == NULL)
5662 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
5669 /* set sadb_address for saidx's. */
5670 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
5671 &saidx->src.sa, FULLMASK, IPSEC_ULPROTO_ANY);
5678 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
5679 &saidx->dst.sa, FULLMASK, IPSEC_ULPROTO_ANY);
5686 /* XXX proxy address (optional) */
5688 /* set sadb_x_policy */
5690 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
5698 /* XXX identity (optional) */
5700 if (idexttype && fqdn) {
5701 /* create identity extension (FQDN) */
5702 struct sadb_ident *id;
5705 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
5706 id = (struct sadb_ident *)p;
5707 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5708 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5709 id->sadb_ident_exttype = idexttype;
5710 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
5711 bcopy(fqdn, id + 1, fqdnlen);
5712 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
5716 /* create identity extension (USERFQDN) */
5717 struct sadb_ident *id;
5721 /* +1 for terminating-NUL */
5722 userfqdnlen = strlen(userfqdn) + 1;
5725 id = (struct sadb_ident *)p;
5726 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5727 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5728 id->sadb_ident_exttype = idexttype;
5729 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
5730 /* XXX is it correct? */
5731 if (curproc && curproc->p_cred)
5732 id->sadb_ident_id = curproc->p_cred->p_ruid;
5733 if (userfqdn && userfqdnlen)
5734 bcopy(userfqdn, id + 1, userfqdnlen);
5735 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
5739 /* XXX sensitivity (optional) */
5741 /* create proposal/combination extension */
5742 m = key_getprop(saidx);
5745 * spec conformant: always attach proposal/combination extension,
5746 * the problem is that we have no way to attach it for ipcomp,
5747 * due to the way sadb_comb is declared in RFC2367.
5756 * outside of spec; make proposal/combination extension optional.
5762 if ((result->m_flags & M_PKTHDR) == 0) {
5767 if (result->m_len < sizeof(struct sadb_msg)) {
5768 result = m_pullup(result, sizeof(struct sadb_msg));
5769 if (result == NULL) {
5775 result->m_pkthdr.len = 0;
5776 for (m = result; m; m = m->m_next)
5777 result->m_pkthdr.len += m->m_len;
5779 mtod(result, struct sadb_msg *)->sadb_msg_len =
5780 PFKEY_UNIT64(result->m_pkthdr.len);
5782 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
5790 static struct secacq *
5791 key_newacq(const struct secasindex *saidx)
5793 struct secacq *newacq;
5796 newacq = malloc(sizeof(struct secacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
5797 if (newacq == NULL) {
5798 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5803 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
5804 newacq->seq = (acq_seq == ~0 ? 1 : ++acq_seq);
5805 newacq->created = time_second;
5808 /* add to acqtree */
5810 LIST_INSERT_HEAD(&acqtree, newacq, chain);
5816 static struct secacq *
5817 key_getacq(const struct secasindex *saidx)
5822 LIST_FOREACH(acq, &acqtree, chain) {
5823 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
5831 static struct secacq *
5832 key_getacqbyseq(seq)
5838 LIST_FOREACH(acq, &acqtree, chain) {
5839 if (acq->seq == seq)
5847 static struct secspacq *
5849 struct secpolicyindex *spidx;
5851 struct secspacq *acq;
5854 acq = malloc(sizeof(struct secspacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
5856 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5861 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
5862 acq->created = time_second;
5865 /* add to spacqtree */
5867 LIST_INSERT_HEAD(&spacqtree, acq, chain);
5873 static struct secspacq *
5875 struct secpolicyindex *spidx;
5877 struct secspacq *acq;
5880 LIST_FOREACH(acq, &spacqtree, chain) {
5881 if (key_cmpspidx_exactly(spidx, &acq->spidx)) {
5882 /* NB: return holding spacq_lock */
5892 * SADB_ACQUIRE processing,
5893 * in first situation, is receiving
5895 * from the ikmpd, and clear sequence of its secasvar entry.
5897 * In second situation, is receiving
5898 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5899 * from a user land process, and return
5900 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5903 * m will always be freed.
5906 key_acquire2(so, m, mhp)
5909 const struct sadb_msghdr *mhp;
5911 const struct sadb_address *src0, *dst0;
5912 struct secasindex saidx;
5913 struct secashead *sah;
5917 IPSEC_ASSERT(so != NULL, ("null socket"));
5918 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5919 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5920 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5923 * Error message from KMd.
5924 * We assume that if error was occured in IKEd, the length of PFKEY
5925 * message is equal to the size of sadb_msg structure.
5926 * We do not raise error even if error occured in this function.
5928 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
5931 /* check sequence number */
5932 if (mhp->msg->sadb_msg_seq == 0) {
5933 ipseclog((LOG_DEBUG, "%s: must specify sequence "
5934 "number.\n", __func__));
5939 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
5941 * the specified larval SA is already gone, or we got
5942 * a bogus sequence number. we can silently ignore it.
5948 /* reset acq counter in order to deletion by timehander. */
5949 acq->created = time_second;
5956 * This message is from user land.
5959 /* map satype to proto */
5960 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5961 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5963 return key_senderror(so, m, EINVAL);
5966 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5967 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5968 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
5970 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5972 return key_senderror(so, m, EINVAL);
5974 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5975 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
5976 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
5978 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5980 return key_senderror(so, m, EINVAL);
5983 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5984 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5986 /* XXX boundary check against sa_len */
5987 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5989 /* get a SA index */
5991 LIST_FOREACH(sah, &sahtree, chain) {
5992 if (sah->state == SADB_SASTATE_DEAD)
5994 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
5999 ipseclog((LOG_DEBUG, "%s: a SA exists already.\n", __func__));
6000 return key_senderror(so, m, EEXIST);
6003 error = key_acquire(&saidx, NULL);
6005 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
6006 __func__, mhp->msg->sadb_msg_errno));
6007 return key_senderror(so, m, error);
6010 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6014 * SADB_REGISTER processing.
6015 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6018 * from the ikmpd, and register a socket to send PF_KEY messages,
6022 * If socket is detached, must free from regnode.
6024 * m will always be freed.
6027 key_register(so, m, mhp)
6030 const struct sadb_msghdr *mhp;
6032 struct secreg *reg, *newreg = 0;
6034 IPSEC_ASSERT(so != NULL, ("null socket"));
6035 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6036 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6037 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6039 /* check for invalid register message */
6040 if (mhp->msg->sadb_msg_satype >= sizeof(regtree)/sizeof(regtree[0]))
6041 return key_senderror(so, m, EINVAL);
6043 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6044 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6047 /* check whether existing or not */
6049 LIST_FOREACH(reg, ®tree[mhp->msg->sadb_msg_satype], chain) {
6050 if (reg->so == so) {
6052 ipseclog((LOG_DEBUG, "%s: socket exists already.\n",
6054 return key_senderror(so, m, EEXIST);
6058 /* create regnode */
6059 newreg = malloc(sizeof(struct secreg), M_IPSEC_SAR, M_NOWAIT|M_ZERO);
6060 if (newreg == NULL) {
6062 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6063 return key_senderror(so, m, ENOBUFS);
6067 ((struct keycb *)sotorawcb(so))->kp_registered++;
6069 /* add regnode to regtree. */
6070 LIST_INSERT_HEAD(®tree[mhp->msg->sadb_msg_satype], newreg, chain);
6076 struct sadb_msg *newmsg;
6077 struct sadb_supported *sup;
6078 u_int len, alen, elen;
6081 struct sadb_alg *alg;
6083 /* create new sadb_msg to reply. */
6085 for (i = 1; i <= SADB_AALG_MAX; i++) {
6086 if (ah_algorithm_lookup(i))
6087 alen += sizeof(struct sadb_alg);
6090 alen += sizeof(struct sadb_supported);
6092 for (i = 1; i <= SADB_EALG_MAX; i++) {
6093 if (esp_algorithm_lookup(i))
6094 elen += sizeof(struct sadb_alg);
6097 elen += sizeof(struct sadb_supported);
6099 len = sizeof(struct sadb_msg) + alen + elen;
6102 return key_senderror(so, m, ENOBUFS);
6104 MGETHDR(n, M_DONTWAIT, MT_DATA);
6106 MCLGET(n, M_DONTWAIT);
6107 if ((n->m_flags & M_EXT) == 0) {
6113 return key_senderror(so, m, ENOBUFS);
6115 n->m_pkthdr.len = n->m_len = len;
6119 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6120 newmsg = mtod(n, struct sadb_msg *);
6121 newmsg->sadb_msg_errno = 0;
6122 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6123 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6125 /* for authentication algorithm */
6127 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6128 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6129 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6130 off += PFKEY_ALIGN8(sizeof(*sup));
6132 for (i = 1; i <= SADB_AALG_MAX; i++) {
6133 struct auth_hash *aalgo;
6134 u_int16_t minkeysize, maxkeysize;
6136 aalgo = ah_algorithm_lookup(i);
6139 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6140 alg->sadb_alg_id = i;
6141 alg->sadb_alg_ivlen = 0;
6142 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
6143 alg->sadb_alg_minbits = _BITS(minkeysize);
6144 alg->sadb_alg_maxbits = _BITS(maxkeysize);
6145 off += PFKEY_ALIGN8(sizeof(*alg));
6149 /* for encryption algorithm */
6151 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6152 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6153 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6154 off += PFKEY_ALIGN8(sizeof(*sup));
6156 for (i = 1; i <= SADB_EALG_MAX; i++) {
6157 struct enc_xform *ealgo;
6159 ealgo = esp_algorithm_lookup(i);
6162 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6163 alg->sadb_alg_id = i;
6164 alg->sadb_alg_ivlen = ealgo->blocksize;
6165 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
6166 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
6167 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6171 IPSEC_ASSERT(off == len,
6172 ("length assumption failed (off %u len %u)", off, len));
6175 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6180 * free secreg entry registered.
6181 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6184 key_freereg(struct socket *so)
6189 IPSEC_ASSERT(so != NULL, ("NULL so"));
6192 * check whether existing or not.
6193 * check all type of SA, because there is a potential that
6194 * one socket is registered to multiple type of SA.
6197 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6198 LIST_FOREACH(reg, ®tree[i], chain) {
6199 if (reg->so == so && __LIST_CHAINED(reg)) {
6200 LIST_REMOVE(reg, chain);
6201 free(reg, M_IPSEC_SAR);
6210 * SADB_EXPIRE processing
6212 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6214 * NOTE: We send only soft lifetime extension.
6217 * others : error number
6220 key_expire(struct secasvar *sav)
6224 struct mbuf *result = NULL, *m;
6227 struct sadb_lifetime *lt;
6229 /* XXX: Why do we lock ? */
6230 s = splnet(); /*called from softclock()*/
6232 IPSEC_ASSERT (sav != NULL, ("null sav"));
6233 IPSEC_ASSERT (sav->sah != NULL, ("null sa header"));
6235 /* set msg header */
6236 satype = key_proto2satype(sav->sah->saidx.proto);
6237 IPSEC_ASSERT(satype != 0, ("invalid proto, satype %u", satype));
6238 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6245 /* create SA extension */
6246 m = key_setsadbsa(sav);
6253 /* create SA extension */
6254 m = key_setsadbxsa2(sav->sah->saidx.mode,
6255 sav->replay ? sav->replay->count : 0,
6256 sav->sah->saidx.reqid);
6263 /* create lifetime extension (current and soft) */
6264 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6265 m = key_alloc_mbuf(len);
6266 if (!m || m->m_next) { /*XXX*/
6272 bzero(mtod(m, caddr_t), len);
6273 lt = mtod(m, struct sadb_lifetime *);
6274 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6275 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6276 lt->sadb_lifetime_allocations = sav->lft_c->sadb_lifetime_allocations;
6277 lt->sadb_lifetime_bytes = sav->lft_c->sadb_lifetime_bytes;
6278 lt->sadb_lifetime_addtime = sav->lft_c->sadb_lifetime_addtime;
6279 lt->sadb_lifetime_usetime = sav->lft_c->sadb_lifetime_usetime;
6280 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6281 bcopy(sav->lft_s, lt, sizeof(*lt));
6284 /* set sadb_address for source */
6285 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6286 &sav->sah->saidx.src.sa,
6287 FULLMASK, IPSEC_ULPROTO_ANY);
6294 /* set sadb_address for destination */
6295 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6296 &sav->sah->saidx.dst.sa,
6297 FULLMASK, IPSEC_ULPROTO_ANY);
6304 if ((result->m_flags & M_PKTHDR) == 0) {
6309 if (result->m_len < sizeof(struct sadb_msg)) {
6310 result = m_pullup(result, sizeof(struct sadb_msg));
6311 if (result == NULL) {
6317 result->m_pkthdr.len = 0;
6318 for (m = result; m; m = m->m_next)
6319 result->m_pkthdr.len += m->m_len;
6321 mtod(result, struct sadb_msg *)->sadb_msg_len =
6322 PFKEY_UNIT64(result->m_pkthdr.len);
6325 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6335 * SADB_FLUSH processing
6338 * from the ikmpd, and free all entries in secastree.
6342 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6344 * m will always be freed.
6347 key_flush(so, m, mhp)
6350 const struct sadb_msghdr *mhp;
6352 struct sadb_msg *newmsg;
6353 struct secashead *sah, *nextsah;
6354 struct secasvar *sav, *nextsav;
6359 IPSEC_ASSERT(so != NULL, ("null socket"));
6360 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6361 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6363 /* map satype to proto */
6364 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6365 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6367 return key_senderror(so, m, EINVAL);
6370 /* no SATYPE specified, i.e. flushing all SA. */
6372 for (sah = LIST_FIRST(&sahtree);
6375 nextsah = LIST_NEXT(sah, chain);
6377 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6378 && proto != sah->saidx.proto)
6382 stateidx < _ARRAYLEN(saorder_state_alive);
6384 state = saorder_state_any[stateidx];
6385 for (sav = LIST_FIRST(&sah->savtree[state]);
6389 nextsav = LIST_NEXT(sav, chain);
6391 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
6396 sah->state = SADB_SASTATE_DEAD;
6400 if (m->m_len < sizeof(struct sadb_msg) ||
6401 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
6402 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6403 return key_senderror(so, m, ENOBUFS);
6409 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
6410 newmsg = mtod(m, struct sadb_msg *);
6411 newmsg->sadb_msg_errno = 0;
6412 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
6414 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6418 * SADB_DUMP processing
6419 * dump all entries including status of DEAD in SAD.
6422 * from the ikmpd, and dump all secasvar leaves
6427 * m will always be freed.
6430 key_dump(so, m, mhp)
6433 const struct sadb_msghdr *mhp;
6435 struct secashead *sah;
6436 struct secasvar *sav;
6442 struct sadb_msg *newmsg;
6445 IPSEC_ASSERT(so != NULL, ("null socket"));
6446 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6447 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6448 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6450 /* map satype to proto */
6451 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6452 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6454 return key_senderror(so, m, EINVAL);
6457 /* count sav entries to be sent to the userland. */
6460 LIST_FOREACH(sah, &sahtree, chain) {
6461 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6462 && proto != sah->saidx.proto)
6466 stateidx < _ARRAYLEN(saorder_state_any);
6468 state = saorder_state_any[stateidx];
6469 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6477 return key_senderror(so, m, ENOENT);
6480 /* send this to the userland, one at a time. */
6482 LIST_FOREACH(sah, &sahtree, chain) {
6483 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6484 && proto != sah->saidx.proto)
6487 /* map proto to satype */
6488 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
6490 ipseclog((LOG_DEBUG, "%s: there was invalid proto in "
6491 "SAD.\n", __func__));
6492 return key_senderror(so, m, EINVAL);
6496 stateidx < _ARRAYLEN(saorder_state_any);
6498 state = saorder_state_any[stateidx];
6499 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6500 n = key_setdumpsa(sav, SADB_DUMP, satype,
6501 --cnt, mhp->msg->sadb_msg_pid);
6504 return key_senderror(so, m, ENOBUFS);
6506 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
6517 * SADB_X_PROMISC processing
6519 * m will always be freed.
6522 key_promisc(so, m, mhp)
6525 const struct sadb_msghdr *mhp;
6529 IPSEC_ASSERT(so != NULL, ("null socket"));
6530 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6531 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6532 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6534 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6536 if (olen < sizeof(struct sadb_msg)) {
6538 return key_senderror(so, m, EINVAL);
6543 } else if (olen == sizeof(struct sadb_msg)) {
6544 /* enable/disable promisc mode */
6547 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
6548 return key_senderror(so, m, EINVAL);
6549 mhp->msg->sadb_msg_errno = 0;
6550 switch (mhp->msg->sadb_msg_satype) {
6553 kp->kp_promisc = mhp->msg->sadb_msg_satype;
6556 return key_senderror(so, m, EINVAL);
6559 /* send the original message back to everyone */
6560 mhp->msg->sadb_msg_errno = 0;
6561 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6563 /* send packet as is */
6565 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
6567 /* TODO: if sadb_msg_seq is specified, send to specific pid */
6568 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6572 static int (*key_typesw[]) __P((struct socket *, struct mbuf *,
6573 const struct sadb_msghdr *)) = {
6574 NULL, /* SADB_RESERVED */
6575 key_getspi, /* SADB_GETSPI */
6576 key_update, /* SADB_UPDATE */
6577 key_add, /* SADB_ADD */
6578 key_delete, /* SADB_DELETE */
6579 key_get, /* SADB_GET */
6580 key_acquire2, /* SADB_ACQUIRE */
6581 key_register, /* SADB_REGISTER */
6582 NULL, /* SADB_EXPIRE */
6583 key_flush, /* SADB_FLUSH */
6584 key_dump, /* SADB_DUMP */
6585 key_promisc, /* SADB_X_PROMISC */
6586 NULL, /* SADB_X_PCHANGE */
6587 key_spdadd, /* SADB_X_SPDUPDATE */
6588 key_spdadd, /* SADB_X_SPDADD */
6589 key_spddelete, /* SADB_X_SPDDELETE */
6590 key_spdget, /* SADB_X_SPDGET */
6591 NULL, /* SADB_X_SPDACQUIRE */
6592 key_spddump, /* SADB_X_SPDDUMP */
6593 key_spdflush, /* SADB_X_SPDFLUSH */
6594 key_spdadd, /* SADB_X_SPDSETIDX */
6595 NULL, /* SADB_X_SPDEXPIRE */
6596 key_spddelete2, /* SADB_X_SPDDELETE2 */
6600 * parse sadb_msg buffer to process PFKEYv2,
6601 * and create a data to response if needed.
6602 * I think to be dealed with mbuf directly.
6604 * msgp : pointer to pointer to a received buffer pulluped.
6605 * This is rewrited to response.
6606 * so : pointer to socket.
6608 * length for buffer to send to user process.
6615 struct sadb_msg *msg;
6616 struct sadb_msghdr mh;
6621 IPSEC_ASSERT(so != NULL, ("null socket"));
6622 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6624 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
6625 KEYDEBUG(KEYDEBUG_KEY_DUMP,
6626 ipseclog((LOG_DEBUG, "%s: passed sadb_msg\n", __func__));
6630 if (m->m_len < sizeof(struct sadb_msg)) {
6631 m = m_pullup(m, sizeof(struct sadb_msg));
6635 msg = mtod(m, struct sadb_msg *);
6636 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
6637 target = KEY_SENDUP_ONE;
6639 if ((m->m_flags & M_PKTHDR) == 0 ||
6640 m->m_pkthdr.len != m->m_pkthdr.len) {
6641 ipseclog((LOG_DEBUG, "%s: invalid message length.\n",__func__));
6642 pfkeystat.out_invlen++;
6647 if (msg->sadb_msg_version != PF_KEY_V2) {
6648 ipseclog((LOG_DEBUG, "%s: PF_KEY version %u is mismatched.\n",
6649 __func__, msg->sadb_msg_version));
6650 pfkeystat.out_invver++;
6655 if (msg->sadb_msg_type > SADB_MAX) {
6656 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
6657 __func__, msg->sadb_msg_type));
6658 pfkeystat.out_invmsgtype++;
6663 /* for old-fashioned code - should be nuked */
6664 if (m->m_pkthdr.len > MCLBYTES) {
6671 MGETHDR(n, M_DONTWAIT, MT_DATA);
6672 if (n && m->m_pkthdr.len > MHLEN) {
6673 MCLGET(n, M_DONTWAIT);
6674 if ((n->m_flags & M_EXT) == 0) {
6683 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
6684 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
6690 /* align the mbuf chain so that extensions are in contiguous region. */
6691 error = key_align(m, &mh);
6698 switch (msg->sadb_msg_satype) {
6699 case SADB_SATYPE_UNSPEC:
6700 switch (msg->sadb_msg_type) {
6708 ipseclog((LOG_DEBUG, "%s: must specify satype "
6709 "when msg type=%u.\n", __func__,
6710 msg->sadb_msg_type));
6711 pfkeystat.out_invsatype++;
6716 case SADB_SATYPE_AH:
6717 case SADB_SATYPE_ESP:
6718 case SADB_X_SATYPE_IPCOMP:
6719 case SADB_X_SATYPE_TCPSIGNATURE:
6720 switch (msg->sadb_msg_type) {
6722 case SADB_X_SPDDELETE:
6724 case SADB_X_SPDDUMP:
6725 case SADB_X_SPDFLUSH:
6726 case SADB_X_SPDSETIDX:
6727 case SADB_X_SPDUPDATE:
6728 case SADB_X_SPDDELETE2:
6729 ipseclog((LOG_DEBUG, "%s: illegal satype=%u\n",
6730 __func__, msg->sadb_msg_type));
6731 pfkeystat.out_invsatype++;
6736 case SADB_SATYPE_RSVP:
6737 case SADB_SATYPE_OSPFV2:
6738 case SADB_SATYPE_RIPV2:
6739 case SADB_SATYPE_MIP:
6740 ipseclog((LOG_DEBUG, "%s: type %u isn't supported.\n",
6741 __func__, msg->sadb_msg_satype));
6742 pfkeystat.out_invsatype++;
6745 case 1: /* XXX: What does it do? */
6746 if (msg->sadb_msg_type == SADB_X_PROMISC)
6750 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
6751 __func__, msg->sadb_msg_satype));
6752 pfkeystat.out_invsatype++;
6757 /* check field of upper layer protocol and address family */
6758 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
6759 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
6760 struct sadb_address *src0, *dst0;
6763 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
6764 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
6766 /* check upper layer protocol */
6767 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
6768 ipseclog((LOG_DEBUG, "%s: upper layer protocol "
6769 "mismatched.\n", __func__));
6770 pfkeystat.out_invaddr++;
6776 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
6777 PFKEY_ADDR_SADDR(dst0)->sa_family) {
6778 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
6780 pfkeystat.out_invaddr++;
6784 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6785 PFKEY_ADDR_SADDR(dst0)->sa_len) {
6786 ipseclog((LOG_DEBUG, "%s: address struct size "
6787 "mismatched.\n", __func__));
6788 pfkeystat.out_invaddr++;
6793 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6795 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6796 sizeof(struct sockaddr_in)) {
6797 pfkeystat.out_invaddr++;
6803 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6804 sizeof(struct sockaddr_in6)) {
6805 pfkeystat.out_invaddr++;
6811 ipseclog((LOG_DEBUG, "%s: unsupported address family\n",
6813 pfkeystat.out_invaddr++;
6814 error = EAFNOSUPPORT;
6818 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6820 plen = sizeof(struct in_addr) << 3;
6823 plen = sizeof(struct in6_addr) << 3;
6826 plen = 0; /*fool gcc*/
6830 /* check max prefix length */
6831 if (src0->sadb_address_prefixlen > plen ||
6832 dst0->sadb_address_prefixlen > plen) {
6833 ipseclog((LOG_DEBUG, "%s: illegal prefixlen.\n",
6835 pfkeystat.out_invaddr++;
6841 * prefixlen == 0 is valid because there can be a case when
6842 * all addresses are matched.
6846 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
6847 key_typesw[msg->sadb_msg_type] == NULL) {
6848 pfkeystat.out_invmsgtype++;
6853 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
6856 msg->sadb_msg_errno = error;
6857 return key_sendup_mbuf(so, m, target);
6861 key_senderror(so, m, code)
6866 struct sadb_msg *msg;
6868 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
6869 ("mbuf too small, len %u", m->m_len));
6871 msg = mtod(m, struct sadb_msg *);
6872 msg->sadb_msg_errno = code;
6873 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
6877 * set the pointer to each header into message buffer.
6878 * m will be freed on error.
6879 * XXX larger-than-MCLBYTES extension?
6884 struct sadb_msghdr *mhp;
6887 struct sadb_ext *ext;
6892 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6893 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6894 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
6895 ("mbuf too small, len %u", m->m_len));
6898 bzero(mhp, sizeof(*mhp));
6900 mhp->msg = mtod(m, struct sadb_msg *);
6901 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
6903 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6904 extlen = end; /*just in case extlen is not updated*/
6905 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
6906 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
6908 /* m is already freed */
6911 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
6914 switch (ext->sadb_ext_type) {
6916 case SADB_EXT_ADDRESS_SRC:
6917 case SADB_EXT_ADDRESS_DST:
6918 case SADB_EXT_ADDRESS_PROXY:
6919 case SADB_EXT_LIFETIME_CURRENT:
6920 case SADB_EXT_LIFETIME_HARD:
6921 case SADB_EXT_LIFETIME_SOFT:
6922 case SADB_EXT_KEY_AUTH:
6923 case SADB_EXT_KEY_ENCRYPT:
6924 case SADB_EXT_IDENTITY_SRC:
6925 case SADB_EXT_IDENTITY_DST:
6926 case SADB_EXT_SENSITIVITY:
6927 case SADB_EXT_PROPOSAL:
6928 case SADB_EXT_SUPPORTED_AUTH:
6929 case SADB_EXT_SUPPORTED_ENCRYPT:
6930 case SADB_EXT_SPIRANGE:
6931 case SADB_X_EXT_POLICY:
6932 case SADB_X_EXT_SA2:
6933 /* duplicate check */
6935 * XXX Are there duplication payloads of either
6936 * KEY_AUTH or KEY_ENCRYPT ?
6938 if (mhp->ext[ext->sadb_ext_type] != NULL) {
6939 ipseclog((LOG_DEBUG, "%s: duplicate ext_type "
6940 "%u\n", __func__, ext->sadb_ext_type));
6942 pfkeystat.out_dupext++;
6947 ipseclog((LOG_DEBUG, "%s: invalid ext_type %u\n",
6948 __func__, ext->sadb_ext_type));
6950 pfkeystat.out_invexttype++;
6954 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
6956 if (key_validate_ext(ext, extlen)) {
6958 pfkeystat.out_invlen++;
6962 n = m_pulldown(m, off, extlen, &toff);
6964 /* m is already freed */
6967 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
6969 mhp->ext[ext->sadb_ext_type] = ext;
6970 mhp->extoff[ext->sadb_ext_type] = off;
6971 mhp->extlen[ext->sadb_ext_type] = extlen;
6976 pfkeystat.out_invlen++;
6984 key_validate_ext(ext, len)
6985 const struct sadb_ext *ext;
6988 const struct sockaddr *sa;
6989 enum { NONE, ADDR } checktype = NONE;
6991 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
6993 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
6996 /* if it does not match minimum/maximum length, bail */
6997 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
6998 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
7000 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
7002 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
7005 /* more checks based on sadb_ext_type XXX need more */
7006 switch (ext->sadb_ext_type) {
7007 case SADB_EXT_ADDRESS_SRC:
7008 case SADB_EXT_ADDRESS_DST:
7009 case SADB_EXT_ADDRESS_PROXY:
7010 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7013 case SADB_EXT_IDENTITY_SRC:
7014 case SADB_EXT_IDENTITY_DST:
7015 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7016 SADB_X_IDENTTYPE_ADDR) {
7017 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7027 switch (checktype) {
7031 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
7032 if (len < baselen + sal)
7034 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7048 REGTREE_LOCK_INIT();
7049 SAHTREE_LOCK_INIT();
7053 for (i = 0; i < IPSEC_DIR_MAX; i++)
7054 LIST_INIT(&sptree[i]);
7056 LIST_INIT(&sahtree);
7058 for (i = 0; i <= SADB_SATYPE_MAX; i++)
7059 LIST_INIT(®tree[i]);
7061 LIST_INIT(&acqtree);
7062 LIST_INIT(&spacqtree);
7064 /* system default */
7065 ip4_def_policy.policy = IPSEC_POLICY_NONE;
7066 ip4_def_policy.refcnt++; /*never reclaim this*/
7068 #ifndef IPSEC_DEBUG2
7069 timeout((void *)key_timehandler, (void *)0, hz);
7070 #endif /*IPSEC_DEBUG2*/
7072 /* initialize key statistics */
7073 keystat.getspi_count = 1;
7075 printf("Fast IPsec: Initialized Security Association Processing.\n");
7081 * XXX: maybe This function is called after INBOUND IPsec processing.
7083 * Special check for tunnel-mode packets.
7084 * We must make some checks for consistency between inner and outer IP header.
7086 * xxx more checks to be provided
7089 key_checktunnelsanity(sav, family, src, dst)
7090 struct secasvar *sav;
7095 IPSEC_ASSERT(sav->sah != NULL, ("null SA header"));
7097 /* XXX: check inner IP header */
7102 /* record data transfer on SA, and update timestamps */
7104 key_sa_recordxfer(sav, m)
7105 struct secasvar *sav;
7108 IPSEC_ASSERT(sav != NULL, ("Null secasvar"));
7109 IPSEC_ASSERT(m != NULL, ("Null mbuf"));
7114 * XXX Currently, there is a difference of bytes size
7115 * between inbound and outbound processing.
7117 sav->lft_c->sadb_lifetime_bytes += m->m_pkthdr.len;
7118 /* to check bytes lifetime is done in key_timehandler(). */
7121 * We use the number of packets as the unit of
7122 * sadb_lifetime_allocations. We increment the variable
7123 * whenever {esp,ah}_{in,out}put is called.
7125 sav->lft_c->sadb_lifetime_allocations++;
7126 /* XXX check for expires? */
7129 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7130 * in seconds. HARD and SOFT lifetime are measured by the time
7131 * difference (again in seconds) from sadb_lifetime_usetime.
7135 * -----+-----+--------+---> t
7136 * <--------------> HARD
7139 sav->lft_c->sadb_lifetime_usetime = time_second;
7140 /* XXX check for expires? */
7147 key_sa_routechange(dst)
7148 struct sockaddr *dst;
7150 struct secashead *sah;
7154 LIST_FOREACH(sah, &sahtree, chain) {
7155 ro = &sah->sa_route;
7156 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7157 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7159 ro->ro_rt = (struct rtentry *)NULL;
7166 key_sa_chgstate(sav, state)
7167 struct secasvar *sav;
7170 IPSEC_ASSERT(sav != NULL, ("NULL sav"));
7171 SAHTREE_LOCK_ASSERT();
7173 if (sav->state != state) {
7174 if (__LIST_CHAINED(sav))
7175 LIST_REMOVE(sav, chain);
7177 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7183 struct secasvar *sav;
7186 IPSEC_ASSERT(sav->iv != NULL, ("null IV"));
7187 key_randomfill(sav->iv, sav->ivlen);
7191 static struct mbuf *
7195 struct mbuf *m = NULL, *n;
7200 MGET(n, M_DONTWAIT, MT_DATA);
7201 if (n && len > MLEN)
7202 MCLGET(n, M_DONTWAIT);
7210 n->m_len = M_TRAILINGSPACE(n);
7211 /* use the bottom of mbuf, hoping we can prepend afterwards */
7212 if (n->m_len > len) {
7213 t = (n->m_len - len) & ~(sizeof(long) - 1);