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>
76 #if defined(INET) || defined(INET6)
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;
897 state_valid = NULL; /* silence gcc */
898 arraysize = 0; /* silence gcc */
901 LIST_FOREACH(sah, &V_sahtree, chain) {
902 if (sah->state == SADB_SASTATE_DEAD)
904 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID)) {
905 if (V_key_preferred_oldsa) {
906 state_valid = saorder_state_valid_prefer_old;
907 arraysize = N(saorder_state_valid_prefer_old);
909 state_valid = saorder_state_valid_prefer_new;
910 arraysize = N(saorder_state_valid_prefer_new);
919 /* search valid state */
920 for (stateidx = 0; stateidx < arraysize; stateidx++) {
921 sav = key_do_allocsa_policy(sah, state_valid[stateidx]);
931 * searching SAD with direction, protocol, mode and state.
932 * called by key_allocsa_policy().
935 * others : found, pointer to a SA.
937 static struct secasvar *
938 key_do_allocsa_policy(struct secashead *sah, u_int state)
940 struct secasvar *sav, *nextsav, *candidate, *d;
946 for (sav = LIST_FIRST(&sah->savtree[state]);
950 nextsav = LIST_NEXT(sav, chain);
953 KEY_CHKSASTATE(sav->state, state, __func__);
956 if (candidate == NULL) {
961 /* Which SA is the better ? */
963 IPSEC_ASSERT(candidate->lft_c != NULL,
964 ("null candidate lifetime"));
965 IPSEC_ASSERT(sav->lft_c != NULL, ("null sav lifetime"));
967 /* What the best method is to compare ? */
968 if (V_key_preferred_oldsa) {
969 if (candidate->lft_c->addtime >
970 sav->lft_c->addtime) {
977 /* preferred new sa rather than old sa */
978 if (candidate->lft_c->addtime <
979 sav->lft_c->addtime) {
986 * prepared to delete the SA when there is more
987 * suitable candidate and the lifetime of the SA is not
990 if (d->lft_h->addtime != 0) {
991 struct mbuf *m, *result;
994 key_sa_chgstate(d, SADB_SASTATE_DEAD);
996 IPSEC_ASSERT(d->refcnt > 0, ("bogus ref count"));
998 satype = key_proto2satype(d->sah->saidx.proto);
1002 m = key_setsadbmsg(SADB_DELETE, 0,
1003 satype, 0, 0, d->refcnt - 1);
1008 /* set sadb_address for saidx's. */
1009 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
1010 &d->sah->saidx.src.sa,
1011 d->sah->saidx.src.sa.sa_len << 3,
1017 /* set sadb_address for saidx's. */
1018 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
1019 &d->sah->saidx.dst.sa,
1020 d->sah->saidx.dst.sa.sa_len << 3,
1026 /* create SA extension */
1027 m = key_setsadbsa(d);
1032 if (result->m_len < sizeof(struct sadb_msg)) {
1033 result = m_pullup(result,
1034 sizeof(struct sadb_msg));
1039 result->m_pkthdr.len = 0;
1040 for (m = result; m; m = m->m_next)
1041 result->m_pkthdr.len += m->m_len;
1042 mtod(result, struct sadb_msg *)->sadb_msg_len =
1043 PFKEY_UNIT64(result->m_pkthdr.len);
1045 if (key_sendup_mbuf(NULL, result,
1046 KEY_SENDUP_REGISTERED))
1053 sa_addref(candidate);
1054 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1055 printf("DP %s cause refcnt++:%d SA:%p\n",
1056 __func__, candidate->refcnt, candidate));
1064 * allocating a usable SA entry for a *INBOUND* packet.
1065 * Must call key_freesav() later.
1066 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
1067 * NULL: not found, or error occured.
1069 * In the comparison, no source address is used--for RFC2401 conformance.
1070 * To quote, from section 4.1:
1071 * A security association is uniquely identified by a triple consisting
1072 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1073 * security protocol (AH or ESP) identifier.
1074 * Note that, however, we do need to keep source address in IPsec SA.
1075 * IKE specification and PF_KEY specification do assume that we
1076 * keep source address in IPsec SA. We see a tricky situation here.
1080 union sockaddr_union *dst,
1083 const char* where, int tag)
1085 struct secashead *sah;
1086 struct secasvar *sav;
1087 u_int stateidx, arraysize, state;
1088 const u_int *saorder_state_valid;
1091 IPSEC_ASSERT(dst != NULL, ("null dst address"));
1093 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1094 printf("DP %s from %s:%u\n", __func__, where, tag));
1097 chkport = (dst->sa.sa_family == AF_INET &&
1098 dst->sa.sa_len == sizeof(struct sockaddr_in) &&
1099 dst->sin.sin_port != 0);
1106 * XXX: to be checked internal IP header somewhere. Also when
1107 * IPsec tunnel packet is received. But ESP tunnel mode is
1108 * encrypted so we can't check internal IP header.
1111 if (V_key_preferred_oldsa) {
1112 saorder_state_valid = saorder_state_valid_prefer_old;
1113 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
1115 saorder_state_valid = saorder_state_valid_prefer_new;
1116 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
1118 LIST_FOREACH(sah, &V_sahtree, chain) {
1119 /* search valid state */
1120 for (stateidx = 0; stateidx < arraysize; stateidx++) {
1121 state = saorder_state_valid[stateidx];
1122 LIST_FOREACH(sav, &sah->savtree[state], chain) {
1124 KEY_CHKSASTATE(sav->state, state, __func__);
1125 /* do not return entries w/ unusable state */
1126 if (sav->state != SADB_SASTATE_MATURE &&
1127 sav->state != SADB_SASTATE_DYING)
1129 if (proto != sav->sah->saidx.proto)
1131 if (spi != sav->spi)
1133 #if 0 /* don't check src */
1134 /* check src address */
1135 if (key_sockaddrcmp(&src->sa, &sav->sah->saidx.src.sa, chkport) != 0)
1138 /* check dst address */
1139 if (key_sockaddrcmp(&dst->sa, &sav->sah->saidx.dst.sa, chkport) != 0)
1150 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1151 printf("DP %s return SA:%p; refcnt %u\n", __func__,
1152 sav, sav ? sav->refcnt : 0));
1157 * Must be called after calling key_allocsp().
1158 * For both the packet without socket and key_freeso().
1161 _key_freesp(struct secpolicy **spp, const char* where, int tag)
1163 struct secpolicy *sp = *spp;
1165 IPSEC_ASSERT(sp != NULL, ("null sp"));
1170 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1171 printf("DP %s SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1172 __func__, sp, sp->id, where, tag, sp->refcnt));
1174 if (sp->refcnt == 0) {
1182 * Must be called after calling key_allocsp().
1183 * For the packet with socket.
1186 key_freeso(struct socket *so)
1188 IPSEC_ASSERT(so != NULL, ("null so"));
1190 switch (so->so_proto->pr_domain->dom_family) {
1191 #if defined(INET) || defined(INET6)
1199 struct inpcb *pcb = sotoinpcb(so);
1201 /* Does it have a PCB ? */
1204 key_freesp_so(&pcb->inp_sp->sp_in);
1205 key_freesp_so(&pcb->inp_sp->sp_out);
1208 #endif /* INET || INET6 */
1210 ipseclog((LOG_DEBUG, "%s: unknown address family=%d.\n",
1211 __func__, so->so_proto->pr_domain->dom_family));
1217 key_freesp_so(struct secpolicy **sp)
1219 IPSEC_ASSERT(sp != NULL && *sp != NULL, ("null sp"));
1221 if ((*sp)->policy == IPSEC_POLICY_ENTRUST ||
1222 (*sp)->policy == IPSEC_POLICY_BYPASS)
1225 IPSEC_ASSERT((*sp)->policy == IPSEC_POLICY_IPSEC,
1226 ("invalid policy %u", (*sp)->policy));
1231 key_addrefsa(struct secasvar *sav, const char* where, int tag)
1234 IPSEC_ASSERT(sav != NULL, ("null sav"));
1235 IPSEC_ASSERT(sav->refcnt > 0, ("refcount must exist"));
1241 * Must be called after calling key_allocsa().
1242 * This function is called by key_freesp() to free some SA allocated
1246 key_freesav(struct secasvar **psav, const char* where, int tag)
1248 struct secasvar *sav = *psav;
1250 IPSEC_ASSERT(sav != NULL, ("null sav"));
1252 if (sa_delref(sav)) {
1253 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1254 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1255 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1259 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1260 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1261 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1265 /* %%% SPD management */
1267 * free security policy entry.
1270 key_delsp(struct secpolicy *sp)
1272 struct ipsecrequest *isr, *nextisr;
1274 IPSEC_ASSERT(sp != NULL, ("null sp"));
1275 SPTREE_LOCK_ASSERT();
1277 sp->state = IPSEC_SPSTATE_DEAD;
1279 IPSEC_ASSERT(sp->refcnt == 0,
1280 ("SP with references deleted (refcnt %u)", sp->refcnt));
1282 /* remove from SP index */
1283 if (__LIST_CHAINED(sp))
1284 LIST_REMOVE(sp, chain);
1286 for (isr = sp->req; isr != NULL; isr = nextisr) {
1287 if (isr->sav != NULL) {
1288 KEY_FREESAV(&isr->sav);
1292 nextisr = isr->next;
1300 * OUT: NULL : not found
1301 * others : found, pointer to a SP.
1303 static struct secpolicy *
1304 key_getsp(struct secpolicyindex *spidx)
1306 struct secpolicy *sp;
1308 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
1311 LIST_FOREACH(sp, &V_sptree[spidx->dir], chain) {
1312 if (sp->state == IPSEC_SPSTATE_DEAD)
1314 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1326 * OUT: NULL : not found
1327 * others : found, pointer to a SP.
1329 static struct secpolicy *
1330 key_getspbyid(u_int32_t id)
1332 struct secpolicy *sp;
1335 LIST_FOREACH(sp, &V_sptree[IPSEC_DIR_INBOUND], chain) {
1336 if (sp->state == IPSEC_SPSTATE_DEAD)
1344 LIST_FOREACH(sp, &V_sptree[IPSEC_DIR_OUTBOUND], chain) {
1345 if (sp->state == IPSEC_SPSTATE_DEAD)
1359 key_newsp(const char* where, int tag)
1361 struct secpolicy *newsp = NULL;
1363 newsp = (struct secpolicy *)
1364 malloc(sizeof(struct secpolicy), M_IPSEC_SP, M_NOWAIT|M_ZERO);
1366 SECPOLICY_LOCK_INIT(newsp);
1371 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1372 printf("DP %s from %s:%u return SP:%p\n", __func__,
1373 where, tag, newsp));
1378 _key_delsp(struct secpolicy *sp)
1380 SECPOLICY_LOCK_DESTROY(sp);
1381 free(sp, M_IPSEC_SP);
1385 * create secpolicy structure from sadb_x_policy structure.
1386 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1387 * so must be set properly later.
1390 key_msg2sp(xpl0, len, error)
1391 struct sadb_x_policy *xpl0;
1395 struct secpolicy *newsp;
1397 IPSEC_ASSERT(xpl0 != NULL, ("null xpl0"));
1398 IPSEC_ASSERT(len >= sizeof(*xpl0), ("policy too short: %zu", len));
1400 if (len != PFKEY_EXTLEN(xpl0)) {
1401 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n", __func__));
1406 if ((newsp = KEY_NEWSP()) == NULL) {
1411 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1412 newsp->policy = xpl0->sadb_x_policy_type;
1415 switch (xpl0->sadb_x_policy_type) {
1416 case IPSEC_POLICY_DISCARD:
1417 case IPSEC_POLICY_NONE:
1418 case IPSEC_POLICY_ENTRUST:
1419 case IPSEC_POLICY_BYPASS:
1423 case IPSEC_POLICY_IPSEC:
1426 struct sadb_x_ipsecrequest *xisr;
1427 struct ipsecrequest **p_isr = &newsp->req;
1429 /* validity check */
1430 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1431 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n",
1438 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1439 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1443 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1444 ipseclog((LOG_DEBUG, "%s: invalid ipsecrequest "
1445 "length.\n", __func__));
1451 /* allocate request buffer */
1452 /* NB: data structure is zero'd */
1453 *p_isr = ipsec_newisr();
1454 if ((*p_isr) == NULL) {
1455 ipseclog((LOG_DEBUG,
1456 "%s: No more memory.\n", __func__));
1463 switch (xisr->sadb_x_ipsecrequest_proto) {
1466 case IPPROTO_IPCOMP:
1469 ipseclog((LOG_DEBUG,
1470 "%s: invalid proto type=%u\n", __func__,
1471 xisr->sadb_x_ipsecrequest_proto));
1473 *error = EPROTONOSUPPORT;
1476 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1478 switch (xisr->sadb_x_ipsecrequest_mode) {
1479 case IPSEC_MODE_TRANSPORT:
1480 case IPSEC_MODE_TUNNEL:
1482 case IPSEC_MODE_ANY:
1484 ipseclog((LOG_DEBUG,
1485 "%s: invalid mode=%u\n", __func__,
1486 xisr->sadb_x_ipsecrequest_mode));
1491 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1493 switch (xisr->sadb_x_ipsecrequest_level) {
1494 case IPSEC_LEVEL_DEFAULT:
1495 case IPSEC_LEVEL_USE:
1496 case IPSEC_LEVEL_REQUIRE:
1498 case IPSEC_LEVEL_UNIQUE:
1499 /* validity check */
1501 * If range violation of reqid, kernel will
1502 * update it, don't refuse it.
1504 if (xisr->sadb_x_ipsecrequest_reqid
1505 > IPSEC_MANUAL_REQID_MAX) {
1506 ipseclog((LOG_DEBUG,
1507 "%s: reqid=%d range "
1508 "violation, updated by kernel.\n",
1510 xisr->sadb_x_ipsecrequest_reqid));
1511 xisr->sadb_x_ipsecrequest_reqid = 0;
1514 /* allocate new reqid id if reqid is zero. */
1515 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1517 if ((reqid = key_newreqid()) == 0) {
1522 (*p_isr)->saidx.reqid = reqid;
1523 xisr->sadb_x_ipsecrequest_reqid = reqid;
1525 /* set it for manual keying. */
1526 (*p_isr)->saidx.reqid =
1527 xisr->sadb_x_ipsecrequest_reqid;
1532 ipseclog((LOG_DEBUG, "%s: invalid level=%u\n",
1534 xisr->sadb_x_ipsecrequest_level));
1539 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1541 /* set IP addresses if there */
1542 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1543 struct sockaddr *paddr;
1545 paddr = (struct sockaddr *)(xisr + 1);
1547 /* validity check */
1549 > sizeof((*p_isr)->saidx.src)) {
1550 ipseclog((LOG_DEBUG, "%s: invalid "
1551 "request address length.\n",
1557 bcopy(paddr, &(*p_isr)->saidx.src,
1560 paddr = (struct sockaddr *)((caddr_t)paddr
1563 /* validity check */
1565 > sizeof((*p_isr)->saidx.dst)) {
1566 ipseclog((LOG_DEBUG, "%s: invalid "
1567 "request address length.\n",
1573 bcopy(paddr, &(*p_isr)->saidx.dst,
1577 (*p_isr)->sp = newsp;
1579 /* initialization for the next. */
1580 p_isr = &(*p_isr)->next;
1581 tlen -= xisr->sadb_x_ipsecrequest_len;
1583 /* validity check */
1585 ipseclog((LOG_DEBUG, "%s: becoming tlen < 0.\n",
1592 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1593 + xisr->sadb_x_ipsecrequest_len);
1598 ipseclog((LOG_DEBUG, "%s: invalid policy type.\n", __func__));
1611 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1613 auto_reqid = (auto_reqid == ~0
1614 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1616 /* XXX should be unique check */
1622 * copy secpolicy struct to sadb_x_policy structure indicated.
1626 struct secpolicy *sp;
1628 struct sadb_x_policy *xpl;
1633 IPSEC_ASSERT(sp != NULL, ("null policy"));
1635 tlen = key_getspreqmsglen(sp);
1637 m = key_alloc_mbuf(tlen);
1638 if (!m || m->m_next) { /*XXX*/
1646 xpl = mtod(m, struct sadb_x_policy *);
1649 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1650 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1651 xpl->sadb_x_policy_type = sp->policy;
1652 xpl->sadb_x_policy_dir = sp->spidx.dir;
1653 xpl->sadb_x_policy_id = sp->id;
1654 p = (caddr_t)xpl + sizeof(*xpl);
1656 /* if is the policy for ipsec ? */
1657 if (sp->policy == IPSEC_POLICY_IPSEC) {
1658 struct sadb_x_ipsecrequest *xisr;
1659 struct ipsecrequest *isr;
1661 for (isr = sp->req; isr != NULL; isr = isr->next) {
1663 xisr = (struct sadb_x_ipsecrequest *)p;
1665 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1666 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1667 xisr->sadb_x_ipsecrequest_level = isr->level;
1668 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1671 bcopy(&isr->saidx.src, p, isr->saidx.src.sa.sa_len);
1672 p += isr->saidx.src.sa.sa_len;
1673 bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len);
1674 p += isr->saidx.src.sa.sa_len;
1676 xisr->sadb_x_ipsecrequest_len =
1677 PFKEY_ALIGN8(sizeof(*xisr)
1678 + isr->saidx.src.sa.sa_len
1679 + isr->saidx.dst.sa.sa_len);
1686 /* m will not be freed nor modified */
1687 static struct mbuf *
1689 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1690 int ndeep, int nitem, ...)
1692 key_gather_mbuf(m, mhp, ndeep, nitem, va_alist)
1694 const struct sadb_msghdr *mhp;
1703 struct mbuf *result = NULL, *n;
1706 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1707 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1709 va_start(ap, nitem);
1710 for (i = 0; i < nitem; i++) {
1711 idx = va_arg(ap, int);
1712 if (idx < 0 || idx > SADB_EXT_MAX)
1714 /* don't attempt to pull empty extension */
1715 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1717 if (idx != SADB_EXT_RESERVED &&
1718 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1721 if (idx == SADB_EXT_RESERVED) {
1722 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1724 IPSEC_ASSERT(len <= MHLEN, ("header too big %u", len));
1726 MGETHDR(n, M_DONTWAIT, MT_DATA);
1731 m_copydata(m, 0, sizeof(struct sadb_msg),
1733 } else if (i < ndeep) {
1734 len = mhp->extlen[idx];
1735 n = key_alloc_mbuf(len);
1736 if (!n || n->m_next) { /*XXX*/
1741 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1744 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1757 if ((result->m_flags & M_PKTHDR) != 0) {
1758 result->m_pkthdr.len = 0;
1759 for (n = result; n; n = n->m_next)
1760 result->m_pkthdr.len += n->m_len;
1772 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1773 * add an entry to SP database, when received
1774 * <base, address(SD), (lifetime(H),) policy>
1776 * Adding to SP database,
1778 * <base, address(SD), (lifetime(H),) policy>
1779 * to the socket which was send.
1781 * SPDADD set a unique policy entry.
1782 * SPDSETIDX like SPDADD without a part of policy requests.
1783 * SPDUPDATE replace a unique policy entry.
1785 * m will always be freed.
1788 key_spdadd(so, m, mhp)
1791 const struct sadb_msghdr *mhp;
1793 struct sadb_address *src0, *dst0;
1794 struct sadb_x_policy *xpl0, *xpl;
1795 struct sadb_lifetime *lft = NULL;
1796 struct secpolicyindex spidx;
1797 struct secpolicy *newsp;
1800 IPSEC_ASSERT(so != NULL, ("null socket"));
1801 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1802 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1803 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
1805 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1806 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1807 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1808 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1809 return key_senderror(so, m, EINVAL);
1811 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1812 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1813 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1814 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1816 return key_senderror(so, m, EINVAL);
1818 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1819 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1820 < sizeof(struct sadb_lifetime)) {
1821 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1823 return key_senderror(so, m, EINVAL);
1825 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1828 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1829 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1830 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1833 * Note: do not parse SADB_X_EXT_NAT_T_* here:
1834 * we are processing traffic endpoints.
1838 /* XXX boundary check against sa_len */
1839 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1842 src0->sadb_address_prefixlen,
1843 dst0->sadb_address_prefixlen,
1844 src0->sadb_address_proto,
1847 /* checking the direciton. */
1848 switch (xpl0->sadb_x_policy_dir) {
1849 case IPSEC_DIR_INBOUND:
1850 case IPSEC_DIR_OUTBOUND:
1853 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
1854 mhp->msg->sadb_msg_errno = EINVAL;
1859 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1860 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1861 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1862 ipseclog((LOG_DEBUG, "%s: Invalid policy type.\n", __func__));
1863 return key_senderror(so, m, EINVAL);
1866 /* policy requests are mandatory when action is ipsec. */
1867 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1868 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1869 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1870 ipseclog((LOG_DEBUG, "%s: some policy requests part required\n",
1872 return key_senderror(so, m, EINVAL);
1876 * checking there is SP already or not.
1877 * SPDUPDATE doesn't depend on whether there is a SP or not.
1878 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1881 newsp = key_getsp(&spidx);
1882 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1885 newsp->state = IPSEC_SPSTATE_DEAD;
1890 if (newsp != NULL) {
1892 ipseclog((LOG_DEBUG, "%s: a SP entry exists already.\n",
1894 return key_senderror(so, m, EEXIST);
1898 /* allocation new SP entry */
1899 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1900 return key_senderror(so, m, error);
1903 if ((newsp->id = key_getnewspid()) == 0) {
1905 return key_senderror(so, m, ENOBUFS);
1908 /* XXX boundary check against sa_len */
1909 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1912 src0->sadb_address_prefixlen,
1913 dst0->sadb_address_prefixlen,
1914 src0->sadb_address_proto,
1917 /* sanity check on addr pair */
1918 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1919 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1921 return key_senderror(so, m, EINVAL);
1923 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1924 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1926 return key_senderror(so, m, EINVAL);
1929 if (newsp->req && newsp->req->saidx.src.sa.sa_family && newsp->req->saidx.dst.sa.sa_family) {
1930 if (newsp->req->saidx.src.sa.sa_family != newsp->req->saidx.dst.sa.sa_family) {
1932 return key_senderror(so, m, EINVAL);
1937 newsp->created = time_second;
1938 newsp->lastused = newsp->created;
1939 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1940 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1942 newsp->refcnt = 1; /* do not reclaim until I say I do */
1943 newsp->state = IPSEC_SPSTATE_ALIVE;
1944 LIST_INSERT_TAIL(&V_sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1946 /* delete the entry in spacqtree */
1947 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1948 struct secspacq *spacq = key_getspacq(&spidx);
1949 if (spacq != NULL) {
1950 /* reset counter in order to deletion by timehandler. */
1951 spacq->created = time_second;
1958 struct mbuf *n, *mpolicy;
1959 struct sadb_msg *newmsg;
1963 * Note: do not send SADB_X_EXT_NAT_T_* here:
1964 * we are sending traffic endpoints.
1967 /* create new sadb_msg to reply. */
1969 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1970 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1971 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1973 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1975 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1978 return key_senderror(so, m, ENOBUFS);
1980 if (n->m_len < sizeof(*newmsg)) {
1981 n = m_pullup(n, sizeof(*newmsg));
1983 return key_senderror(so, m, ENOBUFS);
1985 newmsg = mtod(n, struct sadb_msg *);
1986 newmsg->sadb_msg_errno = 0;
1987 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1990 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
1991 sizeof(*xpl), &off);
1992 if (mpolicy == NULL) {
1993 /* n is already freed */
1994 return key_senderror(so, m, ENOBUFS);
1996 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
1997 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
1999 return key_senderror(so, m, EINVAL);
2001 xpl->sadb_x_policy_id = newsp->id;
2004 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2009 * get new policy id.
2017 u_int32_t newid = 0;
2018 int count = V_key_spi_trycnt; /* XXX */
2019 struct secpolicy *sp;
2021 /* when requesting to allocate spi ranged */
2023 newid = (V_policy_id = (V_policy_id == ~0 ? 1 : V_policy_id + 1));
2025 if ((sp = key_getspbyid(newid)) == NULL)
2031 if (count == 0 || newid == 0) {
2032 ipseclog((LOG_DEBUG, "%s: to allocate policy id is failed.\n",
2041 * SADB_SPDDELETE processing
2043 * <base, address(SD), policy(*)>
2044 * from the user(?), and set SADB_SASTATE_DEAD,
2046 * <base, address(SD), policy(*)>
2048 * policy(*) including direction of policy.
2050 * m will always be freed.
2053 key_spddelete(so, m, mhp)
2056 const struct sadb_msghdr *mhp;
2058 struct sadb_address *src0, *dst0;
2059 struct sadb_x_policy *xpl0;
2060 struct secpolicyindex spidx;
2061 struct secpolicy *sp;
2063 IPSEC_ASSERT(so != NULL, ("null so"));
2064 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2065 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2066 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2068 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
2069 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
2070 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
2071 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2073 return key_senderror(so, m, EINVAL);
2075 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
2076 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
2077 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2078 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2080 return key_senderror(so, m, EINVAL);
2083 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2084 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2085 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2088 * Note: do not parse SADB_X_EXT_NAT_T_* here:
2089 * we are processing traffic endpoints.
2093 /* XXX boundary check against sa_len */
2094 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2097 src0->sadb_address_prefixlen,
2098 dst0->sadb_address_prefixlen,
2099 src0->sadb_address_proto,
2102 /* checking the direciton. */
2103 switch (xpl0->sadb_x_policy_dir) {
2104 case IPSEC_DIR_INBOUND:
2105 case IPSEC_DIR_OUTBOUND:
2108 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
2109 return key_senderror(so, m, EINVAL);
2112 /* Is there SP in SPD ? */
2113 if ((sp = key_getsp(&spidx)) == NULL) {
2114 ipseclog((LOG_DEBUG, "%s: no SP found.\n", __func__));
2115 return key_senderror(so, m, EINVAL);
2118 /* save policy id to buffer to be returned. */
2119 xpl0->sadb_x_policy_id = sp->id;
2122 sp->state = IPSEC_SPSTATE_DEAD;
2128 struct sadb_msg *newmsg;
2131 * Note: do not send SADB_X_EXT_NAT_T_* here:
2132 * we are sending traffic endpoints.
2135 /* create new sadb_msg to reply. */
2136 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2137 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2139 return key_senderror(so, m, ENOBUFS);
2141 newmsg = mtod(n, struct sadb_msg *);
2142 newmsg->sadb_msg_errno = 0;
2143 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2146 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2151 * SADB_SPDDELETE2 processing
2154 * from the user(?), and set SADB_SASTATE_DEAD,
2158 * policy(*) including direction of policy.
2160 * m will always be freed.
2163 key_spddelete2(so, m, mhp)
2166 const struct sadb_msghdr *mhp;
2169 struct secpolicy *sp;
2171 IPSEC_ASSERT(so != NULL, ("null socket"));
2172 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2173 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2174 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2176 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2177 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2178 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", __func__));
2179 return key_senderror(so, m, EINVAL);
2182 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2184 /* Is there SP in SPD ? */
2185 if ((sp = key_getspbyid(id)) == NULL) {
2186 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2187 return key_senderror(so, m, EINVAL);
2191 sp->state = IPSEC_SPSTATE_DEAD;
2196 struct mbuf *n, *nn;
2197 struct sadb_msg *newmsg;
2200 /* create new sadb_msg to reply. */
2201 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2203 MGETHDR(n, M_DONTWAIT, MT_DATA);
2204 if (n && len > MHLEN) {
2205 MCLGET(n, M_DONTWAIT);
2206 if ((n->m_flags & M_EXT) == 0) {
2212 return key_senderror(so, m, ENOBUFS);
2218 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2219 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2221 IPSEC_ASSERT(off == len, ("length inconsistency (off %u len %u)",
2224 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2225 mhp->extlen[SADB_X_EXT_POLICY], M_DONTWAIT);
2228 return key_senderror(so, m, ENOBUFS);
2231 n->m_pkthdr.len = 0;
2232 for (nn = n; nn; nn = nn->m_next)
2233 n->m_pkthdr.len += nn->m_len;
2235 newmsg = mtod(n, struct sadb_msg *);
2236 newmsg->sadb_msg_errno = 0;
2237 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2240 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2245 * SADB_X_GET processing
2250 * <base, address(SD), policy>
2252 * policy(*) including direction of policy.
2254 * m will always be freed.
2257 key_spdget(so, m, mhp)
2260 const struct sadb_msghdr *mhp;
2263 struct secpolicy *sp;
2266 IPSEC_ASSERT(so != NULL, ("null socket"));
2267 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2268 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2269 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2271 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2272 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2273 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2275 return key_senderror(so, m, EINVAL);
2278 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2280 /* Is there SP in SPD ? */
2281 if ((sp = key_getspbyid(id)) == NULL) {
2282 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2283 return key_senderror(so, m, ENOENT);
2286 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2290 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2292 return key_senderror(so, m, ENOBUFS);
2296 * SADB_X_SPDACQUIRE processing.
2297 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2300 * to KMD, and expect to receive
2301 * <base> with SADB_X_SPDACQUIRE if error occured,
2304 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2305 * policy(*) is without policy requests.
2308 * others: error number
2312 struct secpolicy *sp;
2314 struct mbuf *result = NULL, *m;
2315 struct secspacq *newspacq;
2317 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2318 IPSEC_ASSERT(sp->req == NULL, ("policy exists"));
2319 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
2320 ("policy not IPSEC %u", sp->policy));
2322 /* Get an entry to check whether sent message or not. */
2323 newspacq = key_getspacq(&sp->spidx);
2324 if (newspacq != NULL) {
2325 if (V_key_blockacq_count < newspacq->count) {
2326 /* reset counter and do send message. */
2327 newspacq->count = 0;
2329 /* increment counter and do nothing. */
2336 /* make new entry for blocking to send SADB_ACQUIRE. */
2337 newspacq = key_newspacq(&sp->spidx);
2338 if (newspacq == NULL)
2342 /* create new sadb_msg to reply. */
2343 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2349 result->m_pkthdr.len = 0;
2350 for (m = result; m; m = m->m_next)
2351 result->m_pkthdr.len += m->m_len;
2353 mtod(result, struct sadb_msg *)->sadb_msg_len =
2354 PFKEY_UNIT64(result->m_pkthdr.len);
2356 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2360 * SADB_SPDFLUSH processing
2363 * from the user, and free all entries in secpctree.
2367 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2369 * m will always be freed.
2372 key_spdflush(so, m, mhp)
2375 const struct sadb_msghdr *mhp;
2377 struct sadb_msg *newmsg;
2378 struct secpolicy *sp;
2381 IPSEC_ASSERT(so != NULL, ("null socket"));
2382 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2383 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2384 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2386 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2387 return key_senderror(so, m, EINVAL);
2389 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2391 LIST_FOREACH(sp, &V_sptree[dir], chain)
2392 sp->state = IPSEC_SPSTATE_DEAD;
2396 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2397 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2398 return key_senderror(so, m, ENOBUFS);
2404 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2405 newmsg = mtod(m, struct sadb_msg *);
2406 newmsg->sadb_msg_errno = 0;
2407 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2409 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2413 * SADB_SPDDUMP processing
2416 * from the user, and dump all SP leaves
2421 * m will always be freed.
2424 key_spddump(so, m, mhp)
2427 const struct sadb_msghdr *mhp;
2429 struct secpolicy *sp;
2434 IPSEC_ASSERT(so != NULL, ("null socket"));
2435 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2436 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2437 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2439 /* search SPD entry and get buffer size. */
2442 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2443 LIST_FOREACH(sp, &V_sptree[dir], chain) {
2450 return key_senderror(so, m, ENOENT);
2453 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2454 LIST_FOREACH(sp, &V_sptree[dir], chain) {
2456 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2457 mhp->msg->sadb_msg_pid);
2460 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2469 static struct mbuf *
2470 key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq, u_int32_t pid)
2472 struct mbuf *result = NULL, *m;
2473 struct seclifetime lt;
2475 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2481 * Note: do not send SADB_X_EXT_NAT_T_* here:
2482 * we are sending traffic endpoints.
2484 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2485 &sp->spidx.src.sa, sp->spidx.prefs,
2486 sp->spidx.ul_proto);
2491 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2492 &sp->spidx.dst.sa, sp->spidx.prefd,
2493 sp->spidx.ul_proto);
2504 lt.addtime=sp->created;
2505 lt.usetime= sp->lastused;
2506 m = key_setlifetime(<, SADB_EXT_LIFETIME_CURRENT);
2511 lt.addtime=sp->lifetime;
2512 lt.usetime= sp->validtime;
2513 m = key_setlifetime(<, SADB_EXT_LIFETIME_HARD);
2519 if ((result->m_flags & M_PKTHDR) == 0)
2522 if (result->m_len < sizeof(struct sadb_msg)) {
2523 result = m_pullup(result, sizeof(struct sadb_msg));
2528 result->m_pkthdr.len = 0;
2529 for (m = result; m; m = m->m_next)
2530 result->m_pkthdr.len += m->m_len;
2532 mtod(result, struct sadb_msg *)->sadb_msg_len =
2533 PFKEY_UNIT64(result->m_pkthdr.len);
2543 * get PFKEY message length for security policy and request.
2546 key_getspreqmsglen(sp)
2547 struct secpolicy *sp;
2551 tlen = sizeof(struct sadb_x_policy);
2553 /* if is the policy for ipsec ? */
2554 if (sp->policy != IPSEC_POLICY_IPSEC)
2557 /* get length of ipsec requests */
2559 struct ipsecrequest *isr;
2562 for (isr = sp->req; isr != NULL; isr = isr->next) {
2563 len = sizeof(struct sadb_x_ipsecrequest)
2564 + isr->saidx.src.sa.sa_len
2565 + isr->saidx.dst.sa.sa_len;
2567 tlen += PFKEY_ALIGN8(len);
2575 * SADB_SPDEXPIRE processing
2577 * <base, address(SD), lifetime(CH), policy>
2581 * others : error number
2585 struct secpolicy *sp;
2587 struct mbuf *result = NULL, *m;
2590 struct sadb_lifetime *lt;
2592 /* XXX: Why do we lock ? */
2594 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2596 /* set msg header */
2597 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2604 /* create lifetime extension (current and hard) */
2605 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2606 m = key_alloc_mbuf(len);
2607 if (!m || m->m_next) { /*XXX*/
2613 bzero(mtod(m, caddr_t), len);
2614 lt = mtod(m, struct sadb_lifetime *);
2615 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2616 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2617 lt->sadb_lifetime_allocations = 0;
2618 lt->sadb_lifetime_bytes = 0;
2619 lt->sadb_lifetime_addtime = sp->created;
2620 lt->sadb_lifetime_usetime = sp->lastused;
2621 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2622 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2623 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2624 lt->sadb_lifetime_allocations = 0;
2625 lt->sadb_lifetime_bytes = 0;
2626 lt->sadb_lifetime_addtime = sp->lifetime;
2627 lt->sadb_lifetime_usetime = sp->validtime;
2631 * Note: do not send SADB_X_EXT_NAT_T_* here:
2632 * we are sending traffic endpoints.
2635 /* set sadb_address for source */
2636 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2638 sp->spidx.prefs, sp->spidx.ul_proto);
2645 /* set sadb_address for destination */
2646 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2648 sp->spidx.prefd, sp->spidx.ul_proto);
2663 if ((result->m_flags & M_PKTHDR) == 0) {
2668 if (result->m_len < sizeof(struct sadb_msg)) {
2669 result = m_pullup(result, sizeof(struct sadb_msg));
2670 if (result == NULL) {
2676 result->m_pkthdr.len = 0;
2677 for (m = result; m; m = m->m_next)
2678 result->m_pkthdr.len += m->m_len;
2680 mtod(result, struct sadb_msg *)->sadb_msg_len =
2681 PFKEY_UNIT64(result->m_pkthdr.len);
2683 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2691 /* %%% SAD management */
2693 * allocating a memory for new SA head, and copy from the values of mhp.
2694 * OUT: NULL : failure due to the lack of memory.
2695 * others : pointer to new SA head.
2697 static struct secashead *
2699 struct secasindex *saidx;
2701 struct secashead *newsah;
2703 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
2705 newsah = malloc(sizeof(struct secashead), M_IPSEC_SAH, M_NOWAIT|M_ZERO);
2706 if (newsah != NULL) {
2708 for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++)
2709 LIST_INIT(&newsah->savtree[i]);
2710 newsah->saidx = *saidx;
2712 /* add to saidxtree */
2713 newsah->state = SADB_SASTATE_MATURE;
2716 LIST_INSERT_HEAD(&V_sahtree, newsah, chain);
2723 * delete SA index and all SA registerd.
2727 struct secashead *sah;
2729 struct secasvar *sav, *nextsav;
2733 IPSEC_ASSERT(sah != NULL, ("NULL sah"));
2734 SAHTREE_LOCK_ASSERT();
2736 /* searching all SA registerd in the secindex. */
2738 stateidx < _ARRAYLEN(saorder_state_any);
2740 u_int state = saorder_state_any[stateidx];
2741 LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain, nextsav) {
2742 if (sav->refcnt == 0) {
2744 KEY_CHKSASTATE(state, sav->state, __func__);
2746 * do NOT call KEY_FREESAV here:
2747 * it will only delete the sav if refcnt == 1,
2748 * where we already know that refcnt == 0
2752 /* give up to delete this sa */
2757 if (!zombie) { /* delete only if there are savs */
2758 /* remove from tree of SA index */
2759 if (__LIST_CHAINED(sah))
2760 LIST_REMOVE(sah, chain);
2761 if (sah->route_cache.sa_route.ro_rt) {
2762 RTFREE(sah->route_cache.sa_route.ro_rt);
2763 sah->route_cache.sa_route.ro_rt = (struct rtentry *)NULL;
2765 free(sah, M_IPSEC_SAH);
2770 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2771 * and copy the values of mhp into new buffer.
2772 * When SAD message type is GETSPI:
2773 * to set sequence number from acq_seq++,
2774 * to set zero to SPI.
2775 * not to call key_setsava().
2777 * others : pointer to new secasvar.
2779 * does not modify mbuf. does not free mbuf on error.
2781 static struct secasvar *
2782 key_newsav(m, mhp, sah, errp, where, tag)
2784 const struct sadb_msghdr *mhp;
2785 struct secashead *sah;
2790 struct secasvar *newsav;
2791 const struct sadb_sa *xsa;
2793 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2794 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2795 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2796 IPSEC_ASSERT(sah != NULL, ("null secashead"));
2798 newsav = malloc(sizeof(struct secasvar), M_IPSEC_SA, M_NOWAIT|M_ZERO);
2799 if (newsav == NULL) {
2800 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2805 switch (mhp->msg->sadb_msg_type) {
2809 #ifdef IPSEC_DOSEQCHECK
2810 /* sync sequence number */
2811 if (mhp->msg->sadb_msg_seq == 0)
2813 (V_acq_seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq));
2816 newsav->seq = mhp->msg->sadb_msg_seq;
2821 if (mhp->ext[SADB_EXT_SA] == NULL) {
2822 free(newsav, M_IPSEC_SA);
2824 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2829 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2830 newsav->spi = xsa->sadb_sa_spi;
2831 newsav->seq = mhp->msg->sadb_msg_seq;
2834 free(newsav, M_IPSEC_SA);
2841 /* copy sav values */
2842 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2843 *errp = key_setsaval(newsav, m, mhp);
2845 free(newsav, M_IPSEC_SA);
2851 SECASVAR_LOCK_INIT(newsav);
2854 newsav->created = time_second;
2855 newsav->pid = mhp->msg->sadb_msg_pid;
2860 newsav->state = SADB_SASTATE_LARVAL;
2863 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2867 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
2868 printf("DP %s from %s:%u return SP:%p\n", __func__,
2869 where, tag, newsav));
2875 * free() SA variable entry.
2878 key_cleansav(struct secasvar *sav)
2881 * Cleanup xform state. Note that zeroize'ing causes the
2882 * keys to be cleared; otherwise we must do it ourself.
2884 if (sav->tdb_xform != NULL) {
2885 sav->tdb_xform->xf_zeroize(sav);
2886 sav->tdb_xform = NULL;
2888 KASSERT(sav->iv == NULL, ("iv but no xform"));
2889 if (sav->key_auth != NULL)
2890 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
2891 if (sav->key_enc != NULL)
2892 bzero(sav->key_enc->key_data, _KEYLEN(sav->key_enc));
2894 if (sav->key_auth != NULL) {
2895 if (sav->key_auth->key_data != NULL)
2896 free(sav->key_auth->key_data, M_IPSEC_MISC);
2897 free(sav->key_auth, M_IPSEC_MISC);
2898 sav->key_auth = NULL;
2900 if (sav->key_enc != NULL) {
2901 if (sav->key_enc->key_data != NULL)
2902 free(sav->key_enc->key_data, M_IPSEC_MISC);
2903 free(sav->key_enc, M_IPSEC_MISC);
2904 sav->key_enc = NULL;
2907 bzero(sav->sched, sav->schedlen);
2908 free(sav->sched, M_IPSEC_MISC);
2911 if (sav->replay != NULL) {
2912 free(sav->replay, M_IPSEC_MISC);
2915 if (sav->lft_c != NULL) {
2916 free(sav->lft_c, M_IPSEC_MISC);
2919 if (sav->lft_h != NULL) {
2920 free(sav->lft_h, M_IPSEC_MISC);
2923 if (sav->lft_s != NULL) {
2924 free(sav->lft_s, M_IPSEC_MISC);
2930 * free() SA variable entry.
2934 struct secasvar *sav;
2936 IPSEC_ASSERT(sav != NULL, ("null sav"));
2937 IPSEC_ASSERT(sav->refcnt == 0, ("reference count %u > 0", sav->refcnt));
2939 /* remove from SA header */
2940 if (__LIST_CHAINED(sav))
2941 LIST_REMOVE(sav, chain);
2943 SECASVAR_LOCK_DESTROY(sav);
2944 free(sav, M_IPSEC_SA);
2951 * others : found, pointer to a SA.
2953 static struct secashead *
2955 struct secasindex *saidx;
2957 struct secashead *sah;
2960 LIST_FOREACH(sah, &V_sahtree, chain) {
2961 if (sah->state == SADB_SASTATE_DEAD)
2963 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2972 * check not to be duplicated SPI.
2973 * NOTE: this function is too slow due to searching all SAD.
2976 * others : found, pointer to a SA.
2978 static struct secasvar *
2979 key_checkspidup(saidx, spi)
2980 struct secasindex *saidx;
2983 struct secashead *sah;
2984 struct secasvar *sav;
2986 /* check address family */
2987 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
2988 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
2996 LIST_FOREACH(sah, &V_sahtree, chain) {
2997 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
2999 sav = key_getsavbyspi(sah, spi);
3009 * search SAD litmited alive SA, protocol, SPI.
3012 * others : found, pointer to a SA.
3014 static struct secasvar *
3015 key_getsavbyspi(sah, spi)
3016 struct secashead *sah;
3019 struct secasvar *sav;
3020 u_int stateidx, state;
3023 SAHTREE_LOCK_ASSERT();
3024 /* search all status */
3026 stateidx < _ARRAYLEN(saorder_state_alive);
3029 state = saorder_state_alive[stateidx];
3030 LIST_FOREACH(sav, &sah->savtree[state], chain) {
3033 if (sav->state != state) {
3034 ipseclog((LOG_DEBUG, "%s: "
3035 "invalid sav->state (queue: %d SA: %d)\n",
3036 __func__, state, sav->state));
3040 if (sav->spi == spi)
3049 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
3050 * You must update these if need.
3054 * does not modify mbuf. does not free mbuf on error.
3057 key_setsaval(sav, m, mhp)
3058 struct secasvar *sav;
3060 const struct sadb_msghdr *mhp;
3064 IPSEC_ASSERT(m != NULL, ("null mbuf"));
3065 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
3066 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
3068 /* initialization */
3070 sav->key_auth = NULL;
3071 sav->key_enc = NULL;
3078 sav->tdb_xform = NULL; /* transform */
3079 sav->tdb_encalgxform = NULL; /* encoding algorithm */
3080 sav->tdb_authalgxform = NULL; /* authentication algorithm */
3081 sav->tdb_compalgxform = NULL; /* compression algorithm */
3082 /* Initialize even if NAT-T not compiled in: */
3084 sav->natt_esp_frag_len = 0;
3087 if (mhp->ext[SADB_EXT_SA] != NULL) {
3088 const struct sadb_sa *sa0;
3090 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
3091 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
3096 sav->alg_auth = sa0->sadb_sa_auth;
3097 sav->alg_enc = sa0->sadb_sa_encrypt;
3098 sav->flags = sa0->sadb_sa_flags;
3101 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
3102 sav->replay = (struct secreplay *)
3103 malloc(sizeof(struct secreplay)+sa0->sadb_sa_replay, M_IPSEC_MISC, M_NOWAIT|M_ZERO);
3104 if (sav->replay == NULL) {
3105 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3110 if (sa0->sadb_sa_replay != 0)
3111 sav->replay->bitmap = (caddr_t)(sav->replay+1);
3112 sav->replay->wsize = sa0->sadb_sa_replay;
3116 /* Authentication keys */
3117 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
3118 const struct sadb_key *key0;
3121 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
3122 len = mhp->extlen[SADB_EXT_KEY_AUTH];
3125 if (len < sizeof(*key0)) {
3129 switch (mhp->msg->sadb_msg_satype) {
3130 case SADB_SATYPE_AH:
3131 case SADB_SATYPE_ESP:
3132 case SADB_X_SATYPE_TCPSIGNATURE:
3133 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3134 sav->alg_auth != SADB_X_AALG_NULL)
3137 case SADB_X_SATYPE_IPCOMP:
3143 ipseclog((LOG_DEBUG, "%s: invalid key_auth values.\n",
3148 sav->key_auth = (struct seckey *)key_dup_keymsg(key0, len,
3150 if (sav->key_auth == NULL ) {
3151 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3158 /* Encryption key */
3159 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3160 const struct sadb_key *key0;
3163 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3164 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3167 if (len < sizeof(*key0)) {
3171 switch (mhp->msg->sadb_msg_satype) {
3172 case SADB_SATYPE_ESP:
3173 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3174 sav->alg_enc != SADB_EALG_NULL) {
3178 sav->key_enc = (struct seckey *)key_dup_keymsg(key0,
3181 if (sav->key_enc == NULL) {
3182 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3188 case SADB_X_SATYPE_IPCOMP:
3189 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3191 sav->key_enc = NULL; /*just in case*/
3193 case SADB_SATYPE_AH:
3194 case SADB_X_SATYPE_TCPSIGNATURE:
3200 ipseclog((LOG_DEBUG, "%s: invalid key_enc value.\n",
3209 switch (mhp->msg->sadb_msg_satype) {
3210 case SADB_SATYPE_AH:
3211 error = xform_init(sav, XF_AH);
3213 case SADB_SATYPE_ESP:
3214 error = xform_init(sav, XF_ESP);
3216 case SADB_X_SATYPE_IPCOMP:
3217 error = xform_init(sav, XF_IPCOMP);
3219 case SADB_X_SATYPE_TCPSIGNATURE:
3220 error = xform_init(sav, XF_TCPSIGNATURE);
3224 ipseclog((LOG_DEBUG, "%s: unable to initialize SA type %u.\n",
3225 __func__, mhp->msg->sadb_msg_satype));
3230 sav->created = time_second;
3232 /* make lifetime for CURRENT */
3233 sav->lft_c = malloc(sizeof(struct seclifetime), M_IPSEC_MISC, M_NOWAIT);
3234 if (sav->lft_c == NULL) {
3235 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3240 sav->lft_c->allocations = 0;
3241 sav->lft_c->bytes = 0;
3242 sav->lft_c->addtime = time_second;
3243 sav->lft_c->usetime = 0;
3245 /* lifetimes for HARD and SOFT */
3247 const struct sadb_lifetime *lft0;
3249 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3251 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3255 sav->lft_h = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3256 if (sav->lft_h == NULL) {
3257 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3261 /* to be initialize ? */
3264 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3266 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3270 sav->lft_s = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3271 if (sav->lft_s == NULL) {
3272 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3276 /* to be initialize ? */
3283 /* initialization */
3290 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3295 key_mature(struct secasvar *sav)
3299 /* check SPI value */
3300 switch (sav->sah->saidx.proto) {
3304 * RFC 4302, 2.4. Security Parameters Index (SPI), SPI values
3305 * 1-255 reserved by IANA for future use,
3306 * 0 for implementation specific, local use.
3308 if (ntohl(sav->spi) <= 255) {
3309 ipseclog((LOG_DEBUG, "%s: illegal range of SPI %u.\n",
3310 __func__, (u_int32_t)ntohl(sav->spi)));
3317 switch (sav->sah->saidx.proto) {
3320 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3321 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3322 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3323 "given to old-esp.\n", __func__));
3326 error = xform_init(sav, XF_ESP);
3330 if (sav->flags & SADB_X_EXT_DERIV) {
3331 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3332 "given to AH SA.\n", __func__));
3335 if (sav->alg_enc != SADB_EALG_NONE) {
3336 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3337 "mismated.\n", __func__));
3340 error = xform_init(sav, XF_AH);
3342 case IPPROTO_IPCOMP:
3343 if (sav->alg_auth != SADB_AALG_NONE) {
3344 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3345 "mismated.\n", __func__));
3348 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3349 && ntohl(sav->spi) >= 0x10000) {
3350 ipseclog((LOG_DEBUG, "%s: invalid cpi for IPComp.\n",
3354 error = xform_init(sav, XF_IPCOMP);
3357 if (sav->alg_enc != SADB_EALG_NONE) {
3358 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3359 "mismated.\n", __func__));
3362 error = xform_init(sav, XF_TCPSIGNATURE);
3365 ipseclog((LOG_DEBUG, "%s: Invalid satype.\n", __func__));
3366 error = EPROTONOSUPPORT;
3371 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3378 * subroutine for SADB_GET and SADB_DUMP.
3380 static struct mbuf *
3381 key_setdumpsa(struct secasvar *sav, u_int8_t type, u_int8_t satype,
3382 u_int32_t seq, u_int32_t pid)
3384 struct mbuf *result = NULL, *tres = NULL, *m;
3387 SADB_EXT_SA, SADB_X_EXT_SA2,
3388 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3389 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3390 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3391 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3392 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3394 SADB_X_EXT_NAT_T_TYPE,
3395 SADB_X_EXT_NAT_T_SPORT, SADB_X_EXT_NAT_T_DPORT,
3396 SADB_X_EXT_NAT_T_OAI, SADB_X_EXT_NAT_T_OAR,
3397 SADB_X_EXT_NAT_T_FRAG,
3401 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3406 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3408 switch (dumporder[i]) {
3410 m = key_setsadbsa(sav);
3415 case SADB_X_EXT_SA2:
3416 m = key_setsadbxsa2(sav->sah->saidx.mode,
3417 sav->replay ? sav->replay->count : 0,
3418 sav->sah->saidx.reqid);
3423 case SADB_EXT_ADDRESS_SRC:
3424 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3425 &sav->sah->saidx.src.sa,
3426 FULLMASK, IPSEC_ULPROTO_ANY);
3431 case SADB_EXT_ADDRESS_DST:
3432 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3433 &sav->sah->saidx.dst.sa,
3434 FULLMASK, IPSEC_ULPROTO_ANY);
3439 case SADB_EXT_KEY_AUTH:
3442 m = key_setkey(sav->key_auth, SADB_EXT_KEY_AUTH);
3447 case SADB_EXT_KEY_ENCRYPT:
3450 m = key_setkey(sav->key_enc, SADB_EXT_KEY_ENCRYPT);
3455 case SADB_EXT_LIFETIME_CURRENT:
3458 m = key_setlifetime(sav->lft_c,
3459 SADB_EXT_LIFETIME_CURRENT);
3464 case SADB_EXT_LIFETIME_HARD:
3467 m = key_setlifetime(sav->lft_h,
3468 SADB_EXT_LIFETIME_HARD);
3473 case SADB_EXT_LIFETIME_SOFT:
3476 m = key_setlifetime(sav->lft_s,
3477 SADB_EXT_LIFETIME_SOFT);
3484 case SADB_X_EXT_NAT_T_TYPE:
3485 m = key_setsadbxtype(sav->natt_type);
3490 case SADB_X_EXT_NAT_T_DPORT:
3491 m = key_setsadbxport(
3492 KEY_PORTFROMSADDR(&sav->sah->saidx.dst),
3493 SADB_X_EXT_NAT_T_DPORT);
3498 case SADB_X_EXT_NAT_T_SPORT:
3499 m = key_setsadbxport(
3500 KEY_PORTFROMSADDR(&sav->sah->saidx.src),
3501 SADB_X_EXT_NAT_T_SPORT);
3506 case SADB_X_EXT_NAT_T_OAI:
3507 case SADB_X_EXT_NAT_T_OAR:
3508 case SADB_X_EXT_NAT_T_FRAG:
3509 /* We do not (yet) support those. */
3513 case SADB_EXT_ADDRESS_PROXY:
3514 case SADB_EXT_IDENTITY_SRC:
3515 case SADB_EXT_IDENTITY_DST:
3516 /* XXX: should we brought from SPD ? */
3517 case SADB_EXT_SENSITIVITY:
3530 m_cat(result, tres);
3531 if (result->m_len < sizeof(struct sadb_msg)) {
3532 result = m_pullup(result, sizeof(struct sadb_msg));
3537 result->m_pkthdr.len = 0;
3538 for (m = result; m; m = m->m_next)
3539 result->m_pkthdr.len += m->m_len;
3541 mtod(result, struct sadb_msg *)->sadb_msg_len =
3542 PFKEY_UNIT64(result->m_pkthdr.len);
3553 * set data into sadb_msg.
3555 static struct mbuf *
3556 key_setsadbmsg(u_int8_t type, u_int16_t tlen, u_int8_t satype, u_int32_t seq,
3557 pid_t pid, u_int16_t reserved)
3563 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3566 MGETHDR(m, M_DONTWAIT, MT_DATA);
3567 if (m && len > MHLEN) {
3568 MCLGET(m, M_DONTWAIT);
3569 if ((m->m_flags & M_EXT) == 0) {
3576 m->m_pkthdr.len = m->m_len = len;
3579 p = mtod(m, struct sadb_msg *);
3582 p->sadb_msg_version = PF_KEY_V2;
3583 p->sadb_msg_type = type;
3584 p->sadb_msg_errno = 0;
3585 p->sadb_msg_satype = satype;
3586 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3587 p->sadb_msg_reserved = reserved;
3588 p->sadb_msg_seq = seq;
3589 p->sadb_msg_pid = (u_int32_t)pid;
3595 * copy secasvar data into sadb_address.
3597 static struct mbuf *
3599 struct secasvar *sav;
3605 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3606 m = key_alloc_mbuf(len);
3607 if (!m || m->m_next) { /*XXX*/
3613 p = mtod(m, struct sadb_sa *);
3616 p->sadb_sa_len = PFKEY_UNIT64(len);
3617 p->sadb_sa_exttype = SADB_EXT_SA;
3618 p->sadb_sa_spi = sav->spi;
3619 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3620 p->sadb_sa_state = sav->state;
3621 p->sadb_sa_auth = sav->alg_auth;
3622 p->sadb_sa_encrypt = sav->alg_enc;
3623 p->sadb_sa_flags = sav->flags;
3629 * set data into sadb_address.
3631 static struct mbuf *
3632 key_setsadbaddr(u_int16_t exttype, const struct sockaddr *saddr, u_int8_t prefixlen, u_int16_t ul_proto)
3635 struct sadb_address *p;
3638 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3639 PFKEY_ALIGN8(saddr->sa_len);
3640 m = key_alloc_mbuf(len);
3641 if (!m || m->m_next) { /*XXX*/
3647 p = mtod(m, struct sadb_address *);
3650 p->sadb_address_len = PFKEY_UNIT64(len);
3651 p->sadb_address_exttype = exttype;
3652 p->sadb_address_proto = ul_proto;
3653 if (prefixlen == FULLMASK) {
3654 switch (saddr->sa_family) {
3656 prefixlen = sizeof(struct in_addr) << 3;
3659 prefixlen = sizeof(struct in6_addr) << 3;
3665 p->sadb_address_prefixlen = prefixlen;
3666 p->sadb_address_reserved = 0;
3669 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3676 * set data into sadb_x_sa2.
3678 static struct mbuf *
3679 key_setsadbxsa2(u_int8_t mode, u_int32_t seq, u_int32_t reqid)
3682 struct sadb_x_sa2 *p;
3685 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3686 m = key_alloc_mbuf(len);
3687 if (!m || m->m_next) { /*XXX*/
3693 p = mtod(m, struct sadb_x_sa2 *);
3696 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3697 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3698 p->sadb_x_sa2_mode = mode;
3699 p->sadb_x_sa2_reserved1 = 0;
3700 p->sadb_x_sa2_reserved2 = 0;
3701 p->sadb_x_sa2_sequence = seq;
3702 p->sadb_x_sa2_reqid = reqid;
3709 * Set a type in sadb_x_nat_t_type.
3711 static struct mbuf *
3712 key_setsadbxtype(u_int16_t type)
3716 struct sadb_x_nat_t_type *p;
3718 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_type));
3720 m = key_alloc_mbuf(len);
3721 if (!m || m->m_next) { /*XXX*/
3727 p = mtod(m, struct sadb_x_nat_t_type *);
3730 p->sadb_x_nat_t_type_len = PFKEY_UNIT64(len);
3731 p->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
3732 p->sadb_x_nat_t_type_type = type;
3737 * Set a port in sadb_x_nat_t_port.
3738 * In contrast to default RFC 2367 behaviour, port is in network byte order.
3740 static struct mbuf *
3741 key_setsadbxport(u_int16_t port, u_int16_t type)
3745 struct sadb_x_nat_t_port *p;
3747 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_port));
3749 m = key_alloc_mbuf(len);
3750 if (!m || m->m_next) { /*XXX*/
3756 p = mtod(m, struct sadb_x_nat_t_port *);
3759 p->sadb_x_nat_t_port_len = PFKEY_UNIT64(len);
3760 p->sadb_x_nat_t_port_exttype = type;
3761 p->sadb_x_nat_t_port_port = port;
3767 * Get port from sockaddr. Port is in network byte order.
3770 key_portfromsaddr(struct sockaddr *sa)
3773 switch (sa->sa_family) {
3776 return ((struct sockaddr_in *)sa)->sin_port;
3780 return ((struct sockaddr_in6 *)sa)->sin6_port;
3783 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
3784 printf("DP %s unexpected address family %d\n",
3785 __func__, sa->sa_family));
3788 #endif /* IPSEC_NAT_T */
3791 * Set port in struct sockaddr. Port is in network byte order.
3794 key_porttosaddr(struct sockaddr *sa, u_int16_t port)
3797 switch (sa->sa_family) {
3800 ((struct sockaddr_in *)sa)->sin_port = port;
3805 ((struct sockaddr_in6 *)sa)->sin6_port = port;
3809 ipseclog((LOG_DEBUG, "%s: unexpected address family %d.\n",
3810 __func__, sa->sa_family));
3816 * set data into sadb_x_policy
3818 static struct mbuf *
3819 key_setsadbxpolicy(u_int16_t type, u_int8_t dir, u_int32_t id)
3822 struct sadb_x_policy *p;
3825 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3826 m = key_alloc_mbuf(len);
3827 if (!m || m->m_next) { /*XXX*/
3833 p = mtod(m, struct sadb_x_policy *);
3836 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3837 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3838 p->sadb_x_policy_type = type;
3839 p->sadb_x_policy_dir = dir;
3840 p->sadb_x_policy_id = id;
3846 /* Take a key message (sadb_key) from the socket and turn it into one
3847 * of the kernel's key structures (seckey).
3849 * IN: pointer to the src
3850 * OUT: NULL no more memory
3853 key_dup_keymsg(const struct sadb_key *src, u_int len,
3854 struct malloc_type *type)
3857 dst = (struct seckey *)malloc(sizeof(struct seckey), type, M_NOWAIT);
3859 dst->bits = src->sadb_key_bits;
3860 dst->key_data = (char *)malloc(len, type, M_NOWAIT);
3861 if (dst->key_data != NULL) {
3862 bcopy((const char *)src + sizeof(struct sadb_key),
3863 dst->key_data, len);
3865 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3871 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3878 /* Take a lifetime message (sadb_lifetime) passed in on a socket and
3879 * turn it into one of the kernel's lifetime structures (seclifetime).
3881 * IN: pointer to the destination, source and malloc type
3882 * OUT: NULL, no more memory
3885 static struct seclifetime *
3886 key_dup_lifemsg(const struct sadb_lifetime *src,
3887 struct malloc_type *type)
3889 struct seclifetime *dst = NULL;
3891 dst = (struct seclifetime *)malloc(sizeof(struct seclifetime),
3895 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3897 dst->allocations = src->sadb_lifetime_allocations;
3898 dst->bytes = src->sadb_lifetime_bytes;
3899 dst->addtime = src->sadb_lifetime_addtime;
3900 dst->usetime = src->sadb_lifetime_usetime;
3905 /* compare my own address
3906 * OUT: 1: true, i.e. my address.
3911 struct sockaddr *sa;
3914 struct sockaddr_in *sin;
3915 struct in_ifaddr *ia;
3918 IPSEC_ASSERT(sa != NULL, ("null sockaddr"));
3920 switch (sa->sa_family) {
3923 sin = (struct sockaddr_in *)sa;
3925 for (ia = V_in_ifaddrhead.tqh_first; ia;
3926 ia = ia->ia_link.tqe_next)
3928 if (sin->sin_family == ia->ia_addr.sin_family &&
3929 sin->sin_len == ia->ia_addr.sin_len &&
3930 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3932 IN_IFADDR_RUNLOCK();
3936 IN_IFADDR_RUNLOCK();
3941 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3950 * compare my own address for IPv6.
3953 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3955 #include <netinet6/in6_var.h>
3959 struct sockaddr_in6 *sin6;
3961 struct in6_ifaddr *ia;
3963 struct in6_multi *in6m;
3967 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
3968 if (key_sockaddrcmp((struct sockaddr *)sin6,
3969 (struct sockaddr *)&ia->ia_addr, 0) == 0) {
3970 IN6_IFADDR_RUNLOCK();
3977 * XXX why do we care about multlicast here while we don't care
3978 * about IPv4 multicast??
3982 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3984 IN6_IFADDR_RUNLOCK();
3989 IN6_IFADDR_RUNLOCK();
3991 /* loopback, just for safety */
3992 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
4000 * compare two secasindex structure.
4001 * flag can specify to compare 2 saidxes.
4002 * compare two secasindex structure without both mode and reqid.
4003 * don't compare port.
4005 * saidx0: source, it can be in SAD.
4013 const struct secasindex *saidx0,
4014 const struct secasindex *saidx1,
4020 if (saidx0 == NULL && saidx1 == NULL)
4023 if (saidx0 == NULL || saidx1 == NULL)
4026 if (saidx0->proto != saidx1->proto)
4029 if (flag == CMP_EXACTLY) {
4030 if (saidx0->mode != saidx1->mode)
4032 if (saidx0->reqid != saidx1->reqid)
4034 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
4035 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
4039 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
4040 if (flag == CMP_MODE_REQID
4041 ||flag == CMP_REQID) {
4043 * If reqid of SPD is non-zero, unique SA is required.
4044 * The result must be of same reqid in this case.
4046 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
4050 if (flag == CMP_MODE_REQID) {
4051 if (saidx0->mode != IPSEC_MODE_ANY
4052 && saidx0->mode != saidx1->mode)
4058 * If NAT-T is enabled, check ports for tunnel mode.
4059 * Do not check ports if they are set to zero in the SPD.
4060 * Also do not do it for transport mode, as there is no
4061 * port information available in the SP.
4063 if (saidx1->mode == IPSEC_MODE_TUNNEL &&
4064 saidx1->src.sa.sa_family == AF_INET &&
4065 saidx1->dst.sa.sa_family == AF_INET &&
4066 ((const struct sockaddr_in *)(&saidx1->src))->sin_port &&
4067 ((const struct sockaddr_in *)(&saidx1->dst))->sin_port)
4069 #endif /* IPSEC_NAT_T */
4071 if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, chkport) != 0) {
4074 if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, chkport) != 0) {
4083 * compare two secindex structure exactly.
4085 * spidx0: source, it is often in SPD.
4086 * spidx1: object, it is often from PFKEY message.
4092 key_cmpspidx_exactly(
4093 struct secpolicyindex *spidx0,
4094 struct secpolicyindex *spidx1)
4097 if (spidx0 == NULL && spidx1 == NULL)
4100 if (spidx0 == NULL || spidx1 == NULL)
4103 if (spidx0->prefs != spidx1->prefs
4104 || spidx0->prefd != spidx1->prefd
4105 || spidx0->ul_proto != spidx1->ul_proto)
4108 return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
4109 key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
4113 * compare two secindex structure with mask.
4115 * spidx0: source, it is often in SPD.
4116 * spidx1: object, it is often from IP header.
4122 key_cmpspidx_withmask(
4123 struct secpolicyindex *spidx0,
4124 struct secpolicyindex *spidx1)
4127 if (spidx0 == NULL && spidx1 == NULL)
4130 if (spidx0 == NULL || spidx1 == NULL)
4133 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
4134 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
4135 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
4136 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
4139 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4140 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
4141 && spidx0->ul_proto != spidx1->ul_proto)
4144 switch (spidx0->src.sa.sa_family) {
4146 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
4147 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
4149 if (!key_bbcmp(&spidx0->src.sin.sin_addr,
4150 &spidx1->src.sin.sin_addr, spidx0->prefs))
4154 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
4155 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
4158 * scope_id check. if sin6_scope_id is 0, we regard it
4159 * as a wildcard scope, which matches any scope zone ID.
4161 if (spidx0->src.sin6.sin6_scope_id &&
4162 spidx1->src.sin6.sin6_scope_id &&
4163 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
4165 if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
4166 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
4171 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
4176 switch (spidx0->dst.sa.sa_family) {
4178 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
4179 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
4181 if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
4182 &spidx1->dst.sin.sin_addr, spidx0->prefd))
4186 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
4187 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
4190 * scope_id check. if sin6_scope_id is 0, we regard it
4191 * as a wildcard scope, which matches any scope zone ID.
4193 if (spidx0->dst.sin6.sin6_scope_id &&
4194 spidx1->dst.sin6.sin6_scope_id &&
4195 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
4197 if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
4198 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
4203 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
4208 /* XXX Do we check other field ? e.g. flowinfo */
4213 /* returns 0 on match */
4216 const struct sockaddr *sa1,
4217 const struct sockaddr *sa2,
4223 #define satosin(s) ((const struct sockaddr_in *)s)
4227 #define satosin6(s) ((const struct sockaddr_in6 *)s)
4228 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
4231 switch (sa1->sa_family) {
4233 if (sa1->sa_len != sizeof(struct sockaddr_in))
4235 if (satosin(sa1)->sin_addr.s_addr !=
4236 satosin(sa2)->sin_addr.s_addr) {
4239 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
4243 if (sa1->sa_len != sizeof(struct sockaddr_in6))
4244 return 1; /*EINVAL*/
4245 if (satosin6(sa1)->sin6_scope_id !=
4246 satosin6(sa2)->sin6_scope_id) {
4249 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
4250 &satosin6(sa2)->sin6_addr)) {
4254 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
4259 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
4270 * compare two buffers with mask.
4274 * bits: Number of bits to compare
4280 key_bbcmp(const void *a1, const void *a2, u_int bits)
4282 const unsigned char *p1 = a1;
4283 const unsigned char *p2 = a2;
4285 /* XXX: This could be considerably faster if we compare a word
4286 * at a time, but it is complicated on LSB Endian machines */
4288 /* Handle null pointers */
4289 if (p1 == NULL || p2 == NULL)
4299 u_int8_t mask = ~((1<<(8-bits))-1);
4300 if ((*p1 & mask) != (*p2 & mask))
4303 return 1; /* Match! */
4307 key_flush_spd(time_t now)
4309 static u_int16_t sptree_scangen = 0;
4310 u_int16_t gen = sptree_scangen++;
4311 struct secpolicy *sp;
4315 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4318 LIST_FOREACH(sp, &V_sptree[dir], chain) {
4319 if (sp->scangen == gen) /* previously handled */
4322 if (sp->state == IPSEC_SPSTATE_DEAD &&
4325 * Ensure that we only decrease refcnt once,
4326 * when we're the last consumer.
4327 * Directly call SP_DELREF/key_delsp instead
4328 * of KEY_FREESP to avoid unlocking/relocking
4329 * SPTREE_LOCK before key_delsp: may refcnt
4330 * be increased again during that time ?
4331 * NB: also clean entries created by
4339 if (sp->lifetime == 0 && sp->validtime == 0)
4341 if ((sp->lifetime && now - sp->created > sp->lifetime)
4342 || (sp->validtime && now - sp->lastused > sp->validtime)) {
4343 sp->state = IPSEC_SPSTATE_DEAD;
4354 key_flush_sad(time_t now)
4356 struct secashead *sah, *nextsah;
4357 struct secasvar *sav, *nextsav;
4361 LIST_FOREACH_SAFE(sah, &V_sahtree, chain, nextsah) {
4362 /* if sah has been dead, then delete it and process next sah. */
4363 if (sah->state == SADB_SASTATE_DEAD) {
4368 /* if LARVAL entry doesn't become MATURE, delete it. */
4369 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_LARVAL], chain, nextsav) {
4370 /* Need to also check refcnt for a larval SA ??? */
4371 if (now - sav->created > V_key_larval_lifetime)
4376 * check MATURE entry to start to send expire message
4379 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_MATURE], chain, nextsav) {
4380 /* we don't need to check. */
4381 if (sav->lft_s == NULL)
4385 if (sav->lft_c == NULL) {
4386 ipseclog((LOG_DEBUG,"%s: there is no CURRENT "
4387 "time, why?\n", __func__));
4391 /* check SOFT lifetime */
4392 if (sav->lft_s->addtime != 0 &&
4393 now - sav->created > sav->lft_s->addtime) {
4394 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4396 * Actually, only send expire message if
4397 * SA has been used, as it was done before,
4398 * but should we always send such message,
4399 * and let IKE daemon decide if it should be
4400 * renegotiated or not ?
4401 * XXX expire message will actually NOT be
4402 * sent if SA is only used after soft
4403 * lifetime has been reached, see below
4406 if (sav->lft_c->usetime != 0)
4409 /* check SOFT lifetime by bytes */
4411 * XXX I don't know the way to delete this SA
4412 * when new SA is installed. Caution when it's
4413 * installed too big lifetime by time.
4415 else if (sav->lft_s->bytes != 0 &&
4416 sav->lft_s->bytes < sav->lft_c->bytes) {
4418 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4420 * XXX If we keep to send expire
4421 * message in the status of
4422 * DYING. Do remove below code.
4428 /* check DYING entry to change status to DEAD. */
4429 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DYING], chain, nextsav) {
4430 /* we don't need to check. */
4431 if (sav->lft_h == NULL)
4435 if (sav->lft_c == NULL) {
4436 ipseclog((LOG_DEBUG, "%s: there is no CURRENT "
4437 "time, why?\n", __func__));
4441 if (sav->lft_h->addtime != 0 &&
4442 now - sav->created > sav->lft_h->addtime) {
4443 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4446 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4447 else if (sav->lft_s != NULL
4448 && sav->lft_s->addtime != 0
4449 && now - sav->created > sav->lft_s->addtime) {
4451 * XXX: should be checked to be
4452 * installed the valid SA.
4456 * If there is no SA then sending
4462 /* check HARD lifetime by bytes */
4463 else if (sav->lft_h->bytes != 0 &&
4464 sav->lft_h->bytes < sav->lft_c->bytes) {
4465 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4470 /* delete entry in DEAD */
4471 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DEAD], chain, nextsav) {
4473 if (sav->state != SADB_SASTATE_DEAD) {
4474 ipseclog((LOG_DEBUG, "%s: invalid sav->state "
4475 "(queue: %d SA: %d): kill it anyway\n",
4477 SADB_SASTATE_DEAD, sav->state));
4480 * do not call key_freesav() here.
4481 * sav should already be freed, and sav->refcnt
4482 * shows other references to sav
4483 * (such as from SPD).
4491 key_flush_acq(time_t now)
4493 struct secacq *acq, *nextacq;
4497 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
4498 nextacq = LIST_NEXT(acq, chain);
4499 if (now - acq->created > V_key_blockacq_lifetime
4500 && __LIST_CHAINED(acq)) {
4501 LIST_REMOVE(acq, chain);
4502 free(acq, M_IPSEC_SAQ);
4509 key_flush_spacq(time_t now)
4511 struct secspacq *acq, *nextacq;
4515 for (acq = LIST_FIRST(&V_spacqtree); acq != NULL; acq = nextacq) {
4516 nextacq = LIST_NEXT(acq, chain);
4517 if (now - acq->created > V_key_blockacq_lifetime
4518 && __LIST_CHAINED(acq)) {
4519 LIST_REMOVE(acq, chain);
4520 free(acq, M_IPSEC_SAQ);
4528 * scanning SPD and SAD to check status for each entries,
4529 * and do to remove or to expire.
4530 * XXX: year 2038 problem may remain.
4533 key_timehandler(void)
4535 VNET_ITERATOR_DECL(vnet_iter);
4536 time_t now = time_second;
4538 VNET_LIST_RLOCK_NOSLEEP();
4539 VNET_FOREACH(vnet_iter) {
4540 CURVNET_SET(vnet_iter);
4544 key_flush_spacq(now);
4547 VNET_LIST_RUNLOCK_NOSLEEP();
4549 #ifndef IPSEC_DEBUG2
4550 /* do exchange to tick time !! */
4551 (void)timeout((void *)key_timehandler, (void *)0, hz);
4552 #endif /* IPSEC_DEBUG2 */
4560 key_randomfill(&value, sizeof(value));
4565 key_randomfill(p, l)
4571 static int warn = 1;
4574 n = (size_t)read_random(p, (u_int)l);
4578 bcopy(&v, (u_int8_t *)p + n,
4579 l - n < sizeof(v) ? l - n : sizeof(v));
4583 printf("WARNING: pseudo-random number generator "
4584 "used for IPsec processing\n");
4591 * map SADB_SATYPE_* to IPPROTO_*.
4592 * if satype == SADB_SATYPE then satype is mapped to ~0.
4594 * 0: invalid satype.
4597 key_satype2proto(u_int8_t satype)
4600 case SADB_SATYPE_UNSPEC:
4601 return IPSEC_PROTO_ANY;
4602 case SADB_SATYPE_AH:
4604 case SADB_SATYPE_ESP:
4606 case SADB_X_SATYPE_IPCOMP:
4607 return IPPROTO_IPCOMP;
4608 case SADB_X_SATYPE_TCPSIGNATURE:
4617 * map IPPROTO_* to SADB_SATYPE_*
4619 * 0: invalid protocol type.
4622 key_proto2satype(u_int16_t proto)
4626 return SADB_SATYPE_AH;
4628 return SADB_SATYPE_ESP;
4629 case IPPROTO_IPCOMP:
4630 return SADB_X_SATYPE_IPCOMP;
4632 return SADB_X_SATYPE_TCPSIGNATURE;
4641 * SADB_GETSPI processing is to receive
4642 * <base, (SA2), src address, dst address, (SPI range)>
4643 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4644 * tree with the status of LARVAL, and send
4645 * <base, SA(*), address(SD)>
4648 * IN: mhp: pointer to the pointer to each header.
4649 * OUT: NULL if fail.
4650 * other if success, return pointer to the message to send.
4653 key_getspi(so, m, mhp)
4656 const struct sadb_msghdr *mhp;
4658 struct sadb_address *src0, *dst0;
4659 struct secasindex saidx;
4660 struct secashead *newsah;
4661 struct secasvar *newsav;
4668 IPSEC_ASSERT(so != NULL, ("null socket"));
4669 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4670 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4671 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4673 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4674 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4675 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4677 return key_senderror(so, m, EINVAL);
4679 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4680 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4681 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4683 return key_senderror(so, m, EINVAL);
4685 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4686 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4687 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4689 mode = IPSEC_MODE_ANY;
4693 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4694 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4696 /* map satype to proto */
4697 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4698 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4700 return key_senderror(so, m, EINVAL);
4704 * Make sure the port numbers are zero.
4705 * In case of NAT-T we will update them later if needed.
4707 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4709 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4710 sizeof(struct sockaddr_in))
4711 return key_senderror(so, m, EINVAL);
4712 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4715 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4716 sizeof(struct sockaddr_in6))
4717 return key_senderror(so, m, EINVAL);
4718 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4723 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4725 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4726 sizeof(struct sockaddr_in))
4727 return key_senderror(so, m, EINVAL);
4728 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4731 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4732 sizeof(struct sockaddr_in6))
4733 return key_senderror(so, m, EINVAL);
4734 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4740 /* XXX boundary check against sa_len */
4741 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4745 * Handle NAT-T info if present.
4746 * We made sure the port numbers are zero above, so we do
4747 * not have to worry in case we do not update them.
4749 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL)
4750 ipseclog((LOG_DEBUG, "%s: NAT-T OAi present\n", __func__));
4751 if (mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL)
4752 ipseclog((LOG_DEBUG, "%s: NAT-T OAr present\n", __func__));
4754 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
4755 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
4756 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
4757 struct sadb_x_nat_t_type *type;
4758 struct sadb_x_nat_t_port *sport, *dport;
4760 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
4761 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
4762 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
4763 ipseclog((LOG_DEBUG, "%s: invalid nat-t message "
4764 "passed.\n", __func__));
4765 return key_senderror(so, m, EINVAL);
4768 sport = (struct sadb_x_nat_t_port *)
4769 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
4770 dport = (struct sadb_x_nat_t_port *)
4771 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
4774 KEY_PORTTOSADDR(&saidx.src, sport->sadb_x_nat_t_port_port);
4776 KEY_PORTTOSADDR(&saidx.dst, dport->sadb_x_nat_t_port_port);
4780 /* SPI allocation */
4781 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4784 return key_senderror(so, m, EINVAL);
4786 /* get a SA index */
4787 if ((newsah = key_getsah(&saidx)) == NULL) {
4788 /* create a new SA index */
4789 if ((newsah = key_newsah(&saidx)) == NULL) {
4790 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
4791 return key_senderror(so, m, ENOBUFS);
4797 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4798 if (newsav == NULL) {
4799 /* XXX don't free new SA index allocated in above. */
4800 return key_senderror(so, m, error);
4804 newsav->spi = htonl(spi);
4806 /* delete the entry in acqtree */
4807 if (mhp->msg->sadb_msg_seq != 0) {
4809 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4810 /* reset counter in order to deletion by timehandler. */
4811 acq->created = time_second;
4817 struct mbuf *n, *nn;
4818 struct sadb_sa *m_sa;
4819 struct sadb_msg *newmsg;
4822 /* create new sadb_msg to reply. */
4823 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4824 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4826 MGETHDR(n, M_DONTWAIT, MT_DATA);
4828 MCLGET(n, M_DONTWAIT);
4829 if ((n->m_flags & M_EXT) == 0) {
4835 return key_senderror(so, m, ENOBUFS);
4841 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4842 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4844 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4845 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4846 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4847 m_sa->sadb_sa_spi = htonl(spi);
4848 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4850 IPSEC_ASSERT(off == len,
4851 ("length inconsistency (off %u len %u)", off, len));
4853 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4854 SADB_EXT_ADDRESS_DST);
4857 return key_senderror(so, m, ENOBUFS);
4860 if (n->m_len < sizeof(struct sadb_msg)) {
4861 n = m_pullup(n, sizeof(struct sadb_msg));
4863 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4866 n->m_pkthdr.len = 0;
4867 for (nn = n; nn; nn = nn->m_next)
4868 n->m_pkthdr.len += nn->m_len;
4870 newmsg = mtod(n, struct sadb_msg *);
4871 newmsg->sadb_msg_seq = newsav->seq;
4872 newmsg->sadb_msg_errno = 0;
4873 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4876 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4881 * allocating new SPI
4882 * called by key_getspi().
4888 key_do_getnewspi(spirange, saidx)
4889 struct sadb_spirange *spirange;
4890 struct secasindex *saidx;
4894 int count = V_key_spi_trycnt;
4896 /* set spi range to allocate */
4897 if (spirange != NULL) {
4898 min = spirange->sadb_spirange_min;
4899 max = spirange->sadb_spirange_max;
4901 min = V_key_spi_minval;
4902 max = V_key_spi_maxval;
4904 /* IPCOMP needs 2-byte SPI */
4905 if (saidx->proto == IPPROTO_IPCOMP) {
4912 t = min; min = max; max = t;
4917 if (key_checkspidup(saidx, min) != NULL) {
4918 ipseclog((LOG_DEBUG, "%s: SPI %u exists already.\n",
4923 count--; /* taking one cost. */
4931 /* when requesting to allocate spi ranged */
4933 /* generate pseudo-random SPI value ranged. */
4934 newspi = min + (key_random() % (max - min + 1));
4936 if (key_checkspidup(saidx, newspi) == NULL)
4940 if (count == 0 || newspi == 0) {
4941 ipseclog((LOG_DEBUG, "%s: to allocate spi is failed.\n",
4948 keystat.getspi_count =
4949 (keystat.getspi_count + V_key_spi_trycnt - count) / 2;
4955 * SADB_UPDATE processing
4957 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4958 * key(AE), (identity(SD),) (sensitivity)>
4959 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4961 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4962 * (identity(SD),) (sensitivity)>
4965 * m will always be freed.
4968 key_update(so, m, mhp)
4971 const struct sadb_msghdr *mhp;
4973 struct sadb_sa *sa0;
4974 struct sadb_address *src0, *dst0;
4976 struct sadb_x_nat_t_type *type;
4977 struct sadb_x_nat_t_port *sport, *dport;
4978 struct sadb_address *iaddr, *raddr;
4979 struct sadb_x_nat_t_frag *frag;
4981 struct secasindex saidx;
4982 struct secashead *sah;
4983 struct secasvar *sav;
4989 IPSEC_ASSERT(so != NULL, ("null socket"));
4990 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4991 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4992 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4994 /* map satype to proto */
4995 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4996 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4998 return key_senderror(so, m, EINVAL);
5001 if (mhp->ext[SADB_EXT_SA] == NULL ||
5002 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5003 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5004 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5005 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5006 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5007 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5008 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5009 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5010 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5011 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5012 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5014 return key_senderror(so, m, EINVAL);
5016 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5017 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5018 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5019 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5021 return key_senderror(so, m, EINVAL);
5023 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5024 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5025 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5027 mode = IPSEC_MODE_ANY;
5030 /* XXX boundary checking for other extensions */
5032 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5033 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5034 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5036 /* XXX boundary check against sa_len */
5037 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5040 * Make sure the port numbers are zero.
5041 * In case of NAT-T we will update them later if needed.
5043 KEY_PORTTOSADDR(&saidx.src, 0);
5044 KEY_PORTTOSADDR(&saidx.dst, 0);
5048 * Handle NAT-T info if present.
5050 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
5051 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5052 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5054 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
5055 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5056 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5057 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5059 return key_senderror(so, m, EINVAL);
5062 type = (struct sadb_x_nat_t_type *)
5063 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5064 sport = (struct sadb_x_nat_t_port *)
5065 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5066 dport = (struct sadb_x_nat_t_port *)
5067 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5072 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
5073 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
5074 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
5075 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
5076 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5078 return key_senderror(so, m, EINVAL);
5080 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5081 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5082 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
5084 iaddr = raddr = NULL;
5086 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5087 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5088 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5090 return key_senderror(so, m, EINVAL);
5092 frag = (struct sadb_x_nat_t_frag *)
5093 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5099 /* get a SA header */
5100 if ((sah = key_getsah(&saidx)) == NULL) {
5101 ipseclog((LOG_DEBUG, "%s: no SA index found.\n", __func__));
5102 return key_senderror(so, m, ENOENT);
5105 /* set spidx if there */
5107 error = key_setident(sah, m, mhp);
5109 return key_senderror(so, m, error);
5111 /* find a SA with sequence number. */
5112 #ifdef IPSEC_DOSEQCHECK
5113 if (mhp->msg->sadb_msg_seq != 0
5114 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
5115 ipseclog((LOG_DEBUG, "%s: no larval SA with sequence %u "
5116 "exists.\n", __func__, mhp->msg->sadb_msg_seq));
5117 return key_senderror(so, m, ENOENT);
5121 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5124 ipseclog((LOG_DEBUG, "%s: no such a SA found (spi:%u)\n",
5125 __func__, (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5126 return key_senderror(so, m, EINVAL);
5130 /* validity check */
5131 if (sav->sah->saidx.proto != proto) {
5132 ipseclog((LOG_DEBUG, "%s: protocol mismatched "
5133 "(DB=%u param=%u)\n", __func__,
5134 sav->sah->saidx.proto, proto));
5135 return key_senderror(so, m, EINVAL);
5137 #ifdef IPSEC_DOSEQCHECK
5138 if (sav->spi != sa0->sadb_sa_spi) {
5139 ipseclog((LOG_DEBUG, "%s: SPI mismatched (DB:%u param:%u)\n",
5141 (u_int32_t)ntohl(sav->spi),
5142 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5143 return key_senderror(so, m, EINVAL);
5146 if (sav->pid != mhp->msg->sadb_msg_pid) {
5147 ipseclog((LOG_DEBUG, "%s: pid mismatched (DB:%u param:%u)\n",
5148 __func__, sav->pid, mhp->msg->sadb_msg_pid));
5149 return key_senderror(so, m, EINVAL);
5152 /* copy sav values */
5153 error = key_setsaval(sav, m, mhp);
5156 return key_senderror(so, m, error);
5161 * Handle more NAT-T info if present,
5162 * now that we have a sav to fill.
5165 sav->natt_type = type->sadb_x_nat_t_type_type;
5168 KEY_PORTTOSADDR(&sav->sah->saidx.src,
5169 sport->sadb_x_nat_t_port_port);
5171 KEY_PORTTOSADDR(&sav->sah->saidx.dst,
5172 dport->sadb_x_nat_t_port_port);
5176 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5177 * We should actually check for a minimum MTU here, if we
5178 * want to support it in ip_output.
5181 sav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5185 /* check SA values to be mature. */
5186 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
5188 return key_senderror(so, m, 0);
5194 /* set msg buf from mhp */
5195 n = key_getmsgbuf_x1(m, mhp);
5197 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5198 return key_senderror(so, m, ENOBUFS);
5202 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5207 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
5208 * only called by key_update().
5211 * others : found, pointer to a SA.
5213 #ifdef IPSEC_DOSEQCHECK
5214 static struct secasvar *
5215 key_getsavbyseq(sah, seq)
5216 struct secashead *sah;
5219 struct secasvar *sav;
5222 state = SADB_SASTATE_LARVAL;
5224 /* search SAD with sequence number ? */
5225 LIST_FOREACH(sav, &sah->savtree[state], chain) {
5227 KEY_CHKSASTATE(state, sav->state, __func__);
5229 if (sav->seq == seq) {
5231 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
5232 printf("DP %s cause refcnt++:%d SA:%p\n",
5233 __func__, sav->refcnt, sav));
5243 * SADB_ADD processing
5244 * add an entry to SA database, when received
5245 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5246 * key(AE), (identity(SD),) (sensitivity)>
5249 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5250 * (identity(SD),) (sensitivity)>
5253 * IGNORE identity and sensitivity messages.
5255 * m will always be freed.
5261 const struct sadb_msghdr *mhp;
5263 struct sadb_sa *sa0;
5264 struct sadb_address *src0, *dst0;
5266 struct sadb_x_nat_t_type *type;
5267 struct sadb_address *iaddr, *raddr;
5268 struct sadb_x_nat_t_frag *frag;
5270 struct secasindex saidx;
5271 struct secashead *newsah;
5272 struct secasvar *newsav;
5278 IPSEC_ASSERT(so != NULL, ("null socket"));
5279 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5280 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5281 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5283 /* map satype to proto */
5284 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5285 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5287 return key_senderror(so, m, EINVAL);
5290 if (mhp->ext[SADB_EXT_SA] == NULL ||
5291 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5292 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5293 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5294 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5295 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5296 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5297 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5298 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5299 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5300 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5301 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5303 return key_senderror(so, m, EINVAL);
5305 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5306 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5307 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5309 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5311 return key_senderror(so, m, EINVAL);
5313 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5314 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5315 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5317 mode = IPSEC_MODE_ANY;
5321 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5322 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5323 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5325 /* XXX boundary check against sa_len */
5326 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5329 * Make sure the port numbers are zero.
5330 * In case of NAT-T we will update them later if needed.
5332 KEY_PORTTOSADDR(&saidx.src, 0);
5333 KEY_PORTTOSADDR(&saidx.dst, 0);
5337 * Handle NAT-T info if present.
5339 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
5340 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5341 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5342 struct sadb_x_nat_t_port *sport, *dport;
5344 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
5345 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5346 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5347 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5349 return key_senderror(so, m, EINVAL);
5352 type = (struct sadb_x_nat_t_type *)
5353 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5354 sport = (struct sadb_x_nat_t_port *)
5355 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5356 dport = (struct sadb_x_nat_t_port *)
5357 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5360 KEY_PORTTOSADDR(&saidx.src,
5361 sport->sadb_x_nat_t_port_port);
5363 KEY_PORTTOSADDR(&saidx.dst,
5364 dport->sadb_x_nat_t_port_port);
5368 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
5369 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
5370 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
5371 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
5372 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5374 return key_senderror(so, m, EINVAL);
5376 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5377 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5378 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
5380 iaddr = raddr = NULL;
5382 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5383 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5384 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5386 return key_senderror(so, m, EINVAL);
5388 frag = (struct sadb_x_nat_t_frag *)
5389 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5395 /* get a SA header */
5396 if ((newsah = key_getsah(&saidx)) == NULL) {
5397 /* create a new SA header */
5398 if ((newsah = key_newsah(&saidx)) == NULL) {
5399 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
5400 return key_senderror(so, m, ENOBUFS);
5404 /* set spidx if there */
5406 error = key_setident(newsah, m, mhp);
5408 return key_senderror(so, m, error);
5411 /* create new SA entry. */
5412 /* We can create new SA only if SPI is differenct. */
5414 newsav = key_getsavbyspi(newsah, sa0->sadb_sa_spi);
5416 if (newsav != NULL) {
5417 ipseclog((LOG_DEBUG, "%s: SA already exists.\n", __func__));
5418 return key_senderror(so, m, EEXIST);
5420 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
5421 if (newsav == NULL) {
5422 return key_senderror(so, m, error);
5427 * Handle more NAT-T info if present,
5428 * now that we have a sav to fill.
5431 newsav->natt_type = type->sadb_x_nat_t_type_type;
5435 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5436 * We should actually check for a minimum MTU here, if we
5437 * want to support it in ip_output.
5440 newsav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5444 /* check SA values to be mature. */
5445 if ((error = key_mature(newsav)) != 0) {
5446 KEY_FREESAV(&newsav);
5447 return key_senderror(so, m, error);
5451 * don't call key_freesav() here, as we would like to keep the SA
5452 * in the database on success.
5458 /* set msg buf from mhp */
5459 n = key_getmsgbuf_x1(m, mhp);
5461 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5462 return key_senderror(so, m, ENOBUFS);
5466 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5472 key_setident(sah, m, mhp)
5473 struct secashead *sah;
5475 const struct sadb_msghdr *mhp;
5477 const struct sadb_ident *idsrc, *iddst;
5478 int idsrclen, iddstlen;
5480 IPSEC_ASSERT(sah != NULL, ("null secashead"));
5481 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5482 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5483 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5485 /* don't make buffer if not there */
5486 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
5487 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5493 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
5494 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5495 ipseclog((LOG_DEBUG, "%s: invalid identity.\n", __func__));
5499 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
5500 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
5501 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
5502 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
5504 /* validity check */
5505 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
5506 ipseclog((LOG_DEBUG, "%s: ident type mismatch.\n", __func__));
5510 switch (idsrc->sadb_ident_type) {
5511 case SADB_IDENTTYPE_PREFIX:
5512 case SADB_IDENTTYPE_FQDN:
5513 case SADB_IDENTTYPE_USERFQDN:
5515 /* XXX do nothing */
5521 /* make structure */
5522 sah->idents = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5523 if (sah->idents == NULL) {
5524 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5527 sah->identd = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5528 if (sah->identd == NULL) {
5529 free(sah->idents, M_IPSEC_MISC);
5531 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5534 sah->idents->type = idsrc->sadb_ident_type;
5535 sah->idents->id = idsrc->sadb_ident_id;
5537 sah->identd->type = iddst->sadb_ident_type;
5538 sah->identd->id = iddst->sadb_ident_id;
5544 * m will not be freed on return.
5545 * it is caller's responsibility to free the result.
5547 static struct mbuf *
5548 key_getmsgbuf_x1(m, mhp)
5550 const struct sadb_msghdr *mhp;
5554 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5555 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5556 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5558 /* create new sadb_msg to reply. */
5559 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5560 SADB_EXT_SA, SADB_X_EXT_SA2,
5561 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5562 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5563 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5567 if (n->m_len < sizeof(struct sadb_msg)) {
5568 n = m_pullup(n, sizeof(struct sadb_msg));
5572 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5573 mtod(n, struct sadb_msg *)->sadb_msg_len =
5574 PFKEY_UNIT64(n->m_pkthdr.len);
5579 static int key_delete_all __P((struct socket *, struct mbuf *,
5580 const struct sadb_msghdr *, u_int16_t));
5583 * SADB_DELETE processing
5585 * <base, SA(*), address(SD)>
5586 * from the ikmpd, and set SADB_SASTATE_DEAD,
5588 * <base, SA(*), address(SD)>
5591 * m will always be freed.
5594 key_delete(so, m, mhp)
5597 const struct sadb_msghdr *mhp;
5599 struct sadb_sa *sa0;
5600 struct sadb_address *src0, *dst0;
5601 struct secasindex saidx;
5602 struct secashead *sah;
5603 struct secasvar *sav = NULL;
5606 IPSEC_ASSERT(so != NULL, ("null socket"));
5607 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5608 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5609 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5611 /* map satype to proto */
5612 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5613 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5615 return key_senderror(so, m, EINVAL);
5618 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5619 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5620 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5622 return key_senderror(so, m, EINVAL);
5625 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5626 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5627 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5629 return key_senderror(so, m, EINVAL);
5632 if (mhp->ext[SADB_EXT_SA] == NULL) {
5634 * Caller wants us to delete all non-LARVAL SAs
5635 * that match the src/dst. This is used during
5636 * IKE INITIAL-CONTACT.
5638 ipseclog((LOG_DEBUG, "%s: doing delete all.\n", __func__));
5639 return key_delete_all(so, m, mhp, proto);
5640 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5641 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5643 return key_senderror(so, m, EINVAL);
5646 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5647 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5648 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5650 /* XXX boundary check against sa_len */
5651 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5654 * Make sure the port numbers are zero.
5655 * In case of NAT-T we will update them later if needed.
5657 KEY_PORTTOSADDR(&saidx.src, 0);
5658 KEY_PORTTOSADDR(&saidx.dst, 0);
5662 * Handle NAT-T info if present.
5664 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5665 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5666 struct sadb_x_nat_t_port *sport, *dport;
5668 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5669 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5670 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5672 return key_senderror(so, m, EINVAL);
5675 sport = (struct sadb_x_nat_t_port *)
5676 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5677 dport = (struct sadb_x_nat_t_port *)
5678 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5681 KEY_PORTTOSADDR(&saidx.src,
5682 sport->sadb_x_nat_t_port_port);
5684 KEY_PORTTOSADDR(&saidx.dst,
5685 dport->sadb_x_nat_t_port_port);
5689 /* get a SA header */
5691 LIST_FOREACH(sah, &V_sahtree, chain) {
5692 if (sah->state == SADB_SASTATE_DEAD)
5694 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5697 /* get a SA with SPI. */
5698 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5704 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5705 return key_senderror(so, m, ENOENT);
5708 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5714 struct sadb_msg *newmsg;
5716 /* create new sadb_msg to reply. */
5717 /* XXX-BZ NAT-T extensions? */
5718 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5719 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5721 return key_senderror(so, m, ENOBUFS);
5723 if (n->m_len < sizeof(struct sadb_msg)) {
5724 n = m_pullup(n, sizeof(struct sadb_msg));
5726 return key_senderror(so, m, ENOBUFS);
5728 newmsg = mtod(n, struct sadb_msg *);
5729 newmsg->sadb_msg_errno = 0;
5730 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5733 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5738 * delete all SAs for src/dst. Called from key_delete().
5741 key_delete_all(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp,
5744 struct sadb_address *src0, *dst0;
5745 struct secasindex saidx;
5746 struct secashead *sah;
5747 struct secasvar *sav, *nextsav;
5748 u_int stateidx, state;
5750 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5751 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5753 /* XXX boundary check against sa_len */
5754 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5757 * Make sure the port numbers are zero.
5758 * In case of NAT-T we will update them later if needed.
5760 KEY_PORTTOSADDR(&saidx.src, 0);
5761 KEY_PORTTOSADDR(&saidx.dst, 0);
5765 * Handle NAT-T info if present.
5768 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5769 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5770 struct sadb_x_nat_t_port *sport, *dport;
5772 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5773 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5774 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5776 return key_senderror(so, m, EINVAL);
5779 sport = (struct sadb_x_nat_t_port *)
5780 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5781 dport = (struct sadb_x_nat_t_port *)
5782 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5785 KEY_PORTTOSADDR(&saidx.src,
5786 sport->sadb_x_nat_t_port_port);
5788 KEY_PORTTOSADDR(&saidx.dst,
5789 dport->sadb_x_nat_t_port_port);
5794 LIST_FOREACH(sah, &V_sahtree, chain) {
5795 if (sah->state == SADB_SASTATE_DEAD)
5797 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5800 /* Delete all non-LARVAL SAs. */
5802 stateidx < _ARRAYLEN(saorder_state_alive);
5804 state = saorder_state_alive[stateidx];
5805 if (state == SADB_SASTATE_LARVAL)
5807 for (sav = LIST_FIRST(&sah->savtree[state]);
5808 sav != NULL; sav = nextsav) {
5809 nextsav = LIST_NEXT(sav, chain);
5811 if (sav->state != state) {
5812 ipseclog((LOG_DEBUG, "%s: invalid "
5813 "sav->state (queue %d SA %d)\n",
5814 __func__, state, sav->state));
5818 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5826 struct sadb_msg *newmsg;
5828 /* create new sadb_msg to reply. */
5829 /* XXX-BZ NAT-T extensions? */
5830 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5831 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5833 return key_senderror(so, m, ENOBUFS);
5835 if (n->m_len < sizeof(struct sadb_msg)) {
5836 n = m_pullup(n, sizeof(struct sadb_msg));
5838 return key_senderror(so, m, ENOBUFS);
5840 newmsg = mtod(n, struct sadb_msg *);
5841 newmsg->sadb_msg_errno = 0;
5842 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5845 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5850 * SADB_GET processing
5852 * <base, SA(*), address(SD)>
5853 * from the ikmpd, and get a SP and a SA to respond,
5855 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5856 * (identity(SD),) (sensitivity)>
5859 * m will always be freed.
5865 const struct sadb_msghdr *mhp;
5867 struct sadb_sa *sa0;
5868 struct sadb_address *src0, *dst0;
5869 struct secasindex saidx;
5870 struct secashead *sah;
5871 struct secasvar *sav = NULL;
5874 IPSEC_ASSERT(so != NULL, ("null socket"));
5875 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5876 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5877 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5879 /* map satype to proto */
5880 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5881 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5883 return key_senderror(so, m, EINVAL);
5886 if (mhp->ext[SADB_EXT_SA] == NULL ||
5887 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5888 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5889 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5891 return key_senderror(so, m, EINVAL);
5893 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5894 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5895 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5896 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5898 return key_senderror(so, m, EINVAL);
5901 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5902 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5903 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5905 /* XXX boundary check against sa_len */
5906 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5909 * Make sure the port numbers are zero.
5910 * In case of NAT-T we will update them later if needed.
5912 KEY_PORTTOSADDR(&saidx.src, 0);
5913 KEY_PORTTOSADDR(&saidx.dst, 0);
5917 * Handle NAT-T info if present.
5920 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5921 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5922 struct sadb_x_nat_t_port *sport, *dport;
5924 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5925 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5926 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5928 return key_senderror(so, m, EINVAL);
5931 sport = (struct sadb_x_nat_t_port *)
5932 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5933 dport = (struct sadb_x_nat_t_port *)
5934 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5937 KEY_PORTTOSADDR(&saidx.src,
5938 sport->sadb_x_nat_t_port_port);
5940 KEY_PORTTOSADDR(&saidx.dst,
5941 dport->sadb_x_nat_t_port_port);
5945 /* get a SA header */
5947 LIST_FOREACH(sah, &V_sahtree, chain) {
5948 if (sah->state == SADB_SASTATE_DEAD)
5950 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5953 /* get a SA with SPI. */
5954 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5960 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5961 return key_senderror(so, m, ENOENT);
5968 /* map proto to satype */
5969 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5970 ipseclog((LOG_DEBUG, "%s: there was invalid proto in SAD.\n",
5972 return key_senderror(so, m, EINVAL);
5975 /* create new sadb_msg to reply. */
5976 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5977 mhp->msg->sadb_msg_pid);
5979 return key_senderror(so, m, ENOBUFS);
5982 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5986 /* XXX make it sysctl-configurable? */
5988 key_getcomb_setlifetime(comb)
5989 struct sadb_comb *comb;
5992 comb->sadb_comb_soft_allocations = 1;
5993 comb->sadb_comb_hard_allocations = 1;
5994 comb->sadb_comb_soft_bytes = 0;
5995 comb->sadb_comb_hard_bytes = 0;
5996 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5997 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5998 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5999 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
6003 * XXX reorder combinations by preference
6004 * XXX no idea if the user wants ESP authentication or not
6006 static struct mbuf *
6009 struct sadb_comb *comb;
6010 struct enc_xform *algo;
6011 struct mbuf *result = NULL, *m, *n;
6015 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6018 for (i = 1; i <= SADB_EALG_MAX; i++) {
6019 algo = esp_algorithm_lookup(i);
6023 /* discard algorithms with key size smaller than system min */
6024 if (_BITS(algo->maxkey) < V_ipsec_esp_keymin)
6026 if (_BITS(algo->minkey) < V_ipsec_esp_keymin)
6027 encmin = V_ipsec_esp_keymin;
6029 encmin = _BITS(algo->minkey);
6031 if (V_ipsec_esp_auth)
6032 m = key_getcomb_ah();
6034 IPSEC_ASSERT(l <= MLEN,
6035 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6036 MGET(m, M_DONTWAIT, MT_DATA);
6041 bzero(mtod(m, caddr_t), m->m_len);
6048 for (n = m; n; n = n->m_next)
6050 IPSEC_ASSERT((totlen % l) == 0, ("totlen=%u, l=%u", totlen, l));
6052 for (off = 0; off < totlen; off += l) {
6053 n = m_pulldown(m, off, l, &o);
6055 /* m is already freed */
6058 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
6059 bzero(comb, sizeof(*comb));
6060 key_getcomb_setlifetime(comb);
6061 comb->sadb_comb_encrypt = i;
6062 comb->sadb_comb_encrypt_minbits = encmin;
6063 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
6082 const struct auth_hash *ah,
6088 *min = *max = ah->keysize;
6089 if (ah->keysize == 0) {
6091 * Transform takes arbitrary key size but algorithm
6092 * key size is restricted. Enforce this here.
6095 case SADB_X_AALG_MD5: *min = *max = 16; break;
6096 case SADB_X_AALG_SHA: *min = *max = 20; break;
6097 case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
6098 case SADB_X_AALG_SHA2_256: *min = *max = 32; break;
6099 case SADB_X_AALG_SHA2_384: *min = *max = 48; break;
6100 case SADB_X_AALG_SHA2_512: *min = *max = 64; break;
6102 DPRINTF(("%s: unknown AH algorithm %u\n",
6110 * XXX reorder combinations by preference
6112 static struct mbuf *
6115 struct sadb_comb *comb;
6116 struct auth_hash *algo;
6118 u_int16_t minkeysize, maxkeysize;
6120 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6123 for (i = 1; i <= SADB_AALG_MAX; i++) {
6125 /* we prefer HMAC algorithms, not old algorithms */
6126 if (i != SADB_AALG_SHA1HMAC &&
6127 i != SADB_AALG_MD5HMAC &&
6128 i != SADB_X_AALG_SHA2_256 &&
6129 i != SADB_X_AALG_SHA2_384 &&
6130 i != SADB_X_AALG_SHA2_512)
6133 algo = ah_algorithm_lookup(i);
6136 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
6137 /* discard algorithms with key size smaller than system min */
6138 if (_BITS(minkeysize) < V_ipsec_ah_keymin)
6142 IPSEC_ASSERT(l <= MLEN,
6143 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6144 MGET(m, M_DONTWAIT, MT_DATA);
6151 M_PREPEND(m, l, M_DONTWAIT);
6155 comb = mtod(m, struct sadb_comb *);
6156 bzero(comb, sizeof(*comb));
6157 key_getcomb_setlifetime(comb);
6158 comb->sadb_comb_auth = i;
6159 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
6160 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
6167 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
6168 * XXX reorder combinations by preference
6170 static struct mbuf *
6171 key_getcomb_ipcomp()
6173 struct sadb_comb *comb;
6174 struct comp_algo *algo;
6177 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6180 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
6181 algo = ipcomp_algorithm_lookup(i);
6186 IPSEC_ASSERT(l <= MLEN,
6187 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6188 MGET(m, M_DONTWAIT, MT_DATA);
6195 M_PREPEND(m, l, M_DONTWAIT);
6199 comb = mtod(m, struct sadb_comb *);
6200 bzero(comb, sizeof(*comb));
6201 key_getcomb_setlifetime(comb);
6202 comb->sadb_comb_encrypt = i;
6203 /* what should we set into sadb_comb_*_{min,max}bits? */
6210 * XXX no way to pass mode (transport/tunnel) to userland
6211 * XXX replay checking?
6212 * XXX sysctl interface to ipsec_{ah,esp}_keymin
6214 static struct mbuf *
6216 const struct secasindex *saidx;
6218 struct sadb_prop *prop;
6220 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
6223 switch (saidx->proto) {
6225 m = key_getcomb_esp();
6228 m = key_getcomb_ah();
6230 case IPPROTO_IPCOMP:
6231 m = key_getcomb_ipcomp();
6239 M_PREPEND(m, l, M_DONTWAIT);
6244 for (n = m; n; n = n->m_next)
6247 prop = mtod(m, struct sadb_prop *);
6248 bzero(prop, sizeof(*prop));
6249 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
6250 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
6251 prop->sadb_prop_replay = 32; /* XXX */
6257 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
6259 * <base, SA, address(SD), (address(P)), x_policy,
6260 * (identity(SD),) (sensitivity,) proposal>
6261 * to KMD, and expect to receive
6262 * <base> with SADB_ACQUIRE if error occured,
6264 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
6265 * from KMD by PF_KEY.
6267 * XXX x_policy is outside of RFC2367 (KAME extension).
6268 * XXX sensitivity is not supported.
6269 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
6270 * see comment for key_getcomb_ipcomp().
6274 * others: error number
6277 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
6279 struct mbuf *result = NULL, *m;
6280 struct secacq *newacq;
6285 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
6286 satype = key_proto2satype(saidx->proto);
6287 IPSEC_ASSERT(satype != 0, ("null satype, protocol %u", saidx->proto));
6290 * We never do anything about acquirng SA. There is anather
6291 * solution that kernel blocks to send SADB_ACQUIRE message until
6292 * getting something message from IKEd. In later case, to be
6293 * managed with ACQUIRING list.
6295 /* Get an entry to check whether sending message or not. */
6296 if ((newacq = key_getacq(saidx)) != NULL) {
6297 if (V_key_blockacq_count < newacq->count) {
6298 /* reset counter and do send message. */
6301 /* increment counter and do nothing. */
6306 /* make new entry for blocking to send SADB_ACQUIRE. */
6307 if ((newacq = key_newacq(saidx)) == NULL)
6313 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
6321 * No SADB_X_EXT_NAT_T_* here: we do not know
6322 * anything related to NAT-T at this time.
6325 /* set sadb_address for saidx's. */
6326 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6327 &saidx->src.sa, FULLMASK, IPSEC_ULPROTO_ANY);
6334 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6335 &saidx->dst.sa, FULLMASK, IPSEC_ULPROTO_ANY);
6342 /* XXX proxy address (optional) */
6344 /* set sadb_x_policy */
6346 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
6354 /* XXX identity (optional) */
6356 if (idexttype && fqdn) {
6357 /* create identity extension (FQDN) */
6358 struct sadb_ident *id;
6361 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
6362 id = (struct sadb_ident *)p;
6363 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6364 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6365 id->sadb_ident_exttype = idexttype;
6366 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
6367 bcopy(fqdn, id + 1, fqdnlen);
6368 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
6372 /* create identity extension (USERFQDN) */
6373 struct sadb_ident *id;
6377 /* +1 for terminating-NUL */
6378 userfqdnlen = strlen(userfqdn) + 1;
6381 id = (struct sadb_ident *)p;
6382 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6383 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6384 id->sadb_ident_exttype = idexttype;
6385 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
6386 /* XXX is it correct? */
6387 if (curproc && curproc->p_cred)
6388 id->sadb_ident_id = curproc->p_cred->p_ruid;
6389 if (userfqdn && userfqdnlen)
6390 bcopy(userfqdn, id + 1, userfqdnlen);
6391 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
6395 /* XXX sensitivity (optional) */
6397 /* create proposal/combination extension */
6398 m = key_getprop(saidx);
6401 * spec conformant: always attach proposal/combination extension,
6402 * the problem is that we have no way to attach it for ipcomp,
6403 * due to the way sadb_comb is declared in RFC2367.
6412 * outside of spec; make proposal/combination extension optional.
6418 if ((result->m_flags & M_PKTHDR) == 0) {
6423 if (result->m_len < sizeof(struct sadb_msg)) {
6424 result = m_pullup(result, sizeof(struct sadb_msg));
6425 if (result == NULL) {
6431 result->m_pkthdr.len = 0;
6432 for (m = result; m; m = m->m_next)
6433 result->m_pkthdr.len += m->m_len;
6435 mtod(result, struct sadb_msg *)->sadb_msg_len =
6436 PFKEY_UNIT64(result->m_pkthdr.len);
6438 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6446 static struct secacq *
6447 key_newacq(const struct secasindex *saidx)
6449 struct secacq *newacq;
6452 newacq = malloc(sizeof(struct secacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6453 if (newacq == NULL) {
6454 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6459 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
6460 newacq->seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq);
6461 newacq->created = time_second;
6464 /* add to acqtree */
6466 LIST_INSERT_HEAD(&V_acqtree, newacq, chain);
6472 static struct secacq *
6473 key_getacq(const struct secasindex *saidx)
6478 LIST_FOREACH(acq, &V_acqtree, chain) {
6479 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
6487 static struct secacq *
6488 key_getacqbyseq(seq)
6494 LIST_FOREACH(acq, &V_acqtree, chain) {
6495 if (acq->seq == seq)
6503 static struct secspacq *
6505 struct secpolicyindex *spidx;
6507 struct secspacq *acq;
6510 acq = malloc(sizeof(struct secspacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6512 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6517 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
6518 acq->created = time_second;
6521 /* add to spacqtree */
6523 LIST_INSERT_HEAD(&V_spacqtree, acq, chain);
6529 static struct secspacq *
6531 struct secpolicyindex *spidx;
6533 struct secspacq *acq;
6536 LIST_FOREACH(acq, &V_spacqtree, chain) {
6537 if (key_cmpspidx_exactly(spidx, &acq->spidx)) {
6538 /* NB: return holding spacq_lock */
6548 * SADB_ACQUIRE processing,
6549 * in first situation, is receiving
6551 * from the ikmpd, and clear sequence of its secasvar entry.
6553 * In second situation, is receiving
6554 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6555 * from a user land process, and return
6556 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6559 * m will always be freed.
6562 key_acquire2(so, m, mhp)
6565 const struct sadb_msghdr *mhp;
6567 const struct sadb_address *src0, *dst0;
6568 struct secasindex saidx;
6569 struct secashead *sah;
6573 IPSEC_ASSERT(so != NULL, ("null socket"));
6574 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6575 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6576 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6579 * Error message from KMd.
6580 * We assume that if error was occured in IKEd, the length of PFKEY
6581 * message is equal to the size of sadb_msg structure.
6582 * We do not raise error even if error occured in this function.
6584 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
6587 /* check sequence number */
6588 if (mhp->msg->sadb_msg_seq == 0) {
6589 ipseclog((LOG_DEBUG, "%s: must specify sequence "
6590 "number.\n", __func__));
6595 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
6597 * the specified larval SA is already gone, or we got
6598 * a bogus sequence number. we can silently ignore it.
6604 /* reset acq counter in order to deletion by timehander. */
6605 acq->created = time_second;
6612 * This message is from user land.
6615 /* map satype to proto */
6616 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6617 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6619 return key_senderror(so, m, EINVAL);
6622 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
6623 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
6624 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
6626 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6628 return key_senderror(so, m, EINVAL);
6630 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
6631 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
6632 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
6634 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6636 return key_senderror(so, m, EINVAL);
6639 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
6640 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
6642 /* XXX boundary check against sa_len */
6643 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
6646 * Make sure the port numbers are zero.
6647 * In case of NAT-T we will update them later if needed.
6649 KEY_PORTTOSADDR(&saidx.src, 0);
6650 KEY_PORTTOSADDR(&saidx.dst, 0);
6654 * Handle NAT-T info if present.
6657 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
6658 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
6659 struct sadb_x_nat_t_port *sport, *dport;
6661 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
6662 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
6663 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
6665 return key_senderror(so, m, EINVAL);
6668 sport = (struct sadb_x_nat_t_port *)
6669 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
6670 dport = (struct sadb_x_nat_t_port *)
6671 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
6674 KEY_PORTTOSADDR(&saidx.src,
6675 sport->sadb_x_nat_t_port_port);
6677 KEY_PORTTOSADDR(&saidx.dst,
6678 dport->sadb_x_nat_t_port_port);
6682 /* get a SA index */
6684 LIST_FOREACH(sah, &V_sahtree, chain) {
6685 if (sah->state == SADB_SASTATE_DEAD)
6687 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
6692 ipseclog((LOG_DEBUG, "%s: a SA exists already.\n", __func__));
6693 return key_senderror(so, m, EEXIST);
6696 error = key_acquire(&saidx, NULL);
6698 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
6699 __func__, mhp->msg->sadb_msg_errno));
6700 return key_senderror(so, m, error);
6703 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6707 * SADB_REGISTER processing.
6708 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6711 * from the ikmpd, and register a socket to send PF_KEY messages,
6715 * If socket is detached, must free from regnode.
6717 * m will always be freed.
6720 key_register(so, m, mhp)
6723 const struct sadb_msghdr *mhp;
6725 struct secreg *reg, *newreg = 0;
6727 IPSEC_ASSERT(so != NULL, ("null socket"));
6728 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6729 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6730 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6732 /* check for invalid register message */
6733 if (mhp->msg->sadb_msg_satype >= sizeof(V_regtree)/sizeof(V_regtree[0]))
6734 return key_senderror(so, m, EINVAL);
6736 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6737 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6740 /* check whether existing or not */
6742 LIST_FOREACH(reg, &V_regtree[mhp->msg->sadb_msg_satype], chain) {
6743 if (reg->so == so) {
6745 ipseclog((LOG_DEBUG, "%s: socket exists already.\n",
6747 return key_senderror(so, m, EEXIST);
6751 /* create regnode */
6752 newreg = malloc(sizeof(struct secreg), M_IPSEC_SAR, M_NOWAIT|M_ZERO);
6753 if (newreg == NULL) {
6755 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6756 return key_senderror(so, m, ENOBUFS);
6760 ((struct keycb *)sotorawcb(so))->kp_registered++;
6762 /* add regnode to regtree. */
6763 LIST_INSERT_HEAD(&V_regtree[mhp->msg->sadb_msg_satype], newreg, chain);
6769 struct sadb_msg *newmsg;
6770 struct sadb_supported *sup;
6771 u_int len, alen, elen;
6774 struct sadb_alg *alg;
6776 /* create new sadb_msg to reply. */
6778 for (i = 1; i <= SADB_AALG_MAX; i++) {
6779 if (ah_algorithm_lookup(i))
6780 alen += sizeof(struct sadb_alg);
6783 alen += sizeof(struct sadb_supported);
6785 for (i = 1; i <= SADB_EALG_MAX; i++) {
6786 if (esp_algorithm_lookup(i))
6787 elen += sizeof(struct sadb_alg);
6790 elen += sizeof(struct sadb_supported);
6792 len = sizeof(struct sadb_msg) + alen + elen;
6795 return key_senderror(so, m, ENOBUFS);
6797 MGETHDR(n, M_DONTWAIT, MT_DATA);
6799 MCLGET(n, M_DONTWAIT);
6800 if ((n->m_flags & M_EXT) == 0) {
6806 return key_senderror(so, m, ENOBUFS);
6808 n->m_pkthdr.len = n->m_len = len;
6812 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6813 newmsg = mtod(n, struct sadb_msg *);
6814 newmsg->sadb_msg_errno = 0;
6815 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6816 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6818 /* for authentication algorithm */
6820 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6821 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6822 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6823 off += PFKEY_ALIGN8(sizeof(*sup));
6825 for (i = 1; i <= SADB_AALG_MAX; i++) {
6826 struct auth_hash *aalgo;
6827 u_int16_t minkeysize, maxkeysize;
6829 aalgo = ah_algorithm_lookup(i);
6832 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6833 alg->sadb_alg_id = i;
6834 alg->sadb_alg_ivlen = 0;
6835 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
6836 alg->sadb_alg_minbits = _BITS(minkeysize);
6837 alg->sadb_alg_maxbits = _BITS(maxkeysize);
6838 off += PFKEY_ALIGN8(sizeof(*alg));
6842 /* for encryption algorithm */
6844 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6845 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6846 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6847 off += PFKEY_ALIGN8(sizeof(*sup));
6849 for (i = 1; i <= SADB_EALG_MAX; i++) {
6850 struct enc_xform *ealgo;
6852 ealgo = esp_algorithm_lookup(i);
6855 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6856 alg->sadb_alg_id = i;
6857 alg->sadb_alg_ivlen = ealgo->blocksize;
6858 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
6859 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
6860 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6864 IPSEC_ASSERT(off == len,
6865 ("length assumption failed (off %u len %u)", off, len));
6868 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6873 * free secreg entry registered.
6874 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6877 key_freereg(struct socket *so)
6882 IPSEC_ASSERT(so != NULL, ("NULL so"));
6885 * check whether existing or not.
6886 * check all type of SA, because there is a potential that
6887 * one socket is registered to multiple type of SA.
6890 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6891 LIST_FOREACH(reg, &V_regtree[i], chain) {
6892 if (reg->so == so && __LIST_CHAINED(reg)) {
6893 LIST_REMOVE(reg, chain);
6894 free(reg, M_IPSEC_SAR);
6903 * SADB_EXPIRE processing
6905 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6907 * NOTE: We send only soft lifetime extension.
6910 * others : error number
6913 key_expire(struct secasvar *sav)
6917 struct mbuf *result = NULL, *m;
6920 struct sadb_lifetime *lt;
6922 /* XXX: Why do we lock ? */
6923 s = splnet(); /*called from softclock()*/
6925 IPSEC_ASSERT (sav != NULL, ("null sav"));
6926 IPSEC_ASSERT (sav->sah != NULL, ("null sa header"));
6928 /* set msg header */
6929 satype = key_proto2satype(sav->sah->saidx.proto);
6930 IPSEC_ASSERT(satype != 0, ("invalid proto, satype %u", satype));
6931 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6938 /* create SA extension */
6939 m = key_setsadbsa(sav);
6946 /* create SA extension */
6947 m = key_setsadbxsa2(sav->sah->saidx.mode,
6948 sav->replay ? sav->replay->count : 0,
6949 sav->sah->saidx.reqid);
6956 /* create lifetime extension (current and soft) */
6957 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6958 m = key_alloc_mbuf(len);
6959 if (!m || m->m_next) { /*XXX*/
6965 bzero(mtod(m, caddr_t), len);
6966 lt = mtod(m, struct sadb_lifetime *);
6967 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6968 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6969 lt->sadb_lifetime_allocations = sav->lft_c->allocations;
6970 lt->sadb_lifetime_bytes = sav->lft_c->bytes;
6971 lt->sadb_lifetime_addtime = sav->lft_c->addtime;
6972 lt->sadb_lifetime_usetime = sav->lft_c->usetime;
6973 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6974 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6975 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
6976 lt->sadb_lifetime_allocations = sav->lft_s->allocations;
6977 lt->sadb_lifetime_bytes = sav->lft_s->bytes;
6978 lt->sadb_lifetime_addtime = sav->lft_s->addtime;
6979 lt->sadb_lifetime_usetime = sav->lft_s->usetime;
6982 /* set sadb_address for source */
6983 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6984 &sav->sah->saidx.src.sa,
6985 FULLMASK, IPSEC_ULPROTO_ANY);
6992 /* set sadb_address for destination */
6993 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6994 &sav->sah->saidx.dst.sa,
6995 FULLMASK, IPSEC_ULPROTO_ANY);
7003 * XXX-BZ Handle NAT-T extensions here.
7006 if ((result->m_flags & M_PKTHDR) == 0) {
7011 if (result->m_len < sizeof(struct sadb_msg)) {
7012 result = m_pullup(result, sizeof(struct sadb_msg));
7013 if (result == NULL) {
7019 result->m_pkthdr.len = 0;
7020 for (m = result; m; m = m->m_next)
7021 result->m_pkthdr.len += m->m_len;
7023 mtod(result, struct sadb_msg *)->sadb_msg_len =
7024 PFKEY_UNIT64(result->m_pkthdr.len);
7027 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
7037 * SADB_FLUSH processing
7040 * from the ikmpd, and free all entries in secastree.
7044 * NOTE: to do is only marking SADB_SASTATE_DEAD.
7046 * m will always be freed.
7049 key_flush(so, m, mhp)
7052 const struct sadb_msghdr *mhp;
7054 struct sadb_msg *newmsg;
7055 struct secashead *sah, *nextsah;
7056 struct secasvar *sav, *nextsav;
7061 IPSEC_ASSERT(so != NULL, ("null socket"));
7062 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7063 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7065 /* map satype to proto */
7066 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7067 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7069 return key_senderror(so, m, EINVAL);
7072 /* no SATYPE specified, i.e. flushing all SA. */
7074 for (sah = LIST_FIRST(&V_sahtree);
7077 nextsah = LIST_NEXT(sah, chain);
7079 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7080 && proto != sah->saidx.proto)
7084 stateidx < _ARRAYLEN(saorder_state_alive);
7086 state = saorder_state_any[stateidx];
7087 for (sav = LIST_FIRST(&sah->savtree[state]);
7091 nextsav = LIST_NEXT(sav, chain);
7093 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
7098 sah->state = SADB_SASTATE_DEAD;
7102 if (m->m_len < sizeof(struct sadb_msg) ||
7103 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
7104 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
7105 return key_senderror(so, m, ENOBUFS);
7111 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
7112 newmsg = mtod(m, struct sadb_msg *);
7113 newmsg->sadb_msg_errno = 0;
7114 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
7116 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7120 * SADB_DUMP processing
7121 * dump all entries including status of DEAD in SAD.
7124 * from the ikmpd, and dump all secasvar leaves
7129 * m will always be freed.
7132 key_dump(so, m, mhp)
7135 const struct sadb_msghdr *mhp;
7137 struct secashead *sah;
7138 struct secasvar *sav;
7144 struct sadb_msg *newmsg;
7147 IPSEC_ASSERT(so != NULL, ("null socket"));
7148 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7149 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7150 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7152 /* map satype to proto */
7153 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7154 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7156 return key_senderror(so, m, EINVAL);
7159 /* count sav entries to be sent to the userland. */
7162 LIST_FOREACH(sah, &V_sahtree, chain) {
7163 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7164 && proto != sah->saidx.proto)
7168 stateidx < _ARRAYLEN(saorder_state_any);
7170 state = saorder_state_any[stateidx];
7171 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7179 return key_senderror(so, m, ENOENT);
7182 /* send this to the userland, one at a time. */
7184 LIST_FOREACH(sah, &V_sahtree, chain) {
7185 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7186 && proto != sah->saidx.proto)
7189 /* map proto to satype */
7190 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
7192 ipseclog((LOG_DEBUG, "%s: there was invalid proto in "
7193 "SAD.\n", __func__));
7194 return key_senderror(so, m, EINVAL);
7198 stateidx < _ARRAYLEN(saorder_state_any);
7200 state = saorder_state_any[stateidx];
7201 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7202 n = key_setdumpsa(sav, SADB_DUMP, satype,
7203 --cnt, mhp->msg->sadb_msg_pid);
7206 return key_senderror(so, m, ENOBUFS);
7208 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
7219 * SADB_X_PROMISC processing
7221 * m will always be freed.
7224 key_promisc(so, m, mhp)
7227 const struct sadb_msghdr *mhp;
7231 IPSEC_ASSERT(so != NULL, ("null socket"));
7232 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7233 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7234 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7236 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7238 if (olen < sizeof(struct sadb_msg)) {
7240 return key_senderror(so, m, EINVAL);
7245 } else if (olen == sizeof(struct sadb_msg)) {
7246 /* enable/disable promisc mode */
7249 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
7250 return key_senderror(so, m, EINVAL);
7251 mhp->msg->sadb_msg_errno = 0;
7252 switch (mhp->msg->sadb_msg_satype) {
7255 kp->kp_promisc = mhp->msg->sadb_msg_satype;
7258 return key_senderror(so, m, EINVAL);
7261 /* send the original message back to everyone */
7262 mhp->msg->sadb_msg_errno = 0;
7263 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7265 /* send packet as is */
7267 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
7269 /* TODO: if sadb_msg_seq is specified, send to specific pid */
7270 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7274 static int (*key_typesw[]) __P((struct socket *, struct mbuf *,
7275 const struct sadb_msghdr *)) = {
7276 NULL, /* SADB_RESERVED */
7277 key_getspi, /* SADB_GETSPI */
7278 key_update, /* SADB_UPDATE */
7279 key_add, /* SADB_ADD */
7280 key_delete, /* SADB_DELETE */
7281 key_get, /* SADB_GET */
7282 key_acquire2, /* SADB_ACQUIRE */
7283 key_register, /* SADB_REGISTER */
7284 NULL, /* SADB_EXPIRE */
7285 key_flush, /* SADB_FLUSH */
7286 key_dump, /* SADB_DUMP */
7287 key_promisc, /* SADB_X_PROMISC */
7288 NULL, /* SADB_X_PCHANGE */
7289 key_spdadd, /* SADB_X_SPDUPDATE */
7290 key_spdadd, /* SADB_X_SPDADD */
7291 key_spddelete, /* SADB_X_SPDDELETE */
7292 key_spdget, /* SADB_X_SPDGET */
7293 NULL, /* SADB_X_SPDACQUIRE */
7294 key_spddump, /* SADB_X_SPDDUMP */
7295 key_spdflush, /* SADB_X_SPDFLUSH */
7296 key_spdadd, /* SADB_X_SPDSETIDX */
7297 NULL, /* SADB_X_SPDEXPIRE */
7298 key_spddelete2, /* SADB_X_SPDDELETE2 */
7302 * parse sadb_msg buffer to process PFKEYv2,
7303 * and create a data to response if needed.
7304 * I think to be dealed with mbuf directly.
7306 * msgp : pointer to pointer to a received buffer pulluped.
7307 * This is rewrited to response.
7308 * so : pointer to socket.
7310 * length for buffer to send to user process.
7317 struct sadb_msg *msg;
7318 struct sadb_msghdr mh;
7323 IPSEC_ASSERT(so != NULL, ("null socket"));
7324 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7326 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
7327 KEYDEBUG(KEYDEBUG_KEY_DUMP,
7328 ipseclog((LOG_DEBUG, "%s: passed sadb_msg\n", __func__));
7332 if (m->m_len < sizeof(struct sadb_msg)) {
7333 m = m_pullup(m, sizeof(struct sadb_msg));
7337 msg = mtod(m, struct sadb_msg *);
7338 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
7339 target = KEY_SENDUP_ONE;
7341 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len != orglen) {
7342 ipseclog((LOG_DEBUG, "%s: invalid message length.\n",__func__));
7343 PFKEYSTAT_INC(out_invlen);
7348 if (msg->sadb_msg_version != PF_KEY_V2) {
7349 ipseclog((LOG_DEBUG, "%s: PF_KEY version %u is mismatched.\n",
7350 __func__, msg->sadb_msg_version));
7351 PFKEYSTAT_INC(out_invver);
7356 if (msg->sadb_msg_type > SADB_MAX) {
7357 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7358 __func__, msg->sadb_msg_type));
7359 PFKEYSTAT_INC(out_invmsgtype);
7364 /* for old-fashioned code - should be nuked */
7365 if (m->m_pkthdr.len > MCLBYTES) {
7372 MGETHDR(n, M_DONTWAIT, MT_DATA);
7373 if (n && m->m_pkthdr.len > MHLEN) {
7374 MCLGET(n, M_DONTWAIT);
7375 if ((n->m_flags & M_EXT) == 0) {
7384 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
7385 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
7391 /* align the mbuf chain so that extensions are in contiguous region. */
7392 error = key_align(m, &mh);
7399 switch (msg->sadb_msg_satype) {
7400 case SADB_SATYPE_UNSPEC:
7401 switch (msg->sadb_msg_type) {
7409 ipseclog((LOG_DEBUG, "%s: must specify satype "
7410 "when msg type=%u.\n", __func__,
7411 msg->sadb_msg_type));
7412 PFKEYSTAT_INC(out_invsatype);
7417 case SADB_SATYPE_AH:
7418 case SADB_SATYPE_ESP:
7419 case SADB_X_SATYPE_IPCOMP:
7420 case SADB_X_SATYPE_TCPSIGNATURE:
7421 switch (msg->sadb_msg_type) {
7423 case SADB_X_SPDDELETE:
7425 case SADB_X_SPDDUMP:
7426 case SADB_X_SPDFLUSH:
7427 case SADB_X_SPDSETIDX:
7428 case SADB_X_SPDUPDATE:
7429 case SADB_X_SPDDELETE2:
7430 ipseclog((LOG_DEBUG, "%s: illegal satype=%u\n",
7431 __func__, msg->sadb_msg_type));
7432 PFKEYSTAT_INC(out_invsatype);
7437 case SADB_SATYPE_RSVP:
7438 case SADB_SATYPE_OSPFV2:
7439 case SADB_SATYPE_RIPV2:
7440 case SADB_SATYPE_MIP:
7441 ipseclog((LOG_DEBUG, "%s: type %u isn't supported.\n",
7442 __func__, msg->sadb_msg_satype));
7443 PFKEYSTAT_INC(out_invsatype);
7446 case 1: /* XXX: What does it do? */
7447 if (msg->sadb_msg_type == SADB_X_PROMISC)
7451 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7452 __func__, msg->sadb_msg_satype));
7453 PFKEYSTAT_INC(out_invsatype);
7458 /* check field of upper layer protocol and address family */
7459 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
7460 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
7461 struct sadb_address *src0, *dst0;
7464 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
7465 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
7467 /* check upper layer protocol */
7468 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
7469 ipseclog((LOG_DEBUG, "%s: upper layer protocol "
7470 "mismatched.\n", __func__));
7471 PFKEYSTAT_INC(out_invaddr);
7477 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
7478 PFKEY_ADDR_SADDR(dst0)->sa_family) {
7479 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
7481 PFKEYSTAT_INC(out_invaddr);
7485 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7486 PFKEY_ADDR_SADDR(dst0)->sa_len) {
7487 ipseclog((LOG_DEBUG, "%s: address struct size "
7488 "mismatched.\n", __func__));
7489 PFKEYSTAT_INC(out_invaddr);
7494 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7496 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7497 sizeof(struct sockaddr_in)) {
7498 PFKEYSTAT_INC(out_invaddr);
7504 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7505 sizeof(struct sockaddr_in6)) {
7506 PFKEYSTAT_INC(out_invaddr);
7512 ipseclog((LOG_DEBUG, "%s: unsupported address family\n",
7514 PFKEYSTAT_INC(out_invaddr);
7515 error = EAFNOSUPPORT;
7519 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7521 plen = sizeof(struct in_addr) << 3;
7524 plen = sizeof(struct in6_addr) << 3;
7527 plen = 0; /*fool gcc*/
7531 /* check max prefix length */
7532 if (src0->sadb_address_prefixlen > plen ||
7533 dst0->sadb_address_prefixlen > plen) {
7534 ipseclog((LOG_DEBUG, "%s: illegal prefixlen.\n",
7536 PFKEYSTAT_INC(out_invaddr);
7542 * prefixlen == 0 is valid because there can be a case when
7543 * all addresses are matched.
7547 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
7548 key_typesw[msg->sadb_msg_type] == NULL) {
7549 PFKEYSTAT_INC(out_invmsgtype);
7554 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
7557 msg->sadb_msg_errno = error;
7558 return key_sendup_mbuf(so, m, target);
7562 key_senderror(so, m, code)
7567 struct sadb_msg *msg;
7569 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7570 ("mbuf too small, len %u", m->m_len));
7572 msg = mtod(m, struct sadb_msg *);
7573 msg->sadb_msg_errno = code;
7574 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
7578 * set the pointer to each header into message buffer.
7579 * m will be freed on error.
7580 * XXX larger-than-MCLBYTES extension?
7585 struct sadb_msghdr *mhp;
7588 struct sadb_ext *ext;
7593 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7594 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7595 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7596 ("mbuf too small, len %u", m->m_len));
7599 bzero(mhp, sizeof(*mhp));
7601 mhp->msg = mtod(m, struct sadb_msg *);
7602 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
7604 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7605 extlen = end; /*just in case extlen is not updated*/
7606 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
7607 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
7609 /* m is already freed */
7612 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7615 switch (ext->sadb_ext_type) {
7617 case SADB_EXT_ADDRESS_SRC:
7618 case SADB_EXT_ADDRESS_DST:
7619 case SADB_EXT_ADDRESS_PROXY:
7620 case SADB_EXT_LIFETIME_CURRENT:
7621 case SADB_EXT_LIFETIME_HARD:
7622 case SADB_EXT_LIFETIME_SOFT:
7623 case SADB_EXT_KEY_AUTH:
7624 case SADB_EXT_KEY_ENCRYPT:
7625 case SADB_EXT_IDENTITY_SRC:
7626 case SADB_EXT_IDENTITY_DST:
7627 case SADB_EXT_SENSITIVITY:
7628 case SADB_EXT_PROPOSAL:
7629 case SADB_EXT_SUPPORTED_AUTH:
7630 case SADB_EXT_SUPPORTED_ENCRYPT:
7631 case SADB_EXT_SPIRANGE:
7632 case SADB_X_EXT_POLICY:
7633 case SADB_X_EXT_SA2:
7635 case SADB_X_EXT_NAT_T_TYPE:
7636 case SADB_X_EXT_NAT_T_SPORT:
7637 case SADB_X_EXT_NAT_T_DPORT:
7638 case SADB_X_EXT_NAT_T_OAI:
7639 case SADB_X_EXT_NAT_T_OAR:
7640 case SADB_X_EXT_NAT_T_FRAG:
7642 /* duplicate check */
7644 * XXX Are there duplication payloads of either
7645 * KEY_AUTH or KEY_ENCRYPT ?
7647 if (mhp->ext[ext->sadb_ext_type] != NULL) {
7648 ipseclog((LOG_DEBUG, "%s: duplicate ext_type "
7649 "%u\n", __func__, ext->sadb_ext_type));
7651 PFKEYSTAT_INC(out_dupext);
7656 ipseclog((LOG_DEBUG, "%s: invalid ext_type %u\n",
7657 __func__, ext->sadb_ext_type));
7659 PFKEYSTAT_INC(out_invexttype);
7663 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
7665 if (key_validate_ext(ext, extlen)) {
7667 PFKEYSTAT_INC(out_invlen);
7671 n = m_pulldown(m, off, extlen, &toff);
7673 /* m is already freed */
7676 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7678 mhp->ext[ext->sadb_ext_type] = ext;
7679 mhp->extoff[ext->sadb_ext_type] = off;
7680 mhp->extlen[ext->sadb_ext_type] = extlen;
7685 PFKEYSTAT_INC(out_invlen);
7693 key_validate_ext(ext, len)
7694 const struct sadb_ext *ext;
7697 const struct sockaddr *sa;
7698 enum { NONE, ADDR } checktype = NONE;
7700 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
7702 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
7705 /* if it does not match minimum/maximum length, bail */
7706 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
7707 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
7709 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
7711 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
7714 /* more checks based on sadb_ext_type XXX need more */
7715 switch (ext->sadb_ext_type) {
7716 case SADB_EXT_ADDRESS_SRC:
7717 case SADB_EXT_ADDRESS_DST:
7718 case SADB_EXT_ADDRESS_PROXY:
7719 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7722 case SADB_EXT_IDENTITY_SRC:
7723 case SADB_EXT_IDENTITY_DST:
7724 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7725 SADB_X_IDENTTYPE_ADDR) {
7726 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7736 switch (checktype) {
7740 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
7741 if (len < baselen + sal)
7743 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7756 for (i = 0; i < IPSEC_DIR_MAX; i++)
7757 LIST_INIT(&V_sptree[i]);
7759 LIST_INIT(&V_sahtree);
7761 for (i = 0; i <= SADB_SATYPE_MAX; i++)
7762 LIST_INIT(&V_regtree[i]);
7764 LIST_INIT(&V_acqtree);
7765 LIST_INIT(&V_spacqtree);
7767 /* system default */
7768 V_ip4_def_policy.policy = IPSEC_POLICY_NONE;
7769 V_ip4_def_policy.refcnt++; /*never reclaim this*/
7771 if (!IS_DEFAULT_VNET(curvnet))
7775 REGTREE_LOCK_INIT();
7776 SAHTREE_LOCK_INIT();
7780 #ifndef IPSEC_DEBUG2
7781 timeout((void *)key_timehandler, (void *)0, hz);
7782 #endif /*IPSEC_DEBUG2*/
7784 /* initialize key statistics */
7785 keystat.getspi_count = 1;
7787 printf("IPsec: Initialized Security Association Processing.\n");
7794 struct secpolicy *sp, *nextsp;
7795 struct secacq *acq, *nextacq;
7796 struct secspacq *spacq, *nextspacq;
7797 struct secashead *sah, *nextsah;
7802 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7803 for (sp = LIST_FIRST(&V_sptree[i]);
7804 sp != NULL; sp = nextsp) {
7805 nextsp = LIST_NEXT(sp, chain);
7806 if (__LIST_CHAINED(sp)) {
7807 LIST_REMOVE(sp, chain);
7808 free(sp, M_IPSEC_SP);
7815 for (sah = LIST_FIRST(&V_sahtree); sah != NULL; sah = nextsah) {
7816 nextsah = LIST_NEXT(sah, chain);
7817 if (__LIST_CHAINED(sah)) {
7818 LIST_REMOVE(sah, chain);
7819 free(sah, M_IPSEC_SAH);
7825 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7826 LIST_FOREACH(reg, &V_regtree[i], chain) {
7827 if (__LIST_CHAINED(reg)) {
7828 LIST_REMOVE(reg, chain);
7829 free(reg, M_IPSEC_SAR);
7837 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
7838 nextacq = LIST_NEXT(acq, chain);
7839 if (__LIST_CHAINED(acq)) {
7840 LIST_REMOVE(acq, chain);
7841 free(acq, M_IPSEC_SAQ);
7847 for (spacq = LIST_FIRST(&V_spacqtree); spacq != NULL;
7848 spacq = nextspacq) {
7849 nextspacq = LIST_NEXT(spacq, chain);
7850 if (__LIST_CHAINED(spacq)) {
7851 LIST_REMOVE(spacq, chain);
7852 free(spacq, M_IPSEC_SAQ);
7860 * XXX: maybe This function is called after INBOUND IPsec processing.
7862 * Special check for tunnel-mode packets.
7863 * We must make some checks for consistency between inner and outer IP header.
7865 * xxx more checks to be provided
7868 key_checktunnelsanity(sav, family, src, dst)
7869 struct secasvar *sav;
7874 IPSEC_ASSERT(sav->sah != NULL, ("null SA header"));
7876 /* XXX: check inner IP header */
7881 /* record data transfer on SA, and update timestamps */
7883 key_sa_recordxfer(sav, m)
7884 struct secasvar *sav;
7887 IPSEC_ASSERT(sav != NULL, ("Null secasvar"));
7888 IPSEC_ASSERT(m != NULL, ("Null mbuf"));
7893 * XXX Currently, there is a difference of bytes size
7894 * between inbound and outbound processing.
7896 sav->lft_c->bytes += m->m_pkthdr.len;
7897 /* to check bytes lifetime is done in key_timehandler(). */
7900 * We use the number of packets as the unit of
7901 * allocations. We increment the variable
7902 * whenever {esp,ah}_{in,out}put is called.
7904 sav->lft_c->allocations++;
7905 /* XXX check for expires? */
7908 * NOTE: We record CURRENT usetime by using wall clock,
7909 * in seconds. HARD and SOFT lifetime are measured by the time
7910 * difference (again in seconds) from usetime.
7914 * -----+-----+--------+---> t
7915 * <--------------> HARD
7918 sav->lft_c->usetime = time_second;
7919 /* XXX check for expires? */
7926 key_sa_routechange(dst)
7927 struct sockaddr *dst;
7929 struct secashead *sah;
7933 LIST_FOREACH(sah, &V_sahtree, chain) {
7934 ro = &sah->route_cache.sa_route;
7935 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7936 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7938 ro->ro_rt = (struct rtentry *)NULL;
7945 key_sa_chgstate(struct secasvar *sav, u_int8_t state)
7947 IPSEC_ASSERT(sav != NULL, ("NULL sav"));
7948 SAHTREE_LOCK_ASSERT();
7950 if (sav->state != state) {
7951 if (__LIST_CHAINED(sav))
7952 LIST_REMOVE(sav, chain);
7954 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7960 struct secasvar *sav;
7963 IPSEC_ASSERT(sav->iv != NULL, ("null IV"));
7964 key_randomfill(sav->iv, sav->ivlen);
7968 static struct mbuf *
7972 struct mbuf *m = NULL, *n;
7977 MGET(n, M_DONTWAIT, MT_DATA);
7978 if (n && len > MLEN)
7979 MCLGET(n, M_DONTWAIT);
7987 n->m_len = M_TRAILINGSPACE(n);
7988 /* use the bottom of mbuf, hoping we can prepend afterwards */
7989 if (n->m_len > len) {
7990 t = (n->m_len - len) & ~(sizeof(long) - 1);
8007 * Take one of the kernel's security keys and convert it into a PF_KEY
8008 * structure within an mbuf, suitable for sending up to a waiting
8009 * application in user land.
8012 * src: A pointer to a kernel security key.
8013 * exttype: Which type of key this is. Refer to the PF_KEY data structures.
8015 * a valid mbuf or NULL indicating an error
8019 static struct mbuf *
8020 key_setkey(struct seckey *src, u_int16_t exttype)
8029 len = PFKEY_ALIGN8(sizeof(struct sadb_key) + _KEYLEN(src));
8030 m = key_alloc_mbuf(len);
8033 p = mtod(m, struct sadb_key *);
8035 p->sadb_key_len = PFKEY_UNIT64(len);
8036 p->sadb_key_exttype = exttype;
8037 p->sadb_key_bits = src->bits;
8038 bcopy(src->key_data, _KEYBUF(p), _KEYLEN(src));
8044 * Take one of the kernel's lifetime data structures and convert it
8045 * into a PF_KEY structure within an mbuf, suitable for sending up to
8046 * a waiting application in user land.
8049 * src: A pointer to a kernel lifetime structure.
8050 * exttype: Which type of lifetime this is. Refer to the PF_KEY
8051 * data structures for more information.
8053 * a valid mbuf or NULL indicating an error
8057 static struct mbuf *
8058 key_setlifetime(struct seclifetime *src, u_int16_t exttype)
8060 struct mbuf *m = NULL;
8061 struct sadb_lifetime *p;
8062 int len = PFKEY_ALIGN8(sizeof(struct sadb_lifetime));
8067 m = key_alloc_mbuf(len);
8070 p = mtod(m, struct sadb_lifetime *);
8073 p->sadb_lifetime_len = PFKEY_UNIT64(len);
8074 p->sadb_lifetime_exttype = exttype;
8075 p->sadb_lifetime_allocations = src->allocations;
8076 p->sadb_lifetime_bytes = src->bytes;
8077 p->sadb_lifetime_addtime = src->addtime;
8078 p->sadb_lifetime_usetime = src->usetime;