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) {
1886 newsp->state = IPSEC_SPSTATE_DEAD;
1891 if (newsp != NULL) {
1893 ipseclog((LOG_DEBUG, "%s: a SP entry exists already.\n",
1895 return key_senderror(so, m, EEXIST);
1899 /* allocation new SP entry */
1900 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1901 return key_senderror(so, m, error);
1904 if ((newsp->id = key_getnewspid()) == 0) {
1906 return key_senderror(so, m, ENOBUFS);
1909 /* XXX boundary check against sa_len */
1910 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1913 src0->sadb_address_prefixlen,
1914 dst0->sadb_address_prefixlen,
1915 src0->sadb_address_proto,
1918 /* sanity check on addr pair */
1919 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1920 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1922 return key_senderror(so, m, EINVAL);
1924 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1925 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1927 return key_senderror(so, m, EINVAL);
1930 if (newsp->req && newsp->req->saidx.src.sa.sa_family) {
1931 struct sockaddr *sa;
1932 sa = (struct sockaddr *)(src0 + 1);
1933 if (sa->sa_family != newsp->req->saidx.src.sa.sa_family) {
1935 return key_senderror(so, m, EINVAL);
1938 if (newsp->req && newsp->req->saidx.dst.sa.sa_family) {
1939 struct sockaddr *sa;
1940 sa = (struct sockaddr *)(dst0 + 1);
1941 if (sa->sa_family != newsp->req->saidx.dst.sa.sa_family) {
1943 return key_senderror(so, m, EINVAL);
1948 newsp->created = time_second;
1949 newsp->lastused = newsp->created;
1950 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1951 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1953 newsp->refcnt = 1; /* do not reclaim until I say I do */
1954 newsp->state = IPSEC_SPSTATE_ALIVE;
1955 LIST_INSERT_TAIL(&V_sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1957 /* delete the entry in spacqtree */
1958 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1959 struct secspacq *spacq = key_getspacq(&spidx);
1960 if (spacq != NULL) {
1961 /* reset counter in order to deletion by timehandler. */
1962 spacq->created = time_second;
1969 struct mbuf *n, *mpolicy;
1970 struct sadb_msg *newmsg;
1974 * Note: do not send SADB_X_EXT_NAT_T_* here:
1975 * we are sending traffic endpoints.
1978 /* create new sadb_msg to reply. */
1980 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1981 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1982 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1984 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1986 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1989 return key_senderror(so, m, ENOBUFS);
1991 if (n->m_len < sizeof(*newmsg)) {
1992 n = m_pullup(n, sizeof(*newmsg));
1994 return key_senderror(so, m, ENOBUFS);
1996 newmsg = mtod(n, struct sadb_msg *);
1997 newmsg->sadb_msg_errno = 0;
1998 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2001 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
2002 sizeof(*xpl), &off);
2003 if (mpolicy == NULL) {
2004 /* n is already freed */
2005 return key_senderror(so, m, ENOBUFS);
2007 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
2008 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
2010 return key_senderror(so, m, EINVAL);
2012 xpl->sadb_x_policy_id = newsp->id;
2015 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2020 * get new policy id.
2028 u_int32_t newid = 0;
2029 int count = V_key_spi_trycnt; /* XXX */
2030 struct secpolicy *sp;
2032 /* when requesting to allocate spi ranged */
2034 newid = (V_policy_id = (V_policy_id == ~0 ? 1 : V_policy_id + 1));
2036 if ((sp = key_getspbyid(newid)) == NULL)
2042 if (count == 0 || newid == 0) {
2043 ipseclog((LOG_DEBUG, "%s: to allocate policy id is failed.\n",
2052 * SADB_SPDDELETE processing
2054 * <base, address(SD), policy(*)>
2055 * from the user(?), and set SADB_SASTATE_DEAD,
2057 * <base, address(SD), policy(*)>
2059 * policy(*) including direction of policy.
2061 * m will always be freed.
2064 key_spddelete(so, m, mhp)
2067 const struct sadb_msghdr *mhp;
2069 struct sadb_address *src0, *dst0;
2070 struct sadb_x_policy *xpl0;
2071 struct secpolicyindex spidx;
2072 struct secpolicy *sp;
2074 IPSEC_ASSERT(so != NULL, ("null so"));
2075 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2076 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2077 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2079 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
2080 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
2081 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
2082 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2084 return key_senderror(so, m, EINVAL);
2086 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
2087 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
2088 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2089 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2091 return key_senderror(so, m, EINVAL);
2094 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2095 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2096 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2099 * Note: do not parse SADB_X_EXT_NAT_T_* here:
2100 * we are processing traffic endpoints.
2104 /* XXX boundary check against sa_len */
2105 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2108 src0->sadb_address_prefixlen,
2109 dst0->sadb_address_prefixlen,
2110 src0->sadb_address_proto,
2113 /* checking the direciton. */
2114 switch (xpl0->sadb_x_policy_dir) {
2115 case IPSEC_DIR_INBOUND:
2116 case IPSEC_DIR_OUTBOUND:
2119 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
2120 return key_senderror(so, m, EINVAL);
2123 /* Is there SP in SPD ? */
2124 if ((sp = key_getsp(&spidx)) == NULL) {
2125 ipseclog((LOG_DEBUG, "%s: no SP found.\n", __func__));
2126 return key_senderror(so, m, EINVAL);
2129 /* save policy id to buffer to be returned. */
2130 xpl0->sadb_x_policy_id = sp->id;
2133 sp->state = IPSEC_SPSTATE_DEAD;
2139 struct sadb_msg *newmsg;
2142 * Note: do not send SADB_X_EXT_NAT_T_* here:
2143 * we are sending traffic endpoints.
2146 /* create new sadb_msg to reply. */
2147 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2148 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2150 return key_senderror(so, m, ENOBUFS);
2152 newmsg = mtod(n, struct sadb_msg *);
2153 newmsg->sadb_msg_errno = 0;
2154 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2157 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2162 * SADB_SPDDELETE2 processing
2165 * from the user(?), and set SADB_SASTATE_DEAD,
2169 * policy(*) including direction of policy.
2171 * m will always be freed.
2174 key_spddelete2(so, m, mhp)
2177 const struct sadb_msghdr *mhp;
2180 struct secpolicy *sp;
2182 IPSEC_ASSERT(so != NULL, ("null socket"));
2183 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2184 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2185 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2187 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2188 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2189 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", __func__));
2190 return key_senderror(so, m, EINVAL);
2193 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2195 /* Is there SP in SPD ? */
2196 if ((sp = key_getspbyid(id)) == NULL) {
2197 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2198 return key_senderror(so, m, EINVAL);
2202 sp->state = IPSEC_SPSTATE_DEAD;
2207 struct mbuf *n, *nn;
2208 struct sadb_msg *newmsg;
2211 /* create new sadb_msg to reply. */
2212 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2214 MGETHDR(n, M_DONTWAIT, MT_DATA);
2215 if (n && len > MHLEN) {
2216 MCLGET(n, M_DONTWAIT);
2217 if ((n->m_flags & M_EXT) == 0) {
2223 return key_senderror(so, m, ENOBUFS);
2229 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2230 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2232 IPSEC_ASSERT(off == len, ("length inconsistency (off %u len %u)",
2235 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2236 mhp->extlen[SADB_X_EXT_POLICY], M_DONTWAIT);
2239 return key_senderror(so, m, ENOBUFS);
2242 n->m_pkthdr.len = 0;
2243 for (nn = n; nn; nn = nn->m_next)
2244 n->m_pkthdr.len += nn->m_len;
2246 newmsg = mtod(n, struct sadb_msg *);
2247 newmsg->sadb_msg_errno = 0;
2248 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2251 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2256 * SADB_X_GET processing
2261 * <base, address(SD), policy>
2263 * policy(*) including direction of policy.
2265 * m will always be freed.
2268 key_spdget(so, m, mhp)
2271 const struct sadb_msghdr *mhp;
2274 struct secpolicy *sp;
2277 IPSEC_ASSERT(so != NULL, ("null socket"));
2278 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2279 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2280 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2282 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2283 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2284 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2286 return key_senderror(so, m, EINVAL);
2289 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2291 /* Is there SP in SPD ? */
2292 if ((sp = key_getspbyid(id)) == NULL) {
2293 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2294 return key_senderror(so, m, ENOENT);
2297 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2300 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2302 return key_senderror(so, m, ENOBUFS);
2306 * SADB_X_SPDACQUIRE processing.
2307 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2310 * to KMD, and expect to receive
2311 * <base> with SADB_X_SPDACQUIRE if error occured,
2314 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2315 * policy(*) is without policy requests.
2318 * others: error number
2322 struct secpolicy *sp;
2324 struct mbuf *result = NULL, *m;
2325 struct secspacq *newspacq;
2327 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2328 IPSEC_ASSERT(sp->req == NULL, ("policy exists"));
2329 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
2330 ("policy not IPSEC %u", sp->policy));
2332 /* Get an entry to check whether sent message or not. */
2333 newspacq = key_getspacq(&sp->spidx);
2334 if (newspacq != NULL) {
2335 if (V_key_blockacq_count < newspacq->count) {
2336 /* reset counter and do send message. */
2337 newspacq->count = 0;
2339 /* increment counter and do nothing. */
2345 /* make new entry for blocking to send SADB_ACQUIRE. */
2346 newspacq = key_newspacq(&sp->spidx);
2347 if (newspacq == NULL)
2351 /* create new sadb_msg to reply. */
2352 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2358 result->m_pkthdr.len = 0;
2359 for (m = result; m; m = m->m_next)
2360 result->m_pkthdr.len += m->m_len;
2362 mtod(result, struct sadb_msg *)->sadb_msg_len =
2363 PFKEY_UNIT64(result->m_pkthdr.len);
2365 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2369 * SADB_SPDFLUSH processing
2372 * from the user, and free all entries in secpctree.
2376 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2378 * m will always be freed.
2381 key_spdflush(so, m, mhp)
2384 const struct sadb_msghdr *mhp;
2386 struct sadb_msg *newmsg;
2387 struct secpolicy *sp;
2390 IPSEC_ASSERT(so != NULL, ("null socket"));
2391 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2392 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2393 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2395 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2396 return key_senderror(so, m, EINVAL);
2398 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2400 LIST_FOREACH(sp, &V_sptree[dir], chain)
2401 sp->state = IPSEC_SPSTATE_DEAD;
2405 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2406 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2407 return key_senderror(so, m, ENOBUFS);
2413 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2414 newmsg = mtod(m, struct sadb_msg *);
2415 newmsg->sadb_msg_errno = 0;
2416 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2418 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2422 * SADB_SPDDUMP processing
2425 * from the user, and dump all SP leaves
2430 * m will always be freed.
2433 key_spddump(so, m, mhp)
2436 const struct sadb_msghdr *mhp;
2438 struct secpolicy *sp;
2443 IPSEC_ASSERT(so != NULL, ("null socket"));
2444 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2445 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2446 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2448 /* search SPD entry and get buffer size. */
2451 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2452 LIST_FOREACH(sp, &V_sptree[dir], chain) {
2459 return key_senderror(so, m, ENOENT);
2462 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2463 LIST_FOREACH(sp, &V_sptree[dir], chain) {
2465 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2466 mhp->msg->sadb_msg_pid);
2469 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2478 static struct mbuf *
2479 key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq, u_int32_t pid)
2481 struct mbuf *result = NULL, *m;
2482 struct seclifetime lt;
2484 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2490 * Note: do not send SADB_X_EXT_NAT_T_* here:
2491 * we are sending traffic endpoints.
2493 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2494 &sp->spidx.src.sa, sp->spidx.prefs,
2495 sp->spidx.ul_proto);
2500 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2501 &sp->spidx.dst.sa, sp->spidx.prefd,
2502 sp->spidx.ul_proto);
2513 lt.addtime=sp->created;
2514 lt.usetime= sp->lastused;
2515 m = key_setlifetime(<, SADB_EXT_LIFETIME_CURRENT);
2520 lt.addtime=sp->lifetime;
2521 lt.usetime= sp->validtime;
2522 m = key_setlifetime(<, SADB_EXT_LIFETIME_HARD);
2528 if ((result->m_flags & M_PKTHDR) == 0)
2531 if (result->m_len < sizeof(struct sadb_msg)) {
2532 result = m_pullup(result, sizeof(struct sadb_msg));
2537 result->m_pkthdr.len = 0;
2538 for (m = result; m; m = m->m_next)
2539 result->m_pkthdr.len += m->m_len;
2541 mtod(result, struct sadb_msg *)->sadb_msg_len =
2542 PFKEY_UNIT64(result->m_pkthdr.len);
2552 * get PFKEY message length for security policy and request.
2555 key_getspreqmsglen(sp)
2556 struct secpolicy *sp;
2560 tlen = sizeof(struct sadb_x_policy);
2562 /* if is the policy for ipsec ? */
2563 if (sp->policy != IPSEC_POLICY_IPSEC)
2566 /* get length of ipsec requests */
2568 struct ipsecrequest *isr;
2571 for (isr = sp->req; isr != NULL; isr = isr->next) {
2572 len = sizeof(struct sadb_x_ipsecrequest)
2573 + isr->saidx.src.sa.sa_len
2574 + isr->saidx.dst.sa.sa_len;
2576 tlen += PFKEY_ALIGN8(len);
2584 * SADB_SPDEXPIRE processing
2586 * <base, address(SD), lifetime(CH), policy>
2590 * others : error number
2594 struct secpolicy *sp;
2596 struct mbuf *result = NULL, *m;
2599 struct sadb_lifetime *lt;
2601 /* XXX: Why do we lock ? */
2603 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2605 /* set msg header */
2606 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2613 /* create lifetime extension (current and hard) */
2614 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2615 m = key_alloc_mbuf(len);
2616 if (!m || m->m_next) { /*XXX*/
2622 bzero(mtod(m, caddr_t), len);
2623 lt = mtod(m, struct sadb_lifetime *);
2624 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2625 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2626 lt->sadb_lifetime_allocations = 0;
2627 lt->sadb_lifetime_bytes = 0;
2628 lt->sadb_lifetime_addtime = sp->created;
2629 lt->sadb_lifetime_usetime = sp->lastused;
2630 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2631 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2632 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2633 lt->sadb_lifetime_allocations = 0;
2634 lt->sadb_lifetime_bytes = 0;
2635 lt->sadb_lifetime_addtime = sp->lifetime;
2636 lt->sadb_lifetime_usetime = sp->validtime;
2640 * Note: do not send SADB_X_EXT_NAT_T_* here:
2641 * we are sending traffic endpoints.
2644 /* set sadb_address for source */
2645 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2647 sp->spidx.prefs, sp->spidx.ul_proto);
2654 /* set sadb_address for destination */
2655 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2657 sp->spidx.prefd, sp->spidx.ul_proto);
2672 if ((result->m_flags & M_PKTHDR) == 0) {
2677 if (result->m_len < sizeof(struct sadb_msg)) {
2678 result = m_pullup(result, sizeof(struct sadb_msg));
2679 if (result == NULL) {
2685 result->m_pkthdr.len = 0;
2686 for (m = result; m; m = m->m_next)
2687 result->m_pkthdr.len += m->m_len;
2689 mtod(result, struct sadb_msg *)->sadb_msg_len =
2690 PFKEY_UNIT64(result->m_pkthdr.len);
2692 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2700 /* %%% SAD management */
2702 * allocating a memory for new SA head, and copy from the values of mhp.
2703 * OUT: NULL : failure due to the lack of memory.
2704 * others : pointer to new SA head.
2706 static struct secashead *
2708 struct secasindex *saidx;
2710 struct secashead *newsah;
2712 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
2714 newsah = malloc(sizeof(struct secashead), M_IPSEC_SAH, M_NOWAIT|M_ZERO);
2715 if (newsah != NULL) {
2717 for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++)
2718 LIST_INIT(&newsah->savtree[i]);
2719 newsah->saidx = *saidx;
2721 /* add to saidxtree */
2722 newsah->state = SADB_SASTATE_MATURE;
2725 LIST_INSERT_HEAD(&V_sahtree, newsah, chain);
2732 * delete SA index and all SA registerd.
2736 struct secashead *sah;
2738 struct secasvar *sav, *nextsav;
2742 IPSEC_ASSERT(sah != NULL, ("NULL sah"));
2743 SAHTREE_LOCK_ASSERT();
2745 /* searching all SA registerd in the secindex. */
2747 stateidx < _ARRAYLEN(saorder_state_any);
2749 u_int state = saorder_state_any[stateidx];
2750 LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain, nextsav) {
2751 if (sav->refcnt == 0) {
2753 KEY_CHKSASTATE(state, sav->state, __func__);
2755 * do NOT call KEY_FREESAV here:
2756 * it will only delete the sav if refcnt == 1,
2757 * where we already know that refcnt == 0
2761 /* give up to delete this sa */
2766 if (!zombie) { /* delete only if there are savs */
2767 /* remove from tree of SA index */
2768 if (__LIST_CHAINED(sah))
2769 LIST_REMOVE(sah, chain);
2770 if (sah->sa_route.ro_rt) {
2771 RTFREE(sah->sa_route.ro_rt);
2772 sah->sa_route.ro_rt = (struct rtentry *)NULL;
2774 free(sah, M_IPSEC_SAH);
2779 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2780 * and copy the values of mhp into new buffer.
2781 * When SAD message type is GETSPI:
2782 * to set sequence number from acq_seq++,
2783 * to set zero to SPI.
2784 * not to call key_setsava().
2786 * others : pointer to new secasvar.
2788 * does not modify mbuf. does not free mbuf on error.
2790 static struct secasvar *
2791 key_newsav(m, mhp, sah, errp, where, tag)
2793 const struct sadb_msghdr *mhp;
2794 struct secashead *sah;
2799 struct secasvar *newsav;
2800 const struct sadb_sa *xsa;
2802 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2803 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2804 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2805 IPSEC_ASSERT(sah != NULL, ("null secashead"));
2807 newsav = malloc(sizeof(struct secasvar), M_IPSEC_SA, M_NOWAIT|M_ZERO);
2808 if (newsav == NULL) {
2809 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2814 switch (mhp->msg->sadb_msg_type) {
2818 #ifdef IPSEC_DOSEQCHECK
2819 /* sync sequence number */
2820 if (mhp->msg->sadb_msg_seq == 0)
2822 (V_acq_seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq));
2825 newsav->seq = mhp->msg->sadb_msg_seq;
2830 if (mhp->ext[SADB_EXT_SA] == NULL) {
2831 free(newsav, M_IPSEC_SA);
2833 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2838 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2839 newsav->spi = xsa->sadb_sa_spi;
2840 newsav->seq = mhp->msg->sadb_msg_seq;
2843 free(newsav, M_IPSEC_SA);
2850 /* copy sav values */
2851 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2852 *errp = key_setsaval(newsav, m, mhp);
2854 free(newsav, M_IPSEC_SA);
2860 SECASVAR_LOCK_INIT(newsav);
2863 newsav->created = time_second;
2864 newsav->pid = mhp->msg->sadb_msg_pid;
2869 newsav->state = SADB_SASTATE_LARVAL;
2871 /* XXX locking??? */
2872 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2875 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
2876 printf("DP %s from %s:%u return SP:%p\n", __func__,
2877 where, tag, newsav));
2883 * free() SA variable entry.
2886 key_cleansav(struct secasvar *sav)
2889 * Cleanup xform state. Note that zeroize'ing causes the
2890 * keys to be cleared; otherwise we must do it ourself.
2892 if (sav->tdb_xform != NULL) {
2893 sav->tdb_xform->xf_zeroize(sav);
2894 sav->tdb_xform = NULL;
2896 KASSERT(sav->iv == NULL, ("iv but no xform"));
2897 if (sav->key_auth != NULL)
2898 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
2899 if (sav->key_enc != NULL)
2900 bzero(sav->key_enc->key_data, _KEYLEN(sav->key_enc));
2902 if (sav->key_auth != NULL) {
2903 if (sav->key_auth->key_data != NULL)
2904 free(sav->key_auth->key_data, M_IPSEC_MISC);
2905 free(sav->key_auth, M_IPSEC_MISC);
2906 sav->key_auth = NULL;
2908 if (sav->key_enc != NULL) {
2909 if (sav->key_enc->key_data != NULL)
2910 free(sav->key_enc->key_data, M_IPSEC_MISC);
2911 free(sav->key_enc, M_IPSEC_MISC);
2912 sav->key_enc = NULL;
2915 bzero(sav->sched, sav->schedlen);
2916 free(sav->sched, M_IPSEC_MISC);
2919 if (sav->replay != NULL) {
2920 free(sav->replay, M_IPSEC_MISC);
2923 if (sav->lft_c != NULL) {
2924 free(sav->lft_c, M_IPSEC_MISC);
2927 if (sav->lft_h != NULL) {
2928 free(sav->lft_h, M_IPSEC_MISC);
2931 if (sav->lft_s != NULL) {
2932 free(sav->lft_s, M_IPSEC_MISC);
2938 * free() SA variable entry.
2942 struct secasvar *sav;
2944 IPSEC_ASSERT(sav != NULL, ("null sav"));
2945 IPSEC_ASSERT(sav->refcnt == 0, ("reference count %u > 0", sav->refcnt));
2947 /* remove from SA header */
2948 if (__LIST_CHAINED(sav))
2949 LIST_REMOVE(sav, chain);
2951 SECASVAR_LOCK_DESTROY(sav);
2952 free(sav, M_IPSEC_SA);
2959 * others : found, pointer to a SA.
2961 static struct secashead *
2963 struct secasindex *saidx;
2965 struct secashead *sah;
2968 LIST_FOREACH(sah, &V_sahtree, chain) {
2969 if (sah->state == SADB_SASTATE_DEAD)
2971 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2980 * check not to be duplicated SPI.
2981 * NOTE: this function is too slow due to searching all SAD.
2984 * others : found, pointer to a SA.
2986 static struct secasvar *
2987 key_checkspidup(saidx, spi)
2988 struct secasindex *saidx;
2991 struct secashead *sah;
2992 struct secasvar *sav;
2994 /* check address family */
2995 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
2996 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
3004 LIST_FOREACH(sah, &V_sahtree, chain) {
3005 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
3007 sav = key_getsavbyspi(sah, spi);
3017 * search SAD litmited alive SA, protocol, SPI.
3020 * others : found, pointer to a SA.
3022 static struct secasvar *
3023 key_getsavbyspi(sah, spi)
3024 struct secashead *sah;
3027 struct secasvar *sav;
3028 u_int stateidx, state;
3031 SAHTREE_LOCK_ASSERT();
3032 /* search all status */
3034 stateidx < _ARRAYLEN(saorder_state_alive);
3037 state = saorder_state_alive[stateidx];
3038 LIST_FOREACH(sav, &sah->savtree[state], chain) {
3041 if (sav->state != state) {
3042 ipseclog((LOG_DEBUG, "%s: "
3043 "invalid sav->state (queue: %d SA: %d)\n",
3044 __func__, state, sav->state));
3048 if (sav->spi == spi)
3057 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
3058 * You must update these if need.
3062 * does not modify mbuf. does not free mbuf on error.
3065 key_setsaval(sav, m, mhp)
3066 struct secasvar *sav;
3068 const struct sadb_msghdr *mhp;
3072 IPSEC_ASSERT(m != NULL, ("null mbuf"));
3073 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
3074 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
3076 /* initialization */
3078 sav->key_auth = NULL;
3079 sav->key_enc = NULL;
3086 sav->tdb_xform = NULL; /* transform */
3087 sav->tdb_encalgxform = NULL; /* encoding algorithm */
3088 sav->tdb_authalgxform = NULL; /* authentication algorithm */
3089 sav->tdb_compalgxform = NULL; /* compression algorithm */
3090 /* Initialize even if NAT-T not compiled in: */
3092 sav->natt_esp_frag_len = 0;
3095 if (mhp->ext[SADB_EXT_SA] != NULL) {
3096 const struct sadb_sa *sa0;
3098 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
3099 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
3104 sav->alg_auth = sa0->sadb_sa_auth;
3105 sav->alg_enc = sa0->sadb_sa_encrypt;
3106 sav->flags = sa0->sadb_sa_flags;
3109 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
3110 sav->replay = (struct secreplay *)
3111 malloc(sizeof(struct secreplay)+sa0->sadb_sa_replay, M_IPSEC_MISC, M_NOWAIT|M_ZERO);
3112 if (sav->replay == NULL) {
3113 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3118 if (sa0->sadb_sa_replay != 0)
3119 sav->replay->bitmap = (caddr_t)(sav->replay+1);
3120 sav->replay->wsize = sa0->sadb_sa_replay;
3124 /* Authentication keys */
3125 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
3126 const struct sadb_key *key0;
3129 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
3130 len = mhp->extlen[SADB_EXT_KEY_AUTH];
3133 if (len < sizeof(*key0)) {
3137 switch (mhp->msg->sadb_msg_satype) {
3138 case SADB_SATYPE_AH:
3139 case SADB_SATYPE_ESP:
3140 case SADB_X_SATYPE_TCPSIGNATURE:
3141 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3142 sav->alg_auth != SADB_X_AALG_NULL)
3145 case SADB_X_SATYPE_IPCOMP:
3151 ipseclog((LOG_DEBUG, "%s: invalid key_auth values.\n",
3156 sav->key_auth = (struct seckey *)key_dup_keymsg(key0, len,
3158 if (sav->key_auth == NULL ) {
3159 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3166 /* Encryption key */
3167 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3168 const struct sadb_key *key0;
3171 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3172 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3175 if (len < sizeof(*key0)) {
3179 switch (mhp->msg->sadb_msg_satype) {
3180 case SADB_SATYPE_ESP:
3181 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3182 sav->alg_enc != SADB_EALG_NULL) {
3186 sav->key_enc = (struct seckey *)key_dup_keymsg(key0,
3189 if (sav->key_enc == NULL) {
3190 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3196 case SADB_X_SATYPE_IPCOMP:
3197 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3199 sav->key_enc = NULL; /*just in case*/
3201 case SADB_SATYPE_AH:
3202 case SADB_X_SATYPE_TCPSIGNATURE:
3208 ipseclog((LOG_DEBUG, "%s: invalid key_enc value.\n",
3217 switch (mhp->msg->sadb_msg_satype) {
3218 case SADB_SATYPE_AH:
3219 error = xform_init(sav, XF_AH);
3221 case SADB_SATYPE_ESP:
3222 error = xform_init(sav, XF_ESP);
3224 case SADB_X_SATYPE_IPCOMP:
3225 error = xform_init(sav, XF_IPCOMP);
3227 case SADB_X_SATYPE_TCPSIGNATURE:
3228 error = xform_init(sav, XF_TCPSIGNATURE);
3232 ipseclog((LOG_DEBUG, "%s: unable to initialize SA type %u.\n",
3233 __func__, mhp->msg->sadb_msg_satype));
3238 sav->created = time_second;
3240 /* make lifetime for CURRENT */
3241 sav->lft_c = malloc(sizeof(struct seclifetime), M_IPSEC_MISC, M_NOWAIT);
3242 if (sav->lft_c == NULL) {
3243 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3248 sav->lft_c->allocations = 0;
3249 sav->lft_c->bytes = 0;
3250 sav->lft_c->addtime = time_second;
3251 sav->lft_c->usetime = 0;
3253 /* lifetimes for HARD and SOFT */
3255 const struct sadb_lifetime *lft0;
3257 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3259 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3263 sav->lft_h = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3264 if (sav->lft_h == NULL) {
3265 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3269 /* to be initialize ? */
3272 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3274 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3278 sav->lft_s = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3279 if (sav->lft_s == NULL) {
3280 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3284 /* to be initialize ? */
3291 /* initialization */
3298 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3303 key_mature(struct secasvar *sav)
3307 /* check SPI value */
3308 switch (sav->sah->saidx.proto) {
3312 * RFC 4302, 2.4. Security Parameters Index (SPI), SPI values
3313 * 1-255 reserved by IANA for future use,
3314 * 0 for implementation specific, local use.
3316 if (ntohl(sav->spi) <= 255) {
3317 ipseclog((LOG_DEBUG, "%s: illegal range of SPI %u.\n",
3318 __func__, (u_int32_t)ntohl(sav->spi)));
3325 switch (sav->sah->saidx.proto) {
3328 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3329 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3330 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3331 "given to old-esp.\n", __func__));
3334 error = xform_init(sav, XF_ESP);
3338 if (sav->flags & SADB_X_EXT_DERIV) {
3339 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3340 "given to AH SA.\n", __func__));
3343 if (sav->alg_enc != SADB_EALG_NONE) {
3344 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3345 "mismated.\n", __func__));
3348 error = xform_init(sav, XF_AH);
3350 case IPPROTO_IPCOMP:
3351 if (sav->alg_auth != SADB_AALG_NONE) {
3352 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3353 "mismated.\n", __func__));
3356 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3357 && ntohl(sav->spi) >= 0x10000) {
3358 ipseclog((LOG_DEBUG, "%s: invalid cpi for IPComp.\n",
3362 error = xform_init(sav, XF_IPCOMP);
3365 if (sav->alg_enc != SADB_EALG_NONE) {
3366 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3367 "mismated.\n", __func__));
3370 error = xform_init(sav, XF_TCPSIGNATURE);
3373 ipseclog((LOG_DEBUG, "%s: Invalid satype.\n", __func__));
3374 error = EPROTONOSUPPORT;
3379 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3386 * subroutine for SADB_GET and SADB_DUMP.
3388 static struct mbuf *
3389 key_setdumpsa(struct secasvar *sav, u_int8_t type, u_int8_t satype,
3390 u_int32_t seq, u_int32_t pid)
3392 struct mbuf *result = NULL, *tres = NULL, *m;
3395 SADB_EXT_SA, SADB_X_EXT_SA2,
3396 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3397 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3398 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3399 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3400 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3402 SADB_X_EXT_NAT_T_TYPE,
3403 SADB_X_EXT_NAT_T_SPORT, SADB_X_EXT_NAT_T_DPORT,
3404 SADB_X_EXT_NAT_T_OAI, SADB_X_EXT_NAT_T_OAR,
3405 SADB_X_EXT_NAT_T_FRAG,
3409 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3414 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3416 switch (dumporder[i]) {
3418 m = key_setsadbsa(sav);
3423 case SADB_X_EXT_SA2:
3424 m = key_setsadbxsa2(sav->sah->saidx.mode,
3425 sav->replay ? sav->replay->count : 0,
3426 sav->sah->saidx.reqid);
3431 case SADB_EXT_ADDRESS_SRC:
3432 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3433 &sav->sah->saidx.src.sa,
3434 FULLMASK, IPSEC_ULPROTO_ANY);
3439 case SADB_EXT_ADDRESS_DST:
3440 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3441 &sav->sah->saidx.dst.sa,
3442 FULLMASK, IPSEC_ULPROTO_ANY);
3447 case SADB_EXT_KEY_AUTH:
3450 m = key_setkey(sav->key_auth, SADB_EXT_KEY_AUTH);
3455 case SADB_EXT_KEY_ENCRYPT:
3458 m = key_setkey(sav->key_enc, SADB_EXT_KEY_ENCRYPT);
3463 case SADB_EXT_LIFETIME_CURRENT:
3466 m = key_setlifetime(sav->lft_c,
3467 SADB_EXT_LIFETIME_CURRENT);
3472 case SADB_EXT_LIFETIME_HARD:
3475 m = key_setlifetime(sav->lft_h,
3476 SADB_EXT_LIFETIME_HARD);
3481 case SADB_EXT_LIFETIME_SOFT:
3484 m = key_setlifetime(sav->lft_s,
3485 SADB_EXT_LIFETIME_SOFT);
3492 case SADB_X_EXT_NAT_T_TYPE:
3493 m = key_setsadbxtype(sav->natt_type);
3498 case SADB_X_EXT_NAT_T_DPORT:
3499 m = key_setsadbxport(
3500 KEY_PORTFROMSADDR(&sav->sah->saidx.dst),
3501 SADB_X_EXT_NAT_T_DPORT);
3506 case SADB_X_EXT_NAT_T_SPORT:
3507 m = key_setsadbxport(
3508 KEY_PORTFROMSADDR(&sav->sah->saidx.src),
3509 SADB_X_EXT_NAT_T_SPORT);
3514 case SADB_X_EXT_NAT_T_OAI:
3515 case SADB_X_EXT_NAT_T_OAR:
3516 case SADB_X_EXT_NAT_T_FRAG:
3517 /* We do not (yet) support those. */
3521 case SADB_EXT_ADDRESS_PROXY:
3522 case SADB_EXT_IDENTITY_SRC:
3523 case SADB_EXT_IDENTITY_DST:
3524 /* XXX: should we brought from SPD ? */
3525 case SADB_EXT_SENSITIVITY:
3538 m_cat(result, tres);
3539 if (result->m_len < sizeof(struct sadb_msg)) {
3540 result = m_pullup(result, sizeof(struct sadb_msg));
3545 result->m_pkthdr.len = 0;
3546 for (m = result; m; m = m->m_next)
3547 result->m_pkthdr.len += m->m_len;
3549 mtod(result, struct sadb_msg *)->sadb_msg_len =
3550 PFKEY_UNIT64(result->m_pkthdr.len);
3561 * set data into sadb_msg.
3563 static struct mbuf *
3564 key_setsadbmsg(u_int8_t type, u_int16_t tlen, u_int8_t satype, u_int32_t seq,
3565 pid_t pid, u_int16_t reserved)
3571 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3574 MGETHDR(m, M_DONTWAIT, MT_DATA);
3575 if (m && len > MHLEN) {
3576 MCLGET(m, M_DONTWAIT);
3577 if ((m->m_flags & M_EXT) == 0) {
3584 m->m_pkthdr.len = m->m_len = len;
3587 p = mtod(m, struct sadb_msg *);
3590 p->sadb_msg_version = PF_KEY_V2;
3591 p->sadb_msg_type = type;
3592 p->sadb_msg_errno = 0;
3593 p->sadb_msg_satype = satype;
3594 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3595 p->sadb_msg_reserved = reserved;
3596 p->sadb_msg_seq = seq;
3597 p->sadb_msg_pid = (u_int32_t)pid;
3603 * copy secasvar data into sadb_address.
3605 static struct mbuf *
3607 struct secasvar *sav;
3613 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3614 m = key_alloc_mbuf(len);
3615 if (!m || m->m_next) { /*XXX*/
3621 p = mtod(m, struct sadb_sa *);
3624 p->sadb_sa_len = PFKEY_UNIT64(len);
3625 p->sadb_sa_exttype = SADB_EXT_SA;
3626 p->sadb_sa_spi = sav->spi;
3627 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3628 p->sadb_sa_state = sav->state;
3629 p->sadb_sa_auth = sav->alg_auth;
3630 p->sadb_sa_encrypt = sav->alg_enc;
3631 p->sadb_sa_flags = sav->flags;
3637 * set data into sadb_address.
3639 static struct mbuf *
3640 key_setsadbaddr(u_int16_t exttype, const struct sockaddr *saddr, u_int8_t prefixlen, u_int16_t ul_proto)
3643 struct sadb_address *p;
3646 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3647 PFKEY_ALIGN8(saddr->sa_len);
3648 m = key_alloc_mbuf(len);
3649 if (!m || m->m_next) { /*XXX*/
3655 p = mtod(m, struct sadb_address *);
3658 p->sadb_address_len = PFKEY_UNIT64(len);
3659 p->sadb_address_exttype = exttype;
3660 p->sadb_address_proto = ul_proto;
3661 if (prefixlen == FULLMASK) {
3662 switch (saddr->sa_family) {
3664 prefixlen = sizeof(struct in_addr) << 3;
3667 prefixlen = sizeof(struct in6_addr) << 3;
3673 p->sadb_address_prefixlen = prefixlen;
3674 p->sadb_address_reserved = 0;
3677 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3684 * set data into sadb_x_sa2.
3686 static struct mbuf *
3687 key_setsadbxsa2(u_int8_t mode, u_int32_t seq, u_int32_t reqid)
3690 struct sadb_x_sa2 *p;
3693 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3694 m = key_alloc_mbuf(len);
3695 if (!m || m->m_next) { /*XXX*/
3701 p = mtod(m, struct sadb_x_sa2 *);
3704 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3705 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3706 p->sadb_x_sa2_mode = mode;
3707 p->sadb_x_sa2_reserved1 = 0;
3708 p->sadb_x_sa2_reserved2 = 0;
3709 p->sadb_x_sa2_sequence = seq;
3710 p->sadb_x_sa2_reqid = reqid;
3717 * Set a type in sadb_x_nat_t_type.
3719 static struct mbuf *
3720 key_setsadbxtype(u_int16_t type)
3724 struct sadb_x_nat_t_type *p;
3726 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_type));
3728 m = key_alloc_mbuf(len);
3729 if (!m || m->m_next) { /*XXX*/
3735 p = mtod(m, struct sadb_x_nat_t_type *);
3738 p->sadb_x_nat_t_type_len = PFKEY_UNIT64(len);
3739 p->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
3740 p->sadb_x_nat_t_type_type = type;
3745 * Set a port in sadb_x_nat_t_port.
3746 * In contrast to default RFC 2367 behaviour, port is in network byte order.
3748 static struct mbuf *
3749 key_setsadbxport(u_int16_t port, u_int16_t type)
3753 struct sadb_x_nat_t_port *p;
3755 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_port));
3757 m = key_alloc_mbuf(len);
3758 if (!m || m->m_next) { /*XXX*/
3764 p = mtod(m, struct sadb_x_nat_t_port *);
3767 p->sadb_x_nat_t_port_len = PFKEY_UNIT64(len);
3768 p->sadb_x_nat_t_port_exttype = type;
3769 p->sadb_x_nat_t_port_port = port;
3775 * Get port from sockaddr. Port is in network byte order.
3778 key_portfromsaddr(struct sockaddr *sa)
3781 switch (sa->sa_family) {
3784 return ((struct sockaddr_in *)sa)->sin_port;
3788 return ((struct sockaddr_in6 *)sa)->sin6_port;
3791 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
3792 printf("DP %s unexpected address family %d\n",
3793 __func__, sa->sa_family));
3796 #endif /* IPSEC_NAT_T */
3799 * Set port in struct sockaddr. Port is in network byte order.
3802 key_porttosaddr(struct sockaddr *sa, u_int16_t port)
3805 switch (sa->sa_family) {
3808 ((struct sockaddr_in *)sa)->sin_port = port;
3813 ((struct sockaddr_in6 *)sa)->sin6_port = port;
3817 ipseclog((LOG_DEBUG, "%s: unexpected address family %d.\n",
3818 __func__, sa->sa_family));
3824 * set data into sadb_x_policy
3826 static struct mbuf *
3827 key_setsadbxpolicy(u_int16_t type, u_int8_t dir, u_int32_t id)
3830 struct sadb_x_policy *p;
3833 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3834 m = key_alloc_mbuf(len);
3835 if (!m || m->m_next) { /*XXX*/
3841 p = mtod(m, struct sadb_x_policy *);
3844 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3845 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3846 p->sadb_x_policy_type = type;
3847 p->sadb_x_policy_dir = dir;
3848 p->sadb_x_policy_id = id;
3854 /* Take a key message (sadb_key) from the socket and turn it into one
3855 * of the kernel's key structures (seckey).
3857 * IN: pointer to the src
3858 * OUT: NULL no more memory
3861 key_dup_keymsg(const struct sadb_key *src, u_int len,
3862 struct malloc_type *type)
3865 dst = (struct seckey *)malloc(sizeof(struct seckey), type, M_NOWAIT);
3867 dst->bits = src->sadb_key_bits;
3868 dst->key_data = (char *)malloc(len, type, M_NOWAIT);
3869 if (dst->key_data != NULL) {
3870 bcopy((const char *)src + sizeof(struct sadb_key),
3871 dst->key_data, len);
3873 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3879 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3886 /* Take a lifetime message (sadb_lifetime) passed in on a socket and
3887 * turn it into one of the kernel's lifetime structures (seclifetime).
3889 * IN: pointer to the destination, source and malloc type
3890 * OUT: NULL, no more memory
3893 static struct seclifetime *
3894 key_dup_lifemsg(const struct sadb_lifetime *src,
3895 struct malloc_type *type)
3897 struct seclifetime *dst = NULL;
3899 dst = (struct seclifetime *)malloc(sizeof(struct seclifetime),
3903 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3905 dst->allocations = src->sadb_lifetime_allocations;
3906 dst->bytes = src->sadb_lifetime_bytes;
3907 dst->addtime = src->sadb_lifetime_addtime;
3908 dst->usetime = src->sadb_lifetime_usetime;
3913 /* compare my own address
3914 * OUT: 1: true, i.e. my address.
3919 struct sockaddr *sa;
3922 struct sockaddr_in *sin;
3923 struct in_ifaddr *ia;
3926 IPSEC_ASSERT(sa != NULL, ("null sockaddr"));
3928 switch (sa->sa_family) {
3931 sin = (struct sockaddr_in *)sa;
3933 for (ia = V_in_ifaddrhead.tqh_first; ia;
3934 ia = ia->ia_link.tqe_next)
3936 if (sin->sin_family == ia->ia_addr.sin_family &&
3937 sin->sin_len == ia->ia_addr.sin_len &&
3938 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3940 IN_IFADDR_RUNLOCK();
3944 IN_IFADDR_RUNLOCK();
3949 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3958 * compare my own address for IPv6.
3961 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3963 #include <netinet6/in6_var.h>
3967 struct sockaddr_in6 *sin6;
3969 struct in6_ifaddr *ia;
3971 struct in6_multi *in6m;
3975 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
3976 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3977 (struct sockaddr *)&ia->ia_addr, 0) == 0) {
3978 IN6_IFADDR_RUNLOCK();
3985 * XXX why do we care about multlicast here while we don't care
3986 * about IPv4 multicast??
3990 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3992 IN6_IFADDR_RUNLOCK();
3997 IN6_IFADDR_RUNLOCK();
3999 /* loopback, just for safety */
4000 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
4008 * compare two secasindex structure.
4009 * flag can specify to compare 2 saidxes.
4010 * compare two secasindex structure without both mode and reqid.
4011 * don't compare port.
4013 * saidx0: source, it can be in SAD.
4021 const struct secasindex *saidx0,
4022 const struct secasindex *saidx1,
4028 if (saidx0 == NULL && saidx1 == NULL)
4031 if (saidx0 == NULL || saidx1 == NULL)
4034 if (saidx0->proto != saidx1->proto)
4037 if (flag == CMP_EXACTLY) {
4038 if (saidx0->mode != saidx1->mode)
4040 if (saidx0->reqid != saidx1->reqid)
4042 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
4043 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
4047 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
4048 if (flag == CMP_MODE_REQID
4049 ||flag == CMP_REQID) {
4051 * If reqid of SPD is non-zero, unique SA is required.
4052 * The result must be of same reqid in this case.
4054 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
4058 if (flag == CMP_MODE_REQID) {
4059 if (saidx0->mode != IPSEC_MODE_ANY
4060 && saidx0->mode != saidx1->mode)
4066 * If NAT-T is enabled, check ports for tunnel mode.
4067 * Do not check ports if they are set to zero in the SPD.
4068 * Also do not do it for transport mode, as there is no
4069 * port information available in the SP.
4071 if (saidx1->mode == IPSEC_MODE_TUNNEL &&
4072 saidx1->src.sa.sa_family == AF_INET &&
4073 saidx1->dst.sa.sa_family == AF_INET &&
4074 ((const struct sockaddr_in *)(&saidx1->src))->sin_port &&
4075 ((const struct sockaddr_in *)(&saidx1->dst))->sin_port)
4077 #endif /* IPSEC_NAT_T */
4079 if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, chkport) != 0) {
4082 if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, chkport) != 0) {
4091 * compare two secindex structure exactly.
4093 * spidx0: source, it is often in SPD.
4094 * spidx1: object, it is often from PFKEY message.
4100 key_cmpspidx_exactly(
4101 struct secpolicyindex *spidx0,
4102 struct secpolicyindex *spidx1)
4105 if (spidx0 == NULL && spidx1 == NULL)
4108 if (spidx0 == NULL || spidx1 == NULL)
4111 if (spidx0->prefs != spidx1->prefs
4112 || spidx0->prefd != spidx1->prefd
4113 || spidx0->ul_proto != spidx1->ul_proto)
4116 return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
4117 key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
4121 * compare two secindex structure with mask.
4123 * spidx0: source, it is often in SPD.
4124 * spidx1: object, it is often from IP header.
4130 key_cmpspidx_withmask(
4131 struct secpolicyindex *spidx0,
4132 struct secpolicyindex *spidx1)
4135 if (spidx0 == NULL && spidx1 == NULL)
4138 if (spidx0 == NULL || spidx1 == NULL)
4141 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
4142 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
4143 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
4144 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
4147 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4148 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
4149 && spidx0->ul_proto != spidx1->ul_proto)
4152 switch (spidx0->src.sa.sa_family) {
4154 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
4155 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
4157 if (!key_bbcmp(&spidx0->src.sin.sin_addr,
4158 &spidx1->src.sin.sin_addr, spidx0->prefs))
4162 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
4163 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
4166 * scope_id check. if sin6_scope_id is 0, we regard it
4167 * as a wildcard scope, which matches any scope zone ID.
4169 if (spidx0->src.sin6.sin6_scope_id &&
4170 spidx1->src.sin6.sin6_scope_id &&
4171 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
4173 if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
4174 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
4179 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
4184 switch (spidx0->dst.sa.sa_family) {
4186 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
4187 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
4189 if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
4190 &spidx1->dst.sin.sin_addr, spidx0->prefd))
4194 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
4195 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
4198 * scope_id check. if sin6_scope_id is 0, we regard it
4199 * as a wildcard scope, which matches any scope zone ID.
4201 if (spidx0->dst.sin6.sin6_scope_id &&
4202 spidx1->dst.sin6.sin6_scope_id &&
4203 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
4205 if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
4206 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
4211 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
4216 /* XXX Do we check other field ? e.g. flowinfo */
4221 /* returns 0 on match */
4224 const struct sockaddr *sa1,
4225 const struct sockaddr *sa2,
4231 #define satosin(s) ((const struct sockaddr_in *)s)
4235 #define satosin6(s) ((const struct sockaddr_in6 *)s)
4236 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
4239 switch (sa1->sa_family) {
4241 if (sa1->sa_len != sizeof(struct sockaddr_in))
4243 if (satosin(sa1)->sin_addr.s_addr !=
4244 satosin(sa2)->sin_addr.s_addr) {
4247 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
4251 if (sa1->sa_len != sizeof(struct sockaddr_in6))
4252 return 1; /*EINVAL*/
4253 if (satosin6(sa1)->sin6_scope_id !=
4254 satosin6(sa2)->sin6_scope_id) {
4257 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
4258 &satosin6(sa2)->sin6_addr)) {
4262 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
4267 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
4278 * compare two buffers with mask.
4282 * bits: Number of bits to compare
4288 key_bbcmp(const void *a1, const void *a2, u_int bits)
4290 const unsigned char *p1 = a1;
4291 const unsigned char *p2 = a2;
4293 /* XXX: This could be considerably faster if we compare a word
4294 * at a time, but it is complicated on LSB Endian machines */
4296 /* Handle null pointers */
4297 if (p1 == NULL || p2 == NULL)
4307 u_int8_t mask = ~((1<<(8-bits))-1);
4308 if ((*p1 & mask) != (*p2 & mask))
4311 return 1; /* Match! */
4315 key_flush_spd(time_t now)
4317 static u_int16_t sptree_scangen = 0;
4318 u_int16_t gen = sptree_scangen++;
4319 struct secpolicy *sp;
4323 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4326 LIST_FOREACH(sp, &V_sptree[dir], chain) {
4327 if (sp->scangen == gen) /* previously handled */
4330 if (sp->state == IPSEC_SPSTATE_DEAD &&
4333 * Ensure that we only decrease refcnt once,
4334 * when we're the last consumer.
4335 * Directly call SP_DELREF/key_delsp instead
4336 * of KEY_FREESP to avoid unlocking/relocking
4337 * SPTREE_LOCK before key_delsp: may refcnt
4338 * be increased again during that time ?
4339 * NB: also clean entries created by
4347 if (sp->lifetime == 0 && sp->validtime == 0)
4349 if ((sp->lifetime && now - sp->created > sp->lifetime)
4350 || (sp->validtime && now - sp->lastused > sp->validtime)) {
4351 sp->state = IPSEC_SPSTATE_DEAD;
4362 key_flush_sad(time_t now)
4364 struct secashead *sah, *nextsah;
4365 struct secasvar *sav, *nextsav;
4369 LIST_FOREACH_SAFE(sah, &V_sahtree, chain, nextsah) {
4370 /* if sah has been dead, then delete it and process next sah. */
4371 if (sah->state == SADB_SASTATE_DEAD) {
4376 /* if LARVAL entry doesn't become MATURE, delete it. */
4377 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_LARVAL], chain, nextsav) {
4378 /* Need to also check refcnt for a larval SA ??? */
4379 if (now - sav->created > V_key_larval_lifetime)
4384 * check MATURE entry to start to send expire message
4387 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_MATURE], chain, nextsav) {
4388 /* we don't need to check. */
4389 if (sav->lft_s == NULL)
4393 if (sav->lft_c == NULL) {
4394 ipseclog((LOG_DEBUG,"%s: there is no CURRENT "
4395 "time, why?\n", __func__));
4399 /* check SOFT lifetime */
4400 if (sav->lft_s->addtime != 0 &&
4401 now - sav->created > sav->lft_s->addtime) {
4402 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4404 * Actually, only send expire message if
4405 * SA has been used, as it was done before,
4406 * but should we always send such message,
4407 * and let IKE daemon decide if it should be
4408 * renegotiated or not ?
4409 * XXX expire message will actually NOT be
4410 * sent if SA is only used after soft
4411 * lifetime has been reached, see below
4414 if (sav->lft_c->usetime != 0)
4417 /* check SOFT lifetime by bytes */
4419 * XXX I don't know the way to delete this SA
4420 * when new SA is installed. Caution when it's
4421 * installed too big lifetime by time.
4423 else if (sav->lft_s->bytes != 0 &&
4424 sav->lft_s->bytes < sav->lft_c->bytes) {
4426 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4428 * XXX If we keep to send expire
4429 * message in the status of
4430 * DYING. Do remove below code.
4436 /* check DYING entry to change status to DEAD. */
4437 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DYING], chain, nextsav) {
4438 /* we don't need to check. */
4439 if (sav->lft_h == NULL)
4443 if (sav->lft_c == NULL) {
4444 ipseclog((LOG_DEBUG, "%s: there is no CURRENT "
4445 "time, why?\n", __func__));
4449 if (sav->lft_h->addtime != 0 &&
4450 now - sav->created > sav->lft_h->addtime) {
4451 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4454 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4455 else if (sav->lft_s != NULL
4456 && sav->lft_s->addtime != 0
4457 && now - sav->created > sav->lft_s->addtime) {
4459 * XXX: should be checked to be
4460 * installed the valid SA.
4464 * If there is no SA then sending
4470 /* check HARD lifetime by bytes */
4471 else if (sav->lft_h->bytes != 0 &&
4472 sav->lft_h->bytes < sav->lft_c->bytes) {
4473 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4478 /* delete entry in DEAD */
4479 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DEAD], chain, nextsav) {
4481 if (sav->state != SADB_SASTATE_DEAD) {
4482 ipseclog((LOG_DEBUG, "%s: invalid sav->state "
4483 "(queue: %d SA: %d): kill it anyway\n",
4485 SADB_SASTATE_DEAD, sav->state));
4488 * do not call key_freesav() here.
4489 * sav should already be freed, and sav->refcnt
4490 * shows other references to sav
4491 * (such as from SPD).
4499 key_flush_acq(time_t now)
4501 struct secacq *acq, *nextacq;
4505 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
4506 nextacq = LIST_NEXT(acq, chain);
4507 if (now - acq->created > V_key_blockacq_lifetime
4508 && __LIST_CHAINED(acq)) {
4509 LIST_REMOVE(acq, chain);
4510 free(acq, M_IPSEC_SAQ);
4517 key_flush_spacq(time_t now)
4519 struct secspacq *acq, *nextacq;
4523 for (acq = LIST_FIRST(&V_spacqtree); acq != NULL; acq = nextacq) {
4524 nextacq = LIST_NEXT(acq, chain);
4525 if (now - acq->created > V_key_blockacq_lifetime
4526 && __LIST_CHAINED(acq)) {
4527 LIST_REMOVE(acq, chain);
4528 free(acq, M_IPSEC_SAQ);
4536 * scanning SPD and SAD to check status for each entries,
4537 * and do to remove or to expire.
4538 * XXX: year 2038 problem may remain.
4541 key_timehandler(void)
4543 VNET_ITERATOR_DECL(vnet_iter);
4544 time_t now = time_second;
4546 VNET_LIST_RLOCK_NOSLEEP();
4547 VNET_FOREACH(vnet_iter) {
4548 CURVNET_SET(vnet_iter);
4552 key_flush_spacq(now);
4555 VNET_LIST_RUNLOCK_NOSLEEP();
4557 #ifndef IPSEC_DEBUG2
4558 /* do exchange to tick time !! */
4559 (void)timeout((void *)key_timehandler, (void *)0, hz);
4560 #endif /* IPSEC_DEBUG2 */
4568 key_randomfill(&value, sizeof(value));
4573 key_randomfill(p, l)
4579 static int warn = 1;
4582 n = (size_t)read_random(p, (u_int)l);
4586 bcopy(&v, (u_int8_t *)p + n,
4587 l - n < sizeof(v) ? l - n : sizeof(v));
4591 printf("WARNING: pseudo-random number generator "
4592 "used for IPsec processing\n");
4599 * map SADB_SATYPE_* to IPPROTO_*.
4600 * if satype == SADB_SATYPE then satype is mapped to ~0.
4602 * 0: invalid satype.
4605 key_satype2proto(u_int8_t satype)
4608 case SADB_SATYPE_UNSPEC:
4609 return IPSEC_PROTO_ANY;
4610 case SADB_SATYPE_AH:
4612 case SADB_SATYPE_ESP:
4614 case SADB_X_SATYPE_IPCOMP:
4615 return IPPROTO_IPCOMP;
4616 case SADB_X_SATYPE_TCPSIGNATURE:
4625 * map IPPROTO_* to SADB_SATYPE_*
4627 * 0: invalid protocol type.
4630 key_proto2satype(u_int16_t proto)
4634 return SADB_SATYPE_AH;
4636 return SADB_SATYPE_ESP;
4637 case IPPROTO_IPCOMP:
4638 return SADB_X_SATYPE_IPCOMP;
4640 return SADB_X_SATYPE_TCPSIGNATURE;
4649 * SADB_GETSPI processing is to receive
4650 * <base, (SA2), src address, dst address, (SPI range)>
4651 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4652 * tree with the status of LARVAL, and send
4653 * <base, SA(*), address(SD)>
4656 * IN: mhp: pointer to the pointer to each header.
4657 * OUT: NULL if fail.
4658 * other if success, return pointer to the message to send.
4661 key_getspi(so, m, mhp)
4664 const struct sadb_msghdr *mhp;
4666 struct sadb_address *src0, *dst0;
4667 struct secasindex saidx;
4668 struct secashead *newsah;
4669 struct secasvar *newsav;
4676 IPSEC_ASSERT(so != NULL, ("null socket"));
4677 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4678 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4679 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4681 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4682 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4683 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4685 return key_senderror(so, m, EINVAL);
4687 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4688 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4689 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4691 return key_senderror(so, m, EINVAL);
4693 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4694 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4695 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4697 mode = IPSEC_MODE_ANY;
4701 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4702 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4704 /* map satype to proto */
4705 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4706 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4708 return key_senderror(so, m, EINVAL);
4712 * Make sure the port numbers are zero.
4713 * In case of NAT-T we will update them later if needed.
4715 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4717 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4718 sizeof(struct sockaddr_in))
4719 return key_senderror(so, m, EINVAL);
4720 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4723 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4724 sizeof(struct sockaddr_in6))
4725 return key_senderror(so, m, EINVAL);
4726 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4731 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4733 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4734 sizeof(struct sockaddr_in))
4735 return key_senderror(so, m, EINVAL);
4736 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4739 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4740 sizeof(struct sockaddr_in6))
4741 return key_senderror(so, m, EINVAL);
4742 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4748 /* XXX boundary check against sa_len */
4749 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4753 * Handle NAT-T info if present.
4754 * We made sure the port numbers are zero above, so we do
4755 * not have to worry in case we do not update them.
4757 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL)
4758 ipseclog((LOG_DEBUG, "%s: NAT-T OAi present\n", __func__));
4759 if (mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL)
4760 ipseclog((LOG_DEBUG, "%s: NAT-T OAr present\n", __func__));
4762 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
4763 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
4764 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
4765 struct sadb_x_nat_t_type *type;
4766 struct sadb_x_nat_t_port *sport, *dport;
4768 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
4769 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
4770 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
4771 ipseclog((LOG_DEBUG, "%s: invalid nat-t message "
4772 "passed.\n", __func__));
4773 return key_senderror(so, m, EINVAL);
4776 sport = (struct sadb_x_nat_t_port *)
4777 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
4778 dport = (struct sadb_x_nat_t_port *)
4779 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
4782 KEY_PORTTOSADDR(&saidx.src, sport->sadb_x_nat_t_port_port);
4784 KEY_PORTTOSADDR(&saidx.dst, dport->sadb_x_nat_t_port_port);
4788 /* SPI allocation */
4789 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4792 return key_senderror(so, m, EINVAL);
4794 /* get a SA index */
4795 if ((newsah = key_getsah(&saidx)) == NULL) {
4796 /* create a new SA index */
4797 if ((newsah = key_newsah(&saidx)) == NULL) {
4798 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
4799 return key_senderror(so, m, ENOBUFS);
4805 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4806 if (newsav == NULL) {
4807 /* XXX don't free new SA index allocated in above. */
4808 return key_senderror(so, m, error);
4812 newsav->spi = htonl(spi);
4814 /* delete the entry in acqtree */
4815 if (mhp->msg->sadb_msg_seq != 0) {
4817 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4818 /* reset counter in order to deletion by timehandler. */
4819 acq->created = time_second;
4825 struct mbuf *n, *nn;
4826 struct sadb_sa *m_sa;
4827 struct sadb_msg *newmsg;
4830 /* create new sadb_msg to reply. */
4831 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4832 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4834 MGETHDR(n, M_DONTWAIT, MT_DATA);
4836 MCLGET(n, M_DONTWAIT);
4837 if ((n->m_flags & M_EXT) == 0) {
4843 return key_senderror(so, m, ENOBUFS);
4849 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4850 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4852 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4853 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4854 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4855 m_sa->sadb_sa_spi = htonl(spi);
4856 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4858 IPSEC_ASSERT(off == len,
4859 ("length inconsistency (off %u len %u)", off, len));
4861 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4862 SADB_EXT_ADDRESS_DST);
4865 return key_senderror(so, m, ENOBUFS);
4868 if (n->m_len < sizeof(struct sadb_msg)) {
4869 n = m_pullup(n, sizeof(struct sadb_msg));
4871 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4874 n->m_pkthdr.len = 0;
4875 for (nn = n; nn; nn = nn->m_next)
4876 n->m_pkthdr.len += nn->m_len;
4878 newmsg = mtod(n, struct sadb_msg *);
4879 newmsg->sadb_msg_seq = newsav->seq;
4880 newmsg->sadb_msg_errno = 0;
4881 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4884 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4889 * allocating new SPI
4890 * called by key_getspi().
4896 key_do_getnewspi(spirange, saidx)
4897 struct sadb_spirange *spirange;
4898 struct secasindex *saidx;
4902 int count = V_key_spi_trycnt;
4904 /* set spi range to allocate */
4905 if (spirange != NULL) {
4906 min = spirange->sadb_spirange_min;
4907 max = spirange->sadb_spirange_max;
4909 min = V_key_spi_minval;
4910 max = V_key_spi_maxval;
4912 /* IPCOMP needs 2-byte SPI */
4913 if (saidx->proto == IPPROTO_IPCOMP) {
4920 t = min; min = max; max = t;
4925 if (key_checkspidup(saidx, min) != NULL) {
4926 ipseclog((LOG_DEBUG, "%s: SPI %u exists already.\n",
4931 count--; /* taking one cost. */
4939 /* when requesting to allocate spi ranged */
4941 /* generate pseudo-random SPI value ranged. */
4942 newspi = min + (key_random() % (max - min + 1));
4944 if (key_checkspidup(saidx, newspi) == NULL)
4948 if (count == 0 || newspi == 0) {
4949 ipseclog((LOG_DEBUG, "%s: to allocate spi is failed.\n",
4956 keystat.getspi_count =
4957 (keystat.getspi_count + V_key_spi_trycnt - count) / 2;
4963 * SADB_UPDATE processing
4965 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4966 * key(AE), (identity(SD),) (sensitivity)>
4967 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4969 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4970 * (identity(SD),) (sensitivity)>
4973 * m will always be freed.
4976 key_update(so, m, mhp)
4979 const struct sadb_msghdr *mhp;
4981 struct sadb_sa *sa0;
4982 struct sadb_address *src0, *dst0;
4984 struct sadb_x_nat_t_type *type;
4985 struct sadb_x_nat_t_port *sport, *dport;
4986 struct sadb_address *iaddr, *raddr;
4987 struct sadb_x_nat_t_frag *frag;
4989 struct secasindex saidx;
4990 struct secashead *sah;
4991 struct secasvar *sav;
4997 IPSEC_ASSERT(so != NULL, ("null socket"));
4998 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4999 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5000 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5002 /* map satype to proto */
5003 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5004 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5006 return key_senderror(so, m, EINVAL);
5009 if (mhp->ext[SADB_EXT_SA] == NULL ||
5010 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5011 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5012 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5013 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5014 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5015 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5016 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5017 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5018 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5019 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5020 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5022 return key_senderror(so, m, EINVAL);
5024 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5025 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5026 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5027 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5029 return key_senderror(so, m, EINVAL);
5031 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5032 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5033 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5035 mode = IPSEC_MODE_ANY;
5038 /* XXX boundary checking for other extensions */
5040 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5041 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5042 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5044 /* XXX boundary check against sa_len */
5045 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5048 * Make sure the port numbers are zero.
5049 * In case of NAT-T we will update them later if needed.
5051 KEY_PORTTOSADDR(&saidx.src, 0);
5052 KEY_PORTTOSADDR(&saidx.dst, 0);
5056 * Handle NAT-T info if present.
5058 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
5059 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5060 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5062 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
5063 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5064 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5065 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5067 return key_senderror(so, m, EINVAL);
5070 type = (struct sadb_x_nat_t_type *)
5071 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5072 sport = (struct sadb_x_nat_t_port *)
5073 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5074 dport = (struct sadb_x_nat_t_port *)
5075 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5080 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
5081 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
5082 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
5083 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
5084 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5086 return key_senderror(so, m, EINVAL);
5088 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5089 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5090 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
5092 iaddr = raddr = NULL;
5094 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5095 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5096 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5098 return key_senderror(so, m, EINVAL);
5100 frag = (struct sadb_x_nat_t_frag *)
5101 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5107 /* get a SA header */
5108 if ((sah = key_getsah(&saidx)) == NULL) {
5109 ipseclog((LOG_DEBUG, "%s: no SA index found.\n", __func__));
5110 return key_senderror(so, m, ENOENT);
5113 /* set spidx if there */
5115 error = key_setident(sah, m, mhp);
5117 return key_senderror(so, m, error);
5119 /* find a SA with sequence number. */
5120 #ifdef IPSEC_DOSEQCHECK
5121 if (mhp->msg->sadb_msg_seq != 0
5122 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
5123 ipseclog((LOG_DEBUG, "%s: no larval SA with sequence %u "
5124 "exists.\n", __func__, mhp->msg->sadb_msg_seq));
5125 return key_senderror(so, m, ENOENT);
5129 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5132 ipseclog((LOG_DEBUG, "%s: no such a SA found (spi:%u)\n",
5133 __func__, (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5134 return key_senderror(so, m, EINVAL);
5138 /* validity check */
5139 if (sav->sah->saidx.proto != proto) {
5140 ipseclog((LOG_DEBUG, "%s: protocol mismatched "
5141 "(DB=%u param=%u)\n", __func__,
5142 sav->sah->saidx.proto, proto));
5143 return key_senderror(so, m, EINVAL);
5145 #ifdef IPSEC_DOSEQCHECK
5146 if (sav->spi != sa0->sadb_sa_spi) {
5147 ipseclog((LOG_DEBUG, "%s: SPI mismatched (DB:%u param:%u)\n",
5149 (u_int32_t)ntohl(sav->spi),
5150 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5151 return key_senderror(so, m, EINVAL);
5154 if (sav->pid != mhp->msg->sadb_msg_pid) {
5155 ipseclog((LOG_DEBUG, "%s: pid mismatched (DB:%u param:%u)\n",
5156 __func__, sav->pid, mhp->msg->sadb_msg_pid));
5157 return key_senderror(so, m, EINVAL);
5160 /* copy sav values */
5161 error = key_setsaval(sav, m, mhp);
5164 return key_senderror(so, m, error);
5169 * Handle more NAT-T info if present,
5170 * now that we have a sav to fill.
5173 sav->natt_type = type->sadb_x_nat_t_type_type;
5176 KEY_PORTTOSADDR(&sav->sah->saidx.src,
5177 sport->sadb_x_nat_t_port_port);
5179 KEY_PORTTOSADDR(&sav->sah->saidx.dst,
5180 dport->sadb_x_nat_t_port_port);
5184 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5185 * We should actually check for a minimum MTU here, if we
5186 * want to support it in ip_output.
5189 sav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5193 /* check SA values to be mature. */
5194 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
5196 return key_senderror(so, m, 0);
5202 /* set msg buf from mhp */
5203 n = key_getmsgbuf_x1(m, mhp);
5205 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5206 return key_senderror(so, m, ENOBUFS);
5210 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5215 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
5216 * only called by key_update().
5219 * others : found, pointer to a SA.
5221 #ifdef IPSEC_DOSEQCHECK
5222 static struct secasvar *
5223 key_getsavbyseq(sah, seq)
5224 struct secashead *sah;
5227 struct secasvar *sav;
5230 state = SADB_SASTATE_LARVAL;
5232 /* search SAD with sequence number ? */
5233 LIST_FOREACH(sav, &sah->savtree[state], chain) {
5235 KEY_CHKSASTATE(state, sav->state, __func__);
5237 if (sav->seq == seq) {
5239 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
5240 printf("DP %s cause refcnt++:%d SA:%p\n",
5241 __func__, sav->refcnt, sav));
5251 * SADB_ADD processing
5252 * add an entry to SA database, when received
5253 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5254 * key(AE), (identity(SD),) (sensitivity)>
5257 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5258 * (identity(SD),) (sensitivity)>
5261 * IGNORE identity and sensitivity messages.
5263 * m will always be freed.
5269 const struct sadb_msghdr *mhp;
5271 struct sadb_sa *sa0;
5272 struct sadb_address *src0, *dst0;
5274 struct sadb_x_nat_t_type *type;
5275 struct sadb_address *iaddr, *raddr;
5276 struct sadb_x_nat_t_frag *frag;
5278 struct secasindex saidx;
5279 struct secashead *newsah;
5280 struct secasvar *newsav;
5286 IPSEC_ASSERT(so != NULL, ("null socket"));
5287 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5288 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5289 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5291 /* map satype to proto */
5292 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5293 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5295 return key_senderror(so, m, EINVAL);
5298 if (mhp->ext[SADB_EXT_SA] == NULL ||
5299 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5300 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5301 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5302 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5303 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5304 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5305 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5306 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5307 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5308 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5309 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5311 return key_senderror(so, m, EINVAL);
5313 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5314 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5315 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5317 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5319 return key_senderror(so, m, EINVAL);
5321 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5322 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5323 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5325 mode = IPSEC_MODE_ANY;
5329 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5330 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5331 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5333 /* XXX boundary check against sa_len */
5334 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5337 * Make sure the port numbers are zero.
5338 * In case of NAT-T we will update them later if needed.
5340 KEY_PORTTOSADDR(&saidx.src, 0);
5341 KEY_PORTTOSADDR(&saidx.dst, 0);
5345 * Handle NAT-T info if present.
5347 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
5348 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5349 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5350 struct sadb_x_nat_t_port *sport, *dport;
5352 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
5353 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5354 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5355 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5357 return key_senderror(so, m, EINVAL);
5360 type = (struct sadb_x_nat_t_type *)
5361 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5362 sport = (struct sadb_x_nat_t_port *)
5363 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5364 dport = (struct sadb_x_nat_t_port *)
5365 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5368 KEY_PORTTOSADDR(&saidx.src,
5369 sport->sadb_x_nat_t_port_port);
5371 KEY_PORTTOSADDR(&saidx.dst,
5372 dport->sadb_x_nat_t_port_port);
5376 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
5377 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
5378 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
5379 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
5380 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5382 return key_senderror(so, m, EINVAL);
5384 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5385 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5386 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
5388 iaddr = raddr = NULL;
5390 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5391 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5392 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5394 return key_senderror(so, m, EINVAL);
5396 frag = (struct sadb_x_nat_t_frag *)
5397 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5403 /* get a SA header */
5404 if ((newsah = key_getsah(&saidx)) == NULL) {
5405 /* create a new SA header */
5406 if ((newsah = key_newsah(&saidx)) == NULL) {
5407 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
5408 return key_senderror(so, m, ENOBUFS);
5412 /* set spidx if there */
5414 error = key_setident(newsah, m, mhp);
5416 return key_senderror(so, m, error);
5419 /* create new SA entry. */
5420 /* We can create new SA only if SPI is differenct. */
5422 newsav = key_getsavbyspi(newsah, sa0->sadb_sa_spi);
5424 if (newsav != NULL) {
5425 ipseclog((LOG_DEBUG, "%s: SA already exists.\n", __func__));
5426 return key_senderror(so, m, EEXIST);
5428 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
5429 if (newsav == NULL) {
5430 return key_senderror(so, m, error);
5435 * Handle more NAT-T info if present,
5436 * now that we have a sav to fill.
5439 newsav->natt_type = type->sadb_x_nat_t_type_type;
5443 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5444 * We should actually check for a minimum MTU here, if we
5445 * want to support it in ip_output.
5448 newsav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5452 /* check SA values to be mature. */
5453 if ((error = key_mature(newsav)) != 0) {
5454 KEY_FREESAV(&newsav);
5455 return key_senderror(so, m, error);
5459 * don't call key_freesav() here, as we would like to keep the SA
5460 * in the database on success.
5466 /* set msg buf from mhp */
5467 n = key_getmsgbuf_x1(m, mhp);
5469 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5470 return key_senderror(so, m, ENOBUFS);
5474 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5480 key_setident(sah, m, mhp)
5481 struct secashead *sah;
5483 const struct sadb_msghdr *mhp;
5485 const struct sadb_ident *idsrc, *iddst;
5486 int idsrclen, iddstlen;
5488 IPSEC_ASSERT(sah != NULL, ("null secashead"));
5489 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5490 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5491 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5493 /* don't make buffer if not there */
5494 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
5495 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5501 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
5502 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5503 ipseclog((LOG_DEBUG, "%s: invalid identity.\n", __func__));
5507 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
5508 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
5509 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
5510 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
5512 /* validity check */
5513 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
5514 ipseclog((LOG_DEBUG, "%s: ident type mismatch.\n", __func__));
5518 switch (idsrc->sadb_ident_type) {
5519 case SADB_IDENTTYPE_PREFIX:
5520 case SADB_IDENTTYPE_FQDN:
5521 case SADB_IDENTTYPE_USERFQDN:
5523 /* XXX do nothing */
5529 /* make structure */
5530 sah->idents = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5531 if (sah->idents == NULL) {
5532 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5535 sah->identd = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5536 if (sah->identd == NULL) {
5537 free(sah->idents, M_IPSEC_MISC);
5539 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5542 sah->idents->type = idsrc->sadb_ident_type;
5543 sah->idents->id = idsrc->sadb_ident_id;
5545 sah->identd->type = iddst->sadb_ident_type;
5546 sah->identd->id = iddst->sadb_ident_id;
5552 * m will not be freed on return.
5553 * it is caller's responsibility to free the result.
5555 static struct mbuf *
5556 key_getmsgbuf_x1(m, mhp)
5558 const struct sadb_msghdr *mhp;
5562 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5563 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5564 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5566 /* create new sadb_msg to reply. */
5567 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5568 SADB_EXT_SA, SADB_X_EXT_SA2,
5569 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5570 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5571 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5575 if (n->m_len < sizeof(struct sadb_msg)) {
5576 n = m_pullup(n, sizeof(struct sadb_msg));
5580 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5581 mtod(n, struct sadb_msg *)->sadb_msg_len =
5582 PFKEY_UNIT64(n->m_pkthdr.len);
5587 static int key_delete_all __P((struct socket *, struct mbuf *,
5588 const struct sadb_msghdr *, u_int16_t));
5591 * SADB_DELETE processing
5593 * <base, SA(*), address(SD)>
5594 * from the ikmpd, and set SADB_SASTATE_DEAD,
5596 * <base, SA(*), address(SD)>
5599 * m will always be freed.
5602 key_delete(so, m, mhp)
5605 const struct sadb_msghdr *mhp;
5607 struct sadb_sa *sa0;
5608 struct sadb_address *src0, *dst0;
5609 struct secasindex saidx;
5610 struct secashead *sah;
5611 struct secasvar *sav = NULL;
5614 IPSEC_ASSERT(so != NULL, ("null socket"));
5615 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5616 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5617 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5619 /* map satype to proto */
5620 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5621 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5623 return key_senderror(so, m, EINVAL);
5626 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5627 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5628 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5630 return key_senderror(so, m, EINVAL);
5633 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5634 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5635 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5637 return key_senderror(so, m, EINVAL);
5640 if (mhp->ext[SADB_EXT_SA] == NULL) {
5642 * Caller wants us to delete all non-LARVAL SAs
5643 * that match the src/dst. This is used during
5644 * IKE INITIAL-CONTACT.
5646 ipseclog((LOG_DEBUG, "%s: doing delete all.\n", __func__));
5647 return key_delete_all(so, m, mhp, proto);
5648 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5649 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5651 return key_senderror(so, m, EINVAL);
5654 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5655 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5656 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5658 /* XXX boundary check against sa_len */
5659 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5662 * Make sure the port numbers are zero.
5663 * In case of NAT-T we will update them later if needed.
5665 KEY_PORTTOSADDR(&saidx.src, 0);
5666 KEY_PORTTOSADDR(&saidx.dst, 0);
5670 * Handle NAT-T info if present.
5672 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5673 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5674 struct sadb_x_nat_t_port *sport, *dport;
5676 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5677 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5678 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5680 return key_senderror(so, m, EINVAL);
5683 sport = (struct sadb_x_nat_t_port *)
5684 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5685 dport = (struct sadb_x_nat_t_port *)
5686 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5689 KEY_PORTTOSADDR(&saidx.src,
5690 sport->sadb_x_nat_t_port_port);
5692 KEY_PORTTOSADDR(&saidx.dst,
5693 dport->sadb_x_nat_t_port_port);
5697 /* get a SA header */
5699 LIST_FOREACH(sah, &V_sahtree, chain) {
5700 if (sah->state == SADB_SASTATE_DEAD)
5702 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5705 /* get a SA with SPI. */
5706 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5712 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5713 return key_senderror(so, m, ENOENT);
5716 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5722 struct sadb_msg *newmsg;
5724 /* create new sadb_msg to reply. */
5725 /* XXX-BZ NAT-T extensions? */
5726 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5727 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5729 return key_senderror(so, m, ENOBUFS);
5731 if (n->m_len < sizeof(struct sadb_msg)) {
5732 n = m_pullup(n, sizeof(struct sadb_msg));
5734 return key_senderror(so, m, ENOBUFS);
5736 newmsg = mtod(n, struct sadb_msg *);
5737 newmsg->sadb_msg_errno = 0;
5738 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5741 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5746 * delete all SAs for src/dst. Called from key_delete().
5749 key_delete_all(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp,
5752 struct sadb_address *src0, *dst0;
5753 struct secasindex saidx;
5754 struct secashead *sah;
5755 struct secasvar *sav, *nextsav;
5756 u_int stateidx, state;
5758 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5759 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5761 /* XXX boundary check against sa_len */
5762 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5765 * Make sure the port numbers are zero.
5766 * In case of NAT-T we will update them later if needed.
5768 KEY_PORTTOSADDR(&saidx.src, 0);
5769 KEY_PORTTOSADDR(&saidx.dst, 0);
5773 * Handle NAT-T info if present.
5776 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5777 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5778 struct sadb_x_nat_t_port *sport, *dport;
5780 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5781 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5782 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5784 return key_senderror(so, m, EINVAL);
5787 sport = (struct sadb_x_nat_t_port *)
5788 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5789 dport = (struct sadb_x_nat_t_port *)
5790 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5793 KEY_PORTTOSADDR(&saidx.src,
5794 sport->sadb_x_nat_t_port_port);
5796 KEY_PORTTOSADDR(&saidx.dst,
5797 dport->sadb_x_nat_t_port_port);
5802 LIST_FOREACH(sah, &V_sahtree, chain) {
5803 if (sah->state == SADB_SASTATE_DEAD)
5805 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5808 /* Delete all non-LARVAL SAs. */
5810 stateidx < _ARRAYLEN(saorder_state_alive);
5812 state = saorder_state_alive[stateidx];
5813 if (state == SADB_SASTATE_LARVAL)
5815 for (sav = LIST_FIRST(&sah->savtree[state]);
5816 sav != NULL; sav = nextsav) {
5817 nextsav = LIST_NEXT(sav, chain);
5819 if (sav->state != state) {
5820 ipseclog((LOG_DEBUG, "%s: invalid "
5821 "sav->state (queue %d SA %d)\n",
5822 __func__, state, sav->state));
5826 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5834 struct sadb_msg *newmsg;
5836 /* create new sadb_msg to reply. */
5837 /* XXX-BZ NAT-T extensions? */
5838 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5839 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5841 return key_senderror(so, m, ENOBUFS);
5843 if (n->m_len < sizeof(struct sadb_msg)) {
5844 n = m_pullup(n, sizeof(struct sadb_msg));
5846 return key_senderror(so, m, ENOBUFS);
5848 newmsg = mtod(n, struct sadb_msg *);
5849 newmsg->sadb_msg_errno = 0;
5850 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5853 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5858 * SADB_GET processing
5860 * <base, SA(*), address(SD)>
5861 * from the ikmpd, and get a SP and a SA to respond,
5863 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5864 * (identity(SD),) (sensitivity)>
5867 * m will always be freed.
5873 const struct sadb_msghdr *mhp;
5875 struct sadb_sa *sa0;
5876 struct sadb_address *src0, *dst0;
5877 struct secasindex saidx;
5878 struct secashead *sah;
5879 struct secasvar *sav = NULL;
5882 IPSEC_ASSERT(so != NULL, ("null socket"));
5883 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5884 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5885 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5887 /* map satype to proto */
5888 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5889 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5891 return key_senderror(so, m, EINVAL);
5894 if (mhp->ext[SADB_EXT_SA] == NULL ||
5895 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5896 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5897 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5899 return key_senderror(so, m, EINVAL);
5901 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5902 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5903 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5904 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5906 return key_senderror(so, m, EINVAL);
5909 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5910 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5911 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5913 /* XXX boundary check against sa_len */
5914 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5917 * Make sure the port numbers are zero.
5918 * In case of NAT-T we will update them later if needed.
5920 KEY_PORTTOSADDR(&saidx.src, 0);
5921 KEY_PORTTOSADDR(&saidx.dst, 0);
5925 * Handle NAT-T info if present.
5928 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5929 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5930 struct sadb_x_nat_t_port *sport, *dport;
5932 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5933 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5934 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5936 return key_senderror(so, m, EINVAL);
5939 sport = (struct sadb_x_nat_t_port *)
5940 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5941 dport = (struct sadb_x_nat_t_port *)
5942 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5945 KEY_PORTTOSADDR(&saidx.src,
5946 sport->sadb_x_nat_t_port_port);
5948 KEY_PORTTOSADDR(&saidx.dst,
5949 dport->sadb_x_nat_t_port_port);
5953 /* get a SA header */
5955 LIST_FOREACH(sah, &V_sahtree, chain) {
5956 if (sah->state == SADB_SASTATE_DEAD)
5958 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5961 /* get a SA with SPI. */
5962 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5968 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5969 return key_senderror(so, m, ENOENT);
5976 /* map proto to satype */
5977 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5978 ipseclog((LOG_DEBUG, "%s: there was invalid proto in SAD.\n",
5980 return key_senderror(so, m, EINVAL);
5983 /* create new sadb_msg to reply. */
5984 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5985 mhp->msg->sadb_msg_pid);
5987 return key_senderror(so, m, ENOBUFS);
5990 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5994 /* XXX make it sysctl-configurable? */
5996 key_getcomb_setlifetime(comb)
5997 struct sadb_comb *comb;
6000 comb->sadb_comb_soft_allocations = 1;
6001 comb->sadb_comb_hard_allocations = 1;
6002 comb->sadb_comb_soft_bytes = 0;
6003 comb->sadb_comb_hard_bytes = 0;
6004 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
6005 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
6006 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
6007 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
6011 * XXX reorder combinations by preference
6012 * XXX no idea if the user wants ESP authentication or not
6014 static struct mbuf *
6017 struct sadb_comb *comb;
6018 struct enc_xform *algo;
6019 struct mbuf *result = NULL, *m, *n;
6023 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6026 for (i = 1; i <= SADB_EALG_MAX; i++) {
6027 algo = esp_algorithm_lookup(i);
6031 /* discard algorithms with key size smaller than system min */
6032 if (_BITS(algo->maxkey) < V_ipsec_esp_keymin)
6034 if (_BITS(algo->minkey) < V_ipsec_esp_keymin)
6035 encmin = V_ipsec_esp_keymin;
6037 encmin = _BITS(algo->minkey);
6039 if (V_ipsec_esp_auth)
6040 m = key_getcomb_ah();
6042 IPSEC_ASSERT(l <= MLEN,
6043 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6044 MGET(m, M_DONTWAIT, MT_DATA);
6049 bzero(mtod(m, caddr_t), m->m_len);
6056 for (n = m; n; n = n->m_next)
6058 IPSEC_ASSERT((totlen % l) == 0, ("totlen=%u, l=%u", totlen, l));
6060 for (off = 0; off < totlen; off += l) {
6061 n = m_pulldown(m, off, l, &o);
6063 /* m is already freed */
6066 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
6067 bzero(comb, sizeof(*comb));
6068 key_getcomb_setlifetime(comb);
6069 comb->sadb_comb_encrypt = i;
6070 comb->sadb_comb_encrypt_minbits = encmin;
6071 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
6090 const struct auth_hash *ah,
6096 *min = *max = ah->keysize;
6097 if (ah->keysize == 0) {
6099 * Transform takes arbitrary key size but algorithm
6100 * key size is restricted. Enforce this here.
6103 case SADB_X_AALG_MD5: *min = *max = 16; break;
6104 case SADB_X_AALG_SHA: *min = *max = 20; break;
6105 case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
6107 DPRINTF(("%s: unknown AH algorithm %u\n",
6115 * XXX reorder combinations by preference
6117 static struct mbuf *
6120 struct sadb_comb *comb;
6121 struct auth_hash *algo;
6123 u_int16_t minkeysize, maxkeysize;
6125 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6128 for (i = 1; i <= SADB_AALG_MAX; i++) {
6130 /* we prefer HMAC algorithms, not old algorithms */
6131 if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC)
6134 algo = ah_algorithm_lookup(i);
6137 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
6138 /* discard algorithms with key size smaller than system min */
6139 if (_BITS(minkeysize) < V_ipsec_ah_keymin)
6143 IPSEC_ASSERT(l <= MLEN,
6144 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6145 MGET(m, M_DONTWAIT, MT_DATA);
6152 M_PREPEND(m, l, M_DONTWAIT);
6156 comb = mtod(m, struct sadb_comb *);
6157 bzero(comb, sizeof(*comb));
6158 key_getcomb_setlifetime(comb);
6159 comb->sadb_comb_auth = i;
6160 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
6161 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
6168 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
6169 * XXX reorder combinations by preference
6171 static struct mbuf *
6172 key_getcomb_ipcomp()
6174 struct sadb_comb *comb;
6175 struct comp_algo *algo;
6178 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6181 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
6182 algo = ipcomp_algorithm_lookup(i);
6187 IPSEC_ASSERT(l <= MLEN,
6188 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6189 MGET(m, M_DONTWAIT, MT_DATA);
6196 M_PREPEND(m, l, M_DONTWAIT);
6200 comb = mtod(m, struct sadb_comb *);
6201 bzero(comb, sizeof(*comb));
6202 key_getcomb_setlifetime(comb);
6203 comb->sadb_comb_encrypt = i;
6204 /* what should we set into sadb_comb_*_{min,max}bits? */
6211 * XXX no way to pass mode (transport/tunnel) to userland
6212 * XXX replay checking?
6213 * XXX sysctl interface to ipsec_{ah,esp}_keymin
6215 static struct mbuf *
6217 const struct secasindex *saidx;
6219 struct sadb_prop *prop;
6221 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
6224 switch (saidx->proto) {
6226 m = key_getcomb_esp();
6229 m = key_getcomb_ah();
6231 case IPPROTO_IPCOMP:
6232 m = key_getcomb_ipcomp();
6240 M_PREPEND(m, l, M_DONTWAIT);
6245 for (n = m; n; n = n->m_next)
6248 prop = mtod(m, struct sadb_prop *);
6249 bzero(prop, sizeof(*prop));
6250 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
6251 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
6252 prop->sadb_prop_replay = 32; /* XXX */
6258 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
6260 * <base, SA, address(SD), (address(P)), x_policy,
6261 * (identity(SD),) (sensitivity,) proposal>
6262 * to KMD, and expect to receive
6263 * <base> with SADB_ACQUIRE if error occured,
6265 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
6266 * from KMD by PF_KEY.
6268 * XXX x_policy is outside of RFC2367 (KAME extension).
6269 * XXX sensitivity is not supported.
6270 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
6271 * see comment for key_getcomb_ipcomp().
6275 * others: error number
6278 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
6280 struct mbuf *result = NULL, *m;
6281 struct secacq *newacq;
6286 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
6287 satype = key_proto2satype(saidx->proto);
6288 IPSEC_ASSERT(satype != 0, ("null satype, protocol %u", saidx->proto));
6291 * We never do anything about acquirng SA. There is anather
6292 * solution that kernel blocks to send SADB_ACQUIRE message until
6293 * getting something message from IKEd. In later case, to be
6294 * managed with ACQUIRING list.
6296 /* Get an entry to check whether sending message or not. */
6297 if ((newacq = key_getacq(saidx)) != NULL) {
6298 if (V_key_blockacq_count < newacq->count) {
6299 /* reset counter and do send message. */
6302 /* increment counter and do nothing. */
6307 /* make new entry for blocking to send SADB_ACQUIRE. */
6308 if ((newacq = key_newacq(saidx)) == NULL)
6314 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
6322 * No SADB_X_EXT_NAT_T_* here: we do not know
6323 * anything related to NAT-T at this time.
6326 /* set sadb_address for saidx's. */
6327 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6328 &saidx->src.sa, FULLMASK, IPSEC_ULPROTO_ANY);
6335 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6336 &saidx->dst.sa, FULLMASK, IPSEC_ULPROTO_ANY);
6343 /* XXX proxy address (optional) */
6345 /* set sadb_x_policy */
6347 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
6355 /* XXX identity (optional) */
6357 if (idexttype && fqdn) {
6358 /* create identity extension (FQDN) */
6359 struct sadb_ident *id;
6362 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
6363 id = (struct sadb_ident *)p;
6364 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6365 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6366 id->sadb_ident_exttype = idexttype;
6367 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
6368 bcopy(fqdn, id + 1, fqdnlen);
6369 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
6373 /* create identity extension (USERFQDN) */
6374 struct sadb_ident *id;
6378 /* +1 for terminating-NUL */
6379 userfqdnlen = strlen(userfqdn) + 1;
6382 id = (struct sadb_ident *)p;
6383 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6384 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6385 id->sadb_ident_exttype = idexttype;
6386 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
6387 /* XXX is it correct? */
6388 if (curproc && curproc->p_cred)
6389 id->sadb_ident_id = curproc->p_cred->p_ruid;
6390 if (userfqdn && userfqdnlen)
6391 bcopy(userfqdn, id + 1, userfqdnlen);
6392 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
6396 /* XXX sensitivity (optional) */
6398 /* create proposal/combination extension */
6399 m = key_getprop(saidx);
6402 * spec conformant: always attach proposal/combination extension,
6403 * the problem is that we have no way to attach it for ipcomp,
6404 * due to the way sadb_comb is declared in RFC2367.
6413 * outside of spec; make proposal/combination extension optional.
6419 if ((result->m_flags & M_PKTHDR) == 0) {
6424 if (result->m_len < sizeof(struct sadb_msg)) {
6425 result = m_pullup(result, sizeof(struct sadb_msg));
6426 if (result == NULL) {
6432 result->m_pkthdr.len = 0;
6433 for (m = result; m; m = m->m_next)
6434 result->m_pkthdr.len += m->m_len;
6436 mtod(result, struct sadb_msg *)->sadb_msg_len =
6437 PFKEY_UNIT64(result->m_pkthdr.len);
6439 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6447 static struct secacq *
6448 key_newacq(const struct secasindex *saidx)
6450 struct secacq *newacq;
6453 newacq = malloc(sizeof(struct secacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6454 if (newacq == NULL) {
6455 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6460 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
6461 newacq->seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq);
6462 newacq->created = time_second;
6465 /* add to acqtree */
6467 LIST_INSERT_HEAD(&V_acqtree, newacq, chain);
6473 static struct secacq *
6474 key_getacq(const struct secasindex *saidx)
6479 LIST_FOREACH(acq, &V_acqtree, chain) {
6480 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
6488 static struct secacq *
6489 key_getacqbyseq(seq)
6495 LIST_FOREACH(acq, &V_acqtree, chain) {
6496 if (acq->seq == seq)
6504 static struct secspacq *
6506 struct secpolicyindex *spidx;
6508 struct secspacq *acq;
6511 acq = malloc(sizeof(struct secspacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6513 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6518 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
6519 acq->created = time_second;
6522 /* add to spacqtree */
6524 LIST_INSERT_HEAD(&V_spacqtree, acq, chain);
6530 static struct secspacq *
6532 struct secpolicyindex *spidx;
6534 struct secspacq *acq;
6537 LIST_FOREACH(acq, &V_spacqtree, chain) {
6538 if (key_cmpspidx_exactly(spidx, &acq->spidx)) {
6539 /* NB: return holding spacq_lock */
6549 * SADB_ACQUIRE processing,
6550 * in first situation, is receiving
6552 * from the ikmpd, and clear sequence of its secasvar entry.
6554 * In second situation, is receiving
6555 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6556 * from a user land process, and return
6557 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6560 * m will always be freed.
6563 key_acquire2(so, m, mhp)
6566 const struct sadb_msghdr *mhp;
6568 const struct sadb_address *src0, *dst0;
6569 struct secasindex saidx;
6570 struct secashead *sah;
6574 IPSEC_ASSERT(so != NULL, ("null socket"));
6575 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6576 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6577 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6580 * Error message from KMd.
6581 * We assume that if error was occured in IKEd, the length of PFKEY
6582 * message is equal to the size of sadb_msg structure.
6583 * We do not raise error even if error occured in this function.
6585 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
6588 /* check sequence number */
6589 if (mhp->msg->sadb_msg_seq == 0) {
6590 ipseclog((LOG_DEBUG, "%s: must specify sequence "
6591 "number.\n", __func__));
6596 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
6598 * the specified larval SA is already gone, or we got
6599 * a bogus sequence number. we can silently ignore it.
6605 /* reset acq counter in order to deletion by timehander. */
6606 acq->created = time_second;
6613 * This message is from user land.
6616 /* map satype to proto */
6617 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6618 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6620 return key_senderror(so, m, EINVAL);
6623 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
6624 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
6625 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
6627 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6629 return key_senderror(so, m, EINVAL);
6631 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
6632 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
6633 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
6635 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6637 return key_senderror(so, m, EINVAL);
6640 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
6641 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
6643 /* XXX boundary check against sa_len */
6644 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
6647 * Make sure the port numbers are zero.
6648 * In case of NAT-T we will update them later if needed.
6650 KEY_PORTTOSADDR(&saidx.src, 0);
6651 KEY_PORTTOSADDR(&saidx.dst, 0);
6655 * Handle NAT-T info if present.
6658 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
6659 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
6660 struct sadb_x_nat_t_port *sport, *dport;
6662 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
6663 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
6664 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
6666 return key_senderror(so, m, EINVAL);
6669 sport = (struct sadb_x_nat_t_port *)
6670 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
6671 dport = (struct sadb_x_nat_t_port *)
6672 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
6675 KEY_PORTTOSADDR(&saidx.src,
6676 sport->sadb_x_nat_t_port_port);
6678 KEY_PORTTOSADDR(&saidx.dst,
6679 dport->sadb_x_nat_t_port_port);
6683 /* get a SA index */
6685 LIST_FOREACH(sah, &V_sahtree, chain) {
6686 if (sah->state == SADB_SASTATE_DEAD)
6688 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
6693 ipseclog((LOG_DEBUG, "%s: a SA exists already.\n", __func__));
6694 return key_senderror(so, m, EEXIST);
6697 error = key_acquire(&saidx, NULL);
6699 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
6700 __func__, mhp->msg->sadb_msg_errno));
6701 return key_senderror(so, m, error);
6704 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6708 * SADB_REGISTER processing.
6709 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6712 * from the ikmpd, and register a socket to send PF_KEY messages,
6716 * If socket is detached, must free from regnode.
6718 * m will always be freed.
6721 key_register(so, m, mhp)
6724 const struct sadb_msghdr *mhp;
6726 struct secreg *reg, *newreg = 0;
6728 IPSEC_ASSERT(so != NULL, ("null socket"));
6729 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6730 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6731 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6733 /* check for invalid register message */
6734 if (mhp->msg->sadb_msg_satype >= sizeof(V_regtree)/sizeof(V_regtree[0]))
6735 return key_senderror(so, m, EINVAL);
6737 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6738 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6741 /* check whether existing or not */
6743 LIST_FOREACH(reg, &V_regtree[mhp->msg->sadb_msg_satype], chain) {
6744 if (reg->so == so) {
6746 ipseclog((LOG_DEBUG, "%s: socket exists already.\n",
6748 return key_senderror(so, m, EEXIST);
6752 /* create regnode */
6753 newreg = malloc(sizeof(struct secreg), M_IPSEC_SAR, M_NOWAIT|M_ZERO);
6754 if (newreg == NULL) {
6756 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6757 return key_senderror(so, m, ENOBUFS);
6761 ((struct keycb *)sotorawcb(so))->kp_registered++;
6763 /* add regnode to regtree. */
6764 LIST_INSERT_HEAD(&V_regtree[mhp->msg->sadb_msg_satype], newreg, chain);
6770 struct sadb_msg *newmsg;
6771 struct sadb_supported *sup;
6772 u_int len, alen, elen;
6775 struct sadb_alg *alg;
6777 /* create new sadb_msg to reply. */
6779 for (i = 1; i <= SADB_AALG_MAX; i++) {
6780 if (ah_algorithm_lookup(i))
6781 alen += sizeof(struct sadb_alg);
6784 alen += sizeof(struct sadb_supported);
6786 for (i = 1; i <= SADB_EALG_MAX; i++) {
6787 if (esp_algorithm_lookup(i))
6788 elen += sizeof(struct sadb_alg);
6791 elen += sizeof(struct sadb_supported);
6793 len = sizeof(struct sadb_msg) + alen + elen;
6796 return key_senderror(so, m, ENOBUFS);
6798 MGETHDR(n, M_DONTWAIT, MT_DATA);
6800 MCLGET(n, M_DONTWAIT);
6801 if ((n->m_flags & M_EXT) == 0) {
6807 return key_senderror(so, m, ENOBUFS);
6809 n->m_pkthdr.len = n->m_len = len;
6813 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6814 newmsg = mtod(n, struct sadb_msg *);
6815 newmsg->sadb_msg_errno = 0;
6816 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6817 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6819 /* for authentication algorithm */
6821 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6822 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6823 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6824 off += PFKEY_ALIGN8(sizeof(*sup));
6826 for (i = 1; i <= SADB_AALG_MAX; i++) {
6827 struct auth_hash *aalgo;
6828 u_int16_t minkeysize, maxkeysize;
6830 aalgo = ah_algorithm_lookup(i);
6833 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6834 alg->sadb_alg_id = i;
6835 alg->sadb_alg_ivlen = 0;
6836 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
6837 alg->sadb_alg_minbits = _BITS(minkeysize);
6838 alg->sadb_alg_maxbits = _BITS(maxkeysize);
6839 off += PFKEY_ALIGN8(sizeof(*alg));
6843 /* for encryption algorithm */
6845 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6846 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6847 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6848 off += PFKEY_ALIGN8(sizeof(*sup));
6850 for (i = 1; i <= SADB_EALG_MAX; i++) {
6851 struct enc_xform *ealgo;
6853 ealgo = esp_algorithm_lookup(i);
6856 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6857 alg->sadb_alg_id = i;
6858 alg->sadb_alg_ivlen = ealgo->blocksize;
6859 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
6860 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
6861 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6865 IPSEC_ASSERT(off == len,
6866 ("length assumption failed (off %u len %u)", off, len));
6869 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6874 * free secreg entry registered.
6875 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6878 key_freereg(struct socket *so)
6883 IPSEC_ASSERT(so != NULL, ("NULL so"));
6886 * check whether existing or not.
6887 * check all type of SA, because there is a potential that
6888 * one socket is registered to multiple type of SA.
6891 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6892 LIST_FOREACH(reg, &V_regtree[i], chain) {
6893 if (reg->so == so && __LIST_CHAINED(reg)) {
6894 LIST_REMOVE(reg, chain);
6895 free(reg, M_IPSEC_SAR);
6904 * SADB_EXPIRE processing
6906 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6908 * NOTE: We send only soft lifetime extension.
6911 * others : error number
6914 key_expire(struct secasvar *sav)
6918 struct mbuf *result = NULL, *m;
6921 struct sadb_lifetime *lt;
6923 /* XXX: Why do we lock ? */
6924 s = splnet(); /*called from softclock()*/
6926 IPSEC_ASSERT (sav != NULL, ("null sav"));
6927 IPSEC_ASSERT (sav->sah != NULL, ("null sa header"));
6929 /* set msg header */
6930 satype = key_proto2satype(sav->sah->saidx.proto);
6931 IPSEC_ASSERT(satype != 0, ("invalid proto, satype %u", satype));
6932 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6939 /* create SA extension */
6940 m = key_setsadbsa(sav);
6947 /* create SA extension */
6948 m = key_setsadbxsa2(sav->sah->saidx.mode,
6949 sav->replay ? sav->replay->count : 0,
6950 sav->sah->saidx.reqid);
6957 /* create lifetime extension (current and soft) */
6958 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6959 m = key_alloc_mbuf(len);
6960 if (!m || m->m_next) { /*XXX*/
6966 bzero(mtod(m, caddr_t), len);
6967 lt = mtod(m, struct sadb_lifetime *);
6968 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6969 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6970 lt->sadb_lifetime_allocations = sav->lft_c->allocations;
6971 lt->sadb_lifetime_bytes = sav->lft_c->bytes;
6972 lt->sadb_lifetime_addtime = sav->lft_c->addtime;
6973 lt->sadb_lifetime_usetime = sav->lft_c->usetime;
6974 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6975 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6976 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
6977 lt->sadb_lifetime_allocations = sav->lft_s->allocations;
6978 lt->sadb_lifetime_bytes = sav->lft_s->bytes;
6979 lt->sadb_lifetime_addtime = sav->lft_s->addtime;
6980 lt->sadb_lifetime_usetime = sav->lft_s->usetime;
6983 /* set sadb_address for source */
6984 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6985 &sav->sah->saidx.src.sa,
6986 FULLMASK, IPSEC_ULPROTO_ANY);
6993 /* set sadb_address for destination */
6994 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6995 &sav->sah->saidx.dst.sa,
6996 FULLMASK, IPSEC_ULPROTO_ANY);
7004 * XXX-BZ Handle NAT-T extensions here.
7007 if ((result->m_flags & M_PKTHDR) == 0) {
7012 if (result->m_len < sizeof(struct sadb_msg)) {
7013 result = m_pullup(result, sizeof(struct sadb_msg));
7014 if (result == NULL) {
7020 result->m_pkthdr.len = 0;
7021 for (m = result; m; m = m->m_next)
7022 result->m_pkthdr.len += m->m_len;
7024 mtod(result, struct sadb_msg *)->sadb_msg_len =
7025 PFKEY_UNIT64(result->m_pkthdr.len);
7028 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
7038 * SADB_FLUSH processing
7041 * from the ikmpd, and free all entries in secastree.
7045 * NOTE: to do is only marking SADB_SASTATE_DEAD.
7047 * m will always be freed.
7050 key_flush(so, m, mhp)
7053 const struct sadb_msghdr *mhp;
7055 struct sadb_msg *newmsg;
7056 struct secashead *sah, *nextsah;
7057 struct secasvar *sav, *nextsav;
7062 IPSEC_ASSERT(so != NULL, ("null socket"));
7063 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7064 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7066 /* map satype to proto */
7067 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7068 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7070 return key_senderror(so, m, EINVAL);
7073 /* no SATYPE specified, i.e. flushing all SA. */
7075 for (sah = LIST_FIRST(&V_sahtree);
7078 nextsah = LIST_NEXT(sah, chain);
7080 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7081 && proto != sah->saidx.proto)
7085 stateidx < _ARRAYLEN(saorder_state_alive);
7087 state = saorder_state_any[stateidx];
7088 for (sav = LIST_FIRST(&sah->savtree[state]);
7092 nextsav = LIST_NEXT(sav, chain);
7094 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
7099 sah->state = SADB_SASTATE_DEAD;
7103 if (m->m_len < sizeof(struct sadb_msg) ||
7104 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
7105 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
7106 return key_senderror(so, m, ENOBUFS);
7112 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
7113 newmsg = mtod(m, struct sadb_msg *);
7114 newmsg->sadb_msg_errno = 0;
7115 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
7117 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7121 * SADB_DUMP processing
7122 * dump all entries including status of DEAD in SAD.
7125 * from the ikmpd, and dump all secasvar leaves
7130 * m will always be freed.
7133 key_dump(so, m, mhp)
7136 const struct sadb_msghdr *mhp;
7138 struct secashead *sah;
7139 struct secasvar *sav;
7145 struct sadb_msg *newmsg;
7148 IPSEC_ASSERT(so != NULL, ("null socket"));
7149 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7150 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7151 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7153 /* map satype to proto */
7154 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7155 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7157 return key_senderror(so, m, EINVAL);
7160 /* count sav entries to be sent to the userland. */
7163 LIST_FOREACH(sah, &V_sahtree, chain) {
7164 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7165 && proto != sah->saidx.proto)
7169 stateidx < _ARRAYLEN(saorder_state_any);
7171 state = saorder_state_any[stateidx];
7172 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7180 return key_senderror(so, m, ENOENT);
7183 /* send this to the userland, one at a time. */
7185 LIST_FOREACH(sah, &V_sahtree, chain) {
7186 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7187 && proto != sah->saidx.proto)
7190 /* map proto to satype */
7191 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
7193 ipseclog((LOG_DEBUG, "%s: there was invalid proto in "
7194 "SAD.\n", __func__));
7195 return key_senderror(so, m, EINVAL);
7199 stateidx < _ARRAYLEN(saorder_state_any);
7201 state = saorder_state_any[stateidx];
7202 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7203 n = key_setdumpsa(sav, SADB_DUMP, satype,
7204 --cnt, mhp->msg->sadb_msg_pid);
7207 return key_senderror(so, m, ENOBUFS);
7209 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
7220 * SADB_X_PROMISC processing
7222 * m will always be freed.
7225 key_promisc(so, m, mhp)
7228 const struct sadb_msghdr *mhp;
7232 IPSEC_ASSERT(so != NULL, ("null socket"));
7233 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7234 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7235 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7237 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7239 if (olen < sizeof(struct sadb_msg)) {
7241 return key_senderror(so, m, EINVAL);
7246 } else if (olen == sizeof(struct sadb_msg)) {
7247 /* enable/disable promisc mode */
7250 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
7251 return key_senderror(so, m, EINVAL);
7252 mhp->msg->sadb_msg_errno = 0;
7253 switch (mhp->msg->sadb_msg_satype) {
7256 kp->kp_promisc = mhp->msg->sadb_msg_satype;
7259 return key_senderror(so, m, EINVAL);
7262 /* send the original message back to everyone */
7263 mhp->msg->sadb_msg_errno = 0;
7264 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7266 /* send packet as is */
7268 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
7270 /* TODO: if sadb_msg_seq is specified, send to specific pid */
7271 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7275 static int (*key_typesw[]) __P((struct socket *, struct mbuf *,
7276 const struct sadb_msghdr *)) = {
7277 NULL, /* SADB_RESERVED */
7278 key_getspi, /* SADB_GETSPI */
7279 key_update, /* SADB_UPDATE */
7280 key_add, /* SADB_ADD */
7281 key_delete, /* SADB_DELETE */
7282 key_get, /* SADB_GET */
7283 key_acquire2, /* SADB_ACQUIRE */
7284 key_register, /* SADB_REGISTER */
7285 NULL, /* SADB_EXPIRE */
7286 key_flush, /* SADB_FLUSH */
7287 key_dump, /* SADB_DUMP */
7288 key_promisc, /* SADB_X_PROMISC */
7289 NULL, /* SADB_X_PCHANGE */
7290 key_spdadd, /* SADB_X_SPDUPDATE */
7291 key_spdadd, /* SADB_X_SPDADD */
7292 key_spddelete, /* SADB_X_SPDDELETE */
7293 key_spdget, /* SADB_X_SPDGET */
7294 NULL, /* SADB_X_SPDACQUIRE */
7295 key_spddump, /* SADB_X_SPDDUMP */
7296 key_spdflush, /* SADB_X_SPDFLUSH */
7297 key_spdadd, /* SADB_X_SPDSETIDX */
7298 NULL, /* SADB_X_SPDEXPIRE */
7299 key_spddelete2, /* SADB_X_SPDDELETE2 */
7303 * parse sadb_msg buffer to process PFKEYv2,
7304 * and create a data to response if needed.
7305 * I think to be dealed with mbuf directly.
7307 * msgp : pointer to pointer to a received buffer pulluped.
7308 * This is rewrited to response.
7309 * so : pointer to socket.
7311 * length for buffer to send to user process.
7318 struct sadb_msg *msg;
7319 struct sadb_msghdr mh;
7324 IPSEC_ASSERT(so != NULL, ("null socket"));
7325 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7327 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
7328 KEYDEBUG(KEYDEBUG_KEY_DUMP,
7329 ipseclog((LOG_DEBUG, "%s: passed sadb_msg\n", __func__));
7333 if (m->m_len < sizeof(struct sadb_msg)) {
7334 m = m_pullup(m, sizeof(struct sadb_msg));
7338 msg = mtod(m, struct sadb_msg *);
7339 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
7340 target = KEY_SENDUP_ONE;
7342 if ((m->m_flags & M_PKTHDR) == 0 ||
7343 m->m_pkthdr.len != m->m_pkthdr.len) {
7344 ipseclog((LOG_DEBUG, "%s: invalid message length.\n",__func__));
7345 V_pfkeystat.out_invlen++;
7350 if (msg->sadb_msg_version != PF_KEY_V2) {
7351 ipseclog((LOG_DEBUG, "%s: PF_KEY version %u is mismatched.\n",
7352 __func__, msg->sadb_msg_version));
7353 V_pfkeystat.out_invver++;
7358 if (msg->sadb_msg_type > SADB_MAX) {
7359 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7360 __func__, msg->sadb_msg_type));
7361 V_pfkeystat.out_invmsgtype++;
7366 /* for old-fashioned code - should be nuked */
7367 if (m->m_pkthdr.len > MCLBYTES) {
7374 MGETHDR(n, M_DONTWAIT, MT_DATA);
7375 if (n && m->m_pkthdr.len > MHLEN) {
7376 MCLGET(n, M_DONTWAIT);
7377 if ((n->m_flags & M_EXT) == 0) {
7386 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
7387 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
7393 /* align the mbuf chain so that extensions are in contiguous region. */
7394 error = key_align(m, &mh);
7401 switch (msg->sadb_msg_satype) {
7402 case SADB_SATYPE_UNSPEC:
7403 switch (msg->sadb_msg_type) {
7411 ipseclog((LOG_DEBUG, "%s: must specify satype "
7412 "when msg type=%u.\n", __func__,
7413 msg->sadb_msg_type));
7414 V_pfkeystat.out_invsatype++;
7419 case SADB_SATYPE_AH:
7420 case SADB_SATYPE_ESP:
7421 case SADB_X_SATYPE_IPCOMP:
7422 case SADB_X_SATYPE_TCPSIGNATURE:
7423 switch (msg->sadb_msg_type) {
7425 case SADB_X_SPDDELETE:
7427 case SADB_X_SPDDUMP:
7428 case SADB_X_SPDFLUSH:
7429 case SADB_X_SPDSETIDX:
7430 case SADB_X_SPDUPDATE:
7431 case SADB_X_SPDDELETE2:
7432 ipseclog((LOG_DEBUG, "%s: illegal satype=%u\n",
7433 __func__, msg->sadb_msg_type));
7434 V_pfkeystat.out_invsatype++;
7439 case SADB_SATYPE_RSVP:
7440 case SADB_SATYPE_OSPFV2:
7441 case SADB_SATYPE_RIPV2:
7442 case SADB_SATYPE_MIP:
7443 ipseclog((LOG_DEBUG, "%s: type %u isn't supported.\n",
7444 __func__, msg->sadb_msg_satype));
7445 V_pfkeystat.out_invsatype++;
7448 case 1: /* XXX: What does it do? */
7449 if (msg->sadb_msg_type == SADB_X_PROMISC)
7453 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7454 __func__, msg->sadb_msg_satype));
7455 V_pfkeystat.out_invsatype++;
7460 /* check field of upper layer protocol and address family */
7461 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
7462 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
7463 struct sadb_address *src0, *dst0;
7466 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
7467 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
7469 /* check upper layer protocol */
7470 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
7471 ipseclog((LOG_DEBUG, "%s: upper layer protocol "
7472 "mismatched.\n", __func__));
7473 V_pfkeystat.out_invaddr++;
7479 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
7480 PFKEY_ADDR_SADDR(dst0)->sa_family) {
7481 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
7483 V_pfkeystat.out_invaddr++;
7487 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7488 PFKEY_ADDR_SADDR(dst0)->sa_len) {
7489 ipseclog((LOG_DEBUG, "%s: address struct size "
7490 "mismatched.\n", __func__));
7491 V_pfkeystat.out_invaddr++;
7496 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7498 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7499 sizeof(struct sockaddr_in)) {
7500 V_pfkeystat.out_invaddr++;
7506 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7507 sizeof(struct sockaddr_in6)) {
7508 V_pfkeystat.out_invaddr++;
7514 ipseclog((LOG_DEBUG, "%s: unsupported address family\n",
7516 V_pfkeystat.out_invaddr++;
7517 error = EAFNOSUPPORT;
7521 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7523 plen = sizeof(struct in_addr) << 3;
7526 plen = sizeof(struct in6_addr) << 3;
7529 plen = 0; /*fool gcc*/
7533 /* check max prefix length */
7534 if (src0->sadb_address_prefixlen > plen ||
7535 dst0->sadb_address_prefixlen > plen) {
7536 ipseclog((LOG_DEBUG, "%s: illegal prefixlen.\n",
7538 V_pfkeystat.out_invaddr++;
7544 * prefixlen == 0 is valid because there can be a case when
7545 * all addresses are matched.
7549 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
7550 key_typesw[msg->sadb_msg_type] == NULL) {
7551 V_pfkeystat.out_invmsgtype++;
7556 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
7559 msg->sadb_msg_errno = error;
7560 return key_sendup_mbuf(so, m, target);
7564 key_senderror(so, m, code)
7569 struct sadb_msg *msg;
7571 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7572 ("mbuf too small, len %u", m->m_len));
7574 msg = mtod(m, struct sadb_msg *);
7575 msg->sadb_msg_errno = code;
7576 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
7580 * set the pointer to each header into message buffer.
7581 * m will be freed on error.
7582 * XXX larger-than-MCLBYTES extension?
7587 struct sadb_msghdr *mhp;
7590 struct sadb_ext *ext;
7595 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7596 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7597 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7598 ("mbuf too small, len %u", m->m_len));
7601 bzero(mhp, sizeof(*mhp));
7603 mhp->msg = mtod(m, struct sadb_msg *);
7604 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
7606 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7607 extlen = end; /*just in case extlen is not updated*/
7608 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
7609 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
7611 /* m is already freed */
7614 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7617 switch (ext->sadb_ext_type) {
7619 case SADB_EXT_ADDRESS_SRC:
7620 case SADB_EXT_ADDRESS_DST:
7621 case SADB_EXT_ADDRESS_PROXY:
7622 case SADB_EXT_LIFETIME_CURRENT:
7623 case SADB_EXT_LIFETIME_HARD:
7624 case SADB_EXT_LIFETIME_SOFT:
7625 case SADB_EXT_KEY_AUTH:
7626 case SADB_EXT_KEY_ENCRYPT:
7627 case SADB_EXT_IDENTITY_SRC:
7628 case SADB_EXT_IDENTITY_DST:
7629 case SADB_EXT_SENSITIVITY:
7630 case SADB_EXT_PROPOSAL:
7631 case SADB_EXT_SUPPORTED_AUTH:
7632 case SADB_EXT_SUPPORTED_ENCRYPT:
7633 case SADB_EXT_SPIRANGE:
7634 case SADB_X_EXT_POLICY:
7635 case SADB_X_EXT_SA2:
7637 case SADB_X_EXT_NAT_T_TYPE:
7638 case SADB_X_EXT_NAT_T_SPORT:
7639 case SADB_X_EXT_NAT_T_DPORT:
7640 case SADB_X_EXT_NAT_T_OAI:
7641 case SADB_X_EXT_NAT_T_OAR:
7642 case SADB_X_EXT_NAT_T_FRAG:
7644 /* duplicate check */
7646 * XXX Are there duplication payloads of either
7647 * KEY_AUTH or KEY_ENCRYPT ?
7649 if (mhp->ext[ext->sadb_ext_type] != NULL) {
7650 ipseclog((LOG_DEBUG, "%s: duplicate ext_type "
7651 "%u\n", __func__, ext->sadb_ext_type));
7653 V_pfkeystat.out_dupext++;
7658 ipseclog((LOG_DEBUG, "%s: invalid ext_type %u\n",
7659 __func__, ext->sadb_ext_type));
7661 V_pfkeystat.out_invexttype++;
7665 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
7667 if (key_validate_ext(ext, extlen)) {
7669 V_pfkeystat.out_invlen++;
7673 n = m_pulldown(m, off, extlen, &toff);
7675 /* m is already freed */
7678 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7680 mhp->ext[ext->sadb_ext_type] = ext;
7681 mhp->extoff[ext->sadb_ext_type] = off;
7682 mhp->extlen[ext->sadb_ext_type] = extlen;
7687 V_pfkeystat.out_invlen++;
7695 key_validate_ext(ext, len)
7696 const struct sadb_ext *ext;
7699 const struct sockaddr *sa;
7700 enum { NONE, ADDR } checktype = NONE;
7702 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
7704 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
7707 /* if it does not match minimum/maximum length, bail */
7708 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
7709 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
7711 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
7713 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
7716 /* more checks based on sadb_ext_type XXX need more */
7717 switch (ext->sadb_ext_type) {
7718 case SADB_EXT_ADDRESS_SRC:
7719 case SADB_EXT_ADDRESS_DST:
7720 case SADB_EXT_ADDRESS_PROXY:
7721 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7724 case SADB_EXT_IDENTITY_SRC:
7725 case SADB_EXT_IDENTITY_DST:
7726 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7727 SADB_X_IDENTTYPE_ADDR) {
7728 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7738 switch (checktype) {
7742 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
7743 if (len < baselen + sal)
7745 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7758 for (i = 0; i < IPSEC_DIR_MAX; i++)
7759 LIST_INIT(&V_sptree[i]);
7761 LIST_INIT(&V_sahtree);
7763 for (i = 0; i <= SADB_SATYPE_MAX; i++)
7764 LIST_INIT(&V_regtree[i]);
7766 LIST_INIT(&V_acqtree);
7767 LIST_INIT(&V_spacqtree);
7769 /* system default */
7770 V_ip4_def_policy.policy = IPSEC_POLICY_NONE;
7771 V_ip4_def_policy.refcnt++; /*never reclaim this*/
7773 if (!IS_DEFAULT_VNET(curvnet))
7777 REGTREE_LOCK_INIT();
7778 SAHTREE_LOCK_INIT();
7782 #ifndef IPSEC_DEBUG2
7783 timeout((void *)key_timehandler, (void *)0, hz);
7784 #endif /*IPSEC_DEBUG2*/
7786 /* initialize key statistics */
7787 keystat.getspi_count = 1;
7789 printf("IPsec: Initialized Security Association Processing.\n");
7796 struct secpolicy *sp, *nextsp;
7797 struct secacq *acq, *nextacq;
7798 struct secspacq *spacq, *nextspacq;
7799 struct secashead *sah, *nextsah;
7804 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7805 for (sp = LIST_FIRST(&V_sptree[i]);
7806 sp != NULL; sp = nextsp) {
7807 nextsp = LIST_NEXT(sp, chain);
7808 if (__LIST_CHAINED(sp)) {
7809 LIST_REMOVE(sp, chain);
7810 free(sp, M_IPSEC_SP);
7817 for (sah = LIST_FIRST(&V_sahtree); sah != NULL; sah = nextsah) {
7818 nextsah = LIST_NEXT(sah, chain);
7819 if (__LIST_CHAINED(sah)) {
7820 LIST_REMOVE(sah, chain);
7821 free(sah, M_IPSEC_SAH);
7827 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7828 LIST_FOREACH(reg, &V_regtree[i], chain) {
7829 if (__LIST_CHAINED(reg)) {
7830 LIST_REMOVE(reg, chain);
7831 free(reg, M_IPSEC_SAR);
7839 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
7840 nextacq = LIST_NEXT(acq, chain);
7841 if (__LIST_CHAINED(acq)) {
7842 LIST_REMOVE(acq, chain);
7843 free(acq, M_IPSEC_SAQ);
7849 for (spacq = LIST_FIRST(&V_spacqtree); spacq != NULL;
7850 spacq = nextspacq) {
7851 nextspacq = LIST_NEXT(spacq, chain);
7852 if (__LIST_CHAINED(spacq)) {
7853 LIST_REMOVE(spacq, chain);
7854 free(spacq, M_IPSEC_SAQ);
7862 * XXX: maybe This function is called after INBOUND IPsec processing.
7864 * Special check for tunnel-mode packets.
7865 * We must make some checks for consistency between inner and outer IP header.
7867 * xxx more checks to be provided
7870 key_checktunnelsanity(sav, family, src, dst)
7871 struct secasvar *sav;
7876 IPSEC_ASSERT(sav->sah != NULL, ("null SA header"));
7878 /* XXX: check inner IP header */
7883 /* record data transfer on SA, and update timestamps */
7885 key_sa_recordxfer(sav, m)
7886 struct secasvar *sav;
7889 IPSEC_ASSERT(sav != NULL, ("Null secasvar"));
7890 IPSEC_ASSERT(m != NULL, ("Null mbuf"));
7895 * XXX Currently, there is a difference of bytes size
7896 * between inbound and outbound processing.
7898 sav->lft_c->bytes += m->m_pkthdr.len;
7899 /* to check bytes lifetime is done in key_timehandler(). */
7902 * We use the number of packets as the unit of
7903 * allocations. We increment the variable
7904 * whenever {esp,ah}_{in,out}put is called.
7906 sav->lft_c->allocations++;
7907 /* XXX check for expires? */
7910 * NOTE: We record CURRENT usetime by using wall clock,
7911 * in seconds. HARD and SOFT lifetime are measured by the time
7912 * difference (again in seconds) from usetime.
7916 * -----+-----+--------+---> t
7917 * <--------------> HARD
7920 sav->lft_c->usetime = time_second;
7921 /* XXX check for expires? */
7928 key_sa_routechange(dst)
7929 struct sockaddr *dst;
7931 struct secashead *sah;
7935 LIST_FOREACH(sah, &V_sahtree, chain) {
7936 ro = &sah->sa_route;
7937 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7938 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7940 ro->ro_rt = (struct rtentry *)NULL;
7947 key_sa_chgstate(struct secasvar *sav, u_int8_t state)
7949 IPSEC_ASSERT(sav != NULL, ("NULL sav"));
7950 SAHTREE_LOCK_ASSERT();
7952 if (sav->state != state) {
7953 if (__LIST_CHAINED(sav))
7954 LIST_REMOVE(sav, chain);
7956 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7962 struct secasvar *sav;
7965 IPSEC_ASSERT(sav->iv != NULL, ("null IV"));
7966 key_randomfill(sav->iv, sav->ivlen);
7970 static struct mbuf *
7974 struct mbuf *m = NULL, *n;
7979 MGET(n, M_DONTWAIT, MT_DATA);
7980 if (n && len > MLEN)
7981 MCLGET(n, M_DONTWAIT);
7989 n->m_len = M_TRAILINGSPACE(n);
7990 /* use the bottom of mbuf, hoping we can prepend afterwards */
7991 if (n->m_len > len) {
7992 t = (n->m_len - len) & ~(sizeof(long) - 1);
8009 * Take one of the kernel's security keys and convert it into a PF_KEY
8010 * structure within an mbuf, suitable for sending up to a waiting
8011 * application in user land.
8014 * src: A pointer to a kernel security key.
8015 * exttype: Which type of key this is. Refer to the PF_KEY data structures.
8017 * a valid mbuf or NULL indicating an error
8021 static struct mbuf *
8022 key_setkey(struct seckey *src, u_int16_t exttype)
8031 len = PFKEY_ALIGN8(sizeof(struct sadb_key) + _KEYLEN(src));
8032 m = key_alloc_mbuf(len);
8035 p = mtod(m, struct sadb_key *);
8037 p->sadb_key_len = PFKEY_UNIT64(len);
8038 p->sadb_key_exttype = exttype;
8039 p->sadb_key_bits = src->bits;
8040 bcopy(src->key_data, _KEYBUF(p), _KEYLEN(src));
8046 * Take one of the kernel's lifetime data structures and convert it
8047 * into a PF_KEY structure within an mbuf, suitable for sending up to
8048 * a waiting application in user land.
8051 * src: A pointer to a kernel lifetime structure.
8052 * exttype: Which type of lifetime this is. Refer to the PF_KEY
8053 * data structures for more information.
8055 * a valid mbuf or NULL indicating an error
8059 static struct mbuf *
8060 key_setlifetime(struct seclifetime *src, u_int16_t exttype)
8062 struct mbuf *m = NULL;
8063 struct sadb_lifetime *p;
8064 int len = PFKEY_ALIGN8(sizeof(struct sadb_lifetime));
8069 m = key_alloc_mbuf(len);
8072 p = mtod(m, struct sadb_lifetime *);
8075 p->sadb_lifetime_len = PFKEY_UNIT64(len);
8076 p->sadb_lifetime_exttype = exttype;
8077 p->sadb_lifetime_allocations = src->allocations;
8078 p->sadb_lifetime_bytes = src->bytes;
8079 p->sadb_lifetime_addtime = src->addtime;
8080 p->sadb_lifetime_usetime = src->usetime;