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")); \
517 * Return 0 when there are known to be no SP's for the specified
518 * direction. Otherwise return 1. This is used by IPsec code
519 * to optimize performance.
522 key_havesp(u_int dir)
524 return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ?
525 LIST_FIRST(&sptree[dir]) != NULL : 1);
528 /* %%% IPsec policy management */
530 * allocating a SP for OUTBOUND or INBOUND packet.
531 * Must call key_freesp() later.
532 * OUT: NULL: not found
533 * others: found and return the pointer.
536 key_allocsp(struct secpolicyindex *spidx, u_int dir, const char* where, int tag)
538 struct secpolicy *sp;
540 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
541 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
542 ("invalid direction %u", dir));
544 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
545 printf("DP %s from %s:%u\n", __func__, where, tag));
548 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
549 printf("*** objects\n");
550 kdebug_secpolicyindex(spidx));
553 LIST_FOREACH(sp, &sptree[dir], chain) {
554 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
555 printf("*** in SPD\n");
556 kdebug_secpolicyindex(&sp->spidx));
558 if (sp->state == IPSEC_SPSTATE_DEAD)
560 if (key_cmpspidx_withmask(&sp->spidx, spidx))
567 KEY_CHKSPDIR(sp->spidx.dir, dir, __func__);
569 /* found a SPD entry */
570 sp->lastused = time_second;
575 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
576 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
577 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
582 * allocating a SP for OUTBOUND or INBOUND packet.
583 * Must call key_freesp() later.
584 * OUT: NULL: not found
585 * others: found and return the pointer.
588 key_allocsp2(u_int32_t spi,
589 union sockaddr_union *dst,
592 const char* where, int tag)
594 struct secpolicy *sp;
596 IPSEC_ASSERT(dst != NULL, ("null dst"));
597 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
598 ("invalid direction %u", dir));
600 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
601 printf("DP %s from %s:%u\n", __func__, where, tag));
604 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
605 printf("*** objects\n");
606 printf("spi %u proto %u dir %u\n", spi, proto, dir);
607 kdebug_sockaddr(&dst->sa));
610 LIST_FOREACH(sp, &sptree[dir], chain) {
611 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
612 printf("*** in SPD\n");
613 kdebug_secpolicyindex(&sp->spidx));
615 if (sp->state == IPSEC_SPSTATE_DEAD)
617 /* compare simple values, then dst address */
618 if (sp->spidx.ul_proto != proto)
620 /* NB: spi's must exist and match */
621 if (!sp->req || !sp->req->sav || sp->req->sav->spi != spi)
623 if (key_sockaddrcmp(&sp->spidx.dst.sa, &dst->sa, 1) == 0)
630 KEY_CHKSPDIR(sp->spidx.dir, dir, __func__);
632 /* found a SPD entry */
633 sp->lastused = time_second;
638 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
639 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
640 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
645 * return a policy that matches this particular inbound packet.
649 key_gettunnel(const struct sockaddr *osrc,
650 const struct sockaddr *odst,
651 const struct sockaddr *isrc,
652 const struct sockaddr *idst,
653 const char* where, int tag)
655 struct secpolicy *sp;
656 const int dir = IPSEC_DIR_INBOUND;
657 struct ipsecrequest *r1, *r2, *p;
658 struct secpolicyindex spidx;
660 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
661 printf("DP %s from %s:%u\n", __func__, where, tag));
663 if (isrc->sa_family != idst->sa_family) {
664 ipseclog((LOG_ERR, "%s: protocol family mismatched %d != %d\n.",
665 __func__, isrc->sa_family, idst->sa_family));
671 LIST_FOREACH(sp, &sptree[dir], chain) {
672 if (sp->state == IPSEC_SPSTATE_DEAD)
676 for (p = sp->req; p; p = p->next) {
677 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
684 /* here we look at address matches only */
686 if (isrc->sa_len > sizeof(spidx.src) ||
687 idst->sa_len > sizeof(spidx.dst))
689 bcopy(isrc, &spidx.src, isrc->sa_len);
690 bcopy(idst, &spidx.dst, idst->sa_len);
691 if (!key_cmpspidx_withmask(&sp->spidx, &spidx))
694 if (key_sockaddrcmp(&r1->saidx.src.sa, isrc, 0) ||
695 key_sockaddrcmp(&r1->saidx.dst.sa, idst, 0))
699 if (key_sockaddrcmp(&r2->saidx.src.sa, osrc, 0) ||
700 key_sockaddrcmp(&r2->saidx.dst.sa, odst, 0))
709 sp->lastused = time_second;
714 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
715 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
716 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
721 * allocating an SA entry for an *OUTBOUND* packet.
722 * checking each request entries in SP, and acquire an SA if need.
723 * OUT: 0: there are valid requests.
724 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
727 key_checkrequest(struct ipsecrequest *isr, const struct secasindex *saidx)
732 IPSEC_ASSERT(isr != NULL, ("null isr"));
733 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
734 IPSEC_ASSERT(saidx->mode == IPSEC_MODE_TRANSPORT ||
735 saidx->mode == IPSEC_MODE_TUNNEL,
736 ("unexpected policy %u", saidx->mode));
739 * XXX guard against protocol callbacks from the crypto
740 * thread as they reference ipsecrequest.sav which we
741 * temporarily null out below. Need to rethink how we
742 * handle bundled SA's in the callback thread.
744 IPSECREQUEST_LOCK_ASSERT(isr);
746 /* get current level */
747 level = ipsec_get_reqlevel(isr);
750 * We do allocate new SA only if the state of SA in the holder is
751 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
753 if (isr->sav != NULL) {
754 if (isr->sav->sah == NULL)
755 panic("%s: sah is null.\n", __func__);
756 if (isr->sav == (struct secasvar *)LIST_FIRST(
757 &isr->sav->sah->savtree[SADB_SASTATE_DEAD])) {
758 KEY_FREESAV(&isr->sav);
764 * we free any SA stashed in the IPsec request because a different
765 * SA may be involved each time this request is checked, either
766 * because new SAs are being configured, or this request is
767 * associated with an unconnected datagram socket, or this request
768 * is associated with a system default policy.
770 * The operation may have negative impact to performance. We may
771 * want to check cached SA carefully, rather than picking new SA
774 if (isr->sav != NULL) {
775 KEY_FREESAV(&isr->sav);
781 * new SA allocation if no SA found.
782 * key_allocsa_policy should allocate the oldest SA available.
783 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
785 if (isr->sav == NULL)
786 isr->sav = key_allocsa_policy(saidx);
788 /* When there is SA. */
789 if (isr->sav != NULL) {
790 if (isr->sav->state != SADB_SASTATE_MATURE &&
791 isr->sav->state != SADB_SASTATE_DYING)
797 error = key_acquire(saidx, isr->sp);
799 /* XXX What should I do ? */
800 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
805 if (level != IPSEC_LEVEL_REQUIRE) {
806 /* XXX sigh, the interface to this routine is botched */
807 IPSEC_ASSERT(isr->sav == NULL, ("unexpected SA"));
815 * allocating a SA for policy entry from SAD.
816 * NOTE: searching SAD of aliving state.
817 * OUT: NULL: not found.
818 * others: found and return the pointer.
820 static struct secasvar *
821 key_allocsa_policy(const struct secasindex *saidx)
823 #define N(a) _ARRAYLEN(a)
824 struct secashead *sah;
825 struct secasvar *sav;
826 u_int stateidx, arraysize;
827 const u_int *state_valid;
830 LIST_FOREACH(sah, &sahtree, chain) {
831 if (sah->state == SADB_SASTATE_DEAD)
833 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID)) {
834 if (key_preferred_oldsa) {
835 state_valid = saorder_state_valid_prefer_old;
836 arraysize = N(saorder_state_valid_prefer_old);
838 state_valid = saorder_state_valid_prefer_new;
839 arraysize = N(saorder_state_valid_prefer_new);
850 /* search valid state */
851 for (stateidx = 0; stateidx < arraysize; stateidx++) {
852 sav = key_do_allocsa_policy(sah, state_valid[stateidx]);
862 * searching SAD with direction, protocol, mode and state.
863 * called by key_allocsa_policy().
866 * others : found, pointer to a SA.
868 static struct secasvar *
869 key_do_allocsa_policy(struct secashead *sah, u_int state)
871 struct secasvar *sav, *nextsav, *candidate, *d;
877 for (sav = LIST_FIRST(&sah->savtree[state]);
881 nextsav = LIST_NEXT(sav, chain);
884 KEY_CHKSASTATE(sav->state, state, __func__);
887 if (candidate == NULL) {
892 /* Which SA is the better ? */
894 IPSEC_ASSERT(candidate->lft_c != NULL,
895 ("null candidate lifetime"));
896 IPSEC_ASSERT(sav->lft_c != NULL, ("null sav lifetime"));
898 /* What the best method is to compare ? */
899 if (key_preferred_oldsa) {
900 if (candidate->lft_c->sadb_lifetime_addtime >
901 sav->lft_c->sadb_lifetime_addtime) {
908 /* preferred new sa rather than old sa */
909 if (candidate->lft_c->sadb_lifetime_addtime <
910 sav->lft_c->sadb_lifetime_addtime) {
917 * prepared to delete the SA when there is more
918 * suitable candidate and the lifetime of the SA is not
921 if (d->lft_c->sadb_lifetime_addtime != 0) {
922 struct mbuf *m, *result;
925 key_sa_chgstate(d, SADB_SASTATE_DEAD);
927 IPSEC_ASSERT(d->refcnt > 0, ("bogus ref count"));
929 satype = key_proto2satype(d->sah->saidx.proto);
933 m = key_setsadbmsg(SADB_DELETE, 0,
934 satype, 0, 0, d->refcnt - 1);
939 /* set sadb_address for saidx's. */
940 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
941 &d->sah->saidx.src.sa,
942 d->sah->saidx.src.sa.sa_len << 3,
948 /* set sadb_address for saidx's. */
949 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
950 &d->sah->saidx.dst.sa,
951 d->sah->saidx.dst.sa.sa_len << 3,
957 /* create SA extension */
958 m = key_setsadbsa(d);
963 if (result->m_len < sizeof(struct sadb_msg)) {
964 result = m_pullup(result,
965 sizeof(struct sadb_msg));
970 result->m_pkthdr.len = 0;
971 for (m = result; m; m = m->m_next)
972 result->m_pkthdr.len += m->m_len;
973 mtod(result, struct sadb_msg *)->sadb_msg_len =
974 PFKEY_UNIT64(result->m_pkthdr.len);
976 if (key_sendup_mbuf(NULL, result,
977 KEY_SENDUP_REGISTERED))
984 SA_ADDREF(candidate);
985 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
986 printf("DP %s cause refcnt++:%d SA:%p\n",
987 __func__, candidate->refcnt, candidate));
995 * allocating a usable SA entry for a *INBOUND* packet.
996 * Must call key_freesav() later.
997 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
998 * NULL: not found, or error occured.
1000 * In the comparison, no source address is used--for RFC2401 conformance.
1001 * To quote, from section 4.1:
1002 * A security association is uniquely identified by a triple consisting
1003 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1004 * security protocol (AH or ESP) identifier.
1005 * Note that, however, we do need to keep source address in IPsec SA.
1006 * IKE specification and PF_KEY specification do assume that we
1007 * keep source address in IPsec SA. We see a tricky situation here.
1011 union sockaddr_union *dst,
1014 const char* where, int tag)
1016 struct secashead *sah;
1017 struct secasvar *sav;
1018 u_int stateidx, arraysize, state;
1019 const u_int *saorder_state_valid;
1021 IPSEC_ASSERT(dst != NULL, ("null dst address"));
1023 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1024 printf("DP %s from %s:%u\n", __func__, where, tag));
1028 * XXX: to be checked internal IP header somewhere. Also when
1029 * IPsec tunnel packet is received. But ESP tunnel mode is
1030 * encrypted so we can't check internal IP header.
1033 if (key_preferred_oldsa) {
1034 saorder_state_valid = saorder_state_valid_prefer_old;
1035 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
1037 saorder_state_valid = saorder_state_valid_prefer_new;
1038 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
1040 LIST_FOREACH(sah, &sahtree, chain) {
1041 /* search valid state */
1042 for (stateidx = 0; stateidx < arraysize; stateidx++) {
1043 state = saorder_state_valid[stateidx];
1044 LIST_FOREACH(sav, &sah->savtree[state], chain) {
1046 KEY_CHKSASTATE(sav->state, state, __func__);
1047 /* do not return entries w/ unusable state */
1048 if (sav->state != SADB_SASTATE_MATURE &&
1049 sav->state != SADB_SASTATE_DYING)
1051 if (proto != sav->sah->saidx.proto)
1053 if (spi != sav->spi)
1055 #if 0 /* don't check src */
1056 /* check src address */
1057 if (key_sockaddrcmp(&src->sa, &sav->sah->saidx.src.sa, 0) != 0)
1060 /* check dst address */
1061 if (key_sockaddrcmp(&dst->sa, &sav->sah->saidx.dst.sa, 0) != 0)
1072 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1073 printf("DP %s return SA:%p; refcnt %u\n", __func__,
1074 sav, sav ? sav->refcnt : 0));
1079 * Must be called after calling key_allocsp().
1080 * For both the packet without socket and key_freeso().
1083 _key_freesp(struct secpolicy **spp, const char* where, int tag)
1085 struct secpolicy *sp = *spp;
1087 IPSEC_ASSERT(sp != NULL, ("null sp"));
1092 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1093 printf("DP %s SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1094 __func__, sp, sp->id, where, tag, sp->refcnt));
1096 if (sp->refcnt == 0) {
1104 * Must be called after calling key_allocsp().
1105 * For the packet with socket.
1108 key_freeso(struct socket *so)
1110 IPSEC_ASSERT(so != NULL, ("null so"));
1112 switch (so->so_proto->pr_domain->dom_family) {
1116 struct inpcb *pcb = sotoinpcb(so);
1118 /* Does it have a PCB ? */
1121 key_freesp_so(&pcb->inp_sp->sp_in);
1122 key_freesp_so(&pcb->inp_sp->sp_out);
1129 #ifdef HAVE_NRL_INPCB
1130 struct inpcb *pcb = sotoinpcb(so);
1132 /* Does it have a PCB ? */
1135 key_freesp_so(&pcb->inp_sp->sp_in);
1136 key_freesp_so(&pcb->inp_sp->sp_out);
1138 struct in6pcb *pcb = sotoin6pcb(so);
1140 /* Does it have a PCB ? */
1143 key_freesp_so(&pcb->in6p_sp->sp_in);
1144 key_freesp_so(&pcb->in6p_sp->sp_out);
1150 ipseclog((LOG_DEBUG, "%s: unknown address family=%d.\n",
1151 __func__, so->so_proto->pr_domain->dom_family));
1157 key_freesp_so(struct secpolicy **sp)
1159 IPSEC_ASSERT(sp != NULL && *sp != NULL, ("null sp"));
1161 if ((*sp)->policy == IPSEC_POLICY_ENTRUST ||
1162 (*sp)->policy == IPSEC_POLICY_BYPASS)
1165 IPSEC_ASSERT((*sp)->policy == IPSEC_POLICY_IPSEC,
1166 ("invalid policy %u", (*sp)->policy));
1171 * Must be called after calling key_allocsa().
1172 * This function is called by key_freesp() to free some SA allocated
1176 key_freesav(struct secasvar **psav, const char* where, int tag)
1178 struct secasvar *sav = *psav;
1180 IPSEC_ASSERT(sav != NULL, ("null sav"));
1182 /* XXX unguarded? */
1185 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1186 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1187 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1189 if (sav->refcnt == 0) {
1195 /* %%% SPD management */
1197 * free security policy entry.
1200 key_delsp(struct secpolicy *sp)
1202 struct ipsecrequest *isr, *nextisr;
1204 IPSEC_ASSERT(sp != NULL, ("null sp"));
1205 SPTREE_LOCK_ASSERT();
1207 sp->state = IPSEC_SPSTATE_DEAD;
1209 IPSEC_ASSERT(sp->refcnt == 0,
1210 ("SP with references deleted (refcnt %u)", sp->refcnt));
1212 /* remove from SP index */
1213 if (__LIST_CHAINED(sp))
1214 LIST_REMOVE(sp, chain);
1216 for (isr = sp->req; isr != NULL; isr = nextisr) {
1217 if (isr->sav != NULL) {
1218 KEY_FREESAV(&isr->sav);
1222 nextisr = isr->next;
1230 * OUT: NULL : not found
1231 * others : found, pointer to a SP.
1233 static struct secpolicy *
1234 key_getsp(struct secpolicyindex *spidx)
1236 struct secpolicy *sp;
1238 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
1241 LIST_FOREACH(sp, &sptree[spidx->dir], chain) {
1242 if (sp->state == IPSEC_SPSTATE_DEAD)
1244 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1256 * OUT: NULL : not found
1257 * others : found, pointer to a SP.
1259 static struct secpolicy *
1260 key_getspbyid(u_int32_t id)
1262 struct secpolicy *sp;
1265 LIST_FOREACH(sp, &sptree[IPSEC_DIR_INBOUND], chain) {
1266 if (sp->state == IPSEC_SPSTATE_DEAD)
1274 LIST_FOREACH(sp, &sptree[IPSEC_DIR_OUTBOUND], chain) {
1275 if (sp->state == IPSEC_SPSTATE_DEAD)
1289 key_newsp(const char* where, int tag)
1291 struct secpolicy *newsp = NULL;
1293 newsp = (struct secpolicy *)
1294 malloc(sizeof(struct secpolicy), M_IPSEC_SP, M_NOWAIT|M_ZERO);
1296 SECPOLICY_LOCK_INIT(newsp);
1301 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1302 printf("DP %s from %s:%u return SP:%p\n", __func__,
1303 where, tag, newsp));
1308 _key_delsp(struct secpolicy *sp)
1310 SECPOLICY_LOCK_DESTROY(sp);
1311 free(sp, M_IPSEC_SP);
1315 * create secpolicy structure from sadb_x_policy structure.
1316 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1317 * so must be set properly later.
1320 key_msg2sp(xpl0, len, error)
1321 struct sadb_x_policy *xpl0;
1325 struct secpolicy *newsp;
1327 IPSEC_ASSERT(xpl0 != NULL, ("null xpl0"));
1328 IPSEC_ASSERT(len >= sizeof(*xpl0), ("policy too short: %zu", len));
1330 if (len != PFKEY_EXTLEN(xpl0)) {
1331 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n", __func__));
1336 if ((newsp = KEY_NEWSP()) == NULL) {
1341 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1342 newsp->policy = xpl0->sadb_x_policy_type;
1345 switch (xpl0->sadb_x_policy_type) {
1346 case IPSEC_POLICY_DISCARD:
1347 case IPSEC_POLICY_NONE:
1348 case IPSEC_POLICY_ENTRUST:
1349 case IPSEC_POLICY_BYPASS:
1353 case IPSEC_POLICY_IPSEC:
1356 struct sadb_x_ipsecrequest *xisr;
1357 struct ipsecrequest **p_isr = &newsp->req;
1359 /* validity check */
1360 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1361 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n",
1368 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1369 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1373 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1374 ipseclog((LOG_DEBUG, "%s: invalid ipsecrequest "
1375 "length.\n", __func__));
1381 /* allocate request buffer */
1382 /* NB: data structure is zero'd */
1383 *p_isr = ipsec_newisr();
1384 if ((*p_isr) == NULL) {
1385 ipseclog((LOG_DEBUG,
1386 "%s: No more memory.\n", __func__));
1393 switch (xisr->sadb_x_ipsecrequest_proto) {
1396 case IPPROTO_IPCOMP:
1399 ipseclog((LOG_DEBUG,
1400 "%s: invalid proto type=%u\n", __func__,
1401 xisr->sadb_x_ipsecrequest_proto));
1403 *error = EPROTONOSUPPORT;
1406 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1408 switch (xisr->sadb_x_ipsecrequest_mode) {
1409 case IPSEC_MODE_TRANSPORT:
1410 case IPSEC_MODE_TUNNEL:
1412 case IPSEC_MODE_ANY:
1414 ipseclog((LOG_DEBUG,
1415 "%s: invalid mode=%u\n", __func__,
1416 xisr->sadb_x_ipsecrequest_mode));
1421 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1423 switch (xisr->sadb_x_ipsecrequest_level) {
1424 case IPSEC_LEVEL_DEFAULT:
1425 case IPSEC_LEVEL_USE:
1426 case IPSEC_LEVEL_REQUIRE:
1428 case IPSEC_LEVEL_UNIQUE:
1429 /* validity check */
1431 * If range violation of reqid, kernel will
1432 * update it, don't refuse it.
1434 if (xisr->sadb_x_ipsecrequest_reqid
1435 > IPSEC_MANUAL_REQID_MAX) {
1436 ipseclog((LOG_DEBUG,
1437 "%s: reqid=%d range "
1438 "violation, updated by kernel.\n",
1440 xisr->sadb_x_ipsecrequest_reqid));
1441 xisr->sadb_x_ipsecrequest_reqid = 0;
1444 /* allocate new reqid id if reqid is zero. */
1445 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1447 if ((reqid = key_newreqid()) == 0) {
1452 (*p_isr)->saidx.reqid = reqid;
1453 xisr->sadb_x_ipsecrequest_reqid = reqid;
1455 /* set it for manual keying. */
1456 (*p_isr)->saidx.reqid =
1457 xisr->sadb_x_ipsecrequest_reqid;
1462 ipseclog((LOG_DEBUG, "%s: invalid level=%u\n",
1464 xisr->sadb_x_ipsecrequest_level));
1469 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1471 /* set IP addresses if there */
1472 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1473 struct sockaddr *paddr;
1475 paddr = (struct sockaddr *)(xisr + 1);
1477 /* validity check */
1479 > sizeof((*p_isr)->saidx.src)) {
1480 ipseclog((LOG_DEBUG, "%s: invalid "
1481 "request address length.\n",
1487 bcopy(paddr, &(*p_isr)->saidx.src,
1490 paddr = (struct sockaddr *)((caddr_t)paddr
1493 /* validity check */
1495 > sizeof((*p_isr)->saidx.dst)) {
1496 ipseclog((LOG_DEBUG, "%s: invalid "
1497 "request address length.\n",
1503 bcopy(paddr, &(*p_isr)->saidx.dst,
1507 (*p_isr)->sp = newsp;
1509 /* initialization for the next. */
1510 p_isr = &(*p_isr)->next;
1511 tlen -= xisr->sadb_x_ipsecrequest_len;
1513 /* validity check */
1515 ipseclog((LOG_DEBUG, "%s: becoming tlen < 0.\n",
1522 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1523 + xisr->sadb_x_ipsecrequest_len);
1528 ipseclog((LOG_DEBUG, "%s: invalid policy type.\n", __func__));
1541 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1543 auto_reqid = (auto_reqid == ~0
1544 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1546 /* XXX should be unique check */
1552 * copy secpolicy struct to sadb_x_policy structure indicated.
1556 struct secpolicy *sp;
1558 struct sadb_x_policy *xpl;
1563 IPSEC_ASSERT(sp != NULL, ("null policy"));
1565 tlen = key_getspreqmsglen(sp);
1567 m = key_alloc_mbuf(tlen);
1568 if (!m || m->m_next) { /*XXX*/
1576 xpl = mtod(m, struct sadb_x_policy *);
1579 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1580 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1581 xpl->sadb_x_policy_type = sp->policy;
1582 xpl->sadb_x_policy_dir = sp->spidx.dir;
1583 xpl->sadb_x_policy_id = sp->id;
1584 p = (caddr_t)xpl + sizeof(*xpl);
1586 /* if is the policy for ipsec ? */
1587 if (sp->policy == IPSEC_POLICY_IPSEC) {
1588 struct sadb_x_ipsecrequest *xisr;
1589 struct ipsecrequest *isr;
1591 for (isr = sp->req; isr != NULL; isr = isr->next) {
1593 xisr = (struct sadb_x_ipsecrequest *)p;
1595 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1596 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1597 xisr->sadb_x_ipsecrequest_level = isr->level;
1598 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1601 bcopy(&isr->saidx.src, p, isr->saidx.src.sa.sa_len);
1602 p += isr->saidx.src.sa.sa_len;
1603 bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len);
1604 p += isr->saidx.src.sa.sa_len;
1606 xisr->sadb_x_ipsecrequest_len =
1607 PFKEY_ALIGN8(sizeof(*xisr)
1608 + isr->saidx.src.sa.sa_len
1609 + isr->saidx.dst.sa.sa_len);
1616 /* m will not be freed nor modified */
1617 static struct mbuf *
1619 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1620 int ndeep, int nitem, ...)
1622 key_gather_mbuf(m, mhp, ndeep, nitem, va_alist)
1624 const struct sadb_msghdr *mhp;
1633 struct mbuf *result = NULL, *n;
1636 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1637 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1639 va_start(ap, nitem);
1640 for (i = 0; i < nitem; i++) {
1641 idx = va_arg(ap, int);
1642 if (idx < 0 || idx > SADB_EXT_MAX)
1644 /* don't attempt to pull empty extension */
1645 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1647 if (idx != SADB_EXT_RESERVED &&
1648 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1651 if (idx == SADB_EXT_RESERVED) {
1652 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1654 IPSEC_ASSERT(len <= MHLEN, ("header too big %u", len));
1656 MGETHDR(n, M_DONTWAIT, MT_DATA);
1661 m_copydata(m, 0, sizeof(struct sadb_msg),
1663 } else if (i < ndeep) {
1664 len = mhp->extlen[idx];
1665 n = key_alloc_mbuf(len);
1666 if (!n || n->m_next) { /*XXX*/
1671 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1674 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1687 if ((result->m_flags & M_PKTHDR) != 0) {
1688 result->m_pkthdr.len = 0;
1689 for (n = result; n; n = n->m_next)
1690 result->m_pkthdr.len += n->m_len;
1701 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1702 * add an entry to SP database, when received
1703 * <base, address(SD), (lifetime(H),) policy>
1705 * Adding to SP database,
1707 * <base, address(SD), (lifetime(H),) policy>
1708 * to the socket which was send.
1710 * SPDADD set a unique policy entry.
1711 * SPDSETIDX like SPDADD without a part of policy requests.
1712 * SPDUPDATE replace a unique policy entry.
1714 * m will always be freed.
1717 key_spdadd(so, m, mhp)
1720 const struct sadb_msghdr *mhp;
1722 struct sadb_address *src0, *dst0;
1723 struct sadb_x_policy *xpl0, *xpl;
1724 struct sadb_lifetime *lft = NULL;
1725 struct secpolicyindex spidx;
1726 struct secpolicy *newsp;
1729 IPSEC_ASSERT(so != NULL, ("null socket"));
1730 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1731 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1732 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
1734 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1735 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1736 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1737 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1738 return key_senderror(so, m, EINVAL);
1740 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1741 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1742 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1743 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1745 return key_senderror(so, m, EINVAL);
1747 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1748 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1749 < sizeof(struct sadb_lifetime)) {
1750 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1752 return key_senderror(so, m, EINVAL);
1754 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1757 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1758 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1759 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1762 /* XXX boundary check against sa_len */
1763 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1766 src0->sadb_address_prefixlen,
1767 dst0->sadb_address_prefixlen,
1768 src0->sadb_address_proto,
1771 /* checking the direciton. */
1772 switch (xpl0->sadb_x_policy_dir) {
1773 case IPSEC_DIR_INBOUND:
1774 case IPSEC_DIR_OUTBOUND:
1777 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
1778 mhp->msg->sadb_msg_errno = EINVAL;
1783 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1784 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1785 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1786 ipseclog((LOG_DEBUG, "%s: Invalid policy type.\n", __func__));
1787 return key_senderror(so, m, EINVAL);
1790 /* policy requests are mandatory when action is ipsec. */
1791 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1792 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1793 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1794 ipseclog((LOG_DEBUG, "%s: some policy requests part required\n",
1796 return key_senderror(so, m, EINVAL);
1800 * checking there is SP already or not.
1801 * SPDUPDATE doesn't depend on whether there is a SP or not.
1802 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1805 newsp = key_getsp(&spidx);
1806 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1808 newsp->state = IPSEC_SPSTATE_DEAD;
1812 if (newsp != NULL) {
1814 ipseclog((LOG_DEBUG, "%s: a SP entry exists already.\n",
1816 return key_senderror(so, m, EEXIST);
1820 /* allocation new SP entry */
1821 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1822 return key_senderror(so, m, error);
1825 if ((newsp->id = key_getnewspid()) == 0) {
1827 return key_senderror(so, m, ENOBUFS);
1830 /* XXX boundary check against sa_len */
1831 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1834 src0->sadb_address_prefixlen,
1835 dst0->sadb_address_prefixlen,
1836 src0->sadb_address_proto,
1839 /* sanity check on addr pair */
1840 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1841 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1843 return key_senderror(so, m, EINVAL);
1845 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1846 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1848 return key_senderror(so, m, EINVAL);
1851 if (newsp->req && newsp->req->saidx.src.sa.sa_family) {
1852 struct sockaddr *sa;
1853 sa = (struct sockaddr *)(src0 + 1);
1854 if (sa->sa_family != newsp->req->saidx.src.sa.sa_family) {
1856 return key_senderror(so, m, EINVAL);
1859 if (newsp->req && newsp->req->saidx.dst.sa.sa_family) {
1860 struct sockaddr *sa;
1861 sa = (struct sockaddr *)(dst0 + 1);
1862 if (sa->sa_family != newsp->req->saidx.dst.sa.sa_family) {
1864 return key_senderror(so, m, EINVAL);
1869 newsp->created = time_second;
1870 newsp->lastused = newsp->created;
1871 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1872 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1874 newsp->refcnt = 1; /* do not reclaim until I say I do */
1875 newsp->state = IPSEC_SPSTATE_ALIVE;
1876 LIST_INSERT_TAIL(&sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1878 /* delete the entry in spacqtree */
1879 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1880 struct secspacq *spacq = key_getspacq(&spidx);
1881 if (spacq != NULL) {
1882 /* reset counter in order to deletion by timehandler. */
1883 spacq->created = time_second;
1890 struct mbuf *n, *mpolicy;
1891 struct sadb_msg *newmsg;
1894 /* create new sadb_msg to reply. */
1896 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1897 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1898 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1900 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1902 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1905 return key_senderror(so, m, ENOBUFS);
1907 if (n->m_len < sizeof(*newmsg)) {
1908 n = m_pullup(n, sizeof(*newmsg));
1910 return key_senderror(so, m, ENOBUFS);
1912 newmsg = mtod(n, struct sadb_msg *);
1913 newmsg->sadb_msg_errno = 0;
1914 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1917 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
1918 sizeof(*xpl), &off);
1919 if (mpolicy == NULL) {
1920 /* n is already freed */
1921 return key_senderror(so, m, ENOBUFS);
1923 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
1924 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
1926 return key_senderror(so, m, EINVAL);
1928 xpl->sadb_x_policy_id = newsp->id;
1931 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
1936 * get new policy id.
1944 u_int32_t newid = 0;
1945 int count = key_spi_trycnt; /* XXX */
1946 struct secpolicy *sp;
1948 /* when requesting to allocate spi ranged */
1950 newid = (policy_id = (policy_id == ~0 ? 1 : policy_id + 1));
1952 if ((sp = key_getspbyid(newid)) == NULL)
1958 if (count == 0 || newid == 0) {
1959 ipseclog((LOG_DEBUG, "%s: to allocate policy id is failed.\n",
1968 * SADB_SPDDELETE processing
1970 * <base, address(SD), policy(*)>
1971 * from the user(?), and set SADB_SASTATE_DEAD,
1973 * <base, address(SD), policy(*)>
1975 * policy(*) including direction of policy.
1977 * m will always be freed.
1980 key_spddelete(so, m, mhp)
1983 const struct sadb_msghdr *mhp;
1985 struct sadb_address *src0, *dst0;
1986 struct sadb_x_policy *xpl0;
1987 struct secpolicyindex spidx;
1988 struct secpolicy *sp;
1990 IPSEC_ASSERT(so != NULL, ("null so"));
1991 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1992 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1993 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
1995 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1996 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1997 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1998 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2000 return key_senderror(so, m, EINVAL);
2002 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
2003 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
2004 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2005 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2007 return key_senderror(so, m, EINVAL);
2010 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2011 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2012 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2015 /* XXX boundary check against sa_len */
2016 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2019 src0->sadb_address_prefixlen,
2020 dst0->sadb_address_prefixlen,
2021 src0->sadb_address_proto,
2024 /* checking the direciton. */
2025 switch (xpl0->sadb_x_policy_dir) {
2026 case IPSEC_DIR_INBOUND:
2027 case IPSEC_DIR_OUTBOUND:
2030 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
2031 return key_senderror(so, m, EINVAL);
2034 /* Is there SP in SPD ? */
2035 if ((sp = key_getsp(&spidx)) == NULL) {
2036 ipseclog((LOG_DEBUG, "%s: no SP found.\n", __func__));
2037 return key_senderror(so, m, EINVAL);
2040 /* save policy id to buffer to be returned. */
2041 xpl0->sadb_x_policy_id = sp->id;
2043 sp->state = IPSEC_SPSTATE_DEAD;
2044 SECPOLICY_LOCK_DESTROY(sp);
2049 struct sadb_msg *newmsg;
2051 /* create new sadb_msg to reply. */
2052 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2053 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2055 return key_senderror(so, m, ENOBUFS);
2057 newmsg = mtod(n, struct sadb_msg *);
2058 newmsg->sadb_msg_errno = 0;
2059 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2062 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2067 * SADB_SPDDELETE2 processing
2070 * from the user(?), and set SADB_SASTATE_DEAD,
2074 * policy(*) including direction of policy.
2076 * m will always be freed.
2079 key_spddelete2(so, m, mhp)
2082 const struct sadb_msghdr *mhp;
2085 struct secpolicy *sp;
2087 IPSEC_ASSERT(so != NULL, ("null socket"));
2088 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2089 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2090 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2092 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2093 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2094 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", __func__));
2095 key_senderror(so, m, EINVAL);
2099 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2101 /* Is there SP in SPD ? */
2102 if ((sp = key_getspbyid(id)) == NULL) {
2103 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2104 key_senderror(so, m, EINVAL);
2107 sp->state = IPSEC_SPSTATE_DEAD;
2108 SECPOLICY_LOCK_DESTROY(sp);
2112 struct mbuf *n, *nn;
2113 struct sadb_msg *newmsg;
2116 /* create new sadb_msg to reply. */
2117 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2120 return key_senderror(so, m, ENOBUFS);
2121 MGETHDR(n, M_DONTWAIT, MT_DATA);
2122 if (n && len > MHLEN) {
2123 MCLGET(n, M_DONTWAIT);
2124 if ((n->m_flags & M_EXT) == 0) {
2130 return key_senderror(so, m, ENOBUFS);
2136 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2137 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2139 IPSEC_ASSERT(off == len, ("length inconsistency (off %u len %u)",
2142 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2143 mhp->extlen[SADB_X_EXT_POLICY], M_DONTWAIT);
2146 return key_senderror(so, m, ENOBUFS);
2149 n->m_pkthdr.len = 0;
2150 for (nn = n; nn; nn = nn->m_next)
2151 n->m_pkthdr.len += nn->m_len;
2153 newmsg = mtod(n, struct sadb_msg *);
2154 newmsg->sadb_msg_errno = 0;
2155 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2158 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2163 * SADB_X_GET processing
2168 * <base, address(SD), policy>
2170 * policy(*) including direction of policy.
2172 * m will always be freed.
2175 key_spdget(so, m, mhp)
2178 const struct sadb_msghdr *mhp;
2181 struct secpolicy *sp;
2184 IPSEC_ASSERT(so != NULL, ("null socket"));
2185 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2186 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2187 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2189 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2190 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2191 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2193 return key_senderror(so, m, EINVAL);
2196 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2198 /* Is there SP in SPD ? */
2199 if ((sp = key_getspbyid(id)) == NULL) {
2200 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2201 return key_senderror(so, m, ENOENT);
2204 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2207 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2209 return key_senderror(so, m, ENOBUFS);
2213 * SADB_X_SPDACQUIRE processing.
2214 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2217 * to KMD, and expect to receive
2218 * <base> with SADB_X_SPDACQUIRE if error occured,
2221 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2222 * policy(*) is without policy requests.
2225 * others: error number
2229 struct secpolicy *sp;
2231 struct mbuf *result = NULL, *m;
2232 struct secspacq *newspacq;
2235 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2236 IPSEC_ASSERT(sp->req == NULL, ("policy exists"));
2237 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
2238 ("policy not IPSEC %u", sp->policy));
2240 /* Get an entry to check whether sent message or not. */
2241 newspacq = key_getspacq(&sp->spidx);
2242 if (newspacq != NULL) {
2243 if (key_blockacq_count < newspacq->count) {
2244 /* reset counter and do send message. */
2245 newspacq->count = 0;
2247 /* increment counter and do nothing. */
2253 /* make new entry for blocking to send SADB_ACQUIRE. */
2254 newspacq = key_newspacq(&sp->spidx);
2255 if (newspacq == NULL)
2259 /* create new sadb_msg to reply. */
2260 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2267 result->m_pkthdr.len = 0;
2268 for (m = result; m; m = m->m_next)
2269 result->m_pkthdr.len += m->m_len;
2271 mtod(result, struct sadb_msg *)->sadb_msg_len =
2272 PFKEY_UNIT64(result->m_pkthdr.len);
2274 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2283 * SADB_SPDFLUSH processing
2286 * from the user, and free all entries in secpctree.
2290 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2292 * m will always be freed.
2295 key_spdflush(so, m, mhp)
2298 const struct sadb_msghdr *mhp;
2300 struct sadb_msg *newmsg;
2301 struct secpolicy *sp;
2304 IPSEC_ASSERT(so != NULL, ("null socket"));
2305 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2306 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2307 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2309 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2310 return key_senderror(so, m, EINVAL);
2312 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2314 LIST_FOREACH(sp, &sptree[dir], chain)
2315 sp->state = IPSEC_SPSTATE_DEAD;
2319 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2320 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2321 return key_senderror(so, m, ENOBUFS);
2327 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2328 newmsg = mtod(m, struct sadb_msg *);
2329 newmsg->sadb_msg_errno = 0;
2330 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2332 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2336 * SADB_SPDDUMP processing
2339 * from the user, and dump all SP leaves
2344 * m will always be freed.
2347 key_spddump(so, m, mhp)
2350 const struct sadb_msghdr *mhp;
2352 struct secpolicy *sp;
2357 IPSEC_ASSERT(so != NULL, ("null socket"));
2358 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2359 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2360 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2362 /* search SPD entry and get buffer size. */
2364 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2365 LIST_FOREACH(sp, &sptree[dir], chain) {
2371 return key_senderror(so, m, ENOENT);
2373 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2374 LIST_FOREACH(sp, &sptree[dir], chain) {
2376 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2377 mhp->msg->sadb_msg_pid);
2380 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2388 static struct mbuf *
2389 key_setdumpsp(sp, type, seq, pid)
2390 struct secpolicy *sp;
2394 struct mbuf *result = NULL, *m;
2396 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2401 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2402 &sp->spidx.src.sa, sp->spidx.prefs,
2403 sp->spidx.ul_proto);
2408 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2409 &sp->spidx.dst.sa, sp->spidx.prefd,
2410 sp->spidx.ul_proto);
2420 if ((result->m_flags & M_PKTHDR) == 0)
2423 if (result->m_len < sizeof(struct sadb_msg)) {
2424 result = m_pullup(result, sizeof(struct sadb_msg));
2429 result->m_pkthdr.len = 0;
2430 for (m = result; m; m = m->m_next)
2431 result->m_pkthdr.len += m->m_len;
2433 mtod(result, struct sadb_msg *)->sadb_msg_len =
2434 PFKEY_UNIT64(result->m_pkthdr.len);
2444 * get PFKEY message length for security policy and request.
2447 key_getspreqmsglen(sp)
2448 struct secpolicy *sp;
2452 tlen = sizeof(struct sadb_x_policy);
2454 /* if is the policy for ipsec ? */
2455 if (sp->policy != IPSEC_POLICY_IPSEC)
2458 /* get length of ipsec requests */
2460 struct ipsecrequest *isr;
2463 for (isr = sp->req; isr != NULL; isr = isr->next) {
2464 len = sizeof(struct sadb_x_ipsecrequest)
2465 + isr->saidx.src.sa.sa_len
2466 + isr->saidx.dst.sa.sa_len;
2468 tlen += PFKEY_ALIGN8(len);
2476 * SADB_SPDEXPIRE processing
2478 * <base, address(SD), lifetime(CH), policy>
2482 * others : error number
2486 struct secpolicy *sp;
2488 struct mbuf *result = NULL, *m;
2491 struct sadb_lifetime *lt;
2493 /* XXX: Why do we lock ? */
2495 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2497 /* set msg header */
2498 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2505 /* create lifetime extension (current and hard) */
2506 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2507 m = key_alloc_mbuf(len);
2508 if (!m || m->m_next) { /*XXX*/
2514 bzero(mtod(m, caddr_t), len);
2515 lt = mtod(m, struct sadb_lifetime *);
2516 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2517 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2518 lt->sadb_lifetime_allocations = 0;
2519 lt->sadb_lifetime_bytes = 0;
2520 lt->sadb_lifetime_addtime = sp->created;
2521 lt->sadb_lifetime_usetime = sp->lastused;
2522 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2523 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2524 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2525 lt->sadb_lifetime_allocations = 0;
2526 lt->sadb_lifetime_bytes = 0;
2527 lt->sadb_lifetime_addtime = sp->lifetime;
2528 lt->sadb_lifetime_usetime = sp->validtime;
2531 /* set sadb_address for source */
2532 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2534 sp->spidx.prefs, sp->spidx.ul_proto);
2541 /* set sadb_address for destination */
2542 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2544 sp->spidx.prefd, sp->spidx.ul_proto);
2559 if ((result->m_flags & M_PKTHDR) == 0) {
2564 if (result->m_len < sizeof(struct sadb_msg)) {
2565 result = m_pullup(result, sizeof(struct sadb_msg));
2566 if (result == NULL) {
2572 result->m_pkthdr.len = 0;
2573 for (m = result; m; m = m->m_next)
2574 result->m_pkthdr.len += m->m_len;
2576 mtod(result, struct sadb_msg *)->sadb_msg_len =
2577 PFKEY_UNIT64(result->m_pkthdr.len);
2579 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2587 /* %%% SAD management */
2589 * allocating a memory for new SA head, and copy from the values of mhp.
2590 * OUT: NULL : failure due to the lack of memory.
2591 * others : pointer to new SA head.
2593 static struct secashead *
2595 struct secasindex *saidx;
2597 struct secashead *newsah;
2599 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
2601 newsah = malloc(sizeof(struct secashead), M_IPSEC_SAH, M_NOWAIT|M_ZERO);
2602 if (newsah != NULL) {
2604 for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++)
2605 LIST_INIT(&newsah->savtree[i]);
2606 newsah->saidx = *saidx;
2608 /* add to saidxtree */
2609 newsah->state = SADB_SASTATE_MATURE;
2612 LIST_INSERT_HEAD(&sahtree, newsah, chain);
2619 * delete SA index and all SA registerd.
2623 struct secashead *sah;
2625 struct secasvar *sav, *nextsav;
2629 IPSEC_ASSERT(sah != NULL, ("NULL sah"));
2630 SAHTREE_LOCK_ASSERT();
2632 /* searching all SA registerd in the secindex. */
2634 stateidx < _ARRAYLEN(saorder_state_any);
2636 u_int state = saorder_state_any[stateidx];
2637 LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain, nextsav) {
2638 if (sav->refcnt == 0) {
2640 KEY_CHKSASTATE(state, sav->state, __func__);
2643 /* give up to delete this sa */
2648 if (!zombie) { /* delete only if there are savs */
2649 /* remove from tree of SA index */
2650 if (__LIST_CHAINED(sah))
2651 LIST_REMOVE(sah, chain);
2652 if (sah->sa_route.ro_rt) {
2653 RTFREE(sah->sa_route.ro_rt);
2654 sah->sa_route.ro_rt = (struct rtentry *)NULL;
2656 free(sah, M_IPSEC_SAH);
2661 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2662 * and copy the values of mhp into new buffer.
2663 * When SAD message type is GETSPI:
2664 * to set sequence number from acq_seq++,
2665 * to set zero to SPI.
2666 * not to call key_setsava().
2668 * others : pointer to new secasvar.
2670 * does not modify mbuf. does not free mbuf on error.
2672 static struct secasvar *
2673 key_newsav(m, mhp, sah, errp, where, tag)
2675 const struct sadb_msghdr *mhp;
2676 struct secashead *sah;
2681 struct secasvar *newsav;
2682 const struct sadb_sa *xsa;
2684 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2685 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2686 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2687 IPSEC_ASSERT(sah != NULL, ("null secashead"));
2689 newsav = malloc(sizeof(struct secasvar), M_IPSEC_SA, M_NOWAIT|M_ZERO);
2690 if (newsav == NULL) {
2691 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2696 switch (mhp->msg->sadb_msg_type) {
2700 #ifdef IPSEC_DOSEQCHECK
2701 /* sync sequence number */
2702 if (mhp->msg->sadb_msg_seq == 0)
2704 (acq_seq = (acq_seq == ~0 ? 1 : ++acq_seq));
2707 newsav->seq = mhp->msg->sadb_msg_seq;
2712 if (mhp->ext[SADB_EXT_SA] == NULL) {
2713 free(newsav, M_IPSEC_SA);
2715 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2720 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2721 newsav->spi = xsa->sadb_sa_spi;
2722 newsav->seq = mhp->msg->sadb_msg_seq;
2725 free(newsav, M_IPSEC_SA);
2732 /* copy sav values */
2733 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2734 *errp = key_setsaval(newsav, m, mhp);
2736 free(newsav, M_IPSEC_SA);
2742 SECASVAR_LOCK_INIT(newsav);
2745 newsav->created = time_second;
2746 newsav->pid = mhp->msg->sadb_msg_pid;
2751 newsav->state = SADB_SASTATE_LARVAL;
2753 /* XXX locking??? */
2754 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2757 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
2758 printf("DP %s from %s:%u return SP:%p\n", __func__,
2759 where, tag, newsav));
2765 * free() SA variable entry.
2768 key_cleansav(struct secasvar *sav)
2771 * Cleanup xform state. Note that zeroize'ing causes the
2772 * keys to be cleared; otherwise we must do it ourself.
2774 if (sav->tdb_xform != NULL) {
2775 sav->tdb_xform->xf_zeroize(sav);
2776 sav->tdb_xform = NULL;
2778 KASSERT(sav->iv == NULL, ("iv but no xform"));
2779 if (sav->key_auth != NULL)
2780 bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
2781 if (sav->key_enc != NULL)
2782 bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
2784 if (sav->key_auth != NULL) {
2785 free(sav->key_auth, M_IPSEC_MISC);
2786 sav->key_auth = NULL;
2788 if (sav->key_enc != NULL) {
2789 free(sav->key_enc, M_IPSEC_MISC);
2790 sav->key_enc = NULL;
2793 bzero(sav->sched, sav->schedlen);
2794 free(sav->sched, M_IPSEC_MISC);
2797 if (sav->replay != NULL) {
2798 free(sav->replay, M_IPSEC_MISC);
2801 if (sav->lft_c != NULL) {
2802 free(sav->lft_c, M_IPSEC_MISC);
2805 if (sav->lft_h != NULL) {
2806 free(sav->lft_h, M_IPSEC_MISC);
2809 if (sav->lft_s != NULL) {
2810 free(sav->lft_s, M_IPSEC_MISC);
2816 * free() SA variable entry.
2820 struct secasvar *sav;
2822 IPSEC_ASSERT(sav != NULL, ("null sav"));
2823 IPSEC_ASSERT(sav->refcnt == 0, ("reference count %u > 0", sav->refcnt));
2825 /* remove from SA header */
2826 if (__LIST_CHAINED(sav))
2827 LIST_REMOVE(sav, chain);
2829 SECASVAR_LOCK_DESTROY(sav);
2830 free(sav, M_IPSEC_SA);
2837 * others : found, pointer to a SA.
2839 static struct secashead *
2841 struct secasindex *saidx;
2843 struct secashead *sah;
2846 LIST_FOREACH(sah, &sahtree, chain) {
2847 if (sah->state == SADB_SASTATE_DEAD)
2849 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2858 * check not to be duplicated SPI.
2859 * NOTE: this function is too slow due to searching all SAD.
2862 * others : found, pointer to a SA.
2864 static struct secasvar *
2865 key_checkspidup(saidx, spi)
2866 struct secasindex *saidx;
2869 struct secashead *sah;
2870 struct secasvar *sav;
2872 /* check address family */
2873 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
2874 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
2882 LIST_FOREACH(sah, &sahtree, chain) {
2883 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
2885 sav = key_getsavbyspi(sah, spi);
2895 * search SAD litmited alive SA, protocol, SPI.
2898 * others : found, pointer to a SA.
2900 static struct secasvar *
2901 key_getsavbyspi(sah, spi)
2902 struct secashead *sah;
2905 struct secasvar *sav;
2906 u_int stateidx, state;
2909 SAHTREE_LOCK_ASSERT();
2910 /* search all status */
2912 stateidx < _ARRAYLEN(saorder_state_alive);
2915 state = saorder_state_alive[stateidx];
2916 LIST_FOREACH(sav, &sah->savtree[state], chain) {
2919 if (sav->state != state) {
2920 ipseclog((LOG_DEBUG, "%s: "
2921 "invalid sav->state (queue: %d SA: %d)\n",
2922 __func__, state, sav->state));
2926 if (sav->spi == spi)
2935 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
2936 * You must update these if need.
2940 * does not modify mbuf. does not free mbuf on error.
2943 key_setsaval(sav, m, mhp)
2944 struct secasvar *sav;
2946 const struct sadb_msghdr *mhp;
2950 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2951 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2952 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2954 /* initialization */
2956 sav->key_auth = NULL;
2957 sav->key_enc = NULL;
2964 sav->tdb_xform = NULL; /* transform */
2965 sav->tdb_encalgxform = NULL; /* encoding algorithm */
2966 sav->tdb_authalgxform = NULL; /* authentication algorithm */
2967 sav->tdb_compalgxform = NULL; /* compression algorithm */
2970 if (mhp->ext[SADB_EXT_SA] != NULL) {
2971 const struct sadb_sa *sa0;
2973 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2974 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
2979 sav->alg_auth = sa0->sadb_sa_auth;
2980 sav->alg_enc = sa0->sadb_sa_encrypt;
2981 sav->flags = sa0->sadb_sa_flags;
2984 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
2985 sav->replay = (struct secreplay *)
2986 malloc(sizeof(struct secreplay)+sa0->sadb_sa_replay, M_IPSEC_MISC, M_NOWAIT|M_ZERO);
2987 if (sav->replay == NULL) {
2988 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
2993 if (sa0->sadb_sa_replay != 0)
2994 sav->replay->bitmap = (caddr_t)(sav->replay+1);
2995 sav->replay->wsize = sa0->sadb_sa_replay;
2999 /* Authentication keys */
3000 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
3001 const struct sadb_key *key0;
3004 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
3005 len = mhp->extlen[SADB_EXT_KEY_AUTH];
3008 if (len < sizeof(*key0)) {
3012 switch (mhp->msg->sadb_msg_satype) {
3013 case SADB_SATYPE_AH:
3014 case SADB_SATYPE_ESP:
3015 case SADB_X_SATYPE_TCPSIGNATURE:
3016 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3017 sav->alg_auth != SADB_X_AALG_NULL)
3020 case SADB_X_SATYPE_IPCOMP:
3026 ipseclog((LOG_DEBUG, "%s: invalid key_auth values.\n",
3031 sav->key_auth = key_dup(key0, len, M_IPSEC_MISC);
3032 if (sav->key_auth == NULL) {
3033 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3039 /* Encryption key */
3040 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3041 const struct sadb_key *key0;
3044 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3045 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3048 if (len < sizeof(*key0)) {
3052 switch (mhp->msg->sadb_msg_satype) {
3053 case SADB_SATYPE_ESP:
3054 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3055 sav->alg_enc != SADB_EALG_NULL) {
3059 sav->key_enc = key_dup(key0, len, M_IPSEC_MISC);
3060 if (sav->key_enc == NULL) {
3061 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3067 case SADB_X_SATYPE_IPCOMP:
3068 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3070 sav->key_enc = NULL; /*just in case*/
3072 case SADB_SATYPE_AH:
3073 case SADB_X_SATYPE_TCPSIGNATURE:
3079 ipseclog((LOG_DEBUG, "%s: invalid key_enc value.\n",
3088 switch (mhp->msg->sadb_msg_satype) {
3089 case SADB_SATYPE_AH:
3090 error = xform_init(sav, XF_AH);
3092 case SADB_SATYPE_ESP:
3093 error = xform_init(sav, XF_ESP);
3095 case SADB_X_SATYPE_IPCOMP:
3096 error = xform_init(sav, XF_IPCOMP);
3098 case SADB_X_SATYPE_TCPSIGNATURE:
3099 error = xform_init(sav, XF_TCPSIGNATURE);
3103 ipseclog((LOG_DEBUG, "%s: unable to initialize SA type %u.\n",
3104 __func__, mhp->msg->sadb_msg_satype));
3109 sav->created = time_second;
3111 /* make lifetime for CURRENT */
3112 sav->lft_c = malloc(sizeof(struct sadb_lifetime), M_IPSEC_MISC, M_NOWAIT);
3113 if (sav->lft_c == NULL) {
3114 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3119 sav->lft_c->sadb_lifetime_len =
3120 PFKEY_UNIT64(sizeof(struct sadb_lifetime));
3121 sav->lft_c->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
3122 sav->lft_c->sadb_lifetime_allocations = 0;
3123 sav->lft_c->sadb_lifetime_bytes = 0;
3124 sav->lft_c->sadb_lifetime_addtime = time_second;
3125 sav->lft_c->sadb_lifetime_usetime = 0;
3127 /* lifetimes for HARD and SOFT */
3129 const struct sadb_lifetime *lft0;
3131 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3133 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3137 sav->lft_h = key_dup(lft0, sizeof(*lft0), M_IPSEC_MISC);
3138 if (sav->lft_h == NULL) {
3139 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3143 /* to be initialize ? */
3146 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3148 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3152 sav->lft_s = key_dup(lft0, sizeof(*lft0), M_IPSEC_MISC);
3153 if (sav->lft_s == NULL) {
3154 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3158 /* to be initialize ? */
3165 /* initialization */
3172 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3177 key_mature(struct secasvar *sav)
3181 /* check SPI value */
3182 switch (sav->sah->saidx.proto) {
3185 if (ntohl(sav->spi) >= 0 && ntohl(sav->spi) <= 255) {
3186 ipseclog((LOG_DEBUG, "%s: illegal range of SPI %u.\n",
3187 __func__, (u_int32_t)ntohl(sav->spi)));
3194 switch (sav->sah->saidx.proto) {
3197 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3198 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3199 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3200 "given to old-esp.\n", __func__));
3203 error = xform_init(sav, XF_ESP);
3207 if (sav->flags & SADB_X_EXT_DERIV) {
3208 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3209 "given to AH SA.\n", __func__));
3212 if (sav->alg_enc != SADB_EALG_NONE) {
3213 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3214 "mismated.\n", __func__));
3217 error = xform_init(sav, XF_AH);
3219 case IPPROTO_IPCOMP:
3220 if (sav->alg_auth != SADB_AALG_NONE) {
3221 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3222 "mismated.\n", __func__));
3225 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3226 && ntohl(sav->spi) >= 0x10000) {
3227 ipseclog((LOG_DEBUG, "%s: invalid cpi for IPComp.\n",
3231 error = xform_init(sav, XF_IPCOMP);
3234 if (sav->alg_enc != SADB_EALG_NONE) {
3235 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3236 "mismated.\n", __func__));
3239 error = xform_init(sav, XF_TCPSIGNATURE);
3242 ipseclog((LOG_DEBUG, "%s: Invalid satype.\n", __func__));
3243 error = EPROTONOSUPPORT;
3248 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3255 * subroutine for SADB_GET and SADB_DUMP.
3257 static struct mbuf *
3258 key_setdumpsa(sav, type, satype, seq, pid)
3259 struct secasvar *sav;
3260 u_int8_t type, satype;
3263 struct mbuf *result = NULL, *tres = NULL, *m;
3268 SADB_EXT_SA, SADB_X_EXT_SA2,
3269 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3270 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3271 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3272 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3273 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3276 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3281 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3284 switch (dumporder[i]) {
3286 m = key_setsadbsa(sav);
3291 case SADB_X_EXT_SA2:
3292 m = key_setsadbxsa2(sav->sah->saidx.mode,
3293 sav->replay ? sav->replay->count : 0,
3294 sav->sah->saidx.reqid);
3299 case SADB_EXT_ADDRESS_SRC:
3300 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3301 &sav->sah->saidx.src.sa,
3302 FULLMASK, IPSEC_ULPROTO_ANY);
3307 case SADB_EXT_ADDRESS_DST:
3308 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3309 &sav->sah->saidx.dst.sa,
3310 FULLMASK, IPSEC_ULPROTO_ANY);
3315 case SADB_EXT_KEY_AUTH:
3318 l = PFKEY_UNUNIT64(sav->key_auth->sadb_key_len);
3322 case SADB_EXT_KEY_ENCRYPT:
3325 l = PFKEY_UNUNIT64(sav->key_enc->sadb_key_len);
3329 case SADB_EXT_LIFETIME_CURRENT:
3332 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_c)->sadb_ext_len);
3336 case SADB_EXT_LIFETIME_HARD:
3339 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_h)->sadb_ext_len);
3343 case SADB_EXT_LIFETIME_SOFT:
3346 l = PFKEY_UNUNIT64(((struct sadb_ext *)sav->lft_s)->sadb_ext_len);
3350 case SADB_EXT_ADDRESS_PROXY:
3351 case SADB_EXT_IDENTITY_SRC:
3352 case SADB_EXT_IDENTITY_DST:
3353 /* XXX: should we brought from SPD ? */
3354 case SADB_EXT_SENSITIVITY:
3359 if ((!m && !p) || (m && p))
3362 M_PREPEND(tres, l, M_DONTWAIT);
3365 bcopy(p, mtod(tres, caddr_t), l);
3369 m = key_alloc_mbuf(l);
3372 m_copyback(m, 0, l, p);
3380 m_cat(result, tres);
3382 if (result->m_len < sizeof(struct sadb_msg)) {
3383 result = m_pullup(result, sizeof(struct sadb_msg));
3388 result->m_pkthdr.len = 0;
3389 for (m = result; m; m = m->m_next)
3390 result->m_pkthdr.len += m->m_len;
3392 mtod(result, struct sadb_msg *)->sadb_msg_len =
3393 PFKEY_UNIT64(result->m_pkthdr.len);
3404 * set data into sadb_msg.
3406 static struct mbuf *
3407 key_setsadbmsg(type, tlen, satype, seq, pid, reserved)
3408 u_int8_t type, satype;
3418 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3421 MGETHDR(m, M_DONTWAIT, MT_DATA);
3422 if (m && len > MHLEN) {
3423 MCLGET(m, M_DONTWAIT);
3424 if ((m->m_flags & M_EXT) == 0) {
3431 m->m_pkthdr.len = m->m_len = len;
3434 p = mtod(m, struct sadb_msg *);
3437 p->sadb_msg_version = PF_KEY_V2;
3438 p->sadb_msg_type = type;
3439 p->sadb_msg_errno = 0;
3440 p->sadb_msg_satype = satype;
3441 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3442 p->sadb_msg_reserved = reserved;
3443 p->sadb_msg_seq = seq;
3444 p->sadb_msg_pid = (u_int32_t)pid;
3450 * copy secasvar data into sadb_address.
3452 static struct mbuf *
3454 struct secasvar *sav;
3460 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3461 m = key_alloc_mbuf(len);
3462 if (!m || m->m_next) { /*XXX*/
3468 p = mtod(m, struct sadb_sa *);
3471 p->sadb_sa_len = PFKEY_UNIT64(len);
3472 p->sadb_sa_exttype = SADB_EXT_SA;
3473 p->sadb_sa_spi = sav->spi;
3474 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3475 p->sadb_sa_state = sav->state;
3476 p->sadb_sa_auth = sav->alg_auth;
3477 p->sadb_sa_encrypt = sav->alg_enc;
3478 p->sadb_sa_flags = sav->flags;
3484 * set data into sadb_address.
3486 static struct mbuf *
3487 key_setsadbaddr(exttype, saddr, prefixlen, ul_proto)
3489 const struct sockaddr *saddr;
3494 struct sadb_address *p;
3497 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3498 PFKEY_ALIGN8(saddr->sa_len);
3499 m = key_alloc_mbuf(len);
3500 if (!m || m->m_next) { /*XXX*/
3506 p = mtod(m, struct sadb_address *);
3509 p->sadb_address_len = PFKEY_UNIT64(len);
3510 p->sadb_address_exttype = exttype;
3511 p->sadb_address_proto = ul_proto;
3512 if (prefixlen == FULLMASK) {
3513 switch (saddr->sa_family) {
3515 prefixlen = sizeof(struct in_addr) << 3;
3518 prefixlen = sizeof(struct in6_addr) << 3;
3524 p->sadb_address_prefixlen = prefixlen;
3525 p->sadb_address_reserved = 0;
3528 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3535 * set data into sadb_x_sa2.
3537 static struct mbuf *
3538 key_setsadbxsa2(mode, seq, reqid)
3540 u_int32_t seq, reqid;
3543 struct sadb_x_sa2 *p;
3546 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3547 m = key_alloc_mbuf(len);
3548 if (!m || m->m_next) { /*XXX*/
3554 p = mtod(m, struct sadb_x_sa2 *);
3557 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3558 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3559 p->sadb_x_sa2_mode = mode;
3560 p->sadb_x_sa2_reserved1 = 0;
3561 p->sadb_x_sa2_reserved2 = 0;
3562 p->sadb_x_sa2_sequence = seq;
3563 p->sadb_x_sa2_reqid = reqid;
3569 * set data into sadb_x_policy
3571 static struct mbuf *
3572 key_setsadbxpolicy(type, dir, id)
3578 struct sadb_x_policy *p;
3581 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3582 m = key_alloc_mbuf(len);
3583 if (!m || m->m_next) { /*XXX*/
3589 p = mtod(m, struct sadb_x_policy *);
3592 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3593 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3594 p->sadb_x_policy_type = type;
3595 p->sadb_x_policy_dir = dir;
3596 p->sadb_x_policy_id = id;
3603 * copy a buffer into the new buffer allocated.
3606 key_dup(const void *src, u_int len, struct malloc_type *type)
3610 copy = malloc(len, type, M_NOWAIT);
3613 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3615 bcopy(src, copy, len);
3619 /* compare my own address
3620 * OUT: 1: true, i.e. my address.
3625 struct sockaddr *sa;
3628 struct sockaddr_in *sin;
3629 struct in_ifaddr *ia;
3632 IPSEC_ASSERT(sa != NULL, ("null sockaddr"));
3634 switch (sa->sa_family) {
3637 sin = (struct sockaddr_in *)sa;
3638 for (ia = in_ifaddrhead.tqh_first; ia;
3639 ia = ia->ia_link.tqe_next)
3641 if (sin->sin_family == ia->ia_addr.sin_family &&
3642 sin->sin_len == ia->ia_addr.sin_len &&
3643 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3652 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3661 * compare my own address for IPv6.
3664 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3666 #include <netinet6/in6_var.h>
3670 struct sockaddr_in6 *sin6;
3672 struct in6_ifaddr *ia;
3673 struct in6_multi *in6m;
3675 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
3676 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3677 (struct sockaddr *)&ia->ia_addr, 0) == 0)
3682 * XXX why do we care about multlicast here while we don't care
3683 * about IPv4 multicast??
3687 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3692 /* loopback, just for safety */
3693 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
3701 * compare two secasindex structure.
3702 * flag can specify to compare 2 saidxes.
3703 * compare two secasindex structure without both mode and reqid.
3704 * don't compare port.
3706 * saidx0: source, it can be in SAD.
3714 const struct secasindex *saidx0,
3715 const struct secasindex *saidx1,
3719 if (saidx0 == NULL && saidx1 == NULL)
3722 if (saidx0 == NULL || saidx1 == NULL)
3725 if (saidx0->proto != saidx1->proto)
3728 if (flag == CMP_EXACTLY) {
3729 if (saidx0->mode != saidx1->mode)
3731 if (saidx0->reqid != saidx1->reqid)
3733 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
3734 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
3738 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
3739 if (flag == CMP_MODE_REQID
3740 ||flag == CMP_REQID) {
3742 * If reqid of SPD is non-zero, unique SA is required.
3743 * The result must be of same reqid in this case.
3745 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
3749 if (flag == CMP_MODE_REQID) {
3750 if (saidx0->mode != IPSEC_MODE_ANY
3751 && saidx0->mode != saidx1->mode)
3755 if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, 0) != 0) {
3758 if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, 0) != 0) {
3767 * compare two secindex structure exactly.
3769 * spidx0: source, it is often in SPD.
3770 * spidx1: object, it is often from PFKEY message.
3776 key_cmpspidx_exactly(
3777 struct secpolicyindex *spidx0,
3778 struct secpolicyindex *spidx1)
3781 if (spidx0 == NULL && spidx1 == NULL)
3784 if (spidx0 == NULL || spidx1 == NULL)
3787 if (spidx0->prefs != spidx1->prefs
3788 || spidx0->prefd != spidx1->prefd
3789 || spidx0->ul_proto != spidx1->ul_proto)
3792 return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
3793 key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
3797 * compare two secindex structure with mask.
3799 * spidx0: source, it is often in SPD.
3800 * spidx1: object, it is often from IP header.
3806 key_cmpspidx_withmask(
3807 struct secpolicyindex *spidx0,
3808 struct secpolicyindex *spidx1)
3811 if (spidx0 == NULL && spidx1 == NULL)
3814 if (spidx0 == NULL || spidx1 == NULL)
3817 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
3818 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
3819 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
3820 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
3823 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
3824 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
3825 && spidx0->ul_proto != spidx1->ul_proto)
3828 switch (spidx0->src.sa.sa_family) {
3830 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
3831 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
3833 if (!key_bbcmp(&spidx0->src.sin.sin_addr,
3834 &spidx1->src.sin.sin_addr, spidx0->prefs))
3838 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
3839 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
3842 * scope_id check. if sin6_scope_id is 0, we regard it
3843 * as a wildcard scope, which matches any scope zone ID.
3845 if (spidx0->src.sin6.sin6_scope_id &&
3846 spidx1->src.sin6.sin6_scope_id &&
3847 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
3849 if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
3850 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
3855 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
3860 switch (spidx0->dst.sa.sa_family) {
3862 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
3863 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
3865 if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
3866 &spidx1->dst.sin.sin_addr, spidx0->prefd))
3870 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
3871 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
3874 * scope_id check. if sin6_scope_id is 0, we regard it
3875 * as a wildcard scope, which matches any scope zone ID.
3877 if (spidx0->dst.sin6.sin6_scope_id &&
3878 spidx1->dst.sin6.sin6_scope_id &&
3879 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
3881 if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
3882 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
3887 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
3892 /* XXX Do we check other field ? e.g. flowinfo */
3897 /* returns 0 on match */
3900 const struct sockaddr *sa1,
3901 const struct sockaddr *sa2,
3907 #define satosin(s) ((const struct sockaddr_in *)s)
3911 #define satosin6(s) ((const struct sockaddr_in6 *)s)
3912 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
3915 switch (sa1->sa_family) {
3917 if (sa1->sa_len != sizeof(struct sockaddr_in))
3919 if (satosin(sa1)->sin_addr.s_addr !=
3920 satosin(sa2)->sin_addr.s_addr) {
3923 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
3927 if (sa1->sa_len != sizeof(struct sockaddr_in6))
3928 return 1; /*EINVAL*/
3929 if (satosin6(sa1)->sin6_scope_id !=
3930 satosin6(sa2)->sin6_scope_id) {
3933 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
3934 &satosin6(sa2)->sin6_addr)) {
3938 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
3942 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
3953 * compare two buffers with mask.
3957 * bits: Number of bits to compare
3963 key_bbcmp(const void *a1, const void *a2, u_int bits)
3965 const unsigned char *p1 = a1;
3966 const unsigned char *p2 = a2;
3968 /* XXX: This could be considerably faster if we compare a word
3969 * at a time, but it is complicated on LSB Endian machines */
3971 /* Handle null pointers */
3972 if (p1 == NULL || p2 == NULL)
3982 u_int8_t mask = ~((1<<(8-bits))-1);
3983 if ((*p1 & mask) != (*p2 & mask))
3986 return 1; /* Match! */
3990 key_flush_spd(time_t now)
3992 static u_int16_t sptree_scangen = 0;
3993 u_int16_t gen = sptree_scangen++;
3994 struct secpolicy *sp;
3998 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4001 LIST_FOREACH(sp, &sptree[dir], chain) {
4002 if (sp->scangen == gen) /* previously handled */
4005 if (sp->state == IPSEC_SPSTATE_DEAD) {
4006 /* NB: clean entries created by key_spdflush */
4011 if (sp->lifetime == 0 && sp->validtime == 0)
4013 if ((sp->lifetime && now - sp->created > sp->lifetime)
4014 || (sp->validtime && now - sp->lastused > sp->validtime)) {
4015 sp->state = IPSEC_SPSTATE_DEAD;
4027 key_flush_sad(time_t now)
4029 struct secashead *sah, *nextsah;
4030 struct secasvar *sav, *nextsav;
4034 LIST_FOREACH_SAFE(sah, &sahtree, chain, nextsah) {
4035 /* if sah has been dead, then delete it and process next sah. */
4036 if (sah->state == SADB_SASTATE_DEAD) {
4041 /* if LARVAL entry doesn't become MATURE, delete it. */
4042 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_LARVAL], chain, nextsav) {
4043 if (now - sav->created > key_larval_lifetime)
4048 * check MATURE entry to start to send expire message
4051 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_MATURE], chain, nextsav) {
4052 /* we don't need to check. */
4053 if (sav->lft_s == NULL)
4057 if (sav->lft_c == NULL) {
4058 ipseclog((LOG_DEBUG,"%s: there is no CURRENT "
4059 "time, why?\n", __func__));
4063 /* check SOFT lifetime */
4064 if (sav->lft_s->sadb_lifetime_addtime != 0 &&
4065 now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4067 * check SA to be used whether or not.
4068 * when SA hasn't been used, delete it.
4070 if (sav->lft_c->sadb_lifetime_usetime == 0) {
4071 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4074 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4076 * XXX If we keep to send expire
4077 * message in the status of
4078 * DYING. Do remove below code.
4083 /* check SOFT lifetime by bytes */
4085 * XXX I don't know the way to delete this SA
4086 * when new SA is installed. Caution when it's
4087 * installed too big lifetime by time.
4089 else if (sav->lft_s->sadb_lifetime_bytes != 0 &&
4090 sav->lft_s->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4092 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4094 * XXX If we keep to send expire
4095 * message in the status of
4096 * DYING. Do remove below code.
4102 /* check DYING entry to change status to DEAD. */
4103 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DYING], chain, nextsav) {
4104 /* we don't need to check. */
4105 if (sav->lft_h == NULL)
4109 if (sav->lft_c == NULL) {
4110 ipseclog((LOG_DEBUG, "%s: there is no CURRENT "
4111 "time, why?\n", __func__));
4115 if (sav->lft_h->sadb_lifetime_addtime != 0 &&
4116 now - sav->created > sav->lft_h->sadb_lifetime_addtime) {
4117 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4120 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4121 else if (sav->lft_s != NULL
4122 && sav->lft_s->sadb_lifetime_addtime != 0
4123 && now - sav->created > sav->lft_s->sadb_lifetime_addtime) {
4125 * XXX: should be checked to be
4126 * installed the valid SA.
4130 * If there is no SA then sending
4136 /* check HARD lifetime by bytes */
4137 else if (sav->lft_h->sadb_lifetime_bytes != 0 &&
4138 sav->lft_h->sadb_lifetime_bytes < sav->lft_c->sadb_lifetime_bytes) {
4139 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4144 /* delete entry in DEAD */
4145 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DEAD], chain, nextsav) {
4147 if (sav->state != SADB_SASTATE_DEAD) {
4148 ipseclog((LOG_DEBUG, "%s: invalid sav->state "
4149 "(queue: %d SA: %d): kill it anyway\n",
4151 SADB_SASTATE_DEAD, sav->state));
4154 * do not call key_freesav() here.
4155 * sav should already be freed, and sav->refcnt
4156 * shows other references to sav
4157 * (such as from SPD).
4165 key_flush_acq(time_t now)
4167 struct secacq *acq, *nextacq;
4171 for (acq = LIST_FIRST(&acqtree); acq != NULL; acq = nextacq) {
4172 nextacq = LIST_NEXT(acq, chain);
4173 if (now - acq->created > key_blockacq_lifetime
4174 && __LIST_CHAINED(acq)) {
4175 LIST_REMOVE(acq, chain);
4176 free(acq, M_IPSEC_SAQ);
4183 key_flush_spacq(time_t now)
4185 struct secspacq *acq, *nextacq;
4189 for (acq = LIST_FIRST(&spacqtree); acq != NULL; acq = nextacq) {
4190 nextacq = LIST_NEXT(acq, chain);
4191 if (now - acq->created > key_blockacq_lifetime
4192 && __LIST_CHAINED(acq)) {
4193 LIST_REMOVE(acq, chain);
4194 free(acq, M_IPSEC_SAQ);
4202 * scanning SPD and SAD to check status for each entries,
4203 * and do to remove or to expire.
4204 * XXX: year 2038 problem may remain.
4207 key_timehandler(void)
4209 time_t now = time_second;
4214 key_flush_spacq(now);
4216 #ifndef IPSEC_DEBUG2
4217 /* do exchange to tick time !! */
4218 (void)timeout((void *)key_timehandler, (void *)0, hz);
4219 #endif /* IPSEC_DEBUG2 */
4227 key_randomfill(&value, sizeof(value));
4232 key_randomfill(p, l)
4238 static int warn = 1;
4241 n = (size_t)read_random(p, (u_int)l);
4245 bcopy(&v, (u_int8_t *)p + n,
4246 l - n < sizeof(v) ? l - n : sizeof(v));
4250 printf("WARNING: pseudo-random number generator "
4251 "used for IPsec processing\n");
4258 * map SADB_SATYPE_* to IPPROTO_*.
4259 * if satype == SADB_SATYPE then satype is mapped to ~0.
4261 * 0: invalid satype.
4264 key_satype2proto(satype)
4268 case SADB_SATYPE_UNSPEC:
4269 return IPSEC_PROTO_ANY;
4270 case SADB_SATYPE_AH:
4272 case SADB_SATYPE_ESP:
4274 case SADB_X_SATYPE_IPCOMP:
4275 return IPPROTO_IPCOMP;
4276 case SADB_X_SATYPE_TCPSIGNATURE:
4285 * map IPPROTO_* to SADB_SATYPE_*
4287 * 0: invalid protocol type.
4290 key_proto2satype(proto)
4295 return SADB_SATYPE_AH;
4297 return SADB_SATYPE_ESP;
4298 case IPPROTO_IPCOMP:
4299 return SADB_X_SATYPE_IPCOMP;
4301 return SADB_X_SATYPE_TCPSIGNATURE;
4310 * SADB_GETSPI processing is to receive
4311 * <base, (SA2), src address, dst address, (SPI range)>
4312 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4313 * tree with the status of LARVAL, and send
4314 * <base, SA(*), address(SD)>
4317 * IN: mhp: pointer to the pointer to each header.
4318 * OUT: NULL if fail.
4319 * other if success, return pointer to the message to send.
4322 key_getspi(so, m, mhp)
4325 const struct sadb_msghdr *mhp;
4327 struct sadb_address *src0, *dst0;
4328 struct secasindex saidx;
4329 struct secashead *newsah;
4330 struct secasvar *newsav;
4337 IPSEC_ASSERT(so != NULL, ("null socket"));
4338 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4339 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4340 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4342 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4343 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4344 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4346 return key_senderror(so, m, EINVAL);
4348 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4349 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4350 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4352 return key_senderror(so, m, EINVAL);
4354 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4355 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4356 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4358 mode = IPSEC_MODE_ANY;
4362 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4363 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4365 /* map satype to proto */
4366 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4367 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4369 return key_senderror(so, m, EINVAL);
4372 /* make sure if port number is zero. */
4373 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4375 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4376 sizeof(struct sockaddr_in))
4377 return key_senderror(so, m, EINVAL);
4378 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4381 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4382 sizeof(struct sockaddr_in6))
4383 return key_senderror(so, m, EINVAL);
4384 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4389 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4391 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4392 sizeof(struct sockaddr_in))
4393 return key_senderror(so, m, EINVAL);
4394 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4397 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4398 sizeof(struct sockaddr_in6))
4399 return key_senderror(so, m, EINVAL);
4400 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4406 /* XXX boundary check against sa_len */
4407 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4409 /* SPI allocation */
4410 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4413 return key_senderror(so, m, EINVAL);
4415 /* get a SA index */
4416 if ((newsah = key_getsah(&saidx)) == NULL) {
4417 /* create a new SA index */
4418 if ((newsah = key_newsah(&saidx)) == NULL) {
4419 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
4420 return key_senderror(so, m, ENOBUFS);
4426 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4427 if (newsav == NULL) {
4428 /* XXX don't free new SA index allocated in above. */
4429 return key_senderror(so, m, error);
4433 newsav->spi = htonl(spi);
4435 /* delete the entry in acqtree */
4436 if (mhp->msg->sadb_msg_seq != 0) {
4438 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4439 /* reset counter in order to deletion by timehandler. */
4440 acq->created = time_second;
4446 struct mbuf *n, *nn;
4447 struct sadb_sa *m_sa;
4448 struct sadb_msg *newmsg;
4451 /* create new sadb_msg to reply. */
4452 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4453 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4455 return key_senderror(so, m, ENOBUFS);
4457 MGETHDR(n, M_DONTWAIT, MT_DATA);
4459 MCLGET(n, M_DONTWAIT);
4460 if ((n->m_flags & M_EXT) == 0) {
4466 return key_senderror(so, m, ENOBUFS);
4472 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4473 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4475 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4476 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4477 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4478 m_sa->sadb_sa_spi = htonl(spi);
4479 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4481 IPSEC_ASSERT(off == len,
4482 ("length inconsistency (off %u len %u)", off, len));
4484 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4485 SADB_EXT_ADDRESS_DST);
4488 return key_senderror(so, m, ENOBUFS);
4491 if (n->m_len < sizeof(struct sadb_msg)) {
4492 n = m_pullup(n, sizeof(struct sadb_msg));
4494 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4497 n->m_pkthdr.len = 0;
4498 for (nn = n; nn; nn = nn->m_next)
4499 n->m_pkthdr.len += nn->m_len;
4501 newmsg = mtod(n, struct sadb_msg *);
4502 newmsg->sadb_msg_seq = newsav->seq;
4503 newmsg->sadb_msg_errno = 0;
4504 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4507 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4512 * allocating new SPI
4513 * called by key_getspi().
4519 key_do_getnewspi(spirange, saidx)
4520 struct sadb_spirange *spirange;
4521 struct secasindex *saidx;
4525 int count = key_spi_trycnt;
4527 /* set spi range to allocate */
4528 if (spirange != NULL) {
4529 min = spirange->sadb_spirange_min;
4530 max = spirange->sadb_spirange_max;
4532 min = key_spi_minval;
4533 max = key_spi_maxval;
4535 /* IPCOMP needs 2-byte SPI */
4536 if (saidx->proto == IPPROTO_IPCOMP) {
4543 t = min; min = max; max = t;
4548 if (key_checkspidup(saidx, min) != NULL) {
4549 ipseclog((LOG_DEBUG, "%s: SPI %u exists already.\n",
4554 count--; /* taking one cost. */
4562 /* when requesting to allocate spi ranged */
4564 /* generate pseudo-random SPI value ranged. */
4565 newspi = min + (key_random() % (max - min + 1));
4567 if (key_checkspidup(saidx, newspi) == NULL)
4571 if (count == 0 || newspi == 0) {
4572 ipseclog((LOG_DEBUG, "%s: to allocate spi is failed.\n",
4579 keystat.getspi_count =
4580 (keystat.getspi_count + key_spi_trycnt - count) / 2;
4586 * SADB_UPDATE processing
4588 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4589 * key(AE), (identity(SD),) (sensitivity)>
4590 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4592 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4593 * (identity(SD),) (sensitivity)>
4596 * m will always be freed.
4599 key_update(so, m, mhp)
4602 const struct sadb_msghdr *mhp;
4604 struct sadb_sa *sa0;
4605 struct sadb_address *src0, *dst0;
4606 struct secasindex saidx;
4607 struct secashead *sah;
4608 struct secasvar *sav;
4614 IPSEC_ASSERT(so != NULL, ("null socket"));
4615 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4616 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4617 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4619 /* map satype to proto */
4620 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4621 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4623 return key_senderror(so, m, EINVAL);
4626 if (mhp->ext[SADB_EXT_SA] == NULL ||
4627 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4628 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4629 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4630 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4631 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4632 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4633 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4634 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4635 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4636 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4637 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4639 return key_senderror(so, m, EINVAL);
4641 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4642 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4643 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4644 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4646 return key_senderror(so, m, EINVAL);
4648 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4649 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4650 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4652 mode = IPSEC_MODE_ANY;
4655 /* XXX boundary checking for other extensions */
4657 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4658 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4659 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4661 /* XXX boundary check against sa_len */
4662 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4664 /* get a SA header */
4665 if ((sah = key_getsah(&saidx)) == NULL) {
4666 ipseclog((LOG_DEBUG, "%s: no SA index found.\n", __func__));
4667 return key_senderror(so, m, ENOENT);
4670 /* set spidx if there */
4672 error = key_setident(sah, m, mhp);
4674 return key_senderror(so, m, error);
4676 /* find a SA with sequence number. */
4677 #ifdef IPSEC_DOSEQCHECK
4678 if (mhp->msg->sadb_msg_seq != 0
4679 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
4680 ipseclog((LOG_DEBUG, "%s: no larval SA with sequence %u "
4681 "exists.\n", __func__, mhp->msg->sadb_msg_seq));
4682 return key_senderror(so, m, ENOENT);
4686 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
4689 ipseclog((LOG_DEBUG, "%s: no such a SA found (spi:%u)\n",
4690 __func__, (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4691 return key_senderror(so, m, EINVAL);
4695 /* validity check */
4696 if (sav->sah->saidx.proto != proto) {
4697 ipseclog((LOG_DEBUG, "%s: protocol mismatched "
4698 "(DB=%u param=%u)\n", __func__,
4699 sav->sah->saidx.proto, proto));
4700 return key_senderror(so, m, EINVAL);
4702 #ifdef IPSEC_DOSEQCHECK
4703 if (sav->spi != sa0->sadb_sa_spi) {
4704 ipseclog((LOG_DEBUG, "%s: SPI mismatched (DB:%u param:%u)\n",
4706 (u_int32_t)ntohl(sav->spi),
4707 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
4708 return key_senderror(so, m, EINVAL);
4711 if (sav->pid != mhp->msg->sadb_msg_pid) {
4712 ipseclog((LOG_DEBUG, "%s: pid mismatched (DB:%u param:%u)\n",
4713 __func__, sav->pid, mhp->msg->sadb_msg_pid));
4714 return key_senderror(so, m, EINVAL);
4717 /* copy sav values */
4718 error = key_setsaval(sav, m, mhp);
4721 return key_senderror(so, m, error);
4724 /* check SA values to be mature. */
4725 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
4727 return key_senderror(so, m, 0);
4733 /* set msg buf from mhp */
4734 n = key_getmsgbuf_x1(m, mhp);
4736 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
4737 return key_senderror(so, m, ENOBUFS);
4741 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4746 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
4747 * only called by key_update().
4750 * others : found, pointer to a SA.
4752 #ifdef IPSEC_DOSEQCHECK
4753 static struct secasvar *
4754 key_getsavbyseq(sah, seq)
4755 struct secashead *sah;
4758 struct secasvar *sav;
4761 state = SADB_SASTATE_LARVAL;
4763 /* search SAD with sequence number ? */
4764 LIST_FOREACH(sav, &sah->savtree[state], chain) {
4766 KEY_CHKSASTATE(state, sav->state, __func__);
4768 if (sav->seq == seq) {
4770 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
4771 printf("DP %s cause refcnt++:%d SA:%p\n",
4772 __func__, sav->refcnt, sav));
4782 * SADB_ADD processing
4783 * add an entry to SA database, when received
4784 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4785 * key(AE), (identity(SD),) (sensitivity)>
4788 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4789 * (identity(SD),) (sensitivity)>
4792 * IGNORE identity and sensitivity messages.
4794 * m will always be freed.
4800 const struct sadb_msghdr *mhp;
4802 struct sadb_sa *sa0;
4803 struct sadb_address *src0, *dst0;
4804 struct secasindex saidx;
4805 struct secashead *newsah;
4806 struct secasvar *newsav;
4812 IPSEC_ASSERT(so != NULL, ("null socket"));
4813 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4814 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4815 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4817 /* map satype to proto */
4818 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4819 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4821 return key_senderror(so, m, EINVAL);
4824 if (mhp->ext[SADB_EXT_SA] == NULL ||
4825 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4826 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4827 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4828 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4829 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4830 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4831 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4832 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4833 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4834 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4835 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4837 return key_senderror(so, m, EINVAL);
4839 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4840 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4841 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4843 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4845 return key_senderror(so, m, EINVAL);
4847 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4848 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4849 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4851 mode = IPSEC_MODE_ANY;
4855 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4856 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
4857 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
4859 /* XXX boundary check against sa_len */
4860 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4862 /* get a SA header */
4863 if ((newsah = key_getsah(&saidx)) == NULL) {
4864 /* create a new SA header */
4865 if ((newsah = key_newsah(&saidx)) == NULL) {
4866 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
4867 return key_senderror(so, m, ENOBUFS);
4871 /* set spidx if there */
4873 error = key_setident(newsah, m, mhp);
4875 return key_senderror(so, m, error);
4878 /* create new SA entry. */
4879 /* We can create new SA only if SPI is differenct. */
4881 newsav = key_getsavbyspi(newsah, sa0->sadb_sa_spi);
4883 if (newsav != NULL) {
4884 ipseclog((LOG_DEBUG, "%s: SA already exists.\n", __func__));
4885 return key_senderror(so, m, EEXIST);
4887 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4888 if (newsav == NULL) {
4889 return key_senderror(so, m, error);
4892 /* check SA values to be mature. */
4893 if ((error = key_mature(newsav)) != 0) {
4894 KEY_FREESAV(&newsav);
4895 return key_senderror(so, m, error);
4899 * don't call key_freesav() here, as we would like to keep the SA
4900 * in the database on success.
4906 /* set msg buf from mhp */
4907 n = key_getmsgbuf_x1(m, mhp);
4909 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
4910 return key_senderror(so, m, ENOBUFS);
4914 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
4920 key_setident(sah, m, mhp)
4921 struct secashead *sah;
4923 const struct sadb_msghdr *mhp;
4925 const struct sadb_ident *idsrc, *iddst;
4926 int idsrclen, iddstlen;
4928 IPSEC_ASSERT(sah != NULL, ("null secashead"));
4929 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4930 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4931 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4933 /* don't make buffer if not there */
4934 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
4935 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
4941 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
4942 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
4943 ipseclog((LOG_DEBUG, "%s: invalid identity.\n", __func__));
4947 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
4948 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
4949 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
4950 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
4952 /* validity check */
4953 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
4954 ipseclog((LOG_DEBUG, "%s: ident type mismatch.\n", __func__));
4958 switch (idsrc->sadb_ident_type) {
4959 case SADB_IDENTTYPE_PREFIX:
4960 case SADB_IDENTTYPE_FQDN:
4961 case SADB_IDENTTYPE_USERFQDN:
4963 /* XXX do nothing */
4969 /* make structure */
4970 sah->idents = malloc(idsrclen, M_IPSEC_MISC, M_NOWAIT);
4971 if (sah->idents == NULL) {
4972 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
4975 sah->identd = malloc(iddstlen, M_IPSEC_MISC, M_NOWAIT);
4976 if (sah->identd == NULL) {
4977 free(sah->idents, M_IPSEC_MISC);
4979 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
4982 bcopy(idsrc, sah->idents, idsrclen);
4983 bcopy(iddst, sah->identd, iddstlen);
4989 * m will not be freed on return.
4990 * it is caller's responsibility to free the result.
4992 static struct mbuf *
4993 key_getmsgbuf_x1(m, mhp)
4995 const struct sadb_msghdr *mhp;
4999 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5000 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5001 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5003 /* create new sadb_msg to reply. */
5004 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5005 SADB_EXT_SA, SADB_X_EXT_SA2,
5006 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5007 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5008 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5012 if (n->m_len < sizeof(struct sadb_msg)) {
5013 n = m_pullup(n, sizeof(struct sadb_msg));
5017 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5018 mtod(n, struct sadb_msg *)->sadb_msg_len =
5019 PFKEY_UNIT64(n->m_pkthdr.len);
5024 static int key_delete_all __P((struct socket *, struct mbuf *,
5025 const struct sadb_msghdr *, u_int16_t));
5028 * SADB_DELETE processing
5030 * <base, SA(*), address(SD)>
5031 * from the ikmpd, and set SADB_SASTATE_DEAD,
5033 * <base, SA(*), address(SD)>
5036 * m will always be freed.
5039 key_delete(so, m, mhp)
5042 const struct sadb_msghdr *mhp;
5044 struct sadb_sa *sa0;
5045 struct sadb_address *src0, *dst0;
5046 struct secasindex saidx;
5047 struct secashead *sah;
5048 struct secasvar *sav = NULL;
5051 IPSEC_ASSERT(so != NULL, ("null socket"));
5052 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5053 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5054 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5056 /* map satype to proto */
5057 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5058 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5060 return key_senderror(so, m, EINVAL);
5063 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5064 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5065 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5067 return key_senderror(so, m, EINVAL);
5070 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5071 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5072 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5074 return key_senderror(so, m, EINVAL);
5077 if (mhp->ext[SADB_EXT_SA] == NULL) {
5079 * Caller wants us to delete all non-LARVAL SAs
5080 * that match the src/dst. This is used during
5081 * IKE INITIAL-CONTACT.
5083 ipseclog((LOG_DEBUG, "%s: doing delete all.\n", __func__));
5084 return key_delete_all(so, m, mhp, proto);
5085 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5086 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5088 return key_senderror(so, m, EINVAL);
5091 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5092 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5093 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5095 /* XXX boundary check against sa_len */
5096 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5098 /* get a SA header */
5100 LIST_FOREACH(sah, &sahtree, chain) {
5101 if (sah->state == SADB_SASTATE_DEAD)
5103 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5106 /* get a SA with SPI. */
5107 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5113 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5114 return key_senderror(so, m, ENOENT);
5117 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5123 struct sadb_msg *newmsg;
5125 /* create new sadb_msg to reply. */
5126 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5127 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5129 return key_senderror(so, m, ENOBUFS);
5131 if (n->m_len < sizeof(struct sadb_msg)) {
5132 n = m_pullup(n, sizeof(struct sadb_msg));
5134 return key_senderror(so, m, ENOBUFS);
5136 newmsg = mtod(n, struct sadb_msg *);
5137 newmsg->sadb_msg_errno = 0;
5138 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5141 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5146 * delete all SAs for src/dst. Called from key_delete().
5149 key_delete_all(so, m, mhp, proto)
5152 const struct sadb_msghdr *mhp;
5155 struct sadb_address *src0, *dst0;
5156 struct secasindex saidx;
5157 struct secashead *sah;
5158 struct secasvar *sav, *nextsav;
5159 u_int stateidx, state;
5161 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5162 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5164 /* XXX boundary check against sa_len */
5165 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5168 LIST_FOREACH(sah, &sahtree, chain) {
5169 if (sah->state == SADB_SASTATE_DEAD)
5171 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5174 /* Delete all non-LARVAL SAs. */
5176 stateidx < _ARRAYLEN(saorder_state_alive);
5178 state = saorder_state_alive[stateidx];
5179 if (state == SADB_SASTATE_LARVAL)
5181 for (sav = LIST_FIRST(&sah->savtree[state]);
5182 sav != NULL; sav = nextsav) {
5183 nextsav = LIST_NEXT(sav, chain);
5185 if (sav->state != state) {
5186 ipseclog((LOG_DEBUG, "%s: invalid "
5187 "sav->state (queue %d SA %d)\n",
5188 __func__, state, sav->state));
5192 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5200 struct sadb_msg *newmsg;
5202 /* create new sadb_msg to reply. */
5203 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5204 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5206 return key_senderror(so, m, ENOBUFS);
5208 if (n->m_len < sizeof(struct sadb_msg)) {
5209 n = m_pullup(n, sizeof(struct sadb_msg));
5211 return key_senderror(so, m, ENOBUFS);
5213 newmsg = mtod(n, struct sadb_msg *);
5214 newmsg->sadb_msg_errno = 0;
5215 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5218 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5223 * SADB_GET processing
5225 * <base, SA(*), address(SD)>
5226 * from the ikmpd, and get a SP and a SA to respond,
5228 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5229 * (identity(SD),) (sensitivity)>
5232 * m will always be freed.
5238 const struct sadb_msghdr *mhp;
5240 struct sadb_sa *sa0;
5241 struct sadb_address *src0, *dst0;
5242 struct secasindex saidx;
5243 struct secashead *sah;
5244 struct secasvar *sav = NULL;
5247 IPSEC_ASSERT(so != NULL, ("null socket"));
5248 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5249 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5250 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5252 /* map satype to proto */
5253 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5254 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5256 return key_senderror(so, m, EINVAL);
5259 if (mhp->ext[SADB_EXT_SA] == NULL ||
5260 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5261 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5262 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5264 return key_senderror(so, m, EINVAL);
5266 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5267 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5268 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5269 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5271 return key_senderror(so, m, EINVAL);
5274 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5275 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5276 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5278 /* XXX boundary check against sa_len */
5279 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5281 /* get a SA header */
5283 LIST_FOREACH(sah, &sahtree, chain) {
5284 if (sah->state == SADB_SASTATE_DEAD)
5286 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5289 /* get a SA with SPI. */
5290 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5296 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5297 return key_senderror(so, m, ENOENT);
5304 /* map proto to satype */
5305 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5306 ipseclog((LOG_DEBUG, "%s: there was invalid proto in SAD.\n",
5308 return key_senderror(so, m, EINVAL);
5311 /* create new sadb_msg to reply. */
5312 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5313 mhp->msg->sadb_msg_pid);
5315 return key_senderror(so, m, ENOBUFS);
5318 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5322 /* XXX make it sysctl-configurable? */
5324 key_getcomb_setlifetime(comb)
5325 struct sadb_comb *comb;
5328 comb->sadb_comb_soft_allocations = 1;
5329 comb->sadb_comb_hard_allocations = 1;
5330 comb->sadb_comb_soft_bytes = 0;
5331 comb->sadb_comb_hard_bytes = 0;
5332 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5333 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5334 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5335 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
5339 * XXX reorder combinations by preference
5340 * XXX no idea if the user wants ESP authentication or not
5342 static struct mbuf *
5345 struct sadb_comb *comb;
5346 struct enc_xform *algo;
5347 struct mbuf *result = NULL, *m, *n;
5351 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5354 for (i = 1; i <= SADB_EALG_MAX; i++) {
5355 algo = esp_algorithm_lookup(i);
5359 /* discard algorithms with key size smaller than system min */
5360 if (_BITS(algo->maxkey) < ipsec_esp_keymin)
5362 if (_BITS(algo->minkey) < ipsec_esp_keymin)
5363 encmin = ipsec_esp_keymin;
5365 encmin = _BITS(algo->minkey);
5368 m = key_getcomb_ah();
5370 IPSEC_ASSERT(l <= MLEN,
5371 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
5372 MGET(m, M_DONTWAIT, MT_DATA);
5377 bzero(mtod(m, caddr_t), m->m_len);
5384 for (n = m; n; n = n->m_next)
5386 IPSEC_ASSERT((totlen % l) == 0, ("totlen=%u, l=%u", totlen, l));
5388 for (off = 0; off < totlen; off += l) {
5389 n = m_pulldown(m, off, l, &o);
5391 /* m is already freed */
5394 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
5395 bzero(comb, sizeof(*comb));
5396 key_getcomb_setlifetime(comb);
5397 comb->sadb_comb_encrypt = i;
5398 comb->sadb_comb_encrypt_minbits = encmin;
5399 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
5418 const struct auth_hash *ah,
5423 *min = *max = ah->keysize;
5424 if (ah->keysize == 0) {
5426 * Transform takes arbitrary key size but algorithm
5427 * key size is restricted. Enforce this here.
5430 case SADB_X_AALG_MD5: *min = *max = 16; break;
5431 case SADB_X_AALG_SHA: *min = *max = 20; break;
5432 case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
5434 DPRINTF(("%s: unknown AH algorithm %u\n",
5442 * XXX reorder combinations by preference
5444 static struct mbuf *
5447 struct sadb_comb *comb;
5448 struct auth_hash *algo;
5450 u_int16_t minkeysize, maxkeysize;
5452 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5455 for (i = 1; i <= SADB_AALG_MAX; i++) {
5457 /* we prefer HMAC algorithms, not old algorithms */
5458 if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC)
5461 algo = ah_algorithm_lookup(i);
5464 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
5465 /* discard algorithms with key size smaller than system min */
5466 if (_BITS(minkeysize) < ipsec_ah_keymin)
5470 IPSEC_ASSERT(l <= MLEN,
5471 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
5472 MGET(m, M_DONTWAIT, MT_DATA);
5479 M_PREPEND(m, l, M_DONTWAIT);
5483 comb = mtod(m, struct sadb_comb *);
5484 bzero(comb, sizeof(*comb));
5485 key_getcomb_setlifetime(comb);
5486 comb->sadb_comb_auth = i;
5487 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
5488 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
5495 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
5496 * XXX reorder combinations by preference
5498 static struct mbuf *
5499 key_getcomb_ipcomp()
5501 struct sadb_comb *comb;
5502 struct comp_algo *algo;
5505 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5508 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
5509 algo = ipcomp_algorithm_lookup(i);
5514 IPSEC_ASSERT(l <= MLEN,
5515 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
5516 MGET(m, M_DONTWAIT, MT_DATA);
5523 M_PREPEND(m, l, M_DONTWAIT);
5527 comb = mtod(m, struct sadb_comb *);
5528 bzero(comb, sizeof(*comb));
5529 key_getcomb_setlifetime(comb);
5530 comb->sadb_comb_encrypt = i;
5531 /* what should we set into sadb_comb_*_{min,max}bits? */
5538 * XXX no way to pass mode (transport/tunnel) to userland
5539 * XXX replay checking?
5540 * XXX sysctl interface to ipsec_{ah,esp}_keymin
5542 static struct mbuf *
5544 const struct secasindex *saidx;
5546 struct sadb_prop *prop;
5548 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
5551 switch (saidx->proto) {
5553 m = key_getcomb_esp();
5556 m = key_getcomb_ah();
5558 case IPPROTO_IPCOMP:
5559 m = key_getcomb_ipcomp();
5567 M_PREPEND(m, l, M_DONTWAIT);
5572 for (n = m; n; n = n->m_next)
5575 prop = mtod(m, struct sadb_prop *);
5576 bzero(prop, sizeof(*prop));
5577 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
5578 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
5579 prop->sadb_prop_replay = 32; /* XXX */
5585 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
5587 * <base, SA, address(SD), (address(P)), x_policy,
5588 * (identity(SD),) (sensitivity,) proposal>
5589 * to KMD, and expect to receive
5590 * <base> with SADB_ACQUIRE if error occured,
5592 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
5593 * from KMD by PF_KEY.
5595 * XXX x_policy is outside of RFC2367 (KAME extension).
5596 * XXX sensitivity is not supported.
5597 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
5598 * see comment for key_getcomb_ipcomp().
5602 * others: error number
5605 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
5607 struct mbuf *result = NULL, *m;
5608 struct secacq *newacq;
5613 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
5614 satype = key_proto2satype(saidx->proto);
5615 IPSEC_ASSERT(satype != 0, ("null satype, protocol %u", saidx->proto));
5618 * We never do anything about acquirng SA. There is anather
5619 * solution that kernel blocks to send SADB_ACQUIRE message until
5620 * getting something message from IKEd. In later case, to be
5621 * managed with ACQUIRING list.
5623 /* Get an entry to check whether sending message or not. */
5624 if ((newacq = key_getacq(saidx)) != NULL) {
5625 if (key_blockacq_count < newacq->count) {
5626 /* reset counter and do send message. */
5629 /* increment counter and do nothing. */
5634 /* make new entry for blocking to send SADB_ACQUIRE. */
5635 if ((newacq = key_newacq(saidx)) == NULL)
5641 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
5648 /* set sadb_address for saidx's. */
5649 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
5650 &saidx->src.sa, FULLMASK, IPSEC_ULPROTO_ANY);
5657 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
5658 &saidx->dst.sa, FULLMASK, IPSEC_ULPROTO_ANY);
5665 /* XXX proxy address (optional) */
5667 /* set sadb_x_policy */
5669 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
5677 /* XXX identity (optional) */
5679 if (idexttype && fqdn) {
5680 /* create identity extension (FQDN) */
5681 struct sadb_ident *id;
5684 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
5685 id = (struct sadb_ident *)p;
5686 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5687 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
5688 id->sadb_ident_exttype = idexttype;
5689 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
5690 bcopy(fqdn, id + 1, fqdnlen);
5691 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
5695 /* create identity extension (USERFQDN) */
5696 struct sadb_ident *id;
5700 /* +1 for terminating-NUL */
5701 userfqdnlen = strlen(userfqdn) + 1;
5704 id = (struct sadb_ident *)p;
5705 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5706 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
5707 id->sadb_ident_exttype = idexttype;
5708 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
5709 /* XXX is it correct? */
5710 if (curproc && curproc->p_cred)
5711 id->sadb_ident_id = curproc->p_cred->p_ruid;
5712 if (userfqdn && userfqdnlen)
5713 bcopy(userfqdn, id + 1, userfqdnlen);
5714 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
5718 /* XXX sensitivity (optional) */
5720 /* create proposal/combination extension */
5721 m = key_getprop(saidx);
5724 * spec conformant: always attach proposal/combination extension,
5725 * the problem is that we have no way to attach it for ipcomp,
5726 * due to the way sadb_comb is declared in RFC2367.
5735 * outside of spec; make proposal/combination extension optional.
5741 if ((result->m_flags & M_PKTHDR) == 0) {
5746 if (result->m_len < sizeof(struct sadb_msg)) {
5747 result = m_pullup(result, sizeof(struct sadb_msg));
5748 if (result == NULL) {
5754 result->m_pkthdr.len = 0;
5755 for (m = result; m; m = m->m_next)
5756 result->m_pkthdr.len += m->m_len;
5758 mtod(result, struct sadb_msg *)->sadb_msg_len =
5759 PFKEY_UNIT64(result->m_pkthdr.len);
5761 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
5769 static struct secacq *
5770 key_newacq(const struct secasindex *saidx)
5772 struct secacq *newacq;
5775 newacq = malloc(sizeof(struct secacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
5776 if (newacq == NULL) {
5777 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5782 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
5783 newacq->seq = (acq_seq == ~0 ? 1 : ++acq_seq);
5784 newacq->created = time_second;
5787 /* add to acqtree */
5789 LIST_INSERT_HEAD(&acqtree, newacq, chain);
5795 static struct secacq *
5796 key_getacq(const struct secasindex *saidx)
5801 LIST_FOREACH(acq, &acqtree, chain) {
5802 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
5810 static struct secacq *
5811 key_getacqbyseq(seq)
5817 LIST_FOREACH(acq, &acqtree, chain) {
5818 if (acq->seq == seq)
5826 static struct secspacq *
5828 struct secpolicyindex *spidx;
5830 struct secspacq *acq;
5833 acq = malloc(sizeof(struct secspacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
5835 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5840 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
5841 acq->created = time_second;
5844 /* add to spacqtree */
5846 LIST_INSERT_HEAD(&spacqtree, acq, chain);
5852 static struct secspacq *
5854 struct secpolicyindex *spidx;
5856 struct secspacq *acq;
5859 LIST_FOREACH(acq, &spacqtree, chain) {
5860 if (key_cmpspidx_exactly(spidx, &acq->spidx)) {
5861 /* NB: return holding spacq_lock */
5871 * SADB_ACQUIRE processing,
5872 * in first situation, is receiving
5874 * from the ikmpd, and clear sequence of its secasvar entry.
5876 * In second situation, is receiving
5877 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5878 * from a user land process, and return
5879 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
5882 * m will always be freed.
5885 key_acquire2(so, m, mhp)
5888 const struct sadb_msghdr *mhp;
5890 const struct sadb_address *src0, *dst0;
5891 struct secasindex saidx;
5892 struct secashead *sah;
5896 IPSEC_ASSERT(so != NULL, ("null socket"));
5897 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5898 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5899 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5902 * Error message from KMd.
5903 * We assume that if error was occured in IKEd, the length of PFKEY
5904 * message is equal to the size of sadb_msg structure.
5905 * We do not raise error even if error occured in this function.
5907 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
5910 /* check sequence number */
5911 if (mhp->msg->sadb_msg_seq == 0) {
5912 ipseclog((LOG_DEBUG, "%s: must specify sequence "
5913 "number.\n", __func__));
5918 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
5920 * the specified larval SA is already gone, or we got
5921 * a bogus sequence number. we can silently ignore it.
5927 /* reset acq counter in order to deletion by timehander. */
5928 acq->created = time_second;
5935 * This message is from user land.
5938 /* map satype to proto */
5939 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5940 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5942 return key_senderror(so, m, EINVAL);
5945 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5946 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5947 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
5949 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5951 return key_senderror(so, m, EINVAL);
5953 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5954 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
5955 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
5957 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5959 return key_senderror(so, m, EINVAL);
5962 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5963 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5965 /* XXX boundary check against sa_len */
5966 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5968 /* get a SA index */
5970 LIST_FOREACH(sah, &sahtree, chain) {
5971 if (sah->state == SADB_SASTATE_DEAD)
5973 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
5978 ipseclog((LOG_DEBUG, "%s: a SA exists already.\n", __func__));
5979 return key_senderror(so, m, EEXIST);
5982 error = key_acquire(&saidx, NULL);
5984 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
5985 __func__, mhp->msg->sadb_msg_errno));
5986 return key_senderror(so, m, error);
5989 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
5993 * SADB_REGISTER processing.
5994 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
5997 * from the ikmpd, and register a socket to send PF_KEY messages,
6001 * If socket is detached, must free from regnode.
6003 * m will always be freed.
6006 key_register(so, m, mhp)
6009 const struct sadb_msghdr *mhp;
6011 struct secreg *reg, *newreg = 0;
6013 IPSEC_ASSERT(so != NULL, ("null socket"));
6014 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6015 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6016 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6018 /* check for invalid register message */
6019 if (mhp->msg->sadb_msg_satype >= sizeof(regtree)/sizeof(regtree[0]))
6020 return key_senderror(so, m, EINVAL);
6022 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6023 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6026 /* check whether existing or not */
6028 LIST_FOREACH(reg, ®tree[mhp->msg->sadb_msg_satype], chain) {
6029 if (reg->so == so) {
6031 ipseclog((LOG_DEBUG, "%s: socket exists already.\n",
6033 return key_senderror(so, m, EEXIST);
6037 /* create regnode */
6038 newreg = malloc(sizeof(struct secreg), M_IPSEC_SAR, M_NOWAIT|M_ZERO);
6039 if (newreg == NULL) {
6041 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6042 return key_senderror(so, m, ENOBUFS);
6046 ((struct keycb *)sotorawcb(so))->kp_registered++;
6048 /* add regnode to regtree. */
6049 LIST_INSERT_HEAD(®tree[mhp->msg->sadb_msg_satype], newreg, chain);
6055 struct sadb_msg *newmsg;
6056 struct sadb_supported *sup;
6057 u_int len, alen, elen;
6060 struct sadb_alg *alg;
6062 /* create new sadb_msg to reply. */
6064 for (i = 1; i <= SADB_AALG_MAX; i++) {
6065 if (ah_algorithm_lookup(i))
6066 alen += sizeof(struct sadb_alg);
6069 alen += sizeof(struct sadb_supported);
6071 for (i = 1; i <= SADB_EALG_MAX; i++) {
6072 if (esp_algorithm_lookup(i))
6073 elen += sizeof(struct sadb_alg);
6076 elen += sizeof(struct sadb_supported);
6078 len = sizeof(struct sadb_msg) + alen + elen;
6081 return key_senderror(so, m, ENOBUFS);
6083 MGETHDR(n, M_DONTWAIT, MT_DATA);
6085 MCLGET(n, M_DONTWAIT);
6086 if ((n->m_flags & M_EXT) == 0) {
6092 return key_senderror(so, m, ENOBUFS);
6094 n->m_pkthdr.len = n->m_len = len;
6098 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6099 newmsg = mtod(n, struct sadb_msg *);
6100 newmsg->sadb_msg_errno = 0;
6101 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6102 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6104 /* for authentication algorithm */
6106 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6107 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6108 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6109 off += PFKEY_ALIGN8(sizeof(*sup));
6111 for (i = 1; i <= SADB_AALG_MAX; i++) {
6112 struct auth_hash *aalgo;
6113 u_int16_t minkeysize, maxkeysize;
6115 aalgo = ah_algorithm_lookup(i);
6118 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6119 alg->sadb_alg_id = i;
6120 alg->sadb_alg_ivlen = 0;
6121 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
6122 alg->sadb_alg_minbits = _BITS(minkeysize);
6123 alg->sadb_alg_maxbits = _BITS(maxkeysize);
6124 off += PFKEY_ALIGN8(sizeof(*alg));
6128 /* for encryption algorithm */
6130 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6131 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6132 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6133 off += PFKEY_ALIGN8(sizeof(*sup));
6135 for (i = 1; i <= SADB_EALG_MAX; i++) {
6136 struct enc_xform *ealgo;
6138 ealgo = esp_algorithm_lookup(i);
6141 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6142 alg->sadb_alg_id = i;
6143 alg->sadb_alg_ivlen = ealgo->blocksize;
6144 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
6145 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
6146 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6150 IPSEC_ASSERT(off == len,
6151 ("length assumption failed (off %u len %u)", off, len));
6154 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6159 * free secreg entry registered.
6160 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6163 key_freereg(struct socket *so)
6168 IPSEC_ASSERT(so != NULL, ("NULL so"));
6171 * check whether existing or not.
6172 * check all type of SA, because there is a potential that
6173 * one socket is registered to multiple type of SA.
6176 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6177 LIST_FOREACH(reg, ®tree[i], chain) {
6178 if (reg->so == so && __LIST_CHAINED(reg)) {
6179 LIST_REMOVE(reg, chain);
6180 free(reg, M_IPSEC_SAR);
6189 * SADB_EXPIRE processing
6191 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6193 * NOTE: We send only soft lifetime extension.
6196 * others : error number
6199 key_expire(struct secasvar *sav)
6203 struct mbuf *result = NULL, *m;
6206 struct sadb_lifetime *lt;
6208 /* XXX: Why do we lock ? */
6209 s = splnet(); /*called from softclock()*/
6211 IPSEC_ASSERT (sav != NULL, ("null sav"));
6212 IPSEC_ASSERT (sav->sah != NULL, ("null sa header"));
6214 /* set msg header */
6215 satype = key_proto2satype(sav->sah->saidx.proto);
6216 IPSEC_ASSERT(satype != 0, ("invalid proto, satype %u", satype));
6217 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6224 /* create SA extension */
6225 m = key_setsadbsa(sav);
6232 /* create SA extension */
6233 m = key_setsadbxsa2(sav->sah->saidx.mode,
6234 sav->replay ? sav->replay->count : 0,
6235 sav->sah->saidx.reqid);
6242 /* create lifetime extension (current and soft) */
6243 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6244 m = key_alloc_mbuf(len);
6245 if (!m || m->m_next) { /*XXX*/
6251 bzero(mtod(m, caddr_t), len);
6252 lt = mtod(m, struct sadb_lifetime *);
6253 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6254 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6255 lt->sadb_lifetime_allocations = sav->lft_c->sadb_lifetime_allocations;
6256 lt->sadb_lifetime_bytes = sav->lft_c->sadb_lifetime_bytes;
6257 lt->sadb_lifetime_addtime = sav->lft_c->sadb_lifetime_addtime;
6258 lt->sadb_lifetime_usetime = sav->lft_c->sadb_lifetime_usetime;
6259 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6260 bcopy(sav->lft_s, lt, sizeof(*lt));
6263 /* set sadb_address for source */
6264 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6265 &sav->sah->saidx.src.sa,
6266 FULLMASK, IPSEC_ULPROTO_ANY);
6273 /* set sadb_address for destination */
6274 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6275 &sav->sah->saidx.dst.sa,
6276 FULLMASK, IPSEC_ULPROTO_ANY);
6283 if ((result->m_flags & M_PKTHDR) == 0) {
6288 if (result->m_len < sizeof(struct sadb_msg)) {
6289 result = m_pullup(result, sizeof(struct sadb_msg));
6290 if (result == NULL) {
6296 result->m_pkthdr.len = 0;
6297 for (m = result; m; m = m->m_next)
6298 result->m_pkthdr.len += m->m_len;
6300 mtod(result, struct sadb_msg *)->sadb_msg_len =
6301 PFKEY_UNIT64(result->m_pkthdr.len);
6304 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6314 * SADB_FLUSH processing
6317 * from the ikmpd, and free all entries in secastree.
6321 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6323 * m will always be freed.
6326 key_flush(so, m, mhp)
6329 const struct sadb_msghdr *mhp;
6331 struct sadb_msg *newmsg;
6332 struct secashead *sah, *nextsah;
6333 struct secasvar *sav, *nextsav;
6338 IPSEC_ASSERT(so != NULL, ("null socket"));
6339 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6340 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6342 /* map satype to proto */
6343 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6344 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6346 return key_senderror(so, m, EINVAL);
6349 /* no SATYPE specified, i.e. flushing all SA. */
6351 for (sah = LIST_FIRST(&sahtree);
6354 nextsah = LIST_NEXT(sah, chain);
6356 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6357 && proto != sah->saidx.proto)
6361 stateidx < _ARRAYLEN(saorder_state_alive);
6363 state = saorder_state_any[stateidx];
6364 for (sav = LIST_FIRST(&sah->savtree[state]);
6368 nextsav = LIST_NEXT(sav, chain);
6370 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
6375 sah->state = SADB_SASTATE_DEAD;
6379 if (m->m_len < sizeof(struct sadb_msg) ||
6380 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
6381 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6382 return key_senderror(so, m, ENOBUFS);
6388 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
6389 newmsg = mtod(m, struct sadb_msg *);
6390 newmsg->sadb_msg_errno = 0;
6391 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
6393 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6397 * SADB_DUMP processing
6398 * dump all entries including status of DEAD in SAD.
6401 * from the ikmpd, and dump all secasvar leaves
6406 * m will always be freed.
6409 key_dump(so, m, mhp)
6412 const struct sadb_msghdr *mhp;
6414 struct secashead *sah;
6415 struct secasvar *sav;
6421 struct sadb_msg *newmsg;
6424 IPSEC_ASSERT(so != NULL, ("null socket"));
6425 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6426 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6427 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6429 /* map satype to proto */
6430 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6431 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6433 return key_senderror(so, m, EINVAL);
6436 /* count sav entries to be sent to the userland. */
6439 LIST_FOREACH(sah, &sahtree, chain) {
6440 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6441 && proto != sah->saidx.proto)
6445 stateidx < _ARRAYLEN(saorder_state_any);
6447 state = saorder_state_any[stateidx];
6448 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6456 return key_senderror(so, m, ENOENT);
6459 /* send this to the userland, one at a time. */
6461 LIST_FOREACH(sah, &sahtree, chain) {
6462 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6463 && proto != sah->saidx.proto)
6466 /* map proto to satype */
6467 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
6469 ipseclog((LOG_DEBUG, "%s: there was invalid proto in "
6470 "SAD.\n", __func__));
6471 return key_senderror(so, m, EINVAL);
6475 stateidx < _ARRAYLEN(saorder_state_any);
6477 state = saorder_state_any[stateidx];
6478 LIST_FOREACH(sav, &sah->savtree[state], chain) {
6479 n = key_setdumpsa(sav, SADB_DUMP, satype,
6480 --cnt, mhp->msg->sadb_msg_pid);
6483 return key_senderror(so, m, ENOBUFS);
6485 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
6496 * SADB_X_PROMISC processing
6498 * m will always be freed.
6501 key_promisc(so, m, mhp)
6504 const struct sadb_msghdr *mhp;
6508 IPSEC_ASSERT(so != NULL, ("null socket"));
6509 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6510 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6511 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6513 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6515 if (olen < sizeof(struct sadb_msg)) {
6517 return key_senderror(so, m, EINVAL);
6522 } else if (olen == sizeof(struct sadb_msg)) {
6523 /* enable/disable promisc mode */
6526 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
6527 return key_senderror(so, m, EINVAL);
6528 mhp->msg->sadb_msg_errno = 0;
6529 switch (mhp->msg->sadb_msg_satype) {
6532 kp->kp_promisc = mhp->msg->sadb_msg_satype;
6535 return key_senderror(so, m, EINVAL);
6538 /* send the original message back to everyone */
6539 mhp->msg->sadb_msg_errno = 0;
6540 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6542 /* send packet as is */
6544 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
6546 /* TODO: if sadb_msg_seq is specified, send to specific pid */
6547 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
6551 static int (*key_typesw[]) __P((struct socket *, struct mbuf *,
6552 const struct sadb_msghdr *)) = {
6553 NULL, /* SADB_RESERVED */
6554 key_getspi, /* SADB_GETSPI */
6555 key_update, /* SADB_UPDATE */
6556 key_add, /* SADB_ADD */
6557 key_delete, /* SADB_DELETE */
6558 key_get, /* SADB_GET */
6559 key_acquire2, /* SADB_ACQUIRE */
6560 key_register, /* SADB_REGISTER */
6561 NULL, /* SADB_EXPIRE */
6562 key_flush, /* SADB_FLUSH */
6563 key_dump, /* SADB_DUMP */
6564 key_promisc, /* SADB_X_PROMISC */
6565 NULL, /* SADB_X_PCHANGE */
6566 key_spdadd, /* SADB_X_SPDUPDATE */
6567 key_spdadd, /* SADB_X_SPDADD */
6568 key_spddelete, /* SADB_X_SPDDELETE */
6569 key_spdget, /* SADB_X_SPDGET */
6570 NULL, /* SADB_X_SPDACQUIRE */
6571 key_spddump, /* SADB_X_SPDDUMP */
6572 key_spdflush, /* SADB_X_SPDFLUSH */
6573 key_spdadd, /* SADB_X_SPDSETIDX */
6574 NULL, /* SADB_X_SPDEXPIRE */
6575 key_spddelete2, /* SADB_X_SPDDELETE2 */
6579 * parse sadb_msg buffer to process PFKEYv2,
6580 * and create a data to response if needed.
6581 * I think to be dealed with mbuf directly.
6583 * msgp : pointer to pointer to a received buffer pulluped.
6584 * This is rewrited to response.
6585 * so : pointer to socket.
6587 * length for buffer to send to user process.
6594 struct sadb_msg *msg;
6595 struct sadb_msghdr mh;
6600 IPSEC_ASSERT(so != NULL, ("null socket"));
6601 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6603 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
6604 KEYDEBUG(KEYDEBUG_KEY_DUMP,
6605 ipseclog((LOG_DEBUG, "%s: passed sadb_msg\n", __func__));
6609 if (m->m_len < sizeof(struct sadb_msg)) {
6610 m = m_pullup(m, sizeof(struct sadb_msg));
6614 msg = mtod(m, struct sadb_msg *);
6615 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
6616 target = KEY_SENDUP_ONE;
6618 if ((m->m_flags & M_PKTHDR) == 0 ||
6619 m->m_pkthdr.len != m->m_pkthdr.len) {
6620 ipseclog((LOG_DEBUG, "%s: invalid message length.\n",__func__));
6621 pfkeystat.out_invlen++;
6626 if (msg->sadb_msg_version != PF_KEY_V2) {
6627 ipseclog((LOG_DEBUG, "%s: PF_KEY version %u is mismatched.\n",
6628 __func__, msg->sadb_msg_version));
6629 pfkeystat.out_invver++;
6634 if (msg->sadb_msg_type > SADB_MAX) {
6635 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
6636 __func__, msg->sadb_msg_type));
6637 pfkeystat.out_invmsgtype++;
6642 /* for old-fashioned code - should be nuked */
6643 if (m->m_pkthdr.len > MCLBYTES) {
6650 MGETHDR(n, M_DONTWAIT, MT_DATA);
6651 if (n && m->m_pkthdr.len > MHLEN) {
6652 MCLGET(n, M_DONTWAIT);
6653 if ((n->m_flags & M_EXT) == 0) {
6662 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
6663 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
6669 /* align the mbuf chain so that extensions are in contiguous region. */
6670 error = key_align(m, &mh);
6674 if (m->m_next) { /*XXX*/
6682 switch (msg->sadb_msg_satype) {
6683 case SADB_SATYPE_UNSPEC:
6684 switch (msg->sadb_msg_type) {
6692 ipseclog((LOG_DEBUG, "%s: must specify satype "
6693 "when msg type=%u.\n", __func__,
6694 msg->sadb_msg_type));
6695 pfkeystat.out_invsatype++;
6700 case SADB_SATYPE_AH:
6701 case SADB_SATYPE_ESP:
6702 case SADB_X_SATYPE_IPCOMP:
6703 case SADB_X_SATYPE_TCPSIGNATURE:
6704 switch (msg->sadb_msg_type) {
6706 case SADB_X_SPDDELETE:
6708 case SADB_X_SPDDUMP:
6709 case SADB_X_SPDFLUSH:
6710 case SADB_X_SPDSETIDX:
6711 case SADB_X_SPDUPDATE:
6712 case SADB_X_SPDDELETE2:
6713 ipseclog((LOG_DEBUG, "%s: illegal satype=%u\n",
6714 __func__, msg->sadb_msg_type));
6715 pfkeystat.out_invsatype++;
6720 case SADB_SATYPE_RSVP:
6721 case SADB_SATYPE_OSPFV2:
6722 case SADB_SATYPE_RIPV2:
6723 case SADB_SATYPE_MIP:
6724 ipseclog((LOG_DEBUG, "%s: type %u isn't supported.\n",
6725 __func__, msg->sadb_msg_satype));
6726 pfkeystat.out_invsatype++;
6729 case 1: /* XXX: What does it do? */
6730 if (msg->sadb_msg_type == SADB_X_PROMISC)
6734 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
6735 __func__, msg->sadb_msg_satype));
6736 pfkeystat.out_invsatype++;
6741 /* check field of upper layer protocol and address family */
6742 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
6743 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
6744 struct sadb_address *src0, *dst0;
6747 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
6748 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
6750 /* check upper layer protocol */
6751 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
6752 ipseclog((LOG_DEBUG, "%s: upper layer protocol "
6753 "mismatched.\n", __func__));
6754 pfkeystat.out_invaddr++;
6760 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
6761 PFKEY_ADDR_SADDR(dst0)->sa_family) {
6762 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
6764 pfkeystat.out_invaddr++;
6768 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6769 PFKEY_ADDR_SADDR(dst0)->sa_len) {
6770 ipseclog((LOG_DEBUG, "%s: address struct size "
6771 "mismatched.\n", __func__));
6772 pfkeystat.out_invaddr++;
6777 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6779 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6780 sizeof(struct sockaddr_in)) {
6781 pfkeystat.out_invaddr++;
6787 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
6788 sizeof(struct sockaddr_in6)) {
6789 pfkeystat.out_invaddr++;
6795 ipseclog((LOG_DEBUG, "%s: unsupported address family\n",
6797 pfkeystat.out_invaddr++;
6798 error = EAFNOSUPPORT;
6802 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
6804 plen = sizeof(struct in_addr) << 3;
6807 plen = sizeof(struct in6_addr) << 3;
6810 plen = 0; /*fool gcc*/
6814 /* check max prefix length */
6815 if (src0->sadb_address_prefixlen > plen ||
6816 dst0->sadb_address_prefixlen > plen) {
6817 ipseclog((LOG_DEBUG, "%s: illegal prefixlen.\n",
6819 pfkeystat.out_invaddr++;
6825 * prefixlen == 0 is valid because there can be a case when
6826 * all addresses are matched.
6830 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
6831 key_typesw[msg->sadb_msg_type] == NULL) {
6832 pfkeystat.out_invmsgtype++;
6837 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
6840 msg->sadb_msg_errno = error;
6841 return key_sendup_mbuf(so, m, target);
6845 key_senderror(so, m, code)
6850 struct sadb_msg *msg;
6852 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
6853 ("mbuf too small, len %u", m->m_len));
6855 msg = mtod(m, struct sadb_msg *);
6856 msg->sadb_msg_errno = code;
6857 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
6861 * set the pointer to each header into message buffer.
6862 * m will be freed on error.
6863 * XXX larger-than-MCLBYTES extension?
6868 struct sadb_msghdr *mhp;
6871 struct sadb_ext *ext;
6876 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6877 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6878 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
6879 ("mbuf too small, len %u", m->m_len));
6882 bzero(mhp, sizeof(*mhp));
6884 mhp->msg = mtod(m, struct sadb_msg *);
6885 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
6887 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
6888 extlen = end; /*just in case extlen is not updated*/
6889 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
6890 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
6892 /* m is already freed */
6895 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
6898 switch (ext->sadb_ext_type) {
6900 case SADB_EXT_ADDRESS_SRC:
6901 case SADB_EXT_ADDRESS_DST:
6902 case SADB_EXT_ADDRESS_PROXY:
6903 case SADB_EXT_LIFETIME_CURRENT:
6904 case SADB_EXT_LIFETIME_HARD:
6905 case SADB_EXT_LIFETIME_SOFT:
6906 case SADB_EXT_KEY_AUTH:
6907 case SADB_EXT_KEY_ENCRYPT:
6908 case SADB_EXT_IDENTITY_SRC:
6909 case SADB_EXT_IDENTITY_DST:
6910 case SADB_EXT_SENSITIVITY:
6911 case SADB_EXT_PROPOSAL:
6912 case SADB_EXT_SUPPORTED_AUTH:
6913 case SADB_EXT_SUPPORTED_ENCRYPT:
6914 case SADB_EXT_SPIRANGE:
6915 case SADB_X_EXT_POLICY:
6916 case SADB_X_EXT_SA2:
6917 /* duplicate check */
6919 * XXX Are there duplication payloads of either
6920 * KEY_AUTH or KEY_ENCRYPT ?
6922 if (mhp->ext[ext->sadb_ext_type] != NULL) {
6923 ipseclog((LOG_DEBUG, "%s: duplicate ext_type "
6924 "%u\n", __func__, ext->sadb_ext_type));
6926 pfkeystat.out_dupext++;
6931 ipseclog((LOG_DEBUG, "%s: invalid ext_type %u\n",
6932 __func__, ext->sadb_ext_type));
6934 pfkeystat.out_invexttype++;
6938 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
6940 if (key_validate_ext(ext, extlen)) {
6942 pfkeystat.out_invlen++;
6946 n = m_pulldown(m, off, extlen, &toff);
6948 /* m is already freed */
6951 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
6953 mhp->ext[ext->sadb_ext_type] = ext;
6954 mhp->extoff[ext->sadb_ext_type] = off;
6955 mhp->extlen[ext->sadb_ext_type] = extlen;
6960 pfkeystat.out_invlen++;
6968 key_validate_ext(ext, len)
6969 const struct sadb_ext *ext;
6972 const struct sockaddr *sa;
6973 enum { NONE, ADDR } checktype = NONE;
6975 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
6977 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
6980 /* if it does not match minimum/maximum length, bail */
6981 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
6982 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
6984 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
6986 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
6989 /* more checks based on sadb_ext_type XXX need more */
6990 switch (ext->sadb_ext_type) {
6991 case SADB_EXT_ADDRESS_SRC:
6992 case SADB_EXT_ADDRESS_DST:
6993 case SADB_EXT_ADDRESS_PROXY:
6994 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
6997 case SADB_EXT_IDENTITY_SRC:
6998 case SADB_EXT_IDENTITY_DST:
6999 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7000 SADB_X_IDENTTYPE_ADDR) {
7001 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7011 switch (checktype) {
7015 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
7016 if (len < baselen + sal)
7018 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7032 REGTREE_LOCK_INIT();
7033 SAHTREE_LOCK_INIT();
7037 for (i = 0; i < IPSEC_DIR_MAX; i++)
7038 LIST_INIT(&sptree[i]);
7040 LIST_INIT(&sahtree);
7042 for (i = 0; i <= SADB_SATYPE_MAX; i++)
7043 LIST_INIT(®tree[i]);
7045 LIST_INIT(&acqtree);
7046 LIST_INIT(&spacqtree);
7048 /* system default */
7049 ip4_def_policy.policy = IPSEC_POLICY_NONE;
7050 ip4_def_policy.refcnt++; /*never reclaim this*/
7052 #ifndef IPSEC_DEBUG2
7053 timeout((void *)key_timehandler, (void *)0, hz);
7054 #endif /*IPSEC_DEBUG2*/
7056 /* initialize key statistics */
7057 keystat.getspi_count = 1;
7059 printf("Fast IPsec: Initialized Security Association Processing.\n");
7065 * XXX: maybe This function is called after INBOUND IPsec processing.
7067 * Special check for tunnel-mode packets.
7068 * We must make some checks for consistency between inner and outer IP header.
7070 * xxx more checks to be provided
7073 key_checktunnelsanity(sav, family, src, dst)
7074 struct secasvar *sav;
7079 IPSEC_ASSERT(sav->sah != NULL, ("null SA header"));
7081 /* XXX: check inner IP header */
7086 /* record data transfer on SA, and update timestamps */
7088 key_sa_recordxfer(sav, m)
7089 struct secasvar *sav;
7092 IPSEC_ASSERT(sav != NULL, ("Null secasvar"));
7093 IPSEC_ASSERT(m != NULL, ("Null mbuf"));
7098 * XXX Currently, there is a difference of bytes size
7099 * between inbound and outbound processing.
7101 sav->lft_c->sadb_lifetime_bytes += m->m_pkthdr.len;
7102 /* to check bytes lifetime is done in key_timehandler(). */
7105 * We use the number of packets as the unit of
7106 * sadb_lifetime_allocations. We increment the variable
7107 * whenever {esp,ah}_{in,out}put is called.
7109 sav->lft_c->sadb_lifetime_allocations++;
7110 /* XXX check for expires? */
7113 * NOTE: We record CURRENT sadb_lifetime_usetime by using wall clock,
7114 * in seconds. HARD and SOFT lifetime are measured by the time
7115 * difference (again in seconds) from sadb_lifetime_usetime.
7119 * -----+-----+--------+---> t
7120 * <--------------> HARD
7123 sav->lft_c->sadb_lifetime_usetime = time_second;
7124 /* XXX check for expires? */
7131 key_sa_routechange(dst)
7132 struct sockaddr *dst;
7134 struct secashead *sah;
7138 LIST_FOREACH(sah, &sahtree, chain) {
7139 ro = &sah->sa_route;
7140 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7141 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7143 ro->ro_rt = (struct rtentry *)NULL;
7150 key_sa_chgstate(sav, state)
7151 struct secasvar *sav;
7154 IPSEC_ASSERT(sav != NULL, ("NULL sav"));
7155 SAHTREE_LOCK_ASSERT();
7157 if (sav->state != state) {
7158 if (__LIST_CHAINED(sav))
7159 LIST_REMOVE(sav, chain);
7161 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7167 struct secasvar *sav;
7170 IPSEC_ASSERT(sav->iv != NULL, ("null IV"));
7171 key_randomfill(sav->iv, sav->ivlen);
7175 static struct mbuf *
7179 struct mbuf *m = NULL, *n;
7184 MGET(n, M_DONTWAIT, MT_DATA);
7185 if (n && len > MLEN)
7186 MCLGET(n, M_DONTWAIT);
7194 n->m_len = M_TRAILINGSPACE(n);
7195 /* use the bottom of mbuf, hoping we can prepend afterwards */
7196 if (n->m_len > len) {
7197 t = (n->m_len - len) & ~(sizeof(long) - 1);