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;
1770 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1771 * add an entry to SP database, when received
1772 * <base, address(SD), (lifetime(H),) policy>
1774 * Adding to SP database,
1776 * <base, address(SD), (lifetime(H),) policy>
1777 * to the socket which was send.
1779 * SPDADD set a unique policy entry.
1780 * SPDSETIDX like SPDADD without a part of policy requests.
1781 * SPDUPDATE replace a unique policy entry.
1783 * m will always be freed.
1786 key_spdadd(so, m, mhp)
1789 const struct sadb_msghdr *mhp;
1791 struct sadb_address *src0, *dst0;
1792 struct sadb_x_policy *xpl0, *xpl;
1793 struct sadb_lifetime *lft = NULL;
1794 struct secpolicyindex spidx;
1795 struct secpolicy *newsp;
1798 IPSEC_ASSERT(so != NULL, ("null socket"));
1799 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1800 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1801 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
1803 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1804 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1805 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1806 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1807 return key_senderror(so, m, EINVAL);
1809 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1810 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1811 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1812 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1814 return key_senderror(so, m, EINVAL);
1816 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1817 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1818 < sizeof(struct sadb_lifetime)) {
1819 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1821 return key_senderror(so, m, EINVAL);
1823 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1826 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1827 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1828 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1831 * Note: do not parse SADB_X_EXT_NAT_T_* here:
1832 * we are processing traffic endpoints.
1836 /* XXX boundary check against sa_len */
1837 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1840 src0->sadb_address_prefixlen,
1841 dst0->sadb_address_prefixlen,
1842 src0->sadb_address_proto,
1845 /* checking the direciton. */
1846 switch (xpl0->sadb_x_policy_dir) {
1847 case IPSEC_DIR_INBOUND:
1848 case IPSEC_DIR_OUTBOUND:
1851 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
1852 mhp->msg->sadb_msg_errno = EINVAL;
1857 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1858 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1859 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1860 ipseclog((LOG_DEBUG, "%s: Invalid policy type.\n", __func__));
1861 return key_senderror(so, m, EINVAL);
1864 /* policy requests are mandatory when action is ipsec. */
1865 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1866 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1867 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1868 ipseclog((LOG_DEBUG, "%s: some policy requests part required\n",
1870 return key_senderror(so, m, EINVAL);
1874 * checking there is SP already or not.
1875 * SPDUPDATE doesn't depend on whether there is a SP or not.
1876 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1879 newsp = key_getsp(&spidx);
1880 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1883 newsp->state = IPSEC_SPSTATE_DEAD;
1888 if (newsp != NULL) {
1890 ipseclog((LOG_DEBUG, "%s: a SP entry exists already.\n",
1892 return key_senderror(so, m, EEXIST);
1896 /* allocation new SP entry */
1897 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1898 return key_senderror(so, m, error);
1901 if ((newsp->id = key_getnewspid()) == 0) {
1903 return key_senderror(so, m, ENOBUFS);
1906 /* XXX boundary check against sa_len */
1907 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1910 src0->sadb_address_prefixlen,
1911 dst0->sadb_address_prefixlen,
1912 src0->sadb_address_proto,
1915 /* sanity check on addr pair */
1916 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1917 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1919 return key_senderror(so, m, EINVAL);
1921 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1922 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1924 return key_senderror(so, m, EINVAL);
1927 if (newsp->req && newsp->req->saidx.src.sa.sa_family) {
1928 struct sockaddr *sa;
1929 sa = (struct sockaddr *)(src0 + 1);
1930 if (sa->sa_family != newsp->req->saidx.src.sa.sa_family) {
1932 return key_senderror(so, m, EINVAL);
1935 if (newsp->req && newsp->req->saidx.dst.sa.sa_family) {
1936 struct sockaddr *sa;
1937 sa = (struct sockaddr *)(dst0 + 1);
1938 if (sa->sa_family != newsp->req->saidx.dst.sa.sa_family) {
1940 return key_senderror(so, m, EINVAL);
1945 newsp->created = time_second;
1946 newsp->lastused = newsp->created;
1947 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1948 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1950 newsp->refcnt = 1; /* do not reclaim until I say I do */
1951 newsp->state = IPSEC_SPSTATE_ALIVE;
1952 LIST_INSERT_TAIL(&V_sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1954 /* delete the entry in spacqtree */
1955 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1956 struct secspacq *spacq = key_getspacq(&spidx);
1957 if (spacq != NULL) {
1958 /* reset counter in order to deletion by timehandler. */
1959 spacq->created = time_second;
1966 struct mbuf *n, *mpolicy;
1967 struct sadb_msg *newmsg;
1971 * Note: do not send SADB_X_EXT_NAT_T_* here:
1972 * we are sending traffic endpoints.
1975 /* create new sadb_msg to reply. */
1977 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1978 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1979 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1981 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1983 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1986 return key_senderror(so, m, ENOBUFS);
1988 if (n->m_len < sizeof(*newmsg)) {
1989 n = m_pullup(n, sizeof(*newmsg));
1991 return key_senderror(so, m, ENOBUFS);
1993 newmsg = mtod(n, struct sadb_msg *);
1994 newmsg->sadb_msg_errno = 0;
1995 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1998 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
1999 sizeof(*xpl), &off);
2000 if (mpolicy == NULL) {
2001 /* n is already freed */
2002 return key_senderror(so, m, ENOBUFS);
2004 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
2005 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
2007 return key_senderror(so, m, EINVAL);
2009 xpl->sadb_x_policy_id = newsp->id;
2012 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2017 * get new policy id.
2025 u_int32_t newid = 0;
2026 int count = V_key_spi_trycnt; /* XXX */
2027 struct secpolicy *sp;
2029 /* when requesting to allocate spi ranged */
2031 newid = (V_policy_id = (V_policy_id == ~0 ? 1 : V_policy_id + 1));
2033 if ((sp = key_getspbyid(newid)) == NULL)
2039 if (count == 0 || newid == 0) {
2040 ipseclog((LOG_DEBUG, "%s: to allocate policy id is failed.\n",
2049 * SADB_SPDDELETE processing
2051 * <base, address(SD), policy(*)>
2052 * from the user(?), and set SADB_SASTATE_DEAD,
2054 * <base, address(SD), policy(*)>
2056 * policy(*) including direction of policy.
2058 * m will always be freed.
2061 key_spddelete(so, m, mhp)
2064 const struct sadb_msghdr *mhp;
2066 struct sadb_address *src0, *dst0;
2067 struct sadb_x_policy *xpl0;
2068 struct secpolicyindex spidx;
2069 struct secpolicy *sp;
2071 IPSEC_ASSERT(so != NULL, ("null so"));
2072 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2073 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2074 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2076 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
2077 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
2078 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
2079 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2081 return key_senderror(so, m, EINVAL);
2083 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
2084 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
2085 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2086 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2088 return key_senderror(so, m, EINVAL);
2091 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2092 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2093 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2096 * Note: do not parse SADB_X_EXT_NAT_T_* here:
2097 * we are processing traffic endpoints.
2101 /* XXX boundary check against sa_len */
2102 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2105 src0->sadb_address_prefixlen,
2106 dst0->sadb_address_prefixlen,
2107 src0->sadb_address_proto,
2110 /* checking the direciton. */
2111 switch (xpl0->sadb_x_policy_dir) {
2112 case IPSEC_DIR_INBOUND:
2113 case IPSEC_DIR_OUTBOUND:
2116 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
2117 return key_senderror(so, m, EINVAL);
2120 /* Is there SP in SPD ? */
2121 if ((sp = key_getsp(&spidx)) == NULL) {
2122 ipseclog((LOG_DEBUG, "%s: no SP found.\n", __func__));
2123 return key_senderror(so, m, EINVAL);
2126 /* save policy id to buffer to be returned. */
2127 xpl0->sadb_x_policy_id = sp->id;
2130 sp->state = IPSEC_SPSTATE_DEAD;
2136 struct sadb_msg *newmsg;
2139 * Note: do not send SADB_X_EXT_NAT_T_* here:
2140 * we are sending traffic endpoints.
2143 /* create new sadb_msg to reply. */
2144 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2145 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2147 return key_senderror(so, m, ENOBUFS);
2149 newmsg = mtod(n, struct sadb_msg *);
2150 newmsg->sadb_msg_errno = 0;
2151 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2154 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2159 * SADB_SPDDELETE2 processing
2162 * from the user(?), and set SADB_SASTATE_DEAD,
2166 * policy(*) including direction of policy.
2168 * m will always be freed.
2171 key_spddelete2(so, m, mhp)
2174 const struct sadb_msghdr *mhp;
2177 struct secpolicy *sp;
2179 IPSEC_ASSERT(so != NULL, ("null socket"));
2180 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2181 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2182 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2184 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2185 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2186 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", __func__));
2187 return key_senderror(so, m, EINVAL);
2190 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2192 /* Is there SP in SPD ? */
2193 if ((sp = key_getspbyid(id)) == NULL) {
2194 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2195 return key_senderror(so, m, EINVAL);
2199 sp->state = IPSEC_SPSTATE_DEAD;
2204 struct mbuf *n, *nn;
2205 struct sadb_msg *newmsg;
2208 /* create new sadb_msg to reply. */
2209 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2211 MGETHDR(n, M_DONTWAIT, MT_DATA);
2212 if (n && len > MHLEN) {
2213 MCLGET(n, M_DONTWAIT);
2214 if ((n->m_flags & M_EXT) == 0) {
2220 return key_senderror(so, m, ENOBUFS);
2226 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2227 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2229 IPSEC_ASSERT(off == len, ("length inconsistency (off %u len %u)",
2232 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2233 mhp->extlen[SADB_X_EXT_POLICY], M_DONTWAIT);
2236 return key_senderror(so, m, ENOBUFS);
2239 n->m_pkthdr.len = 0;
2240 for (nn = n; nn; nn = nn->m_next)
2241 n->m_pkthdr.len += nn->m_len;
2243 newmsg = mtod(n, struct sadb_msg *);
2244 newmsg->sadb_msg_errno = 0;
2245 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2248 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2253 * SADB_X_GET processing
2258 * <base, address(SD), policy>
2260 * policy(*) including direction of policy.
2262 * m will always be freed.
2265 key_spdget(so, m, mhp)
2268 const struct sadb_msghdr *mhp;
2271 struct secpolicy *sp;
2274 IPSEC_ASSERT(so != NULL, ("null socket"));
2275 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2276 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2277 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2279 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2280 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2281 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2283 return key_senderror(so, m, EINVAL);
2286 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2288 /* Is there SP in SPD ? */
2289 if ((sp = key_getspbyid(id)) == NULL) {
2290 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2291 return key_senderror(so, m, ENOENT);
2294 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2297 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2299 return key_senderror(so, m, ENOBUFS);
2303 * SADB_X_SPDACQUIRE processing.
2304 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2307 * to KMD, and expect to receive
2308 * <base> with SADB_X_SPDACQUIRE if error occured,
2311 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2312 * policy(*) is without policy requests.
2315 * others: error number
2319 struct secpolicy *sp;
2321 struct mbuf *result = NULL, *m;
2322 struct secspacq *newspacq;
2324 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2325 IPSEC_ASSERT(sp->req == NULL, ("policy exists"));
2326 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
2327 ("policy not IPSEC %u", sp->policy));
2329 /* Get an entry to check whether sent message or not. */
2330 newspacq = key_getspacq(&sp->spidx);
2331 if (newspacq != NULL) {
2332 if (V_key_blockacq_count < newspacq->count) {
2333 /* reset counter and do send message. */
2334 newspacq->count = 0;
2336 /* increment counter and do nothing. */
2342 /* make new entry for blocking to send SADB_ACQUIRE. */
2343 newspacq = key_newspacq(&sp->spidx);
2344 if (newspacq == NULL)
2348 /* create new sadb_msg to reply. */
2349 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2355 result->m_pkthdr.len = 0;
2356 for (m = result; m; m = m->m_next)
2357 result->m_pkthdr.len += m->m_len;
2359 mtod(result, struct sadb_msg *)->sadb_msg_len =
2360 PFKEY_UNIT64(result->m_pkthdr.len);
2362 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2366 * SADB_SPDFLUSH processing
2369 * from the user, and free all entries in secpctree.
2373 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2375 * m will always be freed.
2378 key_spdflush(so, m, mhp)
2381 const struct sadb_msghdr *mhp;
2383 struct sadb_msg *newmsg;
2384 struct secpolicy *sp;
2387 IPSEC_ASSERT(so != NULL, ("null socket"));
2388 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2389 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2390 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2392 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2393 return key_senderror(so, m, EINVAL);
2395 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2397 LIST_FOREACH(sp, &V_sptree[dir], chain)
2398 sp->state = IPSEC_SPSTATE_DEAD;
2402 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2403 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2404 return key_senderror(so, m, ENOBUFS);
2410 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2411 newmsg = mtod(m, struct sadb_msg *);
2412 newmsg->sadb_msg_errno = 0;
2413 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2415 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2419 * SADB_SPDDUMP processing
2422 * from the user, and dump all SP leaves
2427 * m will always be freed.
2430 key_spddump(so, m, mhp)
2433 const struct sadb_msghdr *mhp;
2435 struct secpolicy *sp;
2440 IPSEC_ASSERT(so != NULL, ("null socket"));
2441 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2442 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2443 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2445 /* search SPD entry and get buffer size. */
2448 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2449 LIST_FOREACH(sp, &V_sptree[dir], chain) {
2456 return key_senderror(so, m, ENOENT);
2459 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2460 LIST_FOREACH(sp, &V_sptree[dir], chain) {
2462 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2463 mhp->msg->sadb_msg_pid);
2466 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2475 static struct mbuf *
2476 key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq, u_int32_t pid)
2478 struct mbuf *result = NULL, *m;
2479 struct seclifetime lt;
2481 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2487 * Note: do not send SADB_X_EXT_NAT_T_* here:
2488 * we are sending traffic endpoints.
2490 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2491 &sp->spidx.src.sa, sp->spidx.prefs,
2492 sp->spidx.ul_proto);
2497 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2498 &sp->spidx.dst.sa, sp->spidx.prefd,
2499 sp->spidx.ul_proto);
2510 lt.addtime=sp->created;
2511 lt.usetime= sp->lastused;
2512 m = key_setlifetime(<, SADB_EXT_LIFETIME_CURRENT);
2517 lt.addtime=sp->lifetime;
2518 lt.usetime= sp->validtime;
2519 m = key_setlifetime(<, SADB_EXT_LIFETIME_HARD);
2525 if ((result->m_flags & M_PKTHDR) == 0)
2528 if (result->m_len < sizeof(struct sadb_msg)) {
2529 result = m_pullup(result, sizeof(struct sadb_msg));
2534 result->m_pkthdr.len = 0;
2535 for (m = result; m; m = m->m_next)
2536 result->m_pkthdr.len += m->m_len;
2538 mtod(result, struct sadb_msg *)->sadb_msg_len =
2539 PFKEY_UNIT64(result->m_pkthdr.len);
2549 * get PFKEY message length for security policy and request.
2552 key_getspreqmsglen(sp)
2553 struct secpolicy *sp;
2557 tlen = sizeof(struct sadb_x_policy);
2559 /* if is the policy for ipsec ? */
2560 if (sp->policy != IPSEC_POLICY_IPSEC)
2563 /* get length of ipsec requests */
2565 struct ipsecrequest *isr;
2568 for (isr = sp->req; isr != NULL; isr = isr->next) {
2569 len = sizeof(struct sadb_x_ipsecrequest)
2570 + isr->saidx.src.sa.sa_len
2571 + isr->saidx.dst.sa.sa_len;
2573 tlen += PFKEY_ALIGN8(len);
2581 * SADB_SPDEXPIRE processing
2583 * <base, address(SD), lifetime(CH), policy>
2587 * others : error number
2591 struct secpolicy *sp;
2593 struct mbuf *result = NULL, *m;
2596 struct sadb_lifetime *lt;
2598 /* XXX: Why do we lock ? */
2600 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2602 /* set msg header */
2603 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2610 /* create lifetime extension (current and hard) */
2611 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2612 m = key_alloc_mbuf(len);
2613 if (!m || m->m_next) { /*XXX*/
2619 bzero(mtod(m, caddr_t), len);
2620 lt = mtod(m, struct sadb_lifetime *);
2621 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2622 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2623 lt->sadb_lifetime_allocations = 0;
2624 lt->sadb_lifetime_bytes = 0;
2625 lt->sadb_lifetime_addtime = sp->created;
2626 lt->sadb_lifetime_usetime = sp->lastused;
2627 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2628 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2629 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2630 lt->sadb_lifetime_allocations = 0;
2631 lt->sadb_lifetime_bytes = 0;
2632 lt->sadb_lifetime_addtime = sp->lifetime;
2633 lt->sadb_lifetime_usetime = sp->validtime;
2637 * Note: do not send SADB_X_EXT_NAT_T_* here:
2638 * we are sending traffic endpoints.
2641 /* set sadb_address for source */
2642 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2644 sp->spidx.prefs, sp->spidx.ul_proto);
2651 /* set sadb_address for destination */
2652 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2654 sp->spidx.prefd, sp->spidx.ul_proto);
2669 if ((result->m_flags & M_PKTHDR) == 0) {
2674 if (result->m_len < sizeof(struct sadb_msg)) {
2675 result = m_pullup(result, sizeof(struct sadb_msg));
2676 if (result == NULL) {
2682 result->m_pkthdr.len = 0;
2683 for (m = result; m; m = m->m_next)
2684 result->m_pkthdr.len += m->m_len;
2686 mtod(result, struct sadb_msg *)->sadb_msg_len =
2687 PFKEY_UNIT64(result->m_pkthdr.len);
2689 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2697 /* %%% SAD management */
2699 * allocating a memory for new SA head, and copy from the values of mhp.
2700 * OUT: NULL : failure due to the lack of memory.
2701 * others : pointer to new SA head.
2703 static struct secashead *
2705 struct secasindex *saidx;
2707 struct secashead *newsah;
2709 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
2711 newsah = malloc(sizeof(struct secashead), M_IPSEC_SAH, M_NOWAIT|M_ZERO);
2712 if (newsah != NULL) {
2714 for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++)
2715 LIST_INIT(&newsah->savtree[i]);
2716 newsah->saidx = *saidx;
2718 /* add to saidxtree */
2719 newsah->state = SADB_SASTATE_MATURE;
2722 LIST_INSERT_HEAD(&V_sahtree, newsah, chain);
2729 * delete SA index and all SA registerd.
2733 struct secashead *sah;
2735 struct secasvar *sav, *nextsav;
2739 IPSEC_ASSERT(sah != NULL, ("NULL sah"));
2740 SAHTREE_LOCK_ASSERT();
2742 /* searching all SA registerd in the secindex. */
2744 stateidx < _ARRAYLEN(saorder_state_any);
2746 u_int state = saorder_state_any[stateidx];
2747 LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain, nextsav) {
2748 if (sav->refcnt == 0) {
2750 KEY_CHKSASTATE(state, sav->state, __func__);
2752 * do NOT call KEY_FREESAV here:
2753 * it will only delete the sav if refcnt == 1,
2754 * where we already know that refcnt == 0
2758 /* give up to delete this sa */
2763 if (!zombie) { /* delete only if there are savs */
2764 /* remove from tree of SA index */
2765 if (__LIST_CHAINED(sah))
2766 LIST_REMOVE(sah, chain);
2767 if (sah->route_cache.sa_route.ro_rt) {
2768 RTFREE(sah->route_cache.sa_route.ro_rt);
2769 sah->route_cache.sa_route.ro_rt = (struct rtentry *)NULL;
2771 free(sah, M_IPSEC_SAH);
2776 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2777 * and copy the values of mhp into new buffer.
2778 * When SAD message type is GETSPI:
2779 * to set sequence number from acq_seq++,
2780 * to set zero to SPI.
2781 * not to call key_setsava().
2783 * others : pointer to new secasvar.
2785 * does not modify mbuf. does not free mbuf on error.
2787 static struct secasvar *
2788 key_newsav(m, mhp, sah, errp, where, tag)
2790 const struct sadb_msghdr *mhp;
2791 struct secashead *sah;
2796 struct secasvar *newsav;
2797 const struct sadb_sa *xsa;
2799 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2800 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2801 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2802 IPSEC_ASSERT(sah != NULL, ("null secashead"));
2804 newsav = malloc(sizeof(struct secasvar), M_IPSEC_SA, M_NOWAIT|M_ZERO);
2805 if (newsav == NULL) {
2806 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2811 switch (mhp->msg->sadb_msg_type) {
2815 #ifdef IPSEC_DOSEQCHECK
2816 /* sync sequence number */
2817 if (mhp->msg->sadb_msg_seq == 0)
2819 (V_acq_seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq));
2822 newsav->seq = mhp->msg->sadb_msg_seq;
2827 if (mhp->ext[SADB_EXT_SA] == NULL) {
2828 free(newsav, M_IPSEC_SA);
2830 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2835 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2836 newsav->spi = xsa->sadb_sa_spi;
2837 newsav->seq = mhp->msg->sadb_msg_seq;
2840 free(newsav, M_IPSEC_SA);
2847 /* copy sav values */
2848 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2849 *errp = key_setsaval(newsav, m, mhp);
2851 free(newsav, M_IPSEC_SA);
2857 SECASVAR_LOCK_INIT(newsav);
2860 newsav->created = time_second;
2861 newsav->pid = mhp->msg->sadb_msg_pid;
2866 newsav->state = SADB_SASTATE_LARVAL;
2868 /* XXX locking??? */
2869 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2872 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
2873 printf("DP %s from %s:%u return SP:%p\n", __func__,
2874 where, tag, newsav));
2880 * free() SA variable entry.
2883 key_cleansav(struct secasvar *sav)
2886 * Cleanup xform state. Note that zeroize'ing causes the
2887 * keys to be cleared; otherwise we must do it ourself.
2889 if (sav->tdb_xform != NULL) {
2890 sav->tdb_xform->xf_zeroize(sav);
2891 sav->tdb_xform = NULL;
2893 KASSERT(sav->iv == NULL, ("iv but no xform"));
2894 if (sav->key_auth != NULL)
2895 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
2896 if (sav->key_enc != NULL)
2897 bzero(sav->key_enc->key_data, _KEYLEN(sav->key_enc));
2899 if (sav->key_auth != NULL) {
2900 if (sav->key_auth->key_data != NULL)
2901 free(sav->key_auth->key_data, M_IPSEC_MISC);
2902 free(sav->key_auth, M_IPSEC_MISC);
2903 sav->key_auth = NULL;
2905 if (sav->key_enc != NULL) {
2906 if (sav->key_enc->key_data != NULL)
2907 free(sav->key_enc->key_data, M_IPSEC_MISC);
2908 free(sav->key_enc, M_IPSEC_MISC);
2909 sav->key_enc = NULL;
2912 bzero(sav->sched, sav->schedlen);
2913 free(sav->sched, M_IPSEC_MISC);
2916 if (sav->replay != NULL) {
2917 free(sav->replay, M_IPSEC_MISC);
2920 if (sav->lft_c != NULL) {
2921 free(sav->lft_c, M_IPSEC_MISC);
2924 if (sav->lft_h != NULL) {
2925 free(sav->lft_h, M_IPSEC_MISC);
2928 if (sav->lft_s != NULL) {
2929 free(sav->lft_s, M_IPSEC_MISC);
2935 * free() SA variable entry.
2939 struct secasvar *sav;
2941 IPSEC_ASSERT(sav != NULL, ("null sav"));
2942 IPSEC_ASSERT(sav->refcnt == 0, ("reference count %u > 0", sav->refcnt));
2944 /* remove from SA header */
2945 if (__LIST_CHAINED(sav))
2946 LIST_REMOVE(sav, chain);
2948 SECASVAR_LOCK_DESTROY(sav);
2949 free(sav, M_IPSEC_SA);
2956 * others : found, pointer to a SA.
2958 static struct secashead *
2960 struct secasindex *saidx;
2962 struct secashead *sah;
2965 LIST_FOREACH(sah, &V_sahtree, chain) {
2966 if (sah->state == SADB_SASTATE_DEAD)
2968 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2977 * check not to be duplicated SPI.
2978 * NOTE: this function is too slow due to searching all SAD.
2981 * others : found, pointer to a SA.
2983 static struct secasvar *
2984 key_checkspidup(saidx, spi)
2985 struct secasindex *saidx;
2988 struct secashead *sah;
2989 struct secasvar *sav;
2991 /* check address family */
2992 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
2993 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
3001 LIST_FOREACH(sah, &V_sahtree, chain) {
3002 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
3004 sav = key_getsavbyspi(sah, spi);
3014 * search SAD litmited alive SA, protocol, SPI.
3017 * others : found, pointer to a SA.
3019 static struct secasvar *
3020 key_getsavbyspi(sah, spi)
3021 struct secashead *sah;
3024 struct secasvar *sav;
3025 u_int stateidx, state;
3028 SAHTREE_LOCK_ASSERT();
3029 /* search all status */
3031 stateidx < _ARRAYLEN(saorder_state_alive);
3034 state = saorder_state_alive[stateidx];
3035 LIST_FOREACH(sav, &sah->savtree[state], chain) {
3038 if (sav->state != state) {
3039 ipseclog((LOG_DEBUG, "%s: "
3040 "invalid sav->state (queue: %d SA: %d)\n",
3041 __func__, state, sav->state));
3045 if (sav->spi == spi)
3054 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
3055 * You must update these if need.
3059 * does not modify mbuf. does not free mbuf on error.
3062 key_setsaval(sav, m, mhp)
3063 struct secasvar *sav;
3065 const struct sadb_msghdr *mhp;
3069 IPSEC_ASSERT(m != NULL, ("null mbuf"));
3070 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
3071 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
3073 /* initialization */
3075 sav->key_auth = NULL;
3076 sav->key_enc = NULL;
3083 sav->tdb_xform = NULL; /* transform */
3084 sav->tdb_encalgxform = NULL; /* encoding algorithm */
3085 sav->tdb_authalgxform = NULL; /* authentication algorithm */
3086 sav->tdb_compalgxform = NULL; /* compression algorithm */
3087 /* Initialize even if NAT-T not compiled in: */
3089 sav->natt_esp_frag_len = 0;
3092 if (mhp->ext[SADB_EXT_SA] != NULL) {
3093 const struct sadb_sa *sa0;
3095 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
3096 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
3101 sav->alg_auth = sa0->sadb_sa_auth;
3102 sav->alg_enc = sa0->sadb_sa_encrypt;
3103 sav->flags = sa0->sadb_sa_flags;
3106 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
3107 sav->replay = (struct secreplay *)
3108 malloc(sizeof(struct secreplay)+sa0->sadb_sa_replay, M_IPSEC_MISC, M_NOWAIT|M_ZERO);
3109 if (sav->replay == NULL) {
3110 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3115 if (sa0->sadb_sa_replay != 0)
3116 sav->replay->bitmap = (caddr_t)(sav->replay+1);
3117 sav->replay->wsize = sa0->sadb_sa_replay;
3121 /* Authentication keys */
3122 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
3123 const struct sadb_key *key0;
3126 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
3127 len = mhp->extlen[SADB_EXT_KEY_AUTH];
3130 if (len < sizeof(*key0)) {
3134 switch (mhp->msg->sadb_msg_satype) {
3135 case SADB_SATYPE_AH:
3136 case SADB_SATYPE_ESP:
3137 case SADB_X_SATYPE_TCPSIGNATURE:
3138 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3139 sav->alg_auth != SADB_X_AALG_NULL)
3142 case SADB_X_SATYPE_IPCOMP:
3148 ipseclog((LOG_DEBUG, "%s: invalid key_auth values.\n",
3153 sav->key_auth = (struct seckey *)key_dup_keymsg(key0, len,
3155 if (sav->key_auth == NULL ) {
3156 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3163 /* Encryption key */
3164 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3165 const struct sadb_key *key0;
3168 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3169 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3172 if (len < sizeof(*key0)) {
3176 switch (mhp->msg->sadb_msg_satype) {
3177 case SADB_SATYPE_ESP:
3178 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3179 sav->alg_enc != SADB_EALG_NULL) {
3183 sav->key_enc = (struct seckey *)key_dup_keymsg(key0,
3186 if (sav->key_enc == NULL) {
3187 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3193 case SADB_X_SATYPE_IPCOMP:
3194 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3196 sav->key_enc = NULL; /*just in case*/
3198 case SADB_SATYPE_AH:
3199 case SADB_X_SATYPE_TCPSIGNATURE:
3205 ipseclog((LOG_DEBUG, "%s: invalid key_enc value.\n",
3214 switch (mhp->msg->sadb_msg_satype) {
3215 case SADB_SATYPE_AH:
3216 error = xform_init(sav, XF_AH);
3218 case SADB_SATYPE_ESP:
3219 error = xform_init(sav, XF_ESP);
3221 case SADB_X_SATYPE_IPCOMP:
3222 error = xform_init(sav, XF_IPCOMP);
3224 case SADB_X_SATYPE_TCPSIGNATURE:
3225 error = xform_init(sav, XF_TCPSIGNATURE);
3229 ipseclog((LOG_DEBUG, "%s: unable to initialize SA type %u.\n",
3230 __func__, mhp->msg->sadb_msg_satype));
3235 sav->created = time_second;
3237 /* make lifetime for CURRENT */
3238 sav->lft_c = malloc(sizeof(struct seclifetime), M_IPSEC_MISC, M_NOWAIT);
3239 if (sav->lft_c == NULL) {
3240 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3245 sav->lft_c->allocations = 0;
3246 sav->lft_c->bytes = 0;
3247 sav->lft_c->addtime = time_second;
3248 sav->lft_c->usetime = 0;
3250 /* lifetimes for HARD and SOFT */
3252 const struct sadb_lifetime *lft0;
3254 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3256 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3260 sav->lft_h = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3261 if (sav->lft_h == NULL) {
3262 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3266 /* to be initialize ? */
3269 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3271 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3275 sav->lft_s = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3276 if (sav->lft_s == NULL) {
3277 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3281 /* to be initialize ? */
3288 /* initialization */
3295 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3300 key_mature(struct secasvar *sav)
3304 /* check SPI value */
3305 switch (sav->sah->saidx.proto) {
3309 * RFC 4302, 2.4. Security Parameters Index (SPI), SPI values
3310 * 1-255 reserved by IANA for future use,
3311 * 0 for implementation specific, local use.
3313 if (ntohl(sav->spi) <= 255) {
3314 ipseclog((LOG_DEBUG, "%s: illegal range of SPI %u.\n",
3315 __func__, (u_int32_t)ntohl(sav->spi)));
3322 switch (sav->sah->saidx.proto) {
3325 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3326 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3327 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3328 "given to old-esp.\n", __func__));
3331 error = xform_init(sav, XF_ESP);
3335 if (sav->flags & SADB_X_EXT_DERIV) {
3336 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3337 "given to AH SA.\n", __func__));
3340 if (sav->alg_enc != SADB_EALG_NONE) {
3341 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3342 "mismated.\n", __func__));
3345 error = xform_init(sav, XF_AH);
3347 case IPPROTO_IPCOMP:
3348 if (sav->alg_auth != SADB_AALG_NONE) {
3349 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3350 "mismated.\n", __func__));
3353 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3354 && ntohl(sav->spi) >= 0x10000) {
3355 ipseclog((LOG_DEBUG, "%s: invalid cpi for IPComp.\n",
3359 error = xform_init(sav, XF_IPCOMP);
3362 if (sav->alg_enc != SADB_EALG_NONE) {
3363 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3364 "mismated.\n", __func__));
3367 error = xform_init(sav, XF_TCPSIGNATURE);
3370 ipseclog((LOG_DEBUG, "%s: Invalid satype.\n", __func__));
3371 error = EPROTONOSUPPORT;
3376 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3383 * subroutine for SADB_GET and SADB_DUMP.
3385 static struct mbuf *
3386 key_setdumpsa(struct secasvar *sav, u_int8_t type, u_int8_t satype,
3387 u_int32_t seq, u_int32_t pid)
3389 struct mbuf *result = NULL, *tres = NULL, *m;
3392 SADB_EXT_SA, SADB_X_EXT_SA2,
3393 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3394 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3395 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3396 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3397 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3399 SADB_X_EXT_NAT_T_TYPE,
3400 SADB_X_EXT_NAT_T_SPORT, SADB_X_EXT_NAT_T_DPORT,
3401 SADB_X_EXT_NAT_T_OAI, SADB_X_EXT_NAT_T_OAR,
3402 SADB_X_EXT_NAT_T_FRAG,
3406 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3411 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3413 switch (dumporder[i]) {
3415 m = key_setsadbsa(sav);
3420 case SADB_X_EXT_SA2:
3421 m = key_setsadbxsa2(sav->sah->saidx.mode,
3422 sav->replay ? sav->replay->count : 0,
3423 sav->sah->saidx.reqid);
3428 case SADB_EXT_ADDRESS_SRC:
3429 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3430 &sav->sah->saidx.src.sa,
3431 FULLMASK, IPSEC_ULPROTO_ANY);
3436 case SADB_EXT_ADDRESS_DST:
3437 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3438 &sav->sah->saidx.dst.sa,
3439 FULLMASK, IPSEC_ULPROTO_ANY);
3444 case SADB_EXT_KEY_AUTH:
3447 m = key_setkey(sav->key_auth, SADB_EXT_KEY_AUTH);
3452 case SADB_EXT_KEY_ENCRYPT:
3455 m = key_setkey(sav->key_enc, SADB_EXT_KEY_ENCRYPT);
3460 case SADB_EXT_LIFETIME_CURRENT:
3463 m = key_setlifetime(sav->lft_c,
3464 SADB_EXT_LIFETIME_CURRENT);
3469 case SADB_EXT_LIFETIME_HARD:
3472 m = key_setlifetime(sav->lft_h,
3473 SADB_EXT_LIFETIME_HARD);
3478 case SADB_EXT_LIFETIME_SOFT:
3481 m = key_setlifetime(sav->lft_s,
3482 SADB_EXT_LIFETIME_SOFT);
3489 case SADB_X_EXT_NAT_T_TYPE:
3490 m = key_setsadbxtype(sav->natt_type);
3495 case SADB_X_EXT_NAT_T_DPORT:
3496 m = key_setsadbxport(
3497 KEY_PORTFROMSADDR(&sav->sah->saidx.dst),
3498 SADB_X_EXT_NAT_T_DPORT);
3503 case SADB_X_EXT_NAT_T_SPORT:
3504 m = key_setsadbxport(
3505 KEY_PORTFROMSADDR(&sav->sah->saidx.src),
3506 SADB_X_EXT_NAT_T_SPORT);
3511 case SADB_X_EXT_NAT_T_OAI:
3512 case SADB_X_EXT_NAT_T_OAR:
3513 case SADB_X_EXT_NAT_T_FRAG:
3514 /* We do not (yet) support those. */
3518 case SADB_EXT_ADDRESS_PROXY:
3519 case SADB_EXT_IDENTITY_SRC:
3520 case SADB_EXT_IDENTITY_DST:
3521 /* XXX: should we brought from SPD ? */
3522 case SADB_EXT_SENSITIVITY:
3535 m_cat(result, tres);
3536 if (result->m_len < sizeof(struct sadb_msg)) {
3537 result = m_pullup(result, sizeof(struct sadb_msg));
3542 result->m_pkthdr.len = 0;
3543 for (m = result; m; m = m->m_next)
3544 result->m_pkthdr.len += m->m_len;
3546 mtod(result, struct sadb_msg *)->sadb_msg_len =
3547 PFKEY_UNIT64(result->m_pkthdr.len);
3558 * set data into sadb_msg.
3560 static struct mbuf *
3561 key_setsadbmsg(u_int8_t type, u_int16_t tlen, u_int8_t satype, u_int32_t seq,
3562 pid_t pid, u_int16_t reserved)
3568 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3571 MGETHDR(m, M_DONTWAIT, MT_DATA);
3572 if (m && len > MHLEN) {
3573 MCLGET(m, M_DONTWAIT);
3574 if ((m->m_flags & M_EXT) == 0) {
3581 m->m_pkthdr.len = m->m_len = len;
3584 p = mtod(m, struct sadb_msg *);
3587 p->sadb_msg_version = PF_KEY_V2;
3588 p->sadb_msg_type = type;
3589 p->sadb_msg_errno = 0;
3590 p->sadb_msg_satype = satype;
3591 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3592 p->sadb_msg_reserved = reserved;
3593 p->sadb_msg_seq = seq;
3594 p->sadb_msg_pid = (u_int32_t)pid;
3600 * copy secasvar data into sadb_address.
3602 static struct mbuf *
3604 struct secasvar *sav;
3610 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3611 m = key_alloc_mbuf(len);
3612 if (!m || m->m_next) { /*XXX*/
3618 p = mtod(m, struct sadb_sa *);
3621 p->sadb_sa_len = PFKEY_UNIT64(len);
3622 p->sadb_sa_exttype = SADB_EXT_SA;
3623 p->sadb_sa_spi = sav->spi;
3624 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3625 p->sadb_sa_state = sav->state;
3626 p->sadb_sa_auth = sav->alg_auth;
3627 p->sadb_sa_encrypt = sav->alg_enc;
3628 p->sadb_sa_flags = sav->flags;
3634 * set data into sadb_address.
3636 static struct mbuf *
3637 key_setsadbaddr(u_int16_t exttype, const struct sockaddr *saddr, u_int8_t prefixlen, u_int16_t ul_proto)
3640 struct sadb_address *p;
3643 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3644 PFKEY_ALIGN8(saddr->sa_len);
3645 m = key_alloc_mbuf(len);
3646 if (!m || m->m_next) { /*XXX*/
3652 p = mtod(m, struct sadb_address *);
3655 p->sadb_address_len = PFKEY_UNIT64(len);
3656 p->sadb_address_exttype = exttype;
3657 p->sadb_address_proto = ul_proto;
3658 if (prefixlen == FULLMASK) {
3659 switch (saddr->sa_family) {
3661 prefixlen = sizeof(struct in_addr) << 3;
3664 prefixlen = sizeof(struct in6_addr) << 3;
3670 p->sadb_address_prefixlen = prefixlen;
3671 p->sadb_address_reserved = 0;
3674 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3681 * set data into sadb_x_sa2.
3683 static struct mbuf *
3684 key_setsadbxsa2(u_int8_t mode, u_int32_t seq, u_int32_t reqid)
3687 struct sadb_x_sa2 *p;
3690 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3691 m = key_alloc_mbuf(len);
3692 if (!m || m->m_next) { /*XXX*/
3698 p = mtod(m, struct sadb_x_sa2 *);
3701 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3702 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3703 p->sadb_x_sa2_mode = mode;
3704 p->sadb_x_sa2_reserved1 = 0;
3705 p->sadb_x_sa2_reserved2 = 0;
3706 p->sadb_x_sa2_sequence = seq;
3707 p->sadb_x_sa2_reqid = reqid;
3714 * Set a type in sadb_x_nat_t_type.
3716 static struct mbuf *
3717 key_setsadbxtype(u_int16_t type)
3721 struct sadb_x_nat_t_type *p;
3723 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_type));
3725 m = key_alloc_mbuf(len);
3726 if (!m || m->m_next) { /*XXX*/
3732 p = mtod(m, struct sadb_x_nat_t_type *);
3735 p->sadb_x_nat_t_type_len = PFKEY_UNIT64(len);
3736 p->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
3737 p->sadb_x_nat_t_type_type = type;
3742 * Set a port in sadb_x_nat_t_port.
3743 * In contrast to default RFC 2367 behaviour, port is in network byte order.
3745 static struct mbuf *
3746 key_setsadbxport(u_int16_t port, u_int16_t type)
3750 struct sadb_x_nat_t_port *p;
3752 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_port));
3754 m = key_alloc_mbuf(len);
3755 if (!m || m->m_next) { /*XXX*/
3761 p = mtod(m, struct sadb_x_nat_t_port *);
3764 p->sadb_x_nat_t_port_len = PFKEY_UNIT64(len);
3765 p->sadb_x_nat_t_port_exttype = type;
3766 p->sadb_x_nat_t_port_port = port;
3772 * Get port from sockaddr. Port is in network byte order.
3775 key_portfromsaddr(struct sockaddr *sa)
3778 switch (sa->sa_family) {
3781 return ((struct sockaddr_in *)sa)->sin_port;
3785 return ((struct sockaddr_in6 *)sa)->sin6_port;
3788 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
3789 printf("DP %s unexpected address family %d\n",
3790 __func__, sa->sa_family));
3793 #endif /* IPSEC_NAT_T */
3796 * Set port in struct sockaddr. Port is in network byte order.
3799 key_porttosaddr(struct sockaddr *sa, u_int16_t port)
3802 switch (sa->sa_family) {
3805 ((struct sockaddr_in *)sa)->sin_port = port;
3810 ((struct sockaddr_in6 *)sa)->sin6_port = port;
3814 ipseclog((LOG_DEBUG, "%s: unexpected address family %d.\n",
3815 __func__, sa->sa_family));
3821 * set data into sadb_x_policy
3823 static struct mbuf *
3824 key_setsadbxpolicy(u_int16_t type, u_int8_t dir, u_int32_t id)
3827 struct sadb_x_policy *p;
3830 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3831 m = key_alloc_mbuf(len);
3832 if (!m || m->m_next) { /*XXX*/
3838 p = mtod(m, struct sadb_x_policy *);
3841 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3842 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3843 p->sadb_x_policy_type = type;
3844 p->sadb_x_policy_dir = dir;
3845 p->sadb_x_policy_id = id;
3851 /* Take a key message (sadb_key) from the socket and turn it into one
3852 * of the kernel's key structures (seckey).
3854 * IN: pointer to the src
3855 * OUT: NULL no more memory
3858 key_dup_keymsg(const struct sadb_key *src, u_int len,
3859 struct malloc_type *type)
3862 dst = (struct seckey *)malloc(sizeof(struct seckey), type, M_NOWAIT);
3864 dst->bits = src->sadb_key_bits;
3865 dst->key_data = (char *)malloc(len, type, M_NOWAIT);
3866 if (dst->key_data != NULL) {
3867 bcopy((const char *)src + sizeof(struct sadb_key),
3868 dst->key_data, len);
3870 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3876 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3883 /* Take a lifetime message (sadb_lifetime) passed in on a socket and
3884 * turn it into one of the kernel's lifetime structures (seclifetime).
3886 * IN: pointer to the destination, source and malloc type
3887 * OUT: NULL, no more memory
3890 static struct seclifetime *
3891 key_dup_lifemsg(const struct sadb_lifetime *src,
3892 struct malloc_type *type)
3894 struct seclifetime *dst = NULL;
3896 dst = (struct seclifetime *)malloc(sizeof(struct seclifetime),
3900 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3902 dst->allocations = src->sadb_lifetime_allocations;
3903 dst->bytes = src->sadb_lifetime_bytes;
3904 dst->addtime = src->sadb_lifetime_addtime;
3905 dst->usetime = src->sadb_lifetime_usetime;
3910 /* compare my own address
3911 * OUT: 1: true, i.e. my address.
3916 struct sockaddr *sa;
3919 struct sockaddr_in *sin;
3920 struct in_ifaddr *ia;
3923 IPSEC_ASSERT(sa != NULL, ("null sockaddr"));
3925 switch (sa->sa_family) {
3928 sin = (struct sockaddr_in *)sa;
3930 for (ia = V_in_ifaddrhead.tqh_first; ia;
3931 ia = ia->ia_link.tqe_next)
3933 if (sin->sin_family == ia->ia_addr.sin_family &&
3934 sin->sin_len == ia->ia_addr.sin_len &&
3935 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3937 IN_IFADDR_RUNLOCK();
3941 IN_IFADDR_RUNLOCK();
3946 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3955 * compare my own address for IPv6.
3958 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3960 #include <netinet6/in6_var.h>
3964 struct sockaddr_in6 *sin6;
3966 struct in6_ifaddr *ia;
3968 struct in6_multi *in6m;
3972 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
3973 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3974 (struct sockaddr *)&ia->ia_addr, 0) == 0) {
3975 IN6_IFADDR_RUNLOCK();
3982 * XXX why do we care about multlicast here while we don't care
3983 * about IPv4 multicast??
3987 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3989 IN6_IFADDR_RUNLOCK();
3994 IN6_IFADDR_RUNLOCK();
3996 /* loopback, just for safety */
3997 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
4005 * compare two secasindex structure.
4006 * flag can specify to compare 2 saidxes.
4007 * compare two secasindex structure without both mode and reqid.
4008 * don't compare port.
4010 * saidx0: source, it can be in SAD.
4018 const struct secasindex *saidx0,
4019 const struct secasindex *saidx1,
4025 if (saidx0 == NULL && saidx1 == NULL)
4028 if (saidx0 == NULL || saidx1 == NULL)
4031 if (saidx0->proto != saidx1->proto)
4034 if (flag == CMP_EXACTLY) {
4035 if (saidx0->mode != saidx1->mode)
4037 if (saidx0->reqid != saidx1->reqid)
4039 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
4040 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
4044 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
4045 if (flag == CMP_MODE_REQID
4046 ||flag == CMP_REQID) {
4048 * If reqid of SPD is non-zero, unique SA is required.
4049 * The result must be of same reqid in this case.
4051 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
4055 if (flag == CMP_MODE_REQID) {
4056 if (saidx0->mode != IPSEC_MODE_ANY
4057 && saidx0->mode != saidx1->mode)
4063 * If NAT-T is enabled, check ports for tunnel mode.
4064 * Do not check ports if they are set to zero in the SPD.
4065 * Also do not do it for transport mode, as there is no
4066 * port information available in the SP.
4068 if (saidx1->mode == IPSEC_MODE_TUNNEL &&
4069 saidx1->src.sa.sa_family == AF_INET &&
4070 saidx1->dst.sa.sa_family == AF_INET &&
4071 ((const struct sockaddr_in *)(&saidx1->src))->sin_port &&
4072 ((const struct sockaddr_in *)(&saidx1->dst))->sin_port)
4074 #endif /* IPSEC_NAT_T */
4076 if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, chkport) != 0) {
4079 if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, chkport) != 0) {
4088 * compare two secindex structure exactly.
4090 * spidx0: source, it is often in SPD.
4091 * spidx1: object, it is often from PFKEY message.
4097 key_cmpspidx_exactly(
4098 struct secpolicyindex *spidx0,
4099 struct secpolicyindex *spidx1)
4102 if (spidx0 == NULL && spidx1 == NULL)
4105 if (spidx0 == NULL || spidx1 == NULL)
4108 if (spidx0->prefs != spidx1->prefs
4109 || spidx0->prefd != spidx1->prefd
4110 || spidx0->ul_proto != spidx1->ul_proto)
4113 return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
4114 key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
4118 * compare two secindex structure with mask.
4120 * spidx0: source, it is often in SPD.
4121 * spidx1: object, it is often from IP header.
4127 key_cmpspidx_withmask(
4128 struct secpolicyindex *spidx0,
4129 struct secpolicyindex *spidx1)
4132 if (spidx0 == NULL && spidx1 == NULL)
4135 if (spidx0 == NULL || spidx1 == NULL)
4138 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
4139 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
4140 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
4141 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
4144 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4145 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
4146 && spidx0->ul_proto != spidx1->ul_proto)
4149 switch (spidx0->src.sa.sa_family) {
4151 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
4152 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
4154 if (!key_bbcmp(&spidx0->src.sin.sin_addr,
4155 &spidx1->src.sin.sin_addr, spidx0->prefs))
4159 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
4160 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
4163 * scope_id check. if sin6_scope_id is 0, we regard it
4164 * as a wildcard scope, which matches any scope zone ID.
4166 if (spidx0->src.sin6.sin6_scope_id &&
4167 spidx1->src.sin6.sin6_scope_id &&
4168 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
4170 if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
4171 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
4176 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
4181 switch (spidx0->dst.sa.sa_family) {
4183 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
4184 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
4186 if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
4187 &spidx1->dst.sin.sin_addr, spidx0->prefd))
4191 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
4192 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
4195 * scope_id check. if sin6_scope_id is 0, we regard it
4196 * as a wildcard scope, which matches any scope zone ID.
4198 if (spidx0->dst.sin6.sin6_scope_id &&
4199 spidx1->dst.sin6.sin6_scope_id &&
4200 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
4202 if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
4203 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
4208 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
4213 /* XXX Do we check other field ? e.g. flowinfo */
4218 /* returns 0 on match */
4221 const struct sockaddr *sa1,
4222 const struct sockaddr *sa2,
4228 #define satosin(s) ((const struct sockaddr_in *)s)
4232 #define satosin6(s) ((const struct sockaddr_in6 *)s)
4233 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
4236 switch (sa1->sa_family) {
4238 if (sa1->sa_len != sizeof(struct sockaddr_in))
4240 if (satosin(sa1)->sin_addr.s_addr !=
4241 satosin(sa2)->sin_addr.s_addr) {
4244 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
4248 if (sa1->sa_len != sizeof(struct sockaddr_in6))
4249 return 1; /*EINVAL*/
4250 if (satosin6(sa1)->sin6_scope_id !=
4251 satosin6(sa2)->sin6_scope_id) {
4254 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
4255 &satosin6(sa2)->sin6_addr)) {
4259 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
4264 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
4275 * compare two buffers with mask.
4279 * bits: Number of bits to compare
4285 key_bbcmp(const void *a1, const void *a2, u_int bits)
4287 const unsigned char *p1 = a1;
4288 const unsigned char *p2 = a2;
4290 /* XXX: This could be considerably faster if we compare a word
4291 * at a time, but it is complicated on LSB Endian machines */
4293 /* Handle null pointers */
4294 if (p1 == NULL || p2 == NULL)
4304 u_int8_t mask = ~((1<<(8-bits))-1);
4305 if ((*p1 & mask) != (*p2 & mask))
4308 return 1; /* Match! */
4312 key_flush_spd(time_t now)
4314 static u_int16_t sptree_scangen = 0;
4315 u_int16_t gen = sptree_scangen++;
4316 struct secpolicy *sp;
4320 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4323 LIST_FOREACH(sp, &V_sptree[dir], chain) {
4324 if (sp->scangen == gen) /* previously handled */
4327 if (sp->state == IPSEC_SPSTATE_DEAD &&
4330 * Ensure that we only decrease refcnt once,
4331 * when we're the last consumer.
4332 * Directly call SP_DELREF/key_delsp instead
4333 * of KEY_FREESP to avoid unlocking/relocking
4334 * SPTREE_LOCK before key_delsp: may refcnt
4335 * be increased again during that time ?
4336 * NB: also clean entries created by
4344 if (sp->lifetime == 0 && sp->validtime == 0)
4346 if ((sp->lifetime && now - sp->created > sp->lifetime)
4347 || (sp->validtime && now - sp->lastused > sp->validtime)) {
4348 sp->state = IPSEC_SPSTATE_DEAD;
4359 key_flush_sad(time_t now)
4361 struct secashead *sah, *nextsah;
4362 struct secasvar *sav, *nextsav;
4366 LIST_FOREACH_SAFE(sah, &V_sahtree, chain, nextsah) {
4367 /* if sah has been dead, then delete it and process next sah. */
4368 if (sah->state == SADB_SASTATE_DEAD) {
4373 /* if LARVAL entry doesn't become MATURE, delete it. */
4374 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_LARVAL], chain, nextsav) {
4375 /* Need to also check refcnt for a larval SA ??? */
4376 if (now - sav->created > V_key_larval_lifetime)
4381 * check MATURE entry to start to send expire message
4384 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_MATURE], chain, nextsav) {
4385 /* we don't need to check. */
4386 if (sav->lft_s == NULL)
4390 if (sav->lft_c == NULL) {
4391 ipseclog((LOG_DEBUG,"%s: there is no CURRENT "
4392 "time, why?\n", __func__));
4396 /* check SOFT lifetime */
4397 if (sav->lft_s->addtime != 0 &&
4398 now - sav->created > sav->lft_s->addtime) {
4399 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4401 * Actually, only send expire message if
4402 * SA has been used, as it was done before,
4403 * but should we always send such message,
4404 * and let IKE daemon decide if it should be
4405 * renegotiated or not ?
4406 * XXX expire message will actually NOT be
4407 * sent if SA is only used after soft
4408 * lifetime has been reached, see below
4411 if (sav->lft_c->usetime != 0)
4414 /* check SOFT lifetime by bytes */
4416 * XXX I don't know the way to delete this SA
4417 * when new SA is installed. Caution when it's
4418 * installed too big lifetime by time.
4420 else if (sav->lft_s->bytes != 0 &&
4421 sav->lft_s->bytes < sav->lft_c->bytes) {
4423 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4425 * XXX If we keep to send expire
4426 * message in the status of
4427 * DYING. Do remove below code.
4433 /* check DYING entry to change status to DEAD. */
4434 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DYING], chain, nextsav) {
4435 /* we don't need to check. */
4436 if (sav->lft_h == NULL)
4440 if (sav->lft_c == NULL) {
4441 ipseclog((LOG_DEBUG, "%s: there is no CURRENT "
4442 "time, why?\n", __func__));
4446 if (sav->lft_h->addtime != 0 &&
4447 now - sav->created > sav->lft_h->addtime) {
4448 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4451 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4452 else if (sav->lft_s != NULL
4453 && sav->lft_s->addtime != 0
4454 && now - sav->created > sav->lft_s->addtime) {
4456 * XXX: should be checked to be
4457 * installed the valid SA.
4461 * If there is no SA then sending
4467 /* check HARD lifetime by bytes */
4468 else if (sav->lft_h->bytes != 0 &&
4469 sav->lft_h->bytes < sav->lft_c->bytes) {
4470 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4475 /* delete entry in DEAD */
4476 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DEAD], chain, nextsav) {
4478 if (sav->state != SADB_SASTATE_DEAD) {
4479 ipseclog((LOG_DEBUG, "%s: invalid sav->state "
4480 "(queue: %d SA: %d): kill it anyway\n",
4482 SADB_SASTATE_DEAD, sav->state));
4485 * do not call key_freesav() here.
4486 * sav should already be freed, and sav->refcnt
4487 * shows other references to sav
4488 * (such as from SPD).
4496 key_flush_acq(time_t now)
4498 struct secacq *acq, *nextacq;
4502 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
4503 nextacq = LIST_NEXT(acq, chain);
4504 if (now - acq->created > V_key_blockacq_lifetime
4505 && __LIST_CHAINED(acq)) {
4506 LIST_REMOVE(acq, chain);
4507 free(acq, M_IPSEC_SAQ);
4514 key_flush_spacq(time_t now)
4516 struct secspacq *acq, *nextacq;
4520 for (acq = LIST_FIRST(&V_spacqtree); acq != NULL; acq = nextacq) {
4521 nextacq = LIST_NEXT(acq, chain);
4522 if (now - acq->created > V_key_blockacq_lifetime
4523 && __LIST_CHAINED(acq)) {
4524 LIST_REMOVE(acq, chain);
4525 free(acq, M_IPSEC_SAQ);
4533 * scanning SPD and SAD to check status for each entries,
4534 * and do to remove or to expire.
4535 * XXX: year 2038 problem may remain.
4538 key_timehandler(void)
4540 VNET_ITERATOR_DECL(vnet_iter);
4541 time_t now = time_second;
4543 VNET_LIST_RLOCK_NOSLEEP();
4544 VNET_FOREACH(vnet_iter) {
4545 CURVNET_SET(vnet_iter);
4549 key_flush_spacq(now);
4552 VNET_LIST_RUNLOCK_NOSLEEP();
4554 #ifndef IPSEC_DEBUG2
4555 /* do exchange to tick time !! */
4556 (void)timeout((void *)key_timehandler, (void *)0, hz);
4557 #endif /* IPSEC_DEBUG2 */
4565 key_randomfill(&value, sizeof(value));
4570 key_randomfill(p, l)
4576 static int warn = 1;
4579 n = (size_t)read_random(p, (u_int)l);
4583 bcopy(&v, (u_int8_t *)p + n,
4584 l - n < sizeof(v) ? l - n : sizeof(v));
4588 printf("WARNING: pseudo-random number generator "
4589 "used for IPsec processing\n");
4596 * map SADB_SATYPE_* to IPPROTO_*.
4597 * if satype == SADB_SATYPE then satype is mapped to ~0.
4599 * 0: invalid satype.
4602 key_satype2proto(u_int8_t satype)
4605 case SADB_SATYPE_UNSPEC:
4606 return IPSEC_PROTO_ANY;
4607 case SADB_SATYPE_AH:
4609 case SADB_SATYPE_ESP:
4611 case SADB_X_SATYPE_IPCOMP:
4612 return IPPROTO_IPCOMP;
4613 case SADB_X_SATYPE_TCPSIGNATURE:
4622 * map IPPROTO_* to SADB_SATYPE_*
4624 * 0: invalid protocol type.
4627 key_proto2satype(u_int16_t proto)
4631 return SADB_SATYPE_AH;
4633 return SADB_SATYPE_ESP;
4634 case IPPROTO_IPCOMP:
4635 return SADB_X_SATYPE_IPCOMP;
4637 return SADB_X_SATYPE_TCPSIGNATURE;
4646 * SADB_GETSPI processing is to receive
4647 * <base, (SA2), src address, dst address, (SPI range)>
4648 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4649 * tree with the status of LARVAL, and send
4650 * <base, SA(*), address(SD)>
4653 * IN: mhp: pointer to the pointer to each header.
4654 * OUT: NULL if fail.
4655 * other if success, return pointer to the message to send.
4658 key_getspi(so, m, mhp)
4661 const struct sadb_msghdr *mhp;
4663 struct sadb_address *src0, *dst0;
4664 struct secasindex saidx;
4665 struct secashead *newsah;
4666 struct secasvar *newsav;
4673 IPSEC_ASSERT(so != NULL, ("null socket"));
4674 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4675 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4676 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4678 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4679 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4680 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4682 return key_senderror(so, m, EINVAL);
4684 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4685 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4686 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4688 return key_senderror(so, m, EINVAL);
4690 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4691 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4692 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4694 mode = IPSEC_MODE_ANY;
4698 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4699 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4701 /* map satype to proto */
4702 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4703 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4705 return key_senderror(so, m, EINVAL);
4709 * Make sure the port numbers are zero.
4710 * In case of NAT-T we will update them later if needed.
4712 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4714 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4715 sizeof(struct sockaddr_in))
4716 return key_senderror(so, m, EINVAL);
4717 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4720 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4721 sizeof(struct sockaddr_in6))
4722 return key_senderror(so, m, EINVAL);
4723 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4728 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4730 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4731 sizeof(struct sockaddr_in))
4732 return key_senderror(so, m, EINVAL);
4733 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4736 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4737 sizeof(struct sockaddr_in6))
4738 return key_senderror(so, m, EINVAL);
4739 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4745 /* XXX boundary check against sa_len */
4746 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4750 * Handle NAT-T info if present.
4751 * We made sure the port numbers are zero above, so we do
4752 * not have to worry in case we do not update them.
4754 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL)
4755 ipseclog((LOG_DEBUG, "%s: NAT-T OAi present\n", __func__));
4756 if (mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL)
4757 ipseclog((LOG_DEBUG, "%s: NAT-T OAr present\n", __func__));
4759 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
4760 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
4761 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
4762 struct sadb_x_nat_t_type *type;
4763 struct sadb_x_nat_t_port *sport, *dport;
4765 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
4766 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
4767 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
4768 ipseclog((LOG_DEBUG, "%s: invalid nat-t message "
4769 "passed.\n", __func__));
4770 return key_senderror(so, m, EINVAL);
4773 sport = (struct sadb_x_nat_t_port *)
4774 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
4775 dport = (struct sadb_x_nat_t_port *)
4776 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
4779 KEY_PORTTOSADDR(&saidx.src, sport->sadb_x_nat_t_port_port);
4781 KEY_PORTTOSADDR(&saidx.dst, dport->sadb_x_nat_t_port_port);
4785 /* SPI allocation */
4786 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4789 return key_senderror(so, m, EINVAL);
4791 /* get a SA index */
4792 if ((newsah = key_getsah(&saidx)) == NULL) {
4793 /* create a new SA index */
4794 if ((newsah = key_newsah(&saidx)) == NULL) {
4795 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
4796 return key_senderror(so, m, ENOBUFS);
4802 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4803 if (newsav == NULL) {
4804 /* XXX don't free new SA index allocated in above. */
4805 return key_senderror(so, m, error);
4809 newsav->spi = htonl(spi);
4811 /* delete the entry in acqtree */
4812 if (mhp->msg->sadb_msg_seq != 0) {
4814 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4815 /* reset counter in order to deletion by timehandler. */
4816 acq->created = time_second;
4822 struct mbuf *n, *nn;
4823 struct sadb_sa *m_sa;
4824 struct sadb_msg *newmsg;
4827 /* create new sadb_msg to reply. */
4828 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4829 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4831 MGETHDR(n, M_DONTWAIT, MT_DATA);
4833 MCLGET(n, M_DONTWAIT);
4834 if ((n->m_flags & M_EXT) == 0) {
4840 return key_senderror(so, m, ENOBUFS);
4846 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4847 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4849 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4850 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4851 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4852 m_sa->sadb_sa_spi = htonl(spi);
4853 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4855 IPSEC_ASSERT(off == len,
4856 ("length inconsistency (off %u len %u)", off, len));
4858 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4859 SADB_EXT_ADDRESS_DST);
4862 return key_senderror(so, m, ENOBUFS);
4865 if (n->m_len < sizeof(struct sadb_msg)) {
4866 n = m_pullup(n, sizeof(struct sadb_msg));
4868 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4871 n->m_pkthdr.len = 0;
4872 for (nn = n; nn; nn = nn->m_next)
4873 n->m_pkthdr.len += nn->m_len;
4875 newmsg = mtod(n, struct sadb_msg *);
4876 newmsg->sadb_msg_seq = newsav->seq;
4877 newmsg->sadb_msg_errno = 0;
4878 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4881 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4886 * allocating new SPI
4887 * called by key_getspi().
4893 key_do_getnewspi(spirange, saidx)
4894 struct sadb_spirange *spirange;
4895 struct secasindex *saidx;
4899 int count = V_key_spi_trycnt;
4901 /* set spi range to allocate */
4902 if (spirange != NULL) {
4903 min = spirange->sadb_spirange_min;
4904 max = spirange->sadb_spirange_max;
4906 min = V_key_spi_minval;
4907 max = V_key_spi_maxval;
4909 /* IPCOMP needs 2-byte SPI */
4910 if (saidx->proto == IPPROTO_IPCOMP) {
4917 t = min; min = max; max = t;
4922 if (key_checkspidup(saidx, min) != NULL) {
4923 ipseclog((LOG_DEBUG, "%s: SPI %u exists already.\n",
4928 count--; /* taking one cost. */
4936 /* when requesting to allocate spi ranged */
4938 /* generate pseudo-random SPI value ranged. */
4939 newspi = min + (key_random() % (max - min + 1));
4941 if (key_checkspidup(saidx, newspi) == NULL)
4945 if (count == 0 || newspi == 0) {
4946 ipseclog((LOG_DEBUG, "%s: to allocate spi is failed.\n",
4953 keystat.getspi_count =
4954 (keystat.getspi_count + V_key_spi_trycnt - count) / 2;
4960 * SADB_UPDATE processing
4962 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4963 * key(AE), (identity(SD),) (sensitivity)>
4964 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4966 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4967 * (identity(SD),) (sensitivity)>
4970 * m will always be freed.
4973 key_update(so, m, mhp)
4976 const struct sadb_msghdr *mhp;
4978 struct sadb_sa *sa0;
4979 struct sadb_address *src0, *dst0;
4981 struct sadb_x_nat_t_type *type;
4982 struct sadb_x_nat_t_port *sport, *dport;
4983 struct sadb_address *iaddr, *raddr;
4984 struct sadb_x_nat_t_frag *frag;
4986 struct secasindex saidx;
4987 struct secashead *sah;
4988 struct secasvar *sav;
4994 IPSEC_ASSERT(so != NULL, ("null socket"));
4995 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4996 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4997 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4999 /* map satype to proto */
5000 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5001 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5003 return key_senderror(so, m, EINVAL);
5006 if (mhp->ext[SADB_EXT_SA] == NULL ||
5007 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5008 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5009 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5010 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5011 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5012 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5013 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5014 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5015 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5016 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5017 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5019 return key_senderror(so, m, EINVAL);
5021 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5022 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5023 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5024 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5026 return key_senderror(so, m, EINVAL);
5028 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5029 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5030 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5032 mode = IPSEC_MODE_ANY;
5035 /* XXX boundary checking for other extensions */
5037 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5038 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5039 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5041 /* XXX boundary check against sa_len */
5042 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5045 * Make sure the port numbers are zero.
5046 * In case of NAT-T we will update them later if needed.
5048 KEY_PORTTOSADDR(&saidx.src, 0);
5049 KEY_PORTTOSADDR(&saidx.dst, 0);
5053 * Handle NAT-T info if present.
5055 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
5056 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5057 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5059 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
5060 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5061 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5062 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5064 return key_senderror(so, m, EINVAL);
5067 type = (struct sadb_x_nat_t_type *)
5068 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5069 sport = (struct sadb_x_nat_t_port *)
5070 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5071 dport = (struct sadb_x_nat_t_port *)
5072 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5077 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
5078 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
5079 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
5080 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
5081 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5083 return key_senderror(so, m, EINVAL);
5085 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5086 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5087 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
5089 iaddr = raddr = NULL;
5091 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5092 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5093 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5095 return key_senderror(so, m, EINVAL);
5097 frag = (struct sadb_x_nat_t_frag *)
5098 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5104 /* get a SA header */
5105 if ((sah = key_getsah(&saidx)) == NULL) {
5106 ipseclog((LOG_DEBUG, "%s: no SA index found.\n", __func__));
5107 return key_senderror(so, m, ENOENT);
5110 /* set spidx if there */
5112 error = key_setident(sah, m, mhp);
5114 return key_senderror(so, m, error);
5116 /* find a SA with sequence number. */
5117 #ifdef IPSEC_DOSEQCHECK
5118 if (mhp->msg->sadb_msg_seq != 0
5119 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
5120 ipseclog((LOG_DEBUG, "%s: no larval SA with sequence %u "
5121 "exists.\n", __func__, mhp->msg->sadb_msg_seq));
5122 return key_senderror(so, m, ENOENT);
5126 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5129 ipseclog((LOG_DEBUG, "%s: no such a SA found (spi:%u)\n",
5130 __func__, (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5131 return key_senderror(so, m, EINVAL);
5135 /* validity check */
5136 if (sav->sah->saidx.proto != proto) {
5137 ipseclog((LOG_DEBUG, "%s: protocol mismatched "
5138 "(DB=%u param=%u)\n", __func__,
5139 sav->sah->saidx.proto, proto));
5140 return key_senderror(so, m, EINVAL);
5142 #ifdef IPSEC_DOSEQCHECK
5143 if (sav->spi != sa0->sadb_sa_spi) {
5144 ipseclog((LOG_DEBUG, "%s: SPI mismatched (DB:%u param:%u)\n",
5146 (u_int32_t)ntohl(sav->spi),
5147 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5148 return key_senderror(so, m, EINVAL);
5151 if (sav->pid != mhp->msg->sadb_msg_pid) {
5152 ipseclog((LOG_DEBUG, "%s: pid mismatched (DB:%u param:%u)\n",
5153 __func__, sav->pid, mhp->msg->sadb_msg_pid));
5154 return key_senderror(so, m, EINVAL);
5157 /* copy sav values */
5158 error = key_setsaval(sav, m, mhp);
5161 return key_senderror(so, m, error);
5166 * Handle more NAT-T info if present,
5167 * now that we have a sav to fill.
5170 sav->natt_type = type->sadb_x_nat_t_type_type;
5173 KEY_PORTTOSADDR(&sav->sah->saidx.src,
5174 sport->sadb_x_nat_t_port_port);
5176 KEY_PORTTOSADDR(&sav->sah->saidx.dst,
5177 dport->sadb_x_nat_t_port_port);
5181 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5182 * We should actually check for a minimum MTU here, if we
5183 * want to support it in ip_output.
5186 sav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5190 /* check SA values to be mature. */
5191 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
5193 return key_senderror(so, m, 0);
5199 /* set msg buf from mhp */
5200 n = key_getmsgbuf_x1(m, mhp);
5202 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5203 return key_senderror(so, m, ENOBUFS);
5207 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5212 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
5213 * only called by key_update().
5216 * others : found, pointer to a SA.
5218 #ifdef IPSEC_DOSEQCHECK
5219 static struct secasvar *
5220 key_getsavbyseq(sah, seq)
5221 struct secashead *sah;
5224 struct secasvar *sav;
5227 state = SADB_SASTATE_LARVAL;
5229 /* search SAD with sequence number ? */
5230 LIST_FOREACH(sav, &sah->savtree[state], chain) {
5232 KEY_CHKSASTATE(state, sav->state, __func__);
5234 if (sav->seq == seq) {
5236 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
5237 printf("DP %s cause refcnt++:%d SA:%p\n",
5238 __func__, sav->refcnt, sav));
5248 * SADB_ADD processing
5249 * add an entry to SA database, when received
5250 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5251 * key(AE), (identity(SD),) (sensitivity)>
5254 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5255 * (identity(SD),) (sensitivity)>
5258 * IGNORE identity and sensitivity messages.
5260 * m will always be freed.
5266 const struct sadb_msghdr *mhp;
5268 struct sadb_sa *sa0;
5269 struct sadb_address *src0, *dst0;
5271 struct sadb_x_nat_t_type *type;
5272 struct sadb_address *iaddr, *raddr;
5273 struct sadb_x_nat_t_frag *frag;
5275 struct secasindex saidx;
5276 struct secashead *newsah;
5277 struct secasvar *newsav;
5283 IPSEC_ASSERT(so != NULL, ("null socket"));
5284 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5285 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5286 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5288 /* map satype to proto */
5289 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5290 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5292 return key_senderror(so, m, EINVAL);
5295 if (mhp->ext[SADB_EXT_SA] == NULL ||
5296 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5297 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5298 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5299 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5300 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5301 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5302 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5303 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5304 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5305 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5306 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5308 return key_senderror(so, m, EINVAL);
5310 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5311 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5312 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5314 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5316 return key_senderror(so, m, EINVAL);
5318 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5319 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5320 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5322 mode = IPSEC_MODE_ANY;
5326 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5327 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5328 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5330 /* XXX boundary check against sa_len */
5331 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5334 * Make sure the port numbers are zero.
5335 * In case of NAT-T we will update them later if needed.
5337 KEY_PORTTOSADDR(&saidx.src, 0);
5338 KEY_PORTTOSADDR(&saidx.dst, 0);
5342 * Handle NAT-T info if present.
5344 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
5345 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5346 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5347 struct sadb_x_nat_t_port *sport, *dport;
5349 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
5350 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5351 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5352 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5354 return key_senderror(so, m, EINVAL);
5357 type = (struct sadb_x_nat_t_type *)
5358 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5359 sport = (struct sadb_x_nat_t_port *)
5360 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5361 dport = (struct sadb_x_nat_t_port *)
5362 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5365 KEY_PORTTOSADDR(&saidx.src,
5366 sport->sadb_x_nat_t_port_port);
5368 KEY_PORTTOSADDR(&saidx.dst,
5369 dport->sadb_x_nat_t_port_port);
5373 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
5374 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
5375 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
5376 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
5377 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5379 return key_senderror(so, m, EINVAL);
5381 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5382 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5383 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
5385 iaddr = raddr = NULL;
5387 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5388 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5389 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5391 return key_senderror(so, m, EINVAL);
5393 frag = (struct sadb_x_nat_t_frag *)
5394 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5400 /* get a SA header */
5401 if ((newsah = key_getsah(&saidx)) == NULL) {
5402 /* create a new SA header */
5403 if ((newsah = key_newsah(&saidx)) == NULL) {
5404 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
5405 return key_senderror(so, m, ENOBUFS);
5409 /* set spidx if there */
5411 error = key_setident(newsah, m, mhp);
5413 return key_senderror(so, m, error);
5416 /* create new SA entry. */
5417 /* We can create new SA only if SPI is differenct. */
5419 newsav = key_getsavbyspi(newsah, sa0->sadb_sa_spi);
5421 if (newsav != NULL) {
5422 ipseclog((LOG_DEBUG, "%s: SA already exists.\n", __func__));
5423 return key_senderror(so, m, EEXIST);
5425 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
5426 if (newsav == NULL) {
5427 return key_senderror(so, m, error);
5432 * Handle more NAT-T info if present,
5433 * now that we have a sav to fill.
5436 newsav->natt_type = type->sadb_x_nat_t_type_type;
5440 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5441 * We should actually check for a minimum MTU here, if we
5442 * want to support it in ip_output.
5445 newsav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5449 /* check SA values to be mature. */
5450 if ((error = key_mature(newsav)) != 0) {
5451 KEY_FREESAV(&newsav);
5452 return key_senderror(so, m, error);
5456 * don't call key_freesav() here, as we would like to keep the SA
5457 * in the database on success.
5463 /* set msg buf from mhp */
5464 n = key_getmsgbuf_x1(m, mhp);
5466 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5467 return key_senderror(so, m, ENOBUFS);
5471 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5477 key_setident(sah, m, mhp)
5478 struct secashead *sah;
5480 const struct sadb_msghdr *mhp;
5482 const struct sadb_ident *idsrc, *iddst;
5483 int idsrclen, iddstlen;
5485 IPSEC_ASSERT(sah != NULL, ("null secashead"));
5486 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5487 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5488 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5490 /* don't make buffer if not there */
5491 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
5492 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5498 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
5499 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5500 ipseclog((LOG_DEBUG, "%s: invalid identity.\n", __func__));
5504 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
5505 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
5506 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
5507 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
5509 /* validity check */
5510 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
5511 ipseclog((LOG_DEBUG, "%s: ident type mismatch.\n", __func__));
5515 switch (idsrc->sadb_ident_type) {
5516 case SADB_IDENTTYPE_PREFIX:
5517 case SADB_IDENTTYPE_FQDN:
5518 case SADB_IDENTTYPE_USERFQDN:
5520 /* XXX do nothing */
5526 /* make structure */
5527 sah->idents = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5528 if (sah->idents == NULL) {
5529 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5532 sah->identd = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5533 if (sah->identd == NULL) {
5534 free(sah->idents, M_IPSEC_MISC);
5536 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5539 sah->idents->type = idsrc->sadb_ident_type;
5540 sah->idents->id = idsrc->sadb_ident_id;
5542 sah->identd->type = iddst->sadb_ident_type;
5543 sah->identd->id = iddst->sadb_ident_id;
5549 * m will not be freed on return.
5550 * it is caller's responsibility to free the result.
5552 static struct mbuf *
5553 key_getmsgbuf_x1(m, mhp)
5555 const struct sadb_msghdr *mhp;
5559 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5560 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5561 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5563 /* create new sadb_msg to reply. */
5564 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5565 SADB_EXT_SA, SADB_X_EXT_SA2,
5566 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5567 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5568 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5572 if (n->m_len < sizeof(struct sadb_msg)) {
5573 n = m_pullup(n, sizeof(struct sadb_msg));
5577 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5578 mtod(n, struct sadb_msg *)->sadb_msg_len =
5579 PFKEY_UNIT64(n->m_pkthdr.len);
5584 static int key_delete_all __P((struct socket *, struct mbuf *,
5585 const struct sadb_msghdr *, u_int16_t));
5588 * SADB_DELETE processing
5590 * <base, SA(*), address(SD)>
5591 * from the ikmpd, and set SADB_SASTATE_DEAD,
5593 * <base, SA(*), address(SD)>
5596 * m will always be freed.
5599 key_delete(so, m, mhp)
5602 const struct sadb_msghdr *mhp;
5604 struct sadb_sa *sa0;
5605 struct sadb_address *src0, *dst0;
5606 struct secasindex saidx;
5607 struct secashead *sah;
5608 struct secasvar *sav = NULL;
5611 IPSEC_ASSERT(so != NULL, ("null socket"));
5612 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5613 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5614 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5616 /* map satype to proto */
5617 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5618 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5620 return key_senderror(so, m, EINVAL);
5623 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5624 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5625 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5627 return key_senderror(so, m, EINVAL);
5630 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5631 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5632 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5634 return key_senderror(so, m, EINVAL);
5637 if (mhp->ext[SADB_EXT_SA] == NULL) {
5639 * Caller wants us to delete all non-LARVAL SAs
5640 * that match the src/dst. This is used during
5641 * IKE INITIAL-CONTACT.
5643 ipseclog((LOG_DEBUG, "%s: doing delete all.\n", __func__));
5644 return key_delete_all(so, m, mhp, proto);
5645 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5646 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5648 return key_senderror(so, m, EINVAL);
5651 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5652 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5653 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5655 /* XXX boundary check against sa_len */
5656 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5659 * Make sure the port numbers are zero.
5660 * In case of NAT-T we will update them later if needed.
5662 KEY_PORTTOSADDR(&saidx.src, 0);
5663 KEY_PORTTOSADDR(&saidx.dst, 0);
5667 * Handle NAT-T info if present.
5669 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5670 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5671 struct sadb_x_nat_t_port *sport, *dport;
5673 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5674 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5675 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5677 return key_senderror(so, m, EINVAL);
5680 sport = (struct sadb_x_nat_t_port *)
5681 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5682 dport = (struct sadb_x_nat_t_port *)
5683 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5686 KEY_PORTTOSADDR(&saidx.src,
5687 sport->sadb_x_nat_t_port_port);
5689 KEY_PORTTOSADDR(&saidx.dst,
5690 dport->sadb_x_nat_t_port_port);
5694 /* get a SA header */
5696 LIST_FOREACH(sah, &V_sahtree, chain) {
5697 if (sah->state == SADB_SASTATE_DEAD)
5699 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5702 /* get a SA with SPI. */
5703 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5709 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5710 return key_senderror(so, m, ENOENT);
5713 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5719 struct sadb_msg *newmsg;
5721 /* create new sadb_msg to reply. */
5722 /* XXX-BZ NAT-T extensions? */
5723 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5724 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5726 return key_senderror(so, m, ENOBUFS);
5728 if (n->m_len < sizeof(struct sadb_msg)) {
5729 n = m_pullup(n, sizeof(struct sadb_msg));
5731 return key_senderror(so, m, ENOBUFS);
5733 newmsg = mtod(n, struct sadb_msg *);
5734 newmsg->sadb_msg_errno = 0;
5735 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5738 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5743 * delete all SAs for src/dst. Called from key_delete().
5746 key_delete_all(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp,
5749 struct sadb_address *src0, *dst0;
5750 struct secasindex saidx;
5751 struct secashead *sah;
5752 struct secasvar *sav, *nextsav;
5753 u_int stateidx, state;
5755 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5756 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5758 /* XXX boundary check against sa_len */
5759 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5762 * Make sure the port numbers are zero.
5763 * In case of NAT-T we will update them later if needed.
5765 KEY_PORTTOSADDR(&saidx.src, 0);
5766 KEY_PORTTOSADDR(&saidx.dst, 0);
5770 * Handle NAT-T info if present.
5773 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5774 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5775 struct sadb_x_nat_t_port *sport, *dport;
5777 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5778 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5779 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5781 return key_senderror(so, m, EINVAL);
5784 sport = (struct sadb_x_nat_t_port *)
5785 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5786 dport = (struct sadb_x_nat_t_port *)
5787 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5790 KEY_PORTTOSADDR(&saidx.src,
5791 sport->sadb_x_nat_t_port_port);
5793 KEY_PORTTOSADDR(&saidx.dst,
5794 dport->sadb_x_nat_t_port_port);
5799 LIST_FOREACH(sah, &V_sahtree, chain) {
5800 if (sah->state == SADB_SASTATE_DEAD)
5802 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5805 /* Delete all non-LARVAL SAs. */
5807 stateidx < _ARRAYLEN(saorder_state_alive);
5809 state = saorder_state_alive[stateidx];
5810 if (state == SADB_SASTATE_LARVAL)
5812 for (sav = LIST_FIRST(&sah->savtree[state]);
5813 sav != NULL; sav = nextsav) {
5814 nextsav = LIST_NEXT(sav, chain);
5816 if (sav->state != state) {
5817 ipseclog((LOG_DEBUG, "%s: invalid "
5818 "sav->state (queue %d SA %d)\n",
5819 __func__, state, sav->state));
5823 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5831 struct sadb_msg *newmsg;
5833 /* create new sadb_msg to reply. */
5834 /* XXX-BZ NAT-T extensions? */
5835 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5836 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5838 return key_senderror(so, m, ENOBUFS);
5840 if (n->m_len < sizeof(struct sadb_msg)) {
5841 n = m_pullup(n, sizeof(struct sadb_msg));
5843 return key_senderror(so, m, ENOBUFS);
5845 newmsg = mtod(n, struct sadb_msg *);
5846 newmsg->sadb_msg_errno = 0;
5847 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5850 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5855 * SADB_GET processing
5857 * <base, SA(*), address(SD)>
5858 * from the ikmpd, and get a SP and a SA to respond,
5860 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5861 * (identity(SD),) (sensitivity)>
5864 * m will always be freed.
5870 const struct sadb_msghdr *mhp;
5872 struct sadb_sa *sa0;
5873 struct sadb_address *src0, *dst0;
5874 struct secasindex saidx;
5875 struct secashead *sah;
5876 struct secasvar *sav = NULL;
5879 IPSEC_ASSERT(so != NULL, ("null socket"));
5880 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5881 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5882 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5884 /* map satype to proto */
5885 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5886 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5888 return key_senderror(so, m, EINVAL);
5891 if (mhp->ext[SADB_EXT_SA] == NULL ||
5892 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5893 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5894 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5896 return key_senderror(so, m, EINVAL);
5898 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5899 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5900 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5901 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5903 return key_senderror(so, m, EINVAL);
5906 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5907 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5908 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5910 /* XXX boundary check against sa_len */
5911 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5914 * Make sure the port numbers are zero.
5915 * In case of NAT-T we will update them later if needed.
5917 KEY_PORTTOSADDR(&saidx.src, 0);
5918 KEY_PORTTOSADDR(&saidx.dst, 0);
5922 * Handle NAT-T info if present.
5925 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5926 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5927 struct sadb_x_nat_t_port *sport, *dport;
5929 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5930 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5931 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5933 return key_senderror(so, m, EINVAL);
5936 sport = (struct sadb_x_nat_t_port *)
5937 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5938 dport = (struct sadb_x_nat_t_port *)
5939 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5942 KEY_PORTTOSADDR(&saidx.src,
5943 sport->sadb_x_nat_t_port_port);
5945 KEY_PORTTOSADDR(&saidx.dst,
5946 dport->sadb_x_nat_t_port_port);
5950 /* get a SA header */
5952 LIST_FOREACH(sah, &V_sahtree, chain) {
5953 if (sah->state == SADB_SASTATE_DEAD)
5955 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5958 /* get a SA with SPI. */
5959 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5965 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5966 return key_senderror(so, m, ENOENT);
5973 /* map proto to satype */
5974 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5975 ipseclog((LOG_DEBUG, "%s: there was invalid proto in SAD.\n",
5977 return key_senderror(so, m, EINVAL);
5980 /* create new sadb_msg to reply. */
5981 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5982 mhp->msg->sadb_msg_pid);
5984 return key_senderror(so, m, ENOBUFS);
5987 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5991 /* XXX make it sysctl-configurable? */
5993 key_getcomb_setlifetime(comb)
5994 struct sadb_comb *comb;
5997 comb->sadb_comb_soft_allocations = 1;
5998 comb->sadb_comb_hard_allocations = 1;
5999 comb->sadb_comb_soft_bytes = 0;
6000 comb->sadb_comb_hard_bytes = 0;
6001 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
6002 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
6003 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
6004 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
6008 * XXX reorder combinations by preference
6009 * XXX no idea if the user wants ESP authentication or not
6011 static struct mbuf *
6014 struct sadb_comb *comb;
6015 struct enc_xform *algo;
6016 struct mbuf *result = NULL, *m, *n;
6020 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6023 for (i = 1; i <= SADB_EALG_MAX; i++) {
6024 algo = esp_algorithm_lookup(i);
6028 /* discard algorithms with key size smaller than system min */
6029 if (_BITS(algo->maxkey) < V_ipsec_esp_keymin)
6031 if (_BITS(algo->minkey) < V_ipsec_esp_keymin)
6032 encmin = V_ipsec_esp_keymin;
6034 encmin = _BITS(algo->minkey);
6036 if (V_ipsec_esp_auth)
6037 m = key_getcomb_ah();
6039 IPSEC_ASSERT(l <= MLEN,
6040 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6041 MGET(m, M_DONTWAIT, MT_DATA);
6046 bzero(mtod(m, caddr_t), m->m_len);
6053 for (n = m; n; n = n->m_next)
6055 IPSEC_ASSERT((totlen % l) == 0, ("totlen=%u, l=%u", totlen, l));
6057 for (off = 0; off < totlen; off += l) {
6058 n = m_pulldown(m, off, l, &o);
6060 /* m is already freed */
6063 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
6064 bzero(comb, sizeof(*comb));
6065 key_getcomb_setlifetime(comb);
6066 comb->sadb_comb_encrypt = i;
6067 comb->sadb_comb_encrypt_minbits = encmin;
6068 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
6087 const struct auth_hash *ah,
6093 *min = *max = ah->keysize;
6094 if (ah->keysize == 0) {
6096 * Transform takes arbitrary key size but algorithm
6097 * key size is restricted. Enforce this here.
6100 case SADB_X_AALG_MD5: *min = *max = 16; break;
6101 case SADB_X_AALG_SHA: *min = *max = 20; break;
6102 case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
6103 case SADB_X_AALG_SHA2_256: *min = *max = 32; break;
6104 case SADB_X_AALG_SHA2_384: *min = *max = 48; break;
6105 case SADB_X_AALG_SHA2_512: *min = *max = 64; break;
6107 DPRINTF(("%s: unknown AH algorithm %u\n",
6115 * XXX reorder combinations by preference
6117 static struct mbuf *
6120 struct sadb_comb *comb;
6121 struct auth_hash *algo;
6123 u_int16_t minkeysize, maxkeysize;
6125 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6128 for (i = 1; i <= SADB_AALG_MAX; i++) {
6130 /* we prefer HMAC algorithms, not old algorithms */
6131 if (i != SADB_AALG_SHA1HMAC &&
6132 i != SADB_AALG_MD5HMAC &&
6133 i != SADB_X_AALG_SHA2_256 &&
6134 i != SADB_X_AALG_SHA2_384 &&
6135 i != SADB_X_AALG_SHA2_512)
6138 algo = ah_algorithm_lookup(i);
6141 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
6142 /* discard algorithms with key size smaller than system min */
6143 if (_BITS(minkeysize) < V_ipsec_ah_keymin)
6147 IPSEC_ASSERT(l <= MLEN,
6148 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6149 MGET(m, M_DONTWAIT, MT_DATA);
6156 M_PREPEND(m, l, M_DONTWAIT);
6160 comb = mtod(m, struct sadb_comb *);
6161 bzero(comb, sizeof(*comb));
6162 key_getcomb_setlifetime(comb);
6163 comb->sadb_comb_auth = i;
6164 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
6165 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
6172 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
6173 * XXX reorder combinations by preference
6175 static struct mbuf *
6176 key_getcomb_ipcomp()
6178 struct sadb_comb *comb;
6179 struct comp_algo *algo;
6182 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6185 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
6186 algo = ipcomp_algorithm_lookup(i);
6191 IPSEC_ASSERT(l <= MLEN,
6192 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6193 MGET(m, M_DONTWAIT, MT_DATA);
6200 M_PREPEND(m, l, M_DONTWAIT);
6204 comb = mtod(m, struct sadb_comb *);
6205 bzero(comb, sizeof(*comb));
6206 key_getcomb_setlifetime(comb);
6207 comb->sadb_comb_encrypt = i;
6208 /* what should we set into sadb_comb_*_{min,max}bits? */
6215 * XXX no way to pass mode (transport/tunnel) to userland
6216 * XXX replay checking?
6217 * XXX sysctl interface to ipsec_{ah,esp}_keymin
6219 static struct mbuf *
6221 const struct secasindex *saidx;
6223 struct sadb_prop *prop;
6225 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
6228 switch (saidx->proto) {
6230 m = key_getcomb_esp();
6233 m = key_getcomb_ah();
6235 case IPPROTO_IPCOMP:
6236 m = key_getcomb_ipcomp();
6244 M_PREPEND(m, l, M_DONTWAIT);
6249 for (n = m; n; n = n->m_next)
6252 prop = mtod(m, struct sadb_prop *);
6253 bzero(prop, sizeof(*prop));
6254 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
6255 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
6256 prop->sadb_prop_replay = 32; /* XXX */
6262 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
6264 * <base, SA, address(SD), (address(P)), x_policy,
6265 * (identity(SD),) (sensitivity,) proposal>
6266 * to KMD, and expect to receive
6267 * <base> with SADB_ACQUIRE if error occured,
6269 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
6270 * from KMD by PF_KEY.
6272 * XXX x_policy is outside of RFC2367 (KAME extension).
6273 * XXX sensitivity is not supported.
6274 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
6275 * see comment for key_getcomb_ipcomp().
6279 * others: error number
6282 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
6284 struct mbuf *result = NULL, *m;
6285 struct secacq *newacq;
6290 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
6291 satype = key_proto2satype(saidx->proto);
6292 IPSEC_ASSERT(satype != 0, ("null satype, protocol %u", saidx->proto));
6295 * We never do anything about acquirng SA. There is anather
6296 * solution that kernel blocks to send SADB_ACQUIRE message until
6297 * getting something message from IKEd. In later case, to be
6298 * managed with ACQUIRING list.
6300 /* Get an entry to check whether sending message or not. */
6301 if ((newacq = key_getacq(saidx)) != NULL) {
6302 if (V_key_blockacq_count < newacq->count) {
6303 /* reset counter and do send message. */
6306 /* increment counter and do nothing. */
6311 /* make new entry for blocking to send SADB_ACQUIRE. */
6312 if ((newacq = key_newacq(saidx)) == NULL)
6318 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
6326 * No SADB_X_EXT_NAT_T_* here: we do not know
6327 * anything related to NAT-T at this time.
6330 /* set sadb_address for saidx's. */
6331 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6332 &saidx->src.sa, FULLMASK, IPSEC_ULPROTO_ANY);
6339 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6340 &saidx->dst.sa, FULLMASK, IPSEC_ULPROTO_ANY);
6347 /* XXX proxy address (optional) */
6349 /* set sadb_x_policy */
6351 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
6359 /* XXX identity (optional) */
6361 if (idexttype && fqdn) {
6362 /* create identity extension (FQDN) */
6363 struct sadb_ident *id;
6366 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
6367 id = (struct sadb_ident *)p;
6368 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6369 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6370 id->sadb_ident_exttype = idexttype;
6371 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
6372 bcopy(fqdn, id + 1, fqdnlen);
6373 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
6377 /* create identity extension (USERFQDN) */
6378 struct sadb_ident *id;
6382 /* +1 for terminating-NUL */
6383 userfqdnlen = strlen(userfqdn) + 1;
6386 id = (struct sadb_ident *)p;
6387 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6388 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6389 id->sadb_ident_exttype = idexttype;
6390 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
6391 /* XXX is it correct? */
6392 if (curproc && curproc->p_cred)
6393 id->sadb_ident_id = curproc->p_cred->p_ruid;
6394 if (userfqdn && userfqdnlen)
6395 bcopy(userfqdn, id + 1, userfqdnlen);
6396 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
6400 /* XXX sensitivity (optional) */
6402 /* create proposal/combination extension */
6403 m = key_getprop(saidx);
6406 * spec conformant: always attach proposal/combination extension,
6407 * the problem is that we have no way to attach it for ipcomp,
6408 * due to the way sadb_comb is declared in RFC2367.
6417 * outside of spec; make proposal/combination extension optional.
6423 if ((result->m_flags & M_PKTHDR) == 0) {
6428 if (result->m_len < sizeof(struct sadb_msg)) {
6429 result = m_pullup(result, sizeof(struct sadb_msg));
6430 if (result == NULL) {
6436 result->m_pkthdr.len = 0;
6437 for (m = result; m; m = m->m_next)
6438 result->m_pkthdr.len += m->m_len;
6440 mtod(result, struct sadb_msg *)->sadb_msg_len =
6441 PFKEY_UNIT64(result->m_pkthdr.len);
6443 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6451 static struct secacq *
6452 key_newacq(const struct secasindex *saidx)
6454 struct secacq *newacq;
6457 newacq = malloc(sizeof(struct secacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6458 if (newacq == NULL) {
6459 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6464 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
6465 newacq->seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq);
6466 newacq->created = time_second;
6469 /* add to acqtree */
6471 LIST_INSERT_HEAD(&V_acqtree, newacq, chain);
6477 static struct secacq *
6478 key_getacq(const struct secasindex *saidx)
6483 LIST_FOREACH(acq, &V_acqtree, chain) {
6484 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
6492 static struct secacq *
6493 key_getacqbyseq(seq)
6499 LIST_FOREACH(acq, &V_acqtree, chain) {
6500 if (acq->seq == seq)
6508 static struct secspacq *
6510 struct secpolicyindex *spidx;
6512 struct secspacq *acq;
6515 acq = malloc(sizeof(struct secspacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6517 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6522 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
6523 acq->created = time_second;
6526 /* add to spacqtree */
6528 LIST_INSERT_HEAD(&V_spacqtree, acq, chain);
6534 static struct secspacq *
6536 struct secpolicyindex *spidx;
6538 struct secspacq *acq;
6541 LIST_FOREACH(acq, &V_spacqtree, chain) {
6542 if (key_cmpspidx_exactly(spidx, &acq->spidx)) {
6543 /* NB: return holding spacq_lock */
6553 * SADB_ACQUIRE processing,
6554 * in first situation, is receiving
6556 * from the ikmpd, and clear sequence of its secasvar entry.
6558 * In second situation, is receiving
6559 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6560 * from a user land process, and return
6561 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6564 * m will always be freed.
6567 key_acquire2(so, m, mhp)
6570 const struct sadb_msghdr *mhp;
6572 const struct sadb_address *src0, *dst0;
6573 struct secasindex saidx;
6574 struct secashead *sah;
6578 IPSEC_ASSERT(so != NULL, ("null socket"));
6579 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6580 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6581 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6584 * Error message from KMd.
6585 * We assume that if error was occured in IKEd, the length of PFKEY
6586 * message is equal to the size of sadb_msg structure.
6587 * We do not raise error even if error occured in this function.
6589 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
6592 /* check sequence number */
6593 if (mhp->msg->sadb_msg_seq == 0) {
6594 ipseclog((LOG_DEBUG, "%s: must specify sequence "
6595 "number.\n", __func__));
6600 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
6602 * the specified larval SA is already gone, or we got
6603 * a bogus sequence number. we can silently ignore it.
6609 /* reset acq counter in order to deletion by timehander. */
6610 acq->created = time_second;
6617 * This message is from user land.
6620 /* map satype to proto */
6621 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6622 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6624 return key_senderror(so, m, EINVAL);
6627 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
6628 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
6629 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
6631 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6633 return key_senderror(so, m, EINVAL);
6635 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
6636 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
6637 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
6639 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6641 return key_senderror(so, m, EINVAL);
6644 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
6645 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
6647 /* XXX boundary check against sa_len */
6648 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
6651 * Make sure the port numbers are zero.
6652 * In case of NAT-T we will update them later if needed.
6654 KEY_PORTTOSADDR(&saidx.src, 0);
6655 KEY_PORTTOSADDR(&saidx.dst, 0);
6659 * Handle NAT-T info if present.
6662 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
6663 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
6664 struct sadb_x_nat_t_port *sport, *dport;
6666 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
6667 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
6668 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
6670 return key_senderror(so, m, EINVAL);
6673 sport = (struct sadb_x_nat_t_port *)
6674 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
6675 dport = (struct sadb_x_nat_t_port *)
6676 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
6679 KEY_PORTTOSADDR(&saidx.src,
6680 sport->sadb_x_nat_t_port_port);
6682 KEY_PORTTOSADDR(&saidx.dst,
6683 dport->sadb_x_nat_t_port_port);
6687 /* get a SA index */
6689 LIST_FOREACH(sah, &V_sahtree, chain) {
6690 if (sah->state == SADB_SASTATE_DEAD)
6692 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
6697 ipseclog((LOG_DEBUG, "%s: a SA exists already.\n", __func__));
6698 return key_senderror(so, m, EEXIST);
6701 error = key_acquire(&saidx, NULL);
6703 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
6704 __func__, mhp->msg->sadb_msg_errno));
6705 return key_senderror(so, m, error);
6708 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6712 * SADB_REGISTER processing.
6713 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6716 * from the ikmpd, and register a socket to send PF_KEY messages,
6720 * If socket is detached, must free from regnode.
6722 * m will always be freed.
6725 key_register(so, m, mhp)
6728 const struct sadb_msghdr *mhp;
6730 struct secreg *reg, *newreg = 0;
6732 IPSEC_ASSERT(so != NULL, ("null socket"));
6733 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6734 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6735 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6737 /* check for invalid register message */
6738 if (mhp->msg->sadb_msg_satype >= sizeof(V_regtree)/sizeof(V_regtree[0]))
6739 return key_senderror(so, m, EINVAL);
6741 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6742 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6745 /* check whether existing or not */
6747 LIST_FOREACH(reg, &V_regtree[mhp->msg->sadb_msg_satype], chain) {
6748 if (reg->so == so) {
6750 ipseclog((LOG_DEBUG, "%s: socket exists already.\n",
6752 return key_senderror(so, m, EEXIST);
6756 /* create regnode */
6757 newreg = malloc(sizeof(struct secreg), M_IPSEC_SAR, M_NOWAIT|M_ZERO);
6758 if (newreg == NULL) {
6760 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6761 return key_senderror(so, m, ENOBUFS);
6765 ((struct keycb *)sotorawcb(so))->kp_registered++;
6767 /* add regnode to regtree. */
6768 LIST_INSERT_HEAD(&V_regtree[mhp->msg->sadb_msg_satype], newreg, chain);
6774 struct sadb_msg *newmsg;
6775 struct sadb_supported *sup;
6776 u_int len, alen, elen;
6779 struct sadb_alg *alg;
6781 /* create new sadb_msg to reply. */
6783 for (i = 1; i <= SADB_AALG_MAX; i++) {
6784 if (ah_algorithm_lookup(i))
6785 alen += sizeof(struct sadb_alg);
6788 alen += sizeof(struct sadb_supported);
6790 for (i = 1; i <= SADB_EALG_MAX; i++) {
6791 if (esp_algorithm_lookup(i))
6792 elen += sizeof(struct sadb_alg);
6795 elen += sizeof(struct sadb_supported);
6797 len = sizeof(struct sadb_msg) + alen + elen;
6800 return key_senderror(so, m, ENOBUFS);
6802 MGETHDR(n, M_DONTWAIT, MT_DATA);
6804 MCLGET(n, M_DONTWAIT);
6805 if ((n->m_flags & M_EXT) == 0) {
6811 return key_senderror(so, m, ENOBUFS);
6813 n->m_pkthdr.len = n->m_len = len;
6817 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6818 newmsg = mtod(n, struct sadb_msg *);
6819 newmsg->sadb_msg_errno = 0;
6820 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6821 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6823 /* for authentication algorithm */
6825 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6826 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6827 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6828 off += PFKEY_ALIGN8(sizeof(*sup));
6830 for (i = 1; i <= SADB_AALG_MAX; i++) {
6831 struct auth_hash *aalgo;
6832 u_int16_t minkeysize, maxkeysize;
6834 aalgo = ah_algorithm_lookup(i);
6837 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6838 alg->sadb_alg_id = i;
6839 alg->sadb_alg_ivlen = 0;
6840 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
6841 alg->sadb_alg_minbits = _BITS(minkeysize);
6842 alg->sadb_alg_maxbits = _BITS(maxkeysize);
6843 off += PFKEY_ALIGN8(sizeof(*alg));
6847 /* for encryption algorithm */
6849 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6850 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6851 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6852 off += PFKEY_ALIGN8(sizeof(*sup));
6854 for (i = 1; i <= SADB_EALG_MAX; i++) {
6855 struct enc_xform *ealgo;
6857 ealgo = esp_algorithm_lookup(i);
6860 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6861 alg->sadb_alg_id = i;
6862 alg->sadb_alg_ivlen = ealgo->blocksize;
6863 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
6864 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
6865 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6869 IPSEC_ASSERT(off == len,
6870 ("length assumption failed (off %u len %u)", off, len));
6873 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6878 * free secreg entry registered.
6879 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6882 key_freereg(struct socket *so)
6887 IPSEC_ASSERT(so != NULL, ("NULL so"));
6890 * check whether existing or not.
6891 * check all type of SA, because there is a potential that
6892 * one socket is registered to multiple type of SA.
6895 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6896 LIST_FOREACH(reg, &V_regtree[i], chain) {
6897 if (reg->so == so && __LIST_CHAINED(reg)) {
6898 LIST_REMOVE(reg, chain);
6899 free(reg, M_IPSEC_SAR);
6908 * SADB_EXPIRE processing
6910 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6912 * NOTE: We send only soft lifetime extension.
6915 * others : error number
6918 key_expire(struct secasvar *sav)
6922 struct mbuf *result = NULL, *m;
6925 struct sadb_lifetime *lt;
6927 /* XXX: Why do we lock ? */
6928 s = splnet(); /*called from softclock()*/
6930 IPSEC_ASSERT (sav != NULL, ("null sav"));
6931 IPSEC_ASSERT (sav->sah != NULL, ("null sa header"));
6933 /* set msg header */
6934 satype = key_proto2satype(sav->sah->saidx.proto);
6935 IPSEC_ASSERT(satype != 0, ("invalid proto, satype %u", satype));
6936 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6943 /* create SA extension */
6944 m = key_setsadbsa(sav);
6951 /* create SA extension */
6952 m = key_setsadbxsa2(sav->sah->saidx.mode,
6953 sav->replay ? sav->replay->count : 0,
6954 sav->sah->saidx.reqid);
6961 /* create lifetime extension (current and soft) */
6962 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6963 m = key_alloc_mbuf(len);
6964 if (!m || m->m_next) { /*XXX*/
6970 bzero(mtod(m, caddr_t), len);
6971 lt = mtod(m, struct sadb_lifetime *);
6972 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6973 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6974 lt->sadb_lifetime_allocations = sav->lft_c->allocations;
6975 lt->sadb_lifetime_bytes = sav->lft_c->bytes;
6976 lt->sadb_lifetime_addtime = sav->lft_c->addtime;
6977 lt->sadb_lifetime_usetime = sav->lft_c->usetime;
6978 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6979 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6980 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
6981 lt->sadb_lifetime_allocations = sav->lft_s->allocations;
6982 lt->sadb_lifetime_bytes = sav->lft_s->bytes;
6983 lt->sadb_lifetime_addtime = sav->lft_s->addtime;
6984 lt->sadb_lifetime_usetime = sav->lft_s->usetime;
6987 /* set sadb_address for source */
6988 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6989 &sav->sah->saidx.src.sa,
6990 FULLMASK, IPSEC_ULPROTO_ANY);
6997 /* set sadb_address for destination */
6998 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6999 &sav->sah->saidx.dst.sa,
7000 FULLMASK, IPSEC_ULPROTO_ANY);
7008 * XXX-BZ Handle NAT-T extensions here.
7011 if ((result->m_flags & M_PKTHDR) == 0) {
7016 if (result->m_len < sizeof(struct sadb_msg)) {
7017 result = m_pullup(result, sizeof(struct sadb_msg));
7018 if (result == NULL) {
7024 result->m_pkthdr.len = 0;
7025 for (m = result; m; m = m->m_next)
7026 result->m_pkthdr.len += m->m_len;
7028 mtod(result, struct sadb_msg *)->sadb_msg_len =
7029 PFKEY_UNIT64(result->m_pkthdr.len);
7032 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
7042 * SADB_FLUSH processing
7045 * from the ikmpd, and free all entries in secastree.
7049 * NOTE: to do is only marking SADB_SASTATE_DEAD.
7051 * m will always be freed.
7054 key_flush(so, m, mhp)
7057 const struct sadb_msghdr *mhp;
7059 struct sadb_msg *newmsg;
7060 struct secashead *sah, *nextsah;
7061 struct secasvar *sav, *nextsav;
7066 IPSEC_ASSERT(so != NULL, ("null socket"));
7067 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7068 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7070 /* map satype to proto */
7071 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7072 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7074 return key_senderror(so, m, EINVAL);
7077 /* no SATYPE specified, i.e. flushing all SA. */
7079 for (sah = LIST_FIRST(&V_sahtree);
7082 nextsah = LIST_NEXT(sah, chain);
7084 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7085 && proto != sah->saidx.proto)
7089 stateidx < _ARRAYLEN(saorder_state_alive);
7091 state = saorder_state_any[stateidx];
7092 for (sav = LIST_FIRST(&sah->savtree[state]);
7096 nextsav = LIST_NEXT(sav, chain);
7098 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
7103 sah->state = SADB_SASTATE_DEAD;
7107 if (m->m_len < sizeof(struct sadb_msg) ||
7108 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
7109 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
7110 return key_senderror(so, m, ENOBUFS);
7116 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
7117 newmsg = mtod(m, struct sadb_msg *);
7118 newmsg->sadb_msg_errno = 0;
7119 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
7121 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7125 * SADB_DUMP processing
7126 * dump all entries including status of DEAD in SAD.
7129 * from the ikmpd, and dump all secasvar leaves
7134 * m will always be freed.
7137 key_dump(so, m, mhp)
7140 const struct sadb_msghdr *mhp;
7142 struct secashead *sah;
7143 struct secasvar *sav;
7149 struct sadb_msg *newmsg;
7152 IPSEC_ASSERT(so != NULL, ("null socket"));
7153 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7154 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7155 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7157 /* map satype to proto */
7158 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7159 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7161 return key_senderror(so, m, EINVAL);
7164 /* count sav entries to be sent to the userland. */
7167 LIST_FOREACH(sah, &V_sahtree, chain) {
7168 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7169 && proto != sah->saidx.proto)
7173 stateidx < _ARRAYLEN(saorder_state_any);
7175 state = saorder_state_any[stateidx];
7176 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7184 return key_senderror(so, m, ENOENT);
7187 /* send this to the userland, one at a time. */
7189 LIST_FOREACH(sah, &V_sahtree, chain) {
7190 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7191 && proto != sah->saidx.proto)
7194 /* map proto to satype */
7195 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
7197 ipseclog((LOG_DEBUG, "%s: there was invalid proto in "
7198 "SAD.\n", __func__));
7199 return key_senderror(so, m, EINVAL);
7203 stateidx < _ARRAYLEN(saorder_state_any);
7205 state = saorder_state_any[stateidx];
7206 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7207 n = key_setdumpsa(sav, SADB_DUMP, satype,
7208 --cnt, mhp->msg->sadb_msg_pid);
7211 return key_senderror(so, m, ENOBUFS);
7213 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
7224 * SADB_X_PROMISC processing
7226 * m will always be freed.
7229 key_promisc(so, m, mhp)
7232 const struct sadb_msghdr *mhp;
7236 IPSEC_ASSERT(so != NULL, ("null socket"));
7237 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7238 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7239 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7241 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7243 if (olen < sizeof(struct sadb_msg)) {
7245 return key_senderror(so, m, EINVAL);
7250 } else if (olen == sizeof(struct sadb_msg)) {
7251 /* enable/disable promisc mode */
7254 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
7255 return key_senderror(so, m, EINVAL);
7256 mhp->msg->sadb_msg_errno = 0;
7257 switch (mhp->msg->sadb_msg_satype) {
7260 kp->kp_promisc = mhp->msg->sadb_msg_satype;
7263 return key_senderror(so, m, EINVAL);
7266 /* send the original message back to everyone */
7267 mhp->msg->sadb_msg_errno = 0;
7268 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7270 /* send packet as is */
7272 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
7274 /* TODO: if sadb_msg_seq is specified, send to specific pid */
7275 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7279 static int (*key_typesw[]) __P((struct socket *, struct mbuf *,
7280 const struct sadb_msghdr *)) = {
7281 NULL, /* SADB_RESERVED */
7282 key_getspi, /* SADB_GETSPI */
7283 key_update, /* SADB_UPDATE */
7284 key_add, /* SADB_ADD */
7285 key_delete, /* SADB_DELETE */
7286 key_get, /* SADB_GET */
7287 key_acquire2, /* SADB_ACQUIRE */
7288 key_register, /* SADB_REGISTER */
7289 NULL, /* SADB_EXPIRE */
7290 key_flush, /* SADB_FLUSH */
7291 key_dump, /* SADB_DUMP */
7292 key_promisc, /* SADB_X_PROMISC */
7293 NULL, /* SADB_X_PCHANGE */
7294 key_spdadd, /* SADB_X_SPDUPDATE */
7295 key_spdadd, /* SADB_X_SPDADD */
7296 key_spddelete, /* SADB_X_SPDDELETE */
7297 key_spdget, /* SADB_X_SPDGET */
7298 NULL, /* SADB_X_SPDACQUIRE */
7299 key_spddump, /* SADB_X_SPDDUMP */
7300 key_spdflush, /* SADB_X_SPDFLUSH */
7301 key_spdadd, /* SADB_X_SPDSETIDX */
7302 NULL, /* SADB_X_SPDEXPIRE */
7303 key_spddelete2, /* SADB_X_SPDDELETE2 */
7307 * parse sadb_msg buffer to process PFKEYv2,
7308 * and create a data to response if needed.
7309 * I think to be dealed with mbuf directly.
7311 * msgp : pointer to pointer to a received buffer pulluped.
7312 * This is rewrited to response.
7313 * so : pointer to socket.
7315 * length for buffer to send to user process.
7322 struct sadb_msg *msg;
7323 struct sadb_msghdr mh;
7328 IPSEC_ASSERT(so != NULL, ("null socket"));
7329 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7331 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
7332 KEYDEBUG(KEYDEBUG_KEY_DUMP,
7333 ipseclog((LOG_DEBUG, "%s: passed sadb_msg\n", __func__));
7337 if (m->m_len < sizeof(struct sadb_msg)) {
7338 m = m_pullup(m, sizeof(struct sadb_msg));
7342 msg = mtod(m, struct sadb_msg *);
7343 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
7344 target = KEY_SENDUP_ONE;
7346 if ((m->m_flags & M_PKTHDR) == 0 ||
7347 m->m_pkthdr.len != m->m_pkthdr.len) {
7348 ipseclog((LOG_DEBUG, "%s: invalid message length.\n",__func__));
7349 V_pfkeystat.out_invlen++;
7354 if (msg->sadb_msg_version != PF_KEY_V2) {
7355 ipseclog((LOG_DEBUG, "%s: PF_KEY version %u is mismatched.\n",
7356 __func__, msg->sadb_msg_version));
7357 V_pfkeystat.out_invver++;
7362 if (msg->sadb_msg_type > SADB_MAX) {
7363 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7364 __func__, msg->sadb_msg_type));
7365 V_pfkeystat.out_invmsgtype++;
7370 /* for old-fashioned code - should be nuked */
7371 if (m->m_pkthdr.len > MCLBYTES) {
7378 MGETHDR(n, M_DONTWAIT, MT_DATA);
7379 if (n && m->m_pkthdr.len > MHLEN) {
7380 MCLGET(n, M_DONTWAIT);
7381 if ((n->m_flags & M_EXT) == 0) {
7390 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
7391 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
7397 /* align the mbuf chain so that extensions are in contiguous region. */
7398 error = key_align(m, &mh);
7405 switch (msg->sadb_msg_satype) {
7406 case SADB_SATYPE_UNSPEC:
7407 switch (msg->sadb_msg_type) {
7415 ipseclog((LOG_DEBUG, "%s: must specify satype "
7416 "when msg type=%u.\n", __func__,
7417 msg->sadb_msg_type));
7418 V_pfkeystat.out_invsatype++;
7423 case SADB_SATYPE_AH:
7424 case SADB_SATYPE_ESP:
7425 case SADB_X_SATYPE_IPCOMP:
7426 case SADB_X_SATYPE_TCPSIGNATURE:
7427 switch (msg->sadb_msg_type) {
7429 case SADB_X_SPDDELETE:
7431 case SADB_X_SPDDUMP:
7432 case SADB_X_SPDFLUSH:
7433 case SADB_X_SPDSETIDX:
7434 case SADB_X_SPDUPDATE:
7435 case SADB_X_SPDDELETE2:
7436 ipseclog((LOG_DEBUG, "%s: illegal satype=%u\n",
7437 __func__, msg->sadb_msg_type));
7438 V_pfkeystat.out_invsatype++;
7443 case SADB_SATYPE_RSVP:
7444 case SADB_SATYPE_OSPFV2:
7445 case SADB_SATYPE_RIPV2:
7446 case SADB_SATYPE_MIP:
7447 ipseclog((LOG_DEBUG, "%s: type %u isn't supported.\n",
7448 __func__, msg->sadb_msg_satype));
7449 V_pfkeystat.out_invsatype++;
7452 case 1: /* XXX: What does it do? */
7453 if (msg->sadb_msg_type == SADB_X_PROMISC)
7457 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7458 __func__, msg->sadb_msg_satype));
7459 V_pfkeystat.out_invsatype++;
7464 /* check field of upper layer protocol and address family */
7465 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
7466 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
7467 struct sadb_address *src0, *dst0;
7470 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
7471 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
7473 /* check upper layer protocol */
7474 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
7475 ipseclog((LOG_DEBUG, "%s: upper layer protocol "
7476 "mismatched.\n", __func__));
7477 V_pfkeystat.out_invaddr++;
7483 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
7484 PFKEY_ADDR_SADDR(dst0)->sa_family) {
7485 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
7487 V_pfkeystat.out_invaddr++;
7491 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7492 PFKEY_ADDR_SADDR(dst0)->sa_len) {
7493 ipseclog((LOG_DEBUG, "%s: address struct size "
7494 "mismatched.\n", __func__));
7495 V_pfkeystat.out_invaddr++;
7500 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7502 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7503 sizeof(struct sockaddr_in)) {
7504 V_pfkeystat.out_invaddr++;
7510 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7511 sizeof(struct sockaddr_in6)) {
7512 V_pfkeystat.out_invaddr++;
7518 ipseclog((LOG_DEBUG, "%s: unsupported address family\n",
7520 V_pfkeystat.out_invaddr++;
7521 error = EAFNOSUPPORT;
7525 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7527 plen = sizeof(struct in_addr) << 3;
7530 plen = sizeof(struct in6_addr) << 3;
7533 plen = 0; /*fool gcc*/
7537 /* check max prefix length */
7538 if (src0->sadb_address_prefixlen > plen ||
7539 dst0->sadb_address_prefixlen > plen) {
7540 ipseclog((LOG_DEBUG, "%s: illegal prefixlen.\n",
7542 V_pfkeystat.out_invaddr++;
7548 * prefixlen == 0 is valid because there can be a case when
7549 * all addresses are matched.
7553 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
7554 key_typesw[msg->sadb_msg_type] == NULL) {
7555 V_pfkeystat.out_invmsgtype++;
7560 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
7563 msg->sadb_msg_errno = error;
7564 return key_sendup_mbuf(so, m, target);
7568 key_senderror(so, m, code)
7573 struct sadb_msg *msg;
7575 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7576 ("mbuf too small, len %u", m->m_len));
7578 msg = mtod(m, struct sadb_msg *);
7579 msg->sadb_msg_errno = code;
7580 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
7584 * set the pointer to each header into message buffer.
7585 * m will be freed on error.
7586 * XXX larger-than-MCLBYTES extension?
7591 struct sadb_msghdr *mhp;
7594 struct sadb_ext *ext;
7599 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7600 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7601 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7602 ("mbuf too small, len %u", m->m_len));
7605 bzero(mhp, sizeof(*mhp));
7607 mhp->msg = mtod(m, struct sadb_msg *);
7608 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
7610 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7611 extlen = end; /*just in case extlen is not updated*/
7612 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
7613 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
7615 /* m is already freed */
7618 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7621 switch (ext->sadb_ext_type) {
7623 case SADB_EXT_ADDRESS_SRC:
7624 case SADB_EXT_ADDRESS_DST:
7625 case SADB_EXT_ADDRESS_PROXY:
7626 case SADB_EXT_LIFETIME_CURRENT:
7627 case SADB_EXT_LIFETIME_HARD:
7628 case SADB_EXT_LIFETIME_SOFT:
7629 case SADB_EXT_KEY_AUTH:
7630 case SADB_EXT_KEY_ENCRYPT:
7631 case SADB_EXT_IDENTITY_SRC:
7632 case SADB_EXT_IDENTITY_DST:
7633 case SADB_EXT_SENSITIVITY:
7634 case SADB_EXT_PROPOSAL:
7635 case SADB_EXT_SUPPORTED_AUTH:
7636 case SADB_EXT_SUPPORTED_ENCRYPT:
7637 case SADB_EXT_SPIRANGE:
7638 case SADB_X_EXT_POLICY:
7639 case SADB_X_EXT_SA2:
7641 case SADB_X_EXT_NAT_T_TYPE:
7642 case SADB_X_EXT_NAT_T_SPORT:
7643 case SADB_X_EXT_NAT_T_DPORT:
7644 case SADB_X_EXT_NAT_T_OAI:
7645 case SADB_X_EXT_NAT_T_OAR:
7646 case SADB_X_EXT_NAT_T_FRAG:
7648 /* duplicate check */
7650 * XXX Are there duplication payloads of either
7651 * KEY_AUTH or KEY_ENCRYPT ?
7653 if (mhp->ext[ext->sadb_ext_type] != NULL) {
7654 ipseclog((LOG_DEBUG, "%s: duplicate ext_type "
7655 "%u\n", __func__, ext->sadb_ext_type));
7657 V_pfkeystat.out_dupext++;
7662 ipseclog((LOG_DEBUG, "%s: invalid ext_type %u\n",
7663 __func__, ext->sadb_ext_type));
7665 V_pfkeystat.out_invexttype++;
7669 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
7671 if (key_validate_ext(ext, extlen)) {
7673 V_pfkeystat.out_invlen++;
7677 n = m_pulldown(m, off, extlen, &toff);
7679 /* m is already freed */
7682 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7684 mhp->ext[ext->sadb_ext_type] = ext;
7685 mhp->extoff[ext->sadb_ext_type] = off;
7686 mhp->extlen[ext->sadb_ext_type] = extlen;
7691 V_pfkeystat.out_invlen++;
7699 key_validate_ext(ext, len)
7700 const struct sadb_ext *ext;
7703 const struct sockaddr *sa;
7704 enum { NONE, ADDR } checktype = NONE;
7706 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
7708 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
7711 /* if it does not match minimum/maximum length, bail */
7712 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
7713 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
7715 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
7717 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
7720 /* more checks based on sadb_ext_type XXX need more */
7721 switch (ext->sadb_ext_type) {
7722 case SADB_EXT_ADDRESS_SRC:
7723 case SADB_EXT_ADDRESS_DST:
7724 case SADB_EXT_ADDRESS_PROXY:
7725 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7728 case SADB_EXT_IDENTITY_SRC:
7729 case SADB_EXT_IDENTITY_DST:
7730 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7731 SADB_X_IDENTTYPE_ADDR) {
7732 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7742 switch (checktype) {
7746 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
7747 if (len < baselen + sal)
7749 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7762 for (i = 0; i < IPSEC_DIR_MAX; i++)
7763 LIST_INIT(&V_sptree[i]);
7765 LIST_INIT(&V_sahtree);
7767 for (i = 0; i <= SADB_SATYPE_MAX; i++)
7768 LIST_INIT(&V_regtree[i]);
7770 LIST_INIT(&V_acqtree);
7771 LIST_INIT(&V_spacqtree);
7773 /* system default */
7774 V_ip4_def_policy.policy = IPSEC_POLICY_NONE;
7775 V_ip4_def_policy.refcnt++; /*never reclaim this*/
7777 if (!IS_DEFAULT_VNET(curvnet))
7781 REGTREE_LOCK_INIT();
7782 SAHTREE_LOCK_INIT();
7786 #ifndef IPSEC_DEBUG2
7787 timeout((void *)key_timehandler, (void *)0, hz);
7788 #endif /*IPSEC_DEBUG2*/
7790 /* initialize key statistics */
7791 keystat.getspi_count = 1;
7793 printf("IPsec: Initialized Security Association Processing.\n");
7800 struct secpolicy *sp, *nextsp;
7801 struct secacq *acq, *nextacq;
7802 struct secspacq *spacq, *nextspacq;
7803 struct secashead *sah, *nextsah;
7808 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7809 for (sp = LIST_FIRST(&V_sptree[i]);
7810 sp != NULL; sp = nextsp) {
7811 nextsp = LIST_NEXT(sp, chain);
7812 if (__LIST_CHAINED(sp)) {
7813 LIST_REMOVE(sp, chain);
7814 free(sp, M_IPSEC_SP);
7821 for (sah = LIST_FIRST(&V_sahtree); sah != NULL; sah = nextsah) {
7822 nextsah = LIST_NEXT(sah, chain);
7823 if (__LIST_CHAINED(sah)) {
7824 LIST_REMOVE(sah, chain);
7825 free(sah, M_IPSEC_SAH);
7831 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7832 LIST_FOREACH(reg, &V_regtree[i], chain) {
7833 if (__LIST_CHAINED(reg)) {
7834 LIST_REMOVE(reg, chain);
7835 free(reg, M_IPSEC_SAR);
7843 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
7844 nextacq = LIST_NEXT(acq, chain);
7845 if (__LIST_CHAINED(acq)) {
7846 LIST_REMOVE(acq, chain);
7847 free(acq, M_IPSEC_SAQ);
7853 for (spacq = LIST_FIRST(&V_spacqtree); spacq != NULL;
7854 spacq = nextspacq) {
7855 nextspacq = LIST_NEXT(spacq, chain);
7856 if (__LIST_CHAINED(spacq)) {
7857 LIST_REMOVE(spacq, chain);
7858 free(spacq, M_IPSEC_SAQ);
7866 * XXX: maybe This function is called after INBOUND IPsec processing.
7868 * Special check for tunnel-mode packets.
7869 * We must make some checks for consistency between inner and outer IP header.
7871 * xxx more checks to be provided
7874 key_checktunnelsanity(sav, family, src, dst)
7875 struct secasvar *sav;
7880 IPSEC_ASSERT(sav->sah != NULL, ("null SA header"));
7882 /* XXX: check inner IP header */
7887 /* record data transfer on SA, and update timestamps */
7889 key_sa_recordxfer(sav, m)
7890 struct secasvar *sav;
7893 IPSEC_ASSERT(sav != NULL, ("Null secasvar"));
7894 IPSEC_ASSERT(m != NULL, ("Null mbuf"));
7899 * XXX Currently, there is a difference of bytes size
7900 * between inbound and outbound processing.
7902 sav->lft_c->bytes += m->m_pkthdr.len;
7903 /* to check bytes lifetime is done in key_timehandler(). */
7906 * We use the number of packets as the unit of
7907 * allocations. We increment the variable
7908 * whenever {esp,ah}_{in,out}put is called.
7910 sav->lft_c->allocations++;
7911 /* XXX check for expires? */
7914 * NOTE: We record CURRENT usetime by using wall clock,
7915 * in seconds. HARD and SOFT lifetime are measured by the time
7916 * difference (again in seconds) from usetime.
7920 * -----+-----+--------+---> t
7921 * <--------------> HARD
7924 sav->lft_c->usetime = time_second;
7925 /* XXX check for expires? */
7932 key_sa_routechange(dst)
7933 struct sockaddr *dst;
7935 struct secashead *sah;
7939 LIST_FOREACH(sah, &V_sahtree, chain) {
7940 ro = &sah->route_cache.sa_route;
7941 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7942 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7944 ro->ro_rt = (struct rtentry *)NULL;
7951 key_sa_chgstate(struct secasvar *sav, u_int8_t state)
7953 IPSEC_ASSERT(sav != NULL, ("NULL sav"));
7954 SAHTREE_LOCK_ASSERT();
7956 if (sav->state != state) {
7957 if (__LIST_CHAINED(sav))
7958 LIST_REMOVE(sav, chain);
7960 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7966 struct secasvar *sav;
7969 IPSEC_ASSERT(sav->iv != NULL, ("null IV"));
7970 key_randomfill(sav->iv, sav->ivlen);
7974 static struct mbuf *
7978 struct mbuf *m = NULL, *n;
7983 MGET(n, M_DONTWAIT, MT_DATA);
7984 if (n && len > MLEN)
7985 MCLGET(n, M_DONTWAIT);
7993 n->m_len = M_TRAILINGSPACE(n);
7994 /* use the bottom of mbuf, hoping we can prepend afterwards */
7995 if (n->m_len > len) {
7996 t = (n->m_len - len) & ~(sizeof(long) - 1);
8013 * Take one of the kernel's security keys and convert it into a PF_KEY
8014 * structure within an mbuf, suitable for sending up to a waiting
8015 * application in user land.
8018 * src: A pointer to a kernel security key.
8019 * exttype: Which type of key this is. Refer to the PF_KEY data structures.
8021 * a valid mbuf or NULL indicating an error
8025 static struct mbuf *
8026 key_setkey(struct seckey *src, u_int16_t exttype)
8035 len = PFKEY_ALIGN8(sizeof(struct sadb_key) + _KEYLEN(src));
8036 m = key_alloc_mbuf(len);
8039 p = mtod(m, struct sadb_key *);
8041 p->sadb_key_len = PFKEY_UNIT64(len);
8042 p->sadb_key_exttype = exttype;
8043 p->sadb_key_bits = src->bits;
8044 bcopy(src->key_data, _KEYBUF(p), _KEYLEN(src));
8050 * Take one of the kernel's lifetime data structures and convert it
8051 * into a PF_KEY structure within an mbuf, suitable for sending up to
8052 * a waiting application in user land.
8055 * src: A pointer to a kernel lifetime structure.
8056 * exttype: Which type of lifetime this is. Refer to the PF_KEY
8057 * data structures for more information.
8059 * a valid mbuf or NULL indicating an error
8063 static struct mbuf *
8064 key_setlifetime(struct seclifetime *src, u_int16_t exttype)
8066 struct mbuf *m = NULL;
8067 struct sadb_lifetime *p;
8068 int len = PFKEY_ALIGN8(sizeof(struct sadb_lifetime));
8073 m = key_alloc_mbuf(len);
8076 p = mtod(m, struct sadb_lifetime *);
8079 p->sadb_lifetime_len = PFKEY_UNIT64(len);
8080 p->sadb_lifetime_exttype = exttype;
8081 p->sadb_lifetime_allocations = src->allocations;
8082 p->sadb_lifetime_bytes = src->bytes;
8083 p->sadb_lifetime_addtime = src->addtime;
8084 p->sadb_lifetime_usetime = src->usetime;