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/syslog.h>
60 #include <net/route.h>
61 #include <net/raw_cb.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #include <netinet/in_var.h>
69 #include <netinet/ip6.h>
70 #include <netinet6/in6_var.h>
71 #include <netinet6/ip6_var.h>
75 #include <netinet/in_pcb.h>
78 #include <netinet6/in6_pcb.h>
81 #include <net/pfkeyv2.h>
82 #include <netipsec/keydb.h>
83 #include <netipsec/key.h>
84 #include <netipsec/keysock.h>
85 #include <netipsec/key_debug.h>
87 #include <netipsec/ipsec.h>
89 #include <netipsec/ipsec6.h>
92 #include <netipsec/xform.h>
94 #include <machine/stdarg.h>
97 #include <sys/random.h>
100 #define _BITS(bytes) ((bytes) << 3)
103 * Note on SA reference counting:
104 * - SAs that are not in DEAD state will have (total external reference + 1)
105 * following value in reference count field. they cannot be freed and are
106 * referenced from SA header.
107 * - SAs that are in DEAD state will have (total external reference)
108 * in reference count field. they are ready to be freed. reference from
109 * SA header will be removed in key_delsav(), when the reference count
110 * field hits 0 (= no external reference other than from SA header.
113 u_int32_t key_debug_level = 0;
114 static u_int key_spi_trycnt = 1000;
115 static u_int32_t key_spi_minval = 0x100;
116 static u_int32_t key_spi_maxval = 0x0fffffff; /* XXX */
117 static u_int32_t policy_id = 0;
118 static u_int key_int_random = 60; /*interval to initialize randseed,1(m)*/
119 static u_int key_larval_lifetime = 30; /* interval to expire acquiring, 30(s)*/
120 static int key_blockacq_count = 10; /* counter for blocking SADB_ACQUIRE.*/
121 static int key_blockacq_lifetime = 20; /* lifetime for blocking SADB_ACQUIRE.*/
122 static int key_preferred_oldsa = 1; /* preferred old sa rather than new sa.*/
124 static u_int32_t acq_seq = 0;
126 static LIST_HEAD(_sptree, secpolicy) sptree[IPSEC_DIR_MAX]; /* SPD */
127 static struct mtx sptree_lock;
128 #define SPTREE_LOCK_INIT() \
129 mtx_init(&sptree_lock, "sptree", \
130 "fast ipsec security policy database", MTX_DEF)
131 #define SPTREE_LOCK_DESTROY() mtx_destroy(&sptree_lock)
132 #define SPTREE_LOCK() mtx_lock(&sptree_lock)
133 #define SPTREE_UNLOCK() mtx_unlock(&sptree_lock)
134 #define SPTREE_LOCK_ASSERT() mtx_assert(&sptree_lock, MA_OWNED)
136 static LIST_HEAD(_sahtree, secashead) sahtree; /* SAD */
137 static struct mtx sahtree_lock;
138 #define SAHTREE_LOCK_INIT() \
139 mtx_init(&sahtree_lock, "sahtree", \
140 "fast ipsec security association database", MTX_DEF)
141 #define SAHTREE_LOCK_DESTROY() mtx_destroy(&sahtree_lock)
142 #define SAHTREE_LOCK() mtx_lock(&sahtree_lock)
143 #define SAHTREE_UNLOCK() mtx_unlock(&sahtree_lock)
144 #define SAHTREE_LOCK_ASSERT() mtx_assert(&sahtree_lock, MA_OWNED)
147 static LIST_HEAD(_regtree, secreg) regtree[SADB_SATYPE_MAX + 1];
148 static struct mtx regtree_lock;
149 #define REGTREE_LOCK_INIT() \
150 mtx_init(®tree_lock, "regtree", "fast ipsec regtree", MTX_DEF)
151 #define REGTREE_LOCK_DESTROY() mtx_destroy(®tree_lock)
152 #define REGTREE_LOCK() mtx_lock(®tree_lock)
153 #define REGTREE_UNLOCK() mtx_unlock(®tree_lock)
154 #define REGTREE_LOCK_ASSERT() mtx_assert(®tree_lock, MA_OWNED)
156 static LIST_HEAD(_acqtree, secacq) acqtree; /* acquiring list */
157 static struct mtx acq_lock;
158 #define ACQ_LOCK_INIT() \
159 mtx_init(&acq_lock, "acqtree", "fast ipsec acquire list", MTX_DEF)
160 #define ACQ_LOCK_DESTROY() mtx_destroy(&acq_lock)
161 #define ACQ_LOCK() mtx_lock(&acq_lock)
162 #define ACQ_UNLOCK() mtx_unlock(&acq_lock)
163 #define ACQ_LOCK_ASSERT() mtx_assert(&acq_lock, MA_OWNED)
165 static LIST_HEAD(_spacqtree, secspacq) spacqtree; /* SP acquiring list */
166 static struct mtx spacq_lock;
167 #define SPACQ_LOCK_INIT() \
168 mtx_init(&spacq_lock, "spacqtree", \
169 "fast ipsec security policy acquire list", MTX_DEF)
170 #define SPACQ_LOCK_DESTROY() mtx_destroy(&spacq_lock)
171 #define SPACQ_LOCK() mtx_lock(&spacq_lock)
172 #define SPACQ_UNLOCK() mtx_unlock(&spacq_lock)
173 #define SPACQ_LOCK_ASSERT() mtx_assert(&spacq_lock, MA_OWNED)
175 /* search order for SAs */
176 static const u_int saorder_state_valid_prefer_old[] = {
177 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
179 static const u_int saorder_state_valid_prefer_new[] = {
180 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
182 static u_int saorder_state_alive[] = {
184 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
186 static u_int saorder_state_any[] = {
187 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
188 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
191 static const int minsize[] = {
192 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
193 sizeof(struct sadb_sa), /* SADB_EXT_SA */
194 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
195 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
196 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
197 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
198 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
199 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
200 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
201 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
202 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
203 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
204 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
205 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
206 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
207 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
208 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
209 0, /* SADB_X_EXT_KMPRIVATE */
210 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
211 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
213 static const int maxsize[] = {
214 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
215 sizeof(struct sadb_sa), /* SADB_EXT_SA */
216 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
217 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
218 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
219 0, /* SADB_EXT_ADDRESS_SRC */
220 0, /* SADB_EXT_ADDRESS_DST */
221 0, /* SADB_EXT_ADDRESS_PROXY */
222 0, /* SADB_EXT_KEY_AUTH */
223 0, /* SADB_EXT_KEY_ENCRYPT */
224 0, /* SADB_EXT_IDENTITY_SRC */
225 0, /* SADB_EXT_IDENTITY_DST */
226 0, /* SADB_EXT_SENSITIVITY */
227 0, /* SADB_EXT_PROPOSAL */
228 0, /* SADB_EXT_SUPPORTED_AUTH */
229 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
230 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
231 0, /* SADB_X_EXT_KMPRIVATE */
232 0, /* SADB_X_EXT_POLICY */
233 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
236 static int ipsec_esp_keymin = 256;
237 static int ipsec_esp_auth = 0;
238 static int ipsec_ah_keymin = 128;
241 SYSCTL_DECL(_net_key);
244 SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug, CTLFLAG_RW, \
245 &key_debug_level, 0, "");
247 /* max count of trial for the decision of spi value */
248 SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt, CTLFLAG_RW, \
249 &key_spi_trycnt, 0, "");
251 /* minimum spi value to allocate automatically. */
252 SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval, CTLFLAG_RW, \
253 &key_spi_minval, 0, "");
255 /* maximun spi value to allocate automatically. */
256 SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval, CTLFLAG_RW, \
257 &key_spi_maxval, 0, "");
259 /* interval to initialize randseed */
260 SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random, CTLFLAG_RW, \
261 &key_int_random, 0, "");
263 /* lifetime for larval SA */
264 SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime, CTLFLAG_RW, \
265 &key_larval_lifetime, 0, "");
267 /* counter for blocking to send SADB_ACQUIRE to IKEd */
268 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count, CTLFLAG_RW, \
269 &key_blockacq_count, 0, "");
271 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
272 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime, CTLFLAG_RW, \
273 &key_blockacq_lifetime, 0, "");
276 SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth, CTLFLAG_RW, \
277 &ipsec_esp_auth, 0, "");
279 /* minimum ESP key length */
280 SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin, CTLFLAG_RW, \
281 &ipsec_esp_keymin, 0, "");
283 /* minimum AH key length */
284 SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin, CTLFLAG_RW, \
285 &ipsec_ah_keymin, 0, "");
287 /* perfered old SA rather than new SA */
288 SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, preferred_oldsa, CTLFLAG_RW,\
289 &key_preferred_oldsa, 0, "");
291 #define __LIST_CHAINED(elm) \
292 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
293 #define LIST_INSERT_TAIL(head, elm, type, field) \
295 struct type *curelm = LIST_FIRST(head); \
296 if (curelm == NULL) {\
297 LIST_INSERT_HEAD(head, elm, field); \
299 while (LIST_NEXT(curelm, field)) \
300 curelm = LIST_NEXT(curelm, field);\
301 LIST_INSERT_AFTER(curelm, elm, field);\
305 #define KEY_CHKSASTATE(head, sav, name) \
307 if ((head) != (sav)) { \
308 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
309 (name), (head), (sav))); \
314 #define KEY_CHKSPDIR(head, sp, name) \
316 if ((head) != (sp)) { \
317 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
318 "anyway continue.\n", \
319 (name), (head), (sp))); \
323 MALLOC_DEFINE(M_IPSEC_SA, "secasvar", "ipsec security association");
324 MALLOC_DEFINE(M_IPSEC_SAH, "sahead", "ipsec sa head");
325 MALLOC_DEFINE(M_IPSEC_SP, "ipsecpolicy", "ipsec security policy");
326 MALLOC_DEFINE(M_IPSEC_SR, "ipsecrequest", "ipsec security request");
327 MALLOC_DEFINE(M_IPSEC_MISC, "ipsec-misc", "ipsec miscellaneous");
328 MALLOC_DEFINE(M_IPSEC_SAQ, "ipsec-saq", "ipsec sa acquire");
329 MALLOC_DEFINE(M_IPSEC_SAR, "ipsec-reg", "ipsec sa acquire");
332 * set parameters into secpolicyindex buffer.
333 * Must allocate secpolicyindex buffer passed to this function.
335 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
337 bzero((idx), sizeof(struct secpolicyindex)); \
338 (idx)->dir = (_dir); \
339 (idx)->prefs = (ps); \
340 (idx)->prefd = (pd); \
341 (idx)->ul_proto = (ulp); \
342 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
343 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
347 * set parameters into secasindex buffer.
348 * Must allocate secasindex buffer before calling this function.
350 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
352 bzero((idx), sizeof(struct secasindex)); \
353 (idx)->proto = (p); \
355 (idx)->reqid = (r); \
356 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
357 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
362 u_long getspi_count; /* the avarage of count to try to get new SPI */
366 struct sadb_msg *msg;
367 struct sadb_ext *ext[SADB_EXT_MAX + 1];
368 int extoff[SADB_EXT_MAX + 1];
369 int extlen[SADB_EXT_MAX + 1];
372 static struct secasvar *key_allocsa_policy __P((const struct secasindex *));
373 static void key_freesp_so __P((struct secpolicy **));
374 static struct secasvar *key_do_allocsa_policy __P((struct secashead *, u_int));
375 static void key_delsp __P((struct secpolicy *));
376 static struct secpolicy *key_getsp __P((struct secpolicyindex *));
377 static void _key_delsp(struct secpolicy *sp);
378 static struct secpolicy *key_getspbyid __P((u_int32_t));
379 static u_int32_t key_newreqid __P((void));
380 static struct mbuf *key_gather_mbuf __P((struct mbuf *,
381 const struct sadb_msghdr *, int, int, ...));
382 static int key_spdadd __P((struct socket *, struct mbuf *,
383 const struct sadb_msghdr *));
384 static u_int32_t key_getnewspid __P((void));
385 static int key_spddelete __P((struct socket *, struct mbuf *,
386 const struct sadb_msghdr *));
387 static int key_spddelete2 __P((struct socket *, struct mbuf *,
388 const struct sadb_msghdr *));
389 static int key_spdget __P((struct socket *, struct mbuf *,
390 const struct sadb_msghdr *));
391 static int key_spdflush __P((struct socket *, struct mbuf *,
392 const struct sadb_msghdr *));
393 static int key_spddump __P((struct socket *, struct mbuf *,
394 const struct sadb_msghdr *));
395 static struct mbuf *key_setdumpsp __P((struct secpolicy *,
396 u_int8_t, u_int32_t, u_int32_t));
397 static u_int key_getspreqmsglen __P((struct secpolicy *));
398 static int key_spdexpire __P((struct secpolicy *));
399 static struct secashead *key_newsah __P((struct secasindex *));
400 static void key_delsah __P((struct secashead *));
401 static struct secasvar *key_newsav __P((struct mbuf *,
402 const struct sadb_msghdr *, struct secashead *, int *,
404 #define KEY_NEWSAV(m, sadb, sah, e) \
405 key_newsav(m, sadb, sah, e, __FILE__, __LINE__)
406 static void key_delsav __P((struct secasvar *));
407 static struct secashead *key_getsah __P((struct secasindex *));
408 static struct secasvar *key_checkspidup __P((struct secasindex *, u_int32_t));
409 static struct secasvar *key_getsavbyspi __P((struct secashead *, u_int32_t));
410 static int key_setsaval __P((struct secasvar *, struct mbuf *,
411 const struct sadb_msghdr *));
412 static int key_mature __P((struct secasvar *));
413 static struct mbuf *key_setdumpsa __P((struct secasvar *, u_int8_t,
414 u_int8_t, u_int32_t, u_int32_t));
415 static struct mbuf *key_setsadbmsg __P((u_int8_t, u_int16_t, u_int8_t,
416 u_int32_t, pid_t, u_int16_t));
417 static struct mbuf *key_setsadbsa __P((struct secasvar *));
418 static struct mbuf *key_setsadbaddr __P((u_int16_t,
419 const struct sockaddr *, u_int8_t, u_int16_t));
420 static struct mbuf *key_setsadbxsa2 __P((u_int8_t, u_int32_t, u_int32_t));
421 static struct mbuf *key_setsadbxpolicy __P((u_int16_t, u_int8_t,
423 static void *key_dup(const void *, u_int, struct malloc_type *);
425 static int key_ismyaddr6 __P((struct sockaddr_in6 *));
428 /* flags for key_cmpsaidx() */
429 #define CMP_HEAD 1 /* protocol, addresses. */
430 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
431 #define CMP_REQID 3 /* additionally HEAD, reaid. */
432 #define CMP_EXACTLY 4 /* all elements. */
433 static int key_cmpsaidx
434 __P((const struct secasindex *, const struct secasindex *, int));
436 static int key_cmpspidx_exactly
437 __P((struct secpolicyindex *, struct secpolicyindex *));
438 static int key_cmpspidx_withmask
439 __P((struct secpolicyindex *, struct secpolicyindex *));
440 static int key_sockaddrcmp __P((const struct sockaddr *, const struct sockaddr *, int));
441 static int key_bbcmp __P((const void *, const void *, u_int));
442 static u_int16_t key_satype2proto __P((u_int8_t));
443 static u_int8_t key_proto2satype __P((u_int16_t));
445 static int key_getspi __P((struct socket *, struct mbuf *,
446 const struct sadb_msghdr *));
447 static u_int32_t key_do_getnewspi __P((struct sadb_spirange *,
448 struct secasindex *));
449 static int key_update __P((struct socket *, struct mbuf *,
450 const struct sadb_msghdr *));
451 #ifdef IPSEC_DOSEQCHECK
452 static struct secasvar *key_getsavbyseq __P((struct secashead *, u_int32_t));
454 static int key_add __P((struct socket *, struct mbuf *,
455 const struct sadb_msghdr *));
456 static int key_setident __P((struct secashead *, struct mbuf *,
457 const struct sadb_msghdr *));
458 static struct mbuf *key_getmsgbuf_x1 __P((struct mbuf *,
459 const struct sadb_msghdr *));
460 static int key_delete __P((struct socket *, struct mbuf *,
461 const struct sadb_msghdr *));
462 static int key_get __P((struct socket *, struct mbuf *,
463 const struct sadb_msghdr *));
465 static void key_getcomb_setlifetime __P((struct sadb_comb *));
466 static struct mbuf *key_getcomb_esp __P((void));
467 static struct mbuf *key_getcomb_ah __P((void));
468 static struct mbuf *key_getcomb_ipcomp __P((void));
469 static struct mbuf *key_getprop __P((const struct secasindex *));
471 static int key_acquire __P((const struct secasindex *, struct secpolicy *));
472 static struct secacq *key_newacq __P((const struct secasindex *));
473 static struct secacq *key_getacq __P((const struct secasindex *));
474 static struct secacq *key_getacqbyseq __P((u_int32_t));
475 static struct secspacq *key_newspacq __P((struct secpolicyindex *));
476 static struct secspacq *key_getspacq __P((struct secpolicyindex *));
477 static int key_acquire2 __P((struct socket *, struct mbuf *,
478 const struct sadb_msghdr *));
479 static int key_register __P((struct socket *, struct mbuf *,
480 const struct sadb_msghdr *));
481 static int key_expire __P((struct secasvar *));
482 static int key_flush __P((struct socket *, struct mbuf *,
483 const struct sadb_msghdr *));
484 static int key_dump __P((struct socket *, struct mbuf *,
485 const struct sadb_msghdr *));
486 static int key_promisc __P((struct socket *, struct mbuf *,
487 const struct sadb_msghdr *));
488 static int key_senderror __P((struct socket *, struct mbuf *, int));
489 static int key_validate_ext __P((const struct sadb_ext *, int));
490 static int key_align __P((struct mbuf *, struct sadb_msghdr *));
492 static const char *key_getfqdn __P((void));
493 static const char *key_getuserfqdn __P((void));
495 static void key_sa_chgstate __P((struct secasvar *, u_int8_t));
496 static struct mbuf *key_alloc_mbuf __P((int));
498 #define SA_ADDREF(p) do { \
500 IPSEC_ASSERT((p)->refcnt != 0, ("SA refcnt overflow")); \
502 #define SA_DELREF(p) do { \
503 IPSEC_ASSERT((p)->refcnt > 0, ("SA refcnt underflow")); \
507 #define SP_ADDREF(p) do { \
509 IPSEC_ASSERT((p)->refcnt != 0, ("SP refcnt overflow")); \
511 #define SP_DELREF(p) do { \
512 IPSEC_ASSERT((p)->refcnt > 0, ("SP refcnt underflow")); \
518 * Update the refcnt while holding the SPTREE lock.
521 key_addref(struct secpolicy *sp)
529 * Return 0 when there are known to be no SP's for the specified
530 * direction. Otherwise return 1. This is used by IPsec code
531 * to optimize performance.
534 key_havesp(u_int dir)
536 return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ?
537 LIST_FIRST(&sptree[dir]) != NULL : 1);
540 /* %%% IPsec policy management */
542 * allocating a SP for OUTBOUND or INBOUND packet.
543 * Must call key_freesp() later.
544 * OUT: NULL: not found
545 * others: found and return the pointer.
548 key_allocsp(struct secpolicyindex *spidx, u_int dir, const char* where, int tag)
550 struct secpolicy *sp;
552 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
553 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
554 ("invalid direction %u", dir));
556 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
557 printf("DP %s from %s:%u\n", __func__, where, tag));
560 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
561 printf("*** objects\n");
562 kdebug_secpolicyindex(spidx));
565 LIST_FOREACH(sp, &sptree[dir], chain) {
566 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
567 printf("*** in SPD\n");
568 kdebug_secpolicyindex(&sp->spidx));
570 if (sp->state == IPSEC_SPSTATE_DEAD)
572 if (key_cmpspidx_withmask(&sp->spidx, spidx))
579 KEY_CHKSPDIR(sp->spidx.dir, dir, __func__);
581 /* found a SPD entry */
582 sp->lastused = time_second;
587 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
588 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
589 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
594 * allocating a SP for OUTBOUND or INBOUND packet.
595 * Must call key_freesp() later.
596 * OUT: NULL: not found
597 * others: found and return the pointer.
600 key_allocsp2(u_int32_t spi,
601 union sockaddr_union *dst,
604 const char* where, int tag)
606 struct secpolicy *sp;
608 IPSEC_ASSERT(dst != NULL, ("null dst"));
609 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
610 ("invalid direction %u", dir));
612 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
613 printf("DP %s from %s:%u\n", __func__, where, tag));
616 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
617 printf("*** objects\n");
618 printf("spi %u proto %u dir %u\n", spi, proto, dir);
619 kdebug_sockaddr(&dst->sa));
622 LIST_FOREACH(sp, &sptree[dir], chain) {
623 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
624 printf("*** in SPD\n");
625 kdebug_secpolicyindex(&sp->spidx));
627 if (sp->state == IPSEC_SPSTATE_DEAD)
629 /* compare simple values, then dst address */
630 if (sp->spidx.ul_proto != proto)
632 /* NB: spi's must exist and match */
633 if (!sp->req || !sp->req->sav || sp->req->sav->spi != spi)
635 if (key_sockaddrcmp(&sp->spidx.dst.sa, &dst->sa, 1) == 0)
642 KEY_CHKSPDIR(sp->spidx.dir, dir, __func__);
644 /* found a SPD entry */
645 sp->lastused = time_second;
650 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
651 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
652 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
657 * return a policy that matches this particular inbound packet.
661 key_gettunnel(const struct sockaddr *osrc,
662 const struct sockaddr *odst,
663 const struct sockaddr *isrc,
664 const struct sockaddr *idst,
665 const char* where, int tag)
667 struct secpolicy *sp;
668 const int dir = IPSEC_DIR_INBOUND;
669 struct ipsecrequest *r1, *r2, *p;
670 struct secpolicyindex spidx;
672 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
673 printf("DP %s from %s:%u\n", __func__, where, tag));
675 if (isrc->sa_family != idst->sa_family) {
676 ipseclog((LOG_ERR, "%s: protocol family mismatched %d != %d\n.",
677 __func__, isrc->sa_family, idst->sa_family));
683 LIST_FOREACH(sp, &sptree[dir], chain) {
684 if (sp->state == IPSEC_SPSTATE_DEAD)
688 for (p = sp->req; p; p = p->next) {
689 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
696 /* here we look at address matches only */
698 if (isrc->sa_len > sizeof(spidx.src) ||
699 idst->sa_len > sizeof(spidx.dst))
701 bcopy(isrc, &spidx.src, isrc->sa_len);
702 bcopy(idst, &spidx.dst, idst->sa_len);
703 if (!key_cmpspidx_withmask(&sp->spidx, &spidx))
706 if (key_sockaddrcmp(&r1->saidx.src.sa, isrc, 0) ||
707 key_sockaddrcmp(&r1->saidx.dst.sa, idst, 0))
711 if (key_sockaddrcmp(&r2->saidx.src.sa, osrc, 0) ||
712 key_sockaddrcmp(&r2->saidx.dst.sa, odst, 0))
721 sp->lastused = time_second;
726 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
727 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
728 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
733 * allocating an SA entry for an *OUTBOUND* packet.
734 * checking each request entries in SP, and acquire an SA if need.
735 * OUT: 0: there are valid requests.
736 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
739 key_checkrequest(struct ipsecrequest *isr, const struct secasindex *saidx)
744 IPSEC_ASSERT(isr != NULL, ("null isr"));
745 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
746 IPSEC_ASSERT(saidx->mode == IPSEC_MODE_TRANSPORT ||
747 saidx->mode == IPSEC_MODE_TUNNEL,
748 ("unexpected policy %u", saidx->mode));
751 * XXX guard against protocol callbacks from the crypto
752 * thread as they reference ipsecrequest.sav which we
753 * temporarily null out below. Need to rethink how we
754 * handle bundled SA's in the callback thread.
756 IPSECREQUEST_LOCK_ASSERT(isr);
758 /* get current level */
759 level = ipsec_get_reqlevel(isr);
762 * We do allocate new SA only if the state of SA in the holder is
763 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
765 if (isr->sav != NULL) {
766 if (isr->sav->sah == NULL)
767 panic("%s: sah is null.\n", __func__);
768 if (isr->sav == (struct secasvar *)LIST_FIRST(
769 &isr->sav->sah->savtree[SADB_SASTATE_DEAD])) {
770 KEY_FREESAV(&isr->sav);
776 * we free any SA stashed in the IPsec request because a different
777 * SA may be involved each time this request is checked, either
778 * because new SAs are being configured, or this request is
779 * associated with an unconnected datagram socket, or this request
780 * is associated with a system default policy.
782 * The operation may have negative impact to performance. We may
783 * want to check cached SA carefully, rather than picking new SA
786 if (isr->sav != NULL) {
787 KEY_FREESAV(&isr->sav);
793 * new SA allocation if no SA found.
794 * key_allocsa_policy should allocate the oldest SA available.
795 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
797 if (isr->sav == NULL)
798 isr->sav = key_allocsa_policy(saidx);
800 /* When there is SA. */
801 if (isr->sav != NULL) {
802 if (isr->sav->state != SADB_SASTATE_MATURE &&
803 isr->sav->state != SADB_SASTATE_DYING)
809 error = key_acquire(saidx, isr->sp);
811 /* XXX What should I do ? */
812 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
817 if (level != IPSEC_LEVEL_REQUIRE) {
818 /* XXX sigh, the interface to this routine is botched */
819 IPSEC_ASSERT(isr->sav == NULL, ("unexpected SA"));
827 * allocating a SA for policy entry from SAD.
828 * NOTE: searching SAD of aliving state.
829 * OUT: NULL: not found.
830 * others: found and return the pointer.
832 static struct secasvar *
833 key_allocsa_policy(const struct secasindex *saidx)
835 #define N(a) _ARRAYLEN(a)
836 struct secashead *sah;
837 struct secasvar *sav;
838 u_int stateidx, arraysize;
839 const u_int *state_valid;
842 LIST_FOREACH(sah, &sahtree, chain) {
843 if (sah->state == SADB_SASTATE_DEAD)
845 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID)) {
846 if (key_preferred_oldsa) {
847 state_valid = saorder_state_valid_prefer_old;
848 arraysize = N(saorder_state_valid_prefer_old);
850 state_valid = saorder_state_valid_prefer_new;
851 arraysize = N(saorder_state_valid_prefer_new);
862 /* search valid state */
863 for (stateidx = 0; stateidx < arraysize; stateidx++) {
864 sav = key_do_allocsa_policy(sah, state_valid[stateidx]);
874 * searching SAD with direction, protocol, mode and state.
875 * called by key_allocsa_policy().
878 * others : found, pointer to a SA.
880 static struct secasvar *
881 key_do_allocsa_policy(struct secashead *sah, u_int state)
883 struct secasvar *sav, *nextsav, *candidate, *d;
889 for (sav = LIST_FIRST(&sah->savtree[state]);
893 nextsav = LIST_NEXT(sav, chain);
896 KEY_CHKSASTATE(sav->state, state, __func__);
899 if (candidate == NULL) {
904 /* Which SA is the better ? */
906 IPSEC_ASSERT(candidate->lft_c != NULL,
907 ("null candidate lifetime"));
908 IPSEC_ASSERT(sav->lft_c != NULL, ("null sav lifetime"));
910 /* What the best method is to compare ? */
911 if (key_preferred_oldsa) {
912 if (candidate->lft_c->sadb_lifetime_addtime >
913 sav->lft_c->sadb_lifetime_addtime) {
920 /* preferred new sa rather than old sa */
921 if (candidate->lft_c->sadb_lifetime_addtime <
922 sav->lft_c->sadb_lifetime_addtime) {
929 * prepared to delete the SA when there is more
930 * suitable candidate and the lifetime of the SA is not
933 if (d->lft_c->sadb_lifetime_addtime != 0) {
934 struct mbuf *m, *result;
937 key_sa_chgstate(d, SADB_SASTATE_DEAD);
939 IPSEC_ASSERT(d->refcnt > 0, ("bogus ref count"));
941 satype = key_proto2satype(d->sah->saidx.proto);
945 m = key_setsadbmsg(SADB_DELETE, 0,
946 satype, 0, 0, d->refcnt - 1);
951 /* set sadb_address for saidx's. */
952 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
953 &d->sah->saidx.src.sa,
954 d->sah->saidx.src.sa.sa_len << 3,
960 /* set sadb_address for saidx's. */
961 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
962 &d->sah->saidx.dst.sa,
963 d->sah->saidx.dst.sa.sa_len << 3,
969 /* create SA extension */
970 m = key_setsadbsa(d);
975 if (result->m_len < sizeof(struct sadb_msg)) {
976 result = m_pullup(result,
977 sizeof(struct sadb_msg));
982 result->m_pkthdr.len = 0;
983 for (m = result; m; m = m->m_next)
984 result->m_pkthdr.len += m->m_len;
985 mtod(result, struct sadb_msg *)->sadb_msg_len =
986 PFKEY_UNIT64(result->m_pkthdr.len);
988 if (key_sendup_mbuf(NULL, result,
989 KEY_SENDUP_REGISTERED))
996 SA_ADDREF(candidate);
997 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
998 printf("DP %s cause refcnt++:%d SA:%p\n",
999 __func__, candidate->refcnt, candidate));
1007 * allocating a usable SA entry for a *INBOUND* packet.
1008 * Must call key_freesav() later.
1009 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
1010 * NULL: not found, or error occured.
1012 * In the comparison, no source address is used--for RFC2401 conformance.
1013 * To quote, from section 4.1:
1014 * A security association is uniquely identified by a triple consisting
1015 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1016 * security protocol (AH or ESP) identifier.
1017 * Note that, however, we do need to keep source address in IPsec SA.
1018 * IKE specification and PF_KEY specification do assume that we
1019 * keep source address in IPsec SA. We see a tricky situation here.
1023 union sockaddr_union *dst,
1026 const char* where, int tag)
1028 struct secashead *sah;
1029 struct secasvar *sav;
1030 u_int stateidx, arraysize, state;
1031 const u_int *saorder_state_valid;
1033 IPSEC_ASSERT(dst != NULL, ("null dst address"));
1035 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1036 printf("DP %s from %s:%u\n", __func__, where, tag));
1040 * XXX: to be checked internal IP header somewhere. Also when
1041 * IPsec tunnel packet is received. But ESP tunnel mode is
1042 * encrypted so we can't check internal IP header.
1045 if (key_preferred_oldsa) {
1046 saorder_state_valid = saorder_state_valid_prefer_old;
1047 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
1049 saorder_state_valid = saorder_state_valid_prefer_new;
1050 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
1052 LIST_FOREACH(sah, &sahtree, chain) {
1053 /* search valid state */
1054 for (stateidx = 0; stateidx < arraysize; stateidx++) {
1055 state = saorder_state_valid[stateidx];
1056 LIST_FOREACH(sav, &sah->savtree[state], chain) {
1058 KEY_CHKSASTATE(sav->state, state, __func__);
1059 /* do not return entries w/ unusable state */
1060 if (sav->state != SADB_SASTATE_MATURE &&
1061 sav->state != SADB_SASTATE_DYING)
1063 if (proto != sav->sah->saidx.proto)
1065 if (spi != sav->spi)
1067 #if 0 /* don't check src */
1068 /* check src address */
1069 if (key_sockaddrcmp(&src->sa, &sav->sah->saidx.src.sa, 0) != 0)
1072 /* check dst address */
1073 if (key_sockaddrcmp(&dst->sa, &sav->sah->saidx.dst.sa, 0) != 0)
1084 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1085 printf("DP %s return SA:%p; refcnt %u\n", __func__,
1086 sav, sav ? sav->refcnt : 0));
1091 * Must be called after calling key_allocsp().
1092 * For both the packet without socket and key_freeso().
1095 _key_freesp(struct secpolicy **spp, const char* where, int tag)
1097 struct secpolicy *sp = *spp;
1099 IPSEC_ASSERT(sp != NULL, ("null sp"));
1104 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1105 printf("DP %s SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1106 __func__, sp, sp->id, where, tag, sp->refcnt));
1108 if (sp->refcnt == 0) {
1116 * Must be called after calling key_allocsp().
1117 * For the packet with socket.
1120 key_freeso(struct socket *so)
1122 IPSEC_ASSERT(so != NULL, ("null so"));
1124 switch (so->so_proto->pr_domain->dom_family) {
1128 struct inpcb *pcb = sotoinpcb(so);
1130 /* Does it have a PCB ? */
1133 key_freesp_so(&pcb->inp_sp->sp_in);
1134 key_freesp_so(&pcb->inp_sp->sp_out);
1141 #ifdef HAVE_NRL_INPCB
1142 struct inpcb *pcb = sotoinpcb(so);
1144 /* Does it have a PCB ? */
1147 key_freesp_so(&pcb->inp_sp->sp_in);
1148 key_freesp_so(&pcb->inp_sp->sp_out);
1150 struct in6pcb *pcb = sotoin6pcb(so);
1152 /* Does it have a PCB ? */
1155 key_freesp_so(&pcb->in6p_sp->sp_in);
1156 key_freesp_so(&pcb->in6p_sp->sp_out);
1162 ipseclog((LOG_DEBUG, "%s: unknown address family=%d.\n",
1163 __func__, so->so_proto->pr_domain->dom_family));
1169 key_freesp_so(struct secpolicy **sp)
1171 IPSEC_ASSERT(sp != NULL && *sp != NULL, ("null sp"));
1173 if ((*sp)->policy == IPSEC_POLICY_ENTRUST ||
1174 (*sp)->policy == IPSEC_POLICY_BYPASS)
1177 IPSEC_ASSERT((*sp)->policy == IPSEC_POLICY_IPSEC,
1178 ("invalid policy %u", (*sp)->policy));
1183 * Must be called after calling key_allocsa().
1184 * This function is called by key_freesp() to free some SA allocated
1188 key_freesav(struct secasvar **psav, const char* where, int tag)
1190 struct secasvar *sav = *psav;
1192 IPSEC_ASSERT(sav != NULL, ("null sav"));
1194 /* XXX unguarded? */
1197 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1198 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1199 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1201 if (sav->refcnt == 0) {
1207 /* %%% SPD management */
1209 * free security policy entry.
1212 key_delsp(struct secpolicy *sp)
1214 struct ipsecrequest *isr, *nextisr;
1216 IPSEC_ASSERT(sp != NULL, ("null sp"));
1217 SPTREE_LOCK_ASSERT();
1219 sp->state = IPSEC_SPSTATE_DEAD;
1221 IPSEC_ASSERT(sp->refcnt == 0,
1222 ("SP with references deleted (refcnt %u)", sp->refcnt));
1224 /* remove from SP index */
1225 if (__LIST_CHAINED(sp))
1226 LIST_REMOVE(sp, chain);
1228 for (isr = sp->req; isr != NULL; isr = nextisr) {
1229 if (isr->sav != NULL) {
1230 KEY_FREESAV(&isr->sav);
1234 nextisr = isr->next;
1242 * OUT: NULL : not found
1243 * others : found, pointer to a SP.
1245 static struct secpolicy *
1246 key_getsp(struct secpolicyindex *spidx)
1248 struct secpolicy *sp;
1250 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
1253 LIST_FOREACH(sp, &sptree[spidx->dir], chain) {
1254 if (sp->state == IPSEC_SPSTATE_DEAD)
1256 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1268 * OUT: NULL : not found
1269 * others : found, pointer to a SP.
1271 static struct secpolicy *
1272 key_getspbyid(u_int32_t id)
1274 struct secpolicy *sp;
1277 LIST_FOREACH(sp, &sptree[IPSEC_DIR_INBOUND], chain) {
1278 if (sp->state == IPSEC_SPSTATE_DEAD)
1286 LIST_FOREACH(sp, &sptree[IPSEC_DIR_OUTBOUND], chain) {
1287 if (sp->state == IPSEC_SPSTATE_DEAD)
1301 key_newsp(const char* where, int tag)
1303 struct secpolicy *newsp = NULL;
1305 newsp = (struct secpolicy *)
1306 malloc(sizeof(struct secpolicy), M_IPSEC_SP, M_NOWAIT|M_ZERO);
1308 SECPOLICY_LOCK_INIT(newsp);
1313 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1314 printf("DP %s from %s:%u return SP:%p\n", __func__,
1315 where, tag, newsp));
1320 _key_delsp(struct secpolicy *sp)
1322 SECPOLICY_LOCK_DESTROY(sp);
1323 free(sp, M_IPSEC_SP);
1327 * create secpolicy structure from sadb_x_policy structure.
1328 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1329 * so must be set properly later.
1332 key_msg2sp(xpl0, len, error)
1333 struct sadb_x_policy *xpl0;
1337 struct secpolicy *newsp;
1339 IPSEC_ASSERT(xpl0 != NULL, ("null xpl0"));
1340 IPSEC_ASSERT(len >= sizeof(*xpl0), ("policy too short: %zu", len));
1342 if (len != PFKEY_EXTLEN(xpl0)) {
1343 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n", __func__));
1348 if ((newsp = KEY_NEWSP()) == NULL) {
1353 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1354 newsp->policy = xpl0->sadb_x_policy_type;
1357 switch (xpl0->sadb_x_policy_type) {
1358 case IPSEC_POLICY_DISCARD:
1359 case IPSEC_POLICY_NONE:
1360 case IPSEC_POLICY_ENTRUST:
1361 case IPSEC_POLICY_BYPASS:
1365 case IPSEC_POLICY_IPSEC:
1368 struct sadb_x_ipsecrequest *xisr;
1369 struct ipsecrequest **p_isr = &newsp->req;
1371 /* validity check */
1372 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1373 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n",
1380 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1381 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1385 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1386 ipseclog((LOG_DEBUG, "%s: invalid ipsecrequest "
1387 "length.\n", __func__));
1393 /* allocate request buffer */
1394 /* NB: data structure is zero'd */
1395 *p_isr = ipsec_newisr();
1396 if ((*p_isr) == NULL) {
1397 ipseclog((LOG_DEBUG,
1398 "%s: No more memory.\n", __func__));
1405 switch (xisr->sadb_x_ipsecrequest_proto) {
1408 case IPPROTO_IPCOMP:
1411 ipseclog((LOG_DEBUG,
1412 "%s: invalid proto type=%u\n", __func__,
1413 xisr->sadb_x_ipsecrequest_proto));
1415 *error = EPROTONOSUPPORT;
1418 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1420 switch (xisr->sadb_x_ipsecrequest_mode) {
1421 case IPSEC_MODE_TRANSPORT:
1422 case IPSEC_MODE_TUNNEL:
1424 case IPSEC_MODE_ANY:
1426 ipseclog((LOG_DEBUG,
1427 "%s: invalid mode=%u\n", __func__,
1428 xisr->sadb_x_ipsecrequest_mode));
1433 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1435 switch (xisr->sadb_x_ipsecrequest_level) {
1436 case IPSEC_LEVEL_DEFAULT:
1437 case IPSEC_LEVEL_USE:
1438 case IPSEC_LEVEL_REQUIRE:
1440 case IPSEC_LEVEL_UNIQUE:
1441 /* validity check */
1443 * If range violation of reqid, kernel will
1444 * update it, don't refuse it.
1446 if (xisr->sadb_x_ipsecrequest_reqid
1447 > IPSEC_MANUAL_REQID_MAX) {
1448 ipseclog((LOG_DEBUG,
1449 "%s: reqid=%d range "
1450 "violation, updated by kernel.\n",
1452 xisr->sadb_x_ipsecrequest_reqid));
1453 xisr->sadb_x_ipsecrequest_reqid = 0;
1456 /* allocate new reqid id if reqid is zero. */
1457 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1459 if ((reqid = key_newreqid()) == 0) {
1464 (*p_isr)->saidx.reqid = reqid;
1465 xisr->sadb_x_ipsecrequest_reqid = reqid;
1467 /* set it for manual keying. */
1468 (*p_isr)->saidx.reqid =
1469 xisr->sadb_x_ipsecrequest_reqid;
1474 ipseclog((LOG_DEBUG, "%s: invalid level=%u\n",
1476 xisr->sadb_x_ipsecrequest_level));
1481 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1483 /* set IP addresses if there */
1484 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1485 struct sockaddr *paddr;
1487 paddr = (struct sockaddr *)(xisr + 1);
1489 /* validity check */
1491 > sizeof((*p_isr)->saidx.src)) {
1492 ipseclog((LOG_DEBUG, "%s: invalid "
1493 "request address length.\n",
1499 bcopy(paddr, &(*p_isr)->saidx.src,
1502 paddr = (struct sockaddr *)((caddr_t)paddr
1505 /* validity check */
1507 > sizeof((*p_isr)->saidx.dst)) {
1508 ipseclog((LOG_DEBUG, "%s: invalid "
1509 "request address length.\n",
1515 bcopy(paddr, &(*p_isr)->saidx.dst,
1519 (*p_isr)->sp = newsp;
1521 /* initialization for the next. */
1522 p_isr = &(*p_isr)->next;
1523 tlen -= xisr->sadb_x_ipsecrequest_len;
1525 /* validity check */
1527 ipseclog((LOG_DEBUG, "%s: becoming tlen < 0.\n",
1534 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1535 + xisr->sadb_x_ipsecrequest_len);
1540 ipseclog((LOG_DEBUG, "%s: invalid policy type.\n", __func__));
1553 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1555 auto_reqid = (auto_reqid == ~0
1556 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1558 /* XXX should be unique check */
1564 * copy secpolicy struct to sadb_x_policy structure indicated.
1568 struct secpolicy *sp;
1570 struct sadb_x_policy *xpl;
1575 IPSEC_ASSERT(sp != NULL, ("null policy"));
1577 tlen = key_getspreqmsglen(sp);
1579 m = key_alloc_mbuf(tlen);
1580 if (!m || m->m_next) { /*XXX*/
1588 xpl = mtod(m, struct sadb_x_policy *);
1591 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1592 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1593 xpl->sadb_x_policy_type = sp->policy;
1594 xpl->sadb_x_policy_dir = sp->spidx.dir;
1595 xpl->sadb_x_policy_id = sp->id;
1596 p = (caddr_t)xpl + sizeof(*xpl);
1598 /* if is the policy for ipsec ? */
1599 if (sp->policy == IPSEC_POLICY_IPSEC) {
1600 struct sadb_x_ipsecrequest *xisr;
1601 struct ipsecrequest *isr;
1603 for (isr = sp->req; isr != NULL; isr = isr->next) {
1605 xisr = (struct sadb_x_ipsecrequest *)p;
1607 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1608 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1609 xisr->sadb_x_ipsecrequest_level = isr->level;
1610 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1613 bcopy(&isr->saidx.src, p, isr->saidx.src.sa.sa_len);
1614 p += isr->saidx.src.sa.sa_len;
1615 bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len);
1616 p += isr->saidx.src.sa.sa_len;
1618 xisr->sadb_x_ipsecrequest_len =
1619 PFKEY_ALIGN8(sizeof(*xisr)
1620 + isr->saidx.src.sa.sa_len
1621 + isr->saidx.dst.sa.sa_len);
1628 /* m will not be freed nor modified */
1629 static struct mbuf *
1631 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1632 int ndeep, int nitem, ...)
1634 key_gather_mbuf(m, mhp, ndeep, nitem, va_alist)
1636 const struct sadb_msghdr *mhp;
1645 struct mbuf *result = NULL, *n;
1648 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1649 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1651 va_start(ap, nitem);
1652 for (i = 0; i < nitem; i++) {
1653 idx = va_arg(ap, int);
1654 if (idx < 0 || idx > SADB_EXT_MAX)
1656 /* don't attempt to pull empty extension */
1657 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1659 if (idx != SADB_EXT_RESERVED &&
1660 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1663 if (idx == SADB_EXT_RESERVED) {
1664 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1666 IPSEC_ASSERT(len <= MHLEN, ("header too big %u", len));
1668 MGETHDR(n, M_DONTWAIT, MT_DATA);
1673 m_copydata(m, 0, sizeof(struct sadb_msg),
1675 } else if (i < ndeep) {
1676 len = mhp->extlen[idx];
1677 n = key_alloc_mbuf(len);
1678 if (!n || n->m_next) { /*XXX*/
1683 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1686 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1699 if ((result->m_flags & M_PKTHDR) != 0) {
1700 result->m_pkthdr.len = 0;
1701 for (n = result; n; n = n->m_next)
1702 result->m_pkthdr.len += n->m_len;
1713 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1714 * add an entry to SP database, when received
1715 * <base, address(SD), (lifetime(H),) policy>
1717 * Adding to SP database,
1719 * <base, address(SD), (lifetime(H),) policy>
1720 * to the socket which was send.
1722 * SPDADD set a unique policy entry.
1723 * SPDSETIDX like SPDADD without a part of policy requests.
1724 * SPDUPDATE replace a unique policy entry.
1726 * m will always be freed.
1729 key_spdadd(so, m, mhp)
1732 const struct sadb_msghdr *mhp;
1734 struct sadb_address *src0, *dst0;
1735 struct sadb_x_policy *xpl0, *xpl;
1736 struct sadb_lifetime *lft = NULL;
1737 struct secpolicyindex spidx;
1738 struct secpolicy *newsp;
1741 IPSEC_ASSERT(so != NULL, ("null socket"));
1742 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1743 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1744 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
1746 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1747 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1748 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1749 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1750 return key_senderror(so, m, EINVAL);
1752 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1753 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1754 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1755 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1757 return key_senderror(so, m, EINVAL);
1759 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1760 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1761 < sizeof(struct sadb_lifetime)) {
1762 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1764 return key_senderror(so, m, EINVAL);
1766 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1769 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1770 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1771 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1774 /* XXX boundary check against sa_len */
1775 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1778 src0->sadb_address_prefixlen,
1779 dst0->sadb_address_prefixlen,
1780 src0->sadb_address_proto,
1783 /* checking the direciton. */
1784 switch (xpl0->sadb_x_policy_dir) {
1785 case IPSEC_DIR_INBOUND:
1786 case IPSEC_DIR_OUTBOUND:
1789 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
1790 mhp->msg->sadb_msg_errno = EINVAL;
1795 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1796 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1797 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1798 ipseclog((LOG_DEBUG, "%s: Invalid policy type.\n", __func__));
1799 return key_senderror(so, m, EINVAL);
1802 /* policy requests are mandatory when action is ipsec. */
1803 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1804 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1805 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1806 ipseclog((LOG_DEBUG, "%s: some policy requests part required\n",
1808 return key_senderror(so, m, EINVAL);
1812 * checking there is SP already or not.
1813 * SPDUPDATE doesn't depend on whether there is a SP or not.
1814 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1817 newsp = key_getsp(&spidx);
1818 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1820 newsp->state = IPSEC_SPSTATE_DEAD;
1824 if (newsp != NULL) {
1826 ipseclog((LOG_DEBUG, "%s: a SP entry exists already.\n",
1828 return key_senderror(so, m, EEXIST);
1832 /* allocation new SP entry */
1833 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1834 return key_senderror(so, m, error);
1837 if ((newsp->id = key_getnewspid()) == 0) {
1839 return key_senderror(so, m, ENOBUFS);
1842 /* XXX boundary check against sa_len */
1843 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1846 src0->sadb_address_prefixlen,
1847 dst0->sadb_address_prefixlen,
1848 src0->sadb_address_proto,
1851 /* sanity check on addr pair */
1852 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1853 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1855 return key_senderror(so, m, EINVAL);
1857 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1858 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1860 return key_senderror(so, m, EINVAL);
1863 if (newsp->req && newsp->req->saidx.src.sa.sa_family) {
1864 struct sockaddr *sa;
1865 sa = (struct sockaddr *)(src0 + 1);
1866 if (sa->sa_family != newsp->req->saidx.src.sa.sa_family) {
1868 return key_senderror(so, m, EINVAL);
1871 if (newsp->req && newsp->req->saidx.dst.sa.sa_family) {
1872 struct sockaddr *sa;
1873 sa = (struct sockaddr *)(dst0 + 1);
1874 if (sa->sa_family != newsp->req->saidx.dst.sa.sa_family) {
1876 return key_senderror(so, m, EINVAL);
1881 newsp->created = time_second;
1882 newsp->lastused = newsp->created;
1883 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1884 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1886 newsp->refcnt = 1; /* do not reclaim until I say I do */
1887 newsp->state = IPSEC_SPSTATE_ALIVE;
1888 LIST_INSERT_TAIL(&sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1890 /* delete the entry in spacqtree */
1891 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1892 struct secspacq *spacq = key_getspacq(&spidx);
1893 if (spacq != NULL) {
1894 /* reset counter in order to deletion by timehandler. */
1895 spacq->created = time_second;
1902 struct mbuf *n, *mpolicy;
1903 struct sadb_msg *newmsg;
1906 /* create new sadb_msg to reply. */
1908 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1909 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1910 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1912 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1914 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1917 return key_senderror(so, m, ENOBUFS);
1919 if (n->m_len < sizeof(*newmsg)) {
1920 n = m_pullup(n, sizeof(*newmsg));
1922 return key_senderror(so, m, ENOBUFS);
1924 newmsg = mtod(n, struct sadb_msg *);
1925 newmsg->sadb_msg_errno = 0;
1926 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1929 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
1930 sizeof(*xpl), &off);
1931 if (mpolicy == NULL) {
1932 /* n is already freed */
1933 return key_senderror(so, m, ENOBUFS);
1935 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
1936 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
1938 return key_senderror(so, m, EINVAL);
1940 xpl->sadb_x_policy_id = newsp->id;
1943 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
1948 * get new policy id.
1956 u_int32_t newid = 0;
1957 int count = key_spi_trycnt; /* XXX */
1958 struct secpolicy *sp;
1960 /* when requesting to allocate spi ranged */
1962 newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1));
1964 if ((sp = key_getspbyid(newid)) == NULL)
1970 if (count == 0 || newid == 0) {
1971 ipseclog((LOG_DEBUG, "%s: to allocate policy id is failed.\n",
1980 * SADB_SPDDELETE processing
1982 * <base, address(SD), policy(*)>
1983 * from the user(?), and set SADB_SASTATE_DEAD,
1985 * <base, address(SD), policy(*)>
1987 * policy(*) including direction of policy.
1989 * m will always be freed.
1992 key_spddelete(so, m, mhp)
1995 const struct sadb_msghdr *mhp;
1997 struct sadb_address *src0, *dst0;
1998 struct sadb_x_policy *xpl0;
1999 struct secpolicyindex spidx;
2000 struct secpolicy *sp;
2002 IPSEC_ASSERT(so != NULL, ("null so"));
2003 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2004 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2005 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2007 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
2008 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
2009 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
2010 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2012 return key_senderror(so, m, EINVAL);
2014 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
2015 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
2016 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2017 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2019 return key_senderror(so, m, EINVAL);
2022 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2023 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2024 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2027 /* XXX boundary check against sa_len */
2028 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2031 src0->sadb_address_prefixlen,
2032 dst0->sadb_address_prefixlen,
2033 src0->sadb_address_proto,
2036 /* checking the direciton. */
2037 switch (xpl0->sadb_x_policy_dir) {
2038 case IPSEC_DIR_INBOUND:
2039 case IPSEC_DIR_OUTBOUND:
2042 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
2043 return key_senderror(so, m, EINVAL);
2046 /* Is there SP in SPD ? */
2047 if ((sp = key_getsp(&spidx)) == NULL) {
2048 ipseclog((LOG_DEBUG, "%s: no SP found.\n", __func__));
2049 return key_senderror(so, m, EINVAL);
2052 /* save policy id to buffer to be returned. */
2053 xpl0->sadb_x_policy_id = sp->id;
2055 sp->state = IPSEC_SPSTATE_DEAD;
2060 struct sadb_msg *newmsg;
2062 /* create new sadb_msg to reply. */
2063 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2064 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2066 return key_senderror(so, m, ENOBUFS);
2068 newmsg = mtod(n, struct sadb_msg *);
2069 newmsg->sadb_msg_errno = 0;
2070 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2073 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2078 * SADB_SPDDELETE2 processing
2081 * from the user(?), and set SADB_SASTATE_DEAD,
2085 * policy(*) including direction of policy.
2087 * m will always be freed.
2090 key_spddelete2(so, m, mhp)
2093 const struct sadb_msghdr *mhp;
2096 struct secpolicy *sp;
2098 IPSEC_ASSERT(so != NULL, ("null socket"));
2099 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2100 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2101 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2103 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2104 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2105 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", __func__));
2106 key_senderror(so, m, EINVAL);
2110 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2112 /* Is there SP in SPD ? */
2113 if ((sp = key_getspbyid(id)) == NULL) {
2114 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2115 key_senderror(so, m, EINVAL);
2118 sp->state = IPSEC_SPSTATE_DEAD;
2122 struct mbuf *n, *nn;
2123 struct sadb_msg *newmsg;
2126 /* create new sadb_msg to reply. */
2127 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2130 return key_senderror(so, m, ENOBUFS);
2131 MGETHDR(n, M_DONTWAIT, MT_DATA);
2132 if (n && len > MHLEN) {
2133 MCLGET(n, M_DONTWAIT);
2134 if ((n->m_flags & M_EXT) == 0) {
2140 return key_senderror(so, m, ENOBUFS);
2146 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2147 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2149 IPSEC_ASSERT(off == len, ("length inconsistency (off %u len %u)",
2152 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2153 mhp->extlen[SADB_X_EXT_POLICY], M_DONTWAIT);
2156 return key_senderror(so, m, ENOBUFS);
2159 n->m_pkthdr.len = 0;
2160 for (nn = n; nn; nn = nn->m_next)
2161 n->m_pkthdr.len += nn->m_len;
2163 newmsg = mtod(n, struct sadb_msg *);
2164 newmsg->sadb_msg_errno = 0;
2165 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2168 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2173 * SADB_X_GET processing
2178 * <base, address(SD), policy>
2180 * policy(*) including direction of policy.
2182 * m will always be freed.
2185 key_spdget(so, m, mhp)
2188 const struct sadb_msghdr *mhp;
2191 struct secpolicy *sp;
2194 IPSEC_ASSERT(so != NULL, ("null socket"));
2195 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2196 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2197 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2199 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2200 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2201 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2203 return key_senderror(so, m, EINVAL);
2206 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2208 /* Is there SP in SPD ? */
2209 if ((sp = key_getspbyid(id)) == NULL) {
2210 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2211 return key_senderror(so, m, ENOENT);
2214 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2217 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2219 return key_senderror(so, m, ENOBUFS);
2223 * SADB_X_SPDACQUIRE processing.
2224 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2227 * to KMD, and expect to receive
2228 * <base> with SADB_X_SPDACQUIRE if error occured,
2231 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2232 * policy(*) is without policy requests.
2235 * others: error number
2239 struct secpolicy *sp;
2241 struct mbuf *result = NULL, *m;
2242 struct secspacq *newspacq;
2245 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2246 IPSEC_ASSERT(sp->req == NULL, ("policy exists"));
2247 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
2248 ("policy not IPSEC %u", sp->policy));
2250 /* Get an entry to check whether sent message or not. */
2251 newspacq = key_getspacq(&sp->spidx);
2252 if (newspacq != NULL) {
2253 if (key_blockacq_count < newspacq->count) {
2254 /* reset counter and do send message. */
2255 newspacq->count = 0;
2257 /* increment counter and do nothing. */
2263 /* make new entry for blocking to send SADB_ACQUIRE. */
2264 newspacq = key_newspacq(&sp->spidx);
2265 if (newspacq == NULL)
2269 /* create new sadb_msg to reply. */
2270 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2277 result->m_pkthdr.len = 0;
2278 for (m = result; m; m = m->m_next)
2279 result->m_pkthdr.len += m->m_len;
2281 mtod(result, struct sadb_msg *)->sadb_msg_len =
2282 PFKEY_UNIT64(result->m_pkthdr.len);
2284 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2293 * SADB_SPDFLUSH processing
2296 * from the user, and free all entries in secpctree.
2300 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2302 * m will always be freed.
2305 key_spdflush(so, m, mhp)
2308 const struct sadb_msghdr *mhp;
2310 struct sadb_msg *newmsg;
2311 struct secpolicy *sp;
2314 IPSEC_ASSERT(so != NULL, ("null socket"));
2315 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2316 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2317 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2319 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2320 return key_senderror(so, m, EINVAL);
2322 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2324 LIST_FOREACH(sp, &sptree[dir], chain)
2325 sp->state = IPSEC_SPSTATE_DEAD;
2329 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2330 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2331 return key_senderror(so, m, ENOBUFS);
2337 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2338 newmsg = mtod(m, struct sadb_msg *);
2339 newmsg->sadb_msg_errno = 0;
2340 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2342 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2346 * SADB_SPDDUMP processing
2349 * from the user, and dump all SP leaves
2354 * m will always be freed.
2357 key_spddump(so, m, mhp)
2360 const struct sadb_msghdr *mhp;
2362 struct secpolicy *sp;
2367 IPSEC_ASSERT(so != NULL, ("null socket"));
2368 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2369 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2370 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2372 /* search SPD entry and get buffer size. */
2374 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2375 LIST_FOREACH(sp, &sptree[dir], chain) {
2381 return key_senderror(so, m, ENOENT);
2383 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2384 LIST_FOREACH(sp, &sptree[dir], chain) {
2386 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2387 mhp->msg->sadb_msg_pid);
2390 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2398 static struct mbuf *
2399 key_setdumpsp(sp, type, seq, pid)
2400 struct secpolicy *sp;
2404 struct mbuf *result = NULL, *m;
2406 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2411 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2412 &sp->spidx.src.sa, sp->spidx.prefs,
2413 sp->spidx.ul_proto);
2418 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2419 &sp->spidx.dst.sa, sp->spidx.prefd,
2420 sp->spidx.ul_proto);
2430 if ((result->m_flags & M_PKTHDR) == 0)
2433 if (result->m_len < sizeof(struct sadb_msg)) {
2434 result = m_pullup(result, sizeof(struct sadb_msg));
2439 result->m_pkthdr.len = 0;
2440 for (m = result; m; m = m->m_next)
2441 result->m_pkthdr.len += m->m_len;
2443 mtod(result, struct sadb_msg *)->sadb_msg_len =
2444 PFKEY_UNIT64(result->m_pkthdr.len);
2454 * get PFKEY message length for security policy and request.
2457 key_getspreqmsglen(sp)
2458 struct secpolicy *sp;
2462 tlen = sizeof(struct sadb_x_policy);
2464 /* if is the policy for ipsec ? */
2465 if (sp->policy != IPSEC_POLICY_IPSEC)
2468 /* get length of ipsec requests */
2470 struct ipsecrequest *isr;
2473 for (isr = sp->req; isr != NULL; isr = isr->next) {
2474 len = sizeof(struct sadb_x_ipsecrequest)
2475 + isr->saidx.src.sa.sa_len
2476 + isr->saidx.dst.sa.sa_len;
2478 tlen += PFKEY_ALIGN8(len);
2486 * SADB_SPDEXPIRE processing
2488 * <base, address(SD), lifetime(CH), policy>
2492 * others : error number
2496 struct secpolicy *sp;
2498 struct mbuf *result = NULL, *m;
2501 struct sadb_lifetime *lt;
2503 /* XXX: Why do we lock ? */
2505 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2507 /* set msg header */
2508 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2515 /* create lifetime extension (current and hard) */
2516 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2517 m = key_alloc_mbuf(len);
2518 if (!m || m->m_next) { /*XXX*/
2524 bzero(mtod(m, caddr_t), len);
2525 lt = mtod(m, struct sadb_lifetime *);
2526 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2527 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2528 lt->sadb_lifetime_allocations = 0;
2529 lt->sadb_lifetime_bytes = 0;
2530 lt->sadb_lifetime_addtime = sp->created;
2531 lt->sadb_lifetime_usetime = sp->lastused;
2532 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2533 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2534 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2535 lt->sadb_lifetime_allocations = 0;
2536 lt->sadb_lifetime_bytes = 0;
2537 lt->sadb_lifetime_addtime = sp->lifetime;
2538 lt->sadb_lifetime_usetime = sp->validtime;
2541 /* set sadb_address for source */
2542 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2544 sp->spidx.prefs, sp->spidx.ul_proto);
2551 /* set sadb_address for destination */
2552 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2554 sp->spidx.prefd, sp->spidx.ul_proto);
2569 if ((result->m_flags & M_PKTHDR) == 0) {
2574 if (result->m_len < sizeof(struct sadb_msg)) {
2575 result = m_pullup(result, sizeof(struct sadb_msg));
2576 if (result == NULL) {
2582 result->m_pkthdr.len = 0;
2583 for (m = result; m; m = m->m_next)
2584 result->m_pkthdr.len += m->m_len;
2586 mtod(result, struct sadb_msg *)->sadb_msg_len =
2587 PFKEY_UNIT64(result->m_pkthdr.len);
2589 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2597 /* %%% SAD management */
2599 * allocating a memory for new SA head, and copy from the values of mhp.
2600 * OUT: NULL : failure due to the lack of memory.
2601 * others : pointer to new SA head.
2603 static struct secashead *
2605 struct secasindex *saidx;
2607 struct secashead *newsah;
2609 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
2611 newsah = malloc(sizeof(struct secashead), M_IPSEC_SAH, M_NOWAIT|M_ZERO);
2612 if (newsah != NULL) {
2614 for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++)
2615 LIST_INIT(&newsah->savtree[i]);
2616 newsah->saidx = *saidx;
2618 /* add to saidxtree */
2619 newsah->state = SADB_SASTATE_MATURE;
2622 LIST_INSERT_HEAD(&sahtree, newsah, chain);
2629 * delete SA index and all SA registerd.
2633 struct secashead *sah;
2635 struct secasvar *sav, *nextsav;
2639 IPSEC_ASSERT(sah != NULL, ("NULL sah"));
2640 SAHTREE_LOCK_ASSERT();
2642 /* searching all SA registerd in the secindex. */
2644 stateidx < _ARRAYLEN(saorder_state_any);
2646 u_int state = saorder_state_any[stateidx];
2647 LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain, nextsav) {
2648 if (sav->refcnt == 0) {
2650 KEY_CHKSASTATE(state, sav->state, __func__);
2653 /* give up to delete this sa */
2658 if (!zombie) { /* delete only if there are savs */
2659 /* remove from tree of SA index */
2660 if (__LIST_CHAINED(sah))
2661 LIST_REMOVE(sah, chain);
2662 if (sah->sa_route.ro_rt) {
2663 RTFREE(sah->sa_route.ro_rt);
2664 sah->sa_route.ro_rt = (struct rtentry *)NULL;
2666 free(sah, M_IPSEC_SAH);
2671 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2672 * and copy the values of mhp into new buffer.
2673 * When SAD message type is GETSPI:
2674 * to set sequence number from acq_seq++,
2675 * to set zero to SPI.
2676 * not to call key_setsava().
2678 * others : pointer to new secasvar.
2680 * does not modify mbuf. does not free mbuf on error.
2682 static struct secasvar *
2683 key_newsav(m, mhp, sah, errp, where, tag)
2685 const struct sadb_msghdr *mhp;
2686 struct secashead *sah;
2691 struct secasvar *newsav;
2692 const struct sadb_sa *xsa;
2694 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2695 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2696 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2697 IPSEC_ASSERT(sah != NULL, ("null secashead"));
2699 newsav = malloc(sizeof(struct secasvar), M_IPSEC_SA, M_NOWAIT|M_ZERO);
2700 if (newsav == NULL) {
2701 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2706 switch (mhp->msg->sadb_msg_type) {
2710 #ifdef IPSEC_DOSEQCHECK
2711 /* sync sequence number */
2712 if (mhp->msg->sadb_msg_seq == 0)
2714 (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
2717 newsav->seq = mhp->msg->sadb_msg_seq;
2722 if (mhp->ext[SADB_EXT_SA] == NULL) {
2723 free(newsav, M_IPSEC_SA);
2725 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2730 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2731 newsav->spi = xsa->sadb_sa_spi;
2732 newsav->seq = mhp->msg->sadb_msg_seq;
2735 free(newsav, M_IPSEC_SA);
2742 /* copy sav values */
2743 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2744 *errp = key_setsaval(newsav, m, mhp);
2746 free(newsav, M_IPSEC_SA);
2752 SECASVAR_LOCK_INIT(newsav);
2755 newsav->created = time_second;
2756 newsav->pid = mhp->msg->sadb_msg_pid;
2761 newsav->state = SADB_SASTATE_LARVAL;
2763 /* XXX locking??? */
2764 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2767 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
2768 printf("DP %s from %s:%u return SP:%p\n", __func__,
2769 where, tag, newsav));
2775 * free() SA variable entry.
2778 key_cleansav(struct secasvar *sav)
2781 * Cleanup xform state. Note that zeroize'ing causes the
2782 * keys to be cleared; otherwise we must do it ourself.
2784 if (sav->tdb_xform != NULL) {
2785 sav->tdb_xform->xf_zeroize(sav);
2786 sav->tdb_xform = NULL;
2788 KASSERT(sav->iv == NULL, ("iv but no xform"));
2789 if (sav->key_auth != NULL)
2790 bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
2791 if (sav->key_enc != NULL)
2792 bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
2794 if (sav->key_auth != NULL) {
2795 free(sav->key_auth, M_IPSEC_MISC);
2796 sav->key_auth = NULL;
2798 if (sav->key_enc != NULL) {
2799 free(sav->key_enc, M_IPSEC_MISC);
2800 sav->key_enc = NULL;
2803 bzero(sav->sched, sav->schedlen);
2804 free(sav->sched, M_IPSEC_MISC);
2807 if (sav->replay != NULL) {
2808 free(sav->replay, M_IPSEC_MISC);
2811 if (sav->lft_c != NULL) {
2812 free(sav->lft_c, M_IPSEC_MISC);
2815 if (sav->lft_h != NULL) {
2816 free(sav->lft_h, M_IPSEC_MISC);
2819 if (sav->lft_s != NULL) {
2820 free(sav->lft_s, M_IPSEC_MISC);
2826 * free() SA variable entry.
2830 struct secasvar *sav;
2832 IPSEC_ASSERT(sav != NULL, ("null sav"));
2833 IPSEC_ASSERT(sav->refcnt == 0, ("reference count %u > 0", sav->refcnt));
2835 /* remove from SA header */
2836 if (__LIST_CHAINED(sav))
2837 LIST_REMOVE(sav, chain);
2839 SECASVAR_LOCK_DESTROY(sav);
2840 free(sav, M_IPSEC_SA);
2847 * others : found, pointer to a SA.
2849 static struct secashead *
2851 struct secasindex *saidx;
2853 struct secashead *sah;
2856 LIST_FOREACH(sah, &sahtree, chain) {
2857 if (sah->state == SADB_SASTATE_DEAD)
2859 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2868 * check not to be duplicated SPI.
2869 * NOTE: this function is too slow due to searching all SAD.
2872 * others : found, pointer to a SA.
2874 static struct secasvar *
2875 key_checkspidup(saidx, spi)
2876 struct secasindex *saidx;
2879 struct secashead *sah;
2880 struct secasvar *sav;
2882 /* check address family */
2883 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
2884 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
2892 LIST_FOREACH(sah, &sahtree, chain) {
2893 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
2895 sav = key_getsavbyspi(sah, spi);
2905 * search SAD litmited alive SA, protocol, SPI.
2908 * others : found, pointer to a SA.
2910 static struct secasvar *
2911 key_getsavbyspi(sah, spi)
2912 struct secashead *sah;
2915 struct secasvar *sav;
2916 u_int stateidx, state;
2919 SAHTREE_LOCK_ASSERT();
2920 /* search all status */
2922 stateidx < _ARRAYLEN(saorder_state_alive);
2925 state = saorder_state_alive[stateidx];
2926 LIST_FOREACH(sav, &sah->savtree[state], chain) {
2929 if (sav->state != state) {
2930 ipseclog((LOG_DEBUG, "%s: "
2931 "invalid sav->state (queue: %d SA: %d)\n",
2932 __func__, state, sav->state));
2936 if (sav->spi == spi)
2945 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
2946 * You must update these if need.
2950 * does not modify mbuf. does not free mbuf on error.
2953 key_setsaval(sav, m, mhp)
2954 struct secasvar *sav;
2956 const struct sadb_msghdr *mhp;
2960 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2961 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2962 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2964 /* initialization */
2966 sav->key_auth = NULL;
2967 sav->key_enc = NULL;
2974 sav->tdb_xform = NULL; /* transform */
2975 sav->tdb_encalgxform = NULL; /* encoding algorithm */
2976 sav->tdb_authalgxform = NULL; /* authentication algorithm */
2977 sav->tdb_compalgxform = NULL; /* compression algorithm */
2980 if (mhp->ext[SADB_EXT_SA] != NULL) {
2981 const struct sadb_sa *sa0;
2983 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2984 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
2989 sav->alg_auth = sa0->sadb_sa_auth;
2990 sav->alg_enc = sa0->sadb_sa_encrypt;
2991 sav->flags = sa0->sadb_sa_flags;
2994 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
2995 sav->replay = (struct secreplay *)
2996 malloc(sizeof(struct secreplay)+sa0->sadb_sa_replay, M_IPSEC_MISC, M_NOWAIT|M_ZERO);
2997 if (sav->replay == NULL) {
2998 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3003 if (sa0->sadb_sa_replay != 0)
3004 sav->replay->bitmap = (caddr_t)(sav->replay+1);
3005 sav->replay->wsize = sa0->sadb_sa_replay;
3009 /* Authentication keys */
3010 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
3011 const struct sadb_key *key0;
3014 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
3015 len = mhp->extlen[SADB_EXT_KEY_AUTH];
3018 if (len < sizeof(*key0)) {
3022 switch (mhp->msg->sadb_msg_satype) {
3023 case SADB_SATYPE_AH:
3024 case SADB_SATYPE_ESP:
3025 case SADB_X_SATYPE_TCPSIGNATURE:
3026 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3027 sav->alg_auth != SADB_X_AALG_NULL)
3030 case SADB_X_SATYPE_IPCOMP:
3036 ipseclog((LOG_DEBUG, "%s: invalid key_auth values.\n",
3041 sav->key_auth = key_dup(key0, len, M_IPSEC_MISC);
3042 if (sav->key_auth == NULL) {
3043 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3049 /* Encryption key */
3050 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3051 const struct sadb_key *key0;
3054 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3055 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3058 if (len < sizeof(*key0)) {
3062 switch (mhp->msg->sadb_msg_satype) {
3063 case SADB_SATYPE_ESP:
3064 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3065 sav->alg_enc != SADB_EALG_NULL) {
3069 sav->key_enc = key_dup(key0, len, M_IPSEC_MISC);
3070 if (sav->key_enc == NULL) {
3071 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3077 case SADB_X_SATYPE_IPCOMP:
3078 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3080 sav->key_enc = NULL; /*just in case*/
3082 case SADB_SATYPE_AH:
3083 case SADB_X_SATYPE_TCPSIGNATURE:
3089 ipseclog((LOG_DEBUG, "%s: invalid key_enc value.\n",
3098 switch (mhp->msg->sadb_msg_satype) {
3099 case SADB_SATYPE_AH:
3100 error = xform_init(sav, XF_AH);
3102 case SADB_SATYPE_ESP:
3103 error = xform_init(sav, XF_ESP);
3105 case SADB_X_SATYPE_IPCOMP:
3106 error = xform_init(sav, XF_IPCOMP);
3108 case SADB_X_SATYPE_TCPSIGNATURE:
3109 error = xform_init(sav, XF_TCPSIGNATURE);
3113 ipseclog((LOG_DEBUG, "%s: unable to initialize SA type %u.\n",
3114 __func__, mhp->msg->sadb_msg_satype));
3119 sav->created = time_second;
3121 /* make lifetime for CURRENT */
3122 sav->lft_c = malloc(sizeof(struct sadb_lifetime), M_IPSEC_MISC, M_NOWAIT);
3123 if (sav->lft_c == NULL) {
3124 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3129 sav->lft_c->sadb_lifetime_len =
3130 PFKEY_UNIT64(sizeof(struct sadb_lifetime));
3131 sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
3132 sav->lft_c->sadb_lifetime_allocations = 0;
3133 sav->lft_c->sadb_lifetime_bytes = 0;
3134 sav->lft_c->sadb_lifetime_addtime = time_second;
3135 sav->lft_c->sadb_lifetime_usetime = 0;
3137 /* lifetimes for HARD and SOFT */
3139 const struct sadb_lifetime *lft0;
3141 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3143 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3147 sav->lft_h = key_dup(lft0, sizeof(*lft0), M_IPSEC_MISC);
3148 if (sav->lft_h == NULL) {
3149 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3153 /* to be initialize ? */
3156 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3158 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3162 sav->lft_s = key_dup(lft0, sizeof(*lft0), M_IPSEC_MISC);
3163 if (sav->lft_s == NULL) {
3164 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3168 /* to be initialize ? */
3175 /* initialization */
3182 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3187 key_mature(struct secasvar *sav)
3191 /* check SPI value */
3192 switch (sav->sah->saidx.proto) {
3195 if (ntohl(sav->spi) >= 0 && ntohl(sav->spi) <= 255) {
3196 ipseclog((LOG_DEBUG, "%s: illegal range of SPI %u.\n",
3197 __func__, (u_int32_t)ntohl(sav->spi)));
3204 switch (sav->sah->saidx.proto) {
3207 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3208 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3209 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3210 "given to old-esp.\n", __func__));
3213 error = xform_init(sav, XF_ESP);
3217 if (sav->flags & SADB_X_EXT_DERIV) {
3218 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3219 "given to AH SA.\n", __func__));
3222 if (sav->alg_enc != SADB_EALG_NONE) {
3223 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3224 "mismated.\n", __func__));
3227 error = xform_init(sav, XF_AH);
3229 case IPPROTO_IPCOMP:
3230 if (sav->alg_auth != SADB_AALG_NONE) {
3231 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3232 "mismated.\n", __func__));
3235 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3236 && ntohl(sav->spi) >= 0x10000) {
3237 ipseclog((LOG_DEBUG, "%s: invalid cpi for IPComp.\n",
3241 error = xform_init(sav, XF_IPCOMP);
3244 if (sav->alg_enc != SADB_EALG_NONE) {
3245 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3246 "mismated.\n", __func__));
3249 error = xform_init(sav, XF_TCPSIGNATURE);
3252 ipseclog((LOG_DEBUG, "%s: Invalid satype.\n", __func__));
3253 error = EPROTONOSUPPORT;
3258 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3265 * subroutine for SADB_GET and SADB_DUMP.
3267 static struct mbuf *
3268 key_setdumpsa(sav, type, satype, seq, pid)
3269 struct secasvar *sav;
3270 u_int8_t type, satype;
3273 struct mbuf *result = NULL, *tres = NULL, *m;
3278 SADB_EXT_SA, SADB_X_EXT_SA2,
3279 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3280 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3281 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3282 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3283 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3286 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3291 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3294 switch (dumporder[i]) {
3296 m = key_setsadbsa(sav);
3301 case SADB_X_EXT_SA2:
3302 m = key_setsadbxsa2(sav->sah->saidx.mode,
3303 sav->replay ? sav->replay->count : 0,
3304 sav->sah->saidx.reqid);
3309 case SADB_EXT_ADDRESS_SRC:
3310 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3311 &sav->sah->saidx.src.sa,
3312 FULLMASK, IPSEC_ULPROTO_ANY);
3317 case SADB_EXT_ADDRESS_DST:
3318 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3319 &sav->sah->saidx.dst.sa,
3320 FULLMASK, IPSEC_ULPROTO_ANY);
3325 case SADB_EXT_KEY_AUTH:
3328 l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len);
3332 case SADB_EXT_KEY_ENCRYPT:
3335 l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len);
3339 case SADB_EXT_LIFETIME_CURRENT:
3342 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len);
3346 case SADB_EXT_LIFETIME_HARD:
3349 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len);
3353 case SADB_EXT_LIFETIME_SOFT:
3356 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len);
3360 case SADB_EXT_ADDRESS_PROXY:
3361 case SADB_EXT_IDENTITY_SRC:
3362 case SADB_EXT_IDENTITY_DST:
3363 /* XXX: should we brought from SPD ? */
3364 case SADB_EXT_SENSITIVITY:
3369 if ((!m && !p) || (m && p))
3372 M_PREPEND(tres, l, M_DONTWAIT);
3375 bcopy(p, mtod(tres, caddr_t), l);
3379 m = key_alloc_mbuf(l);
3382 m_copyback(m, 0, l, p);
3390 m_cat(result, tres);
3392 if (result->m_len < sizeof(struct sadb_msg)) {
3393 result = m_pullup(result, sizeof(struct sadb_msg));
3398 result->m_pkthdr.len = 0;
3399 for (m = result; m; m = m->m_next)
3400 result->m_pkthdr.len += m->m_len;
3402 mtod(result, struct sadb_msg *)->sadb_msg_len =
3403 PFKEY_UNIT64(result->m_pkthdr.len);
3414 * set data into sadb_msg.
3416 static struct mbuf *
3417 key_setsadbmsg(type, tlen, satype, seq, pid, reserved)
3418 u_int8_t type, satype;
3428 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3431 MGETHDR(m, M_DONTWAIT, MT_DATA);
3432 if (m && len > MHLEN) {
3433 MCLGET(m, M_DONTWAIT);
3434 if ((m->m_flags & M_EXT) == 0) {
3441 m->m_pkthdr.len = m->m_len = len;
3444 p = mtod(m, struct sadb_msg *);
3447 p->sadb_msg_version = PF_KEY_V2;
3448 p->sadb_msg_type = type;
3449 p->sadb_msg_errno = 0;
3450 p->sadb_msg_satype = satype;
3451 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3452 p->sadb_msg_reserved = reserved;
3453 p->sadb_msg_seq = seq;
3454 p->sadb_msg_pid = (u_int32_t)pid;
3460 * copy secasvar data into sadb_address.
3462 static struct mbuf *
3464 struct secasvar *sav;
3470 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3471 m = key_alloc_mbuf(len);
3472 if (!m || m->m_next) { /*XXX*/
3478 p = mtod(m, struct sadb_sa *);
3481 p->sadb_sa_len = PFKEY_UNIT64(len);
3482 p->sadb_sa_exttype = SADB_EXT_SA;
3483 p->sadb_sa_spi = sav->spi;
3484 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3485 p->sadb_sa_state = sav->state;
3486 p->sadb_sa_auth = sav->alg_auth;
3487 p->sadb_sa_encrypt = sav->alg_enc;
3488 p->sadb_sa_flags = sav->flags;
3494 * set data into sadb_address.
3496 static struct mbuf *
3497 key_setsadbaddr(exttype, saddr, prefixlen, ul_proto)
3499 const struct sockaddr *saddr;
3504 struct sadb_address *p;
3507 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3508 PFKEY_ALIGN8(saddr->sa_len);
3509 m = key_alloc_mbuf(len);
3510 if (!m || m->m_next) { /*XXX*/
3516 p = mtod(m, struct sadb_address *);
3519 p->sadb_address_len = PFKEY_UNIT64(len);
3520 p->sadb_address_exttype = exttype;
3521 p->sadb_address_proto = ul_proto;
3522 if (prefixlen == FULLMASK) {
3523 switch (saddr->sa_family) {
3525 prefixlen = sizeof(struct in_addr) << 3;
3528 prefixlen = sizeof(struct in6_addr) << 3;
3534 p->sadb_address_prefixlen = prefixlen;
3535 p->sadb_address_reserved = 0;
3538 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3545 * set data into sadb_x_sa2.
3547 static struct mbuf *
3548 key_setsadbxsa2(mode, seq, reqid)
3550 u_int32_t seq, reqid;
3553 struct sadb_x_sa2 *p;
3556 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3557 m = key_alloc_mbuf(len);
3558 if (!m || m->m_next) { /*XXX*/
3564 p = mtod(m, struct sadb_x_sa2 *);
3567 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3568 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3569 p->sadb_x_sa2_mode = mode;
3570 p->sadb_x_sa2_reserved1 = 0;
3571 p->sadb_x_sa2_reserved2 = 0;
3572 p->sadb_x_sa2_sequence = seq;
3573 p->sadb_x_sa2_reqid = reqid;
3579 * set data into sadb_x_policy
3581 static struct mbuf *
3582 key_setsadbxpolicy(type, dir, id)
3588 struct sadb_x_policy *p;
3591 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3592 m = key_alloc_mbuf(len);
3593 if (!m || m->m_next) { /*XXX*/
3599 p = mtod(m, struct sadb_x_policy *);
3602 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3603 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3604 p->sadb_x_policy_type = type;
3605 p->sadb_x_policy_dir = dir;
3606 p->sadb_x_policy_id = id;
3613 * copy a buffer into the new buffer allocated.
3616 key_dup(const void *src, u_int len, struct malloc_type *type)
3620 copy = malloc(len, type, M_NOWAIT);
3623 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3625 bcopy(src, copy, len);
3629 /* compare my own address
3630 * OUT: 1: true, i.e. my address.
3635 struct sockaddr *sa;
3638 struct sockaddr_in *sin;
3639 struct in_ifaddr *ia;
3642 IPSEC_ASSERT(sa != NULL, ("null sockaddr"));
3644 switch (sa->sa_family) {
3647 sin = (struct sockaddr_in *)sa;
3648 for (ia = in_ifaddrhead.tqh_first; ia;
3649 ia = ia->ia_link.tqe_next)
3651 if (sin->sin_family == ia->ia_addr.sin_family &&
3652 sin->sin_len == ia->ia_addr.sin_len &&
3653 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3662 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3671 * compare my own address for IPv6.
3674 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3676 #include <netinet6/in6_var.h>
3680 struct sockaddr_in6 *sin6;
3682 struct in6_ifaddr *ia;
3683 struct in6_multi *in6m;
3685 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
3686 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3687 (struct sockaddr *)&ia->ia_addr, 0) == 0)
3692 * XXX why do we care about multlicast here while we don't care
3693 * about IPv4 multicast??
3697 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3702 /* loopback, just for safety */
3703 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
3711 * compare two secasindex structure.
3712 * flag can specify to compare 2 saidxes.
3713 * compare two secasindex structure without both mode and reqid.
3714 * don't compare port.
3716 * saidx0: source, it can be in SAD.
3724 const struct secasindex *saidx0,
3725 const struct secasindex *saidx1,
3729 if (saidx0 == NULL && saidx1 == NULL)
3732 if (saidx0 == NULL || saidx1 == NULL)
3735 if (saidx0->proto != saidx1->proto)
3738 if (flag == CMP_EXACTLY) {
3739 if (saidx0->mode != saidx1->mode)
3741 if (saidx0->reqid != saidx1->reqid)
3743 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
3744 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
3748 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
3749 if (flag == CMP_MODE_REQID
3750 ||flag == CMP_REQID) {
3752 * If reqid of SPD is non-zero, unique SA is required.
3753 * The result must be of same reqid in this case.
3755 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
3759 if (flag == CMP_MODE_REQID) {
3760 if (saidx0->mode != IPSEC_MODE_ANY
3761 && saidx0->mode != saidx1->mode)
3765 if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, 0) != 0) {
3768 if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, 0) != 0) {
3777 * compare two secindex structure exactly.
3779 * spidx0: source, it is often in SPD.
3780 * spidx1: object, it is often from PFKEY message.
3786 key_cmpspidx_exactly(
3787 struct secpolicyindex *spidx0,
3788 struct secpolicyindex *spidx1)
3791 if (spidx0 == NULL && spidx1 == NULL)
3794 if (spidx0 == NULL || spidx1 == NULL)
3797 if (spidx0->prefs != spidx1->prefs
3798 || spidx0->prefd != spidx1->prefd
3799 || spidx0->ul_proto != spidx1->ul_proto)
3802 return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
3803 key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
3807 * compare two secindex structure with mask.
3809 * spidx0: source, it is often in SPD.
3810 * spidx1: object, it is often from IP header.
3816 key_cmpspidx_withmask(
3817 struct secpolicyindex *spidx0,
3818 struct secpolicyindex *spidx1)
3821 if (spidx0 == NULL && spidx1 == NULL)
3824 if (spidx0 == NULL || spidx1 == NULL)
3827 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
3828 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
3829 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
3830 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
3833 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
3834 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
3835 && spidx0->ul_proto != spidx1->ul_proto)
3838 switch (spidx0->src.sa.sa_family) {
3840 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
3841 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
3843 if (!key_bbcmp(&spidx0->src.sin.sin_addr,
3844 &spidx1->src.sin.sin_addr, spidx0->prefs))
3848 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
3849 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
3852 * scope_id check. if sin6_scope_id is 0, we regard it
3853 * as a wildcard scope, which matches any scope zone ID.
3855 if (spidx0->src.sin6.sin6_scope_id &&
3856 spidx1->src.sin6.sin6_scope_id &&
3857 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
3859 if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
3860 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
3865 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
3870 switch (spidx0->dst.sa.sa_family) {
3872 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
3873 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
3875 if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
3876 &spidx1->dst.sin.sin_addr, spidx0->prefd))
3880 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
3881 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
3884 * scope_id check. if sin6_scope_id is 0, we regard it
3885 * as a wildcard scope, which matches any scope zone ID.
3887 if (spidx0->dst.sin6.sin6_scope_id &&
3888 spidx1->dst.sin6.sin6_scope_id &&
3889 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
3891 if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
3892 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
3897 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
3902 /* XXX Do we check other field ? e.g. flowinfo */
3907 /* returns 0 on match */
3910 const struct sockaddr *sa1,
3911 const struct sockaddr *sa2,
3917 #define satosin(s) ((const struct sockaddr_in *)s)
3921 #define satosin6(s) ((const struct sockaddr_in6 *)s)
3922 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
3925 switch (sa1->sa_family) {
3927 if (sa1->sa_len != sizeof(struct sockaddr_in))
3929 if (satosin(sa1)->sin_addr.s_addr !=
3930 satosin(sa2)->sin_addr.s_addr) {
3933 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
3937 if (sa1->sa_len != sizeof(struct sockaddr_in6))
3938 return 1; /*EINVAL*/
3939 if (satosin6(sa1)->sin6_scope_id !=
3940 satosin6(sa2)->sin6_scope_id) {
3943 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
3944 &satosin6(sa2)->sin6_addr)) {
3948 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
3952 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
3963 * compare two buffers with mask.
3967 * bits: Number of bits to compare
3973 key_bbcmp(const void *a1, const void *a2, u_int bits)
3975 const unsigned char *p1 = a1;
3976 const unsigned char *p2 = a2;
3978 /* XXX: This could be considerably faster if we compare a word
3979 * at a time, but it is complicated on LSB Endian machines */
3981 /* Handle null pointers */
3982 if (p1 == NULL || p2 == NULL)
3992 u_int8_t mask = ~((1<<(8-bits))-1);
3993 if ((*p1 & mask) != (*p2 & mask))
3996 return 1; /* Match! */
4000 key_flush_spd(time_t now)
4002 static u_int16_t sptree_scangen = 0;
4003 u_int16_t gen = sptree_scangen++;
4004 struct secpolicy *sp;
4008 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4011 LIST_FOREACH(sp, &sptree[dir], chain) {
4012 if (sp->scangen == gen) /* previously handled */
4015 if (sp->state == IPSEC_SPSTATE_DEAD) {
4016 /* NB: clean entries created by key_spdflush */
4021 if (sp->lifetime == 0 && sp->validtime == 0)
4023 if ((sp->lifetime && now - sp->created > sp->lifetime)
4024 || (sp->validtime && now - sp->lastused > sp->validtime)) {
4025 sp->state = IPSEC_SPSTATE_DEAD;
4037 key_flush_sad(time_t now)
4039 struct secashead *sah, *nextsah;
4040 struct secasvar *sav, *nextsav;
4044 LIST_FOREACH_SAFE(sah, &sahtree, chain, nextsah) {
4045 /* if sah has been dead, then delete it and process next sah. */
4046 if (sah->state == SADB_SASTATE_DEAD) {
4051 /* if LARVAL entry doesn't become MATURE, delete it. */
4052 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_LARVAL], chain, nextsav) {
4053 if (now - sav->created > key_larval_lifetime)
4058 * check MATURE entry to start to send expire message
4061 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_MATURE], chain, nextsav) {
4062 /* we don't need to check. */
4063 if (sav->lft_s == NULL)
4067 if (sav->lft_c == NULL) {
4068 ipseclog((LOG_DEBUG,"%s: there is no CURRENT "
4069 "time, why?\n", __func__));
4073 /* check SOFT lifetime */
4074 if (sav->lft_s->sadb_lifetime_addtime != 0 &&
4075 now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4077 * check SA to be used whether or not.
4078 * when SA hasn't been used, delete it.
4080 if (sav->lft_c->sadb_lifetime_usetime == 0) {
4081 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4084 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4086 * XXX If we keep to send expire
4087 * message in the status of
4088 * DYING. Do remove below code.
4093 /* check SOFT lifetime by bytes */
4095 * XXX I don't know the way to delete this SA
4096 * when new SA is installed. Caution when it's
4097 * installed too big lifetime by time.
4099 else if (sav->lft_s->sadb_lifetime_bytes != 0 &&
4100 sav->lft_s->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4102 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4104 * XXX If we keep to send expire
4105 * message in the status of
4106 * DYING. Do remove below code.
4112 /* check DYING entry to change status to DEAD. */
4113 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DYING], chain, nextsav) {
4114 /* we don't need to check. */
4115 if (sav->lft_h == NULL)
4119 if (sav->lft_c == NULL) {
4120 ipseclog((LOG_DEBUG, "%s: there is no CURRENT "
4121 "time, why?\n", __func__));
4125 if (sav->lft_h->sadb_lifetime_addtime != 0 &&
4126 now - sav->created > sav->lft_h->sadb_lifetime_addtime) {
4127 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4130 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4131 else if (sav->lft_s != NULL
4132 && sav->lft_s->sadb_lifetime_addtime != 0
4133 && now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4135 * XXX: should be checked to be
4136 * installed the valid SA.
4140 * If there is no SA then sending
4146 /* check HARD lifetime by bytes */
4147 else if (sav->lft_h->sadb_lifetime_bytes != 0 &&
4148 sav->lft_h->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4149 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4154 /* delete entry in DEAD */
4155 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DEAD], chain, nextsav) {
4157 if (sav->state != SADB_SASTATE_DEAD) {
4158 ipseclog((LOG_DEBUG, "%s: invalid sav->state "
4159 "(queue: %d SA: %d): kill it anyway\n",
4161 SADB_SASTATE_DEAD, sav->state));
4164 * do not call key_freesav() here.
4165 * sav should already be freed, and sav->refcnt
4166 * shows other references to sav
4167 * (such as from SPD).
4175 key_flush_acq(time_t now)
4177 struct secacq *acq, *nextacq;
4181 for (acq = LIST_FIRST(&acqtree); acq != NULL; acq = nextacq) {
4182 nextacq = LIST_NEXT(acq, chain);
4183 if (now - acq->created > key_blockacq_lifetime
4184 && __LIST_CHAINED(acq)) {
4185 LIST_REMOVE(acq, chain);
4186 free(acq, M_IPSEC_SAQ);
4193 key_flush_spacq(time_t now)
4195 struct secspacq *acq, *nextacq;
4199 for (acq = LIST_FIRST(&spacqtree); acq != NULL; acq = nextacq) {
4200 nextacq = LIST_NEXT(acq, chain);
4201 if (now - acq->created > key_blockacq_lifetime
4202 && __LIST_CHAINED(acq)) {
4203 LIST_REMOVE(acq, chain);
4204 free(acq, M_IPSEC_SAQ);
4212 * scanning SPD and SAD to check status for each entries,
4213 * and do to remove or to expire.
4214 * XXX: year 2038 problem may remain.
4217 key_timehandler(void)
4219 time_t now = time_second;
4224 key_flush_spacq(now);
4226 #ifndef IPSEC_DEBUG2
4227 /* do exchange to tick time !! */
4228 (void)timeout((void *)key_timehandler, (void *)0, hz);
4229 #endif /* IPSEC_DEBUG2 */
4237 key_randomfill(&value, sizeof(value));
4242 key_randomfill(p, l)
4248 static int warn = 1;
4251 n = (size_t)read_random(p, (u_int)l);
4255 bcopy(&v, (u_int8_t *)p + n,
4256 l - n < sizeof(v) ? l - n : sizeof(v));
4260 printf("WARNING: pseudo-random number generator "
4261 "used for IPsec processing\n");
4268 * map SADB_SATYPE_* to IPPROTO_*.
4269 * if satype == SADB_SATYPE then satype is mapped to ~0.
4271 * 0: invalid satype.
4274 key_satype2proto(satype)
4278 case SADB_SATYPE_UNSPEC:
4279 return IPSEC_PROTO_ANY;
4280 case SADB_SATYPE_AH:
4282 case SADB_SATYPE_ESP:
4284 case SADB_X_SATYPE_IPCOMP:
4285 return IPPROTO_IPCOMP;
4286 case SADB_X_SATYPE_TCPSIGNATURE:
4295 * map IPPROTO_* to SADB_SATYPE_*
4297 * 0: invalid protocol type.
4300 key_proto2satype(proto)
4305 return SADB_SATYPE_AH;
4307 return SADB_SATYPE_ESP;
4308 case IPPROTO_IPCOMP:
4309 return SADB_X_SATYPE_IPCOMP;
4311 return SADB_X_SATYPE_TCPSIGNATURE;
4320 * SADB_GETSPI processing is to receive
4321 * <base, (SA2), src address, dst address, (SPI range)>
4322 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4323 * tree with the status of LARVAL, and send
4324 * <base, SA(*), address(SD)>
4327 * IN: mhp: pointer to the pointer to each header.
4328 * OUT: NULL if fail.
4329 * other if success, return pointer to the message to send.
4332 key_getspi(so, m, mhp)
4335 const struct sadb_msghdr *mhp;
4337 struct sadb_address *src0, *dst0;
4338 struct secasindex saidx;
4339 struct secashead *newsah;
4340 struct secasvar *newsav;
4347 IPSEC_ASSERT(so != NULL, ("null socket"));
4348 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4349 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4350 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4352 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4353 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4354 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4356 return key_senderror(so, m, EINVAL);
4358 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4359 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4360 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4362 return key_senderror(so, m, EINVAL);
4364 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4365 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4366 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4368 mode = IPSEC_MODE_ANY;
4372 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4373 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4375 /* map satype to proto */
4376 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4377 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4379 return key_senderror(so, m, EINVAL);
4382 /* make sure if port number is zero. */
4383 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4385 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4386 sizeof(struct sockaddr_in))
4387 return key_senderror(so, m, EINVAL);
4388 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4391 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4392 sizeof(struct sockaddr_in6))
4393 return key_senderror(so, m, EINVAL);
4394 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4399 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4401 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4402 sizeof(struct sockaddr_in))
4403 return key_senderror(so, m, EINVAL);
4404 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4407 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4408 sizeof(struct sockaddr_in6))
4409 return key_senderror(so, m, EINVAL);
4410 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4416 /* XXX boundary check against sa_len */
4417 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4419 /* SPI allocation */
4420 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4423 return key_senderror(so, m, EINVAL);
4425 /* get a SA index */
4426 if ((newsah = key_getsah(&saidx)) == NULL) {
4427 /* create a new SA index */
4428 if ((newsah = key_newsah(&saidx)) == NULL) {
4429 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
4430 return key_senderror(so, m, ENOBUFS);
4436 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4437 if (newsav == NULL) {
4438 /* XXX don't free new SA index allocated in above. */
4439 return key_senderror(so, m, error);
4443 newsav->spi = htonl(spi);
4445 /* delete the entry in acqtree */
4446 if (mhp->msg->sadb_msg_seq != 0) {
4448 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4449 /* reset counter in order to deletion by timehandler. */
4450 acq->created = time_second;
4456 struct mbuf *n, *nn;
4457 struct sadb_sa *m_sa;
4458 struct sadb_msg *newmsg;
4461 /* create new sadb_msg to reply. */
4462 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4463 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4465 return key_senderror(so, m, ENOBUFS);
4467 MGETHDR(n, M_DONTWAIT, MT_DATA);
4469 MCLGET(n, M_DONTWAIT);
4470 if ((n->m_flags & M_EXT) == 0) {
4476 return key_senderror(so, m, ENOBUFS);
4482 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4483 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4485 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4486 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4487 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4488 m_sa->sadb_sa_spi = htonl(spi);
4489 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4491 IPSEC_ASSERT(off == len,
4492 ("length inconsistency (off %u len %u)", off, len));
4494 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4495 SADB_EXT_ADDRESS_DST);
4498 return key_senderror(so, m, ENOBUFS);
4501 if (n->m_len < sizeof(struct sadb_msg)) {
4502 n = m_pullup(n, sizeof(struct sadb_msg));
4504 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4507 n->m_pkthdr.len = 0;
4508 for (nn = n; nn; nn = nn->m_next)
4509 n->m_pkthdr.len += nn->m_len;
4511 newmsg = mtod(n, struct sadb_msg *);
4512 newmsg->sadb_msg_seq = newsav->seq;
4513 newmsg->sadb_msg_errno = 0;
4514 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4517 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4522 * allocating new SPI
4523 * called by key_getspi().
4529 key_do_getnewspi(spirange, saidx)
4530 struct sadb_spirange *spirange;
4531 struct secasindex *saidx;
4535 int count = key_spi_trycnt;
4537 /* set spi range to allocate */
4538 if (spirange != NULL) {
4539 min = spirange->sadb_spirange_min;
4540 max = spirange->sadb_spirange_max;
4542 min = key_spi_minval;
4543 max = key_spi_maxval;
4545 /* IPCOMP needs 2-byte SPI */
4546 if (saidx->proto == IPPROTO_IPCOMP) {
4553 t = min; min = max; max = t;
4558 if (key_checkspidup(saidx, min) != NULL) {
4559 ipseclog((LOG_DEBUG, "%s: SPI %u exists already.\n",
4564 count--; /* taking one cost. */
4572 /* when requesting to allocate spi ranged */
4574 /* generate pseudo-random SPI value ranged. */
4575 newspi = min + (key_random() % (max - min + 1));
4577 if (key_checkspidup(saidx, newspi) == NULL)
4581 if (count == 0 || newspi == 0) {
4582 ipseclog((LOG_DEBUG, "%s: to allocate spi is failed.\n",
4589 keystat.getspi_count =
4590 (keystat.getspi_count + key_spi_trycnt - count) / 2;
4596 * SADB_UPDATE processing
4598 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4599 * key(AE), (identity(SD),) (sensitivity)>
4600 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4602 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4603 * (identity(SD),) (sensitivity)>
4606 * m will always be freed.
4609 key_update(so, m, mhp)
4612 const struct sadb_msghdr *mhp;
4614 struct sadb_sa *sa0;
4615 struct sadb_address *src0, *dst0;
4616 struct secasindex saidx;
4617 struct secashead *sah;
4618 struct secasvar *sav;
4624 IPSEC_ASSERT(so != NULL, ("null socket"));
4625 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4626 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4627 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4629 /* map satype to proto */
4630 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4631 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4633 return key_senderror(so, m, EINVAL);
4636 if (mhp->ext[SADB_EXT_SA] == NULL ||
4637 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4638 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4639 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4640 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4641 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4642 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4643 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4644 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4645 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4646 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4647 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4649 return key_senderror(so, m, EINVAL);
4651 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4652 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4653 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4654 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4656 return key_senderror(so, m, EINVAL);
4658 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4659 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4660 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4662 mode = IPSEC_MODE_ANY;
4665 /* XXX boundary checking for other extensions */
4667 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4668 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4669 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4671 /* XXX boundary check against sa_len */
4672 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4674 /* get a SA header */
4675 if ((sah = key_getsah(&saidx)) == NULL) {
4676 ipseclog((LOG_DEBUG, "%s: no SA index found.\n", __func__));
4677 return key_senderror(so, m, ENOENT);
4680 /* set spidx if there */
4682 error = key_setident(sah, m, mhp);
4684 return key_senderror(so, m, error);
4686 /* find a SA with sequence number. */
4687 #ifdef IPSEC_DOSEQCHECK
4688 if (mhp->msg->sadb_msg_seq != 0
4689 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
4690 ipseclog((LOG_DEBUG, "%s: no larval SA with sequence %u "
4691 "exists.\n", __func__, mhp->msg->sadb_msg_seq));
4692 return key_senderror(so, m, ENOENT);
4696 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
4699 ipseclog((LOG_DEBUG, "%s: no such a SA found (spi:%u)\n",
4700 __func__, (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4701 return key_senderror(so, m, EINVAL);
4705 /* validity check */
4706 if (sav->sah->saidx.proto != proto) {
4707 ipseclog((LOG_DEBUG, "%s: protocol mismatched "
4708 "(DB=%u param=%u)\n", __func__,
4709 sav->sah->saidx.proto, proto));
4710 return key_senderror(so, m, EINVAL);
4712 #ifdef IPSEC_DOSEQCHECK
4713 if (sav->spi != sa0->sadb_sa_spi) {
4714 ipseclog((LOG_DEBUG, "%s: SPI mismatched (DB:%u param:%u)\n",
4716 (u_int32_t)ntohl(sav->spi),
4717 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4718 return key_senderror(so, m, EINVAL);
4721 if (sav->pid != mhp->msg->sadb_msg_pid) {
4722 ipseclog((LOG_DEBUG, "%s: pid mismatched (DB:%u param:%u)\n",
4723 __func__, sav->pid, mhp->msg->sadb_msg_pid));
4724 return key_senderror(so, m, EINVAL);
4727 /* copy sav values */
4728 error = key_setsaval(sav, m, mhp);
4731 return key_senderror(so, m, error);
4734 /* check SA values to be mature. */
4735 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
4737 return key_senderror(so, m, 0);
4743 /* set msg buf from mhp */
4744 n = key_getmsgbuf_x1(m, mhp);
4746 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
4747 return key_senderror(so, m, ENOBUFS);
4751 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4756 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
4757 * only called by key_update().
4760 * others : found, pointer to a SA.
4762 #ifdef IPSEC_DOSEQCHECK
4763 static struct secasvar *
4764 key_getsavbyseq(sah, seq)
4765 struct secashead *sah;
4768 struct secasvar *sav;
4771 state = SADB_SASTATE_LARVAL;
4773 /* search SAD with sequence number ? */
4774 LIST_FOREACH(sav, &sah->savtree[state], chain) {
4776 KEY_CHKSASTATE(state, sav->state, __func__);
4778 if (sav->seq == seq) {
4780 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
4781 printf("DP %s cause refcnt++:%d SA:%p\n",
4782 __func__, sav->refcnt, sav));
4792 * SADB_ADD processing
4793 * add an entry to SA database, when received
4794 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4795 * key(AE), (identity(SD),) (sensitivity)>
4798 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4799 * (identity(SD),) (sensitivity)>
4802 * IGNORE identity and sensitivity messages.
4804 * m will always be freed.
4810 const struct sadb_msghdr *mhp;
4812 struct sadb_sa *sa0;
4813 struct sadb_address *src0, *dst0;
4814 struct secasindex saidx;
4815 struct secashead *newsah;
4816 struct secasvar *newsav;
4822 IPSEC_ASSERT(so != NULL, ("null socket"));
4823 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4824 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4825 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4827 /* map satype to proto */
4828 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4829 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4831 return key_senderror(so, m, EINVAL);
4834 if (mhp->ext[SADB_EXT_SA] == NULL ||
4835 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4836 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4837 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4838 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4839 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4840 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4841 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4842 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4843 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4844 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4845 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4847 return key_senderror(so, m, EINVAL);
4849 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4850 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4851 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4853 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4855 return key_senderror(so, m, EINVAL);
4857 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4858 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4859 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4861 mode = IPSEC_MODE_ANY;
4865 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4866 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
4867 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
4869 /* XXX boundary check against sa_len */
4870 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4872 /* get a SA header */
4873 if ((newsah = key_getsah(&saidx)) == NULL) {
4874 /* create a new SA header */
4875 if ((newsah = key_newsah(&saidx)) == NULL) {
4876 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
4877 return key_senderror(so, m, ENOBUFS);
4881 /* set spidx if there */
4883 error = key_setident(newsah, m, mhp);
4885 return key_senderror(so, m, error);
4888 /* create new SA entry. */
4889 /* We can create new SA only if SPI is differenct. */
4891 newsav = key_getsavbyspi(newsah, sa0->sadb_sa_spi);
4893 if (newsav != NULL) {
4894 ipseclog((LOG_DEBUG, "%s: SA already exists.\n", __func__));
4895 return key_senderror(so, m, EEXIST);
4897 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4898 if (newsav == NULL) {
4899 return key_senderror(so, m, error);
4902 /* check SA values to be mature. */
4903 if ((error = key_mature(newsav)) != 0) {
4904 KEY_FREESAV(&newsav);
4905 return key_senderror(so, m, error);
4909 * don't call key_freesav() here, as we would like to keep the SA
4910 * in the database on success.
4916 /* set msg buf from mhp */
4917 n = key_getmsgbuf_x1(m, mhp);
4919 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
4920 return key_senderror(so, m, ENOBUFS);
4924 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4930 key_setident(sah, m, mhp)
4931 struct secashead *sah;
4933 const struct sadb_msghdr *mhp;
4935 const struct sadb_ident *idsrc, *iddst;
4936 int idsrclen, iddstlen;
4938 IPSEC_ASSERT(sah != NULL, ("null secashead"));
4939 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4940 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4941 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4943 /* don't make buffer if not there */
4944 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
4945 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
4951 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
4952 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
4953 ipseclog((LOG_DEBUG, "%s: invalid identity.\n", __func__));
4957 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
4958 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
4959 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
4960 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
4962 /* validity check */
4963 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
4964 ipseclog((LOG_DEBUG, "%s: ident type mismatch.\n", __func__));
4968 switch (idsrc->sadb_ident_type) {
4969 case SADB_IDENTTYPE_PREFIX:
4970 case SADB_IDENTTYPE_FQDN:
4971 case SADB_IDENTTYPE_USERFQDN:
4973 /* XXX do nothing */
4979 /* make structure */
4980 sah->idents = malloc(idsrclen, M_IPSEC_MISC, M_NOWAIT);
4981 if (sah->idents == NULL) {
4982 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
4985 sah->identd = malloc(iddstlen, M_IPSEC_MISC, M_NOWAIT);
4986 if (sah->identd == NULL) {
4987 free(sah->idents, M_IPSEC_MISC);
4989 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
4992 bcopy(idsrc, sah->idents, idsrclen);
4993 bcopy(iddst, sah->identd, iddstlen);
4999 * m will not be freed on return.
5000 * it is caller's responsibility to free the result.
5002 static struct mbuf *
5003 key_getmsgbuf_x1(m, mhp)
5005 const struct sadb_msghdr *mhp;
5009 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5010 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5011 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5013 /* create new sadb_msg to reply. */
5014 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5015 SADB_EXT_SA, SADB_X_EXT_SA2,
5016 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5017 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5018 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5022 if (n->m_len < sizeof(struct sadb_msg)) {
5023 n = m_pullup(n, sizeof(struct sadb_msg));
5027 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5028 mtod(n, struct sadb_msg *)->sadb_msg_len =
5029 PFKEY_UNIT64(n->m_pkthdr.len);
5034 static int key_delete_all __P((struct socket *, struct mbuf *,
5035 const struct sadb_msghdr *, u_int16_t));
5038 * SADB_DELETE processing
5040 * <base, SA(*), address(SD)>
5041 * from the ikmpd, and set SADB_SASTATE_DEAD,
5043 * <base, SA(*), address(SD)>
5046 * m will always be freed.
5049 key_delete(so, m, mhp)
5052 const struct sadb_msghdr *mhp;
5054 struct sadb_sa *sa0;
5055 struct sadb_address *src0, *dst0;
5056 struct secasindex saidx;
5057 struct secashead *sah;
5058 struct secasvar *sav = NULL;
5061 IPSEC_ASSERT(so != NULL, ("null socket"));
5062 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5063 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5064 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5066 /* map satype to proto */
5067 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5068 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5070 return key_senderror(so, m, EINVAL);
5073 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5074 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5075 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5077 return key_senderror(so, m, EINVAL);
5080 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5081 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5082 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5084 return key_senderror(so, m, EINVAL);
5087 if (mhp->ext[SADB_EXT_SA] == NULL) {
5089 * Caller wants us to delete all non-LARVAL SAs
5090 * that match the src/dst. This is used during
5091 * IKE INITIAL-CONTACT.
5093 ipseclog((LOG_DEBUG, "%s: doing delete all.\n", __func__));
5094 return key_delete_all(so, m, mhp, proto);
5095 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5096 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5098 return key_senderror(so, m, EINVAL);
5101 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5102 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5103 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5105 /* XXX boundary check against sa_len */
5106 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5108 /* get a SA header */
5110 LIST_FOREACH(sah, &sahtree, chain) {
5111 if (sah->state == SADB_SASTATE_DEAD)
5113 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5116 /* get a SA with SPI. */
5117 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5123 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5124 return key_senderror(so, m, ENOENT);
5127 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5133 struct sadb_msg *newmsg;
5135 /* create new sadb_msg to reply. */
5136 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5137 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5139 return key_senderror(so, m, ENOBUFS);
5141 if (n->m_len < sizeof(struct sadb_msg)) {
5142 n = m_pullup(n, sizeof(struct sadb_msg));
5144 return key_senderror(so, m, ENOBUFS);
5146 newmsg = mtod(n, struct sadb_msg *);
5147 newmsg->sadb_msg_errno = 0;
5148 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5151 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5156 * delete all SAs for src/dst. Called from key_delete().
5159 key_delete_all(so, m, mhp, proto)
5162 const struct sadb_msghdr *mhp;
5165 struct sadb_address *src0, *dst0;
5166 struct secasindex saidx;
5167 struct secashead *sah;
5168 struct secasvar *sav, *nextsav;
5169 u_int stateidx, state;
5171 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5172 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5174 /* XXX boundary check against sa_len */
5175 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5178 LIST_FOREACH(sah, &sahtree, chain) {
5179 if (sah->state == SADB_SASTATE_DEAD)
5181 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5184 /* Delete all non-LARVAL SAs. */
5186 stateidx < _ARRAYLEN(saorder_state_alive);
5188 state = saorder_state_alive[stateidx];
5189 if (state == SADB_SASTATE_LARVAL)
5191 for (sav = LIST_FIRST(&sah->savtree[state]);
5192 sav != NULL; sav = nextsav) {
5193 nextsav = LIST_NEXT(sav, chain);
5195 if (sav->state != state) {
5196 ipseclog((LOG_DEBUG, "%s: invalid "
5197 "sav->state (queue %d SA %d)\n",
5198 __func__, state, sav->state));
5202 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5210 struct sadb_msg *newmsg;
5212 /* create new sadb_msg to reply. */
5213 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5214 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5216 return key_senderror(so, m, ENOBUFS);
5218 if (n->m_len < sizeof(struct sadb_msg)) {
5219 n = m_pullup(n, sizeof(struct sadb_msg));
5221 return key_senderror(so, m, ENOBUFS);
5223 newmsg = mtod(n, struct sadb_msg *);
5224 newmsg->sadb_msg_errno = 0;
5225 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5228 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5233 * SADB_GET processing
5235 * <base, SA(*), address(SD)>
5236 * from the ikmpd, and get a SP and a SA to respond,
5238 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5239 * (identity(SD),) (sensitivity)>
5242 * m will always be freed.
5248 const struct sadb_msghdr *mhp;
5250 struct sadb_sa *sa0;
5251 struct sadb_address *src0, *dst0;
5252 struct secasindex saidx;
5253 struct secashead *sah;
5254 struct secasvar *sav = NULL;
5257 IPSEC_ASSERT(so != NULL, ("null socket"));
5258 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5259 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5260 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5262 /* map satype to proto */
5263 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5264 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5266 return key_senderror(so, m, EINVAL);
5269 if (mhp->ext[SADB_EXT_SA] == NULL ||
5270 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5271 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5272 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5274 return key_senderror(so, m, EINVAL);
5276 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5277 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5278 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5279 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5281 return key_senderror(so, m, EINVAL);
5284 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5285 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5286 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5288 /* XXX boundary check against sa_len */
5289 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5291 /* get a SA header */
5293 LIST_FOREACH(sah, &sahtree, chain) {
5294 if (sah->state == SADB_SASTATE_DEAD)
5296 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5299 /* get a SA with SPI. */
5300 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5306 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5307 return key_senderror(so, m, ENOENT);
5314 /* map proto to satype */
5315 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5316 ipseclog((LOG_DEBUG, "%s: there was invalid proto in SAD.\n",
5318 return key_senderror(so, m, EINVAL);
5321 /* create new sadb_msg to reply. */
5322 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5323 mhp->msg->sadb_msg_pid);
5325 return key_senderror(so, m, ENOBUFS);
5328 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5332 /* XXX make it sysctl-configurable? */
5334 key_getcomb_setlifetime(comb)
5335 struct sadb_comb *comb;
5338 comb->sadb_comb_soft_allocations = 1;
5339 comb->sadb_comb_hard_allocations = 1;
5340 comb->sadb_comb_soft_bytes = 0;
5341 comb->sadb_comb_hard_bytes = 0;
5342 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5343 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5344 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5345 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
5349 * XXX reorder combinations by preference
5350 * XXX no idea if the user wants ESP authentication or not
5352 static struct mbuf *
5355 struct sadb_comb *comb;
5356 struct enc_xform *algo;
5357 struct mbuf *result = NULL, *m, *n;
5361 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5364 for (i = 1; i <= SADB_EALG_MAX; i++) {
5365 algo = esp_algorithm_lookup(i);
5369 /* discard algorithms with key size smaller than system min */
5370 if (_BITS(algo->maxkey) < ipsec_esp_keymin)
5372 if (_BITS(algo->minkey) < ipsec_esp_keymin)
5373 encmin = ipsec_esp_keymin;
5375 encmin = _BITS(algo->minkey);
5378 m = key_getcomb_ah();
5380 IPSEC_ASSERT(l <= MLEN,
5381 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
5382 MGET(m, M_DONTWAIT, MT_DATA);
5387 bzero(mtod(m, caddr_t), m->m_len);
5394 for (n = m; n; n = n->m_next)
5396 IPSEC_ASSERT((totlen % l) == 0, ("totlen=%u, l=%u", totlen, l));
5398 for (off = 0; off < totlen; off += l) {
5399 n = m_pulldown(m, off, l, &o);
5401 /* m is already freed */
5404 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
5405 bzero(comb, sizeof(*comb));
5406 key_getcomb_setlifetime(comb);
5407 comb->sadb_comb_encrypt = i;
5408 comb->sadb_comb_encrypt_minbits = encmin;
5409 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
5428 const struct auth_hash *ah,
5433 *min = *max = ah->keysize;
5434 if (ah->keysize == 0) {
5436 * Transform takes arbitrary key size but algorithm
5437 * key size is restricted. Enforce this here.
5440 case SADB_X_AALG_MD5: *min = *max = 16; break;
5441 case SADB_X_AALG_SHA: *min = *max = 20; break;
5442 case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
5444 DPRINTF(("%s: unknown AH algorithm %u\n",
5452 * XXX reorder combinations by preference
5454 static struct mbuf *
5457 struct sadb_comb *comb;
5458 struct auth_hash *algo;
5460 u_int16_t minkeysize, maxkeysize;
5462 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5465 for (i = 1; i <= SADB_AALG_MAX; i++) {
5467 /* we prefer HMAC algorithms, not old algorithms */
5468 if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC)
5471 algo = ah_algorithm_lookup(i);
5474 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
5475 /* discard algorithms with key size smaller than system min */
5476 if (_BITS(minkeysize) < ipsec_ah_keymin)
5480 IPSEC_ASSERT(l <= MLEN,
5481 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
5482 MGET(m, M_DONTWAIT, MT_DATA);
5489 M_PREPEND(m, l, M_DONTWAIT);
5493 comb = mtod(m, struct sadb_comb *);
5494 bzero(comb, sizeof(*comb));
5495 key_getcomb_setlifetime(comb);
5496 comb->sadb_comb_auth = i;
5497 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
5498 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
5505 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
5506 * XXX reorder combinations by preference
5508 static struct mbuf *
5509 key_getcomb_ipcomp()
5511 struct sadb_comb *comb;
5512 struct comp_algo *algo;
5515 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5518 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
5519 algo = ipcomp_algorithm_lookup(i);
5524 IPSEC_ASSERT(l <= MLEN,
5525 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
5526 MGET(m, M_DONTWAIT, MT_DATA);
5533 M_PREPEND(m, l, M_DONTWAIT);
5537 comb = mtod(m, struct sadb_comb *);
5538 bzero(comb, sizeof(*comb));
5539 key_getcomb_setlifetime(comb);
5540 comb->sadb_comb_encrypt = i;
5541 /* what should we set into sadb_comb_*_{min,max}bits? */
5548 * XXX no way to pass mode (transport/tunnel) to userland
5549 * XXX replay checking?
5550 * XXX sysctl interface to ipsec_{ah,esp}_keymin
5552 static struct mbuf *
5554 const struct secasindex *saidx;
5556 struct sadb_prop *prop;
5558 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
5561 switch (saidx->proto) {
5563 m = key_getcomb_esp();
5566 m = key_getcomb_ah();
5568 case IPPROTO_IPCOMP:
5569 m = key_getcomb_ipcomp();
5577 M_PREPEND(m, l, M_DONTWAIT);
5582 for (n = m; n; n = n->m_next)
5585 prop = mtod(m, struct sadb_prop *);
5586 bzero(prop, sizeof(*prop));
5587 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
5588 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
5589 prop->sadb_prop_replay = 32; /* XXX */
5595 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5597 * <base, SA, address(SD), (address(P)), x_policy,
5598 * (identity(SD),) (sensitivity,) proposal>
5599 * to KMD, and expect to receive
5600 * <base> with SADB_ACQUIRE if error occured,
5602 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5603 * from KMD by PF_KEY.
5605 * XXX x_policy is outside of RFC2367 (KAME extension).
5606 * XXX sensitivity is not supported.
5607 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
5608 * see comment for key_getcomb_ipcomp().
5612 * others: error number
5615 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
5617 struct mbuf *result = NULL, *m;
5618 struct secacq *newacq;
5623 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
5624 satype = key_proto2satype(saidx->proto);
5625 IPSEC_ASSERT(satype != 0, ("null satype, protocol %u", saidx->proto));
5628 * We never do anything about acquirng SA. There is anather
5629 * solution that kernel blocks to send SADB_ACQUIRE message until
5630 * getting something message from IKEd. In later case, to be
5631 * managed with ACQUIRING list.
5633 /* Get an entry to check whether sending message or not. */
5634 if ((newacq = key_getacq(saidx)) != NULL) {
5635 if (key_blockacq_count < newacq->count) {
5636 /* reset counter and do send message. */
5639 /* increment counter and do nothing. */
5644 /* make new entry for blocking to send SADB_ACQUIRE. */
5645 if ((newacq = key_newacq(saidx)) == NULL)
5651 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
5658 /* set sadb_address for saidx's. */
5659 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
5660 &saidx->src.sa, FULLMASK, IPSEC_ULPROTO_ANY);
5667 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
5668 &saidx->dst.sa, FULLMASK, IPSEC_ULPROTO_ANY);
5675 /* XXX proxy address (optional) */
5677 /* set sadb_x_policy */
5679 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
5687 /* XXX identity (optional) */
5689 if (idexttype && fqdn) {
5690 /* create identity extension (FQDN) */
5691 struct sadb_ident *id;
5694 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
5695 id = (struct sadb_ident *)p;
5696 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5697 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5698 id->sadb_ident_exttype = idexttype;
5699 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
5700 bcopy(fqdn, id + 1, fqdnlen);
5701 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
5705 /* create identity extension (USERFQDN) */
5706 struct sadb_ident *id;
5710 /* +1 for terminating-NUL */
5711 userfqdnlen = strlen(userfqdn) + 1;
5714 id = (struct sadb_ident *)p;
5715 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5716 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5717 id->sadb_ident_exttype = idexttype;
5718 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
5719 /* XXX is it correct? */
5720 if (curproc && curproc->p_cred)
5721 id->sadb_ident_id = curproc->p_cred->p_ruid;
5722 if (userfqdn && userfqdnlen)
5723 bcopy(userfqdn, id + 1, userfqdnlen);
5724 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
5728 /* XXX sensitivity (optional) */
5730 /* create proposal/combination extension */
5731 m = key_getprop(saidx);
5734 * spec conformant: always attach proposal/combination extension,
5735 * the problem is that we have no way to attach it for ipcomp,
5736 * due to the way sadb_comb is declared in RFC2367.
5745 * outside of spec; make proposal/combination extension optional.
5751 if ((result->m_flags & M_PKTHDR) == 0) {
5756 if (result->m_len < sizeof(struct sadb_msg)) {
5757 result = m_pullup(result, sizeof(struct sadb_msg));
5758 if (result == NULL) {
5764 result->m_pkthdr.len = 0;
5765 for (m = result; m; m = m->m_next)
5766 result->m_pkthdr.len += m->m_len;
5768 mtod(result, struct sadb_msg *)->sadb_msg_len =
5769 PFKEY_UNIT64(result->m_pkthdr.len);
5771 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
5779 static struct secacq *
5780 key_newacq(const struct secasindex *saidx)
5782 struct secacq *newacq;
5785 newacq = malloc(sizeof(struct secacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
5786 if (newacq == NULL) {
5787 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5792 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
5793 newacq->seq = (acq_seq == ~0 ? 1 : ++acq_seq);
5794 newacq->created = time_second;
5797 /* add to acqtree */
5799 LIST_INSERT_HEAD(&acqtree, newacq, chain);
5805 static struct secacq *
5806 key_getacq(const struct secasindex *saidx)
5811 LIST_FOREACH(acq, &acqtree, chain) {
5812 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
5820 static struct secacq *
5821 key_getacqbyseq(seq)
5827 LIST_FOREACH(acq, &acqtree, chain) {
5828 if (acq->seq == seq)
5836 static struct secspacq *
5838 struct secpolicyindex *spidx;
5840 struct secspacq *acq;
5843 acq = malloc(sizeof(struct secspacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
5845 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5850 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
5851 acq->created = time_second;
5854 /* add to spacqtree */
5856 LIST_INSERT_HEAD(&spacqtree, acq, chain);
5862 static struct secspacq *
5864 struct secpolicyindex *spidx;
5866 struct secspacq *acq;
5869 LIST_FOREACH(acq, &spacqtree, chain) {
5870 if (key_cmpspidx_exactly(spidx, &acq->spidx)) {
5871 /* NB: return holding spacq_lock */
5881 * SADB_ACQUIRE processing,
5882 * in first situation, is receiving
5884 * from the ikmpd, and clear sequence of its secasvar entry.
5886 * In second situation, is receiving
5887 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5888 * from a user land process, and return
5889 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5892 * m will always be freed.
5895 key_acquire2(so, m, mhp)
5898 const struct sadb_msghdr *mhp;
5900 const struct sadb_address *src0, *dst0;
5901 struct secasindex saidx;
5902 struct secashead *sah;
5906 IPSEC_ASSERT(so != NULL, ("null socket"));
5907 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5908 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5909 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5912 * Error message from KMd.
5913 * We assume that if error was occured in IKEd, the length of PFKEY
5914 * message is equal to the size of sadb_msg structure.
5915 * We do not raise error even if error occured in this function.
5917 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
5920 /* check sequence number */
5921 if (mhp->msg->sadb_msg_seq == 0) {
5922 ipseclog((LOG_DEBUG, "%s: must specify sequence "
5923 "number.\n", __func__));
5928 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
5930 * the specified larval SA is already gone, or we got
5931 * a bogus sequence number. we can silently ignore it.
5937 /* reset acq counter in order to deletion by timehander. */
5938 acq->created = time_second;
5945 * This message is from user land.
5948 /* map satype to proto */
5949 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5950 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5952 return key_senderror(so, m, EINVAL);
5955 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5956 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5957 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
5959 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5961 return key_senderror(so, m, EINVAL);
5963 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5964 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
5965 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
5967 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5969 return key_senderror(so, m, EINVAL);
5972 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5973 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5975 /* XXX boundary check against sa_len */
5976 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5978 /* get a SA index */
5980 LIST_FOREACH(sah, &sahtree, chain) {
5981 if (sah->state == SADB_SASTATE_DEAD)
5983 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
5988 ipseclog((LOG_DEBUG, "%s: a SA exists already.\n", __func__));
5989 return key_senderror(so, m, EEXIST);
5992 error = key_acquire(&saidx, NULL);
5994 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
5995 __func__, mhp->msg->sadb_msg_errno));
5996 return key_senderror(so, m, error);
5999 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6003 * SADB_REGISTER processing.
6004 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6007 * from the ikmpd, and register a socket to send PF_KEY messages,
6011 * If socket is detached, must free from regnode.
6013 * m will always be freed.
6016 key_register(so, m, mhp)
6019 const struct sadb_msghdr *mhp;
6021 struct secreg *reg, *newreg = 0;
6023 IPSEC_ASSERT(so != NULL, ("null socket"));
6024 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6025 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6026 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6028 /* check for invalid register message */
6029 if (mhp->msg->sadb_msg_satype >= sizeof(regtree)/sizeof(regtree[0]))
6030 return key_senderror(so, m, EINVAL);
6032 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6033 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6036 /* check whether existing or not */
6038 LIST_FOREACH(reg, ®tree[mhp->msg->sadb_msg_satype], chain) {
6039 if (reg->so == so) {
6041 ipseclog((LOG_DEBUG, "%s: socket exists already.\n",
6043 return key_senderror(so, m, EEXIST);
6047 /* create regnode */
6048 newreg = malloc(sizeof(struct secreg), M_IPSEC_SAR, M_NOWAIT|M_ZERO);
6049 if (newreg == NULL) {
6051 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6052 return key_senderror(so, m, ENOBUFS);
6056 ((struct keycb *)sotorawcb(so))->kp_registered++;
6058 /* add regnode to regtree. */
6059 LIST_INSERT_HEAD(®tree[mhp->msg->sadb_msg_satype], newreg, chain);
6065 struct sadb_msg *newmsg;
6066 struct sadb_supported *sup;
6067 u_int len, alen, elen;
6070 struct sadb_alg *alg;
6072 /* create new sadb_msg to reply. */
6074 for (i = 1; i <= SADB_AALG_MAX; i++) {
6075 if (ah_algorithm_lookup(i))
6076 alen += sizeof(struct sadb_alg);
6079 alen += sizeof(struct sadb_supported);
6081 for (i = 1; i <= SADB_EALG_MAX; i++) {
6082 if (esp_algorithm_lookup(i))
6083 elen += sizeof(struct sadb_alg);
6086 elen += sizeof(struct sadb_supported);
6088 len = sizeof(struct sadb_msg) + alen + elen;
6091 return key_senderror(so, m, ENOBUFS);
6093 MGETHDR(n, M_DONTWAIT, MT_DATA);
6095 MCLGET(n, M_DONTWAIT);
6096 if ((n->m_flags & M_EXT) == 0) {
6102 return key_senderror(so, m, ENOBUFS);
6104 n->m_pkthdr.len = n->m_len = len;
6108 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6109 newmsg = mtod(n, struct sadb_msg *);
6110 newmsg->sadb_msg_errno = 0;
6111 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6112 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6114 /* for authentication algorithm */
6116 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6117 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6118 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6119 off += PFKEY_ALIGN8(sizeof(*sup));
6121 for (i = 1; i <= SADB_AALG_MAX; i++) {
6122 struct auth_hash *aalgo;
6123 u_int16_t minkeysize, maxkeysize;
6125 aalgo = ah_algorithm_lookup(i);
6128 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6129 alg->sadb_alg_id = i;
6130 alg->sadb_alg_ivlen = 0;
6131 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
6132 alg->sadb_alg_minbits = _BITS(minkeysize);
6133 alg->sadb_alg_maxbits = _BITS(maxkeysize);
6134 off += PFKEY_ALIGN8(sizeof(*alg));
6138 /* for encryption algorithm */
6140 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6141 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6142 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6143 off += PFKEY_ALIGN8(sizeof(*sup));
6145 for (i = 1; i <= SADB_EALG_MAX; i++) {
6146 struct enc_xform *ealgo;
6148 ealgo = esp_algorithm_lookup(i);
6151 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6152 alg->sadb_alg_id = i;
6153 alg->sadb_alg_ivlen = ealgo->blocksize;
6154 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
6155 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
6156 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6160 IPSEC_ASSERT(off == len,
6161 ("length assumption failed (off %u len %u)", off, len));
6164 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6169 * free secreg entry registered.
6170 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6173 key_freereg(struct socket *so)
6178 IPSEC_ASSERT(so != NULL, ("NULL so"));
6181 * check whether existing or not.
6182 * check all type of SA, because there is a potential that
6183 * one socket is registered to multiple type of SA.
6186 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6187 LIST_FOREACH(reg, ®tree[i], chain) {
6188 if (reg->so == so && __LIST_CHAINED(reg)) {
6189 LIST_REMOVE(reg, chain);
6190 free(reg, M_IPSEC_SAR);
6199 * SADB_EXPIRE processing
6201 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6203 * NOTE: We send only soft lifetime extension.
6206 * others : error number
6209 key_expire(struct secasvar *sav)
6213 struct mbuf *result = NULL, *m;
6216 struct sadb_lifetime *lt;
6218 /* XXX: Why do we lock ? */
6219 s = splnet(); /*called from softclock()*/
6221 IPSEC_ASSERT (sav != NULL, ("null sav"));
6222 IPSEC_ASSERT (sav->sah != NULL, ("null sa header"));
6224 /* set msg header */
6225 satype = key_proto2satype(sav->sah->saidx.proto);
6226 IPSEC_ASSERT(satype != 0, ("invalid proto, satype %u", satype));
6227 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6234 /* create SA extension */
6235 m = key_setsadbsa(sav);
6242 /* create SA extension */
6243 m = key_setsadbxsa2(sav->sah->saidx.mode,
6244 sav->replay ? sav->replay->count : 0,
6245 sav->sah->saidx.reqid);
6252 /* create lifetime extension (current and soft) */
6253 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6254 m = key_alloc_mbuf(len);
6255 if (!m || m->m_next) { /*XXX*/
6261 bzero(mtod(m, caddr_t), len);
6262 lt = mtod(m, struct sadb_lifetime *);
6263 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6264 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6265 lt->sadb_lifetime_allocations = sav->lft_c->sadb_lifetime_allocations;
6266 lt->sadb_lifetime_bytes = sav->lft_c->sadb_lifetime_bytes;
6267 lt->sadb_lifetime_addtime = sav->lft_c->sadb_lifetime_addtime;
6268 lt->sadb_lifetime_usetime = sav->lft_c->sadb_lifetime_usetime;
6269 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6270 bcopy(sav->lft_s, lt, sizeof(*lt));
6273 /* set sadb_address for source */
6274 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6275 &sav->sah->saidx.src.sa,
6276 FULLMASK, IPSEC_ULPROTO_ANY);
6283 /* set sadb_address for destination */
6284 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6285 &sav->sah->saidx.dst.sa,
6286 FULLMASK, IPSEC_ULPROTO_ANY);
6293 if ((result->m_flags & M_PKTHDR) == 0) {
6298 if (result->m_len < sizeof(struct sadb_msg)) {
6299 result = m_pullup(result, sizeof(struct sadb_msg));
6300 if (result == NULL) {
6306 result->m_pkthdr.len = 0;
6307 for (m = result; m; m = m->m_next)
6308 result->m_pkthdr.len += m->m_len;
6310 mtod(result, struct sadb_msg *)->sadb_msg_len =
6311 PFKEY_UNIT64(result->m_pkthdr.len);
6314 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6324 * SADB_FLUSH processing
6327 * from the ikmpd, and free all entries in secastree.
6331 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6333 * m will always be freed.
6336 key_flush(so, m, mhp)
6339 const struct sadb_msghdr *mhp;
6341 struct sadb_msg *newmsg;
6342 struct secashead *sah, *nextsah;
6343 struct secasvar *sav, *nextsav;
6348 IPSEC_ASSERT(so != NULL, ("null socket"));
6349 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6350 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6352 /* map satype to proto */
6353 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6354 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6356 return key_senderror(so, m, EINVAL);
6359 /* no SATYPE specified, i.e. flushing all SA. */
6361 for (sah = LIST_FIRST(&sahtree);
6364 nextsah = LIST_NEXT(sah, chain);
6366 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6367 && proto != sah->saidx.proto)
6371 stateidx < _ARRAYLEN(saorder_state_alive);
6373 state = saorder_state_any[stateidx];
6374 for (sav = LIST_FIRST(&sah->savtree[state]);
6378 nextsav = LIST_NEXT(sav, chain);
6380 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
6385 sah->state = SADB_SASTATE_DEAD;
6389 if (m->m_len < sizeof(struct sadb_msg) ||
6390 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
6391 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6392 return key_senderror(so, m, ENOBUFS);
6398 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
6399 newmsg = mtod(m, struct sadb_msg *);
6400 newmsg->sadb_msg_errno = 0;
6401 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
6403 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6407 * SADB_DUMP processing
6408 * dump all entries including status of DEAD in SAD.
6411 * from the ikmpd, and dump all secasvar leaves
6416 * m will always be freed.
6419 key_dump(so, m, mhp)
6422 const struct sadb_msghdr *mhp;
6424 struct secashead *sah;
6425 struct secasvar *sav;
6431 struct sadb_msg *newmsg;
6434 IPSEC_ASSERT(so != NULL, ("null socket"));
6435 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6436 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6437 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6439 /* map satype to proto */
6440 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6441 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6443 return key_senderror(so, m, EINVAL);
6446 /* count sav entries to be sent to the userland. */
6449 LIST_FOREACH(sah, &sahtree, chain) {
6450 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6451 && proto != sah->saidx.proto)
6455 stateidx < _ARRAYLEN(saorder_state_any);
6457 state = saorder_state_any[stateidx];
6458 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6466 return key_senderror(so, m, ENOENT);
6469 /* send this to the userland, one at a time. */
6471 LIST_FOREACH(sah, &sahtree, chain) {
6472 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6473 && proto != sah->saidx.proto)
6476 /* map proto to satype */
6477 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
6479 ipseclog((LOG_DEBUG, "%s: there was invalid proto in "
6480 "SAD.\n", __func__));
6481 return key_senderror(so, m, EINVAL);
6485 stateidx < _ARRAYLEN(saorder_state_any);
6487 state = saorder_state_any[stateidx];
6488 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6489 n = key_setdumpsa(sav, SADB_DUMP, satype,
6490 --cnt, mhp->msg->sadb_msg_pid);
6493 return key_senderror(so, m, ENOBUFS);
6495 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
6506 * SADB_X_PROMISC processing
6508 * m will always be freed.
6511 key_promisc(so, m, mhp)
6514 const struct sadb_msghdr *mhp;
6518 IPSEC_ASSERT(so != NULL, ("null socket"));
6519 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6520 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6521 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6523 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6525 if (olen < sizeof(struct sadb_msg)) {
6527 return key_senderror(so, m, EINVAL);
6532 } else if (olen == sizeof(struct sadb_msg)) {
6533 /* enable/disable promisc mode */
6536 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
6537 return key_senderror(so, m, EINVAL);
6538 mhp->msg->sadb_msg_errno = 0;
6539 switch (mhp->msg->sadb_msg_satype) {
6542 kp->kp_promisc = mhp->msg->sadb_msg_satype;
6545 return key_senderror(so, m, EINVAL);
6548 /* send the original message back to everyone */
6549 mhp->msg->sadb_msg_errno = 0;
6550 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6552 /* send packet as is */
6554 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
6556 /* TODO: if sadb_msg_seq is specified, send to specific pid */
6557 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6561 static int (*key_typesw[]) __P((struct socket *, struct mbuf *,
6562 const struct sadb_msghdr *)) = {
6563 NULL, /* SADB_RESERVED */
6564 key_getspi, /* SADB_GETSPI */
6565 key_update, /* SADB_UPDATE */
6566 key_add, /* SADB_ADD */
6567 key_delete, /* SADB_DELETE */
6568 key_get, /* SADB_GET */
6569 key_acquire2, /* SADB_ACQUIRE */
6570 key_register, /* SADB_REGISTER */
6571 NULL, /* SADB_EXPIRE */
6572 key_flush, /* SADB_FLUSH */
6573 key_dump, /* SADB_DUMP */
6574 key_promisc, /* SADB_X_PROMISC */
6575 NULL, /* SADB_X_PCHANGE */
6576 key_spdadd, /* SADB_X_SPDUPDATE */
6577 key_spdadd, /* SADB_X_SPDADD */
6578 key_spddelete, /* SADB_X_SPDDELETE */
6579 key_spdget, /* SADB_X_SPDGET */
6580 NULL, /* SADB_X_SPDACQUIRE */
6581 key_spddump, /* SADB_X_SPDDUMP */
6582 key_spdflush, /* SADB_X_SPDFLUSH */
6583 key_spdadd, /* SADB_X_SPDSETIDX */
6584 NULL, /* SADB_X_SPDEXPIRE */
6585 key_spddelete2, /* SADB_X_SPDDELETE2 */
6589 * parse sadb_msg buffer to process PFKEYv2,
6590 * and create a data to response if needed.
6591 * I think to be dealed with mbuf directly.
6593 * msgp : pointer to pointer to a received buffer pulluped.
6594 * This is rewrited to response.
6595 * so : pointer to socket.
6597 * length for buffer to send to user process.
6604 struct sadb_msg *msg;
6605 struct sadb_msghdr mh;
6610 IPSEC_ASSERT(so != NULL, ("null socket"));
6611 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6613 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
6614 KEYDEBUG(KEYDEBUG_KEY_DUMP,
6615 ipseclog((LOG_DEBUG, "%s: passed sadb_msg\n", __func__));
6619 if (m->m_len < sizeof(struct sadb_msg)) {
6620 m = m_pullup(m, sizeof(struct sadb_msg));
6624 msg = mtod(m, struct sadb_msg *);
6625 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
6626 target = KEY_SENDUP_ONE;
6628 if ((m->m_flags & M_PKTHDR) == 0 ||
6629 m->m_pkthdr.len != m->m_pkthdr.len) {
6630 ipseclog((LOG_DEBUG, "%s: invalid message length.\n",__func__));
6631 pfkeystat.out_invlen++;
6636 if (msg->sadb_msg_version != PF_KEY_V2) {
6637 ipseclog((LOG_DEBUG, "%s: PF_KEY version %u is mismatched.\n",
6638 __func__, msg->sadb_msg_version));
6639 pfkeystat.out_invver++;
6644 if (msg->sadb_msg_type > SADB_MAX) {
6645 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
6646 __func__, msg->sadb_msg_type));
6647 pfkeystat.out_invmsgtype++;
6652 /* for old-fashioned code - should be nuked */
6653 if (m->m_pkthdr.len > MCLBYTES) {
6660 MGETHDR(n, M_DONTWAIT, MT_DATA);
6661 if (n && m->m_pkthdr.len > MHLEN) {
6662 MCLGET(n, M_DONTWAIT);
6663 if ((n->m_flags & M_EXT) == 0) {
6672 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
6673 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
6679 /* align the mbuf chain so that extensions are in contiguous region. */
6680 error = key_align(m, &mh);
6687 switch (msg->sadb_msg_satype) {
6688 case SADB_SATYPE_UNSPEC:
6689 switch (msg->sadb_msg_type) {
6697 ipseclog((LOG_DEBUG, "%s: must specify satype "
6698 "when msg type=%u.\n", __func__,
6699 msg->sadb_msg_type));
6700 pfkeystat.out_invsatype++;
6705 case SADB_SATYPE_AH:
6706 case SADB_SATYPE_ESP:
6707 case SADB_X_SATYPE_IPCOMP:
6708 case SADB_X_SATYPE_TCPSIGNATURE:
6709 switch (msg->sadb_msg_type) {
6711 case SADB_X_SPDDELETE:
6713 case SADB_X_SPDDUMP:
6714 case SADB_X_SPDFLUSH:
6715 case SADB_X_SPDSETIDX:
6716 case SADB_X_SPDUPDATE:
6717 case SADB_X_SPDDELETE2:
6718 ipseclog((LOG_DEBUG, "%s: illegal satype=%u\n",
6719 __func__, msg->sadb_msg_type));
6720 pfkeystat.out_invsatype++;
6725 case SADB_SATYPE_RSVP:
6726 case SADB_SATYPE_OSPFV2:
6727 case SADB_SATYPE_RIPV2:
6728 case SADB_SATYPE_MIP:
6729 ipseclog((LOG_DEBUG, "%s: type %u isn't supported.\n",
6730 __func__, msg->sadb_msg_satype));
6731 pfkeystat.out_invsatype++;
6734 case 1: /* XXX: What does it do? */
6735 if (msg->sadb_msg_type == SADB_X_PROMISC)
6739 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
6740 __func__, msg->sadb_msg_satype));
6741 pfkeystat.out_invsatype++;
6746 /* check field of upper layer protocol and address family */
6747 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
6748 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
6749 struct sadb_address *src0, *dst0;
6752 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
6753 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
6755 /* check upper layer protocol */
6756 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
6757 ipseclog((LOG_DEBUG, "%s: upper layer protocol "
6758 "mismatched.\n", __func__));
6759 pfkeystat.out_invaddr++;
6765 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
6766 PFKEY_ADDR_SADDR(dst0)->sa_family) {
6767 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
6769 pfkeystat.out_invaddr++;
6773 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6774 PFKEY_ADDR_SADDR(dst0)->sa_len) {
6775 ipseclog((LOG_DEBUG, "%s: address struct size "
6776 "mismatched.\n", __func__));
6777 pfkeystat.out_invaddr++;
6782 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6784 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6785 sizeof(struct sockaddr_in)) {
6786 pfkeystat.out_invaddr++;
6792 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6793 sizeof(struct sockaddr_in6)) {
6794 pfkeystat.out_invaddr++;
6800 ipseclog((LOG_DEBUG, "%s: unsupported address family\n",
6802 pfkeystat.out_invaddr++;
6803 error = EAFNOSUPPORT;
6807 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6809 plen = sizeof(struct in_addr) << 3;
6812 plen = sizeof(struct in6_addr) << 3;
6815 plen = 0; /*fool gcc*/
6819 /* check max prefix length */
6820 if (src0->sadb_address_prefixlen > plen ||
6821 dst0->sadb_address_prefixlen > plen) {
6822 ipseclog((LOG_DEBUG, "%s: illegal prefixlen.\n",
6824 pfkeystat.out_invaddr++;
6830 * prefixlen == 0 is valid because there can be a case when
6831 * all addresses are matched.
6835 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
6836 key_typesw[msg->sadb_msg_type] == NULL) {
6837 pfkeystat.out_invmsgtype++;
6842 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
6845 msg->sadb_msg_errno = error;
6846 return key_sendup_mbuf(so, m, target);
6850 key_senderror(so, m, code)
6855 struct sadb_msg *msg;
6857 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
6858 ("mbuf too small, len %u", m->m_len));
6860 msg = mtod(m, struct sadb_msg *);
6861 msg->sadb_msg_errno = code;
6862 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
6866 * set the pointer to each header into message buffer.
6867 * m will be freed on error.
6868 * XXX larger-than-MCLBYTES extension?
6873 struct sadb_msghdr *mhp;
6876 struct sadb_ext *ext;
6881 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6882 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6883 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
6884 ("mbuf too small, len %u", m->m_len));
6887 bzero(mhp, sizeof(*mhp));
6889 mhp->msg = mtod(m, struct sadb_msg *);
6890 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
6892 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6893 extlen = end; /*just in case extlen is not updated*/
6894 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
6895 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
6897 /* m is already freed */
6900 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
6903 switch (ext->sadb_ext_type) {
6905 case SADB_EXT_ADDRESS_SRC:
6906 case SADB_EXT_ADDRESS_DST:
6907 case SADB_EXT_ADDRESS_PROXY:
6908 case SADB_EXT_LIFETIME_CURRENT:
6909 case SADB_EXT_LIFETIME_HARD:
6910 case SADB_EXT_LIFETIME_SOFT:
6911 case SADB_EXT_KEY_AUTH:
6912 case SADB_EXT_KEY_ENCRYPT:
6913 case SADB_EXT_IDENTITY_SRC:
6914 case SADB_EXT_IDENTITY_DST:
6915 case SADB_EXT_SENSITIVITY:
6916 case SADB_EXT_PROPOSAL:
6917 case SADB_EXT_SUPPORTED_AUTH:
6918 case SADB_EXT_SUPPORTED_ENCRYPT:
6919 case SADB_EXT_SPIRANGE:
6920 case SADB_X_EXT_POLICY:
6921 case SADB_X_EXT_SA2:
6922 /* duplicate check */
6924 * XXX Are there duplication payloads of either
6925 * KEY_AUTH or KEY_ENCRYPT ?
6927 if (mhp->ext[ext->sadb_ext_type] != NULL) {
6928 ipseclog((LOG_DEBUG, "%s: duplicate ext_type "
6929 "%u\n", __func__, ext->sadb_ext_type));
6931 pfkeystat.out_dupext++;
6936 ipseclog((LOG_DEBUG, "%s: invalid ext_type %u\n",
6937 __func__, ext->sadb_ext_type));
6939 pfkeystat.out_invexttype++;
6943 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
6945 if (key_validate_ext(ext, extlen)) {
6947 pfkeystat.out_invlen++;
6951 n = m_pulldown(m, off, extlen, &toff);
6953 /* m is already freed */
6956 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
6958 mhp->ext[ext->sadb_ext_type] = ext;
6959 mhp->extoff[ext->sadb_ext_type] = off;
6960 mhp->extlen[ext->sadb_ext_type] = extlen;
6965 pfkeystat.out_invlen++;
6973 key_validate_ext(ext, len)
6974 const struct sadb_ext *ext;
6977 const struct sockaddr *sa;
6978 enum { NONE, ADDR } checktype = NONE;
6980 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
6982 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
6985 /* if it does not match minimum/maximum length, bail */
6986 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
6987 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
6989 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
6991 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
6994 /* more checks based on sadb_ext_type XXX need more */
6995 switch (ext->sadb_ext_type) {
6996 case SADB_EXT_ADDRESS_SRC:
6997 case SADB_EXT_ADDRESS_DST:
6998 case SADB_EXT_ADDRESS_PROXY:
6999 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7002 case SADB_EXT_IDENTITY_SRC:
7003 case SADB_EXT_IDENTITY_DST:
7004 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7005 SADB_X_IDENTTYPE_ADDR) {
7006 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7016 switch (checktype) {
7020 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
7021 if (len < baselen + sal)
7023 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7037 REGTREE_LOCK_INIT();
7038 SAHTREE_LOCK_INIT();
7042 for (i = 0; i < IPSEC_DIR_MAX; i++)
7043 LIST_INIT(&sptree[i]);
7045 LIST_INIT(&sahtree);
7047 for (i = 0; i <= SADB_SATYPE_MAX; i++)
7048 LIST_INIT(®tree[i]);
7050 LIST_INIT(&acqtree);
7051 LIST_INIT(&spacqtree);
7053 /* system default */
7054 ip4_def_policy.policy = IPSEC_POLICY_NONE;
7055 ip4_def_policy.refcnt++; /*never reclaim this*/
7057 #ifndef IPSEC_DEBUG2
7058 timeout((void *)key_timehandler, (void *)0, hz);
7059 #endif /*IPSEC_DEBUG2*/
7061 /* initialize key statistics */
7062 keystat.getspi_count = 1;
7064 printf("Fast IPsec: Initialized Security Association Processing.\n");
7070 * XXX: maybe This function is called after INBOUND IPsec processing.
7072 * Special check for tunnel-mode packets.
7073 * We must make some checks for consistency between inner and outer IP header.
7075 * xxx more checks to be provided
7078 key_checktunnelsanity(sav, family, src, dst)
7079 struct secasvar *sav;
7084 IPSEC_ASSERT(sav->sah != NULL, ("null SA header"));
7086 /* XXX: check inner IP header */
7091 /* record data transfer on SA, and update timestamps */
7093 key_sa_recordxfer(sav, m)
7094 struct secasvar *sav;
7097 IPSEC_ASSERT(sav != NULL, ("Null secasvar"));
7098 IPSEC_ASSERT(m != NULL, ("Null mbuf"));
7103 * XXX Currently, there is a difference of bytes size
7104 * between inbound and outbound processing.
7106 sav->lft_c->sadb_lifetime_bytes += m->m_pkthdr.len;
7107 /* to check bytes lifetime is done in key_timehandler(). */
7110 * We use the number of packets as the unit of
7111 * sadb_lifetime_allocations. We increment the variable
7112 * whenever {esp,ah}_{in,out}put is called.
7114 sav->lft_c->sadb_lifetime_allocations++;
7115 /* XXX check for expires? */
7118 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7119 * in seconds. HARD and SOFT lifetime are measured by the time
7120 * difference (again in seconds) from sadb_lifetime_usetime.
7124 * -----+-----+--------+---> t
7125 * <--------------> HARD
7128 sav->lft_c->sadb_lifetime_usetime = time_second;
7129 /* XXX check for expires? */
7136 key_sa_routechange(dst)
7137 struct sockaddr *dst;
7139 struct secashead *sah;
7143 LIST_FOREACH(sah, &sahtree, chain) {
7144 ro = &sah->sa_route;
7145 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7146 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7148 ro->ro_rt = (struct rtentry *)NULL;
7155 key_sa_chgstate(sav, state)
7156 struct secasvar *sav;
7159 IPSEC_ASSERT(sav != NULL, ("NULL sav"));
7160 SAHTREE_LOCK_ASSERT();
7162 if (sav->state != state) {
7163 if (__LIST_CHAINED(sav))
7164 LIST_REMOVE(sav, chain);
7166 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7172 struct secasvar *sav;
7175 IPSEC_ASSERT(sav->iv != NULL, ("null IV"));
7176 key_randomfill(sav->iv, sav->ivlen);
7180 static struct mbuf *
7184 struct mbuf *m = NULL, *n;
7189 MGET(n, M_DONTWAIT, MT_DATA);
7190 if (n && len > MLEN)
7191 MCLGET(n, M_DONTWAIT);
7199 n->m_len = M_TRAILINGSPACE(n);
7200 /* use the bottom of mbuf, hoping we can prepend afterwards */
7201 if (n->m_len > len) {
7202 t = (n->m_len - len) & ~(sizeof(long) - 1);