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 #define V_key_spi_trycnt VNET(key_spi_trycnt)
118 static VNET_DEFINE(u_int32_t, key_spi_minval) = 0x100;
119 #define V_key_spi_minval VNET(key_spi_minval)
120 static VNET_DEFINE(u_int32_t, key_spi_maxval) = 0x0fffffff; /* XXX */
121 #define V_key_spi_maxval VNET(key_spi_maxval)
122 static VNET_DEFINE(u_int32_t, policy_id) = 0;
123 #define V_policy_id VNET(policy_id)
124 /*interval to initialize randseed,1(m)*/
125 static VNET_DEFINE(u_int, key_int_random) = 60;
126 #define V_key_int_random VNET(key_int_random)
127 /* interval to expire acquiring, 30(s)*/
128 static VNET_DEFINE(u_int, key_larval_lifetime) = 30;
129 #define V_key_larval_lifetime VNET(key_larval_lifetime)
130 /* counter for blocking SADB_ACQUIRE.*/
131 static VNET_DEFINE(int, key_blockacq_count) = 10;
132 #define V_key_blockacq_count VNET(key_blockacq_count)
133 /* lifetime for blocking SADB_ACQUIRE.*/
134 static VNET_DEFINE(int, key_blockacq_lifetime) = 20;
135 #define V_key_blockacq_lifetime VNET(key_blockacq_lifetime)
136 /* preferred old sa rather than new sa.*/
137 static VNET_DEFINE(int, key_preferred_oldsa) = 1;
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 #define V_ipsec_esp_keymin VNET(ipsec_esp_keymin)
274 static VNET_DEFINE(int, ipsec_esp_auth) = 0;
275 #define V_ipsec_esp_auth VNET(ipsec_esp_auth)
276 static VNET_DEFINE(int, ipsec_ah_keymin) = 128;
277 #define V_ipsec_ah_keymin VNET(ipsec_ah_keymin)
280 SYSCTL_DECL(_net_key);
283 SYSCTL_VNET_INT(_net_key, KEYCTL_DEBUG_LEVEL, debug,
284 CTLFLAG_RW, &VNET_NAME(key_debug_level), 0, "");
286 /* max count of trial for the decision of spi value */
287 SYSCTL_VNET_INT(_net_key, KEYCTL_SPI_TRY, spi_trycnt,
288 CTLFLAG_RW, &VNET_NAME(key_spi_trycnt), 0, "");
290 /* minimum spi value to allocate automatically. */
291 SYSCTL_VNET_INT(_net_key, KEYCTL_SPI_MIN_VALUE,
292 spi_minval, CTLFLAG_RW, &VNET_NAME(key_spi_minval), 0, "");
294 /* maximun spi value to allocate automatically. */
295 SYSCTL_VNET_INT(_net_key, KEYCTL_SPI_MAX_VALUE,
296 spi_maxval, CTLFLAG_RW, &VNET_NAME(key_spi_maxval), 0, "");
298 /* interval to initialize randseed */
299 SYSCTL_VNET_INT(_net_key, KEYCTL_RANDOM_INT,
300 int_random, CTLFLAG_RW, &VNET_NAME(key_int_random), 0, "");
302 /* lifetime for larval SA */
303 SYSCTL_VNET_INT(_net_key, KEYCTL_LARVAL_LIFETIME,
304 larval_lifetime, CTLFLAG_RW, &VNET_NAME(key_larval_lifetime), 0, "");
306 /* counter for blocking to send SADB_ACQUIRE to IKEd */
307 SYSCTL_VNET_INT(_net_key, KEYCTL_BLOCKACQ_COUNT,
308 blockacq_count, CTLFLAG_RW, &VNET_NAME(key_blockacq_count), 0, "");
310 /* lifetime for blocking to send SADB_ACQUIRE to IKEd */
311 SYSCTL_VNET_INT(_net_key, KEYCTL_BLOCKACQ_LIFETIME,
312 blockacq_lifetime, CTLFLAG_RW, &VNET_NAME(key_blockacq_lifetime), 0, "");
315 SYSCTL_VNET_INT(_net_key, KEYCTL_ESP_AUTH, esp_auth,
316 CTLFLAG_RW, &VNET_NAME(ipsec_esp_auth), 0, "");
318 /* minimum ESP key length */
319 SYSCTL_VNET_INT(_net_key, KEYCTL_ESP_KEYMIN,
320 esp_keymin, CTLFLAG_RW, &VNET_NAME(ipsec_esp_keymin), 0, "");
322 /* minimum AH key length */
323 SYSCTL_VNET_INT(_net_key, KEYCTL_AH_KEYMIN, ah_keymin,
324 CTLFLAG_RW, &VNET_NAME(ipsec_ah_keymin), 0, "");
326 /* perfered old SA rather than new SA */
327 SYSCTL_VNET_INT(_net_key, KEYCTL_PREFERED_OLDSA,
328 preferred_oldsa, CTLFLAG_RW, &VNET_NAME(key_preferred_oldsa), 0, "");
330 #define __LIST_CHAINED(elm) \
331 (!((elm)->chain.le_next == NULL && (elm)->chain.le_prev == NULL))
332 #define LIST_INSERT_TAIL(head, elm, type, field) \
334 struct type *curelm = LIST_FIRST(head); \
335 if (curelm == NULL) {\
336 LIST_INSERT_HEAD(head, elm, field); \
338 while (LIST_NEXT(curelm, field)) \
339 curelm = LIST_NEXT(curelm, field);\
340 LIST_INSERT_AFTER(curelm, elm, field);\
344 #define KEY_CHKSASTATE(head, sav, name) \
346 if ((head) != (sav)) { \
347 ipseclog((LOG_DEBUG, "%s: state mismatched (TREE=%d SA=%d)\n", \
348 (name), (head), (sav))); \
353 #define KEY_CHKSPDIR(head, sp, name) \
355 if ((head) != (sp)) { \
356 ipseclog((LOG_DEBUG, "%s: direction mismatched (TREE=%d SP=%d), " \
357 "anyway continue.\n", \
358 (name), (head), (sp))); \
362 MALLOC_DEFINE(M_IPSEC_SA, "secasvar", "ipsec security association");
363 MALLOC_DEFINE(M_IPSEC_SAH, "sahead", "ipsec sa head");
364 MALLOC_DEFINE(M_IPSEC_SP, "ipsecpolicy", "ipsec security policy");
365 MALLOC_DEFINE(M_IPSEC_SR, "ipsecrequest", "ipsec security request");
366 MALLOC_DEFINE(M_IPSEC_MISC, "ipsec-misc", "ipsec miscellaneous");
367 MALLOC_DEFINE(M_IPSEC_SAQ, "ipsec-saq", "ipsec sa acquire");
368 MALLOC_DEFINE(M_IPSEC_SAR, "ipsec-reg", "ipsec sa acquire");
371 * set parameters into secpolicyindex buffer.
372 * Must allocate secpolicyindex buffer passed to this function.
374 #define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \
376 bzero((idx), sizeof(struct secpolicyindex)); \
377 (idx)->dir = (_dir); \
378 (idx)->prefs = (ps); \
379 (idx)->prefd = (pd); \
380 (idx)->ul_proto = (ulp); \
381 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
382 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
386 * set parameters into secasindex buffer.
387 * Must allocate secasindex buffer before calling this function.
389 #define KEY_SETSECASIDX(p, m, r, s, d, idx) \
391 bzero((idx), sizeof(struct secasindex)); \
392 (idx)->proto = (p); \
394 (idx)->reqid = (r); \
395 bcopy((s), &(idx)->src, ((const struct sockaddr *)(s))->sa_len); \
396 bcopy((d), &(idx)->dst, ((const struct sockaddr *)(d))->sa_len); \
401 u_long getspi_count; /* the avarage of count to try to get new SPI */
405 struct sadb_msg *msg;
406 struct sadb_ext *ext[SADB_EXT_MAX + 1];
407 int extoff[SADB_EXT_MAX + 1];
408 int extlen[SADB_EXT_MAX + 1];
411 static struct secasvar *key_allocsa_policy __P((const struct secasindex *));
412 static void key_freesp_so __P((struct secpolicy **));
413 static struct secasvar *key_do_allocsa_policy __P((struct secashead *, u_int));
414 static void key_delsp __P((struct secpolicy *));
415 static struct secpolicy *key_getsp __P((struct secpolicyindex *));
416 static void _key_delsp(struct secpolicy *sp);
417 static struct secpolicy *key_getspbyid __P((u_int32_t));
418 static u_int32_t key_newreqid __P((void));
419 static struct mbuf *key_gather_mbuf __P((struct mbuf *,
420 const struct sadb_msghdr *, int, int, ...));
421 static int key_spdadd __P((struct socket *, struct mbuf *,
422 const struct sadb_msghdr *));
423 static u_int32_t key_getnewspid __P((void));
424 static int key_spddelete __P((struct socket *, struct mbuf *,
425 const struct sadb_msghdr *));
426 static int key_spddelete2 __P((struct socket *, struct mbuf *,
427 const struct sadb_msghdr *));
428 static int key_spdget __P((struct socket *, struct mbuf *,
429 const struct sadb_msghdr *));
430 static int key_spdflush __P((struct socket *, struct mbuf *,
431 const struct sadb_msghdr *));
432 static int key_spddump __P((struct socket *, struct mbuf *,
433 const struct sadb_msghdr *));
434 static struct mbuf *key_setdumpsp __P((struct secpolicy *,
435 u_int8_t, u_int32_t, u_int32_t));
436 static u_int key_getspreqmsglen __P((struct secpolicy *));
437 static int key_spdexpire __P((struct secpolicy *));
438 static struct secashead *key_newsah __P((struct secasindex *));
439 static void key_delsah __P((struct secashead *));
440 static struct secasvar *key_newsav __P((struct mbuf *,
441 const struct sadb_msghdr *, struct secashead *, int *,
443 #define KEY_NEWSAV(m, sadb, sah, e) \
444 key_newsav(m, sadb, sah, e, __FILE__, __LINE__)
445 static void key_delsav __P((struct secasvar *));
446 static struct secashead *key_getsah __P((struct secasindex *));
447 static struct secasvar *key_checkspidup __P((struct secasindex *, u_int32_t));
448 static struct secasvar *key_getsavbyspi __P((struct secashead *, u_int32_t));
449 static int key_setsaval __P((struct secasvar *, struct mbuf *,
450 const struct sadb_msghdr *));
451 static int key_mature __P((struct secasvar *));
452 static struct mbuf *key_setdumpsa __P((struct secasvar *, u_int8_t,
453 u_int8_t, u_int32_t, u_int32_t));
454 static struct mbuf *key_setsadbmsg __P((u_int8_t, u_int16_t, u_int8_t,
455 u_int32_t, pid_t, u_int16_t));
456 static struct mbuf *key_setsadbsa __P((struct secasvar *));
457 static struct mbuf *key_setsadbaddr __P((u_int16_t,
458 const struct sockaddr *, u_int8_t, u_int16_t));
460 static struct mbuf *key_setsadbxport(u_int16_t, u_int16_t);
461 static struct mbuf *key_setsadbxtype(u_int16_t);
463 static void key_porttosaddr(struct sockaddr *, u_int16_t);
464 #define KEY_PORTTOSADDR(saddr, port) \
465 key_porttosaddr((struct sockaddr *)(saddr), (port))
466 static struct mbuf *key_setsadbxsa2 __P((u_int8_t, u_int32_t, u_int32_t));
467 static struct mbuf *key_setsadbxpolicy __P((u_int16_t, u_int8_t,
469 static struct seckey *key_dup_keymsg(const struct sadb_key *, u_int,
470 struct malloc_type *);
471 static struct seclifetime *key_dup_lifemsg(const struct sadb_lifetime *src,
472 struct malloc_type *type);
474 static int key_ismyaddr6 __P((struct sockaddr_in6 *));
477 /* flags for key_cmpsaidx() */
478 #define CMP_HEAD 1 /* protocol, addresses. */
479 #define CMP_MODE_REQID 2 /* additionally HEAD, reqid, mode. */
480 #define CMP_REQID 3 /* additionally HEAD, reaid. */
481 #define CMP_EXACTLY 4 /* all elements. */
482 static int key_cmpsaidx
483 __P((const struct secasindex *, const struct secasindex *, int));
485 static int key_cmpspidx_exactly
486 __P((struct secpolicyindex *, struct secpolicyindex *));
487 static int key_cmpspidx_withmask
488 __P((struct secpolicyindex *, struct secpolicyindex *));
489 static int key_sockaddrcmp __P((const struct sockaddr *, const struct sockaddr *, int));
490 static int key_bbcmp __P((const void *, const void *, u_int));
491 static u_int16_t key_satype2proto __P((u_int8_t));
492 static u_int8_t key_proto2satype __P((u_int16_t));
494 static int key_getspi __P((struct socket *, struct mbuf *,
495 const struct sadb_msghdr *));
496 static u_int32_t key_do_getnewspi __P((struct sadb_spirange *,
497 struct secasindex *));
498 static int key_update __P((struct socket *, struct mbuf *,
499 const struct sadb_msghdr *));
500 #ifdef IPSEC_DOSEQCHECK
501 static struct secasvar *key_getsavbyseq __P((struct secashead *, u_int32_t));
503 static int key_add __P((struct socket *, struct mbuf *,
504 const struct sadb_msghdr *));
505 static int key_setident __P((struct secashead *, struct mbuf *,
506 const struct sadb_msghdr *));
507 static struct mbuf *key_getmsgbuf_x1 __P((struct mbuf *,
508 const struct sadb_msghdr *));
509 static int key_delete __P((struct socket *, struct mbuf *,
510 const struct sadb_msghdr *));
511 static int key_get __P((struct socket *, struct mbuf *,
512 const struct sadb_msghdr *));
514 static void key_getcomb_setlifetime __P((struct sadb_comb *));
515 static struct mbuf *key_getcomb_esp __P((void));
516 static struct mbuf *key_getcomb_ah __P((void));
517 static struct mbuf *key_getcomb_ipcomp __P((void));
518 static struct mbuf *key_getprop __P((const struct secasindex *));
520 static int key_acquire __P((const struct secasindex *, struct secpolicy *));
521 static struct secacq *key_newacq __P((const struct secasindex *));
522 static struct secacq *key_getacq __P((const struct secasindex *));
523 static struct secacq *key_getacqbyseq __P((u_int32_t));
524 static struct secspacq *key_newspacq __P((struct secpolicyindex *));
525 static struct secspacq *key_getspacq __P((struct secpolicyindex *));
526 static int key_acquire2 __P((struct socket *, struct mbuf *,
527 const struct sadb_msghdr *));
528 static int key_register __P((struct socket *, struct mbuf *,
529 const struct sadb_msghdr *));
530 static int key_expire __P((struct secasvar *));
531 static int key_flush __P((struct socket *, struct mbuf *,
532 const struct sadb_msghdr *));
533 static int key_dump __P((struct socket *, struct mbuf *,
534 const struct sadb_msghdr *));
535 static int key_promisc __P((struct socket *, struct mbuf *,
536 const struct sadb_msghdr *));
537 static int key_senderror __P((struct socket *, struct mbuf *, int));
538 static int key_validate_ext __P((const struct sadb_ext *, int));
539 static int key_align __P((struct mbuf *, struct sadb_msghdr *));
540 static struct mbuf *key_setlifetime(struct seclifetime *src,
542 static struct mbuf *key_setkey(struct seckey *src, u_int16_t exttype);
545 static const char *key_getfqdn __P((void));
546 static const char *key_getuserfqdn __P((void));
548 static void key_sa_chgstate __P((struct secasvar *, u_int8_t));
549 static struct mbuf *key_alloc_mbuf __P((int));
552 sa_initref(struct secasvar *sav)
555 refcount_init(&sav->refcnt, 1);
558 sa_addref(struct secasvar *sav)
561 refcount_acquire(&sav->refcnt);
562 IPSEC_ASSERT(sav->refcnt != 0, ("SA refcnt overflow"));
565 sa_delref(struct secasvar *sav)
568 IPSEC_ASSERT(sav->refcnt > 0, ("SA refcnt underflow"));
569 return (refcount_release(&sav->refcnt));
572 #define SP_ADDREF(p) do { \
574 IPSEC_ASSERT((p)->refcnt != 0, ("SP refcnt overflow")); \
576 #define SP_DELREF(p) do { \
577 IPSEC_ASSERT((p)->refcnt > 0, ("SP refcnt underflow")); \
583 * Update the refcnt while holding the SPTREE lock.
586 key_addref(struct secpolicy *sp)
594 * Return 0 when there are known to be no SP's for the specified
595 * direction. Otherwise return 1. This is used by IPsec code
596 * to optimize performance.
599 key_havesp(u_int dir)
602 return (dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND ?
603 LIST_FIRST(&V_sptree[dir]) != NULL : 1);
606 /* %%% IPsec policy management */
608 * allocating a SP for OUTBOUND or INBOUND packet.
609 * Must call key_freesp() later.
610 * OUT: NULL: not found
611 * others: found and return the pointer.
614 key_allocsp(struct secpolicyindex *spidx, u_int dir, const char* where, int tag)
616 struct secpolicy *sp;
618 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
619 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
620 ("invalid direction %u", dir));
622 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
623 printf("DP %s from %s:%u\n", __func__, where, tag));
626 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
627 printf("*** objects\n");
628 kdebug_secpolicyindex(spidx));
631 LIST_FOREACH(sp, &V_sptree[dir], chain) {
632 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
633 printf("*** in SPD\n");
634 kdebug_secpolicyindex(&sp->spidx));
636 if (sp->state == IPSEC_SPSTATE_DEAD)
638 if (key_cmpspidx_withmask(&sp->spidx, spidx))
645 KEY_CHKSPDIR(sp->spidx.dir, dir, __func__);
647 /* found a SPD entry */
648 sp->lastused = time_second;
653 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
654 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
655 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
660 * allocating a SP for OUTBOUND or INBOUND packet.
661 * Must call key_freesp() later.
662 * OUT: NULL: not found
663 * others: found and return the pointer.
666 key_allocsp2(u_int32_t spi,
667 union sockaddr_union *dst,
670 const char* where, int tag)
672 struct secpolicy *sp;
674 IPSEC_ASSERT(dst != NULL, ("null dst"));
675 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
676 ("invalid direction %u", dir));
678 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
679 printf("DP %s from %s:%u\n", __func__, where, tag));
682 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
683 printf("*** objects\n");
684 printf("spi %u proto %u dir %u\n", spi, proto, dir);
685 kdebug_sockaddr(&dst->sa));
688 LIST_FOREACH(sp, &V_sptree[dir], chain) {
689 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
690 printf("*** in SPD\n");
691 kdebug_secpolicyindex(&sp->spidx));
693 if (sp->state == IPSEC_SPSTATE_DEAD)
695 /* compare simple values, then dst address */
696 if (sp->spidx.ul_proto != proto)
698 /* NB: spi's must exist and match */
699 if (!sp->req || !sp->req->sav || sp->req->sav->spi != spi)
701 if (key_sockaddrcmp(&sp->spidx.dst.sa, &dst->sa, 1) == 0)
708 KEY_CHKSPDIR(sp->spidx.dir, dir, __func__);
710 /* found a SPD entry */
711 sp->lastused = time_second;
716 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
717 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
718 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
724 * return a policy that matches this particular inbound packet.
728 key_gettunnel(const struct sockaddr *osrc,
729 const struct sockaddr *odst,
730 const struct sockaddr *isrc,
731 const struct sockaddr *idst,
732 const char* where, int tag)
734 struct secpolicy *sp;
735 const int dir = IPSEC_DIR_INBOUND;
736 struct ipsecrequest *r1, *r2, *p;
737 struct secpolicyindex spidx;
739 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
740 printf("DP %s from %s:%u\n", __func__, where, tag));
742 if (isrc->sa_family != idst->sa_family) {
743 ipseclog((LOG_ERR, "%s: protocol family mismatched %d != %d\n.",
744 __func__, isrc->sa_family, idst->sa_family));
750 LIST_FOREACH(sp, &V_sptree[dir], chain) {
751 if (sp->state == IPSEC_SPSTATE_DEAD)
755 for (p = sp->req; p; p = p->next) {
756 if (p->saidx.mode != IPSEC_MODE_TUNNEL)
763 /* here we look at address matches only */
765 if (isrc->sa_len > sizeof(spidx.src) ||
766 idst->sa_len > sizeof(spidx.dst))
768 bcopy(isrc, &spidx.src, isrc->sa_len);
769 bcopy(idst, &spidx.dst, idst->sa_len);
770 if (!key_cmpspidx_withmask(&sp->spidx, &spidx))
773 if (key_sockaddrcmp(&r1->saidx.src.sa, isrc, 0) ||
774 key_sockaddrcmp(&r1->saidx.dst.sa, idst, 0))
778 if (key_sockaddrcmp(&r2->saidx.src.sa, osrc, 0) ||
779 key_sockaddrcmp(&r2->saidx.dst.sa, odst, 0))
788 sp->lastused = time_second;
793 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
794 printf("DP %s return SP:%p (ID=%u) refcnt %u\n", __func__,
795 sp, sp ? sp->id : 0, sp ? sp->refcnt : 0));
801 * allocating an SA entry for an *OUTBOUND* packet.
802 * checking each request entries in SP, and acquire an SA if need.
803 * OUT: 0: there are valid requests.
804 * ENOENT: policy may be valid, but SA with REQUIRE is on acquiring.
807 key_checkrequest(struct ipsecrequest *isr, const struct secasindex *saidx)
812 IPSEC_ASSERT(isr != NULL, ("null isr"));
813 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
814 IPSEC_ASSERT(saidx->mode == IPSEC_MODE_TRANSPORT ||
815 saidx->mode == IPSEC_MODE_TUNNEL,
816 ("unexpected policy %u", saidx->mode));
819 * XXX guard against protocol callbacks from the crypto
820 * thread as they reference ipsecrequest.sav which we
821 * temporarily null out below. Need to rethink how we
822 * handle bundled SA's in the callback thread.
824 IPSECREQUEST_LOCK_ASSERT(isr);
826 /* get current level */
827 level = ipsec_get_reqlevel(isr);
830 * We do allocate new SA only if the state of SA in the holder is
831 * SADB_SASTATE_DEAD. The SA for outbound must be the oldest.
833 if (isr->sav != NULL) {
834 if (isr->sav->sah == NULL)
835 panic("%s: sah is null.\n", __func__);
836 if (isr->sav == (struct secasvar *)LIST_FIRST(
837 &isr->sav->sah->savtree[SADB_SASTATE_DEAD])) {
838 KEY_FREESAV(&isr->sav);
844 * we free any SA stashed in the IPsec request because a different
845 * SA may be involved each time this request is checked, either
846 * because new SAs are being configured, or this request is
847 * associated with an unconnected datagram socket, or this request
848 * is associated with a system default policy.
850 * The operation may have negative impact to performance. We may
851 * want to check cached SA carefully, rather than picking new SA
854 if (isr->sav != NULL) {
855 KEY_FREESAV(&isr->sav);
861 * new SA allocation if no SA found.
862 * key_allocsa_policy should allocate the oldest SA available.
863 * See key_do_allocsa_policy(), and draft-jenkins-ipsec-rekeying-03.txt.
865 if (isr->sav == NULL)
866 isr->sav = key_allocsa_policy(saidx);
868 /* When there is SA. */
869 if (isr->sav != NULL) {
870 if (isr->sav->state != SADB_SASTATE_MATURE &&
871 isr->sav->state != SADB_SASTATE_DYING)
877 error = key_acquire(saidx, isr->sp);
879 /* XXX What should I do ? */
880 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
885 if (level != IPSEC_LEVEL_REQUIRE) {
886 /* XXX sigh, the interface to this routine is botched */
887 IPSEC_ASSERT(isr->sav == NULL, ("unexpected SA"));
895 * allocating a SA for policy entry from SAD.
896 * NOTE: searching SAD of aliving state.
897 * OUT: NULL: not found.
898 * others: found and return the pointer.
900 static struct secasvar *
901 key_allocsa_policy(const struct secasindex *saidx)
903 #define N(a) _ARRAYLEN(a)
904 struct secashead *sah;
905 struct secasvar *sav;
906 u_int stateidx, arraysize;
907 const u_int *state_valid;
910 LIST_FOREACH(sah, &V_sahtree, chain) {
911 if (sah->state == SADB_SASTATE_DEAD)
913 if (key_cmpsaidx(&sah->saidx, saidx, CMP_MODE_REQID)) {
914 if (V_key_preferred_oldsa) {
915 state_valid = saorder_state_valid_prefer_old;
916 arraysize = N(saorder_state_valid_prefer_old);
918 state_valid = saorder_state_valid_prefer_new;
919 arraysize = N(saorder_state_valid_prefer_new);
930 /* search valid state */
931 for (stateidx = 0; stateidx < arraysize; stateidx++) {
932 sav = key_do_allocsa_policy(sah, state_valid[stateidx]);
942 * searching SAD with direction, protocol, mode and state.
943 * called by key_allocsa_policy().
946 * others : found, pointer to a SA.
948 static struct secasvar *
949 key_do_allocsa_policy(struct secashead *sah, u_int state)
951 struct secasvar *sav, *nextsav, *candidate, *d;
957 for (sav = LIST_FIRST(&sah->savtree[state]);
961 nextsav = LIST_NEXT(sav, chain);
964 KEY_CHKSASTATE(sav->state, state, __func__);
967 if (candidate == NULL) {
972 /* Which SA is the better ? */
974 IPSEC_ASSERT(candidate->lft_c != NULL,
975 ("null candidate lifetime"));
976 IPSEC_ASSERT(sav->lft_c != NULL, ("null sav lifetime"));
978 /* What the best method is to compare ? */
979 if (V_key_preferred_oldsa) {
980 if (candidate->lft_c->addtime >
981 sav->lft_c->addtime) {
988 /* preferred new sa rather than old sa */
989 if (candidate->lft_c->addtime <
990 sav->lft_c->addtime) {
997 * prepared to delete the SA when there is more
998 * suitable candidate and the lifetime of the SA is not
1001 if (d->lft_h->addtime != 0) {
1002 struct mbuf *m, *result;
1005 key_sa_chgstate(d, SADB_SASTATE_DEAD);
1007 IPSEC_ASSERT(d->refcnt > 0, ("bogus ref count"));
1009 satype = key_proto2satype(d->sah->saidx.proto);
1013 m = key_setsadbmsg(SADB_DELETE, 0,
1014 satype, 0, 0, d->refcnt - 1);
1019 /* set sadb_address for saidx's. */
1020 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
1021 &d->sah->saidx.src.sa,
1022 d->sah->saidx.src.sa.sa_len << 3,
1028 /* set sadb_address for saidx's. */
1029 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
1030 &d->sah->saidx.dst.sa,
1031 d->sah->saidx.dst.sa.sa_len << 3,
1037 /* create SA extension */
1038 m = key_setsadbsa(d);
1043 if (result->m_len < sizeof(struct sadb_msg)) {
1044 result = m_pullup(result,
1045 sizeof(struct sadb_msg));
1050 result->m_pkthdr.len = 0;
1051 for (m = result; m; m = m->m_next)
1052 result->m_pkthdr.len += m->m_len;
1053 mtod(result, struct sadb_msg *)->sadb_msg_len =
1054 PFKEY_UNIT64(result->m_pkthdr.len);
1056 if (key_sendup_mbuf(NULL, result,
1057 KEY_SENDUP_REGISTERED))
1064 sa_addref(candidate);
1065 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1066 printf("DP %s cause refcnt++:%d SA:%p\n",
1067 __func__, candidate->refcnt, candidate));
1075 * allocating a usable SA entry for a *INBOUND* packet.
1076 * Must call key_freesav() later.
1077 * OUT: positive: pointer to a usable sav (i.e. MATURE or DYING state).
1078 * NULL: not found, or error occured.
1080 * In the comparison, no source address is used--for RFC2401 conformance.
1081 * To quote, from section 4.1:
1082 * A security association is uniquely identified by a triple consisting
1083 * of a Security Parameter Index (SPI), an IP Destination Address, and a
1084 * security protocol (AH or ESP) identifier.
1085 * Note that, however, we do need to keep source address in IPsec SA.
1086 * IKE specification and PF_KEY specification do assume that we
1087 * keep source address in IPsec SA. We see a tricky situation here.
1091 union sockaddr_union *dst,
1094 const char* where, int tag)
1096 struct secashead *sah;
1097 struct secasvar *sav;
1098 u_int stateidx, arraysize, state;
1099 const u_int *saorder_state_valid;
1102 IPSEC_ASSERT(dst != NULL, ("null dst address"));
1104 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1105 printf("DP %s from %s:%u\n", __func__, where, tag));
1108 chkport = (dst->sa.sa_family == AF_INET &&
1109 dst->sa.sa_len == sizeof(struct sockaddr_in) &&
1110 dst->sin.sin_port != 0);
1117 * XXX: to be checked internal IP header somewhere. Also when
1118 * IPsec tunnel packet is received. But ESP tunnel mode is
1119 * encrypted so we can't check internal IP header.
1122 if (V_key_preferred_oldsa) {
1123 saorder_state_valid = saorder_state_valid_prefer_old;
1124 arraysize = _ARRAYLEN(saorder_state_valid_prefer_old);
1126 saorder_state_valid = saorder_state_valid_prefer_new;
1127 arraysize = _ARRAYLEN(saorder_state_valid_prefer_new);
1129 LIST_FOREACH(sah, &V_sahtree, chain) {
1130 /* search valid state */
1131 for (stateidx = 0; stateidx < arraysize; stateidx++) {
1132 state = saorder_state_valid[stateidx];
1133 LIST_FOREACH(sav, &sah->savtree[state], chain) {
1135 KEY_CHKSASTATE(sav->state, state, __func__);
1136 /* do not return entries w/ unusable state */
1137 if (sav->state != SADB_SASTATE_MATURE &&
1138 sav->state != SADB_SASTATE_DYING)
1140 if (proto != sav->sah->saidx.proto)
1142 if (spi != sav->spi)
1144 #if 0 /* don't check src */
1145 /* check src address */
1146 if (key_sockaddrcmp(&src->sa, &sav->sah->saidx.src.sa, chkport) != 0)
1149 /* check dst address */
1150 if (key_sockaddrcmp(&dst->sa, &sav->sah->saidx.dst.sa, chkport) != 0)
1161 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1162 printf("DP %s return SA:%p; refcnt %u\n", __func__,
1163 sav, sav ? sav->refcnt : 0));
1168 * Must be called after calling key_allocsp().
1169 * For both the packet without socket and key_freeso().
1172 _key_freesp(struct secpolicy **spp, const char* where, int tag)
1174 struct secpolicy *sp = *spp;
1176 IPSEC_ASSERT(sp != NULL, ("null sp"));
1181 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1182 printf("DP %s SP:%p (ID=%u) from %s:%u; refcnt now %u\n",
1183 __func__, sp, sp->id, where, tag, sp->refcnt));
1185 if (sp->refcnt == 0) {
1193 * Must be called after calling key_allocsp().
1194 * For the packet with socket.
1197 key_freeso(struct socket *so)
1199 IPSEC_ASSERT(so != NULL, ("null so"));
1201 switch (so->so_proto->pr_domain->dom_family) {
1202 #if defined(INET) || defined(INET6)
1210 struct inpcb *pcb = sotoinpcb(so);
1212 /* Does it have a PCB ? */
1215 key_freesp_so(&pcb->inp_sp->sp_in);
1216 key_freesp_so(&pcb->inp_sp->sp_out);
1219 #endif /* INET || INET6 */
1221 ipseclog((LOG_DEBUG, "%s: unknown address family=%d.\n",
1222 __func__, so->so_proto->pr_domain->dom_family));
1228 key_freesp_so(struct secpolicy **sp)
1230 IPSEC_ASSERT(sp != NULL && *sp != NULL, ("null sp"));
1232 if ((*sp)->policy == IPSEC_POLICY_ENTRUST ||
1233 (*sp)->policy == IPSEC_POLICY_BYPASS)
1236 IPSEC_ASSERT((*sp)->policy == IPSEC_POLICY_IPSEC,
1237 ("invalid policy %u", (*sp)->policy));
1242 * Must be called after calling key_allocsa().
1243 * This function is called by key_freesp() to free some SA allocated
1247 key_freesav(struct secasvar **psav, const char* where, int tag)
1249 struct secasvar *sav = *psav;
1251 IPSEC_ASSERT(sav != NULL, ("null sav"));
1253 if (sa_delref(sav)) {
1254 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1255 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1256 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1260 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1261 printf("DP %s SA:%p (SPI %u) from %s:%u; refcnt now %u\n",
1262 __func__, sav, ntohl(sav->spi), where, tag, sav->refcnt));
1266 /* %%% SPD management */
1268 * free security policy entry.
1271 key_delsp(struct secpolicy *sp)
1273 struct ipsecrequest *isr, *nextisr;
1275 IPSEC_ASSERT(sp != NULL, ("null sp"));
1276 SPTREE_LOCK_ASSERT();
1278 sp->state = IPSEC_SPSTATE_DEAD;
1280 IPSEC_ASSERT(sp->refcnt == 0,
1281 ("SP with references deleted (refcnt %u)", sp->refcnt));
1283 /* remove from SP index */
1284 if (__LIST_CHAINED(sp))
1285 LIST_REMOVE(sp, chain);
1287 for (isr = sp->req; isr != NULL; isr = nextisr) {
1288 if (isr->sav != NULL) {
1289 KEY_FREESAV(&isr->sav);
1293 nextisr = isr->next;
1301 * OUT: NULL : not found
1302 * others : found, pointer to a SP.
1304 static struct secpolicy *
1305 key_getsp(struct secpolicyindex *spidx)
1307 struct secpolicy *sp;
1309 IPSEC_ASSERT(spidx != NULL, ("null spidx"));
1312 LIST_FOREACH(sp, &V_sptree[spidx->dir], chain) {
1313 if (sp->state == IPSEC_SPSTATE_DEAD)
1315 if (key_cmpspidx_exactly(spidx, &sp->spidx)) {
1327 * OUT: NULL : not found
1328 * others : found, pointer to a SP.
1330 static struct secpolicy *
1331 key_getspbyid(u_int32_t id)
1333 struct secpolicy *sp;
1336 LIST_FOREACH(sp, &V_sptree[IPSEC_DIR_INBOUND], chain) {
1337 if (sp->state == IPSEC_SPSTATE_DEAD)
1345 LIST_FOREACH(sp, &V_sptree[IPSEC_DIR_OUTBOUND], chain) {
1346 if (sp->state == IPSEC_SPSTATE_DEAD)
1360 key_newsp(const char* where, int tag)
1362 struct secpolicy *newsp = NULL;
1364 newsp = (struct secpolicy *)
1365 malloc(sizeof(struct secpolicy), M_IPSEC_SP, M_NOWAIT|M_ZERO);
1367 SECPOLICY_LOCK_INIT(newsp);
1372 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1373 printf("DP %s from %s:%u return SP:%p\n", __func__,
1374 where, tag, newsp));
1379 _key_delsp(struct secpolicy *sp)
1381 SECPOLICY_LOCK_DESTROY(sp);
1382 free(sp, M_IPSEC_SP);
1386 * create secpolicy structure from sadb_x_policy structure.
1387 * NOTE: `state', `secpolicyindex' in secpolicy structure are not set,
1388 * so must be set properly later.
1391 key_msg2sp(xpl0, len, error)
1392 struct sadb_x_policy *xpl0;
1396 struct secpolicy *newsp;
1398 IPSEC_ASSERT(xpl0 != NULL, ("null xpl0"));
1399 IPSEC_ASSERT(len >= sizeof(*xpl0), ("policy too short: %zu", len));
1401 if (len != PFKEY_EXTLEN(xpl0)) {
1402 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n", __func__));
1407 if ((newsp = KEY_NEWSP()) == NULL) {
1412 newsp->spidx.dir = xpl0->sadb_x_policy_dir;
1413 newsp->policy = xpl0->sadb_x_policy_type;
1416 switch (xpl0->sadb_x_policy_type) {
1417 case IPSEC_POLICY_DISCARD:
1418 case IPSEC_POLICY_NONE:
1419 case IPSEC_POLICY_ENTRUST:
1420 case IPSEC_POLICY_BYPASS:
1424 case IPSEC_POLICY_IPSEC:
1427 struct sadb_x_ipsecrequest *xisr;
1428 struct ipsecrequest **p_isr = &newsp->req;
1430 /* validity check */
1431 if (PFKEY_EXTLEN(xpl0) < sizeof(*xpl0)) {
1432 ipseclog((LOG_DEBUG, "%s: Invalid msg length.\n",
1439 tlen = PFKEY_EXTLEN(xpl0) - sizeof(*xpl0);
1440 xisr = (struct sadb_x_ipsecrequest *)(xpl0 + 1);
1444 if (xisr->sadb_x_ipsecrequest_len < sizeof(*xisr)) {
1445 ipseclog((LOG_DEBUG, "%s: invalid ipsecrequest "
1446 "length.\n", __func__));
1452 /* allocate request buffer */
1453 /* NB: data structure is zero'd */
1454 *p_isr = ipsec_newisr();
1455 if ((*p_isr) == NULL) {
1456 ipseclog((LOG_DEBUG,
1457 "%s: No more memory.\n", __func__));
1464 switch (xisr->sadb_x_ipsecrequest_proto) {
1467 case IPPROTO_IPCOMP:
1470 ipseclog((LOG_DEBUG,
1471 "%s: invalid proto type=%u\n", __func__,
1472 xisr->sadb_x_ipsecrequest_proto));
1474 *error = EPROTONOSUPPORT;
1477 (*p_isr)->saidx.proto = xisr->sadb_x_ipsecrequest_proto;
1479 switch (xisr->sadb_x_ipsecrequest_mode) {
1480 case IPSEC_MODE_TRANSPORT:
1481 case IPSEC_MODE_TUNNEL:
1483 case IPSEC_MODE_ANY:
1485 ipseclog((LOG_DEBUG,
1486 "%s: invalid mode=%u\n", __func__,
1487 xisr->sadb_x_ipsecrequest_mode));
1492 (*p_isr)->saidx.mode = xisr->sadb_x_ipsecrequest_mode;
1494 switch (xisr->sadb_x_ipsecrequest_level) {
1495 case IPSEC_LEVEL_DEFAULT:
1496 case IPSEC_LEVEL_USE:
1497 case IPSEC_LEVEL_REQUIRE:
1499 case IPSEC_LEVEL_UNIQUE:
1500 /* validity check */
1502 * If range violation of reqid, kernel will
1503 * update it, don't refuse it.
1505 if (xisr->sadb_x_ipsecrequest_reqid
1506 > IPSEC_MANUAL_REQID_MAX) {
1507 ipseclog((LOG_DEBUG,
1508 "%s: reqid=%d range "
1509 "violation, updated by kernel.\n",
1511 xisr->sadb_x_ipsecrequest_reqid));
1512 xisr->sadb_x_ipsecrequest_reqid = 0;
1515 /* allocate new reqid id if reqid is zero. */
1516 if (xisr->sadb_x_ipsecrequest_reqid == 0) {
1518 if ((reqid = key_newreqid()) == 0) {
1523 (*p_isr)->saidx.reqid = reqid;
1524 xisr->sadb_x_ipsecrequest_reqid = reqid;
1526 /* set it for manual keying. */
1527 (*p_isr)->saidx.reqid =
1528 xisr->sadb_x_ipsecrequest_reqid;
1533 ipseclog((LOG_DEBUG, "%s: invalid level=%u\n",
1535 xisr->sadb_x_ipsecrequest_level));
1540 (*p_isr)->level = xisr->sadb_x_ipsecrequest_level;
1542 /* set IP addresses if there */
1543 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
1544 struct sockaddr *paddr;
1546 paddr = (struct sockaddr *)(xisr + 1);
1548 /* validity check */
1550 > sizeof((*p_isr)->saidx.src)) {
1551 ipseclog((LOG_DEBUG, "%s: invalid "
1552 "request address length.\n",
1558 bcopy(paddr, &(*p_isr)->saidx.src,
1561 paddr = (struct sockaddr *)((caddr_t)paddr
1564 /* validity check */
1566 > sizeof((*p_isr)->saidx.dst)) {
1567 ipseclog((LOG_DEBUG, "%s: invalid "
1568 "request address length.\n",
1574 bcopy(paddr, &(*p_isr)->saidx.dst,
1578 (*p_isr)->sp = newsp;
1580 /* initialization for the next. */
1581 p_isr = &(*p_isr)->next;
1582 tlen -= xisr->sadb_x_ipsecrequest_len;
1584 /* validity check */
1586 ipseclog((LOG_DEBUG, "%s: becoming tlen < 0.\n",
1593 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
1594 + xisr->sadb_x_ipsecrequest_len);
1599 ipseclog((LOG_DEBUG, "%s: invalid policy type.\n", __func__));
1612 static u_int32_t auto_reqid = IPSEC_MANUAL_REQID_MAX + 1;
1614 auto_reqid = (auto_reqid == ~0
1615 ? IPSEC_MANUAL_REQID_MAX + 1 : auto_reqid + 1);
1617 /* XXX should be unique check */
1623 * copy secpolicy struct to sadb_x_policy structure indicated.
1627 struct secpolicy *sp;
1629 struct sadb_x_policy *xpl;
1634 IPSEC_ASSERT(sp != NULL, ("null policy"));
1636 tlen = key_getspreqmsglen(sp);
1638 m = key_alloc_mbuf(tlen);
1639 if (!m || m->m_next) { /*XXX*/
1647 xpl = mtod(m, struct sadb_x_policy *);
1650 xpl->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1651 xpl->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1652 xpl->sadb_x_policy_type = sp->policy;
1653 xpl->sadb_x_policy_dir = sp->spidx.dir;
1654 xpl->sadb_x_policy_id = sp->id;
1655 p = (caddr_t)xpl + sizeof(*xpl);
1657 /* if is the policy for ipsec ? */
1658 if (sp->policy == IPSEC_POLICY_IPSEC) {
1659 struct sadb_x_ipsecrequest *xisr;
1660 struct ipsecrequest *isr;
1662 for (isr = sp->req; isr != NULL; isr = isr->next) {
1664 xisr = (struct sadb_x_ipsecrequest *)p;
1666 xisr->sadb_x_ipsecrequest_proto = isr->saidx.proto;
1667 xisr->sadb_x_ipsecrequest_mode = isr->saidx.mode;
1668 xisr->sadb_x_ipsecrequest_level = isr->level;
1669 xisr->sadb_x_ipsecrequest_reqid = isr->saidx.reqid;
1672 bcopy(&isr->saidx.src, p, isr->saidx.src.sa.sa_len);
1673 p += isr->saidx.src.sa.sa_len;
1674 bcopy(&isr->saidx.dst, p, isr->saidx.dst.sa.sa_len);
1675 p += isr->saidx.src.sa.sa_len;
1677 xisr->sadb_x_ipsecrequest_len =
1678 PFKEY_ALIGN8(sizeof(*xisr)
1679 + isr->saidx.src.sa.sa_len
1680 + isr->saidx.dst.sa.sa_len);
1687 /* m will not be freed nor modified */
1688 static struct mbuf *
1690 key_gather_mbuf(struct mbuf *m, const struct sadb_msghdr *mhp,
1691 int ndeep, int nitem, ...)
1693 key_gather_mbuf(m, mhp, ndeep, nitem, va_alist)
1695 const struct sadb_msghdr *mhp;
1704 struct mbuf *result = NULL, *n;
1707 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1708 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1710 va_start(ap, nitem);
1711 for (i = 0; i < nitem; i++) {
1712 idx = va_arg(ap, int);
1713 if (idx < 0 || idx > SADB_EXT_MAX)
1715 /* don't attempt to pull empty extension */
1716 if (idx == SADB_EXT_RESERVED && mhp->msg == NULL)
1718 if (idx != SADB_EXT_RESERVED &&
1719 (mhp->ext[idx] == NULL || mhp->extlen[idx] == 0))
1722 if (idx == SADB_EXT_RESERVED) {
1723 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
1725 IPSEC_ASSERT(len <= MHLEN, ("header too big %u", len));
1727 MGETHDR(n, M_DONTWAIT, MT_DATA);
1732 m_copydata(m, 0, sizeof(struct sadb_msg),
1734 } else if (i < ndeep) {
1735 len = mhp->extlen[idx];
1736 n = key_alloc_mbuf(len);
1737 if (!n || n->m_next) { /*XXX*/
1742 m_copydata(m, mhp->extoff[idx], mhp->extlen[idx],
1745 n = m_copym(m, mhp->extoff[idx], mhp->extlen[idx],
1758 if ((result->m_flags & M_PKTHDR) != 0) {
1759 result->m_pkthdr.len = 0;
1760 for (n = result; n; n = n->m_next)
1761 result->m_pkthdr.len += n->m_len;
1772 * SADB_X_SPDADD, SADB_X_SPDSETIDX or SADB_X_SPDUPDATE processing
1773 * add an entry to SP database, when received
1774 * <base, address(SD), (lifetime(H),) policy>
1776 * Adding to SP database,
1778 * <base, address(SD), (lifetime(H),) policy>
1779 * to the socket which was send.
1781 * SPDADD set a unique policy entry.
1782 * SPDSETIDX like SPDADD without a part of policy requests.
1783 * SPDUPDATE replace a unique policy entry.
1785 * m will always be freed.
1788 key_spdadd(so, m, mhp)
1791 const struct sadb_msghdr *mhp;
1793 struct sadb_address *src0, *dst0;
1794 struct sadb_x_policy *xpl0, *xpl;
1795 struct sadb_lifetime *lft = NULL;
1796 struct secpolicyindex spidx;
1797 struct secpolicy *newsp;
1800 IPSEC_ASSERT(so != NULL, ("null socket"));
1801 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1802 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
1803 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
1805 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
1806 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
1807 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
1808 ipseclog((LOG_DEBUG, "key_spdadd: invalid message is passed.\n"));
1809 return key_senderror(so, m, EINVAL);
1811 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
1812 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
1813 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
1814 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1816 return key_senderror(so, m, EINVAL);
1818 if (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL) {
1819 if (mhp->extlen[SADB_EXT_LIFETIME_HARD]
1820 < sizeof(struct sadb_lifetime)) {
1821 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
1823 return key_senderror(so, m, EINVAL);
1825 lft = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
1828 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
1829 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
1830 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
1833 * Note: do not parse SADB_X_EXT_NAT_T_* here:
1834 * we are processing traffic endpoints.
1838 /* XXX boundary check against sa_len */
1839 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1842 src0->sadb_address_prefixlen,
1843 dst0->sadb_address_prefixlen,
1844 src0->sadb_address_proto,
1847 /* checking the direciton. */
1848 switch (xpl0->sadb_x_policy_dir) {
1849 case IPSEC_DIR_INBOUND:
1850 case IPSEC_DIR_OUTBOUND:
1853 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
1854 mhp->msg->sadb_msg_errno = EINVAL;
1859 /* key_spdadd() accepts DISCARD, NONE and IPSEC. */
1860 if (xpl0->sadb_x_policy_type == IPSEC_POLICY_ENTRUST
1861 || xpl0->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1862 ipseclog((LOG_DEBUG, "%s: Invalid policy type.\n", __func__));
1863 return key_senderror(so, m, EINVAL);
1866 /* policy requests are mandatory when action is ipsec. */
1867 if (mhp->msg->sadb_msg_type != SADB_X_SPDSETIDX
1868 && xpl0->sadb_x_policy_type == IPSEC_POLICY_IPSEC
1869 && mhp->extlen[SADB_X_EXT_POLICY] <= sizeof(*xpl0)) {
1870 ipseclog((LOG_DEBUG, "%s: some policy requests part required\n",
1872 return key_senderror(so, m, EINVAL);
1876 * checking there is SP already or not.
1877 * SPDUPDATE doesn't depend on whether there is a SP or not.
1878 * If the type is either SPDADD or SPDSETIDX AND a SP is found,
1881 newsp = key_getsp(&spidx);
1882 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1884 newsp->state = IPSEC_SPSTATE_DEAD;
1888 if (newsp != NULL) {
1890 ipseclog((LOG_DEBUG, "%s: a SP entry exists already.\n",
1892 return key_senderror(so, m, EEXIST);
1896 /* allocation new SP entry */
1897 if ((newsp = key_msg2sp(xpl0, PFKEY_EXTLEN(xpl0), &error)) == NULL) {
1898 return key_senderror(so, m, error);
1901 if ((newsp->id = key_getnewspid()) == 0) {
1903 return key_senderror(so, m, ENOBUFS);
1906 /* XXX boundary check against sa_len */
1907 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
1910 src0->sadb_address_prefixlen,
1911 dst0->sadb_address_prefixlen,
1912 src0->sadb_address_proto,
1915 /* sanity check on addr pair */
1916 if (((struct sockaddr *)(src0 + 1))->sa_family !=
1917 ((struct sockaddr *)(dst0+ 1))->sa_family) {
1919 return key_senderror(so, m, EINVAL);
1921 if (((struct sockaddr *)(src0 + 1))->sa_len !=
1922 ((struct sockaddr *)(dst0+ 1))->sa_len) {
1924 return key_senderror(so, m, EINVAL);
1927 if (newsp->req && newsp->req->saidx.src.sa.sa_family) {
1928 struct sockaddr *sa;
1929 sa = (struct sockaddr *)(src0 + 1);
1930 if (sa->sa_family != newsp->req->saidx.src.sa.sa_family) {
1932 return key_senderror(so, m, EINVAL);
1935 if (newsp->req && newsp->req->saidx.dst.sa.sa_family) {
1936 struct sockaddr *sa;
1937 sa = (struct sockaddr *)(dst0 + 1);
1938 if (sa->sa_family != newsp->req->saidx.dst.sa.sa_family) {
1940 return key_senderror(so, m, EINVAL);
1945 newsp->created = time_second;
1946 newsp->lastused = newsp->created;
1947 newsp->lifetime = lft ? lft->sadb_lifetime_addtime : 0;
1948 newsp->validtime = lft ? lft->sadb_lifetime_usetime : 0;
1950 newsp->refcnt = 1; /* do not reclaim until I say I do */
1951 newsp->state = IPSEC_SPSTATE_ALIVE;
1952 LIST_INSERT_TAIL(&V_sptree[newsp->spidx.dir], newsp, secpolicy, chain);
1954 /* delete the entry in spacqtree */
1955 if (mhp->msg->sadb_msg_type == SADB_X_SPDUPDATE) {
1956 struct secspacq *spacq = key_getspacq(&spidx);
1957 if (spacq != NULL) {
1958 /* reset counter in order to deletion by timehandler. */
1959 spacq->created = time_second;
1966 struct mbuf *n, *mpolicy;
1967 struct sadb_msg *newmsg;
1971 * Note: do not send SADB_X_EXT_NAT_T_* here:
1972 * we are sending traffic endpoints.
1975 /* create new sadb_msg to reply. */
1977 n = key_gather_mbuf(m, mhp, 2, 5, SADB_EXT_RESERVED,
1978 SADB_X_EXT_POLICY, SADB_EXT_LIFETIME_HARD,
1979 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1981 n = key_gather_mbuf(m, mhp, 2, 4, SADB_EXT_RESERVED,
1983 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
1986 return key_senderror(so, m, ENOBUFS);
1988 if (n->m_len < sizeof(*newmsg)) {
1989 n = m_pullup(n, sizeof(*newmsg));
1991 return key_senderror(so, m, ENOBUFS);
1993 newmsg = mtod(n, struct sadb_msg *);
1994 newmsg->sadb_msg_errno = 0;
1995 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
1998 mpolicy = m_pulldown(n, PFKEY_ALIGN8(sizeof(struct sadb_msg)),
1999 sizeof(*xpl), &off);
2000 if (mpolicy == NULL) {
2001 /* n is already freed */
2002 return key_senderror(so, m, ENOBUFS);
2004 xpl = (struct sadb_x_policy *)(mtod(mpolicy, caddr_t) + off);
2005 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
2007 return key_senderror(so, m, EINVAL);
2009 xpl->sadb_x_policy_id = newsp->id;
2012 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2017 * get new policy id.
2025 u_int32_t newid = 0;
2026 int count = V_key_spi_trycnt; /* XXX */
2027 struct secpolicy *sp;
2029 /* when requesting to allocate spi ranged */
2031 newid = (V_policy_id = (V_policy_id == ~0 ? 1 : V_policy_id + 1));
2033 if ((sp = key_getspbyid(newid)) == NULL)
2039 if (count == 0 || newid == 0) {
2040 ipseclog((LOG_DEBUG, "%s: to allocate policy id is failed.\n",
2049 * SADB_SPDDELETE processing
2051 * <base, address(SD), policy(*)>
2052 * from the user(?), and set SADB_SASTATE_DEAD,
2054 * <base, address(SD), policy(*)>
2056 * policy(*) including direction of policy.
2058 * m will always be freed.
2061 key_spddelete(so, m, mhp)
2064 const struct sadb_msghdr *mhp;
2066 struct sadb_address *src0, *dst0;
2067 struct sadb_x_policy *xpl0;
2068 struct secpolicyindex spidx;
2069 struct secpolicy *sp;
2071 IPSEC_ASSERT(so != NULL, ("null so"));
2072 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2073 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2074 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2076 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
2077 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
2078 mhp->ext[SADB_X_EXT_POLICY] == NULL) {
2079 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2081 return key_senderror(so, m, EINVAL);
2083 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
2084 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
2085 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2086 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2088 return key_senderror(so, m, EINVAL);
2091 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
2092 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
2093 xpl0 = (struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY];
2096 * Note: do not parse SADB_X_EXT_NAT_T_* here:
2097 * we are processing traffic endpoints.
2101 /* XXX boundary check against sa_len */
2102 KEY_SETSECSPIDX(xpl0->sadb_x_policy_dir,
2105 src0->sadb_address_prefixlen,
2106 dst0->sadb_address_prefixlen,
2107 src0->sadb_address_proto,
2110 /* checking the direciton. */
2111 switch (xpl0->sadb_x_policy_dir) {
2112 case IPSEC_DIR_INBOUND:
2113 case IPSEC_DIR_OUTBOUND:
2116 ipseclog((LOG_DEBUG, "%s: Invalid SP direction.\n", __func__));
2117 return key_senderror(so, m, EINVAL);
2120 /* Is there SP in SPD ? */
2121 if ((sp = key_getsp(&spidx)) == NULL) {
2122 ipseclog((LOG_DEBUG, "%s: no SP found.\n", __func__));
2123 return key_senderror(so, m, EINVAL);
2126 /* save policy id to buffer to be returned. */
2127 xpl0->sadb_x_policy_id = sp->id;
2129 sp->state = IPSEC_SPSTATE_DEAD;
2134 struct sadb_msg *newmsg;
2137 * Note: do not send SADB_X_EXT_NAT_T_* here:
2138 * we are sending traffic endpoints.
2141 /* create new sadb_msg to reply. */
2142 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
2143 SADB_X_EXT_POLICY, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
2145 return key_senderror(so, m, ENOBUFS);
2147 newmsg = mtod(n, struct sadb_msg *);
2148 newmsg->sadb_msg_errno = 0;
2149 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2152 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2157 * SADB_SPDDELETE2 processing
2160 * from the user(?), and set SADB_SASTATE_DEAD,
2164 * policy(*) including direction of policy.
2166 * m will always be freed.
2169 key_spddelete2(so, m, mhp)
2172 const struct sadb_msghdr *mhp;
2175 struct secpolicy *sp;
2177 IPSEC_ASSERT(so != NULL, ("null socket"));
2178 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2179 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2180 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2182 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2183 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2184 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n", __func__));
2185 return key_senderror(so, m, EINVAL);
2188 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2190 /* Is there SP in SPD ? */
2191 if ((sp = key_getspbyid(id)) == NULL) {
2192 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2193 return key_senderror(so, m, EINVAL);
2196 sp->state = IPSEC_SPSTATE_DEAD;
2200 struct mbuf *n, *nn;
2201 struct sadb_msg *newmsg;
2204 /* create new sadb_msg to reply. */
2205 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2207 MGETHDR(n, M_DONTWAIT, MT_DATA);
2208 if (n && len > MHLEN) {
2209 MCLGET(n, M_DONTWAIT);
2210 if ((n->m_flags & M_EXT) == 0) {
2216 return key_senderror(so, m, ENOBUFS);
2222 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
2223 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
2225 IPSEC_ASSERT(off == len, ("length inconsistency (off %u len %u)",
2228 n->m_next = m_copym(m, mhp->extoff[SADB_X_EXT_POLICY],
2229 mhp->extlen[SADB_X_EXT_POLICY], M_DONTWAIT);
2232 return key_senderror(so, m, ENOBUFS);
2235 n->m_pkthdr.len = 0;
2236 for (nn = n; nn; nn = nn->m_next)
2237 n->m_pkthdr.len += nn->m_len;
2239 newmsg = mtod(n, struct sadb_msg *);
2240 newmsg->sadb_msg_errno = 0;
2241 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
2244 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
2249 * SADB_X_GET processing
2254 * <base, address(SD), policy>
2256 * policy(*) including direction of policy.
2258 * m will always be freed.
2261 key_spdget(so, m, mhp)
2264 const struct sadb_msghdr *mhp;
2267 struct secpolicy *sp;
2270 IPSEC_ASSERT(so != NULL, ("null socket"));
2271 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2272 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2273 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2275 if (mhp->ext[SADB_X_EXT_POLICY] == NULL ||
2276 mhp->extlen[SADB_X_EXT_POLICY] < sizeof(struct sadb_x_policy)) {
2277 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2279 return key_senderror(so, m, EINVAL);
2282 id = ((struct sadb_x_policy *)mhp->ext[SADB_X_EXT_POLICY])->sadb_x_policy_id;
2284 /* Is there SP in SPD ? */
2285 if ((sp = key_getspbyid(id)) == NULL) {
2286 ipseclog((LOG_DEBUG, "%s: no SP found id:%u.\n", __func__, id));
2287 return key_senderror(so, m, ENOENT);
2290 n = key_setdumpsp(sp, SADB_X_SPDGET, 0, mhp->msg->sadb_msg_pid);
2293 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2295 return key_senderror(so, m, ENOBUFS);
2299 * SADB_X_SPDACQUIRE processing.
2300 * Acquire policy and SA(s) for a *OUTBOUND* packet.
2303 * to KMD, and expect to receive
2304 * <base> with SADB_X_SPDACQUIRE if error occured,
2307 * with SADB_X_SPDUPDATE from KMD by PF_KEY.
2308 * policy(*) is without policy requests.
2311 * others: error number
2315 struct secpolicy *sp;
2317 struct mbuf *result = NULL, *m;
2318 struct secspacq *newspacq;
2320 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2321 IPSEC_ASSERT(sp->req == NULL, ("policy exists"));
2322 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
2323 ("policy not IPSEC %u", sp->policy));
2325 /* Get an entry to check whether sent message or not. */
2326 newspacq = key_getspacq(&sp->spidx);
2327 if (newspacq != NULL) {
2328 if (V_key_blockacq_count < newspacq->count) {
2329 /* reset counter and do send message. */
2330 newspacq->count = 0;
2332 /* increment counter and do nothing. */
2338 /* make new entry for blocking to send SADB_ACQUIRE. */
2339 newspacq = key_newspacq(&sp->spidx);
2340 if (newspacq == NULL)
2344 /* create new sadb_msg to reply. */
2345 m = key_setsadbmsg(SADB_X_SPDACQUIRE, 0, 0, 0, 0, 0);
2351 result->m_pkthdr.len = 0;
2352 for (m = result; m; m = m->m_next)
2353 result->m_pkthdr.len += m->m_len;
2355 mtod(result, struct sadb_msg *)->sadb_msg_len =
2356 PFKEY_UNIT64(result->m_pkthdr.len);
2358 return key_sendup_mbuf(NULL, m, KEY_SENDUP_REGISTERED);
2362 * SADB_SPDFLUSH processing
2365 * from the user, and free all entries in secpctree.
2369 * NOTE: what to do is only marking SADB_SASTATE_DEAD.
2371 * m will always be freed.
2374 key_spdflush(so, m, mhp)
2377 const struct sadb_msghdr *mhp;
2379 struct sadb_msg *newmsg;
2380 struct secpolicy *sp;
2383 IPSEC_ASSERT(so != NULL, ("null socket"));
2384 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2385 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2386 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2388 if (m->m_len != PFKEY_ALIGN8(sizeof(struct sadb_msg)))
2389 return key_senderror(so, m, EINVAL);
2391 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2393 LIST_FOREACH(sp, &V_sptree[dir], chain)
2394 sp->state = IPSEC_SPSTATE_DEAD;
2398 if (sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
2399 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2400 return key_senderror(so, m, ENOBUFS);
2406 m->m_pkthdr.len = m->m_len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
2407 newmsg = mtod(m, struct sadb_msg *);
2408 newmsg->sadb_msg_errno = 0;
2409 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
2411 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
2415 * SADB_SPDDUMP processing
2418 * from the user, and dump all SP leaves
2423 * m will always be freed.
2426 key_spddump(so, m, mhp)
2429 const struct sadb_msghdr *mhp;
2431 struct secpolicy *sp;
2436 IPSEC_ASSERT(so != NULL, ("null socket"));
2437 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2438 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2439 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2441 /* search SPD entry and get buffer size. */
2444 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2445 LIST_FOREACH(sp, &V_sptree[dir], chain) {
2452 return key_senderror(so, m, ENOENT);
2455 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
2456 LIST_FOREACH(sp, &V_sptree[dir], chain) {
2458 n = key_setdumpsp(sp, SADB_X_SPDDUMP, cnt,
2459 mhp->msg->sadb_msg_pid);
2462 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
2471 static struct mbuf *
2472 key_setdumpsp(struct secpolicy *sp, u_int8_t type, u_int32_t seq, u_int32_t pid)
2474 struct mbuf *result = NULL, *m;
2475 struct seclifetime lt;
2477 m = key_setsadbmsg(type, 0, SADB_SATYPE_UNSPEC, seq, pid, sp->refcnt);
2483 * Note: do not send SADB_X_EXT_NAT_T_* here:
2484 * we are sending traffic endpoints.
2486 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2487 &sp->spidx.src.sa, sp->spidx.prefs,
2488 sp->spidx.ul_proto);
2493 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2494 &sp->spidx.dst.sa, sp->spidx.prefd,
2495 sp->spidx.ul_proto);
2506 lt.addtime=sp->created;
2507 lt.usetime= sp->lastused;
2508 m = key_setlifetime(<, SADB_EXT_LIFETIME_CURRENT);
2513 lt.addtime=sp->lifetime;
2514 lt.usetime= sp->validtime;
2515 m = key_setlifetime(<, SADB_EXT_LIFETIME_HARD);
2521 if ((result->m_flags & M_PKTHDR) == 0)
2524 if (result->m_len < sizeof(struct sadb_msg)) {
2525 result = m_pullup(result, sizeof(struct sadb_msg));
2530 result->m_pkthdr.len = 0;
2531 for (m = result; m; m = m->m_next)
2532 result->m_pkthdr.len += m->m_len;
2534 mtod(result, struct sadb_msg *)->sadb_msg_len =
2535 PFKEY_UNIT64(result->m_pkthdr.len);
2545 * get PFKEY message length for security policy and request.
2548 key_getspreqmsglen(sp)
2549 struct secpolicy *sp;
2553 tlen = sizeof(struct sadb_x_policy);
2555 /* if is the policy for ipsec ? */
2556 if (sp->policy != IPSEC_POLICY_IPSEC)
2559 /* get length of ipsec requests */
2561 struct ipsecrequest *isr;
2564 for (isr = sp->req; isr != NULL; isr = isr->next) {
2565 len = sizeof(struct sadb_x_ipsecrequest)
2566 + isr->saidx.src.sa.sa_len
2567 + isr->saidx.dst.sa.sa_len;
2569 tlen += PFKEY_ALIGN8(len);
2577 * SADB_SPDEXPIRE processing
2579 * <base, address(SD), lifetime(CH), policy>
2583 * others : error number
2587 struct secpolicy *sp;
2589 struct mbuf *result = NULL, *m;
2592 struct sadb_lifetime *lt;
2594 /* XXX: Why do we lock ? */
2596 IPSEC_ASSERT(sp != NULL, ("null secpolicy"));
2598 /* set msg header */
2599 m = key_setsadbmsg(SADB_X_SPDEXPIRE, 0, 0, 0, 0, 0);
2606 /* create lifetime extension (current and hard) */
2607 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
2608 m = key_alloc_mbuf(len);
2609 if (!m || m->m_next) { /*XXX*/
2615 bzero(mtod(m, caddr_t), len);
2616 lt = mtod(m, struct sadb_lifetime *);
2617 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2618 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2619 lt->sadb_lifetime_allocations = 0;
2620 lt->sadb_lifetime_bytes = 0;
2621 lt->sadb_lifetime_addtime = sp->created;
2622 lt->sadb_lifetime_usetime = sp->lastused;
2623 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
2624 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
2625 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2626 lt->sadb_lifetime_allocations = 0;
2627 lt->sadb_lifetime_bytes = 0;
2628 lt->sadb_lifetime_addtime = sp->lifetime;
2629 lt->sadb_lifetime_usetime = sp->validtime;
2633 * Note: do not send SADB_X_EXT_NAT_T_* here:
2634 * we are sending traffic endpoints.
2637 /* set sadb_address for source */
2638 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
2640 sp->spidx.prefs, sp->spidx.ul_proto);
2647 /* set sadb_address for destination */
2648 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
2650 sp->spidx.prefd, sp->spidx.ul_proto);
2665 if ((result->m_flags & M_PKTHDR) == 0) {
2670 if (result->m_len < sizeof(struct sadb_msg)) {
2671 result = m_pullup(result, sizeof(struct sadb_msg));
2672 if (result == NULL) {
2678 result->m_pkthdr.len = 0;
2679 for (m = result; m; m = m->m_next)
2680 result->m_pkthdr.len += m->m_len;
2682 mtod(result, struct sadb_msg *)->sadb_msg_len =
2683 PFKEY_UNIT64(result->m_pkthdr.len);
2685 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
2693 /* %%% SAD management */
2695 * allocating a memory for new SA head, and copy from the values of mhp.
2696 * OUT: NULL : failure due to the lack of memory.
2697 * others : pointer to new SA head.
2699 static struct secashead *
2701 struct secasindex *saidx;
2703 struct secashead *newsah;
2705 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
2707 newsah = malloc(sizeof(struct secashead), M_IPSEC_SAH, M_NOWAIT|M_ZERO);
2708 if (newsah != NULL) {
2710 for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++)
2711 LIST_INIT(&newsah->savtree[i]);
2712 newsah->saidx = *saidx;
2714 /* add to saidxtree */
2715 newsah->state = SADB_SASTATE_MATURE;
2718 LIST_INSERT_HEAD(&V_sahtree, newsah, chain);
2725 * delete SA index and all SA registerd.
2729 struct secashead *sah;
2731 struct secasvar *sav, *nextsav;
2735 IPSEC_ASSERT(sah != NULL, ("NULL sah"));
2736 SAHTREE_LOCK_ASSERT();
2738 /* searching all SA registerd in the secindex. */
2740 stateidx < _ARRAYLEN(saorder_state_any);
2742 u_int state = saorder_state_any[stateidx];
2743 LIST_FOREACH_SAFE(sav, &sah->savtree[state], chain, nextsav) {
2744 if (sav->refcnt == 0) {
2746 KEY_CHKSASTATE(state, sav->state, __func__);
2748 * do NOT call KEY_FREESAV here:
2749 * it will only delete the sav if refcnt == 1,
2750 * where we already know that refcnt == 0
2754 /* give up to delete this sa */
2759 if (!zombie) { /* delete only if there are savs */
2760 /* remove from tree of SA index */
2761 if (__LIST_CHAINED(sah))
2762 LIST_REMOVE(sah, chain);
2763 if (sah->sa_route.ro_rt) {
2764 RTFREE(sah->sa_route.ro_rt);
2765 sah->sa_route.ro_rt = (struct rtentry *)NULL;
2767 free(sah, M_IPSEC_SAH);
2772 * allocating a new SA with LARVAL state. key_add() and key_getspi() call,
2773 * and copy the values of mhp into new buffer.
2774 * When SAD message type is GETSPI:
2775 * to set sequence number from acq_seq++,
2776 * to set zero to SPI.
2777 * not to call key_setsava().
2779 * others : pointer to new secasvar.
2781 * does not modify mbuf. does not free mbuf on error.
2783 static struct secasvar *
2784 key_newsav(m, mhp, sah, errp, where, tag)
2786 const struct sadb_msghdr *mhp;
2787 struct secashead *sah;
2792 struct secasvar *newsav;
2793 const struct sadb_sa *xsa;
2795 IPSEC_ASSERT(m != NULL, ("null mbuf"));
2796 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
2797 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
2798 IPSEC_ASSERT(sah != NULL, ("null secashead"));
2800 newsav = malloc(sizeof(struct secasvar), M_IPSEC_SA, M_NOWAIT|M_ZERO);
2801 if (newsav == NULL) {
2802 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
2807 switch (mhp->msg->sadb_msg_type) {
2811 #ifdef IPSEC_DOSEQCHECK
2812 /* sync sequence number */
2813 if (mhp->msg->sadb_msg_seq == 0)
2815 (V_acq_seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq));
2818 newsav->seq = mhp->msg->sadb_msg_seq;
2823 if (mhp->ext[SADB_EXT_SA] == NULL) {
2824 free(newsav, M_IPSEC_SA);
2826 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
2831 xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
2832 newsav->spi = xsa->sadb_sa_spi;
2833 newsav->seq = mhp->msg->sadb_msg_seq;
2836 free(newsav, M_IPSEC_SA);
2843 /* copy sav values */
2844 if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
2845 *errp = key_setsaval(newsav, m, mhp);
2847 free(newsav, M_IPSEC_SA);
2853 SECASVAR_LOCK_INIT(newsav);
2856 newsav->created = time_second;
2857 newsav->pid = mhp->msg->sadb_msg_pid;
2862 newsav->state = SADB_SASTATE_LARVAL;
2864 /* XXX locking??? */
2865 LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
2868 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
2869 printf("DP %s from %s:%u return SP:%p\n", __func__,
2870 where, tag, newsav));
2876 * free() SA variable entry.
2879 key_cleansav(struct secasvar *sav)
2882 * Cleanup xform state. Note that zeroize'ing causes the
2883 * keys to be cleared; otherwise we must do it ourself.
2885 if (sav->tdb_xform != NULL) {
2886 sav->tdb_xform->xf_zeroize(sav);
2887 sav->tdb_xform = NULL;
2889 KASSERT(sav->iv == NULL, ("iv but no xform"));
2890 if (sav->key_auth != NULL)
2891 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
2892 if (sav->key_enc != NULL)
2893 bzero(sav->key_enc->key_data, _KEYLEN(sav->key_enc));
2895 if (sav->key_auth != NULL) {
2896 if (sav->key_auth->key_data != NULL)
2897 free(sav->key_auth->key_data, M_IPSEC_MISC);
2898 free(sav->key_auth, M_IPSEC_MISC);
2899 sav->key_auth = NULL;
2901 if (sav->key_enc != NULL) {
2902 if (sav->key_enc->key_data != NULL)
2903 free(sav->key_enc->key_data, M_IPSEC_MISC);
2904 free(sav->key_enc, M_IPSEC_MISC);
2905 sav->key_enc = NULL;
2908 bzero(sav->sched, sav->schedlen);
2909 free(sav->sched, M_IPSEC_MISC);
2912 if (sav->replay != NULL) {
2913 free(sav->replay, M_IPSEC_MISC);
2916 if (sav->lft_c != NULL) {
2917 free(sav->lft_c, M_IPSEC_MISC);
2920 if (sav->lft_h != NULL) {
2921 free(sav->lft_h, M_IPSEC_MISC);
2924 if (sav->lft_s != NULL) {
2925 free(sav->lft_s, M_IPSEC_MISC);
2931 * free() SA variable entry.
2935 struct secasvar *sav;
2937 IPSEC_ASSERT(sav != NULL, ("null sav"));
2938 IPSEC_ASSERT(sav->refcnt == 0, ("reference count %u > 0", sav->refcnt));
2940 /* remove from SA header */
2941 if (__LIST_CHAINED(sav))
2942 LIST_REMOVE(sav, chain);
2944 SECASVAR_LOCK_DESTROY(sav);
2945 free(sav, M_IPSEC_SA);
2952 * others : found, pointer to a SA.
2954 static struct secashead *
2956 struct secasindex *saidx;
2958 struct secashead *sah;
2961 LIST_FOREACH(sah, &V_sahtree, chain) {
2962 if (sah->state == SADB_SASTATE_DEAD)
2964 if (key_cmpsaidx(&sah->saidx, saidx, CMP_REQID))
2973 * check not to be duplicated SPI.
2974 * NOTE: this function is too slow due to searching all SAD.
2977 * others : found, pointer to a SA.
2979 static struct secasvar *
2980 key_checkspidup(saidx, spi)
2981 struct secasindex *saidx;
2984 struct secashead *sah;
2985 struct secasvar *sav;
2987 /* check address family */
2988 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
2989 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
2997 LIST_FOREACH(sah, &V_sahtree, chain) {
2998 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
3000 sav = key_getsavbyspi(sah, spi);
3010 * search SAD litmited alive SA, protocol, SPI.
3013 * others : found, pointer to a SA.
3015 static struct secasvar *
3016 key_getsavbyspi(sah, spi)
3017 struct secashead *sah;
3020 struct secasvar *sav;
3021 u_int stateidx, state;
3024 SAHTREE_LOCK_ASSERT();
3025 /* search all status */
3027 stateidx < _ARRAYLEN(saorder_state_alive);
3030 state = saorder_state_alive[stateidx];
3031 LIST_FOREACH(sav, &sah->savtree[state], chain) {
3034 if (sav->state != state) {
3035 ipseclog((LOG_DEBUG, "%s: "
3036 "invalid sav->state (queue: %d SA: %d)\n",
3037 __func__, state, sav->state));
3041 if (sav->spi == spi)
3050 * copy SA values from PF_KEY message except *SPI, SEQ, PID, STATE and TYPE*.
3051 * You must update these if need.
3055 * does not modify mbuf. does not free mbuf on error.
3058 key_setsaval(sav, m, mhp)
3059 struct secasvar *sav;
3061 const struct sadb_msghdr *mhp;
3065 IPSEC_ASSERT(m != NULL, ("null mbuf"));
3066 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
3067 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
3069 /* initialization */
3071 sav->key_auth = NULL;
3072 sav->key_enc = NULL;
3079 sav->tdb_xform = NULL; /* transform */
3080 sav->tdb_encalgxform = NULL; /* encoding algorithm */
3081 sav->tdb_authalgxform = NULL; /* authentication algorithm */
3082 sav->tdb_compalgxform = NULL; /* compression algorithm */
3083 /* Initialize even if NAT-T not compiled in: */
3085 sav->natt_esp_frag_len = 0;
3088 if (mhp->ext[SADB_EXT_SA] != NULL) {
3089 const struct sadb_sa *sa0;
3091 sa0 = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
3092 if (mhp->extlen[SADB_EXT_SA] < sizeof(*sa0)) {
3097 sav->alg_auth = sa0->sadb_sa_auth;
3098 sav->alg_enc = sa0->sadb_sa_encrypt;
3099 sav->flags = sa0->sadb_sa_flags;
3102 if ((sa0->sadb_sa_flags & SADB_X_EXT_OLD) == 0) {
3103 sav->replay = (struct secreplay *)
3104 malloc(sizeof(struct secreplay)+sa0->sadb_sa_replay, M_IPSEC_MISC, M_NOWAIT|M_ZERO);
3105 if (sav->replay == NULL) {
3106 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3111 if (sa0->sadb_sa_replay != 0)
3112 sav->replay->bitmap = (caddr_t)(sav->replay+1);
3113 sav->replay->wsize = sa0->sadb_sa_replay;
3117 /* Authentication keys */
3118 if (mhp->ext[SADB_EXT_KEY_AUTH] != NULL) {
3119 const struct sadb_key *key0;
3122 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_AUTH];
3123 len = mhp->extlen[SADB_EXT_KEY_AUTH];
3126 if (len < sizeof(*key0)) {
3130 switch (mhp->msg->sadb_msg_satype) {
3131 case SADB_SATYPE_AH:
3132 case SADB_SATYPE_ESP:
3133 case SADB_X_SATYPE_TCPSIGNATURE:
3134 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3135 sav->alg_auth != SADB_X_AALG_NULL)
3138 case SADB_X_SATYPE_IPCOMP:
3144 ipseclog((LOG_DEBUG, "%s: invalid key_auth values.\n",
3149 sav->key_auth = (struct seckey *)key_dup_keymsg(key0, len,
3151 if (sav->key_auth == NULL ) {
3152 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3159 /* Encryption key */
3160 if (mhp->ext[SADB_EXT_KEY_ENCRYPT] != NULL) {
3161 const struct sadb_key *key0;
3164 key0 = (const struct sadb_key *)mhp->ext[SADB_EXT_KEY_ENCRYPT];
3165 len = mhp->extlen[SADB_EXT_KEY_ENCRYPT];
3168 if (len < sizeof(*key0)) {
3172 switch (mhp->msg->sadb_msg_satype) {
3173 case SADB_SATYPE_ESP:
3174 if (len == PFKEY_ALIGN8(sizeof(struct sadb_key)) &&
3175 sav->alg_enc != SADB_EALG_NULL) {
3179 sav->key_enc = (struct seckey *)key_dup_keymsg(key0,
3182 if (sav->key_enc == NULL) {
3183 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3189 case SADB_X_SATYPE_IPCOMP:
3190 if (len != PFKEY_ALIGN8(sizeof(struct sadb_key)))
3192 sav->key_enc = NULL; /*just in case*/
3194 case SADB_SATYPE_AH:
3195 case SADB_X_SATYPE_TCPSIGNATURE:
3201 ipseclog((LOG_DEBUG, "%s: invalid key_enc value.\n",
3210 switch (mhp->msg->sadb_msg_satype) {
3211 case SADB_SATYPE_AH:
3212 error = xform_init(sav, XF_AH);
3214 case SADB_SATYPE_ESP:
3215 error = xform_init(sav, XF_ESP);
3217 case SADB_X_SATYPE_IPCOMP:
3218 error = xform_init(sav, XF_IPCOMP);
3220 case SADB_X_SATYPE_TCPSIGNATURE:
3221 error = xform_init(sav, XF_TCPSIGNATURE);
3225 ipseclog((LOG_DEBUG, "%s: unable to initialize SA type %u.\n",
3226 __func__, mhp->msg->sadb_msg_satype));
3231 sav->created = time_second;
3233 /* make lifetime for CURRENT */
3234 sav->lft_c = malloc(sizeof(struct seclifetime), M_IPSEC_MISC, M_NOWAIT);
3235 if (sav->lft_c == NULL) {
3236 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3241 sav->lft_c->allocations = 0;
3242 sav->lft_c->bytes = 0;
3243 sav->lft_c->addtime = time_second;
3244 sav->lft_c->usetime = 0;
3246 /* lifetimes for HARD and SOFT */
3248 const struct sadb_lifetime *lft0;
3250 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_HARD];
3252 if (mhp->extlen[SADB_EXT_LIFETIME_HARD] < sizeof(*lft0)) {
3256 sav->lft_h = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3257 if (sav->lft_h == NULL) {
3258 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3262 /* to be initialize ? */
3265 lft0 = (struct sadb_lifetime *)mhp->ext[SADB_EXT_LIFETIME_SOFT];
3267 if (mhp->extlen[SADB_EXT_LIFETIME_SOFT] < sizeof(*lft0)) {
3271 sav->lft_s = key_dup_lifemsg(lft0, M_IPSEC_MISC);
3272 if (sav->lft_s == NULL) {
3273 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
3277 /* to be initialize ? */
3284 /* initialization */
3291 * validation with a secasvar entry, and set SADB_SATYPE_MATURE.
3296 key_mature(struct secasvar *sav)
3300 /* check SPI value */
3301 switch (sav->sah->saidx.proto) {
3305 * RFC 4302, 2.4. Security Parameters Index (SPI), SPI values
3306 * 1-255 reserved by IANA for future use,
3307 * 0 for implementation specific, local use.
3309 if (ntohl(sav->spi) <= 255) {
3310 ipseclog((LOG_DEBUG, "%s: illegal range of SPI %u.\n",
3311 __func__, (u_int32_t)ntohl(sav->spi)));
3318 switch (sav->sah->saidx.proto) {
3321 if ((sav->flags & (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) ==
3322 (SADB_X_EXT_OLD|SADB_X_EXT_DERIV)) {
3323 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3324 "given to old-esp.\n", __func__));
3327 error = xform_init(sav, XF_ESP);
3331 if (sav->flags & SADB_X_EXT_DERIV) {
3332 ipseclog((LOG_DEBUG, "%s: invalid flag (derived) "
3333 "given to AH SA.\n", __func__));
3336 if (sav->alg_enc != SADB_EALG_NONE) {
3337 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3338 "mismated.\n", __func__));
3341 error = xform_init(sav, XF_AH);
3343 case IPPROTO_IPCOMP:
3344 if (sav->alg_auth != SADB_AALG_NONE) {
3345 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3346 "mismated.\n", __func__));
3349 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0
3350 && ntohl(sav->spi) >= 0x10000) {
3351 ipseclog((LOG_DEBUG, "%s: invalid cpi for IPComp.\n",
3355 error = xform_init(sav, XF_IPCOMP);
3358 if (sav->alg_enc != SADB_EALG_NONE) {
3359 ipseclog((LOG_DEBUG, "%s: protocol and algorithm "
3360 "mismated.\n", __func__));
3363 error = xform_init(sav, XF_TCPSIGNATURE);
3366 ipseclog((LOG_DEBUG, "%s: Invalid satype.\n", __func__));
3367 error = EPROTONOSUPPORT;
3372 key_sa_chgstate(sav, SADB_SASTATE_MATURE);
3379 * subroutine for SADB_GET and SADB_DUMP.
3381 static struct mbuf *
3382 key_setdumpsa(struct secasvar *sav, u_int8_t type, u_int8_t satype,
3383 u_int32_t seq, u_int32_t pid)
3385 struct mbuf *result = NULL, *tres = NULL, *m;
3388 SADB_EXT_SA, SADB_X_EXT_SA2,
3389 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
3390 SADB_EXT_LIFETIME_CURRENT, SADB_EXT_ADDRESS_SRC,
3391 SADB_EXT_ADDRESS_DST, SADB_EXT_ADDRESS_PROXY, SADB_EXT_KEY_AUTH,
3392 SADB_EXT_KEY_ENCRYPT, SADB_EXT_IDENTITY_SRC,
3393 SADB_EXT_IDENTITY_DST, SADB_EXT_SENSITIVITY,
3395 SADB_X_EXT_NAT_T_TYPE,
3396 SADB_X_EXT_NAT_T_SPORT, SADB_X_EXT_NAT_T_DPORT,
3397 SADB_X_EXT_NAT_T_OAI, SADB_X_EXT_NAT_T_OAR,
3398 SADB_X_EXT_NAT_T_FRAG,
3402 m = key_setsadbmsg(type, 0, satype, seq, pid, sav->refcnt);
3407 for (i = sizeof(dumporder)/sizeof(dumporder[0]) - 1; i >= 0; i--) {
3409 switch (dumporder[i]) {
3411 m = key_setsadbsa(sav);
3416 case SADB_X_EXT_SA2:
3417 m = key_setsadbxsa2(sav->sah->saidx.mode,
3418 sav->replay ? sav->replay->count : 0,
3419 sav->sah->saidx.reqid);
3424 case SADB_EXT_ADDRESS_SRC:
3425 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
3426 &sav->sah->saidx.src.sa,
3427 FULLMASK, IPSEC_ULPROTO_ANY);
3432 case SADB_EXT_ADDRESS_DST:
3433 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
3434 &sav->sah->saidx.dst.sa,
3435 FULLMASK, IPSEC_ULPROTO_ANY);
3440 case SADB_EXT_KEY_AUTH:
3443 m = key_setkey(sav->key_auth, SADB_EXT_KEY_AUTH);
3448 case SADB_EXT_KEY_ENCRYPT:
3451 m = key_setkey(sav->key_enc, SADB_EXT_KEY_ENCRYPT);
3456 case SADB_EXT_LIFETIME_CURRENT:
3459 m = key_setlifetime(sav->lft_c,
3460 SADB_EXT_LIFETIME_CURRENT);
3465 case SADB_EXT_LIFETIME_HARD:
3468 m = key_setlifetime(sav->lft_h,
3469 SADB_EXT_LIFETIME_HARD);
3474 case SADB_EXT_LIFETIME_SOFT:
3477 m = key_setlifetime(sav->lft_s,
3478 SADB_EXT_LIFETIME_SOFT);
3485 case SADB_X_EXT_NAT_T_TYPE:
3486 m = key_setsadbxtype(sav->natt_type);
3491 case SADB_X_EXT_NAT_T_DPORT:
3492 m = key_setsadbxport(
3493 KEY_PORTFROMSADDR(&sav->sah->saidx.dst),
3494 SADB_X_EXT_NAT_T_DPORT);
3499 case SADB_X_EXT_NAT_T_SPORT:
3500 m = key_setsadbxport(
3501 KEY_PORTFROMSADDR(&sav->sah->saidx.src),
3502 SADB_X_EXT_NAT_T_SPORT);
3507 case SADB_X_EXT_NAT_T_OAI:
3508 case SADB_X_EXT_NAT_T_OAR:
3509 case SADB_X_EXT_NAT_T_FRAG:
3510 /* We do not (yet) support those. */
3514 case SADB_EXT_ADDRESS_PROXY:
3515 case SADB_EXT_IDENTITY_SRC:
3516 case SADB_EXT_IDENTITY_DST:
3517 /* XXX: should we brought from SPD ? */
3518 case SADB_EXT_SENSITIVITY:
3531 m_cat(result, tres);
3532 if (result->m_len < sizeof(struct sadb_msg)) {
3533 result = m_pullup(result, sizeof(struct sadb_msg));
3538 result->m_pkthdr.len = 0;
3539 for (m = result; m; m = m->m_next)
3540 result->m_pkthdr.len += m->m_len;
3542 mtod(result, struct sadb_msg *)->sadb_msg_len =
3543 PFKEY_UNIT64(result->m_pkthdr.len);
3554 * set data into sadb_msg.
3556 static struct mbuf *
3557 key_setsadbmsg(u_int8_t type, u_int16_t tlen, u_int8_t satype, u_int32_t seq,
3558 pid_t pid, u_int16_t reserved)
3564 len = PFKEY_ALIGN8(sizeof(struct sadb_msg));
3567 MGETHDR(m, M_DONTWAIT, MT_DATA);
3568 if (m && len > MHLEN) {
3569 MCLGET(m, M_DONTWAIT);
3570 if ((m->m_flags & M_EXT) == 0) {
3577 m->m_pkthdr.len = m->m_len = len;
3580 p = mtod(m, struct sadb_msg *);
3583 p->sadb_msg_version = PF_KEY_V2;
3584 p->sadb_msg_type = type;
3585 p->sadb_msg_errno = 0;
3586 p->sadb_msg_satype = satype;
3587 p->sadb_msg_len = PFKEY_UNIT64(tlen);
3588 p->sadb_msg_reserved = reserved;
3589 p->sadb_msg_seq = seq;
3590 p->sadb_msg_pid = (u_int32_t)pid;
3596 * copy secasvar data into sadb_address.
3598 static struct mbuf *
3600 struct secasvar *sav;
3606 len = PFKEY_ALIGN8(sizeof(struct sadb_sa));
3607 m = key_alloc_mbuf(len);
3608 if (!m || m->m_next) { /*XXX*/
3614 p = mtod(m, struct sadb_sa *);
3617 p->sadb_sa_len = PFKEY_UNIT64(len);
3618 p->sadb_sa_exttype = SADB_EXT_SA;
3619 p->sadb_sa_spi = sav->spi;
3620 p->sadb_sa_replay = (sav->replay != NULL ? sav->replay->wsize : 0);
3621 p->sadb_sa_state = sav->state;
3622 p->sadb_sa_auth = sav->alg_auth;
3623 p->sadb_sa_encrypt = sav->alg_enc;
3624 p->sadb_sa_flags = sav->flags;
3630 * set data into sadb_address.
3632 static struct mbuf *
3633 key_setsadbaddr(u_int16_t exttype, const struct sockaddr *saddr, u_int8_t prefixlen, u_int16_t ul_proto)
3636 struct sadb_address *p;
3639 len = PFKEY_ALIGN8(sizeof(struct sadb_address)) +
3640 PFKEY_ALIGN8(saddr->sa_len);
3641 m = key_alloc_mbuf(len);
3642 if (!m || m->m_next) { /*XXX*/
3648 p = mtod(m, struct sadb_address *);
3651 p->sadb_address_len = PFKEY_UNIT64(len);
3652 p->sadb_address_exttype = exttype;
3653 p->sadb_address_proto = ul_proto;
3654 if (prefixlen == FULLMASK) {
3655 switch (saddr->sa_family) {
3657 prefixlen = sizeof(struct in_addr) << 3;
3660 prefixlen = sizeof(struct in6_addr) << 3;
3666 p->sadb_address_prefixlen = prefixlen;
3667 p->sadb_address_reserved = 0;
3670 mtod(m, caddr_t) + PFKEY_ALIGN8(sizeof(struct sadb_address)),
3677 * set data into sadb_x_sa2.
3679 static struct mbuf *
3680 key_setsadbxsa2(u_int8_t mode, u_int32_t seq, u_int32_t reqid)
3683 struct sadb_x_sa2 *p;
3686 len = PFKEY_ALIGN8(sizeof(struct sadb_x_sa2));
3687 m = key_alloc_mbuf(len);
3688 if (!m || m->m_next) { /*XXX*/
3694 p = mtod(m, struct sadb_x_sa2 *);
3697 p->sadb_x_sa2_len = PFKEY_UNIT64(len);
3698 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
3699 p->sadb_x_sa2_mode = mode;
3700 p->sadb_x_sa2_reserved1 = 0;
3701 p->sadb_x_sa2_reserved2 = 0;
3702 p->sadb_x_sa2_sequence = seq;
3703 p->sadb_x_sa2_reqid = reqid;
3710 * Set a type in sadb_x_nat_t_type.
3712 static struct mbuf *
3713 key_setsadbxtype(u_int16_t type)
3717 struct sadb_x_nat_t_type *p;
3719 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_type));
3721 m = key_alloc_mbuf(len);
3722 if (!m || m->m_next) { /*XXX*/
3728 p = mtod(m, struct sadb_x_nat_t_type *);
3731 p->sadb_x_nat_t_type_len = PFKEY_UNIT64(len);
3732 p->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
3733 p->sadb_x_nat_t_type_type = type;
3738 * Set a port in sadb_x_nat_t_port.
3739 * In contrast to default RFC 2367 behaviour, port is in network byte order.
3741 static struct mbuf *
3742 key_setsadbxport(u_int16_t port, u_int16_t type)
3746 struct sadb_x_nat_t_port *p;
3748 len = PFKEY_ALIGN8(sizeof(struct sadb_x_nat_t_port));
3750 m = key_alloc_mbuf(len);
3751 if (!m || m->m_next) { /*XXX*/
3757 p = mtod(m, struct sadb_x_nat_t_port *);
3760 p->sadb_x_nat_t_port_len = PFKEY_UNIT64(len);
3761 p->sadb_x_nat_t_port_exttype = type;
3762 p->sadb_x_nat_t_port_port = port;
3768 * Get port from sockaddr. Port is in network byte order.
3771 key_portfromsaddr(struct sockaddr *sa)
3774 switch (sa->sa_family) {
3777 return ((struct sockaddr_in *)sa)->sin_port;
3781 return ((struct sockaddr_in6 *)sa)->sin6_port;
3784 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
3785 printf("DP %s unexpected address family %d\n",
3786 __func__, sa->sa_family));
3789 #endif /* IPSEC_NAT_T */
3792 * Set port in struct sockaddr. Port is in network byte order.
3795 key_porttosaddr(struct sockaddr *sa, u_int16_t port)
3798 switch (sa->sa_family) {
3801 ((struct sockaddr_in *)sa)->sin_port = port;
3806 ((struct sockaddr_in6 *)sa)->sin6_port = port;
3810 ipseclog((LOG_DEBUG, "%s: unexpected address family %d.\n",
3811 __func__, sa->sa_family));
3817 * set data into sadb_x_policy
3819 static struct mbuf *
3820 key_setsadbxpolicy(u_int16_t type, u_int8_t dir, u_int32_t id)
3823 struct sadb_x_policy *p;
3826 len = PFKEY_ALIGN8(sizeof(struct sadb_x_policy));
3827 m = key_alloc_mbuf(len);
3828 if (!m || m->m_next) { /*XXX*/
3834 p = mtod(m, struct sadb_x_policy *);
3837 p->sadb_x_policy_len = PFKEY_UNIT64(len);
3838 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3839 p->sadb_x_policy_type = type;
3840 p->sadb_x_policy_dir = dir;
3841 p->sadb_x_policy_id = id;
3847 /* Take a key message (sadb_key) from the socket and turn it into one
3848 * of the kernel's key structures (seckey).
3850 * IN: pointer to the src
3851 * OUT: NULL no more memory
3854 key_dup_keymsg(const struct sadb_key *src, u_int len,
3855 struct malloc_type *type)
3858 dst = (struct seckey *)malloc(sizeof(struct seckey), type, M_NOWAIT);
3860 dst->bits = src->sadb_key_bits;
3861 dst->key_data = (char *)malloc(len, type, M_NOWAIT);
3862 if (dst->key_data != NULL) {
3863 bcopy((const char *)src + sizeof(struct sadb_key),
3864 dst->key_data, len);
3866 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3872 ipseclog((LOG_DEBUG, "%s: No more memory.\n",
3879 /* Take a lifetime message (sadb_lifetime) passed in on a socket and
3880 * turn it into one of the kernel's lifetime structures (seclifetime).
3882 * IN: pointer to the destination, source and malloc type
3883 * OUT: NULL, no more memory
3886 static struct seclifetime *
3887 key_dup_lifemsg(const struct sadb_lifetime *src,
3888 struct malloc_type *type)
3890 struct seclifetime *dst = NULL;
3892 dst = (struct seclifetime *)malloc(sizeof(struct seclifetime),
3896 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
3898 dst->allocations = src->sadb_lifetime_allocations;
3899 dst->bytes = src->sadb_lifetime_bytes;
3900 dst->addtime = src->sadb_lifetime_addtime;
3901 dst->usetime = src->sadb_lifetime_usetime;
3906 /* compare my own address
3907 * OUT: 1: true, i.e. my address.
3912 struct sockaddr *sa;
3915 struct sockaddr_in *sin;
3916 struct in_ifaddr *ia;
3919 IPSEC_ASSERT(sa != NULL, ("null sockaddr"));
3921 switch (sa->sa_family) {
3924 sin = (struct sockaddr_in *)sa;
3926 for (ia = V_in_ifaddrhead.tqh_first; ia;
3927 ia = ia->ia_link.tqe_next)
3929 if (sin->sin_family == ia->ia_addr.sin_family &&
3930 sin->sin_len == ia->ia_addr.sin_len &&
3931 sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
3933 IN_IFADDR_RUNLOCK();
3937 IN_IFADDR_RUNLOCK();
3942 return key_ismyaddr6((struct sockaddr_in6 *)sa);
3951 * compare my own address for IPv6.
3954 * NOTE: derived ip6_input() in KAME. This is necessary to modify more.
3956 #include <netinet6/in6_var.h>
3960 struct sockaddr_in6 *sin6;
3962 struct in6_ifaddr *ia;
3964 struct in6_multi *in6m;
3968 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
3969 if (key_sockaddrcmp((struct sockaddr *)&sin6,
3970 (struct sockaddr *)&ia->ia_addr, 0) == 0) {
3971 IN6_IFADDR_RUNLOCK();
3978 * XXX why do we care about multlicast here while we don't care
3979 * about IPv4 multicast??
3983 IN6_LOOKUP_MULTI(sin6->sin6_addr, ia->ia_ifp, in6m);
3985 IN6_IFADDR_RUNLOCK();
3990 IN6_IFADDR_RUNLOCK();
3992 /* loopback, just for safety */
3993 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
4001 * compare two secasindex structure.
4002 * flag can specify to compare 2 saidxes.
4003 * compare two secasindex structure without both mode and reqid.
4004 * don't compare port.
4006 * saidx0: source, it can be in SAD.
4014 const struct secasindex *saidx0,
4015 const struct secasindex *saidx1,
4021 if (saidx0 == NULL && saidx1 == NULL)
4024 if (saidx0 == NULL || saidx1 == NULL)
4027 if (saidx0->proto != saidx1->proto)
4030 if (flag == CMP_EXACTLY) {
4031 if (saidx0->mode != saidx1->mode)
4033 if (saidx0->reqid != saidx1->reqid)
4035 if (bcmp(&saidx0->src, &saidx1->src, saidx0->src.sa.sa_len) != 0 ||
4036 bcmp(&saidx0->dst, &saidx1->dst, saidx0->dst.sa.sa_len) != 0)
4040 /* CMP_MODE_REQID, CMP_REQID, CMP_HEAD */
4041 if (flag == CMP_MODE_REQID
4042 ||flag == CMP_REQID) {
4044 * If reqid of SPD is non-zero, unique SA is required.
4045 * The result must be of same reqid in this case.
4047 if (saidx1->reqid != 0 && saidx0->reqid != saidx1->reqid)
4051 if (flag == CMP_MODE_REQID) {
4052 if (saidx0->mode != IPSEC_MODE_ANY
4053 && saidx0->mode != saidx1->mode)
4059 * If NAT-T is enabled, check ports for tunnel mode.
4060 * Do not check ports if they are set to zero in the SPD.
4061 * Also do not do it for transport mode, as there is no
4062 * port information available in the SP.
4064 if (saidx1->mode == IPSEC_MODE_TUNNEL &&
4065 saidx1->src.sa.sa_family == AF_INET &&
4066 saidx1->dst.sa.sa_family == AF_INET &&
4067 ((const struct sockaddr_in *)(&saidx1->src))->sin_port &&
4068 ((const struct sockaddr_in *)(&saidx1->dst))->sin_port)
4070 #endif /* IPSEC_NAT_T */
4072 if (key_sockaddrcmp(&saidx0->src.sa, &saidx1->src.sa, chkport) != 0) {
4075 if (key_sockaddrcmp(&saidx0->dst.sa, &saidx1->dst.sa, chkport) != 0) {
4084 * compare two secindex structure exactly.
4086 * spidx0: source, it is often in SPD.
4087 * spidx1: object, it is often from PFKEY message.
4093 key_cmpspidx_exactly(
4094 struct secpolicyindex *spidx0,
4095 struct secpolicyindex *spidx1)
4098 if (spidx0 == NULL && spidx1 == NULL)
4101 if (spidx0 == NULL || spidx1 == NULL)
4104 if (spidx0->prefs != spidx1->prefs
4105 || spidx0->prefd != spidx1->prefd
4106 || spidx0->ul_proto != spidx1->ul_proto)
4109 return key_sockaddrcmp(&spidx0->src.sa, &spidx1->src.sa, 1) == 0 &&
4110 key_sockaddrcmp(&spidx0->dst.sa, &spidx1->dst.sa, 1) == 0;
4114 * compare two secindex structure with mask.
4116 * spidx0: source, it is often in SPD.
4117 * spidx1: object, it is often from IP header.
4123 key_cmpspidx_withmask(
4124 struct secpolicyindex *spidx0,
4125 struct secpolicyindex *spidx1)
4128 if (spidx0 == NULL && spidx1 == NULL)
4131 if (spidx0 == NULL || spidx1 == NULL)
4134 if (spidx0->src.sa.sa_family != spidx1->src.sa.sa_family ||
4135 spidx0->dst.sa.sa_family != spidx1->dst.sa.sa_family ||
4136 spidx0->src.sa.sa_len != spidx1->src.sa.sa_len ||
4137 spidx0->dst.sa.sa_len != spidx1->dst.sa.sa_len)
4140 /* if spidx.ul_proto == IPSEC_ULPROTO_ANY, ignore. */
4141 if (spidx0->ul_proto != (u_int16_t)IPSEC_ULPROTO_ANY
4142 && spidx0->ul_proto != spidx1->ul_proto)
4145 switch (spidx0->src.sa.sa_family) {
4147 if (spidx0->src.sin.sin_port != IPSEC_PORT_ANY
4148 && spidx0->src.sin.sin_port != spidx1->src.sin.sin_port)
4150 if (!key_bbcmp(&spidx0->src.sin.sin_addr,
4151 &spidx1->src.sin.sin_addr, spidx0->prefs))
4155 if (spidx0->src.sin6.sin6_port != IPSEC_PORT_ANY
4156 && spidx0->src.sin6.sin6_port != spidx1->src.sin6.sin6_port)
4159 * scope_id check. if sin6_scope_id is 0, we regard it
4160 * as a wildcard scope, which matches any scope zone ID.
4162 if (spidx0->src.sin6.sin6_scope_id &&
4163 spidx1->src.sin6.sin6_scope_id &&
4164 spidx0->src.sin6.sin6_scope_id != spidx1->src.sin6.sin6_scope_id)
4166 if (!key_bbcmp(&spidx0->src.sin6.sin6_addr,
4167 &spidx1->src.sin6.sin6_addr, spidx0->prefs))
4172 if (bcmp(&spidx0->src, &spidx1->src, spidx0->src.sa.sa_len) != 0)
4177 switch (spidx0->dst.sa.sa_family) {
4179 if (spidx0->dst.sin.sin_port != IPSEC_PORT_ANY
4180 && spidx0->dst.sin.sin_port != spidx1->dst.sin.sin_port)
4182 if (!key_bbcmp(&spidx0->dst.sin.sin_addr,
4183 &spidx1->dst.sin.sin_addr, spidx0->prefd))
4187 if (spidx0->dst.sin6.sin6_port != IPSEC_PORT_ANY
4188 && spidx0->dst.sin6.sin6_port != spidx1->dst.sin6.sin6_port)
4191 * scope_id check. if sin6_scope_id is 0, we regard it
4192 * as a wildcard scope, which matches any scope zone ID.
4194 if (spidx0->dst.sin6.sin6_scope_id &&
4195 spidx1->dst.sin6.sin6_scope_id &&
4196 spidx0->dst.sin6.sin6_scope_id != spidx1->dst.sin6.sin6_scope_id)
4198 if (!key_bbcmp(&spidx0->dst.sin6.sin6_addr,
4199 &spidx1->dst.sin6.sin6_addr, spidx0->prefd))
4204 if (bcmp(&spidx0->dst, &spidx1->dst, spidx0->dst.sa.sa_len) != 0)
4209 /* XXX Do we check other field ? e.g. flowinfo */
4214 /* returns 0 on match */
4217 const struct sockaddr *sa1,
4218 const struct sockaddr *sa2,
4224 #define satosin(s) ((const struct sockaddr_in *)s)
4228 #define satosin6(s) ((const struct sockaddr_in6 *)s)
4229 if (sa1->sa_family != sa2->sa_family || sa1->sa_len != sa2->sa_len)
4232 switch (sa1->sa_family) {
4234 if (sa1->sa_len != sizeof(struct sockaddr_in))
4236 if (satosin(sa1)->sin_addr.s_addr !=
4237 satosin(sa2)->sin_addr.s_addr) {
4240 if (port && satosin(sa1)->sin_port != satosin(sa2)->sin_port)
4244 if (sa1->sa_len != sizeof(struct sockaddr_in6))
4245 return 1; /*EINVAL*/
4246 if (satosin6(sa1)->sin6_scope_id !=
4247 satosin6(sa2)->sin6_scope_id) {
4250 if (!IN6_ARE_ADDR_EQUAL(&satosin6(sa1)->sin6_addr,
4251 &satosin6(sa2)->sin6_addr)) {
4255 satosin6(sa1)->sin6_port != satosin6(sa2)->sin6_port) {
4260 if (bcmp(sa1, sa2, sa1->sa_len) != 0)
4271 * compare two buffers with mask.
4275 * bits: Number of bits to compare
4281 key_bbcmp(const void *a1, const void *a2, u_int bits)
4283 const unsigned char *p1 = a1;
4284 const unsigned char *p2 = a2;
4286 /* XXX: This could be considerably faster if we compare a word
4287 * at a time, but it is complicated on LSB Endian machines */
4289 /* Handle null pointers */
4290 if (p1 == NULL || p2 == NULL)
4300 u_int8_t mask = ~((1<<(8-bits))-1);
4301 if ((*p1 & mask) != (*p2 & mask))
4304 return 1; /* Match! */
4308 key_flush_spd(time_t now)
4310 static u_int16_t sptree_scangen = 0;
4311 u_int16_t gen = sptree_scangen++;
4312 struct secpolicy *sp;
4316 for (dir = 0; dir < IPSEC_DIR_MAX; dir++) {
4319 LIST_FOREACH(sp, &V_sptree[dir], chain) {
4320 if (sp->scangen == gen) /* previously handled */
4323 if (sp->state == IPSEC_SPSTATE_DEAD &&
4326 * Ensure that we only decrease refcnt once,
4327 * when we're the last consumer.
4328 * Directly call SP_DELREF/key_delsp instead
4329 * of KEY_FREESP to avoid unlocking/relocking
4330 * SPTREE_LOCK before key_delsp: may refcnt
4331 * be increased again during that time ?
4332 * NB: also clean entries created by
4340 if (sp->lifetime == 0 && sp->validtime == 0)
4342 if ((sp->lifetime && now - sp->created > sp->lifetime)
4343 || (sp->validtime && now - sp->lastused > sp->validtime)) {
4344 sp->state = IPSEC_SPSTATE_DEAD;
4355 key_flush_sad(time_t now)
4357 struct secashead *sah, *nextsah;
4358 struct secasvar *sav, *nextsav;
4362 LIST_FOREACH_SAFE(sah, &V_sahtree, chain, nextsah) {
4363 /* if sah has been dead, then delete it and process next sah. */
4364 if (sah->state == SADB_SASTATE_DEAD) {
4369 /* if LARVAL entry doesn't become MATURE, delete it. */
4370 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_LARVAL], chain, nextsav) {
4371 /* Need to also check refcnt for a larval SA ??? */
4372 if (now - sav->created > V_key_larval_lifetime)
4377 * check MATURE entry to start to send expire message
4380 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_MATURE], chain, nextsav) {
4381 /* we don't need to check. */
4382 if (sav->lft_s == NULL)
4386 if (sav->lft_c == NULL) {
4387 ipseclog((LOG_DEBUG,"%s: there is no CURRENT "
4388 "time, why?\n", __func__));
4392 /* check SOFT lifetime */
4393 if (sav->lft_s->addtime != 0 &&
4394 now - sav->created > sav->lft_s->addtime) {
4395 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4397 * Actually, only send expire message if
4398 * SA has been used, as it was done before,
4399 * but should we always send such message,
4400 * and let IKE daemon decide if it should be
4401 * renegotiated or not ?
4402 * XXX expire message will actually NOT be
4403 * sent if SA is only used after soft
4404 * lifetime has been reached, see below
4407 if (sav->lft_c->usetime != 0)
4410 /* check SOFT lifetime by bytes */
4412 * XXX I don't know the way to delete this SA
4413 * when new SA is installed. Caution when it's
4414 * installed too big lifetime by time.
4416 else if (sav->lft_s->bytes != 0 &&
4417 sav->lft_s->bytes < sav->lft_c->bytes) {
4419 key_sa_chgstate(sav, SADB_SASTATE_DYING);
4421 * XXX If we keep to send expire
4422 * message in the status of
4423 * DYING. Do remove below code.
4429 /* check DYING entry to change status to DEAD. */
4430 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DYING], chain, nextsav) {
4431 /* we don't need to check. */
4432 if (sav->lft_h == NULL)
4436 if (sav->lft_c == NULL) {
4437 ipseclog((LOG_DEBUG, "%s: there is no CURRENT "
4438 "time, why?\n", __func__));
4442 if (sav->lft_h->addtime != 0 &&
4443 now - sav->created > sav->lft_h->addtime) {
4444 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4447 #if 0 /* XXX Should we keep to send expire message until HARD lifetime ? */
4448 else if (sav->lft_s != NULL
4449 && sav->lft_s->addtime != 0
4450 && now - sav->created > sav->lft_s->addtime) {
4452 * XXX: should be checked to be
4453 * installed the valid SA.
4457 * If there is no SA then sending
4463 /* check HARD lifetime by bytes */
4464 else if (sav->lft_h->bytes != 0 &&
4465 sav->lft_h->bytes < sav->lft_c->bytes) {
4466 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
4471 /* delete entry in DEAD */
4472 LIST_FOREACH_SAFE(sav, &sah->savtree[SADB_SASTATE_DEAD], chain, nextsav) {
4474 if (sav->state != SADB_SASTATE_DEAD) {
4475 ipseclog((LOG_DEBUG, "%s: invalid sav->state "
4476 "(queue: %d SA: %d): kill it anyway\n",
4478 SADB_SASTATE_DEAD, sav->state));
4481 * do not call key_freesav() here.
4482 * sav should already be freed, and sav->refcnt
4483 * shows other references to sav
4484 * (such as from SPD).
4492 key_flush_acq(time_t now)
4494 struct secacq *acq, *nextacq;
4498 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
4499 nextacq = LIST_NEXT(acq, chain);
4500 if (now - acq->created > V_key_blockacq_lifetime
4501 && __LIST_CHAINED(acq)) {
4502 LIST_REMOVE(acq, chain);
4503 free(acq, M_IPSEC_SAQ);
4510 key_flush_spacq(time_t now)
4512 struct secspacq *acq, *nextacq;
4516 for (acq = LIST_FIRST(&V_spacqtree); acq != NULL; acq = nextacq) {
4517 nextacq = LIST_NEXT(acq, chain);
4518 if (now - acq->created > V_key_blockacq_lifetime
4519 && __LIST_CHAINED(acq)) {
4520 LIST_REMOVE(acq, chain);
4521 free(acq, M_IPSEC_SAQ);
4529 * scanning SPD and SAD to check status for each entries,
4530 * and do to remove or to expire.
4531 * XXX: year 2038 problem may remain.
4534 key_timehandler(void)
4536 VNET_ITERATOR_DECL(vnet_iter);
4537 time_t now = time_second;
4539 VNET_LIST_RLOCK_NOSLEEP();
4540 VNET_FOREACH(vnet_iter) {
4541 CURVNET_SET(vnet_iter);
4545 key_flush_spacq(now);
4548 VNET_LIST_RUNLOCK_NOSLEEP();
4550 #ifndef IPSEC_DEBUG2
4551 /* do exchange to tick time !! */
4552 (void)timeout((void *)key_timehandler, (void *)0, hz);
4553 #endif /* IPSEC_DEBUG2 */
4561 key_randomfill(&value, sizeof(value));
4566 key_randomfill(p, l)
4572 static int warn = 1;
4575 n = (size_t)read_random(p, (u_int)l);
4579 bcopy(&v, (u_int8_t *)p + n,
4580 l - n < sizeof(v) ? l - n : sizeof(v));
4584 printf("WARNING: pseudo-random number generator "
4585 "used for IPsec processing\n");
4592 * map SADB_SATYPE_* to IPPROTO_*.
4593 * if satype == SADB_SATYPE then satype is mapped to ~0.
4595 * 0: invalid satype.
4598 key_satype2proto(u_int8_t satype)
4601 case SADB_SATYPE_UNSPEC:
4602 return IPSEC_PROTO_ANY;
4603 case SADB_SATYPE_AH:
4605 case SADB_SATYPE_ESP:
4607 case SADB_X_SATYPE_IPCOMP:
4608 return IPPROTO_IPCOMP;
4609 case SADB_X_SATYPE_TCPSIGNATURE:
4618 * map IPPROTO_* to SADB_SATYPE_*
4620 * 0: invalid protocol type.
4623 key_proto2satype(u_int16_t proto)
4627 return SADB_SATYPE_AH;
4629 return SADB_SATYPE_ESP;
4630 case IPPROTO_IPCOMP:
4631 return SADB_X_SATYPE_IPCOMP;
4633 return SADB_X_SATYPE_TCPSIGNATURE;
4642 * SADB_GETSPI processing is to receive
4643 * <base, (SA2), src address, dst address, (SPI range)>
4644 * from the IKMPd, to assign a unique spi value, to hang on the INBOUND
4645 * tree with the status of LARVAL, and send
4646 * <base, SA(*), address(SD)>
4649 * IN: mhp: pointer to the pointer to each header.
4650 * OUT: NULL if fail.
4651 * other if success, return pointer to the message to send.
4654 key_getspi(so, m, mhp)
4657 const struct sadb_msghdr *mhp;
4659 struct sadb_address *src0, *dst0;
4660 struct secasindex saidx;
4661 struct secashead *newsah;
4662 struct secasvar *newsav;
4669 IPSEC_ASSERT(so != NULL, ("null socket"));
4670 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4671 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4672 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4674 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
4675 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
4676 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4678 return key_senderror(so, m, EINVAL);
4680 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
4681 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
4682 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
4684 return key_senderror(so, m, EINVAL);
4686 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
4687 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
4688 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
4690 mode = IPSEC_MODE_ANY;
4694 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
4695 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
4697 /* map satype to proto */
4698 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4699 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4701 return key_senderror(so, m, EINVAL);
4705 * Make sure the port numbers are zero.
4706 * In case of NAT-T we will update them later if needed.
4708 switch (((struct sockaddr *)(src0 + 1))->sa_family) {
4710 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4711 sizeof(struct sockaddr_in))
4712 return key_senderror(so, m, EINVAL);
4713 ((struct sockaddr_in *)(src0 + 1))->sin_port = 0;
4716 if (((struct sockaddr *)(src0 + 1))->sa_len !=
4717 sizeof(struct sockaddr_in6))
4718 return key_senderror(so, m, EINVAL);
4719 ((struct sockaddr_in6 *)(src0 + 1))->sin6_port = 0;
4724 switch (((struct sockaddr *)(dst0 + 1))->sa_family) {
4726 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4727 sizeof(struct sockaddr_in))
4728 return key_senderror(so, m, EINVAL);
4729 ((struct sockaddr_in *)(dst0 + 1))->sin_port = 0;
4732 if (((struct sockaddr *)(dst0 + 1))->sa_len !=
4733 sizeof(struct sockaddr_in6))
4734 return key_senderror(so, m, EINVAL);
4735 ((struct sockaddr_in6 *)(dst0 + 1))->sin6_port = 0;
4741 /* XXX boundary check against sa_len */
4742 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
4746 * Handle NAT-T info if present.
4747 * We made sure the port numbers are zero above, so we do
4748 * not have to worry in case we do not update them.
4750 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL)
4751 ipseclog((LOG_DEBUG, "%s: NAT-T OAi present\n", __func__));
4752 if (mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL)
4753 ipseclog((LOG_DEBUG, "%s: NAT-T OAr present\n", __func__));
4755 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
4756 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
4757 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
4758 struct sadb_x_nat_t_type *type;
4759 struct sadb_x_nat_t_port *sport, *dport;
4761 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
4762 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
4763 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
4764 ipseclog((LOG_DEBUG, "%s: invalid nat-t message "
4765 "passed.\n", __func__));
4766 return key_senderror(so, m, EINVAL);
4769 sport = (struct sadb_x_nat_t_port *)
4770 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
4771 dport = (struct sadb_x_nat_t_port *)
4772 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
4775 KEY_PORTTOSADDR(&saidx.src, sport->sadb_x_nat_t_port_port);
4777 KEY_PORTTOSADDR(&saidx.dst, dport->sadb_x_nat_t_port_port);
4781 /* SPI allocation */
4782 spi = key_do_getnewspi((struct sadb_spirange *)mhp->ext[SADB_EXT_SPIRANGE],
4785 return key_senderror(so, m, EINVAL);
4787 /* get a SA index */
4788 if ((newsah = key_getsah(&saidx)) == NULL) {
4789 /* create a new SA index */
4790 if ((newsah = key_newsah(&saidx)) == NULL) {
4791 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
4792 return key_senderror(so, m, ENOBUFS);
4798 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
4799 if (newsav == NULL) {
4800 /* XXX don't free new SA index allocated in above. */
4801 return key_senderror(so, m, error);
4805 newsav->spi = htonl(spi);
4807 /* delete the entry in acqtree */
4808 if (mhp->msg->sadb_msg_seq != 0) {
4810 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) != NULL) {
4811 /* reset counter in order to deletion by timehandler. */
4812 acq->created = time_second;
4818 struct mbuf *n, *nn;
4819 struct sadb_sa *m_sa;
4820 struct sadb_msg *newmsg;
4823 /* create new sadb_msg to reply. */
4824 len = PFKEY_ALIGN8(sizeof(struct sadb_msg)) +
4825 PFKEY_ALIGN8(sizeof(struct sadb_sa));
4827 MGETHDR(n, M_DONTWAIT, MT_DATA);
4829 MCLGET(n, M_DONTWAIT);
4830 if ((n->m_flags & M_EXT) == 0) {
4836 return key_senderror(so, m, ENOBUFS);
4842 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
4843 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
4845 m_sa = (struct sadb_sa *)(mtod(n, caddr_t) + off);
4846 m_sa->sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4847 m_sa->sadb_sa_exttype = SADB_EXT_SA;
4848 m_sa->sadb_sa_spi = htonl(spi);
4849 off += PFKEY_ALIGN8(sizeof(struct sadb_sa));
4851 IPSEC_ASSERT(off == len,
4852 ("length inconsistency (off %u len %u)", off, len));
4854 n->m_next = key_gather_mbuf(m, mhp, 0, 2, SADB_EXT_ADDRESS_SRC,
4855 SADB_EXT_ADDRESS_DST);
4858 return key_senderror(so, m, ENOBUFS);
4861 if (n->m_len < sizeof(struct sadb_msg)) {
4862 n = m_pullup(n, sizeof(struct sadb_msg));
4864 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
4867 n->m_pkthdr.len = 0;
4868 for (nn = n; nn; nn = nn->m_next)
4869 n->m_pkthdr.len += nn->m_len;
4871 newmsg = mtod(n, struct sadb_msg *);
4872 newmsg->sadb_msg_seq = newsav->seq;
4873 newmsg->sadb_msg_errno = 0;
4874 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
4877 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
4882 * allocating new SPI
4883 * called by key_getspi().
4889 key_do_getnewspi(spirange, saidx)
4890 struct sadb_spirange *spirange;
4891 struct secasindex *saidx;
4895 int count = V_key_spi_trycnt;
4897 /* set spi range to allocate */
4898 if (spirange != NULL) {
4899 min = spirange->sadb_spirange_min;
4900 max = spirange->sadb_spirange_max;
4902 min = V_key_spi_minval;
4903 max = V_key_spi_maxval;
4905 /* IPCOMP needs 2-byte SPI */
4906 if (saidx->proto == IPPROTO_IPCOMP) {
4913 t = min; min = max; max = t;
4918 if (key_checkspidup(saidx, min) != NULL) {
4919 ipseclog((LOG_DEBUG, "%s: SPI %u exists already.\n",
4924 count--; /* taking one cost. */
4932 /* when requesting to allocate spi ranged */
4934 /* generate pseudo-random SPI value ranged. */
4935 newspi = min + (key_random() % (max - min + 1));
4937 if (key_checkspidup(saidx, newspi) == NULL)
4941 if (count == 0 || newspi == 0) {
4942 ipseclog((LOG_DEBUG, "%s: to allocate spi is failed.\n",
4949 keystat.getspi_count =
4950 (keystat.getspi_count + V_key_spi_trycnt - count) / 2;
4956 * SADB_UPDATE processing
4958 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4959 * key(AE), (identity(SD),) (sensitivity)>
4960 * from the ikmpd, and update a secasvar entry whose status is SADB_SASTATE_LARVAL.
4962 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
4963 * (identity(SD),) (sensitivity)>
4966 * m will always be freed.
4969 key_update(so, m, mhp)
4972 const struct sadb_msghdr *mhp;
4974 struct sadb_sa *sa0;
4975 struct sadb_address *src0, *dst0;
4977 struct sadb_x_nat_t_type *type;
4978 struct sadb_x_nat_t_port *sport, *dport;
4979 struct sadb_address *iaddr, *raddr;
4980 struct sadb_x_nat_t_frag *frag;
4982 struct secasindex saidx;
4983 struct secashead *sah;
4984 struct secasvar *sav;
4990 IPSEC_ASSERT(so != NULL, ("null socket"));
4991 IPSEC_ASSERT(m != NULL, ("null mbuf"));
4992 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
4993 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
4995 /* map satype to proto */
4996 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
4997 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
4999 return key_senderror(so, m, EINVAL);
5002 if (mhp->ext[SADB_EXT_SA] == NULL ||
5003 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5004 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5005 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5006 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5007 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5008 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5009 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5010 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5011 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5012 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5013 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5015 return key_senderror(so, m, EINVAL);
5017 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5018 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5019 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5020 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5022 return key_senderror(so, m, EINVAL);
5024 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5025 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5026 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5028 mode = IPSEC_MODE_ANY;
5031 /* XXX boundary checking for other extensions */
5033 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5034 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5035 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5037 /* XXX boundary check against sa_len */
5038 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5041 * Make sure the port numbers are zero.
5042 * In case of NAT-T we will update them later if needed.
5044 KEY_PORTTOSADDR(&saidx.src, 0);
5045 KEY_PORTTOSADDR(&saidx.dst, 0);
5049 * Handle NAT-T info if present.
5051 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
5052 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5053 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5055 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
5056 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5057 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5058 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5060 return key_senderror(so, m, EINVAL);
5063 type = (struct sadb_x_nat_t_type *)
5064 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5065 sport = (struct sadb_x_nat_t_port *)
5066 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5067 dport = (struct sadb_x_nat_t_port *)
5068 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5073 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
5074 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
5075 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
5076 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
5077 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5079 return key_senderror(so, m, EINVAL);
5081 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5082 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5083 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
5085 iaddr = raddr = NULL;
5087 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5088 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5089 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5091 return key_senderror(so, m, EINVAL);
5093 frag = (struct sadb_x_nat_t_frag *)
5094 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5100 /* get a SA header */
5101 if ((sah = key_getsah(&saidx)) == NULL) {
5102 ipseclog((LOG_DEBUG, "%s: no SA index found.\n", __func__));
5103 return key_senderror(so, m, ENOENT);
5106 /* set spidx if there */
5108 error = key_setident(sah, m, mhp);
5110 return key_senderror(so, m, error);
5112 /* find a SA with sequence number. */
5113 #ifdef IPSEC_DOSEQCHECK
5114 if (mhp->msg->sadb_msg_seq != 0
5115 && (sav = key_getsavbyseq(sah, mhp->msg->sadb_msg_seq)) == NULL) {
5116 ipseclog((LOG_DEBUG, "%s: no larval SA with sequence %u "
5117 "exists.\n", __func__, mhp->msg->sadb_msg_seq));
5118 return key_senderror(so, m, ENOENT);
5122 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5125 ipseclog((LOG_DEBUG, "%s: no such a SA found (spi:%u)\n",
5126 __func__, (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5127 return key_senderror(so, m, EINVAL);
5131 /* validity check */
5132 if (sav->sah->saidx.proto != proto) {
5133 ipseclog((LOG_DEBUG, "%s: protocol mismatched "
5134 "(DB=%u param=%u)\n", __func__,
5135 sav->sah->saidx.proto, proto));
5136 return key_senderror(so, m, EINVAL);
5138 #ifdef IPSEC_DOSEQCHECK
5139 if (sav->spi != sa0->sadb_sa_spi) {
5140 ipseclog((LOG_DEBUG, "%s: SPI mismatched (DB:%u param:%u)\n",
5142 (u_int32_t)ntohl(sav->spi),
5143 (u_int32_t)ntohl(sa0->sadb_sa_spi)));
5144 return key_senderror(so, m, EINVAL);
5147 if (sav->pid != mhp->msg->sadb_msg_pid) {
5148 ipseclog((LOG_DEBUG, "%s: pid mismatched (DB:%u param:%u)\n",
5149 __func__, sav->pid, mhp->msg->sadb_msg_pid));
5150 return key_senderror(so, m, EINVAL);
5153 /* copy sav values */
5154 error = key_setsaval(sav, m, mhp);
5157 return key_senderror(so, m, error);
5160 /* check SA values to be mature. */
5161 if ((mhp->msg->sadb_msg_errno = key_mature(sav)) != 0) {
5163 return key_senderror(so, m, 0);
5168 * Handle more NAT-T info if present,
5169 * now that we have a sav to fill.
5172 sav->natt_type = type->sadb_x_nat_t_type_type;
5175 KEY_PORTTOSADDR(&sav->sah->saidx.src,
5176 sport->sadb_x_nat_t_port_port);
5178 KEY_PORTTOSADDR(&sav->sah->saidx.dst,
5179 dport->sadb_x_nat_t_port_port);
5183 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5184 * We should actually check for a minimum MTU here, if we
5185 * want to support it in ip_output.
5188 sav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5195 /* set msg buf from mhp */
5196 n = key_getmsgbuf_x1(m, mhp);
5198 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5199 return key_senderror(so, m, ENOBUFS);
5203 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5208 * search SAD with sequence for a SA which state is SADB_SASTATE_LARVAL.
5209 * only called by key_update().
5212 * others : found, pointer to a SA.
5214 #ifdef IPSEC_DOSEQCHECK
5215 static struct secasvar *
5216 key_getsavbyseq(sah, seq)
5217 struct secashead *sah;
5220 struct secasvar *sav;
5223 state = SADB_SASTATE_LARVAL;
5225 /* search SAD with sequence number ? */
5226 LIST_FOREACH(sav, &sah->savtree[state], chain) {
5228 KEY_CHKSASTATE(state, sav->state, __func__);
5230 if (sav->seq == seq) {
5232 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
5233 printf("DP %s cause refcnt++:%d SA:%p\n",
5234 __func__, sav->refcnt, sav));
5244 * SADB_ADD processing
5245 * add an entry to SA database, when received
5246 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5247 * key(AE), (identity(SD),) (sensitivity)>
5250 * <base, SA, (SA2), (lifetime(HSC),) address(SD), (address(P),)
5251 * (identity(SD),) (sensitivity)>
5254 * IGNORE identity and sensitivity messages.
5256 * m will always be freed.
5262 const struct sadb_msghdr *mhp;
5264 struct sadb_sa *sa0;
5265 struct sadb_address *src0, *dst0;
5267 struct sadb_x_nat_t_type *type;
5268 struct sadb_address *iaddr, *raddr;
5269 struct sadb_x_nat_t_frag *frag;
5271 struct secasindex saidx;
5272 struct secashead *newsah;
5273 struct secasvar *newsav;
5279 IPSEC_ASSERT(so != NULL, ("null socket"));
5280 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5281 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5282 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5284 /* map satype to proto */
5285 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5286 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5288 return key_senderror(so, m, EINVAL);
5291 if (mhp->ext[SADB_EXT_SA] == NULL ||
5292 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5293 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
5294 (mhp->msg->sadb_msg_satype == SADB_SATYPE_ESP &&
5295 mhp->ext[SADB_EXT_KEY_ENCRYPT] == NULL) ||
5296 (mhp->msg->sadb_msg_satype == SADB_SATYPE_AH &&
5297 mhp->ext[SADB_EXT_KEY_AUTH] == NULL) ||
5298 (mhp->ext[SADB_EXT_LIFETIME_HARD] != NULL &&
5299 mhp->ext[SADB_EXT_LIFETIME_SOFT] == NULL) ||
5300 (mhp->ext[SADB_EXT_LIFETIME_HARD] == NULL &&
5301 mhp->ext[SADB_EXT_LIFETIME_SOFT] != NULL)) {
5302 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5304 return key_senderror(so, m, EINVAL);
5306 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5307 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5308 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5310 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5312 return key_senderror(so, m, EINVAL);
5314 if (mhp->ext[SADB_X_EXT_SA2] != NULL) {
5315 mode = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_mode;
5316 reqid = ((struct sadb_x_sa2 *)mhp->ext[SADB_X_EXT_SA2])->sadb_x_sa2_reqid;
5318 mode = IPSEC_MODE_ANY;
5322 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5323 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5324 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5326 /* XXX boundary check against sa_len */
5327 KEY_SETSECASIDX(proto, mode, reqid, src0 + 1, dst0 + 1, &saidx);
5330 * Make sure the port numbers are zero.
5331 * In case of NAT-T we will update them later if needed.
5333 KEY_PORTTOSADDR(&saidx.src, 0);
5334 KEY_PORTTOSADDR(&saidx.dst, 0);
5338 * Handle NAT-T info if present.
5340 if (mhp->ext[SADB_X_EXT_NAT_T_TYPE] != NULL &&
5341 mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5342 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5343 struct sadb_x_nat_t_port *sport, *dport;
5345 if (mhp->extlen[SADB_X_EXT_NAT_T_TYPE] < sizeof(*type) ||
5346 mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5347 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5348 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5350 return key_senderror(so, m, EINVAL);
5353 type = (struct sadb_x_nat_t_type *)
5354 mhp->ext[SADB_X_EXT_NAT_T_TYPE];
5355 sport = (struct sadb_x_nat_t_port *)
5356 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5357 dport = (struct sadb_x_nat_t_port *)
5358 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5361 KEY_PORTTOSADDR(&saidx.src,
5362 sport->sadb_x_nat_t_port_port);
5364 KEY_PORTTOSADDR(&saidx.dst,
5365 dport->sadb_x_nat_t_port_port);
5369 if (mhp->ext[SADB_X_EXT_NAT_T_OAI] != NULL &&
5370 mhp->ext[SADB_X_EXT_NAT_T_OAR] != NULL) {
5371 if (mhp->extlen[SADB_X_EXT_NAT_T_OAI] < sizeof(*iaddr) ||
5372 mhp->extlen[SADB_X_EXT_NAT_T_OAR] < sizeof(*raddr)) {
5373 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5375 return key_senderror(so, m, EINVAL);
5377 iaddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAI];
5378 raddr = (struct sadb_address *)mhp->ext[SADB_X_EXT_NAT_T_OAR];
5379 ipseclog((LOG_DEBUG, "%s: NAT-T OAi/r present\n", __func__));
5381 iaddr = raddr = NULL;
5383 if (mhp->ext[SADB_X_EXT_NAT_T_FRAG] != NULL) {
5384 if (mhp->extlen[SADB_X_EXT_NAT_T_FRAG] < sizeof(*frag)) {
5385 ipseclog((LOG_DEBUG, "%s: invalid message\n",
5387 return key_senderror(so, m, EINVAL);
5389 frag = (struct sadb_x_nat_t_frag *)
5390 mhp->ext[SADB_X_EXT_NAT_T_FRAG];
5396 /* get a SA header */
5397 if ((newsah = key_getsah(&saidx)) == NULL) {
5398 /* create a new SA header */
5399 if ((newsah = key_newsah(&saidx)) == NULL) {
5400 ipseclog((LOG_DEBUG, "%s: No more memory.\n",__func__));
5401 return key_senderror(so, m, ENOBUFS);
5405 /* set spidx if there */
5407 error = key_setident(newsah, m, mhp);
5409 return key_senderror(so, m, error);
5412 /* create new SA entry. */
5413 /* We can create new SA only if SPI is differenct. */
5415 newsav = key_getsavbyspi(newsah, sa0->sadb_sa_spi);
5417 if (newsav != NULL) {
5418 ipseclog((LOG_DEBUG, "%s: SA already exists.\n", __func__));
5419 return key_senderror(so, m, EEXIST);
5421 newsav = KEY_NEWSAV(m, mhp, newsah, &error);
5422 if (newsav == NULL) {
5423 return key_senderror(so, m, error);
5426 /* check SA values to be mature. */
5427 if ((error = key_mature(newsav)) != 0) {
5428 KEY_FREESAV(&newsav);
5429 return key_senderror(so, m, error);
5434 * Handle more NAT-T info if present,
5435 * now that we have a sav to fill.
5438 newsav->natt_type = type->sadb_x_nat_t_type_type;
5442 * In case SADB_X_EXT_NAT_T_FRAG was not given, leave it at 0.
5443 * We should actually check for a minimum MTU here, if we
5444 * want to support it in ip_output.
5447 newsav->natt_esp_frag_len = frag->sadb_x_nat_t_frag_fraglen;
5452 * don't call key_freesav() here, as we would like to keep the SA
5453 * in the database on success.
5459 /* set msg buf from mhp */
5460 n = key_getmsgbuf_x1(m, mhp);
5462 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5463 return key_senderror(so, m, ENOBUFS);
5467 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5473 key_setident(sah, m, mhp)
5474 struct secashead *sah;
5476 const struct sadb_msghdr *mhp;
5478 const struct sadb_ident *idsrc, *iddst;
5479 int idsrclen, iddstlen;
5481 IPSEC_ASSERT(sah != NULL, ("null secashead"));
5482 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5483 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5484 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5486 /* don't make buffer if not there */
5487 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL &&
5488 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5494 if (mhp->ext[SADB_EXT_IDENTITY_SRC] == NULL ||
5495 mhp->ext[SADB_EXT_IDENTITY_DST] == NULL) {
5496 ipseclog((LOG_DEBUG, "%s: invalid identity.\n", __func__));
5500 idsrc = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_SRC];
5501 iddst = (const struct sadb_ident *)mhp->ext[SADB_EXT_IDENTITY_DST];
5502 idsrclen = mhp->extlen[SADB_EXT_IDENTITY_SRC];
5503 iddstlen = mhp->extlen[SADB_EXT_IDENTITY_DST];
5505 /* validity check */
5506 if (idsrc->sadb_ident_type != iddst->sadb_ident_type) {
5507 ipseclog((LOG_DEBUG, "%s: ident type mismatch.\n", __func__));
5511 switch (idsrc->sadb_ident_type) {
5512 case SADB_IDENTTYPE_PREFIX:
5513 case SADB_IDENTTYPE_FQDN:
5514 case SADB_IDENTTYPE_USERFQDN:
5516 /* XXX do nothing */
5522 /* make structure */
5523 sah->idents = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5524 if (sah->idents == NULL) {
5525 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5528 sah->identd = malloc(sizeof(struct secident), M_IPSEC_MISC, M_NOWAIT);
5529 if (sah->identd == NULL) {
5530 free(sah->idents, M_IPSEC_MISC);
5532 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
5535 sah->idents->type = idsrc->sadb_ident_type;
5536 sah->idents->id = idsrc->sadb_ident_id;
5538 sah->identd->type = iddst->sadb_ident_type;
5539 sah->identd->id = iddst->sadb_ident_id;
5545 * m will not be freed on return.
5546 * it is caller's responsibility to free the result.
5548 static struct mbuf *
5549 key_getmsgbuf_x1(m, mhp)
5551 const struct sadb_msghdr *mhp;
5555 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5556 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5557 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5559 /* create new sadb_msg to reply. */
5560 n = key_gather_mbuf(m, mhp, 1, 9, SADB_EXT_RESERVED,
5561 SADB_EXT_SA, SADB_X_EXT_SA2,
5562 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST,
5563 SADB_EXT_LIFETIME_HARD, SADB_EXT_LIFETIME_SOFT,
5564 SADB_EXT_IDENTITY_SRC, SADB_EXT_IDENTITY_DST);
5568 if (n->m_len < sizeof(struct sadb_msg)) {
5569 n = m_pullup(n, sizeof(struct sadb_msg));
5573 mtod(n, struct sadb_msg *)->sadb_msg_errno = 0;
5574 mtod(n, struct sadb_msg *)->sadb_msg_len =
5575 PFKEY_UNIT64(n->m_pkthdr.len);
5580 static int key_delete_all __P((struct socket *, struct mbuf *,
5581 const struct sadb_msghdr *, u_int16_t));
5584 * SADB_DELETE processing
5586 * <base, SA(*), address(SD)>
5587 * from the ikmpd, and set SADB_SASTATE_DEAD,
5589 * <base, SA(*), address(SD)>
5592 * m will always be freed.
5595 key_delete(so, m, mhp)
5598 const struct sadb_msghdr *mhp;
5600 struct sadb_sa *sa0;
5601 struct sadb_address *src0, *dst0;
5602 struct secasindex saidx;
5603 struct secashead *sah;
5604 struct secasvar *sav = NULL;
5607 IPSEC_ASSERT(so != NULL, ("null socket"));
5608 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5609 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5610 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5612 /* map satype to proto */
5613 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5614 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5616 return key_senderror(so, m, EINVAL);
5619 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5620 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5621 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5623 return key_senderror(so, m, EINVAL);
5626 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5627 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5628 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5630 return key_senderror(so, m, EINVAL);
5633 if (mhp->ext[SADB_EXT_SA] == NULL) {
5635 * Caller wants us to delete all non-LARVAL SAs
5636 * that match the src/dst. This is used during
5637 * IKE INITIAL-CONTACT.
5639 ipseclog((LOG_DEBUG, "%s: doing delete all.\n", __func__));
5640 return key_delete_all(so, m, mhp, proto);
5641 } else if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa)) {
5642 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5644 return key_senderror(so, m, EINVAL);
5647 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5648 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5649 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5651 /* XXX boundary check against sa_len */
5652 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5655 * Make sure the port numbers are zero.
5656 * In case of NAT-T we will update them later if needed.
5658 KEY_PORTTOSADDR(&saidx.src, 0);
5659 KEY_PORTTOSADDR(&saidx.dst, 0);
5663 * Handle NAT-T info if present.
5665 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5666 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5667 struct sadb_x_nat_t_port *sport, *dport;
5669 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5670 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5671 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5673 return key_senderror(so, m, EINVAL);
5676 sport = (struct sadb_x_nat_t_port *)
5677 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5678 dport = (struct sadb_x_nat_t_port *)
5679 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5682 KEY_PORTTOSADDR(&saidx.src,
5683 sport->sadb_x_nat_t_port_port);
5685 KEY_PORTTOSADDR(&saidx.dst,
5686 dport->sadb_x_nat_t_port_port);
5690 /* get a SA header */
5692 LIST_FOREACH(sah, &V_sahtree, chain) {
5693 if (sah->state == SADB_SASTATE_DEAD)
5695 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5698 /* get a SA with SPI. */
5699 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5705 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5706 return key_senderror(so, m, ENOENT);
5709 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5715 struct sadb_msg *newmsg;
5717 /* create new sadb_msg to reply. */
5718 /* XXX-BZ NAT-T extensions? */
5719 n = key_gather_mbuf(m, mhp, 1, 4, SADB_EXT_RESERVED,
5720 SADB_EXT_SA, SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5722 return key_senderror(so, m, ENOBUFS);
5724 if (n->m_len < sizeof(struct sadb_msg)) {
5725 n = m_pullup(n, sizeof(struct sadb_msg));
5727 return key_senderror(so, m, ENOBUFS);
5729 newmsg = mtod(n, struct sadb_msg *);
5730 newmsg->sadb_msg_errno = 0;
5731 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5734 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5739 * delete all SAs for src/dst. Called from key_delete().
5742 key_delete_all(struct socket *so, struct mbuf *m, const struct sadb_msghdr *mhp,
5745 struct sadb_address *src0, *dst0;
5746 struct secasindex saidx;
5747 struct secashead *sah;
5748 struct secasvar *sav, *nextsav;
5749 u_int stateidx, state;
5751 src0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_SRC]);
5752 dst0 = (struct sadb_address *)(mhp->ext[SADB_EXT_ADDRESS_DST]);
5754 /* XXX boundary check against sa_len */
5755 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5758 * Make sure the port numbers are zero.
5759 * In case of NAT-T we will update them later if needed.
5761 KEY_PORTTOSADDR(&saidx.src, 0);
5762 KEY_PORTTOSADDR(&saidx.dst, 0);
5766 * Handle NAT-T info if present.
5769 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5770 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5771 struct sadb_x_nat_t_port *sport, *dport;
5773 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5774 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5775 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5777 return key_senderror(so, m, EINVAL);
5780 sport = (struct sadb_x_nat_t_port *)
5781 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5782 dport = (struct sadb_x_nat_t_port *)
5783 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5786 KEY_PORTTOSADDR(&saidx.src,
5787 sport->sadb_x_nat_t_port_port);
5789 KEY_PORTTOSADDR(&saidx.dst,
5790 dport->sadb_x_nat_t_port_port);
5795 LIST_FOREACH(sah, &V_sahtree, chain) {
5796 if (sah->state == SADB_SASTATE_DEAD)
5798 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5801 /* Delete all non-LARVAL SAs. */
5803 stateidx < _ARRAYLEN(saorder_state_alive);
5805 state = saorder_state_alive[stateidx];
5806 if (state == SADB_SASTATE_LARVAL)
5808 for (sav = LIST_FIRST(&sah->savtree[state]);
5809 sav != NULL; sav = nextsav) {
5810 nextsav = LIST_NEXT(sav, chain);
5812 if (sav->state != state) {
5813 ipseclog((LOG_DEBUG, "%s: invalid "
5814 "sav->state (queue %d SA %d)\n",
5815 __func__, state, sav->state));
5819 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
5827 struct sadb_msg *newmsg;
5829 /* create new sadb_msg to reply. */
5830 /* XXX-BZ NAT-T extensions? */
5831 n = key_gather_mbuf(m, mhp, 1, 3, SADB_EXT_RESERVED,
5832 SADB_EXT_ADDRESS_SRC, SADB_EXT_ADDRESS_DST);
5834 return key_senderror(so, m, ENOBUFS);
5836 if (n->m_len < sizeof(struct sadb_msg)) {
5837 n = m_pullup(n, sizeof(struct sadb_msg));
5839 return key_senderror(so, m, ENOBUFS);
5841 newmsg = mtod(n, struct sadb_msg *);
5842 newmsg->sadb_msg_errno = 0;
5843 newmsg->sadb_msg_len = PFKEY_UNIT64(n->m_pkthdr.len);
5846 return key_sendup_mbuf(so, n, KEY_SENDUP_ALL);
5851 * SADB_GET processing
5853 * <base, SA(*), address(SD)>
5854 * from the ikmpd, and get a SP and a SA to respond,
5856 * <base, SA, (lifetime(HSC),) address(SD), (address(P),) key(AE),
5857 * (identity(SD),) (sensitivity)>
5860 * m will always be freed.
5866 const struct sadb_msghdr *mhp;
5868 struct sadb_sa *sa0;
5869 struct sadb_address *src0, *dst0;
5870 struct secasindex saidx;
5871 struct secashead *sah;
5872 struct secasvar *sav = NULL;
5875 IPSEC_ASSERT(so != NULL, ("null socket"));
5876 IPSEC_ASSERT(m != NULL, ("null mbuf"));
5877 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
5878 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
5880 /* map satype to proto */
5881 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
5882 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
5884 return key_senderror(so, m, EINVAL);
5887 if (mhp->ext[SADB_EXT_SA] == NULL ||
5888 mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
5889 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL) {
5890 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5892 return key_senderror(so, m, EINVAL);
5894 if (mhp->extlen[SADB_EXT_SA] < sizeof(struct sadb_sa) ||
5895 mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
5896 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address)) {
5897 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
5899 return key_senderror(so, m, EINVAL);
5902 sa0 = (struct sadb_sa *)mhp->ext[SADB_EXT_SA];
5903 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
5904 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
5906 /* XXX boundary check against sa_len */
5907 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
5910 * Make sure the port numbers are zero.
5911 * In case of NAT-T we will update them later if needed.
5913 KEY_PORTTOSADDR(&saidx.src, 0);
5914 KEY_PORTTOSADDR(&saidx.dst, 0);
5918 * Handle NAT-T info if present.
5921 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
5922 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
5923 struct sadb_x_nat_t_port *sport, *dport;
5925 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
5926 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
5927 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
5929 return key_senderror(so, m, EINVAL);
5932 sport = (struct sadb_x_nat_t_port *)
5933 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
5934 dport = (struct sadb_x_nat_t_port *)
5935 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
5938 KEY_PORTTOSADDR(&saidx.src,
5939 sport->sadb_x_nat_t_port_port);
5941 KEY_PORTTOSADDR(&saidx.dst,
5942 dport->sadb_x_nat_t_port_port);
5946 /* get a SA header */
5948 LIST_FOREACH(sah, &V_sahtree, chain) {
5949 if (sah->state == SADB_SASTATE_DEAD)
5951 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_HEAD) == 0)
5954 /* get a SA with SPI. */
5955 sav = key_getsavbyspi(sah, sa0->sadb_sa_spi);
5961 ipseclog((LOG_DEBUG, "%s: no SA found.\n", __func__));
5962 return key_senderror(so, m, ENOENT);
5969 /* map proto to satype */
5970 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
5971 ipseclog((LOG_DEBUG, "%s: there was invalid proto in SAD.\n",
5973 return key_senderror(so, m, EINVAL);
5976 /* create new sadb_msg to reply. */
5977 n = key_setdumpsa(sav, SADB_GET, satype, mhp->msg->sadb_msg_seq,
5978 mhp->msg->sadb_msg_pid);
5980 return key_senderror(so, m, ENOBUFS);
5983 return key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
5987 /* XXX make it sysctl-configurable? */
5989 key_getcomb_setlifetime(comb)
5990 struct sadb_comb *comb;
5993 comb->sadb_comb_soft_allocations = 1;
5994 comb->sadb_comb_hard_allocations = 1;
5995 comb->sadb_comb_soft_bytes = 0;
5996 comb->sadb_comb_hard_bytes = 0;
5997 comb->sadb_comb_hard_addtime = 86400; /* 1 day */
5998 comb->sadb_comb_soft_addtime = comb->sadb_comb_soft_addtime * 80 / 100;
5999 comb->sadb_comb_soft_usetime = 28800; /* 8 hours */
6000 comb->sadb_comb_hard_usetime = comb->sadb_comb_hard_usetime * 80 / 100;
6004 * XXX reorder combinations by preference
6005 * XXX no idea if the user wants ESP authentication or not
6007 static struct mbuf *
6010 struct sadb_comb *comb;
6011 struct enc_xform *algo;
6012 struct mbuf *result = NULL, *m, *n;
6016 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6019 for (i = 1; i <= SADB_EALG_MAX; i++) {
6020 algo = esp_algorithm_lookup(i);
6024 /* discard algorithms with key size smaller than system min */
6025 if (_BITS(algo->maxkey) < V_ipsec_esp_keymin)
6027 if (_BITS(algo->minkey) < V_ipsec_esp_keymin)
6028 encmin = V_ipsec_esp_keymin;
6030 encmin = _BITS(algo->minkey);
6032 if (V_ipsec_esp_auth)
6033 m = key_getcomb_ah();
6035 IPSEC_ASSERT(l <= MLEN,
6036 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6037 MGET(m, M_DONTWAIT, MT_DATA);
6042 bzero(mtod(m, caddr_t), m->m_len);
6049 for (n = m; n; n = n->m_next)
6051 IPSEC_ASSERT((totlen % l) == 0, ("totlen=%u, l=%u", totlen, l));
6053 for (off = 0; off < totlen; off += l) {
6054 n = m_pulldown(m, off, l, &o);
6056 /* m is already freed */
6059 comb = (struct sadb_comb *)(mtod(n, caddr_t) + o);
6060 bzero(comb, sizeof(*comb));
6061 key_getcomb_setlifetime(comb);
6062 comb->sadb_comb_encrypt = i;
6063 comb->sadb_comb_encrypt_minbits = encmin;
6064 comb->sadb_comb_encrypt_maxbits = _BITS(algo->maxkey);
6083 const struct auth_hash *ah,
6089 *min = *max = ah->keysize;
6090 if (ah->keysize == 0) {
6092 * Transform takes arbitrary key size but algorithm
6093 * key size is restricted. Enforce this here.
6096 case SADB_X_AALG_MD5: *min = *max = 16; break;
6097 case SADB_X_AALG_SHA: *min = *max = 20; break;
6098 case SADB_X_AALG_NULL: *min = 1; *max = 256; break;
6100 DPRINTF(("%s: unknown AH algorithm %u\n",
6108 * XXX reorder combinations by preference
6110 static struct mbuf *
6113 struct sadb_comb *comb;
6114 struct auth_hash *algo;
6116 u_int16_t minkeysize, maxkeysize;
6118 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6121 for (i = 1; i <= SADB_AALG_MAX; i++) {
6123 /* we prefer HMAC algorithms, not old algorithms */
6124 if (i != SADB_AALG_SHA1HMAC && i != SADB_AALG_MD5HMAC)
6127 algo = ah_algorithm_lookup(i);
6130 key_getsizes_ah(algo, i, &minkeysize, &maxkeysize);
6131 /* discard algorithms with key size smaller than system min */
6132 if (_BITS(minkeysize) < V_ipsec_ah_keymin)
6136 IPSEC_ASSERT(l <= MLEN,
6137 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6138 MGET(m, M_DONTWAIT, MT_DATA);
6145 M_PREPEND(m, l, M_DONTWAIT);
6149 comb = mtod(m, struct sadb_comb *);
6150 bzero(comb, sizeof(*comb));
6151 key_getcomb_setlifetime(comb);
6152 comb->sadb_comb_auth = i;
6153 comb->sadb_comb_auth_minbits = _BITS(minkeysize);
6154 comb->sadb_comb_auth_maxbits = _BITS(maxkeysize);
6161 * not really an official behavior. discussed in pf_key@inner.net in Sep2000.
6162 * XXX reorder combinations by preference
6164 static struct mbuf *
6165 key_getcomb_ipcomp()
6167 struct sadb_comb *comb;
6168 struct comp_algo *algo;
6171 const int l = PFKEY_ALIGN8(sizeof(struct sadb_comb));
6174 for (i = 1; i <= SADB_X_CALG_MAX; i++) {
6175 algo = ipcomp_algorithm_lookup(i);
6180 IPSEC_ASSERT(l <= MLEN,
6181 ("l=%u > MLEN=%lu", l, (u_long) MLEN));
6182 MGET(m, M_DONTWAIT, MT_DATA);
6189 M_PREPEND(m, l, M_DONTWAIT);
6193 comb = mtod(m, struct sadb_comb *);
6194 bzero(comb, sizeof(*comb));
6195 key_getcomb_setlifetime(comb);
6196 comb->sadb_comb_encrypt = i;
6197 /* what should we set into sadb_comb_*_{min,max}bits? */
6204 * XXX no way to pass mode (transport/tunnel) to userland
6205 * XXX replay checking?
6206 * XXX sysctl interface to ipsec_{ah,esp}_keymin
6208 static struct mbuf *
6210 const struct secasindex *saidx;
6212 struct sadb_prop *prop;
6214 const int l = PFKEY_ALIGN8(sizeof(struct sadb_prop));
6217 switch (saidx->proto) {
6219 m = key_getcomb_esp();
6222 m = key_getcomb_ah();
6224 case IPPROTO_IPCOMP:
6225 m = key_getcomb_ipcomp();
6233 M_PREPEND(m, l, M_DONTWAIT);
6238 for (n = m; n; n = n->m_next)
6241 prop = mtod(m, struct sadb_prop *);
6242 bzero(prop, sizeof(*prop));
6243 prop->sadb_prop_len = PFKEY_UNIT64(totlen);
6244 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
6245 prop->sadb_prop_replay = 32; /* XXX */
6251 * SADB_ACQUIRE processing called by key_checkrequest() and key_acquire2().
6253 * <base, SA, address(SD), (address(P)), x_policy,
6254 * (identity(SD),) (sensitivity,) proposal>
6255 * to KMD, and expect to receive
6256 * <base> with SADB_ACQUIRE if error occured,
6258 * <base, src address, dst address, (SPI range)> with SADB_GETSPI
6259 * from KMD by PF_KEY.
6261 * XXX x_policy is outside of RFC2367 (KAME extension).
6262 * XXX sensitivity is not supported.
6263 * XXX for ipcomp, RFC2367 does not define how to fill in proposal.
6264 * see comment for key_getcomb_ipcomp().
6268 * others: error number
6271 key_acquire(const struct secasindex *saidx, struct secpolicy *sp)
6273 struct mbuf *result = NULL, *m;
6274 struct secacq *newacq;
6279 IPSEC_ASSERT(saidx != NULL, ("null saidx"));
6280 satype = key_proto2satype(saidx->proto);
6281 IPSEC_ASSERT(satype != 0, ("null satype, protocol %u", saidx->proto));
6284 * We never do anything about acquirng SA. There is anather
6285 * solution that kernel blocks to send SADB_ACQUIRE message until
6286 * getting something message from IKEd. In later case, to be
6287 * managed with ACQUIRING list.
6289 /* Get an entry to check whether sending message or not. */
6290 if ((newacq = key_getacq(saidx)) != NULL) {
6291 if (V_key_blockacq_count < newacq->count) {
6292 /* reset counter and do send message. */
6295 /* increment counter and do nothing. */
6300 /* make new entry for blocking to send SADB_ACQUIRE. */
6301 if ((newacq = key_newacq(saidx)) == NULL)
6307 m = key_setsadbmsg(SADB_ACQUIRE, 0, satype, seq, 0, 0);
6315 * No SADB_X_EXT_NAT_T_* here: we do not know
6316 * anything related to NAT-T at this time.
6319 /* set sadb_address for saidx's. */
6320 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6321 &saidx->src.sa, FULLMASK, IPSEC_ULPROTO_ANY);
6328 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6329 &saidx->dst.sa, FULLMASK, IPSEC_ULPROTO_ANY);
6336 /* XXX proxy address (optional) */
6338 /* set sadb_x_policy */
6340 m = key_setsadbxpolicy(sp->policy, sp->spidx.dir, sp->id);
6348 /* XXX identity (optional) */
6350 if (idexttype && fqdn) {
6351 /* create identity extension (FQDN) */
6352 struct sadb_ident *id;
6355 fqdnlen = strlen(fqdn) + 1; /* +1 for terminating-NUL */
6356 id = (struct sadb_ident *)p;
6357 bzero(id, sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6358 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(fqdnlen));
6359 id->sadb_ident_exttype = idexttype;
6360 id->sadb_ident_type = SADB_IDENTTYPE_FQDN;
6361 bcopy(fqdn, id + 1, fqdnlen);
6362 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(fqdnlen);
6366 /* create identity extension (USERFQDN) */
6367 struct sadb_ident *id;
6371 /* +1 for terminating-NUL */
6372 userfqdnlen = strlen(userfqdn) + 1;
6375 id = (struct sadb_ident *)p;
6376 bzero(id, sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6377 id->sadb_ident_len = PFKEY_UNIT64(sizeof(*id) + PFKEY_ALIGN8(userfqdnlen));
6378 id->sadb_ident_exttype = idexttype;
6379 id->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
6380 /* XXX is it correct? */
6381 if (curproc && curproc->p_cred)
6382 id->sadb_ident_id = curproc->p_cred->p_ruid;
6383 if (userfqdn && userfqdnlen)
6384 bcopy(userfqdn, id + 1, userfqdnlen);
6385 p += sizeof(struct sadb_ident) + PFKEY_ALIGN8(userfqdnlen);
6389 /* XXX sensitivity (optional) */
6391 /* create proposal/combination extension */
6392 m = key_getprop(saidx);
6395 * spec conformant: always attach proposal/combination extension,
6396 * the problem is that we have no way to attach it for ipcomp,
6397 * due to the way sadb_comb is declared in RFC2367.
6406 * outside of spec; make proposal/combination extension optional.
6412 if ((result->m_flags & M_PKTHDR) == 0) {
6417 if (result->m_len < sizeof(struct sadb_msg)) {
6418 result = m_pullup(result, sizeof(struct sadb_msg));
6419 if (result == NULL) {
6425 result->m_pkthdr.len = 0;
6426 for (m = result; m; m = m->m_next)
6427 result->m_pkthdr.len += m->m_len;
6429 mtod(result, struct sadb_msg *)->sadb_msg_len =
6430 PFKEY_UNIT64(result->m_pkthdr.len);
6432 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
6440 static struct secacq *
6441 key_newacq(const struct secasindex *saidx)
6443 struct secacq *newacq;
6446 newacq = malloc(sizeof(struct secacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6447 if (newacq == NULL) {
6448 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6453 bcopy(saidx, &newacq->saidx, sizeof(newacq->saidx));
6454 newacq->seq = (V_acq_seq == ~0 ? 1 : ++V_acq_seq);
6455 newacq->created = time_second;
6458 /* add to acqtree */
6460 LIST_INSERT_HEAD(&V_acqtree, newacq, chain);
6466 static struct secacq *
6467 key_getacq(const struct secasindex *saidx)
6472 LIST_FOREACH(acq, &V_acqtree, chain) {
6473 if (key_cmpsaidx(saidx, &acq->saidx, CMP_EXACTLY))
6481 static struct secacq *
6482 key_getacqbyseq(seq)
6488 LIST_FOREACH(acq, &V_acqtree, chain) {
6489 if (acq->seq == seq)
6497 static struct secspacq *
6499 struct secpolicyindex *spidx;
6501 struct secspacq *acq;
6504 acq = malloc(sizeof(struct secspacq), M_IPSEC_SAQ, M_NOWAIT|M_ZERO);
6506 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6511 bcopy(spidx, &acq->spidx, sizeof(acq->spidx));
6512 acq->created = time_second;
6515 /* add to spacqtree */
6517 LIST_INSERT_HEAD(&V_spacqtree, acq, chain);
6523 static struct secspacq *
6525 struct secpolicyindex *spidx;
6527 struct secspacq *acq;
6530 LIST_FOREACH(acq, &V_spacqtree, chain) {
6531 if (key_cmpspidx_exactly(spidx, &acq->spidx)) {
6532 /* NB: return holding spacq_lock */
6542 * SADB_ACQUIRE processing,
6543 * in first situation, is receiving
6545 * from the ikmpd, and clear sequence of its secasvar entry.
6547 * In second situation, is receiving
6548 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6549 * from a user land process, and return
6550 * <base, address(SD), (address(P),) (identity(SD),) (sensitivity,) proposal>
6553 * m will always be freed.
6556 key_acquire2(so, m, mhp)
6559 const struct sadb_msghdr *mhp;
6561 const struct sadb_address *src0, *dst0;
6562 struct secasindex saidx;
6563 struct secashead *sah;
6567 IPSEC_ASSERT(so != NULL, ("null socket"));
6568 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6569 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6570 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6573 * Error message from KMd.
6574 * We assume that if error was occured in IKEd, the length of PFKEY
6575 * message is equal to the size of sadb_msg structure.
6576 * We do not raise error even if error occured in this function.
6578 if (mhp->msg->sadb_msg_len == PFKEY_UNIT64(sizeof(struct sadb_msg))) {
6581 /* check sequence number */
6582 if (mhp->msg->sadb_msg_seq == 0) {
6583 ipseclog((LOG_DEBUG, "%s: must specify sequence "
6584 "number.\n", __func__));
6589 if ((acq = key_getacqbyseq(mhp->msg->sadb_msg_seq)) == NULL) {
6591 * the specified larval SA is already gone, or we got
6592 * a bogus sequence number. we can silently ignore it.
6598 /* reset acq counter in order to deletion by timehander. */
6599 acq->created = time_second;
6606 * This message is from user land.
6609 /* map satype to proto */
6610 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
6611 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
6613 return key_senderror(so, m, EINVAL);
6616 if (mhp->ext[SADB_EXT_ADDRESS_SRC] == NULL ||
6617 mhp->ext[SADB_EXT_ADDRESS_DST] == NULL ||
6618 mhp->ext[SADB_EXT_PROPOSAL] == NULL) {
6620 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6622 return key_senderror(so, m, EINVAL);
6624 if (mhp->extlen[SADB_EXT_ADDRESS_SRC] < sizeof(struct sadb_address) ||
6625 mhp->extlen[SADB_EXT_ADDRESS_DST] < sizeof(struct sadb_address) ||
6626 mhp->extlen[SADB_EXT_PROPOSAL] < sizeof(struct sadb_prop)) {
6628 ipseclog((LOG_DEBUG, "%s: invalid message is passed.\n",
6630 return key_senderror(so, m, EINVAL);
6633 src0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_SRC];
6634 dst0 = (struct sadb_address *)mhp->ext[SADB_EXT_ADDRESS_DST];
6636 /* XXX boundary check against sa_len */
6637 KEY_SETSECASIDX(proto, IPSEC_MODE_ANY, 0, src0 + 1, dst0 + 1, &saidx);
6640 * Make sure the port numbers are zero.
6641 * In case of NAT-T we will update them later if needed.
6643 KEY_PORTTOSADDR(&saidx.src, 0);
6644 KEY_PORTTOSADDR(&saidx.dst, 0);
6648 * Handle NAT-T info if present.
6651 if (mhp->ext[SADB_X_EXT_NAT_T_SPORT] != NULL &&
6652 mhp->ext[SADB_X_EXT_NAT_T_DPORT] != NULL) {
6653 struct sadb_x_nat_t_port *sport, *dport;
6655 if (mhp->extlen[SADB_X_EXT_NAT_T_SPORT] < sizeof(*sport) ||
6656 mhp->extlen[SADB_X_EXT_NAT_T_DPORT] < sizeof(*dport)) {
6657 ipseclog((LOG_DEBUG, "%s: invalid message.\n",
6659 return key_senderror(so, m, EINVAL);
6662 sport = (struct sadb_x_nat_t_port *)
6663 mhp->ext[SADB_X_EXT_NAT_T_SPORT];
6664 dport = (struct sadb_x_nat_t_port *)
6665 mhp->ext[SADB_X_EXT_NAT_T_DPORT];
6668 KEY_PORTTOSADDR(&saidx.src,
6669 sport->sadb_x_nat_t_port_port);
6671 KEY_PORTTOSADDR(&saidx.dst,
6672 dport->sadb_x_nat_t_port_port);
6676 /* get a SA index */
6678 LIST_FOREACH(sah, &V_sahtree, chain) {
6679 if (sah->state == SADB_SASTATE_DEAD)
6681 if (key_cmpsaidx(&sah->saidx, &saidx, CMP_MODE_REQID))
6686 ipseclog((LOG_DEBUG, "%s: a SA exists already.\n", __func__));
6687 return key_senderror(so, m, EEXIST);
6690 error = key_acquire(&saidx, NULL);
6692 ipseclog((LOG_DEBUG, "%s: error %d returned from key_acquire\n",
6693 __func__, mhp->msg->sadb_msg_errno));
6694 return key_senderror(so, m, error);
6697 return key_sendup_mbuf(so, m, KEY_SENDUP_REGISTERED);
6701 * SADB_REGISTER processing.
6702 * If SATYPE_UNSPEC has been passed as satype, only return sabd_supported.
6705 * from the ikmpd, and register a socket to send PF_KEY messages,
6709 * If socket is detached, must free from regnode.
6711 * m will always be freed.
6714 key_register(so, m, mhp)
6717 const struct sadb_msghdr *mhp;
6719 struct secreg *reg, *newreg = 0;
6721 IPSEC_ASSERT(so != NULL, ("null socket"));
6722 IPSEC_ASSERT(m != NULL, ("null mbuf"));
6723 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
6724 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
6726 /* check for invalid register message */
6727 if (mhp->msg->sadb_msg_satype >= sizeof(V_regtree)/sizeof(V_regtree[0]))
6728 return key_senderror(so, m, EINVAL);
6730 /* When SATYPE_UNSPEC is specified, only return sabd_supported. */
6731 if (mhp->msg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
6734 /* check whether existing or not */
6736 LIST_FOREACH(reg, &V_regtree[mhp->msg->sadb_msg_satype], chain) {
6737 if (reg->so == so) {
6739 ipseclog((LOG_DEBUG, "%s: socket exists already.\n",
6741 return key_senderror(so, m, EEXIST);
6745 /* create regnode */
6746 newreg = malloc(sizeof(struct secreg), M_IPSEC_SAR, M_NOWAIT|M_ZERO);
6747 if (newreg == NULL) {
6749 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
6750 return key_senderror(so, m, ENOBUFS);
6754 ((struct keycb *)sotorawcb(so))->kp_registered++;
6756 /* add regnode to regtree. */
6757 LIST_INSERT_HEAD(&V_regtree[mhp->msg->sadb_msg_satype], newreg, chain);
6763 struct sadb_msg *newmsg;
6764 struct sadb_supported *sup;
6765 u_int len, alen, elen;
6768 struct sadb_alg *alg;
6770 /* create new sadb_msg to reply. */
6772 for (i = 1; i <= SADB_AALG_MAX; i++) {
6773 if (ah_algorithm_lookup(i))
6774 alen += sizeof(struct sadb_alg);
6777 alen += sizeof(struct sadb_supported);
6779 for (i = 1; i <= SADB_EALG_MAX; i++) {
6780 if (esp_algorithm_lookup(i))
6781 elen += sizeof(struct sadb_alg);
6784 elen += sizeof(struct sadb_supported);
6786 len = sizeof(struct sadb_msg) + alen + elen;
6789 return key_senderror(so, m, ENOBUFS);
6791 MGETHDR(n, M_DONTWAIT, MT_DATA);
6793 MCLGET(n, M_DONTWAIT);
6794 if ((n->m_flags & M_EXT) == 0) {
6800 return key_senderror(so, m, ENOBUFS);
6802 n->m_pkthdr.len = n->m_len = len;
6806 m_copydata(m, 0, sizeof(struct sadb_msg), mtod(n, caddr_t) + off);
6807 newmsg = mtod(n, struct sadb_msg *);
6808 newmsg->sadb_msg_errno = 0;
6809 newmsg->sadb_msg_len = PFKEY_UNIT64(len);
6810 off += PFKEY_ALIGN8(sizeof(struct sadb_msg));
6812 /* for authentication algorithm */
6814 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6815 sup->sadb_supported_len = PFKEY_UNIT64(alen);
6816 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
6817 off += PFKEY_ALIGN8(sizeof(*sup));
6819 for (i = 1; i <= SADB_AALG_MAX; i++) {
6820 struct auth_hash *aalgo;
6821 u_int16_t minkeysize, maxkeysize;
6823 aalgo = ah_algorithm_lookup(i);
6826 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6827 alg->sadb_alg_id = i;
6828 alg->sadb_alg_ivlen = 0;
6829 key_getsizes_ah(aalgo, i, &minkeysize, &maxkeysize);
6830 alg->sadb_alg_minbits = _BITS(minkeysize);
6831 alg->sadb_alg_maxbits = _BITS(maxkeysize);
6832 off += PFKEY_ALIGN8(sizeof(*alg));
6836 /* for encryption algorithm */
6838 sup = (struct sadb_supported *)(mtod(n, caddr_t) + off);
6839 sup->sadb_supported_len = PFKEY_UNIT64(elen);
6840 sup->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
6841 off += PFKEY_ALIGN8(sizeof(*sup));
6843 for (i = 1; i <= SADB_EALG_MAX; i++) {
6844 struct enc_xform *ealgo;
6846 ealgo = esp_algorithm_lookup(i);
6849 alg = (struct sadb_alg *)(mtod(n, caddr_t) + off);
6850 alg->sadb_alg_id = i;
6851 alg->sadb_alg_ivlen = ealgo->blocksize;
6852 alg->sadb_alg_minbits = _BITS(ealgo->minkey);
6853 alg->sadb_alg_maxbits = _BITS(ealgo->maxkey);
6854 off += PFKEY_ALIGN8(sizeof(struct sadb_alg));
6858 IPSEC_ASSERT(off == len,
6859 ("length assumption failed (off %u len %u)", off, len));
6862 return key_sendup_mbuf(so, n, KEY_SENDUP_REGISTERED);
6867 * free secreg entry registered.
6868 * XXX: I want to do free a socket marked done SADB_RESIGER to socket.
6871 key_freereg(struct socket *so)
6876 IPSEC_ASSERT(so != NULL, ("NULL so"));
6879 * check whether existing or not.
6880 * check all type of SA, because there is a potential that
6881 * one socket is registered to multiple type of SA.
6884 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
6885 LIST_FOREACH(reg, &V_regtree[i], chain) {
6886 if (reg->so == so && __LIST_CHAINED(reg)) {
6887 LIST_REMOVE(reg, chain);
6888 free(reg, M_IPSEC_SAR);
6897 * SADB_EXPIRE processing
6899 * <base, SA, SA2, lifetime(C and one of HS), address(SD)>
6901 * NOTE: We send only soft lifetime extension.
6904 * others : error number
6907 key_expire(struct secasvar *sav)
6911 struct mbuf *result = NULL, *m;
6914 struct sadb_lifetime *lt;
6916 /* XXX: Why do we lock ? */
6917 s = splnet(); /*called from softclock()*/
6919 IPSEC_ASSERT (sav != NULL, ("null sav"));
6920 IPSEC_ASSERT (sav->sah != NULL, ("null sa header"));
6922 /* set msg header */
6923 satype = key_proto2satype(sav->sah->saidx.proto);
6924 IPSEC_ASSERT(satype != 0, ("invalid proto, satype %u", satype));
6925 m = key_setsadbmsg(SADB_EXPIRE, 0, satype, sav->seq, 0, sav->refcnt);
6932 /* create SA extension */
6933 m = key_setsadbsa(sav);
6940 /* create SA extension */
6941 m = key_setsadbxsa2(sav->sah->saidx.mode,
6942 sav->replay ? sav->replay->count : 0,
6943 sav->sah->saidx.reqid);
6950 /* create lifetime extension (current and soft) */
6951 len = PFKEY_ALIGN8(sizeof(*lt)) * 2;
6952 m = key_alloc_mbuf(len);
6953 if (!m || m->m_next) { /*XXX*/
6959 bzero(mtod(m, caddr_t), len);
6960 lt = mtod(m, struct sadb_lifetime *);
6961 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6962 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
6963 lt->sadb_lifetime_allocations = sav->lft_c->allocations;
6964 lt->sadb_lifetime_bytes = sav->lft_c->bytes;
6965 lt->sadb_lifetime_addtime = sav->lft_c->addtime;
6966 lt->sadb_lifetime_usetime = sav->lft_c->usetime;
6967 lt = (struct sadb_lifetime *)(mtod(m, caddr_t) + len / 2);
6968 lt->sadb_lifetime_len = PFKEY_UNIT64(sizeof(struct sadb_lifetime));
6969 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
6970 lt->sadb_lifetime_allocations = sav->lft_s->allocations;
6971 lt->sadb_lifetime_bytes = sav->lft_s->bytes;
6972 lt->sadb_lifetime_addtime = sav->lft_s->addtime;
6973 lt->sadb_lifetime_usetime = sav->lft_s->usetime;
6976 /* set sadb_address for source */
6977 m = key_setsadbaddr(SADB_EXT_ADDRESS_SRC,
6978 &sav->sah->saidx.src.sa,
6979 FULLMASK, IPSEC_ULPROTO_ANY);
6986 /* set sadb_address for destination */
6987 m = key_setsadbaddr(SADB_EXT_ADDRESS_DST,
6988 &sav->sah->saidx.dst.sa,
6989 FULLMASK, IPSEC_ULPROTO_ANY);
6997 * XXX-BZ Handle NAT-T extensions here.
7000 if ((result->m_flags & M_PKTHDR) == 0) {
7005 if (result->m_len < sizeof(struct sadb_msg)) {
7006 result = m_pullup(result, sizeof(struct sadb_msg));
7007 if (result == NULL) {
7013 result->m_pkthdr.len = 0;
7014 for (m = result; m; m = m->m_next)
7015 result->m_pkthdr.len += m->m_len;
7017 mtod(result, struct sadb_msg *)->sadb_msg_len =
7018 PFKEY_UNIT64(result->m_pkthdr.len);
7021 return key_sendup_mbuf(NULL, result, KEY_SENDUP_REGISTERED);
7031 * SADB_FLUSH processing
7034 * from the ikmpd, and free all entries in secastree.
7038 * NOTE: to do is only marking SADB_SASTATE_DEAD.
7040 * m will always be freed.
7043 key_flush(so, m, mhp)
7046 const struct sadb_msghdr *mhp;
7048 struct sadb_msg *newmsg;
7049 struct secashead *sah, *nextsah;
7050 struct secasvar *sav, *nextsav;
7055 IPSEC_ASSERT(so != NULL, ("null socket"));
7056 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7057 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7059 /* map satype to proto */
7060 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7061 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7063 return key_senderror(so, m, EINVAL);
7066 /* no SATYPE specified, i.e. flushing all SA. */
7068 for (sah = LIST_FIRST(&V_sahtree);
7071 nextsah = LIST_NEXT(sah, chain);
7073 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7074 && proto != sah->saidx.proto)
7078 stateidx < _ARRAYLEN(saorder_state_alive);
7080 state = saorder_state_any[stateidx];
7081 for (sav = LIST_FIRST(&sah->savtree[state]);
7085 nextsav = LIST_NEXT(sav, chain);
7087 key_sa_chgstate(sav, SADB_SASTATE_DEAD);
7092 sah->state = SADB_SASTATE_DEAD;
7096 if (m->m_len < sizeof(struct sadb_msg) ||
7097 sizeof(struct sadb_msg) > m->m_len + M_TRAILINGSPACE(m)) {
7098 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
7099 return key_senderror(so, m, ENOBUFS);
7105 m->m_pkthdr.len = m->m_len = sizeof(struct sadb_msg);
7106 newmsg = mtod(m, struct sadb_msg *);
7107 newmsg->sadb_msg_errno = 0;
7108 newmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
7110 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7114 * SADB_DUMP processing
7115 * dump all entries including status of DEAD in SAD.
7118 * from the ikmpd, and dump all secasvar leaves
7123 * m will always be freed.
7126 key_dump(so, m, mhp)
7129 const struct sadb_msghdr *mhp;
7131 struct secashead *sah;
7132 struct secasvar *sav;
7138 struct sadb_msg *newmsg;
7141 IPSEC_ASSERT(so != NULL, ("null socket"));
7142 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7143 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7144 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7146 /* map satype to proto */
7147 if ((proto = key_satype2proto(mhp->msg->sadb_msg_satype)) == 0) {
7148 ipseclog((LOG_DEBUG, "%s: invalid satype is passed.\n",
7150 return key_senderror(so, m, EINVAL);
7153 /* count sav entries to be sent to the userland. */
7156 LIST_FOREACH(sah, &V_sahtree, chain) {
7157 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7158 && proto != sah->saidx.proto)
7162 stateidx < _ARRAYLEN(saorder_state_any);
7164 state = saorder_state_any[stateidx];
7165 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7173 return key_senderror(so, m, ENOENT);
7176 /* send this to the userland, one at a time. */
7178 LIST_FOREACH(sah, &V_sahtree, chain) {
7179 if (mhp->msg->sadb_msg_satype != SADB_SATYPE_UNSPEC
7180 && proto != sah->saidx.proto)
7183 /* map proto to satype */
7184 if ((satype = key_proto2satype(sah->saidx.proto)) == 0) {
7186 ipseclog((LOG_DEBUG, "%s: there was invalid proto in "
7187 "SAD.\n", __func__));
7188 return key_senderror(so, m, EINVAL);
7192 stateidx < _ARRAYLEN(saorder_state_any);
7194 state = saorder_state_any[stateidx];
7195 LIST_FOREACH(sav, &sah->savtree[state], chain) {
7196 n = key_setdumpsa(sav, SADB_DUMP, satype,
7197 --cnt, mhp->msg->sadb_msg_pid);
7200 return key_senderror(so, m, ENOBUFS);
7202 key_sendup_mbuf(so, n, KEY_SENDUP_ONE);
7213 * SADB_X_PROMISC processing
7215 * m will always be freed.
7218 key_promisc(so, m, mhp)
7221 const struct sadb_msghdr *mhp;
7225 IPSEC_ASSERT(so != NULL, ("null socket"));
7226 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7227 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7228 IPSEC_ASSERT(mhp->msg != NULL, ("null msg"));
7230 olen = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7232 if (olen < sizeof(struct sadb_msg)) {
7234 return key_senderror(so, m, EINVAL);
7239 } else if (olen == sizeof(struct sadb_msg)) {
7240 /* enable/disable promisc mode */
7243 if ((kp = (struct keycb *)sotorawcb(so)) == NULL)
7244 return key_senderror(so, m, EINVAL);
7245 mhp->msg->sadb_msg_errno = 0;
7246 switch (mhp->msg->sadb_msg_satype) {
7249 kp->kp_promisc = mhp->msg->sadb_msg_satype;
7252 return key_senderror(so, m, EINVAL);
7255 /* send the original message back to everyone */
7256 mhp->msg->sadb_msg_errno = 0;
7257 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7259 /* send packet as is */
7261 m_adj(m, PFKEY_ALIGN8(sizeof(struct sadb_msg)));
7263 /* TODO: if sadb_msg_seq is specified, send to specific pid */
7264 return key_sendup_mbuf(so, m, KEY_SENDUP_ALL);
7268 static int (*key_typesw[]) __P((struct socket *, struct mbuf *,
7269 const struct sadb_msghdr *)) = {
7270 NULL, /* SADB_RESERVED */
7271 key_getspi, /* SADB_GETSPI */
7272 key_update, /* SADB_UPDATE */
7273 key_add, /* SADB_ADD */
7274 key_delete, /* SADB_DELETE */
7275 key_get, /* SADB_GET */
7276 key_acquire2, /* SADB_ACQUIRE */
7277 key_register, /* SADB_REGISTER */
7278 NULL, /* SADB_EXPIRE */
7279 key_flush, /* SADB_FLUSH */
7280 key_dump, /* SADB_DUMP */
7281 key_promisc, /* SADB_X_PROMISC */
7282 NULL, /* SADB_X_PCHANGE */
7283 key_spdadd, /* SADB_X_SPDUPDATE */
7284 key_spdadd, /* SADB_X_SPDADD */
7285 key_spddelete, /* SADB_X_SPDDELETE */
7286 key_spdget, /* SADB_X_SPDGET */
7287 NULL, /* SADB_X_SPDACQUIRE */
7288 key_spddump, /* SADB_X_SPDDUMP */
7289 key_spdflush, /* SADB_X_SPDFLUSH */
7290 key_spdadd, /* SADB_X_SPDSETIDX */
7291 NULL, /* SADB_X_SPDEXPIRE */
7292 key_spddelete2, /* SADB_X_SPDDELETE2 */
7296 * parse sadb_msg buffer to process PFKEYv2,
7297 * and create a data to response if needed.
7298 * I think to be dealed with mbuf directly.
7300 * msgp : pointer to pointer to a received buffer pulluped.
7301 * This is rewrited to response.
7302 * so : pointer to socket.
7304 * length for buffer to send to user process.
7311 struct sadb_msg *msg;
7312 struct sadb_msghdr mh;
7317 IPSEC_ASSERT(so != NULL, ("null socket"));
7318 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7320 #if 0 /*kdebug_sadb assumes msg in linear buffer*/
7321 KEYDEBUG(KEYDEBUG_KEY_DUMP,
7322 ipseclog((LOG_DEBUG, "%s: passed sadb_msg\n", __func__));
7326 if (m->m_len < sizeof(struct sadb_msg)) {
7327 m = m_pullup(m, sizeof(struct sadb_msg));
7331 msg = mtod(m, struct sadb_msg *);
7332 orglen = PFKEY_UNUNIT64(msg->sadb_msg_len);
7333 target = KEY_SENDUP_ONE;
7335 if ((m->m_flags & M_PKTHDR) == 0 ||
7336 m->m_pkthdr.len != m->m_pkthdr.len) {
7337 ipseclog((LOG_DEBUG, "%s: invalid message length.\n",__func__));
7338 V_pfkeystat.out_invlen++;
7343 if (msg->sadb_msg_version != PF_KEY_V2) {
7344 ipseclog((LOG_DEBUG, "%s: PF_KEY version %u is mismatched.\n",
7345 __func__, msg->sadb_msg_version));
7346 V_pfkeystat.out_invver++;
7351 if (msg->sadb_msg_type > SADB_MAX) {
7352 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7353 __func__, msg->sadb_msg_type));
7354 V_pfkeystat.out_invmsgtype++;
7359 /* for old-fashioned code - should be nuked */
7360 if (m->m_pkthdr.len > MCLBYTES) {
7367 MGETHDR(n, M_DONTWAIT, MT_DATA);
7368 if (n && m->m_pkthdr.len > MHLEN) {
7369 MCLGET(n, M_DONTWAIT);
7370 if ((n->m_flags & M_EXT) == 0) {
7379 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
7380 n->m_pkthdr.len = n->m_len = m->m_pkthdr.len;
7386 /* align the mbuf chain so that extensions are in contiguous region. */
7387 error = key_align(m, &mh);
7394 switch (msg->sadb_msg_satype) {
7395 case SADB_SATYPE_UNSPEC:
7396 switch (msg->sadb_msg_type) {
7404 ipseclog((LOG_DEBUG, "%s: must specify satype "
7405 "when msg type=%u.\n", __func__,
7406 msg->sadb_msg_type));
7407 V_pfkeystat.out_invsatype++;
7412 case SADB_SATYPE_AH:
7413 case SADB_SATYPE_ESP:
7414 case SADB_X_SATYPE_IPCOMP:
7415 case SADB_X_SATYPE_TCPSIGNATURE:
7416 switch (msg->sadb_msg_type) {
7418 case SADB_X_SPDDELETE:
7420 case SADB_X_SPDDUMP:
7421 case SADB_X_SPDFLUSH:
7422 case SADB_X_SPDSETIDX:
7423 case SADB_X_SPDUPDATE:
7424 case SADB_X_SPDDELETE2:
7425 ipseclog((LOG_DEBUG, "%s: illegal satype=%u\n",
7426 __func__, msg->sadb_msg_type));
7427 V_pfkeystat.out_invsatype++;
7432 case SADB_SATYPE_RSVP:
7433 case SADB_SATYPE_OSPFV2:
7434 case SADB_SATYPE_RIPV2:
7435 case SADB_SATYPE_MIP:
7436 ipseclog((LOG_DEBUG, "%s: type %u isn't supported.\n",
7437 __func__, msg->sadb_msg_satype));
7438 V_pfkeystat.out_invsatype++;
7441 case 1: /* XXX: What does it do? */
7442 if (msg->sadb_msg_type == SADB_X_PROMISC)
7446 ipseclog((LOG_DEBUG, "%s: invalid type %u is passed.\n",
7447 __func__, msg->sadb_msg_satype));
7448 V_pfkeystat.out_invsatype++;
7453 /* check field of upper layer protocol and address family */
7454 if (mh.ext[SADB_EXT_ADDRESS_SRC] != NULL
7455 && mh.ext[SADB_EXT_ADDRESS_DST] != NULL) {
7456 struct sadb_address *src0, *dst0;
7459 src0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_SRC]);
7460 dst0 = (struct sadb_address *)(mh.ext[SADB_EXT_ADDRESS_DST]);
7462 /* check upper layer protocol */
7463 if (src0->sadb_address_proto != dst0->sadb_address_proto) {
7464 ipseclog((LOG_DEBUG, "%s: upper layer protocol "
7465 "mismatched.\n", __func__));
7466 V_pfkeystat.out_invaddr++;
7472 if (PFKEY_ADDR_SADDR(src0)->sa_family !=
7473 PFKEY_ADDR_SADDR(dst0)->sa_family) {
7474 ipseclog((LOG_DEBUG, "%s: address family mismatched.\n",
7476 V_pfkeystat.out_invaddr++;
7480 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7481 PFKEY_ADDR_SADDR(dst0)->sa_len) {
7482 ipseclog((LOG_DEBUG, "%s: address struct size "
7483 "mismatched.\n", __func__));
7484 V_pfkeystat.out_invaddr++;
7489 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7491 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7492 sizeof(struct sockaddr_in)) {
7493 V_pfkeystat.out_invaddr++;
7499 if (PFKEY_ADDR_SADDR(src0)->sa_len !=
7500 sizeof(struct sockaddr_in6)) {
7501 V_pfkeystat.out_invaddr++;
7507 ipseclog((LOG_DEBUG, "%s: unsupported address family\n",
7509 V_pfkeystat.out_invaddr++;
7510 error = EAFNOSUPPORT;
7514 switch (PFKEY_ADDR_SADDR(src0)->sa_family) {
7516 plen = sizeof(struct in_addr) << 3;
7519 plen = sizeof(struct in6_addr) << 3;
7522 plen = 0; /*fool gcc*/
7526 /* check max prefix length */
7527 if (src0->sadb_address_prefixlen > plen ||
7528 dst0->sadb_address_prefixlen > plen) {
7529 ipseclog((LOG_DEBUG, "%s: illegal prefixlen.\n",
7531 V_pfkeystat.out_invaddr++;
7537 * prefixlen == 0 is valid because there can be a case when
7538 * all addresses are matched.
7542 if (msg->sadb_msg_type >= sizeof(key_typesw)/sizeof(key_typesw[0]) ||
7543 key_typesw[msg->sadb_msg_type] == NULL) {
7544 V_pfkeystat.out_invmsgtype++;
7549 return (*key_typesw[msg->sadb_msg_type])(so, m, &mh);
7552 msg->sadb_msg_errno = error;
7553 return key_sendup_mbuf(so, m, target);
7557 key_senderror(so, m, code)
7562 struct sadb_msg *msg;
7564 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7565 ("mbuf too small, len %u", m->m_len));
7567 msg = mtod(m, struct sadb_msg *);
7568 msg->sadb_msg_errno = code;
7569 return key_sendup_mbuf(so, m, KEY_SENDUP_ONE);
7573 * set the pointer to each header into message buffer.
7574 * m will be freed on error.
7575 * XXX larger-than-MCLBYTES extension?
7580 struct sadb_msghdr *mhp;
7583 struct sadb_ext *ext;
7588 IPSEC_ASSERT(m != NULL, ("null mbuf"));
7589 IPSEC_ASSERT(mhp != NULL, ("null msghdr"));
7590 IPSEC_ASSERT(m->m_len >= sizeof(struct sadb_msg),
7591 ("mbuf too small, len %u", m->m_len));
7594 bzero(mhp, sizeof(*mhp));
7596 mhp->msg = mtod(m, struct sadb_msg *);
7597 mhp->ext[0] = (struct sadb_ext *)mhp->msg; /*XXX backward compat */
7599 end = PFKEY_UNUNIT64(mhp->msg->sadb_msg_len);
7600 extlen = end; /*just in case extlen is not updated*/
7601 for (off = sizeof(struct sadb_msg); off < end; off += extlen) {
7602 n = m_pulldown(m, off, sizeof(struct sadb_ext), &toff);
7604 /* m is already freed */
7607 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7610 switch (ext->sadb_ext_type) {
7612 case SADB_EXT_ADDRESS_SRC:
7613 case SADB_EXT_ADDRESS_DST:
7614 case SADB_EXT_ADDRESS_PROXY:
7615 case SADB_EXT_LIFETIME_CURRENT:
7616 case SADB_EXT_LIFETIME_HARD:
7617 case SADB_EXT_LIFETIME_SOFT:
7618 case SADB_EXT_KEY_AUTH:
7619 case SADB_EXT_KEY_ENCRYPT:
7620 case SADB_EXT_IDENTITY_SRC:
7621 case SADB_EXT_IDENTITY_DST:
7622 case SADB_EXT_SENSITIVITY:
7623 case SADB_EXT_PROPOSAL:
7624 case SADB_EXT_SUPPORTED_AUTH:
7625 case SADB_EXT_SUPPORTED_ENCRYPT:
7626 case SADB_EXT_SPIRANGE:
7627 case SADB_X_EXT_POLICY:
7628 case SADB_X_EXT_SA2:
7630 case SADB_X_EXT_NAT_T_TYPE:
7631 case SADB_X_EXT_NAT_T_SPORT:
7632 case SADB_X_EXT_NAT_T_DPORT:
7633 case SADB_X_EXT_NAT_T_OAI:
7634 case SADB_X_EXT_NAT_T_OAR:
7635 case SADB_X_EXT_NAT_T_FRAG:
7637 /* duplicate check */
7639 * XXX Are there duplication payloads of either
7640 * KEY_AUTH or KEY_ENCRYPT ?
7642 if (mhp->ext[ext->sadb_ext_type] != NULL) {
7643 ipseclog((LOG_DEBUG, "%s: duplicate ext_type "
7644 "%u\n", __func__, ext->sadb_ext_type));
7646 V_pfkeystat.out_dupext++;
7651 ipseclog((LOG_DEBUG, "%s: invalid ext_type %u\n",
7652 __func__, ext->sadb_ext_type));
7654 V_pfkeystat.out_invexttype++;
7658 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
7660 if (key_validate_ext(ext, extlen)) {
7662 V_pfkeystat.out_invlen++;
7666 n = m_pulldown(m, off, extlen, &toff);
7668 /* m is already freed */
7671 ext = (struct sadb_ext *)(mtod(n, caddr_t) + toff);
7673 mhp->ext[ext->sadb_ext_type] = ext;
7674 mhp->extoff[ext->sadb_ext_type] = off;
7675 mhp->extlen[ext->sadb_ext_type] = extlen;
7680 V_pfkeystat.out_invlen++;
7688 key_validate_ext(ext, len)
7689 const struct sadb_ext *ext;
7692 const struct sockaddr *sa;
7693 enum { NONE, ADDR } checktype = NONE;
7695 const int sal = offsetof(struct sockaddr, sa_len) + sizeof(sa->sa_len);
7697 if (len != PFKEY_UNUNIT64(ext->sadb_ext_len))
7700 /* if it does not match minimum/maximum length, bail */
7701 if (ext->sadb_ext_type >= sizeof(minsize) / sizeof(minsize[0]) ||
7702 ext->sadb_ext_type >= sizeof(maxsize) / sizeof(maxsize[0]))
7704 if (!minsize[ext->sadb_ext_type] || len < minsize[ext->sadb_ext_type])
7706 if (maxsize[ext->sadb_ext_type] && len > maxsize[ext->sadb_ext_type])
7709 /* more checks based on sadb_ext_type XXX need more */
7710 switch (ext->sadb_ext_type) {
7711 case SADB_EXT_ADDRESS_SRC:
7712 case SADB_EXT_ADDRESS_DST:
7713 case SADB_EXT_ADDRESS_PROXY:
7714 baselen = PFKEY_ALIGN8(sizeof(struct sadb_address));
7717 case SADB_EXT_IDENTITY_SRC:
7718 case SADB_EXT_IDENTITY_DST:
7719 if (((const struct sadb_ident *)ext)->sadb_ident_type ==
7720 SADB_X_IDENTTYPE_ADDR) {
7721 baselen = PFKEY_ALIGN8(sizeof(struct sadb_ident));
7731 switch (checktype) {
7735 sa = (const struct sockaddr *)(((const u_int8_t*)ext)+baselen);
7736 if (len < baselen + sal)
7738 if (baselen + PFKEY_ALIGN8(sa->sa_len) != len)
7751 for (i = 0; i < IPSEC_DIR_MAX; i++)
7752 LIST_INIT(&V_sptree[i]);
7754 LIST_INIT(&V_sahtree);
7756 for (i = 0; i <= SADB_SATYPE_MAX; i++)
7757 LIST_INIT(&V_regtree[i]);
7759 LIST_INIT(&V_acqtree);
7760 LIST_INIT(&V_spacqtree);
7762 /* system default */
7763 V_ip4_def_policy.policy = IPSEC_POLICY_NONE;
7764 V_ip4_def_policy.refcnt++; /*never reclaim this*/
7766 if (!IS_DEFAULT_VNET(curvnet))
7770 REGTREE_LOCK_INIT();
7771 SAHTREE_LOCK_INIT();
7775 #ifndef IPSEC_DEBUG2
7776 timeout((void *)key_timehandler, (void *)0, hz);
7777 #endif /*IPSEC_DEBUG2*/
7779 /* initialize key statistics */
7780 keystat.getspi_count = 1;
7782 printf("IPsec: Initialized Security Association Processing.\n");
7789 struct secpolicy *sp, *nextsp;
7790 struct secacq *acq, *nextacq;
7791 struct secspacq *spacq, *nextspacq;
7792 struct secashead *sah, *nextsah;
7797 for (i = 0; i < IPSEC_DIR_MAX; i++) {
7798 for (sp = LIST_FIRST(&V_sptree[i]);
7799 sp != NULL; sp = nextsp) {
7800 nextsp = LIST_NEXT(sp, chain);
7801 if (__LIST_CHAINED(sp)) {
7802 LIST_REMOVE(sp, chain);
7803 free(sp, M_IPSEC_SP);
7810 for (sah = LIST_FIRST(&V_sahtree); sah != NULL; sah = nextsah) {
7811 nextsah = LIST_NEXT(sah, chain);
7812 if (__LIST_CHAINED(sah)) {
7813 LIST_REMOVE(sah, chain);
7814 free(sah, M_IPSEC_SAH);
7820 for (i = 0; i <= SADB_SATYPE_MAX; i++) {
7821 LIST_FOREACH(reg, &V_regtree[i], chain) {
7822 if (__LIST_CHAINED(reg)) {
7823 LIST_REMOVE(reg, chain);
7824 free(reg, M_IPSEC_SAR);
7832 for (acq = LIST_FIRST(&V_acqtree); acq != NULL; acq = nextacq) {
7833 nextacq = LIST_NEXT(acq, chain);
7834 if (__LIST_CHAINED(acq)) {
7835 LIST_REMOVE(acq, chain);
7836 free(acq, M_IPSEC_SAQ);
7842 for (spacq = LIST_FIRST(&V_spacqtree); spacq != NULL;
7843 spacq = nextspacq) {
7844 nextspacq = LIST_NEXT(spacq, chain);
7845 if (__LIST_CHAINED(spacq)) {
7846 LIST_REMOVE(spacq, chain);
7847 free(spacq, M_IPSEC_SAQ);
7855 * XXX: maybe This function is called after INBOUND IPsec processing.
7857 * Special check for tunnel-mode packets.
7858 * We must make some checks for consistency between inner and outer IP header.
7860 * xxx more checks to be provided
7863 key_checktunnelsanity(sav, family, src, dst)
7864 struct secasvar *sav;
7869 IPSEC_ASSERT(sav->sah != NULL, ("null SA header"));
7871 /* XXX: check inner IP header */
7876 /* record data transfer on SA, and update timestamps */
7878 key_sa_recordxfer(sav, m)
7879 struct secasvar *sav;
7882 IPSEC_ASSERT(sav != NULL, ("Null secasvar"));
7883 IPSEC_ASSERT(m != NULL, ("Null mbuf"));
7888 * XXX Currently, there is a difference of bytes size
7889 * between inbound and outbound processing.
7891 sav->lft_c->bytes += m->m_pkthdr.len;
7892 /* to check bytes lifetime is done in key_timehandler(). */
7895 * We use the number of packets as the unit of
7896 * allocations. We increment the variable
7897 * whenever {esp,ah}_{in,out}put is called.
7899 sav->lft_c->allocations++;
7900 /* XXX check for expires? */
7903 * NOTE: We record CURRENT usetime by using wall clock,
7904 * in seconds. HARD and SOFT lifetime are measured by the time
7905 * difference (again in seconds) from usetime.
7909 * -----+-----+--------+---> t
7910 * <--------------> HARD
7913 sav->lft_c->usetime = time_second;
7914 /* XXX check for expires? */
7921 key_sa_routechange(dst)
7922 struct sockaddr *dst;
7924 struct secashead *sah;
7928 LIST_FOREACH(sah, &V_sahtree, chain) {
7929 ro = &sah->sa_route;
7930 if (ro->ro_rt && dst->sa_len == ro->ro_dst.sa_len
7931 && bcmp(dst, &ro->ro_dst, dst->sa_len) == 0) {
7933 ro->ro_rt = (struct rtentry *)NULL;
7940 key_sa_chgstate(struct secasvar *sav, u_int8_t state)
7942 IPSEC_ASSERT(sav != NULL, ("NULL sav"));
7943 SAHTREE_LOCK_ASSERT();
7945 if (sav->state != state) {
7946 if (__LIST_CHAINED(sav))
7947 LIST_REMOVE(sav, chain);
7949 LIST_INSERT_HEAD(&sav->sah->savtree[state], sav, chain);
7955 struct secasvar *sav;
7958 IPSEC_ASSERT(sav->iv != NULL, ("null IV"));
7959 key_randomfill(sav->iv, sav->ivlen);
7963 static struct mbuf *
7967 struct mbuf *m = NULL, *n;
7972 MGET(n, M_DONTWAIT, MT_DATA);
7973 if (n && len > MLEN)
7974 MCLGET(n, M_DONTWAIT);
7982 n->m_len = M_TRAILINGSPACE(n);
7983 /* use the bottom of mbuf, hoping we can prepend afterwards */
7984 if (n->m_len > len) {
7985 t = (n->m_len - len) & ~(sizeof(long) - 1);
8002 * Take one of the kernel's security keys and convert it into a PF_KEY
8003 * structure within an mbuf, suitable for sending up to a waiting
8004 * application in user land.
8007 * src: A pointer to a kernel security key.
8008 * exttype: Which type of key this is. Refer to the PF_KEY data structures.
8010 * a valid mbuf or NULL indicating an error
8014 static struct mbuf *
8015 key_setkey(struct seckey *src, u_int16_t exttype)
8024 len = PFKEY_ALIGN8(sizeof(struct sadb_key) + _KEYLEN(src));
8025 m = key_alloc_mbuf(len);
8028 p = mtod(m, struct sadb_key *);
8030 p->sadb_key_len = PFKEY_UNIT64(len);
8031 p->sadb_key_exttype = exttype;
8032 p->sadb_key_bits = src->bits;
8033 bcopy(src->key_data, _KEYBUF(p), _KEYLEN(src));
8039 * Take one of the kernel's lifetime data structures and convert it
8040 * into a PF_KEY structure within an mbuf, suitable for sending up to
8041 * a waiting application in user land.
8044 * src: A pointer to a kernel lifetime structure.
8045 * exttype: Which type of lifetime this is. Refer to the PF_KEY
8046 * data structures for more information.
8048 * a valid mbuf or NULL indicating an error
8052 static struct mbuf *
8053 key_setlifetime(struct seclifetime *src, u_int16_t exttype)
8055 struct mbuf *m = NULL;
8056 struct sadb_lifetime *p;
8057 int len = PFKEY_ALIGN8(sizeof(struct sadb_lifetime));
8062 m = key_alloc_mbuf(len);
8065 p = mtod(m, struct sadb_lifetime *);
8068 p->sadb_lifetime_len = PFKEY_UNIT64(len);
8069 p->sadb_lifetime_exttype = exttype;
8070 p->sadb_lifetime_allocations = src->allocations;
8071 p->sadb_lifetime_bytes = src->bytes;
8072 p->sadb_lifetime_addtime = src->addtime;
8073 p->sadb_lifetime_usetime = src->usetime;