2 /* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * This code is referd to RFC 2367
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/mutex.h>
48 #include <sys/domain.h>
49 #include <sys/protosw.h>
50 #include <sys/malloc.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/errno.h>
56 #include <sys/queue.h>
57 #include <sys/refcount.h>
58 #include <sys/syslog.h>
61 #include <net/route.h>
62 #include <net/raw_cb.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/ip.h>
68 #include <netinet/in_var.h>
71 #include <netinet/ip6.h>
72 #include <netinet6/in6_var.h>
73 #include <netinet6/ip6_var.h>
77 #include <netinet/in_pcb.h>
80 #include <netinet6/in6_pcb.h>
83 #include <net/pfkeyv2.h>
84 #include <netipsec/keydb.h>
85 #include <netipsec/key.h>
86 #include <netipsec/keysock.h>
87 #include <netipsec/key_debug.h>
89 #include <netipsec/ipsec.h>
91 #include <netipsec/ipsec6.h>
94 #include <netipsec/xform.h>
96 #include <machine/stdarg.h>
99 #include <sys/random.h>
101 #define FULLMASK 0xff
102 #define _BITS(bytes) ((bytes) << 3)
105 * Note on SA reference counting:
106 * - SAs that are not in DEAD state will have (total external reference + 1)
107 * following value in reference count field. they cannot be freed and are
108 * referenced from SA header.
109 * - SAs that are in DEAD state will have (total external reference)
110 * in reference count field. they are ready to be freed. reference from
111 * SA header will be removed in key_delsav(), when the reference count
112 * field hits 0 (= no external reference other than from SA header.
115 VNET_DEFINE(u_int32_t, key_debug_level) = 0;
116 static VNET_DEFINE(u_int, key_spi_trycnt) = 1000;
117 static VNET_DEFINE(u_int32_t, key_spi_minval) = 0x100;
118 static VNET_DEFINE(u_int32_t, key_spi_maxval) = 0x0fffffff; /* XXX */
119 static VNET_DEFINE(u_int32_t, policy_id) = 0;
120 /*interval to initialize randseed,1(m)*/
121 static VNET_DEFINE(u_int, key_int_random) = 60;
122 /* interval to expire acquiring, 30(s)*/
123 static VNET_DEFINE(u_int, key_larval_lifetime) = 30;
124 /* counter for blocking SADB_ACQUIRE.*/
125 static VNET_DEFINE(int, key_blockacq_count) = 10;
126 /* lifetime for blocking SADB_ACQUIRE.*/
127 static VNET_DEFINE(int, key_blockacq_lifetime) = 20;
128 /* preferred old sa rather than new sa.*/
129 static VNET_DEFINE(int, key_preferred_oldsa) = 1;
130 #define V_key_spi_trycnt VNET(key_spi_trycnt)
131 #define V_key_spi_minval VNET(key_spi_minval)
132 #define V_key_spi_maxval VNET(key_spi_maxval)
133 #define V_policy_id VNET(policy_id)
134 #define V_key_int_random VNET(key_int_random)
135 #define V_key_larval_lifetime VNET(key_larval_lifetime)
136 #define V_key_blockacq_count VNET(key_blockacq_count)
137 #define V_key_blockacq_lifetime VNET(key_blockacq_lifetime)
138 #define V_key_preferred_oldsa VNET(key_preferred_oldsa)
140 static VNET_DEFINE(u_int32_t, acq_seq) = 0;
141 #define V_acq_seq VNET(acq_seq)
144 static VNET_DEFINE(LIST_HEAD(_sptree, secpolicy), sptree[IPSEC_DIR_MAX]);
145 #define V_sptree VNET(sptree)
146 static struct mtx sptree_lock;
147 #define SPTREE_LOCK_INIT() \
148 mtx_init(&sptree_lock, "sptree", \
149 "fast ipsec security policy database", MTX_DEF)
150 #define SPTREE_LOCK_DESTROY() mtx_destroy(&sptree_lock)
151 #define SPTREE_LOCK() mtx_lock(&sptree_lock)
152 #define SPTREE_UNLOCK() mtx_unlock(&sptree_lock)
153 #define SPTREE_LOCK_ASSERT() mtx_assert(&sptree_lock, MA_OWNED)
155 static VNET_DEFINE(LIST_HEAD(_sahtree, secashead), sahtree); /* SAD */
156 #define V_sahtree VNET(sahtree)
157 static struct mtx sahtree_lock;
158 #define SAHTREE_LOCK_INIT() \
159 mtx_init(&sahtree_lock, "sahtree", \
160 "fast ipsec security association database", MTX_DEF)
161 #define SAHTREE_LOCK_DESTROY() mtx_destroy(&sahtree_lock)
162 #define SAHTREE_LOCK() mtx_lock(&sahtree_lock)
163 #define SAHTREE_UNLOCK() mtx_unlock(&sahtree_lock)
164 #define SAHTREE_LOCK_ASSERT() mtx_assert(&sahtree_lock, MA_OWNED)
167 static VNET_DEFINE(LIST_HEAD(_regtree, secreg), regtree[SADB_SATYPE_MAX + 1]);
168 #define V_regtree VNET(regtree)
169 static struct mtx regtree_lock;
170 #define REGTREE_LOCK_INIT() \
171 mtx_init(®tree_lock, "regtree", "fast ipsec regtree", MTX_DEF)
172 #define REGTREE_LOCK_DESTROY() mtx_destroy(®tree_lock)
173 #define REGTREE_LOCK() mtx_lock(®tree_lock)
174 #define REGTREE_UNLOCK() mtx_unlock(®tree_lock)
175 #define REGTREE_LOCK_ASSERT() mtx_assert(®tree_lock, MA_OWNED)
177 static VNET_DEFINE(LIST_HEAD(_acqtree, secacq), acqtree); /* acquiring list */
178 #define V_acqtree VNET(acqtree)
179 static struct mtx acq_lock;
180 #define ACQ_LOCK_INIT() \
181 mtx_init(&acq_lock, "acqtree", "fast ipsec acquire list", MTX_DEF)
182 #define ACQ_LOCK_DESTROY() mtx_destroy(&acq_lock)
183 #define ACQ_LOCK() mtx_lock(&acq_lock)
184 #define ACQ_UNLOCK() mtx_unlock(&acq_lock)
185 #define ACQ_LOCK_ASSERT() mtx_assert(&acq_lock, MA_OWNED)
187 /* SP acquiring list */
188 static VNET_DEFINE(LIST_HEAD(_spacqtree, secspacq), spacqtree);
189 #define V_spacqtree VNET(spacqtree)
190 static struct mtx spacq_lock;
191 #define SPACQ_LOCK_INIT() \
192 mtx_init(&spacq_lock, "spacqtree", \
193 "fast ipsec security policy acquire list", MTX_DEF)
194 #define SPACQ_LOCK_DESTROY() mtx_destroy(&spacq_lock)
195 #define SPACQ_LOCK() mtx_lock(&spacq_lock)
196 #define SPACQ_UNLOCK() mtx_unlock(&spacq_lock)
197 #define SPACQ_LOCK_ASSERT() mtx_assert(&spacq_lock, MA_OWNED)
199 /* search order for SAs */
200 static const u_int saorder_state_valid_prefer_old[] = {
201 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
203 static const u_int saorder_state_valid_prefer_new[] = {
204 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
206 static const u_int saorder_state_alive[] = {
208 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
210 static const u_int saorder_state_any[] = {
211 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
212 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
215 static const int minsize[] = {
216 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
217 sizeof(struct sadb_sa), /* SADB_EXT_SA */
218 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
219 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
220 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
221 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
222 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
223 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
224 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
225 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
226 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
227 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
228 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
229 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
230 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
231 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
232 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
233 0, /* SADB_X_EXT_KMPRIVATE */
234 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
235 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
236 sizeof(struct sadb_x_nat_t_type),/* SADB_X_EXT_NAT_T_TYPE */
237 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_SPORT */
238 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_DPORT */
239 sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAI */
240 sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAR */
241 sizeof(struct sadb_x_nat_t_frag),/* SADB_X_EXT_NAT_T_FRAG */
243 static const int maxsize[] = {
244 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
245 sizeof(struct sadb_sa), /* SADB_EXT_SA */
246 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
247 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
248 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
249 0, /* SADB_EXT_ADDRESS_SRC */
250 0, /* SADB_EXT_ADDRESS_DST */
251 0, /* SADB_EXT_ADDRESS_PROXY */
252 0, /* SADB_EXT_KEY_AUTH */
253 0, /* SADB_EXT_KEY_ENCRYPT */
254 0, /* SADB_EXT_IDENTITY_SRC */
255 0, /* SADB_EXT_IDENTITY_DST */
256 0, /* SADB_EXT_SENSITIVITY */
257 0, /* SADB_EXT_PROPOSAL */
258 0, /* SADB_EXT_SUPPORTED_AUTH */
259 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
260 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
261 0, /* SADB_X_EXT_KMPRIVATE */
262 0, /* SADB_X_EXT_POLICY */
263 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
264 sizeof(struct sadb_x_nat_t_type),/* SADB_X_EXT_NAT_T_TYPE */
265 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_SPORT */
266 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_DPORT */
267 0, /* SADB_X_EXT_NAT_T_OAI */
268 0, /* SADB_X_EXT_NAT_T_OAR */
269 sizeof(struct sadb_x_nat_t_frag),/* SADB_X_EXT_NAT_T_FRAG */
272 static VNET_DEFINE(int, ipsec_esp_keymin) = 256;
273 static VNET_DEFINE(int, ipsec_esp_auth) = 0;
274 static VNET_DEFINE(int, ipsec_ah_keymin) = 128;
276 #define V_ipsec_esp_keymin VNET(ipsec_esp_keymin)
277 #define V_ipsec_esp_auth VNET(ipsec_esp_auth)
278 #define V_ipsec_ah_keymin VNET(ipsec_ah_keymin)
281 SYSCTL_DECL(_net_key);
284 SYSCTL_VNET_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug,
285 CTLFLAG_RW, &VNET_NAME(key_debug_level), 0, "");
287 /* max count of trial for the decision of spi value */
288 SYSCTL_VNET_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt,
289 CTLFLAG_RW, &VNET_NAME(key_spi_trycnt), 0, "");
291 /* minimum spi value to allocate automatically. */
292 SYSCTL_VNET_INT(_net_key, KEYCTL_SPI_MIN_VALUE,
293 spi_minval, CTLFLAG_RW, &VNET_NAME(key_spi_minval), 0, "");
295 /* maximun spi value to allocate automatically. */
296 SYSCTL_VNET_INT(_net_key, KEYCTL_SPI_MAX_VALUE,
297 spi_maxval, CTLFLAG_RW, &VNET_NAME(key_spi_maxval), 0, "");
299 /* interval to initialize randseed */
300 SYSCTL_VNET_INT(_net_key, KEYCTL_RANDOM_INT,
301 int_random, CTLFLAG_RW, &VNET_NAME(key_int_random), 0, "");
303 /* lifetime for larval SA */
304 SYSCTL_VNET_INT(_net_key, KEYCTL_LARVAL_LIFETIME,
305 larval_lifetime, CTLFLAG_RW, &VNET_NAME(key_larval_lifetime), 0, "");
307 /* counter for blocking to send SADB_ACQUIRE to IKEd */
308 SYSCTL_VNET_INT(_net_key, KEYCTL_BLOCKACQ_COUNT,
309 blockacq_count, CTLFLAG_RW, &VNET_NAME(key_blockacq_count), 0, "");
311 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
312 SYSCTL_VNET_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME,
313 blockacq_lifetime, CTLFLAG_RW, &VNET_NAME(key_blockacq_lifetime), 0, "");
316 SYSCTL_VNET_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth,
317 CTLFLAG_RW, &VNET_NAME(ipsec_esp_auth), 0, "");
319 /* minimum ESP key length */
320 SYSCTL_VNET_INT(_net_key, KEYCTL_ESP_KEYMIN,
321 esp_keymin, CTLFLAG_RW, &VNET_NAME(ipsec_esp_keymin), 0, "");
323 /* minimum AH key length */
324 SYSCTL_VNET_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin,
325 CTLFLAG_RW, &VNET_NAME(ipsec_ah_keymin), 0, "");
327 /* perfered old SA rather than new SA */
328 SYSCTL_VNET_INT(_net_key, KEYCTL_PREFERED_OLDSA,
329 preferred_oldsa, CTLFLAG_RW, &VNET_NAME(key_preferred_oldsa), 0, "");
331 #define __LIST_CHAINED(elm) \
332 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
333 #define LIST_INSERT_TAIL(head, elm, type, field) \
335 struct type *curelm = LIST_FIRST(head); \
336 if (curelm == NULL) {\
337 LIST_INSERT_HEAD(head, elm, field); \
339 while (LIST_NEXT(curelm, field)) \
340 curelm = LIST_NEXT(curelm, field);\
341 LIST_INSERT_AFTER(curelm, elm, field);\
345 #define KEY_CHKSASTATE(head, sav, name) \
347 if ((head) != (sav)) { \
348 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
349 (name), (head), (sav))); \
354 #define KEY_CHKSPDIR(head, sp, name) \
356 if ((head) != (sp)) { \
357 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
358 "anyway continue.\n", \
359 (name), (head), (sp))); \
363 MALLOC_DEFINE(M_IPSEC_SA, "secasvar", "ipsec security association");
364 MALLOC_DEFINE(M_IPSEC_SAH, "sahead", "ipsec sa head");
365 MALLOC_DEFINE(M_IPSEC_SP, "ipsecpolicy", "ipsec security policy");
366 MALLOC_DEFINE(M_IPSEC_SR, "ipsecrequest", "ipsec security request");
367 MALLOC_DEFINE(M_IPSEC_MISC, "ipsec-misc", "ipsec miscellaneous");
368 MALLOC_DEFINE(M_IPSEC_SAQ, "ipsec-saq", "ipsec sa acquire");
369 MALLOC_DEFINE(M_IPSEC_SAR, "ipsec-reg", "ipsec sa acquire");
372 * set parameters into secpolicyindex buffer.
373 * Must allocate secpolicyindex buffer passed to this function.
375 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
377 bzero((idx), sizeof(struct secpolicyindex)); \
378 (idx)->dir = (_dir); \
379 (idx)->prefs = (ps); \
380 (idx)->prefd = (pd); \
381 (idx)->ul_proto = (ulp); \
382 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
383 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
387 * set parameters into secasindex buffer.
388 * Must allocate secasindex buffer before calling this function.
390 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
392 bzero((idx), sizeof(struct secasindex)); \
393 (idx)->proto = (p); \
395 (idx)->reqid = (r); \
396 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
397 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
402 u_long getspi_count; /* the avarage of count to try to get new SPI */
406 struct sadb_msg *msg;
407 struct sadb_ext *ext[SADB_EXT_MAX + 1];
408 int extoff[SADB_EXT_MAX + 1];
409 int extlen[SADB_EXT_MAX + 1];
412 static struct secasvar *key_allocsa_policy __P((const struct secasindex *));
413 static void key_freesp_so __P((struct secpolicy **));
414 static struct secasvar *key_do_allocsa_policy __P((struct secashead *, u_int));
415 static void key_delsp __P((struct secpolicy *));
416 static struct secpolicy *key_getsp __P((struct secpolicyindex *));
417 static void _key_delsp(struct secpolicy *sp);
418 static struct secpolicy *key_getspbyid __P((u_int32_t));
419 static u_int32_t key_newreqid __P((void));
420 static struct mbuf *key_gather_mbuf __P((struct mbuf *,
421 const struct sadb_msghdr *, int, int, ...));
422 static int key_spdadd __P((struct socket *, struct mbuf *,
423 const struct sadb_msghdr *));
424 static u_int32_t key_getnewspid __P((void));
425 static int key_spddelete __P((struct socket *, struct mbuf *,
426 const struct sadb_msghdr *));
427 static int key_spddelete2 __P((struct socket *, struct mbuf *,
428 const struct sadb_msghdr *));
429 static int key_spdget __P((struct socket *, struct mbuf *,
430 const struct sadb_msghdr *));
431 static int key_spdflush __P((struct socket *, struct mbuf *,
432 const struct sadb_msghdr *));
433 static int key_spddump __P((struct socket *, struct mbuf *,
434 const struct sadb_msghdr *));
435 static struct mbuf *key_setdumpsp __P((struct secpolicy *,
436 u_int8_t, u_int32_t, u_int32_t));
437 static u_int key_getspreqmsglen __P((struct secpolicy *));
438 static int key_spdexpire __P((struct secpolicy *));
439 static struct secashead *key_newsah __P((struct secasindex *));
440 static void key_delsah __P((struct secashead *));
441 static struct secasvar *key_newsav __P((struct mbuf *,
442 const struct sadb_msghdr *, struct secashead *, int *,
444 #define KEY_NEWSAV(m, sadb, sah, e) \
445 key_newsav(m, sadb, sah, e, __FILE__, __LINE__)
446 static void key_delsav __P((struct secasvar *));
447 static struct secashead *key_getsah __P((struct secasindex *));
448 static struct secasvar *key_checkspidup __P((struct secasindex *, u_int32_t));
449 static struct secasvar *key_getsavbyspi __P((struct secashead *, u_int32_t));
450 static int key_setsaval __P((struct secasvar *, struct mbuf *,
451 const struct sadb_msghdr *));
452 static int key_mature __P((struct secasvar *));
453 static struct mbuf *key_setdumpsa __P((struct secasvar *, u_int8_t,
454 u_int8_t, u_int32_t, u_int32_t));
455 static struct mbuf *key_setsadbmsg __P((u_int8_t, u_int16_t, u_int8_t,
456 u_int32_t, pid_t, u_int16_t));
457 static struct mbuf *key_setsadbsa __P((struct secasvar *));
458 static struct mbuf *key_setsadbaddr __P((u_int16_t,
459 const struct sockaddr *, u_int8_t, u_int16_t));
461 static struct mbuf *key_setsadbxport(u_int16_t, u_int16_t);
462 static struct mbuf *key_setsadbxtype(u_int16_t);
464 static void key_porttosaddr(struct sockaddr *, u_int16_t);
465 #define KEY_PORTTOSADDR(saddr, port) \
466 key_porttosaddr((struct sockaddr *)(saddr), (port))
467 static struct mbuf *key_setsadbxsa2 __P((u_int8_t, u_int32_t, u_int32_t));
468 static struct mbuf *key_setsadbxpolicy __P((u_int16_t, u_int8_t,
470 static struct seckey *key_dup_keymsg(const struct sadb_key *, u_int,
471 struct malloc_type *);
472 static struct seclifetime *key_dup_lifemsg(const struct sadb_lifetime *src,
473 struct malloc_type *type);
475 static int key_ismyaddr6 __P((struct sockaddr_in6 *));
478 /* flags for key_cmpsaidx() */
479 #define CMP_HEAD 1 /* protocol, addresses. */
480 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
481 #define CMP_REQID 3 /* additionally HEAD, reaid. */
482 #define CMP_EXACTLY 4 /* all elements. */
483 static int key_cmpsaidx
484 __P((const struct secasindex *, const struct secasindex *, int));
486 static int key_cmpspidx_exactly
487 __P((struct secpolicyindex *, struct secpolicyindex *));
488 static int key_cmpspidx_withmask
489 __P((struct secpolicyindex *, struct secpolicyindex *));
490 static int key_sockaddrcmp __P((const struct sockaddr *, const struct sockaddr *, int));
491 static int key_bbcmp __P((const void *, const void *, u_int));
492 static u_int16_t key_satype2proto __P((u_int8_t));
493 static u_int8_t key_proto2satype __P((u_int16_t));
495 static int key_getspi __P((struct socket *, struct mbuf *,
496 const struct sadb_msghdr *));
497 static u_int32_t key_do_getnewspi __P((struct sadb_spirange *,
498 struct secasindex *));
499 static int key_update __P((struct socket *, struct mbuf *,
500 const struct sadb_msghdr *));
501 #ifdef IPSEC_DOSEQCHECK
502 static struct secasvar *key_getsavbyseq __P((struct secashead *, u_int32_t));
504 static int key_add __P((struct socket *, struct mbuf *,
505 const struct sadb_msghdr *));
506 static int key_setident __P((struct secashead *, struct mbuf *,
507 const struct sadb_msghdr *));
508 static struct mbuf *key_getmsgbuf_x1 __P((struct mbuf *,
509 const struct sadb_msghdr *));
510 static int key_delete __P((struct socket *, struct mbuf *,
511 const struct sadb_msghdr *));
512 static int key_get __P((struct socket *, struct mbuf *,
513 const struct sadb_msghdr *));
515 static void key_getcomb_setlifetime __P((struct sadb_comb *));
516 static struct mbuf *key_getcomb_esp __P((void));
517 static struct mbuf *key_getcomb_ah __P((void));
518 static struct mbuf *key_getcomb_ipcomp __P((void));
519 static struct mbuf *key_getprop __P((const struct secasindex *));
521 static int key_acquire __P((const struct secasindex *, struct secpolicy *));
522 static struct secacq *key_newacq __P((const struct secasindex *));
523 static struct secacq *key_getacq __P((const struct secasindex *));
524 static struct secacq *key_getacqbyseq __P((u_int32_t));
525 static struct secspacq *key_newspacq __P((struct secpolicyindex *));
526 static struct secspacq *key_getspacq __P((struct secpolicyindex *));
527 static int key_acquire2 __P((struct socket *, struct mbuf *,
528 const struct sadb_msghdr *));
529 static int key_register __P((struct socket *, struct mbuf *,
530 const struct sadb_msghdr *));
531 static int key_expire __P((struct secasvar *));
532 static int key_flush __P((struct socket *, struct mbuf *,
533 const struct sadb_msghdr *));
534 static int key_dump __P((struct socket *, struct mbuf *,
535 const struct sadb_msghdr *));
536 static int key_promisc __P((struct socket *, struct mbuf *,
537 const struct sadb_msghdr *));
538 static int key_senderror __P((struct socket *, struct mbuf *, int));
539 static int key_validate_ext __P((const struct sadb_ext *, int));
540 static int key_align __P((struct mbuf *, struct sadb_msghdr *));
541 static struct mbuf *key_setlifetime(struct seclifetime *src,
543 static struct mbuf *key_setkey(struct seckey *src, u_int16_t exttype);
546 static const char *key_getfqdn __P((void));
547 static const char *key_getuserfqdn __P((void));
549 static void key_sa_chgstate __P((struct secasvar *, u_int8_t));
550 static struct mbuf *key_alloc_mbuf __P((int));
553 sa_initref(struct secasvar *sav)
556 refcount_init(&sav->refcnt, 1);
559 sa_addref(struct secasvar *sav)
562 refcount_acquire(&sav->refcnt);
563 IPSEC_ASSERT(sav->refcnt != 0, ("SA refcnt overflow"));
566 sa_delref(struct secasvar *sav)
569 IPSEC_ASSERT(sav->refcnt > 0, ("SA refcnt underflow"));
570 return (refcount_release(&sav->refcnt));
573 #define SP_ADDREF(p) do { \
575 IPSEC_ASSERT((p)->refcnt != 0, ("SP refcnt overflow")); \
577 #define SP_DELREF(p) do { \
578 IPSEC_ASSERT((p)->refcnt > 0, ("SP refcnt underflow")); \
584 * Update the refcnt while holding the SPTREE lock.
587 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 LIST_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, int tag)
617 struct secpolicy *sp;
619 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
620 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
621 ("invalid direction %u", dir));
623 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
624 printf("DP %s from %s:%u\n", __func__, where, tag));
627 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
628 printf("*** objects\n");
629 kdebug_secpolicyindex(spidx));
632 LIST_FOREACH(sp, &V_sptree[dir], chain) {
633 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
634 printf("*** in SPD\n");
635 kdebug_secpolicyindex(&sp->spidx));
637 if (sp->state == IPSEC_SPSTATE_DEAD)
639 if (key_cmpspidx_withmask(&sp->spidx, spidx))
646 KEY_CHKSPDIR(sp->spidx.dir, dir, __func__);
648 /* found a SPD entry */
649 sp->lastused = time_second;
654 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
655 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
656 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
661 * allocating a SP for OUTBOUND or INBOUND packet.
662 * Must call key_freesp() later.
663 * OUT: NULL: not found
664 * others: found and return the pointer.
667 key_allocsp2(u_int32_t spi,
668 union sockaddr_union *dst,
671 const char* where, int tag)
673 struct secpolicy *sp;
675 IPSEC_ASSERT(dst != NULL, ("null dst"));
676 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
677 ("invalid direction %u", dir));
679 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
680 printf("DP %s from %s:%u\n", __func__, where, tag));
683 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
684 printf("*** objects\n");
685 printf("spi %u proto %u dir %u\n", spi, proto, dir);
686 kdebug_sockaddr(&dst->sa));
689 LIST_FOREACH(sp, &V_sptree[dir], chain) {
690 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
691 printf("*** in SPD\n");
692 kdebug_secpolicyindex(&sp->spidx));
694 if (sp->state == IPSEC_SPSTATE_DEAD)
696 /* compare simple values, then dst address */
697 if (sp->spidx.ul_proto != proto)
699 /* NB: spi's must exist and match */
700 if (!sp->req || !sp->req->sav || sp->req->sav->spi != spi)
702 if (key_sockaddrcmp(&sp->spidx.dst.sa, &dst->sa, 1) == 0)
709 KEY_CHKSPDIR(sp->spidx.dir, dir, __func__);
711 /* found a SPD entry */
712 sp->lastused = time_second;
717 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
718 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
719 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
725 * return a policy that matches this particular inbound packet.
729 key_gettunnel(const struct sockaddr *osrc,
730 const struct sockaddr *odst,
731 const struct sockaddr *isrc,
732 const struct sockaddr *idst,
733 const char* where, int tag)
735 struct secpolicy *sp;
736 const int dir = IPSEC_DIR_INBOUND;
737 struct ipsecrequest *r1, *r2, *p;
738 struct secpolicyindex spidx;
740 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
741 printf("DP %s from %s:%u\n", __func__, where, tag));
743 if (isrc->sa_family != idst->sa_family) {
744 ipseclog((LOG_ERR, "%s: protocol family mismatched %d != %d\n.",
745 __func__, isrc->sa_family, idst->sa_family));
751 LIST_FOREACH(sp, &V_sptree[dir], chain) {
752 if (sp->state == IPSEC_SPSTATE_DEAD)
756 for (p = sp->req; p; p = p->next) {
757 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
764 /* here we look at address matches only */
766 if (isrc->sa_len > sizeof(spidx.src) ||
767 idst->sa_len > sizeof(spidx.dst))
769 bcopy(isrc, &spidx.src, isrc->sa_len);
770 bcopy(idst, &spidx.dst, idst->sa_len);
771 if (!key_cmpspidx_withmask(&sp->spidx, &spidx))
774 if (key_sockaddrcmp(&r1->saidx.src.sa, isrc, 0) ||
775 key_sockaddrcmp(&r1->saidx.dst.sa, idst, 0))
779 if (key_sockaddrcmp(&r2->saidx.src.sa, osrc, 0) ||
780 key_sockaddrcmp(&r2->saidx.dst.sa, odst, 0))
789 sp->lastused = time_second;
794 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
795 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
796 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
802 * allocating an SA entry for an *OUTBOUND* packet.
803 * checking each request entries in SP, and acquire an SA if need.
804 * OUT: 0: there are valid requests.
805 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
808 key_checkrequest(struct ipsecrequest *isr, const struct secasindex *saidx)
812 struct secasvar *sav;
814 IPSEC_ASSERT(isr != NULL, ("null isr"));
815 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
816 IPSEC_ASSERT(saidx->mode == IPSEC_MODE_TRANSPORT ||
817 saidx->mode == IPSEC_MODE_TUNNEL,
818 ("unexpected policy %u", saidx->mode));
821 * XXX guard against protocol callbacks from the crypto
822 * thread as they reference ipsecrequest.sav which we
823 * temporarily null out below. Need to rethink how we
824 * handle bundled SA's in the callback thread.
826 IPSECREQUEST_LOCK_ASSERT(isr);
828 /* get current level */
829 level = ipsec_get_reqlevel(isr);
832 * We check new SA in the IPsec request because a different
833 * SA may be involved each time this request is checked, either
834 * because new SAs are being configured, or this request is
835 * associated with an unconnected datagram socket, or this request
836 * is associated with a system default policy.
838 * key_allocsa_policy should allocate the oldest SA available.
839 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
841 sav = key_allocsa_policy(saidx);
842 if (sav != isr->sav) {
843 /* SA need to be updated. */
844 if (!IPSECREQUEST_UPGRADE(isr)) {
845 /* Kick everyone off. */
846 IPSECREQUEST_UNLOCK(isr);
847 IPSECREQUEST_WLOCK(isr);
849 if (isr->sav != NULL)
850 KEY_FREESAV(&isr->sav);
852 IPSECREQUEST_DOWNGRADE(isr);
853 } else if (sav != NULL)
856 /* When there is SA. */
857 if (isr->sav != NULL) {
858 if (isr->sav->state != SADB_SASTATE_MATURE &&
859 isr->sav->state != SADB_SASTATE_DYING)
865 error = key_acquire(saidx, isr->sp);
867 /* XXX What should I do ? */
868 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
873 if (level != IPSEC_LEVEL_REQUIRE) {
874 /* XXX sigh, the interface to this routine is botched */
875 IPSEC_ASSERT(isr->sav == NULL, ("unexpected SA"));
883 * allocating a SA for policy entry from SAD.
884 * NOTE: searching SAD of aliving state.
885 * OUT: NULL: not found.
886 * others: found and return the pointer.
888 static struct secasvar *
889 key_allocsa_policy(const struct secasindex *saidx)
891 #define N(a) _ARRAYLEN(a)
892 struct secashead *sah;
893 struct secasvar *sav;
894 u_int stateidx, arraysize;
895 const u_int *state_valid;
898 LIST_FOREACH(sah, &V_sahtree, chain) {
899 if (sah->state == SADB_SASTATE_DEAD)
901 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID)) {
902 if (V_key_preferred_oldsa) {
903 state_valid = saorder_state_valid_prefer_old;
904 arraysize = N(saorder_state_valid_prefer_old);
906 state_valid = saorder_state_valid_prefer_new;
907 arraysize = N(saorder_state_valid_prefer_new);
918 /* search valid state */
919 for (stateidx = 0; stateidx < arraysize; stateidx++) {
920 sav = key_do_allocsa_policy(sah, state_valid[stateidx]);
930 * searching SAD with direction, protocol, mode and state.
931 * called by key_allocsa_policy().
934 * others : found, pointer to a SA.
936 static struct secasvar *
937 key_do_allocsa_policy(struct secashead *sah, u_int state)
939 struct secasvar *sav, *nextsav, *candidate, *d;
945 for (sav = LIST_FIRST(&sah->savtree[state]);
949 nextsav = LIST_NEXT(sav, chain);
952 KEY_CHKSASTATE(sav->state, state, __func__);
955 if (candidate == NULL) {
960 /* Which SA is the better ? */
962 IPSEC_ASSERT(candidate->lft_c != NULL,
963 ("null candidate lifetime"));
964 IPSEC_ASSERT(sav->lft_c != NULL, ("null sav lifetime"));
966 /* What the best method is to compare ? */
967 if (V_key_preferred_oldsa) {
968 if (candidate->lft_c->addtime >
969 sav->lft_c->addtime) {
976 /* preferred new sa rather than old sa */
977 if (candidate->lft_c->addtime <
978 sav->lft_c->addtime) {
985 * prepared to delete the SA when there is more
986 * suitable candidate and the lifetime of the SA is not
989 if (d->lft_h->addtime != 0) {
990 struct mbuf *m, *result;
993 key_sa_chgstate(d, SADB_SASTATE_DEAD);
995 IPSEC_ASSERT(d->refcnt > 0, ("bogus ref count"));
997 satype = key_proto2satype(d->sah->saidx.proto);
1001 m = key_setsadbmsg(SADB_DELETE, 0,
1002 satype, 0, 0, d->refcnt - 1);
1007 /* set sadb_address for saidx's. */
1008 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
1009 &d->sah->saidx.src.sa,
1010 d->sah->saidx.src.sa.sa_len << 3,
1016 /* set sadb_address for saidx's. */
1017 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
1018 &d->sah->saidx.dst.sa,
1019 d->sah->saidx.dst.sa.sa_len << 3,
1025 /* create SA extension */
1026 m = key_setsadbsa(d);
1031 if (result->m_len < sizeof(struct sadb_msg)) {
1032 result = m_pullup(result,
1033 sizeof(struct sadb_msg));
1038 result->m_pkthdr.len = 0;
1039 for (m = result; m; m = m->m_next)
1040 result->m_pkthdr.len += m->m_len;
1041 mtod(result, struct sadb_msg *)->sadb_msg_len =
1042 PFKEY_UNIT64(result->m_pkthdr.len);
1044 if (key_sendup_mbuf(NULL, result,
1045 KEY_SENDUP_REGISTERED))
1052 sa_addref(candidate);
1053 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1054 printf("DP %s cause refcnt++:%d SA:%p\n",
1055 __func__, candidate->refcnt, candidate));
1063 * allocating a usable SA entry for a *INBOUND* packet.
1064 * Must call key_freesav() later.
1065 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
1066 * NULL: not found, or error occured.
1068 * In the comparison, no source address is used--for RFC2401 conformance.
1069 * To quote, from section 4.1:
1070 * A security association is uniquely identified by a triple consisting
1071 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1072 * security protocol (AH or ESP) identifier.
1073 * Note that, however, we do need to keep source address in IPsec SA.
1074 * IKE specification and PF_KEY specification do assume that we
1075 * keep source address in IPsec SA. We see a tricky situation here.
1079 union sockaddr_union *dst,
1082 const char* where, int tag)
1084 struct secashead *sah;
1085 struct secasvar *sav;
1086 u_int stateidx, arraysize, state;
1087 const u_int *saorder_state_valid;
1090 IPSEC_ASSERT(dst != NULL, ("null dst address"));
1092 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1093 printf("DP %s from %s:%u\n", __func__, where, tag));
1096 chkport = (dst->sa.sa_family == AF_INET &&
1097 dst->sa.sa_len == sizeof(struct sockaddr_in) &&
1098 dst->sin.sin_port != 0);
1105 * XXX: to be checked internal IP header somewhere. Also when
1106 * IPsec tunnel packet is received. But ESP tunnel mode is
1107 * encrypted so we can't check internal IP header.
1110 if (V_key_preferred_oldsa) {
1111 saorder_state_valid = saorder_state_valid_prefer_old;
1112 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
1114 saorder_state_valid = saorder_state_valid_prefer_new;
1115 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
1117 LIST_FOREACH(sah, &V_sahtree, chain) {
1118 /* search valid state */
1119 for (stateidx = 0; stateidx < arraysize; stateidx++) {
1120 state = saorder_state_valid[stateidx];
1121 LIST_FOREACH(sav, &sah->savtree[state], chain) {
1123 KEY_CHKSASTATE(sav->state, state, __func__);
1124 /* do not return entries w/ unusable state */
1125 if (sav->state != SADB_SASTATE_MATURE &&
1126 sav->state != SADB_SASTATE_DYING)
1128 if (proto != sav->sah->saidx.proto)
1130 if (spi != sav->spi)
1132 #if 0 /* don't check src */
1133 /* check src address */
1134 if (key_sockaddrcmp(&src->sa, &sav->sah->saidx.src.sa, chkport) != 0)
1137 /* check dst address */
1138 if (key_sockaddrcmp(&dst->sa, &sav->sah->saidx.dst.sa, chkport) != 0)
1149 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1150 printf("DP %s return SA:%p; refcnt %u\n", __func__,
1151 sav, sav ? sav->refcnt : 0));
1156 * Must be called after calling key_allocsp().
1157 * For both the packet without socket and key_freeso().
1160 _key_freesp(struct secpolicy **spp, const char* where, int tag)
1162 struct secpolicy *sp = *spp;
1164 IPSEC_ASSERT(sp != NULL, ("null sp"));
1169 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1170 printf("DP %s SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1171 __func__, sp, sp->id, where, tag, sp->refcnt));
1173 if (sp->refcnt == 0) {
1181 * Must be called after calling key_allocsp().
1182 * For the packet with socket.
1185 key_freeso(struct socket *so)
1187 IPSEC_ASSERT(so != NULL, ("null so"));
1189 switch (so->so_proto->pr_domain->dom_family) {
1190 #if defined(INET) || defined(INET6)
1198 struct inpcb *pcb = sotoinpcb(so);
1200 /* Does it have a PCB ? */
1203 key_freesp_so(&pcb->inp_sp->sp_in);
1204 key_freesp_so(&pcb->inp_sp->sp_out);
1207 #endif /* INET || INET6 */
1209 ipseclog((LOG_DEBUG, "%s: unknown address family=%d.\n",
1210 __func__, so->so_proto->pr_domain->dom_family));
1216 key_freesp_so(struct secpolicy **sp)
1218 IPSEC_ASSERT(sp != NULL && *sp != NULL, ("null sp"));
1220 if ((*sp)->policy == IPSEC_POLICY_ENTRUST ||
1221 (*sp)->policy == IPSEC_POLICY_BYPASS)
1224 IPSEC_ASSERT((*sp)->policy == IPSEC_POLICY_IPSEC,
1225 ("invalid policy %u", (*sp)->policy));
1230 key_addrefsa(struct secasvar *sav, const char* where, int tag)
1233 IPSEC_ASSERT(sav != NULL, ("null sav"));
1234 IPSEC_ASSERT(sav->refcnt > 0, ("refcount must exist"));
1240 * Must be called after calling key_allocsa().
1241 * This function is called by key_freesp() to free some SA allocated
1245 key_freesav(struct secasvar **psav, const char* where, int tag)
1247 struct secasvar *sav = *psav;
1249 IPSEC_ASSERT(sav != NULL, ("null sav"));
1251 if (sa_delref(sav)) {
1252 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1253 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1254 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1258 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1259 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1260 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1264 /* %%% SPD management */
1266 * free security policy entry.
1269 key_delsp(struct secpolicy *sp)
1271 struct ipsecrequest *isr, *nextisr;
1273 IPSEC_ASSERT(sp != NULL, ("null sp"));
1274 SPTREE_LOCK_ASSERT();
1276 sp->state = IPSEC_SPSTATE_DEAD;
1278 IPSEC_ASSERT(sp->refcnt == 0,
1279 ("SP with references deleted (refcnt %u)", sp->refcnt));
1281 /* remove from SP index */
1282 if (__LIST_CHAINED(sp))
1283 LIST_REMOVE(sp, chain);
1285 for (isr = sp->req; isr != NULL; isr = nextisr) {
1286 if (isr->sav != NULL) {
1287 KEY_FREESAV(&isr->sav);
1291 nextisr = isr->next;
1299 * OUT: NULL : not found
1300 * others : found, pointer to a SP.
1302 static struct secpolicy *
1303 key_getsp(struct secpolicyindex *spidx)
1305 struct secpolicy *sp;
1307 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
1310 LIST_FOREACH(sp, &V_sptree[spidx->dir], chain) {
1311 if (sp->state == IPSEC_SPSTATE_DEAD)
1313 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1325 * OUT: NULL : not found
1326 * others : found, pointer to a SP.
1328 static struct secpolicy *
1329 key_getspbyid(u_int32_t id)
1331 struct secpolicy *sp;
1334 LIST_FOREACH(sp, &V_sptree[IPSEC_DIR_INBOUND], chain) {
1335 if (sp->state == IPSEC_SPSTATE_DEAD)
1343 LIST_FOREACH(sp, &V_sptree[IPSEC_DIR_OUTBOUND], chain) {
1344 if (sp->state == IPSEC_SPSTATE_DEAD)
1358 key_newsp(const char* where, int tag)
1360 struct secpolicy *newsp = NULL;
1362 newsp = (struct secpolicy *)
1363 malloc(sizeof(struct secpolicy), M_IPSEC_SP, M_NOWAIT|M_ZERO);
1365 SECPOLICY_LOCK_INIT(newsp);
1370 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1371 printf("DP %s from %s:%u return SP:%p\n", __func__,
1372 where, tag, newsp));
1377 _key_delsp(struct secpolicy *sp)
1379 SECPOLICY_LOCK_DESTROY(sp);
1380 free(sp, M_IPSEC_SP);
1384 * create secpolicy structure from sadb_x_policy structure.
1385 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1386 * so must be set properly later.
1389 key_msg2sp(xpl0, len, error)
1390 struct sadb_x_policy *xpl0;
1394 struct secpolicy *newsp;
1396 IPSEC_ASSERT(xpl0 != NULL, ("null xpl0"));
1397 IPSEC_ASSERT(len >= sizeof(*xpl0), ("policy too short: %zu", len));
1399 if (len != PFKEY_EXTLEN(xpl0)) {
1400 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n", __func__));
1405 if ((newsp = KEY_NEWSP()) == NULL) {
1410 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1411 newsp->policy = xpl0->sadb_x_policy_type;
1414 switch (xpl0->sadb_x_policy_type) {
1415 case IPSEC_POLICY_DISCARD:
1416 case IPSEC_POLICY_NONE:
1417 case IPSEC_POLICY_ENTRUST:
1418 case IPSEC_POLICY_BYPASS:
1422 case IPSEC_POLICY_IPSEC:
1425 struct sadb_x_ipsecrequest *xisr;
1426 struct ipsecrequest **p_isr = &newsp->req;
1428 /* validity check */
1429 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1430 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n",
1437 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1438 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1442 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1443 ipseclog((LOG_DEBUG, "%s: invalid ipsecrequest "
1444 "length.\n", __func__));
1450 /* allocate request buffer */
1451 /* NB: data structure is zero'd */
1452 *p_isr = ipsec_newisr();
1453 if ((*p_isr) == NULL) {
1454 ipseclog((LOG_DEBUG,
1455 "%s: No more memory.\n", __func__));
1462 switch (xisr->sadb_x_ipsecrequest_proto) {
1465 case IPPROTO_IPCOMP:
1468 ipseclog((LOG_DEBUG,
1469 "%s: invalid proto type=%u\n", __func__,
1470 xisr->sadb_x_ipsecrequest_proto));
1472 *error = EPROTONOSUPPORT;
1475 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1477 switch (xisr->sadb_x_ipsecrequest_mode) {
1478 case IPSEC_MODE_TRANSPORT:
1479 case IPSEC_MODE_TUNNEL:
1481 case IPSEC_MODE_ANY:
1483 ipseclog((LOG_DEBUG,
1484 "%s: invalid mode=%u\n", __func__,
1485 xisr->sadb_x_ipsecrequest_mode));
1490 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1492 switch (xisr->sadb_x_ipsecrequest_level) {
1493 case IPSEC_LEVEL_DEFAULT:
1494 case IPSEC_LEVEL_USE:
1495 case IPSEC_LEVEL_REQUIRE:
1497 case IPSEC_LEVEL_UNIQUE:
1498 /* validity check */
1500 * If range violation of reqid, kernel will
1501 * update it, don't refuse it.
1503 if (xisr->sadb_x_ipsecrequest_reqid
1504 > IPSEC_MANUAL_REQID_MAX) {
1505 ipseclog((LOG_DEBUG,
1506 "%s: reqid=%d range "
1507 "violation, updated by kernel.\n",
1509 xisr->sadb_x_ipsecrequest_reqid));
1510 xisr->sadb_x_ipsecrequest_reqid = 0;
1513 /* allocate new reqid id if reqid is zero. */
1514 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1516 if ((reqid = key_newreqid()) == 0) {
1521 (*p_isr)->saidx.reqid = reqid;
1522 xisr->sadb_x_ipsecrequest_reqid = reqid;
1524 /* set it for manual keying. */
1525 (*p_isr)->saidx.reqid =
1526 xisr->sadb_x_ipsecrequest_reqid;
1531 ipseclog((LOG_DEBUG, "%s: invalid level=%u\n",
1533 xisr->sadb_x_ipsecrequest_level));
1538 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1540 /* set IP addresses if there */
1541 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1542 struct sockaddr *paddr;
1544 paddr = (struct sockaddr *)(xisr + 1);
1546 /* validity check */
1548 > sizeof((*p_isr)->saidx.src)) {
1549 ipseclog((LOG_DEBUG, "%s: invalid "
1550 "request address length.\n",
1556 bcopy(paddr, &(*p_isr)->saidx.src,
1559 paddr = (struct sockaddr *)((caddr_t)paddr
1562 /* validity check */
1564 > sizeof((*p_isr)->saidx.dst)) {
1565 ipseclog((LOG_DEBUG, "%s: invalid "
1566 "request address length.\n",
1572 bcopy(paddr, &(*p_isr)->saidx.dst,
1576 (*p_isr)->sp = newsp;
1578 /* initialization for the next. */
1579 p_isr = &(*p_isr)->next;
1580 tlen -= xisr->sadb_x_ipsecrequest_len;
1582 /* validity check */
1584 ipseclog((LOG_DEBUG, "%s: becoming tlen < 0.\n",
1591 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1592 + xisr->sadb_x_ipsecrequest_len);
1597 ipseclog((LOG_DEBUG, "%s: invalid policy type.\n", __func__));
1610 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1612 auto_reqid = (auto_reqid == ~0
1613 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1615 /* XXX should be unique check */
1621 * copy secpolicy struct to sadb_x_policy structure indicated.
1625 struct secpolicy *sp;
1627 struct sadb_x_policy *xpl;
1632 IPSEC_ASSERT(sp != NULL, ("null policy"));
1634 tlen = key_getspreqmsglen(sp);
1636 m = key_alloc_mbuf(tlen);
1637 if (!m || m->m_next) { /*XXX*/
1645 xpl = mtod(m, struct sadb_x_policy *);
1648 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1649 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1650 xpl->sadb_x_policy_type = sp->policy;
1651 xpl->sadb_x_policy_dir = sp->spidx.dir;
1652 xpl->sadb_x_policy_id = sp->id;
1653 p = (caddr_t)xpl + sizeof(*xpl);
1655 /* if is the policy for ipsec ? */
1656 if (sp->policy == IPSEC_POLICY_IPSEC) {
1657 struct sadb_x_ipsecrequest *xisr;
1658 struct ipsecrequest *isr;
1660 for (isr = sp->req; isr != NULL; isr = isr->next) {
1662 xisr = (struct sadb_x_ipsecrequest *)p;
1664 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1665 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1666 xisr->sadb_x_ipsecrequest_level = isr->level;
1667 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1670 bcopy(&isr->saidx.src, p, isr->saidx.src.sa.sa_len);
1671 p += isr->saidx.src.sa.sa_len;
1672 bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len);
1673 p += isr->saidx.src.sa.sa_len;
1675 xisr->sadb_x_ipsecrequest_len =
1676 PFKEY_ALIGN8(sizeof(*xisr)
1677 + isr->saidx.src.sa.sa_len
1678 + isr->saidx.dst.sa.sa_len);
1685 /* m will not be freed nor modified */
1686 static struct mbuf *
1688 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1689 int ndeep, int nitem, ...)
1691 key_gather_mbuf(m, mhp, ndeep, nitem, va_alist)
1693 const struct sadb_msghdr *mhp;
1702 struct mbuf *result = NULL, *n;
1705 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1706 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1708 va_start(ap, nitem);
1709 for (i = 0; i < nitem; i++) {
1710 idx = va_arg(ap, int);
1711 if (idx < 0 || idx > SADB_EXT_MAX)
1713 /* don't attempt to pull empty extension */
1714 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1716 if (idx != SADB_EXT_RESERVED &&
1717 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1720 if (idx == SADB_EXT_RESERVED) {
1721 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1723 IPSEC_ASSERT(len <= MHLEN, ("header too big %u", len));
1725 MGETHDR(n, M_DONTWAIT, MT_DATA);
1730 m_copydata(m, 0, sizeof(struct sadb_msg),
1732 } else if (i < ndeep) {
1733 len = mhp->extlen[idx];
1734 n = key_alloc_mbuf(len);
1735 if (!n || n->m_next) { /*XXX*/
1740 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1743 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1756 if ((result->m_flags & M_PKTHDR) != 0) {
1757 result->m_pkthdr.len = 0;
1758 for (n = result; n; n = n->m_next)
1759 result->m_pkthdr.len += n->m_len;
1771 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1772 * add an entry to SP database, when received
1773 * <base, address(SD), (lifetime(H),) policy>
1775 * Adding to SP database,
1777 * <base, address(SD), (lifetime(H),) policy>
1778 * to the socket which was send.
1780 * SPDADD set a unique policy entry.
1781 * SPDSETIDX like SPDADD without a part of policy requests.
1782 * SPDUPDATE replace a unique policy entry.
1784 * m will always be freed.
1787 key_spdadd(so, m, mhp)
1790 const struct sadb_msghdr *mhp;
1792 struct sadb_address *src0, *dst0;
1793 struct sadb_x_policy *xpl0, *xpl;
1794 struct sadb_lifetime *lft = NULL;
1795 struct secpolicyindex spidx;
1796 struct secpolicy *newsp;
1799 IPSEC_ASSERT(so != NULL, ("null socket"));
1800 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1801 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1802 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
1804 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1805 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1806 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1807 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1808 return key_senderror(so, m, EINVAL);
1810 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1811 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1812 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1813 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1815 return key_senderror(so, m, EINVAL);
1817 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1818 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1819 < sizeof(struct sadb_lifetime)) {
1820 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1822 return key_senderror(so, m, EINVAL);
1824 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1827 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1828 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1829 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1832 * Note: do not parse SADB_X_EXT_NAT_T_* here:
1833 * we are processing traffic endpoints.
1837 /* XXX boundary check against sa_len */
1838 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1841 src0->sadb_address_prefixlen,
1842 dst0->sadb_address_prefixlen,
1843 src0->sadb_address_proto,
1846 /* checking the direciton. */
1847 switch (xpl0->sadb_x_policy_dir) {
1848 case IPSEC_DIR_INBOUND:
1849 case IPSEC_DIR_OUTBOUND:
1852 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
1853 mhp->msg->sadb_msg_errno = EINVAL;
1858 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1859 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1860 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1861 ipseclog((LOG_DEBUG, "%s: Invalid policy type.\n", __func__));
1862 return key_senderror(so, m, EINVAL);
1865 /* policy requests are mandatory when action is ipsec. */
1866 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1867 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1868 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1869 ipseclog((LOG_DEBUG, "%s: some policy requests part required\n",
1871 return key_senderror(so, m, EINVAL);
1875 * checking there is SP already or not.
1876 * SPDUPDATE doesn't depend on whether there is a SP or not.
1877 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1880 newsp = key_getsp(&spidx);
1881 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1884 newsp->state = IPSEC_SPSTATE_DEAD;
1889 if (newsp != NULL) {
1891 ipseclog((LOG_DEBUG, "%s: a SP entry exists already.\n",
1893 return key_senderror(so, m, EEXIST);
1897 /* allocation new SP entry */
1898 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1899 return key_senderror(so, m, error);
1902 if ((newsp->id = key_getnewspid()) == 0) {
1904 return key_senderror(so, m, ENOBUFS);
1907 /* XXX boundary check against sa_len */
1908 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1911 src0->sadb_address_prefixlen,
1912 dst0->sadb_address_prefixlen,
1913 src0->sadb_address_proto,
1916 /* sanity check on addr pair */
1917 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1918 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1920 return key_senderror(so, m, EINVAL);
1922 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1923 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1925 return key_senderror(so, m, EINVAL);
1928 if (newsp->req && newsp->req->saidx.src.sa.sa_family) {
1929 struct sockaddr *sa;
1930 sa = (struct sockaddr *)(src0 + 1);
1931 if (sa->sa_family != newsp->req->saidx.src.sa.sa_family) {
1933 return key_senderror(so, m, EINVAL);
1936 if (newsp->req && newsp->req->saidx.dst.sa.sa_family) {
1937 struct sockaddr *sa;
1938 sa = (struct sockaddr *)(dst0 + 1);
1939 if (sa->sa_family != newsp->req->saidx.dst.sa.sa_family) {
1941 return key_senderror(so, m, EINVAL);
1946 newsp->created = time_second;
1947 newsp->lastused = newsp->created;
1948 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1949 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1951 newsp->refcnt = 1; /* do not reclaim until I say I do */
1952 newsp->state = IPSEC_SPSTATE_ALIVE;
1953 LIST_INSERT_TAIL(&V_sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1955 /* delete the entry in spacqtree */
1956 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1957 struct secspacq *spacq = key_getspacq(&spidx);
1958 if (spacq != NULL) {
1959 /* reset counter in order to deletion by timehandler. */
1960 spacq->created = time_second;
1967 struct mbuf *n, *mpolicy;
1968 struct sadb_msg *newmsg;
1972 * Note: do not send SADB_X_EXT_NAT_T_* here:
1973 * we are sending traffic endpoints.
1976 /* create new sadb_msg to reply. */
1978 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1979 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1980 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1982 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1984 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1987 return key_senderror(so, m, ENOBUFS);
1989 if (n->m_len < sizeof(*newmsg)) {
1990 n = m_pullup(n, sizeof(*newmsg));
1992 return key_senderror(so, m, ENOBUFS);
1994 newmsg = mtod(n, struct sadb_msg *);
1995 newmsg->sadb_msg_errno = 0;
1996 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1999 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
2000 sizeof(*xpl), &off);
2001 if (mpolicy == NULL) {
2002 /* n is already freed */
2003 return key_senderror(so, m, ENOBUFS);
2005 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
2006 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
2008 return key_senderror(so, m, EINVAL);
2010 xpl->sadb_x_policy_id = newsp->id;
2013 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2018 * get new policy id.
2026 u_int32_t newid = 0;
2027 int count = V_key_spi_trycnt; /* XXX */
2028 struct secpolicy *sp;
2030 /* when requesting to allocate spi ranged */
2032 newid = (V_policy_id = (V_policy_id == ~0 ? 1 : V_policy_id + 1));
2034 if ((sp = key_getspbyid(newid)) == NULL)
2040 if (count == 0 || newid == 0) {
2041 ipseclog((LOG_DEBUG, "%s: to allocate policy id is failed.\n",
2050 * SADB_SPDDELETE processing
2052 * <base, address(SD), policy(*)>
2053 * from the user(?), and set SADB_SASTATE_DEAD,
2055 * <base, address(SD), policy(*)>
2057 * policy(*) including direction of policy.
2059 * m will always be freed.
2062 key_spddelete(so, m, mhp)
2065 const struct sadb_msghdr *mhp;
2067 struct sadb_address *src0, *dst0;
2068 struct sadb_x_policy *xpl0;
2069 struct secpolicyindex spidx;
2070 struct secpolicy *sp;
2072 IPSEC_ASSERT(so != NULL, ("null so"));
2073 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2074 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2075 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2077 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
2078 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
2079 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
2080 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2082 return key_senderror(so, m, EINVAL);
2084 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
2085 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
2086 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2087 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2089 return key_senderror(so, m, EINVAL);
2092 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2093 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2094 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2097 * Note: do not parse SADB_X_EXT_NAT_T_* here:
2098 * we are processing traffic endpoints.
2102 /* XXX boundary check against sa_len */
2103 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2106 src0->sadb_address_prefixlen,
2107 dst0->sadb_address_prefixlen,
2108 src0->sadb_address_proto,
2111 /* checking the direciton. */
2112 switch (xpl0->sadb_x_policy_dir) {
2113 case IPSEC_DIR_INBOUND:
2114 case IPSEC_DIR_OUTBOUND:
2117 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
2118 return key_senderror(so, m, EINVAL);
2121 /* Is there SP in SPD ? */
2122 if ((sp = key_getsp(&spidx)) == NULL) {
2123 ipseclog((LOG_DEBUG, "%s: no SP found.\n", __func__));
2124 return key_senderror(so, m, EINVAL);
2127 /* save policy id to buffer to be returned. */
2128 xpl0->sadb_x_policy_id = sp->id;
2131 sp->state = IPSEC_SPSTATE_DEAD;
2137 struct sadb_msg *newmsg;
2140 * Note: do not send SADB_X_EXT_NAT_T_* here:
2141 * we are sending traffic endpoints.
2144 /* create new sadb_msg to reply. */
2145 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2146 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2148 return key_senderror(so, m, ENOBUFS);
2150 newmsg = mtod(n, struct sadb_msg *);
2151 newmsg->sadb_msg_errno = 0;
2152 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2155 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2160 * SADB_SPDDELETE2 processing
2163 * from the user(?), and set SADB_SASTATE_DEAD,
2167 * policy(*) including direction of policy.
2169 * m will always be freed.
2172 key_spddelete2(so, m, mhp)
2175 const struct sadb_msghdr *mhp;
2178 struct secpolicy *sp;
2180 IPSEC_ASSERT(so != NULL, ("null socket"));
2181 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2182 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2183 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2185 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2186 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2187 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", __func__));
2188 return key_senderror(so, m, EINVAL);
2191 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2193 /* Is there SP in SPD ? */
2194 if ((sp = key_getspbyid(id)) == NULL) {
2195 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2196 return key_senderror(so, m, EINVAL);
2200 sp->state = IPSEC_SPSTATE_DEAD;
2205 struct mbuf *n, *nn;
2206 struct sadb_msg *newmsg;
2209 /* create new sadb_msg to reply. */
2210 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2212 MGETHDR(n, M_DONTWAIT, MT_DATA);
2213 if (n && len > MHLEN) {
2214 MCLGET(n, M_DONTWAIT);
2215 if ((n->m_flags & M_EXT) == 0) {
2221 return key_senderror(so, m, ENOBUFS);
2227 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2228 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2230 IPSEC_ASSERT(off == len, ("length inconsistency (off %u len %u)",
2233 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2234 mhp->extlen[SADB_X_EXT_POLICY], M_DONTWAIT);
2237 return key_senderror(so, m, ENOBUFS);
2240 n->m_pkthdr.len = 0;
2241 for (nn = n; nn; nn = nn->m_next)
2242 n->m_pkthdr.len += nn->m_len;
2244 newmsg = mtod(n, struct sadb_msg *);
2245 newmsg->sadb_msg_errno = 0;
2246 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2249 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2254 * SADB_X_GET processing
2259 * <base, address(SD), policy>
2261 * policy(*) including direction of policy.
2263 * m will always be freed.
2266 key_spdget(so, m, mhp)
2269 const struct sadb_msghdr *mhp;
2272 struct secpolicy *sp;
2275 IPSEC_ASSERT(so != NULL, ("null socket"));
2276 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2277 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2278 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2280 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2281 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2282 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2284 return key_senderror(so, m, EINVAL);
2287 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2289 /* Is there SP in SPD ? */
2290 if ((sp = key_getspbyid(id)) == NULL) {
2291 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2292 return key_senderror(so, m, ENOENT);
2295 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2299 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2301 return key_senderror(so, m, ENOBUFS);
2305 * SADB_X_SPDACQUIRE processing.
2306 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2309 * to KMD, and expect to receive
2310 * <base> with SADB_X_SPDACQUIRE if error occured,
2313 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2314 * policy(*) is without policy requests.
2317 * others: error number
2321 struct secpolicy *sp;
2323 struct mbuf *result = NULL, *m;
2324 struct secspacq *newspacq;
2326 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2327 IPSEC_ASSERT(sp->req == NULL, ("policy exists"));
2328 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
2329 ("policy not IPSEC %u", sp->policy));
2331 /* Get an entry to check whether sent message or not. */
2332 newspacq = key_getspacq(&sp->spidx);
2333 if (newspacq != NULL) {
2334 if (V_key_blockacq_count < newspacq->count) {
2335 /* reset counter and do send message. */
2336 newspacq->count = 0;
2338 /* increment counter and do nothing. */
2344 /* make new entry for blocking to send SADB_ACQUIRE. */
2345 newspacq = key_newspacq(&sp->spidx);
2346 if (newspacq == NULL)
2350 /* create new sadb_msg to reply. */
2351 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2357 result->m_pkthdr.len = 0;
2358 for (m = result; m; m = m->m_next)
2359 result->m_pkthdr.len += m->m_len;
2361 mtod(result, struct sadb_msg *)->sadb_msg_len =
2362 PFKEY_UNIT64(result->m_pkthdr.len);
2364 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2368 * SADB_SPDFLUSH processing
2371 * from the user, and free all entries in secpctree.
2375 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2377 * m will always be freed.
2380 key_spdflush(so, m, mhp)
2383 const struct sadb_msghdr *mhp;
2385 struct sadb_msg *newmsg;
2386 struct secpolicy *sp;
2389 IPSEC_ASSERT(so != NULL, ("null socket"));
2390 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2391 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2392 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2394 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2395 return key_senderror(so, m, EINVAL);
2397 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2399 LIST_FOREACH(sp, &V_sptree[dir], chain)
2400 sp->state = IPSEC_SPSTATE_DEAD;
2404 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2405 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2406 return key_senderror(so, m, ENOBUFS);
2412 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2413 newmsg = mtod(m, struct sadb_msg *);
2414 newmsg->sadb_msg_errno = 0;
2415 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2417 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2421 * SADB_SPDDUMP processing
2424 * from the user, and dump all SP leaves
2429 * m will always be freed.
2432 key_spddump(so, m, mhp)
2435 const struct sadb_msghdr *mhp;
2437 struct secpolicy *sp;
2442 IPSEC_ASSERT(so != NULL, ("null socket"));
2443 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2444 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2445 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2447 /* search SPD entry and get buffer size. */
2450 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2451 LIST_FOREACH(sp, &V_sptree[dir], chain) {
2458 return key_senderror(so, m, ENOENT);
2461 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2462 LIST_FOREACH(sp, &V_sptree[dir], chain) {
2464 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2465 mhp->msg->sadb_msg_pid);
2468 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2477 static struct mbuf *
2478 key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq, u_int32_t pid)
2480 struct mbuf *result = NULL, *m;
2481 struct seclifetime lt;
2483 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2489 * Note: do not send SADB_X_EXT_NAT_T_* here:
2490 * we are sending traffic endpoints.
2492 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2493 &sp->spidx.src.sa, sp->spidx.prefs,
2494 sp->spidx.ul_proto);
2499 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2500 &sp->spidx.dst.sa, sp->spidx.prefd,
2501 sp->spidx.ul_proto);
2512 lt.addtime=sp->created;
2513 lt.usetime= sp->lastused;
2514 m = key_setlifetime(<, SADB_EXT_LIFETIME_CURRENT);
2519 lt.addtime=sp->lifetime;
2520 lt.usetime= sp->validtime;
2521 m = key_setlifetime(<, SADB_EXT_LIFETIME_HARD);
2527 if ((result->m_flags & M_PKTHDR) == 0)
2530 if (result->m_len < sizeof(struct sadb_msg)) {
2531 result = m_pullup(result, sizeof(struct sadb_msg));
2536 result->m_pkthdr.len = 0;
2537 for (m = result; m; m = m->m_next)
2538 result->m_pkthdr.len += m->m_len;
2540 mtod(result, struct sadb_msg *)->sadb_msg_len =
2541 PFKEY_UNIT64(result->m_pkthdr.len);
2551 * get PFKEY message length for security policy and request.
2554 key_getspreqmsglen(sp)
2555 struct secpolicy *sp;
2559 tlen = sizeof(struct sadb_x_policy);
2561 /* if is the policy for ipsec ? */
2562 if (sp->policy != IPSEC_POLICY_IPSEC)
2565 /* get length of ipsec requests */
2567 struct ipsecrequest *isr;
2570 for (isr = sp->req; isr != NULL; isr = isr->next) {
2571 len = sizeof(struct sadb_x_ipsecrequest)
2572 + isr->saidx.src.sa.sa_len
2573 + isr->saidx.dst.sa.sa_len;
2575 tlen += PFKEY_ALIGN8(len);
2583 * SADB_SPDEXPIRE processing
2585 * <base, address(SD), lifetime(CH), policy>
2589 * others : error number
2593 struct secpolicy *sp;
2595 struct mbuf *result = NULL, *m;
2598 struct sadb_lifetime *lt;
2600 /* XXX: Why do we lock ? */
2602 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2604 /* set msg header */
2605 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2612 /* create lifetime extension (current and hard) */
2613 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2614 m = key_alloc_mbuf(len);
2615 if (!m || m->m_next) { /*XXX*/
2621 bzero(mtod(m, caddr_t), len);
2622 lt = mtod(m, struct sadb_lifetime *);
2623 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2624 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2625 lt->sadb_lifetime_allocations = 0;
2626 lt->sadb_lifetime_bytes = 0;
2627 lt->sadb_lifetime_addtime = sp->created;
2628 lt->sadb_lifetime_usetime = sp->lastused;
2629 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2630 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2631 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2632 lt->sadb_lifetime_allocations = 0;
2633 lt->sadb_lifetime_bytes = 0;
2634 lt->sadb_lifetime_addtime = sp->lifetime;
2635 lt->sadb_lifetime_usetime = sp->validtime;
2639 * Note: do not send SADB_X_EXT_NAT_T_* here:
2640 * we are sending traffic endpoints.
2643 /* set sadb_address for source */
2644 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2646 sp->spidx.prefs, sp->spidx.ul_proto);
2653 /* set sadb_address for destination */
2654 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2656 sp->spidx.prefd, sp->spidx.ul_proto);
2671 if ((result->m_flags & M_PKTHDR) == 0) {
2676 if (result->m_len < sizeof(struct sadb_msg)) {
2677 result = m_pullup(result, sizeof(struct sadb_msg));
2678 if (result == NULL) {
2684 result->m_pkthdr.len = 0;
2685 for (m = result; m; m = m->m_next)
2686 result->m_pkthdr.len += m->m_len;
2688 mtod(result, struct sadb_msg *)->sadb_msg_len =
2689 PFKEY_UNIT64(result->m_pkthdr.len);
2691 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2699 /* %%% SAD management */
2701 * allocating a memory for new SA head, and copy from the values of mhp.
2702 * OUT: NULL : failure due to the lack of memory.
2703 * others : pointer to new SA head.
2705 static struct secashead *
2707 struct secasindex *saidx;
2709 struct secashead *newsah;
2711 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
2713 newsah = malloc(sizeof(struct secashead), M_IPSEC_SAH, M_NOWAIT|M_ZERO);
2714 if (newsah != NULL) {
2716 for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++)
2717 LIST_INIT(&newsah->savtree[i]);
2718 newsah->saidx = *saidx;
2720 /* add to saidxtree */
2721 newsah->state = SADB_SASTATE_MATURE;
2724 LIST_INSERT_HEAD(&V_sahtree, newsah, chain);
2731 * delete SA index and all SA registerd.
2735 struct secashead *sah;
2737 struct secasvar *sav, *nextsav;
2741 IPSEC_ASSERT(sah != NULL, ("NULL sah"));
2742 SAHTREE_LOCK_ASSERT();
2744 /* searching all SA registerd in the secindex. */
2746 stateidx < _ARRAYLEN(saorder_state_any);
2748 u_int state = saorder_state_any[stateidx];
2749 LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain, nextsav) {
2750 if (sav->refcnt == 0) {
2752 KEY_CHKSASTATE(state, sav->state, __func__);
2754 * do NOT call KEY_FREESAV here:
2755 * it will only delete the sav if refcnt == 1,
2756 * where we already know that refcnt == 0
2760 /* give up to delete this sa */
2765 if (!zombie) { /* delete only if there are savs */
2766 /* remove from tree of SA index */
2767 if (__LIST_CHAINED(sah))
2768 LIST_REMOVE(sah, chain);
2769 if (sah->route_cache.sa_route.ro_rt) {
2770 RTFREE(sah->route_cache.sa_route.ro_rt);
2771 sah->route_cache.sa_route.ro_rt = (struct rtentry *)NULL;
2773 free(sah, M_IPSEC_SAH);
2778 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2779 * and copy the values of mhp into new buffer.
2780 * When SAD message type is GETSPI:
2781 * to set sequence number from acq_seq++,
2782 * to set zero to SPI.
2783 * not to call key_setsava().
2785 * others : pointer to new secasvar.
2787 * does not modify mbuf. does not free mbuf on error.
2789 static struct secasvar *
2790 key_newsav(m, mhp, sah, errp, where, tag)
2792 const struct sadb_msghdr *mhp;
2793 struct secashead *sah;
2798 struct secasvar *newsav;
2799 const struct sadb_sa *xsa;
2801 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2802 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2803 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2804 IPSEC_ASSERT(sah != NULL, ("null secashead"));
2806 newsav = malloc(sizeof(struct secasvar), M_IPSEC_SA, M_NOWAIT|M_ZERO);
2807 if (newsav == NULL) {
2808 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2813 switch (mhp->msg->sadb_msg_type) {
2817 #ifdef IPSEC_DOSEQCHECK
2818 /* sync sequence number */
2819 if (mhp->msg->sadb_msg_seq == 0)
2821 (V_acq_seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq));
2824 newsav->seq = mhp->msg->sadb_msg_seq;
2829 if (mhp->ext[SADB_EXT_SA] == NULL) {
2830 free(newsav, M_IPSEC_SA);
2832 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2837 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2838 newsav->spi = xsa->sadb_sa_spi;
2839 newsav->seq = mhp->msg->sadb_msg_seq;
2842 free(newsav, M_IPSEC_SA);
2849 /* copy sav values */
2850 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2851 *errp = key_setsaval(newsav, m, mhp);
2853 free(newsav, M_IPSEC_SA);
2859 SECASVAR_LOCK_INIT(newsav);
2862 newsav->created = time_second;
2863 newsav->pid = mhp->msg->sadb_msg_pid;
2868 newsav->state = SADB_SASTATE_LARVAL;
2871 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2875 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
2876 printf("DP %s from %s:%u return SP:%p\n", __func__,
2877 where, tag, newsav));
2883 * free() SA variable entry.
2886 key_cleansav(struct secasvar *sav)
2889 * Cleanup xform state. Note that zeroize'ing causes the
2890 * keys to be cleared; otherwise we must do it ourself.
2892 if (sav->tdb_xform != NULL) {
2893 sav->tdb_xform->xf_zeroize(sav);
2894 sav->tdb_xform = NULL;
2896 KASSERT(sav->iv == NULL, ("iv but no xform"));
2897 if (sav->key_auth != NULL)
2898 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
2899 if (sav->key_enc != NULL)
2900 bzero(sav->key_enc->key_data, _KEYLEN(sav->key_enc));
2902 if (sav->key_auth != NULL) {
2903 if (sav->key_auth->key_data != NULL)
2904 free(sav->key_auth->key_data, M_IPSEC_MISC);
2905 free(sav->key_auth, M_IPSEC_MISC);
2906 sav->key_auth = NULL;
2908 if (sav->key_enc != NULL) {
2909 if (sav->key_enc->key_data != NULL)
2910 free(sav->key_enc->key_data, M_IPSEC_MISC);
2911 free(sav->key_enc, M_IPSEC_MISC);
2912 sav->key_enc = NULL;
2915 bzero(sav->sched, sav->schedlen);
2916 free(sav->sched, M_IPSEC_MISC);
2919 if (sav->replay != NULL) {
2920 free(sav->replay, M_IPSEC_MISC);
2923 if (sav->lft_c != NULL) {
2924 free(sav->lft_c, M_IPSEC_MISC);
2927 if (sav->lft_h != NULL) {
2928 free(sav->lft_h, M_IPSEC_MISC);
2931 if (sav->lft_s != NULL) {
2932 free(sav->lft_s, M_IPSEC_MISC);
2938 * free() SA variable entry.
2942 struct secasvar *sav;
2944 IPSEC_ASSERT(sav != NULL, ("null sav"));
2945 IPSEC_ASSERT(sav->refcnt == 0, ("reference count %u > 0", sav->refcnt));
2947 /* remove from SA header */
2948 if (__LIST_CHAINED(sav))
2949 LIST_REMOVE(sav, chain);
2951 SECASVAR_LOCK_DESTROY(sav);
2952 free(sav, M_IPSEC_SA);
2959 * others : found, pointer to a SA.
2961 static struct secashead *
2963 struct secasindex *saidx;
2965 struct secashead *sah;
2968 LIST_FOREACH(sah, &V_sahtree, chain) {
2969 if (sah->state == SADB_SASTATE_DEAD)
2971 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2980 * check not to be duplicated SPI.
2981 * NOTE: this function is too slow due to searching all SAD.
2984 * others : found, pointer to a SA.
2986 static struct secasvar *
2987 key_checkspidup(saidx, spi)
2988 struct secasindex *saidx;
2991 struct secashead *sah;
2992 struct secasvar *sav;
2994 /* check address family */
2995 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
2996 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
3004 LIST_FOREACH(sah, &V_sahtree, chain) {
3005 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
3007 sav = key_getsavbyspi(sah, spi);
3017 * search SAD litmited alive SA, protocol, SPI.
3020 * others : found, pointer to a SA.
3022 static struct secasvar *
3023 key_getsavbyspi(sah, spi)
3024 struct secashead *sah;
3027 struct secasvar *sav;
3028 u_int stateidx, state;
3031 SAHTREE_LOCK_ASSERT();
3032 /* search all status */
3034 stateidx < _ARRAYLEN(saorder_state_alive);
3037 state = saorder_state_alive[stateidx];
3038 LIST_FOREACH(sav, &sah->savtree[state], chain) {
3041 if (sav->state != state) {
3042 ipseclog((LOG_DEBUG, "%s: "
3043 "invalid sav->state (queue: %d SA: %d)\n",
3044 __func__, state, sav->state));
3048 if (sav->spi == spi)
3057 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
3058 * You must update these if need.
3062 * does not modify mbuf. does not free mbuf on error.
3065 key_setsaval(sav, m, mhp)
3066 struct secasvar *sav;
3068 const struct sadb_msghdr *mhp;
3072 IPSEC_ASSERT(m != NULL, ("null mbuf"));
3073 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
3074 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
3076 /* initialization */
3078 sav->key_auth = NULL;
3079 sav->key_enc = NULL;
3086 sav->tdb_xform = NULL; /* transform */
3087 sav->tdb_encalgxform = NULL; /* encoding algorithm */
3088 sav->tdb_authalgxform = NULL; /* authentication algorithm */
3089 sav->tdb_compalgxform = NULL; /* compression algorithm */
3090 /* Initialize even if NAT-T not compiled in: */
3092 sav->natt_esp_frag_len = 0;
3095 if (mhp->ext[SADB_EXT_SA] != NULL) {
3096 const struct sadb_sa *sa0;
3098 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
3099 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
3104 sav->alg_auth = sa0->sadb_sa_auth;
3105 sav->alg_enc = sa0->sadb_sa_encrypt;
3106 sav->flags = sa0->sadb_sa_flags;
3109 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
3110 sav->replay = (struct secreplay *)
3111 malloc(sizeof(struct secreplay)+sa0->sadb_sa_replay, M_IPSEC_MISC, M_NOWAIT|M_ZERO);
3112 if (sav->replay == NULL) {
3113 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3118 if (sa0->sadb_sa_replay != 0)
3119 sav->replay->bitmap = (caddr_t)(sav->replay+1);
3120 sav->replay->wsize = sa0->sadb_sa_replay;
3124 /* Authentication keys */
3125 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
3126 const struct sadb_key *key0;
3129 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
3130 len = mhp->extlen[SADB_EXT_KEY_AUTH];
3133 if (len < sizeof(*key0)) {
3137 switch (mhp->msg->sadb_msg_satype) {
3138 case SADB_SATYPE_AH:
3139 case SADB_SATYPE_ESP:
3140 case SADB_X_SATYPE_TCPSIGNATURE:
3141 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3142 sav->alg_auth != SADB_X_AALG_NULL)
3145 case SADB_X_SATYPE_IPCOMP:
3151 ipseclog((LOG_DEBUG, "%s: invalid key_auth values.\n",
3156 sav->key_auth = (struct seckey *)key_dup_keymsg(key0, len,
3158 if (sav->key_auth == NULL ) {
3159 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3166 /* Encryption key */
3167 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3168 const struct sadb_key *key0;
3171 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3172 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3175 if (len < sizeof(*key0)) {
3179 switch (mhp->msg->sadb_msg_satype) {
3180 case SADB_SATYPE_ESP:
3181 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3182 sav->alg_enc != SADB_EALG_NULL) {
3186 sav->key_enc = (struct seckey *)key_dup_keymsg(key0,
3189 if (sav->key_enc == NULL) {
3190 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3196 case SADB_X_SATYPE_IPCOMP:
3197 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3199 sav->key_enc = NULL; /*just in case*/
3201 case SADB_SATYPE_AH:
3202 case SADB_X_SATYPE_TCPSIGNATURE:
3208 ipseclog((LOG_DEBUG, "%s: invalid key_enc value.\n",
3217 switch (mhp->msg->sadb_msg_satype) {
3218 case SADB_SATYPE_AH:
3219 error = xform_init(sav, XF_AH);
3221 case SADB_SATYPE_ESP:
3222 error = xform_init(sav, XF_ESP);
3224 case SADB_X_SATYPE_IPCOMP:
3225 error = xform_init(sav, XF_IPCOMP);
3227 case SADB_X_SATYPE_TCPSIGNATURE:
3228 error = xform_init(sav, XF_TCPSIGNATURE);
3232 ipseclog((LOG_DEBUG, "%s: unable to initialize SA type %u.\n",
3233 __func__, mhp->msg->sadb_msg_satype));
3238 sav->created = time_second;
3240 /* make lifetime for CURRENT */
3241 sav->lft_c = malloc(sizeof(struct seclifetime), M_IPSEC_MISC, M_NOWAIT);
3242 if (sav->lft_c == NULL) {
3243 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3248 sav->lft_c->allocations = 0;
3249 sav->lft_c->bytes = 0;
3250 sav->lft_c->addtime = time_second;
3251 sav->lft_c->usetime = 0;
3253 /* lifetimes for HARD and SOFT */
3255 const struct sadb_lifetime *lft0;
3257 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3259 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3263 sav->lft_h = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3264 if (sav->lft_h == NULL) {
3265 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3269 /* to be initialize ? */
3272 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3274 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3278 sav->lft_s = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3279 if (sav->lft_s == NULL) {
3280 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3284 /* to be initialize ? */
3291 /* initialization */
3298 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3303 key_mature(struct secasvar *sav)
3307 /* check SPI value */
3308 switch (sav->sah->saidx.proto) {
3312 * RFC 4302, 2.4. Security Parameters Index (SPI), SPI values
3313 * 1-255 reserved by IANA for future use,
3314 * 0 for implementation specific, local use.
3316 if (ntohl(sav->spi) <= 255) {
3317 ipseclog((LOG_DEBUG, "%s: illegal range of SPI %u.\n",
3318 __func__, (u_int32_t)ntohl(sav->spi)));
3325 switch (sav->sah->saidx.proto) {
3328 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3329 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3330 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3331 "given to old-esp.\n", __func__));
3334 error = xform_init(sav, XF_ESP);
3338 if (sav->flags & SADB_X_EXT_DERIV) {
3339 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3340 "given to AH SA.\n", __func__));
3343 if (sav->alg_enc != SADB_EALG_NONE) {
3344 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3345 "mismated.\n", __func__));
3348 error = xform_init(sav, XF_AH);
3350 case IPPROTO_IPCOMP:
3351 if (sav->alg_auth != SADB_AALG_NONE) {
3352 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3353 "mismated.\n", __func__));
3356 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3357 && ntohl(sav->spi) >= 0x10000) {
3358 ipseclog((LOG_DEBUG, "%s: invalid cpi for IPComp.\n",
3362 error = xform_init(sav, XF_IPCOMP);
3365 if (sav->alg_enc != SADB_EALG_NONE) {
3366 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3367 "mismated.\n", __func__));
3370 error = xform_init(sav, XF_TCPSIGNATURE);
3373 ipseclog((LOG_DEBUG, "%s: Invalid satype.\n", __func__));
3374 error = EPROTONOSUPPORT;
3379 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3386 * subroutine for SADB_GET and SADB_DUMP.
3388 static struct mbuf *
3389 key_setdumpsa(struct secasvar *sav, u_int8_t type, u_int8_t satype,
3390 u_int32_t seq, u_int32_t pid)
3392 struct mbuf *result = NULL, *tres = NULL, *m;
3395 SADB_EXT_SA, SADB_X_EXT_SA2,
3396 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3397 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3398 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3399 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3400 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3402 SADB_X_EXT_NAT_T_TYPE,
3403 SADB_X_EXT_NAT_T_SPORT, SADB_X_EXT_NAT_T_DPORT,
3404 SADB_X_EXT_NAT_T_OAI, SADB_X_EXT_NAT_T_OAR,
3405 SADB_X_EXT_NAT_T_FRAG,
3409 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3414 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3416 switch (dumporder[i]) {
3418 m = key_setsadbsa(sav);
3423 case SADB_X_EXT_SA2:
3424 m = key_setsadbxsa2(sav->sah->saidx.mode,
3425 sav->replay ? sav->replay->count : 0,
3426 sav->sah->saidx.reqid);
3431 case SADB_EXT_ADDRESS_SRC:
3432 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3433 &sav->sah->saidx.src.sa,
3434 FULLMASK, IPSEC_ULPROTO_ANY);
3439 case SADB_EXT_ADDRESS_DST:
3440 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3441 &sav->sah->saidx.dst.sa,
3442 FULLMASK, IPSEC_ULPROTO_ANY);
3447 case SADB_EXT_KEY_AUTH:
3450 m = key_setkey(sav->key_auth, SADB_EXT_KEY_AUTH);
3455 case SADB_EXT_KEY_ENCRYPT:
3458 m = key_setkey(sav->key_enc, SADB_EXT_KEY_ENCRYPT);
3463 case SADB_EXT_LIFETIME_CURRENT:
3466 m = key_setlifetime(sav->lft_c,
3467 SADB_EXT_LIFETIME_CURRENT);
3472 case SADB_EXT_LIFETIME_HARD:
3475 m = key_setlifetime(sav->lft_h,
3476 SADB_EXT_LIFETIME_HARD);
3481 case SADB_EXT_LIFETIME_SOFT:
3484 m = key_setlifetime(sav->lft_s,
3485 SADB_EXT_LIFETIME_SOFT);
3492 case SADB_X_EXT_NAT_T_TYPE:
3493 m = key_setsadbxtype(sav->natt_type);
3498 case SADB_X_EXT_NAT_T_DPORT:
3499 m = key_setsadbxport(
3500 KEY_PORTFROMSADDR(&sav->sah->saidx.dst),
3501 SADB_X_EXT_NAT_T_DPORT);
3506 case SADB_X_EXT_NAT_T_SPORT:
3507 m = key_setsadbxport(
3508 KEY_PORTFROMSADDR(&sav->sah->saidx.src),
3509 SADB_X_EXT_NAT_T_SPORT);
3514 case SADB_X_EXT_NAT_T_OAI:
3515 case SADB_X_EXT_NAT_T_OAR:
3516 case SADB_X_EXT_NAT_T_FRAG:
3517 /* We do not (yet) support those. */
3521 case SADB_EXT_ADDRESS_PROXY:
3522 case SADB_EXT_IDENTITY_SRC:
3523 case SADB_EXT_IDENTITY_DST:
3524 /* XXX: should we brought from SPD ? */
3525 case SADB_EXT_SENSITIVITY:
3538 m_cat(result, tres);
3539 if (result->m_len < sizeof(struct sadb_msg)) {
3540 result = m_pullup(result, sizeof(struct sadb_msg));
3545 result->m_pkthdr.len = 0;
3546 for (m = result; m; m = m->m_next)
3547 result->m_pkthdr.len += m->m_len;
3549 mtod(result, struct sadb_msg *)->sadb_msg_len =
3550 PFKEY_UNIT64(result->m_pkthdr.len);
3561 * set data into sadb_msg.
3563 static struct mbuf *
3564 key_setsadbmsg(u_int8_t type, u_int16_t tlen, u_int8_t satype, u_int32_t seq,
3565 pid_t pid, u_int16_t reserved)
3571 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3574 MGETHDR(m, M_DONTWAIT, MT_DATA);
3575 if (m && len > MHLEN) {
3576 MCLGET(m, M_DONTWAIT);
3577 if ((m->m_flags & M_EXT) == 0) {
3584 m->m_pkthdr.len = m->m_len = len;
3587 p = mtod(m, struct sadb_msg *);
3590 p->sadb_msg_version = PF_KEY_V2;
3591 p->sadb_msg_type = type;
3592 p->sadb_msg_errno = 0;
3593 p->sadb_msg_satype = satype;
3594 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3595 p->sadb_msg_reserved = reserved;
3596 p->sadb_msg_seq = seq;
3597 p->sadb_msg_pid = (u_int32_t)pid;
3603 * copy secasvar data into sadb_address.
3605 static struct mbuf *
3607 struct secasvar *sav;
3613 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3614 m = key_alloc_mbuf(len);
3615 if (!m || m->m_next) { /*XXX*/
3621 p = mtod(m, struct sadb_sa *);
3624 p->sadb_sa_len = PFKEY_UNIT64(len);
3625 p->sadb_sa_exttype = SADB_EXT_SA;
3626 p->sadb_sa_spi = sav->spi;
3627 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3628 p->sadb_sa_state = sav->state;
3629 p->sadb_sa_auth = sav->alg_auth;
3630 p->sadb_sa_encrypt = sav->alg_enc;
3631 p->sadb_sa_flags = sav->flags;
3637 * set data into sadb_address.
3639 static struct mbuf *
3640 key_setsadbaddr(u_int16_t exttype, const struct sockaddr *saddr, u_int8_t prefixlen, u_int16_t ul_proto)
3643 struct sadb_address *p;
3646 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3647 PFKEY_ALIGN8(saddr->sa_len);
3648 m = key_alloc_mbuf(len);
3649 if (!m || m->m_next) { /*XXX*/
3655 p = mtod(m, struct sadb_address *);
3658 p->sadb_address_len = PFKEY_UNIT64(len);
3659 p->sadb_address_exttype = exttype;
3660 p->sadb_address_proto = ul_proto;
3661 if (prefixlen == FULLMASK) {
3662 switch (saddr->sa_family) {
3664 prefixlen = sizeof(struct in_addr) << 3;
3667 prefixlen = sizeof(struct in6_addr) << 3;
3673 p->sadb_address_prefixlen = prefixlen;
3674 p->sadb_address_reserved = 0;
3677 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3684 * set data into sadb_x_sa2.
3686 static struct mbuf *
3687 key_setsadbxsa2(u_int8_t mode, u_int32_t seq, u_int32_t reqid)
3690 struct sadb_x_sa2 *p;
3693 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3694 m = key_alloc_mbuf(len);
3695 if (!m || m->m_next) { /*XXX*/
3701 p = mtod(m, struct sadb_x_sa2 *);
3704 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3705 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3706 p->sadb_x_sa2_mode = mode;
3707 p->sadb_x_sa2_reserved1 = 0;
3708 p->sadb_x_sa2_reserved2 = 0;
3709 p->sadb_x_sa2_sequence = seq;
3710 p->sadb_x_sa2_reqid = reqid;
3717 * Set a type in sadb_x_nat_t_type.
3719 static struct mbuf *
3720 key_setsadbxtype(u_int16_t type)
3724 struct sadb_x_nat_t_type *p;
3726 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_type));
3728 m = key_alloc_mbuf(len);
3729 if (!m || m->m_next) { /*XXX*/
3735 p = mtod(m, struct sadb_x_nat_t_type *);
3738 p->sadb_x_nat_t_type_len = PFKEY_UNIT64(len);
3739 p->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
3740 p->sadb_x_nat_t_type_type = type;
3745 * Set a port in sadb_x_nat_t_port.
3746 * In contrast to default RFC 2367 behaviour, port is in network byte order.
3748 static struct mbuf *
3749 key_setsadbxport(u_int16_t port, u_int16_t type)
3753 struct sadb_x_nat_t_port *p;
3755 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_port));
3757 m = key_alloc_mbuf(len);
3758 if (!m || m->m_next) { /*XXX*/
3764 p = mtod(m, struct sadb_x_nat_t_port *);
3767 p->sadb_x_nat_t_port_len = PFKEY_UNIT64(len);
3768 p->sadb_x_nat_t_port_exttype = type;
3769 p->sadb_x_nat_t_port_port = port;
3775 * Get port from sockaddr. Port is in network byte order.
3778 key_portfromsaddr(struct sockaddr *sa)
3781 switch (sa->sa_family) {
3784 return ((struct sockaddr_in *)sa)->sin_port;
3788 return ((struct sockaddr_in6 *)sa)->sin6_port;
3791 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
3792 printf("DP %s unexpected address family %d\n",
3793 __func__, sa->sa_family));
3796 #endif /* IPSEC_NAT_T */
3799 * Set port in struct sockaddr. Port is in network byte order.
3802 key_porttosaddr(struct sockaddr *sa, u_int16_t port)
3805 switch (sa->sa_family) {
3808 ((struct sockaddr_in *)sa)->sin_port = port;
3813 ((struct sockaddr_in6 *)sa)->sin6_port = port;
3817 ipseclog((LOG_DEBUG, "%s: unexpected address family %d.\n",
3818 __func__, sa->sa_family));
3824 * set data into sadb_x_policy
3826 static struct mbuf *
3827 key_setsadbxpolicy(u_int16_t type, u_int8_t dir, u_int32_t id)
3830 struct sadb_x_policy *p;
3833 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3834 m = key_alloc_mbuf(len);
3835 if (!m || m->m_next) { /*XXX*/
3841 p = mtod(m, struct sadb_x_policy *);
3844 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3845 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3846 p->sadb_x_policy_type = type;
3847 p->sadb_x_policy_dir = dir;
3848 p->sadb_x_policy_id = id;
3854 /* Take a key message (sadb_key) from the socket and turn it into one
3855 * of the kernel's key structures (seckey).
3857 * IN: pointer to the src
3858 * OUT: NULL no more memory
3861 key_dup_keymsg(const struct sadb_key *src, u_int len,
3862 struct malloc_type *type)
3865 dst = (struct seckey *)malloc(sizeof(struct seckey), type, M_NOWAIT);
3867 dst->bits = src->sadb_key_bits;
3868 dst->key_data = (char *)malloc(len, type, M_NOWAIT);
3869 if (dst->key_data != NULL) {
3870 bcopy((const char *)src + sizeof(struct sadb_key),
3871 dst->key_data, len);
3873 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3879 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3886 /* Take a lifetime message (sadb_lifetime) passed in on a socket and
3887 * turn it into one of the kernel's lifetime structures (seclifetime).
3889 * IN: pointer to the destination, source and malloc type
3890 * OUT: NULL, no more memory
3893 static struct seclifetime *
3894 key_dup_lifemsg(const struct sadb_lifetime *src,
3895 struct malloc_type *type)
3897 struct seclifetime *dst = NULL;
3899 dst = (struct seclifetime *)malloc(sizeof(struct seclifetime),
3903 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3905 dst->allocations = src->sadb_lifetime_allocations;
3906 dst->bytes = src->sadb_lifetime_bytes;
3907 dst->addtime = src->sadb_lifetime_addtime;
3908 dst->usetime = src->sadb_lifetime_usetime;
3913 /* compare my own address
3914 * OUT: 1: true, i.e. my address.
3919 struct sockaddr *sa;
3922 struct sockaddr_in *sin;
3923 struct in_ifaddr *ia;
3926 IPSEC_ASSERT(sa != NULL, ("null sockaddr"));
3928 switch (sa->sa_family) {
3931 sin = (struct sockaddr_in *)sa;
3933 for (ia = V_in_ifaddrhead.tqh_first; ia;
3934 ia = ia->ia_link.tqe_next)
3936 if (sin->sin_family == ia->ia_addr.sin_family &&
3937 sin->sin_len == ia->ia_addr.sin_len &&
3938 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3940 IN_IFADDR_RUNLOCK();
3944 IN_IFADDR_RUNLOCK();
3949 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3958 * compare my own address for IPv6.
3961 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3963 #include <netinet6/in6_var.h>
3967 struct sockaddr_in6 *sin6;
3969 struct in6_ifaddr *ia;
3971 struct in6_multi *in6m;
3975 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
3976 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3977 (struct sockaddr *)&ia->ia_addr, 0) == 0) {
3978 IN6_IFADDR_RUNLOCK();
3985 * XXX why do we care about multlicast here while we don't care
3986 * about IPv4 multicast??
3990 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3992 IN6_IFADDR_RUNLOCK();
3997 IN6_IFADDR_RUNLOCK();
3999 /* loopback, just for safety */
4000 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
4008 * compare two secasindex structure.
4009 * flag can specify to compare 2 saidxes.
4010 * compare two secasindex structure without both mode and reqid.
4011 * don't compare port.
4013 * saidx0: source, it can be in SAD.
4021 const struct secasindex *saidx0,
4022 const struct secasindex *saidx1,
4028 if (saidx0 == NULL && saidx1 == NULL)
4031 if (saidx0 == NULL || saidx1 == NULL)
4034 if (saidx0->proto != saidx1->proto)
4037 if (flag == CMP_EXACTLY) {
4038 if (saidx0->mode != saidx1->mode)
4040 if (saidx0->reqid != saidx1->reqid)
4042 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
4043 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
4047 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
4048 if (flag == CMP_MODE_REQID
4049 ||flag == CMP_REQID) {
4051 * If reqid of SPD is non-zero, unique SA is required.
4052 * The result must be of same reqid in this case.
4054 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
4058 if (flag == CMP_MODE_REQID) {
4059 if (saidx0->mode != IPSEC_MODE_ANY
4060 && saidx0->mode != saidx1->mode)
4066 * If NAT-T is enabled, check ports for tunnel mode.
4067 * Do not check ports if they are set to zero in the SPD.
4068 * Also do not do it for transport mode, as there is no
4069 * port information available in the SP.
4071 if (saidx1->mode == IPSEC_MODE_TUNNEL &&
4072 saidx1->src.sa.sa_family == AF_INET &&
4073 saidx1->dst.sa.sa_family == AF_INET &&
4074 ((const struct sockaddr_in *)(&saidx1->src))->sin_port &&
4075 ((const struct sockaddr_in *)(&saidx1->dst))->sin_port)
4077 #endif /* IPSEC_NAT_T */
4079 if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, chkport) != 0) {
4082 if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, chkport) != 0) {
4091 * compare two secindex structure exactly.
4093 * spidx0: source, it is often in SPD.
4094 * spidx1: object, it is often from PFKEY message.
4100 key_cmpspidx_exactly(
4101 struct secpolicyindex *spidx0,
4102 struct secpolicyindex *spidx1)
4105 if (spidx0 == NULL && spidx1 == NULL)
4108 if (spidx0 == NULL || spidx1 == NULL)
4111 if (spidx0->prefs != spidx1->prefs
4112 || spidx0->prefd != spidx1->prefd
4113 || spidx0->ul_proto != spidx1->ul_proto)
4116 return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
4117 key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
4121 * compare two secindex structure with mask.
4123 * spidx0: source, it is often in SPD.
4124 * spidx1: object, it is often from IP header.
4130 key_cmpspidx_withmask(
4131 struct secpolicyindex *spidx0,
4132 struct secpolicyindex *spidx1)
4135 if (spidx0 == NULL && spidx1 == NULL)
4138 if (spidx0 == NULL || spidx1 == NULL)
4141 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
4142 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
4143 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
4144 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
4147 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4148 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
4149 && spidx0->ul_proto != spidx1->ul_proto)
4152 switch (spidx0->src.sa.sa_family) {
4154 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
4155 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
4157 if (!key_bbcmp(&spidx0->src.sin.sin_addr,
4158 &spidx1->src.sin.sin_addr, spidx0->prefs))
4162 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
4163 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
4166 * scope_id check. if sin6_scope_id is 0, we regard it
4167 * as a wildcard scope, which matches any scope zone ID.
4169 if (spidx0->src.sin6.sin6_scope_id &&
4170 spidx1->src.sin6.sin6_scope_id &&
4171 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
4173 if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
4174 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
4179 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
4184 switch (spidx0->dst.sa.sa_family) {
4186 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
4187 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
4189 if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
4190 &spidx1->dst.sin.sin_addr, spidx0->prefd))
4194 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
4195 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
4198 * scope_id check. if sin6_scope_id is 0, we regard it
4199 * as a wildcard scope, which matches any scope zone ID.
4201 if (spidx0->dst.sin6.sin6_scope_id &&
4202 spidx1->dst.sin6.sin6_scope_id &&
4203 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
4205 if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
4206 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
4211 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
4216 /* XXX Do we check other field ? e.g. flowinfo */
4221 /* returns 0 on match */
4224 const struct sockaddr *sa1,
4225 const struct sockaddr *sa2,
4231 #define satosin(s) ((const struct sockaddr_in *)s)
4235 #define satosin6(s) ((const struct sockaddr_in6 *)s)
4236 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
4239 switch (sa1->sa_family) {
4241 if (sa1->sa_len != sizeof(struct sockaddr_in))
4243 if (satosin(sa1)->sin_addr.s_addr !=
4244 satosin(sa2)->sin_addr.s_addr) {
4247 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
4251 if (sa1->sa_len != sizeof(struct sockaddr_in6))
4252 return 1; /*EINVAL*/
4253 if (satosin6(sa1)->sin6_scope_id !=
4254 satosin6(sa2)->sin6_scope_id) {
4257 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
4258 &satosin6(sa2)->sin6_addr)) {
4262 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
4267 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
4278 * compare two buffers with mask.
4282 * bits: Number of bits to compare
4288 key_bbcmp(const void *a1, const void *a2, u_int bits)
4290 const unsigned char *p1 = a1;
4291 const unsigned char *p2 = a2;
4293 /* XXX: This could be considerably faster if we compare a word
4294 * at a time, but it is complicated on LSB Endian machines */
4296 /* Handle null pointers */
4297 if (p1 == NULL || p2 == NULL)
4307 u_int8_t mask = ~((1<<(8-bits))-1);
4308 if ((*p1 & mask) != (*p2 & mask))
4311 return 1; /* Match! */
4315 key_flush_spd(time_t now)
4317 static u_int16_t sptree_scangen = 0;
4318 u_int16_t gen = sptree_scangen++;
4319 struct secpolicy *sp;
4323 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4326 LIST_FOREACH(sp, &V_sptree[dir], chain) {
4327 if (sp->scangen == gen) /* previously handled */
4330 if (sp->state == IPSEC_SPSTATE_DEAD &&
4333 * Ensure that we only decrease refcnt once,
4334 * when we're the last consumer.
4335 * Directly call SP_DELREF/key_delsp instead
4336 * of KEY_FREESP to avoid unlocking/relocking
4337 * SPTREE_LOCK before key_delsp: may refcnt
4338 * be increased again during that time ?
4339 * NB: also clean entries created by
4347 if (sp->lifetime == 0 && sp->validtime == 0)
4349 if ((sp->lifetime && now - sp->created > sp->lifetime)
4350 || (sp->validtime && now - sp->lastused > sp->validtime)) {
4351 sp->state = IPSEC_SPSTATE_DEAD;
4362 key_flush_sad(time_t now)
4364 struct secashead *sah, *nextsah;
4365 struct secasvar *sav, *nextsav;
4369 LIST_FOREACH_SAFE(sah, &V_sahtree, chain, nextsah) {
4370 /* if sah has been dead, then delete it and process next sah. */
4371 if (sah->state == SADB_SASTATE_DEAD) {
4376 /* if LARVAL entry doesn't become MATURE, delete it. */
4377 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_LARVAL], chain, nextsav) {
4378 /* Need to also check refcnt for a larval SA ??? */
4379 if (now - sav->created > V_key_larval_lifetime)
4384 * check MATURE entry to start to send expire message
4387 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_MATURE], chain, nextsav) {
4388 /* we don't need to check. */
4389 if (sav->lft_s == NULL)
4393 if (sav->lft_c == NULL) {
4394 ipseclog((LOG_DEBUG,"%s: there is no CURRENT "
4395 "time, why?\n", __func__));
4399 /* check SOFT lifetime */
4400 if (sav->lft_s->addtime != 0 &&
4401 now - sav->created > sav->lft_s->addtime) {
4402 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4404 * Actually, only send expire message if
4405 * SA has been used, as it was done before,
4406 * but should we always send such message,
4407 * and let IKE daemon decide if it should be
4408 * renegotiated or not ?
4409 * XXX expire message will actually NOT be
4410 * sent if SA is only used after soft
4411 * lifetime has been reached, see below
4414 if (sav->lft_c->usetime != 0)
4417 /* check SOFT lifetime by bytes */
4419 * XXX I don't know the way to delete this SA
4420 * when new SA is installed. Caution when it's
4421 * installed too big lifetime by time.
4423 else if (sav->lft_s->bytes != 0 &&
4424 sav->lft_s->bytes < sav->lft_c->bytes) {
4426 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4428 * XXX If we keep to send expire
4429 * message in the status of
4430 * DYING. Do remove below code.
4436 /* check DYING entry to change status to DEAD. */
4437 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DYING], chain, nextsav) {
4438 /* we don't need to check. */
4439 if (sav->lft_h == NULL)
4443 if (sav->lft_c == NULL) {
4444 ipseclog((LOG_DEBUG, "%s: there is no CURRENT "
4445 "time, why?\n", __func__));
4449 if (sav->lft_h->addtime != 0 &&
4450 now - sav->created > sav->lft_h->addtime) {
4451 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4454 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4455 else if (sav->lft_s != NULL
4456 && sav->lft_s->addtime != 0
4457 && now - sav->created > sav->lft_s->addtime) {
4459 * XXX: should be checked to be
4460 * installed the valid SA.
4464 * If there is no SA then sending
4470 /* check HARD lifetime by bytes */
4471 else if (sav->lft_h->bytes != 0 &&
4472 sav->lft_h->bytes < sav->lft_c->bytes) {
4473 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4478 /* delete entry in DEAD */
4479 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DEAD], chain, nextsav) {
4481 if (sav->state != SADB_SASTATE_DEAD) {
4482 ipseclog((LOG_DEBUG, "%s: invalid sav->state "
4483 "(queue: %d SA: %d): kill it anyway\n",
4485 SADB_SASTATE_DEAD, sav->state));
4488 * do not call key_freesav() here.
4489 * sav should already be freed, and sav->refcnt
4490 * shows other references to sav
4491 * (such as from SPD).
4499 key_flush_acq(time_t now)
4501 struct secacq *acq, *nextacq;
4505 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
4506 nextacq = LIST_NEXT(acq, chain);
4507 if (now - acq->created > V_key_blockacq_lifetime
4508 && __LIST_CHAINED(acq)) {
4509 LIST_REMOVE(acq, chain);
4510 free(acq, M_IPSEC_SAQ);
4517 key_flush_spacq(time_t now)
4519 struct secspacq *acq, *nextacq;
4523 for (acq = LIST_FIRST(&V_spacqtree); acq != NULL; acq = nextacq) {
4524 nextacq = LIST_NEXT(acq, chain);
4525 if (now - acq->created > V_key_blockacq_lifetime
4526 && __LIST_CHAINED(acq)) {
4527 LIST_REMOVE(acq, chain);
4528 free(acq, M_IPSEC_SAQ);
4536 * scanning SPD and SAD to check status for each entries,
4537 * and do to remove or to expire.
4538 * XXX: year 2038 problem may remain.
4541 key_timehandler(void)
4543 VNET_ITERATOR_DECL(vnet_iter);
4544 time_t now = time_second;
4546 VNET_LIST_RLOCK_NOSLEEP();
4547 VNET_FOREACH(vnet_iter) {
4548 CURVNET_SET(vnet_iter);
4552 key_flush_spacq(now);
4555 VNET_LIST_RUNLOCK_NOSLEEP();
4557 #ifndef IPSEC_DEBUG2
4558 /* do exchange to tick time !! */
4559 (void)timeout((void *)key_timehandler, (void *)0, hz);
4560 #endif /* IPSEC_DEBUG2 */
4568 key_randomfill(&value, sizeof(value));
4573 key_randomfill(p, l)
4579 static int warn = 1;
4582 n = (size_t)read_random(p, (u_int)l);
4586 bcopy(&v, (u_int8_t *)p + n,
4587 l - n < sizeof(v) ? l - n : sizeof(v));
4591 printf("WARNING: pseudo-random number generator "
4592 "used for IPsec processing\n");
4599 * map SADB_SATYPE_* to IPPROTO_*.
4600 * if satype == SADB_SATYPE then satype is mapped to ~0.
4602 * 0: invalid satype.
4605 key_satype2proto(u_int8_t satype)
4608 case SADB_SATYPE_UNSPEC:
4609 return IPSEC_PROTO_ANY;
4610 case SADB_SATYPE_AH:
4612 case SADB_SATYPE_ESP:
4614 case SADB_X_SATYPE_IPCOMP:
4615 return IPPROTO_IPCOMP;
4616 case SADB_X_SATYPE_TCPSIGNATURE:
4625 * map IPPROTO_* to SADB_SATYPE_*
4627 * 0: invalid protocol type.
4630 key_proto2satype(u_int16_t proto)
4634 return SADB_SATYPE_AH;
4636 return SADB_SATYPE_ESP;
4637 case IPPROTO_IPCOMP:
4638 return SADB_X_SATYPE_IPCOMP;
4640 return SADB_X_SATYPE_TCPSIGNATURE;
4649 * SADB_GETSPI processing is to receive
4650 * <base, (SA2), src address, dst address, (SPI range)>
4651 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4652 * tree with the status of LARVAL, and send
4653 * <base, SA(*), address(SD)>
4656 * IN: mhp: pointer to the pointer to each header.
4657 * OUT: NULL if fail.
4658 * other if success, return pointer to the message to send.
4661 key_getspi(so, m, mhp)
4664 const struct sadb_msghdr *mhp;
4666 struct sadb_address *src0, *dst0;
4667 struct secasindex saidx;
4668 struct secashead *newsah;
4669 struct secasvar *newsav;
4676 IPSEC_ASSERT(so != NULL, ("null socket"));
4677 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4678 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4679 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4681 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4682 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4683 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4685 return key_senderror(so, m, EINVAL);
4687 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4688 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4689 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4691 return key_senderror(so, m, EINVAL);
4693 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4694 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4695 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4697 mode = IPSEC_MODE_ANY;
4701 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4702 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4704 /* map satype to proto */
4705 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4706 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4708 return key_senderror(so, m, EINVAL);
4712 * Make sure the port numbers are zero.
4713 * In case of NAT-T we will update them later if needed.
4715 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4717 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4718 sizeof(struct sockaddr_in))
4719 return key_senderror(so, m, EINVAL);
4720 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4723 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4724 sizeof(struct sockaddr_in6))
4725 return key_senderror(so, m, EINVAL);
4726 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4731 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4733 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4734 sizeof(struct sockaddr_in))
4735 return key_senderror(so, m, EINVAL);
4736 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4739 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4740 sizeof(struct sockaddr_in6))
4741 return key_senderror(so, m, EINVAL);
4742 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4748 /* XXX boundary check against sa_len */
4749 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4753 * Handle NAT-T info if present.
4754 * We made sure the port numbers are zero above, so we do
4755 * not have to worry in case we do not update them.
4757 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL)
4758 ipseclog((LOG_DEBUG, "%s: NAT-T OAi present\n", __func__));
4759 if (mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL)
4760 ipseclog((LOG_DEBUG, "%s: NAT-T OAr present\n", __func__));
4762 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
4763 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
4764 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
4765 struct sadb_x_nat_t_type *type;
4766 struct sadb_x_nat_t_port *sport, *dport;
4768 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
4769 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
4770 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
4771 ipseclog((LOG_DEBUG, "%s: invalid nat-t message "
4772 "passed.\n", __func__));
4773 return key_senderror(so, m, EINVAL);
4776 sport = (struct sadb_x_nat_t_port *)
4777 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
4778 dport = (struct sadb_x_nat_t_port *)
4779 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
4782 KEY_PORTTOSADDR(&saidx.src, sport->sadb_x_nat_t_port_port);
4784 KEY_PORTTOSADDR(&saidx.dst, dport->sadb_x_nat_t_port_port);
4788 /* SPI allocation */
4789 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4792 return key_senderror(so, m, EINVAL);
4794 /* get a SA index */
4795 if ((newsah = key_getsah(&saidx)) == NULL) {
4796 /* create a new SA index */
4797 if ((newsah = key_newsah(&saidx)) == NULL) {
4798 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
4799 return key_senderror(so, m, ENOBUFS);
4805 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4806 if (newsav == NULL) {
4807 /* XXX don't free new SA index allocated in above. */
4808 return key_senderror(so, m, error);
4812 newsav->spi = htonl(spi);
4814 /* delete the entry in acqtree */
4815 if (mhp->msg->sadb_msg_seq != 0) {
4817 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4818 /* reset counter in order to deletion by timehandler. */
4819 acq->created = time_second;
4825 struct mbuf *n, *nn;
4826 struct sadb_sa *m_sa;
4827 struct sadb_msg *newmsg;
4830 /* create new sadb_msg to reply. */
4831 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4832 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4834 MGETHDR(n, M_DONTWAIT, MT_DATA);
4836 MCLGET(n, M_DONTWAIT);
4837 if ((n->m_flags & M_EXT) == 0) {
4843 return key_senderror(so, m, ENOBUFS);
4849 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4850 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4852 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4853 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4854 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4855 m_sa->sadb_sa_spi = htonl(spi);
4856 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4858 IPSEC_ASSERT(off == len,
4859 ("length inconsistency (off %u len %u)", off, len));
4861 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4862 SADB_EXT_ADDRESS_DST);
4865 return key_senderror(so, m, ENOBUFS);
4868 if (n->m_len < sizeof(struct sadb_msg)) {
4869 n = m_pullup(n, sizeof(struct sadb_msg));
4871 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4874 n->m_pkthdr.len = 0;
4875 for (nn = n; nn; nn = nn->m_next)
4876 n->m_pkthdr.len += nn->m_len;
4878 newmsg = mtod(n, struct sadb_msg *);
4879 newmsg->sadb_msg_seq = newsav->seq;
4880 newmsg->sadb_msg_errno = 0;
4881 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4884 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4889 * allocating new SPI
4890 * called by key_getspi().
4896 key_do_getnewspi(spirange, saidx)
4897 struct sadb_spirange *spirange;
4898 struct secasindex *saidx;
4902 int count = V_key_spi_trycnt;
4904 /* set spi range to allocate */
4905 if (spirange != NULL) {
4906 min = spirange->sadb_spirange_min;
4907 max = spirange->sadb_spirange_max;
4909 min = V_key_spi_minval;
4910 max = V_key_spi_maxval;
4912 /* IPCOMP needs 2-byte SPI */
4913 if (saidx->proto == IPPROTO_IPCOMP) {
4920 t = min; min = max; max = t;
4925 if (key_checkspidup(saidx, min) != NULL) {
4926 ipseclog((LOG_DEBUG, "%s: SPI %u exists already.\n",
4931 count--; /* taking one cost. */
4939 /* when requesting to allocate spi ranged */
4941 /* generate pseudo-random SPI value ranged. */
4942 newspi = min + (key_random() % (max - min + 1));
4944 if (key_checkspidup(saidx, newspi) == NULL)
4948 if (count == 0 || newspi == 0) {
4949 ipseclog((LOG_DEBUG, "%s: to allocate spi is failed.\n",
4956 keystat.getspi_count =
4957 (keystat.getspi_count + V_key_spi_trycnt - count) / 2;
4963 * SADB_UPDATE processing
4965 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4966 * key(AE), (identity(SD),) (sensitivity)>
4967 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4969 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4970 * (identity(SD),) (sensitivity)>
4973 * m will always be freed.
4976 key_update(so, m, mhp)
4979 const struct sadb_msghdr *mhp;
4981 struct sadb_sa *sa0;
4982 struct sadb_address *src0, *dst0;
4984 struct sadb_x_nat_t_type *type;
4985 struct sadb_x_nat_t_port *sport, *dport;
4986 struct sadb_address *iaddr, *raddr;
4987 struct sadb_x_nat_t_frag *frag;
4989 struct secasindex saidx;
4990 struct secashead *sah;
4991 struct secasvar *sav;
4997 IPSEC_ASSERT(so != NULL, ("null socket"));
4998 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4999 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5000 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5002 /* map satype to proto */
5003 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5004 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5006 return key_senderror(so, m, EINVAL);
5009 if (mhp->ext[SADB_EXT_SA] == NULL ||
5010 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5011 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5012 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5013 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5014 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5015 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5016 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5017 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5018 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5019 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5020 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5022 return key_senderror(so, m, EINVAL);
5024 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5025 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5026 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5027 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5029 return key_senderror(so, m, EINVAL);
5031 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5032 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5033 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5035 mode = IPSEC_MODE_ANY;
5038 /* XXX boundary checking for other extensions */
5040 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5041 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5042 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5044 /* XXX boundary check against sa_len */
5045 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5048 * Make sure the port numbers are zero.
5049 * In case of NAT-T we will update them later if needed.
5051 KEY_PORTTOSADDR(&saidx.src, 0);
5052 KEY_PORTTOSADDR(&saidx.dst, 0);
5056 * Handle NAT-T info if present.
5058 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
5059 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5060 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5062 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
5063 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5064 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5065 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5067 return key_senderror(so, m, EINVAL);
5070 type = (struct sadb_x_nat_t_type *)
5071 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5072 sport = (struct sadb_x_nat_t_port *)
5073 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5074 dport = (struct sadb_x_nat_t_port *)
5075 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5080 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
5081 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
5082 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
5083 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
5084 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5086 return key_senderror(so, m, EINVAL);
5088 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5089 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5090 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
5092 iaddr = raddr = NULL;
5094 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5095 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5096 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5098 return key_senderror(so, m, EINVAL);
5100 frag = (struct sadb_x_nat_t_frag *)
5101 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5107 /* get a SA header */
5108 if ((sah = key_getsah(&saidx)) == NULL) {
5109 ipseclog((LOG_DEBUG, "%s: no SA index found.\n", __func__));
5110 return key_senderror(so, m, ENOENT);
5113 /* set spidx if there */
5115 error = key_setident(sah, m, mhp);
5117 return key_senderror(so, m, error);
5119 /* find a SA with sequence number. */
5120 #ifdef IPSEC_DOSEQCHECK
5121 if (mhp->msg->sadb_msg_seq != 0
5122 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
5123 ipseclog((LOG_DEBUG, "%s: no larval SA with sequence %u "
5124 "exists.\n", __func__, mhp->msg->sadb_msg_seq));
5125 return key_senderror(so, m, ENOENT);
5129 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5132 ipseclog((LOG_DEBUG, "%s: no such a SA found (spi:%u)\n",
5133 __func__, (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5134 return key_senderror(so, m, EINVAL);
5138 /* validity check */
5139 if (sav->sah->saidx.proto != proto) {
5140 ipseclog((LOG_DEBUG, "%s: protocol mismatched "
5141 "(DB=%u param=%u)\n", __func__,
5142 sav->sah->saidx.proto, proto));
5143 return key_senderror(so, m, EINVAL);
5145 #ifdef IPSEC_DOSEQCHECK
5146 if (sav->spi != sa0->sadb_sa_spi) {
5147 ipseclog((LOG_DEBUG, "%s: SPI mismatched (DB:%u param:%u)\n",
5149 (u_int32_t)ntohl(sav->spi),
5150 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5151 return key_senderror(so, m, EINVAL);
5154 if (sav->pid != mhp->msg->sadb_msg_pid) {
5155 ipseclog((LOG_DEBUG, "%s: pid mismatched (DB:%u param:%u)\n",
5156 __func__, sav->pid, mhp->msg->sadb_msg_pid));
5157 return key_senderror(so, m, EINVAL);
5160 /* copy sav values */
5161 error = key_setsaval(sav, m, mhp);
5164 return key_senderror(so, m, error);
5169 * Handle more NAT-T info if present,
5170 * now that we have a sav to fill.
5173 sav->natt_type = type->sadb_x_nat_t_type_type;
5176 KEY_PORTTOSADDR(&sav->sah->saidx.src,
5177 sport->sadb_x_nat_t_port_port);
5179 KEY_PORTTOSADDR(&sav->sah->saidx.dst,
5180 dport->sadb_x_nat_t_port_port);
5184 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5185 * We should actually check for a minimum MTU here, if we
5186 * want to support it in ip_output.
5189 sav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5193 /* check SA values to be mature. */
5194 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
5196 return key_senderror(so, m, 0);
5202 /* set msg buf from mhp */
5203 n = key_getmsgbuf_x1(m, mhp);
5205 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5206 return key_senderror(so, m, ENOBUFS);
5210 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5215 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
5216 * only called by key_update().
5219 * others : found, pointer to a SA.
5221 #ifdef IPSEC_DOSEQCHECK
5222 static struct secasvar *
5223 key_getsavbyseq(sah, seq)
5224 struct secashead *sah;
5227 struct secasvar *sav;
5230 state = SADB_SASTATE_LARVAL;
5232 /* search SAD with sequence number ? */
5233 LIST_FOREACH(sav, &sah->savtree[state], chain) {
5235 KEY_CHKSASTATE(state, sav->state, __func__);
5237 if (sav->seq == seq) {
5239 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
5240 printf("DP %s cause refcnt++:%d SA:%p\n",
5241 __func__, sav->refcnt, sav));
5251 * SADB_ADD processing
5252 * add an entry to SA database, when received
5253 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5254 * key(AE), (identity(SD),) (sensitivity)>
5257 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5258 * (identity(SD),) (sensitivity)>
5261 * IGNORE identity and sensitivity messages.
5263 * m will always be freed.
5269 const struct sadb_msghdr *mhp;
5271 struct sadb_sa *sa0;
5272 struct sadb_address *src0, *dst0;
5274 struct sadb_x_nat_t_type *type;
5275 struct sadb_address *iaddr, *raddr;
5276 struct sadb_x_nat_t_frag *frag;
5278 struct secasindex saidx;
5279 struct secashead *newsah;
5280 struct secasvar *newsav;
5286 IPSEC_ASSERT(so != NULL, ("null socket"));
5287 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5288 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5289 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5291 /* map satype to proto */
5292 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5293 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5295 return key_senderror(so, m, EINVAL);
5298 if (mhp->ext[SADB_EXT_SA] == NULL ||
5299 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5300 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5301 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5302 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5303 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5304 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5305 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5306 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5307 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5308 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5309 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5311 return key_senderror(so, m, EINVAL);
5313 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5314 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5315 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5317 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5319 return key_senderror(so, m, EINVAL);
5321 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5322 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5323 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5325 mode = IPSEC_MODE_ANY;
5329 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5330 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5331 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5333 /* XXX boundary check against sa_len */
5334 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5337 * Make sure the port numbers are zero.
5338 * In case of NAT-T we will update them later if needed.
5340 KEY_PORTTOSADDR(&saidx.src, 0);
5341 KEY_PORTTOSADDR(&saidx.dst, 0);
5345 * Handle NAT-T info if present.
5347 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
5348 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5349 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5350 struct sadb_x_nat_t_port *sport, *dport;
5352 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
5353 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5354 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5355 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5357 return key_senderror(so, m, EINVAL);
5360 type = (struct sadb_x_nat_t_type *)
5361 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5362 sport = (struct sadb_x_nat_t_port *)
5363 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5364 dport = (struct sadb_x_nat_t_port *)
5365 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5368 KEY_PORTTOSADDR(&saidx.src,
5369 sport->sadb_x_nat_t_port_port);
5371 KEY_PORTTOSADDR(&saidx.dst,
5372 dport->sadb_x_nat_t_port_port);
5376 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
5377 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
5378 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
5379 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
5380 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5382 return key_senderror(so, m, EINVAL);
5384 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5385 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5386 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
5388 iaddr = raddr = NULL;
5390 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5391 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5392 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5394 return key_senderror(so, m, EINVAL);
5396 frag = (struct sadb_x_nat_t_frag *)
5397 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5403 /* get a SA header */
5404 if ((newsah = key_getsah(&saidx)) == NULL) {
5405 /* create a new SA header */
5406 if ((newsah = key_newsah(&saidx)) == NULL) {
5407 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
5408 return key_senderror(so, m, ENOBUFS);
5412 /* set spidx if there */
5414 error = key_setident(newsah, m, mhp);
5416 return key_senderror(so, m, error);
5419 /* create new SA entry. */
5420 /* We can create new SA only if SPI is differenct. */
5422 newsav = key_getsavbyspi(newsah, sa0->sadb_sa_spi);
5424 if (newsav != NULL) {
5425 ipseclog((LOG_DEBUG, "%s: SA already exists.\n", __func__));
5426 return key_senderror(so, m, EEXIST);
5428 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
5429 if (newsav == NULL) {
5430 return key_senderror(so, m, error);
5435 * Handle more NAT-T info if present,
5436 * now that we have a sav to fill.
5439 newsav->natt_type = type->sadb_x_nat_t_type_type;
5443 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5444 * We should actually check for a minimum MTU here, if we
5445 * want to support it in ip_output.
5448 newsav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5452 /* check SA values to be mature. */
5453 if ((error = key_mature(newsav)) != 0) {
5454 KEY_FREESAV(&newsav);
5455 return key_senderror(so, m, error);
5459 * don't call key_freesav() here, as we would like to keep the SA
5460 * in the database on success.
5466 /* set msg buf from mhp */
5467 n = key_getmsgbuf_x1(m, mhp);
5469 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5470 return key_senderror(so, m, ENOBUFS);
5474 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5480 key_setident(sah, m, mhp)
5481 struct secashead *sah;
5483 const struct sadb_msghdr *mhp;
5485 const struct sadb_ident *idsrc, *iddst;
5486 int idsrclen, iddstlen;
5488 IPSEC_ASSERT(sah != NULL, ("null secashead"));
5489 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5490 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5491 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5493 /* don't make buffer if not there */
5494 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
5495 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5501 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
5502 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5503 ipseclog((LOG_DEBUG, "%s: invalid identity.\n", __func__));
5507 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
5508 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
5509 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
5510 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
5512 /* validity check */
5513 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
5514 ipseclog((LOG_DEBUG, "%s: ident type mismatch.\n", __func__));
5518 switch (idsrc->sadb_ident_type) {
5519 case SADB_IDENTTYPE_PREFIX:
5520 case SADB_IDENTTYPE_FQDN:
5521 case SADB_IDENTTYPE_USERFQDN:
5523 /* XXX do nothing */
5529 /* make structure */
5530 sah->idents = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5531 if (sah->idents == NULL) {
5532 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5535 sah->identd = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5536 if (sah->identd == NULL) {
5537 free(sah->idents, M_IPSEC_MISC);
5539 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5542 sah->idents->type = idsrc->sadb_ident_type;
5543 sah->idents->id = idsrc->sadb_ident_id;
5545 sah->identd->type = iddst->sadb_ident_type;
5546 sah->identd->id = iddst->sadb_ident_id;
5552 * m will not be freed on return.
5553 * it is caller's responsibility to free the result.
5555 static struct mbuf *
5556 key_getmsgbuf_x1(m, mhp)
5558 const struct sadb_msghdr *mhp;
5562 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5563 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5564 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5566 /* create new sadb_msg to reply. */
5567 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5568 SADB_EXT_SA, SADB_X_EXT_SA2,
5569 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5570 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5571 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5575 if (n->m_len < sizeof(struct sadb_msg)) {
5576 n = m_pullup(n, sizeof(struct sadb_msg));
5580 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5581 mtod(n, struct sadb_msg *)->sadb_msg_len =
5582 PFKEY_UNIT64(n->m_pkthdr.len);
5587 static int key_delete_all __P((struct socket *, struct mbuf *,
5588 const struct sadb_msghdr *, u_int16_t));
5591 * SADB_DELETE processing
5593 * <base, SA(*), address(SD)>
5594 * from the ikmpd, and set SADB_SASTATE_DEAD,
5596 * <base, SA(*), address(SD)>
5599 * m will always be freed.
5602 key_delete(so, m, mhp)
5605 const struct sadb_msghdr *mhp;
5607 struct sadb_sa *sa0;
5608 struct sadb_address *src0, *dst0;
5609 struct secasindex saidx;
5610 struct secashead *sah;
5611 struct secasvar *sav = NULL;
5614 IPSEC_ASSERT(so != NULL, ("null socket"));
5615 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5616 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5617 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5619 /* map satype to proto */
5620 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5621 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5623 return key_senderror(so, m, EINVAL);
5626 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5627 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5628 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5630 return key_senderror(so, m, EINVAL);
5633 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5634 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5635 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5637 return key_senderror(so, m, EINVAL);
5640 if (mhp->ext[SADB_EXT_SA] == NULL) {
5642 * Caller wants us to delete all non-LARVAL SAs
5643 * that match the src/dst. This is used during
5644 * IKE INITIAL-CONTACT.
5646 ipseclog((LOG_DEBUG, "%s: doing delete all.\n", __func__));
5647 return key_delete_all(so, m, mhp, proto);
5648 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5649 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5651 return key_senderror(so, m, EINVAL);
5654 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5655 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5656 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5658 /* XXX boundary check against sa_len */
5659 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5662 * Make sure the port numbers are zero.
5663 * In case of NAT-T we will update them later if needed.
5665 KEY_PORTTOSADDR(&saidx.src, 0);
5666 KEY_PORTTOSADDR(&saidx.dst, 0);
5670 * Handle NAT-T info if present.
5672 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5673 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5674 struct sadb_x_nat_t_port *sport, *dport;
5676 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5677 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5678 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5680 return key_senderror(so, m, EINVAL);
5683 sport = (struct sadb_x_nat_t_port *)
5684 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5685 dport = (struct sadb_x_nat_t_port *)
5686 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5689 KEY_PORTTOSADDR(&saidx.src,
5690 sport->sadb_x_nat_t_port_port);
5692 KEY_PORTTOSADDR(&saidx.dst,
5693 dport->sadb_x_nat_t_port_port);
5697 /* get a SA header */
5699 LIST_FOREACH(sah, &V_sahtree, chain) {
5700 if (sah->state == SADB_SASTATE_DEAD)
5702 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5705 /* get a SA with SPI. */
5706 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5712 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5713 return key_senderror(so, m, ENOENT);
5716 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5722 struct sadb_msg *newmsg;
5724 /* create new sadb_msg to reply. */
5725 /* XXX-BZ NAT-T extensions? */
5726 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5727 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5729 return key_senderror(so, m, ENOBUFS);
5731 if (n->m_len < sizeof(struct sadb_msg)) {
5732 n = m_pullup(n, sizeof(struct sadb_msg));
5734 return key_senderror(so, m, ENOBUFS);
5736 newmsg = mtod(n, struct sadb_msg *);
5737 newmsg->sadb_msg_errno = 0;
5738 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5741 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5746 * delete all SAs for src/dst. Called from key_delete().
5749 key_delete_all(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp,
5752 struct sadb_address *src0, *dst0;
5753 struct secasindex saidx;
5754 struct secashead *sah;
5755 struct secasvar *sav, *nextsav;
5756 u_int stateidx, state;
5758 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5759 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5761 /* XXX boundary check against sa_len */
5762 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5765 * Make sure the port numbers are zero.
5766 * In case of NAT-T we will update them later if needed.
5768 KEY_PORTTOSADDR(&saidx.src, 0);
5769 KEY_PORTTOSADDR(&saidx.dst, 0);
5773 * Handle NAT-T info if present.
5776 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5777 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5778 struct sadb_x_nat_t_port *sport, *dport;
5780 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5781 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5782 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5784 return key_senderror(so, m, EINVAL);
5787 sport = (struct sadb_x_nat_t_port *)
5788 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5789 dport = (struct sadb_x_nat_t_port *)
5790 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5793 KEY_PORTTOSADDR(&saidx.src,
5794 sport->sadb_x_nat_t_port_port);
5796 KEY_PORTTOSADDR(&saidx.dst,
5797 dport->sadb_x_nat_t_port_port);
5802 LIST_FOREACH(sah, &V_sahtree, chain) {
5803 if (sah->state == SADB_SASTATE_DEAD)
5805 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5808 /* Delete all non-LARVAL SAs. */
5810 stateidx < _ARRAYLEN(saorder_state_alive);
5812 state = saorder_state_alive[stateidx];
5813 if (state == SADB_SASTATE_LARVAL)
5815 for (sav = LIST_FIRST(&sah->savtree[state]);
5816 sav != NULL; sav = nextsav) {
5817 nextsav = LIST_NEXT(sav, chain);
5819 if (sav->state != state) {
5820 ipseclog((LOG_DEBUG, "%s: invalid "
5821 "sav->state (queue %d SA %d)\n",
5822 __func__, state, sav->state));
5826 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5834 struct sadb_msg *newmsg;
5836 /* create new sadb_msg to reply. */
5837 /* XXX-BZ NAT-T extensions? */
5838 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5839 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5841 return key_senderror(so, m, ENOBUFS);
5843 if (n->m_len < sizeof(struct sadb_msg)) {
5844 n = m_pullup(n, sizeof(struct sadb_msg));
5846 return key_senderror(so, m, ENOBUFS);
5848 newmsg = mtod(n, struct sadb_msg *);
5849 newmsg->sadb_msg_errno = 0;
5850 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5853 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5858 * SADB_GET processing
5860 * <base, SA(*), address(SD)>
5861 * from the ikmpd, and get a SP and a SA to respond,
5863 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5864 * (identity(SD),) (sensitivity)>
5867 * m will always be freed.
5873 const struct sadb_msghdr *mhp;
5875 struct sadb_sa *sa0;
5876 struct sadb_address *src0, *dst0;
5877 struct secasindex saidx;
5878 struct secashead *sah;
5879 struct secasvar *sav = NULL;
5882 IPSEC_ASSERT(so != NULL, ("null socket"));
5883 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5884 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5885 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5887 /* map satype to proto */
5888 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5889 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5891 return key_senderror(so, m, EINVAL);
5894 if (mhp->ext[SADB_EXT_SA] == NULL ||
5895 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5896 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5897 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5899 return key_senderror(so, m, EINVAL);
5901 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5902 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5903 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5904 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5906 return key_senderror(so, m, EINVAL);
5909 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5910 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5911 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5913 /* XXX boundary check against sa_len */
5914 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5917 * Make sure the port numbers are zero.
5918 * In case of NAT-T we will update them later if needed.
5920 KEY_PORTTOSADDR(&saidx.src, 0);
5921 KEY_PORTTOSADDR(&saidx.dst, 0);
5925 * Handle NAT-T info if present.
5928 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5929 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5930 struct sadb_x_nat_t_port *sport, *dport;
5932 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5933 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5934 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5936 return key_senderror(so, m, EINVAL);
5939 sport = (struct sadb_x_nat_t_port *)
5940 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5941 dport = (struct sadb_x_nat_t_port *)
5942 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5945 KEY_PORTTOSADDR(&saidx.src,
5946 sport->sadb_x_nat_t_port_port);
5948 KEY_PORTTOSADDR(&saidx.dst,
5949 dport->sadb_x_nat_t_port_port);
5953 /* get a SA header */
5955 LIST_FOREACH(sah, &V_sahtree, chain) {
5956 if (sah->state == SADB_SASTATE_DEAD)
5958 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5961 /* get a SA with SPI. */
5962 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5968 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5969 return key_senderror(so, m, ENOENT);
5976 /* map proto to satype */
5977 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5978 ipseclog((LOG_DEBUG, "%s: there was invalid proto in SAD.\n",
5980 return key_senderror(so, m, EINVAL);
5983 /* create new sadb_msg to reply. */
5984 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5985 mhp->msg->sadb_msg_pid);
5987 return key_senderror(so, m, ENOBUFS);
5990 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5994 /* XXX make it sysctl-configurable? */
5996 key_getcomb_setlifetime(comb)
5997 struct sadb_comb *comb;
6000 comb->sadb_comb_soft_allocations = 1;
6001 comb->sadb_comb_hard_allocations = 1;
6002 comb->sadb_comb_soft_bytes = 0;
6003 comb->sadb_comb_hard_bytes = 0;
6004 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
6005 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
6006 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
6007 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
6011 * XXX reorder combinations by preference
6012 * XXX no idea if the user wants ESP authentication or not
6014 static struct mbuf *
6017 struct sadb_comb *comb;
6018 struct enc_xform *algo;
6019 struct mbuf *result = NULL, *m, *n;
6023 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6026 for (i = 1; i <= SADB_EALG_MAX; i++) {
6027 algo = esp_algorithm_lookup(i);
6031 /* discard algorithms with key size smaller than system min */
6032 if (_BITS(algo->maxkey) < V_ipsec_esp_keymin)
6034 if (_BITS(algo->minkey) < V_ipsec_esp_keymin)
6035 encmin = V_ipsec_esp_keymin;
6037 encmin = _BITS(algo->minkey);
6039 if (V_ipsec_esp_auth)
6040 m = key_getcomb_ah();
6042 IPSEC_ASSERT(l <= MLEN,
6043 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6044 MGET(m, M_DONTWAIT, MT_DATA);
6049 bzero(mtod(m, caddr_t), m->m_len);
6056 for (n = m; n; n = n->m_next)
6058 IPSEC_ASSERT((totlen % l) == 0, ("totlen=%u, l=%u", totlen, l));
6060 for (off = 0; off < totlen; off += l) {
6061 n = m_pulldown(m, off, l, &o);
6063 /* m is already freed */
6066 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
6067 bzero(comb, sizeof(*comb));
6068 key_getcomb_setlifetime(comb);
6069 comb->sadb_comb_encrypt = i;
6070 comb->sadb_comb_encrypt_minbits = encmin;
6071 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
6090 const struct auth_hash *ah,
6096 *min = *max = ah->keysize;
6097 if (ah->keysize == 0) {
6099 * Transform takes arbitrary key size but algorithm
6100 * key size is restricted. Enforce this here.
6103 case SADB_X_AALG_MD5: *min = *max = 16; break;
6104 case SADB_X_AALG_SHA: *min = *max = 20; break;
6105 case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
6106 case SADB_X_AALG_SHA2_256: *min = *max = 32; break;
6107 case SADB_X_AALG_SHA2_384: *min = *max = 48; break;
6108 case SADB_X_AALG_SHA2_512: *min = *max = 64; break;
6110 DPRINTF(("%s: unknown AH algorithm %u\n",
6118 * XXX reorder combinations by preference
6120 static struct mbuf *
6123 struct sadb_comb *comb;
6124 struct auth_hash *algo;
6126 u_int16_t minkeysize, maxkeysize;
6128 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6131 for (i = 1; i <= SADB_AALG_MAX; i++) {
6133 /* we prefer HMAC algorithms, not old algorithms */
6134 if (i != SADB_AALG_SHA1HMAC &&
6135 i != SADB_AALG_MD5HMAC &&
6136 i != SADB_X_AALG_SHA2_256 &&
6137 i != SADB_X_AALG_SHA2_384 &&
6138 i != SADB_X_AALG_SHA2_512)
6141 algo = ah_algorithm_lookup(i);
6144 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
6145 /* discard algorithms with key size smaller than system min */
6146 if (_BITS(minkeysize) < V_ipsec_ah_keymin)
6150 IPSEC_ASSERT(l <= MLEN,
6151 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6152 MGET(m, M_DONTWAIT, MT_DATA);
6159 M_PREPEND(m, l, M_DONTWAIT);
6163 comb = mtod(m, struct sadb_comb *);
6164 bzero(comb, sizeof(*comb));
6165 key_getcomb_setlifetime(comb);
6166 comb->sadb_comb_auth = i;
6167 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
6168 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
6175 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
6176 * XXX reorder combinations by preference
6178 static struct mbuf *
6179 key_getcomb_ipcomp()
6181 struct sadb_comb *comb;
6182 struct comp_algo *algo;
6185 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6188 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
6189 algo = ipcomp_algorithm_lookup(i);
6194 IPSEC_ASSERT(l <= MLEN,
6195 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6196 MGET(m, M_DONTWAIT, MT_DATA);
6203 M_PREPEND(m, l, M_DONTWAIT);
6207 comb = mtod(m, struct sadb_comb *);
6208 bzero(comb, sizeof(*comb));
6209 key_getcomb_setlifetime(comb);
6210 comb->sadb_comb_encrypt = i;
6211 /* what should we set into sadb_comb_*_{min,max}bits? */
6218 * XXX no way to pass mode (transport/tunnel) to userland
6219 * XXX replay checking?
6220 * XXX sysctl interface to ipsec_{ah,esp}_keymin
6222 static struct mbuf *
6224 const struct secasindex *saidx;
6226 struct sadb_prop *prop;
6228 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
6231 switch (saidx->proto) {
6233 m = key_getcomb_esp();
6236 m = key_getcomb_ah();
6238 case IPPROTO_IPCOMP:
6239 m = key_getcomb_ipcomp();
6247 M_PREPEND(m, l, M_DONTWAIT);
6252 for (n = m; n; n = n->m_next)
6255 prop = mtod(m, struct sadb_prop *);
6256 bzero(prop, sizeof(*prop));
6257 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
6258 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
6259 prop->sadb_prop_replay = 32; /* XXX */
6265 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
6267 * <base, SA, address(SD), (address(P)), x_policy,
6268 * (identity(SD),) (sensitivity,) proposal>
6269 * to KMD, and expect to receive
6270 * <base> with SADB_ACQUIRE if error occured,
6272 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
6273 * from KMD by PF_KEY.
6275 * XXX x_policy is outside of RFC2367 (KAME extension).
6276 * XXX sensitivity is not supported.
6277 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
6278 * see comment for key_getcomb_ipcomp().
6282 * others: error number
6285 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
6287 struct mbuf *result = NULL, *m;
6288 struct secacq *newacq;
6293 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
6294 satype = key_proto2satype(saidx->proto);
6295 IPSEC_ASSERT(satype != 0, ("null satype, protocol %u", saidx->proto));
6298 * We never do anything about acquirng SA. There is anather
6299 * solution that kernel blocks to send SADB_ACQUIRE message until
6300 * getting something message from IKEd. In later case, to be
6301 * managed with ACQUIRING list.
6303 /* Get an entry to check whether sending message or not. */
6304 if ((newacq = key_getacq(saidx)) != NULL) {
6305 if (V_key_blockacq_count < newacq->count) {
6306 /* reset counter and do send message. */
6309 /* increment counter and do nothing. */
6314 /* make new entry for blocking to send SADB_ACQUIRE. */
6315 if ((newacq = key_newacq(saidx)) == NULL)
6321 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
6329 * No SADB_X_EXT_NAT_T_* here: we do not know
6330 * anything related to NAT-T at this time.
6333 /* set sadb_address for saidx's. */
6334 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6335 &saidx->src.sa, FULLMASK, IPSEC_ULPROTO_ANY);
6342 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6343 &saidx->dst.sa, FULLMASK, IPSEC_ULPROTO_ANY);
6350 /* XXX proxy address (optional) */
6352 /* set sadb_x_policy */
6354 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
6362 /* XXX identity (optional) */
6364 if (idexttype && fqdn) {
6365 /* create identity extension (FQDN) */
6366 struct sadb_ident *id;
6369 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
6370 id = (struct sadb_ident *)p;
6371 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6372 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6373 id->sadb_ident_exttype = idexttype;
6374 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
6375 bcopy(fqdn, id + 1, fqdnlen);
6376 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
6380 /* create identity extension (USERFQDN) */
6381 struct sadb_ident *id;
6385 /* +1 for terminating-NUL */
6386 userfqdnlen = strlen(userfqdn) + 1;
6389 id = (struct sadb_ident *)p;
6390 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6391 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6392 id->sadb_ident_exttype = idexttype;
6393 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
6394 /* XXX is it correct? */
6395 if (curproc && curproc->p_cred)
6396 id->sadb_ident_id = curproc->p_cred->p_ruid;
6397 if (userfqdn && userfqdnlen)
6398 bcopy(userfqdn, id + 1, userfqdnlen);
6399 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
6403 /* XXX sensitivity (optional) */
6405 /* create proposal/combination extension */
6406 m = key_getprop(saidx);
6409 * spec conformant: always attach proposal/combination extension,
6410 * the problem is that we have no way to attach it for ipcomp,
6411 * due to the way sadb_comb is declared in RFC2367.
6420 * outside of spec; make proposal/combination extension optional.
6426 if ((result->m_flags & M_PKTHDR) == 0) {
6431 if (result->m_len < sizeof(struct sadb_msg)) {
6432 result = m_pullup(result, sizeof(struct sadb_msg));
6433 if (result == NULL) {
6439 result->m_pkthdr.len = 0;
6440 for (m = result; m; m = m->m_next)
6441 result->m_pkthdr.len += m->m_len;
6443 mtod(result, struct sadb_msg *)->sadb_msg_len =
6444 PFKEY_UNIT64(result->m_pkthdr.len);
6446 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6454 static struct secacq *
6455 key_newacq(const struct secasindex *saidx)
6457 struct secacq *newacq;
6460 newacq = malloc(sizeof(struct secacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6461 if (newacq == NULL) {
6462 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6467 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
6468 newacq->seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq);
6469 newacq->created = time_second;
6472 /* add to acqtree */
6474 LIST_INSERT_HEAD(&V_acqtree, newacq, chain);
6480 static struct secacq *
6481 key_getacq(const struct secasindex *saidx)
6486 LIST_FOREACH(acq, &V_acqtree, chain) {
6487 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
6495 static struct secacq *
6496 key_getacqbyseq(seq)
6502 LIST_FOREACH(acq, &V_acqtree, chain) {
6503 if (acq->seq == seq)
6511 static struct secspacq *
6513 struct secpolicyindex *spidx;
6515 struct secspacq *acq;
6518 acq = malloc(sizeof(struct secspacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6520 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6525 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
6526 acq->created = time_second;
6529 /* add to spacqtree */
6531 LIST_INSERT_HEAD(&V_spacqtree, acq, chain);
6537 static struct secspacq *
6539 struct secpolicyindex *spidx;
6541 struct secspacq *acq;
6544 LIST_FOREACH(acq, &V_spacqtree, chain) {
6545 if (key_cmpspidx_exactly(spidx, &acq->spidx)) {
6546 /* NB: return holding spacq_lock */
6556 * SADB_ACQUIRE processing,
6557 * in first situation, is receiving
6559 * from the ikmpd, and clear sequence of its secasvar entry.
6561 * In second situation, is receiving
6562 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6563 * from a user land process, and return
6564 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6567 * m will always be freed.
6570 key_acquire2(so, m, mhp)
6573 const struct sadb_msghdr *mhp;
6575 const struct sadb_address *src0, *dst0;
6576 struct secasindex saidx;
6577 struct secashead *sah;
6581 IPSEC_ASSERT(so != NULL, ("null socket"));
6582 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6583 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6584 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6587 * Error message from KMd.
6588 * We assume that if error was occured in IKEd, the length of PFKEY
6589 * message is equal to the size of sadb_msg structure.
6590 * We do not raise error even if error occured in this function.
6592 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
6595 /* check sequence number */
6596 if (mhp->msg->sadb_msg_seq == 0) {
6597 ipseclog((LOG_DEBUG, "%s: must specify sequence "
6598 "number.\n", __func__));
6603 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
6605 * the specified larval SA is already gone, or we got
6606 * a bogus sequence number. we can silently ignore it.
6612 /* reset acq counter in order to deletion by timehander. */
6613 acq->created = time_second;
6620 * This message is from user land.
6623 /* map satype to proto */
6624 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6625 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6627 return key_senderror(so, m, EINVAL);
6630 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
6631 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
6632 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
6634 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6636 return key_senderror(so, m, EINVAL);
6638 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
6639 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
6640 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
6642 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6644 return key_senderror(so, m, EINVAL);
6647 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
6648 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
6650 /* XXX boundary check against sa_len */
6651 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
6654 * Make sure the port numbers are zero.
6655 * In case of NAT-T we will update them later if needed.
6657 KEY_PORTTOSADDR(&saidx.src, 0);
6658 KEY_PORTTOSADDR(&saidx.dst, 0);
6662 * Handle NAT-T info if present.
6665 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
6666 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
6667 struct sadb_x_nat_t_port *sport, *dport;
6669 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
6670 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
6671 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
6673 return key_senderror(so, m, EINVAL);
6676 sport = (struct sadb_x_nat_t_port *)
6677 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
6678 dport = (struct sadb_x_nat_t_port *)
6679 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
6682 KEY_PORTTOSADDR(&saidx.src,
6683 sport->sadb_x_nat_t_port_port);
6685 KEY_PORTTOSADDR(&saidx.dst,
6686 dport->sadb_x_nat_t_port_port);
6690 /* get a SA index */
6692 LIST_FOREACH(sah, &V_sahtree, chain) {
6693 if (sah->state == SADB_SASTATE_DEAD)
6695 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
6700 ipseclog((LOG_DEBUG, "%s: a SA exists already.\n", __func__));
6701 return key_senderror(so, m, EEXIST);
6704 error = key_acquire(&saidx, NULL);
6706 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
6707 __func__, mhp->msg->sadb_msg_errno));
6708 return key_senderror(so, m, error);
6711 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6715 * SADB_REGISTER processing.
6716 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6719 * from the ikmpd, and register a socket to send PF_KEY messages,
6723 * If socket is detached, must free from regnode.
6725 * m will always be freed.
6728 key_register(so, m, mhp)
6731 const struct sadb_msghdr *mhp;
6733 struct secreg *reg, *newreg = 0;
6735 IPSEC_ASSERT(so != NULL, ("null socket"));
6736 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6737 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6738 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6740 /* check for invalid register message */
6741 if (mhp->msg->sadb_msg_satype >= sizeof(V_regtree)/sizeof(V_regtree[0]))
6742 return key_senderror(so, m, EINVAL);
6744 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6745 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6748 /* check whether existing or not */
6750 LIST_FOREACH(reg, &V_regtree[mhp->msg->sadb_msg_satype], chain) {
6751 if (reg->so == so) {
6753 ipseclog((LOG_DEBUG, "%s: socket exists already.\n",
6755 return key_senderror(so, m, EEXIST);
6759 /* create regnode */
6760 newreg = malloc(sizeof(struct secreg), M_IPSEC_SAR, M_NOWAIT|M_ZERO);
6761 if (newreg == NULL) {
6763 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6764 return key_senderror(so, m, ENOBUFS);
6768 ((struct keycb *)sotorawcb(so))->kp_registered++;
6770 /* add regnode to regtree. */
6771 LIST_INSERT_HEAD(&V_regtree[mhp->msg->sadb_msg_satype], newreg, chain);
6777 struct sadb_msg *newmsg;
6778 struct sadb_supported *sup;
6779 u_int len, alen, elen;
6782 struct sadb_alg *alg;
6784 /* create new sadb_msg to reply. */
6786 for (i = 1; i <= SADB_AALG_MAX; i++) {
6787 if (ah_algorithm_lookup(i))
6788 alen += sizeof(struct sadb_alg);
6791 alen += sizeof(struct sadb_supported);
6793 for (i = 1; i <= SADB_EALG_MAX; i++) {
6794 if (esp_algorithm_lookup(i))
6795 elen += sizeof(struct sadb_alg);
6798 elen += sizeof(struct sadb_supported);
6800 len = sizeof(struct sadb_msg) + alen + elen;
6803 return key_senderror(so, m, ENOBUFS);
6805 MGETHDR(n, M_DONTWAIT, MT_DATA);
6807 MCLGET(n, M_DONTWAIT);
6808 if ((n->m_flags & M_EXT) == 0) {
6814 return key_senderror(so, m, ENOBUFS);
6816 n->m_pkthdr.len = n->m_len = len;
6820 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6821 newmsg = mtod(n, struct sadb_msg *);
6822 newmsg->sadb_msg_errno = 0;
6823 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6824 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6826 /* for authentication algorithm */
6828 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6829 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6830 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6831 off += PFKEY_ALIGN8(sizeof(*sup));
6833 for (i = 1; i <= SADB_AALG_MAX; i++) {
6834 struct auth_hash *aalgo;
6835 u_int16_t minkeysize, maxkeysize;
6837 aalgo = ah_algorithm_lookup(i);
6840 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6841 alg->sadb_alg_id = i;
6842 alg->sadb_alg_ivlen = 0;
6843 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
6844 alg->sadb_alg_minbits = _BITS(minkeysize);
6845 alg->sadb_alg_maxbits = _BITS(maxkeysize);
6846 off += PFKEY_ALIGN8(sizeof(*alg));
6850 /* for encryption algorithm */
6852 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6853 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6854 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6855 off += PFKEY_ALIGN8(sizeof(*sup));
6857 for (i = 1; i <= SADB_EALG_MAX; i++) {
6858 struct enc_xform *ealgo;
6860 ealgo = esp_algorithm_lookup(i);
6863 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6864 alg->sadb_alg_id = i;
6865 alg->sadb_alg_ivlen = ealgo->blocksize;
6866 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
6867 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
6868 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6872 IPSEC_ASSERT(off == len,
6873 ("length assumption failed (off %u len %u)", off, len));
6876 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6881 * free secreg entry registered.
6882 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6885 key_freereg(struct socket *so)
6890 IPSEC_ASSERT(so != NULL, ("NULL so"));
6893 * check whether existing or not.
6894 * check all type of SA, because there is a potential that
6895 * one socket is registered to multiple type of SA.
6898 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6899 LIST_FOREACH(reg, &V_regtree[i], chain) {
6900 if (reg->so == so && __LIST_CHAINED(reg)) {
6901 LIST_REMOVE(reg, chain);
6902 free(reg, M_IPSEC_SAR);
6911 * SADB_EXPIRE processing
6913 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6915 * NOTE: We send only soft lifetime extension.
6918 * others : error number
6921 key_expire(struct secasvar *sav)
6925 struct mbuf *result = NULL, *m;
6928 struct sadb_lifetime *lt;
6930 /* XXX: Why do we lock ? */
6931 s = splnet(); /*called from softclock()*/
6933 IPSEC_ASSERT (sav != NULL, ("null sav"));
6934 IPSEC_ASSERT (sav->sah != NULL, ("null sa header"));
6936 /* set msg header */
6937 satype = key_proto2satype(sav->sah->saidx.proto);
6938 IPSEC_ASSERT(satype != 0, ("invalid proto, satype %u", satype));
6939 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6946 /* create SA extension */
6947 m = key_setsadbsa(sav);
6954 /* create SA extension */
6955 m = key_setsadbxsa2(sav->sah->saidx.mode,
6956 sav->replay ? sav->replay->count : 0,
6957 sav->sah->saidx.reqid);
6964 /* create lifetime extension (current and soft) */
6965 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6966 m = key_alloc_mbuf(len);
6967 if (!m || m->m_next) { /*XXX*/
6973 bzero(mtod(m, caddr_t), len);
6974 lt = mtod(m, struct sadb_lifetime *);
6975 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6976 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6977 lt->sadb_lifetime_allocations = sav->lft_c->allocations;
6978 lt->sadb_lifetime_bytes = sav->lft_c->bytes;
6979 lt->sadb_lifetime_addtime = sav->lft_c->addtime;
6980 lt->sadb_lifetime_usetime = sav->lft_c->usetime;
6981 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6982 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6983 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
6984 lt->sadb_lifetime_allocations = sav->lft_s->allocations;
6985 lt->sadb_lifetime_bytes = sav->lft_s->bytes;
6986 lt->sadb_lifetime_addtime = sav->lft_s->addtime;
6987 lt->sadb_lifetime_usetime = sav->lft_s->usetime;
6990 /* set sadb_address for source */
6991 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6992 &sav->sah->saidx.src.sa,
6993 FULLMASK, IPSEC_ULPROTO_ANY);
7000 /* set sadb_address for destination */
7001 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
7002 &sav->sah->saidx.dst.sa,
7003 FULLMASK, IPSEC_ULPROTO_ANY);
7011 * XXX-BZ Handle NAT-T extensions here.
7014 if ((result->m_flags & M_PKTHDR) == 0) {
7019 if (result->m_len < sizeof(struct sadb_msg)) {
7020 result = m_pullup(result, sizeof(struct sadb_msg));
7021 if (result == NULL) {
7027 result->m_pkthdr.len = 0;
7028 for (m = result; m; m = m->m_next)
7029 result->m_pkthdr.len += m->m_len;
7031 mtod(result, struct sadb_msg *)->sadb_msg_len =
7032 PFKEY_UNIT64(result->m_pkthdr.len);
7035 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
7045 * SADB_FLUSH processing
7048 * from the ikmpd, and free all entries in secastree.
7052 * NOTE: to do is only marking SADB_SASTATE_DEAD.
7054 * m will always be freed.
7057 key_flush(so, m, mhp)
7060 const struct sadb_msghdr *mhp;
7062 struct sadb_msg *newmsg;
7063 struct secashead *sah, *nextsah;
7064 struct secasvar *sav, *nextsav;
7069 IPSEC_ASSERT(so != NULL, ("null socket"));
7070 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7071 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7073 /* map satype to proto */
7074 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7075 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7077 return key_senderror(so, m, EINVAL);
7080 /* no SATYPE specified, i.e. flushing all SA. */
7082 for (sah = LIST_FIRST(&V_sahtree);
7085 nextsah = LIST_NEXT(sah, chain);
7087 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7088 && proto != sah->saidx.proto)
7092 stateidx < _ARRAYLEN(saorder_state_alive);
7094 state = saorder_state_any[stateidx];
7095 for (sav = LIST_FIRST(&sah->savtree[state]);
7099 nextsav = LIST_NEXT(sav, chain);
7101 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
7106 sah->state = SADB_SASTATE_DEAD;
7110 if (m->m_len < sizeof(struct sadb_msg) ||
7111 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
7112 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
7113 return key_senderror(so, m, ENOBUFS);
7119 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
7120 newmsg = mtod(m, struct sadb_msg *);
7121 newmsg->sadb_msg_errno = 0;
7122 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
7124 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7128 * SADB_DUMP processing
7129 * dump all entries including status of DEAD in SAD.
7132 * from the ikmpd, and dump all secasvar leaves
7137 * m will always be freed.
7140 key_dump(so, m, mhp)
7143 const struct sadb_msghdr *mhp;
7145 struct secashead *sah;
7146 struct secasvar *sav;
7152 struct sadb_msg *newmsg;
7155 IPSEC_ASSERT(so != NULL, ("null socket"));
7156 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7157 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7158 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7160 /* map satype to proto */
7161 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7162 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7164 return key_senderror(so, m, EINVAL);
7167 /* count sav entries to be sent to the userland. */
7170 LIST_FOREACH(sah, &V_sahtree, chain) {
7171 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7172 && proto != sah->saidx.proto)
7176 stateidx < _ARRAYLEN(saorder_state_any);
7178 state = saorder_state_any[stateidx];
7179 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7187 return key_senderror(so, m, ENOENT);
7190 /* send this to the userland, one at a time. */
7192 LIST_FOREACH(sah, &V_sahtree, chain) {
7193 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7194 && proto != sah->saidx.proto)
7197 /* map proto to satype */
7198 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
7200 ipseclog((LOG_DEBUG, "%s: there was invalid proto in "
7201 "SAD.\n", __func__));
7202 return key_senderror(so, m, EINVAL);
7206 stateidx < _ARRAYLEN(saorder_state_any);
7208 state = saorder_state_any[stateidx];
7209 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7210 n = key_setdumpsa(sav, SADB_DUMP, satype,
7211 --cnt, mhp->msg->sadb_msg_pid);
7214 return key_senderror(so, m, ENOBUFS);
7216 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
7227 * SADB_X_PROMISC processing
7229 * m will always be freed.
7232 key_promisc(so, m, mhp)
7235 const struct sadb_msghdr *mhp;
7239 IPSEC_ASSERT(so != NULL, ("null socket"));
7240 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7241 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7242 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7244 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7246 if (olen < sizeof(struct sadb_msg)) {
7248 return key_senderror(so, m, EINVAL);
7253 } else if (olen == sizeof(struct sadb_msg)) {
7254 /* enable/disable promisc mode */
7257 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
7258 return key_senderror(so, m, EINVAL);
7259 mhp->msg->sadb_msg_errno = 0;
7260 switch (mhp->msg->sadb_msg_satype) {
7263 kp->kp_promisc = mhp->msg->sadb_msg_satype;
7266 return key_senderror(so, m, EINVAL);
7269 /* send the original message back to everyone */
7270 mhp->msg->sadb_msg_errno = 0;
7271 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7273 /* send packet as is */
7275 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
7277 /* TODO: if sadb_msg_seq is specified, send to specific pid */
7278 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7282 static int (*key_typesw[]) __P((struct socket *, struct mbuf *,
7283 const struct sadb_msghdr *)) = {
7284 NULL, /* SADB_RESERVED */
7285 key_getspi, /* SADB_GETSPI */
7286 key_update, /* SADB_UPDATE */
7287 key_add, /* SADB_ADD */
7288 key_delete, /* SADB_DELETE */
7289 key_get, /* SADB_GET */
7290 key_acquire2, /* SADB_ACQUIRE */
7291 key_register, /* SADB_REGISTER */
7292 NULL, /* SADB_EXPIRE */
7293 key_flush, /* SADB_FLUSH */
7294 key_dump, /* SADB_DUMP */
7295 key_promisc, /* SADB_X_PROMISC */
7296 NULL, /* SADB_X_PCHANGE */
7297 key_spdadd, /* SADB_X_SPDUPDATE */
7298 key_spdadd, /* SADB_X_SPDADD */
7299 key_spddelete, /* SADB_X_SPDDELETE */
7300 key_spdget, /* SADB_X_SPDGET */
7301 NULL, /* SADB_X_SPDACQUIRE */
7302 key_spddump, /* SADB_X_SPDDUMP */
7303 key_spdflush, /* SADB_X_SPDFLUSH */
7304 key_spdadd, /* SADB_X_SPDSETIDX */
7305 NULL, /* SADB_X_SPDEXPIRE */
7306 key_spddelete2, /* SADB_X_SPDDELETE2 */
7310 * parse sadb_msg buffer to process PFKEYv2,
7311 * and create a data to response if needed.
7312 * I think to be dealed with mbuf directly.
7314 * msgp : pointer to pointer to a received buffer pulluped.
7315 * This is rewrited to response.
7316 * so : pointer to socket.
7318 * length for buffer to send to user process.
7325 struct sadb_msg *msg;
7326 struct sadb_msghdr mh;
7331 IPSEC_ASSERT(so != NULL, ("null socket"));
7332 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7334 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
7335 KEYDEBUG(KEYDEBUG_KEY_DUMP,
7336 ipseclog((LOG_DEBUG, "%s: passed sadb_msg\n", __func__));
7340 if (m->m_len < sizeof(struct sadb_msg)) {
7341 m = m_pullup(m, sizeof(struct sadb_msg));
7345 msg = mtod(m, struct sadb_msg *);
7346 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
7347 target = KEY_SENDUP_ONE;
7349 if ((m->m_flags & M_PKTHDR) == 0 ||
7350 m->m_pkthdr.len != m->m_pkthdr.len) {
7351 ipseclog((LOG_DEBUG, "%s: invalid message length.\n",__func__));
7352 V_pfkeystat.out_invlen++;
7357 if (msg->sadb_msg_version != PF_KEY_V2) {
7358 ipseclog((LOG_DEBUG, "%s: PF_KEY version %u is mismatched.\n",
7359 __func__, msg->sadb_msg_version));
7360 V_pfkeystat.out_invver++;
7365 if (msg->sadb_msg_type > SADB_MAX) {
7366 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7367 __func__, msg->sadb_msg_type));
7368 V_pfkeystat.out_invmsgtype++;
7373 /* for old-fashioned code - should be nuked */
7374 if (m->m_pkthdr.len > MCLBYTES) {
7381 MGETHDR(n, M_DONTWAIT, MT_DATA);
7382 if (n && m->m_pkthdr.len > MHLEN) {
7383 MCLGET(n, M_DONTWAIT);
7384 if ((n->m_flags & M_EXT) == 0) {
7393 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
7394 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
7400 /* align the mbuf chain so that extensions are in contiguous region. */
7401 error = key_align(m, &mh);
7408 switch (msg->sadb_msg_satype) {
7409 case SADB_SATYPE_UNSPEC:
7410 switch (msg->sadb_msg_type) {
7418 ipseclog((LOG_DEBUG, "%s: must specify satype "
7419 "when msg type=%u.\n", __func__,
7420 msg->sadb_msg_type));
7421 V_pfkeystat.out_invsatype++;
7426 case SADB_SATYPE_AH:
7427 case SADB_SATYPE_ESP:
7428 case SADB_X_SATYPE_IPCOMP:
7429 case SADB_X_SATYPE_TCPSIGNATURE:
7430 switch (msg->sadb_msg_type) {
7432 case SADB_X_SPDDELETE:
7434 case SADB_X_SPDDUMP:
7435 case SADB_X_SPDFLUSH:
7436 case SADB_X_SPDSETIDX:
7437 case SADB_X_SPDUPDATE:
7438 case SADB_X_SPDDELETE2:
7439 ipseclog((LOG_DEBUG, "%s: illegal satype=%u\n",
7440 __func__, msg->sadb_msg_type));
7441 V_pfkeystat.out_invsatype++;
7446 case SADB_SATYPE_RSVP:
7447 case SADB_SATYPE_OSPFV2:
7448 case SADB_SATYPE_RIPV2:
7449 case SADB_SATYPE_MIP:
7450 ipseclog((LOG_DEBUG, "%s: type %u isn't supported.\n",
7451 __func__, msg->sadb_msg_satype));
7452 V_pfkeystat.out_invsatype++;
7455 case 1: /* XXX: What does it do? */
7456 if (msg->sadb_msg_type == SADB_X_PROMISC)
7460 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7461 __func__, msg->sadb_msg_satype));
7462 V_pfkeystat.out_invsatype++;
7467 /* check field of upper layer protocol and address family */
7468 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
7469 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
7470 struct sadb_address *src0, *dst0;
7473 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
7474 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
7476 /* check upper layer protocol */
7477 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
7478 ipseclog((LOG_DEBUG, "%s: upper layer protocol "
7479 "mismatched.\n", __func__));
7480 V_pfkeystat.out_invaddr++;
7486 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
7487 PFKEY_ADDR_SADDR(dst0)->sa_family) {
7488 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
7490 V_pfkeystat.out_invaddr++;
7494 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7495 PFKEY_ADDR_SADDR(dst0)->sa_len) {
7496 ipseclog((LOG_DEBUG, "%s: address struct size "
7497 "mismatched.\n", __func__));
7498 V_pfkeystat.out_invaddr++;
7503 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7505 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7506 sizeof(struct sockaddr_in)) {
7507 V_pfkeystat.out_invaddr++;
7513 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7514 sizeof(struct sockaddr_in6)) {
7515 V_pfkeystat.out_invaddr++;
7521 ipseclog((LOG_DEBUG, "%s: unsupported address family\n",
7523 V_pfkeystat.out_invaddr++;
7524 error = EAFNOSUPPORT;
7528 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7530 plen = sizeof(struct in_addr) << 3;
7533 plen = sizeof(struct in6_addr) << 3;
7536 plen = 0; /*fool gcc*/
7540 /* check max prefix length */
7541 if (src0->sadb_address_prefixlen > plen ||
7542 dst0->sadb_address_prefixlen > plen) {
7543 ipseclog((LOG_DEBUG, "%s: illegal prefixlen.\n",
7545 V_pfkeystat.out_invaddr++;
7551 * prefixlen == 0 is valid because there can be a case when
7552 * all addresses are matched.
7556 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
7557 key_typesw[msg->sadb_msg_type] == NULL) {
7558 V_pfkeystat.out_invmsgtype++;
7563 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
7566 msg->sadb_msg_errno = error;
7567 return key_sendup_mbuf(so, m, target);
7571 key_senderror(so, m, code)
7576 struct sadb_msg *msg;
7578 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7579 ("mbuf too small, len %u", m->m_len));
7581 msg = mtod(m, struct sadb_msg *);
7582 msg->sadb_msg_errno = code;
7583 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
7587 * set the pointer to each header into message buffer.
7588 * m will be freed on error.
7589 * XXX larger-than-MCLBYTES extension?
7594 struct sadb_msghdr *mhp;
7597 struct sadb_ext *ext;
7602 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7603 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7604 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7605 ("mbuf too small, len %u", m->m_len));
7608 bzero(mhp, sizeof(*mhp));
7610 mhp->msg = mtod(m, struct sadb_msg *);
7611 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
7613 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7614 extlen = end; /*just in case extlen is not updated*/
7615 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
7616 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
7618 /* m is already freed */
7621 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7624 switch (ext->sadb_ext_type) {
7626 case SADB_EXT_ADDRESS_SRC:
7627 case SADB_EXT_ADDRESS_DST:
7628 case SADB_EXT_ADDRESS_PROXY:
7629 case SADB_EXT_LIFETIME_CURRENT:
7630 case SADB_EXT_LIFETIME_HARD:
7631 case SADB_EXT_LIFETIME_SOFT:
7632 case SADB_EXT_KEY_AUTH:
7633 case SADB_EXT_KEY_ENCRYPT:
7634 case SADB_EXT_IDENTITY_SRC:
7635 case SADB_EXT_IDENTITY_DST:
7636 case SADB_EXT_SENSITIVITY:
7637 case SADB_EXT_PROPOSAL:
7638 case SADB_EXT_SUPPORTED_AUTH:
7639 case SADB_EXT_SUPPORTED_ENCRYPT:
7640 case SADB_EXT_SPIRANGE:
7641 case SADB_X_EXT_POLICY:
7642 case SADB_X_EXT_SA2:
7644 case SADB_X_EXT_NAT_T_TYPE:
7645 case SADB_X_EXT_NAT_T_SPORT:
7646 case SADB_X_EXT_NAT_T_DPORT:
7647 case SADB_X_EXT_NAT_T_OAI:
7648 case SADB_X_EXT_NAT_T_OAR:
7649 case SADB_X_EXT_NAT_T_FRAG:
7651 /* duplicate check */
7653 * XXX Are there duplication payloads of either
7654 * KEY_AUTH or KEY_ENCRYPT ?
7656 if (mhp->ext[ext->sadb_ext_type] != NULL) {
7657 ipseclog((LOG_DEBUG, "%s: duplicate ext_type "
7658 "%u\n", __func__, ext->sadb_ext_type));
7660 V_pfkeystat.out_dupext++;
7665 ipseclog((LOG_DEBUG, "%s: invalid ext_type %u\n",
7666 __func__, ext->sadb_ext_type));
7668 V_pfkeystat.out_invexttype++;
7672 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
7674 if (key_validate_ext(ext, extlen)) {
7676 V_pfkeystat.out_invlen++;
7680 n = m_pulldown(m, off, extlen, &toff);
7682 /* m is already freed */
7685 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7687 mhp->ext[ext->sadb_ext_type] = ext;
7688 mhp->extoff[ext->sadb_ext_type] = off;
7689 mhp->extlen[ext->sadb_ext_type] = extlen;
7694 V_pfkeystat.out_invlen++;
7702 key_validate_ext(ext, len)
7703 const struct sadb_ext *ext;
7706 const struct sockaddr *sa;
7707 enum { NONE, ADDR } checktype = NONE;
7709 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
7711 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
7714 /* if it does not match minimum/maximum length, bail */
7715 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
7716 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
7718 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
7720 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
7723 /* more checks based on sadb_ext_type XXX need more */
7724 switch (ext->sadb_ext_type) {
7725 case SADB_EXT_ADDRESS_SRC:
7726 case SADB_EXT_ADDRESS_DST:
7727 case SADB_EXT_ADDRESS_PROXY:
7728 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7731 case SADB_EXT_IDENTITY_SRC:
7732 case SADB_EXT_IDENTITY_DST:
7733 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7734 SADB_X_IDENTTYPE_ADDR) {
7735 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7745 switch (checktype) {
7749 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
7750 if (len < baselen + sal)
7752 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7765 for (i = 0; i < IPSEC_DIR_MAX; i++)
7766 LIST_INIT(&V_sptree[i]);
7768 LIST_INIT(&V_sahtree);
7770 for (i = 0; i <= SADB_SATYPE_MAX; i++)
7771 LIST_INIT(&V_regtree[i]);
7773 LIST_INIT(&V_acqtree);
7774 LIST_INIT(&V_spacqtree);
7776 /* system default */
7777 V_ip4_def_policy.policy = IPSEC_POLICY_NONE;
7778 V_ip4_def_policy.refcnt++; /*never reclaim this*/
7780 if (!IS_DEFAULT_VNET(curvnet))
7784 REGTREE_LOCK_INIT();
7785 SAHTREE_LOCK_INIT();
7789 #ifndef IPSEC_DEBUG2
7790 timeout((void *)key_timehandler, (void *)0, hz);
7791 #endif /*IPSEC_DEBUG2*/
7793 /* initialize key statistics */
7794 keystat.getspi_count = 1;
7796 printf("IPsec: Initialized Security Association Processing.\n");
7803 struct secpolicy *sp, *nextsp;
7804 struct secacq *acq, *nextacq;
7805 struct secspacq *spacq, *nextspacq;
7806 struct secashead *sah, *nextsah;
7811 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7812 for (sp = LIST_FIRST(&V_sptree[i]);
7813 sp != NULL; sp = nextsp) {
7814 nextsp = LIST_NEXT(sp, chain);
7815 if (__LIST_CHAINED(sp)) {
7816 LIST_REMOVE(sp, chain);
7817 free(sp, M_IPSEC_SP);
7824 for (sah = LIST_FIRST(&V_sahtree); sah != NULL; sah = nextsah) {
7825 nextsah = LIST_NEXT(sah, chain);
7826 if (__LIST_CHAINED(sah)) {
7827 LIST_REMOVE(sah, chain);
7828 free(sah, M_IPSEC_SAH);
7834 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7835 LIST_FOREACH(reg, &V_regtree[i], chain) {
7836 if (__LIST_CHAINED(reg)) {
7837 LIST_REMOVE(reg, chain);
7838 free(reg, M_IPSEC_SAR);
7846 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
7847 nextacq = LIST_NEXT(acq, chain);
7848 if (__LIST_CHAINED(acq)) {
7849 LIST_REMOVE(acq, chain);
7850 free(acq, M_IPSEC_SAQ);
7856 for (spacq = LIST_FIRST(&V_spacqtree); spacq != NULL;
7857 spacq = nextspacq) {
7858 nextspacq = LIST_NEXT(spacq, chain);
7859 if (__LIST_CHAINED(spacq)) {
7860 LIST_REMOVE(spacq, chain);
7861 free(spacq, M_IPSEC_SAQ);
7869 * XXX: maybe This function is called after INBOUND IPsec processing.
7871 * Special check for tunnel-mode packets.
7872 * We must make some checks for consistency between inner and outer IP header.
7874 * xxx more checks to be provided
7877 key_checktunnelsanity(sav, family, src, dst)
7878 struct secasvar *sav;
7883 IPSEC_ASSERT(sav->sah != NULL, ("null SA header"));
7885 /* XXX: check inner IP header */
7890 /* record data transfer on SA, and update timestamps */
7892 key_sa_recordxfer(sav, m)
7893 struct secasvar *sav;
7896 IPSEC_ASSERT(sav != NULL, ("Null secasvar"));
7897 IPSEC_ASSERT(m != NULL, ("Null mbuf"));
7902 * XXX Currently, there is a difference of bytes size
7903 * between inbound and outbound processing.
7905 sav->lft_c->bytes += m->m_pkthdr.len;
7906 /* to check bytes lifetime is done in key_timehandler(). */
7909 * We use the number of packets as the unit of
7910 * allocations. We increment the variable
7911 * whenever {esp,ah}_{in,out}put is called.
7913 sav->lft_c->allocations++;
7914 /* XXX check for expires? */
7917 * NOTE: We record CURRENT usetime by using wall clock,
7918 * in seconds. HARD and SOFT lifetime are measured by the time
7919 * difference (again in seconds) from usetime.
7923 * -----+-----+--------+---> t
7924 * <--------------> HARD
7927 sav->lft_c->usetime = time_second;
7928 /* XXX check for expires? */
7935 key_sa_routechange(dst)
7936 struct sockaddr *dst;
7938 struct secashead *sah;
7942 LIST_FOREACH(sah, &V_sahtree, chain) {
7943 ro = &sah->route_cache.sa_route;
7944 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7945 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7947 ro->ro_rt = (struct rtentry *)NULL;
7954 key_sa_chgstate(struct secasvar *sav, u_int8_t state)
7956 IPSEC_ASSERT(sav != NULL, ("NULL sav"));
7957 SAHTREE_LOCK_ASSERT();
7959 if (sav->state != state) {
7960 if (__LIST_CHAINED(sav))
7961 LIST_REMOVE(sav, chain);
7963 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7969 struct secasvar *sav;
7972 IPSEC_ASSERT(sav->iv != NULL, ("null IV"));
7973 key_randomfill(sav->iv, sav->ivlen);
7977 static struct mbuf *
7981 struct mbuf *m = NULL, *n;
7986 MGET(n, M_DONTWAIT, MT_DATA);
7987 if (n && len > MLEN)
7988 MCLGET(n, M_DONTWAIT);
7996 n->m_len = M_TRAILINGSPACE(n);
7997 /* use the bottom of mbuf, hoping we can prepend afterwards */
7998 if (n->m_len > len) {
7999 t = (n->m_len - len) & ~(sizeof(long) - 1);
8016 * Take one of the kernel's security keys and convert it into a PF_KEY
8017 * structure within an mbuf, suitable for sending up to a waiting
8018 * application in user land.
8021 * src: A pointer to a kernel security key.
8022 * exttype: Which type of key this is. Refer to the PF_KEY data structures.
8024 * a valid mbuf or NULL indicating an error
8028 static struct mbuf *
8029 key_setkey(struct seckey *src, u_int16_t exttype)
8038 len = PFKEY_ALIGN8(sizeof(struct sadb_key) + _KEYLEN(src));
8039 m = key_alloc_mbuf(len);
8042 p = mtod(m, struct sadb_key *);
8044 p->sadb_key_len = PFKEY_UNIT64(len);
8045 p->sadb_key_exttype = exttype;
8046 p->sadb_key_bits = src->bits;
8047 bcopy(src->key_data, _KEYBUF(p), _KEYLEN(src));
8053 * Take one of the kernel's lifetime data structures and convert it
8054 * into a PF_KEY structure within an mbuf, suitable for sending up to
8055 * a waiting application in user land.
8058 * src: A pointer to a kernel lifetime structure.
8059 * exttype: Which type of lifetime this is. Refer to the PF_KEY
8060 * data structures for more information.
8062 * a valid mbuf or NULL indicating an error
8066 static struct mbuf *
8067 key_setlifetime(struct seclifetime *src, u_int16_t exttype)
8069 struct mbuf *m = NULL;
8070 struct sadb_lifetime *p;
8071 int len = PFKEY_ALIGN8(sizeof(struct sadb_lifetime));
8076 m = key_alloc_mbuf(len);
8079 p = mtod(m, struct sadb_lifetime *);
8082 p->sadb_lifetime_len = PFKEY_UNIT64(len);
8083 p->sadb_lifetime_exttype = exttype;
8084 p->sadb_lifetime_allocations = src->allocations;
8085 p->sadb_lifetime_bytes = src->bytes;
8086 p->sadb_lifetime_addtime = src->addtime;
8087 p->sadb_lifetime_usetime = src->usetime;