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/raw_cb.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <netinet/in_var.h>
70 #include <netinet/ip6.h>
71 #include <netinet6/in6_var.h>
72 #include <netinet6/ip6_var.h>
75 #if defined(INET) || defined(INET6)
76 #include <netinet/in_pcb.h>
79 #include <netinet6/in6_pcb.h>
82 #include <net/pfkeyv2.h>
83 #include <netipsec/keydb.h>
84 #include <netipsec/key.h>
85 #include <netipsec/keysock.h>
86 #include <netipsec/key_debug.h>
88 #include <netipsec/ipsec.h>
90 #include <netipsec/ipsec6.h>
93 #include <netipsec/xform.h>
95 #include <machine/stdarg.h>
98 #include <sys/random.h>
100 #define FULLMASK 0xff
101 #define _BITS(bytes) ((bytes) << 3)
104 * Note on SA reference counting:
105 * - SAs that are not in DEAD state will have (total external reference + 1)
106 * following value in reference count field. they cannot be freed and are
107 * referenced from SA header.
108 * - SAs that are in DEAD state will have (total external reference)
109 * in reference count field. they are ready to be freed. reference from
110 * SA header will be removed in key_delsav(), when the reference count
111 * field hits 0 (= no external reference other than from SA header.
114 VNET_DEFINE(u_int32_t, key_debug_level) = 0;
115 static VNET_DEFINE(u_int, key_spi_trycnt) = 1000;
116 static VNET_DEFINE(u_int32_t, key_spi_minval) = 0x100;
117 static VNET_DEFINE(u_int32_t, key_spi_maxval) = 0x0fffffff; /* XXX */
118 static VNET_DEFINE(u_int32_t, policy_id) = 0;
119 /*interval to initialize randseed,1(m)*/
120 static VNET_DEFINE(u_int, key_int_random) = 60;
121 /* interval to expire acquiring, 30(s)*/
122 static VNET_DEFINE(u_int, key_larval_lifetime) = 30;
123 /* counter for blocking SADB_ACQUIRE.*/
124 static VNET_DEFINE(int, key_blockacq_count) = 10;
125 /* lifetime for blocking SADB_ACQUIRE.*/
126 static VNET_DEFINE(int, key_blockacq_lifetime) = 20;
127 /* preferred old sa rather than new sa.*/
128 static VNET_DEFINE(int, key_preferred_oldsa) = 1;
129 #define V_key_spi_trycnt VNET(key_spi_trycnt)
130 #define V_key_spi_minval VNET(key_spi_minval)
131 #define V_key_spi_maxval VNET(key_spi_maxval)
132 #define V_policy_id VNET(policy_id)
133 #define V_key_int_random VNET(key_int_random)
134 #define V_key_larval_lifetime VNET(key_larval_lifetime)
135 #define V_key_blockacq_count VNET(key_blockacq_count)
136 #define V_key_blockacq_lifetime VNET(key_blockacq_lifetime)
137 #define V_key_preferred_oldsa VNET(key_preferred_oldsa)
139 static VNET_DEFINE(u_int32_t, acq_seq) = 0;
140 #define V_acq_seq VNET(acq_seq)
143 static VNET_DEFINE(LIST_HEAD(_sptree, secpolicy), sptree[IPSEC_DIR_MAX]);
144 #define V_sptree VNET(sptree)
145 static struct mtx sptree_lock;
146 #define SPTREE_LOCK_INIT() \
147 mtx_init(&sptree_lock, "sptree", \
148 "fast ipsec security policy database", MTX_DEF)
149 #define SPTREE_LOCK_DESTROY() mtx_destroy(&sptree_lock)
150 #define SPTREE_LOCK() mtx_lock(&sptree_lock)
151 #define SPTREE_UNLOCK() mtx_unlock(&sptree_lock)
152 #define SPTREE_LOCK_ASSERT() mtx_assert(&sptree_lock, MA_OWNED)
154 static VNET_DEFINE(LIST_HEAD(_sahtree, secashead), sahtree); /* SAD */
155 #define V_sahtree VNET(sahtree)
156 static struct mtx sahtree_lock;
157 #define SAHTREE_LOCK_INIT() \
158 mtx_init(&sahtree_lock, "sahtree", \
159 "fast ipsec security association database", MTX_DEF)
160 #define SAHTREE_LOCK_DESTROY() mtx_destroy(&sahtree_lock)
161 #define SAHTREE_LOCK() mtx_lock(&sahtree_lock)
162 #define SAHTREE_UNLOCK() mtx_unlock(&sahtree_lock)
163 #define SAHTREE_LOCK_ASSERT() mtx_assert(&sahtree_lock, MA_OWNED)
166 static VNET_DEFINE(LIST_HEAD(_regtree, secreg), regtree[SADB_SATYPE_MAX + 1]);
167 #define V_regtree VNET(regtree)
168 static struct mtx regtree_lock;
169 #define REGTREE_LOCK_INIT() \
170 mtx_init(®tree_lock, "regtree", "fast ipsec regtree", MTX_DEF)
171 #define REGTREE_LOCK_DESTROY() mtx_destroy(®tree_lock)
172 #define REGTREE_LOCK() mtx_lock(®tree_lock)
173 #define REGTREE_UNLOCK() mtx_unlock(®tree_lock)
174 #define REGTREE_LOCK_ASSERT() mtx_assert(®tree_lock, MA_OWNED)
176 static VNET_DEFINE(LIST_HEAD(_acqtree, secacq), acqtree); /* acquiring list */
177 #define V_acqtree VNET(acqtree)
178 static struct mtx acq_lock;
179 #define ACQ_LOCK_INIT() \
180 mtx_init(&acq_lock, "acqtree", "fast ipsec acquire list", MTX_DEF)
181 #define ACQ_LOCK_DESTROY() mtx_destroy(&acq_lock)
182 #define ACQ_LOCK() mtx_lock(&acq_lock)
183 #define ACQ_UNLOCK() mtx_unlock(&acq_lock)
184 #define ACQ_LOCK_ASSERT() mtx_assert(&acq_lock, MA_OWNED)
186 /* SP acquiring list */
187 static VNET_DEFINE(LIST_HEAD(_spacqtree, secspacq), spacqtree);
188 #define V_spacqtree VNET(spacqtree)
189 static struct mtx spacq_lock;
190 #define SPACQ_LOCK_INIT() \
191 mtx_init(&spacq_lock, "spacqtree", \
192 "fast ipsec security policy acquire list", MTX_DEF)
193 #define SPACQ_LOCK_DESTROY() mtx_destroy(&spacq_lock)
194 #define SPACQ_LOCK() mtx_lock(&spacq_lock)
195 #define SPACQ_UNLOCK() mtx_unlock(&spacq_lock)
196 #define SPACQ_LOCK_ASSERT() mtx_assert(&spacq_lock, MA_OWNED)
198 /* search order for SAs */
199 static const u_int saorder_state_valid_prefer_old[] = {
200 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
202 static const u_int saorder_state_valid_prefer_new[] = {
203 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
205 static const u_int saorder_state_alive[] = {
207 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
209 static const u_int saorder_state_any[] = {
210 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
211 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
214 static const int minsize[] = {
215 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
216 sizeof(struct sadb_sa), /* SADB_EXT_SA */
217 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
218 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
219 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
220 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
221 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
222 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
223 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
224 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
225 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
226 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
227 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
228 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
229 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
230 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
231 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
232 0, /* SADB_X_EXT_KMPRIVATE */
233 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
234 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
235 sizeof(struct sadb_x_nat_t_type),/* SADB_X_EXT_NAT_T_TYPE */
236 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_SPORT */
237 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_DPORT */
238 sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAI */
239 sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAR */
240 sizeof(struct sadb_x_nat_t_frag),/* SADB_X_EXT_NAT_T_FRAG */
242 static const int maxsize[] = {
243 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
244 sizeof(struct sadb_sa), /* SADB_EXT_SA */
245 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
246 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
247 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
248 0, /* SADB_EXT_ADDRESS_SRC */
249 0, /* SADB_EXT_ADDRESS_DST */
250 0, /* SADB_EXT_ADDRESS_PROXY */
251 0, /* SADB_EXT_KEY_AUTH */
252 0, /* SADB_EXT_KEY_ENCRYPT */
253 0, /* SADB_EXT_IDENTITY_SRC */
254 0, /* SADB_EXT_IDENTITY_DST */
255 0, /* SADB_EXT_SENSITIVITY */
256 0, /* SADB_EXT_PROPOSAL */
257 0, /* SADB_EXT_SUPPORTED_AUTH */
258 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
259 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
260 0, /* SADB_X_EXT_KMPRIVATE */
261 0, /* SADB_X_EXT_POLICY */
262 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
263 sizeof(struct sadb_x_nat_t_type),/* SADB_X_EXT_NAT_T_TYPE */
264 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_SPORT */
265 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_DPORT */
266 0, /* SADB_X_EXT_NAT_T_OAI */
267 0, /* SADB_X_EXT_NAT_T_OAR */
268 sizeof(struct sadb_x_nat_t_frag),/* SADB_X_EXT_NAT_T_FRAG */
271 static VNET_DEFINE(int, ipsec_esp_keymin) = 256;
272 static VNET_DEFINE(int, ipsec_esp_auth) = 0;
273 static VNET_DEFINE(int, ipsec_ah_keymin) = 128;
275 #define V_ipsec_esp_keymin VNET(ipsec_esp_keymin)
276 #define V_ipsec_esp_auth VNET(ipsec_esp_auth)
277 #define V_ipsec_ah_keymin VNET(ipsec_ah_keymin)
280 SYSCTL_DECL(_net_key);
283 SYSCTL_VNET_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug,
284 CTLFLAG_RW, &VNET_NAME(key_debug_level), 0, "");
286 /* max count of trial for the decision of spi value */
287 SYSCTL_VNET_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt,
288 CTLFLAG_RW, &VNET_NAME(key_spi_trycnt), 0, "");
290 /* minimum spi value to allocate automatically. */
291 SYSCTL_VNET_INT(_net_key, KEYCTL_SPI_MIN_VALUE,
292 spi_minval, CTLFLAG_RW, &VNET_NAME(key_spi_minval), 0, "");
294 /* maximun spi value to allocate automatically. */
295 SYSCTL_VNET_INT(_net_key, KEYCTL_SPI_MAX_VALUE,
296 spi_maxval, CTLFLAG_RW, &VNET_NAME(key_spi_maxval), 0, "");
298 /* interval to initialize randseed */
299 SYSCTL_VNET_INT(_net_key, KEYCTL_RANDOM_INT,
300 int_random, CTLFLAG_RW, &VNET_NAME(key_int_random), 0, "");
302 /* lifetime for larval SA */
303 SYSCTL_VNET_INT(_net_key, KEYCTL_LARVAL_LIFETIME,
304 larval_lifetime, CTLFLAG_RW, &VNET_NAME(key_larval_lifetime), 0, "");
306 /* counter for blocking to send SADB_ACQUIRE to IKEd */
307 SYSCTL_VNET_INT(_net_key, KEYCTL_BLOCKACQ_COUNT,
308 blockacq_count, CTLFLAG_RW, &VNET_NAME(key_blockacq_count), 0, "");
310 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
311 SYSCTL_VNET_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME,
312 blockacq_lifetime, CTLFLAG_RW, &VNET_NAME(key_blockacq_lifetime), 0, "");
315 SYSCTL_VNET_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth,
316 CTLFLAG_RW, &VNET_NAME(ipsec_esp_auth), 0, "");
318 /* minimum ESP key length */
319 SYSCTL_VNET_INT(_net_key, KEYCTL_ESP_KEYMIN,
320 esp_keymin, CTLFLAG_RW, &VNET_NAME(ipsec_esp_keymin), 0, "");
322 /* minimum AH key length */
323 SYSCTL_VNET_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin,
324 CTLFLAG_RW, &VNET_NAME(ipsec_ah_keymin), 0, "");
326 /* perfered old SA rather than new SA */
327 SYSCTL_VNET_INT(_net_key, KEYCTL_PREFERED_OLDSA,
328 preferred_oldsa, CTLFLAG_RW, &VNET_NAME(key_preferred_oldsa), 0, "");
330 #define __LIST_CHAINED(elm) \
331 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
332 #define LIST_INSERT_TAIL(head, elm, type, field) \
334 struct type *curelm = LIST_FIRST(head); \
335 if (curelm == NULL) {\
336 LIST_INSERT_HEAD(head, elm, field); \
338 while (LIST_NEXT(curelm, field)) \
339 curelm = LIST_NEXT(curelm, field);\
340 LIST_INSERT_AFTER(curelm, elm, field);\
344 #define KEY_CHKSASTATE(head, sav, name) \
346 if ((head) != (sav)) { \
347 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
348 (name), (head), (sav))); \
353 #define KEY_CHKSPDIR(head, sp, name) \
355 if ((head) != (sp)) { \
356 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
357 "anyway continue.\n", \
358 (name), (head), (sp))); \
362 MALLOC_DEFINE(M_IPSEC_SA, "secasvar", "ipsec security association");
363 MALLOC_DEFINE(M_IPSEC_SAH, "sahead", "ipsec sa head");
364 MALLOC_DEFINE(M_IPSEC_SP, "ipsecpolicy", "ipsec security policy");
365 MALLOC_DEFINE(M_IPSEC_SR, "ipsecrequest", "ipsec security request");
366 MALLOC_DEFINE(M_IPSEC_MISC, "ipsec-misc", "ipsec miscellaneous");
367 MALLOC_DEFINE(M_IPSEC_SAQ, "ipsec-saq", "ipsec sa acquire");
368 MALLOC_DEFINE(M_IPSEC_SAR, "ipsec-reg", "ipsec sa acquire");
371 * set parameters into secpolicyindex buffer.
372 * Must allocate secpolicyindex buffer passed to this function.
374 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
376 bzero((idx), sizeof(struct secpolicyindex)); \
377 (idx)->dir = (_dir); \
378 (idx)->prefs = (ps); \
379 (idx)->prefd = (pd); \
380 (idx)->ul_proto = (ulp); \
381 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
382 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
386 * set parameters into secasindex buffer.
387 * Must allocate secasindex buffer before calling this function.
389 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
391 bzero((idx), sizeof(struct secasindex)); \
392 (idx)->proto = (p); \
394 (idx)->reqid = (r); \
395 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
396 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
401 u_long getspi_count; /* the avarage of count to try to get new SPI */
405 struct sadb_msg *msg;
406 struct sadb_ext *ext[SADB_EXT_MAX + 1];
407 int extoff[SADB_EXT_MAX + 1];
408 int extlen[SADB_EXT_MAX + 1];
411 static struct secasvar *key_allocsa_policy(const struct secasindex *);
412 static void key_freesp_so(struct secpolicy **);
413 static struct secasvar *key_do_allocsa_policy(struct secashead *, u_int);
414 static void key_delsp(struct secpolicy *);
415 static struct secpolicy *key_getsp(struct secpolicyindex *);
416 static void _key_delsp(struct secpolicy *sp);
417 static struct secpolicy *key_getspbyid(u_int32_t);
418 static u_int32_t key_newreqid(void);
419 static struct mbuf *key_gather_mbuf(struct mbuf *,
420 const struct sadb_msghdr *, int, int, ...);
421 static int key_spdadd(struct socket *, struct mbuf *,
422 const struct sadb_msghdr *);
423 static u_int32_t key_getnewspid(void);
424 static int key_spddelete(struct socket *, struct mbuf *,
425 const struct sadb_msghdr *);
426 static int key_spddelete2(struct socket *, struct mbuf *,
427 const struct sadb_msghdr *);
428 static int key_spdget(struct socket *, struct mbuf *,
429 const struct sadb_msghdr *);
430 static int key_spdflush(struct socket *, struct mbuf *,
431 const struct sadb_msghdr *);
432 static int key_spddump(struct socket *, struct mbuf *,
433 const struct sadb_msghdr *);
434 static struct mbuf *key_setdumpsp(struct secpolicy *,
435 u_int8_t, u_int32_t, u_int32_t);
436 static u_int key_getspreqmsglen(struct secpolicy *);
437 static int key_spdexpire(struct secpolicy *);
438 static struct secashead *key_newsah(struct secasindex *);
439 static void key_delsah(struct secashead *);
440 static struct secasvar *key_newsav(struct mbuf *,
441 const struct sadb_msghdr *, struct secashead *, int *,
443 #define KEY_NEWSAV(m, sadb, sah, e) \
444 key_newsav(m, sadb, sah, e, __FILE__, __LINE__)
445 static void key_delsav(struct secasvar *);
446 static struct secashead *key_getsah(struct secasindex *);
447 static struct secasvar *key_checkspidup(struct secasindex *, u_int32_t);
448 static struct secasvar *key_getsavbyspi(struct secashead *, u_int32_t);
449 static int key_setsaval(struct secasvar *, struct mbuf *,
450 const struct sadb_msghdr *);
451 static int key_mature(struct secasvar *);
452 static struct mbuf *key_setdumpsa(struct secasvar *, u_int8_t,
453 u_int8_t, u_int32_t, u_int32_t);
454 static struct mbuf *key_setsadbmsg(u_int8_t, u_int16_t, u_int8_t,
455 u_int32_t, pid_t, u_int16_t);
456 static struct mbuf *key_setsadbsa(struct secasvar *);
457 static struct mbuf *key_setsadbaddr(u_int16_t,
458 const struct sockaddr *, u_int8_t, u_int16_t);
460 static struct mbuf *key_setsadbxport(u_int16_t, u_int16_t);
461 static struct mbuf *key_setsadbxtype(u_int16_t);
463 static void key_porttosaddr(struct sockaddr *, u_int16_t);
464 #define KEY_PORTTOSADDR(saddr, port) \
465 key_porttosaddr((struct sockaddr *)(saddr), (port))
466 static struct mbuf *key_setsadbxsa2(u_int8_t, u_int32_t, u_int32_t);
467 static struct mbuf *key_setsadbxpolicy(u_int16_t, u_int8_t,
469 static struct seckey *key_dup_keymsg(const struct sadb_key *, u_int,
470 struct malloc_type *);
471 static struct seclifetime *key_dup_lifemsg(const struct sadb_lifetime *src,
472 struct malloc_type *type);
474 static int key_ismyaddr6(struct sockaddr_in6 *);
477 /* flags for key_cmpsaidx() */
478 #define CMP_HEAD 1 /* protocol, addresses. */
479 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
480 #define CMP_REQID 3 /* additionally HEAD, reaid. */
481 #define CMP_EXACTLY 4 /* all elements. */
482 static int key_cmpsaidx(const struct secasindex *,
483 const struct secasindex *, int);
484 static int key_cmpspidx_exactly(struct secpolicyindex *,
485 struct secpolicyindex *);
486 static int key_cmpspidx_withmask(struct secpolicyindex *,
487 struct secpolicyindex *);
488 static int key_sockaddrcmp(const struct sockaddr *,
489 const struct sockaddr *, int);
490 static int key_bbcmp(const void *, const void *, u_int);
491 static u_int16_t key_satype2proto(u_int8_t);
492 static u_int8_t key_proto2satype(u_int16_t);
494 static int key_getspi(struct socket *, struct mbuf *,
495 const struct sadb_msghdr *);
496 static u_int32_t key_do_getnewspi(struct sadb_spirange *,
497 struct secasindex *);
498 static int key_update(struct socket *, struct mbuf *,
499 const struct sadb_msghdr *);
500 #ifdef IPSEC_DOSEQCHECK
501 static struct secasvar *key_getsavbyseq(struct secashead *, u_int32_t);
503 static int key_add(struct socket *, struct mbuf *,
504 const struct sadb_msghdr *);
505 static int key_setident(struct secashead *, struct mbuf *,
506 const struct sadb_msghdr *);
507 static struct mbuf *key_getmsgbuf_x1(struct mbuf *,
508 const struct sadb_msghdr *);
509 static int key_delete(struct socket *, struct mbuf *,
510 const struct sadb_msghdr *);
511 static int key_delete_all(struct socket *, struct mbuf *,
512 const struct sadb_msghdr *, u_int16_t);
513 static int key_get(struct socket *, struct mbuf *,
514 const struct sadb_msghdr *);
516 static void key_getcomb_setlifetime(struct sadb_comb *);
517 static struct mbuf *key_getcomb_esp(void);
518 static struct mbuf *key_getcomb_ah(void);
519 static struct mbuf *key_getcomb_ipcomp(void);
520 static struct mbuf *key_getprop(const struct secasindex *);
522 static int key_acquire(const struct secasindex *, struct secpolicy *);
523 static struct secacq *key_newacq(const struct secasindex *);
524 static struct secacq *key_getacq(const struct secasindex *);
525 static struct secacq *key_getacqbyseq(u_int32_t);
526 static struct secspacq *key_newspacq(struct secpolicyindex *);
527 static struct secspacq *key_getspacq(struct secpolicyindex *);
528 static int key_acquire2(struct socket *, struct mbuf *,
529 const struct sadb_msghdr *);
530 static int key_register(struct socket *, struct mbuf *,
531 const struct sadb_msghdr *);
532 static int key_expire(struct secasvar *, int);
533 static int key_flush(struct socket *, struct mbuf *,
534 const struct sadb_msghdr *);
535 static int key_dump(struct socket *, struct mbuf *,
536 const struct sadb_msghdr *);
537 static int key_promisc(struct socket *, struct mbuf *,
538 const struct sadb_msghdr *);
539 static int key_senderror(struct socket *, struct mbuf *, int);
540 static int key_validate_ext(const struct sadb_ext *, int);
541 static int key_align(struct mbuf *, struct sadb_msghdr *);
542 static struct mbuf *key_setlifetime(struct seclifetime *src,
544 static struct mbuf *key_setkey(struct seckey *src, u_int16_t exttype);
547 static const char *key_getfqdn(void);
548 static const char *key_getuserfqdn(void);
550 static void key_sa_chgstate(struct secasvar *, u_int8_t);
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,
618 struct secpolicy *sp;
620 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
621 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
622 ("invalid direction %u", dir));
624 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
625 printf("DP %s from %s:%u\n", __func__, where, tag));
628 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
629 printf("*** objects\n");
630 kdebug_secpolicyindex(spidx));
633 LIST_FOREACH(sp, &V_sptree[dir], chain) {
634 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
635 printf("*** in SPD\n");
636 kdebug_secpolicyindex(&sp->spidx));
638 if (sp->state == IPSEC_SPSTATE_DEAD)
640 if (key_cmpspidx_withmask(&sp->spidx, spidx))
647 KEY_CHKSPDIR(sp->spidx.dir, dir, __func__);
649 /* found a SPD entry */
650 sp->lastused = time_second;
655 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
656 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
657 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
662 * allocating a SP for OUTBOUND or INBOUND packet.
663 * Must call key_freesp() later.
664 * OUT: NULL: not found
665 * others: found and return the pointer.
668 key_allocsp2(u_int32_t spi, union sockaddr_union *dst, u_int8_t proto,
669 u_int dir, const char* where, int tag)
671 struct secpolicy *sp;
673 IPSEC_ASSERT(dst != NULL, ("null dst"));
674 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
675 ("invalid direction %u", dir));
677 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
678 printf("DP %s from %s:%u\n", __func__, where, tag));
681 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
682 printf("*** objects\n");
683 printf("spi %u proto %u dir %u\n", spi, proto, dir);
684 kdebug_sockaddr(&dst->sa));
687 LIST_FOREACH(sp, &V_sptree[dir], chain) {
688 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
689 printf("*** in SPD\n");
690 kdebug_secpolicyindex(&sp->spidx));
692 if (sp->state == IPSEC_SPSTATE_DEAD)
694 /* compare simple values, then dst address */
695 if (sp->spidx.ul_proto != proto)
697 /* NB: spi's must exist and match */
698 if (!sp->req || !sp->req->sav || sp->req->sav->spi != spi)
700 if (key_sockaddrcmp(&sp->spidx.dst.sa, &dst->sa, 1) == 0)
707 KEY_CHKSPDIR(sp->spidx.dir, dir, __func__);
709 /* found a SPD entry */
710 sp->lastused = time_second;
715 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
716 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
717 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
723 * return a policy that matches this particular inbound packet.
727 key_gettunnel(const struct sockaddr *osrc,
728 const struct sockaddr *odst,
729 const struct sockaddr *isrc,
730 const struct sockaddr *idst,
731 const char* where, int tag)
733 struct secpolicy *sp;
734 const int dir = IPSEC_DIR_INBOUND;
735 struct ipsecrequest *r1, *r2, *p;
736 struct secpolicyindex spidx;
738 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
739 printf("DP %s from %s:%u\n", __func__, where, tag));
741 if (isrc->sa_family != idst->sa_family) {
742 ipseclog((LOG_ERR, "%s: protocol family mismatched %d != %d\n.",
743 __func__, isrc->sa_family, idst->sa_family));
749 LIST_FOREACH(sp, &V_sptree[dir], chain) {
750 if (sp->state == IPSEC_SPSTATE_DEAD)
754 for (p = sp->req; p; p = p->next) {
755 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
762 /* here we look at address matches only */
764 if (isrc->sa_len > sizeof(spidx.src) ||
765 idst->sa_len > sizeof(spidx.dst))
767 bcopy(isrc, &spidx.src, isrc->sa_len);
768 bcopy(idst, &spidx.dst, idst->sa_len);
769 if (!key_cmpspidx_withmask(&sp->spidx, &spidx))
772 if (key_sockaddrcmp(&r1->saidx.src.sa, isrc, 0) ||
773 key_sockaddrcmp(&r1->saidx.dst.sa, idst, 0))
777 if (key_sockaddrcmp(&r2->saidx.src.sa, osrc, 0) ||
778 key_sockaddrcmp(&r2->saidx.dst.sa, odst, 0))
787 sp->lastused = time_second;
792 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
793 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
794 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
800 * allocating an SA entry for an *OUTBOUND* packet.
801 * checking each request entries in SP, and acquire an SA if need.
802 * OUT: 0: there are valid requests.
803 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
806 key_checkrequest(struct ipsecrequest *isr, const struct secasindex *saidx)
810 struct secasvar *sav;
812 IPSEC_ASSERT(isr != NULL, ("null isr"));
813 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
814 IPSEC_ASSERT(saidx->mode == IPSEC_MODE_TRANSPORT ||
815 saidx->mode == IPSEC_MODE_TUNNEL,
816 ("unexpected policy %u", saidx->mode));
819 * XXX guard against protocol callbacks from the crypto
820 * thread as they reference ipsecrequest.sav which we
821 * temporarily null out below. Need to rethink how we
822 * handle bundled SA's in the callback thread.
824 IPSECREQUEST_LOCK_ASSERT(isr);
826 /* get current level */
827 level = ipsec_get_reqlevel(isr);
830 * We check new SA in the IPsec request because a different
831 * SA may be involved each time this request is checked, either
832 * because new SAs are being configured, or this request is
833 * associated with an unconnected datagram socket, or this request
834 * is associated with a system default policy.
836 * key_allocsa_policy should allocate the oldest SA available.
837 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
839 sav = key_allocsa_policy(saidx);
840 if (sav != isr->sav) {
841 /* SA need to be updated. */
842 if (!IPSECREQUEST_UPGRADE(isr)) {
843 /* Kick everyone off. */
844 IPSECREQUEST_UNLOCK(isr);
845 IPSECREQUEST_WLOCK(isr);
847 if (isr->sav != NULL)
848 KEY_FREESAV(&isr->sav);
850 IPSECREQUEST_DOWNGRADE(isr);
851 } else if (sav != NULL)
854 /* When there is SA. */
855 if (isr->sav != NULL) {
856 if (isr->sav->state != SADB_SASTATE_MATURE &&
857 isr->sav->state != SADB_SASTATE_DYING)
863 error = key_acquire(saidx, isr->sp);
865 /* XXX What should I do ? */
866 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
871 if (level != IPSEC_LEVEL_REQUIRE) {
872 /* XXX sigh, the interface to this routine is botched */
873 IPSEC_ASSERT(isr->sav == NULL, ("unexpected SA"));
881 * allocating a SA for policy entry from SAD.
882 * NOTE: searching SAD of aliving state.
883 * OUT: NULL: not found.
884 * others: found and return the pointer.
886 static struct secasvar *
887 key_allocsa_policy(const struct secasindex *saidx)
889 #define N(a) _ARRAYLEN(a)
890 struct secashead *sah;
891 struct secasvar *sav;
892 u_int stateidx, arraysize;
893 const u_int *state_valid;
895 state_valid = NULL; /* silence gcc */
896 arraysize = 0; /* silence gcc */
899 LIST_FOREACH(sah, &V_sahtree, chain) {
900 if (sah->state == SADB_SASTATE_DEAD)
902 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID)) {
903 if (V_key_preferred_oldsa) {
904 state_valid = saorder_state_valid_prefer_old;
905 arraysize = N(saorder_state_valid_prefer_old);
907 state_valid = saorder_state_valid_prefer_new;
908 arraysize = N(saorder_state_valid_prefer_new);
917 /* search valid state */
918 for (stateidx = 0; stateidx < arraysize; stateidx++) {
919 sav = key_do_allocsa_policy(sah, state_valid[stateidx]);
929 * searching SAD with direction, protocol, mode and state.
930 * called by key_allocsa_policy().
933 * others : found, pointer to a SA.
935 static struct secasvar *
936 key_do_allocsa_policy(struct secashead *sah, u_int state)
938 struct secasvar *sav, *nextsav, *candidate, *d;
944 for (sav = LIST_FIRST(&sah->savtree[state]);
948 nextsav = LIST_NEXT(sav, chain);
951 KEY_CHKSASTATE(sav->state, state, __func__);
954 if (candidate == NULL) {
959 /* Which SA is the better ? */
961 IPSEC_ASSERT(candidate->lft_c != NULL,
962 ("null candidate lifetime"));
963 IPSEC_ASSERT(sav->lft_c != NULL, ("null sav lifetime"));
965 /* What the best method is to compare ? */
966 if (V_key_preferred_oldsa) {
967 if (candidate->lft_c->addtime >
968 sav->lft_c->addtime) {
975 /* preferred new sa rather than old sa */
976 if (candidate->lft_c->addtime <
977 sav->lft_c->addtime) {
984 * prepared to delete the SA when there is more
985 * suitable candidate and the lifetime of the SA is not
988 if (d->lft_h->addtime != 0) {
989 struct mbuf *m, *result;
992 key_sa_chgstate(d, SADB_SASTATE_DEAD);
994 IPSEC_ASSERT(d->refcnt > 0, ("bogus ref count"));
996 satype = key_proto2satype(d->sah->saidx.proto);
1000 m = key_setsadbmsg(SADB_DELETE, 0,
1001 satype, 0, 0, d->refcnt - 1);
1006 /* set sadb_address for saidx's. */
1007 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
1008 &d->sah->saidx.src.sa,
1009 d->sah->saidx.src.sa.sa_len << 3,
1015 /* set sadb_address for saidx's. */
1016 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
1017 &d->sah->saidx.dst.sa,
1018 d->sah->saidx.dst.sa.sa_len << 3,
1024 /* create SA extension */
1025 m = key_setsadbsa(d);
1030 if (result->m_len < sizeof(struct sadb_msg)) {
1031 result = m_pullup(result,
1032 sizeof(struct sadb_msg));
1037 result->m_pkthdr.len = 0;
1038 for (m = result; m; m = m->m_next)
1039 result->m_pkthdr.len += m->m_len;
1040 mtod(result, struct sadb_msg *)->sadb_msg_len =
1041 PFKEY_UNIT64(result->m_pkthdr.len);
1043 if (key_sendup_mbuf(NULL, result,
1044 KEY_SENDUP_REGISTERED))
1051 sa_addref(candidate);
1052 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1053 printf("DP %s cause refcnt++:%d SA:%p\n",
1054 __func__, candidate->refcnt, candidate));
1062 * allocating a usable SA entry for a *INBOUND* packet.
1063 * Must call key_freesav() later.
1064 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
1065 * NULL: not found, or error occured.
1067 * In the comparison, no source address is used--for RFC2401 conformance.
1068 * To quote, from section 4.1:
1069 * A security association is uniquely identified by a triple consisting
1070 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1071 * security protocol (AH or ESP) identifier.
1072 * Note that, however, we do need to keep source address in IPsec SA.
1073 * IKE specification and PF_KEY specification do assume that we
1074 * keep source address in IPsec SA. We see a tricky situation here.
1077 key_allocsa(union sockaddr_union *dst, u_int proto, u_int32_t spi,
1078 const char* where, int tag)
1080 struct secashead *sah;
1081 struct secasvar *sav;
1082 u_int stateidx, arraysize, state;
1083 const u_int *saorder_state_valid;
1088 IPSEC_ASSERT(dst != NULL, ("null dst address"));
1090 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1091 printf("DP %s from %s:%u\n", __func__, where, tag));
1094 natt_chkport = (dst->sa.sa_family == AF_INET &&
1095 dst->sa.sa_len == sizeof(struct sockaddr_in) &&
1096 dst->sin.sin_port != 0);
1101 * XXX: to be checked internal IP header somewhere. Also when
1102 * IPsec tunnel packet is received. But ESP tunnel mode is
1103 * encrypted so we can't check internal IP header.
1106 if (V_key_preferred_oldsa) {
1107 saorder_state_valid = saorder_state_valid_prefer_old;
1108 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
1110 saorder_state_valid = saorder_state_valid_prefer_new;
1111 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
1113 LIST_FOREACH(sah, &V_sahtree, chain) {
1116 /* search valid state */
1117 for (stateidx = 0; stateidx < arraysize; stateidx++) {
1118 state = saorder_state_valid[stateidx];
1119 LIST_FOREACH(sav, &sah->savtree[state], chain) {
1121 KEY_CHKSASTATE(sav->state, state, __func__);
1122 /* do not return entries w/ unusable state */
1123 if (sav->state != SADB_SASTATE_MATURE &&
1124 sav->state != SADB_SASTATE_DYING)
1126 if (proto != sav->sah->saidx.proto)
1128 if (spi != sav->spi)
1133 * Really only check ports when this is a NAT-T
1134 * SA. Otherwise other lookups providing ports
1137 if (sav->natt_type && natt_chkport)
1140 #if 0 /* don't check src */
1141 /* check src address */
1142 if (key_sockaddrcmp(&src->sa,
1143 &sav->sah->saidx.src.sa, checkport) != 0)
1146 /* check dst address */
1147 if (key_sockaddrcmp(&dst->sa,
1148 &sav->sah->saidx.dst.sa, checkport) != 0)
1159 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1160 printf("DP %s return SA:%p; refcnt %u\n", __func__,
1161 sav, sav ? sav->refcnt : 0));
1166 * Must be called after calling key_allocsp().
1167 * For both the packet without socket and key_freeso().
1170 _key_freesp(struct secpolicy **spp, const char* where, int tag)
1172 struct secpolicy *sp = *spp;
1174 IPSEC_ASSERT(sp != NULL, ("null sp"));
1179 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1180 printf("DP %s SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1181 __func__, sp, sp->id, where, tag, sp->refcnt));
1183 if (sp->refcnt == 0) {
1191 * Must be called after calling key_allocsp().
1192 * For the packet with socket.
1195 key_freeso(struct socket *so)
1197 IPSEC_ASSERT(so != NULL, ("null so"));
1199 switch (so->so_proto->pr_domain->dom_family) {
1200 #if defined(INET) || defined(INET6)
1208 struct inpcb *pcb = sotoinpcb(so);
1210 /* Does it have a PCB ? */
1213 key_freesp_so(&pcb->inp_sp->sp_in);
1214 key_freesp_so(&pcb->inp_sp->sp_out);
1217 #endif /* INET || INET6 */
1219 ipseclog((LOG_DEBUG, "%s: unknown address family=%d.\n",
1220 __func__, so->so_proto->pr_domain->dom_family));
1226 key_freesp_so(struct secpolicy **sp)
1228 IPSEC_ASSERT(sp != NULL && *sp != NULL, ("null sp"));
1230 if ((*sp)->policy == IPSEC_POLICY_ENTRUST ||
1231 (*sp)->policy == IPSEC_POLICY_BYPASS)
1234 IPSEC_ASSERT((*sp)->policy == IPSEC_POLICY_IPSEC,
1235 ("invalid policy %u", (*sp)->policy));
1240 key_addrefsa(struct secasvar *sav, const char* where, int tag)
1243 IPSEC_ASSERT(sav != NULL, ("null sav"));
1244 IPSEC_ASSERT(sav->refcnt > 0, ("refcount must exist"));
1250 * Must be called after calling key_allocsa().
1251 * This function is called by key_freesp() to free some SA allocated
1255 key_freesav(struct secasvar **psav, const char* where, int tag)
1257 struct secasvar *sav = *psav;
1259 IPSEC_ASSERT(sav != NULL, ("null sav"));
1261 if (sa_delref(sav)) {
1262 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1263 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1264 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1268 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1269 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1270 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1274 /* %%% SPD management */
1276 * free security policy entry.
1279 key_delsp(struct secpolicy *sp)
1281 struct ipsecrequest *isr, *nextisr;
1283 IPSEC_ASSERT(sp != NULL, ("null sp"));
1284 SPTREE_LOCK_ASSERT();
1286 sp->state = IPSEC_SPSTATE_DEAD;
1288 IPSEC_ASSERT(sp->refcnt == 0,
1289 ("SP with references deleted (refcnt %u)", sp->refcnt));
1291 /* remove from SP index */
1292 if (__LIST_CHAINED(sp))
1293 LIST_REMOVE(sp, chain);
1295 for (isr = sp->req; isr != NULL; isr = nextisr) {
1296 if (isr->sav != NULL) {
1297 KEY_FREESAV(&isr->sav);
1301 nextisr = isr->next;
1309 * OUT: NULL : not found
1310 * others : found, pointer to a SP.
1312 static struct secpolicy *
1313 key_getsp(struct secpolicyindex *spidx)
1315 struct secpolicy *sp;
1317 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
1320 LIST_FOREACH(sp, &V_sptree[spidx->dir], chain) {
1321 if (sp->state == IPSEC_SPSTATE_DEAD)
1323 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1335 * OUT: NULL : not found
1336 * others : found, pointer to a SP.
1338 static struct secpolicy *
1339 key_getspbyid(u_int32_t id)
1341 struct secpolicy *sp;
1344 LIST_FOREACH(sp, &V_sptree[IPSEC_DIR_INBOUND], chain) {
1345 if (sp->state == IPSEC_SPSTATE_DEAD)
1353 LIST_FOREACH(sp, &V_sptree[IPSEC_DIR_OUTBOUND], chain) {
1354 if (sp->state == IPSEC_SPSTATE_DEAD)
1368 key_newsp(const char* where, int tag)
1370 struct secpolicy *newsp = NULL;
1372 newsp = (struct secpolicy *)
1373 malloc(sizeof(struct secpolicy), M_IPSEC_SP, M_NOWAIT|M_ZERO);
1375 SECPOLICY_LOCK_INIT(newsp);
1380 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1381 printf("DP %s from %s:%u return SP:%p\n", __func__,
1382 where, tag, newsp));
1387 _key_delsp(struct secpolicy *sp)
1389 SECPOLICY_LOCK_DESTROY(sp);
1390 free(sp, M_IPSEC_SP);
1394 * create secpolicy structure from sadb_x_policy structure.
1395 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1396 * so must be set properly later.
1399 key_msg2sp(struct sadb_x_policy *xpl0, size_t len, int *error)
1401 struct secpolicy *newsp;
1403 IPSEC_ASSERT(xpl0 != NULL, ("null xpl0"));
1404 IPSEC_ASSERT(len >= sizeof(*xpl0), ("policy too short: %zu", len));
1406 if (len != PFKEY_EXTLEN(xpl0)) {
1407 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n", __func__));
1412 if ((newsp = KEY_NEWSP()) == NULL) {
1417 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1418 newsp->policy = xpl0->sadb_x_policy_type;
1421 switch (xpl0->sadb_x_policy_type) {
1422 case IPSEC_POLICY_DISCARD:
1423 case IPSEC_POLICY_NONE:
1424 case IPSEC_POLICY_ENTRUST:
1425 case IPSEC_POLICY_BYPASS:
1429 case IPSEC_POLICY_IPSEC:
1432 struct sadb_x_ipsecrequest *xisr;
1433 struct ipsecrequest **p_isr = &newsp->req;
1435 /* validity check */
1436 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1437 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n",
1444 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1445 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1449 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1450 ipseclog((LOG_DEBUG, "%s: invalid ipsecrequest "
1451 "length.\n", __func__));
1457 /* allocate request buffer */
1458 /* NB: data structure is zero'd */
1459 *p_isr = ipsec_newisr();
1460 if ((*p_isr) == NULL) {
1461 ipseclog((LOG_DEBUG,
1462 "%s: No more memory.\n", __func__));
1469 switch (xisr->sadb_x_ipsecrequest_proto) {
1472 case IPPROTO_IPCOMP:
1475 ipseclog((LOG_DEBUG,
1476 "%s: invalid proto type=%u\n", __func__,
1477 xisr->sadb_x_ipsecrequest_proto));
1479 *error = EPROTONOSUPPORT;
1482 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1484 switch (xisr->sadb_x_ipsecrequest_mode) {
1485 case IPSEC_MODE_TRANSPORT:
1486 case IPSEC_MODE_TUNNEL:
1488 case IPSEC_MODE_ANY:
1490 ipseclog((LOG_DEBUG,
1491 "%s: invalid mode=%u\n", __func__,
1492 xisr->sadb_x_ipsecrequest_mode));
1497 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1499 switch (xisr->sadb_x_ipsecrequest_level) {
1500 case IPSEC_LEVEL_DEFAULT:
1501 case IPSEC_LEVEL_USE:
1502 case IPSEC_LEVEL_REQUIRE:
1504 case IPSEC_LEVEL_UNIQUE:
1505 /* validity check */
1507 * If range violation of reqid, kernel will
1508 * update it, don't refuse it.
1510 if (xisr->sadb_x_ipsecrequest_reqid
1511 > IPSEC_MANUAL_REQID_MAX) {
1512 ipseclog((LOG_DEBUG,
1513 "%s: reqid=%d range "
1514 "violation, updated by kernel.\n",
1516 xisr->sadb_x_ipsecrequest_reqid));
1517 xisr->sadb_x_ipsecrequest_reqid = 0;
1520 /* allocate new reqid id if reqid is zero. */
1521 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1523 if ((reqid = key_newreqid()) == 0) {
1528 (*p_isr)->saidx.reqid = reqid;
1529 xisr->sadb_x_ipsecrequest_reqid = reqid;
1531 /* set it for manual keying. */
1532 (*p_isr)->saidx.reqid =
1533 xisr->sadb_x_ipsecrequest_reqid;
1538 ipseclog((LOG_DEBUG, "%s: invalid level=%u\n",
1540 xisr->sadb_x_ipsecrequest_level));
1545 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1547 /* set IP addresses if there */
1548 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1549 struct sockaddr *paddr;
1551 paddr = (struct sockaddr *)(xisr + 1);
1553 /* validity check */
1555 > sizeof((*p_isr)->saidx.src)) {
1556 ipseclog((LOG_DEBUG, "%s: invalid "
1557 "request address length.\n",
1563 bcopy(paddr, &(*p_isr)->saidx.src,
1566 paddr = (struct sockaddr *)((caddr_t)paddr
1569 /* validity check */
1571 > sizeof((*p_isr)->saidx.dst)) {
1572 ipseclog((LOG_DEBUG, "%s: invalid "
1573 "request address length.\n",
1579 bcopy(paddr, &(*p_isr)->saidx.dst,
1583 (*p_isr)->sp = newsp;
1585 /* initialization for the next. */
1586 p_isr = &(*p_isr)->next;
1587 tlen -= xisr->sadb_x_ipsecrequest_len;
1589 /* validity check */
1591 ipseclog((LOG_DEBUG, "%s: becoming tlen < 0.\n",
1598 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1599 + xisr->sadb_x_ipsecrequest_len);
1604 ipseclog((LOG_DEBUG, "%s: invalid policy type.\n", __func__));
1617 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1619 auto_reqid = (auto_reqid == ~0
1620 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1622 /* XXX should be unique check */
1628 * copy secpolicy struct to sadb_x_policy structure indicated.
1631 key_sp2msg(struct secpolicy *sp)
1633 struct sadb_x_policy *xpl;
1638 IPSEC_ASSERT(sp != NULL, ("null policy"));
1640 tlen = key_getspreqmsglen(sp);
1642 m = m_get2(tlen, M_NOWAIT, MT_DATA, 0);
1647 xpl = mtod(m, struct sadb_x_policy *);
1650 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1651 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1652 xpl->sadb_x_policy_type = sp->policy;
1653 xpl->sadb_x_policy_dir = sp->spidx.dir;
1654 xpl->sadb_x_policy_id = sp->id;
1655 p = (caddr_t)xpl + sizeof(*xpl);
1657 /* if is the policy for ipsec ? */
1658 if (sp->policy == IPSEC_POLICY_IPSEC) {
1659 struct sadb_x_ipsecrequest *xisr;
1660 struct ipsecrequest *isr;
1662 for (isr = sp->req; isr != NULL; isr = isr->next) {
1664 xisr = (struct sadb_x_ipsecrequest *)p;
1666 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1667 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1668 xisr->sadb_x_ipsecrequest_level = isr->level;
1669 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1672 bcopy(&isr->saidx.src, p, isr->saidx.src.sa.sa_len);
1673 p += isr->saidx.src.sa.sa_len;
1674 bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len);
1675 p += isr->saidx.src.sa.sa_len;
1677 xisr->sadb_x_ipsecrequest_len =
1678 PFKEY_ALIGN8(sizeof(*xisr)
1679 + isr->saidx.src.sa.sa_len
1680 + isr->saidx.dst.sa.sa_len);
1687 /* m will not be freed nor modified */
1688 static struct mbuf *
1689 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1690 int ndeep, int nitem, ...)
1695 struct mbuf *result = NULL, *n;
1698 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1699 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1701 va_start(ap, nitem);
1702 for (i = 0; i < nitem; i++) {
1703 idx = va_arg(ap, int);
1704 if (idx < 0 || idx > SADB_EXT_MAX)
1706 /* don't attempt to pull empty extension */
1707 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1709 if (idx != SADB_EXT_RESERVED &&
1710 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1713 if (idx == SADB_EXT_RESERVED) {
1714 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1716 IPSEC_ASSERT(len <= MHLEN, ("header too big %u", len));
1718 MGETHDR(n, M_NOWAIT, MT_DATA);
1723 m_copydata(m, 0, sizeof(struct sadb_msg),
1725 } else if (i < ndeep) {
1726 len = mhp->extlen[idx];
1727 n = m_get2(len, M_NOWAIT, MT_DATA, 0);
1732 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1735 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1748 if ((result->m_flags & M_PKTHDR) != 0) {
1749 result->m_pkthdr.len = 0;
1750 for (n = result; n; n = n->m_next)
1751 result->m_pkthdr.len += n->m_len;
1763 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1764 * add an entry to SP database, when received
1765 * <base, address(SD), (lifetime(H),) policy>
1767 * Adding to SP database,
1769 * <base, address(SD), (lifetime(H),) policy>
1770 * to the socket which was send.
1772 * SPDADD set a unique policy entry.
1773 * SPDSETIDX like SPDADD without a part of policy requests.
1774 * SPDUPDATE replace a unique policy entry.
1776 * m will always be freed.
1779 key_spdadd(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
1781 struct sadb_address *src0, *dst0;
1782 struct sadb_x_policy *xpl0, *xpl;
1783 struct sadb_lifetime *lft = NULL;
1784 struct secpolicyindex spidx;
1785 struct secpolicy *newsp;
1788 IPSEC_ASSERT(so != NULL, ("null socket"));
1789 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1790 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1791 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
1793 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1794 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1795 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1796 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1797 return key_senderror(so, m, EINVAL);
1799 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1800 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1801 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1802 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1804 return key_senderror(so, m, EINVAL);
1806 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1807 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1808 < sizeof(struct sadb_lifetime)) {
1809 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1811 return key_senderror(so, m, EINVAL);
1813 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1816 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1817 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1818 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1821 * Note: do not parse SADB_X_EXT_NAT_T_* here:
1822 * we are processing traffic endpoints.
1826 /* XXX boundary check against sa_len */
1827 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1830 src0->sadb_address_prefixlen,
1831 dst0->sadb_address_prefixlen,
1832 src0->sadb_address_proto,
1835 /* checking the direciton. */
1836 switch (xpl0->sadb_x_policy_dir) {
1837 case IPSEC_DIR_INBOUND:
1838 case IPSEC_DIR_OUTBOUND:
1841 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
1842 mhp->msg->sadb_msg_errno = EINVAL;
1847 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1848 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1849 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1850 ipseclog((LOG_DEBUG, "%s: Invalid policy type.\n", __func__));
1851 return key_senderror(so, m, EINVAL);
1854 /* policy requests are mandatory when action is ipsec. */
1855 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1856 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1857 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1858 ipseclog((LOG_DEBUG, "%s: some policy requests part required\n",
1860 return key_senderror(so, m, EINVAL);
1864 * checking there is SP already or not.
1865 * SPDUPDATE doesn't depend on whether there is a SP or not.
1866 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1869 newsp = key_getsp(&spidx);
1870 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1873 newsp->state = IPSEC_SPSTATE_DEAD;
1878 if (newsp != NULL) {
1880 ipseclog((LOG_DEBUG, "%s: a SP entry exists already.\n",
1882 return key_senderror(so, m, EEXIST);
1886 /* allocation new SP entry */
1887 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1888 return key_senderror(so, m, error);
1891 if ((newsp->id = key_getnewspid()) == 0) {
1893 return key_senderror(so, m, ENOBUFS);
1896 /* XXX boundary check against sa_len */
1897 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1900 src0->sadb_address_prefixlen,
1901 dst0->sadb_address_prefixlen,
1902 src0->sadb_address_proto,
1905 /* sanity check on addr pair */
1906 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1907 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1909 return key_senderror(so, m, EINVAL);
1911 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1912 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1914 return key_senderror(so, m, EINVAL);
1917 if (newsp->req && newsp->req->saidx.src.sa.sa_family && newsp->req->saidx.dst.sa.sa_family) {
1918 if (newsp->req->saidx.src.sa.sa_family != newsp->req->saidx.dst.sa.sa_family) {
1920 return key_senderror(so, m, EINVAL);
1925 newsp->created = time_second;
1926 newsp->lastused = newsp->created;
1927 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1928 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1930 newsp->refcnt = 1; /* do not reclaim until I say I do */
1931 newsp->state = IPSEC_SPSTATE_ALIVE;
1932 LIST_INSERT_TAIL(&V_sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1934 /* delete the entry in spacqtree */
1935 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1936 struct secspacq *spacq = key_getspacq(&spidx);
1937 if (spacq != NULL) {
1938 /* reset counter in order to deletion by timehandler. */
1939 spacq->created = time_second;
1946 struct mbuf *n, *mpolicy;
1947 struct sadb_msg *newmsg;
1951 * Note: do not send SADB_X_EXT_NAT_T_* here:
1952 * we are sending traffic endpoints.
1955 /* create new sadb_msg to reply. */
1957 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1958 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1959 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1961 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1963 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1966 return key_senderror(so, m, ENOBUFS);
1968 if (n->m_len < sizeof(*newmsg)) {
1969 n = m_pullup(n, sizeof(*newmsg));
1971 return key_senderror(so, m, ENOBUFS);
1973 newmsg = mtod(n, struct sadb_msg *);
1974 newmsg->sadb_msg_errno = 0;
1975 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1978 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
1979 sizeof(*xpl), &off);
1980 if (mpolicy == NULL) {
1981 /* n is already freed */
1982 return key_senderror(so, m, ENOBUFS);
1984 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
1985 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
1987 return key_senderror(so, m, EINVAL);
1989 xpl->sadb_x_policy_id = newsp->id;
1992 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
1997 * get new policy id.
2005 u_int32_t newid = 0;
2006 int count = V_key_spi_trycnt; /* XXX */
2007 struct secpolicy *sp;
2009 /* when requesting to allocate spi ranged */
2011 newid = (V_policy_id = (V_policy_id == ~0 ? 1 : V_policy_id + 1));
2013 if ((sp = key_getspbyid(newid)) == NULL)
2019 if (count == 0 || newid == 0) {
2020 ipseclog((LOG_DEBUG, "%s: to allocate policy id is failed.\n",
2029 * SADB_SPDDELETE processing
2031 * <base, address(SD), policy(*)>
2032 * from the user(?), and set SADB_SASTATE_DEAD,
2034 * <base, address(SD), policy(*)>
2036 * policy(*) including direction of policy.
2038 * m will always be freed.
2041 key_spddelete(struct socket *so, struct mbuf *m,
2042 const struct sadb_msghdr *mhp)
2044 struct sadb_address *src0, *dst0;
2045 struct sadb_x_policy *xpl0;
2046 struct secpolicyindex spidx;
2047 struct secpolicy *sp;
2049 IPSEC_ASSERT(so != NULL, ("null so"));
2050 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2051 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2052 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2054 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
2055 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
2056 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
2057 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2059 return key_senderror(so, m, EINVAL);
2061 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
2062 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
2063 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2064 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2066 return key_senderror(so, m, EINVAL);
2069 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2070 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2071 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2074 * Note: do not parse SADB_X_EXT_NAT_T_* here:
2075 * we are processing traffic endpoints.
2079 /* XXX boundary check against sa_len */
2080 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2083 src0->sadb_address_prefixlen,
2084 dst0->sadb_address_prefixlen,
2085 src0->sadb_address_proto,
2088 /* checking the direciton. */
2089 switch (xpl0->sadb_x_policy_dir) {
2090 case IPSEC_DIR_INBOUND:
2091 case IPSEC_DIR_OUTBOUND:
2094 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
2095 return key_senderror(so, m, EINVAL);
2098 /* Is there SP in SPD ? */
2099 if ((sp = key_getsp(&spidx)) == NULL) {
2100 ipseclog((LOG_DEBUG, "%s: no SP found.\n", __func__));
2101 return key_senderror(so, m, EINVAL);
2104 /* save policy id to buffer to be returned. */
2105 xpl0->sadb_x_policy_id = sp->id;
2108 sp->state = IPSEC_SPSTATE_DEAD;
2114 struct sadb_msg *newmsg;
2117 * Note: do not send SADB_X_EXT_NAT_T_* here:
2118 * we are sending traffic endpoints.
2121 /* create new sadb_msg to reply. */
2122 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2123 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2125 return key_senderror(so, m, ENOBUFS);
2127 newmsg = mtod(n, struct sadb_msg *);
2128 newmsg->sadb_msg_errno = 0;
2129 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2132 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2137 * SADB_SPDDELETE2 processing
2140 * from the user(?), and set SADB_SASTATE_DEAD,
2144 * policy(*) including direction of policy.
2146 * m will always be freed.
2149 key_spddelete2(struct socket *so, struct mbuf *m,
2150 const struct sadb_msghdr *mhp)
2153 struct secpolicy *sp;
2155 IPSEC_ASSERT(so != NULL, ("null socket"));
2156 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2157 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2158 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2160 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2161 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2162 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", __func__));
2163 return key_senderror(so, m, EINVAL);
2166 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2168 /* Is there SP in SPD ? */
2169 if ((sp = key_getspbyid(id)) == NULL) {
2170 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2171 return key_senderror(so, m, EINVAL);
2175 sp->state = IPSEC_SPSTATE_DEAD;
2180 struct mbuf *n, *nn;
2181 struct sadb_msg *newmsg;
2184 /* create new sadb_msg to reply. */
2185 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2187 MGETHDR(n, M_NOWAIT, MT_DATA);
2188 if (n && len > MHLEN) {
2189 MCLGET(n, M_NOWAIT);
2190 if ((n->m_flags & M_EXT) == 0) {
2196 return key_senderror(so, m, ENOBUFS);
2202 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2203 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2205 IPSEC_ASSERT(off == len, ("length inconsistency (off %u len %u)",
2208 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2209 mhp->extlen[SADB_X_EXT_POLICY], M_NOWAIT);
2212 return key_senderror(so, m, ENOBUFS);
2215 n->m_pkthdr.len = 0;
2216 for (nn = n; nn; nn = nn->m_next)
2217 n->m_pkthdr.len += nn->m_len;
2219 newmsg = mtod(n, struct sadb_msg *);
2220 newmsg->sadb_msg_errno = 0;
2221 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2224 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2229 * SADB_X_SPDGET processing
2234 * <base, address(SD), policy>
2236 * policy(*) including direction of policy.
2238 * m will always be freed.
2241 key_spdget(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
2244 struct secpolicy *sp;
2247 IPSEC_ASSERT(so != NULL, ("null socket"));
2248 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2249 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2250 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2252 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2253 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2254 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2256 return key_senderror(so, m, EINVAL);
2259 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2261 /* Is there SP in SPD ? */
2262 if ((sp = key_getspbyid(id)) == NULL) {
2263 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2264 return key_senderror(so, m, ENOENT);
2267 n = key_setdumpsp(sp, SADB_X_SPDGET, mhp->msg->sadb_msg_seq,
2268 mhp->msg->sadb_msg_pid);
2272 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2274 return key_senderror(so, m, ENOBUFS);
2278 * SADB_X_SPDACQUIRE processing.
2279 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2282 * to KMD, and expect to receive
2283 * <base> with SADB_X_SPDACQUIRE if error occured,
2286 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2287 * policy(*) is without policy requests.
2290 * others: error number
2293 key_spdacquire(struct secpolicy *sp)
2295 struct mbuf *result = NULL, *m;
2296 struct secspacq *newspacq;
2298 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2299 IPSEC_ASSERT(sp->req == NULL, ("policy exists"));
2300 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
2301 ("policy not IPSEC %u", sp->policy));
2303 /* Get an entry to check whether sent message or not. */
2304 newspacq = key_getspacq(&sp->spidx);
2305 if (newspacq != NULL) {
2306 if (V_key_blockacq_count < newspacq->count) {
2307 /* reset counter and do send message. */
2308 newspacq->count = 0;
2310 /* increment counter and do nothing. */
2317 /* make new entry for blocking to send SADB_ACQUIRE. */
2318 newspacq = key_newspacq(&sp->spidx);
2319 if (newspacq == NULL)
2323 /* create new sadb_msg to reply. */
2324 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2330 result->m_pkthdr.len = 0;
2331 for (m = result; m; m = m->m_next)
2332 result->m_pkthdr.len += m->m_len;
2334 mtod(result, struct sadb_msg *)->sadb_msg_len =
2335 PFKEY_UNIT64(result->m_pkthdr.len);
2337 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2341 * SADB_SPDFLUSH processing
2344 * from the user, and free all entries in secpctree.
2348 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2350 * m will always be freed.
2353 key_spdflush(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
2355 struct sadb_msg *newmsg;
2356 struct secpolicy *sp;
2359 IPSEC_ASSERT(so != NULL, ("null socket"));
2360 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2361 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2362 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2364 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2365 return key_senderror(so, m, EINVAL);
2367 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2369 LIST_FOREACH(sp, &V_sptree[dir], chain)
2370 sp->state = IPSEC_SPSTATE_DEAD;
2374 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2375 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2376 return key_senderror(so, m, ENOBUFS);
2382 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2383 newmsg = mtod(m, struct sadb_msg *);
2384 newmsg->sadb_msg_errno = 0;
2385 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2387 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2391 * SADB_SPDDUMP processing
2394 * from the user, and dump all SP leaves
2399 * m will always be freed.
2402 key_spddump(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
2404 struct secpolicy *sp;
2409 IPSEC_ASSERT(so != NULL, ("null socket"));
2410 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2411 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2412 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2414 /* search SPD entry and get buffer size. */
2417 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2418 LIST_FOREACH(sp, &V_sptree[dir], chain) {
2425 return key_senderror(so, m, ENOENT);
2428 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2429 LIST_FOREACH(sp, &V_sptree[dir], chain) {
2431 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2432 mhp->msg->sadb_msg_pid);
2435 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2444 static struct mbuf *
2445 key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq,
2448 struct mbuf *result = NULL, *m;
2449 struct seclifetime lt;
2451 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2457 * Note: do not send SADB_X_EXT_NAT_T_* here:
2458 * we are sending traffic endpoints.
2460 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2461 &sp->spidx.src.sa, sp->spidx.prefs,
2462 sp->spidx.ul_proto);
2467 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2468 &sp->spidx.dst.sa, sp->spidx.prefd,
2469 sp->spidx.ul_proto);
2480 lt.addtime=sp->created;
2481 lt.usetime= sp->lastused;
2482 m = key_setlifetime(<, SADB_EXT_LIFETIME_CURRENT);
2487 lt.addtime=sp->lifetime;
2488 lt.usetime= sp->validtime;
2489 m = key_setlifetime(<, SADB_EXT_LIFETIME_HARD);
2495 if ((result->m_flags & M_PKTHDR) == 0)
2498 if (result->m_len < sizeof(struct sadb_msg)) {
2499 result = m_pullup(result, sizeof(struct sadb_msg));
2504 result->m_pkthdr.len = 0;
2505 for (m = result; m; m = m->m_next)
2506 result->m_pkthdr.len += m->m_len;
2508 mtod(result, struct sadb_msg *)->sadb_msg_len =
2509 PFKEY_UNIT64(result->m_pkthdr.len);
2519 * get PFKEY message length for security policy and request.
2522 key_getspreqmsglen(struct secpolicy *sp)
2526 tlen = sizeof(struct sadb_x_policy);
2528 /* if is the policy for ipsec ? */
2529 if (sp->policy != IPSEC_POLICY_IPSEC)
2532 /* get length of ipsec requests */
2534 struct ipsecrequest *isr;
2537 for (isr = sp->req; isr != NULL; isr = isr->next) {
2538 len = sizeof(struct sadb_x_ipsecrequest)
2539 + isr->saidx.src.sa.sa_len
2540 + isr->saidx.dst.sa.sa_len;
2542 tlen += PFKEY_ALIGN8(len);
2550 * SADB_SPDEXPIRE processing
2552 * <base, address(SD), lifetime(CH), policy>
2556 * others : error number
2559 key_spdexpire(struct secpolicy *sp)
2561 struct mbuf *result = NULL, *m;
2564 struct sadb_lifetime *lt;
2566 /* XXX: Why do we lock ? */
2568 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2570 /* set msg header */
2571 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2578 /* create lifetime extension (current and hard) */
2579 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2580 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
2587 bzero(mtod(m, caddr_t), len);
2588 lt = mtod(m, struct sadb_lifetime *);
2589 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2590 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2591 lt->sadb_lifetime_allocations = 0;
2592 lt->sadb_lifetime_bytes = 0;
2593 lt->sadb_lifetime_addtime = sp->created;
2594 lt->sadb_lifetime_usetime = sp->lastused;
2595 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2596 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2597 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2598 lt->sadb_lifetime_allocations = 0;
2599 lt->sadb_lifetime_bytes = 0;
2600 lt->sadb_lifetime_addtime = sp->lifetime;
2601 lt->sadb_lifetime_usetime = sp->validtime;
2605 * Note: do not send SADB_X_EXT_NAT_T_* here:
2606 * we are sending traffic endpoints.
2609 /* set sadb_address for source */
2610 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2612 sp->spidx.prefs, sp->spidx.ul_proto);
2619 /* set sadb_address for destination */
2620 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2622 sp->spidx.prefd, sp->spidx.ul_proto);
2637 if ((result->m_flags & M_PKTHDR) == 0) {
2642 if (result->m_len < sizeof(struct sadb_msg)) {
2643 result = m_pullup(result, sizeof(struct sadb_msg));
2644 if (result == NULL) {
2650 result->m_pkthdr.len = 0;
2651 for (m = result; m; m = m->m_next)
2652 result->m_pkthdr.len += m->m_len;
2654 mtod(result, struct sadb_msg *)->sadb_msg_len =
2655 PFKEY_UNIT64(result->m_pkthdr.len);
2657 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2665 /* %%% SAD management */
2667 * allocating a memory for new SA head, and copy from the values of mhp.
2668 * OUT: NULL : failure due to the lack of memory.
2669 * others : pointer to new SA head.
2671 static struct secashead *
2672 key_newsah(struct secasindex *saidx)
2674 struct secashead *newsah;
2676 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
2678 newsah = malloc(sizeof(struct secashead), M_IPSEC_SAH, M_NOWAIT|M_ZERO);
2679 if (newsah != NULL) {
2681 for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++)
2682 LIST_INIT(&newsah->savtree[i]);
2683 newsah->saidx = *saidx;
2685 /* add to saidxtree */
2686 newsah->state = SADB_SASTATE_MATURE;
2689 LIST_INSERT_HEAD(&V_sahtree, newsah, chain);
2696 * delete SA index and all SA registerd.
2699 key_delsah(struct secashead *sah)
2701 struct secasvar *sav, *nextsav;
2705 IPSEC_ASSERT(sah != NULL, ("NULL sah"));
2706 SAHTREE_LOCK_ASSERT();
2708 /* searching all SA registerd in the secindex. */
2710 stateidx < _ARRAYLEN(saorder_state_any);
2712 u_int state = saorder_state_any[stateidx];
2713 LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain, nextsav) {
2714 if (sav->refcnt == 0) {
2716 KEY_CHKSASTATE(state, sav->state, __func__);
2718 * do NOT call KEY_FREESAV here:
2719 * it will only delete the sav if refcnt == 1,
2720 * where we already know that refcnt == 0
2724 /* give up to delete this sa */
2729 if (!zombie) { /* delete only if there are savs */
2730 /* remove from tree of SA index */
2731 if (__LIST_CHAINED(sah))
2732 LIST_REMOVE(sah, chain);
2733 free(sah, M_IPSEC_SAH);
2738 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2739 * and copy the values of mhp into new buffer.
2740 * When SAD message type is GETSPI:
2741 * to set sequence number from acq_seq++,
2742 * to set zero to SPI.
2743 * not to call key_setsava().
2745 * others : pointer to new secasvar.
2747 * does not modify mbuf. does not free mbuf on error.
2749 static struct secasvar *
2750 key_newsav(struct mbuf *m, const struct sadb_msghdr *mhp,
2751 struct secashead *sah, int *errp, const char *where, int tag)
2753 struct secasvar *newsav;
2754 const struct sadb_sa *xsa;
2756 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2757 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2758 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2759 IPSEC_ASSERT(sah != NULL, ("null secashead"));
2761 newsav = malloc(sizeof(struct secasvar), M_IPSEC_SA, M_NOWAIT|M_ZERO);
2762 if (newsav == NULL) {
2763 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2768 switch (mhp->msg->sadb_msg_type) {
2772 #ifdef IPSEC_DOSEQCHECK
2773 /* sync sequence number */
2774 if (mhp->msg->sadb_msg_seq == 0)
2776 (V_acq_seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq));
2779 newsav->seq = mhp->msg->sadb_msg_seq;
2784 if (mhp->ext[SADB_EXT_SA] == NULL) {
2785 free(newsav, M_IPSEC_SA);
2787 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2792 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2793 newsav->spi = xsa->sadb_sa_spi;
2794 newsav->seq = mhp->msg->sadb_msg_seq;
2797 free(newsav, M_IPSEC_SA);
2804 /* copy sav values */
2805 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2806 *errp = key_setsaval(newsav, m, mhp);
2808 free(newsav, M_IPSEC_SA);
2814 SECASVAR_LOCK_INIT(newsav);
2817 newsav->created = time_second;
2818 newsav->pid = mhp->msg->sadb_msg_pid;
2823 newsav->state = SADB_SASTATE_LARVAL;
2826 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2830 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
2831 printf("DP %s from %s:%u return SP:%p\n", __func__,
2832 where, tag, newsav));
2838 * free() SA variable entry.
2841 key_cleansav(struct secasvar *sav)
2844 * Cleanup xform state. Note that zeroize'ing causes the
2845 * keys to be cleared; otherwise we must do it ourself.
2847 if (sav->tdb_xform != NULL) {
2848 sav->tdb_xform->xf_zeroize(sav);
2849 sav->tdb_xform = NULL;
2851 KASSERT(sav->iv == NULL, ("iv but no xform"));
2852 if (sav->key_auth != NULL)
2853 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
2854 if (sav->key_enc != NULL)
2855 bzero(sav->key_enc->key_data, _KEYLEN(sav->key_enc));
2857 if (sav->key_auth != NULL) {
2858 if (sav->key_auth->key_data != NULL)
2859 free(sav->key_auth->key_data, M_IPSEC_MISC);
2860 free(sav->key_auth, M_IPSEC_MISC);
2861 sav->key_auth = NULL;
2863 if (sav->key_enc != NULL) {
2864 if (sav->key_enc->key_data != NULL)
2865 free(sav->key_enc->key_data, M_IPSEC_MISC);
2866 free(sav->key_enc, M_IPSEC_MISC);
2867 sav->key_enc = NULL;
2870 bzero(sav->sched, sav->schedlen);
2871 free(sav->sched, M_IPSEC_MISC);
2874 if (sav->replay != NULL) {
2875 free(sav->replay, M_IPSEC_MISC);
2878 if (sav->lft_c != NULL) {
2879 free(sav->lft_c, M_IPSEC_MISC);
2882 if (sav->lft_h != NULL) {
2883 free(sav->lft_h, M_IPSEC_MISC);
2886 if (sav->lft_s != NULL) {
2887 free(sav->lft_s, M_IPSEC_MISC);
2893 * free() SA variable entry.
2896 key_delsav(struct secasvar *sav)
2898 IPSEC_ASSERT(sav != NULL, ("null sav"));
2899 IPSEC_ASSERT(sav->refcnt == 0, ("reference count %u > 0", sav->refcnt));
2901 /* remove from SA header */
2902 if (__LIST_CHAINED(sav))
2903 LIST_REMOVE(sav, chain);
2905 SECASVAR_LOCK_DESTROY(sav);
2906 free(sav, M_IPSEC_SA);
2913 * others : found, pointer to a SA.
2915 static struct secashead *
2916 key_getsah(struct secasindex *saidx)
2918 struct secashead *sah;
2921 LIST_FOREACH(sah, &V_sahtree, chain) {
2922 if (sah->state == SADB_SASTATE_DEAD)
2924 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2933 * check not to be duplicated SPI.
2934 * NOTE: this function is too slow due to searching all SAD.
2937 * others : found, pointer to a SA.
2939 static struct secasvar *
2940 key_checkspidup(struct secasindex *saidx, u_int32_t spi)
2942 struct secashead *sah;
2943 struct secasvar *sav;
2945 /* check address family */
2946 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
2947 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
2955 LIST_FOREACH(sah, &V_sahtree, chain) {
2956 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
2958 sav = key_getsavbyspi(sah, spi);
2968 * search SAD litmited alive SA, protocol, SPI.
2971 * others : found, pointer to a SA.
2973 static struct secasvar *
2974 key_getsavbyspi(struct secashead *sah, u_int32_t spi)
2976 struct secasvar *sav;
2977 u_int stateidx, state;
2980 SAHTREE_LOCK_ASSERT();
2981 /* search all status */
2983 stateidx < _ARRAYLEN(saorder_state_alive);
2986 state = saorder_state_alive[stateidx];
2987 LIST_FOREACH(sav, &sah->savtree[state], chain) {
2990 if (sav->state != state) {
2991 ipseclog((LOG_DEBUG, "%s: "
2992 "invalid sav->state (queue: %d SA: %d)\n",
2993 __func__, state, sav->state));
2997 if (sav->spi == spi)
3006 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
3007 * You must update these if need.
3011 * does not modify mbuf. does not free mbuf on error.
3014 key_setsaval(struct secasvar *sav, struct mbuf *m,
3015 const struct sadb_msghdr *mhp)
3019 IPSEC_ASSERT(m != NULL, ("null mbuf"));
3020 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
3021 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
3023 /* initialization */
3025 sav->key_auth = NULL;
3026 sav->key_enc = NULL;
3033 sav->tdb_xform = NULL; /* transform */
3034 sav->tdb_encalgxform = NULL; /* encoding algorithm */
3035 sav->tdb_authalgxform = NULL; /* authentication algorithm */
3036 sav->tdb_compalgxform = NULL; /* compression algorithm */
3037 /* Initialize even if NAT-T not compiled in: */
3039 sav->natt_esp_frag_len = 0;
3042 if (mhp->ext[SADB_EXT_SA] != NULL) {
3043 const struct sadb_sa *sa0;
3045 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
3046 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
3051 sav->alg_auth = sa0->sadb_sa_auth;
3052 sav->alg_enc = sa0->sadb_sa_encrypt;
3053 sav->flags = sa0->sadb_sa_flags;
3056 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
3057 sav->replay = (struct secreplay *)
3058 malloc(sizeof(struct secreplay)+sa0->sadb_sa_replay, M_IPSEC_MISC, M_NOWAIT|M_ZERO);
3059 if (sav->replay == NULL) {
3060 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3065 if (sa0->sadb_sa_replay != 0)
3066 sav->replay->bitmap = (caddr_t)(sav->replay+1);
3067 sav->replay->wsize = sa0->sadb_sa_replay;
3071 /* Authentication keys */
3072 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
3073 const struct sadb_key *key0;
3076 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
3077 len = mhp->extlen[SADB_EXT_KEY_AUTH];
3080 if (len < sizeof(*key0)) {
3084 switch (mhp->msg->sadb_msg_satype) {
3085 case SADB_SATYPE_AH:
3086 case SADB_SATYPE_ESP:
3087 case SADB_X_SATYPE_TCPSIGNATURE:
3088 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3089 sav->alg_auth != SADB_X_AALG_NULL)
3092 case SADB_X_SATYPE_IPCOMP:
3098 ipseclog((LOG_DEBUG, "%s: invalid key_auth values.\n",
3103 sav->key_auth = (struct seckey *)key_dup_keymsg(key0, len,
3105 if (sav->key_auth == NULL ) {
3106 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3113 /* Encryption key */
3114 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3115 const struct sadb_key *key0;
3118 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3119 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3122 if (len < sizeof(*key0)) {
3126 switch (mhp->msg->sadb_msg_satype) {
3127 case SADB_SATYPE_ESP:
3128 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3129 sav->alg_enc != SADB_EALG_NULL) {
3133 sav->key_enc = (struct seckey *)key_dup_keymsg(key0,
3136 if (sav->key_enc == NULL) {
3137 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3143 case SADB_X_SATYPE_IPCOMP:
3144 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3146 sav->key_enc = NULL; /*just in case*/
3148 case SADB_SATYPE_AH:
3149 case SADB_X_SATYPE_TCPSIGNATURE:
3155 ipseclog((LOG_DEBUG, "%s: invalid key_enc value.\n",
3164 switch (mhp->msg->sadb_msg_satype) {
3165 case SADB_SATYPE_AH:
3166 error = xform_init(sav, XF_AH);
3168 case SADB_SATYPE_ESP:
3169 error = xform_init(sav, XF_ESP);
3171 case SADB_X_SATYPE_IPCOMP:
3172 error = xform_init(sav, XF_IPCOMP);
3174 case SADB_X_SATYPE_TCPSIGNATURE:
3175 error = xform_init(sav, XF_TCPSIGNATURE);
3179 ipseclog((LOG_DEBUG, "%s: unable to initialize SA type %u.\n",
3180 __func__, mhp->msg->sadb_msg_satype));
3185 sav->created = time_second;
3187 /* make lifetime for CURRENT */
3188 sav->lft_c = malloc(sizeof(struct seclifetime), M_IPSEC_MISC, M_NOWAIT);
3189 if (sav->lft_c == NULL) {
3190 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3195 sav->lft_c->allocations = 0;
3196 sav->lft_c->bytes = 0;
3197 sav->lft_c->addtime = time_second;
3198 sav->lft_c->usetime = 0;
3200 /* lifetimes for HARD and SOFT */
3202 const struct sadb_lifetime *lft0;
3204 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3206 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3210 sav->lft_h = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3211 if (sav->lft_h == NULL) {
3212 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3216 /* to be initialize ? */
3219 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3221 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3225 sav->lft_s = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3226 if (sav->lft_s == NULL) {
3227 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3231 /* to be initialize ? */
3238 /* initialization */
3245 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3250 key_mature(struct secasvar *sav)
3254 /* check SPI value */
3255 switch (sav->sah->saidx.proto) {
3259 * RFC 4302, 2.4. Security Parameters Index (SPI), SPI values
3260 * 1-255 reserved by IANA for future use,
3261 * 0 for implementation specific, local use.
3263 if (ntohl(sav->spi) <= 255) {
3264 ipseclog((LOG_DEBUG, "%s: illegal range of SPI %u.\n",
3265 __func__, (u_int32_t)ntohl(sav->spi)));
3272 switch (sav->sah->saidx.proto) {
3275 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3276 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3277 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3278 "given to old-esp.\n", __func__));
3281 error = xform_init(sav, XF_ESP);
3285 if (sav->flags & SADB_X_EXT_DERIV) {
3286 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3287 "given to AH SA.\n", __func__));
3290 if (sav->alg_enc != SADB_EALG_NONE) {
3291 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3292 "mismated.\n", __func__));
3295 error = xform_init(sav, XF_AH);
3297 case IPPROTO_IPCOMP:
3298 if (sav->alg_auth != SADB_AALG_NONE) {
3299 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3300 "mismated.\n", __func__));
3303 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3304 && ntohl(sav->spi) >= 0x10000) {
3305 ipseclog((LOG_DEBUG, "%s: invalid cpi for IPComp.\n",
3309 error = xform_init(sav, XF_IPCOMP);
3312 if (sav->alg_enc != SADB_EALG_NONE) {
3313 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3314 "mismated.\n", __func__));
3317 error = xform_init(sav, XF_TCPSIGNATURE);
3320 ipseclog((LOG_DEBUG, "%s: Invalid satype.\n", __func__));
3321 error = EPROTONOSUPPORT;
3326 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3333 * subroutine for SADB_GET and SADB_DUMP.
3335 static struct mbuf *
3336 key_setdumpsa(struct secasvar *sav, u_int8_t type, u_int8_t satype,
3337 u_int32_t seq, u_int32_t pid)
3339 struct mbuf *result = NULL, *tres = NULL, *m;
3342 SADB_EXT_SA, SADB_X_EXT_SA2,
3343 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3344 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3345 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3346 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3347 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3349 SADB_X_EXT_NAT_T_TYPE,
3350 SADB_X_EXT_NAT_T_SPORT, SADB_X_EXT_NAT_T_DPORT,
3351 SADB_X_EXT_NAT_T_OAI, SADB_X_EXT_NAT_T_OAR,
3352 SADB_X_EXT_NAT_T_FRAG,
3356 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3361 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3363 switch (dumporder[i]) {
3365 m = key_setsadbsa(sav);
3370 case SADB_X_EXT_SA2:
3371 m = key_setsadbxsa2(sav->sah->saidx.mode,
3372 sav->replay ? sav->replay->count : 0,
3373 sav->sah->saidx.reqid);
3378 case SADB_EXT_ADDRESS_SRC:
3379 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3380 &sav->sah->saidx.src.sa,
3381 FULLMASK, IPSEC_ULPROTO_ANY);
3386 case SADB_EXT_ADDRESS_DST:
3387 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3388 &sav->sah->saidx.dst.sa,
3389 FULLMASK, IPSEC_ULPROTO_ANY);
3394 case SADB_EXT_KEY_AUTH:
3397 m = key_setkey(sav->key_auth, SADB_EXT_KEY_AUTH);
3402 case SADB_EXT_KEY_ENCRYPT:
3405 m = key_setkey(sav->key_enc, SADB_EXT_KEY_ENCRYPT);
3410 case SADB_EXT_LIFETIME_CURRENT:
3413 m = key_setlifetime(sav->lft_c,
3414 SADB_EXT_LIFETIME_CURRENT);
3419 case SADB_EXT_LIFETIME_HARD:
3422 m = key_setlifetime(sav->lft_h,
3423 SADB_EXT_LIFETIME_HARD);
3428 case SADB_EXT_LIFETIME_SOFT:
3431 m = key_setlifetime(sav->lft_s,
3432 SADB_EXT_LIFETIME_SOFT);
3439 case SADB_X_EXT_NAT_T_TYPE:
3440 m = key_setsadbxtype(sav->natt_type);
3445 case SADB_X_EXT_NAT_T_DPORT:
3446 m = key_setsadbxport(
3447 KEY_PORTFROMSADDR(&sav->sah->saidx.dst),
3448 SADB_X_EXT_NAT_T_DPORT);
3453 case SADB_X_EXT_NAT_T_SPORT:
3454 m = key_setsadbxport(
3455 KEY_PORTFROMSADDR(&sav->sah->saidx.src),
3456 SADB_X_EXT_NAT_T_SPORT);
3461 case SADB_X_EXT_NAT_T_OAI:
3462 case SADB_X_EXT_NAT_T_OAR:
3463 case SADB_X_EXT_NAT_T_FRAG:
3464 /* We do not (yet) support those. */
3468 case SADB_EXT_ADDRESS_PROXY:
3469 case SADB_EXT_IDENTITY_SRC:
3470 case SADB_EXT_IDENTITY_DST:
3471 /* XXX: should we brought from SPD ? */
3472 case SADB_EXT_SENSITIVITY:
3485 m_cat(result, tres);
3486 if (result->m_len < sizeof(struct sadb_msg)) {
3487 result = m_pullup(result, sizeof(struct sadb_msg));
3492 result->m_pkthdr.len = 0;
3493 for (m = result; m; m = m->m_next)
3494 result->m_pkthdr.len += m->m_len;
3496 mtod(result, struct sadb_msg *)->sadb_msg_len =
3497 PFKEY_UNIT64(result->m_pkthdr.len);
3508 * set data into sadb_msg.
3510 static struct mbuf *
3511 key_setsadbmsg(u_int8_t type, u_int16_t tlen, u_int8_t satype, u_int32_t seq,
3512 pid_t pid, u_int16_t reserved)
3518 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3521 MGETHDR(m, M_NOWAIT, MT_DATA);
3522 if (m && len > MHLEN) {
3523 MCLGET(m, M_NOWAIT);
3524 if ((m->m_flags & M_EXT) == 0) {
3531 m->m_pkthdr.len = m->m_len = len;
3534 p = mtod(m, struct sadb_msg *);
3537 p->sadb_msg_version = PF_KEY_V2;
3538 p->sadb_msg_type = type;
3539 p->sadb_msg_errno = 0;
3540 p->sadb_msg_satype = satype;
3541 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3542 p->sadb_msg_reserved = reserved;
3543 p->sadb_msg_seq = seq;
3544 p->sadb_msg_pid = (u_int32_t)pid;
3550 * copy secasvar data into sadb_address.
3552 static struct mbuf *
3553 key_setsadbsa(struct secasvar *sav)
3559 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3560 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3565 p = mtod(m, struct sadb_sa *);
3567 p->sadb_sa_len = PFKEY_UNIT64(len);
3568 p->sadb_sa_exttype = SADB_EXT_SA;
3569 p->sadb_sa_spi = sav->spi;
3570 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3571 p->sadb_sa_state = sav->state;
3572 p->sadb_sa_auth = sav->alg_auth;
3573 p->sadb_sa_encrypt = sav->alg_enc;
3574 p->sadb_sa_flags = sav->flags;
3580 * set data into sadb_address.
3582 static struct mbuf *
3583 key_setsadbaddr(u_int16_t exttype, const struct sockaddr *saddr,
3584 u_int8_t prefixlen, u_int16_t ul_proto)
3587 struct sadb_address *p;
3590 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3591 PFKEY_ALIGN8(saddr->sa_len);
3592 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3597 p = mtod(m, struct sadb_address *);
3600 p->sadb_address_len = PFKEY_UNIT64(len);
3601 p->sadb_address_exttype = exttype;
3602 p->sadb_address_proto = ul_proto;
3603 if (prefixlen == FULLMASK) {
3604 switch (saddr->sa_family) {
3606 prefixlen = sizeof(struct in_addr) << 3;
3609 prefixlen = sizeof(struct in6_addr) << 3;
3615 p->sadb_address_prefixlen = prefixlen;
3616 p->sadb_address_reserved = 0;
3619 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3626 * set data into sadb_x_sa2.
3628 static struct mbuf *
3629 key_setsadbxsa2(u_int8_t mode, u_int32_t seq, u_int32_t reqid)
3632 struct sadb_x_sa2 *p;
3635 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3636 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3641 p = mtod(m, struct sadb_x_sa2 *);
3644 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3645 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3646 p->sadb_x_sa2_mode = mode;
3647 p->sadb_x_sa2_reserved1 = 0;
3648 p->sadb_x_sa2_reserved2 = 0;
3649 p->sadb_x_sa2_sequence = seq;
3650 p->sadb_x_sa2_reqid = reqid;
3657 * Set a type in sadb_x_nat_t_type.
3659 static struct mbuf *
3660 key_setsadbxtype(u_int16_t type)
3664 struct sadb_x_nat_t_type *p;
3666 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_type));
3668 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3673 p = mtod(m, struct sadb_x_nat_t_type *);
3676 p->sadb_x_nat_t_type_len = PFKEY_UNIT64(len);
3677 p->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
3678 p->sadb_x_nat_t_type_type = type;
3683 * Set a port in sadb_x_nat_t_port.
3684 * In contrast to default RFC 2367 behaviour, port is in network byte order.
3686 static struct mbuf *
3687 key_setsadbxport(u_int16_t port, u_int16_t type)
3691 struct sadb_x_nat_t_port *p;
3693 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_port));
3695 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3700 p = mtod(m, struct sadb_x_nat_t_port *);
3703 p->sadb_x_nat_t_port_len = PFKEY_UNIT64(len);
3704 p->sadb_x_nat_t_port_exttype = type;
3705 p->sadb_x_nat_t_port_port = port;
3711 * Get port from sockaddr. Port is in network byte order.
3714 key_portfromsaddr(struct sockaddr *sa)
3717 switch (sa->sa_family) {
3720 return ((struct sockaddr_in *)sa)->sin_port;
3724 return ((struct sockaddr_in6 *)sa)->sin6_port;
3727 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
3728 printf("DP %s unexpected address family %d\n",
3729 __func__, sa->sa_family));
3732 #endif /* IPSEC_NAT_T */
3735 * Set port in struct sockaddr. Port is in network byte order.
3738 key_porttosaddr(struct sockaddr *sa, u_int16_t port)
3741 switch (sa->sa_family) {
3744 ((struct sockaddr_in *)sa)->sin_port = port;
3749 ((struct sockaddr_in6 *)sa)->sin6_port = port;
3753 ipseclog((LOG_DEBUG, "%s: unexpected address family %d.\n",
3754 __func__, sa->sa_family));
3760 * set data into sadb_x_policy
3762 static struct mbuf *
3763 key_setsadbxpolicy(u_int16_t type, u_int8_t dir, u_int32_t id)
3766 struct sadb_x_policy *p;
3769 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3770 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
3775 p = mtod(m, struct sadb_x_policy *);
3778 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3779 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3780 p->sadb_x_policy_type = type;
3781 p->sadb_x_policy_dir = dir;
3782 p->sadb_x_policy_id = id;
3788 /* Take a key message (sadb_key) from the socket and turn it into one
3789 * of the kernel's key structures (seckey).
3791 * IN: pointer to the src
3792 * OUT: NULL no more memory
3795 key_dup_keymsg(const struct sadb_key *src, u_int len,
3796 struct malloc_type *type)
3799 dst = (struct seckey *)malloc(sizeof(struct seckey), type, M_NOWAIT);
3801 dst->bits = src->sadb_key_bits;
3802 dst->key_data = (char *)malloc(len, type, M_NOWAIT);
3803 if (dst->key_data != NULL) {
3804 bcopy((const char *)src + sizeof(struct sadb_key),
3805 dst->key_data, len);
3807 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3813 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3820 /* Take a lifetime message (sadb_lifetime) passed in on a socket and
3821 * turn it into one of the kernel's lifetime structures (seclifetime).
3823 * IN: pointer to the destination, source and malloc type
3824 * OUT: NULL, no more memory
3827 static struct seclifetime *
3828 key_dup_lifemsg(const struct sadb_lifetime *src, struct malloc_type *type)
3830 struct seclifetime *dst = NULL;
3832 dst = (struct seclifetime *)malloc(sizeof(struct seclifetime),
3836 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3838 dst->allocations = src->sadb_lifetime_allocations;
3839 dst->bytes = src->sadb_lifetime_bytes;
3840 dst->addtime = src->sadb_lifetime_addtime;
3841 dst->usetime = src->sadb_lifetime_usetime;
3846 /* compare my own address
3847 * OUT: 1: true, i.e. my address.
3851 key_ismyaddr(struct sockaddr *sa)
3854 IPSEC_ASSERT(sa != NULL, ("null sockaddr"));
3855 switch (sa->sa_family) {
3858 return (in_localip(satosin(sa)->sin_addr));
3862 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3871 * compare my own address for IPv6.
3874 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3876 #include <netinet6/in6_var.h>
3879 key_ismyaddr6(struct sockaddr_in6 *sin6)
3881 struct in6_ifaddr *ia;
3883 struct in6_multi *in6m;
3887 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
3888 if (key_sockaddrcmp((struct sockaddr *)sin6,
3889 (struct sockaddr *)&ia->ia_addr, 0) == 0) {
3890 IN6_IFADDR_RUNLOCK();
3897 * XXX why do we care about multlicast here while we don't care
3898 * about IPv4 multicast??
3902 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3904 IN6_IFADDR_RUNLOCK();
3909 IN6_IFADDR_RUNLOCK();
3911 /* loopback, just for safety */
3912 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
3920 * compare two secasindex structure.
3921 * flag can specify to compare 2 saidxes.
3922 * compare two secasindex structure without both mode and reqid.
3923 * don't compare port.
3925 * saidx0: source, it can be in SAD.
3932 key_cmpsaidx(const struct secasindex *saidx0, const struct secasindex *saidx1,
3938 if (saidx0 == NULL && saidx1 == NULL)
3941 if (saidx0 == NULL || saidx1 == NULL)
3944 if (saidx0->proto != saidx1->proto)
3947 if (flag == CMP_EXACTLY) {
3948 if (saidx0->mode != saidx1->mode)
3950 if (saidx0->reqid != saidx1->reqid)
3952 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
3953 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
3957 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
3958 if (flag == CMP_MODE_REQID
3959 ||flag == CMP_REQID) {
3961 * If reqid of SPD is non-zero, unique SA is required.
3962 * The result must be of same reqid in this case.
3964 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
3968 if (flag == CMP_MODE_REQID) {
3969 if (saidx0->mode != IPSEC_MODE_ANY
3970 && saidx0->mode != saidx1->mode)
3976 * If NAT-T is enabled, check ports for tunnel mode.
3977 * Do not check ports if they are set to zero in the SPD.
3978 * Also do not do it for native transport mode, as there
3979 * is no port information available in the SP.
3981 if ((saidx1->mode == IPSEC_MODE_TUNNEL ||
3982 (saidx1->mode == IPSEC_MODE_TRANSPORT &&
3983 saidx1->proto == IPPROTO_ESP)) &&
3984 saidx1->src.sa.sa_family == AF_INET &&
3985 saidx1->dst.sa.sa_family == AF_INET &&
3986 ((const struct sockaddr_in *)(&saidx1->src))->sin_port &&
3987 ((const struct sockaddr_in *)(&saidx1->dst))->sin_port)
3989 #endif /* IPSEC_NAT_T */
3991 if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, chkport) != 0) {
3994 if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, chkport) != 0) {
4003 * compare two secindex structure exactly.
4005 * spidx0: source, it is often in SPD.
4006 * spidx1: object, it is often from PFKEY message.
4012 key_cmpspidx_exactly(struct secpolicyindex *spidx0,
4013 struct secpolicyindex *spidx1)
4016 if (spidx0 == NULL && spidx1 == NULL)
4019 if (spidx0 == NULL || spidx1 == NULL)
4022 if (spidx0->prefs != spidx1->prefs
4023 || spidx0->prefd != spidx1->prefd
4024 || spidx0->ul_proto != spidx1->ul_proto)
4027 return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
4028 key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
4032 * compare two secindex structure with mask.
4034 * spidx0: source, it is often in SPD.
4035 * spidx1: object, it is often from IP header.
4041 key_cmpspidx_withmask(struct secpolicyindex *spidx0,
4042 struct secpolicyindex *spidx1)
4045 if (spidx0 == NULL && spidx1 == NULL)
4048 if (spidx0 == NULL || spidx1 == NULL)
4051 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
4052 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
4053 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
4054 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
4057 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4058 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
4059 && spidx0->ul_proto != spidx1->ul_proto)
4062 switch (spidx0->src.sa.sa_family) {
4064 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
4065 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
4067 if (!key_bbcmp(&spidx0->src.sin.sin_addr,
4068 &spidx1->src.sin.sin_addr, spidx0->prefs))
4072 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
4073 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
4076 * scope_id check. if sin6_scope_id is 0, we regard it
4077 * as a wildcard scope, which matches any scope zone ID.
4079 if (spidx0->src.sin6.sin6_scope_id &&
4080 spidx1->src.sin6.sin6_scope_id &&
4081 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
4083 if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
4084 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
4089 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
4094 switch (spidx0->dst.sa.sa_family) {
4096 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
4097 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
4099 if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
4100 &spidx1->dst.sin.sin_addr, spidx0->prefd))
4104 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
4105 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
4108 * scope_id check. if sin6_scope_id is 0, we regard it
4109 * as a wildcard scope, which matches any scope zone ID.
4111 if (spidx0->dst.sin6.sin6_scope_id &&
4112 spidx1->dst.sin6.sin6_scope_id &&
4113 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
4115 if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
4116 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
4121 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
4126 /* XXX Do we check other field ? e.g. flowinfo */
4131 /* returns 0 on match */
4133 key_sockaddrcmp(const struct sockaddr *sa1, const struct sockaddr *sa2,
4139 #define satosin(s) ((const struct sockaddr_in *)s)
4143 #define satosin6(s) ((const struct sockaddr_in6 *)s)
4144 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
4147 switch (sa1->sa_family) {
4149 if (sa1->sa_len != sizeof(struct sockaddr_in))
4151 if (satosin(sa1)->sin_addr.s_addr !=
4152 satosin(sa2)->sin_addr.s_addr) {
4155 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
4159 if (sa1->sa_len != sizeof(struct sockaddr_in6))
4160 return 1; /*EINVAL*/
4161 if (satosin6(sa1)->sin6_scope_id !=
4162 satosin6(sa2)->sin6_scope_id) {
4165 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
4166 &satosin6(sa2)->sin6_addr)) {
4170 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
4175 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
4186 * compare two buffers with mask.
4190 * bits: Number of bits to compare
4196 key_bbcmp(const void *a1, const void *a2, u_int bits)
4198 const unsigned char *p1 = a1;
4199 const unsigned char *p2 = a2;
4201 /* XXX: This could be considerably faster if we compare a word
4202 * at a time, but it is complicated on LSB Endian machines */
4204 /* Handle null pointers */
4205 if (p1 == NULL || p2 == NULL)
4215 u_int8_t mask = ~((1<<(8-bits))-1);
4216 if ((*p1 & mask) != (*p2 & mask))
4219 return 1; /* Match! */
4223 key_flush_spd(time_t now)
4225 static u_int16_t sptree_scangen = 0;
4226 u_int16_t gen = sptree_scangen++;
4227 struct secpolicy *sp;
4231 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4234 LIST_FOREACH(sp, &V_sptree[dir], chain) {
4235 if (sp->scangen == gen) /* previously handled */
4238 if (sp->state == IPSEC_SPSTATE_DEAD &&
4241 * Ensure that we only decrease refcnt once,
4242 * when we're the last consumer.
4243 * Directly call SP_DELREF/key_delsp instead
4244 * of KEY_FREESP to avoid unlocking/relocking
4245 * SPTREE_LOCK before key_delsp: may refcnt
4246 * be increased again during that time ?
4247 * NB: also clean entries created by
4255 if (sp->lifetime == 0 && sp->validtime == 0)
4257 if ((sp->lifetime && now - sp->created > sp->lifetime)
4258 || (sp->validtime && now - sp->lastused > sp->validtime)) {
4259 sp->state = IPSEC_SPSTATE_DEAD;
4270 key_flush_sad(time_t now)
4272 struct secashead *sah, *nextsah;
4273 struct secasvar *sav, *nextsav;
4277 LIST_FOREACH_SAFE(sah, &V_sahtree, chain, nextsah) {
4278 /* if sah has been dead, then delete it and process next sah. */
4279 if (sah->state == SADB_SASTATE_DEAD) {
4284 /* if LARVAL entry doesn't become MATURE, delete it. */
4285 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_LARVAL], chain, nextsav) {
4286 /* Need to also check refcnt for a larval SA ??? */
4287 if (now - sav->created > V_key_larval_lifetime)
4292 * check MATURE entry to start to send expire message
4295 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_MATURE], chain, nextsav) {
4296 /* we don't need to check. */
4297 if (sav->lft_s == NULL)
4301 if (sav->lft_c == NULL) {
4302 ipseclog((LOG_DEBUG,"%s: there is no CURRENT "
4303 "time, why?\n", __func__));
4308 * HARD lifetimes MUST take precedence over SOFT
4309 * lifetimes, meaning if the HARD and SOFT lifetimes
4310 * are the same, the HARD lifetime will appear on the
4313 /* check HARD lifetime */
4314 if ((sav->lft_h->addtime != 0 &&
4315 now - sav->created > sav->lft_h->addtime) ||
4316 (sav->lft_h->bytes != 0 &&
4317 sav->lft_h->bytes < sav->lft_c->bytes)) {
4318 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4322 /* check SOFT lifetime */
4323 else if ((sav->lft_s->addtime != 0 &&
4324 now - sav->created > sav->lft_s->addtime) ||
4325 (sav->lft_s->bytes != 0 &&
4326 sav->lft_s->bytes < sav->lft_c->bytes)) {
4327 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4332 /* check DYING entry to change status to DEAD. */
4333 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DYING], chain, nextsav) {
4334 /* we don't need to check. */
4335 if (sav->lft_h == NULL)
4339 if (sav->lft_c == NULL) {
4340 ipseclog((LOG_DEBUG, "%s: there is no CURRENT "
4341 "time, why?\n", __func__));
4345 if (sav->lft_h->addtime != 0 &&
4346 now - sav->created > sav->lft_h->addtime) {
4347 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4351 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4352 else if (sav->lft_s != NULL
4353 && sav->lft_s->addtime != 0
4354 && now - sav->created > sav->lft_s->addtime) {
4356 * XXX: should be checked to be
4357 * installed the valid SA.
4361 * If there is no SA then sending
4367 /* check HARD lifetime by bytes */
4368 else if (sav->lft_h->bytes != 0 &&
4369 sav->lft_h->bytes < sav->lft_c->bytes) {
4370 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4376 /* delete entry in DEAD */
4377 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DEAD], chain, nextsav) {
4379 if (sav->state != SADB_SASTATE_DEAD) {
4380 ipseclog((LOG_DEBUG, "%s: invalid sav->state "
4381 "(queue: %d SA: %d): kill it anyway\n",
4383 SADB_SASTATE_DEAD, sav->state));
4386 * do not call key_freesav() here.
4387 * sav should already be freed, and sav->refcnt
4388 * shows other references to sav
4389 * (such as from SPD).
4397 key_flush_acq(time_t now)
4399 struct secacq *acq, *nextacq;
4403 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
4404 nextacq = LIST_NEXT(acq, chain);
4405 if (now - acq->created > V_key_blockacq_lifetime
4406 && __LIST_CHAINED(acq)) {
4407 LIST_REMOVE(acq, chain);
4408 free(acq, M_IPSEC_SAQ);
4415 key_flush_spacq(time_t now)
4417 struct secspacq *acq, *nextacq;
4421 for (acq = LIST_FIRST(&V_spacqtree); acq != NULL; acq = nextacq) {
4422 nextacq = LIST_NEXT(acq, chain);
4423 if (now - acq->created > V_key_blockacq_lifetime
4424 && __LIST_CHAINED(acq)) {
4425 LIST_REMOVE(acq, chain);
4426 free(acq, M_IPSEC_SAQ);
4434 * scanning SPD and SAD to check status for each entries,
4435 * and do to remove or to expire.
4436 * XXX: year 2038 problem may remain.
4439 key_timehandler(void)
4441 VNET_ITERATOR_DECL(vnet_iter);
4442 time_t now = time_second;
4444 VNET_LIST_RLOCK_NOSLEEP();
4445 VNET_FOREACH(vnet_iter) {
4446 CURVNET_SET(vnet_iter);
4450 key_flush_spacq(now);
4453 VNET_LIST_RUNLOCK_NOSLEEP();
4455 #ifndef IPSEC_DEBUG2
4456 /* do exchange to tick time !! */
4457 (void)timeout((void *)key_timehandler, (void *)0, hz);
4458 #endif /* IPSEC_DEBUG2 */
4466 key_randomfill(&value, sizeof(value));
4471 key_randomfill(void *p, size_t l)
4475 static int warn = 1;
4478 n = (size_t)read_random(p, (u_int)l);
4482 bcopy(&v, (u_int8_t *)p + n,
4483 l - n < sizeof(v) ? l - n : sizeof(v));
4487 printf("WARNING: pseudo-random number generator "
4488 "used for IPsec processing\n");
4495 * map SADB_SATYPE_* to IPPROTO_*.
4496 * if satype == SADB_SATYPE then satype is mapped to ~0.
4498 * 0: invalid satype.
4501 key_satype2proto(u_int8_t satype)
4504 case SADB_SATYPE_UNSPEC:
4505 return IPSEC_PROTO_ANY;
4506 case SADB_SATYPE_AH:
4508 case SADB_SATYPE_ESP:
4510 case SADB_X_SATYPE_IPCOMP:
4511 return IPPROTO_IPCOMP;
4512 case SADB_X_SATYPE_TCPSIGNATURE:
4521 * map IPPROTO_* to SADB_SATYPE_*
4523 * 0: invalid protocol type.
4526 key_proto2satype(u_int16_t proto)
4530 return SADB_SATYPE_AH;
4532 return SADB_SATYPE_ESP;
4533 case IPPROTO_IPCOMP:
4534 return SADB_X_SATYPE_IPCOMP;
4536 return SADB_X_SATYPE_TCPSIGNATURE;
4545 * SADB_GETSPI processing is to receive
4546 * <base, (SA2), src address, dst address, (SPI range)>
4547 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4548 * tree with the status of LARVAL, and send
4549 * <base, SA(*), address(SD)>
4552 * IN: mhp: pointer to the pointer to each header.
4553 * OUT: NULL if fail.
4554 * other if success, return pointer to the message to send.
4557 key_getspi(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
4559 struct sadb_address *src0, *dst0;
4560 struct secasindex saidx;
4561 struct secashead *newsah;
4562 struct secasvar *newsav;
4569 IPSEC_ASSERT(so != NULL, ("null socket"));
4570 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4571 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4572 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4574 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4575 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4576 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4578 return key_senderror(so, m, EINVAL);
4580 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4581 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4582 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4584 return key_senderror(so, m, EINVAL);
4586 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4587 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4588 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4590 mode = IPSEC_MODE_ANY;
4594 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4595 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4597 /* map satype to proto */
4598 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4599 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4601 return key_senderror(so, m, EINVAL);
4605 * Make sure the port numbers are zero.
4606 * In case of NAT-T we will update them later if needed.
4608 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4610 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4611 sizeof(struct sockaddr_in))
4612 return key_senderror(so, m, EINVAL);
4613 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4616 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4617 sizeof(struct sockaddr_in6))
4618 return key_senderror(so, m, EINVAL);
4619 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4624 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4626 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4627 sizeof(struct sockaddr_in))
4628 return key_senderror(so, m, EINVAL);
4629 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4632 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4633 sizeof(struct sockaddr_in6))
4634 return key_senderror(so, m, EINVAL);
4635 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4641 /* XXX boundary check against sa_len */
4642 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4646 * Handle NAT-T info if present.
4647 * We made sure the port numbers are zero above, so we do
4648 * not have to worry in case we do not update them.
4650 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL)
4651 ipseclog((LOG_DEBUG, "%s: NAT-T OAi present\n", __func__));
4652 if (mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL)
4653 ipseclog((LOG_DEBUG, "%s: NAT-T OAr present\n", __func__));
4655 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
4656 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
4657 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
4658 struct sadb_x_nat_t_type *type;
4659 struct sadb_x_nat_t_port *sport, *dport;
4661 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
4662 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
4663 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
4664 ipseclog((LOG_DEBUG, "%s: invalid nat-t message "
4665 "passed.\n", __func__));
4666 return key_senderror(so, m, EINVAL);
4669 sport = (struct sadb_x_nat_t_port *)
4670 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
4671 dport = (struct sadb_x_nat_t_port *)
4672 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
4675 KEY_PORTTOSADDR(&saidx.src, sport->sadb_x_nat_t_port_port);
4677 KEY_PORTTOSADDR(&saidx.dst, dport->sadb_x_nat_t_port_port);
4681 /* SPI allocation */
4682 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4685 return key_senderror(so, m, EINVAL);
4687 /* get a SA index */
4688 if ((newsah = key_getsah(&saidx)) == NULL) {
4689 /* create a new SA index */
4690 if ((newsah = key_newsah(&saidx)) == NULL) {
4691 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
4692 return key_senderror(so, m, ENOBUFS);
4698 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4699 if (newsav == NULL) {
4700 /* XXX don't free new SA index allocated in above. */
4701 return key_senderror(so, m, error);
4705 newsav->spi = htonl(spi);
4707 /* delete the entry in acqtree */
4708 if (mhp->msg->sadb_msg_seq != 0) {
4710 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4711 /* reset counter in order to deletion by timehandler. */
4712 acq->created = time_second;
4718 struct mbuf *n, *nn;
4719 struct sadb_sa *m_sa;
4720 struct sadb_msg *newmsg;
4723 /* create new sadb_msg to reply. */
4724 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4725 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4727 MGETHDR(n, M_NOWAIT, MT_DATA);
4729 MCLGET(n, M_NOWAIT);
4730 if ((n->m_flags & M_EXT) == 0) {
4736 return key_senderror(so, m, ENOBUFS);
4742 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4743 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4745 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4746 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4747 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4748 m_sa->sadb_sa_spi = htonl(spi);
4749 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4751 IPSEC_ASSERT(off == len,
4752 ("length inconsistency (off %u len %u)", off, len));
4754 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4755 SADB_EXT_ADDRESS_DST);
4758 return key_senderror(so, m, ENOBUFS);
4761 if (n->m_len < sizeof(struct sadb_msg)) {
4762 n = m_pullup(n, sizeof(struct sadb_msg));
4764 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4767 n->m_pkthdr.len = 0;
4768 for (nn = n; nn; nn = nn->m_next)
4769 n->m_pkthdr.len += nn->m_len;
4771 newmsg = mtod(n, struct sadb_msg *);
4772 newmsg->sadb_msg_seq = newsav->seq;
4773 newmsg->sadb_msg_errno = 0;
4774 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4777 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4782 * allocating new SPI
4783 * called by key_getspi().
4789 key_do_getnewspi(struct sadb_spirange *spirange, struct secasindex *saidx)
4793 int count = V_key_spi_trycnt;
4795 /* set spi range to allocate */
4796 if (spirange != NULL) {
4797 min = spirange->sadb_spirange_min;
4798 max = spirange->sadb_spirange_max;
4800 min = V_key_spi_minval;
4801 max = V_key_spi_maxval;
4803 /* IPCOMP needs 2-byte SPI */
4804 if (saidx->proto == IPPROTO_IPCOMP) {
4811 t = min; min = max; max = t;
4816 if (key_checkspidup(saidx, min) != NULL) {
4817 ipseclog((LOG_DEBUG, "%s: SPI %u exists already.\n",
4822 count--; /* taking one cost. */
4830 /* when requesting to allocate spi ranged */
4832 /* generate pseudo-random SPI value ranged. */
4833 newspi = min + (key_random() % (max - min + 1));
4835 if (key_checkspidup(saidx, newspi) == NULL)
4839 if (count == 0 || newspi == 0) {
4840 ipseclog((LOG_DEBUG, "%s: to allocate spi is failed.\n",
4847 keystat.getspi_count =
4848 (keystat.getspi_count + V_key_spi_trycnt - count) / 2;
4854 * SADB_UPDATE processing
4856 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4857 * key(AE), (identity(SD),) (sensitivity)>
4858 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4860 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4861 * (identity(SD),) (sensitivity)>
4864 * m will always be freed.
4867 key_update(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
4869 struct sadb_sa *sa0;
4870 struct sadb_address *src0, *dst0;
4872 struct sadb_x_nat_t_type *type;
4873 struct sadb_x_nat_t_port *sport, *dport;
4874 struct sadb_address *iaddr, *raddr;
4875 struct sadb_x_nat_t_frag *frag;
4877 struct secasindex saidx;
4878 struct secashead *sah;
4879 struct secasvar *sav;
4885 IPSEC_ASSERT(so != NULL, ("null socket"));
4886 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4887 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4888 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4890 /* map satype to proto */
4891 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4892 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4894 return key_senderror(so, m, EINVAL);
4897 if (mhp->ext[SADB_EXT_SA] == NULL ||
4898 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4899 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
4900 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
4901 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
4902 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
4903 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
4904 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
4905 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
4906 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
4907 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
4908 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4910 return key_senderror(so, m, EINVAL);
4912 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
4913 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4914 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4915 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4917 return key_senderror(so, m, EINVAL);
4919 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4920 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4921 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4923 mode = IPSEC_MODE_ANY;
4926 /* XXX boundary checking for other extensions */
4928 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
4929 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4930 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4932 /* XXX boundary check against sa_len */
4933 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4936 * Make sure the port numbers are zero.
4937 * In case of NAT-T we will update them later if needed.
4939 KEY_PORTTOSADDR(&saidx.src, 0);
4940 KEY_PORTTOSADDR(&saidx.dst, 0);
4944 * Handle NAT-T info if present.
4946 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
4947 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
4948 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
4950 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
4951 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
4952 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
4953 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
4955 return key_senderror(so, m, EINVAL);
4958 type = (struct sadb_x_nat_t_type *)
4959 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
4960 sport = (struct sadb_x_nat_t_port *)
4961 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
4962 dport = (struct sadb_x_nat_t_port *)
4963 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
4968 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
4969 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
4970 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
4971 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
4972 ipseclog((LOG_DEBUG, "%s: invalid message\n",
4974 return key_senderror(so, m, EINVAL);
4976 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
4977 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
4978 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
4980 iaddr = raddr = NULL;
4982 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
4983 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
4984 ipseclog((LOG_DEBUG, "%s: invalid message\n",
4986 return key_senderror(so, m, EINVAL);
4988 frag = (struct sadb_x_nat_t_frag *)
4989 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
4995 /* get a SA header */
4996 if ((sah = key_getsah(&saidx)) == NULL) {
4997 ipseclog((LOG_DEBUG, "%s: no SA index found.\n", __func__));
4998 return key_senderror(so, m, ENOENT);
5001 /* set spidx if there */
5003 error = key_setident(sah, m, mhp);
5005 return key_senderror(so, m, error);
5007 /* find a SA with sequence number. */
5008 #ifdef IPSEC_DOSEQCHECK
5009 if (mhp->msg->sadb_msg_seq != 0
5010 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
5011 ipseclog((LOG_DEBUG, "%s: no larval SA with sequence %u "
5012 "exists.\n", __func__, mhp->msg->sadb_msg_seq));
5013 return key_senderror(so, m, ENOENT);
5017 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5020 ipseclog((LOG_DEBUG, "%s: no such a SA found (spi:%u)\n",
5021 __func__, (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5022 return key_senderror(so, m, EINVAL);
5026 /* validity check */
5027 if (sav->sah->saidx.proto != proto) {
5028 ipseclog((LOG_DEBUG, "%s: protocol mismatched "
5029 "(DB=%u param=%u)\n", __func__,
5030 sav->sah->saidx.proto, proto));
5031 return key_senderror(so, m, EINVAL);
5033 #ifdef IPSEC_DOSEQCHECK
5034 if (sav->spi != sa0->sadb_sa_spi) {
5035 ipseclog((LOG_DEBUG, "%s: SPI mismatched (DB:%u param:%u)\n",
5037 (u_int32_t)ntohl(sav->spi),
5038 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5039 return key_senderror(so, m, EINVAL);
5042 if (sav->pid != mhp->msg->sadb_msg_pid) {
5043 ipseclog((LOG_DEBUG, "%s: pid mismatched (DB:%u param:%u)\n",
5044 __func__, sav->pid, mhp->msg->sadb_msg_pid));
5045 return key_senderror(so, m, EINVAL);
5048 /* copy sav values */
5049 error = key_setsaval(sav, m, mhp);
5052 return key_senderror(so, m, error);
5057 * Handle more NAT-T info if present,
5058 * now that we have a sav to fill.
5061 sav->natt_type = type->sadb_x_nat_t_type_type;
5064 KEY_PORTTOSADDR(&sav->sah->saidx.src,
5065 sport->sadb_x_nat_t_port_port);
5067 KEY_PORTTOSADDR(&sav->sah->saidx.dst,
5068 dport->sadb_x_nat_t_port_port);
5072 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5073 * We should actually check for a minimum MTU here, if we
5074 * want to support it in ip_output.
5077 sav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5081 /* check SA values to be mature. */
5082 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
5084 return key_senderror(so, m, 0);
5090 /* set msg buf from mhp */
5091 n = key_getmsgbuf_x1(m, mhp);
5093 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5094 return key_senderror(so, m, ENOBUFS);
5098 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5103 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
5104 * only called by key_update().
5107 * others : found, pointer to a SA.
5109 #ifdef IPSEC_DOSEQCHECK
5110 static struct secasvar *
5111 key_getsavbyseq(struct secashead *sah, u_int32_t seq)
5113 struct secasvar *sav;
5116 state = SADB_SASTATE_LARVAL;
5118 /* search SAD with sequence number ? */
5119 LIST_FOREACH(sav, &sah->savtree[state], chain) {
5121 KEY_CHKSASTATE(state, sav->state, __func__);
5123 if (sav->seq == seq) {
5125 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
5126 printf("DP %s cause refcnt++:%d SA:%p\n",
5127 __func__, sav->refcnt, sav));
5137 * SADB_ADD processing
5138 * add an entry to SA database, when received
5139 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5140 * key(AE), (identity(SD),) (sensitivity)>
5143 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5144 * (identity(SD),) (sensitivity)>
5147 * IGNORE identity and sensitivity messages.
5149 * m will always be freed.
5152 key_add(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
5154 struct sadb_sa *sa0;
5155 struct sadb_address *src0, *dst0;
5157 struct sadb_x_nat_t_type *type;
5158 struct sadb_address *iaddr, *raddr;
5159 struct sadb_x_nat_t_frag *frag;
5161 struct secasindex saidx;
5162 struct secashead *newsah;
5163 struct secasvar *newsav;
5169 IPSEC_ASSERT(so != NULL, ("null socket"));
5170 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5171 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5172 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5174 /* map satype to proto */
5175 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5176 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5178 return key_senderror(so, m, EINVAL);
5181 if (mhp->ext[SADB_EXT_SA] == NULL ||
5182 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5183 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5184 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5185 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5186 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5187 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5188 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5189 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5190 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5191 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5192 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5194 return key_senderror(so, m, EINVAL);
5196 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5197 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5198 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5200 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5202 return key_senderror(so, m, EINVAL);
5204 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5205 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5206 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5208 mode = IPSEC_MODE_ANY;
5212 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5213 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5214 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5216 /* XXX boundary check against sa_len */
5217 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5220 * Make sure the port numbers are zero.
5221 * In case of NAT-T we will update them later if needed.
5223 KEY_PORTTOSADDR(&saidx.src, 0);
5224 KEY_PORTTOSADDR(&saidx.dst, 0);
5228 * Handle NAT-T info if present.
5230 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
5231 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5232 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5233 struct sadb_x_nat_t_port *sport, *dport;
5235 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
5236 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5237 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5238 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5240 return key_senderror(so, m, EINVAL);
5243 type = (struct sadb_x_nat_t_type *)
5244 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5245 sport = (struct sadb_x_nat_t_port *)
5246 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5247 dport = (struct sadb_x_nat_t_port *)
5248 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5251 KEY_PORTTOSADDR(&saidx.src,
5252 sport->sadb_x_nat_t_port_port);
5254 KEY_PORTTOSADDR(&saidx.dst,
5255 dport->sadb_x_nat_t_port_port);
5259 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
5260 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
5261 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
5262 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
5263 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5265 return key_senderror(so, m, EINVAL);
5267 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5268 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5269 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
5271 iaddr = raddr = NULL;
5273 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5274 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5275 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5277 return key_senderror(so, m, EINVAL);
5279 frag = (struct sadb_x_nat_t_frag *)
5280 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5286 /* get a SA header */
5287 if ((newsah = key_getsah(&saidx)) == NULL) {
5288 /* create a new SA header */
5289 if ((newsah = key_newsah(&saidx)) == NULL) {
5290 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
5291 return key_senderror(so, m, ENOBUFS);
5295 /* set spidx if there */
5297 error = key_setident(newsah, m, mhp);
5299 return key_senderror(so, m, error);
5302 /* create new SA entry. */
5303 /* We can create new SA only if SPI is differenct. */
5305 newsav = key_getsavbyspi(newsah, sa0->sadb_sa_spi);
5307 if (newsav != NULL) {
5308 ipseclog((LOG_DEBUG, "%s: SA already exists.\n", __func__));
5309 return key_senderror(so, m, EEXIST);
5311 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
5312 if (newsav == NULL) {
5313 return key_senderror(so, m, error);
5318 * Handle more NAT-T info if present,
5319 * now that we have a sav to fill.
5322 newsav->natt_type = type->sadb_x_nat_t_type_type;
5326 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5327 * We should actually check for a minimum MTU here, if we
5328 * want to support it in ip_output.
5331 newsav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5335 /* check SA values to be mature. */
5336 if ((error = key_mature(newsav)) != 0) {
5337 KEY_FREESAV(&newsav);
5338 return key_senderror(so, m, error);
5342 * don't call key_freesav() here, as we would like to keep the SA
5343 * in the database on success.
5349 /* set msg buf from mhp */
5350 n = key_getmsgbuf_x1(m, mhp);
5352 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5353 return key_senderror(so, m, ENOBUFS);
5357 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5363 key_setident(struct secashead *sah, struct mbuf *m,
5364 const struct sadb_msghdr *mhp)
5366 const struct sadb_ident *idsrc, *iddst;
5367 int idsrclen, iddstlen;
5369 IPSEC_ASSERT(sah != NULL, ("null secashead"));
5370 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5371 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5372 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5374 /* don't make buffer if not there */
5375 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
5376 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5382 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
5383 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5384 ipseclog((LOG_DEBUG, "%s: invalid identity.\n", __func__));
5388 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
5389 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
5390 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
5391 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
5393 /* validity check */
5394 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
5395 ipseclog((LOG_DEBUG, "%s: ident type mismatch.\n", __func__));
5399 switch (idsrc->sadb_ident_type) {
5400 case SADB_IDENTTYPE_PREFIX:
5401 case SADB_IDENTTYPE_FQDN:
5402 case SADB_IDENTTYPE_USERFQDN:
5404 /* XXX do nothing */
5410 /* make structure */
5411 sah->idents = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5412 if (sah->idents == NULL) {
5413 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5416 sah->identd = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5417 if (sah->identd == NULL) {
5418 free(sah->idents, M_IPSEC_MISC);
5420 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5423 sah->idents->type = idsrc->sadb_ident_type;
5424 sah->idents->id = idsrc->sadb_ident_id;
5426 sah->identd->type = iddst->sadb_ident_type;
5427 sah->identd->id = iddst->sadb_ident_id;
5433 * m will not be freed on return.
5434 * it is caller's responsibility to free the result.
5436 static struct mbuf *
5437 key_getmsgbuf_x1(struct mbuf *m, const struct sadb_msghdr *mhp)
5441 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5442 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5443 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5445 /* create new sadb_msg to reply. */
5446 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5447 SADB_EXT_SA, SADB_X_EXT_SA2,
5448 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5449 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5450 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5454 if (n->m_len < sizeof(struct sadb_msg)) {
5455 n = m_pullup(n, sizeof(struct sadb_msg));
5459 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5460 mtod(n, struct sadb_msg *)->sadb_msg_len =
5461 PFKEY_UNIT64(n->m_pkthdr.len);
5467 * SADB_DELETE processing
5469 * <base, SA(*), address(SD)>
5470 * from the ikmpd, and set SADB_SASTATE_DEAD,
5472 * <base, SA(*), address(SD)>
5475 * m will always be freed.
5478 key_delete(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
5480 struct sadb_sa *sa0;
5481 struct sadb_address *src0, *dst0;
5482 struct secasindex saidx;
5483 struct secashead *sah;
5484 struct secasvar *sav = NULL;
5487 IPSEC_ASSERT(so != NULL, ("null socket"));
5488 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5489 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5490 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5492 /* map satype to proto */
5493 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5494 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5496 return key_senderror(so, m, EINVAL);
5499 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5500 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5501 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5503 return key_senderror(so, m, EINVAL);
5506 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5507 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5508 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5510 return key_senderror(so, m, EINVAL);
5513 if (mhp->ext[SADB_EXT_SA] == NULL) {
5515 * Caller wants us to delete all non-LARVAL SAs
5516 * that match the src/dst. This is used during
5517 * IKE INITIAL-CONTACT.
5519 ipseclog((LOG_DEBUG, "%s: doing delete all.\n", __func__));
5520 return key_delete_all(so, m, mhp, proto);
5521 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5522 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5524 return key_senderror(so, m, EINVAL);
5527 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5528 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5529 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5531 /* XXX boundary check against sa_len */
5532 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5535 * Make sure the port numbers are zero.
5536 * In case of NAT-T we will update them later if needed.
5538 KEY_PORTTOSADDR(&saidx.src, 0);
5539 KEY_PORTTOSADDR(&saidx.dst, 0);
5543 * Handle NAT-T info if present.
5545 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5546 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5547 struct sadb_x_nat_t_port *sport, *dport;
5549 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5550 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5551 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5553 return key_senderror(so, m, EINVAL);
5556 sport = (struct sadb_x_nat_t_port *)
5557 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5558 dport = (struct sadb_x_nat_t_port *)
5559 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5562 KEY_PORTTOSADDR(&saidx.src,
5563 sport->sadb_x_nat_t_port_port);
5565 KEY_PORTTOSADDR(&saidx.dst,
5566 dport->sadb_x_nat_t_port_port);
5570 /* get a SA header */
5572 LIST_FOREACH(sah, &V_sahtree, chain) {
5573 if (sah->state == SADB_SASTATE_DEAD)
5575 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5578 /* get a SA with SPI. */
5579 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5585 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5586 return key_senderror(so, m, ENOENT);
5589 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5595 struct sadb_msg *newmsg;
5597 /* create new sadb_msg to reply. */
5598 /* XXX-BZ NAT-T extensions? */
5599 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5600 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5602 return key_senderror(so, m, ENOBUFS);
5604 if (n->m_len < sizeof(struct sadb_msg)) {
5605 n = m_pullup(n, sizeof(struct sadb_msg));
5607 return key_senderror(so, m, ENOBUFS);
5609 newmsg = mtod(n, struct sadb_msg *);
5610 newmsg->sadb_msg_errno = 0;
5611 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5614 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5619 * delete all SAs for src/dst. Called from key_delete().
5622 key_delete_all(struct socket *so, struct mbuf *m,
5623 const struct sadb_msghdr *mhp, u_int16_t proto)
5625 struct sadb_address *src0, *dst0;
5626 struct secasindex saidx;
5627 struct secashead *sah;
5628 struct secasvar *sav, *nextsav;
5629 u_int stateidx, state;
5631 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5632 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5634 /* XXX boundary check against sa_len */
5635 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5638 * Make sure the port numbers are zero.
5639 * In case of NAT-T we will update them later if needed.
5641 KEY_PORTTOSADDR(&saidx.src, 0);
5642 KEY_PORTTOSADDR(&saidx.dst, 0);
5646 * Handle NAT-T info if present.
5649 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5650 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5651 struct sadb_x_nat_t_port *sport, *dport;
5653 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5654 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5655 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5657 return key_senderror(so, m, EINVAL);
5660 sport = (struct sadb_x_nat_t_port *)
5661 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5662 dport = (struct sadb_x_nat_t_port *)
5663 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5666 KEY_PORTTOSADDR(&saidx.src,
5667 sport->sadb_x_nat_t_port_port);
5669 KEY_PORTTOSADDR(&saidx.dst,
5670 dport->sadb_x_nat_t_port_port);
5675 LIST_FOREACH(sah, &V_sahtree, chain) {
5676 if (sah->state == SADB_SASTATE_DEAD)
5678 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5681 /* Delete all non-LARVAL SAs. */
5683 stateidx < _ARRAYLEN(saorder_state_alive);
5685 state = saorder_state_alive[stateidx];
5686 if (state == SADB_SASTATE_LARVAL)
5688 for (sav = LIST_FIRST(&sah->savtree[state]);
5689 sav != NULL; sav = nextsav) {
5690 nextsav = LIST_NEXT(sav, chain);
5692 if (sav->state != state) {
5693 ipseclog((LOG_DEBUG, "%s: invalid "
5694 "sav->state (queue %d SA %d)\n",
5695 __func__, state, sav->state));
5699 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5707 struct sadb_msg *newmsg;
5709 /* create new sadb_msg to reply. */
5710 /* XXX-BZ NAT-T extensions? */
5711 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5712 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5714 return key_senderror(so, m, ENOBUFS);
5716 if (n->m_len < sizeof(struct sadb_msg)) {
5717 n = m_pullup(n, sizeof(struct sadb_msg));
5719 return key_senderror(so, m, ENOBUFS);
5721 newmsg = mtod(n, struct sadb_msg *);
5722 newmsg->sadb_msg_errno = 0;
5723 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5726 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5731 * SADB_GET processing
5733 * <base, SA(*), address(SD)>
5734 * from the ikmpd, and get a SP and a SA to respond,
5736 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5737 * (identity(SD),) (sensitivity)>
5740 * m will always be freed.
5743 key_get(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
5745 struct sadb_sa *sa0;
5746 struct sadb_address *src0, *dst0;
5747 struct secasindex saidx;
5748 struct secashead *sah;
5749 struct secasvar *sav = NULL;
5752 IPSEC_ASSERT(so != NULL, ("null socket"));
5753 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5754 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5755 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5757 /* map satype to proto */
5758 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5759 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5761 return key_senderror(so, m, EINVAL);
5764 if (mhp->ext[SADB_EXT_SA] == NULL ||
5765 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5766 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5767 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5769 return key_senderror(so, m, EINVAL);
5771 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5772 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5773 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5774 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5776 return key_senderror(so, m, EINVAL);
5779 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5780 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5781 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5783 /* XXX boundary check against sa_len */
5784 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5787 * Make sure the port numbers are zero.
5788 * In case of NAT-T we will update them later if needed.
5790 KEY_PORTTOSADDR(&saidx.src, 0);
5791 KEY_PORTTOSADDR(&saidx.dst, 0);
5795 * Handle NAT-T info if present.
5798 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5799 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5800 struct sadb_x_nat_t_port *sport, *dport;
5802 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5803 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5804 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5806 return key_senderror(so, m, EINVAL);
5809 sport = (struct sadb_x_nat_t_port *)
5810 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5811 dport = (struct sadb_x_nat_t_port *)
5812 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5815 KEY_PORTTOSADDR(&saidx.src,
5816 sport->sadb_x_nat_t_port_port);
5818 KEY_PORTTOSADDR(&saidx.dst,
5819 dport->sadb_x_nat_t_port_port);
5823 /* get a SA header */
5825 LIST_FOREACH(sah, &V_sahtree, chain) {
5826 if (sah->state == SADB_SASTATE_DEAD)
5828 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5831 /* get a SA with SPI. */
5832 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5838 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5839 return key_senderror(so, m, ENOENT);
5846 /* map proto to satype */
5847 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5848 ipseclog((LOG_DEBUG, "%s: there was invalid proto in SAD.\n",
5850 return key_senderror(so, m, EINVAL);
5853 /* create new sadb_msg to reply. */
5854 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5855 mhp->msg->sadb_msg_pid);
5857 return key_senderror(so, m, ENOBUFS);
5860 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5864 /* XXX make it sysctl-configurable? */
5866 key_getcomb_setlifetime(struct sadb_comb *comb)
5869 comb->sadb_comb_soft_allocations = 1;
5870 comb->sadb_comb_hard_allocations = 1;
5871 comb->sadb_comb_soft_bytes = 0;
5872 comb->sadb_comb_hard_bytes = 0;
5873 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5874 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5875 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
5876 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
5880 * XXX reorder combinations by preference
5881 * XXX no idea if the user wants ESP authentication or not
5883 static struct mbuf *
5886 struct sadb_comb *comb;
5887 struct enc_xform *algo;
5888 struct mbuf *result = NULL, *m, *n;
5892 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5895 for (i = 1; i <= SADB_EALG_MAX; i++) {
5896 algo = esp_algorithm_lookup(i);
5900 /* discard algorithms with key size smaller than system min */
5901 if (_BITS(algo->maxkey) < V_ipsec_esp_keymin)
5903 if (_BITS(algo->minkey) < V_ipsec_esp_keymin)
5904 encmin = V_ipsec_esp_keymin;
5906 encmin = _BITS(algo->minkey);
5908 if (V_ipsec_esp_auth)
5909 m = key_getcomb_ah();
5911 IPSEC_ASSERT(l <= MLEN,
5912 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
5913 MGET(m, M_NOWAIT, MT_DATA);
5918 bzero(mtod(m, caddr_t), m->m_len);
5925 for (n = m; n; n = n->m_next)
5927 IPSEC_ASSERT((totlen % l) == 0, ("totlen=%u, l=%u", totlen, l));
5929 for (off = 0; off < totlen; off += l) {
5930 n = m_pulldown(m, off, l, &o);
5932 /* m is already freed */
5935 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
5936 bzero(comb, sizeof(*comb));
5937 key_getcomb_setlifetime(comb);
5938 comb->sadb_comb_encrypt = i;
5939 comb->sadb_comb_encrypt_minbits = encmin;
5940 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
5958 key_getsizes_ah(const struct auth_hash *ah, int alg, u_int16_t* min,
5962 *min = *max = ah->keysize;
5963 if (ah->keysize == 0) {
5965 * Transform takes arbitrary key size but algorithm
5966 * key size is restricted. Enforce this here.
5969 case SADB_X_AALG_MD5: *min = *max = 16; break;
5970 case SADB_X_AALG_SHA: *min = *max = 20; break;
5971 case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
5972 case SADB_X_AALG_SHA2_256: *min = *max = 32; break;
5973 case SADB_X_AALG_SHA2_384: *min = *max = 48; break;
5974 case SADB_X_AALG_SHA2_512: *min = *max = 64; break;
5976 DPRINTF(("%s: unknown AH algorithm %u\n",
5984 * XXX reorder combinations by preference
5986 static struct mbuf *
5989 struct sadb_comb *comb;
5990 struct auth_hash *algo;
5992 u_int16_t minkeysize, maxkeysize;
5994 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
5997 for (i = 1; i <= SADB_AALG_MAX; i++) {
5999 /* we prefer HMAC algorithms, not old algorithms */
6000 if (i != SADB_AALG_SHA1HMAC &&
6001 i != SADB_AALG_MD5HMAC &&
6002 i != SADB_X_AALG_SHA2_256 &&
6003 i != SADB_X_AALG_SHA2_384 &&
6004 i != SADB_X_AALG_SHA2_512)
6007 algo = ah_algorithm_lookup(i);
6010 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
6011 /* discard algorithms with key size smaller than system min */
6012 if (_BITS(minkeysize) < V_ipsec_ah_keymin)
6016 IPSEC_ASSERT(l <= MLEN,
6017 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6018 MGET(m, M_NOWAIT, MT_DATA);
6025 M_PREPEND(m, l, M_NOWAIT);
6029 comb = mtod(m, struct sadb_comb *);
6030 bzero(comb, sizeof(*comb));
6031 key_getcomb_setlifetime(comb);
6032 comb->sadb_comb_auth = i;
6033 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
6034 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
6041 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
6042 * XXX reorder combinations by preference
6044 static struct mbuf *
6045 key_getcomb_ipcomp()
6047 struct sadb_comb *comb;
6048 struct comp_algo *algo;
6051 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6054 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
6055 algo = ipcomp_algorithm_lookup(i);
6060 IPSEC_ASSERT(l <= MLEN,
6061 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6062 MGET(m, M_NOWAIT, MT_DATA);
6069 M_PREPEND(m, l, M_NOWAIT);
6073 comb = mtod(m, struct sadb_comb *);
6074 bzero(comb, sizeof(*comb));
6075 key_getcomb_setlifetime(comb);
6076 comb->sadb_comb_encrypt = i;
6077 /* what should we set into sadb_comb_*_{min,max}bits? */
6084 * XXX no way to pass mode (transport/tunnel) to userland
6085 * XXX replay checking?
6086 * XXX sysctl interface to ipsec_{ah,esp}_keymin
6088 static struct mbuf *
6089 key_getprop(const struct secasindex *saidx)
6091 struct sadb_prop *prop;
6093 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
6096 switch (saidx->proto) {
6098 m = key_getcomb_esp();
6101 m = key_getcomb_ah();
6103 case IPPROTO_IPCOMP:
6104 m = key_getcomb_ipcomp();
6112 M_PREPEND(m, l, M_NOWAIT);
6117 for (n = m; n; n = n->m_next)
6120 prop = mtod(m, struct sadb_prop *);
6121 bzero(prop, sizeof(*prop));
6122 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
6123 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
6124 prop->sadb_prop_replay = 32; /* XXX */
6130 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
6132 * <base, SA, address(SD), (address(P)), x_policy,
6133 * (identity(SD),) (sensitivity,) proposal>
6134 * to KMD, and expect to receive
6135 * <base> with SADB_ACQUIRE if error occured,
6137 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
6138 * from KMD by PF_KEY.
6140 * XXX x_policy is outside of RFC2367 (KAME extension).
6141 * XXX sensitivity is not supported.
6142 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
6143 * see comment for key_getcomb_ipcomp().
6147 * others: error number
6150 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
6152 union sockaddr_union addr;
6153 struct mbuf *result, *m;
6154 struct secacq *newacq;
6158 u_int8_t mask, satype;
6160 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
6161 satype = key_proto2satype(saidx->proto);
6162 IPSEC_ASSERT(satype != 0, ("null satype, protocol %u", saidx->proto));
6166 ul_proto = IPSEC_ULPROTO_ANY;
6168 * We never do anything about acquirng SA. There is anather
6169 * solution that kernel blocks to send SADB_ACQUIRE message until
6170 * getting something message from IKEd. In later case, to be
6171 * managed with ACQUIRING list.
6173 /* Get an entry to check whether sending message or not. */
6174 if ((newacq = key_getacq(saidx)) != NULL) {
6175 if (V_key_blockacq_count < newacq->count) {
6176 /* reset counter and do send message. */
6179 /* increment counter and do nothing. */
6184 /* make new entry for blocking to send SADB_ACQUIRE. */
6185 if ((newacq = key_newacq(saidx)) == NULL)
6191 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
6199 * No SADB_X_EXT_NAT_T_* here: we do not know
6200 * anything related to NAT-T at this time.
6204 * set sadb_address for saidx's.
6206 * Note that if sp is supplied, then we're being called from
6207 * key_checkrequest and should supply port and protocol information.
6209 if (sp != NULL && (sp->spidx.ul_proto == IPPROTO_TCP ||
6210 sp->spidx.ul_proto == IPPROTO_UDP))
6211 ul_proto = sp->spidx.ul_proto;
6215 if (ul_proto != IPSEC_ULPROTO_ANY) {
6216 switch (sp->spidx.src.sa.sa_family) {
6218 if (sp->spidx.src.sin.sin_port != IPSEC_PORT_ANY) {
6219 addr.sin.sin_port = sp->spidx.src.sin.sin_port;
6220 mask = sp->spidx.prefs;
6224 if (sp->spidx.src.sin6.sin6_port != IPSEC_PORT_ANY) {
6225 addr.sin6.sin6_port = sp->spidx.src.sin6.sin6_port;
6226 mask = sp->spidx.prefs;
6233 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC, &addr.sa, mask, ul_proto);
6242 if (ul_proto != IPSEC_ULPROTO_ANY) {
6243 switch (sp->spidx.dst.sa.sa_family) {
6245 if (sp->spidx.dst.sin.sin_port != IPSEC_PORT_ANY) {
6246 addr.sin.sin_port = sp->spidx.dst.sin.sin_port;
6247 mask = sp->spidx.prefd;
6251 if (sp->spidx.dst.sin6.sin6_port != IPSEC_PORT_ANY) {
6252 addr.sin6.sin6_port = sp->spidx.dst.sin6.sin6_port;
6253 mask = sp->spidx.prefd;
6260 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST, &addr.sa, mask, ul_proto);
6267 /* XXX proxy address (optional) */
6269 /* set sadb_x_policy */
6271 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
6279 /* XXX identity (optional) */
6281 if (idexttype && fqdn) {
6282 /* create identity extension (FQDN) */
6283 struct sadb_ident *id;
6286 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
6287 id = (struct sadb_ident *)p;
6288 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6289 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6290 id->sadb_ident_exttype = idexttype;
6291 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
6292 bcopy(fqdn, id + 1, fqdnlen);
6293 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
6297 /* create identity extension (USERFQDN) */
6298 struct sadb_ident *id;
6302 /* +1 for terminating-NUL */
6303 userfqdnlen = strlen(userfqdn) + 1;
6306 id = (struct sadb_ident *)p;
6307 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6308 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6309 id->sadb_ident_exttype = idexttype;
6310 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
6311 /* XXX is it correct? */
6312 if (curproc && curproc->p_cred)
6313 id->sadb_ident_id = curproc->p_cred->p_ruid;
6314 if (userfqdn && userfqdnlen)
6315 bcopy(userfqdn, id + 1, userfqdnlen);
6316 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
6320 /* XXX sensitivity (optional) */
6322 /* create proposal/combination extension */
6323 m = key_getprop(saidx);
6326 * spec conformant: always attach proposal/combination extension,
6327 * the problem is that we have no way to attach it for ipcomp,
6328 * due to the way sadb_comb is declared in RFC2367.
6337 * outside of spec; make proposal/combination extension optional.
6343 if ((result->m_flags & M_PKTHDR) == 0) {
6348 if (result->m_len < sizeof(struct sadb_msg)) {
6349 result = m_pullup(result, sizeof(struct sadb_msg));
6350 if (result == NULL) {
6356 result->m_pkthdr.len = 0;
6357 for (m = result; m; m = m->m_next)
6358 result->m_pkthdr.len += m->m_len;
6360 mtod(result, struct sadb_msg *)->sadb_msg_len =
6361 PFKEY_UNIT64(result->m_pkthdr.len);
6363 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6371 static struct secacq *
6372 key_newacq(const struct secasindex *saidx)
6374 struct secacq *newacq;
6377 newacq = malloc(sizeof(struct secacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6378 if (newacq == NULL) {
6379 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6384 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
6385 newacq->seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq);
6386 newacq->created = time_second;
6389 /* add to acqtree */
6391 LIST_INSERT_HEAD(&V_acqtree, newacq, chain);
6397 static struct secacq *
6398 key_getacq(const struct secasindex *saidx)
6403 LIST_FOREACH(acq, &V_acqtree, chain) {
6404 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
6412 static struct secacq *
6413 key_getacqbyseq(u_int32_t seq)
6418 LIST_FOREACH(acq, &V_acqtree, chain) {
6419 if (acq->seq == seq)
6427 static struct secspacq *
6428 key_newspacq(struct secpolicyindex *spidx)
6430 struct secspacq *acq;
6433 acq = malloc(sizeof(struct secspacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6435 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6440 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
6441 acq->created = time_second;
6444 /* add to spacqtree */
6446 LIST_INSERT_HEAD(&V_spacqtree, acq, chain);
6452 static struct secspacq *
6453 key_getspacq(struct secpolicyindex *spidx)
6455 struct secspacq *acq;
6458 LIST_FOREACH(acq, &V_spacqtree, chain) {
6459 if (key_cmpspidx_exactly(spidx, &acq->spidx)) {
6460 /* NB: return holding spacq_lock */
6470 * SADB_ACQUIRE processing,
6471 * in first situation, is receiving
6473 * from the ikmpd, and clear sequence of its secasvar entry.
6475 * In second situation, is receiving
6476 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6477 * from a user land process, and return
6478 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6481 * m will always be freed.
6484 key_acquire2(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
6486 const struct sadb_address *src0, *dst0;
6487 struct secasindex saidx;
6488 struct secashead *sah;
6492 IPSEC_ASSERT(so != NULL, ("null socket"));
6493 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6494 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6495 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6498 * Error message from KMd.
6499 * We assume that if error was occured in IKEd, the length of PFKEY
6500 * message is equal to the size of sadb_msg structure.
6501 * We do not raise error even if error occured in this function.
6503 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
6506 /* check sequence number */
6507 if (mhp->msg->sadb_msg_seq == 0) {
6508 ipseclog((LOG_DEBUG, "%s: must specify sequence "
6509 "number.\n", __func__));
6514 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
6516 * the specified larval SA is already gone, or we got
6517 * a bogus sequence number. we can silently ignore it.
6523 /* reset acq counter in order to deletion by timehander. */
6524 acq->created = time_second;
6531 * This message is from user land.
6534 /* map satype to proto */
6535 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6536 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6538 return key_senderror(so, m, EINVAL);
6541 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
6542 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
6543 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
6545 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6547 return key_senderror(so, m, EINVAL);
6549 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
6550 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
6551 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
6553 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6555 return key_senderror(so, m, EINVAL);
6558 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
6559 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
6561 /* XXX boundary check against sa_len */
6562 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
6565 * Make sure the port numbers are zero.
6566 * In case of NAT-T we will update them later if needed.
6568 KEY_PORTTOSADDR(&saidx.src, 0);
6569 KEY_PORTTOSADDR(&saidx.dst, 0);
6573 * Handle NAT-T info if present.
6576 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
6577 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
6578 struct sadb_x_nat_t_port *sport, *dport;
6580 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
6581 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
6582 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
6584 return key_senderror(so, m, EINVAL);
6587 sport = (struct sadb_x_nat_t_port *)
6588 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
6589 dport = (struct sadb_x_nat_t_port *)
6590 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
6593 KEY_PORTTOSADDR(&saidx.src,
6594 sport->sadb_x_nat_t_port_port);
6596 KEY_PORTTOSADDR(&saidx.dst,
6597 dport->sadb_x_nat_t_port_port);
6601 /* get a SA index */
6603 LIST_FOREACH(sah, &V_sahtree, chain) {
6604 if (sah->state == SADB_SASTATE_DEAD)
6606 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
6611 ipseclog((LOG_DEBUG, "%s: a SA exists already.\n", __func__));
6612 return key_senderror(so, m, EEXIST);
6615 error = key_acquire(&saidx, NULL);
6617 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
6618 __func__, mhp->msg->sadb_msg_errno));
6619 return key_senderror(so, m, error);
6622 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6626 * SADB_REGISTER processing.
6627 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6630 * from the ikmpd, and register a socket to send PF_KEY messages,
6634 * If socket is detached, must free from regnode.
6636 * m will always be freed.
6639 key_register(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
6641 struct secreg *reg, *newreg = 0;
6643 IPSEC_ASSERT(so != NULL, ("null socket"));
6644 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6645 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6646 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6648 /* check for invalid register message */
6649 if (mhp->msg->sadb_msg_satype >= sizeof(V_regtree)/sizeof(V_regtree[0]))
6650 return key_senderror(so, m, EINVAL);
6652 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6653 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6656 /* check whether existing or not */
6658 LIST_FOREACH(reg, &V_regtree[mhp->msg->sadb_msg_satype], chain) {
6659 if (reg->so == so) {
6661 ipseclog((LOG_DEBUG, "%s: socket exists already.\n",
6663 return key_senderror(so, m, EEXIST);
6667 /* create regnode */
6668 newreg = malloc(sizeof(struct secreg), M_IPSEC_SAR, M_NOWAIT|M_ZERO);
6669 if (newreg == NULL) {
6671 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6672 return key_senderror(so, m, ENOBUFS);
6676 ((struct keycb *)sotorawcb(so))->kp_registered++;
6678 /* add regnode to regtree. */
6679 LIST_INSERT_HEAD(&V_regtree[mhp->msg->sadb_msg_satype], newreg, chain);
6685 struct sadb_msg *newmsg;
6686 struct sadb_supported *sup;
6687 u_int len, alen, elen;
6690 struct sadb_alg *alg;
6692 /* create new sadb_msg to reply. */
6694 for (i = 1; i <= SADB_AALG_MAX; i++) {
6695 if (ah_algorithm_lookup(i))
6696 alen += sizeof(struct sadb_alg);
6699 alen += sizeof(struct sadb_supported);
6701 for (i = 1; i <= SADB_EALG_MAX; i++) {
6702 if (esp_algorithm_lookup(i))
6703 elen += sizeof(struct sadb_alg);
6706 elen += sizeof(struct sadb_supported);
6708 len = sizeof(struct sadb_msg) + alen + elen;
6711 return key_senderror(so, m, ENOBUFS);
6713 MGETHDR(n, M_NOWAIT, MT_DATA);
6715 MCLGET(n, M_NOWAIT);
6716 if ((n->m_flags & M_EXT) == 0) {
6722 return key_senderror(so, m, ENOBUFS);
6724 n->m_pkthdr.len = n->m_len = len;
6728 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6729 newmsg = mtod(n, struct sadb_msg *);
6730 newmsg->sadb_msg_errno = 0;
6731 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6732 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6734 /* for authentication algorithm */
6736 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6737 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6738 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6739 off += PFKEY_ALIGN8(sizeof(*sup));
6741 for (i = 1; i <= SADB_AALG_MAX; i++) {
6742 struct auth_hash *aalgo;
6743 u_int16_t minkeysize, maxkeysize;
6745 aalgo = ah_algorithm_lookup(i);
6748 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6749 alg->sadb_alg_id = i;
6750 alg->sadb_alg_ivlen = 0;
6751 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
6752 alg->sadb_alg_minbits = _BITS(minkeysize);
6753 alg->sadb_alg_maxbits = _BITS(maxkeysize);
6754 off += PFKEY_ALIGN8(sizeof(*alg));
6758 /* for encryption algorithm */
6760 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6761 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6762 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6763 off += PFKEY_ALIGN8(sizeof(*sup));
6765 for (i = 1; i <= SADB_EALG_MAX; i++) {
6766 struct enc_xform *ealgo;
6768 ealgo = esp_algorithm_lookup(i);
6771 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6772 alg->sadb_alg_id = i;
6773 alg->sadb_alg_ivlen = ealgo->blocksize;
6774 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
6775 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
6776 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6780 IPSEC_ASSERT(off == len,
6781 ("length assumption failed (off %u len %u)", off, len));
6784 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6789 * free secreg entry registered.
6790 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6793 key_freereg(struct socket *so)
6798 IPSEC_ASSERT(so != NULL, ("NULL so"));
6801 * check whether existing or not.
6802 * check all type of SA, because there is a potential that
6803 * one socket is registered to multiple type of SA.
6806 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6807 LIST_FOREACH(reg, &V_regtree[i], chain) {
6808 if (reg->so == so && __LIST_CHAINED(reg)) {
6809 LIST_REMOVE(reg, chain);
6810 free(reg, M_IPSEC_SAR);
6819 * SADB_EXPIRE processing
6821 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6823 * NOTE: We send only soft lifetime extension.
6826 * others : error number
6829 key_expire(struct secasvar *sav, int hard)
6832 struct mbuf *result = NULL, *m;
6835 struct sadb_lifetime *lt;
6837 IPSEC_ASSERT (sav != NULL, ("null sav"));
6838 IPSEC_ASSERT (sav->sah != NULL, ("null sa header"));
6840 /* set msg header */
6841 satype = key_proto2satype(sav->sah->saidx.proto);
6842 IPSEC_ASSERT(satype != 0, ("invalid proto, satype %u", satype));
6843 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6850 /* create SA extension */
6851 m = key_setsadbsa(sav);
6858 /* create SA extension */
6859 m = key_setsadbxsa2(sav->sah->saidx.mode,
6860 sav->replay ? sav->replay->count : 0,
6861 sav->sah->saidx.reqid);
6868 /* create lifetime extension (current and soft) */
6869 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6870 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
6877 bzero(mtod(m, caddr_t), len);
6878 lt = mtod(m, struct sadb_lifetime *);
6879 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6880 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6881 lt->sadb_lifetime_allocations = sav->lft_c->allocations;
6882 lt->sadb_lifetime_bytes = sav->lft_c->bytes;
6883 lt->sadb_lifetime_addtime = sav->lft_c->addtime;
6884 lt->sadb_lifetime_usetime = sav->lft_c->usetime;
6885 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6886 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6888 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
6889 lt->sadb_lifetime_allocations = sav->lft_h->allocations;
6890 lt->sadb_lifetime_bytes = sav->lft_h->bytes;
6891 lt->sadb_lifetime_addtime = sav->lft_h->addtime;
6892 lt->sadb_lifetime_usetime = sav->lft_h->usetime;
6894 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
6895 lt->sadb_lifetime_allocations = sav->lft_s->allocations;
6896 lt->sadb_lifetime_bytes = sav->lft_s->bytes;
6897 lt->sadb_lifetime_addtime = sav->lft_s->addtime;
6898 lt->sadb_lifetime_usetime = sav->lft_s->usetime;
6902 /* set sadb_address for source */
6903 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6904 &sav->sah->saidx.src.sa,
6905 FULLMASK, IPSEC_ULPROTO_ANY);
6912 /* set sadb_address for destination */
6913 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6914 &sav->sah->saidx.dst.sa,
6915 FULLMASK, IPSEC_ULPROTO_ANY);
6923 * XXX-BZ Handle NAT-T extensions here.
6926 if ((result->m_flags & M_PKTHDR) == 0) {
6931 if (result->m_len < sizeof(struct sadb_msg)) {
6932 result = m_pullup(result, sizeof(struct sadb_msg));
6933 if (result == NULL) {
6939 result->m_pkthdr.len = 0;
6940 for (m = result; m; m = m->m_next)
6941 result->m_pkthdr.len += m->m_len;
6943 mtod(result, struct sadb_msg *)->sadb_msg_len =
6944 PFKEY_UNIT64(result->m_pkthdr.len);
6946 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6955 * SADB_FLUSH processing
6958 * from the ikmpd, and free all entries in secastree.
6962 * NOTE: to do is only marking SADB_SASTATE_DEAD.
6964 * m will always be freed.
6967 key_flush(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
6969 struct sadb_msg *newmsg;
6970 struct secashead *sah, *nextsah;
6971 struct secasvar *sav, *nextsav;
6976 IPSEC_ASSERT(so != NULL, ("null socket"));
6977 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6978 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6980 /* map satype to proto */
6981 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6982 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6984 return key_senderror(so, m, EINVAL);
6987 /* no SATYPE specified, i.e. flushing all SA. */
6989 for (sah = LIST_FIRST(&V_sahtree);
6992 nextsah = LIST_NEXT(sah, chain);
6994 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
6995 && proto != sah->saidx.proto)
6999 stateidx < _ARRAYLEN(saorder_state_alive);
7001 state = saorder_state_any[stateidx];
7002 for (sav = LIST_FIRST(&sah->savtree[state]);
7006 nextsav = LIST_NEXT(sav, chain);
7008 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
7013 sah->state = SADB_SASTATE_DEAD;
7017 if (m->m_len < sizeof(struct sadb_msg) ||
7018 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
7019 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
7020 return key_senderror(so, m, ENOBUFS);
7026 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
7027 newmsg = mtod(m, struct sadb_msg *);
7028 newmsg->sadb_msg_errno = 0;
7029 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
7031 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7035 * SADB_DUMP processing
7036 * dump all entries including status of DEAD in SAD.
7039 * from the ikmpd, and dump all secasvar leaves
7044 * m will always be freed.
7047 key_dump(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
7049 struct secashead *sah;
7050 struct secasvar *sav;
7056 struct sadb_msg *newmsg;
7059 IPSEC_ASSERT(so != NULL, ("null socket"));
7060 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7061 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7062 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7064 /* map satype to proto */
7065 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7066 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7068 return key_senderror(so, m, EINVAL);
7071 /* count sav entries to be sent to the userland. */
7074 LIST_FOREACH(sah, &V_sahtree, chain) {
7075 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7076 && proto != sah->saidx.proto)
7080 stateidx < _ARRAYLEN(saorder_state_any);
7082 state = saorder_state_any[stateidx];
7083 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7091 return key_senderror(so, m, ENOENT);
7094 /* send this to the userland, one at a time. */
7096 LIST_FOREACH(sah, &V_sahtree, chain) {
7097 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7098 && proto != sah->saidx.proto)
7101 /* map proto to satype */
7102 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
7104 ipseclog((LOG_DEBUG, "%s: there was invalid proto in "
7105 "SAD.\n", __func__));
7106 return key_senderror(so, m, EINVAL);
7110 stateidx < _ARRAYLEN(saorder_state_any);
7112 state = saorder_state_any[stateidx];
7113 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7114 n = key_setdumpsa(sav, SADB_DUMP, satype,
7115 --cnt, mhp->msg->sadb_msg_pid);
7118 return key_senderror(so, m, ENOBUFS);
7120 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
7131 * SADB_X_PROMISC processing
7133 * m will always be freed.
7136 key_promisc(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp)
7140 IPSEC_ASSERT(so != NULL, ("null socket"));
7141 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7142 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7143 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7145 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7147 if (olen < sizeof(struct sadb_msg)) {
7149 return key_senderror(so, m, EINVAL);
7154 } else if (olen == sizeof(struct sadb_msg)) {
7155 /* enable/disable promisc mode */
7158 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
7159 return key_senderror(so, m, EINVAL);
7160 mhp->msg->sadb_msg_errno = 0;
7161 switch (mhp->msg->sadb_msg_satype) {
7164 kp->kp_promisc = mhp->msg->sadb_msg_satype;
7167 return key_senderror(so, m, EINVAL);
7170 /* send the original message back to everyone */
7171 mhp->msg->sadb_msg_errno = 0;
7172 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7174 /* send packet as is */
7176 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
7178 /* TODO: if sadb_msg_seq is specified, send to specific pid */
7179 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7183 static int (*key_typesw[])(struct socket *, struct mbuf *,
7184 const struct sadb_msghdr *) = {
7185 NULL, /* SADB_RESERVED */
7186 key_getspi, /* SADB_GETSPI */
7187 key_update, /* SADB_UPDATE */
7188 key_add, /* SADB_ADD */
7189 key_delete, /* SADB_DELETE */
7190 key_get, /* SADB_GET */
7191 key_acquire2, /* SADB_ACQUIRE */
7192 key_register, /* SADB_REGISTER */
7193 NULL, /* SADB_EXPIRE */
7194 key_flush, /* SADB_FLUSH */
7195 key_dump, /* SADB_DUMP */
7196 key_promisc, /* SADB_X_PROMISC */
7197 NULL, /* SADB_X_PCHANGE */
7198 key_spdadd, /* SADB_X_SPDUPDATE */
7199 key_spdadd, /* SADB_X_SPDADD */
7200 key_spddelete, /* SADB_X_SPDDELETE */
7201 key_spdget, /* SADB_X_SPDGET */
7202 NULL, /* SADB_X_SPDACQUIRE */
7203 key_spddump, /* SADB_X_SPDDUMP */
7204 key_spdflush, /* SADB_X_SPDFLUSH */
7205 key_spdadd, /* SADB_X_SPDSETIDX */
7206 NULL, /* SADB_X_SPDEXPIRE */
7207 key_spddelete2, /* SADB_X_SPDDELETE2 */
7211 * parse sadb_msg buffer to process PFKEYv2,
7212 * and create a data to response if needed.
7213 * I think to be dealed with mbuf directly.
7215 * msgp : pointer to pointer to a received buffer pulluped.
7216 * This is rewrited to response.
7217 * so : pointer to socket.
7219 * length for buffer to send to user process.
7222 key_parse(struct mbuf *m, struct socket *so)
7224 struct sadb_msg *msg;
7225 struct sadb_msghdr mh;
7230 IPSEC_ASSERT(so != NULL, ("null socket"));
7231 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7233 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
7234 KEYDEBUG(KEYDEBUG_KEY_DUMP,
7235 ipseclog((LOG_DEBUG, "%s: passed sadb_msg\n", __func__));
7239 if (m->m_len < sizeof(struct sadb_msg)) {
7240 m = m_pullup(m, sizeof(struct sadb_msg));
7244 msg = mtod(m, struct sadb_msg *);
7245 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
7246 target = KEY_SENDUP_ONE;
7248 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len != orglen) {
7249 ipseclog((LOG_DEBUG, "%s: invalid message length.\n",__func__));
7250 PFKEYSTAT_INC(out_invlen);
7255 if (msg->sadb_msg_version != PF_KEY_V2) {
7256 ipseclog((LOG_DEBUG, "%s: PF_KEY version %u is mismatched.\n",
7257 __func__, msg->sadb_msg_version));
7258 PFKEYSTAT_INC(out_invver);
7263 if (msg->sadb_msg_type > SADB_MAX) {
7264 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7265 __func__, msg->sadb_msg_type));
7266 PFKEYSTAT_INC(out_invmsgtype);
7271 /* for old-fashioned code - should be nuked */
7272 if (m->m_pkthdr.len > MCLBYTES) {
7279 MGETHDR(n, M_NOWAIT, MT_DATA);
7280 if (n && m->m_pkthdr.len > MHLEN) {
7281 MCLGET(n, M_NOWAIT);
7282 if ((n->m_flags & M_EXT) == 0) {
7291 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
7292 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
7298 /* align the mbuf chain so that extensions are in contiguous region. */
7299 error = key_align(m, &mh);
7306 switch (msg->sadb_msg_satype) {
7307 case SADB_SATYPE_UNSPEC:
7308 switch (msg->sadb_msg_type) {
7316 ipseclog((LOG_DEBUG, "%s: must specify satype "
7317 "when msg type=%u.\n", __func__,
7318 msg->sadb_msg_type));
7319 PFKEYSTAT_INC(out_invsatype);
7324 case SADB_SATYPE_AH:
7325 case SADB_SATYPE_ESP:
7326 case SADB_X_SATYPE_IPCOMP:
7327 case SADB_X_SATYPE_TCPSIGNATURE:
7328 switch (msg->sadb_msg_type) {
7330 case SADB_X_SPDDELETE:
7332 case SADB_X_SPDDUMP:
7333 case SADB_X_SPDFLUSH:
7334 case SADB_X_SPDSETIDX:
7335 case SADB_X_SPDUPDATE:
7336 case SADB_X_SPDDELETE2:
7337 ipseclog((LOG_DEBUG, "%s: illegal satype=%u\n",
7338 __func__, msg->sadb_msg_type));
7339 PFKEYSTAT_INC(out_invsatype);
7344 case SADB_SATYPE_RSVP:
7345 case SADB_SATYPE_OSPFV2:
7346 case SADB_SATYPE_RIPV2:
7347 case SADB_SATYPE_MIP:
7348 ipseclog((LOG_DEBUG, "%s: type %u isn't supported.\n",
7349 __func__, msg->sadb_msg_satype));
7350 PFKEYSTAT_INC(out_invsatype);
7353 case 1: /* XXX: What does it do? */
7354 if (msg->sadb_msg_type == SADB_X_PROMISC)
7358 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7359 __func__, msg->sadb_msg_satype));
7360 PFKEYSTAT_INC(out_invsatype);
7365 /* check field of upper layer protocol and address family */
7366 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
7367 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
7368 struct sadb_address *src0, *dst0;
7371 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
7372 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
7374 /* check upper layer protocol */
7375 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
7376 ipseclog((LOG_DEBUG, "%s: upper layer protocol "
7377 "mismatched.\n", __func__));
7378 PFKEYSTAT_INC(out_invaddr);
7384 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
7385 PFKEY_ADDR_SADDR(dst0)->sa_family) {
7386 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
7388 PFKEYSTAT_INC(out_invaddr);
7392 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7393 PFKEY_ADDR_SADDR(dst0)->sa_len) {
7394 ipseclog((LOG_DEBUG, "%s: address struct size "
7395 "mismatched.\n", __func__));
7396 PFKEYSTAT_INC(out_invaddr);
7401 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7403 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7404 sizeof(struct sockaddr_in)) {
7405 PFKEYSTAT_INC(out_invaddr);
7411 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7412 sizeof(struct sockaddr_in6)) {
7413 PFKEYSTAT_INC(out_invaddr);
7419 ipseclog((LOG_DEBUG, "%s: unsupported address family\n",
7421 PFKEYSTAT_INC(out_invaddr);
7422 error = EAFNOSUPPORT;
7426 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7428 plen = sizeof(struct in_addr) << 3;
7431 plen = sizeof(struct in6_addr) << 3;
7434 plen = 0; /*fool gcc*/
7438 /* check max prefix length */
7439 if (src0->sadb_address_prefixlen > plen ||
7440 dst0->sadb_address_prefixlen > plen) {
7441 ipseclog((LOG_DEBUG, "%s: illegal prefixlen.\n",
7443 PFKEYSTAT_INC(out_invaddr);
7449 * prefixlen == 0 is valid because there can be a case when
7450 * all addresses are matched.
7454 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
7455 key_typesw[msg->sadb_msg_type] == NULL) {
7456 PFKEYSTAT_INC(out_invmsgtype);
7461 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
7464 msg->sadb_msg_errno = error;
7465 return key_sendup_mbuf(so, m, target);
7469 key_senderror(struct socket *so, struct mbuf *m, int code)
7471 struct sadb_msg *msg;
7473 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7474 ("mbuf too small, len %u", m->m_len));
7476 msg = mtod(m, struct sadb_msg *);
7477 msg->sadb_msg_errno = code;
7478 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
7482 * set the pointer to each header into message buffer.
7483 * m will be freed on error.
7484 * XXX larger-than-MCLBYTES extension?
7487 key_align(struct mbuf *m, struct sadb_msghdr *mhp)
7490 struct sadb_ext *ext;
7495 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7496 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7497 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7498 ("mbuf too small, len %u", m->m_len));
7501 bzero(mhp, sizeof(*mhp));
7503 mhp->msg = mtod(m, struct sadb_msg *);
7504 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
7506 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7507 extlen = end; /*just in case extlen is not updated*/
7508 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
7509 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
7511 /* m is already freed */
7514 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7517 switch (ext->sadb_ext_type) {
7519 case SADB_EXT_ADDRESS_SRC:
7520 case SADB_EXT_ADDRESS_DST:
7521 case SADB_EXT_ADDRESS_PROXY:
7522 case SADB_EXT_LIFETIME_CURRENT:
7523 case SADB_EXT_LIFETIME_HARD:
7524 case SADB_EXT_LIFETIME_SOFT:
7525 case SADB_EXT_KEY_AUTH:
7526 case SADB_EXT_KEY_ENCRYPT:
7527 case SADB_EXT_IDENTITY_SRC:
7528 case SADB_EXT_IDENTITY_DST:
7529 case SADB_EXT_SENSITIVITY:
7530 case SADB_EXT_PROPOSAL:
7531 case SADB_EXT_SUPPORTED_AUTH:
7532 case SADB_EXT_SUPPORTED_ENCRYPT:
7533 case SADB_EXT_SPIRANGE:
7534 case SADB_X_EXT_POLICY:
7535 case SADB_X_EXT_SA2:
7537 case SADB_X_EXT_NAT_T_TYPE:
7538 case SADB_X_EXT_NAT_T_SPORT:
7539 case SADB_X_EXT_NAT_T_DPORT:
7540 case SADB_X_EXT_NAT_T_OAI:
7541 case SADB_X_EXT_NAT_T_OAR:
7542 case SADB_X_EXT_NAT_T_FRAG:
7544 /* duplicate check */
7546 * XXX Are there duplication payloads of either
7547 * KEY_AUTH or KEY_ENCRYPT ?
7549 if (mhp->ext[ext->sadb_ext_type] != NULL) {
7550 ipseclog((LOG_DEBUG, "%s: duplicate ext_type "
7551 "%u\n", __func__, ext->sadb_ext_type));
7553 PFKEYSTAT_INC(out_dupext);
7558 ipseclog((LOG_DEBUG, "%s: invalid ext_type %u\n",
7559 __func__, ext->sadb_ext_type));
7561 PFKEYSTAT_INC(out_invexttype);
7565 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
7567 if (key_validate_ext(ext, extlen)) {
7569 PFKEYSTAT_INC(out_invlen);
7573 n = m_pulldown(m, off, extlen, &toff);
7575 /* m is already freed */
7578 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7580 mhp->ext[ext->sadb_ext_type] = ext;
7581 mhp->extoff[ext->sadb_ext_type] = off;
7582 mhp->extlen[ext->sadb_ext_type] = extlen;
7587 PFKEYSTAT_INC(out_invlen);
7595 key_validate_ext(const struct sadb_ext *ext, int len)
7597 const struct sockaddr *sa;
7598 enum { NONE, ADDR } checktype = NONE;
7600 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
7602 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
7605 /* if it does not match minimum/maximum length, bail */
7606 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
7607 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
7609 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
7611 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
7614 /* more checks based on sadb_ext_type XXX need more */
7615 switch (ext->sadb_ext_type) {
7616 case SADB_EXT_ADDRESS_SRC:
7617 case SADB_EXT_ADDRESS_DST:
7618 case SADB_EXT_ADDRESS_PROXY:
7619 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7622 case SADB_EXT_IDENTITY_SRC:
7623 case SADB_EXT_IDENTITY_DST:
7624 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7625 SADB_X_IDENTTYPE_ADDR) {
7626 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7636 switch (checktype) {
7640 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
7641 if (len < baselen + sal)
7643 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7656 for (i = 0; i < IPSEC_DIR_MAX; i++)
7657 LIST_INIT(&V_sptree[i]);
7659 LIST_INIT(&V_sahtree);
7661 for (i = 0; i <= SADB_SATYPE_MAX; i++)
7662 LIST_INIT(&V_regtree[i]);
7664 LIST_INIT(&V_acqtree);
7665 LIST_INIT(&V_spacqtree);
7667 /* system default */
7668 V_ip4_def_policy.policy = IPSEC_POLICY_NONE;
7669 V_ip4_def_policy.refcnt++; /*never reclaim this*/
7671 if (!IS_DEFAULT_VNET(curvnet))
7675 REGTREE_LOCK_INIT();
7676 SAHTREE_LOCK_INIT();
7680 #ifndef IPSEC_DEBUG2
7681 timeout((void *)key_timehandler, (void *)0, hz);
7682 #endif /*IPSEC_DEBUG2*/
7684 /* initialize key statistics */
7685 keystat.getspi_count = 1;
7687 printf("IPsec: Initialized Security Association Processing.\n");
7694 struct secpolicy *sp, *nextsp;
7695 struct secacq *acq, *nextacq;
7696 struct secspacq *spacq, *nextspacq;
7697 struct secashead *sah, *nextsah;
7702 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7703 for (sp = LIST_FIRST(&V_sptree[i]);
7704 sp != NULL; sp = nextsp) {
7705 nextsp = LIST_NEXT(sp, chain);
7706 if (__LIST_CHAINED(sp)) {
7707 LIST_REMOVE(sp, chain);
7708 free(sp, M_IPSEC_SP);
7715 for (sah = LIST_FIRST(&V_sahtree); sah != NULL; sah = nextsah) {
7716 nextsah = LIST_NEXT(sah, chain);
7717 if (__LIST_CHAINED(sah)) {
7718 LIST_REMOVE(sah, chain);
7719 free(sah, M_IPSEC_SAH);
7725 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7726 LIST_FOREACH(reg, &V_regtree[i], chain) {
7727 if (__LIST_CHAINED(reg)) {
7728 LIST_REMOVE(reg, chain);
7729 free(reg, M_IPSEC_SAR);
7737 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
7738 nextacq = LIST_NEXT(acq, chain);
7739 if (__LIST_CHAINED(acq)) {
7740 LIST_REMOVE(acq, chain);
7741 free(acq, M_IPSEC_SAQ);
7747 for (spacq = LIST_FIRST(&V_spacqtree); spacq != NULL;
7748 spacq = nextspacq) {
7749 nextspacq = LIST_NEXT(spacq, chain);
7750 if (__LIST_CHAINED(spacq)) {
7751 LIST_REMOVE(spacq, chain);
7752 free(spacq, M_IPSEC_SAQ);
7760 * XXX: maybe This function is called after INBOUND IPsec processing.
7762 * Special check for tunnel-mode packets.
7763 * We must make some checks for consistency between inner and outer IP header.
7765 * xxx more checks to be provided
7768 key_checktunnelsanity(struct secasvar *sav, u_int family, caddr_t src,
7771 IPSEC_ASSERT(sav->sah != NULL, ("null SA header"));
7773 /* XXX: check inner IP header */
7778 /* record data transfer on SA, and update timestamps */
7780 key_sa_recordxfer(struct secasvar *sav, struct mbuf *m)
7782 IPSEC_ASSERT(sav != NULL, ("Null secasvar"));
7783 IPSEC_ASSERT(m != NULL, ("Null mbuf"));
7788 * XXX Currently, there is a difference of bytes size
7789 * between inbound and outbound processing.
7791 sav->lft_c->bytes += m->m_pkthdr.len;
7792 /* to check bytes lifetime is done in key_timehandler(). */
7795 * We use the number of packets as the unit of
7796 * allocations. We increment the variable
7797 * whenever {esp,ah}_{in,out}put is called.
7799 sav->lft_c->allocations++;
7800 /* XXX check for expires? */
7803 * NOTE: We record CURRENT usetime by using wall clock,
7804 * in seconds. HARD and SOFT lifetime are measured by the time
7805 * difference (again in seconds) from usetime.
7809 * -----+-----+--------+---> t
7810 * <--------------> HARD
7813 sav->lft_c->usetime = time_second;
7814 /* XXX check for expires? */
7820 key_sa_chgstate(struct secasvar *sav, u_int8_t state)
7822 IPSEC_ASSERT(sav != NULL, ("NULL sav"));
7823 SAHTREE_LOCK_ASSERT();
7825 if (sav->state != state) {
7826 if (__LIST_CHAINED(sav))
7827 LIST_REMOVE(sav, chain);
7829 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7834 key_sa_stir_iv(struct secasvar *sav)
7837 IPSEC_ASSERT(sav->iv != NULL, ("null IV"));
7838 key_randomfill(sav->iv, sav->ivlen);
7842 * Take one of the kernel's security keys and convert it into a PF_KEY
7843 * structure within an mbuf, suitable for sending up to a waiting
7844 * application in user land.
7847 * src: A pointer to a kernel security key.
7848 * exttype: Which type of key this is. Refer to the PF_KEY data structures.
7850 * a valid mbuf or NULL indicating an error
7854 static struct mbuf *
7855 key_setkey(struct seckey *src, u_int16_t exttype)
7864 len = PFKEY_ALIGN8(sizeof(struct sadb_key) + _KEYLEN(src));
7865 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
7870 p = mtod(m, struct sadb_key *);
7872 p->sadb_key_len = PFKEY_UNIT64(len);
7873 p->sadb_key_exttype = exttype;
7874 p->sadb_key_bits = src->bits;
7875 bcopy(src->key_data, _KEYBUF(p), _KEYLEN(src));
7881 * Take one of the kernel's lifetime data structures and convert it
7882 * into a PF_KEY structure within an mbuf, suitable for sending up to
7883 * a waiting application in user land.
7886 * src: A pointer to a kernel lifetime structure.
7887 * exttype: Which type of lifetime this is. Refer to the PF_KEY
7888 * data structures for more information.
7890 * a valid mbuf or NULL indicating an error
7894 static struct mbuf *
7895 key_setlifetime(struct seclifetime *src, u_int16_t exttype)
7897 struct mbuf *m = NULL;
7898 struct sadb_lifetime *p;
7899 int len = PFKEY_ALIGN8(sizeof(struct sadb_lifetime));
7904 m = m_get2(len, M_NOWAIT, MT_DATA, 0);
7909 p = mtod(m, struct sadb_lifetime *);
7912 p->sadb_lifetime_len = PFKEY_UNIT64(len);
7913 p->sadb_lifetime_exttype = exttype;
7914 p->sadb_lifetime_allocations = src->allocations;
7915 p->sadb_lifetime_bytes = src->bytes;
7916 p->sadb_lifetime_addtime = src->addtime;
7917 p->sadb_lifetime_usetime = src->usetime;