2 /* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * This code is referd to RFC 2367
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/mutex.h>
48 #include <sys/domain.h>
49 #include <sys/protosw.h>
50 #include <sys/malloc.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/errno.h>
56 #include <sys/queue.h>
57 #include <sys/refcount.h>
58 #include <sys/syslog.h>
61 #include <net/route.h>
62 #include <net/raw_cb.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/ip.h>
68 #include <netinet/in_var.h>
71 #include <netinet/ip6.h>
72 #include <netinet6/in6_var.h>
73 #include <netinet6/ip6_var.h>
77 #include <netinet/in_pcb.h>
80 #include <netinet6/in6_pcb.h>
83 #include <net/pfkeyv2.h>
84 #include <netipsec/keydb.h>
85 #include <netipsec/key.h>
86 #include <netipsec/keysock.h>
87 #include <netipsec/key_debug.h>
89 #include <netipsec/ipsec.h>
91 #include <netipsec/ipsec6.h>
94 #include <netipsec/xform.h>
96 #include <machine/stdarg.h>
99 #include <sys/random.h>
101 #define FULLMASK 0xff
102 #define _BITS(bytes) ((bytes) << 3)
105 * Note on SA reference counting:
106 * - SAs that are not in DEAD state will have (total external reference + 1)
107 * following value in reference count field. they cannot be freed and are
108 * referenced from SA header.
109 * - SAs that are in DEAD state will have (total external reference)
110 * in reference count field. they are ready to be freed. reference from
111 * SA header will be removed in key_delsav(), when the reference count
112 * field hits 0 (= no external reference other than from SA header.
115 VNET_DEFINE(u_int32_t, key_debug_level) = 0;
116 static VNET_DEFINE(u_int, key_spi_trycnt) = 1000;
117 static VNET_DEFINE(u_int32_t, key_spi_minval) = 0x100;
118 static VNET_DEFINE(u_int32_t, key_spi_maxval) = 0x0fffffff; /* XXX */
119 static VNET_DEFINE(u_int32_t, policy_id) = 0;
120 /*interval to initialize randseed,1(m)*/
121 static VNET_DEFINE(u_int, key_int_random) = 60;
122 /* interval to expire acquiring, 30(s)*/
123 static VNET_DEFINE(u_int, key_larval_lifetime) = 30;
124 /* counter for blocking SADB_ACQUIRE.*/
125 static VNET_DEFINE(int, key_blockacq_count) = 10;
126 /* lifetime for blocking SADB_ACQUIRE.*/
127 static VNET_DEFINE(int, key_blockacq_lifetime) = 20;
128 /* preferred old sa rather than new sa.*/
129 static VNET_DEFINE(int, key_preferred_oldsa) = 1;
130 #define V_key_spi_trycnt VNET(key_spi_trycnt)
131 #define V_key_spi_minval VNET(key_spi_minval)
132 #define V_key_spi_maxval VNET(key_spi_maxval)
133 #define V_policy_id VNET(policy_id)
134 #define V_key_int_random VNET(key_int_random)
135 #define V_key_larval_lifetime VNET(key_larval_lifetime)
136 #define V_key_blockacq_count VNET(key_blockacq_count)
137 #define V_key_blockacq_lifetime VNET(key_blockacq_lifetime)
138 #define V_key_preferred_oldsa VNET(key_preferred_oldsa)
140 static VNET_DEFINE(u_int32_t, acq_seq) = 0;
141 #define V_acq_seq VNET(acq_seq)
144 static VNET_DEFINE(LIST_HEAD(_sptree, secpolicy), sptree[IPSEC_DIR_MAX]);
145 #define V_sptree VNET(sptree)
146 static struct mtx sptree_lock;
147 #define SPTREE_LOCK_INIT() \
148 mtx_init(&sptree_lock, "sptree", \
149 "fast ipsec security policy database", MTX_DEF)
150 #define SPTREE_LOCK_DESTROY() mtx_destroy(&sptree_lock)
151 #define SPTREE_LOCK() mtx_lock(&sptree_lock)
152 #define SPTREE_UNLOCK() mtx_unlock(&sptree_lock)
153 #define SPTREE_LOCK_ASSERT() mtx_assert(&sptree_lock, MA_OWNED)
155 static VNET_DEFINE(LIST_HEAD(_sahtree, secashead), sahtree); /* SAD */
156 #define V_sahtree VNET(sahtree)
157 static struct mtx sahtree_lock;
158 #define SAHTREE_LOCK_INIT() \
159 mtx_init(&sahtree_lock, "sahtree", \
160 "fast ipsec security association database", MTX_DEF)
161 #define SAHTREE_LOCK_DESTROY() mtx_destroy(&sahtree_lock)
162 #define SAHTREE_LOCK() mtx_lock(&sahtree_lock)
163 #define SAHTREE_UNLOCK() mtx_unlock(&sahtree_lock)
164 #define SAHTREE_LOCK_ASSERT() mtx_assert(&sahtree_lock, MA_OWNED)
167 static VNET_DEFINE(LIST_HEAD(_regtree, secreg), regtree[SADB_SATYPE_MAX + 1]);
168 #define V_regtree VNET(regtree)
169 static struct mtx regtree_lock;
170 #define REGTREE_LOCK_INIT() \
171 mtx_init(®tree_lock, "regtree", "fast ipsec regtree", MTX_DEF)
172 #define REGTREE_LOCK_DESTROY() mtx_destroy(®tree_lock)
173 #define REGTREE_LOCK() mtx_lock(®tree_lock)
174 #define REGTREE_UNLOCK() mtx_unlock(®tree_lock)
175 #define REGTREE_LOCK_ASSERT() mtx_assert(®tree_lock, MA_OWNED)
177 static VNET_DEFINE(LIST_HEAD(_acqtree, secacq), acqtree); /* acquiring list */
178 #define V_acqtree VNET(acqtree)
179 static struct mtx acq_lock;
180 #define ACQ_LOCK_INIT() \
181 mtx_init(&acq_lock, "acqtree", "fast ipsec acquire list", MTX_DEF)
182 #define ACQ_LOCK_DESTROY() mtx_destroy(&acq_lock)
183 #define ACQ_LOCK() mtx_lock(&acq_lock)
184 #define ACQ_UNLOCK() mtx_unlock(&acq_lock)
185 #define ACQ_LOCK_ASSERT() mtx_assert(&acq_lock, MA_OWNED)
187 /* SP acquiring list */
188 static VNET_DEFINE(LIST_HEAD(_spacqtree, secspacq), spacqtree);
189 #define V_spacqtree VNET(spacqtree)
190 static struct mtx spacq_lock;
191 #define SPACQ_LOCK_INIT() \
192 mtx_init(&spacq_lock, "spacqtree", \
193 "fast ipsec security policy acquire list", MTX_DEF)
194 #define SPACQ_LOCK_DESTROY() mtx_destroy(&spacq_lock)
195 #define SPACQ_LOCK() mtx_lock(&spacq_lock)
196 #define SPACQ_UNLOCK() mtx_unlock(&spacq_lock)
197 #define SPACQ_LOCK_ASSERT() mtx_assert(&spacq_lock, MA_OWNED)
199 /* search order for SAs */
200 static const u_int saorder_state_valid_prefer_old[] = {
201 SADB_SASTATE_DYING, SADB_SASTATE_MATURE,
203 static const u_int saorder_state_valid_prefer_new[] = {
204 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
206 static const u_int saorder_state_alive[] = {
208 SADB_SASTATE_MATURE, SADB_SASTATE_DYING, SADB_SASTATE_LARVAL
210 static const u_int saorder_state_any[] = {
211 SADB_SASTATE_MATURE, SADB_SASTATE_DYING,
212 SADB_SASTATE_LARVAL, SADB_SASTATE_DEAD
215 static const int minsize[] = {
216 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
217 sizeof(struct sadb_sa), /* SADB_EXT_SA */
218 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
219 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
220 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
221 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_SRC */
222 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_DST */
223 sizeof(struct sadb_address), /* SADB_EXT_ADDRESS_PROXY */
224 sizeof(struct sadb_key), /* SADB_EXT_KEY_AUTH */
225 sizeof(struct sadb_key), /* SADB_EXT_KEY_ENCRYPT */
226 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_SRC */
227 sizeof(struct sadb_ident), /* SADB_EXT_IDENTITY_DST */
228 sizeof(struct sadb_sens), /* SADB_EXT_SENSITIVITY */
229 sizeof(struct sadb_prop), /* SADB_EXT_PROPOSAL */
230 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_AUTH */
231 sizeof(struct sadb_supported), /* SADB_EXT_SUPPORTED_ENCRYPT */
232 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
233 0, /* SADB_X_EXT_KMPRIVATE */
234 sizeof(struct sadb_x_policy), /* SADB_X_EXT_POLICY */
235 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
236 sizeof(struct sadb_x_nat_t_type),/* SADB_X_EXT_NAT_T_TYPE */
237 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_SPORT */
238 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_DPORT */
239 sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAI */
240 sizeof(struct sadb_address), /* SADB_X_EXT_NAT_T_OAR */
241 sizeof(struct sadb_x_nat_t_frag),/* SADB_X_EXT_NAT_T_FRAG */
243 static const int maxsize[] = {
244 sizeof(struct sadb_msg), /* SADB_EXT_RESERVED */
245 sizeof(struct sadb_sa), /* SADB_EXT_SA */
246 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_CURRENT */
247 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_HARD */
248 sizeof(struct sadb_lifetime), /* SADB_EXT_LIFETIME_SOFT */
249 0, /* SADB_EXT_ADDRESS_SRC */
250 0, /* SADB_EXT_ADDRESS_DST */
251 0, /* SADB_EXT_ADDRESS_PROXY */
252 0, /* SADB_EXT_KEY_AUTH */
253 0, /* SADB_EXT_KEY_ENCRYPT */
254 0, /* SADB_EXT_IDENTITY_SRC */
255 0, /* SADB_EXT_IDENTITY_DST */
256 0, /* SADB_EXT_SENSITIVITY */
257 0, /* SADB_EXT_PROPOSAL */
258 0, /* SADB_EXT_SUPPORTED_AUTH */
259 0, /* SADB_EXT_SUPPORTED_ENCRYPT */
260 sizeof(struct sadb_spirange), /* SADB_EXT_SPIRANGE */
261 0, /* SADB_X_EXT_KMPRIVATE */
262 0, /* SADB_X_EXT_POLICY */
263 sizeof(struct sadb_x_sa2), /* SADB_X_SA2 */
264 sizeof(struct sadb_x_nat_t_type),/* SADB_X_EXT_NAT_T_TYPE */
265 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_SPORT */
266 sizeof(struct sadb_x_nat_t_port),/* SADB_X_EXT_NAT_T_DPORT */
267 0, /* SADB_X_EXT_NAT_T_OAI */
268 0, /* SADB_X_EXT_NAT_T_OAR */
269 sizeof(struct sadb_x_nat_t_frag),/* SADB_X_EXT_NAT_T_FRAG */
272 static VNET_DEFINE(int, ipsec_esp_keymin) = 256;
273 static VNET_DEFINE(int, ipsec_esp_auth) = 0;
274 static VNET_DEFINE(int, ipsec_ah_keymin) = 128;
276 #define V_ipsec_esp_keymin VNET(ipsec_esp_keymin)
277 #define V_ipsec_esp_auth VNET(ipsec_esp_auth)
278 #define V_ipsec_ah_keymin VNET(ipsec_ah_keymin)
281 SYSCTL_DECL(_net_key);
284 SYSCTL_VNET_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug,
285 CTLFLAG_RW, &VNET_NAME(key_debug_level), 0, "");
287 /* max count of trial for the decision of spi value */
288 SYSCTL_VNET_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt,
289 CTLFLAG_RW, &VNET_NAME(key_spi_trycnt), 0, "");
291 /* minimum spi value to allocate automatically. */
292 SYSCTL_VNET_INT(_net_key, KEYCTL_SPI_MIN_VALUE,
293 spi_minval, CTLFLAG_RW, &VNET_NAME(key_spi_minval), 0, "");
295 /* maximun spi value to allocate automatically. */
296 SYSCTL_VNET_INT(_net_key, KEYCTL_SPI_MAX_VALUE,
297 spi_maxval, CTLFLAG_RW, &VNET_NAME(key_spi_maxval), 0, "");
299 /* interval to initialize randseed */
300 SYSCTL_VNET_INT(_net_key, KEYCTL_RANDOM_INT,
301 int_random, CTLFLAG_RW, &VNET_NAME(key_int_random), 0, "");
303 /* lifetime for larval SA */
304 SYSCTL_VNET_INT(_net_key, KEYCTL_LARVAL_LIFETIME,
305 larval_lifetime, CTLFLAG_RW, &VNET_NAME(key_larval_lifetime), 0, "");
307 /* counter for blocking to send SADB_ACQUIRE to IKEd */
308 SYSCTL_VNET_INT(_net_key, KEYCTL_BLOCKACQ_COUNT,
309 blockacq_count, CTLFLAG_RW, &VNET_NAME(key_blockacq_count), 0, "");
311 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
312 SYSCTL_VNET_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME,
313 blockacq_lifetime, CTLFLAG_RW, &VNET_NAME(key_blockacq_lifetime), 0, "");
316 SYSCTL_VNET_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth,
317 CTLFLAG_RW, &VNET_NAME(ipsec_esp_auth), 0, "");
319 /* minimum ESP key length */
320 SYSCTL_VNET_INT(_net_key, KEYCTL_ESP_KEYMIN,
321 esp_keymin, CTLFLAG_RW, &VNET_NAME(ipsec_esp_keymin), 0, "");
323 /* minimum AH key length */
324 SYSCTL_VNET_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin,
325 CTLFLAG_RW, &VNET_NAME(ipsec_ah_keymin), 0, "");
327 /* perfered old SA rather than new SA */
328 SYSCTL_VNET_INT(_net_key, KEYCTL_PREFERED_OLDSA,
329 preferred_oldsa, CTLFLAG_RW, &VNET_NAME(key_preferred_oldsa), 0, "");
331 #define __LIST_CHAINED(elm) \
332 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
333 #define LIST_INSERT_TAIL(head, elm, type, field) \
335 struct type *curelm = LIST_FIRST(head); \
336 if (curelm == NULL) {\
337 LIST_INSERT_HEAD(head, elm, field); \
339 while (LIST_NEXT(curelm, field)) \
340 curelm = LIST_NEXT(curelm, field);\
341 LIST_INSERT_AFTER(curelm, elm, field);\
345 #define KEY_CHKSASTATE(head, sav, name) \
347 if ((head) != (sav)) { \
348 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
349 (name), (head), (sav))); \
354 #define KEY_CHKSPDIR(head, sp, name) \
356 if ((head) != (sp)) { \
357 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
358 "anyway continue.\n", \
359 (name), (head), (sp))); \
363 MALLOC_DEFINE(M_IPSEC_SA, "secasvar", "ipsec security association");
364 MALLOC_DEFINE(M_IPSEC_SAH, "sahead", "ipsec sa head");
365 MALLOC_DEFINE(M_IPSEC_SP, "ipsecpolicy", "ipsec security policy");
366 MALLOC_DEFINE(M_IPSEC_SR, "ipsecrequest", "ipsec security request");
367 MALLOC_DEFINE(M_IPSEC_MISC, "ipsec-misc", "ipsec miscellaneous");
368 MALLOC_DEFINE(M_IPSEC_SAQ, "ipsec-saq", "ipsec sa acquire");
369 MALLOC_DEFINE(M_IPSEC_SAR, "ipsec-reg", "ipsec sa acquire");
372 * set parameters into secpolicyindex buffer.
373 * Must allocate secpolicyindex buffer passed to this function.
375 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
377 bzero((idx), sizeof(struct secpolicyindex)); \
378 (idx)->dir = (_dir); \
379 (idx)->prefs = (ps); \
380 (idx)->prefd = (pd); \
381 (idx)->ul_proto = (ulp); \
382 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
383 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
387 * set parameters into secasindex buffer.
388 * Must allocate secasindex buffer before calling this function.
390 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
392 bzero((idx), sizeof(struct secasindex)); \
393 (idx)->proto = (p); \
395 (idx)->reqid = (r); \
396 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
397 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
402 u_long getspi_count; /* the avarage of count to try to get new SPI */
406 struct sadb_msg *msg;
407 struct sadb_ext *ext[SADB_EXT_MAX + 1];
408 int extoff[SADB_EXT_MAX + 1];
409 int extlen[SADB_EXT_MAX + 1];
412 static struct secasvar *key_allocsa_policy __P((const struct secasindex *));
413 static void key_freesp_so __P((struct secpolicy **));
414 static struct secasvar *key_do_allocsa_policy __P((struct secashead *, u_int));
415 static void key_delsp __P((struct secpolicy *));
416 static struct secpolicy *key_getsp __P((struct secpolicyindex *));
417 static void _key_delsp(struct secpolicy *sp);
418 static struct secpolicy *key_getspbyid __P((u_int32_t));
419 static u_int32_t key_newreqid __P((void));
420 static struct mbuf *key_gather_mbuf __P((struct mbuf *,
421 const struct sadb_msghdr *, int, int, ...));
422 static int key_spdadd __P((struct socket *, struct mbuf *,
423 const struct sadb_msghdr *));
424 static u_int32_t key_getnewspid __P((void));
425 static int key_spddelete __P((struct socket *, struct mbuf *,
426 const struct sadb_msghdr *));
427 static int key_spddelete2 __P((struct socket *, struct mbuf *,
428 const struct sadb_msghdr *));
429 static int key_spdget __P((struct socket *, struct mbuf *,
430 const struct sadb_msghdr *));
431 static int key_spdflush __P((struct socket *, struct mbuf *,
432 const struct sadb_msghdr *));
433 static int key_spddump __P((struct socket *, struct mbuf *,
434 const struct sadb_msghdr *));
435 static struct mbuf *key_setdumpsp __P((struct secpolicy *,
436 u_int8_t, u_int32_t, u_int32_t));
437 static u_int key_getspreqmsglen __P((struct secpolicy *));
438 static int key_spdexpire __P((struct secpolicy *));
439 static struct secashead *key_newsah __P((struct secasindex *));
440 static void key_delsah __P((struct secashead *));
441 static struct secasvar *key_newsav __P((struct mbuf *,
442 const struct sadb_msghdr *, struct secashead *, int *,
444 #define KEY_NEWSAV(m, sadb, sah, e) \
445 key_newsav(m, sadb, sah, e, __FILE__, __LINE__)
446 static void key_delsav __P((struct secasvar *));
447 static struct secashead *key_getsah __P((struct secasindex *));
448 static struct secasvar *key_checkspidup __P((struct secasindex *, u_int32_t));
449 static struct secasvar *key_getsavbyspi __P((struct secashead *, u_int32_t));
450 static int key_setsaval __P((struct secasvar *, struct mbuf *,
451 const struct sadb_msghdr *));
452 static int key_mature __P((struct secasvar *));
453 static struct mbuf *key_setdumpsa __P((struct secasvar *, u_int8_t,
454 u_int8_t, u_int32_t, u_int32_t));
455 static struct mbuf *key_setsadbmsg __P((u_int8_t, u_int16_t, u_int8_t,
456 u_int32_t, pid_t, u_int16_t));
457 static struct mbuf *key_setsadbsa __P((struct secasvar *));
458 static struct mbuf *key_setsadbaddr __P((u_int16_t,
459 const struct sockaddr *, u_int8_t, u_int16_t));
461 static struct mbuf *key_setsadbxport(u_int16_t, u_int16_t);
462 static struct mbuf *key_setsadbxtype(u_int16_t);
464 static void key_porttosaddr(struct sockaddr *, u_int16_t);
465 #define KEY_PORTTOSADDR(saddr, port) \
466 key_porttosaddr((struct sockaddr *)(saddr), (port))
467 static struct mbuf *key_setsadbxsa2 __P((u_int8_t, u_int32_t, u_int32_t));
468 static struct mbuf *key_setsadbxpolicy __P((u_int16_t, u_int8_t,
470 static struct seckey *key_dup_keymsg(const struct sadb_key *, u_int,
471 struct malloc_type *);
472 static struct seclifetime *key_dup_lifemsg(const struct sadb_lifetime *src,
473 struct malloc_type *type);
475 static int key_ismyaddr6 __P((struct sockaddr_in6 *));
478 /* flags for key_cmpsaidx() */
479 #define CMP_HEAD 1 /* protocol, addresses. */
480 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
481 #define CMP_REQID 3 /* additionally HEAD, reaid. */
482 #define CMP_EXACTLY 4 /* all elements. */
483 static int key_cmpsaidx
484 __P((const struct secasindex *, const struct secasindex *, int));
486 static int key_cmpspidx_exactly
487 __P((struct secpolicyindex *, struct secpolicyindex *));
488 static int key_cmpspidx_withmask
489 __P((struct secpolicyindex *, struct secpolicyindex *));
490 static int key_sockaddrcmp __P((const struct sockaddr *, const struct sockaddr *, int));
491 static int key_bbcmp __P((const void *, const void *, u_int));
492 static u_int16_t key_satype2proto __P((u_int8_t));
493 static u_int8_t key_proto2satype __P((u_int16_t));
495 static int key_getspi __P((struct socket *, struct mbuf *,
496 const struct sadb_msghdr *));
497 static u_int32_t key_do_getnewspi __P((struct sadb_spirange *,
498 struct secasindex *));
499 static int key_update __P((struct socket *, struct mbuf *,
500 const struct sadb_msghdr *));
501 #ifdef IPSEC_DOSEQCHECK
502 static struct secasvar *key_getsavbyseq __P((struct secashead *, u_int32_t));
504 static int key_add __P((struct socket *, struct mbuf *,
505 const struct sadb_msghdr *));
506 static int key_setident __P((struct secashead *, struct mbuf *,
507 const struct sadb_msghdr *));
508 static struct mbuf *key_getmsgbuf_x1 __P((struct mbuf *,
509 const struct sadb_msghdr *));
510 static int key_delete __P((struct socket *, struct mbuf *,
511 const struct sadb_msghdr *));
512 static int key_get __P((struct socket *, struct mbuf *,
513 const struct sadb_msghdr *));
515 static void key_getcomb_setlifetime __P((struct sadb_comb *));
516 static struct mbuf *key_getcomb_esp __P((void));
517 static struct mbuf *key_getcomb_ah __P((void));
518 static struct mbuf *key_getcomb_ipcomp __P((void));
519 static struct mbuf *key_getprop __P((const struct secasindex *));
521 static int key_acquire __P((const struct secasindex *, struct secpolicy *));
522 static struct secacq *key_newacq __P((const struct secasindex *));
523 static struct secacq *key_getacq __P((const struct secasindex *));
524 static struct secacq *key_getacqbyseq __P((u_int32_t));
525 static struct secspacq *key_newspacq __P((struct secpolicyindex *));
526 static struct secspacq *key_getspacq __P((struct secpolicyindex *));
527 static int key_acquire2 __P((struct socket *, struct mbuf *,
528 const struct sadb_msghdr *));
529 static int key_register __P((struct socket *, struct mbuf *,
530 const struct sadb_msghdr *));
531 static int key_expire __P((struct secasvar *));
532 static int key_flush __P((struct socket *, struct mbuf *,
533 const struct sadb_msghdr *));
534 static int key_dump __P((struct socket *, struct mbuf *,
535 const struct sadb_msghdr *));
536 static int key_promisc __P((struct socket *, struct mbuf *,
537 const struct sadb_msghdr *));
538 static int key_senderror __P((struct socket *, struct mbuf *, int));
539 static int key_validate_ext __P((const struct sadb_ext *, int));
540 static int key_align __P((struct mbuf *, struct sadb_msghdr *));
541 static struct mbuf *key_setlifetime(struct seclifetime *src,
543 static struct mbuf *key_setkey(struct seckey *src, u_int16_t exttype);
546 static const char *key_getfqdn __P((void));
547 static const char *key_getuserfqdn __P((void));
549 static void key_sa_chgstate __P((struct secasvar *, u_int8_t));
550 static struct mbuf *key_alloc_mbuf __P((int));
553 sa_initref(struct secasvar *sav)
556 refcount_init(&sav->refcnt, 1);
559 sa_addref(struct secasvar *sav)
562 refcount_acquire(&sav->refcnt);
563 IPSEC_ASSERT(sav->refcnt != 0, ("SA refcnt overflow"));
566 sa_delref(struct secasvar *sav)
569 IPSEC_ASSERT(sav->refcnt > 0, ("SA refcnt underflow"));
570 return (refcount_release(&sav->refcnt));
573 #define SP_ADDREF(p) do { \
575 IPSEC_ASSERT((p)->refcnt != 0, ("SP refcnt overflow")); \
577 #define SP_DELREF(p) do { \
578 IPSEC_ASSERT((p)->refcnt > 0, ("SP refcnt underflow")); \
584 * Update the refcnt while holding the SPTREE lock.
587 key_addref(struct secpolicy *sp)
595 * Return 0 when there are known to be no SP's for the specified
596 * direction. Otherwise return 1. This is used by IPsec code
597 * to optimize performance.
600 key_havesp(u_int dir)
603 return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ?
604 LIST_FIRST(&V_sptree[dir]) != NULL : 1);
607 /* %%% IPsec policy management */
609 * allocating a SP for OUTBOUND or INBOUND packet.
610 * Must call key_freesp() later.
611 * OUT: NULL: not found
612 * others: found and return the pointer.
615 key_allocsp(struct secpolicyindex *spidx, u_int dir, const char* where, int tag)
617 struct secpolicy *sp;
619 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
620 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
621 ("invalid direction %u", dir));
623 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
624 printf("DP %s from %s:%u\n", __func__, where, tag));
627 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
628 printf("*** objects\n");
629 kdebug_secpolicyindex(spidx));
632 LIST_FOREACH(sp, &V_sptree[dir], chain) {
633 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
634 printf("*** in SPD\n");
635 kdebug_secpolicyindex(&sp->spidx));
637 if (sp->state == IPSEC_SPSTATE_DEAD)
639 if (key_cmpspidx_withmask(&sp->spidx, spidx))
646 KEY_CHKSPDIR(sp->spidx.dir, dir, __func__);
648 /* found a SPD entry */
649 sp->lastused = time_second;
654 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
655 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
656 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
661 * allocating a SP for OUTBOUND or INBOUND packet.
662 * Must call key_freesp() later.
663 * OUT: NULL: not found
664 * others: found and return the pointer.
667 key_allocsp2(u_int32_t spi,
668 union sockaddr_union *dst,
671 const char* where, int tag)
673 struct secpolicy *sp;
675 IPSEC_ASSERT(dst != NULL, ("null dst"));
676 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
677 ("invalid direction %u", dir));
679 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
680 printf("DP %s from %s:%u\n", __func__, where, tag));
683 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
684 printf("*** objects\n");
685 printf("spi %u proto %u dir %u\n", spi, proto, dir);
686 kdebug_sockaddr(&dst->sa));
689 LIST_FOREACH(sp, &V_sptree[dir], chain) {
690 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
691 printf("*** in SPD\n");
692 kdebug_secpolicyindex(&sp->spidx));
694 if (sp->state == IPSEC_SPSTATE_DEAD)
696 /* compare simple values, then dst address */
697 if (sp->spidx.ul_proto != proto)
699 /* NB: spi's must exist and match */
700 if (!sp->req || !sp->req->sav || sp->req->sav->spi != spi)
702 if (key_sockaddrcmp(&sp->spidx.dst.sa, &dst->sa, 1) == 0)
709 KEY_CHKSPDIR(sp->spidx.dir, dir, __func__);
711 /* found a SPD entry */
712 sp->lastused = time_second;
717 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
718 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
719 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
725 * return a policy that matches this particular inbound packet.
729 key_gettunnel(const struct sockaddr *osrc,
730 const struct sockaddr *odst,
731 const struct sockaddr *isrc,
732 const struct sockaddr *idst,
733 const char* where, int tag)
735 struct secpolicy *sp;
736 const int dir = IPSEC_DIR_INBOUND;
737 struct ipsecrequest *r1, *r2, *p;
738 struct secpolicyindex spidx;
740 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
741 printf("DP %s from %s:%u\n", __func__, where, tag));
743 if (isrc->sa_family != idst->sa_family) {
744 ipseclog((LOG_ERR, "%s: protocol family mismatched %d != %d\n.",
745 __func__, isrc->sa_family, idst->sa_family));
751 LIST_FOREACH(sp, &V_sptree[dir], chain) {
752 if (sp->state == IPSEC_SPSTATE_DEAD)
756 for (p = sp->req; p; p = p->next) {
757 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
764 /* here we look at address matches only */
766 if (isrc->sa_len > sizeof(spidx.src) ||
767 idst->sa_len > sizeof(spidx.dst))
769 bcopy(isrc, &spidx.src, isrc->sa_len);
770 bcopy(idst, &spidx.dst, idst->sa_len);
771 if (!key_cmpspidx_withmask(&sp->spidx, &spidx))
774 if (key_sockaddrcmp(&r1->saidx.src.sa, isrc, 0) ||
775 key_sockaddrcmp(&r1->saidx.dst.sa, idst, 0))
779 if (key_sockaddrcmp(&r2->saidx.src.sa, osrc, 0) ||
780 key_sockaddrcmp(&r2->saidx.dst.sa, odst, 0))
789 sp->lastused = time_second;
794 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
795 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
796 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
802 * allocating an SA entry for an *OUTBOUND* packet.
803 * checking each request entries in SP, and acquire an SA if need.
804 * OUT: 0: there are valid requests.
805 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
808 key_checkrequest(struct ipsecrequest *isr, const struct secasindex *saidx)
813 IPSEC_ASSERT(isr != NULL, ("null isr"));
814 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
815 IPSEC_ASSERT(saidx->mode == IPSEC_MODE_TRANSPORT ||
816 saidx->mode == IPSEC_MODE_TUNNEL,
817 ("unexpected policy %u", saidx->mode));
820 * XXX guard against protocol callbacks from the crypto
821 * thread as they reference ipsecrequest.sav which we
822 * temporarily null out below. Need to rethink how we
823 * handle bundled SA's in the callback thread.
825 IPSECREQUEST_LOCK_ASSERT(isr);
827 /* get current level */
828 level = ipsec_get_reqlevel(isr);
831 * We do allocate new SA only if the state of SA in the holder is
832 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
834 if (isr->sav != NULL) {
835 if (isr->sav->sah == NULL)
836 panic("%s: sah is null.\n", __func__);
837 if (isr->sav == (struct secasvar *)LIST_FIRST(
838 &isr->sav->sah->savtree[SADB_SASTATE_DEAD])) {
839 KEY_FREESAV(&isr->sav);
845 * we free any SA stashed in the IPsec request because a different
846 * SA may be involved each time this request is checked, either
847 * because new SAs are being configured, or this request is
848 * associated with an unconnected datagram socket, or this request
849 * is associated with a system default policy.
851 * The operation may have negative impact to performance. We may
852 * want to check cached SA carefully, rather than picking new SA
855 if (isr->sav != NULL) {
856 KEY_FREESAV(&isr->sav);
862 * new SA allocation if no SA found.
863 * key_allocsa_policy should allocate the oldest SA available.
864 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
866 if (isr->sav == NULL)
867 isr->sav = key_allocsa_policy(saidx);
869 /* When there is SA. */
870 if (isr->sav != NULL) {
871 if (isr->sav->state != SADB_SASTATE_MATURE &&
872 isr->sav->state != SADB_SASTATE_DYING)
878 error = key_acquire(saidx, isr->sp);
880 /* XXX What should I do ? */
881 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
886 if (level != IPSEC_LEVEL_REQUIRE) {
887 /* XXX sigh, the interface to this routine is botched */
888 IPSEC_ASSERT(isr->sav == NULL, ("unexpected SA"));
896 * allocating a SA for policy entry from SAD.
897 * NOTE: searching SAD of aliving state.
898 * OUT: NULL: not found.
899 * others: found and return the pointer.
901 static struct secasvar *
902 key_allocsa_policy(const struct secasindex *saidx)
904 #define N(a) _ARRAYLEN(a)
905 struct secashead *sah;
906 struct secasvar *sav;
907 u_int stateidx, arraysize;
908 const u_int *state_valid;
911 LIST_FOREACH(sah, &V_sahtree, chain) {
912 if (sah->state == SADB_SASTATE_DEAD)
914 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID)) {
915 if (V_key_preferred_oldsa) {
916 state_valid = saorder_state_valid_prefer_old;
917 arraysize = N(saorder_state_valid_prefer_old);
919 state_valid = saorder_state_valid_prefer_new;
920 arraysize = N(saorder_state_valid_prefer_new);
931 /* search valid state */
932 for (stateidx = 0; stateidx < arraysize; stateidx++) {
933 sav = key_do_allocsa_policy(sah, state_valid[stateidx]);
943 * searching SAD with direction, protocol, mode and state.
944 * called by key_allocsa_policy().
947 * others : found, pointer to a SA.
949 static struct secasvar *
950 key_do_allocsa_policy(struct secashead *sah, u_int state)
952 struct secasvar *sav, *nextsav, *candidate, *d;
958 for (sav = LIST_FIRST(&sah->savtree[state]);
962 nextsav = LIST_NEXT(sav, chain);
965 KEY_CHKSASTATE(sav->state, state, __func__);
968 if (candidate == NULL) {
973 /* Which SA is the better ? */
975 IPSEC_ASSERT(candidate->lft_c != NULL,
976 ("null candidate lifetime"));
977 IPSEC_ASSERT(sav->lft_c != NULL, ("null sav lifetime"));
979 /* What the best method is to compare ? */
980 if (V_key_preferred_oldsa) {
981 if (candidate->lft_c->addtime >
982 sav->lft_c->addtime) {
989 /* preferred new sa rather than old sa */
990 if (candidate->lft_c->addtime <
991 sav->lft_c->addtime) {
998 * prepared to delete the SA when there is more
999 * suitable candidate and the lifetime of the SA is not
1002 if (d->lft_h->addtime != 0) {
1003 struct mbuf *m, *result;
1006 key_sa_chgstate(d, SADB_SASTATE_DEAD);
1008 IPSEC_ASSERT(d->refcnt > 0, ("bogus ref count"));
1010 satype = key_proto2satype(d->sah->saidx.proto);
1014 m = key_setsadbmsg(SADB_DELETE, 0,
1015 satype, 0, 0, d->refcnt - 1);
1020 /* set sadb_address for saidx's. */
1021 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
1022 &d->sah->saidx.src.sa,
1023 d->sah->saidx.src.sa.sa_len << 3,
1029 /* set sadb_address for saidx's. */
1030 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
1031 &d->sah->saidx.dst.sa,
1032 d->sah->saidx.dst.sa.sa_len << 3,
1038 /* create SA extension */
1039 m = key_setsadbsa(d);
1044 if (result->m_len < sizeof(struct sadb_msg)) {
1045 result = m_pullup(result,
1046 sizeof(struct sadb_msg));
1051 result->m_pkthdr.len = 0;
1052 for (m = result; m; m = m->m_next)
1053 result->m_pkthdr.len += m->m_len;
1054 mtod(result, struct sadb_msg *)->sadb_msg_len =
1055 PFKEY_UNIT64(result->m_pkthdr.len);
1057 if (key_sendup_mbuf(NULL, result,
1058 KEY_SENDUP_REGISTERED))
1065 sa_addref(candidate);
1066 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1067 printf("DP %s cause refcnt++:%d SA:%p\n",
1068 __func__, candidate->refcnt, candidate));
1076 * allocating a usable SA entry for a *INBOUND* packet.
1077 * Must call key_freesav() later.
1078 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
1079 * NULL: not found, or error occured.
1081 * In the comparison, no source address is used--for RFC2401 conformance.
1082 * To quote, from section 4.1:
1083 * A security association is uniquely identified by a triple consisting
1084 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1085 * security protocol (AH or ESP) identifier.
1086 * Note that, however, we do need to keep source address in IPsec SA.
1087 * IKE specification and PF_KEY specification do assume that we
1088 * keep source address in IPsec SA. We see a tricky situation here.
1092 union sockaddr_union *dst,
1095 const char* where, int tag)
1097 struct secashead *sah;
1098 struct secasvar *sav;
1099 u_int stateidx, arraysize, state;
1100 const u_int *saorder_state_valid;
1103 IPSEC_ASSERT(dst != NULL, ("null dst address"));
1105 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1106 printf("DP %s from %s:%u\n", __func__, where, tag));
1109 chkport = (dst->sa.sa_family == AF_INET &&
1110 dst->sa.sa_len == sizeof(struct sockaddr_in) &&
1111 dst->sin.sin_port != 0);
1118 * XXX: to be checked internal IP header somewhere. Also when
1119 * IPsec tunnel packet is received. But ESP tunnel mode is
1120 * encrypted so we can't check internal IP header.
1123 if (V_key_preferred_oldsa) {
1124 saorder_state_valid = saorder_state_valid_prefer_old;
1125 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
1127 saorder_state_valid = saorder_state_valid_prefer_new;
1128 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
1130 LIST_FOREACH(sah, &V_sahtree, chain) {
1131 /* search valid state */
1132 for (stateidx = 0; stateidx < arraysize; stateidx++) {
1133 state = saorder_state_valid[stateidx];
1134 LIST_FOREACH(sav, &sah->savtree[state], chain) {
1136 KEY_CHKSASTATE(sav->state, state, __func__);
1137 /* do not return entries w/ unusable state */
1138 if (sav->state != SADB_SASTATE_MATURE &&
1139 sav->state != SADB_SASTATE_DYING)
1141 if (proto != sav->sah->saidx.proto)
1143 if (spi != sav->spi)
1145 #if 0 /* don't check src */
1146 /* check src address */
1147 if (key_sockaddrcmp(&src->sa, &sav->sah->saidx.src.sa, chkport) != 0)
1150 /* check dst address */
1151 if (key_sockaddrcmp(&dst->sa, &sav->sah->saidx.dst.sa, chkport) != 0)
1162 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1163 printf("DP %s return SA:%p; refcnt %u\n", __func__,
1164 sav, sav ? sav->refcnt : 0));
1169 * Must be called after calling key_allocsp().
1170 * For both the packet without socket and key_freeso().
1173 _key_freesp(struct secpolicy **spp, const char* where, int tag)
1175 struct secpolicy *sp = *spp;
1177 IPSEC_ASSERT(sp != NULL, ("null sp"));
1182 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1183 printf("DP %s SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1184 __func__, sp, sp->id, where, tag, sp->refcnt));
1186 if (sp->refcnt == 0) {
1194 * Must be called after calling key_allocsp().
1195 * For the packet with socket.
1198 key_freeso(struct socket *so)
1200 IPSEC_ASSERT(so != NULL, ("null so"));
1202 switch (so->so_proto->pr_domain->dom_family) {
1203 #if defined(INET) || defined(INET6)
1211 struct inpcb *pcb = sotoinpcb(so);
1213 /* Does it have a PCB ? */
1216 key_freesp_so(&pcb->inp_sp->sp_in);
1217 key_freesp_so(&pcb->inp_sp->sp_out);
1220 #endif /* INET || INET6 */
1222 ipseclog((LOG_DEBUG, "%s: unknown address family=%d.\n",
1223 __func__, so->so_proto->pr_domain->dom_family));
1229 key_freesp_so(struct secpolicy **sp)
1231 IPSEC_ASSERT(sp != NULL && *sp != NULL, ("null sp"));
1233 if ((*sp)->policy == IPSEC_POLICY_ENTRUST ||
1234 (*sp)->policy == IPSEC_POLICY_BYPASS)
1237 IPSEC_ASSERT((*sp)->policy == IPSEC_POLICY_IPSEC,
1238 ("invalid policy %u", (*sp)->policy));
1243 * Must be called after calling key_allocsa().
1244 * This function is called by key_freesp() to free some SA allocated
1248 key_freesav(struct secasvar **psav, const char* where, int tag)
1250 struct secasvar *sav = *psav;
1252 IPSEC_ASSERT(sav != NULL, ("null sav"));
1254 if (sa_delref(sav)) {
1255 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1256 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1257 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1261 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1262 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1263 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1267 /* %%% SPD management */
1269 * free security policy entry.
1272 key_delsp(struct secpolicy *sp)
1274 struct ipsecrequest *isr, *nextisr;
1276 IPSEC_ASSERT(sp != NULL, ("null sp"));
1277 SPTREE_LOCK_ASSERT();
1279 sp->state = IPSEC_SPSTATE_DEAD;
1281 IPSEC_ASSERT(sp->refcnt == 0,
1282 ("SP with references deleted (refcnt %u)", sp->refcnt));
1284 /* remove from SP index */
1285 if (__LIST_CHAINED(sp))
1286 LIST_REMOVE(sp, chain);
1288 for (isr = sp->req; isr != NULL; isr = nextisr) {
1289 if (isr->sav != NULL) {
1290 KEY_FREESAV(&isr->sav);
1294 nextisr = isr->next;
1302 * OUT: NULL : not found
1303 * others : found, pointer to a SP.
1305 static struct secpolicy *
1306 key_getsp(struct secpolicyindex *spidx)
1308 struct secpolicy *sp;
1310 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
1313 LIST_FOREACH(sp, &V_sptree[spidx->dir], chain) {
1314 if (sp->state == IPSEC_SPSTATE_DEAD)
1316 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1328 * OUT: NULL : not found
1329 * others : found, pointer to a SP.
1331 static struct secpolicy *
1332 key_getspbyid(u_int32_t id)
1334 struct secpolicy *sp;
1337 LIST_FOREACH(sp, &V_sptree[IPSEC_DIR_INBOUND], chain) {
1338 if (sp->state == IPSEC_SPSTATE_DEAD)
1346 LIST_FOREACH(sp, &V_sptree[IPSEC_DIR_OUTBOUND], chain) {
1347 if (sp->state == IPSEC_SPSTATE_DEAD)
1361 key_newsp(const char* where, int tag)
1363 struct secpolicy *newsp = NULL;
1365 newsp = (struct secpolicy *)
1366 malloc(sizeof(struct secpolicy), M_IPSEC_SP, M_NOWAIT|M_ZERO);
1368 SECPOLICY_LOCK_INIT(newsp);
1373 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1374 printf("DP %s from %s:%u return SP:%p\n", __func__,
1375 where, tag, newsp));
1380 _key_delsp(struct secpolicy *sp)
1382 SECPOLICY_LOCK_DESTROY(sp);
1383 free(sp, M_IPSEC_SP);
1387 * create secpolicy structure from sadb_x_policy structure.
1388 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1389 * so must be set properly later.
1392 key_msg2sp(xpl0, len, error)
1393 struct sadb_x_policy *xpl0;
1397 struct secpolicy *newsp;
1399 IPSEC_ASSERT(xpl0 != NULL, ("null xpl0"));
1400 IPSEC_ASSERT(len >= sizeof(*xpl0), ("policy too short: %zu", len));
1402 if (len != PFKEY_EXTLEN(xpl0)) {
1403 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n", __func__));
1408 if ((newsp = KEY_NEWSP()) == NULL) {
1413 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1414 newsp->policy = xpl0->sadb_x_policy_type;
1417 switch (xpl0->sadb_x_policy_type) {
1418 case IPSEC_POLICY_DISCARD:
1419 case IPSEC_POLICY_NONE:
1420 case IPSEC_POLICY_ENTRUST:
1421 case IPSEC_POLICY_BYPASS:
1425 case IPSEC_POLICY_IPSEC:
1428 struct sadb_x_ipsecrequest *xisr;
1429 struct ipsecrequest **p_isr = &newsp->req;
1431 /* validity check */
1432 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1433 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n",
1440 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1441 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1445 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1446 ipseclog((LOG_DEBUG, "%s: invalid ipsecrequest "
1447 "length.\n", __func__));
1453 /* allocate request buffer */
1454 /* NB: data structure is zero'd */
1455 *p_isr = ipsec_newisr();
1456 if ((*p_isr) == NULL) {
1457 ipseclog((LOG_DEBUG,
1458 "%s: No more memory.\n", __func__));
1465 switch (xisr->sadb_x_ipsecrequest_proto) {
1468 case IPPROTO_IPCOMP:
1471 ipseclog((LOG_DEBUG,
1472 "%s: invalid proto type=%u\n", __func__,
1473 xisr->sadb_x_ipsecrequest_proto));
1475 *error = EPROTONOSUPPORT;
1478 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1480 switch (xisr->sadb_x_ipsecrequest_mode) {
1481 case IPSEC_MODE_TRANSPORT:
1482 case IPSEC_MODE_TUNNEL:
1484 case IPSEC_MODE_ANY:
1486 ipseclog((LOG_DEBUG,
1487 "%s: invalid mode=%u\n", __func__,
1488 xisr->sadb_x_ipsecrequest_mode));
1493 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1495 switch (xisr->sadb_x_ipsecrequest_level) {
1496 case IPSEC_LEVEL_DEFAULT:
1497 case IPSEC_LEVEL_USE:
1498 case IPSEC_LEVEL_REQUIRE:
1500 case IPSEC_LEVEL_UNIQUE:
1501 /* validity check */
1503 * If range violation of reqid, kernel will
1504 * update it, don't refuse it.
1506 if (xisr->sadb_x_ipsecrequest_reqid
1507 > IPSEC_MANUAL_REQID_MAX) {
1508 ipseclog((LOG_DEBUG,
1509 "%s: reqid=%d range "
1510 "violation, updated by kernel.\n",
1512 xisr->sadb_x_ipsecrequest_reqid));
1513 xisr->sadb_x_ipsecrequest_reqid = 0;
1516 /* allocate new reqid id if reqid is zero. */
1517 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1519 if ((reqid = key_newreqid()) == 0) {
1524 (*p_isr)->saidx.reqid = reqid;
1525 xisr->sadb_x_ipsecrequest_reqid = reqid;
1527 /* set it for manual keying. */
1528 (*p_isr)->saidx.reqid =
1529 xisr->sadb_x_ipsecrequest_reqid;
1534 ipseclog((LOG_DEBUG, "%s: invalid level=%u\n",
1536 xisr->sadb_x_ipsecrequest_level));
1541 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1543 /* set IP addresses if there */
1544 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1545 struct sockaddr *paddr;
1547 paddr = (struct sockaddr *)(xisr + 1);
1549 /* validity check */
1551 > sizeof((*p_isr)->saidx.src)) {
1552 ipseclog((LOG_DEBUG, "%s: invalid "
1553 "request address length.\n",
1559 bcopy(paddr, &(*p_isr)->saidx.src,
1562 paddr = (struct sockaddr *)((caddr_t)paddr
1565 /* validity check */
1567 > sizeof((*p_isr)->saidx.dst)) {
1568 ipseclog((LOG_DEBUG, "%s: invalid "
1569 "request address length.\n",
1575 bcopy(paddr, &(*p_isr)->saidx.dst,
1579 (*p_isr)->sp = newsp;
1581 /* initialization for the next. */
1582 p_isr = &(*p_isr)->next;
1583 tlen -= xisr->sadb_x_ipsecrequest_len;
1585 /* validity check */
1587 ipseclog((LOG_DEBUG, "%s: becoming tlen < 0.\n",
1594 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1595 + xisr->sadb_x_ipsecrequest_len);
1600 ipseclog((LOG_DEBUG, "%s: invalid policy type.\n", __func__));
1613 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1615 auto_reqid = (auto_reqid == ~0
1616 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1618 /* XXX should be unique check */
1624 * copy secpolicy struct to sadb_x_policy structure indicated.
1628 struct secpolicy *sp;
1630 struct sadb_x_policy *xpl;
1635 IPSEC_ASSERT(sp != NULL, ("null policy"));
1637 tlen = key_getspreqmsglen(sp);
1639 m = key_alloc_mbuf(tlen);
1640 if (!m || m->m_next) { /*XXX*/
1648 xpl = mtod(m, struct sadb_x_policy *);
1651 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1652 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1653 xpl->sadb_x_policy_type = sp->policy;
1654 xpl->sadb_x_policy_dir = sp->spidx.dir;
1655 xpl->sadb_x_policy_id = sp->id;
1656 p = (caddr_t)xpl + sizeof(*xpl);
1658 /* if is the policy for ipsec ? */
1659 if (sp->policy == IPSEC_POLICY_IPSEC) {
1660 struct sadb_x_ipsecrequest *xisr;
1661 struct ipsecrequest *isr;
1663 for (isr = sp->req; isr != NULL; isr = isr->next) {
1665 xisr = (struct sadb_x_ipsecrequest *)p;
1667 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1668 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1669 xisr->sadb_x_ipsecrequest_level = isr->level;
1670 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1673 bcopy(&isr->saidx.src, p, isr->saidx.src.sa.sa_len);
1674 p += isr->saidx.src.sa.sa_len;
1675 bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len);
1676 p += isr->saidx.src.sa.sa_len;
1678 xisr->sadb_x_ipsecrequest_len =
1679 PFKEY_ALIGN8(sizeof(*xisr)
1680 + isr->saidx.src.sa.sa_len
1681 + isr->saidx.dst.sa.sa_len);
1688 /* m will not be freed nor modified */
1689 static struct mbuf *
1691 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1692 int ndeep, int nitem, ...)
1694 key_gather_mbuf(m, mhp, ndeep, nitem, va_alist)
1696 const struct sadb_msghdr *mhp;
1705 struct mbuf *result = NULL, *n;
1708 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1709 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1711 va_start(ap, nitem);
1712 for (i = 0; i < nitem; i++) {
1713 idx = va_arg(ap, int);
1714 if (idx < 0 || idx > SADB_EXT_MAX)
1716 /* don't attempt to pull empty extension */
1717 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1719 if (idx != SADB_EXT_RESERVED &&
1720 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1723 if (idx == SADB_EXT_RESERVED) {
1724 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1726 IPSEC_ASSERT(len <= MHLEN, ("header too big %u", len));
1728 MGETHDR(n, M_DONTWAIT, MT_DATA);
1733 m_copydata(m, 0, sizeof(struct sadb_msg),
1735 } else if (i < ndeep) {
1736 len = mhp->extlen[idx];
1737 n = key_alloc_mbuf(len);
1738 if (!n || n->m_next) { /*XXX*/
1743 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1746 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1759 if ((result->m_flags & M_PKTHDR) != 0) {
1760 result->m_pkthdr.len = 0;
1761 for (n = result; n; n = n->m_next)
1762 result->m_pkthdr.len += n->m_len;
1773 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1774 * add an entry to SP database, when received
1775 * <base, address(SD), (lifetime(H),) policy>
1777 * Adding to SP database,
1779 * <base, address(SD), (lifetime(H),) policy>
1780 * to the socket which was send.
1782 * SPDADD set a unique policy entry.
1783 * SPDSETIDX like SPDADD without a part of policy requests.
1784 * SPDUPDATE replace a unique policy entry.
1786 * m will always be freed.
1789 key_spdadd(so, m, mhp)
1792 const struct sadb_msghdr *mhp;
1794 struct sadb_address *src0, *dst0;
1795 struct sadb_x_policy *xpl0, *xpl;
1796 struct sadb_lifetime *lft = NULL;
1797 struct secpolicyindex spidx;
1798 struct secpolicy *newsp;
1801 IPSEC_ASSERT(so != NULL, ("null socket"));
1802 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1803 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1804 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
1806 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1807 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1808 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1809 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1810 return key_senderror(so, m, EINVAL);
1812 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1813 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1814 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1815 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1817 return key_senderror(so, m, EINVAL);
1819 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1820 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1821 < sizeof(struct sadb_lifetime)) {
1822 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1824 return key_senderror(so, m, EINVAL);
1826 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1829 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1830 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1831 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1834 * Note: do not parse SADB_X_EXT_NAT_T_* here:
1835 * we are processing traffic endpoints.
1839 /* XXX boundary check against sa_len */
1840 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1843 src0->sadb_address_prefixlen,
1844 dst0->sadb_address_prefixlen,
1845 src0->sadb_address_proto,
1848 /* checking the direciton. */
1849 switch (xpl0->sadb_x_policy_dir) {
1850 case IPSEC_DIR_INBOUND:
1851 case IPSEC_DIR_OUTBOUND:
1854 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
1855 mhp->msg->sadb_msg_errno = EINVAL;
1860 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1861 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1862 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1863 ipseclog((LOG_DEBUG, "%s: Invalid policy type.\n", __func__));
1864 return key_senderror(so, m, EINVAL);
1867 /* policy requests are mandatory when action is ipsec. */
1868 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1869 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1870 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1871 ipseclog((LOG_DEBUG, "%s: some policy requests part required\n",
1873 return key_senderror(so, m, EINVAL);
1877 * checking there is SP already or not.
1878 * SPDUPDATE doesn't depend on whether there is a SP or not.
1879 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1882 newsp = key_getsp(&spidx);
1883 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1885 newsp->state = IPSEC_SPSTATE_DEAD;
1889 if (newsp != NULL) {
1891 ipseclog((LOG_DEBUG, "%s: a SP entry exists already.\n",
1893 return key_senderror(so, m, EEXIST);
1897 /* allocation new SP entry */
1898 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1899 return key_senderror(so, m, error);
1902 if ((newsp->id = key_getnewspid()) == 0) {
1904 return key_senderror(so, m, ENOBUFS);
1907 /* XXX boundary check against sa_len */
1908 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1911 src0->sadb_address_prefixlen,
1912 dst0->sadb_address_prefixlen,
1913 src0->sadb_address_proto,
1916 /* sanity check on addr pair */
1917 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1918 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1920 return key_senderror(so, m, EINVAL);
1922 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1923 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1925 return key_senderror(so, m, EINVAL);
1928 if (newsp->req && newsp->req->saidx.src.sa.sa_family) {
1929 struct sockaddr *sa;
1930 sa = (struct sockaddr *)(src0 + 1);
1931 if (sa->sa_family != newsp->req->saidx.src.sa.sa_family) {
1933 return key_senderror(so, m, EINVAL);
1936 if (newsp->req && newsp->req->saidx.dst.sa.sa_family) {
1937 struct sockaddr *sa;
1938 sa = (struct sockaddr *)(dst0 + 1);
1939 if (sa->sa_family != newsp->req->saidx.dst.sa.sa_family) {
1941 return key_senderror(so, m, EINVAL);
1946 newsp->created = time_second;
1947 newsp->lastused = newsp->created;
1948 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1949 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1951 newsp->refcnt = 1; /* do not reclaim until I say I do */
1952 newsp->state = IPSEC_SPSTATE_ALIVE;
1953 LIST_INSERT_TAIL(&V_sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1955 /* delete the entry in spacqtree */
1956 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1957 struct secspacq *spacq = key_getspacq(&spidx);
1958 if (spacq != NULL) {
1959 /* reset counter in order to deletion by timehandler. */
1960 spacq->created = time_second;
1967 struct mbuf *n, *mpolicy;
1968 struct sadb_msg *newmsg;
1972 * Note: do not send SADB_X_EXT_NAT_T_* here:
1973 * we are sending traffic endpoints.
1976 /* create new sadb_msg to reply. */
1978 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1979 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1980 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1982 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1984 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1987 return key_senderror(so, m, ENOBUFS);
1989 if (n->m_len < sizeof(*newmsg)) {
1990 n = m_pullup(n, sizeof(*newmsg));
1992 return key_senderror(so, m, ENOBUFS);
1994 newmsg = mtod(n, struct sadb_msg *);
1995 newmsg->sadb_msg_errno = 0;
1996 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1999 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
2000 sizeof(*xpl), &off);
2001 if (mpolicy == NULL) {
2002 /* n is already freed */
2003 return key_senderror(so, m, ENOBUFS);
2005 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
2006 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
2008 return key_senderror(so, m, EINVAL);
2010 xpl->sadb_x_policy_id = newsp->id;
2013 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2018 * get new policy id.
2026 u_int32_t newid = 0;
2027 int count = V_key_spi_trycnt; /* XXX */
2028 struct secpolicy *sp;
2030 /* when requesting to allocate spi ranged */
2032 newid = (V_policy_id = (V_policy_id == ~0 ? 1 : V_policy_id + 1));
2034 if ((sp = key_getspbyid(newid)) == NULL)
2040 if (count == 0 || newid == 0) {
2041 ipseclog((LOG_DEBUG, "%s: to allocate policy id is failed.\n",
2050 * SADB_SPDDELETE processing
2052 * <base, address(SD), policy(*)>
2053 * from the user(?), and set SADB_SASTATE_DEAD,
2055 * <base, address(SD), policy(*)>
2057 * policy(*) including direction of policy.
2059 * m will always be freed.
2062 key_spddelete(so, m, mhp)
2065 const struct sadb_msghdr *mhp;
2067 struct sadb_address *src0, *dst0;
2068 struct sadb_x_policy *xpl0;
2069 struct secpolicyindex spidx;
2070 struct secpolicy *sp;
2072 IPSEC_ASSERT(so != NULL, ("null so"));
2073 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2074 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2075 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2077 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
2078 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
2079 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
2080 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2082 return key_senderror(so, m, EINVAL);
2084 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
2085 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
2086 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2087 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2089 return key_senderror(so, m, EINVAL);
2092 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2093 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2094 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2097 * Note: do not parse SADB_X_EXT_NAT_T_* here:
2098 * we are processing traffic endpoints.
2102 /* XXX boundary check against sa_len */
2103 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2106 src0->sadb_address_prefixlen,
2107 dst0->sadb_address_prefixlen,
2108 src0->sadb_address_proto,
2111 /* checking the direciton. */
2112 switch (xpl0->sadb_x_policy_dir) {
2113 case IPSEC_DIR_INBOUND:
2114 case IPSEC_DIR_OUTBOUND:
2117 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
2118 return key_senderror(so, m, EINVAL);
2121 /* Is there SP in SPD ? */
2122 if ((sp = key_getsp(&spidx)) == NULL) {
2123 ipseclog((LOG_DEBUG, "%s: no SP found.\n", __func__));
2124 return key_senderror(so, m, EINVAL);
2127 /* save policy id to buffer to be returned. */
2128 xpl0->sadb_x_policy_id = sp->id;
2130 sp->state = IPSEC_SPSTATE_DEAD;
2135 struct sadb_msg *newmsg;
2138 * Note: do not send SADB_X_EXT_NAT_T_* here:
2139 * we are sending traffic endpoints.
2142 /* create new sadb_msg to reply. */
2143 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2144 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2146 return key_senderror(so, m, ENOBUFS);
2148 newmsg = mtod(n, struct sadb_msg *);
2149 newmsg->sadb_msg_errno = 0;
2150 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2153 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2158 * SADB_SPDDELETE2 processing
2161 * from the user(?), and set SADB_SASTATE_DEAD,
2165 * policy(*) including direction of policy.
2167 * m will always be freed.
2170 key_spddelete2(so, m, mhp)
2173 const struct sadb_msghdr *mhp;
2176 struct secpolicy *sp;
2178 IPSEC_ASSERT(so != NULL, ("null socket"));
2179 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2180 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2181 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2183 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2184 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2185 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", __func__));
2186 return key_senderror(so, m, EINVAL);
2189 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2191 /* Is there SP in SPD ? */
2192 if ((sp = key_getspbyid(id)) == NULL) {
2193 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2194 return key_senderror(so, m, EINVAL);
2197 sp->state = IPSEC_SPSTATE_DEAD;
2201 struct mbuf *n, *nn;
2202 struct sadb_msg *newmsg;
2205 /* create new sadb_msg to reply. */
2206 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2208 MGETHDR(n, M_DONTWAIT, MT_DATA);
2209 if (n && len > MHLEN) {
2210 MCLGET(n, M_DONTWAIT);
2211 if ((n->m_flags & M_EXT) == 0) {
2217 return key_senderror(so, m, ENOBUFS);
2223 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2224 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2226 IPSEC_ASSERT(off == len, ("length inconsistency (off %u len %u)",
2229 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2230 mhp->extlen[SADB_X_EXT_POLICY], M_DONTWAIT);
2233 return key_senderror(so, m, ENOBUFS);
2236 n->m_pkthdr.len = 0;
2237 for (nn = n; nn; nn = nn->m_next)
2238 n->m_pkthdr.len += nn->m_len;
2240 newmsg = mtod(n, struct sadb_msg *);
2241 newmsg->sadb_msg_errno = 0;
2242 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2245 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2250 * SADB_X_GET processing
2255 * <base, address(SD), policy>
2257 * policy(*) including direction of policy.
2259 * m will always be freed.
2262 key_spdget(so, m, mhp)
2265 const struct sadb_msghdr *mhp;
2268 struct secpolicy *sp;
2271 IPSEC_ASSERT(so != NULL, ("null socket"));
2272 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2273 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2274 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2276 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2277 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2278 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2280 return key_senderror(so, m, EINVAL);
2283 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2285 /* Is there SP in SPD ? */
2286 if ((sp = key_getspbyid(id)) == NULL) {
2287 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2288 return key_senderror(so, m, ENOENT);
2291 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2294 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2296 return key_senderror(so, m, ENOBUFS);
2300 * SADB_X_SPDACQUIRE processing.
2301 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2304 * to KMD, and expect to receive
2305 * <base> with SADB_X_SPDACQUIRE if error occured,
2308 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2309 * policy(*) is without policy requests.
2312 * others: error number
2316 struct secpolicy *sp;
2318 struct mbuf *result = NULL, *m;
2319 struct secspacq *newspacq;
2321 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2322 IPSEC_ASSERT(sp->req == NULL, ("policy exists"));
2323 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
2324 ("policy not IPSEC %u", sp->policy));
2326 /* Get an entry to check whether sent message or not. */
2327 newspacq = key_getspacq(&sp->spidx);
2328 if (newspacq != NULL) {
2329 if (V_key_blockacq_count < newspacq->count) {
2330 /* reset counter and do send message. */
2331 newspacq->count = 0;
2333 /* increment counter and do nothing. */
2339 /* make new entry for blocking to send SADB_ACQUIRE. */
2340 newspacq = key_newspacq(&sp->spidx);
2341 if (newspacq == NULL)
2345 /* create new sadb_msg to reply. */
2346 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2352 result->m_pkthdr.len = 0;
2353 for (m = result; m; m = m->m_next)
2354 result->m_pkthdr.len += m->m_len;
2356 mtod(result, struct sadb_msg *)->sadb_msg_len =
2357 PFKEY_UNIT64(result->m_pkthdr.len);
2359 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2363 * SADB_SPDFLUSH processing
2366 * from the user, and free all entries in secpctree.
2370 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2372 * m will always be freed.
2375 key_spdflush(so, m, mhp)
2378 const struct sadb_msghdr *mhp;
2380 struct sadb_msg *newmsg;
2381 struct secpolicy *sp;
2384 IPSEC_ASSERT(so != NULL, ("null socket"));
2385 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2386 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2387 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2389 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2390 return key_senderror(so, m, EINVAL);
2392 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2394 LIST_FOREACH(sp, &V_sptree[dir], chain)
2395 sp->state = IPSEC_SPSTATE_DEAD;
2399 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2400 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2401 return key_senderror(so, m, ENOBUFS);
2407 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2408 newmsg = mtod(m, struct sadb_msg *);
2409 newmsg->sadb_msg_errno = 0;
2410 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2412 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2416 * SADB_SPDDUMP processing
2419 * from the user, and dump all SP leaves
2424 * m will always be freed.
2427 key_spddump(so, m, mhp)
2430 const struct sadb_msghdr *mhp;
2432 struct secpolicy *sp;
2437 IPSEC_ASSERT(so != NULL, ("null socket"));
2438 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2439 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2440 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2442 /* search SPD entry and get buffer size. */
2445 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2446 LIST_FOREACH(sp, &V_sptree[dir], chain) {
2453 return key_senderror(so, m, ENOENT);
2456 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2457 LIST_FOREACH(sp, &V_sptree[dir], chain) {
2459 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2460 mhp->msg->sadb_msg_pid);
2463 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2472 static struct mbuf *
2473 key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq, u_int32_t pid)
2475 struct mbuf *result = NULL, *m;
2476 struct seclifetime lt;
2478 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2484 * Note: do not send SADB_X_EXT_NAT_T_* here:
2485 * we are sending traffic endpoints.
2487 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2488 &sp->spidx.src.sa, sp->spidx.prefs,
2489 sp->spidx.ul_proto);
2494 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2495 &sp->spidx.dst.sa, sp->spidx.prefd,
2496 sp->spidx.ul_proto);
2507 lt.addtime=sp->created;
2508 lt.usetime= sp->lastused;
2509 m = key_setlifetime(<, SADB_EXT_LIFETIME_CURRENT);
2514 lt.addtime=sp->lifetime;
2515 lt.usetime= sp->validtime;
2516 m = key_setlifetime(<, SADB_EXT_LIFETIME_HARD);
2522 if ((result->m_flags & M_PKTHDR) == 0)
2525 if (result->m_len < sizeof(struct sadb_msg)) {
2526 result = m_pullup(result, sizeof(struct sadb_msg));
2531 result->m_pkthdr.len = 0;
2532 for (m = result; m; m = m->m_next)
2533 result->m_pkthdr.len += m->m_len;
2535 mtod(result, struct sadb_msg *)->sadb_msg_len =
2536 PFKEY_UNIT64(result->m_pkthdr.len);
2546 * get PFKEY message length for security policy and request.
2549 key_getspreqmsglen(sp)
2550 struct secpolicy *sp;
2554 tlen = sizeof(struct sadb_x_policy);
2556 /* if is the policy for ipsec ? */
2557 if (sp->policy != IPSEC_POLICY_IPSEC)
2560 /* get length of ipsec requests */
2562 struct ipsecrequest *isr;
2565 for (isr = sp->req; isr != NULL; isr = isr->next) {
2566 len = sizeof(struct sadb_x_ipsecrequest)
2567 + isr->saidx.src.sa.sa_len
2568 + isr->saidx.dst.sa.sa_len;
2570 tlen += PFKEY_ALIGN8(len);
2578 * SADB_SPDEXPIRE processing
2580 * <base, address(SD), lifetime(CH), policy>
2584 * others : error number
2588 struct secpolicy *sp;
2590 struct mbuf *result = NULL, *m;
2593 struct sadb_lifetime *lt;
2595 /* XXX: Why do we lock ? */
2597 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2599 /* set msg header */
2600 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2607 /* create lifetime extension (current and hard) */
2608 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2609 m = key_alloc_mbuf(len);
2610 if (!m || m->m_next) { /*XXX*/
2616 bzero(mtod(m, caddr_t), len);
2617 lt = mtod(m, struct sadb_lifetime *);
2618 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2619 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2620 lt->sadb_lifetime_allocations = 0;
2621 lt->sadb_lifetime_bytes = 0;
2622 lt->sadb_lifetime_addtime = sp->created;
2623 lt->sadb_lifetime_usetime = sp->lastused;
2624 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2625 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2626 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2627 lt->sadb_lifetime_allocations = 0;
2628 lt->sadb_lifetime_bytes = 0;
2629 lt->sadb_lifetime_addtime = sp->lifetime;
2630 lt->sadb_lifetime_usetime = sp->validtime;
2634 * Note: do not send SADB_X_EXT_NAT_T_* here:
2635 * we are sending traffic endpoints.
2638 /* set sadb_address for source */
2639 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2641 sp->spidx.prefs, sp->spidx.ul_proto);
2648 /* set sadb_address for destination */
2649 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2651 sp->spidx.prefd, sp->spidx.ul_proto);
2666 if ((result->m_flags & M_PKTHDR) == 0) {
2671 if (result->m_len < sizeof(struct sadb_msg)) {
2672 result = m_pullup(result, sizeof(struct sadb_msg));
2673 if (result == NULL) {
2679 result->m_pkthdr.len = 0;
2680 for (m = result; m; m = m->m_next)
2681 result->m_pkthdr.len += m->m_len;
2683 mtod(result, struct sadb_msg *)->sadb_msg_len =
2684 PFKEY_UNIT64(result->m_pkthdr.len);
2686 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2694 /* %%% SAD management */
2696 * allocating a memory for new SA head, and copy from the values of mhp.
2697 * OUT: NULL : failure due to the lack of memory.
2698 * others : pointer to new SA head.
2700 static struct secashead *
2702 struct secasindex *saidx;
2704 struct secashead *newsah;
2706 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
2708 newsah = malloc(sizeof(struct secashead), M_IPSEC_SAH, M_NOWAIT|M_ZERO);
2709 if (newsah != NULL) {
2711 for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++)
2712 LIST_INIT(&newsah->savtree[i]);
2713 newsah->saidx = *saidx;
2715 /* add to saidxtree */
2716 newsah->state = SADB_SASTATE_MATURE;
2719 LIST_INSERT_HEAD(&V_sahtree, newsah, chain);
2726 * delete SA index and all SA registerd.
2730 struct secashead *sah;
2732 struct secasvar *sav, *nextsav;
2736 IPSEC_ASSERT(sah != NULL, ("NULL sah"));
2737 SAHTREE_LOCK_ASSERT();
2739 /* searching all SA registerd in the secindex. */
2741 stateidx < _ARRAYLEN(saorder_state_any);
2743 u_int state = saorder_state_any[stateidx];
2744 LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain, nextsav) {
2745 if (sav->refcnt == 0) {
2747 KEY_CHKSASTATE(state, sav->state, __func__);
2749 * do NOT call KEY_FREESAV here:
2750 * it will only delete the sav if refcnt == 1,
2751 * where we already know that refcnt == 0
2755 /* give up to delete this sa */
2760 if (!zombie) { /* delete only if there are savs */
2761 /* remove from tree of SA index */
2762 if (__LIST_CHAINED(sah))
2763 LIST_REMOVE(sah, chain);
2764 if (sah->sa_route.ro_rt) {
2765 RTFREE(sah->sa_route.ro_rt);
2766 sah->sa_route.ro_rt = (struct rtentry *)NULL;
2768 free(sah, M_IPSEC_SAH);
2773 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2774 * and copy the values of mhp into new buffer.
2775 * When SAD message type is GETSPI:
2776 * to set sequence number from acq_seq++,
2777 * to set zero to SPI.
2778 * not to call key_setsava().
2780 * others : pointer to new secasvar.
2782 * does not modify mbuf. does not free mbuf on error.
2784 static struct secasvar *
2785 key_newsav(m, mhp, sah, errp, where, tag)
2787 const struct sadb_msghdr *mhp;
2788 struct secashead *sah;
2793 struct secasvar *newsav;
2794 const struct sadb_sa *xsa;
2796 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2797 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2798 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2799 IPSEC_ASSERT(sah != NULL, ("null secashead"));
2801 newsav = malloc(sizeof(struct secasvar), M_IPSEC_SA, M_NOWAIT|M_ZERO);
2802 if (newsav == NULL) {
2803 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2808 switch (mhp->msg->sadb_msg_type) {
2812 #ifdef IPSEC_DOSEQCHECK
2813 /* sync sequence number */
2814 if (mhp->msg->sadb_msg_seq == 0)
2816 (V_acq_seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq));
2819 newsav->seq = mhp->msg->sadb_msg_seq;
2824 if (mhp->ext[SADB_EXT_SA] == NULL) {
2825 free(newsav, M_IPSEC_SA);
2827 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2832 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2833 newsav->spi = xsa->sadb_sa_spi;
2834 newsav->seq = mhp->msg->sadb_msg_seq;
2837 free(newsav, M_IPSEC_SA);
2844 /* copy sav values */
2845 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2846 *errp = key_setsaval(newsav, m, mhp);
2848 free(newsav, M_IPSEC_SA);
2854 SECASVAR_LOCK_INIT(newsav);
2857 newsav->created = time_second;
2858 newsav->pid = mhp->msg->sadb_msg_pid;
2863 newsav->state = SADB_SASTATE_LARVAL;
2865 /* XXX locking??? */
2866 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2869 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
2870 printf("DP %s from %s:%u return SP:%p\n", __func__,
2871 where, tag, newsav));
2877 * free() SA variable entry.
2880 key_cleansav(struct secasvar *sav)
2883 * Cleanup xform state. Note that zeroize'ing causes the
2884 * keys to be cleared; otherwise we must do it ourself.
2886 if (sav->tdb_xform != NULL) {
2887 sav->tdb_xform->xf_zeroize(sav);
2888 sav->tdb_xform = NULL;
2890 KASSERT(sav->iv == NULL, ("iv but no xform"));
2891 if (sav->key_auth != NULL)
2892 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
2893 if (sav->key_enc != NULL)
2894 bzero(sav->key_enc->key_data, _KEYLEN(sav->key_enc));
2896 if (sav->key_auth != NULL) {
2897 if (sav->key_auth->key_data != NULL)
2898 free(sav->key_auth->key_data, M_IPSEC_MISC);
2899 free(sav->key_auth, M_IPSEC_MISC);
2900 sav->key_auth = NULL;
2902 if (sav->key_enc != NULL) {
2903 if (sav->key_enc->key_data != NULL)
2904 free(sav->key_enc->key_data, M_IPSEC_MISC);
2905 free(sav->key_enc, M_IPSEC_MISC);
2906 sav->key_enc = NULL;
2909 bzero(sav->sched, sav->schedlen);
2910 free(sav->sched, M_IPSEC_MISC);
2913 if (sav->replay != NULL) {
2914 free(sav->replay, M_IPSEC_MISC);
2917 if (sav->lft_c != NULL) {
2918 free(sav->lft_c, M_IPSEC_MISC);
2921 if (sav->lft_h != NULL) {
2922 free(sav->lft_h, M_IPSEC_MISC);
2925 if (sav->lft_s != NULL) {
2926 free(sav->lft_s, M_IPSEC_MISC);
2932 * free() SA variable entry.
2936 struct secasvar *sav;
2938 IPSEC_ASSERT(sav != NULL, ("null sav"));
2939 IPSEC_ASSERT(sav->refcnt == 0, ("reference count %u > 0", sav->refcnt));
2941 /* remove from SA header */
2942 if (__LIST_CHAINED(sav))
2943 LIST_REMOVE(sav, chain);
2945 SECASVAR_LOCK_DESTROY(sav);
2946 free(sav, M_IPSEC_SA);
2953 * others : found, pointer to a SA.
2955 static struct secashead *
2957 struct secasindex *saidx;
2959 struct secashead *sah;
2962 LIST_FOREACH(sah, &V_sahtree, chain) {
2963 if (sah->state == SADB_SASTATE_DEAD)
2965 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2974 * check not to be duplicated SPI.
2975 * NOTE: this function is too slow due to searching all SAD.
2978 * others : found, pointer to a SA.
2980 static struct secasvar *
2981 key_checkspidup(saidx, spi)
2982 struct secasindex *saidx;
2985 struct secashead *sah;
2986 struct secasvar *sav;
2988 /* check address family */
2989 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
2990 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
2998 LIST_FOREACH(sah, &V_sahtree, chain) {
2999 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
3001 sav = key_getsavbyspi(sah, spi);
3011 * search SAD litmited alive SA, protocol, SPI.
3014 * others : found, pointer to a SA.
3016 static struct secasvar *
3017 key_getsavbyspi(sah, spi)
3018 struct secashead *sah;
3021 struct secasvar *sav;
3022 u_int stateidx, state;
3025 SAHTREE_LOCK_ASSERT();
3026 /* search all status */
3028 stateidx < _ARRAYLEN(saorder_state_alive);
3031 state = saorder_state_alive[stateidx];
3032 LIST_FOREACH(sav, &sah->savtree[state], chain) {
3035 if (sav->state != state) {
3036 ipseclog((LOG_DEBUG, "%s: "
3037 "invalid sav->state (queue: %d SA: %d)\n",
3038 __func__, state, sav->state));
3042 if (sav->spi == spi)
3051 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
3052 * You must update these if need.
3056 * does not modify mbuf. does not free mbuf on error.
3059 key_setsaval(sav, m, mhp)
3060 struct secasvar *sav;
3062 const struct sadb_msghdr *mhp;
3066 IPSEC_ASSERT(m != NULL, ("null mbuf"));
3067 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
3068 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
3070 /* initialization */
3072 sav->key_auth = NULL;
3073 sav->key_enc = NULL;
3080 sav->tdb_xform = NULL; /* transform */
3081 sav->tdb_encalgxform = NULL; /* encoding algorithm */
3082 sav->tdb_authalgxform = NULL; /* authentication algorithm */
3083 sav->tdb_compalgxform = NULL; /* compression algorithm */
3084 /* Initialize even if NAT-T not compiled in: */
3086 sav->natt_esp_frag_len = 0;
3089 if (mhp->ext[SADB_EXT_SA] != NULL) {
3090 const struct sadb_sa *sa0;
3092 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
3093 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
3098 sav->alg_auth = sa0->sadb_sa_auth;
3099 sav->alg_enc = sa0->sadb_sa_encrypt;
3100 sav->flags = sa0->sadb_sa_flags;
3103 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
3104 sav->replay = (struct secreplay *)
3105 malloc(sizeof(struct secreplay)+sa0->sadb_sa_replay, M_IPSEC_MISC, M_NOWAIT|M_ZERO);
3106 if (sav->replay == NULL) {
3107 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3112 if (sa0->sadb_sa_replay != 0)
3113 sav->replay->bitmap = (caddr_t)(sav->replay+1);
3114 sav->replay->wsize = sa0->sadb_sa_replay;
3118 /* Authentication keys */
3119 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
3120 const struct sadb_key *key0;
3123 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
3124 len = mhp->extlen[SADB_EXT_KEY_AUTH];
3127 if (len < sizeof(*key0)) {
3131 switch (mhp->msg->sadb_msg_satype) {
3132 case SADB_SATYPE_AH:
3133 case SADB_SATYPE_ESP:
3134 case SADB_X_SATYPE_TCPSIGNATURE:
3135 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3136 sav->alg_auth != SADB_X_AALG_NULL)
3139 case SADB_X_SATYPE_IPCOMP:
3145 ipseclog((LOG_DEBUG, "%s: invalid key_auth values.\n",
3150 sav->key_auth = (struct seckey *)key_dup_keymsg(key0, len,
3152 if (sav->key_auth == NULL ) {
3153 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3160 /* Encryption key */
3161 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3162 const struct sadb_key *key0;
3165 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3166 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3169 if (len < sizeof(*key0)) {
3173 switch (mhp->msg->sadb_msg_satype) {
3174 case SADB_SATYPE_ESP:
3175 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3176 sav->alg_enc != SADB_EALG_NULL) {
3180 sav->key_enc = (struct seckey *)key_dup_keymsg(key0,
3183 if (sav->key_enc == NULL) {
3184 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3190 case SADB_X_SATYPE_IPCOMP:
3191 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3193 sav->key_enc = NULL; /*just in case*/
3195 case SADB_SATYPE_AH:
3196 case SADB_X_SATYPE_TCPSIGNATURE:
3202 ipseclog((LOG_DEBUG, "%s: invalid key_enc value.\n",
3211 switch (mhp->msg->sadb_msg_satype) {
3212 case SADB_SATYPE_AH:
3213 error = xform_init(sav, XF_AH);
3215 case SADB_SATYPE_ESP:
3216 error = xform_init(sav, XF_ESP);
3218 case SADB_X_SATYPE_IPCOMP:
3219 error = xform_init(sav, XF_IPCOMP);
3221 case SADB_X_SATYPE_TCPSIGNATURE:
3222 error = xform_init(sav, XF_TCPSIGNATURE);
3226 ipseclog((LOG_DEBUG, "%s: unable to initialize SA type %u.\n",
3227 __func__, mhp->msg->sadb_msg_satype));
3232 sav->created = time_second;
3234 /* make lifetime for CURRENT */
3235 sav->lft_c = malloc(sizeof(struct seclifetime), M_IPSEC_MISC, M_NOWAIT);
3236 if (sav->lft_c == NULL) {
3237 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3242 sav->lft_c->allocations = 0;
3243 sav->lft_c->bytes = 0;
3244 sav->lft_c->addtime = time_second;
3245 sav->lft_c->usetime = 0;
3247 /* lifetimes for HARD and SOFT */
3249 const struct sadb_lifetime *lft0;
3251 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3253 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3257 sav->lft_h = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3258 if (sav->lft_h == NULL) {
3259 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3263 /* to be initialize ? */
3266 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3268 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3272 sav->lft_s = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3273 if (sav->lft_s == NULL) {
3274 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3278 /* to be initialize ? */
3285 /* initialization */
3292 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3297 key_mature(struct secasvar *sav)
3301 /* check SPI value */
3302 switch (sav->sah->saidx.proto) {
3306 * RFC 4302, 2.4. Security Parameters Index (SPI), SPI values
3307 * 1-255 reserved by IANA for future use,
3308 * 0 for implementation specific, local use.
3310 if (ntohl(sav->spi) <= 255) {
3311 ipseclog((LOG_DEBUG, "%s: illegal range of SPI %u.\n",
3312 __func__, (u_int32_t)ntohl(sav->spi)));
3319 switch (sav->sah->saidx.proto) {
3322 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3323 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3324 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3325 "given to old-esp.\n", __func__));
3328 error = xform_init(sav, XF_ESP);
3332 if (sav->flags & SADB_X_EXT_DERIV) {
3333 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3334 "given to AH SA.\n", __func__));
3337 if (sav->alg_enc != SADB_EALG_NONE) {
3338 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3339 "mismated.\n", __func__));
3342 error = xform_init(sav, XF_AH);
3344 case IPPROTO_IPCOMP:
3345 if (sav->alg_auth != SADB_AALG_NONE) {
3346 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3347 "mismated.\n", __func__));
3350 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3351 && ntohl(sav->spi) >= 0x10000) {
3352 ipseclog((LOG_DEBUG, "%s: invalid cpi for IPComp.\n",
3356 error = xform_init(sav, XF_IPCOMP);
3359 if (sav->alg_enc != SADB_EALG_NONE) {
3360 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3361 "mismated.\n", __func__));
3364 error = xform_init(sav, XF_TCPSIGNATURE);
3367 ipseclog((LOG_DEBUG, "%s: Invalid satype.\n", __func__));
3368 error = EPROTONOSUPPORT;
3373 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3380 * subroutine for SADB_GET and SADB_DUMP.
3382 static struct mbuf *
3383 key_setdumpsa(struct secasvar *sav, u_int8_t type, u_int8_t satype,
3384 u_int32_t seq, u_int32_t pid)
3386 struct mbuf *result = NULL, *tres = NULL, *m;
3389 SADB_EXT_SA, SADB_X_EXT_SA2,
3390 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3391 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3392 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3393 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3394 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3396 SADB_X_EXT_NAT_T_TYPE,
3397 SADB_X_EXT_NAT_T_SPORT, SADB_X_EXT_NAT_T_DPORT,
3398 SADB_X_EXT_NAT_T_OAI, SADB_X_EXT_NAT_T_OAR,
3399 SADB_X_EXT_NAT_T_FRAG,
3403 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3408 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3410 switch (dumporder[i]) {
3412 m = key_setsadbsa(sav);
3417 case SADB_X_EXT_SA2:
3418 m = key_setsadbxsa2(sav->sah->saidx.mode,
3419 sav->replay ? sav->replay->count : 0,
3420 sav->sah->saidx.reqid);
3425 case SADB_EXT_ADDRESS_SRC:
3426 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3427 &sav->sah->saidx.src.sa,
3428 FULLMASK, IPSEC_ULPROTO_ANY);
3433 case SADB_EXT_ADDRESS_DST:
3434 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3435 &sav->sah->saidx.dst.sa,
3436 FULLMASK, IPSEC_ULPROTO_ANY);
3441 case SADB_EXT_KEY_AUTH:
3444 m = key_setkey(sav->key_auth, SADB_EXT_KEY_AUTH);
3449 case SADB_EXT_KEY_ENCRYPT:
3452 m = key_setkey(sav->key_enc, SADB_EXT_KEY_ENCRYPT);
3457 case SADB_EXT_LIFETIME_CURRENT:
3460 m = key_setlifetime(sav->lft_c,
3461 SADB_EXT_LIFETIME_CURRENT);
3466 case SADB_EXT_LIFETIME_HARD:
3469 m = key_setlifetime(sav->lft_h,
3470 SADB_EXT_LIFETIME_HARD);
3475 case SADB_EXT_LIFETIME_SOFT:
3478 m = key_setlifetime(sav->lft_s,
3479 SADB_EXT_LIFETIME_SOFT);
3486 case SADB_X_EXT_NAT_T_TYPE:
3487 m = key_setsadbxtype(sav->natt_type);
3492 case SADB_X_EXT_NAT_T_DPORT:
3493 m = key_setsadbxport(
3494 KEY_PORTFROMSADDR(&sav->sah->saidx.dst),
3495 SADB_X_EXT_NAT_T_DPORT);
3500 case SADB_X_EXT_NAT_T_SPORT:
3501 m = key_setsadbxport(
3502 KEY_PORTFROMSADDR(&sav->sah->saidx.src),
3503 SADB_X_EXT_NAT_T_SPORT);
3508 case SADB_X_EXT_NAT_T_OAI:
3509 case SADB_X_EXT_NAT_T_OAR:
3510 case SADB_X_EXT_NAT_T_FRAG:
3511 /* We do not (yet) support those. */
3515 case SADB_EXT_ADDRESS_PROXY:
3516 case SADB_EXT_IDENTITY_SRC:
3517 case SADB_EXT_IDENTITY_DST:
3518 /* XXX: should we brought from SPD ? */
3519 case SADB_EXT_SENSITIVITY:
3532 m_cat(result, tres);
3533 if (result->m_len < sizeof(struct sadb_msg)) {
3534 result = m_pullup(result, sizeof(struct sadb_msg));
3539 result->m_pkthdr.len = 0;
3540 for (m = result; m; m = m->m_next)
3541 result->m_pkthdr.len += m->m_len;
3543 mtod(result, struct sadb_msg *)->sadb_msg_len =
3544 PFKEY_UNIT64(result->m_pkthdr.len);
3555 * set data into sadb_msg.
3557 static struct mbuf *
3558 key_setsadbmsg(u_int8_t type, u_int16_t tlen, u_int8_t satype, u_int32_t seq,
3559 pid_t pid, u_int16_t reserved)
3565 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3568 MGETHDR(m, M_DONTWAIT, MT_DATA);
3569 if (m && len > MHLEN) {
3570 MCLGET(m, M_DONTWAIT);
3571 if ((m->m_flags & M_EXT) == 0) {
3578 m->m_pkthdr.len = m->m_len = len;
3581 p = mtod(m, struct sadb_msg *);
3584 p->sadb_msg_version = PF_KEY_V2;
3585 p->sadb_msg_type = type;
3586 p->sadb_msg_errno = 0;
3587 p->sadb_msg_satype = satype;
3588 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3589 p->sadb_msg_reserved = reserved;
3590 p->sadb_msg_seq = seq;
3591 p->sadb_msg_pid = (u_int32_t)pid;
3597 * copy secasvar data into sadb_address.
3599 static struct mbuf *
3601 struct secasvar *sav;
3607 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3608 m = key_alloc_mbuf(len);
3609 if (!m || m->m_next) { /*XXX*/
3615 p = mtod(m, struct sadb_sa *);
3618 p->sadb_sa_len = PFKEY_UNIT64(len);
3619 p->sadb_sa_exttype = SADB_EXT_SA;
3620 p->sadb_sa_spi = sav->spi;
3621 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3622 p->sadb_sa_state = sav->state;
3623 p->sadb_sa_auth = sav->alg_auth;
3624 p->sadb_sa_encrypt = sav->alg_enc;
3625 p->sadb_sa_flags = sav->flags;
3631 * set data into sadb_address.
3633 static struct mbuf *
3634 key_setsadbaddr(u_int16_t exttype, const struct sockaddr *saddr, u_int8_t prefixlen, u_int16_t ul_proto)
3637 struct sadb_address *p;
3640 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3641 PFKEY_ALIGN8(saddr->sa_len);
3642 m = key_alloc_mbuf(len);
3643 if (!m || m->m_next) { /*XXX*/
3649 p = mtod(m, struct sadb_address *);
3652 p->sadb_address_len = PFKEY_UNIT64(len);
3653 p->sadb_address_exttype = exttype;
3654 p->sadb_address_proto = ul_proto;
3655 if (prefixlen == FULLMASK) {
3656 switch (saddr->sa_family) {
3658 prefixlen = sizeof(struct in_addr) << 3;
3661 prefixlen = sizeof(struct in6_addr) << 3;
3667 p->sadb_address_prefixlen = prefixlen;
3668 p->sadb_address_reserved = 0;
3671 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3678 * set data into sadb_x_sa2.
3680 static struct mbuf *
3681 key_setsadbxsa2(u_int8_t mode, u_int32_t seq, u_int32_t reqid)
3684 struct sadb_x_sa2 *p;
3687 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3688 m = key_alloc_mbuf(len);
3689 if (!m || m->m_next) { /*XXX*/
3695 p = mtod(m, struct sadb_x_sa2 *);
3698 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3699 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3700 p->sadb_x_sa2_mode = mode;
3701 p->sadb_x_sa2_reserved1 = 0;
3702 p->sadb_x_sa2_reserved2 = 0;
3703 p->sadb_x_sa2_sequence = seq;
3704 p->sadb_x_sa2_reqid = reqid;
3711 * Set a type in sadb_x_nat_t_type.
3713 static struct mbuf *
3714 key_setsadbxtype(u_int16_t type)
3718 struct sadb_x_nat_t_type *p;
3720 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_type));
3722 m = key_alloc_mbuf(len);
3723 if (!m || m->m_next) { /*XXX*/
3729 p = mtod(m, struct sadb_x_nat_t_type *);
3732 p->sadb_x_nat_t_type_len = PFKEY_UNIT64(len);
3733 p->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
3734 p->sadb_x_nat_t_type_type = type;
3739 * Set a port in sadb_x_nat_t_port.
3740 * In contrast to default RFC 2367 behaviour, port is in network byte order.
3742 static struct mbuf *
3743 key_setsadbxport(u_int16_t port, u_int16_t type)
3747 struct sadb_x_nat_t_port *p;
3749 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_port));
3751 m = key_alloc_mbuf(len);
3752 if (!m || m->m_next) { /*XXX*/
3758 p = mtod(m, struct sadb_x_nat_t_port *);
3761 p->sadb_x_nat_t_port_len = PFKEY_UNIT64(len);
3762 p->sadb_x_nat_t_port_exttype = type;
3763 p->sadb_x_nat_t_port_port = port;
3769 * Get port from sockaddr. Port is in network byte order.
3772 key_portfromsaddr(struct sockaddr *sa)
3775 switch (sa->sa_family) {
3778 return ((struct sockaddr_in *)sa)->sin_port;
3782 return ((struct sockaddr_in6 *)sa)->sin6_port;
3785 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
3786 printf("DP %s unexpected address family %d\n",
3787 __func__, sa->sa_family));
3790 #endif /* IPSEC_NAT_T */
3793 * Set port in struct sockaddr. Port is in network byte order.
3796 key_porttosaddr(struct sockaddr *sa, u_int16_t port)
3799 switch (sa->sa_family) {
3802 ((struct sockaddr_in *)sa)->sin_port = port;
3807 ((struct sockaddr_in6 *)sa)->sin6_port = port;
3811 ipseclog((LOG_DEBUG, "%s: unexpected address family %d.\n",
3812 __func__, sa->sa_family));
3818 * set data into sadb_x_policy
3820 static struct mbuf *
3821 key_setsadbxpolicy(u_int16_t type, u_int8_t dir, u_int32_t id)
3824 struct sadb_x_policy *p;
3827 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3828 m = key_alloc_mbuf(len);
3829 if (!m || m->m_next) { /*XXX*/
3835 p = mtod(m, struct sadb_x_policy *);
3838 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3839 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3840 p->sadb_x_policy_type = type;
3841 p->sadb_x_policy_dir = dir;
3842 p->sadb_x_policy_id = id;
3848 /* Take a key message (sadb_key) from the socket and turn it into one
3849 * of the kernel's key structures (seckey).
3851 * IN: pointer to the src
3852 * OUT: NULL no more memory
3855 key_dup_keymsg(const struct sadb_key *src, u_int len,
3856 struct malloc_type *type)
3859 dst = (struct seckey *)malloc(sizeof(struct seckey), type, M_NOWAIT);
3861 dst->bits = src->sadb_key_bits;
3862 dst->key_data = (char *)malloc(len, type, M_NOWAIT);
3863 if (dst->key_data != NULL) {
3864 bcopy((const char *)src + sizeof(struct sadb_key),
3865 dst->key_data, len);
3867 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3873 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3880 /* Take a lifetime message (sadb_lifetime) passed in on a socket and
3881 * turn it into one of the kernel's lifetime structures (seclifetime).
3883 * IN: pointer to the destination, source and malloc type
3884 * OUT: NULL, no more memory
3887 static struct seclifetime *
3888 key_dup_lifemsg(const struct sadb_lifetime *src,
3889 struct malloc_type *type)
3891 struct seclifetime *dst = NULL;
3893 dst = (struct seclifetime *)malloc(sizeof(struct seclifetime),
3897 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3899 dst->allocations = src->sadb_lifetime_allocations;
3900 dst->bytes = src->sadb_lifetime_bytes;
3901 dst->addtime = src->sadb_lifetime_addtime;
3902 dst->usetime = src->sadb_lifetime_usetime;
3907 /* compare my own address
3908 * OUT: 1: true, i.e. my address.
3913 struct sockaddr *sa;
3916 struct sockaddr_in *sin;
3917 struct in_ifaddr *ia;
3920 IPSEC_ASSERT(sa != NULL, ("null sockaddr"));
3922 switch (sa->sa_family) {
3925 sin = (struct sockaddr_in *)sa;
3927 for (ia = V_in_ifaddrhead.tqh_first; ia;
3928 ia = ia->ia_link.tqe_next)
3930 if (sin->sin_family == ia->ia_addr.sin_family &&
3931 sin->sin_len == ia->ia_addr.sin_len &&
3932 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3934 IN_IFADDR_RUNLOCK();
3938 IN_IFADDR_RUNLOCK();
3943 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3952 * compare my own address for IPv6.
3955 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3957 #include <netinet6/in6_var.h>
3961 struct sockaddr_in6 *sin6;
3963 struct in6_ifaddr *ia;
3965 struct in6_multi *in6m;
3969 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
3970 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3971 (struct sockaddr *)&ia->ia_addr, 0) == 0) {
3972 IN6_IFADDR_RUNLOCK();
3979 * XXX why do we care about multlicast here while we don't care
3980 * about IPv4 multicast??
3984 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3986 IN6_IFADDR_RUNLOCK();
3991 IN6_IFADDR_RUNLOCK();
3993 /* loopback, just for safety */
3994 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
4002 * compare two secasindex structure.
4003 * flag can specify to compare 2 saidxes.
4004 * compare two secasindex structure without both mode and reqid.
4005 * don't compare port.
4007 * saidx0: source, it can be in SAD.
4015 const struct secasindex *saidx0,
4016 const struct secasindex *saidx1,
4022 if (saidx0 == NULL && saidx1 == NULL)
4025 if (saidx0 == NULL || saidx1 == NULL)
4028 if (saidx0->proto != saidx1->proto)
4031 if (flag == CMP_EXACTLY) {
4032 if (saidx0->mode != saidx1->mode)
4034 if (saidx0->reqid != saidx1->reqid)
4036 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
4037 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
4041 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
4042 if (flag == CMP_MODE_REQID
4043 ||flag == CMP_REQID) {
4045 * If reqid of SPD is non-zero, unique SA is required.
4046 * The result must be of same reqid in this case.
4048 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
4052 if (flag == CMP_MODE_REQID) {
4053 if (saidx0->mode != IPSEC_MODE_ANY
4054 && saidx0->mode != saidx1->mode)
4060 * If NAT-T is enabled, check ports for tunnel mode.
4061 * Do not check ports if they are set to zero in the SPD.
4062 * Also do not do it for transport mode, as there is no
4063 * port information available in the SP.
4065 if (saidx1->mode == IPSEC_MODE_TUNNEL &&
4066 saidx1->src.sa.sa_family == AF_INET &&
4067 saidx1->dst.sa.sa_family == AF_INET &&
4068 ((const struct sockaddr_in *)(&saidx1->src))->sin_port &&
4069 ((const struct sockaddr_in *)(&saidx1->dst))->sin_port)
4071 #endif /* IPSEC_NAT_T */
4073 if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, chkport) != 0) {
4076 if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, chkport) != 0) {
4085 * compare two secindex structure exactly.
4087 * spidx0: source, it is often in SPD.
4088 * spidx1: object, it is often from PFKEY message.
4094 key_cmpspidx_exactly(
4095 struct secpolicyindex *spidx0,
4096 struct secpolicyindex *spidx1)
4099 if (spidx0 == NULL && spidx1 == NULL)
4102 if (spidx0 == NULL || spidx1 == NULL)
4105 if (spidx0->prefs != spidx1->prefs
4106 || spidx0->prefd != spidx1->prefd
4107 || spidx0->ul_proto != spidx1->ul_proto)
4110 return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
4111 key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
4115 * compare two secindex structure with mask.
4117 * spidx0: source, it is often in SPD.
4118 * spidx1: object, it is often from IP header.
4124 key_cmpspidx_withmask(
4125 struct secpolicyindex *spidx0,
4126 struct secpolicyindex *spidx1)
4129 if (spidx0 == NULL && spidx1 == NULL)
4132 if (spidx0 == NULL || spidx1 == NULL)
4135 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
4136 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
4137 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
4138 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
4141 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4142 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
4143 && spidx0->ul_proto != spidx1->ul_proto)
4146 switch (spidx0->src.sa.sa_family) {
4148 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
4149 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
4151 if (!key_bbcmp(&spidx0->src.sin.sin_addr,
4152 &spidx1->src.sin.sin_addr, spidx0->prefs))
4156 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
4157 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
4160 * scope_id check. if sin6_scope_id is 0, we regard it
4161 * as a wildcard scope, which matches any scope zone ID.
4163 if (spidx0->src.sin6.sin6_scope_id &&
4164 spidx1->src.sin6.sin6_scope_id &&
4165 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
4167 if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
4168 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
4173 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
4178 switch (spidx0->dst.sa.sa_family) {
4180 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
4181 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
4183 if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
4184 &spidx1->dst.sin.sin_addr, spidx0->prefd))
4188 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
4189 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
4192 * scope_id check. if sin6_scope_id is 0, we regard it
4193 * as a wildcard scope, which matches any scope zone ID.
4195 if (spidx0->dst.sin6.sin6_scope_id &&
4196 spidx1->dst.sin6.sin6_scope_id &&
4197 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
4199 if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
4200 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
4205 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
4210 /* XXX Do we check other field ? e.g. flowinfo */
4215 /* returns 0 on match */
4218 const struct sockaddr *sa1,
4219 const struct sockaddr *sa2,
4225 #define satosin(s) ((const struct sockaddr_in *)s)
4229 #define satosin6(s) ((const struct sockaddr_in6 *)s)
4230 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
4233 switch (sa1->sa_family) {
4235 if (sa1->sa_len != sizeof(struct sockaddr_in))
4237 if (satosin(sa1)->sin_addr.s_addr !=
4238 satosin(sa2)->sin_addr.s_addr) {
4241 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
4245 if (sa1->sa_len != sizeof(struct sockaddr_in6))
4246 return 1; /*EINVAL*/
4247 if (satosin6(sa1)->sin6_scope_id !=
4248 satosin6(sa2)->sin6_scope_id) {
4251 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
4252 &satosin6(sa2)->sin6_addr)) {
4256 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
4261 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
4272 * compare two buffers with mask.
4276 * bits: Number of bits to compare
4282 key_bbcmp(const void *a1, const void *a2, u_int bits)
4284 const unsigned char *p1 = a1;
4285 const unsigned char *p2 = a2;
4287 /* XXX: This could be considerably faster if we compare a word
4288 * at a time, but it is complicated on LSB Endian machines */
4290 /* Handle null pointers */
4291 if (p1 == NULL || p2 == NULL)
4301 u_int8_t mask = ~((1<<(8-bits))-1);
4302 if ((*p1 & mask) != (*p2 & mask))
4305 return 1; /* Match! */
4309 key_flush_spd(time_t now)
4311 static u_int16_t sptree_scangen = 0;
4312 u_int16_t gen = sptree_scangen++;
4313 struct secpolicy *sp;
4317 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4320 LIST_FOREACH(sp, &V_sptree[dir], chain) {
4321 if (sp->scangen == gen) /* previously handled */
4324 if (sp->state == IPSEC_SPSTATE_DEAD &&
4327 * Ensure that we only decrease refcnt once,
4328 * when we're the last consumer.
4329 * Directly call SP_DELREF/key_delsp instead
4330 * of KEY_FREESP to avoid unlocking/relocking
4331 * SPTREE_LOCK before key_delsp: may refcnt
4332 * be increased again during that time ?
4333 * NB: also clean entries created by
4341 if (sp->lifetime == 0 && sp->validtime == 0)
4343 if ((sp->lifetime && now - sp->created > sp->lifetime)
4344 || (sp->validtime && now - sp->lastused > sp->validtime)) {
4345 sp->state = IPSEC_SPSTATE_DEAD;
4356 key_flush_sad(time_t now)
4358 struct secashead *sah, *nextsah;
4359 struct secasvar *sav, *nextsav;
4363 LIST_FOREACH_SAFE(sah, &V_sahtree, chain, nextsah) {
4364 /* if sah has been dead, then delete it and process next sah. */
4365 if (sah->state == SADB_SASTATE_DEAD) {
4370 /* if LARVAL entry doesn't become MATURE, delete it. */
4371 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_LARVAL], chain, nextsav) {
4372 /* Need to also check refcnt for a larval SA ??? */
4373 if (now - sav->created > V_key_larval_lifetime)
4378 * check MATURE entry to start to send expire message
4381 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_MATURE], chain, nextsav) {
4382 /* we don't need to check. */
4383 if (sav->lft_s == NULL)
4387 if (sav->lft_c == NULL) {
4388 ipseclog((LOG_DEBUG,"%s: there is no CURRENT "
4389 "time, why?\n", __func__));
4393 /* check SOFT lifetime */
4394 if (sav->lft_s->addtime != 0 &&
4395 now - sav->created > sav->lft_s->addtime) {
4396 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4398 * Actually, only send expire message if
4399 * SA has been used, as it was done before,
4400 * but should we always send such message,
4401 * and let IKE daemon decide if it should be
4402 * renegotiated or not ?
4403 * XXX expire message will actually NOT be
4404 * sent if SA is only used after soft
4405 * lifetime has been reached, see below
4408 if (sav->lft_c->usetime != 0)
4411 /* check SOFT lifetime by bytes */
4413 * XXX I don't know the way to delete this SA
4414 * when new SA is installed. Caution when it's
4415 * installed too big lifetime by time.
4417 else if (sav->lft_s->bytes != 0 &&
4418 sav->lft_s->bytes < sav->lft_c->bytes) {
4420 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4422 * XXX If we keep to send expire
4423 * message in the status of
4424 * DYING. Do remove below code.
4430 /* check DYING entry to change status to DEAD. */
4431 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DYING], chain, nextsav) {
4432 /* we don't need to check. */
4433 if (sav->lft_h == NULL)
4437 if (sav->lft_c == NULL) {
4438 ipseclog((LOG_DEBUG, "%s: there is no CURRENT "
4439 "time, why?\n", __func__));
4443 if (sav->lft_h->addtime != 0 &&
4444 now - sav->created > sav->lft_h->addtime) {
4445 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4448 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4449 else if (sav->lft_s != NULL
4450 && sav->lft_s->addtime != 0
4451 && now - sav->created > sav->lft_s->addtime) {
4453 * XXX: should be checked to be
4454 * installed the valid SA.
4458 * If there is no SA then sending
4464 /* check HARD lifetime by bytes */
4465 else if (sav->lft_h->bytes != 0 &&
4466 sav->lft_h->bytes < sav->lft_c->bytes) {
4467 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4472 /* delete entry in DEAD */
4473 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DEAD], chain, nextsav) {
4475 if (sav->state != SADB_SASTATE_DEAD) {
4476 ipseclog((LOG_DEBUG, "%s: invalid sav->state "
4477 "(queue: %d SA: %d): kill it anyway\n",
4479 SADB_SASTATE_DEAD, sav->state));
4482 * do not call key_freesav() here.
4483 * sav should already be freed, and sav->refcnt
4484 * shows other references to sav
4485 * (such as from SPD).
4493 key_flush_acq(time_t now)
4495 struct secacq *acq, *nextacq;
4499 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
4500 nextacq = LIST_NEXT(acq, chain);
4501 if (now - acq->created > V_key_blockacq_lifetime
4502 && __LIST_CHAINED(acq)) {
4503 LIST_REMOVE(acq, chain);
4504 free(acq, M_IPSEC_SAQ);
4511 key_flush_spacq(time_t now)
4513 struct secspacq *acq, *nextacq;
4517 for (acq = LIST_FIRST(&V_spacqtree); acq != NULL; acq = nextacq) {
4518 nextacq = LIST_NEXT(acq, chain);
4519 if (now - acq->created > V_key_blockacq_lifetime
4520 && __LIST_CHAINED(acq)) {
4521 LIST_REMOVE(acq, chain);
4522 free(acq, M_IPSEC_SAQ);
4530 * scanning SPD and SAD to check status for each entries,
4531 * and do to remove or to expire.
4532 * XXX: year 2038 problem may remain.
4535 key_timehandler(void)
4537 VNET_ITERATOR_DECL(vnet_iter);
4538 time_t now = time_second;
4540 VNET_LIST_RLOCK_NOSLEEP();
4541 VNET_FOREACH(vnet_iter) {
4542 CURVNET_SET(vnet_iter);
4546 key_flush_spacq(now);
4549 VNET_LIST_RUNLOCK_NOSLEEP();
4551 #ifndef IPSEC_DEBUG2
4552 /* do exchange to tick time !! */
4553 (void)timeout((void *)key_timehandler, (void *)0, hz);
4554 #endif /* IPSEC_DEBUG2 */
4562 key_randomfill(&value, sizeof(value));
4567 key_randomfill(p, l)
4573 static int warn = 1;
4576 n = (size_t)read_random(p, (u_int)l);
4580 bcopy(&v, (u_int8_t *)p + n,
4581 l - n < sizeof(v) ? l - n : sizeof(v));
4585 printf("WARNING: pseudo-random number generator "
4586 "used for IPsec processing\n");
4593 * map SADB_SATYPE_* to IPPROTO_*.
4594 * if satype == SADB_SATYPE then satype is mapped to ~0.
4596 * 0: invalid satype.
4599 key_satype2proto(u_int8_t satype)
4602 case SADB_SATYPE_UNSPEC:
4603 return IPSEC_PROTO_ANY;
4604 case SADB_SATYPE_AH:
4606 case SADB_SATYPE_ESP:
4608 case SADB_X_SATYPE_IPCOMP:
4609 return IPPROTO_IPCOMP;
4610 case SADB_X_SATYPE_TCPSIGNATURE:
4619 * map IPPROTO_* to SADB_SATYPE_*
4621 * 0: invalid protocol type.
4624 key_proto2satype(u_int16_t proto)
4628 return SADB_SATYPE_AH;
4630 return SADB_SATYPE_ESP;
4631 case IPPROTO_IPCOMP:
4632 return SADB_X_SATYPE_IPCOMP;
4634 return SADB_X_SATYPE_TCPSIGNATURE;
4643 * SADB_GETSPI processing is to receive
4644 * <base, (SA2), src address, dst address, (SPI range)>
4645 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4646 * tree with the status of LARVAL, and send
4647 * <base, SA(*), address(SD)>
4650 * IN: mhp: pointer to the pointer to each header.
4651 * OUT: NULL if fail.
4652 * other if success, return pointer to the message to send.
4655 key_getspi(so, m, mhp)
4658 const struct sadb_msghdr *mhp;
4660 struct sadb_address *src0, *dst0;
4661 struct secasindex saidx;
4662 struct secashead *newsah;
4663 struct secasvar *newsav;
4670 IPSEC_ASSERT(so != NULL, ("null socket"));
4671 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4672 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4673 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4675 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4676 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4677 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4679 return key_senderror(so, m, EINVAL);
4681 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4682 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4683 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4685 return key_senderror(so, m, EINVAL);
4687 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4688 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4689 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4691 mode = IPSEC_MODE_ANY;
4695 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4696 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4698 /* map satype to proto */
4699 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4700 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4702 return key_senderror(so, m, EINVAL);
4706 * Make sure the port numbers are zero.
4707 * In case of NAT-T we will update them later if needed.
4709 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4711 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4712 sizeof(struct sockaddr_in))
4713 return key_senderror(so, m, EINVAL);
4714 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4717 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4718 sizeof(struct sockaddr_in6))
4719 return key_senderror(so, m, EINVAL);
4720 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4725 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4727 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4728 sizeof(struct sockaddr_in))
4729 return key_senderror(so, m, EINVAL);
4730 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4733 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4734 sizeof(struct sockaddr_in6))
4735 return key_senderror(so, m, EINVAL);
4736 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4742 /* XXX boundary check against sa_len */
4743 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4747 * Handle NAT-T info if present.
4748 * We made sure the port numbers are zero above, so we do
4749 * not have to worry in case we do not update them.
4751 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL)
4752 ipseclog((LOG_DEBUG, "%s: NAT-T OAi present\n", __func__));
4753 if (mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL)
4754 ipseclog((LOG_DEBUG, "%s: NAT-T OAr present\n", __func__));
4756 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
4757 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
4758 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
4759 struct sadb_x_nat_t_type *type;
4760 struct sadb_x_nat_t_port *sport, *dport;
4762 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
4763 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
4764 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
4765 ipseclog((LOG_DEBUG, "%s: invalid nat-t message "
4766 "passed.\n", __func__));
4767 return key_senderror(so, m, EINVAL);
4770 sport = (struct sadb_x_nat_t_port *)
4771 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
4772 dport = (struct sadb_x_nat_t_port *)
4773 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
4776 KEY_PORTTOSADDR(&saidx.src, sport->sadb_x_nat_t_port_port);
4778 KEY_PORTTOSADDR(&saidx.dst, dport->sadb_x_nat_t_port_port);
4782 /* SPI allocation */
4783 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4786 return key_senderror(so, m, EINVAL);
4788 /* get a SA index */
4789 if ((newsah = key_getsah(&saidx)) == NULL) {
4790 /* create a new SA index */
4791 if ((newsah = key_newsah(&saidx)) == NULL) {
4792 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
4793 return key_senderror(so, m, ENOBUFS);
4799 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4800 if (newsav == NULL) {
4801 /* XXX don't free new SA index allocated in above. */
4802 return key_senderror(so, m, error);
4806 newsav->spi = htonl(spi);
4808 /* delete the entry in acqtree */
4809 if (mhp->msg->sadb_msg_seq != 0) {
4811 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4812 /* reset counter in order to deletion by timehandler. */
4813 acq->created = time_second;
4819 struct mbuf *n, *nn;
4820 struct sadb_sa *m_sa;
4821 struct sadb_msg *newmsg;
4824 /* create new sadb_msg to reply. */
4825 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4826 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4828 MGETHDR(n, M_DONTWAIT, MT_DATA);
4830 MCLGET(n, M_DONTWAIT);
4831 if ((n->m_flags & M_EXT) == 0) {
4837 return key_senderror(so, m, ENOBUFS);
4843 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4844 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4846 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4847 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4848 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4849 m_sa->sadb_sa_spi = htonl(spi);
4850 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4852 IPSEC_ASSERT(off == len,
4853 ("length inconsistency (off %u len %u)", off, len));
4855 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4856 SADB_EXT_ADDRESS_DST);
4859 return key_senderror(so, m, ENOBUFS);
4862 if (n->m_len < sizeof(struct sadb_msg)) {
4863 n = m_pullup(n, sizeof(struct sadb_msg));
4865 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4868 n->m_pkthdr.len = 0;
4869 for (nn = n; nn; nn = nn->m_next)
4870 n->m_pkthdr.len += nn->m_len;
4872 newmsg = mtod(n, struct sadb_msg *);
4873 newmsg->sadb_msg_seq = newsav->seq;
4874 newmsg->sadb_msg_errno = 0;
4875 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4878 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4883 * allocating new SPI
4884 * called by key_getspi().
4890 key_do_getnewspi(spirange, saidx)
4891 struct sadb_spirange *spirange;
4892 struct secasindex *saidx;
4896 int count = V_key_spi_trycnt;
4898 /* set spi range to allocate */
4899 if (spirange != NULL) {
4900 min = spirange->sadb_spirange_min;
4901 max = spirange->sadb_spirange_max;
4903 min = V_key_spi_minval;
4904 max = V_key_spi_maxval;
4906 /* IPCOMP needs 2-byte SPI */
4907 if (saidx->proto == IPPROTO_IPCOMP) {
4914 t = min; min = max; max = t;
4919 if (key_checkspidup(saidx, min) != NULL) {
4920 ipseclog((LOG_DEBUG, "%s: SPI %u exists already.\n",
4925 count--; /* taking one cost. */
4933 /* when requesting to allocate spi ranged */
4935 /* generate pseudo-random SPI value ranged. */
4936 newspi = min + (key_random() % (max - min + 1));
4938 if (key_checkspidup(saidx, newspi) == NULL)
4942 if (count == 0 || newspi == 0) {
4943 ipseclog((LOG_DEBUG, "%s: to allocate spi is failed.\n",
4950 keystat.getspi_count =
4951 (keystat.getspi_count + V_key_spi_trycnt - count) / 2;
4957 * SADB_UPDATE processing
4959 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4960 * key(AE), (identity(SD),) (sensitivity)>
4961 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4963 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4964 * (identity(SD),) (sensitivity)>
4967 * m will always be freed.
4970 key_update(so, m, mhp)
4973 const struct sadb_msghdr *mhp;
4975 struct sadb_sa *sa0;
4976 struct sadb_address *src0, *dst0;
4978 struct sadb_x_nat_t_type *type;
4979 struct sadb_x_nat_t_port *sport, *dport;
4980 struct sadb_address *iaddr, *raddr;
4981 struct sadb_x_nat_t_frag *frag;
4983 struct secasindex saidx;
4984 struct secashead *sah;
4985 struct secasvar *sav;
4991 IPSEC_ASSERT(so != NULL, ("null socket"));
4992 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4993 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4994 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4996 /* map satype to proto */
4997 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4998 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5000 return key_senderror(so, m, EINVAL);
5003 if (mhp->ext[SADB_EXT_SA] == NULL ||
5004 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5005 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5006 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5007 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5008 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5009 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5010 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5011 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5012 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5013 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5014 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5016 return key_senderror(so, m, EINVAL);
5018 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5019 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5020 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5021 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5023 return key_senderror(so, m, EINVAL);
5025 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5026 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5027 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5029 mode = IPSEC_MODE_ANY;
5032 /* XXX boundary checking for other extensions */
5034 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5035 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5036 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5038 /* XXX boundary check against sa_len */
5039 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5042 * Make sure the port numbers are zero.
5043 * In case of NAT-T we will update them later if needed.
5045 KEY_PORTTOSADDR(&saidx.src, 0);
5046 KEY_PORTTOSADDR(&saidx.dst, 0);
5050 * Handle NAT-T info if present.
5052 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
5053 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5054 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5056 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
5057 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5058 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5059 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5061 return key_senderror(so, m, EINVAL);
5064 type = (struct sadb_x_nat_t_type *)
5065 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5066 sport = (struct sadb_x_nat_t_port *)
5067 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5068 dport = (struct sadb_x_nat_t_port *)
5069 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5074 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
5075 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
5076 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
5077 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
5078 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5080 return key_senderror(so, m, EINVAL);
5082 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5083 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5084 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
5086 iaddr = raddr = NULL;
5088 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5089 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5090 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5092 return key_senderror(so, m, EINVAL);
5094 frag = (struct sadb_x_nat_t_frag *)
5095 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5101 /* get a SA header */
5102 if ((sah = key_getsah(&saidx)) == NULL) {
5103 ipseclog((LOG_DEBUG, "%s: no SA index found.\n", __func__));
5104 return key_senderror(so, m, ENOENT);
5107 /* set spidx if there */
5109 error = key_setident(sah, m, mhp);
5111 return key_senderror(so, m, error);
5113 /* find a SA with sequence number. */
5114 #ifdef IPSEC_DOSEQCHECK
5115 if (mhp->msg->sadb_msg_seq != 0
5116 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
5117 ipseclog((LOG_DEBUG, "%s: no larval SA with sequence %u "
5118 "exists.\n", __func__, mhp->msg->sadb_msg_seq));
5119 return key_senderror(so, m, ENOENT);
5123 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5126 ipseclog((LOG_DEBUG, "%s: no such a SA found (spi:%u)\n",
5127 __func__, (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5128 return key_senderror(so, m, EINVAL);
5132 /* validity check */
5133 if (sav->sah->saidx.proto != proto) {
5134 ipseclog((LOG_DEBUG, "%s: protocol mismatched "
5135 "(DB=%u param=%u)\n", __func__,
5136 sav->sah->saidx.proto, proto));
5137 return key_senderror(so, m, EINVAL);
5139 #ifdef IPSEC_DOSEQCHECK
5140 if (sav->spi != sa0->sadb_sa_spi) {
5141 ipseclog((LOG_DEBUG, "%s: SPI mismatched (DB:%u param:%u)\n",
5143 (u_int32_t)ntohl(sav->spi),
5144 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5145 return key_senderror(so, m, EINVAL);
5148 if (sav->pid != mhp->msg->sadb_msg_pid) {
5149 ipseclog((LOG_DEBUG, "%s: pid mismatched (DB:%u param:%u)\n",
5150 __func__, sav->pid, mhp->msg->sadb_msg_pid));
5151 return key_senderror(so, m, EINVAL);
5154 /* copy sav values */
5155 error = key_setsaval(sav, m, mhp);
5158 return key_senderror(so, m, error);
5163 * Handle more NAT-T info if present,
5164 * now that we have a sav to fill.
5167 sav->natt_type = type->sadb_x_nat_t_type_type;
5170 KEY_PORTTOSADDR(&sav->sah->saidx.src,
5171 sport->sadb_x_nat_t_port_port);
5173 KEY_PORTTOSADDR(&sav->sah->saidx.dst,
5174 dport->sadb_x_nat_t_port_port);
5178 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5179 * We should actually check for a minimum MTU here, if we
5180 * want to support it in ip_output.
5183 sav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5187 /* check SA values to be mature. */
5188 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
5190 return key_senderror(so, m, 0);
5196 /* set msg buf from mhp */
5197 n = key_getmsgbuf_x1(m, mhp);
5199 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5200 return key_senderror(so, m, ENOBUFS);
5204 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5209 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
5210 * only called by key_update().
5213 * others : found, pointer to a SA.
5215 #ifdef IPSEC_DOSEQCHECK
5216 static struct secasvar *
5217 key_getsavbyseq(sah, seq)
5218 struct secashead *sah;
5221 struct secasvar *sav;
5224 state = SADB_SASTATE_LARVAL;
5226 /* search SAD with sequence number ? */
5227 LIST_FOREACH(sav, &sah->savtree[state], chain) {
5229 KEY_CHKSASTATE(state, sav->state, __func__);
5231 if (sav->seq == seq) {
5233 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
5234 printf("DP %s cause refcnt++:%d SA:%p\n",
5235 __func__, sav->refcnt, sav));
5245 * SADB_ADD processing
5246 * add an entry to SA database, when received
5247 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5248 * key(AE), (identity(SD),) (sensitivity)>
5251 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5252 * (identity(SD),) (sensitivity)>
5255 * IGNORE identity and sensitivity messages.
5257 * m will always be freed.
5263 const struct sadb_msghdr *mhp;
5265 struct sadb_sa *sa0;
5266 struct sadb_address *src0, *dst0;
5268 struct sadb_x_nat_t_type *type;
5269 struct sadb_address *iaddr, *raddr;
5270 struct sadb_x_nat_t_frag *frag;
5272 struct secasindex saidx;
5273 struct secashead *newsah;
5274 struct secasvar *newsav;
5280 IPSEC_ASSERT(so != NULL, ("null socket"));
5281 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5282 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5283 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5285 /* map satype to proto */
5286 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5287 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5289 return key_senderror(so, m, EINVAL);
5292 if (mhp->ext[SADB_EXT_SA] == NULL ||
5293 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5294 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5295 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5296 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5297 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5298 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5299 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5300 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5301 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5302 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5303 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5305 return key_senderror(so, m, EINVAL);
5307 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5308 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5309 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5311 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5313 return key_senderror(so, m, EINVAL);
5315 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5316 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5317 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5319 mode = IPSEC_MODE_ANY;
5323 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5324 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5325 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5327 /* XXX boundary check against sa_len */
5328 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5331 * Make sure the port numbers are zero.
5332 * In case of NAT-T we will update them later if needed.
5334 KEY_PORTTOSADDR(&saidx.src, 0);
5335 KEY_PORTTOSADDR(&saidx.dst, 0);
5339 * Handle NAT-T info if present.
5341 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
5342 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5343 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5344 struct sadb_x_nat_t_port *sport, *dport;
5346 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
5347 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5348 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5349 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5351 return key_senderror(so, m, EINVAL);
5354 type = (struct sadb_x_nat_t_type *)
5355 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5356 sport = (struct sadb_x_nat_t_port *)
5357 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5358 dport = (struct sadb_x_nat_t_port *)
5359 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5362 KEY_PORTTOSADDR(&saidx.src,
5363 sport->sadb_x_nat_t_port_port);
5365 KEY_PORTTOSADDR(&saidx.dst,
5366 dport->sadb_x_nat_t_port_port);
5370 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
5371 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
5372 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
5373 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
5374 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5376 return key_senderror(so, m, EINVAL);
5378 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5379 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5380 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
5382 iaddr = raddr = NULL;
5384 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5385 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5386 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5388 return key_senderror(so, m, EINVAL);
5390 frag = (struct sadb_x_nat_t_frag *)
5391 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5397 /* get a SA header */
5398 if ((newsah = key_getsah(&saidx)) == NULL) {
5399 /* create a new SA header */
5400 if ((newsah = key_newsah(&saidx)) == NULL) {
5401 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
5402 return key_senderror(so, m, ENOBUFS);
5406 /* set spidx if there */
5408 error = key_setident(newsah, m, mhp);
5410 return key_senderror(so, m, error);
5413 /* create new SA entry. */
5414 /* We can create new SA only if SPI is differenct. */
5416 newsav = key_getsavbyspi(newsah, sa0->sadb_sa_spi);
5418 if (newsav != NULL) {
5419 ipseclog((LOG_DEBUG, "%s: SA already exists.\n", __func__));
5420 return key_senderror(so, m, EEXIST);
5422 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
5423 if (newsav == NULL) {
5424 return key_senderror(so, m, error);
5429 * Handle more NAT-T info if present,
5430 * now that we have a sav to fill.
5433 newsav->natt_type = type->sadb_x_nat_t_type_type;
5437 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5438 * We should actually check for a minimum MTU here, if we
5439 * want to support it in ip_output.
5442 newsav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5446 /* check SA values to be mature. */
5447 if ((error = key_mature(newsav)) != 0) {
5448 KEY_FREESAV(&newsav);
5449 return key_senderror(so, m, error);
5453 * don't call key_freesav() here, as we would like to keep the SA
5454 * in the database on success.
5460 /* set msg buf from mhp */
5461 n = key_getmsgbuf_x1(m, mhp);
5463 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5464 return key_senderror(so, m, ENOBUFS);
5468 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5474 key_setident(sah, m, mhp)
5475 struct secashead *sah;
5477 const struct sadb_msghdr *mhp;
5479 const struct sadb_ident *idsrc, *iddst;
5480 int idsrclen, iddstlen;
5482 IPSEC_ASSERT(sah != NULL, ("null secashead"));
5483 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5484 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5485 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5487 /* don't make buffer if not there */
5488 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
5489 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5495 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
5496 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5497 ipseclog((LOG_DEBUG, "%s: invalid identity.\n", __func__));
5501 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
5502 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
5503 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
5504 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
5506 /* validity check */
5507 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
5508 ipseclog((LOG_DEBUG, "%s: ident type mismatch.\n", __func__));
5512 switch (idsrc->sadb_ident_type) {
5513 case SADB_IDENTTYPE_PREFIX:
5514 case SADB_IDENTTYPE_FQDN:
5515 case SADB_IDENTTYPE_USERFQDN:
5517 /* XXX do nothing */
5523 /* make structure */
5524 sah->idents = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5525 if (sah->idents == NULL) {
5526 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5529 sah->identd = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5530 if (sah->identd == NULL) {
5531 free(sah->idents, M_IPSEC_MISC);
5533 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5536 sah->idents->type = idsrc->sadb_ident_type;
5537 sah->idents->id = idsrc->sadb_ident_id;
5539 sah->identd->type = iddst->sadb_ident_type;
5540 sah->identd->id = iddst->sadb_ident_id;
5546 * m will not be freed on return.
5547 * it is caller's responsibility to free the result.
5549 static struct mbuf *
5550 key_getmsgbuf_x1(m, mhp)
5552 const struct sadb_msghdr *mhp;
5556 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5557 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5558 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5560 /* create new sadb_msg to reply. */
5561 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5562 SADB_EXT_SA, SADB_X_EXT_SA2,
5563 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5564 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5565 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5569 if (n->m_len < sizeof(struct sadb_msg)) {
5570 n = m_pullup(n, sizeof(struct sadb_msg));
5574 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5575 mtod(n, struct sadb_msg *)->sadb_msg_len =
5576 PFKEY_UNIT64(n->m_pkthdr.len);
5581 static int key_delete_all __P((struct socket *, struct mbuf *,
5582 const struct sadb_msghdr *, u_int16_t));
5585 * SADB_DELETE processing
5587 * <base, SA(*), address(SD)>
5588 * from the ikmpd, and set SADB_SASTATE_DEAD,
5590 * <base, SA(*), address(SD)>
5593 * m will always be freed.
5596 key_delete(so, m, mhp)
5599 const struct sadb_msghdr *mhp;
5601 struct sadb_sa *sa0;
5602 struct sadb_address *src0, *dst0;
5603 struct secasindex saidx;
5604 struct secashead *sah;
5605 struct secasvar *sav = NULL;
5608 IPSEC_ASSERT(so != NULL, ("null socket"));
5609 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5610 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5611 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5613 /* map satype to proto */
5614 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5615 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5617 return key_senderror(so, m, EINVAL);
5620 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5621 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5622 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5624 return key_senderror(so, m, EINVAL);
5627 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5628 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5629 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5631 return key_senderror(so, m, EINVAL);
5634 if (mhp->ext[SADB_EXT_SA] == NULL) {
5636 * Caller wants us to delete all non-LARVAL SAs
5637 * that match the src/dst. This is used during
5638 * IKE INITIAL-CONTACT.
5640 ipseclog((LOG_DEBUG, "%s: doing delete all.\n", __func__));
5641 return key_delete_all(so, m, mhp, proto);
5642 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5643 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5645 return key_senderror(so, m, EINVAL);
5648 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5649 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5650 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5652 /* XXX boundary check against sa_len */
5653 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5656 * Make sure the port numbers are zero.
5657 * In case of NAT-T we will update them later if needed.
5659 KEY_PORTTOSADDR(&saidx.src, 0);
5660 KEY_PORTTOSADDR(&saidx.dst, 0);
5664 * Handle NAT-T info if present.
5666 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5667 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5668 struct sadb_x_nat_t_port *sport, *dport;
5670 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5671 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5672 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5674 return key_senderror(so, m, EINVAL);
5677 sport = (struct sadb_x_nat_t_port *)
5678 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5679 dport = (struct sadb_x_nat_t_port *)
5680 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5683 KEY_PORTTOSADDR(&saidx.src,
5684 sport->sadb_x_nat_t_port_port);
5686 KEY_PORTTOSADDR(&saidx.dst,
5687 dport->sadb_x_nat_t_port_port);
5691 /* get a SA header */
5693 LIST_FOREACH(sah, &V_sahtree, chain) {
5694 if (sah->state == SADB_SASTATE_DEAD)
5696 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5699 /* get a SA with SPI. */
5700 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5706 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5707 return key_senderror(so, m, ENOENT);
5710 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5716 struct sadb_msg *newmsg;
5718 /* create new sadb_msg to reply. */
5719 /* XXX-BZ NAT-T extensions? */
5720 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5721 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5723 return key_senderror(so, m, ENOBUFS);
5725 if (n->m_len < sizeof(struct sadb_msg)) {
5726 n = m_pullup(n, sizeof(struct sadb_msg));
5728 return key_senderror(so, m, ENOBUFS);
5730 newmsg = mtod(n, struct sadb_msg *);
5731 newmsg->sadb_msg_errno = 0;
5732 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5735 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5740 * delete all SAs for src/dst. Called from key_delete().
5743 key_delete_all(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp,
5746 struct sadb_address *src0, *dst0;
5747 struct secasindex saidx;
5748 struct secashead *sah;
5749 struct secasvar *sav, *nextsav;
5750 u_int stateidx, state;
5752 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5753 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5755 /* XXX boundary check against sa_len */
5756 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5759 * Make sure the port numbers are zero.
5760 * In case of NAT-T we will update them later if needed.
5762 KEY_PORTTOSADDR(&saidx.src, 0);
5763 KEY_PORTTOSADDR(&saidx.dst, 0);
5767 * Handle NAT-T info if present.
5770 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5771 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5772 struct sadb_x_nat_t_port *sport, *dport;
5774 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5775 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5776 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5778 return key_senderror(so, m, EINVAL);
5781 sport = (struct sadb_x_nat_t_port *)
5782 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5783 dport = (struct sadb_x_nat_t_port *)
5784 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5787 KEY_PORTTOSADDR(&saidx.src,
5788 sport->sadb_x_nat_t_port_port);
5790 KEY_PORTTOSADDR(&saidx.dst,
5791 dport->sadb_x_nat_t_port_port);
5796 LIST_FOREACH(sah, &V_sahtree, chain) {
5797 if (sah->state == SADB_SASTATE_DEAD)
5799 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5802 /* Delete all non-LARVAL SAs. */
5804 stateidx < _ARRAYLEN(saorder_state_alive);
5806 state = saorder_state_alive[stateidx];
5807 if (state == SADB_SASTATE_LARVAL)
5809 for (sav = LIST_FIRST(&sah->savtree[state]);
5810 sav != NULL; sav = nextsav) {
5811 nextsav = LIST_NEXT(sav, chain);
5813 if (sav->state != state) {
5814 ipseclog((LOG_DEBUG, "%s: invalid "
5815 "sav->state (queue %d SA %d)\n",
5816 __func__, state, sav->state));
5820 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5828 struct sadb_msg *newmsg;
5830 /* create new sadb_msg to reply. */
5831 /* XXX-BZ NAT-T extensions? */
5832 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5833 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5835 return key_senderror(so, m, ENOBUFS);
5837 if (n->m_len < sizeof(struct sadb_msg)) {
5838 n = m_pullup(n, sizeof(struct sadb_msg));
5840 return key_senderror(so, m, ENOBUFS);
5842 newmsg = mtod(n, struct sadb_msg *);
5843 newmsg->sadb_msg_errno = 0;
5844 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5847 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5852 * SADB_GET processing
5854 * <base, SA(*), address(SD)>
5855 * from the ikmpd, and get a SP and a SA to respond,
5857 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5858 * (identity(SD),) (sensitivity)>
5861 * m will always be freed.
5867 const struct sadb_msghdr *mhp;
5869 struct sadb_sa *sa0;
5870 struct sadb_address *src0, *dst0;
5871 struct secasindex saidx;
5872 struct secashead *sah;
5873 struct secasvar *sav = NULL;
5876 IPSEC_ASSERT(so != NULL, ("null socket"));
5877 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5878 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5879 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5881 /* map satype to proto */
5882 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5883 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5885 return key_senderror(so, m, EINVAL);
5888 if (mhp->ext[SADB_EXT_SA] == NULL ||
5889 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5890 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5891 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5893 return key_senderror(so, m, EINVAL);
5895 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5896 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5897 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5898 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5900 return key_senderror(so, m, EINVAL);
5903 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5904 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5905 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5907 /* XXX boundary check against sa_len */
5908 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5911 * Make sure the port numbers are zero.
5912 * In case of NAT-T we will update them later if needed.
5914 KEY_PORTTOSADDR(&saidx.src, 0);
5915 KEY_PORTTOSADDR(&saidx.dst, 0);
5919 * Handle NAT-T info if present.
5922 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5923 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5924 struct sadb_x_nat_t_port *sport, *dport;
5926 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5927 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5928 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5930 return key_senderror(so, m, EINVAL);
5933 sport = (struct sadb_x_nat_t_port *)
5934 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5935 dport = (struct sadb_x_nat_t_port *)
5936 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5939 KEY_PORTTOSADDR(&saidx.src,
5940 sport->sadb_x_nat_t_port_port);
5942 KEY_PORTTOSADDR(&saidx.dst,
5943 dport->sadb_x_nat_t_port_port);
5947 /* get a SA header */
5949 LIST_FOREACH(sah, &V_sahtree, chain) {
5950 if (sah->state == SADB_SASTATE_DEAD)
5952 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5955 /* get a SA with SPI. */
5956 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5962 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5963 return key_senderror(so, m, ENOENT);
5970 /* map proto to satype */
5971 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5972 ipseclog((LOG_DEBUG, "%s: there was invalid proto in SAD.\n",
5974 return key_senderror(so, m, EINVAL);
5977 /* create new sadb_msg to reply. */
5978 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5979 mhp->msg->sadb_msg_pid);
5981 return key_senderror(so, m, ENOBUFS);
5984 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5988 /* XXX make it sysctl-configurable? */
5990 key_getcomb_setlifetime(comb)
5991 struct sadb_comb *comb;
5994 comb->sadb_comb_soft_allocations = 1;
5995 comb->sadb_comb_hard_allocations = 1;
5996 comb->sadb_comb_soft_bytes = 0;
5997 comb->sadb_comb_hard_bytes = 0;
5998 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5999 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
6000 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
6001 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
6005 * XXX reorder combinations by preference
6006 * XXX no idea if the user wants ESP authentication or not
6008 static struct mbuf *
6011 struct sadb_comb *comb;
6012 struct enc_xform *algo;
6013 struct mbuf *result = NULL, *m, *n;
6017 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6020 for (i = 1; i <= SADB_EALG_MAX; i++) {
6021 algo = esp_algorithm_lookup(i);
6025 /* discard algorithms with key size smaller than system min */
6026 if (_BITS(algo->maxkey) < V_ipsec_esp_keymin)
6028 if (_BITS(algo->minkey) < V_ipsec_esp_keymin)
6029 encmin = V_ipsec_esp_keymin;
6031 encmin = _BITS(algo->minkey);
6033 if (V_ipsec_esp_auth)
6034 m = key_getcomb_ah();
6036 IPSEC_ASSERT(l <= MLEN,
6037 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6038 MGET(m, M_DONTWAIT, MT_DATA);
6043 bzero(mtod(m, caddr_t), m->m_len);
6050 for (n = m; n; n = n->m_next)
6052 IPSEC_ASSERT((totlen % l) == 0, ("totlen=%u, l=%u", totlen, l));
6054 for (off = 0; off < totlen; off += l) {
6055 n = m_pulldown(m, off, l, &o);
6057 /* m is already freed */
6060 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
6061 bzero(comb, sizeof(*comb));
6062 key_getcomb_setlifetime(comb);
6063 comb->sadb_comb_encrypt = i;
6064 comb->sadb_comb_encrypt_minbits = encmin;
6065 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
6084 const struct auth_hash *ah,
6090 *min = *max = ah->keysize;
6091 if (ah->keysize == 0) {
6093 * Transform takes arbitrary key size but algorithm
6094 * key size is restricted. Enforce this here.
6097 case SADB_X_AALG_MD5: *min = *max = 16; break;
6098 case SADB_X_AALG_SHA: *min = *max = 20; break;
6099 case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
6101 DPRINTF(("%s: unknown AH algorithm %u\n",
6109 * XXX reorder combinations by preference
6111 static struct mbuf *
6114 struct sadb_comb *comb;
6115 struct auth_hash *algo;
6117 u_int16_t minkeysize, maxkeysize;
6119 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6122 for (i = 1; i <= SADB_AALG_MAX; i++) {
6124 /* we prefer HMAC algorithms, not old algorithms */
6125 if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC)
6128 algo = ah_algorithm_lookup(i);
6131 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
6132 /* discard algorithms with key size smaller than system min */
6133 if (_BITS(minkeysize) < V_ipsec_ah_keymin)
6137 IPSEC_ASSERT(l <= MLEN,
6138 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6139 MGET(m, M_DONTWAIT, MT_DATA);
6146 M_PREPEND(m, l, M_DONTWAIT);
6150 comb = mtod(m, struct sadb_comb *);
6151 bzero(comb, sizeof(*comb));
6152 key_getcomb_setlifetime(comb);
6153 comb->sadb_comb_auth = i;
6154 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
6155 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
6162 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
6163 * XXX reorder combinations by preference
6165 static struct mbuf *
6166 key_getcomb_ipcomp()
6168 struct sadb_comb *comb;
6169 struct comp_algo *algo;
6172 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6175 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
6176 algo = ipcomp_algorithm_lookup(i);
6181 IPSEC_ASSERT(l <= MLEN,
6182 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6183 MGET(m, M_DONTWAIT, MT_DATA);
6190 M_PREPEND(m, l, M_DONTWAIT);
6194 comb = mtod(m, struct sadb_comb *);
6195 bzero(comb, sizeof(*comb));
6196 key_getcomb_setlifetime(comb);
6197 comb->sadb_comb_encrypt = i;
6198 /* what should we set into sadb_comb_*_{min,max}bits? */
6205 * XXX no way to pass mode (transport/tunnel) to userland
6206 * XXX replay checking?
6207 * XXX sysctl interface to ipsec_{ah,esp}_keymin
6209 static struct mbuf *
6211 const struct secasindex *saidx;
6213 struct sadb_prop *prop;
6215 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
6218 switch (saidx->proto) {
6220 m = key_getcomb_esp();
6223 m = key_getcomb_ah();
6225 case IPPROTO_IPCOMP:
6226 m = key_getcomb_ipcomp();
6234 M_PREPEND(m, l, M_DONTWAIT);
6239 for (n = m; n; n = n->m_next)
6242 prop = mtod(m, struct sadb_prop *);
6243 bzero(prop, sizeof(*prop));
6244 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
6245 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
6246 prop->sadb_prop_replay = 32; /* XXX */
6252 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
6254 * <base, SA, address(SD), (address(P)), x_policy,
6255 * (identity(SD),) (sensitivity,) proposal>
6256 * to KMD, and expect to receive
6257 * <base> with SADB_ACQUIRE if error occured,
6259 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
6260 * from KMD by PF_KEY.
6262 * XXX x_policy is outside of RFC2367 (KAME extension).
6263 * XXX sensitivity is not supported.
6264 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
6265 * see comment for key_getcomb_ipcomp().
6269 * others: error number
6272 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
6274 struct mbuf *result = NULL, *m;
6275 struct secacq *newacq;
6280 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
6281 satype = key_proto2satype(saidx->proto);
6282 IPSEC_ASSERT(satype != 0, ("null satype, protocol %u", saidx->proto));
6285 * We never do anything about acquirng SA. There is anather
6286 * solution that kernel blocks to send SADB_ACQUIRE message until
6287 * getting something message from IKEd. In later case, to be
6288 * managed with ACQUIRING list.
6290 /* Get an entry to check whether sending message or not. */
6291 if ((newacq = key_getacq(saidx)) != NULL) {
6292 if (V_key_blockacq_count < newacq->count) {
6293 /* reset counter and do send message. */
6296 /* increment counter and do nothing. */
6301 /* make new entry for blocking to send SADB_ACQUIRE. */
6302 if ((newacq = key_newacq(saidx)) == NULL)
6308 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
6316 * No SADB_X_EXT_NAT_T_* here: we do not know
6317 * anything related to NAT-T at this time.
6320 /* set sadb_address for saidx's. */
6321 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6322 &saidx->src.sa, FULLMASK, IPSEC_ULPROTO_ANY);
6329 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6330 &saidx->dst.sa, FULLMASK, IPSEC_ULPROTO_ANY);
6337 /* XXX proxy address (optional) */
6339 /* set sadb_x_policy */
6341 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
6349 /* XXX identity (optional) */
6351 if (idexttype && fqdn) {
6352 /* create identity extension (FQDN) */
6353 struct sadb_ident *id;
6356 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
6357 id = (struct sadb_ident *)p;
6358 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6359 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6360 id->sadb_ident_exttype = idexttype;
6361 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
6362 bcopy(fqdn, id + 1, fqdnlen);
6363 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
6367 /* create identity extension (USERFQDN) */
6368 struct sadb_ident *id;
6372 /* +1 for terminating-NUL */
6373 userfqdnlen = strlen(userfqdn) + 1;
6376 id = (struct sadb_ident *)p;
6377 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6378 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6379 id->sadb_ident_exttype = idexttype;
6380 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
6381 /* XXX is it correct? */
6382 if (curproc && curproc->p_cred)
6383 id->sadb_ident_id = curproc->p_cred->p_ruid;
6384 if (userfqdn && userfqdnlen)
6385 bcopy(userfqdn, id + 1, userfqdnlen);
6386 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
6390 /* XXX sensitivity (optional) */
6392 /* create proposal/combination extension */
6393 m = key_getprop(saidx);
6396 * spec conformant: always attach proposal/combination extension,
6397 * the problem is that we have no way to attach it for ipcomp,
6398 * due to the way sadb_comb is declared in RFC2367.
6407 * outside of spec; make proposal/combination extension optional.
6413 if ((result->m_flags & M_PKTHDR) == 0) {
6418 if (result->m_len < sizeof(struct sadb_msg)) {
6419 result = m_pullup(result, sizeof(struct sadb_msg));
6420 if (result == NULL) {
6426 result->m_pkthdr.len = 0;
6427 for (m = result; m; m = m->m_next)
6428 result->m_pkthdr.len += m->m_len;
6430 mtod(result, struct sadb_msg *)->sadb_msg_len =
6431 PFKEY_UNIT64(result->m_pkthdr.len);
6433 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6441 static struct secacq *
6442 key_newacq(const struct secasindex *saidx)
6444 struct secacq *newacq;
6447 newacq = malloc(sizeof(struct secacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6448 if (newacq == NULL) {
6449 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6454 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
6455 newacq->seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq);
6456 newacq->created = time_second;
6459 /* add to acqtree */
6461 LIST_INSERT_HEAD(&V_acqtree, newacq, chain);
6467 static struct secacq *
6468 key_getacq(const struct secasindex *saidx)
6473 LIST_FOREACH(acq, &V_acqtree, chain) {
6474 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
6482 static struct secacq *
6483 key_getacqbyseq(seq)
6489 LIST_FOREACH(acq, &V_acqtree, chain) {
6490 if (acq->seq == seq)
6498 static struct secspacq *
6500 struct secpolicyindex *spidx;
6502 struct secspacq *acq;
6505 acq = malloc(sizeof(struct secspacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6507 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6512 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
6513 acq->created = time_second;
6516 /* add to spacqtree */
6518 LIST_INSERT_HEAD(&V_spacqtree, acq, chain);
6524 static struct secspacq *
6526 struct secpolicyindex *spidx;
6528 struct secspacq *acq;
6531 LIST_FOREACH(acq, &V_spacqtree, chain) {
6532 if (key_cmpspidx_exactly(spidx, &acq->spidx)) {
6533 /* NB: return holding spacq_lock */
6543 * SADB_ACQUIRE processing,
6544 * in first situation, is receiving
6546 * from the ikmpd, and clear sequence of its secasvar entry.
6548 * In second situation, is receiving
6549 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6550 * from a user land process, and return
6551 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6554 * m will always be freed.
6557 key_acquire2(so, m, mhp)
6560 const struct sadb_msghdr *mhp;
6562 const struct sadb_address *src0, *dst0;
6563 struct secasindex saidx;
6564 struct secashead *sah;
6568 IPSEC_ASSERT(so != NULL, ("null socket"));
6569 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6570 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6571 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6574 * Error message from KMd.
6575 * We assume that if error was occured in IKEd, the length of PFKEY
6576 * message is equal to the size of sadb_msg structure.
6577 * We do not raise error even if error occured in this function.
6579 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
6582 /* check sequence number */
6583 if (mhp->msg->sadb_msg_seq == 0) {
6584 ipseclog((LOG_DEBUG, "%s: must specify sequence "
6585 "number.\n", __func__));
6590 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
6592 * the specified larval SA is already gone, or we got
6593 * a bogus sequence number. we can silently ignore it.
6599 /* reset acq counter in order to deletion by timehander. */
6600 acq->created = time_second;
6607 * This message is from user land.
6610 /* map satype to proto */
6611 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6612 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6614 return key_senderror(so, m, EINVAL);
6617 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
6618 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
6619 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
6621 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6623 return key_senderror(so, m, EINVAL);
6625 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
6626 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
6627 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
6629 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6631 return key_senderror(so, m, EINVAL);
6634 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
6635 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
6637 /* XXX boundary check against sa_len */
6638 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
6641 * Make sure the port numbers are zero.
6642 * In case of NAT-T we will update them later if needed.
6644 KEY_PORTTOSADDR(&saidx.src, 0);
6645 KEY_PORTTOSADDR(&saidx.dst, 0);
6649 * Handle NAT-T info if present.
6652 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
6653 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
6654 struct sadb_x_nat_t_port *sport, *dport;
6656 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
6657 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
6658 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
6660 return key_senderror(so, m, EINVAL);
6663 sport = (struct sadb_x_nat_t_port *)
6664 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
6665 dport = (struct sadb_x_nat_t_port *)
6666 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
6669 KEY_PORTTOSADDR(&saidx.src,
6670 sport->sadb_x_nat_t_port_port);
6672 KEY_PORTTOSADDR(&saidx.dst,
6673 dport->sadb_x_nat_t_port_port);
6677 /* get a SA index */
6679 LIST_FOREACH(sah, &V_sahtree, chain) {
6680 if (sah->state == SADB_SASTATE_DEAD)
6682 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
6687 ipseclog((LOG_DEBUG, "%s: a SA exists already.\n", __func__));
6688 return key_senderror(so, m, EEXIST);
6691 error = key_acquire(&saidx, NULL);
6693 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
6694 __func__, mhp->msg->sadb_msg_errno));
6695 return key_senderror(so, m, error);
6698 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6702 * SADB_REGISTER processing.
6703 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6706 * from the ikmpd, and register a socket to send PF_KEY messages,
6710 * If socket is detached, must free from regnode.
6712 * m will always be freed.
6715 key_register(so, m, mhp)
6718 const struct sadb_msghdr *mhp;
6720 struct secreg *reg, *newreg = 0;
6722 IPSEC_ASSERT(so != NULL, ("null socket"));
6723 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6724 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6725 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6727 /* check for invalid register message */
6728 if (mhp->msg->sadb_msg_satype >= sizeof(V_regtree)/sizeof(V_regtree[0]))
6729 return key_senderror(so, m, EINVAL);
6731 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6732 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6735 /* check whether existing or not */
6737 LIST_FOREACH(reg, &V_regtree[mhp->msg->sadb_msg_satype], chain) {
6738 if (reg->so == so) {
6740 ipseclog((LOG_DEBUG, "%s: socket exists already.\n",
6742 return key_senderror(so, m, EEXIST);
6746 /* create regnode */
6747 newreg = malloc(sizeof(struct secreg), M_IPSEC_SAR, M_NOWAIT|M_ZERO);
6748 if (newreg == NULL) {
6750 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6751 return key_senderror(so, m, ENOBUFS);
6755 ((struct keycb *)sotorawcb(so))->kp_registered++;
6757 /* add regnode to regtree. */
6758 LIST_INSERT_HEAD(&V_regtree[mhp->msg->sadb_msg_satype], newreg, chain);
6764 struct sadb_msg *newmsg;
6765 struct sadb_supported *sup;
6766 u_int len, alen, elen;
6769 struct sadb_alg *alg;
6771 /* create new sadb_msg to reply. */
6773 for (i = 1; i <= SADB_AALG_MAX; i++) {
6774 if (ah_algorithm_lookup(i))
6775 alen += sizeof(struct sadb_alg);
6778 alen += sizeof(struct sadb_supported);
6780 for (i = 1; i <= SADB_EALG_MAX; i++) {
6781 if (esp_algorithm_lookup(i))
6782 elen += sizeof(struct sadb_alg);
6785 elen += sizeof(struct sadb_supported);
6787 len = sizeof(struct sadb_msg) + alen + elen;
6790 return key_senderror(so, m, ENOBUFS);
6792 MGETHDR(n, M_DONTWAIT, MT_DATA);
6794 MCLGET(n, M_DONTWAIT);
6795 if ((n->m_flags & M_EXT) == 0) {
6801 return key_senderror(so, m, ENOBUFS);
6803 n->m_pkthdr.len = n->m_len = len;
6807 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6808 newmsg = mtod(n, struct sadb_msg *);
6809 newmsg->sadb_msg_errno = 0;
6810 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6811 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6813 /* for authentication algorithm */
6815 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6816 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6817 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6818 off += PFKEY_ALIGN8(sizeof(*sup));
6820 for (i = 1; i <= SADB_AALG_MAX; i++) {
6821 struct auth_hash *aalgo;
6822 u_int16_t minkeysize, maxkeysize;
6824 aalgo = ah_algorithm_lookup(i);
6827 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6828 alg->sadb_alg_id = i;
6829 alg->sadb_alg_ivlen = 0;
6830 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
6831 alg->sadb_alg_minbits = _BITS(minkeysize);
6832 alg->sadb_alg_maxbits = _BITS(maxkeysize);
6833 off += PFKEY_ALIGN8(sizeof(*alg));
6837 /* for encryption algorithm */
6839 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6840 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6841 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6842 off += PFKEY_ALIGN8(sizeof(*sup));
6844 for (i = 1; i <= SADB_EALG_MAX; i++) {
6845 struct enc_xform *ealgo;
6847 ealgo = esp_algorithm_lookup(i);
6850 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6851 alg->sadb_alg_id = i;
6852 alg->sadb_alg_ivlen = ealgo->blocksize;
6853 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
6854 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
6855 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6859 IPSEC_ASSERT(off == len,
6860 ("length assumption failed (off %u len %u)", off, len));
6863 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6868 * free secreg entry registered.
6869 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6872 key_freereg(struct socket *so)
6877 IPSEC_ASSERT(so != NULL, ("NULL so"));
6880 * check whether existing or not.
6881 * check all type of SA, because there is a potential that
6882 * one socket is registered to multiple type of SA.
6885 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6886 LIST_FOREACH(reg, &V_regtree[i], chain) {
6887 if (reg->so == so && __LIST_CHAINED(reg)) {
6888 LIST_REMOVE(reg, chain);
6889 free(reg, M_IPSEC_SAR);
6898 * SADB_EXPIRE processing
6900 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6902 * NOTE: We send only soft lifetime extension.
6905 * others : error number
6908 key_expire(struct secasvar *sav)
6912 struct mbuf *result = NULL, *m;
6915 struct sadb_lifetime *lt;
6917 /* XXX: Why do we lock ? */
6918 s = splnet(); /*called from softclock()*/
6920 IPSEC_ASSERT (sav != NULL, ("null sav"));
6921 IPSEC_ASSERT (sav->sah != NULL, ("null sa header"));
6923 /* set msg header */
6924 satype = key_proto2satype(sav->sah->saidx.proto);
6925 IPSEC_ASSERT(satype != 0, ("invalid proto, satype %u", satype));
6926 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6933 /* create SA extension */
6934 m = key_setsadbsa(sav);
6941 /* create SA extension */
6942 m = key_setsadbxsa2(sav->sah->saidx.mode,
6943 sav->replay ? sav->replay->count : 0,
6944 sav->sah->saidx.reqid);
6951 /* create lifetime extension (current and soft) */
6952 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6953 m = key_alloc_mbuf(len);
6954 if (!m || m->m_next) { /*XXX*/
6960 bzero(mtod(m, caddr_t), len);
6961 lt = mtod(m, struct sadb_lifetime *);
6962 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6963 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6964 lt->sadb_lifetime_allocations = sav->lft_c->allocations;
6965 lt->sadb_lifetime_bytes = sav->lft_c->bytes;
6966 lt->sadb_lifetime_addtime = sav->lft_c->addtime;
6967 lt->sadb_lifetime_usetime = sav->lft_c->usetime;
6968 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6969 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6970 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
6971 lt->sadb_lifetime_allocations = sav->lft_s->allocations;
6972 lt->sadb_lifetime_bytes = sav->lft_s->bytes;
6973 lt->sadb_lifetime_addtime = sav->lft_s->addtime;
6974 lt->sadb_lifetime_usetime = sav->lft_s->usetime;
6977 /* set sadb_address for source */
6978 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6979 &sav->sah->saidx.src.sa,
6980 FULLMASK, IPSEC_ULPROTO_ANY);
6987 /* set sadb_address for destination */
6988 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6989 &sav->sah->saidx.dst.sa,
6990 FULLMASK, IPSEC_ULPROTO_ANY);
6998 * XXX-BZ Handle NAT-T extensions here.
7001 if ((result->m_flags & M_PKTHDR) == 0) {
7006 if (result->m_len < sizeof(struct sadb_msg)) {
7007 result = m_pullup(result, sizeof(struct sadb_msg));
7008 if (result == NULL) {
7014 result->m_pkthdr.len = 0;
7015 for (m = result; m; m = m->m_next)
7016 result->m_pkthdr.len += m->m_len;
7018 mtod(result, struct sadb_msg *)->sadb_msg_len =
7019 PFKEY_UNIT64(result->m_pkthdr.len);
7022 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
7032 * SADB_FLUSH processing
7035 * from the ikmpd, and free all entries in secastree.
7039 * NOTE: to do is only marking SADB_SASTATE_DEAD.
7041 * m will always be freed.
7044 key_flush(so, m, mhp)
7047 const struct sadb_msghdr *mhp;
7049 struct sadb_msg *newmsg;
7050 struct secashead *sah, *nextsah;
7051 struct secasvar *sav, *nextsav;
7056 IPSEC_ASSERT(so != NULL, ("null socket"));
7057 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7058 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7060 /* map satype to proto */
7061 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7062 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7064 return key_senderror(so, m, EINVAL);
7067 /* no SATYPE specified, i.e. flushing all SA. */
7069 for (sah = LIST_FIRST(&V_sahtree);
7072 nextsah = LIST_NEXT(sah, chain);
7074 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7075 && proto != sah->saidx.proto)
7079 stateidx < _ARRAYLEN(saorder_state_alive);
7081 state = saorder_state_any[stateidx];
7082 for (sav = LIST_FIRST(&sah->savtree[state]);
7086 nextsav = LIST_NEXT(sav, chain);
7088 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
7093 sah->state = SADB_SASTATE_DEAD;
7097 if (m->m_len < sizeof(struct sadb_msg) ||
7098 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
7099 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
7100 return key_senderror(so, m, ENOBUFS);
7106 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
7107 newmsg = mtod(m, struct sadb_msg *);
7108 newmsg->sadb_msg_errno = 0;
7109 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
7111 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7115 * SADB_DUMP processing
7116 * dump all entries including status of DEAD in SAD.
7119 * from the ikmpd, and dump all secasvar leaves
7124 * m will always be freed.
7127 key_dump(so, m, mhp)
7130 const struct sadb_msghdr *mhp;
7132 struct secashead *sah;
7133 struct secasvar *sav;
7139 struct sadb_msg *newmsg;
7142 IPSEC_ASSERT(so != NULL, ("null socket"));
7143 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7144 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7145 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7147 /* map satype to proto */
7148 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7149 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7151 return key_senderror(so, m, EINVAL);
7154 /* count sav entries to be sent to the userland. */
7157 LIST_FOREACH(sah, &V_sahtree, chain) {
7158 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7159 && proto != sah->saidx.proto)
7163 stateidx < _ARRAYLEN(saorder_state_any);
7165 state = saorder_state_any[stateidx];
7166 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7174 return key_senderror(so, m, ENOENT);
7177 /* send this to the userland, one at a time. */
7179 LIST_FOREACH(sah, &V_sahtree, chain) {
7180 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7181 && proto != sah->saidx.proto)
7184 /* map proto to satype */
7185 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
7187 ipseclog((LOG_DEBUG, "%s: there was invalid proto in "
7188 "SAD.\n", __func__));
7189 return key_senderror(so, m, EINVAL);
7193 stateidx < _ARRAYLEN(saorder_state_any);
7195 state = saorder_state_any[stateidx];
7196 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7197 n = key_setdumpsa(sav, SADB_DUMP, satype,
7198 --cnt, mhp->msg->sadb_msg_pid);
7201 return key_senderror(so, m, ENOBUFS);
7203 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
7214 * SADB_X_PROMISC processing
7216 * m will always be freed.
7219 key_promisc(so, m, mhp)
7222 const struct sadb_msghdr *mhp;
7226 IPSEC_ASSERT(so != NULL, ("null socket"));
7227 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7228 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7229 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7231 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7233 if (olen < sizeof(struct sadb_msg)) {
7235 return key_senderror(so, m, EINVAL);
7240 } else if (olen == sizeof(struct sadb_msg)) {
7241 /* enable/disable promisc mode */
7244 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
7245 return key_senderror(so, m, EINVAL);
7246 mhp->msg->sadb_msg_errno = 0;
7247 switch (mhp->msg->sadb_msg_satype) {
7250 kp->kp_promisc = mhp->msg->sadb_msg_satype;
7253 return key_senderror(so, m, EINVAL);
7256 /* send the original message back to everyone */
7257 mhp->msg->sadb_msg_errno = 0;
7258 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7260 /* send packet as is */
7262 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
7264 /* TODO: if sadb_msg_seq is specified, send to specific pid */
7265 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7269 static int (*key_typesw[]) __P((struct socket *, struct mbuf *,
7270 const struct sadb_msghdr *)) = {
7271 NULL, /* SADB_RESERVED */
7272 key_getspi, /* SADB_GETSPI */
7273 key_update, /* SADB_UPDATE */
7274 key_add, /* SADB_ADD */
7275 key_delete, /* SADB_DELETE */
7276 key_get, /* SADB_GET */
7277 key_acquire2, /* SADB_ACQUIRE */
7278 key_register, /* SADB_REGISTER */
7279 NULL, /* SADB_EXPIRE */
7280 key_flush, /* SADB_FLUSH */
7281 key_dump, /* SADB_DUMP */
7282 key_promisc, /* SADB_X_PROMISC */
7283 NULL, /* SADB_X_PCHANGE */
7284 key_spdadd, /* SADB_X_SPDUPDATE */
7285 key_spdadd, /* SADB_X_SPDADD */
7286 key_spddelete, /* SADB_X_SPDDELETE */
7287 key_spdget, /* SADB_X_SPDGET */
7288 NULL, /* SADB_X_SPDACQUIRE */
7289 key_spddump, /* SADB_X_SPDDUMP */
7290 key_spdflush, /* SADB_X_SPDFLUSH */
7291 key_spdadd, /* SADB_X_SPDSETIDX */
7292 NULL, /* SADB_X_SPDEXPIRE */
7293 key_spddelete2, /* SADB_X_SPDDELETE2 */
7297 * parse sadb_msg buffer to process PFKEYv2,
7298 * and create a data to response if needed.
7299 * I think to be dealed with mbuf directly.
7301 * msgp : pointer to pointer to a received buffer pulluped.
7302 * This is rewrited to response.
7303 * so : pointer to socket.
7305 * length for buffer to send to user process.
7312 struct sadb_msg *msg;
7313 struct sadb_msghdr mh;
7318 IPSEC_ASSERT(so != NULL, ("null socket"));
7319 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7321 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
7322 KEYDEBUG(KEYDEBUG_KEY_DUMP,
7323 ipseclog((LOG_DEBUG, "%s: passed sadb_msg\n", __func__));
7327 if (m->m_len < sizeof(struct sadb_msg)) {
7328 m = m_pullup(m, sizeof(struct sadb_msg));
7332 msg = mtod(m, struct sadb_msg *);
7333 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
7334 target = KEY_SENDUP_ONE;
7336 if ((m->m_flags & M_PKTHDR) == 0 ||
7337 m->m_pkthdr.len != m->m_pkthdr.len) {
7338 ipseclog((LOG_DEBUG, "%s: invalid message length.\n",__func__));
7339 V_pfkeystat.out_invlen++;
7344 if (msg->sadb_msg_version != PF_KEY_V2) {
7345 ipseclog((LOG_DEBUG, "%s: PF_KEY version %u is mismatched.\n",
7346 __func__, msg->sadb_msg_version));
7347 V_pfkeystat.out_invver++;
7352 if (msg->sadb_msg_type > SADB_MAX) {
7353 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7354 __func__, msg->sadb_msg_type));
7355 V_pfkeystat.out_invmsgtype++;
7360 /* for old-fashioned code - should be nuked */
7361 if (m->m_pkthdr.len > MCLBYTES) {
7368 MGETHDR(n, M_DONTWAIT, MT_DATA);
7369 if (n && m->m_pkthdr.len > MHLEN) {
7370 MCLGET(n, M_DONTWAIT);
7371 if ((n->m_flags & M_EXT) == 0) {
7380 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
7381 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
7387 /* align the mbuf chain so that extensions are in contiguous region. */
7388 error = key_align(m, &mh);
7395 switch (msg->sadb_msg_satype) {
7396 case SADB_SATYPE_UNSPEC:
7397 switch (msg->sadb_msg_type) {
7405 ipseclog((LOG_DEBUG, "%s: must specify satype "
7406 "when msg type=%u.\n", __func__,
7407 msg->sadb_msg_type));
7408 V_pfkeystat.out_invsatype++;
7413 case SADB_SATYPE_AH:
7414 case SADB_SATYPE_ESP:
7415 case SADB_X_SATYPE_IPCOMP:
7416 case SADB_X_SATYPE_TCPSIGNATURE:
7417 switch (msg->sadb_msg_type) {
7419 case SADB_X_SPDDELETE:
7421 case SADB_X_SPDDUMP:
7422 case SADB_X_SPDFLUSH:
7423 case SADB_X_SPDSETIDX:
7424 case SADB_X_SPDUPDATE:
7425 case SADB_X_SPDDELETE2:
7426 ipseclog((LOG_DEBUG, "%s: illegal satype=%u\n",
7427 __func__, msg->sadb_msg_type));
7428 V_pfkeystat.out_invsatype++;
7433 case SADB_SATYPE_RSVP:
7434 case SADB_SATYPE_OSPFV2:
7435 case SADB_SATYPE_RIPV2:
7436 case SADB_SATYPE_MIP:
7437 ipseclog((LOG_DEBUG, "%s: type %u isn't supported.\n",
7438 __func__, msg->sadb_msg_satype));
7439 V_pfkeystat.out_invsatype++;
7442 case 1: /* XXX: What does it do? */
7443 if (msg->sadb_msg_type == SADB_X_PROMISC)
7447 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7448 __func__, msg->sadb_msg_satype));
7449 V_pfkeystat.out_invsatype++;
7454 /* check field of upper layer protocol and address family */
7455 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
7456 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
7457 struct sadb_address *src0, *dst0;
7460 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
7461 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
7463 /* check upper layer protocol */
7464 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
7465 ipseclog((LOG_DEBUG, "%s: upper layer protocol "
7466 "mismatched.\n", __func__));
7467 V_pfkeystat.out_invaddr++;
7473 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
7474 PFKEY_ADDR_SADDR(dst0)->sa_family) {
7475 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
7477 V_pfkeystat.out_invaddr++;
7481 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7482 PFKEY_ADDR_SADDR(dst0)->sa_len) {
7483 ipseclog((LOG_DEBUG, "%s: address struct size "
7484 "mismatched.\n", __func__));
7485 V_pfkeystat.out_invaddr++;
7490 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7492 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7493 sizeof(struct sockaddr_in)) {
7494 V_pfkeystat.out_invaddr++;
7500 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7501 sizeof(struct sockaddr_in6)) {
7502 V_pfkeystat.out_invaddr++;
7508 ipseclog((LOG_DEBUG, "%s: unsupported address family\n",
7510 V_pfkeystat.out_invaddr++;
7511 error = EAFNOSUPPORT;
7515 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7517 plen = sizeof(struct in_addr) << 3;
7520 plen = sizeof(struct in6_addr) << 3;
7523 plen = 0; /*fool gcc*/
7527 /* check max prefix length */
7528 if (src0->sadb_address_prefixlen > plen ||
7529 dst0->sadb_address_prefixlen > plen) {
7530 ipseclog((LOG_DEBUG, "%s: illegal prefixlen.\n",
7532 V_pfkeystat.out_invaddr++;
7538 * prefixlen == 0 is valid because there can be a case when
7539 * all addresses are matched.
7543 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
7544 key_typesw[msg->sadb_msg_type] == NULL) {
7545 V_pfkeystat.out_invmsgtype++;
7550 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
7553 msg->sadb_msg_errno = error;
7554 return key_sendup_mbuf(so, m, target);
7558 key_senderror(so, m, code)
7563 struct sadb_msg *msg;
7565 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7566 ("mbuf too small, len %u", m->m_len));
7568 msg = mtod(m, struct sadb_msg *);
7569 msg->sadb_msg_errno = code;
7570 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
7574 * set the pointer to each header into message buffer.
7575 * m will be freed on error.
7576 * XXX larger-than-MCLBYTES extension?
7581 struct sadb_msghdr *mhp;
7584 struct sadb_ext *ext;
7589 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7590 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7591 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7592 ("mbuf too small, len %u", m->m_len));
7595 bzero(mhp, sizeof(*mhp));
7597 mhp->msg = mtod(m, struct sadb_msg *);
7598 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
7600 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7601 extlen = end; /*just in case extlen is not updated*/
7602 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
7603 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
7605 /* m is already freed */
7608 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7611 switch (ext->sadb_ext_type) {
7613 case SADB_EXT_ADDRESS_SRC:
7614 case SADB_EXT_ADDRESS_DST:
7615 case SADB_EXT_ADDRESS_PROXY:
7616 case SADB_EXT_LIFETIME_CURRENT:
7617 case SADB_EXT_LIFETIME_HARD:
7618 case SADB_EXT_LIFETIME_SOFT:
7619 case SADB_EXT_KEY_AUTH:
7620 case SADB_EXT_KEY_ENCRYPT:
7621 case SADB_EXT_IDENTITY_SRC:
7622 case SADB_EXT_IDENTITY_DST:
7623 case SADB_EXT_SENSITIVITY:
7624 case SADB_EXT_PROPOSAL:
7625 case SADB_EXT_SUPPORTED_AUTH:
7626 case SADB_EXT_SUPPORTED_ENCRYPT:
7627 case SADB_EXT_SPIRANGE:
7628 case SADB_X_EXT_POLICY:
7629 case SADB_X_EXT_SA2:
7631 case SADB_X_EXT_NAT_T_TYPE:
7632 case SADB_X_EXT_NAT_T_SPORT:
7633 case SADB_X_EXT_NAT_T_DPORT:
7634 case SADB_X_EXT_NAT_T_OAI:
7635 case SADB_X_EXT_NAT_T_OAR:
7636 case SADB_X_EXT_NAT_T_FRAG:
7638 /* duplicate check */
7640 * XXX Are there duplication payloads of either
7641 * KEY_AUTH or KEY_ENCRYPT ?
7643 if (mhp->ext[ext->sadb_ext_type] != NULL) {
7644 ipseclog((LOG_DEBUG, "%s: duplicate ext_type "
7645 "%u\n", __func__, ext->sadb_ext_type));
7647 V_pfkeystat.out_dupext++;
7652 ipseclog((LOG_DEBUG, "%s: invalid ext_type %u\n",
7653 __func__, ext->sadb_ext_type));
7655 V_pfkeystat.out_invexttype++;
7659 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
7661 if (key_validate_ext(ext, extlen)) {
7663 V_pfkeystat.out_invlen++;
7667 n = m_pulldown(m, off, extlen, &toff);
7669 /* m is already freed */
7672 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7674 mhp->ext[ext->sadb_ext_type] = ext;
7675 mhp->extoff[ext->sadb_ext_type] = off;
7676 mhp->extlen[ext->sadb_ext_type] = extlen;
7681 V_pfkeystat.out_invlen++;
7689 key_validate_ext(ext, len)
7690 const struct sadb_ext *ext;
7693 const struct sockaddr *sa;
7694 enum { NONE, ADDR } checktype = NONE;
7696 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
7698 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
7701 /* if it does not match minimum/maximum length, bail */
7702 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
7703 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
7705 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
7707 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
7710 /* more checks based on sadb_ext_type XXX need more */
7711 switch (ext->sadb_ext_type) {
7712 case SADB_EXT_ADDRESS_SRC:
7713 case SADB_EXT_ADDRESS_DST:
7714 case SADB_EXT_ADDRESS_PROXY:
7715 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7718 case SADB_EXT_IDENTITY_SRC:
7719 case SADB_EXT_IDENTITY_DST:
7720 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7721 SADB_X_IDENTTYPE_ADDR) {
7722 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7732 switch (checktype) {
7736 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
7737 if (len < baselen + sal)
7739 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7752 for (i = 0; i < IPSEC_DIR_MAX; i++)
7753 LIST_INIT(&V_sptree[i]);
7755 LIST_INIT(&V_sahtree);
7757 for (i = 0; i <= SADB_SATYPE_MAX; i++)
7758 LIST_INIT(&V_regtree[i]);
7760 LIST_INIT(&V_acqtree);
7761 LIST_INIT(&V_spacqtree);
7763 /* system default */
7764 V_ip4_def_policy.policy = IPSEC_POLICY_NONE;
7765 V_ip4_def_policy.refcnt++; /*never reclaim this*/
7767 if (!IS_DEFAULT_VNET(curvnet))
7771 REGTREE_LOCK_INIT();
7772 SAHTREE_LOCK_INIT();
7776 #ifndef IPSEC_DEBUG2
7777 timeout((void *)key_timehandler, (void *)0, hz);
7778 #endif /*IPSEC_DEBUG2*/
7780 /* initialize key statistics */
7781 keystat.getspi_count = 1;
7783 printf("IPsec: Initialized Security Association Processing.\n");
7790 struct secpolicy *sp, *nextsp;
7791 struct secacq *acq, *nextacq;
7792 struct secspacq *spacq, *nextspacq;
7793 struct secashead *sah, *nextsah;
7798 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7799 for (sp = LIST_FIRST(&V_sptree[i]);
7800 sp != NULL; sp = nextsp) {
7801 nextsp = LIST_NEXT(sp, chain);
7802 if (__LIST_CHAINED(sp)) {
7803 LIST_REMOVE(sp, chain);
7804 free(sp, M_IPSEC_SP);
7811 for (sah = LIST_FIRST(&V_sahtree); sah != NULL; sah = nextsah) {
7812 nextsah = LIST_NEXT(sah, chain);
7813 if (__LIST_CHAINED(sah)) {
7814 LIST_REMOVE(sah, chain);
7815 free(sah, M_IPSEC_SAH);
7821 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7822 LIST_FOREACH(reg, &V_regtree[i], chain) {
7823 if (__LIST_CHAINED(reg)) {
7824 LIST_REMOVE(reg, chain);
7825 free(reg, M_IPSEC_SAR);
7833 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
7834 nextacq = LIST_NEXT(acq, chain);
7835 if (__LIST_CHAINED(acq)) {
7836 LIST_REMOVE(acq, chain);
7837 free(acq, M_IPSEC_SAQ);
7843 for (spacq = LIST_FIRST(&V_spacqtree); spacq != NULL;
7844 spacq = nextspacq) {
7845 nextspacq = LIST_NEXT(spacq, chain);
7846 if (__LIST_CHAINED(spacq)) {
7847 LIST_REMOVE(spacq, chain);
7848 free(spacq, M_IPSEC_SAQ);
7856 * XXX: maybe This function is called after INBOUND IPsec processing.
7858 * Special check for tunnel-mode packets.
7859 * We must make some checks for consistency between inner and outer IP header.
7861 * xxx more checks to be provided
7864 key_checktunnelsanity(sav, family, src, dst)
7865 struct secasvar *sav;
7870 IPSEC_ASSERT(sav->sah != NULL, ("null SA header"));
7872 /* XXX: check inner IP header */
7877 /* record data transfer on SA, and update timestamps */
7879 key_sa_recordxfer(sav, m)
7880 struct secasvar *sav;
7883 IPSEC_ASSERT(sav != NULL, ("Null secasvar"));
7884 IPSEC_ASSERT(m != NULL, ("Null mbuf"));
7889 * XXX Currently, there is a difference of bytes size
7890 * between inbound and outbound processing.
7892 sav->lft_c->bytes += m->m_pkthdr.len;
7893 /* to check bytes lifetime is done in key_timehandler(). */
7896 * We use the number of packets as the unit of
7897 * allocations. We increment the variable
7898 * whenever {esp,ah}_{in,out}put is called.
7900 sav->lft_c->allocations++;
7901 /* XXX check for expires? */
7904 * NOTE: We record CURRENT usetime by using wall clock,
7905 * in seconds. HARD and SOFT lifetime are measured by the time
7906 * difference (again in seconds) from usetime.
7910 * -----+-----+--------+---> t
7911 * <--------------> HARD
7914 sav->lft_c->usetime = time_second;
7915 /* XXX check for expires? */
7922 key_sa_routechange(dst)
7923 struct sockaddr *dst;
7925 struct secashead *sah;
7929 LIST_FOREACH(sah, &V_sahtree, chain) {
7930 ro = &sah->sa_route;
7931 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7932 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7934 ro->ro_rt = (struct rtentry *)NULL;
7941 key_sa_chgstate(struct secasvar *sav, u_int8_t state)
7943 IPSEC_ASSERT(sav != NULL, ("NULL sav"));
7944 SAHTREE_LOCK_ASSERT();
7946 if (sav->state != state) {
7947 if (__LIST_CHAINED(sav))
7948 LIST_REMOVE(sav, chain);
7950 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7956 struct secasvar *sav;
7959 IPSEC_ASSERT(sav->iv != NULL, ("null IV"));
7960 key_randomfill(sav->iv, sav->ivlen);
7964 static struct mbuf *
7968 struct mbuf *m = NULL, *n;
7973 MGET(n, M_DONTWAIT, MT_DATA);
7974 if (n && len > MLEN)
7975 MCLGET(n, M_DONTWAIT);
7983 n->m_len = M_TRAILINGSPACE(n);
7984 /* use the bottom of mbuf, hoping we can prepend afterwards */
7985 if (n->m_len > len) {
7986 t = (n->m_len - len) & ~(sizeof(long) - 1);
8003 * Take one of the kernel's security keys and convert it into a PF_KEY
8004 * structure within an mbuf, suitable for sending up to a waiting
8005 * application in user land.
8008 * src: A pointer to a kernel security key.
8009 * exttype: Which type of key this is. Refer to the PF_KEY data structures.
8011 * a valid mbuf or NULL indicating an error
8015 static struct mbuf *
8016 key_setkey(struct seckey *src, u_int16_t exttype)
8025 len = PFKEY_ALIGN8(sizeof(struct sadb_key) + _KEYLEN(src));
8026 m = key_alloc_mbuf(len);
8029 p = mtod(m, struct sadb_key *);
8031 p->sadb_key_len = PFKEY_UNIT64(len);
8032 p->sadb_key_exttype = exttype;
8033 p->sadb_key_bits = src->bits;
8034 bcopy(src->key_data, _KEYBUF(p), _KEYLEN(src));
8040 * Take one of the kernel's lifetime data structures and convert it
8041 * into a PF_KEY structure within an mbuf, suitable for sending up to
8042 * a waiting application in user land.
8045 * src: A pointer to a kernel lifetime structure.
8046 * exttype: Which type of lifetime this is. Refer to the PF_KEY
8047 * data structures for more information.
8049 * a valid mbuf or NULL indicating an error
8053 static struct mbuf *
8054 key_setlifetime(struct seclifetime *src, u_int16_t exttype)
8056 struct mbuf *m = NULL;
8057 struct sadb_lifetime *p;
8058 int len = PFKEY_ALIGN8(sizeof(struct sadb_lifetime));
8063 m = key_alloc_mbuf(len);
8066 p = mtod(m, struct sadb_lifetime *);
8069 p->sadb_lifetime_len = PFKEY_UNIT64(len);
8070 p->sadb_lifetime_exttype = exttype;
8071 p->sadb_lifetime_allocations = src->allocations;
8072 p->sadb_lifetime_bytes = src->bytes;
8073 p->sadb_lifetime_addtime = src->addtime;
8074 p->sadb_lifetime_usetime = src->usetime;