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/rmlock.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/errno.h>
57 #include <sys/queue.h>
58 #include <sys/refcount.h>
59 #include <sys/syslog.h>
62 #include <net/if_var.h>
64 #include <net/raw_cb.h>
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/ip.h>
69 #include <netinet/in_var.h>
72 #include <netinet/ip6.h>
73 #include <netinet6/in6_var.h>
74 #include <netinet6/ip6_var.h>
77 #if defined(INET) || defined(INET6)
78 #include <netinet/in_pcb.h>
81 #include <netinet6/in6_pcb.h>
84 #include <net/pfkeyv2.h>
85 #include <netipsec/keydb.h>
86 #include <netipsec/key.h>
87 #include <netipsec/keysock.h>
88 #include <netipsec/key_debug.h>
90 #include <netipsec/ipsec.h>
92 #include <netipsec/ipsec6.h>
95 #include <netipsec/xform.h>
97 #include <machine/stdarg.h>
100 #include <sys/random.h>
102 #define FULLMASK 0xff
103 #define _BITS(bytes) ((bytes) << 3)
106 * Note on SA reference counting:
107 * - SAs that are not in DEAD state will have (total external reference + 1)
108 * following value in reference count field. they cannot be freed and are
109 * referenced from SA header.
110 * - SAs that are in DEAD state will have (total external reference)
111 * in reference count field. they are ready to be freed. reference from
112 * SA header will be removed in key_delsav(), when the reference count
113 * field hits 0 (= no external reference other than from SA header.
116 VNET_DEFINE(u_int32_t, key_debug_level) = 0;
117 static VNET_DEFINE(u_int, key_spi_trycnt) = 1000;
118 static VNET_DEFINE(u_int32_t, key_spi_minval) = 0x100;
119 static VNET_DEFINE(u_int32_t, key_spi_maxval) = 0x0fffffff; /* XXX */
120 static VNET_DEFINE(u_int32_t, policy_id) = 0;
121 /*interval to initialize randseed,1(m)*/
122 static VNET_DEFINE(u_int, key_int_random) = 60;
123 /* interval to expire acquiring, 30(s)*/
124 static VNET_DEFINE(u_int, key_larval_lifetime) = 30;
125 /* counter for blocking SADB_ACQUIRE.*/
126 static VNET_DEFINE(int, key_blockacq_count) = 10;
127 /* lifetime for blocking SADB_ACQUIRE.*/
128 static VNET_DEFINE(int, key_blockacq_lifetime) = 20;
129 /* preferred old sa rather than new sa.*/
130 static VNET_DEFINE(int, key_preferred_oldsa) = 1;
131 #define V_key_spi_trycnt VNET(key_spi_trycnt)
132 #define V_key_spi_minval VNET(key_spi_minval)
133 #define V_key_spi_maxval VNET(key_spi_maxval)
134 #define V_policy_id VNET(policy_id)
135 #define V_key_int_random VNET(key_int_random)
136 #define V_key_larval_lifetime VNET(key_larval_lifetime)
137 #define V_key_blockacq_count VNET(key_blockacq_count)
138 #define V_key_blockacq_lifetime VNET(key_blockacq_lifetime)
139 #define V_key_preferred_oldsa VNET(key_preferred_oldsa)
141 static VNET_DEFINE(u_int32_t, acq_seq) = 0;
142 #define V_acq_seq VNET(acq_seq)
145 static VNET_DEFINE(TAILQ_HEAD(_sptree, secpolicy), sptree[IPSEC_DIR_MAX]);
146 static struct rmlock sptree_lock;
147 #define V_sptree VNET(sptree)
148 #define SPTREE_LOCK_INIT() rm_init(&sptree_lock, "sptree")
149 #define SPTREE_LOCK_DESTROY() rm_destroy(&sptree_lock)
150 #define SPTREE_RLOCK_TRACKER struct rm_priotracker sptree_tracker
151 #define SPTREE_RLOCK() rm_rlock(&sptree_lock, &sptree_tracker)
152 #define SPTREE_RUNLOCK() rm_runlock(&sptree_lock, &sptree_tracker)
153 #define SPTREE_RLOCK_ASSERT() rm_assert(&sptree_lock, RA_RLOCKED)
154 #define SPTREE_WLOCK() rm_wlock(&sptree_lock)
155 #define SPTREE_WUNLOCK() rm_wunlock(&sptree_lock)
156 #define SPTREE_WLOCK_ASSERT() rm_assert(&sptree_lock, RA_WLOCKED)
157 #define SPTREE_UNLOCK_ASSERT() rm_assert(&sptree_lock, RA_UNLOCKED)
159 static VNET_DEFINE(LIST_HEAD(_sahtree, secashead), sahtree); /* SAD */
160 #define V_sahtree VNET(sahtree)
161 static struct mtx sahtree_lock;
162 #define SAHTREE_LOCK_INIT() \
163 mtx_init(&sahtree_lock, "sahtree", \
164 "fast ipsec security association database", MTX_DEF)
165 #define SAHTREE_LOCK_DESTROY() mtx_destroy(&sahtree_lock)
166 #define SAHTREE_LOCK() mtx_lock(&sahtree_lock)
167 #define SAHTREE_UNLOCK() mtx_unlock(&sahtree_lock)
168 #define SAHTREE_LOCK_ASSERT() mtx_assert(&sahtree_lock, MA_OWNED)
171 static VNET_DEFINE(LIST_HEAD(_regtree, secreg), regtree[SADB_SATYPE_MAX + 1]);
172 #define V_regtree VNET(regtree)
173 static struct mtx regtree_lock;
174 #define REGTREE_LOCK_INIT() \
175 mtx_init(®tree_lock, "regtree", "fast ipsec regtree", MTX_DEF)
176 #define REGTREE_LOCK_DESTROY() mtx_destroy(®tree_lock)
177 #define REGTREE_LOCK() mtx_lock(®tree_lock)
178 #define REGTREE_UNLOCK() mtx_unlock(®tree_lock)
179 #define REGTREE_LOCK_ASSERT() mtx_assert(®tree_lock, MA_OWNED)
181 static VNET_DEFINE(LIST_HEAD(_acqtree, secacq), acqtree); /* acquiring list */
182 #define V_acqtree VNET(acqtree)
183 static struct mtx acq_lock;
184 #define ACQ_LOCK_INIT() \
185 mtx_init(&acq_lock, "acqtree", "fast ipsec acquire list", MTX_DEF)
186 #define ACQ_LOCK_DESTROY() mtx_destroy(&acq_lock)
187 #define ACQ_LOCK() mtx_lock(&acq_lock)
188 #define ACQ_UNLOCK() mtx_unlock(&acq_lock)
189 #define ACQ_LOCK_ASSERT() mtx_assert(&acq_lock, MA_OWNED)
191 /* SP acquiring list */
192 static VNET_DEFINE(LIST_HEAD(_spacqtree, secspacq), spacqtree);
193 #define V_spacqtree VNET(spacqtree)
194 static struct mtx spacq_lock;
195 #define SPACQ_LOCK_INIT() \
196 mtx_init(&spacq_lock, "spacqtree", \
197 "fast ipsec security policy acquire list", MTX_DEF)
198 #define SPACQ_LOCK_DESTROY() mtx_destroy(&spacq_lock)
199 #define SPACQ_LOCK() mtx_lock(&spacq_lock)
200 #define SPACQ_UNLOCK() mtx_unlock(&spacq_lock)
201 #define SPACQ_LOCK_ASSERT() mtx_assert(&spacq_lock, MA_OWNED)
203 /* search order for SAs */
204 static const u_int saorder_state_valid_prefer_old[] = {
205 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
207 static const u_int saorder_state_valid_prefer_new[] = {
208 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
210 static const u_int saorder_state_alive[] = {
212 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
214 static const u_int saorder_state_any[] = {
215 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
216 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
219 static const int minsize[] = {
220 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
221 sizeof(struct sadb_sa), /* SADB_EXT_SA */
222 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
223 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
224 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
225 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
226 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
227 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
228 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
229 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
230 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
231 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
232 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
233 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
234 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
235 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
236 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
237 0, /* SADB_X_EXT_KMPRIVATE */
238 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
239 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
240 sizeof(struct sadb_x_nat_t_type),/* SADB_X_EXT_NAT_T_TYPE */
241 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_SPORT */
242 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_DPORT */
243 sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAI */
244 sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAR */
245 sizeof(struct sadb_x_nat_t_frag),/* SADB_X_EXT_NAT_T_FRAG */
247 static const int maxsize[] = {
248 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
249 sizeof(struct sadb_sa), /* SADB_EXT_SA */
250 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
251 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
252 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
253 0, /* SADB_EXT_ADDRESS_SRC */
254 0, /* SADB_EXT_ADDRESS_DST */
255 0, /* SADB_EXT_ADDRESS_PROXY */
256 0, /* SADB_EXT_KEY_AUTH */
257 0, /* SADB_EXT_KEY_ENCRYPT */
258 0, /* SADB_EXT_IDENTITY_SRC */
259 0, /* SADB_EXT_IDENTITY_DST */
260 0, /* SADB_EXT_SENSITIVITY */
261 0, /* SADB_EXT_PROPOSAL */
262 0, /* SADB_EXT_SUPPORTED_AUTH */
263 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
264 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
265 0, /* SADB_X_EXT_KMPRIVATE */
266 0, /* SADB_X_EXT_POLICY */
267 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
268 sizeof(struct sadb_x_nat_t_type),/* SADB_X_EXT_NAT_T_TYPE */
269 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_SPORT */
270 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_DPORT */
271 0, /* SADB_X_EXT_NAT_T_OAI */
272 0, /* SADB_X_EXT_NAT_T_OAR */
273 sizeof(struct sadb_x_nat_t_frag),/* SADB_X_EXT_NAT_T_FRAG */
276 static VNET_DEFINE(int, ipsec_esp_keymin) = 256;
277 static VNET_DEFINE(int, ipsec_esp_auth) = 0;
278 static VNET_DEFINE(int, ipsec_ah_keymin) = 128;
280 #define V_ipsec_esp_keymin VNET(ipsec_esp_keymin)
281 #define V_ipsec_esp_auth VNET(ipsec_esp_auth)
282 #define V_ipsec_ah_keymin VNET(ipsec_ah_keymin)
285 SYSCTL_DECL(_net_key);
288 SYSCTL_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug,
289 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_debug_level), 0, "");
291 /* max count of trial for the decision of spi value */
292 SYSCTL_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt,
293 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_spi_trycnt), 0, "");
295 /* minimum spi value to allocate automatically. */
296 SYSCTL_INT(_net_key, KEYCTL_SPI_MIN_VALUE, spi_minval,
297 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_spi_minval), 0, "");
299 /* maximun spi value to allocate automatically. */
300 SYSCTL_INT(_net_key, KEYCTL_SPI_MAX_VALUE, spi_maxval,
301 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_spi_maxval), 0, "");
303 /* interval to initialize randseed */
304 SYSCTL_INT(_net_key, KEYCTL_RANDOM_INT, int_random,
305 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_int_random), 0, "");
307 /* lifetime for larval SA */
308 SYSCTL_INT(_net_key, KEYCTL_LARVAL_LIFETIME, larval_lifetime,
309 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_larval_lifetime), 0, "");
311 /* counter for blocking to send SADB_ACQUIRE to IKEd */
312 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_COUNT, blockacq_count,
313 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_blockacq_count), 0, "");
315 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
316 SYSCTL_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME, blockacq_lifetime,
317 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_blockacq_lifetime), 0, "");
320 SYSCTL_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth,
321 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_esp_auth), 0, "");
323 /* minimum ESP key length */
324 SYSCTL_INT(_net_key, KEYCTL_ESP_KEYMIN, esp_keymin,
325 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_esp_keymin), 0, "");
327 /* minimum AH key length */
328 SYSCTL_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin,
329 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_ah_keymin), 0, "");
331 /* perfered old SA rather than new SA */
332 SYSCTL_INT(_net_key, KEYCTL_PREFERED_OLDSA, preferred_oldsa,
333 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(key_preferred_oldsa), 0, "");
335 #define __LIST_CHAINED(elm) \
336 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
337 #define LIST_INSERT_TAIL(head, elm, type, field) \
339 struct type *curelm = LIST_FIRST(head); \
340 if (curelm == NULL) {\
341 LIST_INSERT_HEAD(head, elm, field); \
343 while (LIST_NEXT(curelm, field)) \
344 curelm = LIST_NEXT(curelm, field);\
345 LIST_INSERT_AFTER(curelm, elm, field);\
349 #define KEY_CHKSASTATE(head, sav, name) \
351 if ((head) != (sav)) { \
352 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
353 (name), (head), (sav))); \
358 #define KEY_CHKSPDIR(head, sp, name) \
360 if ((head) != (sp)) { \
361 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
362 "anyway continue.\n", \
363 (name), (head), (sp))); \
367 MALLOC_DEFINE(M_IPSEC_SA, "secasvar", "ipsec security association");
368 MALLOC_DEFINE(M_IPSEC_SAH, "sahead", "ipsec sa head");
369 MALLOC_DEFINE(M_IPSEC_SP, "ipsecpolicy", "ipsec security policy");
370 MALLOC_DEFINE(M_IPSEC_SR, "ipsecrequest", "ipsec security request");
371 MALLOC_DEFINE(M_IPSEC_MISC, "ipsec-misc", "ipsec miscellaneous");
372 MALLOC_DEFINE(M_IPSEC_SAQ, "ipsec-saq", "ipsec sa acquire");
373 MALLOC_DEFINE(M_IPSEC_SAR, "ipsec-reg", "ipsec sa acquire");
376 * set parameters into secpolicyindex buffer.
377 * Must allocate secpolicyindex buffer passed to this function.
379 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
381 bzero((idx), sizeof(struct secpolicyindex)); \
382 (idx)->dir = (_dir); \
383 (idx)->prefs = (ps); \
384 (idx)->prefd = (pd); \
385 (idx)->ul_proto = (ulp); \
386 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
387 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
391 * set parameters into secasindex buffer.
392 * Must allocate secasindex buffer before calling this function.
394 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
396 bzero((idx), sizeof(struct secasindex)); \
397 (idx)->proto = (p); \
399 (idx)->reqid = (r); \
400 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
401 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
406 u_long getspi_count; /* the avarage of count to try to get new SPI */
410 struct sadb_msg *msg;
411 struct sadb_ext *ext[SADB_EXT_MAX + 1];
412 int extoff[SADB_EXT_MAX + 1];
413 int extlen[SADB_EXT_MAX + 1];
417 static struct callout key_timer;
420 static struct secasvar *key_allocsa_policy(const struct secasindex *);
421 static void key_freesp_so(struct secpolicy **);
422 static struct secasvar *key_do_allocsa_policy(struct secashead *, u_int);
423 static void key_unlink(struct secpolicy *);
424 static struct secpolicy *key_getsp(struct secpolicyindex *);
425 static struct secpolicy *key_getspbyid(u_int32_t);
426 static u_int32_t key_newreqid(void);
427 static struct mbuf *key_gather_mbuf(struct mbuf *,
428 const struct sadb_msghdr *, int, int, ...);
429 static int key_spdadd(struct socket *, struct mbuf *,
430 const struct sadb_msghdr *);
431 static u_int32_t key_getnewspid(void);
432 static int key_spddelete(struct socket *, struct mbuf *,
433 const struct sadb_msghdr *);
434 static int key_spddelete2(struct socket *, struct mbuf *,
435 const struct sadb_msghdr *);
436 static int key_spdget(struct socket *, struct mbuf *,
437 const struct sadb_msghdr *);
438 static int key_spdflush(struct socket *, struct mbuf *,
439 const struct sadb_msghdr *);
440 static int key_spddump(struct socket *, struct mbuf *,
441 const struct sadb_msghdr *);
442 static struct mbuf *key_setdumpsp(struct secpolicy *,
443 u_int8_t, u_int32_t, u_int32_t);
444 static u_int key_getspreqmsglen(struct secpolicy *);
445 static int key_spdexpire(struct secpolicy *);
446 static struct secashead *key_newsah(struct secasindex *);
447 static void key_delsah(struct secashead *);
448 static struct secasvar *key_newsav(struct mbuf *,
449 const struct sadb_msghdr *, struct secashead *, int *,
451 #define KEY_NEWSAV(m, sadb, sah, e) \
452 key_newsav(m, sadb, sah, e, __FILE__, __LINE__)
453 static void key_delsav(struct secasvar *);
454 static struct secashead *key_getsah(struct secasindex *);
455 static struct secasvar *key_checkspidup(struct secasindex *, u_int32_t);
456 static struct secasvar *key_getsavbyspi(struct secashead *, u_int32_t);
457 static int key_setsaval(struct secasvar *, struct mbuf *,
458 const struct sadb_msghdr *);
459 static int key_mature(struct secasvar *);
460 static struct mbuf *key_setdumpsa(struct secasvar *, u_int8_t,
461 u_int8_t, u_int32_t, u_int32_t);
462 static struct mbuf *key_setsadbmsg(u_int8_t, u_int16_t, u_int8_t,
463 u_int32_t, pid_t, u_int16_t);
464 static struct mbuf *key_setsadbsa(struct secasvar *);
465 static struct mbuf *key_setsadbaddr(u_int16_t,
466 const struct sockaddr *, u_int8_t, u_int16_t);
468 static struct mbuf *key_setsadbxport(u_int16_t, u_int16_t);
469 static struct mbuf *key_setsadbxtype(u_int16_t);
471 static void key_porttosaddr(struct sockaddr *, u_int16_t);
472 #define KEY_PORTTOSADDR(saddr, port) \
473 key_porttosaddr((struct sockaddr *)(saddr), (port))
474 static struct mbuf *key_setsadbxsa2(u_int8_t, u_int32_t, u_int32_t);
475 static struct mbuf *key_setsadbxpolicy(u_int16_t, u_int8_t,
476 u_int32_t, u_int32_t);
477 static struct seckey *key_dup_keymsg(const struct sadb_key *, u_int,
478 struct malloc_type *);
479 static struct seclifetime *key_dup_lifemsg(const struct sadb_lifetime *src,
480 struct malloc_type *type);
482 static int key_ismyaddr6(struct sockaddr_in6 *);
485 /* flags for key_cmpsaidx() */
486 #define CMP_HEAD 1 /* protocol, addresses. */
487 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
488 #define CMP_REQID 3 /* additionally HEAD, reaid. */
489 #define CMP_EXACTLY 4 /* all elements. */
490 static int key_cmpsaidx(const struct secasindex *,
491 const struct secasindex *, int);
492 static int key_cmpspidx_exactly(struct secpolicyindex *,
493 struct secpolicyindex *);
494 static int key_cmpspidx_withmask(struct secpolicyindex *,
495 struct secpolicyindex *);
496 static int key_sockaddrcmp(const struct sockaddr *,
497 const struct sockaddr *, int);
498 static int key_bbcmp(const void *, const void *, u_int);
499 static u_int16_t key_satype2proto(u_int8_t);
500 static u_int8_t key_proto2satype(u_int16_t);
502 static int key_getspi(struct socket *, struct mbuf *,
503 const struct sadb_msghdr *);
504 static u_int32_t key_do_getnewspi(struct sadb_spirange *,
505 struct secasindex *);
506 static int key_update(struct socket *, struct mbuf *,
507 const struct sadb_msghdr *);
508 #ifdef IPSEC_DOSEQCHECK
509 static struct secasvar *key_getsavbyseq(struct secashead *, u_int32_t);
511 static int key_add(struct socket *, struct mbuf *,
512 const struct sadb_msghdr *);
513 static int key_setident(struct secashead *, struct mbuf *,
514 const struct sadb_msghdr *);
515 static struct mbuf *key_getmsgbuf_x1(struct mbuf *,
516 const struct sadb_msghdr *);
517 static int key_delete(struct socket *, struct mbuf *,
518 const struct sadb_msghdr *);
519 static int key_delete_all(struct socket *, struct mbuf *,
520 const struct sadb_msghdr *, u_int16_t);
521 static int key_get(struct socket *, struct mbuf *,
522 const struct sadb_msghdr *);
524 static void key_getcomb_setlifetime(struct sadb_comb *);
525 static struct mbuf *key_getcomb_esp(void);
526 static struct mbuf *key_getcomb_ah(void);
527 static struct mbuf *key_getcomb_ipcomp(void);
528 static struct mbuf *key_getprop(const struct secasindex *);
530 static int key_acquire(const struct secasindex *, struct secpolicy *);
531 static struct secacq *key_newacq(const struct secasindex *);
532 static struct secacq *key_getacq(const struct secasindex *);
533 static struct secacq *key_getacqbyseq(u_int32_t);
534 static struct secspacq *key_newspacq(struct secpolicyindex *);
535 static struct secspacq *key_getspacq(struct secpolicyindex *);
536 static int key_acquire2(struct socket *, struct mbuf *,
537 const struct sadb_msghdr *);
538 static int key_register(struct socket *, struct mbuf *,
539 const struct sadb_msghdr *);
540 static int key_expire(struct secasvar *, int);
541 static int key_flush(struct socket *, struct mbuf *,
542 const struct sadb_msghdr *);
543 static int key_dump(struct socket *, struct mbuf *,
544 const struct sadb_msghdr *);
545 static int key_promisc(struct socket *, struct mbuf *,
546 const struct sadb_msghdr *);
547 static int key_senderror(struct socket *, struct mbuf *, int);
548 static int key_validate_ext(const struct sadb_ext *, int);
549 static int key_align(struct mbuf *, struct sadb_msghdr *);
550 static struct mbuf *key_setlifetime(struct seclifetime *src,
552 static struct mbuf *key_setkey(struct seckey *src, u_int16_t exttype);
555 static const char *key_getfqdn(void);
556 static const char *key_getuserfqdn(void);
558 static void key_sa_chgstate(struct secasvar *, u_int8_t);
561 sa_initref(struct secasvar *sav)
564 refcount_init(&sav->refcnt, 1);
567 sa_addref(struct secasvar *sav)
570 refcount_acquire(&sav->refcnt);
571 IPSEC_ASSERT(sav->refcnt != 0, ("SA refcnt overflow"));
574 sa_delref(struct secasvar *sav)
577 IPSEC_ASSERT(sav->refcnt > 0, ("SA refcnt underflow"));
578 return (refcount_release(&sav->refcnt));
581 #define SP_ADDREF(p) refcount_acquire(&(p)->refcnt)
582 #define SP_DELREF(p) refcount_release(&(p)->refcnt)
585 * Update the refcnt while holding the SPTREE lock.
588 key_addref(struct secpolicy *sp)
595 * Return 0 when there are known to be no SP's for the specified
596 * direction. Otherwise return 1. This is used by IPsec code
597 * to optimize performance.
600 key_havesp(u_int dir)
603 return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ?
604 TAILQ_FIRST(&V_sptree[dir]) != NULL : 1);
607 /* %%% IPsec policy management */
609 * allocating a SP for OUTBOUND or INBOUND packet.
610 * Must call key_freesp() later.
611 * OUT: NULL: not found
612 * others: found and return the pointer.
615 key_allocsp(struct secpolicyindex *spidx, u_int dir, const char* where,
618 SPTREE_RLOCK_TRACKER;
619 struct secpolicy *sp;
621 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
622 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
623 ("invalid direction %u", dir));
625 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
626 printf("DP %s from %s:%u\n", __func__, where, tag));
629 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
630 printf("*** objects\n");
631 kdebug_secpolicyindex(spidx));
634 TAILQ_FOREACH(sp, &V_sptree[dir], chain) {
635 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
636 printf("*** in SPD\n");
637 kdebug_secpolicyindex(&sp->spidx));
638 if (key_cmpspidx_withmask(&sp->spidx, spidx))
645 KEY_CHKSPDIR(sp->spidx.dir, dir, __func__);
647 /* found a SPD entry */
648 sp->lastused = time_second;
653 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
654 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
655 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
660 * allocating a SP for OUTBOUND or INBOUND packet.
661 * Must call key_freesp() later.
662 * OUT: NULL: not found
663 * others: found and return the pointer.
666 key_allocsp2(u_int32_t spi, union sockaddr_union *dst, u_int8_t proto,
667 u_int dir, const char* where, int tag)
669 SPTREE_RLOCK_TRACKER;
670 struct secpolicy *sp;
672 IPSEC_ASSERT(dst != NULL, ("null dst"));
673 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
674 ("invalid direction %u", dir));
676 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
677 printf("DP %s from %s:%u\n", __func__, where, tag));
680 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
681 printf("*** objects\n");
682 printf("spi %u proto %u dir %u\n", spi, proto, dir);
683 kdebug_sockaddr(&dst->sa));
686 TAILQ_FOREACH(sp, &V_sptree[dir], chain) {
687 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
688 printf("*** in SPD\n");
689 kdebug_secpolicyindex(&sp->spidx));
690 /* compare simple values, then dst address */
691 if (sp->spidx.ul_proto != proto)
693 /* NB: spi's must exist and match */
694 if (!sp->req || !sp->req->sav || sp->req->sav->spi != spi)
696 if (key_sockaddrcmp(&sp->spidx.dst.sa, &dst->sa, 1) == 0)
703 KEY_CHKSPDIR(sp->spidx.dir, dir, __func__);
705 /* found a SPD entry */
706 sp->lastused = time_second;
711 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
712 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
713 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
719 * return a policy that matches this particular inbound packet.
723 key_gettunnel(const struct sockaddr *osrc,
724 const struct sockaddr *odst,
725 const struct sockaddr *isrc,
726 const struct sockaddr *idst,
727 const char* where, int tag)
729 struct secpolicy *sp;
730 const int dir = IPSEC_DIR_INBOUND;
731 struct ipsecrequest *r1, *r2, *p;
732 struct secpolicyindex spidx;
734 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
735 printf("DP %s from %s:%u\n", __func__, where, tag));
737 if (isrc->sa_family != idst->sa_family) {
738 ipseclog((LOG_ERR, "%s: protocol family mismatched %d != %d\n.",
739 __func__, isrc->sa_family, idst->sa_family));
745 LIST_FOREACH(sp, &V_sptree[dir], chain) {
746 if (sp->state == IPSEC_SPSTATE_DEAD)
750 for (p = sp->req; p; p = p->next) {
751 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
758 /* here we look at address matches only */
760 if (isrc->sa_len > sizeof(spidx.src) ||
761 idst->sa_len > sizeof(spidx.dst))
763 bcopy(isrc, &spidx.src, isrc->sa_len);
764 bcopy(idst, &spidx.dst, idst->sa_len);
765 if (!key_cmpspidx_withmask(&sp->spidx, &spidx))
768 if (key_sockaddrcmp(&r1->saidx.src.sa, isrc, 0) ||
769 key_sockaddrcmp(&r1->saidx.dst.sa, idst, 0))
773 if (key_sockaddrcmp(&r2->saidx.src.sa, osrc, 0) ||
774 key_sockaddrcmp(&r2->saidx.dst.sa, odst, 0))
783 sp->lastused = time_second;
788 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
789 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
790 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
796 * allocating an SA entry for an *OUTBOUND* packet.
797 * checking each request entries in SP, and acquire an SA if need.
798 * OUT: 0: there are valid requests.
799 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
802 key_checkrequest(struct ipsecrequest *isr, const struct secasindex *saidx)
806 struct secasvar *sav;
808 IPSEC_ASSERT(isr != NULL, ("null isr"));
809 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
810 IPSEC_ASSERT(saidx->mode == IPSEC_MODE_TRANSPORT ||
811 saidx->mode == IPSEC_MODE_TUNNEL,
812 ("unexpected policy %u", saidx->mode));
815 * XXX guard against protocol callbacks from the crypto
816 * thread as they reference ipsecrequest.sav which we
817 * temporarily null out below. Need to rethink how we
818 * handle bundled SA's in the callback thread.
820 IPSECREQUEST_LOCK_ASSERT(isr);
822 /* get current level */
823 level = ipsec_get_reqlevel(isr);
826 * We check new SA in the IPsec request because a different
827 * SA may be involved each time this request is checked, either
828 * because new SAs are being configured, or this request is
829 * associated with an unconnected datagram socket, or this request
830 * is associated with a system default policy.
832 * key_allocsa_policy should allocate the oldest SA available.
833 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
835 sav = key_allocsa_policy(saidx);
836 if (sav != isr->sav) {
837 /* SA need to be updated. */
838 if (!IPSECREQUEST_UPGRADE(isr)) {
839 /* Kick everyone off. */
840 IPSECREQUEST_UNLOCK(isr);
841 IPSECREQUEST_WLOCK(isr);
843 if (isr->sav != NULL)
844 KEY_FREESAV(&isr->sav);
846 IPSECREQUEST_DOWNGRADE(isr);
847 } else if (sav != NULL)
850 /* When there is SA. */
851 if (isr->sav != NULL) {
852 if (isr->sav->state != SADB_SASTATE_MATURE &&
853 isr->sav->state != SADB_SASTATE_DYING)
859 error = key_acquire(saidx, isr->sp);
861 /* XXX What should I do ? */
862 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
867 if (level != IPSEC_LEVEL_REQUIRE) {
868 /* XXX sigh, the interface to this routine is botched */
869 IPSEC_ASSERT(isr->sav == NULL, ("unexpected SA"));
877 * allocating a SA for policy entry from SAD.
878 * NOTE: searching SAD of aliving state.
879 * OUT: NULL: not found.
880 * others: found and return the pointer.
882 static struct secasvar *
883 key_allocsa_policy(const struct secasindex *saidx)
885 #define N(a) _ARRAYLEN(a)
886 struct secashead *sah;
887 struct secasvar *sav;
888 u_int stateidx, arraysize;
889 const u_int *state_valid;
891 state_valid = NULL; /* silence gcc */
892 arraysize = 0; /* silence gcc */
895 LIST_FOREACH(sah, &V_sahtree, chain) {
896 if (sah->state == SADB_SASTATE_DEAD)
898 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID)) {
899 if (V_key_preferred_oldsa) {
900 state_valid = saorder_state_valid_prefer_old;
901 arraysize = N(saorder_state_valid_prefer_old);
903 state_valid = saorder_state_valid_prefer_new;
904 arraysize = N(saorder_state_valid_prefer_new);
913 /* search valid state */
914 for (stateidx = 0; stateidx < arraysize; stateidx++) {
915 sav = key_do_allocsa_policy(sah, state_valid[stateidx]);
925 * searching SAD with direction, protocol, mode and state.
926 * called by key_allocsa_policy().
929 * others : found, pointer to a SA.
931 static struct secasvar *
932 key_do_allocsa_policy(struct secashead *sah, u_int state)
934 struct secasvar *sav, *nextsav, *candidate, *d;
940 for (sav = LIST_FIRST(&sah->savtree[state]);
944 nextsav = LIST_NEXT(sav, chain);
947 KEY_CHKSASTATE(sav->state, state, __func__);
950 if (candidate == NULL) {
955 /* Which SA is the better ? */
957 IPSEC_ASSERT(candidate->lft_c != NULL,
958 ("null candidate lifetime"));
959 IPSEC_ASSERT(sav->lft_c != NULL, ("null sav lifetime"));
961 /* What the best method is to compare ? */
962 if (V_key_preferred_oldsa) {
963 if (candidate->lft_c->addtime >
964 sav->lft_c->addtime) {
971 /* preferred new sa rather than old sa */
972 if (candidate->lft_c->addtime <
973 sav->lft_c->addtime) {
980 * prepared to delete the SA when there is more
981 * suitable candidate and the lifetime of the SA is not
984 if (d->lft_h->addtime != 0) {
985 struct mbuf *m, *result;
988 key_sa_chgstate(d, SADB_SASTATE_DEAD);
990 IPSEC_ASSERT(d->refcnt > 0, ("bogus ref count"));
992 satype = key_proto2satype(d->sah->saidx.proto);
996 m = key_setsadbmsg(SADB_DELETE, 0,
997 satype, 0, 0, d->refcnt - 1);
1002 /* set sadb_address for saidx's. */
1003 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
1004 &d->sah->saidx.src.sa,
1005 d->sah->saidx.src.sa.sa_len << 3,
1011 /* set sadb_address for saidx's. */
1012 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
1013 &d->sah->saidx.dst.sa,
1014 d->sah->saidx.dst.sa.sa_len << 3,
1020 /* create SA extension */
1021 m = key_setsadbsa(d);
1026 if (result->m_len < sizeof(struct sadb_msg)) {
1027 result = m_pullup(result,
1028 sizeof(struct sadb_msg));
1033 result->m_pkthdr.len = 0;
1034 for (m = result; m; m = m->m_next)
1035 result->m_pkthdr.len += m->m_len;
1036 mtod(result, struct sadb_msg *)->sadb_msg_len =
1037 PFKEY_UNIT64(result->m_pkthdr.len);
1039 if (key_sendup_mbuf(NULL, result,
1040 KEY_SENDUP_REGISTERED))
1047 sa_addref(candidate);
1048 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1049 printf("DP %s cause refcnt++:%d SA:%p\n",
1050 __func__, candidate->refcnt, candidate));
1058 * allocating a usable SA entry for a *INBOUND* packet.
1059 * Must call key_freesav() later.
1060 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
1061 * NULL: not found, or error occurred.
1063 * In the comparison, no source address is used--for RFC2401 conformance.
1064 * To quote, from section 4.1:
1065 * A security association is uniquely identified by a triple consisting
1066 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1067 * security protocol (AH or ESP) identifier.
1068 * Note that, however, we do need to keep source address in IPsec SA.
1069 * IKE specification and PF_KEY specification do assume that we
1070 * keep source address in IPsec SA. We see a tricky situation here.
1073 key_allocsa(union sockaddr_union *dst, u_int proto, u_int32_t spi,
1074 const char* where, int tag)
1076 struct secashead *sah;
1077 struct secasvar *sav;
1078 u_int stateidx, arraysize, state;
1079 const u_int *saorder_state_valid;
1084 IPSEC_ASSERT(dst != NULL, ("null dst address"));
1086 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1087 printf("DP %s from %s:%u\n", __func__, where, tag));
1090 natt_chkport = (dst->sa.sa_family == AF_INET &&
1091 dst->sa.sa_len == sizeof(struct sockaddr_in) &&
1092 dst->sin.sin_port != 0);
1097 * XXX: to be checked internal IP header somewhere. Also when
1098 * IPsec tunnel packet is received. But ESP tunnel mode is
1099 * encrypted so we can't check internal IP header.
1102 if (V_key_preferred_oldsa) {
1103 saorder_state_valid = saorder_state_valid_prefer_old;
1104 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
1106 saorder_state_valid = saorder_state_valid_prefer_new;
1107 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
1109 LIST_FOREACH(sah, &V_sahtree, chain) {
1112 /* search valid state */
1113 for (stateidx = 0; stateidx < arraysize; stateidx++) {
1114 state = saorder_state_valid[stateidx];
1115 LIST_FOREACH(sav, &sah->savtree[state], chain) {
1117 KEY_CHKSASTATE(sav->state, state, __func__);
1118 /* do not return entries w/ unusable state */
1119 if (sav->state != SADB_SASTATE_MATURE &&
1120 sav->state != SADB_SASTATE_DYING)
1122 if (proto != sav->sah->saidx.proto)
1124 if (spi != sav->spi)
1129 * Really only check ports when this is a NAT-T
1130 * SA. Otherwise other lookups providing ports
1133 if (sav->natt_type && natt_chkport)
1136 #if 0 /* don't check src */
1137 /* check src address */
1138 if (key_sockaddrcmp(&src->sa,
1139 &sav->sah->saidx.src.sa, checkport) != 0)
1142 /* check dst address */
1143 if (key_sockaddrcmp(&dst->sa,
1144 &sav->sah->saidx.dst.sa, checkport) != 0)
1155 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1156 printf("DP %s return SA:%p; refcnt %u\n", __func__,
1157 sav, sav ? sav->refcnt : 0));
1162 key_allocsa_tunnel(union sockaddr_union *src, union sockaddr_union *dst,
1163 u_int proto, const char* where, int tag)
1165 struct secashead *sah;
1166 struct secasvar *sav;
1167 u_int stateidx, arraysize, state;
1168 const u_int *saorder_state_valid;
1170 IPSEC_ASSERT(src != NULL, ("null src address"));
1171 IPSEC_ASSERT(dst != NULL, ("null dst address"));
1172 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1173 printf("DP %s from %s:%u\n", __func__, where, tag));
1176 if (V_key_preferred_oldsa) {
1177 saorder_state_valid = saorder_state_valid_prefer_old;
1178 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
1180 saorder_state_valid = saorder_state_valid_prefer_new;
1181 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
1183 LIST_FOREACH(sah, &V_sahtree, chain) {
1184 /* search valid state */
1185 for (stateidx = 0; stateidx < arraysize; stateidx++) {
1186 state = saorder_state_valid[stateidx];
1187 LIST_FOREACH(sav, &sah->savtree[state], chain) {
1189 KEY_CHKSASTATE(sav->state, state, __func__);
1190 /* do not return entries w/ unusable state */
1191 if (sav->state != SADB_SASTATE_MATURE &&
1192 sav->state != SADB_SASTATE_DYING)
1194 if (IPSEC_MODE_TUNNEL != sav->sah->saidx.mode)
1196 if (proto != sav->sah->saidx.proto)
1198 /* check src address */
1199 if (key_sockaddrcmp(&src->sa,
1200 &sav->sah->saidx.src.sa, 0) != 0)
1202 /* check dst address */
1203 if (key_sockaddrcmp(&dst->sa,
1204 &sav->sah->saidx.dst.sa, 0) != 0)
1215 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1216 printf("DP %s return SA:%p; refcnt %u\n", __func__,
1217 sav, sav ? sav->refcnt : 0));
1222 * Must be called after calling key_allocsp().
1223 * For both the packet without socket and key_freeso().
1226 _key_freesp(struct secpolicy **spp, const char* where, int tag)
1228 struct ipsecrequest *isr, *nextisr;
1229 struct secpolicy *sp = *spp;
1231 IPSEC_ASSERT(sp != NULL, ("null sp"));
1232 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1233 printf("DP %s SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1234 __func__, sp, sp->id, where, tag, sp->refcnt));
1236 if (SP_DELREF(sp) == 0)
1239 for (isr = sp->req; isr != NULL; isr = nextisr) {
1240 if (isr->sav != NULL) {
1241 KEY_FREESAV(&isr->sav);
1244 nextisr = isr->next;
1247 free(sp, M_IPSEC_SP);
1251 key_unlink(struct secpolicy *sp)
1254 IPSEC_ASSERT(sp != NULL, ("null sp"));
1255 IPSEC_ASSERT(sp->spidx.dir == IPSEC_DIR_INBOUND ||
1256 sp->spidx.dir == IPSEC_DIR_OUTBOUND,
1257 ("invalid direction %u", sp->spidx.dir));
1258 SPTREE_UNLOCK_ASSERT();
1261 if (sp->state == IPSEC_SPSTATE_DEAD) {
1265 sp->state = IPSEC_SPSTATE_DEAD;
1266 TAILQ_REMOVE(&V_sptree[sp->spidx.dir], sp, chain);
1272 * insert a secpolicy into the SP database. Lower priorities first
1275 key_insertsp(struct secpolicy *newsp)
1277 struct secpolicy *sp;
1280 TAILQ_FOREACH(sp, &V_sptree[newsp->spidx.dir], chain) {
1281 if (newsp->priority < sp->priority) {
1282 TAILQ_INSERT_BEFORE(sp, newsp, chain);
1287 TAILQ_INSERT_TAIL(&V_sptree[newsp->spidx.dir], newsp, chain);
1290 newsp->state = IPSEC_SPSTATE_ALIVE;
1295 * Must be called after calling key_allocsp().
1296 * For the packet with socket.
1299 key_freeso(struct socket *so)
1301 IPSEC_ASSERT(so != NULL, ("null so"));
1303 switch (so->so_proto->pr_domain->dom_family) {
1304 #if defined(INET) || defined(INET6)
1312 struct inpcb *pcb = sotoinpcb(so);
1314 /* Does it have a PCB ? */
1317 key_freesp_so(&pcb->inp_sp->sp_in);
1318 key_freesp_so(&pcb->inp_sp->sp_out);
1321 #endif /* INET || INET6 */
1323 ipseclog((LOG_DEBUG, "%s: unknown address family=%d.\n",
1324 __func__, so->so_proto->pr_domain->dom_family));
1330 key_freesp_so(struct secpolicy **sp)
1332 IPSEC_ASSERT(sp != NULL && *sp != NULL, ("null sp"));
1334 if ((*sp)->policy == IPSEC_POLICY_ENTRUST ||
1335 (*sp)->policy == IPSEC_POLICY_BYPASS)
1338 IPSEC_ASSERT((*sp)->policy == IPSEC_POLICY_IPSEC,
1339 ("invalid policy %u", (*sp)->policy));
1344 key_addrefsa(struct secasvar *sav, const char* where, int tag)
1347 IPSEC_ASSERT(sav != NULL, ("null sav"));
1348 IPSEC_ASSERT(sav->refcnt > 0, ("refcount must exist"));
1354 * Must be called after calling key_allocsa().
1355 * This function is called by key_freesp() to free some SA allocated
1359 key_freesav(struct secasvar **psav, const char* where, int tag)
1361 struct secasvar *sav = *psav;
1363 IPSEC_ASSERT(sav != NULL, ("null sav"));
1365 if (sa_delref(sav)) {
1366 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1367 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1368 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1372 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1373 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1374 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1378 /* %%% SPD management */
1381 * OUT: NULL : not found
1382 * others : found, pointer to a SP.
1384 static struct secpolicy *
1385 key_getsp(struct secpolicyindex *spidx)
1387 SPTREE_RLOCK_TRACKER;
1388 struct secpolicy *sp;
1390 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
1393 TAILQ_FOREACH(sp, &V_sptree[spidx->dir], chain) {
1394 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1406 * OUT: NULL : not found
1407 * others : found, pointer to a SP.
1409 static struct secpolicy *
1410 key_getspbyid(u_int32_t id)
1412 SPTREE_RLOCK_TRACKER;
1413 struct secpolicy *sp;
1416 TAILQ_FOREACH(sp, &V_sptree[IPSEC_DIR_INBOUND], chain) {
1423 TAILQ_FOREACH(sp, &V_sptree[IPSEC_DIR_OUTBOUND], chain) {
1436 key_newsp(const char* where, int tag)
1438 struct secpolicy *newsp = NULL;
1440 newsp = (struct secpolicy *)
1441 malloc(sizeof(struct secpolicy), M_IPSEC_SP, M_NOWAIT|M_ZERO);
1443 refcount_init(&newsp->refcnt, 1);
1445 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1446 printf("DP %s from %s:%u return SP:%p\n", __func__,
1447 where, tag, newsp));
1452 * create secpolicy structure from sadb_x_policy structure.
1453 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1454 * so must be set properly later.
1457 key_msg2sp(struct sadb_x_policy *xpl0, size_t len, int *error)
1459 struct secpolicy *newsp;
1461 IPSEC_ASSERT(xpl0 != NULL, ("null xpl0"));
1462 IPSEC_ASSERT(len >= sizeof(*xpl0), ("policy too short: %zu", len));
1464 if (len != PFKEY_EXTLEN(xpl0)) {
1465 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n", __func__));
1470 if ((newsp = KEY_NEWSP()) == NULL) {
1475 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1476 newsp->policy = xpl0->sadb_x_policy_type;
1477 newsp->priority = xpl0->sadb_x_policy_priority;
1480 switch (xpl0->sadb_x_policy_type) {
1481 case IPSEC_POLICY_DISCARD:
1482 case IPSEC_POLICY_NONE:
1483 case IPSEC_POLICY_ENTRUST:
1484 case IPSEC_POLICY_BYPASS:
1488 case IPSEC_POLICY_IPSEC:
1491 struct sadb_x_ipsecrequest *xisr;
1492 struct ipsecrequest **p_isr = &newsp->req;
1494 /* validity check */
1495 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1496 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n",
1503 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1504 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1508 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1509 ipseclog((LOG_DEBUG, "%s: invalid ipsecrequest "
1510 "length.\n", __func__));
1516 /* allocate request buffer */
1517 /* NB: data structure is zero'd */
1518 *p_isr = ipsec_newisr();
1519 if ((*p_isr) == NULL) {
1520 ipseclog((LOG_DEBUG,
1521 "%s: No more memory.\n", __func__));
1528 switch (xisr->sadb_x_ipsecrequest_proto) {
1531 case IPPROTO_IPCOMP:
1534 ipseclog((LOG_DEBUG,
1535 "%s: invalid proto type=%u\n", __func__,
1536 xisr->sadb_x_ipsecrequest_proto));
1538 *error = EPROTONOSUPPORT;
1541 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1543 switch (xisr->sadb_x_ipsecrequest_mode) {
1544 case IPSEC_MODE_TRANSPORT:
1545 case IPSEC_MODE_TUNNEL:
1547 case IPSEC_MODE_ANY:
1549 ipseclog((LOG_DEBUG,
1550 "%s: invalid mode=%u\n", __func__,
1551 xisr->sadb_x_ipsecrequest_mode));
1556 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1558 switch (xisr->sadb_x_ipsecrequest_level) {
1559 case IPSEC_LEVEL_DEFAULT:
1560 case IPSEC_LEVEL_USE:
1561 case IPSEC_LEVEL_REQUIRE:
1563 case IPSEC_LEVEL_UNIQUE:
1564 /* validity check */
1566 * If range violation of reqid, kernel will
1567 * update it, don't refuse it.
1569 if (xisr->sadb_x_ipsecrequest_reqid
1570 > IPSEC_MANUAL_REQID_MAX) {
1571 ipseclog((LOG_DEBUG,
1572 "%s: reqid=%d range "
1573 "violation, updated by kernel.\n",
1575 xisr->sadb_x_ipsecrequest_reqid));
1576 xisr->sadb_x_ipsecrequest_reqid = 0;
1579 /* allocate new reqid id if reqid is zero. */
1580 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1582 if ((reqid = key_newreqid()) == 0) {
1587 (*p_isr)->saidx.reqid = reqid;
1588 xisr->sadb_x_ipsecrequest_reqid = reqid;
1590 /* set it for manual keying. */
1591 (*p_isr)->saidx.reqid =
1592 xisr->sadb_x_ipsecrequest_reqid;
1597 ipseclog((LOG_DEBUG, "%s: invalid level=%u\n",
1599 xisr->sadb_x_ipsecrequest_level));
1604 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1606 /* set IP addresses if there */
1607 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1608 struct sockaddr *paddr;
1610 paddr = (struct sockaddr *)(xisr + 1);
1612 /* validity check */
1614 > sizeof((*p_isr)->saidx.src)) {
1615 ipseclog((LOG_DEBUG, "%s: invalid "
1616 "request address length.\n",
1622 bcopy(paddr, &(*p_isr)->saidx.src,
1625 paddr = (struct sockaddr *)((caddr_t)paddr
1628 /* validity check */
1630 > sizeof((*p_isr)->saidx.dst)) {
1631 ipseclog((LOG_DEBUG, "%s: invalid "
1632 "request address length.\n",
1638 bcopy(paddr, &(*p_isr)->saidx.dst,
1642 (*p_isr)->sp = newsp;
1644 /* initialization for the next. */
1645 p_isr = &(*p_isr)->next;
1646 tlen -= xisr->sadb_x_ipsecrequest_len;
1648 /* validity check */
1650 ipseclog((LOG_DEBUG, "%s: becoming tlen < 0.\n",
1657 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1658 + xisr->sadb_x_ipsecrequest_len);
1663 ipseclog((LOG_DEBUG, "%s: invalid policy type.\n", __func__));
1676 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1678 auto_reqid = (auto_reqid == ~0
1679 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1681 /* XXX should be unique check */
1687 * copy secpolicy struct to sadb_x_policy structure indicated.
1690 key_sp2msg(struct secpolicy *sp)
1692 struct sadb_x_policy *xpl;
1697 IPSEC_ASSERT(sp != NULL, ("null policy"));
1699 tlen = key_getspreqmsglen(sp);
1701 m = m_get2(tlen, M_NOWAIT, MT_DATA, 0);
1706 xpl = mtod(m, struct sadb_x_policy *);
1709 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1710 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1711 xpl->sadb_x_policy_type = sp->policy;
1712 xpl->sadb_x_policy_dir = sp->spidx.dir;
1713 xpl->sadb_x_policy_id = sp->id;
1714 xpl->sadb_x_policy_priority = sp->priority;
1715 p = (caddr_t)xpl + sizeof(*xpl);
1717 /* if is the policy for ipsec ? */
1718 if (sp->policy == IPSEC_POLICY_IPSEC) {
1719 struct sadb_x_ipsecrequest *xisr;
1720 struct ipsecrequest *isr;
1722 for (isr = sp->req; isr != NULL; isr = isr->next) {
1724 xisr = (struct sadb_x_ipsecrequest *)p;
1726 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1727 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1728 xisr->sadb_x_ipsecrequest_level = isr->level;
1729 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1732 bcopy(&isr->saidx.src, p, isr->saidx.src.sa.sa_len);
1733 p += isr->saidx.src.sa.sa_len;
1734 bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len);
1735 p += isr->saidx.src.sa.sa_len;
1737 xisr->sadb_x_ipsecrequest_len =
1738 PFKEY_ALIGN8(sizeof(*xisr)
1739 + isr->saidx.src.sa.sa_len
1740 + isr->saidx.dst.sa.sa_len);
1747 /* m will not be freed nor modified */
1748 static struct mbuf *
1749 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1750 int ndeep, int nitem, ...)
1755 struct mbuf *result = NULL, *n;
1758 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1759 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1761 va_start(ap, nitem);
1762 for (i = 0; i < nitem; i++) {
1763 idx = va_arg(ap, int);
1764 if (idx < 0 || idx > SADB_EXT_MAX)
1766 /* don't attempt to pull empty extension */
1767 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1769 if (idx != SADB_EXT_RESERVED &&
1770 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1773 if (idx == SADB_EXT_RESERVED) {
1774 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1776 IPSEC_ASSERT(len <= MHLEN, ("header too big %u", len));
1778 MGETHDR(n, M_NOWAIT, MT_DATA);
1783 m_copydata(m, 0, sizeof(struct sadb_msg),
1785 } else if (i < ndeep) {
1786 len = mhp->extlen[idx];
1787 n = m_get2(len, M_NOWAIT, MT_DATA, 0);
1792 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1795 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1808 if ((result->m_flags & M_PKTHDR) != 0) {
1809 result->m_pkthdr.len = 0;
1810 for (n = result; n; n = n->m_next)
1811 result->m_pkthdr.len += n->m_len;
1823 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1824 * add an entry to SP database, when received
1825 * <base, address(SD), (lifetime(H),) policy>
1827 * Adding to SP database,
1829 * <base, address(SD), (lifetime(H),) policy>
1830 * to the socket which was send.
1832 * SPDADD set a unique policy entry.
1833 * SPDSETIDX like SPDADD without a part of policy requests.
1834 * SPDUPDATE replace a unique policy entry.
1836 * m will always be freed.
1839 key_spdadd(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
1841 struct sadb_address *src0, *dst0;
1842 struct sadb_x_policy *xpl0, *xpl;
1843 struct sadb_lifetime *lft = NULL;
1844 struct secpolicyindex spidx;
1845 struct secpolicy *newsp;
1848 IPSEC_ASSERT(so != NULL, ("null socket"));
1849 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1850 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1851 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
1853 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1854 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1855 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1856 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1857 return key_senderror(so, m, EINVAL);
1859 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1860 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1861 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1862 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1864 return key_senderror(so, m, EINVAL);
1866 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1867 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1868 < sizeof(struct sadb_lifetime)) {
1869 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1871 return key_senderror(so, m, EINVAL);
1873 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1876 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1877 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1878 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1881 * Note: do not parse SADB_X_EXT_NAT_T_* here:
1882 * we are processing traffic endpoints.
1886 /* XXX boundary check against sa_len */
1887 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1890 src0->sadb_address_prefixlen,
1891 dst0->sadb_address_prefixlen,
1892 src0->sadb_address_proto,
1895 /* checking the direciton. */
1896 switch (xpl0->sadb_x_policy_dir) {
1897 case IPSEC_DIR_INBOUND:
1898 case IPSEC_DIR_OUTBOUND:
1901 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
1902 mhp->msg->sadb_msg_errno = EINVAL;
1907 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1908 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1909 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1910 ipseclog((LOG_DEBUG, "%s: Invalid policy type.\n", __func__));
1911 return key_senderror(so, m, EINVAL);
1914 /* policy requests are mandatory when action is ipsec. */
1915 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1916 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1917 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1918 ipseclog((LOG_DEBUG, "%s: some policy requests part required\n",
1920 return key_senderror(so, m, EINVAL);
1924 * checking there is SP already or not.
1925 * SPDUPDATE doesn't depend on whether there is a SP or not.
1926 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1929 newsp = key_getsp(&spidx);
1930 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1936 if (newsp != NULL) {
1938 ipseclog((LOG_DEBUG, "%s: a SP entry exists already.\n",
1940 return key_senderror(so, m, EEXIST);
1944 /* XXX: there is race between key_getsp and key_msg2sp. */
1946 /* allocation new SP entry */
1947 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1948 return key_senderror(so, m, error);
1951 if ((newsp->id = key_getnewspid()) == 0) {
1953 return key_senderror(so, m, ENOBUFS);
1956 /* XXX boundary check against sa_len */
1957 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1960 src0->sadb_address_prefixlen,
1961 dst0->sadb_address_prefixlen,
1962 src0->sadb_address_proto,
1965 /* sanity check on addr pair */
1966 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1967 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1969 return key_senderror(so, m, EINVAL);
1971 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1972 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1974 return key_senderror(so, m, EINVAL);
1977 if (newsp->req && newsp->req->saidx.src.sa.sa_family &&
1978 newsp->req->saidx.dst.sa.sa_family) {
1979 if (newsp->req->saidx.src.sa.sa_family !=
1980 newsp->req->saidx.dst.sa.sa_family) {
1982 return key_senderror(so, m, EINVAL);
1987 newsp->created = time_second;
1988 newsp->lastused = newsp->created;
1989 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1990 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1992 key_insertsp(newsp);
1994 /* delete the entry in spacqtree */
1995 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1996 struct secspacq *spacq = key_getspacq(&spidx);
1997 if (spacq != NULL) {
1998 /* reset counter in order to deletion by timehandler. */
1999 spacq->created = time_second;
2006 struct mbuf *n, *mpolicy;
2007 struct sadb_msg *newmsg;
2011 * Note: do not send SADB_X_EXT_NAT_T_* here:
2012 * we are sending traffic endpoints.
2015 /* create new sadb_msg to reply. */
2017 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
2018 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
2019 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2021 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
2023 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2026 return key_senderror(so, m, ENOBUFS);
2028 if (n->m_len < sizeof(*newmsg)) {
2029 n = m_pullup(n, sizeof(*newmsg));
2031 return key_senderror(so, m, ENOBUFS);
2033 newmsg = mtod(n, struct sadb_msg *);
2034 newmsg->sadb_msg_errno = 0;
2035 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2038 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
2039 sizeof(*xpl), &off);
2040 if (mpolicy == NULL) {
2041 /* n is already freed */
2042 return key_senderror(so, m, ENOBUFS);
2044 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
2045 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
2047 return key_senderror(so, m, EINVAL);
2049 xpl->sadb_x_policy_id = newsp->id;
2052 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2057 * get new policy id.
2065 u_int32_t newid = 0;
2066 int count = V_key_spi_trycnt; /* XXX */
2067 struct secpolicy *sp;
2069 /* when requesting to allocate spi ranged */
2071 newid = (V_policy_id = (V_policy_id == ~0 ? 1 : V_policy_id + 1));
2073 if ((sp = key_getspbyid(newid)) == NULL)
2079 if (count == 0 || newid == 0) {
2080 ipseclog((LOG_DEBUG, "%s: to allocate policy id is failed.\n",
2089 * SADB_SPDDELETE processing
2091 * <base, address(SD), policy(*)>
2092 * from the user(?), and set SADB_SASTATE_DEAD,
2094 * <base, address(SD), policy(*)>
2096 * policy(*) including direction of policy.
2098 * m will always be freed.
2101 key_spddelete(struct socket *so, struct mbuf *m,
2102 const struct sadb_msghdr *mhp)
2104 struct sadb_address *src0, *dst0;
2105 struct sadb_x_policy *xpl0;
2106 struct secpolicyindex spidx;
2107 struct secpolicy *sp;
2109 IPSEC_ASSERT(so != NULL, ("null so"));
2110 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2111 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2112 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2114 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
2115 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
2116 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
2117 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2119 return key_senderror(so, m, EINVAL);
2121 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
2122 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
2123 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2124 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2126 return key_senderror(so, m, EINVAL);
2129 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2130 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2131 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2134 * Note: do not parse SADB_X_EXT_NAT_T_* here:
2135 * we are processing traffic endpoints.
2139 /* XXX boundary check against sa_len */
2140 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2143 src0->sadb_address_prefixlen,
2144 dst0->sadb_address_prefixlen,
2145 src0->sadb_address_proto,
2148 /* checking the direciton. */
2149 switch (xpl0->sadb_x_policy_dir) {
2150 case IPSEC_DIR_INBOUND:
2151 case IPSEC_DIR_OUTBOUND:
2154 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
2155 return key_senderror(so, m, EINVAL);
2158 /* Is there SP in SPD ? */
2159 if ((sp = key_getsp(&spidx)) == NULL) {
2160 ipseclog((LOG_DEBUG, "%s: no SP found.\n", __func__));
2161 return key_senderror(so, m, EINVAL);
2164 /* save policy id to buffer to be returned. */
2165 xpl0->sadb_x_policy_id = sp->id;
2172 struct sadb_msg *newmsg;
2175 * Note: do not send SADB_X_EXT_NAT_T_* here:
2176 * we are sending traffic endpoints.
2179 /* create new sadb_msg to reply. */
2180 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2181 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2183 return key_senderror(so, m, ENOBUFS);
2185 newmsg = mtod(n, struct sadb_msg *);
2186 newmsg->sadb_msg_errno = 0;
2187 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2190 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2195 * SADB_SPDDELETE2 processing
2198 * from the user(?), and set SADB_SASTATE_DEAD,
2202 * policy(*) including direction of policy.
2204 * m will always be freed.
2207 key_spddelete2(struct socket *so, struct mbuf *m,
2208 const struct sadb_msghdr *mhp)
2211 struct secpolicy *sp;
2213 IPSEC_ASSERT(so != NULL, ("null socket"));
2214 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2215 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2216 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2218 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2219 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2220 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", __func__));
2221 return key_senderror(so, m, EINVAL);
2224 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2226 /* Is there SP in SPD ? */
2227 if ((sp = key_getspbyid(id)) == NULL) {
2228 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2229 return key_senderror(so, m, EINVAL);
2236 struct mbuf *n, *nn;
2237 struct sadb_msg *newmsg;
2240 /* create new sadb_msg to reply. */
2241 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2243 MGETHDR(n, M_NOWAIT, MT_DATA);
2244 if (n && len > MHLEN) {
2245 if (!(MCLGET(n, M_NOWAIT))) {
2251 return key_senderror(so, m, ENOBUFS);
2257 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2258 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2260 IPSEC_ASSERT(off == len, ("length inconsistency (off %u len %u)",
2263 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2264 mhp->extlen[SADB_X_EXT_POLICY], M_NOWAIT);
2267 return key_senderror(so, m, ENOBUFS);
2270 n->m_pkthdr.len = 0;
2271 for (nn = n; nn; nn = nn->m_next)
2272 n->m_pkthdr.len += nn->m_len;
2274 newmsg = mtod(n, struct sadb_msg *);
2275 newmsg->sadb_msg_errno = 0;
2276 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2279 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2284 * SADB_X_SPDGET processing
2289 * <base, address(SD), policy>
2291 * policy(*) including direction of policy.
2293 * m will always be freed.
2296 key_spdget(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
2299 struct secpolicy *sp;
2302 IPSEC_ASSERT(so != NULL, ("null socket"));
2303 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2304 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2305 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2307 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2308 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2309 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2311 return key_senderror(so, m, EINVAL);
2314 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2316 /* Is there SP in SPD ? */
2317 if ((sp = key_getspbyid(id)) == NULL) {
2318 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2319 return key_senderror(so, m, ENOENT);
2322 n = key_setdumpsp(sp, SADB_X_SPDGET, mhp->msg->sadb_msg_seq,
2323 mhp->msg->sadb_msg_pid);
2327 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2329 return key_senderror(so, m, ENOBUFS);
2333 * SADB_X_SPDACQUIRE processing.
2334 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2337 * to KMD, and expect to receive
2338 * <base> with SADB_X_SPDACQUIRE if error occurred,
2341 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2342 * policy(*) is without policy requests.
2345 * others: error number
2348 key_spdacquire(struct secpolicy *sp)
2350 struct mbuf *result = NULL, *m;
2351 struct secspacq *newspacq;
2353 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2354 IPSEC_ASSERT(sp->req == NULL, ("policy exists"));
2355 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
2356 ("policy not IPSEC %u", sp->policy));
2358 /* Get an entry to check whether sent message or not. */
2359 newspacq = key_getspacq(&sp->spidx);
2360 if (newspacq != NULL) {
2361 if (V_key_blockacq_count < newspacq->count) {
2362 /* reset counter and do send message. */
2363 newspacq->count = 0;
2365 /* increment counter and do nothing. */
2372 /* make new entry for blocking to send SADB_ACQUIRE. */
2373 newspacq = key_newspacq(&sp->spidx);
2374 if (newspacq == NULL)
2378 /* create new sadb_msg to reply. */
2379 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2385 result->m_pkthdr.len = 0;
2386 for (m = result; m; m = m->m_next)
2387 result->m_pkthdr.len += m->m_len;
2389 mtod(result, struct sadb_msg *)->sadb_msg_len =
2390 PFKEY_UNIT64(result->m_pkthdr.len);
2392 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2396 * SADB_SPDFLUSH processing
2399 * from the user, and free all entries in secpctree.
2403 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2405 * m will always be freed.
2408 key_spdflush(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
2410 TAILQ_HEAD(, secpolicy) drainq;
2411 struct sadb_msg *newmsg;
2412 struct secpolicy *sp, *nextsp;
2415 IPSEC_ASSERT(so != NULL, ("null socket"));
2416 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2417 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2418 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2420 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2421 return key_senderror(so, m, EINVAL);
2423 TAILQ_INIT(&drainq);
2425 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2426 TAILQ_CONCAT(&drainq, &V_sptree[dir], chain);
2429 * We need to set state to DEAD for each policy to be sure,
2430 * that another thread won't try to unlink it.
2432 TAILQ_FOREACH(sp, &drainq, chain)
2433 sp->state = IPSEC_SPSTATE_DEAD;
2435 sp = TAILQ_FIRST(&drainq);
2436 while (sp != NULL) {
2437 nextsp = TAILQ_NEXT(sp, chain);
2442 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2443 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2444 return key_senderror(so, m, ENOBUFS);
2450 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2451 newmsg = mtod(m, struct sadb_msg *);
2452 newmsg->sadb_msg_errno = 0;
2453 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2455 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2459 * SADB_SPDDUMP processing
2462 * from the user, and dump all SP leaves
2467 * m will always be freed.
2470 key_spddump(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
2472 SPTREE_RLOCK_TRACKER;
2473 struct secpolicy *sp;
2478 IPSEC_ASSERT(so != NULL, ("null socket"));
2479 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2480 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2481 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2483 /* search SPD entry and get buffer size. */
2486 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2487 TAILQ_FOREACH(sp, &V_sptree[dir], chain) {
2494 return key_senderror(so, m, ENOENT);
2497 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2498 TAILQ_FOREACH(sp, &V_sptree[dir], chain) {
2500 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2501 mhp->msg->sadb_msg_pid);
2504 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2513 static struct mbuf *
2514 key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq,
2517 struct mbuf *result = NULL, *m;
2518 struct seclifetime lt;
2520 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2526 * Note: do not send SADB_X_EXT_NAT_T_* here:
2527 * we are sending traffic endpoints.
2529 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2530 &sp->spidx.src.sa, sp->spidx.prefs,
2531 sp->spidx.ul_proto);
2536 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2537 &sp->spidx.dst.sa, sp->spidx.prefd,
2538 sp->spidx.ul_proto);
2549 lt.addtime=sp->created;
2550 lt.usetime= sp->lastused;
2551 m = key_setlifetime(<, SADB_EXT_LIFETIME_CURRENT);
2556 lt.addtime=sp->lifetime;
2557 lt.usetime= sp->validtime;
2558 m = key_setlifetime(<, SADB_EXT_LIFETIME_HARD);
2564 if ((result->m_flags & M_PKTHDR) == 0)
2567 if (result->m_len < sizeof(struct sadb_msg)) {
2568 result = m_pullup(result, sizeof(struct sadb_msg));
2573 result->m_pkthdr.len = 0;
2574 for (m = result; m; m = m->m_next)
2575 result->m_pkthdr.len += m->m_len;
2577 mtod(result, struct sadb_msg *)->sadb_msg_len =
2578 PFKEY_UNIT64(result->m_pkthdr.len);
2588 * get PFKEY message length for security policy and request.
2591 key_getspreqmsglen(struct secpolicy *sp)
2595 tlen = sizeof(struct sadb_x_policy);
2597 /* if is the policy for ipsec ? */
2598 if (sp->policy != IPSEC_POLICY_IPSEC)
2601 /* get length of ipsec requests */
2603 struct ipsecrequest *isr;
2606 for (isr = sp->req; isr != NULL; isr = isr->next) {
2607 len = sizeof(struct sadb_x_ipsecrequest)
2608 + isr->saidx.src.sa.sa_len
2609 + isr->saidx.dst.sa.sa_len;
2611 tlen += PFKEY_ALIGN8(len);
2619 * SADB_SPDEXPIRE processing
2621 * <base, address(SD), lifetime(CH), policy>
2625 * others : error number
2628 key_spdexpire(struct secpolicy *sp)
2630 struct mbuf *result = NULL, *m;
2633 struct sadb_lifetime *lt;
2635 /* XXX: Why do we lock ? */
2637 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2639 /* set msg header */
2640 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2647 /* create lifetime extension (current and hard) */
2648 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2649 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
2656 bzero(mtod(m, caddr_t), len);
2657 lt = mtod(m, struct sadb_lifetime *);
2658 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2659 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2660 lt->sadb_lifetime_allocations = 0;
2661 lt->sadb_lifetime_bytes = 0;
2662 lt->sadb_lifetime_addtime = sp->created;
2663 lt->sadb_lifetime_usetime = sp->lastused;
2664 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2665 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2666 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2667 lt->sadb_lifetime_allocations = 0;
2668 lt->sadb_lifetime_bytes = 0;
2669 lt->sadb_lifetime_addtime = sp->lifetime;
2670 lt->sadb_lifetime_usetime = sp->validtime;
2674 * Note: do not send SADB_X_EXT_NAT_T_* here:
2675 * we are sending traffic endpoints.
2678 /* set sadb_address for source */
2679 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2681 sp->spidx.prefs, sp->spidx.ul_proto);
2688 /* set sadb_address for destination */
2689 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2691 sp->spidx.prefd, sp->spidx.ul_proto);
2706 if ((result->m_flags & M_PKTHDR) == 0) {
2711 if (result->m_len < sizeof(struct sadb_msg)) {
2712 result = m_pullup(result, sizeof(struct sadb_msg));
2713 if (result == NULL) {
2719 result->m_pkthdr.len = 0;
2720 for (m = result; m; m = m->m_next)
2721 result->m_pkthdr.len += m->m_len;
2723 mtod(result, struct sadb_msg *)->sadb_msg_len =
2724 PFKEY_UNIT64(result->m_pkthdr.len);
2726 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2734 /* %%% SAD management */
2736 * allocating a memory for new SA head, and copy from the values of mhp.
2737 * OUT: NULL : failure due to the lack of memory.
2738 * others : pointer to new SA head.
2740 static struct secashead *
2741 key_newsah(struct secasindex *saidx)
2743 struct secashead *newsah;
2745 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
2747 newsah = malloc(sizeof(struct secashead), M_IPSEC_SAH, M_NOWAIT|M_ZERO);
2748 if (newsah != NULL) {
2750 for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++)
2751 LIST_INIT(&newsah->savtree[i]);
2752 newsah->saidx = *saidx;
2754 /* add to saidxtree */
2755 newsah->state = SADB_SASTATE_MATURE;
2758 LIST_INSERT_HEAD(&V_sahtree, newsah, chain);
2765 * delete SA index and all SA registerd.
2768 key_delsah(struct secashead *sah)
2770 struct secasvar *sav, *nextsav;
2774 IPSEC_ASSERT(sah != NULL, ("NULL sah"));
2775 SAHTREE_LOCK_ASSERT();
2777 /* searching all SA registerd in the secindex. */
2779 stateidx < _ARRAYLEN(saorder_state_any);
2781 u_int state = saorder_state_any[stateidx];
2782 LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain, nextsav) {
2783 if (sav->refcnt == 0) {
2785 KEY_CHKSASTATE(state, sav->state, __func__);
2787 * do NOT call KEY_FREESAV here:
2788 * it will only delete the sav if refcnt == 1,
2789 * where we already know that refcnt == 0
2793 /* give up to delete this sa */
2798 if (!zombie) { /* delete only if there are savs */
2799 /* remove from tree of SA index */
2800 if (__LIST_CHAINED(sah))
2801 LIST_REMOVE(sah, chain);
2802 free(sah, M_IPSEC_SAH);
2807 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2808 * and copy the values of mhp into new buffer.
2809 * When SAD message type is GETSPI:
2810 * to set sequence number from acq_seq++,
2811 * to set zero to SPI.
2812 * not to call key_setsava().
2814 * others : pointer to new secasvar.
2816 * does not modify mbuf. does not free mbuf on error.
2818 static struct secasvar *
2819 key_newsav(struct mbuf *m, const struct sadb_msghdr *mhp,
2820 struct secashead *sah, int *errp, const char *where, int tag)
2822 struct secasvar *newsav;
2823 const struct sadb_sa *xsa;
2825 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2826 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2827 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2828 IPSEC_ASSERT(sah != NULL, ("null secashead"));
2830 newsav = malloc(sizeof(struct secasvar), M_IPSEC_SA, M_NOWAIT|M_ZERO);
2831 if (newsav == NULL) {
2832 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2837 switch (mhp->msg->sadb_msg_type) {
2841 #ifdef IPSEC_DOSEQCHECK
2842 /* sync sequence number */
2843 if (mhp->msg->sadb_msg_seq == 0)
2845 (V_acq_seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq));
2848 newsav->seq = mhp->msg->sadb_msg_seq;
2853 if (mhp->ext[SADB_EXT_SA] == NULL) {
2854 free(newsav, M_IPSEC_SA);
2856 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2861 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2862 newsav->spi = xsa->sadb_sa_spi;
2863 newsav->seq = mhp->msg->sadb_msg_seq;
2866 free(newsav, M_IPSEC_SA);
2873 /* copy sav values */
2874 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2875 *errp = key_setsaval(newsav, m, mhp);
2877 free(newsav, M_IPSEC_SA);
2883 SECASVAR_LOCK_INIT(newsav);
2886 newsav->created = time_second;
2887 newsav->pid = mhp->msg->sadb_msg_pid;
2892 newsav->state = SADB_SASTATE_LARVAL;
2895 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2899 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
2900 printf("DP %s from %s:%u return SP:%p\n", __func__,
2901 where, tag, newsav));
2907 * free() SA variable entry.
2910 key_cleansav(struct secasvar *sav)
2913 * Cleanup xform state. Note that zeroize'ing causes the
2914 * keys to be cleared; otherwise we must do it ourself.
2916 if (sav->tdb_xform != NULL) {
2917 sav->tdb_xform->xf_zeroize(sav);
2918 sav->tdb_xform = NULL;
2920 if (sav->key_auth != NULL)
2921 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
2922 if (sav->key_enc != NULL)
2923 bzero(sav->key_enc->key_data, _KEYLEN(sav->key_enc));
2925 if (sav->key_auth != NULL) {
2926 if (sav->key_auth->key_data != NULL)
2927 free(sav->key_auth->key_data, M_IPSEC_MISC);
2928 free(sav->key_auth, M_IPSEC_MISC);
2929 sav->key_auth = NULL;
2931 if (sav->key_enc != NULL) {
2932 if (sav->key_enc->key_data != NULL)
2933 free(sav->key_enc->key_data, M_IPSEC_MISC);
2934 free(sav->key_enc, M_IPSEC_MISC);
2935 sav->key_enc = NULL;
2938 bzero(sav->sched, sav->schedlen);
2939 free(sav->sched, M_IPSEC_MISC);
2942 if (sav->replay != NULL) {
2943 free(sav->replay, M_IPSEC_MISC);
2946 if (sav->lft_c != NULL) {
2947 free(sav->lft_c, M_IPSEC_MISC);
2950 if (sav->lft_h != NULL) {
2951 free(sav->lft_h, M_IPSEC_MISC);
2954 if (sav->lft_s != NULL) {
2955 free(sav->lft_s, M_IPSEC_MISC);
2961 * free() SA variable entry.
2964 key_delsav(struct secasvar *sav)
2966 IPSEC_ASSERT(sav != NULL, ("null sav"));
2967 IPSEC_ASSERT(sav->refcnt == 0, ("reference count %u > 0", sav->refcnt));
2969 /* remove from SA header */
2970 if (__LIST_CHAINED(sav))
2971 LIST_REMOVE(sav, chain);
2973 SECASVAR_LOCK_DESTROY(sav);
2974 free(sav, M_IPSEC_SA);
2981 * others : found, pointer to a SA.
2983 static struct secashead *
2984 key_getsah(struct secasindex *saidx)
2986 struct secashead *sah;
2989 LIST_FOREACH(sah, &V_sahtree, chain) {
2990 if (sah->state == SADB_SASTATE_DEAD)
2992 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
3001 * check not to be duplicated SPI.
3002 * NOTE: this function is too slow due to searching all SAD.
3005 * others : found, pointer to a SA.
3007 static struct secasvar *
3008 key_checkspidup(struct secasindex *saidx, u_int32_t spi)
3010 struct secashead *sah;
3011 struct secasvar *sav;
3013 /* check address family */
3014 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
3015 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
3023 LIST_FOREACH(sah, &V_sahtree, chain) {
3024 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
3026 sav = key_getsavbyspi(sah, spi);
3036 * search SAD litmited alive SA, protocol, SPI.
3039 * others : found, pointer to a SA.
3041 static struct secasvar *
3042 key_getsavbyspi(struct secashead *sah, u_int32_t spi)
3044 struct secasvar *sav;
3045 u_int stateidx, state;
3048 SAHTREE_LOCK_ASSERT();
3049 /* search all status */
3051 stateidx < _ARRAYLEN(saorder_state_alive);
3054 state = saorder_state_alive[stateidx];
3055 LIST_FOREACH(sav, &sah->savtree[state], chain) {
3058 if (sav->state != state) {
3059 ipseclog((LOG_DEBUG, "%s: "
3060 "invalid sav->state (queue: %d SA: %d)\n",
3061 __func__, state, sav->state));
3065 if (sav->spi == spi)
3074 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
3075 * You must update these if need.
3079 * does not modify mbuf. does not free mbuf on error.
3082 key_setsaval(struct secasvar *sav, struct mbuf *m,
3083 const struct sadb_msghdr *mhp)
3087 IPSEC_ASSERT(m != NULL, ("null mbuf"));
3088 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
3089 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
3091 /* initialization */
3093 sav->key_auth = NULL;
3094 sav->key_enc = NULL;
3100 sav->tdb_xform = NULL; /* transform */
3101 sav->tdb_encalgxform = NULL; /* encoding algorithm */
3102 sav->tdb_authalgxform = NULL; /* authentication algorithm */
3103 sav->tdb_compalgxform = NULL; /* compression algorithm */
3104 /* Initialize even if NAT-T not compiled in: */
3106 sav->natt_esp_frag_len = 0;
3109 if (mhp->ext[SADB_EXT_SA] != NULL) {
3110 const struct sadb_sa *sa0;
3112 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
3113 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
3118 sav->alg_auth = sa0->sadb_sa_auth;
3119 sav->alg_enc = sa0->sadb_sa_encrypt;
3120 sav->flags = sa0->sadb_sa_flags;
3123 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
3124 sav->replay = (struct secreplay *)
3125 malloc(sizeof(struct secreplay)+sa0->sadb_sa_replay, M_IPSEC_MISC, M_NOWAIT|M_ZERO);
3126 if (sav->replay == NULL) {
3127 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3132 if (sa0->sadb_sa_replay != 0)
3133 sav->replay->bitmap = (caddr_t)(sav->replay+1);
3134 sav->replay->wsize = sa0->sadb_sa_replay;
3138 /* Authentication keys */
3139 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
3140 const struct sadb_key *key0;
3143 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
3144 len = mhp->extlen[SADB_EXT_KEY_AUTH];
3147 if (len < sizeof(*key0)) {
3151 switch (mhp->msg->sadb_msg_satype) {
3152 case SADB_SATYPE_AH:
3153 case SADB_SATYPE_ESP:
3154 case SADB_X_SATYPE_TCPSIGNATURE:
3155 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3156 sav->alg_auth != SADB_X_AALG_NULL)
3159 case SADB_X_SATYPE_IPCOMP:
3165 ipseclog((LOG_DEBUG, "%s: invalid key_auth values.\n",
3170 sav->key_auth = (struct seckey *)key_dup_keymsg(key0, len,
3172 if (sav->key_auth == NULL ) {
3173 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3180 /* Encryption key */
3181 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3182 const struct sadb_key *key0;
3185 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3186 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3189 if (len < sizeof(*key0)) {
3193 switch (mhp->msg->sadb_msg_satype) {
3194 case SADB_SATYPE_ESP:
3195 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3196 sav->alg_enc != SADB_EALG_NULL) {
3200 sav->key_enc = (struct seckey *)key_dup_keymsg(key0,
3203 if (sav->key_enc == NULL) {
3204 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3210 case SADB_X_SATYPE_IPCOMP:
3211 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3213 sav->key_enc = NULL; /*just in case*/
3215 case SADB_SATYPE_AH:
3216 case SADB_X_SATYPE_TCPSIGNATURE:
3222 ipseclog((LOG_DEBUG, "%s: invalid key_enc value.\n",
3231 switch (mhp->msg->sadb_msg_satype) {
3232 case SADB_SATYPE_AH:
3233 error = xform_init(sav, XF_AH);
3235 case SADB_SATYPE_ESP:
3236 error = xform_init(sav, XF_ESP);
3238 case SADB_X_SATYPE_IPCOMP:
3239 error = xform_init(sav, XF_IPCOMP);
3241 case SADB_X_SATYPE_TCPSIGNATURE:
3242 error = xform_init(sav, XF_TCPSIGNATURE);
3246 ipseclog((LOG_DEBUG, "%s: unable to initialize SA type %u.\n",
3247 __func__, mhp->msg->sadb_msg_satype));
3252 sav->created = time_second;
3254 /* make lifetime for CURRENT */
3255 sav->lft_c = malloc(sizeof(struct seclifetime), M_IPSEC_MISC, M_NOWAIT);
3256 if (sav->lft_c == NULL) {
3257 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3262 sav->lft_c->allocations = 0;
3263 sav->lft_c->bytes = 0;
3264 sav->lft_c->addtime = time_second;
3265 sav->lft_c->usetime = 0;
3267 /* lifetimes for HARD and SOFT */
3269 const struct sadb_lifetime *lft0;
3271 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3273 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3277 sav->lft_h = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3278 if (sav->lft_h == NULL) {
3279 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3283 /* to be initialize ? */
3286 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3288 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3292 sav->lft_s = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3293 if (sav->lft_s == NULL) {
3294 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3298 /* to be initialize ? */
3305 /* initialization */
3312 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3317 key_mature(struct secasvar *sav)
3321 /* check SPI value */
3322 switch (sav->sah->saidx.proto) {
3326 * RFC 4302, 2.4. Security Parameters Index (SPI), SPI values
3327 * 1-255 reserved by IANA for future use,
3328 * 0 for implementation specific, local use.
3330 if (ntohl(sav->spi) <= 255) {
3331 ipseclog((LOG_DEBUG, "%s: illegal range of SPI %u.\n",
3332 __func__, (u_int32_t)ntohl(sav->spi)));
3339 switch (sav->sah->saidx.proto) {
3342 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3343 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3344 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3345 "given to old-esp.\n", __func__));
3348 error = xform_init(sav, XF_ESP);
3352 if (sav->flags & SADB_X_EXT_DERIV) {
3353 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3354 "given to AH SA.\n", __func__));
3357 if (sav->alg_enc != SADB_EALG_NONE) {
3358 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3359 "mismated.\n", __func__));
3362 error = xform_init(sav, XF_AH);
3364 case IPPROTO_IPCOMP:
3365 if (sav->alg_auth != SADB_AALG_NONE) {
3366 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3367 "mismated.\n", __func__));
3370 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3371 && ntohl(sav->spi) >= 0x10000) {
3372 ipseclog((LOG_DEBUG, "%s: invalid cpi for IPComp.\n",
3376 error = xform_init(sav, XF_IPCOMP);
3379 if (sav->alg_enc != SADB_EALG_NONE) {
3380 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3381 "mismated.\n", __func__));
3384 error = xform_init(sav, XF_TCPSIGNATURE);
3387 ipseclog((LOG_DEBUG, "%s: Invalid satype.\n", __func__));
3388 error = EPROTONOSUPPORT;
3393 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3400 * subroutine for SADB_GET and SADB_DUMP.
3402 static struct mbuf *
3403 key_setdumpsa(struct secasvar *sav, u_int8_t type, u_int8_t satype,
3404 u_int32_t seq, u_int32_t pid)
3406 struct mbuf *result = NULL, *tres = NULL, *m;
3409 SADB_EXT_SA, SADB_X_EXT_SA2,
3410 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3411 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3412 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3413 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3414 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3416 SADB_X_EXT_NAT_T_TYPE,
3417 SADB_X_EXT_NAT_T_SPORT, SADB_X_EXT_NAT_T_DPORT,
3418 SADB_X_EXT_NAT_T_OAI, SADB_X_EXT_NAT_T_OAR,
3419 SADB_X_EXT_NAT_T_FRAG,
3423 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3428 for (i = nitems(dumporder) - 1; i >= 0; i--) {
3430 switch (dumporder[i]) {
3432 m = key_setsadbsa(sav);
3437 case SADB_X_EXT_SA2:
3438 m = key_setsadbxsa2(sav->sah->saidx.mode,
3439 sav->replay ? sav->replay->count : 0,
3440 sav->sah->saidx.reqid);
3445 case SADB_EXT_ADDRESS_SRC:
3446 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3447 &sav->sah->saidx.src.sa,
3448 FULLMASK, IPSEC_ULPROTO_ANY);
3453 case SADB_EXT_ADDRESS_DST:
3454 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3455 &sav->sah->saidx.dst.sa,
3456 FULLMASK, IPSEC_ULPROTO_ANY);
3461 case SADB_EXT_KEY_AUTH:
3464 m = key_setkey(sav->key_auth, SADB_EXT_KEY_AUTH);
3469 case SADB_EXT_KEY_ENCRYPT:
3472 m = key_setkey(sav->key_enc, SADB_EXT_KEY_ENCRYPT);
3477 case SADB_EXT_LIFETIME_CURRENT:
3480 m = key_setlifetime(sav->lft_c,
3481 SADB_EXT_LIFETIME_CURRENT);
3486 case SADB_EXT_LIFETIME_HARD:
3489 m = key_setlifetime(sav->lft_h,
3490 SADB_EXT_LIFETIME_HARD);
3495 case SADB_EXT_LIFETIME_SOFT:
3498 m = key_setlifetime(sav->lft_s,
3499 SADB_EXT_LIFETIME_SOFT);
3506 case SADB_X_EXT_NAT_T_TYPE:
3507 m = key_setsadbxtype(sav->natt_type);
3512 case SADB_X_EXT_NAT_T_DPORT:
3513 m = key_setsadbxport(
3514 KEY_PORTFROMSADDR(&sav->sah->saidx.dst),
3515 SADB_X_EXT_NAT_T_DPORT);
3520 case SADB_X_EXT_NAT_T_SPORT:
3521 m = key_setsadbxport(
3522 KEY_PORTFROMSADDR(&sav->sah->saidx.src),
3523 SADB_X_EXT_NAT_T_SPORT);
3528 case SADB_X_EXT_NAT_T_OAI:
3529 case SADB_X_EXT_NAT_T_OAR:
3530 case SADB_X_EXT_NAT_T_FRAG:
3531 /* We do not (yet) support those. */
3535 case SADB_EXT_ADDRESS_PROXY:
3536 case SADB_EXT_IDENTITY_SRC:
3537 case SADB_EXT_IDENTITY_DST:
3538 /* XXX: should we brought from SPD ? */
3539 case SADB_EXT_SENSITIVITY:
3552 m_cat(result, tres);
3554 if (result->m_len < sizeof(struct sadb_msg)) {
3555 result = m_pullup(result, sizeof(struct sadb_msg));
3560 result->m_pkthdr.len = 0;
3561 for (m = result; m; m = m->m_next)
3562 result->m_pkthdr.len += m->m_len;
3564 mtod(result, struct sadb_msg *)->sadb_msg_len =
3565 PFKEY_UNIT64(result->m_pkthdr.len);
3576 * set data into sadb_msg.
3578 static struct mbuf *
3579 key_setsadbmsg(u_int8_t type, u_int16_t tlen, u_int8_t satype, u_int32_t seq,
3580 pid_t pid, u_int16_t reserved)
3586 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3589 MGETHDR(m, M_NOWAIT, MT_DATA);
3590 if (m && len > MHLEN) {
3591 if (!(MCLGET(m, M_NOWAIT))) {
3598 m->m_pkthdr.len = m->m_len = len;
3601 p = mtod(m, struct sadb_msg *);
3604 p->sadb_msg_version = PF_KEY_V2;
3605 p->sadb_msg_type = type;
3606 p->sadb_msg_errno = 0;
3607 p->sadb_msg_satype = satype;
3608 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3609 p->sadb_msg_reserved = reserved;
3610 p->sadb_msg_seq = seq;
3611 p->sadb_msg_pid = (u_int32_t)pid;
3617 * copy secasvar data into sadb_address.
3619 static struct mbuf *
3620 key_setsadbsa(struct secasvar *sav)
3626 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3627 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3632 p = mtod(m, struct sadb_sa *);
3634 p->sadb_sa_len = PFKEY_UNIT64(len);
3635 p->sadb_sa_exttype = SADB_EXT_SA;
3636 p->sadb_sa_spi = sav->spi;
3637 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3638 p->sadb_sa_state = sav->state;
3639 p->sadb_sa_auth = sav->alg_auth;
3640 p->sadb_sa_encrypt = sav->alg_enc;
3641 p->sadb_sa_flags = sav->flags;
3647 * set data into sadb_address.
3649 static struct mbuf *
3650 key_setsadbaddr(u_int16_t exttype, const struct sockaddr *saddr,
3651 u_int8_t prefixlen, u_int16_t ul_proto)
3654 struct sadb_address *p;
3657 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3658 PFKEY_ALIGN8(saddr->sa_len);
3659 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3664 p = mtod(m, struct sadb_address *);
3667 p->sadb_address_len = PFKEY_UNIT64(len);
3668 p->sadb_address_exttype = exttype;
3669 p->sadb_address_proto = ul_proto;
3670 if (prefixlen == FULLMASK) {
3671 switch (saddr->sa_family) {
3673 prefixlen = sizeof(struct in_addr) << 3;
3676 prefixlen = sizeof(struct in6_addr) << 3;
3682 p->sadb_address_prefixlen = prefixlen;
3683 p->sadb_address_reserved = 0;
3686 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3693 * set data into sadb_x_sa2.
3695 static struct mbuf *
3696 key_setsadbxsa2(u_int8_t mode, u_int32_t seq, u_int32_t reqid)
3699 struct sadb_x_sa2 *p;
3702 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3703 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3708 p = mtod(m, struct sadb_x_sa2 *);
3711 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3712 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3713 p->sadb_x_sa2_mode = mode;
3714 p->sadb_x_sa2_reserved1 = 0;
3715 p->sadb_x_sa2_reserved2 = 0;
3716 p->sadb_x_sa2_sequence = seq;
3717 p->sadb_x_sa2_reqid = reqid;
3724 * Set a type in sadb_x_nat_t_type.
3726 static struct mbuf *
3727 key_setsadbxtype(u_int16_t type)
3731 struct sadb_x_nat_t_type *p;
3733 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_type));
3735 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3740 p = mtod(m, struct sadb_x_nat_t_type *);
3743 p->sadb_x_nat_t_type_len = PFKEY_UNIT64(len);
3744 p->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
3745 p->sadb_x_nat_t_type_type = type;
3750 * Set a port in sadb_x_nat_t_port.
3751 * In contrast to default RFC 2367 behaviour, port is in network byte order.
3753 static struct mbuf *
3754 key_setsadbxport(u_int16_t port, u_int16_t type)
3758 struct sadb_x_nat_t_port *p;
3760 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_port));
3762 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3767 p = mtod(m, struct sadb_x_nat_t_port *);
3770 p->sadb_x_nat_t_port_len = PFKEY_UNIT64(len);
3771 p->sadb_x_nat_t_port_exttype = type;
3772 p->sadb_x_nat_t_port_port = port;
3778 * Get port from sockaddr. Port is in network byte order.
3781 key_portfromsaddr(struct sockaddr *sa)
3784 switch (sa->sa_family) {
3787 return ((struct sockaddr_in *)sa)->sin_port;
3791 return ((struct sockaddr_in6 *)sa)->sin6_port;
3794 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
3795 printf("DP %s unexpected address family %d\n",
3796 __func__, sa->sa_family));
3799 #endif /* IPSEC_NAT_T */
3802 * Set port in struct sockaddr. Port is in network byte order.
3805 key_porttosaddr(struct sockaddr *sa, u_int16_t port)
3808 switch (sa->sa_family) {
3811 ((struct sockaddr_in *)sa)->sin_port = port;
3816 ((struct sockaddr_in6 *)sa)->sin6_port = port;
3820 ipseclog((LOG_DEBUG, "%s: unexpected address family %d.\n",
3821 __func__, sa->sa_family));
3827 * set data into sadb_x_policy
3829 static struct mbuf *
3830 key_setsadbxpolicy(u_int16_t type, u_int8_t dir, u_int32_t id, u_int32_t priority)
3833 struct sadb_x_policy *p;
3836 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3837 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3842 p = mtod(m, struct sadb_x_policy *);
3845 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3846 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3847 p->sadb_x_policy_type = type;
3848 p->sadb_x_policy_dir = dir;
3849 p->sadb_x_policy_id = id;
3850 p->sadb_x_policy_priority = priority;
3856 /* Take a key message (sadb_key) from the socket and turn it into one
3857 * of the kernel's key structures (seckey).
3859 * IN: pointer to the src
3860 * OUT: NULL no more memory
3863 key_dup_keymsg(const struct sadb_key *src, u_int len,
3864 struct malloc_type *type)
3867 dst = (struct seckey *)malloc(sizeof(struct seckey), type, M_NOWAIT);
3869 dst->bits = src->sadb_key_bits;
3870 dst->key_data = (char *)malloc(len, type, M_NOWAIT);
3871 if (dst->key_data != NULL) {
3872 bcopy((const char *)src + sizeof(struct sadb_key),
3873 dst->key_data, len);
3875 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3881 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3888 /* Take a lifetime message (sadb_lifetime) passed in on a socket and
3889 * turn it into one of the kernel's lifetime structures (seclifetime).
3891 * IN: pointer to the destination, source and malloc type
3892 * OUT: NULL, no more memory
3895 static struct seclifetime *
3896 key_dup_lifemsg(const struct sadb_lifetime *src, struct malloc_type *type)
3898 struct seclifetime *dst = NULL;
3900 dst = (struct seclifetime *)malloc(sizeof(struct seclifetime),
3904 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3906 dst->allocations = src->sadb_lifetime_allocations;
3907 dst->bytes = src->sadb_lifetime_bytes;
3908 dst->addtime = src->sadb_lifetime_addtime;
3909 dst->usetime = src->sadb_lifetime_usetime;
3914 /* compare my own address
3915 * OUT: 1: true, i.e. my address.
3919 key_ismyaddr(struct sockaddr *sa)
3922 IPSEC_ASSERT(sa != NULL, ("null sockaddr"));
3923 switch (sa->sa_family) {
3926 return (in_localip(satosin(sa)->sin_addr));
3930 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3939 * compare my own address for IPv6.
3944 key_ismyaddr6(struct sockaddr_in6 *sin6)
3946 struct in6_addr in6;
3948 if (!IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
3949 return (in6_localip(&sin6->sin6_addr));
3951 /* Convert address into kernel-internal form */
3952 in6 = sin6->sin6_addr;
3953 in6.s6_addr16[1] = htons(sin6->sin6_scope_id & 0xffff);
3954 return (in6_localip(&in6));
3959 * compare two secasindex structure.
3960 * flag can specify to compare 2 saidxes.
3961 * compare two secasindex structure without both mode and reqid.
3962 * don't compare port.
3964 * saidx0: source, it can be in SAD.
3971 key_cmpsaidx(const struct secasindex *saidx0, const struct secasindex *saidx1,
3977 if (saidx0 == NULL && saidx1 == NULL)
3980 if (saidx0 == NULL || saidx1 == NULL)
3983 if (saidx0->proto != saidx1->proto)
3986 if (flag == CMP_EXACTLY) {
3987 if (saidx0->mode != saidx1->mode)
3989 if (saidx0->reqid != saidx1->reqid)
3991 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
3992 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
3996 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
3997 if (flag == CMP_MODE_REQID
3998 ||flag == CMP_REQID) {
4000 * If reqid of SPD is non-zero, unique SA is required.
4001 * The result must be of same reqid in this case.
4003 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
4007 if (flag == CMP_MODE_REQID) {
4008 if (saidx0->mode != IPSEC_MODE_ANY
4009 && saidx0->mode != saidx1->mode)
4015 * If NAT-T is enabled, check ports for tunnel mode.
4016 * Do not check ports if they are set to zero in the SPD.
4017 * Also do not do it for native transport mode, as there
4018 * is no port information available in the SP.
4020 if ((saidx1->mode == IPSEC_MODE_TUNNEL ||
4021 (saidx1->mode == IPSEC_MODE_TRANSPORT &&
4022 saidx1->proto == IPPROTO_ESP)) &&
4023 saidx1->src.sa.sa_family == AF_INET &&
4024 saidx1->dst.sa.sa_family == AF_INET &&
4025 ((const struct sockaddr_in *)(&saidx1->src))->sin_port &&
4026 ((const struct sockaddr_in *)(&saidx1->dst))->sin_port)
4028 #endif /* IPSEC_NAT_T */
4030 if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, chkport) != 0) {
4033 if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, chkport) != 0) {
4042 * compare two secindex structure exactly.
4044 * spidx0: source, it is often in SPD.
4045 * spidx1: object, it is often from PFKEY message.
4051 key_cmpspidx_exactly(struct secpolicyindex *spidx0,
4052 struct secpolicyindex *spidx1)
4055 if (spidx0 == NULL && spidx1 == NULL)
4058 if (spidx0 == NULL || spidx1 == NULL)
4061 if (spidx0->prefs != spidx1->prefs
4062 || spidx0->prefd != spidx1->prefd
4063 || spidx0->ul_proto != spidx1->ul_proto)
4066 return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
4067 key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
4071 * compare two secindex structure with mask.
4073 * spidx0: source, it is often in SPD.
4074 * spidx1: object, it is often from IP header.
4080 key_cmpspidx_withmask(struct secpolicyindex *spidx0,
4081 struct secpolicyindex *spidx1)
4084 if (spidx0 == NULL && spidx1 == NULL)
4087 if (spidx0 == NULL || spidx1 == NULL)
4090 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
4091 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
4092 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
4093 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
4096 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4097 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
4098 && spidx0->ul_proto != spidx1->ul_proto)
4101 switch (spidx0->src.sa.sa_family) {
4103 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
4104 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
4106 if (!key_bbcmp(&spidx0->src.sin.sin_addr,
4107 &spidx1->src.sin.sin_addr, spidx0->prefs))
4111 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
4112 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
4115 * scope_id check. if sin6_scope_id is 0, we regard it
4116 * as a wildcard scope, which matches any scope zone ID.
4118 if (spidx0->src.sin6.sin6_scope_id &&
4119 spidx1->src.sin6.sin6_scope_id &&
4120 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
4122 if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
4123 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
4128 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
4133 switch (spidx0->dst.sa.sa_family) {
4135 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
4136 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
4138 if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
4139 &spidx1->dst.sin.sin_addr, spidx0->prefd))
4143 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
4144 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
4147 * scope_id check. if sin6_scope_id is 0, we regard it
4148 * as a wildcard scope, which matches any scope zone ID.
4150 if (spidx0->dst.sin6.sin6_scope_id &&
4151 spidx1->dst.sin6.sin6_scope_id &&
4152 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
4154 if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
4155 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
4160 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
4165 /* XXX Do we check other field ? e.g. flowinfo */
4170 /* returns 0 on match */
4172 key_sockaddrcmp(const struct sockaddr *sa1, const struct sockaddr *sa2,
4178 #define satosin(s) ((const struct sockaddr_in *)s)
4182 #define satosin6(s) ((const struct sockaddr_in6 *)s)
4183 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
4186 switch (sa1->sa_family) {
4188 if (sa1->sa_len != sizeof(struct sockaddr_in))
4190 if (satosin(sa1)->sin_addr.s_addr !=
4191 satosin(sa2)->sin_addr.s_addr) {
4194 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
4198 if (sa1->sa_len != sizeof(struct sockaddr_in6))
4199 return 1; /*EINVAL*/
4200 if (satosin6(sa1)->sin6_scope_id !=
4201 satosin6(sa2)->sin6_scope_id) {
4204 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
4205 &satosin6(sa2)->sin6_addr)) {
4209 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
4214 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
4225 * compare two buffers with mask.
4229 * bits: Number of bits to compare
4235 key_bbcmp(const void *a1, const void *a2, u_int bits)
4237 const unsigned char *p1 = a1;
4238 const unsigned char *p2 = a2;
4240 /* XXX: This could be considerably faster if we compare a word
4241 * at a time, but it is complicated on LSB Endian machines */
4243 /* Handle null pointers */
4244 if (p1 == NULL || p2 == NULL)
4254 u_int8_t mask = ~((1<<(8-bits))-1);
4255 if ((*p1 & mask) != (*p2 & mask))
4258 return 1; /* Match! */
4262 key_flush_spd(time_t now)
4264 SPTREE_RLOCK_TRACKER;
4265 struct secpolicy *sp;
4269 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4272 TAILQ_FOREACH(sp, &V_sptree[dir], chain) {
4273 if (sp->lifetime == 0 && sp->validtime == 0)
4275 if ((sp->lifetime &&
4276 now - sp->created > sp->lifetime) ||
4278 now - sp->lastused > sp->validtime)) {
4292 key_flush_sad(time_t now)
4294 struct secashead *sah, *nextsah;
4295 struct secasvar *sav, *nextsav;
4299 LIST_FOREACH_SAFE(sah, &V_sahtree, chain, nextsah) {
4300 /* if sah has been dead, then delete it and process next sah. */
4301 if (sah->state == SADB_SASTATE_DEAD) {
4306 /* if LARVAL entry doesn't become MATURE, delete it. */
4307 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_LARVAL], chain, nextsav) {
4308 /* Need to also check refcnt for a larval SA ??? */
4309 if (now - sav->created > V_key_larval_lifetime)
4314 * check MATURE entry to start to send expire message
4317 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_MATURE], chain, nextsav) {
4318 /* we don't need to check. */
4319 if (sav->lft_s == NULL)
4323 if (sav->lft_c == NULL) {
4324 ipseclog((LOG_DEBUG,"%s: there is no CURRENT "
4325 "time, why?\n", __func__));
4330 * HARD lifetimes MUST take precedence over SOFT
4331 * lifetimes, meaning if the HARD and SOFT lifetimes
4332 * are the same, the HARD lifetime will appear on the
4335 /* check HARD lifetime */
4336 if ((sav->lft_h->addtime != 0 &&
4337 now - sav->created > sav->lft_h->addtime) ||
4338 (sav->lft_h->bytes != 0 &&
4339 sav->lft_h->bytes < sav->lft_c->bytes)) {
4340 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4344 /* check SOFT lifetime */
4345 else if ((sav->lft_s->addtime != 0 &&
4346 now - sav->created > sav->lft_s->addtime) ||
4347 (sav->lft_s->bytes != 0 &&
4348 sav->lft_s->bytes < sav->lft_c->bytes)) {
4349 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4354 /* check DYING entry to change status to DEAD. */
4355 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DYING], chain, nextsav) {
4356 /* we don't need to check. */
4357 if (sav->lft_h == NULL)
4361 if (sav->lft_c == NULL) {
4362 ipseclog((LOG_DEBUG, "%s: there is no CURRENT "
4363 "time, why?\n", __func__));
4367 if (sav->lft_h->addtime != 0 &&
4368 now - sav->created > sav->lft_h->addtime) {
4369 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4373 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4374 else if (sav->lft_s != NULL
4375 && sav->lft_s->addtime != 0
4376 && now - sav->created > sav->lft_s->addtime) {
4378 * XXX: should be checked to be
4379 * installed the valid SA.
4383 * If there is no SA then sending
4389 /* check HARD lifetime by bytes */
4390 else if (sav->lft_h->bytes != 0 &&
4391 sav->lft_h->bytes < sav->lft_c->bytes) {
4392 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4398 /* delete entry in DEAD */
4399 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DEAD], chain, nextsav) {
4401 if (sav->state != SADB_SASTATE_DEAD) {
4402 ipseclog((LOG_DEBUG, "%s: invalid sav->state "
4403 "(queue: %d SA: %d): kill it anyway\n",
4405 SADB_SASTATE_DEAD, sav->state));
4408 * do not call key_freesav() here.
4409 * sav should already be freed, and sav->refcnt
4410 * shows other references to sav
4411 * (such as from SPD).
4419 key_flush_acq(time_t now)
4421 struct secacq *acq, *nextacq;
4425 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
4426 nextacq = LIST_NEXT(acq, chain);
4427 if (now - acq->created > V_key_blockacq_lifetime
4428 && __LIST_CHAINED(acq)) {
4429 LIST_REMOVE(acq, chain);
4430 free(acq, M_IPSEC_SAQ);
4437 key_flush_spacq(time_t now)
4439 struct secspacq *acq, *nextacq;
4443 for (acq = LIST_FIRST(&V_spacqtree); acq != NULL; acq = nextacq) {
4444 nextacq = LIST_NEXT(acq, chain);
4445 if (now - acq->created > V_key_blockacq_lifetime
4446 && __LIST_CHAINED(acq)) {
4447 LIST_REMOVE(acq, chain);
4448 free(acq, M_IPSEC_SAQ);
4456 * scanning SPD and SAD to check status for each entries,
4457 * and do to remove or to expire.
4458 * XXX: year 2038 problem may remain.
4461 key_timehandler(void *arg)
4463 VNET_ITERATOR_DECL(vnet_iter);
4464 time_t now = time_second;
4466 VNET_LIST_RLOCK_NOSLEEP();
4467 VNET_FOREACH(vnet_iter) {
4468 CURVNET_SET(vnet_iter);
4472 key_flush_spacq(now);
4475 VNET_LIST_RUNLOCK_NOSLEEP();
4477 #ifndef IPSEC_DEBUG2
4478 /* do exchange to tick time !! */
4479 callout_schedule(&key_timer, hz);
4480 #endif /* IPSEC_DEBUG2 */
4488 key_randomfill(&value, sizeof(value));
4493 key_randomfill(void *p, size_t l)
4497 static int warn = 1;
4500 n = (size_t)read_random(p, (u_int)l);
4504 bcopy(&v, (u_int8_t *)p + n,
4505 l - n < sizeof(v) ? l - n : sizeof(v));
4509 printf("WARNING: pseudo-random number generator "
4510 "used for IPsec processing\n");
4517 * map SADB_SATYPE_* to IPPROTO_*.
4518 * if satype == SADB_SATYPE then satype is mapped to ~0.
4520 * 0: invalid satype.
4523 key_satype2proto(u_int8_t satype)
4526 case SADB_SATYPE_UNSPEC:
4527 return IPSEC_PROTO_ANY;
4528 case SADB_SATYPE_AH:
4530 case SADB_SATYPE_ESP:
4532 case SADB_X_SATYPE_IPCOMP:
4533 return IPPROTO_IPCOMP;
4534 case SADB_X_SATYPE_TCPSIGNATURE:
4543 * map IPPROTO_* to SADB_SATYPE_*
4545 * 0: invalid protocol type.
4548 key_proto2satype(u_int16_t proto)
4552 return SADB_SATYPE_AH;
4554 return SADB_SATYPE_ESP;
4555 case IPPROTO_IPCOMP:
4556 return SADB_X_SATYPE_IPCOMP;
4558 return SADB_X_SATYPE_TCPSIGNATURE;
4567 * SADB_GETSPI processing is to receive
4568 * <base, (SA2), src address, dst address, (SPI range)>
4569 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4570 * tree with the status of LARVAL, and send
4571 * <base, SA(*), address(SD)>
4574 * IN: mhp: pointer to the pointer to each header.
4575 * OUT: NULL if fail.
4576 * other if success, return pointer to the message to send.
4579 key_getspi(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
4581 struct sadb_address *src0, *dst0;
4582 struct secasindex saidx;
4583 struct secashead *newsah;
4584 struct secasvar *newsav;
4591 IPSEC_ASSERT(so != NULL, ("null socket"));
4592 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4593 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4594 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4596 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4597 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4598 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4600 return key_senderror(so, m, EINVAL);
4602 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4603 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4604 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4606 return key_senderror(so, m, EINVAL);
4608 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4609 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4610 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4612 mode = IPSEC_MODE_ANY;
4616 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4617 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
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);
4627 * Make sure the port numbers are zero.
4628 * In case of NAT-T we will update them later if needed.
4630 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4632 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4633 sizeof(struct sockaddr_in))
4634 return key_senderror(so, m, EINVAL);
4635 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4638 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4639 sizeof(struct sockaddr_in6))
4640 return key_senderror(so, m, EINVAL);
4641 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4646 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4648 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4649 sizeof(struct sockaddr_in))
4650 return key_senderror(so, m, EINVAL);
4651 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4654 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4655 sizeof(struct sockaddr_in6))
4656 return key_senderror(so, m, EINVAL);
4657 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4663 /* XXX boundary check against sa_len */
4664 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4668 * Handle NAT-T info if present.
4669 * We made sure the port numbers are zero above, so we do
4670 * not have to worry in case we do not update them.
4672 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL)
4673 ipseclog((LOG_DEBUG, "%s: NAT-T OAi present\n", __func__));
4674 if (mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL)
4675 ipseclog((LOG_DEBUG, "%s: NAT-T OAr present\n", __func__));
4677 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
4678 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
4679 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
4680 struct sadb_x_nat_t_type *type;
4681 struct sadb_x_nat_t_port *sport, *dport;
4683 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
4684 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
4685 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
4686 ipseclog((LOG_DEBUG, "%s: invalid nat-t message "
4687 "passed.\n", __func__));
4688 return key_senderror(so, m, EINVAL);
4691 sport = (struct sadb_x_nat_t_port *)
4692 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
4693 dport = (struct sadb_x_nat_t_port *)
4694 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
4697 KEY_PORTTOSADDR(&saidx.src, sport->sadb_x_nat_t_port_port);
4699 KEY_PORTTOSADDR(&saidx.dst, dport->sadb_x_nat_t_port_port);
4703 /* SPI allocation */
4704 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4707 return key_senderror(so, m, EINVAL);
4709 /* get a SA index */
4710 if ((newsah = key_getsah(&saidx)) == NULL) {
4711 /* create a new SA index */
4712 if ((newsah = key_newsah(&saidx)) == NULL) {
4713 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
4714 return key_senderror(so, m, ENOBUFS);
4720 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4721 if (newsav == NULL) {
4722 /* XXX don't free new SA index allocated in above. */
4723 return key_senderror(so, m, error);
4727 newsav->spi = htonl(spi);
4729 /* delete the entry in acqtree */
4730 if (mhp->msg->sadb_msg_seq != 0) {
4732 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4733 /* reset counter in order to deletion by timehandler. */
4734 acq->created = time_second;
4740 struct mbuf *n, *nn;
4741 struct sadb_sa *m_sa;
4742 struct sadb_msg *newmsg;
4745 /* create new sadb_msg to reply. */
4746 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4747 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4749 MGETHDR(n, M_NOWAIT, MT_DATA);
4751 if (!(MCLGET(n, M_NOWAIT))) {
4757 return key_senderror(so, m, ENOBUFS);
4763 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4764 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4766 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4767 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4768 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4769 m_sa->sadb_sa_spi = htonl(spi);
4770 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4772 IPSEC_ASSERT(off == len,
4773 ("length inconsistency (off %u len %u)", off, len));
4775 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4776 SADB_EXT_ADDRESS_DST);
4779 return key_senderror(so, m, ENOBUFS);
4782 if (n->m_len < sizeof(struct sadb_msg)) {
4783 n = m_pullup(n, sizeof(struct sadb_msg));
4785 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4788 n->m_pkthdr.len = 0;
4789 for (nn = n; nn; nn = nn->m_next)
4790 n->m_pkthdr.len += nn->m_len;
4792 newmsg = mtod(n, struct sadb_msg *);
4793 newmsg->sadb_msg_seq = newsav->seq;
4794 newmsg->sadb_msg_errno = 0;
4795 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4798 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4803 * allocating new SPI
4804 * called by key_getspi().
4810 key_do_getnewspi(struct sadb_spirange *spirange, struct secasindex *saidx)
4814 int count = V_key_spi_trycnt;
4816 /* set spi range to allocate */
4817 if (spirange != NULL) {
4818 min = spirange->sadb_spirange_min;
4819 max = spirange->sadb_spirange_max;
4821 min = V_key_spi_minval;
4822 max = V_key_spi_maxval;
4824 /* IPCOMP needs 2-byte SPI */
4825 if (saidx->proto == IPPROTO_IPCOMP) {
4832 t = min; min = max; max = t;
4837 if (key_checkspidup(saidx, min) != NULL) {
4838 ipseclog((LOG_DEBUG, "%s: SPI %u exists already.\n",
4843 count--; /* taking one cost. */
4851 /* when requesting to allocate spi ranged */
4853 /* generate pseudo-random SPI value ranged. */
4854 newspi = min + (key_random() % (max - min + 1));
4856 if (key_checkspidup(saidx, newspi) == NULL)
4860 if (count == 0 || newspi == 0) {
4861 ipseclog((LOG_DEBUG, "%s: to allocate spi is failed.\n",
4868 keystat.getspi_count =
4869 (keystat.getspi_count + V_key_spi_trycnt - count) / 2;
4875 * SADB_UPDATE processing
4877 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4878 * key(AE), (identity(SD),) (sensitivity)>
4879 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4881 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4882 * (identity(SD),) (sensitivity)>
4885 * m will always be freed.
4888 key_update(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
4890 struct sadb_sa *sa0;
4891 struct sadb_address *src0, *dst0;
4893 struct sadb_x_nat_t_type *type;
4894 struct sadb_x_nat_t_port *sport, *dport;
4895 struct sadb_address *iaddr, *raddr;
4896 struct sadb_x_nat_t_frag *frag;
4898 struct secasindex saidx;
4899 struct secashead *sah;
4900 struct secasvar *sav;
4906 IPSEC_ASSERT(so != NULL, ("null socket"));
4907 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4908 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4909 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4911 /* map satype to proto */
4912 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4913 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4915 return key_senderror(so, m, EINVAL);
4918 if (mhp->ext[SADB_EXT_SA] == NULL ||
4919 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4920 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4921 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4922 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4923 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4924 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4925 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4926 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4927 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4928 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4929 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4931 return key_senderror(so, m, EINVAL);
4933 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4934 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4935 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4936 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4938 return key_senderror(so, m, EINVAL);
4940 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4941 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4942 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4944 mode = IPSEC_MODE_ANY;
4947 /* XXX boundary checking for other extensions */
4949 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4950 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4951 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4953 /* XXX boundary check against sa_len */
4954 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4957 * Make sure the port numbers are zero.
4958 * In case of NAT-T we will update them later if needed.
4960 KEY_PORTTOSADDR(&saidx.src, 0);
4961 KEY_PORTTOSADDR(&saidx.dst, 0);
4965 * Handle NAT-T info if present.
4967 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
4968 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
4969 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
4971 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
4972 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
4973 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
4974 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
4976 return key_senderror(so, m, EINVAL);
4979 type = (struct sadb_x_nat_t_type *)
4980 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
4981 sport = (struct sadb_x_nat_t_port *)
4982 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
4983 dport = (struct sadb_x_nat_t_port *)
4984 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
4987 sport = dport = NULL;
4989 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
4990 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
4991 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
4992 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
4993 ipseclog((LOG_DEBUG, "%s: invalid message\n",
4995 return key_senderror(so, m, EINVAL);
4997 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
4998 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
4999 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
5001 iaddr = raddr = NULL;
5003 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5004 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5005 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5007 return key_senderror(so, m, EINVAL);
5009 frag = (struct sadb_x_nat_t_frag *)
5010 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5016 /* get a SA header */
5017 if ((sah = key_getsah(&saidx)) == NULL) {
5018 ipseclog((LOG_DEBUG, "%s: no SA index found.\n", __func__));
5019 return key_senderror(so, m, ENOENT);
5022 /* set spidx if there */
5024 error = key_setident(sah, m, mhp);
5026 return key_senderror(so, m, error);
5028 /* find a SA with sequence number. */
5029 #ifdef IPSEC_DOSEQCHECK
5030 if (mhp->msg->sadb_msg_seq != 0
5031 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
5032 ipseclog((LOG_DEBUG, "%s: no larval SA with sequence %u "
5033 "exists.\n", __func__, mhp->msg->sadb_msg_seq));
5034 return key_senderror(so, m, ENOENT);
5038 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5041 ipseclog((LOG_DEBUG, "%s: no such a SA found (spi:%u)\n",
5042 __func__, (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5043 return key_senderror(so, m, EINVAL);
5047 /* validity check */
5048 if (sav->sah->saidx.proto != proto) {
5049 ipseclog((LOG_DEBUG, "%s: protocol mismatched "
5050 "(DB=%u param=%u)\n", __func__,
5051 sav->sah->saidx.proto, proto));
5052 return key_senderror(so, m, EINVAL);
5054 #ifdef IPSEC_DOSEQCHECK
5055 if (sav->spi != sa0->sadb_sa_spi) {
5056 ipseclog((LOG_DEBUG, "%s: SPI mismatched (DB:%u param:%u)\n",
5058 (u_int32_t)ntohl(sav->spi),
5059 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5060 return key_senderror(so, m, EINVAL);
5063 if (sav->pid != mhp->msg->sadb_msg_pid) {
5064 ipseclog((LOG_DEBUG, "%s: pid mismatched (DB:%u param:%u)\n",
5065 __func__, sav->pid, mhp->msg->sadb_msg_pid));
5066 return key_senderror(so, m, EINVAL);
5069 /* copy sav values */
5070 error = key_setsaval(sav, m, mhp);
5073 return key_senderror(so, m, error);
5078 * Handle more NAT-T info if present,
5079 * now that we have a sav to fill.
5082 sav->natt_type = type->sadb_x_nat_t_type_type;
5085 KEY_PORTTOSADDR(&sav->sah->saidx.src,
5086 sport->sadb_x_nat_t_port_port);
5088 KEY_PORTTOSADDR(&sav->sah->saidx.dst,
5089 dport->sadb_x_nat_t_port_port);
5093 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5094 * We should actually check for a minimum MTU here, if we
5095 * want to support it in ip_output.
5098 sav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5102 /* check SA values to be mature. */
5103 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
5105 return key_senderror(so, m, 0);
5111 /* set msg buf from mhp */
5112 n = key_getmsgbuf_x1(m, mhp);
5114 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5115 return key_senderror(so, m, ENOBUFS);
5119 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5124 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
5125 * only called by key_update().
5128 * others : found, pointer to a SA.
5130 #ifdef IPSEC_DOSEQCHECK
5131 static struct secasvar *
5132 key_getsavbyseq(struct secashead *sah, u_int32_t seq)
5134 struct secasvar *sav;
5137 state = SADB_SASTATE_LARVAL;
5139 /* search SAD with sequence number ? */
5140 LIST_FOREACH(sav, &sah->savtree[state], chain) {
5142 KEY_CHKSASTATE(state, sav->state, __func__);
5144 if (sav->seq == seq) {
5146 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
5147 printf("DP %s cause refcnt++:%d SA:%p\n",
5148 __func__, sav->refcnt, sav));
5158 * SADB_ADD processing
5159 * add an entry to SA database, when received
5160 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5161 * key(AE), (identity(SD),) (sensitivity)>
5164 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5165 * (identity(SD),) (sensitivity)>
5168 * IGNORE identity and sensitivity messages.
5170 * m will always be freed.
5173 key_add(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
5175 struct sadb_sa *sa0;
5176 struct sadb_address *src0, *dst0;
5178 struct sadb_x_nat_t_type *type;
5179 struct sadb_address *iaddr, *raddr;
5180 struct sadb_x_nat_t_frag *frag;
5182 struct secasindex saidx;
5183 struct secashead *newsah;
5184 struct secasvar *newsav;
5190 IPSEC_ASSERT(so != NULL, ("null socket"));
5191 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5192 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5193 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5195 /* map satype to proto */
5196 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5197 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5199 return key_senderror(so, m, EINVAL);
5202 if (mhp->ext[SADB_EXT_SA] == NULL ||
5203 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5204 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5205 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5206 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5207 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5208 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5209 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5210 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5211 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5212 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5213 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5215 return key_senderror(so, m, EINVAL);
5217 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5218 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5219 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5221 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5223 return key_senderror(so, m, EINVAL);
5225 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5226 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5227 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5229 mode = IPSEC_MODE_ANY;
5233 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5234 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5235 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5237 /* XXX boundary check against sa_len */
5238 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5241 * Make sure the port numbers are zero.
5242 * In case of NAT-T we will update them later if needed.
5244 KEY_PORTTOSADDR(&saidx.src, 0);
5245 KEY_PORTTOSADDR(&saidx.dst, 0);
5249 * Handle NAT-T info if present.
5251 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
5252 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5253 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5254 struct sadb_x_nat_t_port *sport, *dport;
5256 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
5257 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5258 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5259 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5261 return key_senderror(so, m, EINVAL);
5264 type = (struct sadb_x_nat_t_type *)
5265 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5266 sport = (struct sadb_x_nat_t_port *)
5267 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5268 dport = (struct sadb_x_nat_t_port *)
5269 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5272 KEY_PORTTOSADDR(&saidx.src,
5273 sport->sadb_x_nat_t_port_port);
5275 KEY_PORTTOSADDR(&saidx.dst,
5276 dport->sadb_x_nat_t_port_port);
5280 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
5281 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
5282 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
5283 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
5284 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5286 return key_senderror(so, m, EINVAL);
5288 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5289 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5290 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
5292 iaddr = raddr = NULL;
5294 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5295 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5296 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5298 return key_senderror(so, m, EINVAL);
5300 frag = (struct sadb_x_nat_t_frag *)
5301 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5307 /* get a SA header */
5308 if ((newsah = key_getsah(&saidx)) == NULL) {
5309 /* create a new SA header */
5310 if ((newsah = key_newsah(&saidx)) == NULL) {
5311 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
5312 return key_senderror(so, m, ENOBUFS);
5316 /* set spidx if there */
5318 error = key_setident(newsah, m, mhp);
5320 return key_senderror(so, m, error);
5323 /* create new SA entry. */
5324 /* We can create new SA only if SPI is differenct. */
5326 newsav = key_getsavbyspi(newsah, sa0->sadb_sa_spi);
5328 if (newsav != NULL) {
5329 ipseclog((LOG_DEBUG, "%s: SA already exists.\n", __func__));
5330 return key_senderror(so, m, EEXIST);
5332 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
5333 if (newsav == NULL) {
5334 return key_senderror(so, m, error);
5339 * Handle more NAT-T info if present,
5340 * now that we have a sav to fill.
5343 newsav->natt_type = type->sadb_x_nat_t_type_type;
5347 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5348 * We should actually check for a minimum MTU here, if we
5349 * want to support it in ip_output.
5352 newsav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5356 /* check SA values to be mature. */
5357 if ((error = key_mature(newsav)) != 0) {
5358 KEY_FREESAV(&newsav);
5359 return key_senderror(so, m, error);
5363 * don't call key_freesav() here, as we would like to keep the SA
5364 * in the database on success.
5370 /* set msg buf from mhp */
5371 n = key_getmsgbuf_x1(m, mhp);
5373 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5374 return key_senderror(so, m, ENOBUFS);
5378 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5384 key_setident(struct secashead *sah, struct mbuf *m,
5385 const struct sadb_msghdr *mhp)
5387 const struct sadb_ident *idsrc, *iddst;
5388 int idsrclen, iddstlen;
5390 IPSEC_ASSERT(sah != NULL, ("null secashead"));
5391 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5392 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5393 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5395 /* don't make buffer if not there */
5396 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
5397 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5403 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
5404 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5405 ipseclog((LOG_DEBUG, "%s: invalid identity.\n", __func__));
5409 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
5410 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
5411 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
5412 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
5414 /* validity check */
5415 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
5416 ipseclog((LOG_DEBUG, "%s: ident type mismatch.\n", __func__));
5420 switch (idsrc->sadb_ident_type) {
5421 case SADB_IDENTTYPE_PREFIX:
5422 case SADB_IDENTTYPE_FQDN:
5423 case SADB_IDENTTYPE_USERFQDN:
5425 /* XXX do nothing */
5431 /* make structure */
5432 sah->idents = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5433 if (sah->idents == NULL) {
5434 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5437 sah->identd = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5438 if (sah->identd == NULL) {
5439 free(sah->idents, M_IPSEC_MISC);
5441 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5444 sah->idents->type = idsrc->sadb_ident_type;
5445 sah->idents->id = idsrc->sadb_ident_id;
5447 sah->identd->type = iddst->sadb_ident_type;
5448 sah->identd->id = iddst->sadb_ident_id;
5454 * m will not be freed on return.
5455 * it is caller's responsibility to free the result.
5457 static struct mbuf *
5458 key_getmsgbuf_x1(struct mbuf *m, const struct sadb_msghdr *mhp)
5462 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5463 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5464 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5466 /* create new sadb_msg to reply. */
5467 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5468 SADB_EXT_SA, SADB_X_EXT_SA2,
5469 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5470 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5471 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5475 if (n->m_len < sizeof(struct sadb_msg)) {
5476 n = m_pullup(n, sizeof(struct sadb_msg));
5480 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5481 mtod(n, struct sadb_msg *)->sadb_msg_len =
5482 PFKEY_UNIT64(n->m_pkthdr.len);
5488 * SADB_DELETE processing
5490 * <base, SA(*), address(SD)>
5491 * from the ikmpd, and set SADB_SASTATE_DEAD,
5493 * <base, SA(*), address(SD)>
5496 * m will always be freed.
5499 key_delete(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
5501 struct sadb_sa *sa0;
5502 struct sadb_address *src0, *dst0;
5503 struct secasindex saidx;
5504 struct secashead *sah;
5505 struct secasvar *sav = NULL;
5508 IPSEC_ASSERT(so != NULL, ("null socket"));
5509 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5510 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5511 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5513 /* map satype to proto */
5514 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5515 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5517 return key_senderror(so, m, EINVAL);
5520 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5521 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5522 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5524 return key_senderror(so, m, EINVAL);
5527 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5528 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5529 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5531 return key_senderror(so, m, EINVAL);
5534 if (mhp->ext[SADB_EXT_SA] == NULL) {
5536 * Caller wants us to delete all non-LARVAL SAs
5537 * that match the src/dst. This is used during
5538 * IKE INITIAL-CONTACT.
5540 ipseclog((LOG_DEBUG, "%s: doing delete all.\n", __func__));
5541 return key_delete_all(so, m, mhp, proto);
5542 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5543 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5545 return key_senderror(so, m, EINVAL);
5548 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5549 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5550 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5552 /* XXX boundary check against sa_len */
5553 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5556 * Make sure the port numbers are zero.
5557 * In case of NAT-T we will update them later if needed.
5559 KEY_PORTTOSADDR(&saidx.src, 0);
5560 KEY_PORTTOSADDR(&saidx.dst, 0);
5564 * Handle NAT-T info if present.
5566 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5567 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5568 struct sadb_x_nat_t_port *sport, *dport;
5570 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5571 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5572 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5574 return key_senderror(so, m, EINVAL);
5577 sport = (struct sadb_x_nat_t_port *)
5578 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5579 dport = (struct sadb_x_nat_t_port *)
5580 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5583 KEY_PORTTOSADDR(&saidx.src,
5584 sport->sadb_x_nat_t_port_port);
5586 KEY_PORTTOSADDR(&saidx.dst,
5587 dport->sadb_x_nat_t_port_port);
5591 /* get a SA header */
5593 LIST_FOREACH(sah, &V_sahtree, chain) {
5594 if (sah->state == SADB_SASTATE_DEAD)
5596 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5599 /* get a SA with SPI. */
5600 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5606 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5607 return key_senderror(so, m, ENOENT);
5610 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5616 struct sadb_msg *newmsg;
5618 /* create new sadb_msg to reply. */
5619 /* XXX-BZ NAT-T extensions? */
5620 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5621 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5623 return key_senderror(so, m, ENOBUFS);
5625 if (n->m_len < sizeof(struct sadb_msg)) {
5626 n = m_pullup(n, sizeof(struct sadb_msg));
5628 return key_senderror(so, m, ENOBUFS);
5630 newmsg = mtod(n, struct sadb_msg *);
5631 newmsg->sadb_msg_errno = 0;
5632 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5635 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5640 * delete all SAs for src/dst. Called from key_delete().
5643 key_delete_all(struct socket *so, struct mbuf *m,
5644 const struct sadb_msghdr *mhp, u_int16_t proto)
5646 struct sadb_address *src0, *dst0;
5647 struct secasindex saidx;
5648 struct secashead *sah;
5649 struct secasvar *sav, *nextsav;
5650 u_int stateidx, state;
5652 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5653 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5655 /* XXX boundary check against sa_len */
5656 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5659 * Make sure the port numbers are zero.
5660 * In case of NAT-T we will update them later if needed.
5662 KEY_PORTTOSADDR(&saidx.src, 0);
5663 KEY_PORTTOSADDR(&saidx.dst, 0);
5667 * Handle NAT-T info if present.
5670 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5671 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5672 struct sadb_x_nat_t_port *sport, *dport;
5674 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5675 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5676 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5678 return key_senderror(so, m, EINVAL);
5681 sport = (struct sadb_x_nat_t_port *)
5682 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5683 dport = (struct sadb_x_nat_t_port *)
5684 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5687 KEY_PORTTOSADDR(&saidx.src,
5688 sport->sadb_x_nat_t_port_port);
5690 KEY_PORTTOSADDR(&saidx.dst,
5691 dport->sadb_x_nat_t_port_port);
5696 LIST_FOREACH(sah, &V_sahtree, chain) {
5697 if (sah->state == SADB_SASTATE_DEAD)
5699 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5702 /* Delete all non-LARVAL SAs. */
5704 stateidx < _ARRAYLEN(saorder_state_alive);
5706 state = saorder_state_alive[stateidx];
5707 if (state == SADB_SASTATE_LARVAL)
5709 for (sav = LIST_FIRST(&sah->savtree[state]);
5710 sav != NULL; sav = nextsav) {
5711 nextsav = LIST_NEXT(sav, chain);
5713 if (sav->state != state) {
5714 ipseclog((LOG_DEBUG, "%s: invalid "
5715 "sav->state (queue %d SA %d)\n",
5716 __func__, state, sav->state));
5720 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5728 struct sadb_msg *newmsg;
5730 /* create new sadb_msg to reply. */
5731 /* XXX-BZ NAT-T extensions? */
5732 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5733 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5735 return key_senderror(so, m, ENOBUFS);
5737 if (n->m_len < sizeof(struct sadb_msg)) {
5738 n = m_pullup(n, sizeof(struct sadb_msg));
5740 return key_senderror(so, m, ENOBUFS);
5742 newmsg = mtod(n, struct sadb_msg *);
5743 newmsg->sadb_msg_errno = 0;
5744 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5747 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5752 * SADB_GET processing
5754 * <base, SA(*), address(SD)>
5755 * from the ikmpd, and get a SP and a SA to respond,
5757 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5758 * (identity(SD),) (sensitivity)>
5761 * m will always be freed.
5764 key_get(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
5766 struct sadb_sa *sa0;
5767 struct sadb_address *src0, *dst0;
5768 struct secasindex saidx;
5769 struct secashead *sah;
5770 struct secasvar *sav = NULL;
5773 IPSEC_ASSERT(so != NULL, ("null socket"));
5774 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5775 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5776 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5778 /* map satype to proto */
5779 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5780 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5782 return key_senderror(so, m, EINVAL);
5785 if (mhp->ext[SADB_EXT_SA] == NULL ||
5786 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5787 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5788 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5790 return key_senderror(so, m, EINVAL);
5792 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5793 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5794 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5795 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5797 return key_senderror(so, m, EINVAL);
5800 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5801 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5802 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5804 /* XXX boundary check against sa_len */
5805 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5808 * Make sure the port numbers are zero.
5809 * In case of NAT-T we will update them later if needed.
5811 KEY_PORTTOSADDR(&saidx.src, 0);
5812 KEY_PORTTOSADDR(&saidx.dst, 0);
5816 * Handle NAT-T info if present.
5819 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5820 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5821 struct sadb_x_nat_t_port *sport, *dport;
5823 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5824 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5825 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5827 return key_senderror(so, m, EINVAL);
5830 sport = (struct sadb_x_nat_t_port *)
5831 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5832 dport = (struct sadb_x_nat_t_port *)
5833 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5836 KEY_PORTTOSADDR(&saidx.src,
5837 sport->sadb_x_nat_t_port_port);
5839 KEY_PORTTOSADDR(&saidx.dst,
5840 dport->sadb_x_nat_t_port_port);
5844 /* get a SA header */
5846 LIST_FOREACH(sah, &V_sahtree, chain) {
5847 if (sah->state == SADB_SASTATE_DEAD)
5849 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5852 /* get a SA with SPI. */
5853 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5859 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5860 return key_senderror(so, m, ENOENT);
5867 /* map proto to satype */
5868 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5869 ipseclog((LOG_DEBUG, "%s: there was invalid proto in SAD.\n",
5871 return key_senderror(so, m, EINVAL);
5874 /* create new sadb_msg to reply. */
5875 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5876 mhp->msg->sadb_msg_pid);
5878 return key_senderror(so, m, ENOBUFS);
5881 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5885 /* XXX make it sysctl-configurable? */
5887 key_getcomb_setlifetime(struct sadb_comb *comb)
5890 comb->sadb_comb_soft_allocations = 1;
5891 comb->sadb_comb_hard_allocations = 1;
5892 comb->sadb_comb_soft_bytes = 0;
5893 comb->sadb_comb_hard_bytes = 0;
5894 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5895 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5896 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5897 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
5901 * XXX reorder combinations by preference
5902 * XXX no idea if the user wants ESP authentication or not
5904 static struct mbuf *
5907 struct sadb_comb *comb;
5908 struct enc_xform *algo;
5909 struct mbuf *result = NULL, *m, *n;
5913 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5916 for (i = 1; i <= SADB_EALG_MAX; i++) {
5917 algo = esp_algorithm_lookup(i);
5921 /* discard algorithms with key size smaller than system min */
5922 if (_BITS(algo->maxkey) < V_ipsec_esp_keymin)
5924 if (_BITS(algo->minkey) < V_ipsec_esp_keymin)
5925 encmin = V_ipsec_esp_keymin;
5927 encmin = _BITS(algo->minkey);
5929 if (V_ipsec_esp_auth)
5930 m = key_getcomb_ah();
5932 IPSEC_ASSERT(l <= MLEN,
5933 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
5934 MGET(m, M_NOWAIT, MT_DATA);
5939 bzero(mtod(m, caddr_t), m->m_len);
5946 for (n = m; n; n = n->m_next)
5948 IPSEC_ASSERT((totlen % l) == 0, ("totlen=%u, l=%u", totlen, l));
5950 for (off = 0; off < totlen; off += l) {
5951 n = m_pulldown(m, off, l, &o);
5953 /* m is already freed */
5956 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
5957 bzero(comb, sizeof(*comb));
5958 key_getcomb_setlifetime(comb);
5959 comb->sadb_comb_encrypt = i;
5960 comb->sadb_comb_encrypt_minbits = encmin;
5961 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
5979 key_getsizes_ah(const struct auth_hash *ah, int alg, u_int16_t* min,
5983 *min = *max = ah->keysize;
5984 if (ah->keysize == 0) {
5986 * Transform takes arbitrary key size but algorithm
5987 * key size is restricted. Enforce this here.
5990 case SADB_X_AALG_MD5: *min = *max = 16; break;
5991 case SADB_X_AALG_SHA: *min = *max = 20; break;
5992 case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
5993 case SADB_X_AALG_SHA2_256: *min = *max = 32; break;
5994 case SADB_X_AALG_SHA2_384: *min = *max = 48; break;
5995 case SADB_X_AALG_SHA2_512: *min = *max = 64; break;
5997 DPRINTF(("%s: unknown AH algorithm %u\n",
6005 * XXX reorder combinations by preference
6007 static struct mbuf *
6010 struct sadb_comb *comb;
6011 struct auth_hash *algo;
6013 u_int16_t minkeysize, maxkeysize;
6015 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6018 for (i = 1; i <= SADB_AALG_MAX; i++) {
6020 /* we prefer HMAC algorithms, not old algorithms */
6021 if (i != SADB_AALG_SHA1HMAC &&
6022 i != SADB_AALG_MD5HMAC &&
6023 i != SADB_X_AALG_SHA2_256 &&
6024 i != SADB_X_AALG_SHA2_384 &&
6025 i != SADB_X_AALG_SHA2_512)
6028 algo = ah_algorithm_lookup(i);
6031 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
6032 /* discard algorithms with key size smaller than system min */
6033 if (_BITS(minkeysize) < V_ipsec_ah_keymin)
6037 IPSEC_ASSERT(l <= MLEN,
6038 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6039 MGET(m, M_NOWAIT, MT_DATA);
6046 M_PREPEND(m, l, M_NOWAIT);
6050 comb = mtod(m, struct sadb_comb *);
6051 bzero(comb, sizeof(*comb));
6052 key_getcomb_setlifetime(comb);
6053 comb->sadb_comb_auth = i;
6054 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
6055 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
6062 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
6063 * XXX reorder combinations by preference
6065 static struct mbuf *
6066 key_getcomb_ipcomp()
6068 struct sadb_comb *comb;
6069 struct comp_algo *algo;
6072 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6075 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
6076 algo = ipcomp_algorithm_lookup(i);
6081 IPSEC_ASSERT(l <= MLEN,
6082 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6083 MGET(m, M_NOWAIT, MT_DATA);
6090 M_PREPEND(m, l, M_NOWAIT);
6094 comb = mtod(m, struct sadb_comb *);
6095 bzero(comb, sizeof(*comb));
6096 key_getcomb_setlifetime(comb);
6097 comb->sadb_comb_encrypt = i;
6098 /* what should we set into sadb_comb_*_{min,max}bits? */
6105 * XXX no way to pass mode (transport/tunnel) to userland
6106 * XXX replay checking?
6107 * XXX sysctl interface to ipsec_{ah,esp}_keymin
6109 static struct mbuf *
6110 key_getprop(const struct secasindex *saidx)
6112 struct sadb_prop *prop;
6114 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
6117 switch (saidx->proto) {
6119 m = key_getcomb_esp();
6122 m = key_getcomb_ah();
6124 case IPPROTO_IPCOMP:
6125 m = key_getcomb_ipcomp();
6133 M_PREPEND(m, l, M_NOWAIT);
6138 for (n = m; n; n = n->m_next)
6141 prop = mtod(m, struct sadb_prop *);
6142 bzero(prop, sizeof(*prop));
6143 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
6144 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
6145 prop->sadb_prop_replay = 32; /* XXX */
6151 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
6153 * <base, SA, address(SD), (address(P)), x_policy,
6154 * (identity(SD),) (sensitivity,) proposal>
6155 * to KMD, and expect to receive
6156 * <base> with SADB_ACQUIRE if error occurred,
6158 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
6159 * from KMD by PF_KEY.
6161 * XXX x_policy is outside of RFC2367 (KAME extension).
6162 * XXX sensitivity is not supported.
6163 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
6164 * see comment for key_getcomb_ipcomp().
6168 * others: error number
6171 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
6173 union sockaddr_union addr;
6174 struct mbuf *result, *m;
6175 struct secacq *newacq;
6179 u_int8_t mask, satype;
6181 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
6182 satype = key_proto2satype(saidx->proto);
6183 IPSEC_ASSERT(satype != 0, ("null satype, protocol %u", saidx->proto));
6187 ul_proto = IPSEC_ULPROTO_ANY;
6189 * We never do anything about acquirng SA. There is anather
6190 * solution that kernel blocks to send SADB_ACQUIRE message until
6191 * getting something message from IKEd. In later case, to be
6192 * managed with ACQUIRING list.
6194 /* Get an entry to check whether sending message or not. */
6195 if ((newacq = key_getacq(saidx)) != NULL) {
6196 if (V_key_blockacq_count < newacq->count) {
6197 /* reset counter and do send message. */
6200 /* increment counter and do nothing. */
6205 /* make new entry for blocking to send SADB_ACQUIRE. */
6206 if ((newacq = key_newacq(saidx)) == NULL)
6212 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
6220 * No SADB_X_EXT_NAT_T_* here: we do not know
6221 * anything related to NAT-T at this time.
6225 * set sadb_address for saidx's.
6227 * Note that if sp is supplied, then we're being called from
6228 * key_checkrequest and should supply port and protocol information.
6230 if (sp != NULL && (sp->spidx.ul_proto == IPPROTO_TCP ||
6231 sp->spidx.ul_proto == IPPROTO_UDP))
6232 ul_proto = sp->spidx.ul_proto;
6236 if (ul_proto != IPSEC_ULPROTO_ANY) {
6237 switch (sp->spidx.src.sa.sa_family) {
6239 if (sp->spidx.src.sin.sin_port != IPSEC_PORT_ANY) {
6240 addr.sin.sin_port = sp->spidx.src.sin.sin_port;
6241 mask = sp->spidx.prefs;
6245 if (sp->spidx.src.sin6.sin6_port != IPSEC_PORT_ANY) {
6246 addr.sin6.sin6_port = sp->spidx.src.sin6.sin6_port;
6247 mask = sp->spidx.prefs;
6254 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, &addr.sa, mask, ul_proto);
6263 if (ul_proto != IPSEC_ULPROTO_ANY) {
6264 switch (sp->spidx.dst.sa.sa_family) {
6266 if (sp->spidx.dst.sin.sin_port != IPSEC_PORT_ANY) {
6267 addr.sin.sin_port = sp->spidx.dst.sin.sin_port;
6268 mask = sp->spidx.prefd;
6272 if (sp->spidx.dst.sin6.sin6_port != IPSEC_PORT_ANY) {
6273 addr.sin6.sin6_port = sp->spidx.dst.sin6.sin6_port;
6274 mask = sp->spidx.prefd;
6281 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, &addr.sa, mask, ul_proto);
6288 /* XXX proxy address (optional) */
6290 /* set sadb_x_policy */
6292 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id, sp->priority);
6300 /* XXX identity (optional) */
6302 if (idexttype && fqdn) {
6303 /* create identity extension (FQDN) */
6304 struct sadb_ident *id;
6307 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
6308 id = (struct sadb_ident *)p;
6309 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6310 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6311 id->sadb_ident_exttype = idexttype;
6312 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
6313 bcopy(fqdn, id + 1, fqdnlen);
6314 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
6318 /* create identity extension (USERFQDN) */
6319 struct sadb_ident *id;
6323 /* +1 for terminating-NUL */
6324 userfqdnlen = strlen(userfqdn) + 1;
6327 id = (struct sadb_ident *)p;
6328 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6329 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6330 id->sadb_ident_exttype = idexttype;
6331 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
6332 /* XXX is it correct? */
6333 if (curproc && curproc->p_cred)
6334 id->sadb_ident_id = curproc->p_cred->p_ruid;
6335 if (userfqdn && userfqdnlen)
6336 bcopy(userfqdn, id + 1, userfqdnlen);
6337 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
6341 /* XXX sensitivity (optional) */
6343 /* create proposal/combination extension */
6344 m = key_getprop(saidx);
6347 * spec conformant: always attach proposal/combination extension,
6348 * the problem is that we have no way to attach it for ipcomp,
6349 * due to the way sadb_comb is declared in RFC2367.
6358 * outside of spec; make proposal/combination extension optional.
6364 if ((result->m_flags & M_PKTHDR) == 0) {
6369 if (result->m_len < sizeof(struct sadb_msg)) {
6370 result = m_pullup(result, sizeof(struct sadb_msg));
6371 if (result == NULL) {
6377 result->m_pkthdr.len = 0;
6378 for (m = result; m; m = m->m_next)
6379 result->m_pkthdr.len += m->m_len;
6381 mtod(result, struct sadb_msg *)->sadb_msg_len =
6382 PFKEY_UNIT64(result->m_pkthdr.len);
6384 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6392 static struct secacq *
6393 key_newacq(const struct secasindex *saidx)
6395 struct secacq *newacq;
6398 newacq = malloc(sizeof(struct secacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6399 if (newacq == NULL) {
6400 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6405 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
6406 newacq->seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq);
6407 newacq->created = time_second;
6410 /* add to acqtree */
6412 LIST_INSERT_HEAD(&V_acqtree, newacq, chain);
6418 static struct secacq *
6419 key_getacq(const struct secasindex *saidx)
6424 LIST_FOREACH(acq, &V_acqtree, chain) {
6425 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
6433 static struct secacq *
6434 key_getacqbyseq(u_int32_t seq)
6439 LIST_FOREACH(acq, &V_acqtree, chain) {
6440 if (acq->seq == seq)
6448 static struct secspacq *
6449 key_newspacq(struct secpolicyindex *spidx)
6451 struct secspacq *acq;
6454 acq = malloc(sizeof(struct secspacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6456 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6461 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
6462 acq->created = time_second;
6465 /* add to spacqtree */
6467 LIST_INSERT_HEAD(&V_spacqtree, acq, chain);
6473 static struct secspacq *
6474 key_getspacq(struct secpolicyindex *spidx)
6476 struct secspacq *acq;
6479 LIST_FOREACH(acq, &V_spacqtree, chain) {
6480 if (key_cmpspidx_exactly(spidx, &acq->spidx)) {
6481 /* NB: return holding spacq_lock */
6491 * SADB_ACQUIRE processing,
6492 * in first situation, is receiving
6494 * from the ikmpd, and clear sequence of its secasvar entry.
6496 * In second situation, is receiving
6497 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6498 * from a user land process, and return
6499 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6502 * m will always be freed.
6505 key_acquire2(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
6507 const struct sadb_address *src0, *dst0;
6508 struct secasindex saidx;
6509 struct secashead *sah;
6513 IPSEC_ASSERT(so != NULL, ("null socket"));
6514 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6515 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6516 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6519 * Error message from KMd.
6520 * We assume that if error was occurred in IKEd, the length of PFKEY
6521 * message is equal to the size of sadb_msg structure.
6522 * We do not raise error even if error occurred in this function.
6524 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
6527 /* check sequence number */
6528 if (mhp->msg->sadb_msg_seq == 0) {
6529 ipseclog((LOG_DEBUG, "%s: must specify sequence "
6530 "number.\n", __func__));
6535 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
6537 * the specified larval SA is already gone, or we got
6538 * a bogus sequence number. we can silently ignore it.
6544 /* reset acq counter in order to deletion by timehander. */
6545 acq->created = time_second;
6552 * This message is from user land.
6555 /* map satype to proto */
6556 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6557 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6559 return key_senderror(so, m, EINVAL);
6562 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
6563 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
6564 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
6566 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6568 return key_senderror(so, m, EINVAL);
6570 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
6571 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
6572 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
6574 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6576 return key_senderror(so, m, EINVAL);
6579 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
6580 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
6582 /* XXX boundary check against sa_len */
6583 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
6586 * Make sure the port numbers are zero.
6587 * In case of NAT-T we will update them later if needed.
6589 KEY_PORTTOSADDR(&saidx.src, 0);
6590 KEY_PORTTOSADDR(&saidx.dst, 0);
6594 * Handle NAT-T info if present.
6597 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
6598 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
6599 struct sadb_x_nat_t_port *sport, *dport;
6601 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
6602 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
6603 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
6605 return key_senderror(so, m, EINVAL);
6608 sport = (struct sadb_x_nat_t_port *)
6609 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
6610 dport = (struct sadb_x_nat_t_port *)
6611 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
6614 KEY_PORTTOSADDR(&saidx.src,
6615 sport->sadb_x_nat_t_port_port);
6617 KEY_PORTTOSADDR(&saidx.dst,
6618 dport->sadb_x_nat_t_port_port);
6622 /* get a SA index */
6624 LIST_FOREACH(sah, &V_sahtree, chain) {
6625 if (sah->state == SADB_SASTATE_DEAD)
6627 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
6632 ipseclog((LOG_DEBUG, "%s: a SA exists already.\n", __func__));
6633 return key_senderror(so, m, EEXIST);
6636 error = key_acquire(&saidx, NULL);
6638 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
6639 __func__, mhp->msg->sadb_msg_errno));
6640 return key_senderror(so, m, error);
6643 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6647 * SADB_REGISTER processing.
6648 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6651 * from the ikmpd, and register a socket to send PF_KEY messages,
6655 * If socket is detached, must free from regnode.
6657 * m will always be freed.
6660 key_register(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
6662 struct secreg *reg, *newreg = NULL;
6664 IPSEC_ASSERT(so != NULL, ("null socket"));
6665 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6666 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6667 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6669 /* check for invalid register message */
6670 if (mhp->msg->sadb_msg_satype >= sizeof(V_regtree)/sizeof(V_regtree[0]))
6671 return key_senderror(so, m, EINVAL);
6673 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6674 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6677 /* check whether existing or not */
6679 LIST_FOREACH(reg, &V_regtree[mhp->msg->sadb_msg_satype], chain) {
6680 if (reg->so == so) {
6682 ipseclog((LOG_DEBUG, "%s: socket exists already.\n",
6684 return key_senderror(so, m, EEXIST);
6688 /* create regnode */
6689 newreg = malloc(sizeof(struct secreg), M_IPSEC_SAR, M_NOWAIT|M_ZERO);
6690 if (newreg == NULL) {
6692 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6693 return key_senderror(so, m, ENOBUFS);
6697 ((struct keycb *)sotorawcb(so))->kp_registered++;
6699 /* add regnode to regtree. */
6700 LIST_INSERT_HEAD(&V_regtree[mhp->msg->sadb_msg_satype], newreg, chain);
6706 struct sadb_msg *newmsg;
6707 struct sadb_supported *sup;
6708 u_int len, alen, elen;
6711 struct sadb_alg *alg;
6713 /* create new sadb_msg to reply. */
6715 for (i = 1; i <= SADB_AALG_MAX; i++) {
6716 if (ah_algorithm_lookup(i))
6717 alen += sizeof(struct sadb_alg);
6720 alen += sizeof(struct sadb_supported);
6722 for (i = 1; i <= SADB_EALG_MAX; i++) {
6723 if (esp_algorithm_lookup(i))
6724 elen += sizeof(struct sadb_alg);
6727 elen += sizeof(struct sadb_supported);
6729 len = sizeof(struct sadb_msg) + alen + elen;
6732 return key_senderror(so, m, ENOBUFS);
6734 MGETHDR(n, M_NOWAIT, MT_DATA);
6736 if (!(MCLGET(n, M_NOWAIT))) {
6742 return key_senderror(so, m, ENOBUFS);
6744 n->m_pkthdr.len = n->m_len = len;
6748 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6749 newmsg = mtod(n, struct sadb_msg *);
6750 newmsg->sadb_msg_errno = 0;
6751 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6752 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6754 /* for authentication algorithm */
6756 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6757 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6758 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6759 off += PFKEY_ALIGN8(sizeof(*sup));
6761 for (i = 1; i <= SADB_AALG_MAX; i++) {
6762 struct auth_hash *aalgo;
6763 u_int16_t minkeysize, maxkeysize;
6765 aalgo = ah_algorithm_lookup(i);
6768 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6769 alg->sadb_alg_id = i;
6770 alg->sadb_alg_ivlen = 0;
6771 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
6772 alg->sadb_alg_minbits = _BITS(minkeysize);
6773 alg->sadb_alg_maxbits = _BITS(maxkeysize);
6774 off += PFKEY_ALIGN8(sizeof(*alg));
6778 /* for encryption algorithm */
6780 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6781 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6782 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6783 off += PFKEY_ALIGN8(sizeof(*sup));
6785 for (i = 1; i <= SADB_EALG_MAX; i++) {
6786 struct enc_xform *ealgo;
6788 ealgo = esp_algorithm_lookup(i);
6791 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6792 alg->sadb_alg_id = i;
6793 alg->sadb_alg_ivlen = ealgo->ivsize;
6794 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
6795 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
6796 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6800 IPSEC_ASSERT(off == len,
6801 ("length assumption failed (off %u len %u)", off, len));
6804 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6809 * free secreg entry registered.
6810 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6813 key_freereg(struct socket *so)
6818 IPSEC_ASSERT(so != NULL, ("NULL so"));
6821 * check whether existing or not.
6822 * check all type of SA, because there is a potential that
6823 * one socket is registered to multiple type of SA.
6826 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6827 LIST_FOREACH(reg, &V_regtree[i], chain) {
6828 if (reg->so == so && __LIST_CHAINED(reg)) {
6829 LIST_REMOVE(reg, chain);
6830 free(reg, M_IPSEC_SAR);
6839 * SADB_EXPIRE processing
6841 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6843 * NOTE: We send only soft lifetime extension.
6846 * others : error number
6849 key_expire(struct secasvar *sav, int hard)
6852 struct mbuf *result = NULL, *m;
6855 struct sadb_lifetime *lt;
6857 IPSEC_ASSERT (sav != NULL, ("null sav"));
6858 IPSEC_ASSERT (sav->sah != NULL, ("null sa header"));
6860 /* set msg header */
6861 satype = key_proto2satype(sav->sah->saidx.proto);
6862 IPSEC_ASSERT(satype != 0, ("invalid proto, satype %u", satype));
6863 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6870 /* create SA extension */
6871 m = key_setsadbsa(sav);
6878 /* create SA extension */
6879 m = key_setsadbxsa2(sav->sah->saidx.mode,
6880 sav->replay ? sav->replay->count : 0,
6881 sav->sah->saidx.reqid);
6888 /* create lifetime extension (current and soft) */
6889 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6890 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
6897 bzero(mtod(m, caddr_t), len);
6898 lt = mtod(m, struct sadb_lifetime *);
6899 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6900 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6901 lt->sadb_lifetime_allocations = sav->lft_c->allocations;
6902 lt->sadb_lifetime_bytes = sav->lft_c->bytes;
6903 lt->sadb_lifetime_addtime = sav->lft_c->addtime;
6904 lt->sadb_lifetime_usetime = sav->lft_c->usetime;
6905 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6906 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6908 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
6909 lt->sadb_lifetime_allocations = sav->lft_h->allocations;
6910 lt->sadb_lifetime_bytes = sav->lft_h->bytes;
6911 lt->sadb_lifetime_addtime = sav->lft_h->addtime;
6912 lt->sadb_lifetime_usetime = sav->lft_h->usetime;
6914 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
6915 lt->sadb_lifetime_allocations = sav->lft_s->allocations;
6916 lt->sadb_lifetime_bytes = sav->lft_s->bytes;
6917 lt->sadb_lifetime_addtime = sav->lft_s->addtime;
6918 lt->sadb_lifetime_usetime = sav->lft_s->usetime;
6922 /* set sadb_address for source */
6923 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6924 &sav->sah->saidx.src.sa,
6925 FULLMASK, IPSEC_ULPROTO_ANY);
6932 /* set sadb_address for destination */
6933 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6934 &sav->sah->saidx.dst.sa,
6935 FULLMASK, IPSEC_ULPROTO_ANY);
6943 * XXX-BZ Handle NAT-T extensions here.
6946 if ((result->m_flags & M_PKTHDR) == 0) {
6951 if (result->m_len < sizeof(struct sadb_msg)) {
6952 result = m_pullup(result, sizeof(struct sadb_msg));
6953 if (result == NULL) {
6959 result->m_pkthdr.len = 0;
6960 for (m = result; m; m = m->m_next)
6961 result->m_pkthdr.len += m->m_len;
6963 mtod(result, struct sadb_msg *)->sadb_msg_len =
6964 PFKEY_UNIT64(result->m_pkthdr.len);
6966 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6975 * SADB_FLUSH processing
6978 * from the ikmpd, and free all entries in secastree.
6982 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6984 * m will always be freed.
6987 key_flush(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
6989 struct sadb_msg *newmsg;
6990 struct secashead *sah, *nextsah;
6991 struct secasvar *sav, *nextsav;
6996 IPSEC_ASSERT(so != NULL, ("null socket"));
6997 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6998 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7000 /* map satype to proto */
7001 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7002 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7004 return key_senderror(so, m, EINVAL);
7007 /* no SATYPE specified, i.e. flushing all SA. */
7009 for (sah = LIST_FIRST(&V_sahtree);
7012 nextsah = LIST_NEXT(sah, chain);
7014 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7015 && proto != sah->saidx.proto)
7019 stateidx < _ARRAYLEN(saorder_state_alive);
7021 state = saorder_state_any[stateidx];
7022 for (sav = LIST_FIRST(&sah->savtree[state]);
7026 nextsav = LIST_NEXT(sav, chain);
7028 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
7033 sah->state = SADB_SASTATE_DEAD;
7037 if (m->m_len < sizeof(struct sadb_msg) ||
7038 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
7039 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
7040 return key_senderror(so, m, ENOBUFS);
7046 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
7047 newmsg = mtod(m, struct sadb_msg *);
7048 newmsg->sadb_msg_errno = 0;
7049 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
7051 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7055 * SADB_DUMP processing
7056 * dump all entries including status of DEAD in SAD.
7059 * from the ikmpd, and dump all secasvar leaves
7064 * m will always be freed.
7067 key_dump(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
7069 struct secashead *sah;
7070 struct secasvar *sav;
7076 struct sadb_msg *newmsg;
7079 IPSEC_ASSERT(so != NULL, ("null socket"));
7080 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7081 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7082 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7084 /* map satype to proto */
7085 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7086 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7088 return key_senderror(so, m, EINVAL);
7091 /* count sav entries to be sent to the userland. */
7094 LIST_FOREACH(sah, &V_sahtree, chain) {
7095 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7096 && proto != sah->saidx.proto)
7100 stateidx < _ARRAYLEN(saorder_state_any);
7102 state = saorder_state_any[stateidx];
7103 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7111 return key_senderror(so, m, ENOENT);
7114 /* send this to the userland, one at a time. */
7116 LIST_FOREACH(sah, &V_sahtree, chain) {
7117 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7118 && proto != sah->saidx.proto)
7121 /* map proto to satype */
7122 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
7124 ipseclog((LOG_DEBUG, "%s: there was invalid proto in "
7125 "SAD.\n", __func__));
7126 return key_senderror(so, m, EINVAL);
7130 stateidx < _ARRAYLEN(saorder_state_any);
7132 state = saorder_state_any[stateidx];
7133 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7134 n = key_setdumpsa(sav, SADB_DUMP, satype,
7135 --cnt, mhp->msg->sadb_msg_pid);
7138 return key_senderror(so, m, ENOBUFS);
7140 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
7151 * SADB_X_PROMISC processing
7153 * m will always be freed.
7156 key_promisc(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
7160 IPSEC_ASSERT(so != NULL, ("null socket"));
7161 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7162 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7163 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7165 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7167 if (olen < sizeof(struct sadb_msg)) {
7169 return key_senderror(so, m, EINVAL);
7174 } else if (olen == sizeof(struct sadb_msg)) {
7175 /* enable/disable promisc mode */
7178 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
7179 return key_senderror(so, m, EINVAL);
7180 mhp->msg->sadb_msg_errno = 0;
7181 switch (mhp->msg->sadb_msg_satype) {
7184 kp->kp_promisc = mhp->msg->sadb_msg_satype;
7187 return key_senderror(so, m, EINVAL);
7190 /* send the original message back to everyone */
7191 mhp->msg->sadb_msg_errno = 0;
7192 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7194 /* send packet as is */
7196 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
7198 /* TODO: if sadb_msg_seq is specified, send to specific pid */
7199 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7203 static int (*key_typesw[])(struct socket *, struct mbuf *,
7204 const struct sadb_msghdr *) = {
7205 NULL, /* SADB_RESERVED */
7206 key_getspi, /* SADB_GETSPI */
7207 key_update, /* SADB_UPDATE */
7208 key_add, /* SADB_ADD */
7209 key_delete, /* SADB_DELETE */
7210 key_get, /* SADB_GET */
7211 key_acquire2, /* SADB_ACQUIRE */
7212 key_register, /* SADB_REGISTER */
7213 NULL, /* SADB_EXPIRE */
7214 key_flush, /* SADB_FLUSH */
7215 key_dump, /* SADB_DUMP */
7216 key_promisc, /* SADB_X_PROMISC */
7217 NULL, /* SADB_X_PCHANGE */
7218 key_spdadd, /* SADB_X_SPDUPDATE */
7219 key_spdadd, /* SADB_X_SPDADD */
7220 key_spddelete, /* SADB_X_SPDDELETE */
7221 key_spdget, /* SADB_X_SPDGET */
7222 NULL, /* SADB_X_SPDACQUIRE */
7223 key_spddump, /* SADB_X_SPDDUMP */
7224 key_spdflush, /* SADB_X_SPDFLUSH */
7225 key_spdadd, /* SADB_X_SPDSETIDX */
7226 NULL, /* SADB_X_SPDEXPIRE */
7227 key_spddelete2, /* SADB_X_SPDDELETE2 */
7231 * parse sadb_msg buffer to process PFKEYv2,
7232 * and create a data to response if needed.
7233 * I think to be dealed with mbuf directly.
7235 * msgp : pointer to pointer to a received buffer pulluped.
7236 * This is rewrited to response.
7237 * so : pointer to socket.
7239 * length for buffer to send to user process.
7242 key_parse(struct mbuf *m, struct socket *so)
7244 struct sadb_msg *msg;
7245 struct sadb_msghdr mh;
7250 IPSEC_ASSERT(so != NULL, ("null socket"));
7251 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7253 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
7254 KEYDEBUG(KEYDEBUG_KEY_DUMP,
7255 ipseclog((LOG_DEBUG, "%s: passed sadb_msg\n", __func__));
7259 if (m->m_len < sizeof(struct sadb_msg)) {
7260 m = m_pullup(m, sizeof(struct sadb_msg));
7264 msg = mtod(m, struct sadb_msg *);
7265 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
7266 target = KEY_SENDUP_ONE;
7268 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len != orglen) {
7269 ipseclog((LOG_DEBUG, "%s: invalid message length.\n",__func__));
7270 PFKEYSTAT_INC(out_invlen);
7275 if (msg->sadb_msg_version != PF_KEY_V2) {
7276 ipseclog((LOG_DEBUG, "%s: PF_KEY version %u is mismatched.\n",
7277 __func__, msg->sadb_msg_version));
7278 PFKEYSTAT_INC(out_invver);
7283 if (msg->sadb_msg_type > SADB_MAX) {
7284 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7285 __func__, msg->sadb_msg_type));
7286 PFKEYSTAT_INC(out_invmsgtype);
7291 /* for old-fashioned code - should be nuked */
7292 if (m->m_pkthdr.len > MCLBYTES) {
7299 MGETHDR(n, M_NOWAIT, MT_DATA);
7300 if (n && m->m_pkthdr.len > MHLEN) {
7301 if (!(MCLGET(n, M_NOWAIT))) {
7310 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
7311 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
7317 /* align the mbuf chain so that extensions are in contiguous region. */
7318 error = key_align(m, &mh);
7325 switch (msg->sadb_msg_satype) {
7326 case SADB_SATYPE_UNSPEC:
7327 switch (msg->sadb_msg_type) {
7335 ipseclog((LOG_DEBUG, "%s: must specify satype "
7336 "when msg type=%u.\n", __func__,
7337 msg->sadb_msg_type));
7338 PFKEYSTAT_INC(out_invsatype);
7343 case SADB_SATYPE_AH:
7344 case SADB_SATYPE_ESP:
7345 case SADB_X_SATYPE_IPCOMP:
7346 case SADB_X_SATYPE_TCPSIGNATURE:
7347 switch (msg->sadb_msg_type) {
7349 case SADB_X_SPDDELETE:
7351 case SADB_X_SPDDUMP:
7352 case SADB_X_SPDFLUSH:
7353 case SADB_X_SPDSETIDX:
7354 case SADB_X_SPDUPDATE:
7355 case SADB_X_SPDDELETE2:
7356 ipseclog((LOG_DEBUG, "%s: illegal satype=%u\n",
7357 __func__, msg->sadb_msg_type));
7358 PFKEYSTAT_INC(out_invsatype);
7363 case SADB_SATYPE_RSVP:
7364 case SADB_SATYPE_OSPFV2:
7365 case SADB_SATYPE_RIPV2:
7366 case SADB_SATYPE_MIP:
7367 ipseclog((LOG_DEBUG, "%s: type %u isn't supported.\n",
7368 __func__, msg->sadb_msg_satype));
7369 PFKEYSTAT_INC(out_invsatype);
7372 case 1: /* XXX: What does it do? */
7373 if (msg->sadb_msg_type == SADB_X_PROMISC)
7377 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7378 __func__, msg->sadb_msg_satype));
7379 PFKEYSTAT_INC(out_invsatype);
7384 /* check field of upper layer protocol and address family */
7385 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
7386 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
7387 struct sadb_address *src0, *dst0;
7390 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
7391 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
7393 /* check upper layer protocol */
7394 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
7395 ipseclog((LOG_DEBUG, "%s: upper layer protocol "
7396 "mismatched.\n", __func__));
7397 PFKEYSTAT_INC(out_invaddr);
7403 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
7404 PFKEY_ADDR_SADDR(dst0)->sa_family) {
7405 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
7407 PFKEYSTAT_INC(out_invaddr);
7411 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7412 PFKEY_ADDR_SADDR(dst0)->sa_len) {
7413 ipseclog((LOG_DEBUG, "%s: address struct size "
7414 "mismatched.\n", __func__));
7415 PFKEYSTAT_INC(out_invaddr);
7420 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7422 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7423 sizeof(struct sockaddr_in)) {
7424 PFKEYSTAT_INC(out_invaddr);
7430 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7431 sizeof(struct sockaddr_in6)) {
7432 PFKEYSTAT_INC(out_invaddr);
7438 ipseclog((LOG_DEBUG, "%s: unsupported address family\n",
7440 PFKEYSTAT_INC(out_invaddr);
7441 error = EAFNOSUPPORT;
7445 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7447 plen = sizeof(struct in_addr) << 3;
7450 plen = sizeof(struct in6_addr) << 3;
7453 plen = 0; /*fool gcc*/
7457 /* check max prefix length */
7458 if (src0->sadb_address_prefixlen > plen ||
7459 dst0->sadb_address_prefixlen > plen) {
7460 ipseclog((LOG_DEBUG, "%s: illegal prefixlen.\n",
7462 PFKEYSTAT_INC(out_invaddr);
7468 * prefixlen == 0 is valid because there can be a case when
7469 * all addresses are matched.
7473 if (msg->sadb_msg_type >= nitems(key_typesw) ||
7474 key_typesw[msg->sadb_msg_type] == NULL) {
7475 PFKEYSTAT_INC(out_invmsgtype);
7480 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
7483 msg->sadb_msg_errno = error;
7484 return key_sendup_mbuf(so, m, target);
7488 key_senderror(struct socket *so, struct mbuf *m, int code)
7490 struct sadb_msg *msg;
7492 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7493 ("mbuf too small, len %u", m->m_len));
7495 msg = mtod(m, struct sadb_msg *);
7496 msg->sadb_msg_errno = code;
7497 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
7501 * set the pointer to each header into message buffer.
7502 * m will be freed on error.
7503 * XXX larger-than-MCLBYTES extension?
7506 key_align(struct mbuf *m, struct sadb_msghdr *mhp)
7509 struct sadb_ext *ext;
7514 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7515 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7516 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7517 ("mbuf too small, len %u", m->m_len));
7520 bzero(mhp, sizeof(*mhp));
7522 mhp->msg = mtod(m, struct sadb_msg *);
7523 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
7525 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7526 extlen = end; /*just in case extlen is not updated*/
7527 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
7528 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
7530 /* m is already freed */
7533 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7536 switch (ext->sadb_ext_type) {
7538 case SADB_EXT_ADDRESS_SRC:
7539 case SADB_EXT_ADDRESS_DST:
7540 case SADB_EXT_ADDRESS_PROXY:
7541 case SADB_EXT_LIFETIME_CURRENT:
7542 case SADB_EXT_LIFETIME_HARD:
7543 case SADB_EXT_LIFETIME_SOFT:
7544 case SADB_EXT_KEY_AUTH:
7545 case SADB_EXT_KEY_ENCRYPT:
7546 case SADB_EXT_IDENTITY_SRC:
7547 case SADB_EXT_IDENTITY_DST:
7548 case SADB_EXT_SENSITIVITY:
7549 case SADB_EXT_PROPOSAL:
7550 case SADB_EXT_SUPPORTED_AUTH:
7551 case SADB_EXT_SUPPORTED_ENCRYPT:
7552 case SADB_EXT_SPIRANGE:
7553 case SADB_X_EXT_POLICY:
7554 case SADB_X_EXT_SA2:
7556 case SADB_X_EXT_NAT_T_TYPE:
7557 case SADB_X_EXT_NAT_T_SPORT:
7558 case SADB_X_EXT_NAT_T_DPORT:
7559 case SADB_X_EXT_NAT_T_OAI:
7560 case SADB_X_EXT_NAT_T_OAR:
7561 case SADB_X_EXT_NAT_T_FRAG:
7563 /* duplicate check */
7565 * XXX Are there duplication payloads of either
7566 * KEY_AUTH or KEY_ENCRYPT ?
7568 if (mhp->ext[ext->sadb_ext_type] != NULL) {
7569 ipseclog((LOG_DEBUG, "%s: duplicate ext_type "
7570 "%u\n", __func__, ext->sadb_ext_type));
7572 PFKEYSTAT_INC(out_dupext);
7577 ipseclog((LOG_DEBUG, "%s: invalid ext_type %u\n",
7578 __func__, ext->sadb_ext_type));
7580 PFKEYSTAT_INC(out_invexttype);
7584 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
7586 if (key_validate_ext(ext, extlen)) {
7588 PFKEYSTAT_INC(out_invlen);
7592 n = m_pulldown(m, off, extlen, &toff);
7594 /* m is already freed */
7597 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7599 mhp->ext[ext->sadb_ext_type] = ext;
7600 mhp->extoff[ext->sadb_ext_type] = off;
7601 mhp->extlen[ext->sadb_ext_type] = extlen;
7606 PFKEYSTAT_INC(out_invlen);
7614 key_validate_ext(const struct sadb_ext *ext, int len)
7616 const struct sockaddr *sa;
7617 enum { NONE, ADDR } checktype = NONE;
7619 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
7621 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
7624 /* if it does not match minimum/maximum length, bail */
7625 if (ext->sadb_ext_type >= nitems(minsize) ||
7626 ext->sadb_ext_type >= nitems(maxsize))
7628 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
7630 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
7633 /* more checks based on sadb_ext_type XXX need more */
7634 switch (ext->sadb_ext_type) {
7635 case SADB_EXT_ADDRESS_SRC:
7636 case SADB_EXT_ADDRESS_DST:
7637 case SADB_EXT_ADDRESS_PROXY:
7638 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7641 case SADB_EXT_IDENTITY_SRC:
7642 case SADB_EXT_IDENTITY_DST:
7643 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7644 SADB_X_IDENTTYPE_ADDR) {
7645 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7655 switch (checktype) {
7659 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
7660 if (len < baselen + sal)
7662 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7675 for (i = 0; i < IPSEC_DIR_MAX; i++)
7676 TAILQ_INIT(&V_sptree[i]);
7678 LIST_INIT(&V_sahtree);
7680 for (i = 0; i <= SADB_SATYPE_MAX; i++)
7681 LIST_INIT(&V_regtree[i]);
7683 LIST_INIT(&V_acqtree);
7684 LIST_INIT(&V_spacqtree);
7686 if (!IS_DEFAULT_VNET(curvnet))
7690 REGTREE_LOCK_INIT();
7691 SAHTREE_LOCK_INIT();
7695 #ifndef IPSEC_DEBUG2
7696 callout_init(&key_timer, 1);
7697 callout_reset(&key_timer, hz, key_timehandler, NULL);
7698 #endif /*IPSEC_DEBUG2*/
7700 /* initialize key statistics */
7701 keystat.getspi_count = 1;
7704 printf("IPsec: Initialized Security Association Processing.\n");
7711 TAILQ_HEAD(, secpolicy) drainq;
7712 struct secpolicy *sp, *nextsp;
7713 struct secacq *acq, *nextacq;
7714 struct secspacq *spacq, *nextspacq;
7715 struct secashead *sah, *nextsah;
7719 TAILQ_INIT(&drainq);
7721 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7722 TAILQ_CONCAT(&drainq, &V_sptree[i], chain);
7725 sp = TAILQ_FIRST(&drainq);
7726 while (sp != NULL) {
7727 nextsp = TAILQ_NEXT(sp, chain);
7733 for (sah = LIST_FIRST(&V_sahtree); sah != NULL; sah = nextsah) {
7734 nextsah = LIST_NEXT(sah, chain);
7735 if (__LIST_CHAINED(sah)) {
7736 LIST_REMOVE(sah, chain);
7737 free(sah, M_IPSEC_SAH);
7743 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7744 LIST_FOREACH(reg, &V_regtree[i], chain) {
7745 if (__LIST_CHAINED(reg)) {
7746 LIST_REMOVE(reg, chain);
7747 free(reg, M_IPSEC_SAR);
7755 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
7756 nextacq = LIST_NEXT(acq, chain);
7757 if (__LIST_CHAINED(acq)) {
7758 LIST_REMOVE(acq, chain);
7759 free(acq, M_IPSEC_SAQ);
7765 for (spacq = LIST_FIRST(&V_spacqtree); spacq != NULL;
7766 spacq = nextspacq) {
7767 nextspacq = LIST_NEXT(spacq, chain);
7768 if (__LIST_CHAINED(spacq)) {
7769 LIST_REMOVE(spacq, chain);
7770 free(spacq, M_IPSEC_SAQ);
7778 * XXX: maybe This function is called after INBOUND IPsec processing.
7780 * Special check for tunnel-mode packets.
7781 * We must make some checks for consistency between inner and outer IP header.
7783 * xxx more checks to be provided
7786 key_checktunnelsanity(struct secasvar *sav, u_int family, caddr_t src,
7789 IPSEC_ASSERT(sav->sah != NULL, ("null SA header"));
7791 /* XXX: check inner IP header */
7796 /* record data transfer on SA, and update timestamps */
7798 key_sa_recordxfer(struct secasvar *sav, struct mbuf *m)
7800 IPSEC_ASSERT(sav != NULL, ("Null secasvar"));
7801 IPSEC_ASSERT(m != NULL, ("Null mbuf"));
7806 * XXX Currently, there is a difference of bytes size
7807 * between inbound and outbound processing.
7809 sav->lft_c->bytes += m->m_pkthdr.len;
7810 /* to check bytes lifetime is done in key_timehandler(). */
7813 * We use the number of packets as the unit of
7814 * allocations. We increment the variable
7815 * whenever {esp,ah}_{in,out}put is called.
7817 sav->lft_c->allocations++;
7818 /* XXX check for expires? */
7821 * NOTE: We record CURRENT usetime by using wall clock,
7822 * in seconds. HARD and SOFT lifetime are measured by the time
7823 * difference (again in seconds) from usetime.
7827 * -----+-----+--------+---> t
7828 * <--------------> HARD
7831 sav->lft_c->usetime = time_second;
7832 /* XXX check for expires? */
7838 key_sa_chgstate(struct secasvar *sav, u_int8_t state)
7840 IPSEC_ASSERT(sav != NULL, ("NULL sav"));
7841 SAHTREE_LOCK_ASSERT();
7843 if (sav->state != state) {
7844 if (__LIST_CHAINED(sav))
7845 LIST_REMOVE(sav, chain);
7847 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7852 * Take one of the kernel's security keys and convert it into a PF_KEY
7853 * structure within an mbuf, suitable for sending up to a waiting
7854 * application in user land.
7857 * src: A pointer to a kernel security key.
7858 * exttype: Which type of key this is. Refer to the PF_KEY data structures.
7860 * a valid mbuf or NULL indicating an error
7864 static struct mbuf *
7865 key_setkey(struct seckey *src, u_int16_t exttype)
7874 len = PFKEY_ALIGN8(sizeof(struct sadb_key) + _KEYLEN(src));
7875 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
7880 p = mtod(m, struct sadb_key *);
7882 p->sadb_key_len = PFKEY_UNIT64(len);
7883 p->sadb_key_exttype = exttype;
7884 p->sadb_key_bits = src->bits;
7885 bcopy(src->key_data, _KEYBUF(p), _KEYLEN(src));
7891 * Take one of the kernel's lifetime data structures and convert it
7892 * into a PF_KEY structure within an mbuf, suitable for sending up to
7893 * a waiting application in user land.
7896 * src: A pointer to a kernel lifetime structure.
7897 * exttype: Which type of lifetime this is. Refer to the PF_KEY
7898 * data structures for more information.
7900 * a valid mbuf or NULL indicating an error
7904 static struct mbuf *
7905 key_setlifetime(struct seclifetime *src, u_int16_t exttype)
7907 struct mbuf *m = NULL;
7908 struct sadb_lifetime *p;
7909 int len = PFKEY_ALIGN8(sizeof(struct sadb_lifetime));
7914 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
7919 p = mtod(m, struct sadb_lifetime *);
7922 p->sadb_lifetime_len = PFKEY_UNIT64(len);
7923 p->sadb_lifetime_exttype = exttype;
7924 p->sadb_lifetime_allocations = src->allocations;
7925 p->sadb_lifetime_bytes = src->bytes;
7926 p->sadb_lifetime_addtime = src->addtime;
7927 p->sadb_lifetime_usetime = src->usetime;